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