blob: 857d7277c66f2527074c9808b53704830a26dadd [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** This file contains C code routines used for processing expressions
25**
drhcfab11b2000-06-06 03:31:22 +000026** $Id: expr.c,v 1.8 2000/06/06 03:31:22 drh Exp $
drhcce7d172000-05-31 15:34:51 +000027*/
28#include "sqliteInt.h"
29
30/*
drhfef52082000-06-06 01:50:43 +000031** Walk an expression tree. Return 1 if the expression is constant
32** and 0 if it involves variables.
33*/
34static int isConstant(Expr *p){
35 switch( p->op ){
36 case TK_ID:
37 case TK_FIELD:
38 case TK_DOT:
39 return 0;
40 default: {
41 if( p->pLeft && !isConstant(p->pLeft) ) return 0;
42 if( p->pRight && !isConstant(p->pRight) ) return 0;
43 if( p->pList ){
44 int i;
45 for(i=0; i<p->pList->nExpr; i++){
46 if( !isConstant(p->pList->a[i].pExpr) ) return 0;
47 }
48 }
49 break;
50 }
51 }
52 return 1;
53}
54
55/*
drhcce7d172000-05-31 15:34:51 +000056** This routine walks an expression tree and resolves references to
57** table fields. Nodes of the form ID.ID or ID resolve into an
58** index to the table in the table list and a field offset. The opcode
59** for such nodes is changed to TK_FIELD. The iTable value is changed
drh19a775c2000-06-05 18:54:46 +000060** to the index of the referenced table in pTabList plus the pParse->nTab
61** value. The iField value is changed to the index of the field of the
62** referenced table.
63**
drhfef52082000-06-06 01:50:43 +000064** We also check for instances of the IN operator. IN comes in two
65** forms:
66**
67** expr IN (exprlist)
68** and
69** expr IN (SELECT ...)
70**
71** The first form is handled by creating a set holding the list
72** of allowed values. The second form causes the SELECT to generate
73** a temporary table.
74**
75** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +000076** If it finds any, it generates code to write the value of that select
77** into a memory cell.
drhcce7d172000-05-31 15:34:51 +000078**
79** Unknown fields or tables provoke an error. The function returns
80** the number of errors seen and leaves an error message on pParse->zErrMsg.
81*/
82int sqliteExprResolveIds(Parse *pParse, IdList *pTabList, Expr *pExpr){
83 if( pExpr==0 ) return 0;
84 switch( pExpr->op ){
85 /* A lone identifier */
86 case TK_ID: {
87 int cnt = 0; /* Number of matches */
88 int i; /* Loop counter */
89 char *z = 0;
90 sqliteSetNString(&z, pExpr->token.z, pExpr->token.n, 0);
91 for(i=0; i<pTabList->nId; i++){
92 int j;
93 Table *pTab = pTabList->a[i].pTab;
94 if( pTab==0 ) continue;
95 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +000096 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +000097 cnt++;
drh19a775c2000-06-05 18:54:46 +000098 pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +000099 pExpr->iField = j;
100 }
101 }
102 }
103 sqliteFree(z);
104 if( cnt==0 ){
105 sqliteSetNString(&pParse->zErrMsg, "no such field: ", -1,
106 pExpr->token.z, pExpr->token.n, 0);
107 pParse->nErr++;
108 return 1;
109 }else if( cnt>1 ){
110 sqliteSetNString(&pParse->zErrMsg, "ambiguous field name: ", -1,
111 pExpr->token.z, pExpr->token.n, 0);
112 pParse->nErr++;
113 return 1;
114 }
115 pExpr->op = TK_FIELD;
116 break;
117 }
118
119 /* A table name and field name: ID.ID */
120 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000121 int cnt = 0; /* Number of matches */
122 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000123 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000124 char *zLeft, *zRight; /* Text of an identifier */
125
126 pLeft = pExpr->pLeft;
127 pRight = pExpr->pRight;
128 assert( pLeft && pLeft->op==TK_ID );
129 assert( pRight && pRight->op==TK_ID );
130 zLeft = 0;
131 sqliteSetNString(&zLeft, pLeft->token.z, pLeft->token.n, 0);
132 zRight = 0;
133 sqliteSetNString(&zRight, pRight->token.z, pRight->token.n, 0);
134 for(i=0; i<pTabList->nId; i++){
135 int j;
136 char *zTab;
137 Table *pTab = pTabList->a[i].pTab;
138 if( pTab==0 ) continue;
139 if( pTabList->a[i].zAlias ){
140 zTab = pTabList->a[i].zAlias;
141 }else{
142 zTab = pTab->zName;
143 }
144 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
145 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000146 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000147 cnt++;
drh19a775c2000-06-05 18:54:46 +0000148 pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000149 pExpr->iField = j;
150 }
151 }
152 }
153 sqliteFree(zLeft);
154 sqliteFree(zRight);
155 if( cnt==0 ){
156 sqliteSetNString(&pParse->zErrMsg, "no such field: ", -1,
157 pLeft->token.z, pLeft->token.n, ".", 1,
158 pRight->token.z, pRight->token.n, 0);
159 pParse->nErr++;
160 return 1;
161 }else if( cnt>1 ){
162 sqliteSetNString(&pParse->zErrMsg, "ambiguous field name: ", -1,
163 pLeft->token.z, pLeft->token.n, ".", 1,
164 pRight->token.z, pRight->token.n, 0);
165 pParse->nErr++;
166 return 1;
167 }
168 sqliteExprDelete(pLeft);
169 pExpr->pLeft = 0;
170 sqliteExprDelete(pRight);
171 pExpr->pRight = 0;
172 pExpr->op = TK_FIELD;
173 break;
174 }
175
drhfef52082000-06-06 01:50:43 +0000176 case TK_IN: {
177 Vdbe *v = pParse->pVdbe;
178 if( v==0 ){
179 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe);
180 }
181 if( v==0 ) return 1;
drhcfab11b2000-06-06 03:31:22 +0000182 if( sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
183 return 1;
184 }
drhfef52082000-06-06 01:50:43 +0000185 if( pExpr->pSelect ){
186 /* Case 1: expr IN (SELECT ...)
187 **
188 ** Generate code to write the results of the select into a temporary
189 ** table. The cursor number of the temporary table is stored in
190 ** iTable.
191 */
192 pExpr->iTable = pParse->nTab++;
193 sqliteVdbeAddOp(v, OP_Open, pExpr->iTable, 0, 0, 0);
194 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
195 }else if( pExpr->pList ){
196 /* Case 2: expr IN (exprlist)
197 **
198 ** Create a set to put the exprlist values in. The Set id is stored
199 ** in iTable.
200 */
201 int i, iSet;
202 for(i=0; i<pExpr->pList->nExpr; i++){
203 Expr *pE2 = pExpr->pList->a[i].pExpr;
204 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
205 return 1;
206 }
207 if( !isConstant(pE2) ){
208 sqliteSetString(&pParse->zErrMsg,
209 "right-hand side of IN operator must be constant", 0);
210 pParse->nErr++;
211 return 1;
212 }
213 }
214 iSet = pExpr->iTable = pParse->nSet++;
215 for(i=0; i<pExpr->pList->nExpr; i++){
216 Expr *pE2 = pExpr->pList->a[i].pExpr;
217 switch( pE2->op ){
218 case TK_FLOAT:
219 case TK_INTEGER:
220 case TK_STRING: {
221 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
222 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
223 sqliteVdbeDequoteP3(v, addr);
224 break;
225 }
226 default: {
227 sqliteExprCode(pParse, pE2);
228 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
229 break;
230 }
231 }
232 }
233 }
drhcfab11b2000-06-06 03:31:22 +0000234 break;
drhfef52082000-06-06 01:50:43 +0000235 }
236
drh19a775c2000-06-05 18:54:46 +0000237 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000238 /* This has to be a scalar SELECT. Generate code to put the
239 ** value of this select in a memory cell and record the number
240 ** of the memory cell in iField.
241 */
drh19a775c2000-06-05 18:54:46 +0000242 pExpr->iField = pParse->nMem++;
drhfef52082000-06-06 01:50:43 +0000243 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iField) ){
drh19a775c2000-06-05 18:54:46 +0000244 return 1;
245 }
246 break;
247 }
248
drhcce7d172000-05-31 15:34:51 +0000249 /* For all else, just recursively walk the tree */
250 default: {
251 if( pExpr->pLeft
252 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
253 return 1;
254 }
255 if( pExpr->pRight
256 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
257 return 1;
258 }
259 if( pExpr->pList ){
260 int i;
261 ExprList *pList = pExpr->pList;
262 for(i=0; i<pList->nExpr; i++){
263 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
264 return 1;
265 }
266 }
267 }
268 }
269 }
270 return 0;
271}
272
273#if 0 /* NOT USED */
274/*
275** Compare a token against a string. Return TRUE if they match.
276*/
277static int sqliteTokenCmp(Token *pToken, const char *zStr){
278 int n = strlen(zStr);
279 if( n!=pToken->n ) return 0;
280 return sqliteStrNICmp(pToken->z, zStr, n)==0;
281}
282#endif
283
284/*
285** Convert a function name into its integer identifier. Return the
286** identifier. Return FN_Unknown if the function name is unknown.
287*/
288int sqliteFuncId(Token *pToken){
289 static const struct {
290 char *zName;
291 int len;
292 int id;
293 } aFunc[] = {
294 { "count", 5, FN_Count },
295 { "min", 3, FN_Min },
296 { "max", 3, FN_Max },
297 { "sum", 3, FN_Sum },
298 };
299 int i;
300 for(i=0; i<ArraySize(aFunc); i++){
301 if( aFunc[i].len==pToken->n
302 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
303 return aFunc[i].id;
304 }
305 }
306 return FN_Unknown;
307}
308
309/*
310** Error check the functions in an expression. Make sure all
311** function names are recognized and all functions have the correct
312** number of arguments. Leave an error message in pParse->zErrMsg
313** if anything is amiss. Return the number of errors.
314**
315** if pIsAgg is not null and this expression is an aggregate function
316** (like count(*) or max(value)) then write a 1 into *pIsAgg.
317*/
318int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
319 int nErr = 0;
320 if( pExpr==0 ) return 0;
321 if( pIsAgg ) *pIsAgg = 0;
322 switch( pExpr->op ){
323 case TK_FUNCTION: {
324 int id = sqliteFuncId(&pExpr->token);
325 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
326 int no_such_func = 0;
327 int too_many_args = 0;
328 int too_few_args = 0;
329 int is_agg = 0;
330 int i;
331 switch( id ){
332 case FN_Unknown: {
333 no_such_func = 1;
334 break;
335 }
336 case FN_Count: {
337 no_such_func = !allowAgg;
338 too_many_args = n>1;
339 is_agg = 1;
340 break;
341 }
342 case FN_Max:
343 case FN_Min: {
344 too_few_args = allowAgg ? n<1 : n<2;
345 is_agg = n==1;
346 break;
347 }
348 case FN_Sum: {
349 no_such_func = !allowAgg;
350 too_many_args = n>1;
351 too_few_args = n<1;
352 is_agg = 1;
353 break;
354 }
355 default: break;
356 }
357 if( no_such_func ){
358 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
359 pExpr->token.z, pExpr->token.n, 0);
360 pParse->nErr++;
361 nErr++;
362 }else if( too_many_args ){
363 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
364 pExpr->token.z, pExpr->token.n, "()", 2, 0);
365 pParse->nErr++;
366 nErr++;
367 }else if( too_few_args ){
368 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
369 pExpr->token.z, pExpr->token.n, "()", 2, 0);
370 pParse->nErr++;
371 nErr++;
372 }
373 if( is_agg && pIsAgg ) *pIsAgg = 1;
374 for(i=0; nErr==0 && i<n; i++){
375 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 0, 0);
376 }
377 }
378 default: {
379 if( pExpr->pLeft ){
380 nErr = sqliteExprCheck(pParse, pExpr->pLeft, 0, 0);
381 }
382 if( nErr==0 && pExpr->pRight ){
383 nErr = sqliteExprCheck(pParse, pExpr->pRight, 0, 0);
384 }
drhfef52082000-06-06 01:50:43 +0000385 if( nErr==0 && pExpr->pList ){
386 int n = pExpr->pList->nExpr;
387 int i;
388 for(i=0; nErr==0 && i<n; i++){
389 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 0, 0);
390 }
391 }
drhcce7d172000-05-31 15:34:51 +0000392 break;
393 }
394 }
395 return nErr;
396}
397
398/*
399** Generate code into the current Vdbe to evaluate the given
400** expression and leave the result on the stack.
401*/
402void sqliteExprCode(Parse *pParse, Expr *pExpr){
403 Vdbe *v = pParse->pVdbe;
404 int op;
405 switch( pExpr->op ){
406 case TK_PLUS: op = OP_Add; break;
407 case TK_MINUS: op = OP_Subtract; break;
408 case TK_STAR: op = OP_Multiply; break;
409 case TK_SLASH: op = OP_Divide; break;
410 case TK_AND: op = OP_And; break;
411 case TK_OR: op = OP_Or; break;
412 case TK_LT: op = OP_Lt; break;
413 case TK_LE: op = OP_Le; break;
414 case TK_GT: op = OP_Gt; break;
415 case TK_GE: op = OP_Ge; break;
416 case TK_NE: op = OP_Ne; break;
417 case TK_EQ: op = OP_Eq; break;
418 case TK_LIKE: op = OP_Like; break;
419 case TK_GLOB: op = OP_Glob; break;
420 case TK_ISNULL: op = OP_IsNull; break;
421 case TK_NOTNULL: op = OP_NotNull; break;
422 case TK_NOT: op = OP_Not; break;
423 case TK_UMINUS: op = OP_Negative; break;
424 default: break;
425 }
426 switch( pExpr->op ){
427 case TK_FIELD: {
428 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iField, 0, 0);
429 break;
430 }
431 case TK_INTEGER: {
432 int i = atoi(pExpr->token.z);
433 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
434 break;
435 }
436 case TK_FLOAT: {
437 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
438 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
439 break;
440 }
441 case TK_STRING: {
442 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
443 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
444 sqliteVdbeDequoteP3(v, addr);
445 break;
446 }
447 case TK_NULL: {
drhc61053b2000-06-04 12:58:36 +0000448 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000449 break;
450 }
451 case TK_AND:
452 case TK_OR:
453 case TK_PLUS:
454 case TK_STAR:
455 case TK_MINUS:
456 case TK_SLASH: {
457 sqliteExprCode(pParse, pExpr->pLeft);
458 sqliteExprCode(pParse, pExpr->pRight);
459 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
460 break;
461 }
462 case TK_LT:
463 case TK_LE:
464 case TK_GT:
465 case TK_GE:
466 case TK_NE:
467 case TK_EQ:
468 case TK_LIKE:
469 case TK_GLOB: {
470 int dest;
471 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
472 sqliteExprCode(pParse, pExpr->pLeft);
473 sqliteExprCode(pParse, pExpr->pRight);
474 dest = sqliteVdbeCurrentAddr(v) + 2;
475 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
476 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
477 break;
478 }
479 case TK_NOT:
480 case TK_UMINUS: {
481 sqliteExprCode(pParse, pExpr->pLeft);
482 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
483 break;
484 }
485 case TK_ISNULL:
486 case TK_NOTNULL: {
487 int dest;
drh8be51132000-06-03 19:19:41 +0000488 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000489 sqliteExprCode(pParse, pExpr->pLeft);
490 dest = sqliteVdbeCurrentAddr(v) + 2;
491 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
drh8be51132000-06-03 19:19:41 +0000492 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000493 break;
494 }
495 case TK_FUNCTION: {
496 int id = sqliteFuncId(&pExpr->token);
497 int op;
498 int i;
499 ExprList *pList = pExpr->pList;
500 op = id==FN_Min ? OP_Min : OP_Max;
501 for(i=0; i<pList->nExpr; i++){
502 sqliteExprCode(pParse, pList->a[i].pExpr);
503 if( i>0 ){
504 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
505 }
506 }
507 break;
508 }
drh19a775c2000-06-05 18:54:46 +0000509 case TK_SELECT: {
510 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iField, 0, 0, 0);
511 break;
512 }
drhfef52082000-06-06 01:50:43 +0000513 case TK_IN: {
514 int addr;
515 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
516 sqliteExprCode(pParse, pExpr->pLeft);
517 addr = sqliteVdbeCurrentAddr(v);
518 if( pExpr->pSelect ){
519 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2, 0, 0);
520 }else{
521 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2, 0, 0);
522 }
523 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
524 break;
525 }
526 case TK_BETWEEN: {
527 int lbl = sqliteVdbeMakeLabel(v);
528 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
529 sqliteExprIfFalse(pParse, pExpr, lbl);
530 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
531 sqliteVdbeResolveLabel(v, lbl);
532 break;
533 }
drhcce7d172000-05-31 15:34:51 +0000534 }
535 return;
536}
537
538/*
539** Generate code for a boolean expression such that a jump is made
540** to the label "dest" if the expression is true but execution
541** continues straight thru if the expression is false.
542*/
543void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
544 Vdbe *v = pParse->pVdbe;
545 int op = 0;
546 switch( pExpr->op ){
547 case TK_LT: op = OP_Lt; break;
548 case TK_LE: op = OP_Le; break;
549 case TK_GT: op = OP_Gt; break;
550 case TK_GE: op = OP_Ge; break;
551 case TK_NE: op = OP_Ne; break;
552 case TK_EQ: op = OP_Eq; break;
553 case TK_LIKE: op = OP_Like; break;
554 case TK_GLOB: op = OP_Glob; break;
555 case TK_ISNULL: op = OP_IsNull; break;
556 case TK_NOTNULL: op = OP_NotNull; break;
557 default: break;
558 }
559 switch( pExpr->op ){
560 case TK_AND: {
561 int d2 = sqliteVdbeMakeLabel(v);
562 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
563 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
564 sqliteVdbeResolveLabel(v, d2);
565 break;
566 }
567 case TK_OR: {
568 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
569 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
570 break;
571 }
572 case TK_NOT: {
573 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
574 break;
575 }
576 case TK_LT:
577 case TK_LE:
578 case TK_GT:
579 case TK_GE:
580 case TK_NE:
581 case TK_EQ:
582 case TK_LIKE:
583 case TK_GLOB: {
584 sqliteExprCode(pParse, pExpr->pLeft);
585 sqliteExprCode(pParse, pExpr->pRight);
586 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
587 break;
588 }
589 case TK_ISNULL:
590 case TK_NOTNULL: {
591 sqliteExprCode(pParse, pExpr->pLeft);
592 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
593 break;
594 }
drhfef52082000-06-06 01:50:43 +0000595 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000596 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000597 if( pExpr->pSelect ){
598 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest, 0, 0);
599 }else{
600 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest, 0, 0);
601 }
602 break;
603 }
604 case TK_BETWEEN: {
605 int lbl = sqliteVdbeMakeLabel(v);
606 sqliteExprCode(pParse, pExpr->pLeft);
607 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
608 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
609 sqliteVdbeAddOp(v, OP_Lt, 0, lbl, 0, 0);
610 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
611 sqliteVdbeAddOp(v, OP_Le, 0, dest, 0, 0);
612 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
613 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, lbl);
614 break;
615 }
drhcce7d172000-05-31 15:34:51 +0000616 default: {
617 sqliteExprCode(pParse, pExpr);
618 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
619 break;
620 }
621 }
622}
623
624/*
625** Generate code for boolean expression such that a jump is made
626** to the label "dest" if the expression is false but execution
627** continues straight thru if the expression is true.
628*/
629void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
630 Vdbe *v = pParse->pVdbe;
631 int op = 0;
632 switch( pExpr->op ){
633 case TK_LT: op = OP_Ge; break;
634 case TK_LE: op = OP_Gt; break;
635 case TK_GT: op = OP_Le; break;
636 case TK_GE: op = OP_Lt; break;
637 case TK_NE: op = OP_Eq; break;
638 case TK_EQ: op = OP_Ne; break;
639 case TK_LIKE: op = OP_Like; break;
640 case TK_GLOB: op = OP_Glob; break;
641 case TK_ISNULL: op = OP_NotNull; break;
642 case TK_NOTNULL: op = OP_IsNull; break;
643 default: break;
644 }
645 switch( pExpr->op ){
646 case TK_AND: {
647 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
648 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
649 break;
650 }
651 case TK_OR: {
652 int d2 = sqliteVdbeMakeLabel(v);
653 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
654 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
655 sqliteVdbeResolveLabel(v, d2);
656 break;
657 }
658 case TK_NOT: {
659 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
660 break;
661 }
662 case TK_LT:
663 case TK_LE:
664 case TK_GT:
665 case TK_GE:
666 case TK_NE:
667 case TK_EQ: {
668 sqliteExprCode(pParse, pExpr->pLeft);
669 sqliteExprCode(pParse, pExpr->pRight);
670 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
671 break;
672 }
673 case TK_LIKE:
674 case TK_GLOB: {
675 sqliteExprCode(pParse, pExpr->pLeft);
676 sqliteExprCode(pParse, pExpr->pRight);
677 sqliteVdbeAddOp(v, op, 1, dest, 0, 0);
678 break;
679 }
680 case TK_ISNULL:
681 case TK_NOTNULL: {
682 sqliteExprCode(pParse, pExpr->pLeft);
683 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
684 break;
685 }
drhfef52082000-06-06 01:50:43 +0000686 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000687 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000688 if( pExpr->pSelect ){
689 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest, 0, 0);
690 }else{
691 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest, 0, 0);
692 }
693 break;
694 }
695 case TK_BETWEEN: {
696 int addr;
697 sqliteExprCode(pParse, pExpr->pLeft);
698 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
699 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
700 addr = sqliteVdbeCurrentAddr(v);
701 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3, 0, 0);
702 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, 0);
703 sqliteVdbeAddOp(v, OP_Goto, 0, dest, 0, 0);
704 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
705 sqliteVdbeAddOp(v, OP_Gt, 0, dest, 0, 0);
706 break;
707 }
drhcce7d172000-05-31 15:34:51 +0000708 default: {
709 sqliteExprCode(pParse, pExpr);
710 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
711 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
712 break;
713 }
714 }
715}