messagingfw/msgsrvnstore/server/inc/msvdbadapter.h
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2007-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 __MSVDBADAPTER_H__
       
    17 #define __MSVDBADAPTER_H__
       
    18 
       
    19 #include <msvstd.h>
       
    20 #include <msvids.h>
       
    21 #include <msvuids.h>
       
    22 #include <sqldb.h>
       
    23 #include <msvuids.h>
       
    24 #include "msvcacheentry.h"
       
    25 #include <msvsearchsortquery.h>
       
    26 #include "msvsearchsortconstants.h"
       
    27 
       
    28 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
    29 #include "msvmessagedbadapter.h"
       
    30 #endif
       
    31 
       
    32 /**
       
    33  * CONSTANT DEFINITIONS
       
    34  */
       
    35 #define KMaxNumberOfDrives 8
       
    36 
       
    37 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
    38 class TStatusMtmIdTable;
       
    39 class TConversionResumeStatus;
       
    40 #endif
       
    41 
       
    42 /**
       
    43 @internalComponent
       
    44 @prototype
       
    45 */
       
    46 
       
    47 NONSHARABLE_CLASS (CMsvDBAdapter) : public CBase
       
    48 	{
       
    49 public:
       
    50 	~CMsvDBAdapter();
       
    51 	void BeginTransactionL();
       
    52 	void CommitTransactionL();
       
    53 	void RollbackTransactionL();
       
    54 	void CreateEntryL(const TMsvEntry& aNewEntry, const TMsvId aImmediateVisibleFolder);
       
    55 	void UpdateEntryL(const TMsvEntry& aEntry, const TMsvId aImmediateVisibleFolderconst, TBool aIsParentIdUpdated = ETrue);
       
    56 	void GetEntryL(TMsvId aId, CMsvCacheEntry*& aEntry, TMsvId& aVisibleParentEntryId);
       
    57 	void GetChildrenL(TMsvId aParentId, RPointerArray<CMsvCacheEntry>& aChildArray);
       
    58 	void GetChildrenIdL(TMsvId aParentId, CMsvEntrySelection& aChildArray);
       
    59 	void GetChildrenL(const TDesC8& aQueryStr, TMsvId aParentId, RPointerArray<CMsvCacheEntry>& aChildArray);
       
    60 	void DeleteEntryL(TMsvId aEntryId);
       
    61 	void DeleteEntryL(const CMsvEntrySelection& aEntryIdList);
       
    62 	void GetVisibleFolderIdL(TMsvId aParentId, TMsvId& aVisibleFolderId);
       
    63 	TBool EntryExistsL(TMsvId aId);
       
    64 	TBool GetNextSiblingL(TMsvId aId,TMsvId aParentId,TMsvId& aNextSiblingId);
       
    65 	TBool GetFirstChildIdL(TMsvId aParentId,TMsvId& aFirstChild);
       
    66 	TBool GetVisibleFlagL(TMsvId aEntryId);
       
    67 	void UpdateVisibleFolderL(CMsvEntrySelection* aEntryList, TMsvId aNewVisibleFolderId);
       
    68 	void UpdateOwnerStatusL(TMsvId aId, const TMsvEntry& aParent, TBool aSet);
       
    69 	inline RSqlDatabase& GetDbPointer();
       
    70 	
       
    71 
       
    72 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
    73 	static CMsvDBAdapter* NewL();	
       
    74 	void DetachDBL(TUint aDriveId);
       
    75 	inline TBool IsVisibleFolder(TInt32 aData);
       
    76 	static void ValidateDatabaseL(TDriveNumber aDriveNumber);	
       
    77 	void GetInPreparationIdL(CMsvEntrySelection& aSelection, TUint aDriveId=0);
       
    78 	
       
    79 	#if (defined SYMBIAN_MESSAGESTORE_UNIT_TESTCODE)
       
    80 		static void CreateDBL(TDriveNumber aDriveNumber, TBool aCreateVersionTable=ETrue, TBool aInvalid = EFalse);
       
    81 	#else
       
    82 		static void CreateDBL(TDriveNumber aDriveNumber);
       
    83 	#endif
       
    84 	#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
    85 		static CMsvDBAdapter* OpenL(const TFileName& aDbFileName);
       
    86 		void ConstructOpenDBL(const TFileName& aDbFileName);
       
    87 		void InitializeLConverterStatementL();
       
    88 		void CreateConversionStatusTableL();
       
    89 		void RemoveConversionStatusTableL();
       
    90 		void GetConversionStatusL(TConversionResumeStatus& aConversionResumeStatus);
       
    91 		void CreateIndexEntryL(const TMsvEntry& aNewEntry, const TMsvId aImmediateVisibleFolder);
       
    92 		void AddConversionStatusEntryL(TConversionResumeStatus& aConversionResumeStatus);
       
    93 		void UpdateConversionStatusEntryL(TConversionResumeStatus& aConversionResumeStatus);
       
    94 		void RemoveConversionStatusEntriesL();
       
    95 		void ChangeVersionL(TInt aVersion);
       
    96 		void GetAllMessageIdsL(TMsvId aServiceId,RArray<TMsvId>& aIdArray,TInt32 aMtm,TMsvId lastId = 0);
       
    97 		void GetAllMimeIdsL(TMsvId aId, RArray<TMsvId>& aIdArray);
       
    98 		void GetOwningServiceL(TMsvId aId, TMsvId& aOwningId);
       
    99 		void GetAllServiceIDsForMtmL(TInt32 aMtm, RArray<TMsvId>& aIdArray);
       
   100 		void GetAllIndexEntryTableIdsL(RArray<TMsvId>& aIdArray);
       
   101 		void UpdateVisibleFolderIdL(TMsvId aId, TMsvId visibleId);
       
   102 		TInt GetMessageCountL(TMsvId aServiceId);
       
   103 		void GetRemainingServiceIdsL(RArray<TMsvId>& aIdArray);
       
   104 		void GetVisibleParentL(TMsvId aId, TMsvId& aOwningId);
       
   105 		void GetVisibleParentIdL(TMsvId aParentId, TMsvId& aVisibleFolderId);
       
   106 		TBool GetParentVisibleFlagL(TMsvId parentId);
       
   107 		void GetRemainingIdsL(RArray<TMsvId>& aIdArray);
       
   108 		void DeleteTableL(TUid aMtmId);
       
   109 		void DeleteSearchSortTable();
       
   110 	#endif
       
   111 #else		// #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   112 	static CMsvDBAdapter* NewL(const TFileName& aDbFileName);
       
   113 	static CMsvDBAdapter* OpenL(const TFileName& aDbFileName);
       
   114 	static TInt DeleteDB(const TFileName& aDbFileName);	
       
   115 	void GetMaxTMsvIdL(TMsvId& aMaxTMsvId);
       
   116 	void GetInPreparationIdL(CMsvEntrySelection& aSelection);
       
   117 	inline TInt Size();
       
   118 	static void ValidateDatabaseL(const TFileName& aDbFile);
       
   119 #endif		// #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   120 
       
   121 	void SearchTMsvIdL(const TDesC8& aQuery, RArray<TMsvId>& aIdSelection);
       
   122 	TInt AddEntriesfromIndexTableL(const TDesC8& aQuery);
       
   123 	void CreateQueryEntryL(const TInt& aQueryId,const TMsvIdWithSortField& aEntry);
       
   124 	void GetSearchSortEntriesL(const TInt& aQueryId, RArray<TMsvId>& aEntrySelection, const TBool aOrder = ETrue,const TBool aSortAsInt = EFalse);
       
   125 	void GetLastNTMsvIdsfromTableL(const TInt& aQueryId, RArray<TMsvId>& aIdResult, TInt aLastCount, TBool aOrder = EFalse, TBool aSortAsInt = EFalse);
       
   126 	void DeleteTMsvIdFromQueryL(const TInt& aQueryId,const TMsvId& aId);
       
   127 	void DeleteTMsvIdFromAllQueriesL(const TMsvId& aIdSelection);
       
   128 	void DeleteQueryFromTableL(const TInt& aQueryId);
       
   129 	TInt ReturnIdCountInDBL(const TInt& aQueryId);
       
   130 	TInt GetNextL(TMsvId& aId);
       
   131 	TInt GetPrevL(TMsvId& aId);
       
   132 	void GetFolderIdsL(const TMsvId aParentId, RArray<TMsvId>& aFolderIdList);
       
   133 	void ReturnIdsInIteratorL(const TInt& aQueryId, const TBool aOrder = ETrue,const TBool aSortAsInt = EFalse, const TInt aLimit = 1);
       
   134 	void ClearDBContentsL();
       
   135 	void ConstructSortTableL();
       
   136 	void InitializeSortTableL();
       
   137 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)	
       
   138 	void GetMtmIdsL(const TDesC8& aQuery, RArray<TUid>& aMtmIdArray);
       
   139 	void AttachDBL(TDriveUnit aDriveUnit, TUint aDriveId, TMsvId& aMaxId, CMsvMessageDBAdapter* iMessageDBAdapter = NULL);
       
   140 #else
       
   141 	void AttachDBL(TDriveUnit aDriveUnit, TUint aDriveId, TMsvId& aMaxId);	
       
   142 #endif		// #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)	
       
   143 
       
   144 private:
       
   145 
       
   146 	CMsvDBAdapter();
       
   147 	void InitializeL();
       
   148 	void LoadEntryFromStatementL(RSqlStatement& aStmt, TInt aIndex, TMsvId& aVisibleParentEntryId, CMsvCacheEntry& aEntry);
       
   149 	
       
   150 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   151 	void ConstructL();
       
   152 	TMsvId MaxTMsvIdL(TUint aDriveId);
       
   153 	static void CreateRootEntryL(RSqlDatabase& tempDB);
       
   154 	void DoGetInPreparationIdL(CMsvEntrySelection& aSelection, TUint aDriveId);
       
   155 #else
       
   156 	void ConstructNewL(const TFileName& aDbFileName, TBool& aIsDBCreated);
       
   157 	void ConstructOpenL(const TFileName& aDbFileName);
       
   158 #endif		// #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   159 	
       
   160 
       
   161 
       
   162 // DATA		
       
   163 private:
       
   164 
       
   165 
       
   166 	enum iStatementType
       
   167 		{
       
   168 		ECreateEntry = 0,
       
   169 		EUpdateEntry,			
       
   170 		EUpdateEntryNoParent,
       
   171 		EGetEntry,
       
   172 		EGetChildEntries,
       
   173 		EGetVisibleId,
       
   174 		EEntryExists,
       
   175 		EGetChildIds,
       
   176 		EIndexSentinel
       
   177 		};
       
   178 		
       
   179 	enum
       
   180 		{
       
   181 		KMsvEntryHighPriority=			0x00000002,
       
   182 		KMsvEntryMediumPriority=		0x00000001,
       
   183 		KMsvEntryLowPriority=			0x00000000,
       
   184 		KMsvEntryNewFlag=				0x00000010,
       
   185 		KMsvEntryUnreadFlag=			0x00000020,
       
   186 		};
       
   187 
       
   188 	RSqlDatabase iDatabase;
       
   189 	TBool isTransactionOpen;
       
   190 	RSqlStatement iStatement[EIndexSentinel];
       
   191 
       
   192 	// column indices
       
   193 	TInt iIdIndex[EIndexSentinel];
       
   194 	TInt iParentIdIndex[EIndexSentinel];
       
   195 	TInt iDataIndex[EIndexSentinel];
       
   196 	TInt iPcSyncCountIndex[EGetVisibleId];
       
   197 	TInt iReservedIndex[EGetVisibleId];
       
   198 	TInt iRelatedIdIndex[EGetVisibleId];
       
   199 	TInt iTypeIndex[EIndexSentinel];
       
   200 	TInt iDateIndex[EGetVisibleId];
       
   201 	TInt iSizeIndex[EGetVisibleId];
       
   202 	TInt iErrorIndex[EGetVisibleId];
       
   203 	TInt iBiotypeIndex[EGetVisibleId];
       
   204 	TInt iMtmData1Index[EGetVisibleId];
       
   205 	TInt iMtmData2Index[EGetVisibleId];
       
   206 	TInt iMtmData3Index[EGetVisibleId];
       
   207 	TInt iServiceIdIndex[EGetVisibleId];
       
   208 	TInt iMtmIdIndex[EGetVisibleId];
       
   209 	TInt iDescriptionIndex[EGetVisibleId];
       
   210 	TInt iDetailsIndex[EGetVisibleId];	
       
   211 	TInt iVisibleParent[EEntryExists];
       
   212 	
       
   213 	enum
       
   214 		{
       
   215 		EQueryCreateEntry = 0,
       
   216 		EQueryGetEntryAsc,
       
   217 		EQueryGetEntryDesc,
       
   218 		EQueryGetEntryNumAsc,
       
   219 		EQueryGetEntryNumDesc,
       
   220 		EQueryGetLastNIdsAsc,
       
   221 		EQueryGetLastNIdsDesc,
       
   222 		EQueryGetLastNIdsNumAsc,
       
   223 		EQueryGetLastNIdsNumDesc,
       
   224 		EQueryCount,
       
   225 		EGetFolderIds,
       
   226 		EQueryIteratorNumAsc,
       
   227 		EQueryIteratorNumDesc,
       
   228 		EQueryIteratorAsc,
       
   229 		EQueryIteratorDesc,
       
   230 		EQuerySentinel
       
   231 		};
       
   232 	
       
   233 	RSqlStatement iQueryStatement[EQuerySentinel];
       
   234 	
       
   235 	// column indices
       
   236 	TInt iQIdQueryIndex[EQuerySentinel];
       
   237 	TInt iIdQueryIndex[EQuerySentinel];
       
   238 	TInt iSortfieldQueryIndex[EQuerySentinel];
       
   239 	TInt iRowIdIndex[EQuerySentinel];
       
   240 	TInt iIterOffset[EQuerySentinel];
       
   241 	TInt iIterLimit[EQuerySentinel];
       
   242 	TInt iParentIdfield;
       
   243 	TInt iTMsvidfield;
       
   244 	TInt iIdColumnIndex;
       
   245 	
       
   246 	// Iterator Variables
       
   247 	TInt iOffsetValue;
       
   248 	TInt iIterQuery;
       
   249 	TInt iQueryId;
       
   250 	TInt iLimit;
       
   251 	
       
   252 	/**************** Converter Statements ************************/
       
   253 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)	
       
   254 	enum
       
   255 	{
       
   256 	ECreateConversionStatusEntry = 0,
       
   257 	EUpdateConversionStatusEntry	
       
   258 	};
       
   259 
       
   260 #endif	
       
   261 
       
   262 
       
   263 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   264 	//Data structure to store maximum Ids on a given driveId.
       
   265 	TBool iDbPtrInitialized;
       
   266 	RArray<TBool>* iDatabasePresent;
       
   267 #endif
       
   268 
       
   269 	friend class TSearchSortDbWrapper;
       
   270 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   271 	friend class CMsvMessageDBAdapter;
       
   272 	friend class CMessageConverterManager;
       
   273 #endif
       
   274 
       
   275 #if (defined SYMBIAN_MESSAGESTORE_UNIT_TESTCODE)
       
   276 	friend class CTestDbAdapter;
       
   277 	friend class CTestSearchSortDBadapter;
       
   278 	friend class CTestVersion0Version1Converter;
       
   279 	friend class CTestVersion1Version2Converter;
       
   280 	friend class CTestConverterDBAdapter;
       
   281 	friend class CTestSmsMessageV1toV2Converter;
       
   282 #endif
       
   283 	};
       
   284 	
       
   285 	
       
   286 
       
   287 inline RSqlDatabase& CMsvDBAdapter::GetDbPointer()
       
   288 	{
       
   289 	return iDatabase;
       
   290 	}
       
   291 	
       
   292 	
       
   293 /**
       
   294  * Local Functions
       
   295  */
       
   296 
       
   297 
       
   298 //Get the index of the given parameter name
       
   299 inline TInt GetParamIndexL(RSqlStatement& aStatement, const TDesC& aParameterName)
       
   300 	{
       
   301 	TInt index = aStatement.ParameterIndex(aParameterName);
       
   302 	User::LeaveIfError(index);
       
   303 	return index;
       
   304 	}
       
   305 
       
   306 
       
   307 
       
   308 
       
   309 //Get the index of the given column name
       
   310 inline TInt GetColumnIndexL(RSqlStatement& aStatement, const TDesC& aColumnName)
       
   311 	{
       
   312 	TInt index = aStatement.ColumnIndex(aColumnName);
       
   313 	User::LeaveIfError(index);
       
   314 	return index;
       
   315 	}
       
   316 
       
   317 
       
   318 
       
   319 
       
   320 //Binds the given value with the index given in the sql statement
       
   321 inline void BindIntL(RSqlStatement& aStatement, TInt aIndex, TInt32 aId)
       
   322 	{
       
   323 	User::LeaveIfError(aStatement.BindInt(aIndex, aId));
       
   324 	}
       
   325 
       
   326 
       
   327 
       
   328 
       
   329 //Binds the given value with the index given in the sql statement
       
   330 inline void BindInt64L(RSqlStatement& aStatement, TInt aIndex, TInt64 aId)
       
   331 	{
       
   332 	User::LeaveIfError(aStatement.BindInt64(aIndex, aId));
       
   333 	}
       
   334 
       
   335 
       
   336 
       
   337 
       
   338 inline void BindTextL(RSqlStatement& aStatement,TInt aIndex, const TDesC& aParameterText)
       
   339 	{
       
   340 	User::LeaveIfError(aStatement.BindText(aIndex,aParameterText));	
       
   341 	}
       
   342 
       
   343 
       
   344 
       
   345 
       
   346 //Gets the value of Int32 column
       
   347 inline TInt ColumnInt(RSqlStatement& aStatement, TInt aIndex)
       
   348 	{
       
   349 	return aStatement.ColumnInt(aIndex);
       
   350 	}
       
   351 
       
   352 
       
   353 
       
   354 //Gets the value of Int64 column
       
   355 inline TInt64 ColumnInt64(RSqlStatement& aStatement, TInt aIndex)
       
   356 	{
       
   357 	return aStatement.ColumnInt64(aIndex);
       
   358 	}
       
   359 
       
   360 
       
   361 
       
   362 
       
   363 //Get the value of text column
       
   364 inline HBufC* ColumnTextL(RSqlStatement& aStatement, TInt aIndex)
       
   365 	{
       
   366 	TInt size = aStatement.ColumnSize(aIndex);
       
   367 	HBufC* data = HBufC::NewL(size); 
       
   368 
       
   369 	TPtr ptrData(data->Des());
       
   370 	TInt err = aStatement.ColumnText(aIndex,ptrData);
       
   371 
       
   372 	if(err != KErrNone)
       
   373 		{
       
   374 		delete data;
       
   375 		User::Leave(err);
       
   376 		}
       
   377 	return data;	
       
   378 }
       
   379 
       
   380 	
       
   381 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   382 	
       
   383 
       
   384 inline TBool CMsvDBAdapter::IsVisibleFolder(TInt32 aData)
       
   385 	{
       
   386 	TMsvEntry dummy;
       
   387 	dummy.iData = aData;
       
   388 	return dummy.Visible();
       
   389 	}
       
   390 	
       
   391 #else		// #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   392 
       
   393 	
       
   394 	
       
   395 inline TInt CMsvDBAdapter::Size()
       
   396 	{
       
   397 	return iDatabase.Size();
       
   398 	}
       
   399 
       
   400 #endif 		// #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   401 
       
   402 #endif		// #ifndef __MSVDBADAPTER_H__