1*4520Snw141292
2*4520Snw141292 #pragma ident "%Z%%M% %I% %E% SMI"
3*4520Snw141292
4*4520Snw141292 /*
5*4520Snw141292 ** 2001 September 15
6*4520Snw141292 **
7*4520Snw141292 ** The author disclaims copyright to this source code. In place of
8*4520Snw141292 ** a legal notice, here is a blessing:
9*4520Snw141292 **
10*4520Snw141292 ** May you do good and not evil.
11*4520Snw141292 ** May you find forgiveness for yourself and forgive others.
12*4520Snw141292 ** May you share freely, never taking more than you give.
13*4520Snw141292 **
14*4520Snw141292 *************************************************************************
15*4520Snw141292 ** This file contains routines used for analyzing expressions and
16*4520Snw141292 ** for generating VDBE code that evaluates expressions in SQLite.
17*4520Snw141292 **
18*4520Snw141292 ** $Id: expr.c,v 1.114.2.3 2004/07/22 17:10:10 drh Exp $
19*4520Snw141292 */
20*4520Snw141292 #include "sqliteInt.h"
21*4520Snw141292 #include <ctype.h>
22*4520Snw141292
23*4520Snw141292 /*
24*4520Snw141292 ** Construct a new expression node and return a pointer to it. Memory
25*4520Snw141292 ** for this node is obtained from sqliteMalloc(). The calling function
26*4520Snw141292 ** is responsible for making sure the node eventually gets freed.
27*4520Snw141292 */
sqliteExpr(int op,Expr * pLeft,Expr * pRight,Token * pToken)28*4520Snw141292 Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
29*4520Snw141292 Expr *pNew;
30*4520Snw141292 pNew = sqliteMalloc( sizeof(Expr) );
31*4520Snw141292 if( pNew==0 ){
32*4520Snw141292 /* When malloc fails, we leak memory from pLeft and pRight */
33*4520Snw141292 return 0;
34*4520Snw141292 }
35*4520Snw141292 pNew->op = op;
36*4520Snw141292 pNew->pLeft = pLeft;
37*4520Snw141292 pNew->pRight = pRight;
38*4520Snw141292 if( pToken ){
39*4520Snw141292 assert( pToken->dyn==0 );
40*4520Snw141292 pNew->token = *pToken;
41*4520Snw141292 pNew->span = *pToken;
42*4520Snw141292 }else{
43*4520Snw141292 assert( pNew->token.dyn==0 );
44*4520Snw141292 assert( pNew->token.z==0 );
45*4520Snw141292 assert( pNew->token.n==0 );
46*4520Snw141292 if( pLeft && pRight ){
47*4520Snw141292 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
48*4520Snw141292 }else{
49*4520Snw141292 pNew->span = pNew->token;
50*4520Snw141292 }
51*4520Snw141292 }
52*4520Snw141292 return pNew;
53*4520Snw141292 }
54*4520Snw141292
55*4520Snw141292 /*
56*4520Snw141292 ** Set the Expr.span field of the given expression to span all
57*4520Snw141292 ** text between the two given tokens.
58*4520Snw141292 */
sqliteExprSpan(Expr * pExpr,Token * pLeft,Token * pRight)59*4520Snw141292 void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
60*4520Snw141292 assert( pRight!=0 );
61*4520Snw141292 assert( pLeft!=0 );
62*4520Snw141292 /* Note: pExpr might be NULL due to a prior malloc failure */
63*4520Snw141292 if( pExpr && pRight->z && pLeft->z ){
64*4520Snw141292 if( pLeft->dyn==0 && pRight->dyn==0 ){
65*4520Snw141292 pExpr->span.z = pLeft->z;
66*4520Snw141292 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
67*4520Snw141292 }else{
68*4520Snw141292 pExpr->span.z = 0;
69*4520Snw141292 }
70*4520Snw141292 }
71*4520Snw141292 }
72*4520Snw141292
73*4520Snw141292 /*
74*4520Snw141292 ** Construct a new expression node for a function with multiple
75*4520Snw141292 ** arguments.
76*4520Snw141292 */
sqliteExprFunction(ExprList * pList,Token * pToken)77*4520Snw141292 Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
78*4520Snw141292 Expr *pNew;
79*4520Snw141292 pNew = sqliteMalloc( sizeof(Expr) );
80*4520Snw141292 if( pNew==0 ){
81*4520Snw141292 /* sqliteExprListDelete(pList); // Leak pList when malloc fails */
82*4520Snw141292 return 0;
83*4520Snw141292 }
84*4520Snw141292 pNew->op = TK_FUNCTION;
85*4520Snw141292 pNew->pList = pList;
86*4520Snw141292 if( pToken ){
87*4520Snw141292 assert( pToken->dyn==0 );
88*4520Snw141292 pNew->token = *pToken;
89*4520Snw141292 }else{
90*4520Snw141292 pNew->token.z = 0;
91*4520Snw141292 }
92*4520Snw141292 pNew->span = pNew->token;
93*4520Snw141292 return pNew;
94*4520Snw141292 }
95*4520Snw141292
96*4520Snw141292 /*
97*4520Snw141292 ** Recursively delete an expression tree.
98*4520Snw141292 */
sqliteExprDelete(Expr * p)99*4520Snw141292 void sqliteExprDelete(Expr *p){
100*4520Snw141292 if( p==0 ) return;
101*4520Snw141292 if( p->span.dyn ) sqliteFree((char*)p->span.z);
102*4520Snw141292 if( p->token.dyn ) sqliteFree((char*)p->token.z);
103*4520Snw141292 sqliteExprDelete(p->pLeft);
104*4520Snw141292 sqliteExprDelete(p->pRight);
105*4520Snw141292 sqliteExprListDelete(p->pList);
106*4520Snw141292 sqliteSelectDelete(p->pSelect);
107*4520Snw141292 sqliteFree(p);
108*4520Snw141292 }
109*4520Snw141292
110*4520Snw141292
111*4520Snw141292 /*
112*4520Snw141292 ** The following group of routines make deep copies of expressions,
113*4520Snw141292 ** expression lists, ID lists, and select statements. The copies can
114*4520Snw141292 ** be deleted (by being passed to their respective ...Delete() routines)
115*4520Snw141292 ** without effecting the originals.
116*4520Snw141292 **
117*4520Snw141292 ** The expression list, ID, and source lists return by sqliteExprListDup(),
118*4520Snw141292 ** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
119*4520Snw141292 ** by subsequent calls to sqlite*ListAppend() routines.
120*4520Snw141292 **
121*4520Snw141292 ** Any tables that the SrcList might point to are not duplicated.
122*4520Snw141292 */
sqliteExprDup(Expr * p)123*4520Snw141292 Expr *sqliteExprDup(Expr *p){
124*4520Snw141292 Expr *pNew;
125*4520Snw141292 if( p==0 ) return 0;
126*4520Snw141292 pNew = sqliteMallocRaw( sizeof(*p) );
127*4520Snw141292 if( pNew==0 ) return 0;
128*4520Snw141292 memcpy(pNew, p, sizeof(*pNew));
129*4520Snw141292 if( p->token.z!=0 ){
130*4520Snw141292 pNew->token.z = sqliteStrDup(p->token.z);
131*4520Snw141292 pNew->token.dyn = 1;
132*4520Snw141292 }else{
133*4520Snw141292 assert( pNew->token.z==0 );
134*4520Snw141292 }
135*4520Snw141292 pNew->span.z = 0;
136*4520Snw141292 pNew->pLeft = sqliteExprDup(p->pLeft);
137*4520Snw141292 pNew->pRight = sqliteExprDup(p->pRight);
138*4520Snw141292 pNew->pList = sqliteExprListDup(p->pList);
139*4520Snw141292 pNew->pSelect = sqliteSelectDup(p->pSelect);
140*4520Snw141292 return pNew;
141*4520Snw141292 }
sqliteTokenCopy(Token * pTo,Token * pFrom)142*4520Snw141292 void sqliteTokenCopy(Token *pTo, Token *pFrom){
143*4520Snw141292 if( pTo->dyn ) sqliteFree((char*)pTo->z);
144*4520Snw141292 if( pFrom->z ){
145*4520Snw141292 pTo->n = pFrom->n;
146*4520Snw141292 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
147*4520Snw141292 pTo->dyn = 1;
148*4520Snw141292 }else{
149*4520Snw141292 pTo->z = 0;
150*4520Snw141292 }
151*4520Snw141292 }
sqliteExprListDup(ExprList * p)152*4520Snw141292 ExprList *sqliteExprListDup(ExprList *p){
153*4520Snw141292 ExprList *pNew;
154*4520Snw141292 struct ExprList_item *pItem;
155*4520Snw141292 int i;
156*4520Snw141292 if( p==0 ) return 0;
157*4520Snw141292 pNew = sqliteMalloc( sizeof(*pNew) );
158*4520Snw141292 if( pNew==0 ) return 0;
159*4520Snw141292 pNew->nExpr = pNew->nAlloc = p->nExpr;
160*4520Snw141292 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
161*4520Snw141292 if( pItem==0 ){
162*4520Snw141292 sqliteFree(pNew);
163*4520Snw141292 return 0;
164*4520Snw141292 }
165*4520Snw141292 for(i=0; i<p->nExpr; i++, pItem++){
166*4520Snw141292 Expr *pNewExpr, *pOldExpr;
167*4520Snw141292 pItem->pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr);
168*4520Snw141292 if( pOldExpr->span.z!=0 && pNewExpr ){
169*4520Snw141292 /* Always make a copy of the span for top-level expressions in the
170*4520Snw141292 ** expression list. The logic in SELECT processing that determines
171*4520Snw141292 ** the names of columns in the result set needs this information */
172*4520Snw141292 sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span);
173*4520Snw141292 }
174*4520Snw141292 assert( pNewExpr==0 || pNewExpr->span.z!=0
175*4520Snw141292 || pOldExpr->span.z==0 || sqlite_malloc_failed );
176*4520Snw141292 pItem->zName = sqliteStrDup(p->a[i].zName);
177*4520Snw141292 pItem->sortOrder = p->a[i].sortOrder;
178*4520Snw141292 pItem->isAgg = p->a[i].isAgg;
179*4520Snw141292 pItem->done = 0;
180*4520Snw141292 }
181*4520Snw141292 return pNew;
182*4520Snw141292 }
sqliteSrcListDup(SrcList * p)183*4520Snw141292 SrcList *sqliteSrcListDup(SrcList *p){
184*4520Snw141292 SrcList *pNew;
185*4520Snw141292 int i;
186*4520Snw141292 int nByte;
187*4520Snw141292 if( p==0 ) return 0;
188*4520Snw141292 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
189*4520Snw141292 pNew = sqliteMallocRaw( nByte );
190*4520Snw141292 if( pNew==0 ) return 0;
191*4520Snw141292 pNew->nSrc = pNew->nAlloc = p->nSrc;
192*4520Snw141292 for(i=0; i<p->nSrc; i++){
193*4520Snw141292 struct SrcList_item *pNewItem = &pNew->a[i];
194*4520Snw141292 struct SrcList_item *pOldItem = &p->a[i];
195*4520Snw141292 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
196*4520Snw141292 pNewItem->zName = sqliteStrDup(pOldItem->zName);
197*4520Snw141292 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
198*4520Snw141292 pNewItem->jointype = pOldItem->jointype;
199*4520Snw141292 pNewItem->iCursor = pOldItem->iCursor;
200*4520Snw141292 pNewItem->pTab = 0;
201*4520Snw141292 pNewItem->pSelect = sqliteSelectDup(pOldItem->pSelect);
202*4520Snw141292 pNewItem->pOn = sqliteExprDup(pOldItem->pOn);
203*4520Snw141292 pNewItem->pUsing = sqliteIdListDup(pOldItem->pUsing);
204*4520Snw141292 }
205*4520Snw141292 return pNew;
206*4520Snw141292 }
sqliteIdListDup(IdList * p)207*4520Snw141292 IdList *sqliteIdListDup(IdList *p){
208*4520Snw141292 IdList *pNew;
209*4520Snw141292 int i;
210*4520Snw141292 if( p==0 ) return 0;
211*4520Snw141292 pNew = sqliteMallocRaw( sizeof(*pNew) );
212*4520Snw141292 if( pNew==0 ) return 0;
213*4520Snw141292 pNew->nId = pNew->nAlloc = p->nId;
214*4520Snw141292 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
215*4520Snw141292 if( pNew->a==0 ) return 0;
216*4520Snw141292 for(i=0; i<p->nId; i++){
217*4520Snw141292 struct IdList_item *pNewItem = &pNew->a[i];
218*4520Snw141292 struct IdList_item *pOldItem = &p->a[i];
219*4520Snw141292 pNewItem->zName = sqliteStrDup(pOldItem->zName);
220*4520Snw141292 pNewItem->idx = pOldItem->idx;
221*4520Snw141292 }
222*4520Snw141292 return pNew;
223*4520Snw141292 }
sqliteSelectDup(Select * p)224*4520Snw141292 Select *sqliteSelectDup(Select *p){
225*4520Snw141292 Select *pNew;
226*4520Snw141292 if( p==0 ) return 0;
227*4520Snw141292 pNew = sqliteMallocRaw( sizeof(*p) );
228*4520Snw141292 if( pNew==0 ) return 0;
229*4520Snw141292 pNew->isDistinct = p->isDistinct;
230*4520Snw141292 pNew->pEList = sqliteExprListDup(p->pEList);
231*4520Snw141292 pNew->pSrc = sqliteSrcListDup(p->pSrc);
232*4520Snw141292 pNew->pWhere = sqliteExprDup(p->pWhere);
233*4520Snw141292 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
234*4520Snw141292 pNew->pHaving = sqliteExprDup(p->pHaving);
235*4520Snw141292 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
236*4520Snw141292 pNew->op = p->op;
237*4520Snw141292 pNew->pPrior = sqliteSelectDup(p->pPrior);
238*4520Snw141292 pNew->nLimit = p->nLimit;
239*4520Snw141292 pNew->nOffset = p->nOffset;
240*4520Snw141292 pNew->zSelect = 0;
241*4520Snw141292 pNew->iLimit = -1;
242*4520Snw141292 pNew->iOffset = -1;
243*4520Snw141292 return pNew;
244*4520Snw141292 }
245*4520Snw141292
246*4520Snw141292
247*4520Snw141292 /*
248*4520Snw141292 ** Add a new element to the end of an expression list. If pList is
249*4520Snw141292 ** initially NULL, then create a new expression list.
250*4520Snw141292 */
sqliteExprListAppend(ExprList * pList,Expr * pExpr,Token * pName)251*4520Snw141292 ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
252*4520Snw141292 if( pList==0 ){
253*4520Snw141292 pList = sqliteMalloc( sizeof(ExprList) );
254*4520Snw141292 if( pList==0 ){
255*4520Snw141292 /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
256*4520Snw141292 return 0;
257*4520Snw141292 }
258*4520Snw141292 assert( pList->nAlloc==0 );
259*4520Snw141292 }
260*4520Snw141292 if( pList->nAlloc<=pList->nExpr ){
261*4520Snw141292 pList->nAlloc = pList->nAlloc*2 + 4;
262*4520Snw141292 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
263*4520Snw141292 if( pList->a==0 ){
264*4520Snw141292 /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
265*4520Snw141292 pList->nExpr = pList->nAlloc = 0;
266*4520Snw141292 return pList;
267*4520Snw141292 }
268*4520Snw141292 }
269*4520Snw141292 assert( pList->a!=0 );
270*4520Snw141292 if( pExpr || pName ){
271*4520Snw141292 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
272*4520Snw141292 memset(pItem, 0, sizeof(*pItem));
273*4520Snw141292 pItem->pExpr = pExpr;
274*4520Snw141292 if( pName ){
275*4520Snw141292 sqliteSetNString(&pItem->zName, pName->z, pName->n, 0);
276*4520Snw141292 sqliteDequote(pItem->zName);
277*4520Snw141292 }
278*4520Snw141292 }
279*4520Snw141292 return pList;
280*4520Snw141292 }
281*4520Snw141292
282*4520Snw141292 /*
283*4520Snw141292 ** Delete an entire expression list.
284*4520Snw141292 */
sqliteExprListDelete(ExprList * pList)285*4520Snw141292 void sqliteExprListDelete(ExprList *pList){
286*4520Snw141292 int i;
287*4520Snw141292 if( pList==0 ) return;
288*4520Snw141292 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
289*4520Snw141292 assert( pList->nExpr<=pList->nAlloc );
290*4520Snw141292 for(i=0; i<pList->nExpr; i++){
291*4520Snw141292 sqliteExprDelete(pList->a[i].pExpr);
292*4520Snw141292 sqliteFree(pList->a[i].zName);
293*4520Snw141292 }
294*4520Snw141292 sqliteFree(pList->a);
295*4520Snw141292 sqliteFree(pList);
296*4520Snw141292 }
297*4520Snw141292
298*4520Snw141292 /*
299*4520Snw141292 ** Walk an expression tree. Return 1 if the expression is constant
300*4520Snw141292 ** and 0 if it involves variables.
301*4520Snw141292 **
302*4520Snw141292 ** For the purposes of this function, a double-quoted string (ex: "abc")
303*4520Snw141292 ** is considered a variable but a single-quoted string (ex: 'abc') is
304*4520Snw141292 ** a constant.
305*4520Snw141292 */
sqliteExprIsConstant(Expr * p)306*4520Snw141292 int sqliteExprIsConstant(Expr *p){
307*4520Snw141292 switch( p->op ){
308*4520Snw141292 case TK_ID:
309*4520Snw141292 case TK_COLUMN:
310*4520Snw141292 case TK_DOT:
311*4520Snw141292 case TK_FUNCTION:
312*4520Snw141292 return 0;
313*4520Snw141292 case TK_NULL:
314*4520Snw141292 case TK_STRING:
315*4520Snw141292 case TK_INTEGER:
316*4520Snw141292 case TK_FLOAT:
317*4520Snw141292 case TK_VARIABLE:
318*4520Snw141292 return 1;
319*4520Snw141292 default: {
320*4520Snw141292 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
321*4520Snw141292 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
322*4520Snw141292 if( p->pList ){
323*4520Snw141292 int i;
324*4520Snw141292 for(i=0; i<p->pList->nExpr; i++){
325*4520Snw141292 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
326*4520Snw141292 }
327*4520Snw141292 }
328*4520Snw141292 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
329*4520Snw141292 }
330*4520Snw141292 }
331*4520Snw141292 return 0;
332*4520Snw141292 }
333*4520Snw141292
334*4520Snw141292 /*
335*4520Snw141292 ** If the given expression codes a constant integer that is small enough
336*4520Snw141292 ** to fit in a 32-bit integer, return 1 and put the value of the integer
337*4520Snw141292 ** in *pValue. If the expression is not an integer or if it is too big
338*4520Snw141292 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
339*4520Snw141292 */
sqliteExprIsInteger(Expr * p,int * pValue)340*4520Snw141292 int sqliteExprIsInteger(Expr *p, int *pValue){
341*4520Snw141292 switch( p->op ){
342*4520Snw141292 case TK_INTEGER: {
343*4520Snw141292 if( sqliteFitsIn32Bits(p->token.z) ){
344*4520Snw141292 *pValue = atoi(p->token.z);
345*4520Snw141292 return 1;
346*4520Snw141292 }
347*4520Snw141292 break;
348*4520Snw141292 }
349*4520Snw141292 case TK_STRING: {
350*4520Snw141292 const char *z = p->token.z;
351*4520Snw141292 int n = p->token.n;
352*4520Snw141292 if( n>0 && z[0]=='-' ){ z++; n--; }
353*4520Snw141292 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
354*4520Snw141292 if( n==0 && sqliteFitsIn32Bits(p->token.z) ){
355*4520Snw141292 *pValue = atoi(p->token.z);
356*4520Snw141292 return 1;
357*4520Snw141292 }
358*4520Snw141292 break;
359*4520Snw141292 }
360*4520Snw141292 case TK_UPLUS: {
361*4520Snw141292 return sqliteExprIsInteger(p->pLeft, pValue);
362*4520Snw141292 }
363*4520Snw141292 case TK_UMINUS: {
364*4520Snw141292 int v;
365*4520Snw141292 if( sqliteExprIsInteger(p->pLeft, &v) ){
366*4520Snw141292 *pValue = -v;
367*4520Snw141292 return 1;
368*4520Snw141292 }
369*4520Snw141292 break;
370*4520Snw141292 }
371*4520Snw141292 default: break;
372*4520Snw141292 }
373*4520Snw141292 return 0;
374*4520Snw141292 }
375*4520Snw141292
376*4520Snw141292 /*
377*4520Snw141292 ** Return TRUE if the given string is a row-id column name.
378*4520Snw141292 */
sqliteIsRowid(const char * z)379*4520Snw141292 int sqliteIsRowid(const char *z){
380*4520Snw141292 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
381*4520Snw141292 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
382*4520Snw141292 if( sqliteStrICmp(z, "OID")==0 ) return 1;
383*4520Snw141292 return 0;
384*4520Snw141292 }
385*4520Snw141292
386*4520Snw141292 /*
387*4520Snw141292 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
388*4520Snw141292 ** that name in the set of source tables in pSrcList and make the pExpr
389*4520Snw141292 ** expression node refer back to that source column. The following changes
390*4520Snw141292 ** are made to pExpr:
391*4520Snw141292 **
392*4520Snw141292 ** pExpr->iDb Set the index in db->aDb[] of the database holding
393*4520Snw141292 ** the table.
394*4520Snw141292 ** pExpr->iTable Set to the cursor number for the table obtained
395*4520Snw141292 ** from pSrcList.
396*4520Snw141292 ** pExpr->iColumn Set to the column number within the table.
397*4520Snw141292 ** pExpr->dataType Set to the appropriate data type for the column.
398*4520Snw141292 ** pExpr->op Set to TK_COLUMN.
399*4520Snw141292 ** pExpr->pLeft Any expression this points to is deleted
400*4520Snw141292 ** pExpr->pRight Any expression this points to is deleted.
401*4520Snw141292 **
402*4520Snw141292 ** The pDbToken is the name of the database (the "X"). This value may be
403*4520Snw141292 ** NULL meaning that name is of the form Y.Z or Z. Any available database
404*4520Snw141292 ** can be used. The pTableToken is the name of the table (the "Y"). This
405*4520Snw141292 ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
406*4520Snw141292 ** means that the form of the name is Z and that columns from any table
407*4520Snw141292 ** can be used.
408*4520Snw141292 **
409*4520Snw141292 ** If the name cannot be resolved unambiguously, leave an error message
410*4520Snw141292 ** in pParse and return non-zero. Return zero on success.
411*4520Snw141292 */
lookupName(Parse * pParse,Token * pDbToken,Token * pTableToken,Token * pColumnToken,SrcList * pSrcList,ExprList * pEList,Expr * pExpr)412*4520Snw141292 static int lookupName(
413*4520Snw141292 Parse *pParse, /* The parsing context */
414*4520Snw141292 Token *pDbToken, /* Name of the database containing table, or NULL */
415*4520Snw141292 Token *pTableToken, /* Name of table containing column, or NULL */
416*4520Snw141292 Token *pColumnToken, /* Name of the column. */
417*4520Snw141292 SrcList *pSrcList, /* List of tables used to resolve column names */
418*4520Snw141292 ExprList *pEList, /* List of expressions used to resolve "AS" */
419*4520Snw141292 Expr *pExpr /* Make this EXPR node point to the selected column */
420*4520Snw141292 ){
421*4520Snw141292 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
422*4520Snw141292 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
423*4520Snw141292 char *zCol = 0; /* Name of the column. The "Z" */
424*4520Snw141292 int i, j; /* Loop counters */
425*4520Snw141292 int cnt = 0; /* Number of matching column names */
426*4520Snw141292 int cntTab = 0; /* Number of matching table names */
427*4520Snw141292 sqlite *db = pParse->db; /* The database */
428*4520Snw141292
429*4520Snw141292 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
430*4520Snw141292 if( pDbToken && pDbToken->z ){
431*4520Snw141292 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
432*4520Snw141292 sqliteDequote(zDb);
433*4520Snw141292 }else{
434*4520Snw141292 zDb = 0;
435*4520Snw141292 }
436*4520Snw141292 if( pTableToken && pTableToken->z ){
437*4520Snw141292 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
438*4520Snw141292 sqliteDequote(zTab);
439*4520Snw141292 }else{
440*4520Snw141292 assert( zDb==0 );
441*4520Snw141292 zTab = 0;
442*4520Snw141292 }
443*4520Snw141292 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
444*4520Snw141292 sqliteDequote(zCol);
445*4520Snw141292 if( sqlite_malloc_failed ){
446*4520Snw141292 return 1; /* Leak memory (zDb and zTab) if malloc fails */
447*4520Snw141292 }
448*4520Snw141292 assert( zTab==0 || pEList==0 );
449*4520Snw141292
450*4520Snw141292 pExpr->iTable = -1;
451*4520Snw141292 for(i=0; i<pSrcList->nSrc; i++){
452*4520Snw141292 struct SrcList_item *pItem = &pSrcList->a[i];
453*4520Snw141292 Table *pTab = pItem->pTab;
454*4520Snw141292 Column *pCol;
455*4520Snw141292
456*4520Snw141292 if( pTab==0 ) continue;
457*4520Snw141292 assert( pTab->nCol>0 );
458*4520Snw141292 if( zTab ){
459*4520Snw141292 if( pItem->zAlias ){
460*4520Snw141292 char *zTabName = pItem->zAlias;
461*4520Snw141292 if( sqliteStrICmp(zTabName, zTab)!=0 ) continue;
462*4520Snw141292 }else{
463*4520Snw141292 char *zTabName = pTab->zName;
464*4520Snw141292 if( zTabName==0 || sqliteStrICmp(zTabName, zTab)!=0 ) continue;
465*4520Snw141292 if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
466*4520Snw141292 continue;
467*4520Snw141292 }
468*4520Snw141292 }
469*4520Snw141292 }
470*4520Snw141292 if( 0==(cntTab++) ){
471*4520Snw141292 pExpr->iTable = pItem->iCursor;
472*4520Snw141292 pExpr->iDb = pTab->iDb;
473*4520Snw141292 }
474*4520Snw141292 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
475*4520Snw141292 if( sqliteStrICmp(pCol->zName, zCol)==0 ){
476*4520Snw141292 cnt++;
477*4520Snw141292 pExpr->iTable = pItem->iCursor;
478*4520Snw141292 pExpr->iDb = pTab->iDb;
479*4520Snw141292 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
480*4520Snw141292 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
481*4520Snw141292 pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
482*4520Snw141292 break;
483*4520Snw141292 }
484*4520Snw141292 }
485*4520Snw141292 }
486*4520Snw141292
487*4520Snw141292 /* If we have not already resolved the name, then maybe
488*4520Snw141292 ** it is a new.* or old.* trigger argument reference
489*4520Snw141292 */
490*4520Snw141292 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
491*4520Snw141292 TriggerStack *pTriggerStack = pParse->trigStack;
492*4520Snw141292 Table *pTab = 0;
493*4520Snw141292 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zTab) == 0 ){
494*4520Snw141292 pExpr->iTable = pTriggerStack->newIdx;
495*4520Snw141292 assert( pTriggerStack->pTab );
496*4520Snw141292 pTab = pTriggerStack->pTab;
497*4520Snw141292 }else if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zTab) == 0 ){
498*4520Snw141292 pExpr->iTable = pTriggerStack->oldIdx;
499*4520Snw141292 assert( pTriggerStack->pTab );
500*4520Snw141292 pTab = pTriggerStack->pTab;
501*4520Snw141292 }
502*4520Snw141292
503*4520Snw141292 if( pTab ){
504*4520Snw141292 int j;
505*4520Snw141292 Column *pCol = pTab->aCol;
506*4520Snw141292
507*4520Snw141292 pExpr->iDb = pTab->iDb;
508*4520Snw141292 cntTab++;
509*4520Snw141292 for(j=0; j < pTab->nCol; j++, pCol++) {
510*4520Snw141292 if( sqliteStrICmp(pCol->zName, zCol)==0 ){
511*4520Snw141292 cnt++;
512*4520Snw141292 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
513*4520Snw141292 pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
514*4520Snw141292 break;
515*4520Snw141292 }
516*4520Snw141292 }
517*4520Snw141292 }
518*4520Snw141292 }
519*4520Snw141292
520*4520Snw141292 /*
521*4520Snw141292 ** Perhaps the name is a reference to the ROWID
522*4520Snw141292 */
523*4520Snw141292 if( cnt==0 && cntTab==1 && sqliteIsRowid(zCol) ){
524*4520Snw141292 cnt = 1;
525*4520Snw141292 pExpr->iColumn = -1;
526*4520Snw141292 pExpr->dataType = SQLITE_SO_NUM;
527*4520Snw141292 }
528*4520Snw141292
529*4520Snw141292 /*
530*4520Snw141292 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
531*4520Snw141292 ** might refer to an result-set alias. This happens, for example, when
532*4520Snw141292 ** we are resolving names in the WHERE clause of the following command:
533*4520Snw141292 **
534*4520Snw141292 ** SELECT a+b AS x FROM table WHERE x<10;
535*4520Snw141292 **
536*4520Snw141292 ** In cases like this, replace pExpr with a copy of the expression that
537*4520Snw141292 ** forms the result set entry ("a+b" in the example) and return immediately.
538*4520Snw141292 ** Note that the expression in the result set should have already been
539*4520Snw141292 ** resolved by the time the WHERE clause is resolved.
540*4520Snw141292 */
541*4520Snw141292 if( cnt==0 && pEList!=0 ){
542*4520Snw141292 for(j=0; j<pEList->nExpr; j++){
543*4520Snw141292 char *zAs = pEList->a[j].zName;
544*4520Snw141292 if( zAs!=0 && sqliteStrICmp(zAs, zCol)==0 ){
545*4520Snw141292 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
546*4520Snw141292 pExpr->op = TK_AS;
547*4520Snw141292 pExpr->iColumn = j;
548*4520Snw141292 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
549*4520Snw141292 sqliteFree(zCol);
550*4520Snw141292 assert( zTab==0 && zDb==0 );
551*4520Snw141292 return 0;
552*4520Snw141292 }
553*4520Snw141292 }
554*4520Snw141292 }
555*4520Snw141292
556*4520Snw141292 /*
557*4520Snw141292 ** If X and Y are NULL (in other words if only the column name Z is
558*4520Snw141292 ** supplied) and the value of Z is enclosed in double-quotes, then
559*4520Snw141292 ** Z is a string literal if it doesn't match any column names. In that
560*4520Snw141292 ** case, we need to return right away and not make any changes to
561*4520Snw141292 ** pExpr.
562*4520Snw141292 */
563*4520Snw141292 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
564*4520Snw141292 sqliteFree(zCol);
565*4520Snw141292 return 0;
566*4520Snw141292 }
567*4520Snw141292
568*4520Snw141292 /*
569*4520Snw141292 ** cnt==0 means there was not match. cnt>1 means there were two or
570*4520Snw141292 ** more matches. Either way, we have an error.
571*4520Snw141292 */
572*4520Snw141292 if( cnt!=1 ){
573*4520Snw141292 char *z = 0;
574*4520Snw141292 char *zErr;
575*4520Snw141292 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
576*4520Snw141292 if( zDb ){
577*4520Snw141292 sqliteSetString(&z, zDb, ".", zTab, ".", zCol, 0);
578*4520Snw141292 }else if( zTab ){
579*4520Snw141292 sqliteSetString(&z, zTab, ".", zCol, 0);
580*4520Snw141292 }else{
581*4520Snw141292 z = sqliteStrDup(zCol);
582*4520Snw141292 }
583*4520Snw141292 sqliteErrorMsg(pParse, zErr, z);
584*4520Snw141292 sqliteFree(z);
585*4520Snw141292 }
586*4520Snw141292
587*4520Snw141292 /* Clean up and return
588*4520Snw141292 */
589*4520Snw141292 sqliteFree(zDb);
590*4520Snw141292 sqliteFree(zTab);
591*4520Snw141292 sqliteFree(zCol);
592*4520Snw141292 sqliteExprDelete(pExpr->pLeft);
593*4520Snw141292 pExpr->pLeft = 0;
594*4520Snw141292 sqliteExprDelete(pExpr->pRight);
595*4520Snw141292 pExpr->pRight = 0;
596*4520Snw141292 pExpr->op = TK_COLUMN;
597*4520Snw141292 sqliteAuthRead(pParse, pExpr, pSrcList);
598*4520Snw141292 return cnt!=1;
599*4520Snw141292 }
600*4520Snw141292
601*4520Snw141292 /*
602*4520Snw141292 ** This routine walks an expression tree and resolves references to
603*4520Snw141292 ** table columns. Nodes of the form ID.ID or ID resolve into an
604*4520Snw141292 ** index to the table in the table list and a column offset. The
605*4520Snw141292 ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
606*4520Snw141292 ** value is changed to the index of the referenced table in pTabList
607*4520Snw141292 ** plus the "base" value. The base value will ultimately become the
608*4520Snw141292 ** VDBE cursor number for a cursor that is pointing into the referenced
609*4520Snw141292 ** table. The Expr.iColumn value is changed to the index of the column
610*4520Snw141292 ** of the referenced table. The Expr.iColumn value for the special
611*4520Snw141292 ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
612*4520Snw141292 ** alias for ROWID.
613*4520Snw141292 **
614*4520Snw141292 ** We also check for instances of the IN operator. IN comes in two
615*4520Snw141292 ** forms:
616*4520Snw141292 **
617*4520Snw141292 ** expr IN (exprlist)
618*4520Snw141292 ** and
619*4520Snw141292 ** expr IN (SELECT ...)
620*4520Snw141292 **
621*4520Snw141292 ** The first form is handled by creating a set holding the list
622*4520Snw141292 ** of allowed values. The second form causes the SELECT to generate
623*4520Snw141292 ** a temporary table.
624*4520Snw141292 **
625*4520Snw141292 ** This routine also looks for scalar SELECTs that are part of an expression.
626*4520Snw141292 ** If it finds any, it generates code to write the value of that select
627*4520Snw141292 ** into a memory cell.
628*4520Snw141292 **
629*4520Snw141292 ** Unknown columns or tables provoke an error. The function returns
630*4520Snw141292 ** the number of errors seen and leaves an error message on pParse->zErrMsg.
631*4520Snw141292 */
sqliteExprResolveIds(Parse * pParse,SrcList * pSrcList,ExprList * pEList,Expr * pExpr)632*4520Snw141292 int sqliteExprResolveIds(
633*4520Snw141292 Parse *pParse, /* The parser context */
634*4520Snw141292 SrcList *pSrcList, /* List of tables used to resolve column names */
635*4520Snw141292 ExprList *pEList, /* List of expressions used to resolve "AS" */
636*4520Snw141292 Expr *pExpr /* The expression to be analyzed. */
637*4520Snw141292 ){
638*4520Snw141292 int i;
639*4520Snw141292
640*4520Snw141292 if( pExpr==0 || pSrcList==0 ) return 0;
641*4520Snw141292 for(i=0; i<pSrcList->nSrc; i++){
642*4520Snw141292 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
643*4520Snw141292 }
644*4520Snw141292 switch( pExpr->op ){
645*4520Snw141292 /* Double-quoted strings (ex: "abc") are used as identifiers if
646*4520Snw141292 ** possible. Otherwise they remain as strings. Single-quoted
647*4520Snw141292 ** strings (ex: 'abc') are always string literals.
648*4520Snw141292 */
649*4520Snw141292 case TK_STRING: {
650*4520Snw141292 if( pExpr->token.z[0]=='\'' ) break;
651*4520Snw141292 /* Fall thru into the TK_ID case if this is a double-quoted string */
652*4520Snw141292 }
653*4520Snw141292 /* A lone identifier is the name of a columnd.
654*4520Snw141292 */
655*4520Snw141292 case TK_ID: {
656*4520Snw141292 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
657*4520Snw141292 return 1;
658*4520Snw141292 }
659*4520Snw141292 break;
660*4520Snw141292 }
661*4520Snw141292
662*4520Snw141292 /* A table name and column name: ID.ID
663*4520Snw141292 ** Or a database, table and column: ID.ID.ID
664*4520Snw141292 */
665*4520Snw141292 case TK_DOT: {
666*4520Snw141292 Token *pColumn;
667*4520Snw141292 Token *pTable;
668*4520Snw141292 Token *pDb;
669*4520Snw141292 Expr *pRight;
670*4520Snw141292
671*4520Snw141292 pRight = pExpr->pRight;
672*4520Snw141292 if( pRight->op==TK_ID ){
673*4520Snw141292 pDb = 0;
674*4520Snw141292 pTable = &pExpr->pLeft->token;
675*4520Snw141292 pColumn = &pRight->token;
676*4520Snw141292 }else{
677*4520Snw141292 assert( pRight->op==TK_DOT );
678*4520Snw141292 pDb = &pExpr->pLeft->token;
679*4520Snw141292 pTable = &pRight->pLeft->token;
680*4520Snw141292 pColumn = &pRight->pRight->token;
681*4520Snw141292 }
682*4520Snw141292 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
683*4520Snw141292 return 1;
684*4520Snw141292 }
685*4520Snw141292 break;
686*4520Snw141292 }
687*4520Snw141292
688*4520Snw141292 case TK_IN: {
689*4520Snw141292 Vdbe *v = sqliteGetVdbe(pParse);
690*4520Snw141292 if( v==0 ) return 1;
691*4520Snw141292 if( sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
692*4520Snw141292 return 1;
693*4520Snw141292 }
694*4520Snw141292 if( pExpr->pSelect ){
695*4520Snw141292 /* Case 1: expr IN (SELECT ...)
696*4520Snw141292 **
697*4520Snw141292 ** Generate code to write the results of the select into a temporary
698*4520Snw141292 ** table. The cursor number of the temporary table has already
699*4520Snw141292 ** been put in iTable by sqliteExprResolveInSelect().
700*4520Snw141292 */
701*4520Snw141292 pExpr->iTable = pParse->nTab++;
702*4520Snw141292 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
703*4520Snw141292 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
704*4520Snw141292 }else if( pExpr->pList ){
705*4520Snw141292 /* Case 2: expr IN (exprlist)
706*4520Snw141292 **
707*4520Snw141292 ** Create a set to put the exprlist values in. The Set id is stored
708*4520Snw141292 ** in iTable.
709*4520Snw141292 */
710*4520Snw141292 int i, iSet;
711*4520Snw141292 for(i=0; i<pExpr->pList->nExpr; i++){
712*4520Snw141292 Expr *pE2 = pExpr->pList->a[i].pExpr;
713*4520Snw141292 if( !sqliteExprIsConstant(pE2) ){
714*4520Snw141292 sqliteErrorMsg(pParse,
715*4520Snw141292 "right-hand side of IN operator must be constant");
716*4520Snw141292 return 1;
717*4520Snw141292 }
718*4520Snw141292 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
719*4520Snw141292 return 1;
720*4520Snw141292 }
721*4520Snw141292 }
722*4520Snw141292 iSet = pExpr->iTable = pParse->nSet++;
723*4520Snw141292 for(i=0; i<pExpr->pList->nExpr; i++){
724*4520Snw141292 Expr *pE2 = pExpr->pList->a[i].pExpr;
725*4520Snw141292 switch( pE2->op ){
726*4520Snw141292 case TK_FLOAT:
727*4520Snw141292 case TK_INTEGER:
728*4520Snw141292 case TK_STRING: {
729*4520Snw141292 int addr;
730*4520Snw141292 assert( pE2->token.z );
731*4520Snw141292 addr = sqliteVdbeOp3(v, OP_SetInsert, iSet, 0,
732*4520Snw141292 pE2->token.z, pE2->token.n);
733*4520Snw141292 sqliteVdbeDequoteP3(v, addr);
734*4520Snw141292 break;
735*4520Snw141292 }
736*4520Snw141292 default: {
737*4520Snw141292 sqliteExprCode(pParse, pE2);
738*4520Snw141292 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
739*4520Snw141292 break;
740*4520Snw141292 }
741*4520Snw141292 }
742*4520Snw141292 }
743*4520Snw141292 }
744*4520Snw141292 break;
745*4520Snw141292 }
746*4520Snw141292
747*4520Snw141292 case TK_SELECT: {
748*4520Snw141292 /* This has to be a scalar SELECT. Generate code to put the
749*4520Snw141292 ** value of this select in a memory cell and record the number
750*4520Snw141292 ** of the memory cell in iColumn.
751*4520Snw141292 */
752*4520Snw141292 pExpr->iColumn = pParse->nMem++;
753*4520Snw141292 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
754*4520Snw141292 return 1;
755*4520Snw141292 }
756*4520Snw141292 break;
757*4520Snw141292 }
758*4520Snw141292
759*4520Snw141292 /* For all else, just recursively walk the tree */
760*4520Snw141292 default: {
761*4520Snw141292 if( pExpr->pLeft
762*4520Snw141292 && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
763*4520Snw141292 return 1;
764*4520Snw141292 }
765*4520Snw141292 if( pExpr->pRight
766*4520Snw141292 && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
767*4520Snw141292 return 1;
768*4520Snw141292 }
769*4520Snw141292 if( pExpr->pList ){
770*4520Snw141292 int i;
771*4520Snw141292 ExprList *pList = pExpr->pList;
772*4520Snw141292 for(i=0; i<pList->nExpr; i++){
773*4520Snw141292 Expr *pArg = pList->a[i].pExpr;
774*4520Snw141292 if( sqliteExprResolveIds(pParse, pSrcList, pEList, pArg) ){
775*4520Snw141292 return 1;
776*4520Snw141292 }
777*4520Snw141292 }
778*4520Snw141292 }
779*4520Snw141292 }
780*4520Snw141292 }
781*4520Snw141292 return 0;
782*4520Snw141292 }
783*4520Snw141292
784*4520Snw141292 /*
785*4520Snw141292 ** pExpr is a node that defines a function of some kind. It might
786*4520Snw141292 ** be a syntactic function like "count(x)" or it might be a function
787*4520Snw141292 ** that implements an operator, like "a LIKE b".
788*4520Snw141292 **
789*4520Snw141292 ** This routine makes *pzName point to the name of the function and
790*4520Snw141292 ** *pnName hold the number of characters in the function name.
791*4520Snw141292 */
getFunctionName(Expr * pExpr,const char ** pzName,int * pnName)792*4520Snw141292 static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
793*4520Snw141292 switch( pExpr->op ){
794*4520Snw141292 case TK_FUNCTION: {
795*4520Snw141292 *pzName = pExpr->token.z;
796*4520Snw141292 *pnName = pExpr->token.n;
797*4520Snw141292 break;
798*4520Snw141292 }
799*4520Snw141292 case TK_LIKE: {
800*4520Snw141292 *pzName = "like";
801*4520Snw141292 *pnName = 4;
802*4520Snw141292 break;
803*4520Snw141292 }
804*4520Snw141292 case TK_GLOB: {
805*4520Snw141292 *pzName = "glob";
806*4520Snw141292 *pnName = 4;
807*4520Snw141292 break;
808*4520Snw141292 }
809*4520Snw141292 default: {
810*4520Snw141292 *pzName = "can't happen";
811*4520Snw141292 *pnName = 12;
812*4520Snw141292 break;
813*4520Snw141292 }
814*4520Snw141292 }
815*4520Snw141292 }
816*4520Snw141292
817*4520Snw141292 /*
818*4520Snw141292 ** Error check the functions in an expression. Make sure all
819*4520Snw141292 ** function names are recognized and all functions have the correct
820*4520Snw141292 ** number of arguments. Leave an error message in pParse->zErrMsg
821*4520Snw141292 ** if anything is amiss. Return the number of errors.
822*4520Snw141292 **
823*4520Snw141292 ** if pIsAgg is not null and this expression is an aggregate function
824*4520Snw141292 ** (like count(*) or max(value)) then write a 1 into *pIsAgg.
825*4520Snw141292 */
sqliteExprCheck(Parse * pParse,Expr * pExpr,int allowAgg,int * pIsAgg)826*4520Snw141292 int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
827*4520Snw141292 int nErr = 0;
828*4520Snw141292 if( pExpr==0 ) return 0;
829*4520Snw141292 switch( pExpr->op ){
830*4520Snw141292 case TK_GLOB:
831*4520Snw141292 case TK_LIKE:
832*4520Snw141292 case TK_FUNCTION: {
833*4520Snw141292 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
834*4520Snw141292 int no_such_func = 0; /* True if no such function exists */
835*4520Snw141292 int wrong_num_args = 0; /* True if wrong number of arguments */
836*4520Snw141292 int is_agg = 0; /* True if is an aggregate function */
837*4520Snw141292 int i;
838*4520Snw141292 int nId; /* Number of characters in function name */
839*4520Snw141292 const char *zId; /* The function name. */
840*4520Snw141292 FuncDef *pDef;
841*4520Snw141292
842*4520Snw141292 getFunctionName(pExpr, &zId, &nId);
843*4520Snw141292 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
844*4520Snw141292 if( pDef==0 ){
845*4520Snw141292 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
846*4520Snw141292 if( pDef==0 ){
847*4520Snw141292 no_such_func = 1;
848*4520Snw141292 }else{
849*4520Snw141292 wrong_num_args = 1;
850*4520Snw141292 }
851*4520Snw141292 }else{
852*4520Snw141292 is_agg = pDef->xFunc==0;
853*4520Snw141292 }
854*4520Snw141292 if( is_agg && !allowAgg ){
855*4520Snw141292 sqliteErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
856*4520Snw141292 nErr++;
857*4520Snw141292 is_agg = 0;
858*4520Snw141292 }else if( no_such_func ){
859*4520Snw141292 sqliteErrorMsg(pParse, "no such function: %.*s", nId, zId);
860*4520Snw141292 nErr++;
861*4520Snw141292 }else if( wrong_num_args ){
862*4520Snw141292 sqliteErrorMsg(pParse,"wrong number of arguments to function %.*s()",
863*4520Snw141292 nId, zId);
864*4520Snw141292 nErr++;
865*4520Snw141292 }
866*4520Snw141292 if( is_agg ){
867*4520Snw141292 pExpr->op = TK_AGG_FUNCTION;
868*4520Snw141292 if( pIsAgg ) *pIsAgg = 1;
869*4520Snw141292 }
870*4520Snw141292 for(i=0; nErr==0 && i<n; i++){
871*4520Snw141292 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
872*4520Snw141292 allowAgg && !is_agg, pIsAgg);
873*4520Snw141292 }
874*4520Snw141292 if( pDef==0 ){
875*4520Snw141292 /* Already reported an error */
876*4520Snw141292 }else if( pDef->dataType>=0 ){
877*4520Snw141292 if( pDef->dataType<n ){
878*4520Snw141292 pExpr->dataType =
879*4520Snw141292 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
880*4520Snw141292 }else{
881*4520Snw141292 pExpr->dataType = SQLITE_SO_NUM;
882*4520Snw141292 }
883*4520Snw141292 }else if( pDef->dataType==SQLITE_ARGS ){
884*4520Snw141292 pDef->dataType = SQLITE_SO_TEXT;
885*4520Snw141292 for(i=0; i<n; i++){
886*4520Snw141292 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
887*4520Snw141292 pExpr->dataType = SQLITE_SO_NUM;
888*4520Snw141292 break;
889*4520Snw141292 }
890*4520Snw141292 }
891*4520Snw141292 }else if( pDef->dataType==SQLITE_NUMERIC ){
892*4520Snw141292 pExpr->dataType = SQLITE_SO_NUM;
893*4520Snw141292 }else{
894*4520Snw141292 pExpr->dataType = SQLITE_SO_TEXT;
895*4520Snw141292 }
896*4520Snw141292 }
897*4520Snw141292 default: {
898*4520Snw141292 if( pExpr->pLeft ){
899*4520Snw141292 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
900*4520Snw141292 }
901*4520Snw141292 if( nErr==0 && pExpr->pRight ){
902*4520Snw141292 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
903*4520Snw141292 }
904*4520Snw141292 if( nErr==0 && pExpr->pList ){
905*4520Snw141292 int n = pExpr->pList->nExpr;
906*4520Snw141292 int i;
907*4520Snw141292 for(i=0; nErr==0 && i<n; i++){
908*4520Snw141292 Expr *pE2 = pExpr->pList->a[i].pExpr;
909*4520Snw141292 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
910*4520Snw141292 }
911*4520Snw141292 }
912*4520Snw141292 break;
913*4520Snw141292 }
914*4520Snw141292 }
915*4520Snw141292 return nErr;
916*4520Snw141292 }
917*4520Snw141292
918*4520Snw141292 /*
919*4520Snw141292 ** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
920*4520Snw141292 ** given expression should sort as numeric values or as text.
921*4520Snw141292 **
922*4520Snw141292 ** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
923*4520Snw141292 ** both been called on the expression before it is passed to this routine.
924*4520Snw141292 */
sqliteExprType(Expr * p)925*4520Snw141292 int sqliteExprType(Expr *p){
926*4520Snw141292 if( p==0 ) return SQLITE_SO_NUM;
927*4520Snw141292 while( p ) switch( p->op ){
928*4520Snw141292 case TK_PLUS:
929*4520Snw141292 case TK_MINUS:
930*4520Snw141292 case TK_STAR:
931*4520Snw141292 case TK_SLASH:
932*4520Snw141292 case TK_AND:
933*4520Snw141292 case TK_OR:
934*4520Snw141292 case TK_ISNULL:
935*4520Snw141292 case TK_NOTNULL:
936*4520Snw141292 case TK_NOT:
937*4520Snw141292 case TK_UMINUS:
938*4520Snw141292 case TK_UPLUS:
939*4520Snw141292 case TK_BITAND:
940*4520Snw141292 case TK_BITOR:
941*4520Snw141292 case TK_BITNOT:
942*4520Snw141292 case TK_LSHIFT:
943*4520Snw141292 case TK_RSHIFT:
944*4520Snw141292 case TK_REM:
945*4520Snw141292 case TK_INTEGER:
946*4520Snw141292 case TK_FLOAT:
947*4520Snw141292 case TK_IN:
948*4520Snw141292 case TK_BETWEEN:
949*4520Snw141292 case TK_GLOB:
950*4520Snw141292 case TK_LIKE:
951*4520Snw141292 return SQLITE_SO_NUM;
952*4520Snw141292
953*4520Snw141292 case TK_STRING:
954*4520Snw141292 case TK_NULL:
955*4520Snw141292 case TK_CONCAT:
956*4520Snw141292 case TK_VARIABLE:
957*4520Snw141292 return SQLITE_SO_TEXT;
958*4520Snw141292
959*4520Snw141292 case TK_LT:
960*4520Snw141292 case TK_LE:
961*4520Snw141292 case TK_GT:
962*4520Snw141292 case TK_GE:
963*4520Snw141292 case TK_NE:
964*4520Snw141292 case TK_EQ:
965*4520Snw141292 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
966*4520Snw141292 return SQLITE_SO_NUM;
967*4520Snw141292 }
968*4520Snw141292 p = p->pRight;
969*4520Snw141292 break;
970*4520Snw141292
971*4520Snw141292 case TK_AS:
972*4520Snw141292 p = p->pLeft;
973*4520Snw141292 break;
974*4520Snw141292
975*4520Snw141292 case TK_COLUMN:
976*4520Snw141292 case TK_FUNCTION:
977*4520Snw141292 case TK_AGG_FUNCTION:
978*4520Snw141292 return p->dataType;
979*4520Snw141292
980*4520Snw141292 case TK_SELECT:
981*4520Snw141292 assert( p->pSelect );
982*4520Snw141292 assert( p->pSelect->pEList );
983*4520Snw141292 assert( p->pSelect->pEList->nExpr>0 );
984*4520Snw141292 p = p->pSelect->pEList->a[0].pExpr;
985*4520Snw141292 break;
986*4520Snw141292
987*4520Snw141292 case TK_CASE: {
988*4520Snw141292 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
989*4520Snw141292 return SQLITE_SO_NUM;
990*4520Snw141292 }
991*4520Snw141292 if( p->pList ){
992*4520Snw141292 int i;
993*4520Snw141292 ExprList *pList = p->pList;
994*4520Snw141292 for(i=1; i<pList->nExpr; i+=2){
995*4520Snw141292 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
996*4520Snw141292 return SQLITE_SO_NUM;
997*4520Snw141292 }
998*4520Snw141292 }
999*4520Snw141292 }
1000*4520Snw141292 return SQLITE_SO_TEXT;
1001*4520Snw141292 }
1002*4520Snw141292
1003*4520Snw141292 default:
1004*4520Snw141292 assert( p->op==TK_ABORT ); /* Can't Happen */
1005*4520Snw141292 break;
1006*4520Snw141292 }
1007*4520Snw141292 return SQLITE_SO_NUM;
1008*4520Snw141292 }
1009*4520Snw141292
1010*4520Snw141292 /*
1011*4520Snw141292 ** Generate code into the current Vdbe to evaluate the given
1012*4520Snw141292 ** expression and leave the result on the top of stack.
1013*4520Snw141292 */
sqliteExprCode(Parse * pParse,Expr * pExpr)1014*4520Snw141292 void sqliteExprCode(Parse *pParse, Expr *pExpr){
1015*4520Snw141292 Vdbe *v = pParse->pVdbe;
1016*4520Snw141292 int op;
1017*4520Snw141292 if( v==0 || pExpr==0 ) return;
1018*4520Snw141292 switch( pExpr->op ){
1019*4520Snw141292 case TK_PLUS: op = OP_Add; break;
1020*4520Snw141292 case TK_MINUS: op = OP_Subtract; break;
1021*4520Snw141292 case TK_STAR: op = OP_Multiply; break;
1022*4520Snw141292 case TK_SLASH: op = OP_Divide; break;
1023*4520Snw141292 case TK_AND: op = OP_And; break;
1024*4520Snw141292 case TK_OR: op = OP_Or; break;
1025*4520Snw141292 case TK_LT: op = OP_Lt; break;
1026*4520Snw141292 case TK_LE: op = OP_Le; break;
1027*4520Snw141292 case TK_GT: op = OP_Gt; break;
1028*4520Snw141292 case TK_GE: op = OP_Ge; break;
1029*4520Snw141292 case TK_NE: op = OP_Ne; break;
1030*4520Snw141292 case TK_EQ: op = OP_Eq; break;
1031*4520Snw141292 case TK_ISNULL: op = OP_IsNull; break;
1032*4520Snw141292 case TK_NOTNULL: op = OP_NotNull; break;
1033*4520Snw141292 case TK_NOT: op = OP_Not; break;
1034*4520Snw141292 case TK_UMINUS: op = OP_Negative; break;
1035*4520Snw141292 case TK_BITAND: op = OP_BitAnd; break;
1036*4520Snw141292 case TK_BITOR: op = OP_BitOr; break;
1037*4520Snw141292 case TK_BITNOT: op = OP_BitNot; break;
1038*4520Snw141292 case TK_LSHIFT: op = OP_ShiftLeft; break;
1039*4520Snw141292 case TK_RSHIFT: op = OP_ShiftRight; break;
1040*4520Snw141292 case TK_REM: op = OP_Remainder; break;
1041*4520Snw141292 default: break;
1042*4520Snw141292 }
1043*4520Snw141292 switch( pExpr->op ){
1044*4520Snw141292 case TK_COLUMN: {
1045*4520Snw141292 if( pParse->useAgg ){
1046*4520Snw141292 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
1047*4520Snw141292 }else if( pExpr->iColumn>=0 ){
1048*4520Snw141292 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
1049*4520Snw141292 }else{
1050*4520Snw141292 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
1051*4520Snw141292 }
1052*4520Snw141292 break;
1053*4520Snw141292 }
1054*4520Snw141292 case TK_STRING:
1055*4520Snw141292 case TK_FLOAT:
1056*4520Snw141292 case TK_INTEGER: {
1057*4520Snw141292 if( pExpr->op==TK_INTEGER && sqliteFitsIn32Bits(pExpr->token.z) ){
1058*4520Snw141292 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
1059*4520Snw141292 }else{
1060*4520Snw141292 sqliteVdbeAddOp(v, OP_String, 0, 0);
1061*4520Snw141292 }
1062*4520Snw141292 assert( pExpr->token.z );
1063*4520Snw141292 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1064*4520Snw141292 sqliteVdbeDequoteP3(v, -1);
1065*4520Snw141292 break;
1066*4520Snw141292 }
1067*4520Snw141292 case TK_NULL: {
1068*4520Snw141292 sqliteVdbeAddOp(v, OP_String, 0, 0);
1069*4520Snw141292 break;
1070*4520Snw141292 }
1071*4520Snw141292 case TK_VARIABLE: {
1072*4520Snw141292 sqliteVdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
1073*4520Snw141292 break;
1074*4520Snw141292 }
1075*4520Snw141292 case TK_LT:
1076*4520Snw141292 case TK_LE:
1077*4520Snw141292 case TK_GT:
1078*4520Snw141292 case TK_GE:
1079*4520Snw141292 case TK_NE:
1080*4520Snw141292 case TK_EQ: {
1081*4520Snw141292 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1082*4520Snw141292 op += 6; /* Convert numeric opcodes to text opcodes */
1083*4520Snw141292 }
1084*4520Snw141292 /* Fall through into the next case */
1085*4520Snw141292 }
1086*4520Snw141292 case TK_AND:
1087*4520Snw141292 case TK_OR:
1088*4520Snw141292 case TK_PLUS:
1089*4520Snw141292 case TK_STAR:
1090*4520Snw141292 case TK_MINUS:
1091*4520Snw141292 case TK_REM:
1092*4520Snw141292 case TK_BITAND:
1093*4520Snw141292 case TK_BITOR:
1094*4520Snw141292 case TK_SLASH: {
1095*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1096*4520Snw141292 sqliteExprCode(pParse, pExpr->pRight);
1097*4520Snw141292 sqliteVdbeAddOp(v, op, 0, 0);
1098*4520Snw141292 break;
1099*4520Snw141292 }
1100*4520Snw141292 case TK_LSHIFT:
1101*4520Snw141292 case TK_RSHIFT: {
1102*4520Snw141292 sqliteExprCode(pParse, pExpr->pRight);
1103*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1104*4520Snw141292 sqliteVdbeAddOp(v, op, 0, 0);
1105*4520Snw141292 break;
1106*4520Snw141292 }
1107*4520Snw141292 case TK_CONCAT: {
1108*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1109*4520Snw141292 sqliteExprCode(pParse, pExpr->pRight);
1110*4520Snw141292 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
1111*4520Snw141292 break;
1112*4520Snw141292 }
1113*4520Snw141292 case TK_UMINUS: {
1114*4520Snw141292 assert( pExpr->pLeft );
1115*4520Snw141292 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
1116*4520Snw141292 Token *p = &pExpr->pLeft->token;
1117*4520Snw141292 char *z = sqliteMalloc( p->n + 2 );
1118*4520Snw141292 sprintf(z, "-%.*s", p->n, p->z);
1119*4520Snw141292 if( pExpr->pLeft->op==TK_INTEGER && sqliteFitsIn32Bits(z) ){
1120*4520Snw141292 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1121*4520Snw141292 }else{
1122*4520Snw141292 sqliteVdbeAddOp(v, OP_String, 0, 0);
1123*4520Snw141292 }
1124*4520Snw141292 sqliteVdbeChangeP3(v, -1, z, p->n+1);
1125*4520Snw141292 sqliteFree(z);
1126*4520Snw141292 break;
1127*4520Snw141292 }
1128*4520Snw141292 /* Fall through into TK_NOT */
1129*4520Snw141292 }
1130*4520Snw141292 case TK_BITNOT:
1131*4520Snw141292 case TK_NOT: {
1132*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1133*4520Snw141292 sqliteVdbeAddOp(v, op, 0, 0);
1134*4520Snw141292 break;
1135*4520Snw141292 }
1136*4520Snw141292 case TK_ISNULL:
1137*4520Snw141292 case TK_NOTNULL: {
1138*4520Snw141292 int dest;
1139*4520Snw141292 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
1140*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1141*4520Snw141292 dest = sqliteVdbeCurrentAddr(v) + 2;
1142*4520Snw141292 sqliteVdbeAddOp(v, op, 1, dest);
1143*4520Snw141292 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
1144*4520Snw141292 break;
1145*4520Snw141292 }
1146*4520Snw141292 case TK_AGG_FUNCTION: {
1147*4520Snw141292 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
1148*4520Snw141292 break;
1149*4520Snw141292 }
1150*4520Snw141292 case TK_GLOB:
1151*4520Snw141292 case TK_LIKE:
1152*4520Snw141292 case TK_FUNCTION: {
1153*4520Snw141292 ExprList *pList = pExpr->pList;
1154*4520Snw141292 int nExpr = pList ? pList->nExpr : 0;
1155*4520Snw141292 FuncDef *pDef;
1156*4520Snw141292 int nId;
1157*4520Snw141292 const char *zId;
1158*4520Snw141292 getFunctionName(pExpr, &zId, &nId);
1159*4520Snw141292 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
1160*4520Snw141292 assert( pDef!=0 );
1161*4520Snw141292 nExpr = sqliteExprCodeExprList(pParse, pList, pDef->includeTypes);
1162*4520Snw141292 sqliteVdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
1163*4520Snw141292 break;
1164*4520Snw141292 }
1165*4520Snw141292 case TK_SELECT: {
1166*4520Snw141292 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
1167*4520Snw141292 break;
1168*4520Snw141292 }
1169*4520Snw141292 case TK_IN: {
1170*4520Snw141292 int addr;
1171*4520Snw141292 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
1172*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1173*4520Snw141292 addr = sqliteVdbeCurrentAddr(v);
1174*4520Snw141292 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1175*4520Snw141292 sqliteVdbeAddOp(v, OP_Pop, 2, 0);
1176*4520Snw141292 sqliteVdbeAddOp(v, OP_String, 0, 0);
1177*4520Snw141292 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
1178*4520Snw141292 if( pExpr->pSelect ){
1179*4520Snw141292 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
1180*4520Snw141292 }else{
1181*4520Snw141292 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
1182*4520Snw141292 }
1183*4520Snw141292 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
1184*4520Snw141292 break;
1185*4520Snw141292 }
1186*4520Snw141292 case TK_BETWEEN: {
1187*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1188*4520Snw141292 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1189*4520Snw141292 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1190*4520Snw141292 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1191*4520Snw141292 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1192*4520Snw141292 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1193*4520Snw141292 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1194*4520Snw141292 sqliteVdbeAddOp(v, OP_And, 0, 0);
1195*4520Snw141292 break;
1196*4520Snw141292 }
1197*4520Snw141292 case TK_UPLUS:
1198*4520Snw141292 case TK_AS: {
1199*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1200*4520Snw141292 break;
1201*4520Snw141292 }
1202*4520Snw141292 case TK_CASE: {
1203*4520Snw141292 int expr_end_label;
1204*4520Snw141292 int jumpInst;
1205*4520Snw141292 int addr;
1206*4520Snw141292 int nExpr;
1207*4520Snw141292 int i;
1208*4520Snw141292
1209*4520Snw141292 assert(pExpr->pList);
1210*4520Snw141292 assert((pExpr->pList->nExpr % 2) == 0);
1211*4520Snw141292 assert(pExpr->pList->nExpr > 0);
1212*4520Snw141292 nExpr = pExpr->pList->nExpr;
1213*4520Snw141292 expr_end_label = sqliteVdbeMakeLabel(v);
1214*4520Snw141292 if( pExpr->pLeft ){
1215*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1216*4520Snw141292 }
1217*4520Snw141292 for(i=0; i<nExpr; i=i+2){
1218*4520Snw141292 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
1219*4520Snw141292 if( pExpr->pLeft ){
1220*4520Snw141292 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
1221*4520Snw141292 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1222*4520Snw141292 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1223*4520Snw141292 }else{
1224*4520Snw141292 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
1225*4520Snw141292 }
1226*4520Snw141292 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1227*4520Snw141292 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
1228*4520Snw141292 addr = sqliteVdbeCurrentAddr(v);
1229*4520Snw141292 sqliteVdbeChangeP2(v, jumpInst, addr);
1230*4520Snw141292 }
1231*4520Snw141292 if( pExpr->pLeft ){
1232*4520Snw141292 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1233*4520Snw141292 }
1234*4520Snw141292 if( pExpr->pRight ){
1235*4520Snw141292 sqliteExprCode(pParse, pExpr->pRight);
1236*4520Snw141292 }else{
1237*4520Snw141292 sqliteVdbeAddOp(v, OP_String, 0, 0);
1238*4520Snw141292 }
1239*4520Snw141292 sqliteVdbeResolveLabel(v, expr_end_label);
1240*4520Snw141292 break;
1241*4520Snw141292 }
1242*4520Snw141292 case TK_RAISE: {
1243*4520Snw141292 if( !pParse->trigStack ){
1244*4520Snw141292 sqliteErrorMsg(pParse,
1245*4520Snw141292 "RAISE() may only be used within a trigger-program");
1246*4520Snw141292 pParse->nErr++;
1247*4520Snw141292 return;
1248*4520Snw141292 }
1249*4520Snw141292 if( pExpr->iColumn == OE_Rollback ||
1250*4520Snw141292 pExpr->iColumn == OE_Abort ||
1251*4520Snw141292 pExpr->iColumn == OE_Fail ){
1252*4520Snw141292 sqliteVdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1253*4520Snw141292 pExpr->token.z, pExpr->token.n);
1254*4520Snw141292 sqliteVdbeDequoteP3(v, -1);
1255*4520Snw141292 } else {
1256*4520Snw141292 assert( pExpr->iColumn == OE_Ignore );
1257*4520Snw141292 sqliteVdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
1258*4520Snw141292 "(IGNORE jump)", 0);
1259*4520Snw141292 }
1260*4520Snw141292 }
1261*4520Snw141292 break;
1262*4520Snw141292 }
1263*4520Snw141292 }
1264*4520Snw141292
1265*4520Snw141292 /*
1266*4520Snw141292 ** Generate code that pushes the value of every element of the given
1267*4520Snw141292 ** expression list onto the stack. If the includeTypes flag is true,
1268*4520Snw141292 ** then also push a string that is the datatype of each element onto
1269*4520Snw141292 ** the stack after the value.
1270*4520Snw141292 **
1271*4520Snw141292 ** Return the number of elements pushed onto the stack.
1272*4520Snw141292 */
sqliteExprCodeExprList(Parse * pParse,ExprList * pList,int includeTypes)1273*4520Snw141292 int sqliteExprCodeExprList(
1274*4520Snw141292 Parse *pParse, /* Parsing context */
1275*4520Snw141292 ExprList *pList, /* The expression list to be coded */
1276*4520Snw141292 int includeTypes /* TRUE to put datatypes on the stack too */
1277*4520Snw141292 ){
1278*4520Snw141292 struct ExprList_item *pItem;
1279*4520Snw141292 int i, n;
1280*4520Snw141292 Vdbe *v;
1281*4520Snw141292 if( pList==0 ) return 0;
1282*4520Snw141292 v = sqliteGetVdbe(pParse);
1283*4520Snw141292 n = pList->nExpr;
1284*4520Snw141292 for(pItem=pList->a, i=0; i<n; i++, pItem++){
1285*4520Snw141292 sqliteExprCode(pParse, pItem->pExpr);
1286*4520Snw141292 if( includeTypes ){
1287*4520Snw141292 sqliteVdbeOp3(v, OP_String, 0, 0,
1288*4520Snw141292 sqliteExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text",
1289*4520Snw141292 P3_STATIC);
1290*4520Snw141292 }
1291*4520Snw141292 }
1292*4520Snw141292 return includeTypes ? n*2 : n;
1293*4520Snw141292 }
1294*4520Snw141292
1295*4520Snw141292 /*
1296*4520Snw141292 ** Generate code for a boolean expression such that a jump is made
1297*4520Snw141292 ** to the label "dest" if the expression is true but execution
1298*4520Snw141292 ** continues straight thru if the expression is false.
1299*4520Snw141292 **
1300*4520Snw141292 ** If the expression evaluates to NULL (neither true nor false), then
1301*4520Snw141292 ** take the jump if the jumpIfNull flag is true.
1302*4520Snw141292 */
sqliteExprIfTrue(Parse * pParse,Expr * pExpr,int dest,int jumpIfNull)1303*4520Snw141292 void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1304*4520Snw141292 Vdbe *v = pParse->pVdbe;
1305*4520Snw141292 int op = 0;
1306*4520Snw141292 if( v==0 || pExpr==0 ) return;
1307*4520Snw141292 switch( pExpr->op ){
1308*4520Snw141292 case TK_LT: op = OP_Lt; break;
1309*4520Snw141292 case TK_LE: op = OP_Le; break;
1310*4520Snw141292 case TK_GT: op = OP_Gt; break;
1311*4520Snw141292 case TK_GE: op = OP_Ge; break;
1312*4520Snw141292 case TK_NE: op = OP_Ne; break;
1313*4520Snw141292 case TK_EQ: op = OP_Eq; break;
1314*4520Snw141292 case TK_ISNULL: op = OP_IsNull; break;
1315*4520Snw141292 case TK_NOTNULL: op = OP_NotNull; break;
1316*4520Snw141292 default: break;
1317*4520Snw141292 }
1318*4520Snw141292 switch( pExpr->op ){
1319*4520Snw141292 case TK_AND: {
1320*4520Snw141292 int d2 = sqliteVdbeMakeLabel(v);
1321*4520Snw141292 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1322*4520Snw141292 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1323*4520Snw141292 sqliteVdbeResolveLabel(v, d2);
1324*4520Snw141292 break;
1325*4520Snw141292 }
1326*4520Snw141292 case TK_OR: {
1327*4520Snw141292 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1328*4520Snw141292 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1329*4520Snw141292 break;
1330*4520Snw141292 }
1331*4520Snw141292 case TK_NOT: {
1332*4520Snw141292 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1333*4520Snw141292 break;
1334*4520Snw141292 }
1335*4520Snw141292 case TK_LT:
1336*4520Snw141292 case TK_LE:
1337*4520Snw141292 case TK_GT:
1338*4520Snw141292 case TK_GE:
1339*4520Snw141292 case TK_NE:
1340*4520Snw141292 case TK_EQ: {
1341*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1342*4520Snw141292 sqliteExprCode(pParse, pExpr->pRight);
1343*4520Snw141292 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1344*4520Snw141292 op += 6; /* Convert numeric opcodes to text opcodes */
1345*4520Snw141292 }
1346*4520Snw141292 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
1347*4520Snw141292 break;
1348*4520Snw141292 }
1349*4520Snw141292 case TK_ISNULL:
1350*4520Snw141292 case TK_NOTNULL: {
1351*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1352*4520Snw141292 sqliteVdbeAddOp(v, op, 1, dest);
1353*4520Snw141292 break;
1354*4520Snw141292 }
1355*4520Snw141292 case TK_IN: {
1356*4520Snw141292 int addr;
1357*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1358*4520Snw141292 addr = sqliteVdbeCurrentAddr(v);
1359*4520Snw141292 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1360*4520Snw141292 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1361*4520Snw141292 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
1362*4520Snw141292 if( pExpr->pSelect ){
1363*4520Snw141292 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
1364*4520Snw141292 }else{
1365*4520Snw141292 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
1366*4520Snw141292 }
1367*4520Snw141292 break;
1368*4520Snw141292 }
1369*4520Snw141292 case TK_BETWEEN: {
1370*4520Snw141292 int addr;
1371*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1372*4520Snw141292 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1373*4520Snw141292 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1374*4520Snw141292 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
1375*4520Snw141292 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1376*4520Snw141292 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
1377*4520Snw141292 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1378*4520Snw141292 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
1379*4520Snw141292 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1380*4520Snw141292 break;
1381*4520Snw141292 }
1382*4520Snw141292 default: {
1383*4520Snw141292 sqliteExprCode(pParse, pExpr);
1384*4520Snw141292 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
1385*4520Snw141292 break;
1386*4520Snw141292 }
1387*4520Snw141292 }
1388*4520Snw141292 }
1389*4520Snw141292
1390*4520Snw141292 /*
1391*4520Snw141292 ** Generate code for a boolean expression such that a jump is made
1392*4520Snw141292 ** to the label "dest" if the expression is false but execution
1393*4520Snw141292 ** continues straight thru if the expression is true.
1394*4520Snw141292 **
1395*4520Snw141292 ** If the expression evaluates to NULL (neither true nor false) then
1396*4520Snw141292 ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
1397*4520Snw141292 */
sqliteExprIfFalse(Parse * pParse,Expr * pExpr,int dest,int jumpIfNull)1398*4520Snw141292 void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1399*4520Snw141292 Vdbe *v = pParse->pVdbe;
1400*4520Snw141292 int op = 0;
1401*4520Snw141292 if( v==0 || pExpr==0 ) return;
1402*4520Snw141292 switch( pExpr->op ){
1403*4520Snw141292 case TK_LT: op = OP_Ge; break;
1404*4520Snw141292 case TK_LE: op = OP_Gt; break;
1405*4520Snw141292 case TK_GT: op = OP_Le; break;
1406*4520Snw141292 case TK_GE: op = OP_Lt; break;
1407*4520Snw141292 case TK_NE: op = OP_Eq; break;
1408*4520Snw141292 case TK_EQ: op = OP_Ne; break;
1409*4520Snw141292 case TK_ISNULL: op = OP_NotNull; break;
1410*4520Snw141292 case TK_NOTNULL: op = OP_IsNull; break;
1411*4520Snw141292 default: break;
1412*4520Snw141292 }
1413*4520Snw141292 switch( pExpr->op ){
1414*4520Snw141292 case TK_AND: {
1415*4520Snw141292 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1416*4520Snw141292 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1417*4520Snw141292 break;
1418*4520Snw141292 }
1419*4520Snw141292 case TK_OR: {
1420*4520Snw141292 int d2 = sqliteVdbeMakeLabel(v);
1421*4520Snw141292 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1422*4520Snw141292 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1423*4520Snw141292 sqliteVdbeResolveLabel(v, d2);
1424*4520Snw141292 break;
1425*4520Snw141292 }
1426*4520Snw141292 case TK_NOT: {
1427*4520Snw141292 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1428*4520Snw141292 break;
1429*4520Snw141292 }
1430*4520Snw141292 case TK_LT:
1431*4520Snw141292 case TK_LE:
1432*4520Snw141292 case TK_GT:
1433*4520Snw141292 case TK_GE:
1434*4520Snw141292 case TK_NE:
1435*4520Snw141292 case TK_EQ: {
1436*4520Snw141292 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
1437*4520Snw141292 /* Convert numeric comparison opcodes into text comparison opcodes.
1438*4520Snw141292 ** This step depends on the fact that the text comparision opcodes are
1439*4520Snw141292 ** always 6 greater than their corresponding numeric comparison
1440*4520Snw141292 ** opcodes.
1441*4520Snw141292 */
1442*4520Snw141292 assert( OP_Eq+6 == OP_StrEq );
1443*4520Snw141292 op += 6;
1444*4520Snw141292 }
1445*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1446*4520Snw141292 sqliteExprCode(pParse, pExpr->pRight);
1447*4520Snw141292 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
1448*4520Snw141292 break;
1449*4520Snw141292 }
1450*4520Snw141292 case TK_ISNULL:
1451*4520Snw141292 case TK_NOTNULL: {
1452*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1453*4520Snw141292 sqliteVdbeAddOp(v, op, 1, dest);
1454*4520Snw141292 break;
1455*4520Snw141292 }
1456*4520Snw141292 case TK_IN: {
1457*4520Snw141292 int addr;
1458*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1459*4520Snw141292 addr = sqliteVdbeCurrentAddr(v);
1460*4520Snw141292 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1461*4520Snw141292 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1462*4520Snw141292 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
1463*4520Snw141292 if( pExpr->pSelect ){
1464*4520Snw141292 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
1465*4520Snw141292 }else{
1466*4520Snw141292 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
1467*4520Snw141292 }
1468*4520Snw141292 break;
1469*4520Snw141292 }
1470*4520Snw141292 case TK_BETWEEN: {
1471*4520Snw141292 int addr;
1472*4520Snw141292 sqliteExprCode(pParse, pExpr->pLeft);
1473*4520Snw141292 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1474*4520Snw141292 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1475*4520Snw141292 addr = sqliteVdbeCurrentAddr(v);
1476*4520Snw141292 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
1477*4520Snw141292 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1478*4520Snw141292 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
1479*4520Snw141292 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1480*4520Snw141292 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
1481*4520Snw141292 break;
1482*4520Snw141292 }
1483*4520Snw141292 default: {
1484*4520Snw141292 sqliteExprCode(pParse, pExpr);
1485*4520Snw141292 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
1486*4520Snw141292 break;
1487*4520Snw141292 }
1488*4520Snw141292 }
1489*4520Snw141292 }
1490*4520Snw141292
1491*4520Snw141292 /*
1492*4520Snw141292 ** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1493*4520Snw141292 ** if they are identical and return FALSE if they differ in any way.
1494*4520Snw141292 */
sqliteExprCompare(Expr * pA,Expr * pB)1495*4520Snw141292 int sqliteExprCompare(Expr *pA, Expr *pB){
1496*4520Snw141292 int i;
1497*4520Snw141292 if( pA==0 ){
1498*4520Snw141292 return pB==0;
1499*4520Snw141292 }else if( pB==0 ){
1500*4520Snw141292 return 0;
1501*4520Snw141292 }
1502*4520Snw141292 if( pA->op!=pB->op ) return 0;
1503*4520Snw141292 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1504*4520Snw141292 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
1505*4520Snw141292 if( pA->pList ){
1506*4520Snw141292 if( pB->pList==0 ) return 0;
1507*4520Snw141292 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1508*4520Snw141292 for(i=0; i<pA->pList->nExpr; i++){
1509*4520Snw141292 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
1510*4520Snw141292 return 0;
1511*4520Snw141292 }
1512*4520Snw141292 }
1513*4520Snw141292 }else if( pB->pList ){
1514*4520Snw141292 return 0;
1515*4520Snw141292 }
1516*4520Snw141292 if( pA->pSelect || pB->pSelect ) return 0;
1517*4520Snw141292 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
1518*4520Snw141292 if( pA->token.z ){
1519*4520Snw141292 if( pB->token.z==0 ) return 0;
1520*4520Snw141292 if( pB->token.n!=pA->token.n ) return 0;
1521*4520Snw141292 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
1522*4520Snw141292 }
1523*4520Snw141292 return 1;
1524*4520Snw141292 }
1525*4520Snw141292
1526*4520Snw141292 /*
1527*4520Snw141292 ** Add a new element to the pParse->aAgg[] array and return its index.
1528*4520Snw141292 */
appendAggInfo(Parse * pParse)1529*4520Snw141292 static int appendAggInfo(Parse *pParse){
1530*4520Snw141292 if( (pParse->nAgg & 0x7)==0 ){
1531*4520Snw141292 int amt = pParse->nAgg + 8;
1532*4520Snw141292 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1533*4520Snw141292 if( aAgg==0 ){
1534*4520Snw141292 return -1;
1535*4520Snw141292 }
1536*4520Snw141292 pParse->aAgg = aAgg;
1537*4520Snw141292 }
1538*4520Snw141292 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1539*4520Snw141292 return pParse->nAgg++;
1540*4520Snw141292 }
1541*4520Snw141292
1542*4520Snw141292 /*
1543*4520Snw141292 ** Analyze the given expression looking for aggregate functions and
1544*4520Snw141292 ** for variables that need to be added to the pParse->aAgg[] array.
1545*4520Snw141292 ** Make additional entries to the pParse->aAgg[] array as necessary.
1546*4520Snw141292 **
1547*4520Snw141292 ** This routine should only be called after the expression has been
1548*4520Snw141292 ** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1549*4520Snw141292 **
1550*4520Snw141292 ** If errors are seen, leave an error message in zErrMsg and return
1551*4520Snw141292 ** the number of errors.
1552*4520Snw141292 */
sqliteExprAnalyzeAggregates(Parse * pParse,Expr * pExpr)1553*4520Snw141292 int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1554*4520Snw141292 int i;
1555*4520Snw141292 AggExpr *aAgg;
1556*4520Snw141292 int nErr = 0;
1557*4520Snw141292
1558*4520Snw141292 if( pExpr==0 ) return 0;
1559*4520Snw141292 switch( pExpr->op ){
1560*4520Snw141292 case TK_COLUMN: {
1561*4520Snw141292 aAgg = pParse->aAgg;
1562*4520Snw141292 for(i=0; i<pParse->nAgg; i++){
1563*4520Snw141292 if( aAgg[i].isAgg ) continue;
1564*4520Snw141292 if( aAgg[i].pExpr->iTable==pExpr->iTable
1565*4520Snw141292 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
1566*4520Snw141292 break;
1567*4520Snw141292 }
1568*4520Snw141292 }
1569*4520Snw141292 if( i>=pParse->nAgg ){
1570*4520Snw141292 i = appendAggInfo(pParse);
1571*4520Snw141292 if( i<0 ) return 1;
1572*4520Snw141292 pParse->aAgg[i].isAgg = 0;
1573*4520Snw141292 pParse->aAgg[i].pExpr = pExpr;
1574*4520Snw141292 }
1575*4520Snw141292 pExpr->iAgg = i;
1576*4520Snw141292 break;
1577*4520Snw141292 }
1578*4520Snw141292 case TK_AGG_FUNCTION: {
1579*4520Snw141292 aAgg = pParse->aAgg;
1580*4520Snw141292 for(i=0; i<pParse->nAgg; i++){
1581*4520Snw141292 if( !aAgg[i].isAgg ) continue;
1582*4520Snw141292 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
1583*4520Snw141292 break;
1584*4520Snw141292 }
1585*4520Snw141292 }
1586*4520Snw141292 if( i>=pParse->nAgg ){
1587*4520Snw141292 i = appendAggInfo(pParse);
1588*4520Snw141292 if( i<0 ) return 1;
1589*4520Snw141292 pParse->aAgg[i].isAgg = 1;
1590*4520Snw141292 pParse->aAgg[i].pExpr = pExpr;
1591*4520Snw141292 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
1592*4520Snw141292 pExpr->token.z, pExpr->token.n,
1593*4520Snw141292 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
1594*4520Snw141292 }
1595*4520Snw141292 pExpr->iAgg = i;
1596*4520Snw141292 break;
1597*4520Snw141292 }
1598*4520Snw141292 default: {
1599*4520Snw141292 if( pExpr->pLeft ){
1600*4520Snw141292 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1601*4520Snw141292 }
1602*4520Snw141292 if( nErr==0 && pExpr->pRight ){
1603*4520Snw141292 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1604*4520Snw141292 }
1605*4520Snw141292 if( nErr==0 && pExpr->pList ){
1606*4520Snw141292 int n = pExpr->pList->nExpr;
1607*4520Snw141292 int i;
1608*4520Snw141292 for(i=0; nErr==0 && i<n; i++){
1609*4520Snw141292 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1610*4520Snw141292 }
1611*4520Snw141292 }
1612*4520Snw141292 break;
1613*4520Snw141292 }
1614*4520Snw141292 }
1615*4520Snw141292 return nErr;
1616*4520Snw141292 }
1617*4520Snw141292
1618*4520Snw141292 /*
1619*4520Snw141292 ** Locate a user function given a name and a number of arguments.
1620*4520Snw141292 ** Return a pointer to the FuncDef structure that defines that
1621*4520Snw141292 ** function, or return NULL if the function does not exist.
1622*4520Snw141292 **
1623*4520Snw141292 ** If the createFlag argument is true, then a new (blank) FuncDef
1624*4520Snw141292 ** structure is created and liked into the "db" structure if a
1625*4520Snw141292 ** no matching function previously existed. When createFlag is true
1626*4520Snw141292 ** and the nArg parameter is -1, then only a function that accepts
1627*4520Snw141292 ** any number of arguments will be returned.
1628*4520Snw141292 **
1629*4520Snw141292 ** If createFlag is false and nArg is -1, then the first valid
1630*4520Snw141292 ** function found is returned. A function is valid if either xFunc
1631*4520Snw141292 ** or xStep is non-zero.
1632*4520Snw141292 */
sqliteFindFunction(sqlite * db,const char * zName,int nName,int nArg,int createFlag)1633*4520Snw141292 FuncDef *sqliteFindFunction(
1634*4520Snw141292 sqlite *db, /* An open database */
1635*4520Snw141292 const char *zName, /* Name of the function. Not null-terminated */
1636*4520Snw141292 int nName, /* Number of characters in the name */
1637*4520Snw141292 int nArg, /* Number of arguments. -1 means any number */
1638*4520Snw141292 int createFlag /* Create new entry if true and does not otherwise exist */
1639*4520Snw141292 ){
1640*4520Snw141292 FuncDef *pFirst, *p, *pMaybe;
1641*4520Snw141292 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
1642*4520Snw141292 if( p && !createFlag && nArg<0 ){
1643*4520Snw141292 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1644*4520Snw141292 return p;
1645*4520Snw141292 }
1646*4520Snw141292 pMaybe = 0;
1647*4520Snw141292 while( p && p->nArg!=nArg ){
1648*4520Snw141292 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1649*4520Snw141292 p = p->pNext;
1650*4520Snw141292 }
1651*4520Snw141292 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1652*4520Snw141292 return 0;
1653*4520Snw141292 }
1654*4520Snw141292 if( p==0 && pMaybe ){
1655*4520Snw141292 assert( createFlag==0 );
1656*4520Snw141292 return pMaybe;
1657*4520Snw141292 }
1658*4520Snw141292 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
1659*4520Snw141292 p->nArg = nArg;
1660*4520Snw141292 p->pNext = pFirst;
1661*4520Snw141292 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
1662*4520Snw141292 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
1663*4520Snw141292 }
1664*4520Snw141292 return p;
1665*4520Snw141292 }
1666