drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 12 | ** This file contains routines used for analyzing expressions and |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 13 | ** for generating VDBE code that evaluates expressions in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | ** |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 15 | ** $Id: expr.c,v 1.166 2004/10/04 13:19:24 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
drh | 04738cb | 2002-06-02 18:19:00 +0000 | [diff] [blame] | 18 | #include <ctype.h> |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 19 | |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 20 | /* |
| 21 | ** Return the 'affinity' of the expression pExpr if any. |
| 22 | ** |
| 23 | ** If pExpr is a column, a reference to a column via an 'AS' alias, |
| 24 | ** or a sub-select with a column as the return value, then the |
| 25 | ** affinity of that column is returned. Otherwise, 0x00 is returned, |
| 26 | ** indicating no affinity for the expression. |
| 27 | ** |
| 28 | ** i.e. the WHERE clause expresssions in the following statements all |
| 29 | ** have an affinity: |
| 30 | ** |
| 31 | ** CREATE TABLE t1(a); |
| 32 | ** SELECT * FROM t1 WHERE a; |
| 33 | ** SELECT a AS b FROM t1 WHERE b; |
| 34 | ** SELECT * FROM t1 WHERE (select a from t1); |
| 35 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 36 | char sqlite3ExprAffinity(Expr *pExpr){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 37 | if( pExpr->op==TK_AS ){ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 38 | return sqlite3ExprAffinity(pExpr->pLeft); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 39 | } |
| 40 | if( pExpr->op==TK_SELECT ){ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 41 | return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 42 | } |
| 43 | return pExpr->affinity; |
| 44 | } |
| 45 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 46 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 47 | ** Return the default collation sequence for the expression pExpr. If |
| 48 | ** there is no default collation type, return 0. |
| 49 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 50 | CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ |
| 51 | CollSeq *pColl = 0; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 52 | if( pExpr ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 53 | pColl = pExpr->pColl; |
| 54 | if( pExpr->op==TK_AS && !pColl ){ |
| 55 | return sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 58 | if( sqlite3CheckCollSeq(pParse, pColl) ){ |
| 59 | pColl = 0; |
| 60 | } |
| 61 | return pColl; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 65 | ** pExpr is the left operand of a comparison operator. aff2 is the |
| 66 | ** type affinity of the right operand. This routine returns the |
| 67 | ** type affinity that should be used for the comparison operator. |
| 68 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 69 | char sqlite3CompareAffinity(Expr *pExpr, char aff2){ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 70 | char aff1 = sqlite3ExprAffinity(pExpr); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 71 | if( aff1 && aff2 ){ |
| 72 | /* Both sides of the comparison are columns. If one has numeric or |
| 73 | ** integer affinity, use that. Otherwise use no affinity. |
| 74 | */ |
| 75 | if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){ |
| 76 | return SQLITE_AFF_INTEGER; |
| 77 | }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){ |
| 78 | return SQLITE_AFF_NUMERIC; |
| 79 | }else{ |
| 80 | return SQLITE_AFF_NONE; |
| 81 | } |
| 82 | }else if( !aff1 && !aff2 ){ |
drh | 5f6a87b | 2004-07-19 00:39:45 +0000 | [diff] [blame] | 83 | /* Neither side of the comparison is a column. Compare the |
| 84 | ** results directly. |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 85 | */ |
drh | 5f6a87b | 2004-07-19 00:39:45 +0000 | [diff] [blame] | 86 | /* return SQLITE_AFF_NUMERIC; // Ticket #805 */ |
| 87 | return SQLITE_AFF_NONE; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 88 | }else{ |
| 89 | /* One side is a column, the other is not. Use the columns affinity. */ |
| 90 | return (aff1 + aff2); |
| 91 | } |
| 92 | } |
| 93 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 94 | /* |
| 95 | ** pExpr is a comparison operator. Return the type affinity that should |
| 96 | ** be applied to both operands prior to doing the comparison. |
| 97 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 98 | static char comparisonAffinity(Expr *pExpr){ |
| 99 | char aff; |
| 100 | assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || |
| 101 | pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || |
| 102 | pExpr->op==TK_NE ); |
| 103 | assert( pExpr->pLeft ); |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 104 | aff = sqlite3ExprAffinity(pExpr->pLeft); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 105 | if( pExpr->pRight ){ |
| 106 | aff = sqlite3CompareAffinity(pExpr->pRight, aff); |
| 107 | } |
| 108 | else if( pExpr->pSelect ){ |
| 109 | aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); |
| 110 | } |
| 111 | else if( !aff ){ |
| 112 | aff = SQLITE_AFF_NUMERIC; |
| 113 | } |
| 114 | return aff; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. |
| 119 | ** idx_affinity is the affinity of an indexed column. Return true |
| 120 | ** if the index with affinity idx_affinity may be used to implement |
| 121 | ** the comparison in pExpr. |
| 122 | */ |
| 123 | int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ |
| 124 | char aff = comparisonAffinity(pExpr); |
| 125 | return |
| 126 | (aff==SQLITE_AFF_NONE) || |
| 127 | (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) || |
| 128 | (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) || |
| 129 | (aff==idx_affinity); |
| 130 | } |
| 131 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 132 | /* |
| 133 | ** Return the P1 value that should be used for a binary comparison |
| 134 | ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. |
| 135 | ** If jumpIfNull is true, then set the low byte of the returned |
| 136 | ** P1 value to tell the opcode to jump if either expression |
| 137 | ** evaluates to NULL. |
| 138 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 139 | static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 140 | char aff = sqlite3ExprAffinity(pExpr2); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 141 | return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 142 | } |
| 143 | |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 144 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 145 | ** Return a pointer to the collation sequence that should be used by |
| 146 | ** a binary comparison operator comparing pLeft and pRight. |
| 147 | ** |
| 148 | ** If the left hand expression has a collating sequence type, then it is |
| 149 | ** used. Otherwise the collation sequence for the right hand expression |
| 150 | ** is used, or the default (BINARY) if neither expression has a collating |
| 151 | ** type. |
| 152 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 153 | static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){ |
| 154 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 155 | if( !pColl ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 156 | pColl = sqlite3ExprCollSeq(pParse, pRight); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 157 | } |
| 158 | return pColl; |
| 159 | } |
| 160 | |
| 161 | /* |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 162 | ** Generate code for a comparison operator. |
| 163 | */ |
| 164 | static int codeCompare( |
| 165 | Parse *pParse, /* The parsing (and code generating) context */ |
| 166 | Expr *pLeft, /* The left operand */ |
| 167 | Expr *pRight, /* The right operand */ |
| 168 | int opcode, /* The comparison opcode */ |
| 169 | int dest, /* Jump here if true. */ |
| 170 | int jumpIfNull /* If true, jump if either operand is NULL */ |
| 171 | ){ |
| 172 | int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); |
| 173 | CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 174 | return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 178 | ** Construct a new expression node and return a pointer to it. Memory |
| 179 | ** for this node is obtained from sqliteMalloc(). The calling function |
| 180 | ** is responsible for making sure the node eventually gets freed. |
| 181 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 182 | Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 183 | Expr *pNew; |
| 184 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 185 | if( pNew==0 ){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 186 | /* When malloc fails, we leak memory from pLeft and pRight */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | pNew->op = op; |
| 190 | pNew->pLeft = pLeft; |
| 191 | pNew->pRight = pRight; |
| 192 | if( pToken ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 193 | assert( pToken->dyn==0 ); |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 194 | pNew->span = pNew->token = *pToken; |
| 195 | }else if( pLeft && pRight ){ |
| 196 | sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 197 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 198 | return pNew; |
| 199 | } |
| 200 | |
| 201 | /* |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 202 | ** Join two expressions using an AND operator. If either expression is |
| 203 | ** NULL, then just return the other expression. |
| 204 | */ |
| 205 | Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){ |
| 206 | if( pLeft==0 ){ |
| 207 | return pRight; |
| 208 | }else if( pRight==0 ){ |
| 209 | return pLeft; |
| 210 | }else{ |
| 211 | return sqlite3Expr(TK_AND, pLeft, pRight, 0); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 216 | ** Set the Expr.span field of the given expression to span all |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 217 | ** text between the two given tokens. |
| 218 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 219 | void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 220 | assert( pRight!=0 ); |
| 221 | assert( pLeft!=0 ); |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 222 | if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){ |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 223 | assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 224 | if( pLeft->dyn==0 && pRight->dyn==0 ){ |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 225 | pExpr->span.z = pLeft->z; |
| 226 | pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 227 | }else{ |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 228 | pExpr->span.z = 0; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 229 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | ** Construct a new expression node for a function with multiple |
| 235 | ** arguments. |
| 236 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 237 | Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 238 | Expr *pNew; |
| 239 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 240 | if( pNew==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 241 | /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | pNew->op = TK_FUNCTION; |
| 245 | pNew->pList = pList; |
| 246 | if( pToken ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 247 | assert( pToken->dyn==0 ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 248 | pNew->token = *pToken; |
| 249 | }else{ |
| 250 | pNew->token.z = 0; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 251 | } |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 252 | pNew->span = pNew->token; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 253 | return pNew; |
| 254 | } |
| 255 | |
| 256 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 257 | ** Assign a variable number to an expression that encodes a wildcard |
| 258 | ** in the original SQL statement. |
| 259 | ** |
| 260 | ** Wildcards consisting of a single "?" are assigned the next sequential |
| 261 | ** variable number. |
| 262 | ** |
| 263 | ** Wildcards of the form "?nnn" are assigned the number "nnn". We make |
| 264 | ** sure "nnn" is not too be to avoid a denial of service attack when |
| 265 | ** the SQL statement comes from an external source. |
| 266 | ** |
| 267 | ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number |
| 268 | ** as the previous instance of the same wildcard. Or if this is the first |
| 269 | ** instance of the wildcard, the next sequenial variable number is |
| 270 | ** assigned. |
| 271 | */ |
| 272 | void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ |
| 273 | Token *pToken; |
| 274 | if( pExpr==0 ) return; |
| 275 | pToken = &pExpr->token; |
| 276 | assert( pToken->n>=1 ); |
| 277 | assert( pToken->z!=0 ); |
| 278 | assert( pToken->z[0]!=0 ); |
| 279 | if( pToken->n==1 ){ |
| 280 | /* Wildcard of the form "?". Assign the next variable number */ |
| 281 | pExpr->iTable = ++pParse->nVar; |
| 282 | }else if( pToken->z[0]=='?' ){ |
| 283 | /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
| 284 | ** use it as the variable number */ |
| 285 | int i; |
| 286 | pExpr->iTable = i = atoi(&pToken->z[1]); |
| 287 | if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){ |
| 288 | sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
| 289 | SQLITE_MAX_VARIABLE_NUMBER); |
| 290 | } |
| 291 | if( i>pParse->nVar ){ |
| 292 | pParse->nVar = i; |
| 293 | } |
| 294 | }else{ |
| 295 | /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable |
| 296 | ** number as the prior appearance of the same name, or if the name |
| 297 | ** has never appeared before, reuse the same variable number |
| 298 | */ |
| 299 | int i, n; |
| 300 | n = pToken->n; |
| 301 | for(i=0; i<pParse->nVarExpr; i++){ |
| 302 | Expr *pE; |
| 303 | if( (pE = pParse->apVarExpr[i])!=0 |
| 304 | && pE->token.n==n |
| 305 | && memcmp(pE->token.z, pToken->z, n)==0 ){ |
| 306 | pExpr->iTable = pE->iTable; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | if( i>=pParse->nVarExpr ){ |
| 311 | pExpr->iTable = ++pParse->nVar; |
| 312 | if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ |
| 313 | pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; |
| 314 | pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr, |
| 315 | pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ); |
| 316 | } |
| 317 | if( !sqlite3_malloc_failed ){ |
| 318 | assert( pParse->apVarExpr!=0 ); |
| 319 | pParse->apVarExpr[pParse->nVarExpr++] = pExpr; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 326 | ** Recursively delete an expression tree. |
| 327 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 328 | void sqlite3ExprDelete(Expr *p){ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 329 | if( p==0 ) return; |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 330 | if( p->span.dyn ) sqliteFree((char*)p->span.z); |
| 331 | if( p->token.dyn ) sqliteFree((char*)p->token.z); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 332 | sqlite3ExprDelete(p->pLeft); |
| 333 | sqlite3ExprDelete(p->pRight); |
| 334 | sqlite3ExprListDelete(p->pList); |
| 335 | sqlite3SelectDelete(p->pSelect); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 336 | sqliteFree(p); |
| 337 | } |
| 338 | |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 339 | |
| 340 | /* |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 341 | ** The following group of routines make deep copies of expressions, |
| 342 | ** expression lists, ID lists, and select statements. The copies can |
| 343 | ** be deleted (by being passed to their respective ...Delete() routines) |
| 344 | ** without effecting the originals. |
| 345 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 346 | ** The expression list, ID, and source lists return by sqlite3ExprListDup(), |
| 347 | ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 348 | ** by subsequent calls to sqlite*ListAppend() routines. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 349 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 350 | ** Any tables that the SrcList might point to are not duplicated. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 351 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 352 | Expr *sqlite3ExprDup(Expr *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 353 | Expr *pNew; |
| 354 | if( p==0 ) return 0; |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 355 | pNew = sqliteMallocRaw( sizeof(*p) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 356 | if( pNew==0 ) return 0; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 357 | memcpy(pNew, p, sizeof(*pNew)); |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 358 | if( p->token.z!=0 ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 359 | pNew->token.z = sqliteStrDup(p->token.z); |
| 360 | pNew->token.dyn = 1; |
| 361 | }else{ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 362 | assert( pNew->token.z==0 ); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 363 | } |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 364 | pNew->span.z = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 365 | pNew->pLeft = sqlite3ExprDup(p->pLeft); |
| 366 | pNew->pRight = sqlite3ExprDup(p->pRight); |
| 367 | pNew->pList = sqlite3ExprListDup(p->pList); |
| 368 | pNew->pSelect = sqlite3SelectDup(p->pSelect); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 369 | return pNew; |
| 370 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 371 | void sqlite3TokenCopy(Token *pTo, Token *pFrom){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 372 | if( pTo->dyn ) sqliteFree((char*)pTo->z); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 373 | if( pFrom->z ){ |
| 374 | pTo->n = pFrom->n; |
| 375 | pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); |
| 376 | pTo->dyn = 1; |
| 377 | }else{ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 378 | pTo->z = 0; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 381 | ExprList *sqlite3ExprListDup(ExprList *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 382 | ExprList *pNew; |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 383 | struct ExprList_item *pItem, *pOldItem; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 384 | int i; |
| 385 | if( p==0 ) return 0; |
| 386 | pNew = sqliteMalloc( sizeof(*pNew) ); |
| 387 | if( pNew==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 388 | pNew->nExpr = pNew->nAlloc = p->nExpr; |
drh | 3e7bc9c | 2004-02-21 19:17:17 +0000 | [diff] [blame] | 389 | pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 390 | if( pItem==0 ){ |
| 391 | sqliteFree(pNew); |
| 392 | return 0; |
| 393 | } |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 394 | pOldItem = p->a; |
| 395 | for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 396 | Expr *pNewExpr, *pOldExpr; |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 397 | pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr); |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 398 | if( pOldExpr->span.z!=0 && pNewExpr ){ |
| 399 | /* Always make a copy of the span for top-level expressions in the |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 400 | ** expression list. The logic in SELECT processing that determines |
| 401 | ** the names of columns in the result set needs this information */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 402 | sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 403 | } |
drh | 1f3e905 | 2002-10-31 00:09:39 +0000 | [diff] [blame] | 404 | assert( pNewExpr==0 || pNewExpr->span.z!=0 |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 405 | || pOldExpr->span.z==0 || sqlite3_malloc_failed ); |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 406 | pItem->zName = sqliteStrDup(pOldItem->zName); |
| 407 | pItem->sortOrder = pOldItem->sortOrder; |
| 408 | pItem->isAgg = pOldItem->isAgg; |
drh | 3e7bc9c | 2004-02-21 19:17:17 +0000 | [diff] [blame] | 409 | pItem->done = 0; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 410 | } |
| 411 | return pNew; |
| 412 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 413 | SrcList *sqlite3SrcListDup(SrcList *p){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 414 | SrcList *pNew; |
| 415 | int i; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 416 | int nByte; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 417 | if( p==0 ) return 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 418 | nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 419 | pNew = sqliteMallocRaw( nByte ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 420 | if( pNew==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 421 | pNew->nSrc = pNew->nAlloc = p->nSrc; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 422 | for(i=0; i<p->nSrc; i++){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 423 | struct SrcList_item *pNewItem = &pNew->a[i]; |
| 424 | struct SrcList_item *pOldItem = &p->a[i]; |
| 425 | pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); |
| 426 | pNewItem->zName = sqliteStrDup(pOldItem->zName); |
| 427 | pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); |
| 428 | pNewItem->jointype = pOldItem->jointype; |
| 429 | pNewItem->iCursor = pOldItem->iCursor; |
| 430 | pNewItem->pTab = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 431 | pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); |
| 432 | pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); |
| 433 | pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 434 | } |
| 435 | return pNew; |
| 436 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 437 | IdList *sqlite3IdListDup(IdList *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 438 | IdList *pNew; |
| 439 | int i; |
| 440 | if( p==0 ) return 0; |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 441 | pNew = sqliteMallocRaw( sizeof(*pNew) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 442 | if( pNew==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 443 | pNew->nId = pNew->nAlloc = p->nId; |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 444 | pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); |
drh | e4697f5 | 2002-05-23 02:09:03 +0000 | [diff] [blame] | 445 | if( pNew->a==0 ) return 0; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 446 | for(i=0; i<p->nId; i++){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 447 | struct IdList_item *pNewItem = &pNew->a[i]; |
| 448 | struct IdList_item *pOldItem = &p->a[i]; |
| 449 | pNewItem->zName = sqliteStrDup(pOldItem->zName); |
| 450 | pNewItem->idx = pOldItem->idx; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 451 | } |
| 452 | return pNew; |
| 453 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 454 | Select *sqlite3SelectDup(Select *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 455 | Select *pNew; |
| 456 | if( p==0 ) return 0; |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 457 | pNew = sqliteMallocRaw( sizeof(*p) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 458 | if( pNew==0 ) return 0; |
| 459 | pNew->isDistinct = p->isDistinct; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 460 | pNew->pEList = sqlite3ExprListDup(p->pEList); |
| 461 | pNew->pSrc = sqlite3SrcListDup(p->pSrc); |
| 462 | pNew->pWhere = sqlite3ExprDup(p->pWhere); |
| 463 | pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); |
| 464 | pNew->pHaving = sqlite3ExprDup(p->pHaving); |
| 465 | pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 466 | pNew->op = p->op; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 467 | pNew->pPrior = sqlite3SelectDup(p->pPrior); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 468 | pNew->nLimit = p->nLimit; |
| 469 | pNew->nOffset = p->nOffset; |
| 470 | pNew->zSelect = 0; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 471 | pNew->iLimit = -1; |
| 472 | pNew->iOffset = -1; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 473 | pNew->ppOpenTemp = 0; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 474 | return pNew; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 479 | ** Add a new element to the end of an expression list. If pList is |
| 480 | ** initially NULL, then create a new expression list. |
| 481 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 482 | ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 483 | if( pList==0 ){ |
| 484 | pList = sqliteMalloc( sizeof(ExprList) ); |
| 485 | if( pList==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 486 | /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 487 | return 0; |
| 488 | } |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 489 | assert( pList->nAlloc==0 ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 490 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 491 | if( pList->nAlloc<=pList->nExpr ){ |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 492 | pList->nAlloc = pList->nAlloc*2 + 4; |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 493 | pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); |
| 494 | if( pList->a==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 495 | /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 496 | pList->nExpr = pList->nAlloc = 0; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 497 | return pList; |
| 498 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 499 | } |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 500 | assert( pList->a!=0 ); |
| 501 | if( pExpr || pName ){ |
| 502 | struct ExprList_item *pItem = &pList->a[pList->nExpr++]; |
| 503 | memset(pItem, 0, sizeof(*pItem)); |
| 504 | pItem->pExpr = pExpr; |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 505 | pItem->zName = sqlite3NameFromToken(pName); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 506 | } |
| 507 | return pList; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | ** Delete an entire expression list. |
| 512 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 513 | void sqlite3ExprListDelete(ExprList *pList){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 514 | int i; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 515 | struct ExprList_item *pItem; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 516 | if( pList==0 ) return; |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 517 | assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); |
| 518 | assert( pList->nExpr<=pList->nAlloc ); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 519 | for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
| 520 | sqlite3ExprDelete(pItem->pExpr); |
| 521 | sqliteFree(pItem->zName); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 522 | } |
| 523 | sqliteFree(pList->a); |
| 524 | sqliteFree(pList); |
| 525 | } |
| 526 | |
| 527 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 528 | ** Walk an expression tree. Return 1 if the expression is constant |
| 529 | ** and 0 if it involves variables. |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 530 | ** |
| 531 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 532 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 533 | ** a constant. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 534 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 535 | int sqlite3ExprIsConstant(Expr *p){ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 536 | switch( p->op ){ |
| 537 | case TK_ID: |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 538 | case TK_COLUMN: |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 539 | case TK_DOT: |
drh | 7bdc0c1 | 2003-04-19 17:27:24 +0000 | [diff] [blame] | 540 | case TK_FUNCTION: |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 541 | return 0; |
drh | 7bdc0c1 | 2003-04-19 17:27:24 +0000 | [diff] [blame] | 542 | case TK_NULL: |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 543 | case TK_STRING: |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 544 | case TK_BLOB: |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 545 | case TK_INTEGER: |
| 546 | case TK_FLOAT: |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 547 | case TK_VARIABLE: |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 548 | return 1; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 549 | default: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 550 | if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0; |
| 551 | if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 552 | if( p->pList ){ |
| 553 | int i; |
| 554 | for(i=0; i<p->pList->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 555 | if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 558 | return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 561 | return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 565 | ** If the given expression codes a constant integer that is small enough |
| 566 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| 567 | ** in *pValue. If the expression is not an integer or if it is too big |
| 568 | ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 569 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 570 | int sqlite3ExprIsInteger(Expr *p, int *pValue){ |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 571 | switch( p->op ){ |
| 572 | case TK_INTEGER: { |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 573 | if( sqlite3GetInt32(p->token.z, pValue) ){ |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 574 | return 1; |
| 575 | } |
| 576 | break; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 577 | } |
| 578 | case TK_STRING: { |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 579 | const u8 *z = (u8*)p->token.z; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 580 | int n = p->token.n; |
drh | bd790ee | 2002-06-02 18:22:06 +0000 | [diff] [blame] | 581 | if( n>0 && z[0]=='-' ){ z++; n--; } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 582 | while( n>0 && *z && isdigit(*z) ){ z++; n--; } |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 583 | if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){ |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 584 | return 1; |
| 585 | } |
| 586 | break; |
| 587 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 588 | case TK_UPLUS: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 589 | return sqlite3ExprIsInteger(p->pLeft, pValue); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 590 | } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 591 | case TK_UMINUS: { |
| 592 | int v; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 593 | if( sqlite3ExprIsInteger(p->pLeft, &v) ){ |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 594 | *pValue = -v; |
| 595 | return 1; |
| 596 | } |
| 597 | break; |
| 598 | } |
| 599 | default: break; |
| 600 | } |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 605 | ** Return TRUE if the given string is a row-id column name. |
| 606 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 607 | int sqlite3IsRowid(const char *z){ |
| 608 | if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; |
| 609 | if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; |
| 610 | if( sqlite3StrICmp(z, "OID")==0 ) return 1; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 615 | ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up |
| 616 | ** that name in the set of source tables in pSrcList and make the pExpr |
| 617 | ** expression node refer back to that source column. The following changes |
| 618 | ** are made to pExpr: |
| 619 | ** |
| 620 | ** pExpr->iDb Set the index in db->aDb[] of the database holding |
| 621 | ** the table. |
| 622 | ** pExpr->iTable Set to the cursor number for the table obtained |
| 623 | ** from pSrcList. |
| 624 | ** pExpr->iColumn Set to the column number within the table. |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 625 | ** pExpr->op Set to TK_COLUMN. |
| 626 | ** pExpr->pLeft Any expression this points to is deleted |
| 627 | ** pExpr->pRight Any expression this points to is deleted. |
| 628 | ** |
| 629 | ** The pDbToken is the name of the database (the "X"). This value may be |
| 630 | ** NULL meaning that name is of the form Y.Z or Z. Any available database |
| 631 | ** can be used. The pTableToken is the name of the table (the "Y"). This |
| 632 | ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it |
| 633 | ** means that the form of the name is Z and that columns from any table |
| 634 | ** can be used. |
| 635 | ** |
| 636 | ** If the name cannot be resolved unambiguously, leave an error message |
| 637 | ** in pParse and return non-zero. Return zero on success. |
| 638 | */ |
| 639 | static int lookupName( |
| 640 | Parse *pParse, /* The parsing context */ |
| 641 | Token *pDbToken, /* Name of the database containing table, or NULL */ |
| 642 | Token *pTableToken, /* Name of table containing column, or NULL */ |
| 643 | Token *pColumnToken, /* Name of the column. */ |
| 644 | SrcList *pSrcList, /* List of tables used to resolve column names */ |
| 645 | ExprList *pEList, /* List of expressions used to resolve "AS" */ |
| 646 | Expr *pExpr /* Make this EXPR node point to the selected column */ |
| 647 | ){ |
| 648 | char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ |
| 649 | char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ |
| 650 | char *zCol = 0; /* Name of the column. The "Z" */ |
| 651 | int i, j; /* Loop counters */ |
| 652 | int cnt = 0; /* Number of matching column names */ |
| 653 | int cntTab = 0; /* Number of matching table names */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 654 | sqlite3 *db = pParse->db; /* The database */ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 655 | |
| 656 | assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 657 | zDb = sqlite3NameFromToken(pDbToken); |
| 658 | zTab = sqlite3NameFromToken(pTableToken); |
| 659 | zCol = sqlite3NameFromToken(pColumnToken); |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 660 | if( sqlite3_malloc_failed ){ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 661 | return 1; /* Leak memory (zDb and zTab) if malloc fails */ |
| 662 | } |
| 663 | assert( zTab==0 || pEList==0 ); |
| 664 | |
| 665 | pExpr->iTable = -1; |
| 666 | for(i=0; i<pSrcList->nSrc; i++){ |
| 667 | struct SrcList_item *pItem = &pSrcList->a[i]; |
| 668 | Table *pTab = pItem->pTab; |
| 669 | Column *pCol; |
| 670 | |
| 671 | if( pTab==0 ) continue; |
| 672 | assert( pTab->nCol>0 ); |
| 673 | if( zTab ){ |
| 674 | if( pItem->zAlias ){ |
| 675 | char *zTabName = pItem->zAlias; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 676 | if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 677 | }else{ |
| 678 | char *zTabName = pTab->zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 679 | if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; |
| 680 | if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 681 | continue; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | if( 0==(cntTab++) ){ |
| 686 | pExpr->iTable = pItem->iCursor; |
| 687 | pExpr->iDb = pTab->iDb; |
| 688 | } |
| 689 | for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 690 | if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 691 | cnt++; |
| 692 | pExpr->iTable = pItem->iCursor; |
| 693 | pExpr->iDb = pTab->iDb; |
| 694 | /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ |
| 695 | pExpr->iColumn = j==pTab->iPKey ? -1 : j; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 696 | pExpr->affinity = pTab->aCol[j].affinity; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 697 | pExpr->pColl = pTab->aCol[j].pColl; |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 698 | break; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /* If we have not already resolved the name, then maybe |
| 704 | ** it is a new.* or old.* trigger argument reference |
| 705 | */ |
| 706 | if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ |
| 707 | TriggerStack *pTriggerStack = pParse->trigStack; |
| 708 | Table *pTab = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 709 | if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 710 | pExpr->iTable = pTriggerStack->newIdx; |
| 711 | assert( pTriggerStack->pTab ); |
| 712 | pTab = pTriggerStack->pTab; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 713 | }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 714 | pExpr->iTable = pTriggerStack->oldIdx; |
| 715 | assert( pTriggerStack->pTab ); |
| 716 | pTab = pTriggerStack->pTab; |
| 717 | } |
| 718 | |
| 719 | if( pTab ){ |
| 720 | int j; |
| 721 | Column *pCol = pTab->aCol; |
| 722 | |
| 723 | pExpr->iDb = pTab->iDb; |
| 724 | cntTab++; |
| 725 | for(j=0; j < pTab->nCol; j++, pCol++) { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 726 | if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 727 | cnt++; |
| 728 | pExpr->iColumn = j==pTab->iPKey ? -1 : j; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 729 | pExpr->affinity = pTab->aCol[j].affinity; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 730 | pExpr->pColl = pTab->aCol[j].pColl; |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 731 | break; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | ** Perhaps the name is a reference to the ROWID |
| 739 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 740 | if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 741 | cnt = 1; |
| 742 | pExpr->iColumn = -1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 743 | pExpr->affinity = SQLITE_AFF_INTEGER; |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* |
| 747 | ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z |
| 748 | ** might refer to an result-set alias. This happens, for example, when |
| 749 | ** we are resolving names in the WHERE clause of the following command: |
| 750 | ** |
| 751 | ** SELECT a+b AS x FROM table WHERE x<10; |
| 752 | ** |
| 753 | ** In cases like this, replace pExpr with a copy of the expression that |
| 754 | ** forms the result set entry ("a+b" in the example) and return immediately. |
| 755 | ** Note that the expression in the result set should have already been |
| 756 | ** resolved by the time the WHERE clause is resolved. |
| 757 | */ |
| 758 | if( cnt==0 && pEList!=0 ){ |
| 759 | for(j=0; j<pEList->nExpr; j++){ |
| 760 | char *zAs = pEList->a[j].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 761 | if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 762 | assert( pExpr->pLeft==0 && pExpr->pRight==0 ); |
| 763 | pExpr->op = TK_AS; |
| 764 | pExpr->iColumn = j; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 765 | pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 766 | sqliteFree(zCol); |
| 767 | assert( zTab==0 && zDb==0 ); |
| 768 | return 0; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | ** If X and Y are NULL (in other words if only the column name Z is |
| 775 | ** supplied) and the value of Z is enclosed in double-quotes, then |
| 776 | ** Z is a string literal if it doesn't match any column names. In that |
| 777 | ** case, we need to return right away and not make any changes to |
| 778 | ** pExpr. |
| 779 | */ |
| 780 | if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ |
| 781 | sqliteFree(zCol); |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | ** cnt==0 means there was not match. cnt>1 means there were two or |
| 787 | ** more matches. Either way, we have an error. |
| 788 | */ |
| 789 | if( cnt!=1 ){ |
| 790 | char *z = 0; |
| 791 | char *zErr; |
| 792 | zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; |
| 793 | if( zDb ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 794 | sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 795 | }else if( zTab ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 796 | sqlite3SetString(&z, zTab, ".", zCol, 0); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 797 | }else{ |
| 798 | z = sqliteStrDup(zCol); |
| 799 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 800 | sqlite3ErrorMsg(pParse, zErr, z); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 801 | sqliteFree(z); |
| 802 | } |
| 803 | |
| 804 | /* Clean up and return |
| 805 | */ |
| 806 | sqliteFree(zDb); |
| 807 | sqliteFree(zTab); |
| 808 | sqliteFree(zCol); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 809 | sqlite3ExprDelete(pExpr->pLeft); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 810 | pExpr->pLeft = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 811 | sqlite3ExprDelete(pExpr->pRight); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 812 | pExpr->pRight = 0; |
| 813 | pExpr->op = TK_COLUMN; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 814 | sqlite3AuthRead(pParse, pExpr, pSrcList); |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 815 | return cnt!=1; |
| 816 | } |
| 817 | |
| 818 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 819 | ** This routine walks an expression tree and resolves references to |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 820 | ** table columns. Nodes of the form ID.ID or ID resolve into an |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 821 | ** index to the table in the table list and a column offset. The |
| 822 | ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable |
| 823 | ** value is changed to the index of the referenced table in pTabList |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 824 | ** plus the "base" value. The base value will ultimately become the |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 825 | ** VDBE cursor number for a cursor that is pointing into the referenced |
| 826 | ** table. The Expr.iColumn value is changed to the index of the column |
| 827 | ** of the referenced table. The Expr.iColumn value for the special |
| 828 | ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an |
| 829 | ** alias for ROWID. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 830 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 831 | ** We also check for instances of the IN operator. IN comes in two |
| 832 | ** forms: |
| 833 | ** |
| 834 | ** expr IN (exprlist) |
| 835 | ** and |
| 836 | ** expr IN (SELECT ...) |
| 837 | ** |
| 838 | ** The first form is handled by creating a set holding the list |
| 839 | ** of allowed values. The second form causes the SELECT to generate |
| 840 | ** a temporary table. |
| 841 | ** |
| 842 | ** This routine also looks for scalar SELECTs that are part of an expression. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 843 | ** If it finds any, it generates code to write the value of that select |
| 844 | ** into a memory cell. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 845 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 846 | ** Unknown columns or tables provoke an error. The function returns |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 847 | ** the number of errors seen and leaves an error message on pParse->zErrMsg. |
| 848 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 849 | int sqlite3ExprResolveIds( |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 850 | Parse *pParse, /* The parser context */ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 851 | SrcList *pSrcList, /* List of tables used to resolve column names */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 852 | ExprList *pEList, /* List of expressions used to resolve "AS" */ |
| 853 | Expr *pExpr /* The expression to be analyzed. */ |
| 854 | ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 855 | int i; |
| 856 | |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 857 | if( pExpr==0 || pSrcList==0 ) return 0; |
| 858 | for(i=0; i<pSrcList->nSrc; i++){ |
| 859 | assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 860 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 861 | switch( pExpr->op ){ |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 862 | /* Double-quoted strings (ex: "abc") are used as identifiers if |
| 863 | ** possible. Otherwise they remain as strings. Single-quoted |
| 864 | ** strings (ex: 'abc') are always string literals. |
| 865 | */ |
| 866 | case TK_STRING: { |
| 867 | if( pExpr->token.z[0]=='\'' ) break; |
| 868 | /* Fall thru into the TK_ID case if this is a double-quoted string */ |
| 869 | } |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 870 | /* A lone identifier is the name of a columnd. |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 871 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 872 | case TK_ID: { |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 873 | if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 874 | return 1; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 875 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 879 | /* A table name and column name: ID.ID |
| 880 | ** Or a database, table and column: ID.ID.ID |
| 881 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 882 | case TK_DOT: { |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 883 | Token *pColumn; |
| 884 | Token *pTable; |
| 885 | Token *pDb; |
| 886 | Expr *pRight; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 887 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 888 | pRight = pExpr->pRight; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 889 | if( pRight->op==TK_ID ){ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 890 | pDb = 0; |
| 891 | pTable = &pExpr->pLeft->token; |
| 892 | pColumn = &pRight->token; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 893 | }else{ |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 894 | assert( pRight->op==TK_DOT ); |
| 895 | pDb = &pExpr->pLeft->token; |
| 896 | pTable = &pRight->pLeft->token; |
| 897 | pColumn = &pRight->pRight->token; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 898 | } |
drh | 8141f61 | 2004-01-25 22:44:58 +0000 | [diff] [blame] | 899 | if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 900 | return 1; |
| 901 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 902 | break; |
| 903 | } |
| 904 | |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 905 | case TK_IN: { |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 906 | char affinity; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 907 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 908 | KeyInfo keyInfo; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 909 | int addr; /* Address of OP_OpenTemp instruction */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 910 | |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 911 | if( v==0 ) return 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 912 | if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 913 | return 1; |
| 914 | } |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 915 | affinity = sqlite3ExprAffinity(pExpr->pLeft); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 916 | |
| 917 | /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' |
| 918 | ** expression it is handled the same way. A temporary table is |
| 919 | ** filled with single-field index keys representing the results |
| 920 | ** from the SELECT or the <exprlist>. |
| 921 | ** |
| 922 | ** If the 'x' expression is a column value, or the SELECT... |
| 923 | ** statement returns a column value, then the affinity of that |
| 924 | ** column is used to build the index keys. If both 'x' and the |
| 925 | ** SELECT... statement are columns, then numeric affinity is used |
| 926 | ** if either column has NUMERIC or INTEGER affinity. If neither |
| 927 | ** 'x' nor the SELECT... statement are columns, then numeric affinity |
| 928 | ** is used. |
| 929 | */ |
| 930 | pExpr->iTable = pParse->nTab++; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 931 | addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 932 | memset(&keyInfo, 0, sizeof(keyInfo)); |
| 933 | keyInfo.nField = 1; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 934 | sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 935 | |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 936 | if( pExpr->pSelect ){ |
| 937 | /* Case 1: expr IN (SELECT ...) |
| 938 | ** |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 939 | ** Generate code to write the results of the select into the temporary |
| 940 | ** table allocated and opened above. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 941 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 942 | int iParm = pExpr->iTable + (((int)affinity)<<16); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 943 | ExprList *pEList; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 944 | assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 945 | sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 946 | pEList = pExpr->pSelect->pEList; |
| 947 | if( pEList && pEList->nExpr>0 ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 948 | keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 949 | pEList->a[0].pExpr); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 950 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 951 | }else if( pExpr->pList ){ |
| 952 | /* Case 2: expr IN (exprlist) |
| 953 | ** |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 954 | ** For each expression, build an index key from the evaluation and |
| 955 | ** store it in the temporary table. If <expr> is a column, then use |
| 956 | ** that columns affinity when building index keys. If <expr> is not |
| 957 | ** a column, use numeric affinity. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 958 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 959 | int i; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 960 | if( !affinity ){ |
| 961 | affinity = SQLITE_AFF_NUMERIC; |
| 962 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 963 | keyInfo.aColl[0] = pExpr->pLeft->pColl; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 964 | |
| 965 | /* Loop through each expression in <exprlist>. */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 966 | for(i=0; i<pExpr->pList->nExpr; i++){ |
| 967 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 968 | |
| 969 | /* Check that the expression is constant and valid. */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 970 | if( !sqlite3ExprIsConstant(pE2) ){ |
| 971 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 972 | "right-hand side of IN operator must be constant"); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 973 | return 1; |
| 974 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 975 | if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 976 | return 1; |
| 977 | } |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 978 | |
| 979 | /* Evaluate the expression and insert it into the temp table */ |
| 980 | sqlite3ExprCode(pParse, pE2); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 981 | sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 982 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 983 | sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 984 | } |
| 985 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 986 | sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); |
| 987 | |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 988 | break; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 989 | } |
| 990 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 991 | case TK_SELECT: { |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 992 | /* This has to be a scalar SELECT. Generate code to put the |
| 993 | ** value of this select in a memory cell and record the number |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 994 | ** of the memory cell in iColumn. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 995 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 996 | pExpr->iColumn = pParse->nMem++; |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 997 | if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 998 | return 1; |
| 999 | } |
| 1000 | break; |
| 1001 | } |
| 1002 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1003 | /* For all else, just recursively walk the tree */ |
| 1004 | default: { |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 1005 | if( pExpr->pLeft |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1006 | && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1007 | return 1; |
| 1008 | } |
| 1009 | if( pExpr->pRight |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1010 | && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1011 | return 1; |
| 1012 | } |
| 1013 | if( pExpr->pList ){ |
| 1014 | int i; |
| 1015 | ExprList *pList = pExpr->pList; |
| 1016 | for(i=0; i<pList->nExpr; i++){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1017 | Expr *pArg = pList->a[i].pExpr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1018 | if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1019 | return 1; |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1028 | /* |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1029 | ** pExpr is a node that defines a function of some kind. It might |
| 1030 | ** be a syntactic function like "count(x)" or it might be a function |
| 1031 | ** that implements an operator, like "a LIKE b". |
| 1032 | ** |
| 1033 | ** This routine makes *pzName point to the name of the function and |
| 1034 | ** *pnName hold the number of characters in the function name. |
| 1035 | */ |
| 1036 | static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ |
| 1037 | switch( pExpr->op ){ |
| 1038 | case TK_FUNCTION: { |
| 1039 | *pzName = pExpr->token.z; |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 1040 | *pnName = pExpr->token.n; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1041 | break; |
| 1042 | } |
| 1043 | case TK_LIKE: { |
| 1044 | *pzName = "like"; |
| 1045 | *pnName = 4; |
| 1046 | break; |
| 1047 | } |
| 1048 | case TK_GLOB: { |
| 1049 | *pzName = "glob"; |
| 1050 | *pnName = 4; |
| 1051 | break; |
| 1052 | } |
| 1053 | default: { |
| 1054 | *pzName = "can't happen"; |
| 1055 | *pnName = 12; |
| 1056 | break; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1062 | ** Error check the functions in an expression. Make sure all |
| 1063 | ** function names are recognized and all functions have the correct |
| 1064 | ** number of arguments. Leave an error message in pParse->zErrMsg |
| 1065 | ** if anything is amiss. Return the number of errors. |
| 1066 | ** |
| 1067 | ** if pIsAgg is not null and this expression is an aggregate function |
| 1068 | ** (like count(*) or max(value)) then write a 1 into *pIsAgg. |
| 1069 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1070 | int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1071 | int nErr = 0; |
| 1072 | if( pExpr==0 ) return 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1073 | switch( pExpr->op ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1074 | case TK_GLOB: |
| 1075 | case TK_LIKE: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1076 | case TK_FUNCTION: { |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1077 | int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ |
| 1078 | int no_such_func = 0; /* True if no such function exists */ |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1079 | int wrong_num_args = 0; /* True if wrong number of arguments */ |
| 1080 | int is_agg = 0; /* True if is an aggregate function */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1081 | int i; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1082 | int nId; /* Number of characters in function name */ |
| 1083 | const char *zId; /* The function name. */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1084 | FuncDef *pDef; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1085 | int enc = pParse->db->enc; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1086 | |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1087 | getFunctionName(pExpr, &zId, &nId); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1088 | pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1089 | if( pDef==0 ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1090 | pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1091 | if( pDef==0 ){ |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1092 | no_such_func = 1; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1093 | }else{ |
| 1094 | wrong_num_args = 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1095 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1096 | }else{ |
| 1097 | is_agg = pDef->xFunc==0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1098 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1099 | if( is_agg && !allowAgg ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1100 | sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1101 | nErr++; |
| 1102 | is_agg = 0; |
| 1103 | }else if( no_such_func ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1104 | sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1105 | nErr++; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1106 | }else if( wrong_num_args ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1107 | sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1108 | nId, zId); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1109 | nErr++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1110 | } |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1111 | if( is_agg ){ |
| 1112 | pExpr->op = TK_AGG_FUNCTION; |
| 1113 | if( pIsAgg ) *pIsAgg = 1; |
| 1114 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1115 | for(i=0; nErr==0 && i<n; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1116 | nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr, |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 1117 | allowAgg && !is_agg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1118 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1119 | /* FIX ME: Compute pExpr->affinity based on the expected return |
| 1120 | ** type of the function |
| 1121 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1122 | } |
| 1123 | default: { |
| 1124 | if( pExpr->pLeft ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1125 | nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1126 | } |
| 1127 | if( nErr==0 && pExpr->pRight ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1128 | nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1129 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1130 | if( nErr==0 && pExpr->pList ){ |
| 1131 | int n = pExpr->pList->nExpr; |
| 1132 | int i; |
| 1133 | for(i=0; nErr==0 && i<n; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1134 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1135 | nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1138 | break; |
| 1139 | } |
| 1140 | } |
| 1141 | return nErr; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1145 | ** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck(). |
| 1146 | ** |
| 1147 | ** This routine is provided as a convenience since it is very common |
| 1148 | ** to call ResolveIds() and Check() back to back. |
| 1149 | */ |
| 1150 | int sqlite3ExprResolveAndCheck( |
| 1151 | Parse *pParse, /* The parser context */ |
| 1152 | SrcList *pSrcList, /* List of tables used to resolve column names */ |
| 1153 | ExprList *pEList, /* List of expressions used to resolve "AS" */ |
| 1154 | Expr *pExpr, /* The expression to be analyzed. */ |
| 1155 | int allowAgg, /* True to allow aggregate expressions */ |
| 1156 | int *pIsAgg /* Set to TRUE if aggregates are found */ |
| 1157 | ){ |
| 1158 | if( pExpr==0 ) return 0; |
| 1159 | if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){ |
| 1160 | return 1; |
| 1161 | } |
| 1162 | return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg); |
| 1163 | } |
| 1164 | |
| 1165 | /* |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1166 | ** Generate an instruction that will put the integer describe by |
| 1167 | ** text z[0..n-1] on the stack. |
| 1168 | */ |
| 1169 | static void codeInteger(Vdbe *v, const char *z, int n){ |
| 1170 | int i; |
drh | 6fec076 | 2004-05-30 01:38:43 +0000 | [diff] [blame] | 1171 | if( sqlite3GetInt32(z, &i) ){ |
| 1172 | sqlite3VdbeAddOp(v, OP_Integer, i, 0); |
| 1173 | }else if( sqlite3FitsIn64Bits(z) ){ |
| 1174 | sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n); |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1175 | }else{ |
| 1176 | sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1181 | ** Generate code into the current Vdbe to evaluate the given |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1182 | ** expression and leave the result on the top of stack. |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1183 | ** |
| 1184 | ** This code depends on the fact that certain token values (ex: TK_EQ) |
| 1185 | ** are the same as opcode values (ex: OP_Eq) that implement the corresponding |
| 1186 | ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in |
| 1187 | ** the make process cause these values to align. Assert()s in the code |
| 1188 | ** below verify that the numbers are aligned correctly. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1189 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1190 | void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1191 | Vdbe *v = pParse->pVdbe; |
| 1192 | int op; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1193 | if( v==0 || pExpr==0 ) return; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1194 | op = pExpr->op; |
| 1195 | switch( op ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1196 | case TK_COLUMN: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1197 | if( pParse->useAgg ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1198 | sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1199 | }else if( pExpr->iColumn>=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1200 | sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 1201 | #ifndef NDEBUG |
| 1202 | if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){ |
| 1203 | VdbeComment((v, "# %T", &pExpr->span)); |
| 1204 | } |
| 1205 | #endif |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1206 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1207 | sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1208 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1209 | break; |
| 1210 | } |
| 1211 | case TK_INTEGER: { |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1212 | codeInteger(v, pExpr->token.z, pExpr->token.n); |
| 1213 | break; |
| 1214 | } |
| 1215 | case TK_FLOAT: |
| 1216 | case TK_STRING: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1217 | assert( TK_FLOAT==OP_Real ); |
| 1218 | assert( TK_STRING==OP_String8 ); |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1219 | sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1220 | sqlite3VdbeDequoteP3(v, -1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1221 | break; |
| 1222 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1223 | case TK_BLOB: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1224 | assert( TK_BLOB==OP_HexBlob ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1225 | sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1); |
| 1226 | sqlite3VdbeDequoteP3(v, -1); |
| 1227 | break; |
| 1228 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1229 | case TK_NULL: { |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 1230 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1231 | break; |
| 1232 | } |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1233 | case TK_VARIABLE: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1234 | sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1235 | if( pExpr->token.n>1 ){ |
| 1236 | sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); |
| 1237 | } |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1238 | break; |
| 1239 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1240 | case TK_LT: |
| 1241 | case TK_LE: |
| 1242 | case TK_GT: |
| 1243 | case TK_GE: |
| 1244 | case TK_NE: |
| 1245 | case TK_EQ: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1246 | assert( TK_LT==OP_Lt ); |
| 1247 | assert( TK_LE==OP_Le ); |
| 1248 | assert( TK_GT==OP_Gt ); |
| 1249 | assert( TK_GE==OP_Ge ); |
| 1250 | assert( TK_EQ==OP_Eq ); |
| 1251 | assert( TK_NE==OP_Ne ); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1252 | sqlite3ExprCode(pParse, pExpr->pLeft); |
| 1253 | sqlite3ExprCode(pParse, pExpr->pRight); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1254 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1255 | break; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1256 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1257 | case TK_AND: |
| 1258 | case TK_OR: |
| 1259 | case TK_PLUS: |
| 1260 | case TK_STAR: |
| 1261 | case TK_MINUS: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1262 | case TK_REM: |
| 1263 | case TK_BITAND: |
| 1264 | case TK_BITOR: |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1265 | case TK_SLASH: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1266 | case TK_LSHIFT: |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 1267 | case TK_RSHIFT: |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 1268 | case TK_CONCAT: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1269 | assert( TK_AND==OP_And ); |
| 1270 | assert( TK_OR==OP_Or ); |
| 1271 | assert( TK_PLUS==OP_Add ); |
| 1272 | assert( TK_MINUS==OP_Subtract ); |
| 1273 | assert( TK_REM==OP_Remainder ); |
| 1274 | assert( TK_BITAND==OP_BitAnd ); |
| 1275 | assert( TK_BITOR==OP_BitOr ); |
| 1276 | assert( TK_SLASH==OP_Divide ); |
| 1277 | assert( TK_LSHIFT==OP_ShiftLeft ); |
| 1278 | assert( TK_RSHIFT==OP_ShiftRight ); |
| 1279 | assert( TK_CONCAT==OP_Concat ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1280 | sqlite3ExprCode(pParse, pExpr->pLeft); |
| 1281 | sqlite3ExprCode(pParse, pExpr->pRight); |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 1282 | sqlite3VdbeAddOp(v, op, 0, 0); |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 1283 | break; |
| 1284 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1285 | case TK_UMINUS: { |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1286 | Expr *pLeft = pExpr->pLeft; |
| 1287 | assert( pLeft ); |
| 1288 | if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ |
| 1289 | Token *p = &pLeft->token; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1290 | char *z = sqliteMalloc( p->n + 2 ); |
| 1291 | sprintf(z, "-%.*s", p->n, p->z); |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1292 | if( pLeft->op==TK_FLOAT ){ |
| 1293 | sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 1294 | }else{ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1295 | codeInteger(v, z, p->n+1); |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 1296 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1297 | sqliteFree(z); |
| 1298 | break; |
| 1299 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1300 | /* Fall through into TK_NOT */ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1301 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1302 | case TK_BITNOT: |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1303 | case TK_NOT: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1304 | assert( TK_BITNOT==OP_BitNot ); |
| 1305 | assert( TK_NOT==OP_Not ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1306 | sqlite3ExprCode(pParse, pExpr->pLeft); |
| 1307 | sqlite3VdbeAddOp(v, op, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1308 | break; |
| 1309 | } |
| 1310 | case TK_ISNULL: |
| 1311 | case TK_NOTNULL: { |
| 1312 | int dest; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1313 | assert( TK_ISNULL==OP_IsNull ); |
| 1314 | assert( TK_NOTNULL==OP_NotNull ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1315 | sqlite3VdbeAddOp(v, OP_Integer, 1, 0); |
| 1316 | sqlite3ExprCode(pParse, pExpr->pLeft); |
| 1317 | dest = sqlite3VdbeCurrentAddr(v) + 2; |
| 1318 | sqlite3VdbeAddOp(v, op, 1, dest); |
| 1319 | sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1320 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1321 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1322 | case TK_AGG_FUNCTION: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1323 | sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1324 | break; |
| 1325 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1326 | case TK_GLOB: |
| 1327 | case TK_LIKE: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1328 | case TK_FUNCTION: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1329 | ExprList *pList = pExpr->pList; |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 1330 | int nExpr = pList ? pList->nExpr : 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1331 | FuncDef *pDef; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1332 | int nId; |
| 1333 | const char *zId; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1334 | int p2 = 0; |
| 1335 | int i; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1336 | u8 enc = pParse->db->enc; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1337 | CollSeq *pColl = 0; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1338 | getFunctionName(pExpr, &zId, &nId); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1339 | pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1340 | assert( pDef!=0 ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 1341 | nExpr = sqlite3ExprCodeExprList(pParse, pList); |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1342 | for(i=0; i<nExpr && i<32; i++){ |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1343 | if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ |
| 1344 | p2 |= (1<<i); |
| 1345 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1346 | if( pDef->needCollSeq && !pColl ){ |
| 1347 | pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
| 1348 | } |
| 1349 | } |
| 1350 | if( pDef->needCollSeq ){ |
| 1351 | if( !pColl ) pColl = pParse->db->pDfltColl; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1352 | sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1353 | } |
| 1354 | sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1355 | break; |
| 1356 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1357 | case TK_SELECT: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1358 | sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 1359 | VdbeComment((v, "# load subquery result")); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1360 | break; |
| 1361 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1362 | case TK_IN: { |
| 1363 | int addr; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1364 | char affinity; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* Figure out the affinity to use to create a key from the results |
| 1367 | ** of the expression. affinityStr stores a static string suitable for |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 1368 | ** P3 of OP_MakeRecord. |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1369 | */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1370 | affinity = comparisonAffinity(pExpr); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1371 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1372 | sqlite3VdbeAddOp(v, OP_Integer, 1, 0); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1373 | |
| 1374 | /* Code the <expr> from "<expr> IN (...)". The temporary table |
| 1375 | ** pExpr->iTable contains the values that make up the (...) set. |
| 1376 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1377 | sqlite3ExprCode(pParse, pExpr->pLeft); |
| 1378 | addr = sqlite3VdbeCurrentAddr(v); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1379 | sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1380 | sqlite3VdbeAddOp(v, OP_Pop, 2, 0); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 1381 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1382 | sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1383 | sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1384 | sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); |
| 1385 | sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ |
| 1386 | |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1387 | break; |
| 1388 | } |
| 1389 | case TK_BETWEEN: { |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1390 | Expr *pLeft = pExpr->pLeft; |
| 1391 | struct ExprList_item *pLItem = pExpr->pList->a; |
| 1392 | Expr *pRight = pLItem->pExpr; |
| 1393 | sqlite3ExprCode(pParse, pLeft); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1394 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1395 | sqlite3ExprCode(pParse, pRight); |
| 1396 | codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1397 | sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1398 | pLItem++; |
| 1399 | pRight = pLItem->pExpr; |
| 1400 | sqlite3ExprCode(pParse, pRight); |
| 1401 | codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1402 | sqlite3VdbeAddOp(v, OP_And, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1403 | break; |
| 1404 | } |
drh | 51e9a44 | 2004-01-16 16:42:53 +0000 | [diff] [blame] | 1405 | case TK_UPLUS: |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 1406 | case TK_AS: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1407 | sqlite3ExprCode(pParse, pExpr->pLeft); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 1408 | break; |
| 1409 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1410 | case TK_CASE: { |
| 1411 | int expr_end_label; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1412 | int jumpInst; |
| 1413 | int addr; |
| 1414 | int nExpr; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1415 | int i; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1416 | ExprList *pEList; |
| 1417 | struct ExprList_item *aListelem; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1418 | |
| 1419 | assert(pExpr->pList); |
| 1420 | assert((pExpr->pList->nExpr % 2) == 0); |
| 1421 | assert(pExpr->pList->nExpr > 0); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1422 | pEList = pExpr->pList; |
| 1423 | aListelem = pEList->a; |
| 1424 | nExpr = pEList->nExpr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1425 | expr_end_label = sqlite3VdbeMakeLabel(v); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1426 | if( pExpr->pLeft ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1427 | sqlite3ExprCode(pParse, pExpr->pLeft); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1428 | } |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1429 | for(i=0; i<nExpr; i=i+2){ |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1430 | sqlite3ExprCode(pParse, aListelem[i].pExpr); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1431 | if( pExpr->pLeft ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1432 | sqlite3VdbeAddOp(v, OP_Dup, 1, 1); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1433 | jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, |
| 1434 | OP_Ne, 0, 1); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1435 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1436 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1437 | jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1438 | } |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1439 | sqlite3ExprCode(pParse, aListelem[i+1].pExpr); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1440 | sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); |
| 1441 | addr = sqlite3VdbeCurrentAddr(v); |
| 1442 | sqlite3VdbeChangeP2(v, jumpInst, addr); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1443 | } |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 1444 | if( pExpr->pLeft ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1445 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 1446 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1447 | if( pExpr->pRight ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1448 | sqlite3ExprCode(pParse, pExpr->pRight); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1449 | }else{ |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 1450 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1451 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1452 | sqlite3VdbeResolveLabel(v, expr_end_label); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 1453 | break; |
| 1454 | } |
| 1455 | case TK_RAISE: { |
| 1456 | if( !pParse->trigStack ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1457 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 1458 | "RAISE() may only be used within a trigger-program"); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 1459 | return; |
| 1460 | } |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 1461 | if( pExpr->iColumn!=OE_Ignore ){ |
| 1462 | assert( pExpr->iColumn==OE_Rollback || |
| 1463 | pExpr->iColumn == OE_Abort || |
| 1464 | pExpr->iColumn == OE_Fail ); |
| 1465 | sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, |
| 1466 | pExpr->token.z, pExpr->token.n); |
| 1467 | sqlite3VdbeDequoteP3(v, -1); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 1468 | } else { |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 1469 | assert( pExpr->iColumn == OE_Ignore ); |
| 1470 | sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); |
| 1471 | sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); |
| 1472 | VdbeComment((v, "# raise(IGNORE)")); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 1473 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1474 | } |
| 1475 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1476 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /* |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1480 | ** Generate code that pushes the value of every element of the given |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 1481 | ** expression list onto the stack. |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1482 | ** |
| 1483 | ** Return the number of elements pushed onto the stack. |
| 1484 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1485 | int sqlite3ExprCodeExprList( |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1486 | Parse *pParse, /* Parsing context */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 1487 | ExprList *pList /* The expression list to be coded */ |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1488 | ){ |
| 1489 | struct ExprList_item *pItem; |
| 1490 | int i, n; |
| 1491 | Vdbe *v; |
| 1492 | if( pList==0 ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1493 | v = sqlite3GetVdbe(pParse); |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1494 | n = pList->nExpr; |
| 1495 | for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1496 | sqlite3ExprCode(pParse, pItem->pExpr); |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1497 | } |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 1498 | return n; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1502 | ** Generate code for a boolean expression such that a jump is made |
| 1503 | ** to the label "dest" if the expression is true but execution |
| 1504 | ** continues straight thru if the expression is false. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1505 | ** |
| 1506 | ** If the expression evaluates to NULL (neither true nor false), then |
| 1507 | ** take the jump if the jumpIfNull flag is true. |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1508 | ** |
| 1509 | ** This code depends on the fact that certain token values (ex: TK_EQ) |
| 1510 | ** are the same as opcode values (ex: OP_Eq) that implement the corresponding |
| 1511 | ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in |
| 1512 | ** the make process cause these values to align. Assert()s in the code |
| 1513 | ** below verify that the numbers are aligned correctly. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1514 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1515 | void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1516 | Vdbe *v = pParse->pVdbe; |
| 1517 | int op = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1518 | if( v==0 || pExpr==0 ) return; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1519 | op = pExpr->op; |
| 1520 | switch( op ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1521 | case TK_AND: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1522 | int d2 = sqlite3VdbeMakeLabel(v); |
| 1523 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); |
| 1524 | sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
| 1525 | sqlite3VdbeResolveLabel(v, d2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1526 | break; |
| 1527 | } |
| 1528 | case TK_OR: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1529 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 1530 | sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1531 | break; |
| 1532 | } |
| 1533 | case TK_NOT: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1534 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1535 | break; |
| 1536 | } |
| 1537 | case TK_LT: |
| 1538 | case TK_LE: |
| 1539 | case TK_GT: |
| 1540 | case TK_GE: |
| 1541 | case TK_NE: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 1542 | case TK_EQ: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1543 | assert( TK_LT==OP_Lt ); |
| 1544 | assert( TK_LE==OP_Le ); |
| 1545 | assert( TK_GT==OP_Gt ); |
| 1546 | assert( TK_GE==OP_Ge ); |
| 1547 | assert( TK_EQ==OP_Eq ); |
| 1548 | assert( TK_NE==OP_Ne ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1549 | sqlite3ExprCode(pParse, pExpr->pLeft); |
| 1550 | sqlite3ExprCode(pParse, pExpr->pRight); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1551 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1552 | break; |
| 1553 | } |
| 1554 | case TK_ISNULL: |
| 1555 | case TK_NOTNULL: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1556 | assert( TK_ISNULL==OP_IsNull ); |
| 1557 | assert( TK_NOTNULL==OP_NotNull ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1558 | sqlite3ExprCode(pParse, pExpr->pLeft); |
| 1559 | sqlite3VdbeAddOp(v, op, 1, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1560 | break; |
| 1561 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1562 | case TK_BETWEEN: { |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1563 | /* The expression "x BETWEEN y AND z" is implemented as: |
| 1564 | ** |
| 1565 | ** 1 IF (x < y) GOTO 3 |
| 1566 | ** 2 IF (x <= z) GOTO <dest> |
| 1567 | ** 3 ... |
| 1568 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1569 | int addr; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1570 | Expr *pLeft = pExpr->pLeft; |
| 1571 | Expr *pRight = pExpr->pList->a[0].pExpr; |
| 1572 | sqlite3ExprCode(pParse, pLeft); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1573 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1574 | sqlite3ExprCode(pParse, pRight); |
| 1575 | addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1576 | |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1577 | pRight = pExpr->pList->a[1].pExpr; |
| 1578 | sqlite3ExprCode(pParse, pRight); |
| 1579 | codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1580 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1581 | sqlite3VdbeAddOp(v, OP_Integer, 0, 0); |
| 1582 | sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); |
| 1583 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1584 | break; |
| 1585 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1586 | default: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1587 | sqlite3ExprCode(pParse, pExpr); |
| 1588 | sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1589 | break; |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /* |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 1595 | ** Generate code for a boolean expression such that a jump is made |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1596 | ** to the label "dest" if the expression is false but execution |
| 1597 | ** continues straight thru if the expression is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1598 | ** |
| 1599 | ** If the expression evaluates to NULL (neither true nor false) then |
| 1600 | ** jump if jumpIfNull is true or fall through if jumpIfNull is false. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1601 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1602 | void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1603 | Vdbe *v = pParse->pVdbe; |
| 1604 | int op = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1605 | if( v==0 || pExpr==0 ) return; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame^] | 1606 | |
| 1607 | /* The value of pExpr->op and op are related as follows: |
| 1608 | ** |
| 1609 | ** pExpr->op op |
| 1610 | ** --------- ---------- |
| 1611 | ** TK_ISNULL OP_NotNull |
| 1612 | ** TK_NOTNULL OP_IsNull |
| 1613 | ** TK_NE OP_Eq |
| 1614 | ** TK_EQ OP_Ne |
| 1615 | ** TK_GT OP_Le |
| 1616 | ** TK_LE OP_Gt |
| 1617 | ** TK_GE OP_Lt |
| 1618 | ** TK_LT OP_Ge |
| 1619 | ** |
| 1620 | ** For other values of pExpr->op, op is undefined and unused. |
| 1621 | ** The value of TK_ and OP_ constants are arranged such that we |
| 1622 | ** can compute the mapping above using the following expression. |
| 1623 | ** Assert()s verify that the computation is correct. |
| 1624 | */ |
| 1625 | op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); |
| 1626 | |
| 1627 | /* Verify correct alignment of TK_ and OP_ constants |
| 1628 | */ |
| 1629 | assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); |
| 1630 | assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); |
| 1631 | assert( pExpr->op!=TK_NE || op==OP_Eq ); |
| 1632 | assert( pExpr->op!=TK_EQ || op==OP_Ne ); |
| 1633 | assert( pExpr->op!=TK_LT || op==OP_Ge ); |
| 1634 | assert( pExpr->op!=TK_LE || op==OP_Gt ); |
| 1635 | assert( pExpr->op!=TK_GT || op==OP_Le ); |
| 1636 | assert( pExpr->op!=TK_GE || op==OP_Lt ); |
| 1637 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1638 | switch( pExpr->op ){ |
| 1639 | case TK_AND: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1640 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 1641 | sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1642 | break; |
| 1643 | } |
| 1644 | case TK_OR: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1645 | int d2 = sqlite3VdbeMakeLabel(v); |
| 1646 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); |
| 1647 | sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
| 1648 | sqlite3VdbeResolveLabel(v, d2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1649 | break; |
| 1650 | } |
| 1651 | case TK_NOT: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1652 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1653 | break; |
| 1654 | } |
| 1655 | case TK_LT: |
| 1656 | case TK_LE: |
| 1657 | case TK_GT: |
| 1658 | case TK_GE: |
| 1659 | case TK_NE: |
| 1660 | case TK_EQ: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1661 | sqlite3ExprCode(pParse, pExpr->pLeft); |
| 1662 | sqlite3ExprCode(pParse, pExpr->pRight); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1663 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1664 | break; |
| 1665 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1666 | case TK_ISNULL: |
| 1667 | case TK_NOTNULL: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1668 | sqlite3ExprCode(pParse, pExpr->pLeft); |
| 1669 | sqlite3VdbeAddOp(v, op, 1, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1670 | break; |
| 1671 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1672 | case TK_BETWEEN: { |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1673 | /* The expression is "x BETWEEN y AND z". It is implemented as: |
| 1674 | ** |
| 1675 | ** 1 IF (x >= y) GOTO 3 |
| 1676 | ** 2 GOTO <dest> |
| 1677 | ** 3 IF (x > z) GOTO <dest> |
| 1678 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1679 | int addr; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1680 | Expr *pLeft = pExpr->pLeft; |
| 1681 | Expr *pRight = pExpr->pList->a[0].pExpr; |
| 1682 | sqlite3ExprCode(pParse, pLeft); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1683 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1684 | sqlite3ExprCode(pParse, pRight); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1685 | addr = sqlite3VdbeCurrentAddr(v); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1686 | codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); |
| 1687 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1688 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
| 1689 | sqlite3VdbeAddOp(v, OP_Goto, 0, dest); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1690 | pRight = pExpr->pList->a[1].pExpr; |
| 1691 | sqlite3ExprCode(pParse, pRight); |
| 1692 | codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1695 | default: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1696 | sqlite3ExprCode(pParse, pExpr); |
| 1697 | sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1698 | break; |
| 1699 | } |
| 1700 | } |
| 1701 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1702 | |
| 1703 | /* |
| 1704 | ** Do a deep comparison of two expression trees. Return TRUE (non-zero) |
| 1705 | ** if they are identical and return FALSE if they differ in any way. |
| 1706 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1707 | int sqlite3ExprCompare(Expr *pA, Expr *pB){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1708 | int i; |
| 1709 | if( pA==0 ){ |
| 1710 | return pB==0; |
| 1711 | }else if( pB==0 ){ |
| 1712 | return 0; |
| 1713 | } |
| 1714 | if( pA->op!=pB->op ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1715 | if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; |
| 1716 | if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1717 | if( pA->pList ){ |
| 1718 | if( pB->pList==0 ) return 0; |
| 1719 | if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; |
| 1720 | for(i=0; i<pA->pList->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1721 | if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1722 | return 0; |
| 1723 | } |
| 1724 | } |
| 1725 | }else if( pB->pList ){ |
| 1726 | return 0; |
| 1727 | } |
| 1728 | if( pA->pSelect || pB->pSelect ) return 0; |
drh | 2f2c01e | 2002-07-02 13:05:04 +0000 | [diff] [blame] | 1729 | if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1730 | if( pA->token.z ){ |
| 1731 | if( pB->token.z==0 ) return 0; |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 1732 | if( pB->token.n!=pA->token.n ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1733 | if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1734 | } |
| 1735 | return 1; |
| 1736 | } |
| 1737 | |
| 1738 | /* |
| 1739 | ** Add a new element to the pParse->aAgg[] array and return its index. |
| 1740 | */ |
| 1741 | static int appendAggInfo(Parse *pParse){ |
| 1742 | if( (pParse->nAgg & 0x7)==0 ){ |
| 1743 | int amt = pParse->nAgg + 8; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1744 | AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); |
| 1745 | if( aAgg==0 ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1746 | return -1; |
| 1747 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1748 | pParse->aAgg = aAgg; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1749 | } |
| 1750 | memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); |
| 1751 | return pParse->nAgg++; |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | ** Analyze the given expression looking for aggregate functions and |
| 1756 | ** for variables that need to be added to the pParse->aAgg[] array. |
| 1757 | ** Make additional entries to the pParse->aAgg[] array as necessary. |
| 1758 | ** |
| 1759 | ** This routine should only be called after the expression has been |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1760 | ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck(). |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1761 | ** |
| 1762 | ** If errors are seen, leave an error message in zErrMsg and return |
| 1763 | ** the number of errors. |
| 1764 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1765 | int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1766 | int i; |
| 1767 | AggExpr *aAgg; |
| 1768 | int nErr = 0; |
| 1769 | |
| 1770 | if( pExpr==0 ) return 0; |
| 1771 | switch( pExpr->op ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1772 | case TK_COLUMN: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1773 | aAgg = pParse->aAgg; |
| 1774 | for(i=0; i<pParse->nAgg; i++){ |
| 1775 | if( aAgg[i].isAgg ) continue; |
| 1776 | if( aAgg[i].pExpr->iTable==pExpr->iTable |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1777 | && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1778 | break; |
| 1779 | } |
| 1780 | } |
| 1781 | if( i>=pParse->nAgg ){ |
| 1782 | i = appendAggInfo(pParse); |
| 1783 | if( i<0 ) return 1; |
| 1784 | pParse->aAgg[i].isAgg = 0; |
| 1785 | pParse->aAgg[i].pExpr = pExpr; |
| 1786 | } |
drh | aaf8872 | 2000-06-08 11:25:00 +0000 | [diff] [blame] | 1787 | pExpr->iAgg = i; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1788 | break; |
| 1789 | } |
| 1790 | case TK_AGG_FUNCTION: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1791 | aAgg = pParse->aAgg; |
| 1792 | for(i=0; i<pParse->nAgg; i++){ |
| 1793 | if( !aAgg[i].isAgg ) continue; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1794 | if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1795 | break; |
| 1796 | } |
| 1797 | } |
| 1798 | if( i>=pParse->nAgg ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1799 | u8 enc = pParse->db->enc; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1800 | i = appendAggInfo(pParse); |
| 1801 | if( i<0 ) return 1; |
| 1802 | pParse->aAgg[i].isAgg = 1; |
| 1803 | pParse->aAgg[i].pExpr = pExpr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1804 | pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 1805 | pExpr->token.z, pExpr->token.n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1806 | pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1807 | } |
| 1808 | pExpr->iAgg = i; |
| 1809 | break; |
| 1810 | } |
| 1811 | default: { |
| 1812 | if( pExpr->pLeft ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1813 | nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1814 | } |
| 1815 | if( nErr==0 && pExpr->pRight ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1816 | nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1817 | } |
| 1818 | if( nErr==0 && pExpr->pList ){ |
| 1819 | int n = pExpr->pList->nExpr; |
| 1820 | int i; |
| 1821 | for(i=0; nErr==0 && i<n; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1822 | nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | break; |
| 1826 | } |
| 1827 | } |
| 1828 | return nErr; |
| 1829 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1830 | |
| 1831 | /* |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1832 | ** Locate a user function given a name, a number of arguments and a flag |
| 1833 | ** indicating whether the function prefers UTF-16 over UTF-8. Return a |
| 1834 | ** pointer to the FuncDef structure that defines that function, or return |
| 1835 | ** NULL if the function does not exist. |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1836 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1837 | ** If the createFlag argument is true, then a new (blank) FuncDef |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1838 | ** structure is created and liked into the "db" structure if a |
| 1839 | ** no matching function previously existed. When createFlag is true |
| 1840 | ** and the nArg parameter is -1, then only a function that accepts |
| 1841 | ** any number of arguments will be returned. |
| 1842 | ** |
| 1843 | ** If createFlag is false and nArg is -1, then the first valid |
| 1844 | ** function found is returned. A function is valid if either xFunc |
| 1845 | ** or xStep is non-zero. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1846 | ** |
| 1847 | ** If createFlag is false, then a function with the required name and |
| 1848 | ** number of arguments may be returned even if the eTextRep flag does not |
| 1849 | ** match that requested. |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1850 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1851 | FuncDef *sqlite3FindFunction( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1852 | sqlite3 *db, /* An open database */ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1853 | const char *zName, /* Name of the function. Not null-terminated */ |
| 1854 | int nName, /* Number of characters in the name */ |
| 1855 | int nArg, /* Number of arguments. -1 means any number */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1856 | u8 enc, /* Preferred text encoding */ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1857 | int createFlag /* Create new entry if true and does not otherwise exist */ |
| 1858 | ){ |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1859 | FuncDef *p; /* Iterator variable */ |
| 1860 | FuncDef *pFirst; /* First function with this name */ |
| 1861 | FuncDef *pBest = 0; /* Best match found so far */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1862 | int bestmatch = 0; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1863 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1864 | |
| 1865 | assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1866 | if( nArg<-1 ) nArg = -1; |
| 1867 | |
| 1868 | pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName); |
| 1869 | for(p=pFirst; p; p=p->pNext){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1870 | /* During the search for the best function definition, bestmatch is set |
| 1871 | ** as follows to indicate the quality of the match with the definition |
| 1872 | ** pointed to by pBest: |
| 1873 | ** |
| 1874 | ** 0: pBest is NULL. No match has been found. |
| 1875 | ** 1: A variable arguments function that prefers UTF-8 when a UTF-16 |
| 1876 | ** encoding is requested, or vice versa. |
| 1877 | ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is |
| 1878 | ** requested, or vice versa. |
| 1879 | ** 3: A variable arguments function using the same text encoding. |
| 1880 | ** 4: A function with the exact number of arguments requested that |
| 1881 | ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa. |
| 1882 | ** 5: A function with the exact number of arguments requested that |
| 1883 | ** prefers UTF-16LE when UTF-16BE is requested, or vice versa. |
| 1884 | ** 6: An exact match. |
| 1885 | ** |
| 1886 | ** A larger value of 'matchqual' indicates a more desirable match. |
| 1887 | */ |
danielk1977 | e12c17b | 2004-06-23 12:35:14 +0000 | [diff] [blame] | 1888 | if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1889 | int match = 1; /* Quality of this match */ |
| 1890 | if( p->nArg==nArg || nArg==-1 ){ |
| 1891 | match = 4; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1892 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1893 | if( enc==p->iPrefEnc ){ |
| 1894 | match += 2; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1895 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1896 | else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) || |
| 1897 | (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){ |
| 1898 | match += 1; |
| 1899 | } |
| 1900 | |
| 1901 | if( match>bestmatch ){ |
| 1902 | pBest = p; |
| 1903 | bestmatch = match; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1904 | } |
| 1905 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1906 | } |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1907 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1908 | /* If the createFlag parameter is true, and the seach did not reveal an |
| 1909 | ** exact match for the name, number of arguments and encoding, then add a |
| 1910 | ** new entry to the hash table and return it. |
| 1911 | */ |
| 1912 | if( createFlag && bestmatch<6 && |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1913 | (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){ |
| 1914 | pBest->nArg = nArg; |
| 1915 | pBest->pNext = pFirst; |
| 1916 | pBest->zName = (char*)&pBest[1]; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1917 | pBest->iPrefEnc = enc; |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1918 | memcpy(pBest->zName, zName, nName); |
| 1919 | pBest->zName[nName] = 0; |
| 1920 | sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1921 | } |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1922 | |
| 1923 | if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){ |
| 1924 | return pBest; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1925 | } |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 1926 | return 0; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1927 | } |