Implemented equalizer
[someplayer] / src / dbstorage.cpp
index 03db3fd..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, length 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);");
+                                                               "(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, 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);");
+                                                               "(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, 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");
+                                                               "(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, 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");
+                                                               "(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, 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)");
+                                                               "(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");
@@ -83,35 +112,38 @@ void DbStorage::_prepare_queries() {
 
        _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, "
-                                                                               "length 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() {
@@ -122,6 +154,7 @@ 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;
@@ -131,6 +164,7 @@ DbStorage::~DbStorage() {
        delete _insert_track_query;
        delete _insert_favorites_query;
        delete _update_track_count_query;
+       delete _remove_track_query;
        db.close();
 }
 
@@ -333,10 +367,39 @@ void DbStorage::addToFavorites(Track track) {
 }
 
 void DbStorage::updateTrackCount(Track track) {
-       QSqlQuery *query = _update_track_count_query;
-       query->bindValue(":count", track.count());
+       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();
+       }
+}
+
+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) {