inc/mdssqliteconnection.h
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2002-2009 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:  Adaptation layer to SQLite database*
       
    15 */
       
    16 
       
    17 #ifndef __MDSSQLITECONNECTION_H__
       
    18 #define __MDSSQLITECONNECTION_H__
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <sqldb.h>
       
    22 
       
    23 #include "mdssqlrow.h"
       
    24 
       
    25 
       
    26 // FORWARD DECLARATION
       
    27 class RMdsStatement;
       
    28 
       
    29 
       
    30 
       
    31 // CLASS DECLARATION
       
    32 /**
       
    33 * Class represents methods to communication with SQLite database API.
       
    34 */
       
    35 NONSHARABLE_CLASS(CMdSSqLiteConnection): public CBase
       
    36 	{
       
    37     public: // Constructors and destructor
       
    38 
       
    39         /**
       
    40         * Two-phased constructor.
       
    41         * @param 
       
    42         * @return 
       
    43         */
       
    44         static CMdSSqLiteConnection* NewL();
       
    45         
       
    46         /**
       
    47         * Two-phased constructor.
       
    48         * @param 
       
    49         * @return 
       
    50         */
       
    51         static CMdSSqLiteConnection* NewLC();
       
    52 
       
    53         /**
       
    54         * Destructor.
       
    55         */   
       
    56         virtual ~CMdSSqLiteConnection();
       
    57         
       
    58         /**
       
    59          * Deletes database from disc
       
    60          *
       
    61          * @param aName name of database or NULL to delete default one
       
    62          */
       
    63         static void DeleteDb( TDesC16* aName = NULL );
       
    64         
       
    65     private: // Private constructors
       
    66 
       
    67         /**
       
    68         * CSqLiteConnection.
       
    69         * C++ default constructor.
       
    70         */        
       
    71         CMdSSqLiteConnection();
       
    72 
       
    73         /**
       
    74         * ConstructL.
       
    75         * 2nd phase constructor.
       
    76         */        
       
    77         void ConstructL();
       
    78 
       
    79     public: // Methods from CMdSSqLiteConnection
       
    80 
       
    81 		/**
       
    82 		 * Open database (creates if doesn't exist)
       
    83 		 * @param aFile Database filename and path
       
    84 		 */
       
    85         void OpenDbL( const TDesC& aDbFileName );
       
    86 
       
    87         /**
       
    88          * Close opened database
       
    89          */
       
    90         void CloseDb();
       
    91 
       
    92         /**
       
    93          * Execute sql command (not a query)
       
    94          * @param aCommand SQL clause to be executed (can include wildcards)
       
    95          * @param aVariables data that will replace wildcards in the command
       
    96          * @return KErrNone, if successful, otherwise one of the error codes.
       
    97          */
       
    98         TInt ExecuteL( const TDesC& aCommand,
       
    99                        const RRowData& aVariables,
       
   100                        RMdsStatement* aStatement = NULL );
       
   101 
       
   102         /**
       
   103          * Prepare and execute sql query
       
   104          * @param aQuery the SQL query to perform
       
   105          * @param aStatement ptr to statement id, which must be initially NULL.
       
   106          *        The statement can be used for executing the same query several
       
   107          *        times with different variables. Note that finally the statement
       
   108          *        must be terminated.
       
   109          * @param aVariables data that will replace wildcards in the command
       
   110          */
       
   111         void ExecuteQueryL( const TDesC& aQuery,
       
   112         					RMdsStatement& aStatement,
       
   113             				const RRowData& aVariables );
       
   114 
       
   115         /**
       
   116          * Get next row in query
       
   117          * @param aQuery ID of the query
       
   118          * @param aRow specifies target for the data to receive
       
   119          * @return true if there are more rows available
       
   120          */
       
   121         TBool NextRowL( RMdsStatement& aQuery, RRowData& aRow );
       
   122 
       
   123         /**
       
   124          * Update the current row in query
       
   125          * (in case some of the datatypes need to change)
       
   126          * @param aQuery ID of the query
       
   127          * @param aRow specifies target for the data to receive
       
   128          * @return true if there are more rows available
       
   129          */
       
   130         void CurrentRowL( const RMdsStatement& aQuery, RRowData& aRow );
       
   131 
       
   132         /**
       
   133          * terminates a running query or execution loop
       
   134          * @param aStatement ID of the statement
       
   135          */
       
   136         void Terminate( RMdsStatement& aQuery );
       
   137         
       
   138         /**
       
   139          * Rolls back the transaction explicitly
       
   140          * - possibility to receive leave
       
   141          * eliminates actions on destructor.
       
   142          */
       
   143         void TransactionBeginL();
       
   144 
       
   145         /**
       
   146          * Resets the transaction
       
   147          * (does not commit or rollback or do anything)
       
   148          */
       
   149         void TransactionCommitL();
       
   150 
       
   151         /**
       
   152          * error within transaction
       
   153          */
       
   154         void TransactionRollbackL();
       
   155 
       
   156         /**
       
   157          * Returns DB filename of the connection.
       
   158          */
       
   159         const TDesC& DbFileName() const;
       
   160         
       
   161         void EnableTransaction( TBool aEnable, RMdsStatement& aQuery );
       
   162 
       
   163     protected: // Personal methods     
       
   164 
       
   165         /**
       
   166         * Binds variables in SQLite interface
       
   167         * @param aStatement SQLite statement handle
       
   168         * @param aVariables the list of variables to bind
       
   169         */
       
   170         void DoBindL( RSqlStatement& aStatement, const RRowData& aVariables );
       
   171 
       
   172         /**
       
   173         * Reads row column variables from SQLite interface
       
   174         * @param aStatement SQLite statement handle
       
   175         * @param aRow the row buffer where to read the column data
       
   176         */
       
   177         void ColumnsL( const RSqlStatement& aStatement, RRowData& aRow );
       
   178 
       
   179         /**
       
   180         * Saves trace of last SQL error and leaves.
       
   181         * @param aFailedCommand a C-style representation of the failed SqLite command (for logging)
       
   182         * @param aSqliteError the error reported by sqlite
       
   183         */
       
   184         void TraceAndLeaveL( const TDesC& aFailedCommand, TInt aSqliteError );
       
   185 
       
   186     private: // Member data
       
   187 
       
   188         /**
       
   189          * iMdeSqlDb, interface to the Sqlite database.
       
   190          */
       
   191         RSqlDatabase iMdeSqlDb;
       
   192 
       
   193         HBufC* iDbFileName;
       
   194         
       
   195         TBool iEnableTransaction;
       
   196         
       
   197         TBool iTransactionOngoing;
       
   198 		
       
   199 	    RMdsStatement* iNotFinishFindQuery;
       
   200 	};
       
   201 
       
   202 const TInt KDoNothing = 99;
       
   203 
       
   204 /**
       
   205 * Transaction class
       
   206 */
       
   207 class RMdSTransaction
       
   208     {
       
   209     public:
       
   210 
       
   211         /**
       
   212         * constructor
       
   213         * creates a transaction to given connection
       
   214         */
       
   215         inline RMdSTransaction( CMdSSqLiteConnection& aConnection )
       
   216             : iConnection( aConnection )
       
   217             {
       
   218             TRAP( iErrorState, iConnection.TransactionBeginL() );
       
   219             }
       
   220 
       
   221         /**
       
   222         * cleanup method (from TBase)
       
   223         * according to status does Commit/Rollback/nothing
       
   224         */
       
   225         inline void Close()
       
   226             {
       
   227             if ( iErrorState != KErrNone )
       
   228                 {
       
   229                 TRAP( iErrorState, iConnection.TransactionRollbackL() );
       
   230                 }
       
   231             }
       
   232 
       
   233         /**
       
   234         * Commits the transaction explicitly
       
   235         * - possibility to receive leave
       
   236         * eliminates actions on destructor.
       
   237         */
       
   238         inline void CommitL()
       
   239             {
       
   240             iConnection.TransactionCommitL();
       
   241             Reset();
       
   242             }
       
   243 
       
   244         /**
       
   245         * Rolls back the transaction explicitly
       
   246         * - possibility to receive leave
       
   247         * eliminates actions on destructor.
       
   248         */
       
   249         inline void RollbackL()
       
   250             {
       
   251             iConnection.TransactionRollbackL();
       
   252             Reset();
       
   253             }
       
   254 
       
   255         /**
       
   256         * Resets the transaction
       
   257         * (does not commit or rollback or do anything)
       
   258         */
       
   259         inline void Reset()
       
   260             {
       
   261             iErrorState = KDoNothing;
       
   262             }
       
   263 
       
   264         /**
       
   265         * error within transaction
       
   266         */
       
   267         inline TInt Error()
       
   268             {
       
   269             return iErrorState;
       
   270             }
       
   271 
       
   272     protected:
       
   273 
       
   274 
       
   275         /** the connection */
       
   276     	CMdSSqLiteConnection& iConnection;
       
   277 
       
   278         /** transaction begin error */
       
   279         TInt iErrorState;
       
   280     };
       
   281 
       
   282 /**
       
   283 * An open statement stack helper
       
   284 * ensures a statement gets terminated
       
   285 */
       
   286 class RMdsStatement
       
   287     {
       
   288     friend class CMdSSqLiteConnection;
       
   289     public:
       
   290         /** constructor */
       
   291         inline RMdsStatement( )
       
   292             : iPrepared( EFalse )
       
   293             {
       
   294             }
       
   295 
       
   296         inline void RMdsStatement::Close()
       
   297             {
       
   298             iStatement.Close();
       
   299             }
       
   300     private:
       
   301 
       
   302 		/** SQL statement */
       
   303 		RSqlStatement iStatement;
       
   304 		
       
   305         /** if statement was already prepared */
       
   306         TBool iPrepared;
       
   307     };
       
   308 
       
   309 /*
       
   310 
       
   311   how to use the statement stack helper:
       
   312 
       
   313   {
       
   314   RMdsStatement st( connection );
       
   315   CleanupClosePushL( st );
       
   316   connection.ExecuteL( clause, variables1, st ); // prepared and executed
       
   317   connection.ExecuteL( clause, variables2, st ); // executed normally
       
   318   CleanupStack::PopAndDestroy( st );
       
   319   } // statement automatically terminated
       
   320 
       
   321   {
       
   322   RMdsStatement st( connection );
       
   323   CleanupClosePushL( st );
       
   324   connection.ExecuteQueryL( query, st, variables1 );
       
   325   connection.NextRowL( st, results1 ); // fetch a result row
       
   326   connection.ExecuteQueryL( query, st, variables2 ); // query with different variable set
       
   327   connection.NextRowL( st, results2 ); // fetch another resut row
       
   328   CleanupStack::PopAndDestroy( st );
       
   329   } // statement automatically terminated
       
   330 
       
   331 */
       
   332 	
       
   333 #endif	// __MDSSQLITECONNECTION_H__