wmdrm/wmdrmengine/wmdrmserver/server/src/wmdrmdb.cpp
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     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:  database implementation for WMDRM
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32std.h>
       
    19 
       
    20 #include <systemwarninglevels.hrh>
       
    21 #include "wmdrmdb.h"
       
    22 #include "wmdrmfileserverclient.h"
       
    23 #include "wmdrmdatastore.h"
       
    24 #include "drmrightsstoringlocation.h"
       
    25 #include "drmutilityinternaltypes.h"
       
    26 
       
    27 #define _LOGGING_FILE L"wmdrmserver.txt"
       
    28 
       
    29 #include "flogger.h"
       
    30 #include "logfn.h"
       
    31 
       
    32 #define FROM_HEX_DIGIT(x) (x >= 'a' ? x - 'a' + 10 : x - '0')
       
    33 #define FROM_HEX(x,y) (FROM_HEX_DIGIT(x) * 16 + FROM_HEX_DIGIT(y))
       
    34 
       
    35 // For secure, sync and metering stores in the case of differentiation of stores
       
    36 // to system drive and internal mass drive or for all the stores in the case of 
       
    37 // no differentation
       
    38 _LIT( KDatabaseName, "%c:[10282F1B]hds.db" );
       
    39 
       
    40 // For license store in the case of differentation of stores. Not used
       
    41 // if no differentation is applied.                                              
       
    42 _LIT( KDatabaseName2, "%c:[10282F1B]hds2.db" );
       
    43 _LIT8( KDatabaseConfig, "cache_size=64; page_size=2048; encoding=UTF-8" );
       
    44 
       
    45 _LIT( KBeginTransaction, "BEGIN TRANSACTION" );
       
    46 _LIT( KCommitTransaction, "COMMIT TRANSACTION" );
       
    47 
       
    48 _LIT( KCreateLicenseTableString, "CREATE TABLE IF NOT EXISTS license_t (hashKey BINARY NOT NULL, uniqueKey BINARY NOT NULL, data BINARY, PRIMARY KEY(hashKey, uniqueKey))" );
       
    49 _LIT( KCreateSecureTableString, "CREATE TABLE IF NOT EXISTS secure_t (hashKey BINARY NOT NULL, uniqueKey BINARY NOT NULL, data BINARY, PRIMARY KEY(hashKey, uniqueKey))" );
       
    50 _LIT( KCreateSyncTableString, "CREATE TABLE IF NOT EXISTS sync_t (hashKey BINARY NOT NULL, uniqueKey BINARY NOT NULL, data BINARY, PRIMARY KEY(hashKey, uniqueKey))" );
       
    51 _LIT( KCreateMeteringTableString, "CREATE TABLE IF NOT EXISTS metering_t (hashKey BINARY NOT NULL, uniqueKey BINARY NOT NULL, data BINARY, PRIMARY KEY(hashKey, uniqueKey))" );
       
    52 
       
    53 _LIT( KDeleteLicenseTableString, "DROP TABLE IF EXISTS license_t" );
       
    54 _LIT( KDeleteSecureTableString, "DROP TABLE IF EXISTS secure_t" );
       
    55 _LIT( KDeleteSyncTableString, "DROP TABLE IF EXISTS sync_t" );
       
    56 _LIT( KDeleteMeteringTableString, "DROP TABLE IF EXISTS metering_t" );
       
    57 
       
    58 _LIT( KSelectNRowIDsLicenseTableString, "SELECT RowID FROM license_t LIMIT %d" );
       
    59 _LIT( KSelectNRowIDsSecureTableString, "SELECT RowID FROM secure_t LIMIT %d" );
       
    60 _LIT( KSelectNRowIDsSyncTableString, "SELECT RowID FROM sync_t LIMIT %d" );
       
    61 _LIT( KSelectNRowIDsMeteringTableString, "SELECT RowID FROM metering_t LIMIT %d" );
       
    62 
       
    63 _LIT( KDeleteWithRowIDLicenseTableString, "DELETE FROM license_t WHERE RowID = :rowID" );
       
    64 _LIT( KDeleteWithRowIDSecureTableString, "DELETE FROM secure_t WHERE RowID = :rowID" );
       
    65 _LIT( KDeleteWithRowIDSyncTableString, "DELETE FROM sync_t WHERE RowID = :rowID" );
       
    66 _LIT( KDeleteWithRowIDMeteringTableString, "DELETE FROM metering_t WHERE RowID = :rowID" );
       
    67 
       
    68 _LIT( KDeleteLicenseString, "DELETE FROM license_t WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    69 _LIT( KDeleteSecureString, "DELETE FROM secure_t WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    70 _LIT( KDeleteSyncString, "DELETE FROM sync_t WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    71 _LIT( KDeleteMeteringString, "DELETE FROM metering_t WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    72 
       
    73 _LIT( KInsertLicenseString, "INSERT INTO license_t (hashKey, uniqueKey) VALUES (:HashKey, :UniqueKey)" );
       
    74 _LIT( KInsertSecureString, "INSERT INTO secure_t (hashKey, uniqueKey) VALUES (:HashKey, :UniqueKey)" );
       
    75 _LIT( KInsertSyncString, "INSERT INTO sync_t (hashKey, uniqueKey) VALUES (:HashKey, :UniqueKey)" );
       
    76 _LIT( KInsertMeteringString, "INSERT INTO metering_t (hashKey, uniqueKey) VALUES (:HashKey, :UniqueKey)" );
       
    77 
       
    78 _LIT( KUpdateLicenseDataString, "UPDATE license_t SET data=:Data WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    79 _LIT( KUpdateSecureDataString, "UPDATE secure_t SET data=:Data WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    80 _LIT( KUpdateSyncDataString, "UPDATE sync_t SET data=:Data WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    81 _LIT( KUpdateMeteringDataString, "UPDATE metering_t SET data=:Data WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    82 
       
    83 _LIT( KSelectLicenseString, "SELECT data FROM license_t WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    84 _LIT( KSelectSecureString, "SELECT data FROM secure_t WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    85 _LIT( KSelectSyncString, "SELECT data FROM sync_t WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    86 _LIT( KSelectMeteringString, "SELECT data FROM metering_t WHERE hashKey = :HashKey AND uniqueKey=:UniqueKey" );
       
    87 
       
    88 _LIT( KSelectLicenseEnumeratorString, "SELECT uniqueKey FROM license_t WHERE hashKey = :HashKey" );
       
    89 _LIT( KSelectSecureEnumeratorString, "SELECT uniqueKey FROM secure_t WHERE hashKey = :HashKey" );
       
    90 _LIT( KSelectSyncEnumeratorString, "SELECT uniqueKey FROM sync_t WHERE hashKey = :HashKey" );
       
    91 _LIT( KSelectMeteringEnumeratorString, "SELECT uniqueKey FROM metering_t WHERE hashKey = :HashKey" );
       
    92 
       
    93 _LIT( KSelectLicenseNameSpaceEnumeratorString, "SELECT hashKey, uniqueKey FROM license_t" );
       
    94 _LIT( KSelectSecureNameSpaceEnumeratorString, "SELECT hashKey, uniqueKey FROM secure_t" );
       
    95 _LIT( KSelectSyncNameSpaceEnumeratorString, "SELECT hashKey, uniqueKey FROM sync_t" );
       
    96 _LIT( KSelectMeteringNameSpaceEnumeratorString, "SELECT hashKey, uniqueKey FROM metering_t" );
       
    97 
       
    98 _LIT( KHashKeyColumn, "hashkey" );
       
    99 _LIT( KHashKeyParameter, ":HashKey" );
       
   100 _LIT( KUniqueKeyColumn, "uniquekey" );
       
   101 _LIT( KUniqueKeyParameter, ":UniqueKey" );
       
   102 _LIT( KDataColumn, "data" );
       
   103 _LIT( KDataParameter, ":Data" );
       
   104 _LIT( KRowIDColumn, "rowID" );
       
   105 _LIT( KRowIDParameter, ":rowID" );
       
   106 
       
   107 _LIT8( KSyncStore, "syncstore" );
       
   108 _LIT8( KMeteringStore, "meteringstore" );
       
   109 _LIT8( KSecureStore, "securestore" );
       
   110 _LIT8( KLicenseStore, "licstore" );
       
   111 _LIT8( KHds, "hds" );
       
   112 
       
   113 const TSecureId KWmDrmServerSecureId( 0x10282F1B );
       
   114 const TInt KMaxTIntBufLength( 10 );
       
   115 const TInt KLicenseSize( 5 * 1024 );
       
   116 const TInt KSixtySeconds( 60 );
       
   117 const TInt KMaxOperationsUntilCommit( 60 );
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // CWmDrmDb::CWmDrmDb
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 CWmDrmDb::CWmDrmDb( CWmDrmServer* aServer ) : CTimer( EPriorityHigh ), 
       
   124     iServer( aServer ),
       
   125     iAmountOperationsWithoutCommit( 0 ),
       
   126     iSqlTransactionOngoing( EFalse ),
       
   127     iSqlTransactionOngoing2( EFalse )   
       
   128     {
       
   129     CActiveScheduler::Add( this );
       
   130     }
       
   131 
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // CWmDrmDb::ConstructL
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 void CWmDrmDb::ConstructL()
       
   138     {
       
   139     LOGFN( "CWmDrmDb::ConstructL" );
       
   140     TDrmScheme drmScheme( EDrmSchemeWmDrm );
       
   141     TChar driveLetter;
       
   142 	TChar systemDriveLetter;
       
   143     
       
   144     CTimer::ConstructL();
       
   145     
       
   146     // Check which drive is configured in the Central Repository Key
       
   147     // for the desired storing location of WM DRM rights.
       
   148 	iWmDrmRightsConfigFound = DrmRightsStoringLocation::CheckDrmRightsStorageDriveL( 
       
   149         iServer->Fs(), drmScheme, driveLetter );
       
   150     
       
   151     // Format the default database path on the system drive and open
       
   152     // the database. If the storing location is not configured, this database
       
   153     // will hold all the four stores, otherwise only the secure and metering
       
   154     // stores.
       
   155 	if ( !iWmDrmRightsConfigFound )
       
   156 		{
       
   157     	iDatabasePath.Format( KDatabaseName, (TUint)driveLetter );
       
   158     	OpenDatabaseL( iDatabasePath, iDatabase, EFalse );
       
   159     	}
       
   160     else 
       
   161         {
       
   162         // Format the database path also on the internal mass drive and 
       
   163         // open both databases. The database on the default system drive
       
   164         // includes metering and secure stores, whereas the store on the
       
   165         // configured drive includes sync and license stores.
       
   166 		iDatabasePath.Format( KDatabaseName, 
       
   167 		    (TUint)iServer->Fs().GetSystemDriveChar() );
       
   168         iDatabasePath2.Format( KDatabaseName2, (TUint)driveLetter );
       
   169         OpenDatabaseL( iDatabasePath, iDatabase, EFalse );
       
   170 		OpenDatabaseL( iDatabasePath2, iDatabase2, ETrue );
       
   171         }               
       
   172     }
       
   173 
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // CWmDrmDb::NewL
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 CWmDrmDb* CWmDrmDb::NewL( CWmDrmServer* aServer )
       
   180     {
       
   181     LOGFN( "CWmDrmDb::NewL" );
       
   182     CWmDrmDb* self( CWmDrmDb::NewLC( aServer ) );
       
   183     CleanupStack::Pop( self );
       
   184     return self;
       
   185     }
       
   186 
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // CWmDrmDb::NewLC
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 CWmDrmDb* CWmDrmDb::NewLC( CWmDrmServer* aServer )
       
   193     {
       
   194     LOGFN( "CWmDrmDb::NewLC" );
       
   195     CWmDrmDb* self( new( ELeave ) CWmDrmDb( aServer ) );
       
   196     CleanupStack::PushL( self );
       
   197     self->ConstructL();
       
   198     return self;
       
   199     }
       
   200 
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // CWmDrmDb::~CWmDrmDb
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 CWmDrmDb::~CWmDrmDb()
       
   207     {
       
   208     LOGFN( "CWmDrmDb::~CWmDrmDb" );
       
   209     
       
   210     TInt error( KErrNone );
       
   211     
       
   212     if ( IsActive() )
       
   213         {
       
   214         Cancel();
       
   215         }
       
   216     
       
   217     // Checks if there are prepared SQL operations that need to be 
       
   218     // committed before the object is destroyed.
       
   219     TRAP( error, CheckDatabaseCommitL( ETrue ) );
       
   220        
       
   221     delete iData;
       
   222     iEnumerateNamespaceStmt.Close();
       
   223     iDatabase.Close();
       
   224     if ( iWmDrmRightsConfigFound )
       
   225 		{
       
   226 		iDatabase2.Close();
       
   227 		}
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 // CWmDrmDb::RunL
       
   232 // ---------------------------------------------------------------------------
       
   233 //
       
   234 void CWmDrmDb::RunL()
       
   235     {
       
   236     LOGFN( "CWmDrmDb::RunL()" );
       
   237     
       
   238     switch ( iStatus.Int() )
       
   239         {
       
   240         case KErrNone:
       
   241             // Normal completition
       
   242             
       
   243         case KErrUnderflow:
       
   244             // Time has already passed.
       
   245             
       
   246         case KErrAbort:
       
   247             // System time changed
       
   248             CheckDatabaseCommitL( ETrue );
       
   249             break;
       
   250 
       
   251         default:
       
   252             // Some other (real) error
       
   253             // Handled in RunError
       
   254             User::Leave( iStatus.Int() );
       
   255         }
       
   256     }
       
   257 
       
   258 // ------------------------------------------------------------------------
       
   259 // CWmDrmDb::RunError
       
   260 // ------------------------------------------------------------------------
       
   261 //
       
   262 #if defined( _DEBUG ) || defined( _LOGGING )
       
   263 TInt CWmDrmDb::RunError( TInt aError )
       
   264 #else
       
   265 TInt CWmDrmDb::RunError( TInt /* aError */ )
       
   266 #endif
       
   267     {
       
   268     LOG2( "CWmDrmDb::RunError %d", aError );
       
   269     // Continue normally and return KErrNone.
       
   270     return KErrNone;    
       
   271     }
       
   272     
       
   273 // ------------------------------------------------------------------------
       
   274 // CDRMConsume:: Activate
       
   275 // ------------------------------------------------------------------------
       
   276 void CWmDrmDb::Activate()
       
   277     {
       
   278     LOGFN( "CWmDrmDb::Activate()" );
       
   279     
       
   280     // Activate one minute timer
       
   281     After( TTimeIntervalMicroSeconds32( KSixtySeconds * 1000000 ) );
       
   282     }
       
   283 
       
   284 // ---------------------------------------------------------------------------
       
   285 // CWmDrmDb::InitStoreL 
       
   286 // ---------------------------------------------------------------------------
       
   287 //
       
   288 void CWmDrmDb::InitStoreL( 
       
   289     const TDesC8& aStore,
       
   290     TBool /*aCreateIfMissing*/ )
       
   291     {
       
   292     LOGFN( "CWmDrmDb::InitStoreL" );
       
   293     if ( aStore.CompareC( KHds ) == 0 )
       
   294         {
       
   295         if ( iWmDrmRightsConfigFound )
       
   296             {
       
   297             CreateDatabaseL( iDatabasePath, iDatabase, EFalse );
       
   298             CreateDatabaseL( iDatabasePath2, iDatabase2, ETrue );
       
   299             }
       
   300         else
       
   301             {
       
   302             CreateDatabaseL( iDatabasePath, iDatabase, EFalse );
       
   303             }
       
   304         }
       
   305     else
       
   306         {
       
   307         User::Leave( KErrNotFound );
       
   308         }
       
   309     }
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // CWmDrmDb::InitNameSpaceL 
       
   313 // ---------------------------------------------------------------------------
       
   314 //    
       
   315 void CWmDrmDb::InitNameSpaceL( 
       
   316     const TDesC8& /*aStore*/,
       
   317     const TDesC8& aNamespace,
       
   318     TBool /*aCreateIfMissing*/ )
       
   319     {
       
   320     LOGFN( "CWmDrmDb::InitializeL" );
       
   321     TWmDrmTableType tableType( TableTypeL( aNamespace ) );
       
   322     if ( ( tableType == EWmDrmLicenseTable ) && iWmDrmRightsConfigFound )
       
   323     		{  
       
   324     		CreateTableL( tableType, iDatabase2 );
       
   325     		}
       
   326     else
       
   327     		{		
       
   328   			CreateTableL( tableType, iDatabase );
       
   329   			}
       
   330     }
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // CWmDrmDb::RemoveStoreL 
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 void CWmDrmDb::RemoveStoreL( const TDesC8& aStore )
       
   337     {
       
   338     LOGFN( "CWmDrmDb::RemoveStoreL" );
       
   339     if ( aStore.CompareC( KHds ) == 0 )
       
   340         {
       
   341         // Delete databases
       
   342         iDatabase.Close();
       
   343         User::LeaveIfError( RSqlDatabase::Delete( iDatabasePath ) );
       
   344         
       
   345         // If WM DRM rights storing configuration is found -> delete also
       
   346         // the database on the internal mass drive.
       
   347         if ( iWmDrmRightsConfigFound )
       
   348             {
       
   349             iDatabase2.Close();
       
   350             User::LeaveIfError( RSqlDatabase::Delete( iDatabasePath2 ) );
       
   351             CreateDatabaseL( iDatabasePath, iDatabase, EFalse );
       
   352             CreateDatabaseL( iDatabasePath2, iDatabase2, ETrue );
       
   353             }
       
   354         else
       
   355             {
       
   356             CreateDatabaseL( iDatabasePath, iDatabase, EFalse );
       
   357             }
       
   358         }
       
   359     else
       
   360         {
       
   361         User::Leave( KErrNotFound );
       
   362         }
       
   363     }
       
   364 
       
   365 // ---------------------------------------------------------------------------
       
   366 // CWmDrmDb::RemoveNameSpaceL 
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 void CWmDrmDb::RemoveNameSpaceL( 
       
   370     const TDesC8& /*aStore*/,
       
   371     const TDesC8& aNamespace )
       
   372     {
       
   373     LOGFN( "CWmDrmDb::RemoveNameSpaceL" );
       
   374     TWmDrmTableType tableType( TableTypeL( aNamespace ) );
       
   375     DeleteTableL( tableType );
       
   376     if ( ( tableType == EWmDrmLicenseTable ) && iWmDrmRightsConfigFound )
       
   377     		{  
       
   378     		CreateTableL( tableType, iDatabase2 );
       
   379     		}
       
   380     else
       
   381     		{			
       
   382   			CreateTableL( tableType, iDatabase );
       
   383   			}
       
   384     }
       
   385 
       
   386 // ---------------------------------------------------------------------------
       
   387 // CWmDrmDb::DeleteLicenseStoreL
       
   388 // ---------------------------------------------------------------------------
       
   389 //
       
   390 void CWmDrmDb::DeleteLicenseStoreL()
       
   391     {
       
   392     LOGFN( "CWmDrmDb::DeleteLicenseStoreL" );
       
   393     
       
   394    	DeleteTableL( EWmDrmLicenseTable );
       
   395     DeleteTableL( EWmDrmSecureTable );
       
   396     DeleteTableL( EWmDrmSyncTable );
       
   397     
       
   398     if ( iWmDrmRightsConfigFound )
       
   399     		{
       
   400     		CreateTableL( EWmDrmLicenseTable, iDatabase2 );
       
   401    			}
       
   402     else
       
   403     		{
       
   404     		CreateTableL( EWmDrmLicenseTable, iDatabase );
       
   405     		}
       
   406     CreateTableL( EWmDrmSecureTable, iDatabase );
       
   407 	CreateTableL( EWmDrmSyncTable, iDatabase );		
       
   408     }
       
   409 
       
   410 // ---------------------------------------------------------------------------
       
   411 // CWmDrmDb::CreateRecordL
       
   412 // ---------------------------------------------------------------------------
       
   413 //
       
   414 void CWmDrmDb::CreateRecordL(   
       
   415     const TDesC8& /*aStore*/,
       
   416     const TDesC8& aNamespace,
       
   417     const TDesC8& aHashKey,
       
   418     const TDesC8& aUniqueKey,
       
   419     const TInt&   aSize )
       
   420     {
       
   421     LOGFN( "CWmDrmDb::CreateRecordL" );
       
   422     TPtrC statementString( NULL, 0 );
       
   423     
       
   424     // Check if we should begin transaction, cache operation or commit
       
   425     // cached operations 
       
   426     CheckDatabaseCommitL( EFalse );
       
   427     
       
   428     // First, find the table where record should be created
       
   429     TWmDrmTableType tableType( TableTypeL( aNamespace ) );
       
   430     
       
   431     // Update the dummy database file at this point.
       
   432     if ( iWmDrmRightsConfigFound && ( tableType == EWmDrmLicenseTable ) )
       
   433         {
       
   434         iServer->DataStore()->UpdateDummyDbFileL( aSize, ETrue );
       
   435         }
       
   436     else 
       
   437     	{
       
   438     	iServer->DataStore()->UpdateDummyDbFileL( aSize, EFalse );
       
   439     	} 	    
       
   440         
       
   441     // bind sql statement to database
       
   442     RSqlStatement stmt;
       
   443     CleanupClosePushL( stmt );
       
   444     
       
   445     //Select correct insert string
       
   446     switch ( tableType )
       
   447         {
       
   448         case EWmDrmLicenseTable:
       
   449             statementString.Set( KInsertLicenseString().Ptr(), 
       
   450                                  KInsertLicenseString().Length() );
       
   451             if ( iWmDrmRightsConfigFound )
       
   452                 {
       
   453                 stmt.PrepareL( iDatabase2, statementString );
       
   454                 }
       
   455             else 
       
   456                 {
       
   457                 stmt.PrepareL( iDatabase, statementString );
       
   458                 }           
       
   459             break;
       
   460         case EWmDrmSecureTable:
       
   461             statementString.Set( KInsertSecureString().Ptr(), 
       
   462                                  KInsertSecureString().Length() );
       
   463             stmt.PrepareL( iDatabase, statementString );                     
       
   464             break;
       
   465         case EWmDrmSyncTable:
       
   466             statementString.Set( KInsertSyncString().Ptr(), 
       
   467                                  KInsertSyncString().Length() );        
       
   468         	stmt.PrepareL( iDatabase, statementString );       
       
   469             break;
       
   470         case EWmDrmMeteringTable:
       
   471             statementString.Set( KInsertMeteringString().Ptr(), 
       
   472                                  KInsertMeteringString().Length() );
       
   473             stmt.PrepareL( iDatabase, statementString );                           
       
   474             break;
       
   475         default:
       
   476             User::Leave( KErrArgument );
       
   477             break;
       
   478         }
       
   479 
       
   480     // Get index of hash key parameter
       
   481     TInt hashKeyIndex( stmt.ParameterIndex( KHashKeyParameter ) );
       
   482     User::LeaveIfError( hashKeyIndex );
       
   483     
       
   484     // Replace hash key parameter
       
   485     User::LeaveIfError( stmt.BindBinary( hashKeyIndex, aHashKey ) );
       
   486     
       
   487     // Get index of unique key parameter
       
   488     TInt uniqueKeyIndex( stmt.ParameterIndex( KUniqueKeyParameter ) );
       
   489     User::LeaveIfError( uniqueKeyIndex );
       
   490     
       
   491     // Replace unique key parameter
       
   492     User::LeaveIfError( stmt.BindBinary( uniqueKeyIndex, aUniqueKey ) );
       
   493     
       
   494     TInt err( KErrNone );
       
   495     // Execute SQL statement
       
   496     err = stmt.Exec();
       
   497     if ( err == KSqlErrConstraint )
       
   498         {
       
   499         User::Leave( KErrAlreadyExists );
       
   500         }
       
   501     User::LeaveIfError( err );
       
   502     // Close SQL statement
       
   503     CleanupStack::PopAndDestroy( &stmt );
       
   504     
       
   505     // Update the dummy database file at this point.
       
   506     if ( iWmDrmRightsConfigFound && ( tableType == EWmDrmLicenseTable ) )
       
   507         {
       
   508         iServer->DataStore()->UpdateDummyDbFileL( 0, ETrue );
       
   509         }
       
   510     else 
       
   511     	{    
       
   512     	iServer->DataStore()->UpdateDummyDbFileL( 0, EFalse );
       
   513     	}
       
   514     		
       
   515     iAmountOperationsWithoutCommit++;	
       
   516     }
       
   517 
       
   518 // ---------------------------------------------------------------------------
       
   519 // CWmDrmDb::ReadRecordL
       
   520 // ---------------------------------------------------------------------------
       
   521 //
       
   522 void CWmDrmDb::ReadRecordL(  
       
   523     const TDesC8& /*aStore*/,
       
   524     const TDesC8& aNamespace,
       
   525     const TDesC8& aHashKey,
       
   526     const TDesC8& aUniqueKey )
       
   527     {
       
   528     LOGFN( "CWmDrmDb::ReadRecordL" );
       
   529     TPtrC statementString( NULL, 0 );
       
   530     // First, find from which table record should be read
       
   531     TWmDrmTableType tableType( TableTypeL( aNamespace ) );
       
   532     
       
   533     // bind sql statement to database
       
   534     RSqlStatement stmt;
       
   535     CleanupClosePushL( stmt );
       
   536     
       
   537     // Select correct insert string
       
   538     switch ( tableType )
       
   539         {
       
   540         case EWmDrmLicenseTable:
       
   541             statementString.Set( KSelectLicenseString().Ptr(), 
       
   542                                  KSelectLicenseString().Length() );
       
   543             if ( iWmDrmRightsConfigFound )
       
   544                 {
       
   545                 stmt.PrepareL( iDatabase2, statementString );
       
   546                 }
       
   547             else 
       
   548                 {
       
   549                 stmt.PrepareL( iDatabase, statementString );
       
   550                 }                                           
       
   551             break;
       
   552         case EWmDrmSecureTable:
       
   553             statementString.Set( KSelectSecureString().Ptr(), 
       
   554                                  KSelectSecureString().Length() );
       
   555             stmt.PrepareL( iDatabase, statementString );
       
   556             break;
       
   557         case EWmDrmSyncTable:
       
   558             statementString.Set( KSelectSyncString().Ptr(), 
       
   559                                  KSelectSyncString().Length() );     
       
   560             stmt.PrepareL( iDatabase, statementString );                      
       
   561             break;
       
   562         case EWmDrmMeteringTable:
       
   563             statementString.Set( KSelectMeteringString().Ptr(), 
       
   564                                  KSelectMeteringString().Length() );
       
   565             stmt.PrepareL( iDatabase, statementString );
       
   566             break;
       
   567         default:
       
   568             User::Leave( KErrArgument );
       
   569             break;
       
   570         }
       
   571     
       
   572     // Get index of hash key parameter
       
   573     TInt hashKeyIndex( stmt.ParameterIndex( KHashKeyParameter ) );
       
   574     User::LeaveIfError( hashKeyIndex );
       
   575 
       
   576     // Replace key parameter
       
   577     User::LeaveIfError( stmt.BindBinary( hashKeyIndex, aHashKey ) );
       
   578 
       
   579     TInt uniqueKeyIndex( stmt.ParameterIndex( KUniqueKeyParameter ) );
       
   580     User::LeaveIfError( uniqueKeyIndex );
       
   581     
       
   582     // Replace unique key parameter
       
   583     User::LeaveIfError( stmt.BindBinary( uniqueKeyIndex, aUniqueKey ) );
       
   584     
       
   585     // Read data
       
   586     TInt dataColumnIndex( stmt.ColumnIndex( KDataColumn ) );
       
   587     User::LeaveIfError( dataColumnIndex );
       
   588 
       
   589     if ( stmt.Next() == KSqlAtRow )
       
   590         {
       
   591         // If there is previously cached data, delete it
       
   592         delete iData;
       
   593         iData = NULL;
       
   594         
       
   595         TPtrC8 data( NULL, 0 );
       
   596         User::LeaveIfError( stmt.ColumnBinary( dataColumnIndex, data ) );
       
   597         iData = data.AllocL();
       
   598         }
       
   599     else 
       
   600         {
       
   601         User::Leave( KErrNotFound );
       
   602         }
       
   603 
       
   604     // Close SQL statement
       
   605     CleanupStack::PopAndDestroy( &stmt );
       
   606     }
       
   607 
       
   608 // ---------------------------------------------------------------------------
       
   609 // CWmDrmDb::GetDataSizeL 
       
   610 // ---------------------------------------------------------------------------
       
   611 //
       
   612 void CWmDrmDb::GetDataSizeL( TInt& aDataSize )
       
   613     {
       
   614     LOGFN( "CWmDrmDb::GetDataSizeL" );
       
   615     User::LeaveIfNull( iData );
       
   616     aDataSize = iData->Size();
       
   617     }
       
   618 
       
   619 // ---------------------------------------------------------------------------
       
   620 // CWmDrmDb::DeleteData 
       
   621 // ---------------------------------------------------------------------------
       
   622 //
       
   623 void CWmDrmDb::DeleteData()
       
   624     {
       
   625     LOGFN( "CWmDrmDb::DeleteData" );
       
   626     delete iData;
       
   627     iData = NULL;
       
   628     }
       
   629 
       
   630 // ---------------------------------------------------------------------------
       
   631 // CWmDrmDb::DeleteRecordL
       
   632 // ---------------------------------------------------------------------------
       
   633 //
       
   634 void CWmDrmDb::DeleteRecordL(   
       
   635     const TDesC8& /*aStore*/,
       
   636     const TDesC8& aNamespace,
       
   637     const TDesC8& aHashKey,
       
   638     const TDesC8& aUniqueKey )
       
   639     {
       
   640     LOGFN( "CWmDrmDb::DeleteRecordL" );
       
   641     TPtrC statementString( NULL, 0 );
       
   642     
       
   643     // Check if we should begin transaction, cache operation or commit
       
   644     // cached operations 
       
   645     CheckDatabaseCommitL( EFalse );
       
   646     
       
   647     // First, find the table where record should be created
       
   648     TWmDrmTableType tableType( TableTypeL( aNamespace ) );
       
   649     
       
   650     // bind sql statement to database
       
   651     RSqlStatement stmt;
       
   652     CleanupClosePushL( stmt );
       
   653     
       
   654     //Select correct insert string
       
   655     switch ( tableType )
       
   656         {
       
   657         case EWmDrmLicenseTable:
       
   658             statementString.Set( KDeleteLicenseString().Ptr(), 
       
   659                                  KDeleteLicenseString().Length() );
       
   660             if ( iWmDrmRightsConfigFound )
       
   661                 {
       
   662                 stmt.PrepareL( iDatabase2, statementString );
       
   663                 }
       
   664             else 
       
   665                 {
       
   666                 stmt.PrepareL( iDatabase, statementString );
       
   667                 }                      
       
   668             break;
       
   669         case EWmDrmSecureTable:
       
   670             statementString.Set( KDeleteSecureString().Ptr(), 
       
   671                                  KDeleteSecureString().Length() );
       
   672             stmt.PrepareL( iDatabase, statementString );                     
       
   673             break;
       
   674         case EWmDrmSyncTable:
       
   675             statementString.Set( KDeleteSyncString().Ptr(), 
       
   676                                  KDeleteSyncString().Length() );
       
   677             stmt.PrepareL( iDatabase, statementString );
       
   678             break;
       
   679         case EWmDrmMeteringTable:
       
   680             statementString.Set( KDeleteMeteringString().Ptr(), 
       
   681                                  KDeleteMeteringString().Length() );
       
   682             stmt.PrepareL( iDatabase, statementString );                     
       
   683             break;
       
   684         default:
       
   685             User::Leave( KErrArgument );
       
   686             break;
       
   687         }
       
   688 
       
   689     // Get index of hash key parameter
       
   690     TInt hashKeyIndex( stmt.ParameterIndex( KHashKeyParameter ) );
       
   691     User::LeaveIfError( hashKeyIndex );
       
   692     
       
   693     // Replace hash key parameter
       
   694     User::LeaveIfError( stmt.BindBinary( hashKeyIndex, aHashKey ) );
       
   695     
       
   696     // Get index of unique key parameter
       
   697     TInt uniqueKeyIndex( stmt.ParameterIndex( KUniqueKeyParameter ) );
       
   698     User::LeaveIfError( uniqueKeyIndex );
       
   699     
       
   700     // Replace unique key parameter
       
   701     User::LeaveIfError( stmt.BindBinary( uniqueKeyIndex, aUniqueKey ) );
       
   702     
       
   703     // Execute SQL statement
       
   704     User::LeaveIfError( stmt.Exec() );
       
   705     
       
   706     // Update dummy database file at this point.
       
   707     if ( iWmDrmRightsConfigFound && ( tableType == EWmDrmLicenseTable ) )
       
   708         {
       
   709         iServer->DataStore()->UpdateDummyDbFileL( 0, ETrue );
       
   710         }
       
   711     else 
       
   712     	{
       
   713     	iServer->DataStore()->UpdateDummyDbFileL( 0, EFalse );
       
   714     	} 	  
       
   715     
       
   716     // Close SQL statement
       
   717     CleanupStack::PopAndDestroy( &stmt );
       
   718     
       
   719     iAmountOperationsWithoutCommit++;
       
   720     }
       
   721 
       
   722 // ---------------------------------------------------------------------------
       
   723 // CWmDrmDb::ReadDataL
       
   724 // ---------------------------------------------------------------------------
       
   725 //
       
   726 void CWmDrmDb::ReadDataL( TDes8& aData )
       
   727     {
       
   728     LOGFN( "CWmDrmDb::ReadDataL" );
       
   729     User::LeaveIfNull( iData );
       
   730     aData.Copy( *iData );
       
   731     }
       
   732 
       
   733 // ---------------------------------------------------------------------------
       
   734 // CWmDrmDb::WriteDataL
       
   735 // ---------------------------------------------------------------------------
       
   736 //
       
   737 void CWmDrmDb::WriteDataL(   
       
   738     const TDesC8& /*aStore*/,
       
   739     const TDesC8& aNamespace,
       
   740     const TDesC8& aHashKey,
       
   741     const TDesC8& aUniqueKey, 
       
   742     TDesC8& aData )
       
   743     {
       
   744     LOGFN( "CWmDrmDb::WriteDataL" );
       
   745     TPtrC statementString( NULL, 0 );
       
   746     
       
   747     // Check if we should begin transaction, cache operation or commit
       
   748     // cached operations 
       
   749     CheckDatabaseCommitL( EFalse );
       
   750     
       
   751     // First, find from which table record should be read
       
   752     TWmDrmTableType tableType( TableTypeL( aNamespace ) );
       
   753     
       
   754     // Update the dummy database file at this point.
       
   755     if ( iWmDrmRightsConfigFound && ( tableType == EWmDrmLicenseTable ) )
       
   756         {
       
   757         iServer->DataStore()->UpdateDummyDbFileL( aData.Size(), ETrue );
       
   758         }
       
   759     else 
       
   760     	{
       
   761     	iServer->DataStore()->UpdateDummyDbFileL( aData.Size(), EFalse );
       
   762     	} 	  
       
   763     
       
   764     // bind sql statement to database
       
   765     RSqlStatement stmt;
       
   766     CleanupClosePushL( stmt );
       
   767     
       
   768     //Select correct insert string
       
   769     switch ( tableType )
       
   770         {
       
   771         case EWmDrmLicenseTable:
       
   772             statementString.Set( KUpdateLicenseDataString().Ptr(), 
       
   773                                  KUpdateLicenseDataString().Length() );
       
   774             if ( iWmDrmRightsConfigFound )
       
   775                 {
       
   776                 stmt.PrepareL( iDatabase2, statementString );
       
   777                 }
       
   778             else 
       
   779                 {
       
   780                 stmt.PrepareL( iDatabase, statementString );
       
   781                 }                                           
       
   782             break;
       
   783         case EWmDrmSecureTable:
       
   784             statementString.Set( KUpdateSecureDataString().Ptr(), 
       
   785                                  KUpdateSecureDataString().Length() );
       
   786             stmt.PrepareL( iDatabase, statementString );                     
       
   787             break;
       
   788         case EWmDrmSyncTable:
       
   789             statementString.Set( KUpdateSyncDataString().Ptr(), 
       
   790                                  KUpdateSyncDataString().Length() );
       
   791             stmt.PrepareL( iDatabase, statementString );                                          
       
   792             break;
       
   793         case EWmDrmMeteringTable:
       
   794             statementString.Set( KUpdateMeteringDataString().Ptr(), 
       
   795                                  KUpdateMeteringDataString().Length() );
       
   796             stmt.PrepareL( iDatabase, statementString );
       
   797             break;
       
   798         default:
       
   799             User::Leave( KErrArgument );
       
   800             break;
       
   801         }
       
   802     
       
   803     // Get index of hash key parameter
       
   804     TInt hashKeyIndex( stmt.ParameterIndex( KHashKeyParameter ) );
       
   805     User::LeaveIfError( hashKeyIndex );
       
   806 
       
   807     // Replace key parameter
       
   808     User::LeaveIfError( stmt.BindBinary( hashKeyIndex, aHashKey ) );
       
   809 
       
   810     // Get index of unique key parameter
       
   811     TInt uniqueKeyIndex( stmt.ParameterIndex( KUniqueKeyParameter ) );
       
   812     User::LeaveIfError( uniqueKeyIndex );
       
   813 
       
   814     // Replace key parameter
       
   815     User::LeaveIfError( stmt.BindBinary( uniqueKeyIndex, aUniqueKey ) );
       
   816 
       
   817     // Get index of value parameter
       
   818     TInt dataIndex( stmt.ParameterIndex( KDataParameter ) );
       
   819     User::LeaveIfError( dataIndex );
       
   820 
       
   821     // Replace value parameter
       
   822     User::LeaveIfError( stmt.BindBinary( dataIndex, aData ) );
       
   823 
       
   824     // Execute SQL statement
       
   825     LOG1( "CWmDrmDb::WriteDataL exec" );
       
   826     User::LeaveIfError( stmt.Exec() );
       
   827     LOG1( "CWmDrmDb::WriteDataL exec ok" );
       
   828     
       
   829     // Close SQL statement
       
   830     CleanupStack::PopAndDestroy( &stmt );
       
   831     
       
   832     iAmountOperationsWithoutCommit++;
       
   833     }
       
   834 
       
   835 // ---------------------------------------------------------------------------
       
   836 // CWmDrmDb::EnumerateDataL
       
   837 // ---------------------------------------------------------------------------
       
   838 //
       
   839 void CWmDrmDb::EnumerateDataL(  
       
   840     const TDesC8& /*aStore*/,
       
   841     const TDesC8& aNamespace,
       
   842     const TDesC8& aHashKey,
       
   843     RPointerArray<HBufC8>& aUniqueKeyEntries )
       
   844     {
       
   845     LOGFN( "CWmDrmDb::EnumerateDataL" );
       
   846     TPtrC statementString( NULL, 0 );
       
   847     // First, find from which table record should be read
       
   848     TWmDrmTableType tableType( TableTypeL( aNamespace ) );
       
   849     
       
   850     // bind sql statement to database
       
   851     RSqlStatement stmt;
       
   852     CleanupClosePushL( stmt );
       
   853     
       
   854     //Select correct insert string
       
   855     switch ( tableType )
       
   856         {
       
   857         case EWmDrmLicenseTable:
       
   858             statementString.Set( KSelectLicenseEnumeratorString().Ptr(), 
       
   859                                  KSelectLicenseEnumeratorString().Length() );
       
   860             if ( iWmDrmRightsConfigFound )
       
   861                 {
       
   862                 stmt.PrepareL( iDatabase2, statementString );
       
   863                 }
       
   864             else 
       
   865                 {
       
   866                 stmt.PrepareL( iDatabase, statementString );
       
   867                 }                                                
       
   868             break;
       
   869         case EWmDrmSecureTable:
       
   870             statementString.Set( KSelectSecureEnumeratorString().Ptr(), 
       
   871                                  KSelectSecureEnumeratorString().Length() );
       
   872             stmt.PrepareL( iDatabase, statementString );                     
       
   873             break;
       
   874         case EWmDrmSyncTable:
       
   875             statementString.Set( KSelectSyncEnumeratorString().Ptr(), 
       
   876                                  KSelectSyncEnumeratorString().Length() );
       
   877             stmt.PrepareL( iDatabase, statementString );                       
       
   878             break;
       
   879         case EWmDrmMeteringTable:
       
   880             statementString.Set( KSelectMeteringEnumeratorString().Ptr(), 
       
   881                                  KSelectMeteringEnumeratorString().Length() );
       
   882             stmt.PrepareL( iDatabase, statementString );
       
   883             break;
       
   884         default:
       
   885             User::Leave( KErrArgument );
       
   886             break;
       
   887         }
       
   888 
       
   889     TInt hashKeyIndex( stmt.ParameterIndex( KHashKeyParameter ) );
       
   890     User::LeaveIfError( hashKeyIndex );
       
   891     
       
   892     // Replace hash key parameter
       
   893     User::LeaveIfError( stmt.BindBinary( hashKeyIndex, aHashKey ) );
       
   894 
       
   895     TInt uniqueKeyColumnIndex( stmt.ColumnIndex( KUniqueKeyColumn ) );
       
   896     User::LeaveIfError( uniqueKeyColumnIndex );
       
   897 
       
   898     TInt err( KErrNone );
       
   899     TInt index( 0 );
       
   900     TPtrC8 uniqueKeyPtr( NULL, 0 );
       
   901     HBufC8* uniquekey( NULL );
       
   902     while( ( err = stmt.Next() ) == KSqlAtRow )
       
   903         {
       
   904         index++;
       
   905         User::LeaveIfError( 
       
   906             stmt.ColumnBinary( uniqueKeyColumnIndex, uniqueKeyPtr ) );
       
   907         uniquekey = uniqueKeyPtr.AllocLC();
       
   908         aUniqueKeyEntries.AppendL( uniquekey );
       
   909         CleanupStack::Pop( uniquekey );
       
   910         }
       
   911     if ( err != KSqlAtEnd )
       
   912         {
       
   913         User::Leave( err );
       
   914         }
       
   915     if ( index == 0 )
       
   916         {
       
   917         User::Leave( KErrNotFound );
       
   918         }
       
   919 
       
   920     // Close SQL statement
       
   921     CleanupStack::PopAndDestroy( &stmt );
       
   922     }
       
   923     
       
   924 // ---------------------------------------------------------------------------
       
   925 // CWmDrmDb::EnumerateNameSpaceStartL
       
   926 // ---------------------------------------------------------------------------
       
   927 //
       
   928 void CWmDrmDb::EnumerateNameSpaceStartL(  
       
   929     const TDesC8& /*aStore*/,
       
   930     const TDesC8& aNamespace )
       
   931     {
       
   932     LOGFN( "CWmDrmDb::EnumerateNameSpaceStartL" );
       
   933     TPtrC statementString( NULL, 0 );
       
   934     // First, find from which table record should be read
       
   935     TWmDrmTableType tableType( TableTypeL( aNamespace ) );
       
   936     
       
   937     //Select correct insert string
       
   938     switch ( tableType )
       
   939         {
       
   940         case EWmDrmLicenseTable:
       
   941             statementString.Set( KSelectLicenseNameSpaceEnumeratorString().Ptr(), 
       
   942                                  KSelectLicenseNameSpaceEnumeratorString().Length() );
       
   943             // bind sql statement to database                     
       
   944             iEnumerateNamespaceStmt.Close();
       
   945             if ( iWmDrmRightsConfigFound )
       
   946                 {
       
   947                 iEnumerateNamespaceStmt.PrepareL( iDatabase2, statementString );
       
   948                 }
       
   949             else 
       
   950                 {
       
   951                 iEnumerateNamespaceStmt.PrepareL( iDatabase, statementString );
       
   952                 }                     
       
   953             break;
       
   954         case EWmDrmSecureTable:
       
   955             statementString.Set( KSelectSecureNameSpaceEnumeratorString().Ptr(), 
       
   956                                  KSelectSecureNameSpaceEnumeratorString().Length() );                     
       
   957             iEnumerateNamespaceStmt.Close();
       
   958             iEnumerateNamespaceStmt.PrepareL( iDatabase, statementString );                     
       
   959             break;
       
   960         case EWmDrmSyncTable:
       
   961             statementString.Set( KSelectSyncNameSpaceEnumeratorString().Ptr(), 
       
   962                                  KSelectSyncNameSpaceEnumeratorString().Length() );                     
       
   963             iEnumerateNamespaceStmt.Close();                     
       
   964             iEnumerateNamespaceStmt.PrepareL( iDatabase, statementString );
       
   965             break;
       
   966         case EWmDrmMeteringTable:
       
   967             statementString.Set( KSelectMeteringNameSpaceEnumeratorString().Ptr(), 
       
   968                                  KSelectMeteringNameSpaceEnumeratorString().Length() );                     
       
   969             iEnumerateNamespaceStmt.Close();                     
       
   970             iEnumerateNamespaceStmt.PrepareL( iDatabase, statementString );                     
       
   971             break;
       
   972         default:
       
   973             User::Leave( KErrArgument );
       
   974             break;
       
   975         }
       
   976     
       
   977     iEnumerateNamespaceHashKeyColumnIndex = 
       
   978         iEnumerateNamespaceStmt.ColumnIndex( KHashKeyColumn );
       
   979     User::LeaveIfError( iEnumerateNamespaceHashKeyColumnIndex );
       
   980     
       
   981     iEnumerateNamespaceUniqueKeyColumnIndex = 
       
   982         iEnumerateNamespaceStmt.ColumnIndex( KUniqueKeyColumn );
       
   983     User::LeaveIfError( iEnumerateNamespaceUniqueKeyColumnIndex );
       
   984     iEnumerateNamespaceStarted = ETrue;
       
   985     }
       
   986 
       
   987 // ---------------------------------------------------------------------------
       
   988 // CWmDrmDb::EnumerateNameSpaceNextL
       
   989 // ---------------------------------------------------------------------------
       
   990 //    
       
   991 void CWmDrmDb::EnumerateNameSpaceNextL(  
       
   992     TBuf8<KWmDrmIdSize>& aHashKey,
       
   993     TBuf8<KWmDrmIdSize>& aUniqueKey )
       
   994     {
       
   995     LOGFN( "CWmDrmDb::EnumerateNameSpaceNextL" );
       
   996     if ( !iEnumerateNamespaceStarted )
       
   997         {
       
   998         User::Leave( KErrNotReady );
       
   999         }
       
  1000     if ( iEnumerateNamespaceStmt.Next() == KSqlAtRow )
       
  1001         {
       
  1002         TPtrC8 hashKey( NULL, 0 );
       
  1003         TPtrC8 uniqueKey( NULL, 0 );
       
  1004         User::LeaveIfError( 
       
  1005             iEnumerateNamespaceStmt.ColumnBinary( 
       
  1006                 iEnumerateNamespaceHashKeyColumnIndex, hashKey ) );
       
  1007         aHashKey.Copy( hashKey );
       
  1008         User::LeaveIfError( 
       
  1009             iEnumerateNamespaceStmt.ColumnBinary( 
       
  1010                 iEnumerateNamespaceUniqueKeyColumnIndex, uniqueKey ) );
       
  1011         aUniqueKey.Copy( uniqueKey );
       
  1012         }
       
  1013     else 
       
  1014         {
       
  1015         iEnumerateNamespaceStmt.Close();
       
  1016         iEnumerateNamespaceStarted = EFalse;
       
  1017         User::Leave( KErrNotFound );
       
  1018         }
       
  1019     }
       
  1020 
       
  1021 // ---------------------------------------------------------------------------
       
  1022 // CWmDrmDb::DataBaseSize
       
  1023 // ---------------------------------------------------------------------------
       
  1024 //
       
  1025 TInt CWmDrmDb::DataBaseSize( TBool aConfiguredDrive )
       
  1026     {
       
  1027     // Find the database size for the database which is located in the
       
  1028     // configured drive
       
  1029     if ( aConfiguredDrive )
       
  1030         {
       
  1031         return iDatabase2.Size();
       
  1032         }
       
  1033     else
       
  1034         {
       
  1035         return iDatabase.Size();
       
  1036         }
       
  1037     }
       
  1038 
       
  1039 // ---------------------------------------------------------------------------
       
  1040 // CWmDrmDb::CreateDatabaseL
       
  1041 // ---------------------------------------------------------------------------
       
  1042 //
       
  1043 void CWmDrmDb::CreateDatabaseL( TFileName& aFileNamePath, RSqlDatabase&
       
  1044     aSqlDatabase, TBool aConfiguredDrive )
       
  1045     {
       
  1046     LOGFN( "CWmDrmDb::CreateDatabaseL" );
       
  1047     TSecurityPolicy defaultPolicy( KWmDrmServerSecureId );
       
  1048     RSqlSecurityPolicy securityPolicy;
       
  1049     securityPolicy.CreateL( defaultPolicy );
       
  1050     CleanupClosePushL( securityPolicy );
       
  1051  
       
  1052     aSqlDatabase.CreateL( aFileNamePath, securityPolicy, &KDatabaseConfig );
       
  1053  
       
  1054     if ( aConfiguredDrive ) 
       
  1055         {
       
  1056         // Create the license store to the configured drive
       
  1057         CreateTableL( EWmDrmLicenseTable, aSqlDatabase );   
       
  1058         }
       
  1059     else 
       
  1060         {
       
  1061         // Secure, metering and sync stores are located on the system drive in
       
  1062         // configured and non-configured WM DRM rights storing location case.
       
  1063         CreateTableL( EWmDrmSecureTable, aSqlDatabase );
       
  1064         CreateTableL( EWmDrmMeteringTable, aSqlDatabase );
       
  1065 		CreateTableL( EWmDrmSyncTable, aSqlDatabase );
       
  1066 		
       
  1067         if ( !iWmDrmRightsConfigFound )
       
  1068             {
       
  1069             CreateTableL( EWmDrmLicenseTable, aSqlDatabase );
       
  1070             }
       
  1071         }
       
  1072     
       
  1073     CleanupStack::PopAndDestroy( &securityPolicy );
       
  1074     }
       
  1075 
       
  1076 // ---------------------------------------------------------------------------
       
  1077 // CWmDrmDb::CreateTableL
       
  1078 // ---------------------------------------------------------------------------
       
  1079 //
       
  1080 void CWmDrmDb::CreateTableL( TWmDrmTableType aTableType, RSqlDatabase& aDatabase )
       
  1081     {
       
  1082     LOGFN( "CWmDrmDb::CreateTableL" );
       
  1083     switch ( aTableType )
       
  1084         {
       
  1085         case EWmDrmLicenseTable:
       
  1086             User::LeaveIfError( aDatabase.Exec( KCreateLicenseTableString ) );
       
  1087             break;
       
  1088         case EWmDrmSecureTable:
       
  1089             User::LeaveIfError( aDatabase.Exec( KCreateSecureTableString ) );
       
  1090             break;
       
  1091         case EWmDrmSyncTable:
       
  1092             User::LeaveIfError( aDatabase.Exec( KCreateSyncTableString ) );
       
  1093             break;
       
  1094         case EWmDrmMeteringTable:
       
  1095             User::LeaveIfError( aDatabase.Exec( KCreateMeteringTableString ) );
       
  1096             break;
       
  1097         default:
       
  1098             User::Leave( KErrArgument );
       
  1099             break;
       
  1100         }
       
  1101     }
       
  1102 
       
  1103 // ---------------------------------------------------------------------------
       
  1104 // CWmDrmDb::DeleteTableL
       
  1105 // ---------------------------------------------------------------------------
       
  1106 //
       
  1107 void CWmDrmDb::DeleteTableL( TWmDrmTableType aTableType )
       
  1108     {
       
  1109     LOGFN( "CWmDrmDb::DeleteTableL" );
       
  1110     TInt count( 1 );
       
  1111     TInt64 freeSpace( 0 );
       
  1112     TInt databaseSize( 0 );
       
  1113     
       
  1114     // Commit the cached database operations before deleting the table
       
  1115     CheckDatabaseCommitL( ETrue );
       
  1116         
       
  1117     // Check the free space from the system drive or internal mass drive
       
  1118     // depending on the WM DRM rights storing location configuration and
       
  1119     // the table type of the database
       
  1120     if ( iWmDrmRightsConfigFound && ( aTableType == EWmDrmLicenseTable ) )
       
  1121         {
       
  1122         freeSpace = iServer->FreeSpaceL( ETrue );
       
  1123         databaseSize = DataBaseSize( ETrue );
       
  1124         User::LeaveIfError( databaseSize );
       
  1125         
       
  1126         //Just to make sure we don't have reserved space in usage
       
  1127         iDatabase2.ReleaseReserveAccess();
       
  1128         }
       
  1129     else 
       
  1130         {
       
  1131         freeSpace = iServer->FreeSpaceL( EFalse );
       
  1132         databaseSize = DataBaseSize( EFalse );
       
  1133         User::LeaveIfError( databaseSize );
       
  1134         iDatabase.ReleaseReserveAccess();
       
  1135         }
       
  1136     
       
  1137     //If size of the database < freespace or there aren't anymore any 
       
  1138     //records in the table, then drop whole table.
       
  1139     //Otherwise loop and delete as many records as possible with
       
  1140     //current amount of free space.
       
  1141     while ( databaseSize > freeSpace && count > 0 )
       
  1142         {
       
  1143         
       
  1144         //Get reserved space to usage
       
  1145         if ( iWmDrmRightsConfigFound && ( aTableType == EWmDrmLicenseTable ) )
       
  1146             {
       
  1147             User::LeaveIfError( iDatabase2.GetReserveAccess() );
       
  1148             }
       
  1149         else
       
  1150             {
       
  1151             User::LeaveIfError( iDatabase.GetReserveAccess() );
       
  1152             } 
       
  1153         
       
  1154         //Estimate how many records can be deleted with current amount of free space
       
  1155         TInt number( 0 );
       
  1156         //Very low free space, OOD-notes shown, target to get off as soon as possible
       
  1157         //without risking deletion success
       
  1158         if ( freeSpace <= KDRIVECWARNINGTHRESHOLD )
       
  1159             {
       
  1160             LOG1( "Disk space under warning level" );
       
  1161             if ( freeSpace <= KDRIVECCRITICALTHRESHOLD )
       
  1162                 {
       
  1163                 LOG1( "Disk space under critical level" );
       
  1164                 //Use reserved space
       
  1165                 number = 5;
       
  1166                 }
       
  1167             else
       
  1168                 {
       
  1169                 //Use reserved space and space over critical level
       
  1170                 TInt spaceOverCritical( freeSpace - KDRIVECCRITICALTHRESHOLD );
       
  1171                 LOG2( "spaceOverCritical: %d", spaceOverCritical );
       
  1172                 number = spaceOverCritical / KLicenseSize + 5;
       
  1173                 }
       
  1174             }
       
  1175         //Disk is still almost full, but we don't wan't to get any OOD-notes anymore
       
  1176         else if ( freeSpace <= ( 2 * KDRIVECWARNINGTHRESHOLD ) )
       
  1177             {
       
  1178             LOG1( "Disk almost full, delete with caution and try to avoid OOD-notes" );
       
  1179             TInt spaceOverWarning( freeSpace - KDRIVECWARNINGTHRESHOLD );
       
  1180             LOG2( "spaceOverWarning: %d", spaceOverWarning );
       
  1181             //Use 1/2 space over warning level, OOD-note might still be shown
       
  1182             number = ( spaceOverWarning / 2 ) / KLicenseSize;
       
  1183             }
       
  1184         //Now there is enough space to delete larger number of records
       
  1185         //Make absolutely sure we don't anymore get OOD-notes
       
  1186         else
       
  1187             {
       
  1188             TInt spaceOver2Warnings( freeSpace - 2 * KDRIVECWARNINGTHRESHOLD );
       
  1189             LOG2( "spaceOver2Warnings: %d", spaceOver2Warnings );
       
  1190             //Use space over 2 * warning level, no OOD-notes
       
  1191             number = spaceOver2Warnings / KLicenseSize;
       
  1192             }
       
  1193         if ( number <= 0 )
       
  1194             {
       
  1195             number = 5;
       
  1196             }
       
  1197         
       
  1198         LOG2( "number: %d", number );
       
  1199         
       
  1200         //Array to hold rowIDS of to be deleted records
       
  1201         RArray<TInt64> array;
       
  1202         CleanupClosePushL( array );
       
  1203         
       
  1204         //Get number of rowIDs to array 
       
  1205         SelectNRecordsWithRowIdL( aTableType, number, array );
       
  1206         //Get amount of selected rowIds
       
  1207         count = array.Count();
       
  1208         
       
  1209         //Begin transaction
       
  1210         if ( iWmDrmRightsConfigFound && ( aTableType == EWmDrmLicenseTable ) )
       
  1211             {
       
  1212             User::LeaveIfError( iDatabase2.Exec( KBeginTransaction ) );
       
  1213             iSqlTransactionOngoing2 = ETrue;
       
  1214             //Execute delete with rowIds
       
  1215             DeleteRecordsWithRowIdsL( aTableType, array );
       
  1216             //Commit transaction
       
  1217             User::LeaveIfError( iDatabase2.Exec( KCommitTransaction ) );
       
  1218             iSqlTransactionOngoing2 = EFalse;
       
  1219             }
       
  1220         else 
       
  1221             {
       
  1222             User::LeaveIfError( iDatabase.Exec( KBeginTransaction ) );
       
  1223             iSqlTransactionOngoing = ETrue;
       
  1224             DeleteRecordsWithRowIdsL( aTableType, array );
       
  1225             User::LeaveIfError( iDatabase.Exec( KCommitTransaction ) );
       
  1226             iSqlTransactionOngoing = EFalse;
       
  1227             }
       
  1228         
       
  1229         CleanupStack::PopAndDestroy( &array );
       
  1230         
       
  1231         //Prepare for the next round
       
  1232         if ( iWmDrmRightsConfigFound && ( aTableType == EWmDrmLicenseTable ) )
       
  1233         	{
       
  1234         	freeSpace = iServer->FreeSpaceL( ETrue );
       
  1235         	databaseSize = DataBaseSize( ETrue );
       
  1236         	User::LeaveIfError( databaseSize );
       
  1237         
       
  1238         	//Release access to reserved space
       
  1239         	iDatabase2.ReleaseReserveAccess();
       
  1240         	}
       
  1241     	else 
       
  1242         	{
       
  1243         	freeSpace = iServer->FreeSpaceL( EFalse );
       
  1244         	databaseSize = DataBaseSize( EFalse );
       
  1245         	User::LeaveIfError( databaseSize );
       
  1246         	iDatabase.ReleaseReserveAccess();
       
  1247         	}
       
  1248         }		
       
  1249     DropTableL( aTableType );
       
  1250     }
       
  1251 
       
  1252 // ---------------------------------------------------------------------------
       
  1253 // CWmDrmDb::SelectNRecordsWithRowIdL
       
  1254 // ---------------------------------------------------------------------------
       
  1255 //
       
  1256 void CWmDrmDb::SelectNRecordsWithRowIdL(
       
  1257     TWmDrmTableType aTableType,
       
  1258     TInt aNumber,
       
  1259     RArray<TInt64>& aArray )
       
  1260     {
       
  1261     LOGFN( "CWmDrmDb::SelectNRecordsWithRowIdL" );
       
  1262     TPtr statementPtr( NULL, 0 );
       
  1263     HBufC* statement( NULL );
       
  1264     //Select right statement and append number to be the limit in statement
       
  1265     
       
  1266     //Prepare select statement and get rowID column index
       
  1267     RSqlStatement stmt;
       
  1268     CleanupClosePushL( stmt );
       
  1269     
       
  1270     switch ( aTableType )
       
  1271         {
       
  1272         case EWmDrmLicenseTable:
       
  1273             statement = HBufC::NewLC( KSelectNRowIDsLicenseTableString().Length() + 
       
  1274                                       KMaxTIntBufLength );
       
  1275             statementPtr.Set( statement->Des() );
       
  1276             statementPtr.AppendFormat( KSelectNRowIDsLicenseTableString, aNumber );
       
  1277              //Prepare select statement
       
  1278             if ( iWmDrmRightsConfigFound )
       
  1279                 {
       
  1280                 stmt.PrepareL( iDatabase2, statementPtr );
       
  1281                 }
       
  1282             else
       
  1283                 {
       
  1284                 stmt.PrepareL( iDatabase, statementPtr );
       
  1285                 }
       
  1286             break;
       
  1287         case EWmDrmSecureTable:
       
  1288             statement = HBufC::NewLC( KSelectNRowIDsSecureTableString().Length() + 
       
  1289                                       KMaxTIntBufLength );
       
  1290             statementPtr.Set( statement->Des() );
       
  1291             statementPtr.AppendFormat( KSelectNRowIDsSecureTableString, aNumber );
       
  1292             stmt.PrepareL( iDatabase, statementPtr );
       
  1293             break;
       
  1294         case EWmDrmSyncTable:
       
  1295             statement = HBufC::NewLC( KSelectNRowIDsSyncTableString().Length() + 
       
  1296                                       KMaxTIntBufLength );
       
  1297             statementPtr.Set( statement->Des() );
       
  1298             statementPtr.AppendFormat( KSelectNRowIDsSyncTableString, aNumber );
       
  1299             stmt.PrepareL( iDatabase, statementPtr );
       
  1300             break;
       
  1301         case EWmDrmMeteringTable:
       
  1302             statement = HBufC::NewLC( KSelectNRowIDsMeteringTableString().Length() + 
       
  1303                                       KMaxTIntBufLength );
       
  1304             statementPtr.Set( statement->Des() );
       
  1305             statementPtr.AppendFormat( KSelectNRowIDsMeteringTableString, aNumber );
       
  1306             stmt.PrepareL( iDatabase, statementPtr );
       
  1307             break;
       
  1308         default:
       
  1309             User::Leave( KErrArgument );
       
  1310             break;
       
  1311         }
       
  1312     
       
  1313     //Get rowID column index
       
  1314     TInt rowIDColumnIndex( stmt.ColumnIndex( KRowIDColumn ) );
       
  1315     User::LeaveIfError( rowIDColumnIndex );
       
  1316 
       
  1317     //Append all selected rowIDs to array
       
  1318     TInt err( KErrNone );
       
  1319     while ( ( err = stmt.Next() ) == KSqlAtRow )
       
  1320         {
       
  1321         aArray.AppendL( stmt.ColumnInt64( rowIDColumnIndex ) );
       
  1322         }
       
  1323 
       
  1324     LOG2( "err: %d", err );
       
  1325     if ( err != KSqlAtEnd )
       
  1326         {
       
  1327         User::Leave( err );
       
  1328         }
       
  1329     CleanupStack::PopAndDestroy( 2, statement ); //stmt, statement
       
  1330     }
       
  1331 
       
  1332 // ---------------------------------------------------------------------------
       
  1333 // CWmDrmDb::DeleteRecordsWithRowIdsL
       
  1334 // ---------------------------------------------------------------------------
       
  1335 //
       
  1336 void CWmDrmDb::DeleteRecordsWithRowIdsL(   
       
  1337     TWmDrmTableType aTableType,
       
  1338     RArray<TInt64>& aArray )
       
  1339     {
       
  1340     LOGFN( "CWmDrmDb::DeleteRecordsWithRowIdsL" );
       
  1341     //Select right statement
       
  1342     TPtrC statementString( NULL, 0 );
       
  1343     RSqlStatement stmt;
       
  1344     CleanupClosePushL( stmt );
       
  1345     
       
  1346     //Prepare delete statement and get rowID parameter index
       
  1347     
       
  1348     switch ( aTableType )
       
  1349         {
       
  1350         case EWmDrmLicenseTable:
       
  1351             statementString.Set( KDeleteWithRowIDLicenseTableString().Ptr(), 
       
  1352                                  KDeleteWithRowIDLicenseTableString().Length() );
       
  1353             //Prepare delete statement
       
  1354             if ( iWmDrmRightsConfigFound )
       
  1355                 {
       
  1356                 stmt.PrepareL( iDatabase2, statementString );
       
  1357                 }
       
  1358             else
       
  1359                 {
       
  1360                 stmt.PrepareL( iDatabase, statementString );
       
  1361                 }                    
       
  1362             break;
       
  1363         case EWmDrmSecureTable:
       
  1364             statementString.Set( KDeleteWithRowIDSecureTableString().Ptr(), 
       
  1365                                  KDeleteWithRowIDSecureTableString().Length() );
       
  1366             stmt.PrepareL( iDatabase, statementString );                     
       
  1367             break;
       
  1368         case EWmDrmSyncTable:
       
  1369             statementString.Set( KDeleteWithRowIDSyncTableString().Ptr(), 
       
  1370                                  KDeleteWithRowIDSyncTableString().Length() );
       
  1371             //Prepare delete statement
       
  1372             stmt.PrepareL( iDatabase, statementString );                      
       
  1373             break;
       
  1374         case EWmDrmMeteringTable:
       
  1375             statementString.Set( KDeleteWithRowIDMeteringTableString().Ptr(), 
       
  1376                                  KDeleteWithRowIDMeteringTableString().Length() );
       
  1377             stmt.PrepareL( iDatabase, statementString );                     
       
  1378             break;
       
  1379         default:
       
  1380             User::Leave( KErrArgument );
       
  1381             break;
       
  1382         }
       
  1383     
       
  1384     // Get rowID parameter index
       
  1385     TInt rowIDIndex( stmt.ParameterIndex( KRowIDParameter ) );
       
  1386     User::LeaveIfError( rowIDIndex );
       
  1387     
       
  1388     //Execute delete statement with every rowID value from the array
       
  1389     LOG2( "aArray.Count(): %d", aArray.Count() );
       
  1390     for ( TInt i( 0 ); i < aArray.Count(); ++i )
       
  1391         {
       
  1392         User::LeaveIfError( stmt.BindInt64( rowIDIndex, aArray[i] ) );
       
  1393         User::LeaveIfError( stmt.Exec() );
       
  1394         User::LeaveIfError( stmt.Reset() );
       
  1395         }
       
  1396     CleanupStack::PopAndDestroy( &stmt );
       
  1397     }
       
  1398 
       
  1399 // ---------------------------------------------------------------------------
       
  1400 // CWmDrmDb::DropTableL
       
  1401 // ---------------------------------------------------------------------------
       
  1402 //
       
  1403 void CWmDrmDb::DropTableL( TWmDrmTableType aTableType )
       
  1404     {
       
  1405     LOGFN( "CWmDrmDb::DropTableL" );
       
  1406     switch ( aTableType )
       
  1407         {
       
  1408         case EWmDrmLicenseTable:
       
  1409             if ( iWmDrmRightsConfigFound )
       
  1410                 {
       
  1411                 User::LeaveIfError( iDatabase2.Exec( KDeleteLicenseTableString ) );
       
  1412                 }
       
  1413             else 
       
  1414                 {
       
  1415                 User::LeaveIfError( iDatabase.Exec( KDeleteLicenseTableString ) );
       
  1416                 }
       
  1417             break;
       
  1418         case EWmDrmSecureTable:
       
  1419             User::LeaveIfError( iDatabase.Exec( KDeleteSecureTableString ) );
       
  1420             break;
       
  1421         case EWmDrmSyncTable:
       
  1422         	User::LeaveIfError( iDatabase.Exec( KDeleteSyncTableString ) );
       
  1423             break;
       
  1424         case EWmDrmMeteringTable:
       
  1425             User::LeaveIfError( iDatabase.Exec( KDeleteMeteringTableString ) );
       
  1426             break;
       
  1427         default:
       
  1428             LOG1( "CWmDrmDb::DropTableL error" );
       
  1429             User::Leave( KErrArgument );
       
  1430             break; 
       
  1431         }
       
  1432     }
       
  1433 
       
  1434 // ---------------------------------------------------------------------------
       
  1435 // CWmDrmDb::TableTypeL
       
  1436 // ---------------------------------------------------------------------------
       
  1437 //
       
  1438 CWmDrmDb::TWmDrmTableType CWmDrmDb::TableTypeL( const TDesC8& aNamespace )
       
  1439     {
       
  1440     LOGFN( "CWmDrmDb::TableTypeL" );
       
  1441     if ( aNamespace.CompareC( KLicenseStore  ) == 0 )
       
  1442         {
       
  1443         return EWmDrmLicenseTable;
       
  1444         }
       
  1445     else if ( aNamespace.CompareC( KSecureStore  ) == 0 )
       
  1446         {
       
  1447         return EWmDrmSecureTable;
       
  1448         }
       
  1449     else if ( aNamespace.CompareC( KSyncStore  ) == 0 )
       
  1450         {
       
  1451         return EWmDrmSyncTable;
       
  1452         }
       
  1453     else if ( aNamespace.CompareC( KMeteringStore  ) == 0 )
       
  1454         {
       
  1455         return EWmDrmMeteringTable;
       
  1456         }    
       
  1457     else
       
  1458         {
       
  1459         User::Leave( KErrNotFound );
       
  1460         return EWmDrmLicenseTable;
       
  1461         }
       
  1462     }
       
  1463 
       
  1464 // ---------------------------------------------------------------------------
       
  1465 // CWmDrmDb::OpenDatabaseL
       
  1466 // ---------------------------------------------------------------------------
       
  1467 //
       
  1468 void CWmDrmDb::OpenDatabaseL( TFileName& aFileNamePath, RSqlDatabase&
       
  1469     aSqlDatabase, TBool aConfiguredDrive )
       
  1470     {
       
  1471     TInt error( aSqlDatabase.Open( aFileNamePath ) );
       
  1472     LOG2( "CWmDrmDb: Opening database, status: %d", error );
       
  1473     
       
  1474     if ( error == KErrNotFound )
       
  1475         {
       
  1476         // database does not exist, create one
       
  1477         CreateDatabaseL( aFileNamePath, aSqlDatabase, aConfiguredDrive );
       
  1478 		}
       
  1479     else if ( error == KSqlErrCorrupt || error == KSqlErrNotDb )
       
  1480         {
       
  1481         RemoveStoreL( KHds );
       
  1482         }
       
  1483     else
       
  1484         {
       
  1485         // Error while opening database
       
  1486         User::LeaveIfError( error );
       
  1487         }
       
  1488     User::LeaveIfError( aSqlDatabase.ReserveDriveSpace( 10 * KLicenseSize ) );
       
  1489     }
       
  1490 
       
  1491 // ---------------------------------------------------------------------------
       
  1492 // CWmDrmDb::CheckDatabaseCommitL
       
  1493 // ---------------------------------------------------------------------------
       
  1494 //
       
  1495 void CWmDrmDb::CheckDatabaseCommitL( TBool aEnforcedCommit )
       
  1496     {
       
  1497     LOGFN( "CWmDrmDb::CheckCommitDatabaseL()" );
       
  1498     
       
  1499     // Restart the timer for committing the changes to the database
       
  1500     if ( !aEnforcedCommit )
       
  1501         {
       
  1502         if ( IsActive() )
       
  1503             {
       
  1504             Cancel();
       
  1505             }
       
  1506         Activate();
       
  1507         }
       
  1508         
       
  1509     // Check if the amount of cached database operations reaches the upper 
       
  1510     // limit or if we need to have a forced commit.
       
  1511     if ( ( iAmountOperationsWithoutCommit >= KMaxOperationsUntilCommit ) || 
       
  1512         aEnforcedCommit )
       
  1513         {
       
  1514         if ( iWmDrmRightsConfigFound && iSqlTransactionOngoing2 )
       
  1515             {
       
  1516             User::LeaveIfError( iDatabase2.Exec( KCommitTransaction ) );
       
  1517             iSqlTransactionOngoing2 = EFalse;
       
  1518             }
       
  1519         
       
  1520         if ( iSqlTransactionOngoing )
       
  1521             {
       
  1522             User::LeaveIfError( iDatabase.Exec( KCommitTransaction ) );
       
  1523             iSqlTransactionOngoing = EFalse;
       
  1524             }
       
  1525             
       
  1526         // Reset the counter
       
  1527         iAmountOperationsWithoutCommit = 0;        
       
  1528         }
       
  1529     
       
  1530     // In case of non-forced commit, execute new BEGIN statement for caching
       
  1531     // future SQL statements.
       
  1532     if ( ( iAmountOperationsWithoutCommit == 0 ) && !aEnforcedCommit )
       
  1533         {
       
  1534         if ( iWmDrmRightsConfigFound )
       
  1535             {
       
  1536             User::LeaveIfError( iDatabase2.Exec( KBeginTransaction ) );
       
  1537             iSqlTransactionOngoing2 = ETrue;
       
  1538             }
       
  1539         User::LeaveIfError( iDatabase.Exec( KBeginTransaction ) );
       
  1540         iSqlTransactionOngoing = ETrue;
       
  1541         }
       
  1542     }
       
  1543