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