dbenginebase.cpp

来自「Amarok是一款在LINUX或其他类UNIX操作系统中运行的音频播放器软件。 」· C++ 代码 · 共 545 行 · 第 1/2 页

CPP
545
字号
/*************************************************************************** *   Copyright (C)  2004-2005 Mark Kretschmann <markey@web.de>             * *                  2004 Christian Muehlhaeuser <chris@chris.de>           * *                  2004 Sami Nieminen <sami.nieminen@iki.fi>              * *                  2005 Ian Monroe <ian@monroe.nu>                        * *                                                                         * *   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 Steet, Fifth Floor, Boston, MA  02111-1307, USA.             * ***************************************************************************/#include <dbenginebase.h>#include <qstringlist.h>#include <klocale.h>//////////////////////////////////////////////////////////////////////////////////////////// CLASS DBConnection//////////////////////////////////////////////////////////////////////////////////////////DbConnection::DbConnection( DbConfig* config )    : m_config( config ){}DbConnection::~DbConnection(){}//////////////////////////////////////////////////////////////////////////////////////////// CLASS QueryBuilder//////////////////////////////////////////////////////////////////////////////////////////QueryBuilder::QueryBuilder(){    clear();}voidQueryBuilder::linkTables( int tables ){    m_tables = tableName( tabSong );    if ( !(tables & tabSong ) )    {        // check if only one table is selected (does somebody know a better way to check that?)        if (tables == tabAlbum || tables==tabArtist || tables==tabGenre || tables == tabYear || tables == tabStats)            m_tables = tableName(tables);        else            tables |= tabSong;    }    if ( tables & tabSong )    {        if ( tables & tabAlbum )            m_tables += " INNER JOIN " + tableName( tabAlbum) + " ON album.id=tags.album";        if ( tables & tabArtist )            m_tables += " INNER JOIN " + tableName( tabArtist) + " ON artist.id=tags.artist";        if ( tables & tabGenre )            m_tables += " INNER JOIN " + tableName( tabGenre) + " ON genre.id=tags.genre";        if ( tables & tabYear )            m_tables += " INNER JOIN " + tableName( tabYear) + " ON year.id=tags.year";        if ( tables & tabStats )            m_tables += " INNER JOIN " + tableName( tabStats) + " ON statistics.url=tags.url";    }}voidQueryBuilder::addReturnValue( int table, int value ){    if ( !m_values.isEmpty() && m_values != "DISTINCT " ) m_values += ",";    if ( table & tabStats && value & valScore ) m_values += "round(";    if ( value == valDummy )        m_values += "''";    else    {        m_values += tableName( table ) + ".";        m_values += valueName( value );    }    if ( table & tabStats && value & valScore ) m_values += " + 0.4 )";    m_linkTables |= table;    m_returnValues++;}voidQueryBuilder::addReturnFunctionValue( int function, int table, int value){    if ( !m_values.isEmpty() && m_values != "DISTINCT " ) m_values += ",";    m_values += functionName( function ) + "(";    m_values += tableName( table ) + ".";    m_values += valueName( value )+ ")";    m_values += " AS ";    m_values += functionName( function )+tableName( table )+valueName( value );    m_linkTables |= table;    m_returnValues++;}uintQueryBuilder::countReturnValues(){    return m_returnValues;}voidQueryBuilder::addURLFilters( const QStringList& filter ){    if ( !filter.isEmpty() )    {        m_where += "AND ( true ";        for ( uint i = 0; i < filter.count(); i++ )        {                m_where += "OR tags.url = '" + escapeString( filter[i] ) + "' ";        }        m_where += " ) ";    }    m_linkTables |= tabSong;}voidQueryBuilder::addFilter( int tables, const QString& filter, int /*mode*/ ){    if ( !filter.isEmpty() )    {        m_where += "AND ( true ";        if ( tables & tabAlbum ) m_where += "OR album.name LIKE '%" + escapeString( filter ) + "%' ";        if ( tables & tabArtist ) m_where += "OR artist.name LIKE '%" + escapeString( filter ) + "%' ";        if ( tables & tabGenre ) m_where += "OR genre.name LIKE '%" + escapeString( filter ) + "%' ";        if ( tables & tabYear ) m_where += "OR year.name LIKE '%" + escapeString( filter ) + "%' ";        if ( tables & tabSong ) m_where += "OR tags.title LIKE '%" + escapeString( filter ) + "%' ";        m_where += " ) ";    }    m_linkTables |= tables;}voidQueryBuilder::addFilters( int tables, const QStringList& filter ){    if ( !filter.isEmpty() )    {        m_where += "AND ( true ";        for ( uint i = 0; i < filter.count(); i++ )        {            m_where += " AND ( true ";            if ( tables & tabAlbum ) m_where += "OR album.name LIKE '%" + escapeString( filter[i] ) + "%' ";            if ( tables & tabArtist ) m_where += "OR artist.name LIKE '%" + escapeString( filter[i] ) + "%' ";            if ( tables & tabGenre ) m_where += "OR genre.name LIKE '%" + escapeString( filter[i] ) + "%' ";            if ( tables & tabYear ) m_where += "OR year.name LIKE '%" + escapeString( filter[i] ) + "%' ";            if ( tables & tabSong ) m_where += "OR tags.title LIKE '%" + escapeString( filter[i] ) + "%' ";            m_where += " ) ";        }        m_where += " ) ";    }    m_linkTables |= tables;}voidQueryBuilder::addMatch( int tables, const QString& match ){    if ( !match.isEmpty() )    {        m_where += "AND ( true ";        if ( tables & tabAlbum ) m_where += "OR album.name LIKE '" + escapeString( match ) + "' ";        if ( tables & tabArtist ) m_where += "OR artist.name LIKE '" + escapeString( match ) + "' ";        if ( tables & tabGenre ) m_where += "OR genre.name LIKE '" + escapeString( match ) + "' ";        if ( tables & tabYear ) m_where += "OR year.name LIKE '" + escapeString( match ) + "' ";        if ( tables & tabSong ) m_where += "OR tags.title LIKE '" + escapeString( match ) + "' ";        if ( match == i18n( "Unknown" ) )        {            if ( tables & tabAlbum ) m_where += "OR album.name = '' ";            if ( tables & tabArtist ) m_where += "OR artist.name = '' ";            if ( tables & tabGenre ) m_where += "OR genre.name = '' ";            if ( tables & tabYear ) m_where += "OR year.name = '' ";        }        m_where += " ) ";    }    m_linkTables |= tables;}voidQueryBuilder::addMatch( int tables, int value, const QString& match ){    if ( !match.isEmpty() )    {        m_where += "AND ( true ";        m_where += QString( "OR %1.%2 LIKE '" ).arg( tableName( tables ) ).arg( valueName( value ) ) + escapeString( match ) + "' ";        if ( ( value & valName ) && match == i18n( "Unknown" ) )            m_where += QString( "OR %1.%2 = '' " ).arg( tableName( tables ) ).arg( valueName( value ) );        m_where += " ) ";    }    m_linkTables |= tables;}voidQueryBuilder::addMatches( int tables, const QStringList& match ){    if ( !match.isEmpty() )    {        m_where += "AND ( true ";        for ( uint i = 0; i < match.count(); i++ )        {            if ( tables & tabAlbum ) m_where += "OR album.name LIKE '" + escapeString( match[i] ) + "' ";            if ( tables & tabArtist ) m_where += "OR artist.name LIKE '" + escapeString( match[i] ) + "' ";            if ( tables & tabGenre ) m_where += "OR genre.name LIKE '" + escapeString( match[i] ) + "' ";            if ( tables & tabYear ) m_where += "OR year.name LIKE '" + escapeString( match[i] ) + "' ";            if ( tables & tabSong ) m_where += "OR tags.title LIKE '" + escapeString( match[i] ) + "' ";            if ( tables & tabStats ) m_where += "OR statistics.url LIKE '" + escapeString( match[i] ) + "' ";            if ( match[i] == i18n( "Unknown" ) )            {                if ( tables & tabAlbum ) m_where += "OR album.name = '' ";                if ( tables & tabArtist ) m_where += "OR artist.name = '' ";                if ( tables & tabGenre ) m_where += "OR genre.name = '' ";                if ( tables & tabYear ) m_where += "OR year.name = '' ";            }        }        m_where += " ) ";    }    m_linkTables |= tables;}voidQueryBuilder::excludeFilter( int tables, const QString& filter ){    if ( !filter.isEmpty() )    {        m_where += "AND ( true ";        if ( tables & tabAlbum ) m_where += "AND album.name <> '%" + escapeString( filter ) + "%' ";        if ( tables & tabArtist ) m_where += "AND artist.name <> '%" + escapeString( filter ) + "%' ";        if ( tables & tabGenre ) m_where += "AND genre.name <> '%" + escapeString( filter ) + "%' ";        if ( tables & tabYear ) m_where += "AND year.name <> '%" + escapeString( filter ) + "%' ";        if ( tables & tabSong ) m_where += "AND tags.title <> '%" + escapeString( filter ) + "%' ";

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?