blob: ab07a54f633925eda27ec21e0d732fe47702ee30 [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**
drh4794b982000-06-06 13:54:14 +000026** $Id: expr.c,v 1.9 2000/06/06 13:54:15 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/*
drh4794b982000-06-06 13:54:14 +000056** Walk the expression tree and process operators of the form:
57**
58** expr IN (SELECT ...)
59**
60** These operators have to be processed before field names are
61** resolved because each such operator increments pParse->nTab
62** to reserve a cursor number for its own use. But pParse->nTab
63** needs to be constant once we begin resolving field names.
64**
65** Actually, the processing of IN-SELECT is only started by this
66** routine. This routine allocates a cursor number to the IN-SELECT
67** and then moves on. The code generation is done by
68** sqliteExprResolveIds() which must be called afterwards.
69*/
70void sqliteExprResolveInSelect(Parse *pParse, Expr *pExpr){
71 if( pExpr==0 ) return;
72 if( pExpr->op==TK_IN && pExpr->pSelect!=0 ){
73 pExpr->iTable = pParse->nTab++;
74 }else{
75 if( pExpr->pLeft ) sqliteExprResolveInSelect(pParse, pExpr->pLeft);
76 if( pExpr->pRight ) sqliteExprResolveInSelect(pParse, pExpr->pRight);
77 if( pExpr->pList ){
78 int i;
79 ExprList *pList = pExpr->pList;
80 for(i=0; i<pList->nExpr; i++){
81 sqliteExprResolveInSelect(pParse, pList->a[i].pExpr);
82 }
83 }
84 }
85}
86
87/*
drhcce7d172000-05-31 15:34:51 +000088** This routine walks an expression tree and resolves references to
89** table fields. Nodes of the form ID.ID or ID resolve into an
90** index to the table in the table list and a field offset. The opcode
91** for such nodes is changed to TK_FIELD. The iTable value is changed
drh19a775c2000-06-05 18:54:46 +000092** to the index of the referenced table in pTabList plus the pParse->nTab
93** value. The iField value is changed to the index of the field of the
94** referenced table.
95**
drhfef52082000-06-06 01:50:43 +000096** We also check for instances of the IN operator. IN comes in two
97** forms:
98**
99** expr IN (exprlist)
100** and
101** expr IN (SELECT ...)
102**
103** The first form is handled by creating a set holding the list
104** of allowed values. The second form causes the SELECT to generate
105** a temporary table.
106**
107** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000108** If it finds any, it generates code to write the value of that select
109** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000110**
111** Unknown fields or tables provoke an error. The function returns
112** the number of errors seen and leaves an error message on pParse->zErrMsg.
113*/
114int sqliteExprResolveIds(Parse *pParse, IdList *pTabList, Expr *pExpr){
115 if( pExpr==0 ) return 0;
116 switch( pExpr->op ){
117 /* A lone identifier */
118 case TK_ID: {
119 int cnt = 0; /* Number of matches */
120 int i; /* Loop counter */
121 char *z = 0;
122 sqliteSetNString(&z, pExpr->token.z, pExpr->token.n, 0);
123 for(i=0; i<pTabList->nId; i++){
124 int j;
125 Table *pTab = pTabList->a[i].pTab;
126 if( pTab==0 ) continue;
127 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000128 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000129 cnt++;
drh19a775c2000-06-05 18:54:46 +0000130 pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000131 pExpr->iField = j;
132 }
133 }
134 }
135 sqliteFree(z);
136 if( cnt==0 ){
137 sqliteSetNString(&pParse->zErrMsg, "no such field: ", -1,
138 pExpr->token.z, pExpr->token.n, 0);
139 pParse->nErr++;
140 return 1;
141 }else if( cnt>1 ){
142 sqliteSetNString(&pParse->zErrMsg, "ambiguous field name: ", -1,
143 pExpr->token.z, pExpr->token.n, 0);
144 pParse->nErr++;
145 return 1;
146 }
147 pExpr->op = TK_FIELD;
148 break;
149 }
150
151 /* A table name and field name: ID.ID */
152 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000153 int cnt = 0; /* Number of matches */
154 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000155 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000156 char *zLeft, *zRight; /* Text of an identifier */
157
158 pLeft = pExpr->pLeft;
159 pRight = pExpr->pRight;
160 assert( pLeft && pLeft->op==TK_ID );
161 assert( pRight && pRight->op==TK_ID );
162 zLeft = 0;
163 sqliteSetNString(&zLeft, pLeft->token.z, pLeft->token.n, 0);
164 zRight = 0;
165 sqliteSetNString(&zRight, pRight->token.z, pRight->token.n, 0);
166 for(i=0; i<pTabList->nId; i++){
167 int j;
168 char *zTab;
169 Table *pTab = pTabList->a[i].pTab;
170 if( pTab==0 ) continue;
171 if( pTabList->a[i].zAlias ){
172 zTab = pTabList->a[i].zAlias;
173 }else{
174 zTab = pTab->zName;
175 }
176 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
177 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000178 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000179 cnt++;
drh19a775c2000-06-05 18:54:46 +0000180 pExpr->iTable = i + pParse->nTab;
drhcce7d172000-05-31 15:34:51 +0000181 pExpr->iField = j;
182 }
183 }
184 }
185 sqliteFree(zLeft);
186 sqliteFree(zRight);
187 if( cnt==0 ){
188 sqliteSetNString(&pParse->zErrMsg, "no such field: ", -1,
189 pLeft->token.z, pLeft->token.n, ".", 1,
190 pRight->token.z, pRight->token.n, 0);
191 pParse->nErr++;
192 return 1;
193 }else if( cnt>1 ){
194 sqliteSetNString(&pParse->zErrMsg, "ambiguous field name: ", -1,
195 pLeft->token.z, pLeft->token.n, ".", 1,
196 pRight->token.z, pRight->token.n, 0);
197 pParse->nErr++;
198 return 1;
199 }
200 sqliteExprDelete(pLeft);
201 pExpr->pLeft = 0;
202 sqliteExprDelete(pRight);
203 pExpr->pRight = 0;
204 pExpr->op = TK_FIELD;
205 break;
206 }
207
drhfef52082000-06-06 01:50:43 +0000208 case TK_IN: {
209 Vdbe *v = pParse->pVdbe;
210 if( v==0 ){
211 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe);
212 }
213 if( v==0 ) return 1;
drhcfab11b2000-06-06 03:31:22 +0000214 if( sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
215 return 1;
216 }
drhfef52082000-06-06 01:50:43 +0000217 if( pExpr->pSelect ){
218 /* Case 1: expr IN (SELECT ...)
219 **
220 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000221 ** table. The cursor number of the temporary table has already
222 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000223 */
drh4794b982000-06-06 13:54:14 +0000224 sqliteVdbeAddOp(v, OP_Open, pExpr->iTable, 1, 0, 0);
drhfef52082000-06-06 01:50:43 +0000225 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) );
226 }else if( pExpr->pList ){
227 /* Case 2: expr IN (exprlist)
228 **
229 ** Create a set to put the exprlist values in. The Set id is stored
230 ** in iTable.
231 */
232 int i, iSet;
233 for(i=0; i<pExpr->pList->nExpr; i++){
234 Expr *pE2 = pExpr->pList->a[i].pExpr;
drhfef52082000-06-06 01:50:43 +0000235 if( !isConstant(pE2) ){
236 sqliteSetString(&pParse->zErrMsg,
237 "right-hand side of IN operator must be constant", 0);
238 pParse->nErr++;
239 return 1;
240 }
drh4794b982000-06-06 13:54:14 +0000241 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
242 return 1;
243 }
drhfef52082000-06-06 01:50:43 +0000244 }
245 iSet = pExpr->iTable = pParse->nSet++;
246 for(i=0; i<pExpr->pList->nExpr; i++){
247 Expr *pE2 = pExpr->pList->a[i].pExpr;
248 switch( pE2->op ){
249 case TK_FLOAT:
250 case TK_INTEGER:
251 case TK_STRING: {
252 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
253 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
254 sqliteVdbeDequoteP3(v, addr);
255 break;
256 }
257 default: {
258 sqliteExprCode(pParse, pE2);
259 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0, 0, 0);
260 break;
261 }
262 }
263 }
264 }
drhcfab11b2000-06-06 03:31:22 +0000265 break;
drhfef52082000-06-06 01:50:43 +0000266 }
267
drh19a775c2000-06-05 18:54:46 +0000268 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000269 /* This has to be a scalar SELECT. Generate code to put the
270 ** value of this select in a memory cell and record the number
271 ** of the memory cell in iField.
272 */
drh19a775c2000-06-05 18:54:46 +0000273 pExpr->iField = pParse->nMem++;
drhfef52082000-06-06 01:50:43 +0000274 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iField) ){
drh19a775c2000-06-05 18:54:46 +0000275 return 1;
276 }
277 break;
278 }
279
drhcce7d172000-05-31 15:34:51 +0000280 /* For all else, just recursively walk the tree */
281 default: {
drh4794b982000-06-06 13:54:14 +0000282 if( pExpr->pLeft
283 && sqliteExprResolveIds(pParse, pTabList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000284 return 1;
285 }
286 if( pExpr->pRight
drh4794b982000-06-06 13:54:14 +0000287 && sqliteExprResolveIds(pParse, pTabList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000288 return 1;
289 }
290 if( pExpr->pList ){
291 int i;
292 ExprList *pList = pExpr->pList;
293 for(i=0; i<pList->nExpr; i++){
294 if( sqliteExprResolveIds(pParse, pTabList, pList->a[i].pExpr) ){
295 return 1;
296 }
297 }
298 }
299 }
300 }
301 return 0;
302}
303
304#if 0 /* NOT USED */
305/*
306** Compare a token against a string. Return TRUE if they match.
307*/
308static int sqliteTokenCmp(Token *pToken, const char *zStr){
309 int n = strlen(zStr);
310 if( n!=pToken->n ) return 0;
311 return sqliteStrNICmp(pToken->z, zStr, n)==0;
312}
313#endif
314
315/*
316** Convert a function name into its integer identifier. Return the
317** identifier. Return FN_Unknown if the function name is unknown.
318*/
319int sqliteFuncId(Token *pToken){
320 static const struct {
321 char *zName;
322 int len;
323 int id;
324 } aFunc[] = {
325 { "count", 5, FN_Count },
326 { "min", 3, FN_Min },
327 { "max", 3, FN_Max },
328 { "sum", 3, FN_Sum },
329 };
330 int i;
331 for(i=0; i<ArraySize(aFunc); i++){
332 if( aFunc[i].len==pToken->n
333 && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){
334 return aFunc[i].id;
335 }
336 }
337 return FN_Unknown;
338}
339
340/*
341** Error check the functions in an expression. Make sure all
342** function names are recognized and all functions have the correct
343** number of arguments. Leave an error message in pParse->zErrMsg
344** if anything is amiss. Return the number of errors.
345**
346** if pIsAgg is not null and this expression is an aggregate function
347** (like count(*) or max(value)) then write a 1 into *pIsAgg.
348*/
349int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
350 int nErr = 0;
351 if( pExpr==0 ) return 0;
352 if( pIsAgg ) *pIsAgg = 0;
353 switch( pExpr->op ){
354 case TK_FUNCTION: {
355 int id = sqliteFuncId(&pExpr->token);
356 int n = pExpr->pList ? pExpr->pList->nExpr : 0;
357 int no_such_func = 0;
358 int too_many_args = 0;
359 int too_few_args = 0;
360 int is_agg = 0;
361 int i;
362 switch( id ){
363 case FN_Unknown: {
364 no_such_func = 1;
365 break;
366 }
367 case FN_Count: {
368 no_such_func = !allowAgg;
369 too_many_args = n>1;
370 is_agg = 1;
371 break;
372 }
373 case FN_Max:
374 case FN_Min: {
375 too_few_args = allowAgg ? n<1 : n<2;
376 is_agg = n==1;
377 break;
378 }
379 case FN_Sum: {
380 no_such_func = !allowAgg;
381 too_many_args = n>1;
382 too_few_args = n<1;
383 is_agg = 1;
384 break;
385 }
386 default: break;
387 }
388 if( no_such_func ){
389 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1,
390 pExpr->token.z, pExpr->token.n, 0);
391 pParse->nErr++;
392 nErr++;
393 }else if( too_many_args ){
394 sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1,
395 pExpr->token.z, pExpr->token.n, "()", 2, 0);
396 pParse->nErr++;
397 nErr++;
398 }else if( too_few_args ){
399 sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1,
400 pExpr->token.z, pExpr->token.n, "()", 2, 0);
401 pParse->nErr++;
402 nErr++;
403 }
404 if( is_agg && pIsAgg ) *pIsAgg = 1;
405 for(i=0; nErr==0 && i<n; i++){
406 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 0, 0);
407 }
408 }
409 default: {
410 if( pExpr->pLeft ){
411 nErr = sqliteExprCheck(pParse, pExpr->pLeft, 0, 0);
412 }
413 if( nErr==0 && pExpr->pRight ){
414 nErr = sqliteExprCheck(pParse, pExpr->pRight, 0, 0);
415 }
drhfef52082000-06-06 01:50:43 +0000416 if( nErr==0 && pExpr->pList ){
417 int n = pExpr->pList->nExpr;
418 int i;
419 for(i=0; nErr==0 && i<n; i++){
420 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 0, 0);
421 }
422 }
drhcce7d172000-05-31 15:34:51 +0000423 break;
424 }
425 }
426 return nErr;
427}
428
429/*
430** Generate code into the current Vdbe to evaluate the given
431** expression and leave the result on the stack.
432*/
433void sqliteExprCode(Parse *pParse, Expr *pExpr){
434 Vdbe *v = pParse->pVdbe;
435 int op;
436 switch( pExpr->op ){
437 case TK_PLUS: op = OP_Add; break;
438 case TK_MINUS: op = OP_Subtract; break;
439 case TK_STAR: op = OP_Multiply; break;
440 case TK_SLASH: op = OP_Divide; break;
441 case TK_AND: op = OP_And; break;
442 case TK_OR: op = OP_Or; break;
443 case TK_LT: op = OP_Lt; break;
444 case TK_LE: op = OP_Le; break;
445 case TK_GT: op = OP_Gt; break;
446 case TK_GE: op = OP_Ge; break;
447 case TK_NE: op = OP_Ne; break;
448 case TK_EQ: op = OP_Eq; break;
449 case TK_LIKE: op = OP_Like; break;
450 case TK_GLOB: op = OP_Glob; break;
451 case TK_ISNULL: op = OP_IsNull; break;
452 case TK_NOTNULL: op = OP_NotNull; break;
453 case TK_NOT: op = OP_Not; break;
454 case TK_UMINUS: op = OP_Negative; break;
455 default: break;
456 }
457 switch( pExpr->op ){
458 case TK_FIELD: {
459 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iField, 0, 0);
460 break;
461 }
462 case TK_INTEGER: {
463 int i = atoi(pExpr->token.z);
464 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
465 break;
466 }
467 case TK_FLOAT: {
468 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
469 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
470 break;
471 }
472 case TK_STRING: {
473 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
474 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
475 sqliteVdbeDequoteP3(v, addr);
476 break;
477 }
478 case TK_NULL: {
drhc61053b2000-06-04 12:58:36 +0000479 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000480 break;
481 }
482 case TK_AND:
483 case TK_OR:
484 case TK_PLUS:
485 case TK_STAR:
486 case TK_MINUS:
487 case TK_SLASH: {
488 sqliteExprCode(pParse, pExpr->pLeft);
489 sqliteExprCode(pParse, pExpr->pRight);
490 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
491 break;
492 }
493 case TK_LT:
494 case TK_LE:
495 case TK_GT:
496 case TK_GE:
497 case TK_NE:
498 case TK_EQ:
499 case TK_LIKE:
500 case TK_GLOB: {
501 int dest;
502 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
503 sqliteExprCode(pParse, pExpr->pLeft);
504 sqliteExprCode(pParse, pExpr->pRight);
505 dest = sqliteVdbeCurrentAddr(v) + 2;
506 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
507 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
508 break;
509 }
510 case TK_NOT:
511 case TK_UMINUS: {
512 sqliteExprCode(pParse, pExpr->pLeft);
513 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
514 break;
515 }
516 case TK_ISNULL:
517 case TK_NOTNULL: {
518 int dest;
drh8be51132000-06-03 19:19:41 +0000519 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000520 sqliteExprCode(pParse, pExpr->pLeft);
521 dest = sqliteVdbeCurrentAddr(v) + 2;
522 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
drh8be51132000-06-03 19:19:41 +0000523 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000524 break;
525 }
526 case TK_FUNCTION: {
527 int id = sqliteFuncId(&pExpr->token);
528 int op;
529 int i;
530 ExprList *pList = pExpr->pList;
531 op = id==FN_Min ? OP_Min : OP_Max;
532 for(i=0; i<pList->nExpr; i++){
533 sqliteExprCode(pParse, pList->a[i].pExpr);
534 if( i>0 ){
535 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
536 }
537 }
538 break;
539 }
drh19a775c2000-06-05 18:54:46 +0000540 case TK_SELECT: {
541 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iField, 0, 0, 0);
542 break;
543 }
drhfef52082000-06-06 01:50:43 +0000544 case TK_IN: {
545 int addr;
drh4794b982000-06-06 13:54:14 +0000546 sqliteVdbeAddOp(v, OP_Integer, 1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000547 sqliteExprCode(pParse, pExpr->pLeft);
548 addr = sqliteVdbeCurrentAddr(v);
549 if( pExpr->pSelect ){
550 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2, 0, 0);
551 }else{
552 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2, 0, 0);
553 }
drh4794b982000-06-06 13:54:14 +0000554 sqliteVdbeAddOp(v, OP_AddImm, -1, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000555 break;
556 }
557 case TK_BETWEEN: {
558 int lbl = sqliteVdbeMakeLabel(v);
559 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
560 sqliteExprIfFalse(pParse, pExpr, lbl);
561 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
562 sqliteVdbeResolveLabel(v, lbl);
563 break;
564 }
drhcce7d172000-05-31 15:34:51 +0000565 }
566 return;
567}
568
569/*
570** Generate code for a boolean expression such that a jump is made
571** to the label "dest" if the expression is true but execution
572** continues straight thru if the expression is false.
573*/
574void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
575 Vdbe *v = pParse->pVdbe;
576 int op = 0;
577 switch( pExpr->op ){
578 case TK_LT: op = OP_Lt; break;
579 case TK_LE: op = OP_Le; break;
580 case TK_GT: op = OP_Gt; break;
581 case TK_GE: op = OP_Ge; break;
582 case TK_NE: op = OP_Ne; break;
583 case TK_EQ: op = OP_Eq; break;
584 case TK_LIKE: op = OP_Like; break;
585 case TK_GLOB: op = OP_Glob; break;
586 case TK_ISNULL: op = OP_IsNull; break;
587 case TK_NOTNULL: op = OP_NotNull; break;
588 default: break;
589 }
590 switch( pExpr->op ){
591 case TK_AND: {
592 int d2 = sqliteVdbeMakeLabel(v);
593 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
594 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
595 sqliteVdbeResolveLabel(v, d2);
596 break;
597 }
598 case TK_OR: {
599 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
600 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
601 break;
602 }
603 case TK_NOT: {
604 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
605 break;
606 }
607 case TK_LT:
608 case TK_LE:
609 case TK_GT:
610 case TK_GE:
611 case TK_NE:
612 case TK_EQ:
613 case TK_LIKE:
614 case TK_GLOB: {
615 sqliteExprCode(pParse, pExpr->pLeft);
616 sqliteExprCode(pParse, pExpr->pRight);
617 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
618 break;
619 }
620 case TK_ISNULL:
621 case TK_NOTNULL: {
622 sqliteExprCode(pParse, pExpr->pLeft);
623 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
624 break;
625 }
drhfef52082000-06-06 01:50:43 +0000626 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000627 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000628 if( pExpr->pSelect ){
629 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest, 0, 0);
630 }else{
631 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest, 0, 0);
632 }
633 break;
634 }
635 case TK_BETWEEN: {
636 int lbl = sqliteVdbeMakeLabel(v);
637 sqliteExprCode(pParse, pExpr->pLeft);
638 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
639 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
640 sqliteVdbeAddOp(v, OP_Lt, 0, lbl, 0, 0);
641 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
642 sqliteVdbeAddOp(v, OP_Le, 0, dest, 0, 0);
643 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
644 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, lbl);
645 break;
646 }
drhcce7d172000-05-31 15:34:51 +0000647 default: {
648 sqliteExprCode(pParse, pExpr);
649 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
650 break;
651 }
652 }
653}
654
655/*
656** Generate code for boolean expression such that a jump is made
657** to the label "dest" if the expression is false but execution
658** continues straight thru if the expression is true.
659*/
660void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
661 Vdbe *v = pParse->pVdbe;
662 int op = 0;
663 switch( pExpr->op ){
664 case TK_LT: op = OP_Ge; break;
665 case TK_LE: op = OP_Gt; break;
666 case TK_GT: op = OP_Le; break;
667 case TK_GE: op = OP_Lt; break;
668 case TK_NE: op = OP_Eq; break;
669 case TK_EQ: op = OP_Ne; break;
670 case TK_LIKE: op = OP_Like; break;
671 case TK_GLOB: op = OP_Glob; break;
672 case TK_ISNULL: op = OP_NotNull; break;
673 case TK_NOTNULL: op = OP_IsNull; break;
674 default: break;
675 }
676 switch( pExpr->op ){
677 case TK_AND: {
678 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
679 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
680 break;
681 }
682 case TK_OR: {
683 int d2 = sqliteVdbeMakeLabel(v);
684 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
685 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
686 sqliteVdbeResolveLabel(v, d2);
687 break;
688 }
689 case TK_NOT: {
690 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
691 break;
692 }
693 case TK_LT:
694 case TK_LE:
695 case TK_GT:
696 case TK_GE:
697 case TK_NE:
698 case TK_EQ: {
699 sqliteExprCode(pParse, pExpr->pLeft);
700 sqliteExprCode(pParse, pExpr->pRight);
701 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
702 break;
703 }
704 case TK_LIKE:
705 case TK_GLOB: {
706 sqliteExprCode(pParse, pExpr->pLeft);
707 sqliteExprCode(pParse, pExpr->pRight);
708 sqliteVdbeAddOp(v, op, 1, dest, 0, 0);
709 break;
710 }
711 case TK_ISNULL:
712 case TK_NOTNULL: {
713 sqliteExprCode(pParse, pExpr->pLeft);
714 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
715 break;
716 }
drhfef52082000-06-06 01:50:43 +0000717 case TK_IN: {
drhcfab11b2000-06-06 03:31:22 +0000718 sqliteExprCode(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +0000719 if( pExpr->pSelect ){
720 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest, 0, 0);
721 }else{
722 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest, 0, 0);
723 }
724 break;
725 }
726 case TK_BETWEEN: {
727 int addr;
728 sqliteExprCode(pParse, pExpr->pLeft);
729 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
730 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
731 addr = sqliteVdbeCurrentAddr(v);
732 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3, 0, 0);
733 sqliteVdbeAddOp(v, OP_Pop, 1, 0, 0, 0);
734 sqliteVdbeAddOp(v, OP_Goto, 0, dest, 0, 0);
735 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
736 sqliteVdbeAddOp(v, OP_Gt, 0, dest, 0, 0);
737 break;
738 }
drhcce7d172000-05-31 15:34:51 +0000739 default: {
740 sqliteExprCode(pParse, pExpr);
741 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
742 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
743 break;
744 }
745 }
746}