blob: 7c19904278c412a91d6c94c09e21e170d9143f6e [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**
danielk19776c18b6e2005-01-30 09:17:58 +000015** $Id: expr.c,v 1.190 2005/01/30 09:17:59 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000037 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
40 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000041 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000042 }
43 return pExpr->affinity;
44}
45
drh53db1452004-05-20 13:54:53 +000046/*
danielk19770202b292004-06-09 09:55:16 +000047** Return the default collation sequence for the expression pExpr. If
48** there is no default collation type, return 0.
49*/
danielk19777cedc8d2004-06-10 10:50:08 +000050CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
51 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000052 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000053 pColl = pExpr->pColl;
54 if( pExpr->op==TK_AS && !pColl ){
55 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000056 }
57 }
danielk19777cedc8d2004-06-10 10:50:08 +000058 if( sqlite3CheckCollSeq(pParse, pColl) ){
59 pColl = 0;
60 }
61 return pColl;
danielk19770202b292004-06-09 09:55:16 +000062}
63
64/*
drh626a8792005-01-17 22:08:19 +000065** pExpr is an operand of a comparison operator. aff2 is the
66** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000067** 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*/
drhe4e72072004-11-23 01:47:30 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const 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);
drh73c42a12004-11-20 18:13:10 +0000223 if( p==0 ){
224 return 0; /* Malloc failed */
225 }
drh4e0cff62004-11-05 05:10:28 +0000226 depth = atoi(&pToken->z[1]);
drh2958a4e2004-11-12 03:56:15 +0000227 if( depth>=0 ){
228 p->iTable = pParse->nMem++;
229 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
230 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
231 }else{
232 p->iTable = -1-depth;
233 }
drh4e0cff62004-11-05 05:10:28 +0000234 return p;
235}
236
237/*
drh91bb0ee2004-09-01 03:06:34 +0000238** Join two expressions using an AND operator. If either expression is
239** NULL, then just return the other expression.
240*/
241Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
242 if( pLeft==0 ){
243 return pRight;
244 }else if( pRight==0 ){
245 return pLeft;
246 }else{
247 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
248 }
249}
250
251/*
drh6977fea2002-10-22 23:38:04 +0000252** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000253** text between the two given tokens.
254*/
danielk19774adee202004-05-08 08:23:19 +0000255void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000256 assert( pRight!=0 );
257 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000258 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000259 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000260 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000261 pExpr->span.z = pLeft->z;
262 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000263 }else{
drh6977fea2002-10-22 23:38:04 +0000264 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000265 }
drha76b5df2002-02-23 02:32:10 +0000266 }
267}
268
269/*
270** Construct a new expression node for a function with multiple
271** arguments.
272*/
danielk19774adee202004-05-08 08:23:19 +0000273Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000274 Expr *pNew;
275 pNew = sqliteMalloc( sizeof(Expr) );
276 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000277 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000278 return 0;
279 }
280 pNew->op = TK_FUNCTION;
281 pNew->pList = pList;
282 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000283 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000284 pNew->token = *pToken;
285 }else{
286 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000287 }
drh6977fea2002-10-22 23:38:04 +0000288 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000289 return pNew;
290}
291
292/*
drhfa6bc002004-09-07 16:19:52 +0000293** Assign a variable number to an expression that encodes a wildcard
294** in the original SQL statement.
295**
296** Wildcards consisting of a single "?" are assigned the next sequential
297** variable number.
298**
299** Wildcards of the form "?nnn" are assigned the number "nnn". We make
300** sure "nnn" is not too be to avoid a denial of service attack when
301** the SQL statement comes from an external source.
302**
303** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
304** as the previous instance of the same wildcard. Or if this is the first
305** instance of the wildcard, the next sequenial variable number is
306** assigned.
307*/
308void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
309 Token *pToken;
310 if( pExpr==0 ) return;
311 pToken = &pExpr->token;
312 assert( pToken->n>=1 );
313 assert( pToken->z!=0 );
314 assert( pToken->z[0]!=0 );
315 if( pToken->n==1 ){
316 /* Wildcard of the form "?". Assign the next variable number */
317 pExpr->iTable = ++pParse->nVar;
318 }else if( pToken->z[0]=='?' ){
319 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
320 ** use it as the variable number */
321 int i;
322 pExpr->iTable = i = atoi(&pToken->z[1]);
323 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
324 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
325 SQLITE_MAX_VARIABLE_NUMBER);
326 }
327 if( i>pParse->nVar ){
328 pParse->nVar = i;
329 }
330 }else{
331 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
332 ** number as the prior appearance of the same name, or if the name
333 ** has never appeared before, reuse the same variable number
334 */
335 int i, n;
336 n = pToken->n;
337 for(i=0; i<pParse->nVarExpr; i++){
338 Expr *pE;
339 if( (pE = pParse->apVarExpr[i])!=0
340 && pE->token.n==n
341 && memcmp(pE->token.z, pToken->z, n)==0 ){
342 pExpr->iTable = pE->iTable;
343 break;
344 }
345 }
346 if( i>=pParse->nVarExpr ){
347 pExpr->iTable = ++pParse->nVar;
348 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
349 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
350 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
351 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
352 }
353 if( !sqlite3_malloc_failed ){
354 assert( pParse->apVarExpr!=0 );
355 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
356 }
357 }
358 }
359}
360
361/*
drha2e00042002-01-22 03:13:42 +0000362** Recursively delete an expression tree.
363*/
danielk19774adee202004-05-08 08:23:19 +0000364void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000365 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000366 if( p->span.dyn ) sqliteFree((char*)p->span.z);
367 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000368 sqlite3ExprDelete(p->pLeft);
369 sqlite3ExprDelete(p->pRight);
370 sqlite3ExprListDelete(p->pList);
371 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000372 sqliteFree(p);
373}
374
drha76b5df2002-02-23 02:32:10 +0000375
376/*
drhff78bd22002-02-27 01:47:11 +0000377** The following group of routines make deep copies of expressions,
378** expression lists, ID lists, and select statements. The copies can
379** be deleted (by being passed to their respective ...Delete() routines)
380** without effecting the originals.
381**
danielk19774adee202004-05-08 08:23:19 +0000382** The expression list, ID, and source lists return by sqlite3ExprListDup(),
383** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000384** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000385**
drhad3cab52002-05-24 02:04:32 +0000386** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000387*/
danielk19774adee202004-05-08 08:23:19 +0000388Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000389 Expr *pNew;
390 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000391 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000392 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000393 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000394 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000395 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000396 pNew->token.dyn = 1;
397 }else{
drh4efc4752004-01-16 15:55:37 +0000398 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000399 }
drh6977fea2002-10-22 23:38:04 +0000400 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000401 pNew->pLeft = sqlite3ExprDup(p->pLeft);
402 pNew->pRight = sqlite3ExprDup(p->pRight);
403 pNew->pList = sqlite3ExprListDup(p->pList);
404 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000405 return pNew;
406}
danielk19774adee202004-05-08 08:23:19 +0000407void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000408 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000409 if( pFrom->z ){
410 pTo->n = pFrom->n;
411 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
412 pTo->dyn = 1;
413 }else{
drh4b59ab52002-08-24 18:24:51 +0000414 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000415 }
416}
danielk19774adee202004-05-08 08:23:19 +0000417ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000418 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000419 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000420 int i;
421 if( p==0 ) return 0;
422 pNew = sqliteMalloc( sizeof(*pNew) );
423 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000424 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000425 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000426 if( pItem==0 ){
427 sqliteFree(pNew);
428 return 0;
429 }
drh145716b2004-09-24 12:24:06 +0000430 pOldItem = p->a;
431 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000432 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000433 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000434 if( pOldExpr->span.z!=0 && pNewExpr ){
435 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000436 ** expression list. The logic in SELECT processing that determines
437 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000438 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000439 }
drh1f3e9052002-10-31 00:09:39 +0000440 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000441 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000442 pItem->zName = sqliteStrDup(pOldItem->zName);
443 pItem->sortOrder = pOldItem->sortOrder;
444 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000445 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000446 }
447 return pNew;
448}
danielk197793758c82005-01-21 08:13:14 +0000449
450/*
451** If cursors, triggers, views and subqueries are all omitted from
452** the build, then none of the following routines, except for
453** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
454** called with a NULL argument.
455*/
456#if !defined(SQLITE_OMIT_CURSOR) || !defined(SQLITE_OMIT_VIEW) \
457 || !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000458SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000459 SrcList *pNew;
460 int i;
drh113088e2003-03-20 01:16:58 +0000461 int nByte;
drhad3cab52002-05-24 02:04:32 +0000462 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000463 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000464 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000465 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000466 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000467 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000468 struct SrcList_item *pNewItem = &pNew->a[i];
469 struct SrcList_item *pOldItem = &p->a[i];
470 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
471 pNewItem->zName = sqliteStrDup(pOldItem->zName);
472 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
473 pNewItem->jointype = pOldItem->jointype;
474 pNewItem->iCursor = pOldItem->iCursor;
475 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000476 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
477 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
478 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000479 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000480 }
481 return pNew;
482}
danielk19774adee202004-05-08 08:23:19 +0000483IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000484 IdList *pNew;
485 int i;
486 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000487 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000488 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000489 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000490 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000491 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000492 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000493 struct IdList_item *pNewItem = &pNew->a[i];
494 struct IdList_item *pOldItem = &p->a[i];
495 pNewItem->zName = sqliteStrDup(pOldItem->zName);
496 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000497 }
498 return pNew;
499}
danielk19774adee202004-05-08 08:23:19 +0000500Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000501 Select *pNew;
502 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000503 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000504 if( pNew==0 ) return 0;
505 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000506 pNew->pEList = sqlite3ExprListDup(p->pEList);
507 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
508 pNew->pWhere = sqlite3ExprDup(p->pWhere);
509 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
510 pNew->pHaving = sqlite3ExprDup(p->pHaving);
511 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000512 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000513 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000514 pNew->nLimit = p->nLimit;
515 pNew->nOffset = p->nOffset;
drh7b58dae2003-07-20 01:16:46 +0000516 pNew->iLimit = -1;
517 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000518 pNew->ppOpenTemp = 0;
drhb6c29892004-11-22 19:12:19 +0000519 pNew->pFetch = 0;
danielk1977b3bce662005-01-29 08:32:43 +0000520 pNew->isResolved = 0;
521 pNew->isAgg = 0;
drhff78bd22002-02-27 01:47:11 +0000522 return pNew;
523}
danielk197793758c82005-01-21 08:13:14 +0000524#else
525Select *sqlite3SelectDup(Select *p){
526 assert( p==0 );
527 return 0;
528}
529#endif
drhff78bd22002-02-27 01:47:11 +0000530
531
532/*
drha76b5df2002-02-23 02:32:10 +0000533** Add a new element to the end of an expression list. If pList is
534** initially NULL, then create a new expression list.
535*/
danielk19774adee202004-05-08 08:23:19 +0000536ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000537 if( pList==0 ){
538 pList = sqliteMalloc( sizeof(ExprList) );
539 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000540 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000541 return 0;
542 }
drh4efc4752004-01-16 15:55:37 +0000543 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000544 }
drh4305d102003-07-30 12:34:12 +0000545 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000546 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000547 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
548 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000549 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000550 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000551 return pList;
552 }
drha76b5df2002-02-23 02:32:10 +0000553 }
drh4efc4752004-01-16 15:55:37 +0000554 assert( pList->a!=0 );
555 if( pExpr || pName ){
556 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
557 memset(pItem, 0, sizeof(*pItem));
558 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000559 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000560 }
561 return pList;
562}
563
564/*
565** Delete an entire expression list.
566*/
danielk19774adee202004-05-08 08:23:19 +0000567void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000568 int i;
drhbe5c89a2004-07-26 00:31:09 +0000569 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000570 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000571 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
572 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000573 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
574 sqlite3ExprDelete(pItem->pExpr);
575 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000576 }
577 sqliteFree(pList->a);
578 sqliteFree(pList);
579}
580
581/*
drh626a8792005-01-17 22:08:19 +0000582** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000583**
drh626a8792005-01-17 22:08:19 +0000584** The return value from xFunc determines whether the tree walk continues.
585** 0 means continue walking the tree. 1 means do not walk children
586** of the current node but continue with siblings. 2 means abandon
587** the tree walk completely.
588**
589** The return value from this routine is 1 to abandon the tree walk
590** and 0 to continue.
591*/
592static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
593 ExprList *pList;
594 int rc;
595 if( pExpr==0 ) return 0;
596 rc = (*xFunc)(pArg, pExpr);
597 if( rc==0 ){
598 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
599 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
600 pList = pExpr->pList;
601 if( pList ){
602 int i;
603 struct ExprList_item *pItem;
604 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
605 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
606 }
607 }
608 }
609 return rc>1;
610}
611
612/*
613** This routine is designed as an xFunc for walkExprTree().
614**
615** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000616** at pExpr that the expression that contains pExpr is not a constant
617** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
618** If pExpr does does not disqualify the expression from being a constant
619** then do nothing.
620**
621** After walking the whole tree, if no nodes are found that disqualify
622** the expression as constant, then we assume the whole expression
623** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000624*/
625static int exprNodeIsConstant(void *pArg, Expr *pExpr){
626 switch( pExpr->op ){
627 case TK_ID:
628 case TK_COLUMN:
629 case TK_DOT:
630 case TK_AGG_FUNCTION:
631 case TK_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000632#ifndef SQLITE_OMIT_SUBQUERY
633 case TK_SELECT:
634 case TK_EXISTS:
635#endif
drh626a8792005-01-17 22:08:19 +0000636 *((int*)pArg) = 0;
637 return 2;
638 default:
639 return 0;
640 }
641}
642
643/*
drhfef52082000-06-06 01:50:43 +0000644** Walk an expression tree. Return 1 if the expression is constant
645** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000646**
647** For the purposes of this function, a double-quoted string (ex: "abc")
648** is considered a variable but a single-quoted string (ex: 'abc') is
649** a constant.
drhfef52082000-06-06 01:50:43 +0000650*/
danielk19774adee202004-05-08 08:23:19 +0000651int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000652 int isConst = 1;
653 walkExprTree(p, exprNodeIsConstant, &isConst);
654 return isConst;
drhfef52082000-06-06 01:50:43 +0000655}
656
657/*
drh73b211a2005-01-18 04:00:42 +0000658** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000659** to fit in a 32-bit integer, return 1 and put the value of the integer
660** in *pValue. If the expression is not an integer or if it is too big
661** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000662*/
danielk19774adee202004-05-08 08:23:19 +0000663int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000664 switch( p->op ){
665 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000666 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000667 return 1;
668 }
669 break;
drhe4de1fe2002-06-02 16:09:01 +0000670 }
drh4b59ab52002-08-24 18:24:51 +0000671 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000672 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000673 }
drhe4de1fe2002-06-02 16:09:01 +0000674 case TK_UMINUS: {
675 int v;
danielk19774adee202004-05-08 08:23:19 +0000676 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000677 *pValue = -v;
678 return 1;
679 }
680 break;
681 }
682 default: break;
683 }
684 return 0;
685}
686
687/*
drhc4a3c772001-04-04 11:48:57 +0000688** Return TRUE if the given string is a row-id column name.
689*/
danielk19774adee202004-05-08 08:23:19 +0000690int sqlite3IsRowid(const char *z){
691 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
692 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
693 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000694 return 0;
695}
696
697/*
drh8141f612004-01-25 22:44:58 +0000698** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
699** that name in the set of source tables in pSrcList and make the pExpr
700** expression node refer back to that source column. The following changes
701** are made to pExpr:
702**
703** pExpr->iDb Set the index in db->aDb[] of the database holding
704** the table.
705** pExpr->iTable Set to the cursor number for the table obtained
706** from pSrcList.
707** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000708** pExpr->op Set to TK_COLUMN.
709** pExpr->pLeft Any expression this points to is deleted
710** pExpr->pRight Any expression this points to is deleted.
711**
712** The pDbToken is the name of the database (the "X"). This value may be
713** NULL meaning that name is of the form Y.Z or Z. Any available database
714** can be used. The pTableToken is the name of the table (the "Y"). This
715** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
716** means that the form of the name is Z and that columns from any table
717** can be used.
718**
719** If the name cannot be resolved unambiguously, leave an error message
720** in pParse and return non-zero. Return zero on success.
721*/
722static int lookupName(
723 Parse *pParse, /* The parsing context */
724 Token *pDbToken, /* Name of the database containing table, or NULL */
725 Token *pTableToken, /* Name of table containing column, or NULL */
726 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000727 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000728 Expr *pExpr /* Make this EXPR node point to the selected column */
729){
730 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
731 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
732 char *zCol = 0; /* Name of the column. The "Z" */
733 int i, j; /* Loop counters */
734 int cnt = 0; /* Number of matching column names */
735 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000736 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000737 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
738 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000739 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000740
741 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000742 zDb = sqlite3NameFromToken(pDbToken);
743 zTab = sqlite3NameFromToken(pTableToken);
744 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000745 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000746 return 1; /* Leak memory (zDb and zTab) if malloc fails */
747 }
drh8141f612004-01-25 22:44:58 +0000748
749 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000750 while( pNC && cnt==0 ){
751 SrcList *pSrcList = pNC->pSrcList;
752 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000753
drh626a8792005-01-17 22:08:19 +0000754 pNC->nRef++;
755 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000756 if( pSrcList ){
757 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
758 Table *pTab = pItem->pTab;
759 Column *pCol;
760
761 if( pTab==0 ) continue;
762 assert( pTab->nCol>0 );
763 if( zTab ){
764 if( pItem->zAlias ){
765 char *zTabName = pItem->zAlias;
766 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
767 }else{
768 char *zTabName = pTab->zName;
769 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
770 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
771 continue;
772 }
drh626a8792005-01-17 22:08:19 +0000773 }
drh8141f612004-01-25 22:44:58 +0000774 }
danielk1977b3bce662005-01-29 08:32:43 +0000775 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000776 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000777 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000778 pMatch = pItem;
779 }
780 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
781 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
782 cnt++;
783 pExpr->iTable = pItem->iCursor;
784 pMatch = pItem;
785 pExpr->iDb = pTab->iDb;
786 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
787 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
788 pExpr->affinity = pTab->aCol[j].affinity;
789 pExpr->pColl = pTab->aCol[j].pColl;
790 break;
791 }
drh8141f612004-01-25 22:44:58 +0000792 }
793 }
794 }
drh626a8792005-01-17 22:08:19 +0000795
796#ifndef SQLITE_OMIT_TRIGGER
797 /* If we have not already resolved the name, then maybe
798 ** it is a new.* or old.* trigger argument reference
799 */
800 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
801 TriggerStack *pTriggerStack = pParse->trigStack;
802 Table *pTab = 0;
803 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
804 pExpr->iTable = pTriggerStack->newIdx;
805 assert( pTriggerStack->pTab );
806 pTab = pTriggerStack->pTab;
807 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
808 pExpr->iTable = pTriggerStack->oldIdx;
809 assert( pTriggerStack->pTab );
810 pTab = pTriggerStack->pTab;
811 }
812
813 if( pTab ){
814 int j;
815 Column *pCol = pTab->aCol;
816
817 pExpr->iDb = pTab->iDb;
818 cntTab++;
819 for(j=0; j < pTab->nCol; j++, pCol++) {
820 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
821 cnt++;
822 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
823 pExpr->affinity = pTab->aCol[j].affinity;
824 pExpr->pColl = pTab->aCol[j].pColl;
825 break;
826 }
827 }
828 }
829 }
drhb7f91642004-10-31 02:22:47 +0000830#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000831
drh626a8792005-01-17 22:08:19 +0000832 /*
833 ** Perhaps the name is a reference to the ROWID
834 */
835 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
836 cnt = 1;
837 pExpr->iColumn = -1;
838 pExpr->affinity = SQLITE_AFF_INTEGER;
839 }
drh8141f612004-01-25 22:44:58 +0000840
drh626a8792005-01-17 22:08:19 +0000841 /*
842 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
843 ** might refer to an result-set alias. This happens, for example, when
844 ** we are resolving names in the WHERE clause of the following command:
845 **
846 ** SELECT a+b AS x FROM table WHERE x<10;
847 **
848 ** In cases like this, replace pExpr with a copy of the expression that
849 ** forms the result set entry ("a+b" in the example) and return immediately.
850 ** Note that the expression in the result set should have already been
851 ** resolved by the time the WHERE clause is resolved.
852 */
drh79d5f632005-01-18 17:20:10 +0000853 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000854 for(j=0; j<pEList->nExpr; j++){
855 char *zAs = pEList->a[j].zName;
856 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
857 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
858 pExpr->op = TK_AS;
859 pExpr->iColumn = j;
860 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
861 sqliteFree(zCol);
862 assert( zTab==0 && zDb==0 );
863 return 0;
864 }
865 }
866 }
867
868 /* Advance to the next name context. The loop will exit when either
869 ** we have a match (cnt>0) or when we run out of name contexts.
870 */
871 if( cnt==0 ){
872 pNC = pNC->pNext;
873 }
drh8141f612004-01-25 22:44:58 +0000874 }
875
876 /*
877 ** If X and Y are NULL (in other words if only the column name Z is
878 ** supplied) and the value of Z is enclosed in double-quotes, then
879 ** Z is a string literal if it doesn't match any column names. In that
880 ** case, we need to return right away and not make any changes to
881 ** pExpr.
882 */
883 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
884 sqliteFree(zCol);
885 return 0;
886 }
887
888 /*
889 ** cnt==0 means there was not match. cnt>1 means there were two or
890 ** more matches. Either way, we have an error.
891 */
892 if( cnt!=1 ){
893 char *z = 0;
894 char *zErr;
895 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
896 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000897 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000898 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000899 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000900 }else{
901 z = sqliteStrDup(zCol);
902 }
danielk19774adee202004-05-08 08:23:19 +0000903 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000904 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000905 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000906 }
907
drh51669862004-12-18 18:40:26 +0000908 /* If a column from a table in pSrcList is referenced, then record
909 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
910 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
911 ** column number is greater than the number of bits in the bitmask
912 ** then set the high-order bit of the bitmask.
913 */
914 if( pExpr->iColumn>=0 && pMatch!=0 ){
915 int n = pExpr->iColumn;
916 if( n>=sizeof(Bitmask)*8 ){
917 n = sizeof(Bitmask)*8-1;
918 }
919 assert( pMatch->iCursor==pExpr->iTable );
920 pMatch->colUsed |= 1<<n;
921 }
922
drh8141f612004-01-25 22:44:58 +0000923 /* Clean up and return
924 */
925 sqliteFree(zDb);
926 sqliteFree(zTab);
927 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000928 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000929 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000930 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000931 pExpr->pRight = 0;
932 pExpr->op = TK_COLUMN;
drh626a8792005-01-17 22:08:19 +0000933 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +0000934 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +0000935 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
936 }
drh8141f612004-01-25 22:44:58 +0000937 return cnt!=1;
938}
939
940/*
drh626a8792005-01-17 22:08:19 +0000941** pExpr is a node that defines a function of some kind. It might
942** be a syntactic function like "count(x)" or it might be a function
943** that implements an operator, like "a LIKE b".
944**
945** This routine makes *pzName point to the name of the function and
946** *pnName hold the number of characters in the function name.
947*/
948static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
949 switch( pExpr->op ){
950 case TK_FUNCTION: {
951 *pzName = pExpr->token.z;
952 *pnName = pExpr->token.n;
953 break;
954 }
955 case TK_LIKE: {
956 *pzName = "like";
957 *pnName = 4;
958 break;
959 }
960 case TK_GLOB: {
961 *pzName = "glob";
962 *pnName = 4;
963 break;
964 }
965 case TK_CTIME: {
966 *pzName = "current_time";
967 *pnName = 12;
968 break;
969 }
970 case TK_CDATE: {
971 *pzName = "current_date";
972 *pnName = 12;
973 break;
974 }
975 case TK_CTIMESTAMP: {
976 *pzName = "current_timestamp";
977 *pnName = 17;
978 break;
979 }
drh626a8792005-01-17 22:08:19 +0000980 }
981}
982
983/*
984** This routine is designed as an xFunc for walkExprTree().
985**
drh73b211a2005-01-18 04:00:42 +0000986** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +0000987** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +0000988** the tree or 2 to abort the tree walk.
989**
990** This routine also does error checking and name resolution for
991** function names. The operator for aggregate functions is changed
992** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +0000993*/
994static int nameResolverStep(void *pArg, Expr *pExpr){
995 NameContext *pNC = (NameContext*)pArg;
996 SrcList *pSrcList;
997 Parse *pParse;
drh626a8792005-01-17 22:08:19 +0000998
danielk1977b3bce662005-01-29 08:32:43 +0000999 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001000 assert( pNC!=0 );
1001 pSrcList = pNC->pSrcList;
1002 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001003
drh626a8792005-01-17 22:08:19 +00001004 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1005 ExprSetProperty(pExpr, EP_Resolved);
1006#ifndef NDEBUG
1007 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001008 int i;
drh626a8792005-01-17 22:08:19 +00001009 for(i=0; i<pSrcList->nSrc; i++){
1010 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1011 }
1012 }
1013#endif
1014 switch( pExpr->op ){
1015 /* Double-quoted strings (ex: "abc") are used as identifiers if
1016 ** possible. Otherwise they remain as strings. Single-quoted
1017 ** strings (ex: 'abc') are always string literals.
1018 */
1019 case TK_STRING: {
1020 if( pExpr->token.z[0]=='\'' ) break;
1021 /* Fall thru into the TK_ID case if this is a double-quoted string */
1022 }
1023 /* A lone identifier is the name of a column.
1024 */
1025 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001026 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1027 return 1;
1028 }
1029
1030 /* A table name and column name: ID.ID
1031 ** Or a database, table and column: ID.ID.ID
1032 */
1033 case TK_DOT: {
1034 Token *pColumn;
1035 Token *pTable;
1036 Token *pDb;
1037 Expr *pRight;
1038
danielk1977b3bce662005-01-29 08:32:43 +00001039 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001040 pRight = pExpr->pRight;
1041 if( pRight->op==TK_ID ){
1042 pDb = 0;
1043 pTable = &pExpr->pLeft->token;
1044 pColumn = &pRight->token;
1045 }else{
1046 assert( pRight->op==TK_DOT );
1047 pDb = &pExpr->pLeft->token;
1048 pTable = &pRight->pLeft->token;
1049 pColumn = &pRight->pRight->token;
1050 }
1051 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1052 return 1;
1053 }
1054
1055 /* Resolve function names
1056 */
1057 case TK_CTIME:
1058 case TK_CTIMESTAMP:
1059 case TK_CDATE:
drh626a8792005-01-17 22:08:19 +00001060 case TK_GLOB:
1061 case TK_LIKE:
1062 case TK_FUNCTION: {
1063 ExprList *pList = pExpr->pList; /* The argument list */
1064 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1065 int no_such_func = 0; /* True if no such function exists */
1066 int wrong_num_args = 0; /* True if wrong number of arguments */
1067 int is_agg = 0; /* True if is an aggregate function */
1068 int i;
1069 int nId; /* Number of characters in function name */
1070 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001071 FuncDef *pDef; /* Information about the function */
1072 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001073
1074 getFunctionName(pExpr, &zId, &nId);
1075 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1076 if( pDef==0 ){
1077 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1078 if( pDef==0 ){
1079 no_such_func = 1;
1080 }else{
1081 wrong_num_args = 1;
1082 }
1083 }else{
1084 is_agg = pDef->xFunc==0;
1085 }
1086 if( is_agg && !pNC->allowAgg ){
1087 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1088 pNC->nErr++;
1089 is_agg = 0;
1090 }else if( no_such_func ){
1091 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1092 pNC->nErr++;
1093 }else if( wrong_num_args ){
1094 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1095 nId, zId);
1096 pNC->nErr++;
1097 }
1098 if( is_agg ){
1099 pExpr->op = TK_AGG_FUNCTION;
1100 pNC->hasAgg = 1;
1101 }
drh73b211a2005-01-18 04:00:42 +00001102 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001103 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001104 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001105 }
drh73b211a2005-01-18 04:00:42 +00001106 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001107 /* FIX ME: Compute pExpr->affinity based on the expected return
1108 ** type of the function
1109 */
1110 return is_agg;
1111 }
danielk1977b3bce662005-01-29 08:32:43 +00001112#ifndef SQLITE_OMIT_SUBQUERY
1113 case TK_SELECT:
1114 case TK_EXISTS:
1115#endif
1116 case TK_IN: {
1117 if( pExpr->pSelect ){
1118 int nRef = pNC->nRef;
1119 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1120 assert( pNC->nRef>=nRef );
1121 if( nRef!=pNC->nRef ){
1122 ExprSetProperty(pExpr, EP_VarSelect);
1123 }
1124 }
1125 }
drh626a8792005-01-17 22:08:19 +00001126 }
1127 return 0;
1128}
1129
1130/*
drhcce7d172000-05-31 15:34:51 +00001131** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001132** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001133** index to the table in the table list and a column offset. The
1134** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1135** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001136** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001137** VDBE cursor number for a cursor that is pointing into the referenced
1138** table. The Expr.iColumn value is changed to the index of the column
1139** of the referenced table. The Expr.iColumn value for the special
1140** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1141** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001142**
drh626a8792005-01-17 22:08:19 +00001143** Also resolve function names and check the functions for proper
1144** usage. Make sure all function names are recognized and all functions
1145** have the correct number of arguments. Leave an error message
1146** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1147**
drh73b211a2005-01-18 04:00:42 +00001148** If the expression contains aggregate functions then set the EP_Agg
1149** property on the expression.
drh626a8792005-01-17 22:08:19 +00001150*/
1151int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001152 NameContext *pNC, /* Namespace to resolve expressions in. */
1153 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001154){
drh73b211a2005-01-18 04:00:42 +00001155 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001156 walkExprTree(pExpr, nameResolverStep, pNC);
1157 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001158 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001159 }
1160 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001161}
1162
drh1398ad32005-01-19 23:24:50 +00001163/*
1164** A pointer instance of this structure is used to pass information
1165** through walkExprTree into codeSubqueryStep().
1166*/
1167typedef struct QueryCoder QueryCoder;
1168struct QueryCoder {
1169 Parse *pParse; /* The parsing context */
1170 NameContext *pNC; /* Namespace of first enclosing query */
1171};
1172
drh626a8792005-01-17 22:08:19 +00001173
1174/*
1175** Generate code for subqueries and IN operators.
1176**
drh73b211a2005-01-18 04:00:42 +00001177** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001178**
1179** expr IN (exprlist)
1180** and
1181** expr IN (SELECT ...)
1182**
1183** The first form is handled by creating a set holding the list
1184** of allowed values. The second form causes the SELECT to generate
1185** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001186*/
drh51522cd2005-01-20 13:36:19 +00001187#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001188void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1189 int label = 0; /* Address after sub-select code */
1190 Vdbe *v = sqlite3GetVdbe(pParse);
1191 if( v==0 ) return;
1192
1193 /* If this is not a variable (correlated) select, then execute
1194 ** it only once. Unless this is part of a trigger program. In
1195 ** that case re-execute every time (this could be optimized).
1196 */
1197 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1198 int mem = pParse->nMem++;
1199 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1200 label = sqlite3VdbeMakeLabel(v);
1201 sqlite3VdbeAddOp(v, OP_If, 0, label);
1202 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1203 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1204 }
1205
1206 if( pExpr->pSelect ){
1207 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1208 }
drh6a3ea0e2003-05-02 14:32:12 +00001209
drhcce7d172000-05-31 15:34:51 +00001210 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001211 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001212 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001213 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001214 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001215
danielk1977bf3b7212004-05-18 10:06:24 +00001216 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001217
1218 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1219 ** expression it is handled the same way. A temporary table is
1220 ** filled with single-field index keys representing the results
1221 ** from the SELECT or the <exprlist>.
1222 **
1223 ** If the 'x' expression is a column value, or the SELECT...
1224 ** statement returns a column value, then the affinity of that
1225 ** column is used to build the index keys. If both 'x' and the
1226 ** SELECT... statement are columns, then numeric affinity is used
1227 ** if either column has NUMERIC or INTEGER affinity. If neither
1228 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1229 ** is used.
1230 */
1231 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001232 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001233 memset(&keyInfo, 0, sizeof(keyInfo));
1234 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001235 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001236
drhfef52082000-06-06 01:50:43 +00001237 if( pExpr->pSelect ){
1238 /* Case 1: expr IN (SELECT ...)
1239 **
danielk1977e014a832004-05-17 10:48:57 +00001240 ** Generate code to write the results of the select into the temporary
1241 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001242 */
danielk1977e014a832004-05-17 10:48:57 +00001243 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001244 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001245 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001246 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001247 pEList = pExpr->pSelect->pEList;
1248 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001249 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001250 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001251 }
drhfef52082000-06-06 01:50:43 +00001252 }else if( pExpr->pList ){
1253 /* Case 2: expr IN (exprlist)
1254 **
danielk1977e014a832004-05-17 10:48:57 +00001255 ** For each expression, build an index key from the evaluation and
1256 ** store it in the temporary table. If <expr> is a column, then use
1257 ** that columns affinity when building index keys. If <expr> is not
1258 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001259 */
danielk1977e014a832004-05-17 10:48:57 +00001260 int i;
danielk1977e014a832004-05-17 10:48:57 +00001261 if( !affinity ){
1262 affinity = SQLITE_AFF_NUMERIC;
1263 }
danielk19770202b292004-06-09 09:55:16 +00001264 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001265
1266 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001267 for(i=0; i<pExpr->pList->nExpr; i++){
1268 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001269
1270 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001271 if( !sqlite3ExprIsConstant(pE2) ){
1272 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001273 "right-hand side of IN operator must be constant");
danielk1977b3bce662005-01-29 08:32:43 +00001274 return;
drh4794b982000-06-06 13:54:14 +00001275 }
danielk1977e014a832004-05-17 10:48:57 +00001276
1277 /* Evaluate the expression and insert it into the temp table */
1278 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001279 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001280 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001281 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001282 }
1283 }
danielk19770202b292004-06-09 09:55:16 +00001284 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001285 break;
drhfef52082000-06-06 01:50:43 +00001286 }
1287
drh51522cd2005-01-20 13:36:19 +00001288 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001289 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001290 /* This has to be a scalar SELECT. Generate code to put the
1291 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001292 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001293 */
drh51522cd2005-01-20 13:36:19 +00001294 int sop;
1295 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001296
drh967e8b72000-06-21 13:59:10 +00001297 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001298 pSel = pExpr->pSelect;
1299 if( pExpr->op==TK_SELECT ){
1300 sop = SRT_Mem;
1301 }else{
1302 static const Token one = { "1", 0, 1 };
1303 sop = SRT_Exists;
1304 sqlite3ExprListDelete(pSel->pEList);
1305 pSel->pEList = sqlite3ExprListAppend(0,
1306 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1307 }
danielk1977b3bce662005-01-29 08:32:43 +00001308 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1309 break;
drhcce7d172000-05-31 15:34:51 +00001310 }
1311 }
danielk1977b3bce662005-01-29 08:32:43 +00001312
1313 if( pExpr->pSelect ){
1314 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1315 }
1316 if( label<0 ){
1317 sqlite3VdbeResolveLabel(v, label);
1318 }
1319 return;
drhcce7d172000-05-31 15:34:51 +00001320}
drh51522cd2005-01-20 13:36:19 +00001321#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001322
drhcce7d172000-05-31 15:34:51 +00001323/*
drhfec19aa2004-05-19 20:41:03 +00001324** Generate an instruction that will put the integer describe by
1325** text z[0..n-1] on the stack.
1326*/
1327static void codeInteger(Vdbe *v, const char *z, int n){
1328 int i;
drh6fec0762004-05-30 01:38:43 +00001329 if( sqlite3GetInt32(z, &i) ){
1330 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1331 }else if( sqlite3FitsIn64Bits(z) ){
1332 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001333 }else{
1334 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1335 }
1336}
1337
1338/*
drhcce7d172000-05-31 15:34:51 +00001339** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001340** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001341**
1342** This code depends on the fact that certain token values (ex: TK_EQ)
1343** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1344** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1345** the make process cause these values to align. Assert()s in the code
1346** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001347*/
danielk19774adee202004-05-08 08:23:19 +00001348void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001349 Vdbe *v = pParse->pVdbe;
1350 int op;
danielk19777977a172004-11-09 12:44:37 +00001351 if( v==0 ) return;
1352 if( pExpr==0 ){
1353 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1354 return;
1355 }
drhf2bc0132004-10-04 13:19:23 +00001356 op = pExpr->op;
1357 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001358 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001359 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001360 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001361 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001362 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001363#ifndef NDEBUG
1364 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1365 VdbeComment((v, "# %T", &pExpr->span));
1366 }
1367#endif
drhc4a3c772001-04-04 11:48:57 +00001368 }else{
danielk19774adee202004-05-08 08:23:19 +00001369 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001370 }
drhcce7d172000-05-31 15:34:51 +00001371 break;
1372 }
1373 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001374 codeInteger(v, pExpr->token.z, pExpr->token.n);
1375 break;
1376 }
1377 case TK_FLOAT:
1378 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001379 assert( TK_FLOAT==OP_Real );
1380 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001381 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001382 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001383 break;
1384 }
danielk19775338a5f2005-01-20 13:03:10 +00001385#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001386 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001387 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001388 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1389 sqlite3VdbeDequoteP3(v, -1);
1390 break;
1391 }
danielk19775338a5f2005-01-20 13:03:10 +00001392#endif
drhcce7d172000-05-31 15:34:51 +00001393 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001394 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001395 break;
1396 }
drh50457892003-09-06 01:10:47 +00001397 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001398 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001399 if( pExpr->token.n>1 ){
1400 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1401 }
drh50457892003-09-06 01:10:47 +00001402 break;
1403 }
drh4e0cff62004-11-05 05:10:28 +00001404 case TK_REGISTER: {
1405 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1406 break;
1407 }
drhc9b84a12002-06-20 11:36:48 +00001408 case TK_LT:
1409 case TK_LE:
1410 case TK_GT:
1411 case TK_GE:
1412 case TK_NE:
1413 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001414 assert( TK_LT==OP_Lt );
1415 assert( TK_LE==OP_Le );
1416 assert( TK_GT==OP_Gt );
1417 assert( TK_GE==OP_Ge );
1418 assert( TK_EQ==OP_Eq );
1419 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001420 sqlite3ExprCode(pParse, pExpr->pLeft);
1421 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001422 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001423 break;
drhc9b84a12002-06-20 11:36:48 +00001424 }
drhcce7d172000-05-31 15:34:51 +00001425 case TK_AND:
1426 case TK_OR:
1427 case TK_PLUS:
1428 case TK_STAR:
1429 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001430 case TK_REM:
1431 case TK_BITAND:
1432 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001433 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001434 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001435 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001436 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001437 assert( TK_AND==OP_And );
1438 assert( TK_OR==OP_Or );
1439 assert( TK_PLUS==OP_Add );
1440 assert( TK_MINUS==OP_Subtract );
1441 assert( TK_REM==OP_Remainder );
1442 assert( TK_BITAND==OP_BitAnd );
1443 assert( TK_BITOR==OP_BitOr );
1444 assert( TK_SLASH==OP_Divide );
1445 assert( TK_LSHIFT==OP_ShiftLeft );
1446 assert( TK_RSHIFT==OP_ShiftRight );
1447 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001448 sqlite3ExprCode(pParse, pExpr->pLeft);
1449 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001450 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001451 break;
1452 }
drhcce7d172000-05-31 15:34:51 +00001453 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001454 Expr *pLeft = pExpr->pLeft;
1455 assert( pLeft );
1456 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1457 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001458 char *z = sqliteMalloc( p->n + 2 );
1459 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001460 if( pLeft->op==TK_FLOAT ){
1461 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001462 }else{
drhfec19aa2004-05-19 20:41:03 +00001463 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001464 }
drh6e142f52000-06-08 13:36:40 +00001465 sqliteFree(z);
1466 break;
1467 }
drh1ccde152000-06-17 13:12:39 +00001468 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001469 }
drhbf4133c2001-10-13 02:59:08 +00001470 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001471 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001472 assert( TK_BITNOT==OP_BitNot );
1473 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001474 sqlite3ExprCode(pParse, pExpr->pLeft);
1475 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001476 break;
1477 }
1478 case TK_ISNULL:
1479 case TK_NOTNULL: {
1480 int dest;
drhf2bc0132004-10-04 13:19:23 +00001481 assert( TK_ISNULL==OP_IsNull );
1482 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001483 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1484 sqlite3ExprCode(pParse, pExpr->pLeft);
1485 dest = sqlite3VdbeCurrentAddr(v) + 2;
1486 sqlite3VdbeAddOp(v, op, 1, dest);
1487 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001488 break;
drhcce7d172000-05-31 15:34:51 +00001489 }
drh22827922000-06-06 17:27:05 +00001490 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001491 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001492 break;
1493 }
danielk19777977a172004-11-09 12:44:37 +00001494 case TK_CDATE:
1495 case TK_CTIME:
1496 case TK_CTIMESTAMP:
drh4b59ab52002-08-24 18:24:51 +00001497 case TK_GLOB:
1498 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001499 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001500 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001501 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001502 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001503 int nId;
1504 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001505 int p2 = 0;
1506 int i;
danielk1977d8123362004-06-12 09:25:12 +00001507 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001508 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001509 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001510 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001511 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001512 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001513 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001514 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1515 p2 |= (1<<i);
1516 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001517 if( pDef->needCollSeq && !pColl ){
1518 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1519 }
1520 }
1521 if( pDef->needCollSeq ){
1522 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001523 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001524 }
1525 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001526 break;
1527 }
drhfe2093d2005-01-20 22:48:47 +00001528#ifndef SQLITE_OMIT_SUBQUERY
1529 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001530 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001531 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001532 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001533 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001534 break;
1535 }
drhfef52082000-06-06 01:50:43 +00001536 case TK_IN: {
1537 int addr;
drh94a11212004-09-25 13:12:14 +00001538 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001539 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001540
1541 /* Figure out the affinity to use to create a key from the results
1542 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001543 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001544 */
drh94a11212004-09-25 13:12:14 +00001545 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001546
danielk19774adee202004-05-08 08:23:19 +00001547 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001548
1549 /* Code the <expr> from "<expr> IN (...)". The temporary table
1550 ** pExpr->iTable contains the values that make up the (...) set.
1551 */
danielk19774adee202004-05-08 08:23:19 +00001552 sqlite3ExprCode(pParse, pExpr->pLeft);
1553 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001554 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001555 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001556 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001557 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001558 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001559 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1560 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1561
drhfef52082000-06-06 01:50:43 +00001562 break;
1563 }
danielk197793758c82005-01-21 08:13:14 +00001564#endif
drhfef52082000-06-06 01:50:43 +00001565 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001566 Expr *pLeft = pExpr->pLeft;
1567 struct ExprList_item *pLItem = pExpr->pList->a;
1568 Expr *pRight = pLItem->pExpr;
1569 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001570 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001571 sqlite3ExprCode(pParse, pRight);
1572 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001573 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001574 pLItem++;
1575 pRight = pLItem->pExpr;
1576 sqlite3ExprCode(pParse, pRight);
1577 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001578 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001579 break;
1580 }
drh51e9a442004-01-16 16:42:53 +00001581 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001582 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001583 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001584 break;
1585 }
drh17a7f8d2002-03-24 13:13:27 +00001586 case TK_CASE: {
1587 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001588 int jumpInst;
1589 int addr;
1590 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001591 int i;
drhbe5c89a2004-07-26 00:31:09 +00001592 ExprList *pEList;
1593 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001594
1595 assert(pExpr->pList);
1596 assert((pExpr->pList->nExpr % 2) == 0);
1597 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001598 pEList = pExpr->pList;
1599 aListelem = pEList->a;
1600 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001601 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001602 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001603 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001604 }
drhf5905aa2002-05-26 20:54:33 +00001605 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001606 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001607 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001608 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001609 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1610 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001611 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001612 }else{
danielk19774adee202004-05-08 08:23:19 +00001613 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001614 }
drhbe5c89a2004-07-26 00:31:09 +00001615 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001616 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1617 addr = sqlite3VdbeCurrentAddr(v);
1618 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001619 }
drhf570f012002-05-31 15:51:25 +00001620 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001621 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001622 }
drh17a7f8d2002-03-24 13:13:27 +00001623 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001624 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001625 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001626 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001627 }
danielk19774adee202004-05-08 08:23:19 +00001628 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001629 break;
1630 }
danielk19775338a5f2005-01-20 13:03:10 +00001631#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001632 case TK_RAISE: {
1633 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001634 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001635 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001636 return;
1637 }
drhad6d9462004-09-19 02:15:24 +00001638 if( pExpr->iColumn!=OE_Ignore ){
1639 assert( pExpr->iColumn==OE_Rollback ||
1640 pExpr->iColumn == OE_Abort ||
1641 pExpr->iColumn == OE_Fail );
1642 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1643 pExpr->token.z, pExpr->token.n);
1644 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001645 } else {
drhad6d9462004-09-19 02:15:24 +00001646 assert( pExpr->iColumn == OE_Ignore );
1647 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1648 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1649 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001650 }
drh17a7f8d2002-03-24 13:13:27 +00001651 }
danielk19775338a5f2005-01-20 13:03:10 +00001652#endif
drh17a7f8d2002-03-24 13:13:27 +00001653 break;
drhcce7d172000-05-31 15:34:51 +00001654 }
drhcce7d172000-05-31 15:34:51 +00001655}
1656
danielk197793758c82005-01-21 08:13:14 +00001657#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001658/*
drh25303782004-12-07 15:41:48 +00001659** Generate code that evalutes the given expression and leaves the result
1660** on the stack. See also sqlite3ExprCode().
1661**
1662** This routine might also cache the result and modify the pExpr tree
1663** so that it will make use of the cached result on subsequent evaluations
1664** rather than evaluate the whole expression again. Trivial expressions are
1665** not cached. If the expression is cached, its result is stored in a
1666** memory location.
1667*/
1668void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1669 Vdbe *v = pParse->pVdbe;
1670 int iMem;
1671 int addr1, addr2;
1672 if( v==0 ) return;
1673 addr1 = sqlite3VdbeCurrentAddr(v);
1674 sqlite3ExprCode(pParse, pExpr);
1675 addr2 = sqlite3VdbeCurrentAddr(v);
1676 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1677 iMem = pExpr->iTable = pParse->nMem++;
1678 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1679 pExpr->op = TK_REGISTER;
1680 }
1681}
danielk197793758c82005-01-21 08:13:14 +00001682#endif
drh25303782004-12-07 15:41:48 +00001683
1684/*
drh268380c2004-02-25 13:47:31 +00001685** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001686** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001687**
1688** Return the number of elements pushed onto the stack.
1689*/
danielk19774adee202004-05-08 08:23:19 +00001690int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001691 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001692 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001693){
1694 struct ExprList_item *pItem;
1695 int i, n;
1696 Vdbe *v;
1697 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001698 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001699 n = pList->nExpr;
1700 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001701 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001702 }
drhf9b596e2004-05-26 16:54:42 +00001703 return n;
drh268380c2004-02-25 13:47:31 +00001704}
1705
1706/*
drhcce7d172000-05-31 15:34:51 +00001707** Generate code for a boolean expression such that a jump is made
1708** to the label "dest" if the expression is true but execution
1709** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001710**
1711** If the expression evaluates to NULL (neither true nor false), then
1712** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001713**
1714** This code depends on the fact that certain token values (ex: TK_EQ)
1715** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1716** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1717** the make process cause these values to align. Assert()s in the code
1718** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001719*/
danielk19774adee202004-05-08 08:23:19 +00001720void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001721 Vdbe *v = pParse->pVdbe;
1722 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001723 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001724 op = pExpr->op;
1725 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001726 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001727 int d2 = sqlite3VdbeMakeLabel(v);
1728 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1729 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1730 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001731 break;
1732 }
1733 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001734 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1735 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001736 break;
1737 }
1738 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001739 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001740 break;
1741 }
1742 case TK_LT:
1743 case TK_LE:
1744 case TK_GT:
1745 case TK_GE:
1746 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001747 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001748 assert( TK_LT==OP_Lt );
1749 assert( TK_LE==OP_Le );
1750 assert( TK_GT==OP_Gt );
1751 assert( TK_GE==OP_Ge );
1752 assert( TK_EQ==OP_Eq );
1753 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001754 sqlite3ExprCode(pParse, pExpr->pLeft);
1755 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001756 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001757 break;
1758 }
1759 case TK_ISNULL:
1760 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001761 assert( TK_ISNULL==OP_IsNull );
1762 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001763 sqlite3ExprCode(pParse, pExpr->pLeft);
1764 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001765 break;
1766 }
drhfef52082000-06-06 01:50:43 +00001767 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001768 /* The expression "x BETWEEN y AND z" is implemented as:
1769 **
1770 ** 1 IF (x < y) GOTO 3
1771 ** 2 IF (x <= z) GOTO <dest>
1772 ** 3 ...
1773 */
drhf5905aa2002-05-26 20:54:33 +00001774 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001775 Expr *pLeft = pExpr->pLeft;
1776 Expr *pRight = pExpr->pList->a[0].pExpr;
1777 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001778 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001779 sqlite3ExprCode(pParse, pRight);
1780 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001781
drhbe5c89a2004-07-26 00:31:09 +00001782 pRight = pExpr->pList->a[1].pExpr;
1783 sqlite3ExprCode(pParse, pRight);
1784 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001785
danielk19774adee202004-05-08 08:23:19 +00001786 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1787 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1788 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001789 break;
1790 }
drhcce7d172000-05-31 15:34:51 +00001791 default: {
danielk19774adee202004-05-08 08:23:19 +00001792 sqlite3ExprCode(pParse, pExpr);
1793 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001794 break;
1795 }
1796 }
1797}
1798
1799/*
drh66b89c82000-11-28 20:47:17 +00001800** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001801** to the label "dest" if the expression is false but execution
1802** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001803**
1804** If the expression evaluates to NULL (neither true nor false) then
1805** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001806*/
danielk19774adee202004-05-08 08:23:19 +00001807void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001808 Vdbe *v = pParse->pVdbe;
1809 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001810 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001811
1812 /* The value of pExpr->op and op are related as follows:
1813 **
1814 ** pExpr->op op
1815 ** --------- ----------
1816 ** TK_ISNULL OP_NotNull
1817 ** TK_NOTNULL OP_IsNull
1818 ** TK_NE OP_Eq
1819 ** TK_EQ OP_Ne
1820 ** TK_GT OP_Le
1821 ** TK_LE OP_Gt
1822 ** TK_GE OP_Lt
1823 ** TK_LT OP_Ge
1824 **
1825 ** For other values of pExpr->op, op is undefined and unused.
1826 ** The value of TK_ and OP_ constants are arranged such that we
1827 ** can compute the mapping above using the following expression.
1828 ** Assert()s verify that the computation is correct.
1829 */
1830 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1831
1832 /* Verify correct alignment of TK_ and OP_ constants
1833 */
1834 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1835 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1836 assert( pExpr->op!=TK_NE || op==OP_Eq );
1837 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1838 assert( pExpr->op!=TK_LT || op==OP_Ge );
1839 assert( pExpr->op!=TK_LE || op==OP_Gt );
1840 assert( pExpr->op!=TK_GT || op==OP_Le );
1841 assert( pExpr->op!=TK_GE || op==OP_Lt );
1842
drhcce7d172000-05-31 15:34:51 +00001843 switch( pExpr->op ){
1844 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001845 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1846 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001847 break;
1848 }
1849 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001850 int d2 = sqlite3VdbeMakeLabel(v);
1851 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1852 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1853 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001854 break;
1855 }
1856 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001857 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001858 break;
1859 }
1860 case TK_LT:
1861 case TK_LE:
1862 case TK_GT:
1863 case TK_GE:
1864 case TK_NE:
1865 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001866 sqlite3ExprCode(pParse, pExpr->pLeft);
1867 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001868 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001869 break;
1870 }
drhcce7d172000-05-31 15:34:51 +00001871 case TK_ISNULL:
1872 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001873 sqlite3ExprCode(pParse, pExpr->pLeft);
1874 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001875 break;
1876 }
drhfef52082000-06-06 01:50:43 +00001877 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001878 /* The expression is "x BETWEEN y AND z". It is implemented as:
1879 **
1880 ** 1 IF (x >= y) GOTO 3
1881 ** 2 GOTO <dest>
1882 ** 3 IF (x > z) GOTO <dest>
1883 */
drhfef52082000-06-06 01:50:43 +00001884 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001885 Expr *pLeft = pExpr->pLeft;
1886 Expr *pRight = pExpr->pList->a[0].pExpr;
1887 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001888 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001889 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001890 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001891 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1892
danielk19774adee202004-05-08 08:23:19 +00001893 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1894 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001895 pRight = pExpr->pList->a[1].pExpr;
1896 sqlite3ExprCode(pParse, pRight);
1897 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001898 break;
1899 }
drhcce7d172000-05-31 15:34:51 +00001900 default: {
danielk19774adee202004-05-08 08:23:19 +00001901 sqlite3ExprCode(pParse, pExpr);
1902 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001903 break;
1904 }
1905 }
1906}
drh22827922000-06-06 17:27:05 +00001907
1908/*
1909** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1910** if they are identical and return FALSE if they differ in any way.
1911*/
danielk19774adee202004-05-08 08:23:19 +00001912int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001913 int i;
1914 if( pA==0 ){
1915 return pB==0;
1916 }else if( pB==0 ){
1917 return 0;
1918 }
1919 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001920 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1921 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001922 if( pA->pList ){
1923 if( pB->pList==0 ) return 0;
1924 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1925 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001926 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001927 return 0;
1928 }
1929 }
1930 }else if( pB->pList ){
1931 return 0;
1932 }
1933 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001934 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001935 if( pA->token.z ){
1936 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001937 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001938 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001939 }
1940 return 1;
1941}
1942
1943/*
1944** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001945** The new element is initialized to zero. The calling function is
1946** expected to fill it in.
drh22827922000-06-06 17:27:05 +00001947*/
1948static int appendAggInfo(Parse *pParse){
1949 if( (pParse->nAgg & 0x7)==0 ){
1950 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001951 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1952 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001953 return -1;
1954 }
drh6d4abfb2001-10-22 02:58:08 +00001955 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001956 }
1957 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1958 return pParse->nAgg++;
1959}
1960
1961/*
drh626a8792005-01-17 22:08:19 +00001962** This is an xFunc for walkExprTree() used to implement
1963** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
1964** for additional information.
drh22827922000-06-06 17:27:05 +00001965**
drh626a8792005-01-17 22:08:19 +00001966** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00001967*/
drh626a8792005-01-17 22:08:19 +00001968static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001969 int i;
1970 AggExpr *aAgg;
drh626a8792005-01-17 22:08:19 +00001971 Parse *pParse = (Parse*)pArg;
drh22827922000-06-06 17:27:05 +00001972
drh22827922000-06-06 17:27:05 +00001973 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001974 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001975 aAgg = pParse->aAgg;
1976 for(i=0; i<pParse->nAgg; i++){
1977 if( aAgg[i].isAgg ) continue;
1978 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001979 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001980 break;
1981 }
1982 }
1983 if( i>=pParse->nAgg ){
1984 i = appendAggInfo(pParse);
1985 if( i<0 ) return 1;
1986 pParse->aAgg[i].isAgg = 0;
1987 pParse->aAgg[i].pExpr = pExpr;
1988 }
drhaaf88722000-06-08 11:25:00 +00001989 pExpr->iAgg = i;
drh626a8792005-01-17 22:08:19 +00001990 return 1;
drh22827922000-06-06 17:27:05 +00001991 }
1992 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001993 aAgg = pParse->aAgg;
1994 for(i=0; i<pParse->nAgg; i++){
1995 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001996 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001997 break;
1998 }
1999 }
2000 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00002001 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00002002 i = appendAggInfo(pParse);
2003 if( i<0 ) return 1;
2004 pParse->aAgg[i].isAgg = 1;
2005 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00002006 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00002007 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00002008 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00002009 }
2010 pExpr->iAgg = i;
drh626a8792005-01-17 22:08:19 +00002011 return 1;
drh22827922000-06-06 17:27:05 +00002012 }
2013 }
drh626a8792005-01-17 22:08:19 +00002014 return 0;
2015}
2016
2017/*
2018** Analyze the given expression looking for aggregate functions and
2019** for variables that need to be added to the pParse->aAgg[] array.
2020** Make additional entries to the pParse->aAgg[] array as necessary.
2021**
2022** This routine should only be called after the expression has been
2023** analyzed by sqlite3ExprResolveNames().
2024**
2025** If errors are seen, leave an error message in zErrMsg and return
2026** the number of errors.
2027*/
2028int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
2029 int nErr = pParse->nErr;
2030 walkExprTree(pExpr, analyzeAggregate, pParse);
2031 return pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002032}
drh8e0a2f92002-02-23 23:45:45 +00002033
2034/*
danielk1977d02eb1f2004-06-06 09:44:03 +00002035** Locate a user function given a name, a number of arguments and a flag
2036** indicating whether the function prefers UTF-16 over UTF-8. Return a
2037** pointer to the FuncDef structure that defines that function, or return
2038** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00002039**
drh0bce8352002-02-28 00:41:10 +00002040** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00002041** structure is created and liked into the "db" structure if a
2042** no matching function previously existed. When createFlag is true
2043** and the nArg parameter is -1, then only a function that accepts
2044** any number of arguments will be returned.
2045**
2046** If createFlag is false and nArg is -1, then the first valid
2047** function found is returned. A function is valid if either xFunc
2048** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00002049**
2050** If createFlag is false, then a function with the required name and
2051** number of arguments may be returned even if the eTextRep flag does not
2052** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00002053*/
danielk19774adee202004-05-08 08:23:19 +00002054FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00002055 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00002056 const char *zName, /* Name of the function. Not null-terminated */
2057 int nName, /* Number of characters in the name */
2058 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00002059 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00002060 int createFlag /* Create new entry if true and does not otherwise exist */
2061){
danielk1977d02eb1f2004-06-06 09:44:03 +00002062 FuncDef *p; /* Iterator variable */
2063 FuncDef *pFirst; /* First function with this name */
2064 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00002065 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00002066
danielk1977d8123362004-06-12 09:25:12 +00002067
2068 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00002069 if( nArg<-1 ) nArg = -1;
2070
2071 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
2072 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00002073 /* During the search for the best function definition, bestmatch is set
2074 ** as follows to indicate the quality of the match with the definition
2075 ** pointed to by pBest:
2076 **
2077 ** 0: pBest is NULL. No match has been found.
2078 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
2079 ** encoding is requested, or vice versa.
2080 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
2081 ** requested, or vice versa.
2082 ** 3: A variable arguments function using the same text encoding.
2083 ** 4: A function with the exact number of arguments requested that
2084 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
2085 ** 5: A function with the exact number of arguments requested that
2086 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
2087 ** 6: An exact match.
2088 **
2089 ** A larger value of 'matchqual' indicates a more desirable match.
2090 */
danielk1977e12c17b2004-06-23 12:35:14 +00002091 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00002092 int match = 1; /* Quality of this match */
2093 if( p->nArg==nArg || nArg==-1 ){
2094 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00002095 }
danielk1977d8123362004-06-12 09:25:12 +00002096 if( enc==p->iPrefEnc ){
2097 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00002098 }
danielk1977d8123362004-06-12 09:25:12 +00002099 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
2100 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
2101 match += 1;
2102 }
2103
2104 if( match>bestmatch ){
2105 pBest = p;
2106 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00002107 }
2108 }
drh8e0a2f92002-02-23 23:45:45 +00002109 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002110
danielk1977d8123362004-06-12 09:25:12 +00002111 /* If the createFlag parameter is true, and the seach did not reveal an
2112 ** exact match for the name, number of arguments and encoding, then add a
2113 ** new entry to the hash table and return it.
2114 */
2115 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00002116 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
2117 pBest->nArg = nArg;
2118 pBest->pNext = pFirst;
2119 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00002120 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00002121 memcpy(pBest->zName, zName, nName);
2122 pBest->zName[nName] = 0;
danielk19772c336542005-01-13 02:14:23 +00002123 if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
2124 sqliteFree(pBest);
2125 return 0;
2126 }
drh8e0a2f92002-02-23 23:45:45 +00002127 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002128
2129 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
2130 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00002131 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002132 return 0;
drh8e0a2f92002-02-23 23:45:45 +00002133}