mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbhgplugin/src/mpxdbcategory.cpp
changeset 0 ff3acec5bc43
child 17 c8156a91d13c
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:  Responsible for interation with the category tables:
       
    15 *                Artist, Album, Genre and Composer
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <sqldb.h>
       
    22 
       
    23 #include <mpxlog.h>
       
    24 
       
    25 #include "mpxdbcommonutil.h"
       
    26 #include "mpxdbcommondef.h"
       
    27 #include "mpxdbmanager.h"
       
    28 
       
    29 #include "mpxcollectiondbdef.h"
       
    30 #include "mpxdbpluginqueries.h"
       
    31 #include "mpxdbutil.h"
       
    32 #include "mpxdbcategory.h"
       
    33 
       
    34 // CONSTANTS
       
    35 
       
    36 // maximum number of table name entries per query
       
    37 const TInt KMaxTableNameCount = 2;
       
    38 
       
    39 // ============================ MEMBER FUNCTIONS ==============================
       
    40 
       
    41 
       
    42 // ----------------------------------------------------------------------------
       
    43 // Destructor
       
    44 // ----------------------------------------------------------------------------
       
    45 //
       
    46 CMPXDbCategory::~CMPXDbCategory()
       
    47     {
       
    48     MPX_FUNC("CMPXDbCategory::~CMPXDbCategory");
       
    49     delete iTableName;
       
    50     }
       
    51 
       
    52 // ----------------------------------------------------------------------------
       
    53 // Constructor
       
    54 // ----------------------------------------------------------------------------
       
    55 //
       
    56 CMPXDbCategory::CMPXDbCategory(
       
    57     CMPXDbManager& aDbManager,
       
    58     TMPXGeneralCategory aCategory) :
       
    59     CMPXDbTable(aDbManager),
       
    60     iCategory(aCategory)
       
    61     {
       
    62     MPX_FUNC("CMPXDbCategory::CMPXDbCategory");
       
    63     }
       
    64 
       
    65 // ----------------------------------------------------------------------------
       
    66 // Second phase constructor.
       
    67 // ----------------------------------------------------------------------------
       
    68 //
       
    69 void CMPXDbCategory::BaseConstructL()
       
    70     {
       
    71     MPX_FUNC("CMPXDbCategory::BaseConstructL");
       
    72 
       
    73     CMPXDbTable::BaseConstructL();
       
    74     iTableName = MPXDbUtil::TableNameForCategoryL(iCategory).AllocL();
       
    75     }
       
    76 
       
    77 // ----------------------------------------------------------------------------
       
    78 // CMPXDbCategory::AddItemL
       
    79 // ----------------------------------------------------------------------------
       
    80 //
       
    81 TUint32 CMPXDbCategory::AddItemL(
       
    82     const TDesC& aName,
       
    83     TInt aDriveId,
       
    84     TBool& aNewRecord,
       
    85     TBool aCaseSensitive)
       
    86     {
       
    87     MPX_FUNC("CMPXDbCategory::AddItemL");
       
    88 
       
    89     // try to find the item first
       
    90     TUint32 rowId(MPXDbCommonUtil::GenerateUniqueIdL(iDbManager.Fs(), iCategory,
       
    91         aName, aCaseSensitive));
       
    92     aNewRecord = !CategoryItemExistsL(aDriveId, rowId);
       
    93 
       
    94     if (aNewRecord)
       
    95         {
       
    96         // insert new
       
    97         HBufC* query = PreProcessStringLC(KQueryCategoryInsert);
       
    98         HBufC* name = MPXDbCommonUtil::ProcessSingleQuotesLC(aName);
       
    99 
       
   100         iDbManager.ExecuteQueryL(aDriveId, *query, rowId, name, 1);
       
   101 
       
   102         CleanupStack::PopAndDestroy(name);
       
   103         CleanupStack::PopAndDestroy(query);
       
   104         }
       
   105     else
       
   106         {
       
   107         // increment the number of songs for the category
       
   108         HBufC* query = PreProcessStringLC(KQueryCategoryIncrementSongCount);
       
   109         iDbManager.ExecuteQueryL(aDriveId, *query, rowId);
       
   110         CleanupStack::PopAndDestroy(query);
       
   111         }
       
   112 
       
   113     return rowId;
       
   114     }
       
   115 
       
   116 // ----------------------------------------------------------------------------
       
   117 // CMPXDbCategory::GetNameL
       
   118 // ----------------------------------------------------------------------------
       
   119 //
       
   120 HBufC* CMPXDbCategory::GetNameL(
       
   121     TUint32 aId)
       
   122     {
       
   123     MPX_FUNC("CMPXDbCategory::GetNameL");
       
   124 
       
   125     RSqlStatement recordset(GetCategoryRecordL(aId));
       
   126     CleanupClosePushL(recordset);
       
   127 
       
   128     if (recordset.Next() != KSqlAtRow)
       
   129         {
       
   130         User::LeaveIfError(KErrNotFound);
       
   131         }
       
   132 
       
   133     HBufC* name = MPXDbCommonUtil::GetColumnTextL(recordset, ECategoryName).AllocL();
       
   134     CleanupStack::PopAndDestroy(&recordset);
       
   135 
       
   136     return name;
       
   137     }
       
   138 
       
   139 // ----------------------------------------------------------------------------
       
   140 // CMPXDbCategory::CountL
       
   141 // ----------------------------------------------------------------------------
       
   142 //
       
   143 TInt CMPXDbCategory::CountL()
       
   144     {
       
   145     MPX_FUNC("CMPXDbCategory::CountL");
       
   146 
       
   147     HBufC* query = PreProcessStringLC(KQueryCategoryCount);
       
   148     TInt count(ExecuteSumQueryL(*query));
       
   149     CleanupStack::PopAndDestroy(query);
       
   150 
       
   151     return count;
       
   152     }
       
   153 
       
   154 // ----------------------------------------------------------------------------
       
   155 // CMPXDbCategory::FindAllL
       
   156 // ----------------------------------------------------------------------------
       
   157 //
       
   158 void CMPXDbCategory::FindAllL(
       
   159     const CMPXMedia& aCriteria,
       
   160     const TArray<TMPXAttribute>& aAttrs,
       
   161     CMPXMediaArray& aMediaArray)
       
   162     {
       
   163     MPX_FUNC("CMPXDbCategory::FindAllL");
       
   164 
       
   165     TMPXGeneralType type = aCriteria.ValueTObjectL<TMPXGeneralType>(KMPXMediaGeneralType);
       
   166 
       
   167     const TArray<TMPXAttribute> criteria = aCriteria.Attributes();
       
   168     TInt criteriaCount(criteria.Count());
       
   169 
       
   170     // process the criteria and construct the criteria string
       
   171     CDesCArrayFlat* criteriaArray = new (ELeave) CDesCArrayFlat(criteriaCount + 1);
       
   172     CleanupStack::PushL(criteriaArray);
       
   173 
       
   174     for (TInt i = 0; i < criteriaCount; ++i)
       
   175         {
       
   176         const TMPXAttribute& criterion = criteria[i];
       
   177         if ((type == EMPXItem) && (criterion == KMPXMediaGeneralId))
       
   178             {
       
   179             TUint32 itemId = (aCriteria.ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId)).iId2;
       
   180             if (MPX_ITEM_CATEGORY(itemId) != iCategory)
       
   181                 {
       
   182                 User::Leave(KErrNotSupported);
       
   183                 }
       
   184 
       
   185             HBufC* critStr = PreProcessStringLC(KCriterionCategoryUniqueId);
       
   186             MPXDbCommonUtil::AddSqlCriterionL(*criteriaArray, *critStr, itemId);
       
   187             CleanupStack::PopAndDestroy(critStr);
       
   188             }
       
   189         else if (criterion == KMPXMediaGeneralTitle)
       
   190             {
       
   191 #ifdef RD_MPX_COLLECTION_CACHE
       
   192 
       
   193             if (aCriteria.ValueText(KMPXMediaGeneralTitle).Length() <= 0)
       
   194                 {
       
   195                 TUint32 itemId = MPXDbCommonUtil::GenerateUniqueIdL(
       
   196                                     iDbManager.Fs(), iCategory, KNullDesC, EFalse);
       
   197                 HBufC* critStr = PreProcessStringLC(KCriterionCategoryUniqueId);
       
   198                 MPXDbCommonUtil::AddSqlCriterionL(*criteriaArray, *critStr, itemId);
       
   199                 CleanupStack::PopAndDestroy(critStr);
       
   200                 }
       
   201             else
       
   202                 {
       
   203 
       
   204 #endif //RD_MPX_COLLECTION_CACHE
       
   205                 HBufC* critStr = PreProcessStringLC(KCriterionCategoryName);
       
   206                 HBufC* title = MPXDbCommonUtil::ProcessPatternCharsLC(
       
   207                     aCriteria.ValueText(KMPXMediaGeneralTitle));
       
   208                 MPXDbCommonUtil::AddSqlCriterionL(*criteriaArray, *critStr, *title);
       
   209                 CleanupStack::PopAndDestroy(2, critStr); // title & critStr
       
   210 #ifdef RD_MPX_COLLECTION_CACHE
       
   211                 }
       
   212 #endif //RD_MPX_COLLECTION_CACHE
       
   213             }
       
   214         else
       
   215             {
       
   216             // ignore attribute
       
   217             }
       
   218         }
       
   219 
       
   220     // construct criteria string
       
   221     HBufC* criteriaStr = MPXDbCommonUtil::StringFromArrayLC(*criteriaArray, KMCAndKeyword);
       
   222 
       
   223     // either get all items or items filtered based on criteria
       
   224     HBufC* query = PreProcessStringLC(criteriaStr->Length() ?
       
   225         KQueryCategoryItems() : KQueryCategoryAll());
       
   226     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query, criteriaStr));
       
   227     CleanupStack::PopAndDestroy(3, criteriaArray); // query, criteriaStr, criteriaArray
       
   228     CleanupClosePushL(recordset);
       
   229 
       
   230     // process the results
       
   231     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
       
   232     CleanupStack::PopAndDestroy(&recordset);
       
   233     }
       
   234 
       
   235 // ----------------------------------------------------------------------------
       
   236 // CMPXDbCategory::DecrementSongsForCategoryL
       
   237 // ----------------------------------------------------------------------------
       
   238 //
       
   239 void CMPXDbCategory::DecrementSongsForCategoryL(
       
   240     const TUint32 aId,
       
   241     TInt aDriveId,
       
   242     CMPXMessageArray* aItemChangedMessages,
       
   243     TBool& aItemExist)
       
   244     {
       
   245     MPX_FUNC("CMPXDbCategory::DecrementSongsForCategoryL");
       
   246 
       
   247     // if just one song uses this category. Use <= just in case
       
   248     if (GetSongsCountL(aDriveId, aId) <= 1)
       
   249         {
       
   250         aItemExist = EFalse;
       
   251         // delete the category
       
   252         DeleteCategoryL(aId, aDriveId);
       
   253 
       
   254         if (aItemChangedMessages)
       
   255             {
       
   256             // add the item changed message
       
   257             MPXDbCommonUtil::AddItemChangedMessageL(*aItemChangedMessages, aId, EMPXItemDeleted,
       
   258                 iCategory, KDBPluginUid);
       
   259             }
       
   260         }
       
   261     else
       
   262         {
       
   263         aItemExist = ETrue;
       
   264         // decrement the number of songs for the category
       
   265         HBufC* query = PreProcessStringLC(KQueryCategoryDecrementSongCount);
       
   266         iDbManager.ExecuteQueryL(aDriveId, *query, aId);
       
   267         CleanupStack::PopAndDestroy(query);
       
   268         }
       
   269     }
       
   270 
       
   271 // ----------------------------------------------------------------------------
       
   272 // CMPXDbCategory::DeleteCategoryL
       
   273 // ----------------------------------------------------------------------------
       
   274 //
       
   275 void CMPXDbCategory::DeleteCategoryL(
       
   276     TUint32 aId,
       
   277     TInt aDriveId)
       
   278     {
       
   279     MPX_FUNC("CMPXDbCategory::DeleteCategoryL");
       
   280 
       
   281     HBufC* query = PreProcessStringLC(KQueryCategoryDelete);
       
   282     iDbManager.ExecuteQueryL(aDriveId, *query, aId);
       
   283     CleanupStack::PopAndDestroy(query);
       
   284     }
       
   285 
       
   286 // ----------------------------------------------------------------------------
       
   287 // CMPXDbCategory::GetCategoryItemsL
       
   288 // ----------------------------------------------------------------------------
       
   289 //
       
   290 void CMPXDbCategory::GetCategoryItemsL(
       
   291     const TArray<TMPXAttribute>& aAttrs,
       
   292     CMPXMediaArray& aMediaArray)
       
   293     {
       
   294     MPX_FUNC("CMPXDbCategory::GetCategoryItemsL");
       
   295 
       
   296     // have to run one query to get all items as opposed to individual queries
       
   297     // because of the sorting
       
   298 
       
   299     // construct the unique ID criteria string
       
   300     // (UniqueId = %u OR UniqueId = %u ...)
       
   301     TInt count(aMediaArray.Count());
       
   302     HBufC* criteria = HBufC::NewLC((2 * KCriterionCategoryUniqueId().Length() +
       
   303         KMCIntegerLen + KMCOrKeyword().Length() + 2) * count);
       
   304     TPtr ptr(criteria->Des());
       
   305     ptr.Append(KMCOpenBracket);
       
   306     for (TInt index = 0; index < count; ++index)
       
   307         {
       
   308         CMPXMedia* media = aMediaArray[index];
       
   309 
       
   310         HBufC* critStr = PreProcessStringLC(KCriterionCategoryUniqueId);
       
   311         HBufC* criterion = MPXDbCommonUtil::SqlCriterionLC(*critStr,
       
   312             (media->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId)).iId1);
       
   313         ptr.Append(*criterion);
       
   314         CleanupStack::PopAndDestroy(criterion);
       
   315         CleanupStack::PopAndDestroy(critStr);
       
   316 
       
   317         if (index < (count - 1))
       
   318             {
       
   319             ptr.Append(KMCOrKeyword);
       
   320             }
       
   321         }
       
   322     ptr.Append(KMCCloseBracket);
       
   323 
       
   324     // the array has to be reset as the items have to be returned in a different sort order
       
   325     aMediaArray.Reset();
       
   326 
       
   327     HBufC* query = PreProcessStringLC(KQueryCategoryItems);
       
   328     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query, criteria));
       
   329 
       
   330     CleanupStack::PopAndDestroy(query);
       
   331     CleanupStack::PopAndDestroy(criteria);
       
   332 
       
   333     CleanupClosePushL(recordset);
       
   334     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
       
   335     CleanupStack::PopAndDestroy(&recordset);
       
   336     }
       
   337 
       
   338 // ----------------------------------------------------------------------------
       
   339 // CMPXDbCategory::GetAllCategoryItemsL
       
   340 // ----------------------------------------------------------------------------
       
   341 //
       
   342 void CMPXDbCategory::GetAllCategoryItemsL(
       
   343     const TArray<TMPXAttribute>& aAttrs,
       
   344     CMPXMediaArray& aMediaArray)
       
   345     {
       
   346     MPX_FUNC("CMPXDbCategory::GetAllCategoryItemsL");
       
   347 
       
   348     HBufC* query = PreProcessStringLC(KQueryCategoryAll);
       
   349     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query));
       
   350     CleanupStack::PopAndDestroy(query);
       
   351 
       
   352     CleanupClosePushL(recordset);
       
   353     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
       
   354     CleanupStack::PopAndDestroy(&recordset);
       
   355     }
       
   356 
       
   357 // ----------------------------------------------------------------------------
       
   358 // CMPXDbCategory::GetCategoryItemL
       
   359 // ----------------------------------------------------------------------------
       
   360 //
       
   361 void CMPXDbCategory::GetCategoryItemL(
       
   362     TUint32 aId,
       
   363     const TArray<TMPXAttribute>& aAttrs,
       
   364     CMPXMedia& aMedia)
       
   365     {
       
   366     MPX_FUNC("CMPXDbCategory::GetCategoryItemL");
       
   367 
       
   368     HBufC* query = PreProcessStringLC(KQueryCategoryItem);
       
   369     ExecuteMediaQueryL(aAttrs, aMedia, *query, aId);
       
   370     CleanupStack::PopAndDestroy(query);
       
   371     }
       
   372 
       
   373 // ----------------------------------------------------------------------------
       
   374 // CMPXDbCategory::GetSubCategoryItemsL
       
   375 // ----------------------------------------------------------------------------
       
   376 //
       
   377 void CMPXDbCategory::GetSubCategoryItemsL(
       
   378     TMPXGeneralCategory aParentCategory,
       
   379     TUint32 aParentId,
       
   380     const TArray<TMPXAttribute>& aAttrs,
       
   381     CMPXMediaArray& aMediaArray)
       
   382     {
       
   383     MPX_FUNC("CMPXDbCategory::GetSubCategoryItemsL");
       
   384 
       
   385     // this is only valid for albums belonging to an artist
       
   386     ASSERT((iCategory == EMPXAlbum) && (aParentCategory == EMPXArtist));
       
   387 
       
   388     // to handle the UREL warning
       
   389     (void)aParentCategory;
       
   390 
       
   391     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(KQueryCategorySubcategoryItems, aParentId));
       
   392     CleanupClosePushL(recordset);
       
   393     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
       
   394     CleanupStack::PopAndDestroy(&recordset);
       
   395     }
       
   396 
       
   397 // ----------------------------------------------------------------------------
       
   398 // CMPXDbCategory::CategoryItemExistsL
       
   399 // The category records must be in the same database as the corresponding
       
   400 // Music record, otherwise when adding a duplicate of a song on a
       
   401 // different drive this method will return true and the category record won't
       
   402 // be created.
       
   403 // ----------------------------------------------------------------------------
       
   404 //
       
   405 TBool CMPXDbCategory::CategoryItemExistsL(
       
   406     TInt aDriveId,
       
   407     TUint32 aId)
       
   408     {
       
   409     MPX_FUNC("CMPXDbCategory::CategoryItemExistsL");
       
   410 
       
   411     HBufC* query = PreProcessStringLC(KQueryCategoryItem);
       
   412     RSqlStatement recordset(
       
   413         iDbManager.ExecuteSelectQueryL(aDriveId, *query, aId));
       
   414 
       
   415     TBool exists(recordset.Next() == KSqlAtRow);
       
   416 
       
   417     recordset.Close();
       
   418     CleanupStack::PopAndDestroy(query);
       
   419 
       
   420     return exists;
       
   421     }
       
   422 
       
   423 // ----------------------------------------------------------------------------
       
   424 // CMPXDbCategory::GetSongsCountL
       
   425 // ----------------------------------------------------------------------------
       
   426 //
       
   427 TInt CMPXDbCategory::GetSongsCountL(
       
   428     TInt aDriveId,
       
   429     TUint32 aId)
       
   430     {
       
   431     MPX_FUNC("CMPXDbCategory::GetSongsCountL");
       
   432 
       
   433     HBufC* query = PreProcessStringLC(KQueryCategoryGetSongCount);
       
   434     RSqlStatement recordset(
       
   435         iDbManager.ExecuteSelectQueryL(aDriveId, *query, aId));
       
   436     CleanupClosePushL(recordset);
       
   437 
       
   438     TInt err(KErrNone);
       
   439     TInt ret(0);
       
   440     while ((err = recordset.Next()) == KSqlAtRow)
       
   441         {
       
   442         ret += recordset.ColumnInt(KMPXTableDefaultIndex);
       
   443         }
       
   444 
       
   445     if (err != KSqlAtEnd)
       
   446         {
       
   447         User::Leave(err);
       
   448         }
       
   449 
       
   450     CleanupStack::PopAndDestroy(&recordset);
       
   451     CleanupStack::PopAndDestroy(query);
       
   452 
       
   453     return ret;
       
   454     }
       
   455 
       
   456 void CMPXDbCategory::UpdateItemL(
       
   457     TUint32 /*aId*/,
       
   458     const CMPXMedia& /*aMedia*/,
       
   459     TInt /*aDriveId*/,
       
   460     CMPXMessageArray* /*aItemChangedMessages*/)
       
   461 	{
       
   462 	// nothing
       
   463 	}
       
   464 
       
   465 // ----------------------------------------------------------------------------
       
   466 // CMPXDbCategory::UpdateMediaL
       
   467 // ----------------------------------------------------------------------------
       
   468 //
       
   469 void CMPXDbCategory::UpdateMediaL(
       
   470     RSqlStatement& aRecord,
       
   471     const TArray<TMPXAttribute>& aAttrs,
       
   472     CMPXMedia& aMedia)
       
   473     {
       
   474     MPX_FUNC("CMPXDbCategory::UpdateMediaL");
       
   475 
       
   476     TInt count(aAttrs.Count());
       
   477     for (TInt i = 0; i < count; ++i)
       
   478         {
       
   479         TInt contentId(aAttrs[i].ContentId());
       
   480         TUint attributeId(aAttrs[i].AttributeId());
       
   481 
       
   482         if (contentId == KMPXMediaIdGeneral)
       
   483             {
       
   484             if (attributeId & EMPXMediaGeneralId)
       
   485                 {
       
   486                 aMedia.SetTObjectValueL<TMPXItemId>(KMPXMediaGeneralId,
       
   487                     aRecord.ColumnInt64(ECategoryUniqueId));
       
   488                 }
       
   489             if (attributeId & EMPXMediaGeneralTitle)
       
   490                 {
       
   491                 aMedia.SetTextValueL(KMPXMediaGeneralTitle,
       
   492                     MPXDbCommonUtil::GetColumnTextL(aRecord, ECategoryName));
       
   493                 }
       
   494             if (attributeId & EMPXMediaGeneralCount)
       
   495                 {
       
   496                 aMedia.SetTObjectValueL<TInt>(KMPXMediaGeneralCount,
       
   497                     GetSongsCountL(KDbManagerAllDrives,
       
   498                     aRecord.ColumnInt64(ECategoryUniqueId)));
       
   499                 }
       
   500             } // end if contentId == KMPXMediaIdGeneral
       
   501         } // end for
       
   502 
       
   503     aMedia.SetTObjectValueL<TMPXGeneralType>(KMPXMediaGeneralType, EMPXItem);
       
   504     aMedia.SetTObjectValueL<TMPXGeneralCategory>(KMPXMediaGeneralCategory, iCategory);
       
   505     }
       
   506 
       
   507 // ----------------------------------------------------------------------------
       
   508 // CMPXDbCategory::GetCategoryRecordL
       
   509 // ----------------------------------------------------------------------------
       
   510 //
       
   511 RSqlStatement CMPXDbCategory::GetCategoryRecordL(
       
   512     TUint32 aId)
       
   513     {
       
   514     MPX_FUNC("CMPXDbCategory::GetCategoryRecordL");
       
   515     HBufC* query = PreProcessStringLC(KQueryCategoryItem);
       
   516     RSqlStatement statement(iDbManager.ExecuteSelectQueryL(*query, aId));
       
   517     CleanupStack::PopAndDestroy(query);
       
   518 
       
   519     return statement;
       
   520     }
       
   521 
       
   522 // ----------------------------------------------------------------------------
       
   523 // CMPXDbCategory::PreProcessStringLC
       
   524 // ----------------------------------------------------------------------------
       
   525 //
       
   526 HBufC* CMPXDbCategory::PreProcessStringLC(
       
   527     const TDesC& aQuery)
       
   528     {
       
   529     MPX_FUNC("CMPXDbCategory::PreProcessStringLC");
       
   530 
       
   531     HBufC* query = HBufC::NewLC(aQuery.Length() + KMaxTableNameCount * (iTableName->Length() +
       
   532         KCategoryTablePlaceholder().Length()));
       
   533     TPtr queryPtr(query->Des());
       
   534 
       
   535     // copy the query string
       
   536     queryPtr = aQuery;
       
   537 
       
   538     // replace all instances of the placeholder with the actual table name
       
   539     TInt index(0);
       
   540     while ((index = queryPtr.Find(KCategoryTablePlaceholder)) != KErrNotFound)
       
   541         {
       
   542         queryPtr.Replace(index, KCategoryTablePlaceholder().Length(), *iTableName);
       
   543         }
       
   544 
       
   545     return query;
       
   546     }
       
   547 
       
   548 // ----------------------------------------------------------------------------
       
   549 // CMPXDbCategory::ProcessRecordsetL
       
   550 // Unknown item is stored in the database as NULL (name field). This ensures the
       
   551 // unknown item to be the 1st found record if it exists. This will save time in
       
   552 // searching for the unknown record among the results and avoid performing
       
   553 // descriptor comparison. If the 1st record is the unknown item, it won't be
       
   554 // appended to the array until all other records have been put in the array.
       
   555 //
       
   556 // NOTE: putting unknown item to the end of the array only takes place when title
       
   557 //       field is requested. normal sorting algorithm occurs if title isn't
       
   558 //       requested.
       
   559 // ----------------------------------------------------------------------------
       
   560 //
       
   561 void CMPXDbCategory::ProcessRecordsetL(
       
   562     const TArray<TMPXAttribute>& aAttrs,
       
   563     RSqlStatement& aRecordset,
       
   564     CMPXMediaArray& aMediaArray)
       
   565     {
       
   566     // populate the array
       
   567     TBool firstRecord(ETrue);
       
   568     CMPXMedia* unknownMedia(NULL);
       
   569     TInt prevId(0);
       
   570     TInt err(KErrNone);
       
   571 
       
   572     TInt pPath(0);
       
   573     if (aMediaArray.Count())
       
   574         {
       
   575         CMPXMedia* pMedia = aMediaArray[0];
       
   576         if (pMedia->IsSupported(KMPXMediaGeneralValue))
       
   577             { // Query excuted by OpenL
       
   578             pPath = pMedia->ValueTObjectL<TInt>(KMPXMediaGeneralValue);
       
   579             MPX_ASSERT(pPath);
       
   580             }
       
   581         }
       
   582     RArray<TMPXItemId> ids;
       
   583     CleanupClosePushL(ids);
       
   584 
       
   585     while ((err = aRecordset.Next()) == KSqlAtRow)
       
   586         {
       
   587         TUint32 rowId(aRecordset.ColumnInt64(ECategoryUniqueId));
       
   588         if (prevId == rowId)
       
   589             {
       
   590             continue;
       
   591             }
       
   592 
       
   593         prevId = rowId;
       
   594         CMPXMedia* media = CMPXMedia::NewL();
       
   595         CleanupStack::PushL(media);
       
   596 
       
   597         UpdateMediaL(aRecordset, aAttrs, *media);
       
   598 
       
   599         if (firstRecord &&
       
   600             (MPXDbCommonUtil::GetColumnTextL(aRecordset, ECategoryName).Length() == 0))
       
   601             {
       
   602             unknownMedia = media;
       
   603             }
       
   604 
       
   605         if (!firstRecord || !unknownMedia)
       
   606             {
       
   607             if (media->IsSupported(KMPXMediaGeneralId) && pPath)
       
   608                 {
       
   609                 ids.AppendL(media->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   610                 }
       
   611             aMediaArray.AppendL(*media);
       
   612             CleanupStack::PopAndDestroy(media);
       
   613             }
       
   614 
       
   615         firstRecord = EFalse;
       
   616         } // end while
       
   617 
       
   618     if (err != KSqlAtEnd)
       
   619         {
       
   620         User::LeaveIfError(err);
       
   621         }
       
   622 
       
   623     if (unknownMedia)
       
   624         {
       
   625         if (unknownMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   626             {
       
   627             ids.AppendL(unknownMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   628             }
       
   629         aMediaArray.AppendL(*unknownMedia);
       
   630         CleanupStack::PopAndDestroy(unknownMedia);
       
   631         }
       
   632 
       
   633     // Append ids to the returned path
       
   634     if (pPath)
       
   635         {
       
   636         ((CMPXCollectionPath*)pPath)->AppendL(ids.Array());
       
   637         }
       
   638     CleanupStack::PopAndDestroy(&ids);
       
   639     }
       
   640 
       
   641 // ----------------------------------------------------------------------------
       
   642 // CMPXDbCategory::CreateTableL
       
   643 // ----------------------------------------------------------------------------
       
   644 //
       
   645 void CMPXDbCategory::CreateTableL(
       
   646     RSqlDatabase& aDatabase,
       
   647     TBool /* aCorruptTable */)
       
   648     {
       
   649     MPX_FUNC("CMPXDbCategory::CreateTableL");
       
   650 
       
   651     // create the table
       
   652     HBufC* query = PreProcessStringLC(KCategoryCreateTable);
       
   653     User::LeaveIfError(aDatabase.Exec(*query));
       
   654     CleanupStack::PopAndDestroy(query);
       
   655 
       
   656     // do not create an index on the Name field
       
   657     // as it only slows down the insert/update queries overall
       
   658     }
       
   659 
       
   660 // ----------------------------------------------------------------------------
       
   661 // CMPXDbCategory::DropTableL
       
   662 // ----------------------------------------------------------------------------
       
   663 //
       
   664 void CMPXDbCategory::DropTableL(
       
   665     RSqlDatabase& aDatabase)
       
   666     {
       
   667     MPX_FUNC("CMPXDbCategory::DropTableL");
       
   668 
       
   669     HBufC* query = PreProcessStringLC(KCategoryDropTable);
       
   670     User::LeaveIfError(aDatabase.Exec(*query));
       
   671     CleanupStack::PopAndDestroy(query);
       
   672     }
       
   673 
       
   674 // ----------------------------------------------------------------------------
       
   675 // CMPXDbCategory::CheckTableL
       
   676 // ----------------------------------------------------------------------------
       
   677 //
       
   678 TBool CMPXDbCategory::CheckTableL(
       
   679     RSqlDatabase& aDatabase)
       
   680     {
       
   681     MPX_FUNC("CMPXDbCategory::CheckTableL");
       
   682 
       
   683     HBufC* query = PreProcessStringLC(KCategoryCheckTable);
       
   684     TBool check(DoCheckTable(aDatabase, *query));
       
   685     CleanupStack::PopAndDestroy(query);
       
   686 
       
   687     return check;
       
   688     }
       
   689 
       
   690 // End of File