persistentstorage/dbms/usql/UQ_STD.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 //
       
    15 
       
    16 #include "U32STD.H"
       
    17 #include "D32SQL.H"
       
    18 #include "D32TABLE.H"
       
    19 #include <e32math.h>
       
    20 #include <s32mem.h>
       
    21 
       
    22 //Forward declarations
       
    23 class TSqlParser2;
       
    24 
       
    25 // Classes defined
       
    26 class TSqlToken;
       
    27 class TSqlLexer;
       
    28 class TSqlParser;
       
    29 template <class T,class D> class TMatcher;
       
    30 template <class T,class D> class TDesMatcher;
       
    31 template <class T,class D> class TStreamMatcher;
       
    32 template <class T,class D> class HMatcherPattern;
       
    33 class CSqlCreateTableStatement;
       
    34 class CSqlDropTableStatement;
       
    35 class CSqlAlterTableStatement;
       
    36 class CSqlCreateIndexStatement;
       
    37 class CSqlDropIndexStatement;
       
    38 
       
    39 /**
       
    40 parser classes
       
    41 @internalComponent
       
    42 */
       
    43 enum TSqlTokenType
       
    44 	{
       
    45 	ESqlNoToken=0,
       
    46 //
       
    47 	ESqlEos,
       
    48 //
       
    49 	ESqlIdentifier,
       
    50 	ESqlLiteralInt,
       
    51 	ESqlLiteralReal,
       
    52 	ESqlLiteralTime,
       
    53 	ESqlLiteralText,
       
    54 //
       
    55 	ESqlAsterisk,
       
    56 	ESqlComma,
       
    57 	ESqlLeftBracket,
       
    58 	ESqlRightBracket,
       
    59 	ESqlLess,
       
    60 	ESqlLessEqual,
       
    61 	ESqlEqual,
       
    62 	ESqlGreaterEqual,
       
    63 	ESqlGreater,
       
    64 	ESqlNotEqual
       
    65 	};
       
    66 
       
    67 /**
       
    68 @internalComponent
       
    69 */
       
    70 enum TSqlKeyword
       
    71 	{
       
    72 	ESqlNotKeyword=-1,
       
    73 //
       
    74 #define KEYWORD(s) ESqlKeyword_##s
       
    75 #include "UQ_KEYWD.H"
       
    76 #undef KEYWORD
       
    77 	};
       
    78 
       
    79 class TSqlToken
       
    80 /**
       
    81 @internalComponent
       
    82 */
       
    83 	{
       
    84 	friend class TSqlLexer;
       
    85 public:
       
    86 	inline TBool operator==(TSqlTokenType aType) const;
       
    87 	inline TBool operator!=(TSqlTokenType aType) const;
       
    88 //
       
    89 	inline void SetError(TInt aError);
       
    90 	inline TInt Error() const;
       
    91 //
       
    92 	inline TSqlTokenType Type() const;
       
    93 	inline const RSqlLiteral& Literal() const;
       
    94 	inline RSqlLiteral& Literal();
       
    95 private:
       
    96 	inline TSqlTokenType operator=(TSqlTokenType aType);
       
    97 private:
       
    98 	TInt iType;
       
    99 	RSqlLiteral iLiteral;
       
   100 	};
       
   101 
       
   102 class TSqlLexer
       
   103 /**
       
   104 @internalComponent
       
   105 */
       
   106 	{
       
   107 public:
       
   108 	TSqlLexer(const TDesC& aSql);
       
   109 	inline TSqlTokenType NextToken(TSqlToken& aToken);
       
   110 	static TSqlKeyword Keyword(const TSqlToken& aToken);
       
   111 	static inline TBool IsKeyword(TSqlKeyword aKeyword,const TSqlToken& aToken);
       
   112 	const TText* Next() const;
       
   113 	void Set(const TText* aNext);	
       
   114 private:
       
   115 	TSqlTokenType GetIdentifier(TSqlToken& aToken);
       
   116 	TSqlTokenType GetString(TSqlToken& aToken);
       
   117 	TInt GetInteger(TInt64& aValue);
       
   118 	TSqlTokenType GetNumber(TSqlToken& aToken);
       
   119 	TSqlTokenType GetDate(TSqlToken& aToken);
       
   120 	TSqlTokenType GetNextToken(TSqlToken& aToken);
       
   121 //
       
   122 	static inline TSqlTokenType SqlError(TInt aError=KErrArgument);
       
   123 	static TInt CompareKeyword(TInt aKeyword,const RSqlLiteral& aIdentifier);
       
   124 private:
       
   125 	const TText* iNext;
       
   126 	const TText* iEnd;
       
   127 	};
       
   128 
       
   129 class TSqlParser
       
   130 /**
       
   131 @internalComponent
       
   132 */
       
   133 	{
       
   134 	friend class TSqlParser2;
       
   135 public:
       
   136 	TSqlParser(const TDesC& aSql);
       
   137 //
       
   138 	CSqlQuery* QueryLC();
       
   139 	CSqlSearchCondition* SearchConditionLC();
       
   140 	CSqlDDLStatement* DDLStatementLC();
       
   141 	CSqlDMLStatement* DMLStatementLC();
       
   142 	Sql::TStatementType Type();
       
   143 	TInt PatternFilter(const TDesC& aPattern,const TChar aEscape, TText *aNewPatternBuffer );
       
   144 private:
       
   145 	TSqlTokenType NextToken();
       
   146 	CSqlSearchCondition* SqlError();
       
   147 	inline TInt Error() const;
       
   148 	static TSqlTokenType SqlErrorL();
       
   149 //
       
   150 	TSqlTokenType Parse(TSqlKeyword aKeyword);
       
   151 	TSqlTokenType ParseL(TSqlTokenType aToken);
       
   152 	TSqlTokenType ParseL(TSqlKeyword aKeyword);
       
   153 	TSqlKeyword Keyword();
       
   154 	TSqlTokenType RightBracketL();
       
   155 	TSqlTokenType IdentifierL(TPtrC& aIdentifier);
       
   156 	TPtrC IdentifierL();
       
   157 	void EndL();
       
   158 //
       
   159 	TSqlTokenType ColumnNameL(RSqlColumnList& aList);
       
   160 	TSqlTokenType ColumnListL(RSqlColumnList& aList);
       
   161 	CSqlSearchCondition* SearchCondition(TInt aNot);
       
   162 	CSqlSearchCondition* BooleanTerm(TInt aNot);
       
   163 	CSqlSearchCondition* BooleanFactor(TInt aNot);
       
   164 	CSqlSearchCondition* BooleanPrimary(TInt aNot);
       
   165 	CSqlSearchCondition* Predicate(TInt aNot);
       
   166 	void SortSpecificationL(CDbKey& aKey);
       
   167 	CDbKey* SortSpecificationL();
       
   168 	void DoQueryL(CSqlQuery& aQuery);
       
   169 	CSqlSearchCondition* SearchConditionL();
       
   170 //
       
   171 	TSqlTokenType AddColumnSpecL(TDbCol& aDef);
       
   172 	CSqlDDLStatement* CreateTableLC();
       
   173 	CSqlDDLStatement* DropTableLC();
       
   174 	CSqlDDLStatement* AlterTableLC();
       
   175 	CSqlDDLStatement* CreateIndexLC(TBool aUnique);
       
   176 	CSqlDDLStatement* DropIndexLC();
       
   177 //
       
   178 	TSqlTokenType ColumnValueL(CSqlValues& aValues);
       
   179 	CSqlDMLStatement* InsertStatementLC();
       
   180 	CSqlDMLStatement* UpdateStatementLC();
       
   181 	CSqlDMLStatement* DeleteStatementLC();
       
   182 private:
       
   183 	TSqlLexer iSql;
       
   184 	TSqlToken iToken;
       
   185 	};
       
   186 
       
   187 
       
   188 /**
       
   189 LIKE evaluation support
       
   190 @internalComponent
       
   191 */
       
   192 enum TSegmentType {ENull,ESuccess,EStop,EBeginning,EEnd,EMiddle,ESkip,EEscape,EMask=0xf,EWild=0x80};
       
   193 /**
       
   194 @internalComponent
       
   195 */
       
   196 const TInt KPatternGranularity=0x20;
       
   197 
       
   198 // If this value is changed class documentation must be updated.
       
   199 // Namely, RDbDatabase and TDbQuery.
       
   200 const TInt KMaxSegmentLength=0xff;
       
   201 
       
   202 template <class T,class D>
       
   203 class HMatcherPattern
       
   204 /**
       
   205 @internalComponent
       
   206 */
       
   207 	{
       
   208 	friend class TMatcher<T,D>;
       
   209 	typedef HMatcherPattern<T,D> TThis;
       
   210 public:
       
   211 	static HMatcherPattern<T,D>* NewL(const D& aPattern,TBool aEscape = EFalse);
       
   212 	TInt MatchL(const D& aDes,const TTextOps& aTextOp) const;
       
   213 	TInt MatchL(MStreamBuf& aBuf,TInt aLength,const TTextOps& aTextOp) const;
       
   214 private:
       
   215 	static TInt Construct(HMatcherPattern<T,D>*& aCell,const D& aPattern,TBool aEscape = EFalse);
       
   216 	static inline HMatcherPattern<T,D>* Realloc(HMatcherPattern<T,D>* aThis,TInt aSize);
       
   217 private:
       
   218 	T iPattern[1];
       
   219 	};
       
   220 
       
   221 template <class T,class D>
       
   222 class TMatcher
       
   223 /**
       
   224 @internalComponent
       
   225 */
       
   226 	{
       
   227 public:
       
   228 	TBool MatchL(const HMatcherPattern<T,D>& aPattern,const TTextOps& aTextOp);
       
   229 private:
       
   230 	virtual const T* UnderflowL(const T*& aEnd,TInt aRetain=0) =0;
       
   231 	};
       
   232 
       
   233 template <class T,class D>
       
   234 class TDesMatcher : public TMatcher<T,D>
       
   235 /**
       
   236 @internalComponent
       
   237 */
       
   238 	{
       
   239 public:
       
   240 	inline TDesMatcher(const D& aDes);
       
   241 private:
       
   242 	const T* UnderflowL(const T*& aEnd,TInt aRetain=0);
       
   243 private:
       
   244 	const T* iPtr;
       
   245 	const T* iEnd;
       
   246 	};
       
   247 
       
   248 template <class T,class D>
       
   249 class TStreamMatcher : public TMatcher<T,D>
       
   250 /**
       
   251 @internalComponent
       
   252 */
       
   253 	{
       
   254 public:
       
   255 	inline TStreamMatcher(MStreamBuf& aStreamBuf,TInt aLength);
       
   256 private:
       
   257 	const T* UnderflowL(const T*& aEnd,TInt aRetain=0);
       
   258 private:
       
   259 	enum {EBufSize=0x200};
       
   260 private:
       
   261 	RReadStream iStream;
       
   262 	TInt iRemain;
       
   263 	T iBuffer[EBufSize];
       
   264 	const T* iEnd;
       
   265 	};
       
   266 
       
   267 NONSHARABLE_CLASS(CSqlCreateTableStatement) : public CSqlDDLStatement
       
   268 /**
       
   269 DDL statements
       
   270 @internalComponent
       
   271 */
       
   272 	{
       
   273 private:
       
   274 	CDbIncremental* ExecuteL(CDbDatabase& aDatabase,TDbTextComparison aComparison,TInt& aSteps);
       
   275 public:
       
   276 	TPtrC iName;
       
   277 	CDbColSet iColumns;
       
   278 	};
       
   279 
       
   280 NONSHARABLE_CLASS(CSqlDropTableStatement) : public CSqlDDLStatement
       
   281 /**
       
   282 @internalComponent
       
   283 */
       
   284 	{
       
   285 private:
       
   286 	CDbIncremental* ExecuteL(CDbDatabase& aDatabase,TDbTextComparison aComparison,TInt& aSteps);
       
   287 public:
       
   288 	TPtrC iName;
       
   289 	};
       
   290 
       
   291 NONSHARABLE_CLASS(CSqlAlterTableStatement) : public CSqlDDLStatement
       
   292 /**
       
   293 @internalComponent
       
   294 */
       
   295 	{
       
   296 public:
       
   297 	~CSqlAlterTableStatement();
       
   298 private:
       
   299 	CDbIncremental* ExecuteL(CDbDatabase& aDatabase,TDbTextComparison aComparison,TInt& aSteps);
       
   300 public:
       
   301 	TPtrC iName;
       
   302 	CDbColSet iAdd;
       
   303 	RSqlColumnList iDrop;
       
   304 	};
       
   305 
       
   306 NONSHARABLE_CLASS(CSqlCreateIndexStatement) : public CSqlDDLStatement
       
   307 /**
       
   308 @internalComponent
       
   309 */
       
   310 	{
       
   311 private:
       
   312 	CDbIncremental* ExecuteL(CDbDatabase& aDatabase,TDbTextComparison aComparison,TInt& aSteps);
       
   313 public:
       
   314 	TPtrC iName;
       
   315 	TPtrC iTable;
       
   316 	CDbKey iKey;
       
   317 	};
       
   318 
       
   319 NONSHARABLE_CLASS(CSqlDropIndexStatement) : public CSqlDDLStatement
       
   320 /**
       
   321 @internalComponent
       
   322 */
       
   323 	{
       
   324 private:
       
   325 	CDbIncremental* ExecuteL(CDbDatabase& aDatabase,TDbTextComparison aComparison,TInt& aSteps);
       
   326 public:
       
   327 	TPtrC iName;
       
   328 	TPtrC iTable;
       
   329 	};
       
   330 
       
   331 #include "UQ_STD.INL"