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