blob: ea6054e44f4543651c02d2e494ff9ac394517f90 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
drh1ccde152000-06-17 13:12:39 +000012** This file contains routines used for analyzing expressions and
drhb19a2bc2001-09-16 00:13:26 +000013** for generating VDBE code that evaluates expressions in SQLite.
drhcce7d172000-05-31 15:34:51 +000014**
danielk19777977a172004-11-09 12:44:37 +000015** $Id: expr.c,v 1.169 2004/11/09 12:44:38 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000037 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
40 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000041 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000042 }
43 return pExpr->affinity;
44}
45
drh53db1452004-05-20 13:54:53 +000046/*
danielk19770202b292004-06-09 09:55:16 +000047** Return the default collation sequence for the expression pExpr. If
48** there is no default collation type, return 0.
49*/
danielk19777cedc8d2004-06-10 10:50:08 +000050CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
51 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000052 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000053 pColl = pExpr->pColl;
54 if( pExpr->op==TK_AS && !pColl ){
55 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000056 }
57 }
danielk19777cedc8d2004-06-10 10:50:08 +000058 if( sqlite3CheckCollSeq(pParse, pColl) ){
59 pColl = 0;
60 }
61 return pColl;
danielk19770202b292004-06-09 09:55:16 +000062}
63
64/*
drh53db1452004-05-20 13:54:53 +000065** pExpr is the left operand of a comparison operator. aff2 is the
66** type affinity of the right operand. This routine returns the
67** type affinity that should be used for the comparison operator.
68*/
danielk1977e014a832004-05-17 10:48:57 +000069char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000070 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000071 if( aff1 && aff2 ){
72 /* Both sides of the comparison are columns. If one has numeric or
73 ** integer affinity, use that. Otherwise use no affinity.
74 */
75 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
76 return SQLITE_AFF_INTEGER;
77 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
78 return SQLITE_AFF_NUMERIC;
79 }else{
80 return SQLITE_AFF_NONE;
81 }
82 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000083 /* Neither side of the comparison is a column. Compare the
84 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000085 */
drh5f6a87b2004-07-19 00:39:45 +000086 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
87 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000088 }else{
89 /* One side is a column, the other is not. Use the columns affinity. */
90 return (aff1 + aff2);
91 }
92}
93
drh53db1452004-05-20 13:54:53 +000094/*
95** pExpr is a comparison operator. Return the type affinity that should
96** be applied to both operands prior to doing the comparison.
97*/
danielk1977e014a832004-05-17 10:48:57 +000098static char comparisonAffinity(Expr *pExpr){
99 char aff;
100 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
101 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
102 pExpr->op==TK_NE );
103 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000104 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000105 if( pExpr->pRight ){
106 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
107 }
108 else if( pExpr->pSelect ){
109 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
110 }
111 else if( !aff ){
112 aff = SQLITE_AFF_NUMERIC;
113 }
114 return aff;
115}
116
117/*
118** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
119** idx_affinity is the affinity of an indexed column. Return true
120** if the index with affinity idx_affinity may be used to implement
121** the comparison in pExpr.
122*/
123int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
124 char aff = comparisonAffinity(pExpr);
125 return
126 (aff==SQLITE_AFF_NONE) ||
127 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
128 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
129 (aff==idx_affinity);
130}
131
danielk1977a37cdde2004-05-16 11:15:36 +0000132/*
133** Return the P1 value that should be used for a binary comparison
134** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
135** If jumpIfNull is true, then set the low byte of the returned
136** P1 value to tell the opcode to jump if either expression
137** evaluates to NULL.
138*/
danielk1977e014a832004-05-17 10:48:57 +0000139static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000140 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000141 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000142}
143
drha2e00042002-01-22 03:13:42 +0000144/*
danielk19770202b292004-06-09 09:55:16 +0000145** Return a pointer to the collation sequence that should be used by
146** a binary comparison operator comparing pLeft and pRight.
147**
148** If the left hand expression has a collating sequence type, then it is
149** used. Otherwise the collation sequence for the right hand expression
150** is used, or the default (BINARY) if neither expression has a collating
151** type.
152*/
danielk19777cedc8d2004-06-10 10:50:08 +0000153static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
154 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000155 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000156 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000157 }
158 return pColl;
159}
160
161/*
drhbe5c89a2004-07-26 00:31:09 +0000162** Generate code for a comparison operator.
163*/
164static int codeCompare(
165 Parse *pParse, /* The parsing (and code generating) context */
166 Expr *pLeft, /* The left operand */
167 Expr *pRight, /* The right operand */
168 int opcode, /* The comparison opcode */
169 int dest, /* Jump here if true. */
170 int jumpIfNull /* If true, jump if either operand is NULL */
171){
172 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
173 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000174 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000175}
176
177/*
drha76b5df2002-02-23 02:32:10 +0000178** Construct a new expression node and return a pointer to it. Memory
179** for this node is obtained from sqliteMalloc(). The calling function
180** is responsible for making sure the node eventually gets freed.
181*/
danielk19774adee202004-05-08 08:23:19 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +0000186 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000187 return 0;
188 }
189 pNew->op = op;
190 pNew->pLeft = pLeft;
191 pNew->pRight = pRight;
192 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000193 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000194 pNew->span = pNew->token = *pToken;
195 }else if( pLeft && pRight ){
196 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000197 }
drha76b5df2002-02-23 02:32:10 +0000198 return pNew;
199}
200
201/*
drh4e0cff62004-11-05 05:10:28 +0000202** When doing a nested parse, you can include terms in an expression
203** that look like this: #0 #1 #2 ... These terms refer to elements
204** on the stack. "#0" (or just "#") means the top of the stack.
205** "#1" means the next down on the stack. And so forth.
206**
207** This routine is called by the parser to deal with on of those terms.
208** It immediately generates code to store the value in a memory location.
209** The returns an expression that will code to extract the value from
210** that memory location as needed.
211*/
212Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
213 Vdbe *v = pParse->pVdbe;
214 Expr *p;
215 int depth;
216 if( v==0 ) return 0;
217 if( pParse->nested==0 ){
218 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
219 return 0;
220 }
221 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
222 depth = atoi(&pToken->z[1]);
223 p->iTable = pParse->nMem++;
224 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
225 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
226 return p;
227}
228
229/*
drh91bb0ee2004-09-01 03:06:34 +0000230** Join two expressions using an AND operator. If either expression is
231** NULL, then just return the other expression.
232*/
233Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
234 if( pLeft==0 ){
235 return pRight;
236 }else if( pRight==0 ){
237 return pLeft;
238 }else{
239 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
240 }
241}
242
243/*
drh6977fea2002-10-22 23:38:04 +0000244** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000245** text between the two given tokens.
246*/
danielk19774adee202004-05-08 08:23:19 +0000247void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000248 assert( pRight!=0 );
249 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000250 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000251 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000252 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000253 pExpr->span.z = pLeft->z;
254 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000255 }else{
drh6977fea2002-10-22 23:38:04 +0000256 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000257 }
drha76b5df2002-02-23 02:32:10 +0000258 }
259}
260
261/*
262** Construct a new expression node for a function with multiple
263** arguments.
264*/
danielk19774adee202004-05-08 08:23:19 +0000265Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000266 Expr *pNew;
267 pNew = sqliteMalloc( sizeof(Expr) );
268 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000269 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000270 return 0;
271 }
272 pNew->op = TK_FUNCTION;
273 pNew->pList = pList;
274 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000275 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000276 pNew->token = *pToken;
277 }else{
278 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000279 }
drh6977fea2002-10-22 23:38:04 +0000280 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000281 return pNew;
282}
283
284/*
drhfa6bc002004-09-07 16:19:52 +0000285** Assign a variable number to an expression that encodes a wildcard
286** in the original SQL statement.
287**
288** Wildcards consisting of a single "?" are assigned the next sequential
289** variable number.
290**
291** Wildcards of the form "?nnn" are assigned the number "nnn". We make
292** sure "nnn" is not too be to avoid a denial of service attack when
293** the SQL statement comes from an external source.
294**
295** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
296** as the previous instance of the same wildcard. Or if this is the first
297** instance of the wildcard, the next sequenial variable number is
298** assigned.
299*/
300void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
301 Token *pToken;
302 if( pExpr==0 ) return;
303 pToken = &pExpr->token;
304 assert( pToken->n>=1 );
305 assert( pToken->z!=0 );
306 assert( pToken->z[0]!=0 );
307 if( pToken->n==1 ){
308 /* Wildcard of the form "?". Assign the next variable number */
309 pExpr->iTable = ++pParse->nVar;
310 }else if( pToken->z[0]=='?' ){
311 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
312 ** use it as the variable number */
313 int i;
314 pExpr->iTable = i = atoi(&pToken->z[1]);
315 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
316 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
317 SQLITE_MAX_VARIABLE_NUMBER);
318 }
319 if( i>pParse->nVar ){
320 pParse->nVar = i;
321 }
322 }else{
323 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
324 ** number as the prior appearance of the same name, or if the name
325 ** has never appeared before, reuse the same variable number
326 */
327 int i, n;
328 n = pToken->n;
329 for(i=0; i<pParse->nVarExpr; i++){
330 Expr *pE;
331 if( (pE = pParse->apVarExpr[i])!=0
332 && pE->token.n==n
333 && memcmp(pE->token.z, pToken->z, n)==0 ){
334 pExpr->iTable = pE->iTable;
335 break;
336 }
337 }
338 if( i>=pParse->nVarExpr ){
339 pExpr->iTable = ++pParse->nVar;
340 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
341 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
342 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
343 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
344 }
345 if( !sqlite3_malloc_failed ){
346 assert( pParse->apVarExpr!=0 );
347 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
348 }
349 }
350 }
351}
352
353/*
drha2e00042002-01-22 03:13:42 +0000354** Recursively delete an expression tree.
355*/
danielk19774adee202004-05-08 08:23:19 +0000356void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000357 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000358 if( p->span.dyn ) sqliteFree((char*)p->span.z);
359 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000360 sqlite3ExprDelete(p->pLeft);
361 sqlite3ExprDelete(p->pRight);
362 sqlite3ExprListDelete(p->pList);
363 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000364 sqliteFree(p);
365}
366
drha76b5df2002-02-23 02:32:10 +0000367
368/*
drhff78bd22002-02-27 01:47:11 +0000369** The following group of routines make deep copies of expressions,
370** expression lists, ID lists, and select statements. The copies can
371** be deleted (by being passed to their respective ...Delete() routines)
372** without effecting the originals.
373**
danielk19774adee202004-05-08 08:23:19 +0000374** The expression list, ID, and source lists return by sqlite3ExprListDup(),
375** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000376** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000377**
drhad3cab52002-05-24 02:04:32 +0000378** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000379*/
danielk19774adee202004-05-08 08:23:19 +0000380Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000381 Expr *pNew;
382 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000383 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000384 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000385 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000386 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000387 pNew->token.z = sqliteStrDup(p->token.z);
388 pNew->token.dyn = 1;
389 }else{
drh4efc4752004-01-16 15:55:37 +0000390 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000391 }
drh6977fea2002-10-22 23:38:04 +0000392 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000393 pNew->pLeft = sqlite3ExprDup(p->pLeft);
394 pNew->pRight = sqlite3ExprDup(p->pRight);
395 pNew->pList = sqlite3ExprListDup(p->pList);
396 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000397 return pNew;
398}
danielk19774adee202004-05-08 08:23:19 +0000399void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000400 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000401 if( pFrom->z ){
402 pTo->n = pFrom->n;
403 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
404 pTo->dyn = 1;
405 }else{
drh4b59ab52002-08-24 18:24:51 +0000406 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000407 }
408}
danielk19774adee202004-05-08 08:23:19 +0000409ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000410 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000411 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000412 int i;
413 if( p==0 ) return 0;
414 pNew = sqliteMalloc( sizeof(*pNew) );
415 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000416 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000417 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000418 if( pItem==0 ){
419 sqliteFree(pNew);
420 return 0;
421 }
drh145716b2004-09-24 12:24:06 +0000422 pOldItem = p->a;
423 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000424 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000425 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000426 if( pOldExpr->span.z!=0 && pNewExpr ){
427 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000428 ** expression list. The logic in SELECT processing that determines
429 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000430 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000431 }
drh1f3e9052002-10-31 00:09:39 +0000432 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000433 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000434 pItem->zName = sqliteStrDup(pOldItem->zName);
435 pItem->sortOrder = pOldItem->sortOrder;
436 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000437 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000438 }
439 return pNew;
440}
danielk19774adee202004-05-08 08:23:19 +0000441SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000442 SrcList *pNew;
443 int i;
drh113088e2003-03-20 01:16:58 +0000444 int nByte;
drhad3cab52002-05-24 02:04:32 +0000445 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000446 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000447 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000448 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000449 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000450 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000451 struct SrcList_item *pNewItem = &pNew->a[i];
452 struct SrcList_item *pOldItem = &p->a[i];
453 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
454 pNewItem->zName = sqliteStrDup(pOldItem->zName);
455 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
456 pNewItem->jointype = pOldItem->jointype;
457 pNewItem->iCursor = pOldItem->iCursor;
458 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000459 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
460 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
461 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000462 }
463 return pNew;
464}
danielk19774adee202004-05-08 08:23:19 +0000465IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000466 IdList *pNew;
467 int i;
468 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000469 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000470 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000471 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000472 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000473 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000474 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000475 struct IdList_item *pNewItem = &pNew->a[i];
476 struct IdList_item *pOldItem = &p->a[i];
477 pNewItem->zName = sqliteStrDup(pOldItem->zName);
478 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000479 }
480 return pNew;
481}
danielk19774adee202004-05-08 08:23:19 +0000482Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000483 Select *pNew;
484 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000485 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000486 if( pNew==0 ) return 0;
487 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000488 pNew->pEList = sqlite3ExprListDup(p->pEList);
489 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
490 pNew->pWhere = sqlite3ExprDup(p->pWhere);
491 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
492 pNew->pHaving = sqlite3ExprDup(p->pHaving);
493 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000494 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000495 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000496 pNew->nLimit = p->nLimit;
497 pNew->nOffset = p->nOffset;
498 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000499 pNew->iLimit = -1;
500 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000501 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000502 return pNew;
503}
504
505
506/*
drha76b5df2002-02-23 02:32:10 +0000507** Add a new element to the end of an expression list. If pList is
508** initially NULL, then create a new expression list.
509*/
danielk19774adee202004-05-08 08:23:19 +0000510ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000511 if( pList==0 ){
512 pList = sqliteMalloc( sizeof(ExprList) );
513 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000514 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000515 return 0;
516 }
drh4efc4752004-01-16 15:55:37 +0000517 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000518 }
drh4305d102003-07-30 12:34:12 +0000519 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000520 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000521 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
522 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000523 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000524 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000525 return pList;
526 }
drha76b5df2002-02-23 02:32:10 +0000527 }
drh4efc4752004-01-16 15:55:37 +0000528 assert( pList->a!=0 );
529 if( pExpr || pName ){
530 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
531 memset(pItem, 0, sizeof(*pItem));
532 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000533 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000534 }
535 return pList;
536}
537
538/*
539** Delete an entire expression list.
540*/
danielk19774adee202004-05-08 08:23:19 +0000541void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000542 int i;
drhbe5c89a2004-07-26 00:31:09 +0000543 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000544 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000545 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
546 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000547 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
548 sqlite3ExprDelete(pItem->pExpr);
549 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000550 }
551 sqliteFree(pList->a);
552 sqliteFree(pList);
553}
554
555/*
drhfef52082000-06-06 01:50:43 +0000556** Walk an expression tree. Return 1 if the expression is constant
557** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000558**
559** For the purposes of this function, a double-quoted string (ex: "abc")
560** is considered a variable but a single-quoted string (ex: 'abc') is
561** a constant.
drhfef52082000-06-06 01:50:43 +0000562*/
danielk19774adee202004-05-08 08:23:19 +0000563int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000564 switch( p->op ){
565 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000566 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000567 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000568 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000569 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000570 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000571 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000572 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000573 case TK_INTEGER:
574 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000575 case TK_VARIABLE:
danielk19777977a172004-11-09 12:44:37 +0000576 case TK_CTIME:
577 case TK_CTIMESTAMP:
578 case TK_CDATE:
drh92086432002-01-22 14:11:29 +0000579 return 1;
drhfef52082000-06-06 01:50:43 +0000580 default: {
danielk19774adee202004-05-08 08:23:19 +0000581 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
582 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000583 if( p->pList ){
584 int i;
585 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000586 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000587 }
588 }
drh92086432002-01-22 14:11:29 +0000589 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000590 }
591 }
drh92086432002-01-22 14:11:29 +0000592 return 0;
drhfef52082000-06-06 01:50:43 +0000593}
594
595/*
drh202b2df2004-01-06 01:13:46 +0000596** If the given expression codes a constant integer that is small enough
597** to fit in a 32-bit integer, return 1 and put the value of the integer
598** in *pValue. If the expression is not an integer or if it is too big
599** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000600*/
danielk19774adee202004-05-08 08:23:19 +0000601int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000602 switch( p->op ){
603 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000604 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000605 return 1;
606 }
607 break;
drhe4de1fe2002-06-02 16:09:01 +0000608 }
609 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000610 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000611 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000612 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000613 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000614 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000615 return 1;
616 }
617 break;
618 }
drh4b59ab52002-08-24 18:24:51 +0000619 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000620 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000621 }
drhe4de1fe2002-06-02 16:09:01 +0000622 case TK_UMINUS: {
623 int v;
danielk19774adee202004-05-08 08:23:19 +0000624 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000625 *pValue = -v;
626 return 1;
627 }
628 break;
629 }
630 default: break;
631 }
632 return 0;
633}
634
635/*
drhc4a3c772001-04-04 11:48:57 +0000636** Return TRUE if the given string is a row-id column name.
637*/
danielk19774adee202004-05-08 08:23:19 +0000638int sqlite3IsRowid(const char *z){
639 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
640 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
641 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000642 return 0;
643}
644
645/*
drh8141f612004-01-25 22:44:58 +0000646** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
647** that name in the set of source tables in pSrcList and make the pExpr
648** expression node refer back to that source column. The following changes
649** are made to pExpr:
650**
651** pExpr->iDb Set the index in db->aDb[] of the database holding
652** the table.
653** pExpr->iTable Set to the cursor number for the table obtained
654** from pSrcList.
655** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000656** pExpr->op Set to TK_COLUMN.
657** pExpr->pLeft Any expression this points to is deleted
658** pExpr->pRight Any expression this points to is deleted.
659**
660** The pDbToken is the name of the database (the "X"). This value may be
661** NULL meaning that name is of the form Y.Z or Z. Any available database
662** can be used. The pTableToken is the name of the table (the "Y"). This
663** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
664** means that the form of the name is Z and that columns from any table
665** can be used.
666**
667** If the name cannot be resolved unambiguously, leave an error message
668** in pParse and return non-zero. Return zero on success.
669*/
670static int lookupName(
671 Parse *pParse, /* The parsing context */
672 Token *pDbToken, /* Name of the database containing table, or NULL */
673 Token *pTableToken, /* Name of table containing column, or NULL */
674 Token *pColumnToken, /* Name of the column. */
675 SrcList *pSrcList, /* List of tables used to resolve column names */
676 ExprList *pEList, /* List of expressions used to resolve "AS" */
677 Expr *pExpr /* Make this EXPR node point to the selected column */
678){
679 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
680 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
681 char *zCol = 0; /* Name of the column. The "Z" */
682 int i, j; /* Loop counters */
683 int cnt = 0; /* Number of matching column names */
684 int cntTab = 0; /* Number of matching table names */
drh9bb575f2004-09-06 17:24:11 +0000685 sqlite3 *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000686
687 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000688 zDb = sqlite3NameFromToken(pDbToken);
689 zTab = sqlite3NameFromToken(pTableToken);
690 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000691 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000692 return 1; /* Leak memory (zDb and zTab) if malloc fails */
693 }
694 assert( zTab==0 || pEList==0 );
695
696 pExpr->iTable = -1;
697 for(i=0; i<pSrcList->nSrc; i++){
698 struct SrcList_item *pItem = &pSrcList->a[i];
699 Table *pTab = pItem->pTab;
700 Column *pCol;
701
702 if( pTab==0 ) continue;
703 assert( pTab->nCol>0 );
704 if( zTab ){
705 if( pItem->zAlias ){
706 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000707 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000708 }else{
709 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000710 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
711 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000712 continue;
713 }
714 }
715 }
716 if( 0==(cntTab++) ){
717 pExpr->iTable = pItem->iCursor;
718 pExpr->iDb = pTab->iDb;
719 }
720 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000721 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000722 cnt++;
723 pExpr->iTable = pItem->iCursor;
724 pExpr->iDb = pTab->iDb;
725 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
726 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000727 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000728 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000729 break;
730 }
731 }
732 }
733
drhb7f91642004-10-31 02:22:47 +0000734#ifndef SQLITE_OMIT_TRIGGER
drh8141f612004-01-25 22:44:58 +0000735 /* If we have not already resolved the name, then maybe
736 ** it is a new.* or old.* trigger argument reference
737 */
738 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
739 TriggerStack *pTriggerStack = pParse->trigStack;
740 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000741 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000742 pExpr->iTable = pTriggerStack->newIdx;
743 assert( pTriggerStack->pTab );
744 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000745 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000746 pExpr->iTable = pTriggerStack->oldIdx;
747 assert( pTriggerStack->pTab );
748 pTab = pTriggerStack->pTab;
749 }
750
751 if( pTab ){
752 int j;
753 Column *pCol = pTab->aCol;
754
755 pExpr->iDb = pTab->iDb;
756 cntTab++;
757 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000758 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000759 cnt++;
760 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000761 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000762 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000763 break;
764 }
765 }
766 }
767 }
drhb7f91642004-10-31 02:22:47 +0000768#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000769
770 /*
771 ** Perhaps the name is a reference to the ROWID
772 */
danielk19774adee202004-05-08 08:23:19 +0000773 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000774 cnt = 1;
775 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000776 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000777 }
778
779 /*
780 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
781 ** might refer to an result-set alias. This happens, for example, when
782 ** we are resolving names in the WHERE clause of the following command:
783 **
784 ** SELECT a+b AS x FROM table WHERE x<10;
785 **
786 ** In cases like this, replace pExpr with a copy of the expression that
787 ** forms the result set entry ("a+b" in the example) and return immediately.
788 ** Note that the expression in the result set should have already been
789 ** resolved by the time the WHERE clause is resolved.
790 */
791 if( cnt==0 && pEList!=0 ){
792 for(j=0; j<pEList->nExpr; j++){
793 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000794 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000795 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
796 pExpr->op = TK_AS;
797 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000798 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000799 sqliteFree(zCol);
800 assert( zTab==0 && zDb==0 );
801 return 0;
802 }
803 }
804 }
805
806 /*
807 ** If X and Y are NULL (in other words if only the column name Z is
808 ** supplied) and the value of Z is enclosed in double-quotes, then
809 ** Z is a string literal if it doesn't match any column names. In that
810 ** case, we need to return right away and not make any changes to
811 ** pExpr.
812 */
813 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
814 sqliteFree(zCol);
815 return 0;
816 }
817
818 /*
819 ** cnt==0 means there was not match. cnt>1 means there were two or
820 ** more matches. Either way, we have an error.
821 */
822 if( cnt!=1 ){
823 char *z = 0;
824 char *zErr;
825 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
826 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000827 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000828 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000829 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000830 }else{
831 z = sqliteStrDup(zCol);
832 }
danielk19774adee202004-05-08 08:23:19 +0000833 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000834 sqliteFree(z);
835 }
836
837 /* Clean up and return
838 */
839 sqliteFree(zDb);
840 sqliteFree(zTab);
841 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000842 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000843 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000844 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000845 pExpr->pRight = 0;
846 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000847 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000848 return cnt!=1;
849}
850
851/*
drhcce7d172000-05-31 15:34:51 +0000852** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000853** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000854** index to the table in the table list and a column offset. The
855** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
856** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000857** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000858** VDBE cursor number for a cursor that is pointing into the referenced
859** table. The Expr.iColumn value is changed to the index of the column
860** of the referenced table. The Expr.iColumn value for the special
861** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
862** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000863**
drhfef52082000-06-06 01:50:43 +0000864** We also check for instances of the IN operator. IN comes in two
865** forms:
866**
867** expr IN (exprlist)
868** and
869** expr IN (SELECT ...)
870**
871** The first form is handled by creating a set holding the list
872** of allowed values. The second form causes the SELECT to generate
873** a temporary table.
874**
875** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000876** If it finds any, it generates code to write the value of that select
877** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000878**
drh967e8b72000-06-21 13:59:10 +0000879** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000880** the number of errors seen and leaves an error message on pParse->zErrMsg.
881*/
danielk19774adee202004-05-08 08:23:19 +0000882int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000883 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000884 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000885 ExprList *pEList, /* List of expressions used to resolve "AS" */
886 Expr *pExpr /* The expression to be analyzed. */
887){
drh6a3ea0e2003-05-02 14:32:12 +0000888 int i;
889
drh8141f612004-01-25 22:44:58 +0000890 if( pExpr==0 || pSrcList==0 ) return 0;
891 for(i=0; i<pSrcList->nSrc; i++){
892 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000893 }
drhcce7d172000-05-31 15:34:51 +0000894 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000895 /* Double-quoted strings (ex: "abc") are used as identifiers if
896 ** possible. Otherwise they remain as strings. Single-quoted
897 ** strings (ex: 'abc') are always string literals.
898 */
899 case TK_STRING: {
900 if( pExpr->token.z[0]=='\'' ) break;
901 /* Fall thru into the TK_ID case if this is a double-quoted string */
902 }
drh8141f612004-01-25 22:44:58 +0000903 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000904 */
drhcce7d172000-05-31 15:34:51 +0000905 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000906 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000907 return 1;
drhed6c8672003-01-12 18:02:16 +0000908 }
drhcce7d172000-05-31 15:34:51 +0000909 break;
910 }
911
drhd24cc422003-03-27 12:51:24 +0000912 /* A table name and column name: ID.ID
913 ** Or a database, table and column: ID.ID.ID
914 */
drhcce7d172000-05-31 15:34:51 +0000915 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000916 Token *pColumn;
917 Token *pTable;
918 Token *pDb;
919 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000920
drhcce7d172000-05-31 15:34:51 +0000921 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000922 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000923 pDb = 0;
924 pTable = &pExpr->pLeft->token;
925 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000926 }else{
drh8141f612004-01-25 22:44:58 +0000927 assert( pRight->op==TK_DOT );
928 pDb = &pExpr->pLeft->token;
929 pTable = &pRight->pLeft->token;
930 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000931 }
drh8141f612004-01-25 22:44:58 +0000932 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000933 return 1;
934 }
drhcce7d172000-05-31 15:34:51 +0000935 break;
936 }
937
drhfef52082000-06-06 01:50:43 +0000938 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000939 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000940 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000941 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000942 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000943
drhfef52082000-06-06 01:50:43 +0000944 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000945 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000946 return 1;
947 }
danielk1977bf3b7212004-05-18 10:06:24 +0000948 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000949
950 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
951 ** expression it is handled the same way. A temporary table is
952 ** filled with single-field index keys representing the results
953 ** from the SELECT or the <exprlist>.
954 **
955 ** If the 'x' expression is a column value, or the SELECT...
956 ** statement returns a column value, then the affinity of that
957 ** column is used to build the index keys. If both 'x' and the
958 ** SELECT... statement are columns, then numeric affinity is used
959 ** if either column has NUMERIC or INTEGER affinity. If neither
960 ** 'x' nor the SELECT... statement are columns, then numeric affinity
961 ** is used.
962 */
963 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000964 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000965 memset(&keyInfo, 0, sizeof(keyInfo));
966 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000967 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000968
drhfef52082000-06-06 01:50:43 +0000969 if( pExpr->pSelect ){
970 /* Case 1: expr IN (SELECT ...)
971 **
danielk1977e014a832004-05-17 10:48:57 +0000972 ** Generate code to write the results of the select into the temporary
973 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000974 */
danielk1977e014a832004-05-17 10:48:57 +0000975 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000976 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000978 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000979 pEList = pExpr->pSelect->pEList;
980 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000981 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000982 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000983 }
drhfef52082000-06-06 01:50:43 +0000984 }else if( pExpr->pList ){
985 /* Case 2: expr IN (exprlist)
986 **
danielk1977e014a832004-05-17 10:48:57 +0000987 ** For each expression, build an index key from the evaluation and
988 ** store it in the temporary table. If <expr> is a column, then use
989 ** that columns affinity when building index keys. If <expr> is not
990 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000991 */
danielk1977e014a832004-05-17 10:48:57 +0000992 int i;
danielk1977e014a832004-05-17 10:48:57 +0000993 if( !affinity ){
994 affinity = SQLITE_AFF_NUMERIC;
995 }
danielk19770202b292004-06-09 09:55:16 +0000996 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000997
998 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000999 for(i=0; i<pExpr->pList->nExpr; i++){
1000 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001001
1002 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001003 if( !sqlite3ExprIsConstant(pE2) ){
1004 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001005 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +00001006 return 1;
1007 }
danielk19774adee202004-05-08 08:23:19 +00001008 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +00001009 return 1;
1010 }
danielk1977e014a832004-05-17 10:48:57 +00001011
1012 /* Evaluate the expression and insert it into the temp table */
1013 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001014 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001015 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001016 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001017 }
1018 }
danielk19770202b292004-06-09 09:55:16 +00001019 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
1020
drhcfab11b2000-06-06 03:31:22 +00001021 break;
drhfef52082000-06-06 01:50:43 +00001022 }
1023
drh19a775c2000-06-05 18:54:46 +00001024 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001025 /* This has to be a scalar SELECT. Generate code to put the
1026 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001027 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001028 */
drh967e8b72000-06-21 13:59:10 +00001029 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +00001030 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +00001031 return 1;
1032 }
1033 break;
1034 }
1035
drhcce7d172000-05-31 15:34:51 +00001036 /* For all else, just recursively walk the tree */
1037 default: {
drh4794b982000-06-06 13:54:14 +00001038 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +00001039 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +00001040 return 1;
1041 }
1042 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +00001043 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +00001044 return 1;
1045 }
1046 if( pExpr->pList ){
1047 int i;
1048 ExprList *pList = pExpr->pList;
1049 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001050 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001051 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +00001052 return 1;
1053 }
1054 }
1055 }
1056 }
1057 }
1058 return 0;
1059}
1060
drhcce7d172000-05-31 15:34:51 +00001061/*
drh4b59ab52002-08-24 18:24:51 +00001062** pExpr is a node that defines a function of some kind. It might
1063** be a syntactic function like "count(x)" or it might be a function
1064** that implements an operator, like "a LIKE b".
1065**
1066** This routine makes *pzName point to the name of the function and
1067** *pnName hold the number of characters in the function name.
1068*/
1069static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1070 switch( pExpr->op ){
1071 case TK_FUNCTION: {
1072 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +00001073 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +00001074 break;
1075 }
1076 case TK_LIKE: {
1077 *pzName = "like";
1078 *pnName = 4;
1079 break;
1080 }
1081 case TK_GLOB: {
1082 *pzName = "glob";
1083 *pnName = 4;
1084 break;
1085 }
danielk19777977a172004-11-09 12:44:37 +00001086 case TK_CTIME: {
1087 *pzName = "current_time";
1088 *pnName = 12;
1089 break;
1090 }
1091 case TK_CDATE: {
1092 *pzName = "current_date";
1093 *pnName = 12;
1094 break;
1095 }
1096 case TK_CTIMESTAMP: {
1097 *pzName = "current_timestamp";
1098 *pnName = 17;
1099 break;
1100 }
drh4b59ab52002-08-24 18:24:51 +00001101 default: {
1102 *pzName = "can't happen";
1103 *pnName = 12;
1104 break;
1105 }
1106 }
1107}
1108
1109/*
drhcce7d172000-05-31 15:34:51 +00001110** Error check the functions in an expression. Make sure all
1111** function names are recognized and all functions have the correct
1112** number of arguments. Leave an error message in pParse->zErrMsg
1113** if anything is amiss. Return the number of errors.
1114**
1115** if pIsAgg is not null and this expression is an aggregate function
1116** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1117*/
danielk19774adee202004-05-08 08:23:19 +00001118int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001119 int nErr = 0;
1120 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001121 switch( pExpr->op ){
danielk19777977a172004-11-09 12:44:37 +00001122 case TK_CTIME:
1123 case TK_CTIMESTAMP:
1124 case TK_CDATE:
1125 /* Note: The above three were a seperate case in sqlmoto. Reason? */
drh4b59ab52002-08-24 18:24:51 +00001126 case TK_GLOB:
1127 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001128 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001129 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1130 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001131 int wrong_num_args = 0; /* True if wrong number of arguments */
1132 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001133 int i;
drh4b59ab52002-08-24 18:24:51 +00001134 int nId; /* Number of characters in function name */
1135 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001136 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001137 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001138
drh4b59ab52002-08-24 18:24:51 +00001139 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001140 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001141 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001142 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001143 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001144 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001145 }else{
1146 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001147 }
drh0bce8352002-02-28 00:41:10 +00001148 }else{
1149 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001150 }
drh8e0a2f92002-02-23 23:45:45 +00001151 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001152 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001153 nErr++;
1154 is_agg = 0;
1155 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001156 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001157 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001158 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001159 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001160 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001161 nErr++;
drhcce7d172000-05-31 15:34:51 +00001162 }
drhf7a9e1a2004-02-22 18:40:56 +00001163 if( is_agg ){
1164 pExpr->op = TK_AGG_FUNCTION;
1165 if( pIsAgg ) *pIsAgg = 1;
1166 }
drhcce7d172000-05-31 15:34:51 +00001167 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001168 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001169 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001170 }
danielk19770202b292004-06-09 09:55:16 +00001171 /* FIX ME: Compute pExpr->affinity based on the expected return
1172 ** type of the function
1173 */
drhcce7d172000-05-31 15:34:51 +00001174 }
1175 default: {
1176 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001177 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001178 }
1179 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001180 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001181 }
drhfef52082000-06-06 01:50:43 +00001182 if( nErr==0 && pExpr->pList ){
1183 int n = pExpr->pList->nExpr;
1184 int i;
1185 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001186 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001187 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001188 }
1189 }
drhcce7d172000-05-31 15:34:51 +00001190 break;
1191 }
1192 }
1193 return nErr;
1194}
1195
1196/*
drh290c1942004-08-21 17:54:45 +00001197** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1198**
1199** This routine is provided as a convenience since it is very common
1200** to call ResolveIds() and Check() back to back.
1201*/
1202int sqlite3ExprResolveAndCheck(
1203 Parse *pParse, /* The parser context */
1204 SrcList *pSrcList, /* List of tables used to resolve column names */
1205 ExprList *pEList, /* List of expressions used to resolve "AS" */
1206 Expr *pExpr, /* The expression to be analyzed. */
1207 int allowAgg, /* True to allow aggregate expressions */
1208 int *pIsAgg /* Set to TRUE if aggregates are found */
1209){
1210 if( pExpr==0 ) return 0;
1211 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1212 return 1;
1213 }
1214 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1215}
1216
1217/*
drhfec19aa2004-05-19 20:41:03 +00001218** Generate an instruction that will put the integer describe by
1219** text z[0..n-1] on the stack.
1220*/
1221static void codeInteger(Vdbe *v, const char *z, int n){
1222 int i;
drh6fec0762004-05-30 01:38:43 +00001223 if( sqlite3GetInt32(z, &i) ){
1224 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1225 }else if( sqlite3FitsIn64Bits(z) ){
1226 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001227 }else{
1228 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1229 }
1230}
1231
1232/*
drhcce7d172000-05-31 15:34:51 +00001233** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001234** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001235**
1236** This code depends on the fact that certain token values (ex: TK_EQ)
1237** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1238** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1239** the make process cause these values to align. Assert()s in the code
1240** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001241*/
danielk19774adee202004-05-08 08:23:19 +00001242void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001243 Vdbe *v = pParse->pVdbe;
1244 int op;
danielk19777977a172004-11-09 12:44:37 +00001245 if( v==0 ) return;
1246 if( pExpr==0 ){
1247 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1248 return;
1249 }
drhf2bc0132004-10-04 13:19:23 +00001250 op = pExpr->op;
1251 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001252 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001253 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001254 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001255 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001256 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001257#ifndef NDEBUG
1258 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1259 VdbeComment((v, "# %T", &pExpr->span));
1260 }
1261#endif
drhc4a3c772001-04-04 11:48:57 +00001262 }else{
danielk19774adee202004-05-08 08:23:19 +00001263 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001264 }
drhcce7d172000-05-31 15:34:51 +00001265 break;
1266 }
1267 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001268 codeInteger(v, pExpr->token.z, pExpr->token.n);
1269 break;
1270 }
1271 case TK_FLOAT:
1272 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001273 assert( TK_FLOAT==OP_Real );
1274 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001275 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001276 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001277 break;
1278 }
danielk1977c572ef72004-05-27 09:28:41 +00001279 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001280 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001281 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1282 sqlite3VdbeDequoteP3(v, -1);
1283 break;
1284 }
drhcce7d172000-05-31 15:34:51 +00001285 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001286 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001287 break;
1288 }
drh50457892003-09-06 01:10:47 +00001289 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001290 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001291 if( pExpr->token.n>1 ){
1292 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1293 }
drh50457892003-09-06 01:10:47 +00001294 break;
1295 }
drh4e0cff62004-11-05 05:10:28 +00001296 case TK_REGISTER: {
1297 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1298 break;
1299 }
drhc9b84a12002-06-20 11:36:48 +00001300 case TK_LT:
1301 case TK_LE:
1302 case TK_GT:
1303 case TK_GE:
1304 case TK_NE:
1305 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001306 assert( TK_LT==OP_Lt );
1307 assert( TK_LE==OP_Le );
1308 assert( TK_GT==OP_Gt );
1309 assert( TK_GE==OP_Ge );
1310 assert( TK_EQ==OP_Eq );
1311 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001312 sqlite3ExprCode(pParse, pExpr->pLeft);
1313 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001314 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001315 break;
drhc9b84a12002-06-20 11:36:48 +00001316 }
drhcce7d172000-05-31 15:34:51 +00001317 case TK_AND:
1318 case TK_OR:
1319 case TK_PLUS:
1320 case TK_STAR:
1321 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001322 case TK_REM:
1323 case TK_BITAND:
1324 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001325 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001326 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001327 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001328 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001329 assert( TK_AND==OP_And );
1330 assert( TK_OR==OP_Or );
1331 assert( TK_PLUS==OP_Add );
1332 assert( TK_MINUS==OP_Subtract );
1333 assert( TK_REM==OP_Remainder );
1334 assert( TK_BITAND==OP_BitAnd );
1335 assert( TK_BITOR==OP_BitOr );
1336 assert( TK_SLASH==OP_Divide );
1337 assert( TK_LSHIFT==OP_ShiftLeft );
1338 assert( TK_RSHIFT==OP_ShiftRight );
1339 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001340 sqlite3ExprCode(pParse, pExpr->pLeft);
1341 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001342 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001343 break;
1344 }
drhcce7d172000-05-31 15:34:51 +00001345 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001346 Expr *pLeft = pExpr->pLeft;
1347 assert( pLeft );
1348 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1349 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001350 char *z = sqliteMalloc( p->n + 2 );
1351 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001352 if( pLeft->op==TK_FLOAT ){
1353 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001354 }else{
drhfec19aa2004-05-19 20:41:03 +00001355 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001356 }
drh6e142f52000-06-08 13:36:40 +00001357 sqliteFree(z);
1358 break;
1359 }
drh1ccde152000-06-17 13:12:39 +00001360 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001361 }
drhbf4133c2001-10-13 02:59:08 +00001362 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001363 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001364 assert( TK_BITNOT==OP_BitNot );
1365 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001366 sqlite3ExprCode(pParse, pExpr->pLeft);
1367 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001368 break;
1369 }
1370 case TK_ISNULL:
1371 case TK_NOTNULL: {
1372 int dest;
drhf2bc0132004-10-04 13:19:23 +00001373 assert( TK_ISNULL==OP_IsNull );
1374 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001375 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1376 sqlite3ExprCode(pParse, pExpr->pLeft);
1377 dest = sqlite3VdbeCurrentAddr(v) + 2;
1378 sqlite3VdbeAddOp(v, op, 1, dest);
1379 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001380 break;
drhcce7d172000-05-31 15:34:51 +00001381 }
drh22827922000-06-06 17:27:05 +00001382 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001383 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001384 break;
1385 }
danielk19777977a172004-11-09 12:44:37 +00001386 case TK_CDATE:
1387 case TK_CTIME:
1388 case TK_CTIMESTAMP:
drh4b59ab52002-08-24 18:24:51 +00001389 case TK_GLOB:
1390 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001391 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001392 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001393 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001394 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001395 int nId;
1396 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001397 int p2 = 0;
1398 int i;
danielk1977d8123362004-06-12 09:25:12 +00001399 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001400 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001401 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001402 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001403 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001404 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001405 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001406 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1407 p2 |= (1<<i);
1408 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001409 if( pDef->needCollSeq && !pColl ){
1410 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1411 }
1412 }
1413 if( pDef->needCollSeq ){
1414 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001415 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001416 }
1417 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001418 break;
1419 }
drh19a775c2000-06-05 18:54:46 +00001420 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001421 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001422 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001423 break;
1424 }
drhfef52082000-06-06 01:50:43 +00001425 case TK_IN: {
1426 int addr;
drh94a11212004-09-25 13:12:14 +00001427 char affinity;
danielk1977e014a832004-05-17 10:48:57 +00001428
1429 /* Figure out the affinity to use to create a key from the results
1430 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001431 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001432 */
drh94a11212004-09-25 13:12:14 +00001433 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001434
danielk19774adee202004-05-08 08:23:19 +00001435 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001436
1437 /* Code the <expr> from "<expr> IN (...)". The temporary table
1438 ** pExpr->iTable contains the values that make up the (...) set.
1439 */
danielk19774adee202004-05-08 08:23:19 +00001440 sqlite3ExprCode(pParse, pExpr->pLeft);
1441 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001442 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001443 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001444 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001445 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001446 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001447 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1448 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1449
drhfef52082000-06-06 01:50:43 +00001450 break;
1451 }
1452 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001453 Expr *pLeft = pExpr->pLeft;
1454 struct ExprList_item *pLItem = pExpr->pList->a;
1455 Expr *pRight = pLItem->pExpr;
1456 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001457 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001458 sqlite3ExprCode(pParse, pRight);
1459 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001460 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001461 pLItem++;
1462 pRight = pLItem->pExpr;
1463 sqlite3ExprCode(pParse, pRight);
1464 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001465 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001466 break;
1467 }
drh51e9a442004-01-16 16:42:53 +00001468 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001469 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001470 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001471 break;
1472 }
drh17a7f8d2002-03-24 13:13:27 +00001473 case TK_CASE: {
1474 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001475 int jumpInst;
1476 int addr;
1477 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001478 int i;
drhbe5c89a2004-07-26 00:31:09 +00001479 ExprList *pEList;
1480 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001481
1482 assert(pExpr->pList);
1483 assert((pExpr->pList->nExpr % 2) == 0);
1484 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001485 pEList = pExpr->pList;
1486 aListelem = pEList->a;
1487 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001488 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001489 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001490 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001491 }
drhf5905aa2002-05-26 20:54:33 +00001492 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001493 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001494 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001495 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001496 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1497 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001498 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001499 }else{
danielk19774adee202004-05-08 08:23:19 +00001500 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001501 }
drhbe5c89a2004-07-26 00:31:09 +00001502 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001503 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1504 addr = sqlite3VdbeCurrentAddr(v);
1505 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001506 }
drhf570f012002-05-31 15:51:25 +00001507 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001508 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001509 }
drh17a7f8d2002-03-24 13:13:27 +00001510 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001511 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001512 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001513 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001514 }
danielk19774adee202004-05-08 08:23:19 +00001515 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001516 break;
1517 }
1518 case TK_RAISE: {
1519 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001520 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001521 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001522 return;
1523 }
drhad6d9462004-09-19 02:15:24 +00001524 if( pExpr->iColumn!=OE_Ignore ){
1525 assert( pExpr->iColumn==OE_Rollback ||
1526 pExpr->iColumn == OE_Abort ||
1527 pExpr->iColumn == OE_Fail );
1528 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1529 pExpr->token.z, pExpr->token.n);
1530 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001531 } else {
drhad6d9462004-09-19 02:15:24 +00001532 assert( pExpr->iColumn == OE_Ignore );
1533 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1534 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1535 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001536 }
drh17a7f8d2002-03-24 13:13:27 +00001537 }
1538 break;
drhcce7d172000-05-31 15:34:51 +00001539 }
drhcce7d172000-05-31 15:34:51 +00001540}
1541
1542/*
drh268380c2004-02-25 13:47:31 +00001543** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001544** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001545**
1546** Return the number of elements pushed onto the stack.
1547*/
danielk19774adee202004-05-08 08:23:19 +00001548int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001549 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001550 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001551){
1552 struct ExprList_item *pItem;
1553 int i, n;
1554 Vdbe *v;
1555 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001556 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001557 n = pList->nExpr;
1558 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001559 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001560 }
drhf9b596e2004-05-26 16:54:42 +00001561 return n;
drh268380c2004-02-25 13:47:31 +00001562}
1563
1564/*
drhcce7d172000-05-31 15:34:51 +00001565** Generate code for a boolean expression such that a jump is made
1566** to the label "dest" if the expression is true but execution
1567** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001568**
1569** If the expression evaluates to NULL (neither true nor false), then
1570** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001571**
1572** This code depends on the fact that certain token values (ex: TK_EQ)
1573** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1574** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1575** the make process cause these values to align. Assert()s in the code
1576** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001577*/
danielk19774adee202004-05-08 08:23:19 +00001578void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001579 Vdbe *v = pParse->pVdbe;
1580 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001581 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001582 op = pExpr->op;
1583 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001584 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001585 int d2 = sqlite3VdbeMakeLabel(v);
1586 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1587 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1588 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001589 break;
1590 }
1591 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001592 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1593 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001594 break;
1595 }
1596 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001597 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001598 break;
1599 }
1600 case TK_LT:
1601 case TK_LE:
1602 case TK_GT:
1603 case TK_GE:
1604 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001605 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001606 assert( TK_LT==OP_Lt );
1607 assert( TK_LE==OP_Le );
1608 assert( TK_GT==OP_Gt );
1609 assert( TK_GE==OP_Ge );
1610 assert( TK_EQ==OP_Eq );
1611 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001612 sqlite3ExprCode(pParse, pExpr->pLeft);
1613 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001614 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001615 break;
1616 }
1617 case TK_ISNULL:
1618 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001619 assert( TK_ISNULL==OP_IsNull );
1620 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001621 sqlite3ExprCode(pParse, pExpr->pLeft);
1622 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001623 break;
1624 }
drhfef52082000-06-06 01:50:43 +00001625 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001626 /* The expression "x BETWEEN y AND z" is implemented as:
1627 **
1628 ** 1 IF (x < y) GOTO 3
1629 ** 2 IF (x <= z) GOTO <dest>
1630 ** 3 ...
1631 */
drhf5905aa2002-05-26 20:54:33 +00001632 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001633 Expr *pLeft = pExpr->pLeft;
1634 Expr *pRight = pExpr->pList->a[0].pExpr;
1635 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001636 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001637 sqlite3ExprCode(pParse, pRight);
1638 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001639
drhbe5c89a2004-07-26 00:31:09 +00001640 pRight = pExpr->pList->a[1].pExpr;
1641 sqlite3ExprCode(pParse, pRight);
1642 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001643
danielk19774adee202004-05-08 08:23:19 +00001644 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1645 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1646 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001647 break;
1648 }
drhcce7d172000-05-31 15:34:51 +00001649 default: {
danielk19774adee202004-05-08 08:23:19 +00001650 sqlite3ExprCode(pParse, pExpr);
1651 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001652 break;
1653 }
1654 }
1655}
1656
1657/*
drh66b89c82000-11-28 20:47:17 +00001658** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001659** to the label "dest" if the expression is false but execution
1660** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001661**
1662** If the expression evaluates to NULL (neither true nor false) then
1663** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001664*/
danielk19774adee202004-05-08 08:23:19 +00001665void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001666 Vdbe *v = pParse->pVdbe;
1667 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001668 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001669
1670 /* The value of pExpr->op and op are related as follows:
1671 **
1672 ** pExpr->op op
1673 ** --------- ----------
1674 ** TK_ISNULL OP_NotNull
1675 ** TK_NOTNULL OP_IsNull
1676 ** TK_NE OP_Eq
1677 ** TK_EQ OP_Ne
1678 ** TK_GT OP_Le
1679 ** TK_LE OP_Gt
1680 ** TK_GE OP_Lt
1681 ** TK_LT OP_Ge
1682 **
1683 ** For other values of pExpr->op, op is undefined and unused.
1684 ** The value of TK_ and OP_ constants are arranged such that we
1685 ** can compute the mapping above using the following expression.
1686 ** Assert()s verify that the computation is correct.
1687 */
1688 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1689
1690 /* Verify correct alignment of TK_ and OP_ constants
1691 */
1692 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1693 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1694 assert( pExpr->op!=TK_NE || op==OP_Eq );
1695 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1696 assert( pExpr->op!=TK_LT || op==OP_Ge );
1697 assert( pExpr->op!=TK_LE || op==OP_Gt );
1698 assert( pExpr->op!=TK_GT || op==OP_Le );
1699 assert( pExpr->op!=TK_GE || op==OP_Lt );
1700
drhcce7d172000-05-31 15:34:51 +00001701 switch( pExpr->op ){
1702 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001703 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1704 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001705 break;
1706 }
1707 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001708 int d2 = sqlite3VdbeMakeLabel(v);
1709 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1710 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1711 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001712 break;
1713 }
1714 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001715 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001716 break;
1717 }
1718 case TK_LT:
1719 case TK_LE:
1720 case TK_GT:
1721 case TK_GE:
1722 case TK_NE:
1723 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001724 sqlite3ExprCode(pParse, pExpr->pLeft);
1725 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001726 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001727 break;
1728 }
drhcce7d172000-05-31 15:34:51 +00001729 case TK_ISNULL:
1730 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001731 sqlite3ExprCode(pParse, pExpr->pLeft);
1732 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001733 break;
1734 }
drhfef52082000-06-06 01:50:43 +00001735 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001736 /* The expression is "x BETWEEN y AND z". It is implemented as:
1737 **
1738 ** 1 IF (x >= y) GOTO 3
1739 ** 2 GOTO <dest>
1740 ** 3 IF (x > z) GOTO <dest>
1741 */
drhfef52082000-06-06 01:50:43 +00001742 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001743 Expr *pLeft = pExpr->pLeft;
1744 Expr *pRight = pExpr->pList->a[0].pExpr;
1745 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001746 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001747 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001748 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001749 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1750
danielk19774adee202004-05-08 08:23:19 +00001751 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1752 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001753 pRight = pExpr->pList->a[1].pExpr;
1754 sqlite3ExprCode(pParse, pRight);
1755 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001756 break;
1757 }
drhcce7d172000-05-31 15:34:51 +00001758 default: {
danielk19774adee202004-05-08 08:23:19 +00001759 sqlite3ExprCode(pParse, pExpr);
1760 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001761 break;
1762 }
1763 }
1764}
drh22827922000-06-06 17:27:05 +00001765
1766/*
1767** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1768** if they are identical and return FALSE if they differ in any way.
1769*/
danielk19774adee202004-05-08 08:23:19 +00001770int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001771 int i;
1772 if( pA==0 ){
1773 return pB==0;
1774 }else if( pB==0 ){
1775 return 0;
1776 }
1777 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001778 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1779 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001780 if( pA->pList ){
1781 if( pB->pList==0 ) return 0;
1782 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1783 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001784 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001785 return 0;
1786 }
1787 }
1788 }else if( pB->pList ){
1789 return 0;
1790 }
1791 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001792 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001793 if( pA->token.z ){
1794 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001795 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001796 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001797 }
1798 return 1;
1799}
1800
1801/*
1802** Add a new element to the pParse->aAgg[] array and return its index.
1803*/
1804static int appendAggInfo(Parse *pParse){
1805 if( (pParse->nAgg & 0x7)==0 ){
1806 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001807 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1808 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001809 return -1;
1810 }
drh6d4abfb2001-10-22 02:58:08 +00001811 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001812 }
1813 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1814 return pParse->nAgg++;
1815}
1816
1817/*
1818** Analyze the given expression looking for aggregate functions and
1819** for variables that need to be added to the pParse->aAgg[] array.
1820** Make additional entries to the pParse->aAgg[] array as necessary.
1821**
1822** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001823** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001824**
1825** If errors are seen, leave an error message in zErrMsg and return
1826** the number of errors.
1827*/
danielk19774adee202004-05-08 08:23:19 +00001828int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001829 int i;
1830 AggExpr *aAgg;
1831 int nErr = 0;
1832
1833 if( pExpr==0 ) return 0;
1834 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001835 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001836 aAgg = pParse->aAgg;
1837 for(i=0; i<pParse->nAgg; i++){
1838 if( aAgg[i].isAgg ) continue;
1839 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001840 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001841 break;
1842 }
1843 }
1844 if( i>=pParse->nAgg ){
1845 i = appendAggInfo(pParse);
1846 if( i<0 ) return 1;
1847 pParse->aAgg[i].isAgg = 0;
1848 pParse->aAgg[i].pExpr = pExpr;
1849 }
drhaaf88722000-06-08 11:25:00 +00001850 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001851 break;
1852 }
1853 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001854 aAgg = pParse->aAgg;
1855 for(i=0; i<pParse->nAgg; i++){
1856 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001857 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001858 break;
1859 }
1860 }
1861 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001862 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001863 i = appendAggInfo(pParse);
1864 if( i<0 ) return 1;
1865 pParse->aAgg[i].isAgg = 1;
1866 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001867 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001868 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001869 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001870 }
1871 pExpr->iAgg = i;
1872 break;
1873 }
1874 default: {
1875 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001876 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001877 }
1878 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001879 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001880 }
1881 if( nErr==0 && pExpr->pList ){
1882 int n = pExpr->pList->nExpr;
1883 int i;
1884 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001885 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001886 }
1887 }
1888 break;
1889 }
1890 }
1891 return nErr;
1892}
drh8e0a2f92002-02-23 23:45:45 +00001893
1894/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001895** Locate a user function given a name, a number of arguments and a flag
1896** indicating whether the function prefers UTF-16 over UTF-8. Return a
1897** pointer to the FuncDef structure that defines that function, or return
1898** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001899**
drh0bce8352002-02-28 00:41:10 +00001900** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001901** structure is created and liked into the "db" structure if a
1902** no matching function previously existed. When createFlag is true
1903** and the nArg parameter is -1, then only a function that accepts
1904** any number of arguments will be returned.
1905**
1906** If createFlag is false and nArg is -1, then the first valid
1907** function found is returned. A function is valid if either xFunc
1908** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001909**
1910** If createFlag is false, then a function with the required name and
1911** number of arguments may be returned even if the eTextRep flag does not
1912** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001913*/
danielk19774adee202004-05-08 08:23:19 +00001914FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001915 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001916 const char *zName, /* Name of the function. Not null-terminated */
1917 int nName, /* Number of characters in the name */
1918 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001919 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001920 int createFlag /* Create new entry if true and does not otherwise exist */
1921){
danielk1977d02eb1f2004-06-06 09:44:03 +00001922 FuncDef *p; /* Iterator variable */
1923 FuncDef *pFirst; /* First function with this name */
1924 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001925 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001926
danielk1977d8123362004-06-12 09:25:12 +00001927
1928 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001929 if( nArg<-1 ) nArg = -1;
1930
1931 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1932 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001933 /* During the search for the best function definition, bestmatch is set
1934 ** as follows to indicate the quality of the match with the definition
1935 ** pointed to by pBest:
1936 **
1937 ** 0: pBest is NULL. No match has been found.
1938 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1939 ** encoding is requested, or vice versa.
1940 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1941 ** requested, or vice versa.
1942 ** 3: A variable arguments function using the same text encoding.
1943 ** 4: A function with the exact number of arguments requested that
1944 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1945 ** 5: A function with the exact number of arguments requested that
1946 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1947 ** 6: An exact match.
1948 **
1949 ** A larger value of 'matchqual' indicates a more desirable match.
1950 */
danielk1977e12c17b2004-06-23 12:35:14 +00001951 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001952 int match = 1; /* Quality of this match */
1953 if( p->nArg==nArg || nArg==-1 ){
1954 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001955 }
danielk1977d8123362004-06-12 09:25:12 +00001956 if( enc==p->iPrefEnc ){
1957 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001958 }
danielk1977d8123362004-06-12 09:25:12 +00001959 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1960 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1961 match += 1;
1962 }
1963
1964 if( match>bestmatch ){
1965 pBest = p;
1966 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001967 }
1968 }
drh8e0a2f92002-02-23 23:45:45 +00001969 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001970
danielk1977d8123362004-06-12 09:25:12 +00001971 /* If the createFlag parameter is true, and the seach did not reveal an
1972 ** exact match for the name, number of arguments and encoding, then add a
1973 ** new entry to the hash table and return it.
1974 */
1975 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001976 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1977 pBest->nArg = nArg;
1978 pBest->pNext = pFirst;
1979 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001980 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001981 memcpy(pBest->zName, zName, nName);
1982 pBest->zName[nName] = 0;
1983 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001984 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001985
1986 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1987 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001988 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001989 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001990}