sl@0
|
1 |
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// SQL parser
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
#include "UQ_STD.H"
|
sl@0
|
19 |
|
sl@0
|
20 |
// class TSqlParser
|
sl@0
|
21 |
|
sl@0
|
22 |
const TInt KSqlError = -1;
|
sl@0
|
23 |
|
sl@0
|
24 |
TSqlParser::TSqlParser(const TDesC& aSql)
|
sl@0
|
25 |
: iSql(aSql)
|
sl@0
|
26 |
{
|
sl@0
|
27 |
NextToken(); // parse the first token
|
sl@0
|
28 |
}
|
sl@0
|
29 |
|
sl@0
|
30 |
TSqlTokenType TSqlParser::NextToken()
|
sl@0
|
31 |
{
|
sl@0
|
32 |
return iSql.NextToken(iToken);
|
sl@0
|
33 |
}
|
sl@0
|
34 |
|
sl@0
|
35 |
CSqlSearchCondition* TSqlParser::SqlError()
|
sl@0
|
36 |
{
|
sl@0
|
37 |
if (!Error())
|
sl@0
|
38 |
iToken.SetError(KErrArgument);
|
sl@0
|
39 |
return 0;
|
sl@0
|
40 |
}
|
sl@0
|
41 |
|
sl@0
|
42 |
TSqlTokenType TSqlParser::SqlErrorL()
|
sl@0
|
43 |
//
|
sl@0
|
44 |
// Report a SQL syntax error
|
sl@0
|
45 |
//
|
sl@0
|
46 |
{
|
sl@0
|
47 |
return TSqlTokenType(__LEAVE_IF_ERROR(KErrArgument));
|
sl@0
|
48 |
}
|
sl@0
|
49 |
|
sl@0
|
50 |
TSqlTokenType TSqlParser::Parse(TSqlKeyword aKeyword)
|
sl@0
|
51 |
//
|
sl@0
|
52 |
// look for requested keyword, skip to the next token if found
|
sl@0
|
53 |
// return the next token if found, else ESqlNoToken (==0)
|
sl@0
|
54 |
//
|
sl@0
|
55 |
{
|
sl@0
|
56 |
return TSqlLexer::IsKeyword(aKeyword,iToken) ? NextToken() : ESqlNoToken;
|
sl@0
|
57 |
}
|
sl@0
|
58 |
|
sl@0
|
59 |
TSqlTokenType TSqlParser::ParseL(TSqlTokenType aToken)
|
sl@0
|
60 |
//
|
sl@0
|
61 |
// parse the desired token
|
sl@0
|
62 |
//
|
sl@0
|
63 |
{
|
sl@0
|
64 |
return iToken!=aToken ? SqlErrorL() : NextToken();
|
sl@0
|
65 |
}
|
sl@0
|
66 |
|
sl@0
|
67 |
TSqlTokenType TSqlParser::ParseL(TSqlKeyword aKeyword)
|
sl@0
|
68 |
//
|
sl@0
|
69 |
// parse the desired keyword
|
sl@0
|
70 |
//
|
sl@0
|
71 |
{
|
sl@0
|
72 |
TSqlTokenType t=Parse(aKeyword);
|
sl@0
|
73 |
return t==ESqlNoToken ? SqlErrorL() : t;
|
sl@0
|
74 |
}
|
sl@0
|
75 |
|
sl@0
|
76 |
TSqlKeyword TSqlParser::Keyword()
|
sl@0
|
77 |
//
|
sl@0
|
78 |
// parse a keyword
|
sl@0
|
79 |
//
|
sl@0
|
80 |
{
|
sl@0
|
81 |
TSqlKeyword k=TSqlLexer::Keyword(iToken);
|
sl@0
|
82 |
if (k!=ESqlNotKeyword)
|
sl@0
|
83 |
NextToken();
|
sl@0
|
84 |
return k;
|
sl@0
|
85 |
}
|
sl@0
|
86 |
|
sl@0
|
87 |
TSqlTokenType TSqlParser::RightBracketL()
|
sl@0
|
88 |
//
|
sl@0
|
89 |
// parse a right bracket, and fail if not found
|
sl@0
|
90 |
//
|
sl@0
|
91 |
{
|
sl@0
|
92 |
return ParseL(ESqlRightBracket);
|
sl@0
|
93 |
}
|
sl@0
|
94 |
|
sl@0
|
95 |
void TSqlParser::EndL()
|
sl@0
|
96 |
//
|
sl@0
|
97 |
// Check that the SQL has been fully parsed
|
sl@0
|
98 |
//
|
sl@0
|
99 |
{
|
sl@0
|
100 |
if (iToken!=ESqlEos)
|
sl@0
|
101 |
SqlErrorL();
|
sl@0
|
102 |
}
|
sl@0
|
103 |
|
sl@0
|
104 |
TSqlTokenType TSqlParser::IdentifierL(TPtrC& aIdentifier)
|
sl@0
|
105 |
//
|
sl@0
|
106 |
// parse an identifer, fail if not found
|
sl@0
|
107 |
//
|
sl@0
|
108 |
{
|
sl@0
|
109 |
if (iToken!=ESqlIdentifier)
|
sl@0
|
110 |
return SqlErrorL();
|
sl@0
|
111 |
const TText* p=iToken.Literal().Ptr();
|
sl@0
|
112 |
TInt len=iToken.Literal().End()-p;
|
sl@0
|
113 |
aIdentifier.Set(p,len);
|
sl@0
|
114 |
return NextToken();
|
sl@0
|
115 |
}
|
sl@0
|
116 |
|
sl@0
|
117 |
TPtrC TSqlParser::IdentifierL()
|
sl@0
|
118 |
//
|
sl@0
|
119 |
// parse an identifer, fail if not found
|
sl@0
|
120 |
//
|
sl@0
|
121 |
{
|
sl@0
|
122 |
if (iToken!=ESqlIdentifier)
|
sl@0
|
123 |
SqlErrorL();
|
sl@0
|
124 |
return iToken.Literal().DesC();
|
sl@0
|
125 |
}
|
sl@0
|
126 |
|
sl@0
|
127 |
TSqlTokenType TSqlParser::ColumnNameL(RSqlColumnList& aList)
|
sl@0
|
128 |
//
|
sl@0
|
129 |
// Parse a column-identifier
|
sl@0
|
130 |
//
|
sl@0
|
131 |
{
|
sl@0
|
132 |
__LEAVE_IF_ERROR(aList.Append(IdentifierL()));
|
sl@0
|
133 |
return NextToken();
|
sl@0
|
134 |
}
|
sl@0
|
135 |
|
sl@0
|
136 |
TSqlTokenType TSqlParser::ColumnListL(RSqlColumnList& aList)
|
sl@0
|
137 |
//
|
sl@0
|
138 |
// Parse a column-identifier-comma-list
|
sl@0
|
139 |
//
|
sl@0
|
140 |
{
|
sl@0
|
141 |
for (;;)
|
sl@0
|
142 |
{
|
sl@0
|
143 |
TSqlTokenType t=ColumnNameL(aList);
|
sl@0
|
144 |
if (t!=ESqlComma)
|
sl@0
|
145 |
return t;
|
sl@0
|
146 |
NextToken();
|
sl@0
|
147 |
}
|
sl@0
|
148 |
}
|
sl@0
|
149 |
|
sl@0
|
150 |
TSqlTokenType TSqlParser::AddColumnSpecL(TDbCol& aDef)
|
sl@0
|
151 |
//
|
sl@0
|
152 |
// Parse an add-column-spec
|
sl@0
|
153 |
//
|
sl@0
|
154 |
{
|
sl@0
|
155 |
aDef.iAttributes=0;
|
sl@0
|
156 |
aDef.iName=IdentifierL();
|
sl@0
|
157 |
NextToken();
|
sl@0
|
158 |
//
|
sl@0
|
159 |
TDbColType type;
|
sl@0
|
160 |
switch (Keyword())
|
sl@0
|
161 |
{
|
sl@0
|
162 |
case ESqlKeyword_bit:
|
sl@0
|
163 |
type=EDbColBit;
|
sl@0
|
164 |
break;
|
sl@0
|
165 |
case ESqlKeyword_tinyint:
|
sl@0
|
166 |
type=EDbColInt8;
|
sl@0
|
167 |
break;
|
sl@0
|
168 |
case ESqlKeyword_smallint:
|
sl@0
|
169 |
type=EDbColInt16;
|
sl@0
|
170 |
break;
|
sl@0
|
171 |
case ESqlKeyword_integer:
|
sl@0
|
172 |
type=EDbColInt32;
|
sl@0
|
173 |
break;
|
sl@0
|
174 |
case ESqlKeyword_counter:
|
sl@0
|
175 |
aDef.iAttributes=aDef.EAutoIncrement;
|
sl@0
|
176 |
type=EDbColUint32;
|
sl@0
|
177 |
break;
|
sl@0
|
178 |
case ESqlKeyword_bigint:
|
sl@0
|
179 |
type=EDbColInt64;
|
sl@0
|
180 |
break;
|
sl@0
|
181 |
case ESqlKeyword_real:
|
sl@0
|
182 |
type=EDbColReal32;
|
sl@0
|
183 |
break;
|
sl@0
|
184 |
case ESqlKeyword_double:
|
sl@0
|
185 |
//Return value is intentionaly not checked. Parse() checks for
|
sl@0
|
186 |
//an optional SQL keyword and calls internal checks.
|
sl@0
|
187 |
//coverity[check_return]
|
sl@0
|
188 |
//coverity[unchecked_value]
|
sl@0
|
189 |
Parse(ESqlKeyword_precision);
|
sl@0
|
190 |
// fall through
|
sl@0
|
191 |
case ESqlKeyword_float:
|
sl@0
|
192 |
type=EDbColReal64;
|
sl@0
|
193 |
break;
|
sl@0
|
194 |
case ESqlKeyword_date:
|
sl@0
|
195 |
case ESqlKeyword_time:
|
sl@0
|
196 |
case ESqlKeyword_timestamp:
|
sl@0
|
197 |
type=EDbColDateTime;
|
sl@0
|
198 |
break;
|
sl@0
|
199 |
case ESqlKeyword_char:
|
sl@0
|
200 |
case ESqlKeyword_varchar:
|
sl@0
|
201 |
type=EDbColText;
|
sl@0
|
202 |
break;
|
sl@0
|
203 |
case ESqlKeyword_binary:
|
sl@0
|
204 |
case ESqlKeyword_varbinary:
|
sl@0
|
205 |
type=EDbColBinary;
|
sl@0
|
206 |
break;
|
sl@0
|
207 |
case ESqlKeyword_unsigned: // unsigned tinyint, smallint or integer
|
sl@0
|
208 |
switch (Keyword())
|
sl@0
|
209 |
{
|
sl@0
|
210 |
case ESqlKeyword_tinyint:
|
sl@0
|
211 |
type=EDbColUint8;
|
sl@0
|
212 |
break;
|
sl@0
|
213 |
case ESqlKeyword_smallint:
|
sl@0
|
214 |
type=EDbColUint16;
|
sl@0
|
215 |
break;
|
sl@0
|
216 |
case ESqlKeyword_integer:
|
sl@0
|
217 |
type=EDbColUint32;
|
sl@0
|
218 |
break;
|
sl@0
|
219 |
default:
|
sl@0
|
220 |
return SqlErrorL();
|
sl@0
|
221 |
};
|
sl@0
|
222 |
break;
|
sl@0
|
223 |
case ESqlKeyword_long: // varchar or varbinary
|
sl@0
|
224 |
switch (Keyword())
|
sl@0
|
225 |
{
|
sl@0
|
226 |
case ESqlKeyword_varchar:
|
sl@0
|
227 |
type=EDbColLongText;
|
sl@0
|
228 |
break;
|
sl@0
|
229 |
case ESqlKeyword_varbinary:
|
sl@0
|
230 |
type=EDbColLongBinary;
|
sl@0
|
231 |
break;
|
sl@0
|
232 |
default:
|
sl@0
|
233 |
return SqlErrorL();
|
sl@0
|
234 |
};
|
sl@0
|
235 |
break;
|
sl@0
|
236 |
default:
|
sl@0
|
237 |
return SqlErrorL();
|
sl@0
|
238 |
}
|
sl@0
|
239 |
aDef.iType=type;
|
sl@0
|
240 |
//
|
sl@0
|
241 |
// get any optional length
|
sl@0
|
242 |
aDef.iMaxLength=KDbUndefinedLength;
|
sl@0
|
243 |
TSqlTokenType t=iToken.Type();
|
sl@0
|
244 |
switch (type)
|
sl@0
|
245 |
{
|
sl@0
|
246 |
case EDbColText:
|
sl@0
|
247 |
case EDbColBinary:
|
sl@0
|
248 |
if (t==ESqlLeftBracket)
|
sl@0
|
249 |
{
|
sl@0
|
250 |
if (NextToken()==ESqlLiteralInt)
|
sl@0
|
251 |
{
|
sl@0
|
252 |
iToken.Literal().ToInt32L();
|
sl@0
|
253 |
aDef.iMaxLength=iToken.Literal().Int32();
|
sl@0
|
254 |
NextToken();
|
sl@0
|
255 |
t=RightBracketL();
|
sl@0
|
256 |
}
|
sl@0
|
257 |
else
|
sl@0
|
258 |
return SqlErrorL();
|
sl@0
|
259 |
}
|
sl@0
|
260 |
break;
|
sl@0
|
261 |
default:
|
sl@0
|
262 |
break;
|
sl@0
|
263 |
}
|
sl@0
|
264 |
return t;
|
sl@0
|
265 |
}
|
sl@0
|
266 |
|
sl@0
|
267 |
CSqlSearchCondition* TSqlParser::SearchCondition(TInt aNot)
|
sl@0
|
268 |
//
|
sl@0
|
269 |
// Parse a search-condition
|
sl@0
|
270 |
//
|
sl@0
|
271 |
{
|
sl@0
|
272 |
CSqlSearchCondition* left=BooleanTerm(aNot);
|
sl@0
|
273 |
if (left==0 || Parse(ESqlKeyword_or)==ESqlNoToken)
|
sl@0
|
274 |
return left;
|
sl@0
|
275 |
return CSqlMultiNode::New(aNot ? CSqlMultiNode::EAnd : CSqlMultiNode::EOr,left,SearchCondition(aNot));
|
sl@0
|
276 |
}
|
sl@0
|
277 |
|
sl@0
|
278 |
CSqlSearchCondition* TSqlParser::BooleanTerm(TInt aNot)
|
sl@0
|
279 |
//
|
sl@0
|
280 |
// Parse a boolean-term
|
sl@0
|
281 |
//
|
sl@0
|
282 |
{
|
sl@0
|
283 |
CSqlSearchCondition* left=BooleanFactor(aNot);
|
sl@0
|
284 |
if (left==0 || Parse(ESqlKeyword_and)==ESqlNoToken)
|
sl@0
|
285 |
return left;
|
sl@0
|
286 |
return CSqlMultiNode::New(aNot ? CSqlMultiNode::EOr : CSqlMultiNode::EAnd,left,BooleanTerm(aNot));
|
sl@0
|
287 |
}
|
sl@0
|
288 |
|
sl@0
|
289 |
CSqlSearchCondition* TSqlParser::BooleanFactor(TInt aNot)
|
sl@0
|
290 |
//
|
sl@0
|
291 |
// Parse a boolean-factor
|
sl@0
|
292 |
//
|
sl@0
|
293 |
{
|
sl@0
|
294 |
while (Parse(ESqlKeyword_not))
|
sl@0
|
295 |
aNot=~aNot;
|
sl@0
|
296 |
return BooleanPrimary(aNot);
|
sl@0
|
297 |
}
|
sl@0
|
298 |
|
sl@0
|
299 |
CSqlSearchCondition* TSqlParser::BooleanPrimary(TInt aNot)
|
sl@0
|
300 |
//
|
sl@0
|
301 |
// Parse a boolean-factor
|
sl@0
|
302 |
//
|
sl@0
|
303 |
{
|
sl@0
|
304 |
// brackets only allowed in this element, so this ordering is valid
|
sl@0
|
305 |
if (iToken!=ESqlLeftBracket)
|
sl@0
|
306 |
return Predicate(aNot);
|
sl@0
|
307 |
// bracketed search condition
|
sl@0
|
308 |
NextToken();
|
sl@0
|
309 |
CSqlSearchCondition* node=SearchCondition(aNot);
|
sl@0
|
310 |
if (!node)
|
sl@0
|
311 |
return node;
|
sl@0
|
312 |
if (iToken==ESqlRightBracket)
|
sl@0
|
313 |
{
|
sl@0
|
314 |
NextToken();
|
sl@0
|
315 |
return node;
|
sl@0
|
316 |
}
|
sl@0
|
317 |
delete node;
|
sl@0
|
318 |
return SqlError();
|
sl@0
|
319 |
}
|
sl@0
|
320 |
|
sl@0
|
321 |
CSqlSearchCondition* TSqlParser::Predicate(TInt aNot)
|
sl@0
|
322 |
//
|
sl@0
|
323 |
// Parse predicate
|
sl@0
|
324 |
// On null return, error will already be set
|
sl@0
|
325 |
//
|
sl@0
|
326 |
{
|
sl@0
|
327 |
if (iToken!=ESqlIdentifier) // column name
|
sl@0
|
328 |
return SqlError();
|
sl@0
|
329 |
TPtrC column(iToken.Literal().DesC());
|
sl@0
|
330 |
TSqlTokenType t=NextToken();
|
sl@0
|
331 |
if (t==ESqlIdentifier)
|
sl@0
|
332 |
{ // like-predicate or null-predicate
|
sl@0
|
333 |
switch (Keyword())
|
sl@0
|
334 |
{
|
sl@0
|
335 |
case ESqlKeyword_is: // IS [NOT] NULL
|
sl@0
|
336 |
if (Parse(ESqlKeyword_not))
|
sl@0
|
337 |
aNot=~aNot;
|
sl@0
|
338 |
if (Parse(ESqlKeyword_null)==ESqlNoToken)
|
sl@0
|
339 |
return SqlError();
|
sl@0
|
340 |
return new CSqlNullPredicate(aNot ? CSqlNullPredicate::EIsNotNull : CSqlNullPredicate::EIsNull,column);
|
sl@0
|
341 |
case ESqlKeyword_not: // NOT LIKE
|
sl@0
|
342 |
if (Parse(ESqlKeyword_like)==ESqlNoToken)
|
sl@0
|
343 |
return SqlError();
|
sl@0
|
344 |
aNot=~aNot;
|
sl@0
|
345 |
// drop through to Like
|
sl@0
|
346 |
case ESqlKeyword_like: // LIKE
|
sl@0
|
347 |
{
|
sl@0
|
348 |
if (iToken!=ESqlLiteralText)
|
sl@0
|
349 |
return SqlError();
|
sl@0
|
350 |
|
sl@0
|
351 |
//Following code is for the implementation of limited-ESCAPE-clause
|
sl@0
|
352 |
const TText *next = iSql.Next(); //remember thecurrent position in the SQL statement
|
sl@0
|
353 |
RSqlLiteral pattern = iToken.Literal(); //this is the LIKE pattern, remember it, because the next keyword might be ESCAPE
|
sl@0
|
354 |
|
sl@0
|
355 |
NextToken(); // Searching for ESCAPE keyword
|
sl@0
|
356 |
|
sl@0
|
357 |
if (Keyword() == ESqlKeyword_escape)
|
sl@0
|
358 |
{
|
sl@0
|
359 |
if (pattern.DesC().Length() <= 0 || pattern.DesC().Length() > (KMaxSegmentLength + 2 ))
|
sl@0
|
360 |
return SqlError();
|
sl@0
|
361 |
TPtrC escapeChar(iToken.Literal().DesC());
|
sl@0
|
362 |
if (escapeChar.Length() <= 0)
|
sl@0
|
363 |
return SqlError();
|
sl@0
|
364 |
TChar escchar = escapeChar[0];
|
sl@0
|
365 |
TText newPattern[KMaxSegmentLength + 2]; // '*' can come as first and last char
|
sl@0
|
366 |
TInt count = PatternFilter(pattern.DesC(),escchar, newPattern);//remove the escape characters from the pattern and store it in "newPattern" variable
|
sl@0
|
367 |
if (count <=0 )
|
sl@0
|
368 |
return SqlError();
|
sl@0
|
369 |
iToken.Literal().SetText(newPattern,(newPattern + count));
|
sl@0
|
370 |
// copy the text to RSqlLiteral as "newPattern" is stack based variable and will go out of scope
|
sl@0
|
371 |
if (iToken.Literal().CopyText() != KErrNone)
|
sl@0
|
372 |
{
|
sl@0
|
373 |
return SqlError();
|
sl@0
|
374 |
}
|
sl@0
|
375 |
CSqlSearchCondition* node = new CSqlLikePredicate(aNot ? CSqlLikePredicate::ENotLike : CSqlLikePredicate::ELike,column,iToken.Literal());
|
sl@0
|
376 |
//cleanup iToken.Literal(), because if there is another LIKE predicate, the allocated by CopyText() buffer will
|
sl@0
|
377 |
//be shared between two RSqlLiteral instances and RSqlLiteral::Close() call will crash.
|
sl@0
|
378 |
//RSqlLiteral::RSqlLiteral() will set the "iBuffer" data member to NULL.
|
sl@0
|
379 |
iToken.Literal() = RSqlLiteral();
|
sl@0
|
380 |
if (node)
|
sl@0
|
381 |
{
|
sl@0
|
382 |
NextToken();
|
sl@0
|
383 |
node->iIsEscape = ETrue;
|
sl@0
|
384 |
}
|
sl@0
|
385 |
return node;
|
sl@0
|
386 |
}
|
sl@0
|
387 |
else
|
sl@0
|
388 |
{
|
sl@0
|
389 |
//Set to the previous node
|
sl@0
|
390 |
iSql.Set(next);
|
sl@0
|
391 |
CSqlSearchCondition* node = new CSqlLikePredicate(aNot ? CSqlLikePredicate::ENotLike : CSqlLikePredicate::ELike,column,pattern);
|
sl@0
|
392 |
if(node)
|
sl@0
|
393 |
NextToken();
|
sl@0
|
394 |
return node;
|
sl@0
|
395 |
|
sl@0
|
396 |
}
|
sl@0
|
397 |
}
|
sl@0
|
398 |
default:
|
sl@0
|
399 |
return SqlError();
|
sl@0
|
400 |
}
|
sl@0
|
401 |
}
|
sl@0
|
402 |
// Comparison predicate...
|
sl@0
|
403 |
CSqlSearchCondition::TType op;
|
sl@0
|
404 |
switch (t)
|
sl@0
|
405 |
{
|
sl@0
|
406 |
case ESqlGreaterEqual:
|
sl@0
|
407 |
aNot=~aNot;
|
sl@0
|
408 |
// drop through to less
|
sl@0
|
409 |
case ESqlLess:
|
sl@0
|
410 |
op=aNot ? CSqlSearchCondition::EGreaterEqual : CSqlSearchCondition::ELess;
|
sl@0
|
411 |
break;
|
sl@0
|
412 |
case ESqlGreater:
|
sl@0
|
413 |
aNot=~aNot;
|
sl@0
|
414 |
// drop through to less equal
|
sl@0
|
415 |
case ESqlLessEqual:
|
sl@0
|
416 |
op=aNot ? CSqlSearchCondition::EGreater: CSqlSearchCondition::ELessEqual;
|
sl@0
|
417 |
break;
|
sl@0
|
418 |
case ESqlNotEqual:
|
sl@0
|
419 |
aNot=~aNot;
|
sl@0
|
420 |
// drop through to equal
|
sl@0
|
421 |
case ESqlEqual:
|
sl@0
|
422 |
op=aNot ? CSqlSearchCondition::ENotEqual : CSqlSearchCondition::EEqual;
|
sl@0
|
423 |
break;
|
sl@0
|
424 |
default:
|
sl@0
|
425 |
return SqlError();
|
sl@0
|
426 |
}
|
sl@0
|
427 |
t=NextToken();
|
sl@0
|
428 |
if (t!=ESqlLiteralInt && t!=ESqlLiteralReal && t!=ESqlLiteralTime && t!=ESqlLiteralText)
|
sl@0
|
429 |
return SqlError();
|
sl@0
|
430 |
CSqlSearchCondition* node=new CSqlCompPredicate(op,column,iToken.Literal());
|
sl@0
|
431 |
if (node)
|
sl@0
|
432 |
NextToken();
|
sl@0
|
433 |
return node;
|
sl@0
|
434 |
}
|
sl@0
|
435 |
|
sl@0
|
436 |
CSqlSearchCondition* TSqlParser::SearchConditionL()
|
sl@0
|
437 |
//
|
sl@0
|
438 |
// Parse a search-condition
|
sl@0
|
439 |
//
|
sl@0
|
440 |
{
|
sl@0
|
441 |
CSqlSearchCondition* sc=SearchCondition(0);
|
sl@0
|
442 |
if (!sc)
|
sl@0
|
443 |
{
|
sl@0
|
444 |
__LEAVE_IF_ERROR(Error()); // syntax error
|
sl@0
|
445 |
User::LeaveNoMemory(); // otherwise a OOM error
|
sl@0
|
446 |
}
|
sl@0
|
447 |
return sc;
|
sl@0
|
448 |
}
|
sl@0
|
449 |
|
sl@0
|
450 |
void TSqlParser::SortSpecificationL(CDbKey& aKey)
|
sl@0
|
451 |
//
|
sl@0
|
452 |
// Parse a sort-specification
|
sl@0
|
453 |
//
|
sl@0
|
454 |
{
|
sl@0
|
455 |
for (;;)
|
sl@0
|
456 |
{
|
sl@0
|
457 |
TDbKeyCol col(IdentifierL());
|
sl@0
|
458 |
NextToken();
|
sl@0
|
459 |
if (Parse(ESqlKeyword_desc))
|
sl@0
|
460 |
col.iOrder=col.EDesc;
|
sl@0
|
461 |
else
|
sl@0
|
462 |
{
|
sl@0
|
463 |
//Return value is intentionaly not checked. Parse() checks for
|
sl@0
|
464 |
//an optional SQL keyword and calls internal checks.
|
sl@0
|
465 |
//coverity[check_return]
|
sl@0
|
466 |
//coverity[unchecked_value]
|
sl@0
|
467 |
Parse(ESqlKeyword_asc);
|
sl@0
|
468 |
col.iOrder=col.EAsc;
|
sl@0
|
469 |
}
|
sl@0
|
470 |
aKey.AddL(col);
|
sl@0
|
471 |
if (iToken!=ESqlComma)
|
sl@0
|
472 |
break;
|
sl@0
|
473 |
NextToken();
|
sl@0
|
474 |
}
|
sl@0
|
475 |
}
|
sl@0
|
476 |
|
sl@0
|
477 |
CSqlQuery* TSqlParser::QueryLC()
|
sl@0
|
478 |
//
|
sl@0
|
479 |
// Generate a CSqlQuery
|
sl@0
|
480 |
//
|
sl@0
|
481 |
{
|
sl@0
|
482 |
CSqlQuery* query=new(ELeave) CSqlQuery;
|
sl@0
|
483 |
CleanupStack::PushL(query);
|
sl@0
|
484 |
if (ParseL(ESqlKeyword_select)==ESqlAsterisk)
|
sl@0
|
485 |
NextToken();
|
sl@0
|
486 |
else
|
sl@0
|
487 |
ColumnListL(query->iColumns);
|
sl@0
|
488 |
ParseL(ESqlKeyword_from);
|
sl@0
|
489 |
IdentifierL(query->iTable);
|
sl@0
|
490 |
if (Parse(ESqlKeyword_where))
|
sl@0
|
491 |
query->iSearchCondition=SearchConditionL();
|
sl@0
|
492 |
if (Parse(ESqlKeyword_order))
|
sl@0
|
493 |
{
|
sl@0
|
494 |
ParseL(ESqlKeyword_by);
|
sl@0
|
495 |
SortSpecificationL(query->SortSpecificationL());
|
sl@0
|
496 |
}
|
sl@0
|
497 |
EndL();
|
sl@0
|
498 |
return query;
|
sl@0
|
499 |
}
|
sl@0
|
500 |
|
sl@0
|
501 |
CSqlSearchCondition* TSqlParser::SearchConditionLC()
|
sl@0
|
502 |
//
|
sl@0
|
503 |
// Parse a standalone search-condition
|
sl@0
|
504 |
//
|
sl@0
|
505 |
{
|
sl@0
|
506 |
CSqlSearchCondition* sc=SearchConditionL();
|
sl@0
|
507 |
CleanupStack::PushL(sc);
|
sl@0
|
508 |
EndL();
|
sl@0
|
509 |
return sc;
|
sl@0
|
510 |
}
|
sl@0
|
511 |
|
sl@0
|
512 |
CSqlDDLStatement* TSqlParser::CreateTableLC()
|
sl@0
|
513 |
//
|
sl@0
|
514 |
// Parse a CREATE TABLE statement
|
sl@0
|
515 |
//
|
sl@0
|
516 |
{
|
sl@0
|
517 |
CSqlCreateTableStatement* statement=new(ELeave) CSqlCreateTableStatement;
|
sl@0
|
518 |
CleanupStack::PushL(statement);
|
sl@0
|
519 |
IdentifierL(statement->iName);
|
sl@0
|
520 |
ParseL(ESqlLeftBracket);
|
sl@0
|
521 |
TDbCol def;
|
sl@0
|
522 |
for (;;)
|
sl@0
|
523 |
{
|
sl@0
|
524 |
AddColumnSpecL(def);
|
sl@0
|
525 |
if (Parse(ESqlKeyword_not))
|
sl@0
|
526 |
{
|
sl@0
|
527 |
ParseL(ESqlKeyword_null);
|
sl@0
|
528 |
def.iAttributes|=TDbCol::ENotNull;
|
sl@0
|
529 |
}
|
sl@0
|
530 |
statement->iColumns.AddL(def);
|
sl@0
|
531 |
if (iToken!=ESqlComma)
|
sl@0
|
532 |
break;
|
sl@0
|
533 |
NextToken();
|
sl@0
|
534 |
}
|
sl@0
|
535 |
RightBracketL();
|
sl@0
|
536 |
return statement;
|
sl@0
|
537 |
}
|
sl@0
|
538 |
|
sl@0
|
539 |
CSqlDDLStatement* TSqlParser::DropTableLC()
|
sl@0
|
540 |
//
|
sl@0
|
541 |
// Parse a DROP TABLE statement
|
sl@0
|
542 |
//
|
sl@0
|
543 |
{
|
sl@0
|
544 |
CSqlDropTableStatement* statement=new(ELeave) CSqlDropTableStatement;
|
sl@0
|
545 |
CleanupStack::PushL(statement);
|
sl@0
|
546 |
IdentifierL(statement->iName);
|
sl@0
|
547 |
return statement;
|
sl@0
|
548 |
}
|
sl@0
|
549 |
|
sl@0
|
550 |
CSqlDDLStatement* TSqlParser::AlterTableLC()
|
sl@0
|
551 |
//
|
sl@0
|
552 |
// Parse a CREATE TABLE statement
|
sl@0
|
553 |
//
|
sl@0
|
554 |
{
|
sl@0
|
555 |
CSqlAlterTableStatement* statement=new(ELeave) CSqlAlterTableStatement;
|
sl@0
|
556 |
CleanupStack::PushL(statement);
|
sl@0
|
557 |
IdentifierL(statement->iName);
|
sl@0
|
558 |
TSqlTokenType t=Parse(ESqlKeyword_add);
|
sl@0
|
559 |
if (t!=ESqlNoToken)
|
sl@0
|
560 |
{
|
sl@0
|
561 |
TDbCol def;
|
sl@0
|
562 |
if (t==ESqlLeftBracket)
|
sl@0
|
563 |
{
|
sl@0
|
564 |
NextToken();
|
sl@0
|
565 |
for (;;)
|
sl@0
|
566 |
{
|
sl@0
|
567 |
t=AddColumnSpecL(def);
|
sl@0
|
568 |
statement->iAdd.AddL(def);
|
sl@0
|
569 |
if (t!=ESqlComma)
|
sl@0
|
570 |
break;
|
sl@0
|
571 |
NextToken();
|
sl@0
|
572 |
}
|
sl@0
|
573 |
RightBracketL();
|
sl@0
|
574 |
}
|
sl@0
|
575 |
else
|
sl@0
|
576 |
{
|
sl@0
|
577 |
AddColumnSpecL(def);
|
sl@0
|
578 |
statement->iAdd.AddL(def);
|
sl@0
|
579 |
}
|
sl@0
|
580 |
}
|
sl@0
|
581 |
t=Parse(ESqlKeyword_drop);
|
sl@0
|
582 |
if (t!=ESqlNoToken)
|
sl@0
|
583 |
{
|
sl@0
|
584 |
if (t!=ESqlLeftBracket)
|
sl@0
|
585 |
ColumnNameL(statement->iDrop);
|
sl@0
|
586 |
else
|
sl@0
|
587 |
{
|
sl@0
|
588 |
NextToken();
|
sl@0
|
589 |
ColumnListL(statement->iDrop);
|
sl@0
|
590 |
RightBracketL();
|
sl@0
|
591 |
}
|
sl@0
|
592 |
}
|
sl@0
|
593 |
return statement;
|
sl@0
|
594 |
}
|
sl@0
|
595 |
|
sl@0
|
596 |
CSqlDDLStatement* TSqlParser::CreateIndexLC(TBool aUnique)
|
sl@0
|
597 |
//
|
sl@0
|
598 |
// Parse a CREATE INDEX statement
|
sl@0
|
599 |
//
|
sl@0
|
600 |
{
|
sl@0
|
601 |
CSqlCreateIndexStatement* statement=new(ELeave) CSqlCreateIndexStatement;
|
sl@0
|
602 |
CleanupStack::PushL(statement);
|
sl@0
|
603 |
if (aUnique)
|
sl@0
|
604 |
statement->iKey.MakeUnique();
|
sl@0
|
605 |
IdentifierL(statement->iName);
|
sl@0
|
606 |
ParseL(ESqlKeyword_on);
|
sl@0
|
607 |
IdentifierL(statement->iTable);
|
sl@0
|
608 |
ParseL(ESqlLeftBracket);
|
sl@0
|
609 |
SortSpecificationL(statement->iKey);
|
sl@0
|
610 |
RightBracketL();
|
sl@0
|
611 |
return statement;
|
sl@0
|
612 |
}
|
sl@0
|
613 |
|
sl@0
|
614 |
CSqlDDLStatement* TSqlParser::DropIndexLC()
|
sl@0
|
615 |
//
|
sl@0
|
616 |
// Parse a DROP INDEX statement
|
sl@0
|
617 |
//
|
sl@0
|
618 |
{
|
sl@0
|
619 |
CSqlDropIndexStatement* statement=new(ELeave) CSqlDropIndexStatement;
|
sl@0
|
620 |
CleanupStack::PushL(statement);
|
sl@0
|
621 |
IdentifierL(statement->iName);
|
sl@0
|
622 |
ParseL(ESqlKeyword_from);
|
sl@0
|
623 |
IdentifierL(statement->iTable);
|
sl@0
|
624 |
return statement;
|
sl@0
|
625 |
}
|
sl@0
|
626 |
|
sl@0
|
627 |
CSqlDDLStatement* TSqlParser::DDLStatementLC()
|
sl@0
|
628 |
{
|
sl@0
|
629 |
CSqlDDLStatement* statement;
|
sl@0
|
630 |
if (Parse(ESqlKeyword_create))
|
sl@0
|
631 |
{
|
sl@0
|
632 |
if (Parse(ESqlKeyword_table))
|
sl@0
|
633 |
statement=CreateTableLC();
|
sl@0
|
634 |
else
|
sl@0
|
635 |
{
|
sl@0
|
636 |
TSqlTokenType unique=Parse(ESqlKeyword_unique);
|
sl@0
|
637 |
ParseL(ESqlKeyword_index);
|
sl@0
|
638 |
statement=CreateIndexLC(unique);
|
sl@0
|
639 |
}
|
sl@0
|
640 |
}
|
sl@0
|
641 |
else if (Parse(ESqlKeyword_drop))
|
sl@0
|
642 |
{
|
sl@0
|
643 |
if (Parse(ESqlKeyword_table))
|
sl@0
|
644 |
statement=DropTableLC();
|
sl@0
|
645 |
else
|
sl@0
|
646 |
{
|
sl@0
|
647 |
ParseL(ESqlKeyword_index);
|
sl@0
|
648 |
statement=DropIndexLC();
|
sl@0
|
649 |
}
|
sl@0
|
650 |
}
|
sl@0
|
651 |
else
|
sl@0
|
652 |
{
|
sl@0
|
653 |
ParseL(ESqlKeyword_alter);
|
sl@0
|
654 |
ParseL(ESqlKeyword_table);
|
sl@0
|
655 |
statement=AlterTableLC();
|
sl@0
|
656 |
}
|
sl@0
|
657 |
EndL();
|
sl@0
|
658 |
return statement;
|
sl@0
|
659 |
}
|
sl@0
|
660 |
|
sl@0
|
661 |
TSqlTokenType TSqlParser::ColumnValueL(CSqlValues& aValues)
|
sl@0
|
662 |
//
|
sl@0
|
663 |
// parse a column-value and add to aValues
|
sl@0
|
664 |
//
|
sl@0
|
665 |
{
|
sl@0
|
666 |
switch (iToken.Type())
|
sl@0
|
667 |
{
|
sl@0
|
668 |
case ESqlLiteralInt:
|
sl@0
|
669 |
case ESqlLiteralReal:
|
sl@0
|
670 |
case ESqlLiteralTime:
|
sl@0
|
671 |
case ESqlLiteralText:
|
sl@0
|
672 |
aValues.AddL(iToken.Literal());
|
sl@0
|
673 |
return NextToken();
|
sl@0
|
674 |
case ESqlIdentifier: // NULL
|
sl@0
|
675 |
{
|
sl@0
|
676 |
TSqlTokenType t=ParseL(ESqlKeyword_null);
|
sl@0
|
677 |
aValues.AddL(RSqlLiteral()); // default c'ted RSqlLiteral is null
|
sl@0
|
678 |
return t;
|
sl@0
|
679 |
}
|
sl@0
|
680 |
default: // SQL error
|
sl@0
|
681 |
return SqlErrorL();
|
sl@0
|
682 |
}
|
sl@0
|
683 |
}
|
sl@0
|
684 |
|
sl@0
|
685 |
CSqlDMLStatement* TSqlParser::InsertStatementLC()
|
sl@0
|
686 |
//
|
sl@0
|
687 |
// parse an insert-statement
|
sl@0
|
688 |
//
|
sl@0
|
689 |
{
|
sl@0
|
690 |
ParseL(ESqlKeyword_into);
|
sl@0
|
691 |
CSqlInsertStatement* statement=CSqlInsertStatement::NewLC();
|
sl@0
|
692 |
if (IdentifierL(statement->iQuery.iTable)==ESqlLeftBracket)
|
sl@0
|
693 |
{
|
sl@0
|
694 |
NextToken();
|
sl@0
|
695 |
ColumnListL(statement->iQuery.iColumns);
|
sl@0
|
696 |
RightBracketL();
|
sl@0
|
697 |
}
|
sl@0
|
698 |
ParseL(ESqlKeyword_values);
|
sl@0
|
699 |
ParseL(ESqlLeftBracket);
|
sl@0
|
700 |
CSqlValues& values=statement->ValuesL();
|
sl@0
|
701 |
while (ColumnValueL(values)==ESqlComma)
|
sl@0
|
702 |
NextToken();
|
sl@0
|
703 |
RightBracketL();
|
sl@0
|
704 |
return statement;
|
sl@0
|
705 |
}
|
sl@0
|
706 |
|
sl@0
|
707 |
CSqlDMLStatement* TSqlParser::UpdateStatementLC()
|
sl@0
|
708 |
//
|
sl@0
|
709 |
// parse an update-statement
|
sl@0
|
710 |
//
|
sl@0
|
711 |
{
|
sl@0
|
712 |
CSqlModifyStatement* statement=CSqlModifyStatement::NewLC();
|
sl@0
|
713 |
IdentifierL(statement->iQuery.iTable);
|
sl@0
|
714 |
ParseL(ESqlKeyword_set);
|
sl@0
|
715 |
CSqlValues& values=statement->ValuesL();
|
sl@0
|
716 |
for (;;)
|
sl@0
|
717 |
{
|
sl@0
|
718 |
ColumnNameL(statement->iQuery.iColumns);
|
sl@0
|
719 |
ParseL(ESqlEqual);
|
sl@0
|
720 |
if (ColumnValueL(values)!=ESqlComma)
|
sl@0
|
721 |
break;
|
sl@0
|
722 |
NextToken();
|
sl@0
|
723 |
}
|
sl@0
|
724 |
if (Parse(ESqlKeyword_where))
|
sl@0
|
725 |
statement->iQuery.iSearchCondition=SearchConditionL();
|
sl@0
|
726 |
return statement;
|
sl@0
|
727 |
}
|
sl@0
|
728 |
|
sl@0
|
729 |
CSqlDMLStatement* TSqlParser::DeleteStatementLC()
|
sl@0
|
730 |
//
|
sl@0
|
731 |
// parse a delete-statement
|
sl@0
|
732 |
//
|
sl@0
|
733 |
{
|
sl@0
|
734 |
ParseL(ESqlKeyword_from);
|
sl@0
|
735 |
CSqlModifyStatement* statement=CSqlModifyStatement::NewLC();
|
sl@0
|
736 |
IdentifierL(statement->iQuery.iTable);
|
sl@0
|
737 |
if (Parse(ESqlKeyword_where))
|
sl@0
|
738 |
statement->iQuery.iSearchCondition=SearchConditionL();
|
sl@0
|
739 |
return statement;
|
sl@0
|
740 |
}
|
sl@0
|
741 |
|
sl@0
|
742 |
CSqlDMLStatement* TSqlParser::DMLStatementLC()
|
sl@0
|
743 |
{
|
sl@0
|
744 |
CSqlDMLStatement* statement;
|
sl@0
|
745 |
if (Parse(ESqlKeyword_insert))
|
sl@0
|
746 |
statement=InsertStatementLC();
|
sl@0
|
747 |
else if (Parse(ESqlKeyword_update))
|
sl@0
|
748 |
statement=UpdateStatementLC();
|
sl@0
|
749 |
else
|
sl@0
|
750 |
{
|
sl@0
|
751 |
ParseL(ESqlKeyword_delete);
|
sl@0
|
752 |
statement=DeleteStatementLC();
|
sl@0
|
753 |
}
|
sl@0
|
754 |
EndL();
|
sl@0
|
755 |
return statement;
|
sl@0
|
756 |
}
|
sl@0
|
757 |
|
sl@0
|
758 |
Sql::TStatementType TSqlParser::Type()
|
sl@0
|
759 |
{
|
sl@0
|
760 |
TSqlKeyword k=TSqlLexer::Keyword(iToken);
|
sl@0
|
761 |
if (k==ESqlKeyword_create || k==ESqlKeyword_alter || k==ESqlKeyword_drop)
|
sl@0
|
762 |
return Sql::EDDL;
|
sl@0
|
763 |
if (k==ESqlKeyword_insert || k==ESqlKeyword_update || k==ESqlKeyword_delete)
|
sl@0
|
764 |
return Sql::EDML;
|
sl@0
|
765 |
return Sql::ENone;
|
sl@0
|
766 |
}
|
sl@0
|
767 |
|
sl@0
|
768 |
TInt TSqlParser::PatternFilter(const TDesC& aPattern,const TChar aEscape,TText *aNewPatternBuffer )
|
sl@0
|
769 |
{
|
sl@0
|
770 |
TInt length = aPattern.Length();
|
sl@0
|
771 |
TInt count =0;
|
sl@0
|
772 |
|
sl@0
|
773 |
// Ensure that the pattern begins and ends with an '*'
|
sl@0
|
774 |
if ((length < 2) || (aPattern[0] != KMatchAny || aPattern[length-1] != KMatchAny))
|
sl@0
|
775 |
{
|
sl@0
|
776 |
return KSqlError;
|
sl@0
|
777 |
}
|
sl@0
|
778 |
|
sl@0
|
779 |
for (TInt i = 1 ; i< length -1 ;++i)
|
sl@0
|
780 |
{
|
sl@0
|
781 |
if (aPattern[i]== (TUint)aEscape)
|
sl@0
|
782 |
{
|
sl@0
|
783 |
if ((aPattern[i + 1] == KMatchAny) || (aPattern[i + 1] == KMatchOne) || (aPattern[i + 1] == (TUint)aEscape))
|
sl@0
|
784 |
{
|
sl@0
|
785 |
i++;
|
sl@0
|
786 |
aNewPatternBuffer[count++] = aPattern[i];
|
sl@0
|
787 |
}
|
sl@0
|
788 |
else
|
sl@0
|
789 |
{
|
sl@0
|
790 |
return KSqlError;
|
sl@0
|
791 |
}
|
sl@0
|
792 |
}
|
sl@0
|
793 |
else
|
sl@0
|
794 |
{
|
sl@0
|
795 |
if ((aPattern[i] == KMatchAny || aPattern[i] == KMatchOne) )
|
sl@0
|
796 |
{
|
sl@0
|
797 |
return KSqlError;
|
sl@0
|
798 |
}
|
sl@0
|
799 |
else
|
sl@0
|
800 |
{
|
sl@0
|
801 |
aNewPatternBuffer[count++] = aPattern[i];
|
sl@0
|
802 |
}
|
sl@0
|
803 |
}
|
sl@0
|
804 |
}
|
sl@0
|
805 |
|
sl@0
|
806 |
return count;
|
sl@0
|
807 |
}
|