harvester/harvesterserver/src/ccontentinfodb.cpp
changeset 23 d4d56f5e7c55
parent 20 556534771396
child 24 65456528cac2
equal deleted inserted replaced
20:556534771396 23:d4d56f5e7c55
     1 /*
       
     2 * Copyright (c) 2010 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 "ccotentinfodb.h"
       
    19 #include "ccontentinfo.h"
       
    20 #include <cpixcontentinfocommon.h>
       
    21 #include "OstTraceDefinitions.h"
       
    22 #ifdef OST_TRACE_COMPILER_IN_USE
       
    23 #include "ccontentinfodbTraces.h"
       
    24 #endif
       
    25 
       
    26 
       
    27 // The max length for creating sql query for KCISqlFormatSeek format
       
    28 const TInt KCISqlStringMaxLength(100);
       
    29 //SQL query to fetch the records with given Plugin name
       
    30 _LIT(KCISqlFormatSeek , "SELECT * FROM table1 WHERE NAME='%S'");
       
    31 //SQL query to delete the records with given plugin name
       
    32 _LIT(KCISqlDelete, "DELETE FROM table1 WHERE NAME='%S'");
       
    33 //Syntax for adding a row to sql database
       
    34 _LIT(KInsertRowFormat,"INSERT INTO table1(NAME,INS,BLS) values('%S',%d,%d)");
       
    35 //syntax to retrieve all the rows from the sql database
       
    36 _LIT(KSelectAllRowsFormat,"SELECT * FROM table1");
       
    37 //syntax to update Index status for a given plugin
       
    38 _LIT(KUpdateISFormat,"UPDATE table1 SET INS=%d WHERE NAME = '%S'");
       
    39 //syntax to update Blacklist status for a given plugin
       
    40 _LIT(KUpdateBSFormat,"UPDATE table1 SET BLS=%d WHERE NAME = '%S'");
       
    41 _LIT(KDriveC, "c:");
       
    42 //syntax for removing all the records in the table
       
    43 _LIT(KRemoveAll, "DELETE FROM table1" );
       
    44 //syntax for creating table with plugin name, indexing status and blacklist status as the columns
       
    45 _LIT(KCreateTableFormat,"CREATE TABLE table1(NAME TEXT NOT NULL, INS INTEGER, BLS INTEGER, PRIMARY KEY(NAME))");
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CContentInfoDb::NewL()
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CContentInfoDb* CContentInfoDb::NewL()
       
    52     {
       
    53     OstTraceFunctionEntry0( CCONTENTINFODB_NEWL_ENTRY );
       
    54     CContentInfoDb* instance = CContentInfoDb::NewLC();
       
    55     CleanupStack::Pop( instance );
       
    56     OstTraceFunctionExit0( CCONTENTINFODB_NEWL_EXIT );
       
    57     return instance;
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CContentInfoDb::NewLC()
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CContentInfoDb* CContentInfoDb::NewLC()
       
    65     {
       
    66     OstTraceFunctionEntry0( CCONTENTINFODB_NEWLC_ENTRY );
       
    67     CContentInfoDb* instance = new (ELeave) CContentInfoDb();
       
    68     CleanupStack::PushL( instance );
       
    69     instance->ConstructL();
       
    70     OstTraceFunctionExit0( CCONTENTINFODB_NEWLC_EXIT );
       
    71     return instance;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CContentInfoDb::CContentInfoDb()
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CContentInfoDb::CContentInfoDb()
       
    79     {
       
    80     //Do the necessary initialisation
       
    81     iOpened = EFalse;
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CContentInfoDb::~CContentInfoDb
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 CContentInfoDb::~CContentInfoDb()
       
    89     {
       
    90     iDatabase.Close();
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CBlacklistDb::ConstructL()
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void CContentInfoDb::ConstructL()
       
    98     {
       
    99     OstTraceFunctionEntry0( CCONTENTINFODB_CONSTRUCTL_ENTRY );
       
   100     //Open the sql database.If doesn't exist,create the sql database and open it  
       
   101     RFs fssession;
       
   102     User::LeaveIfError( fssession.Connect() );
       
   103     TFileName privatePath;
       
   104     TFileName datafile;    
       
   105     fssession.CreatePrivatePath(EDriveC);
       
   106     fssession.PrivatePath(privatePath);//data caged path of loading process
       
   107     fssession.Close();
       
   108     datafile.Copy(KDriveC);
       
   109     datafile.Append(privatePath);    
       
   110     datafile.Append( KContentInfoFileName );
       
   111     
       
   112     TInt err = iDatabase.Open( datafile );
       
   113     
       
   114     switch ( err )
       
   115         {
       
   116         case KErrNone:
       
   117             iOpened = ETrue;
       
   118             break;
       
   119             
       
   120         case KErrNotFound:
       
   121         case KErrPathNotFound:
       
   122             {
       
   123             //Create the database
       
   124             TRAPD( error , CreateDBL() );
       
   125             
       
   126             if ( error == KErrNone )
       
   127                 {                
       
   128                 iOpened = ETrue;
       
   129                 }
       
   130             }
       
   131             break;
       
   132             
       
   133         default:
       
   134             break;
       
   135         }
       
   136     
       
   137     OstTraceFunctionExit0( CCONTENTINFODB_CONSTRUCTL_EXIT );
       
   138     return;
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CContentInfoDb::AddL()
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 TInt CContentInfoDb::AddL( CContentInfo* aContentInfo )
       
   146     {
       
   147     OstTraceFunctionEntry0( CCONTENTINFODB_ADDL_ENTRY );
       
   148     //Add the item record to database
       
   149     
       
   150     if ( !iOpened )
       
   151         return KErrNotReady;
       
   152     
       
   153     //Prepare the sql
       
   154     TBuf<KCISqlStringMaxLength> sql;
       
   155     HBufC* contentname = aContentInfo->GetNameL();
       
   156     
       
   157     TInt indexstatus = aContentInfo->GetIndexStatus();
       
   158     TBool blstatus = aContentInfo->GetBlacklistStatus();
       
   159     sql.Format( KInsertRowFormat , contentname , indexstatus , blstatus );
       
   160     
       
   161     TInt err = iDatabase.Exec(sql) ;
       
   162     
       
   163     delete contentname;
       
   164     OstTraceFunctionExit0( CCONTENTINFODB_ADDL_EXIT );
       
   165     return err;
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CContentInfoDb::UpdateBlacklistStatusL
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 TInt  CContentInfoDb::UpdateBlacklistStatusL( const TDesC& aContentName , TInt aBlacklistStatus )
       
   173     {
       
   174     OstTraceFunctionEntry0( CCONTENTINFODB_UPDATEBLACKLISTSTATUSL_ENTRY );
       
   175     //Update the item record to database    
       
   176     if ( !iOpened )
       
   177         return KErrNotReady;
       
   178     
       
   179     //Prepare the sql
       
   180     TBuf<KCISqlStringMaxLength> sql;
       
   181     sql.Format( KUpdateBSFormat , aBlacklistStatus, &aContentName );
       
   182     
       
   183     TInt err = iDatabase.Exec(sql) ;
       
   184     
       
   185     OstTraceFunctionExit0( CCONTENTINFODB_UPDATEBLACKLISTSTATUSL_EXIT );
       
   186     return err;
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CContentInfoDb::UpdatePluginIndexStatusL
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 TInt  CContentInfoDb::UpdatePluginIndexStatusL( const TDesC& aContentName , TInt aIndexStatus )
       
   194     {
       
   195     OstTraceFunctionEntry0( CCONTENTINFODB_UPDATEPLUGININDEXSTATUSL_ENTRY );
       
   196     //Update the item record to database
       
   197     
       
   198     if ( !iOpened )
       
   199         return KErrNotReady;
       
   200     
       
   201     //Prepare the sql
       
   202     TBuf<KCISqlStringMaxLength> sql;
       
   203     sql.Format( KUpdateISFormat , aIndexStatus, &aContentName );
       
   204     
       
   205     TInt err = iDatabase.Exec(sql) ;
       
   206     
       
   207     OstTraceFunctionExit0( CCONTENTINFODB_UPDATEPLUGININDEXSTATUSL_EXIT );
       
   208     return err;
       
   209     }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CContentInfoDb::RemoveL
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void CContentInfoDb::RemoveL( const TDesC& aContentName )
       
   216     {
       
   217     OstTraceFunctionEntry0( CCONTENTINFODB_REMOVEL_ENTRY );
       
   218     //remove the item record to database
       
   219         
       
   220     if ( !iOpened )
       
   221         User::Leave( KErrNotReady );
       
   222     
       
   223     //Prepare the sql
       
   224     TBuf<KCISqlStringMaxLength> sql;
       
   225     sql.Format( KCISqlDelete , &aContentName );
       
   226     
       
   227     User::LeaveIfError( iDatabase.Exec(sql) );
       
   228     OstTraceFunctionExit0( CCONTENTINFODB_REMOVEL_EXIT );
       
   229     return ;
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CContentInfoDb::ResetDatabaseL
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 void CContentInfoDb::ResetDatabaseL( )
       
   237     {
       
   238     OstTraceFunctionEntry0( CCONTENTINFODB_RESETDATABASEL_ENTRY );
       
   239     //remove all the item record to database
       
   240     
       
   241     if ( !iOpened )
       
   242         User::Leave( KErrNotReady );
       
   243     
       
   244     //Prepare the sql
       
   245     TBuf<KCISqlStringMaxLength> sql;
       
   246     sql.Copy( KRemoveAll );
       
   247     
       
   248     User::LeaveIfError( iDatabase.Exec(sql) );
       
   249     
       
   250     OstTraceFunctionExit0( CCONTENTINFODB_RESETDATABASEL_EXIT );
       
   251     return ;
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // CContentInfoDb::FindL
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 TBool CContentInfoDb::FindL(const TDesC& aContentName)
       
   259     {
       
   260     OstTraceFunctionEntry0( CCONTENTINFODB_FINDL_ENTRY );
       
   261     if ( !iOpened )
       
   262        User::Leave( KErrNotReady );
       
   263     
       
   264     //Prepare the sql
       
   265     TBuf<KCISqlStringMaxLength> sql;
       
   266     sql.Format( KCISqlFormatSeek, &aContentName );
       
   267     
       
   268     RSqlStatement stmt;
       
   269     TBool isfound = EFalse;
       
   270     //Error check necessary to avoid sqldb 2 panic, 
       
   271     //if sqlstatement preparation fails, call to Next() raises this panic
       
   272     TInt err = stmt.Prepare( iDatabase , sql );
       
   273     if ( err == KErrNone)
       
   274         isfound = ( KSqlAtRow == stmt.Next() )?ETrue:EFalse; 
       
   275     OstTraceFunctionExit0( CCONTENTINFODB_FINDL_EXIT );
       
   276     return isfound;
       
   277     }
       
   278 
       
   279 // -----------------------------------------------------------------------------
       
   280 // CContentInfoDb::GetContentCountL
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 TInt CContentInfoDb::GetContentCountL()
       
   284     {
       
   285     OstTraceFunctionEntry0( CCONTENTINFODB_GETCONTENTCOUNTL_ENTRY );
       
   286     if ( !iOpened )
       
   287        User::Leave( KErrNotReady );
       
   288     
       
   289     TInt count = 0;
       
   290     
       
   291     //Prepare the sql
       
   292     TBuf<KCISqlStringMaxLength> sql;
       
   293     sql.Copy( KSelectAllRowsFormat );
       
   294     
       
   295     RSqlStatement stmt;
       
   296     TInt err = stmt.Prepare( iDatabase , sql );
       
   297     //Error check necessary to avoid sqldb 2 panic, 
       
   298     //if sqlstatement preparation fails, call to Next() raises this panic     
       
   299     if( err == KErrNone)
       
   300         {
       
   301         while ( KSqlAtEnd != stmt.Next() )
       
   302             ++count;
       
   303         }
       
   304     OstTraceFunctionExit0( CCONTENTINFODB_GETCONTENTCOUNTL_EXIT );
       
   305     return count;
       
   306     }
       
   307 
       
   308 // -----------------------------------------------------------------------------
       
   309 // CContentInfoDb::CreateDBL
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CContentInfoDb::CreateDBL()
       
   313     {
       
   314     OstTraceFunctionEntry0( CCONTENTINFODB_CREATEDBL_ENTRY );
       
   315     
       
   316     iDatabase.Close();
       
   317     
       
   318     TFileName privatePath;
       
   319     TFileName datafile;
       
   320     RFs fssession;
       
   321     User::LeaveIfError( fssession.Connect() );
       
   322     fssession.CreatePrivatePath(EDriveC);
       
   323     fssession.PrivatePath(privatePath);//data caged path of loading process
       
   324     fssession.Close();
       
   325     datafile.Copy(KDriveC);
       
   326     datafile.Append(privatePath);    
       
   327     datafile.Append( KContentInfoFileName );
       
   328     
       
   329     //create the database
       
   330     User::LeaveIfError( iDatabase.Create( datafile ) );
       
   331     
       
   332     //Add table
       
   333     TBuf<KCISqlStringMaxLength> sql;
       
   334     sql.Copy( KCreateTableFormat );
       
   335     
       
   336     User::LeaveIfError( iDatabase.Exec( sql ) );
       
   337     
       
   338     OstTraceFunctionExit0( CCONTENTINFODB_CREATEDBL_EXIT );
       
   339     }
       
   340 //EOF