From b32b84f8c77652ec505a74c1566215c60ece8bf3 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 19 Oct 2011 02:46:28 +0200 Subject: [PATCH] * Properly deal with auto-creating artists & albums. --- .../database/databasecommand_addfiles.cpp | 9 +-- src/libtomahawk/database/databaseimpl.cpp | 75 +++++++++---------- src/libtomahawk/database/databaseimpl.h | 6 +- src/libtomahawk/widgets/whatshotwidget.cpp | 14 +--- 4 files changed, 47 insertions(+), 57 deletions(-) diff --git a/src/libtomahawk/database/databasecommand_addfiles.cpp b/src/libtomahawk/database/databasecommand_addfiles.cpp index bebf7c72e..d0680bb18 100644 --- a/src/libtomahawk/database/databasecommand_addfiles.cpp +++ b/src/libtomahawk/database/databasecommand_addfiles.cpp @@ -160,16 +160,13 @@ DatabaseCommand_AddFiles::exec( DatabaseImpl* dbi ) if( !source()->isLocal() ) url = QString( "servent://%1\t%2" ).arg( source()->userName() ).arg( url ); - bool autoCreate = true; - artistid = dbi->artistId( artist, autoCreate ); + artistid = dbi->artistId( artist, true ); if ( artistid < 1 ) continue; - autoCreate = true; // artistId overwrites autoCreate (reference) - trackid = dbi->trackId( artistid, track, autoCreate ); + trackid = dbi->trackId( artistid, track, true ); if ( trackid < 1 ) continue; - autoCreate = true; // trackId overwrites autoCreate (reference) - albumid = dbi->albumId( artistid, album, autoCreate ); + albumid = dbi->albumId( artistid, album, true ); // Now add the association query_filejoin.bindValue( 0, fileid ); diff --git a/src/libtomahawk/database/databaseimpl.cpp b/src/libtomahawk/database/databaseimpl.cpp index 311e0c7c4..9c36e5986 100644 --- a/src/libtomahawk/database/databaseimpl.cpp +++ b/src/libtomahawk/database/databaseimpl.cpp @@ -259,9 +259,8 @@ DatabaseImpl::file( int fid ) int -DatabaseImpl::artistId( const QString& name_orig, bool& autoCreate ) +DatabaseImpl::artistId( const QString& name_orig, bool autoCreate ) { - bool isnew = false; if ( m_lastart == name_orig ) return m_lastartid; @@ -296,20 +295,17 @@ DatabaseImpl::artistId( const QString& name_orig, bool& autoCreate ) } id = query.lastInsertId().toInt(); - isnew = true; m_lastart = name_orig; m_lastartid = id; } - autoCreate = isnew; return id; } int -DatabaseImpl::trackId( int artistid, const QString& name_orig, bool& isnew ) +DatabaseImpl::trackId( int artistid, const QString& name_orig, bool autoCreate ) { - isnew = false; int id = 0; QString sortname = DatabaseImpl::sortname( name_orig ); //if( ( id = m_artistcache[sortname] ) ) return id; @@ -320,45 +316,46 @@ DatabaseImpl::trackId( int artistid, const QString& name_orig, bool& isnew ) query.addBindValue( sortname ); query.exec(); - if( query.next() ) + if ( query.next() ) { id = query.value( 0 ).toInt(); } - if( id ) + if ( id ) { //m_trackcache[sortname]=id; return id; } - // not found, insert it. - query.prepare( "INSERT INTO track(id,artist,name,sortname) VALUES(NULL,?,?,?)" ); - query.addBindValue( artistid ); - query.addBindValue( name_orig ); - query.addBindValue( sortname ); - if( !query.exec() ) + if ( autoCreate ) { - tDebug() << "Failed to insert track:" << name_orig; - return 0; + // not found, insert it. + query.prepare( "INSERT INTO track(id,artist,name,sortname) VALUES(NULL,?,?,?)" ); + query.addBindValue( artistid ); + query.addBindValue( name_orig ); + query.addBindValue( sortname ); + if ( !query.exec() ) + { + tDebug() << "Failed to insert track:" << name_orig; + return 0; + } + + id = query.lastInsertId().toInt(); } - id = query.lastInsertId().toInt(); - //m_trackcache[sortname]=id; - isnew = true; return id; } int -DatabaseImpl::albumId( int artistid, const QString& name_orig, bool& isnew ) +DatabaseImpl::albumId( int artistid, const QString& name_orig, bool autoCreate ) { - isnew = false; - if( name_orig.isEmpty() ) + if ( name_orig.isEmpty() ) { //qDebug() << Q_FUNC_INFO << "empty album name"; return 0; } - if( m_lastartid == artistid && m_lastalb == name_orig ) + if ( m_lastartid == artistid && m_lastalb == name_orig ) return m_lastalbid; int id = 0; @@ -370,33 +367,35 @@ DatabaseImpl::albumId( int artistid, const QString& name_orig, bool& isnew ) query.addBindValue( artistid ); query.addBindValue( sortname ); query.exec(); - if( query.next() ) + if ( query.next() ) { id = query.value( 0 ).toInt(); } - if( id ) + if ( id ) { m_lastalb = name_orig; m_lastalbid = id; return id; } - // not found, insert it. - query.prepare( "INSERT INTO album(id,artist,name,sortname) VALUES(NULL,?,?,?)" ); - query.addBindValue( artistid ); - query.addBindValue( name_orig ); - query.addBindValue( sortname ); - if( !query.exec() ) + if ( autoCreate ) { - tDebug() << "Failed to insert album:" << name_orig; - return 0; + // not found, insert it. + query.prepare( "INSERT INTO album(id,artist,name,sortname) VALUES(NULL,?,?,?)" ); + query.addBindValue( artistid ); + query.addBindValue( name_orig ); + query.addBindValue( sortname ); + if( !query.exec() ) + { + tDebug() << "Failed to insert album:" << name_orig; + return 0; + } + + id = query.lastInsertId().toInt(); + m_lastalb = name_orig; + m_lastalbid = id; } - id = query.lastInsertId().toInt(); - //m_albumcache[sortname]=id; - isnew = true; - m_lastalb = name_orig; - m_lastalbid = id; return id; } diff --git a/src/libtomahawk/database/databaseimpl.h b/src/libtomahawk/database/databaseimpl.h index 619026327..b40f11aa7 100644 --- a/src/libtomahawk/database/databaseimpl.h +++ b/src/libtomahawk/database/databaseimpl.h @@ -52,9 +52,9 @@ public: TomahawkSqlQuery newquery() { return TomahawkSqlQuery( db ); } QSqlDatabase& database() { return db; } - int artistId( const QString& name_orig, bool& autoCreate ); - int trackId( int artistid, const QString& name_orig, bool& isnew ); - int albumId( int artistid, const QString& name_orig, bool& isnew ); + int artistId( const QString& name_orig, bool autoCreate ); + int trackId( int artistid, const QString& name_orig, bool autoCreate ); + int albumId( int artistid, const QString& name_orig, bool autoCreate ); QList< QPair > searchTable( const QString& table, const QString& name, uint limit = 10 ); QList< int > getTrackFids( int tid ); diff --git a/src/libtomahawk/widgets/whatshotwidget.cpp b/src/libtomahawk/widgets/whatshotwidget.cpp index 2b8ba4612..9a4a27dcc 100644 --- a/src/libtomahawk/widgets/whatshotwidget.cpp +++ b/src/libtomahawk/widgets/whatshotwidget.cpp @@ -179,9 +179,7 @@ WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestDat artistsModel->setColumnStyle( TreeModel::TrackOnly ); foreach ( const QString& artist, artists ) { - artist_ptr artistPtr = Artist::get( artist ); - if ( artistPtr.isNull() ) - artistPtr = Artist::get( 0, artist ); + artist_ptr artistPtr = Artist::get( artist, false ); artistsModel->addArtists( artistPtr ); } @@ -199,13 +197,9 @@ WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestDat foreach ( const Tomahawk::InfoSystem::ArtistAlbumPair& album, albums ) { qDebug() << "Getting album" << album.album << "By" << album.artist; - artist_ptr artistPtr = Artist::get( album.artist ); - if ( artistPtr.isNull() ) - artistPtr = Artist::get( 0, album.artist ); - album_ptr albumPtr = Album::get( 0, album.album, artistPtr ); - - if( !albumPtr.isNull() ) - al << albumPtr; + artist_ptr artistPtr = Artist::get( album.artist, false ); + album_ptr albumPtr = Album::get( artistPtr, album.album, false ); + al << albumPtr; } qDebug() << "Adding albums to model";