File indexing completed on 2024-05-05 04:49:17

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0003  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #include "ServiceSqlCollection.h"
0019 
0020 #include "ServiceSqlQueryMaker.h"
0021 #include <core/storage/SqlStorage.h>
0022 #include <core-impl/storage/StorageManager.h>
0023 
0024 using namespace Collections;
0025 
0026 ServiceSqlCollection::ServiceSqlCollection( const QString &id, const QString &prettyName, ServiceMetaFactory * metaFactory, ServiceSqlRegistry * registry )
0027     : ServiceCollection()
0028     , m_metaFactory( metaFactory )
0029     , m_registry( registry )
0030     , m_collectionId( id )
0031     , m_prettyName( prettyName )
0032 {
0033 }
0034 
0035 ServiceSqlCollection::~ServiceSqlCollection()
0036 {
0037 }
0038 
0039 QString
0040 ServiceSqlCollection::collectionId() const
0041 {
0042     return m_collectionId;
0043 }
0044 
0045 QString
0046 ServiceSqlCollection::prettyName() const
0047 {
0048     return m_prettyName;
0049 }
0050 
0051 QueryMaker*
0052 ServiceSqlCollection::queryMaker()
0053 {
0054     return new ServiceSqlQueryMaker( this, m_metaFactory, m_registry );
0055 }
0056 
0057 QStringList
0058 ServiceSqlCollection::query( const QString &statement )
0059 {
0060     return StorageManager::instance()->sqlStorage()->query( statement );
0061 }
0062 
0063 int
0064 ServiceSqlCollection::insert( const QString &statement, const QString &table )
0065 {
0066     return StorageManager::instance()->sqlStorage()->insert( statement, table );
0067 }
0068 
0069 
0070 QString
0071 ServiceSqlCollection::escape(const QString &text ) const
0072 {
0073     return StorageManager::instance()->sqlStorage()->escape( text );
0074 }
0075 
0076 Meta::TrackPtr
0077 ServiceSqlCollection::trackForUrl(const QUrl &url)
0078 {
0079     if ( !possiblyContainsTrack( url ) ) //do we even bother trying?
0080         return Meta::TrackPtr();
0081 
0082     //split out the parts we can be sure about ( strip username and such info )
0083     QString trackRows = m_metaFactory->getTrackSqlRows() + QLatin1Char(',') + m_metaFactory->getAlbumSqlRows() + QLatin1Char(',') +  m_metaFactory->getArtistSqlRows() + QLatin1Char(',') +  m_metaFactory->getGenreSqlRows();
0084 
0085     QString prefix = m_metaFactory->tablePrefix();
0086 
0087     QString pristineUrl = url.url();
0088 
0089     auto sqlDb = StorageManager::instance()->sqlStorage();
0090 
0091     QString from =  prefix + "_tracks";
0092     from += " LEFT JOIN " + prefix + "_albums ON " + prefix + "_tracks.album_id = " + prefix + "_albums.id";
0093     from += " LEFT JOIN " + prefix + "_artists ON " + prefix + "_albums.artist_id = " + prefix + "_artists.id";
0094     from += " LEFT JOIN " + prefix + "_genre ON " + prefix + "_genre.album_id = " + prefix + "_albums.id";
0095 
0096     QString queryString = QStringLiteral( "select DISTINCT %1 FROM %2 WHERE %3_tracks.preview_url = '%4' GROUP BY %5_tracks.id;" )
0097             .arg( trackRows,
0098                   from,
0099                   prefix,
0100                   sqlDb->escape( pristineUrl ),
0101                   prefix );
0102 
0103     //debug() << "Querying for track: " << queryString;
0104     QStringList result = sqlDb->query( queryString );
0105     //debug() << "result: " << result;
0106 
0107     return m_registry->getTrack( result );
0108 }
0109 
0110 bool
0111 ServiceSqlCollection::possiblyContainsTrack(const QUrl &url) const
0112 {
0113     return url.url().contains( m_metaFactory->tablePrefix(), Qt::CaseInsensitive );
0114 }
0115 
0116