persistentstorage/sql/SRC/Server/SqlSrvDatabase.h
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #ifndef __SQLSRVDATABASE_H__
       
    17 #define __SQLSRVDATABASE_H__
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <sqldb.h>				//RSqlDatabase::TIsolationLevel
       
    21 #include "SqlSrvSecurityMap.h"	
       
    22 #include "sqlite3.h"
       
    23 #include "SqliteSymbian.h"		//sqlite3SymbianLastOsError()
       
    24 #include "SqlSrvStatementUtil.h"//Global sql statement related functions
       
    25 #include "SqlSrvStrings.h"
       
    26 #include "SqlSrvCollation.h"	//User defined collations
       
    27 #include "SqlSrvDbSysSettings.h"//TSqlDbSysSettings
       
    28 #include "SqlSecurityImpl.h"	//CSqlSecurityPolicy
       
    29 #include "SqlUtil.h"			//TSqlLikeWildcard
       
    30 
       
    31 //Forward declarations
       
    32 class TSqlSrvFileData;
       
    33 struct TSqlSrvConfigParams;
       
    34 
       
    35 //////////////////////////////////////////////////////////////////////////////////////////////////////
       
    36 /////////////////////////////   CSqlSrvDatabase class    /////////////////////////////////////////////
       
    37 //////////////////////////////////////////////////////////////////////////////////////////////////////
       
    38 
       
    39 /**
       
    40 SQL database connection manager. (SQL database handle)
       
    41 
       
    42 CSqlSrvDatabase is a server side class which processes the client side requests for:
       
    43 - creating a SQL database (CSqlSrvDatabase::Create());
       
    44 - opening an existing SQL database (CSqlSrvDatabase::Open());
       
    45 - retrieving the last SQL error message (CSqlSrvDatabase::LastErrorMessage());
       
    46 - executing 8-bit and 16-bit SQL statements (CSqlSrvDatabase::ExecL());
       
    47 - setting the transaction isolation level (CSqlSrvDatabase::SetIsolationLevelL());
       
    48 - retrieving the database security policy (CSqlSrvDatabase::SecurityPolicy());
       
    49 - attaching an existing SQL database to current connection (CSqlSrvDatabase::AttachDbL());
       
    50 - detaching previously attached SQL database from current connection (CSqlSrvDatabase::DetachDbL());
       
    51 
       
    52 CSqlSrvDatabase class also manages all platform security related activities for secure databases:
       
    53 - loading/storing database security policies when creating/opening a database connection;
       
    54 - asserting client rights to perform a specific database operation;
       
    55 - maintaining security related internal data structures in tact;
       
    56 
       
    57 @see CSqlSrvDatabase::Create()
       
    58 @see CSqlSrvDatabase::Open()
       
    59 @see CSqlSrvDatabase::LastErrorMessage()
       
    60 @see CSqlSrvDatabase::ExecL()
       
    61 @see CSqlSrvDatabase::SetIsolationLevelL()
       
    62 @see CSqlSrvDatabase::SecurityPolicy()
       
    63 @see CSqlSrvDatabase::AttachDbL()
       
    64 @see CSqlSrvDatabase::DetachDbL()
       
    65 @see CSqlSrvDatabase::LastChangesCount()
       
    66 
       
    67 @internalComponent
       
    68 */
       
    69 NONSHARABLE_CLASS(CSqlSrvDatabase) : public CBase
       
    70 	{		
       
    71 public:
       
    72 	//Object creation methods
       
    73 	static CSqlSrvDatabase* CreateSecureL(const TSqlSrvFileData& aFileData, CSqlSecurityPolicy* aSecurityPolicy);
       
    74 	static CSqlSrvDatabase* CreateL(const TSqlSrvFileData& aFileData);
       
    75 	static CSqlSrvDatabase* OpenL(const TSqlSrvFileData& aFileData);
       
    76 	virtual ~CSqlSrvDatabase();
       
    77 	//Interface methods
       
    78 	inline TPtrC LastErrorMessage() const;
       
    79 	inline void ExecL(TDes16& aSqlStmt);
       
    80 	inline void ExecL(const TDesC8& aSqlStmt);
       
    81 	inline void SetIsolationLevelL(RSqlDatabase::TIsolationLevel aLevel);
       
    82 	inline sqlite3* RawDbHandle() const;
       
    83 	inline const CSqlSecurityPolicy* SecurityPolicy() const;
       
    84 	void AttachDbL(const TSqlSrvFileData& aFileData, const TDesC& aDbName);
       
    85 	void DetachDbL(const TDesC& aDbName);
       
    86 	inline TInt LastChangesCount();
       
    87 	inline TInt64 LastInsertedRowId();
       
    88 	inline TBool InTransaction() const;
       
    89 	TInt64 SizeL(const TDesC& aDbName = KNullDesC);
       
    90 	TInt64 FreeSpaceL(const TDesC& aDbName = KNullDesC);
       
    91 	void QueryConfigL(TDes8& aDest);
       
    92 	TInt CompactL(TInt aSize, const TDesC& aDbName = KNullDesC);
       
    93 	static TInt AuthorizeCallback(void* aDb, TInt aDbOpType, 
       
    94 						   		  const char* aDbObjName1, const char* aDbObjName2, 
       
    95 						   		  const char* aDbName, const char* aTrgOrViewName);
       
    96 
       
    97 private:
       
    98 	CSqlSrvDatabase();
       
    99 	void CreateNewDbFileL(const TSqlSrvFileData& aFileData);
       
   100 	void OpenExistingDbFileL(const TSqlSrvFileData& aFileData);
       
   101 	void InstallAuthorizerL();
       
   102 	inline void InstallCollationsL();
       
   103 	void InstallUDFsL();
       
   104 	const TUint8* SecurityMapKeyL(const TDesC& aDbFileName);
       
   105 	void UpdateSecurityMapL(TBool aAttachedDb, const TSqlSrvFileData& aFileData, 
       
   106 							const TUint8*& aMapKey, const CSqlSecurityPolicy*& aSecurityPolicy);
       
   107 	void RemoveFromMapsL(const TDesC& aDbName);
       
   108 	void InsertInAttachDbMapL(const TDesC& aDbFileName, const TDesC& aDbName);
       
   109 	void InitAttachedDbL(const TSqlSrvFileData& aFileData, const TDesC& aDbName);
       
   110 	TInt FinalizeAttachedDb(const TDesC& aDbName);
       
   111 	static void AttachCleanup(void* aCleanup);
       
   112 	enum TAttachState {EAStNone, EAStDbAttached, EAStSecurityMapUpdated};
       
   113 	void DoAttachSecureDbL(TAttachState& aState, const TSqlSrvFileData& aFileData, const TDesC& aDbName, const TUint8*& aMapKey);
       
   114 	inline void BasicSecurityPolicyCheckL(const CSqlSecurityPolicy& aSecurityPolicy);
       
   115 	inline void StoreSettingsL(const TDesC& aCollationDllName, TInt aDbConfigFileVersion, TSqlCompactionMode aCompactionMode);
       
   116 	void ProcessSettingsL(const TSqlSrvFileData& aFileData, const TDesC& aDbName);
       
   117 	void ApplyConfigUpdatesL(const TDesC& aStoredCollationDllName, const TInt& aStoredDbConfigFileVersion, 
       
   118 							 const TSqlSrvFileData& aFileData, const TDesC& aDbName);
       
   119 	void SetConfigL(const TSqlSrvConfigParams& aConfigParams, TBool aSetPageSize, const TDesC& aLogicalDbName = KNullDesC);
       
   120 	void InitCompactionL(TSqlCompactionMode aCompactionMode, TInt aFreePageThresholdKb, 
       
   121 						 const TDesC& aDbFileName, TSqliteVacuumMode aCurrentVacuumMode, const TDesC& aDbName = KMainDb16);
       
   122 	void NewCompactEntryL(TInt aFreePageThresholdKb, const TDesC& aDbFileName, const TDesC& aDbName);
       
   123 	void ReleaseCompactEntry(const TDesC& aDbName);
       
   124 	static void CompactCleanup(void* aCleanup);
       
   125 	TInt PageSizeL(const TDesC& aDbName = KNullDesC);
       
   126 	//ConstructL() methods
       
   127 	void ConstructCreateSecureL(const TSqlSrvFileData& aFileData, CSqlSecurityPolicy* aSecurityPolicy);
       
   128 	void ConstructCreateL(const TSqlSrvFileData& aFileData);
       
   129 	void DoCommonConstructCreateL(const TSqlSrvFileData& aFileData, TBool aSecureDb);
       
   130 	void ConstructOpenSecureL(const TSqlSrvFileData& aFileData);
       
   131 	void ConstructOpenL(const TSqlSrvFileData& aFileData);
       
   132 	void DoCommonConstructOpenL(const TSqlSrvFileData& aFileData, TBool aSecureDb);
       
   133 	//
       
   134 	static void LikeSqlFunc(sqlite3_context* aContext, int aArgc, sqlite3_value** aArgv);
       
   135 	
       
   136 private:
       
   137 	sqlite3*				iDbHandle;//The database handle, owned by CSqlSrvDatabase.
       
   138 	const CSqlSecurityPolicy* iSecurityPolicy;//The main database security policy, not owned by CSqlSrvDatabase (owned by the CSqlServer::iSecurityMap object).
       
   139 	TUint8 					iFileNameBuf[KMaxFileName + 1];//Temporary buffer for storing UTF8 encoded, zero terminated database names.
       
   140 	TBool					iAuthorizerInstalled;//If non-zero then the authorizer callback is already installed.
       
   141 												 //Fact: the authorizer callback must be installed for each 
       
   142 												 //secure CSqlSrvDatabase object. But it is possible that
       
   143 												 //      - there is one secure database object;
       
   144 												 //		 - a second secure database is attached to the first one.
       
   145 												 //		   in this case there is no need to install the authorizer again.
       
   146 												 //
       
   147 												 //Or there is another situation: non-secure main database + secure attached database.
       
   148 	TBool					iAuthorizerDisabled; //See the comments for iAuthorizerInstalled.
       
   149 												 //This flag is used only when attaching a secure database.
       
   150 												 //Since during this operation the system tables are read/writen,
       
   151 												 //The authorizer callback has to be temporary disabled.
       
   152 	RSqlAttachDbMap			iAttachDbMap;//["dbName":"dbFileName"] map, owned by CSqlSrvDatabase.
       
   153 	const TUint8*			iSecureDbName;//<Drive:><Name[.Ext]>, used as a key in the security map. Not owned by CSqlSrvDatabase.
       
   154 	RSqlCompactDbMap		iCompactDbMap; //["dbName":"Compact db entry"] map, owned by CSqlSrvDatabase.
       
   155 	TInt					iPageSize;	//Main database page size in bytes
       
   156 	
       
   157 	};
       
   158 
       
   159 #include "SqlSrvDatabase.inl"
       
   160 
       
   161 #endif//__SQLSRVDATABASE_H__