wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #define SQLITE_MAX_EXPR_DEPTH |
2 | |||
3 | using System; |
||
4 | using System.Diagnostics; |
||
5 | using System.Text; |
||
6 | |||
7 | using Bitmask = System.UInt64; |
||
8 | using i64 = System.Int64; |
||
9 | using u8 = System.Byte; |
||
10 | using u32 = System.UInt32; |
||
11 | using u16 = System.UInt16; |
||
12 | |||
13 | using Pgno = System.UInt32; |
||
14 | |||
15 | #if !SQLITE_MAX_VARIABLE_NUMBER |
||
16 | using ynVar = System.Int16; |
||
17 | #else |
||
18 | using ynVar = System.Int32; |
||
19 | #endif |
||
20 | |||
21 | namespace Community.CsharpSqlite |
||
22 | { |
||
23 | public partial class Sqlite3 |
||
24 | { |
||
25 | /* |
||
26 | ** 2001 September 15 |
||
27 | ** |
||
28 | ** The author disclaims copyright to this source code. In place of |
||
29 | ** a legal notice, here is a blessing: |
||
30 | ** |
||
31 | ** May you do good and not evil. |
||
32 | ** May you find forgiveness for yourself and forgive others. |
||
33 | ** May you share freely, never taking more than you give. |
||
34 | ** |
||
35 | ************************************************************************* |
||
36 | ** This file contains routines used for analyzing expressions and |
||
37 | ** for generating VDBE code that evaluates expressions in SQLite. |
||
38 | ************************************************************************* |
||
39 | ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart |
||
40 | ** C#-SQLite is an independent reimplementation of the SQLite software library |
||
41 | ** |
||
42 | ** SQLITE_SOURCE_ID: 2011-06-23 19:49:22 4374b7e83ea0a3fbc3691f9c0c936272862f32f2 |
||
43 | ** |
||
44 | ************************************************************************* |
||
45 | */ |
||
46 | //#include "sqliteInt.h" |
||
47 | |||
48 | /* |
||
49 | ** Return the 'affinity' of the expression pExpr if any. |
||
50 | ** |
||
51 | ** If pExpr is a column, a reference to a column via an 'AS' alias, |
||
52 | ** or a sub-select with a column as the return value, then the |
||
53 | ** affinity of that column is returned. Otherwise, 0x00 is returned, |
||
54 | ** indicating no affinity for the expression. |
||
55 | ** |
||
56 | ** i.e. the WHERE clause expresssions in the following statements all |
||
57 | ** have an affinity: |
||
58 | ** |
||
59 | ** CREATE TABLE t1(a); |
||
60 | ** SELECT * FROM t1 WHERE a; |
||
61 | ** SELECT a AS b FROM t1 WHERE b; |
||
62 | ** SELECT * FROM t1 WHERE (select a from t1); |
||
63 | */ |
||
64 | static char sqlite3ExprAffinity( Expr pExpr ) |
||
65 | { |
||
66 | int op = pExpr.op; |
||
67 | if ( op == TK_SELECT ) |
||
68 | { |
||
69 | Debug.Assert( ( pExpr.flags & EP_xIsSelect ) != 0 ); |
||
70 | return sqlite3ExprAffinity( pExpr.x.pSelect.pEList.a[0].pExpr ); |
||
71 | } |
||
72 | #if !SQLITE_OMIT_CAST |
||
73 | if ( op == TK_CAST ) |
||
74 | { |
||
75 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
76 | return sqlite3AffinityType( pExpr.u.zToken ); |
||
77 | } |
||
78 | #endif |
||
79 | if ( ( op == TK_AGG_COLUMN || op == TK_COLUMN || op == TK_REGISTER ) |
||
80 | && pExpr.pTab != null |
||
81 | ) |
||
82 | { |
||
83 | /* op==TK_REGISTER && pExpr.pTab!=0 happens when pExpr was originally |
||
84 | ** a TK_COLUMN but was previously evaluated and cached in a register */ |
||
85 | int j = pExpr.iColumn; |
||
86 | if ( j < 0 ) |
||
87 | return SQLITE_AFF_INTEGER; |
||
88 | Debug.Assert( pExpr.pTab != null && j < pExpr.pTab.nCol ); |
||
89 | return pExpr.pTab.aCol[j].affinity; |
||
90 | } |
||
91 | return pExpr.affinity; |
||
92 | } |
||
93 | |||
94 | /* |
||
95 | ** Set the explicit collating sequence for an expression to the |
||
96 | ** collating sequence supplied in the second argument. |
||
97 | */ |
||
98 | static Expr sqlite3ExprSetColl( Expr pExpr, CollSeq pColl ) |
||
99 | { |
||
100 | if ( pExpr != null && pColl != null ) |
||
101 | { |
||
102 | pExpr.pColl = pColl; |
||
103 | pExpr.flags |= EP_ExpCollate; |
||
104 | } |
||
105 | return pExpr; |
||
106 | } |
||
107 | |||
108 | /* |
||
109 | ** Set the collating sequence for expression pExpr to be the collating |
||
110 | ** sequence named by pToken. Return a pointer to the revised expression. |
||
111 | ** The collating sequence is marked as "explicit" using the EP_ExpCollate |
||
112 | ** flag. An explicit collating sequence will override implicit |
||
113 | ** collating sequences. |
||
114 | */ |
||
115 | static Expr sqlite3ExprSetCollByToken( Parse pParse, Expr pExpr, Token pCollName ) |
||
116 | { |
||
117 | string zColl; /* Dequoted name of collation sequence */ |
||
118 | CollSeq pColl; |
||
119 | sqlite3 db = pParse.db; |
||
120 | zColl = sqlite3NameFromToken( db, pCollName ); |
||
121 | pColl = sqlite3LocateCollSeq( pParse, zColl ); |
||
122 | sqlite3ExprSetColl( pExpr, pColl ); |
||
123 | sqlite3DbFree( db, ref zColl ); |
||
124 | return pExpr; |
||
125 | } |
||
126 | |||
127 | /* |
||
128 | ** Return the default collation sequence for the expression pExpr. If |
||
129 | ** there is no default collation type, return 0. |
||
130 | */ |
||
131 | static CollSeq sqlite3ExprCollSeq( Parse pParse, Expr pExpr ) |
||
132 | { |
||
133 | CollSeq pColl = null; |
||
134 | Expr p = pExpr; |
||
135 | while ( ALWAYS( p ) ) |
||
136 | { |
||
137 | int op; |
||
138 | pColl = pExpr.pColl; |
||
139 | if ( pColl != null ) |
||
140 | break; |
||
141 | op = p.op; |
||
142 | if ( p.pTab != null && ( |
||
143 | op == TK_AGG_COLUMN || op == TK_COLUMN || op == TK_REGISTER || op == TK_TRIGGER |
||
144 | ) ) |
||
145 | { |
||
146 | /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally |
||
147 | ** a TK_COLUMN but was previously evaluated and cached in a register */ |
||
148 | string zColl; |
||
149 | int j = p.iColumn; |
||
150 | if ( j >= 0 ) |
||
151 | { |
||
152 | sqlite3 db = pParse.db; |
||
153 | zColl = p.pTab.aCol[j].zColl; |
||
154 | pColl = sqlite3FindCollSeq( db, ENC( db ), zColl, 0 ); |
||
155 | pExpr.pColl = pColl; |
||
156 | } |
||
157 | break; |
||
158 | } |
||
159 | if ( op != TK_CAST && op != TK_UPLUS ) |
||
160 | { |
||
161 | break; |
||
162 | } |
||
163 | p = p.pLeft; |
||
164 | } |
||
165 | if ( sqlite3CheckCollSeq( pParse, pColl ) != 0 ) |
||
166 | { |
||
167 | pColl = null; |
||
168 | } |
||
169 | return pColl; |
||
170 | } |
||
171 | |||
172 | /* |
||
173 | ** pExpr is an operand of a comparison operator. aff2 is the |
||
174 | ** type affinity of the other operand. This routine returns the |
||
175 | ** type affinity that should be used for the comparison operator. |
||
176 | */ |
||
177 | static char sqlite3CompareAffinity( Expr pExpr, char aff2 ) |
||
178 | { |
||
179 | char aff1 = sqlite3ExprAffinity( pExpr ); |
||
180 | if ( aff1 != '\0' && aff2 != '\0' ) |
||
181 | { |
||
182 | /* Both sides of the comparison are columns. If one has numeric |
||
183 | ** affinity, use that. Otherwise use no affinity. |
||
184 | */ |
||
185 | if ( aff1 >= SQLITE_AFF_NUMERIC || aff2 >= SQLITE_AFF_NUMERIC ) |
||
186 | // if (sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2)) |
||
187 | { |
||
188 | return SQLITE_AFF_NUMERIC; |
||
189 | } |
||
190 | else |
||
191 | { |
||
192 | return SQLITE_AFF_NONE; |
||
193 | } |
||
194 | } |
||
195 | else if ( aff1 == '\0' && aff2 == '\0' ) |
||
196 | { |
||
197 | /* Neither side of the comparison is a column. Compare the |
||
198 | ** results directly. |
||
199 | */ |
||
200 | return SQLITE_AFF_NONE; |
||
201 | } |
||
202 | else |
||
203 | { |
||
204 | /* One side is a column, the other is not. Use the columns affinity. */ |
||
205 | Debug.Assert( aff1 == 0 || aff2 == 0 ); |
||
206 | return ( aff1 != '\0' ? aff1 : aff2 ); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /* |
||
211 | ** pExpr is a comparison operator. Return the type affinity that should |
||
212 | ** be applied to both operands prior to doing the comparison. |
||
213 | */ |
||
214 | static char comparisonAffinity( Expr pExpr ) |
||
215 | { |
||
216 | char aff; |
||
217 | Debug.Assert( pExpr.op == TK_EQ || pExpr.op == TK_IN || pExpr.op == TK_LT || |
||
218 | pExpr.op == TK_GT || pExpr.op == TK_GE || pExpr.op == TK_LE || |
||
219 | pExpr.op == TK_NE || pExpr.op == TK_IS || pExpr.op == TK_ISNOT ); |
||
220 | Debug.Assert( pExpr.pLeft != null ); |
||
221 | aff = sqlite3ExprAffinity( pExpr.pLeft ); |
||
222 | if ( pExpr.pRight != null ) |
||
223 | { |
||
224 | aff = sqlite3CompareAffinity( pExpr.pRight, aff ); |
||
225 | } |
||
226 | else if ( ExprHasProperty( pExpr, EP_xIsSelect ) ) |
||
227 | { |
||
228 | aff = sqlite3CompareAffinity( pExpr.x.pSelect.pEList.a[0].pExpr, aff ); |
||
229 | } |
||
230 | else if ( aff == '\0' ) |
||
231 | { |
||
232 | aff = SQLITE_AFF_NONE; |
||
233 | } |
||
234 | return aff; |
||
235 | } |
||
236 | |||
237 | /* |
||
238 | ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. |
||
239 | ** idx_affinity is the affinity of an indexed column. Return true |
||
240 | ** if the index with affinity idx_affinity may be used to implement |
||
241 | ** the comparison in pExpr. |
||
242 | */ |
||
243 | static bool sqlite3IndexAffinityOk( Expr pExpr, char idx_affinity ) |
||
244 | { |
||
245 | char aff = comparisonAffinity( pExpr ); |
||
246 | switch ( aff ) |
||
247 | { |
||
248 | case SQLITE_AFF_NONE: |
||
249 | return true; |
||
250 | case SQLITE_AFF_TEXT: |
||
251 | return idx_affinity == SQLITE_AFF_TEXT; |
||
252 | default: |
||
253 | return idx_affinity >= SQLITE_AFF_NUMERIC;// sqlite3IsNumericAffinity(idx_affinity); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /* |
||
258 | ** Return the P5 value that should be used for a binary comparison |
||
259 | ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. |
||
260 | */ |
||
261 | static u8 binaryCompareP5( Expr pExpr1, Expr pExpr2, int jumpIfNull ) |
||
262 | { |
||
263 | u8 aff = (u8)sqlite3ExprAffinity( pExpr2 ); |
||
264 | aff = (u8)( (u8)sqlite3CompareAffinity( pExpr1, (char)aff ) | (u8)jumpIfNull ); |
||
265 | return aff; |
||
266 | } |
||
267 | |||
268 | /* |
||
269 | ** Return a pointer to the collation sequence that should be used by |
||
270 | ** a binary comparison operator comparing pLeft and pRight. |
||
271 | ** |
||
272 | ** If the left hand expression has a collating sequence type, then it is |
||
273 | ** used. Otherwise the collation sequence for the right hand expression |
||
274 | ** is used, or the default (BINARY) if neither expression has a collating |
||
275 | ** type. |
||
276 | ** |
||
277 | ** Argument pRight (but not pLeft) may be a null pointer. In this case, |
||
278 | ** it is not considered. |
||
279 | */ |
||
280 | static CollSeq sqlite3BinaryCompareCollSeq( |
||
281 | Parse pParse, |
||
282 | Expr pLeft, |
||
283 | Expr pRight |
||
284 | ) |
||
285 | { |
||
286 | CollSeq pColl; |
||
287 | Debug.Assert( pLeft != null ); |
||
288 | if ( ( pLeft.flags & EP_ExpCollate ) != 0 ) |
||
289 | { |
||
290 | Debug.Assert( pLeft.pColl != null ); |
||
291 | pColl = pLeft.pColl; |
||
292 | } |
||
293 | else if ( pRight != null && ( ( pRight.flags & EP_ExpCollate ) != 0 ) ) |
||
294 | { |
||
295 | Debug.Assert( pRight.pColl != null ); |
||
296 | pColl = pRight.pColl; |
||
297 | } |
||
298 | else |
||
299 | { |
||
300 | pColl = sqlite3ExprCollSeq( pParse, pLeft ); |
||
301 | if ( pColl == null ) |
||
302 | { |
||
303 | pColl = sqlite3ExprCollSeq( pParse, pRight ); |
||
304 | } |
||
305 | } |
||
306 | return pColl; |
||
307 | } |
||
308 | |||
309 | /* |
||
310 | ** Generate code for a comparison operator. |
||
311 | */ |
||
312 | static int codeCompare( |
||
313 | Parse pParse, /* The parsing (and code generating) context */ |
||
314 | Expr pLeft, /* The left operand */ |
||
315 | Expr pRight, /* The right operand */ |
||
316 | int opcode, /* The comparison opcode */ |
||
317 | int in1, int in2, /* Register holding operands */ |
||
318 | int dest, /* Jump here if true. */ |
||
319 | int jumpIfNull /* If true, jump if either operand is NULL */ |
||
320 | ) |
||
321 | { |
||
322 | int p5; |
||
323 | int addr; |
||
324 | CollSeq p4; |
||
325 | |||
326 | p4 = sqlite3BinaryCompareCollSeq( pParse, pLeft, pRight ); |
||
327 | p5 = binaryCompareP5( pLeft, pRight, jumpIfNull ); |
||
328 | addr = sqlite3VdbeAddOp4( pParse.pVdbe, opcode, in2, dest, in1, |
||
329 | p4, P4_COLLSEQ ); |
||
330 | sqlite3VdbeChangeP5( pParse.pVdbe, (u8)p5 ); |
||
331 | return addr; |
||
332 | } |
||
333 | |||
334 | #if SQLITE_MAX_EXPR_DEPTH //>0 |
||
335 | /* |
||
336 | ** Check that argument nHeight is less than or equal to the maximum |
||
337 | ** expression depth allowed. If it is not, leave an error message in |
||
338 | ** pParse. |
||
339 | */ |
||
340 | static int sqlite3ExprCheckHeight( Parse pParse, int nHeight ) |
||
341 | { |
||
342 | int rc = SQLITE_OK; |
||
343 | int mxHeight = pParse.db.aLimit[SQLITE_LIMIT_EXPR_DEPTH]; |
||
344 | if ( nHeight > mxHeight ) |
||
345 | { |
||
346 | sqlite3ErrorMsg( pParse, |
||
347 | "Expression tree is too large (maximum depth %d)", mxHeight |
||
348 | ); |
||
349 | rc = SQLITE_ERROR; |
||
350 | } |
||
351 | return rc; |
||
352 | } |
||
353 | |||
354 | /* The following three functions, heightOfExpr(), heightOfExprList() |
||
355 | ** and heightOfSelect(), are used to determine the maximum height |
||
356 | ** of any expression tree referenced by the structure passed as the |
||
357 | ** first argument. |
||
358 | ** |
||
359 | ** If this maximum height is greater than the current value pointed |
||
360 | ** to by pnHeight, the second parameter, then set pnHeight to that |
||
361 | ** value. |
||
362 | */ |
||
363 | static void heightOfExpr( Expr p, ref int pnHeight ) |
||
364 | { |
||
365 | if ( p != null ) |
||
366 | { |
||
367 | if ( p.nHeight > pnHeight ) |
||
368 | { |
||
369 | pnHeight = p.nHeight; |
||
370 | } |
||
371 | } |
||
372 | } |
||
373 | static void heightOfExprList( ExprList p, ref int pnHeight ) |
||
374 | { |
||
375 | if ( p != null ) |
||
376 | { |
||
377 | int i; |
||
378 | for ( i = 0; i < p.nExpr; i++ ) |
||
379 | { |
||
380 | heightOfExpr( p.a[i].pExpr, ref pnHeight ); |
||
381 | } |
||
382 | } |
||
383 | } |
||
384 | static void heightOfSelect( Select p, ref int pnHeight ) |
||
385 | { |
||
386 | if ( p != null ) |
||
387 | { |
||
388 | heightOfExpr( p.pWhere, ref pnHeight ); |
||
389 | heightOfExpr( p.pHaving, ref pnHeight ); |
||
390 | heightOfExpr( p.pLimit, ref pnHeight ); |
||
391 | heightOfExpr( p.pOffset, ref pnHeight ); |
||
392 | heightOfExprList( p.pEList, ref pnHeight ); |
||
393 | heightOfExprList( p.pGroupBy, ref pnHeight ); |
||
394 | heightOfExprList( p.pOrderBy, ref pnHeight ); |
||
395 | heightOfSelect( p.pPrior, ref pnHeight ); |
||
396 | } |
||
397 | } |
||
398 | |||
399 | /* |
||
400 | ** Set the Expr.nHeight variable in the structure passed as an |
||
401 | ** argument. An expression with no children, Expr.x.pList or |
||
402 | ** Expr.x.pSelect member has a height of 1. Any other expression |
||
403 | ** has a height equal to the maximum height of any other |
||
404 | ** referenced Expr plus one. |
||
405 | */ |
||
406 | static void exprSetHeight( Expr p ) |
||
407 | { |
||
408 | int nHeight = 0; |
||
409 | heightOfExpr( p.pLeft, ref nHeight ); |
||
410 | heightOfExpr( p.pRight, ref nHeight ); |
||
411 | if ( ExprHasProperty( p, EP_xIsSelect ) ) |
||
412 | { |
||
413 | heightOfSelect( p.x.pSelect, ref nHeight ); |
||
414 | } |
||
415 | else |
||
416 | { |
||
417 | heightOfExprList( p.x.pList, ref nHeight ); |
||
418 | } |
||
419 | p.nHeight = nHeight + 1; |
||
420 | } |
||
421 | |||
422 | /* |
||
423 | ** Set the Expr.nHeight variable using the exprSetHeight() function. If |
||
424 | ** the height is greater than the maximum allowed expression depth, |
||
425 | ** leave an error in pParse. |
||
426 | */ |
||
427 | static void sqlite3ExprSetHeight( Parse pParse, Expr p ) |
||
428 | { |
||
429 | exprSetHeight( p ); |
||
430 | sqlite3ExprCheckHeight( pParse, p.nHeight ); |
||
431 | } |
||
432 | |||
433 | /* |
||
434 | ** Return the maximum height of any expression tree referenced |
||
435 | ** by the select statement passed as an argument. |
||
436 | */ |
||
437 | static int sqlite3SelectExprHeight( Select p ) |
||
438 | { |
||
439 | int nHeight = 0; |
||
440 | heightOfSelect( p, ref nHeight ); |
||
441 | return nHeight; |
||
442 | } |
||
443 | #else |
||
444 | //#define exprSetHeight(y) |
||
445 | #endif //* SQLITE_MAX_EXPR_DEPTH>0 */ |
||
446 | |||
447 | /* |
||
448 | ** This routine is the core allocator for Expr nodes. |
||
449 | ** |
||
450 | ** Construct a new expression node and return a pointer to it. Memory |
||
451 | ** for this node and for the pToken argument is a single allocation |
||
452 | ** obtained from sqlite3DbMalloc(). The calling function |
||
453 | ** is responsible for making sure the node eventually gets freed. |
||
454 | ** |
||
455 | ** If dequote is true, then the token (if it exists) is dequoted. |
||
456 | ** If dequote is false, no dequoting is performance. The deQuote |
||
457 | ** parameter is ignored if pToken is NULL or if the token does not |
||
458 | ** appear to be quoted. If the quotes were of the form "..." (double-quotes) |
||
459 | ** then the EP_DblQuoted flag is set on the expression node. |
||
460 | ** |
||
461 | ** Special case: If op==TK_INTEGER and pToken points to a string that |
||
462 | ** can be translated into a 32-bit integer, then the token is not |
||
463 | ** stored in u.zToken. Instead, the integer values is written |
||
464 | ** into u.iValue and the EP_IntValue flag is set. No extra storage |
||
465 | ** is allocated to hold the integer text and the dequote flag is ignored. |
||
466 | */ |
||
467 | static Expr sqlite3ExprAlloc( |
||
468 | sqlite3 db, /* Handle for sqlite3DbMallocZero() (may be null) */ |
||
469 | int op, /* Expression opcode */ |
||
470 | Token pToken, /* Token argument. Might be NULL */ |
||
471 | int dequote /* True to dequote */ |
||
472 | ) |
||
473 | { |
||
474 | Expr pNew; |
||
475 | int nExtra = 0; |
||
476 | int iValue = 0; |
||
477 | |||
478 | if ( pToken != null ) |
||
479 | { |
||
480 | if ( op != TK_INTEGER || pToken.z == null || pToken.z.Length == 0 |
||
481 | || sqlite3GetInt32( pToken.z.ToString(), ref iValue ) == false ) |
||
482 | { |
||
483 | nExtra = pToken.n + 1; |
||
484 | Debug.Assert( iValue >= 0 ); |
||
485 | } |
||
486 | } |
||
487 | pNew = new Expr();//sqlite3DbMallocZero(db, sizeof(Expr)+nExtra); |
||
488 | if ( pNew != null ) |
||
489 | { |
||
490 | pNew.op = (u8)op; |
||
491 | pNew.iAgg = -1; |
||
492 | if ( pToken != null ) |
||
493 | { |
||
494 | if ( nExtra == 0 ) |
||
495 | { |
||
496 | pNew.flags |= EP_IntValue; |
||
497 | pNew.u.iValue = iValue; |
||
498 | } |
||
499 | else |
||
500 | { |
||
501 | int c; |
||
502 | //pNew.u.zToken = (char)&pNew[1]; |
||
503 | if ( pToken.n > 0 ) |
||
504 | pNew.u.zToken = pToken.z.Substring( 0, pToken.n );//memcpy(pNew.u.zToken, pToken.z, pToken.n); |
||
505 | else if ( pToken.n == 0 && string.IsNullOrEmpty(pToken.z)) |
||
506 | pNew.u.zToken = string.Empty; |
||
507 | //pNew.u.zToken[pToken.n] = 0; |
||
508 | if ( dequote != 0 && nExtra >= 3 |
||
509 | && ( ( c = pToken.z[0] ) == '\'' || c == '"' || c == '[' || c == '`' ) ) |
||
510 | { |
||
511 | #if DEBUG_CLASS_EXPR || DEBUG_CLASS_ALL |
||
512 | sqlite3Dequote(ref pNew.u._zToken); |
||
513 | #else |
||
514 | sqlite3Dequote( ref pNew.u.zToken ); |
||
515 | #endif |
||
516 | if ( c == '"' ) |
||
517 | pNew.flags |= EP_DblQuoted; |
||
518 | } |
||
519 | } |
||
520 | } |
||
521 | #if SQLITE_MAX_EXPR_DEPTH//>0 |
||
522 | pNew.nHeight = 1; |
||
523 | #endif |
||
524 | } |
||
525 | return pNew; |
||
526 | } |
||
527 | |||
528 | /* |
||
529 | ** Allocate a new expression node from a zero-terminated token that has |
||
530 | ** already been dequoted. |
||
531 | */ |
||
532 | static Expr sqlite3Expr( |
||
533 | sqlite3 db, /* Handle for sqlite3DbMallocZero() (may be null) */ |
||
534 | int op, /* Expression opcode */ |
||
535 | string zToken /* Token argument. Might be NULL */ |
||
536 | ) |
||
537 | { |
||
538 | Token x = new Token(); |
||
539 | x.z = zToken; |
||
540 | x.n = !string.IsNullOrEmpty( zToken ) ? sqlite3Strlen30( zToken ) : 0; |
||
541 | return sqlite3ExprAlloc( db, op, x, 0 ); |
||
542 | } |
||
543 | |||
544 | /* |
||
545 | ** Attach subtrees pLeft and pRight to the Expr node pRoot. |
||
546 | ** |
||
547 | ** If pRoot==NULL that means that a memory allocation error has occurred. |
||
548 | ** In that case, delete the subtrees pLeft and pRight. |
||
549 | */ |
||
550 | static void sqlite3ExprAttachSubtrees( |
||
551 | sqlite3 db, |
||
552 | Expr pRoot, |
||
553 | Expr pLeft, |
||
554 | Expr pRight |
||
555 | ) |
||
556 | { |
||
557 | if ( pRoot == null ) |
||
558 | { |
||
559 | //Debug.Assert( db.mallocFailed != 0 ); |
||
560 | sqlite3ExprDelete( db, ref pLeft ); |
||
561 | sqlite3ExprDelete( db, ref pRight ); |
||
562 | } |
||
563 | else |
||
564 | { |
||
565 | if ( pRight != null ) |
||
566 | { |
||
567 | pRoot.pRight = pRight; |
||
568 | if ( ( pRight.flags & EP_ExpCollate ) != 0 ) |
||
569 | { |
||
570 | pRoot.flags |= EP_ExpCollate; |
||
571 | pRoot.pColl = pRight.pColl; |
||
572 | } |
||
573 | } |
||
574 | if ( pLeft != null ) |
||
575 | { |
||
576 | pRoot.pLeft = pLeft; |
||
577 | if ( ( pLeft.flags & EP_ExpCollate ) != 0 ) |
||
578 | { |
||
579 | pRoot.flags |= EP_ExpCollate; |
||
580 | pRoot.pColl = pLeft.pColl; |
||
581 | } |
||
582 | } |
||
583 | exprSetHeight( pRoot ); |
||
584 | } |
||
585 | } |
||
586 | |||
587 | /* |
||
588 | ** Allocate a Expr node which joins as many as two subtrees. |
||
589 | ** |
||
590 | ** One or both of the subtrees can be NULL. Return a pointer to the new |
||
591 | ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, |
||
592 | ** free the subtrees and return NULL. |
||
593 | */ |
||
594 | // OVERLOADS, so I don't need to rewrite parse.c |
||
595 | static Expr sqlite3PExpr( Parse pParse, int op, int null_3, int null_4, int null_5 ) |
||
596 | { |
||
597 | return sqlite3PExpr( pParse, op, null, null, null ); |
||
598 | } |
||
599 | static Expr sqlite3PExpr( Parse pParse, int op, int null_3, int null_4, Token pToken ) |
||
600 | { |
||
601 | return sqlite3PExpr( pParse, op, null, null, pToken ); |
||
602 | } |
||
603 | static Expr sqlite3PExpr( Parse pParse, int op, Expr pLeft, int null_4, int null_5 ) |
||
604 | { |
||
605 | return sqlite3PExpr( pParse, op, pLeft, null, null ); |
||
606 | } |
||
607 | static Expr sqlite3PExpr( Parse pParse, int op, Expr pLeft, int null_4, Token pToken ) |
||
608 | { |
||
609 | return sqlite3PExpr( pParse, op, pLeft, null, pToken ); |
||
610 | } |
||
611 | static Expr sqlite3PExpr( Parse pParse, int op, Expr pLeft, Expr pRight, int null_5 ) |
||
612 | { |
||
613 | return sqlite3PExpr( pParse, op, pLeft, pRight, null ); |
||
614 | } |
||
615 | static Expr sqlite3PExpr( |
||
616 | Parse pParse, /* Parsing context */ |
||
617 | int op, /* Expression opcode */ |
||
618 | Expr pLeft, /* Left operand */ |
||
619 | Expr pRight, /* Right operand */ |
||
620 | Token pToken /* Argument Token */ |
||
621 | ) |
||
622 | { |
||
623 | Expr p = sqlite3ExprAlloc( pParse.db, op, pToken, 1 ); |
||
624 | sqlite3ExprAttachSubtrees( pParse.db, p, pLeft, pRight ); |
||
625 | if ( p != null ) |
||
626 | { |
||
627 | sqlite3ExprCheckHeight( pParse, p.nHeight ); |
||
628 | } |
||
629 | return p; |
||
630 | } |
||
631 | |||
632 | /* |
||
633 | ** Join two expressions using an AND operator. If either expression is |
||
634 | ** NULL, then just return the other expression. |
||
635 | */ |
||
636 | static Expr sqlite3ExprAnd( sqlite3 db, Expr pLeft, Expr pRight ) |
||
637 | { |
||
638 | if ( pLeft == null ) |
||
639 | { |
||
640 | return pRight; |
||
641 | } |
||
642 | else if ( pRight == null ) |
||
643 | { |
||
644 | return pLeft; |
||
645 | } |
||
646 | else |
||
647 | { |
||
648 | Expr pNew = sqlite3ExprAlloc( db, TK_AND, null, 0 ); |
||
649 | sqlite3ExprAttachSubtrees( db, pNew, pLeft, pRight ); |
||
650 | return pNew; |
||
651 | } |
||
652 | } |
||
653 | |||
654 | /* |
||
655 | ** Construct a new expression node for a function with multiple |
||
656 | ** arguments. |
||
657 | */ |
||
658 | // OVERLOADS, so I don't need to rewrite parse.c |
||
659 | static Expr sqlite3ExprFunction( Parse pParse, int null_2, Token pToken ) |
||
660 | { |
||
661 | return sqlite3ExprFunction( pParse, null, pToken ); |
||
662 | } |
||
663 | static Expr sqlite3ExprFunction( Parse pParse, ExprList pList, int null_3 ) |
||
664 | { |
||
665 | return sqlite3ExprFunction( pParse, pList, null ); |
||
666 | } |
||
667 | static Expr sqlite3ExprFunction( Parse pParse, ExprList pList, Token pToken ) |
||
668 | { |
||
669 | Expr pNew; |
||
670 | sqlite3 db = pParse.db; |
||
671 | Debug.Assert( pToken != null ); |
||
672 | pNew = sqlite3ExprAlloc( db, TK_FUNCTION, pToken, 1 ); |
||
673 | if ( pNew == null ) |
||
674 | { |
||
675 | sqlite3ExprListDelete( db, ref pList ); /* Avoid memory leak when malloc fails */ |
||
676 | return null; |
||
677 | } |
||
678 | pNew.x.pList = pList; |
||
679 | Debug.Assert( !ExprHasProperty( pNew, EP_xIsSelect ) ); |
||
680 | |||
681 | sqlite3ExprSetHeight( pParse, pNew ); |
||
682 | return pNew; |
||
683 | } |
||
684 | |||
685 | /* |
||
686 | ** Assign a variable number to an expression that encodes a wildcard |
||
687 | ** in the original SQL statement. |
||
688 | ** |
||
689 | ** Wildcards consisting of a single "?" are assigned the next sequential |
||
690 | ** variable number. |
||
691 | ** |
||
692 | ** Wildcards of the form "?nnn" are assigned the number "nnn". We make |
||
693 | ** sure "nnn" is not too be to avoid a denial of service attack when |
||
694 | ** the SQL statement comes from an external source. |
||
695 | ** |
||
696 | ** Wildcards of the form ":aaa", "@aaa" or "$aaa" are assigned the same number |
||
697 | ** as the previous instance of the same wildcard. Or if this is the first |
||
698 | ** instance of the wildcard, the next sequenial variable number is |
||
699 | ** assigned. |
||
700 | */ |
||
701 | static void sqlite3ExprAssignVarNumber( Parse pParse, Expr pExpr ) |
||
702 | { |
||
703 | sqlite3 db = pParse.db; |
||
704 | string z; |
||
705 | |||
706 | if ( pExpr == null ) |
||
707 | return; |
||
708 | Debug.Assert( !ExprHasAnyProperty( pExpr, EP_IntValue | EP_Reduced | EP_TokenOnly ) ); |
||
709 | z = pExpr.u.zToken; |
||
710 | Debug.Assert( z != null ); |
||
711 | Debug.Assert( z.Length != 0 ); |
||
712 | if ( z.Length == 1 ) |
||
713 | { |
||
714 | /* Wildcard of the form "?". Assign the next variable number */ |
||
715 | Debug.Assert( z[0] == '?' ); |
||
716 | pExpr.iColumn = (ynVar)( ++pParse.nVar ); |
||
717 | }else{ |
||
718 | ynVar x = 0; |
||
719 | int n = sqlite3Strlen30(z); |
||
720 | if( z[0]=='?' ){ |
||
721 | /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
||
722 | ** use it as the variable number */ |
||
723 | i64 i = 0; |
||
724 | bool bOk = 0 == sqlite3Atoi64( z.Substring( 1 ), ref i, n - 1, SQLITE_UTF8 ); |
||
725 | pExpr.iColumn = x=(ynVar)i; |
||
726 | testcase( i == 0 ); |
||
727 | testcase( i == 1 ); |
||
728 | testcase( i == db.aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] - 1 ); |
||
729 | testcase( i == db.aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
||
730 | if ( bOk == false || i < 1 || i > db.aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ) |
||
731 | { |
||
732 | sqlite3ErrorMsg( pParse, "variable number must be between ?1 and ?%d", |
||
733 | db.aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
||
734 | x=0; |
||
735 | } |
||
736 | if ( i > pParse.nVar ) |
||
737 | { |
||
738 | pParse.nVar = (int)i; |
||
739 | } |
||
740 | } |
||
741 | else |
||
742 | { |
||
743 | /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable |
||
744 | ** number as the prior appearance of the same name, or if the name |
||
745 | ** has never appeared before, reuse the same variable number |
||
746 | */ |
||
747 | ynVar i; |
||
748 | for(i=0; i<pParse.nzVar; i++){ |
||
749 | if( pParse.azVar[i] != null && z.CompareTo(pParse.azVar[i] ) == 0 ) //memcmp(pParse.azVar[i],z,n+1)==0 ) |
||
750 | { |
||
751 | pExpr.iColumn = x = (ynVar)( i + 1 ); |
||
752 | break; |
||
753 | } |
||
754 | } |
||
755 | if( x==0 ) x = pExpr.iColumn = (ynVar)(++pParse.nVar); |
||
756 | } |
||
757 | if( x>0 ){ |
||
758 | if( x>pParse.nzVar ){ |
||
759 | //char **a; |
||
760 | //a = sqlite3DbRealloc(db, pParse.azVar, x*sizeof(a[0])); |
||
761 | //if( a==0 ) return; /* Error reported through db.mallocFailed */ |
||
762 | //pParse.azVar = a; |
||
763 | //memset(&a[pParse.nzVar], 0, (x-pParse.nzVar)*sizeof(a[0])); |
||
764 | Array.Resize( ref pParse.azVar, x ); |
||
765 | pParse.nzVar = x; |
||
766 | } |
||
767 | if( z[0]!='?' || pParse.azVar[x-1]==null ) |
||
768 | { |
||
769 | //sqlite3DbFree(db, pParse.azVar[x-1]); |
||
770 | pParse.azVar[x - 1] = z.Substring( 0, n );//sqlite3DbStrNDup( db, z, n ); |
||
771 | } |
||
772 | } |
||
773 | } |
||
774 | if ( pParse.nErr == 0 && pParse.nVar > db.aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ) |
||
775 | { |
||
776 | sqlite3ErrorMsg( pParse, "too many SQL variables" ); |
||
777 | } |
||
778 | } |
||
779 | |||
780 | /* |
||
781 | ** Recursively delete an expression tree. |
||
782 | */ |
||
783 | static void sqlite3ExprDelete( sqlite3 db, ref Expr p ) |
||
784 | { |
||
785 | if ( p == null ) |
||
786 | return; |
||
787 | /* Sanity check: Assert that the IntValue is non-negative if it exists */ |
||
788 | Debug.Assert( !ExprHasProperty( p, EP_IntValue ) || p.u.iValue >= 0 ); |
||
789 | if ( !ExprHasAnyProperty( p, EP_TokenOnly ) ) |
||
790 | { |
||
791 | sqlite3ExprDelete( db, ref p.pLeft ); |
||
792 | sqlite3ExprDelete( db, ref p.pRight ); |
||
793 | if ( !ExprHasProperty( p, EP_Reduced ) && ( p.flags2 & EP2_MallocedToken ) != 0 ) |
||
794 | { |
||
795 | #if DEBUG_CLASS_EXPR || DEBUG_CLASS_ALL |
||
796 | sqlite3DbFree( db, ref p.u._zToken ); |
||
797 | #else |
||
798 | sqlite3DbFree( db, ref p.u.zToken ); |
||
799 | #endif |
||
800 | } |
||
801 | if ( ExprHasProperty( p, EP_xIsSelect ) ) |
||
802 | { |
||
803 | sqlite3SelectDelete( db, ref p.x.pSelect ); |
||
804 | } |
||
805 | else |
||
806 | { |
||
807 | sqlite3ExprListDelete( db, ref p.x.pList ); |
||
808 | } |
||
809 | } |
||
810 | if ( !ExprHasProperty( p, EP_Static ) ) |
||
811 | { |
||
812 | sqlite3DbFree( db, ref p ); |
||
813 | } |
||
814 | } |
||
815 | |||
816 | /* |
||
817 | ** Return the number of bytes allocated for the expression structure |
||
818 | ** passed as the first argument. This is always one of EXPR_FULLSIZE, |
||
819 | ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. |
||
820 | */ |
||
821 | static int exprStructSize( Expr p ) |
||
822 | { |
||
823 | if ( ExprHasProperty( p, EP_TokenOnly ) ) |
||
824 | return EXPR_TOKENONLYSIZE; |
||
825 | if ( ExprHasProperty( p, EP_Reduced ) ) |
||
826 | return EXPR_REDUCEDSIZE; |
||
827 | return EXPR_FULLSIZE; |
||
828 | } |
||
829 | |||
830 | /* |
||
831 | ** The dupedExpr*Size() routines each return the number of bytes required |
||
832 | ** to store a copy of an expression or expression tree. They differ in |
||
833 | ** how much of the tree is measured. |
||
834 | ** |
||
835 | ** dupedExprStructSize() Size of only the Expr structure |
||
836 | ** dupedExprNodeSize() Size of Expr + space for token |
||
837 | ** dupedExprSize() Expr + token + subtree components |
||
838 | ** |
||
839 | *************************************************************************** |
||
840 | ** |
||
841 | ** The dupedExprStructSize() function returns two values OR-ed together: |
||
842 | ** (1) the space required for a copy of the Expr structure only and |
||
843 | ** (2) the EP_xxx flags that indicate what the structure size should be. |
||
844 | ** The return values is always one of: |
||
845 | ** |
||
846 | ** EXPR_FULLSIZE |
||
847 | ** EXPR_REDUCEDSIZE | EP_Reduced |
||
848 | ** EXPR_TOKENONLYSIZE | EP_TokenOnly |
||
849 | ** |
||
850 | ** The size of the structure can be found by masking the return value |
||
851 | ** of this routine with 0xfff. The flags can be found by masking the |
||
852 | ** return value with EP_Reduced|EP_TokenOnly. |
||
853 | ** |
||
854 | ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size |
||
855 | ** (unreduced) Expr objects as they or originally constructed by the parser. |
||
856 | ** During expression analysis, extra information is computed and moved into |
||
857 | ** later parts of teh Expr object and that extra information might get chopped |
||
858 | ** off if the expression is reduced. Note also that it does not work to |
||
859 | ** make a EXPRDUP_REDUCE copy of a reduced expression. It is only legal |
||
860 | ** to reduce a pristine expression tree from the parser. The implementation |
||
861 | ** of dupedExprStructSize() contain multiple Debug.Assert() statements that attempt |
||
862 | ** to enforce this constraint. |
||
863 | */ |
||
864 | static int dupedExprStructSize( Expr p, int flags ) |
||
865 | { |
||
866 | int nSize; |
||
867 | Debug.Assert( flags == EXPRDUP_REDUCE || flags == 0 ); /* Only one flag value allowed */ |
||
868 | if ( 0 == ( flags & EXPRDUP_REDUCE ) ) |
||
869 | { |
||
870 | nSize = EXPR_FULLSIZE; |
||
871 | } |
||
872 | else |
||
873 | { |
||
874 | Debug.Assert( !ExprHasAnyProperty( p, EP_TokenOnly | EP_Reduced ) ); |
||
875 | Debug.Assert( !ExprHasProperty( p, EP_FromJoin ) ); |
||
876 | Debug.Assert( ( p.flags2 & EP2_MallocedToken ) == 0 ); |
||
877 | Debug.Assert( ( p.flags2 & EP2_Irreducible ) == 0 ); |
||
878 | if ( p.pLeft != null || p.pRight != null || p.pColl != null || p.x.pList != null || p.x.pSelect != null ) |
||
879 | { |
||
880 | nSize = EXPR_REDUCEDSIZE | EP_Reduced; |
||
881 | } |
||
882 | else |
||
883 | { |
||
884 | nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; |
||
885 | } |
||
886 | } |
||
887 | return nSize; |
||
888 | } |
||
889 | |||
890 | /* |
||
891 | ** This function returns the space in bytes required to store the copy |
||
892 | ** of the Expr structure and a copy of the Expr.u.zToken string (if that |
||
893 | ** string is defined.) |
||
894 | */ |
||
895 | static int dupedExprNodeSize( Expr p, int flags ) |
||
896 | { |
||
897 | int nByte = dupedExprStructSize( p, flags ) & 0xfff; |
||
898 | if ( !ExprHasProperty( p, EP_IntValue ) && p.u.zToken != null ) |
||
899 | { |
||
900 | nByte += sqlite3Strlen30( p.u.zToken ) + 1; |
||
901 | } |
||
902 | return ROUND8( nByte ); |
||
903 | } |
||
904 | |||
905 | /* |
||
906 | ** Return the number of bytes required to create a duplicate of the |
||
907 | ** expression passed as the first argument. The second argument is a |
||
908 | ** mask containing EXPRDUP_XXX flags. |
||
909 | ** |
||
910 | ** The value returned includes space to create a copy of the Expr struct |
||
911 | ** itself and the buffer referred to by Expr.u.zToken, if any. |
||
912 | ** |
||
913 | ** If the EXPRDUP_REDUCE flag is set, then the return value includes |
||
914 | ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft |
||
915 | ** and Expr.pRight variables (but not for any structures pointed to or |
||
916 | ** descended from the Expr.x.pList or Expr.x.pSelect variables). |
||
917 | */ |
||
918 | static int dupedExprSize( Expr p, int flags ) |
||
919 | { |
||
920 | int nByte = 0; |
||
921 | if ( p != null ) |
||
922 | { |
||
923 | nByte = dupedExprNodeSize( p, flags ); |
||
924 | if ( ( flags & EXPRDUP_REDUCE ) != 0 ) |
||
925 | { |
||
926 | nByte += dupedExprSize( p.pLeft, flags ) + dupedExprSize( p.pRight, flags ); |
||
927 | } |
||
928 | } |
||
929 | return nByte; |
||
930 | } |
||
931 | |||
932 | /* |
||
933 | ** This function is similar to sqlite3ExprDup(), except that if pzBuffer |
||
934 | ** is not NULL then *pzBuffer is assumed to point to a buffer large enough |
||
935 | ** to store the copy of expression p, the copies of p->u.zToken |
||
936 | ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, |
||
937 | ** if any. Before returning, *pzBuffer is set to the first byte passed the |
||
938 | ** portion of the buffer copied into by this function. |
||
939 | */ |
||
940 | static Expr exprDup( sqlite3 db, Expr p, int flags, ref Expr pzBuffer ) |
||
941 | { |
||
942 | Expr pNew = null; /* Value to return */ |
||
943 | if ( p != null ) |
||
944 | { |
||
945 | bool isReduced = ( flags & EXPRDUP_REDUCE ) != 0; |
||
946 | ////Expr zAlloc = new Expr(); |
||
947 | u32 staticFlag = 0; |
||
948 | |||
949 | Debug.Assert( pzBuffer == null || isReduced ); |
||
950 | |||
951 | /* Figure out where to write the new Expr structure. */ |
||
952 | //if ( pzBuffer !=null) |
||
953 | //{ |
||
954 | // zAlloc = pzBuffer; |
||
955 | // staticFlag = EP_Static; |
||
956 | //} |
||
957 | //else |
||
958 | //{ |
||
959 | ///Expr zAlloc = new Expr();//sqlite3DbMallocRaw( db, dupedExprSize( p, flags ) ); |
||
960 | //} |
||
961 | // (Expr)zAlloc; |
||
962 | |||
963 | //if ( pNew != null ) |
||
964 | { |
||
965 | /* Set nNewSize to the size allocated for the structure pointed to |
||
966 | ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or |
||
967 | ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed |
||
968 | ** by the copy of the p->u.zToken string (if any). |
||
969 | */ |
||
970 | int nStructSize = dupedExprStructSize( p, flags ); |
||
971 | ////int nNewSize = nStructSize & 0xfff; |
||
972 | ////int nToken; |
||
973 | ////if ( !ExprHasProperty( p, EP_IntValue ) && !string.IsNullOrEmpty( p.u.zToken ) ) |
||
974 | ////{ |
||
975 | //// nToken = sqlite3Strlen30( p.u.zToken ); |
||
976 | ////} |
||
977 | ////else |
||
978 | ////{ |
||
979 | //// nToken = 0; |
||
980 | ////} |
||
981 | if ( isReduced ) |
||
982 | { |
||
983 | Debug.Assert( !ExprHasProperty( p, EP_Reduced ) ); |
||
984 | pNew = p.Copy( EXPR_TOKENONLYSIZE );////memcpy( zAlloc, p, nNewSize ); |
||
985 | } |
||
986 | else |
||
987 | { |
||
988 | ////int nSize = exprStructSize( p ); |
||
989 | ////memcpy( zAlloc, p, nSize ); |
||
990 | pNew = p.Copy(); |
||
991 | ////memset( &zAlloc[nSize], 0, EXPR_FULLSIZE - nSize ); |
||
992 | } |
||
993 | |||
994 | /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ |
||
995 | unchecked |
||
996 | { |
||
997 | pNew.flags &= (ushort)( ~( EP_Reduced | EP_TokenOnly | EP_Static ) ); |
||
998 | } |
||
999 | pNew.flags |= (ushort)( nStructSize & ( EP_Reduced | EP_TokenOnly ) ); |
||
1000 | pNew.flags |= (ushort)staticFlag; |
||
1001 | |||
1002 | /* Copy the p->u.zToken string, if any. */ |
||
1003 | ////if ( nToken != 0 ) |
||
1004 | ////{ |
||
1005 | //// string zToken;// = pNew.u.zToken = (char)&zAlloc[nNewSize]; |
||
1006 | //// zToken = p.u.zToken.Substring( 0, nToken );// memcpy( zToken, p.u.zToken, nToken ); |
||
1007 | ////} |
||
1008 | |||
1009 | if ( 0 == ( ( p.flags | pNew.flags ) & EP_TokenOnly ) ) |
||
1010 | { |
||
1011 | /* Fill in the pNew.x.pSelect or pNew.x.pList member. */ |
||
1012 | if ( ExprHasProperty( p, EP_xIsSelect ) ) |
||
1013 | { |
||
1014 | pNew.x.pSelect = sqlite3SelectDup( db, p.x.pSelect, isReduced ? 1 : 0 ); |
||
1015 | } |
||
1016 | else |
||
1017 | { |
||
1018 | pNew.x.pList = sqlite3ExprListDup( db, p.x.pList, isReduced ? 1 : 0 ); |
||
1019 | } |
||
1020 | } |
||
1021 | |||
1022 | /* Fill in pNew.pLeft and pNew.pRight. */ |
||
1023 | if ( ExprHasAnyProperty( pNew, EP_Reduced | EP_TokenOnly ) ) |
||
1024 | { |
||
1025 | //zAlloc += dupedExprNodeSize( p, flags ); |
||
1026 | if ( ExprHasProperty( pNew, EP_Reduced ) ) |
||
1027 | { |
||
1028 | pNew.pLeft = exprDup( db, p.pLeft, EXPRDUP_REDUCE, ref pzBuffer ); |
||
1029 | pNew.pRight = exprDup( db, p.pRight, EXPRDUP_REDUCE, ref pzBuffer ); |
||
1030 | } |
||
1031 | //if ( pzBuffer != null ) |
||
1032 | //{ |
||
1033 | // pzBuffer = zAlloc; |
||
1034 | //} |
||
1035 | } |
||
1036 | else |
||
1037 | { |
||
1038 | pNew.flags2 = 0; |
||
1039 | if ( !ExprHasAnyProperty( p, EP_TokenOnly ) ) |
||
1040 | { |
||
1041 | pNew.pLeft = sqlite3ExprDup( db, p.pLeft, 0 ); |
||
1042 | pNew.pRight = sqlite3ExprDup( db, p.pRight, 0 ); |
||
1043 | } |
||
1044 | } |
||
1045 | } |
||
1046 | } |
||
1047 | return pNew; |
||
1048 | } |
||
1049 | |||
1050 | /* |
||
1051 | ** The following group of routines make deep copies of expressions, |
||
1052 | ** expression lists, ID lists, and select statements. The copies can |
||
1053 | ** be deleted (by being passed to their respective ...Delete() routines) |
||
1054 | ** without effecting the originals. |
||
1055 | ** |
||
1056 | ** The expression list, ID, and source lists return by sqlite3ExprListDup(), |
||
1057 | ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded |
||
1058 | ** by subsequent calls to sqlite*ListAppend() routines. |
||
1059 | ** |
||
1060 | ** Any tables that the SrcList might point to are not duplicated. |
||
1061 | ** |
||
1062 | ** The flags parameter contains a combination of the EXPRDUP_XXX flags. |
||
1063 | ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a |
||
1064 | ** truncated version of the usual Expr structure that will be stored as |
||
1065 | ** part of the in-memory representation of the database schema. |
||
1066 | */ |
||
1067 | static Expr sqlite3ExprDup( sqlite3 db, Expr p, int flags ) |
||
1068 | { |
||
1069 | Expr ExprDummy = null; |
||
1070 | return exprDup( db, p, flags, ref ExprDummy ); |
||
1071 | } |
||
1072 | |||
1073 | static ExprList sqlite3ExprListDup( sqlite3 db, ExprList p, int flags ) |
||
1074 | { |
||
1075 | ExprList pNew; |
||
1076 | ExprList_item pItem; |
||
1077 | ExprList_item pOldItem; |
||
1078 | |||
1079 | if ( p == null ) |
||
1080 | return null; |
||
1081 | pNew = new ExprList();//sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
||
1082 | //if ( pNew == null ) return null; |
||
1083 | pNew.iECursor = 0; |
||
1084 | pNew.nExpr = pNew.nAlloc = p.nExpr; |
||
1085 | pNew.a = new ExprList_item[p.nExpr];//sqlite3DbMallocRaw(db, p.nExpr*sizeof(p.a[0]) ); |
||
1086 | //if( pItem==null ){ |
||
1087 | // sqlite3DbFree(db,ref pNew); |
||
1088 | // return null; |
||
1089 | //} |
||
1090 | //pOldItem = p.a; |
||
1091 | for (int i = 0; i < p.nExpr; i++ ) |
||
1092 | {//pItem++, pOldItem++){ |
||
1093 | pItem = pNew.a[i] = new ExprList_item(); |
||
1094 | pOldItem = p.a[i]; |
||
1095 | Expr pOldExpr = pOldItem.pExpr; |
||
1096 | pItem.pExpr = sqlite3ExprDup( db, pOldExpr, flags ); |
||
1097 | pItem.zName = pOldItem.zName;// sqlite3DbStrDup(db, pOldItem.zName); |
||
1098 | pItem.zSpan = pOldItem.zSpan;// sqlite3DbStrDup( db, pOldItem.zSpan ); |
||
1099 | pItem.sortOrder = pOldItem.sortOrder; |
||
1100 | pItem.done = 0; |
||
1101 | pItem.iCol = pOldItem.iCol; |
||
1102 | pItem.iAlias = pOldItem.iAlias; |
||
1103 | } |
||
1104 | return pNew; |
||
1105 | } |
||
1106 | |||
1107 | /* |
||
1108 | ** If cursors, triggers, views and subqueries are all omitted from |
||
1109 | ** the build, then none of the following routines, except for |
||
1110 | ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes |
||
1111 | ** called with a NULL argument. |
||
1112 | */ |
||
1113 | #if !SQLITE_OMIT_VIEW || !SQLITE_OMIT_TRIGGER || !SQLITE_OMIT_SUBQUERY |
||
1114 | static SrcList sqlite3SrcListDup( sqlite3 db, SrcList p, int flags ) |
||
1115 | { |
||
1116 | SrcList pNew; |
||
1117 | int nByte; |
||
1118 | if ( p == null ) |
||
1119 | return null; |
||
1120 | //nByte = sizeof(*p) + (p.nSrc>0 ? sizeof(p.a[0]) * (p.nSrc-1) : 0); |
||
1121 | pNew = new SrcList();//sqlite3DbMallocRaw(db, nByte ); |
||
1122 | if ( p.nSrc > 0 ) |
||
1123 | pNew.a = new SrcList_item[p.nSrc]; |
||
1124 | if ( pNew == null ) |
||
1125 | return null; |
||
1126 | pNew.nSrc = pNew.nAlloc = p.nSrc; |
||
1127 | for (int i = 0; i < p.nSrc; i++ ) |
||
1128 | { |
||
1129 | pNew.a[i] = new SrcList_item(); |
||
1130 | SrcList_item pNewItem = pNew.a[i]; |
||
1131 | SrcList_item pOldItem = p.a[i]; |
||
1132 | Table pTab; |
||
1133 | pNewItem.zDatabase = pOldItem.zDatabase;// sqlite3DbStrDup(db, pOldItem.zDatabase); |
||
1134 | pNewItem.zName = pOldItem.zName;// sqlite3DbStrDup(db, pOldItem.zName); |
||
1135 | pNewItem.zAlias = pOldItem.zAlias;// sqlite3DbStrDup(db, pOldItem.zAlias); |
||
1136 | pNewItem.jointype = pOldItem.jointype; |
||
1137 | pNewItem.iCursor = pOldItem.iCursor; |
||
1138 | pNewItem.isPopulated = pOldItem.isPopulated; |
||
1139 | pNewItem.zIndex = pOldItem.zIndex;// sqlite3DbStrDup( db, pOldItem.zIndex ); |
||
1140 | pNewItem.notIndexed = pOldItem.notIndexed; |
||
1141 | pNewItem.pIndex = pOldItem.pIndex; |
||
1142 | pTab = pNewItem.pTab = pOldItem.pTab; |
||
1143 | if ( pTab != null ) |
||
1144 | { |
||
1145 | pTab.nRef++; |
||
1146 | } |
||
1147 | pNewItem.pSelect = sqlite3SelectDup( db, pOldItem.pSelect, flags ); |
||
1148 | pNewItem.pOn = sqlite3ExprDup( db, pOldItem.pOn, flags ); |
||
1149 | pNewItem.pUsing = sqlite3IdListDup( db, pOldItem.pUsing ); |
||
1150 | pNewItem.colUsed = pOldItem.colUsed; |
||
1151 | } |
||
1152 | return pNew; |
||
1153 | } |
||
1154 | |||
1155 | static IdList sqlite3IdListDup( sqlite3 db, IdList p ) |
||
1156 | { |
||
1157 | IdList pNew; |
||
1158 | int i; |
||
1159 | if ( p == null ) |
||
1160 | return null; |
||
1161 | pNew = new IdList();//sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
||
1162 | if ( pNew == null ) |
||
1163 | return null; |
||
1164 | pNew.nId = pNew.nAlloc = p.nId; |
||
1165 | pNew.a = new IdList_item[p.nId];//sqlite3DbMallocRaw(db, p.nId*sizeof(p.a[0]) ); |
||
1166 | if ( pNew.a == null ) |
||
1167 | { |
||
1168 | sqlite3DbFree( db, ref pNew ); |
||
1169 | return null; |
||
1170 | } |
||
1171 | for ( i = 0; i < p.nId; i++ ) |
||
1172 | { |
||
1173 | pNew.a[i] = new IdList_item(); |
||
1174 | IdList_item pNewItem = pNew.a[i]; |
||
1175 | IdList_item pOldItem = p.a[i]; |
||
1176 | pNewItem.zName = pOldItem.zName;// sqlite3DbStrDup(db, pOldItem.zName); |
||
1177 | pNewItem.idx = pOldItem.idx; |
||
1178 | } |
||
1179 | return pNew; |
||
1180 | } |
||
1181 | |||
1182 | static Select sqlite3SelectDup( sqlite3 db, Select p, int flags ) |
||
1183 | { |
||
1184 | Select pNew; |
||
1185 | if ( p == null ) |
||
1186 | return null; |
||
1187 | pNew = new Select();//sqlite3DbMallocRaw(db, sizeof(*p) ); |
||
1188 | //if ( pNew == null ) return null; |
||
1189 | pNew.pEList = sqlite3ExprListDup( db, p.pEList, flags ); |
||
1190 | pNew.pSrc = sqlite3SrcListDup( db, p.pSrc, flags ); |
||
1191 | pNew.pWhere = sqlite3ExprDup( db, p.pWhere, flags ); |
||
1192 | pNew.pGroupBy = sqlite3ExprListDup( db, p.pGroupBy, flags ); |
||
1193 | pNew.pHaving = sqlite3ExprDup( db, p.pHaving, flags ); |
||
1194 | pNew.pOrderBy = sqlite3ExprListDup( db, p.pOrderBy, flags ); |
||
1195 | pNew.op = p.op; |
||
1196 | pNew.pPrior = sqlite3SelectDup( db, p.pPrior, flags ); |
||
1197 | pNew.pLimit = sqlite3ExprDup( db, p.pLimit, flags ); |
||
1198 | pNew.pOffset = sqlite3ExprDup( db, p.pOffset, flags ); |
||
1199 | pNew.iLimit = 0; |
||
1200 | pNew.iOffset = 0; |
||
1201 | pNew.selFlags = (u16)( p.selFlags & ~SF_UsesEphemeral ); |
||
1202 | pNew.pRightmost = null; |
||
1203 | pNew.addrOpenEphm[0] = -1; |
||
1204 | pNew.addrOpenEphm[1] = -1; |
||
1205 | pNew.addrOpenEphm[2] = -1; |
||
1206 | return pNew; |
||
1207 | } |
||
1208 | #else |
||
1209 | Select sqlite3SelectDup(sqlite3 db, Select p, int flags){ |
||
1210 | Debug.Assert( p==null ); |
||
1211 | return null; |
||
1212 | } |
||
1213 | #endif |
||
1214 | |||
1215 | |||
1216 | /* |
||
1217 | ** Add a new element to the end of an expression list. If pList is |
||
1218 | ** initially NULL, then create a new expression list. |
||
1219 | ** |
||
1220 | ** If a memory allocation error occurs, the entire list is freed and |
||
1221 | ** NULL is returned. If non-NULL is returned, then it is guaranteed |
||
1222 | ** that the new entry was successfully appended. |
||
1223 | */ |
||
1224 | // OVERLOADS, so I don't need to rewrite parse.c |
||
1225 | static ExprList sqlite3ExprListAppend( Parse pParse, int null_2, Expr pExpr ) |
||
1226 | { |
||
1227 | return sqlite3ExprListAppend( pParse, null, pExpr ); |
||
1228 | } |
||
1229 | static ExprList sqlite3ExprListAppend( |
||
1230 | Parse pParse, /* Parsing context */ |
||
1231 | ExprList pList, /* List to which to append. Might be NULL */ |
||
1232 | Expr pExpr /* Expression to be appended. Might be NULL */ |
||
1233 | ) |
||
1234 | { |
||
1235 | ////sqlite3 db = pParse.db; |
||
1236 | if ( pList == null ) |
||
1237 | { |
||
1238 | pList = new ExprList(); //sqlite3DbMallocZero(db, ExprList).Length; |
||
1239 | //if ( pList == null ) |
||
1240 | //{ |
||
1241 | // goto no_mem; |
||
1242 | //} |
||
1243 | Debug.Assert( pList.nAlloc == 0 ); |
||
1244 | } |
||
1245 | if ( pList.nAlloc <= pList.nExpr ) |
||
1246 | { |
||
1247 | ExprList_item a; |
||
1248 | int n = pList.nAlloc * 2 + 4; |
||
1249 | //a = sqlite3DbRealloc(db, pList.a, n*sizeof(pList.a[0])); |
||
1250 | //if( a==0 ){ |
||
1251 | // goto no_mem; |
||
1252 | //} |
||
1253 | Array.Resize( ref pList.a, n );// = a; |
||
1254 | pList.nAlloc = pList.a.Length;// sqlite3DbMallocSize(db, a)/sizeof(a[0]); |
||
1255 | } |
||
1256 | Debug.Assert( pList.a != null ); |
||
1257 | if ( true ) |
||
1258 | { |
||
1259 | pList.a[pList.nExpr] = new ExprList_item(); |
||
1260 | //ExprList_item pItem = pList.a[pList.nExpr++]; |
||
1261 | //pItem = new ExprList_item();//memset(pItem, 0, sizeof(*pItem)); |
||
1262 | //pItem.pExpr = pExpr; |
||
1263 | pList.a[pList.nExpr++].pExpr = pExpr; |
||
1264 | } |
||
1265 | return pList; |
||
1266 | |||
1267 | //no_mem: |
||
1268 | // /* Avoid leaking memory if malloc has failed. */ |
||
1269 | // sqlite3ExprDelete( db, ref pExpr ); |
||
1270 | // sqlite3ExprListDelete( db, ref pList ); |
||
1271 | // return null; |
||
1272 | } |
||
1273 | |||
1274 | /* |
||
1275 | ** Set the ExprList.a[].zName element of the most recently added item |
||
1276 | ** on the expression list. |
||
1277 | ** |
||
1278 | ** pList might be NULL following an OOM error. But pName should never be |
||
1279 | ** NULL. If a memory allocation fails, the pParse.db.mallocFailed flag |
||
1280 | ** is set. |
||
1281 | */ |
||
1282 | static void sqlite3ExprListSetName( |
||
1283 | Parse pParse, /* Parsing context */ |
||
1284 | ExprList pList, /* List to which to add the span. */ |
||
1285 | Token pName, /* Name to be added */ |
||
1286 | int dequote /* True to cause the name to be dequoted */ |
||
1287 | ) |
||
1288 | { |
||
1289 | Debug.Assert( pList != null /* || pParse.db.mallocFailed != 0 */ ); |
||
1290 | if ( pList != null ) |
||
1291 | { |
||
1292 | ExprList_item pItem; |
||
1293 | Debug.Assert( pList.nExpr > 0 ); |
||
1294 | pItem = pList.a[pList.nExpr - 1]; |
||
1295 | Debug.Assert( pItem.zName == null ); |
||
1296 | pItem.zName = pName.z.Substring( 0, pName.n );//sqlite3DbStrNDup(pParse.db, pName.z, pName.n); |
||
1297 | if ( dequote != 0 && !string.IsNullOrEmpty( pItem.zName ) ) |
||
1298 | sqlite3Dequote( ref pItem.zName ); |
||
1299 | } |
||
1300 | } |
||
1301 | |||
1302 | /* |
||
1303 | ** Set the ExprList.a[].zSpan element of the most recently added item |
||
1304 | ** on the expression list. |
||
1305 | ** |
||
1306 | ** pList might be NULL following an OOM error. But pSpan should never be |
||
1307 | ** NULL. If a memory allocation fails, the pParse.db.mallocFailed flag |
||
1308 | ** is set. |
||
1309 | */ |
||
1310 | static void sqlite3ExprListSetSpan( |
||
1311 | Parse pParse, /* Parsing context */ |
||
1312 | ExprList pList, /* List to which to add the span. */ |
||
1313 | ExprSpan pSpan /* The span to be added */ |
||
1314 | ) |
||
1315 | { |
||
1316 | sqlite3 db = pParse.db; |
||
1317 | Debug.Assert( pList != null /*|| db.mallocFailed != 0 */ ); |
||
1318 | if ( pList != null ) |
||
1319 | { |
||
1320 | ExprList_item pItem = pList.a[pList.nExpr - 1]; |
||
1321 | Debug.Assert( pList.nExpr > 0 ); |
||
1322 | Debug.Assert( /* db.mallocFailed != 0 || */ pItem.pExpr == pSpan.pExpr ); |
||
1323 | sqlite3DbFree( db, ref pItem.zSpan ); |
||
1324 | pItem.zSpan = pSpan.zStart.Substring( 0, pSpan.zStart.Length <= pSpan.zEnd.Length ? pSpan.zStart.Length : pSpan.zStart.Length - pSpan.zEnd.Length );// sqlite3DbStrNDup( db, pSpan.zStart, |
||
1325 | //(int)( pSpan.zEnd- pSpan.zStart) ); |
||
1326 | } |
||
1327 | } |
||
1328 | |||
1329 | /* |
||
1330 | ** If the expression list pEList contains more than iLimit elements, |
||
1331 | ** leave an error message in pParse. |
||
1332 | */ |
||
1333 | static void sqlite3ExprListCheckLength( |
||
1334 | Parse pParse, |
||
1335 | ExprList pEList, |
||
1336 | string zObject |
||
1337 | ) |
||
1338 | { |
||
1339 | int mx = pParse.db.aLimit[SQLITE_LIMIT_COLUMN]; |
||
1340 | testcase( pEList != null && pEList.nExpr == mx ); |
||
1341 | testcase( pEList != null && pEList.nExpr == mx + 1 ); |
||
1342 | if ( pEList != null && pEList.nExpr > mx ) |
||
1343 | { |
||
1344 | sqlite3ErrorMsg( pParse, "too many columns in %s", zObject ); |
||
1345 | } |
||
1346 | } |
||
1347 | |||
1348 | |||
1349 | /* |
||
1350 | ** Delete an entire expression list. |
||
1351 | */ |
||
1352 | static void sqlite3ExprListDelete( sqlite3 db, ref ExprList pList ) |
||
1353 | { |
||
1354 | int i; |
||
1355 | ExprList_item pItem; |
||
1356 | if ( pList == null ) |
||
1357 | return; |
||
1358 | Debug.Assert( pList.a != null || ( pList.nExpr == 0 && pList.nAlloc == 0 ) ); |
||
1359 | Debug.Assert( pList.nExpr <= pList.nAlloc ); |
||
1360 | for ( i = 0; i < pList.nExpr; i++ ) |
||
1361 | { |
||
1362 | if ( ( pItem = pList.a[i] ) != null ) |
||
1363 | { |
||
1364 | sqlite3ExprDelete( db, ref pItem.pExpr ); |
||
1365 | sqlite3DbFree( db, ref pItem.zName ); |
||
1366 | sqlite3DbFree( db, ref pItem.zSpan ); |
||
1367 | } |
||
1368 | } |
||
1369 | sqlite3DbFree( db, ref pList.a ); |
||
1370 | sqlite3DbFree( db, ref pList ); |
||
1371 | } |
||
1372 | |||
1373 | /* |
||
1374 | ** These routines are Walker callbacks. Walker.u.pi is a pointer |
||
1375 | ** to an integer. These routines are checking an expression to see |
||
1376 | ** if it is a constant. Set *Walker.u.pi to 0 if the expression is |
||
1377 | ** not constant. |
||
1378 | ** |
||
1379 | ** These callback routines are used to implement the following: |
||
1380 | ** |
||
1381 | ** sqlite3ExprIsConstant() |
||
1382 | ** sqlite3ExprIsConstantNotJoin() |
||
1383 | ** sqlite3ExprIsConstantOrFunction() |
||
1384 | ** |
||
1385 | */ |
||
1386 | static int exprNodeIsConstant( Walker pWalker, ref Expr pExpr ) |
||
1387 | { |
||
1388 | /* If pWalker.u.i is 3 then any term of the expression that comes from |
||
1389 | ** the ON or USING clauses of a join disqualifies the expression |
||
1390 | ** from being considered constant. */ |
||
1391 | if ( pWalker.u.i == 3 && ExprHasAnyProperty( pExpr, EP_FromJoin ) ) |
||
1392 | { |
||
1393 | pWalker.u.i = 0; |
||
1394 | return WRC_Abort; |
||
1395 | } |
||
1396 | |||
1397 | switch ( pExpr.op ) |
||
1398 | { |
||
1399 | /* Consider functions to be constant if all their arguments are constant |
||
1400 | ** and pWalker.u.i==2 */ |
||
1401 | case TK_FUNCTION: |
||
1402 | if ( ( pWalker.u.i ) == 2 ) |
||
1403 | return 0; |
||
1404 | goto case TK_ID; |
||
1405 | /* Fall through */ |
||
1406 | case TK_ID: |
||
1407 | case TK_COLUMN: |
||
1408 | case TK_AGG_FUNCTION: |
||
1409 | case TK_AGG_COLUMN: |
||
1410 | testcase( pExpr.op == TK_ID ); |
||
1411 | testcase( pExpr.op == TK_COLUMN ); |
||
1412 | testcase( pExpr.op == TK_AGG_FUNCTION ); |
||
1413 | testcase( pExpr.op == TK_AGG_COLUMN ); |
||
1414 | pWalker.u.i = 0; |
||
1415 | return WRC_Abort; |
||
1416 | default: |
||
1417 | testcase( pExpr.op == TK_SELECT ); /* selectNodeIsConstant will disallow */ |
||
1418 | testcase( pExpr.op == TK_EXISTS ); /* selectNodeIsConstant will disallow */ |
||
1419 | return WRC_Continue; |
||
1420 | } |
||
1421 | } |
||
1422 | |||
1423 | static int selectNodeIsConstant( Walker pWalker, Select NotUsed ) |
||
1424 | { |
||
1425 | UNUSED_PARAMETER( NotUsed ); |
||
1426 | pWalker.u.i = 0; |
||
1427 | return WRC_Abort; |
||
1428 | } |
||
1429 | static int exprIsConst( Expr p, int initFlag ) |
||
1430 | { |
||
1431 | Walker w = new Walker(); |
||
1432 | w.u.i = initFlag; |
||
1433 | w.xExprCallback = exprNodeIsConstant; |
||
1434 | w.xSelectCallback = selectNodeIsConstant; |
||
1435 | sqlite3WalkExpr( w, ref p ); |
||
1436 | return w.u.i; |
||
1437 | } |
||
1438 | |||
1439 | /* |
||
1440 | ** Walk an expression tree. Return 1 if the expression is constant |
||
1441 | ** and 0 if it involves variables or function calls. |
||
1442 | ** |
||
1443 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
||
1444 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
||
1445 | ** a constant. |
||
1446 | */ |
||
1447 | static int sqlite3ExprIsConstant( Expr p ) |
||
1448 | { |
||
1449 | return exprIsConst( p, 1 ); |
||
1450 | } |
||
1451 | |||
1452 | /* |
||
1453 | ** Walk an expression tree. Return 1 if the expression is constant |
||
1454 | ** that does no originate from the ON or USING clauses of a join. |
||
1455 | ** Return 0 if it involves variables or function calls or terms from |
||
1456 | ** an ON or USING clause. |
||
1457 | */ |
||
1458 | static int sqlite3ExprIsConstantNotJoin( Expr p ) |
||
1459 | { |
||
1460 | return exprIsConst( p, 3 ); |
||
1461 | } |
||
1462 | |||
1463 | /* |
||
1464 | ** Walk an expression tree. Return 1 if the expression is constant |
||
1465 | ** or a function call with constant arguments. Return and 0 if there |
||
1466 | ** are any variables. |
||
1467 | ** |
||
1468 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
||
1469 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
||
1470 | ** a constant. |
||
1471 | */ |
||
1472 | static int sqlite3ExprIsConstantOrFunction( Expr p ) |
||
1473 | { |
||
1474 | return exprIsConst( p, 2 ); |
||
1475 | } |
||
1476 | |||
1477 | /* |
||
1478 | ** If the expression p codes a constant integer that is small enough |
||
1479 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
||
1480 | ** in pValue. If the expression is not an integer or if it is too big |
||
1481 | ** to fit in a signed 32-bit integer, return 0 and leave pValue unchanged. |
||
1482 | */ |
||
1483 | static int sqlite3ExprIsInteger( Expr p, ref int pValue ) |
||
1484 | { |
||
1485 | int rc = 0; |
||
1486 | |||
1487 | /* If an expression is an integer literal that fits in a signed 32-bit |
||
1488 | ** integer, then the EP_IntValue flag will have already been set */ |
||
1489 | Debug.Assert( p.op != TK_INTEGER || ( p.flags & EP_IntValue ) != 0 |
||
1490 | || !sqlite3GetInt32( p.u.zToken, ref rc ) ); |
||
1491 | |||
1492 | if ( ( p.flags & EP_IntValue ) != 0 ) |
||
1493 | { |
||
1494 | pValue = (int)p.u.iValue; |
||
1495 | return 1; |
||
1496 | } |
||
1497 | switch ( p.op ) |
||
1498 | { |
||
1499 | case TK_UPLUS: |
||
1500 | { |
||
1501 | rc = sqlite3ExprIsInteger( p.pLeft, ref pValue ); |
||
1502 | break; |
||
1503 | } |
||
1504 | case TK_UMINUS: |
||
1505 | { |
||
1506 | int v = 0; |
||
1507 | if ( sqlite3ExprIsInteger( p.pLeft, ref v ) != 0 ) |
||
1508 | { |
||
1509 | pValue = -v; |
||
1510 | rc = 1; |
||
1511 | } |
||
1512 | break; |
||
1513 | } |
||
1514 | default: |
||
1515 | break; |
||
1516 | } |
||
1517 | return rc; |
||
1518 | } |
||
1519 | |||
1520 | /* |
||
1521 | ** Return FALSE if there is no chance that the expression can be NULL. |
||
1522 | ** |
||
1523 | ** If the expression might be NULL or if the expression is too complex |
||
1524 | ** to tell return TRUE. |
||
1525 | ** |
||
1526 | ** This routine is used as an optimization, to skip OP_IsNull opcodes |
||
1527 | ** when we know that a value cannot be NULL. Hence, a false positive |
||
1528 | ** (returning TRUE when in fact the expression can never be NULL) might |
||
1529 | ** be a small performance hit but is otherwise harmless. On the other |
||
1530 | ** hand, a false negative (returning FALSE when the result could be NULL) |
||
1531 | ** will likely result in an incorrect answer. So when in doubt, return |
||
1532 | ** TRUE. |
||
1533 | */ |
||
1534 | static int sqlite3ExprCanBeNull( Expr p ) |
||
1535 | { |
||
1536 | u8 op; |
||
1537 | while ( p.op == TK_UPLUS || p.op == TK_UMINUS ) |
||
1538 | { |
||
1539 | p = p.pLeft; |
||
1540 | } |
||
1541 | op = p.op; |
||
1542 | if ( op == TK_REGISTER ) |
||
1543 | op = p.op2; |
||
1544 | switch ( op ) |
||
1545 | { |
||
1546 | case TK_INTEGER: |
||
1547 | case TK_STRING: |
||
1548 | case TK_FLOAT: |
||
1549 | case TK_BLOB: |
||
1550 | return 0; |
||
1551 | default: |
||
1552 | return 1; |
||
1553 | } |
||
1554 | } |
||
1555 | |||
1556 | /* |
||
1557 | ** Generate an OP_IsNull instruction that tests register iReg and jumps |
||
1558 | ** to location iDest if the value in iReg is NULL. The value in iReg |
||
1559 | ** was computed by pExpr. If we can look at pExpr at compile-time and |
||
1560 | ** determine that it can never generate a NULL, then the OP_IsNull operation |
||
1561 | ** can be omitted. |
||
1562 | */ |
||
1563 | static void sqlite3ExprCodeIsNullJump( |
||
1564 | Vdbe v, /* The VDBE under construction */ |
||
1565 | Expr pExpr, /* Only generate OP_IsNull if this expr can be NULL */ |
||
1566 | int iReg, /* Test the value in this register for NULL */ |
||
1567 | int iDest /* Jump here if the value is null */ |
||
1568 | ) |
||
1569 | { |
||
1570 | if ( sqlite3ExprCanBeNull( pExpr ) != 0 ) |
||
1571 | { |
||
1572 | sqlite3VdbeAddOp2( v, OP_IsNull, iReg, iDest ); |
||
1573 | } |
||
1574 | } |
||
1575 | |||
1576 | /* |
||
1577 | ** Return TRUE if the given expression is a constant which would be |
||
1578 | ** unchanged by OP_Affinity with the affinity given in the second |
||
1579 | ** argument. |
||
1580 | ** |
||
1581 | ** This routine is used to determine if the OP_Affinity operation |
||
1582 | ** can be omitted. When in doubt return FALSE. A false negative |
||
1583 | ** is harmless. A false positive, however, can result in the wrong |
||
1584 | ** answer. |
||
1585 | */ |
||
1586 | static int sqlite3ExprNeedsNoAffinityChange( Expr p, char aff ) |
||
1587 | { |
||
1588 | u8 op; |
||
1589 | if ( aff == SQLITE_AFF_NONE ) |
||
1590 | return 1; |
||
1591 | while ( p.op == TK_UPLUS || p.op == TK_UMINUS ) |
||
1592 | { |
||
1593 | p = p.pLeft; |
||
1594 | } |
||
1595 | op = p.op; |
||
1596 | if ( op == TK_REGISTER ) |
||
1597 | op = p.op2; |
||
1598 | switch ( op ) |
||
1599 | { |
||
1600 | case TK_INTEGER: |
||
1601 | { |
||
1602 | return ( aff == SQLITE_AFF_INTEGER || aff == SQLITE_AFF_NUMERIC ) ? 1 : 0; |
||
1603 | } |
||
1604 | case TK_FLOAT: |
||
1605 | { |
||
1606 | return ( aff == SQLITE_AFF_REAL || aff == SQLITE_AFF_NUMERIC ) ? 1 : 0; |
||
1607 | } |
||
1608 | case TK_STRING: |
||
1609 | { |
||
1610 | return ( aff == SQLITE_AFF_TEXT ) ? 1 : 0; |
||
1611 | } |
||
1612 | case TK_BLOB: |
||
1613 | { |
||
1614 | return 1; |
||
1615 | } |
||
1616 | case TK_COLUMN: |
||
1617 | { |
||
1618 | Debug.Assert( p.iTable >= 0 ); /* p cannot be part of a CHECK constraint */ |
||
1619 | return ( p.iColumn < 0 |
||
1620 | && ( aff == SQLITE_AFF_INTEGER || aff == SQLITE_AFF_NUMERIC ) ) ? 1 : 0; |
||
1621 | } |
||
1622 | default: |
||
1623 | { |
||
1624 | return 0; |
||
1625 | } |
||
1626 | } |
||
1627 | } |
||
1628 | |||
1629 | /* |
||
1630 | ** Return TRUE if the given string is a row-id column name. |
||
1631 | */ |
||
1632 | static bool sqlite3IsRowid( string z ) |
||
1633 | { |
||
1634 | if ( z.Equals( "_ROWID_", StringComparison.OrdinalIgnoreCase ) ) |
||
1635 | return true; |
||
1636 | if ( z.Equals( "ROWID", StringComparison.OrdinalIgnoreCase ) ) |
||
1637 | return true; |
||
1638 | if ( z.Equals( "OID", StringComparison.OrdinalIgnoreCase ) ) |
||
1639 | return true; |
||
1640 | return false; |
||
1641 | } |
||
1642 | |||
1643 | |||
1644 | /* |
||
1645 | ** Return true if we are able to the IN operator optimization on a |
||
1646 | ** query of the form |
||
1647 | ** |
||
1648 | ** x IN (SELECT ...) |
||
1649 | ** |
||
1650 | ** Where the SELECT... clause is as specified by the parameter to this |
||
1651 | ** routine. |
||
1652 | ** |
||
1653 | ** The Select object passed in has already been preprocessed and no |
||
1654 | ** errors have been found. |
||
1655 | */ |
||
1656 | #if !SQLITE_OMIT_SUBQUERY |
||
1657 | static int isCandidateForInOpt( Select p ) |
||
1658 | { |
||
1659 | SrcList pSrc; |
||
1660 | ExprList pEList; |
||
1661 | Table pTab; |
||
1662 | if ( p == null ) |
||
1663 | return 0; /* right-hand side of IN is SELECT */ |
||
1664 | if ( p.pPrior != null ) |
||
1665 | return 0; /* Not a compound SELECT */ |
||
1666 | if ( ( p.selFlags & ( SF_Distinct | SF_Aggregate ) ) != 0 ) |
||
1667 | { |
||
1668 | testcase( ( p.selFlags & ( SF_Distinct | SF_Aggregate ) ) == SF_Distinct ); |
||
1669 | testcase( ( p.selFlags & ( SF_Distinct | SF_Aggregate ) ) == SF_Aggregate ); |
||
1670 | return 0; /* No DISTINCT keyword and no aggregate functions */ |
||
1671 | } |
||
1672 | Debug.Assert( p.pGroupBy == null ); /* Has no GROUP BY clause */ |
||
1673 | if ( p.pLimit != null ) |
||
1674 | return 0; /* Has no LIMIT clause */ |
||
1675 | Debug.Assert( p.pOffset == null ); /* No LIMIT means no OFFSET */ |
||
1676 | |||
1677 | if ( p.pWhere != null ) |
||
1678 | return 0; /* Has no WHERE clause */ |
||
1679 | pSrc = p.pSrc; |
||
1680 | Debug.Assert( pSrc != null ); |
||
1681 | if ( pSrc.nSrc != 1 ) |
||
1682 | return 0; /* Single term in FROM clause */ |
||
1683 | if ( pSrc.a[0].pSelect != null ) |
||
1684 | return 0; /* FROM is not a subquery or view */ |
||
1685 | pTab = pSrc.a[0].pTab; |
||
1686 | if ( NEVER( pTab == null ) ) |
||
1687 | return 0; |
||
1688 | Debug.Assert( pTab.pSelect == null ); /* FROM clause is not a view */ |
||
1689 | if ( IsVirtual( pTab ) ) |
||
1690 | return 0; /* FROM clause not a virtual table */ |
||
1691 | pEList = p.pEList; |
||
1692 | if ( pEList.nExpr != 1 ) |
||
1693 | return 0; /* One column in the result set */ |
||
1694 | if ( pEList.a[0].pExpr.op != TK_COLUMN ) |
||
1695 | return 0; /* Result is a column */ |
||
1696 | return 1; |
||
1697 | } |
||
1698 | #endif //* SQLITE_OMIT_SUBQUERY */ |
||
1699 | |||
1700 | /* |
||
1701 | ** This function is used by the implementation of the IN (...) operator. |
||
1702 | ** It's job is to find or create a b-tree structure that may be used |
||
1703 | ** either to test for membership of the (...) set or to iterate through |
||
1704 | ** its members, skipping duplicates. |
||
1705 | ** |
||
1706 | ** The index of the cursor opened on the b-tree (database table, database index |
||
1707 | ** or ephermal table) is stored in pX->iTable before this function returns. |
||
1708 | ** The returned value of this function indicates the b-tree type, as follows: |
||
1709 | ** |
||
1710 | ** IN_INDEX_ROWID - The cursor was opened on a database table. |
||
1711 | ** IN_INDEX_INDEX - The cursor was opened on a database index. |
||
1712 | ** IN_INDEX_EPH - The cursor was opened on a specially created and |
||
1713 | ** populated epheremal table. |
||
1714 | ** |
||
1715 | ** An existing b-tree may only be used if the SELECT is of the simple |
||
1716 | ** form: |
||
1717 | ** |
||
1718 | ** SELECT <column> FROM <table> |
||
1719 | ** |
||
1720 | ** If the prNotFound parameter is 0, then the b-tree will be used to iterate |
||
1721 | ** through the set members, skipping any duplicates. In this case an |
||
1722 | ** epheremal table must be used unless the selected <column> is guaranteed |
||
1723 | ** to be unique - either because it is an INTEGER PRIMARY KEY or it |
||
1724 | ** has a UNIQUE constraint or UNIQUE index. |
||
1725 | ** |
||
1726 | ** If the prNotFound parameter is not 0, then the b-tree will be used |
||
1727 | ** for fast set membership tests. In this case an epheremal table must |
||
1728 | ** be used unless <column> is an INTEGER PRIMARY KEY or an index can |
||
1729 | ** be found with <column> as its left-most column. |
||
1730 | ** |
||
1731 | ** When the b-tree is being used for membership tests, the calling function |
||
1732 | ** needs to know whether or not the structure contains an SQL NULL |
||
1733 | ** value in order to correctly evaluate expressions like "X IN (Y, Z)". |
||
1734 | ** If there is any chance that the (...) might contain a NULL value at |
||
1735 | ** runtime, then a register is allocated and the register number written |
||
1736 | ** to *prNotFound. If there is no chance that the (...) contains a |
||
1737 | ** NULL value, then *prNotFound is left unchanged. |
||
1738 | ** |
||
1739 | ** If a register is allocated and its location stored in *prNotFound, then |
||
1740 | ** its initial value is NULL. If the (...) does not remain constant |
||
1741 | ** for the duration of the query (i.e. the SELECT within the (...) |
||
1742 | ** is a correlated subquery) then the value of the allocated register is |
||
1743 | ** reset to NULL each time the subquery is rerun. This allows the |
||
1744 | ** caller to use vdbe code equivalent to the following: |
||
1745 | ** |
||
1746 | ** if( register==NULL ){ |
||
1747 | ** has_null = <test if data structure contains null> |
||
1748 | ** register = 1 |
||
1749 | ** } |
||
1750 | ** |
||
1751 | ** in order to avoid running the <test if data structure contains null> |
||
1752 | ** test more often than is necessary. |
||
1753 | */ |
||
1754 | #if !SQLITE_OMIT_SUBQUERY |
||
1755 | static int sqlite3FindInIndex( Parse pParse, Expr pX, ref int prNotFound ) |
||
1756 | { |
||
1757 | Select p; /* SELECT to the right of IN operator */ |
||
1758 | int eType = 0; /* Type of RHS table. IN_INDEX_* */ |
||
1759 | int iTab = pParse.nTab++; /* Cursor of the RHS table */ |
||
1760 | bool mustBeUnique = ( prNotFound != 0 ); /* True if RHS must be unique */ |
||
1761 | |||
1762 | Debug.Assert( pX.op == TK_IN ); |
||
1763 | |||
1764 | /* Check to see if an existing table or index can be used to |
||
1765 | ** satisfy the query. This is preferable to generating a new |
||
1766 | ** ephemeral table. |
||
1767 | */ |
||
1768 | p = ( ExprHasProperty( pX, EP_xIsSelect ) ? pX.x.pSelect : null ); |
||
1769 | if ( ALWAYS( pParse.nErr == 0 ) && isCandidateForInOpt( p ) != 0 ) |
||
1770 | { |
||
1771 | sqlite3 db = pParse.db; /* Database connection */ |
||
1772 | Expr pExpr = p.pEList.a[0].pExpr; /* Expression <column> */ |
||
1773 | int iCol = pExpr.iColumn; /* Index of column <column> */ |
||
1774 | Vdbe v = sqlite3GetVdbe( pParse ); /* Virtual machine being coded */ |
||
1775 | Table pTab = p.pSrc.a[0].pTab; /* Table <table>. */ |
||
1776 | int iDb; /* Database idx for pTab */ |
||
1777 | |||
1778 | /* Code an OP_VerifyCookie and OP_TableLock for <table>. */ |
||
1779 | iDb = sqlite3SchemaToIndex( db, pTab.pSchema ); |
||
1780 | sqlite3CodeVerifySchema( pParse, iDb ); |
||
1781 | sqlite3TableLock( pParse, iDb, pTab.tnum, 0, pTab.zName ); |
||
1782 | |||
1783 | /* This function is only called from two places. In both cases the vdbe |
||
1784 | ** has already been allocated. So assume sqlite3GetVdbe() is always |
||
1785 | ** successful here. |
||
1786 | */ |
||
1787 | Debug.Assert( v != null ); |
||
1788 | if ( iCol < 0 ) |
||
1789 | { |
||
1790 | int iMem = ++pParse.nMem; |
||
1791 | int iAddr; |
||
1792 | |||
1793 | iAddr = sqlite3VdbeAddOp1( v, OP_If, iMem ); |
||
1794 | sqlite3VdbeAddOp2( v, OP_Integer, 1, iMem ); |
||
1795 | |||
1796 | sqlite3OpenTable( pParse, iTab, iDb, pTab, OP_OpenRead ); |
||
1797 | eType = IN_INDEX_ROWID; |
||
1798 | |||
1799 | sqlite3VdbeJumpHere( v, iAddr ); |
||
1800 | } |
||
1801 | else |
||
1802 | { |
||
1803 | Index pIdx; /* Iterator variable */ |
||
1804 | /* The collation sequence used by the comparison. If an index is to |
||
1805 | ** be used in place of a temp.table, it must be ordered according |
||
1806 | ** to this collation sequence. */ |
||
1807 | CollSeq pReq = sqlite3BinaryCompareCollSeq( pParse, pX.pLeft, pExpr ); |
||
1808 | |||
1809 | /* Check that the affinity that will be used to perform the |
||
1810 | ** comparison is the same as the affinity of the column. If |
||
1811 | ** it is not, it is not possible to use any index. |
||
1812 | */ |
||
1813 | char aff = comparisonAffinity( pX ); |
||
1814 | bool affinity_ok = ( pTab.aCol[iCol].affinity == aff || aff == SQLITE_AFF_NONE ); |
||
1815 | |||
1816 | for ( pIdx = pTab.pIndex; pIdx != null && eType == 0 && affinity_ok; pIdx = pIdx.pNext ) |
||
1817 | { |
||
1818 | if ( ( pIdx.aiColumn[0] == iCol ) |
||
1819 | && ( sqlite3FindCollSeq( db, ENC( db ), pIdx.azColl[0], 0 ) == pReq ) |
||
1820 | && ( mustBeUnique == false || ( pIdx.nColumn == 1 && pIdx.onError != OE_None ) ) |
||
1821 | ) |
||
1822 | { |
||
1823 | int iMem = ++pParse.nMem; |
||
1824 | int iAddr; |
||
1825 | KeyInfo pKey; |
||
1826 | |||
1827 | pKey = sqlite3IndexKeyinfo( pParse, pIdx ); |
||
1828 | |||
1829 | iAddr = sqlite3VdbeAddOp1( v, OP_If, iMem ); |
||
1830 | sqlite3VdbeAddOp2( v, OP_Integer, 1, iMem ); |
||
1831 | |||
1832 | sqlite3VdbeAddOp4( v, OP_OpenRead, iTab, pIdx.tnum, iDb, |
||
1833 | pKey, P4_KEYINFO_HANDOFF ); |
||
1834 | #if SQLITE_DEBUG |
||
1835 | VdbeComment( v, "%s", pIdx.zName ); |
||
1836 | #endif |
||
1837 | eType = IN_INDEX_INDEX; |
||
1838 | |||
1839 | sqlite3VdbeJumpHere( v, iAddr ); |
||
1840 | if ( //prNotFound != null && -- always exists under C# |
||
1841 | pTab.aCol[iCol].notNull == 0 ) |
||
1842 | { |
||
1843 | prNotFound = ++pParse.nMem; |
||
1844 | } |
||
1845 | } |
||
1846 | } |
||
1847 | } |
||
1848 | } |
||
1849 | |||
1850 | if ( eType == 0 ) |
||
1851 | { |
||
1852 | /* Could not found an existing table or index to use as the RHS b-tree. |
||
1853 | ** We will have to generate an ephemeral table to do the job. |
||
1854 | */ |
||
1855 | double savedNQueryLoop = pParse.nQueryLoop; |
||
1856 | int rMayHaveNull = 0; |
||
1857 | eType = IN_INDEX_EPH; |
||
1858 | if ( prNotFound != -1 ) // Klude to show prNotFound not available |
||
1859 | { |
||
1860 | prNotFound = rMayHaveNull = ++pParse.nMem; |
||
1861 | } |
||
1862 | else |
||
1863 | { |
||
1864 | testcase( pParse.nQueryLoop > (double)1 ); |
||
1865 | pParse.nQueryLoop = (double)1; |
||
1866 | if ( pX.pLeft.iColumn < 0 && !ExprHasAnyProperty( pX, EP_xIsSelect ) ) |
||
1867 | { |
||
1868 | eType = IN_INDEX_ROWID; |
||
1869 | } |
||
1870 | } |
||
1871 | sqlite3CodeSubselect( pParse, pX, rMayHaveNull, eType == IN_INDEX_ROWID ); |
||
1872 | pParse.nQueryLoop = savedNQueryLoop; |
||
1873 | } |
||
1874 | else |
||
1875 | { |
||
1876 | pX.iTable = iTab; |
||
1877 | } |
||
1878 | return eType; |
||
1879 | } |
||
1880 | #endif |
||
1881 | |||
1882 | /* |
||
1883 | ** Generate code for scalar subqueries used as a subquery expression, EXISTS, |
||
1884 | ** or IN operators. Examples: |
||
1885 | ** |
||
1886 | ** (SELECT a FROM b) -- subquery |
||
1887 | ** EXISTS (SELECT a FROM b) -- EXISTS subquery |
||
1888 | ** x IN (4,5,11) -- IN operator with list on right-hand side |
||
1889 | ** x IN (SELECT a FROM b) -- IN operator with subquery on the right |
||
1890 | ** |
||
1891 | ** The pExpr parameter describes the expression that contains the IN |
||
1892 | ** operator or subquery. |
||
1893 | ** |
||
1894 | ** If parameter isRowid is non-zero, then expression pExpr is guaranteed |
||
1895 | ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference |
||
1896 | ** to some integer key column of a table B-Tree. In this case, use an |
||
1897 | ** intkey B-Tree to store the set of IN(...) values instead of the usual |
||
1898 | ** (slower) variable length keys B-Tree. |
||
1899 | ** |
||
1900 | ** If rMayHaveNull is non-zero, that means that the operation is an IN |
||
1901 | ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. |
||
1902 | ** Furthermore, the IN is in a WHERE clause and that we really want |
||
1903 | ** to iterate over the RHS of the IN operator in order to quickly locate |
||
1904 | ** all corresponding LHS elements. All this routine does is initialize |
||
1905 | ** the register given by rMayHaveNull to NULL. Calling routines will take |
||
1906 | ** care of changing this register value to non-NULL if the RHS is NULL-free. |
||
1907 | ** |
||
1908 | ** If rMayHaveNull is zero, that means that the subquery is being used |
||
1909 | ** for membership testing only. There is no need to initialize any |
||
1910 | ** registers to indicate the presense or absence of NULLs on the RHS. |
||
1911 | ** |
||
1912 | ** For a SELECT or EXISTS operator, return the register that holds the |
||
1913 | ** result. For IN operators or if an error occurs, the return value is 0. |
||
1914 | */ |
||
1915 | #if !SQLITE_OMIT_SUBQUERY |
||
1916 | static int sqlite3CodeSubselect( |
||
1917 | Parse pParse, /* Parsing context */ |
||
1918 | Expr pExpr, /* The IN, SELECT, or EXISTS operator */ |
||
1919 | int rMayHaveNull, /* Register that records whether NULLs exist in RHS */ |
||
1920 | bool isRowid /* If true, LHS of IN operator is a rowid */ |
||
1921 | ) |
||
1922 | { |
||
1923 | int testAddr = 0; /* One-time test address */ |
||
1924 | int rReg = 0; /* Register storing resulting */ |
||
1925 | Vdbe v = sqlite3GetVdbe( pParse ); |
||
1926 | if ( NEVER( v == null ) ) |
||
1927 | return 0; |
||
1928 | sqlite3ExprCachePush( pParse ); |
||
1929 | |||
1930 | /* This code must be run in its entirety every time it is encountered |
||
1931 | ** if any of the following is true: |
||
1932 | ** |
||
1933 | ** * The right-hand side is a correlated subquery |
||
1934 | ** * The right-hand side is an expression list containing variables |
||
1935 | ** * We are inside a trigger |
||
1936 | ** |
||
1937 | ** If all of the above are false, then we can run this code just once |
||
1938 | ** save the results, and reuse the same result on subsequent invocations. |
||
1939 | */ |
||
1940 | if ( !ExprHasAnyProperty( pExpr, EP_VarSelect ) && null == pParse.pTriggerTab ) |
||
1941 | { |
||
1942 | int mem = ++pParse.nMem; |
||
1943 | sqlite3VdbeAddOp1( v, OP_If, mem ); |
||
1944 | testAddr = sqlite3VdbeAddOp2( v, OP_Integer, 1, mem ); |
||
1945 | Debug.Assert( testAddr > 0 /* || pParse.db.mallocFailed != 0 */ ); |
||
1946 | } |
||
1947 | |||
1948 | #if !SQLITE_OMIT_EXPLAIN |
||
1949 | if ( pParse.explain == 2 ) |
||
1950 | { |
||
1951 | string zMsg = sqlite3MPrintf( |
||
1952 | pParse.db, "EXECUTE %s%s SUBQUERY %d", testAddr != 0 ? string.Empty : "CORRELATED ", |
||
1953 | pExpr.op == TK_IN ? "LIST" : "SCALAR", pParse.iNextSelectId |
||
1954 | ); |
||
1955 | sqlite3VdbeAddOp4( v, OP_Explain, pParse.iSelectId, 0, 0, zMsg, P4_DYNAMIC ); |
||
1956 | } |
||
1957 | #endif |
||
1958 | |||
1959 | switch ( pExpr.op ) |
||
1960 | { |
||
1961 | case TK_IN: |
||
1962 | { |
||
1963 | char affinity; /* Affinity of the LHS of the IN */ |
||
1964 | KeyInfo keyInfo; /* Keyinfo for the generated table */ |
||
1965 | int addr; /* Address of OP_OpenEphemeral instruction */ |
||
1966 | Expr pLeft = pExpr.pLeft; /* the LHS of the IN operator */ |
||
1967 | |||
1968 | if ( rMayHaveNull != 0 ) |
||
1969 | { |
||
1970 | sqlite3VdbeAddOp2( v, OP_Null, 0, rMayHaveNull ); |
||
1971 | } |
||
1972 | |||
1973 | affinity = sqlite3ExprAffinity( pLeft ); |
||
1974 | |||
1975 | /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' |
||
1976 | ** expression it is handled the same way. An ephemeral table is |
||
1977 | ** filled with single-field index keys representing the results |
||
1978 | ** from the SELECT or the <exprlist>. |
||
1979 | ** |
||
1980 | ** If the 'x' expression is a column value, or the SELECT... |
||
1981 | ** statement returns a column value, then the affinity of that |
||
1982 | ** column is used to build the index keys. If both 'x' and the |
||
1983 | ** SELECT... statement are columns, then numeric affinity is used |
||
1984 | ** if either column has NUMERIC or INTEGER affinity. If neither |
||
1985 | ** 'x' nor the SELECT... statement are columns, then numeric affinity |
||
1986 | ** is used. |
||
1987 | */ |
||
1988 | pExpr.iTable = pParse.nTab++; |
||
1989 | addr = sqlite3VdbeAddOp2( v, OP_OpenEphemeral, (int)pExpr.iTable, !isRowid ); |
||
1990 | if ( rMayHaveNull == 0 ) |
||
1991 | sqlite3VdbeChangeP5( v, BTREE_UNORDERED ); |
||
1992 | keyInfo = new KeyInfo();// memset( &keyInfo, 0, sizeof(keyInfo )); |
||
1993 | keyInfo.nField = 1; |
||
1994 | |||
1995 | if ( ExprHasProperty( pExpr, EP_xIsSelect ) ) |
||
1996 | { |
||
1997 | /* Case 1: expr IN (SELECT ...) |
||
1998 | ** |
||
1999 | ** Generate code to write the results of the select into the temporary |
||
2000 | ** table allocated and opened above. |
||
2001 | */ |
||
2002 | SelectDest dest = new SelectDest(); |
||
2003 | ExprList pEList; |
||
2004 | |||
2005 | Debug.Assert( !isRowid ); |
||
2006 | sqlite3SelectDestInit( dest, SRT_Set, pExpr.iTable ); |
||
2007 | dest.affinity = (char)affinity; |
||
2008 | Debug.Assert( ( pExpr.iTable & 0x0000FFFF ) == pExpr.iTable ); |
||
2009 | pExpr.x.pSelect.iLimit = 0; |
||
2010 | if ( sqlite3Select( pParse, pExpr.x.pSelect, ref dest ) != 0 ) |
||
2011 | { |
||
2012 | return 0; |
||
2013 | } |
||
2014 | pEList = pExpr.x.pSelect.pEList; |
||
2015 | if ( ALWAYS( pEList != null ) && pEList.nExpr > 0 ) |
||
2016 | { |
||
2017 | keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq( pParse, pExpr.pLeft, |
||
2018 | pEList.a[0].pExpr ); |
||
2019 | } |
||
2020 | } |
||
2021 | else if ( ALWAYS( pExpr.x.pList != null ) ) |
||
2022 | { |
||
2023 | /* Case 2: expr IN (exprlist) |
||
2024 | ** |
||
2025 | ** For each expression, build an index key from the evaluation and |
||
2026 | ** store it in the temporary table. If <expr> is a column, then use |
||
2027 | ** that columns affinity when building index keys. If <expr> is not |
||
2028 | ** a column, use numeric affinity. |
||
2029 | */ |
||
2030 | int i; |
||
2031 | ExprList pList = pExpr.x.pList; |
||
2032 | ExprList_item pItem; |
||
2033 | int r1, r2, r3; |
||
2034 | |||
2035 | if ( affinity == '\0' ) |
||
2036 | { |
||
2037 | affinity = SQLITE_AFF_NONE; |
||
2038 | } |
||
2039 | keyInfo.aColl[0] = sqlite3ExprCollSeq( pParse, pExpr.pLeft ); |
||
2040 | |||
2041 | /* Loop through each expression in <exprlist>. */ |
||
2042 | r1 = sqlite3GetTempReg( pParse ); |
||
2043 | r2 = sqlite3GetTempReg( pParse ); |
||
2044 | sqlite3VdbeAddOp2( v, OP_Null, 0, r2 ); |
||
2045 | for ( i = 0; i < pList.nExpr; i++ ) |
||
2046 | {//, pItem++){ |
||
2047 | pItem = pList.a[i]; |
||
2048 | Expr pE2 = pItem.pExpr; |
||
2049 | int iValToIns = 0; |
||
2050 | |||
2051 | /* If the expression is not constant then we will need to |
||
2052 | ** disable the test that was generated above that makes sure |
||
2053 | ** this code only executes once. Because for a non-constant |
||
2054 | ** expression we need to rerun this code each time. |
||
2055 | */ |
||
2056 | if ( testAddr != 0 && sqlite3ExprIsConstant( pE2 ) == 0 ) |
||
2057 | { |
||
2058 | sqlite3VdbeChangeToNoop( v, testAddr - 1, 2 ); |
||
2059 | testAddr = 0; |
||
2060 | } |
||
2061 | |||
2062 | /* Evaluate the expression and insert it into the temp table */ |
||
2063 | if ( isRowid && sqlite3ExprIsInteger( pE2, ref iValToIns ) != 0 ) |
||
2064 | { |
||
2065 | sqlite3VdbeAddOp3( v, OP_InsertInt, pExpr.iTable, r2, iValToIns ); |
||
2066 | } |
||
2067 | else |
||
2068 | { |
||
2069 | r3 = sqlite3ExprCodeTarget( pParse, pE2, r1 ); |
||
2070 | if ( isRowid ) |
||
2071 | { |
||
2072 | sqlite3VdbeAddOp2( v, OP_MustBeInt, r3, |
||
2073 | sqlite3VdbeCurrentAddr( v ) + 2 ); |
||
2074 | sqlite3VdbeAddOp3( v, OP_Insert, pExpr.iTable, r2, r3 ); |
||
2075 | } |
||
2076 | else |
||
2077 | { |
||
2078 | sqlite3VdbeAddOp4( v, OP_MakeRecord, r3, 1, r2, affinity, 1 ); |
||
2079 | sqlite3ExprCacheAffinityChange( pParse, r3, 1 ); |
||
2080 | sqlite3VdbeAddOp2( v, OP_IdxInsert, pExpr.iTable, r2 ); |
||
2081 | } |
||
2082 | } |
||
2083 | } |
||
2084 | sqlite3ReleaseTempReg( pParse, r1 ); |
||
2085 | sqlite3ReleaseTempReg( pParse, r2 ); |
||
2086 | } |
||
2087 | if ( !isRowid ) |
||
2088 | { |
||
2089 | sqlite3VdbeChangeP4( v, addr, keyInfo, P4_KEYINFO ); |
||
2090 | } |
||
2091 | break; |
||
2092 | } |
||
2093 | |||
2094 | case TK_EXISTS: |
||
2095 | case TK_SELECT: |
||
2096 | default: |
||
2097 | { |
||
2098 | /* If this has to be a scalar SELECT. Generate code to put the |
||
2099 | ** value of this select in a memory cell and record the number |
||
2100 | ** of the memory cell in iColumn. If this is an EXISTS, write |
||
2101 | ** an integer 0 (not exists) or 1 (exists) into a memory cell |
||
2102 | ** and record that memory cell in iColumn. |
||
2103 | */ |
||
2104 | Select pSel; /* SELECT statement to encode */ |
||
2105 | SelectDest dest = new SelectDest(); /* How to deal with SELECt result */ |
||
2106 | |||
2107 | testcase( pExpr.op == TK_EXISTS ); |
||
2108 | testcase( pExpr.op == TK_SELECT ); |
||
2109 | Debug.Assert( pExpr.op == TK_EXISTS || pExpr.op == TK_SELECT ); |
||
2110 | |||
2111 | Debug.Assert( ExprHasProperty( pExpr, EP_xIsSelect ) ); |
||
2112 | pSel = pExpr.x.pSelect; |
||
2113 | sqlite3SelectDestInit( dest, 0, ++pParse.nMem ); |
||
2114 | if ( pExpr.op == TK_SELECT ) |
||
2115 | { |
||
2116 | dest.eDest = SRT_Mem; |
||
2117 | sqlite3VdbeAddOp2( v, OP_Null, 0, dest.iParm ); |
||
2118 | #if SQLITE_DEBUG |
||
2119 | VdbeComment( v, "Init subquery result" ); |
||
2120 | #endif |
||
2121 | } |
||
2122 | else |
||
2123 | { |
||
2124 | dest.eDest = SRT_Exists; |
||
2125 | sqlite3VdbeAddOp2( v, OP_Integer, 0, dest.iParm ); |
||
2126 | #if SQLITE_DEBUG |
||
2127 | VdbeComment( v, "Init EXISTS result" ); |
||
2128 | #endif |
||
2129 | } |
||
2130 | sqlite3ExprDelete( pParse.db, ref pSel.pLimit ); |
||
2131 | pSel.pLimit = sqlite3PExpr( pParse, TK_INTEGER, null, null, sqlite3IntTokens[1] ); |
||
2132 | pSel.iLimit = 0; |
||
2133 | if ( sqlite3Select( pParse, pSel, ref dest ) != 0 ) |
||
2134 | { |
||
2135 | return 0; |
||
2136 | } |
||
2137 | rReg = dest.iParm; |
||
2138 | ExprSetIrreducible( pExpr ); |
||
2139 | break; |
||
2140 | } |
||
2141 | } |
||
2142 | |||
2143 | if ( testAddr != 0 ) |
||
2144 | { |
||
2145 | sqlite3VdbeJumpHere( v, testAddr - 1 ); |
||
2146 | } |
||
2147 | sqlite3ExprCachePop( pParse, 1 ); |
||
2148 | |||
2149 | return rReg; |
||
2150 | } |
||
2151 | #endif // * SQLITE_OMIT_SUBQUERY */ |
||
2152 | |||
2153 | #if !SQLITE_OMIT_SUBQUERY |
||
2154 | /* |
||
2155 | ** Generate code for an IN expression. |
||
2156 | ** |
||
2157 | ** x IN (SELECT ...) |
||
2158 | ** x IN (value, value, ...) |
||
2159 | ** |
||
2160 | ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) |
||
2161 | ** is an array of zero or more values. The expression is true if the LHS is |
||
2162 | ** contained within the RHS. The value of the expression is unknown (NULL) |
||
2163 | ** if the LHS is NULL or if the LHS is not contained within the RHS and the |
||
2164 | ** RHS contains one or more NULL values. |
||
2165 | ** |
||
2166 | ** This routine generates code will jump to destIfFalse if the LHS is not |
||
2167 | ** contained within the RHS. If due to NULLs we cannot determine if the LHS |
||
2168 | ** is contained in the RHS then jump to destIfNull. If the LHS is contained |
||
2169 | ** within the RHS then fall through. |
||
2170 | */ |
||
2171 | static void sqlite3ExprCodeIN( |
||
2172 | Parse pParse, /* Parsing and code generating context */ |
||
2173 | Expr pExpr, /* The IN expression */ |
||
2174 | int destIfFalse, /* Jump here if LHS is not contained in the RHS */ |
||
2175 | int destIfNull /* Jump here if the results are unknown due to NULLs */ |
||
2176 | ) |
||
2177 | { |
||
2178 | int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ |
||
2179 | char affinity; /* Comparison affinity to use */ |
||
2180 | int eType; /* Type of the RHS */ |
||
2181 | int r1; /* Temporary use register */ |
||
2182 | Vdbe v; /* Statement under construction */ |
||
2183 | |||
2184 | /* Compute the RHS. After this step, the table with cursor |
||
2185 | ** pExpr.iTable will contains the values that make up the RHS. |
||
2186 | */ |
||
2187 | v = pParse.pVdbe; |
||
2188 | Debug.Assert( v != null ); /* OOM detected prior to this routine */ |
||
2189 | VdbeNoopComment( v, "begin IN expr" ); |
||
2190 | eType = sqlite3FindInIndex( pParse, pExpr, ref rRhsHasNull ); |
||
2191 | |||
2192 | /* Figure out the affinity to use to create a key from the results |
||
2193 | ** of the expression. affinityStr stores a static string suitable for |
||
2194 | ** P4 of OP_MakeRecord. |
||
2195 | */ |
||
2196 | affinity = comparisonAffinity( pExpr ); |
||
2197 | |||
2198 | /* Code the LHS, the <expr> from "<expr> IN (...)". |
||
2199 | */ |
||
2200 | sqlite3ExprCachePush( pParse ); |
||
2201 | r1 = sqlite3GetTempReg( pParse ); |
||
2202 | sqlite3ExprCode( pParse, pExpr.pLeft, r1 ); |
||
2203 | |||
2204 | /* If the LHS is NULL, then the result is either false or NULL depending |
||
2205 | ** on whether the RHS is empty or not, respectively. |
||
2206 | */ |
||
2207 | if ( destIfNull == destIfFalse ) |
||
2208 | { |
||
2209 | /* Shortcut for the common case where the false and NULL outcomes are |
||
2210 | ** the same. */ |
||
2211 | sqlite3VdbeAddOp2( v, OP_IsNull, r1, destIfNull ); |
||
2212 | } |
||
2213 | else |
||
2214 | { |
||
2215 | int addr1 = sqlite3VdbeAddOp1( v, OP_NotNull, r1 ); |
||
2216 | sqlite3VdbeAddOp2( v, OP_Rewind, pExpr.iTable, destIfFalse ); |
||
2217 | sqlite3VdbeAddOp2( v, OP_Goto, 0, destIfNull ); |
||
2218 | sqlite3VdbeJumpHere( v, addr1 ); |
||
2219 | } |
||
2220 | |||
2221 | if ( eType == IN_INDEX_ROWID ) |
||
2222 | { |
||
2223 | /* In this case, the RHS is the ROWID of table b-tree |
||
2224 | */ |
||
2225 | sqlite3VdbeAddOp2( v, OP_MustBeInt, r1, destIfFalse ); |
||
2226 | sqlite3VdbeAddOp3( v, OP_NotExists, pExpr.iTable, destIfFalse, r1 ); |
||
2227 | } |
||
2228 | else |
||
2229 | { |
||
2230 | /* In this case, the RHS is an index b-tree. |
||
2231 | */ |
||
2232 | sqlite3VdbeAddOp4( v, OP_Affinity, r1, 1, 0, affinity, 1 ); |
||
2233 | |||
2234 | /* If the set membership test fails, then the result of the |
||
2235 | ** "x IN (...)" expression must be either 0 or NULL. If the set |
||
2236 | ** contains no NULL values, then the result is 0. If the set |
||
2237 | ** contains one or more NULL values, then the result of the |
||
2238 | ** expression is also NULL. |
||
2239 | */ |
||
2240 | if ( rRhsHasNull == 0 || destIfFalse == destIfNull ) |
||
2241 | { |
||
2242 | /* This branch runs if it is known at compile time that the RHS |
||
2243 | ** cannot contain NULL values. This happens as the result |
||
2244 | ** of a "NOT NULL" constraint in the database schema. |
||
2245 | ** |
||
2246 | ** Also run this branch if NULL is equivalent to FALSE |
||
2247 | ** for this particular IN operator. |
||
2248 | */ |
||
2249 | sqlite3VdbeAddOp4Int( v, OP_NotFound, pExpr.iTable, destIfFalse, r1, 1 ); |
||
2250 | |||
2251 | } |
||
2252 | else |
||
2253 | { |
||
2254 | /* In this branch, the RHS of the IN might contain a NULL and |
||
2255 | ** the presence of a NULL on the RHS makes a difference in the |
||
2256 | ** outcome. |
||
2257 | */ |
||
2258 | int j1, j2, j3; |
||
2259 | |||
2260 | /* First check to see if the LHS is contained in the RHS. If so, |
||
2261 | ** then the presence of NULLs in the RHS does not matter, so jump |
||
2262 | ** over all of the code that follows. |
||
2263 | */ |
||
2264 | j1 = sqlite3VdbeAddOp4Int( v, OP_Found, pExpr.iTable, 0, r1, 1 ); |
||
2265 | |||
2266 | /* Here we begin generating code that runs if the LHS is not |
||
2267 | ** contained within the RHS. Generate additional code that |
||
2268 | ** tests the RHS for NULLs. If the RHS contains a NULL then |
||
2269 | ** jump to destIfNull. If there are no NULLs in the RHS then |
||
2270 | ** jump to destIfFalse. |
||
2271 | */ |
||
2272 | j2 = sqlite3VdbeAddOp1( v, OP_NotNull, rRhsHasNull ); |
||
2273 | j3 = sqlite3VdbeAddOp4Int( v, OP_Found, pExpr.iTable, 0, rRhsHasNull, 1 ); |
||
2274 | sqlite3VdbeAddOp2( v, OP_Integer, -1, rRhsHasNull ); |
||
2275 | sqlite3VdbeJumpHere( v, j3 ); |
||
2276 | sqlite3VdbeAddOp2( v, OP_AddImm, rRhsHasNull, 1 ); |
||
2277 | sqlite3VdbeJumpHere( v, j2 ); |
||
2278 | |||
2279 | /* Jump to the appropriate target depending on whether or not |
||
2280 | ** the RHS contains a NULL |
||
2281 | */ |
||
2282 | sqlite3VdbeAddOp2( v, OP_If, rRhsHasNull, destIfNull ); |
||
2283 | sqlite3VdbeAddOp2( v, OP_Goto, 0, destIfFalse ); |
||
2284 | |||
2285 | /* The OP_Found at the top of this branch jumps here when true, |
||
2286 | ** causing the overall IN expression evaluation to fall through. |
||
2287 | */ |
||
2288 | sqlite3VdbeJumpHere( v, j1 ); |
||
2289 | } |
||
2290 | } |
||
2291 | sqlite3ReleaseTempReg( pParse, r1 ); |
||
2292 | sqlite3ExprCachePop( pParse, 1 ); |
||
2293 | VdbeComment( v, "end IN expr" ); |
||
2294 | } |
||
2295 | #endif //* SQLITE_OMIT_SUBQUERY */ |
||
2296 | |||
2297 | /* |
||
2298 | ** Duplicate an 8-byte value |
||
2299 | */ |
||
2300 | //static char *dup8bytes(Vdbe v, string in){ |
||
2301 | // char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); |
||
2302 | // if( out ){ |
||
2303 | // memcpy(out, in, 8); |
||
2304 | // } |
||
2305 | // return out; |
||
2306 | //} |
||
2307 | |||
2308 | #if !SQLITE_OMIT_FLOATING_POINT |
||
2309 | /* |
||
2310 | ** Generate an instruction that will put the floating point |
||
2311 | ** value described by z[0..n-1] into register iMem. |
||
2312 | ** |
||
2313 | ** The z[] string will probably not be zero-terminated. But the |
||
2314 | ** z[n] character is guaranteed to be something that does not look |
||
2315 | ** like the continuation of the number. |
||
2316 | */ |
||
2317 | static void codeReal( Vdbe v, string z, bool negateFlag, int iMem ) |
||
2318 | { |
||
2319 | if ( ALWAYS( !string.IsNullOrEmpty( z ) ) ) |
||
2320 | { |
||
2321 | double value = 0; |
||
2322 | //string zV; |
||
2323 | sqlite3AtoF( z, ref value, sqlite3Strlen30( z ), SQLITE_UTF8 ); |
||
2324 | Debug.Assert( !sqlite3IsNaN( value ) ); /* The new AtoF never returns NaN */ |
||
2325 | if ( negateFlag ) |
||
2326 | value = -value; |
||
2327 | //zV = dup8bytes(v, value); |
||
2328 | sqlite3VdbeAddOp4( v, OP_Real, 0, iMem, 0, value, P4_REAL ); |
||
2329 | } |
||
2330 | } |
||
2331 | #endif |
||
2332 | |||
2333 | /* |
||
2334 | ** Generate an instruction that will put the integer describe by |
||
2335 | ** text z[0..n-1] into register iMem. |
||
2336 | ** |
||
2337 | ** Expr.u.zToken is always UTF8 and zero-terminated. |
||
2338 | */ |
||
2339 | static void codeInteger( Parse pParse, Expr pExpr, bool negFlag, int iMem ) |
||
2340 | { |
||
2341 | Vdbe v = pParse.pVdbe; |
||
2342 | if ( ( pExpr.flags & EP_IntValue ) != 0 ) |
||
2343 | { |
||
2344 | int i = pExpr.u.iValue; |
||
2345 | Debug.Assert( i >= 0 ); |
||
2346 | if ( negFlag ) |
||
2347 | i = -i; |
||
2348 | sqlite3VdbeAddOp2( v, OP_Integer, i, iMem ); |
||
2349 | } |
||
2350 | else |
||
2351 | { |
||
2352 | int c; |
||
2353 | i64 value = 0; |
||
2354 | string z = pExpr.u.zToken; |
||
2355 | Debug.Assert( !string.IsNullOrEmpty( z ) ); |
||
2356 | c = sqlite3Atoi64( z, ref value, sqlite3Strlen30( z ), SQLITE_UTF8 ); |
||
2357 | if ( c == 0 || ( c == 2 && negFlag ) ) |
||
2358 | { |
||
2359 | //char* zV; |
||
2360 | if ( negFlag ) |
||
2361 | { |
||
2362 | value = c == 2 ? SMALLEST_INT64 : -value; |
||
2363 | } |
||
2364 | sqlite3VdbeAddOp4( v, OP_Int64, 0, iMem, 0, value, P4_INT64 ); |
||
2365 | } |
||
2366 | else |
||
2367 | { |
||
2368 | #if SQLITE_OMIT_FLOATING_POINT |
||
2369 | sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : string.Empty, z); |
||
2370 | #else |
||
2371 | codeReal( v, z, negFlag, iMem ); |
||
2372 | #endif |
||
2373 | } |
||
2374 | } |
||
2375 | } |
||
2376 | |||
2377 | /* |
||
2378 | ** Clear a cache entry. |
||
2379 | */ |
||
2380 | static void cacheEntryClear( Parse pParse, yColCache p ) |
||
2381 | { |
||
2382 | if ( p.tempReg != 0 ) |
||
2383 | { |
||
2384 | if ( pParse.nTempReg < ArraySize( pParse.aTempReg ) ) |
||
2385 | { |
||
2386 | pParse.aTempReg[pParse.nTempReg++] = p.iReg; |
||
2387 | } |
||
2388 | p.tempReg = 0; |
||
2389 | } |
||
2390 | } |
||
2391 | |||
2392 | |||
2393 | /* |
||
2394 | ** Record in the column cache that a particular column from a |
||
2395 | ** particular table is stored in a particular register. |
||
2396 | */ |
||
2397 | static void sqlite3ExprCacheStore( Parse pParse, int iTab, int iCol, int iReg ) |
||
2398 | { |
||
2399 | int i; |
||
2400 | int minLru; |
||
2401 | int idxLru; |
||
2402 | yColCache p = new yColCache(); |
||
2403 | |||
2404 | Debug.Assert( iReg > 0 ); /* Register numbers are always positive */ |
||
2405 | Debug.Assert( iCol >= -1 && iCol < 32768 ); /* Finite column numbers */ |
||
2406 | |||
2407 | /* The SQLITE_ColumnCache flag disables the column cache. This is used |
||
2408 | ** for testing only - to verify that SQLite always gets the same answer |
||
2409 | ** with and without the column cache. |
||
2410 | */ |
||
2411 | if ( ( pParse.db.flags & SQLITE_ColumnCache ) != 0 ) |
||
2412 | return; |
||
2413 | |||
2414 | /* First replace any existing entry. |
||
2415 | ** |
||
2416 | ** Actually, the way the column cache is currently used, we are guaranteed |
||
2417 | ** that the object will never already be in cache. Verify this guarantee. |
||
2418 | */ |
||
2419 | #if !NDEBUG |
||
2420 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )//p=pParse.aColCache... p++) |
||
2421 | { |
||
2422 | #if FALSE //* This code wold remove the entry from the cache if it existed */ |
||
2423 | p = pParse.aColCache[i]; |
||
2424 | if ( p.iReg != 0 && p.iTable == iTab && p.iColumn == iCol ) |
||
2425 | { |
||
2426 | cacheEntryClear( pParse, p ); |
||
2427 | p.iLevel = pParse.iCacheLevel; |
||
2428 | p.iReg = iReg; |
||
2429 | p.lru = pParse.iCacheCnt++; |
||
2430 | return; |
||
2431 | } |
||
2432 | #endif |
||
2433 | Debug.Assert( p.iReg == 0 || p.iTable != iTab || p.iColumn != iCol ); |
||
2434 | } |
||
2435 | #endif |
||
2436 | |||
2437 | /* Find an empty slot and replace it */ |
||
2438 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )//p=pParse.aColCache... p++) |
||
2439 | { |
||
2440 | p = pParse.aColCache[i]; |
||
2441 | if ( p.iReg == 0 ) |
||
2442 | { |
||
2443 | p.iLevel = pParse.iCacheLevel; |
||
2444 | p.iTable = iTab; |
||
2445 | p.iColumn = iCol; |
||
2446 | p.iReg = iReg; |
||
2447 | p.tempReg = 0; |
||
2448 | p.lru = pParse.iCacheCnt++; |
||
2449 | return; |
||
2450 | } |
||
2451 | } |
||
2452 | |||
2453 | /* Replace the last recently used */ |
||
2454 | minLru = 0x7fffffff; |
||
2455 | idxLru = -1; |
||
2456 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )//p=pParse.aColCache..., p++) |
||
2457 | { |
||
2458 | p = pParse.aColCache[i]; |
||
2459 | if ( p.lru < minLru ) |
||
2460 | { |
||
2461 | idxLru = i; |
||
2462 | minLru = p.lru; |
||
2463 | } |
||
2464 | } |
||
2465 | if ( ALWAYS( idxLru >= 0 ) ) |
||
2466 | { |
||
2467 | p = pParse.aColCache[idxLru]; |
||
2468 | p.iLevel = pParse.iCacheLevel; |
||
2469 | p.iTable = iTab; |
||
2470 | p.iColumn = iCol; |
||
2471 | p.iReg = iReg; |
||
2472 | p.tempReg = 0; |
||
2473 | p.lru = pParse.iCacheCnt++; |
||
2474 | return; |
||
2475 | } |
||
2476 | } |
||
2477 | |||
2478 | /* |
||
2479 | ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. |
||
2480 | ** Purge the range of registers from the column cache. |
||
2481 | */ |
||
2482 | static void sqlite3ExprCacheRemove( Parse pParse, int iReg, int nReg ) |
||
2483 | { |
||
2484 | int i; |
||
2485 | int iLast = iReg + nReg - 1; |
||
2486 | yColCache p; |
||
2487 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )//p=pParse.aColCache... p++) |
||
2488 | { |
||
2489 | p = pParse.aColCache[i]; |
||
2490 | int r = p.iReg; |
||
2491 | if ( r >= iReg && r <= iLast ) |
||
2492 | { |
||
2493 | cacheEntryClear( pParse, p ); |
||
2494 | p.iReg = 0; |
||
2495 | } |
||
2496 | } |
||
2497 | } |
||
2498 | |||
2499 | /* |
||
2500 | ** Remember the current column cache context. Any new entries added |
||
2501 | ** added to the column cache after this call are removed when the |
||
2502 | ** corresponding pop occurs. |
||
2503 | */ |
||
2504 | static void sqlite3ExprCachePush( Parse pParse ) |
||
2505 | { |
||
2506 | pParse.iCacheLevel++; |
||
2507 | } |
||
2508 | |||
2509 | /* |
||
2510 | ** Remove from the column cache any entries that were added since the |
||
2511 | ** the previous N Push operations. In other words, restore the cache |
||
2512 | ** to the state it was in N Pushes ago. |
||
2513 | */ |
||
2514 | static void sqlite3ExprCachePop( Parse pParse, int N ) |
||
2515 | { |
||
2516 | int i; |
||
2517 | yColCache p; |
||
2518 | Debug.Assert( N > 0 ); |
||
2519 | Debug.Assert( pParse.iCacheLevel >= N ); |
||
2520 | pParse.iCacheLevel -= N; |
||
2521 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )// p++) |
||
2522 | { |
||
2523 | p = pParse.aColCache[i]; |
||
2524 | if ( p.iReg != 0 && p.iLevel > pParse.iCacheLevel ) |
||
2525 | { |
||
2526 | cacheEntryClear( pParse, p ); |
||
2527 | p.iReg = 0; |
||
2528 | } |
||
2529 | } |
||
2530 | } |
||
2531 | |||
2532 | /* |
||
2533 | ** When a cached column is reused, make sure that its register is |
||
2534 | ** no longer available as a temp register. ticket #3879: that same |
||
2535 | ** register might be in the cache in multiple places, so be sure to |
||
2536 | ** get them all. |
||
2537 | */ |
||
2538 | static void sqlite3ExprCachePinRegister( Parse pParse, int iReg ) |
||
2539 | { |
||
2540 | int i; |
||
2541 | yColCache p; |
||
2542 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )//p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++) |
||
2543 | { |
||
2544 | p = pParse.aColCache[i]; |
||
2545 | if ( p.iReg == iReg ) |
||
2546 | { |
||
2547 | p.tempReg = 0; |
||
2548 | } |
||
2549 | } |
||
2550 | } |
||
2551 | |||
2552 | /* |
||
2553 | ** Generate code to extract the value of the iCol-th column of a table. |
||
2554 | */ |
||
2555 | static void sqlite3ExprCodeGetColumnOfTable( |
||
2556 | Vdbe v, /* The VDBE under construction */ |
||
2557 | Table pTab, /* The table containing the value */ |
||
2558 | int iTabCur, /* The cursor for this table */ |
||
2559 | int iCol, /* Index of the column to extract */ |
||
2560 | int regOut /* Extract the value into this register */ |
||
2561 | ) |
||
2562 | { |
||
2563 | if ( iCol < 0 || iCol == pTab.iPKey ) |
||
2564 | { |
||
2565 | sqlite3VdbeAddOp2( v, OP_Rowid, iTabCur, regOut ); |
||
2566 | } |
||
2567 | else |
||
2568 | { |
||
2569 | int op = IsVirtual( pTab ) ? OP_VColumn : OP_Column; |
||
2570 | sqlite3VdbeAddOp3( v, op, iTabCur, iCol, regOut ); |
||
2571 | } |
||
2572 | if ( iCol >= 0 ) |
||
2573 | { |
||
2574 | sqlite3ColumnDefault( v, pTab, iCol, regOut ); |
||
2575 | } |
||
2576 | } |
||
2577 | |||
2578 | /* |
||
2579 | ** Generate code that will extract the iColumn-th column from |
||
2580 | ** table pTab and store the column value in a register. An effort |
||
2581 | ** is made to store the column value in register iReg, but this is |
||
2582 | ** not guaranteed. The location of the column value is returned. |
||
2583 | ** |
||
2584 | ** There must be an open cursor to pTab in iTable when this routine |
||
2585 | ** is called. If iColumn<0 then code is generated that extracts the rowid. |
||
2586 | */ |
||
2587 | static int sqlite3ExprCodeGetColumn( |
||
2588 | Parse pParse, /* Parsing and code generating context */ |
||
2589 | Table pTab, /* Description of the table we are reading from */ |
||
2590 | int iColumn, /* Index of the table column */ |
||
2591 | int iTable, /* The cursor pointing to the table */ |
||
2592 | int iReg /* Store results here */ |
||
2593 | ) |
||
2594 | { |
||
2595 | Vdbe v = pParse.pVdbe; |
||
2596 | int i; |
||
2597 | yColCache p; |
||
2598 | |||
2599 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ ) |
||
2600 | {// p=pParse.aColCache, p++ |
||
2601 | p = pParse.aColCache[i]; |
||
2602 | if ( p.iReg > 0 && p.iTable == iTable && p.iColumn == iColumn ) |
||
2603 | { |
||
2604 | p.lru = pParse.iCacheCnt++; |
||
2605 | sqlite3ExprCachePinRegister( pParse, p.iReg ); |
||
2606 | return p.iReg; |
||
2607 | } |
||
2608 | } |
||
2609 | Debug.Assert( v != null ); |
||
2610 | sqlite3ExprCodeGetColumnOfTable( v, pTab, iTable, iColumn, iReg ); |
||
2611 | sqlite3ExprCacheStore( pParse, iTable, iColumn, iReg ); |
||
2612 | return iReg; |
||
2613 | } |
||
2614 | |||
2615 | /* |
||
2616 | ** Clear all column cache entries. |
||
2617 | */ |
||
2618 | static void sqlite3ExprCacheClear( Parse pParse ) |
||
2619 | { |
||
2620 | int i; |
||
2621 | yColCache p; |
||
2622 | |||
2623 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )// p=pParse.aColCache... p++) |
||
2624 | { |
||
2625 | p = pParse.aColCache[i]; |
||
2626 | if ( p.iReg != 0 ) |
||
2627 | { |
||
2628 | cacheEntryClear( pParse, p ); |
||
2629 | p.iReg = 0; |
||
2630 | } |
||
2631 | } |
||
2632 | } |
||
2633 | |||
2634 | /* |
||
2635 | ** Record the fact that an affinity change has occurred on iCount |
||
2636 | ** registers starting with iStart. |
||
2637 | */ |
||
2638 | static void sqlite3ExprCacheAffinityChange( Parse pParse, int iStart, int iCount ) |
||
2639 | { |
||
2640 | sqlite3ExprCacheRemove( pParse, iStart, iCount ); |
||
2641 | } |
||
2642 | |||
2643 | /* |
||
2644 | ** Generate code to move content from registers iFrom...iFrom+nReg-1 |
||
2645 | ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. |
||
2646 | */ |
||
2647 | static void sqlite3ExprCodeMove( Parse pParse, int iFrom, int iTo, int nReg ) |
||
2648 | { |
||
2649 | int i; |
||
2650 | yColCache p; |
||
2651 | if ( NEVER( iFrom == iTo ) ) |
||
2652 | return; |
||
2653 | sqlite3VdbeAddOp3( pParse.pVdbe, OP_Move, iFrom, iTo, nReg ); |
||
2654 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )// p=pParse.aColCache... p++) |
||
2655 | { |
||
2656 | p = pParse.aColCache[i]; |
||
2657 | int x = p.iReg; |
||
2658 | if ( x >= iFrom && x < iFrom + nReg ) |
||
2659 | { |
||
2660 | p.iReg += iTo - iFrom; |
||
2661 | } |
||
2662 | } |
||
2663 | } |
||
2664 | |||
2665 | /* |
||
2666 | ** Generate code to copy content from registers iFrom...iFrom+nReg-1 |
||
2667 | ** over to iTo..iTo+nReg-1. |
||
2668 | */ |
||
2669 | static void sqlite3ExprCodeCopy( Parse pParse, int iFrom, int iTo, int nReg ) |
||
2670 | { |
||
2671 | int i; |
||
2672 | if ( NEVER( iFrom == iTo ) ) |
||
2673 | return; |
||
2674 | for ( i = 0; i < nReg; i++ ) |
||
2675 | { |
||
2676 | sqlite3VdbeAddOp2( pParse.pVdbe, OP_Copy, iFrom + i, iTo + i ); |
||
2677 | } |
||
2678 | } |
||
2679 | |||
2680 | #if (SQLITE_DEBUG) || (SQLITE_COVERAGE_TEST) |
||
2681 | /* |
||
2682 | ** Return true if any register in the range iFrom..iTo (inclusive) |
||
2683 | ** is used as part of the column cache. |
||
2684 | ** |
||
2685 | ** This routine is used within Debug.Assert() and testcase() macros only |
||
2686 | ** and does not appear in a normal build. |
||
2687 | */ |
||
2688 | static int usedAsColumnCache( Parse pParse, int iFrom, int iTo ) |
||
2689 | { |
||
2690 | int i; |
||
2691 | yColCache p; |
||
2692 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )//p=pParse.aColCache... p++) |
||
2693 | { |
||
2694 | p = pParse.aColCache[i]; |
||
2695 | int r = p.iReg; |
||
2696 | if ( r >= iFrom && r <= iTo ) |
||
2697 | return 1; /*NO_TEST*/ |
||
2698 | } |
||
2699 | return 0; |
||
2700 | } |
||
2701 | #else |
||
2702 | static int usedAsColumnCache( Parse pParse, int iFrom, int iTo ){return 0;} |
||
2703 | #endif //* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ |
||
2704 | |||
2705 | |||
2706 | /* |
||
2707 | ** Generate code into the current Vdbe to evaluate the given |
||
2708 | ** expression. Attempt to store the results in register "target". |
||
2709 | ** Return the register where results are stored. |
||
2710 | ** |
||
2711 | ** With this routine, there is no guarantee that results will |
||
2712 | ** be stored in target. The result might be stored in some other |
||
2713 | ** register if it is convenient to do so. The calling function |
||
2714 | ** must check the return code and move the results to the desired |
||
2715 | ** register. |
||
2716 | */ |
||
2717 | static int sqlite3ExprCodeTarget( Parse pParse, Expr pExpr, int target ) |
||
2718 | { |
||
2719 | Vdbe v = pParse.pVdbe; /* The VM under construction */ |
||
2720 | int op; /* The opcode being coded */ |
||
2721 | int inReg = target; /* Results stored in register inReg */ |
||
2722 | int regFree1 = 0; /* If non-zero free this temporary register */ |
||
2723 | int regFree2 = 0; /* If non-zero free this temporary register */ |
||
2724 | int r1 = 0, r2 = 0, r3 = 0, r4 = 0; /* Various register numbers */ |
||
2725 | sqlite3 db = pParse.db; /* The database connection */ |
||
2726 | |||
2727 | Debug.Assert( target > 0 && target <= pParse.nMem ); |
||
2728 | if ( v == null ) |
||
2729 | { |
||
2730 | //Debug.Assert( pParse.db.mallocFailed != 0 ); |
||
2731 | return 0; |
||
2732 | } |
||
2733 | |||
2734 | if ( pExpr == null ) |
||
2735 | { |
||
2736 | op = TK_NULL; |
||
2737 | } |
||
2738 | else |
||
2739 | { |
||
2740 | op = pExpr.op; |
||
2741 | } |
||
2742 | switch ( op ) |
||
2743 | { |
||
2744 | case TK_AGG_COLUMN: |
||
2745 | { |
||
2746 | AggInfo pAggInfo = pExpr.pAggInfo; |
||
2747 | AggInfo_col pCol = pAggInfo.aCol[pExpr.iAgg]; |
||
2748 | if ( pAggInfo.directMode == 0 ) |
||
2749 | { |
||
2750 | Debug.Assert( pCol.iMem > 0 ); |
||
2751 | inReg = pCol.iMem; |
||
2752 | break; |
||
2753 | } |
||
2754 | else if ( pAggInfo.useSortingIdx != 0 ) |
||
2755 | { |
||
2756 | sqlite3VdbeAddOp3( v, OP_Column, pAggInfo.sortingIdx, |
||
2757 | pCol.iSorterColumn, target ); |
||
2758 | break; |
||
2759 | } |
||
2760 | /* Otherwise, fall thru into the TK_COLUMN case */ |
||
2761 | } |
||
2762 | goto case TK_COLUMN; |
||
2763 | case TK_COLUMN: |
||
2764 | { |
||
2765 | if ( pExpr.iTable < 0 ) |
||
2766 | { |
||
2767 | /* This only happens when coding check constraints */ |
||
2768 | Debug.Assert( pParse.ckBase > 0 ); |
||
2769 | inReg = pExpr.iColumn + pParse.ckBase; |
||
2770 | } |
||
2771 | else |
||
2772 | { |
||
2773 | inReg = sqlite3ExprCodeGetColumn( pParse, pExpr.pTab, |
||
2774 | pExpr.iColumn, pExpr.iTable, target ); |
||
2775 | } |
||
2776 | break; |
||
2777 | } |
||
2778 | case TK_INTEGER: |
||
2779 | { |
||
2780 | codeInteger( pParse, pExpr, false, target ); |
||
2781 | break; |
||
2782 | } |
||
2783 | #if !SQLITE_OMIT_FLOATING_POINT |
||
2784 | case TK_FLOAT: |
||
2785 | { |
||
2786 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
2787 | codeReal( v, pExpr.u.zToken, false, target ); |
||
2788 | break; |
||
2789 | } |
||
2790 | #endif |
||
2791 | case TK_STRING: |
||
2792 | { |
||
2793 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
2794 | sqlite3VdbeAddOp4( v, OP_String8, 0, target, 0, pExpr.u.zToken, 0 ); |
||
2795 | break; |
||
2796 | } |
||
2797 | case TK_NULL: |
||
2798 | { |
||
2799 | sqlite3VdbeAddOp2( v, OP_Null, 0, target ); |
||
2800 | break; |
||
2801 | } |
||
2802 | #if !SQLITE_OMIT_BLOB_LITERAL |
||
2803 | case TK_BLOB: |
||
2804 | { |
||
2805 | int n; |
||
2806 | string z; |
||
2807 | byte[] zBlob; |
||
2808 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
2809 | Debug.Assert( pExpr.u.zToken[0] == 'x' || pExpr.u.zToken[0] == 'X' ); |
||
2810 | Debug.Assert( pExpr.u.zToken[1] == '\'' ); |
||
2811 | z = pExpr.u.zToken.Substring( 2 ); |
||
2812 | n = sqlite3Strlen30( z ) - 1; |
||
2813 | Debug.Assert( z[n] == '\'' ); |
||
2814 | zBlob = sqlite3HexToBlob( sqlite3VdbeDb( v ), z, n ); |
||
2815 | sqlite3VdbeAddOp4( v, OP_Blob, n / 2, target, 0, zBlob, P4_DYNAMIC ); |
||
2816 | break; |
||
2817 | } |
||
2818 | #endif |
||
2819 | case TK_VARIABLE: |
||
2820 | { |
||
2821 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
2822 | Debug.Assert( pExpr.u.zToken != null ); |
||
2823 | Debug.Assert( pExpr.u.zToken.Length != 0 ); |
||
2824 | sqlite3VdbeAddOp2( v, OP_Variable, pExpr.iColumn, target ); |
||
2825 | if ( pExpr.u.zToken.Length > 1 ) |
||
2826 | { |
||
2827 | Debug.Assert( pExpr.u.zToken[0] == '?' |
||
2828 | || pExpr.u.zToken.CompareTo(pParse.azVar[pExpr.iColumn - 1] ) == 0 ); |
||
2829 | sqlite3VdbeChangeP4( v, -1, pParse.azVar[pExpr.iColumn - 1], P4_STATIC ); |
||
2830 | } |
||
2831 | break; |
||
2832 | } |
||
2833 | case TK_REGISTER: |
||
2834 | { |
||
2835 | inReg = pExpr.iTable; |
||
2836 | break; |
||
2837 | } |
||
2838 | case TK_AS: |
||
2839 | { |
||
2840 | inReg = sqlite3ExprCodeTarget( pParse, pExpr.pLeft, target ); |
||
2841 | break; |
||
2842 | } |
||
2843 | #if !SQLITE_OMIT_CAST |
||
2844 | case TK_CAST: |
||
2845 | { |
||
2846 | /* Expressions of the form: CAST(pLeft AS token) */ |
||
2847 | int aff, to_op; |
||
2848 | inReg = sqlite3ExprCodeTarget( pParse, pExpr.pLeft, target ); |
||
2849 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
2850 | aff = sqlite3AffinityType( pExpr.u.zToken ); |
||
2851 | to_op = aff - SQLITE_AFF_TEXT + OP_ToText; |
||
2852 | Debug.Assert( to_op == OP_ToText || aff != SQLITE_AFF_TEXT ); |
||
2853 | Debug.Assert( to_op == OP_ToBlob || aff != SQLITE_AFF_NONE ); |
||
2854 | Debug.Assert( to_op == OP_ToNumeric || aff != SQLITE_AFF_NUMERIC ); |
||
2855 | Debug.Assert( to_op == OP_ToInt || aff != SQLITE_AFF_INTEGER ); |
||
2856 | Debug.Assert( to_op == OP_ToReal || aff != SQLITE_AFF_REAL ); |
||
2857 | testcase( to_op == OP_ToText ); |
||
2858 | testcase( to_op == OP_ToBlob ); |
||
2859 | testcase( to_op == OP_ToNumeric ); |
||
2860 | testcase( to_op == OP_ToInt ); |
||
2861 | testcase( to_op == OP_ToReal ); |
||
2862 | if ( inReg != target ) |
||
2863 | { |
||
2864 | sqlite3VdbeAddOp2( v, OP_SCopy, inReg, target ); |
||
2865 | inReg = target; |
||
2866 | } |
||
2867 | sqlite3VdbeAddOp1( v, to_op, inReg ); |
||
2868 | testcase( usedAsColumnCache( pParse, inReg, inReg ) != 0 ); |
||
2869 | sqlite3ExprCacheAffinityChange( pParse, inReg, 1 ); |
||
2870 | break; |
||
2871 | } |
||
2872 | #endif // * SQLITE_OMIT_CAST */ |
||
2873 | case TK_LT: |
||
2874 | case TK_LE: |
||
2875 | case TK_GT: |
||
2876 | case TK_GE: |
||
2877 | case TK_NE: |
||
2878 | case TK_EQ: |
||
2879 | { |
||
2880 | Debug.Assert( TK_LT == OP_Lt ); |
||
2881 | Debug.Assert( TK_LE == OP_Le ); |
||
2882 | Debug.Assert( TK_GT == OP_Gt ); |
||
2883 | Debug.Assert( TK_GE == OP_Ge ); |
||
2884 | Debug.Assert( TK_EQ == OP_Eq ); |
||
2885 | Debug.Assert( TK_NE == OP_Ne ); |
||
2886 | testcase( op == TK_LT ); |
||
2887 | testcase( op == TK_LE ); |
||
2888 | testcase( op == TK_GT ); |
||
2889 | testcase( op == TK_GE ); |
||
2890 | testcase( op == TK_EQ ); |
||
2891 | testcase( op == TK_NE ); |
||
2892 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
2893 | r2 = sqlite3ExprCodeTemp( pParse, pExpr.pRight, ref regFree2 ); |
||
2894 | codeCompare( pParse, pExpr.pLeft, pExpr.pRight, op, |
||
2895 | r1, r2, inReg, SQLITE_STOREP2 ); |
||
2896 | testcase( regFree1 == 0 ); |
||
2897 | testcase( regFree2 == 0 ); |
||
2898 | break; |
||
2899 | } |
||
2900 | case TK_IS: |
||
2901 | case TK_ISNOT: |
||
2902 | { |
||
2903 | testcase( op == TK_IS ); |
||
2904 | testcase( op == TK_ISNOT ); |
||
2905 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
2906 | r2 = sqlite3ExprCodeTemp( pParse, pExpr.pRight, ref regFree2 ); |
||
2907 | op = ( op == TK_IS ) ? TK_EQ : TK_NE; |
||
2908 | codeCompare( pParse, pExpr.pLeft, pExpr.pRight, op, |
||
2909 | r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ ); |
||
2910 | testcase( regFree1 == 0 ); |
||
2911 | testcase( regFree2 == 0 ); |
||
2912 | break; |
||
2913 | } |
||
2914 | case TK_AND: |
||
2915 | case TK_OR: |
||
2916 | case TK_PLUS: |
||
2917 | case TK_STAR: |
||
2918 | case TK_MINUS: |
||
2919 | case TK_REM: |
||
2920 | case TK_BITAND: |
||
2921 | case TK_BITOR: |
||
2922 | case TK_SLASH: |
||
2923 | case TK_LSHIFT: |
||
2924 | case TK_RSHIFT: |
||
2925 | case TK_CONCAT: |
||
2926 | { |
||
2927 | Debug.Assert( TK_AND == OP_And ); |
||
2928 | Debug.Assert( TK_OR == OP_Or ); |
||
2929 | Debug.Assert( TK_PLUS == OP_Add ); |
||
2930 | Debug.Assert( TK_MINUS == OP_Subtract ); |
||
2931 | Debug.Assert( TK_REM == OP_Remainder ); |
||
2932 | Debug.Assert( TK_BITAND == OP_BitAnd ); |
||
2933 | Debug.Assert( TK_BITOR == OP_BitOr ); |
||
2934 | Debug.Assert( TK_SLASH == OP_Divide ); |
||
2935 | Debug.Assert( TK_LSHIFT == OP_ShiftLeft ); |
||
2936 | Debug.Assert( TK_RSHIFT == OP_ShiftRight ); |
||
2937 | Debug.Assert( TK_CONCAT == OP_Concat ); |
||
2938 | testcase( op == TK_AND ); |
||
2939 | testcase( op == TK_OR ); |
||
2940 | testcase( op == TK_PLUS ); |
||
2941 | testcase( op == TK_MINUS ); |
||
2942 | testcase( op == TK_REM ); |
||
2943 | testcase( op == TK_BITAND ); |
||
2944 | testcase( op == TK_BITOR ); |
||
2945 | testcase( op == TK_SLASH ); |
||
2946 | testcase( op == TK_LSHIFT ); |
||
2947 | testcase( op == TK_RSHIFT ); |
||
2948 | testcase( op == TK_CONCAT ); |
||
2949 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
2950 | r2 = sqlite3ExprCodeTemp( pParse, pExpr.pRight, ref regFree2 ); |
||
2951 | sqlite3VdbeAddOp3( v, op, r2, r1, target ); |
||
2952 | testcase( regFree1 == 0 ); |
||
2953 | testcase( regFree2 == 0 ); |
||
2954 | break; |
||
2955 | } |
||
2956 | case TK_UMINUS: |
||
2957 | { |
||
2958 | Expr pLeft = pExpr.pLeft; |
||
2959 | Debug.Assert( pLeft != null ); |
||
2960 | if ( pLeft.op == TK_INTEGER ) |
||
2961 | { |
||
2962 | codeInteger( pParse, pLeft, true, target ); |
||
2963 | #if !SQLITE_OMIT_FLOATING_POINT |
||
2964 | } |
||
2965 | else if ( pLeft.op == TK_FLOAT ) |
||
2966 | { |
||
2967 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
2968 | codeReal( v, pLeft.u.zToken, true, target ); |
||
2969 | #endif |
||
2970 | } |
||
2971 | else |
||
2972 | { |
||
2973 | regFree1 = r1 = sqlite3GetTempReg( pParse ); |
||
2974 | sqlite3VdbeAddOp2( v, OP_Integer, 0, r1 ); |
||
2975 | r2 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree2 ); |
||
2976 | sqlite3VdbeAddOp3( v, OP_Subtract, r2, r1, target ); |
||
2977 | testcase( regFree2 == 0 ); |
||
2978 | } |
||
2979 | inReg = target; |
||
2980 | break; |
||
2981 | } |
||
2982 | case TK_BITNOT: |
||
2983 | case TK_NOT: |
||
2984 | { |
||
2985 | Debug.Assert( TK_BITNOT == OP_BitNot ); |
||
2986 | Debug.Assert( TK_NOT == OP_Not ); |
||
2987 | testcase( op == TK_BITNOT ); |
||
2988 | testcase( op == TK_NOT ); |
||
2989 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
2990 | testcase( regFree1 == 0 ); |
||
2991 | inReg = target; |
||
2992 | sqlite3VdbeAddOp2( v, op, r1, inReg ); |
||
2993 | break; |
||
2994 | } |
||
2995 | case TK_ISNULL: |
||
2996 | case TK_NOTNULL: |
||
2997 | { |
||
2998 | int addr; |
||
2999 | Debug.Assert( TK_ISNULL == OP_IsNull ); |
||
3000 | Debug.Assert( TK_NOTNULL == OP_NotNull ); |
||
3001 | testcase( op == TK_ISNULL ); |
||
3002 | testcase( op == TK_NOTNULL ); |
||
3003 | sqlite3VdbeAddOp2( v, OP_Integer, 1, target ); |
||
3004 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
3005 | testcase( regFree1 == 0 ); |
||
3006 | addr = sqlite3VdbeAddOp1( v, op, r1 ); |
||
3007 | sqlite3VdbeAddOp2( v, OP_AddImm, target, -1 ); |
||
3008 | sqlite3VdbeJumpHere( v, addr ); |
||
3009 | break; |
||
3010 | } |
||
3011 | case TK_AGG_FUNCTION: |
||
3012 | { |
||
3013 | AggInfo pInfo = pExpr.pAggInfo; |
||
3014 | if ( pInfo == null ) |
||
3015 | { |
||
3016 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
3017 | sqlite3ErrorMsg( pParse, "misuse of aggregate: %s()", pExpr.u.zToken ); |
||
3018 | } |
||
3019 | else |
||
3020 | { |
||
3021 | inReg = pInfo.aFunc[pExpr.iAgg].iMem; |
||
3022 | } |
||
3023 | break; |
||
3024 | } |
||
3025 | case TK_CONST_FUNC: |
||
3026 | case TK_FUNCTION: |
||
3027 | { |
||
3028 | ExprList pFarg; /* List of function arguments */ |
||
3029 | int nFarg; /* Number of function arguments */ |
||
3030 | FuncDef pDef; /* The function definition object */ |
||
3031 | int nId; /* Length of the function name in bytes */ |
||
3032 | string zId; /* The function name */ |
||
3033 | int constMask = 0; /* Mask of function arguments that are constant */ |
||
3034 | int i; /* Loop counter */ |
||
3035 | u8 enc = ENC( db ); /* The text encoding used by this database */ |
||
3036 | CollSeq pColl = null; /* A collating sequence */ |
||
3037 | |||
3038 | Debug.Assert( !ExprHasProperty( pExpr, EP_xIsSelect ) ); |
||
3039 | testcase( op == TK_CONST_FUNC ); |
||
3040 | testcase( op == TK_FUNCTION ); |
||
3041 | if ( ExprHasAnyProperty( pExpr, EP_TokenOnly ) ) |
||
3042 | { |
||
3043 | pFarg = null; |
||
3044 | } |
||
3045 | else |
||
3046 | { |
||
3047 | pFarg = pExpr.x.pList; |
||
3048 | } |
||
3049 | nFarg = pFarg != null ? pFarg.nExpr : 0; |
||
3050 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
3051 | zId = pExpr.u.zToken; |
||
3052 | nId = sqlite3Strlen30( zId ); |
||
3053 | pDef = sqlite3FindFunction( pParse.db, zId, nId, nFarg, enc, 0 ); |
||
3054 | if ( pDef == null ) |
||
3055 | { |
||
3056 | sqlite3ErrorMsg( pParse, "unknown function: %.*s()", nId, zId ); |
||
3057 | break; |
||
3058 | } |
||
3059 | |||
3060 | /* Attempt a direct implementation of the built-in COALESCE() and |
||
3061 | ** IFNULL() functions. This avoids unnecessary evalation of |
||
3062 | ** arguments past the first non-NULL argument. |
||
3063 | */ |
||
3064 | if ( ( pDef.flags & SQLITE_FUNC_COALESCE ) != 0 ) |
||
3065 | { |
||
3066 | int endCoalesce = sqlite3VdbeMakeLabel( v ); |
||
3067 | Debug.Assert( nFarg >= 2 ); |
||
3068 | sqlite3ExprCode( pParse, pFarg.a[0].pExpr, target ); |
||
3069 | for ( i = 1; i < nFarg; i++ ) |
||
3070 | { |
||
3071 | sqlite3VdbeAddOp2( v, OP_NotNull, target, endCoalesce ); |
||
3072 | sqlite3ExprCacheRemove( pParse, target, 1 ); |
||
3073 | sqlite3ExprCachePush( pParse ); |
||
3074 | sqlite3ExprCode( pParse, pFarg.a[i].pExpr, target ); |
||
3075 | sqlite3ExprCachePop( pParse, 1 ); |
||
3076 | } |
||
3077 | sqlite3VdbeResolveLabel( v, endCoalesce ); |
||
3078 | break; |
||
3079 | } |
||
3080 | |||
3081 | if ( pFarg != null ) |
||
3082 | { |
||
3083 | r1 = sqlite3GetTempRange( pParse, nFarg ); |
||
3084 | sqlite3ExprCachePush( pParse ); /* Ticket 2ea2425d34be */ |
||
3085 | sqlite3ExprCodeExprList( pParse, pFarg, r1, true ); |
||
3086 | sqlite3ExprCachePop( pParse, 1 ); /* Ticket 2ea2425d34be */ |
||
3087 | } |
||
3088 | else |
||
3089 | { |
||
3090 | r1 = 0; |
||
3091 | } |
||
3092 | #if !SQLITE_OMIT_VIRTUALTABLE |
||
3093 | /* Possibly overload the function if the first argument is |
||
3094 | ** a virtual table column. |
||
3095 | ** |
||
3096 | ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the |
||
3097 | ** second argument, not the first, as the argument to test to |
||
3098 | ** see if it is a column in a virtual table. This is done because |
||
3099 | ** the left operand of infix functions (the operand we want to |
||
3100 | ** control overloading) ends up as the second argument to the |
||
3101 | ** function. The expression "A glob B" is equivalent to |
||
3102 | ** "glob(B,A). We want to use the A in "A glob B" to test |
||
3103 | ** for function overloading. But we use the B term in "glob(B,A)". |
||
3104 | */ |
||
3105 | if ( nFarg >= 2 && ( pExpr.flags & EP_InfixFunc ) != 0 ) |
||
3106 | { |
||
3107 | pDef = sqlite3VtabOverloadFunction( db, pDef, nFarg, pFarg.a[1].pExpr ); |
||
3108 | } |
||
3109 | else if ( nFarg > 0 ) |
||
3110 | { |
||
3111 | pDef = sqlite3VtabOverloadFunction( db, pDef, nFarg, pFarg.a[0].pExpr ); |
||
3112 | } |
||
3113 | #endif |
||
3114 | for ( i = 0; i < nFarg; i++ ) |
||
3115 | { |
||
3116 | if ( i < 32 && sqlite3ExprIsConstant( pFarg.a[i].pExpr ) != 0 ) |
||
3117 | { |
||
3118 | constMask |= ( 1 << i ); |
||
3119 | } |
||
3120 | if ( ( pDef.flags & SQLITE_FUNC_NEEDCOLL ) != 0 && null == pColl ) |
||
3121 | { |
||
3122 | pColl = sqlite3ExprCollSeq( pParse, pFarg.a[i].pExpr ); |
||
3123 | } |
||
3124 | } |
||
3125 | if ( ( pDef.flags & SQLITE_FUNC_NEEDCOLL ) != 0 ) |
||
3126 | { |
||
3127 | if ( null == pColl ) |
||
3128 | pColl = db.pDfltColl; |
||
3129 | sqlite3VdbeAddOp4( v, OP_CollSeq, 0, 0, 0, pColl, P4_COLLSEQ ); |
||
3130 | } |
||
3131 | sqlite3VdbeAddOp4( v, OP_Function, constMask, r1, target, |
||
3132 | pDef, P4_FUNCDEF ); |
||
3133 | sqlite3VdbeChangeP5( v, (u8)nFarg ); |
||
3134 | if ( nFarg != 0 ) |
||
3135 | { |
||
3136 | sqlite3ReleaseTempRange( pParse, r1, nFarg ); |
||
3137 | } |
||
3138 | break; |
||
3139 | } |
||
3140 | #if !SQLITE_OMIT_SUBQUERY |
||
3141 | case TK_EXISTS: |
||
3142 | case TK_SELECT: |
||
3143 | { |
||
3144 | testcase( op == TK_EXISTS ); |
||
3145 | testcase( op == TK_SELECT ); |
||
3146 | inReg = sqlite3CodeSubselect( pParse, pExpr, 0, false ); |
||
3147 | break; |
||
3148 | } |
||
3149 | case TK_IN: |
||
3150 | { |
||
3151 | int destIfFalse = sqlite3VdbeMakeLabel( v ); |
||
3152 | int destIfNull = sqlite3VdbeMakeLabel( v ); |
||
3153 | sqlite3VdbeAddOp2( v, OP_Null, 0, target ); |
||
3154 | sqlite3ExprCodeIN( pParse, pExpr, destIfFalse, destIfNull ); |
||
3155 | sqlite3VdbeAddOp2( v, OP_Integer, 1, target ); |
||
3156 | sqlite3VdbeResolveLabel( v, destIfFalse ); |
||
3157 | sqlite3VdbeAddOp2( v, OP_AddImm, target, 0 ); |
||
3158 | sqlite3VdbeResolveLabel( v, destIfNull ); |
||
3159 | break; |
||
3160 | } |
||
3161 | #endif //* SQLITE_OMIT_SUBQUERY */ |
||
3162 | |||
3163 | /* |
||
3164 | ** x BETWEEN y AND z |
||
3165 | ** |
||
3166 | ** This is equivalent to |
||
3167 | ** |
||
3168 | ** x>=y AND x<=z |
||
3169 | ** |
||
3170 | ** X is stored in pExpr.pLeft. |
||
3171 | ** Y is stored in pExpr.x.pList.a[0].pExpr. |
||
3172 | ** Z is stored in pExpr.x.pList.a[1].pExpr. |
||
3173 | */ |
||
3174 | case TK_BETWEEN: |
||
3175 | { |
||
3176 | Expr pLeft = pExpr.pLeft; |
||
3177 | ExprList_item pLItem = pExpr.x.pList.a[0]; |
||
3178 | Expr pRight = pLItem.pExpr; |
||
3179 | r1 = sqlite3ExprCodeTemp( pParse, pLeft, ref regFree1 ); |
||
3180 | r2 = sqlite3ExprCodeTemp( pParse, pRight, ref regFree2 ); |
||
3181 | testcase( regFree1 == 0 ); |
||
3182 | testcase( regFree2 == 0 ); |
||
3183 | r3 = sqlite3GetTempReg( pParse ); |
||
3184 | r4 = sqlite3GetTempReg( pParse ); |
||
3185 | codeCompare( pParse, pLeft, pRight, OP_Ge, |
||
3186 | r1, r2, r3, SQLITE_STOREP2 ); |
||
3187 | pLItem = pExpr.x.pList.a[1];// pLItem++; |
||
3188 | pRight = pLItem.pExpr; |
||
3189 | sqlite3ReleaseTempReg( pParse, regFree2 ); |
||
3190 | r2 = sqlite3ExprCodeTemp( pParse, pRight, ref regFree2 ); |
||
3191 | testcase( regFree2 == 0 ); |
||
3192 | codeCompare( pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2 ); |
||
3193 | sqlite3VdbeAddOp3( v, OP_And, r3, r4, target ); |
||
3194 | sqlite3ReleaseTempReg( pParse, r3 ); |
||
3195 | sqlite3ReleaseTempReg( pParse, r4 ); |
||
3196 | break; |
||
3197 | } |
||
3198 | case TK_UPLUS: |
||
3199 | { |
||
3200 | inReg = sqlite3ExprCodeTarget( pParse, pExpr.pLeft, target ); |
||
3201 | break; |
||
3202 | } |
||
3203 | case TK_TRIGGER: |
||
3204 | { |
||
3205 | /* If the opcode is TK_TRIGGER, then the expression is a reference |
||
3206 | ** to a column in the new.* or old.* pseudo-tables available to |
||
3207 | ** trigger programs. In this case Expr.iTable is set to 1 for the |
||
3208 | ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
||
3209 | ** is set to the column of the pseudo-table to read, or to -1 to |
||
3210 | ** read the rowid field. |
||
3211 | ** |
||
3212 | ** The expression is implemented using an OP_Param opcode. The p1 |
||
3213 | ** parameter is set to 0 for an old.rowid reference, or to (i+1) |
||
3214 | ** to reference another column of the old.* pseudo-table, where |
||
3215 | ** i is the index of the column. For a new.rowid reference, p1 is |
||
3216 | ** set to (n+1), where n is the number of columns in each pseudo-table. |
||
3217 | ** For a reference to any other column in the new.* pseudo-table, p1 |
||
3218 | ** is set to (n+2+i), where n and i are as defined previously. For |
||
3219 | ** example, if the table on which triggers are being fired is |
||
3220 | ** declared as: |
||
3221 | ** |
||
3222 | ** CREATE TABLE t1(a, b); |
||
3223 | ** |
||
3224 | ** Then p1 is interpreted as follows: |
||
3225 | ** |
||
3226 | ** p1==0 . old.rowid p1==3 . new.rowid |
||
3227 | ** p1==1 . old.a p1==4 . new.a |
||
3228 | ** p1==2 . old.b p1==5 . new.b |
||
3229 | */ |
||
3230 | Table pTab = pExpr.pTab; |
||
3231 | int p1 = pExpr.iTable * ( pTab.nCol + 1 ) + 1 + pExpr.iColumn; |
||
3232 | |||
3233 | Debug.Assert( pExpr.iTable == 0 || pExpr.iTable == 1 ); |
||
3234 | Debug.Assert( pExpr.iColumn >= -1 && pExpr.iColumn < pTab.nCol ); |
||
3235 | Debug.Assert( pTab.iPKey < 0 || pExpr.iColumn != pTab.iPKey ); |
||
3236 | Debug.Assert( p1 >= 0 && p1 < ( pTab.nCol * 2 + 2 ) ); |
||
3237 | |||
3238 | sqlite3VdbeAddOp2( v, OP_Param, p1, target ); |
||
3239 | VdbeComment( v, "%s.%s -> $%d", |
||
3240 | ( pExpr.iTable != 0 ? "new" : "old" ), |
||
3241 | ( pExpr.iColumn < 0 ? "rowid" : pExpr.pTab.aCol[pExpr.iColumn].zName ), |
||
3242 | target |
||
3243 | ); |
||
3244 | |||
3245 | /* If the column has REAL affinity, it may currently be stored as an |
||
3246 | ** integer. Use OP_RealAffinity to make sure it is really real. */ |
||
3247 | if ( pExpr.iColumn >= 0 |
||
3248 | && pTab.aCol[pExpr.iColumn].affinity == SQLITE_AFF_REAL |
||
3249 | ) |
||
3250 | { |
||
3251 | sqlite3VdbeAddOp1( v, OP_RealAffinity, target ); |
||
3252 | } |
||
3253 | break; |
||
3254 | } |
||
3255 | |||
3256 | /* |
||
3257 | ** Form A: |
||
3258 | ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
||
3259 | ** |
||
3260 | ** Form B: |
||
3261 | ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
||
3262 | ** |
||
3263 | ** Form A is can be transformed into the equivalent form B as follows: |
||
3264 | ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... |
||
3265 | ** WHEN x=eN THEN rN ELSE y END |
||
3266 | ** |
||
3267 | ** X (if it exists) is in pExpr.pLeft. |
||
3268 | ** Y is in pExpr.pRight. The Y is also optional. If there is no |
||
3269 | ** ELSE clause and no other term matches, then the result of the |
||
3270 | ** exprssion is NULL. |
||
3271 | ** Ei is in pExpr.x.pList.a[i*2] and Ri is pExpr.x.pList.a[i*2+1]. |
||
3272 | ** |
||
3273 | ** The result of the expression is the Ri for the first matching Ei, |
||
3274 | ** or if there is no matching Ei, the ELSE term Y, or if there is |
||
3275 | ** no ELSE term, NULL. |
||
3276 | */ |
||
3277 | default: |
||
3278 | { |
||
3279 | Debug.Assert( op == TK_CASE ); |
||
3280 | int endLabel; /* GOTO label for end of CASE stmt */ |
||
3281 | int nextCase; /* GOTO label for next WHEN clause */ |
||
3282 | int nExpr; /* 2x number of WHEN terms */ |
||
3283 | int i; /* Loop counter */ |
||
3284 | ExprList pEList; /* List of WHEN terms */ |
||
3285 | ExprList_item[] aListelem; /* Array of WHEN terms */ |
||
3286 | Expr opCompare = new Expr(); /* The X==Ei expression */ |
||
3287 | Expr cacheX; /* Cached expression X */ |
||
3288 | Expr pX; /* The X expression */ |
||
3289 | Expr pTest = null; /* X==Ei (form A) or just Ei (form B) */ |
||
3290 | #if !NDEBUG |
||
3291 | int iCacheLevel = pParse.iCacheLevel; |
||
3292 | //VVA_ONLY( int iCacheLevel = pParse.iCacheLevel; ) |
||
3293 | #endif |
||
3294 | Debug.Assert( !ExprHasProperty( pExpr, EP_xIsSelect ) && pExpr.x.pList != null ); |
||
3295 | Debug.Assert( ( pExpr.x.pList.nExpr % 2 ) == 0 ); |
||
3296 | Debug.Assert( pExpr.x.pList.nExpr > 0 ); |
||
3297 | pEList = pExpr.x.pList; |
||
3298 | aListelem = pEList.a; |
||
3299 | nExpr = pEList.nExpr; |
||
3300 | endLabel = sqlite3VdbeMakeLabel( v ); |
||
3301 | if ( ( pX = pExpr.pLeft ) != null ) |
||
3302 | { |
||
3303 | cacheX = pX; |
||
3304 | testcase( pX.op == TK_COLUMN ); |
||
3305 | testcase( pX.op == TK_REGISTER ); |
||
3306 | cacheX.iTable = sqlite3ExprCodeTemp( pParse, pX, ref regFree1 ); |
||
3307 | testcase( regFree1 == 0 ); |
||
3308 | cacheX.op = TK_REGISTER; |
||
3309 | opCompare.op = TK_EQ; |
||
3310 | opCompare.pLeft = cacheX; |
||
3311 | pTest = opCompare; |
||
3312 | /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: |
||
3313 | ** The value in regFree1 might get SCopy-ed into the file result. |
||
3314 | ** So make sure that the regFree1 register is not reused for other |
||
3315 | ** purposes and possibly overwritten. */ |
||
3316 | regFree1 = 0; |
||
3317 | } |
||
3318 | for ( i = 0; i < nExpr; i = i + 2 ) |
||
3319 | { |
||
3320 | sqlite3ExprCachePush( pParse ); |
||
3321 | if ( pX != null ) |
||
3322 | { |
||
3323 | Debug.Assert( pTest != null ); |
||
3324 | opCompare.pRight = aListelem[i].pExpr; |
||
3325 | } |
||
3326 | else |
||
3327 | { |
||
3328 | pTest = aListelem[i].pExpr; |
||
3329 | } |
||
3330 | nextCase = sqlite3VdbeMakeLabel( v ); |
||
3331 | testcase( pTest.op == TK_COLUMN ); |
||
3332 | sqlite3ExprIfFalse( pParse, pTest, nextCase, SQLITE_JUMPIFNULL ); |
||
3333 | testcase( aListelem[i + 1].pExpr.op == TK_COLUMN ); |
||
3334 | testcase( aListelem[i + 1].pExpr.op == TK_REGISTER ); |
||
3335 | sqlite3ExprCode( pParse, aListelem[i + 1].pExpr, target ); |
||
3336 | sqlite3VdbeAddOp2( v, OP_Goto, 0, endLabel ); |
||
3337 | sqlite3ExprCachePop( pParse, 1 ); |
||
3338 | sqlite3VdbeResolveLabel( v, nextCase ); |
||
3339 | } |
||
3340 | if ( pExpr.pRight != null ) |
||
3341 | { |
||
3342 | sqlite3ExprCachePush( pParse ); |
||
3343 | sqlite3ExprCode( pParse, pExpr.pRight, target ); |
||
3344 | sqlite3ExprCachePop( pParse, 1 ); |
||
3345 | } |
||
3346 | else |
||
3347 | { |
||
3348 | sqlite3VdbeAddOp2( v, OP_Null, 0, target ); |
||
3349 | } |
||
3350 | #if !NDEBUG |
||
3351 | Debug.Assert( /* db.mallocFailed != 0 || */ pParse.nErr > 0 |
||
3352 | || pParse.iCacheLevel == iCacheLevel ); |
||
3353 | #endif |
||
3354 | sqlite3VdbeResolveLabel( v, endLabel ); |
||
3355 | break; |
||
3356 | } |
||
3357 | #if !SQLITE_OMIT_TRIGGER |
||
3358 | case TK_RAISE: |
||
3359 | { |
||
3360 | Debug.Assert( pExpr.affinity == OE_Rollback |
||
3361 | || pExpr.affinity == OE_Abort |
||
3362 | || pExpr.affinity == OE_Fail |
||
3363 | || pExpr.affinity == OE_Ignore |
||
3364 | ); |
||
3365 | if ( null == pParse.pTriggerTab ) |
||
3366 | { |
||
3367 | sqlite3ErrorMsg( pParse, |
||
3368 | "RAISE() may only be used within a trigger-program" ); |
||
3369 | return 0; |
||
3370 | } |
||
3371 | if ( pExpr.affinity == OE_Abort ) |
||
3372 | { |
||
3373 | sqlite3MayAbort( pParse ); |
||
3374 | } |
||
3375 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
3376 | if ( pExpr.affinity == OE_Ignore ) |
||
3377 | { |
||
3378 | sqlite3VdbeAddOp4( |
||
3379 | v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr.u.zToken, 0 ); |
||
3380 | } |
||
3381 | else |
||
3382 | { |
||
3383 | sqlite3HaltConstraint( pParse, pExpr.affinity, pExpr.u.zToken, 0 ); |
||
3384 | } |
||
3385 | |||
3386 | break; |
||
3387 | } |
||
3388 | #endif |
||
3389 | } |
||
3390 | sqlite3ReleaseTempReg( pParse, regFree1 ); |
||
3391 | sqlite3ReleaseTempReg( pParse, regFree2 ); |
||
3392 | return inReg; |
||
3393 | } |
||
3394 | |||
3395 | /* |
||
3396 | ** Generate code to evaluate an expression and store the results |
||
3397 | ** into a register. Return the register number where the results |
||
3398 | ** are stored. |
||
3399 | ** |
||
3400 | ** If the register is a temporary register that can be deallocated, |
||
3401 | ** then write its number into pReg. If the result register is not |
||
3402 | ** a temporary, then set pReg to zero. |
||
3403 | */ |
||
3404 | static int sqlite3ExprCodeTemp( Parse pParse, Expr pExpr, ref int pReg ) |
||
3405 | { |
||
3406 | int r1 = sqlite3GetTempReg( pParse ); |
||
3407 | int r2 = sqlite3ExprCodeTarget( pParse, pExpr, r1 ); |
||
3408 | if ( r2 == r1 ) |
||
3409 | { |
||
3410 | pReg = r1; |
||
3411 | } |
||
3412 | else |
||
3413 | { |
||
3414 | sqlite3ReleaseTempReg( pParse, r1 ); |
||
3415 | pReg = 0; |
||
3416 | } |
||
3417 | return r2; |
||
3418 | } |
||
3419 | |||
3420 | /* |
||
3421 | ** Generate code that will evaluate expression pExpr and store the |
||
3422 | ** results in register target. The results are guaranteed to appear |
||
3423 | ** in register target. |
||
3424 | */ |
||
3425 | static int sqlite3ExprCode( Parse pParse, Expr pExpr, int target ) |
||
3426 | { |
||
3427 | int inReg; |
||
3428 | |||
3429 | Debug.Assert( target > 0 && target <= pParse.nMem ); |
||
3430 | if ( pExpr != null && pExpr.op == TK_REGISTER ) |
||
3431 | { |
||
3432 | sqlite3VdbeAddOp2( pParse.pVdbe, OP_Copy, pExpr.iTable, target ); |
||
3433 | } |
||
3434 | else |
||
3435 | { |
||
3436 | inReg = sqlite3ExprCodeTarget( pParse, pExpr, target ); |
||
3437 | Debug.Assert( pParse.pVdbe != null /* || pParse.db.mallocFailed != 0 */ ); |
||
3438 | if ( inReg != target && pParse.pVdbe != null ) |
||
3439 | { |
||
3440 | sqlite3VdbeAddOp2( pParse.pVdbe, OP_SCopy, inReg, target ); |
||
3441 | } |
||
3442 | } |
||
3443 | return target; |
||
3444 | } |
||
3445 | |||
3446 | /* |
||
3447 | ** Generate code that evalutes the given expression and puts the result |
||
3448 | ** in register target. |
||
3449 | ** |
||
3450 | ** Also make a copy of the expression results into another "cache" register |
||
3451 | ** and modify the expression so that the next time it is evaluated, |
||
3452 | ** the result is a copy of the cache register. |
||
3453 | ** |
||
3454 | ** This routine is used for expressions that are used multiple |
||
3455 | ** times. They are evaluated once and the results of the expression |
||
3456 | ** are reused. |
||
3457 | */ |
||
3458 | static int sqlite3ExprCodeAndCache( Parse pParse, Expr pExpr, int target ) |
||
3459 | { |
||
3460 | Vdbe v = pParse.pVdbe; |
||
3461 | int inReg; |
||
3462 | inReg = sqlite3ExprCode( pParse, pExpr, target ); |
||
3463 | Debug.Assert( target > 0 ); |
||
3464 | /* This routine is called for terms to INSERT or UPDATE. And the only |
||
3465 | ** other place where expressions can be converted into TK_REGISTER is |
||
3466 | ** in WHERE clause processing. So as currently implemented, there is |
||
3467 | ** no way for a TK_REGISTER to exist here. But it seems prudent to |
||
3468 | ** keep the ALWAYS() in case the conditions above change with future |
||
3469 | ** modifications or enhancements. */ |
||
3470 | if ( ALWAYS( pExpr.op != TK_REGISTER ) ) |
||
3471 | { |
||
3472 | int iMem; |
||
3473 | iMem = ++pParse.nMem; |
||
3474 | sqlite3VdbeAddOp2( v, OP_Copy, inReg, iMem ); |
||
3475 | pExpr.iTable = iMem; |
||
3476 | pExpr.op2 = pExpr.op; |
||
3477 | pExpr.op = TK_REGISTER; |
||
3478 | } |
||
3479 | return inReg; |
||
3480 | } |
||
3481 | |||
3482 | /* |
||
3483 | ** Return TRUE if pExpr is an constant expression that is appropriate |
||
3484 | ** for factoring out of a loop. Appropriate expressions are: |
||
3485 | ** |
||
3486 | ** * Any expression that evaluates to two or more opcodes. |
||
3487 | ** |
||
3488 | ** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, |
||
3489 | ** or OP_Variable that does not need to be placed in a |
||
3490 | ** specific register. |
||
3491 | ** |
||
3492 | ** There is no point in factoring out single-instruction constant |
||
3493 | ** expressions that need to be placed in a particular register. |
||
3494 | ** We could factor them out, but then we would end up adding an |
||
3495 | ** OP_SCopy instruction to move the value into the correct register |
||
3496 | ** later. We might as well just use the original instruction and |
||
3497 | ** avoid the OP_SCopy. |
||
3498 | */ |
||
3499 | static int isAppropriateForFactoring( Expr p ) |
||
3500 | { |
||
3501 | if ( sqlite3ExprIsConstantNotJoin( p ) == 0 ) |
||
3502 | { |
||
3503 | return 0; /* Only constant expressions are appropriate for factoring */ |
||
3504 | } |
||
3505 | if ( ( p.flags & EP_FixedDest ) == 0 ) |
||
3506 | { |
||
3507 | return 1; /* Any constant without a fixed destination is appropriate */ |
||
3508 | } |
||
3509 | while ( p.op == TK_UPLUS ) |
||
3510 | p = p.pLeft; |
||
3511 | switch ( p.op ) |
||
3512 | { |
||
3513 | #if !SQLITE_OMIT_BLOB_LITERAL |
||
3514 | case TK_BLOB: |
||
3515 | #endif |
||
3516 | case TK_VARIABLE: |
||
3517 | case TK_INTEGER: |
||
3518 | case TK_FLOAT: |
||
3519 | case TK_NULL: |
||
3520 | case TK_STRING: |
||
3521 | { |
||
3522 | testcase( p.op == TK_BLOB ); |
||
3523 | testcase( p.op == TK_VARIABLE ); |
||
3524 | testcase( p.op == TK_INTEGER ); |
||
3525 | testcase( p.op == TK_FLOAT ); |
||
3526 | testcase( p.op == TK_NULL ); |
||
3527 | testcase( p.op == TK_STRING ); |
||
3528 | /* Single-instruction constants with a fixed destination are |
||
3529 | ** better done in-line. If we factor them, they will just end |
||
3530 | ** up generating an OP_SCopy to move the value to the destination |
||
3531 | ** register. */ |
||
3532 | return 0; |
||
3533 | } |
||
3534 | case TK_UMINUS: |
||
3535 | { |
||
3536 | if ( p.pLeft.op == TK_FLOAT || p.pLeft.op == TK_INTEGER ) |
||
3537 | { |
||
3538 | return 0; |
||
3539 | } |
||
3540 | break; |
||
3541 | } |
||
3542 | default: |
||
3543 | { |
||
3544 | break; |
||
3545 | } |
||
3546 | } |
||
3547 | return 1; |
||
3548 | } |
||
3549 | |||
3550 | /* |
||
3551 | ** If pExpr is a constant expression that is appropriate for |
||
3552 | ** factoring out of a loop, then evaluate the expression |
||
3553 | ** into a register and convert the expression into a TK_REGISTER |
||
3554 | ** expression. |
||
3555 | */ |
||
3556 | static int evalConstExpr( Walker pWalker, ref Expr pExpr ) |
||
3557 | { |
||
3558 | Parse pParse = pWalker.pParse; |
||
3559 | switch ( pExpr.op ) |
||
3560 | { |
||
3561 | case TK_IN: |
||
3562 | case TK_REGISTER: |
||
3563 | { |
||
3564 | return WRC_Prune; |
||
3565 | } |
||
3566 | case TK_FUNCTION: |
||
3567 | case TK_AGG_FUNCTION: |
||
3568 | case TK_CONST_FUNC: |
||
3569 | { |
||
3570 | /* The arguments to a function have a fixed destination. |
||
3571 | ** Mark them this way to avoid generated unneeded OP_SCopy |
||
3572 | ** instructions. |
||
3573 | */ |
||
3574 | ExprList pList = pExpr.x.pList; |
||
3575 | Debug.Assert( !ExprHasProperty( pExpr, EP_xIsSelect ) ); |
||
3576 | if ( pList != null ) |
||
3577 | { |
||
3578 | int i = pList.nExpr; |
||
3579 | ExprList_item pItem;//= pList.a; |
||
3580 | for ( ; i > 0; i-- ) |
||
3581 | {//, pItem++){ |
||
3582 | pItem = pList.a[pList.nExpr - i]; |
||
3583 | if ( ALWAYS( pItem.pExpr != null ) ) |
||
3584 | pItem.pExpr.flags |= EP_FixedDest; |
||
3585 | } |
||
3586 | } |
||
3587 | break; |
||
3588 | } |
||
3589 | } |
||
3590 | if ( isAppropriateForFactoring( pExpr ) != 0 ) |
||
3591 | { |
||
3592 | int r1 = ++pParse.nMem; |
||
3593 | int r2; |
||
3594 | r2 = sqlite3ExprCodeTarget( pParse, pExpr, r1 ); |
||
3595 | if ( NEVER( r1 != r2 ) ) |
||
3596 | sqlite3ReleaseTempReg( pParse, r1 ); |
||
3597 | pExpr.op2 = pExpr.op; |
||
3598 | pExpr.op = TK_REGISTER; |
||
3599 | pExpr.iTable = r2; |
||
3600 | return WRC_Prune; |
||
3601 | } |
||
3602 | return WRC_Continue; |
||
3603 | } |
||
3604 | |||
3605 | /* |
||
3606 | ** Preevaluate constant subexpressions within pExpr and store the |
||
3607 | ** results in registers. Modify pExpr so that the constant subexpresions |
||
3608 | ** are TK_REGISTER opcodes that refer to the precomputed values. |
||
3609 | ** |
||
3610 | ** This routine is a no-op if the jump to the cookie-check code has |
||
3611 | ** already occur. Since the cookie-check jump is generated prior to |
||
3612 | ** any other serious processing, this check ensures that there is no |
||
3613 | ** way to accidently bypass the constant initializations. |
||
3614 | ** |
||
3615 | ** This routine is also a no-op if the SQLITE_FactorOutConst optimization |
||
3616 | ** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS) |
||
3617 | ** interface. This allows test logic to verify that the same answer is |
||
3618 | ** obtained for queries regardless of whether or not constants are |
||
3619 | ** precomputed into registers or if they are inserted in-line. |
||
3620 | */ |
||
3621 | static void sqlite3ExprCodeConstants( Parse pParse, Expr pExpr ) |
||
3622 | { |
||
3623 | Walker w; |
||
3624 | if ( pParse.cookieGoto != 0 ) |
||
3625 | return; |
||
3626 | if ( ( pParse.db.flags & SQLITE_FactorOutConst ) != 0 ) |
||
3627 | return; |
||
3628 | w = new Walker(); |
||
3629 | w.xExprCallback = (dxExprCallback)evalConstExpr; |
||
3630 | w.xSelectCallback = null; |
||
3631 | w.pParse = pParse; |
||
3632 | sqlite3WalkExpr( w, ref pExpr ); |
||
3633 | } |
||
3634 | |||
3635 | /* |
||
3636 | ** Generate code that pushes the value of every element of the given |
||
3637 | ** expression list into a sequence of registers beginning at target. |
||
3638 | ** |
||
3639 | ** Return the number of elements evaluated. |
||
3640 | */ |
||
3641 | static int sqlite3ExprCodeExprList( |
||
3642 | Parse pParse, /* Parsing context */ |
||
3643 | ExprList pList, /* The expression list to be coded */ |
||
3644 | int target, /* Where to write results */ |
||
3645 | bool doHardCopy /* Make a hard copy of every element */ |
||
3646 | ) |
||
3647 | { |
||
3648 | ExprList_item pItem; |
||
3649 | int i, n; |
||
3650 | Debug.Assert( pList != null ); |
||
3651 | Debug.Assert( target > 0 ); |
||
3652 | Debug.Assert( pParse.pVdbe != null ); /* Never gets this far otherwise */ |
||
3653 | n = pList.nExpr; |
||
3654 | for ( i = 0; i < n; i++ )// pItem++) |
||
3655 | { |
||
3656 | pItem = pList.a[i]; |
||
3657 | Expr pExpr = pItem.pExpr; |
||
3658 | int inReg = sqlite3ExprCodeTarget( pParse, pExpr, target + i ); |
||
3659 | if ( inReg != target + i ) |
||
3660 | { |
||
3661 | sqlite3VdbeAddOp2( pParse.pVdbe, doHardCopy ? OP_Copy : OP_SCopy, |
||
3662 | inReg, target + i ); |
||
3663 | } |
||
3664 | } |
||
3665 | return n; |
||
3666 | } |
||
3667 | |||
3668 | |||
3669 | /* |
||
3670 | ** Generate code for a BETWEEN operator. |
||
3671 | ** |
||
3672 | ** x BETWEEN y AND z |
||
3673 | ** |
||
3674 | ** The above is equivalent to |
||
3675 | ** |
||
3676 | ** x>=y AND x<=z |
||
3677 | ** |
||
3678 | ** Code it as such, taking care to do the common subexpression |
||
3679 | ** elementation of x. |
||
3680 | */ |
||
3681 | static void exprCodeBetween( |
||
3682 | Parse pParse, /* Parsing and code generating context */ |
||
3683 | Expr pExpr, /* The BETWEEN expression */ |
||
3684 | int dest, /* Jump here if the jump is taken */ |
||
3685 | int jumpIfTrue, /* Take the jump if the BETWEEN is true */ |
||
3686 | int jumpIfNull /* Take the jump if the BETWEEN is NULL */ |
||
3687 | ) |
||
3688 | { |
||
3689 | Expr exprAnd = new Expr(); /* The AND operator in x>=y AND x<=z */ |
||
3690 | Expr compLeft = new Expr(); /* The x>=y term */ |
||
3691 | Expr compRight = new Expr(); /* The x<=z term */ |
||
3692 | Expr exprX; /* The x subexpression */ |
||
3693 | int regFree1 = 0; /* Temporary use register */ |
||
3694 | |||
3695 | Debug.Assert( !ExprHasProperty( pExpr, EP_xIsSelect ) ); |
||
3696 | exprX = pExpr.pLeft.Copy(); |
||
3697 | exprAnd.op = TK_AND; |
||
3698 | exprAnd.pLeft = compLeft; |
||
3699 | exprAnd.pRight = compRight; |
||
3700 | compLeft.op = TK_GE; |
||
3701 | compLeft.pLeft = exprX; |
||
3702 | compLeft.pRight = pExpr.x.pList.a[0].pExpr; |
||
3703 | compRight.op = TK_LE; |
||
3704 | compRight.pLeft = exprX; |
||
3705 | compRight.pRight = pExpr.x.pList.a[1].pExpr; |
||
3706 | exprX.iTable = sqlite3ExprCodeTemp( pParse, exprX, ref regFree1 ); |
||
3707 | exprX.op = TK_REGISTER; |
||
3708 | if ( jumpIfTrue != 0 ) |
||
3709 | { |
||
3710 | sqlite3ExprIfTrue( pParse, exprAnd, dest, jumpIfNull ); |
||
3711 | } |
||
3712 | else |
||
3713 | { |
||
3714 | sqlite3ExprIfFalse( pParse, exprAnd, dest, jumpIfNull ); |
||
3715 | } |
||
3716 | sqlite3ReleaseTempReg( pParse, regFree1 ); |
||
3717 | |||
3718 | /* Ensure adequate test coverage */ |
||
3719 | testcase( jumpIfTrue == 0 && jumpIfNull == 0 && regFree1 == 0 ); |
||
3720 | testcase( jumpIfTrue == 0 && jumpIfNull == 0 && regFree1 != 0 ); |
||
3721 | testcase( jumpIfTrue == 0 && jumpIfNull != 0 && regFree1 == 0 ); |
||
3722 | testcase( jumpIfTrue == 0 && jumpIfNull != 0 && regFree1 != 0 ); |
||
3723 | testcase( jumpIfTrue != 0 && jumpIfNull == 0 && regFree1 == 0 ); |
||
3724 | testcase( jumpIfTrue != 0 && jumpIfNull == 0 && regFree1 != 0 ); |
||
3725 | testcase( jumpIfTrue != 0 && jumpIfNull != 0 && regFree1 == 0 ); |
||
3726 | testcase( jumpIfTrue != 0 && jumpIfNull != 0 && regFree1 != 0 ); |
||
3727 | } |
||
3728 | /* |
||
3729 | ** Generate code for a boolean expression such that a jump is made |
||
3730 | ** to the label "dest" if the expression is true but execution |
||
3731 | ** continues straight thru if the expression is false. |
||
3732 | ** |
||
3733 | ** If the expression evaluates to NULL (neither true nor false), then |
||
3734 | ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. |
||
3735 | ** |
||
3736 | ** This code depends on the fact that certain token values (ex: TK_EQ) |
||
3737 | ** are the same as opcode values (ex: OP_Eq) that implement the corresponding |
||
3738 | ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in |
||
3739 | ** the make process cause these values to align. Assert()s in the code |
||
3740 | ** below verify that the numbers are aligned correctly. |
||
3741 | */ |
||
3742 | static void sqlite3ExprIfTrue( Parse pParse, Expr pExpr, int dest, int jumpIfNull ) |
||
3743 | { |
||
3744 | Vdbe v = pParse.pVdbe; |
||
3745 | int op = 0; |
||
3746 | int regFree1 = 0; |
||
3747 | int regFree2 = 0; |
||
3748 | int r1 = 0, r2 = 0; |
||
3749 | |||
3750 | Debug.Assert( jumpIfNull == SQLITE_JUMPIFNULL || jumpIfNull == 0 ); |
||
3751 | if ( NEVER( v == null ) ) |
||
3752 | return; /* Existance of VDBE checked by caller */ |
||
3753 | if ( NEVER( pExpr == null ) ) |
||
3754 | return; /* No way this can happen */ |
||
3755 | op = pExpr.op; |
||
3756 | switch ( op ) |
||
3757 | { |
||
3758 | case TK_AND: |
||
3759 | { |
||
3760 | int d2 = sqlite3VdbeMakeLabel( v ); |
||
3761 | testcase( jumpIfNull == 0 ); |
||
3762 | sqlite3ExprCachePush( pParse ); |
||
3763 | sqlite3ExprIfFalse( pParse, pExpr.pLeft, d2, jumpIfNull ^ SQLITE_JUMPIFNULL ); |
||
3764 | sqlite3ExprIfTrue( pParse, pExpr.pRight, dest, jumpIfNull ); |
||
3765 | sqlite3VdbeResolveLabel( v, d2 ); |
||
3766 | sqlite3ExprCachePop( pParse, 1 ); |
||
3767 | break; |
||
3768 | } |
||
3769 | case TK_OR: |
||
3770 | { |
||
3771 | testcase( jumpIfNull == 0 ); |
||
3772 | sqlite3ExprIfTrue( pParse, pExpr.pLeft, dest, jumpIfNull ); |
||
3773 | sqlite3ExprIfTrue( pParse, pExpr.pRight, dest, jumpIfNull ); |
||
3774 | break; |
||
3775 | } |
||
3776 | case TK_NOT: |
||
3777 | { |
||
3778 | testcase( jumpIfNull == 0 ); |
||
3779 | sqlite3ExprIfFalse( pParse, pExpr.pLeft, dest, jumpIfNull ); |
||
3780 | break; |
||
3781 | } |
||
3782 | case TK_LT: |
||
3783 | case TK_LE: |
||
3784 | case TK_GT: |
||
3785 | case TK_GE: |
||
3786 | case TK_NE: |
||
3787 | case TK_EQ: |
||
3788 | { |
||
3789 | Debug.Assert( TK_LT == OP_Lt ); |
||
3790 | Debug.Assert( TK_LE == OP_Le ); |
||
3791 | Debug.Assert( TK_GT == OP_Gt ); |
||
3792 | Debug.Assert( TK_GE == OP_Ge ); |
||
3793 | Debug.Assert( TK_EQ == OP_Eq ); |
||
3794 | Debug.Assert( TK_NE == OP_Ne ); |
||
3795 | testcase( op == TK_LT ); |
||
3796 | testcase( op == TK_LE ); |
||
3797 | testcase( op == TK_GT ); |
||
3798 | testcase( op == TK_GE ); |
||
3799 | testcase( op == TK_EQ ); |
||
3800 | testcase( op == TK_NE ); |
||
3801 | testcase( jumpIfNull == 0 ); |
||
3802 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
3803 | r2 = sqlite3ExprCodeTemp( pParse, pExpr.pRight, ref regFree2 ); |
||
3804 | codeCompare( pParse, pExpr.pLeft, pExpr.pRight, op, |
||
3805 | r1, r2, dest, jumpIfNull ); |
||
3806 | testcase( regFree1 == 0 ); |
||
3807 | testcase( regFree2 == 0 ); |
||
3808 | break; |
||
3809 | } |
||
3810 | case TK_IS: |
||
3811 | case TK_ISNOT: |
||
3812 | { |
||
3813 | testcase( op == TK_IS ); |
||
3814 | testcase( op == TK_ISNOT ); |
||
3815 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
3816 | r2 = sqlite3ExprCodeTemp( pParse, pExpr.pRight, ref regFree2 ); |
||
3817 | op = ( op == TK_IS ) ? TK_EQ : TK_NE; |
||
3818 | codeCompare( pParse, pExpr.pLeft, pExpr.pRight, op, |
||
3819 | r1, r2, dest, SQLITE_NULLEQ ); |
||
3820 | testcase( regFree1 == 0 ); |
||
3821 | testcase( regFree2 == 0 ); |
||
3822 | break; |
||
3823 | } |
||
3824 | case TK_ISNULL: |
||
3825 | case TK_NOTNULL: |
||
3826 | { |
||
3827 | Debug.Assert( TK_ISNULL == OP_IsNull ); |
||
3828 | Debug.Assert( TK_NOTNULL == OP_NotNull ); |
||
3829 | testcase( op == TK_ISNULL ); |
||
3830 | testcase( op == TK_NOTNULL ); |
||
3831 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
3832 | sqlite3VdbeAddOp2( v, op, r1, dest ); |
||
3833 | testcase( regFree1 == 0 ); |
||
3834 | break; |
||
3835 | } |
||
3836 | case TK_BETWEEN: |
||
3837 | { |
||
3838 | testcase( jumpIfNull == 0 ); |
||
3839 | exprCodeBetween( pParse, pExpr, dest, 1, jumpIfNull ); |
||
3840 | break; |
||
3841 | } |
||
3842 | #if SQLITE_OMIT_SUBQUERY |
||
3843 | case TK_IN: |
||
3844 | { |
||
3845 | int destIfFalse = sqlite3VdbeMakeLabel( v ); |
||
3846 | int destIfNull = jumpIfNull != 0 ? dest : destIfFalse; |
||
3847 | sqlite3ExprCodeIN( pParse, pExpr, destIfFalse, destIfNull ); |
||
3848 | sqlite3VdbeAddOp2( v, OP_Goto, 0, dest ); |
||
3849 | sqlite3VdbeResolveLabel( v, destIfFalse ); |
||
3850 | break; |
||
3851 | } |
||
3852 | #endif |
||
3853 | default: |
||
3854 | { |
||
3855 | r1 = sqlite3ExprCodeTemp( pParse, pExpr, ref regFree1 ); |
||
3856 | sqlite3VdbeAddOp3( v, OP_If, r1, dest, jumpIfNull != 0 ? 1 : 0 ); |
||
3857 | testcase( regFree1 == 0 ); |
||
3858 | testcase( jumpIfNull == 0 ); |
||
3859 | break; |
||
3860 | } |
||
3861 | } |
||
3862 | sqlite3ReleaseTempReg( pParse, regFree1 ); |
||
3863 | sqlite3ReleaseTempReg( pParse, regFree2 ); |
||
3864 | } |
||
3865 | |||
3866 | /* |
||
3867 | ** Generate code for a boolean expression such that a jump is made |
||
3868 | ** to the label "dest" if the expression is false but execution |
||
3869 | ** continues straight thru if the expression is true. |
||
3870 | ** |
||
3871 | ** If the expression evaluates to NULL (neither true nor false) then |
||
3872 | ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull |
||
3873 | ** is 0. |
||
3874 | */ |
||
3875 | static void sqlite3ExprIfFalse( Parse pParse, Expr pExpr, int dest, int jumpIfNull ) |
||
3876 | { |
||
3877 | Vdbe v = pParse.pVdbe; |
||
3878 | int op = 0; |
||
3879 | int regFree1 = 0; |
||
3880 | int regFree2 = 0; |
||
3881 | int r1 = 0, r2 = 0; |
||
3882 | |||
3883 | Debug.Assert( jumpIfNull == SQLITE_JUMPIFNULL || jumpIfNull == 0 ); |
||
3884 | if ( NEVER( v == null ) ) |
||
3885 | return; /* Existance of VDBE checked by caller */ |
||
3886 | if ( pExpr == null ) |
||
3887 | return; |
||
3888 | |||
3889 | /* The value of pExpr.op and op are related as follows: |
||
3890 | ** |
||
3891 | ** pExpr.op op |
||
3892 | ** --------- ---------- |
||
3893 | ** TK_ISNULL OP_NotNull |
||
3894 | ** TK_NOTNULL OP_IsNull |
||
3895 | ** TK_NE OP_Eq |
||
3896 | ** TK_EQ OP_Ne |
||
3897 | ** TK_GT OP_Le |
||
3898 | ** TK_LE OP_Gt |
||
3899 | ** TK_GE OP_Lt |
||
3900 | ** TK_LT OP_Ge |
||
3901 | ** |
||
3902 | ** For other values of pExpr.op, op is undefined and unused. |
||
3903 | ** The value of TK_ and OP_ constants are arranged such that we |
||
3904 | ** can compute the mapping above using the following expression. |
||
3905 | ** Assert()s verify that the computation is correct. |
||
3906 | */ |
||
3907 | op = ( ( pExpr.op + ( TK_ISNULL & 1 ) ) ^ 1 ) - ( TK_ISNULL & 1 ); |
||
3908 | |||
3909 | /* Verify correct alignment of TK_ and OP_ constants |
||
3910 | */ |
||
3911 | Debug.Assert( pExpr.op != TK_ISNULL || op == OP_NotNull ); |
||
3912 | Debug.Assert( pExpr.op != TK_NOTNULL || op == OP_IsNull ); |
||
3913 | Debug.Assert( pExpr.op != TK_NE || op == OP_Eq ); |
||
3914 | Debug.Assert( pExpr.op != TK_EQ || op == OP_Ne ); |
||
3915 | Debug.Assert( pExpr.op != TK_LT || op == OP_Ge ); |
||
3916 | Debug.Assert( pExpr.op != TK_LE || op == OP_Gt ); |
||
3917 | Debug.Assert( pExpr.op != TK_GT || op == OP_Le ); |
||
3918 | Debug.Assert( pExpr.op != TK_GE || op == OP_Lt ); |
||
3919 | |||
3920 | switch ( pExpr.op ) |
||
3921 | { |
||
3922 | case TK_AND: |
||
3923 | { |
||
3924 | testcase( jumpIfNull == 0 ); |
||
3925 | sqlite3ExprIfFalse( pParse, pExpr.pLeft, dest, jumpIfNull ); |
||
3926 | sqlite3ExprIfFalse( pParse, pExpr.pRight, dest, jumpIfNull ); |
||
3927 | break; |
||
3928 | } |
||
3929 | case TK_OR: |
||
3930 | { |
||
3931 | int d2 = sqlite3VdbeMakeLabel( v ); |
||
3932 | testcase( jumpIfNull == 0 ); |
||
3933 | sqlite3ExprCachePush( pParse ); |
||
3934 | sqlite3ExprIfTrue( pParse, pExpr.pLeft, d2, jumpIfNull ^ SQLITE_JUMPIFNULL ); |
||
3935 | sqlite3ExprIfFalse( pParse, pExpr.pRight, dest, jumpIfNull ); |
||
3936 | sqlite3VdbeResolveLabel( v, d2 ); |
||
3937 | sqlite3ExprCachePop( pParse, 1 ); |
||
3938 | break; |
||
3939 | } |
||
3940 | case TK_NOT: |
||
3941 | { |
||
3942 | testcase( jumpIfNull == 0 ); |
||
3943 | sqlite3ExprIfTrue( pParse, pExpr.pLeft, dest, jumpIfNull ); |
||
3944 | break; |
||
3945 | } |
||
3946 | case TK_LT: |
||
3947 | case TK_LE: |
||
3948 | case TK_GT: |
||
3949 | case TK_GE: |
||
3950 | case TK_NE: |
||
3951 | case TK_EQ: |
||
3952 | { |
||
3953 | testcase( op == TK_LT ); |
||
3954 | testcase( op == TK_LE ); |
||
3955 | testcase( op == TK_GT ); |
||
3956 | testcase( op == TK_GE ); |
||
3957 | testcase( op == TK_EQ ); |
||
3958 | testcase( op == TK_NE ); |
||
3959 | testcase( jumpIfNull == 0 ); |
||
3960 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
3961 | r2 = sqlite3ExprCodeTemp( pParse, pExpr.pRight, ref regFree2 ); |
||
3962 | codeCompare( pParse, pExpr.pLeft, pExpr.pRight, op, |
||
3963 | r1, r2, dest, jumpIfNull ); |
||
3964 | testcase( regFree1 == 0 ); |
||
3965 | testcase( regFree2 == 0 ); |
||
3966 | break; |
||
3967 | } |
||
3968 | case TK_IS: |
||
3969 | case TK_ISNOT: |
||
3970 | { |
||
3971 | testcase( pExpr.op == TK_IS ); |
||
3972 | testcase( pExpr.op == TK_ISNOT ); |
||
3973 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
3974 | r2 = sqlite3ExprCodeTemp( pParse, pExpr.pRight, ref regFree2 ); |
||
3975 | op = ( pExpr.op == TK_IS ) ? TK_NE : TK_EQ; |
||
3976 | codeCompare( pParse, pExpr.pLeft, pExpr.pRight, op, |
||
3977 | r1, r2, dest, SQLITE_NULLEQ ); |
||
3978 | testcase( regFree1 == 0 ); |
||
3979 | testcase( regFree2 == 0 ); |
||
3980 | break; |
||
3981 | } |
||
3982 | case TK_ISNULL: |
||
3983 | case TK_NOTNULL: |
||
3984 | { |
||
3985 | testcase( op == TK_ISNULL ); |
||
3986 | testcase( op == TK_NOTNULL ); |
||
3987 | r1 = sqlite3ExprCodeTemp( pParse, pExpr.pLeft, ref regFree1 ); |
||
3988 | sqlite3VdbeAddOp2( v, op, r1, dest ); |
||
3989 | testcase( regFree1 == 0 ); |
||
3990 | break; |
||
3991 | } |
||
3992 | case TK_BETWEEN: |
||
3993 | { |
||
3994 | testcase( jumpIfNull == 0 ); |
||
3995 | exprCodeBetween( pParse, pExpr, dest, 0, jumpIfNull ); |
||
3996 | break; |
||
3997 | } |
||
3998 | #if SQLITE_OMIT_SUBQUERY |
||
3999 | case TK_IN: |
||
4000 | { |
||
4001 | if ( jumpIfNull != 0 ) |
||
4002 | { |
||
4003 | sqlite3ExprCodeIN( pParse, pExpr, dest, dest ); |
||
4004 | } |
||
4005 | else |
||
4006 | { |
||
4007 | int destIfNull = sqlite3VdbeMakeLabel( v ); |
||
4008 | sqlite3ExprCodeIN( pParse, pExpr, dest, destIfNull ); |
||
4009 | sqlite3VdbeResolveLabel( v, destIfNull ); |
||
4010 | } |
||
4011 | break; |
||
4012 | } |
||
4013 | #endif |
||
4014 | default: |
||
4015 | { |
||
4016 | r1 = sqlite3ExprCodeTemp( pParse, pExpr, ref regFree1 ); |
||
4017 | sqlite3VdbeAddOp3( v, OP_IfNot, r1, dest, jumpIfNull != 0 ? 1 : 0 ); |
||
4018 | testcase( regFree1 == 0 ); |
||
4019 | testcase( jumpIfNull == 0 ); |
||
4020 | break; |
||
4021 | } |
||
4022 | } |
||
4023 | sqlite3ReleaseTempReg( pParse, regFree1 ); |
||
4024 | sqlite3ReleaseTempReg( pParse, regFree2 ); |
||
4025 | } |
||
4026 | |||
4027 | /* |
||
4028 | ** Do a deep comparison of two expression trees. Return 0 if the two |
||
4029 | ** expressions are completely identical. Return 1 if they differ only |
||
4030 | ** by a COLLATE operator at the top level. Return 2 if there are differences |
||
4031 | ** other than the top-level COLLATE operator. |
||
4032 | ** |
||
4033 | ** Sometimes this routine will return 2 even if the two expressions |
||
4034 | ** really are equivalent. If we cannot prove that the expressions are |
||
4035 | ** identical, we return 2 just to be safe. So if this routine |
||
4036 | ** returns 2, then you do not really know for certain if the two |
||
4037 | ** expressions are the same. But if you get a 0 or 1 return, then you |
||
4038 | ** can be sure the expressions are the same. In the places where |
||
4039 | ** this routine is used, it does not hurt to get an extra 2 - that |
||
4040 | ** just might result in some slightly slower code. But returning |
||
4041 | ** an incorrect 0 or 1 could lead to a malfunction. |
||
4042 | */ |
||
4043 | static int sqlite3ExprCompare( Expr pA, Expr pB ) |
||
4044 | { |
||
4045 | if ( pA == null || pB == null ) |
||
4046 | { |
||
4047 | return pB == pA ? 0 : 2; |
||
4048 | } |
||
4049 | Debug.Assert( !ExprHasAnyProperty( pA, EP_TokenOnly | EP_Reduced ) ); |
||
4050 | Debug.Assert( !ExprHasAnyProperty( pB, EP_TokenOnly | EP_Reduced ) ); |
||
4051 | if ( ExprHasProperty( pA, EP_xIsSelect ) || ExprHasProperty( pB, EP_xIsSelect ) ) |
||
4052 | { |
||
4053 | return 2; |
||
4054 | } |
||
4055 | if ( ( pA.flags & EP_Distinct ) != ( pB.flags & EP_Distinct ) ) |
||
4056 | return 2; |
||
4057 | if ( pA.op != pB.op ) |
||
4058 | return 2; |
||
4059 | if ( sqlite3ExprCompare( pA.pLeft, pB.pLeft ) != 0 ) |
||
4060 | return 2; |
||
4061 | if ( sqlite3ExprCompare( pA.pRight, pB.pRight ) != 0 ) |
||
4062 | return 2; |
||
4063 | if ( sqlite3ExprListCompare( pA.x.pList, pB.x.pList ) != 0 ) |
||
4064 | return 2; |
||
4065 | if ( pA.iTable != pB.iTable || pA.iColumn != pB.iColumn ) |
||
4066 | return 2; |
||
4067 | if ( ExprHasProperty( pA, EP_IntValue ) ) |
||
4068 | { |
||
4069 | if ( !ExprHasProperty( pB, EP_IntValue ) || pA.u.iValue != pB.u.iValue ) |
||
4070 | { |
||
4071 | return 2; |
||
4072 | } |
||
4073 | } |
||
4074 | else if ( pA.op != TK_COLUMN && pA.u.zToken != null ) |
||
4075 | { |
||
4076 | if ( ExprHasProperty( pB, EP_IntValue ) || NEVER( pB.u.zToken == null ) ) |
||
4077 | return 2; |
||
4078 | if ( !pA.u.zToken.Equals( pB.u.zToken ,StringComparison.OrdinalIgnoreCase ) ) |
||
4079 | { |
||
4080 | return 2; |
||
4081 | } |
||
4082 | } |
||
4083 | if ( ( pA.flags & EP_ExpCollate ) != ( pB.flags & EP_ExpCollate ) ) |
||
4084 | return 1; |
||
4085 | if ( ( pA.flags & EP_ExpCollate ) != 0 && pA.pColl != pB.pColl ) |
||
4086 | return 2; |
||
4087 | return 0; |
||
4088 | } |
||
4089 | |||
4090 | /* |
||
4091 | ** Compare two ExprList objects. Return 0 if they are identical and |
||
4092 | ** non-zero if they differ in any way. |
||
4093 | ** |
||
4094 | ** This routine might return non-zero for equivalent ExprLists. The |
||
4095 | ** only consequence will be disabled optimizations. But this routine |
||
4096 | ** must never return 0 if the two ExprList objects are different, or |
||
4097 | ** a malfunction will result. |
||
4098 | ** |
||
4099 | ** Two NULL pointers are considered to be the same. But a NULL pointer |
||
4100 | ** always differs from a non-NULL pointer. |
||
4101 | */ |
||
4102 | static int sqlite3ExprListCompare( ExprList pA, ExprList pB ) |
||
4103 | { |
||
4104 | int i; |
||
4105 | if ( pA == null && pB == null ) |
||
4106 | return 0; |
||
4107 | if ( pA == null || pB == null ) |
||
4108 | return 1; |
||
4109 | if ( pA.nExpr != pB.nExpr ) |
||
4110 | return 1; |
||
4111 | for ( i = 0; i < pA.nExpr; i++ ) |
||
4112 | { |
||
4113 | Expr pExprA = pA.a[i].pExpr; |
||
4114 | Expr pExprB = pB.a[i].pExpr; |
||
4115 | if ( pA.a[i].sortOrder != pB.a[i].sortOrder ) |
||
4116 | return 1; |
||
4117 | if ( sqlite3ExprCompare( pExprA, pExprB ) != 0 ) |
||
4118 | return 1; |
||
4119 | } |
||
4120 | return 0; |
||
4121 | } |
||
4122 | |||
4123 | /* |
||
4124 | ** Add a new element to the pAggInfo.aCol[] array. Return the index of |
||
4125 | ** the new element. Return a negative number if malloc fails. |
||
4126 | */ |
||
4127 | static int addAggInfoColumn( sqlite3 db, AggInfo pInfo ) |
||
4128 | { |
||
4129 | int i = 0; |
||
4130 | pInfo.aCol = sqlite3ArrayAllocate( |
||
4131 | db, |
||
4132 | pInfo.aCol, |
||
4133 | -1,//sizeof(pInfo.aCol[0]), |
||
4134 | 3, |
||
4135 | ref pInfo.nColumn, |
||
4136 | ref pInfo.nColumnAlloc, |
||
4137 | ref i |
||
4138 | ); |
||
4139 | return i; |
||
4140 | } |
||
4141 | |||
4142 | /* |
||
4143 | ** Add a new element to the pAggInfo.aFunc[] array. Return the index of |
||
4144 | ** the new element. Return a negative number if malloc fails. |
||
4145 | */ |
||
4146 | static int addAggInfoFunc( sqlite3 db, AggInfo pInfo ) |
||
4147 | { |
||
4148 | int i = 0; |
||
4149 | pInfo.aFunc = sqlite3ArrayAllocate( |
||
4150 | db, |
||
4151 | pInfo.aFunc, |
||
4152 | -1,//sizeof(pInfo.aFunc[0]), |
||
4153 | 3, |
||
4154 | ref pInfo.nFunc, |
||
4155 | ref pInfo.nFuncAlloc, |
||
4156 | ref i |
||
4157 | ); |
||
4158 | return i; |
||
4159 | } |
||
4160 | |||
4161 | /* |
||
4162 | ** This is the xExprCallback for a tree walker. It is used to |
||
4163 | ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates |
||
4164 | ** for additional information. |
||
4165 | */ |
||
4166 | static int analyzeAggregate( Walker pWalker, ref Expr pExpr ) |
||
4167 | { |
||
4168 | int i; |
||
4169 | NameContext pNC = pWalker.u.pNC; |
||
4170 | Parse pParse = pNC.pParse; |
||
4171 | SrcList pSrcList = pNC.pSrcList; |
||
4172 | AggInfo pAggInfo = pNC.pAggInfo; |
||
4173 | |||
4174 | switch ( pExpr.op ) |
||
4175 | { |
||
4176 | case TK_AGG_COLUMN: |
||
4177 | case TK_COLUMN: |
||
4178 | { |
||
4179 | testcase( pExpr.op == TK_AGG_COLUMN ); |
||
4180 | testcase( pExpr.op == TK_COLUMN ); |
||
4181 | /* Check to see if the column is in one of the tables in the FROM |
||
4182 | ** clause of the aggregate query */ |
||
4183 | if ( ALWAYS( pSrcList != null ) ) |
||
4184 | { |
||
4185 | SrcList_item pItem;// = pSrcList.a; |
||
4186 | for ( i = 0; i < pSrcList.nSrc; i++ ) |
||
4187 | {//, pItem++){ |
||
4188 | pItem = pSrcList.a[i]; |
||
4189 | AggInfo_col pCol; |
||
4190 | Debug.Assert( !ExprHasAnyProperty( pExpr, EP_TokenOnly | EP_Reduced ) ); |
||
4191 | if ( pExpr.iTable == pItem.iCursor ) |
||
4192 | { |
||
4193 | /* If we reach this point, it means that pExpr refers to a table |
||
4194 | ** that is in the FROM clause of the aggregate query. |
||
4195 | ** |
||
4196 | ** Make an entry for the column in pAggInfo.aCol[] if there |
||
4197 | ** is not an entry there already. |
||
4198 | */ |
||
4199 | int k; |
||
4200 | //pCol = pAggInfo.aCol; |
||
4201 | for ( k = 0; k < pAggInfo.nColumn; k++ ) |
||
4202 | {//, pCol++){ |
||
4203 | pCol = pAggInfo.aCol[k]; |
||
4204 | if ( pCol.iTable == pExpr.iTable && |
||
4205 | pCol.iColumn == pExpr.iColumn ) |
||
4206 | { |
||
4207 | break; |
||
4208 | } |
||
4209 | } |
||
4210 | if ( ( k >= pAggInfo.nColumn ) |
||
4211 | && ( k = addAggInfoColumn( pParse.db, pAggInfo ) ) >= 0 |
||
4212 | ) |
||
4213 | { |
||
4214 | pCol = pAggInfo.aCol[k]; |
||
4215 | pCol.pTab = pExpr.pTab; |
||
4216 | pCol.iTable = pExpr.iTable; |
||
4217 | pCol.iColumn = pExpr.iColumn; |
||
4218 | pCol.iMem = ++pParse.nMem; |
||
4219 | pCol.iSorterColumn = -1; |
||
4220 | pCol.pExpr = pExpr; |
||
4221 | if ( pAggInfo.pGroupBy != null ) |
||
4222 | { |
||
4223 | int j, n; |
||
4224 | ExprList pGB = pAggInfo.pGroupBy; |
||
4225 | ExprList_item pTerm;// = pGB.a; |
||
4226 | n = pGB.nExpr; |
||
4227 | for ( j = 0; j < n; j++ ) |
||
4228 | {//, pTerm++){ |
||
4229 | pTerm = pGB.a[j]; |
||
4230 | Expr pE = pTerm.pExpr; |
||
4231 | if ( pE.op == TK_COLUMN && pE.iTable == pExpr.iTable && |
||
4232 | pE.iColumn == pExpr.iColumn ) |
||
4233 | { |
||
4234 | pCol.iSorterColumn = j; |
||
4235 | break; |
||
4236 | } |
||
4237 | } |
||
4238 | } |
||
4239 | if ( pCol.iSorterColumn < 0 ) |
||
4240 | { |
||
4241 | pCol.iSorterColumn = pAggInfo.nSortingColumn++; |
||
4242 | } |
||
4243 | } |
||
4244 | /* There is now an entry for pExpr in pAggInfo.aCol[] (either |
||
4245 | ** because it was there before or because we just created it). |
||
4246 | ** Convert the pExpr to be a TK_AGG_COLUMN referring to that |
||
4247 | ** pAggInfo.aCol[] entry. |
||
4248 | */ |
||
4249 | ExprSetIrreducible( pExpr ); |
||
4250 | pExpr.pAggInfo = pAggInfo; |
||
4251 | pExpr.op = TK_AGG_COLUMN; |
||
4252 | pExpr.iAgg = (short)k; |
||
4253 | break; |
||
4254 | } /* endif pExpr.iTable==pItem.iCursor */ |
||
4255 | } /* end loop over pSrcList */ |
||
4256 | } |
||
4257 | return WRC_Prune; |
||
4258 | } |
||
4259 | case TK_AGG_FUNCTION: |
||
4260 | { |
||
4261 | /* The pNC.nDepth==0 test causes aggregate functions in subqueries |
||
4262 | ** to be ignored */ |
||
4263 | if ( pNC.nDepth == 0 ) |
||
4264 | { |
||
4265 | /* Check to see if pExpr is a duplicate of another aggregate |
||
4266 | ** function that is already in the pAggInfo structure |
||
4267 | */ |
||
4268 | AggInfo_func pItem;// = pAggInfo.aFunc; |
||
4269 | for ( i = 0; i < pAggInfo.nFunc; i++ ) |
||
4270 | {//, pItem++){ |
||
4271 | pItem = pAggInfo.aFunc[i]; |
||
4272 | if ( sqlite3ExprCompare( pItem.pExpr, pExpr ) == 0 ) |
||
4273 | { |
||
4274 | break; |
||
4275 | } |
||
4276 | } |
||
4277 | if ( i >= pAggInfo.nFunc ) |
||
4278 | { |
||
4279 | /* pExpr is original. Make a new entry in pAggInfo.aFunc[] |
||
4280 | */ |
||
4281 | u8 enc = pParse.db.aDbStatic[0].pSchema.enc;// ENC(pParse.db); |
||
4282 | i = addAggInfoFunc( pParse.db, pAggInfo ); |
||
4283 | if ( i >= 0 ) |
||
4284 | { |
||
4285 | Debug.Assert( !ExprHasProperty( pExpr, EP_xIsSelect ) ); |
||
4286 | pItem = pAggInfo.aFunc[i]; |
||
4287 | pItem.pExpr = pExpr; |
||
4288 | pItem.iMem = ++pParse.nMem; |
||
4289 | Debug.Assert( !ExprHasProperty( pExpr, EP_IntValue ) ); |
||
4290 | pItem.pFunc = sqlite3FindFunction( pParse.db, |
||
4291 | pExpr.u.zToken, sqlite3Strlen30( pExpr.u.zToken ), |
||
4292 | pExpr.x.pList != null ? pExpr.x.pList.nExpr : 0, enc, 0 ); |
||
4293 | if ( ( pExpr.flags & EP_Distinct ) != 0 ) |
||
4294 | { |
||
4295 | pItem.iDistinct = pParse.nTab++; |
||
4296 | } |
||
4297 | else |
||
4298 | { |
||
4299 | pItem.iDistinct = -1; |
||
4300 | } |
||
4301 | } |
||
4302 | } |
||
4303 | /* Make pExpr point to the appropriate pAggInfo.aFunc[] entry |
||
4304 | */ |
||
4305 | Debug.Assert( !ExprHasAnyProperty( pExpr, EP_TokenOnly | EP_Reduced ) ); |
||
4306 | ExprSetIrreducible( pExpr ); |
||
4307 | pExpr.iAgg = (short)i; |
||
4308 | pExpr.pAggInfo = pAggInfo; |
||
4309 | return WRC_Prune; |
||
4310 | } |
||
4311 | break; |
||
4312 | } |
||
4313 | } |
||
4314 | return WRC_Continue; |
||
4315 | } |
||
4316 | |||
4317 | static int analyzeAggregatesInSelect( Walker pWalker, Select pSelect ) |
||
4318 | { |
||
4319 | NameContext pNC = pWalker.u.pNC; |
||
4320 | if ( pNC.nDepth == 0 ) |
||
4321 | { |
||
4322 | pNC.nDepth++; |
||
4323 | sqlite3WalkSelect( pWalker, pSelect ); |
||
4324 | pNC.nDepth--; |
||
4325 | return WRC_Prune; |
||
4326 | } |
||
4327 | else |
||
4328 | { |
||
4329 | return WRC_Continue; |
||
4330 | } |
||
4331 | } |
||
4332 | |||
4333 | |||
4334 | /* |
||
4335 | ** Analyze the given expression looking for aggregate functions and |
||
4336 | ** for variables that need to be added to the pParse.aAgg[] array. |
||
4337 | ** Make additional entries to the pParse.aAgg[] array as necessary. |
||
4338 | ** |
||
4339 | ** This routine should only be called after the expression has been |
||
4340 | ** analyzed by sqlite3ResolveExprNames(). |
||
4341 | */ |
||
4342 | static void sqlite3ExprAnalyzeAggregates( NameContext pNC, ref Expr pExpr ) |
||
4343 | { |
||
4344 | Walker w = new Walker(); |
||
4345 | w.xExprCallback = (dxExprCallback)analyzeAggregate; |
||
4346 | w.xSelectCallback = (dxSelectCallback)analyzeAggregatesInSelect; |
||
4347 | w.u.pNC = pNC; |
||
4348 | Debug.Assert( pNC.pSrcList != null ); |
||
4349 | sqlite3WalkExpr( w, ref pExpr ); |
||
4350 | } |
||
4351 | |||
4352 | /* |
||
4353 | ** Call sqlite3ExprAnalyzeAggregates() for every expression in an |
||
4354 | ** expression list. Return the number of errors. |
||
4355 | ** |
||
4356 | ** If an error is found, the analysis is cut short. |
||
4357 | */ |
||
4358 | static void sqlite3ExprAnalyzeAggList( NameContext pNC, ExprList pList ) |
||
4359 | { |
||
4360 | ExprList_item pItem; |
||
4361 | int i; |
||
4362 | if ( pList != null ) |
||
4363 | { |
||
4364 | for ( i = 0; i < pList.nExpr; i++ )//, pItem++) |
||
4365 | { |
||
4366 | pItem = pList.a[i]; |
||
4367 | sqlite3ExprAnalyzeAggregates( pNC, ref pItem.pExpr ); |
||
4368 | } |
||
4369 | } |
||
4370 | } |
||
4371 | |||
4372 | /* |
||
4373 | ** Allocate a single new register for use to hold some intermediate result. |
||
4374 | */ |
||
4375 | static int sqlite3GetTempReg( Parse pParse ) |
||
4376 | { |
||
4377 | if ( pParse.nTempReg == 0 ) |
||
4378 | { |
||
4379 | return ++pParse.nMem; |
||
4380 | } |
||
4381 | return pParse.aTempReg[--pParse.nTempReg]; |
||
4382 | } |
||
4383 | |||
4384 | /* |
||
4385 | ** Deallocate a register, making available for reuse for some other |
||
4386 | ** purpose. |
||
4387 | ** |
||
4388 | ** If a register is currently being used by the column cache, then |
||
4389 | ** the dallocation is deferred until the column cache line that uses |
||
4390 | ** the register becomes stale. |
||
4391 | */ |
||
4392 | static void sqlite3ReleaseTempReg( Parse pParse, int iReg ) |
||
4393 | { |
||
4394 | if ( iReg != 0 && pParse.nTempReg < ArraySize( pParse.aTempReg ) ) |
||
4395 | { |
||
4396 | int i; |
||
4397 | yColCache p; |
||
4398 | for ( i = 0; i < SQLITE_N_COLCACHE; i++ )//p=pParse.aColCache... p++) |
||
4399 | { |
||
4400 | p = pParse.aColCache[i]; |
||
4401 | if ( p.iReg == iReg ) |
||
4402 | { |
||
4403 | p.tempReg = 1; |
||
4404 | return; |
||
4405 | } |
||
4406 | } |
||
4407 | pParse.aTempReg[pParse.nTempReg++] = iReg; |
||
4408 | } |
||
4409 | } |
||
4410 | |||
4411 | /* |
||
4412 | ** Allocate or deallocate a block of nReg consecutive registers |
||
4413 | */ |
||
4414 | static int sqlite3GetTempRange( Parse pParse, int nReg ) |
||
4415 | { |
||
4416 | int i, n; |
||
4417 | i = pParse.iRangeReg; |
||
4418 | n = pParse.nRangeReg; |
||
4419 | if ( nReg <= n ) |
||
4420 | { |
||
4421 | //Debug.Assert( 1 == usedAsColumnCache( pParse, i, i + n - 1 ) ); |
||
4422 | pParse.iRangeReg += nReg; |
||
4423 | pParse.nRangeReg -= nReg; |
||
4424 | } |
||
4425 | else |
||
4426 | { |
||
4427 | i = pParse.nMem + 1; |
||
4428 | pParse.nMem += nReg; |
||
4429 | } |
||
4430 | return i; |
||
4431 | } |
||
4432 | static void sqlite3ReleaseTempRange( Parse pParse, int iReg, int nReg ) |
||
4433 | { |
||
4434 | sqlite3ExprCacheRemove( pParse, iReg, nReg ); |
||
4435 | if ( nReg > pParse.nRangeReg ) |
||
4436 | { |
||
4437 | pParse.nRangeReg = nReg; |
||
4438 | pParse.iRangeReg = iReg; |
||
4439 | } |
||
4440 | } |
||
4441 | } |
||
4442 | } |