From 71117d3c42d89535cd53c48b535a279ab39f52f3 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Mon, 5 Sep 2011 05:50:02 +0200 Subject: [PATCH] * Filter word by word, not the total term, e.g. 'rock you we will' finds 'we will rock you'. --- .../database/databasecommand_allartists.cpp | 18 +++++++++--------- src/libtomahawk/utils/tomahawkutils.cpp | 7 +++++++ src/libtomahawk/utils/tomahawkutils.h | 1 + 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/libtomahawk/database/databasecommand_allartists.cpp b/src/libtomahawk/database/databasecommand_allartists.cpp index e6c95c2a3..7be9d0138 100644 --- a/src/libtomahawk/database/databasecommand_allartists.cpp +++ b/src/libtomahawk/database/databasecommand_allartists.cpp @@ -21,6 +21,7 @@ #include #include "databaseimpl.h" +#include "utils/tomahawkutils.h" #include "utils/logger.h" @@ -45,8 +46,14 @@ DatabaseCommand_AllArtists::exec( DatabaseImpl* dbi ) if ( !m_filter.isEmpty() ) { - filterToken = QString( "AND file_join.album = album.id AND file_join.track = track.id " - "AND ( artist.name LIKE :filterA OR album.name LIKE :filterB OR track.name LIKE :filterC )" ); + QString filtersql; + QStringList sl = m_filter.split( " ", QString::SkipEmptyParts ); + foreach( QString s, sl ) + { + filtersql += QString( " AND ( artist.name LIKE '%%1%' OR album.name LIKE '%%1%' OR track.name LIKE '%%1%' )" ).arg( TomahawkUtils::sqlEscape( s ) ); + } + + filterToken = QString( "AND file_join.album = album.id AND file_join.track = track.id %1" ).arg( filtersql ); tables = "artist, track, album, file, file_join"; } else @@ -66,13 +73,6 @@ DatabaseCommand_AllArtists::exec( DatabaseImpl* dbi ) .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() ); query.prepare( sql ); - if ( !m_filter.isEmpty() ) - { - query.bindValue( ":filterA", QString( "%%1%" ).arg( m_filter ) ); - query.bindValue( ":filterB", QString( "%%1%" ).arg( m_filter ) ); - query.bindValue( ":filterC", QString( "%%1%" ).arg( m_filter ) ); - } - query.exec(); while( query.next() ) diff --git a/src/libtomahawk/utils/tomahawkutils.cpp b/src/libtomahawk/utils/tomahawkutils.cpp index c9a0e5afc..bbd23d8c1 100644 --- a/src/libtomahawk/utils/tomahawkutils.cpp +++ b/src/libtomahawk/utils/tomahawkutils.cpp @@ -174,6 +174,13 @@ appLogDir() } +QString +sqlEscape( QString sql ) +{ + return sql.replace( "'", "''" ); +} + + QString timeToString( int seconds ) { diff --git a/src/libtomahawk/utils/tomahawkutils.h b/src/libtomahawk/utils/tomahawkutils.h index 2e6c93987..2c905c205 100644 --- a/src/libtomahawk/utils/tomahawkutils.h +++ b/src/libtomahawk/utils/tomahawkutils.h @@ -75,6 +75,7 @@ namespace TomahawkUtils DLLEXPORT QDir appDataDir(); DLLEXPORT QDir appLogDir(); + DLLEXPORT QString sqlEscape( QString sql ); DLLEXPORT QString timeToString( int seconds ); DLLEXPORT QString ageToString( const QDateTime& time ); DLLEXPORT QString filesizeToString( unsigned int size );