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