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