upnpharvester/common/cmsqlwrapper/src/cmsqldbclause.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
parent 39 6369bfd1b60d
child 41 b4d83ea1d6e2
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
     1 /*
       
     2 * Copyright (c) 2008 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:      SQL database sql clause class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 #include <utf.h>
       
    24 #include "cmfillrule.h"
       
    25 #include "cmsqlpropertycollector.h"
       
    26 #include "cmsqlpropertycontainer.h"
       
    27 #include "cmsqlpropertyitem.h"
       
    28 #include "cmsqldbclause.h"
       
    29 #include "cmsqlclausedef.h"
       
    30 #include "msdebug.h"                                                    
       
    31 
       
    32 // CONSTANTS
       
    33 // The size of buf that need to allot for store the rule amount
       
    34 const TUint KCmRuleAmountSize = 5;
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // CCmSqlDbClause::NewL
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CCmSqlDbClause* CCmSqlDbClause::NewL()
       
    41     {
       
    42     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::NewL"));    
       
    43     CCmSqlDbClause* self = CCmSqlDbClause::NewLC();
       
    44     CleanupStack::Pop( self );
       
    45     return self;
       
    46     }
       
    47  
       
    48 // ---------------------------------------------------------------------------
       
    49 // CCmSqlDbClause::NewLC
       
    50 // ---------------------------------------------------------------------------
       
    51 //    
       
    52 CCmSqlDbClause* CCmSqlDbClause::NewLC()
       
    53     {
       
    54     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::NewLC"));    
       
    55     CCmSqlDbClause* self = new ( ELeave ) CCmSqlDbClause();
       
    56     CleanupStack::PushL( self );
       
    57     self->ConstructL();
       
    58     return self;  
       
    59     }    
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CCmSqlDbClause::~CCmSqlDbClause
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 CCmSqlDbClause::~CCmSqlDbClause()
       
    66     {
       
    67     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::~CCmSqlDbClause"));    
       
    68     
       
    69     delete iClause;
       
    70     delete iFreeText;
       
    71     iProfileFilters.Reset();        
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CCmSqlDbClause::FormatClauseL
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 void CCmSqlDbClause::FormatClauseL( CCmFillRule& aRule,
       
    79     TCmMetadataField aField, TCmMediaType aMedia )
       
    80     {
       
    81     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatClauseL"));    
       
    82     
       
    83     delete iClause;
       
    84     iClause = NULL;   
       
    85     if( aField == ECmNone && &aRule )
       
    86         {
       
    87         if( ECmAudio != aRule.MediaType() )
       
    88             {
       
    89             iClause = KCmSqlBaseSelect().AllocL();    
       
    90             }
       
    91         else
       
    92             {
       
    93             iClause = KCmSqlMusicSelect().AllocL();
       
    94             }
       
    95         TRACE(Print(_L("[SQL wrapper]\t Formating media type...")));
       
    96         FormatMediatypeL( aRule );
       
    97         TRACE(Print(_L("[SQL wrapper]\t Formating params...")));
       
    98         FormatParamsL( aRule );
       
    99         TRACE(Print(_L("[SQL wrapper]\t Formating devices...")));
       
   100         FormatDevicesL( aRule );
       
   101         TRACE(Print(_L("[SQL wrapper]\t Formating profile filters...")));
       
   102         FormatProfileFiltersL();
       
   103         TRACE(Print(_L("[SQL wrapper]\t Formating ordering...")));
       
   104         FormatOrderingL( aRule.Method() );
       
   105         TRACE(Print(_L("[SQL wrapper]\t Formating amount...")));
       
   106         FormatAmountL( aRule );
       
   107         // Finish clause    
       
   108         iClause = iClause->ReAllocL( iClause->Length() + 
       
   109             KCmSqlSemicolon().Length() );
       
   110         iClause->Des().Append( KCmSqlSemicolon() );     
       
   111         }
       
   112     else
       
   113         {
       
   114         FormatProperyQueryL( aField, aMedia );
       
   115         }        
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CCmSqlDbClause::FormatFilteringClauseL
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 void CCmSqlDbClause::FormatFilteringClauseL( 
       
   123     CCmSqlPropertyCollector& aPropertys )
       
   124     {
       
   125     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatFilteringClauseL"));    
       
   126     
       
   127     delete iClause;
       
   128     iClause = NULL;
       
   129 
       
   130     TInt count = aPropertys.PropertyContainerCount();
       
   131 
       
   132     if( count == 1 && aPropertys.PropertyContainer( 0 )->Type() == ECmTitle)
       
   133         {
       
   134         //video
       
   135         iClause = KCmSqlFilteredPropertySelectVideo().AllocL();
       
   136         }
       
   137     else
       
   138         {
       
   139         iClause = KCmSqlFilteredPropertySelect().AllocL();
       
   140         }
       
   141 
       
   142     FormatPropertyFilterL( aPropertys );
       
   143     }
       
   144     
       
   145 // ---------------------------------------------------------------------------
       
   146 // CCmSqlDbClause::ClauseL
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 TDesC8& CCmSqlDbClause::ClauseL() const
       
   150     {
       
   151     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::ClauseL"));    
       
   152     
       
   153 #ifdef _DEBUG    
       
   154     HBufC* data16bitBuf = HBufC::NewLC(iClause->Length());
       
   155     TPtr data16bitPtr = data16bitBuf->Des();
       
   156        
       
   157     TInt conversionError = 
       
   158         CnvUtfConverter::ConvertToUnicodeFromUtf8(data16bitPtr, 
       
   159                                                 *iClause );
       
   160     TPtrC temp;                                                
       
   161     for( TInt i = 0; i < (data16bitPtr.Length())/100 + 1 ; i++ ) 
       
   162         {
       
   163         if( i == (data16bitPtr.Length())/100 )
       
   164             {
       
   165             temp.Set( data16bitPtr.Mid(100*i ) );
       
   166             TRACE( Print(_L("SQL clause %S"), &temp ) ); 
       
   167             }
       
   168         else
       
   169             {
       
   170             temp.Set( data16bitPtr.Mid(100*i, 100 ) );
       
   171             TRACE( Print(_L("SQL clause %S"), &temp ) ); 
       
   172             }        
       
   173         }
       
   174     CleanupStack::PopAndDestroy( data16bitBuf );
       
   175 #endif    
       
   176     return *iClause;
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // CCmSqlDbClause::SetDlnaProfileFilters
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 void CCmSqlDbClause::SetDlnaProfileFilters( 
       
   184     const RArray<TInt64>& aProfileIds )
       
   185     {
       
   186     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::SetDlnaProfileFilters"));    
       
   187     
       
   188     iProfileFilters.Reset();
       
   189     for( TInt i = 0; i < aProfileIds.Count() ; i++ )
       
   190         {
       
   191         iProfileFilters.InsertInSignedKeyOrder( aProfileIds[i] );
       
   192         }
       
   193     }
       
   194         
       
   195 // ---------------------------------------------------------------------------
       
   196 // CCmSqlDbClause::FormatMediatypeL
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 void CCmSqlDbClause::FormatMediatypeL( CCmFillRule& aRule )
       
   200     {
       
   201     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatMediatypeL"));    
       
   202     
       
   203     TBuf8<20>mediatype;
       
   204     mediatype.Format( KCmSqlSelectMediatype, aRule.MediaType() );
       
   205     iClause = iClause->ReAllocL( iClause->Length() + mediatype.Length() );
       
   206     iClause->Des().Append( mediatype );
       
   207     }
       
   208     
       
   209 // ---------------------------------------------------------------------------
       
   210 // CCmSqlDbClause::FormatParamsL
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 void CCmSqlDbClause::FormatParamsL( CCmFillRule& aRule )
       
   214     {
       
   215     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatParamsL"));    
       
   216     
       
   217     TCmMetadataField metadataColum;
       
   218     TPtrC8 dataField;
       
   219     TInt dataFieldIndex;
       
   220     TCmOperatorType oper;
       
   221     TInt paramCount;
       
   222     TBool operatorAdded( EFalse );   
       
   223     for( TInt i = 0; i < aRule.RuleCount(); i++ )
       
   224         {
       
   225         aRule.RuleL( i, &metadataColum, &oper, &paramCount );
       
   226         if( paramCount )
       
   227             {            
       
   228             iClause = iClause->ReAllocL( iClause->Length() + 
       
   229                 KCmSqlAnd().Length() + KCmSqlLeftBracket().Length()  );
       
   230             iClause->Des().Append( KCmSqlAnd );
       
   231             iClause->Des().Append( KCmSqlLeftBracket() );                
       
   232             for( TInt j = 0; j < paramCount; j++ )
       
   233                 {
       
   234                 iClause = iClause->ReAllocL( iClause->Length() + 
       
   235                     KCmSqlLeftBracket().Length() );
       
   236                 iClause->Des().Append( KCmSqlLeftBracket() );
       
   237                               
       
   238                 FormatMetadataColumnL( metadataColum, aRule.MediaType() );
       
   239                 // Data field may change to integer value ( index to table )
       
   240                 aRule.RuleParamL( i, j, &dataField );
       
   241                 aRule.RuleParamL( i, j, dataFieldIndex );
       
   242                 
       
   243                 // If rule is free text rule, operator is already appended 
       
   244                 // into Sql clause
       
   245                 operatorAdded = EFalse;
       
   246                 if( ECmFreeText != metadataColum &&
       
   247                     ECmArtist != metadataColum &&
       
   248                     ECmAlbum != metadataColum &&
       
   249                     ECmResolution != metadataColum &&
       
   250                     ECmGenre != metadataColum && 
       
   251                     ECmUpnpClass != metadataColum && 
       
   252                     ECmTitle != metadataColum )
       
   253                     {
       
   254                     FormatOperatorL( metadataColum, oper );
       
   255                     operatorAdded = ETrue;
       
   256                     }
       
   257                       
       
   258                 if( dataField.Size() && 
       
   259                     ( iFreeText || ECmDate == metadataColum || 
       
   260                     ECmFileSize == metadataColum || 
       
   261                     ECmDuration == metadataColum ) )
       
   262                     {
       
   263                     if( ECmDate != metadataColum && 
       
   264                         ECmFileSize != metadataColum && 
       
   265                         ECmDuration != metadataColum )
       
   266                         {
       
   267                         TInt multiplier( 1 );
       
   268                         if( aRule.MediaType() == ECmAudio && 
       
   269                             metadataColum == ECmFreeText )
       
   270                             {
       
   271                             // Three fields are used in audio free text search
       
   272                             multiplier = 3;
       
   273                             }
       
   274                         iFreeText = iFreeText->ReAllocL( iFreeText->Length() +
       
   275                             ( dataField.Length() * multiplier ) );  
       
   276                         HBufC8* temp = HBufC8::NewMaxL( iFreeText->Length() + 
       
   277                             ( dataField.Length() * multiplier ) );
       
   278                         // Format dataField into clause
       
   279                         if( multiplier == 1 )
       
   280                             {                    
       
   281                             temp->Des().Format( *iFreeText, &dataField );
       
   282                             }
       
   283                         else
       
   284                             {
       
   285                             temp->Des().Format( *iFreeText, &dataField, 
       
   286                                 &dataField, &dataField );   
       
   287                             } 
       
   288                         if( iFreeText )
       
   289                             {
       
   290                             delete iFreeText; 
       
   291                             iFreeText = NULL;
       
   292                             }
       
   293                         iClause = iClause->ReAllocL( iClause->Length() + 
       
   294                             temp->Length() );
       
   295                         iClause->Des().Append( *temp );
       
   296                         delete temp;                        
       
   297                         }
       
   298                     else
       
   299                         {
       
   300                         // Date is appended in TInt64 format
       
   301                         TLex8 lex( dataField );
       
   302                         TInt64 num;
       
   303                         lex.Val( num );
       
   304                         iClause = iClause->ReAllocL( iClause->Length() + 
       
   305                             dataField.Length() );                    
       
   306                         iClause->Des().AppendNum( num );
       
   307                         }            
       
   308                     }
       
   309                 else
       
   310                     {
       
   311                     if( operatorAdded )
       
   312                         {
       
   313                         iClause = iClause->ReAllocL( iClause->Length() + 
       
   314                             sizeof( dataFieldIndex ) );
       
   315                             
       
   316                         HBufC8* temp = HBufC8::NewMaxLC( iClause->Length() );
       
   317                         temp->Des().Format( *iClause, dataFieldIndex );
       
   318                         delete iClause;
       
   319                         iClause = NULL;
       
   320                         iClause = temp->AllocL();
       
   321                         CleanupStack::PopAndDestroy( temp );
       
   322                         }
       
   323                     else
       
   324                         {
       
   325                         iClause = iClause->ReAllocL( iClause->Length() + 
       
   326                             sizeof( dataFieldIndex ) );                    
       
   327                         iClause->Des().AppendNum( dataFieldIndex );
       
   328                         }      
       
   329                     }    
       
   330 
       
   331                 iClause = iClause->ReAllocL( iClause->Length() + 
       
   332                     KCmSqlRightBracket().Length() );
       
   333                 iClause->Des().Append( KCmSqlRightBracket() );         
       
   334                 
       
   335                 if( j < paramCount - 1 )
       
   336                     {
       
   337                     iClause = iClause->ReAllocL( iClause->Length() + 
       
   338                         KCmSqlOr().Length() );
       
   339                     iClause->Des().Append( KCmSqlOr() );            
       
   340                     }            
       
   341                 }
       
   342             iClause = iClause->ReAllocL( iClause->Length() + 
       
   343                 KCmSqlRightBracket().Length() );
       
   344             iClause->Des().Append( KCmSqlRightBracket() );             
       
   345             }            
       
   346         }      
       
   347     }
       
   348 
       
   349 // ---------------------------------------------------------------------------
       
   350 // CCmSqlDbClause::FormatDevicesL
       
   351 // ---------------------------------------------------------------------------
       
   352 //    
       
   353 void CCmSqlDbClause::FormatDevicesL( CCmFillRule& aRule )
       
   354     {
       
   355     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatDevicesL"));    
       
   356     
       
   357     TPtrC8 mediaServer;
       
   358     TInt msId(0);
       
   359     if( aRule.MediaServerCount() )
       
   360         {
       
   361         iClause = iClause->ReAllocL( iClause->Length() + 
       
   362             KCmSqlAnd().Length() );
       
   363         iClause->Des().Append( KCmSqlAnd() );
       
   364         iClause = iClause->ReAllocL( iClause->Length() + 
       
   365             KCmSqlLeftBracket().Length() );
       
   366         iClause->Des().Append( KCmSqlLeftBracket() );                
       
   367         }
       
   368     for( TInt k = 0; k < aRule.MediaServerCount(); k++ )
       
   369         {
       
   370         iClause = iClause->ReAllocL( iClause->Length() + 
       
   371             KCmSqlLeftBracket().Length() );
       
   372         iClause->Des().Append( KCmSqlLeftBracket() );                
       
   373         aRule.MediaServerL( k, msId );
       
   374         iClause = iClause->ReAllocL( iClause->Length() + 
       
   375             KCmSqlSelectDeviceId().Length() + sizeof( msId ) );
       
   376         iClause->Des().Append( KCmSqlSelectDeviceId );
       
   377         iClause->Des().AppendNum( msId );
       
   378         iClause = iClause->ReAllocL( iClause->Length() + 
       
   379             KCmSqlRightBracket().Length() );
       
   380         iClause->Des().Append( KCmSqlRightBracket() );          
       
   381         if( k < aRule.MediaServerCount() - 1 )
       
   382             {
       
   383             iClause = iClause->ReAllocL( iClause->Length() + 
       
   384                 KCmSqlOr().Length() );
       
   385             iClause->Des().Append( KCmSqlOr() );            
       
   386             }
       
   387         }
       
   388     if( aRule.MediaServerCount() )
       
   389         {
       
   390         iClause = iClause->ReAllocL( iClause->Length() + 
       
   391             KCmSqlRightBracket().Length() );
       
   392         iClause->Des().Append( KCmSqlRightBracket() );                
       
   393         }                     
       
   394     }
       
   395 
       
   396 // ---------------------------------------------------------------------------
       
   397 // CCmSqlDbClause::FormatOrderingL
       
   398 // ---------------------------------------------------------------------------
       
   399 //
       
   400 void CCmSqlDbClause::FormatOrderingL( TCmFillMethod aMethod )
       
   401     {
       
   402     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatOrderingL"));    
       
   403     
       
   404     switch( aMethod )
       
   405         {
       
   406         case ECmRandom:
       
   407             {
       
   408             iClause = iClause->ReAllocL( iClause->Length() + 
       
   409                 KCmSqlOrderByRandom().Length() );
       
   410             iClause->Des().Append( KCmSqlOrderByRandom() );              
       
   411             break;
       
   412             }
       
   413         case ECmLatest:
       
   414             {
       
   415             iClause = iClause->ReAllocL( iClause->Length() + 
       
   416                 KCmSqlOrderByDate().Length() + KCmSqlDesc().Length() );
       
   417             iClause->Des().Append( KCmSqlOrderByDate() );
       
   418             iClause->Des().Append( KCmSqlDesc() );             
       
   419             break;
       
   420             }        
       
   421         case ECmOldest:
       
   422             {
       
   423             iClause = iClause->ReAllocL( iClause->Length() + 
       
   424                 KCmSqlOrderByDate().Length() + KCmSqlAsc().Length() );
       
   425             iClause->Des().Append( KCmSqlOrderByDate() );
       
   426             iClause->Des().Append( KCmSqlAsc() );
       
   427             break;
       
   428             }        
       
   429         default:
       
   430             {
       
   431             break;
       
   432             }
       
   433         }    
       
   434     }
       
   435 
       
   436 // ---------------------------------------------------------------------------
       
   437 // CCmSqlDbClause::FormatAmountL
       
   438 // ---------------------------------------------------------------------------
       
   439 //
       
   440 void CCmSqlDbClause::FormatAmountL( CCmFillRule& aRule )
       
   441     {
       
   442     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatAmountL"));    
       
   443     
       
   444     if( aRule.LimitType() == EPieces )
       
   445         {
       
   446         iClause = iClause->ReAllocL( iClause->Length() + 
       
   447             KCmSqlLimit().Length() + KCmRuleAmountSize  );
       
   448         iClause->Des().Append( KCmSqlLimit() );
       
   449         iClause->Des().AppendNum( aRule.Amount() );        
       
   450         }
       
   451     else if( aRule.LimitType() == EMbits )
       
   452         {
       
   453         // If amount is wanted in MBytes, no possibility to 
       
   454         // LIMIT result set in this phase
       
   455         }
       
   456     else
       
   457         {
       
   458         // Unlimited rule
       
   459         // Fill quota is used as a maximun amount
       
   460         }    
       
   461     }
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // CCmSqlDbClause::FormatMetadataColumnL
       
   465 // ---------------------------------------------------------------------------
       
   466 //
       
   467 void CCmSqlDbClause::FormatMetadataColumnL( TCmMetadataField aField,
       
   468     TCmMediaType aMediaType )
       
   469     {
       
   470     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatMetadataColumnL"));    
       
   471     
       
   472     if( iFreeText )
       
   473         {
       
   474         delete iFreeText; iFreeText = NULL;
       
   475         }
       
   476     TRACE(Print(_L("[SQL wrapper]\t Formating data field = %d"), 
       
   477         (TInt) aField ));             
       
   478     
       
   479     switch( aField )
       
   480         {       
       
   481         case ECmArtist:
       
   482             {
       
   483             iClause = iClause->ReAllocL( iClause->Length() + 
       
   484                 KCmSqlSelectArtistById().Length() );
       
   485             iClause->Des().Append( KCmSqlSelectArtistById() );
       
   486             break;
       
   487             }                    
       
   488         case ECmAlbum:
       
   489             {
       
   490             iClause = iClause->ReAllocL( iClause->Length() + 
       
   491                 KCmSqlSelectAlbumById().Length() );
       
   492             iClause->Des().Append( KCmSqlSelectAlbumById() );            
       
   493             break;
       
   494             }        
       
   495         case ECmGenre:
       
   496             {
       
   497             iClause = iClause->ReAllocL( iClause->Length() + 
       
   498                 KCmSqlSelectGenreById().Length() );
       
   499             iClause->Des().Append( KCmSqlSelectGenreById() );               
       
   500             break;
       
   501             }        
       
   502         case ECmDuration:
       
   503             {
       
   504             iClause = iClause->ReAllocL( iClause->Length() + 
       
   505                 KCmSqlSelectDuration().Length() );
       
   506             iClause->Des().Append( KCmSqlSelectDuration() );              
       
   507             break;
       
   508             }        
       
   509         case ECmTitle:
       
   510             {
       
   511             iClause = iClause->ReAllocL( iClause->Length() + 
       
   512                 KCmSqlSelectTitle().Length() );
       
   513             iClause->Des().Append( KCmSqlSelectTitle() );
       
   514             break;
       
   515             }        
       
   516         case ECmUpnpClass:
       
   517             {
       
   518             iClause = iClause->ReAllocL( iClause->Length() + 
       
   519                 KCmSqlSelectUpnpclassById().Length() );
       
   520             iClause->Des().Append( KCmSqlSelectUpnpclassById );    
       
   521             break;
       
   522             }        
       
   523         case ECmContentURI:
       
   524             {
       
   525             LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatMetadataColumnL \
       
   526             aField = ECmContentURI"));
       
   527             break;
       
   528             }        
       
   529         case ECmFileSize:
       
   530             {
       
   531             iClause = iClause->ReAllocL( iClause->Length() + 
       
   532                 KCmSqlSelectSize().Length() );
       
   533             iClause->Des().Append( KCmSqlSelectSize() );            
       
   534             break;
       
   535             }        
       
   536         case ECmBitrate:
       
   537             {
       
   538             iClause = iClause->ReAllocL( iClause->Length() + 
       
   539                 KCmSqlSelectBitrate().Length() );
       
   540             iClause->Des().Append( KCmSqlSelectBitrate );    
       
   541             break;
       
   542             }        
       
   543         case ECmResolution:
       
   544             {
       
   545             iClause = iClause->ReAllocL( iClause->Length() +
       
   546                 KCmSqlSelectResolutionById().Length() );
       
   547             iClause->Des().Append( KCmSqlSelectResolutionById );    
       
   548             break;
       
   549             }
       
   550         case ECmDate:
       
   551             {
       
   552             iClause = iClause->ReAllocL( iClause->Length() +
       
   553                 KCmSqlSelectDate().Length() );
       
   554             iClause->Des().Append( KCmSqlSelectDate );
       
   555             break;
       
   556             }  
       
   557         case ECmFreeText:
       
   558             {
       
   559             switch( aMediaType )
       
   560                 {
       
   561                 case ECmAudio:
       
   562                     {
       
   563                     // Title, Artist or album field contains specified field
       
   564                     iFreeText = KCmSqlFreeTextSelectAudio().AllocL();
       
   565                     break;
       
   566                     }                
       
   567                 case ECmImage:           
       
   568                 case ECmVideo: 
       
   569                     {
       
   570                     // Title contains specified field
       
   571                     iFreeText = KCmSqlFreeTextSelectImageOrVideo().AllocL();
       
   572                     break;
       
   573                     }
       
   574                 default:
       
   575                     {
       
   576                     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::\
       
   577                     FormatMetadataColumnL mediatype not found"));                    
       
   578                     break;
       
   579                     }
       
   580                 }
       
   581             break;
       
   582             }
       
   583         default:
       
   584             {
       
   585             LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatMetadataColumnL \
       
   586             field not found"));
       
   587             break;
       
   588             }    
       
   589         }      
       
   590     }
       
   591 
       
   592 // ---------------------------------------------------------------------------
       
   593 // CCmSqlDbClause::FormatOperatorL
       
   594 // ---------------------------------------------------------------------------
       
   595 //
       
   596 void CCmSqlDbClause::FormatOperatorL( TCmMetadataField aMetadataColum, 
       
   597     TCmOperatorType aOper )
       
   598     {
       
   599     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatOperatorL"));    
       
   600     
       
   601     switch( aOper )
       
   602         {           
       
   603         case ECmEquals:
       
   604             {
       
   605             iClause = iClause->ReAllocL( iClause->Length() + 
       
   606                 KCmSqlEqual().Length() + KCmSqlAppendString().Length() );
       
   607             iClause->Des().Append( KCmSqlEqual() );
       
   608             iClause->Des().Append( KCmSqlAppendInteger() );
       
   609             break;
       
   610             }
       
   611         // Greater and smaller operator are always integer operators    
       
   612         case ECmGreater:
       
   613             {
       
   614             if( aMetadataColum != ECmDate && aMetadataColum != ECmFileSize && 
       
   615                 aMetadataColum != ECmDuration )
       
   616                 {            
       
   617                 iClause = iClause->ReAllocL( iClause->Length() + 
       
   618                                              KCmSqlGreater().Length() + 
       
   619                                              KCmSqlAppendInteger().Length() );
       
   620                 iClause->Des().Append( KCmSqlGreater() );
       
   621                 iClause->Des().Append( KCmSqlAppendInteger() );
       
   622                 }
       
   623             else
       
   624                 {
       
   625                 iClause = iClause->ReAllocL( iClause->Length() + 
       
   626                     KCmSqlGreater().Length() + KCmSqlAppendLong().Length() );
       
   627                 iClause->Des().Append( KCmSqlGreater() );                
       
   628                 }    
       
   629             break;
       
   630             } 
       
   631         case ECmSmaller:
       
   632             {
       
   633             if( aMetadataColum != ECmDate && aMetadataColum != ECmFileSize &&
       
   634                 aMetadataColum != ECmDuration )
       
   635                 {
       
   636                 iClause = iClause->ReAllocL( iClause->Length() + 
       
   637                                              KCmSqlSmaller().Length() + 
       
   638                                              KCmSqlAppendInteger().Length() );
       
   639                 iClause->Des().Append( KCmSqlSmaller() );
       
   640                 iClause->Des().Append( KCmSqlAppendInteger() );
       
   641                 }
       
   642             else
       
   643                 {
       
   644                 iClause = iClause->ReAllocL( iClause->Length() + 
       
   645                     KCmSqlSmaller().Length() + KCmSqlAppendLong().Length() );
       
   646                 iClause->Des().Append( KCmSqlSmaller() );               
       
   647                 }                  
       
   648             break;
       
   649             } 
       
   650         case ECmConsistOf:
       
   651             {
       
   652             iClause = iClause->ReAllocL( iClause->Length() + 
       
   653                 KCmSqlLike().Length() + KCmSqlConsistOf().Length() );
       
   654             iClause->Des().Append( KCmSqlLike() );
       
   655             iClause->Des().Append( KCmSqlConsistOf() );             
       
   656             break;
       
   657             } 
       
   658         case ECmBeginsWith:
       
   659             {
       
   660             iClause = iClause->ReAllocL( iClause->Length() + 
       
   661                 KCmSqlLike().Length() + KCmSqlBeginsWith().Length() );
       
   662             iClause->Des().Append( KCmSqlLike() );
       
   663             iClause->Des().Append( KCmSqlBeginsWith() );              
       
   664             break;
       
   665             }
       
   666         case ECmEndsWith:
       
   667             {
       
   668             iClause = iClause->ReAllocL( iClause->Length() + 
       
   669                 KCmSqlLike().Length() + KCmSqlEndsWith().Length() );
       
   670             iClause->Des().Append( KCmSqlLike() );
       
   671             iClause->Des().Append( KCmSqlEndsWith() );              
       
   672             break;
       
   673             }
       
   674         default:
       
   675             {
       
   676             break;
       
   677             }
       
   678         }    
       
   679     }
       
   680     
       
   681 // ---------------------------------------------------------------------------
       
   682 // CCmSqlDbClause::FormatProperyQueryL
       
   683 // ---------------------------------------------------------------------------
       
   684 //     
       
   685 void CCmSqlDbClause::FormatProperyQueryL( TCmMetadataField aField,
       
   686     TCmMediaType aMedia )
       
   687     {
       
   688     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatProperyQueryL"));    
       
   689     
       
   690     if( iClause )
       
   691         {
       
   692         delete iClause;
       
   693         iClause = NULL;
       
   694         }
       
   695     
       
   696     switch( aField )
       
   697         {
       
   698         case ECmArtist:
       
   699             {           
       
   700             iClause = HBufC8::NewL( KCmSqlSelectPropertyValues().Length() + 
       
   701                 KCmSqlArtist().Length() );
       
   702             TPtr8 ptr( iClause->Des() );    
       
   703             ptr.Format( KCmSqlSelectPropertyValues, &KCmSqlArtist );
       
   704             break;
       
   705             }        
       
   706         case ECmAlbum:
       
   707             {
       
   708             iClause = HBufC8::NewL( KCmSqlSelectPropertyValues().Length() + 
       
   709                 KCmSqlAlbum().Length() );
       
   710             TPtr8 ptr( iClause->Des() );    
       
   711             ptr.Format( KCmSqlSelectPropertyValues, &KCmSqlAlbum );           
       
   712             break;
       
   713             }        
       
   714         case ECmGenre:
       
   715             {
       
   716             iClause = HBufC8::NewL( KCmSqlSelectPropertyValues().Length() + 
       
   717                 KCmSqlGenre().Length() );
       
   718             TPtr8 ptr( iClause->Des() );    
       
   719             ptr.Format( KCmSqlSelectPropertyValues, &KCmSqlGenre );
       
   720             break;
       
   721             }        
       
   722         case ECmUpnpClass:
       
   723             {
       
   724             iClause = HBufC8::NewL( KCmSqlSelectPropertyValues().Length() + 
       
   725                 KCmSqlUpnpclass().Length() );
       
   726             TPtr8 ptr( iClause->Des() );    
       
   727             ptr.Format( KCmSqlSelectPropertyValues, &KCmSqlUpnpclass );
       
   728             break;
       
   729             }
       
   730         case ECmTitle:
       
   731             {           
       
   732             iClause = HBufC8::NewL( KCmSqlSelectTitleAndId().Length() + 
       
   733                 sizeof( aMedia ) );
       
   734             TPtr8 ptr( iClause->Des() );    
       
   735             ptr.Format( KCmSqlSelectTitleAndId, aMedia );                 
       
   736             break;
       
   737             }
       
   738         case ECmResolution:
       
   739             {
       
   740             iClause = HBufC8::NewL( KCmSqlSelectPropertyValues().Length() + 
       
   741                 KCmSqlResolutions().Length() );
       
   742             TPtr8 ptr( iClause->Des() );    
       
   743             ptr.Format( KCmSqlSelectPropertyValues, &KCmSqlResolutions );
       
   744             break;
       
   745             }
       
   746         case ECmProfileId:
       
   747             {
       
   748             iClause = HBufC8::NewL( KCmSqlSelectPropertyValues().Length() + 
       
   749                 KCmSqlResolutions().Length() );
       
   750             TPtr8 ptr( iClause->Des() );    
       
   751             ptr.Format( KCmSqlSelectPropertyValues, &KCmSqlUpnpProfiles );
       
   752             break;
       
   753             }
       
   754         default:
       
   755             {
       
   756             break;
       
   757             }
       
   758         }
       
   759     }
       
   760 
       
   761 // ---------------------------------------------------------------------------
       
   762 // CCmSqlDbClause::FormatProfileFiltersL
       
   763 // ---------------------------------------------------------------------------
       
   764 //     
       
   765 void CCmSqlDbClause::FormatProfileFiltersL(  )
       
   766     {
       
   767     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatProfileFiltersL"));    
       
   768     
       
   769     if( iProfileFilters.Count() )
       
   770         {
       
   771         iClause = iClause->ReAllocL( iClause->Length() + 
       
   772             KCmSqlAnd().Length() );
       
   773         iClause->Des().Append( KCmSqlAnd() );
       
   774         iClause = iClause->ReAllocL( iClause->Length() + 
       
   775             KCmSqlLeftBracket().Length() );
       
   776         iClause->Des().Append( KCmSqlLeftBracket() );                
       
   777         }
       
   778     for( TInt k = 0; k < iProfileFilters.Count(); k++ )
       
   779         {
       
   780         iClause = iClause->ReAllocL( iClause->Length() + 
       
   781             KCmSqlLeftBracket().Length() );
       
   782         iClause->Des().Append( KCmSqlLeftBracket() );                
       
   783 
       
   784         iClause = iClause->ReAllocL( iClause->Length() + 
       
   785                                      KCmSqlFilterUpnpProfileId().Length() + 
       
   786                                      sizeof( iProfileFilters[k] ) );
       
   787         iClause->Des().Append( KCmSqlFilterUpnpProfileId );
       
   788         iClause->Des().AppendNum( iProfileFilters[k] );
       
   789         
       
   790         iClause = iClause->ReAllocL( iClause->Length() + 
       
   791             KCmSqlRightBracket().Length() );
       
   792         iClause->Des().Append( KCmSqlRightBracket() );          
       
   793         if( k < iProfileFilters.Count() - 1 )
       
   794             {
       
   795             iClause = iClause->ReAllocL( iClause->Length() + 
       
   796                 KCmSqlAnd().Length() );
       
   797             iClause->Des().Append( KCmSqlAnd() );            
       
   798             }
       
   799         }
       
   800     if( iProfileFilters.Count() )
       
   801         {
       
   802         iClause = iClause->ReAllocL( iClause->Length() + 
       
   803             KCmSqlRightBracket().Length() );
       
   804         iClause->Des().Append( KCmSqlRightBracket() );                
       
   805         }
       
   806     iClause = iClause->ReAllocL( iClause->Length() + 
       
   807         KCmSqlRightBracket().Length() );
       
   808     iClause->Des().Append( KCmSqlRightBracket() );          
       
   809     }
       
   810 
       
   811 // ---------------------------------------------------------------------------
       
   812 // CCmSqlDbClause::FormatPropertyFilterL
       
   813 // ---------------------------------------------------------------------------
       
   814 //
       
   815 void CCmSqlDbClause::FormatPropertyFilterL( 
       
   816     CCmSqlPropertyCollector& aPropertys )
       
   817     {    
       
   818     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::FormatPropertyFilterL"));    
       
   819     
       
   820     CCmSqlPropertyContainer* container = NULL;
       
   821     HBufC8* temp = NULL;   
       
   822     for( TInt i = 0; i < aPropertys.PropertyContainerCount(); i++ )
       
   823         {
       
   824         container = aPropertys.PropertyContainer( i );        
       
   825         switch( container->Type() )
       
   826             {
       
   827             case ECmTitle:
       
   828                 {
       
   829                 AddPropertyFilterL( *container, 
       
   830                     KCmSqlSelectTitle(), temp );                   
       
   831                 break;
       
   832                 }
       
   833             case ECmArtist:
       
   834                 {
       
   835                 AddPropertyFilterL( *container, 
       
   836                     KCmSqlSelectArtistById(), temp );                               
       
   837                 break;
       
   838                 }
       
   839             case ECmAlbum:
       
   840                 {
       
   841                 AddPropertyFilterL( *container, 
       
   842                     KCmSqlSelectAlbumById(), temp );                                
       
   843                 break;
       
   844                 }
       
   845             case ECmGenre:
       
   846                 {
       
   847                 AddPropertyFilterL( *container, 
       
   848                     KCmSqlSelectGenreById(), temp );
       
   849                 break;
       
   850                 }
       
   851             default:
       
   852                 {
       
   853                 break;
       
   854                 }                                                                
       
   855             }
       
   856         container = NULL;
       
   857         }
       
   858     
       
   859     if( temp )
       
   860         {
       
   861         iClause = iClause->ReAllocL( iClause->Length() + temp->Length() );
       
   862         iClause->Des().Append( *temp );
       
   863         delete temp;        
       
   864         }
       
   865     }
       
   866 
       
   867 // ---------------------------------------------------------------------------
       
   868 // CCmSqlDbClause::AddPropertyFilterL
       
   869 // ---------------------------------------------------------------------------
       
   870 //
       
   871 void CCmSqlDbClause::AddPropertyFilterL( 
       
   872     CCmSqlPropertyContainer& aContainer, const TDesC8& aField, 
       
   873     HBufC8*& aClause )
       
   874     {
       
   875     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::AddPropertyFilterL"));    
       
   876     
       
   877     if( aClause && aContainer.PropertyItemCount() )
       
   878         {
       
   879         aClause = aClause->ReAllocL( aClause->Length() + 
       
   880             KCmSqlAnd().Length() + ( 2 * KCmSqlSpace().Length() ) );
       
   881         aClause->Des().Append( KCmSqlSpace() );
       
   882         aClause->Des().Append( KCmSqlAnd() );
       
   883         aClause->Des().Append( KCmSqlSpace() );                       
       
   884         }
       
   885     if( aContainer.PropertyItemCount() && !aClause )
       
   886         {
       
   887         aClause = KCmSqlAnd().AllocL();
       
   888         }
       
   889     if( aContainer.PropertyItemCount() && aClause )
       
   890         {
       
   891         aClause = aClause->ReAllocL( aClause->Length() +
       
   892         		KCmSqlLeftBracket().Length() );
       
   893         aClause->Des().Append( KCmSqlLeftBracket );
       
   894         
       
   895         for( TInt j = 0; j < aContainer.PropertyItemCount() ; j++ )
       
   896             {
       
   897             aClause = aClause->ReAllocL( aClause->Length() + 
       
   898                 KCmSqlSelectTitle().Length() + 
       
   899                 sizeof( aContainer.PropertyItem( j )->Id() ) );
       
   900             aClause->Des().Append( aField );
       
   901             aClause->Des().AppendNum( 
       
   902                 aContainer.PropertyItem( j )->Id() );
       
   903            if( j < aContainer.PropertyItemCount() - 1 )
       
   904                 {
       
   905                 aClause = aClause->ReAllocL( aClause->Length() + 
       
   906                     KCmSqlOr().Length() + ( 2 * KCmSqlSpace().Length() ) );
       
   907                 aClause->Des().Append( KCmSqlSpace() );
       
   908                 aClause->Des().Append( KCmSqlOr() );
       
   909                 aClause->Des().Append( KCmSqlSpace() );             
       
   910                 }                                                         
       
   911             }
       
   912         
       
   913         aClause = aClause->ReAllocL( aClause->Length() +
       
   914         		KCmSqlRightBracket().Length() );
       
   915         aClause->Des().Append( KCmSqlRightBracket );
       
   916         
       
   917         for( TInt k = 0; k < aContainer.PropertyItemCount() ; k++ )
       
   918             {                        
       
   919             aContainer.DeletePropertyItem( k );
       
   920             k--;
       
   921             }
       
   922         }
       
   923     }
       
   924     
       
   925 // ---------------------------------------------------------------------------
       
   926 // Default constructor
       
   927 // ---------------------------------------------------------------------------
       
   928 //    
       
   929 CCmSqlDbClause::CCmSqlDbClause()
       
   930     {
       
   931     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::CCmSqlDbClause"));    
       
   932     }
       
   933  
       
   934 // ---------------------------------------------------------------------------
       
   935 // ConstructL
       
   936 // ---------------------------------------------------------------------------
       
   937 //   
       
   938 void CCmSqlDbClause::ConstructL()
       
   939     {
       
   940     LOG(_L("[SQL Wrapper]\t CCmSqlDbClause::ConstructL"));    
       
   941     }    
       
   942 
       
   943 // End of file
       
   944