1cce7d176Sdrh /* 2b19a2bc6Sdrh ** 2001 September 15 3cce7d176Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 6cce7d176Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 10cce7d176Sdrh ** 11cce7d176Sdrh ************************************************************************* 121ccde15dSdrh ** This file contains routines used for analyzing expressions and 13b19a2bc6Sdrh ** for generating VDBE code that evaluates expressions in SQLite. 14cce7d176Sdrh ** 15*f2bc013cSdrh ** $Id: expr.c,v 1.166 2004/10/04 13:19:24 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 1804738cb9Sdrh #include <ctype.h> 19a2e00042Sdrh 20e014a838Sdanielk1977 /* 21e014a838Sdanielk1977 ** Return the 'affinity' of the expression pExpr if any. 22e014a838Sdanielk1977 ** 23e014a838Sdanielk1977 ** If pExpr is a column, a reference to a column via an 'AS' alias, 24e014a838Sdanielk1977 ** or a sub-select with a column as the return value, then the 25e014a838Sdanielk1977 ** affinity of that column is returned. Otherwise, 0x00 is returned, 26e014a838Sdanielk1977 ** indicating no affinity for the expression. 27e014a838Sdanielk1977 ** 28e014a838Sdanielk1977 ** i.e. the WHERE clause expresssions in the following statements all 29e014a838Sdanielk1977 ** have an affinity: 30e014a838Sdanielk1977 ** 31e014a838Sdanielk1977 ** CREATE TABLE t1(a); 32e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE a; 33e014a838Sdanielk1977 ** SELECT a AS b FROM t1 WHERE b; 34e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE (select a from t1); 35e014a838Sdanielk1977 */ 36bf3b721fSdanielk1977 char sqlite3ExprAffinity(Expr *pExpr){ 37a37cdde0Sdanielk1977 if( pExpr->op==TK_AS ){ 38bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pLeft); 39a37cdde0Sdanielk1977 } 40a37cdde0Sdanielk1977 if( pExpr->op==TK_SELECT ){ 41bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 42a37cdde0Sdanielk1977 } 43a37cdde0Sdanielk1977 return pExpr->affinity; 44a37cdde0Sdanielk1977 } 45a37cdde0Sdanielk1977 4653db1458Sdrh /* 470202b29eSdanielk1977 ** Return the default collation sequence for the expression pExpr. If 480202b29eSdanielk1977 ** there is no default collation type, return 0. 490202b29eSdanielk1977 */ 507cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 517cedc8d4Sdanielk1977 CollSeq *pColl = 0; 520202b29eSdanielk1977 if( pExpr ){ 537cedc8d4Sdanielk1977 pColl = pExpr->pColl; 547cedc8d4Sdanielk1977 if( pExpr->op==TK_AS && !pColl ){ 557cedc8d4Sdanielk1977 return sqlite3ExprCollSeq(pParse, pExpr->pLeft); 560202b29eSdanielk1977 } 570202b29eSdanielk1977 } 587cedc8d4Sdanielk1977 if( sqlite3CheckCollSeq(pParse, pColl) ){ 597cedc8d4Sdanielk1977 pColl = 0; 607cedc8d4Sdanielk1977 } 617cedc8d4Sdanielk1977 return pColl; 620202b29eSdanielk1977 } 630202b29eSdanielk1977 640202b29eSdanielk1977 /* 6553db1458Sdrh ** pExpr is the left operand of a comparison operator. aff2 is the 6653db1458Sdrh ** type affinity of the right operand. This routine returns the 6753db1458Sdrh ** type affinity that should be used for the comparison operator. 6853db1458Sdrh */ 69e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 70bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 71e014a838Sdanielk1977 if( aff1 && aff2 ){ 72e014a838Sdanielk1977 /* Both sides of the comparison are columns. If one has numeric or 73e014a838Sdanielk1977 ** integer affinity, use that. Otherwise use no affinity. 74e014a838Sdanielk1977 */ 75e014a838Sdanielk1977 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){ 76e014a838Sdanielk1977 return SQLITE_AFF_INTEGER; 77e014a838Sdanielk1977 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){ 78e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 79e014a838Sdanielk1977 }else{ 80e014a838Sdanielk1977 return SQLITE_AFF_NONE; 81e014a838Sdanielk1977 } 82e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 835f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 845f6a87b3Sdrh ** results directly. 85e014a838Sdanielk1977 */ 865f6a87b3Sdrh /* return SQLITE_AFF_NUMERIC; // Ticket #805 */ 875f6a87b3Sdrh return SQLITE_AFF_NONE; 88e014a838Sdanielk1977 }else{ 89e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 90e014a838Sdanielk1977 return (aff1 + aff2); 91e014a838Sdanielk1977 } 92e014a838Sdanielk1977 } 93e014a838Sdanielk1977 9453db1458Sdrh /* 9553db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 9653db1458Sdrh ** be applied to both operands prior to doing the comparison. 9753db1458Sdrh */ 98e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 99e014a838Sdanielk1977 char aff; 100e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 101e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 102e014a838Sdanielk1977 pExpr->op==TK_NE ); 103e014a838Sdanielk1977 assert( pExpr->pLeft ); 104bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 105e014a838Sdanielk1977 if( pExpr->pRight ){ 106e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 107e014a838Sdanielk1977 } 108e014a838Sdanielk1977 else if( pExpr->pSelect ){ 109e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 110e014a838Sdanielk1977 } 111e014a838Sdanielk1977 else if( !aff ){ 112e014a838Sdanielk1977 aff = SQLITE_AFF_NUMERIC; 113e014a838Sdanielk1977 } 114e014a838Sdanielk1977 return aff; 115e014a838Sdanielk1977 } 116e014a838Sdanielk1977 117e014a838Sdanielk1977 /* 118e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 119e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 120e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 121e014a838Sdanielk1977 ** the comparison in pExpr. 122e014a838Sdanielk1977 */ 123e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 124e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 125e014a838Sdanielk1977 return 126e014a838Sdanielk1977 (aff==SQLITE_AFF_NONE) || 127e014a838Sdanielk1977 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) || 128e014a838Sdanielk1977 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) || 129e014a838Sdanielk1977 (aff==idx_affinity); 130e014a838Sdanielk1977 } 131e014a838Sdanielk1977 132a37cdde0Sdanielk1977 /* 133a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison 134a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 135a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned 136a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression 137a37cdde0Sdanielk1977 ** evaluates to NULL. 138a37cdde0Sdanielk1977 */ 139e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 140bf3b721fSdanielk1977 char aff = sqlite3ExprAffinity(pExpr2); 141e014a838Sdanielk1977 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0); 142a37cdde0Sdanielk1977 } 143a37cdde0Sdanielk1977 144a2e00042Sdrh /* 1450202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by 1460202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight. 1470202b29eSdanielk1977 ** 1480202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is 1490202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression 1500202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating 1510202b29eSdanielk1977 ** type. 1520202b29eSdanielk1977 */ 1537cedc8d4Sdanielk1977 static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){ 1547cedc8d4Sdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft); 1550202b29eSdanielk1977 if( !pColl ){ 1567cedc8d4Sdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pRight); 1570202b29eSdanielk1977 } 1580202b29eSdanielk1977 return pColl; 1590202b29eSdanielk1977 } 1600202b29eSdanielk1977 1610202b29eSdanielk1977 /* 162be5c89acSdrh ** Generate code for a comparison operator. 163be5c89acSdrh */ 164be5c89acSdrh static int codeCompare( 165be5c89acSdrh Parse *pParse, /* The parsing (and code generating) context */ 166be5c89acSdrh Expr *pLeft, /* The left operand */ 167be5c89acSdrh Expr *pRight, /* The right operand */ 168be5c89acSdrh int opcode, /* The comparison opcode */ 169be5c89acSdrh int dest, /* Jump here if true. */ 170be5c89acSdrh int jumpIfNull /* If true, jump if either operand is NULL */ 171be5c89acSdrh ){ 172be5c89acSdrh int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); 173be5c89acSdrh CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight); 174be5c89acSdrh return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); 175be5c89acSdrh } 176be5c89acSdrh 177be5c89acSdrh /* 178a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 179a76b5dfcSdrh ** for this node is obtained from sqliteMalloc(). The calling function 180a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 181a76b5dfcSdrh */ 1824adee20fSdanielk1977 Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ 183a76b5dfcSdrh Expr *pNew; 184a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 185a76b5dfcSdrh if( pNew==0 ){ 1864efc4754Sdrh /* When malloc fails, we leak memory from pLeft and pRight */ 187a76b5dfcSdrh return 0; 188a76b5dfcSdrh } 189a76b5dfcSdrh pNew->op = op; 190a76b5dfcSdrh pNew->pLeft = pLeft; 191a76b5dfcSdrh pNew->pRight = pRight; 192a76b5dfcSdrh if( pToken ){ 1934b59ab5eSdrh assert( pToken->dyn==0 ); 194145716b3Sdrh pNew->span = pNew->token = *pToken; 195145716b3Sdrh }else if( pLeft && pRight ){ 1964adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 197a76b5dfcSdrh } 198a76b5dfcSdrh return pNew; 199a76b5dfcSdrh } 200a76b5dfcSdrh 201a76b5dfcSdrh /* 20291bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 20391bb0eedSdrh ** NULL, then just return the other expression. 20491bb0eedSdrh */ 20591bb0eedSdrh Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){ 20691bb0eedSdrh if( pLeft==0 ){ 20791bb0eedSdrh return pRight; 20891bb0eedSdrh }else if( pRight==0 ){ 20991bb0eedSdrh return pLeft; 21091bb0eedSdrh }else{ 21191bb0eedSdrh return sqlite3Expr(TK_AND, pLeft, pRight, 0); 21291bb0eedSdrh } 21391bb0eedSdrh } 21491bb0eedSdrh 21591bb0eedSdrh /* 2166977fea8Sdrh ** Set the Expr.span field of the given expression to span all 217a76b5dfcSdrh ** text between the two given tokens. 218a76b5dfcSdrh */ 2194adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 2204efc4754Sdrh assert( pRight!=0 ); 2214efc4754Sdrh assert( pLeft!=0 ); 22271c697efSdrh if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){ 223ad6d9460Sdrh assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); 224145716b3Sdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 2256977fea8Sdrh pExpr->span.z = pLeft->z; 2266977fea8Sdrh pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); 2274b59ab5eSdrh }else{ 2286977fea8Sdrh pExpr->span.z = 0; 2294b59ab5eSdrh } 230a76b5dfcSdrh } 231a76b5dfcSdrh } 232a76b5dfcSdrh 233a76b5dfcSdrh /* 234a76b5dfcSdrh ** Construct a new expression node for a function with multiple 235a76b5dfcSdrh ** arguments. 236a76b5dfcSdrh */ 2374adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ 238a76b5dfcSdrh Expr *pNew; 239a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 240a76b5dfcSdrh if( pNew==0 ){ 2414adee20fSdanielk1977 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */ 242a76b5dfcSdrh return 0; 243a76b5dfcSdrh } 244a76b5dfcSdrh pNew->op = TK_FUNCTION; 245a76b5dfcSdrh pNew->pList = pList; 246a76b5dfcSdrh if( pToken ){ 2474b59ab5eSdrh assert( pToken->dyn==0 ); 248a76b5dfcSdrh pNew->token = *pToken; 249a76b5dfcSdrh }else{ 250a76b5dfcSdrh pNew->token.z = 0; 251a76b5dfcSdrh } 2526977fea8Sdrh pNew->span = pNew->token; 253a76b5dfcSdrh return pNew; 254a76b5dfcSdrh } 255a76b5dfcSdrh 256a76b5dfcSdrh /* 257fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 258fa6bc000Sdrh ** in the original SQL statement. 259fa6bc000Sdrh ** 260fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 261fa6bc000Sdrh ** variable number. 262fa6bc000Sdrh ** 263fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 264fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 265fa6bc000Sdrh ** the SQL statement comes from an external source. 266fa6bc000Sdrh ** 267fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number 268fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 269fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is 270fa6bc000Sdrh ** assigned. 271fa6bc000Sdrh */ 272fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 273fa6bc000Sdrh Token *pToken; 274fa6bc000Sdrh if( pExpr==0 ) return; 275fa6bc000Sdrh pToken = &pExpr->token; 276fa6bc000Sdrh assert( pToken->n>=1 ); 277fa6bc000Sdrh assert( pToken->z!=0 ); 278fa6bc000Sdrh assert( pToken->z[0]!=0 ); 279fa6bc000Sdrh if( pToken->n==1 ){ 280fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 281fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 282fa6bc000Sdrh }else if( pToken->z[0]=='?' ){ 283fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 284fa6bc000Sdrh ** use it as the variable number */ 285fa6bc000Sdrh int i; 286fa6bc000Sdrh pExpr->iTable = i = atoi(&pToken->z[1]); 287fa6bc000Sdrh if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){ 288fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 289fa6bc000Sdrh SQLITE_MAX_VARIABLE_NUMBER); 290fa6bc000Sdrh } 291fa6bc000Sdrh if( i>pParse->nVar ){ 292fa6bc000Sdrh pParse->nVar = i; 293fa6bc000Sdrh } 294fa6bc000Sdrh }else{ 295fa6bc000Sdrh /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable 296fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 297fa6bc000Sdrh ** has never appeared before, reuse the same variable number 298fa6bc000Sdrh */ 299fa6bc000Sdrh int i, n; 300fa6bc000Sdrh n = pToken->n; 301fa6bc000Sdrh for(i=0; i<pParse->nVarExpr; i++){ 302fa6bc000Sdrh Expr *pE; 303fa6bc000Sdrh if( (pE = pParse->apVarExpr[i])!=0 304fa6bc000Sdrh && pE->token.n==n 305fa6bc000Sdrh && memcmp(pE->token.z, pToken->z, n)==0 ){ 306fa6bc000Sdrh pExpr->iTable = pE->iTable; 307fa6bc000Sdrh break; 308fa6bc000Sdrh } 309fa6bc000Sdrh } 310fa6bc000Sdrh if( i>=pParse->nVarExpr ){ 311fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 312fa6bc000Sdrh if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ 313fa6bc000Sdrh pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; 314fa6bc000Sdrh pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr, 315fa6bc000Sdrh pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ); 316fa6bc000Sdrh } 317fa6bc000Sdrh if( !sqlite3_malloc_failed ){ 318fa6bc000Sdrh assert( pParse->apVarExpr!=0 ); 319fa6bc000Sdrh pParse->apVarExpr[pParse->nVarExpr++] = pExpr; 320fa6bc000Sdrh } 321fa6bc000Sdrh } 322fa6bc000Sdrh } 323fa6bc000Sdrh } 324fa6bc000Sdrh 325fa6bc000Sdrh /* 326a2e00042Sdrh ** Recursively delete an expression tree. 327a2e00042Sdrh */ 3284adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 329a2e00042Sdrh if( p==0 ) return; 3304efc4754Sdrh if( p->span.dyn ) sqliteFree((char*)p->span.z); 3314efc4754Sdrh if( p->token.dyn ) sqliteFree((char*)p->token.z); 3324adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 3334adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 3344adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 3354adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 336a2e00042Sdrh sqliteFree(p); 337a2e00042Sdrh } 338a2e00042Sdrh 339a76b5dfcSdrh 340a76b5dfcSdrh /* 341ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 342ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 343ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 344ff78bd2fSdrh ** without effecting the originals. 345ff78bd2fSdrh ** 3464adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 3474adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 348ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 349ff78bd2fSdrh ** 350ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 351ff78bd2fSdrh */ 3524adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){ 353ff78bd2fSdrh Expr *pNew; 354ff78bd2fSdrh if( p==0 ) return 0; 355fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 356ff78bd2fSdrh if( pNew==0 ) return 0; 3573b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 3586977fea8Sdrh if( p->token.z!=0 ){ 3594b59ab5eSdrh pNew->token.z = sqliteStrDup(p->token.z); 3604b59ab5eSdrh pNew->token.dyn = 1; 3614b59ab5eSdrh }else{ 3624efc4754Sdrh assert( pNew->token.z==0 ); 3634b59ab5eSdrh } 3646977fea8Sdrh pNew->span.z = 0; 3654adee20fSdanielk1977 pNew->pLeft = sqlite3ExprDup(p->pLeft); 3664adee20fSdanielk1977 pNew->pRight = sqlite3ExprDup(p->pRight); 3674adee20fSdanielk1977 pNew->pList = sqlite3ExprListDup(p->pList); 3684adee20fSdanielk1977 pNew->pSelect = sqlite3SelectDup(p->pSelect); 369ff78bd2fSdrh return pNew; 370ff78bd2fSdrh } 3714adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 3724b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 3734b59ab5eSdrh if( pFrom->z ){ 3744b59ab5eSdrh pTo->n = pFrom->n; 3754b59ab5eSdrh pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 3764b59ab5eSdrh pTo->dyn = 1; 3774b59ab5eSdrh }else{ 3784b59ab5eSdrh pTo->z = 0; 3794b59ab5eSdrh } 3804b59ab5eSdrh } 3814adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){ 382ff78bd2fSdrh ExprList *pNew; 383145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 384ff78bd2fSdrh int i; 385ff78bd2fSdrh if( p==0 ) return 0; 386ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 387ff78bd2fSdrh if( pNew==0 ) return 0; 3884305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 3893e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 390e0048400Sdanielk1977 if( pItem==0 ){ 391e0048400Sdanielk1977 sqliteFree(pNew); 392e0048400Sdanielk1977 return 0; 393e0048400Sdanielk1977 } 394145716b3Sdrh pOldItem = p->a; 395145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 3964b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 397145716b3Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr); 3986977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 3996977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 4004b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 4014b59ab5eSdrh ** the names of columns in the result set needs this information */ 4024adee20fSdanielk1977 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 4034b59ab5eSdrh } 4041f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 40524b03fd0Sdanielk1977 || pOldExpr->span.z==0 || sqlite3_malloc_failed ); 406145716b3Sdrh pItem->zName = sqliteStrDup(pOldItem->zName); 407145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 408145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 4093e7bc9caSdrh pItem->done = 0; 410ff78bd2fSdrh } 411ff78bd2fSdrh return pNew; 412ff78bd2fSdrh } 4134adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){ 414ad3cab52Sdrh SrcList *pNew; 415ad3cab52Sdrh int i; 416113088ecSdrh int nByte; 417ad3cab52Sdrh if( p==0 ) return 0; 418113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 4194efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 420ad3cab52Sdrh if( pNew==0 ) return 0; 4214305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 422ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 4234efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 4244efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 4254efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 4264efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 4274efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 4284efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 4294efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 4304efc4754Sdrh pNewItem->pTab = 0; 4314adee20fSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 4324adee20fSdanielk1977 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 4334adee20fSdanielk1977 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 434ad3cab52Sdrh } 435ad3cab52Sdrh return pNew; 436ad3cab52Sdrh } 4374adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){ 438ff78bd2fSdrh IdList *pNew; 439ff78bd2fSdrh int i; 440ff78bd2fSdrh if( p==0 ) return 0; 4414efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 442ff78bd2fSdrh if( pNew==0 ) return 0; 4434305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 4444efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 445e4697f5eSdrh if( pNew->a==0 ) return 0; 446ff78bd2fSdrh for(i=0; i<p->nId; i++){ 4474efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 4484efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 4494efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 4504efc4754Sdrh pNewItem->idx = pOldItem->idx; 451ff78bd2fSdrh } 452ff78bd2fSdrh return pNew; 453ff78bd2fSdrh } 4544adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){ 455ff78bd2fSdrh Select *pNew; 456ff78bd2fSdrh if( p==0 ) return 0; 4574efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 458ff78bd2fSdrh if( pNew==0 ) return 0; 459ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 4604adee20fSdanielk1977 pNew->pEList = sqlite3ExprListDup(p->pEList); 4614adee20fSdanielk1977 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 4624adee20fSdanielk1977 pNew->pWhere = sqlite3ExprDup(p->pWhere); 4634adee20fSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 4644adee20fSdanielk1977 pNew->pHaving = sqlite3ExprDup(p->pHaving); 4654adee20fSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 466ff78bd2fSdrh pNew->op = p->op; 4674adee20fSdanielk1977 pNew->pPrior = sqlite3SelectDup(p->pPrior); 468ff78bd2fSdrh pNew->nLimit = p->nLimit; 469ff78bd2fSdrh pNew->nOffset = p->nOffset; 470ff78bd2fSdrh pNew->zSelect = 0; 4717b58daeaSdrh pNew->iLimit = -1; 4727b58daeaSdrh pNew->iOffset = -1; 473dc1bdc4fSdanielk1977 pNew->ppOpenTemp = 0; 474ff78bd2fSdrh return pNew; 475ff78bd2fSdrh } 476ff78bd2fSdrh 477ff78bd2fSdrh 478ff78bd2fSdrh /* 479a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 480a76b5dfcSdrh ** initially NULL, then create a new expression list. 481a76b5dfcSdrh */ 4824adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 483a76b5dfcSdrh if( pList==0 ){ 484a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 485a76b5dfcSdrh if( pList==0 ){ 4864adee20fSdanielk1977 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 487a76b5dfcSdrh return 0; 488a76b5dfcSdrh } 4894efc4754Sdrh assert( pList->nAlloc==0 ); 490a76b5dfcSdrh } 4914305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 4924305d103Sdrh pList->nAlloc = pList->nAlloc*2 + 4; 4934efc4754Sdrh pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); 4944efc4754Sdrh if( pList->a==0 ){ 4954adee20fSdanielk1977 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 4964efc4754Sdrh pList->nExpr = pList->nAlloc = 0; 497a76b5dfcSdrh return pList; 498a76b5dfcSdrh } 499a76b5dfcSdrh } 5004efc4754Sdrh assert( pList->a!=0 ); 5014efc4754Sdrh if( pExpr || pName ){ 5024efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 5034efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 5044efc4754Sdrh pItem->pExpr = pExpr; 505a99db3b6Sdrh pItem->zName = sqlite3NameFromToken(pName); 506a76b5dfcSdrh } 507a76b5dfcSdrh return pList; 508a76b5dfcSdrh } 509a76b5dfcSdrh 510a76b5dfcSdrh /* 511a76b5dfcSdrh ** Delete an entire expression list. 512a76b5dfcSdrh */ 5134adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 514a76b5dfcSdrh int i; 515be5c89acSdrh struct ExprList_item *pItem; 516a76b5dfcSdrh if( pList==0 ) return; 5171bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 5181bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 519be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 520be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 521be5c89acSdrh sqliteFree(pItem->zName); 522a76b5dfcSdrh } 523a76b5dfcSdrh sqliteFree(pList->a); 524a76b5dfcSdrh sqliteFree(pList); 525a76b5dfcSdrh } 526a76b5dfcSdrh 527a76b5dfcSdrh /* 528fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 529fef5208cSdrh ** and 0 if it involves variables. 5302398937bSdrh ** 5312398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 5322398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 5332398937bSdrh ** a constant. 534fef5208cSdrh */ 5354adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 536fef5208cSdrh switch( p->op ){ 537fef5208cSdrh case TK_ID: 538967e8b73Sdrh case TK_COLUMN: 539fef5208cSdrh case TK_DOT: 5407bdc0c1dSdrh case TK_FUNCTION: 541fef5208cSdrh return 0; 5427bdc0c1dSdrh case TK_NULL: 5432398937bSdrh case TK_STRING: 544c572ef7fSdanielk1977 case TK_BLOB: 5459208643dSdrh case TK_INTEGER: 5469208643dSdrh case TK_FLOAT: 54750457896Sdrh case TK_VARIABLE: 5489208643dSdrh return 1; 549fef5208cSdrh default: { 5504adee20fSdanielk1977 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0; 5514adee20fSdanielk1977 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0; 552fef5208cSdrh if( p->pList ){ 553fef5208cSdrh int i; 554fef5208cSdrh for(i=0; i<p->pList->nExpr; i++){ 5554adee20fSdanielk1977 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0; 556fef5208cSdrh } 557fef5208cSdrh } 5589208643dSdrh return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); 559fef5208cSdrh } 560fef5208cSdrh } 5619208643dSdrh return 0; 562fef5208cSdrh } 563fef5208cSdrh 564fef5208cSdrh /* 565202b2df7Sdrh ** If the given expression codes a constant integer that is small enough 566202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 567202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 568202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 569e4de1febSdrh */ 5704adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 571e4de1febSdrh switch( p->op ){ 572e4de1febSdrh case TK_INTEGER: { 573fec19aadSdrh if( sqlite3GetInt32(p->token.z, pValue) ){ 574e4de1febSdrh return 1; 575e4de1febSdrh } 576202b2df7Sdrh break; 577202b2df7Sdrh } 578e4de1febSdrh case TK_STRING: { 5794c755c0fSdrh const u8 *z = (u8*)p->token.z; 580e4de1febSdrh int n = p->token.n; 581bd790ee3Sdrh if( n>0 && z[0]=='-' ){ z++; n--; } 582e4de1febSdrh while( n>0 && *z && isdigit(*z) ){ z++; n--; } 583fec19aadSdrh if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){ 584e4de1febSdrh return 1; 585e4de1febSdrh } 586e4de1febSdrh break; 587e4de1febSdrh } 5884b59ab5eSdrh case TK_UPLUS: { 5894adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 5904b59ab5eSdrh } 591e4de1febSdrh case TK_UMINUS: { 592e4de1febSdrh int v; 5934adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 594e4de1febSdrh *pValue = -v; 595e4de1febSdrh return 1; 596e4de1febSdrh } 597e4de1febSdrh break; 598e4de1febSdrh } 599e4de1febSdrh default: break; 600e4de1febSdrh } 601e4de1febSdrh return 0; 602e4de1febSdrh } 603e4de1febSdrh 604e4de1febSdrh /* 605c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 606c4a3c779Sdrh */ 6074adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 6084adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 6094adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 6104adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 611c4a3c779Sdrh return 0; 612c4a3c779Sdrh } 613c4a3c779Sdrh 614c4a3c779Sdrh /* 6158141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 6168141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 6178141f61eSdrh ** expression node refer back to that source column. The following changes 6188141f61eSdrh ** are made to pExpr: 6198141f61eSdrh ** 6208141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 6218141f61eSdrh ** the table. 6228141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 6238141f61eSdrh ** from pSrcList. 6248141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 6258141f61eSdrh ** pExpr->op Set to TK_COLUMN. 6268141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 6278141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 6288141f61eSdrh ** 6298141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 6308141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 6318141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 6328141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 6338141f61eSdrh ** means that the form of the name is Z and that columns from any table 6348141f61eSdrh ** can be used. 6358141f61eSdrh ** 6368141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 6378141f61eSdrh ** in pParse and return non-zero. Return zero on success. 6388141f61eSdrh */ 6398141f61eSdrh static int lookupName( 6408141f61eSdrh Parse *pParse, /* The parsing context */ 6418141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 6428141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 6438141f61eSdrh Token *pColumnToken, /* Name of the column. */ 6448141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 6458141f61eSdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 6468141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 6478141f61eSdrh ){ 6488141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 6498141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 6508141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 6518141f61eSdrh int i, j; /* Loop counters */ 6528141f61eSdrh int cnt = 0; /* Number of matching column names */ 6538141f61eSdrh int cntTab = 0; /* Number of matching table names */ 6549bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 6558141f61eSdrh 6568141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 657a99db3b6Sdrh zDb = sqlite3NameFromToken(pDbToken); 658a99db3b6Sdrh zTab = sqlite3NameFromToken(pTableToken); 659a99db3b6Sdrh zCol = sqlite3NameFromToken(pColumnToken); 66024b03fd0Sdanielk1977 if( sqlite3_malloc_failed ){ 6618141f61eSdrh return 1; /* Leak memory (zDb and zTab) if malloc fails */ 6628141f61eSdrh } 6638141f61eSdrh assert( zTab==0 || pEList==0 ); 6648141f61eSdrh 6658141f61eSdrh pExpr->iTable = -1; 6668141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 6678141f61eSdrh struct SrcList_item *pItem = &pSrcList->a[i]; 6688141f61eSdrh Table *pTab = pItem->pTab; 6698141f61eSdrh Column *pCol; 6708141f61eSdrh 6718141f61eSdrh if( pTab==0 ) continue; 6728141f61eSdrh assert( pTab->nCol>0 ); 6738141f61eSdrh if( zTab ){ 6748141f61eSdrh if( pItem->zAlias ){ 6758141f61eSdrh char *zTabName = pItem->zAlias; 6764adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 6778141f61eSdrh }else{ 6788141f61eSdrh char *zTabName = pTab->zName; 6794adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 6804adee20fSdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 6818141f61eSdrh continue; 6828141f61eSdrh } 6838141f61eSdrh } 6848141f61eSdrh } 6858141f61eSdrh if( 0==(cntTab++) ){ 6868141f61eSdrh pExpr->iTable = pItem->iCursor; 6878141f61eSdrh pExpr->iDb = pTab->iDb; 6888141f61eSdrh } 6898141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 6904adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 6918141f61eSdrh cnt++; 6928141f61eSdrh pExpr->iTable = pItem->iCursor; 6938141f61eSdrh pExpr->iDb = pTab->iDb; 6948141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 6958141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 696a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 6970202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 6988141f61eSdrh break; 6998141f61eSdrh } 7008141f61eSdrh } 7018141f61eSdrh } 7028141f61eSdrh 7038141f61eSdrh /* If we have not already resolved the name, then maybe 7048141f61eSdrh ** it is a new.* or old.* trigger argument reference 7058141f61eSdrh */ 7068141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 7078141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 7088141f61eSdrh Table *pTab = 0; 7094adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 7108141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 7118141f61eSdrh assert( pTriggerStack->pTab ); 7128141f61eSdrh pTab = pTriggerStack->pTab; 7134adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){ 7148141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 7158141f61eSdrh assert( pTriggerStack->pTab ); 7168141f61eSdrh pTab = pTriggerStack->pTab; 7178141f61eSdrh } 7188141f61eSdrh 7198141f61eSdrh if( pTab ){ 7208141f61eSdrh int j; 7218141f61eSdrh Column *pCol = pTab->aCol; 7228141f61eSdrh 7238141f61eSdrh pExpr->iDb = pTab->iDb; 7248141f61eSdrh cntTab++; 7258141f61eSdrh for(j=0; j < pTab->nCol; j++, pCol++) { 7264adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 7278141f61eSdrh cnt++; 7288141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 729a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 7300202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 7318141f61eSdrh break; 7328141f61eSdrh } 7338141f61eSdrh } 7348141f61eSdrh } 7358141f61eSdrh } 7368141f61eSdrh 7378141f61eSdrh /* 7388141f61eSdrh ** Perhaps the name is a reference to the ROWID 7398141f61eSdrh */ 7404adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 7418141f61eSdrh cnt = 1; 7428141f61eSdrh pExpr->iColumn = -1; 743a37cdde0Sdanielk1977 pExpr->affinity = SQLITE_AFF_INTEGER; 7448141f61eSdrh } 7458141f61eSdrh 7468141f61eSdrh /* 7478141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 7488141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 7498141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 7508141f61eSdrh ** 7518141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 7528141f61eSdrh ** 7538141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 7548141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 7558141f61eSdrh ** Note that the expression in the result set should have already been 7568141f61eSdrh ** resolved by the time the WHERE clause is resolved. 7578141f61eSdrh */ 7588141f61eSdrh if( cnt==0 && pEList!=0 ){ 7598141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 7608141f61eSdrh char *zAs = pEList->a[j].zName; 7614adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 7628141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 7638141f61eSdrh pExpr->op = TK_AS; 7648141f61eSdrh pExpr->iColumn = j; 7654adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 7668141f61eSdrh sqliteFree(zCol); 7678141f61eSdrh assert( zTab==0 && zDb==0 ); 7688141f61eSdrh return 0; 7698141f61eSdrh } 7708141f61eSdrh } 7718141f61eSdrh } 7728141f61eSdrh 7738141f61eSdrh /* 7748141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 7758141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 7768141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 7778141f61eSdrh ** case, we need to return right away and not make any changes to 7788141f61eSdrh ** pExpr. 7798141f61eSdrh */ 7808141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 7818141f61eSdrh sqliteFree(zCol); 7828141f61eSdrh return 0; 7838141f61eSdrh } 7848141f61eSdrh 7858141f61eSdrh /* 7868141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 7878141f61eSdrh ** more matches. Either way, we have an error. 7888141f61eSdrh */ 7898141f61eSdrh if( cnt!=1 ){ 7908141f61eSdrh char *z = 0; 7918141f61eSdrh char *zErr; 7928141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 7938141f61eSdrh if( zDb ){ 7944adee20fSdanielk1977 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); 7958141f61eSdrh }else if( zTab ){ 7964adee20fSdanielk1977 sqlite3SetString(&z, zTab, ".", zCol, 0); 7978141f61eSdrh }else{ 7988141f61eSdrh z = sqliteStrDup(zCol); 7998141f61eSdrh } 8004adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 8018141f61eSdrh sqliteFree(z); 8028141f61eSdrh } 8038141f61eSdrh 8048141f61eSdrh /* Clean up and return 8058141f61eSdrh */ 8068141f61eSdrh sqliteFree(zDb); 8078141f61eSdrh sqliteFree(zTab); 8088141f61eSdrh sqliteFree(zCol); 8094adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 8108141f61eSdrh pExpr->pLeft = 0; 8114adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 8128141f61eSdrh pExpr->pRight = 0; 8138141f61eSdrh pExpr->op = TK_COLUMN; 8144adee20fSdanielk1977 sqlite3AuthRead(pParse, pExpr, pSrcList); 8158141f61eSdrh return cnt!=1; 8168141f61eSdrh } 8178141f61eSdrh 8188141f61eSdrh /* 819cce7d176Sdrh ** This routine walks an expression tree and resolves references to 820967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 821aacc543eSdrh ** index to the table in the table list and a column offset. The 822aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 823aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 824832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 825aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 826aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 827aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 828aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 829aacc543eSdrh ** alias for ROWID. 83019a775c2Sdrh ** 831fef5208cSdrh ** We also check for instances of the IN operator. IN comes in two 832fef5208cSdrh ** forms: 833fef5208cSdrh ** 834fef5208cSdrh ** expr IN (exprlist) 835fef5208cSdrh ** and 836fef5208cSdrh ** expr IN (SELECT ...) 837fef5208cSdrh ** 838fef5208cSdrh ** The first form is handled by creating a set holding the list 839fef5208cSdrh ** of allowed values. The second form causes the SELECT to generate 840fef5208cSdrh ** a temporary table. 841fef5208cSdrh ** 842fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression. 84319a775c2Sdrh ** If it finds any, it generates code to write the value of that select 84419a775c2Sdrh ** into a memory cell. 845cce7d176Sdrh ** 846967e8b73Sdrh ** Unknown columns or tables provoke an error. The function returns 847cce7d176Sdrh ** the number of errors seen and leaves an error message on pParse->zErrMsg. 848cce7d176Sdrh */ 8494adee20fSdanielk1977 int sqlite3ExprResolveIds( 850a2e00042Sdrh Parse *pParse, /* The parser context */ 8518141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 852a2e00042Sdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 853a2e00042Sdrh Expr *pExpr /* The expression to be analyzed. */ 854a2e00042Sdrh ){ 8556a3ea0e6Sdrh int i; 8566a3ea0e6Sdrh 8578141f61eSdrh if( pExpr==0 || pSrcList==0 ) return 0; 8588141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 8598141f61eSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab ); 8606a3ea0e6Sdrh } 861cce7d176Sdrh switch( pExpr->op ){ 8622398937bSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 8632398937bSdrh ** possible. Otherwise they remain as strings. Single-quoted 8642398937bSdrh ** strings (ex: 'abc') are always string literals. 8652398937bSdrh */ 8662398937bSdrh case TK_STRING: { 8672398937bSdrh if( pExpr->token.z[0]=='\'' ) break; 8682398937bSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 8692398937bSdrh } 8708141f61eSdrh /* A lone identifier is the name of a columnd. 871a2e00042Sdrh */ 872cce7d176Sdrh case TK_ID: { 8738141f61eSdrh if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){ 874cce7d176Sdrh return 1; 875ed6c8671Sdrh } 876cce7d176Sdrh break; 877cce7d176Sdrh } 878cce7d176Sdrh 879d24cc427Sdrh /* A table name and column name: ID.ID 880d24cc427Sdrh ** Or a database, table and column: ID.ID.ID 881d24cc427Sdrh */ 882cce7d176Sdrh case TK_DOT: { 8838141f61eSdrh Token *pColumn; 8848141f61eSdrh Token *pTable; 8858141f61eSdrh Token *pDb; 8868141f61eSdrh Expr *pRight; 887cce7d176Sdrh 888cce7d176Sdrh pRight = pExpr->pRight; 889d24cc427Sdrh if( pRight->op==TK_ID ){ 8908141f61eSdrh pDb = 0; 8918141f61eSdrh pTable = &pExpr->pLeft->token; 8928141f61eSdrh pColumn = &pRight->token; 893d24cc427Sdrh }else{ 8948141f61eSdrh assert( pRight->op==TK_DOT ); 8958141f61eSdrh pDb = &pExpr->pLeft->token; 8968141f61eSdrh pTable = &pRight->pLeft->token; 8978141f61eSdrh pColumn = &pRight->pRight->token; 898d24cc427Sdrh } 8998141f61eSdrh if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){ 900daffd0e5Sdrh return 1; 901daffd0e5Sdrh } 902cce7d176Sdrh break; 903cce7d176Sdrh } 904cce7d176Sdrh 905fef5208cSdrh case TK_IN: { 906e014a838Sdanielk1977 char affinity; 9074adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 908d3d39e93Sdrh KeyInfo keyInfo; 9090202b29eSdanielk1977 int addr; /* Address of OP_OpenTemp instruction */ 910d3d39e93Sdrh 911fef5208cSdrh if( v==0 ) return 1; 9124adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 913cfab11bcSdrh return 1; 914cfab11bcSdrh } 915bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 916e014a838Sdanielk1977 917e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 918e014a838Sdanielk1977 ** expression it is handled the same way. A temporary table is 919e014a838Sdanielk1977 ** filled with single-field index keys representing the results 920e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 921fef5208cSdrh ** 922e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 923e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 924e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 925e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 926e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 927e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 928e014a838Sdanielk1977 ** is used. 929fef5208cSdrh */ 930832508b7Sdrh pExpr->iTable = pParse->nTab++; 9310202b29eSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0); 932d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 933d3d39e93Sdrh keyInfo.nField = 1; 934f3218feaSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); 935e014a838Sdanielk1977 936e014a838Sdanielk1977 if( pExpr->pSelect ){ 937e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 938e014a838Sdanielk1977 ** 939e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 940e014a838Sdanielk1977 ** table allocated and opened above. 941e014a838Sdanielk1977 */ 942e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 943be5c89acSdrh ExprList *pEList; 944e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 945bf3b721fSdanielk1977 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0); 946be5c89acSdrh pEList = pExpr->pSelect->pEList; 947be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 9487cedc8d4Sdanielk1977 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, 949be5c89acSdrh pEList->a[0].pExpr); 9500202b29eSdanielk1977 } 951fef5208cSdrh }else if( pExpr->pList ){ 952fef5208cSdrh /* Case 2: expr IN (exprlist) 953fef5208cSdrh ** 954e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 955e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 956e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 957e014a838Sdanielk1977 ** a column, use numeric affinity. 958fef5208cSdrh */ 959e014a838Sdanielk1977 int i; 960e014a838Sdanielk1977 if( !affinity ){ 961e014a838Sdanielk1977 affinity = SQLITE_AFF_NUMERIC; 962e014a838Sdanielk1977 } 9630202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 964e014a838Sdanielk1977 965e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 966fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 967fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 968e014a838Sdanielk1977 969e014a838Sdanielk1977 /* Check that the expression is constant and valid. */ 9704adee20fSdanielk1977 if( !sqlite3ExprIsConstant(pE2) ){ 9714adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 972da93d238Sdrh "right-hand side of IN operator must be constant"); 973fef5208cSdrh return 1; 974fef5208cSdrh } 9754adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){ 9764794b980Sdrh return 1; 9774794b980Sdrh } 978e014a838Sdanielk1977 979e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 9804adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 98194a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); 9820f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 983e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0); 984fef5208cSdrh } 985fef5208cSdrh } 9860202b29eSdanielk1977 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); 9870202b29eSdanielk1977 988cfab11bcSdrh break; 989fef5208cSdrh } 990fef5208cSdrh 99119a775c2Sdrh case TK_SELECT: { 992fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 993fef5208cSdrh ** value of this select in a memory cell and record the number 994967e8b73Sdrh ** of the memory cell in iColumn. 995fef5208cSdrh */ 996967e8b73Sdrh pExpr->iColumn = pParse->nMem++; 997bf3b721fSdanielk1977 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){ 99819a775c2Sdrh return 1; 99919a775c2Sdrh } 100019a775c2Sdrh break; 100119a775c2Sdrh } 100219a775c2Sdrh 1003cce7d176Sdrh /* For all else, just recursively walk the tree */ 1004cce7d176Sdrh default: { 1005cce7d176Sdrh if( pExpr->pLeft 10064adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 1007cce7d176Sdrh return 1; 1008cce7d176Sdrh } 1009cce7d176Sdrh if( pExpr->pRight 10104adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){ 1011cce7d176Sdrh return 1; 1012cce7d176Sdrh } 1013cce7d176Sdrh if( pExpr->pList ){ 1014cce7d176Sdrh int i; 1015cce7d176Sdrh ExprList *pList = pExpr->pList; 1016cce7d176Sdrh for(i=0; i<pList->nExpr; i++){ 1017832508b7Sdrh Expr *pArg = pList->a[i].pExpr; 10184adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){ 1019cce7d176Sdrh return 1; 1020cce7d176Sdrh } 1021cce7d176Sdrh } 1022cce7d176Sdrh } 1023cce7d176Sdrh } 1024cce7d176Sdrh } 1025cce7d176Sdrh return 0; 1026cce7d176Sdrh } 1027cce7d176Sdrh 1028cce7d176Sdrh /* 10294b59ab5eSdrh ** pExpr is a node that defines a function of some kind. It might 10304b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function 10314b59ab5eSdrh ** that implements an operator, like "a LIKE b". 10324b59ab5eSdrh ** 10334b59ab5eSdrh ** This routine makes *pzName point to the name of the function and 10344b59ab5eSdrh ** *pnName hold the number of characters in the function name. 10354b59ab5eSdrh */ 10364b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ 10374b59ab5eSdrh switch( pExpr->op ){ 10384b59ab5eSdrh case TK_FUNCTION: { 10394b59ab5eSdrh *pzName = pExpr->token.z; 10406977fea8Sdrh *pnName = pExpr->token.n; 10414b59ab5eSdrh break; 10424b59ab5eSdrh } 10434b59ab5eSdrh case TK_LIKE: { 10444b59ab5eSdrh *pzName = "like"; 10454b59ab5eSdrh *pnName = 4; 10464b59ab5eSdrh break; 10474b59ab5eSdrh } 10484b59ab5eSdrh case TK_GLOB: { 10494b59ab5eSdrh *pzName = "glob"; 10504b59ab5eSdrh *pnName = 4; 10514b59ab5eSdrh break; 10524b59ab5eSdrh } 10534b59ab5eSdrh default: { 10544b59ab5eSdrh *pzName = "can't happen"; 10554b59ab5eSdrh *pnName = 12; 10564b59ab5eSdrh break; 10574b59ab5eSdrh } 10584b59ab5eSdrh } 10594b59ab5eSdrh } 10604b59ab5eSdrh 10614b59ab5eSdrh /* 1062cce7d176Sdrh ** Error check the functions in an expression. Make sure all 1063cce7d176Sdrh ** function names are recognized and all functions have the correct 1064cce7d176Sdrh ** number of arguments. Leave an error message in pParse->zErrMsg 1065cce7d176Sdrh ** if anything is amiss. Return the number of errors. 1066cce7d176Sdrh ** 1067cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function 1068cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg. 1069cce7d176Sdrh */ 10704adee20fSdanielk1977 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ 1071cce7d176Sdrh int nErr = 0; 1072cce7d176Sdrh if( pExpr==0 ) return 0; 1073cce7d176Sdrh switch( pExpr->op ){ 10744b59ab5eSdrh case TK_GLOB: 10754b59ab5eSdrh case TK_LIKE: 1076cce7d176Sdrh case TK_FUNCTION: { 1077c9b84a1fSdrh int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ 1078c9b84a1fSdrh int no_such_func = 0; /* True if no such function exists */ 1079c9b84a1fSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1080c9b84a1fSdrh int is_agg = 0; /* True if is an aggregate function */ 1081cce7d176Sdrh int i; 10824b59ab5eSdrh int nId; /* Number of characters in function name */ 10834b59ab5eSdrh const char *zId; /* The function name. */ 10840bce8354Sdrh FuncDef *pDef; 1085d8123366Sdanielk1977 int enc = pParse->db->enc; 10860bce8354Sdrh 10874b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 1088d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 10890bce8354Sdrh if( pDef==0 ){ 1090d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 10910bce8354Sdrh if( pDef==0 ){ 1092cce7d176Sdrh no_such_func = 1; 10938e0a2f90Sdrh }else{ 10948e0a2f90Sdrh wrong_num_args = 1; 10958e0a2f90Sdrh } 10968e0a2f90Sdrh }else{ 10970bce8354Sdrh is_agg = pDef->xFunc==0; 1098cce7d176Sdrh } 10998e0a2f90Sdrh if( is_agg && !allowAgg ){ 11004adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId); 11018e0a2f90Sdrh nErr++; 11028e0a2f90Sdrh is_agg = 0; 11038e0a2f90Sdrh }else if( no_such_func ){ 11044adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1105cce7d176Sdrh nErr++; 11068e0a2f90Sdrh }else if( wrong_num_args ){ 11074adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1108f7a9e1acSdrh nId, zId); 11098e0a2f90Sdrh nErr++; 1110cce7d176Sdrh } 1111f7a9e1acSdrh if( is_agg ){ 1112f7a9e1acSdrh pExpr->op = TK_AGG_FUNCTION; 1113f7a9e1acSdrh if( pIsAgg ) *pIsAgg = 1; 1114f7a9e1acSdrh } 1115cce7d176Sdrh for(i=0; nErr==0 && i<n; i++){ 11164adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr, 11174cfa7934Sdrh allowAgg && !is_agg, pIsAgg); 1118cce7d176Sdrh } 11190202b29eSdanielk1977 /* FIX ME: Compute pExpr->affinity based on the expected return 11200202b29eSdanielk1977 ** type of the function 11210202b29eSdanielk1977 */ 1122cce7d176Sdrh } 1123cce7d176Sdrh default: { 1124cce7d176Sdrh if( pExpr->pLeft ){ 11254adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); 1126cce7d176Sdrh } 1127cce7d176Sdrh if( nErr==0 && pExpr->pRight ){ 11284adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); 1129cce7d176Sdrh } 1130fef5208cSdrh if( nErr==0 && pExpr->pList ){ 1131fef5208cSdrh int n = pExpr->pList->nExpr; 1132fef5208cSdrh int i; 1133fef5208cSdrh for(i=0; nErr==0 && i<n; i++){ 11342282792aSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 11354adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg); 1136fef5208cSdrh } 1137fef5208cSdrh } 1138cce7d176Sdrh break; 1139cce7d176Sdrh } 1140cce7d176Sdrh } 1141cce7d176Sdrh return nErr; 1142cce7d176Sdrh } 1143cce7d176Sdrh 1144cce7d176Sdrh /* 1145290c1948Sdrh ** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck(). 1146290c1948Sdrh ** 1147290c1948Sdrh ** This routine is provided as a convenience since it is very common 1148290c1948Sdrh ** to call ResolveIds() and Check() back to back. 1149290c1948Sdrh */ 1150290c1948Sdrh int sqlite3ExprResolveAndCheck( 1151290c1948Sdrh Parse *pParse, /* The parser context */ 1152290c1948Sdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 1153290c1948Sdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 1154290c1948Sdrh Expr *pExpr, /* The expression to be analyzed. */ 1155290c1948Sdrh int allowAgg, /* True to allow aggregate expressions */ 1156290c1948Sdrh int *pIsAgg /* Set to TRUE if aggregates are found */ 1157290c1948Sdrh ){ 1158290c1948Sdrh if( pExpr==0 ) return 0; 1159290c1948Sdrh if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){ 1160290c1948Sdrh return 1; 1161290c1948Sdrh } 1162290c1948Sdrh return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg); 1163290c1948Sdrh } 1164290c1948Sdrh 1165290c1948Sdrh /* 1166fec19aadSdrh ** Generate an instruction that will put the integer describe by 1167fec19aadSdrh ** text z[0..n-1] on the stack. 1168fec19aadSdrh */ 1169fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1170fec19aadSdrh int i; 11716fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 11726fec0762Sdrh sqlite3VdbeAddOp(v, OP_Integer, i, 0); 11736fec0762Sdrh }else if( sqlite3FitsIn64Bits(z) ){ 11746fec0762Sdrh sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n); 1175fec19aadSdrh }else{ 1176fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1177fec19aadSdrh } 1178fec19aadSdrh } 1179fec19aadSdrh 1180fec19aadSdrh /* 1181cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 11821ccde15dSdrh ** expression and leave the result on the top of stack. 1183*f2bc013cSdrh ** 1184*f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1185*f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1186*f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1187*f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1188*f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1189cce7d176Sdrh */ 11904adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1191cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1192cce7d176Sdrh int op; 1193daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1194*f2bc013cSdrh op = pExpr->op; 1195*f2bc013cSdrh switch( op ){ 1196967e8b73Sdrh case TK_COLUMN: { 11972282792aSdrh if( pParse->useAgg ){ 11984adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1199c4a3c779Sdrh }else if( pExpr->iColumn>=0 ){ 12004adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1201145716b3Sdrh #ifndef NDEBUG 1202145716b3Sdrh if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){ 1203ad6d9460Sdrh VdbeComment((v, "# %T", &pExpr->span)); 1204145716b3Sdrh } 1205145716b3Sdrh #endif 1206c4a3c779Sdrh }else{ 12074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0); 12082282792aSdrh } 1209cce7d176Sdrh break; 1210cce7d176Sdrh } 1211cce7d176Sdrh case TK_INTEGER: { 1212fec19aadSdrh codeInteger(v, pExpr->token.z, pExpr->token.n); 1213fec19aadSdrh break; 121451e9a445Sdrh } 1215fec19aadSdrh case TK_FLOAT: 1216fec19aadSdrh case TK_STRING: { 1217*f2bc013cSdrh assert( TK_FLOAT==OP_Real ); 1218*f2bc013cSdrh assert( TK_STRING==OP_String8 ); 1219fec19aadSdrh sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); 12204adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1221cce7d176Sdrh break; 1222cce7d176Sdrh } 1223c572ef7fSdanielk1977 case TK_BLOB: { 1224*f2bc013cSdrh assert( TK_BLOB==OP_HexBlob ); 1225c572ef7fSdanielk1977 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1); 1226c572ef7fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1227c572ef7fSdanielk1977 break; 1228c572ef7fSdanielk1977 } 1229cce7d176Sdrh case TK_NULL: { 12300f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 1231cce7d176Sdrh break; 1232cce7d176Sdrh } 123350457896Sdrh case TK_VARIABLE: { 12344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 1235895d7472Sdrh if( pExpr->token.n>1 ){ 1236895d7472Sdrh sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); 1237895d7472Sdrh } 123850457896Sdrh break; 123950457896Sdrh } 1240c9b84a1fSdrh case TK_LT: 1241c9b84a1fSdrh case TK_LE: 1242c9b84a1fSdrh case TK_GT: 1243c9b84a1fSdrh case TK_GE: 1244c9b84a1fSdrh case TK_NE: 1245c9b84a1fSdrh case TK_EQ: { 1246*f2bc013cSdrh assert( TK_LT==OP_Lt ); 1247*f2bc013cSdrh assert( TK_LE==OP_Le ); 1248*f2bc013cSdrh assert( TK_GT==OP_Gt ); 1249*f2bc013cSdrh assert( TK_GE==OP_Ge ); 1250*f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1251*f2bc013cSdrh assert( TK_NE==OP_Ne ); 1252a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1253a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1254be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); 1255a37cdde0Sdanielk1977 break; 1256c9b84a1fSdrh } 1257cce7d176Sdrh case TK_AND: 1258cce7d176Sdrh case TK_OR: 1259cce7d176Sdrh case TK_PLUS: 1260cce7d176Sdrh case TK_STAR: 1261cce7d176Sdrh case TK_MINUS: 1262bf4133cbSdrh case TK_REM: 1263bf4133cbSdrh case TK_BITAND: 1264bf4133cbSdrh case TK_BITOR: 126517c40294Sdrh case TK_SLASH: 1266bf4133cbSdrh case TK_LSHIFT: 1267855eb1cfSdrh case TK_RSHIFT: 12680040077dSdrh case TK_CONCAT: { 1269*f2bc013cSdrh assert( TK_AND==OP_And ); 1270*f2bc013cSdrh assert( TK_OR==OP_Or ); 1271*f2bc013cSdrh assert( TK_PLUS==OP_Add ); 1272*f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 1273*f2bc013cSdrh assert( TK_REM==OP_Remainder ); 1274*f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 1275*f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 1276*f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 1277*f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 1278*f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 1279*f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 12804adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12814adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1282855eb1cfSdrh sqlite3VdbeAddOp(v, op, 0, 0); 12830040077dSdrh break; 12840040077dSdrh } 1285cce7d176Sdrh case TK_UMINUS: { 1286fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1287fec19aadSdrh assert( pLeft ); 1288fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1289fec19aadSdrh Token *p = &pLeft->token; 12906e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 12916e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 1292fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1293fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1294e6840900Sdrh }else{ 1295fec19aadSdrh codeInteger(v, z, p->n+1); 1296e6840900Sdrh } 12976e142f54Sdrh sqliteFree(z); 12986e142f54Sdrh break; 12996e142f54Sdrh } 13001ccde15dSdrh /* Fall through into TK_NOT */ 13016e142f54Sdrh } 1302bf4133cbSdrh case TK_BITNOT: 13036e142f54Sdrh case TK_NOT: { 1304*f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 1305*f2bc013cSdrh assert( TK_NOT==OP_Not ); 13064adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 13074adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1308cce7d176Sdrh break; 1309cce7d176Sdrh } 1310cce7d176Sdrh case TK_ISNULL: 1311cce7d176Sdrh case TK_NOTNULL: { 1312cce7d176Sdrh int dest; 1313*f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1314*f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 13154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 13164adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 13174adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 13184adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 13194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1320a37cdde0Sdanielk1977 break; 1321*f2bc013cSdrh } 13222282792aSdrh case TK_AGG_FUNCTION: { 13234adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 13242282792aSdrh break; 13252282792aSdrh } 13264b59ab5eSdrh case TK_GLOB: 13274b59ab5eSdrh case TK_LIKE: 1328cce7d176Sdrh case TK_FUNCTION: { 1329cce7d176Sdrh ExprList *pList = pExpr->pList; 133089425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 13310bce8354Sdrh FuncDef *pDef; 13324b59ab5eSdrh int nId; 13334b59ab5eSdrh const char *zId; 1334682f68b0Sdanielk1977 int p2 = 0; 1335682f68b0Sdanielk1977 int i; 1336d8123366Sdanielk1977 u8 enc = pParse->db->enc; 1337dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 13384b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 1339d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 13400bce8354Sdrh assert( pDef!=0 ); 1341f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList); 1342682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 1343d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 1344d02eb1fdSdanielk1977 p2 |= (1<<i); 1345d02eb1fdSdanielk1977 } 1346dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 1347dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 1348dc1bdc4fSdanielk1977 } 1349dc1bdc4fSdanielk1977 } 1350dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 1351dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 1352d8123366Sdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 1353682f68b0Sdanielk1977 } 1354682f68b0Sdanielk1977 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF); 13556ec2733bSdrh break; 13566ec2733bSdrh } 135719a775c2Sdrh case TK_SELECT: { 13584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 1359ad6d9460Sdrh VdbeComment((v, "# load subquery result")); 136019a775c2Sdrh break; 136119a775c2Sdrh } 1362fef5208cSdrh case TK_IN: { 1363fef5208cSdrh int addr; 136494a11211Sdrh char affinity; 1365e014a838Sdanielk1977 1366e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1367e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1368ededfd5eSdanielk1977 ** P3 of OP_MakeRecord. 1369e014a838Sdanielk1977 */ 137094a11211Sdrh affinity = comparisonAffinity(pExpr); 1371e014a838Sdanielk1977 13724adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1373e014a838Sdanielk1977 1374e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1375e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1376e014a838Sdanielk1977 */ 13774adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 13784adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1379e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 13804adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 13810f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 1382e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 138394a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ 1384e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1385e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1386e014a838Sdanielk1977 1387fef5208cSdrh break; 1388fef5208cSdrh } 1389fef5208cSdrh case TK_BETWEEN: { 1390be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1391be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 1392be5c89acSdrh Expr *pRight = pLItem->pExpr; 1393be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 13944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1395be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1396be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); 13974adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 1398be5c89acSdrh pLItem++; 1399be5c89acSdrh pRight = pLItem->pExpr; 1400be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1401be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); 14024adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1403fef5208cSdrh break; 1404fef5208cSdrh } 140551e9a445Sdrh case TK_UPLUS: 1406a2e00042Sdrh case TK_AS: { 14074adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1408a2e00042Sdrh break; 1409a2e00042Sdrh } 141017a7f8ddSdrh case TK_CASE: { 141117a7f8ddSdrh int expr_end_label; 1412f5905aa7Sdrh int jumpInst; 1413f5905aa7Sdrh int addr; 1414f5905aa7Sdrh int nExpr; 141517a7f8ddSdrh int i; 1416be5c89acSdrh ExprList *pEList; 1417be5c89acSdrh struct ExprList_item *aListelem; 141817a7f8ddSdrh 141917a7f8ddSdrh assert(pExpr->pList); 142017a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 142117a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1422be5c89acSdrh pEList = pExpr->pList; 1423be5c89acSdrh aListelem = pEList->a; 1424be5c89acSdrh nExpr = pEList->nExpr; 14254adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 142617a7f8ddSdrh if( pExpr->pLeft ){ 14274adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1428cce7d176Sdrh } 1429f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 1430be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i].pExpr); 143117a7f8ddSdrh if( pExpr->pLeft ){ 14324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 1433be5c89acSdrh jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, 1434be5c89acSdrh OP_Ne, 0, 1); 14354adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1436f5905aa7Sdrh }else{ 14374adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 143817a7f8ddSdrh } 1439be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr); 14404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 14414adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 14424adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst, addr); 144317a7f8ddSdrh } 1444f570f011Sdrh if( pExpr->pLeft ){ 14454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1446f570f011Sdrh } 144717a7f8ddSdrh if( pExpr->pRight ){ 14484adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 144917a7f8ddSdrh }else{ 14500f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 145117a7f8ddSdrh } 14524adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 14536f34903eSdanielk1977 break; 14546f34903eSdanielk1977 } 14556f34903eSdanielk1977 case TK_RAISE: { 14566f34903eSdanielk1977 if( !pParse->trigStack ){ 14574adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1458da93d238Sdrh "RAISE() may only be used within a trigger-program"); 14596f34903eSdanielk1977 return; 14606f34903eSdanielk1977 } 1461ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 1462ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 14636f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 1464ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 14654adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1466701a0aebSdrh pExpr->token.z, pExpr->token.n); 14674adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 14686f34903eSdanielk1977 } else { 14696f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 1470344737f6Sdrh sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); 1471ad6d9460Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 1472ad6d9460Sdrh VdbeComment((v, "# raise(IGNORE)")); 14736f34903eSdanielk1977 } 147417a7f8ddSdrh } 147517a7f8ddSdrh break; 147617a7f8ddSdrh } 1477cce7d176Sdrh } 1478cce7d176Sdrh 1479cce7d176Sdrh /* 1480268380caSdrh ** Generate code that pushes the value of every element of the given 1481f9b596ebSdrh ** expression list onto the stack. 1482268380caSdrh ** 1483268380caSdrh ** Return the number of elements pushed onto the stack. 1484268380caSdrh */ 14854adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1486268380caSdrh Parse *pParse, /* Parsing context */ 1487f9b596ebSdrh ExprList *pList /* The expression list to be coded */ 1488268380caSdrh ){ 1489268380caSdrh struct ExprList_item *pItem; 1490268380caSdrh int i, n; 1491268380caSdrh Vdbe *v; 1492268380caSdrh if( pList==0 ) return 0; 14934adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 1494268380caSdrh n = pList->nExpr; 1495268380caSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 14964adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1497268380caSdrh } 1498f9b596ebSdrh return n; 1499268380caSdrh } 1500268380caSdrh 1501268380caSdrh /* 1502cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1503cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1504cce7d176Sdrh ** continues straight thru if the expression is false. 1505f5905aa7Sdrh ** 1506f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1507f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1508*f2bc013cSdrh ** 1509*f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1510*f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1511*f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1512*f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1513*f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1514cce7d176Sdrh */ 15154adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1516cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1517cce7d176Sdrh int op = 0; 1518daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1519*f2bc013cSdrh op = pExpr->op; 1520*f2bc013cSdrh switch( op ){ 1521cce7d176Sdrh case TK_AND: { 15224adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 15234adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 15244adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 15254adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1526cce7d176Sdrh break; 1527cce7d176Sdrh } 1528cce7d176Sdrh case TK_OR: { 15294adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 15304adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1531cce7d176Sdrh break; 1532cce7d176Sdrh } 1533cce7d176Sdrh case TK_NOT: { 15344adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1535cce7d176Sdrh break; 1536cce7d176Sdrh } 1537cce7d176Sdrh case TK_LT: 1538cce7d176Sdrh case TK_LE: 1539cce7d176Sdrh case TK_GT: 1540cce7d176Sdrh case TK_GE: 1541cce7d176Sdrh case TK_NE: 15420ac65892Sdrh case TK_EQ: { 1543*f2bc013cSdrh assert( TK_LT==OP_Lt ); 1544*f2bc013cSdrh assert( TK_LE==OP_Le ); 1545*f2bc013cSdrh assert( TK_GT==OP_Gt ); 1546*f2bc013cSdrh assert( TK_GE==OP_Ge ); 1547*f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1548*f2bc013cSdrh assert( TK_NE==OP_Ne ); 15494adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15504adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1551be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 1552cce7d176Sdrh break; 1553cce7d176Sdrh } 1554cce7d176Sdrh case TK_ISNULL: 1555cce7d176Sdrh case TK_NOTNULL: { 1556*f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1557*f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 15584adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15594adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1560cce7d176Sdrh break; 1561cce7d176Sdrh } 1562fef5208cSdrh case TK_BETWEEN: { 15630202b29eSdanielk1977 /* The expression "x BETWEEN y AND z" is implemented as: 15640202b29eSdanielk1977 ** 15650202b29eSdanielk1977 ** 1 IF (x < y) GOTO 3 15660202b29eSdanielk1977 ** 2 IF (x <= z) GOTO <dest> 15670202b29eSdanielk1977 ** 3 ... 15680202b29eSdanielk1977 */ 1569f5905aa7Sdrh int addr; 1570be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1571be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 1572be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 15734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1574be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1575be5c89acSdrh addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); 15760202b29eSdanielk1977 1577be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 1578be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1579be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); 15800202b29eSdanielk1977 15814adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 15824adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 15834adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1584fef5208cSdrh break; 1585fef5208cSdrh } 1586cce7d176Sdrh default: { 15874adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 15884adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1589cce7d176Sdrh break; 1590cce7d176Sdrh } 1591cce7d176Sdrh } 1592cce7d176Sdrh } 1593cce7d176Sdrh 1594cce7d176Sdrh /* 159566b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1596cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1597cce7d176Sdrh ** continues straight thru if the expression is true. 1598f5905aa7Sdrh ** 1599f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1600f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1601cce7d176Sdrh */ 16024adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1603cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1604cce7d176Sdrh int op = 0; 1605daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1606*f2bc013cSdrh 1607*f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 1608*f2bc013cSdrh ** 1609*f2bc013cSdrh ** pExpr->op op 1610*f2bc013cSdrh ** --------- ---------- 1611*f2bc013cSdrh ** TK_ISNULL OP_NotNull 1612*f2bc013cSdrh ** TK_NOTNULL OP_IsNull 1613*f2bc013cSdrh ** TK_NE OP_Eq 1614*f2bc013cSdrh ** TK_EQ OP_Ne 1615*f2bc013cSdrh ** TK_GT OP_Le 1616*f2bc013cSdrh ** TK_LE OP_Gt 1617*f2bc013cSdrh ** TK_GE OP_Lt 1618*f2bc013cSdrh ** TK_LT OP_Ge 1619*f2bc013cSdrh ** 1620*f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 1621*f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 1622*f2bc013cSdrh ** can compute the mapping above using the following expression. 1623*f2bc013cSdrh ** Assert()s verify that the computation is correct. 1624*f2bc013cSdrh */ 1625*f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 1626*f2bc013cSdrh 1627*f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 1628*f2bc013cSdrh */ 1629*f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 1630*f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 1631*f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 1632*f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 1633*f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 1634*f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 1635*f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 1636*f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 1637*f2bc013cSdrh 1638cce7d176Sdrh switch( pExpr->op ){ 1639cce7d176Sdrh case TK_AND: { 16404adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 16414adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1642cce7d176Sdrh break; 1643cce7d176Sdrh } 1644cce7d176Sdrh case TK_OR: { 16454adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 16464adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 16474adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 16484adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1649cce7d176Sdrh break; 1650cce7d176Sdrh } 1651cce7d176Sdrh case TK_NOT: { 16524adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1653cce7d176Sdrh break; 1654cce7d176Sdrh } 1655cce7d176Sdrh case TK_LT: 1656cce7d176Sdrh case TK_LE: 1657cce7d176Sdrh case TK_GT: 1658cce7d176Sdrh case TK_GE: 1659cce7d176Sdrh case TK_NE: 1660cce7d176Sdrh case TK_EQ: { 16614adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 16624adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1663be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 1664cce7d176Sdrh break; 1665cce7d176Sdrh } 1666cce7d176Sdrh case TK_ISNULL: 1667cce7d176Sdrh case TK_NOTNULL: { 16684adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 16694adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1670cce7d176Sdrh break; 1671cce7d176Sdrh } 1672fef5208cSdrh case TK_BETWEEN: { 16730202b29eSdanielk1977 /* The expression is "x BETWEEN y AND z". It is implemented as: 16740202b29eSdanielk1977 ** 16750202b29eSdanielk1977 ** 1 IF (x >= y) GOTO 3 16760202b29eSdanielk1977 ** 2 GOTO <dest> 16770202b29eSdanielk1977 ** 3 IF (x > z) GOTO <dest> 16780202b29eSdanielk1977 */ 1679fef5208cSdrh int addr; 1680be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1681be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 1682be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 16834adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1684be5c89acSdrh sqlite3ExprCode(pParse, pRight); 16854adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1686be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); 1687be5c89acSdrh 16884adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 16894adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 1690be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 1691be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1692be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); 1693fef5208cSdrh break; 1694fef5208cSdrh } 1695cce7d176Sdrh default: { 16964adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 16974adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1698cce7d176Sdrh break; 1699cce7d176Sdrh } 1700cce7d176Sdrh } 1701cce7d176Sdrh } 17022282792aSdrh 17032282792aSdrh /* 17042282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 17052282792aSdrh ** if they are identical and return FALSE if they differ in any way. 17062282792aSdrh */ 17074adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 17082282792aSdrh int i; 17092282792aSdrh if( pA==0 ){ 17102282792aSdrh return pB==0; 17112282792aSdrh }else if( pB==0 ){ 17122282792aSdrh return 0; 17132282792aSdrh } 17142282792aSdrh if( pA->op!=pB->op ) return 0; 17154adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 17164adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 17172282792aSdrh if( pA->pList ){ 17182282792aSdrh if( pB->pList==0 ) return 0; 17192282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 17202282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 17214adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 17222282792aSdrh return 0; 17232282792aSdrh } 17242282792aSdrh } 17252282792aSdrh }else if( pB->pList ){ 17262282792aSdrh return 0; 17272282792aSdrh } 17282282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 17292f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 17302282792aSdrh if( pA->token.z ){ 17312282792aSdrh if( pB->token.z==0 ) return 0; 17326977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 17334adee20fSdanielk1977 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 17342282792aSdrh } 17352282792aSdrh return 1; 17362282792aSdrh } 17372282792aSdrh 17382282792aSdrh /* 17392282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index. 17402282792aSdrh */ 17412282792aSdrh static int appendAggInfo(Parse *pParse){ 17422282792aSdrh if( (pParse->nAgg & 0x7)==0 ){ 17432282792aSdrh int amt = pParse->nAgg + 8; 17446d4abfbeSdrh AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 17456d4abfbeSdrh if( aAgg==0 ){ 17462282792aSdrh return -1; 17472282792aSdrh } 17486d4abfbeSdrh pParse->aAgg = aAgg; 17492282792aSdrh } 17502282792aSdrh memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 17512282792aSdrh return pParse->nAgg++; 17522282792aSdrh } 17532282792aSdrh 17542282792aSdrh /* 17552282792aSdrh ** Analyze the given expression looking for aggregate functions and 17562282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 17572282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 17582282792aSdrh ** 17592282792aSdrh ** This routine should only be called after the expression has been 17604adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck(). 17612282792aSdrh ** 17622282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return 17632282792aSdrh ** the number of errors. 17642282792aSdrh */ 17654adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ 17662282792aSdrh int i; 17672282792aSdrh AggExpr *aAgg; 17682282792aSdrh int nErr = 0; 17692282792aSdrh 17702282792aSdrh if( pExpr==0 ) return 0; 17712282792aSdrh switch( pExpr->op ){ 1772967e8b73Sdrh case TK_COLUMN: { 17732282792aSdrh aAgg = pParse->aAgg; 17742282792aSdrh for(i=0; i<pParse->nAgg; i++){ 17752282792aSdrh if( aAgg[i].isAgg ) continue; 17762282792aSdrh if( aAgg[i].pExpr->iTable==pExpr->iTable 1777967e8b73Sdrh && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 17782282792aSdrh break; 17792282792aSdrh } 17802282792aSdrh } 17812282792aSdrh if( i>=pParse->nAgg ){ 17822282792aSdrh i = appendAggInfo(pParse); 17832282792aSdrh if( i<0 ) return 1; 17842282792aSdrh pParse->aAgg[i].isAgg = 0; 17852282792aSdrh pParse->aAgg[i].pExpr = pExpr; 17862282792aSdrh } 1787aaf88729Sdrh pExpr->iAgg = i; 17882282792aSdrh break; 17892282792aSdrh } 17902282792aSdrh case TK_AGG_FUNCTION: { 17912282792aSdrh aAgg = pParse->aAgg; 17922282792aSdrh for(i=0; i<pParse->nAgg; i++){ 17932282792aSdrh if( !aAgg[i].isAgg ) continue; 17944adee20fSdanielk1977 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ 17952282792aSdrh break; 17962282792aSdrh } 17972282792aSdrh } 17982282792aSdrh if( i>=pParse->nAgg ){ 1799d8123366Sdanielk1977 u8 enc = pParse->db->enc; 18002282792aSdrh i = appendAggInfo(pParse); 18012282792aSdrh if( i<0 ) return 1; 18022282792aSdrh pParse->aAgg[i].isAgg = 1; 18032282792aSdrh pParse->aAgg[i].pExpr = pExpr; 18044adee20fSdanielk1977 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, 18056977fea8Sdrh pExpr->token.z, pExpr->token.n, 1806d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 18072282792aSdrh } 18082282792aSdrh pExpr->iAgg = i; 18092282792aSdrh break; 18102282792aSdrh } 18112282792aSdrh default: { 18122282792aSdrh if( pExpr->pLeft ){ 18134adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft); 18142282792aSdrh } 18152282792aSdrh if( nErr==0 && pExpr->pRight ){ 18164adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight); 18172282792aSdrh } 18182282792aSdrh if( nErr==0 && pExpr->pList ){ 18192282792aSdrh int n = pExpr->pList->nExpr; 18202282792aSdrh int i; 18212282792aSdrh for(i=0; nErr==0 && i<n; i++){ 18224adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); 18232282792aSdrh } 18242282792aSdrh } 18252282792aSdrh break; 18262282792aSdrh } 18272282792aSdrh } 18282282792aSdrh return nErr; 18292282792aSdrh } 18308e0a2f90Sdrh 18318e0a2f90Sdrh /* 1832d02eb1fdSdanielk1977 ** Locate a user function given a name, a number of arguments and a flag 1833d02eb1fdSdanielk1977 ** indicating whether the function prefers UTF-16 over UTF-8. Return a 1834d02eb1fdSdanielk1977 ** pointer to the FuncDef structure that defines that function, or return 1835d02eb1fdSdanielk1977 ** NULL if the function does not exist. 18368e0a2f90Sdrh ** 18370bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef 18388e0a2f90Sdrh ** structure is created and liked into the "db" structure if a 18398e0a2f90Sdrh ** no matching function previously existed. When createFlag is true 18408e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts 18418e0a2f90Sdrh ** any number of arguments will be returned. 18428e0a2f90Sdrh ** 18438e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid 18448e0a2f90Sdrh ** function found is returned. A function is valid if either xFunc 18458e0a2f90Sdrh ** or xStep is non-zero. 1846d02eb1fdSdanielk1977 ** 1847d02eb1fdSdanielk1977 ** If createFlag is false, then a function with the required name and 1848d02eb1fdSdanielk1977 ** number of arguments may be returned even if the eTextRep flag does not 1849d02eb1fdSdanielk1977 ** match that requested. 18508e0a2f90Sdrh */ 18514adee20fSdanielk1977 FuncDef *sqlite3FindFunction( 18529bb575fdSdrh sqlite3 *db, /* An open database */ 18538e0a2f90Sdrh const char *zName, /* Name of the function. Not null-terminated */ 18548e0a2f90Sdrh int nName, /* Number of characters in the name */ 18558e0a2f90Sdrh int nArg, /* Number of arguments. -1 means any number */ 1856d8123366Sdanielk1977 u8 enc, /* Preferred text encoding */ 18578e0a2f90Sdrh int createFlag /* Create new entry if true and does not otherwise exist */ 18588e0a2f90Sdrh ){ 1859d02eb1fdSdanielk1977 FuncDef *p; /* Iterator variable */ 1860d02eb1fdSdanielk1977 FuncDef *pFirst; /* First function with this name */ 1861d02eb1fdSdanielk1977 FuncDef *pBest = 0; /* Best match found so far */ 1862d8123366Sdanielk1977 int bestmatch = 0; 1863d02eb1fdSdanielk1977 1864d8123366Sdanielk1977 1865d8123366Sdanielk1977 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); 1866d02eb1fdSdanielk1977 if( nArg<-1 ) nArg = -1; 1867d02eb1fdSdanielk1977 1868d02eb1fdSdanielk1977 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName); 1869d02eb1fdSdanielk1977 for(p=pFirst; p; p=p->pNext){ 1870d8123366Sdanielk1977 /* During the search for the best function definition, bestmatch is set 1871d8123366Sdanielk1977 ** as follows to indicate the quality of the match with the definition 1872d8123366Sdanielk1977 ** pointed to by pBest: 1873d8123366Sdanielk1977 ** 1874d8123366Sdanielk1977 ** 0: pBest is NULL. No match has been found. 1875d8123366Sdanielk1977 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16 1876d8123366Sdanielk1977 ** encoding is requested, or vice versa. 1877d8123366Sdanielk1977 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is 1878d8123366Sdanielk1977 ** requested, or vice versa. 1879d8123366Sdanielk1977 ** 3: A variable arguments function using the same text encoding. 1880d8123366Sdanielk1977 ** 4: A function with the exact number of arguments requested that 1881d8123366Sdanielk1977 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa. 1882d8123366Sdanielk1977 ** 5: A function with the exact number of arguments requested that 1883d8123366Sdanielk1977 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa. 1884d8123366Sdanielk1977 ** 6: An exact match. 1885d8123366Sdanielk1977 ** 1886d8123366Sdanielk1977 ** A larger value of 'matchqual' indicates a more desirable match. 1887d8123366Sdanielk1977 */ 1888e12c17baSdanielk1977 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){ 1889d8123366Sdanielk1977 int match = 1; /* Quality of this match */ 1890d8123366Sdanielk1977 if( p->nArg==nArg || nArg==-1 ){ 1891d8123366Sdanielk1977 match = 4; 18928e0a2f90Sdrh } 1893d8123366Sdanielk1977 if( enc==p->iPrefEnc ){ 1894d8123366Sdanielk1977 match += 2; 18958e0a2f90Sdrh } 1896d8123366Sdanielk1977 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) || 1897d8123366Sdanielk1977 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){ 1898d8123366Sdanielk1977 match += 1; 1899d02eb1fdSdanielk1977 } 1900d8123366Sdanielk1977 1901d8123366Sdanielk1977 if( match>bestmatch ){ 1902d02eb1fdSdanielk1977 pBest = p; 1903d8123366Sdanielk1977 bestmatch = match; 1904d02eb1fdSdanielk1977 } 1905d02eb1fdSdanielk1977 } 1906d02eb1fdSdanielk1977 } 1907d02eb1fdSdanielk1977 1908d8123366Sdanielk1977 /* If the createFlag parameter is true, and the seach did not reveal an 1909d8123366Sdanielk1977 ** exact match for the name, number of arguments and encoding, then add a 1910d8123366Sdanielk1977 ** new entry to the hash table and return it. 1911d8123366Sdanielk1977 */ 1912d8123366Sdanielk1977 if( createFlag && bestmatch<6 && 1913d02eb1fdSdanielk1977 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){ 1914d02eb1fdSdanielk1977 pBest->nArg = nArg; 1915d02eb1fdSdanielk1977 pBest->pNext = pFirst; 1916d02eb1fdSdanielk1977 pBest->zName = (char*)&pBest[1]; 1917d8123366Sdanielk1977 pBest->iPrefEnc = enc; 1918d02eb1fdSdanielk1977 memcpy(pBest->zName, zName, nName); 1919d02eb1fdSdanielk1977 pBest->zName[nName] = 0; 1920d02eb1fdSdanielk1977 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest); 1921d02eb1fdSdanielk1977 } 1922d02eb1fdSdanielk1977 1923d02eb1fdSdanielk1977 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){ 1924d02eb1fdSdanielk1977 return pBest; 1925d02eb1fdSdanielk1977 } 19268e0a2f90Sdrh return 0; 19278e0a2f90Sdrh } 1928