mpxplugins/serviceplugins/collectionplugins/inc/mpxdbmanager.h
changeset 0 ff3acec5bc43
child 11 13afc0e517bd
equal deleted inserted replaced
-1:000000000000 0:ff3acec5bc43
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  This class is responsible for managing all of music collection
       
    15 *                databases.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #ifndef MPXDBMANAGER_H
       
    21 #define MPXDBMANAGER_H
       
    22 
       
    23 // INCLUDES
       
    24 
       
    25 #include <e32base.h>
       
    26 #include <f32file.h>
       
    27 #include <sqldb.h>
       
    28 #include <badesca.h>
       
    29 
       
    30 // FORWARD DECLARATIONS
       
    31 
       
    32 class MMPXTable;
       
    33 class RSqlStatement;
       
    34 
       
    35 // CONSTANTS
       
    36 
       
    37 const TInt KDbManagerAllDrives = 0;
       
    38 
       
    39 // CLASS DECLARATION
       
    40 
       
    41 /**
       
    42 * Generic class responsible for managing databases on multiple drives.
       
    43 *
       
    44 * @lib MPXDbPlugin.lib
       
    45 */
       
    46 class CMPXDbManager :
       
    47     public CBase
       
    48     {
       
    49     protected: //   Constructors
       
    50 
       
    51         /**
       
    52         * C++ default constructor
       
    53         * @param aFs file server session
       
    54         */
       
    55         IMPORT_C CMPXDbManager(RFs& aFs);
       
    56 
       
    57         /**
       
    58         * The second phase constructor to safely construct things
       
    59         * that can leave
       
    60         * @param aDatabaseFile database filename
       
    61         */
       
    62         IMPORT_C void ConstructL(const TFileName& aDatabaseFile);
       
    63 
       
    64     public:
       
    65 
       
    66         /**
       
    67         * Destructor
       
    68         */
       
    69         IMPORT_C virtual ~CMPXDbManager();
       
    70 
       
    71     public: // New methods
       
    72 
       
    73         /**
       
    74         * Checks if all databases have been initialized.
       
    75         * @return ETrue if initialized, EFalse otherwise
       
    76         */
       
    77         IMPORT_C TBool IsInitialized();
       
    78 
       
    79         /**
       
    80         * Begins a transaction on all databases.
       
    81         */
       
    82         IMPORT_C void BeginL();
       
    83 
       
    84         /**
       
    85         * Commits a transaction on all databases.
       
    86         */
       
    87         IMPORT_C void CommitL();
       
    88 
       
    89         /**
       
    90         * Rolls back a transaction on all databases.
       
    91         */
       
    92         IMPORT_C void RollbackL();
       
    93 
       
    94         /**
       
    95         * Checks if the database is currently in a transaction
       
    96         *
       
    97         * @return ETrue if database is currently in a transaction, EFalse otherwise
       
    98         */
       
    99         IMPORT_C TBool InTransaction();
       
   100 
       
   101         /**
       
   102         * Tries to create and open the databases on all specified drives.
       
   103         * If a drive is not valid (like an MC not plugged in) it will be skipped.
       
   104         * @param aDrives array of drives to create and open databases on.
       
   105         */
       
   106         IMPORT_C void InitDatabasesL(RArray<TInt> aDrives);
       
   107 
       
   108         /**
       
   109         * Opens a specified database.
       
   110         * @param aDrive identifies the drive index of the database to open
       
   111         */
       
   112         IMPORT_C void OpenDatabaseL(TInt aDrive);
       
   113 
       
   114         /**
       
   115         * Closes a specified database.
       
   116         * @param aDrive identifies the drive index of the database to close
       
   117         */
       
   118         IMPORT_C void CloseDatabaseL(TInt aDrive);
       
   119 
       
   120         /**
       
   121         * Closes all open databases.
       
   122         */
       
   123         IMPORT_C void CloseAllDatabases();
       
   124 
       
   125         /**
       
   126         * Opens all open databases.
       
   127         */
       
   128         IMPORT_C void OpenAllDatabasesL();
       
   129 
       
   130         /**
       
   131         * Checks if the database on a specified drive is open.
       
   132         * @param aDrive identifies the drive index of the database to check
       
   133         * @return ETrue if the database is open, EFalse otherwise
       
   134         */
       
   135         IMPORT_C TBool IsOpen(TInt aDrive) const;
       
   136 
       
   137         /**
       
   138         * Returns the number of currently open databases.
       
   139         * @return Number of databases
       
   140         */
       
   141         IMPORT_C TInt DatabaseCount() const;
       
   142 
       
   143         /**
       
   144         * Returns the drive corresponding to a give index.
       
   145         * @param aIndex identifies the index in the list of drives the database uses
       
   146         * @return Drive index the database uses.
       
   147         */
       
   148         IMPORT_C TInt DbDrive(TInt aIndex) const;
       
   149 
       
   150         /**
       
   151         * Recreate a specified database.
       
   152         * @param aDrive identifies the drive index ro recreate the database
       
   153         */
       
   154         IMPORT_C void RecreateDatabaseL(TInt aDrive);
       
   155 
       
   156         /**
       
   157         * Recreate all databases.
       
   158         */
       
   159         IMPORT_C void RecreateAllDatabasesL();
       
   160 
       
   161         /**
       
   162         * Return current DB version
       
   163         * @return the version of db structure
       
   164         */
       
   165         IMPORT_C TVersion Version() const;
       
   166 
       
   167         /**
       
   168         * Return current DB version
       
   169         * @return the version of db structure
       
   170         */
       
   171         IMPORT_C void RegisterTableL(MMPXTable& aTable);
       
   172 
       
   173         /**
       
   174         * Executes a select query with variable number of parameters on all
       
   175         * available drives.
       
   176         * @param aFmt query format string
       
   177         * @return resulted result set
       
   178         */
       
   179         IMPORT_C RSqlStatement ExecuteSelectQueryL(TRefByValue<const TDesC> aFmt, ...);
       
   180 
       
   181         /**
       
   182         * Executes a select query with variable number of parameters
       
   183         * against a specified drive
       
   184         * @param aDrive to execute query on
       
   185         * @param aFmt query format string
       
   186         * @return resulted result set
       
   187         */
       
   188         IMPORT_C RSqlStatement ExecuteSelectQueryL(TInt aDrive, TRefByValue<const TDesC> aFmt,
       
   189             ...);
       
   190 
       
   191         /**
       
   192         * Executes a select query with variable number of parameters
       
   193         * on all available drives. Two values will be bound to the statement
       
   194         * @param aStatementId unique id of a statement to bind to
       
   195         *                     creates a new statement if the id is not found
       
   196         * @param aFirstValue TInt Value to bind as first argument
       
   197         * @param aSecondValue TInt Value to bind as second argument
       
   198         * @param aFmt query format string
       
   199         * @return resulted result set. The ownership is not passed so the caller should
       
   200         *         not destroy this as it may be reused in future calls.
       
   201         */
       
   202         IMPORT_C RSqlStatement& ExecuteSelectQueryL( TUint aStatementId,
       
   203                                                      TInt aFirstValue, TInt aSecondValue,
       
   204                                                      TRefByValue<const TDesC> aFmt, ...);
       
   205 
       
   206         /**
       
   207         * Executes a select query with variable number of parameters
       
   208         * on all available drives. Two values will be bound to the statement
       
   209         * @param aStatementId unique id of a statement to bind to
       
   210         *                     creates a new statement if the id is not found
       
   211         * @param aFirstValue TDesC Value to bind as first argument
       
   212         * @param aSecondValue TInt Value to bind as second argument
       
   213         * @param aFmt query format string
       
   214         * @return resulted result set. The ownership is not passed so the caller should
       
   215         *         not destroy this as it may be reused in future calls.
       
   216         */
       
   217         IMPORT_C RSqlStatement& ExecuteSelectQueryL( TUint aStatementId,
       
   218                                                      const TDesC& aFirstValue,
       
   219                                                      TInt aSecondValue,
       
   220                                                      TRefByValue<const TDesC> aFmt, ...);
       
   221         /**
       
   222         * Executes a query that does not return a record set
       
   223         * @param aDrive to execute query on
       
   224         * @param aFmt query format string
       
   225         */
       
   226         IMPORT_C void ExecuteQueryL(TInt aDrive, TRefByValue<const TDesC> aFmt, ...);
       
   227 
       
   228         /**
       
   229         * Prints the contents of all the databases to the log file
       
   230         * @param aDrive to execute query on
       
   231         */
       
   232         IMPORT_C void PrintDatabaseL();
       
   233 
       
   234         /**
       
   235         * Returns the file server session.
       
   236         * @return file server session
       
   237         */
       
   238         IMPORT_C RFs& Fs();
       
   239 
       
   240         /**
       
   241         * Checks if the space on the specified drive(s) is above the critical level
       
   242         * @param aDrive drive ID or KDbManagerAllDrives
       
   243         * @leave KErrDiskFull if the space on the specified drive(s) is below the
       
   244         *        critical level
       
   245         */
       
   246         IMPORT_C void CheckDiskSpaceL(TInt aDrive);
       
   247 
       
   248         /**
       
   249         * Completely recreate all databases.
       
   250         */
       
   251         IMPORT_C void RegenerateAllDatabasesL();
       
   252         
       
   253         /**
       
   254          * Checks if the spefified drive is a remove drive
       
   255          */      
       
   256         IMPORT_C TBool IsRemoteDrive(TDriveNumber aDrive);
       
   257 
       
   258     protected:
       
   259 
       
   260         /**
       
   261         * Creates a database on a specified drive.
       
   262         * @param aDrive identifies the database
       
   263         */
       
   264         IMPORT_C void CreateDatabaseL(TInt aDrive);
       
   265 
       
   266         /**
       
   267         * Remove the database
       
   268         * @param aDrive identifies the database
       
   269         */
       
   270         IMPORT_C void RemoveDatabaseL(TInt aDrive);
       
   271 
       
   272         /**
       
   273         * Create the all tables
       
   274         * @param aDatabase the database to add tables to
       
   275         */
       
   276         IMPORT_C void CreateTablesL(RSqlDatabase& aDatabase);
       
   277 
       
   278         /**
       
   279         * Drop all tables
       
   280         * @param aDatabase the database to drop tables from
       
   281         */
       
   282         IMPORT_C void DropTablesL(RSqlDatabase& aDatabase);
       
   283         
       
   284     public:
       
   285     	
       
   286         /**
       
   287         * Executes a select query with variable number of parameters
       
   288         * against a specified drive
       
   289         * @param aDrive to execute query on
       
   290         * @param aFmt query format string
       
   291         * @return resulted result set
       
   292         */
       
   293         IMPORT_C RSqlStatement ExecuteSelectQueryOnAllDrivesL(TInt aDrive, TRefByValue<const TDesC> aFmt,
       
   294             ...);
       
   295 
       
   296     protected:  // Types
       
   297 
       
   298         typedef struct
       
   299             {
       
   300             TInt iDrive;
       
   301             TBool iOpen;
       
   302             HBufC* iAliasname;
       
   303             } DatabaseHandle;
       
   304 
       
   305     protected:  // Data
       
   306 
       
   307         HBufC* iDbFile;
       
   308         RArray<MMPXTable*> iTables;
       
   309         RSqlDatabase iDatabase;
       
   310         RArray<DatabaseHandle> iDatabaseHandles;
       
   311 
       
   312     private:
       
   313 
       
   314         /**
       
   315         * Create the all tables
       
   316         * @param aDatabase the database to add tables to
       
   317         * @param aCorrupt specifies whether the database is corrupted or not
       
   318         */
       
   319         void CreateTablesL(RSqlDatabase& aDatabase, TBool aCorrupt);
       
   320 
       
   321         /**
       
   322         * Opens a specified database.
       
   323         * @param aDrive identifies the drive unit of the database to open
       
   324         */
       
   325         void OpenDatabaseL(TDriveUnit aDrive);
       
   326 
       
   327         /**
       
   328         * Creates a specified database.
       
   329         * @param aDrive identifies the drive unit of the database to create
       
   330         */
       
   331         void CreateDatabaseL(TDriveUnit aDrive);
       
   332 
       
   333         /**
       
   334         * Attached a specified database.
       
   335         * @param aDrive identifies the drive unit of the database to attach
       
   336         */
       
   337         void AttachDatabaseL(TDriveUnit aDrive);
       
   338 
       
   339         /**
       
   340         * Detach a specified database.
       
   341         * @param aDrive identifies the drive unit of the database to detach
       
   342         */
       
   343         void DetachDatabaseL(TDriveUnit aDrive);
       
   344 
       
   345         /**
       
   346         * Create filename on a specified drive unit.
       
   347         * @param aDrive identifies the drive unit
       
   348         */
       
   349         HBufC* CreateFilenameL(TDriveUnit aDrive);
       
   350 
       
   351 
       
   352         /**
       
   353         * Replaces specially formatted query symbols with a specified alias.
       
   354         * @param aAlias identifies the alias name to use in query
       
   355         */
       
   356         void ReplaceDriveAlias(TDes& aQuery, const TDesC& aAlias);
       
   357 
       
   358         /**
       
   359         * Replaces specially formatted query symbols with a specified alias.
       
   360         * @param aAlias identifies the alias name to use in query
       
   361         * @param aToKen replace by aAlias
       
   362         */
       
   363         void ReplaceDriveAlias(TDes& aQuery, const TDesC& aAlias, const TDesC& aToKen);
       
   364 
       
   365         /**
       
   366         * Removes specially formatted query symbols from the query
       
   367         * @param aQuery identifies the query to remove specially formatted symbols
       
   368         */
       
   369         void RemoveDriveAlias(TDes& aQuery);
       
   370         
       
   371         /**
       
   372         * Removes specially formatted query symbols from the query
       
   373         * @param aQuery identifies the query to remove specially formatted symbols
       
   374         * @param aToKen identifies need remove string
       
   375         */
       
   376         void RemoveDriveAlias(TDes& aQuery, const TDesC& aToKen);
       
   377 
       
   378         /**
       
   379         * Attempts to recreate database by dropping and creating tables
       
   380         * used inside RecreateDatabaseL
       
   381         * @param aFilename database filename
       
   382         */
       
   383         void DoRecreateDatabaseL(HBufC * aFilename);
       
   384 
       
   385         /**
       
   386          * Executes SQL statement against the database.
       
   387          *
       
   388          * @param aDatabase database handle
       
   389          * @param aStatement sql statement buffer
       
   390          * @return KErrNone if successfull, error code otherwise.
       
   391          *
       
   392          */
       
   393         TInt ExecuteSqlStatement(RSqlDatabase& aDatabase, const TDesC& aStatement);
       
   394 
       
   395         /**
       
   396         * Formats the query.
       
   397         * @param aFmt query format
       
   398         * @param aList variable parameter list
       
   399         * @return formatted query, the ownership is transferred.
       
   400         */
       
   401         HBufC* FormatQueryLC(TRefByValue<const TDesC> aFmt, VA_LIST aList);
       
   402 
       
   403         /**
       
   404         * Executes an SQL query on all drives
       
   405         * @param aQuery query string
       
   406         * @return prepared SQL statement
       
   407         */
       
   408         RSqlStatement ExecuteSelectQueryOnAllDrivesL(TPtr aQuery);
       
   409         
       
   410         /**
       
   411         * Executes an SQL query on all drives
       
   412         * @param aDrive drive to execute the query on
       
   413         * @param aQuery query string
       
   414         * @return prepared SQL statement
       
   415         */
       
   416         RSqlStatement ExecuteSelectQueryOnAllDrivesL(TInt aDrive,TPtr aQuery);
       
   417 
       
   418         /**
       
   419         * Executes an SQL query on a specified drive
       
   420         * @param aDrive drive to execute the query on
       
   421         * @param aQuery query string
       
   422         * @return prepared SQL statement
       
   423         */
       
   424         RSqlStatement ExecuteSelectQueryOnDriveL(TInt aDrive, TPtr aQuery);
       
   425 
       
   426         /**
       
   427         * Prepares an sql query given the statment
       
   428         * @param aStatementId unique identifier of the statement
       
   429         * @param aFmt query format string
       
   430         * @param aList variable parameter list
       
   431         * @return reference to the SQL statement
       
   432         */
       
   433         RSqlStatement& PrepareQueryL( TUint aStatementId,
       
   434                                       TRefByValue<const TDesC> aFmt,
       
   435                                       VA_LIST aList );
       
   436 
       
   437         /**
       
   438         * Resets all prepared queries
       
   439         */
       
   440         void ResetPreparedQueries();
       
   441 
       
   442         /**
       
   443         * Create Database
       
   444         */
       
   445         void DoCreateDatabaseL( TDriveUnit aDrive );
       
   446         
       
   447 #ifdef _DEBUG
       
   448 
       
   449         /**
       
   450         * Returns the number of columns from a specified SQL statement
       
   451         * @param aStatement identifies the SQL query to check
       
   452         */
       
   453         TInt GetColumnCountL(RSqlStatement& aStatement);
       
   454 
       
   455         /**
       
   456         * Prints the table of results from a specified SQL query to the debug log
       
   457         * @param aStatement identifies the SQL query made
       
   458         */
       
   459         void PrintTableValuesL(RSqlStatement& aStatement);
       
   460 
       
   461         /**
       
   462         * Finds all the tables on the main or attached drives
       
   463         * @param aAlias identifies the alias name to use if a attached drive KNullDesC if main drive
       
   464         * @param aTableName identifies the array of table names on the main or attached drive
       
   465         */
       
   466         void FindAllTablesL(const TDesC& aAlias, RArray<HBufC*>& aTableName);
       
   467 
       
   468         /**
       
   469         * Prints the table on the main or attached drives
       
   470         * @param aAlias identifies the alias name to use if a attached drive KNullDesC if main drive
       
   471         * @param aTableName identifies the table name on the main or attached drive
       
   472         */
       
   473         void PrintTableL(const TDesC& aAlias, const TDesC& aTableName);
       
   474 
       
   475 #endif
       
   476     private:
       
   477 
       
   478         /*
       
   479         * Structure to hold the state of a sql statement
       
   480         */
       
   481         NONSHARABLE_STRUCT( TSqlStatementState )
       
   482             {
       
   483             TBool iPrepared;
       
   484             TUint iId;
       
   485             };
       
   486 
       
   487     private:
       
   488 
       
   489         TBool iInitialized;
       
   490         TInt iTransactionCount;
       
   491         RFs& iFs;
       
   492 
       
   493         RArray<TSqlStatementState> iPreparedStatements;
       
   494         RPointerArray<RSqlStatement> iStatements;
       
   495     };
       
   496 
       
   497 #endif  // MPXDBMANAGER_H