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