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 | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 15 | ** $Id: expr.c,v 1.76 2002/06/29 02:20:08 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 | |
| 20 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 21 | ** Construct a new expression node and return a pointer to it. Memory |
| 22 | ** for this node is obtained from sqliteMalloc(). The calling function |
| 23 | ** is responsible for making sure the node eventually gets freed. |
| 24 | */ |
| 25 | Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ |
| 26 | Expr *pNew; |
| 27 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 28 | if( pNew==0 ){ |
| 29 | sqliteExprDelete(pLeft); |
| 30 | sqliteExprDelete(pRight); |
| 31 | return 0; |
| 32 | } |
| 33 | pNew->op = op; |
| 34 | pNew->pLeft = pLeft; |
| 35 | pNew->pRight = pRight; |
| 36 | if( pToken ){ |
| 37 | pNew->token = *pToken; |
| 38 | }else{ |
| 39 | pNew->token.z = 0; |
| 40 | pNew->token.n = 0; |
| 41 | } |
| 42 | if( pLeft && pRight ){ |
| 43 | sqliteExprSpan(pNew, &pLeft->span, &pRight->span); |
| 44 | }else{ |
| 45 | pNew->span = pNew->token; |
| 46 | } |
| 47 | return pNew; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | ** Set the Expr.token field of the given expression to span all |
| 52 | ** text between the two given tokens. |
| 53 | */ |
| 54 | void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
| 55 | if( pExpr ){ |
| 56 | pExpr->span.z = pLeft->z; |
| 57 | pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | ** Construct a new expression node for a function with multiple |
| 63 | ** arguments. |
| 64 | */ |
| 65 | Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ |
| 66 | Expr *pNew; |
| 67 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 68 | if( pNew==0 ){ |
| 69 | sqliteExprListDelete(pList); |
| 70 | return 0; |
| 71 | } |
| 72 | pNew->op = TK_FUNCTION; |
| 73 | pNew->pList = pList; |
| 74 | if( pToken ){ |
| 75 | pNew->token = *pToken; |
| 76 | }else{ |
| 77 | pNew->token.z = 0; |
| 78 | pNew->token.n = 0; |
| 79 | } |
| 80 | return pNew; |
| 81 | } |
| 82 | |
| 83 | /* |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 84 | ** Recursively delete an expression tree. |
| 85 | */ |
| 86 | void sqliteExprDelete(Expr *p){ |
| 87 | if( p==0 ) return; |
drh | 75148a2 | 2002-03-03 03:42:31 +0000 | [diff] [blame] | 88 | if( p->pLeft ) sqliteExprDelete(p->pLeft); |
| 89 | if( p->pRight ) sqliteExprDelete(p->pRight); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 90 | if( p->pList ) sqliteExprListDelete(p->pList); |
| 91 | if( p->pSelect ) sqliteSelectDelete(p->pSelect); |
| 92 | sqliteFree(p); |
| 93 | } |
| 94 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 95 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 96 | ** The following group of functions are used to translate the string |
| 97 | ** pointers of tokens in expression from one buffer to another. |
| 98 | ** |
| 99 | ** Normally, the Expr.token.z and Expr.span.z fields point into the |
| 100 | ** original input buffer of an SQL statement. This is usually OK |
| 101 | ** since the SQL statement is executed and the expression is deleted |
| 102 | ** before the input buffer is freed. Making the tokens point to the |
| 103 | ** original input buffer saves many calls to malloc() and thus helps |
| 104 | ** the library to run faster. |
| 105 | ** |
| 106 | ** But sometimes we need an expression to persist past the time when |
| 107 | ** the input buffer is freed. (Example: The SELECT clause of a |
| 108 | ** CREATE VIEW statement contains expressions that must persist for |
| 109 | ** the life of the view.) When that happens we have to make a |
| 110 | ** persistent copy of the input buffer and translate the Expr.token.z |
| 111 | ** and Expr.span.z fields to point to the copy rather than the |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 112 | ** original input buffer. The following group of routines handle that |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 113 | ** translation. |
| 114 | ** |
| 115 | ** The "offset" parameter is the distance from the original input buffer |
| 116 | ** to the persistent copy. These routines recursively walk the entire |
| 117 | ** expression tree and shift all tokens by "offset" amount. |
| 118 | ** |
| 119 | ** The work of figuring out the appropriate "offset" and making the |
| 120 | ** presistent copy of the input buffer is done by the calling routine. |
| 121 | */ |
| 122 | void sqliteExprMoveStrings(Expr *p, int offset){ |
| 123 | if( p==0 ) return; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 124 | if( !p->staticToken ){ |
| 125 | if( p->token.z ) p->token.z += offset; |
| 126 | if( p->span.z ) p->span.z += offset; |
| 127 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 128 | if( p->pLeft ) sqliteExprMoveStrings(p->pLeft, offset); |
| 129 | if( p->pRight ) sqliteExprMoveStrings(p->pRight, offset); |
| 130 | if( p->pList ) sqliteExprListMoveStrings(p->pList, offset); |
| 131 | if( p->pSelect ) sqliteSelectMoveStrings(p->pSelect, offset); |
| 132 | } |
| 133 | void sqliteExprListMoveStrings(ExprList *pList, int offset){ |
| 134 | int i; |
| 135 | if( pList==0 ) return; |
| 136 | for(i=0; i<pList->nExpr; i++){ |
| 137 | sqliteExprMoveStrings(pList->a[i].pExpr, offset); |
| 138 | } |
| 139 | } |
| 140 | void sqliteSelectMoveStrings(Select *pSelect, int offset){ |
| 141 | if( pSelect==0 ) return; |
| 142 | sqliteExprListMoveStrings(pSelect->pEList, offset); |
| 143 | sqliteExprMoveStrings(pSelect->pWhere, offset); |
| 144 | sqliteExprListMoveStrings(pSelect->pGroupBy, offset); |
| 145 | sqliteExprMoveStrings(pSelect->pHaving, offset); |
| 146 | sqliteExprListMoveStrings(pSelect->pOrderBy, offset); |
| 147 | sqliteSelectMoveStrings(pSelect->pPrior, offset); |
| 148 | } |
| 149 | |
| 150 | /* |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 151 | ** The following group of routines make deep copies of expressions, |
| 152 | ** expression lists, ID lists, and select statements. The copies can |
| 153 | ** be deleted (by being passed to their respective ...Delete() routines) |
| 154 | ** without effecting the originals. |
| 155 | ** |
| 156 | ** Note, however, that the Expr.token.z and Expr.span.z fields point to |
| 157 | ** string space that is allocated separately from the expression tree |
| 158 | ** itself. These routines do NOT duplicate that string space. |
| 159 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 160 | ** The expression list, ID, and source lists return by sqliteExprListDup(), |
| 161 | ** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded |
| 162 | ** by subsequent calls to sqlite*ListAppend() routines. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 163 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 164 | ** Any tables that the SrcList might point to are not duplicated. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 165 | */ |
| 166 | Expr *sqliteExprDup(Expr *p){ |
| 167 | Expr *pNew; |
| 168 | if( p==0 ) return 0; |
| 169 | pNew = sqliteMalloc( sizeof(*p) ); |
| 170 | if( pNew==0 ) return 0; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 171 | memcpy(pNew, p, sizeof(*pNew)); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 172 | pNew->pLeft = sqliteExprDup(p->pLeft); |
| 173 | pNew->pRight = sqliteExprDup(p->pRight); |
| 174 | pNew->pList = sqliteExprListDup(p->pList); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 175 | pNew->pSelect = sqliteSelectDup(p->pSelect); |
| 176 | return pNew; |
| 177 | } |
| 178 | ExprList *sqliteExprListDup(ExprList *p){ |
| 179 | ExprList *pNew; |
| 180 | int i; |
| 181 | if( p==0 ) return 0; |
| 182 | pNew = sqliteMalloc( sizeof(*pNew) ); |
| 183 | if( pNew==0 ) return 0; |
| 184 | pNew->nExpr = p->nExpr; |
| 185 | pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); |
drh | e4697f5 | 2002-05-23 02:09:03 +0000 | [diff] [blame] | 186 | if( pNew->a==0 ) return 0; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 187 | for(i=0; i<p->nExpr; i++){ |
| 188 | pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr); |
| 189 | pNew->a[i].zName = sqliteStrDup(p->a[i].zName); |
| 190 | pNew->a[i].sortOrder = p->a[i].sortOrder; |
| 191 | pNew->a[i].isAgg = p->a[i].isAgg; |
| 192 | pNew->a[i].done = 0; |
| 193 | } |
| 194 | return pNew; |
| 195 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 196 | SrcList *sqliteSrcListDup(SrcList *p){ |
| 197 | SrcList *pNew; |
| 198 | int i; |
| 199 | if( p==0 ) return 0; |
| 200 | pNew = sqliteMalloc( sizeof(*pNew) ); |
| 201 | if( pNew==0 ) return 0; |
| 202 | pNew->nSrc = p->nSrc; |
| 203 | pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) ); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 204 | if( pNew->a==0 && p->nSrc != 0 ) return 0; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 205 | for(i=0; i<p->nSrc; i++){ |
| 206 | pNew->a[i].zName = sqliteStrDup(p->a[i].zName); |
| 207 | pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias); |
| 208 | pNew->a[i].jointype = p->a[i].jointype; |
| 209 | pNew->a[i].pTab = 0; |
| 210 | pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect); |
| 211 | pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn); |
| 212 | pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing); |
| 213 | } |
| 214 | return pNew; |
| 215 | } |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 216 | IdList *sqliteIdListDup(IdList *p){ |
| 217 | IdList *pNew; |
| 218 | int i; |
| 219 | if( p==0 ) return 0; |
| 220 | pNew = sqliteMalloc( sizeof(*pNew) ); |
| 221 | if( pNew==0 ) return 0; |
| 222 | pNew->nId = p->nId; |
| 223 | pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) ); |
drh | e4697f5 | 2002-05-23 02:09:03 +0000 | [diff] [blame] | 224 | if( pNew->a==0 ) return 0; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 225 | for(i=0; i<p->nId; i++){ |
| 226 | pNew->a[i].zName = sqliteStrDup(p->a[i].zName); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 227 | pNew->a[i].idx = p->a[i].idx; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 228 | } |
| 229 | return pNew; |
| 230 | } |
| 231 | Select *sqliteSelectDup(Select *p){ |
| 232 | Select *pNew; |
| 233 | if( p==0 ) return 0; |
| 234 | pNew = sqliteMalloc( sizeof(*p) ); |
| 235 | if( pNew==0 ) return 0; |
| 236 | pNew->isDistinct = p->isDistinct; |
| 237 | pNew->pEList = sqliteExprListDup(p->pEList); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 238 | pNew->pSrc = sqliteSrcListDup(p->pSrc); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 239 | pNew->pWhere = sqliteExprDup(p->pWhere); |
| 240 | pNew->pGroupBy = sqliteExprListDup(p->pGroupBy); |
| 241 | pNew->pHaving = sqliteExprDup(p->pHaving); |
| 242 | pNew->pOrderBy = sqliteExprListDup(p->pOrderBy); |
| 243 | pNew->op = p->op; |
| 244 | pNew->pPrior = sqliteSelectDup(p->pPrior); |
| 245 | pNew->nLimit = p->nLimit; |
| 246 | pNew->nOffset = p->nOffset; |
| 247 | pNew->zSelect = 0; |
| 248 | return pNew; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 253 | ** Add a new element to the end of an expression list. If pList is |
| 254 | ** initially NULL, then create a new expression list. |
| 255 | */ |
| 256 | ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ |
| 257 | int i; |
| 258 | if( pList==0 ){ |
| 259 | pList = sqliteMalloc( sizeof(ExprList) ); |
| 260 | if( pList==0 ){ |
| 261 | sqliteExprDelete(pExpr); |
| 262 | return 0; |
| 263 | } |
| 264 | } |
| 265 | if( (pList->nExpr & 7)==0 ){ |
| 266 | int n = pList->nExpr + 8; |
| 267 | struct ExprList_item *a; |
| 268 | a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); |
| 269 | if( a==0 ){ |
| 270 | sqliteExprDelete(pExpr); |
| 271 | return pList; |
| 272 | } |
| 273 | pList->a = a; |
| 274 | } |
| 275 | if( pExpr || pName ){ |
| 276 | i = pList->nExpr++; |
| 277 | pList->a[i].pExpr = pExpr; |
| 278 | pList->a[i].zName = 0; |
| 279 | if( pName ){ |
| 280 | sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); |
| 281 | sqliteDequote(pList->a[i].zName); |
| 282 | } |
| 283 | } |
| 284 | return pList; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | ** Delete an entire expression list. |
| 289 | */ |
| 290 | void sqliteExprListDelete(ExprList *pList){ |
| 291 | int i; |
| 292 | if( pList==0 ) return; |
| 293 | for(i=0; i<pList->nExpr; i++){ |
| 294 | sqliteExprDelete(pList->a[i].pExpr); |
| 295 | sqliteFree(pList->a[i].zName); |
| 296 | } |
| 297 | sqliteFree(pList->a); |
| 298 | sqliteFree(pList); |
| 299 | } |
| 300 | |
| 301 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 302 | ** Walk an expression tree. Return 1 if the expression is constant |
| 303 | ** and 0 if it involves variables. |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 304 | ** |
| 305 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 306 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 307 | ** a constant. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 308 | */ |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 309 | int sqliteExprIsConstant(Expr *p){ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 310 | switch( p->op ){ |
| 311 | case TK_ID: |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 312 | case TK_COLUMN: |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 313 | case TK_DOT: |
| 314 | return 0; |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 315 | case TK_STRING: |
| 316 | return p->token.z[0]=='\''; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 317 | case TK_INTEGER: |
| 318 | case TK_FLOAT: |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 319 | return 1; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 320 | default: { |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 321 | if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0; |
| 322 | if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 323 | if( p->pList ){ |
| 324 | int i; |
| 325 | for(i=0; i<p->pList->nExpr; i++){ |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 326 | if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 329 | return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 332 | return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 336 | ** If the given expression codes a constant integer, return 1 and put |
| 337 | ** the value of the integer in *pValue. If the expression is not an |
| 338 | ** integer, return 0 and leave *pValue unchanged. |
| 339 | */ |
| 340 | int sqliteExprIsInteger(Expr *p, int *pValue){ |
| 341 | switch( p->op ){ |
| 342 | case TK_INTEGER: { |
| 343 | *pValue = atoi(p->token.z); |
| 344 | return 1; |
| 345 | } |
| 346 | case TK_STRING: { |
drh | bd790ee | 2002-06-02 18:22:06 +0000 | [diff] [blame] | 347 | const char *z = p->token.z; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 348 | int n = p->token.n; |
drh | bd790ee | 2002-06-02 18:22:06 +0000 | [diff] [blame] | 349 | if( n>0 && z[0]=='-' ){ z++; n--; } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 350 | while( n>0 && *z && isdigit(*z) ){ z++; n--; } |
| 351 | if( n==0 ){ |
| 352 | *pValue = atoi(p->token.z); |
| 353 | return 1; |
| 354 | } |
| 355 | break; |
| 356 | } |
| 357 | case TK_UMINUS: { |
| 358 | int v; |
| 359 | if( sqliteExprIsInteger(p->pLeft, &v) ){ |
| 360 | *pValue = -v; |
| 361 | return 1; |
| 362 | } |
| 363 | break; |
| 364 | } |
| 365 | default: break; |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 371 | ** Return TRUE if the given string is a row-id column name. |
| 372 | */ |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 373 | int sqliteIsRowid(const char *z){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 374 | if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1; |
| 375 | if( sqliteStrICmp(z, "ROWID")==0 ) return 1; |
| 376 | if( sqliteStrICmp(z, "OID")==0 ) return 1; |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 381 | ** This routine walks an expression tree and resolves references to |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 382 | ** table columns. Nodes of the form ID.ID or ID resolve into an |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 383 | ** index to the table in the table list and a column offset. The |
| 384 | ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable |
| 385 | ** value is changed to the index of the referenced table in pTabList |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 386 | ** plus the "base" value. The base value will ultimately become the |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 387 | ** VDBE cursor number for a cursor that is pointing into the referenced |
| 388 | ** table. The Expr.iColumn value is changed to the index of the column |
| 389 | ** of the referenced table. The Expr.iColumn value for the special |
| 390 | ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an |
| 391 | ** alias for ROWID. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 392 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 393 | ** We also check for instances of the IN operator. IN comes in two |
| 394 | ** forms: |
| 395 | ** |
| 396 | ** expr IN (exprlist) |
| 397 | ** and |
| 398 | ** expr IN (SELECT ...) |
| 399 | ** |
| 400 | ** The first form is handled by creating a set holding the list |
| 401 | ** of allowed values. The second form causes the SELECT to generate |
| 402 | ** a temporary table. |
| 403 | ** |
| 404 | ** This routine also looks for scalar SELECTs that are part of an expression. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 405 | ** If it finds any, it generates code to write the value of that select |
| 406 | ** into a memory cell. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 407 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 408 | ** Unknown columns or tables provoke an error. The function returns |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 409 | ** the number of errors seen and leaves an error message on pParse->zErrMsg. |
| 410 | */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 411 | int sqliteExprResolveIds( |
| 412 | Parse *pParse, /* The parser context */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 413 | int base, /* VDBE cursor number for first entry in pTabList */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 414 | SrcList *pTabList, /* List of tables used to resolve column names */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 415 | ExprList *pEList, /* List of expressions used to resolve "AS" */ |
| 416 | Expr *pExpr /* The expression to be analyzed. */ |
| 417 | ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 418 | if( pExpr==0 || pTabList==0 ) return 0; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 419 | assert( base+pTabList->nSrc<=pParse->nTab ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 420 | switch( pExpr->op ){ |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 421 | /* Double-quoted strings (ex: "abc") are used as identifiers if |
| 422 | ** possible. Otherwise they remain as strings. Single-quoted |
| 423 | ** strings (ex: 'abc') are always string literals. |
| 424 | */ |
| 425 | case TK_STRING: { |
| 426 | if( pExpr->token.z[0]=='\'' ) break; |
| 427 | /* Fall thru into the TK_ID case if this is a double-quoted string */ |
| 428 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 429 | /* A lone identifier. Try and match it as follows: |
| 430 | ** |
| 431 | ** 1. To the name of a column of one of the tables in pTabList |
| 432 | ** |
| 433 | ** 2. To the right side of an AS keyword in the column list of |
| 434 | ** a SELECT statement. (For example, match against 'x' in |
| 435 | ** "SELECT a+b AS 'x' FROM t1".) |
| 436 | ** |
| 437 | ** 3. One of the special names "ROWID", "OID", or "_ROWID_". |
| 438 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 439 | case TK_ID: { |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 440 | int cnt = 0; /* Number of matches */ |
| 441 | int i; /* Loop counter */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 442 | char *z; |
| 443 | assert( pExpr->token.z ); |
| 444 | z = sqliteStrNDup(pExpr->token.z, pExpr->token.n); |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 445 | sqliteDequote(z); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 446 | if( z==0 ) return 1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 447 | for(i=0; i<pTabList->nSrc; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 448 | int j; |
| 449 | Table *pTab = pTabList->a[i].pTab; |
| 450 | if( pTab==0 ) continue; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 451 | assert( pTab->nCol>0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 452 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 453 | if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 454 | cnt++; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 455 | pExpr->iTable = i + base; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 456 | if( j==pTab->iPKey ){ |
| 457 | /* Substitute the record number for the INTEGER PRIMARY KEY */ |
| 458 | pExpr->iColumn = -1; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 459 | pExpr->dataType = SQLITE_SO_NUM; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 460 | }else{ |
| 461 | pExpr->iColumn = j; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 462 | pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 463 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 464 | pExpr->op = TK_COLUMN; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 468 | if( cnt==0 && pEList!=0 ){ |
| 469 | int j; |
| 470 | for(j=0; j<pEList->nExpr; j++){ |
| 471 | char *zAs = pEList->a[j].zName; |
| 472 | if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){ |
| 473 | cnt++; |
| 474 | assert( pExpr->pLeft==0 && pExpr->pRight==0 ); |
| 475 | pExpr->op = TK_AS; |
| 476 | pExpr->iColumn = j; |
drh | 75148a2 | 2002-03-03 03:42:31 +0000 | [diff] [blame] | 477 | pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 481 | if( cnt==0 && sqliteIsRowid(z) ){ |
| 482 | pExpr->iColumn = -1; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 483 | pExpr->iTable = base; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 484 | cnt = 1 + (pTabList->nSrc>1); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 485 | pExpr->op = TK_COLUMN; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 486 | pExpr->dataType = SQLITE_SO_NUM; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 487 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 488 | sqliteFree(z); |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 489 | if( cnt==0 && pExpr->token.z[0]!='"' ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 490 | sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 491 | pExpr->token.z, pExpr->token.n, 0); |
| 492 | pParse->nErr++; |
| 493 | return 1; |
| 494 | }else if( cnt>1 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 495 | sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 496 | pExpr->token.z, pExpr->token.n, 0); |
| 497 | pParse->nErr++; |
| 498 | return 1; |
| 499 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 500 | break; |
| 501 | } |
| 502 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 503 | /* A table name and column name: ID.ID */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 504 | case TK_DOT: { |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 505 | int cnt = 0; /* Number of matches */ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 506 | int cntTab = 0; /* Number of matching tables */ |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 507 | int i; /* Loop counter */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 508 | Expr *pLeft, *pRight; /* Left and right subbranches of the expr */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 509 | char *zLeft, *zRight; /* Text of an identifier */ |
| 510 | |
| 511 | pLeft = pExpr->pLeft; |
| 512 | pRight = pExpr->pRight; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 513 | assert( pLeft && pLeft->op==TK_ID && pLeft->token.z ); |
| 514 | assert( pRight && pRight->op==TK_ID && pRight->token.z ); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 515 | zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n); |
| 516 | zRight = sqliteStrNDup(pRight->token.z, pRight->token.n); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 517 | if( zLeft==0 || zRight==0 ){ |
| 518 | sqliteFree(zLeft); |
| 519 | sqliteFree(zRight); |
| 520 | return 1; |
| 521 | } |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 522 | sqliteDequote(zLeft); |
| 523 | sqliteDequote(zRight); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 524 | pExpr->iTable = -1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 525 | for(i=0; i<pTabList->nSrc; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 526 | int j; |
| 527 | char *zTab; |
| 528 | Table *pTab = pTabList->a[i].pTab; |
| 529 | if( pTab==0 ) continue; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 530 | assert( pTab->nCol>0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 531 | if( pTabList->a[i].zAlias ){ |
| 532 | zTab = pTabList->a[i].zAlias; |
| 533 | }else{ |
| 534 | zTab = pTab->zName; |
| 535 | } |
drh | 094b2bb | 2002-03-13 18:54:07 +0000 | [diff] [blame] | 536 | if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 537 | if( 0==(cntTab++) ) pExpr->iTable = i + base; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 538 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 539 | if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 540 | cnt++; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 541 | pExpr->iTable = i + base; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 542 | if( j==pTab->iPKey ){ |
| 543 | /* Substitute the record number for the INTEGER PRIMARY KEY */ |
| 544 | pExpr->iColumn = -1; |
| 545 | }else{ |
| 546 | pExpr->iColumn = j; |
| 547 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 548 | pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 552 | |
| 553 | /* If we have not already resolved this *.* expression, then maybe |
| 554 | * it is a new.* or old.* trigger argument reference */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 555 | if( cnt == 0 && pParse->trigStack != 0 ){ |
| 556 | TriggerStack *pTriggerStack = pParse->trigStack; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 557 | int t = 0; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 558 | if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){ |
| 559 | pExpr->iTable = pTriggerStack->newIdx; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 560 | cntTab++; |
| 561 | t = 1; |
| 562 | } |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 563 | if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){ |
| 564 | pExpr->iTable = pTriggerStack->oldIdx; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 565 | cntTab++; |
| 566 | t = 1; |
| 567 | } |
| 568 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 569 | if( t ){ |
| 570 | int j; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 571 | Table *pTab = pTriggerStack->pTab; |
| 572 | for(j=0; j < pTab->nCol; j++) { |
| 573 | if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 574 | cnt++; |
| 575 | pExpr->iColumn = j; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 576 | pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 579 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 580 | } |
| 581 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 582 | if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){ |
| 583 | cnt = 1; |
| 584 | pExpr->iColumn = -1; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 585 | pExpr->dataType = SQLITE_SO_NUM; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 586 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 587 | sqliteFree(zLeft); |
| 588 | sqliteFree(zRight); |
| 589 | if( cnt==0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 590 | sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 591 | pLeft->token.z, pLeft->token.n, ".", 1, |
| 592 | pRight->token.z, pRight->token.n, 0); |
| 593 | pParse->nErr++; |
| 594 | return 1; |
| 595 | }else if( cnt>1 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 596 | sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 597 | pLeft->token.z, pLeft->token.n, ".", 1, |
| 598 | pRight->token.z, pRight->token.n, 0); |
| 599 | pParse->nErr++; |
| 600 | return 1; |
| 601 | } |
| 602 | sqliteExprDelete(pLeft); |
| 603 | pExpr->pLeft = 0; |
| 604 | sqliteExprDelete(pRight); |
| 605 | pExpr->pRight = 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 606 | pExpr->op = TK_COLUMN; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 607 | break; |
| 608 | } |
| 609 | |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 610 | case TK_IN: { |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 611 | Vdbe *v = sqliteGetVdbe(pParse); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 612 | if( v==0 ) return 1; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 613 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){ |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 614 | return 1; |
| 615 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 616 | if( pExpr->pSelect ){ |
| 617 | /* Case 1: expr IN (SELECT ...) |
| 618 | ** |
| 619 | ** Generate code to write the results of the select into a temporary |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 620 | ** table. The cursor number of the temporary table has already |
| 621 | ** been put in iTable by sqliteExprResolveInSelect(). |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 622 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 623 | pExpr->iTable = pParse->nTab++; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 624 | sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 625 | sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 626 | }else if( pExpr->pList ){ |
| 627 | /* Case 2: expr IN (exprlist) |
| 628 | ** |
| 629 | ** Create a set to put the exprlist values in. The Set id is stored |
| 630 | ** in iTable. |
| 631 | */ |
| 632 | int i, iSet; |
| 633 | for(i=0; i<pExpr->pList->nExpr; i++){ |
| 634 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 635 | if( !sqliteExprIsConstant(pE2) ){ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 636 | sqliteSetString(&pParse->zErrMsg, |
| 637 | "right-hand side of IN operator must be constant", 0); |
| 638 | pParse->nErr++; |
| 639 | return 1; |
| 640 | } |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 641 | if( sqliteExprCheck(pParse, pE2, 0, 0) ){ |
| 642 | return 1; |
| 643 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 644 | } |
| 645 | iSet = pExpr->iTable = pParse->nSet++; |
| 646 | for(i=0; i<pExpr->pList->nExpr; i++){ |
| 647 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
| 648 | switch( pE2->op ){ |
| 649 | case TK_FLOAT: |
| 650 | case TK_INTEGER: |
| 651 | case TK_STRING: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 652 | int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 653 | assert( pE2->token.z ); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 654 | sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n); |
| 655 | sqliteVdbeDequoteP3(v, addr); |
| 656 | break; |
| 657 | } |
| 658 | default: { |
| 659 | sqliteExprCode(pParse, pE2); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 660 | sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 666 | break; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 667 | } |
| 668 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 669 | case TK_SELECT: { |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 670 | /* This has to be a scalar SELECT. Generate code to put the |
| 671 | ** value of this select in a memory cell and record the number |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 672 | ** of the memory cell in iColumn. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 673 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 674 | pExpr->iColumn = pParse->nMem++; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 675 | if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 676 | return 1; |
| 677 | } |
| 678 | break; |
| 679 | } |
| 680 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 681 | /* For all else, just recursively walk the tree */ |
| 682 | default: { |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 683 | if( pExpr->pLeft |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 684 | && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 685 | return 1; |
| 686 | } |
| 687 | if( pExpr->pRight |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 688 | && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 689 | return 1; |
| 690 | } |
| 691 | if( pExpr->pList ){ |
| 692 | int i; |
| 693 | ExprList *pList = pExpr->pList; |
| 694 | for(i=0; i<pList->nExpr; i++){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 695 | Expr *pArg = pList->a[i].pExpr; |
| 696 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 697 | return 1; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | return 0; |
| 704 | } |
| 705 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 706 | /* |
| 707 | ** Error check the functions in an expression. Make sure all |
| 708 | ** function names are recognized and all functions have the correct |
| 709 | ** number of arguments. Leave an error message in pParse->zErrMsg |
| 710 | ** if anything is amiss. Return the number of errors. |
| 711 | ** |
| 712 | ** if pIsAgg is not null and this expression is an aggregate function |
| 713 | ** (like count(*) or max(value)) then write a 1 into *pIsAgg. |
| 714 | */ |
| 715 | int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ |
| 716 | int nErr = 0; |
| 717 | if( pExpr==0 ) return 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 718 | switch( pExpr->op ){ |
| 719 | case TK_FUNCTION: { |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 720 | int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ |
| 721 | int no_such_func = 0; /* True if no such function exists */ |
| 722 | int is_type_of = 0; /* True if is the special TypeOf() function */ |
| 723 | int wrong_num_args = 0; /* True if wrong number of arguments */ |
| 724 | int is_agg = 0; /* True if is an aggregate function */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 725 | int i; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 726 | FuncDef *pDef; |
| 727 | |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 728 | pDef = sqliteFindFunction(pParse->db, |
| 729 | pExpr->token.z, pExpr->token.n, n, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 730 | if( pDef==0 ){ |
| 731 | pDef = sqliteFindFunction(pParse->db, |
| 732 | pExpr->token.z, pExpr->token.n, -1, 0); |
| 733 | if( pDef==0 ){ |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 734 | if( n==1 && pExpr->token.n==6 |
| 735 | && sqliteStrNICmp(pExpr->token.z, "typeof", 6)==0 ){ |
| 736 | is_type_of = 1; |
| 737 | }else { |
| 738 | no_such_func = 1; |
| 739 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 740 | }else{ |
| 741 | wrong_num_args = 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 742 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 743 | }else{ |
| 744 | is_agg = pDef->xFunc==0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 745 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 746 | if( is_agg && !allowAgg ){ |
| 747 | sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1, |
| 748 | pExpr->token.z, pExpr->token.n, "()", 2, 0); |
| 749 | pParse->nErr++; |
| 750 | nErr++; |
| 751 | is_agg = 0; |
| 752 | }else if( no_such_func ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 753 | sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, |
| 754 | pExpr->token.z, pExpr->token.n, 0); |
| 755 | pParse->nErr++; |
| 756 | nErr++; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 757 | }else if( wrong_num_args ){ |
| 758 | sqliteSetNString(&pParse->zErrMsg, |
| 759 | "wrong number of arguments to function ",-1, |
| 760 | pExpr->token.z, pExpr->token.n, "()", 2, 0); |
| 761 | pParse->nErr++; |
| 762 | nErr++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 763 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 764 | if( is_agg ) pExpr->op = TK_AGG_FUNCTION; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 765 | if( is_agg && pIsAgg ) *pIsAgg = 1; |
| 766 | for(i=0; nErr==0 && i<n; i++){ |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 767 | nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, |
| 768 | allowAgg && !is_agg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 769 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 770 | if( pDef==0 ){ |
| 771 | if( is_type_of ){ |
| 772 | pExpr->op = TK_STRING; |
| 773 | if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){ |
| 774 | pExpr->token.z = "numeric"; |
| 775 | pExpr->token.n = 7; |
| 776 | }else{ |
| 777 | pExpr->token.z = "text"; |
| 778 | pExpr->token.n = 4; |
| 779 | } |
| 780 | } |
| 781 | }else if( pDef->dataType>=0 ){ |
| 782 | if( pDef->dataType<n ){ |
| 783 | pExpr->dataType = |
| 784 | sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr); |
| 785 | }else{ |
| 786 | pExpr->dataType = SQLITE_SO_NUM; |
| 787 | } |
| 788 | }else if( pDef->dataType==SQLITE_ARGS ){ |
| 789 | pDef->dataType = SQLITE_SO_TEXT; |
| 790 | for(i=0; i<n; i++){ |
| 791 | if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){ |
| 792 | pExpr->dataType = SQLITE_SO_NUM; |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | }else if( pDef->dataType==SQLITE_NUMERIC ){ |
| 797 | pExpr->dataType = SQLITE_SO_NUM; |
| 798 | }else{ |
| 799 | pExpr->dataType = SQLITE_SO_TEXT; |
| 800 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 801 | } |
| 802 | default: { |
| 803 | if( pExpr->pLeft ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 804 | nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 805 | } |
| 806 | if( nErr==0 && pExpr->pRight ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 807 | nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 808 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 809 | if( nErr==0 && pExpr->pList ){ |
| 810 | int n = pExpr->pList->nExpr; |
| 811 | int i; |
| 812 | for(i=0; nErr==0 && i<n; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 813 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
| 814 | nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 817 | break; |
| 818 | } |
| 819 | } |
| 820 | return nErr; |
| 821 | } |
| 822 | |
| 823 | /* |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 824 | ** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the |
| 825 | ** given expression should sort as numeric values or as text. |
| 826 | ** |
| 827 | ** The sqliteExprResolveIds() and sqliteExprCheck() routines must have |
| 828 | ** both been called on the expression before it is passed to this routine. |
| 829 | */ |
| 830 | int sqliteExprType(Expr *p){ |
| 831 | if( p==0 ) return SQLITE_SO_NUM; |
| 832 | while( p ) switch( p->op ){ |
| 833 | case TK_PLUS: |
| 834 | case TK_MINUS: |
| 835 | case TK_STAR: |
| 836 | case TK_SLASH: |
| 837 | case TK_AND: |
| 838 | case TK_OR: |
| 839 | case TK_ISNULL: |
| 840 | case TK_NOTNULL: |
| 841 | case TK_NOT: |
| 842 | case TK_UMINUS: |
| 843 | case TK_BITAND: |
| 844 | case TK_BITOR: |
| 845 | case TK_BITNOT: |
| 846 | case TK_LSHIFT: |
| 847 | case TK_RSHIFT: |
| 848 | case TK_REM: |
| 849 | case TK_INTEGER: |
| 850 | case TK_FLOAT: |
| 851 | case TK_IN: |
| 852 | case TK_BETWEEN: |
| 853 | return SQLITE_SO_NUM; |
| 854 | |
| 855 | case TK_STRING: |
| 856 | case TK_NULL: |
| 857 | case TK_CONCAT: |
| 858 | return SQLITE_SO_TEXT; |
| 859 | |
| 860 | case TK_LT: |
| 861 | case TK_LE: |
| 862 | case TK_GT: |
| 863 | case TK_GE: |
| 864 | case TK_NE: |
| 865 | case TK_EQ: |
| 866 | if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){ |
| 867 | return SQLITE_SO_NUM; |
| 868 | } |
| 869 | p = p->pRight; |
| 870 | break; |
| 871 | |
| 872 | case TK_AS: |
| 873 | p = p->pLeft; |
| 874 | break; |
| 875 | |
| 876 | case TK_COLUMN: |
| 877 | case TK_FUNCTION: |
| 878 | case TK_AGG_FUNCTION: |
| 879 | return p->dataType; |
| 880 | |
| 881 | case TK_SELECT: |
| 882 | assert( p->pSelect ); |
| 883 | assert( p->pSelect->pEList ); |
| 884 | assert( p->pSelect->pEList->nExpr>0 ); |
| 885 | p = p->pSelect->pEList->a[0].pExpr; |
| 886 | break; |
| 887 | |
drh | b136320 | 2002-06-26 02:45:03 +0000 | [diff] [blame] | 888 | case TK_CASE: { |
| 889 | if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){ |
| 890 | return SQLITE_SO_NUM; |
| 891 | } |
| 892 | if( p->pList ){ |
| 893 | int i; |
| 894 | ExprList *pList = p->pList; |
| 895 | for(i=1; i<pList->nExpr; i+=2){ |
| 896 | if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){ |
| 897 | return SQLITE_SO_NUM; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | return SQLITE_SO_TEXT; |
| 902 | } |
| 903 | |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 904 | default: |
| 905 | assert( p->op==TK_ABORT ); /* Can't Happen */ |
| 906 | break; |
| 907 | } |
| 908 | return SQLITE_SO_NUM; |
| 909 | } |
| 910 | |
| 911 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 912 | ** Generate code into the current Vdbe to evaluate the given |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 913 | ** expression and leave the result on the top of stack. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 914 | */ |
| 915 | void sqliteExprCode(Parse *pParse, Expr *pExpr){ |
| 916 | Vdbe *v = pParse->pVdbe; |
| 917 | int op; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 918 | if( v==0 || pExpr==0 ) return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 919 | switch( pExpr->op ){ |
| 920 | case TK_PLUS: op = OP_Add; break; |
| 921 | case TK_MINUS: op = OP_Subtract; break; |
| 922 | case TK_STAR: op = OP_Multiply; break; |
| 923 | case TK_SLASH: op = OP_Divide; break; |
| 924 | case TK_AND: op = OP_And; break; |
| 925 | case TK_OR: op = OP_Or; break; |
| 926 | case TK_LT: op = OP_Lt; break; |
| 927 | case TK_LE: op = OP_Le; break; |
| 928 | case TK_GT: op = OP_Gt; break; |
| 929 | case TK_GE: op = OP_Ge; break; |
| 930 | case TK_NE: op = OP_Ne; break; |
| 931 | case TK_EQ: op = OP_Eq; break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 932 | case TK_ISNULL: op = OP_IsNull; break; |
| 933 | case TK_NOTNULL: op = OP_NotNull; break; |
| 934 | case TK_NOT: op = OP_Not; break; |
| 935 | case TK_UMINUS: op = OP_Negative; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 936 | case TK_BITAND: op = OP_BitAnd; break; |
| 937 | case TK_BITOR: op = OP_BitOr; break; |
| 938 | case TK_BITNOT: op = OP_BitNot; break; |
| 939 | case TK_LSHIFT: op = OP_ShiftLeft; break; |
| 940 | case TK_RSHIFT: op = OP_ShiftRight; break; |
| 941 | case TK_REM: op = OP_Remainder; break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 942 | default: break; |
| 943 | } |
| 944 | switch( pExpr->op ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 945 | case TK_COLUMN: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 946 | if( pParse->useAgg ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 947 | sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 948 | }else if( pExpr->iColumn>=0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 949 | sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 950 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 951 | sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 952 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 953 | break; |
| 954 | } |
| 955 | case TK_INTEGER: { |
drh | d9e3093 | 2002-06-09 01:16:01 +0000 | [diff] [blame] | 956 | int iVal = atoi(pExpr->token.z); |
| 957 | char zBuf[30]; |
| 958 | sprintf(zBuf,"%d",iVal); |
| 959 | if( strlen(zBuf)!=pExpr->token.n |
| 960 | || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){ |
| 961 | /* If the integer value cannot be represented exactly in 32 bits, |
| 962 | ** then code it as a string instead. */ |
| 963 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 964 | }else{ |
| 965 | sqliteVdbeAddOp(v, OP_Integer, iVal, 0); |
| 966 | } |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 967 | sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); |
| 968 | break; |
| 969 | } |
| 970 | case TK_FLOAT: { |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 971 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 972 | assert( pExpr->token.z ); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 973 | sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 974 | break; |
| 975 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 976 | case TK_STRING: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 977 | int addr = sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 978 | assert( pExpr->token.z ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 979 | sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n); |
| 980 | sqliteVdbeDequoteP3(v, addr); |
| 981 | break; |
| 982 | } |
| 983 | case TK_NULL: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 984 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 985 | break; |
| 986 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 987 | case TK_LT: |
| 988 | case TK_LE: |
| 989 | case TK_GT: |
| 990 | case TK_GE: |
| 991 | case TK_NE: |
| 992 | case TK_EQ: { |
| 993 | if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){ |
| 994 | op += 6; /* Convert numeric opcodes to text opcodes */ |
| 995 | } |
| 996 | /* Fall through into the next case */ |
| 997 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 998 | case TK_AND: |
| 999 | case TK_OR: |
| 1000 | case TK_PLUS: |
| 1001 | case TK_STAR: |
| 1002 | case TK_MINUS: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1003 | case TK_REM: |
| 1004 | case TK_BITAND: |
| 1005 | case TK_BITOR: |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1006 | case TK_SLASH: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1007 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1008 | sqliteExprCode(pParse, pExpr->pRight); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1009 | sqliteVdbeAddOp(v, op, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1010 | break; |
| 1011 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1012 | case TK_LSHIFT: |
| 1013 | case TK_RSHIFT: { |
| 1014 | sqliteExprCode(pParse, pExpr->pRight); |
| 1015 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1016 | sqliteVdbeAddOp(v, op, 0, 0); |
| 1017 | break; |
| 1018 | } |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 1019 | case TK_CONCAT: { |
| 1020 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1021 | sqliteExprCode(pParse, pExpr->pRight); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1022 | sqliteVdbeAddOp(v, OP_Concat, 2, 0); |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 1023 | break; |
| 1024 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1025 | case TK_UMINUS: { |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1026 | assert( pExpr->pLeft ); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 1027 | if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1028 | Token *p = &pExpr->pLeft->token; |
| 1029 | char *z = sqliteMalloc( p->n + 2 ); |
| 1030 | sprintf(z, "-%.*s", p->n, p->z); |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 1031 | if( pExpr->pLeft->op==TK_INTEGER ){ |
| 1032 | sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0); |
| 1033 | }else{ |
| 1034 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1035 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1036 | sqliteVdbeChangeP3(v, -1, z, p->n+1); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1037 | sqliteFree(z); |
| 1038 | break; |
| 1039 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1040 | /* Fall through into TK_NOT */ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1041 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1042 | case TK_BITNOT: |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1043 | case TK_NOT: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1044 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1045 | sqliteVdbeAddOp(v, op, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1046 | break; |
| 1047 | } |
| 1048 | case TK_ISNULL: |
| 1049 | case TK_NOTNULL: { |
| 1050 | int dest; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1051 | sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1052 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1053 | dest = sqliteVdbeCurrentAddr(v) + 2; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1054 | sqliteVdbeAddOp(v, op, 1, dest); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1055 | sqliteVdbeAddOp(v, OP_AddImm, -1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1058 | case TK_AGG_FUNCTION: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1059 | sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1060 | break; |
| 1061 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1062 | case TK_FUNCTION: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1063 | int i; |
| 1064 | ExprList *pList = pExpr->pList; |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 1065 | int nExpr = pList ? pList->nExpr : 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1066 | FuncDef *pDef; |
| 1067 | pDef = sqliteFindFunction(pParse->db, |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 1068 | pExpr->token.z, pExpr->token.n, nExpr, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1069 | assert( pDef!=0 ); |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 1070 | for(i=0; i<nExpr; i++){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1071 | sqliteExprCode(pParse, pList->a[i].pExpr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1072 | } |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 1073 | sqliteVdbeAddOp(v, OP_Function, nExpr, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1074 | sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1075 | break; |
| 1076 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1077 | case TK_SELECT: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1078 | sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1079 | break; |
| 1080 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1081 | case TK_IN: { |
| 1082 | int addr; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1083 | sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1084 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1085 | addr = sqliteVdbeCurrentAddr(v); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1086 | sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4); |
| 1087 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 1088 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1089 | sqliteVdbeAddOp(v, OP_Goto, 0, addr+6); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1090 | if( pExpr->pSelect ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1091 | sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1092 | }else{ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1093 | sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1094 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1095 | sqliteVdbeAddOp(v, OP_AddImm, -1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1096 | break; |
| 1097 | } |
| 1098 | case TK_BETWEEN: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1099 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1100 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 1101 | sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); |
| 1102 | sqliteVdbeAddOp(v, OP_Ge, 0, 0); |
| 1103 | sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
| 1104 | sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); |
| 1105 | sqliteVdbeAddOp(v, OP_Le, 0, 0); |
| 1106 | sqliteVdbeAddOp(v, OP_And, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1107 | break; |
| 1108 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 1109 | case TK_AS: { |
| 1110 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1111 | break; |
| 1112 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1113 | case TK_CASE: { |
| 1114 | int expr_end_label; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1115 | int jumpInst; |
| 1116 | int addr; |
| 1117 | int nExpr; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1118 | int i; |
| 1119 | |
| 1120 | assert(pExpr->pList); |
| 1121 | assert((pExpr->pList->nExpr % 2) == 0); |
| 1122 | assert(pExpr->pList->nExpr > 0); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1123 | nExpr = pExpr->pList->nExpr; |
| 1124 | expr_end_label = sqliteVdbeMakeLabel(v); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1125 | if( pExpr->pLeft ){ |
| 1126 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1127 | } |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1128 | for(i=0; i<nExpr; i=i+2){ |
| 1129 | sqliteExprCode(pParse, pExpr->pList->a[i].pExpr); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1130 | if( pExpr->pLeft ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1131 | sqliteVdbeAddOp(v, OP_Dup, 1, 1); |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 1132 | jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0); |
| 1133 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1134 | }else{ |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 1135 | jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1136 | } |
| 1137 | sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1138 | sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1139 | addr = sqliteVdbeCurrentAddr(v); |
| 1140 | sqliteVdbeChangeP2(v, jumpInst, addr); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1141 | } |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 1142 | if( pExpr->pLeft ){ |
| 1143 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 1144 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1145 | if( pExpr->pRight ){ |
| 1146 | sqliteExprCode(pParse, pExpr->pRight); |
| 1147 | }else{ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1148 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1149 | } |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1150 | sqliteVdbeResolveLabel(v, expr_end_label); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 1151 | break; |
| 1152 | } |
| 1153 | case TK_RAISE: { |
| 1154 | if( !pParse->trigStack ){ |
| 1155 | sqliteSetNString(&pParse->zErrMsg, |
| 1156 | "RAISE() may only be used within a trigger-program", -1, 0); |
| 1157 | pParse->nErr++; |
| 1158 | return; |
| 1159 | } |
| 1160 | if( pExpr->iColumn == OE_Rollback || |
| 1161 | pExpr->iColumn == OE_Abort || |
| 1162 | pExpr->iColumn == OE_Fail ){ |
| 1163 | char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n); |
| 1164 | sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn); |
| 1165 | sqliteDequote(msg); |
| 1166 | sqliteVdbeChangeP3(v, -1, msg, 0); |
| 1167 | sqliteFree(msg); |
| 1168 | } else { |
| 1169 | assert( pExpr->iColumn == OE_Ignore ); |
| 1170 | sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); |
| 1171 | sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", -1); |
| 1172 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1173 | } |
| 1174 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1175 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | ** Generate code for a boolean expression such that a jump is made |
| 1180 | ** to the label "dest" if the expression is true but execution |
| 1181 | ** continues straight thru if the expression is false. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1182 | ** |
| 1183 | ** If the expression evaluates to NULL (neither true nor false), then |
| 1184 | ** take the jump if the jumpIfNull flag is true. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1185 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1186 | void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1187 | Vdbe *v = pParse->pVdbe; |
| 1188 | int op = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1189 | if( v==0 || pExpr==0 ) return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1190 | switch( pExpr->op ){ |
| 1191 | case TK_LT: op = OP_Lt; break; |
| 1192 | case TK_LE: op = OP_Le; break; |
| 1193 | case TK_GT: op = OP_Gt; break; |
| 1194 | case TK_GE: op = OP_Ge; break; |
| 1195 | case TK_NE: op = OP_Ne; break; |
| 1196 | case TK_EQ: op = OP_Eq; break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1197 | case TK_ISNULL: op = OP_IsNull; break; |
| 1198 | case TK_NOTNULL: op = OP_NotNull; break; |
| 1199 | default: break; |
| 1200 | } |
| 1201 | switch( pExpr->op ){ |
| 1202 | case TK_AND: { |
| 1203 | int d2 = sqliteVdbeMakeLabel(v); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1204 | sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); |
| 1205 | sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1206 | sqliteVdbeResolveLabel(v, d2); |
| 1207 | break; |
| 1208 | } |
| 1209 | case TK_OR: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1210 | sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 1211 | sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | } |
| 1214 | case TK_NOT: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1215 | sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | } |
| 1218 | case TK_LT: |
| 1219 | case TK_LE: |
| 1220 | case TK_GT: |
| 1221 | case TK_GE: |
| 1222 | case TK_NE: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 1223 | case TK_EQ: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1224 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1225 | sqliteExprCode(pParse, pExpr->pRight); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1226 | if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){ |
| 1227 | op += 6; /* Convert numeric opcodes to text opcodes */ |
| 1228 | } |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1229 | sqliteVdbeAddOp(v, op, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1230 | break; |
| 1231 | } |
| 1232 | case TK_ISNULL: |
| 1233 | case TK_NOTNULL: { |
| 1234 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1235 | sqliteVdbeAddOp(v, op, 1, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1236 | break; |
| 1237 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1238 | case TK_IN: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1239 | int addr; |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 1240 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1241 | addr = sqliteVdbeCurrentAddr(v); |
| 1242 | sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3); |
| 1243 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 1244 | sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1245 | if( pExpr->pSelect ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1246 | sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1247 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1248 | sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1249 | } |
| 1250 | break; |
| 1251 | } |
| 1252 | case TK_BETWEEN: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1253 | int addr; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1254 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1255 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1256 | sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1257 | addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1258 | sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1259 | sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1260 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1261 | sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1262 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1263 | break; |
| 1264 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1265 | default: { |
| 1266 | sqliteExprCode(pParse, pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1267 | sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | /* |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 1274 | ** Generate code for a boolean expression such that a jump is made |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1275 | ** to the label "dest" if the expression is false but execution |
| 1276 | ** continues straight thru if the expression is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1277 | ** |
| 1278 | ** If the expression evaluates to NULL (neither true nor false) then |
| 1279 | ** jump if jumpIfNull is true or fall through if jumpIfNull is false. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1280 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1281 | void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1282 | Vdbe *v = pParse->pVdbe; |
| 1283 | int op = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1284 | if( v==0 || pExpr==0 ) return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1285 | switch( pExpr->op ){ |
| 1286 | case TK_LT: op = OP_Ge; break; |
| 1287 | case TK_LE: op = OP_Gt; break; |
| 1288 | case TK_GT: op = OP_Le; break; |
| 1289 | case TK_GE: op = OP_Lt; break; |
| 1290 | case TK_NE: op = OP_Eq; break; |
| 1291 | case TK_EQ: op = OP_Ne; break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1292 | case TK_ISNULL: op = OP_NotNull; break; |
| 1293 | case TK_NOTNULL: op = OP_IsNull; break; |
| 1294 | default: break; |
| 1295 | } |
| 1296 | switch( pExpr->op ){ |
| 1297 | case TK_AND: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1298 | sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 1299 | sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1300 | break; |
| 1301 | } |
| 1302 | case TK_OR: { |
| 1303 | int d2 = sqliteVdbeMakeLabel(v); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1304 | sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); |
| 1305 | sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1306 | sqliteVdbeResolveLabel(v, d2); |
| 1307 | break; |
| 1308 | } |
| 1309 | case TK_NOT: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1310 | sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1311 | break; |
| 1312 | } |
| 1313 | case TK_LT: |
| 1314 | case TK_LE: |
| 1315 | case TK_GT: |
| 1316 | case TK_GE: |
| 1317 | case TK_NE: |
| 1318 | case TK_EQ: { |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1319 | if( pParse->db->file_format>=3 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){ |
| 1320 | op += 6; /* Convert numeric opcodes to text opcodes */ |
| 1321 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1322 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1323 | sqliteExprCode(pParse, pExpr->pRight); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1324 | sqliteVdbeAddOp(v, op, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1325 | break; |
| 1326 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1327 | case TK_ISNULL: |
| 1328 | case TK_NOTNULL: { |
| 1329 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1330 | sqliteVdbeAddOp(v, op, 1, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1331 | break; |
| 1332 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1333 | case TK_IN: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1334 | int addr; |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 1335 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1336 | addr = sqliteVdbeCurrentAddr(v); |
| 1337 | sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3); |
| 1338 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 1339 | sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1340 | if( pExpr->pSelect ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1341 | sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1342 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1343 | sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1344 | } |
| 1345 | break; |
| 1346 | } |
| 1347 | case TK_BETWEEN: { |
| 1348 | int addr; |
| 1349 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1350 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1351 | sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); |
| 1352 | addr = sqliteVdbeCurrentAddr(v); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1353 | sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1354 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 1355 | sqliteVdbeAddOp(v, OP_Goto, 0, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1356 | sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1357 | sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1358 | break; |
| 1359 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1360 | default: { |
| 1361 | sqliteExprCode(pParse, pExpr); |
drh | 461c281 | 2002-05-30 02:35:11 +0000 | [diff] [blame] | 1362 | sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1363 | break; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1367 | |
| 1368 | /* |
| 1369 | ** Do a deep comparison of two expression trees. Return TRUE (non-zero) |
| 1370 | ** if they are identical and return FALSE if they differ in any way. |
| 1371 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1372 | int sqliteExprCompare(Expr *pA, Expr *pB){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1373 | int i; |
| 1374 | if( pA==0 ){ |
| 1375 | return pB==0; |
| 1376 | }else if( pB==0 ){ |
| 1377 | return 0; |
| 1378 | } |
| 1379 | if( pA->op!=pB->op ) return 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1380 | if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0; |
| 1381 | if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1382 | if( pA->pList ){ |
| 1383 | if( pB->pList==0 ) return 0; |
| 1384 | if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; |
| 1385 | for(i=0; i<pA->pList->nExpr; i++){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1386 | if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1387 | return 0; |
| 1388 | } |
| 1389 | } |
| 1390 | }else if( pB->pList ){ |
| 1391 | return 0; |
| 1392 | } |
| 1393 | if( pA->pSelect || pB->pSelect ) return 0; |
| 1394 | if( pA->token.z ){ |
| 1395 | if( pB->token.z==0 ) return 0; |
| 1396 | if( pB->token.n!=pA->token.n ) return 0; |
| 1397 | if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0; |
| 1398 | } |
| 1399 | return 1; |
| 1400 | } |
| 1401 | |
| 1402 | /* |
| 1403 | ** Add a new element to the pParse->aAgg[] array and return its index. |
| 1404 | */ |
| 1405 | static int appendAggInfo(Parse *pParse){ |
| 1406 | if( (pParse->nAgg & 0x7)==0 ){ |
| 1407 | int amt = pParse->nAgg + 8; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1408 | AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); |
| 1409 | if( aAgg==0 ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1410 | return -1; |
| 1411 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1412 | pParse->aAgg = aAgg; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1413 | } |
| 1414 | memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); |
| 1415 | return pParse->nAgg++; |
| 1416 | } |
| 1417 | |
| 1418 | /* |
| 1419 | ** Analyze the given expression looking for aggregate functions and |
| 1420 | ** for variables that need to be added to the pParse->aAgg[] array. |
| 1421 | ** Make additional entries to the pParse->aAgg[] array as necessary. |
| 1422 | ** |
| 1423 | ** This routine should only be called after the expression has been |
| 1424 | ** analyzed by sqliteExprResolveIds() and sqliteExprCheck(). |
| 1425 | ** |
| 1426 | ** If errors are seen, leave an error message in zErrMsg and return |
| 1427 | ** the number of errors. |
| 1428 | */ |
| 1429 | int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ |
| 1430 | int i; |
| 1431 | AggExpr *aAgg; |
| 1432 | int nErr = 0; |
| 1433 | |
| 1434 | if( pExpr==0 ) return 0; |
| 1435 | switch( pExpr->op ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1436 | case TK_COLUMN: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1437 | aAgg = pParse->aAgg; |
| 1438 | for(i=0; i<pParse->nAgg; i++){ |
| 1439 | if( aAgg[i].isAgg ) continue; |
| 1440 | if( aAgg[i].pExpr->iTable==pExpr->iTable |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1441 | && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1442 | break; |
| 1443 | } |
| 1444 | } |
| 1445 | if( i>=pParse->nAgg ){ |
| 1446 | i = appendAggInfo(pParse); |
| 1447 | if( i<0 ) return 1; |
| 1448 | pParse->aAgg[i].isAgg = 0; |
| 1449 | pParse->aAgg[i].pExpr = pExpr; |
| 1450 | } |
drh | aaf8872 | 2000-06-08 11:25:00 +0000 | [diff] [blame] | 1451 | pExpr->iAgg = i; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1452 | break; |
| 1453 | } |
| 1454 | case TK_AGG_FUNCTION: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1455 | aAgg = pParse->aAgg; |
| 1456 | for(i=0; i<pParse->nAgg; i++){ |
| 1457 | if( !aAgg[i].isAgg ) continue; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1458 | if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1459 | break; |
| 1460 | } |
| 1461 | } |
| 1462 | if( i>=pParse->nAgg ){ |
| 1463 | i = appendAggInfo(pParse); |
| 1464 | if( i<0 ) return 1; |
| 1465 | pParse->aAgg[i].isAgg = 1; |
| 1466 | pParse->aAgg[i].pExpr = pExpr; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1467 | pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db, |
drh | f55f25f | 2002-02-28 01:46:11 +0000 | [diff] [blame] | 1468 | pExpr->token.z, pExpr->token.n, |
| 1469 | pExpr->pList ? pExpr->pList->nExpr : 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1470 | } |
| 1471 | pExpr->iAgg = i; |
| 1472 | break; |
| 1473 | } |
| 1474 | default: { |
| 1475 | if( pExpr->pLeft ){ |
| 1476 | nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft); |
| 1477 | } |
| 1478 | if( nErr==0 && pExpr->pRight ){ |
| 1479 | nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight); |
| 1480 | } |
| 1481 | if( nErr==0 && pExpr->pList ){ |
| 1482 | int n = pExpr->pList->nExpr; |
| 1483 | int i; |
| 1484 | for(i=0; nErr==0 && i<n; i++){ |
| 1485 | nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); |
| 1486 | } |
| 1487 | } |
| 1488 | break; |
| 1489 | } |
| 1490 | } |
| 1491 | return nErr; |
| 1492 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1493 | |
| 1494 | /* |
| 1495 | ** Locate a user function given a name and a number of arguments. |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1496 | ** Return a pointer to the FuncDef structure that defines that |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1497 | ** function, or return NULL if the function does not exist. |
| 1498 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1499 | ** If the createFlag argument is true, then a new (blank) FuncDef |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1500 | ** structure is created and liked into the "db" structure if a |
| 1501 | ** no matching function previously existed. When createFlag is true |
| 1502 | ** and the nArg parameter is -1, then only a function that accepts |
| 1503 | ** any number of arguments will be returned. |
| 1504 | ** |
| 1505 | ** If createFlag is false and nArg is -1, then the first valid |
| 1506 | ** function found is returned. A function is valid if either xFunc |
| 1507 | ** or xStep is non-zero. |
| 1508 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1509 | FuncDef *sqliteFindFunction( |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1510 | sqlite *db, /* An open database */ |
| 1511 | const char *zName, /* Name of the function. Not null-terminated */ |
| 1512 | int nName, /* Number of characters in the name */ |
| 1513 | int nArg, /* Number of arguments. -1 means any number */ |
| 1514 | int createFlag /* Create new entry if true and does not otherwise exist */ |
| 1515 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1516 | FuncDef *pFirst, *p, *pMaybe; |
| 1517 | pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1518 | if( p && !createFlag && nArg<0 ){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1519 | while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } |
| 1520 | return p; |
| 1521 | } |
| 1522 | pMaybe = 0; |
| 1523 | while( p && p->nArg!=nArg ){ |
| 1524 | if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; |
| 1525 | p = p->pNext; |
| 1526 | } |
| 1527 | if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ |
| 1528 | return 0; |
| 1529 | } |
| 1530 | if( p==0 && pMaybe ){ |
| 1531 | assert( createFlag==0 ); |
| 1532 | return pMaybe; |
| 1533 | } |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 1534 | if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1535 | p->nArg = nArg; |
| 1536 | p->pNext = pFirst; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1537 | p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1538 | sqliteHashInsert(&db->aFunc, zName, nName, (void*)p); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1539 | } |
| 1540 | return p; |
| 1541 | } |