monotone

monotone Mtn Source Tree

Root/sqlite/expr.c

1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains routines used for analyzing expressions and
13** for generating VDBE code that evaluates expressions in SQLite.
14**
15** $Id: expr.c,v 1.1 2003/08/05 23:03:07 graydon Exp $
16*/
17#include "sqliteInt.h"
18#include <ctype.h>
19
20/*
21** Construct a new expression node and return a pointer to it. Memory
22** for this node is obtained from sqliteMalloc(). The calling function
23** is responsible for making sure the node eventually gets freed.
24*/
25Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
26 Expr *pNew;
27 pNew = sqliteMalloc( sizeof(Expr) );
28 if( pNew==0 ){
29 sqliteExprDelete(pLeft);
30 sqliteExprDelete(pRight);
31 return 0;
32 }
33 pNew->op = op;
34 pNew->pLeft = pLeft;
35 pNew->pRight = pRight;
36 if( pToken ){
37 assert( pToken->dyn==0 );
38 pNew->token = *pToken;
39 pNew->span = *pToken;
40 }else{
41 pNew->token.dyn = 0;
42 pNew->token.z = 0;
43 pNew->token.n = 0;
44 if( pLeft && pRight ){
45 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
46 }else{
47 pNew->span = pNew->token;
48 }
49 }
50 return pNew;
51}
52
53/*
54** Set the Expr.span field of the given expression to span all
55** text between the two given tokens.
56*/
57void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
58 if( pExpr && pRight && pRight->z && pLeft && pLeft->z ){
59 if( pLeft->dyn==0 && pRight->dyn==0 ){
60 pExpr->span.z = pLeft->z;
61 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
62 }else{
63 pExpr->span.z = 0;
64 pExpr->span.n = 0;
65 pExpr->span.dyn = 0;
66 }
67 }
68}
69
70/*
71** Construct a new expression node for a function with multiple
72** arguments.
73*/
74Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
75 Expr *pNew;
76 pNew = sqliteMalloc( sizeof(Expr) );
77 if( pNew==0 ){
78 sqliteExprListDelete(pList);
79 return 0;
80 }
81 pNew->op = TK_FUNCTION;
82 pNew->pList = pList;
83 pNew->token.dyn = 0;
84 if( pToken ){
85 assert( pToken->dyn==0 );
86 pNew->token = *pToken;
87 }else{
88 pNew->token.z = 0;
89 pNew->token.n = 0;
90 }
91 pNew->span = pNew->token;
92 return pNew;
93}
94
95/*
96** Recursively delete an expression tree.
97*/
98void sqliteExprDelete(Expr *p){
99 if( p==0 ) return;
100 if( p->span.dyn && p->span.z ) sqliteFree((char*)p->span.z);
101 if( p->token.dyn && p->token.z ) sqliteFree((char*)p->token.z);
102 if( p->pLeft ) sqliteExprDelete(p->pLeft);
103 if( p->pRight ) sqliteExprDelete(p->pRight);
104 if( p->pList ) sqliteExprListDelete(p->pList);
105 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
106 sqliteFree(p);
107}
108
109
110/*
111** The following group of routines make deep copies of expressions,
112** expression lists, ID lists, and select statements. The copies can
113** be deleted (by being passed to their respective ...Delete() routines)
114** without effecting the originals.
115**
116** The expression list, ID, and source lists return by sqliteExprListDup(),
117** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
118** by subsequent calls to sqlite*ListAppend() routines.
119**
120** Any tables that the SrcList might point to are not duplicated.
121*/
122Expr *sqliteExprDup(Expr *p){
123 Expr *pNew;
124 if( p==0 ) return 0;
125 pNew = sqliteMallocRaw( sizeof(*p) );
126 if( pNew==0 ) return 0;
127 memcpy(pNew, p, sizeof(*pNew));
128 if( p->token.z!=0 ){
129 pNew->token.z = sqliteStrDup(p->token.z);
130 pNew->token.dyn = 1;
131 }else{
132 pNew->token.z = 0;
133 pNew->token.n = 0;
134 pNew->token.dyn = 0;
135 }
136 pNew->span.z = 0;
137 pNew->span.n = 0;
138 pNew->span.dyn = 0;
139 pNew->pLeft = sqliteExprDup(p->pLeft);
140 pNew->pRight = sqliteExprDup(p->pRight);
141 pNew->pList = sqliteExprListDup(p->pList);
142 pNew->pSelect = sqliteSelectDup(p->pSelect);
143 return pNew;
144}
145void sqliteTokenCopy(Token *pTo, Token *pFrom){
146 if( pTo->dyn ) sqliteFree((char*)pTo->z);
147 if( pFrom->z ){
148 pTo->n = pFrom->n;
149 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
150 pTo->dyn = 1;
151 }else{
152 pTo->n = 0;
153 pTo->z = 0;
154 pTo->dyn = 0;
155 }
156}
157ExprList *sqliteExprListDup(ExprList *p){
158 ExprList *pNew;
159 int i;
160 if( p==0 ) return 0;
161 pNew = sqliteMalloc( sizeof(*pNew) );
162 if( pNew==0 ) return 0;
163 pNew->nExpr = p->nExpr;
164 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
165 if( pNew->a==0 ) return 0;
166 for(i=0; i<p->nExpr; i++){
167 Expr *pNewExpr, *pOldExpr;
168 pNew->a[i].pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr);
169 if( pOldExpr->span.z!=0 && pNewExpr ){
170 /* Always make a copy of the span for top-level expressions in the
171 ** expression list. The logic in SELECT processing that determines
172 ** the names of columns in the result set needs this information */
173 sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span);
174 }
175 assert( pNewExpr==0 || pNewExpr->span.z!=0
176 || pOldExpr->span.z==0 || sqlite_malloc_failed );
177 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
178 pNew->a[i].sortOrder = p->a[i].sortOrder;
179 pNew->a[i].isAgg = p->a[i].isAgg;
180 pNew->a[i].done = 0;
181 }
182 return pNew;
183}
184SrcList *sqliteSrcListDup(SrcList *p){
185 SrcList *pNew;
186 int i;
187 int nByte;
188 if( p==0 ) return 0;
189 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
190 pNew = sqliteMalloc( nByte );
191 if( pNew==0 ) return 0;
192 pNew->nSrc = p->nSrc;
193 for(i=0; i<p->nSrc; i++){
194 pNew->a[i].zDatabase = sqliteStrDup(p->a[i].zDatabase);
195 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
196 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
197 pNew->a[i].jointype = p->a[i].jointype;
198 pNew->a[i].iCursor = p->a[i].iCursor;
199 pNew->a[i].pTab = 0;
200 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
201 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
202 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
203 }
204 return pNew;
205}
206IdList *sqliteIdListDup(IdList *p){
207 IdList *pNew;
208 int i;
209 if( p==0 ) return 0;
210 pNew = sqliteMalloc( sizeof(*pNew) );
211 if( pNew==0 ) return 0;
212 pNew->nId = p->nId;
213 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
214 if( pNew->a==0 ) return 0;
215 for(i=0; i<p->nId; i++){
216 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
217 pNew->a[i].idx = p->a[i].idx;
218 }
219 return pNew;
220}
221Select *sqliteSelectDup(Select *p){
222 Select *pNew;
223 if( p==0 ) return 0;
224 pNew = sqliteMalloc( sizeof(*p) );
225 if( pNew==0 ) return 0;
226 pNew->isDistinct = p->isDistinct;
227 pNew->pEList = sqliteExprListDup(p->pEList);
228 pNew->pSrc = sqliteSrcListDup(p->pSrc);
229 pNew->pWhere = sqliteExprDup(p->pWhere);
230 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
231 pNew->pHaving = sqliteExprDup(p->pHaving);
232 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
233 pNew->op = p->op;
234 pNew->pPrior = sqliteSelectDup(p->pPrior);
235 pNew->nLimit = p->nLimit;
236 pNew->nOffset = p->nOffset;
237 pNew->zSelect = 0;
238 return pNew;
239}
240
241
242/*
243** Add a new element to the end of an expression list. If pList is
244** initially NULL, then create a new expression list.
245*/
246ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
247 int i;
248 if( pList==0 ){
249 pList = sqliteMalloc( sizeof(ExprList) );
250 if( pList==0 ){
251 sqliteExprDelete(pExpr);
252 return 0;
253 }
254 }
255 if( (pList->nExpr & 7)==0 ){
256 int n = pList->nExpr + 8;
257 struct ExprList_item *a;
258 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
259 if( a==0 ){
260 sqliteExprDelete(pExpr);
261 return pList;
262 }
263 pList->a = a;
264 }
265 if( pExpr || pName ){
266 i = pList->nExpr++;
267 pList->a[i].pExpr = pExpr;
268 pList->a[i].zName = 0;
269 if( pName ){
270 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
271 sqliteDequote(pList->a[i].zName);
272 }
273 }
274 return pList;
275}
276
277/*
278** Delete an entire expression list.
279*/
280void sqliteExprListDelete(ExprList *pList){
281 int i;
282 if( pList==0 ) return;
283 for(i=0; i<pList->nExpr; i++){
284 sqliteExprDelete(pList->a[i].pExpr);
285 sqliteFree(pList->a[i].zName);
286 }
287 sqliteFree(pList->a);
288 sqliteFree(pList);
289}
290
291/*
292** Walk an expression tree. Return 1 if the expression is constant
293** and 0 if it involves variables.
294**
295** For the purposes of this function, a double-quoted string (ex: "abc")
296** is considered a variable but a single-quoted string (ex: 'abc') is
297** a constant.
298*/
299int sqliteExprIsConstant(Expr *p){
300 switch( p->op ){
301 case TK_ID:
302 case TK_COLUMN:
303 case TK_DOT:
304 case TK_FUNCTION:
305 return 0;
306 case TK_NULL:
307 case TK_STRING:
308 case TK_INTEGER:
309 case TK_FLOAT:
310 return 1;
311 default: {
312 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
313 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
314 if( p->pList ){
315 int i;
316 for(i=0; i<p->pList->nExpr; i++){
317 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
318 }
319 }
320 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
321 }
322 }
323 return 0;
324}
325
326/*
327** If the given expression codes a constant integer, return 1 and put
328** the value of the integer in *pValue. If the expression is not an
329** integer, return 0 and leave *pValue unchanged.
330*/
331int sqliteExprIsInteger(Expr *p, int *pValue){
332 switch( p->op ){
333 case TK_INTEGER: {
334 *pValue = atoi(p->token.z);
335 return 1;
336 }
337 case TK_STRING: {
338 const char *z = p->token.z;
339 int n = p->token.n;
340 if( n>0 && z[0]=='-' ){ z++; n--; }
341 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
342 if( n==0 ){
343 *pValue = atoi(p->token.z);
344 return 1;
345 }
346 break;
347 }
348 case TK_UPLUS: {
349 return sqliteExprIsInteger(p->pLeft, pValue);
350 }
351 case TK_UMINUS: {
352 int v;
353 if( sqliteExprIsInteger(p->pLeft, &v) ){
354 *pValue = -v;
355 return 1;
356 }
357 break;
358 }
359 default: break;
360 }
361 return 0;
362}
363
364/*
365** Return TRUE if the given string is a row-id column name.
366*/
367int sqliteIsRowid(const char *z){
368 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
369 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
370 if( sqliteStrICmp(z, "OID")==0 ) return 1;
371 return 0;
372}
373
374/*
375** This routine walks an expression tree and resolves references to
376** table columns. Nodes of the form ID.ID or ID resolve into an
377** index to the table in the table list and a column offset. The
378** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
379** value is changed to the index of the referenced table in pTabList
380** plus the "base" value. The base value will ultimately become the
381** VDBE cursor number for a cursor that is pointing into the referenced
382** table. The Expr.iColumn value is changed to the index of the column
383** of the referenced table. The Expr.iColumn value for the special
384** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
385** alias for ROWID.
386**
387** We also check for instances of the IN operator. IN comes in two
388** forms:
389**
390** expr IN (exprlist)
391** and
392** expr IN (SELECT ...)
393**
394** The first form is handled by creating a set holding the list
395** of allowed values. The second form causes the SELECT to generate
396** a temporary table.
397**
398** This routine also looks for scalar SELECTs that are part of an expression.
399** If it finds any, it generates code to write the value of that select
400** into a memory cell.
401**
402** Unknown columns or tables provoke an error. The function returns
403** the number of errors seen and leaves an error message on pParse->zErrMsg.
404*/
405int sqliteExprResolveIds(
406 Parse *pParse, /* The parser context */
407 SrcList *pTabList, /* List of tables used to resolve column names */
408 ExprList *pEList, /* List of expressions used to resolve "AS" */
409 Expr *pExpr /* The expression to be analyzed. */
410){
411 int i;
412
413 if( pExpr==0 || pTabList==0 ) return 0;
414 for(i=0; i<pTabList->nSrc; i++){
415 assert( pTabList->a[i].iCursor>=0 && pTabList->a[i].iCursor<pParse->nTab );
416 }
417 switch( pExpr->op ){
418 /* Double-quoted strings (ex: "abc") are used as identifiers if
419 ** possible. Otherwise they remain as strings. Single-quoted
420 ** strings (ex: 'abc') are always string literals.
421 */
422 case TK_STRING: {
423 if( pExpr->token.z[0]=='\'' ) break;
424 /* Fall thru into the TK_ID case if this is a double-quoted string */
425 }
426 /* A lone identifier. Try and match it as follows:
427 **
428 ** 1. To the name of a column of one of the tables in pTabList
429 **
430 ** 2. To the right side of an AS keyword in the column list of
431 ** a SELECT statement. (For example, match against 'x' in
432 ** "SELECT a+b AS 'x' FROM t1".)
433 **
434 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
435 */
436 case TK_ID: {
437 int cnt = 0; /* Number of matches */
438 char *z;
439 int iDb = -1;
440
441 assert( pExpr->token.z );
442 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
443 sqliteDequote(z);
444 if( z==0 ) return 1;
445 for(i=0; i<pTabList->nSrc; i++){
446 int j;
447 Table *pTab = pTabList->a[i].pTab;
448 if( pTab==0 ) continue;
449 iDb = pTab->iDb;
450 assert( pTab->nCol>0 );
451 for(j=0; j<pTab->nCol; j++){
452 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
453 cnt++;
454 pExpr->iTable = pTabList->a[i].iCursor;
455 pExpr->iDb = pTab->iDb;
456 if( j==pTab->iPKey ){
457 /* Substitute the record number for the INTEGER PRIMARY KEY */
458 pExpr->iColumn = -1;
459 pExpr->dataType = SQLITE_SO_NUM;
460 }else{
461 pExpr->iColumn = j;
462 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
463 }
464 pExpr->op = TK_COLUMN;
465 }
466 }
467 }
468 if( cnt==0 && pEList!=0 ){
469 int j;
470 for(j=0; j<pEList->nExpr; j++){
471 char *zAs = pEList->a[j].zName;
472 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
473 cnt++;
474 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
475 pExpr->op = TK_AS;
476 pExpr->iColumn = j;
477 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
478 }
479 }
480 }
481 if( cnt==0 && iDb>=0 && sqliteIsRowid(z) ){
482 pExpr->iColumn = -1;
483 pExpr->iTable = pTabList->a[0].iCursor;
484 pExpr->iDb = iDb;
485 cnt = 1 + (pTabList->nSrc>1);
486 pExpr->op = TK_COLUMN;
487 pExpr->dataType = SQLITE_SO_NUM;
488 }
489 sqliteFree(z);
490 if( cnt==0 && pExpr->token.z[0]!='"' ){
491 sqliteErrorMsg(pParse, "no such column: %T", &pExpr->token);
492 return 1;
493 }else if( cnt>1 ){
494 sqliteErrorMsg(pParse, "ambiguous column name: %T", &pExpr->token);
495 return 1;
496 }
497 if( pExpr->op==TK_COLUMN ){
498 sqliteAuthRead(pParse, pExpr, pTabList);
499 }
500 break;
501 }
502
503 /* A table name and column name: ID.ID
504 ** Or a database, table and column: ID.ID.ID
505 */
506 case TK_DOT: {
507 int cnt = 0; /* Number of matches */
508 int cntTab = 0; /* Number of matching tables */
509 int i; /* Loop counter */
510 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
511 char *zLeft, *zRight; /* Text of an identifier */
512 char *zDb; /* Name of database holding table */
513 sqlite *db = pParse->db;
514
515 pRight = pExpr->pRight;
516 if( pRight->op==TK_ID ){
517 pLeft = pExpr->pLeft;
518 zDb = 0;
519 }else{
520 Expr *pDb = pExpr->pLeft;
521 assert( pDb && pDb->op==TK_ID && pDb->token.z );
522 zDb = sqliteStrNDup(pDb->token.z, pDb->token.n);
523 pLeft = pRight->pLeft;
524 pRight = pRight->pRight;
525 }
526 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
527 assert( pRight && pRight->op==TK_ID && pRight->token.z );
528 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
529 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
530 if( zLeft==0 || zRight==0 ){
531 sqliteFree(zLeft);
532 sqliteFree(zRight);
533 sqliteFree(zDb);
534 return 1;
535 }
536 sqliteDequote(zDb);
537 sqliteDequote(zLeft);
538 sqliteDequote(zRight);
539 pExpr->iTable = -1;
540 for(i=0; i<pTabList->nSrc; i++){
541 int j;
542 char *zTab;
543 Table *pTab = pTabList->a[i].pTab;
544 if( pTab==0 ) continue;
545 assert( pTab->nCol>0 );
546 if( pTabList->a[i].zAlias ){
547 zTab = pTabList->a[i].zAlias;
548 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
549 }else{
550 zTab = pTab->zName;
551 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
552 if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
553 continue;
554 }
555 }
556 if( 0==(cntTab++) ){
557 pExpr->iTable = pTabList->a[i].iCursor;
558 pExpr->iDb = pTab->iDb;
559 }
560 for(j=0; j<pTab->nCol; j++){
561 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
562 cnt++;
563 pExpr->iTable = pTabList->a[i].iCursor;
564 pExpr->iDb = pTab->iDb;
565 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
566 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
567 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
568 }
569 }
570 }
571
572 /* If we have not already resolved this *.* expression, then maybe
573 * it is a new.* or old.* trigger argument reference */
574 if( cnt == 0 && pParse->trigStack != 0 ){
575 TriggerStack *pTriggerStack = pParse->trigStack;
576 int t = 0;
577 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
578 pExpr->iTable = pTriggerStack->newIdx;
579 assert( pTriggerStack->pTab );
580 pExpr->iDb = pTriggerStack->pTab->iDb;
581 cntTab++;
582 t = 1;
583 }
584 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
585 pExpr->iTable = pTriggerStack->oldIdx;
586 assert( pTriggerStack->pTab );
587 pExpr->iDb = pTriggerStack->pTab->iDb;
588 cntTab++;
589 t = 1;
590 }
591
592 if( t ){
593 int j;
594 Table *pTab = pTriggerStack->pTab;
595 for(j=0; j < pTab->nCol; j++) {
596 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
597 cnt++;
598 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
599 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
600 }
601 }
602}
603 }
604
605 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
606 cnt = 1;
607 pExpr->iColumn = -1;
608 pExpr->dataType = SQLITE_SO_NUM;
609 }
610 sqliteFree(zDb);
611 sqliteFree(zLeft);
612 sqliteFree(zRight);
613 if( cnt==0 ){
614 sqliteErrorMsg(pParse, "no such column: %T.%T",
615 &pLeft->token, &pRight->token);
616 return 1;
617 }else if( cnt>1 ){
618 sqliteErrorMsg(pParse, "ambiguous column name: %T.%T",
619 &pLeft->token, &pRight->token);
620 return 1;
621 }
622 sqliteExprDelete(pExpr->pLeft);
623 pExpr->pLeft = 0;
624 sqliteExprDelete(pExpr->pRight);
625 pExpr->pRight = 0;
626 pExpr->op = TK_COLUMN;
627 sqliteAuthRead(pParse, pExpr, pTabList);
628 break;
629 }
630
631 case TK_IN: {
632 Vdbe *v = sqliteGetVdbe(pParse);
633 if( v==0 ) return 1;
634 if( sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
635 return 1;
636 }
637 if( pExpr->pSelect ){
638 /* Case 1: expr IN (SELECT ...)
639 **
640 ** Generate code to write the results of the select into a temporary
641 ** table. The cursor number of the temporary table has already
642 ** been put in iTable by sqliteExprResolveInSelect().
643 */
644 pExpr->iTable = pParse->nTab++;
645 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
646 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
647 }else if( pExpr->pList ){
648 /* Case 2: expr IN (exprlist)
649 **
650 ** Create a set to put the exprlist values in. The Set id is stored
651 ** in iTable.
652 */
653 int i, iSet;
654 for(i=0; i<pExpr->pList->nExpr; i++){
655 Expr *pE2 = pExpr->pList->a[i].pExpr;
656 if( !sqliteExprIsConstant(pE2) ){
657 sqliteErrorMsg(pParse,
658 "right-hand side of IN operator must be constant");
659 return 1;
660 }
661 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
662 return 1;
663 }
664 }
665 iSet = pExpr->iTable = pParse->nSet++;
666 for(i=0; i<pExpr->pList->nExpr; i++){
667 Expr *pE2 = pExpr->pList->a[i].pExpr;
668 switch( pE2->op ){
669 case TK_FLOAT:
670 case TK_INTEGER:
671 case TK_STRING: {
672 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
673 assert( pE2->token.z );
674 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
675 sqliteVdbeDequoteP3(v, addr);
676 break;
677 }
678 default: {
679 sqliteExprCode(pParse, pE2);
680 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
681 break;
682 }
683 }
684 }
685 }
686 break;
687 }
688
689 case TK_SELECT: {
690 /* This has to be a scalar SELECT. Generate code to put the
691 ** value of this select in a memory cell and record the number
692 ** of the memory cell in iColumn.
693 */
694 pExpr->iColumn = pParse->nMem++;
695 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
696 return 1;
697 }
698 break;
699 }
700
701 /* For all else, just recursively walk the tree */
702 default: {
703 if( pExpr->pLeft
704 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
705 return 1;
706 }
707 if( pExpr->pRight
708 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pRight) ){
709 return 1;
710 }
711 if( pExpr->pList ){
712 int i;
713 ExprList *pList = pExpr->pList;
714 for(i=0; i<pList->nExpr; i++){
715 Expr *pArg = pList->a[i].pExpr;
716 if( sqliteExprResolveIds(pParse, pTabList, pEList, pArg) ){
717 return 1;
718 }
719 }
720 }
721 }
722 }
723 return 0;
724}
725
726/*
727** pExpr is a node that defines a function of some kind. It might
728** be a syntactic function like "count(x)" or it might be a function
729** that implements an operator, like "a LIKE b".
730**
731** This routine makes *pzName point to the name of the function and
732** *pnName hold the number of characters in the function name.
733*/
734static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
735 switch( pExpr->op ){
736 case TK_FUNCTION: {
737 *pzName = pExpr->token.z;
738 *pnName = pExpr->token.n;
739 break;
740 }
741 case TK_LIKE: {
742 *pzName = "like";
743 *pnName = 4;
744 break;
745 }
746 case TK_GLOB: {
747 *pzName = "glob";
748 *pnName = 4;
749 break;
750 }
751 default: {
752 *pzName = "can't happen";
753 *pnName = 12;
754 break;
755 }
756 }
757}
758
759/*
760** Error check the functions in an expression. Make sure all
761** function names are recognized and all functions have the correct
762** number of arguments. Leave an error message in pParse->zErrMsg
763** if anything is amiss. Return the number of errors.
764**
765** if pIsAgg is not null and this expression is an aggregate function
766** (like count(*) or max(value)) then write a 1 into *pIsAgg.
767*/
768int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
769 int nErr = 0;
770 if( pExpr==0 ) return 0;
771 switch( pExpr->op ){
772 case TK_GLOB:
773 case TK_LIKE:
774 case TK_FUNCTION: {
775 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
776 int no_such_func = 0; /* True if no such function exists */
777 int is_type_of = 0; /* True if is the special TypeOf() function */
778 int wrong_num_args = 0; /* True if wrong number of arguments */
779 int is_agg = 0; /* True if is an aggregate function */
780 int i;
781 int nId; /* Number of characters in function name */
782 const char *zId; /* The function name. */
783 FuncDef *pDef;
784
785 getFunctionName(pExpr, &zId, &nId);
786 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
787 if( pDef==0 ){
788 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
789 if( pDef==0 ){
790 if( n==1 && nId==6 && sqliteStrNICmp(zId, "typeof", 6)==0 ){
791 is_type_of = 1;
792 }else {
793 no_such_func = 1;
794 }
795 }else{
796 wrong_num_args = 1;
797 }
798 }else{
799 is_agg = pDef->xFunc==0;
800 }
801 if( is_agg && !allowAgg ){
802 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
803 zId, nId, "()", 2, 0);
804 pParse->nErr++;
805 nErr++;
806 is_agg = 0;
807 }else if( no_such_func ){
808 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, zId,nId,0);
809 pParse->nErr++;
810 nErr++;
811 }else if( wrong_num_args ){
812 sqliteSetNString(&pParse->zErrMsg,
813 "wrong number of arguments to function ", -1, zId, nId, "()", 2, 0);
814 pParse->nErr++;
815 nErr++;
816 }
817 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
818 if( is_agg && pIsAgg ) *pIsAgg = 1;
819 for(i=0; nErr==0 && i<n; i++){
820 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
821 allowAgg && !is_agg, pIsAgg);
822 }
823 if( pDef==0 ){
824 if( is_type_of ){
825 pExpr->op = TK_STRING;
826 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
827 pExpr->token.z = "numeric";
828 pExpr->token.n = 7;
829 }else{
830 pExpr->token.z = "text";
831 pExpr->token.n = 4;
832 }
833 }
834 }else if( pDef->dataType>=0 ){
835 if( pDef->dataType<n ){
836 pExpr->dataType =
837 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
838 }else{
839 pExpr->dataType = SQLITE_SO_NUM;
840 }
841 }else if( pDef->dataType==SQLITE_ARGS ){
842 pDef->dataType = SQLITE_SO_TEXT;
843 for(i=0; i<n; i++){
844 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
845 pExpr->dataType = SQLITE_SO_NUM;
846 break;
847 }
848 }
849 }else if( pDef->dataType==SQLITE_NUMERIC ){
850 pExpr->dataType = SQLITE_SO_NUM;
851 }else{
852 pExpr->dataType = SQLITE_SO_TEXT;
853 }
854 }
855 default: {
856 if( pExpr->pLeft ){
857 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
858 }
859 if( nErr==0 && pExpr->pRight ){
860 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
861 }
862 if( nErr==0 && pExpr->pList ){
863 int n = pExpr->pList->nExpr;
864 int i;
865 for(i=0; nErr==0 && i<n; i++){
866 Expr *pE2 = pExpr->pList->a[i].pExpr;
867 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
868 }
869 }
870 break;
871 }
872 }
873 return nErr;
874}
875
876/*
877** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
878** given expression should sort as numeric values or as text.
879**
880** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
881** both been called on the expression before it is passed to this routine.
882*/
883int sqliteExprType(Expr *p){
884 if( p==0 ) return SQLITE_SO_NUM;
885 while( p ) switch( p->op ){
886 case TK_PLUS:
887 case TK_MINUS:
888 case TK_STAR:
889 case TK_SLASH:
890 case TK_AND:
891 case TK_OR:
892 case TK_ISNULL:
893 case TK_NOTNULL:
894 case TK_NOT:
895 case TK_UMINUS:
896 case TK_UPLUS:
897 case TK_BITAND:
898 case TK_BITOR:
899 case TK_BITNOT:
900 case TK_LSHIFT:
901 case TK_RSHIFT:
902 case TK_REM:
903 case TK_INTEGER:
904 case TK_FLOAT:
905 case TK_IN:
906 case TK_BETWEEN:
907 case TK_GLOB:
908 case TK_LIKE:
909 return SQLITE_SO_NUM;
910
911 case TK_STRING:
912 case TK_NULL:
913 case TK_CONCAT:
914 return SQLITE_SO_TEXT;
915
916 case TK_LT:
917 case TK_LE:
918 case TK_GT:
919 case TK_GE:
920 case TK_NE:
921 case TK_EQ:
922 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
923 return SQLITE_SO_NUM;
924 }
925 p = p->pRight;
926 break;
927
928 case TK_AS:
929 p = p->pLeft;
930 break;
931
932 case TK_COLUMN:
933 case TK_FUNCTION:
934 case TK_AGG_FUNCTION:
935 return p->dataType;
936
937 case TK_SELECT:
938 assert( p->pSelect );
939 assert( p->pSelect->pEList );
940 assert( p->pSelect->pEList->nExpr>0 );
941 p = p->pSelect->pEList->a[0].pExpr;
942 break;
943
944 case TK_CASE: {
945 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
946 return SQLITE_SO_NUM;
947 }
948 if( p->pList ){
949 int i;
950 ExprList *pList = p->pList;
951 for(i=1; i<pList->nExpr; i+=2){
952 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
953 return SQLITE_SO_NUM;
954 }
955 }
956 }
957 return SQLITE_SO_TEXT;
958 }
959
960 default:
961 assert( p->op==TK_ABORT ); /* Can't Happen */
962 break;
963 }
964 return SQLITE_SO_NUM;
965}
966
967/*
968** Generate code into the current Vdbe to evaluate the given
969** expression and leave the result on the top of stack.
970*/
971void sqliteExprCode(Parse *pParse, Expr *pExpr){
972 Vdbe *v = pParse->pVdbe;
973 int op;
974 if( v==0 || pExpr==0 ) return;
975 switch( pExpr->op ){
976 case TK_PLUS: op = OP_Add; break;
977 case TK_MINUS: op = OP_Subtract; break;
978 case TK_STAR: op = OP_Multiply; break;
979 case TK_SLASH: op = OP_Divide; break;
980 case TK_AND: op = OP_And; break;
981 case TK_OR: op = OP_Or; break;
982 case TK_LT: op = OP_Lt; break;
983 case TK_LE: op = OP_Le; break;
984 case TK_GT: op = OP_Gt; break;
985 case TK_GE: op = OP_Ge; break;
986 case TK_NE: op = OP_Ne; break;
987 case TK_EQ: op = OP_Eq; break;
988 case TK_ISNULL: op = OP_IsNull; break;
989 case TK_NOTNULL: op = OP_NotNull; break;
990 case TK_NOT: op = OP_Not; break;
991 case TK_UMINUS: op = OP_Negative; break;
992 case TK_BITAND: op = OP_BitAnd; break;
993 case TK_BITOR: op = OP_BitOr; break;
994 case TK_BITNOT: op = OP_BitNot; break;
995 case TK_LSHIFT: op = OP_ShiftLeft; break;
996 case TK_RSHIFT: op = OP_ShiftRight; break;
997 case TK_REM: op = OP_Remainder; break;
998 default: break;
999 }
1000 switch( pExpr->op ){
1001 case TK_COLUMN: {
1002 if( pParse->useAgg ){
1003 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
1004 }else if( pExpr->iColumn>=0 ){
1005 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
1006 }else{
1007 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
1008 }
1009 break;
1010 }
1011 case TK_INTEGER: {
1012 int iVal = atoi(pExpr->token.z);
1013 char zBuf[30];
1014 sprintf(zBuf,"%d",iVal);
1015 if( strlen(zBuf)!=pExpr->token.n
1016 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){
1017 /* If the integer value cannot be represented exactly in 32 bits,
1018 ** then code it as a string instead. */
1019 sqliteVdbeAddOp(v, OP_String, 0, 0);
1020 }else{
1021 sqliteVdbeAddOp(v, OP_Integer, iVal, 0);
1022 }
1023 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1024 break;
1025 }
1026 case TK_FLOAT: {
1027 sqliteVdbeAddOp(v, OP_String, 0, 0);
1028 assert( pExpr->token.z );
1029 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1030 break;
1031 }
1032 case TK_STRING: {
1033 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1034 assert( pExpr->token.z );
1035 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
1036 sqliteVdbeDequoteP3(v, addr);
1037 break;
1038 }
1039 case TK_NULL: {
1040 sqliteVdbeAddOp(v, OP_String, 0, 0);
1041 break;
1042 }
1043 case TK_LT:
1044 case TK_LE:
1045 case TK_GT:
1046 case TK_GE:
1047 case TK_NE:
1048 case TK_EQ: {
1049 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1050 op += 6; /* Convert numeric opcodes to text opcodes */
1051 }
1052 /* Fall through into the next case */
1053 }
1054 case TK_AND:
1055 case TK_OR:
1056 case TK_PLUS:
1057 case TK_STAR:
1058 case TK_MINUS:
1059 case TK_REM:
1060 case TK_BITAND:
1061 case TK_BITOR:
1062 case TK_SLASH: {
1063 sqliteExprCode(pParse, pExpr->pLeft);
1064 sqliteExprCode(pParse, pExpr->pRight);
1065 sqliteVdbeAddOp(v, op, 0, 0);
1066 break;
1067 }
1068 case TK_LSHIFT:
1069 case TK_RSHIFT: {
1070 sqliteExprCode(pParse, pExpr->pRight);
1071 sqliteExprCode(pParse, pExpr->pLeft);
1072 sqliteVdbeAddOp(v, op, 0, 0);
1073 break;
1074 }
1075 case TK_CONCAT: {
1076 sqliteExprCode(pParse, pExpr->pLeft);
1077 sqliteExprCode(pParse, pExpr->pRight);
1078 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
1079 break;
1080 }
1081 case TK_UPLUS: {
1082 Expr *pLeft = pExpr->pLeft;
1083 if( pLeft && pLeft->op==TK_INTEGER ){
1084 sqliteVdbeAddOp(v, OP_Integer, atoi(pLeft->token.z), 0);
1085 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1086 }else if( pLeft && pLeft->op==TK_FLOAT ){
1087 sqliteVdbeAddOp(v, OP_String, 0, 0);
1088 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1089 }else{
1090 sqliteExprCode(pParse, pExpr->pLeft);
1091 }
1092 break;
1093 }
1094 case TK_UMINUS: {
1095 assert( pExpr->pLeft );
1096 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
1097 Token *p = &pExpr->pLeft->token;
1098 char *z = sqliteMalloc( p->n + 2 );
1099 sprintf(z, "-%.*s", p->n, p->z);
1100 if( pExpr->pLeft->op==TK_INTEGER ){
1101 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1102 }else{
1103 sqliteVdbeAddOp(v, OP_String, 0, 0);
1104 }
1105 sqliteVdbeChangeP3(v, -1, z, p->n+1);
1106 sqliteFree(z);
1107 break;
1108 }
1109 /* Fall through into TK_NOT */
1110 }
1111 case TK_BITNOT:
1112 case TK_NOT: {
1113 sqliteExprCode(pParse, pExpr->pLeft);
1114 sqliteVdbeAddOp(v, op, 0, 0);
1115 break;
1116 }
1117 case TK_ISNULL:
1118 case TK_NOTNULL: {
1119 int dest;
1120 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
1121 sqliteExprCode(pParse, pExpr->pLeft);
1122 dest = sqliteVdbeCurrentAddr(v) + 2;
1123 sqliteVdbeAddOp(v, op, 1, dest);
1124 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
1125 break;
1126 }
1127 case TK_AGG_FUNCTION: {
1128 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
1129 break;
1130 }
1131 case TK_GLOB:
1132 case TK_LIKE:
1133 case TK_FUNCTION: {
1134 int i;
1135 ExprList *pList = pExpr->pList;
1136 int nExpr = pList ? pList->nExpr : 0;
1137 FuncDef *pDef;
1138 int nId;
1139 const char *zId;
1140 getFunctionName(pExpr, &zId, &nId);
1141 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
1142 assert( pDef!=0 );
1143 for(i=0; i<nExpr; i++){
1144 sqliteExprCode(pParse, pList->a[i].pExpr);
1145 }
1146 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
1147 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
1148 break;
1149 }
1150 case TK_SELECT: {
1151 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
1152 break;
1153 }
1154 case TK_IN: {
1155 int addr;
1156 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
1157 sqliteExprCode(pParse, pExpr->pLeft);
1158 addr = sqliteVdbeCurrentAddr(v);
1159 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1160 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1161 sqliteVdbeAddOp(v, OP_String, 0, 0);
1162 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
1163 if( pExpr->pSelect ){
1164 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
1165 }else{
1166 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
1167 }
1168 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
1169 break;
1170 }
1171 case TK_BETWEEN: {
1172 sqliteExprCode(pParse, pExpr->pLeft);
1173 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1174 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1175 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1176 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1177 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1178 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1179 sqliteVdbeAddOp(v, OP_And, 0, 0);
1180 break;
1181 }
1182 case TK_AS: {
1183 sqliteExprCode(pParse, pExpr->pLeft);
1184 break;
1185 }
1186 case TK_CASE: {
1187 int expr_end_label;
1188 int jumpInst;
1189 int addr;
1190 int nExpr;
1191 int i;
1192
1193 assert(pExpr->pList);
1194 assert((pExpr->pList->nExpr % 2) == 0);
1195 assert(pExpr->pList->nExpr > 0);
1196 nExpr = pExpr->pList->nExpr;
1197 expr_end_label = sqliteVdbeMakeLabel(v);
1198 if( pExpr->pLeft ){
1199 sqliteExprCode(pParse, pExpr->pLeft);
1200 }
1201 for(i=0; i<nExpr; i=i+2){
1202 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
1203 if( pExpr->pLeft ){
1204 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
1205 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1206 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1207 }else{
1208 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
1209 }
1210 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1211 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
1212 addr = sqliteVdbeCurrentAddr(v);
1213 sqliteVdbeChangeP2(v, jumpInst, addr);
1214 }
1215 if( pExpr->pLeft ){
1216 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1217 }
1218 if( pExpr->pRight ){
1219 sqliteExprCode(pParse, pExpr->pRight);
1220 }else{
1221 sqliteVdbeAddOp(v, OP_String, 0, 0);
1222 }
1223 sqliteVdbeResolveLabel(v, expr_end_label);
1224 break;
1225 }
1226 case TK_RAISE: {
1227 if( !pParse->trigStack ){
1228 sqliteErrorMsg(pParse,
1229 "RAISE() may only be used within a trigger-program");
1230 pParse->nErr++;
1231return;
1232 }
1233 if( pExpr->iColumn == OE_Rollback ||
1234 pExpr->iColumn == OE_Abort ||
1235 pExpr->iColumn == OE_Fail ){
1236 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1237 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1238 sqliteDequote(msg);
1239 sqliteVdbeChangeP3(v, -1, msg, 0);
1240 sqliteFree(msg);
1241 } else {
1242 assert( pExpr->iColumn == OE_Ignore );
1243 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1244 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", 0);
1245 }
1246 }
1247 break;
1248 }
1249}
1250
1251/*
1252** Generate code for a boolean expression such that a jump is made
1253** to the label "dest" if the expression is true but execution
1254** continues straight thru if the expression is false.
1255**
1256** If the expression evaluates to NULL (neither true nor false), then
1257** take the jump if the jumpIfNull flag is true.
1258*/
1259void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1260 Vdbe *v = pParse->pVdbe;
1261 int op = 0;
1262 if( v==0 || pExpr==0 ) return;
1263 switch( pExpr->op ){
1264 case TK_LT: op = OP_Lt; break;
1265 case TK_LE: op = OP_Le; break;
1266 case TK_GT: op = OP_Gt; break;
1267 case TK_GE: op = OP_Ge; break;
1268 case TK_NE: op = OP_Ne; break;
1269 case TK_EQ: op = OP_Eq; break;
1270 case TK_ISNULL: op = OP_IsNull; break;
1271 case TK_NOTNULL: op = OP_NotNull; break;
1272 default: break;
1273 }
1274 switch( pExpr->op ){
1275 case TK_AND: {
1276 int d2 = sqliteVdbeMakeLabel(v);
1277 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1278 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1279 sqliteVdbeResolveLabel(v, d2);
1280 break;
1281 }
1282 case TK_OR: {
1283 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1284 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1285 break;
1286 }
1287 case TK_NOT: {
1288 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1289 break;
1290 }
1291 case TK_LT:
1292 case TK_LE:
1293 case TK_GT:
1294 case TK_GE:
1295 case TK_NE:
1296 case TK_EQ: {
1297 sqliteExprCode(pParse, pExpr->pLeft);
1298 sqliteExprCode(pParse, pExpr->pRight);
1299 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1300 op += 6; /* Convert numeric opcodes to text opcodes */
1301 }
1302 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
1303 break;
1304 }
1305 case TK_ISNULL:
1306 case TK_NOTNULL: {
1307 sqliteExprCode(pParse, pExpr->pLeft);
1308 sqliteVdbeAddOp(v, op, 1, dest);
1309 break;
1310 }
1311 case TK_IN: {
1312 int addr;
1313 sqliteExprCode(pParse, pExpr->pLeft);
1314 addr = sqliteVdbeCurrentAddr(v);
1315 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1316 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1317 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
1318 if( pExpr->pSelect ){
1319 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
1320 }else{
1321 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
1322 }
1323 break;
1324 }
1325 case TK_BETWEEN: {
1326 int addr;
1327 sqliteExprCode(pParse, pExpr->pLeft);
1328 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1329 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1330 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
1331 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1332 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
1333 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1334 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
1335 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1336 break;
1337 }
1338 default: {
1339 sqliteExprCode(pParse, pExpr);
1340 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
1341 break;
1342 }
1343 }
1344}
1345
1346/*
1347** Generate code for a boolean expression such that a jump is made
1348** to the label "dest" if the expression is false but execution
1349** continues straight thru if the expression is true.
1350**
1351** If the expression evaluates to NULL (neither true nor false) then
1352** jump if jumpIfNull is true or fall through if jumpIfNull is false.
1353*/
1354void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1355 Vdbe *v = pParse->pVdbe;
1356 int op = 0;
1357 if( v==0 || pExpr==0 ) return;
1358 switch( pExpr->op ){
1359 case TK_LT: op = OP_Ge; break;
1360 case TK_LE: op = OP_Gt; break;
1361 case TK_GT: op = OP_Le; break;
1362 case TK_GE: op = OP_Lt; break;
1363 case TK_NE: op = OP_Eq; break;
1364 case TK_EQ: op = OP_Ne; break;
1365 case TK_ISNULL: op = OP_NotNull; break;
1366 case TK_NOTNULL: op = OP_IsNull; break;
1367 default: break;
1368 }
1369 switch( pExpr->op ){
1370 case TK_AND: {
1371 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1372 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1373 break;
1374 }
1375 case TK_OR: {
1376 int d2 = sqliteVdbeMakeLabel(v);
1377 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1378 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1379 sqliteVdbeResolveLabel(v, d2);
1380 break;
1381 }
1382 case TK_NOT: {
1383 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1384 break;
1385 }
1386 case TK_LT:
1387 case TK_LE:
1388 case TK_GT:
1389 case TK_GE:
1390 case TK_NE:
1391 case TK_EQ: {
1392 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1393 /* Convert numeric comparison opcodes into text comparison opcodes.
1394 ** This step depends on the fact that the text comparision opcodes are
1395 ** always 6 greater than their corresponding numeric comparison
1396 ** opcodes.
1397 */
1398 assert( OP_Eq+6 == OP_StrEq );
1399 op += 6;
1400 }
1401 sqliteExprCode(pParse, pExpr->pLeft);
1402 sqliteExprCode(pParse, pExpr->pRight);
1403 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
1404 break;
1405 }
1406 case TK_ISNULL:
1407 case TK_NOTNULL: {
1408 sqliteExprCode(pParse, pExpr->pLeft);
1409 sqliteVdbeAddOp(v, op, 1, dest);
1410 break;
1411 }
1412 case TK_IN: {
1413 int addr;
1414 sqliteExprCode(pParse, pExpr->pLeft);
1415 addr = sqliteVdbeCurrentAddr(v);
1416 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1417 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1418 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
1419 if( pExpr->pSelect ){
1420 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
1421 }else{
1422 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
1423 }
1424 break;
1425 }
1426 case TK_BETWEEN: {
1427 int addr;
1428 sqliteExprCode(pParse, pExpr->pLeft);
1429 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1430 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1431 addr = sqliteVdbeCurrentAddr(v);
1432 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
1433 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1434 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
1435 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1436 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
1437 break;
1438 }
1439 default: {
1440 sqliteExprCode(pParse, pExpr);
1441 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
1442 break;
1443 }
1444 }
1445}
1446
1447/*
1448** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1449** if they are identical and return FALSE if they differ in any way.
1450*/
1451int sqliteExprCompare(Expr *pA, Expr *pB){
1452 int i;
1453 if( pA==0 ){
1454 return pB==0;
1455 }else if( pB==0 ){
1456 return 0;
1457 }
1458 if( pA->op!=pB->op ) return 0;
1459 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1460 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
1461 if( pA->pList ){
1462 if( pB->pList==0 ) return 0;
1463 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1464 for(i=0; i<pA->pList->nExpr; i++){
1465 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
1466 return 0;
1467 }
1468 }
1469 }else if( pB->pList ){
1470 return 0;
1471 }
1472 if( pA->pSelect || pB->pSelect ) return 0;
1473 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
1474 if( pA->token.z ){
1475 if( pB->token.z==0 ) return 0;
1476 if( pB->token.n!=pA->token.n ) return 0;
1477 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
1478 }
1479 return 1;
1480}
1481
1482/*
1483** Add a new element to the pParse->aAgg[] array and return its index.
1484*/
1485static int appendAggInfo(Parse *pParse){
1486 if( (pParse->nAgg & 0x7)==0 ){
1487 int amt = pParse->nAgg + 8;
1488 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1489 if( aAgg==0 ){
1490 return -1;
1491 }
1492 pParse->aAgg = aAgg;
1493 }
1494 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1495 return pParse->nAgg++;
1496}
1497
1498/*
1499** Analyze the given expression looking for aggregate functions and
1500** for variables that need to be added to the pParse->aAgg[] array.
1501** Make additional entries to the pParse->aAgg[] array as necessary.
1502**
1503** This routine should only be called after the expression has been
1504** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1505**
1506** If errors are seen, leave an error message in zErrMsg and return
1507** the number of errors.
1508*/
1509int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1510 int i;
1511 AggExpr *aAgg;
1512 int nErr = 0;
1513
1514 if( pExpr==0 ) return 0;
1515 switch( pExpr->op ){
1516 case TK_COLUMN: {
1517 aAgg = pParse->aAgg;
1518 for(i=0; i<pParse->nAgg; i++){
1519 if( aAgg[i].isAgg ) continue;
1520 if( aAgg[i].pExpr->iTable==pExpr->iTable
1521 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
1522 break;
1523 }
1524 }
1525 if( i>=pParse->nAgg ){
1526 i = appendAggInfo(pParse);
1527 if( i<0 ) return 1;
1528 pParse->aAgg[i].isAgg = 0;
1529 pParse->aAgg[i].pExpr = pExpr;
1530 }
1531 pExpr->iAgg = i;
1532 break;
1533 }
1534 case TK_AGG_FUNCTION: {
1535 aAgg = pParse->aAgg;
1536 for(i=0; i<pParse->nAgg; i++){
1537 if( !aAgg[i].isAgg ) continue;
1538 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
1539 break;
1540 }
1541 }
1542 if( i>=pParse->nAgg ){
1543 i = appendAggInfo(pParse);
1544 if( i<0 ) return 1;
1545 pParse->aAgg[i].isAgg = 1;
1546 pParse->aAgg[i].pExpr = pExpr;
1547 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
1548 pExpr->token.z, pExpr->token.n,
1549 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
1550 }
1551 pExpr->iAgg = i;
1552 break;
1553 }
1554 default: {
1555 if( pExpr->pLeft ){
1556 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1557 }
1558 if( nErr==0 && pExpr->pRight ){
1559 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1560 }
1561 if( nErr==0 && pExpr->pList ){
1562 int n = pExpr->pList->nExpr;
1563 int i;
1564 for(i=0; nErr==0 && i<n; i++){
1565 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1566 }
1567 }
1568 break;
1569 }
1570 }
1571 return nErr;
1572}
1573
1574/*
1575** Locate a user function given a name and a number of arguments.
1576** Return a pointer to the FuncDef structure that defines that
1577** function, or return NULL if the function does not exist.
1578**
1579** If the createFlag argument is true, then a new (blank) FuncDef
1580** structure is created and liked into the "db" structure if a
1581** no matching function previously existed. When createFlag is true
1582** and the nArg parameter is -1, then only a function that accepts
1583** any number of arguments will be returned.
1584**
1585** If createFlag is false and nArg is -1, then the first valid
1586** function found is returned. A function is valid if either xFunc
1587** or xStep is non-zero.
1588*/
1589FuncDef *sqliteFindFunction(
1590 sqlite *db, /* An open database */
1591 const char *zName, /* Name of the function. Not null-terminated */
1592 int nName, /* Number of characters in the name */
1593 int nArg, /* Number of arguments. -1 means any number */
1594 int createFlag /* Create new entry if true and does not otherwise exist */
1595){
1596 FuncDef *pFirst, *p, *pMaybe;
1597 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
1598 if( p && !createFlag && nArg<0 ){
1599 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1600 return p;
1601 }
1602 pMaybe = 0;
1603 while( p && p->nArg!=nArg ){
1604 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1605 p = p->pNext;
1606 }
1607 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1608 return 0;
1609 }
1610 if( p==0 && pMaybe ){
1611 assert( createFlag==0 );
1612 return pMaybe;
1613 }
1614 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
1615 p->nArg = nArg;
1616 p->pNext = pFirst;
1617 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
1618 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
1619 }
1620 return p;
1621}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status