Update contrib.
1 // Copyright (c) 1998-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // DBMS Client/Server architecture
26 #include "Sd_DbProps.h"
27 #include "Sd_PolicyProxy.h"
28 #include "Sd_DriveSpace.h"
30 // other classes referenced
38 class CDbsIncremental;
62 EDbsUnimplemented, //0
69 EDbsStreamMarkInvalid,
70 EDbsStreamLocationInvalid,
71 EDbsObserverRequestPending,
79 GLREF_C void Panic(TDbsPanic aPanic);
84 GLREF_C void ExternalizeL(const TDbCol& aCol,RWriteStream& aStream);
85 GLREF_C void ExternalizeL(const CDbColSet& aColSet,RWriteStream& aStream);
86 GLREF_C void InternalizeL(CDbColSet& aColSet,RReadStream& aStream);
87 GLREF_C void ExternalizeL(const CDbKey& aKey,RWriteStream& aStream);
88 GLREF_C void InternalizeL(CDbKey& aKey,RReadStream& aStream);
89 GLREF_C void ExternalizeL(const CDbNames& aNames,RWriteStream& aStream);
90 GLREF_C void InternalizeL(CDbNames& aNames,RReadStream& aStream);
91 GLREF_C void ExternalizeL(const CDbStrings& aNames,RWriteStream& aStream);
92 GLREF_C void InternalizeL(CDbStrings& aNames,RReadStream& aStream);
97 _LIT(KDbsServerName,"!DBMS server");
98 _LIT(KDbsServerImg,"EDBSRV"); // DLL/EXE name
99 const TUid KDbsServerUid3={0x100012a5};
105 const TInt KDbsExitDelay=4*0x100000; // ~4 seconds
106 const TInt KDbsLeavePanic=~(1<<20);
107 const TInt KTablePolicyMaskBit = 1024; //Used in RDbs::Get<Obj>Policy() methods
113 enum TDbsType {EDbsFree,EDbsDatabase,EDbsIncremental,EDbsCursor,EDbsConstraint,EDbsStream,EDbsObserver,EDbsMaxType=EDbsObserver,EDbsSession=EDbsFree};
116 Make sure that KDbsFunc2SecurityPolicyMask array in SD_PolicyProxy.cpp gets updated if
117 new enum items are going to be added to TDbsFunction.
123 EDbsResourceMark, //0
130 // Database functions
131 EDbsDatabaseAuthenticate, //Not used in SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version
135 EDbsDatabaseRollback, //10
136 EDbsDatabaseProperty,
137 EDbsDatabaseCreateTable,
142 EDbsDatabaseOpenObserver,
143 EDbsDatabaseOpenUtility,
144 EDbsDatabaseOpenDropTable,
145 EDbsDatabaseOpenAlterTable, //20
146 EDbsDatabaseOpenCreateIndex,
147 EDbsDatabaseOpenDropIndex,
149 EDbsDatabasePrepareView,
150 EDbsDatabaseOpenTable,
151 // Observer functions
154 // Incremental functions
157 EDbsCursorColumnTypes,
158 EDbsCursorReset, //30
160 EDbsCursorUnevaluated,
163 EDbsCursorAtBeginning,
168 EDbsCursorBookmark, //40
169 EDbsCursorGotoBookmark,
173 EDbsCursorRetrieveRow,
178 EDbsCursorColumnDef, //50
180 EDbsCursorColumnSize,
181 EDbsCursorColumnSource,
182 EDbsCursorColumnSink,
183 EDbsCursorOpenConstraint,
196 EDbsGetSecurityPolicy,
197 // reserved disk space handling functions
198 EDbsReserveDriveSpace,
199 EDbsFreeReservedSpace,
200 EDbsReserveGetAccess,
201 EDbsReserveReleaseAccess, //70
205 //Insert new functuions here
212 const TInt KDbsTypeBits=4;
213 const TInt KDbsMagicBits=4;
214 const TInt KDbsIndexBits=14;
215 const TInt KDbsFunctionBits=9;
216 const TInt KDbsHandleBits=KDbsTypeBits+KDbsMagicBits+KDbsIndexBits;
217 const TInt KDbsTypeMask=(1<<KDbsTypeBits)-1;
218 const TInt KDbsMagicMask=(1<<KDbsMagicBits)-1;
219 const TInt KDbsHandleMask=(1<<KDbsHandleBits)-1;
220 const TInt KDbsObjectReturn=1<<(KDbsFunctionBits-1);
221 const TInt KDbsIndexLimit=1<<KDbsIndexBits;
226 #define KDbsSessionHandle DbsSessionHandle()
231 inline TInt DbsMessage(TInt aFunction,TInt aHandle);
232 inline TDbsFunction DbsFunction(TInt aMessage);
233 inline TInt DbsHandle(TInt aMessage);
234 inline TDbsType DbsType(TInt aHandle);
235 inline TInt DbsMagic(TInt aHandle);
236 inline TInt DbsObjectIndex(TInt aHandle);
237 inline TInt DbsMakeHandle(TInt aIndex,TInt aMagic,TDbsType aType);
238 inline TInt DbsSessionHandle();
240 #if !defined(_SMALL_BUFFER)
244 const TInt KDbsStreamBufSize=0x600; // 1.5K
245 const TInt KDbsStreamIoSize=0xc00; // 3K
246 const TInt KDbsColumnTypesSize=0x200; // .5K
248 const TInt KDbsStreamBufSize=2;
249 const TInt KDbsStreamIoSize=32;
250 const TInt KDbsColumnTypesSize=2;
259 enum {ESize=KDbsStreamBufSize};
271 enum {EMax=KDbsColumnTypesSize};
284 inline void operator=(const TDesC8& aDes);
285 inline void operator=(const TDesC16& aDes);
286 inline void operator=(TInt aVal);
288 static TPtrC8 PrepareLC(const CDbColSet& aColSet);
289 static TPtrC8 PrepareLC(const CDbKey& aKey);
290 static TPtrC8 PrepareLC(const TDbLookupKey& aKey);
303 typedef TDbsParam TDbsMessage[4];
308 class RDbsObject : private RDbs
312 inline void OpenL(const RDbsObject& aDbs,TDbsFunction aFunction);
313 inline void OpenL(const RDbsObject& aDbs,TDbsFunction aFunction,const TIpcArgs& aArgs);
314 inline void OpenL(const RDbs& aDbs,TDbsFunction aFunction,const TIpcArgs& aArgs);
317 inline TInt Handle() const;
319 TInt SendReceive(TDbsFunction aFunction) const;
320 inline TInt SendReceive(TDbsFunction aFunction,const TIpcArgs& aArgs) const;
321 TInt SendReceiveL(TDbsFunction aFunction) const;
322 inline TInt SendReceiveL(TDbsFunction aFunction,const TIpcArgs& aArgs) const;
323 inline void SendReceive(TDbsFunction aFunction,TRequestStatus& aStatus) const;
324 inline void SendReceive(TDbsFunction aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus) const;
326 inline RDbsObject(const RDbs& aDbs);
327 void SendReceive(TDbsFunction aFunction,const TIpcArgs* aArgs,TRequestStatus& aStatus) const;
328 TInt SendReceive(TDbsFunction aFunction,const TIpcArgs* aArgs) const;
329 TInt SendReceiveL(TDbsFunction aFunction,const TIpcArgs* aArgs) const;
330 void OpenL(const RDbsObject& aDbs,TDbsFunction aFunction,const TIpcArgs* aArgs);
338 NONSHARABLE_CLASS(CDbsDatabase) : public CDbDatabase
340 friend class CDbsSecureDatabase;
342 static CDbsDatabase* NewL(const RDbs& aDbs,const TDesC& aDatabase,const TDesC& aFormat);
346 inline CDbsDatabase();
347 void ConstructL(const RDbs& aDbs,TIpcArgs& aArgs);
353 TInt Property(TProperty aProperty);
354 void CreateTableL(const TDesC& aName,const CDbColSet& aColSet,const CDbKey* aPrimaryKey);
355 void TablesL(CDbTableNames& aNames);
356 void ColumnsL(CDbColSet& aColSet,const TDesC& aName);
357 void IndexesL(CDbIndexNames& aNames,const TDesC& aTable);
358 void KeysL(CDbKey& aKey,const TDesC& aName,const TDesC& aTable);
359 CDbNotifier* OpenNotifierL();
360 CDbIncremental* OpenUtilityL(TUtility aType,TInt& aStep);
361 CDbIncremental* OpenCompactL(TInt& aStep);
362 CDbIncremental* OpenRecoverL(TInt& aStep);
363 CDbIncremental* OpenDropTableL(const TDesC& aTable,TInt& aStep);
364 CDbIncremental* OpenAlterTableL(const TDesC& aTable,const CDbColSet& aNewDef,TInt& aStep);
365 CDbIncremental* OpenCreateIndexL(const TDesC& aName,const TDesC& aTable,const CDbKey& aKey,TInt& aStep);
366 CDbIncremental* OpenDropIndexL(const TDesC& aName,const TDesC& aTable,TInt& aStep);
367 CDbIncremental* OpenExecuteL(const TDesC& aSql,TDbTextComparison aComparison,TInt& aInit);
368 CDbCursor* PrepareViewL(const TDbQuery& aQuery,const TDbWindow& aWindow,RDbRowSet::TAccess anAccess);
369 CDbCursor* OpenTableL(const TDesC& aName,RDbRowSet::TAccess anAccess);
374 //Secure shared database
375 NONSHARABLE_CLASS(CDbsSecureDatabase) : public CDbsDatabase
378 static CDbsSecureDatabase* NewL(const RDbs& aDbs,const TDesC& aDatabase,const TDesC& aFormat);
379 virtual ~CDbsSecureDatabase();
382 inline CDbsSecureDatabase();
383 void ConstructL(const RDbs& aDbs,TIpcArgs& aArgs);
390 NONSHARABLE_CLASS(CDbsNotifier) : public CDbNotifier
393 static CDbsNotifier* NewL(const RDbsObject& aDatabase);
395 inline CDbsNotifier();
398 void Notify(TType aEvent,TRequestStatus& aStatus);
407 NONSHARABLE_CLASS(CDbsIncremental) : public CDbAsyncIncremental
410 static CDbsIncremental* NewL(const RDbsObject& aDbs,TDbsFunction aFunction,TIpcArgs& aArgs,TInt& aStep);
413 inline CDbsIncremental();
414 // from CDbAsyncIncremental
415 void Next(TPckgBuf<TInt>& aStep,TRequestStatus& aStatus);
423 NONSHARABLE_CLASS(CDbsCursor) : public CDbCursor
426 static CDbsCursor* NewL(const RDbsObject& aDbs,TDbsFunction aFunction,TIpcArgs& aArgs);
430 void ConstructL(const RDbsObject& aDbs,TDbsFunction aFunction,TIpcArgs& aArgs);
432 TDbColType Type(TDbColNo aCol) const;
433 void ReplaceBlobL(TDbColumn& aCol);
434 TBool RetrieveL(TDbsFunction aFunction,TInt aArg0=0);
438 void Evaluate(TRequestStatus& aStatus);
440 void SetIndexL(const TDesC* anIndex);
441 TBool SeekL(const TDbLookupKey& aKey,RDbTable::TComparison aComparison);
445 TInt CountL(RDbRowSet::TAccuracy aAccuracy);
446 TBool GotoL(RDbRowSet::TPosition aPosition);
447 void Bookmark(TDbBookmark::TMark& aMark);
448 void GotoL(const TDbBookmark::TMark& aMark);
450 void InsertL(TInsert aClearRow);
456 void ColumnsL(CDbColSet& aColSet);
457 void ColumnDef(TDbCol& aCol,TDbColNo aColNo);
458 TDbColType ColumnType(TDbColNo aCol);
460 TDbColumnC ColumnC(TDbColNo aCol); // non-writeable column
461 TDbColumn Column(TDbColNo aCol); // writeable column
462 void SetNullL(TDbColNo aCol);
463 TInt ColumnSize(TDbColNo aCol);
464 MStreamBuf* ColumnSourceL(TDbColNo aCol);
465 MStreamBuf* ColumnSinkL(TDbColNo aCol);
466 CDbRowConstraint* OpenConstraintL(const TDbQuery& aCriteria);
467 TBool MatchL(CDbRowConstraint& aCursor);
468 TInt FindL(RDbRowSet::TDirection aDirection,const TDbQuery& aCriteria);
470 enum TState {EUnknown,EAtBeginning,EAtEnd,EAtRow,ERetrieve,ERead,EWrite};
475 HDbsColumns* iColumns;
482 NONSHARABLE_CLASS(CDbsConstraint) : public CDbRowConstraint
484 friend class CDbsCursor;
486 inline CDbsConstraint();
495 NONSHARABLE_CLASS(HDbsBuf) : public TStreamBuf
498 static HDbsBuf* NewL(const RDbsObject& aObject,TDbsFunction aFunction,TIpcArgs& aArgs);
499 static HDbsBuf* NewLC(const RDbsObject& aObject,TDbsFunction aFunction,TIpcArgs& aArgs);
500 static HDbsBuf* NewLC(const RDbs& aDbSession, TDbsFunction aFunction, TIpcArgs& aArgs);
501 virtual inline ~HDbsBuf();
504 void ConstructL(const RDbsObject& aObject,TDbsFunction aFunction,TIpcArgs& aArgs);
505 void ConstructL(const RDbs& aDbSession, TDbsFunction aFunction, TIpcArgs& aArgs);
507 TInt UnderflowL(TInt aMaxLength);
511 TInt DoReadL(TAny* aPtr,TInt aMaxLength);
512 void DoWriteL(const TAny* aPtr,TInt aLength);
513 TStreamPos DoSeekL(TMark aMark,TStreamLocation aLocation,TInt anOffset);
515 inline void SetPos(TRead,TInt aPos);
516 inline void SetPos(TWrite,TInt aPos);
517 inline TInt Pos(TRead) const;
518 inline TInt Pos(TWrite) const;
519 inline TInt MovePos(TRead,TInt anOffset);
520 inline TInt MovePos(TWrite,TInt anOffset);
521 TInt IpcReadL(TAny* aPtr,TInt aMaxLength);
522 void IpcWriteL(const TAny* aPtr,TInt aLength);
525 inline TInt Lag(TRead) const;
526 inline TInt Lag(TWrite) const;
527 inline TInt Mark(TRead) const;
528 inline TInt Mark(TWrite) const;
539 NONSHARABLE_CLASS(HDbsReadBuf) : public TMemBuf
542 static HDbsReadBuf* NewL(const TDesC8& aDes);
544 inline HDbsReadBuf(const TDesC8& aDes);
551 NONSHARABLE_CLASS(CDbsObserver) : public CActive
559 friend class CDbsObserver;
562 void Notify(const RMessage2& aMessage);
566 void Complete(TInt aStatus);
567 void Event(TInt aEvent);
571 RMessagePtr2 iMessage;
573 friend class HObserver;
575 static CDbsObserver* NewL(CDbsSource& aSource);
576 static CDbsObserver* Collect(CDbsObserver* aNotifier);
577 HObserver* ObserverL();
579 inline CDbsObserver(CDbsSource& aSource);
588 CDbNotifier* iNotifier;
589 TDblQue<HObserver> iQueue;
595 NONSHARABLE_CLASS(CDbsConnection) : public CDbContext
597 friend class RDbsSources;
599 inline CDbsSource& Source() const;
600 static inline const CDbsConnection& Connection(const CDbObject& aObject);
601 static inline CDbsSource& Source(const CDbObject& aObject);
603 inline CDbsConnection() {}
605 inline void Set(CDbsSource& aSource);
613 class CDbsDatabaseStub : public CDbObject
616 static CDbsDatabaseStub* NewL();
617 CDbDatabase* AuthenticateL();
619 inline CDbsDatabaseStub();
625 NONSHARABLE_CLASS(CDbsSource) : public CBase
627 friend class CDbsConnection;
628 friend class CDbsObserver;
630 static CDbsSource* NewL(RFs& aFs,const TDesC& aSource);
632 static inline TInt LinkOffset();
633 TBool Is(const TDesC& aSource) const;
635 inline CDbSource& Source();
636 CDbsObserver::HObserver* ObserverL();
638 inline CDbsSource(const TDbFormat& aFormat);
647 const TDbFormat& iFormat;
650 CDbsObserver* iObserver;
659 typedef TDblQueIter<CDbsSource> TIter;
661 inline RDbsSources(RDbCache& aCache);
664 CDbsConnection* OpenLC(RFs& aFs,const TDesC& aSource,const TDesC& aFormat);
666 TDblQue<CDbsSource> iSources;
673 NONSHARABLE_CLASS(CDbsServer) : public CServer2
676 static CDbsServer* NewL();
678 inline TDes& Name0();
679 inline TDes& Name1();
680 inline TDes& FileName();
681 inline RDbsSources& Sources();
683 inline RDriveSpaceCol& DriveSpaceCol();
684 void RemoveSession();
686 static CDbsServer* Instance();
687 void Panic(const TDesC& aCategory,TInt aCode); // .. panic client!
689 inline CPolicyProxy& PolicyProxy() const;
690 inline RDbPropsFactory& DbPropsFactory();
697 CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const;
700 enum {EServerPriority=0};
703 RDbsSources iSources;
704 TDbName iName0; // used for message servicing
708 CPolicyProxy* iPolicyProxy;
709 RDbPropsFactory iDbPropsFactory;
710 RDriveSpaceCol iDriveSpaceCol;
716 NONSHARABLE_CLASS(CDbsSession) : public CSession2
719 #if !defined(_SMALL_BUFFER)
720 enum {EIndexGranularity=8};
722 enum {EIndexGranularity=1};
732 const MPolicy* iPolicy;
736 DECLARE_DB_DUMP3(aFile)
738 inline TDbsType Type() const;
739 inline CDbsDatabaseStub& DatabaseStub();
740 inline CDbDatabase& Database();
741 inline CDbIncremental& Incremental();
742 inline CDbCursor& Cursor();
743 inline CDbRowConstraint& Constraint();
744 inline HDbsStream& Stream();
745 inline CDbsObserver::HObserver& Observer();
749 virtual ~CDbsSession();
752 virtual void CreateL();
754 void ServiceL(const RMessage2& aMessage);
755 TInt ExtServiceL(const RMessage2& aMessage, TDbsFunction aDbsFunction);
756 void ServiceError(const RMessage2& aMessage,TInt aError);
757 TInt CountResources();
759 inline CDbsServer& Server() const;
760 inline RDbsSources& Sources();
762 const TDesC& ReadName0L(TInt aIndex,const RMessage2& aMessage);
763 const TDesC& ReadName1L(TInt aIndex,const RMessage2& aMessage);
764 const TDesC& ReadFileNameL(TInt aIndex, const RMessage2& aMessage);
765 HBufC* ReadHBufLC(TInt aIndex,const RMessage2& aMessage);
766 HBufC8* ReadHBuf8LC(TInt aIndex,const RMessage2& aMessage);
767 CDbColSet* ColSetLC(TInt aIndex,const RMessage2& aMessage);
768 CDbKey* KeyLC(TInt aIndex,const RMessage2& aMessage);
769 TDbLookupKey* LookupKeyLC(TInt aIndex,TInt aSize,const RMessage2& aMessage);
770 CDbObject* DoAuthenticateL(CDbsConnection* aDbsConnection, const RMessage2& aMessage);
771 TInt DoOpenDatabaseL(const RMessage2& aMessage, const TDbProps& aDbProps);
773 TDbProps* ExtractDbPropsLC(const RMessage2& aMessage);
774 TInt OpenDatabaseL(const RMessage2& aMessage);
775 TInt CreateDatabaseL(const RMessage2& aMessage);
776 TInt DoCreateDatabaseL(const RMessage2& aMessage, const TDbProps& aDbProps);
777 void CopyDatabaseL(const RMessage2& aMessage);
778 void DeleteDatabaseL(const RMessage2& aMessage);
779 TInt GetDatabaseListL(const RMessage2& aMessage);
780 void GetSecurityPolicyL(const RMessage2& aMessage);
781 void ReadRowL(RDbRow& aRow,TInt aSize,const RMessage2& aMessage);
782 void PutRowL(CDbCursor& aCursor,const RMessage2& aMessage);
783 TInt WriteRowL(const RDbRow& aRow,const RMessage2& aMessage);
784 TInt RetrieveRowL(CDbCursor& aCursor,const RMessage2& aMessage);
785 TInt NewCursorL(CDbCursor* aCursor,const RMessage2& aMessage,const MPolicy* aTblSecurityPolicy);
786 TInt NewIncrementalL(CDbIncremental* aCursor,TInt& aInit,const RMessage2& aMessage,const MPolicy* aPolicy);
787 TInt NewStreamL(MStreamBuf* aHost,const RMessage2& aMessage,const MPolicy* aPolicy,TInt aExtent=-1);
788 TInt NewStreamL(TAny* aPtr,TExternalizeFunction aExter,const RMessage2& aMessage,const MPolicy* aPolicy);
789 void NewDbEntryL(CDbObject* aDbObject, const TDbPolicyRequest& aDbPolicyRequest, TInt& aDbHandle);
795 void ReserveDriveSpaceL(TDriveNumber aDrive);
796 void FreeReservedSpace(TDriveNumber aDrive);
797 void GetReserveAccessL(TDriveNumber aDrive);
798 void ReleaseReserveAccess(TDriveNumber aDrive);
800 void GetBackupPathL(const RMessage2& aMessage);
801 TInt GetBackupPathsL(const RMessage2& aMessage);
803 inline TInt Add(CDbIncremental* aIncremental, const MPolicy* aPolicy);
804 inline TInt Add(CDbCursor* aCursor, const MPolicy* aPolicy);
805 inline TInt Add(CDbRowConstraint* aConstraint, const MPolicy* aPolicy);
806 inline TInt Add(HDbsStream* aStream, const MPolicy* aPolicy);
807 inline TInt Add(CDbsObserver::HObserver* aObserver, const MPolicy* aPolicy);
809 TEntry& Object(TInt aHandle);
811 TInt DoAdd(TAny* aObject,TDbsType aType, const MPolicy* aPolicy);
812 void Free(TEntry& aEntry);
818 RMap<TInt, TDbPolicyRequest> iDbPolicyRqColl;
819 CDbsSessDriveSpace* iSessDriveSpace;
828 inline HDbsStream(MStreamBuf* aHost,TInt aReadPos);
829 inline ~HDbsStream();
831 TInt ReadL(const RMessage2& aMessage);
832 void WriteL(const RMessage2& aMessage);
834 inline void SynchL();
844 NONSHARABLE_CLASS(HBufBuf) : public TBufBuf
846 enum {EGranularity=512};
848 static HBufBuf* NewLC();
851 virtual inline ~HBufBuf();
857 #include "SD_STD.INL"