qtinternetradio/irdb/src/irdbwrapper.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 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QSqlQuery>
       
    19 #include <QVariant>
       
    20 #include "irdbwrapper.h"
       
    21 #include "irdb.h"
       
    22 
       
    23 
       
    24 IRDBWrapper::IRDBWrapper()//: m_BuffFlag(true)
       
    25 {     
       
    26     m_pIRDB = IRDB::getInstance();
       
    27 }
       
    28 
       
    29     
       
    30 IRDBWrapper::~IRDBWrapper()
       
    31 {
       
    32     if(m_pIRDB)
       
    33     {  	
       
    34         m_pIRDB->releaseInstance();
       
    35         m_pIRDB = NULL;
       
    36     }
       
    37 } 
       
    38 
       
    39 
       
    40 /*
       
    41 * If all parameters are default, all rows in IRDB will return;
       
    42 */                       
       
    43 QList<QVariant*>* IRDBWrapper::getIRDB(const columnMap* const condAND,  
       
    44                                  const columnMap* const condOR)
       
    45 {
       
    46     QString condSqlStr;
       
    47     QList<QVariant*> *pIRDataSet = NULL;
       
    48     
       
    49     combineCondStr(condAND, condOR, colNameView, condSqlStr);
       
    50 
       
    51     if( (NULL != condAND)||(NULL != condOR) )
       
    52     {
       
    53         combineCondStr(condAND, condOR, colNameView, condSqlStr);
       
    54     }
       
    55     pIRDataSet = new QList<QVariant*>();
       
    56     if( m_pIRDB->selectRowIRDB(this, condSqlStr, false, NULL, pIRDataSet) )
       
    57     {
       
    58         delete pIRDataSet;
       
    59         pIRDataSet = NULL;
       
    60     }
       
    61     
       
    62     return pIRDataSet;
       
    63 }
       
    64 
       
    65 /*
       
    66 * If all parameters are default, all cid rows in IRDB will return;
       
    67 */                       
       
    68 QList<uint>* IRDBWrapper::getChannelId(const columnMap* const condAND,  
       
    69                                        const columnMap* const condOR)
       
    70 {
       
    71     QString condSqlStr;
       
    72     QList<uint>* pQListCIDSet = NULL;
       
    73     /*this case is wrong logic*/
       
    74     if( (NULL != condAND)&&(NULL != condOR) )
       
    75     {
       
    76         return NULL;
       
    77     }
       
    78 
       
    79     if( (NULL != condAND)||(NULL != condOR) )
       
    80     {
       
    81         combineCondStr(condAND, condOR, colNameView, condSqlStr);
       
    82     }
       
    83     pQListCIDSet = new QList<uint>();
       
    84     if( m_pIRDB->selectRowIRDB(this, condSqlStr, true, pQListCIDSet, NULL) )
       
    85     {
       
    86         delete pQListCIDSet;
       
    87         pQListCIDSet = NULL;
       
    88     }
       
    89     
       
    90     return pQListCIDSet;
       
    91 }    
       
    92 
       
    93 
       
    94 bool IRDBWrapper::loadDataOfChannelIdCB(QSqlQuery& aIRDataSet, QList<uint>* pQListCIDSet)
       
    95 {
       
    96     while(aIRDataSet.next()) 
       
    97     {
       
    98         *pQListCIDSet<<aIRDataSet.value(0).toUInt(); 
       
    99     }
       
   100           
       
   101     return true;
       
   102 }
       
   103 
       
   104 
       
   105 bool IRDBWrapper::loadDataOfIRDBCB(QSqlQuery& aIRDataSet, QList<QVariant*>* pIRDataSet)
       
   106 {
       
   107     while (aIRDataSet.next()) 
       
   108     {
       
   109          QVariant* const p = new QVariant[IRDB_CHANNELINFO_COLUMN];
       
   110 
       
   111         *pIRDataSet<<p;
       
   112 
       
   113         for(int i = 0; i < IRDB_CHANNELINFO_COLUMN; i++)
       
   114         {
       
   115             *(p+i) = aIRDataSet.value(i);
       
   116         }
       
   117     }
       
   118 
       
   119     return true;
       
   120 }    
       
   121 
       
   122 bool IRDBWrapper::getIRTableCB(QSqlQuery& aIRDataSet, QList<QVariant*>* p) 
       
   123 {
       
   124     Q_UNUSED(aIRDataSet);
       
   125     Q_UNUSED(p);
       
   126     return true;
       
   127 }
       
   128 
       
   129  
       
   130 void IRDBWrapper::combinePutStr(const columnMap* const RowData,
       
   131                                 const QString* pArryColName,
       
   132                                 QString& insSqlStr, 
       
   133                                 QString& updSqlStr,
       
   134                                 const logoMap* const logoData,
       
   135                                 QList<QByteArray>* logoArrayList)
       
   136 {
       
   137     bool bContinue = false;
       
   138     columnMap::const_iterator it;
       
   139     logoMap::const_iterator itLogoMap;
       
   140     QString escStr;
       
   141     insSqlStr += "(";
       
   142     
       
   143     if(NULL != RowData)
       
   144     {
       
   145         bContinue = true;
       
   146         it = RowData->begin();
       
   147         while(it != RowData->end())
       
   148         {
       
   149             if(it != RowData->begin())
       
   150             {
       
   151                 insSqlStr += ",";  
       
   152             }
       
   153 
       
   154             insSqlStr += pArryColName[it.key()];
       
   155 
       
   156             ++it;
       
   157         }
       
   158     }
       
   159     
       
   160     if(NULL != logoData)
       
   161     {
       
   162         itLogoMap = logoData->begin();
       
   163         while(itLogoMap != logoData->end())
       
   164         {
       
   165             if( (false != bContinue)||(itLogoMap != logoData->begin()) )
       
   166             {
       
   167                      insSqlStr += ",";  
       
   168             }
       
   169  
       
   170             insSqlStr += colNameLogo[itLogoMap.key()];
       
   171             ++itLogoMap;        
       
   172         }
       
   173     }
       
   174     
       
   175     insSqlStr += ") values(";
       
   176     if(NULL != RowData)
       
   177     {
       
   178         it = RowData->begin();
       
   179         while(it != RowData->end())
       
   180         {
       
   181             if(it != RowData->begin())
       
   182             {
       
   183                 insSqlStr += ",";  
       
   184             }
       
   185             //here for escape char,
       
   186             escStr = it.value();
       
   187             escStr.replace('\'', "\'\'");
       
   188             
       
   189             insSqlStr = insSqlStr + "'"+ escStr+ "'";
       
   190        
       
   191             ++it;
       
   192         }    
       
   193     }    
       
   194 
       
   195 
       
   196     if(NULL != logoData)
       
   197     {
       
   198         itLogoMap = logoData->begin();
       
   199         while(itLogoMap != logoData->end())
       
   200         {
       
   201             if( (false != bContinue)||(itLogoMap != logoData->begin()) )
       
   202             {
       
   203                  insSqlStr += ",";  
       
   204             }
       
   205             insSqlStr = insSqlStr + " :"+colNameLogo[itLogoMap.key()];
       
   206             *logoArrayList<<itLogoMap.value();
       
   207 
       
   208             ++itLogoMap;        
       
   209         }
       
   210     }    
       
   211     
       
   212     insSqlStr += " )";
       
   213    
       
   214     //part two, create update string;
       
   215     if(NULL != RowData)
       
   216     {    
       
   217         it = RowData->begin();
       
   218      
       
   219         while( it != RowData->end())
       
   220         {
       
   221             if(it != RowData->begin())
       
   222             {
       
   223                 updSqlStr += ", ";  
       
   224             }
       
   225             if(pArryColName[it.key()] == "opt")
       
   226             {
       
   227                 updSqlStr = updSqlStr + pArryColName[it.key()] + "=" + "'" + QString::number(IRDB_OPT_TRIGGER) + "'";        
       
   228             }
       
   229             else
       
   230             {
       
   231                 //here for escape char,
       
   232                 escStr = it.value();
       
   233                 escStr.replace('\'', "\'\'");
       
   234                 updSqlStr = updSqlStr + pArryColName[it.key()] + "=" + "'" + escStr + "'";
       
   235             }
       
   236             ++it;
       
   237         }
       
   238     }
       
   239     
       
   240     if(NULL != logoData)
       
   241     {
       
   242         itLogoMap = logoData->begin();
       
   243         while(itLogoMap != logoData->end())
       
   244         {
       
   245             if( (false != bContinue)||(itLogoMap != logoData->begin()) )
       
   246             {
       
   247                  updSqlStr += ",";  
       
   248             }
       
   249 
       
   250             updSqlStr = updSqlStr + colNameLogo[itLogoMap.key()] + "=:"+colNameLogo[itLogoMap.key()];
       
   251             ++itLogoMap;        
       
   252         }
       
   253     }    
       
   254     
       
   255     return;
       
   256 }
       
   257 
       
   258 
       
   259 
       
   260 void IRDBWrapper::combineCondStr(const columnMap* const condAND,  
       
   261                                  const columnMap* const condOR,   
       
   262                                  const QString* pArryColName,
       
   263                                  QString& condSqlStr)
       
   264 {
       
   265     bool bDone = false;
       
   266     columnMap::const_iterator it;
       
   267     
       
   268     if(NULL != condAND)
       
   269     {
       
   270         if( true != condAND->empty() )
       
   271         {
       
   272             it = condAND->begin();
       
   273             
       
   274             condSqlStr = " where "; 
       
   275             
       
   276             while( it != condAND->end())
       
   277             {
       
   278                 if(it != condAND->begin())
       
   279                 {
       
   280                     condSqlStr += " AND ";
       
   281                 }
       
   282                 
       
   283                 condSqlStr = condSqlStr + pArryColName[it.key()] + "=" + "'" + it.value()+ "'" ;
       
   284                     
       
   285                 ++it;
       
   286             }
       
   287             
       
   288             bDone = true;               
       
   289         }
       
   290     }
       
   291     
       
   292     if(true == bDone)
       
   293     {
       
   294         return;   
       
   295     }
       
   296 
       
   297     if(NULL != condOR )
       
   298     {
       
   299         if( true != condOR->empty() )
       
   300         {
       
   301             it = condOR->begin();
       
   302             condSqlStr = " where ";
       
   303             
       
   304             while( it != condOR->end())
       
   305             {
       
   306                 if(it != condOR->begin())
       
   307                 {
       
   308                     condSqlStr += " OR ";
       
   309                 }
       
   310                 
       
   311                 condSqlStr = condSqlStr + pArryColName[it.key()] + "=" + "'" + it.value()+ "'" ;
       
   312                 ++it;
       
   313              }               
       
   314         }       
       
   315     }
       
   316 
       
   317     return;
       
   318 }
       
   319 
       
   320  
       
   321 void IRDBWrapper::combineGetStr(const columnMap* const condAND,
       
   322                                 const columnMap* const condOR,
       
   323                                 const QString* pArryColName,
       
   324                                 QString& sltSqlStr)
       
   325 {
       
   326     QString condStr;
       
   327 
       
   328     combineCondStr(condAND, condOR, pArryColName, condStr);
       
   329     sltSqlStr += condStr;
       
   330     
       
   331     return;
       
   332 } 
       
   333 
       
   334 void IRDBWrapper::combineDeleteStr(const columnMap* const condAND,
       
   335                                    const columnMap* const condOR,
       
   336                                    const QString* pArryColName,
       
   337                                    QString& dltSqlStr)
       
   338 {
       
   339     QString condStr;
       
   340 
       
   341     combineCondStr(condAND, condOR, pArryColName, condStr);
       
   342     dltSqlStr += condStr;
       
   343     
       
   344     return;    
       
   345 }
       
   346