mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbcommon/src/mpxdbtable.cpp
changeset 0 ff3acec5bc43
child 11 13afc0e517bd
equal deleted inserted replaced
-1:000000000000 0:ff3acec5bc43
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Base class for all table classes.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <mpxlog.h>
       
    21 #include <mpxmedia.h>
       
    22 #include <mpxmediaarray.h>
       
    23 
       
    24 #include "mpxdbcommonstd.h"
       
    25 #include "mpxdbcommonutil.h"
       
    26 #include "mpxdbmanager.h"
       
    27 #include "mpxdbtable.h"
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ==============================
       
    30 
       
    31 // ----------------------------------------------------------------------------
       
    32 // Constructor
       
    33 // ----------------------------------------------------------------------------
       
    34 //
       
    35 EXPORT_C CMPXDbTable::CMPXDbTable(
       
    36     CMPXDbManager& aDbManager) :
       
    37     iDbManager(aDbManager)
       
    38     {
       
    39     MPX_FUNC("CMPXDbTable::CMPXDbTable");
       
    40     }
       
    41 
       
    42 // ----------------------------------------------------------------------------
       
    43 // Second phase constructor.
       
    44 // ----------------------------------------------------------------------------
       
    45 //
       
    46 EXPORT_C void CMPXDbTable::BaseConstructL()
       
    47     {
       
    48     MPX_FUNC("CMPXDbTable::BaseConstructL");
       
    49     iDbManager.RegisterTableL(*this);
       
    50     }
       
    51 
       
    52 // ----------------------------------------------------------------------------
       
    53 // Destructor
       
    54 // ----------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C CMPXDbTable::~CMPXDbTable()
       
    57     {
       
    58     MPX_FUNC("CMPXDbTable::~CMPXDbTable");
       
    59     }
       
    60 
       
    61 // ----------------------------------------------------------------------------
       
    62 // CMPXDbTable::UpdateMediaL default implementation
       
    63 // ----------------------------------------------------------------------------
       
    64 //
       
    65 EXPORT_C void CMPXDbTable::UpdateMediaL(
       
    66     RSqlStatement& /* aRecord */,
       
    67     const TArray<TMPXAttribute>& /* aAttrs */,
       
    68     CMPXMedia& /* aMedia */)
       
    69     {
       
    70     // do nothing
       
    71     }
       
    72 
       
    73 // ----------------------------------------------------------------------------
       
    74 // CMPXDbTable::ExecuteMediaQueryL
       
    75 // ----------------------------------------------------------------------------
       
    76 //
       
    77 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
    78     const TArray<TMPXAttribute>& aAttrs,
       
    79     CMPXMediaArray& aMediaArray,
       
    80     const TDesC& aQuery,
       
    81     TInt aValue)
       
    82     {
       
    83     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
    84 
       
    85     HBufC* query = HBufC::NewLC(aQuery.Length() + KMCIntegerLen);
       
    86     query->Des().Format(aQuery, aValue);
       
    87     ExecuteMediaQueryL(aAttrs, aMediaArray, *query);
       
    88     CleanupStack::PopAndDestroy(query);
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CMPXDbTable::ExecuteMediaQueryL
       
    93 // ----------------------------------------------------------------------------
       
    94 //
       
    95 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
    96     const TArray<TMPXAttribute>& aAttrs,
       
    97     CMPXMediaArray& aMediaArray,
       
    98     const TDesC& aQuery,
       
    99     const TDesC& aValue)
       
   100     {
       
   101     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   102 
       
   103     HBufC* query = HBufC::NewLC(aQuery.Length() + aValue.Length());
       
   104     query->Des().Format(aQuery, &aValue);
       
   105     ExecuteMediaQueryL(aAttrs, aMediaArray, *query);
       
   106     CleanupStack::PopAndDestroy(query);
       
   107     }
       
   108 
       
   109 // ----------------------------------------------------------------------------
       
   110 // CMPXDbTable::ExecuteMediaQueryL
       
   111 // ----------------------------------------------------------------------------
       
   112 //
       
   113 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   114     const TArray<TMPXAttribute>& aAttrs,
       
   115     CMPXMediaArray& aMediaArray,
       
   116     const TDesC& aQuery,
       
   117     TInt aValue1,
       
   118     TInt aValue2)
       
   119     {
       
   120     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   121 
       
   122     HBufC* query = HBufC::NewLC(aQuery.Length() + 2 * KMCIntegerLen);
       
   123     query->Des().Format(aQuery, aValue1, aValue2);
       
   124     ExecuteMediaQueryL(aAttrs, aMediaArray, *query);
       
   125     CleanupStack::PopAndDestroy(query);
       
   126     }
       
   127 
       
   128 // ----------------------------------------------------------------------------
       
   129 // CMPXDbTable::ExecuteMediaQueryL
       
   130 // ----------------------------------------------------------------------------
       
   131 //
       
   132 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   133     const TArray<TMPXAttribute>& aAttrs,
       
   134     CMPXMediaArray& aMediaArray,
       
   135     const TDesC& aQuery,
       
   136     const TDesC& aValue1,
       
   137     TInt aValue2)
       
   138     {
       
   139     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   140 
       
   141     HBufC* query = HBufC::NewLC(aQuery.Length() + aValue1.Length() + KMCIntegerLen);
       
   142     query->Des().Format(aQuery, &aValue1, aValue2);
       
   143     ExecuteMediaQueryL(aAttrs, aMediaArray, *query);
       
   144     CleanupStack::PopAndDestroy(query);
       
   145     }
       
   146 
       
   147 // ----------------------------------------------------------------------------
       
   148 // CMPXDbTable::ExecuteMediaQueryL
       
   149 // ----------------------------------------------------------------------------
       
   150 //
       
   151 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   152     const TArray<TMPXAttribute>& aAttrs,
       
   153     CMPXMediaArray& aMediaArray,
       
   154     const TDesC& aQuery,
       
   155     const TDesC& aValue1,
       
   156     TInt aValue2,
       
   157     const TDesC& aValue3,
       
   158     TInt aValue4)
       
   159     {
       
   160     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   161 
       
   162     HBufC* query = HBufC::NewLC(aQuery.Length() +
       
   163                                 aValue1.Length() +
       
   164                                 aValue3.Length() +
       
   165                                 2*KMCIntegerLen);
       
   166     query->Des().Format(aQuery, &aValue1, aValue2, &aValue3, aValue4);
       
   167     ExecuteMediaQueryL(aAttrs, aMediaArray, *query);
       
   168     CleanupStack::PopAndDestroy(query);
       
   169     }
       
   170 
       
   171 // ----------------------------------------------------------------------------
       
   172 // CMPXDbTable::ExecuteMediaQueryL
       
   173 // ----------------------------------------------------------------------------
       
   174 //
       
   175 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   176     const TArray<TMPXAttribute>& aAttrs,
       
   177     CMPXMediaArray& aMediaArray,
       
   178     const TDesC& aQuery)
       
   179     {
       
   180     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   181 
       
   182     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(aQuery));
       
   183     CleanupClosePushL(recordset);
       
   184 
       
   185     TInt pPath(0);
       
   186     if (aMediaArray.Count())
       
   187         {
       
   188         CMPXMedia* pMedia = aMediaArray[0];
       
   189         if (pMedia->IsSupported(KMPXMediaGeneralValue))
       
   190             { // Query excuted by OpenL
       
   191             pPath = pMedia->ValueTObjectL<TInt>(KMPXMediaGeneralValue);
       
   192             MPX_ASSERT(pPath);
       
   193             }
       
   194         }
       
   195 
       
   196     TInt err(KErrNone);
       
   197     RArray<TMPXItemId> ids;
       
   198     CleanupClosePushL(ids);
       
   199     while ((err = recordset.Next()) == KSqlAtRow)
       
   200         {
       
   201         CMPXMedia* media = CMPXMedia::NewL();
       
   202         CleanupStack::PushL(media);
       
   203 
       
   204         UpdateMediaL(recordset, aAttrs, *media);
       
   205         if (media->IsSupported(KMPXMediaGeneralId) && pPath)
       
   206             {
       
   207             ids.AppendL(media->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   208             }
       
   209         aMediaArray.AppendL(*media);
       
   210         CleanupStack::PopAndDestroy(media);
       
   211         }
       
   212 
       
   213     // Append ids to the returned path
       
   214     if (pPath)
       
   215         {
       
   216         ((CMPXCollectionPath*)pPath)->AppendL(ids.Array());
       
   217         }
       
   218     CleanupStack::PopAndDestroy(&ids);
       
   219     CleanupStack::PopAndDestroy(&recordset);
       
   220 
       
   221     if (err!= KSqlAtEnd)
       
   222         {
       
   223         User::Leave(KErrCorrupt);
       
   224         }
       
   225     }
       
   226 
       
   227 // ----------------------------------------------------------------------------
       
   228 // CMPXDbTable::ExecuteMediaQueryL
       
   229 // ----------------------------------------------------------------------------
       
   230 //
       
   231 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   232 	TInt aDrive,
       
   233 	const TArray<TMPXAttribute>& aAttrs,
       
   234 	CMPXMediaArray& aMediaArray,
       
   235 	const TDesC& aQuery,
       
   236 	TInt aPlaylistId)
       
   237     {
       
   238     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   239 
       
   240     HBufC* query = HBufC::NewLC(aQuery.Length() + KMCIntegerLen);
       
   241     query->Des().Format(aQuery, aPlaylistId);
       
   242     ExecuteMediaQueryOnDriveL(aDrive, aAttrs, aMediaArray, *query);
       
   243     CleanupStack::PopAndDestroy(query);
       
   244     }
       
   245 
       
   246 // ----------------------------------------------------------------------------
       
   247 // CMPXDbTable::ExecuteMediaQueryOnDriveL
       
   248 // ----------------------------------------------------------------------------
       
   249 //
       
   250 EXPORT_C void CMPXDbTable::ExecuteMediaQueryOnDriveL(
       
   251 	TInt aDrive,
       
   252     const TArray<TMPXAttribute>& aAttrs,
       
   253     CMPXMediaArray& aMediaArray,
       
   254     const TDesC& aQuery)
       
   255     {
       
   256     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   257 
       
   258     RSqlStatement recordset(iDbManager.ExecuteSelectQueryOnAllDrivesL(aDrive,aQuery));
       
   259     CleanupClosePushL(recordset);
       
   260 
       
   261     TInt pPath(0);
       
   262     if (aMediaArray.Count())
       
   263         {
       
   264         CMPXMedia* pMedia = aMediaArray[0];
       
   265         if (pMedia->IsSupported(KMPXMediaGeneralValue))
       
   266             { // Query excuted by OpenL
       
   267             pPath = pMedia->ValueTObjectL<TInt>(KMPXMediaGeneralValue);
       
   268             MPX_ASSERT(pPath);
       
   269             }
       
   270         }
       
   271 
       
   272     TInt err(KErrNone);
       
   273     RArray<TMPXItemId> ids;
       
   274     CleanupClosePushL(ids);
       
   275     while ((err = recordset.Next()) == KSqlAtRow)
       
   276         {
       
   277         CMPXMedia* media = CMPXMedia::NewL();
       
   278         CleanupStack::PushL(media);
       
   279 
       
   280         UpdateMediaL(recordset, aAttrs, *media);
       
   281         if (media->IsSupported(KMPXMediaGeneralId) && pPath)
       
   282             {
       
   283             ids.AppendL(media->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   284             }
       
   285         aMediaArray.AppendL(*media);
       
   286         CleanupStack::PopAndDestroy(media);
       
   287         }
       
   288 
       
   289     // Append ids to the returned path
       
   290     if (pPath)
       
   291         {
       
   292         ((CMPXCollectionPath*)pPath)->AppendL(ids.Array());
       
   293         }
       
   294     CleanupStack::PopAndDestroy(&ids);
       
   295     CleanupStack::PopAndDestroy(&recordset);
       
   296 
       
   297     if (err!= KSqlAtEnd)
       
   298         {
       
   299         User::Leave(KErrCorrupt);
       
   300         }
       
   301     }
       
   302 
       
   303 // ----------------------------------------------------------------------------
       
   304 // CMPXDbTable::ExecuteMediaQueryL
       
   305 // ----------------------------------------------------------------------------
       
   306 //
       
   307 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   308     const TArray<TMPXAttribute>& aAttrs,
       
   309     CMPXMediaArray& aMediaArray,
       
   310     const TBool aAsc,
       
   311     const TDesC& aQuery)
       
   312     {
       
   313 
       
   314     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   315 
       
   316     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(aQuery));
       
   317     CleanupClosePushL(recordset);
       
   318 
       
   319     TInt err(KErrNone);
       
   320     while ((err = recordset.Next()) == KSqlAtRow)
       
   321         {
       
   322         CMPXMedia* media = CMPXMedia::NewL();
       
   323         CleanupStack::PushL(media);
       
   324 
       
   325         UpdateMediaL(recordset, aAttrs, *media);
       
   326         if (aAsc != EFalse)
       
   327             {
       
   328             aMediaArray.AppendL(*media);
       
   329             }
       
   330         else
       
   331             {
       
   332             aMediaArray.InsertL(*media, 0);
       
   333             }
       
   334         CleanupStack::PopAndDestroy(media);
       
   335         }
       
   336 
       
   337     CleanupStack::PopAndDestroy(&recordset);
       
   338 
       
   339 
       
   340     if (err!= KSqlAtEnd)
       
   341         {
       
   342         User::Leave(KErrCorrupt);
       
   343         }
       
   344     }
       
   345 
       
   346 
       
   347 // ----------------------------------------------------------------------------
       
   348 // CMPXDbTable::ExecuteMediaQueryL
       
   349 // ----------------------------------------------------------------------------
       
   350 //
       
   351 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   352     const TArray<TMPXAttribute>& aAttrs,
       
   353     CMPXMedia& aMedia,
       
   354     const TDesC& aQuery)
       
   355     {
       
   356     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   357 
       
   358     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(aQuery));
       
   359     CleanupClosePushL(recordset);
       
   360 
       
   361     if (recordset.Next() != KSqlAtRow)
       
   362         {
       
   363         User::Leave(KErrNotFound);
       
   364         }
       
   365 
       
   366     UpdateMediaL(recordset, aAttrs, aMedia);
       
   367     CleanupStack::PopAndDestroy(&recordset);
       
   368     }
       
   369 
       
   370 // ----------------------------------------------------------------------------
       
   371 // CMPXDbTable::ExecuteMediaQueryL
       
   372 // ----------------------------------------------------------------------------
       
   373 //
       
   374 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   375     const TArray<TMPXAttribute>& aAttrs,
       
   376     CMPXMedia& aMedia,
       
   377     const TDesC& aQuery,
       
   378     TInt aValue)
       
   379     {
       
   380     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   381 
       
   382     HBufC* query = HBufC::NewLC(aQuery.Length() + KMCIntegerLen);
       
   383     query->Des().Format(aQuery, aValue);
       
   384     ExecuteMediaQueryL(aAttrs, aMedia, *query);
       
   385     CleanupStack::PopAndDestroy(query);
       
   386     }
       
   387 
       
   388 // ----------------------------------------------------------------------------
       
   389 // CMPXDbTable::ExecuteMediaQueryL
       
   390 // ----------------------------------------------------------------------------
       
   391 //
       
   392 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   393     const TArray<TMPXAttribute>& aAttrs,
       
   394     CMPXMediaArray& aMediaArray,
       
   395     const TDesC& aQuery,
       
   396     const TDesC& aValue1,
       
   397     const TDesC& aValue2)
       
   398     {
       
   399     MPX_FUNC("CMPXDbTable::ExecuteMediaQueryL");
       
   400 
       
   401     HBufC* query = HBufC::NewLC(aQuery.Length() + aValue1.Length() + aValue2.Length());
       
   402     query->Des().Format(aQuery, &aValue1, &aValue2);
       
   403     ExecuteMediaQueryL(aAttrs, aMediaArray, *query);
       
   404     CleanupStack::PopAndDestroy(query);
       
   405     }
       
   406 
       
   407 // ----------------------------------------------------------------------------
       
   408 // CMPXDbTable::ExecuteIntQueryL
       
   409 // ----------------------------------------------------------------------------
       
   410 //
       
   411 EXPORT_C TUint32 CMPXDbTable::ExecuteIntQueryL(
       
   412     const TDesC& aQuery)
       
   413     {
       
   414     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(aQuery));
       
   415     CleanupClosePushL(recordset);
       
   416 
       
   417     if (recordset.Next() != KSqlAtRow)
       
   418         {
       
   419         User::Leave(KErrNotFound);
       
   420         }
       
   421 
       
   422     TUint32 value(recordset.ColumnInt64(KMPXTableDefaultIndex));
       
   423     CleanupStack::PopAndDestroy(&recordset);
       
   424 
       
   425     return value;
       
   426     }
       
   427 
       
   428 // ----------------------------------------------------------------------------
       
   429 // CMPXDbTable::ExecuteIntQueryL
       
   430 // ----------------------------------------------------------------------------
       
   431 //
       
   432 EXPORT_C TUint32 CMPXDbTable::ExecuteIntQueryL(
       
   433     const TDesC& aQuery,
       
   434     TUint32 aValue)
       
   435     {
       
   436     MPX_FUNC("CMPXDbTable::ExecuteIntQueryL");
       
   437 
       
   438     HBufC* query = HBufC::NewLC(aQuery.Length() + KMCIntegerLen);
       
   439     query->Des().Format(aQuery, aValue);
       
   440     TUint32 ret(ExecuteIntQueryL(*query));
       
   441     CleanupStack::PopAndDestroy(query);
       
   442 
       
   443     return ret;
       
   444     }
       
   445 
       
   446 // ----------------------------------------------------------------------------
       
   447 // CMPXDbTable::ExecuteIntQueryL
       
   448 // ----------------------------------------------------------------------------
       
   449 //
       
   450 EXPORT_C TUint32 CMPXDbTable::ExecuteIntQueryL(
       
   451     const TDesC& aQuery,
       
   452     const TDesC& aValue1,
       
   453     TUint32 aValue2)
       
   454     {
       
   455     MPX_FUNC("CMPXDbTable::ExecuteIntQueryL");
       
   456 
       
   457     // replace single quotes
       
   458     // reserve double the value length for single quote duplicates
       
   459     HBufC* value1 = HBufC::NewLC(aValue1.Length() * 2);
       
   460     TPtr value1Ptr(value1->Des());
       
   461     MPXDbCommonUtil::FindAndReplaceSingleQuote(aValue1, value1Ptr);
       
   462 
       
   463     // construct the query
       
   464     HBufC* query = HBufC::NewLC(aQuery.Length() + value1Ptr.Length() + KMCIntegerLen);
       
   465     query->Des().Format(aQuery, value1, aValue2);
       
   466 
       
   467     // execute the query
       
   468     TUint32 ret(ExecuteIntQueryL(*query));
       
   469 
       
   470     CleanupStack::PopAndDestroy(query);
       
   471     CleanupStack::PopAndDestroy(value1);
       
   472 
       
   473     return ret;
       
   474     }
       
   475 
       
   476 // ----------------------------------------------------------------------------
       
   477 // CMPXDbTable::ExecuteIntQueryL
       
   478 // ----------------------------------------------------------------------------
       
   479 //
       
   480 EXPORT_C TUint32 CMPXDbTable::ExecuteIntQueryL(
       
   481     const TDesC& aQuery,
       
   482     const TDesC& aValue)
       
   483     {
       
   484     MPX_FUNC("CMPXDbTable::ExecuteIntQueryL");
       
   485 
       
   486     // replace single quotes
       
   487     HBufC* value = HBufC::NewLC(aValue.Length() * 2);
       
   488     TPtr valuePtr(value->Des());
       
   489     MPXDbCommonUtil::FindAndReplaceSingleQuote(aValue, valuePtr);
       
   490 
       
   491     // construct the query
       
   492     HBufC* query = HBufC::NewLC(aQuery.Length() + valuePtr.Length());
       
   493     query->Des().Format(aQuery, value);
       
   494 
       
   495     // execute the query
       
   496     TUint32 ret(ExecuteIntQueryL(*query));
       
   497 
       
   498     CleanupStack::PopAndDestroy(query);
       
   499     CleanupStack::PopAndDestroy(value);
       
   500 
       
   501     return ret;
       
   502     }
       
   503 
       
   504 // ----------------------------------------------------------------------------
       
   505 // CMPXDbTable::ExecuteIntQueryL
       
   506 // ----------------------------------------------------------------------------
       
   507 //
       
   508 EXPORT_C TUint32 CMPXDbTable::ExecuteIntQueryL(
       
   509     const TDesC& aQuery,
       
   510     TUint32 aValue1,
       
   511     TUint32 aValue2)
       
   512     {
       
   513     MPX_FUNC("CMPXDbTable::ExecuteIntQueryL");
       
   514 
       
   515     HBufC* query = HBufC::NewLC(aQuery.Length() + 2 * KMCIntegerLen);
       
   516     query->Des().Format(aQuery, aValue1, aValue2);
       
   517     TUint32 ret(ExecuteIntQueryL(*query));
       
   518     CleanupStack::PopAndDestroy(query);
       
   519 
       
   520     return ret;
       
   521     }
       
   522 
       
   523 // ----------------------------------------------------------------------------
       
   524 // CMPXDbTable::ExecuteIntQueryL
       
   525 // ----------------------------------------------------------------------------
       
   526 //
       
   527 EXPORT_C TUint32 CMPXDbTable::ExecuteIntQueryL(TInt aDriveID,const TDesC& aQuery)
       
   528     {
       
   529     MPX_FUNC("CMPXDbTable::ExecuteIntQueryL");
       
   530     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(aDriveID,aQuery));
       
   531     CleanupClosePushL(recordset);
       
   532 
       
   533     if (recordset.Next() != KSqlAtRow)
       
   534         {
       
   535         User::Leave(KErrNotFound);
       
   536         }
       
   537 
       
   538     TUint32 value(recordset.ColumnInt64(KMPXTableDefaultIndex));
       
   539     CleanupStack::PopAndDestroy(&recordset);
       
   540 
       
   541     return value;
       
   542     }
       
   543 
       
   544 // ----------------------------------------------------------------------------
       
   545 // CMPXDbTable::ExecuteSumQueryL
       
   546 // ----------------------------------------------------------------------------
       
   547 //
       
   548 EXPORT_C TInt CMPXDbTable::ExecuteSumQueryL(
       
   549     const TDesC& aQuery)
       
   550     {
       
   551     MPX_FUNC("CMPXDbTable::ExecuteSumQueryL");
       
   552 
       
   553     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(aQuery));
       
   554     CleanupClosePushL(recordset);
       
   555 
       
   556     TInt sum(0);
       
   557     TInt err(KErrNone);
       
   558     while ((err = recordset.Next()) == KSqlAtRow)
       
   559         {
       
   560         sum += recordset.ColumnInt64(KMPXTableDefaultIndex);
       
   561         }
       
   562 
       
   563     CleanupStack::PopAndDestroy(&recordset);
       
   564 
       
   565     if (err != KSqlAtEnd)
       
   566         {
       
   567         User::Leave(KErrCorrupt);
       
   568         }
       
   569 
       
   570     return sum;
       
   571     }
       
   572 
       
   573 // ----------------------------------------------------------------------------
       
   574 // CMPXDbTable::ExecuteSumQueryL
       
   575 // ----------------------------------------------------------------------------
       
   576 //
       
   577 EXPORT_C TInt CMPXDbTable::ExecuteSumQueryL(
       
   578     const TDesC& aQuery,
       
   579     TUint32 aValue)
       
   580     {
       
   581     MPX_FUNC("CMPXDbTable::ExecuteSumQueryL");
       
   582 
       
   583     HBufC* query = HBufC::NewLC(aQuery.Length() + KMCIntegerLen);
       
   584     query->Des().Format(aQuery, aValue);
       
   585     TUint32 ret(ExecuteSumQueryL(*query));
       
   586     CleanupStack::PopAndDestroy(query);
       
   587 
       
   588     return ret;
       
   589     }
       
   590 
       
   591 // ----------------------------------------------------------------------------
       
   592 // CMPXDbTable::ExecuteSumQueryL
       
   593 // ----------------------------------------------------------------------------
       
   594 //
       
   595 EXPORT_C TInt CMPXDbTable::ExecuteSumQueryL(
       
   596     const TDesC& aQuery,
       
   597     TUint32 aValue1,
       
   598     TUint32 aValue2)
       
   599     {
       
   600     MPX_FUNC("CMPXDbTable::ExecuteSumQueryL");
       
   601 
       
   602     HBufC* query = HBufC::NewLC(aQuery.Length() + 2 * KMCIntegerLen);
       
   603     query->Des().Format(aQuery, aValue1, aValue2);
       
   604     TUint32 ret(ExecuteSumQueryL(*query));
       
   605     CleanupStack::PopAndDestroy(query);
       
   606 
       
   607     return ret;
       
   608     }
       
   609 
       
   610 // ----------------------------------------------------------------------------
       
   611 // CMPXDbTable::ExecuteSumQueryL
       
   612 // ----------------------------------------------------------------------------
       
   613 //
       
   614 EXPORT_C TInt CMPXDbTable::ExecuteSumQueryL(
       
   615     const TDesC& aQuery,
       
   616     TUint32 aValue1,
       
   617     TUint32 aValue2,
       
   618     TUint32 aValue3)
       
   619     {
       
   620     MPX_FUNC("CMPXDbTable::ExecuteSumQueryL");
       
   621 
       
   622     HBufC* query = HBufC::NewLC(aQuery.Length() + 3 * KMCIntegerLen);
       
   623     query->Des().Format(aQuery, aValue1, aValue2, aValue3);
       
   624     TUint32 ret(ExecuteSumQueryL(*query));
       
   625     CleanupStack::PopAndDestroy(query);
       
   626 
       
   627     return ret;
       
   628     }
       
   629 
       
   630 // ----------------------------------------------------------------------------
       
   631 // CMPXDbTable::ExecuteSumQueryL
       
   632 // ----------------------------------------------------------------------------
       
   633 //
       
   634 EXPORT_C TUint32 CMPXDbTable::ExecuteSumQueryL(
       
   635     const TDesC& aQuery,
       
   636     const TDesC& aValue1,
       
   637     TUint32 aValue2)
       
   638     {
       
   639     MPX_FUNC("CMPXDbTable::ExecuteSumQueryL");
       
   640 
       
   641     // replace single quotes
       
   642     // reserve double the value length for single quote duplicates
       
   643     HBufC* value1 = HBufC::NewLC(aValue1.Length() * 2);
       
   644     TPtr value1Ptr(value1->Des());
       
   645     MPXDbCommonUtil::FindAndReplaceSingleQuote(aValue1, value1Ptr);
       
   646 
       
   647     // construct the query
       
   648     HBufC* query = HBufC::NewLC(aQuery.Length() + value1Ptr.Length() + KMCIntegerLen);
       
   649     query->Des().Format(aQuery, value1, aValue2);
       
   650 
       
   651     // execute the query
       
   652     TUint32 ret(ExecuteSumQueryL(*query));
       
   653 
       
   654     CleanupStack::PopAndDestroy(query);
       
   655     CleanupStack::PopAndDestroy(value1);
       
   656 
       
   657     return ret;
       
   658     }
       
   659 
       
   660 // ----------------------------------------------------------------------------
       
   661 // CMPXDbTable::ExecuteSumQueryL
       
   662 // ----------------------------------------------------------------------------
       
   663 //
       
   664 EXPORT_C TInt CMPXDbTable::ExecuteSumQueryL(
       
   665     const TDesC& aQuery,
       
   666     const TDesC& aValue1,
       
   667     const TDesC& aValue2)
       
   668     {
       
   669     MPX_FUNC("CMPXDbTable::ExecuteSumQueryL");
       
   670 
       
   671     // replace single quotes
       
   672     // reserve double the value length for single quote duplicates
       
   673     HBufC* value1 = HBufC::NewLC(aValue1.Length() * 2);
       
   674     TPtr value1Ptr(value1->Des());
       
   675     MPXDbCommonUtil::FindAndReplaceSingleQuote(aValue1, value1Ptr);
       
   676 
       
   677     HBufC* value2 = HBufC::NewLC(aValue2.Length() * 2);
       
   678     TPtr value2Ptr(value2->Des());
       
   679     MPXDbCommonUtil::FindAndReplaceSingleQuote(aValue2, value2Ptr);
       
   680 
       
   681     // construct the query
       
   682     HBufC* query = HBufC::NewLC(aQuery.Length() + value1Ptr.Length() + value2Ptr.Length());
       
   683     query->Des().Format(aQuery, value1, value2);
       
   684 
       
   685     // execute the query
       
   686     TUint32 ret(ExecuteSumQueryL(*query));
       
   687 
       
   688     CleanupStack::PopAndDestroy(query);
       
   689     CleanupStack::PopAndDestroy(value2);
       
   690     CleanupStack::PopAndDestroy(value1);
       
   691 
       
   692     return ret;
       
   693     }
       
   694 
       
   695 // ----------------------------------------------------------------------------
       
   696 // CMPXDbTable::ExecuteSumQueryL
       
   697 // ----------------------------------------------------------------------------
       
   698 //
       
   699 EXPORT_C TInt CMPXDbTable::ExecuteSumQueryL(
       
   700     const TDesC& aQuery,
       
   701     const TDesC& aValue)
       
   702     {
       
   703     MPX_FUNC("CMPXDbTable::ExecuteSumQueryL");
       
   704 
       
   705     // replace single quotes
       
   706     // reserve double the value length for single quote duplicates
       
   707     HBufC* value = HBufC::NewLC(aValue.Length() * 2);
       
   708     TPtr valuePtr(value->Des());
       
   709     MPXDbCommonUtil::FindAndReplaceSingleQuote(aValue, valuePtr);
       
   710 
       
   711     // construct the query
       
   712     HBufC* query = HBufC::NewLC(aQuery.Length() + valuePtr.Length());
       
   713     query->Des().Format(aQuery, value);
       
   714 
       
   715     // execute the query
       
   716     TUint32 ret(ExecuteSumQueryL(*query));
       
   717 
       
   718     CleanupStack::PopAndDestroy(query);
       
   719     CleanupStack::PopAndDestroy(value);
       
   720 
       
   721     return ret;
       
   722     }
       
   723 
       
   724 // ----------------------------------------------------------------------------
       
   725 // CMPXDbTable::ExecuteSumQueryL
       
   726 // ----------------------------------------------------------------------------
       
   727 //
       
   728 EXPORT_C TInt CMPXDbTable::ExecuteSumQueryL(
       
   729     const TDesC& aQuery,
       
   730     const TDesC& aValue1,
       
   731     TUint32 aValue2,
       
   732     const TDesC& aValue3,
       
   733     TUint32 aValue4)
       
   734     {
       
   735     MPX_FUNC("CMPXDbTable::ExecuteSumQueryL");
       
   736 
       
   737     // replace single quotes
       
   738     // reserve double the value length for single quote duplicates
       
   739     HBufC* value1 = HBufC::NewLC(aValue1.Length() * 2);
       
   740     TPtr value1Ptr(value1->Des());
       
   741     MPXDbCommonUtil::FindAndReplaceSingleQuote(aValue1, value1Ptr);
       
   742 
       
   743     // replace single quotes
       
   744     // reserve double the value length for single quote duplicates
       
   745     HBufC* value3 = HBufC::NewLC(aValue3.Length() * 2);
       
   746     TPtr value3Ptr(value3->Des());
       
   747     MPXDbCommonUtil::FindAndReplaceSingleQuote(aValue3, value3Ptr);
       
   748 
       
   749     // construct the query
       
   750     HBufC* query = HBufC::NewLC( aQuery.Length() +
       
   751                                  value1Ptr.Length() +
       
   752                                  2 * KMCIntegerLen +
       
   753                                  value3Ptr.Length() );
       
   754     query->Des().Format(aQuery, value1, aValue2, value3, aValue4);
       
   755 
       
   756     // execute the query
       
   757     TUint32 ret(ExecuteSumQueryL(*query));
       
   758 
       
   759     CleanupStack::PopAndDestroy(query);
       
   760     CleanupStack::PopAndDestroy(value3);
       
   761     CleanupStack::PopAndDestroy(value1);
       
   762 
       
   763     return ret;
       
   764     }
       
   765 
       
   766 // ----------------------------------------------------------------------------
       
   767 // CMPXDbTable::ExecuteMediaQueryL
       
   768 // ----------------------------------------------------------------------------
       
   769 //
       
   770 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL(
       
   771     const TArray<TMPXAttribute>& aAttrs,
       
   772     CMPXMediaArray& aMediaArray,
       
   773     const TDesC& aQuery,
       
   774     TInt aValue1,
       
   775     TInt aValue2,
       
   776     TUint aQueryId )
       
   777     {
       
   778     MPX_FUNC("CMPXDbTable::ExecuteMediaOffsetQueryL");
       
   779 
       
   780     RSqlStatement& recordset(iDbManager.ExecuteSelectQueryL(aQueryId,
       
   781                                                             aValue1,
       
   782                                                             aValue2,
       
   783                                                             aQuery));
       
   784     CleanupClosePushL(recordset);
       
   785 
       
   786     TInt err(KErrNone);
       
   787     while ((err = recordset.Next()) == KSqlAtRow)
       
   788         {
       
   789         CMPXMedia* media = CMPXMedia::NewL();
       
   790         CleanupStack::PushL(media);
       
   791 
       
   792         UpdateMediaL(recordset, aAttrs, *media);
       
   793 
       
   794         aMediaArray.AppendL(*media);
       
   795         CleanupStack::PopAndDestroy(media);
       
   796         }
       
   797     CleanupStack::Pop(&recordset);
       
   798 
       
   799     if (err!= KSqlAtEnd)
       
   800         {
       
   801         User::Leave(KErrCorrupt);
       
   802         }
       
   803     }
       
   804 
       
   805 // ----------------------------------------------------------------------------
       
   806 // CMPXDbTable::ExecuteMediaQueryL
       
   807 // ----------------------------------------------------------------------------
       
   808 //
       
   809 EXPORT_C void CMPXDbTable::ExecuteMediaQueryL( const TArray<TMPXAttribute>& aAttrs,
       
   810                                                CMPXMediaArray& aMediaArray,
       
   811                                                const TDesC& aQuery,
       
   812                                                const TDesC& aValue1,
       
   813                                                TInt aValue2,
       
   814                                                TBool aAsc,
       
   815                                                TUint aQueryId )
       
   816     {
       
   817     MPX_DEBUG1("CMPXDbTable::ExecuteMediaAscQueryL  <--");
       
   818 
       
   819     RSqlStatement& recordset(iDbManager.ExecuteSelectQueryL(aQueryId,
       
   820                                                             aValue1,
       
   821                                                             aValue2,
       
   822                                                             aQuery));
       
   823     CleanupClosePushL(recordset);
       
   824 
       
   825     TInt err(KErrNone);
       
   826     while ((err = recordset.Next()) == KSqlAtRow)
       
   827         {
       
   828         CMPXMedia* media = CMPXMedia::NewL();
       
   829         CleanupStack::PushL(media);
       
   830 
       
   831         UpdateMediaL(recordset, aAttrs, *media);
       
   832 
       
   833         if( aAsc )
       
   834             {
       
   835             aMediaArray.AppendL(*media);
       
   836             }
       
   837         else
       
   838             {
       
   839             aMediaArray.InsertL(*media, 0);
       
   840             }
       
   841         CleanupStack::PopAndDestroy(media);
       
   842         }
       
   843 
       
   844     CleanupStack::Pop(&recordset);
       
   845 
       
   846     if (err!= KSqlAtEnd)
       
   847         {
       
   848         User::Leave(KErrCorrupt);
       
   849         }
       
   850 
       
   851     MPX_DEBUG1("CMPXDbTable::ExecuteMediaAscQueryL  -->");
       
   852     }
       
   853 
       
   854 // ----------------------------------------------------------------------------
       
   855 // CMPXDbTable::DoCheckTable
       
   856 // ----------------------------------------------------------------------------
       
   857 //
       
   858 EXPORT_C TBool CMPXDbTable::DoCheckTable(
       
   859     RSqlDatabase& aDatabase,
       
   860     const TDesC& aQuery)
       
   861     {
       
   862     MPX_FUNC("CMPXDbTable::DoCheckTable");
       
   863 
       
   864     RSqlStatement statement;
       
   865     TInt err(statement.Prepare(aDatabase, aQuery));
       
   866     statement.Close();
       
   867 
       
   868     return (err == KErrNone);
       
   869     }
       
   870 
       
   871 
       
   872 // End of File