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