persistentstorage/dbms/inc/D32SQL.H
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // SQL parsing engine
       
    15 // 
       
    16 //
       
    17 
       
    18 #ifndef __D32SQL_H__
       
    19 #define __D32SQL_H__
       
    20 
       
    21 #ifndef __D32DBMS_H__
       
    22 #include <d32dbms.h>
       
    23 #endif
       
    24 
       
    25 // Other class referenced
       
    26 class MStreamBuf;
       
    27 class TDbBlob;
       
    28 class RDbTableRow;
       
    29 class TTextOps;
       
    30 class TSqlParser;
       
    31 class CDbTable;
       
    32 class CDbTableDatabase;
       
    33 class CDbDataSource;
       
    34 class CDbBlobSpace;
       
    35 template <class T,class D> class HMatcherPattern;
       
    36 
       
    37 // Classes defined
       
    38 class Sql;
       
    39 class RSqlLiteral;
       
    40 class CSqlValues;
       
    41 class RSqlColumnList;
       
    42 class CSqlSearchCondition;
       
    43 class CSqlMultiNode;
       
    44 class CSqlBoundNode;
       
    45 class CSqlNullPredicate;
       
    46 class CSqlLiteralNode;
       
    47 class CSqlCompPredicate;
       
    48 class CSqlLikePredicate;
       
    49 class CSqlQuery;
       
    50 class CSqlDDLStatement;
       
    51 class CSqlDMLStatement;
       
    52 class CSqlInsertStatement;
       
    53 class CSqlModifyStatement;
       
    54 
       
    55 /**
       
    56 @internalComponent
       
    57 */
       
    58 typedef HMatcherPattern<TUint8,TDesC8> HMatcherPattern8;
       
    59 typedef HMatcherPattern<TUint16,TDesC16> HMatcherPattern16;
       
    60 
       
    61 /**
       
    62 @internalComponent
       
    63 */
       
    64 class Sql
       
    65 	{
       
    66 public:
       
    67 	enum TStatementType { ENone, EDDL, EDML };
       
    68 public:
       
    69 	static CSqlDDLStatement* ParseDDLStatementLC( const TDesC& aSql );
       
    70 	static CSqlDMLStatement* ParseDMLStatementLC( const TDesC& aSql );
       
    71 	static CSqlQuery* ParseQueryLC( const TDesC& aSql );
       
    72 	static CSqlSearchCondition* ParseSearchConditionLC( const TDesC& aSql );
       
    73 	//
       
    74 	static TStatementType Type( const TDesC& aSql );
       
    75 	};
       
    76 
       
    77 // variant data classes
       
    78 
       
    79 /**
       
    80 @internalComponent
       
    81 */
       
    82 class RSqlLiteral
       
    83 	{
       
    84 public:
       
    85 	inline RSqlLiteral();
       
    86 	void Close();
       
    87 // assignment
       
    88 	inline void SetText( const TText* aPtr, const TText* aEnd );
       
    89 	inline TInt64& SetInt();
       
    90 	inline TReal64& SetReal();
       
    91 	inline TTime& SetTime();
       
    92 // conversions
       
    93 	void ToInt32L();
       
    94 	void ToUint32L();
       
    95 	void ToInt64L();
       
    96 	void ToReal32L();
       
    97 	void ToReal64L();
       
    98 	void ToTimeL();
       
    99 	void ToText8L();
       
   100 	void ToText16L();
       
   101 	void ToPattern8L(TBool aEscape = EFalse);
       
   102 	void ToPattern16L(TBool aEscape = EFalse);
       
   103 	TInt CopyText();
       
   104 // accessors
       
   105 	inline TBool IsNull() const;
       
   106 	inline TInt32 Int32() const;
       
   107 	inline TUint32 Uint32() const;
       
   108 	inline const TInt64& Int64() const;
       
   109 	inline const TTime& Time() const;
       
   110 	inline const TReal32& Real32() const;
       
   111 	inline const TReal64& Real64() const;
       
   112 	inline const TText* Ptr() const;
       
   113 	inline const TText* End() const;
       
   114 	inline const TPtrC DesC() const;
       
   115 	inline const HBufC8& Text8() const;
       
   116 	inline const HBufC16& Text16() const;
       
   117 	inline const HMatcherPattern8& Pattern8() const;
       
   118 	inline const HMatcherPattern16& Pattern16() const;
       
   119 private:
       
   120 	inline TBool IsBasic() const;
       
   121 	inline TBool IsAlloc() const;
       
   122 private:
       
   123 	enum TType { EInt32, EUint32, EReal32, EInt64, ETime, EReal64, EPtr, ENull, EBuf8, EBuf16, EPattern8, EPattern16 };
       
   124 private:
       
   125 	union
       
   126 		{
       
   127 		TInt32 iInt32;
       
   128 		TUint32 iUint32;
       
   129 		TUnion<TInt64> iInt64;
       
   130 		TReal32 iReal32;
       
   131 		TReal64 iReal64;
       
   132 		TUnion<TTime> iTime;
       
   133 		struct { const TText* iPtr; const TText* iEnd; } iPtr;
       
   134 		TAny* iAlloc;
       
   135 		} iVal;
       
   136 	TType iType;
       
   137 	HBufC* iBuffer;
       
   138 	};
       
   139 
       
   140 /**
       
   141 @internalComponent
       
   142 */
       
   143 NONSHARABLE_CLASS(CSqlValues) : public CBase
       
   144 	{
       
   145 public:
       
   146 	CSqlValues();
       
   147 	~CSqlValues();
       
   148 //
       
   149 	void AddL( const RSqlLiteral& aLiteral );
       
   150 	void BindL( const CDbDataSource& aSource );
       
   151 	void WriteL( CDbDataSource& aSource, CDbTable& aTable ) const;
       
   152 private:
       
   153 	enum { EGranularity = 8 };
       
   154 	class TEntry
       
   155 		{
       
   156 	public:
       
   157 		inline TEntry( const RSqlLiteral& aValue );
       
   158 	public:
       
   159 		RSqlLiteral iValue;
       
   160 		TDbColType iType;
       
   161 		};
       
   162 private:
       
   163 	RArray<TEntry> iValues;
       
   164 	};
       
   165 
       
   166 /**
       
   167 @internalComponent
       
   168 */
       
   169 class RSqlColumnList : public RArray<TPtrC>
       
   170 	{
       
   171 	enum { EGranularity = 8 };
       
   172 public:
       
   173 	inline RSqlColumnList();
       
   174 	};
       
   175 
       
   176 // evaluation tree classes
       
   177 
       
   178 /**
       
   179 @internalComponent
       
   180 */
       
   181 class CSqlSearchCondition : public CBase
       
   182 	{
       
   183 public:
       
   184 	enum TType
       
   185 		{
       
   186 		EAnd, EOr,
       
   187 		EIsNull, EIsNotNull,
       
   188 		ELike, ENotLike,
       
   189 		ELess, ELessEqual, EEqual, EGreaterEqual, EGreater, ENotEqual
       
   190 		};
       
   191 public:
       
   192 
       
   193 	TBool iIsEscape; 
       
   194 	
       
   195 	inline TType NodeType() const;
       
   196 	inline CSqlMultiNode* MultiNode();
       
   197 	inline CSqlNullPredicate* NullPredicate();
       
   198 	inline CSqlCompPredicate* CompPredicate();
       
   199 	inline CSqlLikePredicate* LikePredicate();
       
   200 //
       
   201 	virtual void BindL( const RDbTableRow& aSource ) = 0;
       
   202 	virtual TBool EvaluateL( const TTextOps& aTextOp ) const = 0;
       
   203 protected:
       
   204 	CSqlSearchCondition( TType aType );
       
   205 		
       
   206 private:
       
   207 	TType iType;
       
   208 	
       
   209 	};
       
   210 
       
   211 /**
       
   212 @internalComponent
       
   213 */
       
   214 NONSHARABLE_CLASS(CSqlMultiNode) : public CSqlSearchCondition
       
   215 	{
       
   216 private:
       
   217 	typedef CSqlSearchCondition* TEntry;
       
   218 	enum { EGranularity = 4 * sizeof( TEntry ) };
       
   219 public:
       
   220 	static CSqlSearchCondition* New( TType aType, CSqlSearchCondition* aLeft, CSqlSearchCondition* aRight );
       
   221 //
       
   222 	inline TInt Count() const;
       
   223 	inline CSqlSearchCondition* SubNode( TInt aIndex ) const;
       
   224 	inline void SetSubNode( TInt aIndex, CSqlSearchCondition* aSearchCondition ); 
       
   225 	void Remove( CSqlSearchCondition* aSubNode );
       
   226 	static CSqlSearchCondition* Reduce( CSqlMultiNode* aNode );
       
   227 private:
       
   228 	inline CSqlMultiNode( TType aType );
       
   229 	~CSqlMultiNode();
       
   230 	static CSqlMultiNode* New( TType aType, TInt aSize );
       
   231 	static CSqlMultiNode* Grow( CSqlMultiNode* aNode, TInt aExtraSize );
       
   232 	static CSqlMultiNode* Concatenate( CSqlMultiNode* aLeft, CSqlMultiNode* aRight );
       
   233 //
       
   234 	inline TInt Size() const;
       
   235 	static inline TInt Size( TInt aLeaves );
       
   236 	inline TBool IsFull() const;
       
   237 private:
       
   238 // from CSqlSearchCondition
       
   239 	void BindL( const RDbTableRow& aSource );
       
   240 	TBool EvaluateL( const TTextOps& aTextOp ) const;
       
   241 private:
       
   242 	TEntry* iEnd;
       
   243 	TEntry iLeaves[1];
       
   244 	};
       
   245 
       
   246 /**
       
   247 @internalComponent
       
   248 */
       
   249 NONSHARABLE_CLASS(CSqlBoundNode) : public CSqlSearchCondition
       
   250 	{
       
   251 public:
       
   252 	const TDesC& ColumnName() const;
       
   253 	inline TDbColNo ColNo() const;
       
   254 	inline TDbColType ColType() const;
       
   255 protected:
       
   256 	CSqlBoundNode( TType aType, const TDesC& aColumn );
       
   257 	inline TBool IsBound() const;
       
   258 	inline TDbColumnC Column() const;
       
   259 	MStreamBuf& StreamLC( const TDbBlob& aBlob ) const;
       
   260 // from CSqlSearchCondition
       
   261 	void BindL( const RDbTableRow& aSource );
       
   262 private:
       
   263 	const RDbTableRow* iSource;
       
   264 	union
       
   265 		{
       
   266 		TUnion<TPtrC> iName;
       
   267 		struct
       
   268 			{
       
   269 			TDbColNo iNumber;
       
   270 			TDbColType iType;
       
   271 			} iBound;
       
   272 		} iColumn;
       
   273 	};
       
   274 
       
   275 /**
       
   276 @internalComponent
       
   277 */
       
   278 NONSHARABLE_CLASS(CSqlNullPredicate) : public CSqlBoundNode
       
   279 	{
       
   280 public:
       
   281 	CSqlNullPredicate( TType aType, const TDesC& aColumn );
       
   282 private:
       
   283 // from CSqlSearchCondition
       
   284 	TBool EvaluateL( const TTextOps& aTextOp ) const;
       
   285 	};
       
   286 
       
   287 /**
       
   288 @internalComponent
       
   289 */
       
   290 NONSHARABLE_CLASS(CSqlLiteralNode) : public CSqlBoundNode
       
   291 	{
       
   292 public:
       
   293 	inline const RSqlLiteral& Value() const;
       
   294 	inline RSqlLiteral& Value();
       
   295 protected:
       
   296 	CSqlLiteralNode( TType aType, const TDesC& aColumn, const RSqlLiteral& aLiteral );
       
   297 	~CSqlLiteralNode();
       
   298 // from CSqlSearchCondition
       
   299 	void BindL( const RDbTableRow& aSource );
       
   300 private:
       
   301 	RSqlLiteral iLiteral;
       
   302 	};
       
   303 
       
   304 /**
       
   305 @internalComponent
       
   306 */
       
   307 NONSHARABLE_CLASS(CSqlCompPredicate) : public CSqlLiteralNode
       
   308 	{
       
   309 public:
       
   310 	CSqlCompPredicate( TType aType, const TDesC& aColumn, const RSqlLiteral& aLiteral );
       
   311 private:
       
   312 	TInt CompareLongText8L( const TDbBlob& aBlob, const TTextOps& aTextOp ) const;
       
   313 	TInt CompareLongText16L( const TDbBlob& aBlob, const TTextOps& aTextOp ) const;
       
   314 // from CSqlSearchCondition
       
   315 	TBool EvaluateL( const TTextOps& aTextOp ) const;
       
   316 	};
       
   317 
       
   318 /**
       
   319 @internalComponent
       
   320 */
       
   321 NONSHARABLE_CLASS(CSqlLikePredicate) : public CSqlLiteralNode 
       
   322 	{
       
   323 public:
       
   324 	CSqlLikePredicate( TType aType, const TDesC& aColumn, const RSqlLiteral& aPattern );
       
   325 private:
       
   326 // from CSqlSearchCondition
       
   327 	void BindL( const RDbTableRow& aSource );
       
   328 	TBool EvaluateL( const TTextOps& aTextOp ) const;
       
   329 private:
       
   330 	TInt iMatchSize;
       
   331 	};
       
   332 
       
   333 /**
       
   334 @internalComponent
       
   335 */
       
   336 NONSHARABLE_CLASS(CSqlQuery) : public CBase
       
   337 	{
       
   338 	friend class TSqlParser;
       
   339 public:
       
   340 	CSqlQuery();
       
   341 	~CSqlQuery();
       
   342 //
       
   343 	inline const TDesC& Table() const;
       
   344 //
       
   345 	inline TBool HasColumnList() const;
       
   346 	inline const RSqlColumnList& ColumnList() const;
       
   347 //
       
   348 	inline TBool HasSearchCondition() const;
       
   349 	inline CSqlSearchCondition& SearchCondition();
       
   350 	void RemovePredicate( CSqlCompPredicate* aNode );
       
   351 	inline CSqlSearchCondition* AdoptSearchCondition();
       
   352 //
       
   353 	inline TBool HasSortSpecification() const;
       
   354 	inline CDbKey& SortSpecification();
       
   355 private:
       
   356 	CDbKey& SortSpecificationL();
       
   357 private:
       
   358 	TPtrC iTable;
       
   359 	RSqlColumnList iColumns;
       
   360 	CSqlSearchCondition* iSearchCondition;
       
   361 	CDbKey* iSortSpecification;
       
   362 	};
       
   363 
       
   364 /**
       
   365 @internalComponent
       
   366 */
       
   367 class CSqlDDLStatement : public CBase
       
   368 	{
       
   369 protected:
       
   370 	inline CSqlDDLStatement() {}
       
   371 public:
       
   372 	virtual CDbIncremental* ExecuteL( CDbDatabase& aDatabase, TDbTextComparison aComparison, TInt& aStep ) = 0;
       
   373 	};
       
   374 
       
   375 /**
       
   376 @internalComponent
       
   377 */
       
   378 NONSHARABLE_CLASS(CSqlDMLStatement) : public CBase
       
   379 	{
       
   380 	friend class TSqlParser;
       
   381 public:
       
   382 	~CSqlDMLStatement();
       
   383 //
       
   384 	inline CSqlQuery& Query();
       
   385 	inline CSqlValues& Values();
       
   386 	inline CSqlValues* AdoptValues();
       
   387 //
       
   388 	virtual CDbIncremental* ExecuteL( CDbTableDatabase& aDatabase, TDbTextComparison aComparison, TInt& aRows ) = 0;
       
   389 protected:
       
   390 	inline CSqlDMLStatement() {}
       
   391 	inline TBool HasValues() const;
       
   392 private:
       
   393 	CSqlValues& ValuesL();
       
   394 private:
       
   395 	CSqlQuery iQuery;
       
   396 	CSqlValues* iValues;
       
   397 	};
       
   398 
       
   399 /**
       
   400 @internalComponent
       
   401 */
       
   402 NONSHARABLE_CLASS(CSqlInsertStatement) : public CSqlDMLStatement
       
   403 	{
       
   404 public:
       
   405 	static CSqlInsertStatement* NewLC();
       
   406 private:
       
   407 	inline CSqlInsertStatement() {}
       
   408 // from CSqlDMLStatement
       
   409 	CDbIncremental* ExecuteL( CDbTableDatabase& aDatabase, TDbTextComparison aComparison, TInt& aRows );
       
   410 	};
       
   411 
       
   412 /**
       
   413 @internalComponent
       
   414 */
       
   415 NONSHARABLE_CLASS(CSqlModifyStatement) : public CSqlDMLStatement
       
   416 	{
       
   417 public:
       
   418 	static CSqlModifyStatement* NewLC();
       
   419 	inline TBool IsUpdate() const;
       
   420 private:
       
   421 	inline CSqlModifyStatement() {}
       
   422 // from CSqlDMLStatement
       
   423 	CDbIncremental* ExecuteL( CDbTableDatabase& aDatabase, TDbTextComparison aComparison, TInt& aRows );
       
   424 	};
       
   425 
       
   426 /**
       
   427 TSqlParser2 class uses TSqlParser class functionality only to parse particular SQL string
       
   428 and extract SQL type and table name.
       
   429 @internalComponent
       
   430 */
       
   431 class TSqlParser2
       
   432 	{
       
   433 public:
       
   434 	void ParseL(const TDesC& aSql);
       
   435 	inline Sql::TStatementType StatementType() const;
       
   436 	inline const TDesC& TableName() const;
       
   437 
       
   438 private:
       
   439 	Sql::TStatementType	iStatementType;
       
   440 	TPtrC				iTableName;
       
   441 
       
   442 	};
       
   443 
       
   444 #include "D32SQL.INL"
       
   445 
       
   446 #endif//__D32SQL_H__