blob: 513cc6f3ebf00bcd55c2abb723d2a2d83a3060ec [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**
drh4efc4752004-01-16 15:55:37 +000015** $Id: expr.c,v 1.105 2004/01/16 15:55:38 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
20/*
drha76b5df2002-02-23 02:32:10 +000021** Construct a new expression node and return a pointer to it. Memory
22** for this node is obtained from sqliteMalloc(). The calling function
23** is responsible for making sure the node eventually gets freed.
24*/
25Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
26 Expr *pNew;
27 pNew = sqliteMalloc( sizeof(Expr) );
28 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +000029 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +000030 return 0;
31 }
32 pNew->op = op;
33 pNew->pLeft = pLeft;
34 pNew->pRight = pRight;
35 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000036 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000037 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +000038 pNew->span = *pToken;
drha76b5df2002-02-23 02:32:10 +000039 }else{
drh4efc4752004-01-16 15:55:37 +000040 assert( pNew->token.dyn==0 );
41 assert( pNew->token.z==0 );
42 assert( pNew->token.n==0 );
drh6977fea2002-10-22 23:38:04 +000043 if( pLeft && pRight ){
44 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
45 }else{
46 pNew->span = pNew->token;
47 }
drha76b5df2002-02-23 02:32:10 +000048 }
drha76b5df2002-02-23 02:32:10 +000049 return pNew;
50}
51
52/*
drh6977fea2002-10-22 23:38:04 +000053** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +000054** text between the two given tokens.
55*/
56void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +000057 assert( pRight!=0 );
58 assert( pLeft!=0 );
59 /* Note: pExpr might be NULL due to a prior malloc failure */
60 if( pExpr && pRight->z && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +000061 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +000062 pExpr->span.z = pLeft->z;
63 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +000064 }else{
drh6977fea2002-10-22 23:38:04 +000065 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +000066 }
drha76b5df2002-02-23 02:32:10 +000067 }
68}
69
70/*
71** Construct a new expression node for a function with multiple
72** arguments.
73*/
74Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
75 Expr *pNew;
76 pNew = sqliteMalloc( sizeof(Expr) );
77 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +000078 /* sqliteExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +000079 return 0;
80 }
81 pNew->op = TK_FUNCTION;
82 pNew->pList = pList;
83 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000084 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000085 pNew->token = *pToken;
86 }else{
87 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +000088 }
drh6977fea2002-10-22 23:38:04 +000089 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +000090 return pNew;
91}
92
93/*
drha2e00042002-01-22 03:13:42 +000094** Recursively delete an expression tree.
95*/
96void sqliteExprDelete(Expr *p){
97 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +000098 if( p->span.dyn ) sqliteFree((char*)p->span.z);
99 if( p->token.dyn ) sqliteFree((char*)p->token.z);
100 sqliteExprDelete(p->pLeft);
101 sqliteExprDelete(p->pRight);
102 sqliteExprListDelete(p->pList);
103 sqliteSelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000104 sqliteFree(p);
105}
106
drha76b5df2002-02-23 02:32:10 +0000107
108/*
drhff78bd22002-02-27 01:47:11 +0000109** The following group of routines make deep copies of expressions,
110** expression lists, ID lists, and select statements. The copies can
111** be deleted (by being passed to their respective ...Delete() routines)
112** without effecting the originals.
113**
drhad3cab52002-05-24 02:04:32 +0000114** The expression list, ID, and source lists return by sqliteExprListDup(),
115** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
116** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000117**
drhad3cab52002-05-24 02:04:32 +0000118** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000119*/
120Expr *sqliteExprDup(Expr *p){
121 Expr *pNew;
122 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000123 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000124 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000125 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000126 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000127 pNew->token.z = sqliteStrDup(p->token.z);
128 pNew->token.dyn = 1;
129 }else{
drh4efc4752004-01-16 15:55:37 +0000130 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000131 }
drh6977fea2002-10-22 23:38:04 +0000132 pNew->span.z = 0;
drhff78bd22002-02-27 01:47:11 +0000133 pNew->pLeft = sqliteExprDup(p->pLeft);
134 pNew->pRight = sqliteExprDup(p->pRight);
135 pNew->pList = sqliteExprListDup(p->pList);
drhff78bd22002-02-27 01:47:11 +0000136 pNew->pSelect = sqliteSelectDup(p->pSelect);
137 return pNew;
138}
drh4b59ab52002-08-24 18:24:51 +0000139void sqliteTokenCopy(Token *pTo, Token *pFrom){
140 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000141 if( pFrom->z ){
142 pTo->n = pFrom->n;
143 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
144 pTo->dyn = 1;
145 }else{
drh4b59ab52002-08-24 18:24:51 +0000146 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000147 }
148}
drhff78bd22002-02-27 01:47:11 +0000149ExprList *sqliteExprListDup(ExprList *p){
150 ExprList *pNew;
151 int i;
152 if( p==0 ) return 0;
153 pNew = sqliteMalloc( sizeof(*pNew) );
154 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000155 pNew->nExpr = pNew->nAlloc = p->nExpr;
drhff78bd22002-02-27 01:47:11 +0000156 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000157 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000158 for(i=0; i<p->nExpr; i++){
drh4b59ab52002-08-24 18:24:51 +0000159 Expr *pNewExpr, *pOldExpr;
160 pNew->a[i].pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000161 if( pOldExpr->span.z!=0 && pNewExpr ){
162 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000163 ** expression list. The logic in SELECT processing that determines
164 ** the names of columns in the result set needs this information */
drh6977fea2002-10-22 23:38:04 +0000165 sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000166 }
drh1f3e9052002-10-31 00:09:39 +0000167 assert( pNewExpr==0 || pNewExpr->span.z!=0
168 || pOldExpr->span.z==0 || sqlite_malloc_failed );
drhff78bd22002-02-27 01:47:11 +0000169 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
170 pNew->a[i].sortOrder = p->a[i].sortOrder;
171 pNew->a[i].isAgg = p->a[i].isAgg;
172 pNew->a[i].done = 0;
173 }
174 return pNew;
175}
drhad3cab52002-05-24 02:04:32 +0000176SrcList *sqliteSrcListDup(SrcList *p){
177 SrcList *pNew;
178 int i;
drh113088e2003-03-20 01:16:58 +0000179 int nByte;
drhad3cab52002-05-24 02:04:32 +0000180 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000181 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000182 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000183 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000184 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000185 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000186 struct SrcList_item *pNewItem = &pNew->a[i];
187 struct SrcList_item *pOldItem = &p->a[i];
188 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
189 pNewItem->zName = sqliteStrDup(pOldItem->zName);
190 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
191 pNewItem->jointype = pOldItem->jointype;
192 pNewItem->iCursor = pOldItem->iCursor;
193 pNewItem->pTab = 0;
194 pNewItem->pSelect = sqliteSelectDup(pOldItem->pSelect);
195 pNewItem->pOn = sqliteExprDup(pOldItem->pOn);
196 pNewItem->pUsing = sqliteIdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000197 }
198 return pNew;
199}
drhff78bd22002-02-27 01:47:11 +0000200IdList *sqliteIdListDup(IdList *p){
201 IdList *pNew;
202 int i;
203 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000204 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000205 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000206 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000207 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000208 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000209 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000210 struct IdList_item *pNewItem = &pNew->a[i];
211 struct IdList_item *pOldItem = &p->a[i];
212 pNewItem->zName = sqliteStrDup(pOldItem->zName);
213 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000214 }
215 return pNew;
216}
217Select *sqliteSelectDup(Select *p){
218 Select *pNew;
219 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000220 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000221 if( pNew==0 ) return 0;
222 pNew->isDistinct = p->isDistinct;
223 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000224 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000225 pNew->pWhere = sqliteExprDup(p->pWhere);
226 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
227 pNew->pHaving = sqliteExprDup(p->pHaving);
228 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
229 pNew->op = p->op;
230 pNew->pPrior = sqliteSelectDup(p->pPrior);
231 pNew->nLimit = p->nLimit;
232 pNew->nOffset = p->nOffset;
233 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000234 pNew->iLimit = -1;
235 pNew->iOffset = -1;
drhff78bd22002-02-27 01:47:11 +0000236 return pNew;
237}
238
239
240/*
drha76b5df2002-02-23 02:32:10 +0000241** Add a new element to the end of an expression list. If pList is
242** initially NULL, then create a new expression list.
243*/
244ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
245 int i;
246 if( pList==0 ){
247 pList = sqliteMalloc( sizeof(ExprList) );
248 if( pList==0 ){
drh4efc4752004-01-16 15:55:37 +0000249 /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000250 return 0;
251 }
drh4efc4752004-01-16 15:55:37 +0000252 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000253 }
drh4305d102003-07-30 12:34:12 +0000254 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000255 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000256 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
257 if( pList->a==0 ){
258 /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
259 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000260 return pList;
261 }
drha76b5df2002-02-23 02:32:10 +0000262 }
drh4efc4752004-01-16 15:55:37 +0000263 assert( pList->a!=0 );
264 if( pExpr || pName ){
265 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
266 memset(pItem, 0, sizeof(*pItem));
267 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000268 if( pName ){
drh4efc4752004-01-16 15:55:37 +0000269 sqliteSetNString(&pItem->zName, pName->z, pName->n, 0);
270 sqliteDequote(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000271 }
272 }
273 return pList;
274}
275
276/*
277** Delete an entire expression list.
278*/
279void sqliteExprListDelete(ExprList *pList){
280 int i;
281 if( pList==0 ) return;
282 for(i=0; i<pList->nExpr; i++){
283 sqliteExprDelete(pList->a[i].pExpr);
284 sqliteFree(pList->a[i].zName);
285 }
286 sqliteFree(pList->a);
287 sqliteFree(pList);
288}
289
290/*
drhfef52082000-06-06 01:50:43 +0000291** Walk an expression tree. Return 1 if the expression is constant
292** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000293**
294** For the purposes of this function, a double-quoted string (ex: "abc")
295** is considered a variable but a single-quoted string (ex: 'abc') is
296** a constant.
drhfef52082000-06-06 01:50:43 +0000297*/
drh92086432002-01-22 14:11:29 +0000298int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000299 switch( p->op ){
300 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000301 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000302 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000303 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000304 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000305 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000306 case TK_STRING:
drh92086432002-01-22 14:11:29 +0000307 case TK_INTEGER:
308 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000309 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000310 return 1;
drhfef52082000-06-06 01:50:43 +0000311 default: {
drh92086432002-01-22 14:11:29 +0000312 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
313 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000314 if( p->pList ){
315 int i;
316 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000317 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000318 }
319 }
drh92086432002-01-22 14:11:29 +0000320 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000321 }
322 }
drh92086432002-01-22 14:11:29 +0000323 return 0;
drhfef52082000-06-06 01:50:43 +0000324}
325
326/*
drh202b2df2004-01-06 01:13:46 +0000327** If the given expression codes a constant integer that is small enough
328** to fit in a 32-bit integer, return 1 and put the value of the integer
329** in *pValue. If the expression is not an integer or if it is too big
330** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000331*/
332int sqliteExprIsInteger(Expr *p, int *pValue){
333 switch( p->op ){
334 case TK_INTEGER: {
drh202b2df2004-01-06 01:13:46 +0000335 if( sqliteFitsIn32Bits(p->token.z) ){
336 *pValue = atoi(p->token.z);
337 return 1;
338 }
339 break;
drhe4de1fe2002-06-02 16:09:01 +0000340 }
341 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000342 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000343 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000344 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000345 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drh202b2df2004-01-06 01:13:46 +0000346 if( n==0 && sqliteFitsIn32Bits(p->token.z) ){
drhe4de1fe2002-06-02 16:09:01 +0000347 *pValue = atoi(p->token.z);
348 return 1;
349 }
350 break;
351 }
drh4b59ab52002-08-24 18:24:51 +0000352 case TK_UPLUS: {
353 return sqliteExprIsInteger(p->pLeft, pValue);
354 }
drhe4de1fe2002-06-02 16:09:01 +0000355 case TK_UMINUS: {
356 int v;
357 if( sqliteExprIsInteger(p->pLeft, &v) ){
358 *pValue = -v;
359 return 1;
360 }
361 break;
362 }
363 default: break;
364 }
365 return 0;
366}
367
368/*
drhc4a3c772001-04-04 11:48:57 +0000369** Return TRUE if the given string is a row-id column name.
370*/
drha9f9d1c2002-06-29 02:20:08 +0000371int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000372 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
373 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
374 if( sqliteStrICmp(z, "OID")==0 ) return 1;
375 return 0;
376}
377
378/*
drhcce7d172000-05-31 15:34:51 +0000379** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000380** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000381** index to the table in the table list and a column offset. The
382** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
383** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000384** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000385** VDBE cursor number for a cursor that is pointing into the referenced
386** table. The Expr.iColumn value is changed to the index of the column
387** of the referenced table. The Expr.iColumn value for the special
388** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
389** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000390**
drhfef52082000-06-06 01:50:43 +0000391** We also check for instances of the IN operator. IN comes in two
392** forms:
393**
394** expr IN (exprlist)
395** and
396** expr IN (SELECT ...)
397**
398** The first form is handled by creating a set holding the list
399** of allowed values. The second form causes the SELECT to generate
400** a temporary table.
401**
402** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000403** If it finds any, it generates code to write the value of that select
404** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000405**
drh967e8b72000-06-21 13:59:10 +0000406** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000407** the number of errors seen and leaves an error message on pParse->zErrMsg.
408*/
drha2e00042002-01-22 03:13:42 +0000409int sqliteExprResolveIds(
410 Parse *pParse, /* The parser context */
drhad3cab52002-05-24 02:04:32 +0000411 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000412 ExprList *pEList, /* List of expressions used to resolve "AS" */
413 Expr *pExpr /* The expression to be analyzed. */
414){
drh6a3ea0e2003-05-02 14:32:12 +0000415 int i;
416
drhdaffd0e2001-04-11 14:28:42 +0000417 if( pExpr==0 || pTabList==0 ) return 0;
drh6a3ea0e2003-05-02 14:32:12 +0000418 for(i=0; i<pTabList->nSrc; i++){
419 assert( pTabList->a[i].iCursor>=0 && pTabList->a[i].iCursor<pParse->nTab );
420 }
drhcce7d172000-05-31 15:34:51 +0000421 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000422 /* Double-quoted strings (ex: "abc") are used as identifiers if
423 ** possible. Otherwise they remain as strings. Single-quoted
424 ** strings (ex: 'abc') are always string literals.
425 */
426 case TK_STRING: {
427 if( pExpr->token.z[0]=='\'' ) break;
428 /* Fall thru into the TK_ID case if this is a double-quoted string */
429 }
drha2e00042002-01-22 03:13:42 +0000430 /* A lone identifier. Try and match it as follows:
431 **
432 ** 1. To the name of a column of one of the tables in pTabList
433 **
434 ** 2. To the right side of an AS keyword in the column list of
435 ** a SELECT statement. (For example, match against 'x' in
436 ** "SELECT a+b AS 'x' FROM t1".)
437 **
438 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
439 */
drhcce7d172000-05-31 15:34:51 +0000440 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000441 int cnt = 0; /* Number of matches */
drha76b5df2002-02-23 02:32:10 +0000442 char *z;
drhe22a3342003-04-22 20:30:37 +0000443 int iDb = -1;
444
drha76b5df2002-02-23 02:32:10 +0000445 assert( pExpr->token.z );
446 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000447 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000448 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000449 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000450 int j;
451 Table *pTab = pTabList->a[i].pTab;
452 if( pTab==0 ) continue;
drhe22a3342003-04-22 20:30:37 +0000453 iDb = pTab->iDb;
drh417be792002-03-03 18:59:40 +0000454 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000455 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000456 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000457 cnt++;
drh6a3ea0e2003-05-02 14:32:12 +0000458 pExpr->iTable = pTabList->a[i].iCursor;
drhe22a3342003-04-22 20:30:37 +0000459 pExpr->iDb = pTab->iDb;
drh4a324312001-12-21 14:30:42 +0000460 if( j==pTab->iPKey ){
461 /* Substitute the record number for the INTEGER PRIMARY KEY */
462 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000463 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000464 }else{
465 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000466 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000467 }
drha2e00042002-01-22 03:13:42 +0000468 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000469 }
470 }
471 }
drha2e00042002-01-22 03:13:42 +0000472 if( cnt==0 && pEList!=0 ){
473 int j;
474 for(j=0; j<pEList->nExpr; j++){
475 char *zAs = pEList->a[j].zName;
476 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
477 cnt++;
478 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
479 pExpr->op = TK_AS;
480 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000481 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000482 }
483 }
484 }
drhe22a3342003-04-22 20:30:37 +0000485 if( cnt==0 && iDb>=0 && sqliteIsRowid(z) ){
drhc4a3c772001-04-04 11:48:57 +0000486 pExpr->iColumn = -1;
drh6a3ea0e2003-05-02 14:32:12 +0000487 pExpr->iTable = pTabList->a[0].iCursor;
drhe22a3342003-04-22 20:30:37 +0000488 pExpr->iDb = iDb;
drhad3cab52002-05-24 02:04:32 +0000489 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000490 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000491 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000492 }
drhcce7d172000-05-31 15:34:51 +0000493 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000494 if( cnt==0 && pExpr->token.z[0]!='"' ){
drhda93d232003-03-31 02:12:46 +0000495 sqliteErrorMsg(pParse, "no such column: %T", &pExpr->token);
drhcce7d172000-05-31 15:34:51 +0000496 return 1;
497 }else if( cnt>1 ){
drhda93d232003-03-31 02:12:46 +0000498 sqliteErrorMsg(pParse, "ambiguous column name: %T", &pExpr->token);
drhcce7d172000-05-31 15:34:51 +0000499 return 1;
500 }
drhed6c8672003-01-12 18:02:16 +0000501 if( pExpr->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000502 sqliteAuthRead(pParse, pExpr, pTabList);
drhed6c8672003-01-12 18:02:16 +0000503 }
drhcce7d172000-05-31 15:34:51 +0000504 break;
505 }
506
drhd24cc422003-03-27 12:51:24 +0000507 /* A table name and column name: ID.ID
508 ** Or a database, table and column: ID.ID.ID
509 */
drhcce7d172000-05-31 15:34:51 +0000510 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000511 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000512 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000513 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000514 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000515 char *zLeft, *zRight; /* Text of an identifier */
drhd24cc422003-03-27 12:51:24 +0000516 char *zDb; /* Name of database holding table */
517 sqlite *db = pParse->db;
drhcce7d172000-05-31 15:34:51 +0000518
drhcce7d172000-05-31 15:34:51 +0000519 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000520 if( pRight->op==TK_ID ){
521 pLeft = pExpr->pLeft;
522 zDb = 0;
523 }else{
524 Expr *pDb = pExpr->pLeft;
525 assert( pDb && pDb->op==TK_ID && pDb->token.z );
526 zDb = sqliteStrNDup(pDb->token.z, pDb->token.n);
527 pLeft = pRight->pLeft;
528 pRight = pRight->pRight;
529 }
drha76b5df2002-02-23 02:32:10 +0000530 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
531 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000532 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
533 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000534 if( zLeft==0 || zRight==0 ){
535 sqliteFree(zLeft);
536 sqliteFree(zRight);
drhd24cc422003-03-27 12:51:24 +0000537 sqliteFree(zDb);
drhdaffd0e2001-04-11 14:28:42 +0000538 return 1;
539 }
drhd24cc422003-03-27 12:51:24 +0000540 sqliteDequote(zDb);
drh87c40e82001-07-23 14:33:02 +0000541 sqliteDequote(zLeft);
542 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000543 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000544 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000545 int j;
546 char *zTab;
547 Table *pTab = pTabList->a[i].pTab;
548 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000549 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000550 if( pTabList->a[i].zAlias ){
551 zTab = pTabList->a[i].zAlias;
drhd24cc422003-03-27 12:51:24 +0000552 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhcce7d172000-05-31 15:34:51 +0000553 }else{
554 zTab = pTab->zName;
drhd24cc422003-03-27 12:51:24 +0000555 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
556 if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
557 continue;
558 }
drhcce7d172000-05-31 15:34:51 +0000559 }
drhe22a3342003-04-22 20:30:37 +0000560 if( 0==(cntTab++) ){
drh6a3ea0e2003-05-02 14:32:12 +0000561 pExpr->iTable = pTabList->a[i].iCursor;
drhe22a3342003-04-22 20:30:37 +0000562 pExpr->iDb = pTab->iDb;
563 }
drhcce7d172000-05-31 15:34:51 +0000564 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000565 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000566 cnt++;
drh6a3ea0e2003-05-02 14:32:12 +0000567 pExpr->iTable = pTabList->a[i].iCursor;
drhe22a3342003-04-22 20:30:37 +0000568 pExpr->iDb = pTab->iDb;
drh70ce3f02003-04-15 19:22:22 +0000569 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
570 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
drhc9b84a12002-06-20 11:36:48 +0000571 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000572 }
573 }
574 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000575
576 /* If we have not already resolved this *.* expression, then maybe
577 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000578 if( cnt == 0 && pParse->trigStack != 0 ){
579 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000580 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000581 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
582 pExpr->iTable = pTriggerStack->newIdx;
drhe22a3342003-04-22 20:30:37 +0000583 assert( pTriggerStack->pTab );
584 pExpr->iDb = pTriggerStack->pTab->iDb;
danielk1977c3f9bad2002-05-15 08:30:12 +0000585 cntTab++;
586 t = 1;
587 }
danielk1977f29ce552002-05-19 23:43:12 +0000588 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
589 pExpr->iTable = pTriggerStack->oldIdx;
drhe22a3342003-04-22 20:30:37 +0000590 assert( pTriggerStack->pTab );
591 pExpr->iDb = pTriggerStack->pTab->iDb;
danielk1977c3f9bad2002-05-15 08:30:12 +0000592 cntTab++;
593 t = 1;
594 }
595
danielk1977f29ce552002-05-19 23:43:12 +0000596 if( t ){
597 int j;
drhc9b84a12002-06-20 11:36:48 +0000598 Table *pTab = pTriggerStack->pTab;
599 for(j=0; j < pTab->nCol; j++) {
600 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000601 cnt++;
drh70ce3f02003-04-15 19:22:22 +0000602 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
drhc9b84a12002-06-20 11:36:48 +0000603 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000604 }
605 }
danielk1977f29ce552002-05-19 23:43:12 +0000606 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000607 }
608
drhc4a3c772001-04-04 11:48:57 +0000609 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
610 cnt = 1;
611 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000612 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000613 }
drhd24cc422003-03-27 12:51:24 +0000614 sqliteFree(zDb);
drhcce7d172000-05-31 15:34:51 +0000615 sqliteFree(zLeft);
616 sqliteFree(zRight);
617 if( cnt==0 ){
drhda93d232003-03-31 02:12:46 +0000618 sqliteErrorMsg(pParse, "no such column: %T.%T",
619 &pLeft->token, &pRight->token);
drhcce7d172000-05-31 15:34:51 +0000620 return 1;
621 }else if( cnt>1 ){
drhda93d232003-03-31 02:12:46 +0000622 sqliteErrorMsg(pParse, "ambiguous column name: %T.%T",
623 &pLeft->token, &pRight->token);
drhcce7d172000-05-31 15:34:51 +0000624 return 1;
625 }
drhd24cc422003-03-27 12:51:24 +0000626 sqliteExprDelete(pExpr->pLeft);
drhcce7d172000-05-31 15:34:51 +0000627 pExpr->pLeft = 0;
drhd24cc422003-03-27 12:51:24 +0000628 sqliteExprDelete(pExpr->pRight);
drhcce7d172000-05-31 15:34:51 +0000629 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000630 pExpr->op = TK_COLUMN;
drh6a3ea0e2003-05-02 14:32:12 +0000631 sqliteAuthRead(pParse, pExpr, pTabList);
drhcce7d172000-05-31 15:34:51 +0000632 break;
633 }
634
drhfef52082000-06-06 01:50:43 +0000635 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000636 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000637 if( v==0 ) return 1;
drh6a3ea0e2003-05-02 14:32:12 +0000638 if( sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000639 return 1;
640 }
drhfef52082000-06-06 01:50:43 +0000641 if( pExpr->pSelect ){
642 /* Case 1: expr IN (SELECT ...)
643 **
644 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000645 ** table. The cursor number of the temporary table has already
646 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000647 */
drh832508b2002-03-02 17:04:07 +0000648 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000649 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000650 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000651 }else if( pExpr->pList ){
652 /* Case 2: expr IN (exprlist)
653 **
654 ** Create a set to put the exprlist values in. The Set id is stored
655 ** in iTable.
656 */
657 int i, iSet;
658 for(i=0; i<pExpr->pList->nExpr; i++){
659 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000660 if( !sqliteExprIsConstant(pE2) ){
drhda93d232003-03-31 02:12:46 +0000661 sqliteErrorMsg(pParse,
662 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000663 return 1;
664 }
drh4794b982000-06-06 13:54:14 +0000665 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
666 return 1;
667 }
drhfef52082000-06-06 01:50:43 +0000668 }
669 iSet = pExpr->iTable = pParse->nSet++;
670 for(i=0; i<pExpr->pList->nExpr; i++){
671 Expr *pE2 = pExpr->pList->a[i].pExpr;
672 switch( pE2->op ){
673 case TK_FLOAT:
674 case TK_INTEGER:
675 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000676 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000677 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000678 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
679 sqliteVdbeDequoteP3(v, addr);
680 break;
681 }
682 default: {
683 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000684 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000685 break;
686 }
687 }
688 }
689 }
drhcfab11b2000-06-06 03:31:22 +0000690 break;
drhfef52082000-06-06 01:50:43 +0000691 }
692
drh19a775c2000-06-05 18:54:46 +0000693 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000694 /* This has to be a scalar SELECT. Generate code to put the
695 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000696 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000697 */
drh967e8b72000-06-21 13:59:10 +0000698 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000699 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000700 return 1;
701 }
702 break;
703 }
704
drhcce7d172000-05-31 15:34:51 +0000705 /* For all else, just recursively walk the tree */
706 default: {
drh4794b982000-06-06 13:54:14 +0000707 if( pExpr->pLeft
drh6a3ea0e2003-05-02 14:32:12 +0000708 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000709 return 1;
710 }
711 if( pExpr->pRight
drh6a3ea0e2003-05-02 14:32:12 +0000712 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000713 return 1;
714 }
715 if( pExpr->pList ){
716 int i;
717 ExprList *pList = pExpr->pList;
718 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000719 Expr *pArg = pList->a[i].pExpr;
drh6a3ea0e2003-05-02 14:32:12 +0000720 if( sqliteExprResolveIds(pParse, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000721 return 1;
722 }
723 }
724 }
725 }
726 }
727 return 0;
728}
729
drhcce7d172000-05-31 15:34:51 +0000730/*
drh4b59ab52002-08-24 18:24:51 +0000731** pExpr is a node that defines a function of some kind. It might
732** be a syntactic function like "count(x)" or it might be a function
733** that implements an operator, like "a LIKE b".
734**
735** This routine makes *pzName point to the name of the function and
736** *pnName hold the number of characters in the function name.
737*/
738static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
739 switch( pExpr->op ){
740 case TK_FUNCTION: {
741 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000742 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000743 break;
744 }
745 case TK_LIKE: {
746 *pzName = "like";
747 *pnName = 4;
748 break;
749 }
750 case TK_GLOB: {
751 *pzName = "glob";
752 *pnName = 4;
753 break;
754 }
755 default: {
756 *pzName = "can't happen";
757 *pnName = 12;
758 break;
759 }
760 }
761}
762
763/*
drhcce7d172000-05-31 15:34:51 +0000764** Error check the functions in an expression. Make sure all
765** function names are recognized and all functions have the correct
766** number of arguments. Leave an error message in pParse->zErrMsg
767** if anything is amiss. Return the number of errors.
768**
769** if pIsAgg is not null and this expression is an aggregate function
770** (like count(*) or max(value)) then write a 1 into *pIsAgg.
771*/
772int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
773 int nErr = 0;
774 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000775 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000776 case TK_GLOB:
777 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000778 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000779 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
780 int no_such_func = 0; /* True if no such function exists */
781 int is_type_of = 0; /* True if is the special TypeOf() function */
782 int wrong_num_args = 0; /* True if wrong number of arguments */
783 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000784 int i;
drh4b59ab52002-08-24 18:24:51 +0000785 int nId; /* Number of characters in function name */
786 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000787 FuncDef *pDef;
788
drh4b59ab52002-08-24 18:24:51 +0000789 getFunctionName(pExpr, &zId, &nId);
790 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000791 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000792 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000793 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000794 if( n==1 && nId==6 && sqliteStrNICmp(zId, "typeof", 6)==0 ){
drhc9b84a12002-06-20 11:36:48 +0000795 is_type_of = 1;
796 }else {
797 no_such_func = 1;
798 }
drh0bce8352002-02-28 00:41:10 +0000799 }else{
800 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000801 }
drh0bce8352002-02-28 00:41:10 +0000802 }else{
803 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000804 }
drh8e0a2f92002-02-23 23:45:45 +0000805 if( is_agg && !allowAgg ){
806 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
drh4b59ab52002-08-24 18:24:51 +0000807 zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000808 pParse->nErr++;
809 nErr++;
810 is_agg = 0;
811 }else if( no_such_func ){
drh4b59ab52002-08-24 18:24:51 +0000812 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, zId,nId,0);
drhcce7d172000-05-31 15:34:51 +0000813 pParse->nErr++;
814 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000815 }else if( wrong_num_args ){
816 sqliteSetNString(&pParse->zErrMsg,
drh4b59ab52002-08-24 18:24:51 +0000817 "wrong number of arguments to function ", -1, zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000818 pParse->nErr++;
819 nErr++;
drhcce7d172000-05-31 15:34:51 +0000820 }
drh22827922000-06-06 17:27:05 +0000821 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000822 if( is_agg && pIsAgg ) *pIsAgg = 1;
823 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000824 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
825 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000826 }
drhc9b84a12002-06-20 11:36:48 +0000827 if( pDef==0 ){
828 if( is_type_of ){
829 pExpr->op = TK_STRING;
830 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
831 pExpr->token.z = "numeric";
832 pExpr->token.n = 7;
833 }else{
834 pExpr->token.z = "text";
835 pExpr->token.n = 4;
836 }
837 }
838 }else if( pDef->dataType>=0 ){
839 if( pDef->dataType<n ){
840 pExpr->dataType =
841 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
842 }else{
843 pExpr->dataType = SQLITE_SO_NUM;
844 }
845 }else if( pDef->dataType==SQLITE_ARGS ){
846 pDef->dataType = SQLITE_SO_TEXT;
847 for(i=0; i<n; i++){
848 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
849 pExpr->dataType = SQLITE_SO_NUM;
850 break;
851 }
852 }
853 }else if( pDef->dataType==SQLITE_NUMERIC ){
854 pExpr->dataType = SQLITE_SO_NUM;
855 }else{
856 pExpr->dataType = SQLITE_SO_TEXT;
857 }
drhcce7d172000-05-31 15:34:51 +0000858 }
859 default: {
860 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000861 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000862 }
863 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000864 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000865 }
drhfef52082000-06-06 01:50:43 +0000866 if( nErr==0 && pExpr->pList ){
867 int n = pExpr->pList->nExpr;
868 int i;
869 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000870 Expr *pE2 = pExpr->pList->a[i].pExpr;
871 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000872 }
873 }
drhcce7d172000-05-31 15:34:51 +0000874 break;
875 }
876 }
877 return nErr;
878}
879
880/*
drhc9b84a12002-06-20 11:36:48 +0000881** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
882** given expression should sort as numeric values or as text.
883**
884** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
885** both been called on the expression before it is passed to this routine.
886*/
887int sqliteExprType(Expr *p){
888 if( p==0 ) return SQLITE_SO_NUM;
889 while( p ) switch( p->op ){
890 case TK_PLUS:
891 case TK_MINUS:
892 case TK_STAR:
893 case TK_SLASH:
894 case TK_AND:
895 case TK_OR:
896 case TK_ISNULL:
897 case TK_NOTNULL:
898 case TK_NOT:
899 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +0000900 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +0000901 case TK_BITAND:
902 case TK_BITOR:
903 case TK_BITNOT:
904 case TK_LSHIFT:
905 case TK_RSHIFT:
906 case TK_REM:
907 case TK_INTEGER:
908 case TK_FLOAT:
909 case TK_IN:
910 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +0000911 case TK_GLOB:
912 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +0000913 return SQLITE_SO_NUM;
914
915 case TK_STRING:
916 case TK_NULL:
917 case TK_CONCAT:
drh50457892003-09-06 01:10:47 +0000918 case TK_VARIABLE:
drhc9b84a12002-06-20 11:36:48 +0000919 return SQLITE_SO_TEXT;
920
921 case TK_LT:
922 case TK_LE:
923 case TK_GT:
924 case TK_GE:
925 case TK_NE:
926 case TK_EQ:
927 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
928 return SQLITE_SO_NUM;
929 }
930 p = p->pRight;
931 break;
932
933 case TK_AS:
934 p = p->pLeft;
935 break;
936
937 case TK_COLUMN:
938 case TK_FUNCTION:
939 case TK_AGG_FUNCTION:
940 return p->dataType;
941
942 case TK_SELECT:
943 assert( p->pSelect );
944 assert( p->pSelect->pEList );
945 assert( p->pSelect->pEList->nExpr>0 );
946 p = p->pSelect->pEList->a[0].pExpr;
947 break;
948
drhb1363202002-06-26 02:45:03 +0000949 case TK_CASE: {
950 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
951 return SQLITE_SO_NUM;
952 }
953 if( p->pList ){
954 int i;
955 ExprList *pList = p->pList;
956 for(i=1; i<pList->nExpr; i+=2){
957 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
958 return SQLITE_SO_NUM;
959 }
960 }
961 }
962 return SQLITE_SO_TEXT;
963 }
964
drhc9b84a12002-06-20 11:36:48 +0000965 default:
966 assert( p->op==TK_ABORT ); /* Can't Happen */
967 break;
968 }
969 return SQLITE_SO_NUM;
970}
971
972/*
drhcce7d172000-05-31 15:34:51 +0000973** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000974** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000975*/
976void sqliteExprCode(Parse *pParse, Expr *pExpr){
977 Vdbe *v = pParse->pVdbe;
978 int op;
drhdaffd0e2001-04-11 14:28:42 +0000979 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000980 switch( pExpr->op ){
981 case TK_PLUS: op = OP_Add; break;
982 case TK_MINUS: op = OP_Subtract; break;
983 case TK_STAR: op = OP_Multiply; break;
984 case TK_SLASH: op = OP_Divide; break;
985 case TK_AND: op = OP_And; break;
986 case TK_OR: op = OP_Or; break;
987 case TK_LT: op = OP_Lt; break;
988 case TK_LE: op = OP_Le; break;
989 case TK_GT: op = OP_Gt; break;
990 case TK_GE: op = OP_Ge; break;
991 case TK_NE: op = OP_Ne; break;
992 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000993 case TK_ISNULL: op = OP_IsNull; break;
994 case TK_NOTNULL: op = OP_NotNull; break;
995 case TK_NOT: op = OP_Not; break;
996 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000997 case TK_BITAND: op = OP_BitAnd; break;
998 case TK_BITOR: op = OP_BitOr; break;
999 case TK_BITNOT: op = OP_BitNot; break;
1000 case TK_LSHIFT: op = OP_ShiftLeft; break;
1001 case TK_RSHIFT: op = OP_ShiftRight; break;
1002 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +00001003 default: break;
1004 }
1005 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001006 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001007 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +00001008 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001009 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +00001010 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001011 }else{
drh99fcd712001-10-13 01:06:47 +00001012 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001013 }
drhcce7d172000-05-31 15:34:51 +00001014 break;
1015 }
1016 case TK_INTEGER: {
drh202b2df2004-01-06 01:13:46 +00001017 if( !sqliteFitsIn32Bits(pExpr->token.z) ){
drhd9e30932002-06-09 01:16:01 +00001018 sqliteVdbeAddOp(v, OP_String, 0, 0);
1019 }else{
drh202b2df2004-01-06 01:13:46 +00001020 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
drhd9e30932002-06-09 01:16:01 +00001021 }
drhe6840902002-03-06 03:08:25 +00001022 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1023 break;
1024 }
1025 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +00001026 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001027 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +00001028 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001029 break;
1030 }
drhcce7d172000-05-31 15:34:51 +00001031 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +00001032 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001033 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +00001034 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
1035 sqliteVdbeDequoteP3(v, addr);
1036 break;
1037 }
1038 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +00001039 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001040 break;
1041 }
drh50457892003-09-06 01:10:47 +00001042 case TK_VARIABLE: {
drh7c972de2003-09-06 22:18:07 +00001043 sqliteVdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001044 break;
1045 }
drhc9b84a12002-06-20 11:36:48 +00001046 case TK_LT:
1047 case TK_LE:
1048 case TK_GT:
1049 case TK_GE:
1050 case TK_NE:
1051 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001052 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001053 op += 6; /* Convert numeric opcodes to text opcodes */
1054 }
1055 /* Fall through into the next case */
1056 }
drhcce7d172000-05-31 15:34:51 +00001057 case TK_AND:
1058 case TK_OR:
1059 case TK_PLUS:
1060 case TK_STAR:
1061 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001062 case TK_REM:
1063 case TK_BITAND:
1064 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001065 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001066 sqliteExprCode(pParse, pExpr->pLeft);
1067 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001068 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001069 break;
1070 }
drhbf4133c2001-10-13 02:59:08 +00001071 case TK_LSHIFT:
1072 case TK_RSHIFT: {
1073 sqliteExprCode(pParse, pExpr->pRight);
1074 sqliteExprCode(pParse, pExpr->pLeft);
1075 sqliteVdbeAddOp(v, op, 0, 0);
1076 break;
1077 }
drh00400772000-06-16 20:51:26 +00001078 case TK_CONCAT: {
1079 sqliteExprCode(pParse, pExpr->pLeft);
1080 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001081 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001082 break;
1083 }
drh4b59ab52002-08-24 18:24:51 +00001084 case TK_UPLUS: {
1085 Expr *pLeft = pExpr->pLeft;
1086 if( pLeft && pLeft->op==TK_INTEGER ){
drh202b2df2004-01-06 01:13:46 +00001087 if( sqliteFitsIn32Bits(pLeft->token.z) ){
1088 sqliteVdbeAddOp(v, OP_Integer, atoi(pLeft->token.z), 0);
1089 }else{
1090 sqliteVdbeAddOp(v, OP_String, 0, 0);
1091 }
drh4b59ab52002-08-24 18:24:51 +00001092 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1093 }else if( pLeft && pLeft->op==TK_FLOAT ){
1094 sqliteVdbeAddOp(v, OP_String, 0, 0);
1095 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1096 }else{
1097 sqliteExprCode(pParse, pExpr->pLeft);
1098 }
1099 break;
1100 }
drhcce7d172000-05-31 15:34:51 +00001101 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001102 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001103 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001104 Token *p = &pExpr->pLeft->token;
1105 char *z = sqliteMalloc( p->n + 2 );
1106 sprintf(z, "-%.*s", p->n, p->z);
drh202b2df2004-01-06 01:13:46 +00001107 if( pExpr->pLeft->op==TK_INTEGER && sqliteFitsIn32Bits(z) ){
drhe6840902002-03-06 03:08:25 +00001108 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1109 }else{
1110 sqliteVdbeAddOp(v, OP_String, 0, 0);
1111 }
drh99fcd712001-10-13 01:06:47 +00001112 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001113 sqliteFree(z);
1114 break;
1115 }
drh1ccde152000-06-17 13:12:39 +00001116 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001117 }
drhbf4133c2001-10-13 02:59:08 +00001118 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001119 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001120 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001121 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001122 break;
1123 }
1124 case TK_ISNULL:
1125 case TK_NOTNULL: {
1126 int dest;
drh99fcd712001-10-13 01:06:47 +00001127 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001128 sqliteExprCode(pParse, pExpr->pLeft);
1129 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001130 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001131 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001132 break;
1133 }
drh22827922000-06-06 17:27:05 +00001134 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001135 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001136 break;
1137 }
drh4b59ab52002-08-24 18:24:51 +00001138 case TK_GLOB:
1139 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001140 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001141 int i;
1142 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001143 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001144 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001145 int nId;
1146 const char *zId;
1147 getFunctionName(pExpr, &zId, &nId);
1148 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001149 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001150 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001151 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001152 }
drh89425d52002-02-28 03:04:48 +00001153 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001154 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001155 break;
1156 }
drh19a775c2000-06-05 18:54:46 +00001157 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001158 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001159 break;
1160 }
drhfef52082000-06-06 01:50:43 +00001161 case TK_IN: {
1162 int addr;
drh99fcd712001-10-13 01:06:47 +00001163 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001164 sqliteExprCode(pParse, pExpr->pLeft);
1165 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001166 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1167 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1168 sqliteVdbeAddOp(v, OP_String, 0, 0);
1169 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001170 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001171 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001172 }else{
drhf5905aa2002-05-26 20:54:33 +00001173 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001174 }
drh99fcd712001-10-13 01:06:47 +00001175 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001176 break;
1177 }
1178 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001179 sqliteExprCode(pParse, pExpr->pLeft);
1180 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1181 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1182 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1183 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1184 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1185 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1186 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001187 break;
1188 }
drha2e00042002-01-22 03:13:42 +00001189 case TK_AS: {
1190 sqliteExprCode(pParse, pExpr->pLeft);
1191 break;
1192 }
drh17a7f8d2002-03-24 13:13:27 +00001193 case TK_CASE: {
1194 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001195 int jumpInst;
1196 int addr;
1197 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001198 int i;
1199
1200 assert(pExpr->pList);
1201 assert((pExpr->pList->nExpr % 2) == 0);
1202 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001203 nExpr = pExpr->pList->nExpr;
1204 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001205 if( pExpr->pLeft ){
1206 sqliteExprCode(pParse, pExpr->pLeft);
1207 }
drhf5905aa2002-05-26 20:54:33 +00001208 for(i=0; i<nExpr; i=i+2){
1209 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001210 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001211 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001212 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1213 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001214 }else{
drhf570f012002-05-31 15:51:25 +00001215 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001216 }
1217 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001218 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001219 addr = sqliteVdbeCurrentAddr(v);
1220 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001221 }
drhf570f012002-05-31 15:51:25 +00001222 if( pExpr->pLeft ){
1223 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1224 }
drh17a7f8d2002-03-24 13:13:27 +00001225 if( pExpr->pRight ){
1226 sqliteExprCode(pParse, pExpr->pRight);
1227 }else{
drhf5905aa2002-05-26 20:54:33 +00001228 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001229 }
drhf5905aa2002-05-26 20:54:33 +00001230 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001231 break;
1232 }
1233 case TK_RAISE: {
1234 if( !pParse->trigStack ){
drhda93d232003-03-31 02:12:46 +00001235 sqliteErrorMsg(pParse,
1236 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001237 pParse->nErr++;
1238 return;
1239 }
1240 if( pExpr->iColumn == OE_Rollback ||
1241 pExpr->iColumn == OE_Abort ||
1242 pExpr->iColumn == OE_Fail ){
1243 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1244 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1245 sqliteDequote(msg);
1246 sqliteVdbeChangeP3(v, -1, msg, 0);
1247 sqliteFree(msg);
1248 } else {
1249 assert( pExpr->iColumn == OE_Ignore );
1250 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drh483750b2003-01-29 18:46:51 +00001251 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001252 }
drh17a7f8d2002-03-24 13:13:27 +00001253 }
1254 break;
drhcce7d172000-05-31 15:34:51 +00001255 }
drhcce7d172000-05-31 15:34:51 +00001256}
1257
1258/*
1259** Generate code for a boolean expression such that a jump is made
1260** to the label "dest" if the expression is true but execution
1261** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001262**
1263** If the expression evaluates to NULL (neither true nor false), then
1264** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001265*/
drhf5905aa2002-05-26 20:54:33 +00001266void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001267 Vdbe *v = pParse->pVdbe;
1268 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001269 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001270 switch( pExpr->op ){
1271 case TK_LT: op = OP_Lt; break;
1272 case TK_LE: op = OP_Le; break;
1273 case TK_GT: op = OP_Gt; break;
1274 case TK_GE: op = OP_Ge; break;
1275 case TK_NE: op = OP_Ne; break;
1276 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001277 case TK_ISNULL: op = OP_IsNull; break;
1278 case TK_NOTNULL: op = OP_NotNull; break;
1279 default: break;
1280 }
1281 switch( pExpr->op ){
1282 case TK_AND: {
1283 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001284 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1285 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001286 sqliteVdbeResolveLabel(v, d2);
1287 break;
1288 }
1289 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001290 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1291 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001292 break;
1293 }
1294 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001295 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001296 break;
1297 }
1298 case TK_LT:
1299 case TK_LE:
1300 case TK_GT:
1301 case TK_GE:
1302 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001303 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001304 sqliteExprCode(pParse, pExpr->pLeft);
1305 sqliteExprCode(pParse, pExpr->pRight);
drh491791a2002-07-18 00:34:09 +00001306 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001307 op += 6; /* Convert numeric opcodes to text opcodes */
1308 }
drhf5905aa2002-05-26 20:54:33 +00001309 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001310 break;
1311 }
1312 case TK_ISNULL:
1313 case TK_NOTNULL: {
1314 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001315 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001316 break;
1317 }
drhfef52082000-06-06 01:50:43 +00001318 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001319 int addr;
drhcfab11b2000-06-06 03:31:22 +00001320 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001321 addr = sqliteVdbeCurrentAddr(v);
1322 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1323 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1324 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001325 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001326 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001327 }else{
drh99fcd712001-10-13 01:06:47 +00001328 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001329 }
1330 break;
1331 }
1332 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001333 int addr;
drhfef52082000-06-06 01:50:43 +00001334 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001335 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001336 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001337 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001338 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001339 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001340 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001341 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001342 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001343 break;
1344 }
drhcce7d172000-05-31 15:34:51 +00001345 default: {
1346 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001347 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001348 break;
1349 }
1350 }
1351}
1352
1353/*
drh66b89c82000-11-28 20:47:17 +00001354** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001355** to the label "dest" if the expression is false but execution
1356** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001357**
1358** If the expression evaluates to NULL (neither true nor false) then
1359** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001360*/
drhf5905aa2002-05-26 20:54:33 +00001361void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001362 Vdbe *v = pParse->pVdbe;
1363 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001364 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001365 switch( pExpr->op ){
1366 case TK_LT: op = OP_Ge; break;
1367 case TK_LE: op = OP_Gt; break;
1368 case TK_GT: op = OP_Le; break;
1369 case TK_GE: op = OP_Lt; break;
1370 case TK_NE: op = OP_Eq; break;
1371 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001372 case TK_ISNULL: op = OP_NotNull; break;
1373 case TK_NOTNULL: op = OP_IsNull; break;
1374 default: break;
1375 }
1376 switch( pExpr->op ){
1377 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001378 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1379 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001380 break;
1381 }
1382 case TK_OR: {
1383 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001384 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1385 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001386 sqliteVdbeResolveLabel(v, d2);
1387 break;
1388 }
1389 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001390 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001391 break;
1392 }
1393 case TK_LT:
1394 case TK_LE:
1395 case TK_GT:
1396 case TK_GE:
1397 case TK_NE:
1398 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001399 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drh8f619cc2002-09-08 00:04:50 +00001400 /* Convert numeric comparison opcodes into text comparison opcodes.
1401 ** This step depends on the fact that the text comparision opcodes are
1402 ** always 6 greater than their corresponding numeric comparison
1403 ** opcodes.
1404 */
1405 assert( OP_Eq+6 == OP_StrEq );
1406 op += 6;
drhc9b84a12002-06-20 11:36:48 +00001407 }
drhcce7d172000-05-31 15:34:51 +00001408 sqliteExprCode(pParse, pExpr->pLeft);
1409 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001410 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001411 break;
1412 }
drhcce7d172000-05-31 15:34:51 +00001413 case TK_ISNULL:
1414 case TK_NOTNULL: {
1415 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001416 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001417 break;
1418 }
drhfef52082000-06-06 01:50:43 +00001419 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001420 int addr;
drhcfab11b2000-06-06 03:31:22 +00001421 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001422 addr = sqliteVdbeCurrentAddr(v);
1423 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1424 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1425 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001426 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001427 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001428 }else{
drh99fcd712001-10-13 01:06:47 +00001429 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001430 }
1431 break;
1432 }
1433 case TK_BETWEEN: {
1434 int addr;
1435 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001436 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001437 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1438 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001439 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001440 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1441 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001442 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001443 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001444 break;
1445 }
drhcce7d172000-05-31 15:34:51 +00001446 default: {
1447 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001448 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001449 break;
1450 }
1451 }
1452}
drh22827922000-06-06 17:27:05 +00001453
1454/*
1455** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1456** if they are identical and return FALSE if they differ in any way.
1457*/
drhd8bc7082000-06-07 23:51:50 +00001458int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001459 int i;
1460 if( pA==0 ){
1461 return pB==0;
1462 }else if( pB==0 ){
1463 return 0;
1464 }
1465 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001466 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1467 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001468 if( pA->pList ){
1469 if( pB->pList==0 ) return 0;
1470 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1471 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001472 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001473 return 0;
1474 }
1475 }
1476 }else if( pB->pList ){
1477 return 0;
1478 }
1479 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001480 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001481 if( pA->token.z ){
1482 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001483 if( pB->token.n!=pA->token.n ) return 0;
1484 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001485 }
1486 return 1;
1487}
1488
1489/*
1490** Add a new element to the pParse->aAgg[] array and return its index.
1491*/
1492static int appendAggInfo(Parse *pParse){
1493 if( (pParse->nAgg & 0x7)==0 ){
1494 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001495 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1496 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001497 return -1;
1498 }
drh6d4abfb2001-10-22 02:58:08 +00001499 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001500 }
1501 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1502 return pParse->nAgg++;
1503}
1504
1505/*
1506** Analyze the given expression looking for aggregate functions and
1507** for variables that need to be added to the pParse->aAgg[] array.
1508** Make additional entries to the pParse->aAgg[] array as necessary.
1509**
1510** This routine should only be called after the expression has been
1511** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1512**
1513** If errors are seen, leave an error message in zErrMsg and return
1514** the number of errors.
1515*/
1516int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1517 int i;
1518 AggExpr *aAgg;
1519 int nErr = 0;
1520
1521 if( pExpr==0 ) return 0;
1522 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001523 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001524 aAgg = pParse->aAgg;
1525 for(i=0; i<pParse->nAgg; i++){
1526 if( aAgg[i].isAgg ) continue;
1527 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001528 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001529 break;
1530 }
1531 }
1532 if( i>=pParse->nAgg ){
1533 i = appendAggInfo(pParse);
1534 if( i<0 ) return 1;
1535 pParse->aAgg[i].isAgg = 0;
1536 pParse->aAgg[i].pExpr = pExpr;
1537 }
drhaaf88722000-06-08 11:25:00 +00001538 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001539 break;
1540 }
1541 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001542 aAgg = pParse->aAgg;
1543 for(i=0; i<pParse->nAgg; i++){
1544 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001545 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001546 break;
1547 }
1548 }
1549 if( i>=pParse->nAgg ){
1550 i = appendAggInfo(pParse);
1551 if( i<0 ) return 1;
1552 pParse->aAgg[i].isAgg = 1;
1553 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001554 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001555 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001556 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001557 }
1558 pExpr->iAgg = i;
1559 break;
1560 }
1561 default: {
1562 if( pExpr->pLeft ){
1563 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1564 }
1565 if( nErr==0 && pExpr->pRight ){
1566 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1567 }
1568 if( nErr==0 && pExpr->pList ){
1569 int n = pExpr->pList->nExpr;
1570 int i;
1571 for(i=0; nErr==0 && i<n; i++){
1572 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1573 }
1574 }
1575 break;
1576 }
1577 }
1578 return nErr;
1579}
drh8e0a2f92002-02-23 23:45:45 +00001580
1581/*
1582** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001583** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001584** function, or return NULL if the function does not exist.
1585**
drh0bce8352002-02-28 00:41:10 +00001586** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001587** structure is created and liked into the "db" structure if a
1588** no matching function previously existed. When createFlag is true
1589** and the nArg parameter is -1, then only a function that accepts
1590** any number of arguments will be returned.
1591**
1592** If createFlag is false and nArg is -1, then the first valid
1593** function found is returned. A function is valid if either xFunc
1594** or xStep is non-zero.
1595*/
drh0bce8352002-02-28 00:41:10 +00001596FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001597 sqlite *db, /* An open database */
1598 const char *zName, /* Name of the function. Not null-terminated */
1599 int nName, /* Number of characters in the name */
1600 int nArg, /* Number of arguments. -1 means any number */
1601 int createFlag /* Create new entry if true and does not otherwise exist */
1602){
drh0bce8352002-02-28 00:41:10 +00001603 FuncDef *pFirst, *p, *pMaybe;
1604 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001605 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001606 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1607 return p;
1608 }
1609 pMaybe = 0;
1610 while( p && p->nArg!=nArg ){
1611 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1612 p = p->pNext;
1613 }
1614 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1615 return 0;
1616 }
1617 if( p==0 && pMaybe ){
1618 assert( createFlag==0 );
1619 return pMaybe;
1620 }
drh89425d52002-02-28 03:04:48 +00001621 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001622 p->nArg = nArg;
1623 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001624 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001625 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001626 }
1627 return p;
1628}