blob: f706a5c22c38edbffb66d0d8af99a7a55a18dbb5 [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**
drh202b2df2004-01-06 01:13:46 +000015** $Id: expr.c,v 1.103 2004/01/06 01:13:46 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 ){
29 sqliteExprDelete(pLeft);
30 sqliteExprDelete(pRight);
31 return 0;
32 }
33 pNew->op = op;
34 pNew->pLeft = pLeft;
35 pNew->pRight = pRight;
36 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000037 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000038 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +000039 pNew->span = *pToken;
drha76b5df2002-02-23 02:32:10 +000040 }else{
drh4b59ab52002-08-24 18:24:51 +000041 pNew->token.dyn = 0;
drha76b5df2002-02-23 02:32:10 +000042 pNew->token.z = 0;
43 pNew->token.n = 0;
drh6977fea2002-10-22 23:38:04 +000044 if( pLeft && pRight ){
45 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
46 }else{
47 pNew->span = pNew->token;
48 }
drha76b5df2002-02-23 02:32:10 +000049 }
drha76b5df2002-02-23 02:32:10 +000050 return pNew;
51}
52
53/*
drh6977fea2002-10-22 23:38:04 +000054** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +000055** text between the two given tokens.
56*/
57void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh6977fea2002-10-22 23:38:04 +000058 if( pExpr && pRight && pRight->z && pLeft && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +000059 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +000060 pExpr->span.z = pLeft->z;
61 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +000062 }else{
drh6977fea2002-10-22 23:38:04 +000063 pExpr->span.z = 0;
64 pExpr->span.n = 0;
65 pExpr->span.dyn = 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 ){
78 sqliteExprListDelete(pList);
79 return 0;
80 }
81 pNew->op = TK_FUNCTION;
82 pNew->pList = pList;
drh4b59ab52002-08-24 18:24:51 +000083 pNew->token.dyn = 0;
drha76b5df2002-02-23 02:32:10 +000084 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000085 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000086 pNew->token = *pToken;
87 }else{
88 pNew->token.z = 0;
89 pNew->token.n = 0;
90 }
drh6977fea2002-10-22 23:38:04 +000091 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +000092 return pNew;
93}
94
95/*
drha2e00042002-01-22 03:13:42 +000096** Recursively delete an expression tree.
97*/
98void sqliteExprDelete(Expr *p){
99 if( p==0 ) return;
drh6977fea2002-10-22 23:38:04 +0000100 if( p->span.dyn && p->span.z ) sqliteFree((char*)p->span.z);
drh4b59ab52002-08-24 18:24:51 +0000101 if( p->token.dyn && p->token.z ) sqliteFree((char*)p->token.z);
drh75148a22002-03-03 03:42:31 +0000102 if( p->pLeft ) sqliteExprDelete(p->pLeft);
103 if( p->pRight ) sqliteExprDelete(p->pRight);
drha2e00042002-01-22 03:13:42 +0000104 if( p->pList ) sqliteExprListDelete(p->pList);
105 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
106 sqliteFree(p);
107}
108
drha76b5df2002-02-23 02:32:10 +0000109
110/*
drhff78bd22002-02-27 01:47:11 +0000111** The following group of routines make deep copies of expressions,
112** expression lists, ID lists, and select statements. The copies can
113** be deleted (by being passed to their respective ...Delete() routines)
114** without effecting the originals.
115**
drhad3cab52002-05-24 02:04:32 +0000116** The expression list, ID, and source lists return by sqliteExprListDup(),
117** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
118** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000119**
drhad3cab52002-05-24 02:04:32 +0000120** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000121*/
122Expr *sqliteExprDup(Expr *p){
123 Expr *pNew;
124 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000125 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000126 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000127 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000128 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000129 pNew->token.z = sqliteStrDup(p->token.z);
130 pNew->token.dyn = 1;
131 }else{
132 pNew->token.z = 0;
133 pNew->token.n = 0;
134 pNew->token.dyn = 0;
135 }
drh6977fea2002-10-22 23:38:04 +0000136 pNew->span.z = 0;
137 pNew->span.n = 0;
138 pNew->span.dyn = 0;
drhff78bd22002-02-27 01:47:11 +0000139 pNew->pLeft = sqliteExprDup(p->pLeft);
140 pNew->pRight = sqliteExprDup(p->pRight);
141 pNew->pList = sqliteExprListDup(p->pList);
drhff78bd22002-02-27 01:47:11 +0000142 pNew->pSelect = sqliteSelectDup(p->pSelect);
143 return pNew;
144}
drh4b59ab52002-08-24 18:24:51 +0000145void sqliteTokenCopy(Token *pTo, Token *pFrom){
146 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000147 if( pFrom->z ){
148 pTo->n = pFrom->n;
149 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
150 pTo->dyn = 1;
151 }else{
152 pTo->n = 0;
153 pTo->z = 0;
154 pTo->dyn = 0;
155 }
156}
drhff78bd22002-02-27 01:47:11 +0000157ExprList *sqliteExprListDup(ExprList *p){
158 ExprList *pNew;
159 int i;
160 if( p==0 ) return 0;
161 pNew = sqliteMalloc( sizeof(*pNew) );
162 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000163 pNew->nExpr = pNew->nAlloc = p->nExpr;
drhff78bd22002-02-27 01:47:11 +0000164 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000165 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000166 for(i=0; i<p->nExpr; i++){
drh4b59ab52002-08-24 18:24:51 +0000167 Expr *pNewExpr, *pOldExpr;
168 pNew->a[i].pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000169 if( pOldExpr->span.z!=0 && pNewExpr ){
170 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000171 ** expression list. The logic in SELECT processing that determines
172 ** the names of columns in the result set needs this information */
drh6977fea2002-10-22 23:38:04 +0000173 sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000174 }
drh1f3e9052002-10-31 00:09:39 +0000175 assert( pNewExpr==0 || pNewExpr->span.z!=0
176 || pOldExpr->span.z==0 || sqlite_malloc_failed );
drhff78bd22002-02-27 01:47:11 +0000177 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
178 pNew->a[i].sortOrder = p->a[i].sortOrder;
179 pNew->a[i].isAgg = p->a[i].isAgg;
180 pNew->a[i].done = 0;
181 }
182 return pNew;
183}
drhad3cab52002-05-24 02:04:32 +0000184SrcList *sqliteSrcListDup(SrcList *p){
185 SrcList *pNew;
186 int i;
drh113088e2003-03-20 01:16:58 +0000187 int nByte;
drhad3cab52002-05-24 02:04:32 +0000188 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000189 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
190 pNew = sqliteMalloc( nByte );
drhad3cab52002-05-24 02:04:32 +0000191 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000192 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000193 for(i=0; i<p->nSrc; i++){
drhf26e09c2003-05-31 16:21:12 +0000194 pNew->a[i].zDatabase = sqliteStrDup(p->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +0000195 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
196 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
197 pNew->a[i].jointype = p->a[i].jointype;
drh6a3ea0e2003-05-02 14:32:12 +0000198 pNew->a[i].iCursor = p->a[i].iCursor;
drhad3cab52002-05-24 02:04:32 +0000199 pNew->a[i].pTab = 0;
200 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
201 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
202 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
203 }
204 return pNew;
205}
drhff78bd22002-02-27 01:47:11 +0000206IdList *sqliteIdListDup(IdList *p){
207 IdList *pNew;
208 int i;
209 if( p==0 ) return 0;
210 pNew = sqliteMalloc( sizeof(*pNew) );
211 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000212 pNew->nId = pNew->nAlloc = p->nId;
drhff78bd22002-02-27 01:47:11 +0000213 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000214 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000215 for(i=0; i<p->nId; i++){
216 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
drhff78bd22002-02-27 01:47:11 +0000217 pNew->a[i].idx = p->a[i].idx;
drhff78bd22002-02-27 01:47:11 +0000218 }
219 return pNew;
220}
221Select *sqliteSelectDup(Select *p){
222 Select *pNew;
223 if( p==0 ) return 0;
224 pNew = sqliteMalloc( sizeof(*p) );
225 if( pNew==0 ) return 0;
226 pNew->isDistinct = p->isDistinct;
227 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000228 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000229 pNew->pWhere = sqliteExprDup(p->pWhere);
230 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
231 pNew->pHaving = sqliteExprDup(p->pHaving);
232 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
233 pNew->op = p->op;
234 pNew->pPrior = sqliteSelectDup(p->pPrior);
235 pNew->nLimit = p->nLimit;
236 pNew->nOffset = p->nOffset;
237 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000238 pNew->iLimit = -1;
239 pNew->iOffset = -1;
drhff78bd22002-02-27 01:47:11 +0000240 return pNew;
241}
242
243
244/*
drha76b5df2002-02-23 02:32:10 +0000245** Add a new element to the end of an expression list. If pList is
246** initially NULL, then create a new expression list.
247*/
248ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
249 int i;
250 if( pList==0 ){
251 pList = sqliteMalloc( sizeof(ExprList) );
252 if( pList==0 ){
253 sqliteExprDelete(pExpr);
254 return 0;
255 }
drh4305d102003-07-30 12:34:12 +0000256 pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000257 }
drh4305d102003-07-30 12:34:12 +0000258 if( pList->nAlloc<=pList->nExpr ){
drha76b5df2002-02-23 02:32:10 +0000259 struct ExprList_item *a;
drh4305d102003-07-30 12:34:12 +0000260 pList->nAlloc = pList->nAlloc*2 + 4;
261 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
drha76b5df2002-02-23 02:32:10 +0000262 if( a==0 ){
263 sqliteExprDelete(pExpr);
264 return pList;
265 }
266 pList->a = a;
267 }
drhf9112212003-09-23 10:25:32 +0000268 if( pList->a && (pExpr || pName) ){
drha76b5df2002-02-23 02:32:10 +0000269 i = pList->nExpr++;
drh256ada02003-12-10 03:13:43 +0000270 memset(&pList->a[i], 0, sizeof(pList->a[i]));
drha76b5df2002-02-23 02:32:10 +0000271 pList->a[i].pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000272 if( pName ){
273 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
274 sqliteDequote(pList->a[i].zName);
275 }
276 }
277 return pList;
278}
279
280/*
281** Delete an entire expression list.
282*/
283void sqliteExprListDelete(ExprList *pList){
284 int i;
285 if( pList==0 ) return;
286 for(i=0; i<pList->nExpr; i++){
287 sqliteExprDelete(pList->a[i].pExpr);
288 sqliteFree(pList->a[i].zName);
289 }
290 sqliteFree(pList->a);
291 sqliteFree(pList);
292}
293
294/*
drhfef52082000-06-06 01:50:43 +0000295** Walk an expression tree. Return 1 if the expression is constant
296** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000297**
298** For the purposes of this function, a double-quoted string (ex: "abc")
299** is considered a variable but a single-quoted string (ex: 'abc') is
300** a constant.
drhfef52082000-06-06 01:50:43 +0000301*/
drh92086432002-01-22 14:11:29 +0000302int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000303 switch( p->op ){
304 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000305 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000306 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000307 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000308 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000309 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000310 case TK_STRING:
drh92086432002-01-22 14:11:29 +0000311 case TK_INTEGER:
312 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000313 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000314 return 1;
drhfef52082000-06-06 01:50:43 +0000315 default: {
drh92086432002-01-22 14:11:29 +0000316 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
317 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000318 if( p->pList ){
319 int i;
320 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000321 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000322 }
323 }
drh92086432002-01-22 14:11:29 +0000324 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000325 }
326 }
drh92086432002-01-22 14:11:29 +0000327 return 0;
drhfef52082000-06-06 01:50:43 +0000328}
329
330/*
drh202b2df2004-01-06 01:13:46 +0000331** If the given expression codes a constant integer that is small enough
332** to fit in a 32-bit integer, return 1 and put the value of the integer
333** in *pValue. If the expression is not an integer or if it is too big
334** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000335*/
336int sqliteExprIsInteger(Expr *p, int *pValue){
337 switch( p->op ){
338 case TK_INTEGER: {
drh202b2df2004-01-06 01:13:46 +0000339 if( sqliteFitsIn32Bits(p->token.z) ){
340 *pValue = atoi(p->token.z);
341 return 1;
342 }
343 break;
drhe4de1fe2002-06-02 16:09:01 +0000344 }
345 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000346 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000347 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000348 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000349 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drh202b2df2004-01-06 01:13:46 +0000350 if( n==0 && sqliteFitsIn32Bits(p->token.z) ){
drhe4de1fe2002-06-02 16:09:01 +0000351 *pValue = atoi(p->token.z);
352 return 1;
353 }
354 break;
355 }
drh4b59ab52002-08-24 18:24:51 +0000356 case TK_UPLUS: {
357 return sqliteExprIsInteger(p->pLeft, pValue);
358 }
drhe4de1fe2002-06-02 16:09:01 +0000359 case TK_UMINUS: {
360 int v;
361 if( sqliteExprIsInteger(p->pLeft, &v) ){
362 *pValue = -v;
363 return 1;
364 }
365 break;
366 }
367 default: break;
368 }
369 return 0;
370}
371
372/*
drhc4a3c772001-04-04 11:48:57 +0000373** Return TRUE if the given string is a row-id column name.
374*/
drha9f9d1c2002-06-29 02:20:08 +0000375int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000376 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
377 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
378 if( sqliteStrICmp(z, "OID")==0 ) return 1;
379 return 0;
380}
381
382/*
drhcce7d172000-05-31 15:34:51 +0000383** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000384** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000385** index to the table in the table list and a column offset. The
386** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
387** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000388** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000389** VDBE cursor number for a cursor that is pointing into the referenced
390** table. The Expr.iColumn value is changed to the index of the column
391** of the referenced table. The Expr.iColumn value for the special
392** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
393** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000394**
drhfef52082000-06-06 01:50:43 +0000395** We also check for instances of the IN operator. IN comes in two
396** forms:
397**
398** expr IN (exprlist)
399** and
400** expr IN (SELECT ...)
401**
402** The first form is handled by creating a set holding the list
403** of allowed values. The second form causes the SELECT to generate
404** a temporary table.
405**
406** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000407** If it finds any, it generates code to write the value of that select
408** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000409**
drh967e8b72000-06-21 13:59:10 +0000410** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000411** the number of errors seen and leaves an error message on pParse->zErrMsg.
412*/
drha2e00042002-01-22 03:13:42 +0000413int sqliteExprResolveIds(
414 Parse *pParse, /* The parser context */
drhad3cab52002-05-24 02:04:32 +0000415 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000416 ExprList *pEList, /* List of expressions used to resolve "AS" */
417 Expr *pExpr /* The expression to be analyzed. */
418){
drh6a3ea0e2003-05-02 14:32:12 +0000419 int i;
420
drhdaffd0e2001-04-11 14:28:42 +0000421 if( pExpr==0 || pTabList==0 ) return 0;
drh6a3ea0e2003-05-02 14:32:12 +0000422 for(i=0; i<pTabList->nSrc; i++){
423 assert( pTabList->a[i].iCursor>=0 && pTabList->a[i].iCursor<pParse->nTab );
424 }
drhcce7d172000-05-31 15:34:51 +0000425 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000426 /* Double-quoted strings (ex: "abc") are used as identifiers if
427 ** possible. Otherwise they remain as strings. Single-quoted
428 ** strings (ex: 'abc') are always string literals.
429 */
430 case TK_STRING: {
431 if( pExpr->token.z[0]=='\'' ) break;
432 /* Fall thru into the TK_ID case if this is a double-quoted string */
433 }
drha2e00042002-01-22 03:13:42 +0000434 /* A lone identifier. Try and match it as follows:
435 **
436 ** 1. To the name of a column of one of the tables in pTabList
437 **
438 ** 2. To the right side of an AS keyword in the column list of
439 ** a SELECT statement. (For example, match against 'x' in
440 ** "SELECT a+b AS 'x' FROM t1".)
441 **
442 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
443 */
drhcce7d172000-05-31 15:34:51 +0000444 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000445 int cnt = 0; /* Number of matches */
drha76b5df2002-02-23 02:32:10 +0000446 char *z;
drhe22a3342003-04-22 20:30:37 +0000447 int iDb = -1;
448
drha76b5df2002-02-23 02:32:10 +0000449 assert( pExpr->token.z );
450 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000451 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000452 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000453 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000454 int j;
455 Table *pTab = pTabList->a[i].pTab;
456 if( pTab==0 ) continue;
drhe22a3342003-04-22 20:30:37 +0000457 iDb = pTab->iDb;
drh417be792002-03-03 18:59:40 +0000458 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000459 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000460 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000461 cnt++;
drh6a3ea0e2003-05-02 14:32:12 +0000462 pExpr->iTable = pTabList->a[i].iCursor;
drhe22a3342003-04-22 20:30:37 +0000463 pExpr->iDb = pTab->iDb;
drh4a324312001-12-21 14:30:42 +0000464 if( j==pTab->iPKey ){
465 /* Substitute the record number for the INTEGER PRIMARY KEY */
466 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000467 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000468 }else{
469 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000470 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000471 }
drha2e00042002-01-22 03:13:42 +0000472 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000473 }
474 }
475 }
drha2e00042002-01-22 03:13:42 +0000476 if( cnt==0 && pEList!=0 ){
477 int j;
478 for(j=0; j<pEList->nExpr; j++){
479 char *zAs = pEList->a[j].zName;
480 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
481 cnt++;
482 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
483 pExpr->op = TK_AS;
484 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000485 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000486 }
487 }
488 }
drhe22a3342003-04-22 20:30:37 +0000489 if( cnt==0 && iDb>=0 && sqliteIsRowid(z) ){
drhc4a3c772001-04-04 11:48:57 +0000490 pExpr->iColumn = -1;
drh6a3ea0e2003-05-02 14:32:12 +0000491 pExpr->iTable = pTabList->a[0].iCursor;
drhe22a3342003-04-22 20:30:37 +0000492 pExpr->iDb = iDb;
drhad3cab52002-05-24 02:04:32 +0000493 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000494 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000495 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000496 }
drhcce7d172000-05-31 15:34:51 +0000497 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000498 if( cnt==0 && pExpr->token.z[0]!='"' ){
drhda93d232003-03-31 02:12:46 +0000499 sqliteErrorMsg(pParse, "no such column: %T", &pExpr->token);
drhcce7d172000-05-31 15:34:51 +0000500 return 1;
501 }else if( cnt>1 ){
drhda93d232003-03-31 02:12:46 +0000502 sqliteErrorMsg(pParse, "ambiguous column name: %T", &pExpr->token);
drhcce7d172000-05-31 15:34:51 +0000503 return 1;
504 }
drhed6c8672003-01-12 18:02:16 +0000505 if( pExpr->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000506 sqliteAuthRead(pParse, pExpr, pTabList);
drhed6c8672003-01-12 18:02:16 +0000507 }
drhcce7d172000-05-31 15:34:51 +0000508 break;
509 }
510
drhd24cc422003-03-27 12:51:24 +0000511 /* A table name and column name: ID.ID
512 ** Or a database, table and column: ID.ID.ID
513 */
drhcce7d172000-05-31 15:34:51 +0000514 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000515 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000516 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000517 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000518 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000519 char *zLeft, *zRight; /* Text of an identifier */
drhd24cc422003-03-27 12:51:24 +0000520 char *zDb; /* Name of database holding table */
521 sqlite *db = pParse->db;
drhcce7d172000-05-31 15:34:51 +0000522
drhcce7d172000-05-31 15:34:51 +0000523 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000524 if( pRight->op==TK_ID ){
525 pLeft = pExpr->pLeft;
526 zDb = 0;
527 }else{
528 Expr *pDb = pExpr->pLeft;
529 assert( pDb && pDb->op==TK_ID && pDb->token.z );
530 zDb = sqliteStrNDup(pDb->token.z, pDb->token.n);
531 pLeft = pRight->pLeft;
532 pRight = pRight->pRight;
533 }
drha76b5df2002-02-23 02:32:10 +0000534 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
535 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000536 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
537 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000538 if( zLeft==0 || zRight==0 ){
539 sqliteFree(zLeft);
540 sqliteFree(zRight);
drhd24cc422003-03-27 12:51:24 +0000541 sqliteFree(zDb);
drhdaffd0e2001-04-11 14:28:42 +0000542 return 1;
543 }
drhd24cc422003-03-27 12:51:24 +0000544 sqliteDequote(zDb);
drh87c40e82001-07-23 14:33:02 +0000545 sqliteDequote(zLeft);
546 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000547 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000548 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000549 int j;
550 char *zTab;
551 Table *pTab = pTabList->a[i].pTab;
552 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000553 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000554 if( pTabList->a[i].zAlias ){
555 zTab = pTabList->a[i].zAlias;
drhd24cc422003-03-27 12:51:24 +0000556 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhcce7d172000-05-31 15:34:51 +0000557 }else{
558 zTab = pTab->zName;
drhd24cc422003-03-27 12:51:24 +0000559 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
560 if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
561 continue;
562 }
drhcce7d172000-05-31 15:34:51 +0000563 }
drhe22a3342003-04-22 20:30:37 +0000564 if( 0==(cntTab++) ){
drh6a3ea0e2003-05-02 14:32:12 +0000565 pExpr->iTable = pTabList->a[i].iCursor;
drhe22a3342003-04-22 20:30:37 +0000566 pExpr->iDb = pTab->iDb;
567 }
drhcce7d172000-05-31 15:34:51 +0000568 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000569 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000570 cnt++;
drh6a3ea0e2003-05-02 14:32:12 +0000571 pExpr->iTable = pTabList->a[i].iCursor;
drhe22a3342003-04-22 20:30:37 +0000572 pExpr->iDb = pTab->iDb;
drh70ce3f02003-04-15 19:22:22 +0000573 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
574 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
drhc9b84a12002-06-20 11:36:48 +0000575 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000576 }
577 }
578 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000579
580 /* If we have not already resolved this *.* expression, then maybe
581 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000582 if( cnt == 0 && pParse->trigStack != 0 ){
583 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000584 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000585 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
586 pExpr->iTable = pTriggerStack->newIdx;
drhe22a3342003-04-22 20:30:37 +0000587 assert( pTriggerStack->pTab );
588 pExpr->iDb = pTriggerStack->pTab->iDb;
danielk1977c3f9bad2002-05-15 08:30:12 +0000589 cntTab++;
590 t = 1;
591 }
danielk1977f29ce552002-05-19 23:43:12 +0000592 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
593 pExpr->iTable = pTriggerStack->oldIdx;
drhe22a3342003-04-22 20:30:37 +0000594 assert( pTriggerStack->pTab );
595 pExpr->iDb = pTriggerStack->pTab->iDb;
danielk1977c3f9bad2002-05-15 08:30:12 +0000596 cntTab++;
597 t = 1;
598 }
599
danielk1977f29ce552002-05-19 23:43:12 +0000600 if( t ){
601 int j;
drhc9b84a12002-06-20 11:36:48 +0000602 Table *pTab = pTriggerStack->pTab;
603 for(j=0; j < pTab->nCol; j++) {
604 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000605 cnt++;
drh70ce3f02003-04-15 19:22:22 +0000606 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
drhc9b84a12002-06-20 11:36:48 +0000607 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000608 }
609 }
danielk1977f29ce552002-05-19 23:43:12 +0000610 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000611 }
612
drhc4a3c772001-04-04 11:48:57 +0000613 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
614 cnt = 1;
615 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000616 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000617 }
drhd24cc422003-03-27 12:51:24 +0000618 sqliteFree(zDb);
drhcce7d172000-05-31 15:34:51 +0000619 sqliteFree(zLeft);
620 sqliteFree(zRight);
621 if( cnt==0 ){
drhda93d232003-03-31 02:12:46 +0000622 sqliteErrorMsg(pParse, "no such column: %T.%T",
623 &pLeft->token, &pRight->token);
drhcce7d172000-05-31 15:34:51 +0000624 return 1;
625 }else if( cnt>1 ){
drhda93d232003-03-31 02:12:46 +0000626 sqliteErrorMsg(pParse, "ambiguous column name: %T.%T",
627 &pLeft->token, &pRight->token);
drhcce7d172000-05-31 15:34:51 +0000628 return 1;
629 }
drhd24cc422003-03-27 12:51:24 +0000630 sqliteExprDelete(pExpr->pLeft);
drhcce7d172000-05-31 15:34:51 +0000631 pExpr->pLeft = 0;
drhd24cc422003-03-27 12:51:24 +0000632 sqliteExprDelete(pExpr->pRight);
drhcce7d172000-05-31 15:34:51 +0000633 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000634 pExpr->op = TK_COLUMN;
drh6a3ea0e2003-05-02 14:32:12 +0000635 sqliteAuthRead(pParse, pExpr, pTabList);
drhcce7d172000-05-31 15:34:51 +0000636 break;
637 }
638
drhfef52082000-06-06 01:50:43 +0000639 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000640 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000641 if( v==0 ) return 1;
drh6a3ea0e2003-05-02 14:32:12 +0000642 if( sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000643 return 1;
644 }
drhfef52082000-06-06 01:50:43 +0000645 if( pExpr->pSelect ){
646 /* Case 1: expr IN (SELECT ...)
647 **
648 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000649 ** table. The cursor number of the temporary table has already
650 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000651 */
drh832508b2002-03-02 17:04:07 +0000652 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000653 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000654 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000655 }else if( pExpr->pList ){
656 /* Case 2: expr IN (exprlist)
657 **
658 ** Create a set to put the exprlist values in. The Set id is stored
659 ** in iTable.
660 */
661 int i, iSet;
662 for(i=0; i<pExpr->pList->nExpr; i++){
663 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000664 if( !sqliteExprIsConstant(pE2) ){
drhda93d232003-03-31 02:12:46 +0000665 sqliteErrorMsg(pParse,
666 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000667 return 1;
668 }
drh4794b982000-06-06 13:54:14 +0000669 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
670 return 1;
671 }
drhfef52082000-06-06 01:50:43 +0000672 }
673 iSet = pExpr->iTable = pParse->nSet++;
674 for(i=0; i<pExpr->pList->nExpr; i++){
675 Expr *pE2 = pExpr->pList->a[i].pExpr;
676 switch( pE2->op ){
677 case TK_FLOAT:
678 case TK_INTEGER:
679 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000680 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000681 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000682 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
683 sqliteVdbeDequoteP3(v, addr);
684 break;
685 }
686 default: {
687 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000688 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000689 break;
690 }
691 }
692 }
693 }
drhcfab11b2000-06-06 03:31:22 +0000694 break;
drhfef52082000-06-06 01:50:43 +0000695 }
696
drh19a775c2000-06-05 18:54:46 +0000697 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000698 /* This has to be a scalar SELECT. Generate code to put the
699 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000700 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000701 */
drh967e8b72000-06-21 13:59:10 +0000702 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000703 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000704 return 1;
705 }
706 break;
707 }
708
drhcce7d172000-05-31 15:34:51 +0000709 /* For all else, just recursively walk the tree */
710 default: {
drh4794b982000-06-06 13:54:14 +0000711 if( pExpr->pLeft
drh6a3ea0e2003-05-02 14:32:12 +0000712 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000713 return 1;
714 }
715 if( pExpr->pRight
drh6a3ea0e2003-05-02 14:32:12 +0000716 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000717 return 1;
718 }
719 if( pExpr->pList ){
720 int i;
721 ExprList *pList = pExpr->pList;
722 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000723 Expr *pArg = pList->a[i].pExpr;
drh6a3ea0e2003-05-02 14:32:12 +0000724 if( sqliteExprResolveIds(pParse, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000725 return 1;
726 }
727 }
728 }
729 }
730 }
731 return 0;
732}
733
drhcce7d172000-05-31 15:34:51 +0000734/*
drh4b59ab52002-08-24 18:24:51 +0000735** pExpr is a node that defines a function of some kind. It might
736** be a syntactic function like "count(x)" or it might be a function
737** that implements an operator, like "a LIKE b".
738**
739** This routine makes *pzName point to the name of the function and
740** *pnName hold the number of characters in the function name.
741*/
742static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
743 switch( pExpr->op ){
744 case TK_FUNCTION: {
745 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000746 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000747 break;
748 }
749 case TK_LIKE: {
750 *pzName = "like";
751 *pnName = 4;
752 break;
753 }
754 case TK_GLOB: {
755 *pzName = "glob";
756 *pnName = 4;
757 break;
758 }
759 default: {
760 *pzName = "can't happen";
761 *pnName = 12;
762 break;
763 }
764 }
765}
766
767/*
drhcce7d172000-05-31 15:34:51 +0000768** Error check the functions in an expression. Make sure all
769** function names are recognized and all functions have the correct
770** number of arguments. Leave an error message in pParse->zErrMsg
771** if anything is amiss. Return the number of errors.
772**
773** if pIsAgg is not null and this expression is an aggregate function
774** (like count(*) or max(value)) then write a 1 into *pIsAgg.
775*/
776int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
777 int nErr = 0;
778 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000779 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000780 case TK_GLOB:
781 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000782 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000783 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
784 int no_such_func = 0; /* True if no such function exists */
785 int is_type_of = 0; /* True if is the special TypeOf() function */
786 int wrong_num_args = 0; /* True if wrong number of arguments */
787 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000788 int i;
drh4b59ab52002-08-24 18:24:51 +0000789 int nId; /* Number of characters in function name */
790 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000791 FuncDef *pDef;
792
drh4b59ab52002-08-24 18:24:51 +0000793 getFunctionName(pExpr, &zId, &nId);
794 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000795 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000796 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000797 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000798 if( n==1 && nId==6 && sqliteStrNICmp(zId, "typeof", 6)==0 ){
drhc9b84a12002-06-20 11:36:48 +0000799 is_type_of = 1;
800 }else {
801 no_such_func = 1;
802 }
drh0bce8352002-02-28 00:41:10 +0000803 }else{
804 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000805 }
drh0bce8352002-02-28 00:41:10 +0000806 }else{
807 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000808 }
drh8e0a2f92002-02-23 23:45:45 +0000809 if( is_agg && !allowAgg ){
810 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
drh4b59ab52002-08-24 18:24:51 +0000811 zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000812 pParse->nErr++;
813 nErr++;
814 is_agg = 0;
815 }else if( no_such_func ){
drh4b59ab52002-08-24 18:24:51 +0000816 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, zId,nId,0);
drhcce7d172000-05-31 15:34:51 +0000817 pParse->nErr++;
818 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000819 }else if( wrong_num_args ){
820 sqliteSetNString(&pParse->zErrMsg,
drh4b59ab52002-08-24 18:24:51 +0000821 "wrong number of arguments to function ", -1, zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000822 pParse->nErr++;
823 nErr++;
drhcce7d172000-05-31 15:34:51 +0000824 }
drh22827922000-06-06 17:27:05 +0000825 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000826 if( is_agg && pIsAgg ) *pIsAgg = 1;
827 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000828 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
829 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000830 }
drhc9b84a12002-06-20 11:36:48 +0000831 if( pDef==0 ){
832 if( is_type_of ){
833 pExpr->op = TK_STRING;
834 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
835 pExpr->token.z = "numeric";
836 pExpr->token.n = 7;
837 }else{
838 pExpr->token.z = "text";
839 pExpr->token.n = 4;
840 }
841 }
842 }else if( pDef->dataType>=0 ){
843 if( pDef->dataType<n ){
844 pExpr->dataType =
845 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
846 }else{
847 pExpr->dataType = SQLITE_SO_NUM;
848 }
849 }else if( pDef->dataType==SQLITE_ARGS ){
850 pDef->dataType = SQLITE_SO_TEXT;
851 for(i=0; i<n; i++){
852 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
853 pExpr->dataType = SQLITE_SO_NUM;
854 break;
855 }
856 }
857 }else if( pDef->dataType==SQLITE_NUMERIC ){
858 pExpr->dataType = SQLITE_SO_NUM;
859 }else{
860 pExpr->dataType = SQLITE_SO_TEXT;
861 }
drhcce7d172000-05-31 15:34:51 +0000862 }
863 default: {
864 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000865 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000866 }
867 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000868 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000869 }
drhfef52082000-06-06 01:50:43 +0000870 if( nErr==0 && pExpr->pList ){
871 int n = pExpr->pList->nExpr;
872 int i;
873 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000874 Expr *pE2 = pExpr->pList->a[i].pExpr;
875 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000876 }
877 }
drhcce7d172000-05-31 15:34:51 +0000878 break;
879 }
880 }
881 return nErr;
882}
883
884/*
drhc9b84a12002-06-20 11:36:48 +0000885** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
886** given expression should sort as numeric values or as text.
887**
888** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
889** both been called on the expression before it is passed to this routine.
890*/
891int sqliteExprType(Expr *p){
892 if( p==0 ) return SQLITE_SO_NUM;
893 while( p ) switch( p->op ){
894 case TK_PLUS:
895 case TK_MINUS:
896 case TK_STAR:
897 case TK_SLASH:
898 case TK_AND:
899 case TK_OR:
900 case TK_ISNULL:
901 case TK_NOTNULL:
902 case TK_NOT:
903 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +0000904 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +0000905 case TK_BITAND:
906 case TK_BITOR:
907 case TK_BITNOT:
908 case TK_LSHIFT:
909 case TK_RSHIFT:
910 case TK_REM:
911 case TK_INTEGER:
912 case TK_FLOAT:
913 case TK_IN:
914 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +0000915 case TK_GLOB:
916 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +0000917 return SQLITE_SO_NUM;
918
919 case TK_STRING:
920 case TK_NULL:
921 case TK_CONCAT:
drh50457892003-09-06 01:10:47 +0000922 case TK_VARIABLE:
drhc9b84a12002-06-20 11:36:48 +0000923 return SQLITE_SO_TEXT;
924
925 case TK_LT:
926 case TK_LE:
927 case TK_GT:
928 case TK_GE:
929 case TK_NE:
930 case TK_EQ:
931 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
932 return SQLITE_SO_NUM;
933 }
934 p = p->pRight;
935 break;
936
937 case TK_AS:
938 p = p->pLeft;
939 break;
940
941 case TK_COLUMN:
942 case TK_FUNCTION:
943 case TK_AGG_FUNCTION:
944 return p->dataType;
945
946 case TK_SELECT:
947 assert( p->pSelect );
948 assert( p->pSelect->pEList );
949 assert( p->pSelect->pEList->nExpr>0 );
950 p = p->pSelect->pEList->a[0].pExpr;
951 break;
952
drhb1363202002-06-26 02:45:03 +0000953 case TK_CASE: {
954 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
955 return SQLITE_SO_NUM;
956 }
957 if( p->pList ){
958 int i;
959 ExprList *pList = p->pList;
960 for(i=1; i<pList->nExpr; i+=2){
961 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
962 return SQLITE_SO_NUM;
963 }
964 }
965 }
966 return SQLITE_SO_TEXT;
967 }
968
drhc9b84a12002-06-20 11:36:48 +0000969 default:
970 assert( p->op==TK_ABORT ); /* Can't Happen */
971 break;
972 }
973 return SQLITE_SO_NUM;
974}
975
976/*
drh202b2df2004-01-06 01:13:46 +0000977** Run
978
979/*
drhcce7d172000-05-31 15:34:51 +0000980** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000981** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000982*/
983void sqliteExprCode(Parse *pParse, Expr *pExpr){
984 Vdbe *v = pParse->pVdbe;
985 int op;
drhdaffd0e2001-04-11 14:28:42 +0000986 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000987 switch( pExpr->op ){
988 case TK_PLUS: op = OP_Add; break;
989 case TK_MINUS: op = OP_Subtract; break;
990 case TK_STAR: op = OP_Multiply; break;
991 case TK_SLASH: op = OP_Divide; break;
992 case TK_AND: op = OP_And; break;
993 case TK_OR: op = OP_Or; break;
994 case TK_LT: op = OP_Lt; break;
995 case TK_LE: op = OP_Le; break;
996 case TK_GT: op = OP_Gt; break;
997 case TK_GE: op = OP_Ge; break;
998 case TK_NE: op = OP_Ne; break;
999 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001000 case TK_ISNULL: op = OP_IsNull; break;
1001 case TK_NOTNULL: op = OP_NotNull; break;
1002 case TK_NOT: op = OP_Not; break;
1003 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001004 case TK_BITAND: op = OP_BitAnd; break;
1005 case TK_BITOR: op = OP_BitOr; break;
1006 case TK_BITNOT: op = OP_BitNot; break;
1007 case TK_LSHIFT: op = OP_ShiftLeft; break;
1008 case TK_RSHIFT: op = OP_ShiftRight; break;
1009 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +00001010 default: break;
1011 }
1012 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001013 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001014 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +00001015 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001016 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +00001017 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001018 }else{
drh99fcd712001-10-13 01:06:47 +00001019 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001020 }
drhcce7d172000-05-31 15:34:51 +00001021 break;
1022 }
1023 case TK_INTEGER: {
drh202b2df2004-01-06 01:13:46 +00001024 if( !sqliteFitsIn32Bits(pExpr->token.z) ){
drhd9e30932002-06-09 01:16:01 +00001025 sqliteVdbeAddOp(v, OP_String, 0, 0);
1026 }else{
drh202b2df2004-01-06 01:13:46 +00001027 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
drhd9e30932002-06-09 01:16:01 +00001028 }
drhe6840902002-03-06 03:08:25 +00001029 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1030 break;
1031 }
1032 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +00001033 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001034 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +00001035 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001036 break;
1037 }
drhcce7d172000-05-31 15:34:51 +00001038 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +00001039 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001040 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +00001041 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
1042 sqliteVdbeDequoteP3(v, addr);
1043 break;
1044 }
1045 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +00001046 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001047 break;
1048 }
drh50457892003-09-06 01:10:47 +00001049 case TK_VARIABLE: {
drh7c972de2003-09-06 22:18:07 +00001050 sqliteVdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001051 break;
1052 }
drhc9b84a12002-06-20 11:36:48 +00001053 case TK_LT:
1054 case TK_LE:
1055 case TK_GT:
1056 case TK_GE:
1057 case TK_NE:
1058 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001059 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001060 op += 6; /* Convert numeric opcodes to text opcodes */
1061 }
1062 /* Fall through into the next case */
1063 }
drhcce7d172000-05-31 15:34:51 +00001064 case TK_AND:
1065 case TK_OR:
1066 case TK_PLUS:
1067 case TK_STAR:
1068 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001069 case TK_REM:
1070 case TK_BITAND:
1071 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001072 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001073 sqliteExprCode(pParse, pExpr->pLeft);
1074 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001075 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001076 break;
1077 }
drhbf4133c2001-10-13 02:59:08 +00001078 case TK_LSHIFT:
1079 case TK_RSHIFT: {
1080 sqliteExprCode(pParse, pExpr->pRight);
1081 sqliteExprCode(pParse, pExpr->pLeft);
1082 sqliteVdbeAddOp(v, op, 0, 0);
1083 break;
1084 }
drh00400772000-06-16 20:51:26 +00001085 case TK_CONCAT: {
1086 sqliteExprCode(pParse, pExpr->pLeft);
1087 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001088 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001089 break;
1090 }
drh4b59ab52002-08-24 18:24:51 +00001091 case TK_UPLUS: {
1092 Expr *pLeft = pExpr->pLeft;
1093 if( pLeft && pLeft->op==TK_INTEGER ){
drh202b2df2004-01-06 01:13:46 +00001094 if( sqliteFitsIn32Bits(pLeft->token.z) ){
1095 sqliteVdbeAddOp(v, OP_Integer, atoi(pLeft->token.z), 0);
1096 }else{
1097 sqliteVdbeAddOp(v, OP_String, 0, 0);
1098 }
drh4b59ab52002-08-24 18:24:51 +00001099 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1100 }else if( pLeft && pLeft->op==TK_FLOAT ){
1101 sqliteVdbeAddOp(v, OP_String, 0, 0);
1102 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1103 }else{
1104 sqliteExprCode(pParse, pExpr->pLeft);
1105 }
1106 break;
1107 }
drhcce7d172000-05-31 15:34:51 +00001108 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001109 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001110 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001111 Token *p = &pExpr->pLeft->token;
1112 char *z = sqliteMalloc( p->n + 2 );
1113 sprintf(z, "-%.*s", p->n, p->z);
drh202b2df2004-01-06 01:13:46 +00001114 if( pExpr->pLeft->op==TK_INTEGER && sqliteFitsIn32Bits(z) ){
drhe6840902002-03-06 03:08:25 +00001115 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1116 }else{
1117 sqliteVdbeAddOp(v, OP_String, 0, 0);
1118 }
drh99fcd712001-10-13 01:06:47 +00001119 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001120 sqliteFree(z);
1121 break;
1122 }
drh1ccde152000-06-17 13:12:39 +00001123 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001124 }
drhbf4133c2001-10-13 02:59:08 +00001125 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001126 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001127 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001128 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001129 break;
1130 }
1131 case TK_ISNULL:
1132 case TK_NOTNULL: {
1133 int dest;
drh99fcd712001-10-13 01:06:47 +00001134 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001135 sqliteExprCode(pParse, pExpr->pLeft);
1136 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001137 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001138 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001139 break;
1140 }
drh22827922000-06-06 17:27:05 +00001141 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001142 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001143 break;
1144 }
drh4b59ab52002-08-24 18:24:51 +00001145 case TK_GLOB:
1146 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001147 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001148 int i;
1149 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001150 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001151 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001152 int nId;
1153 const char *zId;
1154 getFunctionName(pExpr, &zId, &nId);
1155 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001156 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001157 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001158 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001159 }
drh89425d52002-02-28 03:04:48 +00001160 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001161 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001162 break;
1163 }
drh19a775c2000-06-05 18:54:46 +00001164 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001165 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001166 break;
1167 }
drhfef52082000-06-06 01:50:43 +00001168 case TK_IN: {
1169 int addr;
drh99fcd712001-10-13 01:06:47 +00001170 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001171 sqliteExprCode(pParse, pExpr->pLeft);
1172 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001173 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1174 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1175 sqliteVdbeAddOp(v, OP_String, 0, 0);
1176 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001177 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001178 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001179 }else{
drhf5905aa2002-05-26 20:54:33 +00001180 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001181 }
drh99fcd712001-10-13 01:06:47 +00001182 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001183 break;
1184 }
1185 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001186 sqliteExprCode(pParse, pExpr->pLeft);
1187 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1188 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1189 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1190 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1191 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1192 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1193 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001194 break;
1195 }
drha2e00042002-01-22 03:13:42 +00001196 case TK_AS: {
1197 sqliteExprCode(pParse, pExpr->pLeft);
1198 break;
1199 }
drh17a7f8d2002-03-24 13:13:27 +00001200 case TK_CASE: {
1201 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001202 int jumpInst;
1203 int addr;
1204 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001205 int i;
1206
1207 assert(pExpr->pList);
1208 assert((pExpr->pList->nExpr % 2) == 0);
1209 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001210 nExpr = pExpr->pList->nExpr;
1211 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001212 if( pExpr->pLeft ){
1213 sqliteExprCode(pParse, pExpr->pLeft);
1214 }
drhf5905aa2002-05-26 20:54:33 +00001215 for(i=0; i<nExpr; i=i+2){
1216 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001217 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001218 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001219 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1220 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001221 }else{
drhf570f012002-05-31 15:51:25 +00001222 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001223 }
1224 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001225 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001226 addr = sqliteVdbeCurrentAddr(v);
1227 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001228 }
drhf570f012002-05-31 15:51:25 +00001229 if( pExpr->pLeft ){
1230 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1231 }
drh17a7f8d2002-03-24 13:13:27 +00001232 if( pExpr->pRight ){
1233 sqliteExprCode(pParse, pExpr->pRight);
1234 }else{
drhf5905aa2002-05-26 20:54:33 +00001235 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001236 }
drhf5905aa2002-05-26 20:54:33 +00001237 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001238 break;
1239 }
1240 case TK_RAISE: {
1241 if( !pParse->trigStack ){
drhda93d232003-03-31 02:12:46 +00001242 sqliteErrorMsg(pParse,
1243 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001244 pParse->nErr++;
1245 return;
1246 }
1247 if( pExpr->iColumn == OE_Rollback ||
1248 pExpr->iColumn == OE_Abort ||
1249 pExpr->iColumn == OE_Fail ){
1250 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1251 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1252 sqliteDequote(msg);
1253 sqliteVdbeChangeP3(v, -1, msg, 0);
1254 sqliteFree(msg);
1255 } else {
1256 assert( pExpr->iColumn == OE_Ignore );
1257 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drh483750b2003-01-29 18:46:51 +00001258 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001259 }
drh17a7f8d2002-03-24 13:13:27 +00001260 }
1261 break;
drhcce7d172000-05-31 15:34:51 +00001262 }
drhcce7d172000-05-31 15:34:51 +00001263}
1264
1265/*
1266** Generate code for a boolean expression such that a jump is made
1267** to the label "dest" if the expression is true but execution
1268** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001269**
1270** If the expression evaluates to NULL (neither true nor false), then
1271** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001272*/
drhf5905aa2002-05-26 20:54:33 +00001273void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001274 Vdbe *v = pParse->pVdbe;
1275 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001276 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001277 switch( pExpr->op ){
1278 case TK_LT: op = OP_Lt; break;
1279 case TK_LE: op = OP_Le; break;
1280 case TK_GT: op = OP_Gt; break;
1281 case TK_GE: op = OP_Ge; break;
1282 case TK_NE: op = OP_Ne; break;
1283 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001284 case TK_ISNULL: op = OP_IsNull; break;
1285 case TK_NOTNULL: op = OP_NotNull; break;
1286 default: break;
1287 }
1288 switch( pExpr->op ){
1289 case TK_AND: {
1290 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001291 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1292 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001293 sqliteVdbeResolveLabel(v, d2);
1294 break;
1295 }
1296 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001297 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1298 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001299 break;
1300 }
1301 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001302 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001303 break;
1304 }
1305 case TK_LT:
1306 case TK_LE:
1307 case TK_GT:
1308 case TK_GE:
1309 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001310 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001311 sqliteExprCode(pParse, pExpr->pLeft);
1312 sqliteExprCode(pParse, pExpr->pRight);
drh491791a2002-07-18 00:34:09 +00001313 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001314 op += 6; /* Convert numeric opcodes to text opcodes */
1315 }
drhf5905aa2002-05-26 20:54:33 +00001316 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001317 break;
1318 }
1319 case TK_ISNULL:
1320 case TK_NOTNULL: {
1321 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001322 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001323 break;
1324 }
drhfef52082000-06-06 01:50:43 +00001325 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001326 int addr;
drhcfab11b2000-06-06 03:31:22 +00001327 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001328 addr = sqliteVdbeCurrentAddr(v);
1329 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1330 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1331 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001332 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001333 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001334 }else{
drh99fcd712001-10-13 01:06:47 +00001335 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001336 }
1337 break;
1338 }
1339 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001340 int addr;
drhfef52082000-06-06 01:50:43 +00001341 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001342 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001343 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001344 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001345 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001346 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001347 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001348 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001349 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001350 break;
1351 }
drhcce7d172000-05-31 15:34:51 +00001352 default: {
1353 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001354 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001355 break;
1356 }
1357 }
1358}
1359
1360/*
drh66b89c82000-11-28 20:47:17 +00001361** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001362** to the label "dest" if the expression is false but execution
1363** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001364**
1365** If the expression evaluates to NULL (neither true nor false) then
1366** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001367*/
drhf5905aa2002-05-26 20:54:33 +00001368void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001369 Vdbe *v = pParse->pVdbe;
1370 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001371 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001372 switch( pExpr->op ){
1373 case TK_LT: op = OP_Ge; break;
1374 case TK_LE: op = OP_Gt; break;
1375 case TK_GT: op = OP_Le; break;
1376 case TK_GE: op = OP_Lt; break;
1377 case TK_NE: op = OP_Eq; break;
1378 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001379 case TK_ISNULL: op = OP_NotNull; break;
1380 case TK_NOTNULL: op = OP_IsNull; break;
1381 default: break;
1382 }
1383 switch( pExpr->op ){
1384 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001385 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1386 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001387 break;
1388 }
1389 case TK_OR: {
1390 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001391 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1392 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001393 sqliteVdbeResolveLabel(v, d2);
1394 break;
1395 }
1396 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001397 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001398 break;
1399 }
1400 case TK_LT:
1401 case TK_LE:
1402 case TK_GT:
1403 case TK_GE:
1404 case TK_NE:
1405 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001406 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drh8f619cc2002-09-08 00:04:50 +00001407 /* Convert numeric comparison opcodes into text comparison opcodes.
1408 ** This step depends on the fact that the text comparision opcodes are
1409 ** always 6 greater than their corresponding numeric comparison
1410 ** opcodes.
1411 */
1412 assert( OP_Eq+6 == OP_StrEq );
1413 op += 6;
drhc9b84a12002-06-20 11:36:48 +00001414 }
drhcce7d172000-05-31 15:34:51 +00001415 sqliteExprCode(pParse, pExpr->pLeft);
1416 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001417 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001418 break;
1419 }
drhcce7d172000-05-31 15:34:51 +00001420 case TK_ISNULL:
1421 case TK_NOTNULL: {
1422 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001423 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001424 break;
1425 }
drhfef52082000-06-06 01:50:43 +00001426 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001427 int addr;
drhcfab11b2000-06-06 03:31:22 +00001428 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001429 addr = sqliteVdbeCurrentAddr(v);
1430 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1431 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1432 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001433 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001434 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001435 }else{
drh99fcd712001-10-13 01:06:47 +00001436 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001437 }
1438 break;
1439 }
1440 case TK_BETWEEN: {
1441 int addr;
1442 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001443 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001444 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1445 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001446 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001447 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1448 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001449 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001450 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001451 break;
1452 }
drhcce7d172000-05-31 15:34:51 +00001453 default: {
1454 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001455 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001456 break;
1457 }
1458 }
1459}
drh22827922000-06-06 17:27:05 +00001460
1461/*
1462** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1463** if they are identical and return FALSE if they differ in any way.
1464*/
drhd8bc7082000-06-07 23:51:50 +00001465int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001466 int i;
1467 if( pA==0 ){
1468 return pB==0;
1469 }else if( pB==0 ){
1470 return 0;
1471 }
1472 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001473 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1474 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001475 if( pA->pList ){
1476 if( pB->pList==0 ) return 0;
1477 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1478 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001479 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001480 return 0;
1481 }
1482 }
1483 }else if( pB->pList ){
1484 return 0;
1485 }
1486 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001487 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001488 if( pA->token.z ){
1489 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001490 if( pB->token.n!=pA->token.n ) return 0;
1491 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001492 }
1493 return 1;
1494}
1495
1496/*
1497** Add a new element to the pParse->aAgg[] array and return its index.
1498*/
1499static int appendAggInfo(Parse *pParse){
1500 if( (pParse->nAgg & 0x7)==0 ){
1501 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001502 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1503 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001504 return -1;
1505 }
drh6d4abfb2001-10-22 02:58:08 +00001506 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001507 }
1508 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1509 return pParse->nAgg++;
1510}
1511
1512/*
1513** Analyze the given expression looking for aggregate functions and
1514** for variables that need to be added to the pParse->aAgg[] array.
1515** Make additional entries to the pParse->aAgg[] array as necessary.
1516**
1517** This routine should only be called after the expression has been
1518** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1519**
1520** If errors are seen, leave an error message in zErrMsg and return
1521** the number of errors.
1522*/
1523int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1524 int i;
1525 AggExpr *aAgg;
1526 int nErr = 0;
1527
1528 if( pExpr==0 ) return 0;
1529 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001530 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001531 aAgg = pParse->aAgg;
1532 for(i=0; i<pParse->nAgg; i++){
1533 if( aAgg[i].isAgg ) continue;
1534 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001535 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001536 break;
1537 }
1538 }
1539 if( i>=pParse->nAgg ){
1540 i = appendAggInfo(pParse);
1541 if( i<0 ) return 1;
1542 pParse->aAgg[i].isAgg = 0;
1543 pParse->aAgg[i].pExpr = pExpr;
1544 }
drhaaf88722000-06-08 11:25:00 +00001545 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001546 break;
1547 }
1548 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001549 aAgg = pParse->aAgg;
1550 for(i=0; i<pParse->nAgg; i++){
1551 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001552 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001553 break;
1554 }
1555 }
1556 if( i>=pParse->nAgg ){
1557 i = appendAggInfo(pParse);
1558 if( i<0 ) return 1;
1559 pParse->aAgg[i].isAgg = 1;
1560 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001561 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001562 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001563 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001564 }
1565 pExpr->iAgg = i;
1566 break;
1567 }
1568 default: {
1569 if( pExpr->pLeft ){
1570 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1571 }
1572 if( nErr==0 && pExpr->pRight ){
1573 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1574 }
1575 if( nErr==0 && pExpr->pList ){
1576 int n = pExpr->pList->nExpr;
1577 int i;
1578 for(i=0; nErr==0 && i<n; i++){
1579 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1580 }
1581 }
1582 break;
1583 }
1584 }
1585 return nErr;
1586}
drh8e0a2f92002-02-23 23:45:45 +00001587
1588/*
1589** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001590** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001591** function, or return NULL if the function does not exist.
1592**
drh0bce8352002-02-28 00:41:10 +00001593** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001594** structure is created and liked into the "db" structure if a
1595** no matching function previously existed. When createFlag is true
1596** and the nArg parameter is -1, then only a function that accepts
1597** any number of arguments will be returned.
1598**
1599** If createFlag is false and nArg is -1, then the first valid
1600** function found is returned. A function is valid if either xFunc
1601** or xStep is non-zero.
1602*/
drh0bce8352002-02-28 00:41:10 +00001603FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001604 sqlite *db, /* An open database */
1605 const char *zName, /* Name of the function. Not null-terminated */
1606 int nName, /* Number of characters in the name */
1607 int nArg, /* Number of arguments. -1 means any number */
1608 int createFlag /* Create new entry if true and does not otherwise exist */
1609){
drh0bce8352002-02-28 00:41:10 +00001610 FuncDef *pFirst, *p, *pMaybe;
1611 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001612 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001613 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1614 return p;
1615 }
1616 pMaybe = 0;
1617 while( p && p->nArg!=nArg ){
1618 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1619 p = p->pNext;
1620 }
1621 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1622 return 0;
1623 }
1624 if( p==0 && pMaybe ){
1625 assert( createFlag==0 );
1626 return pMaybe;
1627 }
drh89425d52002-02-28 03:04:48 +00001628 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001629 p->nArg = nArg;
1630 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001631 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001632 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001633 }
1634 return p;
1635}