blob: 1b3e8527f5920eed58571632e2ea48a60e516d6b [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**
drhfef52082000-06-06 01:50:43 +000026** $Id: expr.c,v 1.7 2000/06/06 01:50:43 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;
182 if( pExpr->pSelect ){
183 /* Case 1: expr IN (SELECT ...)
184 **
185 ** Generate code to write the results of the select into a temporary
186 ** table. The cursor number of the temporary table is stored in
187 ** iTable.
188 */
189 pExpr->iTable = pParse->nTab++;
190 sqliteVdbeAddOp(v, OP_Open, pExpr->iTable, 0, 0, 0);
191 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
192 }else if( pExpr->pList ){
193 /* Case 2: expr IN (exprlist)
194 **
195 ** Create a set to put the exprlist values in. The Set id is stored
196 ** in iTable.
197 */
198 int i, iSet;
199 for(i=0; i<pExpr->pList->nExpr; i++){
200 Expr *pE2 = pExpr->pList->a[i].pExpr;
201 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
202 return 1;
203 }
204 if( !isConstant(pE2) ){
205 sqliteSetString(&pParse->zErrMsg,
206 "right-hand side of IN operator must be constant", 0);
207 pParse->nErr++;
208 return 1;
209 }
210 }
211 iSet = pExpr->iTable = pParse->nSet++;
212 for(i=0; i<pExpr->pList->nExpr; i++){
213 Expr *pE2 = pExpr->pList->a[i].pExpr;
214 switch( pE2->op ){
215 case TK_FLOAT:
216 case TK_INTEGER:
217 case TK_STRING: {
218 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
219 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
220 sqliteVdbeDequoteP3(v, addr);
221 break;
222 }
223 default: {
224 sqliteExprCode(pParse, pE2);
225 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
226 break;
227 }
228 }
229 }
230 }
231 }
232
drh19a775c2000-06-05 18:54:46 +0000233 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000234 /* This has to be a scalar SELECT. Generate code to put the
235 ** value of this select in a memory cell and record the number
236 ** of the memory cell in iField.
237 */
drh19a775c2000-06-05 18:54:46 +0000238 pExpr->iField = pParse->nMem++;
drhfef52082000-06-06 01:50:43 +0000239 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iField) ){
drh19a775c2000-06-05 18:54:46 +0000240 return 1;
241 }
242 break;
243 }
244
drhcce7d172000-05-31 15:34:51 +0000245 /* For all else, just recursively walk the tree */
246 default: {
247 if( pExpr->pLeft
248 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
249 return 1;
250 }
251 if( pExpr->pRight
252 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
253 return 1;
254 }
255 if( pExpr->pList ){
256 int i;
257 ExprList *pList = pExpr->pList;
258 for(i=0; i<pList->nExpr; i++){
259 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
260 return 1;
261 }
262 }
263 }
264 }
265 }
266 return 0;
267}
268
269#if 0 /* NOT USED */
270/*
271** Compare a token against a string. Return TRUE if they match.
272*/
273static int sqliteTokenCmp(Token *pToken, const char *zStr){
274 int n = strlen(zStr);
275 if( n!=pToken->n ) return 0;
276 return sqliteStrNICmp(pToken->z, zStr, n)==0;
277}
278#endif
279
280/*
281** Convert a function name into its integer identifier. Return the
282** identifier. Return FN_Unknown if the function name is unknown.
283*/
284int sqliteFuncId(Token *pToken){
285 static const struct {
286 char *zName;
287 int len;
288 int id;
289 } aFunc[] = {
290 { "count", 5, FN_Count },
291 { "min", 3, FN_Min },
292 { "max", 3, FN_Max },
293 { "sum", 3, FN_Sum },
294 };
295 int i;
296 for(i=0; i<ArraySize(aFunc); i++){
297 if( aFunc[i].len==pToken->n
298 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
299 return aFunc[i].id;
300 }
301 }
302 return FN_Unknown;
303}
304
305/*
306** Error check the functions in an expression. Make sure all
307** function names are recognized and all functions have the correct
308** number of arguments. Leave an error message in pParse->zErrMsg
309** if anything is amiss. Return the number of errors.
310**
311** if pIsAgg is not null and this expression is an aggregate function
312** (like count(*) or max(value)) then write a 1 into *pIsAgg.
313*/
314int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
315 int nErr = 0;
316 if( pExpr==0 ) return 0;
317 if( pIsAgg ) *pIsAgg = 0;
318 switch( pExpr->op ){
319 case TK_FUNCTION: {
320 int id = sqliteFuncId(&pExpr->token);
321 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
322 int no_such_func = 0;
323 int too_many_args = 0;
324 int too_few_args = 0;
325 int is_agg = 0;
326 int i;
327 switch( id ){
328 case FN_Unknown: {
329 no_such_func = 1;
330 break;
331 }
332 case FN_Count: {
333 no_such_func = !allowAgg;
334 too_many_args = n>1;
335 is_agg = 1;
336 break;
337 }
338 case FN_Max:
339 case FN_Min: {
340 too_few_args = allowAgg ? n<1 : n<2;
341 is_agg = n==1;
342 break;
343 }
344 case FN_Sum: {
345 no_such_func = !allowAgg;
346 too_many_args = n>1;
347 too_few_args = n<1;
348 is_agg = 1;
349 break;
350 }
351 default: break;
352 }
353 if( no_such_func ){
354 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
355 pExpr->token.z, pExpr->token.n, 0);
356 pParse->nErr++;
357 nErr++;
358 }else if( too_many_args ){
359 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
360 pExpr->token.z, pExpr->token.n, "()", 2, 0);
361 pParse->nErr++;
362 nErr++;
363 }else if( too_few_args ){
364 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
365 pExpr->token.z, pExpr->token.n, "()", 2, 0);
366 pParse->nErr++;
367 nErr++;
368 }
369 if( is_agg && pIsAgg ) *pIsAgg = 1;
370 for(i=0; nErr==0 && i<n; i++){
371 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 0, 0);
372 }
373 }
374 default: {
375 if( pExpr->pLeft ){
376 nErr = sqliteExprCheck(pParse, pExpr->pLeft, 0, 0);
377 }
378 if( nErr==0 && pExpr->pRight ){
379 nErr = sqliteExprCheck(pParse, pExpr->pRight, 0, 0);
380 }
drhfef52082000-06-06 01:50:43 +0000381 if( nErr==0 && pExpr->pList ){
382 int n = pExpr->pList->nExpr;
383 int i;
384 for(i=0; nErr==0 && i<n; i++){
385 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 0, 0);
386 }
387 }
drhcce7d172000-05-31 15:34:51 +0000388 break;
389 }
390 }
391 return nErr;
392}
393
394/*
395** Generate code into the current Vdbe to evaluate the given
396** expression and leave the result on the stack.
397*/
398void sqliteExprCode(Parse *pParse, Expr *pExpr){
399 Vdbe *v = pParse->pVdbe;
400 int op;
401 switch( pExpr->op ){
402 case TK_PLUS: op = OP_Add; break;
403 case TK_MINUS: op = OP_Subtract; break;
404 case TK_STAR: op = OP_Multiply; break;
405 case TK_SLASH: op = OP_Divide; break;
406 case TK_AND: op = OP_And; break;
407 case TK_OR: op = OP_Or; break;
408 case TK_LT: op = OP_Lt; break;
409 case TK_LE: op = OP_Le; break;
410 case TK_GT: op = OP_Gt; break;
411 case TK_GE: op = OP_Ge; break;
412 case TK_NE: op = OP_Ne; break;
413 case TK_EQ: op = OP_Eq; break;
414 case TK_LIKE: op = OP_Like; break;
415 case TK_GLOB: op = OP_Glob; break;
416 case TK_ISNULL: op = OP_IsNull; break;
417 case TK_NOTNULL: op = OP_NotNull; break;
418 case TK_NOT: op = OP_Not; break;
419 case TK_UMINUS: op = OP_Negative; break;
420 default: break;
421 }
422 switch( pExpr->op ){
423 case TK_FIELD: {
424 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iField, 0, 0);
425 break;
426 }
427 case TK_INTEGER: {
428 int i = atoi(pExpr->token.z);
429 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
430 break;
431 }
432 case TK_FLOAT: {
433 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
434 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
435 break;
436 }
437 case TK_STRING: {
438 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
439 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
440 sqliteVdbeDequoteP3(v, addr);
441 break;
442 }
443 case TK_NULL: {
drhc61053b2000-06-04 12:58:36 +0000444 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000445 break;
446 }
447 case TK_AND:
448 case TK_OR:
449 case TK_PLUS:
450 case TK_STAR:
451 case TK_MINUS:
452 case TK_SLASH: {
453 sqliteExprCode(pParse, pExpr->pLeft);
454 sqliteExprCode(pParse, pExpr->pRight);
455 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
456 break;
457 }
458 case TK_LT:
459 case TK_LE:
460 case TK_GT:
461 case TK_GE:
462 case TK_NE:
463 case TK_EQ:
464 case TK_LIKE:
465 case TK_GLOB: {
466 int dest;
467 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
468 sqliteExprCode(pParse, pExpr->pLeft);
469 sqliteExprCode(pParse, pExpr->pRight);
470 dest = sqliteVdbeCurrentAddr(v) + 2;
471 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
472 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
473 break;
474 }
475 case TK_NOT:
476 case TK_UMINUS: {
477 sqliteExprCode(pParse, pExpr->pLeft);
478 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
479 break;
480 }
481 case TK_ISNULL:
482 case TK_NOTNULL: {
483 int dest;
drh8be51132000-06-03 19:19:41 +0000484 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000485 sqliteExprCode(pParse, pExpr->pLeft);
486 dest = sqliteVdbeCurrentAddr(v) + 2;
487 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
drh8be51132000-06-03 19:19:41 +0000488 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000489 break;
490 }
491 case TK_FUNCTION: {
492 int id = sqliteFuncId(&pExpr->token);
493 int op;
494 int i;
495 ExprList *pList = pExpr->pList;
496 op = id==FN_Min ? OP_Min : OP_Max;
497 for(i=0; i<pList->nExpr; i++){
498 sqliteExprCode(pParse, pList->a[i].pExpr);
499 if( i>0 ){
500 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
501 }
502 }
503 break;
504 }
drh19a775c2000-06-05 18:54:46 +0000505 case TK_SELECT: {
506 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iField, 0, 0, 0);
507 break;
508 }
drhfef52082000-06-06 01:50:43 +0000509 case TK_IN: {
510 int addr;
511 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
512 sqliteExprCode(pParse, pExpr->pLeft);
513 addr = sqliteVdbeCurrentAddr(v);
514 if( pExpr->pSelect ){
515 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2, 0, 0);
516 }else{
517 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2, 0, 0);
518 }
519 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
520 break;
521 }
522 case TK_BETWEEN: {
523 int lbl = sqliteVdbeMakeLabel(v);
524 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
525 sqliteExprIfFalse(pParse, pExpr, lbl);
526 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
527 sqliteVdbeResolveLabel(v, lbl);
528 break;
529 }
drhcce7d172000-05-31 15:34:51 +0000530 }
531 return;
532}
533
534/*
535** Generate code for a boolean expression such that a jump is made
536** to the label "dest" if the expression is true but execution
537** continues straight thru if the expression is false.
538*/
539void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
540 Vdbe *v = pParse->pVdbe;
541 int op = 0;
542 switch( pExpr->op ){
543 case TK_LT: op = OP_Lt; break;
544 case TK_LE: op = OP_Le; break;
545 case TK_GT: op = OP_Gt; break;
546 case TK_GE: op = OP_Ge; break;
547 case TK_NE: op = OP_Ne; break;
548 case TK_EQ: op = OP_Eq; break;
549 case TK_LIKE: op = OP_Like; break;
550 case TK_GLOB: op = OP_Glob; break;
551 case TK_ISNULL: op = OP_IsNull; break;
552 case TK_NOTNULL: op = OP_NotNull; break;
553 default: break;
554 }
555 switch( pExpr->op ){
556 case TK_AND: {
557 int d2 = sqliteVdbeMakeLabel(v);
558 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
559 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
560 sqliteVdbeResolveLabel(v, d2);
561 break;
562 }
563 case TK_OR: {
564 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
565 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
566 break;
567 }
568 case TK_NOT: {
569 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
570 break;
571 }
572 case TK_LT:
573 case TK_LE:
574 case TK_GT:
575 case TK_GE:
576 case TK_NE:
577 case TK_EQ:
578 case TK_LIKE:
579 case TK_GLOB: {
580 sqliteExprCode(pParse, pExpr->pLeft);
581 sqliteExprCode(pParse, pExpr->pRight);
582 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
583 break;
584 }
585 case TK_ISNULL:
586 case TK_NOTNULL: {
587 sqliteExprCode(pParse, pExpr->pLeft);
588 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
589 break;
590 }
drhfef52082000-06-06 01:50:43 +0000591 case TK_IN: {
592 if( pExpr->pSelect ){
593 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest, 0, 0);
594 }else{
595 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest, 0, 0);
596 }
597 break;
598 }
599 case TK_BETWEEN: {
600 int lbl = sqliteVdbeMakeLabel(v);
601 sqliteExprCode(pParse, pExpr->pLeft);
602 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
603 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
604 sqliteVdbeAddOp(v, OP_Lt, 0, lbl, 0, 0);
605 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
606 sqliteVdbeAddOp(v, OP_Le, 0, dest, 0, 0);
607 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
608 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, lbl);
609 break;
610 }
drhcce7d172000-05-31 15:34:51 +0000611 default: {
612 sqliteExprCode(pParse, pExpr);
613 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
614 break;
615 }
616 }
617}
618
619/*
620** Generate code for boolean expression such that a jump is made
621** to the label "dest" if the expression is false but execution
622** continues straight thru if the expression is true.
623*/
624void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
625 Vdbe *v = pParse->pVdbe;
626 int op = 0;
627 switch( pExpr->op ){
628 case TK_LT: op = OP_Ge; break;
629 case TK_LE: op = OP_Gt; break;
630 case TK_GT: op = OP_Le; break;
631 case TK_GE: op = OP_Lt; break;
632 case TK_NE: op = OP_Eq; break;
633 case TK_EQ: op = OP_Ne; break;
634 case TK_LIKE: op = OP_Like; break;
635 case TK_GLOB: op = OP_Glob; break;
636 case TK_ISNULL: op = OP_NotNull; break;
637 case TK_NOTNULL: op = OP_IsNull; break;
638 default: break;
639 }
640 switch( pExpr->op ){
641 case TK_AND: {
642 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
643 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
644 break;
645 }
646 case TK_OR: {
647 int d2 = sqliteVdbeMakeLabel(v);
648 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
649 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
650 sqliteVdbeResolveLabel(v, d2);
651 break;
652 }
653 case TK_NOT: {
654 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
655 break;
656 }
657 case TK_LT:
658 case TK_LE:
659 case TK_GT:
660 case TK_GE:
661 case TK_NE:
662 case TK_EQ: {
663 sqliteExprCode(pParse, pExpr->pLeft);
664 sqliteExprCode(pParse, pExpr->pRight);
665 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
666 break;
667 }
668 case TK_LIKE:
669 case TK_GLOB: {
670 sqliteExprCode(pParse, pExpr->pLeft);
671 sqliteExprCode(pParse, pExpr->pRight);
672 sqliteVdbeAddOp(v, op, 1, dest, 0, 0);
673 break;
674 }
675 case TK_ISNULL:
676 case TK_NOTNULL: {
677 sqliteExprCode(pParse, pExpr->pLeft);
678 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
679 break;
680 }
drhfef52082000-06-06 01:50:43 +0000681 case TK_IN: {
682 if( pExpr->pSelect ){
683 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest, 0, 0);
684 }else{
685 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest, 0, 0);
686 }
687 break;
688 }
689 case TK_BETWEEN: {
690 int addr;
691 sqliteExprCode(pParse, pExpr->pLeft);
692 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
693 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
694 addr = sqliteVdbeCurrentAddr(v);
695 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3, 0, 0);
696 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, 0);
697 sqliteVdbeAddOp(v, OP_Goto, 0, dest, 0, 0);
698 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
699 sqliteVdbeAddOp(v, OP_Gt, 0, dest, 0, 0);
700 break;
701 }
drhcce7d172000-05-31 15:34:51 +0000702 default: {
703 sqliteExprCode(pParse, pExpr);
704 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
705 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
706 break;
707 }
708 }
709}