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