qtinternetradio/irdb/src/songhistorywrapper.cpp
changeset 16 5723da102db1
child 17 38bbf2dcd608
equal deleted inserted replaced
15:065198191975 16:5723da102db1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Description: 
       
    13 *
       
    14 */
       
    15 
       
    16 #include <QSqlQuery>
       
    17 #include <QVariant>
       
    18 #include "songhistorywrapper.h"
       
    19 #include "irdb.h"
       
    20 
       
    21 songHistoryWrapper::songHistoryWrapper()
       
    22 {     
       
    23 }
       
    24 
       
    25 songHistoryWrapper::~songHistoryWrapper()
       
    26 {
       
    27 } 
       
    28 
       
    29 bool songHistoryWrapper::putSongHistory(const columnMap* const RowData,
       
    30                                         const columnMap* const condAND,
       
    31                                         const columnMap* const condOR)
       
    32 {
       
    33     QString sSongName;
       
    34     QString sArtistName;
       
    35     uint uCid = 0;
       
    36     QString insSqlStr;
       
    37     QString updSqlStr;
       
    38     QString condSqlStr;       
       
    39 
       
    40 
       
    41     if( NULL == RowData )
       
    42     {
       
    43         return false;
       
    44     }
       
    45 
       
    46     if(RowData->isEmpty())
       
    47     {
       
    48         return false;
       
    49     }
       
    50 
       
    51     if(NULL != condOR)
       
    52     {
       
    53         return false;
       
    54     }
       
    55  
       
    56     if(condAND)
       
    57     {
       
    58         if(condAND->isEmpty())
       
    59         {
       
    60             return false;
       
    61         }
       
    62     }
       
    63 
       
    64     insSqlStr = "insert into songHistory ";
       
    65     updSqlStr = "update songHistory set ";        
       
    66     combinePutStr(RowData,colNameView, insSqlStr, updSqlStr); 
       
    67     
       
    68     if(condAND == NULL) //for update or insert;
       
    69     {
       
    70         /*
       
    71         * insert string, there must be have songName and ChannelId;
       
    72         * update string, there must have songName, channelId and artistname. 
       
    73         */  
       
    74         sSongName = RowData->value(songName);
       
    75         uCid = (RowData->value(channelId)).toUInt();
       
    76         sArtistName = (RowData->value(artistName));
       
    77         
       
    78         if( (sSongName.isEmpty()) || (0 == uCid) )
       
    79         {
       
    80             return false;
       
    81         }
       
    82         if(sArtistName.isEmpty())
       
    83         {
       
    84             /*
       
    85             * In songhistorywrapper.cpp, 
       
    86             * if (songname not change && artistname not change && channeled not change) then update, 
       
    87             * otherwise insert a new one.
       
    88             */
       
    89             updSqlStr.clear();
       
    90         }
       
    91         else
       
    92         {
       
    93             //combine cond str;
       
    94             //combineCondStr(RowData, NULL, colNameSongHistory, condSqlStr); 
       
    95             condSqlStr = "where songName = '" + sSongName +"'";
       
    96             condSqlStr += " AND ";
       
    97             condSqlStr = condSqlStr + "channelId = " + QString::number(uCid);
       
    98             condSqlStr += " AND ";
       
    99             condSqlStr = condSqlStr + "artistName = '" + sArtistName+"'";
       
   100         }
       
   101         
       
   102     }    
       
   103     else //if condtion isn't null, it is update. for update, songName must be in condAND or condOR; 
       
   104     {
       
   105         /*
       
   106         * if cond != NULL,
       
   107         * there must have songname and channelId and artistName in cond AND 
       
   108         * there must have not channelId in Rowdata
       
   109         */
       
   110         if(!(RowData->value(channelId)).isEmpty())
       
   111         {
       
   112             return false;
       
   113         }
       
   114         sSongName = condAND->value(songName);
       
   115         uCid = (condAND->value(channelId)).toUInt();
       
   116         sArtistName = (condAND->value(artistName));
       
   117         
       
   118         if( ( sSongName.isEmpty()) || (0 == uCid) || (sArtistName.isEmpty()) )
       
   119         {
       
   120             return false;
       
   121         }
       
   122         
       
   123         //combine update string;
       
   124         combineCondStr(condAND, NULL, colNameView, condSqlStr);    
       
   125         insSqlStr.clear();
       
   126         //updSqlStr += condSqlStr;
       
   127     }
       
   128     
       
   129     return m_pIRDB->chgRowSongHistory(insSqlStr, updSqlStr, condSqlStr, sSongName, uCid)? false:true;     
       
   130 }
       
   131 
       
   132 bool songHistoryWrapper::deleteSongHistory(const columnMap* const condAND,
       
   133                                            const columnMap* const condOR)
       
   134 {
       
   135     QString deleteSqlStr = "delete from SongHistory ";
       
   136     
       
   137     if( (NULL != condAND)&&(NULL != condOR) )
       
   138     {
       
   139         return false;
       
   140     }
       
   141 
       
   142     if(condAND)
       
   143     {
       
   144         if(condAND->isEmpty())
       
   145         {
       
   146             return false;
       
   147         }
       
   148     }
       
   149 
       
   150     if(condOR)
       
   151     {
       
   152         if(condOR->isEmpty())
       
   153         {
       
   154             return false;
       
   155         }
       
   156     }
       
   157 
       
   158     combineDeleteStr(condAND, condOR, colNameView, deleteSqlStr);
       
   159     return m_pIRDB->deleteRow(deleteSqlStr)? false:true;
       
   160 }
       
   161 
       
   162 
       
   163 QList<QVariant*>* songHistoryWrapper::getSongHistory(const columnMap* const condAND,  
       
   164                                                      const columnMap* const condOR)
       
   165 {
       
   166     QString sltSqlStr = "select * from songHistory ";
       
   167     QList<QVariant*>* pDataSet = NULL;
       
   168     
       
   169     if( (NULL != condAND)&&(NULL != condOR) )
       
   170     {
       
   171         return NULL;
       
   172     }
       
   173 
       
   174     if(condAND)
       
   175     {
       
   176         if(condAND->isEmpty())
       
   177         {
       
   178             return NULL;
       
   179         }
       
   180     }
       
   181 
       
   182     if(condOR)
       
   183     {
       
   184         if(condOR->isEmpty())
       
   185         {
       
   186             return false;
       
   187         }
       
   188     }
       
   189 
       
   190     combineGetStr(condAND, condOR, colNameView, sltSqlStr);
       
   191     sltSqlStr = sltSqlStr + " order by songPlaySeq desc ";
       
   192     pDataSet = new QList<QVariant*>();
       
   193     if( m_pIRDB->selectRow(this, sltSqlStr, pDataSet) )
       
   194     {
       
   195         delete pDataSet;
       
   196         pDataSet = NULL;
       
   197     }
       
   198     
       
   199     return pDataSet;
       
   200 }
       
   201 
       
   202 bool songHistoryWrapper::getIRTableCB(QSqlQuery& aIRDataSet, QList<QVariant*>* pDataSet)
       
   203 {
       
   204     while (aIRDataSet.next()) 
       
   205     {
       
   206         QVariant* const p = new QVariant[IRDB_SONGHISTORY_COLUMN];
       
   207         *pDataSet<<p;
       
   208 
       
   209         for(int i = 0; i < IRDB_SONGHISTORY_COLUMN; i++)
       
   210         {
       
   211             *(p+i) = aIRDataSet.value(i);
       
   212         }
       
   213     }
       
   214 
       
   215     return true;
       
   216 
       
   217 }
       
   218 
       
   219