mpserviceplugins/mpxsqlitedbhgplugin/src/mpxdbcategory.cpp
changeset 22 ecf06a08d4d9
child 25 3ec52facab4d
equal deleted inserted replaced
20:82baf59ce8dd 22:ecf06a08d4d9
       
     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 #ifdef ABSTRACTAUDIOALBUM_INCLUDED
       
   215           else if (criterion == KMPXMediaGeneralUri)
       
   216                  {  
       
   217                  TUint32 itemId(MPXDbCommonUtil::GenerateUniqueIdL(iDbManager.Fs(), iCategory,
       
   218                        aCriteria.ValueText(KMPXMediaGeneralUri), (iCategory != EMPXGenre)));   
       
   219                    HBufC* critStr = PreProcessStringLC(KCriterionCategoryUniqueId);
       
   220                    MPXDbCommonUtil::AddSqlCriterionL(*criteriaArray, *critStr, itemId);
       
   221              CleanupStack::PopAndDestroy(critStr);
       
   222              }
       
   223         else if (criterion == KMPXMediaGeneralDrive)
       
   224             {
       
   225             const TDesC& drive(aCriteria.ValueText(KMPXMediaGeneralDrive));
       
   226             TDriveUnit driveUnit(drive);
       
   227             MPXDbCommonUtil::AddSqlCriterionL(*criteriaArray, KCriterionAbstractAlbumVolumeId,
       
   228                 MPXDbCommonUtil::GetVolIdMatchDriveIdL(iDbManager.Fs(), driveUnit));
       
   229             }
       
   230 #endif // ABSTRACTAUDIOALBUM_INCLUDED
       
   231         else
       
   232             {
       
   233             // ignore attribute
       
   234             }
       
   235         }
       
   236 
       
   237     // construct criteria string
       
   238     HBufC* criteriaStr = MPXDbCommonUtil::StringFromArrayLC(*criteriaArray, KMCAndKeyword);
       
   239 
       
   240     // either get all items or items filtered based on criteria
       
   241     HBufC* query = PreProcessStringLC(criteriaStr->Length() ?
       
   242         KQueryCategoryItems() : KQueryCategoryAll());
       
   243     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query, criteriaStr));
       
   244     CleanupStack::PopAndDestroy(3, criteriaArray); // query, criteriaStr, criteriaArray
       
   245     CleanupClosePushL(recordset);
       
   246 
       
   247     // process the results
       
   248     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
       
   249     CleanupStack::PopAndDestroy(&recordset);
       
   250     }
       
   251 
       
   252 // ----------------------------------------------------------------------------
       
   253 // CMPXDbCategory::DecrementSongsForCategoryL
       
   254 // ----------------------------------------------------------------------------
       
   255 //
       
   256 void CMPXDbCategory::DecrementSongsForCategoryL(
       
   257     const TUint32 aId,
       
   258     TInt aDriveId,
       
   259     CMPXMessageArray* aItemChangedMessages,
       
   260     TBool& aItemExist,
       
   261     TBool /*aMTPInUse*/)
       
   262     {
       
   263     MPX_FUNC("CMPXDbCategory::DecrementSongsForCategoryL");
       
   264 
       
   265     // if just one song uses this category. Use <= just in case
       
   266     if (GetSongsCountL(aDriveId, aId) <= 1)
       
   267         {
       
   268         aItemExist = EFalse;
       
   269         // delete the category
       
   270         DeleteCategoryL(aId, aDriveId);
       
   271 
       
   272         if (aItemChangedMessages)
       
   273             {
       
   274             // add the item changed message
       
   275             MPXDbCommonUtil::AddItemChangedMessageL(*aItemChangedMessages, aId, EMPXItemDeleted,
       
   276                 iCategory, KDBPluginUid);
       
   277             }
       
   278         }
       
   279     else
       
   280         {
       
   281         aItemExist = ETrue;
       
   282         // decrement the number of songs for the category
       
   283         HBufC* query = PreProcessStringLC(KQueryCategoryDecrementSongCount);
       
   284         iDbManager.ExecuteQueryL(aDriveId, *query, aId);
       
   285         CleanupStack::PopAndDestroy(query);
       
   286         }
       
   287     }
       
   288 
       
   289 // ----------------------------------------------------------------------------
       
   290 // CMPXDbCategory::DeleteCategoryL
       
   291 // ----------------------------------------------------------------------------
       
   292 //
       
   293 void CMPXDbCategory::DeleteCategoryL(
       
   294     TUint32 aId,
       
   295     TInt aDriveId)
       
   296     {
       
   297     MPX_FUNC("CMPXDbCategory::DeleteCategoryL");
       
   298 
       
   299     HBufC* query = PreProcessStringLC(KQueryCategoryDelete);
       
   300     iDbManager.ExecuteQueryL(aDriveId, *query, aId);
       
   301     CleanupStack::PopAndDestroy(query);
       
   302     }
       
   303 
       
   304 // ----------------------------------------------------------------------------
       
   305 // CMPXDbCategory::GetCategoryItemsL
       
   306 // ----------------------------------------------------------------------------
       
   307 //
       
   308 void CMPXDbCategory::GetCategoryItemsL(
       
   309     const TArray<TMPXAttribute>& aAttrs,
       
   310     CMPXMediaArray& aMediaArray)
       
   311     {
       
   312     MPX_FUNC("CMPXDbCategory::GetCategoryItemsL");
       
   313 
       
   314     // have to run one query to get all items as opposed to individual queries
       
   315     // because of the sorting
       
   316 
       
   317     // construct the unique ID criteria string
       
   318     // (UniqueId = %u OR UniqueId = %u ...)
       
   319     TInt count(aMediaArray.Count());
       
   320     HBufC* criteria = HBufC::NewLC((2 * KCriterionCategoryUniqueId().Length() +
       
   321         KMCIntegerLen + KMCOrKeyword().Length() + 2) * count);
       
   322     TPtr ptr(criteria->Des());
       
   323     ptr.Append(KMCOpenBracket);
       
   324     for (TInt index = 0; index < count; ++index)
       
   325         {
       
   326         CMPXMedia* media = aMediaArray[index];
       
   327 
       
   328         HBufC* critStr = PreProcessStringLC(KCriterionCategoryUniqueId);
       
   329         HBufC* criterion = MPXDbCommonUtil::SqlCriterionLC(*critStr,
       
   330             (media->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId)).iId1);
       
   331         ptr.Append(*criterion);
       
   332         CleanupStack::PopAndDestroy(criterion);
       
   333         CleanupStack::PopAndDestroy(critStr);
       
   334 
       
   335         if (index < (count - 1))
       
   336             {
       
   337             ptr.Append(KMCOrKeyword);
       
   338             }
       
   339         }
       
   340     ptr.Append(KMCCloseBracket);
       
   341 
       
   342     // the array has to be reset as the items have to be returned in a different sort order
       
   343     aMediaArray.Reset();
       
   344 
       
   345     HBufC* query = PreProcessStringLC(KQueryCategoryItems);
       
   346     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query, criteria));
       
   347 
       
   348     CleanupStack::PopAndDestroy(query);
       
   349     CleanupStack::PopAndDestroy(criteria);
       
   350 
       
   351     CleanupClosePushL(recordset);
       
   352     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
       
   353     CleanupStack::PopAndDestroy(&recordset);
       
   354     }
       
   355 
       
   356 // ----------------------------------------------------------------------------
       
   357 // CMPXDbCategory::GetAllCategoryItemsL
       
   358 // ----------------------------------------------------------------------------
       
   359 //
       
   360 void CMPXDbCategory::GetAllCategoryItemsL(
       
   361     const TArray<TMPXAttribute>& aAttrs,
       
   362     CMPXMediaArray& aMediaArray)
       
   363     {
       
   364     MPX_FUNC("CMPXDbCategory::GetAllCategoryItemsL");
       
   365 
       
   366     HBufC* query = PreProcessStringLC(KQueryCategoryAll);
       
   367     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query));
       
   368     CleanupStack::PopAndDestroy(query);
       
   369 
       
   370     CleanupClosePushL(recordset);
       
   371     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
       
   372     CleanupStack::PopAndDestroy(&recordset);
       
   373     }
       
   374 
       
   375 // ----------------------------------------------------------------------------
       
   376 // CMPXDbCategory::GetCategoryItemL
       
   377 // ----------------------------------------------------------------------------
       
   378 //
       
   379 void CMPXDbCategory::GetCategoryItemL(
       
   380     TUint32 aId,
       
   381     const TArray<TMPXAttribute>& aAttrs,
       
   382     CMPXMedia& aMedia)
       
   383     {
       
   384     MPX_FUNC("CMPXDbCategory::GetCategoryItemL");
       
   385 
       
   386     HBufC* query = PreProcessStringLC(KQueryCategoryItem);
       
   387     ExecuteMediaQueryL(aAttrs, aMedia, *query, aId);
       
   388     CleanupStack::PopAndDestroy(query);
       
   389     }
       
   390 
       
   391 // ----------------------------------------------------------------------------
       
   392 // CMPXDbCategory::GetSubCategoryItemsL
       
   393 // ----------------------------------------------------------------------------
       
   394 //
       
   395 void CMPXDbCategory::GetSubCategoryItemsL(
       
   396     TMPXGeneralCategory aParentCategory,
       
   397     TUint32 aParentId,
       
   398     const TArray<TMPXAttribute>& aAttrs,
       
   399     CMPXMediaArray& aMediaArray)
       
   400     {
       
   401     MPX_FUNC("CMPXDbCategory::GetSubCategoryItemsL");
       
   402 
       
   403     // this is only valid for albums belonging to an artist
       
   404     ASSERT((iCategory == EMPXAlbum) && (aParentCategory == EMPXArtist));
       
   405 
       
   406     // to handle the UREL warning
       
   407     (void)aParentCategory;
       
   408 
       
   409     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(KQueryCategorySubcategoryItems, aParentId));
       
   410     CleanupClosePushL(recordset);
       
   411     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
       
   412     CleanupStack::PopAndDestroy(&recordset);
       
   413     }
       
   414 
       
   415 // ----------------------------------------------------------------------------
       
   416 // CMPXDbCategory::CategoryItemExistsL
       
   417 // The category records must be in the same database as the corresponding
       
   418 // Music record, otherwise when adding a duplicate of a song on a
       
   419 // different drive this method will return true and the category record won't
       
   420 // be created.
       
   421 // ----------------------------------------------------------------------------
       
   422 //
       
   423 TBool CMPXDbCategory::CategoryItemExistsL(
       
   424     TInt aDriveId,
       
   425     TUint32 aId)
       
   426     {
       
   427     MPX_FUNC("CMPXDbCategory::CategoryItemExistsL");
       
   428 
       
   429     HBufC* query = PreProcessStringLC(KQueryCategoryItem);
       
   430     RSqlStatement recordset(
       
   431         iDbManager.ExecuteSelectQueryL(aDriveId, *query, aId));
       
   432 
       
   433     TBool exists(recordset.Next() == KSqlAtRow);
       
   434 
       
   435     recordset.Close();
       
   436     CleanupStack::PopAndDestroy(query);
       
   437 
       
   438     return exists;
       
   439     }
       
   440 
       
   441 // ----------------------------------------------------------------------------
       
   442 // CMPXDbCategory::GetSongsCountL
       
   443 // ----------------------------------------------------------------------------
       
   444 //
       
   445 TInt CMPXDbCategory::GetSongsCountL(
       
   446     TInt aDriveId,
       
   447     TUint32 aId)
       
   448     {
       
   449     MPX_FUNC("CMPXDbCategory::GetSongsCountL");
       
   450 
       
   451     HBufC* query = PreProcessStringLC(KQueryCategoryGetSongCount);
       
   452     RSqlStatement recordset(
       
   453         iDbManager.ExecuteSelectQueryL(aDriveId, *query, aId));
       
   454     CleanupClosePushL(recordset);
       
   455 
       
   456     TInt err(KErrNone);
       
   457     TInt ret(0);
       
   458     while ((err = recordset.Next()) == KSqlAtRow)
       
   459         {
       
   460         ret += recordset.ColumnInt(KMPXTableDefaultIndex);
       
   461         }
       
   462 
       
   463     if (err != KSqlAtEnd)
       
   464         {
       
   465         User::Leave(err);
       
   466         }
       
   467 
       
   468     CleanupStack::PopAndDestroy(&recordset);
       
   469     CleanupStack::PopAndDestroy(query);
       
   470 
       
   471     return ret;
       
   472     }
       
   473 
       
   474 void CMPXDbCategory::UpdateItemL(
       
   475     TUint32 /*aId*/, 
       
   476     const CMPXMedia& /*aMedia*/, 
       
   477     TInt /*aDriveId*/, 
       
   478     CMPXMessageArray* /*aItemChangedMessages*/)
       
   479 	{
       
   480 	// nothing
       
   481 	}
       
   482 
       
   483 // ----------------------------------------------------------------------------
       
   484 // CMPXDbCategory::UpdateMediaL
       
   485 // ----------------------------------------------------------------------------
       
   486 //
       
   487 void CMPXDbCategory::UpdateMediaL(
       
   488     RSqlStatement& aRecord,
       
   489     const TArray<TMPXAttribute>& aAttrs,
       
   490     CMPXMedia& aMedia)
       
   491     {
       
   492     MPX_FUNC("CMPXDbCategory::UpdateMediaL");
       
   493 
       
   494     TInt count(aAttrs.Count());
       
   495     for (TInt i = 0; i < count; ++i)
       
   496         {
       
   497         TInt contentId(aAttrs[i].ContentId());
       
   498         TUint attributeId(aAttrs[i].AttributeId());
       
   499 
       
   500         if (contentId == KMPXMediaIdGeneral)
       
   501             {
       
   502             if (attributeId & EMPXMediaGeneralId)
       
   503                 {
       
   504                 aMedia.SetTObjectValueL<TMPXItemId>(KMPXMediaGeneralId,
       
   505                     aRecord.ColumnInt64(ECategoryUniqueId));
       
   506                 }
       
   507             if (attributeId & EMPXMediaGeneralTitle)
       
   508                 {
       
   509                 aMedia.SetTextValueL(KMPXMediaGeneralTitle,
       
   510                     MPXDbCommonUtil::GetColumnTextL(aRecord, ECategoryName));
       
   511                 }
       
   512             if (attributeId & EMPXMediaGeneralCount)
       
   513                 {
       
   514                 aMedia.SetTObjectValueL<TInt>(KMPXMediaGeneralCount,
       
   515                     GetSongsCountL(KDbManagerAllDrives,
       
   516                     aRecord.ColumnInt64(ECategoryUniqueId)));
       
   517                 }
       
   518             } // end if contentId == KMPXMediaIdGeneral
       
   519         } // end for
       
   520 
       
   521     aMedia.SetTObjectValueL<TMPXGeneralType>(KMPXMediaGeneralType, EMPXItem);
       
   522     aMedia.SetTObjectValueL<TMPXGeneralCategory>(KMPXMediaGeneralCategory, iCategory);
       
   523     }
       
   524 
       
   525 // ----------------------------------------------------------------------------
       
   526 // CMPXDbCategory::GetCategoryRecordL
       
   527 // ----------------------------------------------------------------------------
       
   528 //
       
   529 RSqlStatement CMPXDbCategory::GetCategoryRecordL(
       
   530     TUint32 aId)
       
   531     {
       
   532     MPX_FUNC("CMPXDbCategory::GetCategoryRecordL");
       
   533     HBufC* query = PreProcessStringLC(KQueryCategoryItem);
       
   534     RSqlStatement statement(iDbManager.ExecuteSelectQueryL(*query, aId));
       
   535     CleanupStack::PopAndDestroy(query);
       
   536 
       
   537     return statement;
       
   538     }
       
   539 
       
   540 // ----------------------------------------------------------------------------
       
   541 // CMPXDbCategory::PreProcessStringLC
       
   542 // ----------------------------------------------------------------------------
       
   543 //
       
   544 HBufC* CMPXDbCategory::PreProcessStringLC(
       
   545     const TDesC& aQuery)
       
   546     {
       
   547     MPX_FUNC("CMPXDbCategory::PreProcessStringLC");
       
   548 
       
   549     HBufC* query = HBufC::NewLC(aQuery.Length() + KMaxTableNameCount * (iTableName->Length() +
       
   550         KCategoryTablePlaceholder().Length()));
       
   551     TPtr queryPtr(query->Des());
       
   552 
       
   553     // copy the query string
       
   554     queryPtr = aQuery;
       
   555 
       
   556     // replace all instances of the placeholder with the actual table name
       
   557     TInt index(0);
       
   558     while ((index = queryPtr.Find(KCategoryTablePlaceholder)) != KErrNotFound)
       
   559         {
       
   560         queryPtr.Replace(index, KCategoryTablePlaceholder().Length(), *iTableName);
       
   561         }
       
   562 
       
   563     return query;
       
   564     }
       
   565 
       
   566 // ----------------------------------------------------------------------------
       
   567 // CMPXDbCategory::ProcessRecordsetL
       
   568 // Unknown item is stored in the database as NULL (name field). This ensures the
       
   569 // unknown item to be the 1st found record if it exists. This will save time in
       
   570 // searching for the unknown record among the results and avoid performing
       
   571 // descriptor comparison. If the 1st record is the unknown item, it won't be
       
   572 // appended to the array until all other records have been put in the array.
       
   573 //
       
   574 // NOTE: putting unknown item to the end of the array only takes place when title
       
   575 //       field is requested. normal sorting algorithm occurs if title isn't
       
   576 //       requested.
       
   577 // ----------------------------------------------------------------------------
       
   578 //
       
   579 void CMPXDbCategory::ProcessRecordsetL(
       
   580     const TArray<TMPXAttribute>& aAttrs,
       
   581     RSqlStatement& aRecordset,
       
   582     CMPXMediaArray& aMediaArray)
       
   583     {
       
   584     // populate the array
       
   585     TBool firstRecord(ETrue);
       
   586     CMPXMedia* unknownMedia(NULL);
       
   587     TInt prevId(0);
       
   588     TInt err(KErrNone);
       
   589 
       
   590     TInt pPath(0);
       
   591     if (aMediaArray.Count())
       
   592         {
       
   593         CMPXMedia* pMedia = aMediaArray[0];
       
   594         if (pMedia->IsSupported(KMPXMediaGeneralValue))
       
   595             { // Query excuted by OpenL
       
   596             pPath = pMedia->ValueTObjectL<TInt>(KMPXMediaGeneralValue);
       
   597             MPX_ASSERT(pPath);
       
   598             }
       
   599         }
       
   600     RArray<TMPXItemId> ids;
       
   601     CleanupClosePushL(ids);
       
   602 
       
   603     while ((err = aRecordset.Next()) == KSqlAtRow)
       
   604         {
       
   605         TUint32 rowId(aRecordset.ColumnInt64(ECategoryUniqueId));
       
   606         if (prevId == rowId)
       
   607             {
       
   608             continue;
       
   609             }
       
   610 
       
   611         prevId = rowId;
       
   612         CMPXMedia* media = CMPXMedia::NewL();
       
   613         CleanupStack::PushL(media);
       
   614 
       
   615         UpdateMediaL(aRecordset, aAttrs, *media);
       
   616 
       
   617         if (firstRecord &&
       
   618             (MPXDbCommonUtil::GetColumnTextL(aRecordset, ECategoryName).Length() == 0))
       
   619             {
       
   620             unknownMedia = media;
       
   621             }
       
   622 
       
   623         if (!firstRecord || !unknownMedia)
       
   624             {
       
   625             if (media->IsSupported(KMPXMediaGeneralId) && pPath)
       
   626                 {
       
   627                 ids.AppendL(media->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   628                 }
       
   629             aMediaArray.AppendL(*media);
       
   630             CleanupStack::PopAndDestroy(media);
       
   631             }
       
   632 
       
   633         firstRecord = EFalse;
       
   634         } // end while
       
   635 
       
   636     if (err != KSqlAtEnd)
       
   637         {
       
   638         User::LeaveIfError(err);
       
   639         }
       
   640 
       
   641     if (unknownMedia)
       
   642         {
       
   643         if (unknownMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   644             {
       
   645             ids.AppendL(unknownMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   646             }
       
   647         aMediaArray.AppendL(*unknownMedia);
       
   648         CleanupStack::PopAndDestroy(unknownMedia);
       
   649         }
       
   650 
       
   651     // Append ids to the returned path
       
   652     if (pPath)
       
   653         {
       
   654         ((CMPXCollectionPath*)pPath)->AppendL(ids.Array());
       
   655         }
       
   656     CleanupStack::PopAndDestroy(&ids);
       
   657     }
       
   658 
       
   659 // ----------------------------------------------------------------------------
       
   660 // CMPXDbCategory::CreateTableL
       
   661 // ----------------------------------------------------------------------------
       
   662 //
       
   663 void CMPXDbCategory::CreateTableL(
       
   664     RSqlDatabase& aDatabase,
       
   665     TBool /* aCorruptTable */)
       
   666     {
       
   667     MPX_FUNC("CMPXDbCategory::CreateTableL");
       
   668 
       
   669     // create the table
       
   670     HBufC* query = PreProcessStringLC(KCategoryCreateTable);
       
   671     User::LeaveIfError(aDatabase.Exec(*query));
       
   672     CleanupStack::PopAndDestroy(query);
       
   673 
       
   674     // do not create an index on the Name field
       
   675     // as it only slows down the insert/update queries overall
       
   676     }
       
   677 
       
   678 // ----------------------------------------------------------------------------
       
   679 // CMPXDbCategory::DropTableL
       
   680 // ----------------------------------------------------------------------------
       
   681 //
       
   682 void CMPXDbCategory::DropTableL(
       
   683     RSqlDatabase& aDatabase)
       
   684     {
       
   685     MPX_FUNC("CMPXDbCategory::DropTableL");
       
   686 
       
   687     HBufC* query = PreProcessStringLC(KCategoryDropTable);
       
   688     User::LeaveIfError(aDatabase.Exec(*query));
       
   689     CleanupStack::PopAndDestroy(query);
       
   690     }
       
   691 
       
   692 // ----------------------------------------------------------------------------
       
   693 // CMPXDbCategory::CheckTableL
       
   694 // ----------------------------------------------------------------------------
       
   695 //
       
   696 TBool CMPXDbCategory::CheckTableL(
       
   697     RSqlDatabase& aDatabase)
       
   698     {
       
   699     MPX_FUNC("CMPXDbCategory::CheckTableL");
       
   700 
       
   701     HBufC* query = PreProcessStringLC(KCategoryCheckTable);
       
   702     TBool check(DoCheckTable(aDatabase, *query));
       
   703     CleanupStack::PopAndDestroy(query);
       
   704 
       
   705     return check;
       
   706     }
       
   707 
       
   708 // End of File