X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fdbstorage.cpp;h=dac28681c2e1c380f57a986267748ff9f9a54520;hb=f9685f48a0d412250d81fa0ed67054d8978f4a21;hp=7c6af3ae584c418cdae8cb2ddbdf877447066646;hpb=7b2a40e92ca217556f2ea5dbb95248b2800909a1;p=someplayer diff --git a/src/dbstorage.cpp b/src/dbstorage.cpp index 7c6af3a..dac2868 100644 --- a/src/dbstorage.cpp +++ b/src/dbstorage.cpp @@ -1,3 +1,22 @@ +/* + * SomePlayer - An alternate music player for Maemo 5 + * Copyright (C) 2010 Nikolay (somebody) Tischenko + * + * 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 #include @@ -25,38 +44,42 @@ void DbStorage::_prepare_queries() { _get_albums_for_artist_query->prepare("SELECT name FROM album WHERE artist_id in (SELECT id from artist WHERE name = :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 " + _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 album WHERE name = :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 " + _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 " + _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 " + _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 " + _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"); + _check_artist_query = new QSqlQuery(db); _check_artist_query->prepare("SELECT id FROM artist WHERE name = :name"); @@ -73,10 +96,16 @@ 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"); } void DbStorage::_create_database_structure() { @@ -95,6 +124,7 @@ void DbStorage::_create_database_structure() { "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) " ");"); @@ -115,6 +145,15 @@ DbStorage::~DbStorage() { delete _get_never_played_query; delete _get_recently_added_query; delete _get_tracks_for_album_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; db.close(); } @@ -153,7 +192,8 @@ QList 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 +213,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 +235,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 +257,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 +279,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); @@ -282,6 +326,7 @@ void DbStorage::addTrack(Track track) { 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; @@ -304,10 +349,23 @@ 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::updateTrack(Track) { +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(); + } } int DbStorage::_check_add_artist(QString artist) {