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