contentstorage/castorage/src/casqlitestorage.cpp
changeset 85 7feec50967db
child 86 e492551a0d54
equal deleted inserted replaced
4:1a2a00e78665 85:7feec50967db
       
     1 /*
       
     2  * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  */
       
    17 
       
    18 #include <BAUTILS.H>
       
    19 #include "caconsts.h"
       
    20 #include "casqlitestorage.h"
       
    21 #include "casqlcommands.h"
       
    22 #include "casqlquery.h"
       
    23 #include "casqlquerycreator.h"
       
    24 #include "cainnerentry.h"
       
    25 #include "cainnerquery.h"
       
    26 #include "caarraycleanup.inl"
       
    27 #include "calocalizationentry.h"
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CCASqLiteStorage::CCpStorageEngine()
       
    31 //
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CCaSqLiteStorage::CCaSqLiteStorage()
       
    35     {
       
    36     }
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CCASqLiteStorage::ConstructL()
       
    40 //
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 void CCaSqLiteStorage::ConstructL()
       
    44     {
       
    45     RFs rfs;
       
    46     CleanupClosePushL( rfs );
       
    47     User::LeaveIfError( rfs.Connect() );
       
    48 
       
    49     TFileName privatePathCDriveDb;
       
    50     User::LeaveIfError( CreatePrivateDirPath( privatePathCDriveDb, KCDrive,
       
    51             KDbName, rfs ) );
       
    52 
       
    53     TFileName privatePathZDriveDb;
       
    54     User::LeaveIfError( CreatePrivateDirPath( privatePathZDriveDb, KZDrive,
       
    55             KDbName, rfs ) );
       
    56 
       
    57     TFileName privatePathCDrive;
       
    58     User::LeaveIfError( CreatePrivateDirPath( privatePathCDrive, KCDrive,
       
    59             KNullDesC, rfs ) );
       
    60 
       
    61     if( iSqlDb.Open( privatePathCDriveDb, &KSqlDbConfig ) )
       
    62         {
       
    63         if( ( BaflUtils::FileExists( rfs, privatePathCDriveDb ) ) )
       
    64             {
       
    65             User::LeaveIfError( BaflUtils::DeleteFile( rfs,
       
    66                     privatePathCDriveDb ) );
       
    67             }
       
    68 
       
    69         if( !( BaflUtils::FileExists( rfs, privatePathZDriveDb ) ) )
       
    70             {
       
    71             User::Panic( _L("fatal error - castoragedb not exists in ROM"),
       
    72                     KErrNotFound );
       
    73             }
       
    74         else
       
    75             {
       
    76             if( !( BaflUtils::FolderExists( rfs, privatePathCDrive ) ) )
       
    77                 {
       
    78                 User::LeaveIfError( rfs.CreatePrivatePath( EDriveC ) );
       
    79                 }
       
    80             User::LeaveIfError( BaflUtils::CopyFile( rfs,
       
    81                     privatePathZDriveDb, privatePathCDrive ) );
       
    82             User::LeaveIfError( rfs.SetAtt( privatePathCDriveDb,
       
    83                     KEntryAttNormal, KEntryAttReadOnly ) );
       
    84             User::LeaveIfError( iSqlDb.Open( privatePathCDriveDb,
       
    85                     &KSqlDbConfig ) );
       
    86             }
       
    87         }
       
    88 
       
    89     CleanupStack::PopAndDestroy( &rfs );
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // CCASqLiteStorage::NewL()
       
    94 //
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 CCaSqLiteStorage* CCaSqLiteStorage::NewL()
       
    98     {
       
    99     CCaSqLiteStorage* self = CCaSqLiteStorage::NewLC();
       
   100     CleanupStack::Pop( self );
       
   101     return self;
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // CCASqLiteStorage::NewLC()
       
   106 //
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 CCaSqLiteStorage* CCaSqLiteStorage::NewLC()
       
   110     {
       
   111     CCaSqLiteStorage* self = new ( ELeave ) CCaSqLiteStorage();
       
   112     CleanupStack::PushL( self );
       
   113     self->ConstructL();
       
   114     return self;
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // CCASqLiteStorage::~CCpStorageEngine()
       
   119 //
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 CCaSqLiteStorage::~CCaSqLiteStorage()
       
   123     {
       
   124     iSqlDb.Close();
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // CCaSqLiteStorage::CreatePrivateDirPath()
       
   129 //
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 TInt CCaSqLiteStorage::CreatePrivateDirPath( TFileName& aPrivatePath,
       
   133         const TDesC& aDrive, const TDesC& aDbName, RFs& aFsSession )
       
   134     {
       
   135     TInt error( KErrNone );
       
   136 
       
   137 #ifdef CONTENT_ARSENAL_STORAGE_UT
       
   138     TFileName KPathWithoutDrive( KDoubleSlash );
       
   139 #else
       
   140     TFileName KPathWithoutDrive;
       
   141     error = aFsSession.PrivatePath( KPathWithoutDrive );
       
   142     if( error != KErrNone )
       
   143         {
       
   144         return error;
       
   145         }
       
   146 #endif
       
   147 
       
   148     aPrivatePath.Copy( aDrive );
       
   149     aPrivatePath.Append( KPathWithoutDrive );
       
   150     aPrivatePath.Append( aDbName );
       
   151 
       
   152     return error;
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------------------------
       
   156 // CCASqLiteStorage::GetListL
       
   157 //
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 void CCaSqLiteStorage::GetEntriesL( const CCaInnerQuery* aQuery,
       
   161         RPointerArray<CCaInnerEntry>& aResultContainer )
       
   162     {
       
   163     //    Fetch entries from
       
   164     CCaSqlQuery* sqlGetEntriesQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   165     CaSqlQueryCreator::CreateGetEntriesQueryL( aQuery, sqlGetEntriesQuery );
       
   166     sqlGetEntriesQuery->PrepareL();
       
   167     sqlGetEntriesQuery->BindValuesForGetEntriesL( aQuery );
       
   168     sqlGetEntriesQuery->ExecuteL( aResultContainer, CCaSqlQuery::EEntry );
       
   169     CleanupStack::PopAndDestroy( sqlGetEntriesQuery );
       
   170 
       
   171     //    Populate the entries with their attributes
       
   172     if( aResultContainer.Count() > 0 )
       
   173         {
       
   174         CCaSqlQuery* sqlGetAttributesQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   175         CaSqlQueryCreator::CreateGetAttributesQueryL(
       
   176                 aResultContainer.Count(), sqlGetAttributesQuery );
       
   177         sqlGetAttributesQuery->PrepareL();
       
   178         sqlGetAttributesQuery->BindEntryIdsL( aResultContainer );
       
   179         sqlGetAttributesQuery->ExecuteL( aResultContainer,
       
   180                 CCaSqlQuery::EAttribute );
       
   181         CleanupStack::PopAndDestroy( sqlGetAttributesQuery );
       
   182         }
       
   183     //  set entries if proper order if they were fetched by ids
       
   184     if( aQuery->GetIds().Count() > 0 )
       
   185         {
       
   186         SetEntriesInProperOrderL( aQuery->GetIds(), aResultContainer );
       
   187         }
       
   188     }
       
   189 
       
   190 void CCaSqLiteStorage::LocalizeEntryL( CCaLocalizationEntry& aLocalization )
       
   191     {
       
   192     CCaSqlQuery* sqlLocalizeEntryQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   193     CaSqlQueryCreator::CreateLocalizeEntryQueryL( sqlLocalizeEntryQuery );
       
   194     sqlLocalizeEntryQuery->PrepareL();
       
   195     sqlLocalizeEntryQuery->BindValuesForLocalizeL( aLocalization );
       
   196     sqlLocalizeEntryQuery->ExecuteL( );
       
   197     CleanupStack::PopAndDestroy( sqlLocalizeEntryQuery );
       
   198     }
       
   199 
       
   200 void CCaSqLiteStorage::GetLocalizationsL( RPointerArray<CCaLocalizationEntry>& aResultContainer )
       
   201     {
       
   202     CCaSqlQuery* sqlGetLocalizationsQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   203     CaSqlQueryCreator::CreateGetLocalizationsQueryL( sqlGetLocalizationsQuery );
       
   204     
       
   205     sqlGetLocalizationsQuery->PrepareL();
       
   206     sqlGetLocalizationsQuery->ExecuteLocalizationsL( aResultContainer );
       
   207     CleanupStack::PopAndDestroy( sqlGetLocalizationsQuery );
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // CCASqLiteStorage::GetEntriesIdsL
       
   212 //
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void CCaSqLiteStorage::GetEntriesIdsL( const CCaInnerQuery* aQuery, 
       
   216         RArray<TInt>& aResultIdArray )
       
   217     {
       
   218     CCaSqlQuery* sqlGetEntriesIdsQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   219     CaSqlQueryCreator::CreateGetEntriesQueryL( aQuery,
       
   220             sqlGetEntriesIdsQuery );
       
   221     sqlGetEntriesIdsQuery->PrepareL();
       
   222     sqlGetEntriesIdsQuery->BindValuesForGetEntriesL( aQuery );
       
   223     sqlGetEntriesIdsQuery->ExecuteL( aResultIdArray,
       
   224             CCaSqlQuery::EEntryTable );
       
   225     CleanupStack::PopAndDestroy( sqlGetEntriesIdsQuery );
       
   226     }
       
   227 
       
   228 // ---------------------------------------------------------------------------
       
   229 // CCASqLiteStorage::GetEntriesIdsL
       
   230 //
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 void CCaSqLiteStorage::GetParentsIdsL( const RArray<TInt>& aEntryIdArray,
       
   234         RArray<TInt>& aParentIdArray )
       
   235     {
       
   236     CCaSqlQuery* sqlGetParentIdsQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   237     CaSqlQueryCreator::CreateGetParentsIdsQueryL( aEntryIdArray,
       
   238             sqlGetParentIdsQuery, aParentIdArray );
       
   239     sqlGetParentIdsQuery->PrepareL();
       
   240     sqlGetParentIdsQuery->BindIdsL( aEntryIdArray, KSQLGEEntryId );
       
   241     if( aParentIdArray.Count() > 0 )
       
   242         {
       
   243         sqlGetParentIdsQuery->BindIdsL( aParentIdArray, KSQLGEIdGroup );
       
   244         }
       
   245     TInt parentCount = sqlGetParentIdsQuery->ExecuteL( aParentIdArray,
       
   246             CCaSqlQuery::EGroupTable );
       
   247     CleanupStack::PopAndDestroy( sqlGetParentIdsQuery );
       
   248     if( parentCount > 0 )
       
   249         {
       
   250         GetParentsIdsL( aParentIdArray, aParentIdArray );
       
   251         }
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // CCASqLiteStorage::AddL
       
   256 //
       
   257 // ---------------------------------------------------------------------------
       
   258 //
       
   259 void CCaSqLiteStorage::AddL( CCaInnerEntry* aEntry, TBool aUpdate)
       
   260     {
       
   261     RPointerArray<CCaSqlQuery> sqlQueries;
       
   262     CleanupResetAndDestroyPushL( sqlQueries );
       
   263 
       
   264     CaSqlQueryCreator::CreateAddQueryL( aEntry, sqlQueries, iSqlDb );
       
   265 
       
   266     ExecuteStatementL( KSqlStatementBegin );
       
   267     TRAPD( err, ExecuteAddL( aEntry, sqlQueries ) );
       
   268     if( err )
       
   269         {
       
   270         ExecuteStatementL( KSqlStatementRollback );
       
   271         User::Leave( err );
       
   272         }
       
   273     ExecuteStatementL( KSqlStatementCommit );
       
   274 
       
   275     CleanupStack::PopAndDestroy( &sqlQueries );
       
   276     
       
   277     if( aUpdate )
       
   278         {
       
   279         RemoveFromLocalizationL( aEntry->GetId() );
       
   280         }
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // CCASqLiteStorage::RemoveL
       
   285 //
       
   286 // ---------------------------------------------------------------------------
       
   287 //
       
   288 void CCaSqLiteStorage::RemoveL( const RArray<TInt>& aEntryIds )
       
   289     {
       
   290     ExecuteStatementL( KSqlStatementBegin );
       
   291     TRAPD( err, ExecuteRemoveL( aEntryIds ) );
       
   292     if( err )
       
   293         {
       
   294         ExecuteStatementL( KSqlStatementRollback );
       
   295         User::Leave( err );
       
   296         }
       
   297     ExecuteStatementL( KSqlStatementCommit );
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // CCASqLiteStorage::OrganizeL( const RArray<TInt>& aEntryIds, 
       
   302 //        TCaOperationParams aParams )
       
   303 //
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 void CCaSqLiteStorage::OrganizeL( const RArray<TInt>& aEntryIds,
       
   307         TCaOperationParams aParams )
       
   308     {
       
   309     ExecuteStatementL( KSqlStatementBegin );
       
   310     TRAPD( err, ExecuteOrganizeL( aEntryIds, aParams ) );
       
   311     if( err )
       
   312         {
       
   313         ExecuteStatementL( KSqlStatementRollback );
       
   314         User::Leave( err );
       
   315         }
       
   316     ExecuteStatementL( KSqlStatementCommit );
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // CCASqLiteStorage::TouchL( const TInt aEntryId )
       
   321 //
       
   322 // ---------------------------------------------------------------------------
       
   323 //
       
   324 void CCaSqLiteStorage::TouchL( const TInt aEntryId )
       
   325     {
       
   326     ExecuteStatementL( KSqlStatementBegin );
       
   327     TRAPD( err, ExecuteTouchL( aEntryId ) );
       
   328     if( err )
       
   329         {
       
   330         ExecuteStatementL( KSqlStatementRollback );
       
   331         User::Leave( err );
       
   332         }
       
   333     ExecuteStatementL( KSqlStatementCommit );
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 // CCASqLiteStorage::DbPropertyL( ( const TDesC& aProperty, TDes& aPropertyValue )
       
   338 //
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 void CCaSqLiteStorage::DbPropertyL( const TDesC& aProperty,
       
   342         TDes& aPropertyValue )
       
   343     {
       
   344     ExecuteStatementL( KSqlStatementBegin );
       
   345     TRAPD( err, ExecuteDbPropertyL( aProperty, aPropertyValue ) );
       
   346     if( err )
       
   347         {
       
   348         ExecuteStatementL( KSqlStatementRollback );
       
   349         User::Leave( err );
       
   350         }
       
   351     ExecuteStatementL( KSqlStatementCommit );
       
   352     }
       
   353 
       
   354 // ---------------------------------------------------------------------------
       
   355 // CCASqLiteStorage::SetDBPropertyL( const TDesC& aProperty, const TDesC& aPropertyValue )
       
   356 //
       
   357 // ---------------------------------------------------------------------------
       
   358 //
       
   359 void CCaSqLiteStorage::SetDBPropertyL( const TDesC& aProperty,
       
   360         const TDesC& aPropertyValue )
       
   361     {
       
   362     ExecuteStatementL( KSqlStatementBegin );
       
   363     TRAPD( err, ExecuteSetDbPropertyL( aProperty, aPropertyValue ) );
       
   364     if( err )
       
   365         {
       
   366         ExecuteStatementL( KSqlStatementRollback );
       
   367         User::Leave( err );
       
   368         }
       
   369     ExecuteStatementL( KSqlStatementCommit );
       
   370     }
       
   371 
       
   372 // ---------------------------------------------------------------------------
       
   373 // CCaSqLiteStorage::RemoveFromLocalizationL( const TInt aEntryId )
       
   374 //
       
   375 // ---------------------------------------------------------------------------
       
   376 //
       
   377 void CCaSqLiteStorage::RemoveFromLocalizationL( const TInt aEntryId )
       
   378     {
       
   379     ExecuteStatementL( KSqlStatementBegin );
       
   380     TRAPD( err, ExecuteRemoveFromLocalizationL( aEntryId ) );
       
   381     if( err )
       
   382         {
       
   383         ExecuteStatementL( KSqlStatementRollback );
       
   384         User::Leave( err );
       
   385         }
       
   386     ExecuteStatementL( KSqlStatementCommit );
       
   387     }
       
   388 
       
   389 
       
   390 // ---------------------------------------------------------------------------
       
   391 // CCaSqLiteStorage::CustomSortL( const RArray<TInt>& aEntryIds, const TInt aGroupId )
       
   392 //
       
   393 // ---------------------------------------------------------------------------
       
   394 //
       
   395 void CCaSqLiteStorage::CustomSortL( const RArray<TInt>& aEntryIds,
       
   396         const TInt aGroupId )
       
   397     {
       
   398     RPointerArray<CCaSqlQuery> sqlQueries;
       
   399     CleanupResetAndDestroyPushL( sqlQueries );
       
   400 
       
   401     CaSqlQueryCreator::CreateCustomSortQueryL( aEntryIds, sqlQueries, iSqlDb );
       
   402 
       
   403     ExecuteStatementL( KSqlStatementBegin );
       
   404     TRAPD( err, ExecuteCustomSortL( aEntryIds, aGroupId, sqlQueries ) );
       
   405     if( err )
       
   406         {
       
   407         ExecuteStatementL( KSqlStatementRollback );
       
   408         User::Leave( err );
       
   409         }
       
   410     ExecuteStatementL( KSqlStatementCommit );
       
   411 
       
   412     CleanupStack::PopAndDestroy( &sqlQueries );
       
   413     }
       
   414 
       
   415 // ---------------------------------------------------------------------------
       
   416 // CCaSqLiteStorage::ExecuteCustomSortL( const RArray<TInt>& aEntryIds, const TInt aGroupId )
       
   417 //
       
   418 // ---------------------------------------------------------------------------
       
   419 //
       
   420 void CCaSqLiteStorage::ExecuteCustomSortL( const RArray<TInt>& aEntryIds,
       
   421         const TInt aGroupId , RPointerArray<CCaSqlQuery>& aSqlQuery )
       
   422     {
       
   423     for( TInt i = 0; i < aSqlQuery.Count(); i++ )
       
   424         {
       
   425         TInt newPosition = i + 1;
       
   426         TInt entryId = aEntryIds[i];
       
   427         
       
   428         aSqlQuery[i]->PrepareL();
       
   429         aSqlQuery[i]->BindValuesForCustomSortL( aGroupId, entryId, newPosition );
       
   430         aSqlQuery[i]->ExecuteL();
       
   431         aSqlQuery[i]->CloseStatement();
       
   432         }
       
   433     }
       
   434 
       
   435 // ---------------------------------------------------------------------------
       
   436 // CCaSqLiteStorage::ExecuteRemoveFromLocalizationL( const TInt aEntryId )
       
   437 //
       
   438 // ---------------------------------------------------------------------------
       
   439 //
       
   440 void CCaSqLiteStorage::ExecuteRemoveFromLocalizationL( const TInt aEntryId )
       
   441     {
       
   442     CCaSqlQuery* query = CCaSqlQuery::NewLC( iSqlDb );
       
   443     query->SetQueryL( KSQLDeleteLocalizationTextWhereEntryId );
       
   444     query->PrepareL();
       
   445     query->BindValuesForRemoveFromLocalizationL( aEntryId );
       
   446     query->ExecuteL();
       
   447     CleanupStack::PopAndDestroy( query );
       
   448     }
       
   449 
       
   450 // ---------------------------------------------------------------------------
       
   451 // CCASqLiteStorage::ExecuteStatementL( const TDesC& aStatement )
       
   452 //
       
   453 // ---------------------------------------------------------------------------
       
   454 //
       
   455 void CCaSqLiteStorage::ExecuteStatementL( const TDesC& aStatement )
       
   456     {
       
   457     CCaSqlQuery* query = CCaSqlQuery::NewLC( iSqlDb );
       
   458     query->SetQueryL( aStatement );
       
   459     query->PrepareL();
       
   460     query->ExecuteL();
       
   461     CleanupStack::PopAndDestroy( query );
       
   462     }
       
   463 
       
   464 // ---------------------------------------------------------------------------
       
   465 // CCASqLiteStorage::ExecuteAddL(CCaInnerEntry* aEntry, 
       
   466 //         RPointerArray<CCASqlQuery>& sqlQuery)
       
   467 //
       
   468 // ---------------------------------------------------------------------------
       
   469 //
       
   470 void CCaSqLiteStorage::ExecuteAddL( CCaInnerEntry* aEntry, 
       
   471         RPointerArray<CCaSqlQuery>& aSqlQuery )
       
   472     {
       
   473     TBool isAttributeDeleted( EFalse );
       
   474     for( TInt i = 0; i < aSqlQuery.Count(); i++ )
       
   475         {
       
   476         if( aSqlQuery[i]->Type() == CCaSqlQuery::EIconTable )
       
   477             {
       
   478             //check if icon exists - just update
       
   479             CCaSqlQuery* query = CCaSqlQuery::NewLC( iSqlDb );
       
   480             query->SetQueryL( KSQLGetIconId );
       
   481             query->SetTableType( CCaSqlQuery::EIconTable );
       
   482             query->PrepareL();
       
   483             query->BindValuesForGetIconL( aEntry );
       
   484             CCaInnerEntry::TIconAttributes iconAttributes;
       
   485             TInt countIcons = query->ExecuteL( iconAttributes );
       
   486             CleanupStack::PopAndDestroy( query );
       
   487    
       
   488             aSqlQuery[i]->PrepareL();
       
   489             aSqlQuery[i]->BindValuesForAddL( aEntry );
       
   490             TInt tmpId( 0 );
       
   491             TRAPD( err, tmpId = aSqlQuery[i]->ExecuteL() );
       
   492             aSqlQuery[i]->CloseStatement();
       
   493             if( !countIcons && !aEntry->GetIconId() )
       
   494                 { // new icon added
       
   495                 aEntry->SetIconId( tmpId );
       
   496                 }
       
   497 
       
   498             if( err == KSqlErrConstraint )
       
   499                 {
       
   500                 // ignore, this means that the icon cannot be removed
       
   501                 // because some other entry has the same icon.
       
   502                 }
       
   503             else if( err )
       
   504                 {
       
   505                 User::Leave( err );
       
   506                 }
       
   507 
       
   508             }
       
   509         else if( aSqlQuery[i]->Type() == CCaSqlQuery::EEntryTable )
       
   510             {
       
   511             aSqlQuery[i]->PrepareL();
       
   512             aSqlQuery[i]->BindValuesForAddL( aEntry );
       
   513             TInt tmpId = aSqlQuery[i]->ExecuteL();
       
   514             aSqlQuery[i]->CloseStatement();
       
   515             if( !aEntry->GetId() )
       
   516                 {
       
   517                 // new entry added
       
   518                 aEntry->SetId( tmpId );
       
   519                 }
       
   520             }
       
   521         else if( !isAttributeDeleted
       
   522                 && aSqlQuery[i]->Type() == CCaSqlQuery::EAttributeTable )
       
   523             {
       
   524             // delete all entry's attributes, just ones
       
   525             aSqlQuery[i]->PrepareL();
       
   526             aSqlQuery[i]->BindEntryIdL( aEntry->GetId() );
       
   527             aSqlQuery[i]->ExecuteL();
       
   528             aSqlQuery[i]->CloseStatement();
       
   529             isAttributeDeleted = ETrue;
       
   530             }
       
   531         else if( aSqlQuery[i]->Type()==CCaSqlQuery::EAttributeTable )
       
   532             {
       
   533             // add new attribute(s) if it's neccesery
       
   534             aSqlQuery[i]->PrepareL();
       
   535             for( TInt k(0); k < aEntry->GetAttributes().Count(); k++ )
       
   536                 {
       
   537                 // if(id_entry, name) exist it'll replaced by new value
       
   538                 aSqlQuery[i]->BindValuesForAddAttributeL( aEntry,
       
   539                         aEntry->GetAttributes()[k] );
       
   540                 aSqlQuery[i]->ExecuteL();
       
   541                 aSqlQuery[i]->ResetStatement();
       
   542                 }
       
   543             aSqlQuery[i]->CloseStatement();
       
   544             }
       
   545         }
       
   546     }
       
   547 // ---------------------------------------------------------------------------
       
   548 // CCASqLiteStorage::ExecuteRemoveL( const RArray<TInt>& aEntryIds)
       
   549 //
       
   550 // ---------------------------------------------------------------------------
       
   551 //
       
   552 void CCaSqLiteStorage::ExecuteRemoveL( const RArray<TInt>& aEntryIds )
       
   553     {
       
   554     for( int i=0; i<aEntryIds.Count(); i++ )
       
   555         {
       
   556         // check if entry exist in database
       
   557         CCaSqlQuery* queryEntry = CCaSqlQuery::NewLC( iSqlDb );
       
   558         queryEntry->SetQueryL( KSQLGetEntryIdFromEntryTable );
       
   559         queryEntry->PrepareL();
       
   560         queryEntry->BindEntryIdL( aEntryIds[i] );
       
   561         TInt entryCount( 0 );
       
   562         queryEntry->ExecuteL( entryCount );
       
   563         CleanupStack::PopAndDestroy( queryEntry );
       
   564         if( !entryCount )
       
   565             {
       
   566             User::Leave( KErrNotFound );
       
   567             }
       
   568 
       
   569         // SELECT Id_Icon FROM CA_ENTRY WHERE Id = aEntryId;
       
   570         CCaSqlQuery* queryIconId = CCaSqlQuery::NewLC( iSqlDb );
       
   571         queryIconId->SetQueryL( KSQLGETIconIdFromEntryTable );
       
   572         queryIconId->PrepareL();
       
   573         queryIconId->BindEntryIdL( aEntryIds[i] );
       
   574         TInt iconId( 0 );
       
   575         queryIconId->ExecuteL( iconId );
       
   576         CleanupStack::PopAndDestroy( queryIconId );
       
   577 
       
   578         RPointerArray<CCaSqlQuery> sqlQuery;
       
   579         CleanupResetAndDestroyPushL( sqlQuery );
       
   580         CaSqlQueryCreator::CreateRemoveQueryL( sqlQuery, iSqlDb );
       
   581         for( TInt j = 0; j < sqlQuery.Count(); j++ )
       
   582             {
       
   583             if( !( sqlQuery[j]->Type() == CCaSqlQuery::EIconTable &&
       
   584                     iconId == 0 ) )
       
   585                 {
       
   586                 sqlQuery[j]->PrepareL();
       
   587                 sqlQuery[j]->BindValuesForRemoveL( aEntryIds[i], iconId );
       
   588                 TRAPD( err, sqlQuery[j]->ExecuteL() );
       
   589                 sqlQuery[j]->CloseStatement();
       
   590 
       
   591                 if( sqlQuery[j]->Type() == CCaSqlQuery::EIconTable &&
       
   592                         err == KSqlErrConstraint )
       
   593                     {
       
   594                     // ignore, this means that the icon cannot be removed
       
   595                     // because some other entry has the same icon.
       
   596                     }
       
   597                 else if( err )
       
   598                     {
       
   599                     User::Leave( err );
       
   600                     }
       
   601                 }
       
   602             }
       
   603         CleanupStack::PopAndDestroy( &sqlQuery );
       
   604         }
       
   605     }
       
   606 
       
   607 // ---------------------------------------------------------------------------
       
   608 // CCASqLiteStorage::ExecuteOrganizeL(const RArray<TInt>& aEntryIds,
       
   609 //      TCaOperationParams aParams)
       
   610 //
       
   611 // ---------------------------------------------------------------------------
       
   612 //
       
   613 void CCaSqLiteStorage::ExecuteOrganizeL( const RArray<TInt>& aEntryIds,
       
   614         TCaOperationParams aParams )
       
   615     {
       
   616     VerifyOrganizeParamsL( aEntryIds, aParams );
       
   617 
       
   618     RPointerArray<CCaSqlQuery> sqlQueries;
       
   619     CleanupResetAndDestroyPushL( sqlQueries );
       
   620 
       
   621     TBool revereseInsertOrder = CaSqlQueryCreator::CreateOrganizeQueryL(
       
   622             aEntryIds, aParams, iSqlDb, sqlQueries );
       
   623 
       
   624     for( TInt i = 0; i < sqlQueries.Count(); i++ )
       
   625         {
       
   626         switch( sqlQueries[i]->Type() )
       
   627             {
       
   628             case CCaSqlQuery::EGroupTableRemove:
       
   629                 {
       
   630                 sqlQueries[i]->PrepareL();
       
   631                 sqlQueries[i]->BindValuesForOrganizeL( aEntryIds, aParams );
       
   632                 sqlQueries[i]->ExecuteL();
       
   633                 sqlQueries[i]->CloseStatement();
       
   634                 }
       
   635                 break;
       
   636 
       
   637             case CCaSqlQuery::EGroupTableInsert:
       
   638                 {
       
   639                 sqlQueries[i]->PrepareL();
       
   640                 for( TInt j = 0; j < aEntryIds.Count(); j++ )
       
   641                     {
       
   642                     TInt k = (revereseInsertOrder) ? 
       
   643                         (aEntryIds.Count() - 1) - j : j;
       
   644                     sqlQueries[i]->BindValuesForOrganizeL( 
       
   645                             aEntryIds, aParams, aEntryIds[k] );
       
   646                     sqlQueries[i]->ExecuteL();
       
   647                     sqlQueries[i]->ResetStatement();
       
   648                     }
       
   649                 sqlQueries[i]->CloseStatement();
       
   650                 }
       
   651                 break;
       
   652             }
       
   653         }
       
   654 
       
   655     CleanupStack::PopAndDestroy( &sqlQueries );
       
   656     }
       
   657 
       
   658 // ---------------------------------------------------------------------------
       
   659 // CCASqLiteStorage::ExecuteTouchL( const TInt aEntryId )
       
   660 //
       
   661 // ---------------------------------------------------------------------------
       
   662 //
       
   663 void CCaSqLiteStorage::ExecuteTouchL( const TInt aEntryId )
       
   664     {
       
   665     RPointerArray<CCaSqlQuery> sqlQuery;
       
   666     CleanupResetAndDestroyPushL( sqlQuery );
       
   667 
       
   668     CaSqlQueryCreator::CreateTouchQueryL( sqlQuery, iSqlDb );
       
   669 
       
   670     TTime time;
       
   671     time.UniversalTime();
       
   672 
       
   673     for( TInt i = 0; i < sqlQuery.Count(); i++ )
       
   674         {
       
   675         sqlQuery[i]->PrepareL();
       
   676         sqlQuery[i]->BindValuesForLaunchL( aEntryId, time.Int64() );
       
   677         sqlQuery[i]->ExecuteL();
       
   678         sqlQuery[i]->CloseStatement();
       
   679         }
       
   680 
       
   681     CleanupStack::PopAndDestroy( &sqlQuery );
       
   682     }
       
   683 
       
   684 // ---------------------------------------------------------------------------
       
   685 // ExecuteDbPropertyL( const TDesC& aProperty, TDes& aPropertyValue )
       
   686 //
       
   687 // ---------------------------------------------------------------------------
       
   688 //
       
   689 void CCaSqLiteStorage::ExecuteDbPropertyL( const TDesC& aProperty,
       
   690         TDes& aPropertyValue )
       
   691     {
       
   692     CCaSqlQuery* sqlQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   693     sqlQuery->SetQueryL( KSQLGetProperty );
       
   694     sqlQuery->PrepareL();
       
   695     sqlQuery->BindValuesForPropertyL( aProperty );
       
   696     sqlQuery->ExecutePropertyL( aPropertyValue );
       
   697     sqlQuery->CloseStatement();
       
   698     CleanupStack::PopAndDestroy( sqlQuery );
       
   699     }
       
   700 
       
   701 // ---------------------------------------------------------------------------
       
   702 // ExecuteSetDbPropertyL( const TDesC& aProperty, const TDesC& aPropertyValue )
       
   703 //
       
   704 // ---------------------------------------------------------------------------
       
   705 //
       
   706 void CCaSqLiteStorage::ExecuteSetDbPropertyL( const TDesC& aProperty,
       
   707         const TDesC& aPropertyValue )
       
   708     {
       
   709     CCaSqlQuery* sqlQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   710     sqlQuery->SetQueryL( KSQLUpdateProperty );
       
   711     sqlQuery->PrepareL();
       
   712     sqlQuery->BindValuesForPropertyUpdateL( aProperty, aPropertyValue );
       
   713     sqlQuery->ExecuteL();
       
   714     sqlQuery->CloseStatement();
       
   715     CleanupStack::PopAndDestroy( sqlQuery );
       
   716     }
       
   717 
       
   718 
       
   719 // ---------------------------------------------------------------------------
       
   720 // CCASqLiteStorage::RemoveOldEntriesFromLaunchTableL( TInt aDays )
       
   721 //
       
   722 // ---------------------------------------------------------------------------
       
   723 //
       
   724 void CCaSqLiteStorage::RemoveOldEntriesFromLaunchTableL( TInt aDays )
       
   725     {
       
   726     TTime presentTime;
       
   727     presentTime.UniversalTime();
       
   728     TTime borderTime = presentTime - ( TTimeIntervalDays( aDays ) );
       
   729 
       
   730     CCaSqlQuery* sqlQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   731     sqlQuery->SetQueryL( KSQLDeleteOldFromLaunch );
       
   732     sqlQuery->PrepareL();
       
   733     sqlQuery->BindValuesForLaunchL( 0, borderTime.Int64() );
       
   734     sqlQuery->ExecuteL();
       
   735 
       
   736     CleanupStack::PopAndDestroy( sqlQuery );
       
   737     }
       
   738 
       
   739 // ---------------------------------------------------------------------------
       
   740 // CCASqLiteStorage::VerifyOrganizeParamsL( const RArray<TInt>& aEntryIds,
       
   741 //      TCaOperationParams aParams);
       
   742 //
       
   743 // ---------------------------------------------------------------------------
       
   744 //
       
   745 void CCaSqLiteStorage::VerifyOrganizeParamsL( const RArray<TInt>& aEntryIds,
       
   746         TCaOperationParams aParams )
       
   747     {
       
   748     switch( aParams.iOperationType )
       
   749         {
       
   750         case TCaOperationParams::ERemove:
       
   751             {
       
   752             CCaSqlQuery * query = CCaSqlQuery::NewLC( iSqlDb );
       
   753             CaSqlQueryCreator::PopulateQueryWithIdListL( aEntryIds.Count(),
       
   754                     query, KSQLOrganizeGetEntryIdCountFromGroup );
       
   755 
       
   756             query->PrepareL();
       
   757             query->BindValuesForOrganizeL( aEntryIds, aParams );
       
   758             TInt dbEntryCount;
       
   759             query->ExecuteL( dbEntryCount );
       
   760             query->CloseStatement();
       
   761             if( dbEntryCount < aEntryIds.Count() )
       
   762                 {
       
   763                 User::Leave( KErrArgument );
       
   764                 }
       
   765 
       
   766             CleanupStack::PopAndDestroy( query );
       
   767             break;
       
   768             }
       
   769         case TCaOperationParams::EInsert:
       
   770             {
       
   771             CCaSqlQuery * query = CCaSqlQuery::NewLC( iSqlDb );
       
   772             CaSqlQueryCreator::PopulateQueryWithIdListL( 1, query,
       
   773                     KSQLOrganizeGetEntryIdCountFromGroup );
       
   774             RArray<TInt> id;
       
   775             CleanupClosePushL( id );
       
   776             id.AppendL( aParams.iBeforeEntryId );
       
   777 
       
   778             query->PrepareL();
       
   779             query->BindValuesForOrganizeL( id, aParams );
       
   780             TInt dbEntryCount;
       
   781             query->ExecuteL( dbEntryCount );
       
   782             query->CloseStatement();
       
   783             if( dbEntryCount == 0 )
       
   784                 {
       
   785                 User::Leave( KErrArgument );
       
   786                 }
       
   787 
       
   788             CleanupStack::PopAndDestroy( &id );
       
   789             CleanupStack::PopAndDestroy( query );
       
   790             break;
       
   791             }
       
   792         case TCaOperationParams::EPrepend:
       
   793         case TCaOperationParams::EAppend:
       
   794         default:
       
   795             {
       
   796             //            TODO: do nothing when default
       
   797             break;
       
   798             }
       
   799         }
       
   800     }
       
   801 
       
   802 // ---------------------------------------------------------------------------
       
   803 // CCASqLiteStorage::SetEntriesInProperOrderL
       
   804 // ---------------------------------------------------------------------------
       
   805 //
       
   806 void CCaSqLiteStorage::SetEntriesInProperOrderL(
       
   807         const RArray<TInt>& aEntryIds,
       
   808         RPointerArray<CCaInnerEntry>& aResultContainer )
       
   809     {
       
   810     for( TInt i = 0; i < aEntryIds.Count(); i++ )
       
   811         {
       
   812         for( TInt j = i + 1; j < aResultContainer.Count(); j++ )
       
   813             {
       
   814             if( aEntryIds[i] == aResultContainer[j]->GetId() )
       
   815                 {
       
   816                 CCaInnerEntry* temporary( NULL );
       
   817                 temporary = aResultContainer[i];
       
   818                 aResultContainer[i] = aResultContainer[j];
       
   819                 aResultContainer[j] = temporary;
       
   820                 break;
       
   821                 }
       
   822             }
       
   823         }
       
   824     }
       
   825