blob: 8e5b41a600a68c76e3e90dbd7ab6d9e8fc0628e7 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** This module contains C code that generates VDBE code used to process
13** the WHERE clause of SQL statements. Also found here are subroutines
14** to generate VDBE code to evaluate expressions.
15**
drh6b563442001-11-07 16:48:26 +000016** $Id: where.c,v 1.25 2001/11/07 16:48:28 drh Exp $
drh75897232000-05-29 14:26:00 +000017*/
18#include "sqliteInt.h"
19
20/*
21** The query generator uses an array of instances of this structure to
22** help it analyze the subexpressions of the WHERE clause. Each WHERE
23** clause subexpression is separated from the others by an AND operator.
24*/
25typedef struct ExprInfo ExprInfo;
26struct ExprInfo {
27 Expr *p; /* Pointer to the subexpression */
28 int indexable; /* True if this subexprssion is usable by an index */
drh967e8b72000-06-21 13:59:10 +000029 int idxLeft; /* p->pLeft is a column in this table number. -1 if
30 ** p->pLeft is not the column of any table */
31 int idxRight; /* p->pRight is a column in this table number. -1 if
32 ** p->pRight is not the column of any table */
drh75897232000-05-29 14:26:00 +000033 unsigned prereqLeft; /* Tables referenced by p->pLeft */
34 unsigned prereqRight; /* Tables referenced by p->pRight */
35};
36
37/*
38** Determine the number of elements in an array.
39*/
40#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
41
42/*
43** This routine is used to divide the WHERE expression into subexpressions
44** separated by the AND operator.
45**
46** aSlot[] is an array of subexpressions structures.
47** There are nSlot spaces left in this array. This routine attempts to
48** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
49** The return value is the number of slots filled.
50*/
51static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
52 int cnt = 0;
53 if( pExpr==0 || nSlot<1 ) return 0;
54 if( nSlot==1 || pExpr->op!=TK_AND ){
55 aSlot[0].p = pExpr;
56 return 1;
57 }
58 if( pExpr->pLeft->op!=TK_AND ){
59 aSlot[0].p = pExpr->pLeft;
60 cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
61 }else{
62 cnt = exprSplit(nSlot, aSlot, pExpr->pRight);
63 cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pLeft);
64 }
65 return cnt;
66}
67
68/*
69** This routine walks (recursively) an expression tree and generates
70** a bitmask indicating which tables are used in that expression
71** tree. Bit 0 of the mask is set if table 0 is used. But 1 is set
72** if table 1 is used. And so forth.
73**
74** In order for this routine to work, the calling function must have
75** previously invoked sqliteExprResolveIds() on the expression. See
76** the header comment on that routine for additional information.
drh19a775c2000-06-05 18:54:46 +000077**
78** "base" is the cursor number (the value of the iTable field) that
79** corresponds to the first entry in the table list. This is the
80** same as pParse->nTab.
drh75897232000-05-29 14:26:00 +000081*/
drh19a775c2000-06-05 18:54:46 +000082static int exprTableUsage(int base, Expr *p){
drh75897232000-05-29 14:26:00 +000083 unsigned int mask = 0;
84 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +000085 if( p->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +000086 return 1<< (p->iTable - base);
drh75897232000-05-29 14:26:00 +000087 }
88 if( p->pRight ){
drh19a775c2000-06-05 18:54:46 +000089 mask = exprTableUsage(base, p->pRight);
drh75897232000-05-29 14:26:00 +000090 }
91 if( p->pLeft ){
drh19a775c2000-06-05 18:54:46 +000092 mask |= exprTableUsage(base, p->pLeft);
drh75897232000-05-29 14:26:00 +000093 }
94 return mask;
95}
96
97/*
98** The input to this routine is an ExprInfo structure with only the
99** "p" field filled in. The job of this routine is to analyze the
100** subexpression and populate all the other fields of the ExprInfo
101** structure.
drh19a775c2000-06-05 18:54:46 +0000102**
103** "base" is the cursor number (the value of the iTable field) that
drh80ff32f2001-11-04 18:32:46 +0000104** corresponds to the first entry in the table list. This is the
drh19a775c2000-06-05 18:54:46 +0000105** same as pParse->nTab.
drh75897232000-05-29 14:26:00 +0000106*/
drh19a775c2000-06-05 18:54:46 +0000107static void exprAnalyze(int base, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000108 Expr *pExpr = pInfo->p;
drh19a775c2000-06-05 18:54:46 +0000109 pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft);
110 pInfo->prereqRight = exprTableUsage(base, pExpr->pRight);
drh75897232000-05-29 14:26:00 +0000111 pInfo->indexable = 0;
112 pInfo->idxLeft = -1;
113 pInfo->idxRight = -1;
114 if( pExpr->op==TK_EQ && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drh967e8b72000-06-21 13:59:10 +0000115 if( pExpr->pRight->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000116 pInfo->idxRight = pExpr->pRight->iTable - base;
drh75897232000-05-29 14:26:00 +0000117 pInfo->indexable = 1;
118 }
drh967e8b72000-06-21 13:59:10 +0000119 if( pExpr->pLeft->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000120 pInfo->idxLeft = pExpr->pLeft->iTable - base;
drh75897232000-05-29 14:26:00 +0000121 pInfo->indexable = 1;
122 }
123 }
124}
125
126/*
127** Generating the beginning of the loop used for WHERE clause processing.
128** The return value is a pointer to an (opaque) structure that contains
129** information needed to terminate the loop. Later, the calling routine
130** should invoke sqliteWhereEnd() with the return value of this function
131** in order to complete the WHERE clause processing.
132**
133** If an error occurs, this routine returns NULL.
134*/
135WhereInfo *sqliteWhereBegin(
136 Parse *pParse, /* The parser context */
137 IdList *pTabList, /* A list of all tables */
138 Expr *pWhere, /* The WHERE clause */
139 int pushKey /* If TRUE, leave the table key on the stack */
140){
141 int i; /* Loop counter */
142 WhereInfo *pWInfo; /* Will become the return value of this function */
143 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
144 int brk, cont; /* Addresses used during code generation */
145 int *aOrder; /* Order in which pTabList entries are searched */
146 int nExpr; /* Number of subexpressions in the WHERE clause */
147 int loopMask; /* One bit set for each outer loop */
148 int haveKey; /* True if KEY is on the stack */
drh19a775c2000-06-05 18:54:46 +0000149 int base; /* First available index for OP_Open opcodes */
drh6b563442001-11-07 16:48:26 +0000150 int nCur; /* Next unused cursor number */
drhc4a3c772001-04-04 11:48:57 +0000151 int aDirect[32]; /* If TRUE, then index this table using ROWID */
drh75897232000-05-29 14:26:00 +0000152 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
153
drh6b563442001-11-07 16:48:26 +0000154 /* Allocate space for aOrder[] and aiMem[]. */
drh75897232000-05-29 14:26:00 +0000155 aOrder = sqliteMalloc( sizeof(int) * pTabList->nId );
156
157 /* Allocate and initialize the WhereInfo structure that will become the
158 ** return value.
159 */
drh6b563442001-11-07 16:48:26 +0000160 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nId*sizeof(WhereLevel) );
drhdaffd0e2001-04-11 14:28:42 +0000161 if( sqlite_malloc_failed ){
drh75897232000-05-29 14:26:00 +0000162 sqliteFree(aOrder);
drhdaffd0e2001-04-11 14:28:42 +0000163 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000164 return 0;
165 }
166 pWInfo->pParse = pParse;
167 pWInfo->pTabList = pTabList;
drh19a775c2000-06-05 18:54:46 +0000168 base = pWInfo->base = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000169 nCur = base + pTabList->nId;
drh75897232000-05-29 14:26:00 +0000170
171 /* Split the WHERE clause into as many as 32 separate subexpressions
172 ** where each subexpression is separated by an AND operator. Any additional
173 ** subexpressions are attached in the aExpr[32] and will not enter
174 ** into the query optimizer computations. 32 is chosen as the cutoff
175 ** since that is the number of bits in an integer that we use for an
176 ** expression-used mask.
177 */
178 memset(aExpr, 0, sizeof(aExpr));
179 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
180
181 /* Analyze all of the subexpressions.
182 */
183 for(i=0; i<nExpr; i++){
drh19a775c2000-06-05 18:54:46 +0000184 exprAnalyze(pParse->nTab, &aExpr[i]);
drh75897232000-05-29 14:26:00 +0000185 }
186
187 /* Figure out a good nesting order for the tables. aOrder[0] will
188 ** be the index in pTabList of the outermost table. aOrder[1] will
189 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
190 ** be the innermost loop.
191 **
drh7e391e12000-05-30 20:17:49 +0000192 ** Someday will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000193 ** for an effiecient query. But for now, just use whatever order the
194 ** tables appear in in the pTabList.
195 */
196 for(i=0; i<pTabList->nId; i++){
197 aOrder[i] = i;
198 }
199
200 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000201 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
202 ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner
drhc4a3c772001-04-04 11:48:57 +0000203 ** loop. If the expression uses only the ROWID field, then set
204 ** aDirect[i] to 1.
drh75897232000-05-29 14:26:00 +0000205 **
206 ** Actually, if there are more than 32 tables in the join, only the
207 ** first 32 tables are candidates for indices.
208 */
209 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000210 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000211 int j;
drh75897232000-05-29 14:26:00 +0000212 int idx = aOrder[i];
213 Table *pTab = pTabList->a[idx].pTab;
214 Index *pIdx;
215 Index *pBestIdx = 0;
216
drhc4a3c772001-04-04 11:48:57 +0000217 /* Check to see if there is an expression that uses only the
218 ** ROWID field of this table. If so, set aDirect[i] to 1.
219 ** If not, set aDirect[i] to 0.
220 */
221 aDirect[i] = 0;
222 for(j=0; j<nExpr; j++){
223 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
224 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
225 aDirect[i] = 1;
226 break;
227 }
228 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
229 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
230 aDirect[i] = 1;
231 break;
232 }
233 }
234 if( aDirect[i] ){
235 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000236 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000237 continue;
238 }
239
drh75897232000-05-29 14:26:00 +0000240 /* Do a search for usable indices. Leave pBestIdx pointing to
drh7e391e12000-05-30 20:17:49 +0000241 ** the most specific usable index.
drh75897232000-05-29 14:26:00 +0000242 **
243 ** "Most specific" means that pBestIdx is the usable index that
drh967e8b72000-06-21 13:59:10 +0000244 ** has the largest value for nColumn. A usable index is one for
245 ** which there are subexpressions to compute every column of the
drh75897232000-05-29 14:26:00 +0000246 ** index.
247 */
248 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh967e8b72000-06-21 13:59:10 +0000249 int columnMask = 0;
drh75897232000-05-29 14:26:00 +0000250
drh967e8b72000-06-21 13:59:10 +0000251 if( pIdx->nColumn>32 ) continue;
drh75897232000-05-29 14:26:00 +0000252 for(j=0; j<nExpr; j++){
253 if( aExpr[j].idxLeft==idx
254 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000255 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000256 int k;
drh967e8b72000-06-21 13:59:10 +0000257 for(k=0; k<pIdx->nColumn; k++){
258 if( pIdx->aiColumn[k]==iColumn ){
259 columnMask |= 1<<k;
drh75897232000-05-29 14:26:00 +0000260 break;
261 }
262 }
263 }
264 if( aExpr[j].idxRight==idx
265 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000266 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000267 int k;
drh967e8b72000-06-21 13:59:10 +0000268 for(k=0; k<pIdx->nColumn; k++){
269 if( pIdx->aiColumn[k]==iColumn ){
270 columnMask |= 1<<k;
drh75897232000-05-29 14:26:00 +0000271 break;
272 }
273 }
274 }
275 }
drh967e8b72000-06-21 13:59:10 +0000276 if( columnMask + 1 == (1<<pIdx->nColumn) ){
277 if( pBestIdx==0 || pBestIdx->nColumn<pIdx->nColumn ){
drh75897232000-05-29 14:26:00 +0000278 pBestIdx = pIdx;
279 }
280 }
281 }
drh6b563442001-11-07 16:48:26 +0000282 pWInfo->a[i].pIdx = pBestIdx;
drh7e391e12000-05-30 20:17:49 +0000283 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000284 if( pBestIdx ){
285 pWInfo->a[i].iCur = nCur++;
286 }
drh75897232000-05-29 14:26:00 +0000287 }
288
drh6b563442001-11-07 16:48:26 +0000289 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000290 */
291 for(i=0; i<pTabList->nId; i++){
drhf57b3392001-10-08 13:22:32 +0000292 int openOp;
293 Table *pTab;
294
295 pTab = pTabList->a[i].pTab;
296 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000297 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
298 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000299 if( i==0 && !pParse->schemaVerified &&
300 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000301 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000302 pParse->schemaVerified = 1;
303 }
drh6b563442001-11-07 16:48:26 +0000304 if( pWInfo->a[i].pIdx!=0 ){
305 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
306 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000307 }
308 }
309
310 /* Generate the code to do the search
311 */
drh75897232000-05-29 14:26:00 +0000312 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000313 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
drh75897232000-05-29 14:26:00 +0000314 for(i=0; i<pTabList->nId; i++){
315 int j, k;
316 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000317 int goDirect;
318 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000319 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000320
drh6b563442001-11-07 16:48:26 +0000321 if( i<ARRAYSIZE(aDirect) ){
322 pIdx = pLevel->pIdx;
drhc4a3c772001-04-04 11:48:57 +0000323 goDirect = aDirect[i];
324 }else{
325 pIdx = 0;
326 goDirect = 0;
327 }
328
329 if( goDirect ){
330 /* Case 1: We can directly reference a single row using the ROWID field.
331 */
332 cont = brk;
333 for(k=0; k<nExpr; k++){
334 if( aExpr[k].p==0 ) continue;
335 if( aExpr[k].idxLeft==idx
336 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
337 && aExpr[k].p->pLeft->iColumn<0
338 ){
339 sqliteExprCode(pParse, aExpr[k].p->pRight);
340 aExpr[k].p = 0;
341 break;
342 }
343 if( aExpr[k].idxRight==idx
344 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
345 && aExpr[k].p->pRight->iColumn<0
346 ){
347 sqliteExprCode(pParse, aExpr[k].p->pLeft);
348 aExpr[k].p = 0;
349 break;
350 }
351 }
drh99fcd712001-10-13 01:06:47 +0000352 sqliteVdbeAddOp(v, OP_AddImm, 0, 0);
drh6b563442001-11-07 16:48:26 +0000353 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
354 cont = pLevel->cont = brk;
drhc4a3c772001-04-04 11:48:57 +0000355 if( i==pTabList->nId-1 && pushKey ){
356 haveKey = 1;
357 }else{
drh99fcd712001-10-13 01:06:47 +0000358 sqliteVdbeAddOp(v, OP_NotFound, base+idx, brk);
drhc4a3c772001-04-04 11:48:57 +0000359 haveKey = 0;
360 }
drh6b563442001-11-07 16:48:26 +0000361 pLevel->op = OP_Noop;
drhc4a3c772001-04-04 11:48:57 +0000362 }else if( pIdx==0 ){
363 /* Case 2: There was no usable index. We must do a complete
drh75897232000-05-29 14:26:00 +0000364 ** scan of the table.
365 */
drh6b563442001-11-07 16:48:26 +0000366 int start;
367
368 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
369 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
370 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
371 start = sqliteVdbeCurrentAddr(v);
372 pLevel->op = OP_Next;
373 pLevel->p1 = base+idx;
374 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000375 haveKey = 0;
376 }else{
drhc4a3c772001-04-04 11:48:57 +0000377 /* Case 3: We do have a usable index in pIdx.
drh75897232000-05-29 14:26:00 +0000378 */
drh6b563442001-11-07 16:48:26 +0000379 int start;
drh967e8b72000-06-21 13:59:10 +0000380 for(j=0; j<pIdx->nColumn; j++){
drh75897232000-05-29 14:26:00 +0000381 for(k=0; k<nExpr; k++){
382 if( aExpr[k].p==0 ) continue;
383 if( aExpr[k].idxLeft==idx
384 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000385 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000386 ){
387 sqliteExprCode(pParse, aExpr[k].p->pRight);
388 aExpr[k].p = 0;
389 break;
390 }
391 if( aExpr[k].idxRight==idx
392 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000393 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000394 ){
395 sqliteExprCode(pParse, aExpr[k].p->pLeft);
396 aExpr[k].p = 0;
397 break;
398 }
399 }
400 }
drh6b563442001-11-07 16:48:26 +0000401 pLevel->iMem = pParse->nMem++;
402 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
403 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000404 sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nColumn, 0);
drh6b563442001-11-07 16:48:26 +0000405 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
406 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
407 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
408 sqliteVdbeAddOp(v, OP_IdxGT, pLevel->iCur, brk);
409 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drh75897232000-05-29 14:26:00 +0000410 if( i==pTabList->nId-1 && pushKey ){
411 haveKey = 1;
412 }else{
drh99fcd712001-10-13 01:06:47 +0000413 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000414 haveKey = 0;
415 }
drh6b563442001-11-07 16:48:26 +0000416 pLevel->op = OP_Next;
417 pLevel->p1 = pLevel->iCur;
418 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000419 }
420 loopMask |= 1<<idx;
421
422 /* Insert code to test every subexpression that can be completely
423 ** computed using the current set of tables.
424 */
425 for(j=0; j<nExpr; j++){
426 if( aExpr[j].p==0 ) continue;
427 if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue;
428 if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue;
429 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000430 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000431 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000432 }
433 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
434 aExpr[j].p = 0;
435 }
436 brk = cont;
437 }
438 pWInfo->iContinue = cont;
439 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000440 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000441 }
442 sqliteFree(aOrder);
443 return pWInfo;
444}
445
446/*
447** Generate the end of the WHERE loop.
448*/
449void sqliteWhereEnd(WhereInfo *pWInfo){
450 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000451 int i;
drh19a775c2000-06-05 18:54:46 +0000452 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000453 WhereLevel *pLevel;
drh19a775c2000-06-05 18:54:46 +0000454
drh6b563442001-11-07 16:48:26 +0000455 for(i=pWInfo->pTabList->nId-1; i>=0; i--){
456 pLevel = &pWInfo->a[i];
457 sqliteVdbeResolveLabel(v, pLevel->cont);
458 if( pLevel->op!=OP_Noop ){
459 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000460 }
drh6b563442001-11-07 16:48:26 +0000461 sqliteVdbeResolveLabel(v, pLevel->brk);
drh19a775c2000-06-05 18:54:46 +0000462 }
drh6b563442001-11-07 16:48:26 +0000463 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
464 for(i=0; i<pWInfo->pTabList->nId; i++){
465 pLevel = &pWInfo->a[i];
466 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
467 if( pLevel->pIdx!=0 ){
468 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
469 }
drh19a775c2000-06-05 18:54:46 +0000470 }
drh75897232000-05-29 14:26:00 +0000471 sqliteFree(pWInfo);
472 return;
473}