blob: 7567d849d558f4b8945d80606105898b01acd632 [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**
drh2958a4e2004-11-12 03:56:15 +000015** $Id: expr.c,v 1.170 2004/11/12 03:56:15 drh 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.
drh2958a4e2004-11-12 03:56:15 +0000205** "#1" means the next down on the stack. And so forth. #-1 means
206** memory location 0. #-2 means memory location 1. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000207**
208** This routine is called by the parser to deal with on of those terms.
209** It immediately generates code to store the value in a memory location.
210** The returns an expression that will code to extract the value from
211** that memory location as needed.
212*/
213Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
214 Vdbe *v = pParse->pVdbe;
215 Expr *p;
216 int depth;
217 if( v==0 ) return 0;
218 if( pParse->nested==0 ){
219 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
220 return 0;
221 }
222 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
223 depth = atoi(&pToken->z[1]);
drh2958a4e2004-11-12 03:56:15 +0000224 if( depth>=0 ){
225 p->iTable = pParse->nMem++;
226 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
227 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
228 }else{
229 p->iTable = -1-depth;
230 }
drh4e0cff62004-11-05 05:10:28 +0000231 return p;
232}
233
234/*
drh91bb0ee2004-09-01 03:06:34 +0000235** Join two expressions using an AND operator. If either expression is
236** NULL, then just return the other expression.
237*/
238Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
239 if( pLeft==0 ){
240 return pRight;
241 }else if( pRight==0 ){
242 return pLeft;
243 }else{
244 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
245 }
246}
247
248/*
drh6977fea2002-10-22 23:38:04 +0000249** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000250** text between the two given tokens.
251*/
danielk19774adee202004-05-08 08:23:19 +0000252void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000253 assert( pRight!=0 );
254 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000255 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000256 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000257 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000258 pExpr->span.z = pLeft->z;
259 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000260 }else{
drh6977fea2002-10-22 23:38:04 +0000261 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000262 }
drha76b5df2002-02-23 02:32:10 +0000263 }
264}
265
266/*
267** Construct a new expression node for a function with multiple
268** arguments.
269*/
danielk19774adee202004-05-08 08:23:19 +0000270Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000271 Expr *pNew;
272 pNew = sqliteMalloc( sizeof(Expr) );
273 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000274 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000275 return 0;
276 }
277 pNew->op = TK_FUNCTION;
278 pNew->pList = pList;
279 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000280 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000281 pNew->token = *pToken;
282 }else{
283 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000284 }
drh6977fea2002-10-22 23:38:04 +0000285 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000286 return pNew;
287}
288
289/*
drhfa6bc002004-09-07 16:19:52 +0000290** Assign a variable number to an expression that encodes a wildcard
291** in the original SQL statement.
292**
293** Wildcards consisting of a single "?" are assigned the next sequential
294** variable number.
295**
296** Wildcards of the form "?nnn" are assigned the number "nnn". We make
297** sure "nnn" is not too be to avoid a denial of service attack when
298** the SQL statement comes from an external source.
299**
300** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
301** as the previous instance of the same wildcard. Or if this is the first
302** instance of the wildcard, the next sequenial variable number is
303** assigned.
304*/
305void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
306 Token *pToken;
307 if( pExpr==0 ) return;
308 pToken = &pExpr->token;
309 assert( pToken->n>=1 );
310 assert( pToken->z!=0 );
311 assert( pToken->z[0]!=0 );
312 if( pToken->n==1 ){
313 /* Wildcard of the form "?". Assign the next variable number */
314 pExpr->iTable = ++pParse->nVar;
315 }else if( pToken->z[0]=='?' ){
316 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
317 ** use it as the variable number */
318 int i;
319 pExpr->iTable = i = atoi(&pToken->z[1]);
320 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
321 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
322 SQLITE_MAX_VARIABLE_NUMBER);
323 }
324 if( i>pParse->nVar ){
325 pParse->nVar = i;
326 }
327 }else{
328 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
329 ** number as the prior appearance of the same name, or if the name
330 ** has never appeared before, reuse the same variable number
331 */
332 int i, n;
333 n = pToken->n;
334 for(i=0; i<pParse->nVarExpr; i++){
335 Expr *pE;
336 if( (pE = pParse->apVarExpr[i])!=0
337 && pE->token.n==n
338 && memcmp(pE->token.z, pToken->z, n)==0 ){
339 pExpr->iTable = pE->iTable;
340 break;
341 }
342 }
343 if( i>=pParse->nVarExpr ){
344 pExpr->iTable = ++pParse->nVar;
345 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
346 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
347 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
348 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
349 }
350 if( !sqlite3_malloc_failed ){
351 assert( pParse->apVarExpr!=0 );
352 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
353 }
354 }
355 }
356}
357
358/*
drha2e00042002-01-22 03:13:42 +0000359** Recursively delete an expression tree.
360*/
danielk19774adee202004-05-08 08:23:19 +0000361void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000362 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000363 if( p->span.dyn ) sqliteFree((char*)p->span.z);
364 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000365 sqlite3ExprDelete(p->pLeft);
366 sqlite3ExprDelete(p->pRight);
367 sqlite3ExprListDelete(p->pList);
368 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000369 sqliteFree(p);
370}
371
drha76b5df2002-02-23 02:32:10 +0000372
373/*
drhff78bd22002-02-27 01:47:11 +0000374** The following group of routines make deep copies of expressions,
375** expression lists, ID lists, and select statements. The copies can
376** be deleted (by being passed to their respective ...Delete() routines)
377** without effecting the originals.
378**
danielk19774adee202004-05-08 08:23:19 +0000379** The expression list, ID, and source lists return by sqlite3ExprListDup(),
380** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000381** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000382**
drhad3cab52002-05-24 02:04:32 +0000383** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000384*/
danielk19774adee202004-05-08 08:23:19 +0000385Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000386 Expr *pNew;
387 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000388 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000389 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000390 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000391 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000392 pNew->token.z = sqliteStrDup(p->token.z);
393 pNew->token.dyn = 1;
394 }else{
drh4efc4752004-01-16 15:55:37 +0000395 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000396 }
drh6977fea2002-10-22 23:38:04 +0000397 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000398 pNew->pLeft = sqlite3ExprDup(p->pLeft);
399 pNew->pRight = sqlite3ExprDup(p->pRight);
400 pNew->pList = sqlite3ExprListDup(p->pList);
401 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000402 return pNew;
403}
danielk19774adee202004-05-08 08:23:19 +0000404void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000405 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000406 if( pFrom->z ){
407 pTo->n = pFrom->n;
408 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
409 pTo->dyn = 1;
410 }else{
drh4b59ab52002-08-24 18:24:51 +0000411 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000412 }
413}
danielk19774adee202004-05-08 08:23:19 +0000414ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000415 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000416 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000417 int i;
418 if( p==0 ) return 0;
419 pNew = sqliteMalloc( sizeof(*pNew) );
420 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000421 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000422 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000423 if( pItem==0 ){
424 sqliteFree(pNew);
425 return 0;
426 }
drh145716b2004-09-24 12:24:06 +0000427 pOldItem = p->a;
428 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000429 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000430 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000431 if( pOldExpr->span.z!=0 && pNewExpr ){
432 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000433 ** expression list. The logic in SELECT processing that determines
434 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000435 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000436 }
drh1f3e9052002-10-31 00:09:39 +0000437 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000438 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000439 pItem->zName = sqliteStrDup(pOldItem->zName);
440 pItem->sortOrder = pOldItem->sortOrder;
441 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000442 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000443 }
444 return pNew;
445}
danielk19774adee202004-05-08 08:23:19 +0000446SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000447 SrcList *pNew;
448 int i;
drh113088e2003-03-20 01:16:58 +0000449 int nByte;
drhad3cab52002-05-24 02:04:32 +0000450 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000451 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000452 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000453 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000454 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000455 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000456 struct SrcList_item *pNewItem = &pNew->a[i];
457 struct SrcList_item *pOldItem = &p->a[i];
458 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
459 pNewItem->zName = sqliteStrDup(pOldItem->zName);
460 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
461 pNewItem->jointype = pOldItem->jointype;
462 pNewItem->iCursor = pOldItem->iCursor;
463 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000464 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
465 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
466 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000467 }
468 return pNew;
469}
danielk19774adee202004-05-08 08:23:19 +0000470IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000471 IdList *pNew;
472 int i;
473 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000474 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000475 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000476 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000477 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000478 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000479 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000480 struct IdList_item *pNewItem = &pNew->a[i];
481 struct IdList_item *pOldItem = &p->a[i];
482 pNewItem->zName = sqliteStrDup(pOldItem->zName);
483 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000484 }
485 return pNew;
486}
danielk19774adee202004-05-08 08:23:19 +0000487Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000488 Select *pNew;
489 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000490 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000491 if( pNew==0 ) return 0;
492 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000493 pNew->pEList = sqlite3ExprListDup(p->pEList);
494 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
495 pNew->pWhere = sqlite3ExprDup(p->pWhere);
496 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
497 pNew->pHaving = sqlite3ExprDup(p->pHaving);
498 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000499 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000500 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000501 pNew->nLimit = p->nLimit;
502 pNew->nOffset = p->nOffset;
503 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000504 pNew->iLimit = -1;
505 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000506 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000507 return pNew;
508}
509
510
511/*
drha76b5df2002-02-23 02:32:10 +0000512** Add a new element to the end of an expression list. If pList is
513** initially NULL, then create a new expression list.
514*/
danielk19774adee202004-05-08 08:23:19 +0000515ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000516 if( pList==0 ){
517 pList = sqliteMalloc( sizeof(ExprList) );
518 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000519 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000520 return 0;
521 }
drh4efc4752004-01-16 15:55:37 +0000522 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000523 }
drh4305d102003-07-30 12:34:12 +0000524 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000525 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000526 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
527 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000528 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000529 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000530 return pList;
531 }
drha76b5df2002-02-23 02:32:10 +0000532 }
drh4efc4752004-01-16 15:55:37 +0000533 assert( pList->a!=0 );
534 if( pExpr || pName ){
535 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
536 memset(pItem, 0, sizeof(*pItem));
537 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000538 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000539 }
540 return pList;
541}
542
543/*
544** Delete an entire expression list.
545*/
danielk19774adee202004-05-08 08:23:19 +0000546void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000547 int i;
drhbe5c89a2004-07-26 00:31:09 +0000548 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000549 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000550 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
551 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000552 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
553 sqlite3ExprDelete(pItem->pExpr);
554 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000555 }
556 sqliteFree(pList->a);
557 sqliteFree(pList);
558}
559
560/*
drhfef52082000-06-06 01:50:43 +0000561** Walk an expression tree. Return 1 if the expression is constant
562** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000563**
564** For the purposes of this function, a double-quoted string (ex: "abc")
565** is considered a variable but a single-quoted string (ex: 'abc') is
566** a constant.
drhfef52082000-06-06 01:50:43 +0000567*/
danielk19774adee202004-05-08 08:23:19 +0000568int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000569 switch( p->op ){
570 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000571 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000572 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000573 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000574 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000575 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000576 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000577 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000578 case TK_INTEGER:
579 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000580 case TK_VARIABLE:
danielk19777977a172004-11-09 12:44:37 +0000581 case TK_CTIME:
582 case TK_CTIMESTAMP:
583 case TK_CDATE:
drh92086432002-01-22 14:11:29 +0000584 return 1;
drhfef52082000-06-06 01:50:43 +0000585 default: {
danielk19774adee202004-05-08 08:23:19 +0000586 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
587 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000588 if( p->pList ){
589 int i;
590 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000591 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000592 }
593 }
drh92086432002-01-22 14:11:29 +0000594 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000595 }
596 }
drh92086432002-01-22 14:11:29 +0000597 return 0;
drhfef52082000-06-06 01:50:43 +0000598}
599
600/*
drh202b2df2004-01-06 01:13:46 +0000601** If the given expression codes a constant integer that is small enough
602** to fit in a 32-bit integer, return 1 and put the value of the integer
603** in *pValue. If the expression is not an integer or if it is too big
604** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000605*/
danielk19774adee202004-05-08 08:23:19 +0000606int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000607 switch( p->op ){
608 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000609 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000610 return 1;
611 }
612 break;
drhe4de1fe2002-06-02 16:09:01 +0000613 }
614 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000615 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000616 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000617 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000618 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000619 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000620 return 1;
621 }
622 break;
623 }
drh4b59ab52002-08-24 18:24:51 +0000624 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000625 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000626 }
drhe4de1fe2002-06-02 16:09:01 +0000627 case TK_UMINUS: {
628 int v;
danielk19774adee202004-05-08 08:23:19 +0000629 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000630 *pValue = -v;
631 return 1;
632 }
633 break;
634 }
635 default: break;
636 }
637 return 0;
638}
639
640/*
drhc4a3c772001-04-04 11:48:57 +0000641** Return TRUE if the given string is a row-id column name.
642*/
danielk19774adee202004-05-08 08:23:19 +0000643int sqlite3IsRowid(const char *z){
644 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
645 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
646 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000647 return 0;
648}
649
650/*
drh8141f612004-01-25 22:44:58 +0000651** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
652** that name in the set of source tables in pSrcList and make the pExpr
653** expression node refer back to that source column. The following changes
654** are made to pExpr:
655**
656** pExpr->iDb Set the index in db->aDb[] of the database holding
657** the table.
658** pExpr->iTable Set to the cursor number for the table obtained
659** from pSrcList.
660** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000661** pExpr->op Set to TK_COLUMN.
662** pExpr->pLeft Any expression this points to is deleted
663** pExpr->pRight Any expression this points to is deleted.
664**
665** The pDbToken is the name of the database (the "X"). This value may be
666** NULL meaning that name is of the form Y.Z or Z. Any available database
667** can be used. The pTableToken is the name of the table (the "Y"). This
668** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
669** means that the form of the name is Z and that columns from any table
670** can be used.
671**
672** If the name cannot be resolved unambiguously, leave an error message
673** in pParse and return non-zero. Return zero on success.
674*/
675static int lookupName(
676 Parse *pParse, /* The parsing context */
677 Token *pDbToken, /* Name of the database containing table, or NULL */
678 Token *pTableToken, /* Name of table containing column, or NULL */
679 Token *pColumnToken, /* Name of the column. */
680 SrcList *pSrcList, /* List of tables used to resolve column names */
681 ExprList *pEList, /* List of expressions used to resolve "AS" */
682 Expr *pExpr /* Make this EXPR node point to the selected column */
683){
684 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
685 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
686 char *zCol = 0; /* Name of the column. The "Z" */
687 int i, j; /* Loop counters */
688 int cnt = 0; /* Number of matching column names */
689 int cntTab = 0; /* Number of matching table names */
drh9bb575f2004-09-06 17:24:11 +0000690 sqlite3 *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000691
692 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000693 zDb = sqlite3NameFromToken(pDbToken);
694 zTab = sqlite3NameFromToken(pTableToken);
695 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000696 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000697 return 1; /* Leak memory (zDb and zTab) if malloc fails */
698 }
699 assert( zTab==0 || pEList==0 );
700
701 pExpr->iTable = -1;
702 for(i=0; i<pSrcList->nSrc; i++){
703 struct SrcList_item *pItem = &pSrcList->a[i];
704 Table *pTab = pItem->pTab;
705 Column *pCol;
706
707 if( pTab==0 ) continue;
708 assert( pTab->nCol>0 );
709 if( zTab ){
710 if( pItem->zAlias ){
711 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000712 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000713 }else{
714 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000715 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
716 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000717 continue;
718 }
719 }
720 }
721 if( 0==(cntTab++) ){
722 pExpr->iTable = pItem->iCursor;
723 pExpr->iDb = pTab->iDb;
724 }
725 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000726 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000727 cnt++;
728 pExpr->iTable = pItem->iCursor;
729 pExpr->iDb = pTab->iDb;
730 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
731 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000732 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000733 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000734 break;
735 }
736 }
737 }
738
drhb7f91642004-10-31 02:22:47 +0000739#ifndef SQLITE_OMIT_TRIGGER
drh8141f612004-01-25 22:44:58 +0000740 /* If we have not already resolved the name, then maybe
741 ** it is a new.* or old.* trigger argument reference
742 */
743 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
744 TriggerStack *pTriggerStack = pParse->trigStack;
745 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000746 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000747 pExpr->iTable = pTriggerStack->newIdx;
748 assert( pTriggerStack->pTab );
749 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000750 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000751 pExpr->iTable = pTriggerStack->oldIdx;
752 assert( pTriggerStack->pTab );
753 pTab = pTriggerStack->pTab;
754 }
755
756 if( pTab ){
757 int j;
758 Column *pCol = pTab->aCol;
759
760 pExpr->iDb = pTab->iDb;
761 cntTab++;
762 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000763 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000764 cnt++;
765 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000766 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000767 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000768 break;
769 }
770 }
771 }
772 }
drhb7f91642004-10-31 02:22:47 +0000773#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000774
775 /*
776 ** Perhaps the name is a reference to the ROWID
777 */
danielk19774adee202004-05-08 08:23:19 +0000778 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000779 cnt = 1;
780 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000781 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000782 }
783
784 /*
785 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
786 ** might refer to an result-set alias. This happens, for example, when
787 ** we are resolving names in the WHERE clause of the following command:
788 **
789 ** SELECT a+b AS x FROM table WHERE x<10;
790 **
791 ** In cases like this, replace pExpr with a copy of the expression that
792 ** forms the result set entry ("a+b" in the example) and return immediately.
793 ** Note that the expression in the result set should have already been
794 ** resolved by the time the WHERE clause is resolved.
795 */
796 if( cnt==0 && pEList!=0 ){
797 for(j=0; j<pEList->nExpr; j++){
798 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000799 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000800 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
801 pExpr->op = TK_AS;
802 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000803 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000804 sqliteFree(zCol);
805 assert( zTab==0 && zDb==0 );
806 return 0;
807 }
808 }
809 }
810
811 /*
812 ** If X and Y are NULL (in other words if only the column name Z is
813 ** supplied) and the value of Z is enclosed in double-quotes, then
814 ** Z is a string literal if it doesn't match any column names. In that
815 ** case, we need to return right away and not make any changes to
816 ** pExpr.
817 */
818 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
819 sqliteFree(zCol);
820 return 0;
821 }
822
823 /*
824 ** cnt==0 means there was not match. cnt>1 means there were two or
825 ** more matches. Either way, we have an error.
826 */
827 if( cnt!=1 ){
828 char *z = 0;
829 char *zErr;
830 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
831 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000832 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000833 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000834 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000835 }else{
836 z = sqliteStrDup(zCol);
837 }
danielk19774adee202004-05-08 08:23:19 +0000838 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000839 sqliteFree(z);
840 }
841
842 /* Clean up and return
843 */
844 sqliteFree(zDb);
845 sqliteFree(zTab);
846 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000847 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000848 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000849 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000850 pExpr->pRight = 0;
851 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000852 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000853 return cnt!=1;
854}
855
856/*
drhcce7d172000-05-31 15:34:51 +0000857** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000858** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000859** index to the table in the table list and a column offset. The
860** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
861** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000862** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000863** VDBE cursor number for a cursor that is pointing into the referenced
864** table. The Expr.iColumn value is changed to the index of the column
865** of the referenced table. The Expr.iColumn value for the special
866** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
867** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000868**
drhfef52082000-06-06 01:50:43 +0000869** We also check for instances of the IN operator. IN comes in two
870** forms:
871**
872** expr IN (exprlist)
873** and
874** expr IN (SELECT ...)
875**
876** The first form is handled by creating a set holding the list
877** of allowed values. The second form causes the SELECT to generate
878** a temporary table.
879**
880** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000881** If it finds any, it generates code to write the value of that select
882** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000883**
drh967e8b72000-06-21 13:59:10 +0000884** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000885** the number of errors seen and leaves an error message on pParse->zErrMsg.
886*/
danielk19774adee202004-05-08 08:23:19 +0000887int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000888 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000889 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000890 ExprList *pEList, /* List of expressions used to resolve "AS" */
891 Expr *pExpr /* The expression to be analyzed. */
892){
drh6a3ea0e2003-05-02 14:32:12 +0000893 int i;
894
drh8141f612004-01-25 22:44:58 +0000895 if( pExpr==0 || pSrcList==0 ) return 0;
896 for(i=0; i<pSrcList->nSrc; i++){
897 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000898 }
drhcce7d172000-05-31 15:34:51 +0000899 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000900 /* Double-quoted strings (ex: "abc") are used as identifiers if
901 ** possible. Otherwise they remain as strings. Single-quoted
902 ** strings (ex: 'abc') are always string literals.
903 */
904 case TK_STRING: {
905 if( pExpr->token.z[0]=='\'' ) break;
906 /* Fall thru into the TK_ID case if this is a double-quoted string */
907 }
drh8141f612004-01-25 22:44:58 +0000908 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000909 */
drhcce7d172000-05-31 15:34:51 +0000910 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000911 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000912 return 1;
drhed6c8672003-01-12 18:02:16 +0000913 }
drhcce7d172000-05-31 15:34:51 +0000914 break;
915 }
916
drhd24cc422003-03-27 12:51:24 +0000917 /* A table name and column name: ID.ID
918 ** Or a database, table and column: ID.ID.ID
919 */
drhcce7d172000-05-31 15:34:51 +0000920 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000921 Token *pColumn;
922 Token *pTable;
923 Token *pDb;
924 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000925
drhcce7d172000-05-31 15:34:51 +0000926 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000927 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000928 pDb = 0;
929 pTable = &pExpr->pLeft->token;
930 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000931 }else{
drh8141f612004-01-25 22:44:58 +0000932 assert( pRight->op==TK_DOT );
933 pDb = &pExpr->pLeft->token;
934 pTable = &pRight->pLeft->token;
935 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000936 }
drh8141f612004-01-25 22:44:58 +0000937 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000938 return 1;
939 }
drhcce7d172000-05-31 15:34:51 +0000940 break;
941 }
942
drhfef52082000-06-06 01:50:43 +0000943 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000944 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000945 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000946 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000947 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000948
drhfef52082000-06-06 01:50:43 +0000949 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000950 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000951 return 1;
952 }
danielk1977bf3b7212004-05-18 10:06:24 +0000953 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000954
955 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
956 ** expression it is handled the same way. A temporary table is
957 ** filled with single-field index keys representing the results
958 ** from the SELECT or the <exprlist>.
959 **
960 ** If the 'x' expression is a column value, or the SELECT...
961 ** statement returns a column value, then the affinity of that
962 ** column is used to build the index keys. If both 'x' and the
963 ** SELECT... statement are columns, then numeric affinity is used
964 ** if either column has NUMERIC or INTEGER affinity. If neither
965 ** 'x' nor the SELECT... statement are columns, then numeric affinity
966 ** is used.
967 */
968 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000969 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000970 memset(&keyInfo, 0, sizeof(keyInfo));
971 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000972 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000973
drhfef52082000-06-06 01:50:43 +0000974 if( pExpr->pSelect ){
975 /* Case 1: expr IN (SELECT ...)
976 **
danielk1977e014a832004-05-17 10:48:57 +0000977 ** Generate code to write the results of the select into the temporary
978 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000979 */
danielk1977e014a832004-05-17 10:48:57 +0000980 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000981 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000982 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000983 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000984 pEList = pExpr->pSelect->pEList;
985 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000986 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000987 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000988 }
drhfef52082000-06-06 01:50:43 +0000989 }else if( pExpr->pList ){
990 /* Case 2: expr IN (exprlist)
991 **
danielk1977e014a832004-05-17 10:48:57 +0000992 ** For each expression, build an index key from the evaluation and
993 ** store it in the temporary table. If <expr> is a column, then use
994 ** that columns affinity when building index keys. If <expr> is not
995 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000996 */
danielk1977e014a832004-05-17 10:48:57 +0000997 int i;
danielk1977e014a832004-05-17 10:48:57 +0000998 if( !affinity ){
999 affinity = SQLITE_AFF_NUMERIC;
1000 }
danielk19770202b292004-06-09 09:55:16 +00001001 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001002
1003 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001004 for(i=0; i<pExpr->pList->nExpr; i++){
1005 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001006
1007 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001008 if( !sqlite3ExprIsConstant(pE2) ){
1009 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001010 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +00001011 return 1;
1012 }
danielk19774adee202004-05-08 08:23:19 +00001013 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +00001014 return 1;
1015 }
danielk1977e014a832004-05-17 10:48:57 +00001016
1017 /* Evaluate the expression and insert it into the temp table */
1018 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001019 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001020 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001021 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001022 }
1023 }
danielk19770202b292004-06-09 09:55:16 +00001024 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
1025
drhcfab11b2000-06-06 03:31:22 +00001026 break;
drhfef52082000-06-06 01:50:43 +00001027 }
1028
drh19a775c2000-06-05 18:54:46 +00001029 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001030 /* This has to be a scalar SELECT. Generate code to put the
1031 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001032 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001033 */
drh967e8b72000-06-21 13:59:10 +00001034 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +00001035 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +00001036 return 1;
1037 }
1038 break;
1039 }
1040
drhcce7d172000-05-31 15:34:51 +00001041 /* For all else, just recursively walk the tree */
1042 default: {
drh4794b982000-06-06 13:54:14 +00001043 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +00001044 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +00001045 return 1;
1046 }
1047 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +00001048 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +00001049 return 1;
1050 }
1051 if( pExpr->pList ){
1052 int i;
1053 ExprList *pList = pExpr->pList;
1054 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001055 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001056 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +00001057 return 1;
1058 }
1059 }
1060 }
1061 }
1062 }
1063 return 0;
1064}
1065
drhcce7d172000-05-31 15:34:51 +00001066/*
drh4b59ab52002-08-24 18:24:51 +00001067** pExpr is a node that defines a function of some kind. It might
1068** be a syntactic function like "count(x)" or it might be a function
1069** that implements an operator, like "a LIKE b".
1070**
1071** This routine makes *pzName point to the name of the function and
1072** *pnName hold the number of characters in the function name.
1073*/
1074static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1075 switch( pExpr->op ){
1076 case TK_FUNCTION: {
1077 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +00001078 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +00001079 break;
1080 }
1081 case TK_LIKE: {
1082 *pzName = "like";
1083 *pnName = 4;
1084 break;
1085 }
1086 case TK_GLOB: {
1087 *pzName = "glob";
1088 *pnName = 4;
1089 break;
1090 }
danielk19777977a172004-11-09 12:44:37 +00001091 case TK_CTIME: {
1092 *pzName = "current_time";
1093 *pnName = 12;
1094 break;
1095 }
1096 case TK_CDATE: {
1097 *pzName = "current_date";
1098 *pnName = 12;
1099 break;
1100 }
1101 case TK_CTIMESTAMP: {
1102 *pzName = "current_timestamp";
1103 *pnName = 17;
1104 break;
1105 }
drh4b59ab52002-08-24 18:24:51 +00001106 default: {
1107 *pzName = "can't happen";
1108 *pnName = 12;
1109 break;
1110 }
1111 }
1112}
1113
1114/*
drhcce7d172000-05-31 15:34:51 +00001115** Error check the functions in an expression. Make sure all
1116** function names are recognized and all functions have the correct
1117** number of arguments. Leave an error message in pParse->zErrMsg
1118** if anything is amiss. Return the number of errors.
1119**
1120** if pIsAgg is not null and this expression is an aggregate function
1121** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1122*/
danielk19774adee202004-05-08 08:23:19 +00001123int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001124 int nErr = 0;
1125 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001126 switch( pExpr->op ){
danielk19777977a172004-11-09 12:44:37 +00001127 case TK_CTIME:
1128 case TK_CTIMESTAMP:
1129 case TK_CDATE:
1130 /* Note: The above three were a seperate case in sqlmoto. Reason? */
drh4b59ab52002-08-24 18:24:51 +00001131 case TK_GLOB:
1132 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001133 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001134 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1135 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001136 int wrong_num_args = 0; /* True if wrong number of arguments */
1137 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001138 int i;
drh4b59ab52002-08-24 18:24:51 +00001139 int nId; /* Number of characters in function name */
1140 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001141 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001142 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001143
drh4b59ab52002-08-24 18:24:51 +00001144 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001145 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001146 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001147 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001148 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001149 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001150 }else{
1151 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001152 }
drh0bce8352002-02-28 00:41:10 +00001153 }else{
1154 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001155 }
drh8e0a2f92002-02-23 23:45:45 +00001156 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001157 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001158 nErr++;
1159 is_agg = 0;
1160 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001161 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001162 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001163 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001164 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001165 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001166 nErr++;
drhcce7d172000-05-31 15:34:51 +00001167 }
drhf7a9e1a2004-02-22 18:40:56 +00001168 if( is_agg ){
1169 pExpr->op = TK_AGG_FUNCTION;
1170 if( pIsAgg ) *pIsAgg = 1;
1171 }
drhcce7d172000-05-31 15:34:51 +00001172 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001173 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001174 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001175 }
danielk19770202b292004-06-09 09:55:16 +00001176 /* FIX ME: Compute pExpr->affinity based on the expected return
1177 ** type of the function
1178 */
drhcce7d172000-05-31 15:34:51 +00001179 }
1180 default: {
1181 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001182 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001183 }
1184 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001185 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001186 }
drhfef52082000-06-06 01:50:43 +00001187 if( nErr==0 && pExpr->pList ){
1188 int n = pExpr->pList->nExpr;
1189 int i;
1190 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001191 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001192 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001193 }
1194 }
drhcce7d172000-05-31 15:34:51 +00001195 break;
1196 }
1197 }
1198 return nErr;
1199}
1200
1201/*
drh290c1942004-08-21 17:54:45 +00001202** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1203**
1204** This routine is provided as a convenience since it is very common
1205** to call ResolveIds() and Check() back to back.
1206*/
1207int sqlite3ExprResolveAndCheck(
1208 Parse *pParse, /* The parser context */
1209 SrcList *pSrcList, /* List of tables used to resolve column names */
1210 ExprList *pEList, /* List of expressions used to resolve "AS" */
1211 Expr *pExpr, /* The expression to be analyzed. */
1212 int allowAgg, /* True to allow aggregate expressions */
1213 int *pIsAgg /* Set to TRUE if aggregates are found */
1214){
1215 if( pExpr==0 ) return 0;
1216 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1217 return 1;
1218 }
1219 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1220}
1221
1222/*
drhfec19aa2004-05-19 20:41:03 +00001223** Generate an instruction that will put the integer describe by
1224** text z[0..n-1] on the stack.
1225*/
1226static void codeInteger(Vdbe *v, const char *z, int n){
1227 int i;
drh6fec0762004-05-30 01:38:43 +00001228 if( sqlite3GetInt32(z, &i) ){
1229 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1230 }else if( sqlite3FitsIn64Bits(z) ){
1231 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001232 }else{
1233 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1234 }
1235}
1236
1237/*
drhcce7d172000-05-31 15:34:51 +00001238** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001239** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001240**
1241** This code depends on the fact that certain token values (ex: TK_EQ)
1242** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1243** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1244** the make process cause these values to align. Assert()s in the code
1245** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001246*/
danielk19774adee202004-05-08 08:23:19 +00001247void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001248 Vdbe *v = pParse->pVdbe;
1249 int op;
danielk19777977a172004-11-09 12:44:37 +00001250 if( v==0 ) return;
1251 if( pExpr==0 ){
1252 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1253 return;
1254 }
drhf2bc0132004-10-04 13:19:23 +00001255 op = pExpr->op;
1256 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001257 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001258 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001259 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001260 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001261 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001262#ifndef NDEBUG
1263 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1264 VdbeComment((v, "# %T", &pExpr->span));
1265 }
1266#endif
drhc4a3c772001-04-04 11:48:57 +00001267 }else{
danielk19774adee202004-05-08 08:23:19 +00001268 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001269 }
drhcce7d172000-05-31 15:34:51 +00001270 break;
1271 }
1272 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001273 codeInteger(v, pExpr->token.z, pExpr->token.n);
1274 break;
1275 }
1276 case TK_FLOAT:
1277 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001278 assert( TK_FLOAT==OP_Real );
1279 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001280 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001281 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001282 break;
1283 }
danielk1977c572ef72004-05-27 09:28:41 +00001284 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001285 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001286 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1287 sqlite3VdbeDequoteP3(v, -1);
1288 break;
1289 }
drhcce7d172000-05-31 15:34:51 +00001290 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001291 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001292 break;
1293 }
drh50457892003-09-06 01:10:47 +00001294 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001295 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001296 if( pExpr->token.n>1 ){
1297 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1298 }
drh50457892003-09-06 01:10:47 +00001299 break;
1300 }
drh4e0cff62004-11-05 05:10:28 +00001301 case TK_REGISTER: {
1302 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1303 break;
1304 }
drhc9b84a12002-06-20 11:36:48 +00001305 case TK_LT:
1306 case TK_LE:
1307 case TK_GT:
1308 case TK_GE:
1309 case TK_NE:
1310 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001311 assert( TK_LT==OP_Lt );
1312 assert( TK_LE==OP_Le );
1313 assert( TK_GT==OP_Gt );
1314 assert( TK_GE==OP_Ge );
1315 assert( TK_EQ==OP_Eq );
1316 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001317 sqlite3ExprCode(pParse, pExpr->pLeft);
1318 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001319 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001320 break;
drhc9b84a12002-06-20 11:36:48 +00001321 }
drhcce7d172000-05-31 15:34:51 +00001322 case TK_AND:
1323 case TK_OR:
1324 case TK_PLUS:
1325 case TK_STAR:
1326 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001327 case TK_REM:
1328 case TK_BITAND:
1329 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001330 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001331 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001332 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001333 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001334 assert( TK_AND==OP_And );
1335 assert( TK_OR==OP_Or );
1336 assert( TK_PLUS==OP_Add );
1337 assert( TK_MINUS==OP_Subtract );
1338 assert( TK_REM==OP_Remainder );
1339 assert( TK_BITAND==OP_BitAnd );
1340 assert( TK_BITOR==OP_BitOr );
1341 assert( TK_SLASH==OP_Divide );
1342 assert( TK_LSHIFT==OP_ShiftLeft );
1343 assert( TK_RSHIFT==OP_ShiftRight );
1344 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001345 sqlite3ExprCode(pParse, pExpr->pLeft);
1346 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001347 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001348 break;
1349 }
drhcce7d172000-05-31 15:34:51 +00001350 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001351 Expr *pLeft = pExpr->pLeft;
1352 assert( pLeft );
1353 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1354 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001355 char *z = sqliteMalloc( p->n + 2 );
1356 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001357 if( pLeft->op==TK_FLOAT ){
1358 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001359 }else{
drhfec19aa2004-05-19 20:41:03 +00001360 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001361 }
drh6e142f52000-06-08 13:36:40 +00001362 sqliteFree(z);
1363 break;
1364 }
drh1ccde152000-06-17 13:12:39 +00001365 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001366 }
drhbf4133c2001-10-13 02:59:08 +00001367 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001368 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001369 assert( TK_BITNOT==OP_BitNot );
1370 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001371 sqlite3ExprCode(pParse, pExpr->pLeft);
1372 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001373 break;
1374 }
1375 case TK_ISNULL:
1376 case TK_NOTNULL: {
1377 int dest;
drhf2bc0132004-10-04 13:19:23 +00001378 assert( TK_ISNULL==OP_IsNull );
1379 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001380 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1381 sqlite3ExprCode(pParse, pExpr->pLeft);
1382 dest = sqlite3VdbeCurrentAddr(v) + 2;
1383 sqlite3VdbeAddOp(v, op, 1, dest);
1384 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001385 break;
drhcce7d172000-05-31 15:34:51 +00001386 }
drh22827922000-06-06 17:27:05 +00001387 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001388 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001389 break;
1390 }
danielk19777977a172004-11-09 12:44:37 +00001391 case TK_CDATE:
1392 case TK_CTIME:
1393 case TK_CTIMESTAMP:
drh4b59ab52002-08-24 18:24:51 +00001394 case TK_GLOB:
1395 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001396 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001397 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001398 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001399 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001400 int nId;
1401 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001402 int p2 = 0;
1403 int i;
danielk1977d8123362004-06-12 09:25:12 +00001404 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001405 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001406 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001407 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001408 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001409 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001410 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001411 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1412 p2 |= (1<<i);
1413 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001414 if( pDef->needCollSeq && !pColl ){
1415 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1416 }
1417 }
1418 if( pDef->needCollSeq ){
1419 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001420 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001421 }
1422 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001423 break;
1424 }
drh19a775c2000-06-05 18:54:46 +00001425 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001426 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001427 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001428 break;
1429 }
drhfef52082000-06-06 01:50:43 +00001430 case TK_IN: {
1431 int addr;
drh94a11212004-09-25 13:12:14 +00001432 char affinity;
danielk1977e014a832004-05-17 10:48:57 +00001433
1434 /* Figure out the affinity to use to create a key from the results
1435 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001436 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001437 */
drh94a11212004-09-25 13:12:14 +00001438 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001439
danielk19774adee202004-05-08 08:23:19 +00001440 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001441
1442 /* Code the <expr> from "<expr> IN (...)". The temporary table
1443 ** pExpr->iTable contains the values that make up the (...) set.
1444 */
danielk19774adee202004-05-08 08:23:19 +00001445 sqlite3ExprCode(pParse, pExpr->pLeft);
1446 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001447 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001448 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001449 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001450 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001451 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001452 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1453 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1454
drhfef52082000-06-06 01:50:43 +00001455 break;
1456 }
1457 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001458 Expr *pLeft = pExpr->pLeft;
1459 struct ExprList_item *pLItem = pExpr->pList->a;
1460 Expr *pRight = pLItem->pExpr;
1461 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001462 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001463 sqlite3ExprCode(pParse, pRight);
1464 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001465 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001466 pLItem++;
1467 pRight = pLItem->pExpr;
1468 sqlite3ExprCode(pParse, pRight);
1469 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001470 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001471 break;
1472 }
drh51e9a442004-01-16 16:42:53 +00001473 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001474 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001475 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001476 break;
1477 }
drh17a7f8d2002-03-24 13:13:27 +00001478 case TK_CASE: {
1479 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001480 int jumpInst;
1481 int addr;
1482 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001483 int i;
drhbe5c89a2004-07-26 00:31:09 +00001484 ExprList *pEList;
1485 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001486
1487 assert(pExpr->pList);
1488 assert((pExpr->pList->nExpr % 2) == 0);
1489 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001490 pEList = pExpr->pList;
1491 aListelem = pEList->a;
1492 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001493 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001494 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001495 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001496 }
drhf5905aa2002-05-26 20:54:33 +00001497 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001498 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001499 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001500 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001501 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1502 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001503 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001504 }else{
danielk19774adee202004-05-08 08:23:19 +00001505 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001506 }
drhbe5c89a2004-07-26 00:31:09 +00001507 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001508 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1509 addr = sqlite3VdbeCurrentAddr(v);
1510 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001511 }
drhf570f012002-05-31 15:51:25 +00001512 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001513 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001514 }
drh17a7f8d2002-03-24 13:13:27 +00001515 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001516 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001517 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001518 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001519 }
danielk19774adee202004-05-08 08:23:19 +00001520 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001521 break;
1522 }
1523 case TK_RAISE: {
1524 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001525 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001526 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001527 return;
1528 }
drhad6d9462004-09-19 02:15:24 +00001529 if( pExpr->iColumn!=OE_Ignore ){
1530 assert( pExpr->iColumn==OE_Rollback ||
1531 pExpr->iColumn == OE_Abort ||
1532 pExpr->iColumn == OE_Fail );
1533 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1534 pExpr->token.z, pExpr->token.n);
1535 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001536 } else {
drhad6d9462004-09-19 02:15:24 +00001537 assert( pExpr->iColumn == OE_Ignore );
1538 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1539 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1540 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001541 }
drh17a7f8d2002-03-24 13:13:27 +00001542 }
1543 break;
drhcce7d172000-05-31 15:34:51 +00001544 }
drhcce7d172000-05-31 15:34:51 +00001545}
1546
1547/*
drh268380c2004-02-25 13:47:31 +00001548** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001549** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001550**
1551** Return the number of elements pushed onto the stack.
1552*/
danielk19774adee202004-05-08 08:23:19 +00001553int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001554 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001555 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001556){
1557 struct ExprList_item *pItem;
1558 int i, n;
1559 Vdbe *v;
1560 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001561 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001562 n = pList->nExpr;
1563 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001564 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001565 }
drhf9b596e2004-05-26 16:54:42 +00001566 return n;
drh268380c2004-02-25 13:47:31 +00001567}
1568
1569/*
drhcce7d172000-05-31 15:34:51 +00001570** Generate code for a boolean expression such that a jump is made
1571** to the label "dest" if the expression is true but execution
1572** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001573**
1574** If the expression evaluates to NULL (neither true nor false), then
1575** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001576**
1577** This code depends on the fact that certain token values (ex: TK_EQ)
1578** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1579** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1580** the make process cause these values to align. Assert()s in the code
1581** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001582*/
danielk19774adee202004-05-08 08:23:19 +00001583void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001584 Vdbe *v = pParse->pVdbe;
1585 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001586 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001587 op = pExpr->op;
1588 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001589 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001590 int d2 = sqlite3VdbeMakeLabel(v);
1591 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1592 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1593 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001594 break;
1595 }
1596 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001597 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1598 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001599 break;
1600 }
1601 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001602 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001603 break;
1604 }
1605 case TK_LT:
1606 case TK_LE:
1607 case TK_GT:
1608 case TK_GE:
1609 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001610 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001611 assert( TK_LT==OP_Lt );
1612 assert( TK_LE==OP_Le );
1613 assert( TK_GT==OP_Gt );
1614 assert( TK_GE==OP_Ge );
1615 assert( TK_EQ==OP_Eq );
1616 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001617 sqlite3ExprCode(pParse, pExpr->pLeft);
1618 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001619 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001620 break;
1621 }
1622 case TK_ISNULL:
1623 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001624 assert( TK_ISNULL==OP_IsNull );
1625 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001626 sqlite3ExprCode(pParse, pExpr->pLeft);
1627 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001628 break;
1629 }
drhfef52082000-06-06 01:50:43 +00001630 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001631 /* The expression "x BETWEEN y AND z" is implemented as:
1632 **
1633 ** 1 IF (x < y) GOTO 3
1634 ** 2 IF (x <= z) GOTO <dest>
1635 ** 3 ...
1636 */
drhf5905aa2002-05-26 20:54:33 +00001637 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001638 Expr *pLeft = pExpr->pLeft;
1639 Expr *pRight = pExpr->pList->a[0].pExpr;
1640 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001641 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001642 sqlite3ExprCode(pParse, pRight);
1643 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001644
drhbe5c89a2004-07-26 00:31:09 +00001645 pRight = pExpr->pList->a[1].pExpr;
1646 sqlite3ExprCode(pParse, pRight);
1647 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001648
danielk19774adee202004-05-08 08:23:19 +00001649 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1650 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1651 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001652 break;
1653 }
drhcce7d172000-05-31 15:34:51 +00001654 default: {
danielk19774adee202004-05-08 08:23:19 +00001655 sqlite3ExprCode(pParse, pExpr);
1656 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001657 break;
1658 }
1659 }
1660}
1661
1662/*
drh66b89c82000-11-28 20:47:17 +00001663** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001664** to the label "dest" if the expression is false but execution
1665** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001666**
1667** If the expression evaluates to NULL (neither true nor false) then
1668** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001669*/
danielk19774adee202004-05-08 08:23:19 +00001670void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001671 Vdbe *v = pParse->pVdbe;
1672 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001673 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001674
1675 /* The value of pExpr->op and op are related as follows:
1676 **
1677 ** pExpr->op op
1678 ** --------- ----------
1679 ** TK_ISNULL OP_NotNull
1680 ** TK_NOTNULL OP_IsNull
1681 ** TK_NE OP_Eq
1682 ** TK_EQ OP_Ne
1683 ** TK_GT OP_Le
1684 ** TK_LE OP_Gt
1685 ** TK_GE OP_Lt
1686 ** TK_LT OP_Ge
1687 **
1688 ** For other values of pExpr->op, op is undefined and unused.
1689 ** The value of TK_ and OP_ constants are arranged such that we
1690 ** can compute the mapping above using the following expression.
1691 ** Assert()s verify that the computation is correct.
1692 */
1693 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1694
1695 /* Verify correct alignment of TK_ and OP_ constants
1696 */
1697 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1698 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1699 assert( pExpr->op!=TK_NE || op==OP_Eq );
1700 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1701 assert( pExpr->op!=TK_LT || op==OP_Ge );
1702 assert( pExpr->op!=TK_LE || op==OP_Gt );
1703 assert( pExpr->op!=TK_GT || op==OP_Le );
1704 assert( pExpr->op!=TK_GE || op==OP_Lt );
1705
drhcce7d172000-05-31 15:34:51 +00001706 switch( pExpr->op ){
1707 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001708 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1709 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001710 break;
1711 }
1712 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001713 int d2 = sqlite3VdbeMakeLabel(v);
1714 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1715 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1716 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001717 break;
1718 }
1719 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001720 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001721 break;
1722 }
1723 case TK_LT:
1724 case TK_LE:
1725 case TK_GT:
1726 case TK_GE:
1727 case TK_NE:
1728 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001729 sqlite3ExprCode(pParse, pExpr->pLeft);
1730 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001731 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001732 break;
1733 }
drhcce7d172000-05-31 15:34:51 +00001734 case TK_ISNULL:
1735 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001736 sqlite3ExprCode(pParse, pExpr->pLeft);
1737 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001738 break;
1739 }
drhfef52082000-06-06 01:50:43 +00001740 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001741 /* The expression is "x BETWEEN y AND z". It is implemented as:
1742 **
1743 ** 1 IF (x >= y) GOTO 3
1744 ** 2 GOTO <dest>
1745 ** 3 IF (x > z) GOTO <dest>
1746 */
drhfef52082000-06-06 01:50:43 +00001747 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001748 Expr *pLeft = pExpr->pLeft;
1749 Expr *pRight = pExpr->pList->a[0].pExpr;
1750 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001751 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001752 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001753 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001754 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1755
danielk19774adee202004-05-08 08:23:19 +00001756 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1757 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001758 pRight = pExpr->pList->a[1].pExpr;
1759 sqlite3ExprCode(pParse, pRight);
1760 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001761 break;
1762 }
drhcce7d172000-05-31 15:34:51 +00001763 default: {
danielk19774adee202004-05-08 08:23:19 +00001764 sqlite3ExprCode(pParse, pExpr);
1765 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001766 break;
1767 }
1768 }
1769}
drh22827922000-06-06 17:27:05 +00001770
1771/*
1772** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1773** if they are identical and return FALSE if they differ in any way.
1774*/
danielk19774adee202004-05-08 08:23:19 +00001775int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001776 int i;
1777 if( pA==0 ){
1778 return pB==0;
1779 }else if( pB==0 ){
1780 return 0;
1781 }
1782 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001783 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1784 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001785 if( pA->pList ){
1786 if( pB->pList==0 ) return 0;
1787 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1788 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001789 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001790 return 0;
1791 }
1792 }
1793 }else if( pB->pList ){
1794 return 0;
1795 }
1796 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001797 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001798 if( pA->token.z ){
1799 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001800 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001801 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001802 }
1803 return 1;
1804}
1805
1806/*
1807** Add a new element to the pParse->aAgg[] array and return its index.
1808*/
1809static int appendAggInfo(Parse *pParse){
1810 if( (pParse->nAgg & 0x7)==0 ){
1811 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001812 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1813 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001814 return -1;
1815 }
drh6d4abfb2001-10-22 02:58:08 +00001816 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001817 }
1818 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1819 return pParse->nAgg++;
1820}
1821
1822/*
1823** Analyze the given expression looking for aggregate functions and
1824** for variables that need to be added to the pParse->aAgg[] array.
1825** Make additional entries to the pParse->aAgg[] array as necessary.
1826**
1827** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001828** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001829**
1830** If errors are seen, leave an error message in zErrMsg and return
1831** the number of errors.
1832*/
danielk19774adee202004-05-08 08:23:19 +00001833int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001834 int i;
1835 AggExpr *aAgg;
1836 int nErr = 0;
1837
1838 if( pExpr==0 ) return 0;
1839 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001840 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001841 aAgg = pParse->aAgg;
1842 for(i=0; i<pParse->nAgg; i++){
1843 if( aAgg[i].isAgg ) continue;
1844 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001845 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001846 break;
1847 }
1848 }
1849 if( i>=pParse->nAgg ){
1850 i = appendAggInfo(pParse);
1851 if( i<0 ) return 1;
1852 pParse->aAgg[i].isAgg = 0;
1853 pParse->aAgg[i].pExpr = pExpr;
1854 }
drhaaf88722000-06-08 11:25:00 +00001855 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001856 break;
1857 }
1858 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001859 aAgg = pParse->aAgg;
1860 for(i=0; i<pParse->nAgg; i++){
1861 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001862 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001863 break;
1864 }
1865 }
1866 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001867 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001868 i = appendAggInfo(pParse);
1869 if( i<0 ) return 1;
1870 pParse->aAgg[i].isAgg = 1;
1871 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001872 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001873 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001874 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001875 }
1876 pExpr->iAgg = i;
1877 break;
1878 }
1879 default: {
1880 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001881 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001882 }
1883 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001884 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001885 }
1886 if( nErr==0 && pExpr->pList ){
1887 int n = pExpr->pList->nExpr;
1888 int i;
1889 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001890 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001891 }
1892 }
1893 break;
1894 }
1895 }
1896 return nErr;
1897}
drh8e0a2f92002-02-23 23:45:45 +00001898
1899/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001900** Locate a user function given a name, a number of arguments and a flag
1901** indicating whether the function prefers UTF-16 over UTF-8. Return a
1902** pointer to the FuncDef structure that defines that function, or return
1903** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001904**
drh0bce8352002-02-28 00:41:10 +00001905** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001906** structure is created and liked into the "db" structure if a
1907** no matching function previously existed. When createFlag is true
1908** and the nArg parameter is -1, then only a function that accepts
1909** any number of arguments will be returned.
1910**
1911** If createFlag is false and nArg is -1, then the first valid
1912** function found is returned. A function is valid if either xFunc
1913** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001914**
1915** If createFlag is false, then a function with the required name and
1916** number of arguments may be returned even if the eTextRep flag does not
1917** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001918*/
danielk19774adee202004-05-08 08:23:19 +00001919FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001920 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001921 const char *zName, /* Name of the function. Not null-terminated */
1922 int nName, /* Number of characters in the name */
1923 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001924 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001925 int createFlag /* Create new entry if true and does not otherwise exist */
1926){
danielk1977d02eb1f2004-06-06 09:44:03 +00001927 FuncDef *p; /* Iterator variable */
1928 FuncDef *pFirst; /* First function with this name */
1929 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001930 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001931
danielk1977d8123362004-06-12 09:25:12 +00001932
1933 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001934 if( nArg<-1 ) nArg = -1;
1935
1936 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1937 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001938 /* During the search for the best function definition, bestmatch is set
1939 ** as follows to indicate the quality of the match with the definition
1940 ** pointed to by pBest:
1941 **
1942 ** 0: pBest is NULL. No match has been found.
1943 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1944 ** encoding is requested, or vice versa.
1945 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1946 ** requested, or vice versa.
1947 ** 3: A variable arguments function using the same text encoding.
1948 ** 4: A function with the exact number of arguments requested that
1949 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1950 ** 5: A function with the exact number of arguments requested that
1951 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1952 ** 6: An exact match.
1953 **
1954 ** A larger value of 'matchqual' indicates a more desirable match.
1955 */
danielk1977e12c17b2004-06-23 12:35:14 +00001956 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001957 int match = 1; /* Quality of this match */
1958 if( p->nArg==nArg || nArg==-1 ){
1959 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001960 }
danielk1977d8123362004-06-12 09:25:12 +00001961 if( enc==p->iPrefEnc ){
1962 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001963 }
danielk1977d8123362004-06-12 09:25:12 +00001964 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1965 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1966 match += 1;
1967 }
1968
1969 if( match>bestmatch ){
1970 pBest = p;
1971 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001972 }
1973 }
drh8e0a2f92002-02-23 23:45:45 +00001974 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001975
danielk1977d8123362004-06-12 09:25:12 +00001976 /* If the createFlag parameter is true, and the seach did not reveal an
1977 ** exact match for the name, number of arguments and encoding, then add a
1978 ** new entry to the hash table and return it.
1979 */
1980 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001981 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1982 pBest->nArg = nArg;
1983 pBest->pNext = pFirst;
1984 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001985 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001986 memcpy(pBest->zName, zName, nName);
1987 pBest->zName[nName] = 0;
1988 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001989 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001990
1991 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1992 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001993 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001994 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001995}