Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: third_party/sqlite/amalgamation/sqlite3.05.c

Issue 1636873003: Try for backport (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@zzsql_import3_10_2_websql_backport
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /************** Begin file trigger.c *****************************************/
2 /*
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
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.
10 **
11 *************************************************************************
12 ** This file contains the implementation for TRIGGERs
13 */
14 /* #include "sqliteInt.h" */
15
16 #ifndef SQLITE_OMIT_TRIGGER
17 /*
18 ** Delete a linked list of TriggerStep structures.
19 */
20 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerS tep){
21 while( pTriggerStep ){
22 TriggerStep * pTmp = pTriggerStep;
23 pTriggerStep = pTriggerStep->pNext;
24
25 sqlite3ExprDelete(db, pTmp->pWhere);
26 sqlite3ExprListDelete(db, pTmp->pExprList);
27 sqlite3SelectDelete(db, pTmp->pSelect);
28 sqlite3IdListDelete(db, pTmp->pIdList);
29
30 sqlite3DbFree(db, pTmp);
31 }
32 }
33
34 /*
35 ** Given table pTab, return a list of all the triggers attached to
36 ** the table. The list is connected by Trigger.pNext pointers.
37 **
38 ** All of the triggers on pTab that are in the same database as pTab
39 ** are already attached to pTab->pTrigger. But there might be additional
40 ** triggers on pTab in the TEMP schema. This routine prepends all
41 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
42 ** and returns the combined list.
43 **
44 ** To state it another way: This routine returns a list of all triggers
45 ** that fire off of pTab. The list will include any TEMP triggers on
46 ** pTab as well as the triggers lised in pTab->pTrigger.
47 */
48 SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
49 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
50 Trigger *pList = 0; /* List of triggers to return */
51
52 if( pParse->disableTriggers ){
53 return 0;
54 }
55
56 if( pTmpSchema!=pTab->pSchema ){
57 HashElem *p;
58 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
59 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
60 Trigger *pTrig = (Trigger *)sqliteHashData(p);
61 if( pTrig->pTabSchema==pTab->pSchema
62 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
63 ){
64 pTrig->pNext = (pList ? pList : pTab->pTrigger);
65 pList = pTrig;
66 }
67 }
68 }
69
70 return (pList ? pList : pTab->pTrigger);
71 }
72
73 /*
74 ** This is called by the parser when it sees a CREATE TRIGGER statement
75 ** up to the point of the BEGIN before the trigger actions. A Trigger
76 ** structure is generated based on the information available and stored
77 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
78 ** sqlite3FinishTrigger() function is called to complete the trigger
79 ** construction process.
80 */
81 SQLITE_PRIVATE void sqlite3BeginTrigger(
82 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
83 Token *pName1, /* The name of the trigger */
84 Token *pName2, /* The name of the trigger */
85 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
86 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
87 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
88 SrcList *pTableName,/* The name of the table/view the trigger applies to */
89 Expr *pWhen, /* WHEN clause */
90 int isTemp, /* True if the TEMPORARY keyword is present */
91 int noErr /* Suppress errors if the trigger already exists */
92 ){
93 Trigger *pTrigger = 0; /* The new trigger */
94 Table *pTab; /* Table that the trigger fires off of */
95 char *zName = 0; /* Name of the trigger */
96 sqlite3 *db = pParse->db; /* The database connection */
97 int iDb; /* The database to store the trigger in */
98 Token *pName; /* The unqualified db name */
99 DbFixer sFix; /* State vector for the DB fixer */
100 int iTabDb; /* Index of the database holding pTab */
101
102 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
103 assert( pName2!=0 );
104 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
105 assert( op>0 && op<0xff );
106 if( isTemp ){
107 /* If TEMP was specified, then the trigger name may not be qualified. */
108 if( pName2->n>0 ){
109 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
110 goto trigger_cleanup;
111 }
112 iDb = 1;
113 pName = pName1;
114 }else{
115 /* Figure out the db that the trigger will be created in */
116 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
117 if( iDb<0 ){
118 goto trigger_cleanup;
119 }
120 }
121 if( !pTableName || db->mallocFailed ){
122 goto trigger_cleanup;
123 }
124
125 /* A long-standing parser bug is that this syntax was allowed:
126 **
127 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
128 ** ^^^^^^^^
129 **
130 ** To maintain backwards compatibility, ignore the database
131 ** name on pTableName if we are reparsing out of SQLITE_MASTER.
132 */
133 if( db->init.busy && iDb!=1 ){
134 sqlite3DbFree(db, pTableName->a[0].zDatabase);
135 pTableName->a[0].zDatabase = 0;
136 }
137
138 /* If the trigger name was unqualified, and the table is a temp table,
139 ** then set iDb to 1 to create the trigger in the temporary database.
140 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
141 ** exist, the error is caught by the block below.
142 */
143 pTab = sqlite3SrcListLookup(pParse, pTableName);
144 if( db->init.busy==0 && pName2->n==0 && pTab
145 && pTab->pSchema==db->aDb[1].pSchema ){
146 iDb = 1;
147 }
148
149 /* Ensure the table name matches database name and that the table exists */
150 if( db->mallocFailed ) goto trigger_cleanup;
151 assert( pTableName->nSrc==1 );
152 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
153 if( sqlite3FixSrcList(&sFix, pTableName) ){
154 goto trigger_cleanup;
155 }
156 pTab = sqlite3SrcListLookup(pParse, pTableName);
157 if( !pTab ){
158 /* The table does not exist. */
159 if( db->init.iDb==1 ){
160 /* Ticket #3810.
161 ** Normally, whenever a table is dropped, all associated triggers are
162 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
163 ** and the table is dropped by a different database connection, the
164 ** trigger is not visible to the database connection that does the
165 ** drop so the trigger cannot be dropped. This results in an
166 ** "orphaned trigger" - a trigger whose associated table is missing.
167 */
168 db->init.orphanTrigger = 1;
169 }
170 goto trigger_cleanup;
171 }
172 if( IsVirtual(pTab) ){
173 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
174 goto trigger_cleanup;
175 }
176
177 /* Check that the trigger name is not reserved and that no trigger of the
178 ** specified name exists */
179 zName = sqlite3NameFromToken(db, pName);
180 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
181 goto trigger_cleanup;
182 }
183 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
184 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
185 if( !noErr ){
186 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
187 }else{
188 assert( !db->init.busy );
189 sqlite3CodeVerifySchema(pParse, iDb);
190 }
191 goto trigger_cleanup;
192 }
193
194 /* Do not create a trigger on a system table */
195 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
196 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
197 goto trigger_cleanup;
198 }
199
200 /* INSTEAD of triggers are only for views and views only support INSTEAD
201 ** of triggers.
202 */
203 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
204 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
205 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
206 goto trigger_cleanup;
207 }
208 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
209 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
210 " trigger on table: %S", pTableName, 0);
211 goto trigger_cleanup;
212 }
213 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
214
215 #ifndef SQLITE_OMIT_AUTHORIZATION
216 {
217 int code = SQLITE_CREATE_TRIGGER;
218 const char *zDb = db->aDb[iTabDb].zName;
219 const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
220 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
221 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
222 goto trigger_cleanup;
223 }
224 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
225 goto trigger_cleanup;
226 }
227 }
228 #endif
229
230 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
231 ** cannot appear on views. So we might as well translate every
232 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
233 ** elsewhere.
234 */
235 if (tr_tm == TK_INSTEAD){
236 tr_tm = TK_BEFORE;
237 }
238
239 /* Build the Trigger object */
240 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
241 if( pTrigger==0 ) goto trigger_cleanup;
242 pTrigger->zName = zName;
243 zName = 0;
244 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
245 pTrigger->pSchema = db->aDb[iDb].pSchema;
246 pTrigger->pTabSchema = pTab->pSchema;
247 pTrigger->op = (u8)op;
248 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
249 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
250 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
251 assert( pParse->pNewTrigger==0 );
252 pParse->pNewTrigger = pTrigger;
253
254 trigger_cleanup:
255 sqlite3DbFree(db, zName);
256 sqlite3SrcListDelete(db, pTableName);
257 sqlite3IdListDelete(db, pColumns);
258 sqlite3ExprDelete(db, pWhen);
259 if( !pParse->pNewTrigger ){
260 sqlite3DeleteTrigger(db, pTrigger);
261 }else{
262 assert( pParse->pNewTrigger==pTrigger );
263 }
264 }
265
266 /*
267 ** This routine is called after all of the trigger actions have been parsed
268 ** in order to complete the process of building the trigger.
269 */
270 SQLITE_PRIVATE void sqlite3FinishTrigger(
271 Parse *pParse, /* Parser context */
272 TriggerStep *pStepList, /* The triggered program */
273 Token *pAll /* Token that describes the complete CREATE TRIGGER */
274 ){
275 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
276 char *zName; /* Name of trigger */
277 sqlite3 *db = pParse->db; /* The database */
278 DbFixer sFix; /* Fixer object */
279 int iDb; /* Database containing the trigger */
280 Token nameToken; /* Trigger name for error reporting */
281
282 pParse->pNewTrigger = 0;
283 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
284 zName = pTrig->zName;
285 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
286 pTrig->step_list = pStepList;
287 while( pStepList ){
288 pStepList->pTrig = pTrig;
289 pStepList = pStepList->pNext;
290 }
291 nameToken.z = pTrig->zName;
292 nameToken.n = sqlite3Strlen30(nameToken.z);
293 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
294 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
295 || sqlite3FixExpr(&sFix, pTrig->pWhen)
296 ){
297 goto triggerfinish_cleanup;
298 }
299
300 /* if we are not initializing,
301 ** build the sqlite_master entry
302 */
303 if( !db->init.busy ){
304 Vdbe *v;
305 char *z;
306
307 /* Make an entry in the sqlite_master table */
308 v = sqlite3GetVdbe(pParse);
309 if( v==0 ) goto triggerfinish_cleanup;
310 sqlite3BeginWriteOperation(pParse, 0, iDb);
311 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
312 sqlite3NestedParse(pParse,
313 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
314 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
315 pTrig->table, z);
316 sqlite3DbFree(db, z);
317 sqlite3ChangeCookie(pParse, iDb);
318 sqlite3VdbeAddParseSchemaOp(v, iDb,
319 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
320 }
321
322 if( db->init.busy ){
323 Trigger *pLink = pTrig;
324 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
325 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
326 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
327 if( pTrig ){
328 db->mallocFailed = 1;
329 }else if( pLink->pSchema==pLink->pTabSchema ){
330 Table *pTab;
331 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
332 assert( pTab!=0 );
333 pLink->pNext = pTab->pTrigger;
334 pTab->pTrigger = pLink;
335 }
336 }
337
338 triggerfinish_cleanup:
339 sqlite3DeleteTrigger(db, pTrig);
340 assert( !pParse->pNewTrigger );
341 sqlite3DeleteTriggerStep(db, pStepList);
342 }
343
344 /*
345 ** Turn a SELECT statement (that the pSelect parameter points to) into
346 ** a trigger step. Return a pointer to a TriggerStep structure.
347 **
348 ** The parser calls this routine when it finds a SELECT statement in
349 ** body of a TRIGGER.
350 */
351 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelec t){
352 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
353 if( pTriggerStep==0 ) {
354 sqlite3SelectDelete(db, pSelect);
355 return 0;
356 }
357 pTriggerStep->op = TK_SELECT;
358 pTriggerStep->pSelect = pSelect;
359 pTriggerStep->orconf = OE_Default;
360 return pTriggerStep;
361 }
362
363 /*
364 ** Allocate space to hold a new trigger step. The allocated space
365 ** holds both the TriggerStep object and the TriggerStep.target.z string.
366 **
367 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
368 */
369 static TriggerStep *triggerStepAllocate(
370 sqlite3 *db, /* Database connection */
371 u8 op, /* Trigger opcode */
372 Token *pName /* The target name */
373 ){
374 TriggerStep *pTriggerStep;
375
376 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
377 if( pTriggerStep ){
378 char *z = (char*)&pTriggerStep[1];
379 memcpy(z, pName->z, pName->n);
380 sqlite3Dequote(z);
381 pTriggerStep->zTarget = z;
382 pTriggerStep->op = op;
383 }
384 return pTriggerStep;
385 }
386
387 /*
388 ** Build a trigger step out of an INSERT statement. Return a pointer
389 ** to the new trigger step.
390 **
391 ** The parser calls this routine when it sees an INSERT inside the
392 ** body of a trigger.
393 */
394 SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
395 sqlite3 *db, /* The database connection */
396 Token *pTableName, /* Name of the table into which we insert */
397 IdList *pColumn, /* List of columns in pTableName to insert into */
398 Select *pSelect, /* A SELECT statement that supplies values */
399 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
400 ){
401 TriggerStep *pTriggerStep;
402
403 assert(pSelect != 0 || db->mallocFailed);
404
405 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
406 if( pTriggerStep ){
407 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
408 pTriggerStep->pIdList = pColumn;
409 pTriggerStep->orconf = orconf;
410 }else{
411 sqlite3IdListDelete(db, pColumn);
412 }
413 sqlite3SelectDelete(db, pSelect);
414
415 return pTriggerStep;
416 }
417
418 /*
419 ** Construct a trigger step that implements an UPDATE statement and return
420 ** a pointer to that trigger step. The parser calls this routine when it
421 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
422 */
423 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
424 sqlite3 *db, /* The database connection */
425 Token *pTableName, /* Name of the table to be updated */
426 ExprList *pEList, /* The SET clause: list of column and new values */
427 Expr *pWhere, /* The WHERE clause */
428 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
429 ){
430 TriggerStep *pTriggerStep;
431
432 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
433 if( pTriggerStep ){
434 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
435 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
436 pTriggerStep->orconf = orconf;
437 }
438 sqlite3ExprListDelete(db, pEList);
439 sqlite3ExprDelete(db, pWhere);
440 return pTriggerStep;
441 }
442
443 /*
444 ** Construct a trigger step that implements a DELETE statement and return
445 ** a pointer to that trigger step. The parser calls this routine when it
446 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
447 */
448 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
449 sqlite3 *db, /* Database connection */
450 Token *pTableName, /* The table from which rows are deleted */
451 Expr *pWhere /* The WHERE clause */
452 ){
453 TriggerStep *pTriggerStep;
454
455 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
456 if( pTriggerStep ){
457 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
458 pTriggerStep->orconf = OE_Default;
459 }
460 sqlite3ExprDelete(db, pWhere);
461 return pTriggerStep;
462 }
463
464 /*
465 ** Recursively delete a Trigger structure
466 */
467 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
468 if( pTrigger==0 ) return;
469 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
470 sqlite3DbFree(db, pTrigger->zName);
471 sqlite3DbFree(db, pTrigger->table);
472 sqlite3ExprDelete(db, pTrigger->pWhen);
473 sqlite3IdListDelete(db, pTrigger->pColumns);
474 sqlite3DbFree(db, pTrigger);
475 }
476
477 /*
478 ** This function is called to drop a trigger from the database schema.
479 **
480 ** This may be called directly from the parser and therefore identifies
481 ** the trigger by name. The sqlite3DropTriggerPtr() routine does the
482 ** same job as this routine except it takes a pointer to the trigger
483 ** instead of the trigger name.
484 **/
485 SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr) {
486 Trigger *pTrigger = 0;
487 int i;
488 const char *zDb;
489 const char *zName;
490 sqlite3 *db = pParse->db;
491
492 if( db->mallocFailed ) goto drop_trigger_cleanup;
493 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
494 goto drop_trigger_cleanup;
495 }
496
497 assert( pName->nSrc==1 );
498 zDb = pName->a[0].zDatabase;
499 zName = pName->a[0].zName;
500 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
501 for(i=OMIT_TEMPDB; i<db->nDb; i++){
502 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
503 if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
504 assert( sqlite3SchemaMutexHeld(db, j, 0) );
505 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
506 if( pTrigger ) break;
507 }
508 if( !pTrigger ){
509 if( !noErr ){
510 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
511 }else{
512 sqlite3CodeVerifyNamedSchema(pParse, zDb);
513 }
514 pParse->checkSchema = 1;
515 goto drop_trigger_cleanup;
516 }
517 sqlite3DropTriggerPtr(pParse, pTrigger);
518
519 drop_trigger_cleanup:
520 sqlite3SrcListDelete(db, pName);
521 }
522
523 /*
524 ** Return a pointer to the Table structure for the table that a trigger
525 ** is set on.
526 */
527 static Table *tableOfTrigger(Trigger *pTrigger){
528 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
529 }
530
531
532 /*
533 ** Drop a trigger given a pointer to that trigger.
534 */
535 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
536 Table *pTable;
537 Vdbe *v;
538 sqlite3 *db = pParse->db;
539 int iDb;
540
541 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
542 assert( iDb>=0 && iDb<db->nDb );
543 pTable = tableOfTrigger(pTrigger);
544 assert( pTable );
545 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
546 #ifndef SQLITE_OMIT_AUTHORIZATION
547 {
548 int code = SQLITE_DROP_TRIGGER;
549 const char *zDb = db->aDb[iDb].zName;
550 const char *zTab = SCHEMA_TABLE(iDb);
551 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
552 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
553 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
554 return;
555 }
556 }
557 #endif
558
559 /* Generate code to destroy the database record of the trigger.
560 */
561 assert( pTable!=0 );
562 if( (v = sqlite3GetVdbe(pParse))!=0 ){
563 sqlite3NestedParse(pParse,
564 "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
565 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrigger->zName
566 );
567 sqlite3ChangeCookie(pParse, iDb);
568 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
569 }
570 }
571
572 /*
573 ** Remove a trigger from the hash tables of the sqlite* pointer.
574 */
575 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const ch ar *zName){
576 Trigger *pTrigger;
577 Hash *pHash;
578
579 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
580 pHash = &(db->aDb[iDb].pSchema->trigHash);
581 pTrigger = sqlite3HashInsert(pHash, zName, 0);
582 if( ALWAYS(pTrigger) ){
583 if( pTrigger->pSchema==pTrigger->pTabSchema ){
584 Table *pTab = tableOfTrigger(pTrigger);
585 Trigger **pp;
586 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
587 *pp = (*pp)->pNext;
588 }
589 sqlite3DeleteTrigger(db, pTrigger);
590 db->flags |= SQLITE_InternChanges;
591 }
592 }
593
594 /*
595 ** pEList is the SET clause of an UPDATE statement. Each entry
596 ** in pEList is of the format <id>=<expr>. If any of the entries
597 ** in pEList have an <id> which matches an identifier in pIdList,
598 ** then return TRUE. If pIdList==NULL, then it is considered a
599 ** wildcard that matches anything. Likewise if pEList==NULL then
600 ** it matches anything so always return true. Return false only
601 ** if there is no match.
602 */
603 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
604 int e;
605 if( pIdList==0 || NEVER(pEList==0) ) return 1;
606 for(e=0; e<pEList->nExpr; e++){
607 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
608 }
609 return 0;
610 }
611
612 /*
613 ** Return a list of all triggers on table pTab if there exists at least
614 ** one trigger that must be fired when an operation of type 'op' is
615 ** performed on the table, and, if that operation is an UPDATE, if at
616 ** least one of the columns in pChanges is being modified.
617 */
618 SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
619 Parse *pParse, /* Parse context */
620 Table *pTab, /* The table the contains the triggers */
621 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
622 ExprList *pChanges, /* Columns that change in an UPDATE statement */
623 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
624 ){
625 int mask = 0;
626 Trigger *pList = 0;
627 Trigger *p;
628
629 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
630 pList = sqlite3TriggerList(pParse, pTab);
631 }
632 assert( pList==0 || IsVirtual(pTab)==0 );
633 for(p=pList; p; p=p->pNext){
634 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
635 mask |= p->tr_tm;
636 }
637 }
638 if( pMask ){
639 *pMask = mask;
640 }
641 return (mask ? pList : 0);
642 }
643
644 /*
645 ** Convert the pStep->zTarget string into a SrcList and return a pointer
646 ** to that SrcList.
647 **
648 ** This routine adds a specific database name, if needed, to the target when
649 ** forming the SrcList. This prevents a trigger in one database from
650 ** referring to a target in another database. An exception is when the
651 ** trigger is in TEMP in which case it can refer to any other database it
652 ** wants.
653 */
654 static SrcList *targetSrcList(
655 Parse *pParse, /* The parsing context */
656 TriggerStep *pStep /* The trigger containing the target token */
657 ){
658 sqlite3 *db = pParse->db;
659 int iDb; /* Index of the database to use */
660 SrcList *pSrc; /* SrcList to be returned */
661
662 pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
663 if( pSrc ){
664 assert( pSrc->nSrc>0 );
665 pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
666 iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
667 if( iDb==0 || iDb>=2 ){
668 assert( iDb<db->nDb );
669 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
670 }
671 }
672 return pSrc;
673 }
674
675 /*
676 ** Generate VDBE code for the statements inside the body of a single
677 ** trigger.
678 */
679 static int codeTriggerProgram(
680 Parse *pParse, /* The parser context */
681 TriggerStep *pStepList, /* List of statements inside the trigger body */
682 int orconf /* Conflict algorithm. (OE_Abort, etc) */
683 ){
684 TriggerStep *pStep;
685 Vdbe *v = pParse->pVdbe;
686 sqlite3 *db = pParse->db;
687
688 assert( pParse->pTriggerTab && pParse->pToplevel );
689 assert( pStepList );
690 assert( v!=0 );
691 for(pStep=pStepList; pStep; pStep=pStep->pNext){
692 /* Figure out the ON CONFLICT policy that will be used for this step
693 ** of the trigger program. If the statement that caused this trigger
694 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
695 ** the ON CONFLICT policy that was specified as part of the trigger
696 ** step statement. Example:
697 **
698 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
699 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
700 ** END;
701 **
702 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
703 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
704 */
705 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
706 assert( pParse->okConstFactor==0 );
707
708 switch( pStep->op ){
709 case TK_UPDATE: {
710 sqlite3Update(pParse,
711 targetSrcList(pParse, pStep),
712 sqlite3ExprListDup(db, pStep->pExprList, 0),
713 sqlite3ExprDup(db, pStep->pWhere, 0),
714 pParse->eOrconf
715 );
716 break;
717 }
718 case TK_INSERT: {
719 sqlite3Insert(pParse,
720 targetSrcList(pParse, pStep),
721 sqlite3SelectDup(db, pStep->pSelect, 0),
722 sqlite3IdListDup(db, pStep->pIdList),
723 pParse->eOrconf
724 );
725 break;
726 }
727 case TK_DELETE: {
728 sqlite3DeleteFrom(pParse,
729 targetSrcList(pParse, pStep),
730 sqlite3ExprDup(db, pStep->pWhere, 0)
731 );
732 break;
733 }
734 default: assert( pStep->op==TK_SELECT ); {
735 SelectDest sDest;
736 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
737 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
738 sqlite3Select(pParse, pSelect, &sDest);
739 sqlite3SelectDelete(db, pSelect);
740 break;
741 }
742 }
743 if( pStep->op!=TK_SELECT ){
744 sqlite3VdbeAddOp0(v, OP_ResetCount);
745 }
746 }
747
748 return 0;
749 }
750
751 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
752 /*
753 ** This function is used to add VdbeComment() annotations to a VDBE
754 ** program. It is not used in production code, only for debugging.
755 */
756 static const char *onErrorText(int onError){
757 switch( onError ){
758 case OE_Abort: return "abort";
759 case OE_Rollback: return "rollback";
760 case OE_Fail: return "fail";
761 case OE_Replace: return "replace";
762 case OE_Ignore: return "ignore";
763 case OE_Default: return "default";
764 }
765 return "n/a";
766 }
767 #endif
768
769 /*
770 ** Parse context structure pFrom has just been used to create a sub-vdbe
771 ** (trigger program). If an error has occurred, transfer error information
772 ** from pFrom to pTo.
773 */
774 static void transferParseError(Parse *pTo, Parse *pFrom){
775 assert( pFrom->zErrMsg==0 || pFrom->nErr );
776 assert( pTo->zErrMsg==0 || pTo->nErr );
777 if( pTo->nErr==0 ){
778 pTo->zErrMsg = pFrom->zErrMsg;
779 pTo->nErr = pFrom->nErr;
780 pTo->rc = pFrom->rc;
781 }else{
782 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
783 }
784 }
785
786 /*
787 ** Create and populate a new TriggerPrg object with a sub-program
788 ** implementing trigger pTrigger with ON CONFLICT policy orconf.
789 */
790 static TriggerPrg *codeRowTrigger(
791 Parse *pParse, /* Current parse context */
792 Trigger *pTrigger, /* Trigger to code */
793 Table *pTab, /* The table pTrigger is attached to */
794 int orconf /* ON CONFLICT policy to code trigger program with */
795 ){
796 Parse *pTop = sqlite3ParseToplevel(pParse);
797 sqlite3 *db = pParse->db; /* Database handle */
798 TriggerPrg *pPrg; /* Value to return */
799 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
800 Vdbe *v; /* Temporary VM */
801 NameContext sNC; /* Name context for sub-vdbe */
802 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
803 Parse *pSubParse; /* Parse context for sub-vdbe */
804 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
805
806 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
807 assert( pTop->pVdbe );
808
809 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
810 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
811 ** list of the top-level Parse object sooner rather than later. */
812 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
813 if( !pPrg ) return 0;
814 pPrg->pNext = pTop->pTriggerPrg;
815 pTop->pTriggerPrg = pPrg;
816 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
817 if( !pProgram ) return 0;
818 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
819 pPrg->pTrigger = pTrigger;
820 pPrg->orconf = orconf;
821 pPrg->aColmask[0] = 0xffffffff;
822 pPrg->aColmask[1] = 0xffffffff;
823
824 /* Allocate and populate a new Parse context to use for coding the
825 ** trigger sub-program. */
826 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
827 if( !pSubParse ) return 0;
828 memset(&sNC, 0, sizeof(sNC));
829 sNC.pParse = pSubParse;
830 pSubParse->db = db;
831 pSubParse->pTriggerTab = pTab;
832 pSubParse->pToplevel = pTop;
833 pSubParse->zAuthContext = pTrigger->zName;
834 pSubParse->eTriggerOp = pTrigger->op;
835 pSubParse->nQueryLoop = pParse->nQueryLoop;
836
837 v = sqlite3GetVdbe(pSubParse);
838 if( v ){
839 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
840 pTrigger->zName, onErrorText(orconf),
841 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
842 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
843 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
844 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
845 pTab->zName
846 ));
847 #ifndef SQLITE_OMIT_TRACE
848 sqlite3VdbeChangeP4(v, -1,
849 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
850 );
851 #endif
852
853 /* If one was specified, code the WHEN clause. If it evaluates to false
854 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
855 ** OP_Halt inserted at the end of the program. */
856 if( pTrigger->pWhen ){
857 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
858 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
859 && db->mallocFailed==0
860 ){
861 iEndTrigger = sqlite3VdbeMakeLabel(v);
862 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
863 }
864 sqlite3ExprDelete(db, pWhen);
865 }
866
867 /* Code the trigger program into the sub-vdbe. */
868 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
869
870 /* Insert an OP_Halt at the end of the sub-program. */
871 if( iEndTrigger ){
872 sqlite3VdbeResolveLabel(v, iEndTrigger);
873 }
874 sqlite3VdbeAddOp0(v, OP_Halt);
875 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
876
877 transferParseError(pParse, pSubParse);
878 if( db->mallocFailed==0 ){
879 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
880 }
881 pProgram->nMem = pSubParse->nMem;
882 pProgram->nCsr = pSubParse->nTab;
883 pProgram->nOnce = pSubParse->nOnce;
884 pProgram->token = (void *)pTrigger;
885 pPrg->aColmask[0] = pSubParse->oldmask;
886 pPrg->aColmask[1] = pSubParse->newmask;
887 sqlite3VdbeDelete(v);
888 }
889
890 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
891 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
892 sqlite3ParserReset(pSubParse);
893 sqlite3StackFree(db, pSubParse);
894
895 return pPrg;
896 }
897
898 /*
899 ** Return a pointer to a TriggerPrg object containing the sub-program for
900 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
901 ** TriggerPrg object exists, a new object is allocated and populated before
902 ** being returned.
903 */
904 static TriggerPrg *getRowTrigger(
905 Parse *pParse, /* Current parse context */
906 Trigger *pTrigger, /* Trigger to code */
907 Table *pTab, /* The table trigger pTrigger is attached to */
908 int orconf /* ON CONFLICT algorithm. */
909 ){
910 Parse *pRoot = sqlite3ParseToplevel(pParse);
911 TriggerPrg *pPrg;
912
913 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
914
915 /* It may be that this trigger has already been coded (or is in the
916 ** process of being coded). If this is the case, then an entry with
917 ** a matching TriggerPrg.pTrigger field will be present somewhere
918 ** in the Parse.pTriggerPrg list. Search for such an entry. */
919 for(pPrg=pRoot->pTriggerPrg;
920 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
921 pPrg=pPrg->pNext
922 );
923
924 /* If an existing TriggerPrg could not be located, create a new one. */
925 if( !pPrg ){
926 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
927 }
928
929 return pPrg;
930 }
931
932 /*
933 ** Generate code for the trigger program associated with trigger p on
934 ** table pTab. The reg, orconf and ignoreJump parameters passed to this
935 ** function are the same as those described in the header function for
936 ** sqlite3CodeRowTrigger()
937 */
938 SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
939 Parse *pParse, /* Parse context */
940 Trigger *p, /* Trigger to code */
941 Table *pTab, /* The table to code triggers from */
942 int reg, /* Reg array containing OLD.* and NEW.* values */
943 int orconf, /* ON CONFLICT policy */
944 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
945 ){
946 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
947 TriggerPrg *pPrg;
948 pPrg = getRowTrigger(pParse, p, pTab, orconf);
949 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
950
951 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
952 ** is a pointer to the sub-vdbe containing the trigger program. */
953 if( pPrg ){
954 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
955
956 sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
957 sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
958 VdbeComment(
959 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
960
961 /* Set the P5 operand of the OP_Program instruction to non-zero if
962 ** recursive invocation of this trigger program is disallowed. Recursive
963 ** invocation is disallowed if (a) the sub-program is really a trigger,
964 ** not a foreign key action, and (b) the flag to enable recursive triggers
965 ** is clear. */
966 sqlite3VdbeChangeP5(v, (u8)bRecursive);
967 }
968 }
969
970 /*
971 ** This is called to code the required FOR EACH ROW triggers for an operation
972 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
973 ** is given by the op parameter. The tr_tm parameter determines whether the
974 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
975 ** parameter pChanges is passed the list of columns being modified.
976 **
977 ** If there are no triggers that fire at the specified time for the specified
978 ** operation on pTab, this function is a no-op.
979 **
980 ** The reg argument is the address of the first in an array of registers
981 ** that contain the values substituted for the new.* and old.* references
982 ** in the trigger program. If N is the number of columns in table pTab
983 ** (a copy of pTab->nCol), then registers are populated as follows:
984 **
985 ** Register Contains
986 ** ------------------------------------------------------
987 ** reg+0 OLD.rowid
988 ** reg+1 OLD.* value of left-most column of pTab
989 ** ... ...
990 ** reg+N OLD.* value of right-most column of pTab
991 ** reg+N+1 NEW.rowid
992 ** reg+N+2 OLD.* value of left-most column of pTab
993 ** ... ...
994 ** reg+N+N+1 NEW.* value of right-most column of pTab
995 **
996 ** For ON DELETE triggers, the registers containing the NEW.* values will
997 ** never be accessed by the trigger program, so they are not allocated or
998 ** populated by the caller (there is no data to populate them with anyway).
999 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1000 ** are never accessed, and so are not allocated by the caller. So, for an
1001 ** ON INSERT trigger, the value passed to this function as parameter reg
1002 ** is not a readable register, although registers (reg+N) through
1003 ** (reg+N+N+1) are.
1004 **
1005 ** Parameter orconf is the default conflict resolution algorithm for the
1006 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1007 ** is the instruction that control should jump to if a trigger program
1008 ** raises an IGNORE exception.
1009 */
1010 SQLITE_PRIVATE void sqlite3CodeRowTrigger(
1011 Parse *pParse, /* Parse context */
1012 Trigger *pTrigger, /* List of triggers on table pTab */
1013 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1014 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
1015 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
1016 Table *pTab, /* The table to code triggers from */
1017 int reg, /* The first in an array of registers (see above) */
1018 int orconf, /* ON CONFLICT policy */
1019 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
1020 ){
1021 Trigger *p; /* Used to iterate through pTrigger list */
1022
1023 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1024 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1025 assert( (op==TK_UPDATE)==(pChanges!=0) );
1026
1027 for(p=pTrigger; p; p=p->pNext){
1028
1029 /* Sanity checking: The schema for the trigger and for the table are
1030 ** always defined. The trigger must be in the same schema as the table
1031 ** or else it must be a TEMP trigger. */
1032 assert( p->pSchema!=0 );
1033 assert( p->pTabSchema!=0 );
1034 assert( p->pSchema==p->pTabSchema
1035 || p->pSchema==pParse->db->aDb[1].pSchema );
1036
1037 /* Determine whether we should code this trigger */
1038 if( p->op==op
1039 && p->tr_tm==tr_tm
1040 && checkColumnOverlap(p->pColumns, pChanges)
1041 ){
1042 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
1043 }
1044 }
1045 }
1046
1047 /*
1048 ** Triggers may access values stored in the old.* or new.* pseudo-table.
1049 ** This function returns a 32-bit bitmask indicating which columns of the
1050 ** old.* or new.* tables actually are used by triggers. This information
1051 ** may be used by the caller, for example, to avoid having to load the entire
1052 ** old.* record into memory when executing an UPDATE or DELETE command.
1053 **
1054 ** Bit 0 of the returned mask is set if the left-most column of the
1055 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
1056 ** the second leftmost column value is required, and so on. If there
1057 ** are more than 32 columns in the table, and at least one of the columns
1058 ** with an index greater than 32 may be accessed, 0xffffffff is returned.
1059 **
1060 ** It is not possible to determine if the old.rowid or new.rowid column is
1061 ** accessed by triggers. The caller must always assume that it is.
1062 **
1063 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1064 ** applies to the old.* table. If 1, the new.* table.
1065 **
1066 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1067 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1068 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1069 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1070 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
1071 */
1072 SQLITE_PRIVATE u32 sqlite3TriggerColmask(
1073 Parse *pParse, /* Parse context */
1074 Trigger *pTrigger, /* List of triggers on table pTab */
1075 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
1076 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
1077 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
1078 Table *pTab, /* The table to code triggers from */
1079 int orconf /* Default ON CONFLICT policy for trigger steps */
1080 ){
1081 const int op = pChanges ? TK_UPDATE : TK_DELETE;
1082 u32 mask = 0;
1083 Trigger *p;
1084
1085 assert( isNew==1 || isNew==0 );
1086 for(p=pTrigger; p; p=p->pNext){
1087 if( p->op==op && (tr_tm&p->tr_tm)
1088 && checkColumnOverlap(p->pColumns,pChanges)
1089 ){
1090 TriggerPrg *pPrg;
1091 pPrg = getRowTrigger(pParse, p, pTab, orconf);
1092 if( pPrg ){
1093 mask |= pPrg->aColmask[isNew];
1094 }
1095 }
1096 }
1097
1098 return mask;
1099 }
1100
1101 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
1102
1103 /************** End of trigger.c *********************************************/
1104 /************** Begin file update.c ******************************************/
1105 /*
1106 ** 2001 September 15
1107 **
1108 ** The author disclaims copyright to this source code. In place of
1109 ** a legal notice, here is a blessing:
1110 **
1111 ** May you do good and not evil.
1112 ** May you find forgiveness for yourself and forgive others.
1113 ** May you share freely, never taking more than you give.
1114 **
1115 *************************************************************************
1116 ** This file contains C code routines that are called by the parser
1117 ** to handle UPDATE statements.
1118 */
1119 /* #include "sqliteInt.h" */
1120
1121 #ifndef SQLITE_OMIT_VIRTUALTABLE
1122 /* Forward declaration */
1123 static void updateVirtualTable(
1124 Parse *pParse, /* The parsing context */
1125 SrcList *pSrc, /* The virtual table to be modified */
1126 Table *pTab, /* The virtual table */
1127 ExprList *pChanges, /* The columns to change in the UPDATE statement */
1128 Expr *pRowidExpr, /* Expression used to recompute the rowid */
1129 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
1130 Expr *pWhere, /* WHERE clause of the UPDATE statement */
1131 int onError /* ON CONFLICT strategy */
1132 );
1133 #endif /* SQLITE_OMIT_VIRTUALTABLE */
1134
1135 /*
1136 ** The most recently coded instruction was an OP_Column to retrieve the
1137 ** i-th column of table pTab. This routine sets the P4 parameter of the
1138 ** OP_Column to the default value, if any.
1139 **
1140 ** The default value of a column is specified by a DEFAULT clause in the
1141 ** column definition. This was either supplied by the user when the table
1142 ** was created, or added later to the table definition by an ALTER TABLE
1143 ** command. If the latter, then the row-records in the table btree on disk
1144 ** may not contain a value for the column and the default value, taken
1145 ** from the P4 parameter of the OP_Column instruction, is returned instead.
1146 ** If the former, then all row-records are guaranteed to include a value
1147 ** for the column and the P4 value is not required.
1148 **
1149 ** Column definitions created by an ALTER TABLE command may only have
1150 ** literal default values specified: a number, null or a string. (If a more
1151 ** complicated default expression value was provided, it is evaluated
1152 ** when the ALTER TABLE is executed and one of the literal values written
1153 ** into the sqlite_master table.)
1154 **
1155 ** Therefore, the P4 parameter is only required if the default value for
1156 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
1157 ** function is capable of transforming these types of expressions into
1158 ** sqlite3_value objects.
1159 **
1160 ** If parameter iReg is not negative, code an OP_RealAffinity instruction
1161 ** on register iReg. This is used when an equivalent integer value is
1162 ** stored in place of an 8-byte floating point value in order to save
1163 ** space.
1164 */
1165 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
1166 assert( pTab!=0 );
1167 if( !pTab->pSelect ){
1168 sqlite3_value *pValue = 0;
1169 u8 enc = ENC(sqlite3VdbeDb(v));
1170 Column *pCol = &pTab->aCol[i];
1171 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
1172 assert( i<pTab->nCol );
1173 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
1174 pCol->affinity, &pValue);
1175 if( pValue ){
1176 sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
1177 }
1178 #ifndef SQLITE_OMIT_FLOATING_POINT
1179 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
1180 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
1181 }
1182 #endif
1183 }
1184 }
1185
1186 /*
1187 ** Process an UPDATE statement.
1188 **
1189 ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
1190 ** \_______/ \________/ \______/ \________________/
1191 * onError pTabList pChanges pWhere
1192 */
1193 SQLITE_PRIVATE void sqlite3Update(
1194 Parse *pParse, /* The parser context */
1195 SrcList *pTabList, /* The table in which we should change things */
1196 ExprList *pChanges, /* Things to be changed */
1197 Expr *pWhere, /* The WHERE clause. May be null */
1198 int onError /* How to handle constraint errors */
1199 ){
1200 int i, j; /* Loop counters */
1201 Table *pTab; /* The table to be updated */
1202 int addrTop = 0; /* VDBE instruction address of the start of the loop */
1203 WhereInfo *pWInfo; /* Information about the WHERE clause */
1204 Vdbe *v; /* The virtual database engine */
1205 Index *pIdx; /* For looping over indices */
1206 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
1207 int nIdx; /* Number of indices that need updating */
1208 int iBaseCur; /* Base cursor number */
1209 int iDataCur; /* Cursor for the canonical data btree */
1210 int iIdxCur; /* Cursor for the first index */
1211 sqlite3 *db; /* The database structure */
1212 int *aRegIdx = 0; /* One register assigned to each index to be updated */
1213 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
1214 ** an expression for the i-th column of the table.
1215 ** aXRef[i]==-1 if the i-th column is not changed. */
1216 u8 *aToOpen; /* 1 for tables and indices to be opened */
1217 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
1218 u8 chngRowid; /* Rowid changed in a normal table */
1219 u8 chngKey; /* Either chngPk or chngRowid */
1220 Expr *pRowidExpr = 0; /* Expression defining the new record number */
1221 AuthContext sContext; /* The authorization context */
1222 NameContext sNC; /* The name-context to resolve expressions in */
1223 int iDb; /* Database containing the table being updated */
1224 int okOnePass; /* True for one-pass algorithm without the FIFO */
1225 int hasFK; /* True if foreign key processing is required */
1226 int labelBreak; /* Jump here to break out of UPDATE loop */
1227 int labelContinue; /* Jump here to continue next step of UPDATE loop */
1228
1229 #ifndef SQLITE_OMIT_TRIGGER
1230 int isView; /* True when updating a view (INSTEAD OF trigger) */
1231 Trigger *pTrigger; /* List of triggers on pTab, if required */
1232 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
1233 #endif
1234 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
1235 int iEph = 0; /* Ephemeral table holding all primary key values */
1236 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
1237 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
1238
1239 /* Register Allocations */
1240 int regRowCount = 0; /* A count of rows changed */
1241 int regOldRowid = 0; /* The old rowid */
1242 int regNewRowid = 0; /* The new rowid */
1243 int regNew = 0; /* Content of the NEW.* table in triggers */
1244 int regOld = 0; /* Content of OLD.* table in triggers */
1245 int regRowSet = 0; /* Rowset of rows to be updated */
1246 int regKey = 0; /* composite PRIMARY KEY value */
1247
1248 memset(&sContext, 0, sizeof(sContext));
1249 db = pParse->db;
1250 if( pParse->nErr || db->mallocFailed ){
1251 goto update_cleanup;
1252 }
1253 assert( pTabList->nSrc==1 );
1254
1255 /* Locate the table which we want to update.
1256 */
1257 pTab = sqlite3SrcListLookup(pParse, pTabList);
1258 if( pTab==0 ) goto update_cleanup;
1259 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1260
1261 /* Figure out if we have any triggers and if the table being
1262 ** updated is a view.
1263 */
1264 #ifndef SQLITE_OMIT_TRIGGER
1265 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
1266 isView = pTab->pSelect!=0;
1267 assert( pTrigger || tmask==0 );
1268 #else
1269 # define pTrigger 0
1270 # define isView 0
1271 # define tmask 0
1272 #endif
1273 #ifdef SQLITE_OMIT_VIEW
1274 # undef isView
1275 # define isView 0
1276 #endif
1277
1278 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
1279 goto update_cleanup;
1280 }
1281 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
1282 goto update_cleanup;
1283 }
1284
1285 /* Allocate a cursors for the main database table and for all indices.
1286 ** The index cursors might not be used, but if they are used they
1287 ** need to occur right after the database cursor. So go ahead and
1288 ** allocate enough space, just in case.
1289 */
1290 pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
1291 iIdxCur = iDataCur+1;
1292 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
1293 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
1294 if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){
1295 iDataCur = pParse->nTab;
1296 pTabList->a[0].iCursor = iDataCur;
1297 }
1298 pParse->nTab++;
1299 }
1300
1301 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
1302 ** Initialize aXRef[] and aToOpen[] to their default values.
1303 */
1304 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
1305 if( aXRef==0 ) goto update_cleanup;
1306 aRegIdx = aXRef+pTab->nCol;
1307 aToOpen = (u8*)(aRegIdx+nIdx);
1308 memset(aToOpen, 1, nIdx+1);
1309 aToOpen[nIdx+1] = 0;
1310 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
1311
1312 /* Initialize the name-context */
1313 memset(&sNC, 0, sizeof(sNC));
1314 sNC.pParse = pParse;
1315 sNC.pSrcList = pTabList;
1316
1317 /* Resolve the column names in all the expressions of the
1318 ** of the UPDATE statement. Also find the column index
1319 ** for each column to be updated in the pChanges array. For each
1320 ** column to be updated, make sure we have authorization to change
1321 ** that column.
1322 */
1323 chngRowid = chngPk = 0;
1324 for(i=0; i<pChanges->nExpr; i++){
1325 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
1326 goto update_cleanup;
1327 }
1328 for(j=0; j<pTab->nCol; j++){
1329 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
1330 if( j==pTab->iPKey ){
1331 chngRowid = 1;
1332 pRowidExpr = pChanges->a[i].pExpr;
1333 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
1334 chngPk = 1;
1335 }
1336 aXRef[j] = i;
1337 break;
1338 }
1339 }
1340 if( j>=pTab->nCol ){
1341 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
1342 j = -1;
1343 chngRowid = 1;
1344 pRowidExpr = pChanges->a[i].pExpr;
1345 }else{
1346 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
1347 pParse->checkSchema = 1;
1348 goto update_cleanup;
1349 }
1350 }
1351 #ifndef SQLITE_OMIT_AUTHORIZATION
1352 {
1353 int rc;
1354 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
1355 j<0 ? "ROWID" : pTab->aCol[j].zName,
1356 db->aDb[iDb].zName);
1357 if( rc==SQLITE_DENY ){
1358 goto update_cleanup;
1359 }else if( rc==SQLITE_IGNORE ){
1360 aXRef[j] = -1;
1361 }
1362 }
1363 #endif
1364 }
1365 assert( (chngRowid & chngPk)==0 );
1366 assert( chngRowid==0 || chngRowid==1 );
1367 assert( chngPk==0 || chngPk==1 );
1368 chngKey = chngRowid + chngPk;
1369
1370 /* The SET expressions are not actually used inside the WHERE loop.
1371 ** So reset the colUsed mask. Unless this is a virtual table. In that
1372 ** case, set all bits of the colUsed mask (to ensure that the virtual
1373 ** table implementation makes all columns available).
1374 */
1375 pTabList->a[0].colUsed = IsVirtual(pTab) ? (Bitmask)-1 : 0;
1376
1377 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
1378
1379 /* There is one entry in the aRegIdx[] array for each index on the table
1380 ** being updated. Fill in aRegIdx[] with a register number that will hold
1381 ** the key for accessing each index.
1382 **
1383 ** FIXME: Be smarter about omitting indexes that use expressions.
1384 */
1385 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1386 int reg;
1387 if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
1388 reg = ++pParse->nMem;
1389 }else{
1390 reg = 0;
1391 for(i=0; i<pIdx->nKeyCol; i++){
1392 i16 iIdxCol = pIdx->aiColumn[i];
1393 if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
1394 reg = ++pParse->nMem;
1395 break;
1396 }
1397 }
1398 }
1399 if( reg==0 ) aToOpen[j+1] = 0;
1400 aRegIdx[j] = reg;
1401 }
1402
1403 /* Begin generating code. */
1404 v = sqlite3GetVdbe(pParse);
1405 if( v==0 ) goto update_cleanup;
1406 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
1407 sqlite3BeginWriteOperation(pParse, 1, iDb);
1408
1409 /* Allocate required registers. */
1410 if( !IsVirtual(pTab) ){
1411 regRowSet = ++pParse->nMem;
1412 regOldRowid = regNewRowid = ++pParse->nMem;
1413 if( chngPk || pTrigger || hasFK ){
1414 regOld = pParse->nMem + 1;
1415 pParse->nMem += pTab->nCol;
1416 }
1417 if( chngKey || pTrigger || hasFK ){
1418 regNewRowid = ++pParse->nMem;
1419 }
1420 regNew = pParse->nMem + 1;
1421 pParse->nMem += pTab->nCol;
1422 }
1423
1424 /* Start the view context. */
1425 if( isView ){
1426 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
1427 }
1428
1429 /* If we are trying to update a view, realize that view into
1430 ** an ephemeral table.
1431 */
1432 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
1433 if( isView ){
1434 sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
1435 }
1436 #endif
1437
1438 /* Resolve the column names in all the expressions in the
1439 ** WHERE clause.
1440 */
1441 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
1442 goto update_cleanup;
1443 }
1444
1445 #ifndef SQLITE_OMIT_VIRTUALTABLE
1446 /* Virtual tables must be handled separately */
1447 if( IsVirtual(pTab) ){
1448 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
1449 pWhere, onError);
1450 goto update_cleanup;
1451 }
1452 #endif
1453
1454 /* Begin the database scan
1455 */
1456 if( HasRowid(pTab) ){
1457 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
1458 pWInfo = sqlite3WhereBegin(
1459 pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur
1460 );
1461 if( pWInfo==0 ) goto update_cleanup;
1462 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
1463
1464 /* Remember the rowid of every item to be updated.
1465 */
1466 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
1467 if( !okOnePass ){
1468 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
1469 }
1470
1471 /* End the database scan loop.
1472 */
1473 sqlite3WhereEnd(pWInfo);
1474 }else{
1475 int iPk; /* First of nPk memory cells holding PRIMARY KEY value */
1476 i16 nPk; /* Number of components of the PRIMARY KEY */
1477 int addrOpen; /* Address of the OpenEphemeral instruction */
1478
1479 assert( pPk!=0 );
1480 nPk = pPk->nKeyCol;
1481 iPk = pParse->nMem+1;
1482 pParse->nMem += nPk;
1483 regKey = ++pParse->nMem;
1484 iEph = pParse->nTab++;
1485 sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
1486 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
1487 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
1488 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
1489 WHERE_ONEPASS_DESIRED, iIdxCur);
1490 if( pWInfo==0 ) goto update_cleanup;
1491 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
1492 for(i=0; i<nPk; i++){
1493 assert( pPk->aiColumn[i]>=0 );
1494 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
1495 iPk+i);
1496 }
1497 if( okOnePass ){
1498 sqlite3VdbeChangeToNoop(v, addrOpen);
1499 nKey = nPk;
1500 regKey = iPk;
1501 }else{
1502 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
1503 sqlite3IndexAffinityStr(db, pPk), nPk);
1504 sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey);
1505 }
1506 sqlite3WhereEnd(pWInfo);
1507 }
1508
1509 /* Initialize the count of updated rows
1510 */
1511 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
1512 regRowCount = ++pParse->nMem;
1513 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
1514 }
1515
1516 labelBreak = sqlite3VdbeMakeLabel(v);
1517 if( !isView ){
1518 /*
1519 ** Open every index that needs updating. Note that if any
1520 ** index could potentially invoke a REPLACE conflict resolution
1521 ** action, then we need to open all indices because we might need
1522 ** to be deleting some records.
1523 */
1524 if( onError==OE_Replace ){
1525 memset(aToOpen, 1, nIdx+1);
1526 }else{
1527 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1528 if( pIdx->onError==OE_Replace ){
1529 memset(aToOpen, 1, nIdx+1);
1530 break;
1531 }
1532 }
1533 }
1534 if( okOnePass ){
1535 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
1536 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
1537 }
1538 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
1539 0, 0);
1540 }
1541
1542 /* Top of the update loop */
1543 if( okOnePass ){
1544 if( aToOpen[iDataCur-iBaseCur] && !isView ){
1545 assert( pPk );
1546 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
1547 VdbeCoverageNeverTaken(v);
1548 }
1549 labelContinue = labelBreak;
1550 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
1551 VdbeCoverageIf(v, pPk==0);
1552 VdbeCoverageIf(v, pPk!=0);
1553 }else if( pPk ){
1554 labelContinue = sqlite3VdbeMakeLabel(v);
1555 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
1556 addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
1557 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
1558 VdbeCoverage(v);
1559 }else{
1560 labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
1561 regOldRowid);
1562 VdbeCoverage(v);
1563 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
1564 VdbeCoverage(v);
1565 }
1566
1567 /* If the record number will change, set register regNewRowid to
1568 ** contain the new value. If the record number is not being modified,
1569 ** then regNewRowid is the same register as regOldRowid, which is
1570 ** already populated. */
1571 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
1572 if( chngRowid ){
1573 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
1574 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
1575 }
1576
1577 /* Compute the old pre-UPDATE content of the row being changed, if that
1578 ** information is needed */
1579 if( chngPk || hasFK || pTrigger ){
1580 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
1581 oldmask |= sqlite3TriggerColmask(pParse,
1582 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
1583 );
1584 for(i=0; i<pTab->nCol; i++){
1585 if( oldmask==0xffffffff
1586 || (i<32 && (oldmask & MASKBIT32(i))!=0)
1587 || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
1588 ){
1589 testcase( oldmask!=0xffffffff && i==31 );
1590 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
1591 }else{
1592 sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
1593 }
1594 }
1595 if( chngRowid==0 && pPk==0 ){
1596 sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
1597 }
1598 }
1599
1600 /* Populate the array of registers beginning at regNew with the new
1601 ** row data. This array is used to check constants, create the new
1602 ** table and index records, and as the values for any new.* references
1603 ** made by triggers.
1604 **
1605 ** If there are one or more BEFORE triggers, then do not populate the
1606 ** registers associated with columns that are (a) not modified by
1607 ** this UPDATE statement and (b) not accessed by new.* references. The
1608 ** values for registers not modified by the UPDATE must be reloaded from
1609 ** the database after the BEFORE triggers are fired anyway (as the trigger
1610 ** may have modified them). So not loading those that are not going to
1611 ** be used eliminates some redundant opcodes.
1612 */
1613 newmask = sqlite3TriggerColmask(
1614 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
1615 );
1616 for(i=0; i<pTab->nCol; i++){
1617 if( i==pTab->iPKey ){
1618 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
1619 }else{
1620 j = aXRef[i];
1621 if( j>=0 ){
1622 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
1623 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
1624 /* This branch loads the value of a column that will not be changed
1625 ** into a register. This is done if there are no BEFORE triggers, or
1626 ** if there are one or more BEFORE triggers that use this value via
1627 ** a new.* reference in a trigger program.
1628 */
1629 testcase( i==31 );
1630 testcase( i==32 );
1631 sqlite3ExprCodeGetColumnToReg(pParse, pTab, i, iDataCur, regNew+i);
1632 }else{
1633 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
1634 }
1635 }
1636 }
1637
1638 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
1639 ** verified. One could argue that this is wrong.
1640 */
1641 if( tmask&TRIGGER_BEFORE ){
1642 sqlite3TableAffinity(v, pTab, regNew);
1643 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
1644 TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
1645
1646 /* The row-trigger may have deleted the row being updated. In this
1647 ** case, jump to the next row. No updates or AFTER triggers are
1648 ** required. This behavior - what happens when the row being updated
1649 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
1650 ** documentation.
1651 */
1652 if( pPk ){
1653 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
1654 VdbeCoverage(v);
1655 }else{
1656 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
1657 VdbeCoverage(v);
1658 }
1659
1660 /* If it did not delete it, the row-trigger may still have modified
1661 ** some of the columns of the row being updated. Load the values for
1662 ** all columns not modified by the update statement into their
1663 ** registers in case this has happened.
1664 */
1665 for(i=0; i<pTab->nCol; i++){
1666 if( aXRef[i]<0 && i!=pTab->iPKey ){
1667 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
1668 }
1669 }
1670 }
1671
1672 if( !isView ){
1673 int addr1 = 0; /* Address of jump instruction */
1674 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
1675
1676 /* Do constraint checks. */
1677 assert( regOldRowid>0 );
1678 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
1679 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace);
1680
1681 /* Do FK constraint checks. */
1682 if( hasFK ){
1683 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
1684 }
1685
1686 /* Delete the index entries associated with the current record. */
1687 if( bReplace || chngKey ){
1688 if( pPk ){
1689 addr1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
1690 }else{
1691 addr1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
1692 }
1693 VdbeCoverageNeverTaken(v);
1694 }
1695 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
1696
1697 /* If changing the record number, delete the old record. */
1698 if( hasFK || chngKey || pPk!=0 ){
1699 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
1700 }
1701 if( bReplace || chngKey ){
1702 sqlite3VdbeJumpHere(v, addr1);
1703 }
1704
1705 if( hasFK ){
1706 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
1707 }
1708
1709 /* Insert the new index entries and the new record. */
1710 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
1711 regNewRowid, aRegIdx, 1, 0, 0);
1712
1713 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
1714 ** handle rows (possibly in other tables) that refer via a foreign key
1715 ** to the row just updated. */
1716 if( hasFK ){
1717 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
1718 }
1719 }
1720
1721 /* Increment the row counter
1722 */
1723 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
1724 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
1725 }
1726
1727 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
1728 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
1729
1730 /* Repeat the above with the next record to be updated, until
1731 ** all record selected by the WHERE clause have been updated.
1732 */
1733 if( okOnePass ){
1734 /* Nothing to do at end-of-loop for a single-pass */
1735 }else if( pPk ){
1736 sqlite3VdbeResolveLabel(v, labelContinue);
1737 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
1738 }else{
1739 sqlite3VdbeGoto(v, labelContinue);
1740 }
1741 sqlite3VdbeResolveLabel(v, labelBreak);
1742
1743 /* Close all tables */
1744 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1745 assert( aRegIdx );
1746 if( aToOpen[i+1] ){
1747 sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
1748 }
1749 }
1750 if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
1751
1752 /* Update the sqlite_sequence table by storing the content of the
1753 ** maximum rowid counter values recorded while inserting into
1754 ** autoincrement tables.
1755 */
1756 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
1757 sqlite3AutoincrementEnd(pParse);
1758 }
1759
1760 /*
1761 ** Return the number of rows that were changed. If this routine is
1762 ** generating code because of a call to sqlite3NestedParse(), do not
1763 ** invoke the callback function.
1764 */
1765 if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
1766 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
1767 sqlite3VdbeSetNumCols(v, 1);
1768 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
1769 }
1770
1771 update_cleanup:
1772 sqlite3AuthContextPop(&sContext);
1773 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
1774 sqlite3SrcListDelete(db, pTabList);
1775 sqlite3ExprListDelete(db, pChanges);
1776 sqlite3ExprDelete(db, pWhere);
1777 return;
1778 }
1779 /* Make sure "isView" and other macros defined above are undefined. Otherwise
1780 ** they may interfere with compilation of other functions in this file
1781 ** (or in another file, if this file becomes part of the amalgamation). */
1782 #ifdef isView
1783 #undef isView
1784 #endif
1785 #ifdef pTrigger
1786 #undef pTrigger
1787 #endif
1788
1789 #ifndef SQLITE_OMIT_VIRTUALTABLE
1790 /*
1791 ** Generate code for an UPDATE of a virtual table.
1792 **
1793 ** There are two possible strategies - the default and the special
1794 ** "onepass" strategy. Onepass is only used if the virtual table
1795 ** implementation indicates that pWhere may match at most one row.
1796 **
1797 ** The default strategy is to create an ephemeral table that contains
1798 ** for each row to be changed:
1799 **
1800 ** (A) The original rowid of that row.
1801 ** (B) The revised rowid for the row.
1802 ** (C) The content of every column in the row.
1803 **
1804 ** Then loop through the contents of this ephemeral table executing a
1805 ** VUpdate for each row. When finished, drop the ephemeral table.
1806 **
1807 ** The "onepass" strategy does not use an ephemeral table. Instead, it
1808 ** stores the same values (A, B and C above) in a register array and
1809 ** makes a single invocation of VUpdate.
1810 */
1811 static void updateVirtualTable(
1812 Parse *pParse, /* The parsing context */
1813 SrcList *pSrc, /* The virtual table to be modified */
1814 Table *pTab, /* The virtual table */
1815 ExprList *pChanges, /* The columns to change in the UPDATE statement */
1816 Expr *pRowid, /* Expression used to recompute the rowid */
1817 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
1818 Expr *pWhere, /* WHERE clause of the UPDATE statement */
1819 int onError /* ON CONFLICT strategy */
1820 ){
1821 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
1822 int ephemTab; /* Table holding the result of the SELECT */
1823 int i; /* Loop counter */
1824 sqlite3 *db = pParse->db; /* Database connection */
1825 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
1826 WhereInfo *pWInfo;
1827 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
1828 int regArg; /* First register in VUpdate arg array */
1829 int regRec; /* Register in which to assemble record */
1830 int regRowid; /* Register for ephem table rowid */
1831 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
1832 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
1833 int bOnePass; /* True to use onepass strategy */
1834 int addr; /* Address of OP_OpenEphemeral */
1835
1836 /* Allocate nArg registers to martial the arguments to VUpdate. Then
1837 ** create and open the ephemeral table in which the records created from
1838 ** these arguments will be temporarily stored. */
1839 assert( v );
1840 ephemTab = pParse->nTab++;
1841 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg);
1842 regArg = pParse->nMem + 1;
1843 pParse->nMem += nArg;
1844 regRec = ++pParse->nMem;
1845 regRowid = ++pParse->nMem;
1846
1847 /* Start scanning the virtual table */
1848 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0);
1849 if( pWInfo==0 ) return;
1850
1851 /* Populate the argument registers. */
1852 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
1853 if( pRowid ){
1854 sqlite3ExprCode(pParse, pRowid, regArg+1);
1855 }else{
1856 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1);
1857 }
1858 for(i=0; i<pTab->nCol; i++){
1859 if( aXRef[i]>=0 ){
1860 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
1861 }else{
1862 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
1863 }
1864 }
1865
1866 bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
1867
1868 if( bOnePass ){
1869 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
1870 ** above. Also, if this is a top-level parse (not a trigger), clear the
1871 ** multi-write flag so that the VM does not open a statement journal */
1872 sqlite3VdbeChangeToNoop(v, addr);
1873 if( sqlite3IsToplevel(pParse) ){
1874 pParse->isMultiWrite = 0;
1875 }
1876 }else{
1877 /* Create a record from the argument register contents and insert it into
1878 ** the ephemeral table. */
1879 sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
1880 sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
1881 sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
1882 }
1883
1884
1885 if( bOnePass==0 ){
1886 /* End the virtual table scan */
1887 sqlite3WhereEnd(pWInfo);
1888
1889 /* Begin scannning through the ephemeral table. */
1890 addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
1891
1892 /* Extract arguments from the current row of the ephemeral table and
1893 ** invoke the VUpdate method. */
1894 for(i=0; i<nArg; i++){
1895 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i);
1896 }
1897 }
1898 sqlite3VtabMakeWritable(pParse, pTab);
1899 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
1900 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
1901 sqlite3MayAbort(pParse);
1902
1903 /* End of the ephemeral table scan. Or, if using the onepass strategy,
1904 ** jump to here if the scan visited zero rows. */
1905 if( bOnePass==0 ){
1906 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
1907 sqlite3VdbeJumpHere(v, addr);
1908 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
1909 }else{
1910 sqlite3WhereEnd(pWInfo);
1911 }
1912 }
1913 #endif /* SQLITE_OMIT_VIRTUALTABLE */
1914
1915 /************** End of update.c **********************************************/
1916 /************** Begin file vacuum.c ******************************************/
1917 /*
1918 ** 2003 April 6
1919 **
1920 ** The author disclaims copyright to this source code. In place of
1921 ** a legal notice, here is a blessing:
1922 **
1923 ** May you do good and not evil.
1924 ** May you find forgiveness for yourself and forgive others.
1925 ** May you share freely, never taking more than you give.
1926 **
1927 *************************************************************************
1928 ** This file contains code used to implement the VACUUM command.
1929 **
1930 ** Most of the code in this file may be omitted by defining the
1931 ** SQLITE_OMIT_VACUUM macro.
1932 */
1933 /* #include "sqliteInt.h" */
1934 /* #include "vdbeInt.h" */
1935
1936 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
1937 /*
1938 ** Finalize a prepared statement. If there was an error, store the
1939 ** text of the error message in *pzErrMsg. Return the result code.
1940 */
1941 static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
1942 int rc;
1943 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
1944 if( rc ){
1945 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
1946 }
1947 return rc;
1948 }
1949
1950 /*
1951 ** Execute zSql on database db. Return an error code.
1952 */
1953 static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
1954 sqlite3_stmt *pStmt;
1955 VVA_ONLY( int rc; )
1956 if( !zSql ){
1957 return SQLITE_NOMEM;
1958 }
1959 if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
1960 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
1961 return sqlite3_errcode(db);
1962 }
1963 VVA_ONLY( rc = ) sqlite3_step(pStmt);
1964 assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
1965 return vacuumFinalize(db, pStmt, pzErrMsg);
1966 }
1967
1968 /*
1969 ** Execute zSql on database db. The statement returns exactly
1970 ** one column. Execute this as SQL on the same database.
1971 */
1972 static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
1973 sqlite3_stmt *pStmt;
1974 int rc;
1975
1976 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
1977 if( rc!=SQLITE_OK ) return rc;
1978
1979 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1980 rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
1981 if( rc!=SQLITE_OK ){
1982 vacuumFinalize(db, pStmt, pzErrMsg);
1983 return rc;
1984 }
1985 }
1986
1987 return vacuumFinalize(db, pStmt, pzErrMsg);
1988 }
1989
1990 /*
1991 ** The VACUUM command is used to clean up the database,
1992 ** collapse free space, etc. It is modelled after the VACUUM command
1993 ** in PostgreSQL. The VACUUM command works as follows:
1994 **
1995 ** (1) Create a new transient database file
1996 ** (2) Copy all content from the database being vacuumed into
1997 ** the new transient database file
1998 ** (3) Copy content from the transient database back into the
1999 ** original database.
2000 **
2001 ** The transient database requires temporary disk space approximately
2002 ** equal to the size of the original database. The copy operation of
2003 ** step (3) requires additional temporary disk space approximately equal
2004 ** to the size of the original database for the rollback journal.
2005 ** Hence, temporary disk space that is approximately 2x the size of the
2006 ** original database is required. Every page of the database is written
2007 ** approximately 3 times: Once for step (2) and twice for step (3).
2008 ** Two writes per page are required in step (3) because the original
2009 ** database content must be written into the rollback journal prior to
2010 ** overwriting the database with the vacuumed content.
2011 **
2012 ** Only 1x temporary space and only 1x writes would be required if
2013 ** the copy of step (3) were replaced by deleting the original database
2014 ** and renaming the transient database as the original. But that will
2015 ** not work if other processes are attached to the original database.
2016 ** And a power loss in between deleting the original and renaming the
2017 ** transient would cause the database file to appear to be deleted
2018 ** following reboot.
2019 */
2020 SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
2021 Vdbe *v = sqlite3GetVdbe(pParse);
2022 if( v ){
2023 sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
2024 sqlite3VdbeUsesBtree(v, 0);
2025 }
2026 return;
2027 }
2028
2029 /*
2030 ** This routine implements the OP_Vacuum opcode of the VDBE.
2031 */
2032 SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
2033 int rc = SQLITE_OK; /* Return code from service routines */
2034 Btree *pMain; /* The database being vacuumed */
2035 Btree *pTemp; /* The temporary database we vacuum into */
2036 char *zSql = 0; /* SQL statements */
2037 int saved_flags; /* Saved value of the db->flags */
2038 int saved_nChange; /* Saved value of db->nChange */
2039 int saved_nTotalChange; /* Saved value of db->nTotalChange */
2040 void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */
2041 Db *pDb = 0; /* Database to detach at end of vacuum */
2042 int isMemDb; /* True if vacuuming a :memory: database */
2043 int nRes; /* Bytes of reserved space at the end of each page */
2044 int nDb; /* Number of attached databases */
2045
2046 if( !db->autoCommit ){
2047 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
2048 return SQLITE_ERROR;
2049 }
2050 if( db->nVdbeActive>1 ){
2051 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
2052 return SQLITE_ERROR;
2053 }
2054
2055 /* Save the current value of the database flags so that it can be
2056 ** restored before returning. Then set the writable-schema flag, and
2057 ** disable CHECK and foreign key constraints. */
2058 saved_flags = db->flags;
2059 saved_nChange = db->nChange;
2060 saved_nTotalChange = db->nTotalChange;
2061 saved_xTrace = db->xTrace;
2062 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
2063 db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
2064 db->xTrace = 0;
2065
2066 pMain = db->aDb[0].pBt;
2067 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
2068
2069 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
2070 ** can be set to 'off' for this file, as it is not recovered if a crash
2071 ** occurs anyway. The integrity of the database is maintained by a
2072 ** (possibly synchronous) transaction opened on the main database before
2073 ** sqlite3BtreeCopyFile() is called.
2074 **
2075 ** An optimisation would be to use a non-journaled pager.
2076 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
2077 ** that actually made the VACUUM run slower. Very little journalling
2078 ** actually occurs when doing a vacuum since the vacuum_db is initially
2079 ** empty. Only the journal header is written. Apparently it takes more
2080 ** time to parse and run the PRAGMA to turn journalling off than it does
2081 ** to write the journal header file.
2082 */
2083 nDb = db->nDb;
2084 if( sqlite3TempInMemory(db) ){
2085 zSql = "ATTACH ':memory:' AS vacuum_db;";
2086 }else{
2087 zSql = "ATTACH '' AS vacuum_db;";
2088 }
2089 rc = execSql(db, pzErrMsg, zSql);
2090 if( db->nDb>nDb ){
2091 pDb = &db->aDb[db->nDb-1];
2092 assert( strcmp(pDb->zName,"vacuum_db")==0 );
2093 }
2094 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2095 pTemp = db->aDb[db->nDb-1].pBt;
2096
2097 /* The call to execSql() to attach the temp database has left the file
2098 ** locked (as there was more than one active statement when the transaction
2099 ** to read the schema was concluded. Unlock it here so that this doesn't
2100 ** cause problems for the call to BtreeSetPageSize() below. */
2101 sqlite3BtreeCommit(pTemp);
2102
2103 nRes = sqlite3BtreeGetOptimalReserve(pMain);
2104
2105 /* A VACUUM cannot change the pagesize of an encrypted database. */
2106 #ifdef SQLITE_HAS_CODEC
2107 if( db->nextPagesize ){
2108 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
2109 int nKey;
2110 char *zKey;
2111 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
2112 if( nKey ) db->nextPagesize = 0;
2113 }
2114 #endif
2115
2116 rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
2117 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2118
2119 /* Begin a transaction and take an exclusive lock on the main database
2120 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
2121 ** to ensure that we do not try to change the page-size on a WAL database.
2122 */
2123 rc = execSql(db, pzErrMsg, "BEGIN;");
2124 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2125 rc = sqlite3BtreeBeginTrans(pMain, 2);
2126 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2127
2128 /* Do not attempt to change the page size for a WAL database */
2129 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
2130 ==PAGER_JOURNALMODE_WAL ){
2131 db->nextPagesize = 0;
2132 }
2133
2134 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
2135 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
2136 || NEVER(db->mallocFailed)
2137 ){
2138 rc = SQLITE_NOMEM;
2139 goto end_of_vacuum;
2140 }
2141
2142 #ifndef SQLITE_OMIT_AUTOVACUUM
2143 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
2144 sqlite3BtreeGetAutoVacuum(pMain));
2145 #endif
2146
2147 /* Query the schema of the main database. Create a mirror schema
2148 ** in the temporary database.
2149 */
2150 rc = execExecSql(db, pzErrMsg,
2151 "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
2152 " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
2153 " AND coalesce(rootpage,1)>0"
2154 );
2155 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2156 rc = execExecSql(db, pzErrMsg,
2157 "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
2158 " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
2159 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2160 rc = execExecSql(db, pzErrMsg,
2161 "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
2162 " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
2163 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2164
2165 /* Loop through the tables in the main database. For each, do
2166 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
2167 ** the contents to the temporary database.
2168 */
2169 assert( (db->flags & SQLITE_Vacuum)==0 );
2170 db->flags |= SQLITE_Vacuum;
2171 rc = execExecSql(db, pzErrMsg,
2172 "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
2173 "|| ' SELECT * FROM main.' || quote(name) || ';'"
2174 "FROM main.sqlite_master "
2175 "WHERE type = 'table' AND name!='sqlite_sequence' "
2176 " AND coalesce(rootpage,1)>0"
2177 );
2178 assert( (db->flags & SQLITE_Vacuum)!=0 );
2179 db->flags &= ~SQLITE_Vacuum;
2180 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2181
2182 /* Copy over the sequence table
2183 */
2184 rc = execExecSql(db, pzErrMsg,
2185 "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
2186 "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
2187 );
2188 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2189 rc = execExecSql(db, pzErrMsg,
2190 "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
2191 "|| ' SELECT * FROM main.' || quote(name) || ';' "
2192 "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
2193 );
2194 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2195
2196
2197 /* Copy the triggers, views, and virtual tables from the main database
2198 ** over to the temporary database. None of these objects has any
2199 ** associated storage, so all we have to do is copy their entries
2200 ** from the SQLITE_MASTER table.
2201 */
2202 rc = execSql(db, pzErrMsg,
2203 "INSERT INTO vacuum_db.sqlite_master "
2204 " SELECT type, name, tbl_name, rootpage, sql"
2205 " FROM main.sqlite_master"
2206 " WHERE type='view' OR type='trigger'"
2207 " OR (type='table' AND rootpage=0)"
2208 );
2209 if( rc ) goto end_of_vacuum;
2210
2211 /* At this point, there is a write transaction open on both the
2212 ** vacuum database and the main database. Assuming no error occurs,
2213 ** both transactions are closed by this block - the main database
2214 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
2215 ** call to sqlite3BtreeCommit().
2216 */
2217 {
2218 u32 meta;
2219 int i;
2220
2221 /* This array determines which meta meta values are preserved in the
2222 ** vacuum. Even entries are the meta value number and odd entries
2223 ** are an increment to apply to the meta value after the vacuum.
2224 ** The increment is used to increase the schema cookie so that other
2225 ** connections to the same database will know to reread the schema.
2226 */
2227 static const unsigned char aCopy[] = {
2228 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
2229 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
2230 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
2231 BTREE_USER_VERSION, 0, /* Preserve the user version */
2232 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
2233 };
2234
2235 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
2236 assert( 1==sqlite3BtreeIsInTrans(pMain) );
2237
2238 /* Copy Btree meta values */
2239 for(i=0; i<ArraySize(aCopy); i+=2){
2240 /* GetMeta() and UpdateMeta() cannot fail in this context because
2241 ** we already have page 1 loaded into cache and marked dirty. */
2242 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
2243 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
2244 if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
2245 }
2246
2247 rc = sqlite3BtreeCopyFile(pMain, pTemp);
2248 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2249 rc = sqlite3BtreeCommit(pTemp);
2250 if( rc!=SQLITE_OK ) goto end_of_vacuum;
2251 #ifndef SQLITE_OMIT_AUTOVACUUM
2252 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
2253 #endif
2254 }
2255
2256 assert( rc==SQLITE_OK );
2257 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
2258
2259 end_of_vacuum:
2260 /* Restore the original value of db->flags */
2261 db->flags = saved_flags;
2262 db->nChange = saved_nChange;
2263 db->nTotalChange = saved_nTotalChange;
2264 db->xTrace = saved_xTrace;
2265 sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
2266
2267 /* Currently there is an SQL level transaction open on the vacuum
2268 ** database. No locks are held on any other files (since the main file
2269 ** was committed at the btree level). So it safe to end the transaction
2270 ** by manually setting the autoCommit flag to true and detaching the
2271 ** vacuum database. The vacuum_db journal file is deleted when the pager
2272 ** is closed by the DETACH.
2273 */
2274 db->autoCommit = 1;
2275
2276 if( pDb ){
2277 sqlite3BtreeClose(pDb->pBt);
2278 pDb->pBt = 0;
2279 pDb->pSchema = 0;
2280 }
2281
2282 /* This both clears the schemas and reduces the size of the db->aDb[]
2283 ** array. */
2284 sqlite3ResetAllSchemasOfConnection(db);
2285
2286 return rc;
2287 }
2288
2289 #endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
2290
2291 /************** End of vacuum.c **********************************************/
2292 /************** Begin file vtab.c ********************************************/
2293 /*
2294 ** 2006 June 10
2295 **
2296 ** The author disclaims copyright to this source code. In place of
2297 ** a legal notice, here is a blessing:
2298 **
2299 ** May you do good and not evil.
2300 ** May you find forgiveness for yourself and forgive others.
2301 ** May you share freely, never taking more than you give.
2302 **
2303 *************************************************************************
2304 ** This file contains code used to help implement virtual tables.
2305 */
2306 #ifndef SQLITE_OMIT_VIRTUALTABLE
2307 /* #include "sqliteInt.h" */
2308
2309 /*
2310 ** Before a virtual table xCreate() or xConnect() method is invoked, the
2311 ** sqlite3.pVtabCtx member variable is set to point to an instance of
2312 ** this struct allocated on the stack. It is used by the implementation of
2313 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
2314 ** are invoked only from within xCreate and xConnect methods.
2315 */
2316 struct VtabCtx {
2317 VTable *pVTable; /* The virtual table being constructed */
2318 Table *pTab; /* The Table object to which the virtual table belongs */
2319 VtabCtx *pPrior; /* Parent context (if any) */
2320 int bDeclared; /* True after sqlite3_declare_vtab() is called */
2321 };
2322
2323 /*
2324 ** The actual function that does the work of creating a new module.
2325 ** This function implements the sqlite3_create_module() and
2326 ** sqlite3_create_module_v2() interfaces.
2327 */
2328 static int createModule(
2329 sqlite3 *db, /* Database in which module is registered */
2330 const char *zName, /* Name assigned to this module */
2331 const sqlite3_module *pModule, /* The definition of the module */
2332 void *pAux, /* Context pointer for xCreate/xConnect */
2333 void (*xDestroy)(void *) /* Module destructor function */
2334 ){
2335 int rc = SQLITE_OK;
2336 int nName;
2337
2338 sqlite3_mutex_enter(db->mutex);
2339 nName = sqlite3Strlen30(zName);
2340 if( sqlite3HashFind(&db->aModule, zName) ){
2341 rc = SQLITE_MISUSE_BKPT;
2342 }else{
2343 Module *pMod;
2344 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
2345 if( pMod ){
2346 Module *pDel;
2347 char *zCopy = (char *)(&pMod[1]);
2348 memcpy(zCopy, zName, nName+1);
2349 pMod->zName = zCopy;
2350 pMod->pModule = pModule;
2351 pMod->pAux = pAux;
2352 pMod->xDestroy = xDestroy;
2353 pMod->pEpoTab = 0;
2354 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
2355 assert( pDel==0 || pDel==pMod );
2356 if( pDel ){
2357 db->mallocFailed = 1;
2358 sqlite3DbFree(db, pDel);
2359 }
2360 }
2361 }
2362 rc = sqlite3ApiExit(db, rc);
2363 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
2364
2365 sqlite3_mutex_leave(db->mutex);
2366 return rc;
2367 }
2368
2369
2370 /*
2371 ** External API function used to create a new virtual-table module.
2372 */
2373 SQLITE_API int SQLITE_STDCALL sqlite3_create_module(
2374 sqlite3 *db, /* Database in which module is registered */
2375 const char *zName, /* Name assigned to this module */
2376 const sqlite3_module *pModule, /* The definition of the module */
2377 void *pAux /* Context pointer for xCreate/xConnect */
2378 ){
2379 #ifdef SQLITE_ENABLE_API_ARMOR
2380 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
2381 #endif
2382 return createModule(db, zName, pModule, pAux, 0);
2383 }
2384
2385 /*
2386 ** External API function used to create a new virtual-table module.
2387 */
2388 SQLITE_API int SQLITE_STDCALL sqlite3_create_module_v2(
2389 sqlite3 *db, /* Database in which module is registered */
2390 const char *zName, /* Name assigned to this module */
2391 const sqlite3_module *pModule, /* The definition of the module */
2392 void *pAux, /* Context pointer for xCreate/xConnect */
2393 void (*xDestroy)(void *) /* Module destructor function */
2394 ){
2395 #ifdef SQLITE_ENABLE_API_ARMOR
2396 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
2397 #endif
2398 return createModule(db, zName, pModule, pAux, xDestroy);
2399 }
2400
2401 /*
2402 ** Lock the virtual table so that it cannot be disconnected.
2403 ** Locks nest. Every lock should have a corresponding unlock.
2404 ** If an unlock is omitted, resources leaks will occur.
2405 **
2406 ** If a disconnect is attempted while a virtual table is locked,
2407 ** the disconnect is deferred until all locks have been removed.
2408 */
2409 SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
2410 pVTab->nRef++;
2411 }
2412
2413
2414 /*
2415 ** pTab is a pointer to a Table structure representing a virtual-table.
2416 ** Return a pointer to the VTable object used by connection db to access
2417 ** this virtual-table, if one has been created, or NULL otherwise.
2418 */
2419 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
2420 VTable *pVtab;
2421 assert( IsVirtual(pTab) );
2422 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
2423 return pVtab;
2424 }
2425
2426 /*
2427 ** Decrement the ref-count on a virtual table object. When the ref-count
2428 ** reaches zero, call the xDisconnect() method to delete the object.
2429 */
2430 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
2431 sqlite3 *db = pVTab->db;
2432
2433 assert( db );
2434 assert( pVTab->nRef>0 );
2435 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
2436
2437 pVTab->nRef--;
2438 if( pVTab->nRef==0 ){
2439 sqlite3_vtab *p = pVTab->pVtab;
2440 if( p ){
2441 p->pModule->xDisconnect(p);
2442 }
2443 sqlite3DbFree(db, pVTab);
2444 }
2445 }
2446
2447 /*
2448 ** Table p is a virtual table. This function moves all elements in the
2449 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
2450 ** database connections to be disconnected at the next opportunity.
2451 ** Except, if argument db is not NULL, then the entry associated with
2452 ** connection db is left in the p->pVTable list.
2453 */
2454 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
2455 VTable *pRet = 0;
2456 VTable *pVTable = p->pVTable;
2457 p->pVTable = 0;
2458
2459 /* Assert that the mutex (if any) associated with the BtShared database
2460 ** that contains table p is held by the caller. See header comments
2461 ** above function sqlite3VtabUnlockList() for an explanation of why
2462 ** this makes it safe to access the sqlite3.pDisconnect list of any
2463 ** database connection that may have an entry in the p->pVTable list.
2464 */
2465 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
2466
2467 while( pVTable ){
2468 sqlite3 *db2 = pVTable->db;
2469 VTable *pNext = pVTable->pNext;
2470 assert( db2 );
2471 if( db2==db ){
2472 pRet = pVTable;
2473 p->pVTable = pRet;
2474 pRet->pNext = 0;
2475 }else{
2476 pVTable->pNext = db2->pDisconnect;
2477 db2->pDisconnect = pVTable;
2478 }
2479 pVTable = pNext;
2480 }
2481
2482 assert( !db || pRet );
2483 return pRet;
2484 }
2485
2486 /*
2487 ** Table *p is a virtual table. This function removes the VTable object
2488 ** for table *p associated with database connection db from the linked
2489 ** list in p->pVTab. It also decrements the VTable ref count. This is
2490 ** used when closing database connection db to free all of its VTable
2491 ** objects without disturbing the rest of the Schema object (which may
2492 ** be being used by other shared-cache connections).
2493 */
2494 SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
2495 VTable **ppVTab;
2496
2497 assert( IsVirtual(p) );
2498 assert( sqlite3BtreeHoldsAllMutexes(db) );
2499 assert( sqlite3_mutex_held(db->mutex) );
2500
2501 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
2502 if( (*ppVTab)->db==db ){
2503 VTable *pVTab = *ppVTab;
2504 *ppVTab = pVTab->pNext;
2505 sqlite3VtabUnlock(pVTab);
2506 break;
2507 }
2508 }
2509 }
2510
2511
2512 /*
2513 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
2514 **
2515 ** This function may only be called when the mutexes associated with all
2516 ** shared b-tree databases opened using connection db are held by the
2517 ** caller. This is done to protect the sqlite3.pDisconnect list. The
2518 ** sqlite3.pDisconnect list is accessed only as follows:
2519 **
2520 ** 1) By this function. In this case, all BtShared mutexes and the mutex
2521 ** associated with the database handle itself must be held.
2522 **
2523 ** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
2524 ** the sqlite3.pDisconnect list. In this case either the BtShared mutex
2525 ** associated with the database the virtual table is stored in is held
2526 ** or, if the virtual table is stored in a non-sharable database, then
2527 ** the database handle mutex is held.
2528 **
2529 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
2530 ** by multiple threads. It is thread-safe.
2531 */
2532 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
2533 VTable *p = db->pDisconnect;
2534 db->pDisconnect = 0;
2535
2536 assert( sqlite3BtreeHoldsAllMutexes(db) );
2537 assert( sqlite3_mutex_held(db->mutex) );
2538
2539 if( p ){
2540 sqlite3ExpirePreparedStatements(db);
2541 do {
2542 VTable *pNext = p->pNext;
2543 sqlite3VtabUnlock(p);
2544 p = pNext;
2545 }while( p );
2546 }
2547 }
2548
2549 /*
2550 ** Clear any and all virtual-table information from the Table record.
2551 ** This routine is called, for example, just before deleting the Table
2552 ** record.
2553 **
2554 ** Since it is a virtual-table, the Table structure contains a pointer
2555 ** to the head of a linked list of VTable structures. Each VTable
2556 ** structure is associated with a single sqlite3* user of the schema.
2557 ** The reference count of the VTable structure associated with database
2558 ** connection db is decremented immediately (which may lead to the
2559 ** structure being xDisconnected and free). Any other VTable structures
2560 ** in the list are moved to the sqlite3.pDisconnect list of the associated
2561 ** database connection.
2562 */
2563 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
2564 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
2565 if( p->azModuleArg ){
2566 int i;
2567 for(i=0; i<p->nModuleArg; i++){
2568 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
2569 }
2570 sqlite3DbFree(db, p->azModuleArg);
2571 }
2572 }
2573
2574 /*
2575 ** Add a new module argument to pTable->azModuleArg[].
2576 ** The string is not copied - the pointer is stored. The
2577 ** string will be freed automatically when the table is
2578 ** deleted.
2579 */
2580 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
2581 int nBytes = sizeof(char *)*(2+pTable->nModuleArg);
2582 char **azModuleArg;
2583 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
2584 if( azModuleArg==0 ){
2585 sqlite3DbFree(db, zArg);
2586 }else{
2587 int i = pTable->nModuleArg++;
2588 azModuleArg[i] = zArg;
2589 azModuleArg[i+1] = 0;
2590 pTable->azModuleArg = azModuleArg;
2591 }
2592 }
2593
2594 /*
2595 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
2596 ** statement. The module name has been parsed, but the optional list
2597 ** of parameters that follow the module name are still pending.
2598 */
2599 SQLITE_PRIVATE void sqlite3VtabBeginParse(
2600 Parse *pParse, /* Parsing context */
2601 Token *pName1, /* Name of new table, or database name */
2602 Token *pName2, /* Name of new table or NULL */
2603 Token *pModuleName, /* Name of the module for the virtual table */
2604 int ifNotExists /* No error if the table already exists */
2605 ){
2606 int iDb; /* The database the table is being created in */
2607 Table *pTable; /* The new virtual table */
2608 sqlite3 *db; /* Database connection */
2609
2610 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
2611 pTable = pParse->pNewTable;
2612 if( pTable==0 ) return;
2613 assert( 0==pTable->pIndex );
2614
2615 db = pParse->db;
2616 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
2617 assert( iDb>=0 );
2618
2619 pTable->tabFlags |= TF_Virtual;
2620 pTable->nModuleArg = 0;
2621 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
2622 addModuleArgument(db, pTable, 0);
2623 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
2624 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
2625 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
2626 );
2627 pParse->sNameToken.n = (int)(
2628 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
2629 );
2630
2631 #ifndef SQLITE_OMIT_AUTHORIZATION
2632 /* Creating a virtual table invokes the authorization callback twice.
2633 ** The first invocation, to obtain permission to INSERT a row into the
2634 ** sqlite_master table, has already been made by sqlite3StartTable().
2635 ** The second call, to obtain permission to create the table, is made now.
2636 */
2637 if( pTable->azModuleArg ){
2638 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
2639 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
2640 }
2641 #endif
2642 }
2643
2644 /*
2645 ** This routine takes the module argument that has been accumulating
2646 ** in pParse->zArg[] and appends it to the list of arguments on the
2647 ** virtual table currently under construction in pParse->pTable.
2648 */
2649 static void addArgumentToVtab(Parse *pParse){
2650 if( pParse->sArg.z && pParse->pNewTable ){
2651 const char *z = (const char*)pParse->sArg.z;
2652 int n = pParse->sArg.n;
2653 sqlite3 *db = pParse->db;
2654 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
2655 }
2656 }
2657
2658 /*
2659 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
2660 ** has been completely parsed.
2661 */
2662 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
2663 Table *pTab = pParse->pNewTable; /* The table being constructed */
2664 sqlite3 *db = pParse->db; /* The database connection */
2665
2666 if( pTab==0 ) return;
2667 addArgumentToVtab(pParse);
2668 pParse->sArg.z = 0;
2669 if( pTab->nModuleArg<1 ) return;
2670
2671 /* If the CREATE VIRTUAL TABLE statement is being entered for the
2672 ** first time (in other words if the virtual table is actually being
2673 ** created now instead of just being read out of sqlite_master) then
2674 ** do additional initialization work and store the statement text
2675 ** in the sqlite_master table.
2676 */
2677 if( !db->init.busy ){
2678 char *zStmt;
2679 char *zWhere;
2680 int iDb;
2681 int iReg;
2682 Vdbe *v;
2683
2684 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
2685 if( pEnd ){
2686 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
2687 }
2688 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
2689
2690 /* A slot for the record has already been allocated in the
2691 ** SQLITE_MASTER table. We just need to update that slot with all
2692 ** the information we've collected.
2693 **
2694 ** The VM register number pParse->regRowid holds the rowid of an
2695 ** entry in the sqlite_master table tht was created for this vtab
2696 ** by sqlite3StartTable().
2697 */
2698 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
2699 sqlite3NestedParse(pParse,
2700 "UPDATE %Q.%s "
2701 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
2702 "WHERE rowid=#%d",
2703 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2704 pTab->zName,
2705 pTab->zName,
2706 zStmt,
2707 pParse->regRowid
2708 );
2709 sqlite3DbFree(db, zStmt);
2710 v = sqlite3GetVdbe(pParse);
2711 sqlite3ChangeCookie(pParse, iDb);
2712
2713 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
2714 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
2715 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
2716
2717 iReg = ++pParse->nMem;
2718 sqlite3VdbeLoadString(v, iReg, pTab->zName);
2719 sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
2720 }
2721
2722 /* If we are rereading the sqlite_master table create the in-memory
2723 ** record of the table. The xConnect() method is not called until
2724 ** the first time the virtual table is used in an SQL statement. This
2725 ** allows a schema that contains virtual tables to be loaded before
2726 ** the required virtual table implementations are registered. */
2727 else {
2728 Table *pOld;
2729 Schema *pSchema = pTab->pSchema;
2730 const char *zName = pTab->zName;
2731 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
2732 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
2733 if( pOld ){
2734 db->mallocFailed = 1;
2735 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
2736 return;
2737 }
2738 pParse->pNewTable = 0;
2739 }
2740 }
2741
2742 /*
2743 ** The parser calls this routine when it sees the first token
2744 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
2745 */
2746 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
2747 addArgumentToVtab(pParse);
2748 pParse->sArg.z = 0;
2749 pParse->sArg.n = 0;
2750 }
2751
2752 /*
2753 ** The parser calls this routine for each token after the first token
2754 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
2755 */
2756 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
2757 Token *pArg = &pParse->sArg;
2758 if( pArg->z==0 ){
2759 pArg->z = p->z;
2760 pArg->n = p->n;
2761 }else{
2762 assert(pArg->z <= p->z);
2763 pArg->n = (int)(&p->z[p->n] - pArg->z);
2764 }
2765 }
2766
2767 /*
2768 ** Invoke a virtual table constructor (either xCreate or xConnect). The
2769 ** pointer to the function to invoke is passed as the fourth parameter
2770 ** to this procedure.
2771 */
2772 static int vtabCallConstructor(
2773 sqlite3 *db,
2774 Table *pTab,
2775 Module *pMod,
2776 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
2777 char **pzErr
2778 ){
2779 VtabCtx sCtx;
2780 VTable *pVTable;
2781 int rc;
2782 const char *const*azArg = (const char *const*)pTab->azModuleArg;
2783 int nArg = pTab->nModuleArg;
2784 char *zErr = 0;
2785 char *zModuleName;
2786 int iDb;
2787 VtabCtx *pCtx;
2788
2789 /* Check that the virtual-table is not already being initialized */
2790 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
2791 if( pCtx->pTab==pTab ){
2792 *pzErr = sqlite3MPrintf(db,
2793 "vtable constructor called recursively: %s", pTab->zName
2794 );
2795 return SQLITE_LOCKED;
2796 }
2797 }
2798
2799 zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
2800 if( !zModuleName ){
2801 return SQLITE_NOMEM;
2802 }
2803
2804 pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
2805 if( !pVTable ){
2806 sqlite3DbFree(db, zModuleName);
2807 return SQLITE_NOMEM;
2808 }
2809 pVTable->db = db;
2810 pVTable->pMod = pMod;
2811
2812 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
2813 pTab->azModuleArg[1] = db->aDb[iDb].zName;
2814
2815 /* Invoke the virtual table constructor */
2816 assert( &db->pVtabCtx );
2817 assert( xConstruct );
2818 sCtx.pTab = pTab;
2819 sCtx.pVTable = pVTable;
2820 sCtx.pPrior = db->pVtabCtx;
2821 sCtx.bDeclared = 0;
2822 db->pVtabCtx = &sCtx;
2823 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
2824 db->pVtabCtx = sCtx.pPrior;
2825 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
2826 assert( sCtx.pTab==pTab );
2827
2828 if( SQLITE_OK!=rc ){
2829 if( zErr==0 ){
2830 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
2831 }else {
2832 *pzErr = sqlite3MPrintf(db, "%s", zErr);
2833 sqlite3_free(zErr);
2834 }
2835 sqlite3DbFree(db, pVTable);
2836 }else if( ALWAYS(pVTable->pVtab) ){
2837 /* Justification of ALWAYS(): A correct vtab constructor must allocate
2838 ** the sqlite3_vtab object if successful. */
2839 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
2840 pVTable->pVtab->pModule = pMod->pModule;
2841 pVTable->nRef = 1;
2842 if( sCtx.bDeclared==0 ){
2843 const char *zFormat = "vtable constructor did not declare schema: %s";
2844 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
2845 sqlite3VtabUnlock(pVTable);
2846 rc = SQLITE_ERROR;
2847 }else{
2848 int iCol;
2849 u8 oooHidden = 0;
2850 /* If everything went according to plan, link the new VTable structure
2851 ** into the linked list headed by pTab->pVTable. Then loop through the
2852 ** columns of the table to see if any of them contain the token "hidden".
2853 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
2854 ** the type string. */
2855 pVTable->pNext = pTab->pVTable;
2856 pTab->pVTable = pVTable;
2857
2858 for(iCol=0; iCol<pTab->nCol; iCol++){
2859 char *zType = pTab->aCol[iCol].zType;
2860 int nType;
2861 int i = 0;
2862 if( !zType ){
2863 pTab->tabFlags |= oooHidden;
2864 continue;
2865 }
2866 nType = sqlite3Strlen30(zType);
2867 if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
2868 for(i=0; i<nType; i++){
2869 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
2870 && (zType[i+7]=='\0' || zType[i+7]==' ')
2871 ){
2872 i++;
2873 break;
2874 }
2875 }
2876 }
2877 if( i<nType ){
2878 int j;
2879 int nDel = 6 + (zType[i+6] ? 1 : 0);
2880 for(j=i; (j+nDel)<=nType; j++){
2881 zType[j] = zType[j+nDel];
2882 }
2883 if( zType[i]=='\0' && i>0 ){
2884 assert(zType[i-1]==' ');
2885 zType[i-1] = '\0';
2886 }
2887 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
2888 oooHidden = TF_OOOHidden;
2889 }else{
2890 pTab->tabFlags |= oooHidden;
2891 }
2892 }
2893 }
2894 }
2895
2896 sqlite3DbFree(db, zModuleName);
2897 return rc;
2898 }
2899
2900 /*
2901 ** This function is invoked by the parser to call the xConnect() method
2902 ** of the virtual table pTab. If an error occurs, an error code is returned
2903 ** and an error left in pParse.
2904 **
2905 ** This call is a no-op if table pTab is not a virtual table.
2906 */
2907 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
2908 sqlite3 *db = pParse->db;
2909 const char *zMod;
2910 Module *pMod;
2911 int rc;
2912
2913 assert( pTab );
2914 if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
2915 return SQLITE_OK;
2916 }
2917
2918 /* Locate the required virtual table module */
2919 zMod = pTab->azModuleArg[0];
2920 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
2921
2922 if( !pMod ){
2923 const char *zModule = pTab->azModuleArg[0];
2924 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
2925 rc = SQLITE_ERROR;
2926 }else{
2927 char *zErr = 0;
2928 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
2929 if( rc!=SQLITE_OK ){
2930 sqlite3ErrorMsg(pParse, "%s", zErr);
2931 }
2932 sqlite3DbFree(db, zErr);
2933 }
2934
2935 return rc;
2936 }
2937 /*
2938 ** Grow the db->aVTrans[] array so that there is room for at least one
2939 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
2940 */
2941 static int growVTrans(sqlite3 *db){
2942 const int ARRAY_INCR = 5;
2943
2944 /* Grow the sqlite3.aVTrans array if required */
2945 if( (db->nVTrans%ARRAY_INCR)==0 ){
2946 VTable **aVTrans;
2947 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
2948 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
2949 if( !aVTrans ){
2950 return SQLITE_NOMEM;
2951 }
2952 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
2953 db->aVTrans = aVTrans;
2954 }
2955
2956 return SQLITE_OK;
2957 }
2958
2959 /*
2960 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
2961 ** have already been reserved using growVTrans().
2962 */
2963 static void addToVTrans(sqlite3 *db, VTable *pVTab){
2964 /* Add pVtab to the end of sqlite3.aVTrans */
2965 db->aVTrans[db->nVTrans++] = pVTab;
2966 sqlite3VtabLock(pVTab);
2967 }
2968
2969 /*
2970 ** This function is invoked by the vdbe to call the xCreate method
2971 ** of the virtual table named zTab in database iDb.
2972 **
2973 ** If an error occurs, *pzErr is set to point an an English language
2974 ** description of the error and an SQLITE_XXX error code is returned.
2975 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
2976 */
2977 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
2978 int rc = SQLITE_OK;
2979 Table *pTab;
2980 Module *pMod;
2981 const char *zMod;
2982
2983 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
2984 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
2985
2986 /* Locate the required virtual table module */
2987 zMod = pTab->azModuleArg[0];
2988 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
2989
2990 /* If the module has been registered and includes a Create method,
2991 ** invoke it now. If the module has not been registered, return an
2992 ** error. Otherwise, do nothing.
2993 */
2994 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
2995 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
2996 rc = SQLITE_ERROR;
2997 }else{
2998 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
2999 }
3000
3001 /* Justification of ALWAYS(): The xConstructor method is required to
3002 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
3003 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
3004 rc = growVTrans(db);
3005 if( rc==SQLITE_OK ){
3006 addToVTrans(db, sqlite3GetVTable(db, pTab));
3007 }
3008 }
3009
3010 return rc;
3011 }
3012
3013 /*
3014 ** This function is used to set the schema of a virtual table. It is only
3015 ** valid to call this function from within the xCreate() or xConnect() of a
3016 ** virtual table module.
3017 */
3018 SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3 *db, const char *zCre ateTable){
3019 VtabCtx *pCtx;
3020 Parse *pParse;
3021 int rc = SQLITE_OK;
3022 Table *pTab;
3023 char *zErr = 0;
3024
3025 #ifdef SQLITE_ENABLE_API_ARMOR
3026 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
3027 return SQLITE_MISUSE_BKPT;
3028 }
3029 #endif
3030 sqlite3_mutex_enter(db->mutex);
3031 pCtx = db->pVtabCtx;
3032 if( !pCtx || pCtx->bDeclared ){
3033 sqlite3Error(db, SQLITE_MISUSE);
3034 sqlite3_mutex_leave(db->mutex);
3035 return SQLITE_MISUSE_BKPT;
3036 }
3037 pTab = pCtx->pTab;
3038 assert( (pTab->tabFlags & TF_Virtual)!=0 );
3039
3040 pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
3041 if( pParse==0 ){
3042 rc = SQLITE_NOMEM;
3043 }else{
3044 pParse->declareVtab = 1;
3045 pParse->db = db;
3046 pParse->nQueryLoop = 1;
3047
3048 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
3049 && pParse->pNewTable
3050 && !db->mallocFailed
3051 && !pParse->pNewTable->pSelect
3052 && (pParse->pNewTable->tabFlags & TF_Virtual)==0
3053 ){
3054 if( !pTab->aCol ){
3055 pTab->aCol = pParse->pNewTable->aCol;
3056 pTab->nCol = pParse->pNewTable->nCol;
3057 pParse->pNewTable->nCol = 0;
3058 pParse->pNewTable->aCol = 0;
3059 }
3060 pCtx->bDeclared = 1;
3061 }else{
3062 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
3063 sqlite3DbFree(db, zErr);
3064 rc = SQLITE_ERROR;
3065 }
3066 pParse->declareVtab = 0;
3067
3068 if( pParse->pVdbe ){
3069 sqlite3VdbeFinalize(pParse->pVdbe);
3070 }
3071 sqlite3DeleteTable(db, pParse->pNewTable);
3072 sqlite3ParserReset(pParse);
3073 sqlite3StackFree(db, pParse);
3074 }
3075
3076 assert( (rc&0xff)==rc );
3077 rc = sqlite3ApiExit(db, rc);
3078 sqlite3_mutex_leave(db->mutex);
3079 return rc;
3080 }
3081
3082 /*
3083 ** This function is invoked by the vdbe to call the xDestroy method
3084 ** of the virtual table named zTab in database iDb. This occurs
3085 ** when a DROP TABLE is mentioned.
3086 **
3087 ** This call is a no-op if zTab is not a virtual table.
3088 */
3089 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab ){
3090 int rc = SQLITE_OK;
3091 Table *pTab;
3092
3093 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
3094 if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
3095 VTable *p;
3096 int (*xDestroy)(sqlite3_vtab *);
3097 for(p=pTab->pVTable; p; p=p->pNext){
3098 assert( p->pVtab );
3099 if( p->pVtab->nRef>0 ){
3100 return SQLITE_LOCKED;
3101 }
3102 }
3103 p = vtabDisconnectAll(db, pTab);
3104 xDestroy = p->pMod->pModule->xDestroy;
3105 assert( xDestroy!=0 ); /* Checked before the virtual table is created */
3106 rc = xDestroy(p->pVtab);
3107 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
3108 if( rc==SQLITE_OK ){
3109 assert( pTab->pVTable==p && p->pNext==0 );
3110 p->pVtab = 0;
3111 pTab->pVTable = 0;
3112 sqlite3VtabUnlock(p);
3113 }
3114 }
3115
3116 return rc;
3117 }
3118
3119 /*
3120 ** This function invokes either the xRollback or xCommit method
3121 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
3122 ** called is identified by the second argument, "offset", which is
3123 ** the offset of the method to call in the sqlite3_module structure.
3124 **
3125 ** The array is cleared after invoking the callbacks.
3126 */
3127 static void callFinaliser(sqlite3 *db, int offset){
3128 int i;
3129 if( db->aVTrans ){
3130 VTable **aVTrans = db->aVTrans;
3131 db->aVTrans = 0;
3132 for(i=0; i<db->nVTrans; i++){
3133 VTable *pVTab = aVTrans[i];
3134 sqlite3_vtab *p = pVTab->pVtab;
3135 if( p ){
3136 int (*x)(sqlite3_vtab *);
3137 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
3138 if( x ) x(p);
3139 }
3140 pVTab->iSavepoint = 0;
3141 sqlite3VtabUnlock(pVTab);
3142 }
3143 sqlite3DbFree(db, aVTrans);
3144 db->nVTrans = 0;
3145 }
3146 }
3147
3148 /*
3149 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
3150 ** array. Return the error code for the first error that occurs, or
3151 ** SQLITE_OK if all xSync operations are successful.
3152 **
3153 ** If an error message is available, leave it in p->zErrMsg.
3154 */
3155 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
3156 int i;
3157 int rc = SQLITE_OK;
3158 VTable **aVTrans = db->aVTrans;
3159
3160 db->aVTrans = 0;
3161 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
3162 int (*x)(sqlite3_vtab *);
3163 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
3164 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
3165 rc = x(pVtab);
3166 sqlite3VtabImportErrmsg(p, pVtab);
3167 }
3168 }
3169 db->aVTrans = aVTrans;
3170 return rc;
3171 }
3172
3173 /*
3174 ** Invoke the xRollback method of all virtual tables in the
3175 ** sqlite3.aVTrans array. Then clear the array itself.
3176 */
3177 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
3178 callFinaliser(db, offsetof(sqlite3_module,xRollback));
3179 return SQLITE_OK;
3180 }
3181
3182 /*
3183 ** Invoke the xCommit method of all virtual tables in the
3184 ** sqlite3.aVTrans array. Then clear the array itself.
3185 */
3186 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
3187 callFinaliser(db, offsetof(sqlite3_module,xCommit));
3188 return SQLITE_OK;
3189 }
3190
3191 /*
3192 ** If the virtual table pVtab supports the transaction interface
3193 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
3194 ** not currently open, invoke the xBegin method now.
3195 **
3196 ** If the xBegin call is successful, place the sqlite3_vtab pointer
3197 ** in the sqlite3.aVTrans array.
3198 */
3199 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
3200 int rc = SQLITE_OK;
3201 const sqlite3_module *pModule;
3202
3203 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
3204 ** than zero, then this function is being called from within a
3205 ** virtual module xSync() callback. It is illegal to write to
3206 ** virtual module tables in this case, so return SQLITE_LOCKED.
3207 */
3208 if( sqlite3VtabInSync(db) ){
3209 return SQLITE_LOCKED;
3210 }
3211 if( !pVTab ){
3212 return SQLITE_OK;
3213 }
3214 pModule = pVTab->pVtab->pModule;
3215
3216 if( pModule->xBegin ){
3217 int i;
3218
3219 /* If pVtab is already in the aVTrans array, return early */
3220 for(i=0; i<db->nVTrans; i++){
3221 if( db->aVTrans[i]==pVTab ){
3222 return SQLITE_OK;
3223 }
3224 }
3225
3226 /* Invoke the xBegin method. If successful, add the vtab to the
3227 ** sqlite3.aVTrans[] array. */
3228 rc = growVTrans(db);
3229 if( rc==SQLITE_OK ){
3230 rc = pModule->xBegin(pVTab->pVtab);
3231 if( rc==SQLITE_OK ){
3232 int iSvpt = db->nStatement + db->nSavepoint;
3233 addToVTrans(db, pVTab);
3234 if( iSvpt ) rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, iSvpt-1);
3235 }
3236 }
3237 }
3238 return rc;
3239 }
3240
3241 /*
3242 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
3243 ** virtual tables that currently have an open transaction. Pass iSavepoint
3244 ** as the second argument to the virtual table method invoked.
3245 **
3246 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
3247 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
3248 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
3249 ** an open transaction is invoked.
3250 **
3251 ** If any virtual table method returns an error code other than SQLITE_OK,
3252 ** processing is abandoned and the error returned to the caller of this
3253 ** function immediately. If all calls to virtual table methods are successful,
3254 ** SQLITE_OK is returned.
3255 */
3256 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
3257 int rc = SQLITE_OK;
3258
3259 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
3260 assert( iSavepoint>=-1 );
3261 if( db->aVTrans ){
3262 int i;
3263 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
3264 VTable *pVTab = db->aVTrans[i];
3265 const sqlite3_module *pMod = pVTab->pMod->pModule;
3266 if( pVTab->pVtab && pMod->iVersion>=2 ){
3267 int (*xMethod)(sqlite3_vtab *, int);
3268 switch( op ){
3269 case SAVEPOINT_BEGIN:
3270 xMethod = pMod->xSavepoint;
3271 pVTab->iSavepoint = iSavepoint+1;
3272 break;
3273 case SAVEPOINT_ROLLBACK:
3274 xMethod = pMod->xRollbackTo;
3275 break;
3276 default:
3277 xMethod = pMod->xRelease;
3278 break;
3279 }
3280 if( xMethod && pVTab->iSavepoint>iSavepoint ){
3281 rc = xMethod(pVTab->pVtab, iSavepoint);
3282 }
3283 }
3284 }
3285 }
3286 return rc;
3287 }
3288
3289 /*
3290 ** The first parameter (pDef) is a function implementation. The
3291 ** second parameter (pExpr) is the first argument to this function.
3292 ** If pExpr is a column in a virtual table, then let the virtual
3293 ** table implementation have an opportunity to overload the function.
3294 **
3295 ** This routine is used to allow virtual table implementations to
3296 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
3297 **
3298 ** Return either the pDef argument (indicating no change) or a
3299 ** new FuncDef structure that is marked as ephemeral using the
3300 ** SQLITE_FUNC_EPHEM flag.
3301 */
3302 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
3303 sqlite3 *db, /* Database connection for reporting malloc problems */
3304 FuncDef *pDef, /* Function to possibly overload */
3305 int nArg, /* Number of arguments to the function */
3306 Expr *pExpr /* First argument to the function */
3307 ){
3308 Table *pTab;
3309 sqlite3_vtab *pVtab;
3310 sqlite3_module *pMod;
3311 void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
3312 void *pArg = 0;
3313 FuncDef *pNew;
3314 int rc = 0;
3315 char *zLowerName;
3316 unsigned char *z;
3317
3318
3319 /* Check to see the left operand is a column in a virtual table */
3320 if( NEVER(pExpr==0) ) return pDef;
3321 if( pExpr->op!=TK_COLUMN ) return pDef;
3322 pTab = pExpr->pTab;
3323 if( NEVER(pTab==0) ) return pDef;
3324 if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
3325 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
3326 assert( pVtab!=0 );
3327 assert( pVtab->pModule!=0 );
3328 pMod = (sqlite3_module *)pVtab->pModule;
3329 if( pMod->xFindFunction==0 ) return pDef;
3330
3331 /* Call the xFindFunction method on the virtual table implementation
3332 ** to see if the implementation wants to overload this function
3333 */
3334 zLowerName = sqlite3DbStrDup(db, pDef->zName);
3335 if( zLowerName ){
3336 for(z=(unsigned char*)zLowerName; *z; z++){
3337 *z = sqlite3UpperToLower[*z];
3338 }
3339 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
3340 sqlite3DbFree(db, zLowerName);
3341 }
3342 if( rc==0 ){
3343 return pDef;
3344 }
3345
3346 /* Create a new ephemeral function definition for the overloaded
3347 ** function */
3348 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
3349 + sqlite3Strlen30(pDef->zName) + 1);
3350 if( pNew==0 ){
3351 return pDef;
3352 }
3353 *pNew = *pDef;
3354 pNew->zName = (char *)&pNew[1];
3355 memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
3356 pNew->xFunc = xFunc;
3357 pNew->pUserData = pArg;
3358 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
3359 return pNew;
3360 }
3361
3362 /*
3363 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
3364 ** array so that an OP_VBegin will get generated for it. Add pTab to the
3365 ** array if it is missing. If pTab is already in the array, this routine
3366 ** is a no-op.
3367 */
3368 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
3369 Parse *pToplevel = sqlite3ParseToplevel(pParse);
3370 int i, n;
3371 Table **apVtabLock;
3372
3373 assert( IsVirtual(pTab) );
3374 for(i=0; i<pToplevel->nVtabLock; i++){
3375 if( pTab==pToplevel->apVtabLock[i] ) return;
3376 }
3377 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
3378 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
3379 if( apVtabLock ){
3380 pToplevel->apVtabLock = apVtabLock;
3381 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
3382 }else{
3383 pToplevel->db->mallocFailed = 1;
3384 }
3385 }
3386
3387 /*
3388 ** Check to see if virtual tale module pMod can be have an eponymous
3389 ** virtual table instance. If it can, create one if one does not already
3390 ** exist. Return non-zero if the eponymous virtual table instance exists
3391 ** when this routine returns, and return zero if it does not exist.
3392 **
3393 ** An eponymous virtual table instance is one that is named after its
3394 ** module, and more importantly, does not require a CREATE VIRTUAL TABLE
3395 ** statement in order to come into existance. Eponymous virtual table
3396 ** instances always exist. They cannot be DROP-ed.
3397 **
3398 ** Any virtual table module for which xConnect and xCreate are the same
3399 ** method can have an eponymous virtual table instance.
3400 */
3401 SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
3402 const sqlite3_module *pModule = pMod->pModule;
3403 Table *pTab;
3404 char *zErr = 0;
3405 int nName;
3406 int rc;
3407 sqlite3 *db = pParse->db;
3408 if( pMod->pEpoTab ) return 1;
3409 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
3410 nName = sqlite3Strlen30(pMod->zName) + 1;
3411 pTab = sqlite3DbMallocZero(db, sizeof(Table) + nName);
3412 if( pTab==0 ) return 0;
3413 pMod->pEpoTab = pTab;
3414 pTab->zName = (char*)&pTab[1];
3415 memcpy(pTab->zName, pMod->zName, nName);
3416 pTab->nRef = 1;
3417 pTab->pSchema = db->aDb[0].pSchema;
3418 pTab->tabFlags |= TF_Virtual;
3419 pTab->nModuleArg = 0;
3420 pTab->iPKey = -1;
3421 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
3422 addModuleArgument(db, pTab, 0);
3423 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
3424 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
3425 if( rc ){
3426 sqlite3ErrorMsg(pParse, "%s", zErr);
3427 sqlite3DbFree(db, zErr);
3428 sqlite3VtabEponymousTableClear(db, pMod);
3429 return 0;
3430 }
3431 return 1;
3432 }
3433
3434 /*
3435 ** Erase the eponymous virtual table instance associated with
3436 ** virtual table module pMod, if it exists.
3437 */
3438 SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
3439 Table *pTab = pMod->pEpoTab;
3440 if( pTab!=0 ){
3441 sqlite3DeleteColumnNames(db, pTab);
3442 sqlite3VtabClear(db, pTab);
3443 sqlite3DbFree(db, pTab);
3444 pMod->pEpoTab = 0;
3445 }
3446 }
3447
3448 /*
3449 ** Return the ON CONFLICT resolution mode in effect for the virtual
3450 ** table update operation currently in progress.
3451 **
3452 ** The results of this routine are undefined unless it is called from
3453 ** within an xUpdate method.
3454 */
3455 SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *db){
3456 static const unsigned char aMap[] = {
3457 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
3458 };
3459 #ifdef SQLITE_ENABLE_API_ARMOR
3460 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3461 #endif
3462 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
3463 assert( OE_Ignore==4 && OE_Replace==5 );
3464 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
3465 return (int)aMap[db->vtabOnConflict-1];
3466 }
3467
3468 /*
3469 ** Call from within the xCreate() or xConnect() methods to provide
3470 ** the SQLite core with additional information about the behavior
3471 ** of the virtual table being implemented.
3472 */
3473 SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3 *db, int op, ...){
3474 va_list ap;
3475 int rc = SQLITE_OK;
3476
3477 #ifdef SQLITE_ENABLE_API_ARMOR
3478 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3479 #endif
3480 sqlite3_mutex_enter(db->mutex);
3481 va_start(ap, op);
3482 switch( op ){
3483 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
3484 VtabCtx *p = db->pVtabCtx;
3485 if( !p ){
3486 rc = SQLITE_MISUSE_BKPT;
3487 }else{
3488 assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
3489 p->pVTable->bConstraint = (u8)va_arg(ap, int);
3490 }
3491 break;
3492 }
3493 default:
3494 rc = SQLITE_MISUSE_BKPT;
3495 break;
3496 }
3497 va_end(ap);
3498
3499 if( rc!=SQLITE_OK ) sqlite3Error(db, rc);
3500 sqlite3_mutex_leave(db->mutex);
3501 return rc;
3502 }
3503
3504 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3505
3506 /************** End of vtab.c ************************************************/
3507 /************** Begin file wherecode.c ***************************************/
3508 /*
3509 ** 2015-06-06
3510 **
3511 ** The author disclaims copyright to this source code. In place of
3512 ** a legal notice, here is a blessing:
3513 **
3514 ** May you do good and not evil.
3515 ** May you find forgiveness for yourself and forgive others.
3516 ** May you share freely, never taking more than you give.
3517 **
3518 *************************************************************************
3519 ** This module contains C code that generates VDBE code used to process
3520 ** the WHERE clause of SQL statements.
3521 **
3522 ** This file was split off from where.c on 2015-06-06 in order to reduce the
3523 ** size of where.c and make it easier to edit. This file contains the routines
3524 ** that actually generate the bulk of the WHERE loop code. The original where.c
3525 ** file retains the code that does query planning and analysis.
3526 */
3527 /* #include "sqliteInt.h" */
3528 /************** Include whereInt.h in the middle of wherecode.c **************/
3529 /************** Begin file whereInt.h ****************************************/
3530 /*
3531 ** 2013-11-12
3532 **
3533 ** The author disclaims copyright to this source code. In place of
3534 ** a legal notice, here is a blessing:
3535 **
3536 ** May you do good and not evil.
3537 ** May you find forgiveness for yourself and forgive others.
3538 ** May you share freely, never taking more than you give.
3539 **
3540 *************************************************************************
3541 **
3542 ** This file contains structure and macro definitions for the query
3543 ** planner logic in "where.c". These definitions are broken out into
3544 ** a separate source file for easier editing.
3545 */
3546
3547 /*
3548 ** Trace output macros
3549 */
3550 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
3551 /***/ int sqlite3WhereTrace;
3552 #endif
3553 #if defined(SQLITE_DEBUG) \
3554 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
3555 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
3556 # define WHERETRACE_ENABLED 1
3557 #else
3558 # define WHERETRACE(K,X)
3559 #endif
3560
3561 /* Forward references
3562 */
3563 typedef struct WhereClause WhereClause;
3564 typedef struct WhereMaskSet WhereMaskSet;
3565 typedef struct WhereOrInfo WhereOrInfo;
3566 typedef struct WhereAndInfo WhereAndInfo;
3567 typedef struct WhereLevel WhereLevel;
3568 typedef struct WhereLoop WhereLoop;
3569 typedef struct WherePath WherePath;
3570 typedef struct WhereTerm WhereTerm;
3571 typedef struct WhereLoopBuilder WhereLoopBuilder;
3572 typedef struct WhereScan WhereScan;
3573 typedef struct WhereOrCost WhereOrCost;
3574 typedef struct WhereOrSet WhereOrSet;
3575
3576 /*
3577 ** This object contains information needed to implement a single nested
3578 ** loop in WHERE clause.
3579 **
3580 ** Contrast this object with WhereLoop. This object describes the
3581 ** implementation of the loop. WhereLoop describes the algorithm.
3582 ** This object contains a pointer to the WhereLoop algorithm as one of
3583 ** its elements.
3584 **
3585 ** The WhereInfo object contains a single instance of this object for
3586 ** each term in the FROM clause (which is to say, for each of the
3587 ** nested loops as implemented). The order of WhereLevel objects determines
3588 ** the loop nested order, with WhereInfo.a[0] being the outer loop and
3589 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
3590 */
3591 struct WhereLevel {
3592 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
3593 int iTabCur; /* The VDBE cursor used to access the table */
3594 int iIdxCur; /* The VDBE cursor used to access pIdx */
3595 int addrBrk; /* Jump here to break out of the loop */
3596 int addrNxt; /* Jump here to start the next IN combination */
3597 int addrSkip; /* Jump here for next iteration of skip-scan */
3598 int addrCont; /* Jump here to continue with the next loop cycle */
3599 int addrFirst; /* First instruction of interior of the loop */
3600 int addrBody; /* Beginning of the body of this loop */
3601 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
3602 int iLikeRepCntr; /* LIKE range processing counter register */
3603 int addrLikeRep; /* LIKE range processing address */
3604 #endif
3605 u8 iFrom; /* Which entry in the FROM clause */
3606 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
3607 int p1, p2; /* Operands of the opcode used to ends the loop */
3608 union { /* Information that depends on pWLoop->wsFlags */
3609 struct {
3610 int nIn; /* Number of entries in aInLoop[] */
3611 struct InLoop {
3612 int iCur; /* The VDBE cursor used by this IN operator */
3613 int addrInTop; /* Top of the IN loop */
3614 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
3615 } *aInLoop; /* Information about each nested IN operator */
3616 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
3617 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
3618 } u;
3619 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
3620 Bitmask notReady; /* FROM entries not usable at this level */
3621 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3622 int addrVisit; /* Address at which row is visited */
3623 #endif
3624 };
3625
3626 /*
3627 ** Each instance of this object represents an algorithm for evaluating one
3628 ** term of a join. Every term of the FROM clause will have at least
3629 ** one corresponding WhereLoop object (unless INDEXED BY constraints
3630 ** prevent a query solution - which is an error) and many terms of the
3631 ** FROM clause will have multiple WhereLoop objects, each describing a
3632 ** potential way of implementing that FROM-clause term, together with
3633 ** dependencies and cost estimates for using the chosen algorithm.
3634 **
3635 ** Query planning consists of building up a collection of these WhereLoop
3636 ** objects, then computing a particular sequence of WhereLoop objects, with
3637 ** one WhereLoop object per FROM clause term, that satisfy all dependencies
3638 ** and that minimize the overall cost.
3639 */
3640 struct WhereLoop {
3641 Bitmask prereq; /* Bitmask of other loops that must run first */
3642 Bitmask maskSelf; /* Bitmask identifying table iTab */
3643 #ifdef SQLITE_DEBUG
3644 char cId; /* Symbolic ID of this loop for debugging use */
3645 #endif
3646 u8 iTab; /* Position in FROM clause of table for this loop */
3647 u8 iSortIdx; /* Sorting index number. 0==None */
3648 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
3649 LogEst rRun; /* Cost of running each loop */
3650 LogEst nOut; /* Estimated number of output rows */
3651 union {
3652 struct { /* Information for internal btree tables */
3653 u16 nEq; /* Number of equality constraints */
3654 Index *pIndex; /* Index used, or NULL */
3655 } btree;
3656 struct { /* Information for virtual tables */
3657 int idxNum; /* Index number */
3658 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
3659 i8 isOrdered; /* True if satisfies ORDER BY */
3660 u16 omitMask; /* Terms that may be omitted */
3661 char *idxStr; /* Index identifier string */
3662 } vtab;
3663 } u;
3664 u32 wsFlags; /* WHERE_* flags describing the plan */
3665 u16 nLTerm; /* Number of entries in aLTerm[] */
3666 u16 nSkip; /* Number of NULL aLTerm[] entries */
3667 /**** whereLoopXfer() copies fields above ***********************/
3668 # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
3669 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
3670 WhereTerm **aLTerm; /* WhereTerms used */
3671 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
3672 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
3673 };
3674
3675 /* This object holds the prerequisites and the cost of running a
3676 ** subquery on one operand of an OR operator in the WHERE clause.
3677 ** See WhereOrSet for additional information
3678 */
3679 struct WhereOrCost {
3680 Bitmask prereq; /* Prerequisites */
3681 LogEst rRun; /* Cost of running this subquery */
3682 LogEst nOut; /* Number of outputs for this subquery */
3683 };
3684
3685 /* The WhereOrSet object holds a set of possible WhereOrCosts that
3686 ** correspond to the subquery(s) of OR-clause processing. Only the
3687 ** best N_OR_COST elements are retained.
3688 */
3689 #define N_OR_COST 3
3690 struct WhereOrSet {
3691 u16 n; /* Number of valid a[] entries */
3692 WhereOrCost a[N_OR_COST]; /* Set of best costs */
3693 };
3694
3695 /*
3696 ** Each instance of this object holds a sequence of WhereLoop objects
3697 ** that implement some or all of a query plan.
3698 **
3699 ** Think of each WhereLoop object as a node in a graph with arcs
3700 ** showing dependencies and costs for travelling between nodes. (That is
3701 ** not a completely accurate description because WhereLoop costs are a
3702 ** vector, not a scalar, and because dependencies are many-to-one, not
3703 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
3704 ** Then a WherePath object is a path through the graph that visits some
3705 ** or all of the WhereLoop objects once.
3706 **
3707 ** The "solver" works by creating the N best WherePath objects of length
3708 ** 1. Then using those as a basis to compute the N best WherePath objects
3709 ** of length 2. And so forth until the length of WherePaths equals the
3710 ** number of nodes in the FROM clause. The best (lowest cost) WherePath
3711 ** at the end is the chosen query plan.
3712 */
3713 struct WherePath {
3714 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
3715 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
3716 LogEst nRow; /* Estimated number of rows generated by this path */
3717 LogEst rCost; /* Total cost of this path */
3718 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
3719 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
3720 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
3721 };
3722
3723 /*
3724 ** The query generator uses an array of instances of this structure to
3725 ** help it analyze the subexpressions of the WHERE clause. Each WHERE
3726 ** clause subexpression is separated from the others by AND operators,
3727 ** usually, or sometimes subexpressions separated by OR.
3728 **
3729 ** All WhereTerms are collected into a single WhereClause structure.
3730 ** The following identity holds:
3731 **
3732 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
3733 **
3734 ** When a term is of the form:
3735 **
3736 ** X <op> <expr>
3737 **
3738 ** where X is a column name and <op> is one of certain operators,
3739 ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
3740 ** cursor number and column number for X. WhereTerm.eOperator records
3741 ** the <op> using a bitmask encoding defined by WO_xxx below. The
3742 ** use of a bitmask encoding for the operator allows us to search
3743 ** quickly for terms that match any of several different operators.
3744 **
3745 ** A WhereTerm might also be two or more subterms connected by OR:
3746 **
3747 ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
3748 **
3749 ** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
3750 ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
3751 ** is collected about the OR clause.
3752 **
3753 ** If a term in the WHERE clause does not match either of the two previous
3754 ** categories, then eOperator==0. The WhereTerm.pExpr field is still set
3755 ** to the original subexpression content and wtFlags is set up appropriately
3756 ** but no other fields in the WhereTerm object are meaningful.
3757 **
3758 ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
3759 ** but they do so indirectly. A single WhereMaskSet structure translates
3760 ** cursor number into bits and the translated bit is stored in the prereq
3761 ** fields. The translation is used in order to maximize the number of
3762 ** bits that will fit in a Bitmask. The VDBE cursor numbers might be
3763 ** spread out over the non-negative integers. For example, the cursor
3764 ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
3765 ** translates these sparse cursor numbers into consecutive integers
3766 ** beginning with 0 in order to make the best possible use of the available
3767 ** bits in the Bitmask. So, in the example above, the cursor numbers
3768 ** would be mapped into integers 0 through 7.
3769 **
3770 ** The number of terms in a join is limited by the number of bits
3771 ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
3772 ** is only able to process joins with 64 or fewer tables.
3773 */
3774 struct WhereTerm {
3775 Expr *pExpr; /* Pointer to the subexpression that is this term */
3776 int iParent; /* Disable pWC->a[iParent] when this term disabled */
3777 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
3778 union {
3779 int leftColumn; /* Column number of X in "X <op> <expr>" */
3780 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
3781 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
3782 } u;
3783 LogEst truthProb; /* Probability of truth for this expression */
3784 u16 eOperator; /* A WO_xx value describing <op> */
3785 u16 wtFlags; /* TERM_xxx bit flags. See below */
3786 u8 nChild; /* Number of children that must disable us */
3787 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
3788 WhereClause *pWC; /* The clause this term is part of */
3789 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
3790 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
3791 };
3792
3793 /*
3794 ** Allowed values of WhereTerm.wtFlags
3795 */
3796 #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */
3797 #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */
3798 #define TERM_CODED 0x04 /* This term is already coded */
3799 #define TERM_COPIED 0x08 /* Has a child */
3800 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
3801 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
3802 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
3803 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
3804 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
3805 #else
3806 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */
3807 #endif
3808 #define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */
3809 #define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */
3810 #define TERM_LIKE 0x400 /* The original LIKE operator */
3811 #define TERM_IS 0x800 /* Term.pExpr is an IS operator */
3812
3813 /*
3814 ** An instance of the WhereScan object is used as an iterator for locating
3815 ** terms in the WHERE clause that are useful to the query planner.
3816 */
3817 struct WhereScan {
3818 WhereClause *pOrigWC; /* Original, innermost WhereClause */
3819 WhereClause *pWC; /* WhereClause currently being scanned */
3820 const char *zCollName; /* Required collating sequence, if not NULL */
3821 Expr *pIdxExpr; /* Search for this index expression */
3822 char idxaff; /* Must match this affinity, if zCollName!=NULL */
3823 unsigned char nEquiv; /* Number of entries in aEquiv[] */
3824 unsigned char iEquiv; /* Next unused slot in aEquiv[] */
3825 u32 opMask; /* Acceptable operators */
3826 int k; /* Resume scanning at this->pWC->a[this->k] */
3827 int aiCur[11]; /* Cursors in the equivalence class */
3828 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
3829 };
3830
3831 /*
3832 ** An instance of the following structure holds all information about a
3833 ** WHERE clause. Mostly this is a container for one or more WhereTerms.
3834 **
3835 ** Explanation of pOuter: For a WHERE clause of the form
3836 **
3837 ** a AND ((b AND c) OR (d AND e)) AND f
3838 **
3839 ** There are separate WhereClause objects for the whole clause and for
3840 ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
3841 ** subclauses points to the WhereClause object for the whole clause.
3842 */
3843 struct WhereClause {
3844 WhereInfo *pWInfo; /* WHERE clause processing context */
3845 WhereClause *pOuter; /* Outer conjunction */
3846 u8 op; /* Split operator. TK_AND or TK_OR */
3847 int nTerm; /* Number of terms */
3848 int nSlot; /* Number of entries in a[] */
3849 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
3850 #if defined(SQLITE_SMALL_STACK)
3851 WhereTerm aStatic[1]; /* Initial static space for a[] */
3852 #else
3853 WhereTerm aStatic[8]; /* Initial static space for a[] */
3854 #endif
3855 };
3856
3857 /*
3858 ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
3859 ** a dynamically allocated instance of the following structure.
3860 */
3861 struct WhereOrInfo {
3862 WhereClause wc; /* Decomposition into subterms */
3863 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
3864 };
3865
3866 /*
3867 ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
3868 ** a dynamically allocated instance of the following structure.
3869 */
3870 struct WhereAndInfo {
3871 WhereClause wc; /* The subexpression broken out */
3872 };
3873
3874 /*
3875 ** An instance of the following structure keeps track of a mapping
3876 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
3877 **
3878 ** The VDBE cursor numbers are small integers contained in
3879 ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
3880 ** clause, the cursor numbers might not begin with 0 and they might
3881 ** contain gaps in the numbering sequence. But we want to make maximum
3882 ** use of the bits in our bitmasks. This structure provides a mapping
3883 ** from the sparse cursor numbers into consecutive integers beginning
3884 ** with 0.
3885 **
3886 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
3887 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
3888 **
3889 ** For example, if the WHERE clause expression used these VDBE
3890 ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
3891 ** would map those cursor numbers into bits 0 through 5.
3892 **
3893 ** Note that the mapping is not necessarily ordered. In the example
3894 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
3895 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
3896 ** does not really matter. What is important is that sparse cursor
3897 ** numbers all get mapped into bit numbers that begin with 0 and contain
3898 ** no gaps.
3899 */
3900 struct WhereMaskSet {
3901 int n; /* Number of assigned cursor values */
3902 int ix[BMS]; /* Cursor assigned to each bit */
3903 };
3904
3905 /*
3906 ** Initialize a WhereMaskSet object
3907 */
3908 #define initMaskSet(P) (P)->n=0
3909
3910 /*
3911 ** This object is a convenience wrapper holding all information needed
3912 ** to construct WhereLoop objects for a particular query.
3913 */
3914 struct WhereLoopBuilder {
3915 WhereInfo *pWInfo; /* Information about this WHERE */
3916 WhereClause *pWC; /* WHERE clause terms */
3917 ExprList *pOrderBy; /* ORDER BY clause */
3918 WhereLoop *pNew; /* Template WhereLoop */
3919 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
3920 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
3921 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
3922 int nRecValid; /* Number of valid fields currently in pRec */
3923 #endif
3924 };
3925
3926 /*
3927 ** The WHERE clause processing routine has two halves. The
3928 ** first part does the start of the WHERE loop and the second
3929 ** half does the tail of the WHERE loop. An instance of
3930 ** this structure is returned by the first half and passed
3931 ** into the second half to give some continuity.
3932 **
3933 ** An instance of this object holds the complete state of the query
3934 ** planner.
3935 */
3936 struct WhereInfo {
3937 Parse *pParse; /* Parsing and code generating context */
3938 SrcList *pTabList; /* List of tables in the join */
3939 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
3940 ExprList *pResultSet; /* Result set. DISTINCT operates on these */
3941 WhereLoop *pLoops; /* List of all WhereLoop objects */
3942 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
3943 LogEst nRowOut; /* Estimated number of output rows */
3944 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
3945 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
3946 u8 sorted; /* True if really sorted (not just grouped) */
3947 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
3948 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
3949 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
3950 u8 nLevel; /* Number of nested loop */
3951 int iTop; /* The very beginning of the WHERE loop */
3952 int iContinue; /* Jump here to continue with next record */
3953 int iBreak; /* Jump here to break out of the loop */
3954 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
3955 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
3956 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
3957 WhereClause sWC; /* Decomposition of the WHERE clause */
3958 WhereLevel a[1]; /* Information about each nest loop in WHERE */
3959 };
3960
3961 /*
3962 ** Private interfaces - callable only by other where.c routines.
3963 **
3964 ** where.c:
3965 */
3966 SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
3967 SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
3968 WhereClause *pWC, /* The WHERE clause to be searched */
3969 int iCur, /* Cursor number of LHS */
3970 int iColumn, /* Column number of LHS */
3971 Bitmask notReady, /* RHS must not overlap with this mask */
3972 u32 op, /* Mask of WO_xx values describing operator */
3973 Index *pIdx /* Must be compatible with this index, if not NULL */
3974 );
3975
3976 /* wherecode.c: */
3977 #ifndef SQLITE_OMIT_EXPLAIN
3978 SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
3979 Parse *pParse, /* Parse context */
3980 SrcList *pTabList, /* Table list this loop refers to */
3981 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
3982 int iLevel, /* Value for "level" column of output */
3983 int iFrom, /* Value for "from" column of output */
3984 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
3985 );
3986 #else
3987 # define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
3988 #endif /* SQLITE_OMIT_EXPLAIN */
3989 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3990 SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
3991 Vdbe *v, /* Vdbe to add scanstatus entry to */
3992 SrcList *pSrclist, /* FROM clause pLvl reads data from */
3993 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
3994 int addrExplain /* Address of OP_Explain (or 0) */
3995 );
3996 #else
3997 # define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
3998 #endif
3999 SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
4000 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
4001 int iLevel, /* Which level of pWInfo->a[] should be coded */
4002 Bitmask notReady /* Which tables are currently available */
4003 );
4004
4005 /* whereexpr.c: */
4006 SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
4007 SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
4008 SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
4009 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
4010 SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
4011 SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
4012 SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereC lause*);
4013
4014
4015
4016
4017
4018 /*
4019 ** Bitmasks for the operators on WhereTerm objects. These are all
4020 ** operators that are of interest to the query planner. An
4021 ** OR-ed combination of these values can be used when searching for
4022 ** particular WhereTerms within a WhereClause.
4023 */
4024 #define WO_IN 0x0001
4025 #define WO_EQ 0x0002
4026 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
4027 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
4028 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
4029 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
4030 #define WO_MATCH 0x0040
4031 #define WO_IS 0x0080
4032 #define WO_ISNULL 0x0100
4033 #define WO_OR 0x0200 /* Two or more OR-connected terms */
4034 #define WO_AND 0x0400 /* Two or more AND-connected terms */
4035 #define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
4036 #define WO_NOOP 0x1000 /* This term does not restrict search space */
4037
4038 #define WO_ALL 0x1fff /* Mask of all possible WO_* values */
4039 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
4040
4041 /*
4042 ** These are definitions of bits in the WhereLoop.wsFlags field.
4043 ** The particular combination of bits in each WhereLoop help to
4044 ** determine the algorithm that WhereLoop represents.
4045 */
4046 #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */
4047 #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */
4048 #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */
4049 #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */
4050 #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
4051 #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */
4052 #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */
4053 #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */
4054 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
4055 #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */
4056 #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
4057 #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */
4058 #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
4059 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
4060 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
4061 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
4062 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
4063 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
4064 #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
4065
4066 /************** End of whereInt.h ********************************************/
4067 /************** Continuing where we left off in wherecode.c ******************/
4068
4069 #ifndef SQLITE_OMIT_EXPLAIN
4070 /*
4071 ** This routine is a helper for explainIndexRange() below
4072 **
4073 ** pStr holds the text of an expression that we are building up one term
4074 ** at a time. This routine adds a new term to the end of the expression.
4075 ** Terms are separated by AND so add the "AND" text for second and subsequent
4076 ** terms only.
4077 */
4078 static void explainAppendTerm(
4079 StrAccum *pStr, /* The text expression being built */
4080 int iTerm, /* Index of this term. First is zero */
4081 const char *zColumn, /* Name of the column */
4082 const char *zOp /* Name of the operator */
4083 ){
4084 if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
4085 sqlite3StrAccumAppendAll(pStr, zColumn);
4086 sqlite3StrAccumAppend(pStr, zOp, 1);
4087 sqlite3StrAccumAppend(pStr, "?", 1);
4088 }
4089
4090 /*
4091 ** Return the name of the i-th column of the pIdx index.
4092 */
4093 static const char *explainIndexColumnName(Index *pIdx, int i){
4094 i = pIdx->aiColumn[i];
4095 if( i==XN_EXPR ) return "<expr>";
4096 if( i==XN_ROWID ) return "rowid";
4097 return pIdx->pTable->aCol[i].zName;
4098 }
4099
4100 /*
4101 ** Argument pLevel describes a strategy for scanning table pTab. This
4102 ** function appends text to pStr that describes the subset of table
4103 ** rows scanned by the strategy in the form of an SQL expression.
4104 **
4105 ** For example, if the query:
4106 **
4107 ** SELECT * FROM t1 WHERE a=1 AND b>2;
4108 **
4109 ** is run and there is an index on (a, b), then this function returns a
4110 ** string similar to:
4111 **
4112 ** "a=? AND b>?"
4113 */
4114 static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
4115 Index *pIndex = pLoop->u.btree.pIndex;
4116 u16 nEq = pLoop->u.btree.nEq;
4117 u16 nSkip = pLoop->nSkip;
4118 int i, j;
4119
4120 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
4121 sqlite3StrAccumAppend(pStr, " (", 2);
4122 for(i=0; i<nEq; i++){
4123 const char *z = explainIndexColumnName(pIndex, i);
4124 if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
4125 sqlite3XPrintf(pStr, 0, i>=nSkip ? "%s=?" : "ANY(%s)", z);
4126 }
4127
4128 j = i;
4129 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
4130 const char *z = explainIndexColumnName(pIndex, i);
4131 explainAppendTerm(pStr, i++, z, ">");
4132 }
4133 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
4134 const char *z = explainIndexColumnName(pIndex, j);
4135 explainAppendTerm(pStr, i, z, "<");
4136 }
4137 sqlite3StrAccumAppend(pStr, ")", 1);
4138 }
4139
4140 /*
4141 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
4142 ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
4143 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
4144 ** is added to the output to describe the table scan strategy in pLevel.
4145 **
4146 ** If an OP_Explain opcode is added to the VM, its address is returned.
4147 ** Otherwise, if no OP_Explain is coded, zero is returned.
4148 */
4149 SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
4150 Parse *pParse, /* Parse context */
4151 SrcList *pTabList, /* Table list this loop refers to */
4152 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
4153 int iLevel, /* Value for "level" column of output */
4154 int iFrom, /* Value for "from" column of output */
4155 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
4156 ){
4157 int ret = 0;
4158 #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
4159 if( pParse->explain==2 )
4160 #endif
4161 {
4162 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
4163 Vdbe *v = pParse->pVdbe; /* VM being constructed */
4164 sqlite3 *db = pParse->db; /* Database handle */
4165 int iId = pParse->iSelectId; /* Select id (left-most output column) */
4166 int isSearch; /* True for a SEARCH. False for SCAN. */
4167 WhereLoop *pLoop; /* The controlling WhereLoop object */
4168 u32 flags; /* Flags that describe this loop */
4169 char *zMsg; /* Text to add to EQP output */
4170 StrAccum str; /* EQP output string */
4171 char zBuf[100]; /* Initial space for EQP output string */
4172
4173 pLoop = pLevel->pWLoop;
4174 flags = pLoop->wsFlags;
4175 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
4176
4177 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
4178 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
4179 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
4180
4181 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
4182 sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
4183 if( pItem->pSelect ){
4184 sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
4185 }else{
4186 sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
4187 }
4188
4189 if( pItem->zAlias ){
4190 sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
4191 }
4192 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
4193 const char *zFmt = 0;
4194 Index *pIdx;
4195
4196 assert( pLoop->u.btree.pIndex!=0 );
4197 pIdx = pLoop->u.btree.pIndex;
4198 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
4199 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
4200 if( isSearch ){
4201 zFmt = "PRIMARY KEY";
4202 }
4203 }else if( flags & WHERE_PARTIALIDX ){
4204 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
4205 }else if( flags & WHERE_AUTO_INDEX ){
4206 zFmt = "AUTOMATIC COVERING INDEX";
4207 }else if( flags & WHERE_IDX_ONLY ){
4208 zFmt = "COVERING INDEX %s";
4209 }else{
4210 zFmt = "INDEX %s";
4211 }
4212 if( zFmt ){
4213 sqlite3StrAccumAppend(&str, " USING ", 7);
4214 sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
4215 explainIndexRange(&str, pLoop);
4216 }
4217 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
4218 const char *zRangeOp;
4219 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
4220 zRangeOp = "=";
4221 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
4222 zRangeOp = ">? AND rowid<";
4223 }else if( flags&WHERE_BTM_LIMIT ){
4224 zRangeOp = ">";
4225 }else{
4226 assert( flags&WHERE_TOP_LIMIT);
4227 zRangeOp = "<";
4228 }
4229 sqlite3XPrintf(&str, 0, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
4230 }
4231 #ifndef SQLITE_OMIT_VIRTUALTABLE
4232 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
4233 sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
4234 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
4235 }
4236 #endif
4237 #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
4238 if( pLoop->nOut>=10 ){
4239 sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
4240 }else{
4241 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
4242 }
4243 #endif
4244 zMsg = sqlite3StrAccumFinish(&str);
4245 ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
4246 }
4247 return ret;
4248 }
4249 #endif /* SQLITE_OMIT_EXPLAIN */
4250
4251 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
4252 /*
4253 ** Configure the VM passed as the first argument with an
4254 ** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
4255 ** implement level pLvl. Argument pSrclist is a pointer to the FROM
4256 ** clause that the scan reads data from.
4257 **
4258 ** If argument addrExplain is not 0, it must be the address of an
4259 ** OP_Explain instruction that describes the same loop.
4260 */
4261 SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
4262 Vdbe *v, /* Vdbe to add scanstatus entry to */
4263 SrcList *pSrclist, /* FROM clause pLvl reads data from */
4264 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
4265 int addrExplain /* Address of OP_Explain (or 0) */
4266 ){
4267 const char *zObj = 0;
4268 WhereLoop *pLoop = pLvl->pWLoop;
4269 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
4270 zObj = pLoop->u.btree.pIndex->zName;
4271 }else{
4272 zObj = pSrclist->a[pLvl->iFrom].zName;
4273 }
4274 sqlite3VdbeScanStatus(
4275 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
4276 );
4277 }
4278 #endif
4279
4280
4281 /*
4282 ** Disable a term in the WHERE clause. Except, do not disable the term
4283 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
4284 ** or USING clause of that join.
4285 **
4286 ** Consider the term t2.z='ok' in the following queries:
4287 **
4288 ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
4289 ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
4290 ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
4291 **
4292 ** The t2.z='ok' is disabled in the in (2) because it originates
4293 ** in the ON clause. The term is disabled in (3) because it is not part
4294 ** of a LEFT OUTER JOIN. In (1), the term is not disabled.
4295 **
4296 ** Disabling a term causes that term to not be tested in the inner loop
4297 ** of the join. Disabling is an optimization. When terms are satisfied
4298 ** by indices, we disable them to prevent redundant tests in the inner
4299 ** loop. We would get the correct results if nothing were ever disabled,
4300 ** but joins might run a little slower. The trick is to disable as much
4301 ** as we can without disabling too much. If we disabled in (1), we'd get
4302 ** the wrong answer. See ticket #813.
4303 **
4304 ** If all the children of a term are disabled, then that term is also
4305 ** automatically disabled. In this way, terms get disabled if derived
4306 ** virtual terms are tested first. For example:
4307 **
4308 ** x GLOB 'abc*' AND x>='abc' AND x<'acd'
4309 ** \___________/ \______/ \_____/
4310 ** parent child1 child2
4311 **
4312 ** Only the parent term was in the original WHERE clause. The child1
4313 ** and child2 terms were added by the LIKE optimization. If both of
4314 ** the virtual child terms are valid, then testing of the parent can be
4315 ** skipped.
4316 **
4317 ** Usually the parent term is marked as TERM_CODED. But if the parent
4318 ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
4319 ** The TERM_LIKECOND marking indicates that the term should be coded inside
4320 ** a conditional such that is only evaluated on the second pass of a
4321 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
4322 */
4323 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
4324 int nLoop = 0;
4325 while( pTerm
4326 && (pTerm->wtFlags & TERM_CODED)==0
4327 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
4328 && (pLevel->notReady & pTerm->prereqAll)==0
4329 ){
4330 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
4331 pTerm->wtFlags |= TERM_LIKECOND;
4332 }else{
4333 pTerm->wtFlags |= TERM_CODED;
4334 }
4335 if( pTerm->iParent<0 ) break;
4336 pTerm = &pTerm->pWC->a[pTerm->iParent];
4337 pTerm->nChild--;
4338 if( pTerm->nChild!=0 ) break;
4339 nLoop++;
4340 }
4341 }
4342
4343 /*
4344 ** Code an OP_Affinity opcode to apply the column affinity string zAff
4345 ** to the n registers starting at base.
4346 **
4347 ** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
4348 ** beginning and end of zAff are ignored. If all entries in zAff are
4349 ** SQLITE_AFF_BLOB, then no code gets generated.
4350 **
4351 ** This routine makes its own copy of zAff so that the caller is free
4352 ** to modify zAff after this routine returns.
4353 */
4354 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
4355 Vdbe *v = pParse->pVdbe;
4356 if( zAff==0 ){
4357 assert( pParse->db->mallocFailed );
4358 return;
4359 }
4360 assert( v!=0 );
4361
4362 /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
4363 ** and end of the affinity string.
4364 */
4365 while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
4366 n--;
4367 base++;
4368 zAff++;
4369 }
4370 while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
4371 n--;
4372 }
4373
4374 /* Code the OP_Affinity opcode if there is anything left to do. */
4375 if( n>0 ){
4376 sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
4377 sqlite3VdbeChangeP4(v, -1, zAff, n);
4378 sqlite3ExprCacheAffinityChange(pParse, base, n);
4379 }
4380 }
4381
4382
4383 /*
4384 ** Generate code for a single equality term of the WHERE clause. An equality
4385 ** term can be either X=expr or X IN (...). pTerm is the term to be
4386 ** coded.
4387 **
4388 ** The current value for the constraint is left in register iReg.
4389 **
4390 ** For a constraint of the form X=expr, the expression is evaluated and its
4391 ** result is left on the stack. For constraints of the form X IN (...)
4392 ** this routine sets up a loop that will iterate over all values of X.
4393 */
4394 static int codeEqualityTerm(
4395 Parse *pParse, /* The parsing context */
4396 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
4397 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
4398 int iEq, /* Index of the equality term within this level */
4399 int bRev, /* True for reverse-order IN operations */
4400 int iTarget /* Attempt to leave results in this register */
4401 ){
4402 Expr *pX = pTerm->pExpr;
4403 Vdbe *v = pParse->pVdbe;
4404 int iReg; /* Register holding results */
4405
4406 assert( iTarget>0 );
4407 if( pX->op==TK_EQ || pX->op==TK_IS ){
4408 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
4409 }else if( pX->op==TK_ISNULL ){
4410 iReg = iTarget;
4411 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
4412 #ifndef SQLITE_OMIT_SUBQUERY
4413 }else{
4414 int eType;
4415 int iTab;
4416 struct InLoop *pIn;
4417 WhereLoop *pLoop = pLevel->pWLoop;
4418
4419 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
4420 && pLoop->u.btree.pIndex!=0
4421 && pLoop->u.btree.pIndex->aSortOrder[iEq]
4422 ){
4423 testcase( iEq==0 );
4424 testcase( bRev );
4425 bRev = !bRev;
4426 }
4427 assert( pX->op==TK_IN );
4428 iReg = iTarget;
4429 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
4430 if( eType==IN_INDEX_INDEX_DESC ){
4431 testcase( bRev );
4432 bRev = !bRev;
4433 }
4434 iTab = pX->iTable;
4435 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
4436 VdbeCoverageIf(v, bRev);
4437 VdbeCoverageIf(v, !bRev);
4438 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
4439 pLoop->wsFlags |= WHERE_IN_ABLE;
4440 if( pLevel->u.in.nIn==0 ){
4441 pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
4442 }
4443 pLevel->u.in.nIn++;
4444 pLevel->u.in.aInLoop =
4445 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
4446 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
4447 pIn = pLevel->u.in.aInLoop;
4448 if( pIn ){
4449 pIn += pLevel->u.in.nIn - 1;
4450 pIn->iCur = iTab;
4451 if( eType==IN_INDEX_ROWID ){
4452 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
4453 }else{
4454 pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
4455 }
4456 pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
4457 sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
4458 }else{
4459 pLevel->u.in.nIn = 0;
4460 }
4461 #endif
4462 }
4463 disableTerm(pLevel, pTerm);
4464 return iReg;
4465 }
4466
4467 /*
4468 ** Generate code that will evaluate all == and IN constraints for an
4469 ** index scan.
4470 **
4471 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
4472 ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
4473 ** The index has as many as three equality constraints, but in this
4474 ** example, the third "c" value is an inequality. So only two
4475 ** constraints are coded. This routine will generate code to evaluate
4476 ** a==5 and b IN (1,2,3). The current values for a and b will be stored
4477 ** in consecutive registers and the index of the first register is returned.
4478 **
4479 ** In the example above nEq==2. But this subroutine works for any value
4480 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
4481 ** The only thing it does is allocate the pLevel->iMem memory cell and
4482 ** compute the affinity string.
4483 **
4484 ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
4485 ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
4486 ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
4487 ** occurs after the nEq quality constraints.
4488 **
4489 ** This routine allocates a range of nEq+nExtraReg memory cells and returns
4490 ** the index of the first memory cell in that range. The code that
4491 ** calls this routine will use that memory range to store keys for
4492 ** start and termination conditions of the loop.
4493 ** key value of the loop. If one or more IN operators appear, then
4494 ** this routine allocates an additional nEq memory cells for internal
4495 ** use.
4496 **
4497 ** Before returning, *pzAff is set to point to a buffer containing a
4498 ** copy of the column affinity string of the index allocated using
4499 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
4500 ** with equality constraints that use BLOB or NONE affinity are set to
4501 ** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
4502 **
4503 ** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
4504 ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
4505 **
4506 ** In the example above, the index on t1(a) has TEXT affinity. But since
4507 ** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
4508 ** no conversion should be attempted before using a t2.b value as part of
4509 ** a key to search the index. Hence the first byte in the returned affinity
4510 ** string in this example would be set to SQLITE_AFF_BLOB.
4511 */
4512 static int codeAllEqualityTerms(
4513 Parse *pParse, /* Parsing context */
4514 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
4515 int bRev, /* Reverse the order of IN operators */
4516 int nExtraReg, /* Number of extra registers to allocate */
4517 char **pzAff /* OUT: Set to point to affinity string */
4518 ){
4519 u16 nEq; /* The number of == or IN constraints to code */
4520 u16 nSkip; /* Number of left-most columns to skip */
4521 Vdbe *v = pParse->pVdbe; /* The vm under construction */
4522 Index *pIdx; /* The index being used for this loop */
4523 WhereTerm *pTerm; /* A single constraint term */
4524 WhereLoop *pLoop; /* The WhereLoop object */
4525 int j; /* Loop counter */
4526 int regBase; /* Base register */
4527 int nReg; /* Number of registers to allocate */
4528 char *zAff; /* Affinity string to return */
4529
4530 /* This module is only called on query plans that use an index. */
4531 pLoop = pLevel->pWLoop;
4532 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
4533 nEq = pLoop->u.btree.nEq;
4534 nSkip = pLoop->nSkip;
4535 pIdx = pLoop->u.btree.pIndex;
4536 assert( pIdx!=0 );
4537
4538 /* Figure out how many memory cells we will need then allocate them.
4539 */
4540 regBase = pParse->nMem + 1;
4541 nReg = pLoop->u.btree.nEq + nExtraReg;
4542 pParse->nMem += nReg;
4543
4544 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
4545 if( !zAff ){
4546 pParse->db->mallocFailed = 1;
4547 }
4548
4549 if( nSkip ){
4550 int iIdxCur = pLevel->iIdxCur;
4551 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
4552 VdbeCoverageIf(v, bRev==0);
4553 VdbeCoverageIf(v, bRev!=0);
4554 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
4555 j = sqlite3VdbeAddOp0(v, OP_Goto);
4556 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
4557 iIdxCur, 0, regBase, nSkip);
4558 VdbeCoverageIf(v, bRev==0);
4559 VdbeCoverageIf(v, bRev!=0);
4560 sqlite3VdbeJumpHere(v, j);
4561 for(j=0; j<nSkip; j++){
4562 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
4563 testcase( pIdx->aiColumn[j]==XN_EXPR );
4564 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
4565 }
4566 }
4567
4568 /* Evaluate the equality constraints
4569 */
4570 assert( zAff==0 || (int)strlen(zAff)>=nEq );
4571 for(j=nSkip; j<nEq; j++){
4572 int r1;
4573 pTerm = pLoop->aLTerm[j];
4574 assert( pTerm!=0 );
4575 /* The following testcase is true for indices with redundant columns.
4576 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
4577 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
4578 testcase( pTerm->wtFlags & TERM_VIRTUAL );
4579 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
4580 if( r1!=regBase+j ){
4581 if( nReg==1 ){
4582 sqlite3ReleaseTempReg(pParse, regBase);
4583 regBase = r1;
4584 }else{
4585 sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
4586 }
4587 }
4588 testcase( pTerm->eOperator & WO_ISNULL );
4589 testcase( pTerm->eOperator & WO_IN );
4590 if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
4591 Expr *pRight = pTerm->pExpr->pRight;
4592 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
4593 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
4594 VdbeCoverage(v);
4595 }
4596 if( zAff ){
4597 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
4598 zAff[j] = SQLITE_AFF_BLOB;
4599 }
4600 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
4601 zAff[j] = SQLITE_AFF_BLOB;
4602 }
4603 }
4604 }
4605 }
4606 *pzAff = zAff;
4607 return regBase;
4608 }
4609
4610 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
4611 /*
4612 ** If the most recently coded instruction is a constant range contraint
4613 ** that originated from the LIKE optimization, then change the P3 to be
4614 ** pLoop->iLikeRepCntr and set P5.
4615 **
4616 ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
4617 ** expression: "x>='ABC' AND x<'abd'". But this requires that the range
4618 ** scan loop run twice, once for strings and a second time for BLOBs.
4619 ** The OP_String opcodes on the second pass convert the upper and lower
4620 ** bound string contants to blobs. This routine makes the necessary changes
4621 ** to the OP_String opcodes for that to happen.
4622 **
4623 ** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
4624 ** only the one pass through the string space is required, so this routine
4625 ** becomes a no-op.
4626 */
4627 static void whereLikeOptimizationStringFixup(
4628 Vdbe *v, /* prepared statement under construction */
4629 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
4630 WhereTerm *pTerm /* The upper or lower bound just coded */
4631 ){
4632 if( pTerm->wtFlags & TERM_LIKEOPT ){
4633 VdbeOp *pOp;
4634 assert( pLevel->iLikeRepCntr>0 );
4635 pOp = sqlite3VdbeGetOp(v, -1);
4636 assert( pOp!=0 );
4637 assert( pOp->opcode==OP_String8
4638 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
4639 pOp->p3 = pLevel->iLikeRepCntr;
4640 pOp->p5 = 1;
4641 }
4642 }
4643 #else
4644 # define whereLikeOptimizationStringFixup(A,B,C)
4645 #endif
4646
4647 #ifdef SQLITE_ENABLE_CURSOR_HINTS
4648 /*
4649 ** Information is passed from codeCursorHint() down to individual nodes of
4650 ** the expression tree (by sqlite3WalkExpr()) using an instance of this
4651 ** structure.
4652 */
4653 struct CCurHint {
4654 int iTabCur; /* Cursor for the main table */
4655 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
4656 Index *pIdx; /* The index used to access the table */
4657 };
4658
4659 /*
4660 ** This function is called for every node of an expression that is a candidate
4661 ** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
4662 ** the table CCurHint.iTabCur, verify that the same column can be
4663 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
4664 */
4665 static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
4666 struct CCurHint *pHint = pWalker->u.pCCurHint;
4667 assert( pHint->pIdx!=0 );
4668 if( pExpr->op==TK_COLUMN
4669 && pExpr->iTable==pHint->iTabCur
4670 && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0
4671 ){
4672 pWalker->eCode = 1;
4673 }
4674 return WRC_Continue;
4675 }
4676
4677
4678 /*
4679 ** This function is called on every node of an expression tree used as an
4680 ** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
4681 ** that accesses any table other than the one identified by
4682 ** CCurHint.iTabCur, then do the following:
4683 **
4684 ** 1) allocate a register and code an OP_Column instruction to read
4685 ** the specified column into the new register, and
4686 **
4687 ** 2) transform the expression node to a TK_REGISTER node that reads
4688 ** from the newly populated register.
4689 **
4690 ** Also, if the node is a TK_COLUMN that does access the table idenified
4691 ** by pCCurHint.iTabCur, and an index is being used (which we will
4692 ** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
4693 ** an access of the index rather than the original table.
4694 */
4695 static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
4696 int rc = WRC_Continue;
4697 struct CCurHint *pHint = pWalker->u.pCCurHint;
4698 if( pExpr->op==TK_COLUMN ){
4699 if( pExpr->iTable!=pHint->iTabCur ){
4700 Vdbe *v = pWalker->pParse->pVdbe;
4701 int reg = ++pWalker->pParse->nMem; /* Register for column value */
4702 sqlite3ExprCodeGetColumnOfTable(
4703 v, pExpr->pTab, pExpr->iTable, pExpr->iColumn, reg
4704 );
4705 pExpr->op = TK_REGISTER;
4706 pExpr->iTable = reg;
4707 }else if( pHint->pIdx!=0 ){
4708 pExpr->iTable = pHint->iIdxCur;
4709 pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn);
4710 assert( pExpr->iColumn>=0 );
4711 }
4712 }else if( pExpr->op==TK_AGG_FUNCTION ){
4713 /* An aggregate function in the WHERE clause of a query means this must
4714 ** be a correlated sub-query, and expression pExpr is an aggregate from
4715 ** the parent context. Do not walk the function arguments in this case.
4716 **
4717 ** todo: It should be possible to replace this node with a TK_REGISTER
4718 ** expression, as the result of the expression must be stored in a
4719 ** register at this point. The same holds for TK_AGG_COLUMN nodes. */
4720 rc = WRC_Prune;
4721 }
4722 return rc;
4723 }
4724
4725 /*
4726 ** Insert an OP_CursorHint instruction if it is appropriate to do so.
4727 */
4728 static void codeCursorHint(
4729 WhereInfo *pWInfo, /* The where clause */
4730 WhereLevel *pLevel, /* Which loop to provide hints for */
4731 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
4732 ){
4733 Parse *pParse = pWInfo->pParse;
4734 sqlite3 *db = pParse->db;
4735 Vdbe *v = pParse->pVdbe;
4736 Expr *pExpr = 0;
4737 WhereLoop *pLoop = pLevel->pWLoop;
4738 int iCur;
4739 WhereClause *pWC;
4740 WhereTerm *pTerm;
4741 int i, j;
4742 struct CCurHint sHint;
4743 Walker sWalker;
4744
4745 if( OptimizationDisabled(db, SQLITE_CursorHints) ) return;
4746 iCur = pLevel->iTabCur;
4747 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
4748 sHint.iTabCur = iCur;
4749 sHint.iIdxCur = pLevel->iIdxCur;
4750 sHint.pIdx = pLoop->u.btree.pIndex;
4751 memset(&sWalker, 0, sizeof(sWalker));
4752 sWalker.pParse = pParse;
4753 sWalker.u.pCCurHint = &sHint;
4754 pWC = &pWInfo->sWC;
4755 for(i=0; i<pWC->nTerm; i++){
4756 pTerm = &pWC->a[i];
4757 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
4758 if( pTerm->prereqAll & pLevel->notReady ) continue;
4759 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
4760
4761 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
4762 ** the cursor. These terms are not needed as hints for a pure range
4763 ** scan (that has no == terms) so omit them. */
4764 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
4765 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
4766 if( j<pLoop->nLTerm ) continue;
4767 }
4768
4769 /* No subqueries or non-deterministic functions allowed */
4770 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
4771
4772 /* For an index scan, make sure referenced columns are actually in
4773 ** the index. */
4774 if( sHint.pIdx!=0 ){
4775 sWalker.eCode = 0;
4776 sWalker.xExprCallback = codeCursorHintCheckExpr;
4777 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
4778 if( sWalker.eCode ) continue;
4779 }
4780
4781 /* If we survive all prior tests, that means this term is worth hinting */
4782 pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
4783 }
4784 if( pExpr!=0 ){
4785 sWalker.xExprCallback = codeCursorHintFixExpr;
4786 sqlite3WalkExpr(&sWalker, pExpr);
4787 sqlite3VdbeAddOp4(v, OP_CursorHint,
4788 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
4789 (const char*)pExpr, P4_EXPR);
4790 }
4791 }
4792 #else
4793 # define codeCursorHint(A,B,C) /* No-op */
4794 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
4795
4796 /*
4797 ** Generate code for the start of the iLevel-th loop in the WHERE clause
4798 ** implementation described by pWInfo.
4799 */
4800 SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
4801 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
4802 int iLevel, /* Which level of pWInfo->a[] should be coded */
4803 Bitmask notReady /* Which tables are currently available */
4804 ){
4805 int j, k; /* Loop counters */
4806 int iCur; /* The VDBE cursor for the table */
4807 int addrNxt; /* Where to jump to continue with the next IN case */
4808 int omitTable; /* True if we use the index only */
4809 int bRev; /* True if we need to scan in reverse order */
4810 WhereLevel *pLevel; /* The where level to be coded */
4811 WhereLoop *pLoop; /* The WhereLoop object being coded */
4812 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
4813 WhereTerm *pTerm; /* A WHERE clause term */
4814 Parse *pParse; /* Parsing context */
4815 sqlite3 *db; /* Database connection */
4816 Vdbe *v; /* The prepared stmt under constructions */
4817 struct SrcList_item *pTabItem; /* FROM clause term being coded */
4818 int addrBrk; /* Jump here to break out of the loop */
4819 int addrCont; /* Jump here to continue with next cycle */
4820 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
4821 int iReleaseReg = 0; /* Temp register to free before returning */
4822
4823 pParse = pWInfo->pParse;
4824 v = pParse->pVdbe;
4825 pWC = &pWInfo->sWC;
4826 db = pParse->db;
4827 pLevel = &pWInfo->a[iLevel];
4828 pLoop = pLevel->pWLoop;
4829 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
4830 iCur = pTabItem->iCursor;
4831 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
4832 bRev = (pWInfo->revMask>>iLevel)&1;
4833 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
4834 && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
4835 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
4836
4837 /* Create labels for the "break" and "continue" instructions
4838 ** for the current loop. Jump to addrBrk to break out of a loop.
4839 ** Jump to cont to go immediately to the next iteration of the
4840 ** loop.
4841 **
4842 ** When there is an IN operator, we also have a "addrNxt" label that
4843 ** means to continue with the next IN value combination. When
4844 ** there are no IN operators in the constraints, the "addrNxt" label
4845 ** is the same as "addrBrk".
4846 */
4847 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
4848 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
4849
4850 /* If this is the right table of a LEFT OUTER JOIN, allocate and
4851 ** initialize a memory cell that records if this table matches any
4852 ** row of the left table of the join.
4853 */
4854 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
4855 pLevel->iLeftJoin = ++pParse->nMem;
4856 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
4857 VdbeComment((v, "init LEFT JOIN no-match flag"));
4858 }
4859
4860 /* Special case of a FROM clause subquery implemented as a co-routine */
4861 if( pTabItem->fg.viaCoroutine ){
4862 int regYield = pTabItem->regReturn;
4863 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
4864 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
4865 VdbeCoverage(v);
4866 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
4867 pLevel->op = OP_Goto;
4868 }else
4869
4870 #ifndef SQLITE_OMIT_VIRTUALTABLE
4871 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
4872 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
4873 ** to access the data.
4874 */
4875 int iReg; /* P3 Value for OP_VFilter */
4876 int addrNotFound;
4877 int nConstraint = pLoop->nLTerm;
4878
4879 sqlite3ExprCachePush(pParse);
4880 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
4881 addrNotFound = pLevel->addrBrk;
4882 for(j=0; j<nConstraint; j++){
4883 int iTarget = iReg+j+2;
4884 pTerm = pLoop->aLTerm[j];
4885 if( pTerm==0 ) continue;
4886 if( pTerm->eOperator & WO_IN ){
4887 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
4888 addrNotFound = pLevel->addrNxt;
4889 }else{
4890 sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
4891 }
4892 }
4893 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
4894 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
4895 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
4896 pLoop->u.vtab.idxStr,
4897 pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
4898 VdbeCoverage(v);
4899 pLoop->u.vtab.needFree = 0;
4900 for(j=0; j<nConstraint && j<16; j++){
4901 if( (pLoop->u.vtab.omitMask>>j)&1 ){
4902 disableTerm(pLevel, pLoop->aLTerm[j]);
4903 }
4904 }
4905 pLevel->p1 = iCur;
4906 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
4907 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
4908 sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
4909 sqlite3ExprCachePop(pParse);
4910 }else
4911 #endif /* SQLITE_OMIT_VIRTUALTABLE */
4912
4913 if( (pLoop->wsFlags & WHERE_IPK)!=0
4914 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
4915 ){
4916 /* Case 2: We can directly reference a single row using an
4917 ** equality comparison against the ROWID field. Or
4918 ** we reference multiple rows using a "rowid IN (...)"
4919 ** construct.
4920 */
4921 assert( pLoop->u.btree.nEq==1 );
4922 pTerm = pLoop->aLTerm[0];
4923 assert( pTerm!=0 );
4924 assert( pTerm->pExpr!=0 );
4925 assert( omitTable==0 );
4926 testcase( pTerm->wtFlags & TERM_VIRTUAL );
4927 iReleaseReg = ++pParse->nMem;
4928 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
4929 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
4930 addrNxt = pLevel->addrNxt;
4931 sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
4932 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
4933 VdbeCoverage(v);
4934 sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
4935 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
4936 VdbeComment((v, "pk"));
4937 pLevel->op = OP_Noop;
4938 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
4939 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
4940 ){
4941 /* Case 3: We have an inequality comparison against the ROWID field.
4942 */
4943 int testOp = OP_Noop;
4944 int start;
4945 int memEndValue = 0;
4946 WhereTerm *pStart, *pEnd;
4947
4948 assert( omitTable==0 );
4949 j = 0;
4950 pStart = pEnd = 0;
4951 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
4952 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
4953 assert( pStart!=0 || pEnd!=0 );
4954 if( bRev ){
4955 pTerm = pStart;
4956 pStart = pEnd;
4957 pEnd = pTerm;
4958 }
4959 codeCursorHint(pWInfo, pLevel, pEnd);
4960 if( pStart ){
4961 Expr *pX; /* The expression that defines the start bound */
4962 int r1, rTemp; /* Registers for holding the start boundary */
4963
4964 /* The following constant maps TK_xx codes into corresponding
4965 ** seek opcodes. It depends on a particular ordering of TK_xx
4966 */
4967 const u8 aMoveOp[] = {
4968 /* TK_GT */ OP_SeekGT,
4969 /* TK_LE */ OP_SeekLE,
4970 /* TK_LT */ OP_SeekLT,
4971 /* TK_GE */ OP_SeekGE
4972 };
4973 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
4974 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
4975 assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
4976
4977 assert( (pStart->wtFlags & TERM_VNULL)==0 );
4978 testcase( pStart->wtFlags & TERM_VIRTUAL );
4979 pX = pStart->pExpr;
4980 assert( pX!=0 );
4981 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
4982 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
4983 sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
4984 VdbeComment((v, "pk"));
4985 VdbeCoverageIf(v, pX->op==TK_GT);
4986 VdbeCoverageIf(v, pX->op==TK_LE);
4987 VdbeCoverageIf(v, pX->op==TK_LT);
4988 VdbeCoverageIf(v, pX->op==TK_GE);
4989 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
4990 sqlite3ReleaseTempReg(pParse, rTemp);
4991 disableTerm(pLevel, pStart);
4992 }else{
4993 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
4994 VdbeCoverageIf(v, bRev==0);
4995 VdbeCoverageIf(v, bRev!=0);
4996 }
4997 if( pEnd ){
4998 Expr *pX;
4999 pX = pEnd->pExpr;
5000 assert( pX!=0 );
5001 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
5002 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
5003 testcase( pEnd->wtFlags & TERM_VIRTUAL );
5004 memEndValue = ++pParse->nMem;
5005 sqlite3ExprCode(pParse, pX->pRight, memEndValue);
5006 if( pX->op==TK_LT || pX->op==TK_GT ){
5007 testOp = bRev ? OP_Le : OP_Ge;
5008 }else{
5009 testOp = bRev ? OP_Lt : OP_Gt;
5010 }
5011 disableTerm(pLevel, pEnd);
5012 }
5013 start = sqlite3VdbeCurrentAddr(v);
5014 pLevel->op = bRev ? OP_Prev : OP_Next;
5015 pLevel->p1 = iCur;
5016 pLevel->p2 = start;
5017 assert( pLevel->p5==0 );
5018 if( testOp!=OP_Noop ){
5019 iRowidReg = ++pParse->nMem;
5020 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
5021 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
5022 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
5023 VdbeCoverageIf(v, testOp==OP_Le);
5024 VdbeCoverageIf(v, testOp==OP_Lt);
5025 VdbeCoverageIf(v, testOp==OP_Ge);
5026 VdbeCoverageIf(v, testOp==OP_Gt);
5027 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
5028 }
5029 }else if( pLoop->wsFlags & WHERE_INDEXED ){
5030 /* Case 4: A scan using an index.
5031 **
5032 ** The WHERE clause may contain zero or more equality
5033 ** terms ("==" or "IN" operators) that refer to the N
5034 ** left-most columns of the index. It may also contain
5035 ** inequality constraints (>, <, >= or <=) on the indexed
5036 ** column that immediately follows the N equalities. Only
5037 ** the right-most column can be an inequality - the rest must
5038 ** use the "==" and "IN" operators. For example, if the
5039 ** index is on (x,y,z), then the following clauses are all
5040 ** optimized:
5041 **
5042 ** x=5
5043 ** x=5 AND y=10
5044 ** x=5 AND y<10
5045 ** x=5 AND y>5 AND y<10
5046 ** x=5 AND y=5 AND z<=10
5047 **
5048 ** The z<10 term of the following cannot be used, only
5049 ** the x=5 term:
5050 **
5051 ** x=5 AND z<10
5052 **
5053 ** N may be zero if there are inequality constraints.
5054 ** If there are no inequality constraints, then N is at
5055 ** least one.
5056 **
5057 ** This case is also used when there are no WHERE clause
5058 ** constraints but an index is selected anyway, in order
5059 ** to force the output order to conform to an ORDER BY.
5060 */
5061 static const u8 aStartOp[] = {
5062 0,
5063 0,
5064 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
5065 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
5066 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
5067 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
5068 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
5069 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
5070 };
5071 static const u8 aEndOp[] = {
5072 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
5073 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
5074 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
5075 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
5076 };
5077 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
5078 int regBase; /* Base register holding constraint values */
5079 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
5080 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
5081 int startEq; /* True if range start uses ==, >= or <= */
5082 int endEq; /* True if range end uses ==, >= or <= */
5083 int start_constraints; /* Start of range is constrained */
5084 int nConstraint; /* Number of constraint terms */
5085 Index *pIdx; /* The index we will be using */
5086 int iIdxCur; /* The VDBE cursor for the index */
5087 int nExtraReg = 0; /* Number of extra registers needed */
5088 int op; /* Instruction opcode */
5089 char *zStartAff; /* Affinity for start of range constraint */
5090 char cEndAff = 0; /* Affinity for end of range constraint */
5091 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
5092 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
5093
5094 pIdx = pLoop->u.btree.pIndex;
5095 iIdxCur = pLevel->iIdxCur;
5096 assert( nEq>=pLoop->nSkip );
5097
5098 /* If this loop satisfies a sort order (pOrderBy) request that
5099 ** was passed to this function to implement a "SELECT min(x) ..."
5100 ** query, then the caller will only allow the loop to run for
5101 ** a single iteration. This means that the first row returned
5102 ** should not have a NULL value stored in 'x'. If column 'x' is
5103 ** the first one after the nEq equality constraints in the index,
5104 ** this requires some special handling.
5105 */
5106 assert( pWInfo->pOrderBy==0
5107 || pWInfo->pOrderBy->nExpr==1
5108 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
5109 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
5110 && pWInfo->nOBSat>0
5111 && (pIdx->nKeyCol>nEq)
5112 ){
5113 assert( pLoop->nSkip==0 );
5114 bSeekPastNull = 1;
5115 nExtraReg = 1;
5116 }
5117
5118 /* Find any inequality constraint terms for the start and end
5119 ** of the range.
5120 */
5121 j = nEq;
5122 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
5123 pRangeStart = pLoop->aLTerm[j++];
5124 nExtraReg = 1;
5125 /* Like optimization range constraints always occur in pairs */
5126 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
5127 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
5128 }
5129 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
5130 pRangeEnd = pLoop->aLTerm[j++];
5131 nExtraReg = 1;
5132 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
5133 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
5134 assert( pRangeStart!=0 ); /* LIKE opt constraints */
5135 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
5136 pLevel->iLikeRepCntr = ++pParse->nMem;
5137 testcase( bRev );
5138 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
5139 sqlite3VdbeAddOp2(v, OP_Integer,
5140 bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
5141 pLevel->iLikeRepCntr);
5142 VdbeComment((v, "LIKE loop counter"));
5143 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
5144 }
5145 #endif
5146 if( pRangeStart==0
5147 && (j = pIdx->aiColumn[nEq])>=0
5148 && pIdx->pTable->aCol[j].notNull==0
5149 ){
5150 bSeekPastNull = 1;
5151 }
5152 }
5153 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
5154
5155 /* If we are doing a reverse order scan on an ascending index, or
5156 ** a forward order scan on a descending index, interchange the
5157 ** start and end terms (pRangeStart and pRangeEnd).
5158 */
5159 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
5160 || (bRev && pIdx->nKeyCol==nEq)
5161 ){
5162 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
5163 SWAP(u8, bSeekPastNull, bStopAtNull);
5164 }
5165
5166 /* Generate code to evaluate all constraint terms using == or IN
5167 ** and store the values of those terms in an array of registers
5168 ** starting at regBase.
5169 */
5170 codeCursorHint(pWInfo, pLevel, pRangeEnd);
5171 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
5172 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
5173 if( zStartAff ) cEndAff = zStartAff[nEq];
5174 addrNxt = pLevel->addrNxt;
5175
5176 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
5177 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
5178 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
5179 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
5180 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
5181 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
5182 start_constraints = pRangeStart || nEq>0;
5183
5184 /* Seek the index cursor to the start of the range. */
5185 nConstraint = nEq;
5186 if( pRangeStart ){
5187 Expr *pRight = pRangeStart->pExpr->pRight;
5188 sqlite3ExprCode(pParse, pRight, regBase+nEq);
5189 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
5190 if( (pRangeStart->wtFlags & TERM_VNULL)==0
5191 && sqlite3ExprCanBeNull(pRight)
5192 ){
5193 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
5194 VdbeCoverage(v);
5195 }
5196 if( zStartAff ){
5197 if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){
5198 /* Since the comparison is to be performed with no conversions
5199 ** applied to the operands, set the affinity to apply to pRight to
5200 ** SQLITE_AFF_BLOB. */
5201 zStartAff[nEq] = SQLITE_AFF_BLOB;
5202 }
5203 if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
5204 zStartAff[nEq] = SQLITE_AFF_BLOB;
5205 }
5206 }
5207 nConstraint++;
5208 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
5209 }else if( bSeekPastNull ){
5210 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
5211 nConstraint++;
5212 startEq = 0;
5213 start_constraints = 1;
5214 }
5215 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
5216 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
5217 assert( op!=0 );
5218 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
5219 VdbeCoverage(v);
5220 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
5221 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
5222 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
5223 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
5224 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
5225 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
5226
5227 /* Load the value for the inequality constraint at the end of the
5228 ** range (if any).
5229 */
5230 nConstraint = nEq;
5231 if( pRangeEnd ){
5232 Expr *pRight = pRangeEnd->pExpr->pRight;
5233 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
5234 sqlite3ExprCode(pParse, pRight, regBase+nEq);
5235 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
5236 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
5237 && sqlite3ExprCanBeNull(pRight)
5238 ){
5239 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
5240 VdbeCoverage(v);
5241 }
5242 if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB
5243 && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
5244 ){
5245 codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
5246 }
5247 nConstraint++;
5248 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
5249 }else if( bStopAtNull ){
5250 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
5251 endEq = 0;
5252 nConstraint++;
5253 }
5254 sqlite3DbFree(db, zStartAff);
5255
5256 /* Top of the loop body */
5257 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
5258
5259 /* Check if the index cursor is past the end of the range. */
5260 if( nConstraint ){
5261 op = aEndOp[bRev*2 + endEq];
5262 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
5263 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
5264 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
5265 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
5266 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
5267 }
5268
5269 /* Seek the table cursor, if required */
5270 disableTerm(pLevel, pRangeStart);
5271 disableTerm(pLevel, pRangeEnd);
5272 if( omitTable ){
5273 /* pIdx is a covering index. No need to access the main table. */
5274 }else if( HasRowid(pIdx->pTable) ){
5275 iRowidReg = ++pParse->nMem;
5276 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
5277 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
5278 if( pWInfo->eOnePass!=ONEPASS_OFF ){
5279 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
5280 VdbeCoverage(v);
5281 }else{
5282 sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
5283 }
5284 }else if( iCur!=iIdxCur ){
5285 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
5286 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
5287 for(j=0; j<pPk->nKeyCol; j++){
5288 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
5289 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
5290 }
5291 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
5292 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
5293 }
5294
5295 /* Record the instruction used to terminate the loop. Disable
5296 ** WHERE clause terms made redundant by the index range scan.
5297 */
5298 if( pLoop->wsFlags & WHERE_ONEROW ){
5299 pLevel->op = OP_Noop;
5300 }else if( bRev ){
5301 pLevel->op = OP_Prev;
5302 }else{
5303 pLevel->op = OP_Next;
5304 }
5305 pLevel->p1 = iIdxCur;
5306 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
5307 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
5308 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
5309 }else{
5310 assert( pLevel->p5==0 );
5311 }
5312 }else
5313
5314 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
5315 if( pLoop->wsFlags & WHERE_MULTI_OR ){
5316 /* Case 5: Two or more separately indexed terms connected by OR
5317 **
5318 ** Example:
5319 **
5320 ** CREATE TABLE t1(a,b,c,d);
5321 ** CREATE INDEX i1 ON t1(a);
5322 ** CREATE INDEX i2 ON t1(b);
5323 ** CREATE INDEX i3 ON t1(c);
5324 **
5325 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
5326 **
5327 ** In the example, there are three indexed terms connected by OR.
5328 ** The top of the loop looks like this:
5329 **
5330 ** Null 1 # Zero the rowset in reg 1
5331 **
5332 ** Then, for each indexed term, the following. The arguments to
5333 ** RowSetTest are such that the rowid of the current row is inserted
5334 ** into the RowSet. If it is already present, control skips the
5335 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
5336 **
5337 ** sqlite3WhereBegin(<term>)
5338 ** RowSetTest # Insert rowid into rowset
5339 ** Gosub 2 A
5340 ** sqlite3WhereEnd()
5341 **
5342 ** Following the above, code to terminate the loop. Label A, the target
5343 ** of the Gosub above, jumps to the instruction right after the Goto.
5344 **
5345 ** Null 1 # Zero the rowset in reg 1
5346 ** Goto B # The loop is finished.
5347 **
5348 ** A: <loop body> # Return data, whatever.
5349 **
5350 ** Return 2 # Jump back to the Gosub
5351 **
5352 ** B: <after the loop>
5353 **
5354 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
5355 ** use an ephemeral index instead of a RowSet to record the primary
5356 ** keys of the rows we have already seen.
5357 **
5358 */
5359 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
5360 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
5361 Index *pCov = 0; /* Potential covering index (or NULL) */
5362 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
5363
5364 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
5365 int regRowset = 0; /* Register for RowSet object */
5366 int regRowid = 0; /* Register holding rowid */
5367 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
5368 int iRetInit; /* Address of regReturn init */
5369 int untestedTerms = 0; /* Some terms not completely tested */
5370 int ii; /* Loop counter */
5371 u16 wctrlFlags; /* Flags for sub-WHERE clause */
5372 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
5373 Table *pTab = pTabItem->pTab;
5374
5375 pTerm = pLoop->aLTerm[0];
5376 assert( pTerm!=0 );
5377 assert( pTerm->eOperator & WO_OR );
5378 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
5379 pOrWc = &pTerm->u.pOrInfo->wc;
5380 pLevel->op = OP_Return;
5381 pLevel->p1 = regReturn;
5382
5383 /* Set up a new SrcList in pOrTab containing the table being scanned
5384 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
5385 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
5386 */
5387 if( pWInfo->nLevel>1 ){
5388 int nNotReady; /* The number of notReady tables */
5389 struct SrcList_item *origSrc; /* Original list of tables */
5390 nNotReady = pWInfo->nLevel - iLevel - 1;
5391 pOrTab = sqlite3StackAllocRaw(db,
5392 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
5393 if( pOrTab==0 ) return notReady;
5394 pOrTab->nAlloc = (u8)(nNotReady + 1);
5395 pOrTab->nSrc = pOrTab->nAlloc;
5396 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
5397 origSrc = pWInfo->pTabList->a;
5398 for(k=1; k<=nNotReady; k++){
5399 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
5400 }
5401 }else{
5402 pOrTab = pWInfo->pTabList;
5403 }
5404
5405 /* Initialize the rowset register to contain NULL. An SQL NULL is
5406 ** equivalent to an empty rowset. Or, create an ephemeral index
5407 ** capable of holding primary keys in the case of a WITHOUT ROWID.
5408 **
5409 ** Also initialize regReturn to contain the address of the instruction
5410 ** immediately following the OP_Return at the bottom of the loop. This
5411 ** is required in a few obscure LEFT JOIN cases where control jumps
5412 ** over the top of the loop into the body of it. In this case the
5413 ** correct response for the end-of-loop code (the OP_Return) is to
5414 ** fall through to the next instruction, just as an OP_Next does if
5415 ** called on an uninitialized cursor.
5416 */
5417 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
5418 if( HasRowid(pTab) ){
5419 regRowset = ++pParse->nMem;
5420 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
5421 }else{
5422 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
5423 regRowset = pParse->nTab++;
5424 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
5425 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
5426 }
5427 regRowid = ++pParse->nMem;
5428 }
5429 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
5430
5431 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
5432 ** Then for every term xN, evaluate as the subexpression: xN AND z
5433 ** That way, terms in y that are factored into the disjunction will
5434 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
5435 **
5436 ** Actually, each subexpression is converted to "xN AND w" where w is
5437 ** the "interesting" terms of z - terms that did not originate in the
5438 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
5439 ** indices.
5440 **
5441 ** This optimization also only applies if the (x1 OR x2 OR ...) term
5442 ** is not contained in the ON clause of a LEFT JOIN.
5443 ** See ticket http://www.sqlite.org/src/info/f2369304e4
5444 */
5445 if( pWC->nTerm>1 ){
5446 int iTerm;
5447 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
5448 Expr *pExpr = pWC->a[iTerm].pExpr;
5449 if( &pWC->a[iTerm] == pTerm ) continue;
5450 if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
5451 if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
5452 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
5453 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
5454 pExpr = sqlite3ExprDup(db, pExpr, 0);
5455 pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
5456 }
5457 if( pAndExpr ){
5458 pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr, 0);
5459 }
5460 }
5461
5462 /* Run a separate WHERE clause for each term of the OR clause. After
5463 ** eliminating duplicates from other WHERE clauses, the action for each
5464 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
5465 */
5466 wctrlFlags = WHERE_OMIT_OPEN_CLOSE
5467 | WHERE_FORCE_TABLE
5468 | WHERE_ONETABLE_ONLY
5469 | WHERE_NO_AUTOINDEX;
5470 for(ii=0; ii<pOrWc->nTerm; ii++){
5471 WhereTerm *pOrTerm = &pOrWc->a[ii];
5472 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
5473 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
5474 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
5475 int jmp1 = 0; /* Address of jump operation */
5476 if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
5477 pAndExpr->pLeft = pOrExpr;
5478 pOrExpr = pAndExpr;
5479 }
5480 /* Loop through table entries that match term pOrTerm. */
5481 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
5482 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
5483 wctrlFlags, iCovCur);
5484 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
5485 if( pSubWInfo ){
5486 WhereLoop *pSubLoop;
5487 int addrExplain = sqlite3WhereExplainOneScan(
5488 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
5489 );
5490 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
5491
5492 /* This is the sub-WHERE clause body. First skip over
5493 ** duplicate rows from prior sub-WHERE clauses, and record the
5494 ** rowid (or PRIMARY KEY) for the current row so that the same
5495 ** row will be skipped in subsequent sub-WHERE clauses.
5496 */
5497 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
5498 int r;
5499 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
5500 if( HasRowid(pTab) ){
5501 r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
5502 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0,
5503 r,iSet);
5504 VdbeCoverage(v);
5505 }else{
5506 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
5507 int nPk = pPk->nKeyCol;
5508 int iPk;
5509
5510 /* Read the PK into an array of temp registers. */
5511 r = sqlite3GetTempRange(pParse, nPk);
5512 for(iPk=0; iPk<nPk; iPk++){
5513 int iCol = pPk->aiColumn[iPk];
5514 sqlite3ExprCodeGetColumnToReg(pParse, pTab, iCol, iCur, r+iPk);
5515 }
5516
5517 /* Check if the temp table already contains this key. If so,
5518 ** the row has already been included in the result set and
5519 ** can be ignored (by jumping past the Gosub below). Otherwise,
5520 ** insert the key into the temp table and proceed with processing
5521 ** the row.
5522 **
5523 ** Use some of the same optimizations as OP_RowSetTest: If iSet
5524 ** is zero, assume that the key cannot already be present in
5525 ** the temp table. And if iSet is -1, assume that there is no
5526 ** need to insert the key into the temp table, as it will never
5527 ** be tested for. */
5528 if( iSet ){
5529 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
5530 VdbeCoverage(v);
5531 }
5532 if( iSet>=0 ){
5533 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
5534 sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
5535 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
5536 }
5537
5538 /* Release the array of temp registers */
5539 sqlite3ReleaseTempRange(pParse, r, nPk);
5540 }
5541 }
5542
5543 /* Invoke the main loop body as a subroutine */
5544 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
5545
5546 /* Jump here (skipping the main loop body subroutine) if the
5547 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
5548 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
5549
5550 /* The pSubWInfo->untestedTerms flag means that this OR term
5551 ** contained one or more AND term from a notReady table. The
5552 ** terms from the notReady table could not be tested and will
5553 ** need to be tested later.
5554 */
5555 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
5556
5557 /* If all of the OR-connected terms are optimized using the same
5558 ** index, and the index is opened using the same cursor number
5559 ** by each call to sqlite3WhereBegin() made by this loop, it may
5560 ** be possible to use that index as a covering index.
5561 **
5562 ** If the call to sqlite3WhereBegin() above resulted in a scan that
5563 ** uses an index, and this is either the first OR-connected term
5564 ** processed or the index is the same as that used by all previous
5565 ** terms, set pCov to the candidate covering index. Otherwise, set
5566 ** pCov to NULL to indicate that no candidate covering index will
5567 ** be available.
5568 */
5569 pSubLoop = pSubWInfo->a[0].pWLoop;
5570 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
5571 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
5572 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
5573 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
5574 ){
5575 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
5576 pCov = pSubLoop->u.btree.pIndex;
5577 wctrlFlags |= WHERE_REOPEN_IDX;
5578 }else{
5579 pCov = 0;
5580 }
5581
5582 /* Finish the loop through table entries that match term pOrTerm. */
5583 sqlite3WhereEnd(pSubWInfo);
5584 }
5585 }
5586 }
5587 pLevel->u.pCovidx = pCov;
5588 if( pCov ) pLevel->iIdxCur = iCovCur;
5589 if( pAndExpr ){
5590 pAndExpr->pLeft = 0;
5591 sqlite3ExprDelete(db, pAndExpr);
5592 }
5593 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
5594 sqlite3VdbeGoto(v, pLevel->addrBrk);
5595 sqlite3VdbeResolveLabel(v, iLoopBody);
5596
5597 if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
5598 if( !untestedTerms ) disableTerm(pLevel, pTerm);
5599 }else
5600 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
5601
5602 {
5603 /* Case 6: There is no usable index. We must do a complete
5604 ** scan of the entire table.
5605 */
5606 static const u8 aStep[] = { OP_Next, OP_Prev };
5607 static const u8 aStart[] = { OP_Rewind, OP_Last };
5608 assert( bRev==0 || bRev==1 );
5609 if( pTabItem->fg.isRecursive ){
5610 /* Tables marked isRecursive have only a single row that is stored in
5611 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
5612 pLevel->op = OP_Noop;
5613 }else{
5614 codeCursorHint(pWInfo, pLevel, 0);
5615 pLevel->op = aStep[bRev];
5616 pLevel->p1 = iCur;
5617 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
5618 VdbeCoverageIf(v, bRev==0);
5619 VdbeCoverageIf(v, bRev!=0);
5620 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
5621 }
5622 }
5623
5624 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
5625 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
5626 #endif
5627
5628 /* Insert code to test every subexpression that can be completely
5629 ** computed using the current set of tables.
5630 */
5631 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
5632 Expr *pE;
5633 int skipLikeAddr = 0;
5634 testcase( pTerm->wtFlags & TERM_VIRTUAL );
5635 testcase( pTerm->wtFlags & TERM_CODED );
5636 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
5637 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
5638 testcase( pWInfo->untestedTerms==0
5639 && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
5640 pWInfo->untestedTerms = 1;
5641 continue;
5642 }
5643 pE = pTerm->pExpr;
5644 assert( pE!=0 );
5645 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
5646 continue;
5647 }
5648 if( pTerm->wtFlags & TERM_LIKECOND ){
5649 #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
5650 continue;
5651 #else
5652 assert( pLevel->iLikeRepCntr>0 );
5653 skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
5654 VdbeCoverage(v);
5655 #endif
5656 }
5657 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
5658 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
5659 pTerm->wtFlags |= TERM_CODED;
5660 }
5661
5662 /* Insert code to test for implied constraints based on transitivity
5663 ** of the "==" operator.
5664 **
5665 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
5666 ** and we are coding the t1 loop and the t2 loop has not yet coded,
5667 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
5668 ** the implied "t1.a=123" constraint.
5669 */
5670 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
5671 Expr *pE, *pEAlt;
5672 WhereTerm *pAlt;
5673 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
5674 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
5675 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
5676 if( pTerm->leftCursor!=iCur ) continue;
5677 if( pLevel->iLeftJoin ) continue;
5678 pE = pTerm->pExpr;
5679 assert( !ExprHasProperty(pE, EP_FromJoin) );
5680 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
5681 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
5682 WO_EQ|WO_IN|WO_IS, 0);
5683 if( pAlt==0 ) continue;
5684 if( pAlt->wtFlags & (TERM_CODED) ) continue;
5685 testcase( pAlt->eOperator & WO_EQ );
5686 testcase( pAlt->eOperator & WO_IS );
5687 testcase( pAlt->eOperator & WO_IN );
5688 VdbeModuleComment((v, "begin transitive constraint"));
5689 pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
5690 if( pEAlt ){
5691 *pEAlt = *pAlt->pExpr;
5692 pEAlt->pLeft = pE->pLeft;
5693 sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
5694 sqlite3StackFree(db, pEAlt);
5695 }
5696 }
5697
5698 /* For a LEFT OUTER JOIN, generate code that will record the fact that
5699 ** at least one row of the right table has matched the left table.
5700 */
5701 if( pLevel->iLeftJoin ){
5702 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
5703 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
5704 VdbeComment((v, "record LEFT JOIN hit"));
5705 sqlite3ExprCacheClear(pParse);
5706 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
5707 testcase( pTerm->wtFlags & TERM_VIRTUAL );
5708 testcase( pTerm->wtFlags & TERM_CODED );
5709 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
5710 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
5711 assert( pWInfo->untestedTerms );
5712 continue;
5713 }
5714 assert( pTerm->pExpr );
5715 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
5716 pTerm->wtFlags |= TERM_CODED;
5717 }
5718 }
5719
5720 return pLevel->notReady;
5721 }
5722
5723 /************** End of wherecode.c *******************************************/
5724 /************** Begin file whereexpr.c ***************************************/
5725 /*
5726 ** 2015-06-08
5727 **
5728 ** The author disclaims copyright to this source code. In place of
5729 ** a legal notice, here is a blessing:
5730 **
5731 ** May you do good and not evil.
5732 ** May you find forgiveness for yourself and forgive others.
5733 ** May you share freely, never taking more than you give.
5734 **
5735 *************************************************************************
5736 ** This module contains C code that generates VDBE code used to process
5737 ** the WHERE clause of SQL statements.
5738 **
5739 ** This file was originally part of where.c but was split out to improve
5740 ** readability and editabiliity. This file contains utility routines for
5741 ** analyzing Expr objects in the WHERE clause.
5742 */
5743 /* #include "sqliteInt.h" */
5744 /* #include "whereInt.h" */
5745
5746 /* Forward declarations */
5747 static void exprAnalyze(SrcList*, WhereClause*, int);
5748
5749 /*
5750 ** Deallocate all memory associated with a WhereOrInfo object.
5751 */
5752 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
5753 sqlite3WhereClauseClear(&p->wc);
5754 sqlite3DbFree(db, p);
5755 }
5756
5757 /*
5758 ** Deallocate all memory associated with a WhereAndInfo object.
5759 */
5760 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
5761 sqlite3WhereClauseClear(&p->wc);
5762 sqlite3DbFree(db, p);
5763 }
5764
5765 /*
5766 ** Add a single new WhereTerm entry to the WhereClause object pWC.
5767 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
5768 ** The index in pWC->a[] of the new WhereTerm is returned on success.
5769 ** 0 is returned if the new WhereTerm could not be added due to a memory
5770 ** allocation error. The memory allocation failure will be recorded in
5771 ** the db->mallocFailed flag so that higher-level functions can detect it.
5772 **
5773 ** This routine will increase the size of the pWC->a[] array as necessary.
5774 **
5775 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
5776 ** for freeing the expression p is assumed by the WhereClause object pWC.
5777 ** This is true even if this routine fails to allocate a new WhereTerm.
5778 **
5779 ** WARNING: This routine might reallocate the space used to store
5780 ** WhereTerms. All pointers to WhereTerms should be invalidated after
5781 ** calling this routine. Such pointers may be reinitialized by referencing
5782 ** the pWC->a[] array.
5783 */
5784 static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
5785 WhereTerm *pTerm;
5786 int idx;
5787 testcase( wtFlags & TERM_VIRTUAL );
5788 if( pWC->nTerm>=pWC->nSlot ){
5789 WhereTerm *pOld = pWC->a;
5790 sqlite3 *db = pWC->pWInfo->pParse->db;
5791 pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
5792 if( pWC->a==0 ){
5793 if( wtFlags & TERM_DYNAMIC ){
5794 sqlite3ExprDelete(db, p);
5795 }
5796 pWC->a = pOld;
5797 return 0;
5798 }
5799 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
5800 if( pOld!=pWC->aStatic ){
5801 sqlite3DbFree(db, pOld);
5802 }
5803 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
5804 memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
5805 }
5806 pTerm = &pWC->a[idx = pWC->nTerm++];
5807 if( p && ExprHasProperty(p, EP_Unlikely) ){
5808 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
5809 }else{
5810 pTerm->truthProb = 1;
5811 }
5812 pTerm->pExpr = sqlite3ExprSkipCollate(p);
5813 pTerm->wtFlags = wtFlags;
5814 pTerm->pWC = pWC;
5815 pTerm->iParent = -1;
5816 return idx;
5817 }
5818
5819 /*
5820 ** Return TRUE if the given operator is one of the operators that is
5821 ** allowed for an indexable WHERE clause term. The allowed operators are
5822 ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
5823 */
5824 static int allowedOp(int op){
5825 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
5826 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
5827 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
5828 assert( TK_GE==TK_EQ+4 );
5829 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
5830 }
5831
5832 /*
5833 ** Commute a comparison operator. Expressions of the form "X op Y"
5834 ** are converted into "Y op X".
5835 **
5836 ** If left/right precedence rules come into play when determining the
5837 ** collating sequence, then COLLATE operators are adjusted to ensure
5838 ** that the collating sequence does not change. For example:
5839 ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
5840 ** the left hand side of a comparison overrides any collation sequence
5841 ** attached to the right. For the same reason the EP_Collate flag
5842 ** is not commuted.
5843 */
5844 static void exprCommute(Parse *pParse, Expr *pExpr){
5845 u16 expRight = (pExpr->pRight->flags & EP_Collate);
5846 u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
5847 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
5848 if( expRight==expLeft ){
5849 /* Either X and Y both have COLLATE operator or neither do */
5850 if( expRight ){
5851 /* Both X and Y have COLLATE operators. Make sure X is always
5852 ** used by clearing the EP_Collate flag from Y. */
5853 pExpr->pRight->flags &= ~EP_Collate;
5854 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
5855 /* Neither X nor Y have COLLATE operators, but X has a non-default
5856 ** collating sequence. So add the EP_Collate marker on X to cause
5857 ** it to be searched first. */
5858 pExpr->pLeft->flags |= EP_Collate;
5859 }
5860 }
5861 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
5862 if( pExpr->op>=TK_GT ){
5863 assert( TK_LT==TK_GT+2 );
5864 assert( TK_GE==TK_LE+2 );
5865 assert( TK_GT>TK_EQ );
5866 assert( TK_GT<TK_LE );
5867 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
5868 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
5869 }
5870 }
5871
5872 /*
5873 ** Translate from TK_xx operator to WO_xx bitmask.
5874 */
5875 static u16 operatorMask(int op){
5876 u16 c;
5877 assert( allowedOp(op) );
5878 if( op==TK_IN ){
5879 c = WO_IN;
5880 }else if( op==TK_ISNULL ){
5881 c = WO_ISNULL;
5882 }else if( op==TK_IS ){
5883 c = WO_IS;
5884 }else{
5885 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
5886 c = (u16)(WO_EQ<<(op-TK_EQ));
5887 }
5888 assert( op!=TK_ISNULL || c==WO_ISNULL );
5889 assert( op!=TK_IN || c==WO_IN );
5890 assert( op!=TK_EQ || c==WO_EQ );
5891 assert( op!=TK_LT || c==WO_LT );
5892 assert( op!=TK_LE || c==WO_LE );
5893 assert( op!=TK_GT || c==WO_GT );
5894 assert( op!=TK_GE || c==WO_GE );
5895 assert( op!=TK_IS || c==WO_IS );
5896 return c;
5897 }
5898
5899
5900 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
5901 /*
5902 ** Check to see if the given expression is a LIKE or GLOB operator that
5903 ** can be optimized using inequality constraints. Return TRUE if it is
5904 ** so and false if not.
5905 **
5906 ** In order for the operator to be optimizible, the RHS must be a string
5907 ** literal that does not begin with a wildcard. The LHS must be a column
5908 ** that may only be NULL, a string, or a BLOB, never a number. (This means
5909 ** that virtual tables cannot participate in the LIKE optimization.) The
5910 ** collating sequence for the column on the LHS must be appropriate for
5911 ** the operator.
5912 */
5913 static int isLikeOrGlob(
5914 Parse *pParse, /* Parsing and code generating context */
5915 Expr *pExpr, /* Test this expression */
5916 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
5917 int *pisComplete, /* True if the only wildcard is % in the last character */
5918 int *pnoCase /* True if uppercase is equivalent to lowercase */
5919 ){
5920 const char *z = 0; /* String on RHS of LIKE operator */
5921 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
5922 ExprList *pList; /* List of operands to the LIKE operator */
5923 int c; /* One character in z[] */
5924 int cnt; /* Number of non-wildcard prefix characters */
5925 char wc[3]; /* Wildcard characters */
5926 sqlite3 *db = pParse->db; /* Database connection */
5927 sqlite3_value *pVal = 0;
5928 int op; /* Opcode of pRight */
5929
5930 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
5931 return 0;
5932 }
5933 #ifdef SQLITE_EBCDIC
5934 if( *pnoCase ) return 0;
5935 #endif
5936 pList = pExpr->x.pList;
5937 pLeft = pList->a[1].pExpr;
5938 if( pLeft->op!=TK_COLUMN
5939 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
5940 || IsVirtual(pLeft->pTab) /* Value might be numeric */
5941 ){
5942 /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
5943 ** be the name of an indexed column with TEXT affinity. */
5944 return 0;
5945 }
5946 assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
5947
5948 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
5949 op = pRight->op;
5950 if( op==TK_VARIABLE ){
5951 Vdbe *pReprepare = pParse->pReprepare;
5952 int iCol = pRight->iColumn;
5953 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
5954 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
5955 z = (char *)sqlite3_value_text(pVal);
5956 }
5957 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
5958 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
5959 }else if( op==TK_STRING ){
5960 z = pRight->u.zToken;
5961 }
5962 if( z ){
5963 cnt = 0;
5964 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
5965 cnt++;
5966 }
5967 if( cnt!=0 && 255!=(u8)z[cnt-1] ){
5968 Expr *pPrefix;
5969 *pisComplete = c==wc[0] && z[cnt+1]==0;
5970 pPrefix = sqlite3Expr(db, TK_STRING, z);
5971 if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
5972 *ppPrefix = pPrefix;
5973 if( op==TK_VARIABLE ){
5974 Vdbe *v = pParse->pVdbe;
5975 sqlite3VdbeSetVarmask(v, pRight->iColumn);
5976 if( *pisComplete && pRight->u.zToken[1] ){
5977 /* If the rhs of the LIKE expression is a variable, and the current
5978 ** value of the variable means there is no need to invoke the LIKE
5979 ** function, then no OP_Variable will be added to the program.
5980 ** This causes problems for the sqlite3_bind_parameter_name()
5981 ** API. To work around them, add a dummy OP_Variable here.
5982 */
5983 int r1 = sqlite3GetTempReg(pParse);
5984 sqlite3ExprCodeTarget(pParse, pRight, r1);
5985 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
5986 sqlite3ReleaseTempReg(pParse, r1);
5987 }
5988 }
5989 }else{
5990 z = 0;
5991 }
5992 }
5993
5994 sqlite3ValueFree(pVal);
5995 return (z!=0);
5996 }
5997 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
5998
5999
6000 #ifndef SQLITE_OMIT_VIRTUALTABLE
6001 /*
6002 ** Check to see if the given expression is of the form
6003 **
6004 ** column OP expr
6005 **
6006 ** where OP is one of MATCH, GLOB, LIKE or REGEXP and "column" is a
6007 ** column of a virtual table.
6008 **
6009 ** If it is then return TRUE. If not, return FALSE.
6010 */
6011 static int isMatchOfColumn(
6012 Expr *pExpr, /* Test this expression */
6013 unsigned char *peOp2 /* OUT: 0 for MATCH, or else an op2 value */
6014 ){
6015 struct Op2 {
6016 const char *zOp;
6017 unsigned char eOp2;
6018 } aOp[] = {
6019 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
6020 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
6021 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
6022 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
6023 };
6024 ExprList *pList;
6025 Expr *pCol; /* Column reference */
6026 int i;
6027
6028 if( pExpr->op!=TK_FUNCTION ){
6029 return 0;
6030 }
6031 pList = pExpr->x.pList;
6032 if( pList==0 || pList->nExpr!=2 ){
6033 return 0;
6034 }
6035 pCol = pList->a[1].pExpr;
6036 if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){
6037 return 0;
6038 }
6039 for(i=0; i<ArraySize(aOp); i++){
6040 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
6041 *peOp2 = aOp[i].eOp2;
6042 return 1;
6043 }
6044 }
6045 return 0;
6046 }
6047 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6048
6049 /*
6050 ** If the pBase expression originated in the ON or USING clause of
6051 ** a join, then transfer the appropriate markings over to derived.
6052 */
6053 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
6054 if( pDerived ){
6055 pDerived->flags |= pBase->flags & EP_FromJoin;
6056 pDerived->iRightJoinTable = pBase->iRightJoinTable;
6057 }
6058 }
6059
6060 /*
6061 ** Mark term iChild as being a child of term iParent
6062 */
6063 static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
6064 pWC->a[iChild].iParent = iParent;
6065 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
6066 pWC->a[iParent].nChild++;
6067 }
6068
6069 /*
6070 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
6071 ** a conjunction, then return just pTerm when N==0. If N is exceeds
6072 ** the number of available subterms, return NULL.
6073 */
6074 static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
6075 if( pTerm->eOperator!=WO_AND ){
6076 return N==0 ? pTerm : 0;
6077 }
6078 if( N<pTerm->u.pAndInfo->wc.nTerm ){
6079 return &pTerm->u.pAndInfo->wc.a[N];
6080 }
6081 return 0;
6082 }
6083
6084 /*
6085 ** Subterms pOne and pTwo are contained within WHERE clause pWC. The
6086 ** two subterms are in disjunction - they are OR-ed together.
6087 **
6088 ** If these two terms are both of the form: "A op B" with the same
6089 ** A and B values but different operators and if the operators are
6090 ** compatible (if one is = and the other is <, for example) then
6091 ** add a new virtual AND term to pWC that is the combination of the
6092 ** two.
6093 **
6094 ** Some examples:
6095 **
6096 ** x<y OR x=y --> x<=y
6097 ** x=y OR x=y --> x=y
6098 ** x<=y OR x<y --> x<=y
6099 **
6100 ** The following is NOT generated:
6101 **
6102 ** x<y OR x>y --> x!=y
6103 */
6104 static void whereCombineDisjuncts(
6105 SrcList *pSrc, /* the FROM clause */
6106 WhereClause *pWC, /* The complete WHERE clause */
6107 WhereTerm *pOne, /* First disjunct */
6108 WhereTerm *pTwo /* Second disjunct */
6109 ){
6110 u16 eOp = pOne->eOperator | pTwo->eOperator;
6111 sqlite3 *db; /* Database connection (for malloc) */
6112 Expr *pNew; /* New virtual expression */
6113 int op; /* Operator for the combined expression */
6114 int idxNew; /* Index in pWC of the next virtual term */
6115
6116 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
6117 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
6118 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
6119 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
6120 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
6121 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
6122 if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
6123 if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
6124 /* If we reach this point, it means the two subterms can be combined */
6125 if( (eOp & (eOp-1))!=0 ){
6126 if( eOp & (WO_LT|WO_LE) ){
6127 eOp = WO_LE;
6128 }else{
6129 assert( eOp & (WO_GT|WO_GE) );
6130 eOp = WO_GE;
6131 }
6132 }
6133 db = pWC->pWInfo->pParse->db;
6134 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
6135 if( pNew==0 ) return;
6136 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
6137 pNew->op = op;
6138 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
6139 exprAnalyze(pSrc, pWC, idxNew);
6140 }
6141
6142 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
6143 /*
6144 ** Analyze a term that consists of two or more OR-connected
6145 ** subterms. So in:
6146 **
6147 ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
6148 ** ^^^^^^^^^^^^^^^^^^^^
6149 **
6150 ** This routine analyzes terms such as the middle term in the above example.
6151 ** A WhereOrTerm object is computed and attached to the term under
6152 ** analysis, regardless of the outcome of the analysis. Hence:
6153 **
6154 ** WhereTerm.wtFlags |= TERM_ORINFO
6155 ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
6156 **
6157 ** The term being analyzed must have two or more of OR-connected subterms.
6158 ** A single subterm might be a set of AND-connected sub-subterms.
6159 ** Examples of terms under analysis:
6160 **
6161 ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
6162 ** (B) x=expr1 OR expr2=x OR x=expr3
6163 ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
6164 ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
6165 ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
6166 ** (F) x>A OR (x=A AND y>=B)
6167 **
6168 ** CASE 1:
6169 **
6170 ** If all subterms are of the form T.C=expr for some single column of C and
6171 ** a single table T (as shown in example B above) then create a new virtual
6172 ** term that is an equivalent IN expression. In other words, if the term
6173 ** being analyzed is:
6174 **
6175 ** x = expr1 OR expr2 = x OR x = expr3
6176 **
6177 ** then create a new virtual term like this:
6178 **
6179 ** x IN (expr1,expr2,expr3)
6180 **
6181 ** CASE 2:
6182 **
6183 ** If there are exactly two disjuncts and one side has x>A and the other side
6184 ** has x=A (for the same x and A) then add a new virtual conjunct term to the
6185 ** WHERE clause of the form "x>=A". Example:
6186 **
6187 ** x>A OR (x=A AND y>B) adds: x>=A
6188 **
6189 ** The added conjunct can sometimes be helpful in query planning.
6190 **
6191 ** CASE 3:
6192 **
6193 ** If all subterms are indexable by a single table T, then set
6194 **
6195 ** WhereTerm.eOperator = WO_OR
6196 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
6197 **
6198 ** A subterm is "indexable" if it is of the form
6199 ** "T.C <op> <expr>" where C is any column of table T and
6200 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
6201 ** A subterm is also indexable if it is an AND of two or more
6202 ** subsubterms at least one of which is indexable. Indexable AND
6203 ** subterms have their eOperator set to WO_AND and they have
6204 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
6205 **
6206 ** From another point of view, "indexable" means that the subterm could
6207 ** potentially be used with an index if an appropriate index exists.
6208 ** This analysis does not consider whether or not the index exists; that
6209 ** is decided elsewhere. This analysis only looks at whether subterms
6210 ** appropriate for indexing exist.
6211 **
6212 ** All examples A through E above satisfy case 3. But if a term
6213 ** also satisfies case 1 (such as B) we know that the optimizer will
6214 ** always prefer case 1, so in that case we pretend that case 3 is not
6215 ** satisfied.
6216 **
6217 ** It might be the case that multiple tables are indexable. For example,
6218 ** (E) above is indexable on tables P, Q, and R.
6219 **
6220 ** Terms that satisfy case 3 are candidates for lookup by using
6221 ** separate indices to find rowids for each subterm and composing
6222 ** the union of all rowids using a RowSet object. This is similar
6223 ** to "bitmap indices" in other database engines.
6224 **
6225 ** OTHERWISE:
6226 **
6227 ** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
6228 ** zero. This term is not useful for search.
6229 */
6230 static void exprAnalyzeOrTerm(
6231 SrcList *pSrc, /* the FROM clause */
6232 WhereClause *pWC, /* the complete WHERE clause */
6233 int idxTerm /* Index of the OR-term to be analyzed */
6234 ){
6235 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
6236 Parse *pParse = pWInfo->pParse; /* Parser context */
6237 sqlite3 *db = pParse->db; /* Database connection */
6238 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
6239 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
6240 int i; /* Loop counters */
6241 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
6242 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
6243 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
6244 Bitmask chngToIN; /* Tables that might satisfy case 1 */
6245 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
6246
6247 /*
6248 ** Break the OR clause into its separate subterms. The subterms are
6249 ** stored in a WhereClause structure containing within the WhereOrInfo
6250 ** object that is attached to the original OR clause term.
6251 */
6252 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
6253 assert( pExpr->op==TK_OR );
6254 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
6255 if( pOrInfo==0 ) return;
6256 pTerm->wtFlags |= TERM_ORINFO;
6257 pOrWc = &pOrInfo->wc;
6258 sqlite3WhereClauseInit(pOrWc, pWInfo);
6259 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
6260 sqlite3WhereExprAnalyze(pSrc, pOrWc);
6261 if( db->mallocFailed ) return;
6262 assert( pOrWc->nTerm>=2 );
6263
6264 /*
6265 ** Compute the set of tables that might satisfy cases 1 or 3.
6266 */
6267 indexable = ~(Bitmask)0;
6268 chngToIN = ~(Bitmask)0;
6269 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
6270 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
6271 WhereAndInfo *pAndInfo;
6272 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
6273 chngToIN = 0;
6274 pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
6275 if( pAndInfo ){
6276 WhereClause *pAndWC;
6277 WhereTerm *pAndTerm;
6278 int j;
6279 Bitmask b = 0;
6280 pOrTerm->u.pAndInfo = pAndInfo;
6281 pOrTerm->wtFlags |= TERM_ANDINFO;
6282 pOrTerm->eOperator = WO_AND;
6283 pAndWC = &pAndInfo->wc;
6284 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
6285 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
6286 sqlite3WhereExprAnalyze(pSrc, pAndWC);
6287 pAndWC->pOuter = pWC;
6288 testcase( db->mallocFailed );
6289 if( !db->mallocFailed ){
6290 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
6291 assert( pAndTerm->pExpr );
6292 if( allowedOp(pAndTerm->pExpr->op) ){
6293 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
6294 }
6295 }
6296 }
6297 indexable &= b;
6298 }
6299 }else if( pOrTerm->wtFlags & TERM_COPIED ){
6300 /* Skip this term for now. We revisit it when we process the
6301 ** corresponding TERM_VIRTUAL term */
6302 }else{
6303 Bitmask b;
6304 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
6305 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
6306 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
6307 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
6308 }
6309 indexable &= b;
6310 if( (pOrTerm->eOperator & WO_EQ)==0 ){
6311 chngToIN = 0;
6312 }else{
6313 chngToIN &= b;
6314 }
6315 }
6316 }
6317
6318 /*
6319 ** Record the set of tables that satisfy case 3. The set might be
6320 ** empty.
6321 */
6322 pOrInfo->indexable = indexable;
6323 pTerm->eOperator = indexable==0 ? 0 : WO_OR;
6324
6325 /* For a two-way OR, attempt to implementation case 2.
6326 */
6327 if( indexable && pOrWc->nTerm==2 ){
6328 int iOne = 0;
6329 WhereTerm *pOne;
6330 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
6331 int iTwo = 0;
6332 WhereTerm *pTwo;
6333 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
6334 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
6335 }
6336 }
6337 }
6338
6339 /*
6340 ** chngToIN holds a set of tables that *might* satisfy case 1. But
6341 ** we have to do some additional checking to see if case 1 really
6342 ** is satisfied.
6343 **
6344 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
6345 ** that there is no possibility of transforming the OR clause into an
6346 ** IN operator because one or more terms in the OR clause contain
6347 ** something other than == on a column in the single table. The 1-bit
6348 ** case means that every term of the OR clause is of the form
6349 ** "table.column=expr" for some single table. The one bit that is set
6350 ** will correspond to the common table. We still need to check to make
6351 ** sure the same column is used on all terms. The 2-bit case is when
6352 ** the all terms are of the form "table1.column=table2.column". It
6353 ** might be possible to form an IN operator with either table1.column
6354 ** or table2.column as the LHS if either is common to every term of
6355 ** the OR clause.
6356 **
6357 ** Note that terms of the form "table.column1=table.column2" (the
6358 ** same table on both sizes of the ==) cannot be optimized.
6359 */
6360 if( chngToIN ){
6361 int okToChngToIN = 0; /* True if the conversion to IN is valid */
6362 int iColumn = -1; /* Column index on lhs of IN operator */
6363 int iCursor = -1; /* Table cursor common to all terms */
6364 int j = 0; /* Loop counter */
6365
6366 /* Search for a table and column that appears on one side or the
6367 ** other of the == operator in every subterm. That table and column
6368 ** will be recorded in iCursor and iColumn. There might not be any
6369 ** such table and column. Set okToChngToIN if an appropriate table
6370 ** and column is found but leave okToChngToIN false if not found.
6371 */
6372 for(j=0; j<2 && !okToChngToIN; j++){
6373 pOrTerm = pOrWc->a;
6374 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
6375 assert( pOrTerm->eOperator & WO_EQ );
6376 pOrTerm->wtFlags &= ~TERM_OR_OK;
6377 if( pOrTerm->leftCursor==iCursor ){
6378 /* This is the 2-bit case and we are on the second iteration and
6379 ** current term is from the first iteration. So skip this term. */
6380 assert( j==1 );
6381 continue;
6382 }
6383 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
6384 pOrTerm->leftCursor))==0 ){
6385 /* This term must be of the form t1.a==t2.b where t2 is in the
6386 ** chngToIN set but t1 is not. This term will be either preceded
6387 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
6388 ** and use its inversion. */
6389 testcase( pOrTerm->wtFlags & TERM_COPIED );
6390 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
6391 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
6392 continue;
6393 }
6394 iColumn = pOrTerm->u.leftColumn;
6395 iCursor = pOrTerm->leftCursor;
6396 break;
6397 }
6398 if( i<0 ){
6399 /* No candidate table+column was found. This can only occur
6400 ** on the second iteration */
6401 assert( j==1 );
6402 assert( IsPowerOfTwo(chngToIN) );
6403 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
6404 break;
6405 }
6406 testcase( j==1 );
6407
6408 /* We have found a candidate table and column. Check to see if that
6409 ** table and column is common to every term in the OR clause */
6410 okToChngToIN = 1;
6411 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
6412 assert( pOrTerm->eOperator & WO_EQ );
6413 if( pOrTerm->leftCursor!=iCursor ){
6414 pOrTerm->wtFlags &= ~TERM_OR_OK;
6415 }else if( pOrTerm->u.leftColumn!=iColumn ){
6416 okToChngToIN = 0;
6417 }else{
6418 int affLeft, affRight;
6419 /* If the right-hand side is also a column, then the affinities
6420 ** of both right and left sides must be such that no type
6421 ** conversions are required on the right. (Ticket #2249)
6422 */
6423 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
6424 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
6425 if( affRight!=0 && affRight!=affLeft ){
6426 okToChngToIN = 0;
6427 }else{
6428 pOrTerm->wtFlags |= TERM_OR_OK;
6429 }
6430 }
6431 }
6432 }
6433
6434 /* At this point, okToChngToIN is true if original pTerm satisfies
6435 ** case 1. In that case, construct a new virtual term that is
6436 ** pTerm converted into an IN operator.
6437 */
6438 if( okToChngToIN ){
6439 Expr *pDup; /* A transient duplicate expression */
6440 ExprList *pList = 0; /* The RHS of the IN operator */
6441 Expr *pLeft = 0; /* The LHS of the IN operator */
6442 Expr *pNew; /* The complete IN operator */
6443
6444 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
6445 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
6446 assert( pOrTerm->eOperator & WO_EQ );
6447 assert( pOrTerm->leftCursor==iCursor );
6448 assert( pOrTerm->u.leftColumn==iColumn );
6449 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
6450 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
6451 pLeft = pOrTerm->pExpr->pLeft;
6452 }
6453 assert( pLeft!=0 );
6454 pDup = sqlite3ExprDup(db, pLeft, 0);
6455 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
6456 if( pNew ){
6457 int idxNew;
6458 transferJoinMarkings(pNew, pExpr);
6459 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
6460 pNew->x.pList = pList;
6461 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
6462 testcase( idxNew==0 );
6463 exprAnalyze(pSrc, pWC, idxNew);
6464 pTerm = &pWC->a[idxTerm];
6465 markTermAsChild(pWC, idxNew, idxTerm);
6466 }else{
6467 sqlite3ExprListDelete(db, pList);
6468 }
6469 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
6470 }
6471 }
6472 }
6473 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
6474
6475 /*
6476 ** We already know that pExpr is a binary operator where both operands are
6477 ** column references. This routine checks to see if pExpr is an equivalence
6478 ** relation:
6479 ** 1. The SQLITE_Transitive optimization must be enabled
6480 ** 2. Must be either an == or an IS operator
6481 ** 3. Not originating in the ON clause of an OUTER JOIN
6482 ** 4. The affinities of A and B must be compatible
6483 ** 5a. Both operands use the same collating sequence OR
6484 ** 5b. The overall collating sequence is BINARY
6485 ** If this routine returns TRUE, that means that the RHS can be substituted
6486 ** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
6487 ** This is an optimization. No harm comes from returning 0. But if 1 is
6488 ** returned when it should not be, then incorrect answers might result.
6489 */
6490 static int termIsEquivalence(Parse *pParse, Expr *pExpr){
6491 char aff1, aff2;
6492 CollSeq *pColl;
6493 const char *zColl1, *zColl2;
6494 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
6495 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
6496 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
6497 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
6498 aff2 = sqlite3ExprAffinity(pExpr->pRight);
6499 if( aff1!=aff2
6500 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
6501 ){
6502 return 0;
6503 }
6504 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
6505 if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
6506 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
6507 /* Since pLeft and pRight are both a column references, their collating
6508 ** sequence should always be defined. */
6509 zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
6510 pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
6511 zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
6512 return sqlite3StrICmp(zColl1, zColl2)==0;
6513 }
6514
6515 /*
6516 ** Recursively walk the expressions of a SELECT statement and generate
6517 ** a bitmask indicating which tables are used in that expression
6518 ** tree.
6519 */
6520 static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
6521 Bitmask mask = 0;
6522 while( pS ){
6523 SrcList *pSrc = pS->pSrc;
6524 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
6525 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
6526 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
6527 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
6528 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
6529 if( ALWAYS(pSrc!=0) ){
6530 int i;
6531 for(i=0; i<pSrc->nSrc; i++){
6532 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
6533 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
6534 }
6535 }
6536 pS = pS->pPrior;
6537 }
6538 return mask;
6539 }
6540
6541 /*
6542 ** Expression pExpr is one operand of a comparison operator that might
6543 ** be useful for indexing. This routine checks to see if pExpr appears
6544 ** in any index. Return TRUE (1) if pExpr is an indexed term and return
6545 ** FALSE (0) if not. If TRUE is returned, also set *piCur to the cursor
6546 ** number of the table that is indexed and *piColumn to the column number
6547 ** of the column that is indexed, or -2 if an expression is being indexed.
6548 **
6549 ** If pExpr is a TK_COLUMN column reference, then this routine always returns
6550 ** true even if that particular column is not indexed, because the column
6551 ** might be added to an automatic index later.
6552 */
6553 static int exprMightBeIndexed(
6554 SrcList *pFrom, /* The FROM clause */
6555 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
6556 Expr *pExpr, /* An operand of a comparison operator */
6557 int *piCur, /* Write the referenced table cursor number here */
6558 int *piColumn /* Write the referenced table column number here */
6559 ){
6560 Index *pIdx;
6561 int i;
6562 int iCur;
6563 if( pExpr->op==TK_COLUMN ){
6564 *piCur = pExpr->iTable;
6565 *piColumn = pExpr->iColumn;
6566 return 1;
6567 }
6568 if( mPrereq==0 ) return 0; /* No table references */
6569 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
6570 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
6571 iCur = pFrom->a[i].iCursor;
6572 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
6573 if( pIdx->aColExpr==0 ) continue;
6574 for(i=0; i<pIdx->nKeyCol; i++){
6575 if( pIdx->aiColumn[i]!=(-2) ) continue;
6576 if( sqlite3ExprCompare(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
6577 *piCur = iCur;
6578 *piColumn = -2;
6579 return 1;
6580 }
6581 }
6582 }
6583 return 0;
6584 }
6585
6586 /*
6587 ** The input to this routine is an WhereTerm structure with only the
6588 ** "pExpr" field filled in. The job of this routine is to analyze the
6589 ** subexpression and populate all the other fields of the WhereTerm
6590 ** structure.
6591 **
6592 ** If the expression is of the form "<expr> <op> X" it gets commuted
6593 ** to the standard form of "X <op> <expr>".
6594 **
6595 ** If the expression is of the form "X <op> Y" where both X and Y are
6596 ** columns, then the original expression is unchanged and a new virtual
6597 ** term of the form "Y <op> X" is added to the WHERE clause and
6598 ** analyzed separately. The original term is marked with TERM_COPIED
6599 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
6600 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
6601 ** is a commuted copy of a prior term.) The original term has nChild=1
6602 ** and the copy has idxParent set to the index of the original term.
6603 */
6604 static void exprAnalyze(
6605 SrcList *pSrc, /* the FROM clause */
6606 WhereClause *pWC, /* the WHERE clause */
6607 int idxTerm /* Index of the term to be analyzed */
6608 ){
6609 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
6610 WhereTerm *pTerm; /* The term to be analyzed */
6611 WhereMaskSet *pMaskSet; /* Set of table index masks */
6612 Expr *pExpr; /* The expression to be analyzed */
6613 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
6614 Bitmask prereqAll; /* Prerequesites of pExpr */
6615 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
6616 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
6617 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
6618 int noCase = 0; /* uppercase equivalent to lowercase */
6619 int op; /* Top-level operator. pExpr->op */
6620 Parse *pParse = pWInfo->pParse; /* Parsing context */
6621 sqlite3 *db = pParse->db; /* Database connection */
6622 unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */
6623
6624 if( db->mallocFailed ){
6625 return;
6626 }
6627 pTerm = &pWC->a[idxTerm];
6628 pMaskSet = &pWInfo->sMaskSet;
6629 pExpr = pTerm->pExpr;
6630 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
6631 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
6632 op = pExpr->op;
6633 if( op==TK_IN ){
6634 assert( pExpr->pRight==0 );
6635 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
6636 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
6637 }else{
6638 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
6639 }
6640 }else if( op==TK_ISNULL ){
6641 pTerm->prereqRight = 0;
6642 }else{
6643 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
6644 }
6645 prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
6646 if( ExprHasProperty(pExpr, EP_FromJoin) ){
6647 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
6648 prereqAll |= x;
6649 extraRight = x-1; /* ON clause terms may not be used with an index
6650 ** on left table of a LEFT JOIN. Ticket #3015 */
6651 }
6652 pTerm->prereqAll = prereqAll;
6653 pTerm->leftCursor = -1;
6654 pTerm->iParent = -1;
6655 pTerm->eOperator = 0;
6656 if( allowedOp(op) ){
6657 int iCur, iColumn;
6658 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
6659 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
6660 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
6661 if( exprMightBeIndexed(pSrc, prereqLeft, pLeft, &iCur, &iColumn) ){
6662 pTerm->leftCursor = iCur;
6663 pTerm->u.leftColumn = iColumn;
6664 pTerm->eOperator = operatorMask(op) & opMask;
6665 }
6666 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
6667 if( pRight
6668 && exprMightBeIndexed(pSrc, pTerm->prereqRight, pRight, &iCur, &iColumn)
6669 ){
6670 WhereTerm *pNew;
6671 Expr *pDup;
6672 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
6673 if( pTerm->leftCursor>=0 ){
6674 int idxNew;
6675 pDup = sqlite3ExprDup(db, pExpr, 0);
6676 if( db->mallocFailed ){
6677 sqlite3ExprDelete(db, pDup);
6678 return;
6679 }
6680 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
6681 if( idxNew==0 ) return;
6682 pNew = &pWC->a[idxNew];
6683 markTermAsChild(pWC, idxNew, idxTerm);
6684 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
6685 pTerm = &pWC->a[idxTerm];
6686 pTerm->wtFlags |= TERM_COPIED;
6687
6688 if( termIsEquivalence(pParse, pDup) ){
6689 pTerm->eOperator |= WO_EQUIV;
6690 eExtraOp = WO_EQUIV;
6691 }
6692 }else{
6693 pDup = pExpr;
6694 pNew = pTerm;
6695 }
6696 exprCommute(pParse, pDup);
6697 pNew->leftCursor = iCur;
6698 pNew->u.leftColumn = iColumn;
6699 testcase( (prereqLeft | extraRight) != prereqLeft );
6700 pNew->prereqRight = prereqLeft | extraRight;
6701 pNew->prereqAll = prereqAll;
6702 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
6703 }
6704 }
6705
6706 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
6707 /* If a term is the BETWEEN operator, create two new virtual terms
6708 ** that define the range that the BETWEEN implements. For example:
6709 **
6710 ** a BETWEEN b AND c
6711 **
6712 ** is converted into:
6713 **
6714 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
6715 **
6716 ** The two new terms are added onto the end of the WhereClause object.
6717 ** The new terms are "dynamic" and are children of the original BETWEEN
6718 ** term. That means that if the BETWEEN term is coded, the children are
6719 ** skipped. Or, if the children are satisfied by an index, the original
6720 ** BETWEEN term is skipped.
6721 */
6722 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
6723 ExprList *pList = pExpr->x.pList;
6724 int i;
6725 static const u8 ops[] = {TK_GE, TK_LE};
6726 assert( pList!=0 );
6727 assert( pList->nExpr==2 );
6728 for(i=0; i<2; i++){
6729 Expr *pNewExpr;
6730 int idxNew;
6731 pNewExpr = sqlite3PExpr(pParse, ops[i],
6732 sqlite3ExprDup(db, pExpr->pLeft, 0),
6733 sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
6734 transferJoinMarkings(pNewExpr, pExpr);
6735 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
6736 testcase( idxNew==0 );
6737 exprAnalyze(pSrc, pWC, idxNew);
6738 pTerm = &pWC->a[idxTerm];
6739 markTermAsChild(pWC, idxNew, idxTerm);
6740 }
6741 }
6742 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
6743
6744 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
6745 /* Analyze a term that is composed of two or more subterms connected by
6746 ** an OR operator.
6747 */
6748 else if( pExpr->op==TK_OR ){
6749 assert( pWC->op==TK_AND );
6750 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
6751 pTerm = &pWC->a[idxTerm];
6752 }
6753 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
6754
6755 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
6756 /* Add constraints to reduce the search space on a LIKE or GLOB
6757 ** operator.
6758 **
6759 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
6760 **
6761 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
6762 **
6763 ** The last character of the prefix "abc" is incremented to form the
6764 ** termination condition "abd". If case is not significant (the default
6765 ** for LIKE) then the lower-bound is made all uppercase and the upper-
6766 ** bound is made all lowercase so that the bounds also work when comparing
6767 ** BLOBs.
6768 */
6769 if( pWC->op==TK_AND
6770 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
6771 ){
6772 Expr *pLeft; /* LHS of LIKE/GLOB operator */
6773 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
6774 Expr *pNewExpr1;
6775 Expr *pNewExpr2;
6776 int idxNew1;
6777 int idxNew2;
6778 const char *zCollSeqName; /* Name of collating sequence */
6779 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
6780
6781 pLeft = pExpr->x.pList->a[1].pExpr;
6782 pStr2 = sqlite3ExprDup(db, pStr1, 0);
6783
6784 /* Convert the lower bound to upper-case and the upper bound to
6785 ** lower-case (upper-case is less than lower-case in ASCII) so that
6786 ** the range constraints also work for BLOBs
6787 */
6788 if( noCase && !pParse->db->mallocFailed ){
6789 int i;
6790 char c;
6791 pTerm->wtFlags |= TERM_LIKE;
6792 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
6793 pStr1->u.zToken[i] = sqlite3Toupper(c);
6794 pStr2->u.zToken[i] = sqlite3Tolower(c);
6795 }
6796 }
6797
6798 if( !db->mallocFailed ){
6799 u8 c, *pC; /* Last character before the first wildcard */
6800 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
6801 c = *pC;
6802 if( noCase ){
6803 /* The point is to increment the last character before the first
6804 ** wildcard. But if we increment '@', that will push it into the
6805 ** alphabetic range where case conversions will mess up the
6806 ** inequality. To avoid this, make sure to also run the full
6807 ** LIKE on all candidate expressions by clearing the isComplete flag
6808 */
6809 if( c=='A'-1 ) isComplete = 0;
6810 c = sqlite3UpperToLower[c];
6811 }
6812 *pC = c + 1;
6813 }
6814 zCollSeqName = noCase ? "NOCASE" : "BINARY";
6815 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
6816 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
6817 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
6818 pStr1, 0);
6819 transferJoinMarkings(pNewExpr1, pExpr);
6820 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
6821 testcase( idxNew1==0 );
6822 exprAnalyze(pSrc, pWC, idxNew1);
6823 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
6824 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
6825 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
6826 pStr2, 0);
6827 transferJoinMarkings(pNewExpr2, pExpr);
6828 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
6829 testcase( idxNew2==0 );
6830 exprAnalyze(pSrc, pWC, idxNew2);
6831 pTerm = &pWC->a[idxTerm];
6832 if( isComplete ){
6833 markTermAsChild(pWC, idxNew1, idxTerm);
6834 markTermAsChild(pWC, idxNew2, idxTerm);
6835 }
6836 }
6837 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
6838
6839 #ifndef SQLITE_OMIT_VIRTUALTABLE
6840 /* Add a WO_MATCH auxiliary term to the constraint set if the
6841 ** current expression is of the form: column MATCH expr.
6842 ** This information is used by the xBestIndex methods of
6843 ** virtual tables. The native query optimizer does not attempt
6844 ** to do anything with MATCH functions.
6845 */
6846 if( isMatchOfColumn(pExpr, &eOp2) ){
6847 int idxNew;
6848 Expr *pRight, *pLeft;
6849 WhereTerm *pNewTerm;
6850 Bitmask prereqColumn, prereqExpr;
6851
6852 pRight = pExpr->x.pList->a[0].pExpr;
6853 pLeft = pExpr->x.pList->a[1].pExpr;
6854 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
6855 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
6856 if( (prereqExpr & prereqColumn)==0 ){
6857 Expr *pNewExpr;
6858 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
6859 0, sqlite3ExprDup(db, pRight, 0), 0);
6860 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
6861 testcase( idxNew==0 );
6862 pNewTerm = &pWC->a[idxNew];
6863 pNewTerm->prereqRight = prereqExpr;
6864 pNewTerm->leftCursor = pLeft->iTable;
6865 pNewTerm->u.leftColumn = pLeft->iColumn;
6866 pNewTerm->eOperator = WO_MATCH;
6867 pNewTerm->eMatchOp = eOp2;
6868 markTermAsChild(pWC, idxNew, idxTerm);
6869 pTerm = &pWC->a[idxTerm];
6870 pTerm->wtFlags |= TERM_COPIED;
6871 pNewTerm->prereqAll = pTerm->prereqAll;
6872 }
6873 }
6874 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6875
6876 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
6877 /* When sqlite_stat3 histogram data is available an operator of the
6878 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
6879 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
6880 ** virtual term of that form.
6881 **
6882 ** Note that the virtual term must be tagged with TERM_VNULL.
6883 */
6884 if( pExpr->op==TK_NOTNULL
6885 && pExpr->pLeft->op==TK_COLUMN
6886 && pExpr->pLeft->iColumn>=0
6887 && OptimizationEnabled(db, SQLITE_Stat34)
6888 ){
6889 Expr *pNewExpr;
6890 Expr *pLeft = pExpr->pLeft;
6891 int idxNew;
6892 WhereTerm *pNewTerm;
6893
6894 pNewExpr = sqlite3PExpr(pParse, TK_GT,
6895 sqlite3ExprDup(db, pLeft, 0),
6896 sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
6897
6898 idxNew = whereClauseInsert(pWC, pNewExpr,
6899 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
6900 if( idxNew ){
6901 pNewTerm = &pWC->a[idxNew];
6902 pNewTerm->prereqRight = 0;
6903 pNewTerm->leftCursor = pLeft->iTable;
6904 pNewTerm->u.leftColumn = pLeft->iColumn;
6905 pNewTerm->eOperator = WO_GT;
6906 markTermAsChild(pWC, idxNew, idxTerm);
6907 pTerm = &pWC->a[idxTerm];
6908 pTerm->wtFlags |= TERM_COPIED;
6909 pNewTerm->prereqAll = pTerm->prereqAll;
6910 }
6911 }
6912 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
6913
6914 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
6915 ** an index for tables to the left of the join.
6916 */
6917 pTerm->prereqRight |= extraRight;
6918 }
6919
6920 /***************************************************************************
6921 ** Routines with file scope above. Interface to the rest of the where.c
6922 ** subsystem follows.
6923 ***************************************************************************/
6924
6925 /*
6926 ** This routine identifies subexpressions in the WHERE clause where
6927 ** each subexpression is separated by the AND operator or some other
6928 ** operator specified in the op parameter. The WhereClause structure
6929 ** is filled with pointers to subexpressions. For example:
6930 **
6931 ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
6932 ** \________/ \_______________/ \________________/
6933 ** slot[0] slot[1] slot[2]
6934 **
6935 ** The original WHERE clause in pExpr is unaltered. All this routine
6936 ** does is make slot[] entries point to substructure within pExpr.
6937 **
6938 ** In the previous sentence and in the diagram, "slot[]" refers to
6939 ** the WhereClause.a[] array. The slot[] array grows as needed to contain
6940 ** all terms of the WHERE clause.
6941 */
6942 SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
6943 Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
6944 pWC->op = op;
6945 if( pE2==0 ) return;
6946 if( pE2->op!=op ){
6947 whereClauseInsert(pWC, pExpr, 0);
6948 }else{
6949 sqlite3WhereSplit(pWC, pE2->pLeft, op);
6950 sqlite3WhereSplit(pWC, pE2->pRight, op);
6951 }
6952 }
6953
6954 /*
6955 ** Initialize a preallocated WhereClause structure.
6956 */
6957 SQLITE_PRIVATE void sqlite3WhereClauseInit(
6958 WhereClause *pWC, /* The WhereClause to be initialized */
6959 WhereInfo *pWInfo /* The WHERE processing context */
6960 ){
6961 pWC->pWInfo = pWInfo;
6962 pWC->pOuter = 0;
6963 pWC->nTerm = 0;
6964 pWC->nSlot = ArraySize(pWC->aStatic);
6965 pWC->a = pWC->aStatic;
6966 }
6967
6968 /*
6969 ** Deallocate a WhereClause structure. The WhereClause structure
6970 ** itself is not freed. This routine is the inverse of
6971 ** sqlite3WhereClauseInit().
6972 */
6973 SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
6974 int i;
6975 WhereTerm *a;
6976 sqlite3 *db = pWC->pWInfo->pParse->db;
6977 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
6978 if( a->wtFlags & TERM_DYNAMIC ){
6979 sqlite3ExprDelete(db, a->pExpr);
6980 }
6981 if( a->wtFlags & TERM_ORINFO ){
6982 whereOrInfoDelete(db, a->u.pOrInfo);
6983 }else if( a->wtFlags & TERM_ANDINFO ){
6984 whereAndInfoDelete(db, a->u.pAndInfo);
6985 }
6986 }
6987 if( pWC->a!=pWC->aStatic ){
6988 sqlite3DbFree(db, pWC->a);
6989 }
6990 }
6991
6992
6993 /*
6994 ** These routines walk (recursively) an expression tree and generate
6995 ** a bitmask indicating which tables are used in that expression
6996 ** tree.
6997 */
6998 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
6999 Bitmask mask = 0;
7000 if( p==0 ) return 0;
7001 if( p->op==TK_COLUMN ){
7002 mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
7003 return mask;
7004 }
7005 mask = sqlite3WhereExprUsage(pMaskSet, p->pRight);
7006 mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
7007 if( ExprHasProperty(p, EP_xIsSelect) ){
7008 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
7009 }else{
7010 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
7011 }
7012 return mask;
7013 }
7014 SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprLis t *pList){
7015 int i;
7016 Bitmask mask = 0;
7017 if( pList ){
7018 for(i=0; i<pList->nExpr; i++){
7019 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
7020 }
7021 }
7022 return mask;
7023 }
7024
7025
7026 /*
7027 ** Call exprAnalyze on all terms in a WHERE clause.
7028 **
7029 ** Note that exprAnalyze() might add new virtual terms onto the
7030 ** end of the WHERE clause. We do not want to analyze these new
7031 ** virtual terms, so start analyzing at the end and work forward
7032 ** so that the added virtual terms are never processed.
7033 */
7034 SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
7035 SrcList *pTabList, /* the FROM clause */
7036 WhereClause *pWC /* the WHERE clause to be analyzed */
7037 ){
7038 int i;
7039 for(i=pWC->nTerm-1; i>=0; i--){
7040 exprAnalyze(pTabList, pWC, i);
7041 }
7042 }
7043
7044 /*
7045 ** For table-valued-functions, transform the function arguments into
7046 ** new WHERE clause terms.
7047 **
7048 ** Each function argument translates into an equality constraint against
7049 ** a HIDDEN column in the table.
7050 */
7051 SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(
7052 Parse *pParse, /* Parsing context */
7053 struct SrcList_item *pItem, /* The FROM clause term to process */
7054 WhereClause *pWC /* Xfer function arguments to here */
7055 ){
7056 Table *pTab;
7057 int j, k;
7058 ExprList *pArgs;
7059 Expr *pColRef;
7060 Expr *pTerm;
7061 if( pItem->fg.isTabFunc==0 ) return;
7062 pTab = pItem->pTab;
7063 assert( pTab!=0 );
7064 pArgs = pItem->u1.pFuncArg;
7065 if( pArgs==0 ) return;
7066 for(j=k=0; j<pArgs->nExpr; j++){
7067 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
7068 if( k>=pTab->nCol ){
7069 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
7070 pTab->zName, j);
7071 return;
7072 }
7073 pColRef = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
7074 if( pColRef==0 ) return;
7075 pColRef->iTable = pItem->iCursor;
7076 pColRef->iColumn = k++;
7077 pColRef->pTab = pTab;
7078 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
7079 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
7080 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
7081 }
7082 }
7083
7084 /************** End of whereexpr.c *******************************************/
7085 /************** Begin file where.c *******************************************/
7086 /*
7087 ** 2001 September 15
7088 **
7089 ** The author disclaims copyright to this source code. In place of
7090 ** a legal notice, here is a blessing:
7091 **
7092 ** May you do good and not evil.
7093 ** May you find forgiveness for yourself and forgive others.
7094 ** May you share freely, never taking more than you give.
7095 **
7096 *************************************************************************
7097 ** This module contains C code that generates VDBE code used to process
7098 ** the WHERE clause of SQL statements. This module is responsible for
7099 ** generating the code that loops through a table looking for applicable
7100 ** rows. Indices are selected and used to speed the search when doing
7101 ** so is applicable. Because this module is responsible for selecting
7102 ** indices, you might also think of this module as the "query optimizer".
7103 */
7104 /* #include "sqliteInt.h" */
7105 /* #include "whereInt.h" */
7106
7107 /* Forward declaration of methods */
7108 static int whereLoopResize(sqlite3*, WhereLoop*, int);
7109
7110 /* Test variable that can be set to enable WHERE tracing */
7111 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
7112 /***/ int sqlite3WhereTrace = 0;
7113 #endif
7114
7115
7116 /*
7117 ** Return the estimated number of output rows from a WHERE clause
7118 */
7119 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
7120 return sqlite3LogEstToInt(pWInfo->nRowOut);
7121 }
7122
7123 /*
7124 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
7125 ** WHERE clause returns outputs for DISTINCT processing.
7126 */
7127 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
7128 return pWInfo->eDistinct;
7129 }
7130
7131 /*
7132 ** Return TRUE if the WHERE clause returns rows in ORDER BY order.
7133 ** Return FALSE if the output needs to be sorted.
7134 */
7135 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
7136 return pWInfo->nOBSat;
7137 }
7138
7139 /*
7140 ** Return the VDBE address or label to jump to in order to continue
7141 ** immediately with the next row of a WHERE clause.
7142 */
7143 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
7144 assert( pWInfo->iContinue!=0 );
7145 return pWInfo->iContinue;
7146 }
7147
7148 /*
7149 ** Return the VDBE address or label to jump to in order to break
7150 ** out of a WHERE loop.
7151 */
7152 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
7153 return pWInfo->iBreak;
7154 }
7155
7156 /*
7157 ** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
7158 ** operate directly on the rowis returned by a WHERE clause. Return
7159 ** ONEPASS_SINGLE (1) if the statement can operation directly because only
7160 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
7161 ** optimization can be used on multiple
7162 **
7163 ** If the ONEPASS optimization is used (if this routine returns true)
7164 ** then also write the indices of open cursors used by ONEPASS
7165 ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
7166 ** table and iaCur[1] gets the cursor used by an auxiliary index.
7167 ** Either value may be -1, indicating that cursor is not used.
7168 ** Any cursors returned will have been opened for writing.
7169 **
7170 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
7171 ** unable to use the ONEPASS optimization.
7172 */
7173 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
7174 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
7175 #ifdef WHERETRACE_ENABLED
7176 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
7177 sqlite3DebugPrintf("%s cursors: %d %d\n",
7178 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
7179 aiCur[0], aiCur[1]);
7180 }
7181 #endif
7182 return pWInfo->eOnePass;
7183 }
7184
7185 /*
7186 ** Move the content of pSrc into pDest
7187 */
7188 static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
7189 pDest->n = pSrc->n;
7190 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
7191 }
7192
7193 /*
7194 ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
7195 **
7196 ** The new entry might overwrite an existing entry, or it might be
7197 ** appended, or it might be discarded. Do whatever is the right thing
7198 ** so that pSet keeps the N_OR_COST best entries seen so far.
7199 */
7200 static int whereOrInsert(
7201 WhereOrSet *pSet, /* The WhereOrSet to be updated */
7202 Bitmask prereq, /* Prerequisites of the new entry */
7203 LogEst rRun, /* Run-cost of the new entry */
7204 LogEst nOut /* Number of outputs for the new entry */
7205 ){
7206 u16 i;
7207 WhereOrCost *p;
7208 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
7209 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
7210 goto whereOrInsert_done;
7211 }
7212 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
7213 return 0;
7214 }
7215 }
7216 if( pSet->n<N_OR_COST ){
7217 p = &pSet->a[pSet->n++];
7218 p->nOut = nOut;
7219 }else{
7220 p = pSet->a;
7221 for(i=1; i<pSet->n; i++){
7222 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
7223 }
7224 if( p->rRun<=rRun ) return 0;
7225 }
7226 whereOrInsert_done:
7227 p->prereq = prereq;
7228 p->rRun = rRun;
7229 if( p->nOut>nOut ) p->nOut = nOut;
7230 return 1;
7231 }
7232
7233 /*
7234 ** Return the bitmask for the given cursor number. Return 0 if
7235 ** iCursor is not in the set.
7236 */
7237 SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
7238 int i;
7239 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
7240 for(i=0; i<pMaskSet->n; i++){
7241 if( pMaskSet->ix[i]==iCursor ){
7242 return MASKBIT(i);
7243 }
7244 }
7245 return 0;
7246 }
7247
7248 /*
7249 ** Create a new mask for cursor iCursor.
7250 **
7251 ** There is one cursor per table in the FROM clause. The number of
7252 ** tables in the FROM clause is limited by a test early in the
7253 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
7254 ** array will never overflow.
7255 */
7256 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
7257 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
7258 pMaskSet->ix[pMaskSet->n++] = iCursor;
7259 }
7260
7261 /*
7262 ** Advance to the next WhereTerm that matches according to the criteria
7263 ** established when the pScan object was initialized by whereScanInit().
7264 ** Return NULL if there are no more matching WhereTerms.
7265 */
7266 static WhereTerm *whereScanNext(WhereScan *pScan){
7267 int iCur; /* The cursor on the LHS of the term */
7268 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
7269 Expr *pX; /* An expression being tested */
7270 WhereClause *pWC; /* Shorthand for pScan->pWC */
7271 WhereTerm *pTerm; /* The term being tested */
7272 int k = pScan->k; /* Where to start scanning */
7273
7274 while( pScan->iEquiv<=pScan->nEquiv ){
7275 iCur = pScan->aiCur[pScan->iEquiv-1];
7276 iColumn = pScan->aiColumn[pScan->iEquiv-1];
7277 if( iColumn==XN_EXPR && pScan->pIdxExpr==0 ) return 0;
7278 while( (pWC = pScan->pWC)!=0 ){
7279 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
7280 if( pTerm->leftCursor==iCur
7281 && pTerm->u.leftColumn==iColumn
7282 && (iColumn!=XN_EXPR
7283 || sqlite3ExprCompare(pTerm->pExpr->pLeft,pScan->pIdxExpr,iCur)==0)
7284 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
7285 ){
7286 if( (pTerm->eOperator & WO_EQUIV)!=0
7287 && pScan->nEquiv<ArraySize(pScan->aiCur)
7288 && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
7289 ){
7290 int j;
7291 for(j=0; j<pScan->nEquiv; j++){
7292 if( pScan->aiCur[j]==pX->iTable
7293 && pScan->aiColumn[j]==pX->iColumn ){
7294 break;
7295 }
7296 }
7297 if( j==pScan->nEquiv ){
7298 pScan->aiCur[j] = pX->iTable;
7299 pScan->aiColumn[j] = pX->iColumn;
7300 pScan->nEquiv++;
7301 }
7302 }
7303 if( (pTerm->eOperator & pScan->opMask)!=0 ){
7304 /* Verify the affinity and collating sequence match */
7305 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
7306 CollSeq *pColl;
7307 Parse *pParse = pWC->pWInfo->pParse;
7308 pX = pTerm->pExpr;
7309 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
7310 continue;
7311 }
7312 assert(pX->pLeft);
7313 pColl = sqlite3BinaryCompareCollSeq(pParse,
7314 pX->pLeft, pX->pRight);
7315 if( pColl==0 ) pColl = pParse->db->pDfltColl;
7316 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
7317 continue;
7318 }
7319 }
7320 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
7321 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
7322 && pX->iTable==pScan->aiCur[0]
7323 && pX->iColumn==pScan->aiColumn[0]
7324 ){
7325 testcase( pTerm->eOperator & WO_IS );
7326 continue;
7327 }
7328 pScan->k = k+1;
7329 return pTerm;
7330 }
7331 }
7332 }
7333 pScan->pWC = pScan->pWC->pOuter;
7334 k = 0;
7335 }
7336 pScan->pWC = pScan->pOrigWC;
7337 k = 0;
7338 pScan->iEquiv++;
7339 }
7340 return 0;
7341 }
7342
7343 /*
7344 ** Initialize a WHERE clause scanner object. Return a pointer to the
7345 ** first match. Return NULL if there are no matches.
7346 **
7347 ** The scanner will be searching the WHERE clause pWC. It will look
7348 ** for terms of the form "X <op> <expr>" where X is column iColumn of table
7349 ** iCur. The <op> must be one of the operators described by opMask.
7350 **
7351 ** If the search is for X and the WHERE clause contains terms of the
7352 ** form X=Y then this routine might also return terms of the form
7353 ** "Y <op> <expr>". The number of levels of transitivity is limited,
7354 ** but is enough to handle most commonly occurring SQL statements.
7355 **
7356 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with
7357 ** index pIdx.
7358 */
7359 static WhereTerm *whereScanInit(
7360 WhereScan *pScan, /* The WhereScan object being initialized */
7361 WhereClause *pWC, /* The WHERE clause to be scanned */
7362 int iCur, /* Cursor to scan for */
7363 int iColumn, /* Column to scan for */
7364 u32 opMask, /* Operator(s) to scan for */
7365 Index *pIdx /* Must be compatible with this index */
7366 ){
7367 int j = 0;
7368
7369 /* memset(pScan, 0, sizeof(*pScan)); */
7370 pScan->pOrigWC = pWC;
7371 pScan->pWC = pWC;
7372 pScan->pIdxExpr = 0;
7373 if( pIdx ){
7374 j = iColumn;
7375 iColumn = pIdx->aiColumn[j];
7376 if( iColumn==XN_EXPR ) pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
7377 }
7378 if( pIdx && iColumn>=0 ){
7379 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
7380 pScan->zCollName = pIdx->azColl[j];
7381 }else{
7382 pScan->idxaff = 0;
7383 pScan->zCollName = 0;
7384 }
7385 pScan->opMask = opMask;
7386 pScan->k = 0;
7387 pScan->aiCur[0] = iCur;
7388 pScan->aiColumn[0] = iColumn;
7389 pScan->nEquiv = 1;
7390 pScan->iEquiv = 1;
7391 return whereScanNext(pScan);
7392 }
7393
7394 /*
7395 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
7396 ** where X is a reference to the iColumn of table iCur and <op> is one of
7397 ** the WO_xx operator codes specified by the op parameter.
7398 ** Return a pointer to the term. Return 0 if not found.
7399 **
7400 ** If pIdx!=0 then search for terms matching the iColumn-th column of pIdx
7401 ** rather than the iColumn-th column of table iCur.
7402 **
7403 ** The term returned might by Y=<expr> if there is another constraint in
7404 ** the WHERE clause that specifies that X=Y. Any such constraints will be
7405 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
7406 ** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
7407 ** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
7408 ** other equivalent values. Hence a search for X will return <expr> if X=A1
7409 ** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
7410 **
7411 ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
7412 ** then try for the one with no dependencies on <expr> - in other words where
7413 ** <expr> is a constant expression of some kind. Only return entries of
7414 ** the form "X <op> Y" where Y is a column in another table if no terms of
7415 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
7416 ** exist, try to return a term that does not use WO_EQUIV.
7417 */
7418 SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
7419 WhereClause *pWC, /* The WHERE clause to be searched */
7420 int iCur, /* Cursor number of LHS */
7421 int iColumn, /* Column number of LHS */
7422 Bitmask notReady, /* RHS must not overlap with this mask */
7423 u32 op, /* Mask of WO_xx values describing operator */
7424 Index *pIdx /* Must be compatible with this index, if not NULL */
7425 ){
7426 WhereTerm *pResult = 0;
7427 WhereTerm *p;
7428 WhereScan scan;
7429
7430 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
7431 op &= WO_EQ|WO_IS;
7432 while( p ){
7433 if( (p->prereqRight & notReady)==0 ){
7434 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
7435 testcase( p->eOperator & WO_IS );
7436 return p;
7437 }
7438 if( pResult==0 ) pResult = p;
7439 }
7440 p = whereScanNext(&scan);
7441 }
7442 return pResult;
7443 }
7444
7445 /*
7446 ** This function searches pList for an entry that matches the iCol-th column
7447 ** of index pIdx.
7448 **
7449 ** If such an expression is found, its index in pList->a[] is returned. If
7450 ** no expression is found, -1 is returned.
7451 */
7452 static int findIndexCol(
7453 Parse *pParse, /* Parse context */
7454 ExprList *pList, /* Expression list to search */
7455 int iBase, /* Cursor for table associated with pIdx */
7456 Index *pIdx, /* Index to match column of */
7457 int iCol /* Column of index to match */
7458 ){
7459 int i;
7460 const char *zColl = pIdx->azColl[iCol];
7461
7462 for(i=0; i<pList->nExpr; i++){
7463 Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
7464 if( p->op==TK_COLUMN
7465 && p->iColumn==pIdx->aiColumn[iCol]
7466 && p->iTable==iBase
7467 ){
7468 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
7469 if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){
7470 return i;
7471 }
7472 }
7473 }
7474
7475 return -1;
7476 }
7477
7478 /*
7479 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
7480 */
7481 static int indexColumnNotNull(Index *pIdx, int iCol){
7482 int j;
7483 assert( pIdx!=0 );
7484 assert( iCol>=0 && iCol<pIdx->nColumn );
7485 j = pIdx->aiColumn[iCol];
7486 if( j>=0 ){
7487 return pIdx->pTable->aCol[j].notNull;
7488 }else if( j==(-1) ){
7489 return 1;
7490 }else{
7491 assert( j==(-2) );
7492 return 0; /* Assume an indexed expression can always yield a NULL */
7493
7494 }
7495 }
7496
7497 /*
7498 ** Return true if the DISTINCT expression-list passed as the third argument
7499 ** is redundant.
7500 **
7501 ** A DISTINCT list is redundant if any subset of the columns in the
7502 ** DISTINCT list are collectively unique and individually non-null.
7503 */
7504 static int isDistinctRedundant(
7505 Parse *pParse, /* Parsing context */
7506 SrcList *pTabList, /* The FROM clause */
7507 WhereClause *pWC, /* The WHERE clause */
7508 ExprList *pDistinct /* The result set that needs to be DISTINCT */
7509 ){
7510 Table *pTab;
7511 Index *pIdx;
7512 int i;
7513 int iBase;
7514
7515 /* If there is more than one table or sub-select in the FROM clause of
7516 ** this query, then it will not be possible to show that the DISTINCT
7517 ** clause is redundant. */
7518 if( pTabList->nSrc!=1 ) return 0;
7519 iBase = pTabList->a[0].iCursor;
7520 pTab = pTabList->a[0].pTab;
7521
7522 /* If any of the expressions is an IPK column on table iBase, then return
7523 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
7524 ** current SELECT is a correlated sub-query.
7525 */
7526 for(i=0; i<pDistinct->nExpr; i++){
7527 Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
7528 if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
7529 }
7530
7531 /* Loop through all indices on the table, checking each to see if it makes
7532 ** the DISTINCT qualifier redundant. It does so if:
7533 **
7534 ** 1. The index is itself UNIQUE, and
7535 **
7536 ** 2. All of the columns in the index are either part of the pDistinct
7537 ** list, or else the WHERE clause contains a term of the form "col=X",
7538 ** where X is a constant value. The collation sequences of the
7539 ** comparison and select-list expressions must match those of the index.
7540 **
7541 ** 3. All of those index columns for which the WHERE clause does not
7542 ** contain a "col=X" term are subject to a NOT NULL constraint.
7543 */
7544 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
7545 if( !IsUniqueIndex(pIdx) ) continue;
7546 for(i=0; i<pIdx->nKeyCol; i++){
7547 if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){
7548 if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
7549 if( indexColumnNotNull(pIdx, i)==0 ) break;
7550 }
7551 }
7552 if( i==pIdx->nKeyCol ){
7553 /* This index implies that the DISTINCT qualifier is redundant. */
7554 return 1;
7555 }
7556 }
7557
7558 return 0;
7559 }
7560
7561
7562 /*
7563 ** Estimate the logarithm of the input value to base 2.
7564 */
7565 static LogEst estLog(LogEst N){
7566 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
7567 }
7568
7569 /*
7570 ** Convert OP_Column opcodes to OP_Copy in previously generated code.
7571 **
7572 ** This routine runs over generated VDBE code and translates OP_Column
7573 ** opcodes into OP_Copy when the table is being accessed via co-routine
7574 ** instead of via table lookup.
7575 **
7576 ** If the bIncrRowid parameter is 0, then any OP_Rowid instructions on
7577 ** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero,
7578 ** then each OP_Rowid is transformed into an instruction to increment the
7579 ** value stored in its output register.
7580 */
7581 static void translateColumnToCopy(
7582 Vdbe *v, /* The VDBE containing code to translate */
7583 int iStart, /* Translate from this opcode to the end */
7584 int iTabCur, /* OP_Column/OP_Rowid references to this table */
7585 int iRegister, /* The first column is in this register */
7586 int bIncrRowid /* If non-zero, transform OP_rowid to OP_AddImm(1) */
7587 ){
7588 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
7589 int iEnd = sqlite3VdbeCurrentAddr(v);
7590 for(; iStart<iEnd; iStart++, pOp++){
7591 if( pOp->p1!=iTabCur ) continue;
7592 if( pOp->opcode==OP_Column ){
7593 pOp->opcode = OP_Copy;
7594 pOp->p1 = pOp->p2 + iRegister;
7595 pOp->p2 = pOp->p3;
7596 pOp->p3 = 0;
7597 }else if( pOp->opcode==OP_Rowid ){
7598 if( bIncrRowid ){
7599 /* Increment the value stored in the P2 operand of the OP_Rowid. */
7600 pOp->opcode = OP_AddImm;
7601 pOp->p1 = pOp->p2;
7602 pOp->p2 = 1;
7603 }else{
7604 pOp->opcode = OP_Null;
7605 pOp->p1 = 0;
7606 pOp->p3 = 0;
7607 }
7608 }
7609 }
7610 }
7611
7612 /*
7613 ** Two routines for printing the content of an sqlite3_index_info
7614 ** structure. Used for testing and debugging only. If neither
7615 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
7616 ** are no-ops.
7617 */
7618 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
7619 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
7620 int i;
7621 if( !sqlite3WhereTrace ) return;
7622 for(i=0; i<p->nConstraint; i++){
7623 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
7624 i,
7625 p->aConstraint[i].iColumn,
7626 p->aConstraint[i].iTermOffset,
7627 p->aConstraint[i].op,
7628 p->aConstraint[i].usable);
7629 }
7630 for(i=0; i<p->nOrderBy; i++){
7631 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
7632 i,
7633 p->aOrderBy[i].iColumn,
7634 p->aOrderBy[i].desc);
7635 }
7636 }
7637 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
7638 int i;
7639 if( !sqlite3WhereTrace ) return;
7640 for(i=0; i<p->nConstraint; i++){
7641 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
7642 i,
7643 p->aConstraintUsage[i].argvIndex,
7644 p->aConstraintUsage[i].omit);
7645 }
7646 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
7647 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
7648 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
7649 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
7650 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
7651 }
7652 #else
7653 #define TRACE_IDX_INPUTS(A)
7654 #define TRACE_IDX_OUTPUTS(A)
7655 #endif
7656
7657 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
7658 /*
7659 ** Return TRUE if the WHERE clause term pTerm is of a form where it
7660 ** could be used with an index to access pSrc, assuming an appropriate
7661 ** index existed.
7662 */
7663 static int termCanDriveIndex(
7664 WhereTerm *pTerm, /* WHERE clause term to check */
7665 struct SrcList_item *pSrc, /* Table we are trying to access */
7666 Bitmask notReady /* Tables in outer loops of the join */
7667 ){
7668 char aff;
7669 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
7670 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
7671 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
7672 if( pTerm->u.leftColumn<0 ) return 0;
7673 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
7674 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
7675 testcase( pTerm->pExpr->op==TK_IS );
7676 return 1;
7677 }
7678 #endif
7679
7680
7681 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
7682 /*
7683 ** Generate code to construct the Index object for an automatic index
7684 ** and to set up the WhereLevel object pLevel so that the code generator
7685 ** makes use of the automatic index.
7686 */
7687 static void constructAutomaticIndex(
7688 Parse *pParse, /* The parsing context */
7689 WhereClause *pWC, /* The WHERE clause */
7690 struct SrcList_item *pSrc, /* The FROM clause term to get the next index */
7691 Bitmask notReady, /* Mask of cursors that are not available */
7692 WhereLevel *pLevel /* Write new index here */
7693 ){
7694 int nKeyCol; /* Number of columns in the constructed index */
7695 WhereTerm *pTerm; /* A single term of the WHERE clause */
7696 WhereTerm *pWCEnd; /* End of pWC->a[] */
7697 Index *pIdx; /* Object describing the transient index */
7698 Vdbe *v; /* Prepared statement under construction */
7699 int addrInit; /* Address of the initialization bypass jump */
7700 Table *pTable; /* The table being indexed */
7701 int addrTop; /* Top of the index fill loop */
7702 int regRecord; /* Register holding an index record */
7703 int n; /* Column counter */
7704 int i; /* Loop counter */
7705 int mxBitCol; /* Maximum column in pSrc->colUsed */
7706 CollSeq *pColl; /* Collating sequence to on a column */
7707 WhereLoop *pLoop; /* The Loop object */
7708 char *zNotUsed; /* Extra space on the end of pIdx */
7709 Bitmask idxCols; /* Bitmap of columns used for indexing */
7710 Bitmask extraCols; /* Bitmap of additional columns */
7711 u8 sentWarning = 0; /* True if a warnning has been issued */
7712 Expr *pPartial = 0; /* Partial Index Expression */
7713 int iContinue = 0; /* Jump here to skip excluded rows */
7714 struct SrcList_item *pTabItem; /* FROM clause term being indexed */
7715 int addrCounter = 0; /* Address where integer counter is initialized */
7716 int regBase; /* Array of registers where record is assembled */
7717
7718 /* Generate code to skip over the creation and initialization of the
7719 ** transient index on 2nd and subsequent iterations of the loop. */
7720 v = pParse->pVdbe;
7721 assert( v!=0 );
7722 addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v);
7723
7724 /* Count the number of columns that will be added to the index
7725 ** and used to match WHERE clause constraints */
7726 nKeyCol = 0;
7727 pTable = pSrc->pTab;
7728 pWCEnd = &pWC->a[pWC->nTerm];
7729 pLoop = pLevel->pWLoop;
7730 idxCols = 0;
7731 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
7732 Expr *pExpr = pTerm->pExpr;
7733 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */
7734 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */
7735 || pLoop->prereq!=0 ); /* table of a LEFT JOIN */
7736 if( pLoop->prereq==0
7737 && (pTerm->wtFlags & TERM_VIRTUAL)==0
7738 && !ExprHasProperty(pExpr, EP_FromJoin)
7739 && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
7740 pPartial = sqlite3ExprAnd(pParse->db, pPartial,
7741 sqlite3ExprDup(pParse->db, pExpr, 0));
7742 }
7743 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
7744 int iCol = pTerm->u.leftColumn;
7745 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
7746 testcase( iCol==BMS );
7747 testcase( iCol==BMS-1 );
7748 if( !sentWarning ){
7749 sqlite3_log(SQLITE_WARNING_AUTOINDEX,
7750 "automatic index on %s(%s)", pTable->zName,
7751 pTable->aCol[iCol].zName);
7752 sentWarning = 1;
7753 }
7754 if( (idxCols & cMask)==0 ){
7755 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
7756 goto end_auto_index_create;
7757 }
7758 pLoop->aLTerm[nKeyCol++] = pTerm;
7759 idxCols |= cMask;
7760 }
7761 }
7762 }
7763 assert( nKeyCol>0 );
7764 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
7765 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
7766 | WHERE_AUTO_INDEX;
7767
7768 /* Count the number of additional columns needed to create a
7769 ** covering index. A "covering index" is an index that contains all
7770 ** columns that are needed by the query. With a covering index, the
7771 ** original table never needs to be accessed. Automatic indices must
7772 ** be a covering index because the index will not be updated if the
7773 ** original table changes and the index and table cannot both be used
7774 ** if they go out of sync.
7775 */
7776 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
7777 mxBitCol = MIN(BMS-1,pTable->nCol);
7778 testcase( pTable->nCol==BMS-1 );
7779 testcase( pTable->nCol==BMS-2 );
7780 for(i=0; i<mxBitCol; i++){
7781 if( extraCols & MASKBIT(i) ) nKeyCol++;
7782 }
7783 if( pSrc->colUsed & MASKBIT(BMS-1) ){
7784 nKeyCol += pTable->nCol - BMS + 1;
7785 }
7786
7787 /* Construct the Index object to describe this index */
7788 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
7789 if( pIdx==0 ) goto end_auto_index_create;
7790 pLoop->u.btree.pIndex = pIdx;
7791 pIdx->zName = "auto-index";
7792 pIdx->pTable = pTable;
7793 n = 0;
7794 idxCols = 0;
7795 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
7796 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
7797 int iCol = pTerm->u.leftColumn;
7798 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
7799 testcase( iCol==BMS-1 );
7800 testcase( iCol==BMS );
7801 if( (idxCols & cMask)==0 ){
7802 Expr *pX = pTerm->pExpr;
7803 idxCols |= cMask;
7804 pIdx->aiColumn[n] = pTerm->u.leftColumn;
7805 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
7806 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
7807 n++;
7808 }
7809 }
7810 }
7811 assert( (u32)n==pLoop->u.btree.nEq );
7812
7813 /* Add additional columns needed to make the automatic index into
7814 ** a covering index */
7815 for(i=0; i<mxBitCol; i++){
7816 if( extraCols & MASKBIT(i) ){
7817 pIdx->aiColumn[n] = i;
7818 pIdx->azColl[n] = sqlite3StrBINARY;
7819 n++;
7820 }
7821 }
7822 if( pSrc->colUsed & MASKBIT(BMS-1) ){
7823 for(i=BMS-1; i<pTable->nCol; i++){
7824 pIdx->aiColumn[n] = i;
7825 pIdx->azColl[n] = sqlite3StrBINARY;
7826 n++;
7827 }
7828 }
7829 assert( n==nKeyCol );
7830 pIdx->aiColumn[n] = XN_ROWID;
7831 pIdx->azColl[n] = sqlite3StrBINARY;
7832
7833 /* Create the automatic index */
7834 assert( pLevel->iIdxCur>=0 );
7835 pLevel->iIdxCur = pParse->nTab++;
7836 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
7837 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
7838 VdbeComment((v, "for %s", pTable->zName));
7839
7840 /* Fill the automatic index with content */
7841 sqlite3ExprCachePush(pParse);
7842 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
7843 if( pTabItem->fg.viaCoroutine ){
7844 int regYield = pTabItem->regReturn;
7845 addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0);
7846 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
7847 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield);
7848 VdbeCoverage(v);
7849 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
7850 }else{
7851 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
7852 }
7853 if( pPartial ){
7854 iContinue = sqlite3VdbeMakeLabel(v);
7855 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
7856 pLoop->wsFlags |= WHERE_PARTIALIDX;
7857 }
7858 regRecord = sqlite3GetTempReg(pParse);
7859 regBase = sqlite3GenerateIndexKey(
7860 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
7861 );
7862 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
7863 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
7864 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
7865 if( pTabItem->fg.viaCoroutine ){
7866 sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
7867 translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult, 1);
7868 sqlite3VdbeGoto(v, addrTop);
7869 pTabItem->fg.viaCoroutine = 0;
7870 }else{
7871 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
7872 }
7873 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
7874 sqlite3VdbeJumpHere(v, addrTop);
7875 sqlite3ReleaseTempReg(pParse, regRecord);
7876 sqlite3ExprCachePop(pParse);
7877
7878 /* Jump here when skipping the initialization */
7879 sqlite3VdbeJumpHere(v, addrInit);
7880
7881 end_auto_index_create:
7882 sqlite3ExprDelete(pParse->db, pPartial);
7883 }
7884 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
7885
7886 #ifndef SQLITE_OMIT_VIRTUALTABLE
7887 /*
7888 ** Allocate and populate an sqlite3_index_info structure. It is the
7889 ** responsibility of the caller to eventually release the structure
7890 ** by passing the pointer returned by this function to sqlite3_free().
7891 */
7892 static sqlite3_index_info *allocateIndexInfo(
7893 Parse *pParse,
7894 WhereClause *pWC,
7895 Bitmask mUnusable, /* Ignore terms with these prereqs */
7896 struct SrcList_item *pSrc,
7897 ExprList *pOrderBy
7898 ){
7899 int i, j;
7900 int nTerm;
7901 struct sqlite3_index_constraint *pIdxCons;
7902 struct sqlite3_index_orderby *pIdxOrderBy;
7903 struct sqlite3_index_constraint_usage *pUsage;
7904 WhereTerm *pTerm;
7905 int nOrderBy;
7906 sqlite3_index_info *pIdxInfo;
7907
7908 /* Count the number of possible WHERE clause constraints referring
7909 ** to this virtual table */
7910 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
7911 if( pTerm->leftCursor != pSrc->iCursor ) continue;
7912 if( pTerm->prereqRight & mUnusable ) continue;
7913 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
7914 testcase( pTerm->eOperator & WO_IN );
7915 testcase( pTerm->eOperator & WO_ISNULL );
7916 testcase( pTerm->eOperator & WO_IS );
7917 testcase( pTerm->eOperator & WO_ALL );
7918 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
7919 if( pTerm->wtFlags & TERM_VNULL ) continue;
7920 assert( pTerm->u.leftColumn>=(-1) );
7921 nTerm++;
7922 }
7923
7924 /* If the ORDER BY clause contains only columns in the current
7925 ** virtual table then allocate space for the aOrderBy part of
7926 ** the sqlite3_index_info structure.
7927 */
7928 nOrderBy = 0;
7929 if( pOrderBy ){
7930 int n = pOrderBy->nExpr;
7931 for(i=0; i<n; i++){
7932 Expr *pExpr = pOrderBy->a[i].pExpr;
7933 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
7934 }
7935 if( i==n){
7936 nOrderBy = n;
7937 }
7938 }
7939
7940 /* Allocate the sqlite3_index_info structure
7941 */
7942 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
7943 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
7944 + sizeof(*pIdxOrderBy)*nOrderBy );
7945 if( pIdxInfo==0 ){
7946 sqlite3ErrorMsg(pParse, "out of memory");
7947 return 0;
7948 }
7949
7950 /* Initialize the structure. The sqlite3_index_info structure contains
7951 ** many fields that are declared "const" to prevent xBestIndex from
7952 ** changing them. We have to do some funky casting in order to
7953 ** initialize those fields.
7954 */
7955 pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
7956 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
7957 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
7958 *(int*)&pIdxInfo->nConstraint = nTerm;
7959 *(int*)&pIdxInfo->nOrderBy = nOrderBy;
7960 *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
7961 *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
7962 *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
7963 pUsage;
7964
7965 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
7966 u8 op;
7967 if( pTerm->leftCursor != pSrc->iCursor ) continue;
7968 if( pTerm->prereqRight & mUnusable ) continue;
7969 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
7970 testcase( pTerm->eOperator & WO_IN );
7971 testcase( pTerm->eOperator & WO_IS );
7972 testcase( pTerm->eOperator & WO_ISNULL );
7973 testcase( pTerm->eOperator & WO_ALL );
7974 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
7975 if( pTerm->wtFlags & TERM_VNULL ) continue;
7976 assert( pTerm->u.leftColumn>=(-1) );
7977 pIdxCons[j].iColumn = pTerm->u.leftColumn;
7978 pIdxCons[j].iTermOffset = i;
7979 op = (u8)pTerm->eOperator & WO_ALL;
7980 if( op==WO_IN ) op = WO_EQ;
7981 if( op==WO_MATCH ){
7982 op = pTerm->eMatchOp;
7983 }
7984 pIdxCons[j].op = op;
7985 /* The direct assignment in the previous line is possible only because
7986 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
7987 ** following asserts verify this fact. */
7988 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
7989 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
7990 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
7991 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
7992 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
7993 assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
7994 assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
7995 j++;
7996 }
7997 for(i=0; i<nOrderBy; i++){
7998 Expr *pExpr = pOrderBy->a[i].pExpr;
7999 pIdxOrderBy[i].iColumn = pExpr->iColumn;
8000 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
8001 }
8002
8003 return pIdxInfo;
8004 }
8005
8006 /*
8007 ** The table object reference passed as the second argument to this function
8008 ** must represent a virtual table. This function invokes the xBestIndex()
8009 ** method of the virtual table with the sqlite3_index_info object that
8010 ** comes in as the 3rd argument to this function.
8011 **
8012 ** If an error occurs, pParse is populated with an error message and a
8013 ** non-zero value is returned. Otherwise, 0 is returned and the output
8014 ** part of the sqlite3_index_info structure is left populated.
8015 **
8016 ** Whether or not an error is returned, it is the responsibility of the
8017 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
8018 ** that this is required.
8019 */
8020 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
8021 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
8022 int i;
8023 int rc;
8024
8025 TRACE_IDX_INPUTS(p);
8026 rc = pVtab->pModule->xBestIndex(pVtab, p);
8027 TRACE_IDX_OUTPUTS(p);
8028
8029 if( rc!=SQLITE_OK ){
8030 if( rc==SQLITE_NOMEM ){
8031 pParse->db->mallocFailed = 1;
8032 }else if( !pVtab->zErrMsg ){
8033 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
8034 }else{
8035 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
8036 }
8037 }
8038 sqlite3_free(pVtab->zErrMsg);
8039 pVtab->zErrMsg = 0;
8040
8041 for(i=0; i<p->nConstraint; i++){
8042 if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
8043 sqlite3ErrorMsg(pParse,
8044 "table %s: xBestIndex returned an invalid plan", pTab->zName);
8045 }
8046 }
8047
8048 return pParse->nErr;
8049 }
8050 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
8051
8052 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8053 /*
8054 ** Estimate the location of a particular key among all keys in an
8055 ** index. Store the results in aStat as follows:
8056 **
8057 ** aStat[0] Est. number of rows less than pRec
8058 ** aStat[1] Est. number of rows equal to pRec
8059 **
8060 ** Return the index of the sample that is the smallest sample that
8061 ** is greater than or equal to pRec. Note that this index is not an index
8062 ** into the aSample[] array - it is an index into a virtual set of samples
8063 ** based on the contents of aSample[] and the number of fields in record
8064 ** pRec.
8065 */
8066 static int whereKeyStats(
8067 Parse *pParse, /* Database connection */
8068 Index *pIdx, /* Index to consider domain of */
8069 UnpackedRecord *pRec, /* Vector of values to consider */
8070 int roundUp, /* Round up if true. Round down if false */
8071 tRowcnt *aStat /* OUT: stats written here */
8072 ){
8073 IndexSample *aSample = pIdx->aSample;
8074 int iCol; /* Index of required stats in anEq[] etc. */
8075 int i; /* Index of first sample >= pRec */
8076 int iSample; /* Smallest sample larger than or equal to pRec */
8077 int iMin = 0; /* Smallest sample not yet tested */
8078 int iTest; /* Next sample to test */
8079 int res; /* Result of comparison operation */
8080 int nField; /* Number of fields in pRec */
8081 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
8082
8083 #ifndef SQLITE_DEBUG
8084 UNUSED_PARAMETER( pParse );
8085 #endif
8086 assert( pRec!=0 );
8087 assert( pIdx->nSample>0 );
8088 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
8089
8090 /* Do a binary search to find the first sample greater than or equal
8091 ** to pRec. If pRec contains a single field, the set of samples to search
8092 ** is simply the aSample[] array. If the samples in aSample[] contain more
8093 ** than one fields, all fields following the first are ignored.
8094 **
8095 ** If pRec contains N fields, where N is more than one, then as well as the
8096 ** samples in aSample[] (truncated to N fields), the search also has to
8097 ** consider prefixes of those samples. For example, if the set of samples
8098 ** in aSample is:
8099 **
8100 ** aSample[0] = (a, 5)
8101 ** aSample[1] = (a, 10)
8102 ** aSample[2] = (b, 5)
8103 ** aSample[3] = (c, 100)
8104 ** aSample[4] = (c, 105)
8105 **
8106 ** Then the search space should ideally be the samples above and the
8107 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
8108 ** the code actually searches this set:
8109 **
8110 ** 0: (a)
8111 ** 1: (a, 5)
8112 ** 2: (a, 10)
8113 ** 3: (a, 10)
8114 ** 4: (b)
8115 ** 5: (b, 5)
8116 ** 6: (c)
8117 ** 7: (c, 100)
8118 ** 8: (c, 105)
8119 ** 9: (c, 105)
8120 **
8121 ** For each sample in the aSample[] array, N samples are present in the
8122 ** effective sample array. In the above, samples 0 and 1 are based on
8123 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
8124 **
8125 ** Often, sample i of each block of N effective samples has (i+1) fields.
8126 ** Except, each sample may be extended to ensure that it is greater than or
8127 ** equal to the previous sample in the array. For example, in the above,
8128 ** sample 2 is the first sample of a block of N samples, so at first it
8129 ** appears that it should be 1 field in size. However, that would make it
8130 ** smaller than sample 1, so the binary search would not work. As a result,
8131 ** it is extended to two fields. The duplicates that this creates do not
8132 ** cause any problems.
8133 */
8134 nField = pRec->nField;
8135 iCol = 0;
8136 iSample = pIdx->nSample * nField;
8137 do{
8138 int iSamp; /* Index in aSample[] of test sample */
8139 int n; /* Number of fields in test sample */
8140
8141 iTest = (iMin+iSample)/2;
8142 iSamp = iTest / nField;
8143 if( iSamp>0 ){
8144 /* The proposed effective sample is a prefix of sample aSample[iSamp].
8145 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
8146 ** fields that is greater than the previous effective sample. */
8147 for(n=(iTest % nField) + 1; n<nField; n++){
8148 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
8149 }
8150 }else{
8151 n = iTest + 1;
8152 }
8153
8154 pRec->nField = n;
8155 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
8156 if( res<0 ){
8157 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
8158 iMin = iTest+1;
8159 }else if( res==0 && n<nField ){
8160 iLower = aSample[iSamp].anLt[n-1];
8161 iMin = iTest+1;
8162 res = -1;
8163 }else{
8164 iSample = iTest;
8165 iCol = n-1;
8166 }
8167 }while( res && iMin<iSample );
8168 i = iSample / nField;
8169
8170 #ifdef SQLITE_DEBUG
8171 /* The following assert statements check that the binary search code
8172 ** above found the right answer. This block serves no purpose other
8173 ** than to invoke the asserts. */
8174 if( pParse->db->mallocFailed==0 ){
8175 if( res==0 ){
8176 /* If (res==0) is true, then pRec must be equal to sample i. */
8177 assert( i<pIdx->nSample );
8178 assert( iCol==nField-1 );
8179 pRec->nField = nField;
8180 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
8181 || pParse->db->mallocFailed
8182 );
8183 }else{
8184 /* Unless i==pIdx->nSample, indicating that pRec is larger than
8185 ** all samples in the aSample[] array, pRec must be smaller than the
8186 ** (iCol+1) field prefix of sample i. */
8187 assert( i<=pIdx->nSample && i>=0 );
8188 pRec->nField = iCol+1;
8189 assert( i==pIdx->nSample
8190 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
8191 || pParse->db->mallocFailed );
8192
8193 /* if i==0 and iCol==0, then record pRec is smaller than all samples
8194 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
8195 ** be greater than or equal to the (iCol) field prefix of sample i.
8196 ** If (i>0), then pRec must also be greater than sample (i-1). */
8197 if( iCol>0 ){
8198 pRec->nField = iCol;
8199 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
8200 || pParse->db->mallocFailed );
8201 }
8202 if( i>0 ){
8203 pRec->nField = nField;
8204 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
8205 || pParse->db->mallocFailed );
8206 }
8207 }
8208 }
8209 #endif /* ifdef SQLITE_DEBUG */
8210
8211 if( res==0 ){
8212 /* Record pRec is equal to sample i */
8213 assert( iCol==nField-1 );
8214 aStat[0] = aSample[i].anLt[iCol];
8215 aStat[1] = aSample[i].anEq[iCol];
8216 }else{
8217 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
8218 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
8219 ** is larger than all samples in the array. */
8220 tRowcnt iUpper, iGap;
8221 if( i>=pIdx->nSample ){
8222 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
8223 }else{
8224 iUpper = aSample[i].anLt[iCol];
8225 }
8226
8227 if( iLower>=iUpper ){
8228 iGap = 0;
8229 }else{
8230 iGap = iUpper - iLower;
8231 }
8232 if( roundUp ){
8233 iGap = (iGap*2)/3;
8234 }else{
8235 iGap = iGap/3;
8236 }
8237 aStat[0] = iLower + iGap;
8238 aStat[1] = pIdx->aAvgEq[iCol];
8239 }
8240
8241 /* Restore the pRec->nField value before returning. */
8242 pRec->nField = nField;
8243 return i;
8244 }
8245 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
8246
8247 /*
8248 ** If it is not NULL, pTerm is a term that provides an upper or lower
8249 ** bound on a range scan. Without considering pTerm, it is estimated
8250 ** that the scan will visit nNew rows. This function returns the number
8251 ** estimated to be visited after taking pTerm into account.
8252 **
8253 ** If the user explicitly specified a likelihood() value for this term,
8254 ** then the return value is the likelihood multiplied by the number of
8255 ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
8256 ** has a likelihood of 0.50, and any other term a likelihood of 0.25.
8257 */
8258 static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
8259 LogEst nRet = nNew;
8260 if( pTerm ){
8261 if( pTerm->truthProb<=0 ){
8262 nRet += pTerm->truthProb;
8263 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
8264 nRet -= 20; assert( 20==sqlite3LogEst(4) );
8265 }
8266 }
8267 return nRet;
8268 }
8269
8270
8271 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8272 /*
8273 ** Return the affinity for a single column of an index.
8274 */
8275 static char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
8276 assert( iCol>=0 && iCol<pIdx->nColumn );
8277 if( !pIdx->zColAff ){
8278 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB;
8279 }
8280 return pIdx->zColAff[iCol];
8281 }
8282 #endif
8283
8284
8285 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8286 /*
8287 ** This function is called to estimate the number of rows visited by a
8288 ** range-scan on a skip-scan index. For example:
8289 **
8290 ** CREATE INDEX i1 ON t1(a, b, c);
8291 ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
8292 **
8293 ** Value pLoop->nOut is currently set to the estimated number of rows
8294 ** visited for scanning (a=? AND b=?). This function reduces that estimate
8295 ** by some factor to account for the (c BETWEEN ? AND ?) expression based
8296 ** on the stat4 data for the index. this scan will be peformed multiple
8297 ** times (once for each (a,b) combination that matches a=?) is dealt with
8298 ** by the caller.
8299 **
8300 ** It does this by scanning through all stat4 samples, comparing values
8301 ** extracted from pLower and pUpper with the corresponding column in each
8302 ** sample. If L and U are the number of samples found to be less than or
8303 ** equal to the values extracted from pLower and pUpper respectively, and
8304 ** N is the total number of samples, the pLoop->nOut value is adjusted
8305 ** as follows:
8306 **
8307 ** nOut = nOut * ( min(U - L, 1) / N )
8308 **
8309 ** If pLower is NULL, or a value cannot be extracted from the term, L is
8310 ** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
8311 ** U is set to N.
8312 **
8313 ** Normally, this function sets *pbDone to 1 before returning. However,
8314 ** if no value can be extracted from either pLower or pUpper (and so the
8315 ** estimate of the number of rows delivered remains unchanged), *pbDone
8316 ** is left as is.
8317 **
8318 ** If an error occurs, an SQLite error code is returned. Otherwise,
8319 ** SQLITE_OK.
8320 */
8321 static int whereRangeSkipScanEst(
8322 Parse *pParse, /* Parsing & code generating context */
8323 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
8324 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
8325 WhereLoop *pLoop, /* Update the .nOut value of this loop */
8326 int *pbDone /* Set to true if at least one expr. value extracted */
8327 ){
8328 Index *p = pLoop->u.btree.pIndex;
8329 int nEq = pLoop->u.btree.nEq;
8330 sqlite3 *db = pParse->db;
8331 int nLower = -1;
8332 int nUpper = p->nSample+1;
8333 int rc = SQLITE_OK;
8334 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
8335 CollSeq *pColl;
8336
8337 sqlite3_value *p1 = 0; /* Value extracted from pLower */
8338 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
8339 sqlite3_value *pVal = 0; /* Value extracted from record */
8340
8341 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
8342 if( pLower ){
8343 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
8344 nLower = 0;
8345 }
8346 if( pUpper && rc==SQLITE_OK ){
8347 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
8348 nUpper = p2 ? 0 : p->nSample;
8349 }
8350
8351 if( p1 || p2 ){
8352 int i;
8353 int nDiff;
8354 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
8355 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
8356 if( rc==SQLITE_OK && p1 ){
8357 int res = sqlite3MemCompare(p1, pVal, pColl);
8358 if( res>=0 ) nLower++;
8359 }
8360 if( rc==SQLITE_OK && p2 ){
8361 int res = sqlite3MemCompare(p2, pVal, pColl);
8362 if( res>=0 ) nUpper++;
8363 }
8364 }
8365 nDiff = (nUpper - nLower);
8366 if( nDiff<=0 ) nDiff = 1;
8367
8368 /* If there is both an upper and lower bound specified, and the
8369 ** comparisons indicate that they are close together, use the fallback
8370 ** method (assume that the scan visits 1/64 of the rows) for estimating
8371 ** the number of rows visited. Otherwise, estimate the number of rows
8372 ** using the method described in the header comment for this function. */
8373 if( nDiff!=1 || pUpper==0 || pLower==0 ){
8374 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
8375 pLoop->nOut -= nAdjust;
8376 *pbDone = 1;
8377 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
8378 nLower, nUpper, nAdjust*-1, pLoop->nOut));
8379 }
8380
8381 }else{
8382 assert( *pbDone==0 );
8383 }
8384
8385 sqlite3ValueFree(p1);
8386 sqlite3ValueFree(p2);
8387 sqlite3ValueFree(pVal);
8388
8389 return rc;
8390 }
8391 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
8392
8393 /*
8394 ** This function is used to estimate the number of rows that will be visited
8395 ** by scanning an index for a range of values. The range may have an upper
8396 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
8397 ** and lower bounds are represented by pLower and pUpper respectively. For
8398 ** example, assuming that index p is on t1(a):
8399 **
8400 ** ... FROM t1 WHERE a > ? AND a < ? ...
8401 ** |_____| |_____|
8402 ** | |
8403 ** pLower pUpper
8404 **
8405 ** If either of the upper or lower bound is not present, then NULL is passed in
8406 ** place of the corresponding WhereTerm.
8407 **
8408 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
8409 ** column subject to the range constraint. Or, equivalently, the number of
8410 ** equality constraints optimized by the proposed index scan. For example,
8411 ** assuming index p is on t1(a, b), and the SQL query is:
8412 **
8413 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
8414 **
8415 ** then nEq is set to 1 (as the range restricted column, b, is the second
8416 ** left-most column of the index). Or, if the query is:
8417 **
8418 ** ... FROM t1 WHERE a > ? AND a < ? ...
8419 **
8420 ** then nEq is set to 0.
8421 **
8422 ** When this function is called, *pnOut is set to the sqlite3LogEst() of the
8423 ** number of rows that the index scan is expected to visit without
8424 ** considering the range constraints. If nEq is 0, then *pnOut is the number of
8425 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
8426 ** to account for the range constraints pLower and pUpper.
8427 **
8428 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
8429 ** used, a single range inequality reduces the search space by a factor of 4.
8430 ** and a pair of constraints (x>? AND x<?) reduces the expected number of
8431 ** rows visited by a factor of 64.
8432 */
8433 static int whereRangeScanEst(
8434 Parse *pParse, /* Parsing & code generating context */
8435 WhereLoopBuilder *pBuilder,
8436 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
8437 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
8438 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
8439 ){
8440 int rc = SQLITE_OK;
8441 int nOut = pLoop->nOut;
8442 LogEst nNew;
8443
8444 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8445 Index *p = pLoop->u.btree.pIndex;
8446 int nEq = pLoop->u.btree.nEq;
8447
8448 if( p->nSample>0 && nEq<p->nSampleCol ){
8449 if( nEq==pBuilder->nRecValid ){
8450 UnpackedRecord *pRec = pBuilder->pRec;
8451 tRowcnt a[2];
8452 u8 aff;
8453
8454 /* Variable iLower will be set to the estimate of the number of rows in
8455 ** the index that are less than the lower bound of the range query. The
8456 ** lower bound being the concatenation of $P and $L, where $P is the
8457 ** key-prefix formed by the nEq values matched against the nEq left-most
8458 ** columns of the index, and $L is the value in pLower.
8459 **
8460 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
8461 ** is not a simple variable or literal value), the lower bound of the
8462 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
8463 ** if $L is available, whereKeyStats() is called for both ($P) and
8464 ** ($P:$L) and the larger of the two returned values is used.
8465 **
8466 ** Similarly, iUpper is to be set to the estimate of the number of rows
8467 ** less than the upper bound of the range query. Where the upper bound
8468 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
8469 ** of iUpper are requested of whereKeyStats() and the smaller used.
8470 **
8471 ** The number of rows between the two bounds is then just iUpper-iLower.
8472 */
8473 tRowcnt iLower; /* Rows less than the lower bound */
8474 tRowcnt iUpper; /* Rows less than the upper bound */
8475 int iLwrIdx = -2; /* aSample[] for the lower bound */
8476 int iUprIdx = -1; /* aSample[] for the upper bound */
8477
8478 if( pRec ){
8479 testcase( pRec->nField!=pBuilder->nRecValid );
8480 pRec->nField = pBuilder->nRecValid;
8481 }
8482 aff = sqlite3IndexColumnAffinity(pParse->db, p, nEq);
8483 assert( nEq!=p->nKeyCol || aff==SQLITE_AFF_INTEGER );
8484 /* Determine iLower and iUpper using ($P) only. */
8485 if( nEq==0 ){
8486 iLower = 0;
8487 iUpper = p->nRowEst0;
8488 }else{
8489 /* Note: this call could be optimized away - since the same values must
8490 ** have been requested when testing key $P in whereEqualScanEst(). */
8491 whereKeyStats(pParse, p, pRec, 0, a);
8492 iLower = a[0];
8493 iUpper = a[0] + a[1];
8494 }
8495
8496 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
8497 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
8498 assert( p->aSortOrder!=0 );
8499 if( p->aSortOrder[nEq] ){
8500 /* The roles of pLower and pUpper are swapped for a DESC index */
8501 SWAP(WhereTerm*, pLower, pUpper);
8502 }
8503
8504 /* If possible, improve on the iLower estimate using ($P:$L). */
8505 if( pLower ){
8506 int bOk; /* True if value is extracted from pExpr */
8507 Expr *pExpr = pLower->pExpr->pRight;
8508 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
8509 if( rc==SQLITE_OK && bOk ){
8510 tRowcnt iNew;
8511 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
8512 iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
8513 if( iNew>iLower ) iLower = iNew;
8514 nOut--;
8515 pLower = 0;
8516 }
8517 }
8518
8519 /* If possible, improve on the iUpper estimate using ($P:$U). */
8520 if( pUpper ){
8521 int bOk; /* True if value is extracted from pExpr */
8522 Expr *pExpr = pUpper->pExpr->pRight;
8523 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
8524 if( rc==SQLITE_OK && bOk ){
8525 tRowcnt iNew;
8526 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
8527 iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
8528 if( iNew<iUpper ) iUpper = iNew;
8529 nOut--;
8530 pUpper = 0;
8531 }
8532 }
8533
8534 pBuilder->pRec = pRec;
8535 if( rc==SQLITE_OK ){
8536 if( iUpper>iLower ){
8537 nNew = sqlite3LogEst(iUpper - iLower);
8538 /* TUNING: If both iUpper and iLower are derived from the same
8539 ** sample, then assume they are 4x more selective. This brings
8540 ** the estimated selectivity more in line with what it would be
8541 ** if estimated without the use of STAT3/4 tables. */
8542 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
8543 }else{
8544 nNew = 10; assert( 10==sqlite3LogEst(2) );
8545 }
8546 if( nNew<nOut ){
8547 nOut = nNew;
8548 }
8549 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
8550 (u32)iLower, (u32)iUpper, nOut));
8551 }
8552 }else{
8553 int bDone = 0;
8554 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
8555 if( bDone ) return rc;
8556 }
8557 }
8558 #else
8559 UNUSED_PARAMETER(pParse);
8560 UNUSED_PARAMETER(pBuilder);
8561 assert( pLower || pUpper );
8562 #endif
8563 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
8564 nNew = whereRangeAdjust(pLower, nOut);
8565 nNew = whereRangeAdjust(pUpper, nNew);
8566
8567 /* TUNING: If there is both an upper and lower limit and neither limit
8568 ** has an application-defined likelihood(), assume the range is
8569 ** reduced by an additional 75%. This means that, by default, an open-ended
8570 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
8571 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
8572 ** match 1/64 of the index. */
8573 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
8574 nNew -= 20;
8575 }
8576
8577 nOut -= (pLower!=0) + (pUpper!=0);
8578 if( nNew<10 ) nNew = 10;
8579 if( nNew<nOut ) nOut = nNew;
8580 #if defined(WHERETRACE_ENABLED)
8581 if( pLoop->nOut>nOut ){
8582 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
8583 pLoop->nOut, nOut));
8584 }
8585 #endif
8586 pLoop->nOut = (LogEst)nOut;
8587 return rc;
8588 }
8589
8590 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8591 /*
8592 ** Estimate the number of rows that will be returned based on
8593 ** an equality constraint x=VALUE and where that VALUE occurs in
8594 ** the histogram data. This only works when x is the left-most
8595 ** column of an index and sqlite_stat3 histogram data is available
8596 ** for that index. When pExpr==NULL that means the constraint is
8597 ** "x IS NULL" instead of "x=VALUE".
8598 **
8599 ** Write the estimated row count into *pnRow and return SQLITE_OK.
8600 ** If unable to make an estimate, leave *pnRow unchanged and return
8601 ** non-zero.
8602 **
8603 ** This routine can fail if it is unable to load a collating sequence
8604 ** required for string comparison, or if unable to allocate memory
8605 ** for a UTF conversion required for comparison. The error is stored
8606 ** in the pParse structure.
8607 */
8608 static int whereEqualScanEst(
8609 Parse *pParse, /* Parsing & code generating context */
8610 WhereLoopBuilder *pBuilder,
8611 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
8612 tRowcnt *pnRow /* Write the revised row estimate here */
8613 ){
8614 Index *p = pBuilder->pNew->u.btree.pIndex;
8615 int nEq = pBuilder->pNew->u.btree.nEq;
8616 UnpackedRecord *pRec = pBuilder->pRec;
8617 u8 aff; /* Column affinity */
8618 int rc; /* Subfunction return code */
8619 tRowcnt a[2]; /* Statistics */
8620 int bOk;
8621
8622 assert( nEq>=1 );
8623 assert( nEq<=p->nColumn );
8624 assert( p->aSample!=0 );
8625 assert( p->nSample>0 );
8626 assert( pBuilder->nRecValid<nEq );
8627
8628 /* If values are not available for all fields of the index to the left
8629 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
8630 if( pBuilder->nRecValid<(nEq-1) ){
8631 return SQLITE_NOTFOUND;
8632 }
8633
8634 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
8635 ** below would return the same value. */
8636 if( nEq>=p->nColumn ){
8637 *pnRow = 1;
8638 return SQLITE_OK;
8639 }
8640
8641 aff = sqlite3IndexColumnAffinity(pParse->db, p, nEq-1);
8642 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
8643 pBuilder->pRec = pRec;
8644 if( rc!=SQLITE_OK ) return rc;
8645 if( bOk==0 ) return SQLITE_NOTFOUND;
8646 pBuilder->nRecValid = nEq;
8647
8648 whereKeyStats(pParse, p, pRec, 0, a);
8649 WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1]));
8650 *pnRow = a[1];
8651
8652 return rc;
8653 }
8654 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
8655
8656 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8657 /*
8658 ** Estimate the number of rows that will be returned based on
8659 ** an IN constraint where the right-hand side of the IN operator
8660 ** is a list of values. Example:
8661 **
8662 ** WHERE x IN (1,2,3,4)
8663 **
8664 ** Write the estimated row count into *pnRow and return SQLITE_OK.
8665 ** If unable to make an estimate, leave *pnRow unchanged and return
8666 ** non-zero.
8667 **
8668 ** This routine can fail if it is unable to load a collating sequence
8669 ** required for string comparison, or if unable to allocate memory
8670 ** for a UTF conversion required for comparison. The error is stored
8671 ** in the pParse structure.
8672 */
8673 static int whereInScanEst(
8674 Parse *pParse, /* Parsing & code generating context */
8675 WhereLoopBuilder *pBuilder,
8676 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
8677 tRowcnt *pnRow /* Write the revised row estimate here */
8678 ){
8679 Index *p = pBuilder->pNew->u.btree.pIndex;
8680 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
8681 int nRecValid = pBuilder->nRecValid;
8682 int rc = SQLITE_OK; /* Subfunction return code */
8683 tRowcnt nEst; /* Number of rows for a single term */
8684 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
8685 int i; /* Loop counter */
8686
8687 assert( p->aSample!=0 );
8688 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
8689 nEst = nRow0;
8690 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
8691 nRowEst += nEst;
8692 pBuilder->nRecValid = nRecValid;
8693 }
8694
8695 if( rc==SQLITE_OK ){
8696 if( nRowEst > nRow0 ) nRowEst = nRow0;
8697 *pnRow = nRowEst;
8698 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
8699 }
8700 assert( pBuilder->nRecValid==nRecValid );
8701 return rc;
8702 }
8703 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
8704
8705
8706 #ifdef WHERETRACE_ENABLED
8707 /*
8708 ** Print the content of a WhereTerm object
8709 */
8710 static void whereTermPrint(WhereTerm *pTerm, int iTerm){
8711 if( pTerm==0 ){
8712 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
8713 }else{
8714 char zType[4];
8715 memcpy(zType, "...", 4);
8716 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
8717 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
8718 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
8719 sqlite3DebugPrintf(
8720 "TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x wtFlags=0x%04x\n",
8721 iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb,
8722 pTerm->eOperator, pTerm->wtFlags);
8723 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
8724 }
8725 }
8726 #endif
8727
8728 #ifdef WHERETRACE_ENABLED
8729 /*
8730 ** Print a WhereLoop object for debugging purposes
8731 */
8732 static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
8733 WhereInfo *pWInfo = pWC->pWInfo;
8734 int nb = 1+(pWInfo->pTabList->nSrc+7)/8;
8735 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
8736 Table *pTab = pItem->pTab;
8737 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
8738 p->iTab, nb, p->maskSelf, nb, p->prereq);
8739 sqlite3DebugPrintf(" %12s",
8740 pItem->zAlias ? pItem->zAlias : pTab->zName);
8741 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
8742 const char *zName;
8743 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
8744 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
8745 int i = sqlite3Strlen30(zName) - 1;
8746 while( zName[i]!='_' ) i--;
8747 zName += i;
8748 }
8749 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
8750 }else{
8751 sqlite3DebugPrintf("%20s","");
8752 }
8753 }else{
8754 char *z;
8755 if( p->u.vtab.idxStr ){
8756 z = sqlite3_mprintf("(%d,\"%s\",%x)",
8757 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
8758 }else{
8759 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
8760 }
8761 sqlite3DebugPrintf(" %-19s", z);
8762 sqlite3_free(z);
8763 }
8764 if( p->wsFlags & WHERE_SKIPSCAN ){
8765 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
8766 }else{
8767 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
8768 }
8769 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
8770 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
8771 int i;
8772 for(i=0; i<p->nLTerm; i++){
8773 whereTermPrint(p->aLTerm[i], i);
8774 }
8775 }
8776 }
8777 #endif
8778
8779 /*
8780 ** Convert bulk memory into a valid WhereLoop that can be passed
8781 ** to whereLoopClear harmlessly.
8782 */
8783 static void whereLoopInit(WhereLoop *p){
8784 p->aLTerm = p->aLTermSpace;
8785 p->nLTerm = 0;
8786 p->nLSlot = ArraySize(p->aLTermSpace);
8787 p->wsFlags = 0;
8788 }
8789
8790 /*
8791 ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
8792 */
8793 static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
8794 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
8795 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
8796 sqlite3_free(p->u.vtab.idxStr);
8797 p->u.vtab.needFree = 0;
8798 p->u.vtab.idxStr = 0;
8799 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
8800 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
8801 sqlite3DbFree(db, p->u.btree.pIndex);
8802 p->u.btree.pIndex = 0;
8803 }
8804 }
8805 }
8806
8807 /*
8808 ** Deallocate internal memory used by a WhereLoop object
8809 */
8810 static void whereLoopClear(sqlite3 *db, WhereLoop *p){
8811 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
8812 whereLoopClearUnion(db, p);
8813 whereLoopInit(p);
8814 }
8815
8816 /*
8817 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
8818 */
8819 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
8820 WhereTerm **paNew;
8821 if( p->nLSlot>=n ) return SQLITE_OK;
8822 n = (n+7)&~7;
8823 paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
8824 if( paNew==0 ) return SQLITE_NOMEM;
8825 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
8826 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
8827 p->aLTerm = paNew;
8828 p->nLSlot = n;
8829 return SQLITE_OK;
8830 }
8831
8832 /*
8833 ** Transfer content from the second pLoop into the first.
8834 */
8835 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
8836 whereLoopClearUnion(db, pTo);
8837 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
8838 memset(&pTo->u, 0, sizeof(pTo->u));
8839 return SQLITE_NOMEM;
8840 }
8841 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
8842 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
8843 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
8844 pFrom->u.vtab.needFree = 0;
8845 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
8846 pFrom->u.btree.pIndex = 0;
8847 }
8848 return SQLITE_OK;
8849 }
8850
8851 /*
8852 ** Delete a WhereLoop object
8853 */
8854 static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
8855 whereLoopClear(db, p);
8856 sqlite3DbFree(db, p);
8857 }
8858
8859 /*
8860 ** Free a WhereInfo structure
8861 */
8862 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
8863 if( ALWAYS(pWInfo) ){
8864 int i;
8865 for(i=0; i<pWInfo->nLevel; i++){
8866 WhereLevel *pLevel = &pWInfo->a[i];
8867 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
8868 sqlite3DbFree(db, pLevel->u.in.aInLoop);
8869 }
8870 }
8871 sqlite3WhereClauseClear(&pWInfo->sWC);
8872 while( pWInfo->pLoops ){
8873 WhereLoop *p = pWInfo->pLoops;
8874 pWInfo->pLoops = p->pNextLoop;
8875 whereLoopDelete(db, p);
8876 }
8877 sqlite3DbFree(db, pWInfo);
8878 }
8879 }
8880
8881 /*
8882 ** Return TRUE if all of the following are true:
8883 **
8884 ** (1) X has the same or lower cost that Y
8885 ** (2) X is a proper subset of Y
8886 ** (3) X skips at least as many columns as Y
8887 **
8888 ** By "proper subset" we mean that X uses fewer WHERE clause terms
8889 ** than Y and that every WHERE clause term used by X is also used
8890 ** by Y.
8891 **
8892 ** If X is a proper subset of Y then Y is a better choice and ought
8893 ** to have a lower cost. This routine returns TRUE when that cost
8894 ** relationship is inverted and needs to be adjusted. The third rule
8895 ** was added because if X uses skip-scan less than Y it still might
8896 ** deserve a lower cost even if it is a proper subset of Y.
8897 */
8898 static int whereLoopCheaperProperSubset(
8899 const WhereLoop *pX, /* First WhereLoop to compare */
8900 const WhereLoop *pY /* Compare against this WhereLoop */
8901 ){
8902 int i, j;
8903 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
8904 return 0; /* X is not a subset of Y */
8905 }
8906 if( pY->nSkip > pX->nSkip ) return 0;
8907 if( pX->rRun >= pY->rRun ){
8908 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
8909 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
8910 }
8911 for(i=pX->nLTerm-1; i>=0; i--){
8912 if( pX->aLTerm[i]==0 ) continue;
8913 for(j=pY->nLTerm-1; j>=0; j--){
8914 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
8915 }
8916 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
8917 }
8918 return 1; /* All conditions meet */
8919 }
8920
8921 /*
8922 ** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so
8923 ** that:
8924 **
8925 ** (1) pTemplate costs less than any other WhereLoops that are a proper
8926 ** subset of pTemplate
8927 **
8928 ** (2) pTemplate costs more than any other WhereLoops for which pTemplate
8929 ** is a proper subset.
8930 **
8931 ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
8932 ** WHERE clause terms than Y and that every WHERE clause term used by X is
8933 ** also used by Y.
8934 */
8935 static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
8936 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
8937 for(; p; p=p->pNextLoop){
8938 if( p->iTab!=pTemplate->iTab ) continue;
8939 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
8940 if( whereLoopCheaperProperSubset(p, pTemplate) ){
8941 /* Adjust pTemplate cost downward so that it is cheaper than its
8942 ** subset p. */
8943 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
8944 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
8945 pTemplate->rRun = p->rRun;
8946 pTemplate->nOut = p->nOut - 1;
8947 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
8948 /* Adjust pTemplate cost upward so that it is costlier than p since
8949 ** pTemplate is a proper subset of p */
8950 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
8951 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
8952 pTemplate->rRun = p->rRun;
8953 pTemplate->nOut = p->nOut + 1;
8954 }
8955 }
8956 }
8957
8958 /*
8959 ** Search the list of WhereLoops in *ppPrev looking for one that can be
8960 ** supplanted by pTemplate.
8961 **
8962 ** Return NULL if the WhereLoop list contains an entry that can supplant
8963 ** pTemplate, in other words if pTemplate does not belong on the list.
8964 **
8965 ** If pX is a WhereLoop that pTemplate can supplant, then return the
8966 ** link that points to pX.
8967 **
8968 ** If pTemplate cannot supplant any existing element of the list but needs
8969 ** to be added to the list, then return a pointer to the tail of the list.
8970 */
8971 static WhereLoop **whereLoopFindLesser(
8972 WhereLoop **ppPrev,
8973 const WhereLoop *pTemplate
8974 ){
8975 WhereLoop *p;
8976 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
8977 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
8978 /* If either the iTab or iSortIdx values for two WhereLoop are different
8979 ** then those WhereLoops need to be considered separately. Neither is
8980 ** a candidate to replace the other. */
8981 continue;
8982 }
8983 /* In the current implementation, the rSetup value is either zero
8984 ** or the cost of building an automatic index (NlogN) and the NlogN
8985 ** is the same for compatible WhereLoops. */
8986 assert( p->rSetup==0 || pTemplate->rSetup==0
8987 || p->rSetup==pTemplate->rSetup );
8988
8989 /* whereLoopAddBtree() always generates and inserts the automatic index
8990 ** case first. Hence compatible candidate WhereLoops never have a larger
8991 ** rSetup. Call this SETUP-INVARIANT */
8992 assert( p->rSetup>=pTemplate->rSetup );
8993
8994 /* Any loop using an appliation-defined index (or PRIMARY KEY or
8995 ** UNIQUE constraint) with one or more == constraints is better
8996 ** than an automatic index. Unless it is a skip-scan. */
8997 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
8998 && (pTemplate->nSkip)==0
8999 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
9000 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
9001 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
9002 ){
9003 break;
9004 }
9005
9006 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
9007 ** discarded. WhereLoop p is better if:
9008 ** (1) p has no more dependencies than pTemplate, and
9009 ** (2) p has an equal or lower cost than pTemplate
9010 */
9011 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
9012 && p->rSetup<=pTemplate->rSetup /* (2a) */
9013 && p->rRun<=pTemplate->rRun /* (2b) */
9014 && p->nOut<=pTemplate->nOut /* (2c) */
9015 ){
9016 return 0; /* Discard pTemplate */
9017 }
9018
9019 /* If pTemplate is always better than p, then cause p to be overwritten
9020 ** with pTemplate. pTemplate is better than p if:
9021 ** (1) pTemplate has no more dependences than p, and
9022 ** (2) pTemplate has an equal or lower cost than p.
9023 */
9024 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
9025 && p->rRun>=pTemplate->rRun /* (2a) */
9026 && p->nOut>=pTemplate->nOut /* (2b) */
9027 ){
9028 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
9029 break; /* Cause p to be overwritten by pTemplate */
9030 }
9031 }
9032 return ppPrev;
9033 }
9034
9035 /*
9036 ** Insert or replace a WhereLoop entry using the template supplied.
9037 **
9038 ** An existing WhereLoop entry might be overwritten if the new template
9039 ** is better and has fewer dependencies. Or the template will be ignored
9040 ** and no insert will occur if an existing WhereLoop is faster and has
9041 ** fewer dependencies than the template. Otherwise a new WhereLoop is
9042 ** added based on the template.
9043 **
9044 ** If pBuilder->pOrSet is not NULL then we care about only the
9045 ** prerequisites and rRun and nOut costs of the N best loops. That
9046 ** information is gathered in the pBuilder->pOrSet object. This special
9047 ** processing mode is used only for OR clause processing.
9048 **
9049 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
9050 ** still might overwrite similar loops with the new template if the
9051 ** new template is better. Loops may be overwritten if the following
9052 ** conditions are met:
9053 **
9054 ** (1) They have the same iTab.
9055 ** (2) They have the same iSortIdx.
9056 ** (3) The template has same or fewer dependencies than the current loop
9057 ** (4) The template has the same or lower cost than the current loop
9058 */
9059 static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
9060 WhereLoop **ppPrev, *p;
9061 WhereInfo *pWInfo = pBuilder->pWInfo;
9062 sqlite3 *db = pWInfo->pParse->db;
9063
9064 /* If pBuilder->pOrSet is defined, then only keep track of the costs
9065 ** and prereqs.
9066 */
9067 if( pBuilder->pOrSet!=0 ){
9068 if( pTemplate->nLTerm ){
9069 #if WHERETRACE_ENABLED
9070 u16 n = pBuilder->pOrSet->n;
9071 int x =
9072 #endif
9073 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
9074 pTemplate->nOut);
9075 #if WHERETRACE_ENABLED /* 0x8 */
9076 if( sqlite3WhereTrace & 0x8 ){
9077 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
9078 whereLoopPrint(pTemplate, pBuilder->pWC);
9079 }
9080 #endif
9081 }
9082 return SQLITE_OK;
9083 }
9084
9085 /* Look for an existing WhereLoop to replace with pTemplate
9086 */
9087 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
9088 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
9089
9090 if( ppPrev==0 ){
9091 /* There already exists a WhereLoop on the list that is better
9092 ** than pTemplate, so just ignore pTemplate */
9093 #if WHERETRACE_ENABLED /* 0x8 */
9094 if( sqlite3WhereTrace & 0x8 ){
9095 sqlite3DebugPrintf(" skip: ");
9096 whereLoopPrint(pTemplate, pBuilder->pWC);
9097 }
9098 #endif
9099 return SQLITE_OK;
9100 }else{
9101 p = *ppPrev;
9102 }
9103
9104 /* If we reach this point it means that either p[] should be overwritten
9105 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
9106 ** WhereLoop and insert it.
9107 */
9108 #if WHERETRACE_ENABLED /* 0x8 */
9109 if( sqlite3WhereTrace & 0x8 ){
9110 if( p!=0 ){
9111 sqlite3DebugPrintf("replace: ");
9112 whereLoopPrint(p, pBuilder->pWC);
9113 }
9114 sqlite3DebugPrintf(" add: ");
9115 whereLoopPrint(pTemplate, pBuilder->pWC);
9116 }
9117 #endif
9118 if( p==0 ){
9119 /* Allocate a new WhereLoop to add to the end of the list */
9120 *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
9121 if( p==0 ) return SQLITE_NOMEM;
9122 whereLoopInit(p);
9123 p->pNextLoop = 0;
9124 }else{
9125 /* We will be overwriting WhereLoop p[]. But before we do, first
9126 ** go through the rest of the list and delete any other entries besides
9127 ** p[] that are also supplated by pTemplate */
9128 WhereLoop **ppTail = &p->pNextLoop;
9129 WhereLoop *pToDel;
9130 while( *ppTail ){
9131 ppTail = whereLoopFindLesser(ppTail, pTemplate);
9132 if( ppTail==0 ) break;
9133 pToDel = *ppTail;
9134 if( pToDel==0 ) break;
9135 *ppTail = pToDel->pNextLoop;
9136 #if WHERETRACE_ENABLED /* 0x8 */
9137 if( sqlite3WhereTrace & 0x8 ){
9138 sqlite3DebugPrintf(" delete: ");
9139 whereLoopPrint(pToDel, pBuilder->pWC);
9140 }
9141 #endif
9142 whereLoopDelete(db, pToDel);
9143 }
9144 }
9145 whereLoopXfer(db, p, pTemplate);
9146 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
9147 Index *pIndex = p->u.btree.pIndex;
9148 if( pIndex && pIndex->tnum==0 ){
9149 p->u.btree.pIndex = 0;
9150 }
9151 }
9152 return SQLITE_OK;
9153 }
9154
9155 /*
9156 ** Adjust the WhereLoop.nOut value downward to account for terms of the
9157 ** WHERE clause that reference the loop but which are not used by an
9158 ** index.
9159 *
9160 ** For every WHERE clause term that is not used by the index
9161 ** and which has a truth probability assigned by one of the likelihood(),
9162 ** likely(), or unlikely() SQL functions, reduce the estimated number
9163 ** of output rows by the probability specified.
9164 **
9165 ** TUNING: For every WHERE clause term that is not used by the index
9166 ** and which does not have an assigned truth probability, heuristics
9167 ** described below are used to try to estimate the truth probability.
9168 ** TODO --> Perhaps this is something that could be improved by better
9169 ** table statistics.
9170 **
9171 ** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
9172 ** value corresponds to -1 in LogEst notation, so this means decrement
9173 ** the WhereLoop.nOut field for every such WHERE clause term.
9174 **
9175 ** Heuristic 2: If there exists one or more WHERE clause terms of the
9176 ** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
9177 ** final output row estimate is no greater than 1/4 of the total number
9178 ** of rows in the table. In other words, assume that x==EXPR will filter
9179 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
9180 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
9181 ** on the "x" column and so in that case only cap the output row estimate
9182 ** at 1/2 instead of 1/4.
9183 */
9184 static void whereLoopOutputAdjust(
9185 WhereClause *pWC, /* The WHERE clause */
9186 WhereLoop *pLoop, /* The loop to adjust downward */
9187 LogEst nRow /* Number of rows in the entire table */
9188 ){
9189 WhereTerm *pTerm, *pX;
9190 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
9191 int i, j, k;
9192 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
9193
9194 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
9195 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
9196 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
9197 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
9198 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
9199 for(j=pLoop->nLTerm-1; j>=0; j--){
9200 pX = pLoop->aLTerm[j];
9201 if( pX==0 ) continue;
9202 if( pX==pTerm ) break;
9203 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
9204 }
9205 if( j<0 ){
9206 if( pTerm->truthProb<=0 ){
9207 /* If a truth probability is specified using the likelihood() hints,
9208 ** then use the probability provided by the application. */
9209 pLoop->nOut += pTerm->truthProb;
9210 }else{
9211 /* In the absence of explicit truth probabilities, use heuristics to
9212 ** guess a reasonable truth probability. */
9213 pLoop->nOut--;
9214 if( pTerm->eOperator&(WO_EQ|WO_IS) ){
9215 Expr *pRight = pTerm->pExpr->pRight;
9216 testcase( pTerm->pExpr->op==TK_IS );
9217 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
9218 k = 10;
9219 }else{
9220 k = 20;
9221 }
9222 if( iReduce<k ) iReduce = k;
9223 }
9224 }
9225 }
9226 }
9227 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce;
9228 }
9229
9230 /*
9231 ** Adjust the cost C by the costMult facter T. This only occurs if
9232 ** compiled with -DSQLITE_ENABLE_COSTMULT
9233 */
9234 #ifdef SQLITE_ENABLE_COSTMULT
9235 # define ApplyCostMultiplier(C,T) C += T
9236 #else
9237 # define ApplyCostMultiplier(C,T)
9238 #endif
9239
9240 /*
9241 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
9242 ** index pIndex. Try to match one more.
9243 **
9244 ** When this function is called, pBuilder->pNew->nOut contains the
9245 ** number of rows expected to be visited by filtering using the nEq
9246 ** terms only. If it is modified, this value is restored before this
9247 ** function returns.
9248 **
9249 ** If pProbe->tnum==0, that means pIndex is a fake index used for the
9250 ** INTEGER PRIMARY KEY.
9251 */
9252 static int whereLoopAddBtreeIndex(
9253 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
9254 struct SrcList_item *pSrc, /* FROM clause term being analyzed */
9255 Index *pProbe, /* An index on pSrc */
9256 LogEst nInMul /* log(Number of iterations due to IN) */
9257 ){
9258 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
9259 Parse *pParse = pWInfo->pParse; /* Parsing context */
9260 sqlite3 *db = pParse->db; /* Database connection malloc context */
9261 WhereLoop *pNew; /* Template WhereLoop under construction */
9262 WhereTerm *pTerm; /* A WhereTerm under consideration */
9263 int opMask; /* Valid operators for constraints */
9264 WhereScan scan; /* Iterator for WHERE terms */
9265 Bitmask saved_prereq; /* Original value of pNew->prereq */
9266 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
9267 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
9268 u16 saved_nSkip; /* Original value of pNew->nSkip */
9269 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
9270 LogEst saved_nOut; /* Original value of pNew->nOut */
9271 int rc = SQLITE_OK; /* Return code */
9272 LogEst rSize; /* Number of rows in the table */
9273 LogEst rLogSize; /* Logarithm of table size */
9274 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
9275
9276 pNew = pBuilder->pNew;
9277 if( db->mallocFailed ) return SQLITE_NOMEM;
9278
9279 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
9280 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
9281 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
9282 opMask = WO_LT|WO_LE;
9283 }else if( /*pProbe->tnum<=0 ||*/ (pSrc->fg.jointype & JT_LEFT)!=0 ){
9284 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
9285 }else{
9286 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
9287 }
9288 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
9289
9290 assert( pNew->u.btree.nEq<pProbe->nColumn );
9291
9292 saved_nEq = pNew->u.btree.nEq;
9293 saved_nSkip = pNew->nSkip;
9294 saved_nLTerm = pNew->nLTerm;
9295 saved_wsFlags = pNew->wsFlags;
9296 saved_prereq = pNew->prereq;
9297 saved_nOut = pNew->nOut;
9298 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
9299 opMask, pProbe);
9300 pNew->rSetup = 0;
9301 rSize = pProbe->aiRowLogEst[0];
9302 rLogSize = estLog(rSize);
9303 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
9304 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
9305 LogEst rCostIdx;
9306 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
9307 int nIn = 0;
9308 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
9309 int nRecValid = pBuilder->nRecValid;
9310 #endif
9311 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
9312 && indexColumnNotNull(pProbe, saved_nEq)
9313 ){
9314 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
9315 }
9316 if( pTerm->prereqRight & pNew->maskSelf ) continue;
9317
9318 /* Do not allow the upper bound of a LIKE optimization range constraint
9319 ** to mix with a lower range bound from some other source */
9320 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
9321
9322 pNew->wsFlags = saved_wsFlags;
9323 pNew->u.btree.nEq = saved_nEq;
9324 pNew->nLTerm = saved_nLTerm;
9325 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
9326 pNew->aLTerm[pNew->nLTerm++] = pTerm;
9327 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
9328
9329 assert( nInMul==0
9330 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
9331 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
9332 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
9333 );
9334
9335 if( eOp & WO_IN ){
9336 Expr *pExpr = pTerm->pExpr;
9337 pNew->wsFlags |= WHERE_COLUMN_IN;
9338 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
9339 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
9340 nIn = 46; assert( 46==sqlite3LogEst(25) );
9341 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
9342 /* "x IN (value, value, ...)" */
9343 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
9344 }
9345 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
9346 ** changes "x IN (?)" into "x=?". */
9347
9348 }else if( eOp & (WO_EQ|WO_IS) ){
9349 int iCol = pProbe->aiColumn[saved_nEq];
9350 pNew->wsFlags |= WHERE_COLUMN_EQ;
9351 assert( saved_nEq==pNew->u.btree.nEq );
9352 if( iCol==XN_ROWID
9353 || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
9354 ){
9355 if( iCol>=0 && pProbe->uniqNotNull==0 ){
9356 pNew->wsFlags |= WHERE_UNQ_WANTED;
9357 }else{
9358 pNew->wsFlags |= WHERE_ONEROW;
9359 }
9360 }
9361 }else if( eOp & WO_ISNULL ){
9362 pNew->wsFlags |= WHERE_COLUMN_NULL;
9363 }else if( eOp & (WO_GT|WO_GE) ){
9364 testcase( eOp & WO_GT );
9365 testcase( eOp & WO_GE );
9366 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
9367 pBtm = pTerm;
9368 pTop = 0;
9369 if( pTerm->wtFlags & TERM_LIKEOPT ){
9370 /* Range contraints that come from the LIKE optimization are
9371 ** always used in pairs. */
9372 pTop = &pTerm[1];
9373 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
9374 assert( pTop->wtFlags & TERM_LIKEOPT );
9375 assert( pTop->eOperator==WO_LT );
9376 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
9377 pNew->aLTerm[pNew->nLTerm++] = pTop;
9378 pNew->wsFlags |= WHERE_TOP_LIMIT;
9379 }
9380 }else{
9381 assert( eOp & (WO_LT|WO_LE) );
9382 testcase( eOp & WO_LT );
9383 testcase( eOp & WO_LE );
9384 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
9385 pTop = pTerm;
9386 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
9387 pNew->aLTerm[pNew->nLTerm-2] : 0;
9388 }
9389
9390 /* At this point pNew->nOut is set to the number of rows expected to
9391 ** be visited by the index scan before considering term pTerm, or the
9392 ** values of nIn and nInMul. In other words, assuming that all
9393 ** "x IN(...)" terms are replaced with "x = ?". This block updates
9394 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
9395 assert( pNew->nOut==saved_nOut );
9396 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
9397 /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
9398 ** data, using some other estimate. */
9399 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
9400 }else{
9401 int nEq = ++pNew->u.btree.nEq;
9402 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
9403
9404 assert( pNew->nOut==saved_nOut );
9405 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
9406 assert( (eOp & WO_IN) || nIn==0 );
9407 testcase( eOp & WO_IN );
9408 pNew->nOut += pTerm->truthProb;
9409 pNew->nOut -= nIn;
9410 }else{
9411 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
9412 tRowcnt nOut = 0;
9413 if( nInMul==0
9414 && pProbe->nSample
9415 && pNew->u.btree.nEq<=pProbe->nSampleCol
9416 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
9417 ){
9418 Expr *pExpr = pTerm->pExpr;
9419 if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
9420 testcase( eOp & WO_EQ );
9421 testcase( eOp & WO_IS );
9422 testcase( eOp & WO_ISNULL );
9423 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
9424 }else{
9425 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
9426 }
9427 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
9428 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
9429 if( nOut ){
9430 pNew->nOut = sqlite3LogEst(nOut);
9431 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
9432 pNew->nOut -= nIn;
9433 }
9434 }
9435 if( nOut==0 )
9436 #endif
9437 {
9438 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
9439 if( eOp & WO_ISNULL ){
9440 /* TUNING: If there is no likelihood() value, assume that a
9441 ** "col IS NULL" expression matches twice as many rows
9442 ** as (col=?). */
9443 pNew->nOut += 10;
9444 }
9445 }
9446 }
9447 }
9448
9449 /* Set rCostIdx to the cost of visiting selected rows in index. Add
9450 ** it to pNew->rRun, which is currently set to the cost of the index
9451 ** seek only. Then, if this is a non-covering index, add the cost of
9452 ** visiting the rows in the main table. */
9453 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
9454 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
9455 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
9456 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
9457 }
9458 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
9459
9460 nOutUnadjusted = pNew->nOut;
9461 pNew->rRun += nInMul + nIn;
9462 pNew->nOut += nInMul + nIn;
9463 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
9464 rc = whereLoopInsert(pBuilder, pNew);
9465
9466 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
9467 pNew->nOut = saved_nOut;
9468 }else{
9469 pNew->nOut = nOutUnadjusted;
9470 }
9471
9472 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
9473 && pNew->u.btree.nEq<pProbe->nColumn
9474 ){
9475 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
9476 }
9477 pNew->nOut = saved_nOut;
9478 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
9479 pBuilder->nRecValid = nRecValid;
9480 #endif
9481 }
9482 pNew->prereq = saved_prereq;
9483 pNew->u.btree.nEq = saved_nEq;
9484 pNew->nSkip = saved_nSkip;
9485 pNew->wsFlags = saved_wsFlags;
9486 pNew->nOut = saved_nOut;
9487 pNew->nLTerm = saved_nLTerm;
9488
9489 /* Consider using a skip-scan if there are no WHERE clause constraints
9490 ** available for the left-most terms of the index, and if the average
9491 ** number of repeats in the left-most terms is at least 18.
9492 **
9493 ** The magic number 18 is selected on the basis that scanning 17 rows
9494 ** is almost always quicker than an index seek (even though if the index
9495 ** contains fewer than 2^17 rows we assume otherwise in other parts of
9496 ** the code). And, even if it is not, it should not be too much slower.
9497 ** On the other hand, the extra seeks could end up being significantly
9498 ** more expensive. */
9499 assert( 42==sqlite3LogEst(18) );
9500 if( saved_nEq==saved_nSkip
9501 && saved_nEq+1<pProbe->nKeyCol
9502 && pProbe->noSkipScan==0
9503 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
9504 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
9505 ){
9506 LogEst nIter;
9507 pNew->u.btree.nEq++;
9508 pNew->nSkip++;
9509 pNew->aLTerm[pNew->nLTerm++] = 0;
9510 pNew->wsFlags |= WHERE_SKIPSCAN;
9511 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
9512 pNew->nOut -= nIter;
9513 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
9514 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
9515 nIter += 5;
9516 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
9517 pNew->nOut = saved_nOut;
9518 pNew->u.btree.nEq = saved_nEq;
9519 pNew->nSkip = saved_nSkip;
9520 pNew->wsFlags = saved_wsFlags;
9521 }
9522
9523 return rc;
9524 }
9525
9526 /*
9527 ** Return True if it is possible that pIndex might be useful in
9528 ** implementing the ORDER BY clause in pBuilder.
9529 **
9530 ** Return False if pBuilder does not contain an ORDER BY clause or
9531 ** if there is no way for pIndex to be useful in implementing that
9532 ** ORDER BY clause.
9533 */
9534 static int indexMightHelpWithOrderBy(
9535 WhereLoopBuilder *pBuilder,
9536 Index *pIndex,
9537 int iCursor
9538 ){
9539 ExprList *pOB;
9540 ExprList *aColExpr;
9541 int ii, jj;
9542
9543 if( pIndex->bUnordered ) return 0;
9544 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
9545 for(ii=0; ii<pOB->nExpr; ii++){
9546 Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
9547 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
9548 if( pExpr->iColumn<0 ) return 1;
9549 for(jj=0; jj<pIndex->nKeyCol; jj++){
9550 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
9551 }
9552 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
9553 for(jj=0; jj<pIndex->nKeyCol; jj++){
9554 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
9555 if( sqlite3ExprCompare(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
9556 return 1;
9557 }
9558 }
9559 }
9560 }
9561 return 0;
9562 }
9563
9564 /*
9565 ** Return a bitmask where 1s indicate that the corresponding column of
9566 ** the table is used by an index. Only the first 63 columns are considered.
9567 */
9568 static Bitmask columnsInIndex(Index *pIdx){
9569 Bitmask m = 0;
9570 int j;
9571 for(j=pIdx->nColumn-1; j>=0; j--){
9572 int x = pIdx->aiColumn[j];
9573 if( x>=0 ){
9574 testcase( x==BMS-1 );
9575 testcase( x==BMS-2 );
9576 if( x<BMS-1 ) m |= MASKBIT(x);
9577 }
9578 }
9579 return m;
9580 }
9581
9582 /* Check to see if a partial index with pPartIndexWhere can be used
9583 ** in the current query. Return true if it can be and false if not.
9584 */
9585 static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
9586 int i;
9587 WhereTerm *pTerm;
9588 while( pWhere->op==TK_AND ){
9589 if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0;
9590 pWhere = pWhere->pRight;
9591 }
9592 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
9593 Expr *pExpr = pTerm->pExpr;
9594 if( sqlite3ExprImpliesExpr(pExpr, pWhere, iTab)
9595 && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
9596 ){
9597 return 1;
9598 }
9599 }
9600 return 0;
9601 }
9602
9603 /*
9604 ** Add all WhereLoop objects for a single table of the join where the table
9605 ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be
9606 ** a b-tree table, not a virtual table.
9607 **
9608 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
9609 ** are calculated as follows:
9610 **
9611 ** For a full scan, assuming the table (or index) contains nRow rows:
9612 **
9613 ** cost = nRow * 3.0 // full-table scan
9614 ** cost = nRow * K // scan of covering index
9615 ** cost = nRow * (K+3.0) // scan of non-covering index
9616 **
9617 ** where K is a value between 1.1 and 3.0 set based on the relative
9618 ** estimated average size of the index and table records.
9619 **
9620 ** For an index scan, where nVisit is the number of index rows visited
9621 ** by the scan, and nSeek is the number of seek operations required on
9622 ** the index b-tree:
9623 **
9624 ** cost = nSeek * (log(nRow) + K * nVisit) // covering index
9625 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
9626 **
9627 ** Normally, nSeek is 1. nSeek values greater than 1 come about if the
9628 ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
9629 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
9630 **
9631 ** The estimated values (nRow, nVisit, nSeek) often contain a large amount
9632 ** of uncertainty. For this reason, scoring is designed to pick plans that
9633 ** "do the least harm" if the estimates are inaccurate. For example, a
9634 ** log(nRow) factor is omitted from a non-covering index scan in order to
9635 ** bias the scoring in favor of using an index, since the worst-case
9636 ** performance of using an index is far better than the worst-case performance
9637 ** of a full table scan.
9638 */
9639 static int whereLoopAddBtree(
9640 WhereLoopBuilder *pBuilder, /* WHERE clause information */
9641 Bitmask mExtra /* Extra prerequesites for using this table */
9642 ){
9643 WhereInfo *pWInfo; /* WHERE analysis context */
9644 Index *pProbe; /* An index we are evaluating */
9645 Index sPk; /* A fake index object for the primary key */
9646 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
9647 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
9648 SrcList *pTabList; /* The FROM clause */
9649 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
9650 WhereLoop *pNew; /* Template WhereLoop object */
9651 int rc = SQLITE_OK; /* Return code */
9652 int iSortIdx = 1; /* Index number */
9653 int b; /* A boolean value */
9654 LogEst rSize; /* number of rows in the table */
9655 LogEst rLogSize; /* Logarithm of the number of rows in the table */
9656 WhereClause *pWC; /* The parsed WHERE clause */
9657 Table *pTab; /* Table being queried */
9658
9659 pNew = pBuilder->pNew;
9660 pWInfo = pBuilder->pWInfo;
9661 pTabList = pWInfo->pTabList;
9662 pSrc = pTabList->a + pNew->iTab;
9663 pTab = pSrc->pTab;
9664 pWC = pBuilder->pWC;
9665 assert( !IsVirtual(pSrc->pTab) );
9666
9667 if( pSrc->pIBIndex ){
9668 /* An INDEXED BY clause specifies a particular index to use */
9669 pProbe = pSrc->pIBIndex;
9670 }else if( !HasRowid(pTab) ){
9671 pProbe = pTab->pIndex;
9672 }else{
9673 /* There is no INDEXED BY clause. Create a fake Index object in local
9674 ** variable sPk to represent the rowid primary key index. Make this
9675 ** fake index the first in a chain of Index objects with all of the real
9676 ** indices to follow */
9677 Index *pFirst; /* First of real indices on the table */
9678 memset(&sPk, 0, sizeof(Index));
9679 sPk.nKeyCol = 1;
9680 sPk.nColumn = 1;
9681 sPk.aiColumn = &aiColumnPk;
9682 sPk.aiRowLogEst = aiRowEstPk;
9683 sPk.onError = OE_Replace;
9684 sPk.pTable = pTab;
9685 sPk.szIdxRow = pTab->szTabRow;
9686 aiRowEstPk[0] = pTab->nRowLogEst;
9687 aiRowEstPk[1] = 0;
9688 pFirst = pSrc->pTab->pIndex;
9689 if( pSrc->fg.notIndexed==0 ){
9690 /* The real indices of the table are only considered if the
9691 ** NOT INDEXED qualifier is omitted from the FROM clause */
9692 sPk.pNext = pFirst;
9693 }
9694 pProbe = &sPk;
9695 }
9696 rSize = pTab->nRowLogEst;
9697 rLogSize = estLog(rSize);
9698
9699 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
9700 /* Automatic indexes */
9701 if( !pBuilder->pOrSet /* Not part of an OR optimization */
9702 && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0
9703 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
9704 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */
9705 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
9706 && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
9707 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
9708 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
9709 ){
9710 /* Generate auto-index WhereLoops */
9711 WhereTerm *pTerm;
9712 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
9713 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
9714 if( pTerm->prereqRight & pNew->maskSelf ) continue;
9715 if( termCanDriveIndex(pTerm, pSrc, 0) ){
9716 pNew->u.btree.nEq = 1;
9717 pNew->nSkip = 0;
9718 pNew->u.btree.pIndex = 0;
9719 pNew->nLTerm = 1;
9720 pNew->aLTerm[0] = pTerm;
9721 /* TUNING: One-time cost for computing the automatic index is
9722 ** estimated to be X*N*log2(N) where N is the number of rows in
9723 ** the table being indexed and where X is 7 (LogEst=28) for normal
9724 ** tables or 1.375 (LogEst=4) for views and subqueries. The value
9725 ** of X is smaller for views and subqueries so that the query planner
9726 ** will be more aggressive about generating automatic indexes for
9727 ** those objects, since there is no opportunity to add schema
9728 ** indexes on subqueries and views. */
9729 pNew->rSetup = rLogSize + rSize + 4;
9730 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
9731 pNew->rSetup += 24;
9732 }
9733 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
9734 /* TUNING: Each index lookup yields 20 rows in the table. This
9735 ** is more than the usual guess of 10 rows, since we have no way
9736 ** of knowing how selective the index will ultimately be. It would
9737 ** not be unreasonable to make this value much larger. */
9738 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
9739 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
9740 pNew->wsFlags = WHERE_AUTO_INDEX;
9741 pNew->prereq = mExtra | pTerm->prereqRight;
9742 rc = whereLoopInsert(pBuilder, pNew);
9743 }
9744 }
9745 }
9746 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
9747
9748 /* Loop over all indices
9749 */
9750 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
9751 if( pProbe->pPartIdxWhere!=0
9752 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
9753 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
9754 continue; /* Partial index inappropriate for this query */
9755 }
9756 rSize = pProbe->aiRowLogEst[0];
9757 pNew->u.btree.nEq = 0;
9758 pNew->nSkip = 0;
9759 pNew->nLTerm = 0;
9760 pNew->iSortIdx = 0;
9761 pNew->rSetup = 0;
9762 pNew->prereq = mExtra;
9763 pNew->nOut = rSize;
9764 pNew->u.btree.pIndex = pProbe;
9765 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
9766 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
9767 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
9768 if( pProbe->tnum<=0 ){
9769 /* Integer primary key index */
9770 pNew->wsFlags = WHERE_IPK;
9771
9772 /* Full table scan */
9773 pNew->iSortIdx = b ? iSortIdx : 0;
9774 /* TUNING: Cost of full table scan is (N*3.0). */
9775 pNew->rRun = rSize + 16;
9776 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
9777 whereLoopOutputAdjust(pWC, pNew, rSize);
9778 rc = whereLoopInsert(pBuilder, pNew);
9779 pNew->nOut = rSize;
9780 if( rc ) break;
9781 }else{
9782 Bitmask m;
9783 if( pProbe->isCovering ){
9784 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
9785 m = 0;
9786 }else{
9787 m = pSrc->colUsed & ~columnsInIndex(pProbe);
9788 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
9789 }
9790
9791 /* Full scan via index */
9792 if( b
9793 || !HasRowid(pTab)
9794 || ( m==0
9795 && pProbe->bUnordered==0
9796 && (pProbe->szIdxRow<pTab->szTabRow)
9797 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
9798 && sqlite3GlobalConfig.bUseCis
9799 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
9800 )
9801 ){
9802 pNew->iSortIdx = b ? iSortIdx : 0;
9803
9804 /* The cost of visiting the index rows is N*K, where K is
9805 ** between 1.1 and 3.0, depending on the relative sizes of the
9806 ** index and table rows. If this is a non-covering index scan,
9807 ** also add the cost of visiting table rows (N*3.0). */
9808 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
9809 if( m!=0 ){
9810 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
9811 }
9812 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
9813 whereLoopOutputAdjust(pWC, pNew, rSize);
9814 rc = whereLoopInsert(pBuilder, pNew);
9815 pNew->nOut = rSize;
9816 if( rc ) break;
9817 }
9818 }
9819
9820 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
9821 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
9822 sqlite3Stat4ProbeFree(pBuilder->pRec);
9823 pBuilder->nRecValid = 0;
9824 pBuilder->pRec = 0;
9825 #endif
9826
9827 /* If there was an INDEXED BY clause, then only that one index is
9828 ** considered. */
9829 if( pSrc->pIBIndex ) break;
9830 }
9831 return rc;
9832 }
9833
9834 #ifndef SQLITE_OMIT_VIRTUALTABLE
9835 /*
9836 ** Add all WhereLoop objects for a table of the join identified by
9837 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
9838 **
9839 ** If there are no LEFT or CROSS JOIN joins in the query, both mExtra and
9840 ** mUnusable are set to 0. Otherwise, mExtra is a mask of all FROM clause
9841 ** entries that occur before the virtual table in the FROM clause and are
9842 ** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
9843 ** mUnusable mask contains all FROM clause entries that occur after the
9844 ** virtual table and are separated from it by at least one LEFT or
9845 ** CROSS JOIN.
9846 **
9847 ** For example, if the query were:
9848 **
9849 ** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
9850 **
9851 ** then mExtra corresponds to (t1, t2) and mUnusable to (t5, t6).
9852 **
9853 ** All the tables in mExtra must be scanned before the current virtual
9854 ** table. So any terms for which all prerequisites are satisfied by
9855 ** mExtra may be specified as "usable" in all calls to xBestIndex.
9856 ** Conversely, all tables in mUnusable must be scanned after the current
9857 ** virtual table, so any terms for which the prerequisites overlap with
9858 ** mUnusable should always be configured as "not-usable" for xBestIndex.
9859 */
9860 static int whereLoopAddVirtual(
9861 WhereLoopBuilder *pBuilder, /* WHERE clause information */
9862 Bitmask mExtra, /* Tables that must be scanned before this one */
9863 Bitmask mUnusable /* Tables that must be scanned after this one */
9864 ){
9865 WhereInfo *pWInfo; /* WHERE analysis context */
9866 Parse *pParse; /* The parsing context */
9867 WhereClause *pWC; /* The WHERE clause */
9868 struct SrcList_item *pSrc; /* The FROM clause term to search */
9869 Table *pTab;
9870 sqlite3 *db;
9871 sqlite3_index_info *pIdxInfo;
9872 struct sqlite3_index_constraint *pIdxCons;
9873 struct sqlite3_index_constraint_usage *pUsage;
9874 WhereTerm *pTerm;
9875 int i, j;
9876 int iTerm, mxTerm;
9877 int nConstraint;
9878 int seenIn = 0; /* True if an IN operator is seen */
9879 int seenVar = 0; /* True if a non-constant constraint is seen */
9880 int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */
9881 WhereLoop *pNew;
9882 int rc = SQLITE_OK;
9883
9884 assert( (mExtra & mUnusable)==0 );
9885 pWInfo = pBuilder->pWInfo;
9886 pParse = pWInfo->pParse;
9887 db = pParse->db;
9888 pWC = pBuilder->pWC;
9889 pNew = pBuilder->pNew;
9890 pSrc = &pWInfo->pTabList->a[pNew->iTab];
9891 pTab = pSrc->pTab;
9892 assert( IsVirtual(pTab) );
9893 pIdxInfo = allocateIndexInfo(pParse, pWC, mUnusable, pSrc,pBuilder->pOrderBy);
9894 if( pIdxInfo==0 ) return SQLITE_NOMEM;
9895 pNew->prereq = 0;
9896 pNew->rSetup = 0;
9897 pNew->wsFlags = WHERE_VIRTUALTABLE;
9898 pNew->nLTerm = 0;
9899 pNew->u.vtab.needFree = 0;
9900 pUsage = pIdxInfo->aConstraintUsage;
9901 nConstraint = pIdxInfo->nConstraint;
9902 if( whereLoopResize(db, pNew, nConstraint) ){
9903 sqlite3DbFree(db, pIdxInfo);
9904 return SQLITE_NOMEM;
9905 }
9906
9907 for(iPhase=0; iPhase<=3; iPhase++){
9908 if( !seenIn && (iPhase&1)!=0 ){
9909 iPhase++;
9910 if( iPhase>3 ) break;
9911 }
9912 if( !seenVar && iPhase>1 ) break;
9913 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
9914 for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
9915 j = pIdxCons->iTermOffset;
9916 pTerm = &pWC->a[j];
9917 switch( iPhase ){
9918 case 0: /* Constants without IN operator */
9919 pIdxCons->usable = 0;
9920 if( (pTerm->eOperator & WO_IN)!=0 ){
9921 seenIn = 1;
9922 }
9923 if( (pTerm->prereqRight & ~mExtra)!=0 ){
9924 seenVar = 1;
9925 }else if( (pTerm->eOperator & WO_IN)==0 ){
9926 pIdxCons->usable = 1;
9927 }
9928 break;
9929 case 1: /* Constants with IN operators */
9930 assert( seenIn );
9931 pIdxCons->usable = (pTerm->prereqRight & ~mExtra)==0;
9932 break;
9933 case 2: /* Variables without IN */
9934 assert( seenVar );
9935 pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
9936 break;
9937 default: /* Variables with IN */
9938 assert( seenVar && seenIn );
9939 pIdxCons->usable = 1;
9940 break;
9941 }
9942 }
9943 memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
9944 if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
9945 pIdxInfo->idxStr = 0;
9946 pIdxInfo->idxNum = 0;
9947 pIdxInfo->needToFreeIdxStr = 0;
9948 pIdxInfo->orderByConsumed = 0;
9949 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
9950 pIdxInfo->estimatedRows = 25;
9951 pIdxInfo->idxFlags = 0;
9952 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
9953 rc = vtabBestIndex(pParse, pTab, pIdxInfo);
9954 if( rc ) goto whereLoopAddVtab_exit;
9955 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
9956 pNew->prereq = mExtra;
9957 mxTerm = -1;
9958 assert( pNew->nLSlot>=nConstraint );
9959 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
9960 pNew->u.vtab.omitMask = 0;
9961 for(i=0; i<nConstraint; i++, pIdxCons++){
9962 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
9963 j = pIdxCons->iTermOffset;
9964 if( iTerm>=nConstraint
9965 || j<0
9966 || j>=pWC->nTerm
9967 || pNew->aLTerm[iTerm]!=0
9968 ){
9969 rc = SQLITE_ERROR;
9970 sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
9971 goto whereLoopAddVtab_exit;
9972 }
9973 testcase( iTerm==nConstraint-1 );
9974 testcase( j==0 );
9975 testcase( j==pWC->nTerm-1 );
9976 pTerm = &pWC->a[j];
9977 pNew->prereq |= pTerm->prereqRight;
9978 assert( iTerm<pNew->nLSlot );
9979 pNew->aLTerm[iTerm] = pTerm;
9980 if( iTerm>mxTerm ) mxTerm = iTerm;
9981 testcase( iTerm==15 );
9982 testcase( iTerm==16 );
9983 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
9984 if( (pTerm->eOperator & WO_IN)!=0 ){
9985 if( pUsage[i].omit==0 ){
9986 /* Do not attempt to use an IN constraint if the virtual table
9987 ** says that the equivalent EQ constraint cannot be safely omitted.
9988 ** If we do attempt to use such a constraint, some rows might be
9989 ** repeated in the output. */
9990 break;
9991 }
9992 /* A virtual table that is constrained by an IN clause may not
9993 ** consume the ORDER BY clause because (1) the order of IN terms
9994 ** is not necessarily related to the order of output terms and
9995 ** (2) Multiple outputs from a single IN value will not merge
9996 ** together. */
9997 pIdxInfo->orderByConsumed = 0;
9998 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
9999 }
10000 }
10001 }
10002 if( i>=nConstraint ){
10003 pNew->nLTerm = mxTerm+1;
10004 assert( pNew->nLTerm<=pNew->nLSlot );
10005 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
10006 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
10007 pIdxInfo->needToFreeIdxStr = 0;
10008 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
10009 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
10010 pIdxInfo->nOrderBy : 0);
10011 pNew->rSetup = 0;
10012 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
10013 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
10014
10015 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
10016 ** that the scan will visit at most one row. Clear it otherwise. */
10017 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
10018 pNew->wsFlags |= WHERE_ONEROW;
10019 }else{
10020 pNew->wsFlags &= ~WHERE_ONEROW;
10021 }
10022 whereLoopInsert(pBuilder, pNew);
10023 if( pNew->u.vtab.needFree ){
10024 sqlite3_free(pNew->u.vtab.idxStr);
10025 pNew->u.vtab.needFree = 0;
10026 }
10027 }
10028 }
10029
10030 whereLoopAddVtab_exit:
10031 if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
10032 sqlite3DbFree(db, pIdxInfo);
10033 return rc;
10034 }
10035 #endif /* SQLITE_OMIT_VIRTUALTABLE */
10036
10037 /*
10038 ** Add WhereLoop entries to handle OR terms. This works for either
10039 ** btrees or virtual tables.
10040 */
10041 static int whereLoopAddOr(
10042 WhereLoopBuilder *pBuilder,
10043 Bitmask mExtra,
10044 Bitmask mUnusable
10045 ){
10046 WhereInfo *pWInfo = pBuilder->pWInfo;
10047 WhereClause *pWC;
10048 WhereLoop *pNew;
10049 WhereTerm *pTerm, *pWCEnd;
10050 int rc = SQLITE_OK;
10051 int iCur;
10052 WhereClause tempWC;
10053 WhereLoopBuilder sSubBuild;
10054 WhereOrSet sSum, sCur;
10055 struct SrcList_item *pItem;
10056
10057 pWC = pBuilder->pWC;
10058 pWCEnd = pWC->a + pWC->nTerm;
10059 pNew = pBuilder->pNew;
10060 memset(&sSum, 0, sizeof(sSum));
10061 pItem = pWInfo->pTabList->a + pNew->iTab;
10062 iCur = pItem->iCursor;
10063
10064 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
10065 if( (pTerm->eOperator & WO_OR)!=0
10066 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
10067 ){
10068 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
10069 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
10070 WhereTerm *pOrTerm;
10071 int once = 1;
10072 int i, j;
10073
10074 sSubBuild = *pBuilder;
10075 sSubBuild.pOrderBy = 0;
10076 sSubBuild.pOrSet = &sCur;
10077
10078 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
10079 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
10080 if( (pOrTerm->eOperator & WO_AND)!=0 ){
10081 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
10082 }else if( pOrTerm->leftCursor==iCur ){
10083 tempWC.pWInfo = pWC->pWInfo;
10084 tempWC.pOuter = pWC;
10085 tempWC.op = TK_AND;
10086 tempWC.nTerm = 1;
10087 tempWC.a = pOrTerm;
10088 sSubBuild.pWC = &tempWC;
10089 }else{
10090 continue;
10091 }
10092 sCur.n = 0;
10093 #ifdef WHERETRACE_ENABLED
10094 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
10095 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
10096 if( sqlite3WhereTrace & 0x400 ){
10097 for(i=0; i<sSubBuild.pWC->nTerm; i++){
10098 whereTermPrint(&sSubBuild.pWC->a[i], i);
10099 }
10100 }
10101 #endif
10102 #ifndef SQLITE_OMIT_VIRTUALTABLE
10103 if( IsVirtual(pItem->pTab) ){
10104 rc = whereLoopAddVirtual(&sSubBuild, mExtra, mUnusable);
10105 }else
10106 #endif
10107 {
10108 rc = whereLoopAddBtree(&sSubBuild, mExtra);
10109 }
10110 if( rc==SQLITE_OK ){
10111 rc = whereLoopAddOr(&sSubBuild, mExtra, mUnusable);
10112 }
10113 assert( rc==SQLITE_OK || sCur.n==0 );
10114 if( sCur.n==0 ){
10115 sSum.n = 0;
10116 break;
10117 }else if( once ){
10118 whereOrMove(&sSum, &sCur);
10119 once = 0;
10120 }else{
10121 WhereOrSet sPrev;
10122 whereOrMove(&sPrev, &sSum);
10123 sSum.n = 0;
10124 for(i=0; i<sPrev.n; i++){
10125 for(j=0; j<sCur.n; j++){
10126 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
10127 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
10128 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
10129 }
10130 }
10131 }
10132 }
10133 pNew->nLTerm = 1;
10134 pNew->aLTerm[0] = pTerm;
10135 pNew->wsFlags = WHERE_MULTI_OR;
10136 pNew->rSetup = 0;
10137 pNew->iSortIdx = 0;
10138 memset(&pNew->u, 0, sizeof(pNew->u));
10139 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
10140 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
10141 ** of all sub-scans required by the OR-scan. However, due to rounding
10142 ** errors, it may be that the cost of the OR-scan is equal to its
10143 ** most expensive sub-scan. Add the smallest possible penalty
10144 ** (equivalent to multiplying the cost by 1.07) to ensure that
10145 ** this does not happen. Otherwise, for WHERE clauses such as the
10146 ** following where there is an index on "y":
10147 **
10148 ** WHERE likelihood(x=?, 0.99) OR y=?
10149 **
10150 ** the planner may elect to "OR" together a full-table scan and an
10151 ** index lookup. And other similarly odd results. */
10152 pNew->rRun = sSum.a[i].rRun + 1;
10153 pNew->nOut = sSum.a[i].nOut;
10154 pNew->prereq = sSum.a[i].prereq;
10155 rc = whereLoopInsert(pBuilder, pNew);
10156 }
10157 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
10158 }
10159 }
10160 return rc;
10161 }
10162
10163 /*
10164 ** Add all WhereLoop objects for all tables
10165 */
10166 static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
10167 WhereInfo *pWInfo = pBuilder->pWInfo;
10168 Bitmask mExtra = 0;
10169 Bitmask mPrior = 0;
10170 int iTab;
10171 SrcList *pTabList = pWInfo->pTabList;
10172 struct SrcList_item *pItem;
10173 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
10174 sqlite3 *db = pWInfo->pParse->db;
10175 int rc = SQLITE_OK;
10176 WhereLoop *pNew;
10177 u8 priorJointype = 0;
10178
10179 /* Loop over the tables in the join, from left to right */
10180 pNew = pBuilder->pNew;
10181 whereLoopInit(pNew);
10182 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
10183 Bitmask mUnusable = 0;
10184 pNew->iTab = iTab;
10185 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
10186 if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
10187 /* This condition is true when pItem is the FROM clause term on the
10188 ** right-hand-side of a LEFT or CROSS JOIN. */
10189 mExtra = mPrior;
10190 }
10191 priorJointype = pItem->fg.jointype;
10192 if( IsVirtual(pItem->pTab) ){
10193 struct SrcList_item *p;
10194 for(p=&pItem[1]; p<pEnd; p++){
10195 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
10196 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
10197 }
10198 }
10199 rc = whereLoopAddVirtual(pBuilder, mExtra, mUnusable);
10200 }else{
10201 rc = whereLoopAddBtree(pBuilder, mExtra);
10202 }
10203 if( rc==SQLITE_OK ){
10204 rc = whereLoopAddOr(pBuilder, mExtra, mUnusable);
10205 }
10206 mPrior |= pNew->maskSelf;
10207 if( rc || db->mallocFailed ) break;
10208 }
10209
10210 whereLoopClear(db, pNew);
10211 return rc;
10212 }
10213
10214 /*
10215 ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
10216 ** parameters) to see if it outputs rows in the requested ORDER BY
10217 ** (or GROUP BY) without requiring a separate sort operation. Return N:
10218 **
10219 ** N>0: N terms of the ORDER BY clause are satisfied
10220 ** N==0: No terms of the ORDER BY clause are satisfied
10221 ** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
10222 **
10223 ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
10224 ** strict. With GROUP BY and DISTINCT the only requirement is that
10225 ** equivalent rows appear immediately adjacent to one another. GROUP BY
10226 ** and DISTINCT do not require rows to appear in any particular order as long
10227 ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
10228 ** the pOrderBy terms can be matched in any order. With ORDER BY, the
10229 ** pOrderBy terms must be matched in strict left-to-right order.
10230 */
10231 static i8 wherePathSatisfiesOrderBy(
10232 WhereInfo *pWInfo, /* The WHERE clause */
10233 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
10234 WherePath *pPath, /* The WherePath to check */
10235 u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
10236 u16 nLoop, /* Number of entries in pPath->aLoop[] */
10237 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
10238 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
10239 ){
10240 u8 revSet; /* True if rev is known */
10241 u8 rev; /* Composite sort order */
10242 u8 revIdx; /* Index sort order */
10243 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
10244 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
10245 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
10246 u16 nKeyCol; /* Number of key columns in pIndex */
10247 u16 nColumn; /* Total number of ordered columns in the index */
10248 u16 nOrderBy; /* Number terms in the ORDER BY clause */
10249 int iLoop; /* Index of WhereLoop in pPath being processed */
10250 int i, j; /* Loop counters */
10251 int iCur; /* Cursor number for current WhereLoop */
10252 int iColumn; /* A column number within table iCur */
10253 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
10254 WhereTerm *pTerm; /* A single term of the WHERE clause */
10255 Expr *pOBExpr; /* An expression from the ORDER BY clause */
10256 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
10257 Index *pIndex; /* The index associated with pLoop */
10258 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
10259 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
10260 Bitmask obDone; /* Mask of all ORDER BY terms */
10261 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
10262 Bitmask ready; /* Mask of inner loops */
10263
10264 /*
10265 ** We say the WhereLoop is "one-row" if it generates no more than one
10266 ** row of output. A WhereLoop is one-row if all of the following are true:
10267 ** (a) All index columns match with WHERE_COLUMN_EQ.
10268 ** (b) The index is unique
10269 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
10270 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
10271 **
10272 ** We say the WhereLoop is "order-distinct" if the set of columns from
10273 ** that WhereLoop that are in the ORDER BY clause are different for every
10274 ** row of the WhereLoop. Every one-row WhereLoop is automatically
10275 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
10276 ** is not order-distinct. To be order-distinct is not quite the same as being
10277 ** UNIQUE since a UNIQUE column or index can have multiple rows that
10278 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
10279 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
10280 **
10281 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
10282 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
10283 ** automatically order-distinct.
10284 */
10285
10286 assert( pOrderBy!=0 );
10287 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
10288
10289 nOrderBy = pOrderBy->nExpr;
10290 testcase( nOrderBy==BMS-1 );
10291 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
10292 isOrderDistinct = 1;
10293 obDone = MASKBIT(nOrderBy)-1;
10294 orderDistinctMask = 0;
10295 ready = 0;
10296 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
10297 if( iLoop>0 ) ready |= pLoop->maskSelf;
10298 pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
10299 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
10300 if( pLoop->u.vtab.isOrdered ) obSat = obDone;
10301 break;
10302 }
10303 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
10304
10305 /* Mark off any ORDER BY term X that is a column in the table of
10306 ** the current loop for which there is term in the WHERE
10307 ** clause of the form X IS NULL or X=? that reference only outer
10308 ** loops.
10309 */
10310 for(i=0; i<nOrderBy; i++){
10311 if( MASKBIT(i) & obSat ) continue;
10312 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
10313 if( pOBExpr->op!=TK_COLUMN ) continue;
10314 if( pOBExpr->iTable!=iCur ) continue;
10315 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
10316 ~ready, WO_EQ|WO_ISNULL|WO_IS, 0);
10317 if( pTerm==0 ) continue;
10318 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
10319 const char *z1, *z2;
10320 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
10321 if( !pColl ) pColl = db->pDfltColl;
10322 z1 = pColl->zName;
10323 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
10324 if( !pColl ) pColl = db->pDfltColl;
10325 z2 = pColl->zName;
10326 if( sqlite3StrICmp(z1, z2)!=0 ) continue;
10327 testcase( pTerm->pExpr->op==TK_IS );
10328 }
10329 obSat |= MASKBIT(i);
10330 }
10331
10332 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
10333 if( pLoop->wsFlags & WHERE_IPK ){
10334 pIndex = 0;
10335 nKeyCol = 0;
10336 nColumn = 1;
10337 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
10338 return 0;
10339 }else{
10340 nKeyCol = pIndex->nKeyCol;
10341 nColumn = pIndex->nColumn;
10342 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
10343 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
10344 || !HasRowid(pIndex->pTable));
10345 isOrderDistinct = IsUniqueIndex(pIndex);
10346 }
10347
10348 /* Loop through all columns of the index and deal with the ones
10349 ** that are not constrained by == or IN.
10350 */
10351 rev = revSet = 0;
10352 distinctColumns = 0;
10353 for(j=0; j<nColumn; j++){
10354 u8 bOnce; /* True to run the ORDER BY search loop */
10355
10356 /* Skip over == and IS NULL terms */
10357 if( j<pLoop->u.btree.nEq
10358 && pLoop->nSkip==0
10359 && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0
10360 ){
10361 if( i & WO_ISNULL ){
10362 testcase( isOrderDistinct );
10363 isOrderDistinct = 0;
10364 }
10365 continue;
10366 }
10367
10368 /* Get the column number in the table (iColumn) and sort order
10369 ** (revIdx) for the j-th column of the index.
10370 */
10371 if( pIndex ){
10372 iColumn = pIndex->aiColumn[j];
10373 revIdx = pIndex->aSortOrder[j];
10374 if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
10375 }else{
10376 iColumn = XN_ROWID;
10377 revIdx = 0;
10378 }
10379
10380 /* An unconstrained column that might be NULL means that this
10381 ** WhereLoop is not well-ordered
10382 */
10383 if( isOrderDistinct
10384 && iColumn>=0
10385 && j>=pLoop->u.btree.nEq
10386 && pIndex->pTable->aCol[iColumn].notNull==0
10387 ){
10388 isOrderDistinct = 0;
10389 }
10390
10391 /* Find the ORDER BY term that corresponds to the j-th column
10392 ** of the index and mark that ORDER BY term off
10393 */
10394 bOnce = 1;
10395 isMatch = 0;
10396 for(i=0; bOnce && i<nOrderBy; i++){
10397 if( MASKBIT(i) & obSat ) continue;
10398 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
10399 testcase( wctrlFlags & WHERE_GROUPBY );
10400 testcase( wctrlFlags & WHERE_DISTINCTBY );
10401 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
10402 if( iColumn>=(-1) ){
10403 if( pOBExpr->op!=TK_COLUMN ) continue;
10404 if( pOBExpr->iTable!=iCur ) continue;
10405 if( pOBExpr->iColumn!=iColumn ) continue;
10406 }else{
10407 if( sqlite3ExprCompare(pOBExpr,pIndex->aColExpr->a[j].pExpr,iCur) ){
10408 continue;
10409 }
10410 }
10411 if( iColumn>=0 ){
10412 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
10413 if( !pColl ) pColl = db->pDfltColl;
10414 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
10415 }
10416 isMatch = 1;
10417 break;
10418 }
10419 if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
10420 /* Make sure the sort order is compatible in an ORDER BY clause.
10421 ** Sort order is irrelevant for a GROUP BY clause. */
10422 if( revSet ){
10423 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0;
10424 }else{
10425 rev = revIdx ^ pOrderBy->a[i].sortOrder;
10426 if( rev ) *pRevMask |= MASKBIT(iLoop);
10427 revSet = 1;
10428 }
10429 }
10430 if( isMatch ){
10431 if( iColumn<0 ){
10432 testcase( distinctColumns==0 );
10433 distinctColumns = 1;
10434 }
10435 obSat |= MASKBIT(i);
10436 }else{
10437 /* No match found */
10438 if( j==0 || j<nKeyCol ){
10439 testcase( isOrderDistinct!=0 );
10440 isOrderDistinct = 0;
10441 }
10442 break;
10443 }
10444 } /* end Loop over all index columns */
10445 if( distinctColumns ){
10446 testcase( isOrderDistinct==0 );
10447 isOrderDistinct = 1;
10448 }
10449 } /* end-if not one-row */
10450
10451 /* Mark off any other ORDER BY terms that reference pLoop */
10452 if( isOrderDistinct ){
10453 orderDistinctMask |= pLoop->maskSelf;
10454 for(i=0; i<nOrderBy; i++){
10455 Expr *p;
10456 Bitmask mTerm;
10457 if( MASKBIT(i) & obSat ) continue;
10458 p = pOrderBy->a[i].pExpr;
10459 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
10460 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
10461 if( (mTerm&~orderDistinctMask)==0 ){
10462 obSat |= MASKBIT(i);
10463 }
10464 }
10465 }
10466 } /* End the loop over all WhereLoops from outer-most down to inner-most */
10467 if( obSat==obDone ) return (i8)nOrderBy;
10468 if( !isOrderDistinct ){
10469 for(i=nOrderBy-1; i>0; i--){
10470 Bitmask m = MASKBIT(i) - 1;
10471 if( (obSat&m)==m ) return i;
10472 }
10473 return 0;
10474 }
10475 return -1;
10476 }
10477
10478
10479 /*
10480 ** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
10481 ** the planner assumes that the specified pOrderBy list is actually a GROUP
10482 ** BY clause - and so any order that groups rows as required satisfies the
10483 ** request.
10484 **
10485 ** Normally, in this case it is not possible for the caller to determine
10486 ** whether or not the rows are really being delivered in sorted order, or
10487 ** just in some other order that provides the required grouping. However,
10488 ** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
10489 ** this function may be called on the returned WhereInfo object. It returns
10490 ** true if the rows really will be sorted in the specified order, or false
10491 ** otherwise.
10492 **
10493 ** For example, assuming:
10494 **
10495 ** CREATE INDEX i1 ON t1(x, Y);
10496 **
10497 ** then
10498 **
10499 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
10500 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
10501 */
10502 SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
10503 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
10504 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
10505 return pWInfo->sorted;
10506 }
10507
10508 #ifdef WHERETRACE_ENABLED
10509 /* For debugging use only: */
10510 static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
10511 static char zName[65];
10512 int i;
10513 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
10514 if( pLast ) zName[i++] = pLast->cId;
10515 zName[i] = 0;
10516 return zName;
10517 }
10518 #endif
10519
10520 /*
10521 ** Return the cost of sorting nRow rows, assuming that the keys have
10522 ** nOrderby columns and that the first nSorted columns are already in
10523 ** order.
10524 */
10525 static LogEst whereSortingCost(
10526 WhereInfo *pWInfo,
10527 LogEst nRow,
10528 int nOrderBy,
10529 int nSorted
10530 ){
10531 /* TUNING: Estimated cost of a full external sort, where N is
10532 ** the number of rows to sort is:
10533 **
10534 ** cost = (3.0 * N * log(N)).
10535 **
10536 ** Or, if the order-by clause has X terms but only the last Y
10537 ** terms are out of order, then block-sorting will reduce the
10538 ** sorting cost to:
10539 **
10540 ** cost = (3.0 * N * log(N)) * (Y/X)
10541 **
10542 ** The (Y/X) term is implemented using stack variable rScale
10543 ** below. */
10544 LogEst rScale, rSortCost;
10545 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
10546 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
10547 rSortCost = nRow + estLog(nRow) + rScale + 16;
10548
10549 /* TUNING: The cost of implementing DISTINCT using a B-TREE is
10550 ** similar but with a larger constant of proportionality.
10551 ** Multiply by an additional factor of 3.0. */
10552 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
10553 rSortCost += 16;
10554 }
10555
10556 return rSortCost;
10557 }
10558
10559 /*
10560 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
10561 ** attempts to find the lowest cost path that visits each WhereLoop
10562 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
10563 **
10564 ** Assume that the total number of output rows that will need to be sorted
10565 ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
10566 ** costs if nRowEst==0.
10567 **
10568 ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
10569 ** error occurs.
10570 */
10571 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
10572 int mxChoice; /* Maximum number of simultaneous paths tracked */
10573 int nLoop; /* Number of terms in the join */
10574 Parse *pParse; /* Parsing context */
10575 sqlite3 *db; /* The database connection */
10576 int iLoop; /* Loop counter over the terms of the join */
10577 int ii, jj; /* Loop counters */
10578 int mxI = 0; /* Index of next entry to replace */
10579 int nOrderBy; /* Number of ORDER BY clause terms */
10580 LogEst mxCost = 0; /* Maximum cost of a set of paths */
10581 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
10582 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
10583 WherePath *aFrom; /* All nFrom paths at the previous level */
10584 WherePath *aTo; /* The nTo best paths at the current level */
10585 WherePath *pFrom; /* An element of aFrom[] that we are working on */
10586 WherePath *pTo; /* An element of aTo[] that we are working on */
10587 WhereLoop *pWLoop; /* One of the WhereLoop objects */
10588 WhereLoop **pX; /* Used to divy up the pSpace memory */
10589 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
10590 char *pSpace; /* Temporary memory used by this routine */
10591 int nSpace; /* Bytes of space allocated at pSpace */
10592
10593 pParse = pWInfo->pParse;
10594 db = pParse->db;
10595 nLoop = pWInfo->nLevel;
10596 /* TUNING: For simple queries, only the best path is tracked.
10597 ** For 2-way joins, the 5 best paths are followed.
10598 ** For joins of 3 or more tables, track the 10 best paths */
10599 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
10600 assert( nLoop<=pWInfo->pTabList->nSrc );
10601 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
10602
10603 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
10604 ** case the purpose of this call is to estimate the number of rows returned
10605 ** by the overall query. Once this estimate has been obtained, the caller
10606 ** will invoke this function a second time, passing the estimate as the
10607 ** nRowEst parameter. */
10608 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
10609 nOrderBy = 0;
10610 }else{
10611 nOrderBy = pWInfo->pOrderBy->nExpr;
10612 }
10613
10614 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
10615 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
10616 nSpace += sizeof(LogEst) * nOrderBy;
10617 pSpace = sqlite3DbMallocRaw(db, nSpace);
10618 if( pSpace==0 ) return SQLITE_NOMEM;
10619 aTo = (WherePath*)pSpace;
10620 aFrom = aTo+mxChoice;
10621 memset(aFrom, 0, sizeof(aFrom[0]));
10622 pX = (WhereLoop**)(aFrom+mxChoice);
10623 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
10624 pFrom->aLoop = pX;
10625 }
10626 if( nOrderBy ){
10627 /* If there is an ORDER BY clause and it is not being ignored, set up
10628 ** space for the aSortCost[] array. Each element of the aSortCost array
10629 ** is either zero - meaning it has not yet been initialized - or the
10630 ** cost of sorting nRowEst rows of data where the first X terms of
10631 ** the ORDER BY clause are already in order, where X is the array
10632 ** index. */
10633 aSortCost = (LogEst*)pX;
10634 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
10635 }
10636 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
10637 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
10638
10639 /* Seed the search with a single WherePath containing zero WhereLoops.
10640 **
10641 ** TUNING: Do not let the number of iterations go above 28. If the cost
10642 ** of computing an automatic index is not paid back within the first 28
10643 ** rows, then do not use the automatic index. */
10644 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
10645 nFrom = 1;
10646 assert( aFrom[0].isOrdered==0 );
10647 if( nOrderBy ){
10648 /* If nLoop is zero, then there are no FROM terms in the query. Since
10649 ** in this case the query may return a maximum of one row, the results
10650 ** are already in the requested order. Set isOrdered to nOrderBy to
10651 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
10652 ** -1, indicating that the result set may or may not be ordered,
10653 ** depending on the loops added to the current plan. */
10654 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
10655 }
10656
10657 /* Compute successively longer WherePaths using the previous generation
10658 ** of WherePaths as the basis for the next. Keep track of the mxChoice
10659 ** best paths at each generation */
10660 for(iLoop=0; iLoop<nLoop; iLoop++){
10661 nTo = 0;
10662 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
10663 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
10664 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
10665 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
10666 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
10667 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
10668 Bitmask maskNew; /* Mask of src visited by (..) */
10669 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
10670
10671 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
10672 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
10673 /* At this point, pWLoop is a candidate to be the next loop.
10674 ** Compute its cost */
10675 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
10676 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
10677 nOut = pFrom->nRow + pWLoop->nOut;
10678 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
10679 if( isOrdered<0 ){
10680 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
10681 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
10682 iLoop, pWLoop, &revMask);
10683 }else{
10684 revMask = pFrom->revLoop;
10685 }
10686 if( isOrdered>=0 && isOrdered<nOrderBy ){
10687 if( aSortCost[isOrdered]==0 ){
10688 aSortCost[isOrdered] = whereSortingCost(
10689 pWInfo, nRowEst, nOrderBy, isOrdered
10690 );
10691 }
10692 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]);
10693
10694 WHERETRACE(0x002,
10695 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
10696 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
10697 rUnsorted, rCost));
10698 }else{
10699 rCost = rUnsorted;
10700 }
10701
10702 /* Check to see if pWLoop should be added to the set of
10703 ** mxChoice best-so-far paths.
10704 **
10705 ** First look for an existing path among best-so-far paths
10706 ** that covers the same set of loops and has the same isOrdered
10707 ** setting as the current path candidate.
10708 **
10709 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
10710 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
10711 ** of legal values for isOrdered, -1..64.
10712 */
10713 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
10714 if( pTo->maskLoop==maskNew
10715 && ((pTo->isOrdered^isOrdered)&0x80)==0
10716 ){
10717 testcase( jj==nTo-1 );
10718 break;
10719 }
10720 }
10721 if( jj>=nTo ){
10722 /* None of the existing best-so-far paths match the candidate. */
10723 if( nTo>=mxChoice
10724 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
10725 ){
10726 /* The current candidate is no better than any of the mxChoice
10727 ** paths currently in the best-so-far buffer. So discard
10728 ** this candidate as not viable. */
10729 #ifdef WHERETRACE_ENABLED /* 0x4 */
10730 if( sqlite3WhereTrace&0x4 ){
10731 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n",
10732 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
10733 isOrdered>=0 ? isOrdered+'0' : '?');
10734 }
10735 #endif
10736 continue;
10737 }
10738 /* If we reach this points it means that the new candidate path
10739 ** needs to be added to the set of best-so-far paths. */
10740 if( nTo<mxChoice ){
10741 /* Increase the size of the aTo set by one */
10742 jj = nTo++;
10743 }else{
10744 /* New path replaces the prior worst to keep count below mxChoice */
10745 jj = mxI;
10746 }
10747 pTo = &aTo[jj];
10748 #ifdef WHERETRACE_ENABLED /* 0x4 */
10749 if( sqlite3WhereTrace&0x4 ){
10750 sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n",
10751 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
10752 isOrdered>=0 ? isOrdered+'0' : '?');
10753 }
10754 #endif
10755 }else{
10756 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
10757 ** same set of loops and has the sam isOrdered setting as the
10758 ** candidate path. Check to see if the candidate should replace
10759 ** pTo or if the candidate should be skipped */
10760 if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){
10761 #ifdef WHERETRACE_ENABLED /* 0x4 */
10762 if( sqlite3WhereTrace&0x4 ){
10763 sqlite3DebugPrintf(
10764 "Skip %s cost=%-3d,%3d order=%c",
10765 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
10766 isOrdered>=0 ? isOrdered+'0' : '?');
10767 sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n",
10768 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
10769 pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
10770 }
10771 #endif
10772 /* Discard the candidate path from further consideration */
10773 testcase( pTo->rCost==rCost );
10774 continue;
10775 }
10776 testcase( pTo->rCost==rCost+1 );
10777 /* Control reaches here if the candidate path is better than the
10778 ** pTo path. Replace pTo with the candidate. */
10779 #ifdef WHERETRACE_ENABLED /* 0x4 */
10780 if( sqlite3WhereTrace&0x4 ){
10781 sqlite3DebugPrintf(
10782 "Update %s cost=%-3d,%3d order=%c",
10783 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
10784 isOrdered>=0 ? isOrdered+'0' : '?');
10785 sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n",
10786 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
10787 pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
10788 }
10789 #endif
10790 }
10791 /* pWLoop is a winner. Add it to the set of best so far */
10792 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
10793 pTo->revLoop = revMask;
10794 pTo->nRow = nOut;
10795 pTo->rCost = rCost;
10796 pTo->rUnsorted = rUnsorted;
10797 pTo->isOrdered = isOrdered;
10798 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
10799 pTo->aLoop[iLoop] = pWLoop;
10800 if( nTo>=mxChoice ){
10801 mxI = 0;
10802 mxCost = aTo[0].rCost;
10803 mxUnsorted = aTo[0].nRow;
10804 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
10805 if( pTo->rCost>mxCost
10806 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
10807 ){
10808 mxCost = pTo->rCost;
10809 mxUnsorted = pTo->rUnsorted;
10810 mxI = jj;
10811 }
10812 }
10813 }
10814 }
10815 }
10816
10817 #ifdef WHERETRACE_ENABLED /* >=2 */
10818 if( sqlite3WhereTrace & 0x02 ){
10819 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
10820 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
10821 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
10822 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
10823 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
10824 if( pTo->isOrdered>0 ){
10825 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
10826 }else{
10827 sqlite3DebugPrintf("\n");
10828 }
10829 }
10830 }
10831 #endif
10832
10833 /* Swap the roles of aFrom and aTo for the next generation */
10834 pFrom = aTo;
10835 aTo = aFrom;
10836 aFrom = pFrom;
10837 nFrom = nTo;
10838 }
10839
10840 if( nFrom==0 ){
10841 sqlite3ErrorMsg(pParse, "no query solution");
10842 sqlite3DbFree(db, pSpace);
10843 return SQLITE_ERROR;
10844 }
10845
10846 /* Find the lowest cost path. pFrom will be left pointing to that path */
10847 pFrom = aFrom;
10848 for(ii=1; ii<nFrom; ii++){
10849 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
10850 }
10851 assert( pWInfo->nLevel==nLoop );
10852 /* Load the lowest cost path into pWInfo */
10853 for(iLoop=0; iLoop<nLoop; iLoop++){
10854 WhereLevel *pLevel = pWInfo->a + iLoop;
10855 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
10856 pLevel->iFrom = pWLoop->iTab;
10857 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
10858 }
10859 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
10860 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
10861 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
10862 && nRowEst
10863 ){
10864 Bitmask notUsed;
10865 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
10866 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
10867 if( rc==pWInfo->pResultSet->nExpr ){
10868 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
10869 }
10870 }
10871 if( pWInfo->pOrderBy ){
10872 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
10873 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
10874 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
10875 }
10876 }else{
10877 pWInfo->nOBSat = pFrom->isOrdered;
10878 if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
10879 pWInfo->revMask = pFrom->revLoop;
10880 }
10881 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
10882 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
10883 ){
10884 Bitmask revMask = 0;
10885 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
10886 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
10887 );
10888 assert( pWInfo->sorted==0 );
10889 if( nOrder==pWInfo->pOrderBy->nExpr ){
10890 pWInfo->sorted = 1;
10891 pWInfo->revMask = revMask;
10892 }
10893 }
10894 }
10895
10896
10897 pWInfo->nRowOut = pFrom->nRow;
10898
10899 /* Free temporary memory and return success */
10900 sqlite3DbFree(db, pSpace);
10901 return SQLITE_OK;
10902 }
10903
10904 /*
10905 ** Most queries use only a single table (they are not joins) and have
10906 ** simple == constraints against indexed fields. This routine attempts
10907 ** to plan those simple cases using much less ceremony than the
10908 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
10909 ** times for the common case.
10910 **
10911 ** Return non-zero on success, if this query can be handled by this
10912 ** no-frills query planner. Return zero if this query needs the
10913 ** general-purpose query planner.
10914 */
10915 static int whereShortCut(WhereLoopBuilder *pBuilder){
10916 WhereInfo *pWInfo;
10917 struct SrcList_item *pItem;
10918 WhereClause *pWC;
10919 WhereTerm *pTerm;
10920 WhereLoop *pLoop;
10921 int iCur;
10922 int j;
10923 Table *pTab;
10924 Index *pIdx;
10925
10926 pWInfo = pBuilder->pWInfo;
10927 if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
10928 assert( pWInfo->pTabList->nSrc>=1 );
10929 pItem = pWInfo->pTabList->a;
10930 pTab = pItem->pTab;
10931 if( IsVirtual(pTab) ) return 0;
10932 if( pItem->fg.isIndexedBy ) return 0;
10933 iCur = pItem->iCursor;
10934 pWC = &pWInfo->sWC;
10935 pLoop = pBuilder->pNew;
10936 pLoop->wsFlags = 0;
10937 pLoop->nSkip = 0;
10938 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
10939 if( pTerm ){
10940 testcase( pTerm->eOperator & WO_IS );
10941 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
10942 pLoop->aLTerm[0] = pTerm;
10943 pLoop->nLTerm = 1;
10944 pLoop->u.btree.nEq = 1;
10945 /* TUNING: Cost of a rowid lookup is 10 */
10946 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
10947 }else{
10948 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
10949 int opMask;
10950 assert( pLoop->aLTermSpace==pLoop->aLTerm );
10951 if( !IsUniqueIndex(pIdx)
10952 || pIdx->pPartIdxWhere!=0
10953 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
10954 ) continue;
10955 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
10956 for(j=0; j<pIdx->nKeyCol; j++){
10957 pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx);
10958 if( pTerm==0 ) break;
10959 testcase( pTerm->eOperator & WO_IS );
10960 pLoop->aLTerm[j] = pTerm;
10961 }
10962 if( j!=pIdx->nKeyCol ) continue;
10963 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
10964 if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
10965 pLoop->wsFlags |= WHERE_IDX_ONLY;
10966 }
10967 pLoop->nLTerm = j;
10968 pLoop->u.btree.nEq = j;
10969 pLoop->u.btree.pIndex = pIdx;
10970 /* TUNING: Cost of a unique index lookup is 15 */
10971 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
10972 break;
10973 }
10974 }
10975 if( pLoop->wsFlags ){
10976 pLoop->nOut = (LogEst)1;
10977 pWInfo->a[0].pWLoop = pLoop;
10978 pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
10979 pWInfo->a[0].iTabCur = iCur;
10980 pWInfo->nRowOut = 1;
10981 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
10982 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
10983 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
10984 }
10985 #ifdef SQLITE_DEBUG
10986 pLoop->cId = '0';
10987 #endif
10988 return 1;
10989 }
10990 return 0;
10991 }
10992
10993 /*
10994 ** Generate the beginning of the loop used for WHERE clause processing.
10995 ** The return value is a pointer to an opaque structure that contains
10996 ** information needed to terminate the loop. Later, the calling routine
10997 ** should invoke sqlite3WhereEnd() with the return value of this function
10998 ** in order to complete the WHERE clause processing.
10999 **
11000 ** If an error occurs, this routine returns NULL.
11001 **
11002 ** The basic idea is to do a nested loop, one loop for each table in
11003 ** the FROM clause of a select. (INSERT and UPDATE statements are the
11004 ** same as a SELECT with only a single table in the FROM clause.) For
11005 ** example, if the SQL is this:
11006 **
11007 ** SELECT * FROM t1, t2, t3 WHERE ...;
11008 **
11009 ** Then the code generated is conceptually like the following:
11010 **
11011 ** foreach row1 in t1 do \ Code generated
11012 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
11013 ** foreach row3 in t3 do /
11014 ** ...
11015 ** end \ Code generated
11016 ** end |-- by sqlite3WhereEnd()
11017 ** end /
11018 **
11019 ** Note that the loops might not be nested in the order in which they
11020 ** appear in the FROM clause if a different order is better able to make
11021 ** use of indices. Note also that when the IN operator appears in
11022 ** the WHERE clause, it might result in additional nested loops for
11023 ** scanning through all values on the right-hand side of the IN.
11024 **
11025 ** There are Btree cursors associated with each table. t1 uses cursor
11026 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
11027 ** And so forth. This routine generates code to open those VDBE cursors
11028 ** and sqlite3WhereEnd() generates the code to close them.
11029 **
11030 ** The code that sqlite3WhereBegin() generates leaves the cursors named
11031 ** in pTabList pointing at their appropriate entries. The [...] code
11032 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
11033 ** data from the various tables of the loop.
11034 **
11035 ** If the WHERE clause is empty, the foreach loops must each scan their
11036 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
11037 ** the tables have indices and there are terms in the WHERE clause that
11038 ** refer to those indices, a complete table scan can be avoided and the
11039 ** code will run much faster. Most of the work of this routine is checking
11040 ** to see if there are indices that can be used to speed up the loop.
11041 **
11042 ** Terms of the WHERE clause are also used to limit which rows actually
11043 ** make it to the "..." in the middle of the loop. After each "foreach",
11044 ** terms of the WHERE clause that use only terms in that loop and outer
11045 ** loops are evaluated and if false a jump is made around all subsequent
11046 ** inner loops (or around the "..." if the test occurs within the inner-
11047 ** most loop)
11048 **
11049 ** OUTER JOINS
11050 **
11051 ** An outer join of tables t1 and t2 is conceptally coded as follows:
11052 **
11053 ** foreach row1 in t1 do
11054 ** flag = 0
11055 ** foreach row2 in t2 do
11056 ** start:
11057 ** ...
11058 ** flag = 1
11059 ** end
11060 ** if flag==0 then
11061 ** move the row2 cursor to a null row
11062 ** goto start
11063 ** fi
11064 ** end
11065 **
11066 ** ORDER BY CLAUSE PROCESSING
11067 **
11068 ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
11069 ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
11070 ** if there is one. If there is no ORDER BY clause or if this routine
11071 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
11072 **
11073 ** The iIdxCur parameter is the cursor number of an index. If
11074 ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index
11075 ** to use for OR clause processing. The WHERE clause should use this
11076 ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
11077 ** the first cursor in an array of cursors for all indices. iIdxCur should
11078 ** be used to compute the appropriate cursor depending on which index is
11079 ** used.
11080 */
11081 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
11082 Parse *pParse, /* The parser context */
11083 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
11084 Expr *pWhere, /* The WHERE clause */
11085 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
11086 ExprList *pResultSet, /* Result set of the query */
11087 u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
11088 int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */
11089 ){
11090 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
11091 int nTabList; /* Number of elements in pTabList */
11092 WhereInfo *pWInfo; /* Will become the return value of this function */
11093 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
11094 Bitmask notReady; /* Cursors that are not yet positioned */
11095 WhereLoopBuilder sWLB; /* The WhereLoop builder */
11096 WhereMaskSet *pMaskSet; /* The expression mask set */
11097 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
11098 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
11099 int ii; /* Loop counter */
11100 sqlite3 *db; /* Database connection */
11101 int rc; /* Return code */
11102 u8 bFordelete = 0;
11103
11104 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
11105 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
11106 && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
11107 ));
11108
11109 /* Variable initialization */
11110 db = pParse->db;
11111 memset(&sWLB, 0, sizeof(sWLB));
11112
11113 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
11114 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
11115 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
11116 sWLB.pOrderBy = pOrderBy;
11117
11118 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
11119 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
11120 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
11121 wctrlFlags &= ~WHERE_WANT_DISTINCT;
11122 }
11123
11124 /* The number of tables in the FROM clause is limited by the number of
11125 ** bits in a Bitmask
11126 */
11127 testcase( pTabList->nSrc==BMS );
11128 if( pTabList->nSrc>BMS ){
11129 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
11130 return 0;
11131 }
11132
11133 /* This function normally generates a nested loop for all tables in
11134 ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should
11135 ** only generate code for the first table in pTabList and assume that
11136 ** any cursors associated with subsequent tables are uninitialized.
11137 */
11138 nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
11139
11140 /* Allocate and initialize the WhereInfo structure that will become the
11141 ** return value. A single allocation is used to store the WhereInfo
11142 ** struct, the contents of WhereInfo.a[], the WhereClause structure
11143 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
11144 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
11145 ** some architectures. Hence the ROUND8() below.
11146 */
11147 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
11148 pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop));
11149 if( db->mallocFailed ){
11150 sqlite3DbFree(db, pWInfo);
11151 pWInfo = 0;
11152 goto whereBeginError;
11153 }
11154 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
11155 pWInfo->nLevel = nTabList;
11156 pWInfo->pParse = pParse;
11157 pWInfo->pTabList = pTabList;
11158 pWInfo->pOrderBy = pOrderBy;
11159 pWInfo->pResultSet = pResultSet;
11160 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
11161 pWInfo->wctrlFlags = wctrlFlags;
11162 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
11163 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
11164 pMaskSet = &pWInfo->sMaskSet;
11165 sWLB.pWInfo = pWInfo;
11166 sWLB.pWC = &pWInfo->sWC;
11167 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
11168 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
11169 whereLoopInit(sWLB.pNew);
11170 #ifdef SQLITE_DEBUG
11171 sWLB.pNew->cId = '*';
11172 #endif
11173
11174 /* Split the WHERE clause into separate subexpressions where each
11175 ** subexpression is separated by an AND operator.
11176 */
11177 initMaskSet(pMaskSet);
11178 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
11179 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
11180
11181 /* Special case: a WHERE clause that is constant. Evaluate the
11182 ** expression and either jump over all of the code or fall thru.
11183 */
11184 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
11185 if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
11186 sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
11187 SQLITE_JUMPIFNULL);
11188 sWLB.pWC->a[ii].wtFlags |= TERM_CODED;
11189 }
11190 }
11191
11192 /* Special case: No FROM clause
11193 */
11194 if( nTabList==0 ){
11195 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
11196 if( wctrlFlags & WHERE_WANT_DISTINCT ){
11197 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
11198 }
11199 }
11200
11201 /* Assign a bit from the bitmask to every term in the FROM clause.
11202 **
11203 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
11204 **
11205 ** The rule of the previous sentence ensures thta if X is the bitmask for
11206 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
11207 ** Knowing the bitmask for all tables to the left of a left join is
11208 ** important. Ticket #3015.
11209 **
11210 ** Note that bitmasks are created for all pTabList->nSrc tables in
11211 ** pTabList, not just the first nTabList tables. nTabList is normally
11212 ** equal to pTabList->nSrc but might be shortened to 1 if the
11213 ** WHERE_ONETABLE_ONLY flag is set.
11214 */
11215 for(ii=0; ii<pTabList->nSrc; ii++){
11216 createMask(pMaskSet, pTabList->a[ii].iCursor);
11217 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
11218 }
11219 #ifdef SQLITE_DEBUG
11220 for(ii=0; ii<pTabList->nSrc; ii++){
11221 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
11222 assert( m==MASKBIT(ii) );
11223 }
11224 #endif
11225
11226 /* Analyze all of the subexpressions. */
11227 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
11228 if( db->mallocFailed ) goto whereBeginError;
11229
11230 if( wctrlFlags & WHERE_WANT_DISTINCT ){
11231 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
11232 /* The DISTINCT marking is pointless. Ignore it. */
11233 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
11234 }else if( pOrderBy==0 ){
11235 /* Try to ORDER BY the result set to make distinct processing easier */
11236 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
11237 pWInfo->pOrderBy = pResultSet;
11238 }
11239 }
11240
11241 /* Construct the WhereLoop objects */
11242 WHERETRACE(0xffff,("*** Optimizer Start *** (wctrlFlags: 0x%x)\n",
11243 wctrlFlags));
11244 #if defined(WHERETRACE_ENABLED)
11245 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
11246 int i;
11247 for(i=0; i<sWLB.pWC->nTerm; i++){
11248 whereTermPrint(&sWLB.pWC->a[i], i);
11249 }
11250 }
11251 #endif
11252
11253 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
11254 rc = whereLoopAddAll(&sWLB);
11255 if( rc ) goto whereBeginError;
11256
11257 #ifdef WHERETRACE_ENABLED
11258 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
11259 WhereLoop *p;
11260 int i;
11261 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
11262 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
11263 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
11264 p->cId = zLabel[i%sizeof(zLabel)];
11265 whereLoopPrint(p, sWLB.pWC);
11266 }
11267 }
11268 #endif
11269
11270 wherePathSolver(pWInfo, 0);
11271 if( db->mallocFailed ) goto whereBeginError;
11272 if( pWInfo->pOrderBy ){
11273 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
11274 if( db->mallocFailed ) goto whereBeginError;
11275 }
11276 }
11277 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
11278 pWInfo->revMask = (Bitmask)(-1);
11279 }
11280 if( pParse->nErr || NEVER(db->mallocFailed) ){
11281 goto whereBeginError;
11282 }
11283 #ifdef WHERETRACE_ENABLED
11284 if( sqlite3WhereTrace ){
11285 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
11286 if( pWInfo->nOBSat>0 ){
11287 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
11288 }
11289 switch( pWInfo->eDistinct ){
11290 case WHERE_DISTINCT_UNIQUE: {
11291 sqlite3DebugPrintf(" DISTINCT=unique");
11292 break;
11293 }
11294 case WHERE_DISTINCT_ORDERED: {
11295 sqlite3DebugPrintf(" DISTINCT=ordered");
11296 break;
11297 }
11298 case WHERE_DISTINCT_UNORDERED: {
11299 sqlite3DebugPrintf(" DISTINCT=unordered");
11300 break;
11301 }
11302 }
11303 sqlite3DebugPrintf("\n");
11304 for(ii=0; ii<pWInfo->nLevel; ii++){
11305 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
11306 }
11307 }
11308 #endif
11309 /* Attempt to omit tables from the join that do not effect the result */
11310 if( pWInfo->nLevel>=2
11311 && pResultSet!=0
11312 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
11313 ){
11314 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
11315 if( sWLB.pOrderBy ){
11316 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
11317 }
11318 while( pWInfo->nLevel>=2 ){
11319 WhereTerm *pTerm, *pEnd;
11320 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
11321 if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break;
11322 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
11323 && (pLoop->wsFlags & WHERE_ONEROW)==0
11324 ){
11325 break;
11326 }
11327 if( (tabUsed & pLoop->maskSelf)!=0 ) break;
11328 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
11329 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
11330 if( (pTerm->prereqAll & pLoop->maskSelf)!=0
11331 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
11332 ){
11333 break;
11334 }
11335 }
11336 if( pTerm<pEnd ) break;
11337 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
11338 pWInfo->nLevel--;
11339 nTabList--;
11340 }
11341 }
11342 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
11343 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
11344
11345 /* If the caller is an UPDATE or DELETE statement that is requesting
11346 ** to use a one-pass algorithm, determine if this is appropriate.
11347 ** The one-pass algorithm only works if the WHERE clause constrains
11348 ** the statement to update or delete a single row.
11349 */
11350 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
11351 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
11352 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
11353 int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
11354 if( bOnerow || ( (wctrlFlags & WHERE_ONEPASS_MULTIROW)
11355 && 0==(wsFlags & WHERE_VIRTUALTABLE)
11356 )){
11357 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
11358 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
11359 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
11360 bFordelete = OPFLAG_FORDELETE;
11361 }
11362 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
11363 }
11364 }
11365 }
11366
11367 /* Open all tables in the pTabList and any indices selected for
11368 ** searching those tables.
11369 */
11370 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
11371 Table *pTab; /* Table to open */
11372 int iDb; /* Index of database containing table/index */
11373 struct SrcList_item *pTabItem;
11374
11375 pTabItem = &pTabList->a[pLevel->iFrom];
11376 pTab = pTabItem->pTab;
11377 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
11378 pLoop = pLevel->pWLoop;
11379 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
11380 /* Do nothing */
11381 }else
11382 #ifndef SQLITE_OMIT_VIRTUALTABLE
11383 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
11384 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
11385 int iCur = pTabItem->iCursor;
11386 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
11387 }else if( IsVirtual(pTab) ){
11388 /* noop */
11389 }else
11390 #endif
11391 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
11392 && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
11393 int op = OP_OpenRead;
11394 if( pWInfo->eOnePass!=ONEPASS_OFF ){
11395 op = OP_OpenWrite;
11396 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
11397 };
11398 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
11399 assert( pTabItem->iCursor==pLevel->iTabCur );
11400 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
11401 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
11402 if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){
11403 Bitmask b = pTabItem->colUsed;
11404 int n = 0;
11405 for(; b; b=b>>1, n++){}
11406 sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
11407 SQLITE_INT_TO_PTR(n), P4_INT32);
11408 assert( n<=pTab->nCol );
11409 }
11410 #ifdef SQLITE_ENABLE_CURSOR_HINTS
11411 if( pLoop->u.btree.pIndex!=0 ){
11412 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete);
11413 }else
11414 #endif
11415 {
11416 sqlite3VdbeChangeP5(v, bFordelete);
11417 }
11418 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
11419 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
11420 (const u8*)&pTabItem->colUsed, P4_INT64);
11421 #endif
11422 }else{
11423 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
11424 }
11425 if( pLoop->wsFlags & WHERE_INDEXED ){
11426 Index *pIx = pLoop->u.btree.pIndex;
11427 int iIndexCur;
11428 int op = OP_OpenRead;
11429 /* iIdxCur is always set if to a positive value if ONEPASS is possible */
11430 assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
11431 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
11432 && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0
11433 ){
11434 /* This is one term of an OR-optimization using the PRIMARY KEY of a
11435 ** WITHOUT ROWID table. No need for a separate index */
11436 iIndexCur = pLevel->iTabCur;
11437 op = 0;
11438 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
11439 Index *pJ = pTabItem->pTab->pIndex;
11440 iIndexCur = iIdxCur;
11441 assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
11442 while( ALWAYS(pJ) && pJ!=pIx ){
11443 iIndexCur++;
11444 pJ = pJ->pNext;
11445 }
11446 op = OP_OpenWrite;
11447 pWInfo->aiCurOnePass[1] = iIndexCur;
11448 }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
11449 iIndexCur = iIdxCur;
11450 if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx;
11451 }else{
11452 iIndexCur = pParse->nTab++;
11453 }
11454 pLevel->iIdxCur = iIndexCur;
11455 assert( pIx->pSchema==pTab->pSchema );
11456 assert( iIndexCur>=0 );
11457 if( op ){
11458 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
11459 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
11460 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
11461 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
11462 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
11463 ){
11464 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
11465 }
11466 VdbeComment((v, "%s", pIx->zName));
11467 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
11468 {
11469 u64 colUsed = 0;
11470 int ii, jj;
11471 for(ii=0; ii<pIx->nColumn; ii++){
11472 jj = pIx->aiColumn[ii];
11473 if( jj<0 ) continue;
11474 if( jj>63 ) jj = 63;
11475 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
11476 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
11477 }
11478 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
11479 (u8*)&colUsed, P4_INT64);
11480 }
11481 #endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
11482 }
11483 }
11484 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
11485 }
11486 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
11487 if( db->mallocFailed ) goto whereBeginError;
11488
11489 /* Generate the code to do the search. Each iteration of the for
11490 ** loop below generates code for a single nested loop of the VM
11491 ** program.
11492 */
11493 notReady = ~(Bitmask)0;
11494 for(ii=0; ii<nTabList; ii++){
11495 int addrExplain;
11496 int wsFlags;
11497 pLevel = &pWInfo->a[ii];
11498 wsFlags = pLevel->pWLoop->wsFlags;
11499 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
11500 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
11501 constructAutomaticIndex(pParse, &pWInfo->sWC,
11502 &pTabList->a[pLevel->iFrom], notReady, pLevel);
11503 if( db->mallocFailed ) goto whereBeginError;
11504 }
11505 #endif
11506 addrExplain = sqlite3WhereExplainOneScan(
11507 pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
11508 );
11509 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
11510 notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
11511 pWInfo->iContinue = pLevel->addrCont;
11512 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
11513 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
11514 }
11515 }
11516
11517 /* Done. */
11518 VdbeModuleComment((v, "Begin WHERE-core"));
11519 return pWInfo;
11520
11521 /* Jump here if malloc fails */
11522 whereBeginError:
11523 if( pWInfo ){
11524 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
11525 whereInfoFree(db, pWInfo);
11526 }
11527 return 0;
11528 }
11529
11530 /*
11531 ** Generate the end of the WHERE loop. See comments on
11532 ** sqlite3WhereBegin() for additional information.
11533 */
11534 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
11535 Parse *pParse = pWInfo->pParse;
11536 Vdbe *v = pParse->pVdbe;
11537 int i;
11538 WhereLevel *pLevel;
11539 WhereLoop *pLoop;
11540 SrcList *pTabList = pWInfo->pTabList;
11541 sqlite3 *db = pParse->db;
11542
11543 /* Generate loop termination code.
11544 */
11545 VdbeModuleComment((v, "End WHERE-core"));
11546 sqlite3ExprCacheClear(pParse);
11547 for(i=pWInfo->nLevel-1; i>=0; i--){
11548 int addr;
11549 pLevel = &pWInfo->a[i];
11550 pLoop = pLevel->pWLoop;
11551 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
11552 if( pLevel->op!=OP_Noop ){
11553 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
11554 sqlite3VdbeChangeP5(v, pLevel->p5);
11555 VdbeCoverage(v);
11556 VdbeCoverageIf(v, pLevel->op==OP_Next);
11557 VdbeCoverageIf(v, pLevel->op==OP_Prev);
11558 VdbeCoverageIf(v, pLevel->op==OP_VNext);
11559 }
11560 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
11561 struct InLoop *pIn;
11562 int j;
11563 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
11564 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
11565 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
11566 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
11567 VdbeCoverage(v);
11568 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
11569 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
11570 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
11571 }
11572 }
11573 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
11574 if( pLevel->addrSkip ){
11575 sqlite3VdbeGoto(v, pLevel->addrSkip);
11576 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
11577 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
11578 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
11579 }
11580 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
11581 if( pLevel->addrLikeRep ){
11582 int op;
11583 if( sqlite3VdbeGetOp(v, pLevel->addrLikeRep-1)->p1 ){
11584 op = OP_DecrJumpZero;
11585 }else{
11586 op = OP_JumpZeroIncr;
11587 }
11588 sqlite3VdbeAddOp2(v, op, pLevel->iLikeRepCntr, pLevel->addrLikeRep);
11589 VdbeCoverage(v);
11590 }
11591 #endif
11592 if( pLevel->iLeftJoin ){
11593 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
11594 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
11595 || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
11596 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
11597 sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
11598 }
11599 if( pLoop->wsFlags & WHERE_INDEXED ){
11600 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
11601 }
11602 if( pLevel->op==OP_Return ){
11603 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
11604 }else{
11605 sqlite3VdbeGoto(v, pLevel->addrFirst);
11606 }
11607 sqlite3VdbeJumpHere(v, addr);
11608 }
11609 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
11610 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
11611 }
11612
11613 /* The "break" point is here, just past the end of the outer loop.
11614 ** Set it.
11615 */
11616 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
11617
11618 assert( pWInfo->nLevel<=pTabList->nSrc );
11619 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
11620 int k, last;
11621 VdbeOp *pOp;
11622 Index *pIdx = 0;
11623 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
11624 Table *pTab = pTabItem->pTab;
11625 assert( pTab!=0 );
11626 pLoop = pLevel->pWLoop;
11627
11628 /* For a co-routine, change all OP_Column references to the table of
11629 ** the co-routine into OP_Copy of result contained in a register.
11630 ** OP_Rowid becomes OP_Null.
11631 */
11632 if( pTabItem->fg.viaCoroutine && !db->mallocFailed ){
11633 translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur,
11634 pTabItem->regResult, 0);
11635 continue;
11636 }
11637
11638 /* Close all of the cursors that were opened by sqlite3WhereBegin.
11639 ** Except, do not close cursors that will be reused by the OR optimization
11640 ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors
11641 ** created for the ONEPASS optimization.
11642 */
11643 if( (pTab->tabFlags & TF_Ephemeral)==0
11644 && pTab->pSelect==0
11645 && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
11646 ){
11647 int ws = pLoop->wsFlags;
11648 if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){
11649 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
11650 }
11651 if( (ws & WHERE_INDEXED)!=0
11652 && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0
11653 && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
11654 ){
11655 sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
11656 }
11657 }
11658
11659 /* If this scan uses an index, make VDBE code substitutions to read data
11660 ** from the index instead of from the table where possible. In some cases
11661 ** this optimization prevents the table from ever being read, which can
11662 ** yield a significant performance boost.
11663 **
11664 ** Calls to the code generator in between sqlite3WhereBegin and
11665 ** sqlite3WhereEnd will have created code that references the table
11666 ** directly. This loop scans all that code looking for opcodes
11667 ** that reference the table and converts them into opcodes that
11668 ** reference the index.
11669 */
11670 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
11671 pIdx = pLoop->u.btree.pIndex;
11672 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
11673 pIdx = pLevel->u.pCovidx;
11674 }
11675 if( pIdx
11676 && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable))
11677 && !db->mallocFailed
11678 ){
11679 last = sqlite3VdbeCurrentAddr(v);
11680 k = pLevel->addrBody;
11681 pOp = sqlite3VdbeGetOp(v, k);
11682 for(; k<last; k++, pOp++){
11683 if( pOp->p1!=pLevel->iTabCur ) continue;
11684 if( pOp->opcode==OP_Column ){
11685 int x = pOp->p2;
11686 assert( pIdx->pTable==pTab );
11687 if( !HasRowid(pTab) ){
11688 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
11689 x = pPk->aiColumn[x];
11690 assert( x>=0 );
11691 }
11692 x = sqlite3ColumnOfIndex(pIdx, x);
11693 if( x>=0 ){
11694 pOp->p2 = x;
11695 pOp->p1 = pLevel->iIdxCur;
11696 }
11697 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
11698 }else if( pOp->opcode==OP_Rowid ){
11699 pOp->p1 = pLevel->iIdxCur;
11700 pOp->opcode = OP_IdxRowid;
11701 }
11702 }
11703 }
11704 }
11705
11706 /* Final cleanup
11707 */
11708 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
11709 whereInfoFree(db, pWInfo);
11710 return;
11711 }
11712
11713 /************** End of where.c ***********************************************/
11714 /************** Begin file parse.c *******************************************/
11715 /*
11716 ** 2000-05-29
11717 **
11718 ** The author disclaims copyright to this source code. In place of
11719 ** a legal notice, here is a blessing:
11720 **
11721 ** May you do good and not evil.
11722 ** May you find forgiveness for yourself and forgive others.
11723 ** May you share freely, never taking more than you give.
11724 **
11725 *************************************************************************
11726 ** Driver template for the LEMON parser generator.
11727 **
11728 ** The "lemon" program processes an LALR(1) input grammar file, then uses
11729 ** this template to construct a parser. The "lemon" program inserts text
11730 ** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
11731 ** interstitial "-" characters) contained in this template is changed into
11732 ** the value of the %name directive from the grammar. Otherwise, the content
11733 ** of this template is copied straight through into the generate parser
11734 ** source file.
11735 **
11736 ** The following is the concatenation of all %include directives from the
11737 ** input grammar file:
11738 */
11739 /* #include <stdio.h> */
11740 /************ Begin %include sections from the grammar ************************/
11741
11742 /* #include "sqliteInt.h" */
11743
11744 /*
11745 ** Disable all error recovery processing in the parser push-down
11746 ** automaton.
11747 */
11748 #define YYNOERRORRECOVERY 1
11749
11750 /*
11751 ** Make yytestcase() the same as testcase()
11752 */
11753 #define yytestcase(X) testcase(X)
11754
11755 /*
11756 ** Indicate that sqlite3ParserFree() will never be called with a null
11757 ** pointer.
11758 */
11759 #define YYPARSEFREENEVERNULL 1
11760
11761 /*
11762 ** Alternative datatype for the argument to the malloc() routine passed
11763 ** into sqlite3ParserAlloc(). The default is size_t.
11764 */
11765 #define YYMALLOCARGTYPE u64
11766
11767 /*
11768 ** An instance of this structure holds information about the
11769 ** LIMIT clause of a SELECT statement.
11770 */
11771 struct LimitVal {
11772 Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */
11773 Expr *pOffset; /* The OFFSET expression. NULL if there is none */
11774 };
11775
11776 /*
11777 ** An instance of this structure is used to store the LIKE,
11778 ** GLOB, NOT LIKE, and NOT GLOB operators.
11779 */
11780 struct LikeOp {
11781 Token eOperator; /* "like" or "glob" or "regexp" */
11782 int bNot; /* True if the NOT keyword is present */
11783 };
11784
11785 /*
11786 ** An instance of the following structure describes the event of a
11787 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
11788 ** TK_DELETE, or TK_INSTEAD. If the event is of the form
11789 **
11790 ** UPDATE ON (a,b,c)
11791 **
11792 ** Then the "b" IdList records the list "a,b,c".
11793 */
11794 struct TrigEvent { int a; IdList * b; };
11795
11796 /*
11797 ** An instance of this structure holds the ATTACH key and the key type.
11798 */
11799 struct AttachKey { int type; Token key; };
11800
11801
11802 /*
11803 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
11804 ** all elements in the list. And make sure list length does not exceed
11805 ** SQLITE_LIMIT_COMPOUND_SELECT.
11806 */
11807 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
11808 if( p->pPrior ){
11809 Select *pNext = 0, *pLoop;
11810 int mxSelect, cnt = 0;
11811 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
11812 pLoop->pNext = pNext;
11813 pLoop->selFlags |= SF_Compound;
11814 }
11815 if( (p->selFlags & SF_MultiValue)==0 &&
11816 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
11817 cnt>mxSelect
11818 ){
11819 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
11820 }
11821 }
11822 }
11823
11824 /* This is a utility routine used to set the ExprSpan.zStart and
11825 ** ExprSpan.zEnd values of pOut so that the span covers the complete
11826 ** range of text beginning with pStart and going to the end of pEnd.
11827 */
11828 static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
11829 pOut->zStart = pStart->z;
11830 pOut->zEnd = &pEnd->z[pEnd->n];
11831 }
11832
11833 /* Construct a new Expr object from a single identifier. Use the
11834 ** new Expr to populate pOut. Set the span of pOut to be the identifier
11835 ** that created the expression.
11836 */
11837 static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
11838 pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
11839 pOut->zStart = pValue->z;
11840 pOut->zEnd = &pValue->z[pValue->n];
11841 }
11842
11843 /* This routine constructs a binary expression node out of two ExprSpan
11844 ** objects and uses the result to populate a new ExprSpan object.
11845 */
11846 static void spanBinaryExpr(
11847 ExprSpan *pOut, /* Write the result here */
11848 Parse *pParse, /* The parsing context. Errors accumulate here */
11849 int op, /* The binary operation */
11850 ExprSpan *pLeft, /* The left operand */
11851 ExprSpan *pRight /* The right operand */
11852 ){
11853 pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
11854 pOut->zStart = pLeft->zStart;
11855 pOut->zEnd = pRight->zEnd;
11856 }
11857
11858 /* If doNot is true, then add a TK_NOT Expr-node wrapper around the
11859 ** outside of *ppExpr.
11860 */
11861 static void exprNot(Parse *pParse, int doNot, Expr **ppExpr){
11862 if( doNot ) *ppExpr = sqlite3PExpr(pParse, TK_NOT, *ppExpr, 0, 0);
11863 }
11864
11865 /* Construct an expression node for a unary postfix operator
11866 */
11867 static void spanUnaryPostfix(
11868 ExprSpan *pOut, /* Write the new expression node here */
11869 Parse *pParse, /* Parsing context to record errors */
11870 int op, /* The operator */
11871 ExprSpan *pOperand, /* The operand */
11872 Token *pPostOp /* The operand token for setting the span */
11873 ){
11874 pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
11875 pOut->zStart = pOperand->zStart;
11876 pOut->zEnd = &pPostOp->z[pPostOp->n];
11877 }
11878
11879 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
11880 ** unary TK_ISNULL or TK_NOTNULL expression. */
11881 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
11882 sqlite3 *db = pParse->db;
11883 if( pY && pA && pY->op==TK_NULL ){
11884 pA->op = (u8)op;
11885 sqlite3ExprDelete(db, pA->pRight);
11886 pA->pRight = 0;
11887 }
11888 }
11889
11890 /* Construct an expression node for a unary prefix operator
11891 */
11892 static void spanUnaryPrefix(
11893 ExprSpan *pOut, /* Write the new expression node here */
11894 Parse *pParse, /* Parsing context to record errors */
11895 int op, /* The operator */
11896 ExprSpan *pOperand, /* The operand */
11897 Token *pPreOp /* The operand token for setting the span */
11898 ){
11899 pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
11900 pOut->zStart = pPreOp->z;
11901 pOut->zEnd = pOperand->zEnd;
11902 }
11903
11904 /* Add a single new term to an ExprList that is used to store a
11905 ** list of identifiers. Report an error if the ID list contains
11906 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
11907 ** error while parsing a legacy schema.
11908 */
11909 static ExprList *parserAddExprIdListTerm(
11910 Parse *pParse,
11911 ExprList *pPrior,
11912 Token *pIdToken,
11913 int hasCollate,
11914 int sortOrder
11915 ){
11916 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
11917 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
11918 && pParse->db->init.busy==0
11919 ){
11920 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
11921 pIdToken->n, pIdToken->z);
11922 }
11923 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
11924 return p;
11925 }
11926 /**************** End of %include directives **********************************/
11927 /* These constants specify the various numeric values for terminal symbols
11928 ** in a format understandable to "makeheaders". This section is blank unless
11929 ** "lemon" is run with the "-m" command-line option.
11930 ***************** Begin makeheaders token definitions *************************/
11931 /**************** End makeheaders token definitions ***************************/
11932
11933 /* The next sections is a series of control #defines.
11934 ** various aspects of the generated parser.
11935 ** YYCODETYPE is the data type used to store the integer codes
11936 ** that represent terminal and non-terminal symbols.
11937 ** "unsigned char" is used if there are fewer than
11938 ** 256 symbols. Larger types otherwise.
11939 ** YYNOCODE is a number of type YYCODETYPE that is not used for
11940 ** any terminal or nonterminal symbol.
11941 ** YYFALLBACK If defined, this indicates that one or more tokens
11942 ** (also known as: "terminal symbols") have fall-back
11943 ** values which should be used if the original symbol
11944 ** would not parse. This permits keywords to sometimes
11945 ** be used as identifiers, for example.
11946 ** YYACTIONTYPE is the data type used for "action codes" - numbers
11947 ** that indicate what to do in response to the next
11948 ** token.
11949 ** sqlite3ParserTOKENTYPE is the data type used for minor type for termin al
11950 ** symbols. Background: A "minor type" is a semantic
11951 ** value associated with a terminal or non-terminal
11952 ** symbols. For example, for an "ID" terminal symbol,
11953 ** the minor type might be the name of the identifier.
11954 ** Each non-terminal can have a different minor type.
11955 ** Terminal symbols all have the same minor type, though.
11956 ** This macros defines the minor type for terminal
11957 ** symbols.
11958 ** YYMINORTYPE is the data type used for all minor types.
11959 ** This is typically a union of many types, one of
11960 ** which is sqlite3ParserTOKENTYPE. The entry in the unio n
11961 ** for terminal symbols is called "yy0".
11962 ** YYSTACKDEPTH is the maximum depth of the parser's stack. If
11963 ** zero the stack is dynamically sized using realloc()
11964 ** sqlite3ParserARG_SDECL A static variable declaration for the %extra_ar gument
11965 ** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
11966 ** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
11967 ** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
11968 ** YYERRORSYMBOL is the code number of the error symbol. If not
11969 ** defined, then do no error processing.
11970 ** YYNSTATE the combined number of states.
11971 ** YYNRULE the number of rules in the grammar
11972 ** YY_MAX_SHIFT Maximum value for shift actions
11973 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
11974 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
11975 ** YY_MIN_REDUCE Maximum value for reduce actions
11976 ** YY_ERROR_ACTION The yy_action[] code for syntax error
11977 ** YY_ACCEPT_ACTION The yy_action[] code for accept
11978 ** YY_NO_ACTION The yy_action[] code for no-op
11979 */
11980 #ifndef INTERFACE
11981 # define INTERFACE 1
11982 #endif
11983 /************* Begin control #defines *****************************************/
11984 #define YYCODETYPE unsigned char
11985 #define YYNOCODE 253
11986 #define YYACTIONTYPE unsigned short int
11987 #define YYWILDCARD 70
11988 #define sqlite3ParserTOKENTYPE Token
11989 typedef union {
11990 int yyinit;
11991 sqlite3ParserTOKENTYPE yy0;
11992 int yy4;
11993 struct TrigEvent yy90;
11994 ExprSpan yy118;
11995 TriggerStep* yy203;
11996 struct {int value; int mask;} yy215;
11997 SrcList* yy259;
11998 struct LimitVal yy292;
11999 Expr* yy314;
12000 ExprList* yy322;
12001 struct LikeOp yy342;
12002 IdList* yy384;
12003 Select* yy387;
12004 With* yy451;
12005 } YYMINORTYPE;
12006 #ifndef YYSTACKDEPTH
12007 #define YYSTACKDEPTH 100
12008 #endif
12009 #define sqlite3ParserARG_SDECL Parse *pParse;
12010 #define sqlite3ParserARG_PDECL ,Parse *pParse
12011 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
12012 #define sqlite3ParserARG_STORE yypParser->pParse = pParse
12013 #define YYFALLBACK 1
12014 #define YYNSTATE 436
12015 #define YYNRULE 328
12016 #define YY_MAX_SHIFT 435
12017 #define YY_MIN_SHIFTREDUCE 649
12018 #define YY_MAX_SHIFTREDUCE 976
12019 #define YY_MIN_REDUCE 977
12020 #define YY_MAX_REDUCE 1304
12021 #define YY_ERROR_ACTION 1305
12022 #define YY_ACCEPT_ACTION 1306
12023 #define YY_NO_ACTION 1307
12024 /************* End control #defines *******************************************/
12025
12026 /* The yyzerominor constant is used to initialize instances of
12027 ** YYMINORTYPE objects to zero. */
12028 static const YYMINORTYPE yyzerominor = { 0 };
12029
12030 /* Define the yytestcase() macro to be a no-op if is not already defined
12031 ** otherwise.
12032 **
12033 ** Applications can choose to define yytestcase() in the %include section
12034 ** to a macro that can assist in verifying code coverage. For production
12035 ** code the yytestcase() macro should be turned off. But it is useful
12036 ** for testing.
12037 */
12038 #ifndef yytestcase
12039 # define yytestcase(X)
12040 #endif
12041
12042
12043 /* Next are the tables used to determine what action to take based on the
12044 ** current state and lookahead token. These tables are used to implement
12045 ** functions that take a state number and lookahead value and return an
12046 ** action integer.
12047 **
12048 ** Suppose the action integer is N. Then the action is determined as
12049 ** follows
12050 **
12051 ** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
12052 ** token onto the stack and goto state N.
12053 **
12054 ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
12055 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
12056 **
12057 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
12058 ** and YY_MAX_REDUCE
12059
12060 ** N == YY_ERROR_ACTION A syntax error has occurred.
12061 **
12062 ** N == YY_ACCEPT_ACTION The parser accepts its input.
12063 **
12064 ** N == YY_NO_ACTION No such action. Denotes unused
12065 ** slots in the yy_action[] table.
12066 **
12067 ** The action table is constructed as a single large table named yy_action[].
12068 ** Given state S and lookahead X, the action is computed as
12069 **
12070 ** yy_action[ yy_shift_ofst[S] + X ]
12071 **
12072 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
12073 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
12074 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
12075 ** and that yy_default[S] should be used instead.
12076 **
12077 ** The formula above is for computing the action when the lookahead is
12078 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
12079 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
12080 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
12081 ** YY_SHIFT_USE_DFLT.
12082 **
12083 ** The following are the tables generated in this section:
12084 **
12085 ** yy_action[] A single table containing all actions.
12086 ** yy_lookahead[] A table containing the lookahead for each entry in
12087 ** yy_action. Used to detect hash collisions.
12088 ** yy_shift_ofst[] For each state, the offset into yy_action for
12089 ** shifting terminals.
12090 ** yy_reduce_ofst[] For each state, the offset into yy_action for
12091 ** shifting non-terminals after a reduce.
12092 ** yy_default[] Default action for each state.
12093 **
12094 *********** Begin parsing tables **********************************************/
12095 #define YY_ACTTAB_COUNT (1501)
12096 static const YYACTIONTYPE yy_action[] = {
12097 /* 0 */ 311, 1306, 145, 651, 2, 192, 652, 338, 780, 92,
12098 /* 10 */ 92, 92, 92, 85, 90, 90, 90, 90, 89, 89,
12099 /* 20 */ 88, 88, 88, 87, 335, 88, 88, 88, 87, 335,
12100 /* 30 */ 327, 856, 856, 92, 92, 92, 92, 697, 90, 90,
12101 /* 40 */ 90, 90, 89, 89, 88, 88, 88, 87, 335, 76,
12102 /* 50 */ 807, 74, 93, 94, 84, 868, 871, 860, 860, 91,
12103 /* 60 */ 91, 92, 92, 92, 92, 335, 90, 90, 90, 90,
12104 /* 70 */ 89, 89, 88, 88, 88, 87, 335, 311, 780, 90,
12105 /* 80 */ 90, 90, 90, 89, 89, 88, 88, 88, 87, 335,
12106 /* 90 */ 356, 808, 776, 701, 689, 689, 86, 83, 166, 257,
12107 /* 100 */ 809, 715, 430, 86, 83, 166, 324, 697, 856, 856,
12108 /* 110 */ 201, 158, 276, 387, 271, 386, 188, 689, 689, 828,
12109 /* 120 */ 86, 83, 166, 269, 833, 49, 123, 87, 335, 93,
12110 /* 130 */ 94, 84, 868, 871, 860, 860, 91, 91, 92, 92,
12111 /* 140 */ 92, 92, 239, 90, 90, 90, 90, 89, 89, 88,
12112 /* 150 */ 88, 88, 87, 335, 311, 763, 333, 332, 216, 408,
12113 /* 160 */ 394, 69, 231, 393, 690, 691, 396, 910, 251, 354,
12114 /* 170 */ 250, 288, 315, 430, 908, 430, 909, 89, 89, 88,
12115 /* 180 */ 88, 88, 87, 335, 391, 856, 856, 690, 691, 183,
12116 /* 190 */ 95, 123, 384, 381, 380, 833, 31, 833, 49, 912,
12117 /* 200 */ 912, 751, 752, 379, 123, 311, 93, 94, 84, 868,
12118 /* 210 */ 871, 860, 860, 91, 91, 92, 92, 92, 92, 114,
12119 /* 220 */ 90, 90, 90, 90, 89, 89, 88, 88, 88, 87,
12120 /* 230 */ 335, 430, 408, 399, 435, 657, 856, 856, 346, 57,
12121 /* 240 */ 232, 828, 109, 704, 366, 689, 689, 363, 825, 760,
12122 /* 250 */ 97, 749, 752, 833, 49, 708, 708, 93, 94, 84,
12123 /* 260 */ 868, 871, 860, 860, 91, 91, 92, 92, 92, 92,
12124 /* 270 */ 423, 90, 90, 90, 90, 89, 89, 88, 88, 88,
12125 /* 280 */ 87, 335, 311, 114, 22, 361, 688, 58, 408, 390,
12126 /* 290 */ 251, 349, 240, 213, 762, 689, 689, 847, 685, 115,
12127 /* 300 */ 361, 231, 393, 689, 689, 396, 183, 689, 689, 384,
12128 /* 310 */ 381, 380, 361, 856, 856, 690, 691, 160, 159, 223,
12129 /* 320 */ 379, 738, 25, 806, 707, 841, 143, 689, 689, 835,
12130 /* 330 */ 392, 339, 766, 766, 93, 94, 84, 868, 871, 860,
12131 /* 340 */ 860, 91, 91, 92, 92, 92, 92, 914, 90, 90,
12132 /* 350 */ 90, 90, 89, 89, 88, 88, 88, 87, 335, 311,
12133 /* 360 */ 840, 840, 840, 266, 257, 690, 691, 778, 706, 86,
12134 /* 370 */ 83, 166, 219, 690, 691, 737, 1, 690, 691, 689,
12135 /* 380 */ 689, 689, 689, 430, 86, 83, 166, 249, 688, 937,
12136 /* 390 */ 856, 856, 427, 699, 700, 828, 298, 690, 691, 221,
12137 /* 400 */ 686, 115, 123, 944, 795, 833, 48, 342, 305, 970,
12138 /* 410 */ 847, 93, 94, 84, 868, 871, 860, 860, 91, 91,
12139 /* 420 */ 92, 92, 92, 92, 114, 90, 90, 90, 90, 89,
12140 /* 430 */ 89, 88, 88, 88, 87, 335, 311, 940, 841, 679,
12141 /* 440 */ 713, 429, 835, 430, 251, 354, 250, 355, 288, 690,
12142 /* 450 */ 691, 690, 691, 285, 941, 340, 971, 287, 210, 23,
12143 /* 460 */ 174, 793, 832, 430, 353, 833, 10, 856, 856, 24,
12144 /* 470 */ 942, 151, 753, 840, 840, 840, 794, 968, 1290, 321,
12145 /* 480 */ 398, 1290, 356, 352, 754, 833, 49, 935, 93, 94,
12146 /* 490 */ 84, 868, 871, 860, 860, 91, 91, 92, 92, 92,
12147 /* 500 */ 92, 430, 90, 90, 90, 90, 89, 89, 88, 88,
12148 /* 510 */ 88, 87, 335, 311, 376, 114, 907, 705, 430, 907,
12149 /* 520 */ 328, 890, 114, 833, 10, 966, 430, 857, 857, 320,
12150 /* 530 */ 189, 163, 832, 165, 430, 906, 344, 323, 906, 904,
12151 /* 540 */ 833, 10, 965, 306, 856, 856, 187, 419, 833, 10,
12152 /* 550 */ 220, 869, 872, 832, 222, 403, 833, 49, 1219, 793,
12153 /* 560 */ 68, 937, 406, 245, 66, 93, 94, 84, 868, 871,
12154 /* 570 */ 860, 860, 91, 91, 92, 92, 92, 92, 861, 90,
12155 /* 580 */ 90, 90, 90, 89, 89, 88, 88, 88, 87, 335,
12156 /* 590 */ 311, 404, 213, 762, 834, 345, 114, 940, 902, 368,
12157 /* 600 */ 727, 5, 316, 192, 396, 772, 780, 269, 230, 242,
12158 /* 610 */ 771, 244, 397, 164, 941, 385, 123, 347, 55, 355,
12159 /* 620 */ 329, 856, 856, 728, 333, 332, 688, 968, 1291, 724,
12160 /* 630 */ 942, 1291, 413, 214, 833, 9, 362, 286, 955, 115,
12161 /* 640 */ 718, 311, 93, 94, 84, 868, 871, 860, 860, 91,
12162 /* 650 */ 91, 92, 92, 92, 92, 430, 90, 90, 90, 90,
12163 /* 660 */ 89, 89, 88, 88, 88, 87, 335, 912, 912, 1300,
12164 /* 670 */ 1300, 758, 856, 856, 325, 966, 780, 833, 35, 747,
12165 /* 680 */ 720, 334, 699, 700, 977, 652, 338, 243, 745, 920,
12166 /* 690 */ 920, 369, 187, 93, 94, 84, 868, 871, 860, 860,
12167 /* 700 */ 91, 91, 92, 92, 92, 92, 114, 90, 90, 90,
12168 /* 710 */ 90, 89, 89, 88, 88, 88, 87, 335, 311, 430,
12169 /* 720 */ 954, 430, 112, 310, 430, 693, 317, 698, 400, 430,
12170 /* 730 */ 793, 359, 430, 1017, 430, 192, 430, 401, 780, 430,
12171 /* 740 */ 360, 833, 36, 833, 12, 430, 833, 27, 316, 856,
12172 /* 750 */ 856, 833, 37, 20, 833, 38, 833, 39, 833, 28,
12173 /* 760 */ 72, 833, 29, 663, 664, 665, 264, 833, 40, 234,
12174 /* 770 */ 93, 94, 84, 868, 871, 860, 860, 91, 91, 92,
12175 /* 780 */ 92, 92, 92, 430, 90, 90, 90, 90, 89, 89,
12176 /* 790 */ 88, 88, 88, 87, 335, 311, 430, 698, 430, 917,
12177 /* 800 */ 147, 430, 165, 916, 275, 833, 41, 430, 780, 430,
12178 /* 810 */ 21, 430, 259, 430, 262, 274, 430, 367, 833, 42,
12179 /* 820 */ 833, 11, 430, 833, 43, 235, 856, 856, 793, 833,
12180 /* 830 */ 99, 833, 44, 833, 45, 833, 32, 75, 833, 46,
12181 /* 840 */ 305, 967, 257, 257, 833, 47, 311, 93, 94, 84,
12182 /* 850 */ 868, 871, 860, 860, 91, 91, 92, 92, 92, 92,
12183 /* 860 */ 430, 90, 90, 90, 90, 89, 89, 88, 88, 88,
12184 /* 870 */ 87, 335, 430, 186, 185, 184, 238, 856, 856, 650,
12185 /* 880 */ 2, 1064, 833, 33, 739, 217, 218, 257, 971, 257,
12186 /* 890 */ 426, 317, 257, 774, 833, 117, 257, 311, 93, 94,
12187 /* 900 */ 84, 868, 871, 860, 860, 91, 91, 92, 92, 92,
12188 /* 910 */ 92, 430, 90, 90, 90, 90, 89, 89, 88, 88,
12189 /* 920 */ 88, 87, 335, 430, 318, 124, 212, 163, 856, 856,
12190 /* 930 */ 943, 900, 898, 833, 118, 759, 726, 725, 257, 755,
12191 /* 940 */ 289, 289, 733, 734, 961, 833, 119, 682, 311, 93,
12192 /* 950 */ 82, 84, 868, 871, 860, 860, 91, 91, 92, 92,
12193 /* 960 */ 92, 92, 430, 90, 90, 90, 90, 89, 89, 88,
12194 /* 970 */ 88, 88, 87, 335, 430, 716, 246, 322, 331, 856,
12195 /* 980 */ 856, 256, 114, 357, 833, 53, 808, 913, 913, 932,
12196 /* 990 */ 156, 416, 420, 424, 930, 809, 833, 34, 364, 311,
12197 /* 1000 */ 253, 94, 84, 868, 871, 860, 860, 91, 91, 92,
12198 /* 1010 */ 92, 92, 92, 430, 90, 90, 90, 90, 89, 89,
12199 /* 1020 */ 88, 88, 88, 87, 335, 430, 114, 114, 114, 960,
12200 /* 1030 */ 856, 856, 307, 258, 830, 833, 100, 191, 252, 377,
12201 /* 1040 */ 267, 68, 197, 68, 261, 716, 769, 833, 50, 71,
12202 /* 1050 */ 911, 911, 263, 84, 868, 871, 860, 860, 91, 91,
12203 /* 1060 */ 92, 92, 92, 92, 430, 90, 90, 90, 90, 89,
12204 /* 1070 */ 89, 88, 88, 88, 87, 335, 80, 425, 802, 3,
12205 /* 1080 */ 1214, 191, 430, 265, 336, 336, 833, 101, 741, 80,
12206 /* 1090 */ 425, 897, 3, 723, 722, 428, 721, 336, 336, 430,
12207 /* 1100 */ 893, 270, 430, 197, 833, 102, 430, 800, 428, 430,
12208 /* 1110 */ 695, 430, 843, 111, 414, 430, 784, 409, 430, 831,
12209 /* 1120 */ 430, 833, 98, 123, 833, 116, 847, 414, 833, 49,
12210 /* 1130 */ 779, 833, 113, 833, 106, 226, 123, 833, 105, 847,
12211 /* 1140 */ 833, 103, 833, 104, 791, 411, 77, 78, 290, 412,
12212 /* 1150 */ 430, 291, 114, 79, 432, 431, 389, 430, 835, 77,
12213 /* 1160 */ 78, 897, 839, 408, 410, 430, 79, 432, 431, 372,
12214 /* 1170 */ 703, 835, 833, 52, 430, 80, 425, 430, 3, 833,
12215 /* 1180 */ 54, 772, 843, 336, 336, 684, 771, 833, 51, 840,
12216 /* 1190 */ 840, 840, 842, 19, 428, 672, 833, 26, 671, 833,
12217 /* 1200 */ 30, 673, 840, 840, 840, 842, 19, 207, 661, 278,
12218 /* 1210 */ 304, 148, 280, 414, 282, 248, 358, 822, 382, 6,
12219 /* 1220 */ 348, 161, 273, 80, 425, 847, 3, 934, 895, 720,
12220 /* 1230 */ 894, 336, 336, 296, 157, 415, 241, 284, 674, 958,
12221 /* 1240 */ 194, 953, 428, 951, 948, 77, 78, 777, 319, 56,
12222 /* 1250 */ 59, 135, 79, 432, 431, 121, 66, 835, 146, 128,
12223 /* 1260 */ 350, 414, 819, 130, 351, 131, 132, 133, 375, 173,
12224 /* 1270 */ 107, 138, 149, 847, 365, 178, 62, 70, 425, 936,
12225 /* 1280 */ 3, 827, 889, 371, 255, 336, 336, 792, 840, 840,
12226 /* 1290 */ 840, 842, 19, 77, 78, 915, 428, 208, 179, 144,
12227 /* 1300 */ 79, 432, 431, 373, 260, 835, 180, 326, 675, 181,
12228 /* 1310 */ 308, 744, 388, 743, 731, 414, 718, 742, 730, 712,
12229 /* 1320 */ 402, 309, 711, 272, 788, 65, 710, 847, 709, 277,
12230 /* 1330 */ 193, 789, 787, 279, 876, 73, 840, 840, 840, 842,
12231 /* 1340 */ 19, 786, 281, 418, 283, 422, 227, 77, 78, 330,
12232 /* 1350 */ 228, 229, 96, 767, 79, 432, 431, 407, 67, 835,
12233 /* 1360 */ 215, 292, 293, 405, 294, 303, 302, 301, 204, 299,
12234 /* 1370 */ 295, 202, 676, 681, 7, 433, 669, 203, 205, 206,
12235 /* 1380 */ 125, 110, 313, 434, 667, 666, 658, 168, 224, 237,
12236 /* 1390 */ 840, 840, 840, 842, 19, 120, 656, 337, 236, 155,
12237 /* 1400 */ 167, 341, 233, 314, 108, 905, 903, 826, 127, 126,
12238 /* 1410 */ 756, 170, 129, 172, 247, 928, 134, 136, 171, 60,
12239 /* 1420 */ 61, 123, 169, 137, 933, 175, 176, 927, 8, 13,
12240 /* 1430 */ 177, 254, 918, 139, 191, 924, 140, 370, 678, 150,
12241 /* 1440 */ 374, 182, 274, 268, 141, 122, 63, 14, 378, 15,
12242 /* 1450 */ 383, 64, 225, 846, 845, 874, 16, 4, 729, 765,
12243 /* 1460 */ 770, 162, 395, 209, 211, 142, 801, 878, 796, 312,
12244 /* 1470 */ 71, 68, 875, 873, 939, 190, 417, 938, 17, 195,
12245 /* 1480 */ 196, 152, 18, 975, 199, 976, 153, 198, 154, 421,
12246 /* 1490 */ 877, 844, 696, 81, 200, 297, 343, 1019, 1018, 300,
12247 /* 1500 */ 653,
12248 };
12249 static const YYCODETYPE yy_lookahead[] = {
12250 /* 0 */ 19, 144, 145, 146, 147, 24, 1, 2, 27, 80,
12251 /* 10 */ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
12252 /* 20 */ 91, 92, 93, 94, 95, 91, 92, 93, 94, 95,
12253 /* 30 */ 19, 50, 51, 80, 81, 82, 83, 27, 85, 86,
12254 /* 40 */ 87, 88, 89, 90, 91, 92, 93, 94, 95, 137,
12255 /* 50 */ 177, 139, 71, 72, 73, 74, 75, 76, 77, 78,
12256 /* 60 */ 79, 80, 81, 82, 83, 95, 85, 86, 87, 88,
12257 /* 70 */ 89, 90, 91, 92, 93, 94, 95, 19, 97, 85,
12258 /* 80 */ 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
12259 /* 90 */ 152, 33, 212, 173, 27, 28, 223, 224, 225, 152,
12260 /* 100 */ 42, 181, 152, 223, 224, 225, 95, 97, 50, 51,
12261 /* 110 */ 99, 100, 101, 102, 103, 104, 105, 27, 28, 59,
12262 /* 120 */ 223, 224, 225, 112, 174, 175, 66, 94, 95, 71,
12263 /* 130 */ 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
12264 /* 140 */ 82, 83, 195, 85, 86, 87, 88, 89, 90, 91,
12265 /* 150 */ 92, 93, 94, 95, 19, 197, 89, 90, 220, 209,
12266 /* 160 */ 210, 26, 119, 120, 97, 98, 208, 100, 108, 109,
12267 /* 170 */ 110, 152, 157, 152, 107, 152, 109, 89, 90, 91,
12268 /* 180 */ 92, 93, 94, 95, 163, 50, 51, 97, 98, 99,
12269 /* 190 */ 55, 66, 102, 103, 104, 174, 175, 174, 175, 132,
12270 /* 200 */ 133, 192, 193, 113, 66, 19, 71, 72, 73, 74,
12271 /* 210 */ 75, 76, 77, 78, 79, 80, 81, 82, 83, 198,
12272 /* 220 */ 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
12273 /* 230 */ 95, 152, 209, 210, 148, 149, 50, 51, 100, 53,
12274 /* 240 */ 154, 59, 156, 174, 229, 27, 28, 232, 163, 163,
12275 /* 250 */ 22, 192, 193, 174, 175, 27, 28, 71, 72, 73,
12276 /* 260 */ 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
12277 /* 270 */ 251, 85, 86, 87, 88, 89, 90, 91, 92, 93,
12278 /* 280 */ 94, 95, 19, 198, 198, 152, 152, 24, 209, 210,
12279 /* 290 */ 108, 109, 110, 196, 197, 27, 28, 69, 164, 165,
12280 /* 300 */ 152, 119, 120, 27, 28, 208, 99, 27, 28, 102,
12281 /* 310 */ 103, 104, 152, 50, 51, 97, 98, 89, 90, 185,
12282 /* 320 */ 113, 187, 22, 177, 174, 97, 58, 27, 28, 101,
12283 /* 330 */ 115, 245, 117, 118, 71, 72, 73, 74, 75, 76,
12284 /* 340 */ 77, 78, 79, 80, 81, 82, 83, 11, 85, 86,
12285 /* 350 */ 87, 88, 89, 90, 91, 92, 93, 94, 95, 19,
12286 /* 360 */ 132, 133, 134, 23, 152, 97, 98, 91, 174, 223,
12287 /* 370 */ 224, 225, 239, 97, 98, 187, 22, 97, 98, 27,
12288 /* 380 */ 28, 27, 28, 152, 223, 224, 225, 239, 152, 163,
12289 /* 390 */ 50, 51, 170, 171, 172, 59, 160, 97, 98, 239,
12290 /* 400 */ 164, 165, 66, 242, 124, 174, 175, 195, 22, 23,
12291 /* 410 */ 69, 71, 72, 73, 74, 75, 76, 77, 78, 79,
12292 /* 420 */ 80, 81, 82, 83, 198, 85, 86, 87, 88, 89,
12293 /* 430 */ 90, 91, 92, 93, 94, 95, 19, 12, 97, 21,
12294 /* 440 */ 23, 152, 101, 152, 108, 109, 110, 221, 152, 97,
12295 /* 450 */ 98, 97, 98, 152, 29, 243, 70, 226, 23, 233,
12296 /* 460 */ 26, 26, 152, 152, 238, 174, 175, 50, 51, 22,
12297 /* 470 */ 45, 24, 47, 132, 133, 134, 124, 22, 23, 188,
12298 /* 480 */ 163, 26, 152, 65, 59, 174, 175, 163, 71, 72,
12299 /* 490 */ 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
12300 /* 500 */ 83, 152, 85, 86, 87, 88, 89, 90, 91, 92,
12301 /* 510 */ 93, 94, 95, 19, 19, 198, 152, 23, 152, 152,
12302 /* 520 */ 209, 103, 198, 174, 175, 70, 152, 50, 51, 219,
12303 /* 530 */ 213, 214, 152, 98, 152, 171, 172, 188, 171, 172,
12304 /* 540 */ 174, 175, 248, 249, 50, 51, 51, 251, 174, 175,
12305 /* 550 */ 220, 74, 75, 152, 188, 152, 174, 175, 140, 124,
12306 /* 560 */ 26, 163, 188, 16, 130, 71, 72, 73, 74, 75,
12307 /* 570 */ 76, 77, 78, 79, 80, 81, 82, 83, 101, 85,
12308 /* 580 */ 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
12309 /* 590 */ 19, 209, 196, 197, 23, 231, 198, 12, 231, 219,
12310 /* 600 */ 37, 22, 107, 24, 208, 116, 27, 112, 201, 62,
12311 /* 610 */ 121, 64, 152, 152, 29, 52, 66, 221, 211, 221,
12312 /* 620 */ 219, 50, 51, 60, 89, 90, 152, 22, 23, 183,
12313 /* 630 */ 45, 26, 47, 22, 174, 175, 238, 152, 164, 165,
12314 /* 640 */ 106, 19, 71, 72, 73, 74, 75, 76, 77, 78,
12315 /* 650 */ 79, 80, 81, 82, 83, 152, 85, 86, 87, 88,
12316 /* 660 */ 89, 90, 91, 92, 93, 94, 95, 132, 133, 119,
12317 /* 670 */ 120, 163, 50, 51, 111, 70, 97, 174, 175, 181,
12318 /* 680 */ 182, 170, 171, 172, 0, 1, 2, 140, 190, 108,
12319 /* 690 */ 109, 110, 51, 71, 72, 73, 74, 75, 76, 77,
12320 /* 700 */ 78, 79, 80, 81, 82, 83, 198, 85, 86, 87,
12321 /* 710 */ 88, 89, 90, 91, 92, 93, 94, 95, 19, 152,
12322 /* 720 */ 152, 152, 22, 166, 152, 168, 169, 27, 19, 152,
12323 /* 730 */ 26, 19, 152, 122, 152, 24, 152, 28, 27, 152,
12324 /* 740 */ 28, 174, 175, 174, 175, 152, 174, 175, 107, 50,
12325 /* 750 */ 51, 174, 175, 22, 174, 175, 174, 175, 174, 175,
12326 /* 760 */ 138, 174, 175, 7, 8, 9, 16, 174, 175, 152,
12327 /* 770 */ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
12328 /* 780 */ 81, 82, 83, 152, 85, 86, 87, 88, 89, 90,
12329 /* 790 */ 91, 92, 93, 94, 95, 19, 152, 97, 152, 31,
12330 /* 800 */ 24, 152, 98, 35, 101, 174, 175, 152, 97, 152,
12331 /* 810 */ 79, 152, 62, 152, 64, 112, 152, 49, 174, 175,
12332 /* 820 */ 174, 175, 152, 174, 175, 152, 50, 51, 124, 174,
12333 /* 830 */ 175, 174, 175, 174, 175, 174, 175, 138, 174, 175,
12334 /* 840 */ 22, 23, 152, 152, 174, 175, 19, 71, 72, 73,
12335 /* 850 */ 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
12336 /* 860 */ 152, 85, 86, 87, 88, 89, 90, 91, 92, 93,
12337 /* 870 */ 94, 95, 152, 108, 109, 110, 152, 50, 51, 146,
12338 /* 880 */ 147, 23, 174, 175, 26, 195, 195, 152, 70, 152,
12339 /* 890 */ 168, 169, 152, 26, 174, 175, 152, 19, 71, 72,
12340 /* 900 */ 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
12341 /* 910 */ 83, 152, 85, 86, 87, 88, 89, 90, 91, 92,
12342 /* 920 */ 93, 94, 95, 152, 246, 247, 213, 214, 50, 51,
12343 /* 930 */ 195, 152, 195, 174, 175, 195, 100, 101, 152, 195,
12344 /* 940 */ 152, 152, 7, 8, 152, 174, 175, 163, 19, 71,
12345 /* 950 */ 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
12346 /* 960 */ 82, 83, 152, 85, 86, 87, 88, 89, 90, 91,
12347 /* 970 */ 92, 93, 94, 95, 152, 27, 152, 189, 189, 50,
12348 /* 980 */ 51, 195, 198, 152, 174, 175, 33, 132, 133, 152,
12349 /* 990 */ 123, 163, 163, 163, 152, 42, 174, 175, 152, 19,
12350 /* 1000 */ 152, 72, 73, 74, 75, 76, 77, 78, 79, 80,
12351 /* 1010 */ 81, 82, 83, 152, 85, 86, 87, 88, 89, 90,
12352 /* 1020 */ 91, 92, 93, 94, 95, 152, 198, 198, 198, 23,
12353 /* 1030 */ 50, 51, 26, 152, 23, 174, 175, 26, 23, 23,
12354 /* 1040 */ 23, 26, 26, 26, 152, 97, 23, 174, 175, 26,
12355 /* 1050 */ 132, 133, 152, 73, 74, 75, 76, 77, 78, 79,
12356 /* 1060 */ 80, 81, 82, 83, 152, 85, 86, 87, 88, 89,
12357 /* 1070 */ 90, 91, 92, 93, 94, 95, 19, 20, 23, 22,
12358 /* 1080 */ 23, 26, 152, 152, 27, 28, 174, 175, 152, 19,
12359 /* 1090 */ 20, 27, 22, 183, 183, 38, 152, 27, 28, 152,
12360 /* 1100 */ 23, 152, 152, 26, 174, 175, 152, 152, 38, 152,
12361 /* 1110 */ 23, 152, 27, 26, 57, 152, 215, 163, 152, 152,
12362 /* 1120 */ 152, 174, 175, 66, 174, 175, 69, 57, 174, 175,
12363 /* 1130 */ 152, 174, 175, 174, 175, 212, 66, 174, 175, 69,
12364 /* 1140 */ 174, 175, 174, 175, 152, 152, 89, 90, 152, 193,
12365 /* 1150 */ 152, 152, 198, 96, 97, 98, 91, 152, 101, 89,
12366 /* 1160 */ 90, 97, 152, 209, 210, 152, 96, 97, 98, 235,
12367 /* 1170 */ 152, 101, 174, 175, 152, 19, 20, 152, 22, 174,
12368 /* 1180 */ 175, 116, 97, 27, 28, 152, 121, 174, 175, 132,
12369 /* 1190 */ 133, 134, 135, 136, 38, 152, 174, 175, 152, 174,
12370 /* 1200 */ 175, 152, 132, 133, 134, 135, 136, 234, 152, 212,
12371 /* 1210 */ 150, 199, 212, 57, 212, 240, 240, 203, 178, 200,
12372 /* 1220 */ 216, 186, 177, 19, 20, 69, 22, 203, 177, 182,
12373 /* 1230 */ 177, 27, 28, 202, 200, 228, 216, 216, 155, 39,
12374 /* 1240 */ 122, 159, 38, 159, 41, 89, 90, 91, 159, 241,
12375 /* 1250 */ 241, 22, 96, 97, 98, 71, 130, 101, 222, 191,
12376 /* 1260 */ 18, 57, 203, 194, 159, 194, 194, 194, 18, 158,
12377 /* 1270 */ 244, 191, 222, 69, 159, 158, 137, 19, 20, 203,
12378 /* 1280 */ 22, 191, 203, 46, 236, 27, 28, 159, 132, 133,
12379 /* 1290 */ 134, 135, 136, 89, 90, 237, 38, 159, 158, 22,
12380 /* 1300 */ 96, 97, 98, 179, 159, 101, 158, 48, 159, 158,
12381 /* 1310 */ 179, 176, 107, 176, 184, 57, 106, 176, 184, 176,
12382 /* 1320 */ 125, 179, 178, 176, 218, 107, 176, 69, 176, 217,
12383 /* 1330 */ 159, 218, 218, 217, 159, 137, 132, 133, 134, 135,
12384 /* 1340 */ 136, 218, 217, 179, 217, 179, 227, 89, 90, 95,
12385 /* 1350 */ 230, 230, 129, 207, 96, 97, 98, 126, 128, 101,
12386 /* 1360 */ 5, 206, 205, 127, 204, 10, 11, 12, 13, 14,
12387 /* 1370 */ 203, 25, 17, 162, 26, 161, 13, 153, 153, 6,
12388 /* 1380 */ 247, 180, 250, 151, 151, 151, 151, 32, 180, 34,
12389 /* 1390 */ 132, 133, 134, 135, 136, 167, 4, 3, 43, 22,
12390 /* 1400 */ 15, 68, 142, 250, 16, 23, 23, 120, 111, 131,
12391 /* 1410 */ 20, 56, 123, 125, 16, 1, 123, 131, 63, 79,
12392 /* 1420 */ 79, 66, 67, 111, 28, 36, 122, 1, 5, 22,
12393 /* 1430 */ 107, 140, 54, 54, 26, 61, 107, 44, 20, 24,
12394 /* 1440 */ 19, 105, 112, 23, 22, 40, 22, 22, 53, 22,
12395 /* 1450 */ 53, 22, 53, 23, 23, 23, 22, 22, 30, 116,
12396 /* 1460 */ 23, 122, 26, 23, 23, 22, 28, 11, 124, 114,
12397 /* 1470 */ 26, 26, 23, 23, 23, 36, 24, 23, 36, 26,
12398 /* 1480 */ 22, 22, 36, 23, 122, 23, 22, 26, 22, 24,
12399 /* 1490 */ 23, 23, 23, 22, 122, 23, 141, 122, 122, 15,
12400 /* 1500 */ 1,
12401 };
12402 #define YY_SHIFT_USE_DFLT (-89)
12403 #define YY_SHIFT_COUNT (435)
12404 #define YY_SHIFT_MIN (-88)
12405 #define YY_SHIFT_MAX (1499)
12406 static const short yy_shift_ofst[] = {
12407 /* 0 */ 5, 1057, 1355, 1070, 1204, 1204, 1204, 90, 60, -19,
12408 /* 10 */ 58, 58, 186, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
12409 /* 20 */ 67, 67, 182, 336, 218, 550, 135, 263, 340, 417,
12410 /* 30 */ 494, 571, 622, 699, 776, 827, 827, 827, 827, 827,
12411 /* 40 */ 827, 827, 827, 827, 827, 827, 827, 827, 827, 827,
12412 /* 50 */ 878, 827, 929, 980, 980, 1156, 1204, 1204, 1204, 1204,
12413 /* 60 */ 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
12414 /* 70 */ 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
12415 /* 80 */ 1204, 1204, 1204, 1204, 1258, 1204, 1204, 1204, 1204, 1204,
12416 /* 90 */ 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, -71, -47,
12417 /* 100 */ -47, -47, -47, -47, -6, 88, -66, 218, 218, 418,
12418 /* 110 */ 495, 535, 535, 33, 43, 10, -30, -89, -89, -89,
12419 /* 120 */ 11, 425, 425, 268, 455, 605, 218, 218, 218, 218,
12420 /* 130 */ 218, 218, 218, 218, 218, 218, 218, 218, 218, 218,
12421 /* 140 */ 218, 218, 218, 218, 218, 684, 138, 10, 43, 125,
12422 /* 150 */ 125, 125, 125, 125, 125, -89, -89, -89, 228, 341,
12423 /* 160 */ 341, 207, 276, 300, 280, 352, 354, 218, 218, 218,
12424 /* 170 */ 218, 218, 218, 218, 218, 218, 218, 218, 218, 218,
12425 /* 180 */ 218, 218, 218, 218, 563, 563, 563, 218, 218, 435,
12426 /* 190 */ 218, 218, 218, 579, 218, 218, 585, 218, 218, 218,
12427 /* 200 */ 218, 218, 218, 218, 218, 218, 218, 581, 768, 711,
12428 /* 210 */ 711, 711, 704, 215, 1065, 756, 434, 709, 709, 712,
12429 /* 220 */ 434, 712, 534, 858, 641, 953, 709, -88, 953, 953,
12430 /* 230 */ 867, 489, 447, 1200, 1118, 1118, 1203, 1203, 1118, 1229,
12431 /* 240 */ 1184, 1126, 1242, 1242, 1242, 1242, 1118, 1250, 1126, 1229,
12432 /* 250 */ 1184, 1184, 1126, 1118, 1250, 1139, 1237, 1118, 1118, 1250,
12433 /* 260 */ 1277, 1118, 1250, 1118, 1250, 1277, 1205, 1205, 1205, 1259,
12434 /* 270 */ 1277, 1205, 1210, 1205, 1259, 1205, 1205, 1195, 1218, 1195,
12435 /* 280 */ 1218, 1195, 1218, 1195, 1218, 1118, 1118, 1198, 1277, 1254,
12436 /* 290 */ 1254, 1277, 1223, 1231, 1230, 1236, 1126, 1346, 1348, 1363,
12437 /* 300 */ 1363, 1373, 1373, 1373, 1373, -89, -89, -89, -89, -89,
12438 /* 310 */ -89, 477, 547, 386, 818, 750, 765, 700, 1006, 731,
12439 /* 320 */ 1011, 1015, 1016, 1017, 948, 836, 935, 703, 1023, 1055,
12440 /* 330 */ 1064, 1077, 855, 918, 1087, 1085, 611, 1392, 1394, 1377,
12441 /* 340 */ 1260, 1385, 1333, 1388, 1382, 1383, 1287, 1278, 1297, 1289,
12442 /* 350 */ 1390, 1288, 1398, 1414, 1293, 1286, 1340, 1341, 1312, 1396,
12443 /* 360 */ 1389, 1304, 1426, 1423, 1407, 1323, 1291, 1378, 1408, 1379,
12444 /* 370 */ 1374, 1393, 1329, 1415, 1418, 1421, 1330, 1336, 1422, 1395,
12445 /* 380 */ 1424, 1425, 1420, 1427, 1397, 1428, 1429, 1399, 1405, 1430,
12446 /* 390 */ 1431, 1432, 1343, 1434, 1437, 1435, 1436, 1339, 1440, 1441,
12447 /* 400 */ 1438, 1439, 1443, 1344, 1444, 1442, 1445, 1446, 1444, 1449,
12448 /* 410 */ 1450, 1451, 1453, 1454, 1458, 1456, 1460, 1459, 1452, 1461,
12449 /* 420 */ 1462, 1464, 1465, 1461, 1467, 1466, 1468, 1469, 1471, 1362,
12450 /* 430 */ 1372, 1375, 1376, 1472, 1484, 1499,
12451 };
12452 #define YY_REDUCE_USE_DFLT (-144)
12453 #define YY_REDUCE_COUNT (310)
12454 #define YY_REDUCE_MIN (-143)
12455 #define YY_REDUCE_MAX (1235)
12456 static const short yy_reduce_ofst[] = {
12457 /* 0 */ -143, 954, 86, 21, -50, 23, 79, 134, 226, -120,
12458 /* 10 */ -127, 146, 161, 291, 349, 366, 311, 382, 374, 231,
12459 /* 20 */ 364, 367, 396, 398, 236, 317, -103, -103, -103, -103,
12460 /* 30 */ -103, -103, -103, -103, -103, -103, -103, -103, -103, -103,
12461 /* 40 */ -103, -103, -103, -103, -103, -103, -103, -103, -103, -103,
12462 /* 50 */ -103, -103, -103, -103, -103, 460, 503, 567, 569, 572,
12463 /* 60 */ 577, 580, 582, 584, 587, 593, 631, 644, 646, 649,
12464 /* 70 */ 655, 657, 659, 661, 664, 670, 708, 720, 759, 771,
12465 /* 80 */ 810, 822, 861, 873, 912, 930, 947, 950, 957, 959,
12466 /* 90 */ 963, 966, 968, 998, 1005, 1013, 1022, 1025, -103, -103,
12467 /* 100 */ -103, -103, -103, -103, -103, -103, -103, 474, 212, 15,
12468 /* 110 */ 498, 222, 511, -103, 97, 557, -103, -103, -103, -103,
12469 /* 120 */ -80, 9, 59, 19, 294, 294, -53, -62, 690, 691,
12470 /* 130 */ 735, 737, 740, 744, 133, 310, 148, 330, 160, 380,
12471 /* 140 */ 786, 788, 401, 296, 789, 733, 85, 722, -42, 324,
12472 /* 150 */ 508, 784, 828, 829, 830, 678, 713, 407, 69, 150,
12473 /* 160 */ 194, 188, 289, 301, 403, 461, 485, 568, 617, 673,
12474 /* 170 */ 724, 779, 792, 824, 831, 837, 842, 846, 848, 881,
12475 /* 180 */ 892, 900, 931, 936, 446, 910, 911, 944, 949, 901,
12476 /* 190 */ 955, 967, 978, 923, 992, 993, 956, 996, 999, 1010,
12477 /* 200 */ 289, 1018, 1033, 1043, 1046, 1049, 1056, 934, 973, 997,
12478 /* 210 */ 1000, 1002, 901, 1012, 1019, 1060, 1014, 1004, 1020, 975,
12479 /* 220 */ 1024, 976, 1040, 1035, 1047, 1045, 1021, 1007, 1051, 1053,
12480 /* 230 */ 1031, 1034, 1083, 1026, 1082, 1084, 1008, 1009, 1089, 1036,
12481 /* 240 */ 1068, 1059, 1069, 1071, 1072, 1073, 1105, 1111, 1076, 1050,
12482 /* 250 */ 1080, 1090, 1079, 1115, 1117, 1058, 1048, 1128, 1138, 1140,
12483 /* 260 */ 1124, 1145, 1148, 1149, 1151, 1131, 1135, 1137, 1141, 1130,
12484 /* 270 */ 1142, 1143, 1144, 1147, 1134, 1150, 1152, 1106, 1112, 1113,
12485 /* 280 */ 1116, 1114, 1125, 1123, 1127, 1171, 1175, 1119, 1164, 1120,
12486 /* 290 */ 1121, 1166, 1146, 1155, 1157, 1160, 1167, 1211, 1214, 1224,
12487 /* 300 */ 1225, 1232, 1233, 1234, 1235, 1132, 1153, 1133, 1201, 1208,
12488 /* 310 */ 1228,
12489 };
12490 static const YYACTIONTYPE yy_default[] = {
12491 /* 0 */ 982, 1300, 1300, 1300, 1214, 1214, 1214, 1305, 1300, 1109,
12492 /* 10 */ 1138, 1138, 1274, 1305, 1305, 1305, 1305, 1305, 1305, 1212,
12493 /* 20 */ 1305, 1305, 1305, 1300, 1305, 1113, 1144, 1305, 1305, 1305,
12494 /* 30 */ 1305, 1305, 1305, 1305, 1305, 1273, 1275, 1152, 1151, 1254,
12495 /* 40 */ 1125, 1149, 1142, 1146, 1215, 1208, 1209, 1207, 1211, 1216,
12496 /* 50 */ 1305, 1145, 1177, 1192, 1176, 1305, 1305, 1305, 1305, 1305,
12497 /* 60 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12498 /* 70 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12499 /* 80 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12500 /* 90 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1186, 1191,
12501 /* 100 */ 1198, 1190, 1187, 1179, 1178, 1180, 1181, 1305, 1305, 1008,
12502 /* 110 */ 1074, 1305, 1305, 1182, 1305, 1020, 1183, 1195, 1194, 1193,
12503 /* 120 */ 1015, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12504 /* 130 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12505 /* 140 */ 1305, 1305, 1305, 1305, 1305, 982, 1300, 1305, 1305, 1300,
12506 /* 150 */ 1300, 1300, 1300, 1300, 1300, 1292, 1113, 1103, 1305, 1305,
12507 /* 160 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1280, 1278,
12508 /* 170 */ 1305, 1227, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12509 /* 180 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12510 /* 190 */ 1305, 1305, 1305, 1109, 1305, 1305, 1305, 1305, 1305, 1305,
12511 /* 200 */ 1305, 1305, 1305, 1305, 1305, 1305, 988, 1305, 1247, 1109,
12512 /* 210 */ 1109, 1109, 1111, 1089, 1101, 990, 1148, 1127, 1127, 1259,
12513 /* 220 */ 1148, 1259, 1045, 1068, 1042, 1138, 1127, 1210, 1138, 1138,
12514 /* 230 */ 1110, 1101, 1305, 1285, 1118, 1118, 1277, 1277, 1118, 1157,
12515 /* 240 */ 1078, 1148, 1085, 1085, 1085, 1085, 1118, 1005, 1148, 1157,
12516 /* 250 */ 1078, 1078, 1148, 1118, 1005, 1253, 1251, 1118, 1118, 1005,
12517 /* 260 */ 1220, 1118, 1005, 1118, 1005, 1220, 1076, 1076, 1076, 1060,
12518 /* 270 */ 1220, 1076, 1045, 1076, 1060, 1076, 1076, 1131, 1126, 1131,
12519 /* 280 */ 1126, 1131, 1126, 1131, 1126, 1118, 1118, 1305, 1220, 1224,
12520 /* 290 */ 1224, 1220, 1143, 1132, 1141, 1139, 1148, 1011, 1063, 998,
12521 /* 300 */ 998, 987, 987, 987, 987, 1297, 1297, 1292, 1047, 1047,
12522 /* 310 */ 1030, 1305, 1305, 1305, 1305, 1305, 1305, 1022, 1305, 1229,
12523 /* 320 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12524 /* 330 */ 1305, 1305, 1305, 1305, 1305, 1305, 1164, 1305, 983, 1287,
12525 /* 340 */ 1305, 1305, 1284, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12526 /* 350 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12527 /* 360 */ 1305, 1257, 1305, 1305, 1305, 1305, 1305, 1305, 1250, 1249,
12528 /* 370 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12529 /* 380 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305,
12530 /* 390 */ 1305, 1305, 1092, 1305, 1305, 1305, 1096, 1305, 1305, 1305,
12531 /* 400 */ 1305, 1305, 1305, 1305, 1140, 1305, 1133, 1305, 1213, 1305,
12532 /* 410 */ 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1305, 1302,
12533 /* 420 */ 1305, 1305, 1305, 1301, 1305, 1305, 1305, 1305, 1305, 1166,
12534 /* 430 */ 1305, 1165, 1169, 1305, 996, 1305,
12535 };
12536 /********** End of lemon-generated parsing tables *****************************/
12537
12538 /* The next table maps tokens (terminal symbols) into fallback tokens.
12539 ** If a construct like the following:
12540 **
12541 ** %fallback ID X Y Z.
12542 **
12543 ** appears in the grammar, then ID becomes a fallback token for X, Y,
12544 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
12545 ** but it does not parse, the type of the token is changed to ID and
12546 ** the parse is retried before an error is thrown.
12547 **
12548 ** This feature can be used, for example, to cause some keywords in a language
12549 ** to revert to identifiers if they keyword does not apply in the context where
12550 ** it appears.
12551 */
12552 #ifdef YYFALLBACK
12553 static const YYCODETYPE yyFallback[] = {
12554 0, /* $ => nothing */
12555 0, /* SEMI => nothing */
12556 27, /* EXPLAIN => ID */
12557 27, /* QUERY => ID */
12558 27, /* PLAN => ID */
12559 27, /* BEGIN => ID */
12560 0, /* TRANSACTION => nothing */
12561 27, /* DEFERRED => ID */
12562 27, /* IMMEDIATE => ID */
12563 27, /* EXCLUSIVE => ID */
12564 0, /* COMMIT => nothing */
12565 27, /* END => ID */
12566 27, /* ROLLBACK => ID */
12567 27, /* SAVEPOINT => ID */
12568 27, /* RELEASE => ID */
12569 0, /* TO => nothing */
12570 0, /* TABLE => nothing */
12571 0, /* CREATE => nothing */
12572 27, /* IF => ID */
12573 0, /* NOT => nothing */
12574 0, /* EXISTS => nothing */
12575 27, /* TEMP => ID */
12576 0, /* LP => nothing */
12577 0, /* RP => nothing */
12578 0, /* AS => nothing */
12579 27, /* WITHOUT => ID */
12580 0, /* COMMA => nothing */
12581 0, /* ID => nothing */
12582 0, /* INDEXED => nothing */
12583 27, /* ABORT => ID */
12584 27, /* ACTION => ID */
12585 27, /* AFTER => ID */
12586 27, /* ANALYZE => ID */
12587 27, /* ASC => ID */
12588 27, /* ATTACH => ID */
12589 27, /* BEFORE => ID */
12590 27, /* BY => ID */
12591 27, /* CASCADE => ID */
12592 27, /* CAST => ID */
12593 27, /* COLUMNKW => ID */
12594 27, /* CONFLICT => ID */
12595 27, /* DATABASE => ID */
12596 27, /* DESC => ID */
12597 27, /* DETACH => ID */
12598 27, /* EACH => ID */
12599 27, /* FAIL => ID */
12600 27, /* FOR => ID */
12601 27, /* IGNORE => ID */
12602 27, /* INITIALLY => ID */
12603 27, /* INSTEAD => ID */
12604 27, /* LIKE_KW => ID */
12605 27, /* MATCH => ID */
12606 27, /* NO => ID */
12607 27, /* KEY => ID */
12608 27, /* OF => ID */
12609 27, /* OFFSET => ID */
12610 27, /* PRAGMA => ID */
12611 27, /* RAISE => ID */
12612 27, /* RECURSIVE => ID */
12613 27, /* REPLACE => ID */
12614 27, /* RESTRICT => ID */
12615 27, /* ROW => ID */
12616 27, /* TRIGGER => ID */
12617 27, /* VACUUM => ID */
12618 27, /* VIEW => ID */
12619 27, /* VIRTUAL => ID */
12620 27, /* WITH => ID */
12621 27, /* REINDEX => ID */
12622 27, /* RENAME => ID */
12623 27, /* CTIME_KW => ID */
12624 };
12625 #endif /* YYFALLBACK */
12626
12627 /* The following structure represents a single element of the
12628 ** parser's stack. Information stored includes:
12629 **
12630 ** + The state number for the parser at this level of the stack.
12631 **
12632 ** + The value of the token stored at this level of the stack.
12633 ** (In other words, the "major" token.)
12634 **
12635 ** + The semantic value stored at this level of the stack. This is
12636 ** the information used by the action routines in the grammar.
12637 ** It is sometimes called the "minor" token.
12638 **
12639 ** After the "shift" half of a SHIFTREDUCE action, the stateno field
12640 ** actually contains the reduce action for the second half of the
12641 ** SHIFTREDUCE.
12642 */
12643 struct yyStackEntry {
12644 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
12645 YYCODETYPE major; /* The major token value. This is the code
12646 ** number for the token at this stack level */
12647 YYMINORTYPE minor; /* The user-supplied minor token value. This
12648 ** is the value of the token */
12649 };
12650 typedef struct yyStackEntry yyStackEntry;
12651
12652 /* The state of the parser is completely contained in an instance of
12653 ** the following structure */
12654 struct yyParser {
12655 int yyidx; /* Index of top element in stack */
12656 #ifdef YYTRACKMAXSTACKDEPTH
12657 int yyidxMax; /* Maximum value of yyidx */
12658 #endif
12659 int yyerrcnt; /* Shifts left before out of the error */
12660 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
12661 #if YYSTACKDEPTH<=0
12662 int yystksz; /* Current side of the stack */
12663 yyStackEntry *yystack; /* The parser's stack */
12664 #else
12665 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
12666 #endif
12667 };
12668 typedef struct yyParser yyParser;
12669
12670 #ifndef NDEBUG
12671 /* #include <stdio.h> */
12672 static FILE *yyTraceFILE = 0;
12673 static char *yyTracePrompt = 0;
12674 #endif /* NDEBUG */
12675
12676 #ifndef NDEBUG
12677 /*
12678 ** Turn parser tracing on by giving a stream to which to write the trace
12679 ** and a prompt to preface each trace message. Tracing is turned off
12680 ** by making either argument NULL
12681 **
12682 ** Inputs:
12683 ** <ul>
12684 ** <li> A FILE* to which trace output should be written.
12685 ** If NULL, then tracing is turned off.
12686 ** <li> A prefix string written at the beginning of every
12687 ** line of trace output. If NULL, then tracing is
12688 ** turned off.
12689 ** </ul>
12690 **
12691 ** Outputs:
12692 ** None.
12693 */
12694 SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
12695 yyTraceFILE = TraceFILE;
12696 yyTracePrompt = zTracePrompt;
12697 if( yyTraceFILE==0 ) yyTracePrompt = 0;
12698 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
12699 }
12700 #endif /* NDEBUG */
12701
12702 #ifndef NDEBUG
12703 /* For tracing shifts, the names of all terminals and nonterminals
12704 ** are required. The following table supplies these names */
12705 static const char *const yyTokenName[] = {
12706 "$", "SEMI", "EXPLAIN", "QUERY",
12707 "PLAN", "BEGIN", "TRANSACTION", "DEFERRED",
12708 "IMMEDIATE", "EXCLUSIVE", "COMMIT", "END",
12709 "ROLLBACK", "SAVEPOINT", "RELEASE", "TO",
12710 "TABLE", "CREATE", "IF", "NOT",
12711 "EXISTS", "TEMP", "LP", "RP",
12712 "AS", "WITHOUT", "COMMA", "ID",
12713 "INDEXED", "ABORT", "ACTION", "AFTER",
12714 "ANALYZE", "ASC", "ATTACH", "BEFORE",
12715 "BY", "CASCADE", "CAST", "COLUMNKW",
12716 "CONFLICT", "DATABASE", "DESC", "DETACH",
12717 "EACH", "FAIL", "FOR", "IGNORE",
12718 "INITIALLY", "INSTEAD", "LIKE_KW", "MATCH",
12719 "NO", "KEY", "OF", "OFFSET",
12720 "PRAGMA", "RAISE", "RECURSIVE", "REPLACE",
12721 "RESTRICT", "ROW", "TRIGGER", "VACUUM",
12722 "VIEW", "VIRTUAL", "WITH", "REINDEX",
12723 "RENAME", "CTIME_KW", "ANY", "OR",
12724 "AND", "IS", "BETWEEN", "IN",
12725 "ISNULL", "NOTNULL", "NE", "EQ",
12726 "GT", "LE", "LT", "GE",
12727 "ESCAPE", "BITAND", "BITOR", "LSHIFT",
12728 "RSHIFT", "PLUS", "MINUS", "STAR",
12729 "SLASH", "REM", "CONCAT", "COLLATE",
12730 "BITNOT", "STRING", "JOIN_KW", "CONSTRAINT",
12731 "DEFAULT", "NULL", "PRIMARY", "UNIQUE",
12732 "CHECK", "REFERENCES", "AUTOINCR", "ON",
12733 "INSERT", "DELETE", "UPDATE", "SET",
12734 "DEFERRABLE", "FOREIGN", "DROP", "UNION",
12735 "ALL", "EXCEPT", "INTERSECT", "SELECT",
12736 "VALUES", "DISTINCT", "DOT", "FROM",
12737 "JOIN", "USING", "ORDER", "GROUP",
12738 "HAVING", "LIMIT", "WHERE", "INTO",
12739 "INTEGER", "FLOAT", "BLOB", "VARIABLE",
12740 "CASE", "WHEN", "THEN", "ELSE",
12741 "INDEX", "ALTER", "ADD", "error",
12742 "input", "cmdlist", "ecmd", "explain",
12743 "cmdx", "cmd", "transtype", "trans_opt",
12744 "nm", "savepoint_opt", "create_table", "create_table_args",
12745 "createkw", "temp", "ifnotexists", "dbnm",
12746 "columnlist", "conslist_opt", "table_options", "select",
12747 "column", "columnid", "type", "carglist",
12748 "typetoken", "typename", "signed", "plus_num",
12749 "minus_num", "ccons", "term", "expr",
12750 "onconf", "sortorder", "autoinc", "eidlist_opt",
12751 "refargs", "defer_subclause", "refarg", "refact",
12752 "init_deferred_pred_opt", "conslist", "tconscomma", "tcons",
12753 "sortlist", "eidlist", "defer_subclause_opt", "orconf",
12754 "resolvetype", "raisetype", "ifexists", "fullname",
12755 "selectnowith", "oneselect", "with", "multiselect_op",
12756 "distinct", "selcollist", "from", "where_opt",
12757 "groupby_opt", "having_opt", "orderby_opt", "limit_opt",
12758 "values", "nexprlist", "exprlist", "sclp",
12759 "as", "seltablist", "stl_prefix", "joinop",
12760 "indexed_opt", "on_opt", "using_opt", "idlist",
12761 "setlist", "insert_cmd", "idlist_opt", "likeop",
12762 "between_op", "in_op", "case_operand", "case_exprlist",
12763 "case_else", "uniqueflag", "collate", "nmnum",
12764 "trigger_decl", "trigger_cmd_list", "trigger_time", "trigger_event",
12765 "foreach_clause", "when_clause", "trigger_cmd", "trnm",
12766 "tridxby", "database_kw_opt", "key_opt", "add_column_fullname",
12767 "kwcolumn_opt", "create_vtab", "vtabarglist", "vtabarg",
12768 "vtabargtoken", "lp", "anylist", "wqlist",
12769 };
12770 #endif /* NDEBUG */
12771
12772 #ifndef NDEBUG
12773 /* For tracing reduce actions, the names of all rules are required.
12774 */
12775 static const char *const yyRuleName[] = {
12776 /* 0 */ "input ::= cmdlist",
12777 /* 1 */ "cmdlist ::= cmdlist ecmd",
12778 /* 2 */ "cmdlist ::= ecmd",
12779 /* 3 */ "ecmd ::= SEMI",
12780 /* 4 */ "ecmd ::= explain cmdx SEMI",
12781 /* 5 */ "explain ::=",
12782 /* 6 */ "explain ::= EXPLAIN",
12783 /* 7 */ "explain ::= EXPLAIN QUERY PLAN",
12784 /* 8 */ "cmdx ::= cmd",
12785 /* 9 */ "cmd ::= BEGIN transtype trans_opt",
12786 /* 10 */ "trans_opt ::=",
12787 /* 11 */ "trans_opt ::= TRANSACTION",
12788 /* 12 */ "trans_opt ::= TRANSACTION nm",
12789 /* 13 */ "transtype ::=",
12790 /* 14 */ "transtype ::= DEFERRED",
12791 /* 15 */ "transtype ::= IMMEDIATE",
12792 /* 16 */ "transtype ::= EXCLUSIVE",
12793 /* 17 */ "cmd ::= COMMIT trans_opt",
12794 /* 18 */ "cmd ::= END trans_opt",
12795 /* 19 */ "cmd ::= ROLLBACK trans_opt",
12796 /* 20 */ "savepoint_opt ::= SAVEPOINT",
12797 /* 21 */ "savepoint_opt ::=",
12798 /* 22 */ "cmd ::= SAVEPOINT nm",
12799 /* 23 */ "cmd ::= RELEASE savepoint_opt nm",
12800 /* 24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
12801 /* 25 */ "cmd ::= create_table create_table_args",
12802 /* 26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
12803 /* 27 */ "createkw ::= CREATE",
12804 /* 28 */ "ifnotexists ::=",
12805 /* 29 */ "ifnotexists ::= IF NOT EXISTS",
12806 /* 30 */ "temp ::= TEMP",
12807 /* 31 */ "temp ::=",
12808 /* 32 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
12809 /* 33 */ "create_table_args ::= AS select",
12810 /* 34 */ "table_options ::=",
12811 /* 35 */ "table_options ::= WITHOUT nm",
12812 /* 36 */ "columnlist ::= columnlist COMMA column",
12813 /* 37 */ "columnlist ::= column",
12814 /* 38 */ "column ::= columnid type carglist",
12815 /* 39 */ "columnid ::= nm",
12816 /* 40 */ "nm ::= ID|INDEXED",
12817 /* 41 */ "nm ::= STRING",
12818 /* 42 */ "nm ::= JOIN_KW",
12819 /* 43 */ "type ::=",
12820 /* 44 */ "type ::= typetoken",
12821 /* 45 */ "typetoken ::= typename",
12822 /* 46 */ "typetoken ::= typename LP signed RP",
12823 /* 47 */ "typetoken ::= typename LP signed COMMA signed RP",
12824 /* 48 */ "typename ::= ID|STRING",
12825 /* 49 */ "typename ::= typename ID|STRING",
12826 /* 50 */ "signed ::= plus_num",
12827 /* 51 */ "signed ::= minus_num",
12828 /* 52 */ "carglist ::= carglist ccons",
12829 /* 53 */ "carglist ::=",
12830 /* 54 */ "ccons ::= CONSTRAINT nm",
12831 /* 55 */ "ccons ::= DEFAULT term",
12832 /* 56 */ "ccons ::= DEFAULT LP expr RP",
12833 /* 57 */ "ccons ::= DEFAULT PLUS term",
12834 /* 58 */ "ccons ::= DEFAULT MINUS term",
12835 /* 59 */ "ccons ::= DEFAULT ID|INDEXED",
12836 /* 60 */ "ccons ::= NULL onconf",
12837 /* 61 */ "ccons ::= NOT NULL onconf",
12838 /* 62 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
12839 /* 63 */ "ccons ::= UNIQUE onconf",
12840 /* 64 */ "ccons ::= CHECK LP expr RP",
12841 /* 65 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
12842 /* 66 */ "ccons ::= defer_subclause",
12843 /* 67 */ "ccons ::= COLLATE ID|STRING",
12844 /* 68 */ "autoinc ::=",
12845 /* 69 */ "autoinc ::= AUTOINCR",
12846 /* 70 */ "refargs ::=",
12847 /* 71 */ "refargs ::= refargs refarg",
12848 /* 72 */ "refarg ::= MATCH nm",
12849 /* 73 */ "refarg ::= ON INSERT refact",
12850 /* 74 */ "refarg ::= ON DELETE refact",
12851 /* 75 */ "refarg ::= ON UPDATE refact",
12852 /* 76 */ "refact ::= SET NULL",
12853 /* 77 */ "refact ::= SET DEFAULT",
12854 /* 78 */ "refact ::= CASCADE",
12855 /* 79 */ "refact ::= RESTRICT",
12856 /* 80 */ "refact ::= NO ACTION",
12857 /* 81 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
12858 /* 82 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
12859 /* 83 */ "init_deferred_pred_opt ::=",
12860 /* 84 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
12861 /* 85 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
12862 /* 86 */ "conslist_opt ::=",
12863 /* 87 */ "conslist_opt ::= COMMA conslist",
12864 /* 88 */ "conslist ::= conslist tconscomma tcons",
12865 /* 89 */ "conslist ::= tcons",
12866 /* 90 */ "tconscomma ::= COMMA",
12867 /* 91 */ "tconscomma ::=",
12868 /* 92 */ "tcons ::= CONSTRAINT nm",
12869 /* 93 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
12870 /* 94 */ "tcons ::= UNIQUE LP sortlist RP onconf",
12871 /* 95 */ "tcons ::= CHECK LP expr RP onconf",
12872 /* 96 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refarg s defer_subclause_opt",
12873 /* 97 */ "defer_subclause_opt ::=",
12874 /* 98 */ "defer_subclause_opt ::= defer_subclause",
12875 /* 99 */ "onconf ::=",
12876 /* 100 */ "onconf ::= ON CONFLICT resolvetype",
12877 /* 101 */ "orconf ::=",
12878 /* 102 */ "orconf ::= OR resolvetype",
12879 /* 103 */ "resolvetype ::= raisetype",
12880 /* 104 */ "resolvetype ::= IGNORE",
12881 /* 105 */ "resolvetype ::= REPLACE",
12882 /* 106 */ "cmd ::= DROP TABLE ifexists fullname",
12883 /* 107 */ "ifexists ::= IF EXISTS",
12884 /* 108 */ "ifexists ::=",
12885 /* 109 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select ",
12886 /* 110 */ "cmd ::= DROP VIEW ifexists fullname",
12887 /* 111 */ "cmd ::= select",
12888 /* 112 */ "select ::= with selectnowith",
12889 /* 113 */ "selectnowith ::= oneselect",
12890 /* 114 */ "selectnowith ::= selectnowith multiselect_op oneselect",
12891 /* 115 */ "multiselect_op ::= UNION",
12892 /* 116 */ "multiselect_op ::= UNION ALL",
12893 /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
12894 /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
12895 /* 119 */ "oneselect ::= values",
12896 /* 120 */ "values ::= VALUES LP nexprlist RP",
12897 /* 121 */ "values ::= values COMMA LP exprlist RP",
12898 /* 122 */ "distinct ::= DISTINCT",
12899 /* 123 */ "distinct ::= ALL",
12900 /* 124 */ "distinct ::=",
12901 /* 125 */ "sclp ::= selcollist COMMA",
12902 /* 126 */ "sclp ::=",
12903 /* 127 */ "selcollist ::= sclp expr as",
12904 /* 128 */ "selcollist ::= sclp STAR",
12905 /* 129 */ "selcollist ::= sclp nm DOT STAR",
12906 /* 130 */ "as ::= AS nm",
12907 /* 131 */ "as ::= ID|STRING",
12908 /* 132 */ "as ::=",
12909 /* 133 */ "from ::=",
12910 /* 134 */ "from ::= FROM seltablist",
12911 /* 135 */ "stl_prefix ::= seltablist joinop",
12912 /* 136 */ "stl_prefix ::=",
12913 /* 137 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
12914 /* 138 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt ",
12915 /* 139 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
12916 /* 140 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
12917 /* 141 */ "dbnm ::=",
12918 /* 142 */ "dbnm ::= DOT nm",
12919 /* 143 */ "fullname ::= nm dbnm",
12920 /* 144 */ "joinop ::= COMMA|JOIN",
12921 /* 145 */ "joinop ::= JOIN_KW JOIN",
12922 /* 146 */ "joinop ::= JOIN_KW nm JOIN",
12923 /* 147 */ "joinop ::= JOIN_KW nm nm JOIN",
12924 /* 148 */ "on_opt ::= ON expr",
12925 /* 149 */ "on_opt ::=",
12926 /* 150 */ "indexed_opt ::=",
12927 /* 151 */ "indexed_opt ::= INDEXED BY nm",
12928 /* 152 */ "indexed_opt ::= NOT INDEXED",
12929 /* 153 */ "using_opt ::= USING LP idlist RP",
12930 /* 154 */ "using_opt ::=",
12931 /* 155 */ "orderby_opt ::=",
12932 /* 156 */ "orderby_opt ::= ORDER BY sortlist",
12933 /* 157 */ "sortlist ::= sortlist COMMA expr sortorder",
12934 /* 158 */ "sortlist ::= expr sortorder",
12935 /* 159 */ "sortorder ::= ASC",
12936 /* 160 */ "sortorder ::= DESC",
12937 /* 161 */ "sortorder ::=",
12938 /* 162 */ "groupby_opt ::=",
12939 /* 163 */ "groupby_opt ::= GROUP BY nexprlist",
12940 /* 164 */ "having_opt ::=",
12941 /* 165 */ "having_opt ::= HAVING expr",
12942 /* 166 */ "limit_opt ::=",
12943 /* 167 */ "limit_opt ::= LIMIT expr",
12944 /* 168 */ "limit_opt ::= LIMIT expr OFFSET expr",
12945 /* 169 */ "limit_opt ::= LIMIT expr COMMA expr",
12946 /* 170 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt",
12947 /* 171 */ "where_opt ::=",
12948 /* 172 */ "where_opt ::= WHERE expr",
12949 /* 173 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_op t",
12950 /* 174 */ "setlist ::= setlist COMMA nm EQ expr",
12951 /* 175 */ "setlist ::= nm EQ expr",
12952 /* 176 */ "cmd ::= with insert_cmd INTO fullname idlist_opt select",
12953 /* 177 */ "cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALUES",
12954 /* 178 */ "insert_cmd ::= INSERT orconf",
12955 /* 179 */ "insert_cmd ::= REPLACE",
12956 /* 180 */ "idlist_opt ::=",
12957 /* 181 */ "idlist_opt ::= LP idlist RP",
12958 /* 182 */ "idlist ::= idlist COMMA nm",
12959 /* 183 */ "idlist ::= nm",
12960 /* 184 */ "expr ::= term",
12961 /* 185 */ "expr ::= LP expr RP",
12962 /* 186 */ "term ::= NULL",
12963 /* 187 */ "expr ::= ID|INDEXED",
12964 /* 188 */ "expr ::= JOIN_KW",
12965 /* 189 */ "expr ::= nm DOT nm",
12966 /* 190 */ "expr ::= nm DOT nm DOT nm",
12967 /* 191 */ "term ::= INTEGER|FLOAT|BLOB",
12968 /* 192 */ "term ::= STRING",
12969 /* 193 */ "expr ::= VARIABLE",
12970 /* 194 */ "expr ::= expr COLLATE ID|STRING",
12971 /* 195 */ "expr ::= CAST LP expr AS typetoken RP",
12972 /* 196 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
12973 /* 197 */ "expr ::= ID|INDEXED LP STAR RP",
12974 /* 198 */ "term ::= CTIME_KW",
12975 /* 199 */ "expr ::= expr AND expr",
12976 /* 200 */ "expr ::= expr OR expr",
12977 /* 201 */ "expr ::= expr LT|GT|GE|LE expr",
12978 /* 202 */ "expr ::= expr EQ|NE expr",
12979 /* 203 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
12980 /* 204 */ "expr ::= expr PLUS|MINUS expr",
12981 /* 205 */ "expr ::= expr STAR|SLASH|REM expr",
12982 /* 206 */ "expr ::= expr CONCAT expr",
12983 /* 207 */ "likeop ::= LIKE_KW|MATCH",
12984 /* 208 */ "likeop ::= NOT LIKE_KW|MATCH",
12985 /* 209 */ "expr ::= expr likeop expr",
12986 /* 210 */ "expr ::= expr likeop expr ESCAPE expr",
12987 /* 211 */ "expr ::= expr ISNULL|NOTNULL",
12988 /* 212 */ "expr ::= expr NOT NULL",
12989 /* 213 */ "expr ::= expr IS expr",
12990 /* 214 */ "expr ::= expr IS NOT expr",
12991 /* 215 */ "expr ::= NOT expr",
12992 /* 216 */ "expr ::= BITNOT expr",
12993 /* 217 */ "expr ::= MINUS expr",
12994 /* 218 */ "expr ::= PLUS expr",
12995 /* 219 */ "between_op ::= BETWEEN",
12996 /* 220 */ "between_op ::= NOT BETWEEN",
12997 /* 221 */ "expr ::= expr between_op expr AND expr",
12998 /* 222 */ "in_op ::= IN",
12999 /* 223 */ "in_op ::= NOT IN",
13000 /* 224 */ "expr ::= expr in_op LP exprlist RP",
13001 /* 225 */ "expr ::= LP select RP",
13002 /* 226 */ "expr ::= expr in_op LP select RP",
13003 /* 227 */ "expr ::= expr in_op nm dbnm",
13004 /* 228 */ "expr ::= EXISTS LP select RP",
13005 /* 229 */ "expr ::= CASE case_operand case_exprlist case_else END",
13006 /* 230 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
13007 /* 231 */ "case_exprlist ::= WHEN expr THEN expr",
13008 /* 232 */ "case_else ::= ELSE expr",
13009 /* 233 */ "case_else ::=",
13010 /* 234 */ "case_operand ::= expr",
13011 /* 235 */ "case_operand ::=",
13012 /* 236 */ "exprlist ::= nexprlist",
13013 /* 237 */ "exprlist ::=",
13014 /* 238 */ "nexprlist ::= nexprlist COMMA expr",
13015 /* 239 */ "nexprlist ::= expr",
13016 /* 240 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortl ist RP where_opt",
13017 /* 241 */ "uniqueflag ::= UNIQUE",
13018 /* 242 */ "uniqueflag ::=",
13019 /* 243 */ "eidlist_opt ::=",
13020 /* 244 */ "eidlist_opt ::= LP eidlist RP",
13021 /* 245 */ "eidlist ::= eidlist COMMA nm collate sortorder",
13022 /* 246 */ "eidlist ::= nm collate sortorder",
13023 /* 247 */ "collate ::=",
13024 /* 248 */ "collate ::= COLLATE ID|STRING",
13025 /* 249 */ "cmd ::= DROP INDEX ifexists fullname",
13026 /* 250 */ "cmd ::= VACUUM",
13027 /* 251 */ "cmd ::= VACUUM nm",
13028 /* 252 */ "cmd ::= PRAGMA nm dbnm",
13029 /* 253 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
13030 /* 254 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
13031 /* 255 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
13032 /* 256 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
13033 /* 257 */ "nmnum ::= plus_num",
13034 /* 258 */ "nmnum ::= nm",
13035 /* 259 */ "nmnum ::= ON",
13036 /* 260 */ "nmnum ::= DELETE",
13037 /* 261 */ "nmnum ::= DEFAULT",
13038 /* 262 */ "plus_num ::= PLUS INTEGER|FLOAT",
13039 /* 263 */ "plus_num ::= INTEGER|FLOAT",
13040 /* 264 */ "minus_num ::= MINUS INTEGER|FLOAT",
13041 /* 265 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
13042 /* 266 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigg er_event ON fullname foreach_clause when_clause",
13043 /* 267 */ "trigger_time ::= BEFORE",
13044 /* 268 */ "trigger_time ::= AFTER",
13045 /* 269 */ "trigger_time ::= INSTEAD OF",
13046 /* 270 */ "trigger_time ::=",
13047 /* 271 */ "trigger_event ::= DELETE|INSERT",
13048 /* 272 */ "trigger_event ::= UPDATE",
13049 /* 273 */ "trigger_event ::= UPDATE OF idlist",
13050 /* 274 */ "foreach_clause ::=",
13051 /* 275 */ "foreach_clause ::= FOR EACH ROW",
13052 /* 276 */ "when_clause ::=",
13053 /* 277 */ "when_clause ::= WHEN expr",
13054 /* 278 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
13055 /* 279 */ "trigger_cmd_list ::= trigger_cmd SEMI",
13056 /* 280 */ "trnm ::= nm",
13057 /* 281 */ "trnm ::= nm DOT nm",
13058 /* 282 */ "tridxby ::=",
13059 /* 283 */ "tridxby ::= INDEXED BY nm",
13060 /* 284 */ "tridxby ::= NOT INDEXED",
13061 /* 285 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
13062 /* 286 */ "trigger_cmd ::= insert_cmd INTO trnm idlist_opt select",
13063 /* 287 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
13064 /* 288 */ "trigger_cmd ::= select",
13065 /* 289 */ "expr ::= RAISE LP IGNORE RP",
13066 /* 290 */ "expr ::= RAISE LP raisetype COMMA nm RP",
13067 /* 291 */ "raisetype ::= ROLLBACK",
13068 /* 292 */ "raisetype ::= ABORT",
13069 /* 293 */ "raisetype ::= FAIL",
13070 /* 294 */ "cmd ::= DROP TRIGGER ifexists fullname",
13071 /* 295 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
13072 /* 296 */ "cmd ::= DETACH database_kw_opt expr",
13073 /* 297 */ "key_opt ::=",
13074 /* 298 */ "key_opt ::= KEY expr",
13075 /* 299 */ "database_kw_opt ::= DATABASE",
13076 /* 300 */ "database_kw_opt ::=",
13077 /* 301 */ "cmd ::= REINDEX",
13078 /* 302 */ "cmd ::= REINDEX nm dbnm",
13079 /* 303 */ "cmd ::= ANALYZE",
13080 /* 304 */ "cmd ::= ANALYZE nm dbnm",
13081 /* 305 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
13082 /* 306 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
13083 /* 307 */ "add_column_fullname ::= fullname",
13084 /* 308 */ "kwcolumn_opt ::=",
13085 /* 309 */ "kwcolumn_opt ::= COLUMNKW",
13086 /* 310 */ "cmd ::= create_vtab",
13087 /* 311 */ "cmd ::= create_vtab LP vtabarglist RP",
13088 /* 312 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm" ,
13089 /* 313 */ "vtabarglist ::= vtabarg",
13090 /* 314 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
13091 /* 315 */ "vtabarg ::=",
13092 /* 316 */ "vtabarg ::= vtabarg vtabargtoken",
13093 /* 317 */ "vtabargtoken ::= ANY",
13094 /* 318 */ "vtabargtoken ::= lp anylist RP",
13095 /* 319 */ "lp ::= LP",
13096 /* 320 */ "anylist ::=",
13097 /* 321 */ "anylist ::= anylist LP anylist RP",
13098 /* 322 */ "anylist ::= anylist ANY",
13099 /* 323 */ "with ::=",
13100 /* 324 */ "with ::= WITH wqlist",
13101 /* 325 */ "with ::= WITH RECURSIVE wqlist",
13102 /* 326 */ "wqlist ::= nm eidlist_opt AS LP select RP",
13103 /* 327 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
13104 };
13105 #endif /* NDEBUG */
13106
13107
13108 #if YYSTACKDEPTH<=0
13109 /*
13110 ** Try to increase the size of the parser stack.
13111 */
13112 static void yyGrowStack(yyParser *p){
13113 int newSize;
13114 yyStackEntry *pNew;
13115
13116 newSize = p->yystksz*2 + 100;
13117 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
13118 if( pNew ){
13119 p->yystack = pNew;
13120 p->yystksz = newSize;
13121 #ifndef NDEBUG
13122 if( yyTraceFILE ){
13123 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
13124 yyTracePrompt, p->yystksz);
13125 }
13126 #endif
13127 }
13128 }
13129 #endif
13130
13131 /* Datatype of the argument to the memory allocated passed as the
13132 ** second argument to sqlite3ParserAlloc() below. This can be changed by
13133 ** putting an appropriate #define in the %include section of the input
13134 ** grammar.
13135 */
13136 #ifndef YYMALLOCARGTYPE
13137 # define YYMALLOCARGTYPE size_t
13138 #endif
13139
13140 /*
13141 ** This function allocates a new parser.
13142 ** The only argument is a pointer to a function which works like
13143 ** malloc.
13144 **
13145 ** Inputs:
13146 ** A pointer to the function used to allocate memory.
13147 **
13148 ** Outputs:
13149 ** A pointer to a parser. This pointer is used in subsequent calls
13150 ** to sqlite3Parser and sqlite3ParserFree.
13151 */
13152 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){
13153 yyParser *pParser;
13154 pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
13155 if( pParser ){
13156 pParser->yyidx = -1;
13157 #ifdef YYTRACKMAXSTACKDEPTH
13158 pParser->yyidxMax = 0;
13159 #endif
13160 #if YYSTACKDEPTH<=0
13161 pParser->yystack = NULL;
13162 pParser->yystksz = 0;
13163 yyGrowStack(pParser);
13164 #endif
13165 }
13166 return pParser;
13167 }
13168
13169 /* The following function deletes the "minor type" or semantic value
13170 ** associated with a symbol. The symbol can be either a terminal
13171 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
13172 ** a pointer to the value to be deleted. The code used to do the
13173 ** deletions is derived from the %destructor and/or %token_destructor
13174 ** directives of the input grammar.
13175 */
13176 static void yy_destructor(
13177 yyParser *yypParser, /* The parser */
13178 YYCODETYPE yymajor, /* Type code for object to destroy */
13179 YYMINORTYPE *yypminor /* The object to be destroyed */
13180 ){
13181 sqlite3ParserARG_FETCH;
13182 switch( yymajor ){
13183 /* Here is inserted the actions which take place when a
13184 ** terminal or non-terminal is destroyed. This can happen
13185 ** when the symbol is popped from the stack during a
13186 ** reduce or during error processing or when a parser is
13187 ** being destroyed before it is finished parsing.
13188 **
13189 ** Note: during a reduce, the only symbols destroyed are those
13190 ** which appear on the RHS of the rule, but which are *not* used
13191 ** inside the C code.
13192 */
13193 /********* Begin destructor definitions ***************************************/
13194 case 163: /* select */
13195 case 196: /* selectnowith */
13196 case 197: /* oneselect */
13197 case 208: /* values */
13198 {
13199 sqlite3SelectDelete(pParse->db, (yypminor->yy387));
13200 }
13201 break;
13202 case 174: /* term */
13203 case 175: /* expr */
13204 {
13205 sqlite3ExprDelete(pParse->db, (yypminor->yy118).pExpr);
13206 }
13207 break;
13208 case 179: /* eidlist_opt */
13209 case 188: /* sortlist */
13210 case 189: /* eidlist */
13211 case 201: /* selcollist */
13212 case 204: /* groupby_opt */
13213 case 206: /* orderby_opt */
13214 case 209: /* nexprlist */
13215 case 210: /* exprlist */
13216 case 211: /* sclp */
13217 case 220: /* setlist */
13218 case 227: /* case_exprlist */
13219 {
13220 sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
13221 }
13222 break;
13223 case 195: /* fullname */
13224 case 202: /* from */
13225 case 213: /* seltablist */
13226 case 214: /* stl_prefix */
13227 {
13228 sqlite3SrcListDelete(pParse->db, (yypminor->yy259));
13229 }
13230 break;
13231 case 198: /* with */
13232 case 251: /* wqlist */
13233 {
13234 sqlite3WithDelete(pParse->db, (yypminor->yy451));
13235 }
13236 break;
13237 case 203: /* where_opt */
13238 case 205: /* having_opt */
13239 case 217: /* on_opt */
13240 case 226: /* case_operand */
13241 case 228: /* case_else */
13242 case 237: /* when_clause */
13243 case 242: /* key_opt */
13244 {
13245 sqlite3ExprDelete(pParse->db, (yypminor->yy314));
13246 }
13247 break;
13248 case 218: /* using_opt */
13249 case 219: /* idlist */
13250 case 222: /* idlist_opt */
13251 {
13252 sqlite3IdListDelete(pParse->db, (yypminor->yy384));
13253 }
13254 break;
13255 case 233: /* trigger_cmd_list */
13256 case 238: /* trigger_cmd */
13257 {
13258 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy203));
13259 }
13260 break;
13261 case 235: /* trigger_event */
13262 {
13263 sqlite3IdListDelete(pParse->db, (yypminor->yy90).b);
13264 }
13265 break;
13266 /********* End destructor definitions *****************************************/
13267 default: break; /* If no destructor action specified: do nothing */
13268 }
13269 }
13270
13271 /*
13272 ** Pop the parser's stack once.
13273 **
13274 ** If there is a destructor routine associated with the token which
13275 ** is popped from the stack, then call it.
13276 */
13277 static void yy_pop_parser_stack(yyParser *pParser){
13278 yyStackEntry *yytos;
13279 assert( pParser->yyidx>=0 );
13280 yytos = &pParser->yystack[pParser->yyidx--];
13281 #ifndef NDEBUG
13282 if( yyTraceFILE ){
13283 fprintf(yyTraceFILE,"%sPopping %s\n",
13284 yyTracePrompt,
13285 yyTokenName[yytos->major]);
13286 }
13287 #endif
13288 yy_destructor(pParser, yytos->major, &yytos->minor);
13289 }
13290
13291 /*
13292 ** Deallocate and destroy a parser. Destructors are called for
13293 ** all stack elements before shutting the parser down.
13294 **
13295 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
13296 ** is defined in a %include section of the input grammar) then it is
13297 ** assumed that the input pointer is never NULL.
13298 */
13299 SQLITE_PRIVATE void sqlite3ParserFree(
13300 void *p, /* The parser to be deleted */
13301 void (*freeProc)(void*) /* Function used to reclaim memory */
13302 ){
13303 yyParser *pParser = (yyParser*)p;
13304 #ifndef YYPARSEFREENEVERNULL
13305 if( pParser==0 ) return;
13306 #endif
13307 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
13308 #if YYSTACKDEPTH<=0
13309 free(pParser->yystack);
13310 #endif
13311 (*freeProc)((void*)pParser);
13312 }
13313
13314 /*
13315 ** Return the peak depth of the stack for a parser.
13316 */
13317 #ifdef YYTRACKMAXSTACKDEPTH
13318 SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
13319 yyParser *pParser = (yyParser*)p;
13320 return pParser->yyidxMax;
13321 }
13322 #endif
13323
13324 /*
13325 ** Find the appropriate action for a parser given the terminal
13326 ** look-ahead token iLookAhead.
13327 */
13328 static int yy_find_shift_action(
13329 yyParser *pParser, /* The parser */
13330 YYCODETYPE iLookAhead /* The look-ahead token */
13331 ){
13332 int i;
13333 int stateno = pParser->yystack[pParser->yyidx].stateno;
13334
13335 if( stateno>=YY_MIN_REDUCE ) return stateno;
13336 assert( stateno <= YY_SHIFT_COUNT );
13337 do{
13338 i = yy_shift_ofst[stateno];
13339 if( i==YY_SHIFT_USE_DFLT ) return yy_default[stateno];
13340 assert( iLookAhead!=YYNOCODE );
13341 i += iLookAhead;
13342 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
13343 if( iLookAhead>0 ){
13344 #ifdef YYFALLBACK
13345 YYCODETYPE iFallback; /* Fallback token */
13346 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
13347 && (iFallback = yyFallback[iLookAhead])!=0 ){
13348 #ifndef NDEBUG
13349 if( yyTraceFILE ){
13350 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
13351 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
13352 }
13353 #endif
13354 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
13355 iLookAhead = iFallback;
13356 continue;
13357 }
13358 #endif
13359 #ifdef YYWILDCARD
13360 {
13361 int j = i - iLookAhead + YYWILDCARD;
13362 if(
13363 #if YY_SHIFT_MIN+YYWILDCARD<0
13364 j>=0 &&
13365 #endif
13366 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
13367 j<YY_ACTTAB_COUNT &&
13368 #endif
13369 yy_lookahead[j]==YYWILDCARD
13370 ){
13371 #ifndef NDEBUG
13372 if( yyTraceFILE ){
13373 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
13374 yyTracePrompt, yyTokenName[iLookAhead],
13375 yyTokenName[YYWILDCARD]);
13376 }
13377 #endif /* NDEBUG */
13378 return yy_action[j];
13379 }
13380 }
13381 #endif /* YYWILDCARD */
13382 }
13383 return yy_default[stateno];
13384 }else{
13385 return yy_action[i];
13386 }
13387 }while(1);
13388 }
13389
13390 /*
13391 ** Find the appropriate action for a parser given the non-terminal
13392 ** look-ahead token iLookAhead.
13393 */
13394 static int yy_find_reduce_action(
13395 int stateno, /* Current state number */
13396 YYCODETYPE iLookAhead /* The look-ahead token */
13397 ){
13398 int i;
13399 #ifdef YYERRORSYMBOL
13400 if( stateno>YY_REDUCE_COUNT ){
13401 return yy_default[stateno];
13402 }
13403 #else
13404 assert( stateno<=YY_REDUCE_COUNT );
13405 #endif
13406 i = yy_reduce_ofst[stateno];
13407 assert( i!=YY_REDUCE_USE_DFLT );
13408 assert( iLookAhead!=YYNOCODE );
13409 i += iLookAhead;
13410 #ifdef YYERRORSYMBOL
13411 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
13412 return yy_default[stateno];
13413 }
13414 #else
13415 assert( i>=0 && i<YY_ACTTAB_COUNT );
13416 assert( yy_lookahead[i]==iLookAhead );
13417 #endif
13418 return yy_action[i];
13419 }
13420
13421 /*
13422 ** The following routine is called if the stack overflows.
13423 */
13424 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
13425 sqlite3ParserARG_FETCH;
13426 yypParser->yyidx--;
13427 #ifndef NDEBUG
13428 if( yyTraceFILE ){
13429 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
13430 }
13431 #endif
13432 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
13433 /* Here code is inserted which will execute if the parser
13434 ** stack every overflows */
13435 /******** Begin %stack_overflow code ******************************************/
13436
13437 UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
13438 sqlite3ErrorMsg(pParse, "parser stack overflow");
13439 /******** End %stack_overflow code ********************************************/
13440 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
13441 }
13442
13443 /*
13444 ** Print tracing information for a SHIFT action
13445 */
13446 #ifndef NDEBUG
13447 static void yyTraceShift(yyParser *yypParser, int yyNewState){
13448 if( yyTraceFILE ){
13449 if( yyNewState<YYNSTATE ){
13450 fprintf(yyTraceFILE,"%sShift '%s', go to state %d\n",
13451 yyTracePrompt,yyTokenName[yypParser->yystack[yypParser->yyidx].major],
13452 yyNewState);
13453 }else{
13454 fprintf(yyTraceFILE,"%sShift '%s'\n",
13455 yyTracePrompt,yyTokenName[yypParser->yystack[yypParser->yyidx].major]);
13456 }
13457 }
13458 }
13459 #else
13460 # define yyTraceShift(X,Y)
13461 #endif
13462
13463 /*
13464 ** Perform a shift action.
13465 */
13466 static void yy_shift(
13467 yyParser *yypParser, /* The parser to be shifted */
13468 int yyNewState, /* The new state to shift in */
13469 int yyMajor, /* The major token to shift in */
13470 YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */
13471 ){
13472 yyStackEntry *yytos;
13473 yypParser->yyidx++;
13474 #ifdef YYTRACKMAXSTACKDEPTH
13475 if( yypParser->yyidx>yypParser->yyidxMax ){
13476 yypParser->yyidxMax = yypParser->yyidx;
13477 }
13478 #endif
13479 #if YYSTACKDEPTH>0
13480 if( yypParser->yyidx>=YYSTACKDEPTH ){
13481 yyStackOverflow(yypParser, yypMinor);
13482 return;
13483 }
13484 #else
13485 if( yypParser->yyidx>=yypParser->yystksz ){
13486 yyGrowStack(yypParser);
13487 if( yypParser->yyidx>=yypParser->yystksz ){
13488 yyStackOverflow(yypParser, yypMinor);
13489 return;
13490 }
13491 }
13492 #endif
13493 yytos = &yypParser->yystack[yypParser->yyidx];
13494 yytos->stateno = (YYACTIONTYPE)yyNewState;
13495 yytos->major = (YYCODETYPE)yyMajor;
13496 yytos->minor = *yypMinor;
13497 yyTraceShift(yypParser, yyNewState);
13498 }
13499
13500 /* The following table contains information about every rule that
13501 ** is used during the reduce.
13502 */
13503 static const struct {
13504 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
13505 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
13506 } yyRuleInfo[] = {
13507 { 144, 1 },
13508 { 145, 2 },
13509 { 145, 1 },
13510 { 146, 1 },
13511 { 146, 3 },
13512 { 147, 0 },
13513 { 147, 1 },
13514 { 147, 3 },
13515 { 148, 1 },
13516 { 149, 3 },
13517 { 151, 0 },
13518 { 151, 1 },
13519 { 151, 2 },
13520 { 150, 0 },
13521 { 150, 1 },
13522 { 150, 1 },
13523 { 150, 1 },
13524 { 149, 2 },
13525 { 149, 2 },
13526 { 149, 2 },
13527 { 153, 1 },
13528 { 153, 0 },
13529 { 149, 2 },
13530 { 149, 3 },
13531 { 149, 5 },
13532 { 149, 2 },
13533 { 154, 6 },
13534 { 156, 1 },
13535 { 158, 0 },
13536 { 158, 3 },
13537 { 157, 1 },
13538 { 157, 0 },
13539 { 155, 5 },
13540 { 155, 2 },
13541 { 162, 0 },
13542 { 162, 2 },
13543 { 160, 3 },
13544 { 160, 1 },
13545 { 164, 3 },
13546 { 165, 1 },
13547 { 152, 1 },
13548 { 152, 1 },
13549 { 152, 1 },
13550 { 166, 0 },
13551 { 166, 1 },
13552 { 168, 1 },
13553 { 168, 4 },
13554 { 168, 6 },
13555 { 169, 1 },
13556 { 169, 2 },
13557 { 170, 1 },
13558 { 170, 1 },
13559 { 167, 2 },
13560 { 167, 0 },
13561 { 173, 2 },
13562 { 173, 2 },
13563 { 173, 4 },
13564 { 173, 3 },
13565 { 173, 3 },
13566 { 173, 2 },
13567 { 173, 2 },
13568 { 173, 3 },
13569 { 173, 5 },
13570 { 173, 2 },
13571 { 173, 4 },
13572 { 173, 4 },
13573 { 173, 1 },
13574 { 173, 2 },
13575 { 178, 0 },
13576 { 178, 1 },
13577 { 180, 0 },
13578 { 180, 2 },
13579 { 182, 2 },
13580 { 182, 3 },
13581 { 182, 3 },
13582 { 182, 3 },
13583 { 183, 2 },
13584 { 183, 2 },
13585 { 183, 1 },
13586 { 183, 1 },
13587 { 183, 2 },
13588 { 181, 3 },
13589 { 181, 2 },
13590 { 184, 0 },
13591 { 184, 2 },
13592 { 184, 2 },
13593 { 161, 0 },
13594 { 161, 2 },
13595 { 185, 3 },
13596 { 185, 1 },
13597 { 186, 1 },
13598 { 186, 0 },
13599 { 187, 2 },
13600 { 187, 7 },
13601 { 187, 5 },
13602 { 187, 5 },
13603 { 187, 10 },
13604 { 190, 0 },
13605 { 190, 1 },
13606 { 176, 0 },
13607 { 176, 3 },
13608 { 191, 0 },
13609 { 191, 2 },
13610 { 192, 1 },
13611 { 192, 1 },
13612 { 192, 1 },
13613 { 149, 4 },
13614 { 194, 2 },
13615 { 194, 0 },
13616 { 149, 9 },
13617 { 149, 4 },
13618 { 149, 1 },
13619 { 163, 2 },
13620 { 196, 1 },
13621 { 196, 3 },
13622 { 199, 1 },
13623 { 199, 2 },
13624 { 199, 1 },
13625 { 197, 9 },
13626 { 197, 1 },
13627 { 208, 4 },
13628 { 208, 5 },
13629 { 200, 1 },
13630 { 200, 1 },
13631 { 200, 0 },
13632 { 211, 2 },
13633 { 211, 0 },
13634 { 201, 3 },
13635 { 201, 2 },
13636 { 201, 4 },
13637 { 212, 2 },
13638 { 212, 1 },
13639 { 212, 0 },
13640 { 202, 0 },
13641 { 202, 2 },
13642 { 214, 2 },
13643 { 214, 0 },
13644 { 213, 7 },
13645 { 213, 9 },
13646 { 213, 7 },
13647 { 213, 7 },
13648 { 159, 0 },
13649 { 159, 2 },
13650 { 195, 2 },
13651 { 215, 1 },
13652 { 215, 2 },
13653 { 215, 3 },
13654 { 215, 4 },
13655 { 217, 2 },
13656 { 217, 0 },
13657 { 216, 0 },
13658 { 216, 3 },
13659 { 216, 2 },
13660 { 218, 4 },
13661 { 218, 0 },
13662 { 206, 0 },
13663 { 206, 3 },
13664 { 188, 4 },
13665 { 188, 2 },
13666 { 177, 1 },
13667 { 177, 1 },
13668 { 177, 0 },
13669 { 204, 0 },
13670 { 204, 3 },
13671 { 205, 0 },
13672 { 205, 2 },
13673 { 207, 0 },
13674 { 207, 2 },
13675 { 207, 4 },
13676 { 207, 4 },
13677 { 149, 6 },
13678 { 203, 0 },
13679 { 203, 2 },
13680 { 149, 8 },
13681 { 220, 5 },
13682 { 220, 3 },
13683 { 149, 6 },
13684 { 149, 7 },
13685 { 221, 2 },
13686 { 221, 1 },
13687 { 222, 0 },
13688 { 222, 3 },
13689 { 219, 3 },
13690 { 219, 1 },
13691 { 175, 1 },
13692 { 175, 3 },
13693 { 174, 1 },
13694 { 175, 1 },
13695 { 175, 1 },
13696 { 175, 3 },
13697 { 175, 5 },
13698 { 174, 1 },
13699 { 174, 1 },
13700 { 175, 1 },
13701 { 175, 3 },
13702 { 175, 6 },
13703 { 175, 5 },
13704 { 175, 4 },
13705 { 174, 1 },
13706 { 175, 3 },
13707 { 175, 3 },
13708 { 175, 3 },
13709 { 175, 3 },
13710 { 175, 3 },
13711 { 175, 3 },
13712 { 175, 3 },
13713 { 175, 3 },
13714 { 223, 1 },
13715 { 223, 2 },
13716 { 175, 3 },
13717 { 175, 5 },
13718 { 175, 2 },
13719 { 175, 3 },
13720 { 175, 3 },
13721 { 175, 4 },
13722 { 175, 2 },
13723 { 175, 2 },
13724 { 175, 2 },
13725 { 175, 2 },
13726 { 224, 1 },
13727 { 224, 2 },
13728 { 175, 5 },
13729 { 225, 1 },
13730 { 225, 2 },
13731 { 175, 5 },
13732 { 175, 3 },
13733 { 175, 5 },
13734 { 175, 4 },
13735 { 175, 4 },
13736 { 175, 5 },
13737 { 227, 5 },
13738 { 227, 4 },
13739 { 228, 2 },
13740 { 228, 0 },
13741 { 226, 1 },
13742 { 226, 0 },
13743 { 210, 1 },
13744 { 210, 0 },
13745 { 209, 3 },
13746 { 209, 1 },
13747 { 149, 12 },
13748 { 229, 1 },
13749 { 229, 0 },
13750 { 179, 0 },
13751 { 179, 3 },
13752 { 189, 5 },
13753 { 189, 3 },
13754 { 230, 0 },
13755 { 230, 2 },
13756 { 149, 4 },
13757 { 149, 1 },
13758 { 149, 2 },
13759 { 149, 3 },
13760 { 149, 5 },
13761 { 149, 6 },
13762 { 149, 5 },
13763 { 149, 6 },
13764 { 231, 1 },
13765 { 231, 1 },
13766 { 231, 1 },
13767 { 231, 1 },
13768 { 231, 1 },
13769 { 171, 2 },
13770 { 171, 1 },
13771 { 172, 2 },
13772 { 149, 5 },
13773 { 232, 11 },
13774 { 234, 1 },
13775 { 234, 1 },
13776 { 234, 2 },
13777 { 234, 0 },
13778 { 235, 1 },
13779 { 235, 1 },
13780 { 235, 3 },
13781 { 236, 0 },
13782 { 236, 3 },
13783 { 237, 0 },
13784 { 237, 2 },
13785 { 233, 3 },
13786 { 233, 2 },
13787 { 239, 1 },
13788 { 239, 3 },
13789 { 240, 0 },
13790 { 240, 3 },
13791 { 240, 2 },
13792 { 238, 7 },
13793 { 238, 5 },
13794 { 238, 5 },
13795 { 238, 1 },
13796 { 175, 4 },
13797 { 175, 6 },
13798 { 193, 1 },
13799 { 193, 1 },
13800 { 193, 1 },
13801 { 149, 4 },
13802 { 149, 6 },
13803 { 149, 3 },
13804 { 242, 0 },
13805 { 242, 2 },
13806 { 241, 1 },
13807 { 241, 0 },
13808 { 149, 1 },
13809 { 149, 3 },
13810 { 149, 1 },
13811 { 149, 3 },
13812 { 149, 6 },
13813 { 149, 6 },
13814 { 243, 1 },
13815 { 244, 0 },
13816 { 244, 1 },
13817 { 149, 1 },
13818 { 149, 4 },
13819 { 245, 8 },
13820 { 246, 1 },
13821 { 246, 3 },
13822 { 247, 0 },
13823 { 247, 2 },
13824 { 248, 1 },
13825 { 248, 3 },
13826 { 249, 1 },
13827 { 250, 0 },
13828 { 250, 4 },
13829 { 250, 2 },
13830 { 198, 0 },
13831 { 198, 2 },
13832 { 198, 3 },
13833 { 251, 6 },
13834 { 251, 8 },
13835 };
13836
13837 static void yy_accept(yyParser*); /* Forward Declaration */
13838
13839 /*
13840 ** Perform a reduce action and the shift that must immediately
13841 ** follow the reduce.
13842 */
13843 static void yy_reduce(
13844 yyParser *yypParser, /* The parser */
13845 int yyruleno /* Number of the rule by which to reduce */
13846 ){
13847 int yygoto; /* The next state */
13848 int yyact; /* The next action */
13849 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
13850 yyStackEntry *yymsp; /* The top of the parser's stack */
13851 int yysize; /* Amount to pop the stack */
13852 sqlite3ParserARG_FETCH;
13853 yymsp = &yypParser->yystack[yypParser->yyidx];
13854 #ifndef NDEBUG
13855 if( yyTraceFILE && yyruleno>=0
13856 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
13857 yysize = yyRuleInfo[yyruleno].nrhs;
13858 fprintf(yyTraceFILE, "%sReduce [%s], go to state %d.\n", yyTracePrompt,
13859 yyRuleName[yyruleno], yymsp[-yysize].stateno);
13860 }
13861 #endif /* NDEBUG */
13862 yygotominor = yyzerominor;
13863
13864 switch( yyruleno ){
13865 /* Beginning here are the reduction cases. A typical example
13866 ** follows:
13867 ** case 0:
13868 ** #line <lineno> <grammarfile>
13869 ** { ... } // User supplied code
13870 ** #line <lineno> <thisfile>
13871 ** break;
13872 */
13873 /********** Begin reduce actions **********************************************/
13874 case 5: /* explain ::= */
13875 { sqlite3BeginParse(pParse, 0); }
13876 break;
13877 case 6: /* explain ::= EXPLAIN */
13878 { sqlite3BeginParse(pParse, 1); }
13879 break;
13880 case 7: /* explain ::= EXPLAIN QUERY PLAN */
13881 { sqlite3BeginParse(pParse, 2); }
13882 break;
13883 case 8: /* cmdx ::= cmd */
13884 { sqlite3FinishCoding(pParse); }
13885 break;
13886 case 9: /* cmd ::= BEGIN transtype trans_opt */
13887 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy4);}
13888 break;
13889 case 13: /* transtype ::= */
13890 {yygotominor.yy4 = TK_DEFERRED;}
13891 break;
13892 case 14: /* transtype ::= DEFERRED */
13893 case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
13894 case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
13895 case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
13896 case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==1 17);
13897 {yygotominor.yy4 = yymsp[0].major;}
13898 break;
13899 case 17: /* cmd ::= COMMIT trans_opt */
13900 case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
13901 {sqlite3CommitTransaction(pParse);}
13902 break;
13903 case 19: /* cmd ::= ROLLBACK trans_opt */
13904 {sqlite3RollbackTransaction(pParse);}
13905 break;
13906 case 22: /* cmd ::= SAVEPOINT nm */
13907 {
13908 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
13909 }
13910 break;
13911 case 23: /* cmd ::= RELEASE savepoint_opt nm */
13912 {
13913 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
13914 }
13915 break;
13916 case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
13917 {
13918 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
13919 }
13920 break;
13921 case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
13922 {
13923 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].m inor.yy4,0,0,yymsp[-2].minor.yy4);
13924 }
13925 break;
13926 case 27: /* createkw ::= CREATE */
13927 {
13928 pParse->db->lookaside.bEnabled = 0;
13929 yygotominor.yy0 = yymsp[0].minor.yy0;
13930 }
13931 break;
13932 case 28: /* ifnotexists ::= */
13933 case 31: /* temp ::= */ yytestcase(yyruleno==31);
13934 case 34: /* table_options ::= */ yytestcase(yyruleno==34);
13935 case 68: /* autoinc ::= */ yytestcase(yyruleno==68);
13936 case 81: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ y ytestcase(yyruleno==81);
13937 case 83: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==83);
13938 case 85: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(y yruleno==85);
13939 case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
13940 case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
13941 case 124: /* distinct ::= */ yytestcase(yyruleno==124);
13942 case 219: /* between_op ::= BETWEEN */ yytestcase(yyruleno==219);
13943 case 222: /* in_op ::= IN */ yytestcase(yyruleno==222);
13944 case 247: /* collate ::= */ yytestcase(yyruleno==247);
13945 {yygotominor.yy4 = 0;}
13946 break;
13947 case 29: /* ifnotexists ::= IF NOT EXISTS */
13948 case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
13949 case 69: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==69);
13950 case 84: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yy ruleno==84);
13951 case 107: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==107);
13952 case 220: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==220);
13953 case 223: /* in_op ::= NOT IN */ yytestcase(yyruleno==223);
13954 case 248: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==248);
13955 {yygotominor.yy4 = 1;}
13956 break;
13957 case 32: /* create_table_args ::= LP columnlist conslist_opt RP table_opti ons */
13958 {
13959 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].mino r.yy4,0);
13960 }
13961 break;
13962 case 33: /* create_table_args ::= AS select */
13963 {
13964 sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy387);
13965 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
13966 }
13967 break;
13968 case 35: /* table_options ::= WITHOUT nm */
13969 {
13970 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5 )==0 ){
13971 yygotominor.yy4 = TF_WithoutRowid | TF_NoVisibleRowid;
13972 }else{
13973 yygotominor.yy4 = 0;
13974 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
13975 }
13976 }
13977 break;
13978 case 38: /* column ::= columnid type carglist */
13979 {
13980 yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
13981 yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse ->sLastToken.n;
13982 }
13983 break;
13984 case 39: /* columnid ::= nm */
13985 {
13986 sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
13987 yygotominor.yy0 = yymsp[0].minor.yy0;
13988 pParse->constraintName.n = 0;
13989 }
13990 break;
13991 case 40: /* nm ::= ID|INDEXED */
13992 case 41: /* nm ::= STRING */ yytestcase(yyruleno==41);
13993 case 42: /* nm ::= JOIN_KW */ yytestcase(yyruleno==42);
13994 case 45: /* typetoken ::= typename */ yytestcase(yyruleno==45);
13995 case 48: /* typename ::= ID|STRING */ yytestcase(yyruleno==48);
13996 case 130: /* as ::= AS nm */ yytestcase(yyruleno==130);
13997 case 131: /* as ::= ID|STRING */ yytestcase(yyruleno==131);
13998 case 142: /* dbnm ::= DOT nm */ yytestcase(yyruleno==142);
13999 case 151: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==151);
14000 case 257: /* nmnum ::= plus_num */ yytestcase(yyruleno==257);
14001 case 258: /* nmnum ::= nm */ yytestcase(yyruleno==258);
14002 case 259: /* nmnum ::= ON */ yytestcase(yyruleno==259);
14003 case 260: /* nmnum ::= DELETE */ yytestcase(yyruleno==260);
14004 case 261: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==261);
14005 case 262: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==262);
14006 case 263: /* plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==263);
14007 case 264: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==264 );
14008 case 280: /* trnm ::= nm */ yytestcase(yyruleno==280);
14009 {yygotominor.yy0 = yymsp[0].minor.yy0;}
14010 break;
14011 case 44: /* type ::= typetoken */
14012 {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
14013 break;
14014 case 46: /* typetoken ::= typename LP signed RP */
14015 {
14016 yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
14017 yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[ -3].minor.yy0.z);
14018 }
14019 break;
14020 case 47: /* typetoken ::= typename LP signed COMMA signed RP */
14021 {
14022 yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
14023 yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[ -5].minor.yy0.z);
14024 }
14025 break;
14026 case 49: /* typename ::= typename ID|STRING */
14027 {yygotominor.yy0.z=yymsp[-1].minor.yy0.z; yygotominor.yy0.n=yymsp[0].minor.yy0.n +(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
14028 break;
14029 case 54: /* ccons ::= CONSTRAINT nm */
14030 case 92: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==92);
14031 {pParse->constraintName = yymsp[0].minor.yy0;}
14032 break;
14033 case 55: /* ccons ::= DEFAULT term */
14034 case 57: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==57);
14035 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy118);}
14036 break;
14037 case 56: /* ccons ::= DEFAULT LP expr RP */
14038 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy118);}
14039 break;
14040 case 58: /* ccons ::= DEFAULT MINUS term */
14041 {
14042 ExprSpan v;
14043 v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy118.pExpr, 0, 0);
14044 v.zStart = yymsp[-1].minor.yy0.z;
14045 v.zEnd = yymsp[0].minor.yy118.zEnd;
14046 sqlite3AddDefaultValue(pParse,&v);
14047 }
14048 break;
14049 case 59: /* ccons ::= DEFAULT ID|INDEXED */
14050 {
14051 ExprSpan v;
14052 spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
14053 sqlite3AddDefaultValue(pParse,&v);
14054 }
14055 break;
14056 case 61: /* ccons ::= NOT NULL onconf */
14057 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
14058 break;
14059 case 62: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
14060 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy4,yymsp[0].minor.yy4,yymsp[-2]. minor.yy4);}
14061 break;
14062 case 63: /* ccons ::= UNIQUE onconf */
14063 {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy4,0,0,0,0);}
14064 break;
14065 case 64: /* ccons ::= CHECK LP expr RP */
14066 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy118.pExpr);}
14067 break;
14068 case 65: /* ccons ::= REFERENCES nm eidlist_opt refargs */
14069 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yym sp[0].minor.yy4);}
14070 break;
14071 case 66: /* ccons ::= defer_subclause */
14072 {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy4);}
14073 break;
14074 case 67: /* ccons ::= COLLATE ID|STRING */
14075 {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
14076 break;
14077 case 70: /* refargs ::= */
14078 { yygotominor.yy4 = OE_None*0x0101; /* EV: R-19803-45884 */}
14079 break;
14080 case 71: /* refargs ::= refargs refarg */
14081 { yygotominor.yy4 = (yymsp[-1].minor.yy4 & ~yymsp[0].minor.yy215.mask) | yymsp[0 ].minor.yy215.value; }
14082 break;
14083 case 72: /* refarg ::= MATCH nm */
14084 case 73: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==73);
14085 { yygotominor.yy215.value = 0; yygotominor.yy215.mask = 0x000000; }
14086 break;
14087 case 74: /* refarg ::= ON DELETE refact */
14088 { yygotominor.yy215.value = yymsp[0].minor.yy4; yygotominor.yy215.mask = 0x0 000ff; }
14089 break;
14090 case 75: /* refarg ::= ON UPDATE refact */
14091 { yygotominor.yy215.value = yymsp[0].minor.yy4<<8; yygotominor.yy215.mask = 0x0 0ff00; }
14092 break;
14093 case 76: /* refact ::= SET NULL */
14094 { yygotominor.yy4 = OE_SetNull; /* EV: R-33326-45252 */}
14095 break;
14096 case 77: /* refact ::= SET DEFAULT */
14097 { yygotominor.yy4 = OE_SetDflt; /* EV: R-33326-45252 */}
14098 break;
14099 case 78: /* refact ::= CASCADE */
14100 { yygotominor.yy4 = OE_Cascade; /* EV: R-33326-45252 */}
14101 break;
14102 case 79: /* refact ::= RESTRICT */
14103 { yygotominor.yy4 = OE_Restrict; /* EV: R-33326-45252 */}
14104 break;
14105 case 80: /* refact ::= NO ACTION */
14106 { yygotominor.yy4 = OE_None; /* EV: R-33326-45252 */}
14107 break;
14108 case 82: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
14109 case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno ==98);
14110 case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==10 0);
14111 case 102: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==102);
14112 case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
14113 case 178: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==178);
14114 {yygotominor.yy4 = yymsp[0].minor.yy4;}
14115 break;
14116 case 86: /* conslist_opt ::= */
14117 {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
14118 break;
14119 case 87: /* conslist_opt ::= COMMA conslist */
14120 {yygotominor.yy0 = yymsp[-1].minor.yy0;}
14121 break;
14122 case 90: /* tconscomma ::= COMMA */
14123 {pParse->constraintName.n = 0;}
14124 break;
14125 case 93: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
14126 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy4,yymsp[-2]. minor.yy4,0);}
14127 break;
14128 case 94: /* tcons ::= UNIQUE LP sortlist RP onconf */
14129 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy4,0,0,0, 0);}
14130 break;
14131 case 95: /* tcons ::= CHECK LP expr RP onconf */
14132 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy118.pExpr);}
14133 break;
14134 case 96: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
14135 {
14136 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[-1].minor.yy4);
14137 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy4);
14138 }
14139 break;
14140 case 99: /* onconf ::= */
14141 case 101: /* orconf ::= */ yytestcase(yyruleno==101);
14142 {yygotominor.yy4 = OE_Default;}
14143 break;
14144 case 104: /* resolvetype ::= IGNORE */
14145 {yygotominor.yy4 = OE_Ignore;}
14146 break;
14147 case 105: /* resolvetype ::= REPLACE */
14148 case 179: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==179);
14149 {yygotominor.yy4 = OE_Replace;}
14150 break;
14151 case 106: /* cmd ::= DROP TABLE ifexists fullname */
14152 {
14153 sqlite3DropTable(pParse, yymsp[0].minor.yy259, 0, yymsp[-1].minor.yy4);
14154 }
14155 break;
14156 case 109: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
14157 {
14158 sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[- 3].minor.yy0, yymsp[-2].minor.yy322, yymsp[0].minor.yy387, yymsp[-7].minor.yy4, yymsp[-5].minor.yy4);
14159 }
14160 break;
14161 case 110: /* cmd ::= DROP VIEW ifexists fullname */
14162 {
14163 sqlite3DropTable(pParse, yymsp[0].minor.yy259, 1, yymsp[-1].minor.yy4);
14164 }
14165 break;
14166 case 111: /* cmd ::= select */
14167 {
14168 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
14169 sqlite3Select(pParse, yymsp[0].minor.yy387, &dest);
14170 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
14171 }
14172 break;
14173 case 112: /* select ::= with selectnowith */
14174 {
14175 Select *p = yymsp[0].minor.yy387;
14176 if( p ){
14177 p->pWith = yymsp[-1].minor.yy451;
14178 parserDoubleLinkSelect(pParse, p);
14179 }else{
14180 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy451);
14181 }
14182 yygotominor.yy387 = p;
14183 }
14184 break;
14185 case 113: /* selectnowith ::= oneselect */
14186 case 119: /* oneselect ::= values */ yytestcase(yyruleno==119);
14187 {yygotominor.yy387 = yymsp[0].minor.yy387;}
14188 break;
14189 case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
14190 {
14191 Select *pRhs = yymsp[0].minor.yy387;
14192 Select *pLhs = yymsp[-2].minor.yy387;
14193 if( pRhs && pRhs->pPrior ){
14194 SrcList *pFrom;
14195 Token x;
14196 x.n = 0;
14197 parserDoubleLinkSelect(pParse, pRhs);
14198 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
14199 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
14200 }
14201 if( pRhs ){
14202 pRhs->op = (u8)yymsp[-1].minor.yy4;
14203 pRhs->pPrior = pLhs;
14204 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
14205 pRhs->selFlags &= ~SF_MultiValue;
14206 if( yymsp[-1].minor.yy4!=TK_ALL ) pParse->hasCompound = 1;
14207 }else{
14208 sqlite3SelectDelete(pParse->db, pLhs);
14209 }
14210 yygotominor.yy387 = pRhs;
14211 }
14212 break;
14213 case 116: /* multiselect_op ::= UNION ALL */
14214 {yygotominor.yy4 = TK_ALL;}
14215 break;
14216 case 118: /* oneselect ::= SELECT distinct selcollist from where_opt group by_opt having_opt orderby_opt limit_opt */
14217 {
14218 yygotominor.yy387 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].mi nor.yy259,yymsp[-4].minor.yy314,yymsp[-3].minor.yy322,yymsp[-2].minor.yy314,yyms p[-1].minor.yy322,yymsp[-7].minor.yy4,yymsp[0].minor.yy292.pLimit,yymsp[0].minor .yy292.pOffset);
14219 #if SELECTTRACE_ENABLED
14220 /* Populate the Select.zSelName[] string that is used to help with
14221 ** query planner debugging, to differentiate between multiple Select
14222 ** objects in a complex query.
14223 **
14224 ** If the SELECT keyword is immediately followed by a C-style comment
14225 ** then extract the first few alphanumeric characters from within that
14226 ** comment to be the zSelName value. Otherwise, the label is #N where
14227 ** is an integer that is incremented with each SELECT statement seen.
14228 */
14229 if( yygotominor.yy387!=0 ){
14230 const char *z = yymsp[-8].minor.yy0.z+6;
14231 int i;
14232 sqlite3_snprintf(sizeof(yygotominor.yy387->zSelName), yygotominor.yy387->zSe lName, "#%d",
14233 ++pParse->nSelect);
14234 while( z[0]==' ' ) z++;
14235 if( z[0]=='/' && z[1]=='*' ){
14236 z += 2;
14237 while( z[0]==' ' ) z++;
14238 for(i=0; sqlite3Isalnum(z[i]); i++){}
14239 sqlite3_snprintf(sizeof(yygotominor.yy387->zSelName), yygotominor.yy387->z SelName, "%.*s", i, z);
14240 }
14241 }
14242 #endif /* SELECTRACE_ENABLED */
14243 }
14244 break;
14245 case 120: /* values ::= VALUES LP nexprlist RP */
14246 {
14247 yygotominor.yy387 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF _Values,0,0);
14248 }
14249 break;
14250 case 121: /* values ::= values COMMA LP exprlist RP */
14251 {
14252 Select *pRight, *pLeft = yymsp[-4].minor.yy387;
14253 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values|SF_ MultiValue,0,0);
14254 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
14255 if( pRight ){
14256 pRight->op = TK_ALL;
14257 pLeft = yymsp[-4].minor.yy387;
14258 pRight->pPrior = pLeft;
14259 yygotominor.yy387 = pRight;
14260 }else{
14261 yygotominor.yy387 = pLeft;
14262 }
14263 }
14264 break;
14265 case 122: /* distinct ::= DISTINCT */
14266 {yygotominor.yy4 = SF_Distinct;}
14267 break;
14268 case 123: /* distinct ::= ALL */
14269 {yygotominor.yy4 = SF_All;}
14270 break;
14271 case 125: /* sclp ::= selcollist COMMA */
14272 case 244: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==244);
14273 {yygotominor.yy322 = yymsp[-1].minor.yy322;}
14274 break;
14275 case 126: /* sclp ::= */
14276 case 155: /* orderby_opt ::= */ yytestcase(yyruleno==155);
14277 case 162: /* groupby_opt ::= */ yytestcase(yyruleno==162);
14278 case 237: /* exprlist ::= */ yytestcase(yyruleno==237);
14279 case 243: /* eidlist_opt ::= */ yytestcase(yyruleno==243);
14280 {yygotominor.yy322 = 0;}
14281 break;
14282 case 127: /* selcollist ::= sclp expr as */
14283 {
14284 yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, yyms p[-1].minor.yy118.pExpr);
14285 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy322 , &yymsp[0].minor.yy0, 1);
14286 sqlite3ExprListSetSpan(pParse,yygotominor.yy322,&yymsp[-1].minor.yy118);
14287 }
14288 break;
14289 case 128: /* selcollist ::= sclp STAR */
14290 {
14291 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
14292 yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy322, p);
14293 }
14294 break;
14295 case 129: /* selcollist ::= sclp nm DOT STAR */
14296 {
14297 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0, &yymsp[0].minor.yy0);
14298 Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
14299 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
14300 yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, pDot);
14301 }
14302 break;
14303 case 132: /* as ::= */
14304 {yygotominor.yy0.n = 0;}
14305 break;
14306 case 133: /* from ::= */
14307 {yygotominor.yy259 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy259)) ;}
14308 break;
14309 case 134: /* from ::= FROM seltablist */
14310 {
14311 yygotominor.yy259 = yymsp[0].minor.yy259;
14312 sqlite3SrcListShiftJoinType(yygotominor.yy259);
14313 }
14314 break;
14315 case 135: /* stl_prefix ::= seltablist joinop */
14316 {
14317 yygotominor.yy259 = yymsp[-1].minor.yy259;
14318 if( ALWAYS(yygotominor.yy259 && yygotominor.yy259->nSrc>0) ) yygotominor.yy25 9->a[yygotominor.yy259->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy4;
14319 }
14320 break;
14321 case 136: /* stl_prefix ::= */
14322 {yygotominor.yy259 = 0;}
14323 break;
14324 case 137: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using _opt */
14325 {
14326 yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259, &yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor .yy314,yymsp[0].minor.yy384);
14327 sqlite3SrcListIndexedBy(pParse, yygotominor.yy259, &yymsp[-2].minor.yy0);
14328 }
14329 break;
14330 case 138: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt us ing_opt */
14331 {
14332 yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy259, &yymsp[-7].minor.yy0,&yymsp[-6].minor.yy0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor .yy314,yymsp[0].minor.yy384);
14333 sqlite3SrcListFuncArgs(pParse, yygotominor.yy259, yymsp[-4].minor.yy322);
14334 }
14335 break;
14336 case 139: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
14337 {
14338 yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy25 9,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy387,yymsp[-1].minor.yy314,yymsp[0]. minor.yy384);
14339 }
14340 break;
14341 case 140: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_op t */
14342 {
14343 if( yymsp[-6].minor.yy259==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor. yy314==0 && yymsp[0].minor.yy384==0 ){
14344 yygotominor.yy259 = yymsp[-4].minor.yy259;
14345 }else if( yymsp[-4].minor.yy259->nSrc==1 ){
14346 yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy 259,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
14347 if( yygotominor.yy259 ){
14348 struct SrcList_item *pNew = &yygotominor.yy259->a[yygotominor.yy259->nSr c-1];
14349 struct SrcList_item *pOld = yymsp[-4].minor.yy259->a;
14350 pNew->zName = pOld->zName;
14351 pNew->zDatabase = pOld->zDatabase;
14352 pNew->pSelect = pOld->pSelect;
14353 pOld->zName = pOld->zDatabase = 0;
14354 pOld->pSelect = 0;
14355 }
14356 sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy259);
14357 }else{
14358 Select *pSubquery;
14359 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy259);
14360 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy259,0,0,0,0,SF_Nes tedFrom,0,0);
14361 yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy 259,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy314,yymsp[0].minor.yy38 4);
14362 }
14363 }
14364 break;
14365 case 141: /* dbnm ::= */
14366 case 150: /* indexed_opt ::= */ yytestcase(yyruleno==150);
14367 {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
14368 break;
14369 case 143: /* fullname ::= nm dbnm */
14370 {yygotominor.yy259 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yym sp[0].minor.yy0);}
14371 break;
14372 case 144: /* joinop ::= COMMA|JOIN */
14373 { yygotominor.yy4 = JT_INNER; }
14374 break;
14375 case 145: /* joinop ::= JOIN_KW JOIN */
14376 { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
14377 break;
14378 case 146: /* joinop ::= JOIN_KW nm JOIN */
14379 { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor .yy0,0); }
14380 break;
14381 case 147: /* joinop ::= JOIN_KW nm nm JOIN */
14382 { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor .yy0,&yymsp[-1].minor.yy0); }
14383 break;
14384 case 148: /* on_opt ::= ON expr */
14385 case 165: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==165);
14386 case 172: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==172);
14387 case 232: /* case_else ::= ELSE expr */ yytestcase(yyruleno==232);
14388 case 234: /* case_operand ::= expr */ yytestcase(yyruleno==234);
14389 {yygotominor.yy314 = yymsp[0].minor.yy118.pExpr;}
14390 break;
14391 case 149: /* on_opt ::= */
14392 case 164: /* having_opt ::= */ yytestcase(yyruleno==164);
14393 case 171: /* where_opt ::= */ yytestcase(yyruleno==171);
14394 case 233: /* case_else ::= */ yytestcase(yyruleno==233);
14395 case 235: /* case_operand ::= */ yytestcase(yyruleno==235);
14396 {yygotominor.yy314 = 0;}
14397 break;
14398 case 152: /* indexed_opt ::= NOT INDEXED */
14399 {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
14400 break;
14401 case 153: /* using_opt ::= USING LP idlist RP */
14402 case 181: /* idlist_opt ::= LP idlist RP */ yytestcase(yyruleno==181);
14403 {yygotominor.yy384 = yymsp[-1].minor.yy384;}
14404 break;
14405 case 154: /* using_opt ::= */
14406 case 180: /* idlist_opt ::= */ yytestcase(yyruleno==180);
14407 {yygotominor.yy384 = 0;}
14408 break;
14409 case 156: /* orderby_opt ::= ORDER BY sortlist */
14410 case 163: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==16 3);
14411 case 236: /* exprlist ::= nexprlist */ yytestcase(yyruleno==236);
14412 {yygotominor.yy322 = yymsp[0].minor.yy322;}
14413 break;
14414 case 157: /* sortlist ::= sortlist COMMA expr sortorder */
14415 {
14416 yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322,yymsp[- 1].minor.yy118.pExpr);
14417 sqlite3ExprListSetSortOrder(yygotominor.yy322,yymsp[0].minor.yy4);
14418 }
14419 break;
14420 case 158: /* sortlist ::= expr sortorder */
14421 {
14422 yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy118.pExpr );
14423 sqlite3ExprListSetSortOrder(yygotominor.yy322,yymsp[0].minor.yy4);
14424 }
14425 break;
14426 case 159: /* sortorder ::= ASC */
14427 {yygotominor.yy4 = SQLITE_SO_ASC;}
14428 break;
14429 case 160: /* sortorder ::= DESC */
14430 {yygotominor.yy4 = SQLITE_SO_DESC;}
14431 break;
14432 case 161: /* sortorder ::= */
14433 {yygotominor.yy4 = SQLITE_SO_UNDEFINED;}
14434 break;
14435 case 166: /* limit_opt ::= */
14436 {yygotominor.yy292.pLimit = 0; yygotominor.yy292.pOffset = 0;}
14437 break;
14438 case 167: /* limit_opt ::= LIMIT expr */
14439 {yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr; yygotominor.yy292.pOffse t = 0;}
14440 break;
14441 case 168: /* limit_opt ::= LIMIT expr OFFSET expr */
14442 {yygotominor.yy292.pLimit = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pOffs et = yymsp[0].minor.yy118.pExpr;}
14443 break;
14444 case 169: /* limit_opt ::= LIMIT expr COMMA expr */
14445 {yygotominor.yy292.pOffset = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pLim it = yymsp[0].minor.yy118.pExpr;}
14446 break;
14447 case 170: /* cmd ::= with DELETE FROM fullname indexed_opt where_opt */
14448 {
14449 sqlite3WithPush(pParse, yymsp[-5].minor.yy451, 1);
14450 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy259, &yymsp[-1].minor.yy0);
14451 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy259,yymsp[0].minor.yy314);
14452 }
14453 break;
14454 case 173: /* cmd ::= with UPDATE orconf fullname indexed_opt SET setlist w here_opt */
14455 {
14456 sqlite3WithPush(pParse, yymsp[-7].minor.yy451, 1);
14457 sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy259, &yymsp[-3].minor.yy0);
14458 sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy322,"set list");
14459 sqlite3Update(pParse,yymsp[-4].minor.yy259,yymsp[-1].minor.yy322,yymsp[0].mino r.yy314,yymsp[-5].minor.yy4);
14460 }
14461 break;
14462 case 174: /* setlist ::= setlist COMMA nm EQ expr */
14463 {
14464 yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp [0].minor.yy118.pExpr);
14465 sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
14466 }
14467 break;
14468 case 175: /* setlist ::= nm EQ expr */
14469 {
14470 yygotominor.yy322 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy118.pExp r);
14471 sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
14472 }
14473 break;
14474 case 176: /* cmd ::= with insert_cmd INTO fullname idlist_opt select */
14475 {
14476 sqlite3WithPush(pParse, yymsp[-5].minor.yy451, 1);
14477 sqlite3Insert(pParse, yymsp[-2].minor.yy259, yymsp[0].minor.yy387, yymsp[-1].m inor.yy384, yymsp[-4].minor.yy4);
14478 }
14479 break;
14480 case 177: /* cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALU ES */
14481 {
14482 sqlite3WithPush(pParse, yymsp[-6].minor.yy451, 1);
14483 sqlite3Insert(pParse, yymsp[-3].minor.yy259, 0, yymsp[-2].minor.yy384, yymsp[- 5].minor.yy4);
14484 }
14485 break;
14486 case 182: /* idlist ::= idlist COMMA nm */
14487 {yygotominor.yy384 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy384,&yymsp [0].minor.yy0);}
14488 break;
14489 case 183: /* idlist ::= nm */
14490 {yygotominor.yy384 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
14491 break;
14492 case 184: /* expr ::= term */
14493 {yygotominor.yy118 = yymsp[0].minor.yy118;}
14494 break;
14495 case 185: /* expr ::= LP expr RP */
14496 {yygotominor.yy118.pExpr = yymsp[-1].minor.yy118.pExpr; spanSet(&yygotominor.yy1 18,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
14497 break;
14498 case 186: /* term ::= NULL */
14499 case 191: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==191);
14500 case 192: /* term ::= STRING */ yytestcase(yyruleno==192);
14501 {spanExpr(&yygotominor.yy118, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
14502 break;
14503 case 187: /* expr ::= ID|INDEXED */
14504 case 188: /* expr ::= JOIN_KW */ yytestcase(yyruleno==188);
14505 {spanExpr(&yygotominor.yy118, pParse, TK_ID, &yymsp[0].minor.yy0);}
14506 break;
14507 case 189: /* expr ::= nm DOT nm */
14508 {
14509 Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
14510 Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
14511 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
14512 spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
14513 }
14514 break;
14515 case 190: /* expr ::= nm DOT nm DOT nm */
14516 {
14517 Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
14518 Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
14519 Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
14520 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
14521 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
14522 spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
14523 }
14524 break;
14525 case 193: /* expr ::= VARIABLE */
14526 {
14527 if( yymsp[0].minor.yy0.n>=2 && yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit( yymsp[0].minor.yy0.z[1]) ){
14528 /* When doing a nested parse, one can include terms in an expression
14529 ** that look like this: #1 #2 ... These terms refer to registers
14530 ** in the virtual machine. #N is the N-th register. */
14531 if( pParse->nested==0 ){
14532 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
14533 yygotominor.yy118.pExpr = 0;
14534 }else{
14535 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0 ].minor.yy0);
14536 if( yygotominor.yy118.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &y ygotominor.yy118.pExpr->iTable);
14537 }
14538 }else{
14539 spanExpr(&yygotominor.yy118, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
14540 sqlite3ExprAssignVarNumber(pParse, yygotominor.yy118.pExpr);
14541 }
14542 spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
14543 }
14544 break;
14545 case 194: /* expr ::= expr COLLATE ID|STRING */
14546 {
14547 yygotominor.yy118.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.y y118.pExpr, &yymsp[0].minor.yy0, 1);
14548 yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
14549 yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
14550 }
14551 break;
14552 case 195: /* expr ::= CAST LP expr AS typetoken RP */
14553 {
14554 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy118. pExpr, 0, &yymsp[-1].minor.yy0);
14555 spanSet(&yygotominor.yy118,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
14556 }
14557 break;
14558 case 196: /* expr ::= ID|INDEXED LP distinct exprlist RP */
14559 {
14560 if( yymsp[-1].minor.yy322 && yymsp[-1].minor.yy322->nExpr>pParse->db->aLimit[S QLITE_LIMIT_FUNCTION_ARG] ){
14561 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].mino r.yy0);
14562 }
14563 yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, & yymsp[-4].minor.yy0);
14564 spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
14565 if( yymsp[-2].minor.yy4==SF_Distinct && yygotominor.yy118.pExpr ){
14566 yygotominor.yy118.pExpr->flags |= EP_Distinct;
14567 }
14568 }
14569 break;
14570 case 197: /* expr ::= ID|INDEXED LP STAR RP */
14571 {
14572 yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0) ;
14573 spanSet(&yygotominor.yy118,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
14574 }
14575 break;
14576 case 198: /* term ::= CTIME_KW */
14577 {
14578 yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0);
14579 spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
14580 }
14581 break;
14582 case 199: /* expr ::= expr AND expr */
14583 case 200: /* expr ::= expr OR expr */ yytestcase(yyruleno==200);
14584 case 201: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==201);
14585 case 202: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==202);
14586 case 203: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(y yruleno==203);
14587 case 204: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==204);
14588 case 205: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==205 );
14589 case 206: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==206);
14590 {spanBinaryExpr(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[-2].minor.yy118 ,&yymsp[0].minor.yy118);}
14591 break;
14592 case 207: /* likeop ::= LIKE_KW|MATCH */
14593 {yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.bNot = 0;}
14594 break;
14595 case 208: /* likeop ::= NOT LIKE_KW|MATCH */
14596 {yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.bNot = 1;}
14597 break;
14598 case 209: /* expr ::= expr likeop expr */
14599 {
14600 ExprList *pList;
14601 pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy118.pExpr);
14602 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy118.pExpr);
14603 yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor. yy342.eOperator);
14604 exprNot(pParse, yymsp[-1].minor.yy342.bNot, &yygotominor.yy118.pExpr);
14605 yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
14606 yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
14607 if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
14608 }
14609 break;
14610 case 210: /* expr ::= expr likeop expr ESCAPE expr */
14611 {
14612 ExprList *pList;
14613 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
14614 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy118.pExpr);
14615 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
14616 yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor. yy342.eOperator);
14617 exprNot(pParse, yymsp[-3].minor.yy342.bNot, &yygotominor.yy118.pExpr);
14618 yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
14619 yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
14620 if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
14621 }
14622 break;
14623 case 211: /* expr ::= expr ISNULL|NOTNULL */
14624 {spanUnaryPostfix(&yygotominor.yy118,pParse,yymsp[0].major,&yymsp[-1].minor.yy11 8,&yymsp[0].minor.yy0);}
14625 break;
14626 case 212: /* expr ::= expr NOT NULL */
14627 {spanUnaryPostfix(&yygotominor.yy118,pParse,TK_NOTNULL,&yymsp[-2].minor.yy118,&y ymsp[0].minor.yy0);}
14628 break;
14629 case 213: /* expr ::= expr IS expr */
14630 {
14631 spanBinaryExpr(&yygotominor.yy118,pParse,TK_IS,&yymsp[-2].minor.yy118,&yymsp[0 ].minor.yy118);
14632 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExp r, TK_ISNULL);
14633 }
14634 break;
14635 case 214: /* expr ::= expr IS NOT expr */
14636 {
14637 spanBinaryExpr(&yygotominor.yy118,pParse,TK_ISNOT,&yymsp[-3].minor.yy118,&yyms p[0].minor.yy118);
14638 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExp r, TK_NOTNULL);
14639 }
14640 break;
14641 case 215: /* expr ::= NOT expr */
14642 case 216: /* expr ::= BITNOT expr */ yytestcase(yyruleno==216);
14643 {spanUnaryPrefix(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[0].minor.yy118 ,&yymsp[-1].minor.yy0);}
14644 break;
14645 case 217: /* expr ::= MINUS expr */
14646 {spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UMINUS,&yymsp[0].minor.yy118,&yyms p[-1].minor.yy0);}
14647 break;
14648 case 218: /* expr ::= PLUS expr */
14649 {spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UPLUS,&yymsp[0].minor.yy118,&yymsp [-1].minor.yy0);}
14650 break;
14651 case 221: /* expr ::= expr between_op expr AND expr */
14652 {
14653 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr) ;
14654 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
14655 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy1 18.pExpr, 0, 0);
14656 if( yygotominor.yy118.pExpr ){
14657 yygotominor.yy118.pExpr->x.pList = pList;
14658 }else{
14659 sqlite3ExprListDelete(pParse->db, pList);
14660 }
14661 exprNot(pParse, yymsp[-3].minor.yy4, &yygotominor.yy118.pExpr);
14662 yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
14663 yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
14664 }
14665 break;
14666 case 224: /* expr ::= expr in_op LP exprlist RP */
14667 {
14668 if( yymsp[-1].minor.yy322==0 ){
14669 /* Expressions of the form
14670 **
14671 ** expr1 IN ()
14672 ** expr1 NOT IN ()
14673 **
14674 ** simplify to constants 0 (false) and 1 (true), respectively,
14675 ** regardless of the value of expr1.
14676 */
14677 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3I ntTokens[yymsp[-3].minor.yy4]);
14678 sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy118.pExpr);
14679 }else if( yymsp[-1].minor.yy322->nExpr==1 ){
14680 /* Expressions of the form:
14681 **
14682 ** expr1 IN (?1)
14683 ** expr1 NOT IN (?2)
14684 **
14685 ** with exactly one value on the RHS can be simplified to something
14686 ** like this:
14687 **
14688 ** expr1 == ?1
14689 ** expr1 <> ?2
14690 **
14691 ** But, the RHS of the == or <> is marked with the EP_Generic flag
14692 ** so that it may not contribute to the computation of comparison
14693 ** affinity or the collating sequence to use for comparison. Otherwise,
14694 ** the semantics would be subtly different from IN or NOT IN.
14695 */
14696 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
14697 yymsp[-1].minor.yy322->a[0].pExpr = 0;
14698 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
14699 /* pRHS cannot be NULL because a malloc error would have been detected
14700 ** before now and control would have never reached this point */
14701 if( ALWAYS(pRHS) ){
14702 pRHS->flags &= ~EP_Collate;
14703 pRHS->flags |= EP_Generic;
14704 }
14705 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, yymsp[-3].minor.yy4 ? TK_NE : TK_EQ, yymsp[-4].minor.yy118.pExpr, pRHS, 0);
14706 }else{
14707 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy11 8.pExpr, 0, 0);
14708 if( yygotominor.yy118.pExpr ){
14709 yygotominor.yy118.pExpr->x.pList = yymsp[-1].minor.yy322;
14710 sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy118.pExpr);
14711 }else{
14712 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
14713 }
14714 exprNot(pParse, yymsp[-3].minor.yy4, &yygotominor.yy118.pExpr);
14715 }
14716 yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
14717 yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
14718 }
14719 break;
14720 case 225: /* expr ::= LP select RP */
14721 {
14722 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
14723 if( yygotominor.yy118.pExpr ){
14724 yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
14725 ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect|EP_Subquery);
14726 sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy118.pExpr);
14727 }else{
14728 sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
14729 }
14730 yygotominor.yy118.zStart = yymsp[-2].minor.yy0.z;
14731 yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
14732 }
14733 break;
14734 case 226: /* expr ::= expr in_op LP select RP */
14735 {
14736 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118. pExpr, 0, 0);
14737 if( yygotominor.yy118.pExpr ){
14738 yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
14739 ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect|EP_Subquery);
14740 sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy118.pExpr);
14741 }else{
14742 sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
14743 }
14744 exprNot(pParse, yymsp[-3].minor.yy4, &yygotominor.yy118.pExpr);
14745 yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
14746 yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
14747 }
14748 break;
14749 case 227: /* expr ::= expr in_op nm dbnm */
14750 {
14751 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yym sp[0].minor.yy0);
14752 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy118. pExpr, 0, 0);
14753 if( yygotominor.yy118.pExpr ){
14754 yygotominor.yy118.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0 ,0,0,0,0);
14755 ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect|EP_Subquery);
14756 sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy118.pExpr);
14757 }else{
14758 sqlite3SrcListDelete(pParse->db, pSrc);
14759 }
14760 exprNot(pParse, yymsp[-2].minor.yy4, &yygotominor.yy118.pExpr);
14761 yygotominor.yy118.zStart = yymsp[-3].minor.yy118.zStart;
14762 yygotominor.yy118.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[ 0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n];
14763 }
14764 break;
14765 case 228: /* expr ::= EXISTS LP select RP */
14766 {
14767 Expr *p = yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0) ;
14768 if( p ){
14769 p->x.pSelect = yymsp[-1].minor.yy387;
14770 ExprSetProperty(p, EP_xIsSelect|EP_Subquery);
14771 sqlite3ExprSetHeightAndFlags(pParse, p);
14772 }else{
14773 sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
14774 }
14775 yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
14776 yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
14777 }
14778 break;
14779 case 229: /* expr ::= CASE case_operand case_exprlist case_else END */
14780 {
14781 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy314, 0, 0);
14782 if( yygotominor.yy118.pExpr ){
14783 yygotominor.yy118.pExpr->x.pList = yymsp[-1].minor.yy314 ? sqlite3ExprListAp pend(pParse,yymsp[-2].minor.yy322,yymsp[-1].minor.yy314) : yymsp[-2].minor.yy322 ;
14784 sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy118.pExpr);
14785 }else{
14786 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
14787 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy314);
14788 }
14789 yygotominor.yy118.zStart = yymsp[-4].minor.yy0.z;
14790 yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
14791 }
14792 break;
14793 case 230: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
14794 {
14795 yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[ -2].minor.yy118.pExpr);
14796 yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].m inor.yy118.pExpr);
14797 }
14798 break;
14799 case 231: /* case_exprlist ::= WHEN expr THEN expr */
14800 {
14801 yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExp r);
14802 yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].m inor.yy118.pExpr);
14803 }
14804 break;
14805 case 238: /* nexprlist ::= nexprlist COMMA expr */
14806 {yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0] .minor.yy118.pExpr);}
14807 break;
14808 case 239: /* nexprlist ::= expr */
14809 {yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy118.pExpr); }
14810 break;
14811 case 240: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm L P sortlist RP where_opt */
14812 {
14813 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
14814 sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy322, yymsp[-10].minor.yy4,
14815 &yymsp[-11].minor.yy0, yymsp[0].minor.yy314, SQLITE_SO_ASC , yymsp[-8].minor.yy4);
14816 }
14817 break;
14818 case 241: /* uniqueflag ::= UNIQUE */
14819 case 292: /* raisetype ::= ABORT */ yytestcase(yyruleno==292);
14820 {yygotominor.yy4 = OE_Abort;}
14821 break;
14822 case 242: /* uniqueflag ::= */
14823 {yygotominor.yy4 = OE_None;}
14824 break;
14825 case 245: /* eidlist ::= eidlist COMMA nm collate sortorder */
14826 {
14827 yygotominor.yy322 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy322, &yy msp[-2].minor.yy0, yymsp[-1].minor.yy4, yymsp[0].minor.yy4);
14828 }
14829 break;
14830 case 246: /* eidlist ::= nm collate sortorder */
14831 {
14832 yygotominor.yy322 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, y ymsp[-1].minor.yy4, yymsp[0].minor.yy4);
14833 }
14834 break;
14835 case 249: /* cmd ::= DROP INDEX ifexists fullname */
14836 {sqlite3DropIndex(pParse, yymsp[0].minor.yy259, yymsp[-1].minor.yy4);}
14837 break;
14838 case 250: /* cmd ::= VACUUM */
14839 case 251: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==251);
14840 {sqlite3Vacuum(pParse);}
14841 break;
14842 case 252: /* cmd ::= PRAGMA nm dbnm */
14843 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
14844 break;
14845 case 253: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
14846 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor. yy0,0);}
14847 break;
14848 case 254: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
14849 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor .yy0,0);}
14850 break;
14851 case 255: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
14852 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor. yy0,1);}
14853 break;
14854 case 256: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
14855 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor .yy0,1);}
14856 break;
14857 case 265: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
14858 {
14859 Token all;
14860 all.z = yymsp[-3].minor.yy0.z;
14861 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.y y0.n;
14862 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy203, &all);
14863 }
14864 break;
14865 case 266: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_tim e trigger_event ON fullname foreach_clause when_clause */
14866 {
14867 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[ -5].minor.yy4, yymsp[-4].minor.yy90.a, yymsp[-4].minor.yy90.b, yymsp[-2].minor.y y259, yymsp[0].minor.yy314, yymsp[-10].minor.yy4, yymsp[-8].minor.yy4);
14868 yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].mino r.yy0);
14869 }
14870 break;
14871 case 267: /* trigger_time ::= BEFORE */
14872 case 270: /* trigger_time ::= */ yytestcase(yyruleno==270);
14873 { yygotominor.yy4 = TK_BEFORE; }
14874 break;
14875 case 268: /* trigger_time ::= AFTER */
14876 { yygotominor.yy4 = TK_AFTER; }
14877 break;
14878 case 269: /* trigger_time ::= INSTEAD OF */
14879 { yygotominor.yy4 = TK_INSTEAD;}
14880 break;
14881 case 271: /* trigger_event ::= DELETE|INSERT */
14882 case 272: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==272);
14883 {yygotominor.yy90.a = yymsp[0].major; yygotominor.yy90.b = 0;}
14884 break;
14885 case 273: /* trigger_event ::= UPDATE OF idlist */
14886 {yygotominor.yy90.a = TK_UPDATE; yygotominor.yy90.b = yymsp[0].minor.yy384;}
14887 break;
14888 case 276: /* when_clause ::= */
14889 case 297: /* key_opt ::= */ yytestcase(yyruleno==297);
14890 { yygotominor.yy314 = 0; }
14891 break;
14892 case 277: /* when_clause ::= WHEN expr */
14893 case 298: /* key_opt ::= KEY expr */ yytestcase(yyruleno==298);
14894 { yygotominor.yy314 = yymsp[0].minor.yy118.pExpr; }
14895 break;
14896 case 278: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
14897 {
14898 assert( yymsp[-2].minor.yy203!=0 );
14899 yymsp[-2].minor.yy203->pLast->pNext = yymsp[-1].minor.yy203;
14900 yymsp[-2].minor.yy203->pLast = yymsp[-1].minor.yy203;
14901 yygotominor.yy203 = yymsp[-2].minor.yy203;
14902 }
14903 break;
14904 case 279: /* trigger_cmd_list ::= trigger_cmd SEMI */
14905 {
14906 assert( yymsp[-1].minor.yy203!=0 );
14907 yymsp[-1].minor.yy203->pLast = yymsp[-1].minor.yy203;
14908 yygotominor.yy203 = yymsp[-1].minor.yy203;
14909 }
14910 break;
14911 case 281: /* trnm ::= nm DOT nm */
14912 {
14913 yygotominor.yy0 = yymsp[0].minor.yy0;
14914 sqlite3ErrorMsg(pParse,
14915 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
14916 "statements within triggers");
14917 }
14918 break;
14919 case 283: /* tridxby ::= INDEXED BY nm */
14920 {
14921 sqlite3ErrorMsg(pParse,
14922 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
14923 "within triggers");
14924 }
14925 break;
14926 case 284: /* tridxby ::= NOT INDEXED */
14927 {
14928 sqlite3ErrorMsg(pParse,
14929 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
14930 "within triggers");
14931 }
14932 break;
14933 case 285: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_ opt */
14934 { yygotominor.yy203 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy322, yymsp[0].minor.yy314, yymsp[-5].minor.yy4); }
14935 break;
14936 case 286: /* trigger_cmd ::= insert_cmd INTO trnm idlist_opt select */
14937 {yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy384, yymsp[0].minor.yy387, yymsp[-4].minor.yy4);}
14938 break;
14939 case 287: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
14940 {yygotominor.yy203 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy314);}
14941 break;
14942 case 288: /* trigger_cmd ::= select */
14943 {yygotominor.yy203 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy387); }
14944 break;
14945 case 289: /* expr ::= RAISE LP IGNORE RP */
14946 {
14947 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
14948 if( yygotominor.yy118.pExpr ){
14949 yygotominor.yy118.pExpr->affinity = OE_Ignore;
14950 }
14951 yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
14952 yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
14953 }
14954 break;
14955 case 290: /* expr ::= RAISE LP raisetype COMMA nm RP */
14956 {
14957 yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].mino r.yy0);
14958 if( yygotominor.yy118.pExpr ) {
14959 yygotominor.yy118.pExpr->affinity = (char)yymsp[-3].minor.yy4;
14960 }
14961 yygotominor.yy118.zStart = yymsp[-5].minor.yy0.z;
14962 yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
14963 }
14964 break;
14965 case 291: /* raisetype ::= ROLLBACK */
14966 {yygotominor.yy4 = OE_Rollback;}
14967 break;
14968 case 293: /* raisetype ::= FAIL */
14969 {yygotominor.yy4 = OE_Fail;}
14970 break;
14971 case 294: /* cmd ::= DROP TRIGGER ifexists fullname */
14972 {
14973 sqlite3DropTrigger(pParse,yymsp[0].minor.yy259,yymsp[-1].minor.yy4);
14974 }
14975 break;
14976 case 295: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
14977 {
14978 sqlite3Attach(pParse, yymsp[-3].minor.yy118.pExpr, yymsp[-1].minor.yy118.pExpr , yymsp[0].minor.yy314);
14979 }
14980 break;
14981 case 296: /* cmd ::= DETACH database_kw_opt expr */
14982 {
14983 sqlite3Detach(pParse, yymsp[0].minor.yy118.pExpr);
14984 }
14985 break;
14986 case 301: /* cmd ::= REINDEX */
14987 {sqlite3Reindex(pParse, 0, 0);}
14988 break;
14989 case 302: /* cmd ::= REINDEX nm dbnm */
14990 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
14991 break;
14992 case 303: /* cmd ::= ANALYZE */
14993 {sqlite3Analyze(pParse, 0, 0);}
14994 break;
14995 case 304: /* cmd ::= ANALYZE nm dbnm */
14996 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
14997 break;
14998 case 305: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
14999 {
15000 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy259,&yymsp[0].minor.yy0);
15001 }
15002 break;
15003 case 306: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt colu mn */
15004 {
15005 sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
15006 }
15007 break;
15008 case 307: /* add_column_fullname ::= fullname */
15009 {
15010 pParse->db->lookaside.bEnabled = 0;
15011 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259);
15012 }
15013 break;
15014 case 310: /* cmd ::= create_vtab */
15015 {sqlite3VtabFinishParse(pParse,0);}
15016 break;
15017 case 311: /* cmd ::= create_vtab LP vtabarglist RP */
15018 {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
15019 break;
15020 case 312: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm US ING nm */
15021 {
15022 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &y ymsp[0].minor.yy0, yymsp[-4].minor.yy4);
15023 }
15024 break;
15025 case 315: /* vtabarg ::= */
15026 {sqlite3VtabArgInit(pParse);}
15027 break;
15028 case 317: /* vtabargtoken ::= ANY */
15029 case 318: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==318);
15030 case 319: /* lp ::= LP */ yytestcase(yyruleno==319);
15031 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
15032 break;
15033 case 323: /* with ::= */
15034 {yygotominor.yy451 = 0;}
15035 break;
15036 case 324: /* with ::= WITH wqlist */
15037 case 325: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==325);
15038 { yygotominor.yy451 = yymsp[0].minor.yy451; }
15039 break;
15040 case 326: /* wqlist ::= nm eidlist_opt AS LP select RP */
15041 {
15042 yygotominor.yy451 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4]. minor.yy322, yymsp[-1].minor.yy387);
15043 }
15044 break;
15045 case 327: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
15046 {
15047 yygotominor.yy451 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy451, &yymsp[-5].m inor.yy0, yymsp[-4].minor.yy322, yymsp[-1].minor.yy387);
15048 }
15049 break;
15050 default:
15051 /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
15052 /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
15053 /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
15054 /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
15055 /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
15056 /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
15057 /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
15058 /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
15059 /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
15060 /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
15061 /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25) ;
15062 /* (36) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==36) ;
15063 /* (37) columnlist ::= column */ yytestcase(yyruleno==37);
15064 /* (43) type ::= */ yytestcase(yyruleno==43);
15065 /* (50) signed ::= plus_num */ yytestcase(yyruleno==50);
15066 /* (51) signed ::= minus_num */ yytestcase(yyruleno==51);
15067 /* (52) carglist ::= carglist ccons */ yytestcase(yyruleno==52);
15068 /* (53) carglist ::= */ yytestcase(yyruleno==53);
15069 /* (60) ccons ::= NULL onconf */ yytestcase(yyruleno==60);
15070 /* (88) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==88) ;
15071 /* (89) conslist ::= tcons */ yytestcase(yyruleno==89);
15072 /* (91) tconscomma ::= */ yytestcase(yyruleno==91);
15073 /* (274) foreach_clause ::= */ yytestcase(yyruleno==274);
15074 /* (275) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==275);
15075 /* (282) tridxby ::= */ yytestcase(yyruleno==282);
15076 /* (299) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==299);
15077 /* (300) database_kw_opt ::= */ yytestcase(yyruleno==300);
15078 /* (308) kwcolumn_opt ::= */ yytestcase(yyruleno==308);
15079 /* (309) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==309);
15080 /* (313) vtabarglist ::= vtabarg */ yytestcase(yyruleno==313);
15081 /* (314) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno= =314);
15082 /* (316) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==316);
15083 /* (320) anylist ::= */ yytestcase(yyruleno==320);
15084 /* (321) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==321);
15085 /* (322) anylist ::= anylist ANY */ yytestcase(yyruleno==322);
15086 break;
15087 /********** End reduce actions ************************************************/
15088 };
15089 assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
15090 yygoto = yyRuleInfo[yyruleno].lhs;
15091 yysize = yyRuleInfo[yyruleno].nrhs;
15092 yypParser->yyidx -= yysize;
15093 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
15094 if( yyact <= YY_MAX_SHIFTREDUCE ){
15095 if( yyact>YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
15096 /* If the reduce action popped at least
15097 ** one element off the stack, then we can push the new element back
15098 ** onto the stack here, and skip the stack overflow test in yy_shift().
15099 ** That gives a significant speed improvement. */
15100 if( yysize ){
15101 yypParser->yyidx++;
15102 yymsp -= yysize-1;
15103 yymsp->stateno = (YYACTIONTYPE)yyact;
15104 yymsp->major = (YYCODETYPE)yygoto;
15105 yymsp->minor = yygotominor;
15106 yyTraceShift(yypParser, yyact);
15107 }else{
15108 yy_shift(yypParser,yyact,yygoto,&yygotominor);
15109 }
15110 }else{
15111 assert( yyact == YY_ACCEPT_ACTION );
15112 yy_accept(yypParser);
15113 }
15114 }
15115
15116 /*
15117 ** The following code executes when the parse fails
15118 */
15119 #ifndef YYNOERRORRECOVERY
15120 static void yy_parse_failed(
15121 yyParser *yypParser /* The parser */
15122 ){
15123 sqlite3ParserARG_FETCH;
15124 #ifndef NDEBUG
15125 if( yyTraceFILE ){
15126 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
15127 }
15128 #endif
15129 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
15130 /* Here code is inserted which will be executed whenever the
15131 ** parser fails */
15132 /************ Begin %parse_failure code ***************************************/
15133 /************ End %parse_failure code *****************************************/
15134 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument varia ble */
15135 }
15136 #endif /* YYNOERRORRECOVERY */
15137
15138 /*
15139 ** The following code executes when a syntax error first occurs.
15140 */
15141 static void yy_syntax_error(
15142 yyParser *yypParser, /* The parser */
15143 int yymajor, /* The major type of the error token */
15144 YYMINORTYPE yyminor /* The minor type of the error token */
15145 ){
15146 sqlite3ParserARG_FETCH;
15147 #define TOKEN (yyminor.yy0)
15148 /************ Begin %syntax_error code ****************************************/
15149
15150 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
15151 assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */
15152 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
15153 /************ End %syntax_error code ******************************************/
15154 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument varia ble */
15155 }
15156
15157 /*
15158 ** The following is executed when the parser accepts
15159 */
15160 static void yy_accept(
15161 yyParser *yypParser /* The parser */
15162 ){
15163 sqlite3ParserARG_FETCH;
15164 #ifndef NDEBUG
15165 if( yyTraceFILE ){
15166 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
15167 }
15168 #endif
15169 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
15170 /* Here code is inserted which will be executed whenever the
15171 ** parser accepts */
15172 /*********** Begin %parse_accept code *****************************************/
15173 /*********** End %parse_accept code *******************************************/
15174 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument varia ble */
15175 }
15176
15177 /* The main parser program.
15178 ** The first argument is a pointer to a structure obtained from
15179 ** "sqlite3ParserAlloc" which describes the current state of the parser.
15180 ** The second argument is the major token number. The third is
15181 ** the minor token. The fourth optional argument is whatever the
15182 ** user wants (and specified in the grammar) and is available for
15183 ** use by the action routines.
15184 **
15185 ** Inputs:
15186 ** <ul>
15187 ** <li> A pointer to the parser (an opaque structure.)
15188 ** <li> The major token number.
15189 ** <li> The minor token number.
15190 ** <li> An option argument of a grammar-specified type.
15191 ** </ul>
15192 **
15193 ** Outputs:
15194 ** None.
15195 */
15196 SQLITE_PRIVATE void sqlite3Parser(
15197 void *yyp, /* The parser */
15198 int yymajor, /* The major token code number */
15199 sqlite3ParserTOKENTYPE yyminor /* The value for the token */
15200 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
15201 ){
15202 YYMINORTYPE yyminorunion;
15203 int yyact; /* The parser action. */
15204 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
15205 int yyendofinput; /* True if we are at the end of input */
15206 #endif
15207 #ifdef YYERRORSYMBOL
15208 int yyerrorhit = 0; /* True if yymajor has invoked an error */
15209 #endif
15210 yyParser *yypParser; /* The parser */
15211
15212 /* (re)initialize the parser, if necessary */
15213 yypParser = (yyParser*)yyp;
15214 if( yypParser->yyidx<0 ){
15215 #if YYSTACKDEPTH<=0
15216 if( yypParser->yystksz <=0 ){
15217 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
15218 yyminorunion = yyzerominor;
15219 yyStackOverflow(yypParser, &yyminorunion);
15220 return;
15221 }
15222 #endif
15223 yypParser->yyidx = 0;
15224 yypParser->yyerrcnt = -1;
15225 yypParser->yystack[0].stateno = 0;
15226 yypParser->yystack[0].major = 0;
15227 #ifndef NDEBUG
15228 if( yyTraceFILE ){
15229 fprintf(yyTraceFILE,"%sInitialize. Empty stack. State 0\n",
15230 yyTracePrompt);
15231 }
15232 #endif
15233 }
15234 yyminorunion.yy0 = yyminor;
15235 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
15236 yyendofinput = (yymajor==0);
15237 #endif
15238 sqlite3ParserARG_STORE;
15239
15240 #ifndef NDEBUG
15241 if( yyTraceFILE ){
15242 fprintf(yyTraceFILE,"%sInput '%s'\n",yyTracePrompt,yyTokenName[yymajor]);
15243 }
15244 #endif
15245
15246 do{
15247 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
15248 if( yyact <= YY_MAX_SHIFTREDUCE ){
15249 if( yyact > YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
15250 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
15251 yypParser->yyerrcnt--;
15252 yymajor = YYNOCODE;
15253 }else if( yyact <= YY_MAX_REDUCE ){
15254 yy_reduce(yypParser,yyact-YY_MIN_REDUCE);
15255 }else{
15256 assert( yyact == YY_ERROR_ACTION );
15257 #ifdef YYERRORSYMBOL
15258 int yymx;
15259 #endif
15260 #ifndef NDEBUG
15261 if( yyTraceFILE ){
15262 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
15263 }
15264 #endif
15265 #ifdef YYERRORSYMBOL
15266 /* A syntax error has occurred.
15267 ** The response to an error depends upon whether or not the
15268 ** grammar defines an error token "ERROR".
15269 **
15270 ** This is what we do if the grammar does define ERROR:
15271 **
15272 ** * Call the %syntax_error function.
15273 **
15274 ** * Begin popping the stack until we enter a state where
15275 ** it is legal to shift the error symbol, then shift
15276 ** the error symbol.
15277 **
15278 ** * Set the error count to three.
15279 **
15280 ** * Begin accepting and shifting new tokens. No new error
15281 ** processing will occur until three tokens have been
15282 ** shifted successfully.
15283 **
15284 */
15285 if( yypParser->yyerrcnt<0 ){
15286 yy_syntax_error(yypParser,yymajor,yyminorunion);
15287 }
15288 yymx = yypParser->yystack[yypParser->yyidx].major;
15289 if( yymx==YYERRORSYMBOL || yyerrorhit ){
15290 #ifndef NDEBUG
15291 if( yyTraceFILE ){
15292 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
15293 yyTracePrompt,yyTokenName[yymajor]);
15294 }
15295 #endif
15296 yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
15297 yymajor = YYNOCODE;
15298 }else{
15299 while(
15300 yypParser->yyidx >= 0 &&
15301 yymx != YYERRORSYMBOL &&
15302 (yyact = yy_find_reduce_action(
15303 yypParser->yystack[yypParser->yyidx].stateno,
15304 YYERRORSYMBOL)) >= YY_MIN_REDUCE
15305 ){
15306 yy_pop_parser_stack(yypParser);
15307 }
15308 if( yypParser->yyidx < 0 || yymajor==0 ){
15309 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
15310 yy_parse_failed(yypParser);
15311 yymajor = YYNOCODE;
15312 }else if( yymx!=YYERRORSYMBOL ){
15313 YYMINORTYPE u2;
15314 u2.YYERRSYMDT = 0;
15315 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
15316 }
15317 }
15318 yypParser->yyerrcnt = 3;
15319 yyerrorhit = 1;
15320 #elif defined(YYNOERRORRECOVERY)
15321 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
15322 ** do any kind of error recovery. Instead, simply invoke the syntax
15323 ** error routine and continue going as if nothing had happened.
15324 **
15325 ** Applications can set this macro (for example inside %include) if
15326 ** they intend to abandon the parse upon the first syntax error seen.
15327 */
15328 yy_syntax_error(yypParser,yymajor,yyminorunion);
15329 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
15330 yymajor = YYNOCODE;
15331
15332 #else /* YYERRORSYMBOL is not defined */
15333 /* This is what we do if the grammar does not define ERROR:
15334 **
15335 ** * Report an error message, and throw away the input token.
15336 **
15337 ** * If the input token is $, then fail the parse.
15338 **
15339 ** As before, subsequent error messages are suppressed until
15340 ** three input tokens have been successfully shifted.
15341 */
15342 if( yypParser->yyerrcnt<=0 ){
15343 yy_syntax_error(yypParser,yymajor,yyminorunion);
15344 }
15345 yypParser->yyerrcnt = 3;
15346 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
15347 if( yyendofinput ){
15348 yy_parse_failed(yypParser);
15349 }
15350 yymajor = YYNOCODE;
15351 #endif
15352 }
15353 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
15354 #ifndef NDEBUG
15355 if( yyTraceFILE ){
15356 int i;
15357 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
15358 for(i=1; i<=yypParser->yyidx; i++)
15359 fprintf(yyTraceFILE,"%c%s", i==1 ? '[' : ' ',
15360 yyTokenName[yypParser->yystack[i].major]);
15361 fprintf(yyTraceFILE,"]\n");
15362 }
15363 #endif
15364 return;
15365 }
15366
15367 /************** End of parse.c ***********************************************/
15368 /************** Begin file tokenize.c ****************************************/
15369 /*
15370 ** 2001 September 15
15371 **
15372 ** The author disclaims copyright to this source code. In place of
15373 ** a legal notice, here is a blessing:
15374 **
15375 ** May you do good and not evil.
15376 ** May you find forgiveness for yourself and forgive others.
15377 ** May you share freely, never taking more than you give.
15378 **
15379 *************************************************************************
15380 ** An tokenizer for SQL
15381 **
15382 ** This file contains C code that splits an SQL input string up into
15383 ** individual tokens and sends those tokens one-by-one over to the
15384 ** parser for analysis.
15385 */
15386 /* #include "sqliteInt.h" */
15387 /* #include <stdlib.h> */
15388
15389 /*
15390 ** The charMap() macro maps alphabetic characters into their
15391 ** lower-case ASCII equivalent. On ASCII machines, this is just
15392 ** an upper-to-lower case map. On EBCDIC machines we also need
15393 ** to adjust the encoding. Only alphabetic characters and underscores
15394 ** need to be translated.
15395 */
15396 #ifdef SQLITE_ASCII
15397 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
15398 #endif
15399 #ifdef SQLITE_EBCDIC
15400 # define charMap(X) ebcdicToAscii[(unsigned char)X]
15401 const unsigned char ebcdicToAscii[] = {
15402 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
15403 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
15404 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
15405 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
15406 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
15407 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
15408 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
15409 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
15410 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
15411 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
15412 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
15413 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
15414 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
15415 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
15416 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
15417 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
15418 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
15419 };
15420 #endif
15421
15422 /*
15423 ** The sqlite3KeywordCode function looks up an identifier to determine if
15424 ** it is a keyword. If it is a keyword, the token code of that keyword is
15425 ** returned. If the input is not a keyword, TK_ID is returned.
15426 **
15427 ** The implementation of this routine was generated by a program,
15428 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
15429 ** The output of the mkkeywordhash.c program is written into a file
15430 ** named keywordhash.h and then included into this source file by
15431 ** the #include below.
15432 */
15433 /************** Include keywordhash.h in the middle of tokenize.c ************/
15434 /************** Begin file keywordhash.h *************************************/
15435 /***** This file contains automatically generated code ******
15436 **
15437 ** The code in this file has been automatically generated by
15438 **
15439 ** sqlite/tool/mkkeywordhash.c
15440 **
15441 ** The code in this file implements a function that determines whether
15442 ** or not a given identifier is really an SQL keyword. The same thing
15443 ** might be implemented more directly using a hand-written hash table.
15444 ** But by using this automatically generated code, the size of the code
15445 ** is substantially reduced. This is important for embedded applications
15446 ** on platforms with limited memory.
15447 */
15448 /* Hash score: 182 */
15449 static int keywordCode(const char *z, int n, int *pType){
15450 /* zText[] encodes 834 bytes of keywords in 554 bytes */
15451 /* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
15452 /* ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE */
15453 /* XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY */
15454 /* UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERECURSIVE */
15455 /* BETWEENOTNULLIKECASCADELETECASECOLLATECREATECURRENT_DATEDETACH */
15456 /* IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN */
15457 /* WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMIT */
15458 /* CONFLICTCROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAIL */
15459 /* FROMFULLGLOBYIFISNULLORDERESTRICTRIGHTROLLBACKROWUNIONUSING */
15460 /* VACUUMVIEWINITIALLY */
15461 static const char zText[553] = {
15462 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
15463 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
15464 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
15465 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
15466 'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
15467 'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
15468 'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
15469 'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
15470 'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
15471 'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
15472 'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S',
15473 'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A',
15474 'T','E','B','E','G','I','N','N','E','R','E','C','U','R','S','I','V','E',
15475 'B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C','A',
15476 'S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L','A',
15477 'T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A',
15478 'T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E','J',
15479 'O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A','L',
15480 'Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U','E',
15481 'S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W','H',
15482 'E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C','E',
15483 'A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E',
15484 'M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M',
15485 'I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R',
15486 'R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M','A',
15487 'R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D',
15488 'R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L','O',
15489 'B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T',
15490 'R','I','C','T','R','I','G','H','T','R','O','L','L','B','A','C','K','R',
15491 'O','W','U','N','I','O','N','U','S','I','N','G','V','A','C','U','U','M',
15492 'V','I','E','W','I','N','I','T','I','A','L','L','Y',
15493 };
15494 static const unsigned char aHash[127] = {
15495 76, 105, 117, 74, 0, 45, 0, 0, 82, 0, 77, 0, 0,
15496 42, 12, 78, 15, 0, 116, 85, 54, 112, 0, 19, 0, 0,
15497 121, 0, 119, 115, 0, 22, 93, 0, 9, 0, 0, 70, 71,
15498 0, 69, 6, 0, 48, 90, 102, 0, 118, 101, 0, 0, 44,
15499 0, 103, 24, 0, 17, 0, 122, 53, 23, 0, 5, 110, 25,
15500 96, 0, 0, 124, 106, 60, 123, 57, 28, 55, 0, 91, 0,
15501 100, 26, 0, 99, 0, 0, 0, 95, 92, 97, 88, 109, 14,
15502 39, 108, 0, 81, 0, 18, 89, 111, 32, 0, 120, 80, 113,
15503 62, 46, 84, 0, 0, 94, 40, 59, 114, 0, 36, 0, 0,
15504 29, 0, 86, 63, 64, 0, 20, 61, 0, 56,
15505 };
15506 static const unsigned char aNext[124] = {
15507 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
15508 0, 2, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0,
15509 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15510 0, 0, 0, 0, 33, 0, 21, 0, 0, 0, 0, 0, 50,
15511 0, 43, 3, 47, 0, 0, 0, 0, 30, 0, 58, 0, 38,
15512 0, 0, 0, 1, 66, 0, 0, 67, 0, 41, 0, 0, 0,
15513 0, 0, 0, 49, 65, 0, 0, 0, 0, 31, 52, 16, 34,
15514 10, 0, 0, 0, 0, 0, 0, 0, 11, 72, 79, 0, 8,
15515 0, 104, 98, 0, 107, 0, 87, 0, 75, 51, 0, 27, 37,
15516 73, 83, 0, 35, 68, 0, 0,
15517 };
15518 static const unsigned char aLen[124] = {
15519 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
15520 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 6,
15521 11, 6, 2, 7, 5, 5, 9, 6, 9, 9, 7, 10, 10,
15522 4, 6, 2, 3, 9, 4, 2, 6, 5, 7, 4, 5, 7,
15523 6, 6, 5, 6, 5, 5, 9, 7, 7, 3, 2, 4, 4,
15524 7, 3, 6, 4, 7, 6, 12, 6, 9, 4, 6, 5, 4,
15525 7, 6, 5, 6, 7, 5, 4, 5, 6, 5, 7, 3, 7,
15526 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 8, 8,
15527 2, 4, 4, 4, 4, 4, 2, 2, 6, 5, 8, 5, 8,
15528 3, 5, 5, 6, 4, 9, 3,
15529 };
15530 static const unsigned short int aOffset[124] = {
15531 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
15532 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
15533 86, 91, 95, 96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
15534 159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192,
15535 199, 204, 209, 212, 218, 221, 225, 234, 240, 240, 240, 243, 246,
15536 250, 251, 255, 261, 265, 272, 278, 290, 296, 305, 307, 313, 318,
15537 320, 327, 332, 337, 343, 349, 354, 358, 361, 367, 371, 378, 380,
15538 387, 389, 391, 400, 404, 410, 416, 424, 429, 429, 445, 452, 459,
15539 460, 467, 471, 475, 479, 483, 486, 488, 490, 496, 500, 508, 513,
15540 521, 524, 529, 534, 540, 544, 549,
15541 };
15542 static const unsigned char aCode[124] = {
15543 TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE,
15544 TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN,
15545 TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD,
15546 TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE,
15547 TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE,
15548 TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW,
15549 TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_SAVEPOINT,
15550 TK_INTERSECT, TK_TRIGGER, TK_REFERENCES, TK_CONSTRAINT, TK_INTO,
15551 TK_OFFSET, TK_OF, TK_SET, TK_TEMP, TK_TEMP,
15552 TK_OR, TK_UNIQUE, TK_QUERY, TK_WITHOUT, TK_WITH,
15553 TK_JOIN_KW, TK_RELEASE, TK_ATTACH, TK_HAVING, TK_GROUP,
15554 TK_UPDATE, TK_BEGIN, TK_JOIN_KW, TK_RECURSIVE, TK_BETWEEN,
15555 TK_NOTNULL, TK_NOT, TK_NO, TK_NULL, TK_LIKE_KW,
15556 TK_CASCADE, TK_ASC, TK_DELETE, TK_CASE, TK_COLLATE,
15557 TK_CREATE, TK_CTIME_KW, TK_DETACH, TK_IMMEDIATE, TK_JOIN,
15558 TK_INSERT, TK_MATCH, TK_PLAN, TK_ANALYZE, TK_PRAGMA,
15559 TK_ABORT, TK_VALUES, TK_VIRTUAL, TK_LIMIT, TK_WHEN,
15560 TK_WHERE, TK_RENAME, TK_AFTER, TK_REPLACE, TK_AND,
15561 TK_DEFAULT, TK_AUTOINCR, TK_TO, TK_IN, TK_CAST,
15562 TK_COLUMNKW, TK_COMMIT, TK_CONFLICT, TK_JOIN_KW, TK_CTIME_KW,
15563 TK_CTIME_KW, TK_PRIMARY, TK_DEFERRED, TK_DISTINCT, TK_IS,
15564 TK_DROP, TK_FAIL, TK_FROM, TK_JOIN_KW, TK_LIKE_KW,
15565 TK_BY, TK_IF, TK_ISNULL, TK_ORDER, TK_RESTRICT,
15566 TK_JOIN_KW, TK_ROLLBACK, TK_ROW, TK_UNION, TK_USING,
15567 TK_VACUUM, TK_VIEW, TK_INITIALLY, TK_ALL,
15568 };
15569 int h, i;
15570 if( n>=2 ){
15571 h = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
15572 for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
15573 if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
15574 testcase( i==0 ); /* REINDEX */
15575 testcase( i==1 ); /* INDEXED */
15576 testcase( i==2 ); /* INDEX */
15577 testcase( i==3 ); /* DESC */
15578 testcase( i==4 ); /* ESCAPE */
15579 testcase( i==5 ); /* EACH */
15580 testcase( i==6 ); /* CHECK */
15581 testcase( i==7 ); /* KEY */
15582 testcase( i==8 ); /* BEFORE */
15583 testcase( i==9 ); /* FOREIGN */
15584 testcase( i==10 ); /* FOR */
15585 testcase( i==11 ); /* IGNORE */
15586 testcase( i==12 ); /* REGEXP */
15587 testcase( i==13 ); /* EXPLAIN */
15588 testcase( i==14 ); /* INSTEAD */
15589 testcase( i==15 ); /* ADD */
15590 testcase( i==16 ); /* DATABASE */
15591 testcase( i==17 ); /* AS */
15592 testcase( i==18 ); /* SELECT */
15593 testcase( i==19 ); /* TABLE */
15594 testcase( i==20 ); /* LEFT */
15595 testcase( i==21 ); /* THEN */
15596 testcase( i==22 ); /* END */
15597 testcase( i==23 ); /* DEFERRABLE */
15598 testcase( i==24 ); /* ELSE */
15599 testcase( i==25 ); /* EXCEPT */
15600 testcase( i==26 ); /* TRANSACTION */
15601 testcase( i==27 ); /* ACTION */
15602 testcase( i==28 ); /* ON */
15603 testcase( i==29 ); /* NATURAL */
15604 testcase( i==30 ); /* ALTER */
15605 testcase( i==31 ); /* RAISE */
15606 testcase( i==32 ); /* EXCLUSIVE */
15607 testcase( i==33 ); /* EXISTS */
15608 testcase( i==34 ); /* SAVEPOINT */
15609 testcase( i==35 ); /* INTERSECT */
15610 testcase( i==36 ); /* TRIGGER */
15611 testcase( i==37 ); /* REFERENCES */
15612 testcase( i==38 ); /* CONSTRAINT */
15613 testcase( i==39 ); /* INTO */
15614 testcase( i==40 ); /* OFFSET */
15615 testcase( i==41 ); /* OF */
15616 testcase( i==42 ); /* SET */
15617 testcase( i==43 ); /* TEMPORARY */
15618 testcase( i==44 ); /* TEMP */
15619 testcase( i==45 ); /* OR */
15620 testcase( i==46 ); /* UNIQUE */
15621 testcase( i==47 ); /* QUERY */
15622 testcase( i==48 ); /* WITHOUT */
15623 testcase( i==49 ); /* WITH */
15624 testcase( i==50 ); /* OUTER */
15625 testcase( i==51 ); /* RELEASE */
15626 testcase( i==52 ); /* ATTACH */
15627 testcase( i==53 ); /* HAVING */
15628 testcase( i==54 ); /* GROUP */
15629 testcase( i==55 ); /* UPDATE */
15630 testcase( i==56 ); /* BEGIN */
15631 testcase( i==57 ); /* INNER */
15632 testcase( i==58 ); /* RECURSIVE */
15633 testcase( i==59 ); /* BETWEEN */
15634 testcase( i==60 ); /* NOTNULL */
15635 testcase( i==61 ); /* NOT */
15636 testcase( i==62 ); /* NO */
15637 testcase( i==63 ); /* NULL */
15638 testcase( i==64 ); /* LIKE */
15639 testcase( i==65 ); /* CASCADE */
15640 testcase( i==66 ); /* ASC */
15641 testcase( i==67 ); /* DELETE */
15642 testcase( i==68 ); /* CASE */
15643 testcase( i==69 ); /* COLLATE */
15644 testcase( i==70 ); /* CREATE */
15645 testcase( i==71 ); /* CURRENT_DATE */
15646 testcase( i==72 ); /* DETACH */
15647 testcase( i==73 ); /* IMMEDIATE */
15648 testcase( i==74 ); /* JOIN */
15649 testcase( i==75 ); /* INSERT */
15650 testcase( i==76 ); /* MATCH */
15651 testcase( i==77 ); /* PLAN */
15652 testcase( i==78 ); /* ANALYZE */
15653 testcase( i==79 ); /* PRAGMA */
15654 testcase( i==80 ); /* ABORT */
15655 testcase( i==81 ); /* VALUES */
15656 testcase( i==82 ); /* VIRTUAL */
15657 testcase( i==83 ); /* LIMIT */
15658 testcase( i==84 ); /* WHEN */
15659 testcase( i==85 ); /* WHERE */
15660 testcase( i==86 ); /* RENAME */
15661 testcase( i==87 ); /* AFTER */
15662 testcase( i==88 ); /* REPLACE */
15663 testcase( i==89 ); /* AND */
15664 testcase( i==90 ); /* DEFAULT */
15665 testcase( i==91 ); /* AUTOINCREMENT */
15666 testcase( i==92 ); /* TO */
15667 testcase( i==93 ); /* IN */
15668 testcase( i==94 ); /* CAST */
15669 testcase( i==95 ); /* COLUMN */
15670 testcase( i==96 ); /* COMMIT */
15671 testcase( i==97 ); /* CONFLICT */
15672 testcase( i==98 ); /* CROSS */
15673 testcase( i==99 ); /* CURRENT_TIMESTAMP */
15674 testcase( i==100 ); /* CURRENT_TIME */
15675 testcase( i==101 ); /* PRIMARY */
15676 testcase( i==102 ); /* DEFERRED */
15677 testcase( i==103 ); /* DISTINCT */
15678 testcase( i==104 ); /* IS */
15679 testcase( i==105 ); /* DROP */
15680 testcase( i==106 ); /* FAIL */
15681 testcase( i==107 ); /* FROM */
15682 testcase( i==108 ); /* FULL */
15683 testcase( i==109 ); /* GLOB */
15684 testcase( i==110 ); /* BY */
15685 testcase( i==111 ); /* IF */
15686 testcase( i==112 ); /* ISNULL */
15687 testcase( i==113 ); /* ORDER */
15688 testcase( i==114 ); /* RESTRICT */
15689 testcase( i==115 ); /* RIGHT */
15690 testcase( i==116 ); /* ROLLBACK */
15691 testcase( i==117 ); /* ROW */
15692 testcase( i==118 ); /* UNION */
15693 testcase( i==119 ); /* USING */
15694 testcase( i==120 ); /* VACUUM */
15695 testcase( i==121 ); /* VIEW */
15696 testcase( i==122 ); /* INITIALLY */
15697 testcase( i==123 ); /* ALL */
15698 *pType = aCode[i];
15699 break;
15700 }
15701 }
15702 }
15703 return n;
15704 }
15705 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
15706 int id = TK_ID;
15707 keywordCode((char*)z, n, &id);
15708 return id;
15709 }
15710 #define SQLITE_N_KEYWORD 124
15711
15712 /************** End of keywordhash.h *****************************************/
15713 /************** Continuing where we left off in tokenize.c *******************/
15714
15715
15716 /*
15717 ** If X is a character that can be used in an identifier then
15718 ** IdChar(X) will be true. Otherwise it is false.
15719 **
15720 ** For ASCII, any character with the high-order bit set is
15721 ** allowed in an identifier. For 7-bit characters,
15722 ** sqlite3IsIdChar[X] must be 1.
15723 **
15724 ** For EBCDIC, the rules are more complex but have the same
15725 ** end result.
15726 **
15727 ** Ticket #1066. the SQL standard does not allow '$' in the
15728 ** middle of identifiers. But many SQL implementations do.
15729 ** SQLite will allow '$' in identifiers for compatibility.
15730 ** But the feature is undocumented.
15731 */
15732 #ifdef SQLITE_ASCII
15733 #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
15734 #endif
15735 #ifdef SQLITE_EBCDIC
15736 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
15737 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
15738 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
15739 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
15740 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
15741 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
15742 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
15743 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
15744 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
15745 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
15746 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
15747 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
15748 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
15749 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
15750 };
15751 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
15752 #endif
15753
15754 /* Make the IdChar function accessible from ctime.c */
15755 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
15756 SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
15757 #endif
15758
15759
15760 /*
15761 ** Return the length of the token that begins at z[0].
15762 ** Store the token type in *tokenType before returning.
15763 */
15764 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
15765 int i, c;
15766 switch( *z ){
15767 case ' ': case '\t': case '\n': case '\f': case '\r': {
15768 testcase( z[0]==' ' );
15769 testcase( z[0]=='\t' );
15770 testcase( z[0]=='\n' );
15771 testcase( z[0]=='\f' );
15772 testcase( z[0]=='\r' );
15773 for(i=1; sqlite3Isspace(z[i]); i++){}
15774 *tokenType = TK_SPACE;
15775 return i;
15776 }
15777 case '-': {
15778 if( z[1]=='-' ){
15779 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
15780 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
15781 return i;
15782 }
15783 *tokenType = TK_MINUS;
15784 return 1;
15785 }
15786 case '(': {
15787 *tokenType = TK_LP;
15788 return 1;
15789 }
15790 case ')': {
15791 *tokenType = TK_RP;
15792 return 1;
15793 }
15794 case ';': {
15795 *tokenType = TK_SEMI;
15796 return 1;
15797 }
15798 case '+': {
15799 *tokenType = TK_PLUS;
15800 return 1;
15801 }
15802 case '*': {
15803 *tokenType = TK_STAR;
15804 return 1;
15805 }
15806 case '/': {
15807 if( z[1]!='*' || z[2]==0 ){
15808 *tokenType = TK_SLASH;
15809 return 1;
15810 }
15811 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
15812 if( c ) i++;
15813 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
15814 return i;
15815 }
15816 case '%': {
15817 *tokenType = TK_REM;
15818 return 1;
15819 }
15820 case '=': {
15821 *tokenType = TK_EQ;
15822 return 1 + (z[1]=='=');
15823 }
15824 case '<': {
15825 if( (c=z[1])=='=' ){
15826 *tokenType = TK_LE;
15827 return 2;
15828 }else if( c=='>' ){
15829 *tokenType = TK_NE;
15830 return 2;
15831 }else if( c=='<' ){
15832 *tokenType = TK_LSHIFT;
15833 return 2;
15834 }else{
15835 *tokenType = TK_LT;
15836 return 1;
15837 }
15838 }
15839 case '>': {
15840 if( (c=z[1])=='=' ){
15841 *tokenType = TK_GE;
15842 return 2;
15843 }else if( c=='>' ){
15844 *tokenType = TK_RSHIFT;
15845 return 2;
15846 }else{
15847 *tokenType = TK_GT;
15848 return 1;
15849 }
15850 }
15851 case '!': {
15852 if( z[1]!='=' ){
15853 *tokenType = TK_ILLEGAL;
15854 return 2;
15855 }else{
15856 *tokenType = TK_NE;
15857 return 2;
15858 }
15859 }
15860 case '|': {
15861 if( z[1]!='|' ){
15862 *tokenType = TK_BITOR;
15863 return 1;
15864 }else{
15865 *tokenType = TK_CONCAT;
15866 return 2;
15867 }
15868 }
15869 case ',': {
15870 *tokenType = TK_COMMA;
15871 return 1;
15872 }
15873 case '&': {
15874 *tokenType = TK_BITAND;
15875 return 1;
15876 }
15877 case '~': {
15878 *tokenType = TK_BITNOT;
15879 return 1;
15880 }
15881 case '`':
15882 case '\'':
15883 case '"': {
15884 int delim = z[0];
15885 testcase( delim=='`' );
15886 testcase( delim=='\'' );
15887 testcase( delim=='"' );
15888 for(i=1; (c=z[i])!=0; i++){
15889 if( c==delim ){
15890 if( z[i+1]==delim ){
15891 i++;
15892 }else{
15893 break;
15894 }
15895 }
15896 }
15897 if( c=='\'' ){
15898 *tokenType = TK_STRING;
15899 return i+1;
15900 }else if( c!=0 ){
15901 *tokenType = TK_ID;
15902 return i+1;
15903 }else{
15904 *tokenType = TK_ILLEGAL;
15905 return i;
15906 }
15907 }
15908 case '.': {
15909 #ifndef SQLITE_OMIT_FLOATING_POINT
15910 if( !sqlite3Isdigit(z[1]) )
15911 #endif
15912 {
15913 *tokenType = TK_DOT;
15914 return 1;
15915 }
15916 /* If the next character is a digit, this is a floating point
15917 ** number that begins with ".". Fall thru into the next case */
15918 }
15919 case '0': case '1': case '2': case '3': case '4':
15920 case '5': case '6': case '7': case '8': case '9': {
15921 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
15922 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
15923 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
15924 testcase( z[0]=='9' );
15925 *tokenType = TK_INTEGER;
15926 #ifndef SQLITE_OMIT_HEX_INTEGER
15927 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
15928 for(i=3; sqlite3Isxdigit(z[i]); i++){}
15929 return i;
15930 }
15931 #endif
15932 for(i=0; sqlite3Isdigit(z[i]); i++){}
15933 #ifndef SQLITE_OMIT_FLOATING_POINT
15934 if( z[i]=='.' ){
15935 i++;
15936 while( sqlite3Isdigit(z[i]) ){ i++; }
15937 *tokenType = TK_FLOAT;
15938 }
15939 if( (z[i]=='e' || z[i]=='E') &&
15940 ( sqlite3Isdigit(z[i+1])
15941 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
15942 )
15943 ){
15944 i += 2;
15945 while( sqlite3Isdigit(z[i]) ){ i++; }
15946 *tokenType = TK_FLOAT;
15947 }
15948 #endif
15949 while( IdChar(z[i]) ){
15950 *tokenType = TK_ILLEGAL;
15951 i++;
15952 }
15953 return i;
15954 }
15955 case '[': {
15956 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
15957 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
15958 return i;
15959 }
15960 case '?': {
15961 *tokenType = TK_VARIABLE;
15962 for(i=1; sqlite3Isdigit(z[i]); i++){}
15963 return i;
15964 }
15965 #ifndef SQLITE_OMIT_TCL_VARIABLE
15966 case '$':
15967 #endif
15968 case '@': /* For compatibility with MS SQL Server */
15969 case '#':
15970 case ':': {
15971 int n = 0;
15972 testcase( z[0]=='$' ); testcase( z[0]=='@' );
15973 testcase( z[0]==':' ); testcase( z[0]=='#' );
15974 *tokenType = TK_VARIABLE;
15975 for(i=1; (c=z[i])!=0; i++){
15976 if( IdChar(c) ){
15977 n++;
15978 #ifndef SQLITE_OMIT_TCL_VARIABLE
15979 }else if( c=='(' && n>0 ){
15980 do{
15981 i++;
15982 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
15983 if( c==')' ){
15984 i++;
15985 }else{
15986 *tokenType = TK_ILLEGAL;
15987 }
15988 break;
15989 }else if( c==':' && z[i+1]==':' ){
15990 i++;
15991 #endif
15992 }else{
15993 break;
15994 }
15995 }
15996 if( n==0 ) *tokenType = TK_ILLEGAL;
15997 return i;
15998 }
15999 #ifndef SQLITE_OMIT_BLOB_LITERAL
16000 case 'x': case 'X': {
16001 testcase( z[0]=='x' ); testcase( z[0]=='X' );
16002 if( z[1]=='\'' ){
16003 *tokenType = TK_BLOB;
16004 for(i=2; sqlite3Isxdigit(z[i]); i++){}
16005 if( z[i]!='\'' || i%2 ){
16006 *tokenType = TK_ILLEGAL;
16007 while( z[i] && z[i]!='\'' ){ i++; }
16008 }
16009 if( z[i] ) i++;
16010 return i;
16011 }
16012 /* Otherwise fall through to the next case */
16013 }
16014 #endif
16015 default: {
16016 if( !IdChar(*z) ){
16017 break;
16018 }
16019 for(i=1; IdChar(z[i]); i++){}
16020 *tokenType = TK_ID;
16021 return keywordCode((char*)z, i, tokenType);
16022 }
16023 }
16024 *tokenType = TK_ILLEGAL;
16025 return 1;
16026 }
16027
16028 /*
16029 ** Run the parser on the given SQL string. The parser structure is
16030 ** passed in. An SQLITE_ status code is returned. If an error occurs
16031 ** then an and attempt is made to write an error message into
16032 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
16033 ** error message.
16034 */
16035 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzEr rMsg){
16036 int nErr = 0; /* Number of errors encountered */
16037 int i; /* Loop counter */
16038 void *pEngine; /* The LEMON-generated LALR(1) parser */
16039 int tokenType; /* type of the next token */
16040 int lastTokenParsed = -1; /* type of the previous token */
16041 u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */
16042 sqlite3 *db = pParse->db; /* The database connection */
16043 int mxSqlLen; /* Max length of an SQL string */
16044
16045 assert( zSql!=0 );
16046 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
16047 if( db->nVdbeActive==0 ){
16048 db->u1.isInterrupted = 0;
16049 }
16050 pParse->rc = SQLITE_OK;
16051 pParse->zTail = zSql;
16052 i = 0;
16053 assert( pzErrMsg!=0 );
16054 /* sqlite3ParserTrace(stdout, "parser: "); */
16055 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
16056 if( pEngine==0 ){
16057 db->mallocFailed = 1;
16058 return SQLITE_NOMEM;
16059 }
16060 assert( pParse->pNewTable==0 );
16061 assert( pParse->pNewTrigger==0 );
16062 assert( pParse->nVar==0 );
16063 assert( pParse->nzVar==0 );
16064 assert( pParse->azVar==0 );
16065 enableLookaside = db->lookaside.bEnabled;
16066 if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
16067 while( zSql[i]!=0 ){
16068 assert( i>=0 );
16069 pParse->sLastToken.z = &zSql[i];
16070 pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
16071 i += pParse->sLastToken.n;
16072 if( i>mxSqlLen ){
16073 pParse->rc = SQLITE_TOOBIG;
16074 break;
16075 }
16076 if( tokenType>=TK_SPACE ){
16077 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
16078 if( db->u1.isInterrupted ){
16079 sqlite3ErrorMsg(pParse, "interrupt");
16080 pParse->rc = SQLITE_INTERRUPT;
16081 break;
16082 }
16083 if( tokenType==TK_ILLEGAL ){
16084 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
16085 &pParse->sLastToken);
16086 break;
16087 }
16088 }else{
16089 if( tokenType==TK_SEMI ) pParse->zTail = &zSql[i];
16090 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
16091 lastTokenParsed = tokenType;
16092 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
16093 }
16094 }
16095 assert( nErr==0 );
16096 if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
16097 assert( zSql[i]==0 );
16098 if( lastTokenParsed!=TK_SEMI ){
16099 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
16100 pParse->zTail = &zSql[i];
16101 }
16102 if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
16103 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
16104 }
16105 }
16106 #ifdef YYTRACKMAXSTACKDEPTH
16107 sqlite3_mutex_enter(sqlite3MallocMutex());
16108 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
16109 sqlite3ParserStackPeak(pEngine)
16110 );
16111 sqlite3_mutex_leave(sqlite3MallocMutex());
16112 #endif /* YYDEBUG */
16113 sqlite3ParserFree(pEngine, sqlite3_free);
16114 db->lookaside.bEnabled = enableLookaside;
16115 if( db->mallocFailed ){
16116 pParse->rc = SQLITE_NOMEM;
16117 }
16118 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
16119 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
16120 }
16121 assert( pzErrMsg!=0 );
16122 if( pParse->zErrMsg ){
16123 *pzErrMsg = pParse->zErrMsg;
16124 sqlite3_log(pParse->rc, "%s", *pzErrMsg);
16125 pParse->zErrMsg = 0;
16126 nErr++;
16127 }
16128 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
16129 sqlite3VdbeDelete(pParse->pVdbe);
16130 pParse->pVdbe = 0;
16131 }
16132 #ifndef SQLITE_OMIT_SHARED_CACHE
16133 if( pParse->nested==0 ){
16134 sqlite3DbFree(db, pParse->aTableLock);
16135 pParse->aTableLock = 0;
16136 pParse->nTableLock = 0;
16137 }
16138 #endif
16139 #ifndef SQLITE_OMIT_VIRTUALTABLE
16140 sqlite3_free(pParse->apVtabLock);
16141 #endif
16142
16143 if( !IN_DECLARE_VTAB ){
16144 /* If the pParse->declareVtab flag is set, do not delete any table
16145 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
16146 ** will take responsibility for freeing the Table structure.
16147 */
16148 sqlite3DeleteTable(db, pParse->pNewTable);
16149 }
16150
16151 sqlite3WithDelete(db, pParse->pWithToFree);
16152 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
16153 for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
16154 sqlite3DbFree(db, pParse->azVar);
16155 while( pParse->pAinc ){
16156 AutoincInfo *p = pParse->pAinc;
16157 pParse->pAinc = p->pNext;
16158 sqlite3DbFree(db, p);
16159 }
16160 while( pParse->pZombieTab ){
16161 Table *p = pParse->pZombieTab;
16162 pParse->pZombieTab = p->pNextZombie;
16163 sqlite3DeleteTable(db, p);
16164 }
16165 assert( nErr==0 || pParse->rc!=SQLITE_OK );
16166 return nErr;
16167 }
16168
16169 /************** End of tokenize.c ********************************************/
16170 /************** Begin file complete.c ****************************************/
16171 /*
16172 ** 2001 September 15
16173 **
16174 ** The author disclaims copyright to this source code. In place of
16175 ** a legal notice, here is a blessing:
16176 **
16177 ** May you do good and not evil.
16178 ** May you find forgiveness for yourself and forgive others.
16179 ** May you share freely, never taking more than you give.
16180 **
16181 *************************************************************************
16182 ** An tokenizer for SQL
16183 **
16184 ** This file contains C code that implements the sqlite3_complete() API.
16185 ** This code used to be part of the tokenizer.c source file. But by
16186 ** separating it out, the code will be automatically omitted from
16187 ** static links that do not use it.
16188 */
16189 /* #include "sqliteInt.h" */
16190 #ifndef SQLITE_OMIT_COMPLETE
16191
16192 /*
16193 ** This is defined in tokenize.c. We just have to import the definition.
16194 */
16195 #ifndef SQLITE_AMALGAMATION
16196 #ifdef SQLITE_ASCII
16197 #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
16198 #endif
16199 #ifdef SQLITE_EBCDIC
16200 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
16201 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
16202 #endif
16203 #endif /* SQLITE_AMALGAMATION */
16204
16205
16206 /*
16207 ** Token types used by the sqlite3_complete() routine. See the header
16208 ** comments on that procedure for additional information.
16209 */
16210 #define tkSEMI 0
16211 #define tkWS 1
16212 #define tkOTHER 2
16213 #ifndef SQLITE_OMIT_TRIGGER
16214 #define tkEXPLAIN 3
16215 #define tkCREATE 4
16216 #define tkTEMP 5
16217 #define tkTRIGGER 6
16218 #define tkEND 7
16219 #endif
16220
16221 /*
16222 ** Return TRUE if the given SQL string ends in a semicolon.
16223 **
16224 ** Special handling is require for CREATE TRIGGER statements.
16225 ** Whenever the CREATE TRIGGER keywords are seen, the statement
16226 ** must end with ";END;".
16227 **
16228 ** This implementation uses a state machine with 8 states:
16229 **
16230 ** (0) INVALID We have not yet seen a non-whitespace character.
16231 **
16232 ** (1) START At the beginning or end of an SQL statement. This routine
16233 ** returns 1 if it ends in the START state and 0 if it ends
16234 ** in any other state.
16235 **
16236 ** (2) NORMAL We are in the middle of statement which ends with a single
16237 ** semicolon.
16238 **
16239 ** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
16240 ** a statement.
16241 **
16242 ** (4) CREATE The keyword CREATE has been seen at the beginning of a
16243 ** statement, possibly preceded by EXPLAIN and/or followed by
16244 ** TEMP or TEMPORARY
16245 **
16246 ** (5) TRIGGER We are in the middle of a trigger definition that must be
16247 ** ended by a semicolon, the keyword END, and another semicolon.
16248 **
16249 ** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
16250 ** the end of a trigger definition.
16251 **
16252 ** (7) END We've seen the ";END" of the ";END;" that occurs at the end
16253 ** of a trigger definition.
16254 **
16255 ** Transitions between states above are determined by tokens extracted
16256 ** from the input. The following tokens are significant:
16257 **
16258 ** (0) tkSEMI A semicolon.
16259 ** (1) tkWS Whitespace.
16260 ** (2) tkOTHER Any other SQL token.
16261 ** (3) tkEXPLAIN The "explain" keyword.
16262 ** (4) tkCREATE The "create" keyword.
16263 ** (5) tkTEMP The "temp" or "temporary" keyword.
16264 ** (6) tkTRIGGER The "trigger" keyword.
16265 ** (7) tkEND The "end" keyword.
16266 **
16267 ** Whitespace never causes a state transition and is always ignored.
16268 ** This means that a SQL string of all whitespace is invalid.
16269 **
16270 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
16271 ** to recognize the end of a trigger can be omitted. All we have to do
16272 ** is look for a semicolon that is not part of an string or comment.
16273 */
16274 SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *zSql){
16275 u8 state = 0; /* Current state, using numbers defined in header comment */
16276 u8 token; /* Value of the next token */
16277
16278 #ifndef SQLITE_OMIT_TRIGGER
16279 /* A complex statement machine used to detect the end of a CREATE TRIGGER
16280 ** statement. This is the normal case.
16281 */
16282 static const u8 trans[8][8] = {
16283 /* Token: */
16284 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
16285 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
16286 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
16287 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
16288 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
16289 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
16290 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
16291 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
16292 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
16293 };
16294 #else
16295 /* If triggers are not supported by this compile then the statement machine
16296 ** used to detect the end of a statement is much simpler
16297 */
16298 static const u8 trans[3][3] = {
16299 /* Token: */
16300 /* State: ** SEMI WS OTHER */
16301 /* 0 INVALID: */ { 1, 0, 2, },
16302 /* 1 START: */ { 1, 1, 2, },
16303 /* 2 NORMAL: */ { 1, 2, 2, },
16304 };
16305 #endif /* SQLITE_OMIT_TRIGGER */
16306
16307 #ifdef SQLITE_ENABLE_API_ARMOR
16308 if( zSql==0 ){
16309 (void)SQLITE_MISUSE_BKPT;
16310 return 0;
16311 }
16312 #endif
16313
16314 while( *zSql ){
16315 switch( *zSql ){
16316 case ';': { /* A semicolon */
16317 token = tkSEMI;
16318 break;
16319 }
16320 case ' ':
16321 case '\r':
16322 case '\t':
16323 case '\n':
16324 case '\f': { /* White space is ignored */
16325 token = tkWS;
16326 break;
16327 }
16328 case '/': { /* C-style comments */
16329 if( zSql[1]!='*' ){
16330 token = tkOTHER;
16331 break;
16332 }
16333 zSql += 2;
16334 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
16335 if( zSql[0]==0 ) return 0;
16336 zSql++;
16337 token = tkWS;
16338 break;
16339 }
16340 case '-': { /* SQL-style comments from "--" to end of line */
16341 if( zSql[1]!='-' ){
16342 token = tkOTHER;
16343 break;
16344 }
16345 while( *zSql && *zSql!='\n' ){ zSql++; }
16346 if( *zSql==0 ) return state==1;
16347 token = tkWS;
16348 break;
16349 }
16350 case '[': { /* Microsoft-style identifiers in [...] */
16351 zSql++;
16352 while( *zSql && *zSql!=']' ){ zSql++; }
16353 if( *zSql==0 ) return 0;
16354 token = tkOTHER;
16355 break;
16356 }
16357 case '`': /* Grave-accent quoted symbols used by MySQL */
16358 case '"': /* single- and double-quoted strings */
16359 case '\'': {
16360 int c = *zSql;
16361 zSql++;
16362 while( *zSql && *zSql!=c ){ zSql++; }
16363 if( *zSql==0 ) return 0;
16364 token = tkOTHER;
16365 break;
16366 }
16367 default: {
16368 #ifdef SQLITE_EBCDIC
16369 unsigned char c;
16370 #endif
16371 if( IdChar((u8)*zSql) ){
16372 /* Keywords and unquoted identifiers */
16373 int nId;
16374 for(nId=1; IdChar(zSql[nId]); nId++){}
16375 #ifdef SQLITE_OMIT_TRIGGER
16376 token = tkOTHER;
16377 #else
16378 switch( *zSql ){
16379 case 'c': case 'C': {
16380 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
16381 token = tkCREATE;
16382 }else{
16383 token = tkOTHER;
16384 }
16385 break;
16386 }
16387 case 't': case 'T': {
16388 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
16389 token = tkTRIGGER;
16390 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
16391 token = tkTEMP;
16392 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
16393 token = tkTEMP;
16394 }else{
16395 token = tkOTHER;
16396 }
16397 break;
16398 }
16399 case 'e': case 'E': {
16400 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
16401 token = tkEND;
16402 }else
16403 #ifndef SQLITE_OMIT_EXPLAIN
16404 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
16405 token = tkEXPLAIN;
16406 }else
16407 #endif
16408 {
16409 token = tkOTHER;
16410 }
16411 break;
16412 }
16413 default: {
16414 token = tkOTHER;
16415 break;
16416 }
16417 }
16418 #endif /* SQLITE_OMIT_TRIGGER */
16419 zSql += nId-1;
16420 }else{
16421 /* Operators and special symbols */
16422 token = tkOTHER;
16423 }
16424 break;
16425 }
16426 }
16427 state = trans[state][token];
16428 zSql++;
16429 }
16430 return state==1;
16431 }
16432
16433 #ifndef SQLITE_OMIT_UTF16
16434 /*
16435 ** This routine is the same as the sqlite3_complete() routine described
16436 ** above, except that the parameter is required to be UTF-16 encoded, not
16437 ** UTF-8.
16438 */
16439 SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *zSql){
16440 sqlite3_value *pVal;
16441 char const *zSql8;
16442 int rc;
16443
16444 #ifndef SQLITE_OMIT_AUTOINIT
16445 rc = sqlite3_initialize();
16446 if( rc ) return rc;
16447 #endif
16448 pVal = sqlite3ValueNew(0);
16449 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
16450 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
16451 if( zSql8 ){
16452 rc = sqlite3_complete(zSql8);
16453 }else{
16454 rc = SQLITE_NOMEM;
16455 }
16456 sqlite3ValueFree(pVal);
16457 return rc & 0xff;
16458 }
16459 #endif /* SQLITE_OMIT_UTF16 */
16460 #endif /* SQLITE_OMIT_COMPLETE */
16461
16462 /************** End of complete.c ********************************************/
16463 /************** Begin file main.c ********************************************/
16464 /*
16465 ** 2001 September 15
16466 **
16467 ** The author disclaims copyright to this source code. In place of
16468 ** a legal notice, here is a blessing:
16469 **
16470 ** May you do good and not evil.
16471 ** May you find forgiveness for yourself and forgive others.
16472 ** May you share freely, never taking more than you give.
16473 **
16474 *************************************************************************
16475 ** Main file for the SQLite library. The routines in this file
16476 ** implement the programmer interface to the library. Routines in
16477 ** other files are for internal use by SQLite and should not be
16478 ** accessed by users of the library.
16479 */
16480 /* #include "sqliteInt.h" */
16481
16482 #ifdef SQLITE_ENABLE_FTS3
16483 /************** Include fts3.h in the middle of main.c ***********************/
16484 /************** Begin file fts3.h ********************************************/
16485 /*
16486 ** 2006 Oct 10
16487 **
16488 ** The author disclaims copyright to this source code. In place of
16489 ** a legal notice, here is a blessing:
16490 **
16491 ** May you do good and not evil.
16492 ** May you find forgiveness for yourself and forgive others.
16493 ** May you share freely, never taking more than you give.
16494 **
16495 ******************************************************************************
16496 **
16497 ** This header file is used by programs that want to link against the
16498 ** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
16499 */
16500 /* #include "sqlite3.h" */
16501
16502 #if 0
16503 extern "C" {
16504 #endif /* __cplusplus */
16505
16506 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
16507
16508 #if 0
16509 } /* extern "C" */
16510 #endif /* __cplusplus */
16511
16512 /************** End of fts3.h ************************************************/
16513 /************** Continuing where we left off in main.c ***********************/
16514 #endif
16515 #ifdef SQLITE_ENABLE_RTREE
16516 /************** Include rtree.h in the middle of main.c **********************/
16517 /************** Begin file rtree.h *******************************************/
16518 /*
16519 ** 2008 May 26
16520 **
16521 ** The author disclaims copyright to this source code. In place of
16522 ** a legal notice, here is a blessing:
16523 **
16524 ** May you do good and not evil.
16525 ** May you find forgiveness for yourself and forgive others.
16526 ** May you share freely, never taking more than you give.
16527 **
16528 ******************************************************************************
16529 **
16530 ** This header file is used by programs that want to link against the
16531 ** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
16532 */
16533 /* #include "sqlite3.h" */
16534
16535 #if 0
16536 extern "C" {
16537 #endif /* __cplusplus */
16538
16539 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
16540
16541 #if 0
16542 } /* extern "C" */
16543 #endif /* __cplusplus */
16544
16545 /************** End of rtree.h ***********************************************/
16546 /************** Continuing where we left off in main.c ***********************/
16547 #endif
16548 #ifdef SQLITE_ENABLE_ICU
16549 /************** Include sqliteicu.h in the middle of main.c ******************/
16550 /************** Begin file sqliteicu.h ***************************************/
16551 /*
16552 ** 2008 May 26
16553 **
16554 ** The author disclaims copyright to this source code. In place of
16555 ** a legal notice, here is a blessing:
16556 **
16557 ** May you do good and not evil.
16558 ** May you find forgiveness for yourself and forgive others.
16559 ** May you share freely, never taking more than you give.
16560 **
16561 ******************************************************************************
16562 **
16563 ** This header file is used by programs that want to link against the
16564 ** ICU extension. All it does is declare the sqlite3IcuInit() interface.
16565 */
16566 /* #include "sqlite3.h" */
16567
16568 #if 0
16569 extern "C" {
16570 #endif /* __cplusplus */
16571
16572 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
16573
16574 #if 0
16575 } /* extern "C" */
16576 #endif /* __cplusplus */
16577
16578
16579 /************** End of sqliteicu.h *******************************************/
16580 /************** Continuing where we left off in main.c ***********************/
16581 #endif
16582 #ifdef SQLITE_ENABLE_JSON1
16583 SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*);
16584 #endif
16585 #ifdef SQLITE_ENABLE_FTS5
16586 SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*);
16587 #endif
16588
16589 #ifndef SQLITE_AMALGAMATION
16590 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
16591 ** contains the text of SQLITE_VERSION macro.
16592 */
16593 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
16594 #endif
16595
16596 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
16597 ** a pointer to the to the sqlite3_version[] string constant.
16598 */
16599 SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void){ return sqlite3_v ersion; }
16600
16601 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
16602 ** pointer to a string constant whose value is the same as the
16603 ** SQLITE_SOURCE_ID C preprocessor macro.
16604 */
16605 SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void){ return SQLITE_SOUR CE_ID; }
16606
16607 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
16608 ** returns an integer equal to SQLITE_VERSION_NUMBER.
16609 */
16610 SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void){ return SQLITE_VER SION_NUMBER; }
16611
16612 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
16613 ** zero if and only if SQLite was compiled with mutexing code omitted due to
16614 ** the SQLITE_THREADSAFE compile-time option being set to 0.
16615 */
16616 SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void){ return SQLITE_THREADSAFE ; }
16617
16618 /*
16619 ** When compiling the test fixture or with debugging enabled (on Win32),
16620 ** this variable being set to non-zero will cause OSTRACE macros to emit
16621 ** extra diagnostic information.
16622 */
16623 #ifdef SQLITE_HAVE_OS_TRACE
16624 # ifndef SQLITE_DEBUG_OS_TRACE
16625 # define SQLITE_DEBUG_OS_TRACE 0
16626 # endif
16627 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
16628 #endif
16629
16630 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
16631 /*
16632 ** If the following function pointer is not NULL and if
16633 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
16634 ** I/O active are written using this function. These messages
16635 ** are intended for debugging activity only.
16636 */
16637 SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
16638 #endif
16639
16640 /*
16641 ** If the following global variable points to a string which is the
16642 ** name of a directory, then that directory will be used to store
16643 ** temporary files.
16644 **
16645 ** See also the "PRAGMA temp_store_directory" SQL command.
16646 */
16647 SQLITE_API char *sqlite3_temp_directory = 0;
16648
16649 /*
16650 ** If the following global variable points to a string which is the
16651 ** name of a directory, then that directory will be used to store
16652 ** all database files specified with a relative pathname.
16653 **
16654 ** See also the "PRAGMA data_store_directory" SQL command.
16655 */
16656 SQLITE_API char *sqlite3_data_directory = 0;
16657
16658 /*
16659 ** Initialize SQLite.
16660 **
16661 ** This routine must be called to initialize the memory allocation,
16662 ** VFS, and mutex subsystems prior to doing any serious work with
16663 ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
16664 ** this routine will be called automatically by key routines such as
16665 ** sqlite3_open().
16666 **
16667 ** This routine is a no-op except on its very first call for the process,
16668 ** or for the first call after a call to sqlite3_shutdown.
16669 **
16670 ** The first thread to call this routine runs the initialization to
16671 ** completion. If subsequent threads call this routine before the first
16672 ** thread has finished the initialization process, then the subsequent
16673 ** threads must block until the first thread finishes with the initialization.
16674 **
16675 ** The first thread might call this routine recursively. Recursive
16676 ** calls to this routine should not block, of course. Otherwise the
16677 ** initialization process would never complete.
16678 **
16679 ** Let X be the first thread to enter this routine. Let Y be some other
16680 ** thread. Then while the initial invocation of this routine by X is
16681 ** incomplete, it is required that:
16682 **
16683 ** * Calls to this routine from Y must block until the outer-most
16684 ** call by X completes.
16685 **
16686 ** * Recursive calls to this routine from thread X return immediately
16687 ** without blocking.
16688 */
16689 SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void){
16690 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
16691 int rc; /* Result code */
16692 #ifdef SQLITE_EXTRA_INIT
16693 int bRunExtraInit = 0; /* Extra initialization needed */
16694 #endif
16695
16696 #ifdef SQLITE_OMIT_WSD
16697 rc = sqlite3_wsd_init(4096, 24);
16698 if( rc!=SQLITE_OK ){
16699 return rc;
16700 }
16701 #endif
16702
16703 /* If the following assert() fails on some obscure processor/compiler
16704 ** combination, the work-around is to set the correct pointer
16705 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
16706 assert( SQLITE_PTRSIZE==sizeof(char*) );
16707
16708 /* If SQLite is already completely initialized, then this call
16709 ** to sqlite3_initialize() should be a no-op. But the initialization
16710 ** must be complete. So isInit must not be set until the very end
16711 ** of this routine.
16712 */
16713 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
16714
16715 /* Make sure the mutex subsystem is initialized. If unable to
16716 ** initialize the mutex subsystem, return early with the error.
16717 ** If the system is so sick that we are unable to allocate a mutex,
16718 ** there is not much SQLite is going to be able to do.
16719 **
16720 ** The mutex subsystem must take care of serializing its own
16721 ** initialization.
16722 */
16723 rc = sqlite3MutexInit();
16724 if( rc ) return rc;
16725
16726 /* Initialize the malloc() system and the recursive pInitMutex mutex.
16727 ** This operation is protected by the STATIC_MASTER mutex. Note that
16728 ** MutexAlloc() is called for a static mutex prior to initializing the
16729 ** malloc subsystem - this implies that the allocation of a static
16730 ** mutex must not require support from the malloc subsystem.
16731 */
16732 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
16733 sqlite3_mutex_enter(pMaster);
16734 sqlite3GlobalConfig.isMutexInit = 1;
16735 if( !sqlite3GlobalConfig.isMallocInit ){
16736 rc = sqlite3MallocInit();
16737 }
16738 if( rc==SQLITE_OK ){
16739 sqlite3GlobalConfig.isMallocInit = 1;
16740 if( !sqlite3GlobalConfig.pInitMutex ){
16741 sqlite3GlobalConfig.pInitMutex =
16742 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
16743 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
16744 rc = SQLITE_NOMEM;
16745 }
16746 }
16747 }
16748 if( rc==SQLITE_OK ){
16749 sqlite3GlobalConfig.nRefInitMutex++;
16750 }
16751 sqlite3_mutex_leave(pMaster);
16752
16753 /* If rc is not SQLITE_OK at this point, then either the malloc
16754 ** subsystem could not be initialized or the system failed to allocate
16755 ** the pInitMutex mutex. Return an error in either case. */
16756 if( rc!=SQLITE_OK ){
16757 return rc;
16758 }
16759
16760 /* Do the rest of the initialization under the recursive mutex so
16761 ** that we will be able to handle recursive calls into
16762 ** sqlite3_initialize(). The recursive calls normally come through
16763 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
16764 ** recursive calls might also be possible.
16765 **
16766 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
16767 ** to the xInit method, so the xInit method need not be threadsafe.
16768 **
16769 ** The following mutex is what serializes access to the appdef pcache xInit
16770 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
16771 ** call to sqlite3PcacheInitialize().
16772 */
16773 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
16774 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
16775 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
16776 sqlite3GlobalConfig.inProgress = 1;
16777 #ifdef SQLITE_ENABLE_SQLLOG
16778 {
16779 extern void sqlite3_init_sqllog(void);
16780 sqlite3_init_sqllog();
16781 }
16782 #endif
16783 memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
16784 sqlite3RegisterGlobalFunctions();
16785 if( sqlite3GlobalConfig.isPCacheInit==0 ){
16786 rc = sqlite3PcacheInitialize();
16787 }
16788 if( rc==SQLITE_OK ){
16789 sqlite3GlobalConfig.isPCacheInit = 1;
16790 rc = sqlite3OsInit();
16791 }
16792 if( rc==SQLITE_OK ){
16793 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
16794 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
16795 sqlite3GlobalConfig.isInit = 1;
16796 #ifdef SQLITE_EXTRA_INIT
16797 bRunExtraInit = 1;
16798 #endif
16799 }
16800 sqlite3GlobalConfig.inProgress = 0;
16801 }
16802 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
16803
16804 /* Go back under the static mutex and clean up the recursive
16805 ** mutex to prevent a resource leak.
16806 */
16807 sqlite3_mutex_enter(pMaster);
16808 sqlite3GlobalConfig.nRefInitMutex--;
16809 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
16810 assert( sqlite3GlobalConfig.nRefInitMutex==0 );
16811 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
16812 sqlite3GlobalConfig.pInitMutex = 0;
16813 }
16814 sqlite3_mutex_leave(pMaster);
16815
16816 /* The following is just a sanity check to make sure SQLite has
16817 ** been compiled correctly. It is important to run this code, but
16818 ** we don't want to run it too often and soak up CPU cycles for no
16819 ** reason. So we run it once during initialization.
16820 */
16821 #ifndef NDEBUG
16822 #ifndef SQLITE_OMIT_FLOATING_POINT
16823 /* This section of code's only "output" is via assert() statements. */
16824 if ( rc==SQLITE_OK ){
16825 u64 x = (((u64)1)<<63)-1;
16826 double y;
16827 assert(sizeof(x)==8);
16828 assert(sizeof(x)==sizeof(y));
16829 memcpy(&y, &x, 8);
16830 assert( sqlite3IsNaN(y) );
16831 }
16832 #endif
16833 #endif
16834
16835 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
16836 ** compile-time option.
16837 */
16838 #ifdef SQLITE_EXTRA_INIT
16839 if( bRunExtraInit ){
16840 int SQLITE_EXTRA_INIT(const char*);
16841 rc = SQLITE_EXTRA_INIT(0);
16842 }
16843 #endif
16844
16845 return rc;
16846 }
16847
16848 /*
16849 ** Undo the effects of sqlite3_initialize(). Must not be called while
16850 ** there are outstanding database connections or memory allocations or
16851 ** while any part of SQLite is otherwise in use in any thread. This
16852 ** routine is not threadsafe. But it is safe to invoke this routine
16853 ** on when SQLite is already shut down. If SQLite is already shut down
16854 ** when this routine is invoked, then this routine is a harmless no-op.
16855 */
16856 SQLITE_API int SQLITE_STDCALL sqlite3_shutdown(void){
16857 #ifdef SQLITE_OMIT_WSD
16858 int rc = sqlite3_wsd_init(4096, 24);
16859 if( rc!=SQLITE_OK ){
16860 return rc;
16861 }
16862 #endif
16863
16864 if( sqlite3GlobalConfig.isInit ){
16865 #ifdef SQLITE_EXTRA_SHUTDOWN
16866 void SQLITE_EXTRA_SHUTDOWN(void);
16867 SQLITE_EXTRA_SHUTDOWN();
16868 #endif
16869 sqlite3_os_end();
16870 sqlite3_reset_auto_extension();
16871 sqlite3GlobalConfig.isInit = 0;
16872 }
16873 if( sqlite3GlobalConfig.isPCacheInit ){
16874 sqlite3PcacheShutdown();
16875 sqlite3GlobalConfig.isPCacheInit = 0;
16876 }
16877 if( sqlite3GlobalConfig.isMallocInit ){
16878 sqlite3MallocEnd();
16879 sqlite3GlobalConfig.isMallocInit = 0;
16880
16881 #ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
16882 /* The heap subsystem has now been shutdown and these values are supposed
16883 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
16884 ** which would rely on that heap subsystem; therefore, make sure these
16885 ** values cannot refer to heap memory that was just invalidated when the
16886 ** heap subsystem was shutdown. This is only done if the current call to
16887 ** this function resulted in the heap subsystem actually being shutdown.
16888 */
16889 sqlite3_data_directory = 0;
16890 sqlite3_temp_directory = 0;
16891 #endif
16892 }
16893 if( sqlite3GlobalConfig.isMutexInit ){
16894 sqlite3MutexEnd();
16895 sqlite3GlobalConfig.isMutexInit = 0;
16896 }
16897
16898 return SQLITE_OK;
16899 }
16900
16901 /*
16902 ** This API allows applications to modify the global configuration of
16903 ** the SQLite library at run-time.
16904 **
16905 ** This routine should only be called when there are no outstanding
16906 ** database connections or memory allocations. This routine is not
16907 ** threadsafe. Failure to heed these warnings can lead to unpredictable
16908 ** behavior.
16909 */
16910 SQLITE_API int SQLITE_CDECL sqlite3_config(int op, ...){
16911 va_list ap;
16912 int rc = SQLITE_OK;
16913
16914 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
16915 ** the SQLite library is in use. */
16916 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
16917
16918 va_start(ap, op);
16919 switch( op ){
16920
16921 /* Mutex configuration options are only available in a threadsafe
16922 ** compile.
16923 */
16924 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
16925 case SQLITE_CONFIG_SINGLETHREAD: {
16926 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
16927 ** Single-thread. */
16928 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */
16929 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
16930 break;
16931 }
16932 #endif
16933 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
16934 case SQLITE_CONFIG_MULTITHREAD: {
16935 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
16936 ** Multi-thread. */
16937 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
16938 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
16939 break;
16940 }
16941 #endif
16942 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
16943 case SQLITE_CONFIG_SERIALIZED: {
16944 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
16945 ** Serialized. */
16946 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
16947 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */
16948 break;
16949 }
16950 #endif
16951 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
16952 case SQLITE_CONFIG_MUTEX: {
16953 /* Specify an alternative mutex implementation */
16954 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
16955 break;
16956 }
16957 #endif
16958 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
16959 case SQLITE_CONFIG_GETMUTEX: {
16960 /* Retrieve the current mutex implementation */
16961 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
16962 break;
16963 }
16964 #endif
16965
16966 case SQLITE_CONFIG_MALLOC: {
16967 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
16968 ** single argument which is a pointer to an instance of the
16969 ** sqlite3_mem_methods structure. The argument specifies alternative
16970 ** low-level memory allocation routines to be used in place of the memory
16971 ** allocation routines built into SQLite. */
16972 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
16973 break;
16974 }
16975 case SQLITE_CONFIG_GETMALLOC: {
16976 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
16977 ** single argument which is a pointer to an instance of the
16978 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
16979 ** filled with the currently defined memory allocation routines. */
16980 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
16981 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
16982 break;
16983 }
16984 case SQLITE_CONFIG_MEMSTATUS: {
16985 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
16986 ** single argument of type int, interpreted as a boolean, which enables
16987 ** or disables the collection of memory allocation statistics. */
16988 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
16989 break;
16990 }
16991 case SQLITE_CONFIG_SCRATCH: {
16992 /* EVIDENCE-OF: R-08404-60887 There are three arguments to
16993 ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from
16994 ** which the scratch allocations will be drawn, the size of each scratch
16995 ** allocation (sz), and the maximum number of scratch allocations (N). */
16996 sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
16997 sqlite3GlobalConfig.szScratch = va_arg(ap, int);
16998 sqlite3GlobalConfig.nScratch = va_arg(ap, int);
16999 break;
17000 }
17001 case SQLITE_CONFIG_PAGECACHE: {
17002 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
17003 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
17004 ** the size of each page cache line (sz), and the number of cache lines
17005 ** (N). */
17006 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
17007 sqlite3GlobalConfig.szPage = va_arg(ap, int);
17008 sqlite3GlobalConfig.nPage = va_arg(ap, int);
17009 break;
17010 }
17011 case SQLITE_CONFIG_PCACHE_HDRSZ: {
17012 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
17013 ** a single parameter which is a pointer to an integer and writes into
17014 ** that integer the number of extra bytes per page required for each page
17015 ** in SQLITE_CONFIG_PAGECACHE. */
17016 *va_arg(ap, int*) =
17017 sqlite3HeaderSizeBtree() +
17018 sqlite3HeaderSizePcache() +
17019 sqlite3HeaderSizePcache1();
17020 break;
17021 }
17022
17023 case SQLITE_CONFIG_PCACHE: {
17024 /* no-op */
17025 break;
17026 }
17027 case SQLITE_CONFIG_GETPCACHE: {
17028 /* now an error */
17029 rc = SQLITE_ERROR;
17030 break;
17031 }
17032
17033 case SQLITE_CONFIG_PCACHE2: {
17034 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
17035 ** single argument which is a pointer to an sqlite3_pcache_methods2
17036 ** object. This object specifies the interface to a custom page cache
17037 ** implementation. */
17038 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
17039 break;
17040 }
17041 case SQLITE_CONFIG_GETPCACHE2: {
17042 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
17043 ** single argument which is a pointer to an sqlite3_pcache_methods2
17044 ** object. SQLite copies of the current page cache implementation into
17045 ** that object. */
17046 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
17047 sqlite3PCacheSetDefault();
17048 }
17049 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
17050 break;
17051 }
17052
17053 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
17054 ** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
17055 ** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
17056 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
17057 case SQLITE_CONFIG_HEAP: {
17058 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
17059 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
17060 ** number of bytes in the memory buffer, and the minimum allocation size.
17061 */
17062 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
17063 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
17064 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
17065
17066 if( sqlite3GlobalConfig.mnReq<1 ){
17067 sqlite3GlobalConfig.mnReq = 1;
17068 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
17069 /* cap min request size at 2^12 */
17070 sqlite3GlobalConfig.mnReq = (1<<12);
17071 }
17072
17073 if( sqlite3GlobalConfig.pHeap==0 ){
17074 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
17075 ** is NULL, then SQLite reverts to using its default memory allocator
17076 ** (the system malloc() implementation), undoing any prior invocation of
17077 ** SQLITE_CONFIG_MALLOC.
17078 **
17079 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
17080 ** revert to its default implementation when sqlite3_initialize() is run
17081 */
17082 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
17083 }else{
17084 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
17085 ** alternative memory allocator is engaged to handle all of SQLites
17086 ** memory allocation needs. */
17087 #ifdef SQLITE_ENABLE_MEMSYS3
17088 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
17089 #endif
17090 #ifdef SQLITE_ENABLE_MEMSYS5
17091 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
17092 #endif
17093 }
17094 break;
17095 }
17096 #endif
17097
17098 case SQLITE_CONFIG_LOOKASIDE: {
17099 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
17100 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
17101 break;
17102 }
17103
17104 /* Record a pointer to the logger function and its first argument.
17105 ** The default is NULL. Logging is disabled if the function pointer is
17106 ** NULL.
17107 */
17108 case SQLITE_CONFIG_LOG: {
17109 /* MSVC is picky about pulling func ptrs from va lists.
17110 ** http://support.microsoft.com/kb/47961
17111 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
17112 */
17113 typedef void(*LOGFUNC_t)(void*,int,const char*);
17114 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
17115 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
17116 break;
17117 }
17118
17119 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
17120 ** can be changed at start-time using the
17121 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
17122 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
17123 */
17124 case SQLITE_CONFIG_URI: {
17125 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
17126 ** argument of type int. If non-zero, then URI handling is globally
17127 ** enabled. If the parameter is zero, then URI handling is globally
17128 ** disabled. */
17129 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
17130 break;
17131 }
17132
17133 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
17134 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
17135 ** option takes a single integer argument which is interpreted as a
17136 ** boolean in order to enable or disable the use of covering indices for
17137 ** full table scans in the query optimizer. */
17138 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
17139 break;
17140 }
17141
17142 #ifdef SQLITE_ENABLE_SQLLOG
17143 case SQLITE_CONFIG_SQLLOG: {
17144 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
17145 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
17146 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
17147 break;
17148 }
17149 #endif
17150
17151 case SQLITE_CONFIG_MMAP_SIZE: {
17152 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
17153 ** integer (sqlite3_int64) values that are the default mmap size limit
17154 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
17155 ** mmap size limit. */
17156 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
17157 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
17158 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
17159 ** negative, then that argument is changed to its compile-time default.
17160 **
17161 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
17162 ** silently truncated if necessary so that it does not exceed the
17163 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
17164 ** compile-time option.
17165 */
17166 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
17167 mxMmap = SQLITE_MAX_MMAP_SIZE;
17168 }
17169 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
17170 if( szMmap>mxMmap) szMmap = mxMmap;
17171 sqlite3GlobalConfig.mxMmap = mxMmap;
17172 sqlite3GlobalConfig.szMmap = szMmap;
17173 break;
17174 }
17175
17176 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
17177 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
17178 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
17179 ** unsigned integer value that specifies the maximum size of the created
17180 ** heap. */
17181 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
17182 break;
17183 }
17184 #endif
17185
17186 case SQLITE_CONFIG_PMASZ: {
17187 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
17188 break;
17189 }
17190
17191 default: {
17192 rc = SQLITE_ERROR;
17193 break;
17194 }
17195 }
17196 va_end(ap);
17197 return rc;
17198 }
17199
17200 /*
17201 ** Set up the lookaside buffers for a database connection.
17202 ** Return SQLITE_OK on success.
17203 ** If lookaside is already active, return SQLITE_BUSY.
17204 **
17205 ** The sz parameter is the number of bytes in each lookaside slot.
17206 ** The cnt parameter is the number of slots. If pStart is NULL the
17207 ** space for the lookaside memory is obtained from sqlite3_malloc().
17208 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
17209 ** the lookaside memory.
17210 */
17211 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
17212 #ifndef SQLITE_OMIT_LOOKASIDE
17213 void *pStart;
17214 if( db->lookaside.nOut ){
17215 return SQLITE_BUSY;
17216 }
17217 /* Free any existing lookaside buffer for this handle before
17218 ** allocating a new one so we don't have to have space for
17219 ** both at the same time.
17220 */
17221 if( db->lookaside.bMalloced ){
17222 sqlite3_free(db->lookaside.pStart);
17223 }
17224 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
17225 ** than a pointer to be useful.
17226 */
17227 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
17228 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
17229 if( cnt<0 ) cnt = 0;
17230 if( sz==0 || cnt==0 ){
17231 sz = 0;
17232 pStart = 0;
17233 }else if( pBuf==0 ){
17234 sqlite3BeginBenignMalloc();
17235 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
17236 sqlite3EndBenignMalloc();
17237 if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
17238 }else{
17239 pStart = pBuf;
17240 }
17241 db->lookaside.pStart = pStart;
17242 db->lookaside.pFree = 0;
17243 db->lookaside.sz = (u16)sz;
17244 if( pStart ){
17245 int i;
17246 LookasideSlot *p;
17247 assert( sz > (int)sizeof(LookasideSlot*) );
17248 p = (LookasideSlot*)pStart;
17249 for(i=cnt-1; i>=0; i--){
17250 p->pNext = db->lookaside.pFree;
17251 db->lookaside.pFree = p;
17252 p = (LookasideSlot*)&((u8*)p)[sz];
17253 }
17254 db->lookaside.pEnd = p;
17255 db->lookaside.bEnabled = 1;
17256 db->lookaside.bMalloced = pBuf==0 ?1:0;
17257 }else{
17258 db->lookaside.pStart = db;
17259 db->lookaside.pEnd = db;
17260 db->lookaside.bEnabled = 0;
17261 db->lookaside.bMalloced = 0;
17262 }
17263 #endif /* SQLITE_OMIT_LOOKASIDE */
17264 return SQLITE_OK;
17265 }
17266
17267 /*
17268 ** Return the mutex associated with a database connection.
17269 */
17270 SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3 *db){
17271 #ifdef SQLITE_ENABLE_API_ARMOR
17272 if( !sqlite3SafetyCheckOk(db) ){
17273 (void)SQLITE_MISUSE_BKPT;
17274 return 0;
17275 }
17276 #endif
17277 return db->mutex;
17278 }
17279
17280 /*
17281 ** Free up as much memory as we can from the given database
17282 ** connection.
17283 */
17284 SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3 *db){
17285 int i;
17286
17287 #ifdef SQLITE_ENABLE_API_ARMOR
17288 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
17289 #endif
17290 sqlite3_mutex_enter(db->mutex);
17291 sqlite3BtreeEnterAll(db);
17292 for(i=0; i<db->nDb; i++){
17293 Btree *pBt = db->aDb[i].pBt;
17294 if( pBt ){
17295 Pager *pPager = sqlite3BtreePager(pBt);
17296 sqlite3PagerShrink(pPager);
17297 }
17298 }
17299 sqlite3BtreeLeaveAll(db);
17300 sqlite3_mutex_leave(db->mutex);
17301 return SQLITE_OK;
17302 }
17303
17304 /*
17305 ** Flush any dirty pages in the pager-cache for any attached database
17306 ** to disk.
17307 */
17308 SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3 *db){
17309 int i;
17310 int rc = SQLITE_OK;
17311 int bSeenBusy = 0;
17312
17313 #ifdef SQLITE_ENABLE_API_ARMOR
17314 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
17315 #endif
17316 sqlite3_mutex_enter(db->mutex);
17317 sqlite3BtreeEnterAll(db);
17318 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
17319 Btree *pBt = db->aDb[i].pBt;
17320 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
17321 Pager *pPager = sqlite3BtreePager(pBt);
17322 rc = sqlite3PagerFlush(pPager);
17323 if( rc==SQLITE_BUSY ){
17324 bSeenBusy = 1;
17325 rc = SQLITE_OK;
17326 }
17327 }
17328 }
17329 sqlite3BtreeLeaveAll(db);
17330 sqlite3_mutex_leave(db->mutex);
17331 return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
17332 }
17333
17334 /*
17335 ** Configuration settings for an individual database connection
17336 */
17337 SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3 *db, int op, ...){
17338 va_list ap;
17339 int rc;
17340 va_start(ap, op);
17341 switch( op ){
17342 case SQLITE_DBCONFIG_LOOKASIDE: {
17343 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
17344 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
17345 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
17346 rc = setupLookaside(db, pBuf, sz, cnt);
17347 break;
17348 }
17349 default: {
17350 static const struct {
17351 int op; /* The opcode */
17352 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
17353 } aFlagOp[] = {
17354 { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys },
17355 { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger },
17356 };
17357 unsigned int i;
17358 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
17359 for(i=0; i<ArraySize(aFlagOp); i++){
17360 if( aFlagOp[i].op==op ){
17361 int onoff = va_arg(ap, int);
17362 int *pRes = va_arg(ap, int*);
17363 int oldFlags = db->flags;
17364 if( onoff>0 ){
17365 db->flags |= aFlagOp[i].mask;
17366 }else if( onoff==0 ){
17367 db->flags &= ~aFlagOp[i].mask;
17368 }
17369 if( oldFlags!=db->flags ){
17370 sqlite3ExpirePreparedStatements(db);
17371 }
17372 if( pRes ){
17373 *pRes = (db->flags & aFlagOp[i].mask)!=0;
17374 }
17375 rc = SQLITE_OK;
17376 break;
17377 }
17378 }
17379 break;
17380 }
17381 }
17382 va_end(ap);
17383 return rc;
17384 }
17385
17386
17387 /*
17388 ** Return true if the buffer z[0..n-1] contains all spaces.
17389 */
17390 static int allSpaces(const char *z, int n){
17391 while( n>0 && z[n-1]==' ' ){ n--; }
17392 return n==0;
17393 }
17394
17395 /*
17396 ** This is the default collating function named "BINARY" which is always
17397 ** available.
17398 **
17399 ** If the padFlag argument is not NULL then space padding at the end
17400 ** of strings is ignored. This implements the RTRIM collation.
17401 */
17402 static int binCollFunc(
17403 void *padFlag,
17404 int nKey1, const void *pKey1,
17405 int nKey2, const void *pKey2
17406 ){
17407 int rc, n;
17408 n = nKey1<nKey2 ? nKey1 : nKey2;
17409 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
17410 ** strings byte by byte using the memcmp() function from the standard C
17411 ** library. */
17412 rc = memcmp(pKey1, pKey2, n);
17413 if( rc==0 ){
17414 if( padFlag
17415 && allSpaces(((char*)pKey1)+n, nKey1-n)
17416 && allSpaces(((char*)pKey2)+n, nKey2-n)
17417 ){
17418 /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra
17419 ** spaces at the end of either string do not change the result. In other
17420 ** words, strings will compare equal to one another as long as they
17421 ** differ only in the number of spaces at the end.
17422 */
17423 }else{
17424 rc = nKey1 - nKey2;
17425 }
17426 }
17427 return rc;
17428 }
17429
17430 /*
17431 ** Another built-in collating sequence: NOCASE.
17432 **
17433 ** This collating sequence is intended to be used for "case independent
17434 ** comparison". SQLite's knowledge of upper and lower case equivalents
17435 ** extends only to the 26 characters used in the English language.
17436 **
17437 ** At the moment there is only a UTF-8 implementation.
17438 */
17439 static int nocaseCollatingFunc(
17440 void *NotUsed,
17441 int nKey1, const void *pKey1,
17442 int nKey2, const void *pKey2
17443 ){
17444 int r = sqlite3StrNICmp(
17445 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
17446 UNUSED_PARAMETER(NotUsed);
17447 if( 0==r ){
17448 r = nKey1-nKey2;
17449 }
17450 return r;
17451 }
17452
17453 /*
17454 ** Return the ROWID of the most recent insert
17455 */
17456 SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3 *db){
17457 #ifdef SQLITE_ENABLE_API_ARMOR
17458 if( !sqlite3SafetyCheckOk(db) ){
17459 (void)SQLITE_MISUSE_BKPT;
17460 return 0;
17461 }
17462 #endif
17463 return db->lastRowid;
17464 }
17465
17466 /*
17467 ** Return the number of changes in the most recent call to sqlite3_exec().
17468 */
17469 SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3 *db){
17470 #ifdef SQLITE_ENABLE_API_ARMOR
17471 if( !sqlite3SafetyCheckOk(db) ){
17472 (void)SQLITE_MISUSE_BKPT;
17473 return 0;
17474 }
17475 #endif
17476 return db->nChange;
17477 }
17478
17479 /*
17480 ** Return the number of changes since the database handle was opened.
17481 */
17482 SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3 *db){
17483 #ifdef SQLITE_ENABLE_API_ARMOR
17484 if( !sqlite3SafetyCheckOk(db) ){
17485 (void)SQLITE_MISUSE_BKPT;
17486 return 0;
17487 }
17488 #endif
17489 return db->nTotalChange;
17490 }
17491
17492 /*
17493 ** Close all open savepoints. This function only manipulates fields of the
17494 ** database handle object, it does not close any savepoints that may be open
17495 ** at the b-tree/pager level.
17496 */
17497 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
17498 while( db->pSavepoint ){
17499 Savepoint *pTmp = db->pSavepoint;
17500 db->pSavepoint = pTmp->pNext;
17501 sqlite3DbFree(db, pTmp);
17502 }
17503 db->nSavepoint = 0;
17504 db->nStatement = 0;
17505 db->isTransactionSavepoint = 0;
17506 }
17507
17508 /*
17509 ** Invoke the destructor function associated with FuncDef p, if any. Except,
17510 ** if this is not the last copy of the function, do not invoke it. Multiple
17511 ** copies of a single function are created when create_function() is called
17512 ** with SQLITE_ANY as the encoding.
17513 */
17514 static void functionDestroy(sqlite3 *db, FuncDef *p){
17515 FuncDestructor *pDestructor = p->pDestructor;
17516 if( pDestructor ){
17517 pDestructor->nRef--;
17518 if( pDestructor->nRef==0 ){
17519 pDestructor->xDestroy(pDestructor->pUserData);
17520 sqlite3DbFree(db, pDestructor);
17521 }
17522 }
17523 }
17524
17525 /*
17526 ** Disconnect all sqlite3_vtab objects that belong to database connection
17527 ** db. This is called when db is being closed.
17528 */
17529 static void disconnectAllVtab(sqlite3 *db){
17530 #ifndef SQLITE_OMIT_VIRTUALTABLE
17531 int i;
17532 HashElem *p;
17533 sqlite3BtreeEnterAll(db);
17534 for(i=0; i<db->nDb; i++){
17535 Schema *pSchema = db->aDb[i].pSchema;
17536 if( db->aDb[i].pSchema ){
17537 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
17538 Table *pTab = (Table *)sqliteHashData(p);
17539 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
17540 }
17541 }
17542 }
17543 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
17544 Module *pMod = (Module *)sqliteHashData(p);
17545 if( pMod->pEpoTab ){
17546 sqlite3VtabDisconnect(db, pMod->pEpoTab);
17547 }
17548 }
17549 sqlite3VtabUnlockList(db);
17550 sqlite3BtreeLeaveAll(db);
17551 #else
17552 UNUSED_PARAMETER(db);
17553 #endif
17554 }
17555
17556 /*
17557 ** Return TRUE if database connection db has unfinalized prepared
17558 ** statements or unfinished sqlite3_backup objects.
17559 */
17560 static int connectionIsBusy(sqlite3 *db){
17561 int j;
17562 assert( sqlite3_mutex_held(db->mutex) );
17563 if( db->pVdbe ) return 1;
17564 for(j=0; j<db->nDb; j++){
17565 Btree *pBt = db->aDb[j].pBt;
17566 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
17567 }
17568 return 0;
17569 }
17570
17571 /*
17572 ** Close an existing SQLite database
17573 */
17574 static int sqlite3Close(sqlite3 *db, int forceZombie){
17575 if( !db ){
17576 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
17577 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
17578 return SQLITE_OK;
17579 }
17580 if( !sqlite3SafetyCheckSickOrOk(db) ){
17581 return SQLITE_MISUSE_BKPT;
17582 }
17583 sqlite3_mutex_enter(db->mutex);
17584
17585 /* Force xDisconnect calls on all virtual tables */
17586 disconnectAllVtab(db);
17587
17588 /* If a transaction is open, the disconnectAllVtab() call above
17589 ** will not have called the xDisconnect() method on any virtual
17590 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
17591 ** call will do so. We need to do this before the check for active
17592 ** SQL statements below, as the v-table implementation may be storing
17593 ** some prepared statements internally.
17594 */
17595 sqlite3VtabRollback(db);
17596
17597 /* Legacy behavior (sqlite3_close() behavior) is to return
17598 ** SQLITE_BUSY if the connection can not be closed immediately.
17599 */
17600 if( !forceZombie && connectionIsBusy(db) ){
17601 sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
17602 "statements or unfinished backups");
17603 sqlite3_mutex_leave(db->mutex);
17604 return SQLITE_BUSY;
17605 }
17606
17607 #ifdef SQLITE_ENABLE_SQLLOG
17608 if( sqlite3GlobalConfig.xSqllog ){
17609 /* Closing the handle. Fourth parameter is passed the value 2. */
17610 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
17611 }
17612 #endif
17613
17614 /* Convert the connection into a zombie and then close it.
17615 */
17616 db->magic = SQLITE_MAGIC_ZOMBIE;
17617 sqlite3LeaveMutexAndCloseZombie(db);
17618 return SQLITE_OK;
17619 }
17620
17621 /*
17622 ** Two variations on the public interface for closing a database
17623 ** connection. The sqlite3_close() version returns SQLITE_BUSY and
17624 ** leaves the connection option if there are unfinalized prepared
17625 ** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
17626 ** version forces the connection to become a zombie if there are
17627 ** unclosed resources, and arranges for deallocation when the last
17628 ** prepare statement or sqlite3_backup closes.
17629 */
17630 SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3 *db){ return sqlite3Close(db ,0); }
17631 SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3 *db){ return sqlite3Close (db,1); }
17632
17633
17634 /*
17635 ** Close the mutex on database connection db.
17636 **
17637 ** Furthermore, if database connection db is a zombie (meaning that there
17638 ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
17639 ** every sqlite3_stmt has now been finalized and every sqlite3_backup has
17640 ** finished, then free all resources.
17641 */
17642 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
17643 HashElem *i; /* Hash table iterator */
17644 int j;
17645
17646 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
17647 ** or if the connection has not yet been closed by sqlite3_close_v2(),
17648 ** then just leave the mutex and return.
17649 */
17650 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
17651 sqlite3_mutex_leave(db->mutex);
17652 return;
17653 }
17654
17655 /* If we reach this point, it means that the database connection has
17656 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
17657 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
17658 ** go ahead and free all resources.
17659 */
17660
17661 /* If a transaction is open, roll it back. This also ensures that if
17662 ** any database schemas have been modified by an uncommitted transaction
17663 ** they are reset. And that the required b-tree mutex is held to make
17664 ** the pager rollback and schema reset an atomic operation. */
17665 sqlite3RollbackAll(db, SQLITE_OK);
17666
17667 /* Free any outstanding Savepoint structures. */
17668 sqlite3CloseSavepoints(db);
17669
17670 /* Close all database connections */
17671 for(j=0; j<db->nDb; j++){
17672 struct Db *pDb = &db->aDb[j];
17673 if( pDb->pBt ){
17674 sqlite3BtreeClose(pDb->pBt);
17675 pDb->pBt = 0;
17676 if( j!=1 ){
17677 pDb->pSchema = 0;
17678 }
17679 }
17680 }
17681 /* Clear the TEMP schema separately and last */
17682 if( db->aDb[1].pSchema ){
17683 sqlite3SchemaClear(db->aDb[1].pSchema);
17684 }
17685 sqlite3VtabUnlockList(db);
17686
17687 /* Free up the array of auxiliary databases */
17688 sqlite3CollapseDatabaseArray(db);
17689 assert( db->nDb<=2 );
17690 assert( db->aDb==db->aDbStatic );
17691
17692 /* Tell the code in notify.c that the connection no longer holds any
17693 ** locks and does not require any further unlock-notify callbacks.
17694 */
17695 sqlite3ConnectionClosed(db);
17696
17697 for(j=0; j<ArraySize(db->aFunc.a); j++){
17698 FuncDef *pNext, *pHash, *p;
17699 for(p=db->aFunc.a[j]; p; p=pHash){
17700 pHash = p->pHash;
17701 while( p ){
17702 functionDestroy(db, p);
17703 pNext = p->pNext;
17704 sqlite3DbFree(db, p);
17705 p = pNext;
17706 }
17707 }
17708 }
17709 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
17710 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
17711 /* Invoke any destructors registered for collation sequence user data. */
17712 for(j=0; j<3; j++){
17713 if( pColl[j].xDel ){
17714 pColl[j].xDel(pColl[j].pUser);
17715 }
17716 }
17717 sqlite3DbFree(db, pColl);
17718 }
17719 sqlite3HashClear(&db->aCollSeq);
17720 #ifndef SQLITE_OMIT_VIRTUALTABLE
17721 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
17722 Module *pMod = (Module *)sqliteHashData(i);
17723 if( pMod->xDestroy ){
17724 pMod->xDestroy(pMod->pAux);
17725 }
17726 sqlite3VtabEponymousTableClear(db, pMod);
17727 sqlite3DbFree(db, pMod);
17728 }
17729 sqlite3HashClear(&db->aModule);
17730 #endif
17731
17732 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
17733 sqlite3ValueFree(db->pErr);
17734 sqlite3CloseExtensions(db);
17735 #if SQLITE_USER_AUTHENTICATION
17736 sqlite3_free(db->auth.zAuthUser);
17737 sqlite3_free(db->auth.zAuthPW);
17738 #endif
17739
17740 db->magic = SQLITE_MAGIC_ERROR;
17741
17742 /* The temp-database schema is allocated differently from the other schema
17743 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
17744 ** So it needs to be freed here. Todo: Why not roll the temp schema into
17745 ** the same sqliteMalloc() as the one that allocates the database
17746 ** structure?
17747 */
17748 sqlite3DbFree(db, db->aDb[1].pSchema);
17749 sqlite3_mutex_leave(db->mutex);
17750 db->magic = SQLITE_MAGIC_CLOSED;
17751 sqlite3_mutex_free(db->mutex);
17752 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */
17753 if( db->lookaside.bMalloced ){
17754 sqlite3_free(db->lookaside.pStart);
17755 }
17756 sqlite3_free(db);
17757 }
17758
17759 /*
17760 ** Rollback all database files. If tripCode is not SQLITE_OK, then
17761 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit
17762 ** breaker") and made to return tripCode if there are any further
17763 ** attempts to use that cursor. Read cursors remain open and valid
17764 ** but are "saved" in case the table pages are moved around.
17765 */
17766 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
17767 int i;
17768 int inTrans = 0;
17769 int schemaChange;
17770 assert( sqlite3_mutex_held(db->mutex) );
17771 sqlite3BeginBenignMalloc();
17772
17773 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
17774 ** This is important in case the transaction being rolled back has
17775 ** modified the database schema. If the b-tree mutexes are not taken
17776 ** here, then another shared-cache connection might sneak in between
17777 ** the database rollback and schema reset, which can cause false
17778 ** corruption reports in some cases. */
17779 sqlite3BtreeEnterAll(db);
17780 schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0;
17781
17782 for(i=0; i<db->nDb; i++){
17783 Btree *p = db->aDb[i].pBt;
17784 if( p ){
17785 if( sqlite3BtreeIsInTrans(p) ){
17786 inTrans = 1;
17787 }
17788 sqlite3BtreeRollback(p, tripCode, !schemaChange);
17789 }
17790 }
17791 sqlite3VtabRollback(db);
17792 sqlite3EndBenignMalloc();
17793
17794 if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
17795 sqlite3ExpirePreparedStatements(db);
17796 sqlite3ResetAllSchemasOfConnection(db);
17797 }
17798 sqlite3BtreeLeaveAll(db);
17799
17800 /* Any deferred constraint violations have now been resolved. */
17801 db->nDeferredCons = 0;
17802 db->nDeferredImmCons = 0;
17803 db->flags &= ~SQLITE_DeferFKs;
17804
17805 /* If one has been configured, invoke the rollback-hook callback */
17806 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
17807 db->xRollbackCallback(db->pRollbackArg);
17808 }
17809 }
17810
17811 /*
17812 ** Return a static string containing the name corresponding to the error code
17813 ** specified in the argument.
17814 */
17815 #if defined(SQLITE_NEED_ERR_NAME)
17816 SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
17817 const char *zName = 0;
17818 int i, origRc = rc;
17819 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
17820 switch( rc ){
17821 case SQLITE_OK: zName = "SQLITE_OK"; break;
17822 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
17823 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
17824 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
17825 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
17826 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break;
17827 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
17828 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break;
17829 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break;
17830 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
17831 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
17832 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
17833 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
17834 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
17835 case SQLITE_READONLY_CANTLOCK: zName = "SQLITE_READONLY_CANTLOCK"; break;
17836 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
17837 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
17838 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
17839 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
17840 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
17841 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
17842 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
17843 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
17844 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
17845 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
17846 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
17847 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
17848 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
17849 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
17850 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
17851 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
17852 case SQLITE_IOERR_CHECKRESERVEDLOCK:
17853 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
17854 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
17855 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break;
17856 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break;
17857 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break;
17858 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break;
17859 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break;
17860 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
17861 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
17862 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
17863 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
17864 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
17865 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
17866 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
17867 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
17868 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
17869 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
17870 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
17871 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
17872 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
17873 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
17874 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
17875 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
17876 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
17877 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
17878 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
17879 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
17880 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
17881 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
17882 case SQLITE_CONSTRAINT_FOREIGNKEY:
17883 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
17884 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break;
17885 case SQLITE_CONSTRAINT_PRIMARYKEY:
17886 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
17887 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
17888 case SQLITE_CONSTRAINT_COMMITHOOK:
17889 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
17890 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break;
17891 case SQLITE_CONSTRAINT_FUNCTION:
17892 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
17893 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break;
17894 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
17895 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
17896 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
17897 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
17898 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
17899 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
17900 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
17901 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
17902 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break;
17903 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
17904 case SQLITE_NOTICE_RECOVER_ROLLBACK:
17905 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
17906 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break;
17907 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break;
17908 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
17909 }
17910 }
17911 if( zName==0 ){
17912 static char zBuf[50];
17913 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
17914 zName = zBuf;
17915 }
17916 return zName;
17917 }
17918 #endif
17919
17920 /*
17921 ** Return a static string that describes the kind of error specified in the
17922 ** argument.
17923 */
17924 SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
17925 static const char* const aMsg[] = {
17926 /* SQLITE_OK */ "not an error",
17927 /* SQLITE_ERROR */ "SQL logic error or missing database",
17928 /* SQLITE_INTERNAL */ 0,
17929 /* SQLITE_PERM */ "access permission denied",
17930 /* SQLITE_ABORT */ "callback requested query abort",
17931 /* SQLITE_BUSY */ "database is locked",
17932 /* SQLITE_LOCKED */ "database table is locked",
17933 /* SQLITE_NOMEM */ "out of memory",
17934 /* SQLITE_READONLY */ "attempt to write a readonly database",
17935 /* SQLITE_INTERRUPT */ "interrupted",
17936 /* SQLITE_IOERR */ "disk I/O error",
17937 /* SQLITE_CORRUPT */ "database disk image is malformed",
17938 /* SQLITE_NOTFOUND */ "unknown operation",
17939 /* SQLITE_FULL */ "database or disk is full",
17940 /* SQLITE_CANTOPEN */ "unable to open database file",
17941 /* SQLITE_PROTOCOL */ "locking protocol",
17942 /* SQLITE_EMPTY */ "table contains no data",
17943 /* SQLITE_SCHEMA */ "database schema has changed",
17944 /* SQLITE_TOOBIG */ "string or blob too big",
17945 /* SQLITE_CONSTRAINT */ "constraint failed",
17946 /* SQLITE_MISMATCH */ "datatype mismatch",
17947 /* SQLITE_MISUSE */ "library routine called out of sequence",
17948 /* SQLITE_NOLFS */ "large file support is disabled",
17949 /* SQLITE_AUTH */ "authorization denied",
17950 /* SQLITE_FORMAT */ "auxiliary database format error",
17951 /* SQLITE_RANGE */ "bind or column index out of range",
17952 /* SQLITE_NOTADB */ "file is encrypted or is not a database",
17953 };
17954 const char *zErr = "unknown error";
17955 switch( rc ){
17956 case SQLITE_ABORT_ROLLBACK: {
17957 zErr = "abort due to ROLLBACK";
17958 break;
17959 }
17960 default: {
17961 rc &= 0xff;
17962 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
17963 zErr = aMsg[rc];
17964 }
17965 break;
17966 }
17967 }
17968 return zErr;
17969 }
17970
17971 /*
17972 ** This routine implements a busy callback that sleeps and tries
17973 ** again until a timeout value is reached. The timeout value is
17974 ** an integer number of milliseconds passed in as the first
17975 ** argument.
17976 */
17977 static int sqliteDefaultBusyCallback(
17978 void *ptr, /* Database connection */
17979 int count /* Number of times table has been busy */
17980 ){
17981 #if SQLITE_OS_WIN || HAVE_USLEEP
17982 static const u8 delays[] =
17983 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
17984 static const u8 totals[] =
17985 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
17986 # define NDELAY ArraySize(delays)
17987 sqlite3 *db = (sqlite3 *)ptr;
17988 int timeout = db->busyTimeout;
17989 int delay, prior;
17990
17991 assert( count>=0 );
17992 if( count < NDELAY ){
17993 delay = delays[count];
17994 prior = totals[count];
17995 }else{
17996 delay = delays[NDELAY-1];
17997 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
17998 }
17999 if( prior + delay > timeout ){
18000 delay = timeout - prior;
18001 if( delay<=0 ) return 0;
18002 }
18003 sqlite3OsSleep(db->pVfs, delay*1000);
18004 return 1;
18005 #else
18006 sqlite3 *db = (sqlite3 *)ptr;
18007 int timeout = ((sqlite3 *)ptr)->busyTimeout;
18008 if( (count+1)*1000 > timeout ){
18009 return 0;
18010 }
18011 sqlite3OsSleep(db->pVfs, 1000000);
18012 return 1;
18013 #endif
18014 }
18015
18016 /*
18017 ** Invoke the given busy handler.
18018 **
18019 ** This routine is called when an operation failed with a lock.
18020 ** If this routine returns non-zero, the lock is retried. If it
18021 ** returns 0, the operation aborts with an SQLITE_BUSY error.
18022 */
18023 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
18024 int rc;
18025 if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
18026 rc = p->xFunc(p->pArg, p->nBusy);
18027 if( rc==0 ){
18028 p->nBusy = -1;
18029 }else{
18030 p->nBusy++;
18031 }
18032 return rc;
18033 }
18034
18035 /*
18036 ** This routine sets the busy callback for an Sqlite database to the
18037 ** given callback function with the given argument.
18038 */
18039 SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(
18040 sqlite3 *db,
18041 int (*xBusy)(void*,int),
18042 void *pArg
18043 ){
18044 #ifdef SQLITE_ENABLE_API_ARMOR
18045 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
18046 #endif
18047 sqlite3_mutex_enter(db->mutex);
18048 db->busyHandler.xFunc = xBusy;
18049 db->busyHandler.pArg = pArg;
18050 db->busyHandler.nBusy = 0;
18051 db->busyTimeout = 0;
18052 sqlite3_mutex_leave(db->mutex);
18053 return SQLITE_OK;
18054 }
18055
18056 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
18057 /*
18058 ** This routine sets the progress callback for an Sqlite database to the
18059 ** given callback function with the given argument. The progress callback will
18060 ** be invoked every nOps opcodes.
18061 */
18062 SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(
18063 sqlite3 *db,
18064 int nOps,
18065 int (*xProgress)(void*),
18066 void *pArg
18067 ){
18068 #ifdef SQLITE_ENABLE_API_ARMOR
18069 if( !sqlite3SafetyCheckOk(db) ){
18070 (void)SQLITE_MISUSE_BKPT;
18071 return;
18072 }
18073 #endif
18074 sqlite3_mutex_enter(db->mutex);
18075 if( nOps>0 ){
18076 db->xProgress = xProgress;
18077 db->nProgressOps = (unsigned)nOps;
18078 db->pProgressArg = pArg;
18079 }else{
18080 db->xProgress = 0;
18081 db->nProgressOps = 0;
18082 db->pProgressArg = 0;
18083 }
18084 sqlite3_mutex_leave(db->mutex);
18085 }
18086 #endif
18087
18088
18089 /*
18090 ** This routine installs a default busy handler that waits for the
18091 ** specified number of milliseconds before returning 0.
18092 */
18093 SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3 *db, int ms){
18094 #ifdef SQLITE_ENABLE_API_ARMOR
18095 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
18096 #endif
18097 if( ms>0 ){
18098 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
18099 db->busyTimeout = ms;
18100 }else{
18101 sqlite3_busy_handler(db, 0, 0);
18102 }
18103 return SQLITE_OK;
18104 }
18105
18106 /*
18107 ** Cause any pending operation to stop at its earliest opportunity.
18108 */
18109 SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3 *db){
18110 #ifdef SQLITE_ENABLE_API_ARMOR
18111 if( !sqlite3SafetyCheckOk(db) ){
18112 (void)SQLITE_MISUSE_BKPT;
18113 return;
18114 }
18115 #endif
18116 db->u1.isInterrupted = 1;
18117 }
18118
18119
18120 /*
18121 ** This function is exactly the same as sqlite3_create_function(), except
18122 ** that it is designed to be called by internal code. The difference is
18123 ** that if a malloc() fails in sqlite3_create_function(), an error code
18124 ** is returned and the mallocFailed flag cleared.
18125 */
18126 SQLITE_PRIVATE int sqlite3CreateFunc(
18127 sqlite3 *db,
18128 const char *zFunctionName,
18129 int nArg,
18130 int enc,
18131 void *pUserData,
18132 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
18133 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
18134 void (*xFinal)(sqlite3_context*),
18135 FuncDestructor *pDestructor
18136 ){
18137 FuncDef *p;
18138 int nName;
18139 int extraFlags;
18140
18141 assert( sqlite3_mutex_held(db->mutex) );
18142 if( zFunctionName==0 ||
18143 (xFunc && (xFinal || xStep)) ||
18144 (!xFunc && (xFinal && !xStep)) ||
18145 (!xFunc && (!xFinal && xStep)) ||
18146 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
18147 (255<(nName = sqlite3Strlen30( zFunctionName))) ){
18148 return SQLITE_MISUSE_BKPT;
18149 }
18150
18151 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
18152 extraFlags = enc & SQLITE_DETERMINISTIC;
18153 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
18154
18155 #ifndef SQLITE_OMIT_UTF16
18156 /* If SQLITE_UTF16 is specified as the encoding type, transform this
18157 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
18158 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
18159 **
18160 ** If SQLITE_ANY is specified, add three versions of the function
18161 ** to the hash table.
18162 */
18163 if( enc==SQLITE_UTF16 ){
18164 enc = SQLITE_UTF16NATIVE;
18165 }else if( enc==SQLITE_ANY ){
18166 int rc;
18167 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
18168 pUserData, xFunc, xStep, xFinal, pDestructor);
18169 if( rc==SQLITE_OK ){
18170 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
18171 pUserData, xFunc, xStep, xFinal, pDestructor);
18172 }
18173 if( rc!=SQLITE_OK ){
18174 return rc;
18175 }
18176 enc = SQLITE_UTF16BE;
18177 }
18178 #else
18179 enc = SQLITE_UTF8;
18180 #endif
18181
18182 /* Check if an existing function is being overridden or deleted. If so,
18183 ** and there are active VMs, then return SQLITE_BUSY. If a function
18184 ** is being overridden/deleted but there are no active VMs, allow the
18185 ** operation to continue but invalidate all precompiled statements.
18186 */
18187 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
18188 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
18189 if( db->nVdbeActive ){
18190 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
18191 "unable to delete/modify user-function due to active statements");
18192 assert( !db->mallocFailed );
18193 return SQLITE_BUSY;
18194 }else{
18195 sqlite3ExpirePreparedStatements(db);
18196 }
18197 }
18198
18199 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
18200 assert(p || db->mallocFailed);
18201 if( !p ){
18202 return SQLITE_NOMEM;
18203 }
18204
18205 /* If an older version of the function with a configured destructor is
18206 ** being replaced invoke the destructor function here. */
18207 functionDestroy(db, p);
18208
18209 if( pDestructor ){
18210 pDestructor->nRef++;
18211 }
18212 p->pDestructor = pDestructor;
18213 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
18214 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
18215 p->xFunc = xFunc;
18216 p->xStep = xStep;
18217 p->xFinalize = xFinal;
18218 p->pUserData = pUserData;
18219 p->nArg = (u16)nArg;
18220 return SQLITE_OK;
18221 }
18222
18223 /*
18224 ** Create new user functions.
18225 */
18226 SQLITE_API int SQLITE_STDCALL sqlite3_create_function(
18227 sqlite3 *db,
18228 const char *zFunc,
18229 int nArg,
18230 int enc,
18231 void *p,
18232 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
18233 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
18234 void (*xFinal)(sqlite3_context*)
18235 ){
18236 return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
18237 xFinal, 0);
18238 }
18239
18240 SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
18241 sqlite3 *db,
18242 const char *zFunc,
18243 int nArg,
18244 int enc,
18245 void *p,
18246 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
18247 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
18248 void (*xFinal)(sqlite3_context*),
18249 void (*xDestroy)(void *)
18250 ){
18251 int rc = SQLITE_ERROR;
18252 FuncDestructor *pArg = 0;
18253
18254 #ifdef SQLITE_ENABLE_API_ARMOR
18255 if( !sqlite3SafetyCheckOk(db) ){
18256 return SQLITE_MISUSE_BKPT;
18257 }
18258 #endif
18259 sqlite3_mutex_enter(db->mutex);
18260 if( xDestroy ){
18261 pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
18262 if( !pArg ){
18263 xDestroy(p);
18264 goto out;
18265 }
18266 pArg->xDestroy = xDestroy;
18267 pArg->pUserData = p;
18268 }
18269 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
18270 if( pArg && pArg->nRef==0 ){
18271 assert( rc!=SQLITE_OK );
18272 xDestroy(p);
18273 sqlite3DbFree(db, pArg);
18274 }
18275
18276 out:
18277 rc = sqlite3ApiExit(db, rc);
18278 sqlite3_mutex_leave(db->mutex);
18279 return rc;
18280 }
18281
18282 #ifndef SQLITE_OMIT_UTF16
18283 SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
18284 sqlite3 *db,
18285 const void *zFunctionName,
18286 int nArg,
18287 int eTextRep,
18288 void *p,
18289 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
18290 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
18291 void (*xFinal)(sqlite3_context*)
18292 ){
18293 int rc;
18294 char *zFunc8;
18295
18296 #ifdef SQLITE_ENABLE_API_ARMOR
18297 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
18298 #endif
18299 sqlite3_mutex_enter(db->mutex);
18300 assert( !db->mallocFailed );
18301 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
18302 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
18303 sqlite3DbFree(db, zFunc8);
18304 rc = sqlite3ApiExit(db, rc);
18305 sqlite3_mutex_leave(db->mutex);
18306 return rc;
18307 }
18308 #endif
18309
18310
18311 /*
18312 ** Declare that a function has been overloaded by a virtual table.
18313 **
18314 ** If the function already exists as a regular global function, then
18315 ** this routine is a no-op. If the function does not exist, then create
18316 ** a new one that always throws a run-time error.
18317 **
18318 ** When virtual tables intend to provide an overloaded function, they
18319 ** should call this routine to make sure the global function exists.
18320 ** A global function must exist in order for name resolution to work
18321 ** properly.
18322 */
18323 SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(
18324 sqlite3 *db,
18325 const char *zName,
18326 int nArg
18327 ){
18328 int nName = sqlite3Strlen30(zName);
18329 int rc = SQLITE_OK;
18330
18331 #ifdef SQLITE_ENABLE_API_ARMOR
18332 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
18333 return SQLITE_MISUSE_BKPT;
18334 }
18335 #endif
18336 sqlite3_mutex_enter(db->mutex);
18337 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
18338 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
18339 0, sqlite3InvalidFunction, 0, 0, 0);
18340 }
18341 rc = sqlite3ApiExit(db, rc);
18342 sqlite3_mutex_leave(db->mutex);
18343 return rc;
18344 }
18345
18346 #ifndef SQLITE_OMIT_TRACE
18347 /*
18348 ** Register a trace function. The pArg from the previously registered trace
18349 ** is returned.
18350 **
18351 ** A NULL trace function means that no tracing is executes. A non-NULL
18352 ** trace is a pointer to a function that is invoked at the start of each
18353 ** SQL statement.
18354 */
18355 SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3 *db, void (*xTrace)(void*, const char*), void *pArg){
18356 void *pOld;
18357
18358 #ifdef SQLITE_ENABLE_API_ARMOR
18359 if( !sqlite3SafetyCheckOk(db) ){
18360 (void)SQLITE_MISUSE_BKPT;
18361 return 0;
18362 }
18363 #endif
18364 sqlite3_mutex_enter(db->mutex);
18365 pOld = db->pTraceArg;
18366 db->xTrace = xTrace;
18367 db->pTraceArg = pArg;
18368 sqlite3_mutex_leave(db->mutex);
18369 return pOld;
18370 }
18371 /*
18372 ** Register a profile function. The pArg from the previously registered
18373 ** profile function is returned.
18374 **
18375 ** A NULL profile function means that no profiling is executes. A non-NULL
18376 ** profile is a pointer to a function that is invoked at the conclusion of
18377 ** each SQL statement that is run.
18378 */
18379 SQLITE_API void *SQLITE_STDCALL sqlite3_profile(
18380 sqlite3 *db,
18381 void (*xProfile)(void*,const char*,sqlite_uint64),
18382 void *pArg
18383 ){
18384 void *pOld;
18385
18386 #ifdef SQLITE_ENABLE_API_ARMOR
18387 if( !sqlite3SafetyCheckOk(db) ){
18388 (void)SQLITE_MISUSE_BKPT;
18389 return 0;
18390 }
18391 #endif
18392 sqlite3_mutex_enter(db->mutex);
18393 pOld = db->pProfileArg;
18394 db->xProfile = xProfile;
18395 db->pProfileArg = pArg;
18396 sqlite3_mutex_leave(db->mutex);
18397 return pOld;
18398 }
18399 #endif /* SQLITE_OMIT_TRACE */
18400
18401 /*
18402 ** Register a function to be invoked when a transaction commits.
18403 ** If the invoked function returns non-zero, then the commit becomes a
18404 ** rollback.
18405 */
18406 SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(
18407 sqlite3 *db, /* Attach the hook to this database */
18408 int (*xCallback)(void*), /* Function to invoke on each commit */
18409 void *pArg /* Argument to the function */
18410 ){
18411 void *pOld;
18412
18413 #ifdef SQLITE_ENABLE_API_ARMOR
18414 if( !sqlite3SafetyCheckOk(db) ){
18415 (void)SQLITE_MISUSE_BKPT;
18416 return 0;
18417 }
18418 #endif
18419 sqlite3_mutex_enter(db->mutex);
18420 pOld = db->pCommitArg;
18421 db->xCommitCallback = xCallback;
18422 db->pCommitArg = pArg;
18423 sqlite3_mutex_leave(db->mutex);
18424 return pOld;
18425 }
18426
18427 /*
18428 ** Register a callback to be invoked each time a row is updated,
18429 ** inserted or deleted using this database connection.
18430 */
18431 SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
18432 sqlite3 *db, /* Attach the hook to this database */
18433 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
18434 void *pArg /* Argument to the function */
18435 ){
18436 void *pRet;
18437
18438 #ifdef SQLITE_ENABLE_API_ARMOR
18439 if( !sqlite3SafetyCheckOk(db) ){
18440 (void)SQLITE_MISUSE_BKPT;
18441 return 0;
18442 }
18443 #endif
18444 sqlite3_mutex_enter(db->mutex);
18445 pRet = db->pUpdateArg;
18446 db->xUpdateCallback = xCallback;
18447 db->pUpdateArg = pArg;
18448 sqlite3_mutex_leave(db->mutex);
18449 return pRet;
18450 }
18451
18452 /*
18453 ** Register a callback to be invoked each time a transaction is rolled
18454 ** back by this database connection.
18455 */
18456 SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(
18457 sqlite3 *db, /* Attach the hook to this database */
18458 void (*xCallback)(void*), /* Callback function */
18459 void *pArg /* Argument to the function */
18460 ){
18461 void *pRet;
18462
18463 #ifdef SQLITE_ENABLE_API_ARMOR
18464 if( !sqlite3SafetyCheckOk(db) ){
18465 (void)SQLITE_MISUSE_BKPT;
18466 return 0;
18467 }
18468 #endif
18469 sqlite3_mutex_enter(db->mutex);
18470 pRet = db->pRollbackArg;
18471 db->xRollbackCallback = xCallback;
18472 db->pRollbackArg = pArg;
18473 sqlite3_mutex_leave(db->mutex);
18474 return pRet;
18475 }
18476
18477 #ifndef SQLITE_OMIT_WAL
18478 /*
18479 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
18480 ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
18481 ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
18482 ** wal_autocheckpoint()).
18483 */
18484 SQLITE_PRIVATE int sqlite3WalDefaultHook(
18485 void *pClientData, /* Argument */
18486 sqlite3 *db, /* Connection */
18487 const char *zDb, /* Database */
18488 int nFrame /* Size of WAL */
18489 ){
18490 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
18491 sqlite3BeginBenignMalloc();
18492 sqlite3_wal_checkpoint(db, zDb);
18493 sqlite3EndBenignMalloc();
18494 }
18495 return SQLITE_OK;
18496 }
18497 #endif /* SQLITE_OMIT_WAL */
18498
18499 /*
18500 ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
18501 ** a database after committing a transaction if there are nFrame or
18502 ** more frames in the log file. Passing zero or a negative value as the
18503 ** nFrame parameter disables automatic checkpoints entirely.
18504 **
18505 ** The callback registered by this function replaces any existing callback
18506 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
18507 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
18508 ** configured by this function.
18509 */
18510 SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame ){
18511 #ifdef SQLITE_OMIT_WAL
18512 UNUSED_PARAMETER(db);
18513 UNUSED_PARAMETER(nFrame);
18514 #else
18515 #ifdef SQLITE_ENABLE_API_ARMOR
18516 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
18517 #endif
18518 if( nFrame>0 ){
18519 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
18520 }else{
18521 sqlite3_wal_hook(db, 0, 0);
18522 }
18523 #endif
18524 return SQLITE_OK;
18525 }
18526
18527 /*
18528 ** Register a callback to be invoked each time a transaction is written
18529 ** into the write-ahead-log by this database connection.
18530 */
18531 SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
18532 sqlite3 *db, /* Attach the hook to this db handle */
18533 int(*xCallback)(void *, sqlite3*, const char*, int),
18534 void *pArg /* First argument passed to xCallback() */
18535 ){
18536 #ifndef SQLITE_OMIT_WAL
18537 void *pRet;
18538 #ifdef SQLITE_ENABLE_API_ARMOR
18539 if( !sqlite3SafetyCheckOk(db) ){
18540 (void)SQLITE_MISUSE_BKPT;
18541 return 0;
18542 }
18543 #endif
18544 sqlite3_mutex_enter(db->mutex);
18545 pRet = db->pWalArg;
18546 db->xWalCallback = xCallback;
18547 db->pWalArg = pArg;
18548 sqlite3_mutex_leave(db->mutex);
18549 return pRet;
18550 #else
18551 return 0;
18552 #endif
18553 }
18554
18555 /*
18556 ** Checkpoint database zDb.
18557 */
18558 SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
18559 sqlite3 *db, /* Database handle */
18560 const char *zDb, /* Name of attached database (or NULL) */
18561 int eMode, /* SQLITE_CHECKPOINT_* value */
18562 int *pnLog, /* OUT: Size of WAL log in frames */
18563 int *pnCkpt /* OUT: Total number of frames checkpointed */
18564 ){
18565 #ifdef SQLITE_OMIT_WAL
18566 return SQLITE_OK;
18567 #else
18568 int rc; /* Return code */
18569 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
18570
18571 #ifdef SQLITE_ENABLE_API_ARMOR
18572 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
18573 #endif
18574
18575 /* Initialize the output variables to -1 in case an error occurs. */
18576 if( pnLog ) *pnLog = -1;
18577 if( pnCkpt ) *pnCkpt = -1;
18578
18579 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
18580 assert( SQLITE_CHECKPOINT_FULL==1 );
18581 assert( SQLITE_CHECKPOINT_RESTART==2 );
18582 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
18583 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
18584 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
18585 ** mode: */
18586 return SQLITE_MISUSE;
18587 }
18588
18589 sqlite3_mutex_enter(db->mutex);
18590 if( zDb && zDb[0] ){
18591 iDb = sqlite3FindDbName(db, zDb);
18592 }
18593 if( iDb<0 ){
18594 rc = SQLITE_ERROR;
18595 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
18596 }else{
18597 db->busyHandler.nBusy = 0;
18598 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
18599 sqlite3Error(db, rc);
18600 }
18601 rc = sqlite3ApiExit(db, rc);
18602 sqlite3_mutex_leave(db->mutex);
18603 return rc;
18604 #endif
18605 }
18606
18607
18608 /*
18609 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
18610 ** to contains a zero-length string, all attached databases are
18611 ** checkpointed.
18612 */
18613 SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zD b){
18614 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
18615 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
18616 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
18617 }
18618
18619 #ifndef SQLITE_OMIT_WAL
18620 /*
18621 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
18622 ** not currently open in WAL mode.
18623 **
18624 ** If a transaction is open on the database being checkpointed, this
18625 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
18626 ** an error occurs while running the checkpoint, an SQLite error code is
18627 ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
18628 **
18629 ** The mutex on database handle db should be held by the caller. The mutex
18630 ** associated with the specific b-tree being checkpointed is taken by
18631 ** this function while the checkpoint is running.
18632 **
18633 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
18634 ** checkpointed. If an error is encountered it is returned immediately -
18635 ** no attempt is made to checkpoint any remaining databases.
18636 **
18637 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
18638 */
18639 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog , int *pnCkpt){
18640 int rc = SQLITE_OK; /* Return code */
18641 int i; /* Used to iterate through attached dbs */
18642 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
18643
18644 assert( sqlite3_mutex_held(db->mutex) );
18645 assert( !pnLog || *pnLog==-1 );
18646 assert( !pnCkpt || *pnCkpt==-1 );
18647
18648 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
18649 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
18650 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
18651 pnLog = 0;
18652 pnCkpt = 0;
18653 if( rc==SQLITE_BUSY ){
18654 bBusy = 1;
18655 rc = SQLITE_OK;
18656 }
18657 }
18658 }
18659
18660 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
18661 }
18662 #endif /* SQLITE_OMIT_WAL */
18663
18664 /*
18665 ** This function returns true if main-memory should be used instead of
18666 ** a temporary file for transient pager files and statement journals.
18667 ** The value returned depends on the value of db->temp_store (runtime
18668 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
18669 ** following table describes the relationship between these two values
18670 ** and this functions return value.
18671 **
18672 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database
18673 ** ----------------- -------------- ------------------------------
18674 ** 0 any file (return 0)
18675 ** 1 1 file (return 0)
18676 ** 1 2 memory (return 1)
18677 ** 1 0 file (return 0)
18678 ** 2 1 file (return 0)
18679 ** 2 2 memory (return 1)
18680 ** 2 0 memory (return 1)
18681 ** 3 any memory (return 1)
18682 */
18683 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
18684 #if SQLITE_TEMP_STORE==1
18685 return ( db->temp_store==2 );
18686 #endif
18687 #if SQLITE_TEMP_STORE==2
18688 return ( db->temp_store!=1 );
18689 #endif
18690 #if SQLITE_TEMP_STORE==3
18691 UNUSED_PARAMETER(db);
18692 return 1;
18693 #endif
18694 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
18695 UNUSED_PARAMETER(db);
18696 return 0;
18697 #endif
18698 }
18699
18700 /*
18701 ** Return UTF-8 encoded English language explanation of the most recent
18702 ** error.
18703 */
18704 SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3 *db){
18705 const char *z;
18706 if( !db ){
18707 return sqlite3ErrStr(SQLITE_NOMEM);
18708 }
18709 if( !sqlite3SafetyCheckSickOrOk(db) ){
18710 return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
18711 }
18712 sqlite3_mutex_enter(db->mutex);
18713 if( db->mallocFailed ){
18714 z = sqlite3ErrStr(SQLITE_NOMEM);
18715 }else{
18716 testcase( db->pErr==0 );
18717 z = (char*)sqlite3_value_text(db->pErr);
18718 assert( !db->mallocFailed );
18719 if( z==0 ){
18720 z = sqlite3ErrStr(db->errCode);
18721 }
18722 }
18723 sqlite3_mutex_leave(db->mutex);
18724 return z;
18725 }
18726
18727 #ifndef SQLITE_OMIT_UTF16
18728 /*
18729 ** Return UTF-16 encoded English language explanation of the most recent
18730 ** error.
18731 */
18732 SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3 *db){
18733 static const u16 outOfMem[] = {
18734 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
18735 };
18736 static const u16 misuse[] = {
18737 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
18738 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
18739 'c', 'a', 'l', 'l', 'e', 'd', ' ',
18740 'o', 'u', 't', ' ',
18741 'o', 'f', ' ',
18742 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
18743 };
18744
18745 const void *z;
18746 if( !db ){
18747 return (void *)outOfMem;
18748 }
18749 if( !sqlite3SafetyCheckSickOrOk(db) ){
18750 return (void *)misuse;
18751 }
18752 sqlite3_mutex_enter(db->mutex);
18753 if( db->mallocFailed ){
18754 z = (void *)outOfMem;
18755 }else{
18756 z = sqlite3_value_text16(db->pErr);
18757 if( z==0 ){
18758 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
18759 z = sqlite3_value_text16(db->pErr);
18760 }
18761 /* A malloc() may have failed within the call to sqlite3_value_text16()
18762 ** above. If this is the case, then the db->mallocFailed flag needs to
18763 ** be cleared before returning. Do this directly, instead of via
18764 ** sqlite3ApiExit(), to avoid setting the database handle error message.
18765 */
18766 db->mallocFailed = 0;
18767 }
18768 sqlite3_mutex_leave(db->mutex);
18769 return z;
18770 }
18771 #endif /* SQLITE_OMIT_UTF16 */
18772
18773 /*
18774 ** Return the most recent error code generated by an SQLite routine. If NULL is
18775 ** passed to this function, we assume a malloc() failed during sqlite3_open().
18776 */
18777 SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db){
18778 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
18779 return SQLITE_MISUSE_BKPT;
18780 }
18781 if( !db || db->mallocFailed ){
18782 return SQLITE_NOMEM;
18783 }
18784 return db->errCode & db->errMask;
18785 }
18786 SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db){
18787 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
18788 return SQLITE_MISUSE_BKPT;
18789 }
18790 if( !db || db->mallocFailed ){
18791 return SQLITE_NOMEM;
18792 }
18793 return db->errCode;
18794 }
18795
18796 /*
18797 ** Return a string that describes the kind of error specified in the
18798 ** argument. For now, this simply calls the internal sqlite3ErrStr()
18799 ** function.
18800 */
18801 SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int rc){
18802 return sqlite3ErrStr(rc);
18803 }
18804
18805 /*
18806 ** Create a new collating function for database "db". The name is zName
18807 ** and the encoding is enc.
18808 */
18809 static int createCollation(
18810 sqlite3* db,
18811 const char *zName,
18812 u8 enc,
18813 void* pCtx,
18814 int(*xCompare)(void*,int,const void*,int,const void*),
18815 void(*xDel)(void*)
18816 ){
18817 CollSeq *pColl;
18818 int enc2;
18819
18820 assert( sqlite3_mutex_held(db->mutex) );
18821
18822 /* If SQLITE_UTF16 is specified as the encoding type, transform this
18823 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
18824 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
18825 */
18826 enc2 = enc;
18827 testcase( enc2==SQLITE_UTF16 );
18828 testcase( enc2==SQLITE_UTF16_ALIGNED );
18829 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
18830 enc2 = SQLITE_UTF16NATIVE;
18831 }
18832 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
18833 return SQLITE_MISUSE_BKPT;
18834 }
18835
18836 /* Check if this call is removing or replacing an existing collation
18837 ** sequence. If so, and there are active VMs, return busy. If there
18838 ** are no active VMs, invalidate any pre-compiled statements.
18839 */
18840 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
18841 if( pColl && pColl->xCmp ){
18842 if( db->nVdbeActive ){
18843 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
18844 "unable to delete/modify collation sequence due to active statements");
18845 return SQLITE_BUSY;
18846 }
18847 sqlite3ExpirePreparedStatements(db);
18848
18849 /* If collation sequence pColl was created directly by a call to
18850 ** sqlite3_create_collation, and not generated by synthCollSeq(),
18851 ** then any copies made by synthCollSeq() need to be invalidated.
18852 ** Also, collation destructor - CollSeq.xDel() - function may need
18853 ** to be called.
18854 */
18855 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
18856 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
18857 int j;
18858 for(j=0; j<3; j++){
18859 CollSeq *p = &aColl[j];
18860 if( p->enc==pColl->enc ){
18861 if( p->xDel ){
18862 p->xDel(p->pUser);
18863 }
18864 p->xCmp = 0;
18865 }
18866 }
18867 }
18868 }
18869
18870 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
18871 if( pColl==0 ) return SQLITE_NOMEM;
18872 pColl->xCmp = xCompare;
18873 pColl->pUser = pCtx;
18874 pColl->xDel = xDel;
18875 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
18876 sqlite3Error(db, SQLITE_OK);
18877 return SQLITE_OK;
18878 }
18879
18880
18881 /*
18882 ** This array defines hard upper bounds on limit values. The
18883 ** initializer must be kept in sync with the SQLITE_LIMIT_*
18884 ** #defines in sqlite3.h.
18885 */
18886 static const int aHardLimit[] = {
18887 SQLITE_MAX_LENGTH,
18888 SQLITE_MAX_SQL_LENGTH,
18889 SQLITE_MAX_COLUMN,
18890 SQLITE_MAX_EXPR_DEPTH,
18891 SQLITE_MAX_COMPOUND_SELECT,
18892 SQLITE_MAX_VDBE_OP,
18893 SQLITE_MAX_FUNCTION_ARG,
18894 SQLITE_MAX_ATTACHED,
18895 SQLITE_MAX_LIKE_PATTERN_LENGTH,
18896 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
18897 SQLITE_MAX_TRIGGER_DEPTH,
18898 SQLITE_MAX_WORKER_THREADS,
18899 };
18900
18901 /*
18902 ** Make sure the hard limits are set to reasonable values
18903 */
18904 #if SQLITE_MAX_LENGTH<100
18905 # error SQLITE_MAX_LENGTH must be at least 100
18906 #endif
18907 #if SQLITE_MAX_SQL_LENGTH<100
18908 # error SQLITE_MAX_SQL_LENGTH must be at least 100
18909 #endif
18910 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
18911 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
18912 #endif
18913 #if SQLITE_MAX_COMPOUND_SELECT<2
18914 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
18915 #endif
18916 #if SQLITE_MAX_VDBE_OP<40
18917 # error SQLITE_MAX_VDBE_OP must be at least 40
18918 #endif
18919 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
18920 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
18921 #endif
18922 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
18923 # error SQLITE_MAX_ATTACHED must be between 0 and 125
18924 #endif
18925 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
18926 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
18927 #endif
18928 #if SQLITE_MAX_COLUMN>32767
18929 # error SQLITE_MAX_COLUMN must not exceed 32767
18930 #endif
18931 #if SQLITE_MAX_TRIGGER_DEPTH<1
18932 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
18933 #endif
18934 #if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
18935 # error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
18936 #endif
18937
18938
18939 /*
18940 ** Change the value of a limit. Report the old value.
18941 ** If an invalid limit index is supplied, report -1.
18942 ** Make no changes but still report the old value if the
18943 ** new limit is negative.
18944 **
18945 ** A new lower limit does not shrink existing constructs.
18946 ** It merely prevents new constructs that exceed the limit
18947 ** from forming.
18948 */
18949 SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3 *db, int limitId, int newLim it){
18950 int oldLimit;
18951
18952 #ifdef SQLITE_ENABLE_API_ARMOR
18953 if( !sqlite3SafetyCheckOk(db) ){
18954 (void)SQLITE_MISUSE_BKPT;
18955 return -1;
18956 }
18957 #endif
18958
18959 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
18960 ** there is a hard upper bound set at compile-time by a C preprocessor
18961 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
18962 ** "_MAX_".)
18963 */
18964 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
18965 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
18966 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
18967 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
18968 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
18969 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
18970 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
18971 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
18972 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
18973 SQLITE_MAX_LIKE_PATTERN_LENGTH );
18974 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
18975 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
18976 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
18977 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
18978
18979
18980 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
18981 return -1;
18982 }
18983 oldLimit = db->aLimit[limitId];
18984 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
18985 if( newLimit>aHardLimit[limitId] ){
18986 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
18987 }
18988 db->aLimit[limitId] = newLimit;
18989 }
18990 return oldLimit; /* IMP: R-53341-35419 */
18991 }
18992
18993 /*
18994 ** This function is used to parse both URIs and non-URI filenames passed by the
18995 ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
18996 ** URIs specified as part of ATTACH statements.
18997 **
18998 ** The first argument to this function is the name of the VFS to use (or
18999 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
19000 ** query parameter. The second argument contains the URI (or non-URI filename)
19001 ** itself. When this function is called the *pFlags variable should contain
19002 ** the default flags to open the database handle with. The value stored in
19003 ** *pFlags may be updated before returning if the URI filename contains
19004 ** "cache=xxx" or "mode=xxx" query parameters.
19005 **
19006 ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
19007 ** the VFS that should be used to open the database file. *pzFile is set to
19008 ** point to a buffer containing the name of the file to open. It is the
19009 ** responsibility of the caller to eventually call sqlite3_free() to release
19010 ** this buffer.
19011 **
19012 ** If an error occurs, then an SQLite error code is returned and *pzErrMsg
19013 ** may be set to point to a buffer containing an English language error
19014 ** message. It is the responsibility of the caller to eventually release
19015 ** this buffer by calling sqlite3_free().
19016 */
19017 SQLITE_PRIVATE int sqlite3ParseUri(
19018 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
19019 const char *zUri, /* Nul-terminated URI to parse */
19020 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
19021 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
19022 char **pzFile, /* OUT: Filename component of URI */
19023 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
19024 ){
19025 int rc = SQLITE_OK;
19026 unsigned int flags = *pFlags;
19027 const char *zVfs = zDefaultVfs;
19028 char *zFile;
19029 char c;
19030 int nUri = sqlite3Strlen30(zUri);
19031
19032 assert( *pzErrMsg==0 );
19033
19034 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
19035 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
19036 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
19037 ){
19038 char *zOpt;
19039 int eState; /* Parser state when parsing URI */
19040 int iIn; /* Input character index */
19041 int iOut = 0; /* Output character index */
19042 u64 nByte = nUri+2; /* Bytes of space to allocate */
19043
19044 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
19045 ** method that there may be extra parameters following the file-name. */
19046 flags |= SQLITE_OPEN_URI;
19047
19048 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
19049 zFile = sqlite3_malloc64(nByte);
19050 if( !zFile ) return SQLITE_NOMEM;
19051
19052 iIn = 5;
19053 #ifdef SQLITE_ALLOW_URI_AUTHORITY
19054 if( strncmp(zUri+5, "///", 3)==0 ){
19055 iIn = 7;
19056 /* The following condition causes URIs with five leading / characters
19057 ** like file://///host/path to be converted into UNCs like //host/path.
19058 ** The correct URI for that UNC has only two or four leading / characters
19059 ** file://host/path or file:////host/path. But 5 leading slashes is a
19060 ** common error, we are told, so we handle it as a special case. */
19061 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
19062 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
19063 iIn = 16;
19064 }
19065 #else
19066 /* Discard the scheme and authority segments of the URI. */
19067 if( zUri[5]=='/' && zUri[6]=='/' ){
19068 iIn = 7;
19069 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
19070 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
19071 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
19072 iIn-7, &zUri[7]);
19073 rc = SQLITE_ERROR;
19074 goto parse_uri_out;
19075 }
19076 }
19077 #endif
19078
19079 /* Copy the filename and any query parameters into the zFile buffer.
19080 ** Decode %HH escape codes along the way.
19081 **
19082 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
19083 ** on the parsing context. As follows:
19084 **
19085 ** 0: Parsing file-name.
19086 ** 1: Parsing name section of a name=value query parameter.
19087 ** 2: Parsing value section of a name=value query parameter.
19088 */
19089 eState = 0;
19090 while( (c = zUri[iIn])!=0 && c!='#' ){
19091 iIn++;
19092 if( c=='%'
19093 && sqlite3Isxdigit(zUri[iIn])
19094 && sqlite3Isxdigit(zUri[iIn+1])
19095 ){
19096 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
19097 octet += sqlite3HexToInt(zUri[iIn++]);
19098
19099 assert( octet>=0 && octet<256 );
19100 if( octet==0 ){
19101 /* This branch is taken when "%00" appears within the URI. In this
19102 ** case we ignore all text in the remainder of the path, name or
19103 ** value currently being parsed. So ignore the current character
19104 ** and skip to the next "?", "=" or "&", as appropriate. */
19105 while( (c = zUri[iIn])!=0 && c!='#'
19106 && (eState!=0 || c!='?')
19107 && (eState!=1 || (c!='=' && c!='&'))
19108 && (eState!=2 || c!='&')
19109 ){
19110 iIn++;
19111 }
19112 continue;
19113 }
19114 c = octet;
19115 }else if( eState==1 && (c=='&' || c=='=') ){
19116 if( zFile[iOut-1]==0 ){
19117 /* An empty option name. Ignore this option altogether. */
19118 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
19119 continue;
19120 }
19121 if( c=='&' ){
19122 zFile[iOut++] = '\0';
19123 }else{
19124 eState = 2;
19125 }
19126 c = 0;
19127 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
19128 c = 0;
19129 eState = 1;
19130 }
19131 zFile[iOut++] = c;
19132 }
19133 if( eState==1 ) zFile[iOut++] = '\0';
19134 zFile[iOut++] = '\0';
19135 zFile[iOut++] = '\0';
19136
19137 /* Check if there were any options specified that should be interpreted
19138 ** here. Options that are interpreted here include "vfs" and those that
19139 ** correspond to flags that may be passed to the sqlite3_open_v2()
19140 ** method. */
19141 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
19142 while( zOpt[0] ){
19143 int nOpt = sqlite3Strlen30(zOpt);
19144 char *zVal = &zOpt[nOpt+1];
19145 int nVal = sqlite3Strlen30(zVal);
19146
19147 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
19148 zVfs = zVal;
19149 }else{
19150 struct OpenMode {
19151 const char *z;
19152 int mode;
19153 } *aMode = 0;
19154 char *zModeType = 0;
19155 int mask = 0;
19156 int limit = 0;
19157
19158 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
19159 static struct OpenMode aCacheMode[] = {
19160 { "shared", SQLITE_OPEN_SHAREDCACHE },
19161 { "private", SQLITE_OPEN_PRIVATECACHE },
19162 { 0, 0 }
19163 };
19164
19165 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
19166 aMode = aCacheMode;
19167 limit = mask;
19168 zModeType = "cache";
19169 }
19170 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
19171 static struct OpenMode aOpenMode[] = {
19172 { "ro", SQLITE_OPEN_READONLY },
19173 { "rw", SQLITE_OPEN_READWRITE },
19174 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
19175 { "memory", SQLITE_OPEN_MEMORY },
19176 { 0, 0 }
19177 };
19178
19179 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
19180 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
19181 aMode = aOpenMode;
19182 limit = mask & flags;
19183 zModeType = "access";
19184 }
19185
19186 if( aMode ){
19187 int i;
19188 int mode = 0;
19189 for(i=0; aMode[i].z; i++){
19190 const char *z = aMode[i].z;
19191 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
19192 mode = aMode[i].mode;
19193 break;
19194 }
19195 }
19196 if( mode==0 ){
19197 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
19198 rc = SQLITE_ERROR;
19199 goto parse_uri_out;
19200 }
19201 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
19202 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
19203 zModeType, zVal);
19204 rc = SQLITE_PERM;
19205 goto parse_uri_out;
19206 }
19207 flags = (flags & ~mask) | mode;
19208 }
19209 }
19210
19211 zOpt = &zVal[nVal+1];
19212 }
19213
19214 }else{
19215 zFile = sqlite3_malloc64(nUri+2);
19216 if( !zFile ) return SQLITE_NOMEM;
19217 memcpy(zFile, zUri, nUri);
19218 zFile[nUri] = '\0';
19219 zFile[nUri+1] = '\0';
19220 flags &= ~SQLITE_OPEN_URI;
19221 }
19222
19223 *ppVfs = sqlite3_vfs_find(zVfs);
19224 if( *ppVfs==0 ){
19225 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
19226 rc = SQLITE_ERROR;
19227 }
19228 parse_uri_out:
19229 if( rc!=SQLITE_OK ){
19230 sqlite3_free(zFile);
19231 zFile = 0;
19232 }
19233 *pFlags = flags;
19234 *pzFile = zFile;
19235 return rc;
19236 }
19237
19238
19239 /*
19240 ** This routine does the work of opening a database on behalf of
19241 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
19242 ** is UTF-8 encoded.
19243 */
19244 static int openDatabase(
19245 const char *zFilename, /* Database filename UTF-8 encoded */
19246 sqlite3 **ppDb, /* OUT: Returned database handle */
19247 unsigned int flags, /* Operational flags */
19248 const char *zVfs /* Name of the VFS to use */
19249 ){
19250 sqlite3 *db; /* Store allocated handle here */
19251 int rc; /* Return code */
19252 int isThreadsafe; /* True for threadsafe connections */
19253 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
19254 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
19255
19256 #ifdef SQLITE_ENABLE_API_ARMOR
19257 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
19258 #endif
19259 *ppDb = 0;
19260 #ifndef SQLITE_OMIT_AUTOINIT
19261 rc = sqlite3_initialize();
19262 if( rc ) return rc;
19263 #endif
19264
19265 /* Only allow sensible combinations of bits in the flags argument.
19266 ** Throw an error if any non-sense combination is used. If we
19267 ** do not block illegal combinations here, it could trigger
19268 ** assert() statements in deeper layers. Sensible combinations
19269 ** are:
19270 **
19271 ** 1: SQLITE_OPEN_READONLY
19272 ** 2: SQLITE_OPEN_READWRITE
19273 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
19274 */
19275 assert( SQLITE_OPEN_READONLY == 0x01 );
19276 assert( SQLITE_OPEN_READWRITE == 0x02 );
19277 assert( SQLITE_OPEN_CREATE == 0x04 );
19278 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
19279 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
19280 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
19281 if( ((1<<(flags&7)) & 0x46)==0 ){
19282 return SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */
19283 }
19284
19285 if( sqlite3GlobalConfig.bCoreMutex==0 ){
19286 isThreadsafe = 0;
19287 }else if( flags & SQLITE_OPEN_NOMUTEX ){
19288 isThreadsafe = 0;
19289 }else if( flags & SQLITE_OPEN_FULLMUTEX ){
19290 isThreadsafe = 1;
19291 }else{
19292 isThreadsafe = sqlite3GlobalConfig.bFullMutex;
19293 }
19294 if( flags & SQLITE_OPEN_PRIVATECACHE ){
19295 flags &= ~SQLITE_OPEN_SHAREDCACHE;
19296 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
19297 flags |= SQLITE_OPEN_SHAREDCACHE;
19298 }
19299
19300 /* Remove harmful bits from the flags parameter
19301 **
19302 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
19303 ** dealt with in the previous code block. Besides these, the only
19304 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
19305 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
19306 ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask
19307 ** off all other flags.
19308 */
19309 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
19310 SQLITE_OPEN_EXCLUSIVE |
19311 SQLITE_OPEN_MAIN_DB |
19312 SQLITE_OPEN_TEMP_DB |
19313 SQLITE_OPEN_TRANSIENT_DB |
19314 SQLITE_OPEN_MAIN_JOURNAL |
19315 SQLITE_OPEN_TEMP_JOURNAL |
19316 SQLITE_OPEN_SUBJOURNAL |
19317 SQLITE_OPEN_MASTER_JOURNAL |
19318 SQLITE_OPEN_NOMUTEX |
19319 SQLITE_OPEN_FULLMUTEX |
19320 SQLITE_OPEN_WAL
19321 );
19322
19323 /* Allocate the sqlite data structure */
19324 db = sqlite3MallocZero( sizeof(sqlite3) );
19325 if( db==0 ) goto opendb_out;
19326 if( isThreadsafe ){
19327 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
19328 if( db->mutex==0 ){
19329 sqlite3_free(db);
19330 db = 0;
19331 goto opendb_out;
19332 }
19333 }
19334 sqlite3_mutex_enter(db->mutex);
19335 db->errMask = 0xff;
19336 db->nDb = 2;
19337 db->magic = SQLITE_MAGIC_BUSY;
19338 db->aDb = db->aDbStatic;
19339
19340 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
19341 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
19342 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
19343 db->autoCommit = 1;
19344 db->nextAutovac = -1;
19345 db->szMmap = sqlite3GlobalConfig.szMmap;
19346 db->nextPagesize = 0;
19347 db->nMaxSorterMmap = 0x7FFFFFFF;
19348 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
19349 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
19350 | SQLITE_AutoIndex
19351 #endif
19352 #if SQLITE_DEFAULT_CKPTFULLFSYNC
19353 | SQLITE_CkptFullFSync
19354 #endif
19355 #if SQLITE_DEFAULT_FILE_FORMAT<4
19356 | SQLITE_LegacyFileFmt
19357 #endif
19358 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
19359 | SQLITE_LoadExtension
19360 #endif
19361 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
19362 | SQLITE_RecTriggers
19363 #endif
19364 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
19365 | SQLITE_ForeignKeys
19366 #endif
19367 #if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
19368 | SQLITE_ReverseOrder
19369 #endif
19370 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
19371 | SQLITE_CellSizeCk
19372 #endif
19373 ;
19374 sqlite3HashInit(&db->aCollSeq);
19375 #ifndef SQLITE_OMIT_VIRTUALTABLE
19376 sqlite3HashInit(&db->aModule);
19377 #endif
19378
19379 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
19380 ** and UTF-16, so add a version for each to avoid any unnecessary
19381 ** conversions. The only error that can occur here is a malloc() failure.
19382 **
19383 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
19384 ** functions:
19385 */
19386 createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0);
19387 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0);
19388 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0);
19389 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
19390 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
19391 if( db->mallocFailed ){
19392 goto opendb_out;
19393 }
19394 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
19395 ** strings is BINARY.
19396 */
19397 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0);
19398 assert( db->pDfltColl!=0 );
19399
19400 /* Parse the filename/URI argument. */
19401 db->openFlags = flags;
19402 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
19403 if( rc!=SQLITE_OK ){
19404 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
19405 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
19406 sqlite3_free(zErrMsg);
19407 goto opendb_out;
19408 }
19409
19410 /* Open the backend database driver */
19411 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
19412 flags | SQLITE_OPEN_MAIN_DB);
19413 if( rc!=SQLITE_OK ){
19414 if( rc==SQLITE_IOERR_NOMEM ){
19415 rc = SQLITE_NOMEM;
19416 }
19417 sqlite3Error(db, rc);
19418 goto opendb_out;
19419 }
19420 sqlite3BtreeEnter(db->aDb[0].pBt);
19421 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
19422 if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
19423 sqlite3BtreeLeave(db->aDb[0].pBt);
19424 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
19425
19426 /* The default safety_level for the main database is 'full'; for the temp
19427 ** database it is 'NONE'. This matches the pager layer defaults.
19428 */
19429 db->aDb[0].zName = "main";
19430 db->aDb[0].safety_level = 3;
19431 db->aDb[1].zName = "temp";
19432 db->aDb[1].safety_level = 1;
19433
19434 db->magic = SQLITE_MAGIC_OPEN;
19435 if( db->mallocFailed ){
19436 goto opendb_out;
19437 }
19438
19439 /* Register all built-in functions, but do not attempt to read the
19440 ** database schema yet. This is delayed until the first time the database
19441 ** is accessed.
19442 */
19443 sqlite3Error(db, SQLITE_OK);
19444 sqlite3RegisterBuiltinFunctions(db);
19445
19446 /* Load automatic extensions - extensions that have been registered
19447 ** using the sqlite3_automatic_extension() API.
19448 */
19449 rc = sqlite3_errcode(db);
19450 if( rc==SQLITE_OK ){
19451 sqlite3AutoLoadExtensions(db);
19452 rc = sqlite3_errcode(db);
19453 if( rc!=SQLITE_OK ){
19454 goto opendb_out;
19455 }
19456 }
19457
19458 #ifdef SQLITE_ENABLE_FTS1
19459 if( !db->mallocFailed ){
19460 extern int sqlite3Fts1Init(sqlite3*);
19461 rc = sqlite3Fts1Init(db);
19462 }
19463 #endif
19464
19465 #ifdef SQLITE_ENABLE_FTS2
19466 if( !db->mallocFailed && rc==SQLITE_OK ){
19467 extern int sqlite3Fts2Init(sqlite3*);
19468 rc = sqlite3Fts2Init(db);
19469 }
19470 #endif
19471
19472 #ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */
19473 if( !db->mallocFailed && rc==SQLITE_OK ){
19474 rc = sqlite3Fts3Init(db);
19475 }
19476 #endif
19477
19478 #ifdef SQLITE_ENABLE_FTS5
19479 if( !db->mallocFailed && rc==SQLITE_OK ){
19480 rc = sqlite3Fts5Init(db);
19481 }
19482 #endif
19483
19484 #ifdef DEFAULT_ENABLE_RECOVER
19485 /* Initialize recover virtual table for testing. */
19486 extern int recoverVtableInit(sqlite3 *db);
19487 if( !db->mallocFailed && rc==SQLITE_OK ){
19488 rc = recoverVtableInit(db);
19489 }
19490 #endif
19491
19492 #ifdef SQLITE_ENABLE_ICU
19493 if( !db->mallocFailed && rc==SQLITE_OK ){
19494 rc = sqlite3IcuInit(db);
19495 }
19496 #endif
19497
19498 #ifdef SQLITE_ENABLE_RTREE
19499 if( !db->mallocFailed && rc==SQLITE_OK){
19500 rc = sqlite3RtreeInit(db);
19501 }
19502 #endif
19503
19504 #ifdef SQLITE_ENABLE_DBSTAT_VTAB
19505 if( !db->mallocFailed && rc==SQLITE_OK){
19506 rc = sqlite3DbstatRegister(db);
19507 }
19508 #endif
19509
19510 #ifdef SQLITE_ENABLE_JSON1
19511 if( !db->mallocFailed && rc==SQLITE_OK){
19512 rc = sqlite3Json1Init(db);
19513 }
19514 #endif
19515
19516 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
19517 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
19518 ** mode. Doing nothing at all also makes NORMAL the default.
19519 */
19520 #ifdef SQLITE_DEFAULT_LOCKING_MODE
19521 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
19522 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
19523 SQLITE_DEFAULT_LOCKING_MODE);
19524 #endif
19525
19526 if( rc ) sqlite3Error(db, rc);
19527
19528 /* Enable the lookaside-malloc subsystem */
19529 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
19530 sqlite3GlobalConfig.nLookaside);
19531
19532 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
19533
19534 opendb_out:
19535 if( db ){
19536 assert( db->mutex!=0 || isThreadsafe==0
19537 || sqlite3GlobalConfig.bFullMutex==0 );
19538 sqlite3_mutex_leave(db->mutex);
19539 }
19540 rc = sqlite3_errcode(db);
19541 assert( db!=0 || rc==SQLITE_NOMEM );
19542 if( rc==SQLITE_NOMEM ){
19543 sqlite3_close(db);
19544 db = 0;
19545 }else if( rc!=SQLITE_OK ){
19546 db->magic = SQLITE_MAGIC_SICK;
19547 }
19548 *ppDb = db;
19549 #ifdef SQLITE_ENABLE_SQLLOG
19550 if( sqlite3GlobalConfig.xSqllog ){
19551 /* Opening a db handle. Fourth parameter is passed 0. */
19552 void *pArg = sqlite3GlobalConfig.pSqllogArg;
19553 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
19554 }
19555 #endif
19556 #if defined(SQLITE_HAS_CODEC)
19557 if( rc==SQLITE_OK ){
19558 const char *zHexKey = sqlite3_uri_parameter(zOpen, "hexkey");
19559 if( zHexKey && zHexKey[0] ){
19560 u8 iByte;
19561 int i;
19562 char zKey[40];
19563 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zHexKey[i]); i++){
19564 iByte = (iByte<<4) + sqlite3HexToInt(zHexKey[i]);
19565 if( (i&1)!=0 ) zKey[i/2] = iByte;
19566 }
19567 sqlite3_key_v2(db, 0, zKey, i/2);
19568 }
19569 }
19570 #endif
19571 sqlite3_free(zOpen);
19572 return rc & 0xff;
19573 }
19574
19575 /*
19576 ** Open a new database handle.
19577 */
19578 SQLITE_API int SQLITE_STDCALL sqlite3_open(
19579 const char *zFilename,
19580 sqlite3 **ppDb
19581 ){
19582 return openDatabase(zFilename, ppDb,
19583 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
19584 }
19585 SQLITE_API int SQLITE_STDCALL sqlite3_open_v2(
19586 const char *filename, /* Database filename (UTF-8) */
19587 sqlite3 **ppDb, /* OUT: SQLite db handle */
19588 int flags, /* Flags */
19589 const char *zVfs /* Name of VFS module to use */
19590 ){
19591 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
19592 }
19593
19594 #ifndef SQLITE_OMIT_UTF16
19595 /*
19596 ** Open a new database handle.
19597 */
19598 SQLITE_API int SQLITE_STDCALL sqlite3_open16(
19599 const void *zFilename,
19600 sqlite3 **ppDb
19601 ){
19602 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
19603 sqlite3_value *pVal;
19604 int rc;
19605
19606 #ifdef SQLITE_ENABLE_API_ARMOR
19607 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
19608 #endif
19609 *ppDb = 0;
19610 #ifndef SQLITE_OMIT_AUTOINIT
19611 rc = sqlite3_initialize();
19612 if( rc ) return rc;
19613 #endif
19614 if( zFilename==0 ) zFilename = "\000\000";
19615 pVal = sqlite3ValueNew(0);
19616 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
19617 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
19618 if( zFilename8 ){
19619 rc = openDatabase(zFilename8, ppDb,
19620 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
19621 assert( *ppDb || rc==SQLITE_NOMEM );
19622 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
19623 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
19624 }
19625 }else{
19626 rc = SQLITE_NOMEM;
19627 }
19628 sqlite3ValueFree(pVal);
19629
19630 return rc & 0xff;
19631 }
19632 #endif /* SQLITE_OMIT_UTF16 */
19633
19634 /*
19635 ** Register a new collation sequence with the database handle db.
19636 */
19637 SQLITE_API int SQLITE_STDCALL sqlite3_create_collation(
19638 sqlite3* db,
19639 const char *zName,
19640 int enc,
19641 void* pCtx,
19642 int(*xCompare)(void*,int,const void*,int,const void*)
19643 ){
19644 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
19645 }
19646
19647 /*
19648 ** Register a new collation sequence with the database handle db.
19649 */
19650 SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2(
19651 sqlite3* db,
19652 const char *zName,
19653 int enc,
19654 void* pCtx,
19655 int(*xCompare)(void*,int,const void*,int,const void*),
19656 void(*xDel)(void*)
19657 ){
19658 int rc;
19659
19660 #ifdef SQLITE_ENABLE_API_ARMOR
19661 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
19662 #endif
19663 sqlite3_mutex_enter(db->mutex);
19664 assert( !db->mallocFailed );
19665 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
19666 rc = sqlite3ApiExit(db, rc);
19667 sqlite3_mutex_leave(db->mutex);
19668 return rc;
19669 }
19670
19671 #ifndef SQLITE_OMIT_UTF16
19672 /*
19673 ** Register a new collation sequence with the database handle db.
19674 */
19675 SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16(
19676 sqlite3* db,
19677 const void *zName,
19678 int enc,
19679 void* pCtx,
19680 int(*xCompare)(void*,int,const void*,int,const void*)
19681 ){
19682 int rc = SQLITE_OK;
19683 char *zName8;
19684
19685 #ifdef SQLITE_ENABLE_API_ARMOR
19686 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
19687 #endif
19688 sqlite3_mutex_enter(db->mutex);
19689 assert( !db->mallocFailed );
19690 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
19691 if( zName8 ){
19692 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
19693 sqlite3DbFree(db, zName8);
19694 }
19695 rc = sqlite3ApiExit(db, rc);
19696 sqlite3_mutex_leave(db->mutex);
19697 return rc;
19698 }
19699 #endif /* SQLITE_OMIT_UTF16 */
19700
19701 /*
19702 ** Register a collation sequence factory callback with the database handle
19703 ** db. Replace any previously installed collation sequence factory.
19704 */
19705 SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed(
19706 sqlite3 *db,
19707 void *pCollNeededArg,
19708 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
19709 ){
19710 #ifdef SQLITE_ENABLE_API_ARMOR
19711 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
19712 #endif
19713 sqlite3_mutex_enter(db->mutex);
19714 db->xCollNeeded = xCollNeeded;
19715 db->xCollNeeded16 = 0;
19716 db->pCollNeededArg = pCollNeededArg;
19717 sqlite3_mutex_leave(db->mutex);
19718 return SQLITE_OK;
19719 }
19720
19721 #ifndef SQLITE_OMIT_UTF16
19722 /*
19723 ** Register a collation sequence factory callback with the database handle
19724 ** db. Replace any previously installed collation sequence factory.
19725 */
19726 SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16(
19727 sqlite3 *db,
19728 void *pCollNeededArg,
19729 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
19730 ){
19731 #ifdef SQLITE_ENABLE_API_ARMOR
19732 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
19733 #endif
19734 sqlite3_mutex_enter(db->mutex);
19735 db->xCollNeeded = 0;
19736 db->xCollNeeded16 = xCollNeeded16;
19737 db->pCollNeededArg = pCollNeededArg;
19738 sqlite3_mutex_leave(db->mutex);
19739 return SQLITE_OK;
19740 }
19741 #endif /* SQLITE_OMIT_UTF16 */
19742
19743 #ifndef SQLITE_OMIT_DEPRECATED
19744 /*
19745 ** This function is now an anachronism. It used to be used to recover from a
19746 ** malloc() failure, but SQLite now does this automatically.
19747 */
19748 SQLITE_API int SQLITE_STDCALL sqlite3_global_recover(void){
19749 return SQLITE_OK;
19750 }
19751 #endif
19752
19753 /*
19754 ** Test to see whether or not the database connection is in autocommit
19755 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
19756 ** by default. Autocommit is disabled by a BEGIN statement and reenabled
19757 ** by the next COMMIT or ROLLBACK.
19758 */
19759 SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3 *db){
19760 #ifdef SQLITE_ENABLE_API_ARMOR
19761 if( !sqlite3SafetyCheckOk(db) ){
19762 (void)SQLITE_MISUSE_BKPT;
19763 return 0;
19764 }
19765 #endif
19766 return db->autoCommit;
19767 }
19768
19769 /*
19770 ** The following routines are substitutes for constants SQLITE_CORRUPT,
19771 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
19772 ** constants. They serve two purposes:
19773 **
19774 ** 1. Serve as a convenient place to set a breakpoint in a debugger
19775 ** to detect when version error conditions occurs.
19776 **
19777 ** 2. Invoke sqlite3_log() to provide the source code location where
19778 ** a low-level error is first detected.
19779 */
19780 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
19781 testcase( sqlite3GlobalConfig.xLog!=0 );
19782 sqlite3_log(SQLITE_CORRUPT,
19783 "database corruption at line %d of [%.10s]",
19784 lineno, 20+sqlite3_sourceid());
19785 return SQLITE_CORRUPT;
19786 }
19787 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
19788 testcase( sqlite3GlobalConfig.xLog!=0 );
19789 sqlite3_log(SQLITE_MISUSE,
19790 "misuse at line %d of [%.10s]",
19791 lineno, 20+sqlite3_sourceid());
19792 return SQLITE_MISUSE;
19793 }
19794 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
19795 testcase( sqlite3GlobalConfig.xLog!=0 );
19796 sqlite3_log(SQLITE_CANTOPEN,
19797 "cannot open file at line %d of [%.10s]",
19798 lineno, 20+sqlite3_sourceid());
19799 return SQLITE_CANTOPEN;
19800 }
19801
19802
19803 #ifndef SQLITE_OMIT_DEPRECATED
19804 /*
19805 ** This is a convenience routine that makes sure that all thread-specific
19806 ** data for this thread has been deallocated.
19807 **
19808 ** SQLite no longer uses thread-specific data so this routine is now a
19809 ** no-op. It is retained for historical compatibility.
19810 */
19811 SQLITE_API void SQLITE_STDCALL sqlite3_thread_cleanup(void){
19812 }
19813 #endif
19814
19815 /*
19816 ** Return meta information about a specific column of a database table.
19817 ** See comment in sqlite3.h (sqlite.h.in) for details.
19818 */
19819 SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
19820 sqlite3 *db, /* Connection handle */
19821 const char *zDbName, /* Database name or NULL */
19822 const char *zTableName, /* Table name */
19823 const char *zColumnName, /* Column name */
19824 char const **pzDataType, /* OUTPUT: Declared data type */
19825 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
19826 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
19827 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
19828 int *pAutoinc /* OUTPUT: True if column is auto-increment */
19829 ){
19830 int rc;
19831 char *zErrMsg = 0;
19832 Table *pTab = 0;
19833 Column *pCol = 0;
19834 int iCol = 0;
19835 char const *zDataType = 0;
19836 char const *zCollSeq = 0;
19837 int notnull = 0;
19838 int primarykey = 0;
19839 int autoinc = 0;
19840
19841
19842 #ifdef SQLITE_ENABLE_API_ARMOR
19843 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
19844 return SQLITE_MISUSE_BKPT;
19845 }
19846 #endif
19847
19848 /* Ensure the database schema has been loaded */
19849 sqlite3_mutex_enter(db->mutex);
19850 sqlite3BtreeEnterAll(db);
19851 rc = sqlite3Init(db, &zErrMsg);
19852 if( SQLITE_OK!=rc ){
19853 goto error_out;
19854 }
19855
19856 /* Locate the table in question */
19857 pTab = sqlite3FindTable(db, zTableName, zDbName);
19858 if( !pTab || pTab->pSelect ){
19859 pTab = 0;
19860 goto error_out;
19861 }
19862
19863 /* Find the column for which info is requested */
19864 if( zColumnName==0 ){
19865 /* Query for existance of table only */
19866 }else{
19867 for(iCol=0; iCol<pTab->nCol; iCol++){
19868 pCol = &pTab->aCol[iCol];
19869 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
19870 break;
19871 }
19872 }
19873 if( iCol==pTab->nCol ){
19874 if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){
19875 iCol = pTab->iPKey;
19876 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
19877 }else{
19878 pTab = 0;
19879 goto error_out;
19880 }
19881 }
19882 }
19883
19884 /* The following block stores the meta information that will be returned
19885 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
19886 ** and autoinc. At this point there are two possibilities:
19887 **
19888 ** 1. The specified column name was rowid", "oid" or "_rowid_"
19889 ** and there is no explicitly declared IPK column.
19890 **
19891 ** 2. The table is not a view and the column name identified an
19892 ** explicitly declared column. Copy meta information from *pCol.
19893 */
19894 if( pCol ){
19895 zDataType = pCol->zType;
19896 zCollSeq = pCol->zColl;
19897 notnull = pCol->notNull!=0;
19898 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
19899 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
19900 }else{
19901 zDataType = "INTEGER";
19902 primarykey = 1;
19903 }
19904 if( !zCollSeq ){
19905 zCollSeq = sqlite3StrBINARY;
19906 }
19907
19908 error_out:
19909 sqlite3BtreeLeaveAll(db);
19910
19911 /* Whether the function call succeeded or failed, set the output parameters
19912 ** to whatever their local counterparts contain. If an error did occur,
19913 ** this has the effect of zeroing all output parameters.
19914 */
19915 if( pzDataType ) *pzDataType = zDataType;
19916 if( pzCollSeq ) *pzCollSeq = zCollSeq;
19917 if( pNotNull ) *pNotNull = notnull;
19918 if( pPrimaryKey ) *pPrimaryKey = primarykey;
19919 if( pAutoinc ) *pAutoinc = autoinc;
19920
19921 if( SQLITE_OK==rc && !pTab ){
19922 sqlite3DbFree(db, zErrMsg);
19923 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
19924 zColumnName);
19925 rc = SQLITE_ERROR;
19926 }
19927 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
19928 sqlite3DbFree(db, zErrMsg);
19929 rc = sqlite3ApiExit(db, rc);
19930 sqlite3_mutex_leave(db->mutex);
19931 return rc;
19932 }
19933
19934 /*
19935 ** Sleep for a little while. Return the amount of time slept.
19936 */
19937 SQLITE_API int SQLITE_STDCALL sqlite3_sleep(int ms){
19938 sqlite3_vfs *pVfs;
19939 int rc;
19940 pVfs = sqlite3_vfs_find(0);
19941 if( pVfs==0 ) return 0;
19942
19943 /* This function works in milliseconds, but the underlying OsSleep()
19944 ** API uses microseconds. Hence the 1000's.
19945 */
19946 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
19947 return rc;
19948 }
19949
19950 /*
19951 ** Enable or disable the extended result codes.
19952 */
19953 SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3 *db, int ono ff){
19954 #ifdef SQLITE_ENABLE_API_ARMOR
19955 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
19956 #endif
19957 sqlite3_mutex_enter(db->mutex);
19958 db->errMask = onoff ? 0xffffffff : 0xff;
19959 sqlite3_mutex_leave(db->mutex);
19960 return SQLITE_OK;
19961 }
19962
19963 /*
19964 ** Invoke the xFileControl method on a particular database.
19965 */
19966 SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3 *db, const char *zDbN ame, int op, void *pArg){
19967 int rc = SQLITE_ERROR;
19968 Btree *pBtree;
19969
19970 #ifdef SQLITE_ENABLE_API_ARMOR
19971 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
19972 #endif
19973 sqlite3_mutex_enter(db->mutex);
19974 pBtree = sqlite3DbNameToBtree(db, zDbName);
19975 if( pBtree ){
19976 Pager *pPager;
19977 sqlite3_file *fd;
19978 sqlite3BtreeEnter(pBtree);
19979 pPager = sqlite3BtreePager(pBtree);
19980 assert( pPager!=0 );
19981 fd = sqlite3PagerFile(pPager);
19982 assert( fd!=0 );
19983 if( op==SQLITE_FCNTL_FILE_POINTER ){
19984 *(sqlite3_file**)pArg = fd;
19985 rc = SQLITE_OK;
19986 }else if( op==SQLITE_FCNTL_VFS_POINTER ){
19987 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
19988 rc = SQLITE_OK;
19989 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
19990 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
19991 rc = SQLITE_OK;
19992 }else if( fd->pMethods ){
19993 rc = sqlite3OsFileControl(fd, op, pArg);
19994 }else{
19995 rc = SQLITE_NOTFOUND;
19996 }
19997 sqlite3BtreeLeave(pBtree);
19998 }
19999 sqlite3_mutex_leave(db->mutex);
20000 return rc;
20001 }
20002
20003 /*
20004 ** Interface to the testing logic.
20005 */
20006 SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
20007 int rc = 0;
20008 #ifdef SQLITE_OMIT_BUILTIN_TEST
20009 UNUSED_PARAMETER(op);
20010 #else
20011 va_list ap;
20012 va_start(ap, op);
20013 switch( op ){
20014
20015 /*
20016 ** Save the current state of the PRNG.
20017 */
20018 case SQLITE_TESTCTRL_PRNG_SAVE: {
20019 sqlite3PrngSaveState();
20020 break;
20021 }
20022
20023 /*
20024 ** Restore the state of the PRNG to the last state saved using
20025 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
20026 ** this verb acts like PRNG_RESET.
20027 */
20028 case SQLITE_TESTCTRL_PRNG_RESTORE: {
20029 sqlite3PrngRestoreState();
20030 break;
20031 }
20032
20033 /*
20034 ** Reset the PRNG back to its uninitialized state. The next call
20035 ** to sqlite3_randomness() will reseed the PRNG using a single call
20036 ** to the xRandomness method of the default VFS.
20037 */
20038 case SQLITE_TESTCTRL_PRNG_RESET: {
20039 sqlite3_randomness(0,0);
20040 break;
20041 }
20042
20043 /*
20044 ** sqlite3_test_control(BITVEC_TEST, size, program)
20045 **
20046 ** Run a test against a Bitvec object of size. The program argument
20047 ** is an array of integers that defines the test. Return -1 on a
20048 ** memory allocation error, 0 on success, or non-zero for an error.
20049 ** See the sqlite3BitvecBuiltinTest() for additional information.
20050 */
20051 case SQLITE_TESTCTRL_BITVEC_TEST: {
20052 int sz = va_arg(ap, int);
20053 int *aProg = va_arg(ap, int*);
20054 rc = sqlite3BitvecBuiltinTest(sz, aProg);
20055 break;
20056 }
20057
20058 /*
20059 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
20060 **
20061 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
20062 ** if xCallback is not NULL.
20063 **
20064 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
20065 ** is called immediately after installing the new callback and the return
20066 ** value from sqlite3FaultSim(0) becomes the return from
20067 ** sqlite3_test_control().
20068 */
20069 case SQLITE_TESTCTRL_FAULT_INSTALL: {
20070 /* MSVC is picky about pulling func ptrs from va lists.
20071 ** http://support.microsoft.com/kb/47961
20072 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
20073 */
20074 typedef int(*TESTCALLBACKFUNC_t)(int);
20075 sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
20076 rc = sqlite3FaultSim(0);
20077 break;
20078 }
20079
20080 /*
20081 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
20082 **
20083 ** Register hooks to call to indicate which malloc() failures
20084 ** are benign.
20085 */
20086 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
20087 typedef void (*void_function)(void);
20088 void_function xBenignBegin;
20089 void_function xBenignEnd;
20090 xBenignBegin = va_arg(ap, void_function);
20091 xBenignEnd = va_arg(ap, void_function);
20092 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
20093 break;
20094 }
20095
20096 /*
20097 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
20098 **
20099 ** Set the PENDING byte to the value in the argument, if X>0.
20100 ** Make no changes if X==0. Return the value of the pending byte
20101 ** as it existing before this routine was called.
20102 **
20103 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
20104 ** an incompatible database file format. Changing the PENDING byte
20105 ** while any database connection is open results in undefined and
20106 ** deleterious behavior.
20107 */
20108 case SQLITE_TESTCTRL_PENDING_BYTE: {
20109 rc = PENDING_BYTE;
20110 #ifndef SQLITE_OMIT_WSD
20111 {
20112 unsigned int newVal = va_arg(ap, unsigned int);
20113 if( newVal ) sqlite3PendingByte = newVal;
20114 }
20115 #endif
20116 break;
20117 }
20118
20119 /*
20120 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
20121 **
20122 ** This action provides a run-time test to see whether or not
20123 ** assert() was enabled at compile-time. If X is true and assert()
20124 ** is enabled, then the return value is true. If X is true and
20125 ** assert() is disabled, then the return value is zero. If X is
20126 ** false and assert() is enabled, then the assertion fires and the
20127 ** process aborts. If X is false and assert() is disabled, then the
20128 ** return value is zero.
20129 */
20130 case SQLITE_TESTCTRL_ASSERT: {
20131 volatile int x = 0;
20132 assert( (x = va_arg(ap,int))!=0 );
20133 rc = x;
20134 break;
20135 }
20136
20137
20138 /*
20139 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
20140 **
20141 ** This action provides a run-time test to see how the ALWAYS and
20142 ** NEVER macros were defined at compile-time.
20143 **
20144 ** The return value is ALWAYS(X).
20145 **
20146 ** The recommended test is X==2. If the return value is 2, that means
20147 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
20148 ** default setting. If the return value is 1, then ALWAYS() is either
20149 ** hard-coded to true or else it asserts if its argument is false.
20150 ** The first behavior (hard-coded to true) is the case if
20151 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
20152 ** behavior (assert if the argument to ALWAYS() is false) is the case if
20153 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
20154 **
20155 ** The run-time test procedure might look something like this:
20156 **
20157 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
20158 ** // ALWAYS() and NEVER() are no-op pass-through macros
20159 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
20160 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
20161 ** }else{
20162 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
20163 ** }
20164 */
20165 case SQLITE_TESTCTRL_ALWAYS: {
20166 int x = va_arg(ap,int);
20167 rc = ALWAYS(x);
20168 break;
20169 }
20170
20171 /*
20172 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
20173 **
20174 ** The integer returned reveals the byte-order of the computer on which
20175 ** SQLite is running:
20176 **
20177 ** 1 big-endian, determined at run-time
20178 ** 10 little-endian, determined at run-time
20179 ** 432101 big-endian, determined at compile-time
20180 ** 123410 little-endian, determined at compile-time
20181 */
20182 case SQLITE_TESTCTRL_BYTEORDER: {
20183 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
20184 break;
20185 }
20186
20187 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
20188 **
20189 ** Set the nReserve size to N for the main database on the database
20190 ** connection db.
20191 */
20192 case SQLITE_TESTCTRL_RESERVE: {
20193 sqlite3 *db = va_arg(ap, sqlite3*);
20194 int x = va_arg(ap,int);
20195 sqlite3_mutex_enter(db->mutex);
20196 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
20197 sqlite3_mutex_leave(db->mutex);
20198 break;
20199 }
20200
20201 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
20202 **
20203 ** Enable or disable various optimizations for testing purposes. The
20204 ** argument N is a bitmask of optimizations to be disabled. For normal
20205 ** operation N should be 0. The idea is that a test program (like the
20206 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
20207 ** with various optimizations disabled to verify that the same answer
20208 ** is obtained in every case.
20209 */
20210 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
20211 sqlite3 *db = va_arg(ap, sqlite3*);
20212 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
20213 break;
20214 }
20215
20216 #ifdef SQLITE_N_KEYWORD
20217 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
20218 **
20219 ** If zWord is a keyword recognized by the parser, then return the
20220 ** number of keywords. Or if zWord is not a keyword, return 0.
20221 **
20222 ** This test feature is only available in the amalgamation since
20223 ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
20224 ** is built using separate source files.
20225 */
20226 case SQLITE_TESTCTRL_ISKEYWORD: {
20227 const char *zWord = va_arg(ap, const char*);
20228 int n = sqlite3Strlen30(zWord);
20229 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
20230 break;
20231 }
20232 #endif
20233
20234 /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
20235 **
20236 ** Pass pFree into sqlite3ScratchFree().
20237 ** If sz>0 then allocate a scratch buffer into pNew.
20238 */
20239 case SQLITE_TESTCTRL_SCRATCHMALLOC: {
20240 void *pFree, **ppNew;
20241 int sz;
20242 sz = va_arg(ap, int);
20243 ppNew = va_arg(ap, void**);
20244 pFree = va_arg(ap, void*);
20245 if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
20246 sqlite3ScratchFree(pFree);
20247 break;
20248 }
20249
20250 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
20251 **
20252 ** If parameter onoff is non-zero, configure the wrappers so that all
20253 ** subsequent calls to localtime() and variants fail. If onoff is zero,
20254 ** undo this setting.
20255 */
20256 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
20257 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
20258 break;
20259 }
20260
20261 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
20262 **
20263 ** Set or clear a flag that indicates that the database file is always well-
20264 ** formed and never corrupt. This flag is clear by default, indicating that
20265 ** database files might have arbitrary corruption. Setting the flag during
20266 ** testing causes certain assert() statements in the code to be activated
20267 ** that demonstrat invariants on well-formed database files.
20268 */
20269 case SQLITE_TESTCTRL_NEVER_CORRUPT: {
20270 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
20271 break;
20272 }
20273
20274
20275 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
20276 **
20277 ** Set the VDBE coverage callback function to xCallback with context
20278 ** pointer ptr.
20279 */
20280 case SQLITE_TESTCTRL_VDBE_COVERAGE: {
20281 #ifdef SQLITE_VDBE_COVERAGE
20282 typedef void (*branch_callback)(void*,int,u8,u8);
20283 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
20284 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
20285 #endif
20286 break;
20287 }
20288
20289 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
20290 case SQLITE_TESTCTRL_SORTER_MMAP: {
20291 sqlite3 *db = va_arg(ap, sqlite3*);
20292 db->nMaxSorterMmap = va_arg(ap, int);
20293 break;
20294 }
20295
20296 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
20297 **
20298 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
20299 ** not.
20300 */
20301 case SQLITE_TESTCTRL_ISINIT: {
20302 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
20303 break;
20304 }
20305
20306 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
20307 **
20308 ** This test control is used to create imposter tables. "db" is a pointer
20309 ** to the database connection. dbName is the database name (ex: "main" or
20310 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
20311 ** or off. "tnum" is the root page of the b-tree to which the imposter
20312 ** table should connect.
20313 **
20314 ** Enable imposter mode only when the schema has already been parsed. Then
20315 ** run a single CREATE TABLE statement to construct the imposter table in
20316 ** the parsed schema. Then turn imposter mode back off again.
20317 **
20318 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
20319 ** the schema to be reparsed the next time it is needed. This has the
20320 ** effect of erasing all imposter tables.
20321 */
20322 case SQLITE_TESTCTRL_IMPOSTER: {
20323 sqlite3 *db = va_arg(ap, sqlite3*);
20324 sqlite3_mutex_enter(db->mutex);
20325 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
20326 db->init.busy = db->init.imposterTable = va_arg(ap,int);
20327 db->init.newTnum = va_arg(ap,int);
20328 if( db->init.busy==0 && db->init.newTnum>0 ){
20329 sqlite3ResetAllSchemasOfConnection(db);
20330 }
20331 sqlite3_mutex_leave(db->mutex);
20332 break;
20333 }
20334 }
20335 va_end(ap);
20336 #endif /* SQLITE_OMIT_BUILTIN_TEST */
20337 return rc;
20338 }
20339
20340 /*
20341 ** This is a utility routine, useful to VFS implementations, that checks
20342 ** to see if a database file was a URI that contained a specific query
20343 ** parameter, and if so obtains the value of the query parameter.
20344 **
20345 ** The zFilename argument is the filename pointer passed into the xOpen()
20346 ** method of a VFS implementation. The zParam argument is the name of the
20347 ** query parameter we seek. This routine returns the value of the zParam
20348 ** parameter if it exists. If the parameter does not exist, this routine
20349 ** returns a NULL pointer.
20350 */
20351 SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilenam e, const char *zParam){
20352 if( zFilename==0 || zParam==0 ) return 0;
20353 zFilename += sqlite3Strlen30(zFilename) + 1;
20354 while( zFilename[0] ){
20355 int x = strcmp(zFilename, zParam);
20356 zFilename += sqlite3Strlen30(zFilename) + 1;
20357 if( x==0 ) return zFilename;
20358 zFilename += sqlite3Strlen30(zFilename) + 1;
20359 }
20360 return 0;
20361 }
20362
20363 /*
20364 ** Return a boolean value for a query parameter.
20365 */
20366 SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFilename, const c har *zParam, int bDflt){
20367 const char *z = sqlite3_uri_parameter(zFilename, zParam);
20368 bDflt = bDflt!=0;
20369 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
20370 }
20371
20372 /*
20373 ** Return a 64-bit integer value for a query parameter.
20374 */
20375 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(
20376 const char *zFilename, /* Filename as passed to xOpen */
20377 const char *zParam, /* URI parameter sought */
20378 sqlite3_int64 bDflt /* return if parameter is missing */
20379 ){
20380 const char *z = sqlite3_uri_parameter(zFilename, zParam);
20381 sqlite3_int64 v;
20382 if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){
20383 bDflt = v;
20384 }
20385 return bDflt;
20386 }
20387
20388 /*
20389 ** Return the Btree pointer identified by zDbName. Return NULL if not found.
20390 */
20391 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
20392 int i;
20393 for(i=0; i<db->nDb; i++){
20394 if( db->aDb[i].pBt
20395 && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0)
20396 ){
20397 return db->aDb[i].pBt;
20398 }
20399 }
20400 return 0;
20401 }
20402
20403 /*
20404 ** Return the filename of the database associated with a database
20405 ** connection.
20406 */
20407 SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const cha r *zDbName){
20408 Btree *pBt;
20409 #ifdef SQLITE_ENABLE_API_ARMOR
20410 if( !sqlite3SafetyCheckOk(db) ){
20411 (void)SQLITE_MISUSE_BKPT;
20412 return 0;
20413 }
20414 #endif
20415 pBt = sqlite3DbNameToBtree(db, zDbName);
20416 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
20417 }
20418
20419 /*
20420 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
20421 ** no such database exists.
20422 */
20423 SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbNa me){
20424 Btree *pBt;
20425 #ifdef SQLITE_ENABLE_API_ARMOR
20426 if( !sqlite3SafetyCheckOk(db) ){
20427 (void)SQLITE_MISUSE_BKPT;
20428 return -1;
20429 }
20430 #endif
20431 pBt = sqlite3DbNameToBtree(db, zDbName);
20432 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
20433 }
20434
20435 #ifdef SQLITE_ENABLE_SNAPSHOT
20436 /*
20437 ** Obtain a snapshot handle for the snapshot of database zDb currently
20438 ** being read by handle db.
20439 */
20440 SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_get(
20441 sqlite3 *db,
20442 const char *zDb,
20443 sqlite3_snapshot **ppSnapshot
20444 ){
20445 int rc = SQLITE_ERROR;
20446 #ifndef SQLITE_OMIT_WAL
20447 int iDb;
20448
20449 #ifdef SQLITE_ENABLE_API_ARMOR
20450 if( !sqlite3SafetyCheckOk(db) ){
20451 return SQLITE_MISUSE_BKPT;
20452 }
20453 #endif
20454 sqlite3_mutex_enter(db->mutex);
20455
20456 iDb = sqlite3FindDbName(db, zDb);
20457 if( iDb==0 || iDb>1 ){
20458 Btree *pBt = db->aDb[iDb].pBt;
20459 if( 0==sqlite3BtreeIsInTrans(pBt) ){
20460 rc = sqlite3BtreeBeginTrans(pBt, 0);
20461 if( rc==SQLITE_OK ){
20462 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
20463 }
20464 }
20465 }
20466
20467 sqlite3_mutex_leave(db->mutex);
20468 #endif /* SQLITE_OMIT_WAL */
20469 return rc;
20470 }
20471
20472 /*
20473 ** Open a read-transaction on the snapshot idendified by pSnapshot.
20474 */
20475 SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_open(
20476 sqlite3 *db,
20477 const char *zDb,
20478 sqlite3_snapshot *pSnapshot
20479 ){
20480 int rc = SQLITE_ERROR;
20481 #ifndef SQLITE_OMIT_WAL
20482
20483 #ifdef SQLITE_ENABLE_API_ARMOR
20484 if( !sqlite3SafetyCheckOk(db) ){
20485 return SQLITE_MISUSE_BKPT;
20486 }
20487 #endif
20488 sqlite3_mutex_enter(db->mutex);
20489 if( db->autoCommit==0 ){
20490 int iDb;
20491 iDb = sqlite3FindDbName(db, zDb);
20492 if( iDb==0 || iDb>1 ){
20493 Btree *pBt = db->aDb[iDb].pBt;
20494 if( 0==sqlite3BtreeIsInReadTrans(pBt) ){
20495 rc = sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), pSnapshot);
20496 if( rc==SQLITE_OK ){
20497 rc = sqlite3BtreeBeginTrans(pBt, 0);
20498 sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), 0);
20499 }
20500 }
20501 }
20502 }
20503
20504 sqlite3_mutex_leave(db->mutex);
20505 #endif /* SQLITE_OMIT_WAL */
20506 return rc;
20507 }
20508
20509 /*
20510 ** Free a snapshot handle obtained from sqlite3_snapshot_get().
20511 */
20512 SQLITE_API void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot ){
20513 sqlite3_free(pSnapshot);
20514 }
20515 #endif /* SQLITE_ENABLE_SNAPSHOT */
20516
20517 /************** End of main.c ************************************************/
20518 /************** Begin file notify.c ******************************************/
20519 /*
20520 ** 2009 March 3
20521 **
20522 ** The author disclaims copyright to this source code. In place of
20523 ** a legal notice, here is a blessing:
20524 **
20525 ** May you do good and not evil.
20526 ** May you find forgiveness for yourself and forgive others.
20527 ** May you share freely, never taking more than you give.
20528 **
20529 *************************************************************************
20530 **
20531 ** This file contains the implementation of the sqlite3_unlock_notify()
20532 ** API method and its associated functionality.
20533 */
20534 /* #include "sqliteInt.h" */
20535 /* #include "btreeInt.h" */
20536
20537 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
20538 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
20539
20540 /*
20541 ** Public interfaces:
20542 **
20543 ** sqlite3ConnectionBlocked()
20544 ** sqlite3ConnectionUnlocked()
20545 ** sqlite3ConnectionClosed()
20546 ** sqlite3_unlock_notify()
20547 */
20548
20549 #define assertMutexHeld() \
20550 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
20551
20552 /*
20553 ** Head of a linked list of all sqlite3 objects created by this process
20554 ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
20555 ** is not NULL. This variable may only accessed while the STATIC_MASTER
20556 ** mutex is held.
20557 */
20558 static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
20559
20560 #ifndef NDEBUG
20561 /*
20562 ** This function is a complex assert() that verifies the following
20563 ** properties of the blocked connections list:
20564 **
20565 ** 1) Each entry in the list has a non-NULL value for either
20566 ** pUnlockConnection or pBlockingConnection, or both.
20567 **
20568 ** 2) All entries in the list that share a common value for
20569 ** xUnlockNotify are grouped together.
20570 **
20571 ** 3) If the argument db is not NULL, then none of the entries in the
20572 ** blocked connections list have pUnlockConnection or pBlockingConnection
20573 ** set to db. This is used when closing connection db.
20574 */
20575 static void checkListProperties(sqlite3 *db){
20576 sqlite3 *p;
20577 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
20578 int seen = 0;
20579 sqlite3 *p2;
20580
20581 /* Verify property (1) */
20582 assert( p->pUnlockConnection || p->pBlockingConnection );
20583
20584 /* Verify property (2) */
20585 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
20586 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
20587 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
20588 assert( db==0 || p->pUnlockConnection!=db );
20589 assert( db==0 || p->pBlockingConnection!=db );
20590 }
20591 }
20592 }
20593 #else
20594 # define checkListProperties(x)
20595 #endif
20596
20597 /*
20598 ** Remove connection db from the blocked connections list. If connection
20599 ** db is not currently a part of the list, this function is a no-op.
20600 */
20601 static void removeFromBlockedList(sqlite3 *db){
20602 sqlite3 **pp;
20603 assertMutexHeld();
20604 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
20605 if( *pp==db ){
20606 *pp = (*pp)->pNextBlocked;
20607 break;
20608 }
20609 }
20610 }
20611
20612 /*
20613 ** Add connection db to the blocked connections list. It is assumed
20614 ** that it is not already a part of the list.
20615 */
20616 static void addToBlockedList(sqlite3 *db){
20617 sqlite3 **pp;
20618 assertMutexHeld();
20619 for(
20620 pp=&sqlite3BlockedList;
20621 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
20622 pp=&(*pp)->pNextBlocked
20623 );
20624 db->pNextBlocked = *pp;
20625 *pp = db;
20626 }
20627
20628 /*
20629 ** Obtain the STATIC_MASTER mutex.
20630 */
20631 static void enterMutex(void){
20632 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
20633 checkListProperties(0);
20634 }
20635
20636 /*
20637 ** Release the STATIC_MASTER mutex.
20638 */
20639 static void leaveMutex(void){
20640 assertMutexHeld();
20641 checkListProperties(0);
20642 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
20643 }
20644
20645 /*
20646 ** Register an unlock-notify callback.
20647 **
20648 ** This is called after connection "db" has attempted some operation
20649 ** but has received an SQLITE_LOCKED error because another connection
20650 ** (call it pOther) in the same process was busy using the same shared
20651 ** cache. pOther is found by looking at db->pBlockingConnection.
20652 **
20653 ** If there is no blocking connection, the callback is invoked immediately,
20654 ** before this routine returns.
20655 **
20656 ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
20657 ** a deadlock.
20658 **
20659 ** Otherwise, make arrangements to invoke xNotify when pOther drops
20660 ** its locks.
20661 **
20662 ** Each call to this routine overrides any prior callbacks registered
20663 ** on the same "db". If xNotify==0 then any prior callbacks are immediately
20664 ** cancelled.
20665 */
20666 SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify(
20667 sqlite3 *db,
20668 void (*xNotify)(void **, int),
20669 void *pArg
20670 ){
20671 int rc = SQLITE_OK;
20672
20673 sqlite3_mutex_enter(db->mutex);
20674 enterMutex();
20675
20676 if( xNotify==0 ){
20677 removeFromBlockedList(db);
20678 db->pBlockingConnection = 0;
20679 db->pUnlockConnection = 0;
20680 db->xUnlockNotify = 0;
20681 db->pUnlockArg = 0;
20682 }else if( 0==db->pBlockingConnection ){
20683 /* The blocking transaction has been concluded. Or there never was a
20684 ** blocking transaction. In either case, invoke the notify callback
20685 ** immediately.
20686 */
20687 xNotify(&pArg, 1);
20688 }else{
20689 sqlite3 *p;
20690
20691 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
20692 if( p ){
20693 rc = SQLITE_LOCKED; /* Deadlock detected. */
20694 }else{
20695 db->pUnlockConnection = db->pBlockingConnection;
20696 db->xUnlockNotify = xNotify;
20697 db->pUnlockArg = pArg;
20698 removeFromBlockedList(db);
20699 addToBlockedList(db);
20700 }
20701 }
20702
20703 leaveMutex();
20704 assert( !db->mallocFailed );
20705 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
20706 sqlite3_mutex_leave(db->mutex);
20707 return rc;
20708 }
20709
20710 /*
20711 ** This function is called while stepping or preparing a statement
20712 ** associated with connection db. The operation will return SQLITE_LOCKED
20713 ** to the user because it requires a lock that will not be available
20714 ** until connection pBlocker concludes its current transaction.
20715 */
20716 SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
20717 enterMutex();
20718 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
20719 addToBlockedList(db);
20720 }
20721 db->pBlockingConnection = pBlocker;
20722 leaveMutex();
20723 }
20724
20725 /*
20726 ** This function is called when
20727 ** the transaction opened by database db has just finished. Locks held
20728 ** by database connection db have been released.
20729 **
20730 ** This function loops through each entry in the blocked connections
20731 ** list and does the following:
20732 **
20733 ** 1) If the sqlite3.pBlockingConnection member of a list entry is
20734 ** set to db, then set pBlockingConnection=0.
20735 **
20736 ** 2) If the sqlite3.pUnlockConnection member of a list entry is
20737 ** set to db, then invoke the configured unlock-notify callback and
20738 ** set pUnlockConnection=0.
20739 **
20740 ** 3) If the two steps above mean that pBlockingConnection==0 and
20741 ** pUnlockConnection==0, remove the entry from the blocked connections
20742 ** list.
20743 */
20744 SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
20745 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
20746 int nArg = 0; /* Number of entries in aArg[] */
20747 sqlite3 **pp; /* Iterator variable */
20748 void **aArg; /* Arguments to the unlock callback */
20749 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
20750 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
20751
20752 aArg = aStatic;
20753 enterMutex(); /* Enter STATIC_MASTER mutex */
20754
20755 /* This loop runs once for each entry in the blocked-connections list. */
20756 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
20757 sqlite3 *p = *pp;
20758
20759 /* Step 1. */
20760 if( p->pBlockingConnection==db ){
20761 p->pBlockingConnection = 0;
20762 }
20763
20764 /* Step 2. */
20765 if( p->pUnlockConnection==db ){
20766 assert( p->xUnlockNotify );
20767 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
20768 xUnlockNotify(aArg, nArg);
20769 nArg = 0;
20770 }
20771
20772 sqlite3BeginBenignMalloc();
20773 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
20774 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
20775 if( (!aDyn && nArg==(int)ArraySize(aStatic))
20776 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
20777 ){
20778 /* The aArg[] array needs to grow. */
20779 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
20780 if( pNew ){
20781 memcpy(pNew, aArg, nArg*sizeof(void *));
20782 sqlite3_free(aDyn);
20783 aDyn = aArg = pNew;
20784 }else{
20785 /* This occurs when the array of context pointers that need to
20786 ** be passed to the unlock-notify callback is larger than the
20787 ** aStatic[] array allocated on the stack and the attempt to
20788 ** allocate a larger array from the heap has failed.
20789 **
20790 ** This is a difficult situation to handle. Returning an error
20791 ** code to the caller is insufficient, as even if an error code
20792 ** is returned the transaction on connection db will still be
20793 ** closed and the unlock-notify callbacks on blocked connections
20794 ** will go unissued. This might cause the application to wait
20795 ** indefinitely for an unlock-notify callback that will never
20796 ** arrive.
20797 **
20798 ** Instead, invoke the unlock-notify callback with the context
20799 ** array already accumulated. We can then clear the array and
20800 ** begin accumulating any further context pointers without
20801 ** requiring any dynamic allocation. This is sub-optimal because
20802 ** it means that instead of one callback with a large array of
20803 ** context pointers the application will receive two or more
20804 ** callbacks with smaller arrays of context pointers, which will
20805 ** reduce the applications ability to prioritize multiple
20806 ** connections. But it is the best that can be done under the
20807 ** circumstances.
20808 */
20809 xUnlockNotify(aArg, nArg);
20810 nArg = 0;
20811 }
20812 }
20813 sqlite3EndBenignMalloc();
20814
20815 aArg[nArg++] = p->pUnlockArg;
20816 xUnlockNotify = p->xUnlockNotify;
20817 p->pUnlockConnection = 0;
20818 p->xUnlockNotify = 0;
20819 p->pUnlockArg = 0;
20820 }
20821
20822 /* Step 3. */
20823 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
20824 /* Remove connection p from the blocked connections list. */
20825 *pp = p->pNextBlocked;
20826 p->pNextBlocked = 0;
20827 }else{
20828 pp = &p->pNextBlocked;
20829 }
20830 }
20831
20832 if( nArg!=0 ){
20833 xUnlockNotify(aArg, nArg);
20834 }
20835 sqlite3_free(aDyn);
20836 leaveMutex(); /* Leave STATIC_MASTER mutex */
20837 }
20838
20839 /*
20840 ** This is called when the database connection passed as an argument is
20841 ** being closed. The connection is removed from the blocked list.
20842 */
20843 SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
20844 sqlite3ConnectionUnlocked(db);
20845 enterMutex();
20846 removeFromBlockedList(db);
20847 checkListProperties(db);
20848 leaveMutex();
20849 }
20850 #endif
20851
20852 /************** End of notify.c **********************************************/
20853 /************** Begin file recover.c *****************************************/
20854 /*
20855 ** 2012 Jan 11
20856 **
20857 ** The author disclaims copyright to this source code. In place of
20858 ** a legal notice, here is a blessing:
20859 **
20860 ** May you do good and not evil.
20861 ** May you find forgiveness for yourself and forgive others.
20862 ** May you share freely, never taking more than you give.
20863 */
20864 /* TODO(shess): THIS MODULE IS STILL EXPERIMENTAL. DO NOT USE IT. */
20865 /* Implements a virtual table "recover" which can be used to recover
20866 * data from a corrupt table. The table is walked manually, with
20867 * corrupt items skipped. Additionally, any errors while reading will
20868 * be skipped.
20869 *
20870 * Given a table with this definition:
20871 *
20872 * CREATE TABLE Stuff (
20873 * name TEXT PRIMARY KEY,
20874 * value TEXT NOT NULL
20875 * );
20876 *
20877 * to recover the data from teh table, you could do something like:
20878 *
20879 * -- Attach another database, the original is not trustworthy.
20880 * ATTACH DATABASE '/tmp/db.db' AS rdb;
20881 * -- Create a new version of the table.
20882 * CREATE TABLE rdb.Stuff (
20883 * name TEXT PRIMARY KEY,
20884 * value TEXT NOT NULL
20885 * );
20886 * -- This will read the original table's data.
20887 * CREATE VIRTUAL TABLE temp.recover_Stuff using recover(
20888 * main.Stuff,
20889 * name TEXT STRICT NOT NULL, -- only real TEXT data allowed
20890 * value TEXT STRICT NOT NULL
20891 * );
20892 * -- Corruption means the UNIQUE constraint may no longer hold for
20893 * -- Stuff, so either OR REPLACE or OR IGNORE must be used.
20894 * INSERT OR REPLACE INTO rdb.Stuff (rowid, name, value )
20895 * SELECT rowid, name, value FROM temp.recover_Stuff;
20896 * DROP TABLE temp.recover_Stuff;
20897 * DETACH DATABASE rdb;
20898 * -- Move db.db to replace original db in filesystem.
20899 *
20900 *
20901 * Usage
20902 *
20903 * Given the goal of dealing with corruption, it would not be safe to
20904 * create a recovery table in the database being recovered. So
20905 * recovery tables must be created in the temp database. They are not
20906 * appropriate to persist, in any case. [As a bonus, sqlite_master
20907 * tables can be recovered. Perhaps more cute than useful, though.]
20908 *
20909 * The parameters are a specifier for the table to read, and a column
20910 * definition for each bit of data stored in that table. The named
20911 * table must be convertable to a root page number by reading the
20912 * sqlite_master table. Bare table names are assumed to be in
20913 * database 0 ("main"), other databases can be specified in db.table
20914 * fashion.
20915 *
20916 * Column definitions are similar to BUT NOT THE SAME AS those
20917 * provided to CREATE statements:
20918 * column-def: column-name [type-name [STRICT] [NOT NULL]]
20919 * type-name: (ANY|ROWID|INTEGER|FLOAT|NUMERIC|TEXT|BLOB)
20920 *
20921 * Only those exact type names are accepted, there is no type
20922 * intuition. The only constraints accepted are STRICT (see below)
20923 * and NOT NULL. Anything unexpected will cause the create to fail.
20924 *
20925 * ANY is a convenience to indicate that manifest typing is desired.
20926 * It is equivalent to not specifying a type at all. The results for
20927 * such columns will have the type of the data's storage. The exposed
20928 * schema will contain no type for that column.
20929 *
20930 * ROWID is used for columns representing aliases to the rowid
20931 * (INTEGER PRIMARY KEY, with or without AUTOINCREMENT), to make the
20932 * concept explicit. Such columns are actually stored as NULL, so
20933 * they cannot be simply ignored. The exposed schema will be INTEGER
20934 * for that column.
20935 *
20936 * NOT NULL causes rows with a NULL in that column to be skipped. It
20937 * also adds NOT NULL to the column in the exposed schema. If the
20938 * table has ever had columns added using ALTER TABLE, then those
20939 * columns implicitly contain NULL for rows which have not been
20940 * updated. [Workaround using COALESCE() in your SELECT statement.]
20941 *
20942 * The created table is read-only, with no indices. Any SELECT will
20943 * be a full-table scan, returning each valid row read from the
20944 * storage of the backing table. The rowid will be the rowid of the
20945 * row from the backing table. "Valid" means:
20946 * - The cell metadata for the row is well-formed. Mainly this means that
20947 * the cell header info describes a payload of the size indicated by
20948 * the cell's payload size.
20949 * - The cell does not run off the page.
20950 * - The cell does not overlap any other cell on the page.
20951 * - The cell contains doesn't contain too many columns.
20952 * - The types of the serialized data match the indicated types (see below).
20953 *
20954 *
20955 * Type affinity versus type storage.
20956 *
20957 * http://www.sqlite.org/datatype3.html describes SQLite's type
20958 * affinity system. The system provides for automated coercion of
20959 * types in certain cases, transparently enough that many developers
20960 * do not realize that it is happening. Importantly, it implies that
20961 * the raw data stored in the database may not have the obvious type.
20962 *
20963 * Differences between the stored data types and the expected data
20964 * types may be a signal of corruption. This module makes some
20965 * allowances for automatic coercion. It is important to be concious
20966 * of the difference between the schema exposed by the module, and the
20967 * data types read from storage. The following table describes how
20968 * the module interprets things:
20969 *
20970 * type schema data STRICT
20971 * ---- ------ ---- ------
20972 * ANY <none> any any
20973 * ROWID INTEGER n/a n/a
20974 * INTEGER INTEGER integer integer
20975 * FLOAT FLOAT integer or float float
20976 * NUMERIC NUMERIC integer, float, or text integer or float
20977 * TEXT TEXT text or blob text
20978 * BLOB BLOB blob blob
20979 *
20980 * type is the type provided to the recover module, schema is the
20981 * schema exposed by the module, data is the acceptable types of data
20982 * decoded from storage, and STRICT is a modification of that.
20983 *
20984 * A very loose recovery system might use ANY for all columns, then
20985 * use the appropriate sqlite3_column_*() calls to coerce to expected
20986 * types. This doesn't provide much protection if a page from a
20987 * different table with the same column count is linked into an
20988 * inappropriate btree.
20989 *
20990 * A very tight recovery system might use STRICT to enforce typing on
20991 * all columns, preferring to skip rows which are valid at the storage
20992 * level but don't contain the right types. Note that FLOAT STRICT is
20993 * almost certainly not appropriate, since integral values are
20994 * transparently stored as integers, when that is more efficient.
20995 *
20996 * Another option is to use ANY for all columns and inspect each
20997 * result manually (using sqlite3_column_*). This should only be
20998 * necessary in cases where developers have used manifest typing (test
20999 * to make sure before you decide that you aren't using manifest
21000 * typing!).
21001 *
21002 *
21003 * Caveats
21004 *
21005 * Leaf pages not referenced by interior nodes will not be found.
21006 *
21007 * Leaf pages referenced from interior nodes of other tables will not
21008 * be resolved.
21009 *
21010 * Rows referencing invalid overflow pages will be skipped.
21011 *
21012 * SQlite rows have a header which describes how to interpret the rest
21013 * of the payload. The header can be valid in cases where the rest of
21014 * the record is actually corrupt (in the sense that the data is not
21015 * the intended data). This can especially happen WRT overflow pages,
21016 * as lack of atomic updates between pages is the primary form of
21017 * corruption I have seen in the wild.
21018 */
21019 /* The implementation is via a series of cursors. The cursor
21020 * implementations follow the pattern:
21021 *
21022 * // Creates the cursor using various initialization info.
21023 * int cursorCreate(...);
21024 *
21025 * // Returns 1 if there is no more data, 0 otherwise.
21026 * int cursorEOF(Cursor *pCursor);
21027 *
21028 * // Various accessors can be used if not at EOF.
21029 *
21030 * // Move to the next item.
21031 * int cursorNext(Cursor *pCursor);
21032 *
21033 * // Destroy the memory associated with the cursor.
21034 * void cursorDestroy(Cursor *pCursor);
21035 *
21036 * References in the following are to sections at
21037 * http://www.sqlite.org/fileformat2.html .
21038 *
21039 * RecoverLeafCursor iterates the records in a leaf table node
21040 * described in section 1.5 "B-tree Pages". When the node is
21041 * exhausted, an interior cursor is used to get the next leaf node,
21042 * and iteration continues there.
21043 *
21044 * RecoverInteriorCursor iterates the child pages in an interior table
21045 * node described in section 1.5 "B-tree Pages". When the node is
21046 * exhausted, a parent interior cursor is used to get the next
21047 * interior node at the same level, and iteration continues there.
21048 *
21049 * Together these record the path from the leaf level to the root of
21050 * the tree. Iteration happens from the leaves rather than the root
21051 * both for efficiency and putting the special case at the front of
21052 * the list is easier to implement.
21053 *
21054 * RecoverCursor uses a RecoverLeafCursor to iterate the rows of a
21055 * table, returning results via the SQLite virtual table interface.
21056 */
21057 /* TODO(shess): It might be useful to allow DEFAULT in types to
21058 * specify what to do for NULL when an ALTER TABLE case comes up.
21059 * Unfortunately, simply adding it to the exposed schema and using
21060 * sqlite3_result_null() does not cause the default to be generate.
21061 * Handling it ourselves seems hard, unfortunately.
21062 */
21063
21064 /* #include <assert.h> */
21065 /* #include <ctype.h> */
21066 /* #include <stdio.h> */
21067 /* #include <string.h> */
21068
21069 /* Internal SQLite things that are used:
21070 * u32, u64, i64 types.
21071 * Btree, Pager, and DbPage structs.
21072 * DbPage.pData, .pPager, and .pgno
21073 * sqlite3 struct.
21074 * sqlite3BtreePager() and sqlite3BtreeGetPageSize()
21075 * sqlite3BtreeGetOptimalReserve()
21076 * sqlite3PagerGet() and sqlite3PagerUnref()
21077 * getVarint().
21078 */
21079 /* #include "sqliteInt.h" */
21080
21081 /* For debugging. */
21082 #if 0
21083 #define FNENTRY() fprintf(stderr, "In %s\n", __FUNCTION__)
21084 #else
21085 #define FNENTRY()
21086 #endif
21087
21088 /* Generic constants and helper functions. */
21089
21090 static const unsigned char kTableLeafPage = 0x0D;
21091 static const unsigned char kTableInteriorPage = 0x05;
21092
21093 /* From section 1.5. */
21094 static const unsigned kiPageTypeOffset = 0;
21095 static const unsigned kiPageFreeBlockOffset = 1;
21096 static const unsigned kiPageCellCountOffset = 3;
21097 static const unsigned kiPageCellContentOffset = 5;
21098 static const unsigned kiPageFragmentedBytesOffset = 7;
21099 static const unsigned knPageLeafHeaderBytes = 8;
21100 /* Interior pages contain an additional field. */
21101 static const unsigned kiPageRightChildOffset = 8;
21102 static const unsigned kiPageInteriorHeaderBytes = 12;
21103
21104 /* Accepted types are specified by a mask. */
21105 #define MASK_ROWID (1<<0)
21106 #define MASK_INTEGER (1<<1)
21107 #define MASK_FLOAT (1<<2)
21108 #define MASK_TEXT (1<<3)
21109 #define MASK_BLOB (1<<4)
21110 #define MASK_NULL (1<<5)
21111
21112 /* Helpers to decode fixed-size fields. */
21113 static u32 decodeUnsigned16(const unsigned char *pData){
21114 return (pData[0]<<8) + pData[1];
21115 }
21116 static u32 decodeUnsigned32(const unsigned char *pData){
21117 return (decodeUnsigned16(pData)<<16) + decodeUnsigned16(pData+2);
21118 }
21119 static i64 decodeSigned(const unsigned char *pData, unsigned nBytes){
21120 i64 r = (char)(*pData);
21121 while( --nBytes ){
21122 r <<= 8;
21123 r += *(++pData);
21124 }
21125 return r;
21126 }
21127 /* Derived from vdbeaux.c, sqlite3VdbeSerialGet(), case 7. */
21128 /* TODO(shess): Determine if swapMixedEndianFloat() applies. */
21129 static double decodeFloat64(const unsigned char *pData){
21130 #if !defined(NDEBUG)
21131 static const u64 t1 = ((u64)0x3ff00000)<<32;
21132 static const double r1 = 1.0;
21133 u64 t2 = t1;
21134 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
21135 #endif
21136 i64 x = decodeSigned(pData, 8);
21137 double d;
21138 memcpy(&d, &x, sizeof(x));
21139 return d;
21140 }
21141
21142 /* Return true if a varint can safely be read from pData/nData. */
21143 /* TODO(shess): DbPage points into the middle of a buffer which
21144 * contains the page data before DbPage. So code should always be
21145 * able to read a small number of varints safely. Consider whether to
21146 * trust that or not.
21147 */
21148 static int checkVarint(const unsigned char *pData, unsigned nData){
21149 unsigned i;
21150
21151 /* In the worst case the decoder takes all 8 bits of the 9th byte. */
21152 if( nData>=9 ){
21153 return 1;
21154 }
21155
21156 /* Look for a high-bit-clear byte in what's left. */
21157 for( i=0; i<nData; ++i ){
21158 if( !(pData[i]&0x80) ){
21159 return 1;
21160 }
21161 }
21162
21163 /* Cannot decode in the space given. */
21164 return 0;
21165 }
21166
21167 /* Return 1 if n varints can be read from pData/nData. */
21168 static int checkVarints(const unsigned char *pData, unsigned nData,
21169 unsigned n){
21170 unsigned nCur = 0; /* Byte offset within current varint. */
21171 unsigned nFound = 0; /* Number of varints found. */
21172 unsigned i;
21173
21174 /* In the worst case the decoder takes all 8 bits of the 9th byte. */
21175 if( nData>=9*n ){
21176 return 1;
21177 }
21178
21179 for( i=0; nFound<n && i<nData; ++i ){
21180 nCur++;
21181 if( nCur==9 || !(pData[i]&0x80) ){
21182 nFound++;
21183 nCur = 0;
21184 }
21185 }
21186
21187 return nFound==n;
21188 }
21189
21190 /* ctype and str[n]casecmp() can be affected by locale (eg, tr_TR).
21191 * These versions consider only the ASCII space.
21192 */
21193 /* TODO(shess): It may be reasonable to just remove the need for these
21194 * entirely. The module could require "TEXT STRICT NOT NULL", not
21195 * "Text Strict Not Null" or whatever the developer felt like typing
21196 * that day. Handling corrupt data is a PERFECT place to be pedantic.
21197 */
21198 static int ascii_isspace(char c){
21199 /* From fts3_expr.c */
21200 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
21201 }
21202 static int ascii_isalnum(int x){
21203 /* From fts3_tokenizer1.c */
21204 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
21205 }
21206 static int ascii_tolower(int x){
21207 /* From fts3_tokenizer1.c */
21208 return (x>='A' && x<='Z') ? x-'A'+'a' : x;
21209 }
21210 /* TODO(shess): Consider sqlite3_strnicmp() */
21211 static int ascii_strncasecmp(const char *s1, const char *s2, size_t n){
21212 const unsigned char *us1 = (const unsigned char *)s1;
21213 const unsigned char *us2 = (const unsigned char *)s2;
21214 while( *us1 && *us2 && n && ascii_tolower(*us1)==ascii_tolower(*us2) ){
21215 us1++, us2++, n--;
21216 }
21217 return n ? ascii_tolower(*us1)-ascii_tolower(*us2) : 0;
21218 }
21219 static int ascii_strcasecmp(const char *s1, const char *s2){
21220 /* If s2 is equal through strlen(s1), will exit while() due to s1's
21221 * trailing NUL, and return NUL-s2[strlen(s1)].
21222 */
21223 return ascii_strncasecmp(s1, s2, strlen(s1)+1);
21224 }
21225
21226 /* For some reason I kept making mistakes with offset calculations. */
21227 static const unsigned char *PageData(DbPage *pPage, unsigned iOffset){
21228 assert( iOffset<=pPage->nPageSize );
21229 return (unsigned char *)pPage->pData + iOffset;
21230 }
21231
21232 /* The first page in the file contains a file header in the first 100
21233 * bytes. The page's header information comes after that. Note that
21234 * the offsets in the page's header information are relative to the
21235 * beginning of the page, NOT the end of the page header.
21236 */
21237 static const unsigned char *PageHeader(DbPage *pPage){
21238 if( pPage->pgno==1 ){
21239 const unsigned nDatabaseHeader = 100;
21240 return PageData(pPage, nDatabaseHeader);
21241 }else{
21242 return PageData(pPage, 0);
21243 }
21244 }
21245
21246 /* Helper to fetch the pager and page size for the named database. */
21247 static int GetPager(sqlite3 *db, const char *zName,
21248 Pager **pPager, unsigned *pnPageSize){
21249 Btree *pBt = NULL;
21250 int i;
21251 for( i=0; i<db->nDb; ++i ){
21252 if( ascii_strcasecmp(db->aDb[i].zName, zName)==0 ){
21253 pBt = db->aDb[i].pBt;
21254 break;
21255 }
21256 }
21257 if( !pBt ){
21258 return SQLITE_ERROR;
21259 }
21260
21261 *pPager = sqlite3BtreePager(pBt);
21262 *pnPageSize =
21263 sqlite3BtreeGetPageSize(pBt) - sqlite3BtreeGetOptimalReserve(pBt);
21264 return SQLITE_OK;
21265 }
21266
21267 /* iSerialType is a type read from a record header. See "2.1 Record Format".
21268 */
21269
21270 /* Storage size of iSerialType in bytes. My interpretation of SQLite
21271 * documentation is that text and blob fields can have 32-bit length.
21272 * Values past 2^31-12 will need more than 32 bits to encode, which is
21273 * why iSerialType is u64.
21274 */
21275 static u32 SerialTypeLength(u64 iSerialType){
21276 switch( iSerialType ){
21277 case 0 : return 0; /* NULL */
21278 case 1 : return 1; /* Various integers. */
21279 case 2 : return 2;
21280 case 3 : return 3;
21281 case 4 : return 4;
21282 case 5 : return 6;
21283 case 6 : return 8;
21284 case 7 : return 8; /* 64-bit float. */
21285 case 8 : return 0; /* Constant 0. */
21286 case 9 : return 0; /* Constant 1. */
21287 case 10 : case 11 : assert( !"RESERVED TYPE"); return 0;
21288 }
21289 return (u32)((iSerialType>>1) - 6);
21290 }
21291
21292 /* True if iSerialType refers to a blob. */
21293 static int SerialTypeIsBlob(u64 iSerialType){
21294 assert( iSerialType>=12 );
21295 return (iSerialType%2)==0;
21296 }
21297
21298 /* Returns true if the serialized type represented by iSerialType is
21299 * compatible with the given type mask.
21300 */
21301 static int SerialTypeIsCompatible(u64 iSerialType, unsigned char mask){
21302 switch( iSerialType ){
21303 case 0 : return (mask&MASK_NULL)!=0;
21304 case 1 : return (mask&MASK_INTEGER)!=0;
21305 case 2 : return (mask&MASK_INTEGER)!=0;
21306 case 3 : return (mask&MASK_INTEGER)!=0;
21307 case 4 : return (mask&MASK_INTEGER)!=0;
21308 case 5 : return (mask&MASK_INTEGER)!=0;
21309 case 6 : return (mask&MASK_INTEGER)!=0;
21310 case 7 : return (mask&MASK_FLOAT)!=0;
21311 case 8 : return (mask&MASK_INTEGER)!=0;
21312 case 9 : return (mask&MASK_INTEGER)!=0;
21313 case 10 : assert( !"RESERVED TYPE"); return 0;
21314 case 11 : assert( !"RESERVED TYPE"); return 0;
21315 }
21316 return (mask&(SerialTypeIsBlob(iSerialType) ? MASK_BLOB : MASK_TEXT));
21317 }
21318
21319 /* Versions of strdup() with return values appropriate for
21320 * sqlite3_free(). malloc.c has sqlite3DbStrDup()/NDup(), but those
21321 * need sqlite3DbFree(), which seems intrusive.
21322 */
21323 static char *sqlite3_strndup(const char *z, unsigned n){
21324 char *zNew;
21325
21326 if( z==NULL ){
21327 return NULL;
21328 }
21329
21330 zNew = sqlite3_malloc(n+1);
21331 if( zNew!=NULL ){
21332 memcpy(zNew, z, n);
21333 zNew[n] = '\0';
21334 }
21335 return zNew;
21336 }
21337 static char *sqlite3_strdup(const char *z){
21338 if( z==NULL ){
21339 return NULL;
21340 }
21341 return sqlite3_strndup(z, strlen(z));
21342 }
21343
21344 /* Fetch the page number of zTable in zDb from sqlite_master in zDb,
21345 * and put it in *piRootPage.
21346 */
21347 static int getRootPage(sqlite3 *db, const char *zDb, const char *zTable,
21348 u32 *piRootPage){
21349 char *zSql; /* SQL selecting root page of named element. */
21350 sqlite3_stmt *pStmt;
21351 int rc;
21352
21353 if( strcmp(zTable, "sqlite_master")==0 ){
21354 *piRootPage = 1;
21355 return SQLITE_OK;
21356 }
21357
21358 zSql = sqlite3_mprintf("SELECT rootpage FROM %s.sqlite_master "
21359 "WHERE type = 'table' AND tbl_name = %Q",
21360 zDb, zTable);
21361 if( !zSql ){
21362 return SQLITE_NOMEM;
21363 }
21364
21365 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
21366 sqlite3_free(zSql);
21367 if( rc!=SQLITE_OK ){
21368 return rc;
21369 }
21370
21371 /* Require a result. */
21372 rc = sqlite3_step(pStmt);
21373 if( rc==SQLITE_DONE ){
21374 rc = SQLITE_CORRUPT;
21375 }else if( rc==SQLITE_ROW ){
21376 *piRootPage = sqlite3_column_int(pStmt, 0);
21377
21378 /* Require only one result. */
21379 rc = sqlite3_step(pStmt);
21380 if( rc==SQLITE_DONE ){
21381 rc = SQLITE_OK;
21382 }else if( rc==SQLITE_ROW ){
21383 rc = SQLITE_CORRUPT;
21384 }
21385 }
21386 sqlite3_finalize(pStmt);
21387 return rc;
21388 }
21389
21390 static int getEncoding(sqlite3 *db, const char *zDb, int* piEncoding){
21391 sqlite3_stmt *pStmt;
21392 int rc;
21393 char *zSql = sqlite3_mprintf("PRAGMA %s.encoding", zDb);
21394 if( !zSql ){
21395 return SQLITE_NOMEM;
21396 }
21397
21398 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
21399 sqlite3_free(zSql);
21400 if( rc!=SQLITE_OK ){
21401 return rc;
21402 }
21403
21404 /* Require a result. */
21405 rc = sqlite3_step(pStmt);
21406 if( rc==SQLITE_DONE ){
21407 /* This case should not be possible. */
21408 rc = SQLITE_CORRUPT;
21409 }else if( rc==SQLITE_ROW ){
21410 if( sqlite3_column_type(pStmt, 0)==SQLITE_TEXT ){
21411 const char* z = (const char *)sqlite3_column_text(pStmt, 0);
21412 /* These strings match the literals in pragma.c. */
21413 if( !strcmp(z, "UTF-16le") ){
21414 *piEncoding = SQLITE_UTF16LE;
21415 }else if( !strcmp(z, "UTF-16be") ){
21416 *piEncoding = SQLITE_UTF16BE;
21417 }else if( !strcmp(z, "UTF-8") ){
21418 *piEncoding = SQLITE_UTF8;
21419 }else{
21420 /* This case should not be possible. */
21421 *piEncoding = SQLITE_UTF8;
21422 }
21423 }else{
21424 /* This case should not be possible. */
21425 *piEncoding = SQLITE_UTF8;
21426 }
21427
21428 /* Require only one result. */
21429 rc = sqlite3_step(pStmt);
21430 if( rc==SQLITE_DONE ){
21431 rc = SQLITE_OK;
21432 }else if( rc==SQLITE_ROW ){
21433 /* This case should not be possible. */
21434 rc = SQLITE_CORRUPT;
21435 }
21436 }
21437 sqlite3_finalize(pStmt);
21438 return rc;
21439 }
21440
21441 /* Cursor for iterating interior nodes. Interior page cells contain a
21442 * child page number and a rowid. The child page contains items left
21443 * of the rowid (less than). The rightmost page of the subtree is
21444 * stored in the page header.
21445 *
21446 * interiorCursorDestroy - release all resources associated with the
21447 * cursor and any parent cursors.
21448 * interiorCursorCreate - create a cursor with the given parent and page.
21449 * interiorCursorEOF - returns true if neither the cursor nor the
21450 * parent cursors can return any more data.
21451 * interiorCursorNextPage - fetch the next child page from the cursor.
21452 *
21453 * Logically, interiorCursorNextPage() returns the next child page
21454 * number from the page the cursor is currently reading, calling the
21455 * parent cursor as necessary to get new pages to read, until done.
21456 * SQLITE_ROW if a page is returned, SQLITE_DONE if out of pages,
21457 * error otherwise. Unfortunately, if the table is corrupted
21458 * unexpected pages can be returned. If any unexpected page is found,
21459 * leaf or otherwise, it is returned to the caller for processing,
21460 * with the interior cursor left empty. The next call to
21461 * interiorCursorNextPage() will recurse to the parent cursor until an
21462 * interior page to iterate is returned.
21463 *
21464 * Note that while interiorCursorNextPage() will refuse to follow
21465 * loops, it does not keep track of pages returned for purposes of
21466 * preventing duplication.
21467 *
21468 * Note that interiorCursorEOF() could return false (not at EOF), and
21469 * interiorCursorNextPage() could still return SQLITE_DONE. This
21470 * could happen if there are more cells to iterate in an interior
21471 * page, but those cells refer to invalid pages.
21472 */
21473 typedef struct RecoverInteriorCursor RecoverInteriorCursor;
21474 struct RecoverInteriorCursor {
21475 RecoverInteriorCursor *pParent; /* Parent node to this node. */
21476 DbPage *pPage; /* Reference to leaf page. */
21477 unsigned nPageSize; /* Size of page. */
21478 unsigned nChildren; /* Number of children on the page. */
21479 unsigned iChild; /* Index of next child to return. */
21480 };
21481
21482 static void interiorCursorDestroy(RecoverInteriorCursor *pCursor){
21483 /* Destroy all the cursors to the root. */
21484 while( pCursor ){
21485 RecoverInteriorCursor *p = pCursor;
21486 pCursor = pCursor->pParent;
21487
21488 if( p->pPage ){
21489 sqlite3PagerUnref(p->pPage);
21490 p->pPage = NULL;
21491 }
21492
21493 memset(p, 0xA5, sizeof(*p));
21494 sqlite3_free(p);
21495 }
21496 }
21497
21498 /* Internal helper. Reset storage in preparation for iterating pPage. */
21499 static void interiorCursorSetPage(RecoverInteriorCursor *pCursor,
21500 DbPage *pPage){
21501 const unsigned knMinCellLength = 2 + 4 + 1;
21502 unsigned nMaxChildren;
21503 assert( PageHeader(pPage)[kiPageTypeOffset]==kTableInteriorPage );
21504
21505 if( pCursor->pPage ){
21506 sqlite3PagerUnref(pCursor->pPage);
21507 pCursor->pPage = NULL;
21508 }
21509 pCursor->pPage = pPage;
21510 pCursor->iChild = 0;
21511
21512 /* A child for each cell, plus one in the header. */
21513 pCursor->nChildren = decodeUnsigned16(PageHeader(pPage) +
21514 kiPageCellCountOffset) + 1;
21515
21516 /* Each child requires a 16-bit offset from an array after the header,
21517 * and each child contains a 32-bit page number and at least a varint
21518 * (min size of one byte). The final child page is in the header. So
21519 * the maximum value for nChildren is:
21520 * (nPageSize - kiPageInteriorHeaderBytes) /
21521 * (sizeof(uint16) + sizeof(uint32) + 1) + 1
21522 */
21523 /* TODO(shess): This count is very unlikely to be corrupted in
21524 * isolation, so seeing this could signal to skip the page. OTOH, I
21525 * can't offhand think of how to get here unless this or the page-type
21526 * byte is corrupted. Could be an overflow page, but it would require
21527 * a very large database.
21528 */
21529 nMaxChildren =
21530 (pCursor->nPageSize - kiPageInteriorHeaderBytes) / knMinCellLength + 1;
21531 if (pCursor->nChildren > nMaxChildren) {
21532 pCursor->nChildren = nMaxChildren;
21533 }
21534 }
21535
21536 static int interiorCursorCreate(RecoverInteriorCursor *pParent,
21537 DbPage *pPage, int nPageSize,
21538 RecoverInteriorCursor **ppCursor){
21539 RecoverInteriorCursor *pCursor =
21540 sqlite3_malloc(sizeof(RecoverInteriorCursor));
21541 if( !pCursor ){
21542 return SQLITE_NOMEM;
21543 }
21544
21545 memset(pCursor, 0, sizeof(*pCursor));
21546 pCursor->pParent = pParent;
21547 pCursor->nPageSize = nPageSize;
21548 interiorCursorSetPage(pCursor, pPage);
21549 *ppCursor = pCursor;
21550 return SQLITE_OK;
21551 }
21552
21553 /* Internal helper. Return the child page number at iChild. */
21554 static unsigned interiorCursorChildPage(RecoverInteriorCursor *pCursor){
21555 const unsigned char *pPageHeader; /* Header of the current page. */
21556 const unsigned char *pCellOffsets; /* Offset to page's cell offsets. */
21557 unsigned iCellOffset; /* Offset of target cell. */
21558
21559 assert( pCursor->iChild<pCursor->nChildren );
21560
21561 /* Rightmost child is in the header. */
21562 pPageHeader = PageHeader(pCursor->pPage);
21563 if( pCursor->iChild==pCursor->nChildren-1 ){
21564 return decodeUnsigned32(pPageHeader + kiPageRightChildOffset);
21565 }
21566
21567 /* Each cell is a 4-byte integer page number and a varint rowid
21568 * which is greater than the rowid of items in that sub-tree (this
21569 * module ignores ordering). The offset is from the beginning of the
21570 * page, not from the page header.
21571 */
21572 pCellOffsets = pPageHeader + kiPageInteriorHeaderBytes;
21573 iCellOffset = decodeUnsigned16(pCellOffsets + pCursor->iChild*2);
21574 if( iCellOffset<=pCursor->nPageSize-4 ){
21575 return decodeUnsigned32(PageData(pCursor->pPage, iCellOffset));
21576 }
21577
21578 /* TODO(shess): Check for cell overlaps? Cells require 4 bytes plus
21579 * a varint. Check could be identical to leaf check (or even a
21580 * shared helper testing for "Cells starting in this range"?).
21581 */
21582
21583 /* If the offset is broken, return an invalid page number. */
21584 return 0;
21585 }
21586
21587 static int interiorCursorEOF(RecoverInteriorCursor *pCursor){
21588 /* Find a parent with remaining children. EOF if none found. */
21589 while( pCursor && pCursor->iChild>=pCursor->nChildren ){
21590 pCursor = pCursor->pParent;
21591 }
21592 return pCursor==NULL;
21593 }
21594
21595 /* Internal helper. Used to detect if iPage would cause a loop. */
21596 static int interiorCursorPageInUse(RecoverInteriorCursor *pCursor,
21597 unsigned iPage){
21598 /* Find any parent using the indicated page. */
21599 while( pCursor && pCursor->pPage->pgno!=iPage ){
21600 pCursor = pCursor->pParent;
21601 }
21602 return pCursor!=NULL;
21603 }
21604
21605 /* Get the next page from the interior cursor at *ppCursor. Returns
21606 * SQLITE_ROW with the page in *ppPage, or SQLITE_DONE if out of
21607 * pages, or the error SQLite returned.
21608 *
21609 * If the tree is uneven, then when the cursor attempts to get a new
21610 * interior page from the parent cursor, it may get a non-interior
21611 * page. In that case, the new page is returned, and *ppCursor is
21612 * updated to point to the parent cursor (this cursor is freed).
21613 */
21614 /* TODO(shess): I've tried to avoid recursion in most of this code,
21615 * but this case is more challenging because the recursive call is in
21616 * the middle of operation. One option for converting it without
21617 * adding memory management would be to retain the head pointer and
21618 * use a helper to "back up" as needed. Another option would be to
21619 * reverse the list during traversal.
21620 */
21621 static int interiorCursorNextPage(RecoverInteriorCursor **ppCursor,
21622 DbPage **ppPage){
21623 RecoverInteriorCursor *pCursor = *ppCursor;
21624 while( 1 ){
21625 int rc;
21626 const unsigned char *pPageHeader; /* Header of found page. */
21627
21628 /* Find a valid child page which isn't on the stack. */
21629 while( pCursor->iChild<pCursor->nChildren ){
21630 const unsigned iPage = interiorCursorChildPage(pCursor);
21631 pCursor->iChild++;
21632 if( interiorCursorPageInUse(pCursor, iPage) ){
21633 fprintf(stderr, "Loop detected at %d\n", iPage);
21634 }else{
21635 int rc = sqlite3PagerGet(pCursor->pPage->pPager, iPage, ppPage, 0);
21636 if( rc==SQLITE_OK ){
21637 return SQLITE_ROW;
21638 }
21639 }
21640 }
21641
21642 /* This page has no more children. Get next page from parent. */
21643 if( !pCursor->pParent ){
21644 return SQLITE_DONE;
21645 }
21646 rc = interiorCursorNextPage(&pCursor->pParent, ppPage);
21647 if( rc!=SQLITE_ROW ){
21648 return rc;
21649 }
21650
21651 /* If a non-interior page is received, that either means that the
21652 * tree is uneven, or that a child was re-used (say as an overflow
21653 * page). Remove this cursor and let the caller handle the page.
21654 */
21655 pPageHeader = PageHeader(*ppPage);
21656 if( pPageHeader[kiPageTypeOffset]!=kTableInteriorPage ){
21657 *ppCursor = pCursor->pParent;
21658 pCursor->pParent = NULL;
21659 interiorCursorDestroy(pCursor);
21660 return SQLITE_ROW;
21661 }
21662
21663 /* Iterate the new page. */
21664 interiorCursorSetPage(pCursor, *ppPage);
21665 *ppPage = NULL;
21666 }
21667
21668 assert(NULL); /* NOTREACHED() */
21669 return SQLITE_CORRUPT;
21670 }
21671
21672 /* Large rows are spilled to overflow pages. The row's main page
21673 * stores the overflow page number after the local payload, with a
21674 * linked list forward from there as necessary. overflowMaybeCreate()
21675 * and overflowGetSegment() provide an abstraction for accessing such
21676 * data while centralizing the code.
21677 *
21678 * overflowDestroy - releases all resources associated with the structure.
21679 * overflowMaybeCreate - create the overflow structure if it is needed
21680 * to represent the given record. See function comment.
21681 * overflowGetSegment - fetch a segment from the record, accounting
21682 * for overflow pages. Segments which are not
21683 * entirely contained with a page are constructed
21684 * into a buffer which is returned. See function comment.
21685 */
21686 typedef struct RecoverOverflow RecoverOverflow;
21687 struct RecoverOverflow {
21688 RecoverOverflow *pNextOverflow;
21689 DbPage *pPage;
21690 unsigned nPageSize;
21691 };
21692
21693 static void overflowDestroy(RecoverOverflow *pOverflow){
21694 while( pOverflow ){
21695 RecoverOverflow *p = pOverflow;
21696 pOverflow = p->pNextOverflow;
21697
21698 if( p->pPage ){
21699 sqlite3PagerUnref(p->pPage);
21700 p->pPage = NULL;
21701 }
21702
21703 memset(p, 0xA5, sizeof(*p));
21704 sqlite3_free(p);
21705 }
21706 }
21707
21708 /* Internal helper. Used to detect if iPage would cause a loop. */
21709 static int overflowPageInUse(RecoverOverflow *pOverflow, unsigned iPage){
21710 while( pOverflow && pOverflow->pPage->pgno!=iPage ){
21711 pOverflow = pOverflow->pNextOverflow;
21712 }
21713 return pOverflow!=NULL;
21714 }
21715
21716 /* Setup to access an nRecordBytes record beginning at iRecordOffset
21717 * in pPage. If nRecordBytes can be satisfied entirely from pPage,
21718 * then no overflow pages are needed an *pnLocalRecordBytes is set to
21719 * nRecordBytes. Otherwise, *ppOverflow is set to the head of a list
21720 * of overflow pages, and *pnLocalRecordBytes is set to the number of
21721 * bytes local to pPage.
21722 *
21723 * overflowGetSegment() will do the right thing regardless of whether
21724 * those values are set to be in-page or not.
21725 */
21726 static int overflowMaybeCreate(DbPage *pPage, unsigned nPageSize,
21727 unsigned iRecordOffset, unsigned nRecordBytes,
21728 unsigned *pnLocalRecordBytes,
21729 RecoverOverflow **ppOverflow){
21730 unsigned nLocalRecordBytes; /* Record bytes in the leaf page. */
21731 unsigned iNextPage; /* Next page number for record data. */
21732 unsigned nBytes; /* Maximum record bytes as of current page. */
21733 int rc;
21734 RecoverOverflow *pFirstOverflow; /* First in linked list of pages. */
21735 RecoverOverflow *pLastOverflow; /* End of linked list. */
21736
21737 /* Calculations from the "Table B-Tree Leaf Cell" part of section
21738 * 1.5 of http://www.sqlite.org/fileformat2.html . maxLocal and
21739 * minLocal to match naming in btree.c.
21740 */
21741 const unsigned maxLocal = nPageSize - 35;
21742 const unsigned minLocal = ((nPageSize-12)*32/255)-23; /* m */
21743
21744 /* Always fit anything smaller than maxLocal. */
21745 if( nRecordBytes<=maxLocal ){
21746 *pnLocalRecordBytes = nRecordBytes;
21747 *ppOverflow = NULL;
21748 return SQLITE_OK;
21749 }
21750
21751 /* Calculate the remainder after accounting for minLocal on the leaf
21752 * page and what packs evenly into overflow pages. If the remainder
21753 * does not fit into maxLocal, then a partially-full overflow page
21754 * will be required in any case, so store as little as possible locally.
21755 */
21756 nLocalRecordBytes = minLocal+((nRecordBytes-minLocal)%(nPageSize-4));
21757 if( maxLocal<nLocalRecordBytes ){
21758 nLocalRecordBytes = minLocal;
21759 }
21760
21761 /* Don't read off the end of the page. */
21762 if( iRecordOffset+nLocalRecordBytes+4>nPageSize ){
21763 return SQLITE_CORRUPT;
21764 }
21765
21766 /* First overflow page number is after the local bytes. */
21767 iNextPage =
21768 decodeUnsigned32(PageData(pPage, iRecordOffset + nLocalRecordBytes));
21769 nBytes = nLocalRecordBytes;
21770
21771 /* While there are more pages to read, and more bytes are needed,
21772 * get another page.
21773 */
21774 pFirstOverflow = pLastOverflow = NULL;
21775 rc = SQLITE_OK;
21776 while( iNextPage && nBytes<nRecordBytes ){
21777 RecoverOverflow *pOverflow; /* New overflow page for the list. */
21778
21779 rc = sqlite3PagerGet(pPage->pPager, iNextPage, &pPage, 0);
21780 if( rc!=SQLITE_OK ){
21781 break;
21782 }
21783
21784 pOverflow = sqlite3_malloc(sizeof(RecoverOverflow));
21785 if( !pOverflow ){
21786 sqlite3PagerUnref(pPage);
21787 rc = SQLITE_NOMEM;
21788 break;
21789 }
21790 memset(pOverflow, 0, sizeof(*pOverflow));
21791 pOverflow->pPage = pPage;
21792 pOverflow->nPageSize = nPageSize;
21793
21794 if( !pFirstOverflow ){
21795 pFirstOverflow = pOverflow;
21796 }else{
21797 pLastOverflow->pNextOverflow = pOverflow;
21798 }
21799 pLastOverflow = pOverflow;
21800
21801 iNextPage = decodeUnsigned32(pPage->pData);
21802 nBytes += nPageSize-4;
21803
21804 /* Avoid loops. */
21805 if( overflowPageInUse(pFirstOverflow, iNextPage) ){
21806 fprintf(stderr, "Overflow loop detected at %d\n", iNextPage);
21807 rc = SQLITE_CORRUPT;
21808 break;
21809 }
21810 }
21811
21812 /* If there were not enough pages, or too many, things are corrupt.
21813 * Not having enough pages is an obvious problem, all the data
21814 * cannot be read. Too many pages means that the contents of the
21815 * row between the main page and the overflow page(s) is
21816 * inconsistent (most likely one or more of the overflow pages does
21817 * not really belong to this row).
21818 */
21819 if( rc==SQLITE_OK && (nBytes<nRecordBytes || iNextPage) ){
21820 rc = SQLITE_CORRUPT;
21821 }
21822
21823 if( rc==SQLITE_OK ){
21824 *ppOverflow = pFirstOverflow;
21825 *pnLocalRecordBytes = nLocalRecordBytes;
21826 }else if( pFirstOverflow ){
21827 overflowDestroy(pFirstOverflow);
21828 }
21829 return rc;
21830 }
21831
21832 /* Use in concert with overflowMaybeCreate() to efficiently read parts
21833 * of a potentially-overflowing record. pPage and iRecordOffset are
21834 * the values passed into overflowMaybeCreate(), nLocalRecordBytes and
21835 * pOverflow are the values returned by that call.
21836 *
21837 * On SQLITE_OK, *ppBase points to nRequestBytes of data at
21838 * iRequestOffset within the record. If the data exists contiguously
21839 * in a page, a direct pointer is returned, otherwise a buffer from
21840 * sqlite3_malloc() is returned with the data. *pbFree is set true if
21841 * sqlite3_free() should be called on *ppBase.
21842 */
21843 /* Operation of this function is subtle. At any time, pPage is the
21844 * current page, with iRecordOffset and nLocalRecordBytes being record
21845 * data within pPage, and pOverflow being the overflow page after
21846 * pPage. This allows the code to handle both the initial leaf page
21847 * and overflow pages consistently by adjusting the values
21848 * appropriately.
21849 */
21850 static int overflowGetSegment(DbPage *pPage, unsigned iRecordOffset,
21851 unsigned nLocalRecordBytes,
21852 RecoverOverflow *pOverflow,
21853 unsigned iRequestOffset, unsigned nRequestBytes,
21854 unsigned char **ppBase, int *pbFree){
21855 unsigned nBase; /* Amount of data currently collected. */
21856 unsigned char *pBase; /* Buffer to collect record data into. */
21857
21858 /* Skip to the page containing the start of the data. */
21859 while( iRequestOffset>=nLocalRecordBytes && pOverflow ){
21860 /* Factor out current page's contribution. */
21861 iRequestOffset -= nLocalRecordBytes;
21862
21863 /* Move forward to the next page in the list. */
21864 pPage = pOverflow->pPage;
21865 iRecordOffset = 4;
21866 nLocalRecordBytes = pOverflow->nPageSize - iRecordOffset;
21867 pOverflow = pOverflow->pNextOverflow;
21868 }
21869
21870 /* If the requested data is entirely within this page, return a
21871 * pointer into the page.
21872 */
21873 if( iRequestOffset+nRequestBytes<=nLocalRecordBytes ){
21874 /* TODO(shess): "assignment discards qualifiers from pointer target type"
21875 * Having ppBase be const makes sense, but sqlite3_free() takes non-const.
21876 */
21877 *ppBase = (unsigned char *)PageData(pPage, iRecordOffset + iRequestOffset);
21878 *pbFree = 0;
21879 return SQLITE_OK;
21880 }
21881
21882 /* The data range would require additional pages. */
21883 if( !pOverflow ){
21884 /* Should never happen, the range is outside the nRecordBytes
21885 * passed to overflowMaybeCreate().
21886 */
21887 assert(NULL); /* NOTREACHED */
21888 return SQLITE_ERROR;
21889 }
21890
21891 /* Get a buffer to construct into. */
21892 nBase = 0;
21893 pBase = sqlite3_malloc(nRequestBytes);
21894 if( !pBase ){
21895 return SQLITE_NOMEM;
21896 }
21897 while( nBase<nRequestBytes ){
21898 /* Copy over data present on this page. */
21899 unsigned nCopyBytes = nRequestBytes - nBase;
21900 if( nLocalRecordBytes-iRequestOffset<nCopyBytes ){
21901 nCopyBytes = nLocalRecordBytes - iRequestOffset;
21902 }
21903 memcpy(pBase + nBase, PageData(pPage, iRecordOffset + iRequestOffset),
21904 nCopyBytes);
21905 nBase += nCopyBytes;
21906
21907 if( pOverflow ){
21908 /* Copy from start of record data in future pages. */
21909 iRequestOffset = 0;
21910
21911 /* Move forward to the next page in the list. Should match
21912 * first while() loop.
21913 */
21914 pPage = pOverflow->pPage;
21915 iRecordOffset = 4;
21916 nLocalRecordBytes = pOverflow->nPageSize - iRecordOffset;
21917 pOverflow = pOverflow->pNextOverflow;
21918 }else if( nBase<nRequestBytes ){
21919 /* Ran out of overflow pages with data left to deliver. Not
21920 * possible if the requested range fits within nRecordBytes
21921 * passed to overflowMaybeCreate() when creating pOverflow.
21922 */
21923 assert(NULL); /* NOTREACHED */
21924 sqlite3_free(pBase);
21925 return SQLITE_ERROR;
21926 }
21927 }
21928 assert( nBase==nRequestBytes );
21929 *ppBase = pBase;
21930 *pbFree = 1;
21931 return SQLITE_OK;
21932 }
21933
21934 /* Primary structure for iterating the contents of a table.
21935 *
21936 * leafCursorDestroy - release all resources associated with the cursor.
21937 * leafCursorCreate - create a cursor to iterate items from tree at
21938 * the provided root page.
21939 * leafCursorNextValidCell - get the cursor ready to access data from
21940 * the next valid cell in the table.
21941 * leafCursorCellRowid - get the current cell's rowid.
21942 * leafCursorCellColumns - get current cell's column count.
21943 * leafCursorCellColInfo - get type and data for a column in current cell.
21944 *
21945 * leafCursorNextValidCell skips cells which fail simple integrity
21946 * checks, such as overlapping other cells, or being located at
21947 * impossible offsets, or where header data doesn't correctly describe
21948 * payload data. Returns SQLITE_ROW if a valid cell is found,
21949 * SQLITE_DONE if all pages in the tree were exhausted.
21950 *
21951 * leafCursorCellColInfo() accounts for overflow pages in the style of
21952 * overflowGetSegment().
21953 */
21954 typedef struct RecoverLeafCursor RecoverLeafCursor;
21955 struct RecoverLeafCursor {
21956 RecoverInteriorCursor *pParent; /* Parent node to this node. */
21957 DbPage *pPage; /* Reference to leaf page. */
21958 unsigned nPageSize; /* Size of pPage. */
21959 unsigned nCells; /* Number of cells in pPage. */
21960 unsigned iCell; /* Current cell. */
21961
21962 /* Info parsed from data in iCell. */
21963 i64 iRowid; /* rowid parsed. */
21964 unsigned nRecordCols; /* how many items in the record. */
21965 u64 iRecordOffset; /* offset to record data. */
21966 /* TODO(shess): nRecordBytes and nRecordHeaderBytes are used in
21967 * leafCursorCellColInfo() to prevent buffer overruns.
21968 * leafCursorCellDecode() already verified that the cell is valid, so
21969 * those checks should be redundant.
21970 */
21971 u64 nRecordBytes; /* Size of record data. */
21972 unsigned nLocalRecordBytes; /* Amount of record data in-page. */
21973 unsigned nRecordHeaderBytes; /* Size of record header data. */
21974 unsigned char *pRecordHeader; /* Pointer to record header data. */
21975 int bFreeRecordHeader; /* True if record header requires free. */
21976 RecoverOverflow *pOverflow; /* Cell overflow info, if needed. */
21977 };
21978
21979 /* Internal helper shared between next-page and create-cursor. If
21980 * pPage is a leaf page, it will be stored in the cursor and state
21981 * initialized for reading cells.
21982 *
21983 * If pPage is an interior page, a new parent cursor is created and
21984 * injected on the stack. This is necessary to handle trees with
21985 * uneven depth, but also is used during initial setup.
21986 *
21987 * If pPage is not a table page at all, it is discarded.
21988 *
21989 * If SQLITE_OK is returned, the caller no longer owns pPage,
21990 * otherwise the caller is responsible for discarding it.
21991 */
21992 static int leafCursorLoadPage(RecoverLeafCursor *pCursor, DbPage *pPage){
21993 const unsigned char *pPageHeader; /* Header of *pPage */
21994
21995 /* Release the current page. */
21996 if( pCursor->pPage ){
21997 sqlite3PagerUnref(pCursor->pPage);
21998 pCursor->pPage = NULL;
21999 pCursor->iCell = pCursor->nCells = 0;
22000 }
22001
22002 /* If the page is an unexpected interior node, inject a new stack
22003 * layer and try again from there.
22004 */
22005 pPageHeader = PageHeader(pPage);
22006 if( pPageHeader[kiPageTypeOffset]==kTableInteriorPage ){
22007 RecoverInteriorCursor *pParent;
22008 int rc = interiorCursorCreate(pCursor->pParent, pPage, pCursor->nPageSize,
22009 &pParent);
22010 if( rc!=SQLITE_OK ){
22011 return rc;
22012 }
22013 pCursor->pParent = pParent;
22014 return SQLITE_OK;
22015 }
22016
22017 /* Not a leaf page, skip it. */
22018 if( pPageHeader[kiPageTypeOffset]!=kTableLeafPage ){
22019 sqlite3PagerUnref(pPage);
22020 return SQLITE_OK;
22021 }
22022
22023 /* Take ownership of the page and start decoding. */
22024 pCursor->pPage = pPage;
22025 pCursor->iCell = 0;
22026 pCursor->nCells = decodeUnsigned16(pPageHeader + kiPageCellCountOffset);
22027 return SQLITE_OK;
22028 }
22029
22030 /* Get the next leaf-level page in the tree. Returns SQLITE_ROW when
22031 * a leaf page is found, SQLITE_DONE when no more leaves exist, or any
22032 * error which occurred.
22033 */
22034 static int leafCursorNextPage(RecoverLeafCursor *pCursor){
22035 if( !pCursor->pParent ){
22036 return SQLITE_DONE;
22037 }
22038
22039 /* Repeatedly load the parent's next child page until a leaf is found. */
22040 do {
22041 DbPage *pNextPage;
22042 int rc = interiorCursorNextPage(&pCursor->pParent, &pNextPage);
22043 if( rc!=SQLITE_ROW ){
22044 assert( rc==SQLITE_DONE );
22045 return rc;
22046 }
22047
22048 rc = leafCursorLoadPage(pCursor, pNextPage);
22049 if( rc!=SQLITE_OK ){
22050 sqlite3PagerUnref(pNextPage);
22051 return rc;
22052 }
22053 } while( !pCursor->pPage );
22054
22055 return SQLITE_ROW;
22056 }
22057
22058 static void leafCursorDestroyCellData(RecoverLeafCursor *pCursor){
22059 if( pCursor->bFreeRecordHeader ){
22060 sqlite3_free(pCursor->pRecordHeader);
22061 }
22062 pCursor->bFreeRecordHeader = 0;
22063 pCursor->pRecordHeader = NULL;
22064
22065 if( pCursor->pOverflow ){
22066 overflowDestroy(pCursor->pOverflow);
22067 pCursor->pOverflow = NULL;
22068 }
22069 }
22070
22071 static void leafCursorDestroy(RecoverLeafCursor *pCursor){
22072 leafCursorDestroyCellData(pCursor);
22073
22074 if( pCursor->pParent ){
22075 interiorCursorDestroy(pCursor->pParent);
22076 pCursor->pParent = NULL;
22077 }
22078
22079 if( pCursor->pPage ){
22080 sqlite3PagerUnref(pCursor->pPage);
22081 pCursor->pPage = NULL;
22082 }
22083
22084 memset(pCursor, 0xA5, sizeof(*pCursor));
22085 sqlite3_free(pCursor);
22086 }
22087
22088 /* Create a cursor to iterate the rows from the leaf pages of a table
22089 * rooted at iRootPage.
22090 */
22091 /* TODO(shess): recoverOpen() calls this to setup the cursor, and I
22092 * think that recoverFilter() may make a hard assumption that the
22093 * cursor returned will turn up at least one valid cell.
22094 *
22095 * The cases I can think of which break this assumption are:
22096 * - pPage is a valid leaf page with no valid cells.
22097 * - pPage is a valid interior page with no valid leaves.
22098 * - pPage is a valid interior page who's leaves contain no valid cells.
22099 * - pPage is not a valid leaf or interior page.
22100 */
22101 static int leafCursorCreate(Pager *pPager, unsigned nPageSize,
22102 u32 iRootPage, RecoverLeafCursor **ppCursor){
22103 DbPage *pPage; /* Reference to page at iRootPage. */
22104 RecoverLeafCursor *pCursor; /* Leaf cursor being constructed. */
22105 int rc;
22106
22107 /* Start out with the root page. */
22108 rc = sqlite3PagerGet(pPager, iRootPage, &pPage, 0);
22109 if( rc!=SQLITE_OK ){
22110 return rc;
22111 }
22112
22113 pCursor = sqlite3_malloc(sizeof(RecoverLeafCursor));
22114 if( !pCursor ){
22115 sqlite3PagerUnref(pPage);
22116 return SQLITE_NOMEM;
22117 }
22118 memset(pCursor, 0, sizeof(*pCursor));
22119
22120 pCursor->nPageSize = nPageSize;
22121
22122 rc = leafCursorLoadPage(pCursor, pPage);
22123 if( rc!=SQLITE_OK ){
22124 sqlite3PagerUnref(pPage);
22125 leafCursorDestroy(pCursor);
22126 return rc;
22127 }
22128
22129 /* pPage wasn't a leaf page, find the next leaf page. */
22130 if( !pCursor->pPage ){
22131 rc = leafCursorNextPage(pCursor);
22132 if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ){
22133 leafCursorDestroy(pCursor);
22134 return rc;
22135 }
22136 }
22137
22138 *ppCursor = pCursor;
22139 return SQLITE_OK;
22140 }
22141
22142 /* Useful for setting breakpoints. */
22143 static int ValidateError(){
22144 return SQLITE_ERROR;
22145 }
22146
22147 /* Setup the cursor for reading the information from cell iCell. */
22148 static int leafCursorCellDecode(RecoverLeafCursor *pCursor){
22149 const unsigned char *pPageHeader; /* Header of current page. */
22150 const unsigned char *pPageEnd; /* Byte after end of current page. */
22151 const unsigned char *pCellOffsets; /* Pointer to page's cell offsets. */
22152 unsigned iCellOffset; /* Offset of current cell (iCell). */
22153 const unsigned char *pCell; /* Pointer to data at iCellOffset. */
22154 unsigned nCellMaxBytes; /* Maximum local size of iCell. */
22155 unsigned iEndOffset; /* End of iCell's in-page data. */
22156 u64 nRecordBytes; /* Expected size of cell, w/overflow. */
22157 u64 iRowid; /* iCell's rowid (in table). */
22158 unsigned nRead; /* Amount of cell read. */
22159 unsigned nRecordHeaderRead; /* Header data read. */
22160 u64 nRecordHeaderBytes; /* Header size expected. */
22161 unsigned nRecordCols; /* Columns read from header. */
22162 u64 nRecordColBytes; /* Bytes in payload for those columns. */
22163 unsigned i;
22164 int rc;
22165
22166 assert( pCursor->iCell<pCursor->nCells );
22167
22168 leafCursorDestroyCellData(pCursor);
22169
22170 /* Find the offset to the row. */
22171 pPageHeader = PageHeader(pCursor->pPage);
22172 pCellOffsets = pPageHeader + knPageLeafHeaderBytes;
22173 pPageEnd = PageData(pCursor->pPage, pCursor->nPageSize);
22174 if( pCellOffsets + pCursor->iCell*2 + 2 > pPageEnd ){
22175 return ValidateError();
22176 }
22177 iCellOffset = decodeUnsigned16(pCellOffsets + pCursor->iCell*2);
22178 if( iCellOffset>=pCursor->nPageSize ){
22179 return ValidateError();
22180 }
22181
22182 pCell = PageData(pCursor->pPage, iCellOffset);
22183 nCellMaxBytes = pCursor->nPageSize - iCellOffset;
22184
22185 /* B-tree leaf cells lead with varint record size, varint rowid and
22186 * varint header size.
22187 */
22188 /* TODO(shess): The smallest page size is 512 bytes, which has an m
22189 * of 39. Three varints need at most 27 bytes to encode. I think.
22190 */
22191 if( !checkVarints(pCell, nCellMaxBytes, 3) ){
22192 return ValidateError();
22193 }
22194
22195 nRead = getVarint(pCell, &nRecordBytes);
22196 assert( iCellOffset+nRead<=pCursor->nPageSize );
22197 pCursor->nRecordBytes = nRecordBytes;
22198
22199 nRead += getVarint(pCell + nRead, &iRowid);
22200 assert( iCellOffset+nRead<=pCursor->nPageSize );
22201 pCursor->iRowid = (i64)iRowid;
22202
22203 pCursor->iRecordOffset = iCellOffset + nRead;
22204
22205 /* Start overflow setup here because nLocalRecordBytes is needed to
22206 * check cell overlap.
22207 */
22208 rc = overflowMaybeCreate(pCursor->pPage, pCursor->nPageSize,
22209 pCursor->iRecordOffset, pCursor->nRecordBytes,
22210 &pCursor->nLocalRecordBytes,
22211 &pCursor->pOverflow);
22212 if( rc!=SQLITE_OK ){
22213 return ValidateError();
22214 }
22215
22216 /* Check that no other cell starts within this cell. */
22217 iEndOffset = pCursor->iRecordOffset + pCursor->nLocalRecordBytes;
22218 for( i=0; i<pCursor->nCells && pCellOffsets + i*2 + 2 <= pPageEnd; ++i ){
22219 const unsigned iOtherOffset = decodeUnsigned16(pCellOffsets + i*2);
22220 if( iOtherOffset>iCellOffset && iOtherOffset<iEndOffset ){
22221 return ValidateError();
22222 }
22223 }
22224
22225 nRecordHeaderRead = getVarint(pCell + nRead, &nRecordHeaderBytes);
22226 assert( nRecordHeaderBytes<=nRecordBytes );
22227 pCursor->nRecordHeaderBytes = nRecordHeaderBytes;
22228
22229 /* Large headers could overflow if pages are small. */
22230 rc = overflowGetSegment(pCursor->pPage,
22231 pCursor->iRecordOffset, pCursor->nLocalRecordBytes,
22232 pCursor->pOverflow, 0, nRecordHeaderBytes,
22233 &pCursor->pRecordHeader, &pCursor->bFreeRecordHeader);
22234 if( rc!=SQLITE_OK ){
22235 return ValidateError();
22236 }
22237
22238 /* Tally up the column count and size of data. */
22239 nRecordCols = 0;
22240 nRecordColBytes = 0;
22241 while( nRecordHeaderRead<nRecordHeaderBytes ){
22242 u64 iSerialType; /* Type descriptor for current column. */
22243 if( !checkVarint(pCursor->pRecordHeader + nRecordHeaderRead,
22244 nRecordHeaderBytes - nRecordHeaderRead) ){
22245 return ValidateError();
22246 }
22247 nRecordHeaderRead += getVarint(pCursor->pRecordHeader + nRecordHeaderRead,
22248 &iSerialType);
22249 if( iSerialType==10 || iSerialType==11 ){
22250 return ValidateError();
22251 }
22252 nRecordColBytes += SerialTypeLength(iSerialType);
22253 nRecordCols++;
22254 }
22255 pCursor->nRecordCols = nRecordCols;
22256
22257 /* Parsing the header used as many bytes as expected. */
22258 if( nRecordHeaderRead!=nRecordHeaderBytes ){
22259 return ValidateError();
22260 }
22261
22262 /* Calculated record is size of expected record. */
22263 if( nRecordHeaderBytes+nRecordColBytes!=nRecordBytes ){
22264 return ValidateError();
22265 }
22266
22267 return SQLITE_OK;
22268 }
22269
22270 static i64 leafCursorCellRowid(RecoverLeafCursor *pCursor){
22271 return pCursor->iRowid;
22272 }
22273
22274 static unsigned leafCursorCellColumns(RecoverLeafCursor *pCursor){
22275 return pCursor->nRecordCols;
22276 }
22277
22278 /* Get the column info for the cell. Pass NULL for ppBase to prevent
22279 * retrieving the data segment. If *pbFree is true, *ppBase must be
22280 * freed by the caller using sqlite3_free().
22281 */
22282 static int leafCursorCellColInfo(RecoverLeafCursor *pCursor,
22283 unsigned iCol, u64 *piColType,
22284 unsigned char **ppBase, int *pbFree){
22285 const unsigned char *pRecordHeader; /* Current cell's header. */
22286 u64 nRecordHeaderBytes; /* Bytes in pRecordHeader. */
22287 unsigned nRead; /* Bytes read from header. */
22288 u64 iColEndOffset; /* Offset to end of column in cell. */
22289 unsigned nColsSkipped; /* Count columns as procesed. */
22290 u64 iSerialType; /* Type descriptor for current column. */
22291
22292 /* Implicit NULL for columns past the end. This case happens when
22293 * rows have not been updated since an ALTER TABLE added columns.
22294 * It is more convenient to address here than in callers.
22295 */
22296 if( iCol>=pCursor->nRecordCols ){
22297 *piColType = 0;
22298 if( ppBase ){
22299 *ppBase = 0;
22300 *pbFree = 0;
22301 }
22302 return SQLITE_OK;
22303 }
22304
22305 /* Must be able to decode header size. */
22306 pRecordHeader = pCursor->pRecordHeader;
22307 if( !checkVarint(pRecordHeader, pCursor->nRecordHeaderBytes) ){
22308 return SQLITE_CORRUPT;
22309 }
22310
22311 /* Rather than caching the header size and how many bytes it took,
22312 * decode it every time.
22313 */
22314 nRead = getVarint(pRecordHeader, &nRecordHeaderBytes);
22315 assert( nRecordHeaderBytes==pCursor->nRecordHeaderBytes );
22316
22317 /* Scan forward to the indicated column. Scans to _after_ column
22318 * for later range checking.
22319 */
22320 /* TODO(shess): This could get expensive for very wide tables. An
22321 * array of iSerialType could be built in leafCursorCellDecode(), but
22322 * the number of columns is dynamic per row, so it would add memory
22323 * management complexity. Enough info to efficiently forward
22324 * iterate could be kept, if all clients forward iterate
22325 * (recoverColumn() may not).
22326 */
22327 iColEndOffset = 0;
22328 nColsSkipped = 0;
22329 while( nColsSkipped<=iCol && nRead<nRecordHeaderBytes ){
22330 if( !checkVarint(pRecordHeader + nRead, nRecordHeaderBytes - nRead) ){
22331 return SQLITE_CORRUPT;
22332 }
22333 nRead += getVarint(pRecordHeader + nRead, &iSerialType);
22334 iColEndOffset += SerialTypeLength(iSerialType);
22335 nColsSkipped++;
22336 }
22337
22338 /* Column's data extends past record's end. */
22339 if( nRecordHeaderBytes+iColEndOffset>pCursor->nRecordBytes ){
22340 return SQLITE_CORRUPT;
22341 }
22342
22343 *piColType = iSerialType;
22344 if( ppBase ){
22345 const u32 nColBytes = SerialTypeLength(iSerialType);
22346
22347 /* Offset from start of record to beginning of column. */
22348 const unsigned iColOffset = nRecordHeaderBytes+iColEndOffset-nColBytes;
22349
22350 return overflowGetSegment(pCursor->pPage, pCursor->iRecordOffset,
22351 pCursor->nLocalRecordBytes, pCursor->pOverflow,
22352 iColOffset, nColBytes, ppBase, pbFree);
22353 }
22354 return SQLITE_OK;
22355 }
22356
22357 static int leafCursorNextValidCell(RecoverLeafCursor *pCursor){
22358 while( 1 ){
22359 int rc;
22360
22361 /* Move to the next cell. */
22362 pCursor->iCell++;
22363
22364 /* No more cells, get the next leaf. */
22365 if( pCursor->iCell>=pCursor->nCells ){
22366 rc = leafCursorNextPage(pCursor);
22367 if( rc!=SQLITE_ROW ){
22368 return rc;
22369 }
22370 assert( pCursor->iCell==0 );
22371 }
22372
22373 /* If the cell is valid, indicate that a row is available. */
22374 rc = leafCursorCellDecode(pCursor);
22375 if( rc==SQLITE_OK ){
22376 return SQLITE_ROW;
22377 }
22378
22379 /* Iterate until done or a valid row is found. */
22380 /* TODO(shess): Remove debugging output. */
22381 fprintf(stderr, "Skipping invalid cell\n");
22382 }
22383 return SQLITE_ERROR;
22384 }
22385
22386 typedef struct Recover Recover;
22387 struct Recover {
22388 sqlite3_vtab base;
22389 sqlite3 *db; /* Host database connection */
22390 char *zDb; /* Database containing target table */
22391 char *zTable; /* Target table */
22392 unsigned nCols; /* Number of columns in target table */
22393 unsigned char *pTypes; /* Types of columns in target table */
22394 };
22395
22396 /* Internal helper for deleting the module. */
22397 static void recoverRelease(Recover *pRecover){
22398 sqlite3_free(pRecover->zDb);
22399 sqlite3_free(pRecover->zTable);
22400 sqlite3_free(pRecover->pTypes);
22401 memset(pRecover, 0xA5, sizeof(*pRecover));
22402 sqlite3_free(pRecover);
22403 }
22404
22405 /* Helper function for initializing the module. Forward-declared so
22406 * recoverCreate() and recoverConnect() can see it.
22407 */
22408 static int recoverInit(
22409 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **
22410 );
22411
22412 static int recoverCreate(
22413 sqlite3 *db,
22414 void *pAux,
22415 int argc, const char *const*argv,
22416 sqlite3_vtab **ppVtab,
22417 char **pzErr
22418 ){
22419 FNENTRY();
22420 return recoverInit(db, pAux, argc, argv, ppVtab, pzErr);
22421 }
22422
22423 /* This should never be called. */
22424 static int recoverConnect(
22425 sqlite3 *db,
22426 void *pAux,
22427 int argc, const char *const*argv,
22428 sqlite3_vtab **ppVtab,
22429 char **pzErr
22430 ){
22431 FNENTRY();
22432 return recoverInit(db, pAux, argc, argv, ppVtab, pzErr);
22433 }
22434
22435 /* No indices supported. */
22436 static int recoverBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
22437 FNENTRY();
22438 return SQLITE_OK;
22439 }
22440
22441 /* Logically, this should never be called. */
22442 static int recoverDisconnect(sqlite3_vtab *pVtab){
22443 FNENTRY();
22444 recoverRelease((Recover*)pVtab);
22445 return SQLITE_OK;
22446 }
22447
22448 static int recoverDestroy(sqlite3_vtab *pVtab){
22449 FNENTRY();
22450 recoverRelease((Recover*)pVtab);
22451 return SQLITE_OK;
22452 }
22453
22454 typedef struct RecoverCursor RecoverCursor;
22455 struct RecoverCursor {
22456 sqlite3_vtab_cursor base;
22457 RecoverLeafCursor *pLeafCursor;
22458 int iEncoding;
22459 int bEOF;
22460 };
22461
22462 static int recoverOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
22463 Recover *pRecover = (Recover*)pVTab;
22464 u32 iRootPage; /* Root page of the backing table. */
22465 int iEncoding; /* UTF encoding for backing database. */
22466 unsigned nPageSize; /* Size of pages in backing database. */
22467 Pager *pPager; /* Backing database pager. */
22468 RecoverLeafCursor *pLeafCursor; /* Cursor to read table's leaf pages. */
22469 RecoverCursor *pCursor; /* Cursor to read rows from leaves. */
22470 int rc;
22471
22472 FNENTRY();
22473
22474 iRootPage = 0;
22475 rc = getRootPage(pRecover->db, pRecover->zDb, pRecover->zTable,
22476 &iRootPage);
22477 if( rc!=SQLITE_OK ){
22478 return rc;
22479 }
22480
22481 iEncoding = 0;
22482 rc = getEncoding(pRecover->db, pRecover->zDb, &iEncoding);
22483 if( rc!=SQLITE_OK ){
22484 return rc;
22485 }
22486
22487 rc = GetPager(pRecover->db, pRecover->zDb, &pPager, &nPageSize);
22488 if( rc!=SQLITE_OK ){
22489 return rc;
22490 }
22491
22492 rc = leafCursorCreate(pPager, nPageSize, iRootPage, &pLeafCursor);
22493 if( rc!=SQLITE_OK ){
22494 return rc;
22495 }
22496
22497 pCursor = sqlite3_malloc(sizeof(RecoverCursor));
22498 if( !pCursor ){
22499 leafCursorDestroy(pLeafCursor);
22500 return SQLITE_NOMEM;
22501 }
22502 memset(pCursor, 0, sizeof(*pCursor));
22503 pCursor->base.pVtab = pVTab;
22504 pCursor->pLeafCursor = pLeafCursor;
22505 pCursor->iEncoding = iEncoding;
22506
22507 /* If no leaf pages were found, empty result set. */
22508 /* TODO(shess): leafCursorNextValidCell() would return SQLITE_ROW or
22509 * SQLITE_DONE to indicate whether there is further data to consider.
22510 */
22511 pCursor->bEOF = (pLeafCursor->pPage==NULL);
22512
22513 *ppCursor = (sqlite3_vtab_cursor*)pCursor;
22514 return SQLITE_OK;
22515 }
22516
22517 static int recoverClose(sqlite3_vtab_cursor *cur){
22518 RecoverCursor *pCursor = (RecoverCursor*)cur;
22519 FNENTRY();
22520 if( pCursor->pLeafCursor ){
22521 leafCursorDestroy(pCursor->pLeafCursor);
22522 pCursor->pLeafCursor = NULL;
22523 }
22524 memset(pCursor, 0xA5, sizeof(*pCursor));
22525 sqlite3_free(cur);
22526 return SQLITE_OK;
22527 }
22528
22529 /* Helpful place to set a breakpoint. */
22530 static int RecoverInvalidCell(){
22531 return SQLITE_ERROR;
22532 }
22533
22534 /* Returns SQLITE_OK if the cell has an appropriate number of columns
22535 * with the appropriate types of data.
22536 */
22537 static int recoverValidateLeafCell(Recover *pRecover, RecoverCursor *pCursor){
22538 unsigned i;
22539
22540 /* If the row's storage has too many columns, skip it. */
22541 if( leafCursorCellColumns(pCursor->pLeafCursor)>pRecover->nCols ){
22542 return RecoverInvalidCell();
22543 }
22544
22545 /* Skip rows with unexpected types. */
22546 for( i=0; i<pRecover->nCols; ++i ){
22547 u64 iType; /* Storage type of column i. */
22548 int rc;
22549
22550 /* ROWID alias. */
22551 if( (pRecover->pTypes[i]&MASK_ROWID) ){
22552 continue;
22553 }
22554
22555 rc = leafCursorCellColInfo(pCursor->pLeafCursor, i, &iType, NULL, NULL);
22556 assert( rc==SQLITE_OK );
22557 if( rc!=SQLITE_OK || !SerialTypeIsCompatible(iType, pRecover->pTypes[i]) ){
22558 return RecoverInvalidCell();
22559 }
22560 }
22561
22562 return SQLITE_OK;
22563 }
22564
22565 static int recoverNext(sqlite3_vtab_cursor *pVtabCursor){
22566 RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
22567 Recover *pRecover = (Recover*)pCursor->base.pVtab;
22568 int rc;
22569
22570 FNENTRY();
22571
22572 /* Scan forward to the next cell with valid storage, then check that
22573 * the stored data matches the schema.
22574 */
22575 while( (rc = leafCursorNextValidCell(pCursor->pLeafCursor))==SQLITE_ROW ){
22576 if( recoverValidateLeafCell(pRecover, pCursor)==SQLITE_OK ){
22577 return SQLITE_OK;
22578 }
22579 }
22580
22581 if( rc==SQLITE_DONE ){
22582 pCursor->bEOF = 1;
22583 return SQLITE_OK;
22584 }
22585
22586 assert( rc!=SQLITE_OK );
22587 return rc;
22588 }
22589
22590 static int recoverFilter(
22591 sqlite3_vtab_cursor *pVtabCursor,
22592 int idxNum, const char *idxStr,
22593 int argc, sqlite3_value **argv
22594 ){
22595 RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
22596 Recover *pRecover = (Recover*)pCursor->base.pVtab;
22597 int rc;
22598
22599 FNENTRY();
22600
22601 /* There were no valid leaf pages in the table. */
22602 if( pCursor->bEOF ){
22603 return SQLITE_OK;
22604 }
22605
22606 /* Load the first cell, and iterate forward if it's not valid. If no cells at
22607 * all are valid, recoverNext() sets bEOF and returns appropriately.
22608 */
22609 rc = leafCursorCellDecode(pCursor->pLeafCursor);
22610 if( rc!=SQLITE_OK || recoverValidateLeafCell(pRecover, pCursor)!=SQLITE_OK ){
22611 return recoverNext(pVtabCursor);
22612 }
22613
22614 return SQLITE_OK;
22615 }
22616
22617 static int recoverEof(sqlite3_vtab_cursor *pVtabCursor){
22618 RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
22619 FNENTRY();
22620 return pCursor->bEOF;
22621 }
22622
22623 static int recoverColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
22624 RecoverCursor *pCursor = (RecoverCursor*)cur;
22625 Recover *pRecover = (Recover*)pCursor->base.pVtab;
22626 u64 iColType; /* Storage type of column i. */
22627 unsigned char *pColData; /* Column i's data. */
22628 int shouldFree; /* Non-zero if pColData should be freed. */
22629 int rc;
22630
22631 FNENTRY();
22632
22633 if( (unsigned)i>=pRecover->nCols ){
22634 return SQLITE_ERROR;
22635 }
22636
22637 /* ROWID alias. */
22638 if( (pRecover->pTypes[i]&MASK_ROWID) ){
22639 sqlite3_result_int64(ctx, leafCursorCellRowid(pCursor->pLeafCursor));
22640 return SQLITE_OK;
22641 }
22642
22643 pColData = NULL;
22644 shouldFree = 0;
22645 rc = leafCursorCellColInfo(pCursor->pLeafCursor, i, &iColType,
22646 &pColData, &shouldFree);
22647 if( rc!=SQLITE_OK ){
22648 return rc;
22649 }
22650 /* recoverValidateLeafCell() should guarantee that this will never
22651 * occur.
22652 */
22653 if( !SerialTypeIsCompatible(iColType, pRecover->pTypes[i]) ){
22654 if( shouldFree ){
22655 sqlite3_free(pColData);
22656 }
22657 return SQLITE_ERROR;
22658 }
22659
22660 switch( iColType ){
22661 case 0 : sqlite3_result_null(ctx); break;
22662 case 1 : sqlite3_result_int64(ctx, decodeSigned(pColData, 1)); break;
22663 case 2 : sqlite3_result_int64(ctx, decodeSigned(pColData, 2)); break;
22664 case 3 : sqlite3_result_int64(ctx, decodeSigned(pColData, 3)); break;
22665 case 4 : sqlite3_result_int64(ctx, decodeSigned(pColData, 4)); break;
22666 case 5 : sqlite3_result_int64(ctx, decodeSigned(pColData, 6)); break;
22667 case 6 : sqlite3_result_int64(ctx, decodeSigned(pColData, 8)); break;
22668 case 7 : sqlite3_result_double(ctx, decodeFloat64(pColData)); break;
22669 case 8 : sqlite3_result_int(ctx, 0); break;
22670 case 9 : sqlite3_result_int(ctx, 1); break;
22671 case 10 : assert( iColType!=10 ); break;
22672 case 11 : assert( iColType!=11 ); break;
22673
22674 default : {
22675 u32 l = SerialTypeLength(iColType);
22676
22677 /* If pColData was already allocated, arrange to pass ownership. */
22678 sqlite3_destructor_type pFn = SQLITE_TRANSIENT;
22679 if( shouldFree ){
22680 pFn = sqlite3_free;
22681 shouldFree = 0;
22682 }
22683
22684 if( SerialTypeIsBlob(iColType) ){
22685 sqlite3_result_blob(ctx, pColData, l, pFn);
22686 }else{
22687 if( pCursor->iEncoding==SQLITE_UTF16LE ){
22688 sqlite3_result_text16le(ctx, (const void*)pColData, l, pFn);
22689 }else if( pCursor->iEncoding==SQLITE_UTF16BE ){
22690 sqlite3_result_text16be(ctx, (const void*)pColData, l, pFn);
22691 }else{
22692 sqlite3_result_text(ctx, (const char*)pColData, l, pFn);
22693 }
22694 }
22695 } break;
22696 }
22697 if( shouldFree ){
22698 sqlite3_free(pColData);
22699 }
22700 return SQLITE_OK;
22701 }
22702
22703 static int recoverRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
22704 RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
22705 FNENTRY();
22706 *pRowid = leafCursorCellRowid(pCursor->pLeafCursor);
22707 return SQLITE_OK;
22708 }
22709
22710 static sqlite3_module recoverModule = {
22711 0, /* iVersion */
22712 recoverCreate, /* xCreate - create a table */
22713 recoverConnect, /* xConnect - connect to an existing table */
22714 recoverBestIndex, /* xBestIndex - Determine search strategy */
22715 recoverDisconnect, /* xDisconnect - Disconnect from a table */
22716 recoverDestroy, /* xDestroy - Drop a table */
22717 recoverOpen, /* xOpen - open a cursor */
22718 recoverClose, /* xClose - close a cursor */
22719 recoverFilter, /* xFilter - configure scan constraints */
22720 recoverNext, /* xNext - advance a cursor */
22721 recoverEof, /* xEof */
22722 recoverColumn, /* xColumn - read data */
22723 recoverRowid, /* xRowid - read data */
22724 0, /* xUpdate - write data */
22725 0, /* xBegin - begin transaction */
22726 0, /* xSync - sync transaction */
22727 0, /* xCommit - commit transaction */
22728 0, /* xRollback - rollback transaction */
22729 0, /* xFindFunction - function overloading */
22730 0, /* xRename - rename the table */
22731 };
22732
22733 CHROMIUM_SQLITE_API
22734 int recoverVtableInit(sqlite3 *db){
22735 return sqlite3_create_module_v2(db, "recover", &recoverModule, NULL, 0);
22736 }
22737
22738 /* This section of code is for parsing the create input and
22739 * initializing the module.
22740 */
22741
22742 /* Find the next word in zText and place the endpoints in pzWord*.
22743 * Returns true if the word is non-empty. "Word" is defined as
22744 * ASCII alphanumeric plus '_' at this time.
22745 */
22746 static int findWord(const char *zText,
22747 const char **pzWordStart, const char **pzWordEnd){
22748 int r;
22749 while( ascii_isspace(*zText) ){
22750 zText++;
22751 }
22752 *pzWordStart = zText;
22753 while( ascii_isalnum(*zText) || *zText=='_' ){
22754 zText++;
22755 }
22756 r = zText>*pzWordStart; /* In case pzWordStart==pzWordEnd */
22757 *pzWordEnd = zText;
22758 return r;
22759 }
22760
22761 /* Return true if the next word in zText is zWord, also setting
22762 * *pzContinue to the character after the word.
22763 */
22764 static int expectWord(const char *zText, const char *zWord,
22765 const char **pzContinue){
22766 const char *zWordStart, *zWordEnd;
22767 if( findWord(zText, &zWordStart, &zWordEnd) &&
22768 ascii_strncasecmp(zWord, zWordStart, zWordEnd - zWordStart)==0 ){
22769 *pzContinue = zWordEnd;
22770 return 1;
22771 }
22772 return 0;
22773 }
22774
22775 /* Parse the name and type information out of parameter. In case of
22776 * success, *pzNameStart/End contain the name of the column,
22777 * *pzTypeStart/End contain the top-level type, and *pTypeMask has the
22778 * type mask to use for the column.
22779 */
22780 static int findNameAndType(const char *parameter,
22781 const char **pzNameStart, const char **pzNameEnd,
22782 const char **pzTypeStart, const char **pzTypeEnd,
22783 unsigned char *pTypeMask){
22784 unsigned nNameLen; /* Length of found name. */
22785 const char *zEnd; /* Current end of parsed column information. */
22786 int bNotNull; /* Non-zero if NULL is not allowed for name. */
22787 int bStrict; /* Non-zero if column requires exact type match. */
22788 const char *zDummy; /* Dummy parameter, result unused. */
22789 unsigned i;
22790
22791 /* strictMask is used for STRICT, strictMask|otherMask if STRICT is
22792 * not supplied. zReplace provides an alternate type to expose to
22793 * the caller.
22794 */
22795 static struct {
22796 const char *zName;
22797 unsigned char strictMask;
22798 unsigned char otherMask;
22799 const char *zReplace;
22800 } kTypeInfo[] = {
22801 { "ANY",
22802 MASK_INTEGER | MASK_FLOAT | MASK_BLOB | MASK_TEXT | MASK_NULL,
22803 0, "",
22804 },
22805 { "ROWID", MASK_INTEGER | MASK_ROWID, 0, "INTEGER", },
22806 { "INTEGER", MASK_INTEGER | MASK_NULL, 0, NULL, },
22807 { "FLOAT", MASK_FLOAT | MASK_NULL, MASK_INTEGER, NULL, },
22808 { "NUMERIC", MASK_INTEGER | MASK_FLOAT | MASK_NULL, MASK_TEXT, NULL, },
22809 { "TEXT", MASK_TEXT | MASK_NULL, MASK_BLOB, NULL, },
22810 { "BLOB", MASK_BLOB | MASK_NULL, 0, NULL, },
22811 };
22812
22813 if( !findWord(parameter, pzNameStart, pzNameEnd) ){
22814 return SQLITE_MISUSE;
22815 }
22816
22817 /* Manifest typing, accept any storage type. */
22818 if( !findWord(*pzNameEnd, pzTypeStart, pzTypeEnd) ){
22819 *pzTypeEnd = *pzTypeStart = "";
22820 *pTypeMask = MASK_INTEGER | MASK_FLOAT | MASK_BLOB | MASK_TEXT | MASK_NULL;
22821 return SQLITE_OK;
22822 }
22823
22824 nNameLen = *pzTypeEnd - *pzTypeStart;
22825 for( i=0; i<ArraySize(kTypeInfo); ++i ){
22826 if( ascii_strncasecmp(kTypeInfo[i].zName, *pzTypeStart, nNameLen)==0 ){
22827 break;
22828 }
22829 }
22830 if( i==ArraySize(kTypeInfo) ){
22831 return SQLITE_MISUSE;
22832 }
22833
22834 zEnd = *pzTypeEnd;
22835 bStrict = 0;
22836 if( expectWord(zEnd, "STRICT", &zEnd) ){
22837 /* TODO(shess): Ick. But I don't want another single-purpose
22838 * flag, either.
22839 */
22840 if( kTypeInfo[i].zReplace && !kTypeInfo[i].zReplace[0] ){
22841 return SQLITE_MISUSE;
22842 }
22843 bStrict = 1;
22844 }
22845
22846 bNotNull = 0;
22847 if( expectWord(zEnd, "NOT", &zEnd) ){
22848 if( expectWord(zEnd, "NULL", &zEnd) ){
22849 bNotNull = 1;
22850 }else{
22851 /* Anything other than NULL after NOT is an error. */
22852 return SQLITE_MISUSE;
22853 }
22854 }
22855
22856 /* Anything else is an error. */
22857 if( findWord(zEnd, &zDummy, &zDummy) ){
22858 return SQLITE_MISUSE;
22859 }
22860
22861 *pTypeMask = kTypeInfo[i].strictMask;
22862 if( !bStrict ){
22863 *pTypeMask |= kTypeInfo[i].otherMask;
22864 }
22865 if( bNotNull ){
22866 *pTypeMask &= ~MASK_NULL;
22867 }
22868 if( kTypeInfo[i].zReplace ){
22869 *pzTypeStart = kTypeInfo[i].zReplace;
22870 *pzTypeEnd = *pzTypeStart + strlen(*pzTypeStart);
22871 }
22872 return SQLITE_OK;
22873 }
22874
22875 /* Parse the arguments, placing type masks in *pTypes and the exposed
22876 * schema in *pzCreateSql (for sqlite3_declare_vtab).
22877 */
22878 static int ParseColumnsAndGenerateCreate(unsigned nCols,
22879 const char *const *pCols,
22880 char **pzCreateSql,
22881 unsigned char *pTypes,
22882 char **pzErr){
22883 unsigned i;
22884 char *zCreateSql = sqlite3_mprintf("CREATE TABLE x(");
22885 if( !zCreateSql ){
22886 return SQLITE_NOMEM;
22887 }
22888
22889 for( i=0; i<nCols; i++ ){
22890 const char *zSep = (i < nCols - 1 ? ", " : ")");
22891 const char *zNotNull = "";
22892 const char *zNameStart, *zNameEnd;
22893 const char *zTypeStart, *zTypeEnd;
22894 int rc = findNameAndType(pCols[i],
22895 &zNameStart, &zNameEnd,
22896 &zTypeStart, &zTypeEnd,
22897 &pTypes[i]);
22898 if( rc!=SQLITE_OK ){
22899 *pzErr = sqlite3_mprintf("unable to parse column %d", i);
22900 sqlite3_free(zCreateSql);
22901 return rc;
22902 }
22903
22904 if( !(pTypes[i]&MASK_NULL) ){
22905 zNotNull = " NOT NULL";
22906 }
22907
22908 /* Add name and type to the create statement. */
22909 zCreateSql = sqlite3_mprintf("%z%.*s %.*s%s%s",
22910 zCreateSql,
22911 zNameEnd - zNameStart, zNameStart,
22912 zTypeEnd - zTypeStart, zTypeStart,
22913 zNotNull, zSep);
22914 if( !zCreateSql ){
22915 return SQLITE_NOMEM;
22916 }
22917 }
22918
22919 *pzCreateSql = zCreateSql;
22920 return SQLITE_OK;
22921 }
22922
22923 /* Helper function for initializing the module. */
22924 /* argv[0] module name
22925 * argv[1] db name for virtual table
22926 * argv[2] virtual table name
22927 * argv[3] backing table name
22928 * argv[4] columns
22929 */
22930 /* TODO(shess): Since connect isn't supported, could inline into
22931 * recoverCreate().
22932 */
22933 /* TODO(shess): Explore cases where it would make sense to set *pzErr. */
22934 static int recoverInit(
22935 sqlite3 *db, /* Database connection */
22936 void *pAux, /* unused */
22937 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
22938 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
22939 char **pzErr /* OUT: Error message, if any */
22940 ){
22941 const int kTypeCol = 4; /* First argument with column type info. */
22942 Recover *pRecover; /* Virtual table structure being created. */
22943 char *zDot; /* Any dot found in "db.table" backing. */
22944 u32 iRootPage; /* Root page of backing table. */
22945 char *zCreateSql; /* Schema of created virtual table. */
22946 int rc;
22947
22948 /* Require to be in the temp database. */
22949 if( ascii_strcasecmp(argv[1], "temp")!=0 ){
22950 *pzErr = sqlite3_mprintf("recover table must be in temp database");
22951 return SQLITE_MISUSE;
22952 }
22953
22954 /* Need the backing table and at least one column. */
22955 if( argc<=kTypeCol ){
22956 *pzErr = sqlite3_mprintf("no columns specified");
22957 return SQLITE_MISUSE;
22958 }
22959
22960 pRecover = sqlite3_malloc(sizeof(Recover));
22961 if( !pRecover ){
22962 return SQLITE_NOMEM;
22963 }
22964 memset(pRecover, 0, sizeof(*pRecover));
22965 pRecover->base.pModule = &recoverModule;
22966 pRecover->db = db;
22967
22968 /* Parse out db.table, assuming main if no dot. */
22969 zDot = strchr(argv[3], '.');
22970 if( !zDot ){
22971 pRecover->zDb = sqlite3_strdup(db->aDb[0].zName);
22972 pRecover->zTable = sqlite3_strdup(argv[3]);
22973 }else if( zDot>argv[3] && zDot[1]!='\0' ){
22974 pRecover->zDb = sqlite3_strndup(argv[3], zDot - argv[3]);
22975 pRecover->zTable = sqlite3_strdup(zDot + 1);
22976 }else{
22977 /* ".table" or "db." not allowed. */
22978 *pzErr = sqlite3_mprintf("ill-formed table specifier");
22979 recoverRelease(pRecover);
22980 return SQLITE_ERROR;
22981 }
22982
22983 pRecover->nCols = argc - kTypeCol;
22984 pRecover->pTypes = sqlite3_malloc(pRecover->nCols);
22985 if( !pRecover->zDb || !pRecover->zTable || !pRecover->pTypes ){
22986 recoverRelease(pRecover);
22987 return SQLITE_NOMEM;
22988 }
22989
22990 /* Require the backing table to exist. */
22991 /* TODO(shess): Be more pedantic about the form of the descriptor
22992 * string. This already fails for poorly-formed strings, simply
22993 * because there won't be a root page, but it would make more sense
22994 * to be explicit.
22995 */
22996 rc = getRootPage(pRecover->db, pRecover->zDb, pRecover->zTable, &iRootPage);
22997 if( rc!=SQLITE_OK ){
22998 *pzErr = sqlite3_mprintf("unable to find backing table");
22999 recoverRelease(pRecover);
23000 return rc;
23001 }
23002
23003 /* Parse the column definitions. */
23004 rc = ParseColumnsAndGenerateCreate(pRecover->nCols, argv + kTypeCol,
23005 &zCreateSql, pRecover->pTypes, pzErr);
23006 if( rc!=SQLITE_OK ){
23007 recoverRelease(pRecover);
23008 return rc;
23009 }
23010
23011 rc = sqlite3_declare_vtab(db, zCreateSql);
23012 sqlite3_free(zCreateSql);
23013 if( rc!=SQLITE_OK ){
23014 recoverRelease(pRecover);
23015 return rc;
23016 }
23017
23018 *ppVtab = (sqlite3_vtab *)pRecover;
23019 return SQLITE_OK;
23020 }
23021
23022 /************** End of recover.c *********************************************/
23023
23024 /* Chain include. */
23025 #include "sqlite3.06.c"
OLDNEW
« no previous file with comments | « third_party/sqlite/amalgamation/sqlite3.04.c ('k') | third_party/sqlite/amalgamation/sqlite3.06.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698