Implemented equalizer
[someplayer] / src / dbstorage.cpp
index 95cc980..aba866c 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * SomePlayer - An alternate music player for Maemo 5
+ * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
 #include "dbstorage.h"
 #include <QSqlQuery>
 #include <QSqlResult>
@@ -19,49 +38,59 @@ DbStorage::DbStorage(QString path) {
 
 void DbStorage::_prepare_queries() {
        _get_artists_query = new QSqlQuery(db);
-       _get_artists_query->prepare("SELECT name FROM artist");
+       _get_artists_query->prepare("SELECT name FROM artist ORDER BY name");
 
        _get_albums_for_artist_query = new QSqlQuery(db);
-       _get_albums_for_artist_query->prepare("SELECT name FROM album WHERE artist_id in (SELECT id from artist WHERE name = :name);");
+       _get_albums_for_artist_query->prepare("SELECT name FROM album WHERE artist_id in (SELECT id from artist WHERE UPPER(name) = UPPER(:name)) ORDER BY name;");
 
        _get_tracks_for_album_query = new QSqlQuery(db);
-       _get_tracks_for_album_query->prepare("SELECT id, title, source, count FROM tracks WHERE artist_id IN "
-                                                                                "(SELECT id FROM artist WHERE name = :artist_name) AND album_id IN "
-                                                                                "(SELECT id FROM album WHERE name =: album_name);");
+       _get_tracks_for_album_query->prepare("SELECT id, title, source, count, length FROM tracks WHERE artist_id IN "
+                                                               "(SELECT id FROM artist WHERE UPPER(name) = UPPER(:artist_name)) AND album_id IN "
+                                                               "(SELECT id FROM album WHERE UPPER(name) = UPPER(:album_name));");
 
        _get_favorites_query = new QSqlQuery(db);
-       _get_favorites_query->prepare("SELECT track_id as id, title, artist, album.name as album, source, count FROM "
-                                                                 "(SELECT tracks.id AS track_id, artist.name AS artist, title, count, source, tracks.album_id FROM "
-                                                                 "tracks JOIN artist ON tracks.artist_id = artist.id) "
-                                                                 "JOIN album ON album_id = album.id WHERE track_id IN "
-                                                                 "(SELECT track_id FROM favorites);");
+       _get_favorites_query->prepare("SELECT track_id as id, title, artist, album.name as album, source, count, length FROM "
+                                                               "(SELECT tracks.id AS track_id, artist.name AS artist, title, count, source, tracks.album_id, length FROM "
+                                                               "tracks JOIN artist ON tracks.artist_id = artist.id) "
+                                                               "JOIN album ON album_id = album.id WHERE track_id IN "
+                                                               "(SELECT track_id FROM favorites);");
 
        _get_most_played_query = new QSqlQuery(db);
-       _get_most_played_query->prepare("SELECT track_id as id, title, artist, album.name as album, source, count FROM "
-                                                                       "(SELECT tracks.id AS track_id, artist.name AS artist, title, count, source, tracks.album_id FROM "
-                                                                       "tracks JOIN artist ON tracks.artist_id = artist.id) "
-                                                                       "JOIN album ON album_id = album.id ORDER BY count DESC "
-                                                                       "LIMIT 0, :max");
+       _get_most_played_query->prepare("SELECT track_id as id, title, artist, album.name as album, source, count, length FROM "
+                                                               "(SELECT tracks.id AS track_id, artist.name AS artist, title, count, source, tracks.album_id, length FROM "
+                                                               "tracks JOIN artist ON tracks.artist_id = artist.id) "
+                                                               "JOIN album ON album_id = album.id ORDER BY count DESC "
+                                                               "LIMIT 0, :max");
 
        _get_never_played_query = new QSqlQuery(db);
-       _get_never_played_query->prepare("SELECT track_id as id, title, artist, album.name as album, source, count FROM "
-                                                                        "(SELECT tracks.id AS track_id, artist.name AS artist, title, count, source, tracks.album_id FROM "
-                                                                        "tracks JOIN artist ON tracks.artist_id = artist.id) "
-                                                                        "JOIN album ON album_id = album.id "
-                                                                        "WHERE count = 0");
+       _get_never_played_query->prepare("SELECT track_id as id, title, artist, album.name as album, source, count, length FROM "
+                                                               "(SELECT tracks.id AS track_id, artist.name AS artist, title, count, source, tracks.album_id, length FROM "
+                                                               "tracks JOIN artist ON tracks.artist_id = artist.id) "
+                                                               "JOIN album ON album_id = album.id "
+                                                               "WHERE count = 0");
 
        _get_recently_added_query = new QSqlQuery(db);
-       _get_recently_added_query->prepare("SELECT track_id as id, title, artist, album.name as album, source, count FROM "
-                                                                          "(SELECT tracks.id AS track_id, artist.name AS artist, title, count, source, tracks.album_id FROM "
-                                                                          "tracks JOIN artist ON tracks.artist_id = artist.id) "
-                                                                          "JOIN album ON album_id = album.id "
-                                                                          "WHERE track_id IN "
-                                                                          "(SELECT track_id FROM adding_date ORDER BY date DESC LIMIT 0, :max)");
+       _get_recently_added_query->prepare("SELECT track_id as id, title, artist, album.name as album, source, count, length FROM "
+                                                               "(SELECT tracks.id AS track_id, artist.name AS artist, title, count, source, tracks.album_id, length FROM "
+                                                               "tracks JOIN artist ON tracks.artist_id = artist.id) "
+                                                               "JOIN album ON album_id = album.id "
+                                                               "WHERE track_id IN "
+                                                               "(SELECT track_id FROM adding_date ORDER BY date DESC LIMIT 0, :max)");
+
+       _get_track_count = new QSqlQuery(db);
+       _get_track_count->prepare("SELECT count from tracks WHERE id = :id");
+
+       _get_track_by_source_query = new QSqlQuery(db);
+       _get_track_by_source_query->prepare("SELECT track_id AS id, title, artist, album.name AS album, source, count, length FROM "
+                                                               "(SELECT tracks.id AS track_id, artist.name AS artist, title, count, tracks.album_id, length FROM "
+                                                               "tracks JOIN artist ON tracks.artist_id = artist.id AND source = :source) "
+                                                               "JOIN album ON album_id = album.id LIMIT 1");
+
        _check_artist_query = new QSqlQuery(db);
-       _check_artist_query->prepare("SELECT id FROM artist WHERE name = :name");
+       _check_artist_query->prepare("SELECT id FROM artist WHERE UPPER(name) = UPPER(:name)");
 
        _check_album_query = new QSqlQuery(db);
-       _check_album_query->prepare("SELECT id FROM album WHERE name = :name AND artist_id = :artist_id");
+       _check_album_query->prepare("SELECT id FROM album WHERE UPPER(name) = UPPER(:name) AND artist_id = :artist_id");
 
        _check_track_query = new QSqlQuery(db);
        _check_track_query->prepare("SELECT id FROM tracks WHERE source = :source");
@@ -73,38 +102,48 @@ void DbStorage::_prepare_queries() {
        _insert_album_query->prepare("INSERT INTO album (name, artist_id) values (:name, :artist_id)");
 
        _insert_track_query = new QSqlQuery(db);
-       _insert_track_query->prepare("INSERT INTO tracks (title, artist_id, album_id, source) values (:title, :artist_id, :album_id, :source)");
+       _insert_track_query->prepare("INSERT INTO tracks (title, artist_id, album_id, source, length) values (:title, :artist_id, :album_id, :source, :length)");
 
        _insert_date_query = new QSqlQuery(db);
        _insert_date_query->prepare("INSERT INTO adding_date (track_id, date) values (:track_id, strftime('%s', 'now'))");
+
+       _insert_favorites_query = new QSqlQuery(db);
+       _insert_favorites_query->prepare("INSERT INTO favorites (track_id) values (:track_id)");
+
+       _update_track_count_query = new QSqlQuery(db);
+       _update_track_count_query->prepare("UPDATE tracks SET count = :count where id = :id");
+
+       _remove_track_query = new QSqlQuery(db);
+       _remove_track_query->prepare("DELETE FROM tracks WHERE id = :id");
 }
 
 void DbStorage::_create_database_structure() {
        QSqlQuery *query = new QSqlQuery(db);
        query->exec("create table artist (id integer primary key, "
-                                                                               "name text "
-                                                                               ");");
+                                                               "name text "
+                                                               ");");
        query->exec("create table album (id integer primary key, "
-                                                                               "artist_id integer, "
-                                                                               "name text, "
-                                                                               "foreign key(artist_id) references arist(id) "
-                                                                               ");");
+                                                               "artist_id integer, "
+                                                               "name text, "
+                                                               "foreign key(artist_id) references arist(id) "
+                                                               ");");
        query->exec("create table tracks (id integer primary key, "
-                                                                               "artist_id integer, "
-                                                                               "album_id integer, "
-                                                                               "title text, "
-                                                                               "source text, "
-                                                                               "count integer default 0, "
-                                                                               "foreign key(artist_id) references artist(id), "
-                                                                               "foreign key(album_id) references album(id) "
-                                                                               ");");
+                                                               "artist_id integer, "
+                                                               "album_id integer, "
+                                                               "title text, "
+                                                               "source text, "
+                                                               "count integer default 0, "
+                                                               "length integer default 0, "
+                                                               "foreign key(artist_id) references artist(id), "
+                                                               "foreign key(album_id) references album(id) "
+                                                               ");");
        query->exec("create table favorites (track_id integer, "
-                                                                               "foreign key(track_id) references tracks(id) "
-                                                                               ");");
+                                                               "foreign key(track_id) references tracks(id) "
+                                                               ");");
        query->exec("create table adding_date (track_id integer, "
-                                                                               "date integer, "
-                                                                               "foreign key(track_id) references tracks(id) "
-                                                                               ");");
+                                                               "date integer, "
+                                                               "foreign key(track_id) references tracks(id) "
+                                                               ");");
 }
 
 DbStorage::~DbStorage() {
@@ -115,6 +154,17 @@ DbStorage::~DbStorage() {
        delete _get_never_played_query;
        delete _get_recently_added_query;
        delete _get_tracks_for_album_query;
+       delete _get_track_by_source_query;
+       delete _check_album_query;
+       delete _check_artist_query;
+       delete _check_track_query;
+       delete _insert_album_query;
+       delete _insert_artist_query;
+       delete _insert_date_query;
+       delete _insert_track_query;
+       delete _insert_favorites_query;
+       delete _update_track_count_query;
+       delete _remove_track_query;
        db.close();
 }
 
@@ -153,7 +203,8 @@ QList<Track> DbStorage::getTracksForAlbum(QString album, QString artist) {
                QString title = query->value(1).toString();
                QString source = query->value(2).toString();
                int count = query->value(3).toInt();
-               TrackMetadata meta (title, artist, album);
+               int length = query->value(4).toInt();
+               TrackMetadata meta (title, artist, album, length);
                Track track(id, meta, source);
                track.setCount(count);
                tracks.append(track);
@@ -173,7 +224,8 @@ Playlist DbStorage::getFavorites() {
                QString album = query->value(3).toString();
                QString source = query->value(4).toString();
                int count = query->value(5).toInt();
-               TrackMetadata meta(title, artist, album);
+               int length = query->value(6).toInt();
+               TrackMetadata meta(title, artist, album, length);
                Track track(id, meta, source);
                track.setCount(count);
                playlist.addTrack(track);
@@ -194,7 +246,8 @@ Playlist DbStorage::getMostPlayed() {
                QString album = query->value(3).toString();
                QString source = query->value(4).toString();
                int count = query->value(5).toInt();
-               TrackMetadata meta(title, artist, album);
+               int length = query->value(6).toInt();
+               TrackMetadata meta(title, artist, album, length);
                Track track(id, meta, source);
                track.setCount(count);
                playlist.addTrack(track);
@@ -215,7 +268,8 @@ Playlist DbStorage::getNeverPlayed() {
                QString album = query->value(3).toString();
                QString source = query->value(4).toString();
                int count = query->value(5).toInt();
-               TrackMetadata meta(title, artist, album);
+               int length = query->value(6).toInt();
+               TrackMetadata meta(title, artist, album, length);
                Track track(id, meta, source);
                track.setCount(count);
                playlist.addTrack(track);
@@ -236,7 +290,8 @@ Playlist DbStorage::getRecentlyAdded() {
                QString album = query->value(3).toString();
                QString source = query->value(4).toString();
                int count = query->value(5).toInt();
-               TrackMetadata meta(title, artist, album);
+               int length = query->value(6).toInt();
+               TrackMetadata meta(title, artist, album, length);
                Track track(id, meta, source);
                track.setCount(count);
                playlist.addTrack(track);
@@ -270,11 +325,19 @@ void DbStorage::addTrack(Track track) {
                //big bang
                return;
        }
-       QSqlQuery* query = _insert_track_query;
+       QSqlQuery *query = _check_track_query;
+       query->bindValue(":source", source);
+       query->exec();
+       if (query->next()) {
+               // already in datebase, skip
+               return;
+       }
+       query = _insert_track_query;
        query->bindValue(":title", title);
        query->bindValue(":artist_id", artist_id);
        query->bindValue(":album_id", album_id);
        query->bindValue(":source", source);
+       query->bindValue(":length", track.metadata().length());
        if (query->exec()) {
                //ok
                query = _check_track_query;
@@ -297,10 +360,46 @@ void DbStorage::addTrack(Track track) {
        }
 }
 
-void DbStorage::addToFavorites(Track) {
+void DbStorage::addToFavorites(Track track) {
+       QSqlQuery *query = _insert_favorites_query;
+       query->bindValue(":track_id", track.id());
+       query->exec();
+}
+
+void DbStorage::updateTrackCount(Track track) {
+       QSqlQuery *query = _get_track_count;
+       query->bindValue(":id", track.id());
+       query->exec();
+       if (query->next()) {
+               int count = query->value(0).toInt();
+               query = _update_track_count_query;
+               query->bindValue(":count", count+1);
+               query->bindValue(":id", track.id());
+               query->exec();
+       }
 }
 
-void DbStorage::updateTrack(Track) {
+Track DbStorage::updateTrack(Track track) {
+       QSqlQuery *query = _remove_track_query;
+       query->bindValue(":id", track.id());
+       addTrack(track);
+       query = _get_track_by_source_query;
+       query->bindValue(":source", track.source());
+       query->exec();
+       if (query->next()) {
+               int id = query->value(0).toInt();
+               QString title = query->value(1).toString();
+               QString artist = query->value(2).toString();
+               QString album = query->value(3).toString();
+               QString source = query->value(4).toString();
+               int count = query->value(5).toInt();
+               int length = query->value(6).toInt();
+               TrackMetadata meta(title, artist, album, length);
+               Track ntrack(id, meta, source);
+               ntrack.setCount(count);
+               return ntrack;
+       }
+       return track;
 }
 
 int DbStorage::_check_add_artist(QString artist) {