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

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

Issue 2755803002: NCI: trybot test for sqlite 3.17 import. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 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 callback.c ****************************************/
2 /*
3 ** 2005 May 23
4 **
5 ** The author disclaims copyright to this source code. In place of
6 ** a legal notice, here is a blessing:
7 **
8 ** May you do good and not evil.
9 ** May you find forgiveness for yourself and forgive others.
10 ** May you share freely, never taking more than you give.
11 **
12 *************************************************************************
13 **
14 ** This file contains functions used to access the internal hash tables
15 ** of user defined functions and collation sequences.
16 */
17
18 /* #include "sqliteInt.h" */
19
20 /*
21 ** Invoke the 'collation needed' callback to request a collation sequence
22 ** in the encoding enc of name zName, length nName.
23 */
24 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
25 assert( !db->xCollNeeded || !db->xCollNeeded16 );
26 if( db->xCollNeeded ){
27 char *zExternal = sqlite3DbStrDup(db, zName);
28 if( !zExternal ) return;
29 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
30 sqlite3DbFree(db, zExternal);
31 }
32 #ifndef SQLITE_OMIT_UTF16
33 if( db->xCollNeeded16 ){
34 char const *zExternal;
35 sqlite3_value *pTmp = sqlite3ValueNew(db);
36 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
37 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
38 if( zExternal ){
39 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
40 }
41 sqlite3ValueFree(pTmp);
42 }
43 #endif
44 }
45
46 /*
47 ** This routine is called if the collation factory fails to deliver a
48 ** collation function in the best encoding but there may be other versions
49 ** of this collation function (for other text encodings) available. Use one
50 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
51 ** possible.
52 */
53 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
54 CollSeq *pColl2;
55 char *z = pColl->zName;
56 int i;
57 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
58 for(i=0; i<3; i++){
59 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
60 if( pColl2->xCmp!=0 ){
61 memcpy(pColl, pColl2, sizeof(CollSeq));
62 pColl->xDel = 0; /* Do not copy the destructor */
63 return SQLITE_OK;
64 }
65 }
66 return SQLITE_ERROR;
67 }
68
69 /*
70 ** This function is responsible for invoking the collation factory callback
71 ** or substituting a collation sequence of a different encoding when the
72 ** requested collation sequence is not available in the desired encoding.
73 **
74 ** If it is not NULL, then pColl must point to the database native encoding
75 ** collation sequence with name zName, length nName.
76 **
77 ** The return value is either the collation sequence to be used in database
78 ** db for collation type name zName, length nName, or NULL, if no collation
79 ** sequence can be found. If no collation is found, leave an error message.
80 **
81 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
82 */
83 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
84 Parse *pParse, /* Parsing context */
85 u8 enc, /* The desired encoding for the collating sequence */
86 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
87 const char *zName /* Collating sequence name */
88 ){
89 CollSeq *p;
90 sqlite3 *db = pParse->db;
91
92 p = pColl;
93 if( !p ){
94 p = sqlite3FindCollSeq(db, enc, zName, 0);
95 }
96 if( !p || !p->xCmp ){
97 /* No collation sequence of this type for this encoding is registered.
98 ** Call the collation factory to see if it can supply us with one.
99 */
100 callCollNeeded(db, enc, zName);
101 p = sqlite3FindCollSeq(db, enc, zName, 0);
102 }
103 if( p && !p->xCmp && synthCollSeq(db, p) ){
104 p = 0;
105 }
106 assert( !p || p->xCmp );
107 if( p==0 ){
108 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
109 }
110 return p;
111 }
112
113 /*
114 ** This routine is called on a collation sequence before it is used to
115 ** check that it is defined. An undefined collation sequence exists when
116 ** a database is loaded that contains references to collation sequences
117 ** that have not been defined by sqlite3_create_collation() etc.
118 **
119 ** If required, this routine calls the 'collation needed' callback to
120 ** request a definition of the collating sequence. If this doesn't work,
121 ** an equivalent collating sequence that uses a text encoding different
122 ** from the main database is substituted, if one is available.
123 */
124 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
125 if( pColl ){
126 const char *zName = pColl->zName;
127 sqlite3 *db = pParse->db;
128 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
129 if( !p ){
130 return SQLITE_ERROR;
131 }
132 assert( p==pColl );
133 }
134 return SQLITE_OK;
135 }
136
137
138
139 /*
140 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
141 ** specified by zName and nName is not found and parameter 'create' is
142 ** true, then create a new entry. Otherwise return NULL.
143 **
144 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
145 ** array of three CollSeq structures. The first is the collation sequence
146 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
147 **
148 ** Stored immediately after the three collation sequences is a copy of
149 ** the collation sequence name. A pointer to this string is stored in
150 ** each collation sequence structure.
151 */
152 static CollSeq *findCollSeqEntry(
153 sqlite3 *db, /* Database connection */
154 const char *zName, /* Name of the collating sequence */
155 int create /* Create a new entry if true */
156 ){
157 CollSeq *pColl;
158 pColl = sqlite3HashFind(&db->aCollSeq, zName);
159
160 if( 0==pColl && create ){
161 int nName = sqlite3Strlen30(zName);
162 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1);
163 if( pColl ){
164 CollSeq *pDel = 0;
165 pColl[0].zName = (char*)&pColl[3];
166 pColl[0].enc = SQLITE_UTF8;
167 pColl[1].zName = (char*)&pColl[3];
168 pColl[1].enc = SQLITE_UTF16LE;
169 pColl[2].zName = (char*)&pColl[3];
170 pColl[2].enc = SQLITE_UTF16BE;
171 memcpy(pColl[0].zName, zName, nName);
172 pColl[0].zName[nName] = 0;
173 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
174
175 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
176 ** return the pColl pointer to be deleted (because it wasn't added
177 ** to the hash table).
178 */
179 assert( pDel==0 || pDel==pColl );
180 if( pDel!=0 ){
181 sqlite3OomFault(db);
182 sqlite3DbFree(db, pDel);
183 pColl = 0;
184 }
185 }
186 }
187 return pColl;
188 }
189
190 /*
191 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
192 ** Return the CollSeq* pointer for the collation sequence named zName
193 ** for the encoding 'enc' from the database 'db'.
194 **
195 ** If the entry specified is not found and 'create' is true, then create a
196 ** new entry. Otherwise return NULL.
197 **
198 ** A separate function sqlite3LocateCollSeq() is a wrapper around
199 ** this routine. sqlite3LocateCollSeq() invokes the collation factory
200 ** if necessary and generates an error message if the collating sequence
201 ** cannot be found.
202 **
203 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
204 */
205 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
206 sqlite3 *db,
207 u8 enc,
208 const char *zName,
209 int create
210 ){
211 CollSeq *pColl;
212 if( zName ){
213 pColl = findCollSeqEntry(db, zName, create);
214 }else{
215 pColl = db->pDfltColl;
216 }
217 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
218 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
219 if( pColl ) pColl += enc-1;
220 return pColl;
221 }
222
223 /* During the search for the best function definition, this procedure
224 ** is called to test how well the function passed as the first argument
225 ** matches the request for a function with nArg arguments in a system
226 ** that uses encoding enc. The value returned indicates how well the
227 ** request is matched. A higher value indicates a better match.
228 **
229 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
230 ** is also -1. In other words, we are searching for a function that
231 ** takes a variable number of arguments.
232 **
233 ** If nArg is -2 that means that we are searching for any function
234 ** regardless of the number of arguments it uses, so return a positive
235 ** match score for any
236 **
237 ** The returned value is always between 0 and 6, as follows:
238 **
239 ** 0: Not a match.
240 ** 1: UTF8/16 conversion required and function takes any number of arguments.
241 ** 2: UTF16 byte order change required and function takes any number of args.
242 ** 3: encoding matches and function takes any number of arguments
243 ** 4: UTF8/16 conversion required - argument count matches exactly
244 ** 5: UTF16 byte order conversion required - argument count matches exactly
245 ** 6: Perfect match: encoding and argument count match exactly.
246 **
247 ** If nArg==(-2) then any function with a non-null xSFunc is
248 ** a perfect match and any function with xSFunc NULL is
249 ** a non-match.
250 */
251 #define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
252 static int matchQuality(
253 FuncDef *p, /* The function we are evaluating for match quality */
254 int nArg, /* Desired number of arguments. (-1)==any */
255 u8 enc /* Desired text encoding */
256 ){
257 int match;
258
259 /* nArg of -2 is a special case */
260 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
261
262 /* Wrong number of arguments means "no match" */
263 if( p->nArg!=nArg && p->nArg>=0 ) return 0;
264
265 /* Give a better score to a function with a specific number of arguments
266 ** than to function that accepts any number of arguments. */
267 if( p->nArg==nArg ){
268 match = 4;
269 }else{
270 match = 1;
271 }
272
273 /* Bonus points if the text encoding matches */
274 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
275 match += 2; /* Exact encoding match */
276 }else if( (enc & p->funcFlags & 2)!=0 ){
277 match += 1; /* Both are UTF16, but with different byte orders */
278 }
279
280 return match;
281 }
282
283 /*
284 ** Search a FuncDefHash for a function with the given name. Return
285 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
286 */
287 static FuncDef *functionSearch(
288 int h, /* Hash of the name */
289 const char *zFunc /* Name of function */
290 ){
291 FuncDef *p;
292 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
293 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
294 return p;
295 }
296 }
297 return 0;
298 }
299
300 /*
301 ** Insert a new FuncDef into a FuncDefHash hash table.
302 */
303 SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
304 FuncDef *aDef, /* List of global functions to be inserted */
305 int nDef /* Length of the apDef[] list */
306 ){
307 int i;
308 for(i=0; i<nDef; i++){
309 FuncDef *pOther;
310 const char *zName = aDef[i].zName;
311 int nName = sqlite3Strlen30(zName);
312 int h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ;
313 pOther = functionSearch(h, zName);
314 if( pOther ){
315 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
316 aDef[i].pNext = pOther->pNext;
317 pOther->pNext = &aDef[i];
318 }else{
319 aDef[i].pNext = 0;
320 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
321 sqlite3BuiltinFunctions.a[h] = &aDef[i];
322 }
323 }
324 }
325
326
327
328 /*
329 ** Locate a user function given a name, a number of arguments and a flag
330 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
331 ** pointer to the FuncDef structure that defines that function, or return
332 ** NULL if the function does not exist.
333 **
334 ** If the createFlag argument is true, then a new (blank) FuncDef
335 ** structure is created and liked into the "db" structure if a
336 ** no matching function previously existed.
337 **
338 ** If nArg is -2, then the first valid function found is returned. A
339 ** function is valid if xSFunc is non-zero. The nArg==(-2)
340 ** case is used to see if zName is a valid function name for some number
341 ** of arguments. If nArg is -2, then createFlag must be 0.
342 **
343 ** If createFlag is false, then a function with the required name and
344 ** number of arguments may be returned even if the eTextRep flag does not
345 ** match that requested.
346 */
347 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
348 sqlite3 *db, /* An open database */
349 const char *zName, /* Name of the function. zero-terminated */
350 int nArg, /* Number of arguments. -1 means any number */
351 u8 enc, /* Preferred text encoding */
352 u8 createFlag /* Create new entry if true and does not otherwise exist */
353 ){
354 FuncDef *p; /* Iterator variable */
355 FuncDef *pBest = 0; /* Best match found so far */
356 int bestScore = 0; /* Score of best match */
357 int h; /* Hash value */
358 int nName; /* Length of the name */
359
360 assert( nArg>=(-2) );
361 assert( nArg>=(-1) || createFlag==0 );
362 nName = sqlite3Strlen30(zName);
363
364 /* First search for a match amongst the application-defined functions.
365 */
366 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
367 while( p ){
368 int score = matchQuality(p, nArg, enc);
369 if( score>bestScore ){
370 pBest = p;
371 bestScore = score;
372 }
373 p = p->pNext;
374 }
375
376 /* If no match is found, search the built-in functions.
377 **
378 ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
379 ** functions even if a prior app-defined function was found. And give
380 ** priority to built-in functions.
381 **
382 ** Except, if createFlag is true, that means that we are trying to
383 ** install a new function. Whatever FuncDef structure is returned it will
384 ** have fields overwritten with new information appropriate for the
385 ** new function. But the FuncDefs for built-in functions are read-only.
386 ** So we must not search for built-ins when creating a new function.
387 */
388 if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
389 bestScore = 0;
390 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ;
391 p = functionSearch(h, zName);
392 while( p ){
393 int score = matchQuality(p, nArg, enc);
394 if( score>bestScore ){
395 pBest = p;
396 bestScore = score;
397 }
398 p = p->pNext;
399 }
400 }
401
402 /* If the createFlag parameter is true and the search did not reveal an
403 ** exact match for the name, number of arguments and encoding, then add a
404 ** new entry to the hash table and return it.
405 */
406 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
407 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
408 FuncDef *pOther;
409 pBest->zName = (const char*)&pBest[1];
410 pBest->nArg = (u16)nArg;
411 pBest->funcFlags = enc;
412 memcpy((char*)&pBest[1], zName, nName+1);
413 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
414 if( pOther==pBest ){
415 sqlite3DbFree(db, pBest);
416 sqlite3OomFault(db);
417 return 0;
418 }else{
419 pBest->pNext = pOther;
420 }
421 }
422
423 if( pBest && (pBest->xSFunc || createFlag) ){
424 return pBest;
425 }
426 return 0;
427 }
428
429 /*
430 ** Free all resources held by the schema structure. The void* argument points
431 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
432 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
433 ** of the schema hash tables).
434 **
435 ** The Schema.cache_size variable is not cleared.
436 */
437 SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
438 Hash temp1;
439 Hash temp2;
440 HashElem *pElem;
441 Schema *pSchema = (Schema *)p;
442
443 temp1 = pSchema->tblHash;
444 temp2 = pSchema->trigHash;
445 sqlite3HashInit(&pSchema->trigHash);
446 sqlite3HashClear(&pSchema->idxHash);
447 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
448 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
449 }
450 sqlite3HashClear(&temp2);
451 sqlite3HashInit(&pSchema->tblHash);
452 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
453 Table *pTab = sqliteHashData(pElem);
454 sqlite3DeleteTable(0, pTab);
455 }
456 sqlite3HashClear(&temp1);
457 sqlite3HashClear(&pSchema->fkeyHash);
458 pSchema->pSeqTab = 0;
459 if( pSchema->schemaFlags & DB_SchemaLoaded ){
460 pSchema->iGeneration++;
461 pSchema->schemaFlags &= ~DB_SchemaLoaded;
462 }
463 }
464
465 /*
466 ** Find and return the schema associated with a BTree. Create
467 ** a new one if necessary.
468 */
469 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
470 Schema * p;
471 if( pBt ){
472 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
473 }else{
474 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
475 }
476 if( !p ){
477 sqlite3OomFault(db);
478 }else if ( 0==p->file_format ){
479 sqlite3HashInit(&p->tblHash);
480 sqlite3HashInit(&p->idxHash);
481 sqlite3HashInit(&p->trigHash);
482 sqlite3HashInit(&p->fkeyHash);
483 p->enc = SQLITE_UTF8;
484 }
485 return p;
486 }
487
488 /************** End of callback.c ********************************************/
489 /************** Begin file delete.c ******************************************/
490 /*
491 ** 2001 September 15
492 **
493 ** The author disclaims copyright to this source code. In place of
494 ** a legal notice, here is a blessing:
495 **
496 ** May you do good and not evil.
497 ** May you find forgiveness for yourself and forgive others.
498 ** May you share freely, never taking more than you give.
499 **
500 *************************************************************************
501 ** This file contains C code routines that are called by the parser
502 ** in order to generate code for DELETE FROM statements.
503 */
504 /* #include "sqliteInt.h" */
505
506 /*
507 ** While a SrcList can in general represent multiple tables and subqueries
508 ** (as in the FROM clause of a SELECT statement) in this case it contains
509 ** the name of a single table, as one might find in an INSERT, DELETE,
510 ** or UPDATE statement. Look up that table in the symbol table and
511 ** return a pointer. Set an error message and return NULL if the table
512 ** name is not found or if any other error occurs.
513 **
514 ** The following fields are initialized appropriate in pSrc:
515 **
516 ** pSrc->a[0].pTab Pointer to the Table object
517 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
518 **
519 */
520 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
521 struct SrcList_item *pItem = pSrc->a;
522 Table *pTab;
523 assert( pItem && pSrc->nSrc==1 );
524 pTab = sqlite3LocateTableItem(pParse, 0, pItem);
525 sqlite3DeleteTable(pParse->db, pItem->pTab);
526 pItem->pTab = pTab;
527 if( pTab ){
528 pTab->nTabRef++;
529 }
530 if( sqlite3IndexedByLookup(pParse, pItem) ){
531 pTab = 0;
532 }
533 return pTab;
534 }
535
536 /*
537 ** Check to make sure the given table is writable. If it is not
538 ** writable, generate an error message and return 1. If it is
539 ** writable return 0;
540 */
541 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
542 /* A table is not writable under the following circumstances:
543 **
544 ** 1) It is a virtual table and no implementation of the xUpdate method
545 ** has been provided, or
546 ** 2) It is a system table (i.e. sqlite_master), this call is not
547 ** part of a nested parse and writable_schema pragma has not
548 ** been specified.
549 **
550 ** In either case leave an error message in pParse and return non-zero.
551 */
552 if( ( IsVirtual(pTab)
553 && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
554 || ( (pTab->tabFlags & TF_Readonly)!=0
555 && (pParse->db->flags & SQLITE_WriteSchema)==0
556 && pParse->nested==0 )
557 ){
558 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
559 return 1;
560 }
561
562 #ifndef SQLITE_OMIT_VIEW
563 if( !viewOk && pTab->pSelect ){
564 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
565 return 1;
566 }
567 #endif
568 return 0;
569 }
570
571
572 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
573 /*
574 ** Evaluate a view and store its result in an ephemeral table. The
575 ** pWhere argument is an optional WHERE clause that restricts the
576 ** set of rows in the view that are to be added to the ephemeral table.
577 */
578 SQLITE_PRIVATE void sqlite3MaterializeView(
579 Parse *pParse, /* Parsing context */
580 Table *pView, /* View definition */
581 Expr *pWhere, /* Optional WHERE clause to be added */
582 int iCur /* Cursor number for ephemeral table */
583 ){
584 SelectDest dest;
585 Select *pSel;
586 SrcList *pFrom;
587 sqlite3 *db = pParse->db;
588 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
589 pWhere = sqlite3ExprDup(db, pWhere, 0);
590 pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
591 if( pFrom ){
592 assert( pFrom->nSrc==1 );
593 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
594 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
595 assert( pFrom->a[0].pOn==0 );
596 assert( pFrom->a[0].pUsing==0 );
597 }
598 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0,
599 SF_IncludeHidden, 0, 0);
600 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
601 sqlite3Select(pParse, pSel, &dest);
602 sqlite3SelectDelete(db, pSel);
603 }
604 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
605
606 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
607 /*
608 ** Generate an expression tree to implement the WHERE, ORDER BY,
609 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
610 **
611 ** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
612 ** \__________________________/
613 ** pLimitWhere (pInClause)
614 */
615 SQLITE_PRIVATE Expr *sqlite3LimitWhere(
616 Parse *pParse, /* The parser context */
617 SrcList *pSrc, /* the FROM clause -- which tables to scan */
618 Expr *pWhere, /* The WHERE clause. May be null */
619 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
620 Expr *pLimit, /* The LIMIT clause. May be null */
621 Expr *pOffset, /* The OFFSET clause. May be null */
622 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
623 ){
624 Expr *pWhereRowid = NULL; /* WHERE rowid .. */
625 Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */
626 Expr *pSelectRowid = NULL; /* SELECT rowid ... */
627 ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */
628 SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */
629 Select *pSelect = NULL; /* Complete SELECT tree */
630
631 /* Check that there isn't an ORDER BY without a LIMIT clause.
632 */
633 if( pOrderBy && (pLimit == 0) ) {
634 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
635 goto limit_where_cleanup;
636 }
637
638 /* We only need to generate a select expression if there
639 ** is a limit/offset term to enforce.
640 */
641 if( pLimit == 0 ) {
642 /* if pLimit is null, pOffset will always be null as well. */
643 assert( pOffset == 0 );
644 return pWhere;
645 }
646
647 /* Generate a select expression tree to enforce the limit/offset
648 ** term for the DELETE or UPDATE statement. For example:
649 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
650 ** becomes:
651 ** DELETE FROM table_a WHERE rowid IN (
652 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
653 ** );
654 */
655
656 pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0);
657 if( pSelectRowid == 0 ) goto limit_where_cleanup;
658 pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
659 if( pEList == 0 ) goto limit_where_cleanup;
660
661 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
662 ** and the SELECT subtree. */
663 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
664 if( pSelectSrc == 0 ) {
665 sqlite3ExprListDelete(pParse->db, pEList);
666 goto limit_where_cleanup;
667 }
668
669 /* generate the SELECT expression tree. */
670 pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
671 pOrderBy,0,pLimit,pOffset);
672 if( pSelect == 0 ) return 0;
673
674 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
675 pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0);
676 pInClause = pWhereRowid ? sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0) : 0;
677 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
678 return pInClause;
679
680 limit_where_cleanup:
681 sqlite3ExprDelete(pParse->db, pWhere);
682 sqlite3ExprListDelete(pParse->db, pOrderBy);
683 sqlite3ExprDelete(pParse->db, pLimit);
684 sqlite3ExprDelete(pParse->db, pOffset);
685 return 0;
686 }
687 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
688 /* && !defined(SQLITE_OMIT_SUBQUERY) */
689
690 /*
691 ** Generate code for a DELETE FROM statement.
692 **
693 ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
694 ** \________/ \________________/
695 ** pTabList pWhere
696 */
697 SQLITE_PRIVATE void sqlite3DeleteFrom(
698 Parse *pParse, /* The parser context */
699 SrcList *pTabList, /* The table from which we should delete things */
700 Expr *pWhere /* The WHERE clause. May be null */
701 ){
702 Vdbe *v; /* The virtual database engine */
703 Table *pTab; /* The table from which records will be deleted */
704 int i; /* Loop counter */
705 WhereInfo *pWInfo; /* Information about the WHERE clause */
706 Index *pIdx; /* For looping over indices of the table */
707 int iTabCur; /* Cursor number for the table */
708 int iDataCur = 0; /* VDBE cursor for the canonical data source */
709 int iIdxCur = 0; /* Cursor number of the first index */
710 int nIdx; /* Number of indices */
711 sqlite3 *db; /* Main database structure */
712 AuthContext sContext; /* Authorization context */
713 NameContext sNC; /* Name context to resolve expressions in */
714 int iDb; /* Database number */
715 int memCnt = -1; /* Memory cell used for change counting */
716 int rcauth; /* Value returned by authorization callback */
717 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
718 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
719 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
720 Index *pPk; /* The PRIMARY KEY index on the table */
721 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
722 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
723 int iKey; /* Memory cell holding key of row to be deleted */
724 i16 nKey; /* Number of memory cells in the row key */
725 int iEphCur = 0; /* Ephemeral table holding all primary key values */
726 int iRowSet = 0; /* Register for rowset of rows to delete */
727 int addrBypass = 0; /* Address of jump over the delete logic */
728 int addrLoop = 0; /* Top of the delete loop */
729 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
730 int bComplex; /* True if there are triggers or FKs or
731 ** subqueries in the WHERE clause */
732
733 #ifndef SQLITE_OMIT_TRIGGER
734 int isView; /* True if attempting to delete from a view */
735 Trigger *pTrigger; /* List of table triggers, if required */
736 #endif
737
738 memset(&sContext, 0, sizeof(sContext));
739 db = pParse->db;
740 if( pParse->nErr || db->mallocFailed ){
741 goto delete_from_cleanup;
742 }
743 assert( pTabList->nSrc==1 );
744
745 /* Locate the table which we want to delete. This table has to be
746 ** put in an SrcList structure because some of the subroutines we
747 ** will be calling are designed to work with multiple tables and expect
748 ** an SrcList* parameter instead of just a Table* parameter.
749 */
750 pTab = sqlite3SrcListLookup(pParse, pTabList);
751 if( pTab==0 ) goto delete_from_cleanup;
752
753 /* Figure out if we have any triggers and if the table being
754 ** deleted from is a view
755 */
756 #ifndef SQLITE_OMIT_TRIGGER
757 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
758 isView = pTab->pSelect!=0;
759 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
760 #else
761 # define pTrigger 0
762 # define isView 0
763 #endif
764 #ifdef SQLITE_OMIT_VIEW
765 # undef isView
766 # define isView 0
767 #endif
768
769 /* If pTab is really a view, make sure it has been initialized.
770 */
771 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
772 goto delete_from_cleanup;
773 }
774
775 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
776 goto delete_from_cleanup;
777 }
778 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
779 assert( iDb<db->nDb );
780 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
781 db->aDb[iDb].zDbSName);
782 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
783 if( rcauth==SQLITE_DENY ){
784 goto delete_from_cleanup;
785 }
786 assert(!isView || pTrigger);
787
788 /* Assign cursor numbers to the table and all its indices.
789 */
790 assert( pTabList->nSrc==1 );
791 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
792 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
793 pParse->nTab++;
794 }
795
796 /* Start the view context
797 */
798 if( isView ){
799 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
800 }
801
802 /* Begin generating code.
803 */
804 v = sqlite3GetVdbe(pParse);
805 if( v==0 ){
806 goto delete_from_cleanup;
807 }
808 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
809 sqlite3BeginWriteOperation(pParse, 1, iDb);
810
811 /* If we are trying to delete from a view, realize that view into
812 ** an ephemeral table.
813 */
814 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
815 if( isView ){
816 sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
817 iDataCur = iIdxCur = iTabCur;
818 }
819 #endif
820
821 /* Resolve the column names in the WHERE clause.
822 */
823 memset(&sNC, 0, sizeof(sNC));
824 sNC.pParse = pParse;
825 sNC.pSrcList = pTabList;
826 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
827 goto delete_from_cleanup;
828 }
829
830 /* Initialize the counter of the number of rows deleted, if
831 ** we are counting rows.
832 */
833 if( db->flags & SQLITE_CountRows ){
834 memCnt = ++pParse->nMem;
835 sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
836 }
837
838 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
839 /* Special case: A DELETE without a WHERE clause deletes everything.
840 ** It is easier just to erase the whole table. Prior to version 3.6.5,
841 ** this optimization caused the row change count (the value returned by
842 ** API function sqlite3_count_changes) to be set incorrectly. */
843 if( rcauth==SQLITE_OK
844 && pWhere==0
845 && !bComplex
846 && !IsVirtual(pTab)
847 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
848 && db->xPreUpdateCallback==0
849 #endif
850 ){
851 assert( !isView );
852 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
853 if( HasRowid(pTab) ){
854 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
855 pTab->zName, P4_STATIC);
856 }
857 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
858 assert( pIdx->pSchema==pTab->pSchema );
859 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
860 }
861 }else
862 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
863 {
864 u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE;
865 if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
866 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
867 if( HasRowid(pTab) ){
868 /* For a rowid table, initialize the RowSet to an empty set */
869 pPk = 0;
870 nPk = 1;
871 iRowSet = ++pParse->nMem;
872 sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
873 }else{
874 /* For a WITHOUT ROWID table, create an ephemeral table used to
875 ** hold all primary keys for rows to be deleted. */
876 pPk = sqlite3PrimaryKeyIndex(pTab);
877 assert( pPk!=0 );
878 nPk = pPk->nKeyCol;
879 iPk = pParse->nMem+1;
880 pParse->nMem += nPk;
881 iEphCur = pParse->nTab++;
882 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
883 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
884 }
885
886 /* Construct a query to find the rowid or primary key for every row
887 ** to be deleted, based on the WHERE clause. Set variable eOnePass
888 ** to indicate the strategy used to implement this delete:
889 **
890 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
891 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
892 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
893 */
894 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
895 if( pWInfo==0 ) goto delete_from_cleanup;
896 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
897 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
898 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
899
900 /* Keep track of the number of rows to be deleted */
901 if( db->flags & SQLITE_CountRows ){
902 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
903 }
904
905 /* Extract the rowid or primary key for the current row */
906 if( pPk ){
907 for(i=0; i<nPk; i++){
908 assert( pPk->aiColumn[i]>=0 );
909 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
910 pPk->aiColumn[i], iPk+i);
911 }
912 iKey = iPk;
913 }else{
914 iKey = pParse->nMem + 1;
915 iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
916 if( iKey>pParse->nMem ) pParse->nMem = iKey;
917 }
918
919 if( eOnePass!=ONEPASS_OFF ){
920 /* For ONEPASS, no need to store the rowid/primary-key. There is only
921 ** one, so just keep it in its register(s) and fall through to the
922 ** delete code. */
923 nKey = nPk; /* OP_Found will use an unpacked key */
924 aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
925 if( aToOpen==0 ){
926 sqlite3WhereEnd(pWInfo);
927 goto delete_from_cleanup;
928 }
929 memset(aToOpen, 1, nIdx+1);
930 aToOpen[nIdx+1] = 0;
931 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
932 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
933 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
934 }else{
935 if( pPk ){
936 /* Add the PK key for this row to the temporary table */
937 iKey = ++pParse->nMem;
938 nKey = 0; /* Zero tells OP_Found to use a composite key */
939 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
940 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
941 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk);
942 }else{
943 /* Add the rowid of the row to be deleted to the RowSet */
944 nKey = 1; /* OP_Seek always uses a single rowid */
945 sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
946 }
947 }
948
949 /* If this DELETE cannot use the ONEPASS strategy, this is the
950 ** end of the WHERE loop */
951 if( eOnePass!=ONEPASS_OFF ){
952 addrBypass = sqlite3VdbeMakeLabel(v);
953 }else{
954 sqlite3WhereEnd(pWInfo);
955 }
956
957 /* Unless this is a view, open cursors for the table we are
958 ** deleting from and all its indices. If this is a view, then the
959 ** only effect this statement has is to fire the INSTEAD OF
960 ** triggers.
961 */
962 if( !isView ){
963 int iAddrOnce = 0;
964 if( eOnePass==ONEPASS_MULTI ){
965 iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
966 }
967 testcase( IsVirtual(pTab) );
968 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
969 iTabCur, aToOpen, &iDataCur, &iIdxCur);
970 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
971 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
972 if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
973 }
974
975 /* Set up a loop over the rowids/primary-keys that were found in the
976 ** where-clause loop above.
977 */
978 if( eOnePass!=ONEPASS_OFF ){
979 assert( nKey==nPk ); /* OP_Found will use an unpacked key */
980 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
981 assert( pPk!=0 || pTab->pSelect!=0 );
982 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
983 VdbeCoverage(v);
984 }
985 }else if( pPk ){
986 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
987 sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
988 assert( nKey==0 ); /* OP_Found will use a composite key */
989 }else{
990 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
991 VdbeCoverage(v);
992 assert( nKey==1 );
993 }
994
995 /* Delete the row */
996 #ifndef SQLITE_OMIT_VIRTUALTABLE
997 if( IsVirtual(pTab) ){
998 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
999 sqlite3VtabMakeWritable(pParse, pTab);
1000 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
1001 sqlite3VdbeChangeP5(v, OE_Abort);
1002 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
1003 sqlite3MayAbort(pParse);
1004 if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){
1005 pParse->isMultiWrite = 0;
1006 }
1007 }else
1008 #endif
1009 {
1010 int count = (pParse->nested==0); /* True to count changes */
1011 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
1012 iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
1013 }
1014
1015 /* End of the loop over all rowids/primary-keys. */
1016 if( eOnePass!=ONEPASS_OFF ){
1017 sqlite3VdbeResolveLabel(v, addrBypass);
1018 sqlite3WhereEnd(pWInfo);
1019 }else if( pPk ){
1020 sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
1021 sqlite3VdbeJumpHere(v, addrLoop);
1022 }else{
1023 sqlite3VdbeGoto(v, addrLoop);
1024 sqlite3VdbeJumpHere(v, addrLoop);
1025 }
1026 } /* End non-truncate path */
1027
1028 /* Update the sqlite_sequence table by storing the content of the
1029 ** maximum rowid counter values recorded while inserting into
1030 ** autoincrement tables.
1031 */
1032 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
1033 sqlite3AutoincrementEnd(pParse);
1034 }
1035
1036 /* Return the number of rows that were deleted. If this routine is
1037 ** generating code because of a call to sqlite3NestedParse(), do not
1038 ** invoke the callback function.
1039 */
1040 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
1041 sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
1042 sqlite3VdbeSetNumCols(v, 1);
1043 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
1044 }
1045
1046 delete_from_cleanup:
1047 sqlite3AuthContextPop(&sContext);
1048 sqlite3SrcListDelete(db, pTabList);
1049 sqlite3ExprDelete(db, pWhere);
1050 sqlite3DbFree(db, aToOpen);
1051 return;
1052 }
1053 /* Make sure "isView" and other macros defined above are undefined. Otherwise
1054 ** they may interfere with compilation of other functions in this file
1055 ** (or in another file, if this file becomes part of the amalgamation). */
1056 #ifdef isView
1057 #undef isView
1058 #endif
1059 #ifdef pTrigger
1060 #undef pTrigger
1061 #endif
1062
1063 /*
1064 ** This routine generates VDBE code that causes a single row of a
1065 ** single table to be deleted. Both the original table entry and
1066 ** all indices are removed.
1067 **
1068 ** Preconditions:
1069 **
1070 ** 1. iDataCur is an open cursor on the btree that is the canonical data
1071 ** store for the table. (This will be either the table itself,
1072 ** in the case of a rowid table, or the PRIMARY KEY index in the case
1073 ** of a WITHOUT ROWID table.)
1074 **
1075 ** 2. Read/write cursors for all indices of pTab must be open as
1076 ** cursor number iIdxCur+i for the i-th index.
1077 **
1078 ** 3. The primary key for the row to be deleted must be stored in a
1079 ** sequence of nPk memory cells starting at iPk. If nPk==0 that means
1080 ** that a search record formed from OP_MakeRecord is contained in the
1081 ** single memory location iPk.
1082 **
1083 ** eMode:
1084 ** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
1085 ** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
1086 ** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
1087 ** then this function must seek iDataCur to the entry identified by iPk
1088 ** and nPk before reading from it.
1089 **
1090 ** If eMode is ONEPASS_MULTI, then this call is being made as part
1091 ** of a ONEPASS delete that affects multiple rows. In this case, if
1092 ** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
1093 ** iDataCur, then its position should be preserved following the delete
1094 ** operation. Or, if iIdxNoSeek is not a valid cursor number, the
1095 ** position of iDataCur should be preserved instead.
1096 **
1097 ** iIdxNoSeek:
1098 ** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
1099 ** then it identifies an index cursor (from within array of cursors
1100 ** starting at iIdxCur) that already points to the index entry to be deleted.
1101 ** Except, this optimization is disabled if there are BEFORE triggers since
1102 ** the trigger body might have moved the cursor.
1103 */
1104 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
1105 Parse *pParse, /* Parsing context */
1106 Table *pTab, /* Table containing the row to be deleted */
1107 Trigger *pTrigger, /* List of triggers to (potentially) fire */
1108 int iDataCur, /* Cursor from which column data is extracted */
1109 int iIdxCur, /* First index cursor */
1110 int iPk, /* First memory cell containing the PRIMARY KEY */
1111 i16 nPk, /* Number of PRIMARY KEY memory cells */
1112 u8 count, /* If non-zero, increment the row change counter */
1113 u8 onconf, /* Default ON CONFLICT policy for triggers */
1114 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
1115 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
1116 ){
1117 Vdbe *v = pParse->pVdbe; /* Vdbe */
1118 int iOld = 0; /* First register in OLD.* array */
1119 int iLabel; /* Label resolved to end of generated code */
1120 u8 opSeek; /* Seek opcode */
1121
1122 /* Vdbe is guaranteed to have been allocated by this stage. */
1123 assert( v );
1124 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
1125 iDataCur, iIdxCur, iPk, (int)nPk));
1126
1127 /* Seek cursor iCur to the row to delete. If this row no longer exists
1128 ** (this can happen if a trigger program has already deleted it), do
1129 ** not attempt to delete it or fire any DELETE triggers. */
1130 iLabel = sqlite3VdbeMakeLabel(v);
1131 opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
1132 if( eMode==ONEPASS_OFF ){
1133 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
1134 VdbeCoverageIf(v, opSeek==OP_NotExists);
1135 VdbeCoverageIf(v, opSeek==OP_NotFound);
1136 }
1137
1138 /* If there are any triggers to fire, allocate a range of registers to
1139 ** use for the old.* references in the triggers. */
1140 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
1141 u32 mask; /* Mask of OLD.* columns in use */
1142 int iCol; /* Iterator used while populating OLD.* */
1143 int addrStart; /* Start of BEFORE trigger programs */
1144
1145 /* TODO: Could use temporary registers here. Also could attempt to
1146 ** avoid copying the contents of the rowid register. */
1147 mask = sqlite3TriggerColmask(
1148 pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
1149 );
1150 mask |= sqlite3FkOldmask(pParse, pTab);
1151 iOld = pParse->nMem+1;
1152 pParse->nMem += (1 + pTab->nCol);
1153
1154 /* Populate the OLD.* pseudo-table register array. These values will be
1155 ** used by any BEFORE and AFTER triggers that exist. */
1156 sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
1157 for(iCol=0; iCol<pTab->nCol; iCol++){
1158 testcase( mask!=0xffffffff && iCol==31 );
1159 testcase( mask!=0xffffffff && iCol==32 );
1160 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
1161 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
1162 }
1163 }
1164
1165 /* Invoke BEFORE DELETE trigger programs. */
1166 addrStart = sqlite3VdbeCurrentAddr(v);
1167 sqlite3CodeRowTrigger(pParse, pTrigger,
1168 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
1169 );
1170
1171 /* If any BEFORE triggers were coded, then seek the cursor to the
1172 ** row to be deleted again. It may be that the BEFORE triggers moved
1173 ** the cursor or already deleted the row that the cursor was
1174 ** pointing to.
1175 **
1176 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
1177 ** may have moved that cursor.
1178 */
1179 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
1180 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
1181 VdbeCoverageIf(v, opSeek==OP_NotExists);
1182 VdbeCoverageIf(v, opSeek==OP_NotFound);
1183 testcase( iIdxNoSeek>=0 );
1184 iIdxNoSeek = -1;
1185 }
1186
1187 /* Do FK processing. This call checks that any FK constraints that
1188 ** refer to this table (i.e. constraints attached to other tables)
1189 ** are not violated by deleting this row. */
1190 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
1191 }
1192
1193 /* Delete the index and table entries. Skip this step if pTab is really
1194 ** a view (in which case the only effect of the DELETE statement is to
1195 ** fire the INSTEAD OF triggers).
1196 **
1197 ** If variable 'count' is non-zero, then this OP_Delete instruction should
1198 ** invoke the update-hook. The pre-update-hook, on the other hand should
1199 ** be invoked unless table pTab is a system table. The difference is that
1200 ** the update-hook is not invoked for rows removed by REPLACE, but the
1201 ** pre-update-hook is.
1202 */
1203 if( pTab->pSelect==0 ){
1204 u8 p5 = 0;
1205 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
1206 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
1207 if( pParse->nested==0 ){
1208 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
1209 }
1210 if( eMode!=ONEPASS_OFF ){
1211 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
1212 }
1213 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
1214 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
1215 }
1216 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
1217 sqlite3VdbeChangeP5(v, p5);
1218 }
1219
1220 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
1221 ** handle rows (possibly in other tables) that refer via a foreign key
1222 ** to the row just deleted. */
1223 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
1224
1225 /* Invoke AFTER DELETE trigger programs. */
1226 sqlite3CodeRowTrigger(pParse, pTrigger,
1227 TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
1228 );
1229
1230 /* Jump here if the row had already been deleted before any BEFORE
1231 ** trigger programs were invoked. Or if a trigger program throws a
1232 ** RAISE(IGNORE) exception. */
1233 sqlite3VdbeResolveLabel(v, iLabel);
1234 VdbeModuleComment((v, "END: GenRowDel()"));
1235 }
1236
1237 /*
1238 ** This routine generates VDBE code that causes the deletion of all
1239 ** index entries associated with a single row of a single table, pTab
1240 **
1241 ** Preconditions:
1242 **
1243 ** 1. A read/write cursor "iDataCur" must be open on the canonical storage
1244 ** btree for the table pTab. (This will be either the table itself
1245 ** for rowid tables or to the primary key index for WITHOUT ROWID
1246 ** tables.)
1247 **
1248 ** 2. Read/write cursors for all indices of pTab must be open as
1249 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
1250 ** index is the 0-th index.)
1251 **
1252 ** 3. The "iDataCur" cursor must be already be positioned on the row
1253 ** that is to be deleted.
1254 */
1255 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
1256 Parse *pParse, /* Parsing and code generating context */
1257 Table *pTab, /* Table containing the row to be deleted */
1258 int iDataCur, /* Cursor of table holding data. */
1259 int iIdxCur, /* First index cursor */
1260 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
1261 int iIdxNoSeek /* Do not delete from this cursor */
1262 ){
1263 int i; /* Index loop counter */
1264 int r1 = -1; /* Register holding an index key */
1265 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
1266 Index *pIdx; /* Current index */
1267 Index *pPrior = 0; /* Prior index */
1268 Vdbe *v; /* The prepared statement under construction */
1269 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
1270
1271 v = pParse->pVdbe;
1272 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
1273 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
1274 assert( iIdxCur+i!=iDataCur || pPk==pIdx );
1275 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
1276 if( pIdx==pPk ) continue;
1277 if( iIdxCur+i==iIdxNoSeek ) continue;
1278 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
1279 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
1280 &iPartIdxLabel, pPrior, r1);
1281 sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
1282 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
1283 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
1284 pPrior = pIdx;
1285 }
1286 }
1287
1288 /*
1289 ** Generate code that will assemble an index key and stores it in register
1290 ** regOut. The key with be for index pIdx which is an index on pTab.
1291 ** iCur is the index of a cursor open on the pTab table and pointing to
1292 ** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
1293 ** iCur must be the cursor of the PRIMARY KEY index.
1294 **
1295 ** Return a register number which is the first in a block of
1296 ** registers that holds the elements of the index key. The
1297 ** block of registers has already been deallocated by the time
1298 ** this routine returns.
1299 **
1300 ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
1301 ** to that label if pIdx is a partial index that should be skipped.
1302 ** The label should be resolved using sqlite3ResolvePartIdxLabel().
1303 ** A partial index should be skipped if its WHERE clause evaluates
1304 ** to false or null. If pIdx is not a partial index, *piPartIdxLabel
1305 ** will be set to zero which is an empty label that is ignored by
1306 ** sqlite3ResolvePartIdxLabel().
1307 **
1308 ** The pPrior and regPrior parameters are used to implement a cache to
1309 ** avoid unnecessary register loads. If pPrior is not NULL, then it is
1310 ** a pointer to a different index for which an index key has just been
1311 ** computed into register regPrior. If the current pIdx index is generating
1312 ** its key into the same sequence of registers and if pPrior and pIdx share
1313 ** a column in common, then the register corresponding to that column already
1314 ** holds the correct value and the loading of that register is skipped.
1315 ** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
1316 ** on a table with multiple indices, and especially with the ROWID or
1317 ** PRIMARY KEY columns of the index.
1318 */
1319 SQLITE_PRIVATE int sqlite3GenerateIndexKey(
1320 Parse *pParse, /* Parsing context */
1321 Index *pIdx, /* The index for which to generate a key */
1322 int iDataCur, /* Cursor number from which to take column data */
1323 int regOut, /* Put the new key into this register if not 0 */
1324 int prefixOnly, /* Compute only a unique prefix of the key */
1325 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
1326 Index *pPrior, /* Previously generated index key */
1327 int regPrior /* Register holding previous generated key */
1328 ){
1329 Vdbe *v = pParse->pVdbe;
1330 int j;
1331 int regBase;
1332 int nCol;
1333
1334 if( piPartIdxLabel ){
1335 if( pIdx->pPartIdxWhere ){
1336 *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
1337 pParse->iSelfTab = iDataCur;
1338 sqlite3ExprCachePush(pParse);
1339 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
1340 SQLITE_JUMPIFNULL);
1341 }else{
1342 *piPartIdxLabel = 0;
1343 }
1344 }
1345 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
1346 regBase = sqlite3GetTempRange(pParse, nCol);
1347 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
1348 for(j=0; j<nCol; j++){
1349 if( pPrior
1350 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
1351 && pPrior->aiColumn[j]!=XN_EXPR
1352 ){
1353 /* This column was already computed by the previous index */
1354 continue;
1355 }
1356 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
1357 /* If the column affinity is REAL but the number is an integer, then it
1358 ** might be stored in the table as an integer (using a compact
1359 ** representation) then converted to REAL by an OP_RealAffinity opcode.
1360 ** But we are getting ready to store this value back into an index, where
1361 ** it should be converted by to INTEGER again. So omit the OP_RealAffinity
1362 ** opcode if it is present */
1363 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
1364 }
1365 if( regOut ){
1366 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
1367 if( pIdx->pTable->pSelect ){
1368 const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
1369 sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
1370 }
1371 }
1372 sqlite3ReleaseTempRange(pParse, regBase, nCol);
1373 return regBase;
1374 }
1375
1376 /*
1377 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
1378 ** because it was a partial index, then this routine should be called to
1379 ** resolve that label.
1380 */
1381 SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
1382 if( iLabel ){
1383 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
1384 sqlite3ExprCachePop(pParse);
1385 }
1386 }
1387
1388 /************** End of delete.c **********************************************/
1389 /************** Begin file func.c ********************************************/
1390 /*
1391 ** 2002 February 23
1392 **
1393 ** The author disclaims copyright to this source code. In place of
1394 ** a legal notice, here is a blessing:
1395 **
1396 ** May you do good and not evil.
1397 ** May you find forgiveness for yourself and forgive others.
1398 ** May you share freely, never taking more than you give.
1399 **
1400 *************************************************************************
1401 ** This file contains the C-language implementations for many of the SQL
1402 ** functions of SQLite. (Some function, and in particular the date and
1403 ** time functions, are implemented separately.)
1404 */
1405 /* #include "sqliteInt.h" */
1406 /* #include <stdlib.h> */
1407 /* #include <assert.h> */
1408 /* #include "vdbeInt.h" */
1409
1410 /*
1411 ** Return the collating function associated with a function.
1412 */
1413 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
1414 VdbeOp *pOp;
1415 assert( context->pVdbe!=0 );
1416 pOp = &context->pVdbe->aOp[context->iOp-1];
1417 assert( pOp->opcode==OP_CollSeq );
1418 assert( pOp->p4type==P4_COLLSEQ );
1419 return pOp->p4.pColl;
1420 }
1421
1422 /*
1423 ** Indicate that the accumulator load should be skipped on this
1424 ** iteration of the aggregate loop.
1425 */
1426 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
1427 context->skipFlag = 1;
1428 }
1429
1430 /*
1431 ** Implementation of the non-aggregate min() and max() functions
1432 */
1433 static void minmaxFunc(
1434 sqlite3_context *context,
1435 int argc,
1436 sqlite3_value **argv
1437 ){
1438 int i;
1439 int mask; /* 0 for min() or 0xffffffff for max() */
1440 int iBest;
1441 CollSeq *pColl;
1442
1443 assert( argc>1 );
1444 mask = sqlite3_user_data(context)==0 ? 0 : -1;
1445 pColl = sqlite3GetFuncCollSeq(context);
1446 assert( pColl );
1447 assert( mask==-1 || mask==0 );
1448 iBest = 0;
1449 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1450 for(i=1; i<argc; i++){
1451 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
1452 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
1453 testcase( mask==0 );
1454 iBest = i;
1455 }
1456 }
1457 sqlite3_result_value(context, argv[iBest]);
1458 }
1459
1460 /*
1461 ** Return the type of the argument.
1462 */
1463 static void typeofFunc(
1464 sqlite3_context *context,
1465 int NotUsed,
1466 sqlite3_value **argv
1467 ){
1468 const char *z = 0;
1469 UNUSED_PARAMETER(NotUsed);
1470 switch( sqlite3_value_type(argv[0]) ){
1471 case SQLITE_INTEGER: z = "integer"; break;
1472 case SQLITE_TEXT: z = "text"; break;
1473 case SQLITE_FLOAT: z = "real"; break;
1474 case SQLITE_BLOB: z = "blob"; break;
1475 default: z = "null"; break;
1476 }
1477 sqlite3_result_text(context, z, -1, SQLITE_STATIC);
1478 }
1479
1480
1481 /*
1482 ** Implementation of the length() function
1483 */
1484 static void lengthFunc(
1485 sqlite3_context *context,
1486 int argc,
1487 sqlite3_value **argv
1488 ){
1489 int len;
1490
1491 assert( argc==1 );
1492 UNUSED_PARAMETER(argc);
1493 switch( sqlite3_value_type(argv[0]) ){
1494 case SQLITE_BLOB:
1495 case SQLITE_INTEGER:
1496 case SQLITE_FLOAT: {
1497 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
1498 break;
1499 }
1500 case SQLITE_TEXT: {
1501 const unsigned char *z = sqlite3_value_text(argv[0]);
1502 if( z==0 ) return;
1503 len = 0;
1504 while( *z ){
1505 len++;
1506 SQLITE_SKIP_UTF8(z);
1507 }
1508 sqlite3_result_int(context, len);
1509 break;
1510 }
1511 default: {
1512 sqlite3_result_null(context);
1513 break;
1514 }
1515 }
1516 }
1517
1518 /*
1519 ** Implementation of the abs() function.
1520 **
1521 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
1522 ** the numeric argument X.
1523 */
1524 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1525 assert( argc==1 );
1526 UNUSED_PARAMETER(argc);
1527 switch( sqlite3_value_type(argv[0]) ){
1528 case SQLITE_INTEGER: {
1529 i64 iVal = sqlite3_value_int64(argv[0]);
1530 if( iVal<0 ){
1531 if( iVal==SMALLEST_INT64 ){
1532 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
1533 ** then abs(X) throws an integer overflow error since there is no
1534 ** equivalent positive 64-bit two complement value. */
1535 sqlite3_result_error(context, "integer overflow", -1);
1536 return;
1537 }
1538 iVal = -iVal;
1539 }
1540 sqlite3_result_int64(context, iVal);
1541 break;
1542 }
1543 case SQLITE_NULL: {
1544 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
1545 sqlite3_result_null(context);
1546 break;
1547 }
1548 default: {
1549 /* Because sqlite3_value_double() returns 0.0 if the argument is not
1550 ** something that can be converted into a number, we have:
1551 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
1552 ** that cannot be converted to a numeric value.
1553 */
1554 double rVal = sqlite3_value_double(argv[0]);
1555 if( rVal<0 ) rVal = -rVal;
1556 sqlite3_result_double(context, rVal);
1557 break;
1558 }
1559 }
1560 }
1561
1562 /*
1563 ** Implementation of the instr() function.
1564 **
1565 ** instr(haystack,needle) finds the first occurrence of needle
1566 ** in haystack and returns the number of previous characters plus 1,
1567 ** or 0 if needle does not occur within haystack.
1568 **
1569 ** If both haystack and needle are BLOBs, then the result is one more than
1570 ** the number of bytes in haystack prior to the first occurrence of needle,
1571 ** or 0 if needle never occurs in haystack.
1572 */
1573 static void instrFunc(
1574 sqlite3_context *context,
1575 int argc,
1576 sqlite3_value **argv
1577 ){
1578 const unsigned char *zHaystack;
1579 const unsigned char *zNeedle;
1580 int nHaystack;
1581 int nNeedle;
1582 int typeHaystack, typeNeedle;
1583 int N = 1;
1584 int isText;
1585
1586 UNUSED_PARAMETER(argc);
1587 typeHaystack = sqlite3_value_type(argv[0]);
1588 typeNeedle = sqlite3_value_type(argv[1]);
1589 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
1590 nHaystack = sqlite3_value_bytes(argv[0]);
1591 nNeedle = sqlite3_value_bytes(argv[1]);
1592 if( nNeedle>0 ){
1593 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
1594 zHaystack = sqlite3_value_blob(argv[0]);
1595 zNeedle = sqlite3_value_blob(argv[1]);
1596 assert( zNeedle!=0 );
1597 assert( zHaystack!=0 || nHaystack==0 );
1598 isText = 0;
1599 }else{
1600 zHaystack = sqlite3_value_text(argv[0]);
1601 zNeedle = sqlite3_value_text(argv[1]);
1602 isText = 1;
1603 if( zHaystack==0 || zNeedle==0 ) return;
1604 }
1605 while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
1606 N++;
1607 do{
1608 nHaystack--;
1609 zHaystack++;
1610 }while( isText && (zHaystack[0]&0xc0)==0x80 );
1611 }
1612 if( nNeedle>nHaystack ) N = 0;
1613 }
1614 sqlite3_result_int(context, N);
1615 }
1616
1617 /*
1618 ** Implementation of the printf() function.
1619 */
1620 static void printfFunc(
1621 sqlite3_context *context,
1622 int argc,
1623 sqlite3_value **argv
1624 ){
1625 PrintfArguments x;
1626 StrAccum str;
1627 const char *zFormat;
1628 int n;
1629 sqlite3 *db = sqlite3_context_db_handle(context);
1630
1631 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
1632 x.nArg = argc-1;
1633 x.nUsed = 0;
1634 x.apArg = argv+1;
1635 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
1636 str.printfFlags = SQLITE_PRINTF_SQLFUNC;
1637 sqlite3XPrintf(&str, zFormat, &x);
1638 n = str.nChar;
1639 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
1640 SQLITE_DYNAMIC);
1641 }
1642 }
1643
1644 /*
1645 ** Implementation of the substr() function.
1646 **
1647 ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
1648 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
1649 ** of x. If x is text, then we actually count UTF-8 characters.
1650 ** If x is a blob, then we count bytes.
1651 **
1652 ** If p1 is negative, then we begin abs(p1) from the end of x[].
1653 **
1654 ** If p2 is negative, return the p2 characters preceding p1.
1655 */
1656 static void substrFunc(
1657 sqlite3_context *context,
1658 int argc,
1659 sqlite3_value **argv
1660 ){
1661 const unsigned char *z;
1662 const unsigned char *z2;
1663 int len;
1664 int p0type;
1665 i64 p1, p2;
1666 int negP2 = 0;
1667
1668 assert( argc==3 || argc==2 );
1669 if( sqlite3_value_type(argv[1])==SQLITE_NULL
1670 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
1671 ){
1672 return;
1673 }
1674 p0type = sqlite3_value_type(argv[0]);
1675 p1 = sqlite3_value_int(argv[1]);
1676 if( p0type==SQLITE_BLOB ){
1677 len = sqlite3_value_bytes(argv[0]);
1678 z = sqlite3_value_blob(argv[0]);
1679 if( z==0 ) return;
1680 assert( len==sqlite3_value_bytes(argv[0]) );
1681 }else{
1682 z = sqlite3_value_text(argv[0]);
1683 if( z==0 ) return;
1684 len = 0;
1685 if( p1<0 ){
1686 for(z2=z; *z2; len++){
1687 SQLITE_SKIP_UTF8(z2);
1688 }
1689 }
1690 }
1691 #ifdef SQLITE_SUBSTR_COMPATIBILITY
1692 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
1693 ** as substr(X,1,N) - it returns the first N characters of X. This
1694 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
1695 ** from 2009-02-02 for compatibility of applications that exploited the
1696 ** old buggy behavior. */
1697 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
1698 #endif
1699 if( argc==3 ){
1700 p2 = sqlite3_value_int(argv[2]);
1701 if( p2<0 ){
1702 p2 = -p2;
1703 negP2 = 1;
1704 }
1705 }else{
1706 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
1707 }
1708 if( p1<0 ){
1709 p1 += len;
1710 if( p1<0 ){
1711 p2 += p1;
1712 if( p2<0 ) p2 = 0;
1713 p1 = 0;
1714 }
1715 }else if( p1>0 ){
1716 p1--;
1717 }else if( p2>0 ){
1718 p2--;
1719 }
1720 if( negP2 ){
1721 p1 -= p2;
1722 if( p1<0 ){
1723 p2 += p1;
1724 p1 = 0;
1725 }
1726 }
1727 assert( p1>=0 && p2>=0 );
1728 if( p0type!=SQLITE_BLOB ){
1729 while( *z && p1 ){
1730 SQLITE_SKIP_UTF8(z);
1731 p1--;
1732 }
1733 for(z2=z; *z2 && p2; p2--){
1734 SQLITE_SKIP_UTF8(z2);
1735 }
1736 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
1737 SQLITE_UTF8);
1738 }else{
1739 if( p1+p2>len ){
1740 p2 = len-p1;
1741 if( p2<0 ) p2 = 0;
1742 }
1743 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
1744 }
1745 }
1746
1747 /*
1748 ** Implementation of the round() function
1749 */
1750 #ifndef SQLITE_OMIT_FLOATING_POINT
1751 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1752 int n = 0;
1753 double r;
1754 char *zBuf;
1755 assert( argc==1 || argc==2 );
1756 if( argc==2 ){
1757 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
1758 n = sqlite3_value_int(argv[1]);
1759 if( n>30 ) n = 30;
1760 if( n<0 ) n = 0;
1761 }
1762 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
1763 r = sqlite3_value_double(argv[0]);
1764 /* If Y==0 and X will fit in a 64-bit int,
1765 ** handle the rounding directly,
1766 ** otherwise use printf.
1767 */
1768 if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
1769 r = (double)((sqlite_int64)(r+0.5));
1770 }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
1771 r = -(double)((sqlite_int64)((-r)+0.5));
1772 }else{
1773 zBuf = sqlite3_mprintf("%.*f",n,r);
1774 if( zBuf==0 ){
1775 sqlite3_result_error_nomem(context);
1776 return;
1777 }
1778 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
1779 sqlite3_free(zBuf);
1780 }
1781 sqlite3_result_double(context, r);
1782 }
1783 #endif
1784
1785 /*
1786 ** Allocate nByte bytes of space using sqlite3Malloc(). If the
1787 ** allocation fails, call sqlite3_result_error_nomem() to notify
1788 ** the database handle that malloc() has failed and return NULL.
1789 ** If nByte is larger than the maximum string or blob length, then
1790 ** raise an SQLITE_TOOBIG exception and return NULL.
1791 */
1792 static void *contextMalloc(sqlite3_context *context, i64 nByte){
1793 char *z;
1794 sqlite3 *db = sqlite3_context_db_handle(context);
1795 assert( nByte>0 );
1796 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
1797 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
1798 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1799 sqlite3_result_error_toobig(context);
1800 z = 0;
1801 }else{
1802 z = sqlite3Malloc(nByte);
1803 if( !z ){
1804 sqlite3_result_error_nomem(context);
1805 }
1806 }
1807 return z;
1808 }
1809
1810 /*
1811 ** Implementation of the upper() and lower() SQL functions.
1812 */
1813 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1814 char *z1;
1815 const char *z2;
1816 int i, n;
1817 UNUSED_PARAMETER(argc);
1818 z2 = (char*)sqlite3_value_text(argv[0]);
1819 n = sqlite3_value_bytes(argv[0]);
1820 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
1821 assert( z2==(char*)sqlite3_value_text(argv[0]) );
1822 if( z2 ){
1823 z1 = contextMalloc(context, ((i64)n)+1);
1824 if( z1 ){
1825 for(i=0; i<n; i++){
1826 z1[i] = (char)sqlite3Toupper(z2[i]);
1827 }
1828 sqlite3_result_text(context, z1, n, sqlite3_free);
1829 }
1830 }
1831 }
1832 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1833 char *z1;
1834 const char *z2;
1835 int i, n;
1836 UNUSED_PARAMETER(argc);
1837 z2 = (char*)sqlite3_value_text(argv[0]);
1838 n = sqlite3_value_bytes(argv[0]);
1839 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
1840 assert( z2==(char*)sqlite3_value_text(argv[0]) );
1841 if( z2 ){
1842 z1 = contextMalloc(context, ((i64)n)+1);
1843 if( z1 ){
1844 for(i=0; i<n; i++){
1845 z1[i] = sqlite3Tolower(z2[i]);
1846 }
1847 sqlite3_result_text(context, z1, n, sqlite3_free);
1848 }
1849 }
1850 }
1851
1852 /*
1853 ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
1854 ** as VDBE code so that unused argument values do not have to be computed.
1855 ** However, we still need some kind of function implementation for this
1856 ** routines in the function table. The noopFunc macro provides this.
1857 ** noopFunc will never be called so it doesn't matter what the implementation
1858 ** is. We might as well use the "version()" function as a substitute.
1859 */
1860 #define noopFunc versionFunc /* Substitute function - never called */
1861
1862 /*
1863 ** Implementation of random(). Return a random integer.
1864 */
1865 static void randomFunc(
1866 sqlite3_context *context,
1867 int NotUsed,
1868 sqlite3_value **NotUsed2
1869 ){
1870 sqlite_int64 r;
1871 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1872 sqlite3_randomness(sizeof(r), &r);
1873 if( r<0 ){
1874 /* We need to prevent a random number of 0x8000000000000000
1875 ** (or -9223372036854775808) since when you do abs() of that
1876 ** number of you get the same value back again. To do this
1877 ** in a way that is testable, mask the sign bit off of negative
1878 ** values, resulting in a positive value. Then take the
1879 ** 2s complement of that positive value. The end result can
1880 ** therefore be no less than -9223372036854775807.
1881 */
1882 r = -(r & LARGEST_INT64);
1883 }
1884 sqlite3_result_int64(context, r);
1885 }
1886
1887 /*
1888 ** Implementation of randomblob(N). Return a random blob
1889 ** that is N bytes long.
1890 */
1891 static void randomBlob(
1892 sqlite3_context *context,
1893 int argc,
1894 sqlite3_value **argv
1895 ){
1896 int n;
1897 unsigned char *p;
1898 assert( argc==1 );
1899 UNUSED_PARAMETER(argc);
1900 n = sqlite3_value_int(argv[0]);
1901 if( n<1 ){
1902 n = 1;
1903 }
1904 p = contextMalloc(context, n);
1905 if( p ){
1906 sqlite3_randomness(n, p);
1907 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
1908 }
1909 }
1910
1911 /*
1912 ** Implementation of the last_insert_rowid() SQL function. The return
1913 ** value is the same as the sqlite3_last_insert_rowid() API function.
1914 */
1915 static void last_insert_rowid(
1916 sqlite3_context *context,
1917 int NotUsed,
1918 sqlite3_value **NotUsed2
1919 ){
1920 sqlite3 *db = sqlite3_context_db_handle(context);
1921 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1922 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
1923 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
1924 ** function. */
1925 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
1926 }
1927
1928 /*
1929 ** Implementation of the changes() SQL function.
1930 **
1931 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
1932 ** around the sqlite3_changes() C/C++ function and hence follows the same
1933 ** rules for counting changes.
1934 */
1935 static void changes(
1936 sqlite3_context *context,
1937 int NotUsed,
1938 sqlite3_value **NotUsed2
1939 ){
1940 sqlite3 *db = sqlite3_context_db_handle(context);
1941 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1942 sqlite3_result_int(context, sqlite3_changes(db));
1943 }
1944
1945 /*
1946 ** Implementation of the total_changes() SQL function. The return value is
1947 ** the same as the sqlite3_total_changes() API function.
1948 */
1949 static void total_changes(
1950 sqlite3_context *context,
1951 int NotUsed,
1952 sqlite3_value **NotUsed2
1953 ){
1954 sqlite3 *db = sqlite3_context_db_handle(context);
1955 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1956 /* IMP: R-52756-41993 This function is a wrapper around the
1957 ** sqlite3_total_changes() C/C++ interface. */
1958 sqlite3_result_int(context, sqlite3_total_changes(db));
1959 }
1960
1961 /*
1962 ** A structure defining how to do GLOB-style comparisons.
1963 */
1964 struct compareInfo {
1965 u8 matchAll; /* "*" or "%" */
1966 u8 matchOne; /* "?" or "_" */
1967 u8 matchSet; /* "[" or 0 */
1968 u8 noCase; /* true to ignore case differences */
1969 };
1970
1971 /*
1972 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
1973 ** character is exactly one byte in size. Also, provde the Utf8Read()
1974 ** macro for fast reading of the next character in the common case where
1975 ** the next character is ASCII.
1976 */
1977 #if defined(SQLITE_EBCDIC)
1978 # define sqlite3Utf8Read(A) (*((*A)++))
1979 # define Utf8Read(A) (*(A++))
1980 #else
1981 # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
1982 #endif
1983
1984 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
1985 /* The correct SQL-92 behavior is for the LIKE operator to ignore
1986 ** case. Thus 'a' LIKE 'A' would be true. */
1987 static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
1988 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
1989 ** is case sensitive causing 'a' LIKE 'A' to be false */
1990 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
1991
1992 /*
1993 ** Possible error returns from patternMatch()
1994 */
1995 #define SQLITE_MATCH 0
1996 #define SQLITE_NOMATCH 1
1997 #define SQLITE_NOWILDCARDMATCH 2
1998
1999 /*
2000 ** Compare two UTF-8 strings for equality where the first string is
2001 ** a GLOB or LIKE expression. Return values:
2002 **
2003 ** SQLITE_MATCH: Match
2004 ** SQLITE_NOMATCH: No match
2005 ** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
2006 **
2007 ** Globbing rules:
2008 **
2009 ** '*' Matches any sequence of zero or more characters.
2010 **
2011 ** '?' Matches exactly one character.
2012 **
2013 ** [...] Matches one character from the enclosed list of
2014 ** characters.
2015 **
2016 ** [^...] Matches one character not in the enclosed list.
2017 **
2018 ** With the [...] and [^...] matching, a ']' character can be included
2019 ** in the list by making it the first character after '[' or '^'. A
2020 ** range of characters can be specified using '-'. Example:
2021 ** "[a-z]" matches any single lower-case letter. To match a '-', make
2022 ** it the last character in the list.
2023 **
2024 ** Like matching rules:
2025 **
2026 ** '%' Matches any sequence of zero or more characters
2027 **
2028 *** '_' Matches any one character
2029 **
2030 ** Ec Where E is the "esc" character and c is any other
2031 ** character, including '%', '_', and esc, match exactly c.
2032 **
2033 ** The comments within this routine usually assume glob matching.
2034 **
2035 ** This routine is usually quick, but can be N**2 in the worst case.
2036 */
2037 static int patternCompare(
2038 const u8 *zPattern, /* The glob pattern */
2039 const u8 *zString, /* The string to compare against the glob */
2040 const struct compareInfo *pInfo, /* Information about how to do the compare */
2041 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
2042 ){
2043 u32 c, c2; /* Next pattern and input string chars */
2044 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
2045 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
2046 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
2047 const u8 *zEscaped = 0; /* One past the last escaped input char */
2048
2049 while( (c = Utf8Read(zPattern))!=0 ){
2050 if( c==matchAll ){ /* Match "*" */
2051 /* Skip over multiple "*" characters in the pattern. If there
2052 ** are also "?" characters, skip those as well, but consume a
2053 ** single character of the input string for each "?" skipped */
2054 while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
2055 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
2056 return SQLITE_NOWILDCARDMATCH;
2057 }
2058 }
2059 if( c==0 ){
2060 return SQLITE_MATCH; /* "*" at the end of the pattern matches */
2061 }else if( c==matchOther ){
2062 if( pInfo->matchSet==0 ){
2063 c = sqlite3Utf8Read(&zPattern);
2064 if( c==0 ) return SQLITE_NOWILDCARDMATCH;
2065 }else{
2066 /* "[...]" immediately follows the "*". We have to do a slow
2067 ** recursive search in this case, but it is an unusual case. */
2068 assert( matchOther<0x80 ); /* '[' is a single-byte character */
2069 while( *zString ){
2070 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
2071 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
2072 SQLITE_SKIP_UTF8(zString);
2073 }
2074 return SQLITE_NOWILDCARDMATCH;
2075 }
2076 }
2077
2078 /* At this point variable c contains the first character of the
2079 ** pattern string past the "*". Search in the input string for the
2080 ** first matching character and recursively continue the match from
2081 ** that point.
2082 **
2083 ** For a case-insensitive search, set variable cx to be the same as
2084 ** c but in the other case and search the input string for either
2085 ** c or cx.
2086 */
2087 if( c<=0x80 ){
2088 u32 cx;
2089 int bMatch;
2090 if( noCase ){
2091 cx = sqlite3Toupper(c);
2092 c = sqlite3Tolower(c);
2093 }else{
2094 cx = c;
2095 }
2096 while( (c2 = *(zString++))!=0 ){
2097 if( c2!=c && c2!=cx ) continue;
2098 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
2099 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
2100 }
2101 }else{
2102 int bMatch;
2103 while( (c2 = Utf8Read(zString))!=0 ){
2104 if( c2!=c ) continue;
2105 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
2106 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
2107 }
2108 }
2109 return SQLITE_NOWILDCARDMATCH;
2110 }
2111 if( c==matchOther ){
2112 if( pInfo->matchSet==0 ){
2113 c = sqlite3Utf8Read(&zPattern);
2114 if( c==0 ) return SQLITE_NOMATCH;
2115 zEscaped = zPattern;
2116 }else{
2117 u32 prior_c = 0;
2118 int seen = 0;
2119 int invert = 0;
2120 c = sqlite3Utf8Read(&zString);
2121 if( c==0 ) return SQLITE_NOMATCH;
2122 c2 = sqlite3Utf8Read(&zPattern);
2123 if( c2=='^' ){
2124 invert = 1;
2125 c2 = sqlite3Utf8Read(&zPattern);
2126 }
2127 if( c2==']' ){
2128 if( c==']' ) seen = 1;
2129 c2 = sqlite3Utf8Read(&zPattern);
2130 }
2131 while( c2 && c2!=']' ){
2132 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
2133 c2 = sqlite3Utf8Read(&zPattern);
2134 if( c>=prior_c && c<=c2 ) seen = 1;
2135 prior_c = 0;
2136 }else{
2137 if( c==c2 ){
2138 seen = 1;
2139 }
2140 prior_c = c2;
2141 }
2142 c2 = sqlite3Utf8Read(&zPattern);
2143 }
2144 if( c2==0 || (seen ^ invert)==0 ){
2145 return SQLITE_NOMATCH;
2146 }
2147 continue;
2148 }
2149 }
2150 c2 = Utf8Read(zString);
2151 if( c==c2 ) continue;
2152 if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
2153 continue;
2154 }
2155 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
2156 return SQLITE_NOMATCH;
2157 }
2158 return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
2159 }
2160
2161 /*
2162 ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
2163 ** non-zero if there is no match.
2164 */
2165 SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
2166 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
2167 }
2168
2169 /*
2170 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
2171 ** a miss - like strcmp().
2172 */
2173 SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
2174 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
2175 }
2176
2177 /*
2178 ** Count the number of times that the LIKE operator (or GLOB which is
2179 ** just a variation of LIKE) gets called. This is used for testing
2180 ** only.
2181 */
2182 #ifdef SQLITE_TEST
2183 SQLITE_API int sqlite3_like_count = 0;
2184 #endif
2185
2186
2187 /*
2188 ** Implementation of the like() SQL function. This function implements
2189 ** the build-in LIKE operator. The first argument to the function is the
2190 ** pattern and the second argument is the string. So, the SQL statements:
2191 **
2192 ** A LIKE B
2193 **
2194 ** is implemented as like(B,A).
2195 **
2196 ** This same function (with a different compareInfo structure) computes
2197 ** the GLOB operator.
2198 */
2199 static void likeFunc(
2200 sqlite3_context *context,
2201 int argc,
2202 sqlite3_value **argv
2203 ){
2204 const unsigned char *zA, *zB;
2205 u32 escape;
2206 int nPat;
2207 sqlite3 *db = sqlite3_context_db_handle(context);
2208 struct compareInfo *pInfo = sqlite3_user_data(context);
2209
2210 #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
2211 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
2212 || sqlite3_value_type(argv[1])==SQLITE_BLOB
2213 ){
2214 #ifdef SQLITE_TEST
2215 sqlite3_like_count++;
2216 #endif
2217 sqlite3_result_int(context, 0);
2218 return;
2219 }
2220 #endif
2221 zB = sqlite3_value_text(argv[0]);
2222 zA = sqlite3_value_text(argv[1]);
2223
2224 /* Limit the length of the LIKE or GLOB pattern to avoid problems
2225 ** of deep recursion and N*N behavior in patternCompare().
2226 */
2227 nPat = sqlite3_value_bytes(argv[0]);
2228 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
2229 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
2230 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
2231 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
2232 return;
2233 }
2234 assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */
2235
2236 if( argc==3 ){
2237 /* The escape character string must consist of a single UTF-8 character.
2238 ** Otherwise, return an error.
2239 */
2240 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
2241 if( zEsc==0 ) return;
2242 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
2243 sqlite3_result_error(context,
2244 "ESCAPE expression must be a single character", -1);
2245 return;
2246 }
2247 escape = sqlite3Utf8Read(&zEsc);
2248 }else{
2249 escape = pInfo->matchSet;
2250 }
2251 if( zA && zB ){
2252 #ifdef SQLITE_TEST
2253 sqlite3_like_count++;
2254 #endif
2255 sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)==SQLITE_MA TCH);
2256 }
2257 }
2258
2259 /*
2260 ** Implementation of the NULLIF(x,y) function. The result is the first
2261 ** argument if the arguments are different. The result is NULL if the
2262 ** arguments are equal to each other.
2263 */
2264 static void nullifFunc(
2265 sqlite3_context *context,
2266 int NotUsed,
2267 sqlite3_value **argv
2268 ){
2269 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
2270 UNUSED_PARAMETER(NotUsed);
2271 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
2272 sqlite3_result_value(context, argv[0]);
2273 }
2274 }
2275
2276 /*
2277 ** Implementation of the sqlite_version() function. The result is the version
2278 ** of the SQLite library that is running.
2279 */
2280 static void versionFunc(
2281 sqlite3_context *context,
2282 int NotUsed,
2283 sqlite3_value **NotUsed2
2284 ){
2285 UNUSED_PARAMETER2(NotUsed, NotUsed2);
2286 /* IMP: R-48699-48617 This function is an SQL wrapper around the
2287 ** sqlite3_libversion() C-interface. */
2288 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
2289 }
2290
2291 /*
2292 ** Implementation of the sqlite_source_id() function. The result is a string
2293 ** that identifies the particular version of the source code used to build
2294 ** SQLite.
2295 */
2296 static void sourceidFunc(
2297 sqlite3_context *context,
2298 int NotUsed,
2299 sqlite3_value **NotUsed2
2300 ){
2301 UNUSED_PARAMETER2(NotUsed, NotUsed2);
2302 /* IMP: R-24470-31136 This function is an SQL wrapper around the
2303 ** sqlite3_sourceid() C interface. */
2304 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
2305 }
2306
2307 /*
2308 ** Implementation of the sqlite_log() function. This is a wrapper around
2309 ** sqlite3_log(). The return value is NULL. The function exists purely for
2310 ** its side-effects.
2311 */
2312 static void errlogFunc(
2313 sqlite3_context *context,
2314 int argc,
2315 sqlite3_value **argv
2316 ){
2317 UNUSED_PARAMETER(argc);
2318 UNUSED_PARAMETER(context);
2319 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
2320 }
2321
2322 /*
2323 ** Implementation of the sqlite_compileoption_used() function.
2324 ** The result is an integer that identifies if the compiler option
2325 ** was used to build SQLite.
2326 */
2327 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2328 static void compileoptionusedFunc(
2329 sqlite3_context *context,
2330 int argc,
2331 sqlite3_value **argv
2332 ){
2333 const char *zOptName;
2334 assert( argc==1 );
2335 UNUSED_PARAMETER(argc);
2336 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
2337 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
2338 ** function.
2339 */
2340 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
2341 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
2342 }
2343 }
2344 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2345
2346 /*
2347 ** Implementation of the sqlite_compileoption_get() function.
2348 ** The result is a string that identifies the compiler options
2349 ** used to build SQLite.
2350 */
2351 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2352 static void compileoptiongetFunc(
2353 sqlite3_context *context,
2354 int argc,
2355 sqlite3_value **argv
2356 ){
2357 int n;
2358 assert( argc==1 );
2359 UNUSED_PARAMETER(argc);
2360 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
2361 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
2362 */
2363 n = sqlite3_value_int(argv[0]);
2364 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
2365 }
2366 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2367
2368 /* Array for converting from half-bytes (nybbles) into ASCII hex
2369 ** digits. */
2370 static const char hexdigits[] = {
2371 '0', '1', '2', '3', '4', '5', '6', '7',
2372 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
2373 };
2374
2375 /*
2376 ** Implementation of the QUOTE() function. This function takes a single
2377 ** argument. If the argument is numeric, the return value is the same as
2378 ** the argument. If the argument is NULL, the return value is the string
2379 ** "NULL". Otherwise, the argument is enclosed in single quotes with
2380 ** single-quote escapes.
2381 */
2382 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
2383 assert( argc==1 );
2384 UNUSED_PARAMETER(argc);
2385 switch( sqlite3_value_type(argv[0]) ){
2386 case SQLITE_FLOAT: {
2387 double r1, r2;
2388 char zBuf[50];
2389 r1 = sqlite3_value_double(argv[0]);
2390 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
2391 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
2392 if( r1!=r2 ){
2393 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
2394 }
2395 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
2396 break;
2397 }
2398 case SQLITE_INTEGER: {
2399 sqlite3_result_value(context, argv[0]);
2400 break;
2401 }
2402 case SQLITE_BLOB: {
2403 char *zText = 0;
2404 char const *zBlob = sqlite3_value_blob(argv[0]);
2405 int nBlob = sqlite3_value_bytes(argv[0]);
2406 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
2407 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
2408 if( zText ){
2409 int i;
2410 for(i=0; i<nBlob; i++){
2411 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
2412 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
2413 }
2414 zText[(nBlob*2)+2] = '\'';
2415 zText[(nBlob*2)+3] = '\0';
2416 zText[0] = 'X';
2417 zText[1] = '\'';
2418 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
2419 sqlite3_free(zText);
2420 }
2421 break;
2422 }
2423 case SQLITE_TEXT: {
2424 int i,j;
2425 u64 n;
2426 const unsigned char *zArg = sqlite3_value_text(argv[0]);
2427 char *z;
2428
2429 if( zArg==0 ) return;
2430 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
2431 z = contextMalloc(context, ((i64)i)+((i64)n)+3);
2432 if( z ){
2433 z[0] = '\'';
2434 for(i=0, j=1; zArg[i]; i++){
2435 z[j++] = zArg[i];
2436 if( zArg[i]=='\'' ){
2437 z[j++] = '\'';
2438 }
2439 }
2440 z[j++] = '\'';
2441 z[j] = 0;
2442 sqlite3_result_text(context, z, j, sqlite3_free);
2443 }
2444 break;
2445 }
2446 default: {
2447 assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
2448 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
2449 break;
2450 }
2451 }
2452 }
2453
2454 /*
2455 ** The unicode() function. Return the integer unicode code-point value
2456 ** for the first character of the input string.
2457 */
2458 static void unicodeFunc(
2459 sqlite3_context *context,
2460 int argc,
2461 sqlite3_value **argv
2462 ){
2463 const unsigned char *z = sqlite3_value_text(argv[0]);
2464 (void)argc;
2465 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
2466 }
2467
2468 /*
2469 ** The char() function takes zero or more arguments, each of which is
2470 ** an integer. It constructs a string where each character of the string
2471 ** is the unicode character for the corresponding integer argument.
2472 */
2473 static void charFunc(
2474 sqlite3_context *context,
2475 int argc,
2476 sqlite3_value **argv
2477 ){
2478 unsigned char *z, *zOut;
2479 int i;
2480 zOut = z = sqlite3_malloc64( argc*4+1 );
2481 if( z==0 ){
2482 sqlite3_result_error_nomem(context);
2483 return;
2484 }
2485 for(i=0; i<argc; i++){
2486 sqlite3_int64 x;
2487 unsigned c;
2488 x = sqlite3_value_int64(argv[i]);
2489 if( x<0 || x>0x10ffff ) x = 0xfffd;
2490 c = (unsigned)(x & 0x1fffff);
2491 if( c<0x00080 ){
2492 *zOut++ = (u8)(c&0xFF);
2493 }else if( c<0x00800 ){
2494 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
2495 *zOut++ = 0x80 + (u8)(c & 0x3F);
2496 }else if( c<0x10000 ){
2497 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
2498 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
2499 *zOut++ = 0x80 + (u8)(c & 0x3F);
2500 }else{
2501 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
2502 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
2503 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
2504 *zOut++ = 0x80 + (u8)(c & 0x3F);
2505 } \
2506 }
2507 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
2508 }
2509
2510 /*
2511 ** The hex() function. Interpret the argument as a blob. Return
2512 ** a hexadecimal rendering as text.
2513 */
2514 static void hexFunc(
2515 sqlite3_context *context,
2516 int argc,
2517 sqlite3_value **argv
2518 ){
2519 int i, n;
2520 const unsigned char *pBlob;
2521 char *zHex, *z;
2522 assert( argc==1 );
2523 UNUSED_PARAMETER(argc);
2524 pBlob = sqlite3_value_blob(argv[0]);
2525 n = sqlite3_value_bytes(argv[0]);
2526 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
2527 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
2528 if( zHex ){
2529 for(i=0; i<n; i++, pBlob++){
2530 unsigned char c = *pBlob;
2531 *(z++) = hexdigits[(c>>4)&0xf];
2532 *(z++) = hexdigits[c&0xf];
2533 }
2534 *z = 0;
2535 sqlite3_result_text(context, zHex, n*2, sqlite3_free);
2536 }
2537 }
2538
2539 /*
2540 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
2541 */
2542 static void zeroblobFunc(
2543 sqlite3_context *context,
2544 int argc,
2545 sqlite3_value **argv
2546 ){
2547 i64 n;
2548 int rc;
2549 assert( argc==1 );
2550 UNUSED_PARAMETER(argc);
2551 n = sqlite3_value_int64(argv[0]);
2552 if( n<0 ) n = 0;
2553 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
2554 if( rc ){
2555 sqlite3_result_error_code(context, rc);
2556 }
2557 }
2558
2559 /*
2560 ** The replace() function. Three arguments are all strings: call
2561 ** them A, B, and C. The result is also a string which is derived
2562 ** from A by replacing every occurrence of B with C. The match
2563 ** must be exact. Collating sequences are not used.
2564 */
2565 static void replaceFunc(
2566 sqlite3_context *context,
2567 int argc,
2568 sqlite3_value **argv
2569 ){
2570 const unsigned char *zStr; /* The input string A */
2571 const unsigned char *zPattern; /* The pattern string B */
2572 const unsigned char *zRep; /* The replacement string C */
2573 unsigned char *zOut; /* The output */
2574 int nStr; /* Size of zStr */
2575 int nPattern; /* Size of zPattern */
2576 int nRep; /* Size of zRep */
2577 i64 nOut; /* Maximum size of zOut */
2578 int loopLimit; /* Last zStr[] that might match zPattern[] */
2579 int i, j; /* Loop counters */
2580
2581 assert( argc==3 );
2582 UNUSED_PARAMETER(argc);
2583 zStr = sqlite3_value_text(argv[0]);
2584 if( zStr==0 ) return;
2585 nStr = sqlite3_value_bytes(argv[0]);
2586 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
2587 zPattern = sqlite3_value_text(argv[1]);
2588 if( zPattern==0 ){
2589 assert( sqlite3_value_type(argv[1])==SQLITE_NULL
2590 || sqlite3_context_db_handle(context)->mallocFailed );
2591 return;
2592 }
2593 if( zPattern[0]==0 ){
2594 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
2595 sqlite3_result_value(context, argv[0]);
2596 return;
2597 }
2598 nPattern = sqlite3_value_bytes(argv[1]);
2599 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
2600 zRep = sqlite3_value_text(argv[2]);
2601 if( zRep==0 ) return;
2602 nRep = sqlite3_value_bytes(argv[2]);
2603 assert( zRep==sqlite3_value_text(argv[2]) );
2604 nOut = nStr + 1;
2605 assert( nOut<SQLITE_MAX_LENGTH );
2606 zOut = contextMalloc(context, (i64)nOut);
2607 if( zOut==0 ){
2608 return;
2609 }
2610 loopLimit = nStr - nPattern;
2611 for(i=j=0; i<=loopLimit; i++){
2612 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
2613 zOut[j++] = zStr[i];
2614 }else{
2615 u8 *zOld;
2616 sqlite3 *db = sqlite3_context_db_handle(context);
2617 nOut += nRep - nPattern;
2618 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
2619 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
2620 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
2621 sqlite3_result_error_toobig(context);
2622 sqlite3_free(zOut);
2623 return;
2624 }
2625 zOld = zOut;
2626 zOut = sqlite3_realloc64(zOut, (int)nOut);
2627 if( zOut==0 ){
2628 sqlite3_result_error_nomem(context);
2629 sqlite3_free(zOld);
2630 return;
2631 }
2632 memcpy(&zOut[j], zRep, nRep);
2633 j += nRep;
2634 i += nPattern-1;
2635 }
2636 }
2637 assert( j+nStr-i+1==nOut );
2638 memcpy(&zOut[j], &zStr[i], nStr-i);
2639 j += nStr - i;
2640 assert( j<=nOut );
2641 zOut[j] = 0;
2642 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
2643 }
2644
2645 /*
2646 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
2647 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
2648 */
2649 static void trimFunc(
2650 sqlite3_context *context,
2651 int argc,
2652 sqlite3_value **argv
2653 ){
2654 const unsigned char *zIn; /* Input string */
2655 const unsigned char *zCharSet; /* Set of characters to trim */
2656 int nIn; /* Number of bytes in input */
2657 int flags; /* 1: trimleft 2: trimright 3: trim */
2658 int i; /* Loop counter */
2659 unsigned char *aLen = 0; /* Length of each character in zCharSet */
2660 unsigned char **azChar = 0; /* Individual characters in zCharSet */
2661 int nChar; /* Number of characters in zCharSet */
2662
2663 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
2664 return;
2665 }
2666 zIn = sqlite3_value_text(argv[0]);
2667 if( zIn==0 ) return;
2668 nIn = sqlite3_value_bytes(argv[0]);
2669 assert( zIn==sqlite3_value_text(argv[0]) );
2670 if( argc==1 ){
2671 static const unsigned char lenOne[] = { 1 };
2672 static unsigned char * const azOne[] = { (u8*)" " };
2673 nChar = 1;
2674 aLen = (u8*)lenOne;
2675 azChar = (unsigned char **)azOne;
2676 zCharSet = 0;
2677 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
2678 return;
2679 }else{
2680 const unsigned char *z;
2681 for(z=zCharSet, nChar=0; *z; nChar++){
2682 SQLITE_SKIP_UTF8(z);
2683 }
2684 if( nChar>0 ){
2685 azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
2686 if( azChar==0 ){
2687 return;
2688 }
2689 aLen = (unsigned char*)&azChar[nChar];
2690 for(z=zCharSet, nChar=0; *z; nChar++){
2691 azChar[nChar] = (unsigned char *)z;
2692 SQLITE_SKIP_UTF8(z);
2693 aLen[nChar] = (u8)(z - azChar[nChar]);
2694 }
2695 }
2696 }
2697 if( nChar>0 ){
2698 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
2699 if( flags & 1 ){
2700 while( nIn>0 ){
2701 int len = 0;
2702 for(i=0; i<nChar; i++){
2703 len = aLen[i];
2704 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
2705 }
2706 if( i>=nChar ) break;
2707 zIn += len;
2708 nIn -= len;
2709 }
2710 }
2711 if( flags & 2 ){
2712 while( nIn>0 ){
2713 int len = 0;
2714 for(i=0; i<nChar; i++){
2715 len = aLen[i];
2716 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
2717 }
2718 if( i>=nChar ) break;
2719 nIn -= len;
2720 }
2721 }
2722 if( zCharSet ){
2723 sqlite3_free(azChar);
2724 }
2725 }
2726 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
2727 }
2728
2729
2730 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
2731 /*
2732 ** The "unknown" function is automatically substituted in place of
2733 ** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
2734 ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
2735 ** When the "sqlite3" command-line shell is built using this functionality,
2736 ** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
2737 ** involving application-defined functions to be examined in a generic
2738 ** sqlite3 shell.
2739 */
2740 static void unknownFunc(
2741 sqlite3_context *context,
2742 int argc,
2743 sqlite3_value **argv
2744 ){
2745 /* no-op */
2746 }
2747 #endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
2748
2749
2750 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
2751 ** is only available if the SQLITE_SOUNDEX compile-time option is used
2752 ** when SQLite is built.
2753 */
2754 #ifdef SQLITE_SOUNDEX
2755 /*
2756 ** Compute the soundex encoding of a word.
2757 **
2758 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
2759 ** soundex encoding of the string X.
2760 */
2761 static void soundexFunc(
2762 sqlite3_context *context,
2763 int argc,
2764 sqlite3_value **argv
2765 ){
2766 char zResult[8];
2767 const u8 *zIn;
2768 int i, j;
2769 static const unsigned char iCode[] = {
2770 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2771 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2772 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2773 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2774 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
2775 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
2776 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
2777 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
2778 };
2779 assert( argc==1 );
2780 zIn = (u8*)sqlite3_value_text(argv[0]);
2781 if( zIn==0 ) zIn = (u8*)"";
2782 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
2783 if( zIn[i] ){
2784 u8 prevcode = iCode[zIn[i]&0x7f];
2785 zResult[0] = sqlite3Toupper(zIn[i]);
2786 for(j=1; j<4 && zIn[i]; i++){
2787 int code = iCode[zIn[i]&0x7f];
2788 if( code>0 ){
2789 if( code!=prevcode ){
2790 prevcode = code;
2791 zResult[j++] = code + '0';
2792 }
2793 }else{
2794 prevcode = 0;
2795 }
2796 }
2797 while( j<4 ){
2798 zResult[j++] = '0';
2799 }
2800 zResult[j] = 0;
2801 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
2802 }else{
2803 /* IMP: R-64894-50321 The string "?000" is returned if the argument
2804 ** is NULL or contains no ASCII alphabetic characters. */
2805 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
2806 }
2807 }
2808 #endif /* SQLITE_SOUNDEX */
2809
2810 #ifndef SQLITE_OMIT_LOAD_EXTENSION
2811 /*
2812 ** A function that loads a shared-library extension then returns NULL.
2813 */
2814 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
2815 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
2816 const char *zProc;
2817 sqlite3 *db = sqlite3_context_db_handle(context);
2818 char *zErrMsg = 0;
2819
2820 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
2821 ** flag is set. See the sqlite3_enable_load_extension() API.
2822 */
2823 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
2824 sqlite3_result_error(context, "not authorized", -1);
2825 return;
2826 }
2827
2828 if( argc==2 ){
2829 zProc = (const char *)sqlite3_value_text(argv[1]);
2830 }else{
2831 zProc = 0;
2832 }
2833 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
2834 sqlite3_result_error(context, zErrMsg, -1);
2835 sqlite3_free(zErrMsg);
2836 }
2837 }
2838 #endif
2839
2840
2841 /*
2842 ** An instance of the following structure holds the context of a
2843 ** sum() or avg() aggregate computation.
2844 */
2845 typedef struct SumCtx SumCtx;
2846 struct SumCtx {
2847 double rSum; /* Floating point sum */
2848 i64 iSum; /* Integer sum */
2849 i64 cnt; /* Number of elements summed */
2850 u8 overflow; /* True if integer overflow seen */
2851 u8 approx; /* True if non-integer value was input to the sum */
2852 };
2853
2854 /*
2855 ** Routines used to compute the sum, average, and total.
2856 **
2857 ** The SUM() function follows the (broken) SQL standard which means
2858 ** that it returns NULL if it sums over no inputs. TOTAL returns
2859 ** 0.0 in that case. In addition, TOTAL always returns a float where
2860 ** SUM might return an integer if it never encounters a floating point
2861 ** value. TOTAL never fails, but SUM might through an exception if
2862 ** it overflows an integer.
2863 */
2864 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
2865 SumCtx *p;
2866 int type;
2867 assert( argc==1 );
2868 UNUSED_PARAMETER(argc);
2869 p = sqlite3_aggregate_context(context, sizeof(*p));
2870 type = sqlite3_value_numeric_type(argv[0]);
2871 if( p && type!=SQLITE_NULL ){
2872 p->cnt++;
2873 if( type==SQLITE_INTEGER ){
2874 i64 v = sqlite3_value_int64(argv[0]);
2875 p->rSum += v;
2876 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
2877 p->overflow = 1;
2878 }
2879 }else{
2880 p->rSum += sqlite3_value_double(argv[0]);
2881 p->approx = 1;
2882 }
2883 }
2884 }
2885 static void sumFinalize(sqlite3_context *context){
2886 SumCtx *p;
2887 p = sqlite3_aggregate_context(context, 0);
2888 if( p && p->cnt>0 ){
2889 if( p->overflow ){
2890 sqlite3_result_error(context,"integer overflow",-1);
2891 }else if( p->approx ){
2892 sqlite3_result_double(context, p->rSum);
2893 }else{
2894 sqlite3_result_int64(context, p->iSum);
2895 }
2896 }
2897 }
2898 static void avgFinalize(sqlite3_context *context){
2899 SumCtx *p;
2900 p = sqlite3_aggregate_context(context, 0);
2901 if( p && p->cnt>0 ){
2902 sqlite3_result_double(context, p->rSum/(double)p->cnt);
2903 }
2904 }
2905 static void totalFinalize(sqlite3_context *context){
2906 SumCtx *p;
2907 p = sqlite3_aggregate_context(context, 0);
2908 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
2909 sqlite3_result_double(context, p ? p->rSum : (double)0);
2910 }
2911
2912 /*
2913 ** The following structure keeps track of state information for the
2914 ** count() aggregate function.
2915 */
2916 typedef struct CountCtx CountCtx;
2917 struct CountCtx {
2918 i64 n;
2919 };
2920
2921 /*
2922 ** Routines to implement the count() aggregate function.
2923 */
2924 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
2925 CountCtx *p;
2926 p = sqlite3_aggregate_context(context, sizeof(*p));
2927 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
2928 p->n++;
2929 }
2930
2931 #ifndef SQLITE_OMIT_DEPRECATED
2932 /* The sqlite3_aggregate_count() function is deprecated. But just to make
2933 ** sure it still operates correctly, verify that its count agrees with our
2934 ** internal count when using count(*) and when the total count can be
2935 ** expressed as a 32-bit integer. */
2936 assert( argc==1 || p==0 || p->n>0x7fffffff
2937 || p->n==sqlite3_aggregate_count(context) );
2938 #endif
2939 }
2940 static void countFinalize(sqlite3_context *context){
2941 CountCtx *p;
2942 p = sqlite3_aggregate_context(context, 0);
2943 sqlite3_result_int64(context, p ? p->n : 0);
2944 }
2945
2946 /*
2947 ** Routines to implement min() and max() aggregate functions.
2948 */
2949 static void minmaxStep(
2950 sqlite3_context *context,
2951 int NotUsed,
2952 sqlite3_value **argv
2953 ){
2954 Mem *pArg = (Mem *)argv[0];
2955 Mem *pBest;
2956 UNUSED_PARAMETER(NotUsed);
2957
2958 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
2959 if( !pBest ) return;
2960
2961 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
2962 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
2963 }else if( pBest->flags ){
2964 int max;
2965 int cmp;
2966 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
2967 /* This step function is used for both the min() and max() aggregates,
2968 ** the only difference between the two being that the sense of the
2969 ** comparison is inverted. For the max() aggregate, the
2970 ** sqlite3_user_data() function returns (void *)-1. For min() it
2971 ** returns (void *)db, where db is the sqlite3* database pointer.
2972 ** Therefore the next statement sets variable 'max' to 1 for the max()
2973 ** aggregate, or 0 for min().
2974 */
2975 max = sqlite3_user_data(context)!=0;
2976 cmp = sqlite3MemCompare(pBest, pArg, pColl);
2977 if( (max && cmp<0) || (!max && cmp>0) ){
2978 sqlite3VdbeMemCopy(pBest, pArg);
2979 }else{
2980 sqlite3SkipAccumulatorLoad(context);
2981 }
2982 }else{
2983 pBest->db = sqlite3_context_db_handle(context);
2984 sqlite3VdbeMemCopy(pBest, pArg);
2985 }
2986 }
2987 static void minMaxFinalize(sqlite3_context *context){
2988 sqlite3_value *pRes;
2989 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
2990 if( pRes ){
2991 if( pRes->flags ){
2992 sqlite3_result_value(context, pRes);
2993 }
2994 sqlite3VdbeMemRelease(pRes);
2995 }
2996 }
2997
2998 /*
2999 ** group_concat(EXPR, ?SEPARATOR?)
3000 */
3001 static void groupConcatStep(
3002 sqlite3_context *context,
3003 int argc,
3004 sqlite3_value **argv
3005 ){
3006 const char *zVal;
3007 StrAccum *pAccum;
3008 const char *zSep;
3009 int nVal, nSep;
3010 assert( argc==1 || argc==2 );
3011 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
3012 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
3013
3014 if( pAccum ){
3015 sqlite3 *db = sqlite3_context_db_handle(context);
3016 int firstTerm = pAccum->mxAlloc==0;
3017 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
3018 if( !firstTerm ){
3019 if( argc==2 ){
3020 zSep = (char*)sqlite3_value_text(argv[1]);
3021 nSep = sqlite3_value_bytes(argv[1]);
3022 }else{
3023 zSep = ",";
3024 nSep = 1;
3025 }
3026 if( zSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
3027 }
3028 zVal = (char*)sqlite3_value_text(argv[0]);
3029 nVal = sqlite3_value_bytes(argv[0]);
3030 if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
3031 }
3032 }
3033 static void groupConcatFinalize(sqlite3_context *context){
3034 StrAccum *pAccum;
3035 pAccum = sqlite3_aggregate_context(context, 0);
3036 if( pAccum ){
3037 if( pAccum->accError==STRACCUM_TOOBIG ){
3038 sqlite3_result_error_toobig(context);
3039 }else if( pAccum->accError==STRACCUM_NOMEM ){
3040 sqlite3_result_error_nomem(context);
3041 }else{
3042 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
3043 sqlite3_free);
3044 }
3045 }
3046 }
3047
3048 /*
3049 ** This routine does per-connection function registration. Most
3050 ** of the built-in functions above are part of the global function set.
3051 ** This routine only deals with those that are not global.
3052 */
3053 SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
3054 int rc = sqlite3_overload_function(db, "MATCH", 2);
3055 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
3056 if( rc==SQLITE_NOMEM ){
3057 sqlite3OomFault(db);
3058 }
3059 }
3060
3061 /*
3062 ** Set the LIKEOPT flag on the 2-argument function with the given name.
3063 */
3064 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
3065 FuncDef *pDef;
3066 pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
3067 if( ALWAYS(pDef) ){
3068 pDef->funcFlags |= flagVal;
3069 }
3070 }
3071
3072 /*
3073 ** Register the built-in LIKE and GLOB functions. The caseSensitive
3074 ** parameter determines whether or not the LIKE operator is case
3075 ** sensitive. GLOB is always case sensitive.
3076 */
3077 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive) {
3078 struct compareInfo *pInfo;
3079 if( caseSensitive ){
3080 pInfo = (struct compareInfo*)&likeInfoAlt;
3081 }else{
3082 pInfo = (struct compareInfo*)&likeInfoNorm;
3083 }
3084 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
3085 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
3086 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
3087 (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
3088 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
3089 setLikeOptFlag(db, "like",
3090 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
3091 }
3092
3093 /*
3094 ** pExpr points to an expression which implements a function. If
3095 ** it is appropriate to apply the LIKE optimization to that function
3096 ** then set aWc[0] through aWc[2] to the wildcard characters and
3097 ** return TRUE. If the function is not a LIKE-style function then
3098 ** return FALSE.
3099 **
3100 ** *pIsNocase is set to true if uppercase and lowercase are equivalent for
3101 ** the function (default for LIKE). If the function makes the distinction
3102 ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
3103 ** false.
3104 */
3105 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocas e, char *aWc){
3106 FuncDef *pDef;
3107 if( pExpr->op!=TK_FUNCTION
3108 || !pExpr->x.pList
3109 || pExpr->x.pList->nExpr!=2
3110 ){
3111 return 0;
3112 }
3113 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
3114 pDef = sqlite3FindFunction(db, pExpr->u.zToken, 2, SQLITE_UTF8, 0);
3115 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
3116 return 0;
3117 }
3118
3119 /* The memcpy() statement assumes that the wildcard characters are
3120 ** the first three statements in the compareInfo structure. The
3121 ** asserts() that follow verify that assumption
3122 */
3123 memcpy(aWc, pDef->pUserData, 3);
3124 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
3125 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
3126 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
3127 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
3128 return 1;
3129 }
3130
3131 /*
3132 ** All of the FuncDef structures in the aBuiltinFunc[] array above
3133 ** to the global function hash table. This occurs at start-time (as
3134 ** a consequence of calling sqlite3_initialize()).
3135 **
3136 ** After this routine runs
3137 */
3138 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
3139 /*
3140 ** The following array holds FuncDef structures for all of the functions
3141 ** defined in this file.
3142 **
3143 ** The array cannot be constant since changes are made to the
3144 ** FuncDef.pHash elements at start-time. The elements of this array
3145 ** are read-only after initialization is complete.
3146 **
3147 ** For peak efficiency, put the most frequently used function last.
3148 */
3149 static FuncDef aBuiltinFunc[] = {
3150 #ifdef SQLITE_SOUNDEX
3151 FUNCTION(soundex, 1, 0, 0, soundexFunc ),
3152 #endif
3153 #ifndef SQLITE_OMIT_LOAD_EXTENSION
3154 VFUNCTION(load_extension, 1, 0, 0, loadExt ),
3155 VFUNCTION(load_extension, 2, 0, 0, loadExt ),
3156 #endif
3157 #if SQLITE_USER_AUTHENTICATION
3158 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ),
3159 #endif
3160 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
3161 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
3162 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
3163 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
3164 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
3165 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
3166 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
3167 #ifdef SQLITE_DEBUG
3168 FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY),
3169 #endif
3170 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
3171 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
3172 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
3173 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
3174 FUNCTION(trim, 1, 3, 0, trimFunc ),
3175 FUNCTION(trim, 2, 3, 0, trimFunc ),
3176 FUNCTION(min, -1, 0, 1, minmaxFunc ),
3177 FUNCTION(min, 0, 0, 1, 0 ),
3178 AGGREGATE2(min, 1, 0, 1, minmaxStep, minMaxFinalize,
3179 SQLITE_FUNC_MINMAX ),
3180 FUNCTION(max, -1, 1, 1, minmaxFunc ),
3181 FUNCTION(max, 0, 1, 1, 0 ),
3182 AGGREGATE2(max, 1, 1, 1, minmaxStep, minMaxFinalize,
3183 SQLITE_FUNC_MINMAX ),
3184 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
3185 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
3186 FUNCTION(instr, 2, 0, 0, instrFunc ),
3187 FUNCTION(printf, -1, 0, 0, printfFunc ),
3188 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
3189 FUNCTION(char, -1, 0, 0, charFunc ),
3190 FUNCTION(abs, 1, 0, 0, absFunc ),
3191 #ifndef SQLITE_OMIT_FLOATING_POINT
3192 FUNCTION(round, 1, 0, 0, roundFunc ),
3193 FUNCTION(round, 2, 0, 0, roundFunc ),
3194 #endif
3195 FUNCTION(upper, 1, 0, 0, upperFunc ),
3196 FUNCTION(lower, 1, 0, 0, lowerFunc ),
3197 FUNCTION(hex, 1, 0, 0, hexFunc ),
3198 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
3199 VFUNCTION(random, 0, 0, 0, randomFunc ),
3200 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
3201 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
3202 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
3203 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
3204 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
3205 FUNCTION(quote, 1, 0, 0, quoteFunc ),
3206 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
3207 VFUNCTION(changes, 0, 0, 0, changes ),
3208 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
3209 FUNCTION(replace, 3, 0, 0, replaceFunc ),
3210 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
3211 FUNCTION(substr, 2, 0, 0, substrFunc ),
3212 FUNCTION(substr, 3, 0, 0, substrFunc ),
3213 AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ),
3214 AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ),
3215 AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ),
3216 AGGREGATE2(count, 0, 0, 0, countStep, countFinalize,
3217 SQLITE_FUNC_COUNT ),
3218 AGGREGATE(count, 1, 0, 0, countStep, countFinalize ),
3219 AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize),
3220 AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize),
3221
3222 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
3223 #ifdef SQLITE_CASE_SENSITIVE_LIKE
3224 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
3225 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
3226 #else
3227 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
3228 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
3229 #endif
3230 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
3231 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
3232 #endif
3233 FUNCTION(coalesce, 1, 0, 0, 0 ),
3234 FUNCTION(coalesce, 0, 0, 0, 0 ),
3235 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
3236 };
3237 #ifndef SQLITE_OMIT_ALTERTABLE
3238 sqlite3AlterFunctions();
3239 #endif
3240 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
3241 sqlite3AnalyzeFunctions();
3242 #endif
3243 sqlite3RegisterDateTimeFunctions();
3244 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc));
3245
3246 #if 0 /* Enable to print out how the built-in functions are hashed */
3247 {
3248 int i;
3249 FuncDef *p;
3250 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
3251 printf("FUNC-HASH %02d:", i);
3252 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
3253 int n = sqlite3Strlen30(p->zName);
3254 int h = p->zName[0] + n;
3255 printf(" %s(%d)", p->zName, h);
3256 }
3257 printf("\n");
3258 }
3259 }
3260 #endif
3261 }
3262
3263 /************** End of func.c ************************************************/
3264 /************** Begin file fkey.c ********************************************/
3265 /*
3266 **
3267 ** The author disclaims copyright to this source code. In place of
3268 ** a legal notice, here is a blessing:
3269 **
3270 ** May you do good and not evil.
3271 ** May you find forgiveness for yourself and forgive others.
3272 ** May you share freely, never taking more than you give.
3273 **
3274 *************************************************************************
3275 ** This file contains code used by the compiler to add foreign key
3276 ** support to compiled SQL statements.
3277 */
3278 /* #include "sqliteInt.h" */
3279
3280 #ifndef SQLITE_OMIT_FOREIGN_KEY
3281 #ifndef SQLITE_OMIT_TRIGGER
3282
3283 /*
3284 ** Deferred and Immediate FKs
3285 ** --------------------------
3286 **
3287 ** Foreign keys in SQLite come in two flavours: deferred and immediate.
3288 ** If an immediate foreign key constraint is violated,
3289 ** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
3290 ** statement transaction rolled back. If a
3291 ** deferred foreign key constraint is violated, no action is taken
3292 ** immediately. However if the application attempts to commit the
3293 ** transaction before fixing the constraint violation, the attempt fails.
3294 **
3295 ** Deferred constraints are implemented using a simple counter associated
3296 ** with the database handle. The counter is set to zero each time a
3297 ** database transaction is opened. Each time a statement is executed
3298 ** that causes a foreign key violation, the counter is incremented. Each
3299 ** time a statement is executed that removes an existing violation from
3300 ** the database, the counter is decremented. When the transaction is
3301 ** committed, the commit fails if the current value of the counter is
3302 ** greater than zero. This scheme has two big drawbacks:
3303 **
3304 ** * When a commit fails due to a deferred foreign key constraint,
3305 ** there is no way to tell which foreign constraint is not satisfied,
3306 ** or which row it is not satisfied for.
3307 **
3308 ** * If the database contains foreign key violations when the
3309 ** transaction is opened, this may cause the mechanism to malfunction.
3310 **
3311 ** Despite these problems, this approach is adopted as it seems simpler
3312 ** than the alternatives.
3313 **
3314 ** INSERT operations:
3315 **
3316 ** I.1) For each FK for which the table is the child table, search
3317 ** the parent table for a match. If none is found increment the
3318 ** constraint counter.
3319 **
3320 ** I.2) For each FK for which the table is the parent table,
3321 ** search the child table for rows that correspond to the new
3322 ** row in the parent table. Decrement the counter for each row
3323 ** found (as the constraint is now satisfied).
3324 **
3325 ** DELETE operations:
3326 **
3327 ** D.1) For each FK for which the table is the child table,
3328 ** search the parent table for a row that corresponds to the
3329 ** deleted row in the child table. If such a row is not found,
3330 ** decrement the counter.
3331 **
3332 ** D.2) For each FK for which the table is the parent table, search
3333 ** the child table for rows that correspond to the deleted row
3334 ** in the parent table. For each found increment the counter.
3335 **
3336 ** UPDATE operations:
3337 **
3338 ** An UPDATE command requires that all 4 steps above are taken, but only
3339 ** for FK constraints for which the affected columns are actually
3340 ** modified (values must be compared at runtime).
3341 **
3342 ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
3343 ** This simplifies the implementation a bit.
3344 **
3345 ** For the purposes of immediate FK constraints, the OR REPLACE conflict
3346 ** resolution is considered to delete rows before the new row is inserted.
3347 ** If a delete caused by OR REPLACE violates an FK constraint, an exception
3348 ** is thrown, even if the FK constraint would be satisfied after the new
3349 ** row is inserted.
3350 **
3351 ** Immediate constraints are usually handled similarly. The only difference
3352 ** is that the counter used is stored as part of each individual statement
3353 ** object (struct Vdbe). If, after the statement has run, its immediate
3354 ** constraint counter is greater than zero,
3355 ** it returns SQLITE_CONSTRAINT_FOREIGNKEY
3356 ** and the statement transaction is rolled back. An exception is an INSERT
3357 ** statement that inserts a single row only (no triggers). In this case,
3358 ** instead of using a counter, an exception is thrown immediately if the
3359 ** INSERT violates a foreign key constraint. This is necessary as such
3360 ** an INSERT does not open a statement transaction.
3361 **
3362 ** TODO: How should dropping a table be handled? How should renaming a
3363 ** table be handled?
3364 **
3365 **
3366 ** Query API Notes
3367 ** ---------------
3368 **
3369 ** Before coding an UPDATE or DELETE row operation, the code-generator
3370 ** for those two operations needs to know whether or not the operation
3371 ** requires any FK processing and, if so, which columns of the original
3372 ** row are required by the FK processing VDBE code (i.e. if FKs were
3373 ** implemented using triggers, which of the old.* columns would be
3374 ** accessed). No information is required by the code-generator before
3375 ** coding an INSERT operation. The functions used by the UPDATE/DELETE
3376 ** generation code to query for this information are:
3377 **
3378 ** sqlite3FkRequired() - Test to see if FK processing is required.
3379 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
3380 **
3381 **
3382 ** Externally accessible module functions
3383 ** --------------------------------------
3384 **
3385 ** sqlite3FkCheck() - Check for foreign key violations.
3386 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
3387 ** sqlite3FkDelete() - Delete an FKey structure.
3388 */
3389
3390 /*
3391 ** VDBE Calling Convention
3392 ** -----------------------
3393 **
3394 ** Example:
3395 **
3396 ** For the following INSERT statement:
3397 **
3398 ** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
3399 ** INSERT INTO t1 VALUES(1, 2, 3.1);
3400 **
3401 ** Register (x): 2 (type integer)
3402 ** Register (x+1): 1 (type integer)
3403 ** Register (x+2): NULL (type NULL)
3404 ** Register (x+3): 3.1 (type real)
3405 */
3406
3407 /*
3408 ** A foreign key constraint requires that the key columns in the parent
3409 ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
3410 ** Given that pParent is the parent table for foreign key constraint pFKey,
3411 ** search the schema for a unique index on the parent key columns.
3412 **
3413 ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
3414 ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
3415 ** is set to point to the unique index.
3416 **
3417 ** If the parent key consists of a single column (the foreign key constraint
3418 ** is not a composite foreign key), output variable *paiCol is set to NULL.
3419 ** Otherwise, it is set to point to an allocated array of size N, where
3420 ** N is the number of columns in the parent key. The first element of the
3421 ** array is the index of the child table column that is mapped by the FK
3422 ** constraint to the parent table column stored in the left-most column
3423 ** of index *ppIdx. The second element of the array is the index of the
3424 ** child table column that corresponds to the second left-most column of
3425 ** *ppIdx, and so on.
3426 **
3427 ** If the required index cannot be found, either because:
3428 **
3429 ** 1) The named parent key columns do not exist, or
3430 **
3431 ** 2) The named parent key columns do exist, but are not subject to a
3432 ** UNIQUE or PRIMARY KEY constraint, or
3433 **
3434 ** 3) No parent key columns were provided explicitly as part of the
3435 ** foreign key definition, and the parent table does not have a
3436 ** PRIMARY KEY, or
3437 **
3438 ** 4) No parent key columns were provided explicitly as part of the
3439 ** foreign key definition, and the PRIMARY KEY of the parent table
3440 ** consists of a different number of columns to the child key in
3441 ** the child table.
3442 **
3443 ** then non-zero is returned, and a "foreign key mismatch" error loaded
3444 ** into pParse. If an OOM error occurs, non-zero is returned and the
3445 ** pParse->db->mallocFailed flag is set.
3446 */
3447 SQLITE_PRIVATE int sqlite3FkLocateIndex(
3448 Parse *pParse, /* Parse context to store any error in */
3449 Table *pParent, /* Parent table of FK constraint pFKey */
3450 FKey *pFKey, /* Foreign key to find index for */
3451 Index **ppIdx, /* OUT: Unique index on parent table */
3452 int **paiCol /* OUT: Map of index columns in pFKey */
3453 ){
3454 Index *pIdx = 0; /* Value to return via *ppIdx */
3455 int *aiCol = 0; /* Value to return via *paiCol */
3456 int nCol = pFKey->nCol; /* Number of columns in parent key */
3457 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
3458
3459 /* The caller is responsible for zeroing output parameters. */
3460 assert( ppIdx && *ppIdx==0 );
3461 assert( !paiCol || *paiCol==0 );
3462 assert( pParse );
3463
3464 /* If this is a non-composite (single column) foreign key, check if it
3465 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
3466 ** and *paiCol set to zero and return early.
3467 **
3468 ** Otherwise, for a composite foreign key (more than one column), allocate
3469 ** space for the aiCol array (returned via output parameter *paiCol).
3470 ** Non-composite foreign keys do not require the aiCol array.
3471 */
3472 if( nCol==1 ){
3473 /* The FK maps to the IPK if any of the following are true:
3474 **
3475 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
3476 ** mapped to the primary key of table pParent, or
3477 ** 2) The FK is explicitly mapped to a column declared as INTEGER
3478 ** PRIMARY KEY.
3479 */
3480 if( pParent->iPKey>=0 ){
3481 if( !zKey ) return 0;
3482 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
3483 }
3484 }else if( paiCol ){
3485 assert( nCol>1 );
3486 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
3487 if( !aiCol ) return 1;
3488 *paiCol = aiCol;
3489 }
3490
3491 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
3492 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
3493 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
3494 ** of columns. If each indexed column corresponds to a foreign key
3495 ** column of pFKey, then this index is a winner. */
3496
3497 if( zKey==0 ){
3498 /* If zKey is NULL, then this foreign key is implicitly mapped to
3499 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
3500 ** identified by the test. */
3501 if( IsPrimaryKeyIndex(pIdx) ){
3502 if( aiCol ){
3503 int i;
3504 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
3505 }
3506 break;
3507 }
3508 }else{
3509 /* If zKey is non-NULL, then this foreign key was declared to
3510 ** map to an explicit list of columns in table pParent. Check if this
3511 ** index matches those columns. Also, check that the index uses
3512 ** the default collation sequences for each column. */
3513 int i, j;
3514 for(i=0; i<nCol; i++){
3515 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
3516 const char *zDfltColl; /* Def. collation for column */
3517 char *zIdxCol; /* Name of indexed column */
3518
3519 if( iCol<0 ) break; /* No foreign keys against expression indexes */
3520
3521 /* If the index uses a collation sequence that is different from
3522 ** the default collation sequence for the column, this index is
3523 ** unusable. Bail out early in this case. */
3524 zDfltColl = pParent->aCol[iCol].zColl;
3525 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
3526 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
3527
3528 zIdxCol = pParent->aCol[iCol].zName;
3529 for(j=0; j<nCol; j++){
3530 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
3531 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
3532 break;
3533 }
3534 }
3535 if( j==nCol ) break;
3536 }
3537 if( i==nCol ) break; /* pIdx is usable */
3538 }
3539 }
3540 }
3541
3542 if( !pIdx ){
3543 if( !pParse->disableTriggers ){
3544 sqlite3ErrorMsg(pParse,
3545 "foreign key mismatch - \"%w\" referencing \"%w\"",
3546 pFKey->pFrom->zName, pFKey->zTo);
3547 }
3548 sqlite3DbFree(pParse->db, aiCol);
3549 return 1;
3550 }
3551
3552 *ppIdx = pIdx;
3553 return 0;
3554 }
3555
3556 /*
3557 ** This function is called when a row is inserted into or deleted from the
3558 ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
3559 ** on the child table of pFKey, this function is invoked twice for each row
3560 ** affected - once to "delete" the old row, and then again to "insert" the
3561 ** new row.
3562 **
3563 ** Each time it is called, this function generates VDBE code to locate the
3564 ** row in the parent table that corresponds to the row being inserted into
3565 ** or deleted from the child table. If the parent row can be found, no
3566 ** special action is taken. Otherwise, if the parent row can *not* be
3567 ** found in the parent table:
3568 **
3569 ** Operation | FK type | Action taken
3570 ** --------------------------------------------------------------------------
3571 ** INSERT immediate Increment the "immediate constraint counter".
3572 **
3573 ** DELETE immediate Decrement the "immediate constraint counter".
3574 **
3575 ** INSERT deferred Increment the "deferred constraint counter".
3576 **
3577 ** DELETE deferred Decrement the "deferred constraint counter".
3578 **
3579 ** These operations are identified in the comment at the top of this file
3580 ** (fkey.c) as "I.1" and "D.1".
3581 */
3582 static void fkLookupParent(
3583 Parse *pParse, /* Parse context */
3584 int iDb, /* Index of database housing pTab */
3585 Table *pTab, /* Parent table of FK pFKey */
3586 Index *pIdx, /* Unique index on parent key columns in pTab */
3587 FKey *pFKey, /* Foreign key constraint */
3588 int *aiCol, /* Map from parent key columns to child table columns */
3589 int regData, /* Address of array containing child table row */
3590 int nIncr, /* Increment constraint counter by this */
3591 int isIgnore /* If true, pretend pTab contains all NULL values */
3592 ){
3593 int i; /* Iterator variable */
3594 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
3595 int iCur = pParse->nTab - 1; /* Cursor number to use */
3596 int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */
3597
3598 /* If nIncr is less than zero, then check at runtime if there are any
3599 ** outstanding constraints to resolve. If there are not, there is no need
3600 ** to check if deleting this row resolves any outstanding violations.
3601 **
3602 ** Check if any of the key columns in the child table row are NULL. If
3603 ** any are, then the constraint is considered satisfied. No need to
3604 ** search for a matching row in the parent table. */
3605 if( nIncr<0 ){
3606 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
3607 VdbeCoverage(v);
3608 }
3609 for(i=0; i<pFKey->nCol; i++){
3610 int iReg = aiCol[i] + regData + 1;
3611 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
3612 }
3613
3614 if( isIgnore==0 ){
3615 if( pIdx==0 ){
3616 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
3617 ** column of the parent table (table pTab). */
3618 int iMustBeInt; /* Address of MustBeInt instruction */
3619 int regTemp = sqlite3GetTempReg(pParse);
3620
3621 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
3622 ** apply the affinity of the parent key). If this fails, then there
3623 ** is no matching parent key. Before using MustBeInt, make a copy of
3624 ** the value. Otherwise, the value inserted into the child key column
3625 ** will have INTEGER affinity applied to it, which may not be correct. */
3626 sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
3627 iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
3628 VdbeCoverage(v);
3629
3630 /* If the parent table is the same as the child table, and we are about
3631 ** to increment the constraint-counter (i.e. this is an INSERT operation),
3632 ** then check if the row being inserted matches itself. If so, do not
3633 ** increment the constraint-counter. */
3634 if( pTab==pFKey->pFrom && nIncr==1 ){
3635 sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
3636 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
3637 }
3638
3639 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
3640 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
3641 sqlite3VdbeGoto(v, iOk);
3642 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
3643 sqlite3VdbeJumpHere(v, iMustBeInt);
3644 sqlite3ReleaseTempReg(pParse, regTemp);
3645 }else{
3646 int nCol = pFKey->nCol;
3647 int regTemp = sqlite3GetTempRange(pParse, nCol);
3648 int regRec = sqlite3GetTempReg(pParse);
3649
3650 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
3651 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
3652 for(i=0; i<nCol; i++){
3653 sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
3654 }
3655
3656 /* If the parent table is the same as the child table, and we are about
3657 ** to increment the constraint-counter (i.e. this is an INSERT operation),
3658 ** then check if the row being inserted matches itself. If so, do not
3659 ** increment the constraint-counter.
3660 **
3661 ** If any of the parent-key values are NULL, then the row cannot match
3662 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
3663 ** of the parent-key values are NULL (at this point it is known that
3664 ** none of the child key values are).
3665 */
3666 if( pTab==pFKey->pFrom && nIncr==1 ){
3667 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
3668 for(i=0; i<nCol; i++){
3669 int iChild = aiCol[i]+1+regData;
3670 int iParent = pIdx->aiColumn[i]+1+regData;
3671 assert( pIdx->aiColumn[i]>=0 );
3672 assert( aiCol[i]!=pTab->iPKey );
3673 if( pIdx->aiColumn[i]==pTab->iPKey ){
3674 /* The parent key is a composite key that includes the IPK column */
3675 iParent = regData;
3676 }
3677 sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
3678 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
3679 }
3680 sqlite3VdbeGoto(v, iOk);
3681 }
3682
3683 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
3684 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
3685 sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
3686
3687 sqlite3ReleaseTempReg(pParse, regRec);
3688 sqlite3ReleaseTempRange(pParse, regTemp, nCol);
3689 }
3690 }
3691
3692 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
3693 && !pParse->pToplevel
3694 && !pParse->isMultiWrite
3695 ){
3696 /* Special case: If this is an INSERT statement that will insert exactly
3697 ** one row into the table, raise a constraint immediately instead of
3698 ** incrementing a counter. This is necessary as the VM code is being
3699 ** generated for will not open a statement transaction. */
3700 assert( nIncr==1 );
3701 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
3702 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
3703 }else{
3704 if( nIncr>0 && pFKey->isDeferred==0 ){
3705 sqlite3MayAbort(pParse);
3706 }
3707 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
3708 }
3709
3710 sqlite3VdbeResolveLabel(v, iOk);
3711 sqlite3VdbeAddOp1(v, OP_Close, iCur);
3712 }
3713
3714
3715 /*
3716 ** Return an Expr object that refers to a memory register corresponding
3717 ** to column iCol of table pTab.
3718 **
3719 ** regBase is the first of an array of register that contains the data
3720 ** for pTab. regBase itself holds the rowid. regBase+1 holds the first
3721 ** column. regBase+2 holds the second column, and so forth.
3722 */
3723 static Expr *exprTableRegister(
3724 Parse *pParse, /* Parsing and code generating context */
3725 Table *pTab, /* The table whose content is at r[regBase]... */
3726 int regBase, /* Contents of table pTab */
3727 i16 iCol /* Which column of pTab is desired */
3728 ){
3729 Expr *pExpr;
3730 Column *pCol;
3731 const char *zColl;
3732 sqlite3 *db = pParse->db;
3733
3734 pExpr = sqlite3Expr(db, TK_REGISTER, 0);
3735 if( pExpr ){
3736 if( iCol>=0 && iCol!=pTab->iPKey ){
3737 pCol = &pTab->aCol[iCol];
3738 pExpr->iTable = regBase + iCol + 1;
3739 pExpr->affinity = pCol->affinity;
3740 zColl = pCol->zColl;
3741 if( zColl==0 ) zColl = db->pDfltColl->zName;
3742 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
3743 }else{
3744 pExpr->iTable = regBase;
3745 pExpr->affinity = SQLITE_AFF_INTEGER;
3746 }
3747 }
3748 return pExpr;
3749 }
3750
3751 /*
3752 ** Return an Expr object that refers to column iCol of table pTab which
3753 ** has cursor iCur.
3754 */
3755 static Expr *exprTableColumn(
3756 sqlite3 *db, /* The database connection */
3757 Table *pTab, /* The table whose column is desired */
3758 int iCursor, /* The open cursor on the table */
3759 i16 iCol /* The column that is wanted */
3760 ){
3761 Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
3762 if( pExpr ){
3763 pExpr->pTab = pTab;
3764 pExpr->iTable = iCursor;
3765 pExpr->iColumn = iCol;
3766 }
3767 return pExpr;
3768 }
3769
3770 /*
3771 ** This function is called to generate code executed when a row is deleted
3772 ** from the parent table of foreign key constraint pFKey and, if pFKey is
3773 ** deferred, when a row is inserted into the same table. When generating
3774 ** code for an SQL UPDATE operation, this function may be called twice -
3775 ** once to "delete" the old row and once to "insert" the new row.
3776 **
3777 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
3778 ** the number of FK violations in the db) or +1 when deleting one (as this
3779 ** may increase the number of FK constraint problems).
3780 **
3781 ** The code generated by this function scans through the rows in the child
3782 ** table that correspond to the parent table row being deleted or inserted.
3783 ** For each child row found, one of the following actions is taken:
3784 **
3785 ** Operation | FK type | Action taken
3786 ** --------------------------------------------------------------------------
3787 ** DELETE immediate Increment the "immediate constraint counter".
3788 ** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
3789 ** throw a "FOREIGN KEY constraint failed" exception.
3790 **
3791 ** INSERT immediate Decrement the "immediate constraint counter".
3792 **
3793 ** DELETE deferred Increment the "deferred constraint counter".
3794 ** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
3795 ** throw a "FOREIGN KEY constraint failed" exception.
3796 **
3797 ** INSERT deferred Decrement the "deferred constraint counter".
3798 **
3799 ** These operations are identified in the comment at the top of this file
3800 ** (fkey.c) as "I.2" and "D.2".
3801 */
3802 static void fkScanChildren(
3803 Parse *pParse, /* Parse context */
3804 SrcList *pSrc, /* The child table to be scanned */
3805 Table *pTab, /* The parent table */
3806 Index *pIdx, /* Index on parent covering the foreign key */
3807 FKey *pFKey, /* The foreign key linking pSrc to pTab */
3808 int *aiCol, /* Map from pIdx cols to child table cols */
3809 int regData, /* Parent row data starts here */
3810 int nIncr /* Amount to increment deferred counter by */
3811 ){
3812 sqlite3 *db = pParse->db; /* Database handle */
3813 int i; /* Iterator variable */
3814 Expr *pWhere = 0; /* WHERE clause to scan with */
3815 NameContext sNameContext; /* Context used to resolve WHERE clause */
3816 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
3817 int iFkIfZero = 0; /* Address of OP_FkIfZero */
3818 Vdbe *v = sqlite3GetVdbe(pParse);
3819
3820 assert( pIdx==0 || pIdx->pTable==pTab );
3821 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
3822 assert( pIdx!=0 || pFKey->nCol==1 );
3823 assert( pIdx!=0 || HasRowid(pTab) );
3824
3825 if( nIncr<0 ){
3826 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
3827 VdbeCoverage(v);
3828 }
3829
3830 /* Create an Expr object representing an SQL expression like:
3831 **
3832 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
3833 **
3834 ** The collation sequence used for the comparison should be that of
3835 ** the parent key columns. The affinity of the parent key column should
3836 ** be applied to each child key value before the comparison takes place.
3837 */
3838 for(i=0; i<pFKey->nCol; i++){
3839 Expr *pLeft; /* Value from parent table row */
3840 Expr *pRight; /* Column ref to child table */
3841 Expr *pEq; /* Expression (pLeft = pRight) */
3842 i16 iCol; /* Index of column in child table */
3843 const char *zCol; /* Name of column in child table */
3844
3845 iCol = pIdx ? pIdx->aiColumn[i] : -1;
3846 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
3847 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
3848 assert( iCol>=0 );
3849 zCol = pFKey->pFrom->aCol[iCol].zName;
3850 pRight = sqlite3Expr(db, TK_ID, zCol);
3851 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
3852 pWhere = sqlite3ExprAnd(db, pWhere, pEq);
3853 }
3854
3855 /* If the child table is the same as the parent table, then add terms
3856 ** to the WHERE clause that prevent this entry from being scanned.
3857 ** The added WHERE clause terms are like this:
3858 **
3859 ** $current_rowid!=rowid
3860 ** NOT( $current_a==a AND $current_b==b AND ... )
3861 **
3862 ** The first form is used for rowid tables. The second form is used
3863 ** for WITHOUT ROWID tables. In the second form, the primary key is
3864 ** (a,b,...)
3865 */
3866 if( pTab==pFKey->pFrom && nIncr>0 ){
3867 Expr *pNe; /* Expression (pLeft != pRight) */
3868 Expr *pLeft; /* Value from parent table row */
3869 Expr *pRight; /* Column ref to child table */
3870 if( HasRowid(pTab) ){
3871 pLeft = exprTableRegister(pParse, pTab, regData, -1);
3872 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
3873 pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight);
3874 }else{
3875 Expr *pEq, *pAll = 0;
3876 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
3877 assert( pIdx!=0 );
3878 for(i=0; i<pPk->nKeyCol; i++){
3879 i16 iCol = pIdx->aiColumn[i];
3880 assert( iCol>=0 );
3881 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
3882 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
3883 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
3884 pAll = sqlite3ExprAnd(db, pAll, pEq);
3885 }
3886 pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0);
3887 }
3888 pWhere = sqlite3ExprAnd(db, pWhere, pNe);
3889 }
3890
3891 /* Resolve the references in the WHERE clause. */
3892 memset(&sNameContext, 0, sizeof(NameContext));
3893 sNameContext.pSrcList = pSrc;
3894 sNameContext.pParse = pParse;
3895 sqlite3ResolveExprNames(&sNameContext, pWhere);
3896
3897 /* Create VDBE to loop through the entries in pSrc that match the WHERE
3898 ** clause. For each row found, increment either the deferred or immediate
3899 ** foreign key constraint counter. */
3900 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
3901 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
3902 if( pWInfo ){
3903 sqlite3WhereEnd(pWInfo);
3904 }
3905
3906 /* Clean up the WHERE clause constructed above. */
3907 sqlite3ExprDelete(db, pWhere);
3908 if( iFkIfZero ){
3909 sqlite3VdbeJumpHere(v, iFkIfZero);
3910 }
3911 }
3912
3913 /*
3914 ** This function returns a linked list of FKey objects (connected by
3915 ** FKey.pNextTo) holding all children of table pTab. For example,
3916 ** given the following schema:
3917 **
3918 ** CREATE TABLE t1(a PRIMARY KEY);
3919 ** CREATE TABLE t2(b REFERENCES t1(a);
3920 **
3921 ** Calling this function with table "t1" as an argument returns a pointer
3922 ** to the FKey structure representing the foreign key constraint on table
3923 ** "t2". Calling this function with "t2" as the argument would return a
3924 ** NULL pointer (as there are no FK constraints for which t2 is the parent
3925 ** table).
3926 */
3927 SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
3928 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
3929 }
3930
3931 /*
3932 ** The second argument is a Trigger structure allocated by the
3933 ** fkActionTrigger() routine. This function deletes the Trigger structure
3934 ** and all of its sub-components.
3935 **
3936 ** The Trigger structure or any of its sub-components may be allocated from
3937 ** the lookaside buffer belonging to database handle dbMem.
3938 */
3939 static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
3940 if( p ){
3941 TriggerStep *pStep = p->step_list;
3942 sqlite3ExprDelete(dbMem, pStep->pWhere);
3943 sqlite3ExprListDelete(dbMem, pStep->pExprList);
3944 sqlite3SelectDelete(dbMem, pStep->pSelect);
3945 sqlite3ExprDelete(dbMem, p->pWhen);
3946 sqlite3DbFree(dbMem, p);
3947 }
3948 }
3949
3950 /*
3951 ** This function is called to generate code that runs when table pTab is
3952 ** being dropped from the database. The SrcList passed as the second argument
3953 ** to this function contains a single entry guaranteed to resolve to
3954 ** table pTab.
3955 **
3956 ** Normally, no code is required. However, if either
3957 **
3958 ** (a) The table is the parent table of a FK constraint, or
3959 ** (b) The table is the child table of a deferred FK constraint and it is
3960 ** determined at runtime that there are outstanding deferred FK
3961 ** constraint violations in the database,
3962 **
3963 ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
3964 ** the table from the database. Triggers are disabled while running this
3965 ** DELETE, but foreign key actions are not.
3966 */
3967 SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTa b){
3968 sqlite3 *db = pParse->db;
3969 if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
3970 int iSkip = 0;
3971 Vdbe *v = sqlite3GetVdbe(pParse);
3972
3973 assert( v ); /* VDBE has already been allocated */
3974 if( sqlite3FkReferences(pTab)==0 ){
3975 /* Search for a deferred foreign key constraint for which this table
3976 ** is the child table. If one cannot be found, return without
3977 ** generating any VDBE code. If one can be found, then jump over
3978 ** the entire DELETE if there are no outstanding deferred constraints
3979 ** when this statement is run. */
3980 FKey *p;
3981 for(p=pTab->pFKey; p; p=p->pNextFrom){
3982 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
3983 }
3984 if( !p ) return;
3985 iSkip = sqlite3VdbeMakeLabel(v);
3986 sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
3987 }
3988
3989 pParse->disableTriggers = 1;
3990 sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
3991 pParse->disableTriggers = 0;
3992
3993 /* If the DELETE has generated immediate foreign key constraint
3994 ** violations, halt the VDBE and return an error at this point, before
3995 ** any modifications to the schema are made. This is because statement
3996 ** transactions are not able to rollback schema changes.
3997 **
3998 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
3999 ** the statement transaction will not be rolled back even if FK
4000 ** constraints are violated.
4001 */
4002 if( (db->flags & SQLITE_DeferFKs)==0 ){
4003 sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
4004 VdbeCoverage(v);
4005 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
4006 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
4007 }
4008
4009 if( iSkip ){
4010 sqlite3VdbeResolveLabel(v, iSkip);
4011 }
4012 }
4013 }
4014
4015
4016 /*
4017 ** The second argument points to an FKey object representing a foreign key
4018 ** for which pTab is the child table. An UPDATE statement against pTab
4019 ** is currently being processed. For each column of the table that is
4020 ** actually updated, the corresponding element in the aChange[] array
4021 ** is zero or greater (if a column is unmodified the corresponding element
4022 ** is set to -1). If the rowid column is modified by the UPDATE statement
4023 ** the bChngRowid argument is non-zero.
4024 **
4025 ** This function returns true if any of the columns that are part of the
4026 ** child key for FK constraint *p are modified.
4027 */
4028 static int fkChildIsModified(
4029 Table *pTab, /* Table being updated */
4030 FKey *p, /* Foreign key for which pTab is the child */
4031 int *aChange, /* Array indicating modified columns */
4032 int bChngRowid /* True if rowid is modified by this update */
4033 ){
4034 int i;
4035 for(i=0; i<p->nCol; i++){
4036 int iChildKey = p->aCol[i].iFrom;
4037 if( aChange[iChildKey]>=0 ) return 1;
4038 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
4039 }
4040 return 0;
4041 }
4042
4043 /*
4044 ** The second argument points to an FKey object representing a foreign key
4045 ** for which pTab is the parent table. An UPDATE statement against pTab
4046 ** is currently being processed. For each column of the table that is
4047 ** actually updated, the corresponding element in the aChange[] array
4048 ** is zero or greater (if a column is unmodified the corresponding element
4049 ** is set to -1). If the rowid column is modified by the UPDATE statement
4050 ** the bChngRowid argument is non-zero.
4051 **
4052 ** This function returns true if any of the columns that are part of the
4053 ** parent key for FK constraint *p are modified.
4054 */
4055 static int fkParentIsModified(
4056 Table *pTab,
4057 FKey *p,
4058 int *aChange,
4059 int bChngRowid
4060 ){
4061 int i;
4062 for(i=0; i<p->nCol; i++){
4063 char *zKey = p->aCol[i].zCol;
4064 int iKey;
4065 for(iKey=0; iKey<pTab->nCol; iKey++){
4066 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
4067 Column *pCol = &pTab->aCol[iKey];
4068 if( zKey ){
4069 if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
4070 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
4071 return 1;
4072 }
4073 }
4074 }
4075 }
4076 return 0;
4077 }
4078
4079 /*
4080 ** Return true if the parser passed as the first argument is being
4081 ** used to code a trigger that is really a "SET NULL" action belonging
4082 ** to trigger pFKey.
4083 */
4084 static int isSetNullAction(Parse *pParse, FKey *pFKey){
4085 Parse *pTop = sqlite3ParseToplevel(pParse);
4086 if( pTop->pTriggerPrg ){
4087 Trigger *p = pTop->pTriggerPrg->pTrigger;
4088 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
4089 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
4090 ){
4091 return 1;
4092 }
4093 }
4094 return 0;
4095 }
4096
4097 /*
4098 ** This function is called when inserting, deleting or updating a row of
4099 ** table pTab to generate VDBE code to perform foreign key constraint
4100 ** processing for the operation.
4101 **
4102 ** For a DELETE operation, parameter regOld is passed the index of the
4103 ** first register in an array of (pTab->nCol+1) registers containing the
4104 ** rowid of the row being deleted, followed by each of the column values
4105 ** of the row being deleted, from left to right. Parameter regNew is passed
4106 ** zero in this case.
4107 **
4108 ** For an INSERT operation, regOld is passed zero and regNew is passed the
4109 ** first register of an array of (pTab->nCol+1) registers containing the new
4110 ** row data.
4111 **
4112 ** For an UPDATE operation, this function is called twice. Once before
4113 ** the original record is deleted from the table using the calling convention
4114 ** described for DELETE. Then again after the original record is deleted
4115 ** but before the new record is inserted using the INSERT convention.
4116 */
4117 SQLITE_PRIVATE void sqlite3FkCheck(
4118 Parse *pParse, /* Parse context */
4119 Table *pTab, /* Row is being deleted from this table */
4120 int regOld, /* Previous row data is stored here */
4121 int regNew, /* New row data is stored here */
4122 int *aChange, /* Array indicating UPDATEd columns (or 0) */
4123 int bChngRowid /* True if rowid is UPDATEd */
4124 ){
4125 sqlite3 *db = pParse->db; /* Database handle */
4126 FKey *pFKey; /* Used to iterate through FKs */
4127 int iDb; /* Index of database containing pTab */
4128 const char *zDb; /* Name of database containing pTab */
4129 int isIgnoreErrors = pParse->disableTriggers;
4130
4131 /* Exactly one of regOld and regNew should be non-zero. */
4132 assert( (regOld==0)!=(regNew==0) );
4133
4134 /* If foreign-keys are disabled, this function is a no-op. */
4135 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
4136
4137 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
4138 zDb = db->aDb[iDb].zDbSName;
4139
4140 /* Loop through all the foreign key constraints for which pTab is the
4141 ** child table (the table that the foreign key definition is part of). */
4142 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
4143 Table *pTo; /* Parent table of foreign key pFKey */
4144 Index *pIdx = 0; /* Index on key columns in pTo */
4145 int *aiFree = 0;
4146 int *aiCol;
4147 int iCol;
4148 int i;
4149 int bIgnore = 0;
4150
4151 if( aChange
4152 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
4153 && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
4154 ){
4155 continue;
4156 }
4157
4158 /* Find the parent table of this foreign key. Also find a unique index
4159 ** on the parent key columns in the parent table. If either of these
4160 ** schema items cannot be located, set an error in pParse and return
4161 ** early. */
4162 if( pParse->disableTriggers ){
4163 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
4164 }else{
4165 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
4166 }
4167 if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
4168 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
4169 if( !isIgnoreErrors || db->mallocFailed ) return;
4170 if( pTo==0 ){
4171 /* If isIgnoreErrors is true, then a table is being dropped. In this
4172 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
4173 ** before actually dropping it in order to check FK constraints.
4174 ** If the parent table of an FK constraint on the current table is
4175 ** missing, behave as if it is empty. i.e. decrement the relevant
4176 ** FK counter for each row of the current table with non-NULL keys.
4177 */
4178 Vdbe *v = sqlite3GetVdbe(pParse);
4179 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
4180 for(i=0; i<pFKey->nCol; i++){
4181 int iReg = pFKey->aCol[i].iFrom + regOld + 1;
4182 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
4183 }
4184 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
4185 }
4186 continue;
4187 }
4188 assert( pFKey->nCol==1 || (aiFree && pIdx) );
4189
4190 if( aiFree ){
4191 aiCol = aiFree;
4192 }else{
4193 iCol = pFKey->aCol[0].iFrom;
4194 aiCol = &iCol;
4195 }
4196 for(i=0; i<pFKey->nCol; i++){
4197 if( aiCol[i]==pTab->iPKey ){
4198 aiCol[i] = -1;
4199 }
4200 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
4201 #ifndef SQLITE_OMIT_AUTHORIZATION
4202 /* Request permission to read the parent key columns. If the
4203 ** authorization callback returns SQLITE_IGNORE, behave as if any
4204 ** values read from the parent table are NULL. */
4205 if( db->xAuth ){
4206 int rcauth;
4207 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
4208 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
4209 bIgnore = (rcauth==SQLITE_IGNORE);
4210 }
4211 #endif
4212 }
4213
4214 /* Take a shared-cache advisory read-lock on the parent table. Allocate
4215 ** a cursor to use to search the unique index on the parent key columns
4216 ** in the parent table. */
4217 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
4218 pParse->nTab++;
4219
4220 if( regOld!=0 ){
4221 /* A row is being removed from the child table. Search for the parent.
4222 ** If the parent does not exist, removing the child row resolves an
4223 ** outstanding foreign key constraint violation. */
4224 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
4225 }
4226 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
4227 /* A row is being added to the child table. If a parent row cannot
4228 ** be found, adding the child row has violated the FK constraint.
4229 **
4230 ** If this operation is being performed as part of a trigger program
4231 ** that is actually a "SET NULL" action belonging to this very
4232 ** foreign key, then omit this scan altogether. As all child key
4233 ** values are guaranteed to be NULL, it is not possible for adding
4234 ** this row to cause an FK violation. */
4235 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
4236 }
4237
4238 sqlite3DbFree(db, aiFree);
4239 }
4240
4241 /* Loop through all the foreign key constraints that refer to this table.
4242 ** (the "child" constraints) */
4243 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
4244 Index *pIdx = 0; /* Foreign key index for pFKey */
4245 SrcList *pSrc;
4246 int *aiCol = 0;
4247
4248 if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
4249 continue;
4250 }
4251
4252 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
4253 && !pParse->pToplevel && !pParse->isMultiWrite
4254 ){
4255 assert( regOld==0 && regNew!=0 );
4256 /* Inserting a single row into a parent table cannot cause (or fix)
4257 ** an immediate foreign key violation. So do nothing in this case. */
4258 continue;
4259 }
4260
4261 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
4262 if( !isIgnoreErrors || db->mallocFailed ) return;
4263 continue;
4264 }
4265 assert( aiCol || pFKey->nCol==1 );
4266
4267 /* Create a SrcList structure containing the child table. We need the
4268 ** child table as a SrcList for sqlite3WhereBegin() */
4269 pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
4270 if( pSrc ){
4271 struct SrcList_item *pItem = pSrc->a;
4272 pItem->pTab = pFKey->pFrom;
4273 pItem->zName = pFKey->pFrom->zName;
4274 pItem->pTab->nTabRef++;
4275 pItem->iCursor = pParse->nTab++;
4276
4277 if( regNew!=0 ){
4278 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
4279 }
4280 if( regOld!=0 ){
4281 int eAction = pFKey->aAction[aChange!=0];
4282 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
4283 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
4284 ** action applies, then any foreign key violations caused by
4285 ** removing the parent key will be rectified by the action trigger.
4286 ** So do not set the "may-abort" flag in this case.
4287 **
4288 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
4289 ** may-abort flag will eventually be set on this statement anyway
4290 ** (when this function is called as part of processing the UPDATE
4291 ** within the action trigger).
4292 **
4293 ** Note 2: At first glance it may seem like SQLite could simply omit
4294 ** all OP_FkCounter related scans when either CASCADE or SET NULL
4295 ** applies. The trouble starts if the CASCADE or SET NULL action
4296 ** trigger causes other triggers or action rules attached to the
4297 ** child table to fire. In these cases the fk constraint counters
4298 ** might be set incorrectly if any OP_FkCounter related scans are
4299 ** omitted. */
4300 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
4301 sqlite3MayAbort(pParse);
4302 }
4303 }
4304 pItem->zName = 0;
4305 sqlite3SrcListDelete(db, pSrc);
4306 }
4307 sqlite3DbFree(db, aiCol);
4308 }
4309 }
4310
4311 #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
4312
4313 /*
4314 ** This function is called before generating code to update or delete a
4315 ** row contained in table pTab.
4316 */
4317 SQLITE_PRIVATE u32 sqlite3FkOldmask(
4318 Parse *pParse, /* Parse context */
4319 Table *pTab /* Table being modified */
4320 ){
4321 u32 mask = 0;
4322 if( pParse->db->flags&SQLITE_ForeignKeys ){
4323 FKey *p;
4324 int i;
4325 for(p=pTab->pFKey; p; p=p->pNextFrom){
4326 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
4327 }
4328 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
4329 Index *pIdx = 0;
4330 sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
4331 if( pIdx ){
4332 for(i=0; i<pIdx->nKeyCol; i++){
4333 assert( pIdx->aiColumn[i]>=0 );
4334 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
4335 }
4336 }
4337 }
4338 }
4339 return mask;
4340 }
4341
4342
4343 /*
4344 ** This function is called before generating code to update or delete a
4345 ** row contained in table pTab. If the operation is a DELETE, then
4346 ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
4347 ** to an array of size N, where N is the number of columns in table pTab.
4348 ** If the i'th column is not modified by the UPDATE, then the corresponding
4349 ** entry in the aChange[] array is set to -1. If the column is modified,
4350 ** the value is 0 or greater. Parameter chngRowid is set to true if the
4351 ** UPDATE statement modifies the rowid fields of the table.
4352 **
4353 ** If any foreign key processing will be required, this function returns
4354 ** true. If there is no foreign key related processing, this function
4355 ** returns false.
4356 */
4357 SQLITE_PRIVATE int sqlite3FkRequired(
4358 Parse *pParse, /* Parse context */
4359 Table *pTab, /* Table being modified */
4360 int *aChange, /* Non-NULL for UPDATE operations */
4361 int chngRowid /* True for UPDATE that affects rowid */
4362 ){
4363 if( pParse->db->flags&SQLITE_ForeignKeys ){
4364 if( !aChange ){
4365 /* A DELETE operation. Foreign key processing is required if the
4366 ** table in question is either the child or parent table for any
4367 ** foreign key constraint. */
4368 return (sqlite3FkReferences(pTab) || pTab->pFKey);
4369 }else{
4370 /* This is an UPDATE. Foreign key processing is only required if the
4371 ** operation modifies one or more child or parent key columns. */
4372 FKey *p;
4373
4374 /* Check if any child key columns are being modified. */
4375 for(p=pTab->pFKey; p; p=p->pNextFrom){
4376 if( fkChildIsModified(pTab, p, aChange, chngRowid) ) return 1;
4377 }
4378
4379 /* Check if any parent key columns are being modified. */
4380 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
4381 if( fkParentIsModified(pTab, p, aChange, chngRowid) ) return 1;
4382 }
4383 }
4384 }
4385 return 0;
4386 }
4387
4388 /*
4389 ** This function is called when an UPDATE or DELETE operation is being
4390 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
4391 ** If the current operation is an UPDATE, then the pChanges parameter is
4392 ** passed a pointer to the list of columns being modified. If it is a
4393 ** DELETE, pChanges is passed a NULL pointer.
4394 **
4395 ** It returns a pointer to a Trigger structure containing a trigger
4396 ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
4397 ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
4398 ** returned (these actions require no special handling by the triggers
4399 ** sub-system, code for them is created by fkScanChildren()).
4400 **
4401 ** For example, if pFKey is the foreign key and pTab is table "p" in
4402 ** the following schema:
4403 **
4404 ** CREATE TABLE p(pk PRIMARY KEY);
4405 ** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
4406 **
4407 ** then the returned trigger structure is equivalent to:
4408 **
4409 ** CREATE TRIGGER ... DELETE ON p BEGIN
4410 ** DELETE FROM c WHERE ck = old.pk;
4411 ** END;
4412 **
4413 ** The returned pointer is cached as part of the foreign key object. It
4414 ** is eventually freed along with the rest of the foreign key object by
4415 ** sqlite3FkDelete().
4416 */
4417 static Trigger *fkActionTrigger(
4418 Parse *pParse, /* Parse context */
4419 Table *pTab, /* Table being updated or deleted from */
4420 FKey *pFKey, /* Foreign key to get action for */
4421 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
4422 ){
4423 sqlite3 *db = pParse->db; /* Database handle */
4424 int action; /* One of OE_None, OE_Cascade etc. */
4425 Trigger *pTrigger; /* Trigger definition to return */
4426 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
4427
4428 action = pFKey->aAction[iAction];
4429 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
4430 return 0;
4431 }
4432 pTrigger = pFKey->apTrigger[iAction];
4433
4434 if( action!=OE_None && !pTrigger ){
4435 char const *zFrom; /* Name of child table */
4436 int nFrom; /* Length in bytes of zFrom */
4437 Index *pIdx = 0; /* Parent key index for this FK */
4438 int *aiCol = 0; /* child table cols -> parent key cols */
4439 TriggerStep *pStep = 0; /* First (only) step of trigger program */
4440 Expr *pWhere = 0; /* WHERE clause of trigger step */
4441 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
4442 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
4443 int i; /* Iterator variable */
4444 Expr *pWhen = 0; /* WHEN clause for the trigger */
4445
4446 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
4447 assert( aiCol || pFKey->nCol==1 );
4448
4449 for(i=0; i<pFKey->nCol; i++){
4450 Token tOld = { "old", 3 }; /* Literal "old" token */
4451 Token tNew = { "new", 3 }; /* Literal "new" token */
4452 Token tFromCol; /* Name of column in child table */
4453 Token tToCol; /* Name of column in parent table */
4454 int iFromCol; /* Idx of column in child table */
4455 Expr *pEq; /* tFromCol = OLD.tToCol */
4456
4457 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
4458 assert( iFromCol>=0 );
4459 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
4460 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
4461 sqlite3TokenInit(&tToCol,
4462 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
4463 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
4464
4465 /* Create the expression "OLD.zToCol = zFromCol". It is important
4466 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
4467 ** that the affinity and collation sequence associated with the
4468 ** parent table are used for the comparison. */
4469 pEq = sqlite3PExpr(pParse, TK_EQ,
4470 sqlite3PExpr(pParse, TK_DOT,
4471 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
4472 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
4473 sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
4474 );
4475 pWhere = sqlite3ExprAnd(db, pWhere, pEq);
4476
4477 /* For ON UPDATE, construct the next term of the WHEN clause.
4478 ** The final WHEN clause will be like this:
4479 **
4480 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
4481 */
4482 if( pChanges ){
4483 pEq = sqlite3PExpr(pParse, TK_IS,
4484 sqlite3PExpr(pParse, TK_DOT,
4485 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
4486 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
4487 sqlite3PExpr(pParse, TK_DOT,
4488 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
4489 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0))
4490 );
4491 pWhen = sqlite3ExprAnd(db, pWhen, pEq);
4492 }
4493
4494 if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
4495 Expr *pNew;
4496 if( action==OE_Cascade ){
4497 pNew = sqlite3PExpr(pParse, TK_DOT,
4498 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
4499 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0));
4500 }else if( action==OE_SetDflt ){
4501 Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
4502 if( pDflt ){
4503 pNew = sqlite3ExprDup(db, pDflt, 0);
4504 }else{
4505 pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
4506 }
4507 }else{
4508 pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
4509 }
4510 pList = sqlite3ExprListAppend(pParse, pList, pNew);
4511 sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
4512 }
4513 }
4514 sqlite3DbFree(db, aiCol);
4515
4516 zFrom = pFKey->pFrom->zName;
4517 nFrom = sqlite3Strlen30(zFrom);
4518
4519 if( action==OE_Restrict ){
4520 Token tFrom;
4521 Expr *pRaise;
4522
4523 tFrom.z = zFrom;
4524 tFrom.n = nFrom;
4525 pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
4526 if( pRaise ){
4527 pRaise->affinity = OE_Abort;
4528 }
4529 pSelect = sqlite3SelectNew(pParse,
4530 sqlite3ExprListAppend(pParse, 0, pRaise),
4531 sqlite3SrcListAppend(db, 0, &tFrom, 0),
4532 pWhere,
4533 0, 0, 0, 0, 0, 0
4534 );
4535 pWhere = 0;
4536 }
4537
4538 /* Disable lookaside memory allocation */
4539 db->lookaside.bDisable++;
4540
4541 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
4542 sizeof(Trigger) + /* struct Trigger */
4543 sizeof(TriggerStep) + /* Single step in trigger program */
4544 nFrom + 1 /* Space for pStep->zTarget */
4545 );
4546 if( pTrigger ){
4547 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
4548 pStep->zTarget = (char *)&pStep[1];
4549 memcpy((char *)pStep->zTarget, zFrom, nFrom);
4550
4551 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
4552 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
4553 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
4554 if( pWhen ){
4555 pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0);
4556 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
4557 }
4558 }
4559
4560 /* Re-enable the lookaside buffer, if it was disabled earlier. */
4561 db->lookaside.bDisable--;
4562
4563 sqlite3ExprDelete(db, pWhere);
4564 sqlite3ExprDelete(db, pWhen);
4565 sqlite3ExprListDelete(db, pList);
4566 sqlite3SelectDelete(db, pSelect);
4567 if( db->mallocFailed==1 ){
4568 fkTriggerDelete(db, pTrigger);
4569 return 0;
4570 }
4571 assert( pStep!=0 );
4572
4573 switch( action ){
4574 case OE_Restrict:
4575 pStep->op = TK_SELECT;
4576 break;
4577 case OE_Cascade:
4578 if( !pChanges ){
4579 pStep->op = TK_DELETE;
4580 break;
4581 }
4582 default:
4583 pStep->op = TK_UPDATE;
4584 }
4585 pStep->pTrig = pTrigger;
4586 pTrigger->pSchema = pTab->pSchema;
4587 pTrigger->pTabSchema = pTab->pSchema;
4588 pFKey->apTrigger[iAction] = pTrigger;
4589 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
4590 }
4591
4592 return pTrigger;
4593 }
4594
4595 /*
4596 ** This function is called when deleting or updating a row to implement
4597 ** any required CASCADE, SET NULL or SET DEFAULT actions.
4598 */
4599 SQLITE_PRIVATE void sqlite3FkActions(
4600 Parse *pParse, /* Parse context */
4601 Table *pTab, /* Table being updated or deleted from */
4602 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
4603 int regOld, /* Address of array containing old row */
4604 int *aChange, /* Array indicating UPDATEd columns (or 0) */
4605 int bChngRowid /* True if rowid is UPDATEd */
4606 ){
4607 /* If foreign-key support is enabled, iterate through all FKs that
4608 ** refer to table pTab. If there is an action associated with the FK
4609 ** for this operation (either update or delete), invoke the associated
4610 ** trigger sub-program. */
4611 if( pParse->db->flags&SQLITE_ForeignKeys ){
4612 FKey *pFKey; /* Iterator variable */
4613 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
4614 if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
4615 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
4616 if( pAct ){
4617 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
4618 }
4619 }
4620 }
4621 }
4622 }
4623
4624 #endif /* ifndef SQLITE_OMIT_TRIGGER */
4625
4626 /*
4627 ** Free all memory associated with foreign key definitions attached to
4628 ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
4629 ** hash table.
4630 */
4631 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
4632 FKey *pFKey; /* Iterator variable */
4633 FKey *pNext; /* Copy of pFKey->pNextFrom */
4634
4635 assert( db==0 || IsVirtual(pTab)
4636 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
4637 for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
4638
4639 /* Remove the FK from the fkeyHash hash table. */
4640 if( !db || db->pnBytesFreed==0 ){
4641 if( pFKey->pPrevTo ){
4642 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
4643 }else{
4644 void *p = (void *)pFKey->pNextTo;
4645 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
4646 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
4647 }
4648 if( pFKey->pNextTo ){
4649 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
4650 }
4651 }
4652
4653 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
4654 ** classified as either immediate or deferred.
4655 */
4656 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
4657
4658 /* Delete any triggers created to implement actions for this FK. */
4659 #ifndef SQLITE_OMIT_TRIGGER
4660 fkTriggerDelete(db, pFKey->apTrigger[0]);
4661 fkTriggerDelete(db, pFKey->apTrigger[1]);
4662 #endif
4663
4664 pNext = pFKey->pNextFrom;
4665 sqlite3DbFree(db, pFKey);
4666 }
4667 }
4668 #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
4669
4670 /************** End of fkey.c ************************************************/
4671 /************** Begin file insert.c ******************************************/
4672 /*
4673 ** 2001 September 15
4674 **
4675 ** The author disclaims copyright to this source code. In place of
4676 ** a legal notice, here is a blessing:
4677 **
4678 ** May you do good and not evil.
4679 ** May you find forgiveness for yourself and forgive others.
4680 ** May you share freely, never taking more than you give.
4681 **
4682 *************************************************************************
4683 ** This file contains C code routines that are called by the parser
4684 ** to handle INSERT statements in SQLite.
4685 */
4686 /* #include "sqliteInt.h" */
4687
4688 /*
4689 ** Generate code that will
4690 **
4691 ** (1) acquire a lock for table pTab then
4692 ** (2) open pTab as cursor iCur.
4693 **
4694 ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
4695 ** for that table that is actually opened.
4696 */
4697 SQLITE_PRIVATE void sqlite3OpenTable(
4698 Parse *pParse, /* Generate code into this VDBE */
4699 int iCur, /* The cursor number of the table */
4700 int iDb, /* The database index in sqlite3.aDb[] */
4701 Table *pTab, /* The table to be opened */
4702 int opcode /* OP_OpenRead or OP_OpenWrite */
4703 ){
4704 Vdbe *v;
4705 assert( !IsVirtual(pTab) );
4706 v = sqlite3GetVdbe(pParse);
4707 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
4708 sqlite3TableLock(pParse, iDb, pTab->tnum,
4709 (opcode==OP_OpenWrite)?1:0, pTab->zName);
4710 if( HasRowid(pTab) ){
4711 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
4712 VdbeComment((v, "%s", pTab->zName));
4713 }else{
4714 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
4715 assert( pPk!=0 );
4716 assert( pPk->tnum==pTab->tnum );
4717 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
4718 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
4719 VdbeComment((v, "%s", pTab->zName));
4720 }
4721 }
4722
4723 /*
4724 ** Return a pointer to the column affinity string associated with index
4725 ** pIdx. A column affinity string has one character for each column in
4726 ** the table, according to the affinity of the column:
4727 **
4728 ** Character Column affinity
4729 ** ------------------------------
4730 ** 'A' BLOB
4731 ** 'B' TEXT
4732 ** 'C' NUMERIC
4733 ** 'D' INTEGER
4734 ** 'F' REAL
4735 **
4736 ** An extra 'D' is appended to the end of the string to cover the
4737 ** rowid that appears as the last column in every index.
4738 **
4739 ** Memory for the buffer containing the column index affinity string
4740 ** is managed along with the rest of the Index structure. It will be
4741 ** released when sqlite3DeleteIndex() is called.
4742 */
4743 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
4744 if( !pIdx->zColAff ){
4745 /* The first time a column affinity string for a particular index is
4746 ** required, it is allocated and populated here. It is then stored as
4747 ** a member of the Index structure for subsequent use.
4748 **
4749 ** The column affinity string will eventually be deleted by
4750 ** sqliteDeleteIndex() when the Index structure itself is cleaned
4751 ** up.
4752 */
4753 int n;
4754 Table *pTab = pIdx->pTable;
4755 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
4756 if( !pIdx->zColAff ){
4757 sqlite3OomFault(db);
4758 return 0;
4759 }
4760 for(n=0; n<pIdx->nColumn; n++){
4761 i16 x = pIdx->aiColumn[n];
4762 if( x>=0 ){
4763 pIdx->zColAff[n] = pTab->aCol[x].affinity;
4764 }else if( x==XN_ROWID ){
4765 pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
4766 }else{
4767 char aff;
4768 assert( x==XN_EXPR );
4769 assert( pIdx->aColExpr!=0 );
4770 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
4771 if( aff==0 ) aff = SQLITE_AFF_BLOB;
4772 pIdx->zColAff[n] = aff;
4773 }
4774 }
4775 pIdx->zColAff[n] = 0;
4776 }
4777
4778 return pIdx->zColAff;
4779 }
4780
4781 /*
4782 ** Compute the affinity string for table pTab, if it has not already been
4783 ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
4784 **
4785 ** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
4786 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
4787 ** for register iReg and following. Or if affinities exists and iReg==0,
4788 ** then just set the P4 operand of the previous opcode (which should be
4789 ** an OP_MakeRecord) to the affinity string.
4790 **
4791 ** A column affinity string has one character per column:
4792 **
4793 ** Character Column affinity
4794 ** ------------------------------
4795 ** 'A' BLOB
4796 ** 'B' TEXT
4797 ** 'C' NUMERIC
4798 ** 'D' INTEGER
4799 ** 'E' REAL
4800 */
4801 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
4802 int i;
4803 char *zColAff = pTab->zColAff;
4804 if( zColAff==0 ){
4805 sqlite3 *db = sqlite3VdbeDb(v);
4806 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
4807 if( !zColAff ){
4808 sqlite3OomFault(db);
4809 return;
4810 }
4811
4812 for(i=0; i<pTab->nCol; i++){
4813 zColAff[i] = pTab->aCol[i].affinity;
4814 }
4815 do{
4816 zColAff[i--] = 0;
4817 }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
4818 pTab->zColAff = zColAff;
4819 }
4820 i = sqlite3Strlen30(zColAff);
4821 if( i ){
4822 if( iReg ){
4823 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
4824 }else{
4825 sqlite3VdbeChangeP4(v, -1, zColAff, i);
4826 }
4827 }
4828 }
4829
4830 /*
4831 ** Return non-zero if the table pTab in database iDb or any of its indices
4832 ** have been opened at any point in the VDBE program. This is used to see if
4833 ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
4834 ** run without using a temporary table for the results of the SELECT.
4835 */
4836 static int readsTable(Parse *p, int iDb, Table *pTab){
4837 Vdbe *v = sqlite3GetVdbe(p);
4838 int i;
4839 int iEnd = sqlite3VdbeCurrentAddr(v);
4840 #ifndef SQLITE_OMIT_VIRTUALTABLE
4841 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
4842 #endif
4843
4844 for(i=1; i<iEnd; i++){
4845 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
4846 assert( pOp!=0 );
4847 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
4848 Index *pIndex;
4849 int tnum = pOp->p2;
4850 if( tnum==pTab->tnum ){
4851 return 1;
4852 }
4853 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
4854 if( tnum==pIndex->tnum ){
4855 return 1;
4856 }
4857 }
4858 }
4859 #ifndef SQLITE_OMIT_VIRTUALTABLE
4860 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
4861 assert( pOp->p4.pVtab!=0 );
4862 assert( pOp->p4type==P4_VTAB );
4863 return 1;
4864 }
4865 #endif
4866 }
4867 return 0;
4868 }
4869
4870 #ifndef SQLITE_OMIT_AUTOINCREMENT
4871 /*
4872 ** Locate or create an AutoincInfo structure associated with table pTab
4873 ** which is in database iDb. Return the register number for the register
4874 ** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
4875 ** table. (Also return zero when doing a VACUUM since we do not want to
4876 ** update the AUTOINCREMENT counters during a VACUUM.)
4877 **
4878 ** There is at most one AutoincInfo structure per table even if the
4879 ** same table is autoincremented multiple times due to inserts within
4880 ** triggers. A new AutoincInfo structure is created if this is the
4881 ** first use of table pTab. On 2nd and subsequent uses, the original
4882 ** AutoincInfo structure is used.
4883 **
4884 ** Three memory locations are allocated:
4885 **
4886 ** (1) Register to hold the name of the pTab table.
4887 ** (2) Register to hold the maximum ROWID of pTab.
4888 ** (3) Register to hold the rowid in sqlite_sequence of pTab
4889 **
4890 ** The 2nd register is the one that is returned. That is all the
4891 ** insert routine needs to know about.
4892 */
4893 static int autoIncBegin(
4894 Parse *pParse, /* Parsing context */
4895 int iDb, /* Index of the database holding pTab */
4896 Table *pTab /* The table we are writing to */
4897 ){
4898 int memId = 0; /* Register holding maximum rowid */
4899 if( (pTab->tabFlags & TF_Autoincrement)!=0
4900 && (pParse->db->flags & SQLITE_Vacuum)==0
4901 ){
4902 Parse *pToplevel = sqlite3ParseToplevel(pParse);
4903 AutoincInfo *pInfo;
4904
4905 pInfo = pToplevel->pAinc;
4906 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
4907 if( pInfo==0 ){
4908 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
4909 if( pInfo==0 ) return 0;
4910 pInfo->pNext = pToplevel->pAinc;
4911 pToplevel->pAinc = pInfo;
4912 pInfo->pTab = pTab;
4913 pInfo->iDb = iDb;
4914 pToplevel->nMem++; /* Register to hold name of table */
4915 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
4916 pToplevel->nMem++; /* Rowid in sqlite_sequence */
4917 }
4918 memId = pInfo->regCtr;
4919 }
4920 return memId;
4921 }
4922
4923 /*
4924 ** This routine generates code that will initialize all of the
4925 ** register used by the autoincrement tracker.
4926 */
4927 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
4928 AutoincInfo *p; /* Information about an AUTOINCREMENT */
4929 sqlite3 *db = pParse->db; /* The database connection */
4930 Db *pDb; /* Database only autoinc table */
4931 int memId; /* Register holding max rowid */
4932 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
4933
4934 /* This routine is never called during trigger-generation. It is
4935 ** only called from the top-level */
4936 assert( pParse->pTriggerTab==0 );
4937 assert( sqlite3IsToplevel(pParse) );
4938
4939 assert( v ); /* We failed long ago if this is not so */
4940 for(p = pParse->pAinc; p; p = p->pNext){
4941 static const int iLn = VDBE_OFFSET_LINENO(2);
4942 static const VdbeOpList autoInc[] = {
4943 /* 0 */ {OP_Null, 0, 0, 0},
4944 /* 1 */ {OP_Rewind, 0, 9, 0},
4945 /* 2 */ {OP_Column, 0, 0, 0},
4946 /* 3 */ {OP_Ne, 0, 7, 0},
4947 /* 4 */ {OP_Rowid, 0, 0, 0},
4948 /* 5 */ {OP_Column, 0, 1, 0},
4949 /* 6 */ {OP_Goto, 0, 9, 0},
4950 /* 7 */ {OP_Next, 0, 2, 0},
4951 /* 8 */ {OP_Integer, 0, 0, 0},
4952 /* 9 */ {OP_Close, 0, 0, 0}
4953 };
4954 VdbeOp *aOp;
4955 pDb = &db->aDb[p->iDb];
4956 memId = p->regCtr;
4957 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
4958 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
4959 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
4960 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
4961 if( aOp==0 ) break;
4962 aOp[0].p2 = memId;
4963 aOp[0].p3 = memId+1;
4964 aOp[2].p3 = memId;
4965 aOp[3].p1 = memId-1;
4966 aOp[3].p3 = memId;
4967 aOp[3].p5 = SQLITE_JUMPIFNULL;
4968 aOp[4].p2 = memId+1;
4969 aOp[5].p3 = memId;
4970 aOp[8].p2 = memId;
4971 }
4972 }
4973
4974 /*
4975 ** Update the maximum rowid for an autoincrement calculation.
4976 **
4977 ** This routine should be called when the regRowid register holds a
4978 ** new rowid that is about to be inserted. If that new rowid is
4979 ** larger than the maximum rowid in the memId memory cell, then the
4980 ** memory cell is updated.
4981 */
4982 static void autoIncStep(Parse *pParse, int memId, int regRowid){
4983 if( memId>0 ){
4984 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
4985 }
4986 }
4987
4988 /*
4989 ** This routine generates the code needed to write autoincrement
4990 ** maximum rowid values back into the sqlite_sequence register.
4991 ** Every statement that might do an INSERT into an autoincrement
4992 ** table (either directly or through triggers) needs to call this
4993 ** routine just before the "exit" code.
4994 */
4995 static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
4996 AutoincInfo *p;
4997 Vdbe *v = pParse->pVdbe;
4998 sqlite3 *db = pParse->db;
4999
5000 assert( v );
5001 for(p = pParse->pAinc; p; p = p->pNext){
5002 static const int iLn = VDBE_OFFSET_LINENO(2);
5003 static const VdbeOpList autoIncEnd[] = {
5004 /* 0 */ {OP_NotNull, 0, 2, 0},
5005 /* 1 */ {OP_NewRowid, 0, 0, 0},
5006 /* 2 */ {OP_MakeRecord, 0, 2, 0},
5007 /* 3 */ {OP_Insert, 0, 0, 0},
5008 /* 4 */ {OP_Close, 0, 0, 0}
5009 };
5010 VdbeOp *aOp;
5011 Db *pDb = &db->aDb[p->iDb];
5012 int iRec;
5013 int memId = p->regCtr;
5014
5015 iRec = sqlite3GetTempReg(pParse);
5016 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
5017 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
5018 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
5019 if( aOp==0 ) break;
5020 aOp[0].p1 = memId+1;
5021 aOp[1].p2 = memId+1;
5022 aOp[2].p1 = memId-1;
5023 aOp[2].p3 = iRec;
5024 aOp[3].p2 = iRec;
5025 aOp[3].p3 = memId+1;
5026 aOp[3].p5 = OPFLAG_APPEND;
5027 sqlite3ReleaseTempReg(pParse, iRec);
5028 }
5029 }
5030 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
5031 if( pParse->pAinc ) autoIncrementEnd(pParse);
5032 }
5033 #else
5034 /*
5035 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
5036 ** above are all no-ops
5037 */
5038 # define autoIncBegin(A,B,C) (0)
5039 # define autoIncStep(A,B,C)
5040 #endif /* SQLITE_OMIT_AUTOINCREMENT */
5041
5042
5043 /* Forward declaration */
5044 static int xferOptimization(
5045 Parse *pParse, /* Parser context */
5046 Table *pDest, /* The table we are inserting into */
5047 Select *pSelect, /* A SELECT statement to use as the data source */
5048 int onError, /* How to handle constraint errors */
5049 int iDbDest /* The database of pDest */
5050 );
5051
5052 /*
5053 ** This routine is called to handle SQL of the following forms:
5054 **
5055 ** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
5056 ** insert into TABLE (IDLIST) select
5057 ** insert into TABLE (IDLIST) default values
5058 **
5059 ** The IDLIST following the table name is always optional. If omitted,
5060 ** then a list of all (non-hidden) columns for the table is substituted.
5061 ** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
5062 ** is omitted.
5063 **
5064 ** For the pSelect parameter holds the values to be inserted for the
5065 ** first two forms shown above. A VALUES clause is really just short-hand
5066 ** for a SELECT statement that omits the FROM clause and everything else
5067 ** that follows. If the pSelect parameter is NULL, that means that the
5068 ** DEFAULT VALUES form of the INSERT statement is intended.
5069 **
5070 ** The code generated follows one of four templates. For a simple
5071 ** insert with data coming from a single-row VALUES clause, the code executes
5072 ** once straight down through. Pseudo-code follows (we call this
5073 ** the "1st template"):
5074 **
5075 ** open write cursor to <table> and its indices
5076 ** put VALUES clause expressions into registers
5077 ** write the resulting record into <table>
5078 ** cleanup
5079 **
5080 ** The three remaining templates assume the statement is of the form
5081 **
5082 ** INSERT INTO <table> SELECT ...
5083 **
5084 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
5085 ** in other words if the SELECT pulls all columns from a single table
5086 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
5087 ** if <table2> and <table1> are distinct tables but have identical
5088 ** schemas, including all the same indices, then a special optimization
5089 ** is invoked that copies raw records from <table2> over to <table1>.
5090 ** See the xferOptimization() function for the implementation of this
5091 ** template. This is the 2nd template.
5092 **
5093 ** open a write cursor to <table>
5094 ** open read cursor on <table2>
5095 ** transfer all records in <table2> over to <table>
5096 ** close cursors
5097 ** foreach index on <table>
5098 ** open a write cursor on the <table> index
5099 ** open a read cursor on the corresponding <table2> index
5100 ** transfer all records from the read to the write cursors
5101 ** close cursors
5102 ** end foreach
5103 **
5104 ** The 3rd template is for when the second template does not apply
5105 ** and the SELECT clause does not read from <table> at any time.
5106 ** The generated code follows this template:
5107 **
5108 ** X <- A
5109 ** goto B
5110 ** A: setup for the SELECT
5111 ** loop over the rows in the SELECT
5112 ** load values into registers R..R+n
5113 ** yield X
5114 ** end loop
5115 ** cleanup after the SELECT
5116 ** end-coroutine X
5117 ** B: open write cursor to <table> and its indices
5118 ** C: yield X, at EOF goto D
5119 ** insert the select result into <table> from R..R+n
5120 ** goto C
5121 ** D: cleanup
5122 **
5123 ** The 4th template is used if the insert statement takes its
5124 ** values from a SELECT but the data is being inserted into a table
5125 ** that is also read as part of the SELECT. In the third form,
5126 ** we have to use an intermediate table to store the results of
5127 ** the select. The template is like this:
5128 **
5129 ** X <- A
5130 ** goto B
5131 ** A: setup for the SELECT
5132 ** loop over the tables in the SELECT
5133 ** load value into register R..R+n
5134 ** yield X
5135 ** end loop
5136 ** cleanup after the SELECT
5137 ** end co-routine R
5138 ** B: open temp table
5139 ** L: yield X, at EOF goto M
5140 ** insert row from R..R+n into temp table
5141 ** goto L
5142 ** M: open write cursor to <table> and its indices
5143 ** rewind temp table
5144 ** C: loop over rows of intermediate table
5145 ** transfer values form intermediate table into <table>
5146 ** end loop
5147 ** D: cleanup
5148 */
5149 SQLITE_PRIVATE void sqlite3Insert(
5150 Parse *pParse, /* Parser context */
5151 SrcList *pTabList, /* Name of table into which we are inserting */
5152 Select *pSelect, /* A SELECT statement to use as the data source */
5153 IdList *pColumn, /* Column names corresponding to IDLIST. */
5154 int onError /* How to handle constraint errors */
5155 ){
5156 sqlite3 *db; /* The main database structure */
5157 Table *pTab; /* The table to insert into. aka TABLE */
5158 char *zTab; /* Name of the table into which we are inserting */
5159 int i, j; /* Loop counters */
5160 Vdbe *v; /* Generate code into this virtual machine */
5161 Index *pIdx; /* For looping over indices of the table */
5162 int nColumn; /* Number of columns in the data */
5163 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
5164 int iDataCur = 0; /* VDBE cursor that is the main data repository */
5165 int iIdxCur = 0; /* First index cursor */
5166 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
5167 int endOfLoop; /* Label for the end of the insertion loop */
5168 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
5169 int addrInsTop = 0; /* Jump to label "D" */
5170 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
5171 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
5172 int iDb; /* Index of database holding TABLE */
5173 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
5174 u8 appendFlag = 0; /* True if the insert is likely to be an append */
5175 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
5176 u8 bIdListInOrder; /* True if IDLIST is in table order */
5177 ExprList *pList = 0; /* List of VALUES() to be inserted */
5178
5179 /* Register allocations */
5180 int regFromSelect = 0;/* Base register for data coming from SELECT */
5181 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
5182 int regRowCount = 0; /* Memory cell used for the row counter */
5183 int regIns; /* Block of regs holding rowid+data being inserted */
5184 int regRowid; /* registers holding insert rowid */
5185 int regData; /* register holding first column to insert */
5186 int *aRegIdx = 0; /* One register allocated to each index */
5187
5188 #ifndef SQLITE_OMIT_TRIGGER
5189 int isView; /* True if attempting to insert into a view */
5190 Trigger *pTrigger; /* List of triggers on pTab, if required */
5191 int tmask; /* Mask of trigger times */
5192 #endif
5193
5194 db = pParse->db;
5195 memset(&dest, 0, sizeof(dest));
5196 if( pParse->nErr || db->mallocFailed ){
5197 goto insert_cleanup;
5198 }
5199
5200 /* If the Select object is really just a simple VALUES() list with a
5201 ** single row (the common case) then keep that one row of values
5202 ** and discard the other (unused) parts of the pSelect object
5203 */
5204 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
5205 pList = pSelect->pEList;
5206 pSelect->pEList = 0;
5207 sqlite3SelectDelete(db, pSelect);
5208 pSelect = 0;
5209 }
5210
5211 /* Locate the table into which we will be inserting new information.
5212 */
5213 assert( pTabList->nSrc==1 );
5214 zTab = pTabList->a[0].zName;
5215 if( NEVER(zTab==0) ) goto insert_cleanup;
5216 pTab = sqlite3SrcListLookup(pParse, pTabList);
5217 if( pTab==0 ){
5218 goto insert_cleanup;
5219 }
5220 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
5221 assert( iDb<db->nDb );
5222 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
5223 db->aDb[iDb].zDbSName) ){
5224 goto insert_cleanup;
5225 }
5226 withoutRowid = !HasRowid(pTab);
5227
5228 /* Figure out if we have any triggers and if the table being
5229 ** inserted into is a view
5230 */
5231 #ifndef SQLITE_OMIT_TRIGGER
5232 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
5233 isView = pTab->pSelect!=0;
5234 #else
5235 # define pTrigger 0
5236 # define tmask 0
5237 # define isView 0
5238 #endif
5239 #ifdef SQLITE_OMIT_VIEW
5240 # undef isView
5241 # define isView 0
5242 #endif
5243 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
5244
5245 /* If pTab is really a view, make sure it has been initialized.
5246 ** ViewGetColumnNames() is a no-op if pTab is not a view.
5247 */
5248 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
5249 goto insert_cleanup;
5250 }
5251
5252 /* Cannot insert into a read-only table.
5253 */
5254 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
5255 goto insert_cleanup;
5256 }
5257
5258 /* Allocate a VDBE
5259 */
5260 v = sqlite3GetVdbe(pParse);
5261 if( v==0 ) goto insert_cleanup;
5262 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
5263 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
5264
5265 #ifndef SQLITE_OMIT_XFER_OPT
5266 /* If the statement is of the form
5267 **
5268 ** INSERT INTO <table1> SELECT * FROM <table2>;
5269 **
5270 ** Then special optimizations can be applied that make the transfer
5271 ** very fast and which reduce fragmentation of indices.
5272 **
5273 ** This is the 2nd template.
5274 */
5275 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
5276 assert( !pTrigger );
5277 assert( pList==0 );
5278 goto insert_end;
5279 }
5280 #endif /* SQLITE_OMIT_XFER_OPT */
5281
5282 /* If this is an AUTOINCREMENT table, look up the sequence number in the
5283 ** sqlite_sequence table and store it in memory cell regAutoinc.
5284 */
5285 regAutoinc = autoIncBegin(pParse, iDb, pTab);
5286
5287 /* Allocate registers for holding the rowid of the new row,
5288 ** the content of the new row, and the assembled row record.
5289 */
5290 regRowid = regIns = pParse->nMem+1;
5291 pParse->nMem += pTab->nCol + 1;
5292 if( IsVirtual(pTab) ){
5293 regRowid++;
5294 pParse->nMem++;
5295 }
5296 regData = regRowid+1;
5297
5298 /* If the INSERT statement included an IDLIST term, then make sure
5299 ** all elements of the IDLIST really are columns of the table and
5300 ** remember the column indices.
5301 **
5302 ** If the table has an INTEGER PRIMARY KEY column and that column
5303 ** is named in the IDLIST, then record in the ipkColumn variable
5304 ** the index into IDLIST of the primary key column. ipkColumn is
5305 ** the index of the primary key as it appears in IDLIST, not as
5306 ** is appears in the original table. (The index of the INTEGER
5307 ** PRIMARY KEY in the original table is pTab->iPKey.)
5308 */
5309 bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0;
5310 if( pColumn ){
5311 for(i=0; i<pColumn->nId; i++){
5312 pColumn->a[i].idx = -1;
5313 }
5314 for(i=0; i<pColumn->nId; i++){
5315 for(j=0; j<pTab->nCol; j++){
5316 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
5317 pColumn->a[i].idx = j;
5318 if( i!=j ) bIdListInOrder = 0;
5319 if( j==pTab->iPKey ){
5320 ipkColumn = i; assert( !withoutRowid );
5321 }
5322 break;
5323 }
5324 }
5325 if( j>=pTab->nCol ){
5326 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
5327 ipkColumn = i;
5328 bIdListInOrder = 0;
5329 }else{
5330 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
5331 pTabList, 0, pColumn->a[i].zName);
5332 pParse->checkSchema = 1;
5333 goto insert_cleanup;
5334 }
5335 }
5336 }
5337 }
5338
5339 /* Figure out how many columns of data are supplied. If the data
5340 ** is coming from a SELECT statement, then generate a co-routine that
5341 ** produces a single row of the SELECT on each invocation. The
5342 ** co-routine is the common header to the 3rd and 4th templates.
5343 */
5344 if( pSelect ){
5345 /* Data is coming from a SELECT or from a multi-row VALUES clause.
5346 ** Generate a co-routine to run the SELECT. */
5347 int regYield; /* Register holding co-routine entry-point */
5348 int addrTop; /* Top of the co-routine */
5349 int rc; /* Result code */
5350
5351 regYield = ++pParse->nMem;
5352 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
5353 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
5354 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
5355 dest.iSdst = bIdListInOrder ? regData : 0;
5356 dest.nSdst = pTab->nCol;
5357 rc = sqlite3Select(pParse, pSelect, &dest);
5358 regFromSelect = dest.iSdst;
5359 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
5360 sqlite3VdbeEndCoroutine(v, regYield);
5361 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
5362 assert( pSelect->pEList );
5363 nColumn = pSelect->pEList->nExpr;
5364
5365 /* Set useTempTable to TRUE if the result of the SELECT statement
5366 ** should be written into a temporary table (template 4). Set to
5367 ** FALSE if each output row of the SELECT can be written directly into
5368 ** the destination table (template 3).
5369 **
5370 ** A temp table must be used if the table being updated is also one
5371 ** of the tables being read by the SELECT statement. Also use a
5372 ** temp table in the case of row triggers.
5373 */
5374 if( pTrigger || readsTable(pParse, iDb, pTab) ){
5375 useTempTable = 1;
5376 }
5377
5378 if( useTempTable ){
5379 /* Invoke the coroutine to extract information from the SELECT
5380 ** and add it to a transient table srcTab. The code generated
5381 ** here is from the 4th template:
5382 **
5383 ** B: open temp table
5384 ** L: yield X, goto M at EOF
5385 ** insert row from R..R+n into temp table
5386 ** goto L
5387 ** M: ...
5388 */
5389 int regRec; /* Register to hold packed record */
5390 int regTempRowid; /* Register to hold temp table ROWID */
5391 int addrL; /* Label "L" */
5392
5393 srcTab = pParse->nTab++;
5394 regRec = sqlite3GetTempReg(pParse);
5395 regTempRowid = sqlite3GetTempReg(pParse);
5396 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
5397 addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
5398 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
5399 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
5400 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
5401 sqlite3VdbeGoto(v, addrL);
5402 sqlite3VdbeJumpHere(v, addrL);
5403 sqlite3ReleaseTempReg(pParse, regRec);
5404 sqlite3ReleaseTempReg(pParse, regTempRowid);
5405 }
5406 }else{
5407 /* This is the case if the data for the INSERT is coming from a
5408 ** single-row VALUES clause
5409 */
5410 NameContext sNC;
5411 memset(&sNC, 0, sizeof(sNC));
5412 sNC.pParse = pParse;
5413 srcTab = -1;
5414 assert( useTempTable==0 );
5415 if( pList ){
5416 nColumn = pList->nExpr;
5417 if( sqlite3ResolveExprListNames(&sNC, pList) ){
5418 goto insert_cleanup;
5419 }
5420 }else{
5421 nColumn = 0;
5422 }
5423 }
5424
5425 /* If there is no IDLIST term but the table has an integer primary
5426 ** key, the set the ipkColumn variable to the integer primary key
5427 ** column index in the original table definition.
5428 */
5429 if( pColumn==0 && nColumn>0 ){
5430 ipkColumn = pTab->iPKey;
5431 }
5432
5433 /* Make sure the number of columns in the source data matches the number
5434 ** of columns to be inserted into the table.
5435 */
5436 for(i=0; i<pTab->nCol; i++){
5437 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
5438 }
5439 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
5440 sqlite3ErrorMsg(pParse,
5441 "table %S has %d columns but %d values were supplied",
5442 pTabList, 0, pTab->nCol-nHidden, nColumn);
5443 goto insert_cleanup;
5444 }
5445 if( pColumn!=0 && nColumn!=pColumn->nId ){
5446 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
5447 goto insert_cleanup;
5448 }
5449
5450 /* Initialize the count of rows to be inserted
5451 */
5452 if( db->flags & SQLITE_CountRows ){
5453 regRowCount = ++pParse->nMem;
5454 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
5455 }
5456
5457 /* If this is not a view, open the table and and all indices */
5458 if( !isView ){
5459 int nIdx;
5460 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
5461 &iDataCur, &iIdxCur);
5462 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1));
5463 if( aRegIdx==0 ){
5464 goto insert_cleanup;
5465 }
5466 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
5467 assert( pIdx );
5468 aRegIdx[i] = ++pParse->nMem;
5469 pParse->nMem += pIdx->nColumn;
5470 }
5471 }
5472
5473 /* This is the top of the main insertion loop */
5474 if( useTempTable ){
5475 /* This block codes the top of loop only. The complete loop is the
5476 ** following pseudocode (template 4):
5477 **
5478 ** rewind temp table, if empty goto D
5479 ** C: loop over rows of intermediate table
5480 ** transfer values form intermediate table into <table>
5481 ** end loop
5482 ** D: ...
5483 */
5484 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
5485 addrCont = sqlite3VdbeCurrentAddr(v);
5486 }else if( pSelect ){
5487 /* This block codes the top of loop only. The complete loop is the
5488 ** following pseudocode (template 3):
5489 **
5490 ** C: yield X, at EOF goto D
5491 ** insert the select result into <table> from R..R+n
5492 ** goto C
5493 ** D: ...
5494 */
5495 addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
5496 VdbeCoverage(v);
5497 }
5498
5499 /* Run the BEFORE and INSTEAD OF triggers, if there are any
5500 */
5501 endOfLoop = sqlite3VdbeMakeLabel(v);
5502 if( tmask & TRIGGER_BEFORE ){
5503 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
5504
5505 /* build the NEW.* reference row. Note that if there is an INTEGER
5506 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
5507 ** translated into a unique ID for the row. But on a BEFORE trigger,
5508 ** we do not know what the unique ID will be (because the insert has
5509 ** not happened yet) so we substitute a rowid of -1
5510 */
5511 if( ipkColumn<0 ){
5512 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
5513 }else{
5514 int addr1;
5515 assert( !withoutRowid );
5516 if( useTempTable ){
5517 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
5518 }else{
5519 assert( pSelect==0 ); /* Otherwise useTempTable is true */
5520 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
5521 }
5522 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
5523 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
5524 sqlite3VdbeJumpHere(v, addr1);
5525 sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
5526 }
5527
5528 /* Cannot have triggers on a virtual table. If it were possible,
5529 ** this block would have to account for hidden column.
5530 */
5531 assert( !IsVirtual(pTab) );
5532
5533 /* Create the new column data
5534 */
5535 for(i=j=0; i<pTab->nCol; i++){
5536 if( pColumn ){
5537 for(j=0; j<pColumn->nId; j++){
5538 if( pColumn->a[j].idx==i ) break;
5539 }
5540 }
5541 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
5542 || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
5543 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
5544 }else if( useTempTable ){
5545 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
5546 }else{
5547 assert( pSelect==0 ); /* Otherwise useTempTable is true */
5548 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
5549 }
5550 if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
5551 }
5552
5553 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
5554 ** do not attempt any conversions before assembling the record.
5555 ** If this is a real table, attempt conversions as required by the
5556 ** table column affinities.
5557 */
5558 if( !isView ){
5559 sqlite3TableAffinity(v, pTab, regCols+1);
5560 }
5561
5562 /* Fire BEFORE or INSTEAD OF triggers */
5563 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
5564 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
5565
5566 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
5567 }
5568
5569 /* Compute the content of the next row to insert into a range of
5570 ** registers beginning at regIns.
5571 */
5572 if( !isView ){
5573 if( IsVirtual(pTab) ){
5574 /* The row that the VUpdate opcode will delete: none */
5575 sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
5576 }
5577 if( ipkColumn>=0 ){
5578 if( useTempTable ){
5579 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
5580 }else if( pSelect ){
5581 sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
5582 }else{
5583 VdbeOp *pOp;
5584 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
5585 pOp = sqlite3VdbeGetOp(v, -1);
5586 if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
5587 appendFlag = 1;
5588 pOp->opcode = OP_NewRowid;
5589 pOp->p1 = iDataCur;
5590 pOp->p2 = regRowid;
5591 pOp->p3 = regAutoinc;
5592 }
5593 }
5594 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
5595 ** to generate a unique primary key value.
5596 */
5597 if( !appendFlag ){
5598 int addr1;
5599 if( !IsVirtual(pTab) ){
5600 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
5601 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
5602 sqlite3VdbeJumpHere(v, addr1);
5603 }else{
5604 addr1 = sqlite3VdbeCurrentAddr(v);
5605 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
5606 }
5607 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
5608 }
5609 }else if( IsVirtual(pTab) || withoutRowid ){
5610 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
5611 }else{
5612 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
5613 appendFlag = 1;
5614 }
5615 autoIncStep(pParse, regAutoinc, regRowid);
5616
5617 /* Compute data for all columns of the new entry, beginning
5618 ** with the first column.
5619 */
5620 nHidden = 0;
5621 for(i=0; i<pTab->nCol; i++){
5622 int iRegStore = regRowid+1+i;
5623 if( i==pTab->iPKey ){
5624 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
5625 ** Whenever this column is read, the rowid will be substituted
5626 ** in its place. Hence, fill this column with a NULL to avoid
5627 ** taking up data space with information that will never be used.
5628 ** As there may be shallow copies of this value, make it a soft-NULL */
5629 sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
5630 continue;
5631 }
5632 if( pColumn==0 ){
5633 if( IsHiddenColumn(&pTab->aCol[i]) ){
5634 j = -1;
5635 nHidden++;
5636 }else{
5637 j = i - nHidden;
5638 }
5639 }else{
5640 for(j=0; j<pColumn->nId; j++){
5641 if( pColumn->a[j].idx==i ) break;
5642 }
5643 }
5644 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
5645 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
5646 }else if( useTempTable ){
5647 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
5648 }else if( pSelect ){
5649 if( regFromSelect!=regData ){
5650 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
5651 }
5652 }else{
5653 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
5654 }
5655 }
5656
5657 /* Generate code to check constraints and generate index keys and
5658 ** do the insertion.
5659 */
5660 #ifndef SQLITE_OMIT_VIRTUALTABLE
5661 if( IsVirtual(pTab) ){
5662 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
5663 sqlite3VtabMakeWritable(pParse, pTab);
5664 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
5665 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
5666 sqlite3MayAbort(pParse);
5667 }else
5668 #endif
5669 {
5670 int isReplace; /* Set to true if constraints may cause a replace */
5671 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
5672 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
5673 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0
5674 );
5675 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
5676
5677 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
5678 ** constraints or (b) there are no triggers and this table is not a
5679 ** parent table in a foreign key constraint. It is safe to set the
5680 ** flag in the second case as if any REPLACE constraint is hit, an
5681 ** OP_Delete or OP_IdxDelete instruction will be executed on each
5682 ** cursor that is disturbed. And these instructions both clear the
5683 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
5684 ** functionality. */
5685 bUseSeek = (isReplace==0 || (pTrigger==0 &&
5686 ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0)
5687 ));
5688 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
5689 regIns, aRegIdx, 0, appendFlag, bUseSeek
5690 );
5691 }
5692 }
5693
5694 /* Update the count of rows that are inserted
5695 */
5696 if( (db->flags & SQLITE_CountRows)!=0 ){
5697 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
5698 }
5699
5700 if( pTrigger ){
5701 /* Code AFTER triggers */
5702 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
5703 pTab, regData-2-pTab->nCol, onError, endOfLoop);
5704 }
5705
5706 /* The bottom of the main insertion loop, if the data source
5707 ** is a SELECT statement.
5708 */
5709 sqlite3VdbeResolveLabel(v, endOfLoop);
5710 if( useTempTable ){
5711 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
5712 sqlite3VdbeJumpHere(v, addrInsTop);
5713 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
5714 }else if( pSelect ){
5715 sqlite3VdbeGoto(v, addrCont);
5716 sqlite3VdbeJumpHere(v, addrInsTop);
5717 }
5718
5719 insert_end:
5720 /* Update the sqlite_sequence table by storing the content of the
5721 ** maximum rowid counter values recorded while inserting into
5722 ** autoincrement tables.
5723 */
5724 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
5725 sqlite3AutoincrementEnd(pParse);
5726 }
5727
5728 /*
5729 ** Return the number of rows inserted. If this routine is
5730 ** generating code because of a call to sqlite3NestedParse(), do not
5731 ** invoke the callback function.
5732 */
5733 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
5734 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
5735 sqlite3VdbeSetNumCols(v, 1);
5736 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
5737 }
5738
5739 insert_cleanup:
5740 sqlite3SrcListDelete(db, pTabList);
5741 sqlite3ExprListDelete(db, pList);
5742 sqlite3SelectDelete(db, pSelect);
5743 sqlite3IdListDelete(db, pColumn);
5744 sqlite3DbFree(db, aRegIdx);
5745 }
5746
5747 /* Make sure "isView" and other macros defined above are undefined. Otherwise
5748 ** they may interfere with compilation of other functions in this file
5749 ** (or in another file, if this file becomes part of the amalgamation). */
5750 #ifdef isView
5751 #undef isView
5752 #endif
5753 #ifdef pTrigger
5754 #undef pTrigger
5755 #endif
5756 #ifdef tmask
5757 #undef tmask
5758 #endif
5759
5760 /*
5761 ** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
5762 */
5763 #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
5764 #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
5765
5766 /* This is the Walker callback from checkConstraintUnchanged(). Set
5767 ** bit 0x01 of pWalker->eCode if
5768 ** pWalker->eCode to 0 if this expression node references any of the
5769 ** columns that are being modifed by an UPDATE statement.
5770 */
5771 static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
5772 if( pExpr->op==TK_COLUMN ){
5773 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
5774 if( pExpr->iColumn>=0 ){
5775 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
5776 pWalker->eCode |= CKCNSTRNT_COLUMN;
5777 }
5778 }else{
5779 pWalker->eCode |= CKCNSTRNT_ROWID;
5780 }
5781 }
5782 return WRC_Continue;
5783 }
5784
5785 /*
5786 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
5787 ** only columns that are modified by the UPDATE are those for which
5788 ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
5789 **
5790 ** Return true if CHECK constraint pExpr does not use any of the
5791 ** changing columns (or the rowid if it is changing). In other words,
5792 ** return true if this CHECK constraint can be skipped when validating
5793 ** the new row in the UPDATE statement.
5794 */
5795 static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){
5796 Walker w;
5797 memset(&w, 0, sizeof(w));
5798 w.eCode = 0;
5799 w.xExprCallback = checkConstraintExprNode;
5800 w.u.aiCol = aiChng;
5801 sqlite3WalkExpr(&w, pExpr);
5802 if( !chngRowid ){
5803 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
5804 w.eCode &= ~CKCNSTRNT_ROWID;
5805 }
5806 testcase( w.eCode==0 );
5807 testcase( w.eCode==CKCNSTRNT_COLUMN );
5808 testcase( w.eCode==CKCNSTRNT_ROWID );
5809 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
5810 return !w.eCode;
5811 }
5812
5813 /*
5814 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
5815 ** on table pTab.
5816 **
5817 ** The regNewData parameter is the first register in a range that contains
5818 ** the data to be inserted or the data after the update. There will be
5819 ** pTab->nCol+1 registers in this range. The first register (the one
5820 ** that regNewData points to) will contain the new rowid, or NULL in the
5821 ** case of a WITHOUT ROWID table. The second register in the range will
5822 ** contain the content of the first table column. The third register will
5823 ** contain the content of the second table column. And so forth.
5824 **
5825 ** The regOldData parameter is similar to regNewData except that it contains
5826 ** the data prior to an UPDATE rather than afterwards. regOldData is zero
5827 ** for an INSERT. This routine can distinguish between UPDATE and INSERT by
5828 ** checking regOldData for zero.
5829 **
5830 ** For an UPDATE, the pkChng boolean is true if the true primary key (the
5831 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
5832 ** might be modified by the UPDATE. If pkChng is false, then the key of
5833 ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
5834 **
5835 ** For an INSERT, the pkChng boolean indicates whether or not the rowid
5836 ** was explicitly specified as part of the INSERT statement. If pkChng
5837 ** is zero, it means that the either rowid is computed automatically or
5838 ** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
5839 ** pkChng will only be true if the INSERT statement provides an integer
5840 ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
5841 **
5842 ** The code generated by this routine will store new index entries into
5843 ** registers identified by aRegIdx[]. No index entry is created for
5844 ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
5845 ** the same as the order of indices on the linked list of indices
5846 ** at pTab->pIndex.
5847 **
5848 ** The caller must have already opened writeable cursors on the main
5849 ** table and all applicable indices (that is to say, all indices for which
5850 ** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
5851 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
5852 ** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
5853 ** for the first index in the pTab->pIndex list. Cursors for other indices
5854 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
5855 **
5856 ** This routine also generates code to check constraints. NOT NULL,
5857 ** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
5858 ** then the appropriate action is performed. There are five possible
5859 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
5860 **
5861 ** Constraint type Action What Happens
5862 ** --------------- ---------- ----------------------------------------
5863 ** any ROLLBACK The current transaction is rolled back and
5864 ** sqlite3_step() returns immediately with a
5865 ** return code of SQLITE_CONSTRAINT.
5866 **
5867 ** any ABORT Back out changes from the current command
5868 ** only (do not do a complete rollback) then
5869 ** cause sqlite3_step() to return immediately
5870 ** with SQLITE_CONSTRAINT.
5871 **
5872 ** any FAIL Sqlite3_step() returns immediately with a
5873 ** return code of SQLITE_CONSTRAINT. The
5874 ** transaction is not rolled back and any
5875 ** changes to prior rows are retained.
5876 **
5877 ** any IGNORE The attempt in insert or update the current
5878 ** row is skipped, without throwing an error.
5879 ** Processing continues with the next row.
5880 ** (There is an immediate jump to ignoreDest.)
5881 **
5882 ** NOT NULL REPLACE The NULL value is replace by the default
5883 ** value for that column. If the default value
5884 ** is NULL, the action is the same as ABORT.
5885 **
5886 ** UNIQUE REPLACE The other row that conflicts with the row
5887 ** being inserted is removed.
5888 **
5889 ** CHECK REPLACE Illegal. The results in an exception.
5890 **
5891 ** Which action to take is determined by the overrideError parameter.
5892 ** Or if overrideError==OE_Default, then the pParse->onError parameter
5893 ** is used. Or if pParse->onError==OE_Default then the onError value
5894 ** for the constraint is used.
5895 */
5896 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
5897 Parse *pParse, /* The parser context */
5898 Table *pTab, /* The table being inserted or updated */
5899 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
5900 int iDataCur, /* Canonical data cursor (main table or PK index) */
5901 int iIdxCur, /* First index cursor */
5902 int regNewData, /* First register in a range holding values to insert */
5903 int regOldData, /* Previous content. 0 for INSERTs */
5904 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
5905 u8 overrideError, /* Override onError to this if not OE_Default */
5906 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
5907 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
5908 int *aiChng /* column i is unchanged if aiChng[i]<0 */
5909 ){
5910 Vdbe *v; /* VDBE under constrution */
5911 Index *pIdx; /* Pointer to one of the indices */
5912 Index *pPk = 0; /* The PRIMARY KEY index */
5913 sqlite3 *db; /* Database connection */
5914 int i; /* loop counter */
5915 int ix; /* Index loop counter */
5916 int nCol; /* Number of columns */
5917 int onError; /* Conflict resolution strategy */
5918 int addr1; /* Address of jump instruction */
5919 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
5920 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
5921 int ipkTop = 0; /* Top of the rowid change constraint check */
5922 int ipkBottom = 0; /* Bottom of the rowid change constraint check */
5923 u8 isUpdate; /* True if this is an UPDATE operation */
5924 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
5925
5926 isUpdate = regOldData!=0;
5927 db = pParse->db;
5928 v = sqlite3GetVdbe(pParse);
5929 assert( v!=0 );
5930 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
5931 nCol = pTab->nCol;
5932
5933 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
5934 ** normal rowid tables. nPkField is the number of key fields in the
5935 ** pPk index or 1 for a rowid table. In other words, nPkField is the
5936 ** number of fields in the true primary key of the table. */
5937 if( HasRowid(pTab) ){
5938 pPk = 0;
5939 nPkField = 1;
5940 }else{
5941 pPk = sqlite3PrimaryKeyIndex(pTab);
5942 nPkField = pPk->nKeyCol;
5943 }
5944
5945 /* Record that this module has started */
5946 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
5947 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
5948
5949 /* Test all NOT NULL constraints.
5950 */
5951 for(i=0; i<nCol; i++){
5952 if( i==pTab->iPKey ){
5953 continue; /* ROWID is never NULL */
5954 }
5955 if( aiChng && aiChng[i]<0 ){
5956 /* Don't bother checking for NOT NULL on columns that do not change */
5957 continue;
5958 }
5959 onError = pTab->aCol[i].notNull;
5960 if( onError==OE_None ) continue; /* This column is allowed to be NULL */
5961 if( overrideError!=OE_Default ){
5962 onError = overrideError;
5963 }else if( onError==OE_Default ){
5964 onError = OE_Abort;
5965 }
5966 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
5967 onError = OE_Abort;
5968 }
5969 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
5970 || onError==OE_Ignore || onError==OE_Replace );
5971 switch( onError ){
5972 case OE_Abort:
5973 sqlite3MayAbort(pParse);
5974 /* Fall through */
5975 case OE_Rollback:
5976 case OE_Fail: {
5977 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
5978 pTab->aCol[i].zName);
5979 sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
5980 regNewData+1+i);
5981 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
5982 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
5983 VdbeCoverage(v);
5984 break;
5985 }
5986 case OE_Ignore: {
5987 sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
5988 VdbeCoverage(v);
5989 break;
5990 }
5991 default: {
5992 assert( onError==OE_Replace );
5993 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i);
5994 VdbeCoverage(v);
5995 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
5996 sqlite3VdbeJumpHere(v, addr1);
5997 break;
5998 }
5999 }
6000 }
6001
6002 /* Test all CHECK constraints
6003 */
6004 #ifndef SQLITE_OMIT_CHECK
6005 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
6006 ExprList *pCheck = pTab->pCheck;
6007 pParse->ckBase = regNewData+1;
6008 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
6009 for(i=0; i<pCheck->nExpr; i++){
6010 int allOk;
6011 Expr *pExpr = pCheck->a[i].pExpr;
6012 if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
6013 allOk = sqlite3VdbeMakeLabel(v);
6014 sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
6015 if( onError==OE_Ignore ){
6016 sqlite3VdbeGoto(v, ignoreDest);
6017 }else{
6018 char *zName = pCheck->a[i].zName;
6019 if( zName==0 ) zName = pTab->zName;
6020 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
6021 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
6022 onError, zName, P4_TRANSIENT,
6023 P5_ConstraintCheck);
6024 }
6025 sqlite3VdbeResolveLabel(v, allOk);
6026 }
6027 }
6028 #endif /* !defined(SQLITE_OMIT_CHECK) */
6029
6030 /* If rowid is changing, make sure the new rowid does not previously
6031 ** exist in the table.
6032 */
6033 if( pkChng && pPk==0 ){
6034 int addrRowidOk = sqlite3VdbeMakeLabel(v);
6035
6036 /* Figure out what action to take in case of a rowid collision */
6037 onError = pTab->keyConf;
6038 if( overrideError!=OE_Default ){
6039 onError = overrideError;
6040 }else if( onError==OE_Default ){
6041 onError = OE_Abort;
6042 }
6043
6044 if( isUpdate ){
6045 /* pkChng!=0 does not mean that the rowid has changed, only that
6046 ** it might have changed. Skip the conflict logic below if the rowid
6047 ** is unchanged. */
6048 sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
6049 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
6050 VdbeCoverage(v);
6051 }
6052
6053 /* If the response to a rowid conflict is REPLACE but the response
6054 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
6055 ** to defer the running of the rowid conflict checking until after
6056 ** the UNIQUE constraints have run.
6057 */
6058 if( onError==OE_Replace && overrideError!=OE_Replace ){
6059 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
6060 if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
6061 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
6062 break;
6063 }
6064 }
6065 }
6066
6067 /* Check to see if the new rowid already exists in the table. Skip
6068 ** the following conflict logic if it does not. */
6069 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
6070 VdbeCoverage(v);
6071
6072 /* Generate code that deals with a rowid collision */
6073 switch( onError ){
6074 default: {
6075 onError = OE_Abort;
6076 /* Fall thru into the next case */
6077 }
6078 case OE_Rollback:
6079 case OE_Abort:
6080 case OE_Fail: {
6081 sqlite3RowidConstraint(pParse, onError, pTab);
6082 break;
6083 }
6084 case OE_Replace: {
6085 /* If there are DELETE triggers on this table and the
6086 ** recursive-triggers flag is set, call GenerateRowDelete() to
6087 ** remove the conflicting row from the table. This will fire
6088 ** the triggers and remove both the table and index b-tree entries.
6089 **
6090 ** Otherwise, if there are no triggers or the recursive-triggers
6091 ** flag is not set, but the table has one or more indexes, call
6092 ** GenerateRowIndexDelete(). This removes the index b-tree entries
6093 ** only. The table b-tree entry will be replaced by the new entry
6094 ** when it is inserted.
6095 **
6096 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
6097 ** also invoke MultiWrite() to indicate that this VDBE may require
6098 ** statement rollback (if the statement is aborted after the delete
6099 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
6100 ** but being more selective here allows statements like:
6101 **
6102 ** REPLACE INTO t(rowid) VALUES($newrowid)
6103 **
6104 ** to run without a statement journal if there are no indexes on the
6105 ** table.
6106 */
6107 Trigger *pTrigger = 0;
6108 if( db->flags&SQLITE_RecTriggers ){
6109 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
6110 }
6111 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
6112 sqlite3MultiWrite(pParse);
6113 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
6114 regNewData, 1, 0, OE_Replace, 1, -1);
6115 }else{
6116 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
6117 if( HasRowid(pTab) ){
6118 /* This OP_Delete opcode fires the pre-update-hook only. It does
6119 ** not modify the b-tree. It is more efficient to let the coming
6120 ** OP_Insert replace the existing entry than it is to delete the
6121 ** existing entry and then insert a new one. */
6122 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
6123 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
6124 }
6125 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
6126 if( pTab->pIndex ){
6127 sqlite3MultiWrite(pParse);
6128 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
6129 }
6130 }
6131 seenReplace = 1;
6132 break;
6133 }
6134 case OE_Ignore: {
6135 /*assert( seenReplace==0 );*/
6136 sqlite3VdbeGoto(v, ignoreDest);
6137 break;
6138 }
6139 }
6140 sqlite3VdbeResolveLabel(v, addrRowidOk);
6141 if( ipkTop ){
6142 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
6143 sqlite3VdbeJumpHere(v, ipkTop);
6144 }
6145 }
6146
6147 /* Test all UNIQUE constraints by creating entries for each UNIQUE
6148 ** index and making sure that duplicate entries do not already exist.
6149 ** Compute the revised record entries for indices as we go.
6150 **
6151 ** This loop also handles the case of the PRIMARY KEY index for a
6152 ** WITHOUT ROWID table.
6153 */
6154 for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
6155 int regIdx; /* Range of registers hold conent for pIdx */
6156 int regR; /* Range of registers holding conflicting PK */
6157 int iThisCur; /* Cursor for this UNIQUE index */
6158 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
6159
6160 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
6161 if( bAffinityDone==0 ){
6162 sqlite3TableAffinity(v, pTab, regNewData+1);
6163 bAffinityDone = 1;
6164 }
6165 iThisCur = iIdxCur+ix;
6166 addrUniqueOk = sqlite3VdbeMakeLabel(v);
6167
6168 /* Skip partial indices for which the WHERE clause is not true */
6169 if( pIdx->pPartIdxWhere ){
6170 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
6171 pParse->ckBase = regNewData+1;
6172 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
6173 SQLITE_JUMPIFNULL);
6174 pParse->ckBase = 0;
6175 }
6176
6177 /* Create a record for this index entry as it should appear after
6178 ** the insert or update. Store that record in the aRegIdx[ix] register
6179 */
6180 regIdx = aRegIdx[ix]+1;
6181 for(i=0; i<pIdx->nColumn; i++){
6182 int iField = pIdx->aiColumn[i];
6183 int x;
6184 if( iField==XN_EXPR ){
6185 pParse->ckBase = regNewData+1;
6186 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
6187 pParse->ckBase = 0;
6188 VdbeComment((v, "%s column %d", pIdx->zName, i));
6189 }else{
6190 if( iField==XN_ROWID || iField==pTab->iPKey ){
6191 x = regNewData;
6192 }else{
6193 x = iField + regNewData + 1;
6194 }
6195 sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
6196 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
6197 }
6198 }
6199 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
6200 VdbeComment((v, "for %s", pIdx->zName));
6201
6202 /* In an UPDATE operation, if this index is the PRIMARY KEY index
6203 ** of a WITHOUT ROWID table and there has been no change the
6204 ** primary key, then no collision is possible. The collision detection
6205 ** logic below can all be skipped. */
6206 if( isUpdate && pPk==pIdx && pkChng==0 ){
6207 sqlite3VdbeResolveLabel(v, addrUniqueOk);
6208 continue;
6209 }
6210
6211 /* Find out what action to take in case there is a uniqueness conflict */
6212 onError = pIdx->onError;
6213 if( onError==OE_None ){
6214 sqlite3VdbeResolveLabel(v, addrUniqueOk);
6215 continue; /* pIdx is not a UNIQUE index */
6216 }
6217 if( overrideError!=OE_Default ){
6218 onError = overrideError;
6219 }else if( onError==OE_Default ){
6220 onError = OE_Abort;
6221 }
6222
6223 /* Collision detection may be omitted if all of the following are true:
6224 ** (1) The conflict resolution algorithm is REPLACE
6225 ** (2) The table is a WITHOUT ROWID table
6226 ** (3) There are no secondary indexes on the table
6227 ** (4) No delete triggers need to be fired if there is a conflict
6228 ** (5) No FK constraint counters need to be updated if a conflict occurs.
6229 */
6230 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
6231 && pPk==pIdx /* Condition 2 */
6232 && onError==OE_Replace /* Condition 1 */
6233 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
6234 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
6235 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
6236 (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
6237 ){
6238 sqlite3VdbeResolveLabel(v, addrUniqueOk);
6239 continue;
6240 }
6241
6242 /* Check to see if the new index entry will be unique */
6243 sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
6244 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
6245
6246 /* Generate code to handle collisions */
6247 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
6248 if( isUpdate || onError==OE_Replace ){
6249 if( HasRowid(pTab) ){
6250 sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
6251 /* Conflict only if the rowid of the existing index entry
6252 ** is different from old-rowid */
6253 if( isUpdate ){
6254 sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
6255 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
6256 VdbeCoverage(v);
6257 }
6258 }else{
6259 int x;
6260 /* Extract the PRIMARY KEY from the end of the index entry and
6261 ** store it in registers regR..regR+nPk-1 */
6262 if( pIdx!=pPk ){
6263 for(i=0; i<pPk->nKeyCol; i++){
6264 assert( pPk->aiColumn[i]>=0 );
6265 x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
6266 sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
6267 VdbeComment((v, "%s.%s", pTab->zName,
6268 pTab->aCol[pPk->aiColumn[i]].zName));
6269 }
6270 }
6271 if( isUpdate ){
6272 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
6273 ** table, only conflict if the new PRIMARY KEY values are actually
6274 ** different from the old.
6275 **
6276 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
6277 ** of the matched index row are different from the original PRIMARY
6278 ** KEY values of this row before the update. */
6279 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
6280 int op = OP_Ne;
6281 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
6282
6283 for(i=0; i<pPk->nKeyCol; i++){
6284 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
6285 x = pPk->aiColumn[i];
6286 assert( x>=0 );
6287 if( i==(pPk->nKeyCol-1) ){
6288 addrJump = addrUniqueOk;
6289 op = OP_Eq;
6290 }
6291 sqlite3VdbeAddOp4(v, op,
6292 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
6293 );
6294 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
6295 VdbeCoverageIf(v, op==OP_Eq);
6296 VdbeCoverageIf(v, op==OP_Ne);
6297 }
6298 }
6299 }
6300 }
6301
6302 /* Generate code that executes if the new index entry is not unique */
6303 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
6304 || onError==OE_Ignore || onError==OE_Replace );
6305 switch( onError ){
6306 case OE_Rollback:
6307 case OE_Abort:
6308 case OE_Fail: {
6309 sqlite3UniqueConstraint(pParse, onError, pIdx);
6310 break;
6311 }
6312 case OE_Ignore: {
6313 sqlite3VdbeGoto(v, ignoreDest);
6314 break;
6315 }
6316 default: {
6317 Trigger *pTrigger = 0;
6318 assert( onError==OE_Replace );
6319 sqlite3MultiWrite(pParse);
6320 if( db->flags&SQLITE_RecTriggers ){
6321 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
6322 }
6323 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
6324 regR, nPkField, 0, OE_Replace,
6325 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
6326 seenReplace = 1;
6327 break;
6328 }
6329 }
6330 sqlite3VdbeResolveLabel(v, addrUniqueOk);
6331 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
6332 }
6333 if( ipkTop ){
6334 sqlite3VdbeGoto(v, ipkTop+1);
6335 sqlite3VdbeJumpHere(v, ipkBottom);
6336 }
6337
6338 *pbMayReplace = seenReplace;
6339 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
6340 }
6341
6342 #ifdef SQLITE_ENABLE_NULL_TRIM
6343 /*
6344 ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
6345 ** to be the number of columns in table pTab that must not be NULL-trimmed.
6346 **
6347 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
6348 */
6349 SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
6350 u16 i;
6351
6352 /* Records with omitted columns are only allowed for schema format
6353 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
6354 if( pTab->pSchema->file_format<2 ) return;
6355
6356 for(i=pTab->nCol; i>1 && pTab->aCol[i-1].pDflt==0; i--){}
6357 sqlite3VdbeChangeP5(v, i);
6358 }
6359 #endif
6360
6361 /*
6362 ** This routine generates code to finish the INSERT or UPDATE operation
6363 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
6364 ** A consecutive range of registers starting at regNewData contains the
6365 ** rowid and the content to be inserted.
6366 **
6367 ** The arguments to this routine should be the same as the first six
6368 ** arguments to sqlite3GenerateConstraintChecks.
6369 */
6370 SQLITE_PRIVATE void sqlite3CompleteInsertion(
6371 Parse *pParse, /* The parser context */
6372 Table *pTab, /* the table into which we are inserting */
6373 int iDataCur, /* Cursor of the canonical data source */
6374 int iIdxCur, /* First index cursor */
6375 int regNewData, /* Range of content */
6376 int *aRegIdx, /* Register used by each index. 0 for unused indices */
6377 int update_flags, /* True for UPDATE, False for INSERT */
6378 int appendBias, /* True if this is likely to be an append */
6379 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
6380 ){
6381 Vdbe *v; /* Prepared statements under construction */
6382 Index *pIdx; /* An index being inserted or updated */
6383 u8 pik_flags; /* flag values passed to the btree insert */
6384 int regData; /* Content registers (after the rowid) */
6385 int regRec; /* Register holding assembled record for the table */
6386 int i; /* Loop counter */
6387 u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
6388
6389 assert( update_flags==0
6390 || update_flags==OPFLAG_ISUPDATE
6391 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
6392 );
6393
6394 v = sqlite3GetVdbe(pParse);
6395 assert( v!=0 );
6396 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
6397 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
6398 if( aRegIdx[i]==0 ) continue;
6399 bAffinityDone = 1;
6400 if( pIdx->pPartIdxWhere ){
6401 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
6402 VdbeCoverage(v);
6403 }
6404 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
6405 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
6406 assert( pParse->nested==0 );
6407 pik_flags |= OPFLAG_NCHANGE;
6408 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
6409 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
6410 if( update_flags==0 ){
6411 sqlite3VdbeAddOp4(v, OP_InsertInt,
6412 iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
6413 );
6414 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
6415 }
6416 #endif
6417 }
6418 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
6419 aRegIdx[i]+1,
6420 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
6421 sqlite3VdbeChangeP5(v, pik_flags);
6422 }
6423 if( !HasRowid(pTab) ) return;
6424 regData = regNewData + 1;
6425 regRec = sqlite3GetTempReg(pParse);
6426 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
6427 sqlite3SetMakeRecordP5(v, pTab);
6428 if( !bAffinityDone ){
6429 sqlite3TableAffinity(v, pTab, 0);
6430 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
6431 }
6432 if( pParse->nested ){
6433 pik_flags = 0;
6434 }else{
6435 pik_flags = OPFLAG_NCHANGE;
6436 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
6437 }
6438 if( appendBias ){
6439 pik_flags |= OPFLAG_APPEND;
6440 }
6441 if( useSeekResult ){
6442 pik_flags |= OPFLAG_USESEEKRESULT;
6443 }
6444 sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
6445 if( !pParse->nested ){
6446 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
6447 }
6448 sqlite3VdbeChangeP5(v, pik_flags);
6449 }
6450
6451 /*
6452 ** Allocate cursors for the pTab table and all its indices and generate
6453 ** code to open and initialized those cursors.
6454 **
6455 ** The cursor for the object that contains the complete data (normally
6456 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
6457 ** ROWID table) is returned in *piDataCur. The first index cursor is
6458 ** returned in *piIdxCur. The number of indices is returned.
6459 **
6460 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
6461 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
6462 ** If iBase is negative, then allocate the next available cursor.
6463 **
6464 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
6465 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
6466 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
6467 ** pTab->pIndex list.
6468 **
6469 ** If pTab is a virtual table, then this routine is a no-op and the
6470 ** *piDataCur and *piIdxCur values are left uninitialized.
6471 */
6472 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
6473 Parse *pParse, /* Parsing context */
6474 Table *pTab, /* Table to be opened */
6475 int op, /* OP_OpenRead or OP_OpenWrite */
6476 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
6477 int iBase, /* Use this for the table cursor, if there is one */
6478 u8 *aToOpen, /* If not NULL: boolean for each table and index */
6479 int *piDataCur, /* Write the database source cursor number here */
6480 int *piIdxCur /* Write the first index cursor number here */
6481 ){
6482 int i;
6483 int iDb;
6484 int iDataCur;
6485 Index *pIdx;
6486 Vdbe *v;
6487
6488 assert( op==OP_OpenRead || op==OP_OpenWrite );
6489 assert( op==OP_OpenWrite || p5==0 );
6490 if( IsVirtual(pTab) ){
6491 /* This routine is a no-op for virtual tables. Leave the output
6492 ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
6493 ** can detect if they are used by mistake in the caller. */
6494 return 0;
6495 }
6496 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
6497 v = sqlite3GetVdbe(pParse);
6498 assert( v!=0 );
6499 if( iBase<0 ) iBase = pParse->nTab;
6500 iDataCur = iBase++;
6501 if( piDataCur ) *piDataCur = iDataCur;
6502 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
6503 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
6504 }else{
6505 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
6506 }
6507 if( piIdxCur ) *piIdxCur = iBase;
6508 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
6509 int iIdxCur = iBase++;
6510 assert( pIdx->pSchema==pTab->pSchema );
6511 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
6512 if( piDataCur ) *piDataCur = iIdxCur;
6513 p5 = 0;
6514 }
6515 if( aToOpen==0 || aToOpen[i+1] ){
6516 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
6517 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
6518 sqlite3VdbeChangeP5(v, p5);
6519 VdbeComment((v, "%s", pIdx->zName));
6520 }
6521 }
6522 if( iBase>pParse->nTab ) pParse->nTab = iBase;
6523 return i;
6524 }
6525
6526
6527 #ifdef SQLITE_TEST
6528 /*
6529 ** The following global variable is incremented whenever the
6530 ** transfer optimization is used. This is used for testing
6531 ** purposes only - to make sure the transfer optimization really
6532 ** is happening when it is supposed to.
6533 */
6534 SQLITE_API int sqlite3_xferopt_count;
6535 #endif /* SQLITE_TEST */
6536
6537
6538 #ifndef SQLITE_OMIT_XFER_OPT
6539 /*
6540 ** Check to see if index pSrc is compatible as a source of data
6541 ** for index pDest in an insert transfer optimization. The rules
6542 ** for a compatible index:
6543 **
6544 ** * The index is over the same set of columns
6545 ** * The same DESC and ASC markings occurs on all columns
6546 ** * The same onError processing (OE_Abort, OE_Ignore, etc)
6547 ** * The same collating sequence on each column
6548 ** * The index has the exact same WHERE clause
6549 */
6550 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
6551 int i;
6552 assert( pDest && pSrc );
6553 assert( pDest->pTable!=pSrc->pTable );
6554 if( pDest->nKeyCol!=pSrc->nKeyCol ){
6555 return 0; /* Different number of columns */
6556 }
6557 if( pDest->onError!=pSrc->onError ){
6558 return 0; /* Different conflict resolution strategies */
6559 }
6560 for(i=0; i<pSrc->nKeyCol; i++){
6561 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
6562 return 0; /* Different columns indexed */
6563 }
6564 if( pSrc->aiColumn[i]==XN_EXPR ){
6565 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
6566 if( sqlite3ExprCompare(pSrc->aColExpr->a[i].pExpr,
6567 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
6568 return 0; /* Different expressions in the index */
6569 }
6570 }
6571 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
6572 return 0; /* Different sort orders */
6573 }
6574 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
6575 return 0; /* Different collating sequences */
6576 }
6577 }
6578 if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
6579 return 0; /* Different WHERE clauses */
6580 }
6581
6582 /* If no test above fails then the indices must be compatible */
6583 return 1;
6584 }
6585
6586 /*
6587 ** Attempt the transfer optimization on INSERTs of the form
6588 **
6589 ** INSERT INTO tab1 SELECT * FROM tab2;
6590 **
6591 ** The xfer optimization transfers raw records from tab2 over to tab1.
6592 ** Columns are not decoded and reassembled, which greatly improves
6593 ** performance. Raw index records are transferred in the same way.
6594 **
6595 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
6596 ** There are lots of rules for determining compatibility - see comments
6597 ** embedded in the code for details.
6598 **
6599 ** This routine returns TRUE if the optimization is guaranteed to be used.
6600 ** Sometimes the xfer optimization will only work if the destination table
6601 ** is empty - a factor that can only be determined at run-time. In that
6602 ** case, this routine generates code for the xfer optimization but also
6603 ** does a test to see if the destination table is empty and jumps over the
6604 ** xfer optimization code if the test fails. In that case, this routine
6605 ** returns FALSE so that the caller will know to go ahead and generate
6606 ** an unoptimized transfer. This routine also returns FALSE if there
6607 ** is no chance that the xfer optimization can be applied.
6608 **
6609 ** This optimization is particularly useful at making VACUUM run faster.
6610 */
6611 static int xferOptimization(
6612 Parse *pParse, /* Parser context */
6613 Table *pDest, /* The table we are inserting into */
6614 Select *pSelect, /* A SELECT statement to use as the data source */
6615 int onError, /* How to handle constraint errors */
6616 int iDbDest /* The database of pDest */
6617 ){
6618 sqlite3 *db = pParse->db;
6619 ExprList *pEList; /* The result set of the SELECT */
6620 Table *pSrc; /* The table in the FROM clause of SELECT */
6621 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
6622 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
6623 int i; /* Loop counter */
6624 int iDbSrc; /* The database of pSrc */
6625 int iSrc, iDest; /* Cursors from source and destination */
6626 int addr1, addr2; /* Loop addresses */
6627 int emptyDestTest = 0; /* Address of test for empty pDest */
6628 int emptySrcTest = 0; /* Address of test for empty pSrc */
6629 Vdbe *v; /* The VDBE we are building */
6630 int regAutoinc; /* Memory register used by AUTOINC */
6631 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
6632 int regData, regRowid; /* Registers holding data and rowid */
6633
6634 if( pSelect==0 ){
6635 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
6636 }
6637 if( pParse->pWith || pSelect->pWith ){
6638 /* Do not attempt to process this query if there are an WITH clauses
6639 ** attached to it. Proceeding may generate a false "no such table: xxx"
6640 ** error if pSelect reads from a CTE named "xxx". */
6641 return 0;
6642 }
6643 if( sqlite3TriggerList(pParse, pDest) ){
6644 return 0; /* tab1 must not have triggers */
6645 }
6646 #ifndef SQLITE_OMIT_VIRTUALTABLE
6647 if( pDest->tabFlags & TF_Virtual ){
6648 return 0; /* tab1 must not be a virtual table */
6649 }
6650 #endif
6651 if( onError==OE_Default ){
6652 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
6653 if( onError==OE_Default ) onError = OE_Abort;
6654 }
6655 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
6656 if( pSelect->pSrc->nSrc!=1 ){
6657 return 0; /* FROM clause must have exactly one term */
6658 }
6659 if( pSelect->pSrc->a[0].pSelect ){
6660 return 0; /* FROM clause cannot contain a subquery */
6661 }
6662 if( pSelect->pWhere ){
6663 return 0; /* SELECT may not have a WHERE clause */
6664 }
6665 if( pSelect->pOrderBy ){
6666 return 0; /* SELECT may not have an ORDER BY clause */
6667 }
6668 /* Do not need to test for a HAVING clause. If HAVING is present but
6669 ** there is no ORDER BY, we will get an error. */
6670 if( pSelect->pGroupBy ){
6671 return 0; /* SELECT may not have a GROUP BY clause */
6672 }
6673 if( pSelect->pLimit ){
6674 return 0; /* SELECT may not have a LIMIT clause */
6675 }
6676 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
6677 if( pSelect->pPrior ){
6678 return 0; /* SELECT may not be a compound query */
6679 }
6680 if( pSelect->selFlags & SF_Distinct ){
6681 return 0; /* SELECT may not be DISTINCT */
6682 }
6683 pEList = pSelect->pEList;
6684 assert( pEList!=0 );
6685 if( pEList->nExpr!=1 ){
6686 return 0; /* The result set must have exactly one column */
6687 }
6688 assert( pEList->a[0].pExpr );
6689 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
6690 return 0; /* The result set must be the special operator "*" */
6691 }
6692
6693 /* At this point we have established that the statement is of the
6694 ** correct syntactic form to participate in this optimization. Now
6695 ** we have to check the semantics.
6696 */
6697 pItem = pSelect->pSrc->a;
6698 pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
6699 if( pSrc==0 ){
6700 return 0; /* FROM clause does not contain a real table */
6701 }
6702 if( pSrc==pDest ){
6703 return 0; /* tab1 and tab2 may not be the same table */
6704 }
6705 if( HasRowid(pDest)!=HasRowid(pSrc) ){
6706 return 0; /* source and destination must both be WITHOUT ROWID or not */
6707 }
6708 #ifndef SQLITE_OMIT_VIRTUALTABLE
6709 if( pSrc->tabFlags & TF_Virtual ){
6710 return 0; /* tab2 must not be a virtual table */
6711 }
6712 #endif
6713 if( pSrc->pSelect ){
6714 return 0; /* tab2 may not be a view */
6715 }
6716 if( pDest->nCol!=pSrc->nCol ){
6717 return 0; /* Number of columns must be the same in tab1 and tab2 */
6718 }
6719 if( pDest->iPKey!=pSrc->iPKey ){
6720 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
6721 }
6722 for(i=0; i<pDest->nCol; i++){
6723 Column *pDestCol = &pDest->aCol[i];
6724 Column *pSrcCol = &pSrc->aCol[i];
6725 #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
6726 if( (db->flags & SQLITE_Vacuum)==0
6727 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
6728 ){
6729 return 0; /* Neither table may have __hidden__ columns */
6730 }
6731 #endif
6732 if( pDestCol->affinity!=pSrcCol->affinity ){
6733 return 0; /* Affinity must be the same on all columns */
6734 }
6735 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
6736 return 0; /* Collating sequence must be the same on all columns */
6737 }
6738 if( pDestCol->notNull && !pSrcCol->notNull ){
6739 return 0; /* tab2 must be NOT NULL if tab1 is */
6740 }
6741 /* Default values for second and subsequent columns need to match. */
6742 if( i>0 ){
6743 assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
6744 assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
6745 if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
6746 || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
6747 pSrcCol->pDflt->u.zToken)!=0)
6748 ){
6749 return 0; /* Default values must be the same for all columns */
6750 }
6751 }
6752 }
6753 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
6754 if( IsUniqueIndex(pDestIdx) ){
6755 destHasUniqueIdx = 1;
6756 }
6757 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
6758 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
6759 }
6760 if( pSrcIdx==0 ){
6761 return 0; /* pDestIdx has no corresponding index in pSrc */
6762 }
6763 }
6764 #ifndef SQLITE_OMIT_CHECK
6765 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
6766 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
6767 }
6768 #endif
6769 #ifndef SQLITE_OMIT_FOREIGN_KEY
6770 /* Disallow the transfer optimization if the destination table constains
6771 ** any foreign key constraints. This is more restrictive than necessary.
6772 ** But the main beneficiary of the transfer optimization is the VACUUM
6773 ** command, and the VACUUM command disables foreign key constraints. So
6774 ** the extra complication to make this rule less restrictive is probably
6775 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
6776 */
6777 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
6778 return 0;
6779 }
6780 #endif
6781 if( (db->flags & SQLITE_CountRows)!=0 ){
6782 return 0; /* xfer opt does not play well with PRAGMA count_changes */
6783 }
6784
6785 /* If we get this far, it means that the xfer optimization is at
6786 ** least a possibility, though it might only work if the destination
6787 ** table (tab1) is initially empty.
6788 */
6789 #ifdef SQLITE_TEST
6790 sqlite3_xferopt_count++;
6791 #endif
6792 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
6793 v = sqlite3GetVdbe(pParse);
6794 sqlite3CodeVerifySchema(pParse, iDbSrc);
6795 iSrc = pParse->nTab++;
6796 iDest = pParse->nTab++;
6797 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
6798 regData = sqlite3GetTempReg(pParse);
6799 regRowid = sqlite3GetTempReg(pParse);
6800 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
6801 assert( HasRowid(pDest) || destHasUniqueIdx );
6802 if( (db->flags & SQLITE_Vacuum)==0 && (
6803 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
6804 || destHasUniqueIdx /* (2) */
6805 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
6806 )){
6807 /* In some circumstances, we are able to run the xfer optimization
6808 ** only if the destination table is initially empty. Unless the
6809 ** SQLITE_Vacuum flag is set, this block generates code to make
6810 ** that determination. If SQLITE_Vacuum is set, then the destination
6811 ** table is always empty.
6812 **
6813 ** Conditions under which the destination must be empty:
6814 **
6815 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
6816 ** (If the destination is not initially empty, the rowid fields
6817 ** of index entries might need to change.)
6818 **
6819 ** (2) The destination has a unique index. (The xfer optimization
6820 ** is unable to test uniqueness.)
6821 **
6822 ** (3) onError is something other than OE_Abort and OE_Rollback.
6823 */
6824 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
6825 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
6826 sqlite3VdbeJumpHere(v, addr1);
6827 }
6828 if( HasRowid(pSrc) ){
6829 u8 insFlags;
6830 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
6831 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
6832 if( pDest->iPKey>=0 ){
6833 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
6834 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
6835 VdbeCoverage(v);
6836 sqlite3RowidConstraint(pParse, onError, pDest);
6837 sqlite3VdbeJumpHere(v, addr2);
6838 autoIncStep(pParse, regAutoinc, regRowid);
6839 }else if( pDest->pIndex==0 ){
6840 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
6841 }else{
6842 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
6843 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
6844 }
6845 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
6846 if( db->flags & SQLITE_Vacuum ){
6847 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
6848 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
6849 OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
6850 }else{
6851 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
6852 }
6853 sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
6854 (char*)pDest, P4_TABLE);
6855 sqlite3VdbeChangeP5(v, insFlags);
6856 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
6857 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
6858 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
6859 }else{
6860 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
6861 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
6862 }
6863 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
6864 u8 idxInsFlags = 0;
6865 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
6866 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
6867 }
6868 assert( pSrcIdx );
6869 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
6870 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
6871 VdbeComment((v, "%s", pSrcIdx->zName));
6872 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
6873 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
6874 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
6875 VdbeComment((v, "%s", pDestIdx->zName));
6876 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
6877 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
6878 if( db->flags & SQLITE_Vacuum ){
6879 /* This INSERT command is part of a VACUUM operation, which guarantees
6880 ** that the destination table is empty. If all indexed columns use
6881 ** collation sequence BINARY, then it can also be assumed that the
6882 ** index will be populated by inserting keys in strictly sorted
6883 ** order. In this case, instead of seeking within the b-tree as part
6884 ** of every OP_IdxInsert opcode, an OP_Last is added before the
6885 ** OP_IdxInsert to seek to the point within the b-tree where each key
6886 ** should be inserted. This is faster.
6887 **
6888 ** If any of the indexed columns use a collation sequence other than
6889 ** BINARY, this optimization is disabled. This is because the user
6890 ** might change the definition of a collation sequence and then run
6891 ** a VACUUM command. In that case keys may not be written in strictly
6892 ** sorted order. */
6893 for(i=0; i<pSrcIdx->nColumn; i++){
6894 const char *zColl = pSrcIdx->azColl[i];
6895 assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0
6896 || sqlite3StrBINARY==zColl );
6897 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
6898 }
6899 if( i==pSrcIdx->nColumn ){
6900 idxInsFlags = OPFLAG_USESEEKRESULT;
6901 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
6902 }
6903 }
6904 if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
6905 idxInsFlags |= OPFLAG_NCHANGE;
6906 }
6907 sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
6908 sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
6909 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
6910 sqlite3VdbeJumpHere(v, addr1);
6911 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
6912 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
6913 }
6914 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
6915 sqlite3ReleaseTempReg(pParse, regRowid);
6916 sqlite3ReleaseTempReg(pParse, regData);
6917 if( emptyDestTest ){
6918 sqlite3AutoincrementEnd(pParse);
6919 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
6920 sqlite3VdbeJumpHere(v, emptyDestTest);
6921 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
6922 return 0;
6923 }else{
6924 return 1;
6925 }
6926 }
6927 #endif /* SQLITE_OMIT_XFER_OPT */
6928
6929 /************** End of insert.c **********************************************/
6930 /************** Begin file legacy.c ******************************************/
6931 /*
6932 ** 2001 September 15
6933 **
6934 ** The author disclaims copyright to this source code. In place of
6935 ** a legal notice, here is a blessing:
6936 **
6937 ** May you do good and not evil.
6938 ** May you find forgiveness for yourself and forgive others.
6939 ** May you share freely, never taking more than you give.
6940 **
6941 *************************************************************************
6942 ** Main file for the SQLite library. The routines in this file
6943 ** implement the programmer interface to the library. Routines in
6944 ** other files are for internal use by SQLite and should not be
6945 ** accessed by users of the library.
6946 */
6947
6948 /* #include "sqliteInt.h" */
6949
6950 /*
6951 ** Execute SQL code. Return one of the SQLITE_ success/failure
6952 ** codes. Also write an error message into memory obtained from
6953 ** malloc() and make *pzErrMsg point to that message.
6954 **
6955 ** If the SQL is a query, then for each row in the query result
6956 ** the xCallback() function is called. pArg becomes the first
6957 ** argument to xCallback(). If xCallback=NULL then no callback
6958 ** is invoked, even for queries.
6959 */
6960 SQLITE_API int sqlite3_exec(
6961 sqlite3 *db, /* The database on which the SQL executes */
6962 const char *zSql, /* The SQL to be executed */
6963 sqlite3_callback xCallback, /* Invoke this callback routine */
6964 void *pArg, /* First argument to xCallback() */
6965 char **pzErrMsg /* Write error messages here */
6966 ){
6967 int rc = SQLITE_OK; /* Return code */
6968 const char *zLeftover; /* Tail of unprocessed SQL */
6969 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
6970 char **azCols = 0; /* Names of result columns */
6971 int callbackIsInit; /* True if callback data is initialized */
6972
6973 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
6974 if( zSql==0 ) zSql = "";
6975
6976 sqlite3_mutex_enter(db->mutex);
6977 sqlite3Error(db, SQLITE_OK);
6978 while( rc==SQLITE_OK && zSql[0] ){
6979 int nCol;
6980 char **azVals = 0;
6981
6982 pStmt = 0;
6983 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
6984 assert( rc==SQLITE_OK || pStmt==0 );
6985 if( rc!=SQLITE_OK ){
6986 continue;
6987 }
6988 if( !pStmt ){
6989 /* this happens for a comment or white-space */
6990 zSql = zLeftover;
6991 continue;
6992 }
6993
6994 callbackIsInit = 0;
6995 nCol = sqlite3_column_count(pStmt);
6996
6997 while( 1 ){
6998 int i;
6999 rc = sqlite3_step(pStmt);
7000
7001 /* Invoke the callback function if required */
7002 if( xCallback && (SQLITE_ROW==rc ||
7003 (SQLITE_DONE==rc && !callbackIsInit
7004 && db->flags&SQLITE_NullCallback)) ){
7005 if( !callbackIsInit ){
7006 azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
7007 if( azCols==0 ){
7008 goto exec_out;
7009 }
7010 for(i=0; i<nCol; i++){
7011 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
7012 /* sqlite3VdbeSetColName() installs column names as UTF8
7013 ** strings so there is no way for sqlite3_column_name() to fail. */
7014 assert( azCols[i]!=0 );
7015 }
7016 callbackIsInit = 1;
7017 }
7018 if( rc==SQLITE_ROW ){
7019 azVals = &azCols[nCol];
7020 for(i=0; i<nCol; i++){
7021 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
7022 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
7023 sqlite3OomFault(db);
7024 goto exec_out;
7025 }
7026 }
7027 }
7028 if( xCallback(pArg, nCol, azVals, azCols) ){
7029 /* EVIDENCE-OF: R-38229-40159 If the callback function to
7030 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
7031 ** return SQLITE_ABORT. */
7032 rc = SQLITE_ABORT;
7033 sqlite3VdbeFinalize((Vdbe *)pStmt);
7034 pStmt = 0;
7035 sqlite3Error(db, SQLITE_ABORT);
7036 goto exec_out;
7037 }
7038 }
7039
7040 if( rc!=SQLITE_ROW ){
7041 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
7042 pStmt = 0;
7043 zSql = zLeftover;
7044 while( sqlite3Isspace(zSql[0]) ) zSql++;
7045 break;
7046 }
7047 }
7048
7049 sqlite3DbFree(db, azCols);
7050 azCols = 0;
7051 }
7052
7053 exec_out:
7054 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
7055 sqlite3DbFree(db, azCols);
7056
7057 rc = sqlite3ApiExit(db, rc);
7058 if( rc!=SQLITE_OK && pzErrMsg ){
7059 int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
7060 *pzErrMsg = sqlite3Malloc(nErrMsg);
7061 if( *pzErrMsg ){
7062 memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
7063 }else{
7064 rc = SQLITE_NOMEM_BKPT;
7065 sqlite3Error(db, SQLITE_NOMEM);
7066 }
7067 }else if( pzErrMsg ){
7068 *pzErrMsg = 0;
7069 }
7070
7071 assert( (rc&db->errMask)==rc );
7072 sqlite3_mutex_leave(db->mutex);
7073 return rc;
7074 }
7075
7076 /************** End of legacy.c **********************************************/
7077 /************** Begin file loadext.c *****************************************/
7078 /*
7079 ** 2006 June 7
7080 **
7081 ** The author disclaims copyright to this source code. In place of
7082 ** a legal notice, here is a blessing:
7083 **
7084 ** May you do good and not evil.
7085 ** May you find forgiveness for yourself and forgive others.
7086 ** May you share freely, never taking more than you give.
7087 **
7088 *************************************************************************
7089 ** This file contains code used to dynamically load extensions into
7090 ** the SQLite library.
7091 */
7092
7093 #ifndef SQLITE_CORE
7094 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
7095 #endif
7096 /************** Include sqlite3ext.h in the middle of loadext.c **************/
7097 /************** Begin file sqlite3ext.h **************************************/
7098 /*
7099 ** 2006 June 7
7100 **
7101 ** The author disclaims copyright to this source code. In place of
7102 ** a legal notice, here is a blessing:
7103 **
7104 ** May you do good and not evil.
7105 ** May you find forgiveness for yourself and forgive others.
7106 ** May you share freely, never taking more than you give.
7107 **
7108 *************************************************************************
7109 ** This header file defines the SQLite interface for use by
7110 ** shared libraries that want to be imported as extensions into
7111 ** an SQLite instance. Shared libraries that intend to be loaded
7112 ** as extensions by SQLite should #include this file instead of
7113 ** sqlite3.h.
7114 */
7115 #ifndef SQLITE3EXT_H
7116 #define SQLITE3EXT_H
7117 /* #include "sqlite3.h" */
7118
7119 /*
7120 ** The following structure holds pointers to all of the SQLite API
7121 ** routines.
7122 **
7123 ** WARNING: In order to maintain backwards compatibility, add new
7124 ** interfaces to the end of this structure only. If you insert new
7125 ** interfaces in the middle of this structure, then older different
7126 ** versions of SQLite will not be able to load each other's shared
7127 ** libraries!
7128 */
7129 struct sqlite3_api_routines {
7130 void * (*aggregate_context)(sqlite3_context*,int nBytes);
7131 int (*aggregate_count)(sqlite3_context*);
7132 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
7133 int (*bind_double)(sqlite3_stmt*,int,double);
7134 int (*bind_int)(sqlite3_stmt*,int,int);
7135 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
7136 int (*bind_null)(sqlite3_stmt*,int);
7137 int (*bind_parameter_count)(sqlite3_stmt*);
7138 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
7139 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
7140 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
7141 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
7142 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
7143 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
7144 int (*busy_timeout)(sqlite3*,int ms);
7145 int (*changes)(sqlite3*);
7146 int (*close)(sqlite3*);
7147 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
7148 int eTextRep,const char*));
7149 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
7150 int eTextRep,const void*));
7151 const void * (*column_blob)(sqlite3_stmt*,int iCol);
7152 int (*column_bytes)(sqlite3_stmt*,int iCol);
7153 int (*column_bytes16)(sqlite3_stmt*,int iCol);
7154 int (*column_count)(sqlite3_stmt*pStmt);
7155 const char * (*column_database_name)(sqlite3_stmt*,int);
7156 const void * (*column_database_name16)(sqlite3_stmt*,int);
7157 const char * (*column_decltype)(sqlite3_stmt*,int i);
7158 const void * (*column_decltype16)(sqlite3_stmt*,int);
7159 double (*column_double)(sqlite3_stmt*,int iCol);
7160 int (*column_int)(sqlite3_stmt*,int iCol);
7161 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
7162 const char * (*column_name)(sqlite3_stmt*,int);
7163 const void * (*column_name16)(sqlite3_stmt*,int);
7164 const char * (*column_origin_name)(sqlite3_stmt*,int);
7165 const void * (*column_origin_name16)(sqlite3_stmt*,int);
7166 const char * (*column_table_name)(sqlite3_stmt*,int);
7167 const void * (*column_table_name16)(sqlite3_stmt*,int);
7168 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
7169 const void * (*column_text16)(sqlite3_stmt*,int iCol);
7170 int (*column_type)(sqlite3_stmt*,int iCol);
7171 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
7172 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
7173 int (*complete)(const char*sql);
7174 int (*complete16)(const void*sql);
7175 int (*create_collation)(sqlite3*,const char*,int,void*,
7176 int(*)(void*,int,const void*,int,const void*));
7177 int (*create_collation16)(sqlite3*,const void*,int,void*,
7178 int(*)(void*,int,const void*,int,const void*));
7179 int (*create_function)(sqlite3*,const char*,int,int,void*,
7180 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
7181 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
7182 void (*xFinal)(sqlite3_context*));
7183 int (*create_function16)(sqlite3*,const void*,int,int,void*,
7184 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
7185 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
7186 void (*xFinal)(sqlite3_context*));
7187 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
7188 int (*data_count)(sqlite3_stmt*pStmt);
7189 sqlite3 * (*db_handle)(sqlite3_stmt*);
7190 int (*declare_vtab)(sqlite3*,const char*);
7191 int (*enable_shared_cache)(int);
7192 int (*errcode)(sqlite3*db);
7193 const char * (*errmsg)(sqlite3*);
7194 const void * (*errmsg16)(sqlite3*);
7195 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
7196 int (*expired)(sqlite3_stmt*);
7197 int (*finalize)(sqlite3_stmt*pStmt);
7198 void (*free)(void*);
7199 void (*free_table)(char**result);
7200 int (*get_autocommit)(sqlite3*);
7201 void * (*get_auxdata)(sqlite3_context*,int);
7202 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
7203 int (*global_recover)(void);
7204 void (*interruptx)(sqlite3*);
7205 sqlite_int64 (*last_insert_rowid)(sqlite3*);
7206 const char * (*libversion)(void);
7207 int (*libversion_number)(void);
7208 void *(*malloc)(int);
7209 char * (*mprintf)(const char*,...);
7210 int (*open)(const char*,sqlite3**);
7211 int (*open16)(const void*,sqlite3**);
7212 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
7213 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
7214 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
7215 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
7216 void *(*realloc)(void*,int);
7217 int (*reset)(sqlite3_stmt*pStmt);
7218 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
7219 void (*result_double)(sqlite3_context*,double);
7220 void (*result_error)(sqlite3_context*,const char*,int);
7221 void (*result_error16)(sqlite3_context*,const void*,int);
7222 void (*result_int)(sqlite3_context*,int);
7223 void (*result_int64)(sqlite3_context*,sqlite_int64);
7224 void (*result_null)(sqlite3_context*);
7225 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
7226 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
7227 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
7228 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
7229 void (*result_value)(sqlite3_context*,sqlite3_value*);
7230 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
7231 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
7232 const char*,const char*),void*);
7233 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
7234 char * (*snprintf)(int,char*,const char*,...);
7235 int (*step)(sqlite3_stmt*);
7236 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
7237 char const**,char const**,int*,int*,int*);
7238 void (*thread_cleanup)(void);
7239 int (*total_changes)(sqlite3*);
7240 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
7241 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
7242 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
7243 sqlite_int64),void*);
7244 void * (*user_data)(sqlite3_context*);
7245 const void * (*value_blob)(sqlite3_value*);
7246 int (*value_bytes)(sqlite3_value*);
7247 int (*value_bytes16)(sqlite3_value*);
7248 double (*value_double)(sqlite3_value*);
7249 int (*value_int)(sqlite3_value*);
7250 sqlite_int64 (*value_int64)(sqlite3_value*);
7251 int (*value_numeric_type)(sqlite3_value*);
7252 const unsigned char * (*value_text)(sqlite3_value*);
7253 const void * (*value_text16)(sqlite3_value*);
7254 const void * (*value_text16be)(sqlite3_value*);
7255 const void * (*value_text16le)(sqlite3_value*);
7256 int (*value_type)(sqlite3_value*);
7257 char *(*vmprintf)(const char*,va_list);
7258 /* Added ??? */
7259 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
7260 /* Added by 3.3.13 */
7261 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
7262 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
7263 int (*clear_bindings)(sqlite3_stmt*);
7264 /* Added by 3.4.1 */
7265 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
7266 void (*xDestroy)(void *));
7267 /* Added by 3.5.0 */
7268 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
7269 int (*blob_bytes)(sqlite3_blob*);
7270 int (*blob_close)(sqlite3_blob*);
7271 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
7272 int,sqlite3_blob**);
7273 int (*blob_read)(sqlite3_blob*,void*,int,int);
7274 int (*blob_write)(sqlite3_blob*,const void*,int,int);
7275 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
7276 int(*)(void*,int,const void*,int,const void*),
7277 void(*)(void*));
7278 int (*file_control)(sqlite3*,const char*,int,void*);
7279 sqlite3_int64 (*memory_highwater)(int);
7280 sqlite3_int64 (*memory_used)(void);
7281 sqlite3_mutex *(*mutex_alloc)(int);
7282 void (*mutex_enter)(sqlite3_mutex*);
7283 void (*mutex_free)(sqlite3_mutex*);
7284 void (*mutex_leave)(sqlite3_mutex*);
7285 int (*mutex_try)(sqlite3_mutex*);
7286 int (*open_v2)(const char*,sqlite3**,int,const char*);
7287 int (*release_memory)(int);
7288 void (*result_error_nomem)(sqlite3_context*);
7289 void (*result_error_toobig)(sqlite3_context*);
7290 int (*sleep)(int);
7291 void (*soft_heap_limit)(int);
7292 sqlite3_vfs *(*vfs_find)(const char*);
7293 int (*vfs_register)(sqlite3_vfs*,int);
7294 int (*vfs_unregister)(sqlite3_vfs*);
7295 int (*xthreadsafe)(void);
7296 void (*result_zeroblob)(sqlite3_context*,int);
7297 void (*result_error_code)(sqlite3_context*,int);
7298 int (*test_control)(int, ...);
7299 void (*randomness)(int,void*);
7300 sqlite3 *(*context_db_handle)(sqlite3_context*);
7301 int (*extended_result_codes)(sqlite3*,int);
7302 int (*limit)(sqlite3*,int,int);
7303 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
7304 const char *(*sql)(sqlite3_stmt*);
7305 int (*status)(int,int*,int*,int);
7306 int (*backup_finish)(sqlite3_backup*);
7307 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
7308 int (*backup_pagecount)(sqlite3_backup*);
7309 int (*backup_remaining)(sqlite3_backup*);
7310 int (*backup_step)(sqlite3_backup*,int);
7311 const char *(*compileoption_get)(int);
7312 int (*compileoption_used)(const char*);
7313 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
7314 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
7315 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
7316 void (*xFinal)(sqlite3_context*),
7317 void(*xDestroy)(void*));
7318 int (*db_config)(sqlite3*,int,...);
7319 sqlite3_mutex *(*db_mutex)(sqlite3*);
7320 int (*db_status)(sqlite3*,int,int*,int*,int);
7321 int (*extended_errcode)(sqlite3*);
7322 void (*log)(int,const char*,...);
7323 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
7324 const char *(*sourceid)(void);
7325 int (*stmt_status)(sqlite3_stmt*,int,int);
7326 int (*strnicmp)(const char*,const char*,int);
7327 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
7328 int (*wal_autocheckpoint)(sqlite3*,int);
7329 int (*wal_checkpoint)(sqlite3*,const char*);
7330 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
7331 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
7332 int (*vtab_config)(sqlite3*,int op,...);
7333 int (*vtab_on_conflict)(sqlite3*);
7334 /* Version 3.7.16 and later */
7335 int (*close_v2)(sqlite3*);
7336 const char *(*db_filename)(sqlite3*,const char*);
7337 int (*db_readonly)(sqlite3*,const char*);
7338 int (*db_release_memory)(sqlite3*);
7339 const char *(*errstr)(int);
7340 int (*stmt_busy)(sqlite3_stmt*);
7341 int (*stmt_readonly)(sqlite3_stmt*);
7342 int (*stricmp)(const char*,const char*);
7343 int (*uri_boolean)(const char*,const char*,int);
7344 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
7345 const char *(*uri_parameter)(const char*,const char*);
7346 char *(*vsnprintf)(int,char*,const char*,va_list);
7347 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
7348 /* Version 3.8.7 and later */
7349 int (*auto_extension)(void(*)(void));
7350 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
7351 void(*)(void*));
7352 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
7353 void(*)(void*),unsigned char);
7354 int (*cancel_auto_extension)(void(*)(void));
7355 int (*load_extension)(sqlite3*,const char*,const char*,char**);
7356 void *(*malloc64)(sqlite3_uint64);
7357 sqlite3_uint64 (*msize)(void*);
7358 void *(*realloc64)(void*,sqlite3_uint64);
7359 void (*reset_auto_extension)(void);
7360 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
7361 void(*)(void*));
7362 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
7363 void(*)(void*), unsigned char);
7364 int (*strglob)(const char*,const char*);
7365 /* Version 3.8.11 and later */
7366 sqlite3_value *(*value_dup)(const sqlite3_value*);
7367 void (*value_free)(sqlite3_value*);
7368 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
7369 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
7370 /* Version 3.9.0 and later */
7371 unsigned int (*value_subtype)(sqlite3_value*);
7372 void (*result_subtype)(sqlite3_context*,unsigned int);
7373 /* Version 3.10.0 and later */
7374 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
7375 int (*strlike)(const char*,const char*,unsigned int);
7376 int (*db_cacheflush)(sqlite3*);
7377 /* Version 3.12.0 and later */
7378 int (*system_errno)(sqlite3*);
7379 /* Version 3.14.0 and later */
7380 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
7381 char *(*expanded_sql)(sqlite3_stmt*);
7382 };
7383
7384 /*
7385 ** This is the function signature used for all extension entry points. It
7386 ** is also defined in the file "loadext.c".
7387 */
7388 typedef int (*sqlite3_loadext_entry)(
7389 sqlite3 *db, /* Handle to the database. */
7390 char **pzErrMsg, /* Used to set error string on failure. */
7391 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
7392 );
7393
7394 /*
7395 ** The following macros redefine the API routines so that they are
7396 ** redirected through the global sqlite3_api structure.
7397 **
7398 ** This header file is also used by the loadext.c source file
7399 ** (part of the main SQLite library - not an extension) so that
7400 ** it can get access to the sqlite3_api_routines structure
7401 ** definition. But the main library does not want to redefine
7402 ** the API. So the redefinition macros are only valid if the
7403 ** SQLITE_CORE macros is undefined.
7404 */
7405 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
7406 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
7407 #ifndef SQLITE_OMIT_DEPRECATED
7408 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
7409 #endif
7410 #define sqlite3_bind_blob sqlite3_api->bind_blob
7411 #define sqlite3_bind_double sqlite3_api->bind_double
7412 #define sqlite3_bind_int sqlite3_api->bind_int
7413 #define sqlite3_bind_int64 sqlite3_api->bind_int64
7414 #define sqlite3_bind_null sqlite3_api->bind_null
7415 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
7416 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
7417 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
7418 #define sqlite3_bind_text sqlite3_api->bind_text
7419 #define sqlite3_bind_text16 sqlite3_api->bind_text16
7420 #define sqlite3_bind_value sqlite3_api->bind_value
7421 #define sqlite3_busy_handler sqlite3_api->busy_handler
7422 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
7423 #define sqlite3_changes sqlite3_api->changes
7424 #define sqlite3_close sqlite3_api->close
7425 #define sqlite3_collation_needed sqlite3_api->collation_needed
7426 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
7427 #define sqlite3_column_blob sqlite3_api->column_blob
7428 #define sqlite3_column_bytes sqlite3_api->column_bytes
7429 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
7430 #define sqlite3_column_count sqlite3_api->column_count
7431 #define sqlite3_column_database_name sqlite3_api->column_database_name
7432 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
7433 #define sqlite3_column_decltype sqlite3_api->column_decltype
7434 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
7435 #define sqlite3_column_double sqlite3_api->column_double
7436 #define sqlite3_column_int sqlite3_api->column_int
7437 #define sqlite3_column_int64 sqlite3_api->column_int64
7438 #define sqlite3_column_name sqlite3_api->column_name
7439 #define sqlite3_column_name16 sqlite3_api->column_name16
7440 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
7441 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
7442 #define sqlite3_column_table_name sqlite3_api->column_table_name
7443 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
7444 #define sqlite3_column_text sqlite3_api->column_text
7445 #define sqlite3_column_text16 sqlite3_api->column_text16
7446 #define sqlite3_column_type sqlite3_api->column_type
7447 #define sqlite3_column_value sqlite3_api->column_value
7448 #define sqlite3_commit_hook sqlite3_api->commit_hook
7449 #define sqlite3_complete sqlite3_api->complete
7450 #define sqlite3_complete16 sqlite3_api->complete16
7451 #define sqlite3_create_collation sqlite3_api->create_collation
7452 #define sqlite3_create_collation16 sqlite3_api->create_collation16
7453 #define sqlite3_create_function sqlite3_api->create_function
7454 #define sqlite3_create_function16 sqlite3_api->create_function16
7455 #define sqlite3_create_module sqlite3_api->create_module
7456 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
7457 #define sqlite3_data_count sqlite3_api->data_count
7458 #define sqlite3_db_handle sqlite3_api->db_handle
7459 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
7460 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
7461 #define sqlite3_errcode sqlite3_api->errcode
7462 #define sqlite3_errmsg sqlite3_api->errmsg
7463 #define sqlite3_errmsg16 sqlite3_api->errmsg16
7464 #define sqlite3_exec sqlite3_api->exec
7465 #ifndef SQLITE_OMIT_DEPRECATED
7466 #define sqlite3_expired sqlite3_api->expired
7467 #endif
7468 #define sqlite3_finalize sqlite3_api->finalize
7469 #define sqlite3_free sqlite3_api->free
7470 #define sqlite3_free_table sqlite3_api->free_table
7471 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
7472 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
7473 #define sqlite3_get_table sqlite3_api->get_table
7474 #ifndef SQLITE_OMIT_DEPRECATED
7475 #define sqlite3_global_recover sqlite3_api->global_recover
7476 #endif
7477 #define sqlite3_interrupt sqlite3_api->interruptx
7478 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
7479 #define sqlite3_libversion sqlite3_api->libversion
7480 #define sqlite3_libversion_number sqlite3_api->libversion_number
7481 #define sqlite3_malloc sqlite3_api->malloc
7482 #define sqlite3_mprintf sqlite3_api->mprintf
7483 #define sqlite3_open sqlite3_api->open
7484 #define sqlite3_open16 sqlite3_api->open16
7485 #define sqlite3_prepare sqlite3_api->prepare
7486 #define sqlite3_prepare16 sqlite3_api->prepare16
7487 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
7488 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
7489 #define sqlite3_profile sqlite3_api->profile
7490 #define sqlite3_progress_handler sqlite3_api->progress_handler
7491 #define sqlite3_realloc sqlite3_api->realloc
7492 #define sqlite3_reset sqlite3_api->reset
7493 #define sqlite3_result_blob sqlite3_api->result_blob
7494 #define sqlite3_result_double sqlite3_api->result_double
7495 #define sqlite3_result_error sqlite3_api->result_error
7496 #define sqlite3_result_error16 sqlite3_api->result_error16
7497 #define sqlite3_result_int sqlite3_api->result_int
7498 #define sqlite3_result_int64 sqlite3_api->result_int64
7499 #define sqlite3_result_null sqlite3_api->result_null
7500 #define sqlite3_result_text sqlite3_api->result_text
7501 #define sqlite3_result_text16 sqlite3_api->result_text16
7502 #define sqlite3_result_text16be sqlite3_api->result_text16be
7503 #define sqlite3_result_text16le sqlite3_api->result_text16le
7504 #define sqlite3_result_value sqlite3_api->result_value
7505 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
7506 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
7507 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
7508 #define sqlite3_snprintf sqlite3_api->snprintf
7509 #define sqlite3_step sqlite3_api->step
7510 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
7511 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
7512 #define sqlite3_total_changes sqlite3_api->total_changes
7513 #define sqlite3_trace sqlite3_api->trace
7514 #ifndef SQLITE_OMIT_DEPRECATED
7515 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
7516 #endif
7517 #define sqlite3_update_hook sqlite3_api->update_hook
7518 #define sqlite3_user_data sqlite3_api->user_data
7519 #define sqlite3_value_blob sqlite3_api->value_blob
7520 #define sqlite3_value_bytes sqlite3_api->value_bytes
7521 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
7522 #define sqlite3_value_double sqlite3_api->value_double
7523 #define sqlite3_value_int sqlite3_api->value_int
7524 #define sqlite3_value_int64 sqlite3_api->value_int64
7525 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
7526 #define sqlite3_value_text sqlite3_api->value_text
7527 #define sqlite3_value_text16 sqlite3_api->value_text16
7528 #define sqlite3_value_text16be sqlite3_api->value_text16be
7529 #define sqlite3_value_text16le sqlite3_api->value_text16le
7530 #define sqlite3_value_type sqlite3_api->value_type
7531 #define sqlite3_vmprintf sqlite3_api->vmprintf
7532 #define sqlite3_vsnprintf sqlite3_api->vsnprintf
7533 #define sqlite3_overload_function sqlite3_api->overload_function
7534 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
7535 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
7536 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
7537 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
7538 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
7539 #define sqlite3_blob_close sqlite3_api->blob_close
7540 #define sqlite3_blob_open sqlite3_api->blob_open
7541 #define sqlite3_blob_read sqlite3_api->blob_read
7542 #define sqlite3_blob_write sqlite3_api->blob_write
7543 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
7544 #define sqlite3_file_control sqlite3_api->file_control
7545 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
7546 #define sqlite3_memory_used sqlite3_api->memory_used
7547 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
7548 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
7549 #define sqlite3_mutex_free sqlite3_api->mutex_free
7550 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
7551 #define sqlite3_mutex_try sqlite3_api->mutex_try
7552 #define sqlite3_open_v2 sqlite3_api->open_v2
7553 #define sqlite3_release_memory sqlite3_api->release_memory
7554 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
7555 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
7556 #define sqlite3_sleep sqlite3_api->sleep
7557 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
7558 #define sqlite3_vfs_find sqlite3_api->vfs_find
7559 #define sqlite3_vfs_register sqlite3_api->vfs_register
7560 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
7561 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
7562 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
7563 #define sqlite3_result_error_code sqlite3_api->result_error_code
7564 #define sqlite3_test_control sqlite3_api->test_control
7565 #define sqlite3_randomness sqlite3_api->randomness
7566 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
7567 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
7568 #define sqlite3_limit sqlite3_api->limit
7569 #define sqlite3_next_stmt sqlite3_api->next_stmt
7570 #define sqlite3_sql sqlite3_api->sql
7571 #define sqlite3_status sqlite3_api->status
7572 #define sqlite3_backup_finish sqlite3_api->backup_finish
7573 #define sqlite3_backup_init sqlite3_api->backup_init
7574 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
7575 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
7576 #define sqlite3_backup_step sqlite3_api->backup_step
7577 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
7578 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
7579 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
7580 #define sqlite3_db_config sqlite3_api->db_config
7581 #define sqlite3_db_mutex sqlite3_api->db_mutex
7582 #define sqlite3_db_status sqlite3_api->db_status
7583 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
7584 #define sqlite3_log sqlite3_api->log
7585 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
7586 #define sqlite3_sourceid sqlite3_api->sourceid
7587 #define sqlite3_stmt_status sqlite3_api->stmt_status
7588 #define sqlite3_strnicmp sqlite3_api->strnicmp
7589 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
7590 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
7591 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
7592 #define sqlite3_wal_hook sqlite3_api->wal_hook
7593 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
7594 #define sqlite3_vtab_config sqlite3_api->vtab_config
7595 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
7596 /* Version 3.7.16 and later */
7597 #define sqlite3_close_v2 sqlite3_api->close_v2
7598 #define sqlite3_db_filename sqlite3_api->db_filename
7599 #define sqlite3_db_readonly sqlite3_api->db_readonly
7600 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
7601 #define sqlite3_errstr sqlite3_api->errstr
7602 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
7603 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
7604 #define sqlite3_stricmp sqlite3_api->stricmp
7605 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
7606 #define sqlite3_uri_int64 sqlite3_api->uri_int64
7607 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
7608 #define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf
7609 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
7610 /* Version 3.8.7 and later */
7611 #define sqlite3_auto_extension sqlite3_api->auto_extension
7612 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
7613 #define sqlite3_bind_text64 sqlite3_api->bind_text64
7614 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
7615 #define sqlite3_load_extension sqlite3_api->load_extension
7616 #define sqlite3_malloc64 sqlite3_api->malloc64
7617 #define sqlite3_msize sqlite3_api->msize
7618 #define sqlite3_realloc64 sqlite3_api->realloc64
7619 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
7620 #define sqlite3_result_blob64 sqlite3_api->result_blob64
7621 #define sqlite3_result_text64 sqlite3_api->result_text64
7622 #define sqlite3_strglob sqlite3_api->strglob
7623 /* Version 3.8.11 and later */
7624 #define sqlite3_value_dup sqlite3_api->value_dup
7625 #define sqlite3_value_free sqlite3_api->value_free
7626 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
7627 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
7628 /* Version 3.9.0 and later */
7629 #define sqlite3_value_subtype sqlite3_api->value_subtype
7630 #define sqlite3_result_subtype sqlite3_api->result_subtype
7631 /* Version 3.10.0 and later */
7632 #define sqlite3_status64 sqlite3_api->status64
7633 #define sqlite3_strlike sqlite3_api->strlike
7634 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
7635 /* Version 3.12.0 and later */
7636 #define sqlite3_system_errno sqlite3_api->system_errno
7637 /* Version 3.14.0 and later */
7638 #define sqlite3_trace_v2 sqlite3_api->trace_v2
7639 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
7640 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
7641
7642 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
7643 /* This case when the file really is being compiled as a loadable
7644 ** extension */
7645 # define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
7646 # define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
7647 # define SQLITE_EXTENSION_INIT3 \
7648 extern const sqlite3_api_routines *sqlite3_api;
7649 #else
7650 /* This case when the file is being statically linked into the
7651 ** application */
7652 # define SQLITE_EXTENSION_INIT1 /*no-op*/
7653 # define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
7654 # define SQLITE_EXTENSION_INIT3 /*no-op*/
7655 #endif
7656
7657 #endif /* SQLITE3EXT_H */
7658
7659 /************** End of sqlite3ext.h ******************************************/
7660 /************** Continuing where we left off in loadext.c ********************/
7661 /* #include "sqliteInt.h" */
7662
7663 #ifndef SQLITE_OMIT_LOAD_EXTENSION
7664 /*
7665 ** Some API routines are omitted when various features are
7666 ** excluded from a build of SQLite. Substitute a NULL pointer
7667 ** for any missing APIs.
7668 */
7669 #ifndef SQLITE_ENABLE_COLUMN_METADATA
7670 # define sqlite3_column_database_name 0
7671 # define sqlite3_column_database_name16 0
7672 # define sqlite3_column_table_name 0
7673 # define sqlite3_column_table_name16 0
7674 # define sqlite3_column_origin_name 0
7675 # define sqlite3_column_origin_name16 0
7676 #endif
7677
7678 #ifdef SQLITE_OMIT_AUTHORIZATION
7679 # define sqlite3_set_authorizer 0
7680 #endif
7681
7682 #ifdef SQLITE_OMIT_UTF16
7683 # define sqlite3_bind_text16 0
7684 # define sqlite3_collation_needed16 0
7685 # define sqlite3_column_decltype16 0
7686 # define sqlite3_column_name16 0
7687 # define sqlite3_column_text16 0
7688 # define sqlite3_complete16 0
7689 # define sqlite3_create_collation16 0
7690 # define sqlite3_create_function16 0
7691 # define sqlite3_errmsg16 0
7692 # define sqlite3_open16 0
7693 # define sqlite3_prepare16 0
7694 # define sqlite3_prepare16_v2 0
7695 # define sqlite3_result_error16 0
7696 # define sqlite3_result_text16 0
7697 # define sqlite3_result_text16be 0
7698 # define sqlite3_result_text16le 0
7699 # define sqlite3_value_text16 0
7700 # define sqlite3_value_text16be 0
7701 # define sqlite3_value_text16le 0
7702 # define sqlite3_column_database_name16 0
7703 # define sqlite3_column_table_name16 0
7704 # define sqlite3_column_origin_name16 0
7705 #endif
7706
7707 #ifdef SQLITE_OMIT_COMPLETE
7708 # define sqlite3_complete 0
7709 # define sqlite3_complete16 0
7710 #endif
7711
7712 #ifdef SQLITE_OMIT_DECLTYPE
7713 # define sqlite3_column_decltype16 0
7714 # define sqlite3_column_decltype 0
7715 #endif
7716
7717 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
7718 # define sqlite3_progress_handler 0
7719 #endif
7720
7721 #ifdef SQLITE_OMIT_VIRTUALTABLE
7722 # define sqlite3_create_module 0
7723 # define sqlite3_create_module_v2 0
7724 # define sqlite3_declare_vtab 0
7725 # define sqlite3_vtab_config 0
7726 # define sqlite3_vtab_on_conflict 0
7727 #endif
7728
7729 #ifdef SQLITE_OMIT_SHARED_CACHE
7730 # define sqlite3_enable_shared_cache 0
7731 #endif
7732
7733 #if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
7734 # define sqlite3_profile 0
7735 # define sqlite3_trace 0
7736 #endif
7737
7738 #ifdef SQLITE_OMIT_GET_TABLE
7739 # define sqlite3_free_table 0
7740 # define sqlite3_get_table 0
7741 #endif
7742
7743 #ifdef SQLITE_OMIT_INCRBLOB
7744 #define sqlite3_bind_zeroblob 0
7745 #define sqlite3_blob_bytes 0
7746 #define sqlite3_blob_close 0
7747 #define sqlite3_blob_open 0
7748 #define sqlite3_blob_read 0
7749 #define sqlite3_blob_write 0
7750 #define sqlite3_blob_reopen 0
7751 #endif
7752
7753 #if defined(SQLITE_OMIT_TRACE)
7754 # define sqlite3_trace_v2 0
7755 #endif
7756
7757 /*
7758 ** The following structure contains pointers to all SQLite API routines.
7759 ** A pointer to this structure is passed into extensions when they are
7760 ** loaded so that the extension can make calls back into the SQLite
7761 ** library.
7762 **
7763 ** When adding new APIs, add them to the bottom of this structure
7764 ** in order to preserve backwards compatibility.
7765 **
7766 ** Extensions that use newer APIs should first call the
7767 ** sqlite3_libversion_number() to make sure that the API they
7768 ** intend to use is supported by the library. Extensions should
7769 ** also check to make sure that the pointer to the function is
7770 ** not NULL before calling it.
7771 */
7772 static const sqlite3_api_routines sqlite3Apis = {
7773 sqlite3_aggregate_context,
7774 #ifndef SQLITE_OMIT_DEPRECATED
7775 sqlite3_aggregate_count,
7776 #else
7777 0,
7778 #endif
7779 sqlite3_bind_blob,
7780 sqlite3_bind_double,
7781 sqlite3_bind_int,
7782 sqlite3_bind_int64,
7783 sqlite3_bind_null,
7784 sqlite3_bind_parameter_count,
7785 sqlite3_bind_parameter_index,
7786 sqlite3_bind_parameter_name,
7787 sqlite3_bind_text,
7788 sqlite3_bind_text16,
7789 sqlite3_bind_value,
7790 sqlite3_busy_handler,
7791 sqlite3_busy_timeout,
7792 sqlite3_changes,
7793 sqlite3_close,
7794 sqlite3_collation_needed,
7795 sqlite3_collation_needed16,
7796 sqlite3_column_blob,
7797 sqlite3_column_bytes,
7798 sqlite3_column_bytes16,
7799 sqlite3_column_count,
7800 sqlite3_column_database_name,
7801 sqlite3_column_database_name16,
7802 sqlite3_column_decltype,
7803 sqlite3_column_decltype16,
7804 sqlite3_column_double,
7805 sqlite3_column_int,
7806 sqlite3_column_int64,
7807 sqlite3_column_name,
7808 sqlite3_column_name16,
7809 sqlite3_column_origin_name,
7810 sqlite3_column_origin_name16,
7811 sqlite3_column_table_name,
7812 sqlite3_column_table_name16,
7813 sqlite3_column_text,
7814 sqlite3_column_text16,
7815 sqlite3_column_type,
7816 sqlite3_column_value,
7817 sqlite3_commit_hook,
7818 sqlite3_complete,
7819 sqlite3_complete16,
7820 sqlite3_create_collation,
7821 sqlite3_create_collation16,
7822 sqlite3_create_function,
7823 sqlite3_create_function16,
7824 sqlite3_create_module,
7825 sqlite3_data_count,
7826 sqlite3_db_handle,
7827 sqlite3_declare_vtab,
7828 sqlite3_enable_shared_cache,
7829 sqlite3_errcode,
7830 sqlite3_errmsg,
7831 sqlite3_errmsg16,
7832 sqlite3_exec,
7833 #ifndef SQLITE_OMIT_DEPRECATED
7834 sqlite3_expired,
7835 #else
7836 0,
7837 #endif
7838 sqlite3_finalize,
7839 sqlite3_free,
7840 sqlite3_free_table,
7841 sqlite3_get_autocommit,
7842 sqlite3_get_auxdata,
7843 sqlite3_get_table,
7844 0, /* Was sqlite3_global_recover(), but that function is deprecated */
7845 sqlite3_interrupt,
7846 sqlite3_last_insert_rowid,
7847 sqlite3_libversion,
7848 sqlite3_libversion_number,
7849 sqlite3_malloc,
7850 sqlite3_mprintf,
7851 sqlite3_open,
7852 sqlite3_open16,
7853 sqlite3_prepare,
7854 sqlite3_prepare16,
7855 sqlite3_profile,
7856 sqlite3_progress_handler,
7857 sqlite3_realloc,
7858 sqlite3_reset,
7859 sqlite3_result_blob,
7860 sqlite3_result_double,
7861 sqlite3_result_error,
7862 sqlite3_result_error16,
7863 sqlite3_result_int,
7864 sqlite3_result_int64,
7865 sqlite3_result_null,
7866 sqlite3_result_text,
7867 sqlite3_result_text16,
7868 sqlite3_result_text16be,
7869 sqlite3_result_text16le,
7870 sqlite3_result_value,
7871 sqlite3_rollback_hook,
7872 sqlite3_set_authorizer,
7873 sqlite3_set_auxdata,
7874 sqlite3_snprintf,
7875 sqlite3_step,
7876 sqlite3_table_column_metadata,
7877 #ifndef SQLITE_OMIT_DEPRECATED
7878 sqlite3_thread_cleanup,
7879 #else
7880 0,
7881 #endif
7882 sqlite3_total_changes,
7883 sqlite3_trace,
7884 #ifndef SQLITE_OMIT_DEPRECATED
7885 sqlite3_transfer_bindings,
7886 #else
7887 0,
7888 #endif
7889 sqlite3_update_hook,
7890 sqlite3_user_data,
7891 sqlite3_value_blob,
7892 sqlite3_value_bytes,
7893 sqlite3_value_bytes16,
7894 sqlite3_value_double,
7895 sqlite3_value_int,
7896 sqlite3_value_int64,
7897 sqlite3_value_numeric_type,
7898 sqlite3_value_text,
7899 sqlite3_value_text16,
7900 sqlite3_value_text16be,
7901 sqlite3_value_text16le,
7902 sqlite3_value_type,
7903 sqlite3_vmprintf,
7904 /*
7905 ** The original API set ends here. All extensions can call any
7906 ** of the APIs above provided that the pointer is not NULL. But
7907 ** before calling APIs that follow, extension should check the
7908 ** sqlite3_libversion_number() to make sure they are dealing with
7909 ** a library that is new enough to support that API.
7910 *************************************************************************
7911 */
7912 sqlite3_overload_function,
7913
7914 /*
7915 ** Added after 3.3.13
7916 */
7917 sqlite3_prepare_v2,
7918 sqlite3_prepare16_v2,
7919 sqlite3_clear_bindings,
7920
7921 /*
7922 ** Added for 3.4.1
7923 */
7924 sqlite3_create_module_v2,
7925
7926 /*
7927 ** Added for 3.5.0
7928 */
7929 sqlite3_bind_zeroblob,
7930 sqlite3_blob_bytes,
7931 sqlite3_blob_close,
7932 sqlite3_blob_open,
7933 sqlite3_blob_read,
7934 sqlite3_blob_write,
7935 sqlite3_create_collation_v2,
7936 sqlite3_file_control,
7937 sqlite3_memory_highwater,
7938 sqlite3_memory_used,
7939 #ifdef SQLITE_MUTEX_OMIT
7940 0,
7941 0,
7942 0,
7943 0,
7944 0,
7945 #else
7946 sqlite3_mutex_alloc,
7947 sqlite3_mutex_enter,
7948 sqlite3_mutex_free,
7949 sqlite3_mutex_leave,
7950 sqlite3_mutex_try,
7951 #endif
7952 sqlite3_open_v2,
7953 sqlite3_release_memory,
7954 sqlite3_result_error_nomem,
7955 sqlite3_result_error_toobig,
7956 sqlite3_sleep,
7957 sqlite3_soft_heap_limit,
7958 sqlite3_vfs_find,
7959 sqlite3_vfs_register,
7960 sqlite3_vfs_unregister,
7961
7962 /*
7963 ** Added for 3.5.8
7964 */
7965 sqlite3_threadsafe,
7966 sqlite3_result_zeroblob,
7967 sqlite3_result_error_code,
7968 sqlite3_test_control,
7969 sqlite3_randomness,
7970 sqlite3_context_db_handle,
7971
7972 /*
7973 ** Added for 3.6.0
7974 */
7975 sqlite3_extended_result_codes,
7976 sqlite3_limit,
7977 sqlite3_next_stmt,
7978 sqlite3_sql,
7979 sqlite3_status,
7980
7981 /*
7982 ** Added for 3.7.4
7983 */
7984 sqlite3_backup_finish,
7985 sqlite3_backup_init,
7986 sqlite3_backup_pagecount,
7987 sqlite3_backup_remaining,
7988 sqlite3_backup_step,
7989 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
7990 sqlite3_compileoption_get,
7991 sqlite3_compileoption_used,
7992 #else
7993 0,
7994 0,
7995 #endif
7996 sqlite3_create_function_v2,
7997 sqlite3_db_config,
7998 sqlite3_db_mutex,
7999 sqlite3_db_status,
8000 sqlite3_extended_errcode,
8001 sqlite3_log,
8002 sqlite3_soft_heap_limit64,
8003 sqlite3_sourceid,
8004 sqlite3_stmt_status,
8005 sqlite3_strnicmp,
8006 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
8007 sqlite3_unlock_notify,
8008 #else
8009 0,
8010 #endif
8011 #ifndef SQLITE_OMIT_WAL
8012 sqlite3_wal_autocheckpoint,
8013 sqlite3_wal_checkpoint,
8014 sqlite3_wal_hook,
8015 #else
8016 0,
8017 0,
8018 0,
8019 #endif
8020 sqlite3_blob_reopen,
8021 sqlite3_vtab_config,
8022 sqlite3_vtab_on_conflict,
8023 sqlite3_close_v2,
8024 sqlite3_db_filename,
8025 sqlite3_db_readonly,
8026 sqlite3_db_release_memory,
8027 sqlite3_errstr,
8028 sqlite3_stmt_busy,
8029 sqlite3_stmt_readonly,
8030 sqlite3_stricmp,
8031 sqlite3_uri_boolean,
8032 sqlite3_uri_int64,
8033 sqlite3_uri_parameter,
8034 sqlite3_vsnprintf,
8035 sqlite3_wal_checkpoint_v2,
8036 /* Version 3.8.7 and later */
8037 sqlite3_auto_extension,
8038 sqlite3_bind_blob64,
8039 sqlite3_bind_text64,
8040 sqlite3_cancel_auto_extension,
8041 sqlite3_load_extension,
8042 sqlite3_malloc64,
8043 sqlite3_msize,
8044 sqlite3_realloc64,
8045 sqlite3_reset_auto_extension,
8046 sqlite3_result_blob64,
8047 sqlite3_result_text64,
8048 sqlite3_strglob,
8049 /* Version 3.8.11 and later */
8050 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
8051 sqlite3_value_free,
8052 sqlite3_result_zeroblob64,
8053 sqlite3_bind_zeroblob64,
8054 /* Version 3.9.0 and later */
8055 sqlite3_value_subtype,
8056 sqlite3_result_subtype,
8057 /* Version 3.10.0 and later */
8058 sqlite3_status64,
8059 sqlite3_strlike,
8060 sqlite3_db_cacheflush,
8061 /* Version 3.12.0 and later */
8062 sqlite3_system_errno,
8063 /* Version 3.14.0 and later */
8064 sqlite3_trace_v2,
8065 sqlite3_expanded_sql
8066 };
8067
8068 /*
8069 ** Attempt to load an SQLite extension library contained in the file
8070 ** zFile. The entry point is zProc. zProc may be 0 in which case a
8071 ** default entry point name (sqlite3_extension_init) is used. Use
8072 ** of the default name is recommended.
8073 **
8074 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
8075 **
8076 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
8077 ** error message text. The calling function should free this memory
8078 ** by calling sqlite3DbFree(db, ).
8079 */
8080 static int sqlite3LoadExtension(
8081 sqlite3 *db, /* Load the extension into this database connection */
8082 const char *zFile, /* Name of the shared library containing extension */
8083 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
8084 char **pzErrMsg /* Put error message here if not 0 */
8085 ){
8086 sqlite3_vfs *pVfs = db->pVfs;
8087 void *handle;
8088 sqlite3_loadext_entry xInit;
8089 char *zErrmsg = 0;
8090 const char *zEntry;
8091 char *zAltEntry = 0;
8092 void **aHandle;
8093 u64 nMsg = 300 + sqlite3Strlen30(zFile);
8094 int ii;
8095 int rc;
8096
8097 /* Shared library endings to try if zFile cannot be loaded as written */
8098 static const char *azEndings[] = {
8099 #if SQLITE_OS_WIN
8100 "dll"
8101 #elif defined(__APPLE__)
8102 "dylib"
8103 #else
8104 "so"
8105 #endif
8106 };
8107
8108
8109 if( pzErrMsg ) *pzErrMsg = 0;
8110
8111 /* Ticket #1863. To avoid a creating security problems for older
8112 ** applications that relink against newer versions of SQLite, the
8113 ** ability to run load_extension is turned off by default. One
8114 ** must call either sqlite3_enable_load_extension(db) or
8115 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
8116 ** to turn on extension loading.
8117 */
8118 if( (db->flags & SQLITE_LoadExtension)==0 ){
8119 if( pzErrMsg ){
8120 *pzErrMsg = sqlite3_mprintf("not authorized");
8121 }
8122 return SQLITE_ERROR;
8123 }
8124
8125 zEntry = zProc ? zProc : "sqlite3_extension_init";
8126
8127 handle = sqlite3OsDlOpen(pVfs, zFile);
8128 #if SQLITE_OS_UNIX || SQLITE_OS_WIN
8129 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
8130 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
8131 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
8132 handle = sqlite3OsDlOpen(pVfs, zAltFile);
8133 sqlite3_free(zAltFile);
8134 }
8135 #endif
8136 if( handle==0 ){
8137 if( pzErrMsg ){
8138 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
8139 if( zErrmsg ){
8140 sqlite3_snprintf(nMsg, zErrmsg,
8141 "unable to open shared library [%s]", zFile);
8142 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
8143 }
8144 }
8145 return SQLITE_ERROR;
8146 }
8147 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
8148
8149 /* If no entry point was specified and the default legacy
8150 ** entry point name "sqlite3_extension_init" was not found, then
8151 ** construct an entry point name "sqlite3_X_init" where the X is
8152 ** replaced by the lowercase value of every ASCII alphabetic
8153 ** character in the filename after the last "/" upto the first ".",
8154 ** and eliding the first three characters if they are "lib".
8155 ** Examples:
8156 **
8157 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
8158 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
8159 */
8160 if( xInit==0 && zProc==0 ){
8161 int iFile, iEntry, c;
8162 int ncFile = sqlite3Strlen30(zFile);
8163 zAltEntry = sqlite3_malloc64(ncFile+30);
8164 if( zAltEntry==0 ){
8165 sqlite3OsDlClose(pVfs, handle);
8166 return SQLITE_NOMEM_BKPT;
8167 }
8168 memcpy(zAltEntry, "sqlite3_", 8);
8169 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
8170 iFile++;
8171 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
8172 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
8173 if( sqlite3Isalpha(c) ){
8174 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
8175 }
8176 }
8177 memcpy(zAltEntry+iEntry, "_init", 6);
8178 zEntry = zAltEntry;
8179 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
8180 }
8181 if( xInit==0 ){
8182 if( pzErrMsg ){
8183 nMsg += sqlite3Strlen30(zEntry);
8184 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
8185 if( zErrmsg ){
8186 sqlite3_snprintf(nMsg, zErrmsg,
8187 "no entry point [%s] in shared library [%s]", zEntry, zFile);
8188 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
8189 }
8190 }
8191 sqlite3OsDlClose(pVfs, handle);
8192 sqlite3_free(zAltEntry);
8193 return SQLITE_ERROR;
8194 }
8195 sqlite3_free(zAltEntry);
8196 rc = xInit(db, &zErrmsg, &sqlite3Apis);
8197 if( rc ){
8198 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
8199 if( pzErrMsg ){
8200 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
8201 }
8202 sqlite3_free(zErrmsg);
8203 sqlite3OsDlClose(pVfs, handle);
8204 return SQLITE_ERROR;
8205 }
8206
8207 /* Append the new shared library handle to the db->aExtension array. */
8208 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
8209 if( aHandle==0 ){
8210 return SQLITE_NOMEM_BKPT;
8211 }
8212 if( db->nExtension>0 ){
8213 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
8214 }
8215 sqlite3DbFree(db, db->aExtension);
8216 db->aExtension = aHandle;
8217
8218 db->aExtension[db->nExtension++] = handle;
8219 return SQLITE_OK;
8220 }
8221 SQLITE_API int sqlite3_load_extension(
8222 sqlite3 *db, /* Load the extension into this database connection */
8223 const char *zFile, /* Name of the shared library containing extension */
8224 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
8225 char **pzErrMsg /* Put error message here if not 0 */
8226 ){
8227 int rc;
8228 sqlite3_mutex_enter(db->mutex);
8229 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
8230 rc = sqlite3ApiExit(db, rc);
8231 sqlite3_mutex_leave(db->mutex);
8232 return rc;
8233 }
8234
8235 /*
8236 ** Call this routine when the database connection is closing in order
8237 ** to clean up loaded extensions
8238 */
8239 SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
8240 int i;
8241 assert( sqlite3_mutex_held(db->mutex) );
8242 for(i=0; i<db->nExtension; i++){
8243 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
8244 }
8245 sqlite3DbFree(db, db->aExtension);
8246 }
8247
8248 /*
8249 ** Enable or disable extension loading. Extension loading is disabled by
8250 ** default so as not to open security holes in older applications.
8251 */
8252 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
8253 sqlite3_mutex_enter(db->mutex);
8254 if( onoff ){
8255 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
8256 }else{
8257 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
8258 }
8259 sqlite3_mutex_leave(db->mutex);
8260 return SQLITE_OK;
8261 }
8262
8263 #endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
8264
8265 /*
8266 ** The following object holds the list of automatically loaded
8267 ** extensions.
8268 **
8269 ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
8270 ** mutex must be held while accessing this list.
8271 */
8272 typedef struct sqlite3AutoExtList sqlite3AutoExtList;
8273 static SQLITE_WSD struct sqlite3AutoExtList {
8274 u32 nExt; /* Number of entries in aExt[] */
8275 void (**aExt)(void); /* Pointers to the extension init functions */
8276 } sqlite3Autoext = { 0, 0 };
8277
8278 /* The "wsdAutoext" macro will resolve to the autoextension
8279 ** state vector. If writable static data is unsupported on the target,
8280 ** we have to locate the state vector at run-time. In the more common
8281 ** case where writable static data is supported, wsdStat can refer directly
8282 ** to the "sqlite3Autoext" state vector declared above.
8283 */
8284 #ifdef SQLITE_OMIT_WSD
8285 # define wsdAutoextInit \
8286 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
8287 # define wsdAutoext x[0]
8288 #else
8289 # define wsdAutoextInit
8290 # define wsdAutoext sqlite3Autoext
8291 #endif
8292
8293
8294 /*
8295 ** Register a statically linked extension that is automatically
8296 ** loaded by every new database connection.
8297 */
8298 SQLITE_API int sqlite3_auto_extension(
8299 void (*xInit)(void)
8300 ){
8301 int rc = SQLITE_OK;
8302 #ifndef SQLITE_OMIT_AUTOINIT
8303 rc = sqlite3_initialize();
8304 if( rc ){
8305 return rc;
8306 }else
8307 #endif
8308 {
8309 u32 i;
8310 #if SQLITE_THREADSAFE
8311 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
8312 #endif
8313 wsdAutoextInit;
8314 sqlite3_mutex_enter(mutex);
8315 for(i=0; i<wsdAutoext.nExt; i++){
8316 if( wsdAutoext.aExt[i]==xInit ) break;
8317 }
8318 if( i==wsdAutoext.nExt ){
8319 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
8320 void (**aNew)(void);
8321 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
8322 if( aNew==0 ){
8323 rc = SQLITE_NOMEM_BKPT;
8324 }else{
8325 wsdAutoext.aExt = aNew;
8326 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
8327 wsdAutoext.nExt++;
8328 }
8329 }
8330 sqlite3_mutex_leave(mutex);
8331 assert( (rc&0xff)==rc );
8332 return rc;
8333 }
8334 }
8335
8336 /*
8337 ** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
8338 ** set of routines that is invoked for each new database connection, if it
8339 ** is currently on the list. If xInit is not on the list, then this
8340 ** routine is a no-op.
8341 **
8342 ** Return 1 if xInit was found on the list and removed. Return 0 if xInit
8343 ** was not on the list.
8344 */
8345 SQLITE_API int sqlite3_cancel_auto_extension(
8346 void (*xInit)(void)
8347 ){
8348 #if SQLITE_THREADSAFE
8349 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
8350 #endif
8351 int i;
8352 int n = 0;
8353 wsdAutoextInit;
8354 sqlite3_mutex_enter(mutex);
8355 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
8356 if( wsdAutoext.aExt[i]==xInit ){
8357 wsdAutoext.nExt--;
8358 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
8359 n++;
8360 break;
8361 }
8362 }
8363 sqlite3_mutex_leave(mutex);
8364 return n;
8365 }
8366
8367 /*
8368 ** Reset the automatic extension loading mechanism.
8369 */
8370 SQLITE_API void sqlite3_reset_auto_extension(void){
8371 #ifndef SQLITE_OMIT_AUTOINIT
8372 if( sqlite3_initialize()==SQLITE_OK )
8373 #endif
8374 {
8375 #if SQLITE_THREADSAFE
8376 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
8377 #endif
8378 wsdAutoextInit;
8379 sqlite3_mutex_enter(mutex);
8380 sqlite3_free(wsdAutoext.aExt);
8381 wsdAutoext.aExt = 0;
8382 wsdAutoext.nExt = 0;
8383 sqlite3_mutex_leave(mutex);
8384 }
8385 }
8386
8387 /*
8388 ** Load all automatic extensions.
8389 **
8390 ** If anything goes wrong, set an error in the database connection.
8391 */
8392 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
8393 u32 i;
8394 int go = 1;
8395 int rc;
8396 sqlite3_loadext_entry xInit;
8397
8398 wsdAutoextInit;
8399 if( wsdAutoext.nExt==0 ){
8400 /* Common case: early out without every having to acquire a mutex */
8401 return;
8402 }
8403 for(i=0; go; i++){
8404 char *zErrmsg;
8405 #if SQLITE_THREADSAFE
8406 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
8407 #endif
8408 #ifdef SQLITE_OMIT_LOAD_EXTENSION
8409 const sqlite3_api_routines *pThunk = 0;
8410 #else
8411 const sqlite3_api_routines *pThunk = &sqlite3Apis;
8412 #endif
8413 sqlite3_mutex_enter(mutex);
8414 if( i>=wsdAutoext.nExt ){
8415 xInit = 0;
8416 go = 0;
8417 }else{
8418 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
8419 }
8420 sqlite3_mutex_leave(mutex);
8421 zErrmsg = 0;
8422 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
8423 sqlite3ErrorWithMsg(db, rc,
8424 "automatic extension loading failed: %s", zErrmsg);
8425 go = 0;
8426 }
8427 sqlite3_free(zErrmsg);
8428 }
8429 }
8430
8431 /************** End of loadext.c *********************************************/
8432 /************** Begin file pragma.c ******************************************/
8433 /*
8434 ** 2003 April 6
8435 **
8436 ** The author disclaims copyright to this source code. In place of
8437 ** a legal notice, here is a blessing:
8438 **
8439 ** May you do good and not evil.
8440 ** May you find forgiveness for yourself and forgive others.
8441 ** May you share freely, never taking more than you give.
8442 **
8443 *************************************************************************
8444 ** This file contains code used to implement the PRAGMA command.
8445 */
8446 /* #include "sqliteInt.h" */
8447
8448 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
8449 # if defined(__APPLE__)
8450 # define SQLITE_ENABLE_LOCKING_STYLE 1
8451 # else
8452 # define SQLITE_ENABLE_LOCKING_STYLE 0
8453 # endif
8454 #endif
8455
8456 /***************************************************************************
8457 ** The "pragma.h" include file is an automatically generated file that
8458 ** that includes the PragType_XXXX macro definitions and the aPragmaName[]
8459 ** object. This ensures that the aPragmaName[] table is arranged in
8460 ** lexicographical order to facility a binary search of the pragma name.
8461 ** Do not edit pragma.h directly. Edit and rerun the script in at
8462 ** ../tool/mkpragmatab.tcl. */
8463 /************** Include pragma.h in the middle of pragma.c *******************/
8464 /************** Begin file pragma.h ******************************************/
8465 /* DO NOT EDIT!
8466 ** This file is automatically generated by the script at
8467 ** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
8468 ** that script and rerun it.
8469 */
8470
8471 /* The various pragma types */
8472 #define PragTyp_HEADER_VALUE 0
8473 #define PragTyp_AUTO_VACUUM 1
8474 #define PragTyp_FLAG 2
8475 #define PragTyp_BUSY_TIMEOUT 3
8476 #define PragTyp_CACHE_SIZE 4
8477 #define PragTyp_CACHE_SPILL 5
8478 #define PragTyp_CASE_SENSITIVE_LIKE 6
8479 #define PragTyp_COLLATION_LIST 7
8480 #define PragTyp_COMPILE_OPTIONS 8
8481 #define PragTyp_DATA_STORE_DIRECTORY 9
8482 #define PragTyp_DATABASE_LIST 10
8483 #define PragTyp_DEFAULT_CACHE_SIZE 11
8484 #define PragTyp_ENCODING 12
8485 #define PragTyp_FOREIGN_KEY_CHECK 13
8486 #define PragTyp_FOREIGN_KEY_LIST 14
8487 #define PragTyp_INCREMENTAL_VACUUM 15
8488 #define PragTyp_INDEX_INFO 16
8489 #define PragTyp_INDEX_LIST 17
8490 #define PragTyp_INTEGRITY_CHECK 18
8491 #define PragTyp_JOURNAL_MODE 19
8492 #define PragTyp_JOURNAL_SIZE_LIMIT 20
8493 #define PragTyp_LOCK_PROXY_FILE 21
8494 #define PragTyp_LOCKING_MODE 22
8495 #define PragTyp_PAGE_COUNT 23
8496 #define PragTyp_MMAP_SIZE 24
8497 #define PragTyp_PAGE_SIZE 25
8498 #define PragTyp_SECURE_DELETE 26
8499 #define PragTyp_SHRINK_MEMORY 27
8500 #define PragTyp_SOFT_HEAP_LIMIT 28
8501 #define PragTyp_STATS 29
8502 #define PragTyp_SYNCHRONOUS 30
8503 #define PragTyp_TABLE_INFO 31
8504 #define PragTyp_TEMP_STORE 32
8505 #define PragTyp_TEMP_STORE_DIRECTORY 33
8506 #define PragTyp_THREADS 34
8507 #define PragTyp_WAL_AUTOCHECKPOINT 35
8508 #define PragTyp_WAL_CHECKPOINT 36
8509 #define PragTyp_ACTIVATE_EXTENSIONS 37
8510 #define PragTyp_HEXKEY 38
8511 #define PragTyp_KEY 39
8512 #define PragTyp_REKEY 40
8513 #define PragTyp_LOCK_STATUS 41
8514 #define PragTyp_PARSER_TRACE 42
8515
8516 /* Property flags associated with various pragma. */
8517 #define PragFlg_NeedSchema 0x01 /* Force schema load before running */
8518 #define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */
8519 #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
8520 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
8521 #define PragFlg_Result0 0x10 /* Acts as query when no argument */
8522 #define PragFlg_Result1 0x20 /* Acts as query when has one argument */
8523 #define PragFlg_SchemaOpt 0x40 /* Schema restricts name search if present */
8524 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
8525
8526 /* Names of columns for pragmas that return multi-column result
8527 ** or that return single-column results where the name of the
8528 ** result column is different from the name of the pragma
8529 */
8530 static const char *const pragCName[] = {
8531 /* 0 */ "cache_size", /* Used by: default_cache_size */
8532 /* 1 */ "cid", /* Used by: table_info */
8533 /* 2 */ "name",
8534 /* 3 */ "type",
8535 /* 4 */ "notnull",
8536 /* 5 */ "dflt_value",
8537 /* 6 */ "pk",
8538 /* 7 */ "table", /* Used by: stats */
8539 /* 8 */ "index",
8540 /* 9 */ "width",
8541 /* 10 */ "height",
8542 /* 11 */ "seqno", /* Used by: index_info */
8543 /* 12 */ "cid",
8544 /* 13 */ "name",
8545 /* 14 */ "seqno", /* Used by: index_xinfo */
8546 /* 15 */ "cid",
8547 /* 16 */ "name",
8548 /* 17 */ "desc",
8549 /* 18 */ "coll",
8550 /* 19 */ "key",
8551 /* 20 */ "seq", /* Used by: index_list */
8552 /* 21 */ "name",
8553 /* 22 */ "unique",
8554 /* 23 */ "origin",
8555 /* 24 */ "partial",
8556 /* 25 */ "seq", /* Used by: database_list */
8557 /* 26 */ "name",
8558 /* 27 */ "file",
8559 /* 28 */ "seq", /* Used by: collation_list */
8560 /* 29 */ "name",
8561 /* 30 */ "id", /* Used by: foreign_key_list */
8562 /* 31 */ "seq",
8563 /* 32 */ "table",
8564 /* 33 */ "from",
8565 /* 34 */ "to",
8566 /* 35 */ "on_update",
8567 /* 36 */ "on_delete",
8568 /* 37 */ "match",
8569 /* 38 */ "table", /* Used by: foreign_key_check */
8570 /* 39 */ "rowid",
8571 /* 40 */ "parent",
8572 /* 41 */ "fkid",
8573 /* 42 */ "busy", /* Used by: wal_checkpoint */
8574 /* 43 */ "log",
8575 /* 44 */ "checkpointed",
8576 /* 45 */ "timeout", /* Used by: busy_timeout */
8577 /* 46 */ "database", /* Used by: lock_status */
8578 /* 47 */ "status",
8579 };
8580
8581 /* Definitions of all built-in pragmas */
8582 typedef struct PragmaName {
8583 const char *const zName; /* Name of pragma */
8584 u8 ePragTyp; /* PragTyp_XXX value */
8585 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
8586 u8 iPragCName; /* Start of column names in pragCName[] */
8587 u8 nPragCName; /* Num of col names. 0 means use pragma name */
8588 u32 iArg; /* Extra argument */
8589 } PragmaName;
8590 static const PragmaName aPragmaName[] = {
8591 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
8592 {/* zName: */ "activate_extensions",
8593 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
8594 /* ePragFlg: */ 0,
8595 /* ColNames: */ 0, 0,
8596 /* iArg: */ 0 },
8597 #endif
8598 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
8599 {/* zName: */ "application_id",
8600 /* ePragTyp: */ PragTyp_HEADER_VALUE,
8601 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
8602 /* ColNames: */ 0, 0,
8603 /* iArg: */ BTREE_APPLICATION_ID },
8604 #endif
8605 #if !defined(SQLITE_OMIT_AUTOVACUUM)
8606 {/* zName: */ "auto_vacuum",
8607 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
8608 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_ NoColumns1,
8609 /* ColNames: */ 0, 0,
8610 /* iArg: */ 0 },
8611 #endif
8612 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8613 #if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
8614 {/* zName: */ "automatic_index",
8615 /* ePragTyp: */ PragTyp_FLAG,
8616 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8617 /* ColNames: */ 0, 0,
8618 /* iArg: */ SQLITE_AutoIndex },
8619 #endif
8620 #endif
8621 {/* zName: */ "busy_timeout",
8622 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
8623 /* ePragFlg: */ PragFlg_Result0,
8624 /* ColNames: */ 45, 1,
8625 /* iArg: */ 0 },
8626 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
8627 {/* zName: */ "cache_size",
8628 /* ePragTyp: */ PragTyp_CACHE_SIZE,
8629 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_ NoColumns1,
8630 /* ColNames: */ 0, 0,
8631 /* iArg: */ 0 },
8632 #endif
8633 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8634 {/* zName: */ "cache_spill",
8635 /* ePragTyp: */ PragTyp_CACHE_SPILL,
8636 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
8637 /* ColNames: */ 0, 0,
8638 /* iArg: */ 0 },
8639 #endif
8640 {/* zName: */ "case_sensitive_like",
8641 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
8642 /* ePragFlg: */ PragFlg_NoColumns,
8643 /* ColNames: */ 0, 0,
8644 /* iArg: */ 0 },
8645 {/* zName: */ "cell_size_check",
8646 /* ePragTyp: */ PragTyp_FLAG,
8647 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8648 /* ColNames: */ 0, 0,
8649 /* iArg: */ SQLITE_CellSizeCk },
8650 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8651 {/* zName: */ "checkpoint_fullfsync",
8652 /* ePragTyp: */ PragTyp_FLAG,
8653 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8654 /* ColNames: */ 0, 0,
8655 /* iArg: */ SQLITE_CkptFullFSync },
8656 #endif
8657 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
8658 {/* zName: */ "collation_list",
8659 /* ePragTyp: */ PragTyp_COLLATION_LIST,
8660 /* ePragFlg: */ PragFlg_Result0,
8661 /* ColNames: */ 28, 2,
8662 /* iArg: */ 0 },
8663 #endif
8664 #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
8665 {/* zName: */ "compile_options",
8666 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
8667 /* ePragFlg: */ PragFlg_Result0,
8668 /* ColNames: */ 0, 0,
8669 /* iArg: */ 0 },
8670 #endif
8671 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8672 {/* zName: */ "count_changes",
8673 /* ePragTyp: */ PragTyp_FLAG,
8674 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8675 /* ColNames: */ 0, 0,
8676 /* iArg: */ SQLITE_CountRows },
8677 #endif
8678 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
8679 {/* zName: */ "data_store_directory",
8680 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
8681 /* ePragFlg: */ PragFlg_NoColumns1,
8682 /* ColNames: */ 0, 0,
8683 /* iArg: */ 0 },
8684 #endif
8685 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
8686 {/* zName: */ "data_version",
8687 /* ePragTyp: */ PragTyp_HEADER_VALUE,
8688 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
8689 /* ColNames: */ 0, 0,
8690 /* iArg: */ BTREE_DATA_VERSION },
8691 #endif
8692 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
8693 {/* zName: */ "database_list",
8694 /* ePragTyp: */ PragTyp_DATABASE_LIST,
8695 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0,
8696 /* ColNames: */ 25, 3,
8697 /* iArg: */ 0 },
8698 #endif
8699 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
8700 {/* zName: */ "default_cache_size",
8701 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
8702 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_ NoColumns1,
8703 /* ColNames: */ 0, 1,
8704 /* iArg: */ 0 },
8705 #endif
8706 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8707 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
8708 {/* zName: */ "defer_foreign_keys",
8709 /* ePragTyp: */ PragTyp_FLAG,
8710 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8711 /* ColNames: */ 0, 0,
8712 /* iArg: */ SQLITE_DeferFKs },
8713 #endif
8714 #endif
8715 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8716 {/* zName: */ "empty_result_callbacks",
8717 /* ePragTyp: */ PragTyp_FLAG,
8718 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8719 /* ColNames: */ 0, 0,
8720 /* iArg: */ SQLITE_NullCallback },
8721 #endif
8722 #if !defined(SQLITE_OMIT_UTF16)
8723 {/* zName: */ "encoding",
8724 /* ePragTyp: */ PragTyp_ENCODING,
8725 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8726 /* ColNames: */ 0, 0,
8727 /* iArg: */ 0 },
8728 #endif
8729 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
8730 {/* zName: */ "foreign_key_check",
8731 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
8732 /* ePragFlg: */ PragFlg_NeedSchema,
8733 /* ColNames: */ 38, 4,
8734 /* iArg: */ 0 },
8735 #endif
8736 #if !defined(SQLITE_OMIT_FOREIGN_KEY)
8737 {/* zName: */ "foreign_key_list",
8738 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
8739 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
8740 /* ColNames: */ 30, 8,
8741 /* iArg: */ 0 },
8742 #endif
8743 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8744 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
8745 {/* zName: */ "foreign_keys",
8746 /* ePragTyp: */ PragTyp_FLAG,
8747 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8748 /* ColNames: */ 0, 0,
8749 /* iArg: */ SQLITE_ForeignKeys },
8750 #endif
8751 #endif
8752 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
8753 {/* zName: */ "freelist_count",
8754 /* ePragTyp: */ PragTyp_HEADER_VALUE,
8755 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
8756 /* ColNames: */ 0, 0,
8757 /* iArg: */ BTREE_FREE_PAGE_COUNT },
8758 #endif
8759 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8760 {/* zName: */ "full_column_names",
8761 /* ePragTyp: */ PragTyp_FLAG,
8762 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8763 /* ColNames: */ 0, 0,
8764 /* iArg: */ SQLITE_FullColNames },
8765 {/* zName: */ "fullfsync",
8766 /* ePragTyp: */ PragTyp_FLAG,
8767 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8768 /* ColNames: */ 0, 0,
8769 /* iArg: */ SQLITE_FullFSync },
8770 #endif
8771 #if defined(SQLITE_HAS_CODEC)
8772 {/* zName: */ "hexkey",
8773 /* ePragTyp: */ PragTyp_HEXKEY,
8774 /* ePragFlg: */ 0,
8775 /* ColNames: */ 0, 0,
8776 /* iArg: */ 0 },
8777 {/* zName: */ "hexrekey",
8778 /* ePragTyp: */ PragTyp_HEXKEY,
8779 /* ePragFlg: */ 0,
8780 /* ColNames: */ 0, 0,
8781 /* iArg: */ 0 },
8782 #endif
8783 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8784 #if !defined(SQLITE_OMIT_CHECK)
8785 {/* zName: */ "ignore_check_constraints",
8786 /* ePragTyp: */ PragTyp_FLAG,
8787 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8788 /* ColNames: */ 0, 0,
8789 /* iArg: */ SQLITE_IgnoreChecks },
8790 #endif
8791 #endif
8792 #if !defined(SQLITE_OMIT_AUTOVACUUM)
8793 {/* zName: */ "incremental_vacuum",
8794 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
8795 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_NoColumns,
8796 /* ColNames: */ 0, 0,
8797 /* iArg: */ 0 },
8798 #endif
8799 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
8800 {/* zName: */ "index_info",
8801 /* ePragTyp: */ PragTyp_INDEX_INFO,
8802 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
8803 /* ColNames: */ 11, 3,
8804 /* iArg: */ 0 },
8805 {/* zName: */ "index_list",
8806 /* ePragTyp: */ PragTyp_INDEX_LIST,
8807 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
8808 /* ColNames: */ 20, 5,
8809 /* iArg: */ 0 },
8810 {/* zName: */ "index_xinfo",
8811 /* ePragTyp: */ PragTyp_INDEX_INFO,
8812 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
8813 /* ColNames: */ 14, 6,
8814 /* iArg: */ 1 },
8815 #endif
8816 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
8817 {/* zName: */ "integrity_check",
8818 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
8819 /* ePragFlg: */ PragFlg_NeedSchema,
8820 /* ColNames: */ 0, 0,
8821 /* iArg: */ 0 },
8822 #endif
8823 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
8824 {/* zName: */ "journal_mode",
8825 /* ePragTyp: */ PragTyp_JOURNAL_MODE,
8826 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
8827 /* ColNames: */ 0, 0,
8828 /* iArg: */ 0 },
8829 {/* zName: */ "journal_size_limit",
8830 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
8831 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
8832 /* ColNames: */ 0, 0,
8833 /* iArg: */ 0 },
8834 #endif
8835 #if defined(SQLITE_HAS_CODEC)
8836 {/* zName: */ "key",
8837 /* ePragTyp: */ PragTyp_KEY,
8838 /* ePragFlg: */ 0,
8839 /* ColNames: */ 0, 0,
8840 /* iArg: */ 0 },
8841 #endif
8842 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8843 {/* zName: */ "legacy_file_format",
8844 /* ePragTyp: */ PragTyp_FLAG,
8845 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8846 /* ColNames: */ 0, 0,
8847 /* iArg: */ SQLITE_LegacyFileFmt },
8848 #endif
8849 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
8850 {/* zName: */ "lock_proxy_file",
8851 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
8852 /* ePragFlg: */ PragFlg_NoColumns1,
8853 /* ColNames: */ 0, 0,
8854 /* iArg: */ 0 },
8855 #endif
8856 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
8857 {/* zName: */ "lock_status",
8858 /* ePragTyp: */ PragTyp_LOCK_STATUS,
8859 /* ePragFlg: */ PragFlg_Result0,
8860 /* ColNames: */ 46, 2,
8861 /* iArg: */ 0 },
8862 #endif
8863 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
8864 {/* zName: */ "locking_mode",
8865 /* ePragTyp: */ PragTyp_LOCKING_MODE,
8866 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
8867 /* ColNames: */ 0, 0,
8868 /* iArg: */ 0 },
8869 {/* zName: */ "max_page_count",
8870 /* ePragTyp: */ PragTyp_PAGE_COUNT,
8871 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
8872 /* ColNames: */ 0, 0,
8873 /* iArg: */ 0 },
8874 {/* zName: */ "mmap_size",
8875 /* ePragTyp: */ PragTyp_MMAP_SIZE,
8876 /* ePragFlg: */ 0,
8877 /* ColNames: */ 0, 0,
8878 /* iArg: */ 0 },
8879 {/* zName: */ "page_count",
8880 /* ePragTyp: */ PragTyp_PAGE_COUNT,
8881 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
8882 /* ColNames: */ 0, 0,
8883 /* iArg: */ 0 },
8884 {/* zName: */ "page_size",
8885 /* ePragTyp: */ PragTyp_PAGE_SIZE,
8886 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
8887 /* ColNames: */ 0, 0,
8888 /* iArg: */ 0 },
8889 #endif
8890 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE)
8891 {/* zName: */ "parser_trace",
8892 /* ePragTyp: */ PragTyp_PARSER_TRACE,
8893 /* ePragFlg: */ 0,
8894 /* ColNames: */ 0, 0,
8895 /* iArg: */ 0 },
8896 #endif
8897 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8898 {/* zName: */ "query_only",
8899 /* ePragTyp: */ PragTyp_FLAG,
8900 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8901 /* ColNames: */ 0, 0,
8902 /* iArg: */ SQLITE_QueryOnly },
8903 #endif
8904 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
8905 {/* zName: */ "quick_check",
8906 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
8907 /* ePragFlg: */ PragFlg_NeedSchema,
8908 /* ColNames: */ 0, 0,
8909 /* iArg: */ 0 },
8910 #endif
8911 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8912 {/* zName: */ "read_uncommitted",
8913 /* ePragTyp: */ PragTyp_FLAG,
8914 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8915 /* ColNames: */ 0, 0,
8916 /* iArg: */ SQLITE_ReadUncommitted },
8917 {/* zName: */ "recursive_triggers",
8918 /* ePragTyp: */ PragTyp_FLAG,
8919 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8920 /* ColNames: */ 0, 0,
8921 /* iArg: */ SQLITE_RecTriggers },
8922 #endif
8923 #if defined(SQLITE_HAS_CODEC)
8924 {/* zName: */ "rekey",
8925 /* ePragTyp: */ PragTyp_REKEY,
8926 /* ePragFlg: */ 0,
8927 /* ColNames: */ 0, 0,
8928 /* iArg: */ 0 },
8929 #endif
8930 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8931 {/* zName: */ "reverse_unordered_selects",
8932 /* ePragTyp: */ PragTyp_FLAG,
8933 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8934 /* ColNames: */ 0, 0,
8935 /* iArg: */ SQLITE_ReverseOrder },
8936 #endif
8937 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
8938 {/* zName: */ "schema_version",
8939 /* ePragTyp: */ PragTyp_HEADER_VALUE,
8940 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
8941 /* ColNames: */ 0, 0,
8942 /* iArg: */ BTREE_SCHEMA_VERSION },
8943 #endif
8944 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
8945 {/* zName: */ "secure_delete",
8946 /* ePragTyp: */ PragTyp_SECURE_DELETE,
8947 /* ePragFlg: */ PragFlg_Result0,
8948 /* ColNames: */ 0, 0,
8949 /* iArg: */ 0 },
8950 #endif
8951 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8952 {/* zName: */ "short_column_names",
8953 /* ePragTyp: */ PragTyp_FLAG,
8954 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8955 /* ColNames: */ 0, 0,
8956 /* iArg: */ SQLITE_ShortColNames },
8957 #endif
8958 {/* zName: */ "shrink_memory",
8959 /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
8960 /* ePragFlg: */ PragFlg_NoColumns,
8961 /* ColNames: */ 0, 0,
8962 /* iArg: */ 0 },
8963 {/* zName: */ "soft_heap_limit",
8964 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
8965 /* ePragFlg: */ PragFlg_Result0,
8966 /* ColNames: */ 0, 0,
8967 /* iArg: */ 0 },
8968 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
8969 #if defined(SQLITE_DEBUG)
8970 {/* zName: */ "sql_trace",
8971 /* ePragTyp: */ PragTyp_FLAG,
8972 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
8973 /* ColNames: */ 0, 0,
8974 /* iArg: */ SQLITE_SqlTrace },
8975 #endif
8976 #endif
8977 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
8978 {/* zName: */ "stats",
8979 /* ePragTyp: */ PragTyp_STATS,
8980 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
8981 /* ColNames: */ 7, 4,
8982 /* iArg: */ 0 },
8983 #endif
8984 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
8985 {/* zName: */ "synchronous",
8986 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
8987 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_ NoColumns1,
8988 /* ColNames: */ 0, 0,
8989 /* iArg: */ 0 },
8990 #endif
8991 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
8992 {/* zName: */ "table_info",
8993 /* ePragTyp: */ PragTyp_TABLE_INFO,
8994 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
8995 /* ColNames: */ 1, 6,
8996 /* iArg: */ 0 },
8997 #endif
8998 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
8999 {/* zName: */ "temp_store",
9000 /* ePragTyp: */ PragTyp_TEMP_STORE,
9001 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
9002 /* ColNames: */ 0, 0,
9003 /* iArg: */ 0 },
9004 {/* zName: */ "temp_store_directory",
9005 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
9006 /* ePragFlg: */ PragFlg_NoColumns1,
9007 /* ColNames: */ 0, 0,
9008 /* iArg: */ 0 },
9009 #endif
9010 {/* zName: */ "threads",
9011 /* ePragTyp: */ PragTyp_THREADS,
9012 /* ePragFlg: */ PragFlg_Result0,
9013 /* ColNames: */ 0, 0,
9014 /* iArg: */ 0 },
9015 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
9016 {/* zName: */ "user_version",
9017 /* ePragTyp: */ PragTyp_HEADER_VALUE,
9018 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
9019 /* ColNames: */ 0, 0,
9020 /* iArg: */ BTREE_USER_VERSION },
9021 #endif
9022 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
9023 #if defined(SQLITE_DEBUG)
9024 {/* zName: */ "vdbe_addoptrace",
9025 /* ePragTyp: */ PragTyp_FLAG,
9026 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
9027 /* ColNames: */ 0, 0,
9028 /* iArg: */ SQLITE_VdbeAddopTrace },
9029 {/* zName: */ "vdbe_debug",
9030 /* ePragTyp: */ PragTyp_FLAG,
9031 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
9032 /* ColNames: */ 0, 0,
9033 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
9034 {/* zName: */ "vdbe_eqp",
9035 /* ePragTyp: */ PragTyp_FLAG,
9036 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
9037 /* ColNames: */ 0, 0,
9038 /* iArg: */ SQLITE_VdbeEQP },
9039 {/* zName: */ "vdbe_listing",
9040 /* ePragTyp: */ PragTyp_FLAG,
9041 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
9042 /* ColNames: */ 0, 0,
9043 /* iArg: */ SQLITE_VdbeListing },
9044 {/* zName: */ "vdbe_trace",
9045 /* ePragTyp: */ PragTyp_FLAG,
9046 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
9047 /* ColNames: */ 0, 0,
9048 /* iArg: */ SQLITE_VdbeTrace },
9049 #endif
9050 #endif
9051 #if !defined(SQLITE_OMIT_WAL)
9052 {/* zName: */ "wal_autocheckpoint",
9053 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
9054 /* ePragFlg: */ 0,
9055 /* ColNames: */ 0, 0,
9056 /* iArg: */ 0 },
9057 {/* zName: */ "wal_checkpoint",
9058 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
9059 /* ePragFlg: */ PragFlg_NeedSchema,
9060 /* ColNames: */ 42, 3,
9061 /* iArg: */ 0 },
9062 #endif
9063 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
9064 {/* zName: */ "writable_schema",
9065 /* ePragTyp: */ PragTyp_FLAG,
9066 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
9067 /* ColNames: */ 0, 0,
9068 /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
9069 #endif
9070 };
9071 /* Number of pragmas: 60 on by default, 73 total. */
9072
9073 /************** End of pragma.h **********************************************/
9074 /************** Continuing where we left off in pragma.c *********************/
9075
9076 /*
9077 ** Interpret the given string as a safety level. Return 0 for OFF,
9078 ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
9079 ** unrecognized string argument. The FULL and EXTRA option is disallowed
9080 ** if the omitFull parameter it 1.
9081 **
9082 ** Note that the values returned are one less that the values that
9083 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
9084 ** to support legacy SQL code. The safety level used to be boolean
9085 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
9086 */
9087 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
9088 /* 123456789 123456789 123 */
9089 static const char zText[] = "onoffalseyestruextrafull";
9090 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
9091 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
9092 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
9093 /* on no off false yes true extra full */
9094 int i, n;
9095 if( sqlite3Isdigit(*z) ){
9096 return (u8)sqlite3Atoi(z);
9097 }
9098 n = sqlite3Strlen30(z);
9099 for(i=0; i<ArraySize(iLength); i++){
9100 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
9101 && (!omitFull || iValue[i]<=1)
9102 ){
9103 return iValue[i];
9104 }
9105 }
9106 return dflt;
9107 }
9108
9109 /*
9110 ** Interpret the given string as a boolean value.
9111 */
9112 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
9113 return getSafetyLevel(z,1,dflt)!=0;
9114 }
9115
9116 /* The sqlite3GetBoolean() function is used by other modules but the
9117 ** remainder of this file is specific to PRAGMA processing. So omit
9118 ** the rest of the file if PRAGMAs are omitted from the build.
9119 */
9120 #if !defined(SQLITE_OMIT_PRAGMA)
9121
9122 /*
9123 ** Interpret the given string as a locking mode value.
9124 */
9125 static int getLockingMode(const char *z){
9126 if( z ){
9127 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
9128 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
9129 }
9130 return PAGER_LOCKINGMODE_QUERY;
9131 }
9132
9133 #ifndef SQLITE_OMIT_AUTOVACUUM
9134 /*
9135 ** Interpret the given string as an auto-vacuum mode value.
9136 **
9137 ** The following strings, "none", "full" and "incremental" are
9138 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
9139 */
9140 static int getAutoVacuum(const char *z){
9141 int i;
9142 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
9143 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
9144 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
9145 i = sqlite3Atoi(z);
9146 return (u8)((i>=0&&i<=2)?i:0);
9147 }
9148 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
9149
9150 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
9151 /*
9152 ** Interpret the given string as a temp db location. Return 1 for file
9153 ** backed temporary databases, 2 for the Red-Black tree in memory database
9154 ** and 0 to use the compile-time default.
9155 */
9156 static int getTempStore(const char *z){
9157 if( z[0]>='0' && z[0]<='2' ){
9158 return z[0] - '0';
9159 }else if( sqlite3StrICmp(z, "file")==0 ){
9160 return 1;
9161 }else if( sqlite3StrICmp(z, "memory")==0 ){
9162 return 2;
9163 }else{
9164 return 0;
9165 }
9166 }
9167 #endif /* SQLITE_PAGER_PRAGMAS */
9168
9169 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
9170 /*
9171 ** Invalidate temp storage, either when the temp storage is changed
9172 ** from default, or when 'file' and the temp_store_directory has changed
9173 */
9174 static int invalidateTempStorage(Parse *pParse){
9175 sqlite3 *db = pParse->db;
9176 if( db->aDb[1].pBt!=0 ){
9177 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
9178 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
9179 "from within a transaction");
9180 return SQLITE_ERROR;
9181 }
9182 sqlite3BtreeClose(db->aDb[1].pBt);
9183 db->aDb[1].pBt = 0;
9184 sqlite3ResetAllSchemasOfConnection(db);
9185 }
9186 return SQLITE_OK;
9187 }
9188 #endif /* SQLITE_PAGER_PRAGMAS */
9189
9190 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
9191 /*
9192 ** If the TEMP database is open, close it and mark the database schema
9193 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
9194 ** or DEFAULT_TEMP_STORE pragmas.
9195 */
9196 static int changeTempStorage(Parse *pParse, const char *zStorageType){
9197 int ts = getTempStore(zStorageType);
9198 sqlite3 *db = pParse->db;
9199 if( db->temp_store==ts ) return SQLITE_OK;
9200 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
9201 return SQLITE_ERROR;
9202 }
9203 db->temp_store = (u8)ts;
9204 return SQLITE_OK;
9205 }
9206 #endif /* SQLITE_PAGER_PRAGMAS */
9207
9208 /*
9209 ** Set result column names for a pragma.
9210 */
9211 static void setPragmaResultColumnNames(
9212 Vdbe *v, /* The query under construction */
9213 const PragmaName *pPragma /* The pragma */
9214 ){
9215 u8 n = pPragma->nPragCName;
9216 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
9217 if( n==0 ){
9218 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
9219 }else{
9220 int i, j;
9221 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
9222 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
9223 }
9224 }
9225 }
9226
9227 /*
9228 ** Generate code to return a single integer value.
9229 */
9230 static void returnSingleInt(Vdbe *v, i64 value){
9231 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
9232 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
9233 }
9234
9235 /*
9236 ** Generate code to return a single text value.
9237 */
9238 static void returnSingleText(
9239 Vdbe *v, /* Prepared statement under construction */
9240 const char *zValue /* Value to be returned */
9241 ){
9242 if( zValue ){
9243 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
9244 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
9245 }
9246 }
9247
9248
9249 /*
9250 ** Set the safety_level and pager flags for pager iDb. Or if iDb<0
9251 ** set these values for all pagers.
9252 */
9253 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
9254 static void setAllPagerFlags(sqlite3 *db){
9255 if( db->autoCommit ){
9256 Db *pDb = db->aDb;
9257 int n = db->nDb;
9258 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
9259 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
9260 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
9261 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
9262 == PAGER_FLAGS_MASK );
9263 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
9264 while( (n--) > 0 ){
9265 if( pDb->pBt ){
9266 sqlite3BtreeSetPagerFlags(pDb->pBt,
9267 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
9268 }
9269 pDb++;
9270 }
9271 }
9272 }
9273 #else
9274 # define setAllPagerFlags(X) /* no-op */
9275 #endif
9276
9277
9278 /*
9279 ** Return a human-readable name for a constraint resolution action.
9280 */
9281 #ifndef SQLITE_OMIT_FOREIGN_KEY
9282 static const char *actionName(u8 action){
9283 const char *zName;
9284 switch( action ){
9285 case OE_SetNull: zName = "SET NULL"; break;
9286 case OE_SetDflt: zName = "SET DEFAULT"; break;
9287 case OE_Cascade: zName = "CASCADE"; break;
9288 case OE_Restrict: zName = "RESTRICT"; break;
9289 default: zName = "NO ACTION";
9290 assert( action==OE_None ); break;
9291 }
9292 return zName;
9293 }
9294 #endif
9295
9296
9297 /*
9298 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
9299 ** defined in pager.h. This function returns the associated lowercase
9300 ** journal-mode name.
9301 */
9302 SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
9303 static char * const azModeName[] = {
9304 "delete", "persist", "off", "truncate", "memory"
9305 #ifndef SQLITE_OMIT_WAL
9306 , "wal"
9307 #endif
9308 };
9309 assert( PAGER_JOURNALMODE_DELETE==0 );
9310 assert( PAGER_JOURNALMODE_PERSIST==1 );
9311 assert( PAGER_JOURNALMODE_OFF==2 );
9312 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
9313 assert( PAGER_JOURNALMODE_MEMORY==4 );
9314 assert( PAGER_JOURNALMODE_WAL==5 );
9315 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
9316
9317 if( eMode==ArraySize(azModeName) ) return 0;
9318 return azModeName[eMode];
9319 }
9320
9321 /*
9322 ** Locate a pragma in the aPragmaName[] array.
9323 */
9324 static const PragmaName *pragmaLocate(const char *zName){
9325 int upr, lwr, mid = 0, rc;
9326 lwr = 0;
9327 upr = ArraySize(aPragmaName)-1;
9328 while( lwr<=upr ){
9329 mid = (lwr+upr)/2;
9330 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
9331 if( rc==0 ) break;
9332 if( rc<0 ){
9333 upr = mid - 1;
9334 }else{
9335 lwr = mid + 1;
9336 }
9337 }
9338 return lwr>upr ? 0 : &aPragmaName[mid];
9339 }
9340
9341 /*
9342 ** Process a pragma statement.
9343 **
9344 ** Pragmas are of this form:
9345 **
9346 ** PRAGMA [schema.]id [= value]
9347 **
9348 ** The identifier might also be a string. The value is a string, and
9349 ** identifier, or a number. If minusFlag is true, then the value is
9350 ** a number that was preceded by a minus sign.
9351 **
9352 ** If the left side is "database.id" then pId1 is the database name
9353 ** and pId2 is the id. If the left side is just "id" then pId1 is the
9354 ** id and pId2 is any empty string.
9355 */
9356 SQLITE_PRIVATE void sqlite3Pragma(
9357 Parse *pParse,
9358 Token *pId1, /* First part of [schema.]id field */
9359 Token *pId2, /* Second part of [schema.]id field, or NULL */
9360 Token *pValue, /* Token for <value>, or NULL */
9361 int minusFlag /* True if a '-' sign preceded <value> */
9362 ){
9363 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
9364 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
9365 const char *zDb = 0; /* The database name */
9366 Token *pId; /* Pointer to <id> token */
9367 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
9368 int iDb; /* Database index for <database> */
9369 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
9370 sqlite3 *db = pParse->db; /* The database connection */
9371 Db *pDb; /* The specific database being pragmaed */
9372 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
9373 const PragmaName *pPragma; /* The pragma */
9374
9375 if( v==0 ) return;
9376 sqlite3VdbeRunOnlyOnce(v);
9377 pParse->nMem = 2;
9378
9379 /* Interpret the [schema.] part of the pragma statement. iDb is the
9380 ** index of the database this pragma is being applied to in db.aDb[]. */
9381 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
9382 if( iDb<0 ) return;
9383 pDb = &db->aDb[iDb];
9384
9385 /* If the temp database has been explicitly named as part of the
9386 ** pragma, make sure it is open.
9387 */
9388 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
9389 return;
9390 }
9391
9392 zLeft = sqlite3NameFromToken(db, pId);
9393 if( !zLeft ) return;
9394 if( minusFlag ){
9395 zRight = sqlite3MPrintf(db, "-%T", pValue);
9396 }else{
9397 zRight = sqlite3NameFromToken(db, pValue);
9398 }
9399
9400 assert( pId2 );
9401 zDb = pId2->n>0 ? pDb->zDbSName : 0;
9402 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
9403 goto pragma_out;
9404 }
9405
9406 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
9407 ** connection. If it returns SQLITE_OK, then assume that the VFS
9408 ** handled the pragma and generate a no-op prepared statement.
9409 **
9410 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
9411 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
9412 ** object corresponding to the database file to which the pragma
9413 ** statement refers.
9414 **
9415 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
9416 ** file control is an array of pointers to strings (char**) in which the
9417 ** second element of the array is the name of the pragma and the third
9418 ** element is the argument to the pragma or NULL if the pragma has no
9419 ** argument.
9420 */
9421 aFcntl[0] = 0;
9422 aFcntl[1] = zLeft;
9423 aFcntl[2] = zRight;
9424 aFcntl[3] = 0;
9425 db->busyHandler.nBusy = 0;
9426 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
9427 if( rc==SQLITE_OK ){
9428 sqlite3VdbeSetNumCols(v, 1);
9429 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
9430 returnSingleText(v, aFcntl[0]);
9431 sqlite3_free(aFcntl[0]);
9432 goto pragma_out;
9433 }
9434 if( rc!=SQLITE_NOTFOUND ){
9435 if( aFcntl[0] ){
9436 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
9437 sqlite3_free(aFcntl[0]);
9438 }
9439 pParse->nErr++;
9440 pParse->rc = rc;
9441 goto pragma_out;
9442 }
9443
9444 /* Locate the pragma in the lookup table */
9445 pPragma = pragmaLocate(zLeft);
9446 if( pPragma==0 ) goto pragma_out;
9447
9448 /* Make sure the database schema is loaded if the pragma requires that */
9449 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
9450 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
9451 }
9452
9453 /* Register the result column names for pragmas that return results */
9454 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
9455 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
9456 ){
9457 setPragmaResultColumnNames(v, pPragma);
9458 }
9459
9460 /* Jump to the appropriate pragma handler */
9461 switch( pPragma->ePragTyp ){
9462
9463 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
9464 /*
9465 ** PRAGMA [schema.]default_cache_size
9466 ** PRAGMA [schema.]default_cache_size=N
9467 **
9468 ** The first form reports the current persistent setting for the
9469 ** page cache size. The value returned is the maximum number of
9470 ** pages in the page cache. The second form sets both the current
9471 ** page cache size value and the persistent page cache size value
9472 ** stored in the database file.
9473 **
9474 ** Older versions of SQLite would set the default cache size to a
9475 ** negative number to indicate synchronous=OFF. These days, synchronous
9476 ** is always on by default regardless of the sign of the default cache
9477 ** size. But continue to take the absolute value of the default cache
9478 ** size of historical compatibility.
9479 */
9480 case PragTyp_DEFAULT_CACHE_SIZE: {
9481 static const int iLn = VDBE_OFFSET_LINENO(2);
9482 static const VdbeOpList getCacheSize[] = {
9483 { OP_Transaction, 0, 0, 0}, /* 0 */
9484 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
9485 { OP_IfPos, 1, 8, 0},
9486 { OP_Integer, 0, 2, 0},
9487 { OP_Subtract, 1, 2, 1},
9488 { OP_IfPos, 1, 8, 0},
9489 { OP_Integer, 0, 1, 0}, /* 6 */
9490 { OP_Noop, 0, 0, 0},
9491 { OP_ResultRow, 1, 1, 0},
9492 };
9493 VdbeOp *aOp;
9494 sqlite3VdbeUsesBtree(v, iDb);
9495 if( !zRight ){
9496 pParse->nMem += 2;
9497 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
9498 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
9499 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
9500 aOp[0].p1 = iDb;
9501 aOp[1].p1 = iDb;
9502 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
9503 }else{
9504 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
9505 sqlite3BeginWriteOperation(pParse, 0, iDb);
9506 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
9507 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9508 pDb->pSchema->cache_size = size;
9509 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
9510 }
9511 break;
9512 }
9513 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
9514
9515 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
9516 /*
9517 ** PRAGMA [schema.]page_size
9518 ** PRAGMA [schema.]page_size=N
9519 **
9520 ** The first form reports the current setting for the
9521 ** database page size in bytes. The second form sets the
9522 ** database page size value. The value can only be set if
9523 ** the database has not yet been created.
9524 */
9525 case PragTyp_PAGE_SIZE: {
9526 Btree *pBt = pDb->pBt;
9527 assert( pBt!=0 );
9528 if( !zRight ){
9529 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
9530 returnSingleInt(v, size);
9531 }else{
9532 /* Malloc may fail when setting the page-size, as there is an internal
9533 ** buffer that the pager module resizes using sqlite3_realloc().
9534 */
9535 db->nextPagesize = sqlite3Atoi(zRight);
9536 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
9537 sqlite3OomFault(db);
9538 }
9539 }
9540 break;
9541 }
9542
9543 /*
9544 ** PRAGMA [schema.]secure_delete
9545 ** PRAGMA [schema.]secure_delete=ON/OFF
9546 **
9547 ** The first form reports the current setting for the
9548 ** secure_delete flag. The second form changes the secure_delete
9549 ** flag setting and reports thenew value.
9550 */
9551 case PragTyp_SECURE_DELETE: {
9552 Btree *pBt = pDb->pBt;
9553 int b = -1;
9554 assert( pBt!=0 );
9555 if( zRight ){
9556 b = sqlite3GetBoolean(zRight, 0);
9557 }
9558 if( pId2->n==0 && b>=0 ){
9559 int ii;
9560 for(ii=0; ii<db->nDb; ii++){
9561 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
9562 }
9563 }
9564 b = sqlite3BtreeSecureDelete(pBt, b);
9565 returnSingleInt(v, b);
9566 break;
9567 }
9568
9569 /*
9570 ** PRAGMA [schema.]max_page_count
9571 ** PRAGMA [schema.]max_page_count=N
9572 **
9573 ** The first form reports the current setting for the
9574 ** maximum number of pages in the database file. The
9575 ** second form attempts to change this setting. Both
9576 ** forms return the current setting.
9577 **
9578 ** The absolute value of N is used. This is undocumented and might
9579 ** change. The only purpose is to provide an easy way to test
9580 ** the sqlite3AbsInt32() function.
9581 **
9582 ** PRAGMA [schema.]page_count
9583 **
9584 ** Return the number of pages in the specified database.
9585 */
9586 case PragTyp_PAGE_COUNT: {
9587 int iReg;
9588 sqlite3CodeVerifySchema(pParse, iDb);
9589 iReg = ++pParse->nMem;
9590 if( sqlite3Tolower(zLeft[0])=='p' ){
9591 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
9592 }else{
9593 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
9594 sqlite3AbsInt32(sqlite3Atoi(zRight)));
9595 }
9596 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
9597 break;
9598 }
9599
9600 /*
9601 ** PRAGMA [schema.]locking_mode
9602 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
9603 */
9604 case PragTyp_LOCKING_MODE: {
9605 const char *zRet = "normal";
9606 int eMode = getLockingMode(zRight);
9607
9608 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
9609 /* Simple "PRAGMA locking_mode;" statement. This is a query for
9610 ** the current default locking mode (which may be different to
9611 ** the locking-mode of the main database).
9612 */
9613 eMode = db->dfltLockMode;
9614 }else{
9615 Pager *pPager;
9616 if( pId2->n==0 ){
9617 /* This indicates that no database name was specified as part
9618 ** of the PRAGMA command. In this case the locking-mode must be
9619 ** set on all attached databases, as well as the main db file.
9620 **
9621 ** Also, the sqlite3.dfltLockMode variable is set so that
9622 ** any subsequently attached databases also use the specified
9623 ** locking mode.
9624 */
9625 int ii;
9626 assert(pDb==&db->aDb[0]);
9627 for(ii=2; ii<db->nDb; ii++){
9628 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
9629 sqlite3PagerLockingMode(pPager, eMode);
9630 }
9631 db->dfltLockMode = (u8)eMode;
9632 }
9633 pPager = sqlite3BtreePager(pDb->pBt);
9634 eMode = sqlite3PagerLockingMode(pPager, eMode);
9635 }
9636
9637 assert( eMode==PAGER_LOCKINGMODE_NORMAL
9638 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
9639 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
9640 zRet = "exclusive";
9641 }
9642 returnSingleText(v, zRet);
9643 break;
9644 }
9645
9646 /*
9647 ** PRAGMA [schema.]journal_mode
9648 ** PRAGMA [schema.]journal_mode =
9649 ** (delete|persist|off|truncate|memory|wal|off)
9650 */
9651 case PragTyp_JOURNAL_MODE: {
9652 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
9653 int ii; /* Loop counter */
9654
9655 if( zRight==0 ){
9656 /* If there is no "=MODE" part of the pragma, do a query for the
9657 ** current mode */
9658 eMode = PAGER_JOURNALMODE_QUERY;
9659 }else{
9660 const char *zMode;
9661 int n = sqlite3Strlen30(zRight);
9662 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
9663 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
9664 }
9665 if( !zMode ){
9666 /* If the "=MODE" part does not match any known journal mode,
9667 ** then do a query */
9668 eMode = PAGER_JOURNALMODE_QUERY;
9669 }
9670 }
9671 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
9672 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
9673 iDb = 0;
9674 pId2->n = 1;
9675 }
9676 for(ii=db->nDb-1; ii>=0; ii--){
9677 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
9678 sqlite3VdbeUsesBtree(v, ii);
9679 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
9680 }
9681 }
9682 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
9683 break;
9684 }
9685
9686 /*
9687 ** PRAGMA [schema.]journal_size_limit
9688 ** PRAGMA [schema.]journal_size_limit=N
9689 **
9690 ** Get or set the size limit on rollback journal files.
9691 */
9692 case PragTyp_JOURNAL_SIZE_LIMIT: {
9693 Pager *pPager = sqlite3BtreePager(pDb->pBt);
9694 i64 iLimit = -2;
9695 if( zRight ){
9696 sqlite3DecOrHexToI64(zRight, &iLimit);
9697 if( iLimit<-1 ) iLimit = -1;
9698 }
9699 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
9700 returnSingleInt(v, iLimit);
9701 break;
9702 }
9703
9704 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
9705
9706 /*
9707 ** PRAGMA [schema.]auto_vacuum
9708 ** PRAGMA [schema.]auto_vacuum=N
9709 **
9710 ** Get or set the value of the database 'auto-vacuum' parameter.
9711 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
9712 */
9713 #ifndef SQLITE_OMIT_AUTOVACUUM
9714 case PragTyp_AUTO_VACUUM: {
9715 Btree *pBt = pDb->pBt;
9716 assert( pBt!=0 );
9717 if( !zRight ){
9718 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
9719 }else{
9720 int eAuto = getAutoVacuum(zRight);
9721 assert( eAuto>=0 && eAuto<=2 );
9722 db->nextAutovac = (u8)eAuto;
9723 /* Call SetAutoVacuum() to set initialize the internal auto and
9724 ** incr-vacuum flags. This is required in case this connection
9725 ** creates the database file. It is important that it is created
9726 ** as an auto-vacuum capable db.
9727 */
9728 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
9729 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
9730 /* When setting the auto_vacuum mode to either "full" or
9731 ** "incremental", write the value of meta[6] in the database
9732 ** file. Before writing to meta[6], check that meta[3] indicates
9733 ** that this really is an auto-vacuum capable database.
9734 */
9735 static const int iLn = VDBE_OFFSET_LINENO(2);
9736 static const VdbeOpList setMeta6[] = {
9737 { OP_Transaction, 0, 1, 0}, /* 0 */
9738 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
9739 { OP_If, 1, 0, 0}, /* 2 */
9740 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
9741 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
9742 };
9743 VdbeOp *aOp;
9744 int iAddr = sqlite3VdbeCurrentAddr(v);
9745 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
9746 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
9747 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
9748 aOp[0].p1 = iDb;
9749 aOp[1].p1 = iDb;
9750 aOp[2].p2 = iAddr+4;
9751 aOp[4].p1 = iDb;
9752 aOp[4].p3 = eAuto - 1;
9753 sqlite3VdbeUsesBtree(v, iDb);
9754 }
9755 }
9756 break;
9757 }
9758 #endif
9759
9760 /*
9761 ** PRAGMA [schema.]incremental_vacuum(N)
9762 **
9763 ** Do N steps of incremental vacuuming on a database.
9764 */
9765 #ifndef SQLITE_OMIT_AUTOVACUUM
9766 case PragTyp_INCREMENTAL_VACUUM: {
9767 int iLimit, addr;
9768 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
9769 iLimit = 0x7fffffff;
9770 }
9771 sqlite3BeginWriteOperation(pParse, 0, iDb);
9772 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
9773 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
9774 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
9775 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
9776 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
9777 sqlite3VdbeJumpHere(v, addr);
9778 break;
9779 }
9780 #endif
9781
9782 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
9783 /*
9784 ** PRAGMA [schema.]cache_size
9785 ** PRAGMA [schema.]cache_size=N
9786 **
9787 ** The first form reports the current local setting for the
9788 ** page cache size. The second form sets the local
9789 ** page cache size value. If N is positive then that is the
9790 ** number of pages in the cache. If N is negative, then the
9791 ** number of pages is adjusted so that the cache uses -N kibibytes
9792 ** of memory.
9793 */
9794 case PragTyp_CACHE_SIZE: {
9795 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9796 if( !zRight ){
9797 returnSingleInt(v, pDb->pSchema->cache_size);
9798 }else{
9799 int size = sqlite3Atoi(zRight);
9800 pDb->pSchema->cache_size = size;
9801 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
9802 }
9803 break;
9804 }
9805
9806 /*
9807 ** PRAGMA [schema.]cache_spill
9808 ** PRAGMA cache_spill=BOOLEAN
9809 ** PRAGMA [schema.]cache_spill=N
9810 **
9811 ** The first form reports the current local setting for the
9812 ** page cache spill size. The second form turns cache spill on
9813 ** or off. When turnning cache spill on, the size is set to the
9814 ** current cache_size. The third form sets a spill size that
9815 ** may be different form the cache size.
9816 ** If N is positive then that is the
9817 ** number of pages in the cache. If N is negative, then the
9818 ** number of pages is adjusted so that the cache uses -N kibibytes
9819 ** of memory.
9820 **
9821 ** If the number of cache_spill pages is less then the number of
9822 ** cache_size pages, no spilling occurs until the page count exceeds
9823 ** the number of cache_size pages.
9824 **
9825 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
9826 ** not just the schema specified.
9827 */
9828 case PragTyp_CACHE_SPILL: {
9829 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9830 if( !zRight ){
9831 returnSingleInt(v,
9832 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
9833 sqlite3BtreeSetSpillSize(pDb->pBt,0));
9834 }else{
9835 int size = 1;
9836 if( sqlite3GetInt32(zRight, &size) ){
9837 sqlite3BtreeSetSpillSize(pDb->pBt, size);
9838 }
9839 if( sqlite3GetBoolean(zRight, size!=0) ){
9840 db->flags |= SQLITE_CacheSpill;
9841 }else{
9842 db->flags &= ~SQLITE_CacheSpill;
9843 }
9844 setAllPagerFlags(db);
9845 }
9846 break;
9847 }
9848
9849 /*
9850 ** PRAGMA [schema.]mmap_size(N)
9851 **
9852 ** Used to set mapping size limit. The mapping size limit is
9853 ** used to limit the aggregate size of all memory mapped regions of the
9854 ** database file. If this parameter is set to zero, then memory mapping
9855 ** is not used at all. If N is negative, then the default memory map
9856 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
9857 ** The parameter N is measured in bytes.
9858 **
9859 ** This value is advisory. The underlying VFS is free to memory map
9860 ** as little or as much as it wants. Except, if N is set to 0 then the
9861 ** upper layers will never invoke the xFetch interfaces to the VFS.
9862 */
9863 case PragTyp_MMAP_SIZE: {
9864 sqlite3_int64 sz;
9865 #if SQLITE_MAX_MMAP_SIZE>0
9866 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9867 if( zRight ){
9868 int ii;
9869 sqlite3DecOrHexToI64(zRight, &sz);
9870 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
9871 if( pId2->n==0 ) db->szMmap = sz;
9872 for(ii=db->nDb-1; ii>=0; ii--){
9873 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
9874 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
9875 }
9876 }
9877 }
9878 sz = -1;
9879 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
9880 #else
9881 sz = 0;
9882 rc = SQLITE_OK;
9883 #endif
9884 if( rc==SQLITE_OK ){
9885 returnSingleInt(v, sz);
9886 }else if( rc!=SQLITE_NOTFOUND ){
9887 pParse->nErr++;
9888 pParse->rc = rc;
9889 }
9890 break;
9891 }
9892
9893 /*
9894 ** PRAGMA temp_store
9895 ** PRAGMA temp_store = "default"|"memory"|"file"
9896 **
9897 ** Return or set the local value of the temp_store flag. Changing
9898 ** the local value does not make changes to the disk file and the default
9899 ** value will be restored the next time the database is opened.
9900 **
9901 ** Note that it is possible for the library compile-time options to
9902 ** override this setting
9903 */
9904 case PragTyp_TEMP_STORE: {
9905 if( !zRight ){
9906 returnSingleInt(v, db->temp_store);
9907 }else{
9908 changeTempStorage(pParse, zRight);
9909 }
9910 break;
9911 }
9912
9913 /*
9914 ** PRAGMA temp_store_directory
9915 ** PRAGMA temp_store_directory = ""|"directory_name"
9916 **
9917 ** Return or set the local value of the temp_store_directory flag. Changing
9918 ** the value sets a specific directory to be used for temporary files.
9919 ** Setting to a null string reverts to the default temporary directory search.
9920 ** If temporary directory is changed, then invalidateTempStorage.
9921 **
9922 */
9923 case PragTyp_TEMP_STORE_DIRECTORY: {
9924 if( !zRight ){
9925 returnSingleText(v, sqlite3_temp_directory);
9926 }else{
9927 #ifndef SQLITE_OMIT_WSD
9928 if( zRight[0] ){
9929 int res;
9930 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
9931 if( rc!=SQLITE_OK || res==0 ){
9932 sqlite3ErrorMsg(pParse, "not a writable directory");
9933 goto pragma_out;
9934 }
9935 }
9936 if( SQLITE_TEMP_STORE==0
9937 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
9938 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
9939 ){
9940 invalidateTempStorage(pParse);
9941 }
9942 sqlite3_free(sqlite3_temp_directory);
9943 if( zRight[0] ){
9944 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
9945 }else{
9946 sqlite3_temp_directory = 0;
9947 }
9948 #endif /* SQLITE_OMIT_WSD */
9949 }
9950 break;
9951 }
9952
9953 #if SQLITE_OS_WIN
9954 /*
9955 ** PRAGMA data_store_directory
9956 ** PRAGMA data_store_directory = ""|"directory_name"
9957 **
9958 ** Return or set the local value of the data_store_directory flag. Changing
9959 ** the value sets a specific directory to be used for database files that
9960 ** were specified with a relative pathname. Setting to a null string reverts
9961 ** to the default database directory, which for database files specified with
9962 ** a relative path will probably be based on the current directory for the
9963 ** process. Database file specified with an absolute path are not impacted
9964 ** by this setting, regardless of its value.
9965 **
9966 */
9967 case PragTyp_DATA_STORE_DIRECTORY: {
9968 if( !zRight ){
9969 returnSingleText(v, sqlite3_data_directory);
9970 }else{
9971 #ifndef SQLITE_OMIT_WSD
9972 if( zRight[0] ){
9973 int res;
9974 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
9975 if( rc!=SQLITE_OK || res==0 ){
9976 sqlite3ErrorMsg(pParse, "not a writable directory");
9977 goto pragma_out;
9978 }
9979 }
9980 sqlite3_free(sqlite3_data_directory);
9981 if( zRight[0] ){
9982 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
9983 }else{
9984 sqlite3_data_directory = 0;
9985 }
9986 #endif /* SQLITE_OMIT_WSD */
9987 }
9988 break;
9989 }
9990 #endif
9991
9992 #if SQLITE_ENABLE_LOCKING_STYLE
9993 /*
9994 ** PRAGMA [schema.]lock_proxy_file
9995 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
9996 **
9997 ** Return or set the value of the lock_proxy_file flag. Changing
9998 ** the value sets a specific file to be used for database access locks.
9999 **
10000 */
10001 case PragTyp_LOCK_PROXY_FILE: {
10002 if( !zRight ){
10003 Pager *pPager = sqlite3BtreePager(pDb->pBt);
10004 char *proxy_file_path = NULL;
10005 sqlite3_file *pFile = sqlite3PagerFile(pPager);
10006 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
10007 &proxy_file_path);
10008 returnSingleText(v, proxy_file_path);
10009 }else{
10010 Pager *pPager = sqlite3BtreePager(pDb->pBt);
10011 sqlite3_file *pFile = sqlite3PagerFile(pPager);
10012 int res;
10013 if( zRight[0] ){
10014 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
10015 zRight);
10016 } else {
10017 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
10018 NULL);
10019 }
10020 if( res!=SQLITE_OK ){
10021 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
10022 goto pragma_out;
10023 }
10024 }
10025 break;
10026 }
10027 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
10028
10029 /*
10030 ** PRAGMA [schema.]synchronous
10031 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
10032 **
10033 ** Return or set the local value of the synchronous flag. Changing
10034 ** the local value does not make changes to the disk file and the
10035 ** default value will be restored the next time the database is
10036 ** opened.
10037 */
10038 case PragTyp_SYNCHRONOUS: {
10039 if( !zRight ){
10040 returnSingleInt(v, pDb->safety_level-1);
10041 }else{
10042 if( !db->autoCommit ){
10043 sqlite3ErrorMsg(pParse,
10044 "Safety level may not be changed inside a transaction");
10045 }else{
10046 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
10047 if( iLevel==0 ) iLevel = 1;
10048 pDb->safety_level = iLevel;
10049 pDb->bSyncSet = 1;
10050 setAllPagerFlags(db);
10051 }
10052 }
10053 break;
10054 }
10055 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
10056
10057 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
10058 case PragTyp_FLAG: {
10059 if( zRight==0 ){
10060 setPragmaResultColumnNames(v, pPragma);
10061 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
10062 }else{
10063 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
10064 if( db->autoCommit==0 ){
10065 /* Foreign key support may not be enabled or disabled while not
10066 ** in auto-commit mode. */
10067 mask &= ~(SQLITE_ForeignKeys);
10068 }
10069 #if SQLITE_USER_AUTHENTICATION
10070 if( db->auth.authLevel==UAUTH_User ){
10071 /* Do not allow non-admin users to modify the schema arbitrarily */
10072 mask &= ~(SQLITE_WriteSchema);
10073 }
10074 #endif
10075
10076 if( sqlite3GetBoolean(zRight, 0) ){
10077 db->flags |= mask;
10078 }else{
10079 db->flags &= ~mask;
10080 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
10081 }
10082
10083 /* Many of the flag-pragmas modify the code generated by the SQL
10084 ** compiler (eg. count_changes). So add an opcode to expire all
10085 ** compiled SQL statements after modifying a pragma value.
10086 */
10087 sqlite3VdbeAddOp0(v, OP_Expire);
10088 setAllPagerFlags(db);
10089 }
10090 break;
10091 }
10092 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
10093
10094 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
10095 /*
10096 ** PRAGMA table_info(<table>)
10097 **
10098 ** Return a single row for each column of the named table. The columns of
10099 ** the returned data set are:
10100 **
10101 ** cid: Column id (numbered from left to right, starting at 0)
10102 ** name: Column name
10103 ** type: Column declaration type.
10104 ** notnull: True if 'NOT NULL' is part of column declaration
10105 ** dflt_value: The default value for the column, if any.
10106 */
10107 case PragTyp_TABLE_INFO: if( zRight ){
10108 Table *pTab;
10109 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
10110 if( pTab ){
10111 int i, k;
10112 int nHidden = 0;
10113 Column *pCol;
10114 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
10115 pParse->nMem = 6;
10116 sqlite3CodeVerifySchema(pParse, iDb);
10117 sqlite3ViewGetColumnNames(pParse, pTab);
10118 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
10119 if( IsHiddenColumn(pCol) ){
10120 nHidden++;
10121 continue;
10122 }
10123 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
10124 k = 0;
10125 }else if( pPk==0 ){
10126 k = 1;
10127 }else{
10128 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
10129 }
10130 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
10131 sqlite3VdbeMultiLoad(v, 1, "issisi",
10132 i-nHidden,
10133 pCol->zName,
10134 sqlite3ColumnType(pCol,""),
10135 pCol->notNull ? 1 : 0,
10136 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
10137 k);
10138 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
10139 }
10140 }
10141 }
10142 break;
10143
10144 case PragTyp_STATS: {
10145 Index *pIdx;
10146 HashElem *i;
10147 pParse->nMem = 4;
10148 sqlite3CodeVerifySchema(pParse, iDb);
10149 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
10150 Table *pTab = sqliteHashData(i);
10151 sqlite3VdbeMultiLoad(v, 1, "ssii",
10152 pTab->zName,
10153 0,
10154 pTab->szTabRow,
10155 pTab->nRowLogEst);
10156 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
10157 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
10158 sqlite3VdbeMultiLoad(v, 2, "sii",
10159 pIdx->zName,
10160 pIdx->szIdxRow,
10161 pIdx->aiRowLogEst[0]);
10162 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
10163 }
10164 }
10165 }
10166 break;
10167
10168 case PragTyp_INDEX_INFO: if( zRight ){
10169 Index *pIdx;
10170 Table *pTab;
10171 pIdx = sqlite3FindIndex(db, zRight, zDb);
10172 if( pIdx ){
10173 int i;
10174 int mx;
10175 if( pPragma->iArg ){
10176 /* PRAGMA index_xinfo (newer version with more rows and columns) */
10177 mx = pIdx->nColumn;
10178 pParse->nMem = 6;
10179 }else{
10180 /* PRAGMA index_info (legacy version) */
10181 mx = pIdx->nKeyCol;
10182 pParse->nMem = 3;
10183 }
10184 pTab = pIdx->pTable;
10185 sqlite3CodeVerifySchema(pParse, iDb);
10186 assert( pParse->nMem<=pPragma->nPragCName );
10187 for(i=0; i<mx; i++){
10188 i16 cnum = pIdx->aiColumn[i];
10189 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum,
10190 cnum<0 ? 0 : pTab->aCol[cnum].zName);
10191 if( pPragma->iArg ){
10192 sqlite3VdbeMultiLoad(v, 4, "isi",
10193 pIdx->aSortOrder[i],
10194 pIdx->azColl[i],
10195 i<pIdx->nKeyCol);
10196 }
10197 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
10198 }
10199 }
10200 }
10201 break;
10202
10203 case PragTyp_INDEX_LIST: if( zRight ){
10204 Index *pIdx;
10205 Table *pTab;
10206 int i;
10207 pTab = sqlite3FindTable(db, zRight, zDb);
10208 if( pTab ){
10209 pParse->nMem = 5;
10210 sqlite3CodeVerifySchema(pParse, iDb);
10211 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
10212 const char *azOrigin[] = { "c", "u", "pk" };
10213 sqlite3VdbeMultiLoad(v, 1, "isisi",
10214 i,
10215 pIdx->zName,
10216 IsUniqueIndex(pIdx),
10217 azOrigin[pIdx->idxType],
10218 pIdx->pPartIdxWhere!=0);
10219 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
10220 }
10221 }
10222 }
10223 break;
10224
10225 case PragTyp_DATABASE_LIST: {
10226 int i;
10227 pParse->nMem = 3;
10228 for(i=0; i<db->nDb; i++){
10229 if( db->aDb[i].pBt==0 ) continue;
10230 assert( db->aDb[i].zDbSName!=0 );
10231 sqlite3VdbeMultiLoad(v, 1, "iss",
10232 i,
10233 db->aDb[i].zDbSName,
10234 sqlite3BtreeGetFilename(db->aDb[i].pBt));
10235 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
10236 }
10237 }
10238 break;
10239
10240 case PragTyp_COLLATION_LIST: {
10241 int i = 0;
10242 HashElem *p;
10243 pParse->nMem = 2;
10244 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
10245 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
10246 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
10247 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
10248 }
10249 }
10250 break;
10251 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
10252
10253 #ifndef SQLITE_OMIT_FOREIGN_KEY
10254 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
10255 FKey *pFK;
10256 Table *pTab;
10257 pTab = sqlite3FindTable(db, zRight, zDb);
10258 if( pTab ){
10259 pFK = pTab->pFKey;
10260 if( pFK ){
10261 int i = 0;
10262 pParse->nMem = 8;
10263 sqlite3CodeVerifySchema(pParse, iDb);
10264 while(pFK){
10265 int j;
10266 for(j=0; j<pFK->nCol; j++){
10267 sqlite3VdbeMultiLoad(v, 1, "iissssss",
10268 i,
10269 j,
10270 pFK->zTo,
10271 pTab->aCol[pFK->aCol[j].iFrom].zName,
10272 pFK->aCol[j].zCol,
10273 actionName(pFK->aAction[1]), /* ON UPDATE */
10274 actionName(pFK->aAction[0]), /* ON DELETE */
10275 "NONE");
10276 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
10277 }
10278 ++i;
10279 pFK = pFK->pNextFrom;
10280 }
10281 }
10282 }
10283 }
10284 break;
10285 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
10286
10287 #ifndef SQLITE_OMIT_FOREIGN_KEY
10288 #ifndef SQLITE_OMIT_TRIGGER
10289 case PragTyp_FOREIGN_KEY_CHECK: {
10290 FKey *pFK; /* A foreign key constraint */
10291 Table *pTab; /* Child table contain "REFERENCES" keyword */
10292 Table *pParent; /* Parent table that child points to */
10293 Index *pIdx; /* Index in the parent table */
10294 int i; /* Loop counter: Foreign key number for pTab */
10295 int j; /* Loop counter: Field of the foreign key */
10296 HashElem *k; /* Loop counter: Next table in schema */
10297 int x; /* result variable */
10298 int regResult; /* 3 registers to hold a result row */
10299 int regKey; /* Register to hold key for checking the FK */
10300 int regRow; /* Registers to hold a row from pTab */
10301 int addrTop; /* Top of a loop checking foreign keys */
10302 int addrOk; /* Jump here if the key is OK */
10303 int *aiCols; /* child to parent column mapping */
10304
10305 regResult = pParse->nMem+1;
10306 pParse->nMem += 4;
10307 regKey = ++pParse->nMem;
10308 regRow = ++pParse->nMem;
10309 sqlite3CodeVerifySchema(pParse, iDb);
10310 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
10311 while( k ){
10312 if( zRight ){
10313 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
10314 k = 0;
10315 }else{
10316 pTab = (Table*)sqliteHashData(k);
10317 k = sqliteHashNext(k);
10318 }
10319 if( pTab==0 || pTab->pFKey==0 ) continue;
10320 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
10321 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
10322 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
10323 sqlite3VdbeLoadString(v, regResult, pTab->zName);
10324 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
10325 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
10326 if( pParent==0 ) continue;
10327 pIdx = 0;
10328 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
10329 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
10330 if( x==0 ){
10331 if( pIdx==0 ){
10332 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
10333 }else{
10334 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
10335 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
10336 }
10337 }else{
10338 k = 0;
10339 break;
10340 }
10341 }
10342 assert( pParse->nErr>0 || pFK==0 );
10343 if( pFK ) break;
10344 if( pParse->nTab<i ) pParse->nTab = i;
10345 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
10346 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
10347 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
10348 pIdx = 0;
10349 aiCols = 0;
10350 if( pParent ){
10351 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
10352 assert( x==0 );
10353 }
10354 addrOk = sqlite3VdbeMakeLabel(v);
10355 if( pParent && pIdx==0 ){
10356 int iKey = pFK->aCol[0].iFrom;
10357 assert( iKey>=0 && iKey<pTab->nCol );
10358 if( iKey!=pTab->iPKey ){
10359 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
10360 sqlite3ColumnDefault(v, pTab, iKey, regRow);
10361 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
10362 }else{
10363 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
10364 }
10365 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, 0, regRow); VdbeCoverage(v);
10366 sqlite3VdbeGoto(v, addrOk);
10367 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
10368 }else{
10369 for(j=0; j<pFK->nCol; j++){
10370 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
10371 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
10372 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
10373 }
10374 if( pParent ){
10375 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
10376 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
10377 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
10378 VdbeCoverage(v);
10379 }
10380 }
10381 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
10382 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1);
10383 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
10384 sqlite3VdbeResolveLabel(v, addrOk);
10385 sqlite3DbFree(db, aiCols);
10386 }
10387 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
10388 sqlite3VdbeJumpHere(v, addrTop);
10389 }
10390 }
10391 break;
10392 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
10393 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
10394
10395 #ifndef NDEBUG
10396 case PragTyp_PARSER_TRACE: {
10397 if( zRight ){
10398 if( sqlite3GetBoolean(zRight, 0) ){
10399 sqlite3ParserTrace(stdout, "parser: ");
10400 }else{
10401 sqlite3ParserTrace(0, 0);
10402 }
10403 }
10404 }
10405 break;
10406 #endif
10407
10408 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
10409 ** used will be case sensitive or not depending on the RHS.
10410 */
10411 case PragTyp_CASE_SENSITIVE_LIKE: {
10412 if( zRight ){
10413 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
10414 }
10415 }
10416 break;
10417
10418 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
10419 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
10420 #endif
10421
10422 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
10423 /* Pragma "quick_check" is reduced version of
10424 ** integrity_check designed to detect most database corruption
10425 ** without most of the overhead of a full integrity-check.
10426 */
10427 case PragTyp_INTEGRITY_CHECK: {
10428 int i, j, addr, mxErr;
10429
10430 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
10431
10432 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
10433 ** then iDb is set to the index of the database identified by <db>.
10434 ** In this case, the integrity of database iDb only is verified by
10435 ** the VDBE created below.
10436 **
10437 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
10438 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
10439 ** to -1 here, to indicate that the VDBE should verify the integrity
10440 ** of all attached databases. */
10441 assert( iDb>=0 );
10442 assert( iDb==0 || pId2->z );
10443 if( pId2->z==0 ) iDb = -1;
10444
10445 /* Initialize the VDBE program */
10446 pParse->nMem = 6;
10447
10448 /* Set the maximum error count */
10449 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
10450 if( zRight ){
10451 sqlite3GetInt32(zRight, &mxErr);
10452 if( mxErr<=0 ){
10453 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
10454 }
10455 }
10456 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
10457
10458 /* Do an integrity check on each database file */
10459 for(i=0; i<db->nDb; i++){
10460 HashElem *x;
10461 Hash *pTbls;
10462 int *aRoot;
10463 int cnt = 0;
10464 int mxIdx = 0;
10465 int nIdx;
10466
10467 if( OMIT_TEMPDB && i==1 ) continue;
10468 if( iDb>=0 && i!=iDb ) continue;
10469
10470 sqlite3CodeVerifySchema(pParse, i);
10471 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
10472 VdbeCoverage(v);
10473 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
10474 sqlite3VdbeJumpHere(v, addr);
10475
10476 /* Do an integrity check of the B-Tree
10477 **
10478 ** Begin by finding the root pages numbers
10479 ** for all tables and indices in the database.
10480 */
10481 assert( sqlite3SchemaMutexHeld(db, i, 0) );
10482 pTbls = &db->aDb[i].pSchema->tblHash;
10483 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
10484 Table *pTab = sqliteHashData(x);
10485 Index *pIdx;
10486 if( HasRowid(pTab) ) cnt++;
10487 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
10488 if( nIdx>mxIdx ) mxIdx = nIdx;
10489 }
10490 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
10491 if( aRoot==0 ) break;
10492 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
10493 Table *pTab = sqliteHashData(x);
10494 Index *pIdx;
10495 if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum;
10496 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
10497 aRoot[cnt++] = pIdx->tnum;
10498 }
10499 }
10500 aRoot[cnt] = 0;
10501
10502 /* Make sure sufficient number of registers have been allocated */
10503 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
10504
10505 /* Do the b-tree integrity checks */
10506 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
10507 sqlite3VdbeChangeP5(v, (u8)i);
10508 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
10509 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
10510 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
10511 P4_DYNAMIC);
10512 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
10513 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
10514 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
10515 sqlite3VdbeJumpHere(v, addr);
10516
10517 /* Make sure all the indices are constructed correctly.
10518 */
10519 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
10520 Table *pTab = sqliteHashData(x);
10521 Index *pIdx, *pPk;
10522 Index *pPrior = 0;
10523 int loopTop;
10524 int iDataCur, iIdxCur;
10525 int r1 = -1;
10526
10527 if( pTab->pIndex==0 ) continue;
10528 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
10529 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
10530 VdbeCoverage(v);
10531 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
10532 sqlite3VdbeJumpHere(v, addr);
10533 sqlite3ExprCacheClear(pParse);
10534 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
10535 1, 0, &iDataCur, &iIdxCur);
10536 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
10537 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
10538 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
10539 }
10540 assert( pParse->nMem>=8+j );
10541 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
10542 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
10543 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
10544 /* Verify that all NOT NULL columns really are NOT NULL */
10545 for(j=0; j<pTab->nCol; j++){
10546 char *zErr;
10547 int jmp2, jmp3;
10548 if( j==pTab->iPKey ) continue;
10549 if( pTab->aCol[j].notNull==0 ) continue;
10550 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
10551 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
10552 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
10553 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
10554 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
10555 pTab->aCol[j].zName);
10556 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
10557 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
10558 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
10559 sqlite3VdbeAddOp0(v, OP_Halt);
10560 sqlite3VdbeJumpHere(v, jmp2);
10561 sqlite3VdbeJumpHere(v, jmp3);
10562 }
10563 /* Validate index entries for the current row */
10564 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
10565 int jmp2, jmp3, jmp4, jmp5;
10566 int ckUniq = sqlite3VdbeMakeLabel(v);
10567 if( pPk==pIdx ) continue;
10568 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
10569 pPrior, r1);
10570 pPrior = pIdx;
10571 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
10572 /* Verify that an index entry exists for the current table row */
10573 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
10574 pIdx->nColumn); VdbeCoverage(v);
10575 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
10576 sqlite3VdbeLoadString(v, 3, "row ");
10577 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
10578 sqlite3VdbeLoadString(v, 4, " missing from index ");
10579 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
10580 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
10581 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
10582 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
10583 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
10584 sqlite3VdbeAddOp0(v, OP_Halt);
10585 sqlite3VdbeJumpHere(v, jmp2);
10586 /* For UNIQUE indexes, verify that only one entry exists with the
10587 ** current key. The entry is unique if (1) any column is NULL
10588 ** or (2) the next entry has a different key */
10589 if( IsUniqueIndex(pIdx) ){
10590 int uniqOk = sqlite3VdbeMakeLabel(v);
10591 int jmp6;
10592 int kk;
10593 for(kk=0; kk<pIdx->nKeyCol; kk++){
10594 int iCol = pIdx->aiColumn[kk];
10595 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
10596 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
10597 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
10598 VdbeCoverage(v);
10599 }
10600 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
10601 sqlite3VdbeGoto(v, uniqOk);
10602 sqlite3VdbeJumpHere(v, jmp6);
10603 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
10604 pIdx->nKeyCol); VdbeCoverage(v);
10605 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
10606 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
10607 sqlite3VdbeGoto(v, jmp5);
10608 sqlite3VdbeResolveLabel(v, uniqOk);
10609 }
10610 sqlite3VdbeJumpHere(v, jmp4);
10611 sqlite3ResolvePartIdxLabel(pParse, jmp3);
10612 }
10613 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
10614 sqlite3VdbeJumpHere(v, loopTop-1);
10615 #ifndef SQLITE_OMIT_BTREECOUNT
10616 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
10617 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
10618 if( pPk==pIdx ) continue;
10619 addr = sqlite3VdbeCurrentAddr(v);
10620 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
10621 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
10622 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
10623 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
10624 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
10625 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
10626 sqlite3VdbeLoadString(v, 3, pIdx->zName);
10627 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
10628 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
10629 }
10630 #endif /* SQLITE_OMIT_BTREECOUNT */
10631 }
10632 }
10633 {
10634 static const int iLn = VDBE_OFFSET_LINENO(2);
10635 static const VdbeOpList endCode[] = {
10636 { OP_AddImm, 1, 0, 0}, /* 0 */
10637 { OP_If, 1, 4, 0}, /* 1 */
10638 { OP_String8, 0, 3, 0}, /* 2 */
10639 { OP_ResultRow, 3, 1, 0}, /* 3 */
10640 };
10641 VdbeOp *aOp;
10642
10643 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
10644 if( aOp ){
10645 aOp[0].p2 = -mxErr;
10646 aOp[2].p4type = P4_STATIC;
10647 aOp[2].p4.z = "ok";
10648 }
10649 }
10650 }
10651 break;
10652 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
10653
10654 #ifndef SQLITE_OMIT_UTF16
10655 /*
10656 ** PRAGMA encoding
10657 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
10658 **
10659 ** In its first form, this pragma returns the encoding of the main
10660 ** database. If the database is not initialized, it is initialized now.
10661 **
10662 ** The second form of this pragma is a no-op if the main database file
10663 ** has not already been initialized. In this case it sets the default
10664 ** encoding that will be used for the main database file if a new file
10665 ** is created. If an existing main database file is opened, then the
10666 ** default text encoding for the existing database is used.
10667 **
10668 ** In all cases new databases created using the ATTACH command are
10669 ** created to use the same default text encoding as the main database. If
10670 ** the main database has not been initialized and/or created when ATTACH
10671 ** is executed, this is done before the ATTACH operation.
10672 **
10673 ** In the second form this pragma sets the text encoding to be used in
10674 ** new database files created using this database handle. It is only
10675 ** useful if invoked immediately after the main database i
10676 */
10677 case PragTyp_ENCODING: {
10678 static const struct EncName {
10679 char *zName;
10680 u8 enc;
10681 } encnames[] = {
10682 { "UTF8", SQLITE_UTF8 },
10683 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
10684 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
10685 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
10686 { "UTF16le", SQLITE_UTF16LE },
10687 { "UTF16be", SQLITE_UTF16BE },
10688 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
10689 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
10690 { 0, 0 }
10691 };
10692 const struct EncName *pEnc;
10693 if( !zRight ){ /* "PRAGMA encoding" */
10694 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
10695 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
10696 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
10697 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
10698 returnSingleText(v, encnames[ENC(pParse->db)].zName);
10699 }else{ /* "PRAGMA encoding = XXX" */
10700 /* Only change the value of sqlite.enc if the database handle is not
10701 ** initialized. If the main database exists, the new sqlite.enc value
10702 ** will be overwritten when the schema is next loaded. If it does not
10703 ** already exists, it will be created to use the new encoding value.
10704 */
10705 if(
10706 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
10707 DbHasProperty(db, 0, DB_Empty)
10708 ){
10709 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
10710 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
10711 SCHEMA_ENC(db) = ENC(db) =
10712 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
10713 break;
10714 }
10715 }
10716 if( !pEnc->zName ){
10717 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
10718 }
10719 }
10720 }
10721 }
10722 break;
10723 #endif /* SQLITE_OMIT_UTF16 */
10724
10725 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
10726 /*
10727 ** PRAGMA [schema.]schema_version
10728 ** PRAGMA [schema.]schema_version = <integer>
10729 **
10730 ** PRAGMA [schema.]user_version
10731 ** PRAGMA [schema.]user_version = <integer>
10732 **
10733 ** PRAGMA [schema.]freelist_count
10734 **
10735 ** PRAGMA [schema.]data_version
10736 **
10737 ** PRAGMA [schema.]application_id
10738 ** PRAGMA [schema.]application_id = <integer>
10739 **
10740 ** The pragma's schema_version and user_version are used to set or get
10741 ** the value of the schema-version and user-version, respectively. Both
10742 ** the schema-version and the user-version are 32-bit signed integers
10743 ** stored in the database header.
10744 **
10745 ** The schema-cookie is usually only manipulated internally by SQLite. It
10746 ** is incremented by SQLite whenever the database schema is modified (by
10747 ** creating or dropping a table or index). The schema version is used by
10748 ** SQLite each time a query is executed to ensure that the internal cache
10749 ** of the schema used when compiling the SQL query matches the schema of
10750 ** the database against which the compiled query is actually executed.
10751 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
10752 ** the schema-version is potentially dangerous and may lead to program
10753 ** crashes or database corruption. Use with caution!
10754 **
10755 ** The user-version is not used internally by SQLite. It may be used by
10756 ** applications for any purpose.
10757 */
10758 case PragTyp_HEADER_VALUE: {
10759 int iCookie = pPragma->iArg; /* Which cookie to read or write */
10760 sqlite3VdbeUsesBtree(v, iDb);
10761 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
10762 /* Write the specified cookie value */
10763 static const VdbeOpList setCookie[] = {
10764 { OP_Transaction, 0, 1, 0}, /* 0 */
10765 { OP_SetCookie, 0, 0, 0}, /* 1 */
10766 };
10767 VdbeOp *aOp;
10768 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
10769 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
10770 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
10771 aOp[0].p1 = iDb;
10772 aOp[1].p1 = iDb;
10773 aOp[1].p2 = iCookie;
10774 aOp[1].p3 = sqlite3Atoi(zRight);
10775 }else{
10776 /* Read the specified cookie value */
10777 static const VdbeOpList readCookie[] = {
10778 { OP_Transaction, 0, 0, 0}, /* 0 */
10779 { OP_ReadCookie, 0, 1, 0}, /* 1 */
10780 { OP_ResultRow, 1, 1, 0}
10781 };
10782 VdbeOp *aOp;
10783 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
10784 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
10785 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
10786 aOp[0].p1 = iDb;
10787 aOp[1].p1 = iDb;
10788 aOp[1].p3 = iCookie;
10789 sqlite3VdbeReusable(v);
10790 }
10791 }
10792 break;
10793 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
10794
10795 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
10796 /*
10797 ** PRAGMA compile_options
10798 **
10799 ** Return the names of all compile-time options used in this build,
10800 ** one option per row.
10801 */
10802 case PragTyp_COMPILE_OPTIONS: {
10803 int i = 0;
10804 const char *zOpt;
10805 pParse->nMem = 1;
10806 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
10807 sqlite3VdbeLoadString(v, 1, zOpt);
10808 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
10809 }
10810 sqlite3VdbeReusable(v);
10811 }
10812 break;
10813 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
10814
10815 #ifndef SQLITE_OMIT_WAL
10816 /*
10817 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
10818 **
10819 ** Checkpoint the database.
10820 */
10821 case PragTyp_WAL_CHECKPOINT: {
10822 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
10823 int eMode = SQLITE_CHECKPOINT_PASSIVE;
10824 if( zRight ){
10825 if( sqlite3StrICmp(zRight, "full")==0 ){
10826 eMode = SQLITE_CHECKPOINT_FULL;
10827 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
10828 eMode = SQLITE_CHECKPOINT_RESTART;
10829 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
10830 eMode = SQLITE_CHECKPOINT_TRUNCATE;
10831 }
10832 }
10833 pParse->nMem = 3;
10834 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
10835 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
10836 }
10837 break;
10838
10839 /*
10840 ** PRAGMA wal_autocheckpoint
10841 ** PRAGMA wal_autocheckpoint = N
10842 **
10843 ** Configure a database connection to automatically checkpoint a database
10844 ** after accumulating N frames in the log. Or query for the current value
10845 ** of N.
10846 */
10847 case PragTyp_WAL_AUTOCHECKPOINT: {
10848 if( zRight ){
10849 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
10850 }
10851 returnSingleInt(v,
10852 db->xWalCallback==sqlite3WalDefaultHook ?
10853 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
10854 }
10855 break;
10856 #endif
10857
10858 /*
10859 ** PRAGMA shrink_memory
10860 **
10861 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
10862 ** connection on which it is invoked to free up as much memory as it
10863 ** can, by calling sqlite3_db_release_memory().
10864 */
10865 case PragTyp_SHRINK_MEMORY: {
10866 sqlite3_db_release_memory(db);
10867 break;
10868 }
10869
10870 /*
10871 ** PRAGMA busy_timeout
10872 ** PRAGMA busy_timeout = N
10873 **
10874 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
10875 ** if one is set. If no busy handler or a different busy handler is set
10876 ** then 0 is returned. Setting the busy_timeout to 0 or negative
10877 ** disables the timeout.
10878 */
10879 /*case PragTyp_BUSY_TIMEOUT*/ default: {
10880 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
10881 if( zRight ){
10882 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
10883 }
10884 returnSingleInt(v, db->busyTimeout);
10885 break;
10886 }
10887
10888 /*
10889 ** PRAGMA soft_heap_limit
10890 ** PRAGMA soft_heap_limit = N
10891 **
10892 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
10893 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
10894 ** specified and is a non-negative integer.
10895 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
10896 ** returns the same integer that would be returned by the
10897 ** sqlite3_soft_heap_limit64(-1) C-language function.
10898 */
10899 case PragTyp_SOFT_HEAP_LIMIT: {
10900 sqlite3_int64 N;
10901 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
10902 sqlite3_soft_heap_limit64(N);
10903 }
10904 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
10905 break;
10906 }
10907
10908 /*
10909 ** PRAGMA threads
10910 ** PRAGMA threads = N
10911 **
10912 ** Configure the maximum number of worker threads. Return the new
10913 ** maximum, which might be less than requested.
10914 */
10915 case PragTyp_THREADS: {
10916 sqlite3_int64 N;
10917 if( zRight
10918 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
10919 && N>=0
10920 ){
10921 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
10922 }
10923 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
10924 break;
10925 }
10926
10927 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
10928 /*
10929 ** Report the current state of file logs for all databases
10930 */
10931 case PragTyp_LOCK_STATUS: {
10932 static const char *const azLockName[] = {
10933 "unlocked", "shared", "reserved", "pending", "exclusive"
10934 };
10935 int i;
10936 pParse->nMem = 2;
10937 for(i=0; i<db->nDb; i++){
10938 Btree *pBt;
10939 const char *zState = "unknown";
10940 int j;
10941 if( db->aDb[i].zDbSName==0 ) continue;
10942 pBt = db->aDb[i].pBt;
10943 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
10944 zState = "closed";
10945 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
10946 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
10947 zState = azLockName[j];
10948 }
10949 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
10950 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
10951 }
10952 break;
10953 }
10954 #endif
10955
10956 #ifdef SQLITE_HAS_CODEC
10957 case PragTyp_KEY: {
10958 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
10959 break;
10960 }
10961 case PragTyp_REKEY: {
10962 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
10963 break;
10964 }
10965 case PragTyp_HEXKEY: {
10966 if( zRight ){
10967 u8 iByte;
10968 int i;
10969 char zKey[40];
10970 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
10971 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
10972 if( (i&1)!=0 ) zKey[i/2] = iByte;
10973 }
10974 if( (zLeft[3] & 0xf)==0xb ){
10975 sqlite3_key_v2(db, zDb, zKey, i/2);
10976 }else{
10977 sqlite3_rekey_v2(db, zDb, zKey, i/2);
10978 }
10979 }
10980 break;
10981 }
10982 #endif
10983 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
10984 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
10985 #ifdef SQLITE_HAS_CODEC
10986 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
10987 sqlite3_activate_see(&zRight[4]);
10988 }
10989 #endif
10990 #ifdef SQLITE_ENABLE_CEROD
10991 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
10992 sqlite3_activate_cerod(&zRight[6]);
10993 }
10994 #endif
10995 }
10996 break;
10997 #endif
10998
10999 } /* End of the PRAGMA switch */
11000
11001 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
11002 ** purpose is to execute assert() statements to verify that if the
11003 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
11004 ** to the PRAGMA, the implementation has not added any OP_ResultRow
11005 ** instructions to the VM. */
11006 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
11007 sqlite3VdbeVerifyNoResultRow(v);
11008 }
11009
11010 pragma_out:
11011 sqlite3DbFree(db, zLeft);
11012 sqlite3DbFree(db, zRight);
11013 }
11014 #ifndef SQLITE_OMIT_VIRTUALTABLE
11015 /*****************************************************************************
11016 ** Implementation of an eponymous virtual table that runs a pragma.
11017 **
11018 */
11019 typedef struct PragmaVtab PragmaVtab;
11020 typedef struct PragmaVtabCursor PragmaVtabCursor;
11021 struct PragmaVtab {
11022 sqlite3_vtab base; /* Base class. Must be first */
11023 sqlite3 *db; /* The database connection to which it belongs */
11024 const PragmaName *pName; /* Name of the pragma */
11025 u8 nHidden; /* Number of hidden columns */
11026 u8 iHidden; /* Index of the first hidden column */
11027 };
11028 struct PragmaVtabCursor {
11029 sqlite3_vtab_cursor base; /* Base class. Must be first */
11030 sqlite3_stmt *pPragma; /* The pragma statement to run */
11031 sqlite_int64 iRowid; /* Current rowid */
11032 char *azArg[2]; /* Value of the argument and schema */
11033 };
11034
11035 /*
11036 ** Pragma virtual table module xConnect method.
11037 */
11038 static int pragmaVtabConnect(
11039 sqlite3 *db,
11040 void *pAux,
11041 int argc, const char *const*argv,
11042 sqlite3_vtab **ppVtab,
11043 char **pzErr
11044 ){
11045 const PragmaName *pPragma = (const PragmaName*)pAux;
11046 PragmaVtab *pTab = 0;
11047 int rc;
11048 int i, j;
11049 char cSep = '(';
11050 StrAccum acc;
11051 char zBuf[200];
11052
11053 UNUSED_PARAMETER(argc);
11054 UNUSED_PARAMETER(argv);
11055 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
11056 sqlite3StrAccumAppendAll(&acc, "CREATE TABLE x");
11057 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
11058 sqlite3XPrintf(&acc, "%c\"%s\"", cSep, pragCName[j]);
11059 cSep = ',';
11060 }
11061 if( i==0 ){
11062 sqlite3XPrintf(&acc, "(\"%s\"", pPragma->zName);
11063 cSep = ',';
11064 i++;
11065 }
11066 j = 0;
11067 if( pPragma->mPragFlg & PragFlg_Result1 ){
11068 sqlite3StrAccumAppendAll(&acc, ",arg HIDDEN");
11069 j++;
11070 }
11071 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
11072 sqlite3StrAccumAppendAll(&acc, ",schema HIDDEN");
11073 j++;
11074 }
11075 sqlite3StrAccumAppend(&acc, ")", 1);
11076 sqlite3StrAccumFinish(&acc);
11077 assert( strlen(zBuf) < sizeof(zBuf)-1 );
11078 rc = sqlite3_declare_vtab(db, zBuf);
11079 if( rc==SQLITE_OK ){
11080 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
11081 if( pTab==0 ){
11082 rc = SQLITE_NOMEM;
11083 }else{
11084 memset(pTab, 0, sizeof(PragmaVtab));
11085 pTab->pName = pPragma;
11086 pTab->db = db;
11087 pTab->iHidden = i;
11088 pTab->nHidden = j;
11089 }
11090 }else{
11091 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
11092 }
11093
11094 *ppVtab = (sqlite3_vtab*)pTab;
11095 return rc;
11096 }
11097
11098 /*
11099 ** Pragma virtual table module xDisconnect method.
11100 */
11101 static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
11102 PragmaVtab *pTab = (PragmaVtab*)pVtab;
11103 sqlite3_free(pTab);
11104 return SQLITE_OK;
11105 }
11106
11107 /* Figure out the best index to use to search a pragma virtual table.
11108 **
11109 ** There are not really any index choices. But we want to encourage the
11110 ** query planner to give == constraints on as many hidden parameters as
11111 ** possible, and especially on the first hidden parameter. So return a
11112 ** high cost if hidden parameters are unconstrained.
11113 */
11114 static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
11115 PragmaVtab *pTab = (PragmaVtab*)tab;
11116 const struct sqlite3_index_constraint *pConstraint;
11117 int i, j;
11118 int seen[2];
11119
11120 pIdxInfo->estimatedCost = (double)1;
11121 if( pTab->nHidden==0 ){ return SQLITE_OK; }
11122 pConstraint = pIdxInfo->aConstraint;
11123 seen[0] = 0;
11124 seen[1] = 0;
11125 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
11126 if( pConstraint->usable==0 ) continue;
11127 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
11128 if( pConstraint->iColumn < pTab->iHidden ) continue;
11129 j = pConstraint->iColumn - pTab->iHidden;
11130 assert( j < 2 );
11131 seen[j] = i+1;
11132 }
11133 if( seen[0]==0 ){
11134 pIdxInfo->estimatedCost = (double)2147483647;
11135 pIdxInfo->estimatedRows = 2147483647;
11136 return SQLITE_OK;
11137 }
11138 j = seen[0]-1;
11139 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
11140 pIdxInfo->aConstraintUsage[j].omit = 1;
11141 if( seen[1]==0 ) return SQLITE_OK;
11142 pIdxInfo->estimatedCost = (double)20;
11143 pIdxInfo->estimatedRows = 20;
11144 j = seen[1]-1;
11145 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
11146 pIdxInfo->aConstraintUsage[j].omit = 1;
11147 return SQLITE_OK;
11148 }
11149
11150 /* Create a new cursor for the pragma virtual table */
11151 static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
11152 PragmaVtabCursor *pCsr;
11153 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
11154 if( pCsr==0 ) return SQLITE_NOMEM;
11155 memset(pCsr, 0, sizeof(PragmaVtabCursor));
11156 pCsr->base.pVtab = pVtab;
11157 *ppCursor = &pCsr->base;
11158 return SQLITE_OK;
11159 }
11160
11161 /* Clear all content from pragma virtual table cursor. */
11162 static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
11163 int i;
11164 sqlite3_finalize(pCsr->pPragma);
11165 pCsr->pPragma = 0;
11166 for(i=0; i<ArraySize(pCsr->azArg); i++){
11167 sqlite3_free(pCsr->azArg[i]);
11168 pCsr->azArg[i] = 0;
11169 }
11170 }
11171
11172 /* Close a pragma virtual table cursor */
11173 static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
11174 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
11175 pragmaVtabCursorClear(pCsr);
11176 sqlite3_free(pCsr);
11177 return SQLITE_OK;
11178 }
11179
11180 /* Advance the pragma virtual table cursor to the next row */
11181 static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
11182 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
11183 int rc = SQLITE_OK;
11184
11185 /* Increment the xRowid value */
11186 pCsr->iRowid++;
11187 assert( pCsr->pPragma );
11188 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
11189 rc = sqlite3_finalize(pCsr->pPragma);
11190 pCsr->pPragma = 0;
11191 pragmaVtabCursorClear(pCsr);
11192 }
11193 return rc;
11194 }
11195
11196 /*
11197 ** Pragma virtual table module xFilter method.
11198 */
11199 static int pragmaVtabFilter(
11200 sqlite3_vtab_cursor *pVtabCursor,
11201 int idxNum, const char *idxStr,
11202 int argc, sqlite3_value **argv
11203 ){
11204 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
11205 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
11206 int rc;
11207 int i, j;
11208 StrAccum acc;
11209 char *zSql;
11210
11211 UNUSED_PARAMETER(idxNum);
11212 UNUSED_PARAMETER(idxStr);
11213 pragmaVtabCursorClear(pCsr);
11214 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
11215 for(i=0; i<argc; i++, j++){
11216 assert( j<ArraySize(pCsr->azArg) );
11217 pCsr->azArg[j] = sqlite3_mprintf("%s", sqlite3_value_text(argv[i]));
11218 if( pCsr->azArg[j]==0 ){
11219 return SQLITE_NOMEM;
11220 }
11221 }
11222 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
11223 sqlite3StrAccumAppendAll(&acc, "PRAGMA ");
11224 if( pCsr->azArg[1] ){
11225 sqlite3XPrintf(&acc, "%Q.", pCsr->azArg[1]);
11226 }
11227 sqlite3StrAccumAppendAll(&acc, pTab->pName->zName);
11228 if( pCsr->azArg[0] ){
11229 sqlite3XPrintf(&acc, "=%Q", pCsr->azArg[0]);
11230 }
11231 zSql = sqlite3StrAccumFinish(&acc);
11232 if( zSql==0 ) return SQLITE_NOMEM;
11233 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
11234 sqlite3_free(zSql);
11235 if( rc!=SQLITE_OK ){
11236 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
11237 return rc;
11238 }
11239 return pragmaVtabNext(pVtabCursor);
11240 }
11241
11242 /*
11243 ** Pragma virtual table module xEof method.
11244 */
11245 static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
11246 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
11247 return (pCsr->pPragma==0);
11248 }
11249
11250 /* The xColumn method simply returns the corresponding column from
11251 ** the PRAGMA.
11252 */
11253 static int pragmaVtabColumn(
11254 sqlite3_vtab_cursor *pVtabCursor,
11255 sqlite3_context *ctx,
11256 int i
11257 ){
11258 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
11259 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
11260 if( i<pTab->iHidden ){
11261 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
11262 }else{
11263 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
11264 }
11265 return SQLITE_OK;
11266 }
11267
11268 /*
11269 ** Pragma virtual table module xRowid method.
11270 */
11271 static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
11272 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
11273 *p = pCsr->iRowid;
11274 return SQLITE_OK;
11275 }
11276
11277 /* The pragma virtual table object */
11278 static const sqlite3_module pragmaVtabModule = {
11279 0, /* iVersion */
11280 0, /* xCreate - create a table */
11281 pragmaVtabConnect, /* xConnect - connect to an existing table */
11282 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
11283 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
11284 0, /* xDestroy - Drop a table */
11285 pragmaVtabOpen, /* xOpen - open a cursor */
11286 pragmaVtabClose, /* xClose - close a cursor */
11287 pragmaVtabFilter, /* xFilter - configure scan constraints */
11288 pragmaVtabNext, /* xNext - advance a cursor */
11289 pragmaVtabEof, /* xEof */
11290 pragmaVtabColumn, /* xColumn - read data */
11291 pragmaVtabRowid, /* xRowid - read data */
11292 0, /* xUpdate - write data */
11293 0, /* xBegin - begin transaction */
11294 0, /* xSync - sync transaction */
11295 0, /* xCommit - commit transaction */
11296 0, /* xRollback - rollback transaction */
11297 0, /* xFindFunction - function overloading */
11298 0, /* xRename - rename the table */
11299 0, /* xSavepoint */
11300 0, /* xRelease */
11301 0 /* xRollbackTo */
11302 };
11303
11304 /*
11305 ** Check to see if zTabName is really the name of a pragma. If it is,
11306 ** then register an eponymous virtual table for that pragma and return
11307 ** a pointer to the Module object for the new virtual table.
11308 */
11309 SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName) {
11310 const PragmaName *pName;
11311 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
11312 pName = pragmaLocate(zName+7);
11313 if( pName==0 ) return 0;
11314 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
11315 assert( sqlite3HashFind(&db->aModule, zName)==0 );
11316 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
11317 }
11318
11319 #endif /* SQLITE_OMIT_VIRTUALTABLE */
11320
11321 #endif /* SQLITE_OMIT_PRAGMA */
11322
11323 /************** End of pragma.c **********************************************/
11324 /************** Begin file prepare.c *****************************************/
11325 /*
11326 ** 2005 May 25
11327 **
11328 ** The author disclaims copyright to this source code. In place of
11329 ** a legal notice, here is a blessing:
11330 **
11331 ** May you do good and not evil.
11332 ** May you find forgiveness for yourself and forgive others.
11333 ** May you share freely, never taking more than you give.
11334 **
11335 *************************************************************************
11336 ** This file contains the implementation of the sqlite3_prepare()
11337 ** interface, and routines that contribute to loading the database schema
11338 ** from disk.
11339 */
11340 /* #include "sqliteInt.h" */
11341
11342 /*
11343 ** Fill the InitData structure with an error message that indicates
11344 ** that the database is corrupt.
11345 */
11346 static void corruptSchema(
11347 InitData *pData, /* Initialization context */
11348 const char *zObj, /* Object being parsed at the point of error */
11349 const char *zExtra /* Error information */
11350 ){
11351 sqlite3 *db = pData->db;
11352 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
11353 char *z;
11354 if( zObj==0 ) zObj = "?";
11355 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
11356 if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
11357 sqlite3DbFree(db, *pData->pzErrMsg);
11358 *pData->pzErrMsg = z;
11359 }
11360 pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
11361 }
11362
11363 /*
11364 ** This is the callback routine for the code that initializes the
11365 ** database. See sqlite3Init() below for additional information.
11366 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
11367 **
11368 ** Each callback contains the following information:
11369 **
11370 ** argv[0] = name of thing being created
11371 ** argv[1] = root page number for table or index. 0 for trigger or view.
11372 ** argv[2] = SQL text for the CREATE statement.
11373 **
11374 */
11375 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
11376 InitData *pData = (InitData*)pInit;
11377 sqlite3 *db = pData->db;
11378 int iDb = pData->iDb;
11379
11380 assert( argc==3 );
11381 UNUSED_PARAMETER2(NotUsed, argc);
11382 assert( sqlite3_mutex_held(db->mutex) );
11383 DbClearProperty(db, iDb, DB_Empty);
11384 if( db->mallocFailed ){
11385 corruptSchema(pData, argv[0], 0);
11386 return 1;
11387 }
11388
11389 assert( iDb>=0 && iDb<db->nDb );
11390 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
11391 if( argv[1]==0 ){
11392 corruptSchema(pData, argv[0], 0);
11393 }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
11394 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
11395 ** But because db->init.busy is set to 1, no VDBE code is generated
11396 ** or executed. All the parser does is build the internal data
11397 ** structures that describe the table, index, or view.
11398 */
11399 int rc;
11400 u8 saved_iDb = db->init.iDb;
11401 sqlite3_stmt *pStmt;
11402 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
11403
11404 assert( db->init.busy );
11405 db->init.iDb = iDb;
11406 db->init.newTnum = sqlite3Atoi(argv[1]);
11407 db->init.orphanTrigger = 0;
11408 TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
11409 rc = db->errCode;
11410 assert( (rc&0xFF)==(rcp&0xFF) );
11411 db->init.iDb = saved_iDb;
11412 assert( saved_iDb==0 || (db->flags & SQLITE_Vacuum)!=0 );
11413 if( SQLITE_OK!=rc ){
11414 if( db->init.orphanTrigger ){
11415 assert( iDb==1 );
11416 }else{
11417 pData->rc = rc;
11418 if( rc==SQLITE_NOMEM ){
11419 sqlite3OomFault(db);
11420 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
11421 corruptSchema(pData, argv[0], sqlite3_errmsg(db));
11422 }
11423 }
11424 }
11425 sqlite3_finalize(pStmt);
11426 }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
11427 corruptSchema(pData, argv[0], 0);
11428 }else{
11429 /* If the SQL column is blank it means this is an index that
11430 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
11431 ** constraint for a CREATE TABLE. The index should have already
11432 ** been created when we processed the CREATE TABLE. All we have
11433 ** to do here is record the root page number for that index.
11434 */
11435 Index *pIndex;
11436 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
11437 if( pIndex==0 ){
11438 /* This can occur if there exists an index on a TEMP table which
11439 ** has the same name as another index on a permanent index. Since
11440 ** the permanent table is hidden by the TEMP table, we can also
11441 ** safely ignore the index on the permanent table.
11442 */
11443 /* Do Nothing */;
11444 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
11445 corruptSchema(pData, argv[0], "invalid rootpage");
11446 }
11447 }
11448 return 0;
11449 }
11450
11451 /*
11452 ** Attempt to read the database schema and initialize internal
11453 ** data structures for a single database file. The index of the
11454 ** database file is given by iDb. iDb==0 is used for the main
11455 ** database. iDb==1 should never be used. iDb>=2 is used for
11456 ** auxiliary databases. Return one of the SQLITE_ error codes to
11457 ** indicate success or failure.
11458 */
11459 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
11460 int rc;
11461 int i;
11462 #ifndef SQLITE_OMIT_DEPRECATED
11463 int size;
11464 #endif
11465 Db *pDb;
11466 char const *azArg[4];
11467 int meta[5];
11468 InitData initData;
11469 const char *zMasterName;
11470 int openedTransaction = 0;
11471
11472 assert( iDb>=0 && iDb<db->nDb );
11473 assert( db->aDb[iDb].pSchema );
11474 assert( sqlite3_mutex_held(db->mutex) );
11475 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
11476
11477 /* Construct the in-memory representation schema tables (sqlite_master or
11478 ** sqlite_temp_master) by invoking the parser directly. The appropriate
11479 ** table name will be inserted automatically by the parser so we can just
11480 ** use the abbreviation "x" here. The parser will also automatically tag
11481 ** the schema table as read-only. */
11482 azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
11483 azArg[1] = "1";
11484 azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
11485 "rootpage integer,sql text)";
11486 azArg[3] = 0;
11487 initData.db = db;
11488 initData.iDb = iDb;
11489 initData.rc = SQLITE_OK;
11490 initData.pzErrMsg = pzErrMsg;
11491 sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
11492 if( initData.rc ){
11493 rc = initData.rc;
11494 goto error_out;
11495 }
11496
11497 /* Create a cursor to hold the database open
11498 */
11499 pDb = &db->aDb[iDb];
11500 if( pDb->pBt==0 ){
11501 if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
11502 DbSetProperty(db, 1, DB_SchemaLoaded);
11503 }
11504 return SQLITE_OK;
11505 }
11506
11507 /* If there is not already a read-only (or read-write) transaction opened
11508 ** on the b-tree database, open one now. If a transaction is opened, it
11509 ** will be closed before this function returns. */
11510 sqlite3BtreeEnter(pDb->pBt);
11511 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
11512 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
11513 if( rc!=SQLITE_OK ){
11514 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
11515 goto initone_error_out;
11516 }
11517 openedTransaction = 1;
11518 }
11519
11520 /* Get the database meta information.
11521 **
11522 ** Meta values are as follows:
11523 ** meta[0] Schema cookie. Changes with each schema change.
11524 ** meta[1] File format of schema layer.
11525 ** meta[2] Size of the page cache.
11526 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
11527 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
11528 ** meta[5] User version
11529 ** meta[6] Incremental vacuum mode
11530 ** meta[7] unused
11531 ** meta[8] unused
11532 ** meta[9] unused
11533 **
11534 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
11535 ** the possible values of meta[4].
11536 */
11537 for(i=0; i<ArraySize(meta); i++){
11538 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
11539 }
11540 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
11541
11542 /* If opening a non-empty database, check the text encoding. For the
11543 ** main database, set sqlite3.enc to the encoding of the main database.
11544 ** For an attached db, it is an error if the encoding is not the same
11545 ** as sqlite3.enc.
11546 */
11547 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
11548 if( iDb==0 ){
11549 #ifndef SQLITE_OMIT_UTF16
11550 u8 encoding;
11551 /* If opening the main database, set ENC(db). */
11552 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
11553 if( encoding==0 ) encoding = SQLITE_UTF8;
11554 ENC(db) = encoding;
11555 #else
11556 ENC(db) = SQLITE_UTF8;
11557 #endif
11558 }else{
11559 /* If opening an attached database, the encoding much match ENC(db) */
11560 if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
11561 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
11562 " text encoding as main database");
11563 rc = SQLITE_ERROR;
11564 goto initone_error_out;
11565 }
11566 }
11567 }else{
11568 DbSetProperty(db, iDb, DB_Empty);
11569 }
11570 pDb->pSchema->enc = ENC(db);
11571
11572 if( pDb->pSchema->cache_size==0 ){
11573 #ifndef SQLITE_OMIT_DEPRECATED
11574 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
11575 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
11576 pDb->pSchema->cache_size = size;
11577 #else
11578 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
11579 #endif
11580 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
11581 }
11582
11583 /*
11584 ** file_format==1 Version 3.0.0.
11585 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
11586 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
11587 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
11588 */
11589 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
11590 if( pDb->pSchema->file_format==0 ){
11591 pDb->pSchema->file_format = 1;
11592 }
11593 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
11594 sqlite3SetString(pzErrMsg, db, "unsupported file format");
11595 rc = SQLITE_ERROR;
11596 goto initone_error_out;
11597 }
11598
11599 /* Ticket #2804: When we open a database in the newer file format,
11600 ** clear the legacy_file_format pragma flag so that a VACUUM will
11601 ** not downgrade the database and thus invalidate any descending
11602 ** indices that the user might have created.
11603 */
11604 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
11605 db->flags &= ~SQLITE_LegacyFileFmt;
11606 }
11607
11608 /* Read the schema information out of the schema tables
11609 */
11610 assert( db->init.busy );
11611 {
11612 char *zSql;
11613 zSql = sqlite3MPrintf(db,
11614 "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
11615 db->aDb[iDb].zDbSName, zMasterName);
11616 #ifndef SQLITE_OMIT_AUTHORIZATION
11617 {
11618 sqlite3_xauth xAuth;
11619 xAuth = db->xAuth;
11620 db->xAuth = 0;
11621 #endif
11622 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
11623 #ifndef SQLITE_OMIT_AUTHORIZATION
11624 db->xAuth = xAuth;
11625 }
11626 #endif
11627 if( rc==SQLITE_OK ) rc = initData.rc;
11628 sqlite3DbFree(db, zSql);
11629 #ifndef SQLITE_OMIT_ANALYZE
11630 if( rc==SQLITE_OK ){
11631 sqlite3AnalysisLoad(db, iDb);
11632 }
11633 #endif
11634 }
11635 if( db->mallocFailed ){
11636 rc = SQLITE_NOMEM_BKPT;
11637 sqlite3ResetAllSchemasOfConnection(db);
11638 }
11639 if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
11640 /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
11641 ** the schema loaded, even if errors occurred. In this situation the
11642 ** current sqlite3_prepare() operation will fail, but the following one
11643 ** will attempt to compile the supplied statement against whatever subset
11644 ** of the schema was loaded before the error occurred. The primary
11645 ** purpose of this is to allow access to the sqlite_master table
11646 ** even when its contents have been corrupted.
11647 */
11648 DbSetProperty(db, iDb, DB_SchemaLoaded);
11649 rc = SQLITE_OK;
11650 }
11651
11652 /* Jump here for an error that occurs after successfully allocating
11653 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
11654 ** before that point, jump to error_out.
11655 */
11656 initone_error_out:
11657 if( openedTransaction ){
11658 sqlite3BtreeCommit(pDb->pBt);
11659 }
11660 sqlite3BtreeLeave(pDb->pBt);
11661
11662 error_out:
11663 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
11664 sqlite3OomFault(db);
11665 }
11666 return rc;
11667 }
11668
11669 /*
11670 ** Initialize all database files - the main database file, the file
11671 ** used to store temporary tables, and any additional database files
11672 ** created using ATTACH statements. Return a success code. If an
11673 ** error occurs, write an error message into *pzErrMsg.
11674 **
11675 ** After a database is initialized, the DB_SchemaLoaded bit is set
11676 ** bit is set in the flags field of the Db structure. If the database
11677 ** file was of zero-length, then the DB_Empty flag is also set.
11678 */
11679 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
11680 int i, rc;
11681 int commit_internal = !(db->flags&SQLITE_InternChanges);
11682
11683 assert( sqlite3_mutex_held(db->mutex) );
11684 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
11685 assert( db->init.busy==0 );
11686 rc = SQLITE_OK;
11687 db->init.busy = 1;
11688 ENC(db) = SCHEMA_ENC(db);
11689 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
11690 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
11691 rc = sqlite3InitOne(db, i, pzErrMsg);
11692 if( rc ){
11693 sqlite3ResetOneSchema(db, i);
11694 }
11695 }
11696
11697 /* Once all the other databases have been initialized, load the schema
11698 ** for the TEMP database. This is loaded last, as the TEMP database
11699 ** schema may contain references to objects in other databases.
11700 */
11701 #ifndef SQLITE_OMIT_TEMPDB
11702 assert( db->nDb>1 );
11703 if( rc==SQLITE_OK && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
11704 rc = sqlite3InitOne(db, 1, pzErrMsg);
11705 if( rc ){
11706 sqlite3ResetOneSchema(db, 1);
11707 }
11708 }
11709 #endif
11710
11711 db->init.busy = 0;
11712 if( rc==SQLITE_OK && commit_internal ){
11713 sqlite3CommitInternalChanges(db);
11714 }
11715
11716 return rc;
11717 }
11718
11719 /*
11720 ** This routine is a no-op if the database schema is already initialized.
11721 ** Otherwise, the schema is loaded. An error code is returned.
11722 */
11723 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
11724 int rc = SQLITE_OK;
11725 sqlite3 *db = pParse->db;
11726 assert( sqlite3_mutex_held(db->mutex) );
11727 if( !db->init.busy ){
11728 rc = sqlite3Init(db, &pParse->zErrMsg);
11729 }
11730 if( rc!=SQLITE_OK ){
11731 pParse->rc = rc;
11732 pParse->nErr++;
11733 }
11734 return rc;
11735 }
11736
11737
11738 /*
11739 ** Check schema cookies in all databases. If any cookie is out
11740 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
11741 ** make no changes to pParse->rc.
11742 */
11743 static void schemaIsValid(Parse *pParse){
11744 sqlite3 *db = pParse->db;
11745 int iDb;
11746 int rc;
11747 int cookie;
11748
11749 assert( pParse->checkSchema );
11750 assert( sqlite3_mutex_held(db->mutex) );
11751 for(iDb=0; iDb<db->nDb; iDb++){
11752 int openedTransaction = 0; /* True if a transaction is opened */
11753 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
11754 if( pBt==0 ) continue;
11755
11756 /* If there is not already a read-only (or read-write) transaction opened
11757 ** on the b-tree database, open one now. If a transaction is opened, it
11758 ** will be closed immediately after reading the meta-value. */
11759 if( !sqlite3BtreeIsInReadTrans(pBt) ){
11760 rc = sqlite3BtreeBeginTrans(pBt, 0);
11761 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
11762 sqlite3OomFault(db);
11763 }
11764 if( rc!=SQLITE_OK ) return;
11765 openedTransaction = 1;
11766 }
11767
11768 /* Read the schema cookie from the database. If it does not match the
11769 ** value stored as part of the in-memory schema representation,
11770 ** set Parse.rc to SQLITE_SCHEMA. */
11771 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
11772 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
11773 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
11774 sqlite3ResetOneSchema(db, iDb);
11775 pParse->rc = SQLITE_SCHEMA;
11776 }
11777
11778 /* Close the transaction, if one was opened. */
11779 if( openedTransaction ){
11780 sqlite3BtreeCommit(pBt);
11781 }
11782 }
11783 }
11784
11785 /*
11786 ** Convert a schema pointer into the iDb index that indicates
11787 ** which database file in db->aDb[] the schema refers to.
11788 **
11789 ** If the same database is attached more than once, the first
11790 ** attached database is returned.
11791 */
11792 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
11793 int i = -1000000;
11794
11795 /* If pSchema is NULL, then return -1000000. This happens when code in
11796 ** expr.c is trying to resolve a reference to a transient table (i.e. one
11797 ** created by a sub-select). In this case the return value of this
11798 ** function should never be used.
11799 **
11800 ** We return -1000000 instead of the more usual -1 simply because using
11801 ** -1000000 as the incorrect index into db->aDb[] is much
11802 ** more likely to cause a segfault than -1 (of course there are assert()
11803 ** statements too, but it never hurts to play the odds).
11804 */
11805 assert( sqlite3_mutex_held(db->mutex) );
11806 if( pSchema ){
11807 for(i=0; ALWAYS(i<db->nDb); i++){
11808 if( db->aDb[i].pSchema==pSchema ){
11809 break;
11810 }
11811 }
11812 assert( i>=0 && i<db->nDb );
11813 }
11814 return i;
11815 }
11816
11817 /*
11818 ** Free all memory allocations in the pParse object
11819 */
11820 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
11821 if( pParse ){
11822 sqlite3 *db = pParse->db;
11823 sqlite3DbFree(db, pParse->aLabel);
11824 sqlite3ExprListDelete(db, pParse->pConstExpr);
11825 if( db ){
11826 assert( db->lookaside.bDisable >= pParse->disableLookaside );
11827 db->lookaside.bDisable -= pParse->disableLookaside;
11828 }
11829 pParse->disableLookaside = 0;
11830 }
11831 }
11832
11833 /*
11834 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
11835 */
11836 static int sqlite3Prepare(
11837 sqlite3 *db, /* Database handle. */
11838 const char *zSql, /* UTF-8 encoded SQL statement. */
11839 int nBytes, /* Length of zSql in bytes. */
11840 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
11841 Vdbe *pReprepare, /* VM being reprepared */
11842 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
11843 const char **pzTail /* OUT: End of parsed string */
11844 ){
11845 char *zErrMsg = 0; /* Error message */
11846 int rc = SQLITE_OK; /* Result code */
11847 int i; /* Loop counter */
11848 Parse sParse; /* Parsing context */
11849
11850 memset(&sParse, 0, PARSE_HDR_SZ);
11851 memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
11852 sParse.pReprepare = pReprepare;
11853 assert( ppStmt && *ppStmt==0 );
11854 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
11855 assert( sqlite3_mutex_held(db->mutex) );
11856
11857 /* Check to verify that it is possible to get a read lock on all
11858 ** database schemas. The inability to get a read lock indicates that
11859 ** some other database connection is holding a write-lock, which in
11860 ** turn means that the other connection has made uncommitted changes
11861 ** to the schema.
11862 **
11863 ** Were we to proceed and prepare the statement against the uncommitted
11864 ** schema changes and if those schema changes are subsequently rolled
11865 ** back and different changes are made in their place, then when this
11866 ** prepared statement goes to run the schema cookie would fail to detect
11867 ** the schema change. Disaster would follow.
11868 **
11869 ** This thread is currently holding mutexes on all Btrees (because
11870 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
11871 ** is not possible for another thread to start a new schema change
11872 ** while this routine is running. Hence, we do not need to hold
11873 ** locks on the schema, we just need to make sure nobody else is
11874 ** holding them.
11875 **
11876 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
11877 ** but it does *not* override schema lock detection, so this all still
11878 ** works even if READ_UNCOMMITTED is set.
11879 */
11880 for(i=0; i<db->nDb; i++) {
11881 Btree *pBt = db->aDb[i].pBt;
11882 if( pBt ){
11883 assert( sqlite3BtreeHoldsMutex(pBt) );
11884 rc = sqlite3BtreeSchemaLocked(pBt);
11885 if( rc ){
11886 const char *zDb = db->aDb[i].zDbSName;
11887 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
11888 testcase( db->flags & SQLITE_ReadUncommitted );
11889 goto end_prepare;
11890 }
11891 }
11892 }
11893
11894 sqlite3VtabUnlockList(db);
11895
11896 sParse.db = db;
11897 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
11898 char *zSqlCopy;
11899 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
11900 testcase( nBytes==mxLen );
11901 testcase( nBytes==mxLen+1 );
11902 if( nBytes>mxLen ){
11903 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
11904 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
11905 goto end_prepare;
11906 }
11907 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
11908 if( zSqlCopy ){
11909 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
11910 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
11911 sqlite3DbFree(db, zSqlCopy);
11912 }else{
11913 sParse.zTail = &zSql[nBytes];
11914 }
11915 }else{
11916 sqlite3RunParser(&sParse, zSql, &zErrMsg);
11917 }
11918 assert( 0==sParse.nQueryLoop );
11919
11920 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
11921 if( sParse.checkSchema ){
11922 schemaIsValid(&sParse);
11923 }
11924 if( db->mallocFailed ){
11925 sParse.rc = SQLITE_NOMEM_BKPT;
11926 }
11927 if( pzTail ){
11928 *pzTail = sParse.zTail;
11929 }
11930 rc = sParse.rc;
11931
11932 #ifndef SQLITE_OMIT_EXPLAIN
11933 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
11934 static const char * const azColName[] = {
11935 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
11936 "selectid", "order", "from", "detail"
11937 };
11938 int iFirst, mx;
11939 if( sParse.explain==2 ){
11940 sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
11941 iFirst = 8;
11942 mx = 12;
11943 }else{
11944 sqlite3VdbeSetNumCols(sParse.pVdbe, 8);
11945 iFirst = 0;
11946 mx = 8;
11947 }
11948 for(i=iFirst; i<mx; i++){
11949 sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
11950 azColName[i], SQLITE_STATIC);
11951 }
11952 }
11953 #endif
11954
11955 if( db->init.busy==0 ){
11956 Vdbe *pVdbe = sParse.pVdbe;
11957 sqlite3VdbeSetSql(pVdbe, zSql, (int)(sParse.zTail-zSql), saveSqlFlag);
11958 }
11959 if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
11960 sqlite3VdbeFinalize(sParse.pVdbe);
11961 assert(!(*ppStmt));
11962 }else{
11963 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
11964 }
11965
11966 if( zErrMsg ){
11967 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
11968 sqlite3DbFree(db, zErrMsg);
11969 }else{
11970 sqlite3Error(db, rc);
11971 }
11972
11973 /* Delete any TriggerPrg structures allocated while parsing this statement. */
11974 while( sParse.pTriggerPrg ){
11975 TriggerPrg *pT = sParse.pTriggerPrg;
11976 sParse.pTriggerPrg = pT->pNext;
11977 sqlite3DbFree(db, pT);
11978 }
11979
11980 end_prepare:
11981
11982 sqlite3ParserReset(&sParse);
11983 rc = sqlite3ApiExit(db, rc);
11984 assert( (rc&db->errMask)==rc );
11985 return rc;
11986 }
11987 static int sqlite3LockAndPrepare(
11988 sqlite3 *db, /* Database handle. */
11989 const char *zSql, /* UTF-8 encoded SQL statement. */
11990 int nBytes, /* Length of zSql in bytes. */
11991 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
11992 Vdbe *pOld, /* VM being reprepared */
11993 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
11994 const char **pzTail /* OUT: End of parsed string */
11995 ){
11996 int rc;
11997
11998 #ifdef SQLITE_ENABLE_API_ARMOR
11999 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
12000 #endif
12001 *ppStmt = 0;
12002 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
12003 return SQLITE_MISUSE_BKPT;
12004 }
12005 sqlite3_mutex_enter(db->mutex);
12006 sqlite3BtreeEnterAll(db);
12007 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
12008 if( rc==SQLITE_SCHEMA ){
12009 sqlite3_finalize(*ppStmt);
12010 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
12011 }
12012 sqlite3BtreeLeaveAll(db);
12013 sqlite3_mutex_leave(db->mutex);
12014 assert( rc==SQLITE_OK || *ppStmt==0 );
12015 return rc;
12016 }
12017
12018 /*
12019 ** Rerun the compilation of a statement after a schema change.
12020 **
12021 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
12022 ** if the statement cannot be recompiled because another connection has
12023 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
12024 ** occurs, return SQLITE_SCHEMA.
12025 */
12026 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
12027 int rc;
12028 sqlite3_stmt *pNew;
12029 const char *zSql;
12030 sqlite3 *db;
12031
12032 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
12033 zSql = sqlite3_sql((sqlite3_stmt *)p);
12034 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
12035 db = sqlite3VdbeDb(p);
12036 assert( sqlite3_mutex_held(db->mutex) );
12037 rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
12038 if( rc ){
12039 if( rc==SQLITE_NOMEM ){
12040 sqlite3OomFault(db);
12041 }
12042 assert( pNew==0 );
12043 return rc;
12044 }else{
12045 assert( pNew!=0 );
12046 }
12047 sqlite3VdbeSwap((Vdbe*)pNew, p);
12048 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
12049 sqlite3VdbeResetStepResult((Vdbe*)pNew);
12050 sqlite3VdbeFinalize((Vdbe*)pNew);
12051 return SQLITE_OK;
12052 }
12053
12054
12055 /*
12056 ** Two versions of the official API. Legacy and new use. In the legacy
12057 ** version, the original SQL text is not saved in the prepared statement
12058 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
12059 ** sqlite3_step(). In the new version, the original SQL text is retained
12060 ** and the statement is automatically recompiled if an schema change
12061 ** occurs.
12062 */
12063 SQLITE_API int sqlite3_prepare(
12064 sqlite3 *db, /* Database handle. */
12065 const char *zSql, /* UTF-8 encoded SQL statement. */
12066 int nBytes, /* Length of zSql in bytes. */
12067 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
12068 const char **pzTail /* OUT: End of parsed string */
12069 ){
12070 int rc;
12071 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
12072 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
12073 return rc;
12074 }
12075 SQLITE_API int sqlite3_prepare_v2(
12076 sqlite3 *db, /* Database handle. */
12077 const char *zSql, /* UTF-8 encoded SQL statement. */
12078 int nBytes, /* Length of zSql in bytes. */
12079 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
12080 const char **pzTail /* OUT: End of parsed string */
12081 ){
12082 int rc;
12083 rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
12084 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
12085 return rc;
12086 }
12087
12088
12089 #ifndef SQLITE_OMIT_UTF16
12090 /*
12091 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
12092 */
12093 static int sqlite3Prepare16(
12094 sqlite3 *db, /* Database handle. */
12095 const void *zSql, /* UTF-16 encoded SQL statement. */
12096 int nBytes, /* Length of zSql in bytes. */
12097 int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
12098 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
12099 const void **pzTail /* OUT: End of parsed string */
12100 ){
12101 /* This function currently works by first transforming the UTF-16
12102 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
12103 ** tricky bit is figuring out the pointer to return in *pzTail.
12104 */
12105 char *zSql8;
12106 const char *zTail8 = 0;
12107 int rc = SQLITE_OK;
12108
12109 #ifdef SQLITE_ENABLE_API_ARMOR
12110 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
12111 #endif
12112 *ppStmt = 0;
12113 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
12114 return SQLITE_MISUSE_BKPT;
12115 }
12116 if( nBytes>=0 ){
12117 int sz;
12118 const char *z = (const char*)zSql;
12119 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
12120 nBytes = sz;
12121 }
12122 sqlite3_mutex_enter(db->mutex);
12123 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
12124 if( zSql8 ){
12125 rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
12126 }
12127
12128 if( zTail8 && pzTail ){
12129 /* If sqlite3_prepare returns a tail pointer, we calculate the
12130 ** equivalent pointer into the UTF-16 string by counting the unicode
12131 ** characters between zSql8 and zTail8, and then returning a pointer
12132 ** the same number of characters into the UTF-16 string.
12133 */
12134 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
12135 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
12136 }
12137 sqlite3DbFree(db, zSql8);
12138 rc = sqlite3ApiExit(db, rc);
12139 sqlite3_mutex_leave(db->mutex);
12140 return rc;
12141 }
12142
12143 /*
12144 ** Two versions of the official API. Legacy and new use. In the legacy
12145 ** version, the original SQL text is not saved in the prepared statement
12146 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
12147 ** sqlite3_step(). In the new version, the original SQL text is retained
12148 ** and the statement is automatically recompiled if an schema change
12149 ** occurs.
12150 */
12151 SQLITE_API int sqlite3_prepare16(
12152 sqlite3 *db, /* Database handle. */
12153 const void *zSql, /* UTF-16 encoded SQL statement. */
12154 int nBytes, /* Length of zSql in bytes. */
12155 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
12156 const void **pzTail /* OUT: End of parsed string */
12157 ){
12158 int rc;
12159 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
12160 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
12161 return rc;
12162 }
12163 SQLITE_API int sqlite3_prepare16_v2(
12164 sqlite3 *db, /* Database handle. */
12165 const void *zSql, /* UTF-16 encoded SQL statement. */
12166 int nBytes, /* Length of zSql in bytes. */
12167 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
12168 const void **pzTail /* OUT: End of parsed string */
12169 ){
12170 int rc;
12171 rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
12172 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
12173 return rc;
12174 }
12175
12176 #endif /* SQLITE_OMIT_UTF16 */
12177
12178 /************** End of prepare.c *********************************************/
12179 /************** Begin file select.c ******************************************/
12180 /*
12181 ** 2001 September 15
12182 **
12183 ** The author disclaims copyright to this source code. In place of
12184 ** a legal notice, here is a blessing:
12185 **
12186 ** May you do good and not evil.
12187 ** May you find forgiveness for yourself and forgive others.
12188 ** May you share freely, never taking more than you give.
12189 **
12190 *************************************************************************
12191 ** This file contains C code routines that are called by the parser
12192 ** to handle SELECT statements in SQLite.
12193 */
12194 /* #include "sqliteInt.h" */
12195
12196 /*
12197 ** Trace output macros
12198 */
12199 #if SELECTTRACE_ENABLED
12200 /***/ int sqlite3SelectTrace = 0;
12201 # define SELECTTRACE(K,P,S,X) \
12202 if(sqlite3SelectTrace&(K)) \
12203 sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
12204 (S)->zSelName,(S)),\
12205 sqlite3DebugPrintf X
12206 #else
12207 # define SELECTTRACE(K,P,S,X)
12208 #endif
12209
12210
12211 /*
12212 ** An instance of the following object is used to record information about
12213 ** how to process the DISTINCT keyword, to simplify passing that information
12214 ** into the selectInnerLoop() routine.
12215 */
12216 typedef struct DistinctCtx DistinctCtx;
12217 struct DistinctCtx {
12218 u8 isTnct; /* True if the DISTINCT keyword is present */
12219 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
12220 int tabTnct; /* Ephemeral table used for DISTINCT processing */
12221 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
12222 };
12223
12224 /*
12225 ** An instance of the following object is used to record information about
12226 ** the ORDER BY (or GROUP BY) clause of query is being coded.
12227 */
12228 typedef struct SortCtx SortCtx;
12229 struct SortCtx {
12230 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
12231 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
12232 int iECursor; /* Cursor number for the sorter */
12233 int regReturn; /* Register holding block-output return address */
12234 int labelBkOut; /* Start label for the block-output subroutine */
12235 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
12236 int labelDone; /* Jump here when done, ex: LIMIT reached */
12237 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
12238 u8 bOrderedInnerLoop; /* ORDER BY correctly sorts the inner loop */
12239 };
12240 #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
12241
12242 /*
12243 ** Delete all the content of a Select structure. Deallocate the structure
12244 ** itself only if bFree is true.
12245 */
12246 static void clearSelect(sqlite3 *db, Select *p, int bFree){
12247 while( p ){
12248 Select *pPrior = p->pPrior;
12249 sqlite3ExprListDelete(db, p->pEList);
12250 sqlite3SrcListDelete(db, p->pSrc);
12251 sqlite3ExprDelete(db, p->pWhere);
12252 sqlite3ExprListDelete(db, p->pGroupBy);
12253 sqlite3ExprDelete(db, p->pHaving);
12254 sqlite3ExprListDelete(db, p->pOrderBy);
12255 sqlite3ExprDelete(db, p->pLimit);
12256 sqlite3ExprDelete(db, p->pOffset);
12257 if( p->pWith ) sqlite3WithDelete(db, p->pWith);
12258 if( bFree ) sqlite3DbFree(db, p);
12259 p = pPrior;
12260 bFree = 1;
12261 }
12262 }
12263
12264 /*
12265 ** Initialize a SelectDest structure.
12266 */
12267 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iPar m){
12268 pDest->eDest = (u8)eDest;
12269 pDest->iSDParm = iParm;
12270 pDest->zAffSdst = 0;
12271 pDest->iSdst = 0;
12272 pDest->nSdst = 0;
12273 }
12274
12275
12276 /*
12277 ** Allocate a new Select structure and return a pointer to that
12278 ** structure.
12279 */
12280 SQLITE_PRIVATE Select *sqlite3SelectNew(
12281 Parse *pParse, /* Parsing context */
12282 ExprList *pEList, /* which columns to include in the result */
12283 SrcList *pSrc, /* the FROM clause -- which tables to scan */
12284 Expr *pWhere, /* the WHERE clause */
12285 ExprList *pGroupBy, /* the GROUP BY clause */
12286 Expr *pHaving, /* the HAVING clause */
12287 ExprList *pOrderBy, /* the ORDER BY clause */
12288 u32 selFlags, /* Flag parameters, such as SF_Distinct */
12289 Expr *pLimit, /* LIMIT value. NULL means not used */
12290 Expr *pOffset /* OFFSET value. NULL means no offset */
12291 ){
12292 Select *pNew;
12293 Select standin;
12294 sqlite3 *db = pParse->db;
12295 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
12296 if( pNew==0 ){
12297 assert( db->mallocFailed );
12298 pNew = &standin;
12299 }
12300 if( pEList==0 ){
12301 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ASTERISK,0));
12302 }
12303 pNew->pEList = pEList;
12304 pNew->op = TK_SELECT;
12305 pNew->selFlags = selFlags;
12306 pNew->iLimit = 0;
12307 pNew->iOffset = 0;
12308 #if SELECTTRACE_ENABLED
12309 pNew->zSelName[0] = 0;
12310 #endif
12311 pNew->addrOpenEphm[0] = -1;
12312 pNew->addrOpenEphm[1] = -1;
12313 pNew->nSelectRow = 0;
12314 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
12315 pNew->pSrc = pSrc;
12316 pNew->pWhere = pWhere;
12317 pNew->pGroupBy = pGroupBy;
12318 pNew->pHaving = pHaving;
12319 pNew->pOrderBy = pOrderBy;
12320 pNew->pPrior = 0;
12321 pNew->pNext = 0;
12322 pNew->pLimit = pLimit;
12323 pNew->pOffset = pOffset;
12324 pNew->pWith = 0;
12325 assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 );
12326 if( db->mallocFailed ) {
12327 clearSelect(db, pNew, pNew!=&standin);
12328 pNew = 0;
12329 }else{
12330 assert( pNew->pSrc!=0 || pParse->nErr>0 );
12331 }
12332 assert( pNew!=&standin );
12333 return pNew;
12334 }
12335
12336 #if SELECTTRACE_ENABLED
12337 /*
12338 ** Set the name of a Select object
12339 */
12340 SQLITE_PRIVATE void sqlite3SelectSetName(Select *p, const char *zName){
12341 if( p && zName ){
12342 sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName);
12343 }
12344 }
12345 #endif
12346
12347
12348 /*
12349 ** Delete the given Select structure and all of its substructures.
12350 */
12351 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
12352 if( p ) clearSelect(db, p, 1);
12353 }
12354
12355 /*
12356 ** Return a pointer to the right-most SELECT statement in a compound.
12357 */
12358 static Select *findRightmost(Select *p){
12359 while( p->pNext ) p = p->pNext;
12360 return p;
12361 }
12362
12363 /*
12364 ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
12365 ** type of join. Return an integer constant that expresses that type
12366 ** in terms of the following bit values:
12367 **
12368 ** JT_INNER
12369 ** JT_CROSS
12370 ** JT_OUTER
12371 ** JT_NATURAL
12372 ** JT_LEFT
12373 ** JT_RIGHT
12374 **
12375 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
12376 **
12377 ** If an illegal or unsupported join type is seen, then still return
12378 ** a join type, but put an error in the pParse structure.
12379 */
12380 SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *p C){
12381 int jointype = 0;
12382 Token *apAll[3];
12383 Token *p;
12384 /* 0123456789 123456789 123456789 123 */
12385 static const char zKeyText[] = "naturaleftouterightfullinnercross";
12386 static const struct {
12387 u8 i; /* Beginning of keyword text in zKeyText[] */
12388 u8 nChar; /* Length of the keyword in characters */
12389 u8 code; /* Join type mask */
12390 } aKeyword[] = {
12391 /* natural */ { 0, 7, JT_NATURAL },
12392 /* left */ { 6, 4, JT_LEFT|JT_OUTER },
12393 /* outer */ { 10, 5, JT_OUTER },
12394 /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
12395 /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
12396 /* inner */ { 23, 5, JT_INNER },
12397 /* cross */ { 28, 5, JT_INNER|JT_CROSS },
12398 };
12399 int i, j;
12400 apAll[0] = pA;
12401 apAll[1] = pB;
12402 apAll[2] = pC;
12403 for(i=0; i<3 && apAll[i]; i++){
12404 p = apAll[i];
12405 for(j=0; j<ArraySize(aKeyword); j++){
12406 if( p->n==aKeyword[j].nChar
12407 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
12408 jointype |= aKeyword[j].code;
12409 break;
12410 }
12411 }
12412 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
12413 if( j>=ArraySize(aKeyword) ){
12414 jointype |= JT_ERROR;
12415 break;
12416 }
12417 }
12418 if(
12419 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
12420 (jointype & JT_ERROR)!=0
12421 ){
12422 const char *zSp = " ";
12423 assert( pB!=0 );
12424 if( pC==0 ){ zSp++; }
12425 sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
12426 "%T %T%s%T", pA, pB, zSp, pC);
12427 jointype = JT_INNER;
12428 }else if( (jointype & JT_OUTER)!=0
12429 && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
12430 sqlite3ErrorMsg(pParse,
12431 "RIGHT and FULL OUTER JOINs are not currently supported");
12432 jointype = JT_INNER;
12433 }
12434 return jointype;
12435 }
12436
12437 /*
12438 ** Return the index of a column in a table. Return -1 if the column
12439 ** is not contained in the table.
12440 */
12441 static int columnIndex(Table *pTab, const char *zCol){
12442 int i;
12443 for(i=0; i<pTab->nCol; i++){
12444 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
12445 }
12446 return -1;
12447 }
12448
12449 /*
12450 ** Search the first N tables in pSrc, from left to right, looking for a
12451 ** table that has a column named zCol.
12452 **
12453 ** When found, set *piTab and *piCol to the table index and column index
12454 ** of the matching column and return TRUE.
12455 **
12456 ** If not found, return FALSE.
12457 */
12458 static int tableAndColumnIndex(
12459 SrcList *pSrc, /* Array of tables to search */
12460 int N, /* Number of tables in pSrc->a[] to search */
12461 const char *zCol, /* Name of the column we are looking for */
12462 int *piTab, /* Write index of pSrc->a[] here */
12463 int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
12464 ){
12465 int i; /* For looping over tables in pSrc */
12466 int iCol; /* Index of column matching zCol */
12467
12468 assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
12469 for(i=0; i<N; i++){
12470 iCol = columnIndex(pSrc->a[i].pTab, zCol);
12471 if( iCol>=0 ){
12472 if( piTab ){
12473 *piTab = i;
12474 *piCol = iCol;
12475 }
12476 return 1;
12477 }
12478 }
12479 return 0;
12480 }
12481
12482 /*
12483 ** This function is used to add terms implied by JOIN syntax to the
12484 ** WHERE clause expression of a SELECT statement. The new term, which
12485 ** is ANDed with the existing WHERE clause, is of the form:
12486 **
12487 ** (tab1.col1 = tab2.col2)
12488 **
12489 ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
12490 ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
12491 ** column iColRight of tab2.
12492 */
12493 static void addWhereTerm(
12494 Parse *pParse, /* Parsing context */
12495 SrcList *pSrc, /* List of tables in FROM clause */
12496 int iLeft, /* Index of first table to join in pSrc */
12497 int iColLeft, /* Index of column in first table */
12498 int iRight, /* Index of second table in pSrc */
12499 int iColRight, /* Index of column in second table */
12500 int isOuterJoin, /* True if this is an OUTER join */
12501 Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
12502 ){
12503 sqlite3 *db = pParse->db;
12504 Expr *pE1;
12505 Expr *pE2;
12506 Expr *pEq;
12507
12508 assert( iLeft<iRight );
12509 assert( pSrc->nSrc>iRight );
12510 assert( pSrc->a[iLeft].pTab );
12511 assert( pSrc->a[iRight].pTab );
12512
12513 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
12514 pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
12515
12516 pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
12517 if( pEq && isOuterJoin ){
12518 ExprSetProperty(pEq, EP_FromJoin);
12519 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
12520 ExprSetVVAProperty(pEq, EP_NoReduce);
12521 pEq->iRightJoinTable = (i16)pE2->iTable;
12522 }
12523 *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
12524 }
12525
12526 /*
12527 ** Set the EP_FromJoin property on all terms of the given expression.
12528 ** And set the Expr.iRightJoinTable to iTable for every term in the
12529 ** expression.
12530 **
12531 ** The EP_FromJoin property is used on terms of an expression to tell
12532 ** the LEFT OUTER JOIN processing logic that this term is part of the
12533 ** join restriction specified in the ON or USING clause and not a part
12534 ** of the more general WHERE clause. These terms are moved over to the
12535 ** WHERE clause during join processing but we need to remember that they
12536 ** originated in the ON or USING clause.
12537 **
12538 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
12539 ** expression depends on table iRightJoinTable even if that table is not
12540 ** explicitly mentioned in the expression. That information is needed
12541 ** for cases like this:
12542 **
12543 ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
12544 **
12545 ** The where clause needs to defer the handling of the t1.x=5
12546 ** term until after the t2 loop of the join. In that way, a
12547 ** NULL t2 row will be inserted whenever t1.x!=5. If we do not
12548 ** defer the handling of t1.x=5, it will be processed immediately
12549 ** after the t1 loop and rows with t1.x!=5 will never appear in
12550 ** the output, which is incorrect.
12551 */
12552 static void setJoinExpr(Expr *p, int iTable){
12553 while( p ){
12554 ExprSetProperty(p, EP_FromJoin);
12555 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
12556 ExprSetVVAProperty(p, EP_NoReduce);
12557 p->iRightJoinTable = (i16)iTable;
12558 if( p->op==TK_FUNCTION && p->x.pList ){
12559 int i;
12560 for(i=0; i<p->x.pList->nExpr; i++){
12561 setJoinExpr(p->x.pList->a[i].pExpr, iTable);
12562 }
12563 }
12564 setJoinExpr(p->pLeft, iTable);
12565 p = p->pRight;
12566 }
12567 }
12568
12569 /*
12570 ** This routine processes the join information for a SELECT statement.
12571 ** ON and USING clauses are converted into extra terms of the WHERE clause.
12572 ** NATURAL joins also create extra WHERE clause terms.
12573 **
12574 ** The terms of a FROM clause are contained in the Select.pSrc structure.
12575 ** The left most table is the first entry in Select.pSrc. The right-most
12576 ** table is the last entry. The join operator is held in the entry to
12577 ** the left. Thus entry 0 contains the join operator for the join between
12578 ** entries 0 and 1. Any ON or USING clauses associated with the join are
12579 ** also attached to the left entry.
12580 **
12581 ** This routine returns the number of errors encountered.
12582 */
12583 static int sqliteProcessJoin(Parse *pParse, Select *p){
12584 SrcList *pSrc; /* All tables in the FROM clause */
12585 int i, j; /* Loop counters */
12586 struct SrcList_item *pLeft; /* Left table being joined */
12587 struct SrcList_item *pRight; /* Right table being joined */
12588
12589 pSrc = p->pSrc;
12590 pLeft = &pSrc->a[0];
12591 pRight = &pLeft[1];
12592 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
12593 Table *pLeftTab = pLeft->pTab;
12594 Table *pRightTab = pRight->pTab;
12595 int isOuter;
12596
12597 if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
12598 isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
12599
12600 /* When the NATURAL keyword is present, add WHERE clause terms for
12601 ** every column that the two tables have in common.
12602 */
12603 if( pRight->fg.jointype & JT_NATURAL ){
12604 if( pRight->pOn || pRight->pUsing ){
12605 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
12606 "an ON or USING clause", 0);
12607 return 1;
12608 }
12609 for(j=0; j<pRightTab->nCol; j++){
12610 char *zName; /* Name of column in the right table */
12611 int iLeft; /* Matching left table */
12612 int iLeftCol; /* Matching column in the left table */
12613
12614 zName = pRightTab->aCol[j].zName;
12615 if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
12616 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
12617 isOuter, &p->pWhere);
12618 }
12619 }
12620 }
12621
12622 /* Disallow both ON and USING clauses in the same join
12623 */
12624 if( pRight->pOn && pRight->pUsing ){
12625 sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
12626 "clauses in the same join");
12627 return 1;
12628 }
12629
12630 /* Add the ON clause to the end of the WHERE clause, connected by
12631 ** an AND operator.
12632 */
12633 if( pRight->pOn ){
12634 if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
12635 p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
12636 pRight->pOn = 0;
12637 }
12638
12639 /* Create extra terms on the WHERE clause for each column named
12640 ** in the USING clause. Example: If the two tables to be joined are
12641 ** A and B and the USING clause names X, Y, and Z, then add this
12642 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
12643 ** Report an error if any column mentioned in the USING clause is
12644 ** not contained in both tables to be joined.
12645 */
12646 if( pRight->pUsing ){
12647 IdList *pList = pRight->pUsing;
12648 for(j=0; j<pList->nId; j++){
12649 char *zName; /* Name of the term in the USING clause */
12650 int iLeft; /* Table on the left with matching column name */
12651 int iLeftCol; /* Column number of matching column on the left */
12652 int iRightCol; /* Column number of matching column on the right */
12653
12654 zName = pList->a[j].zName;
12655 iRightCol = columnIndex(pRightTab, zName);
12656 if( iRightCol<0
12657 || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
12658 ){
12659 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
12660 "not present in both tables", zName);
12661 return 1;
12662 }
12663 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
12664 isOuter, &p->pWhere);
12665 }
12666 }
12667 }
12668 return 0;
12669 }
12670
12671 /* Forward reference */
12672 static KeyInfo *keyInfoFromExprList(
12673 Parse *pParse, /* Parsing context */
12674 ExprList *pList, /* Form the KeyInfo object from this ExprList */
12675 int iStart, /* Begin with this column of pList */
12676 int nExtra /* Add this many extra columns to the end */
12677 );
12678
12679 /*
12680 ** Generate code that will push the record in registers regData
12681 ** through regData+nData-1 onto the sorter.
12682 */
12683 static void pushOntoSorter(
12684 Parse *pParse, /* Parser context */
12685 SortCtx *pSort, /* Information about the ORDER BY clause */
12686 Select *pSelect, /* The whole SELECT statement */
12687 int regData, /* First register holding data to be sorted */
12688 int regOrigData, /* First register holding data before packing */
12689 int nData, /* Number of elements in the data array */
12690 int nPrefixReg /* No. of reg prior to regData available for use */
12691 ){
12692 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
12693 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
12694 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
12695 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
12696 int regBase; /* Regs for sorter record */
12697 int regRecord = ++pParse->nMem; /* Assembled sorter record */
12698 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
12699 int op; /* Opcode to add sorter record to sorter */
12700 int iLimit; /* LIMIT counter */
12701
12702 assert( bSeq==0 || bSeq==1 );
12703 assert( nData==1 || regData==regOrigData || regOrigData==0 );
12704 if( nPrefixReg ){
12705 assert( nPrefixReg==nExpr+bSeq );
12706 regBase = regData - nExpr - bSeq;
12707 }else{
12708 regBase = pParse->nMem + 1;
12709 pParse->nMem += nBase;
12710 }
12711 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
12712 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
12713 pSort->labelDone = sqlite3VdbeMakeLabel(v);
12714 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
12715 SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
12716 if( bSeq ){
12717 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
12718 }
12719 if( nPrefixReg==0 && nData>0 ){
12720 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
12721 }
12722 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
12723 if( nOBSat>0 ){
12724 int regPrevKey; /* The first nOBSat columns of the previous row */
12725 int addrFirst; /* Address of the OP_IfNot opcode */
12726 int addrJmp; /* Address of the OP_Jump opcode */
12727 VdbeOp *pOp; /* Opcode that opens the sorter */
12728 int nKey; /* Number of sorting key columns, including OP_Sequence */
12729 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
12730
12731 regPrevKey = pParse->nMem+1;
12732 pParse->nMem += pSort->nOBSat;
12733 nKey = nExpr - pSort->nOBSat + bSeq;
12734 if( bSeq ){
12735 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
12736 }else{
12737 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
12738 }
12739 VdbeCoverage(v);
12740 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
12741 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
12742 if( pParse->db->mallocFailed ) return;
12743 pOp->p2 = nKey + nData;
12744 pKI = pOp->p4.pKeyInfo;
12745 memset(pKI->aSortOrder, 0, pKI->nField); /* Makes OP_Jump below testable */
12746 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
12747 testcase( pKI->nXField>2 );
12748 pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat,
12749 pKI->nXField-1);
12750 addrJmp = sqlite3VdbeCurrentAddr(v);
12751 sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
12752 pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
12753 pSort->regReturn = ++pParse->nMem;
12754 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
12755 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
12756 if( iLimit ){
12757 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
12758 VdbeCoverage(v);
12759 }
12760 sqlite3VdbeJumpHere(v, addrFirst);
12761 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
12762 sqlite3VdbeJumpHere(v, addrJmp);
12763 }
12764 if( pSort->sortFlags & SORTFLAG_UseSorter ){
12765 op = OP_SorterInsert;
12766 }else{
12767 op = OP_IdxInsert;
12768 }
12769 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
12770 regBase+nOBSat, nBase-nOBSat);
12771 if( iLimit ){
12772 int addr;
12773 int r1 = 0;
12774 /* Fill the sorter until it contains LIMIT+OFFSET entries. (The iLimit
12775 ** register is initialized with value of LIMIT+OFFSET.) After the sorter
12776 ** fills up, delete the least entry in the sorter after each insert.
12777 ** Thus we never hold more than the LIMIT+OFFSET rows in memory at once */
12778 addr = sqlite3VdbeAddOp1(v, OP_IfNotZero, iLimit); VdbeCoverage(v);
12779 sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
12780 if( pSort->bOrderedInnerLoop ){
12781 r1 = ++pParse->nMem;
12782 sqlite3VdbeAddOp3(v, OP_Column, pSort->iECursor, nExpr, r1);
12783 VdbeComment((v, "seq"));
12784 }
12785 sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor);
12786 if( pSort->bOrderedInnerLoop ){
12787 /* If the inner loop is driven by an index such that values from
12788 ** the same iteration of the inner loop are in sorted order, then
12789 ** immediately jump to the next iteration of an inner loop if the
12790 ** entry from the current iteration does not fit into the top
12791 ** LIMIT+OFFSET entries of the sorter. */
12792 int iBrk = sqlite3VdbeCurrentAddr(v) + 2;
12793 sqlite3VdbeAddOp3(v, OP_Eq, regBase+nExpr, iBrk, r1);
12794 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
12795 VdbeCoverage(v);
12796 }
12797 sqlite3VdbeJumpHere(v, addr);
12798 }
12799 }
12800
12801 /*
12802 ** Add code to implement the OFFSET
12803 */
12804 static void codeOffset(
12805 Vdbe *v, /* Generate code into this VM */
12806 int iOffset, /* Register holding the offset counter */
12807 int iContinue /* Jump here to skip the current record */
12808 ){
12809 if( iOffset>0 ){
12810 sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);
12811 VdbeComment((v, "OFFSET"));
12812 }
12813 }
12814
12815 /*
12816 ** Add code that will check to make sure the N registers starting at iMem
12817 ** form a distinct entry. iTab is a sorting index that holds previously
12818 ** seen combinations of the N values. A new entry is made in iTab
12819 ** if the current N values are new.
12820 **
12821 ** A jump to addrRepeat is made and the N+1 values are popped from the
12822 ** stack if the top N elements are not distinct.
12823 */
12824 static void codeDistinct(
12825 Parse *pParse, /* Parsing and code generating context */
12826 int iTab, /* A sorting index used to test for distinctness */
12827 int addrRepeat, /* Jump to here if not distinct */
12828 int N, /* Number of elements */
12829 int iMem /* First element */
12830 ){
12831 Vdbe *v;
12832 int r1;
12833
12834 v = pParse->pVdbe;
12835 r1 = sqlite3GetTempReg(pParse);
12836 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
12837 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
12838 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
12839 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
12840 sqlite3ReleaseTempReg(pParse, r1);
12841 }
12842
12843 /*
12844 ** This routine generates the code for the inside of the inner loop
12845 ** of a SELECT.
12846 **
12847 ** If srcTab is negative, then the pEList expressions
12848 ** are evaluated in order to get the data for this row. If srcTab is
12849 ** zero or more, then data is pulled from srcTab and pEList is used only
12850 ** to get the number of columns and the collation sequence for each column.
12851 */
12852 static void selectInnerLoop(
12853 Parse *pParse, /* The parser context */
12854 Select *p, /* The complete select statement being coded */
12855 ExprList *pEList, /* List of values being extracted */
12856 int srcTab, /* Pull data from this table */
12857 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
12858 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
12859 SelectDest *pDest, /* How to dispose of the results */
12860 int iContinue, /* Jump here to continue with next row */
12861 int iBreak /* Jump here to break out of the inner loop */
12862 ){
12863 Vdbe *v = pParse->pVdbe;
12864 int i;
12865 int hasDistinct; /* True if the DISTINCT keyword is present */
12866 int eDest = pDest->eDest; /* How to dispose of results */
12867 int iParm = pDest->iSDParm; /* First argument to disposal method */
12868 int nResultCol; /* Number of result columns */
12869 int nPrefixReg = 0; /* Number of extra registers before regResult */
12870
12871 /* Usually, regResult is the first cell in an array of memory cells
12872 ** containing the current result row. In this case regOrig is set to the
12873 ** same value. However, if the results are being sent to the sorter, the
12874 ** values for any expressions that are also part of the sort-key are omitted
12875 ** from this array. In this case regOrig is set to zero. */
12876 int regResult; /* Start of memory holding current results */
12877 int regOrig; /* Start of memory holding full result (or 0) */
12878
12879 assert( v );
12880 assert( pEList!=0 );
12881 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
12882 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
12883 if( pSort==0 && !hasDistinct ){
12884 assert( iContinue!=0 );
12885 codeOffset(v, p->iOffset, iContinue);
12886 }
12887
12888 /* Pull the requested columns.
12889 */
12890 nResultCol = pEList->nExpr;
12891
12892 if( pDest->iSdst==0 ){
12893 if( pSort ){
12894 nPrefixReg = pSort->pOrderBy->nExpr;
12895 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
12896 pParse->nMem += nPrefixReg;
12897 }
12898 pDest->iSdst = pParse->nMem+1;
12899 pParse->nMem += nResultCol;
12900 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
12901 /* This is an error condition that can result, for example, when a SELECT
12902 ** on the right-hand side of an INSERT contains more result columns than
12903 ** there are columns in the table on the left. The error will be caught
12904 ** and reported later. But we need to make sure enough memory is allocated
12905 ** to avoid other spurious errors in the meantime. */
12906 pParse->nMem += nResultCol;
12907 }
12908 pDest->nSdst = nResultCol;
12909 regOrig = regResult = pDest->iSdst;
12910 if( srcTab>=0 ){
12911 for(i=0; i<nResultCol; i++){
12912 sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
12913 VdbeComment((v, "%s", pEList->a[i].zName));
12914 }
12915 }else if( eDest!=SRT_Exists ){
12916 /* If the destination is an EXISTS(...) expression, the actual
12917 ** values returned by the SELECT are not required.
12918 */
12919 u8 ecelFlags;
12920 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
12921 ecelFlags = SQLITE_ECEL_DUP;
12922 }else{
12923 ecelFlags = 0;
12924 }
12925 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
12926 /* For each expression in pEList that is a copy of an expression in
12927 ** the ORDER BY clause (pSort->pOrderBy), set the associated
12928 ** iOrderByCol value to one more than the index of the ORDER BY
12929 ** expression within the sort-key that pushOntoSorter() will generate.
12930 ** This allows the pEList field to be omitted from the sorted record,
12931 ** saving space and CPU cycles. */
12932 ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
12933 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
12934 int j;
12935 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
12936 pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
12937 }
12938 }
12939 regOrig = 0;
12940 assert( eDest==SRT_Set || eDest==SRT_Mem
12941 || eDest==SRT_Coroutine || eDest==SRT_Output );
12942 }
12943 nResultCol = sqlite3ExprCodeExprList(pParse,pEList,regResult,0,ecelFlags);
12944 }
12945
12946 /* If the DISTINCT keyword was present on the SELECT statement
12947 ** and this row has been seen before, then do not make this row
12948 ** part of the result.
12949 */
12950 if( hasDistinct ){
12951 switch( pDistinct->eTnctType ){
12952 case WHERE_DISTINCT_ORDERED: {
12953 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
12954 int iJump; /* Jump destination */
12955 int regPrev; /* Previous row content */
12956
12957 /* Allocate space for the previous row */
12958 regPrev = pParse->nMem+1;
12959 pParse->nMem += nResultCol;
12960
12961 /* Change the OP_OpenEphemeral coded earlier to an OP_Null
12962 ** sets the MEM_Cleared bit on the first register of the
12963 ** previous value. This will cause the OP_Ne below to always
12964 ** fail on the first iteration of the loop even if the first
12965 ** row is all NULLs.
12966 */
12967 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
12968 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
12969 pOp->opcode = OP_Null;
12970 pOp->p1 = 1;
12971 pOp->p2 = regPrev;
12972
12973 iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
12974 for(i=0; i<nResultCol; i++){
12975 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
12976 if( i<nResultCol-1 ){
12977 sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
12978 VdbeCoverage(v);
12979 }else{
12980 sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
12981 VdbeCoverage(v);
12982 }
12983 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
12984 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
12985 }
12986 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
12987 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
12988 break;
12989 }
12990
12991 case WHERE_DISTINCT_UNIQUE: {
12992 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
12993 break;
12994 }
12995
12996 default: {
12997 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
12998 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
12999 regResult);
13000 break;
13001 }
13002 }
13003 if( pSort==0 ){
13004 codeOffset(v, p->iOffset, iContinue);
13005 }
13006 }
13007
13008 switch( eDest ){
13009 /* In this mode, write each query result to the key of the temporary
13010 ** table iParm.
13011 */
13012 #ifndef SQLITE_OMIT_COMPOUND_SELECT
13013 case SRT_Union: {
13014 int r1;
13015 r1 = sqlite3GetTempReg(pParse);
13016 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
13017 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
13018 sqlite3ReleaseTempReg(pParse, r1);
13019 break;
13020 }
13021
13022 /* Construct a record from the query result, but instead of
13023 ** saving that record, use it as a key to delete elements from
13024 ** the temporary table iParm.
13025 */
13026 case SRT_Except: {
13027 sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
13028 break;
13029 }
13030 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
13031
13032 /* Store the result as data using a unique key.
13033 */
13034 case SRT_Fifo:
13035 case SRT_DistFifo:
13036 case SRT_Table:
13037 case SRT_EphemTab: {
13038 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
13039 testcase( eDest==SRT_Table );
13040 testcase( eDest==SRT_EphemTab );
13041 testcase( eDest==SRT_Fifo );
13042 testcase( eDest==SRT_DistFifo );
13043 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
13044 #ifndef SQLITE_OMIT_CTE
13045 if( eDest==SRT_DistFifo ){
13046 /* If the destination is DistFifo, then cursor (iParm+1) is open
13047 ** on an ephemeral index. If the current row is already present
13048 ** in the index, do not write it to the output. If not, add the
13049 ** current row to the index and proceed with writing it to the
13050 ** output table as well. */
13051 int addr = sqlite3VdbeCurrentAddr(v) + 4;
13052 sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
13053 VdbeCoverage(v);
13054 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
13055 assert( pSort==0 );
13056 }
13057 #endif
13058 if( pSort ){
13059 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg);
13060 }else{
13061 int r2 = sqlite3GetTempReg(pParse);
13062 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
13063 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
13064 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
13065 sqlite3ReleaseTempReg(pParse, r2);
13066 }
13067 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
13068 break;
13069 }
13070
13071 #ifndef SQLITE_OMIT_SUBQUERY
13072 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
13073 ** then there should be a single item on the stack. Write this
13074 ** item into the set table with bogus data.
13075 */
13076 case SRT_Set: {
13077 if( pSort ){
13078 /* At first glance you would think we could optimize out the
13079 ** ORDER BY in this case since the order of entries in the set
13080 ** does not matter. But there might be a LIMIT clause, in which
13081 ** case the order does matter */
13082 pushOntoSorter(
13083 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
13084 }else{
13085 int r1 = sqlite3GetTempReg(pParse);
13086 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
13087 sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol,
13088 r1, pDest->zAffSdst, nResultCol);
13089 sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
13090 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
13091 sqlite3ReleaseTempReg(pParse, r1);
13092 }
13093 break;
13094 }
13095
13096 /* If any row exist in the result set, record that fact and abort.
13097 */
13098 case SRT_Exists: {
13099 sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
13100 /* The LIMIT clause will terminate the loop for us */
13101 break;
13102 }
13103
13104 /* If this is a scalar select that is part of an expression, then
13105 ** store the results in the appropriate memory cell or array of
13106 ** memory cells and break out of the scan loop.
13107 */
13108 case SRT_Mem: {
13109 if( pSort ){
13110 assert( nResultCol<=pDest->nSdst );
13111 pushOntoSorter(
13112 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
13113 }else{
13114 assert( nResultCol==pDest->nSdst );
13115 assert( regResult==iParm );
13116 /* The LIMIT clause will jump out of the loop for us */
13117 }
13118 break;
13119 }
13120 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
13121
13122 case SRT_Coroutine: /* Send data to a co-routine */
13123 case SRT_Output: { /* Return the results */
13124 testcase( eDest==SRT_Coroutine );
13125 testcase( eDest==SRT_Output );
13126 if( pSort ){
13127 pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
13128 nPrefixReg);
13129 }else if( eDest==SRT_Coroutine ){
13130 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
13131 }else{
13132 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
13133 sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
13134 }
13135 break;
13136 }
13137
13138 #ifndef SQLITE_OMIT_CTE
13139 /* Write the results into a priority queue that is order according to
13140 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
13141 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
13142 ** pSO->nExpr columns, then make sure all keys are unique by adding a
13143 ** final OP_Sequence column. The last column is the record as a blob.
13144 */
13145 case SRT_DistQueue:
13146 case SRT_Queue: {
13147 int nKey;
13148 int r1, r2, r3;
13149 int addrTest = 0;
13150 ExprList *pSO;
13151 pSO = pDest->pOrderBy;
13152 assert( pSO );
13153 nKey = pSO->nExpr;
13154 r1 = sqlite3GetTempReg(pParse);
13155 r2 = sqlite3GetTempRange(pParse, nKey+2);
13156 r3 = r2+nKey+1;
13157 if( eDest==SRT_DistQueue ){
13158 /* If the destination is DistQueue, then cursor (iParm+1) is open
13159 ** on a second ephemeral index that holds all values every previously
13160 ** added to the queue. */
13161 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
13162 regResult, nResultCol);
13163 VdbeCoverage(v);
13164 }
13165 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
13166 if( eDest==SRT_DistQueue ){
13167 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
13168 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
13169 }
13170 for(i=0; i<nKey; i++){
13171 sqlite3VdbeAddOp2(v, OP_SCopy,
13172 regResult + pSO->a[i].u.x.iOrderByCol - 1,
13173 r2+i);
13174 }
13175 sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
13176 sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
13177 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
13178 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
13179 sqlite3ReleaseTempReg(pParse, r1);
13180 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
13181 break;
13182 }
13183 #endif /* SQLITE_OMIT_CTE */
13184
13185
13186
13187 #if !defined(SQLITE_OMIT_TRIGGER)
13188 /* Discard the results. This is used for SELECT statements inside
13189 ** the body of a TRIGGER. The purpose of such selects is to call
13190 ** user-defined functions that have side effects. We do not care
13191 ** about the actual results of the select.
13192 */
13193 default: {
13194 assert( eDest==SRT_Discard );
13195 break;
13196 }
13197 #endif
13198 }
13199
13200 /* Jump to the end of the loop if the LIMIT is reached. Except, if
13201 ** there is a sorter, in which case the sorter has already limited
13202 ** the output for us.
13203 */
13204 if( pSort==0 && p->iLimit ){
13205 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
13206 }
13207 }
13208
13209 /*
13210 ** Allocate a KeyInfo object sufficient for an index of N key columns and
13211 ** X extra columns.
13212 */
13213 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
13214 int nExtra = (N+X)*(sizeof(CollSeq*)+1);
13215 KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
13216 if( p ){
13217 p->aSortOrder = (u8*)&p->aColl[N+X];
13218 p->nField = (u16)N;
13219 p->nXField = (u16)X;
13220 p->enc = ENC(db);
13221 p->db = db;
13222 p->nRef = 1;
13223 memset(&p[1], 0, nExtra);
13224 }else{
13225 sqlite3OomFault(db);
13226 }
13227 return p;
13228 }
13229
13230 /*
13231 ** Deallocate a KeyInfo object
13232 */
13233 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
13234 if( p ){
13235 assert( p->nRef>0 );
13236 p->nRef--;
13237 if( p->nRef==0 ) sqlite3DbFree(p->db, p);
13238 }
13239 }
13240
13241 /*
13242 ** Make a new pointer to a KeyInfo object
13243 */
13244 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
13245 if( p ){
13246 assert( p->nRef>0 );
13247 p->nRef++;
13248 }
13249 return p;
13250 }
13251
13252 #ifdef SQLITE_DEBUG
13253 /*
13254 ** Return TRUE if a KeyInfo object can be change. The KeyInfo object
13255 ** can only be changed if this is just a single reference to the object.
13256 **
13257 ** This routine is used only inside of assert() statements.
13258 */
13259 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
13260 #endif /* SQLITE_DEBUG */
13261
13262 /*
13263 ** Given an expression list, generate a KeyInfo structure that records
13264 ** the collating sequence for each expression in that expression list.
13265 **
13266 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
13267 ** KeyInfo structure is appropriate for initializing a virtual index to
13268 ** implement that clause. If the ExprList is the result set of a SELECT
13269 ** then the KeyInfo structure is appropriate for initializing a virtual
13270 ** index to implement a DISTINCT test.
13271 **
13272 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
13273 ** function is responsible for seeing that this structure is eventually
13274 ** freed.
13275 */
13276 static KeyInfo *keyInfoFromExprList(
13277 Parse *pParse, /* Parsing context */
13278 ExprList *pList, /* Form the KeyInfo object from this ExprList */
13279 int iStart, /* Begin with this column of pList */
13280 int nExtra /* Add this many extra columns to the end */
13281 ){
13282 int nExpr;
13283 KeyInfo *pInfo;
13284 struct ExprList_item *pItem;
13285 sqlite3 *db = pParse->db;
13286 int i;
13287
13288 nExpr = pList->nExpr;
13289 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
13290 if( pInfo ){
13291 assert( sqlite3KeyInfoIsWriteable(pInfo) );
13292 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
13293 CollSeq *pColl;
13294 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
13295 if( !pColl ) pColl = db->pDfltColl;
13296 pInfo->aColl[i-iStart] = pColl;
13297 pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
13298 }
13299 }
13300 return pInfo;
13301 }
13302
13303 /*
13304 ** Name of the connection operator, used for error messages.
13305 */
13306 static const char *selectOpName(int id){
13307 char *z;
13308 switch( id ){
13309 case TK_ALL: z = "UNION ALL"; break;
13310 case TK_INTERSECT: z = "INTERSECT"; break;
13311 case TK_EXCEPT: z = "EXCEPT"; break;
13312 default: z = "UNION"; break;
13313 }
13314 return z;
13315 }
13316
13317 #ifndef SQLITE_OMIT_EXPLAIN
13318 /*
13319 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
13320 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
13321 ** where the caption is of the form:
13322 **
13323 ** "USE TEMP B-TREE FOR xxx"
13324 **
13325 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
13326 ** is determined by the zUsage argument.
13327 */
13328 static void explainTempTable(Parse *pParse, const char *zUsage){
13329 if( pParse->explain==2 ){
13330 Vdbe *v = pParse->pVdbe;
13331 char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
13332 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
13333 }
13334 }
13335
13336 /*
13337 ** Assign expression b to lvalue a. A second, no-op, version of this macro
13338 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
13339 ** in sqlite3Select() to assign values to structure member variables that
13340 ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
13341 ** code with #ifndef directives.
13342 */
13343 # define explainSetInteger(a, b) a = b
13344
13345 #else
13346 /* No-op versions of the explainXXX() functions and macros. */
13347 # define explainTempTable(y,z)
13348 # define explainSetInteger(y,z)
13349 #endif
13350
13351 #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
13352 /*
13353 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
13354 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
13355 ** where the caption is of one of the two forms:
13356 **
13357 ** "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
13358 ** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
13359 **
13360 ** where iSub1 and iSub2 are the integers passed as the corresponding
13361 ** function parameters, and op is the text representation of the parameter
13362 ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
13363 ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
13364 ** false, or the second form if it is true.
13365 */
13366 static void explainComposite(
13367 Parse *pParse, /* Parse context */
13368 int op, /* One of TK_UNION, TK_EXCEPT etc. */
13369 int iSub1, /* Subquery id 1 */
13370 int iSub2, /* Subquery id 2 */
13371 int bUseTmp /* True if a temp table was used */
13372 ){
13373 assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
13374 if( pParse->explain==2 ){
13375 Vdbe *v = pParse->pVdbe;
13376 char *zMsg = sqlite3MPrintf(
13377 pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
13378 bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
13379 );
13380 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
13381 }
13382 }
13383 #else
13384 /* No-op versions of the explainXXX() functions and macros. */
13385 # define explainComposite(v,w,x,y,z)
13386 #endif
13387
13388 /*
13389 ** If the inner loop was generated using a non-null pOrderBy argument,
13390 ** then the results were placed in a sorter. After the loop is terminated
13391 ** we need to run the sorter and output the results. The following
13392 ** routine generates the code needed to do that.
13393 */
13394 static void generateSortTail(
13395 Parse *pParse, /* Parsing context */
13396 Select *p, /* The SELECT statement */
13397 SortCtx *pSort, /* Information on the ORDER BY clause */
13398 int nColumn, /* Number of columns of data */
13399 SelectDest *pDest /* Write the sorted results here */
13400 ){
13401 Vdbe *v = pParse->pVdbe; /* The prepared statement */
13402 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
13403 int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */
13404 int addr;
13405 int addrOnce = 0;
13406 int iTab;
13407 ExprList *pOrderBy = pSort->pOrderBy;
13408 int eDest = pDest->eDest;
13409 int iParm = pDest->iSDParm;
13410 int regRow;
13411 int regRowid;
13412 int iCol;
13413 int nKey;
13414 int iSortTab; /* Sorter cursor to read from */
13415 int nSortData; /* Trailing values to read from sorter */
13416 int i;
13417 int bSeq; /* True if sorter record includes seq. no. */
13418 struct ExprList_item *aOutEx = p->pEList->a;
13419
13420 assert( addrBreak<0 );
13421 if( pSort->labelBkOut ){
13422 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
13423 sqlite3VdbeGoto(v, addrBreak);
13424 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
13425 }
13426 iTab = pSort->iECursor;
13427 if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
13428 regRowid = 0;
13429 regRow = pDest->iSdst;
13430 nSortData = nColumn;
13431 }else{
13432 regRowid = sqlite3GetTempReg(pParse);
13433 regRow = sqlite3GetTempRange(pParse, nColumn);
13434 nSortData = nColumn;
13435 }
13436 nKey = pOrderBy->nExpr - pSort->nOBSat;
13437 if( pSort->sortFlags & SORTFLAG_UseSorter ){
13438 int regSortOut = ++pParse->nMem;
13439 iSortTab = pParse->nTab++;
13440 if( pSort->labelBkOut ){
13441 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
13442 }
13443 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData);
13444 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
13445 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
13446 VdbeCoverage(v);
13447 codeOffset(v, p->iOffset, addrContinue);
13448 sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
13449 bSeq = 0;
13450 }else{
13451 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
13452 codeOffset(v, p->iOffset, addrContinue);
13453 iSortTab = iTab;
13454 bSeq = 1;
13455 }
13456 for(i=0, iCol=nKey+bSeq; i<nSortData; i++){
13457 int iRead;
13458 if( aOutEx[i].u.x.iOrderByCol ){
13459 iRead = aOutEx[i].u.x.iOrderByCol-1;
13460 }else{
13461 iRead = iCol++;
13462 }
13463 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
13464 VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
13465 }
13466 switch( eDest ){
13467 case SRT_Table:
13468 case SRT_EphemTab: {
13469 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
13470 sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
13471 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
13472 break;
13473 }
13474 #ifndef SQLITE_OMIT_SUBQUERY
13475 case SRT_Set: {
13476 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
13477 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
13478 pDest->zAffSdst, nColumn);
13479 sqlite3ExprCacheAffinityChange(pParse, regRow, nColumn);
13480 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
13481 break;
13482 }
13483 case SRT_Mem: {
13484 /* The LIMIT clause will terminate the loop for us */
13485 break;
13486 }
13487 #endif
13488 default: {
13489 assert( eDest==SRT_Output || eDest==SRT_Coroutine );
13490 testcase( eDest==SRT_Output );
13491 testcase( eDest==SRT_Coroutine );
13492 if( eDest==SRT_Output ){
13493 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
13494 sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
13495 }else{
13496 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
13497 }
13498 break;
13499 }
13500 }
13501 if( regRowid ){
13502 if( eDest==SRT_Set ){
13503 sqlite3ReleaseTempRange(pParse, regRow, nColumn);
13504 }else{
13505 sqlite3ReleaseTempReg(pParse, regRow);
13506 }
13507 sqlite3ReleaseTempReg(pParse, regRowid);
13508 }
13509 /* The bottom of the loop
13510 */
13511 sqlite3VdbeResolveLabel(v, addrContinue);
13512 if( pSort->sortFlags & SORTFLAG_UseSorter ){
13513 sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
13514 }else{
13515 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
13516 }
13517 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
13518 sqlite3VdbeResolveLabel(v, addrBreak);
13519 }
13520
13521 /*
13522 ** Return a pointer to a string containing the 'declaration type' of the
13523 ** expression pExpr. The string may be treated as static by the caller.
13524 **
13525 ** Also try to estimate the size of the returned value and return that
13526 ** result in *pEstWidth.
13527 **
13528 ** The declaration type is the exact datatype definition extracted from the
13529 ** original CREATE TABLE statement if the expression is a column. The
13530 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
13531 ** is considered a column can be complex in the presence of subqueries. The
13532 ** result-set expression in all of the following SELECT statements is
13533 ** considered a column by this function.
13534 **
13535 ** SELECT col FROM tbl;
13536 ** SELECT (SELECT col FROM tbl;
13537 ** SELECT (SELECT col FROM tbl);
13538 ** SELECT abc FROM (SELECT col AS abc FROM tbl);
13539 **
13540 ** The declaration type for any expression other than a column is NULL.
13541 **
13542 ** This routine has either 3 or 6 parameters depending on whether or not
13543 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
13544 */
13545 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13546 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
13547 #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
13548 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
13549 #endif
13550 static const char *columnTypeImpl(
13551 NameContext *pNC,
13552 Expr *pExpr,
13553 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13554 const char **pzOrigDb,
13555 const char **pzOrigTab,
13556 const char **pzOrigCol,
13557 #endif
13558 u8 *pEstWidth
13559 ){
13560 char const *zType = 0;
13561 int j;
13562 u8 estWidth = 1;
13563 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13564 char const *zOrigDb = 0;
13565 char const *zOrigTab = 0;
13566 char const *zOrigCol = 0;
13567 #endif
13568
13569 assert( pExpr!=0 );
13570 assert( pNC->pSrcList!=0 );
13571 switch( pExpr->op ){
13572 case TK_AGG_COLUMN:
13573 case TK_COLUMN: {
13574 /* The expression is a column. Locate the table the column is being
13575 ** extracted from in NameContext.pSrcList. This table may be real
13576 ** database table or a subquery.
13577 */
13578 Table *pTab = 0; /* Table structure column is extracted from */
13579 Select *pS = 0; /* Select the column is extracted from */
13580 int iCol = pExpr->iColumn; /* Index of column in pTab */
13581 testcase( pExpr->op==TK_AGG_COLUMN );
13582 testcase( pExpr->op==TK_COLUMN );
13583 while( pNC && !pTab ){
13584 SrcList *pTabList = pNC->pSrcList;
13585 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
13586 if( j<pTabList->nSrc ){
13587 pTab = pTabList->a[j].pTab;
13588 pS = pTabList->a[j].pSelect;
13589 }else{
13590 pNC = pNC->pNext;
13591 }
13592 }
13593
13594 if( pTab==0 ){
13595 /* At one time, code such as "SELECT new.x" within a trigger would
13596 ** cause this condition to run. Since then, we have restructured how
13597 ** trigger code is generated and so this condition is no longer
13598 ** possible. However, it can still be true for statements like
13599 ** the following:
13600 **
13601 ** CREATE TABLE t1(col INTEGER);
13602 ** SELECT (SELECT t1.col) FROM FROM t1;
13603 **
13604 ** when columnType() is called on the expression "t1.col" in the
13605 ** sub-select. In this case, set the column type to NULL, even
13606 ** though it should really be "INTEGER".
13607 **
13608 ** This is not a problem, as the column type of "t1.col" is never
13609 ** used. When columnType() is called on the expression
13610 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
13611 ** branch below. */
13612 break;
13613 }
13614
13615 assert( pTab && pExpr->pTab==pTab );
13616 if( pS ){
13617 /* The "table" is actually a sub-select or a view in the FROM clause
13618 ** of the SELECT statement. Return the declaration type and origin
13619 ** data for the result-set column of the sub-select.
13620 */
13621 if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
13622 /* If iCol is less than zero, then the expression requests the
13623 ** rowid of the sub-select or view. This expression is legal (see
13624 ** test case misc2.2.2) - it always evaluates to NULL.
13625 **
13626 ** The ALWAYS() is because iCol>=pS->pEList->nExpr will have been
13627 ** caught already by name resolution.
13628 */
13629 NameContext sNC;
13630 Expr *p = pS->pEList->a[iCol].pExpr;
13631 sNC.pSrcList = pS->pSrc;
13632 sNC.pNext = pNC;
13633 sNC.pParse = pNC->pParse;
13634 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth);
13635 }
13636 }else if( pTab->pSchema ){
13637 /* A real table */
13638 assert( !pS );
13639 if( iCol<0 ) iCol = pTab->iPKey;
13640 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
13641 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13642 if( iCol<0 ){
13643 zType = "INTEGER";
13644 zOrigCol = "rowid";
13645 }else{
13646 zOrigCol = pTab->aCol[iCol].zName;
13647 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
13648 estWidth = pTab->aCol[iCol].szEst;
13649 }
13650 zOrigTab = pTab->zName;
13651 if( pNC->pParse ){
13652 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
13653 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
13654 }
13655 #else
13656 if( iCol<0 ){
13657 zType = "INTEGER";
13658 }else{
13659 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
13660 estWidth = pTab->aCol[iCol].szEst;
13661 }
13662 #endif
13663 }
13664 break;
13665 }
13666 #ifndef SQLITE_OMIT_SUBQUERY
13667 case TK_SELECT: {
13668 /* The expression is a sub-select. Return the declaration type and
13669 ** origin info for the single column in the result set of the SELECT
13670 ** statement.
13671 */
13672 NameContext sNC;
13673 Select *pS = pExpr->x.pSelect;
13674 Expr *p = pS->pEList->a[0].pExpr;
13675 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
13676 sNC.pSrcList = pS->pSrc;
13677 sNC.pNext = pNC;
13678 sNC.pParse = pNC->pParse;
13679 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth);
13680 break;
13681 }
13682 #endif
13683 }
13684
13685 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13686 if( pzOrigDb ){
13687 assert( pzOrigTab && pzOrigCol );
13688 *pzOrigDb = zOrigDb;
13689 *pzOrigTab = zOrigTab;
13690 *pzOrigCol = zOrigCol;
13691 }
13692 #endif
13693 if( pEstWidth ) *pEstWidth = estWidth;
13694 return zType;
13695 }
13696
13697 /*
13698 ** Generate code that will tell the VDBE the declaration types of columns
13699 ** in the result set.
13700 */
13701 static void generateColumnTypes(
13702 Parse *pParse, /* Parser context */
13703 SrcList *pTabList, /* List of tables */
13704 ExprList *pEList /* Expressions defining the result set */
13705 ){
13706 #ifndef SQLITE_OMIT_DECLTYPE
13707 Vdbe *v = pParse->pVdbe;
13708 int i;
13709 NameContext sNC;
13710 sNC.pSrcList = pTabList;
13711 sNC.pParse = pParse;
13712 for(i=0; i<pEList->nExpr; i++){
13713 Expr *p = pEList->a[i].pExpr;
13714 const char *zType;
13715 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13716 const char *zOrigDb = 0;
13717 const char *zOrigTab = 0;
13718 const char *zOrigCol = 0;
13719 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
13720
13721 /* The vdbe must make its own copy of the column-type and other
13722 ** column specific strings, in case the schema is reset before this
13723 ** virtual machine is deleted.
13724 */
13725 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
13726 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
13727 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
13728 #else
13729 zType = columnType(&sNC, p, 0, 0, 0, 0);
13730 #endif
13731 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
13732 }
13733 #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
13734 }
13735
13736 /*
13737 ** Generate code that will tell the VDBE the names of columns
13738 ** in the result set. This information is used to provide the
13739 ** azCol[] values in the callback.
13740 */
13741 static void generateColumnNames(
13742 Parse *pParse, /* Parser context */
13743 SrcList *pTabList, /* List of tables */
13744 ExprList *pEList /* Expressions defining the result set */
13745 ){
13746 Vdbe *v = pParse->pVdbe;
13747 int i, j;
13748 sqlite3 *db = pParse->db;
13749 int fullNames, shortNames;
13750
13751 #ifndef SQLITE_OMIT_EXPLAIN
13752 /* If this is an EXPLAIN, skip this step */
13753 if( pParse->explain ){
13754 return;
13755 }
13756 #endif
13757
13758 if( pParse->colNamesSet || db->mallocFailed ) return;
13759 assert( v!=0 );
13760 assert( pTabList!=0 );
13761 pParse->colNamesSet = 1;
13762 fullNames = (db->flags & SQLITE_FullColNames)!=0;
13763 shortNames = (db->flags & SQLITE_ShortColNames)!=0;
13764 sqlite3VdbeSetNumCols(v, pEList->nExpr);
13765 for(i=0; i<pEList->nExpr; i++){
13766 Expr *p;
13767 p = pEList->a[i].pExpr;
13768 if( NEVER(p==0) ) continue;
13769 if( pEList->a[i].zName ){
13770 char *zName = pEList->a[i].zName;
13771 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
13772 }else if( p->op==TK_COLUMN || p->op==TK_AGG_COLUMN ){
13773 Table *pTab;
13774 char *zCol;
13775 int iCol = p->iColumn;
13776 for(j=0; ALWAYS(j<pTabList->nSrc); j++){
13777 if( pTabList->a[j].iCursor==p->iTable ) break;
13778 }
13779 assert( j<pTabList->nSrc );
13780 pTab = pTabList->a[j].pTab;
13781 if( iCol<0 ) iCol = pTab->iPKey;
13782 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
13783 if( iCol<0 ){
13784 zCol = "rowid";
13785 }else{
13786 zCol = pTab->aCol[iCol].zName;
13787 }
13788 if( !shortNames && !fullNames ){
13789 sqlite3VdbeSetColName(v, i, COLNAME_NAME,
13790 sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
13791 }else if( fullNames ){
13792 char *zName = 0;
13793 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
13794 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
13795 }else{
13796 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
13797 }
13798 }else{
13799 const char *z = pEList->a[i].zSpan;
13800 z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
13801 sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
13802 }
13803 }
13804 generateColumnTypes(pParse, pTabList, pEList);
13805 }
13806
13807 /*
13808 ** Given an expression list (which is really the list of expressions
13809 ** that form the result set of a SELECT statement) compute appropriate
13810 ** column names for a table that would hold the expression list.
13811 **
13812 ** All column names will be unique.
13813 **
13814 ** Only the column names are computed. Column.zType, Column.zColl,
13815 ** and other fields of Column are zeroed.
13816 **
13817 ** Return SQLITE_OK on success. If a memory allocation error occurs,
13818 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
13819 */
13820 SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
13821 Parse *pParse, /* Parsing context */
13822 ExprList *pEList, /* Expr list from which to derive column names */
13823 i16 *pnCol, /* Write the number of columns here */
13824 Column **paCol /* Write the new column list here */
13825 ){
13826 sqlite3 *db = pParse->db; /* Database connection */
13827 int i, j; /* Loop counters */
13828 u32 cnt; /* Index added to make the name unique */
13829 Column *aCol, *pCol; /* For looping over result columns */
13830 int nCol; /* Number of columns in the result set */
13831 Expr *p; /* Expression for a single result column */
13832 char *zName; /* Column name */
13833 int nName; /* Size of name in zName[] */
13834 Hash ht; /* Hash table of column names */
13835
13836 sqlite3HashInit(&ht);
13837 if( pEList ){
13838 nCol = pEList->nExpr;
13839 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
13840 testcase( aCol==0 );
13841 }else{
13842 nCol = 0;
13843 aCol = 0;
13844 }
13845 assert( nCol==(i16)nCol );
13846 *pnCol = nCol;
13847 *paCol = aCol;
13848
13849 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
13850 /* Get an appropriate name for the column
13851 */
13852 p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
13853 if( (zName = pEList->a[i].zName)!=0 ){
13854 /* If the column contains an "AS <name>" phrase, use <name> as the name */
13855 }else{
13856 Expr *pColExpr = p; /* The expression that is the result column name */
13857 Table *pTab; /* Table associated with this expression */
13858 while( pColExpr->op==TK_DOT ){
13859 pColExpr = pColExpr->pRight;
13860 assert( pColExpr!=0 );
13861 }
13862 if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
13863 /* For columns use the column name name */
13864 int iCol = pColExpr->iColumn;
13865 pTab = pColExpr->pTab;
13866 if( iCol<0 ) iCol = pTab->iPKey;
13867 zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
13868 }else if( pColExpr->op==TK_ID ){
13869 assert( !ExprHasProperty(pColExpr, EP_IntValue) );
13870 zName = pColExpr->u.zToken;
13871 }else{
13872 /* Use the original text of the column expression as its name */
13873 zName = pEList->a[i].zSpan;
13874 }
13875 }
13876 zName = sqlite3MPrintf(db, "%s", zName);
13877
13878 /* Make sure the column name is unique. If the name is not unique,
13879 ** append an integer to the name so that it becomes unique.
13880 */
13881 cnt = 0;
13882 while( zName && sqlite3HashFind(&ht, zName)!=0 ){
13883 nName = sqlite3Strlen30(zName);
13884 if( nName>0 ){
13885 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
13886 if( zName[j]==':' ) nName = j;
13887 }
13888 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
13889 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
13890 }
13891 pCol->zName = zName;
13892 sqlite3ColumnPropertiesFromName(0, pCol);
13893 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
13894 sqlite3OomFault(db);
13895 }
13896 }
13897 sqlite3HashClear(&ht);
13898 if( db->mallocFailed ){
13899 for(j=0; j<i; j++){
13900 sqlite3DbFree(db, aCol[j].zName);
13901 }
13902 sqlite3DbFree(db, aCol);
13903 *paCol = 0;
13904 *pnCol = 0;
13905 return SQLITE_NOMEM_BKPT;
13906 }
13907 return SQLITE_OK;
13908 }
13909
13910 /*
13911 ** Add type and collation information to a column list based on
13912 ** a SELECT statement.
13913 **
13914 ** The column list presumably came from selectColumnNamesFromExprList().
13915 ** The column list has only names, not types or collations. This
13916 ** routine goes through and adds the types and collations.
13917 **
13918 ** This routine requires that all identifiers in the SELECT
13919 ** statement be resolved.
13920 */
13921 SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(
13922 Parse *pParse, /* Parsing contexts */
13923 Table *pTab, /* Add column type information to this table */
13924 Select *pSelect /* SELECT used to determine types and collations */
13925 ){
13926 sqlite3 *db = pParse->db;
13927 NameContext sNC;
13928 Column *pCol;
13929 CollSeq *pColl;
13930 int i;
13931 Expr *p;
13932 struct ExprList_item *a;
13933 u64 szAll = 0;
13934
13935 assert( pSelect!=0 );
13936 assert( (pSelect->selFlags & SF_Resolved)!=0 );
13937 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
13938 if( db->mallocFailed ) return;
13939 memset(&sNC, 0, sizeof(sNC));
13940 sNC.pSrcList = pSelect->pSrc;
13941 a = pSelect->pEList->a;
13942 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
13943 const char *zType;
13944 int n, m;
13945 p = a[i].pExpr;
13946 zType = columnType(&sNC, p, 0, 0, 0, &pCol->szEst);
13947 szAll += pCol->szEst;
13948 pCol->affinity = sqlite3ExprAffinity(p);
13949 if( zType && (m = sqlite3Strlen30(zType))>0 ){
13950 n = sqlite3Strlen30(pCol->zName);
13951 pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
13952 if( pCol->zName ){
13953 memcpy(&pCol->zName[n+1], zType, m+1);
13954 pCol->colFlags |= COLFLAG_HASTYPE;
13955 }
13956 }
13957 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
13958 pColl = sqlite3ExprCollSeq(pParse, p);
13959 if( pColl && pCol->zColl==0 ){
13960 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
13961 }
13962 }
13963 pTab->szTabRow = sqlite3LogEst(szAll*4);
13964 }
13965
13966 /*
13967 ** Given a SELECT statement, generate a Table structure that describes
13968 ** the result set of that SELECT.
13969 */
13970 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
13971 Table *pTab;
13972 sqlite3 *db = pParse->db;
13973 int savedFlags;
13974
13975 savedFlags = db->flags;
13976 db->flags &= ~SQLITE_FullColNames;
13977 db->flags |= SQLITE_ShortColNames;
13978 sqlite3SelectPrep(pParse, pSelect, 0);
13979 if( pParse->nErr ) return 0;
13980 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
13981 db->flags = savedFlags;
13982 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
13983 if( pTab==0 ){
13984 return 0;
13985 }
13986 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
13987 ** is disabled */
13988 assert( db->lookaside.bDisable );
13989 pTab->nTabRef = 1;
13990 pTab->zName = 0;
13991 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
13992 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
13993 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect);
13994 pTab->iPKey = -1;
13995 if( db->mallocFailed ){
13996 sqlite3DeleteTable(db, pTab);
13997 return 0;
13998 }
13999 return pTab;
14000 }
14001
14002 /*
14003 ** Get a VDBE for the given parser context. Create a new one if necessary.
14004 ** If an error occurs, return NULL and leave a message in pParse.
14005 */
14006 static SQLITE_NOINLINE Vdbe *allocVdbe(Parse *pParse){
14007 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
14008 if( v ) sqlite3VdbeAddOp2(v, OP_Init, 0, 1);
14009 if( pParse->pToplevel==0
14010 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
14011 ){
14012 pParse->okConstFactor = 1;
14013 }
14014 return v;
14015 }
14016 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
14017 Vdbe *v = pParse->pVdbe;
14018 return v ? v : allocVdbe(pParse);
14019 }
14020
14021
14022 /*
14023 ** Compute the iLimit and iOffset fields of the SELECT based on the
14024 ** pLimit and pOffset expressions. pLimit and pOffset hold the expressions
14025 ** that appear in the original SQL statement after the LIMIT and OFFSET
14026 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
14027 ** are the integer memory register numbers for counters used to compute
14028 ** the limit and offset. If there is no limit and/or offset, then
14029 ** iLimit and iOffset are negative.
14030 **
14031 ** This routine changes the values of iLimit and iOffset only if
14032 ** a limit or offset is defined by pLimit and pOffset. iLimit and
14033 ** iOffset should have been preset to appropriate default values (zero)
14034 ** prior to calling this routine.
14035 **
14036 ** The iOffset register (if it exists) is initialized to the value
14037 ** of the OFFSET. The iLimit register is initialized to LIMIT. Register
14038 ** iOffset+1 is initialized to LIMIT+OFFSET.
14039 **
14040 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
14041 ** redefined. The UNION ALL operator uses this property to force
14042 ** the reuse of the same limit and offset registers across multiple
14043 ** SELECT statements.
14044 */
14045 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
14046 Vdbe *v = 0;
14047 int iLimit = 0;
14048 int iOffset;
14049 int n;
14050 if( p->iLimit ) return;
14051
14052 /*
14053 ** "LIMIT -1" always shows all rows. There is some
14054 ** controversy about what the correct behavior should be.
14055 ** The current implementation interprets "LIMIT 0" to mean
14056 ** no rows.
14057 */
14058 sqlite3ExprCacheClear(pParse);
14059 assert( p->pOffset==0 || p->pLimit!=0 );
14060 if( p->pLimit ){
14061 p->iLimit = iLimit = ++pParse->nMem;
14062 v = sqlite3GetVdbe(pParse);
14063 assert( v!=0 );
14064 if( sqlite3ExprIsInteger(p->pLimit, &n) ){
14065 sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
14066 VdbeComment((v, "LIMIT counter"));
14067 if( n==0 ){
14068 sqlite3VdbeGoto(v, iBreak);
14069 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
14070 p->nSelectRow = sqlite3LogEst((u64)n);
14071 p->selFlags |= SF_FixedLimit;
14072 }
14073 }else{
14074 sqlite3ExprCode(pParse, p->pLimit, iLimit);
14075 sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
14076 VdbeComment((v, "LIMIT counter"));
14077 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
14078 }
14079 if( p->pOffset ){
14080 p->iOffset = iOffset = ++pParse->nMem;
14081 pParse->nMem++; /* Allocate an extra register for limit+offset */
14082 sqlite3ExprCode(pParse, p->pOffset, iOffset);
14083 sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
14084 VdbeComment((v, "OFFSET counter"));
14085 sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
14086 VdbeComment((v, "LIMIT+OFFSET"));
14087 }
14088 }
14089 }
14090
14091 #ifndef SQLITE_OMIT_COMPOUND_SELECT
14092 /*
14093 ** Return the appropriate collating sequence for the iCol-th column of
14094 ** the result set for the compound-select statement "p". Return NULL if
14095 ** the column has no default collating sequence.
14096 **
14097 ** The collating sequence for the compound select is taken from the
14098 ** left-most term of the select that has a collating sequence.
14099 */
14100 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
14101 CollSeq *pRet;
14102 if( p->pPrior ){
14103 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
14104 }else{
14105 pRet = 0;
14106 }
14107 assert( iCol>=0 );
14108 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
14109 ** have been thrown during name resolution and we would not have gotten
14110 ** this far */
14111 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
14112 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
14113 }
14114 return pRet;
14115 }
14116
14117 /*
14118 ** The select statement passed as the second parameter is a compound SELECT
14119 ** with an ORDER BY clause. This function allocates and returns a KeyInfo
14120 ** structure suitable for implementing the ORDER BY.
14121 **
14122 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
14123 ** function is responsible for ensuring that this structure is eventually
14124 ** freed.
14125 */
14126 static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
14127 ExprList *pOrderBy = p->pOrderBy;
14128 int nOrderBy = p->pOrderBy->nExpr;
14129 sqlite3 *db = pParse->db;
14130 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
14131 if( pRet ){
14132 int i;
14133 for(i=0; i<nOrderBy; i++){
14134 struct ExprList_item *pItem = &pOrderBy->a[i];
14135 Expr *pTerm = pItem->pExpr;
14136 CollSeq *pColl;
14137
14138 if( pTerm->flags & EP_Collate ){
14139 pColl = sqlite3ExprCollSeq(pParse, pTerm);
14140 }else{
14141 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
14142 if( pColl==0 ) pColl = db->pDfltColl;
14143 pOrderBy->a[i].pExpr =
14144 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
14145 }
14146 assert( sqlite3KeyInfoIsWriteable(pRet) );
14147 pRet->aColl[i] = pColl;
14148 pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
14149 }
14150 }
14151
14152 return pRet;
14153 }
14154
14155 #ifndef SQLITE_OMIT_CTE
14156 /*
14157 ** This routine generates VDBE code to compute the content of a WITH RECURSIVE
14158 ** query of the form:
14159 **
14160 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
14161 ** \___________/ \_______________/
14162 ** p->pPrior p
14163 **
14164 **
14165 ** There is exactly one reference to the recursive-table in the FROM clause
14166 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
14167 **
14168 ** The setup-query runs once to generate an initial set of rows that go
14169 ** into a Queue table. Rows are extracted from the Queue table one by
14170 ** one. Each row extracted from Queue is output to pDest. Then the single
14171 ** extracted row (now in the iCurrent table) becomes the content of the
14172 ** recursive-table for a recursive-query run. The output of the recursive-query
14173 ** is added back into the Queue table. Then another row is extracted from Queue
14174 ** and the iteration continues until the Queue table is empty.
14175 **
14176 ** If the compound query operator is UNION then no duplicate rows are ever
14177 ** inserted into the Queue table. The iDistinct table keeps a copy of all rows
14178 ** that have ever been inserted into Queue and causes duplicates to be
14179 ** discarded. If the operator is UNION ALL, then duplicates are allowed.
14180 **
14181 ** If the query has an ORDER BY, then entries in the Queue table are kept in
14182 ** ORDER BY order and the first entry is extracted for each cycle. Without
14183 ** an ORDER BY, the Queue table is just a FIFO.
14184 **
14185 ** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
14186 ** have been output to pDest. A LIMIT of zero means to output no rows and a
14187 ** negative LIMIT means to output all rows. If there is also an OFFSET clause
14188 ** with a positive value, then the first OFFSET outputs are discarded rather
14189 ** than being sent to pDest. The LIMIT count does not begin until after OFFSET
14190 ** rows have been skipped.
14191 */
14192 static void generateWithRecursiveQuery(
14193 Parse *pParse, /* Parsing context */
14194 Select *p, /* The recursive SELECT to be coded */
14195 SelectDest *pDest /* What to do with query results */
14196 ){
14197 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
14198 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
14199 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
14200 Select *pSetup = p->pPrior; /* The setup query */
14201 int addrTop; /* Top of the loop */
14202 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
14203 int iCurrent = 0; /* The Current table */
14204 int regCurrent; /* Register holding Current table */
14205 int iQueue; /* The Queue table */
14206 int iDistinct = 0; /* To ensure unique results if UNION */
14207 int eDest = SRT_Fifo; /* How to write to Queue */
14208 SelectDest destQueue; /* SelectDest targetting the Queue table */
14209 int i; /* Loop counter */
14210 int rc; /* Result code */
14211 ExprList *pOrderBy; /* The ORDER BY clause */
14212 Expr *pLimit, *pOffset; /* Saved LIMIT and OFFSET */
14213 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
14214
14215 /* Obtain authorization to do a recursive query */
14216 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
14217
14218 /* Process the LIMIT and OFFSET clauses, if they exist */
14219 addrBreak = sqlite3VdbeMakeLabel(v);
14220 p->nSelectRow = 320; /* 4 billion rows */
14221 computeLimitRegisters(pParse, p, addrBreak);
14222 pLimit = p->pLimit;
14223 pOffset = p->pOffset;
14224 regLimit = p->iLimit;
14225 regOffset = p->iOffset;
14226 p->pLimit = p->pOffset = 0;
14227 p->iLimit = p->iOffset = 0;
14228 pOrderBy = p->pOrderBy;
14229
14230 /* Locate the cursor number of the Current table */
14231 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
14232 if( pSrc->a[i].fg.isRecursive ){
14233 iCurrent = pSrc->a[i].iCursor;
14234 break;
14235 }
14236 }
14237
14238 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
14239 ** the Distinct table must be exactly one greater than Queue in order
14240 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
14241 iQueue = pParse->nTab++;
14242 if( p->op==TK_UNION ){
14243 eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
14244 iDistinct = pParse->nTab++;
14245 }else{
14246 eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
14247 }
14248 sqlite3SelectDestInit(&destQueue, eDest, iQueue);
14249
14250 /* Allocate cursors for Current, Queue, and Distinct. */
14251 regCurrent = ++pParse->nMem;
14252 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
14253 if( pOrderBy ){
14254 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
14255 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
14256 (char*)pKeyInfo, P4_KEYINFO);
14257 destQueue.pOrderBy = pOrderBy;
14258 }else{
14259 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
14260 }
14261 VdbeComment((v, "Queue table"));
14262 if( iDistinct ){
14263 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
14264 p->selFlags |= SF_UsesEphemeral;
14265 }
14266
14267 /* Detach the ORDER BY clause from the compound SELECT */
14268 p->pOrderBy = 0;
14269
14270 /* Store the results of the setup-query in Queue. */
14271 pSetup->pNext = 0;
14272 rc = sqlite3Select(pParse, pSetup, &destQueue);
14273 pSetup->pNext = p;
14274 if( rc ) goto end_of_recursive_query;
14275
14276 /* Find the next row in the Queue and output that row */
14277 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
14278
14279 /* Transfer the next row in Queue over to Current */
14280 sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
14281 if( pOrderBy ){
14282 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
14283 }else{
14284 sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
14285 }
14286 sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
14287
14288 /* Output the single row in Current */
14289 addrCont = sqlite3VdbeMakeLabel(v);
14290 codeOffset(v, regOffset, addrCont);
14291 selectInnerLoop(pParse, p, p->pEList, iCurrent,
14292 0, 0, pDest, addrCont, addrBreak);
14293 if( regLimit ){
14294 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
14295 VdbeCoverage(v);
14296 }
14297 sqlite3VdbeResolveLabel(v, addrCont);
14298
14299 /* Execute the recursive SELECT taking the single row in Current as
14300 ** the value for the recursive-table. Store the results in the Queue.
14301 */
14302 if( p->selFlags & SF_Aggregate ){
14303 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
14304 }else{
14305 p->pPrior = 0;
14306 sqlite3Select(pParse, p, &destQueue);
14307 assert( p->pPrior==0 );
14308 p->pPrior = pSetup;
14309 }
14310
14311 /* Keep running the loop until the Queue is empty */
14312 sqlite3VdbeGoto(v, addrTop);
14313 sqlite3VdbeResolveLabel(v, addrBreak);
14314
14315 end_of_recursive_query:
14316 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
14317 p->pOrderBy = pOrderBy;
14318 p->pLimit = pLimit;
14319 p->pOffset = pOffset;
14320 return;
14321 }
14322 #endif /* SQLITE_OMIT_CTE */
14323
14324 /* Forward references */
14325 static int multiSelectOrderBy(
14326 Parse *pParse, /* Parsing context */
14327 Select *p, /* The right-most of SELECTs to be coded */
14328 SelectDest *pDest /* What to do with query results */
14329 );
14330
14331 /*
14332 ** Handle the special case of a compound-select that originates from a
14333 ** VALUES clause. By handling this as a special case, we avoid deep
14334 ** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
14335 ** on a VALUES clause.
14336 **
14337 ** Because the Select object originates from a VALUES clause:
14338 ** (1) It has no LIMIT or OFFSET
14339 ** (2) All terms are UNION ALL
14340 ** (3) There is no ORDER BY clause
14341 */
14342 static int multiSelectValues(
14343 Parse *pParse, /* Parsing context */
14344 Select *p, /* The right-most of SELECTs to be coded */
14345 SelectDest *pDest /* What to do with query results */
14346 ){
14347 Select *pPrior;
14348 int nRow = 1;
14349 int rc = 0;
14350 assert( p->selFlags & SF_MultiValue );
14351 do{
14352 assert( p->selFlags & SF_Values );
14353 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
14354 assert( p->pLimit==0 );
14355 assert( p->pOffset==0 );
14356 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
14357 if( p->pPrior==0 ) break;
14358 assert( p->pPrior->pNext==p );
14359 p = p->pPrior;
14360 nRow++;
14361 }while(1);
14362 while( p ){
14363 pPrior = p->pPrior;
14364 p->pPrior = 0;
14365 rc = sqlite3Select(pParse, p, pDest);
14366 p->pPrior = pPrior;
14367 if( rc ) break;
14368 p->nSelectRow = nRow;
14369 p = p->pNext;
14370 }
14371 return rc;
14372 }
14373
14374 /*
14375 ** This routine is called to process a compound query form from
14376 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
14377 ** INTERSECT
14378 **
14379 ** "p" points to the right-most of the two queries. the query on the
14380 ** left is p->pPrior. The left query could also be a compound query
14381 ** in which case this routine will be called recursively.
14382 **
14383 ** The results of the total query are to be written into a destination
14384 ** of type eDest with parameter iParm.
14385 **
14386 ** Example 1: Consider a three-way compound SQL statement.
14387 **
14388 ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
14389 **
14390 ** This statement is parsed up as follows:
14391 **
14392 ** SELECT c FROM t3
14393 ** |
14394 ** `-----> SELECT b FROM t2
14395 ** |
14396 ** `------> SELECT a FROM t1
14397 **
14398 ** The arrows in the diagram above represent the Select.pPrior pointer.
14399 ** So if this routine is called with p equal to the t3 query, then
14400 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
14401 **
14402 ** Notice that because of the way SQLite parses compound SELECTs, the
14403 ** individual selects always group from left to right.
14404 */
14405 static int multiSelect(
14406 Parse *pParse, /* Parsing context */
14407 Select *p, /* The right-most of SELECTs to be coded */
14408 SelectDest *pDest /* What to do with query results */
14409 ){
14410 int rc = SQLITE_OK; /* Success code from a subroutine */
14411 Select *pPrior; /* Another SELECT immediately to our left */
14412 Vdbe *v; /* Generate code to this VDBE */
14413 SelectDest dest; /* Alternative data destination */
14414 Select *pDelete = 0; /* Chain of simple selects to delete */
14415 sqlite3 *db; /* Database connection */
14416 #ifndef SQLITE_OMIT_EXPLAIN
14417 int iSub1 = 0; /* EQP id of left-hand query */
14418 int iSub2 = 0; /* EQP id of right-hand query */
14419 #endif
14420
14421 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
14422 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
14423 */
14424 assert( p && p->pPrior ); /* Calling function guarantees this much */
14425 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
14426 db = pParse->db;
14427 pPrior = p->pPrior;
14428 dest = *pDest;
14429 if( pPrior->pOrderBy ){
14430 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
14431 selectOpName(p->op));
14432 rc = 1;
14433 goto multi_select_end;
14434 }
14435 if( pPrior->pLimit ){
14436 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
14437 selectOpName(p->op));
14438 rc = 1;
14439 goto multi_select_end;
14440 }
14441
14442 v = sqlite3GetVdbe(pParse);
14443 assert( v!=0 ); /* The VDBE already created by calling function */
14444
14445 /* Create the destination temporary table if necessary
14446 */
14447 if( dest.eDest==SRT_EphemTab ){
14448 assert( p->pEList );
14449 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
14450 dest.eDest = SRT_Table;
14451 }
14452
14453 /* Special handling for a compound-select that originates as a VALUES clause.
14454 */
14455 if( p->selFlags & SF_MultiValue ){
14456 rc = multiSelectValues(pParse, p, &dest);
14457 goto multi_select_end;
14458 }
14459
14460 /* Make sure all SELECTs in the statement have the same number of elements
14461 ** in their result sets.
14462 */
14463 assert( p->pEList && pPrior->pEList );
14464 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
14465
14466 #ifndef SQLITE_OMIT_CTE
14467 if( p->selFlags & SF_Recursive ){
14468 generateWithRecursiveQuery(pParse, p, &dest);
14469 }else
14470 #endif
14471
14472 /* Compound SELECTs that have an ORDER BY clause are handled separately.
14473 */
14474 if( p->pOrderBy ){
14475 return multiSelectOrderBy(pParse, p, pDest);
14476 }else
14477
14478 /* Generate code for the left and right SELECT statements.
14479 */
14480 switch( p->op ){
14481 case TK_ALL: {
14482 int addr = 0;
14483 int nLimit;
14484 assert( !pPrior->pLimit );
14485 pPrior->iLimit = p->iLimit;
14486 pPrior->iOffset = p->iOffset;
14487 pPrior->pLimit = p->pLimit;
14488 pPrior->pOffset = p->pOffset;
14489 explainSetInteger(iSub1, pParse->iNextSelectId);
14490 rc = sqlite3Select(pParse, pPrior, &dest);
14491 p->pLimit = 0;
14492 p->pOffset = 0;
14493 if( rc ){
14494 goto multi_select_end;
14495 }
14496 p->pPrior = 0;
14497 p->iLimit = pPrior->iLimit;
14498 p->iOffset = pPrior->iOffset;
14499 if( p->iLimit ){
14500 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
14501 VdbeComment((v, "Jump ahead if LIMIT reached"));
14502 if( p->iOffset ){
14503 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
14504 p->iLimit, p->iOffset+1, p->iOffset);
14505 }
14506 }
14507 explainSetInteger(iSub2, pParse->iNextSelectId);
14508 rc = sqlite3Select(pParse, p, &dest);
14509 testcase( rc!=SQLITE_OK );
14510 pDelete = p->pPrior;
14511 p->pPrior = pPrior;
14512 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
14513 if( pPrior->pLimit
14514 && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
14515 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
14516 ){
14517 p->nSelectRow = sqlite3LogEst((u64)nLimit);
14518 }
14519 if( addr ){
14520 sqlite3VdbeJumpHere(v, addr);
14521 }
14522 break;
14523 }
14524 case TK_EXCEPT:
14525 case TK_UNION: {
14526 int unionTab; /* Cursor number of the temporary table holding result */
14527 u8 op = 0; /* One of the SRT_ operations to apply to self */
14528 int priorOp; /* The SRT_ operation to apply to prior selects */
14529 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
14530 int addr;
14531 SelectDest uniondest;
14532
14533 testcase( p->op==TK_EXCEPT );
14534 testcase( p->op==TK_UNION );
14535 priorOp = SRT_Union;
14536 if( dest.eDest==priorOp ){
14537 /* We can reuse a temporary table generated by a SELECT to our
14538 ** right.
14539 */
14540 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
14541 assert( p->pOffset==0 ); /* Not allowed on leftward elements */
14542 unionTab = dest.iSDParm;
14543 }else{
14544 /* We will need to create our own temporary table to hold the
14545 ** intermediate results.
14546 */
14547 unionTab = pParse->nTab++;
14548 assert( p->pOrderBy==0 );
14549 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
14550 assert( p->addrOpenEphm[0] == -1 );
14551 p->addrOpenEphm[0] = addr;
14552 findRightmost(p)->selFlags |= SF_UsesEphemeral;
14553 assert( p->pEList );
14554 }
14555
14556 /* Code the SELECT statements to our left
14557 */
14558 assert( !pPrior->pOrderBy );
14559 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
14560 explainSetInteger(iSub1, pParse->iNextSelectId);
14561 rc = sqlite3Select(pParse, pPrior, &uniondest);
14562 if( rc ){
14563 goto multi_select_end;
14564 }
14565
14566 /* Code the current SELECT statement
14567 */
14568 if( p->op==TK_EXCEPT ){
14569 op = SRT_Except;
14570 }else{
14571 assert( p->op==TK_UNION );
14572 op = SRT_Union;
14573 }
14574 p->pPrior = 0;
14575 pLimit = p->pLimit;
14576 p->pLimit = 0;
14577 pOffset = p->pOffset;
14578 p->pOffset = 0;
14579 uniondest.eDest = op;
14580 explainSetInteger(iSub2, pParse->iNextSelectId);
14581 rc = sqlite3Select(pParse, p, &uniondest);
14582 testcase( rc!=SQLITE_OK );
14583 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
14584 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
14585 sqlite3ExprListDelete(db, p->pOrderBy);
14586 pDelete = p->pPrior;
14587 p->pPrior = pPrior;
14588 p->pOrderBy = 0;
14589 if( p->op==TK_UNION ){
14590 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
14591 }
14592 sqlite3ExprDelete(db, p->pLimit);
14593 p->pLimit = pLimit;
14594 p->pOffset = pOffset;
14595 p->iLimit = 0;
14596 p->iOffset = 0;
14597
14598 /* Convert the data in the temporary table into whatever form
14599 ** it is that we currently need.
14600 */
14601 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
14602 if( dest.eDest!=priorOp ){
14603 int iCont, iBreak, iStart;
14604 assert( p->pEList );
14605 if( dest.eDest==SRT_Output ){
14606 Select *pFirst = p;
14607 while( pFirst->pPrior ) pFirst = pFirst->pPrior;
14608 generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList);
14609 }
14610 iBreak = sqlite3VdbeMakeLabel(v);
14611 iCont = sqlite3VdbeMakeLabel(v);
14612 computeLimitRegisters(pParse, p, iBreak);
14613 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
14614 iStart = sqlite3VdbeCurrentAddr(v);
14615 selectInnerLoop(pParse, p, p->pEList, unionTab,
14616 0, 0, &dest, iCont, iBreak);
14617 sqlite3VdbeResolveLabel(v, iCont);
14618 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
14619 sqlite3VdbeResolveLabel(v, iBreak);
14620 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
14621 }
14622 break;
14623 }
14624 default: assert( p->op==TK_INTERSECT ); {
14625 int tab1, tab2;
14626 int iCont, iBreak, iStart;
14627 Expr *pLimit, *pOffset;
14628 int addr;
14629 SelectDest intersectdest;
14630 int r1;
14631
14632 /* INTERSECT is different from the others since it requires
14633 ** two temporary tables. Hence it has its own case. Begin
14634 ** by allocating the tables we will need.
14635 */
14636 tab1 = pParse->nTab++;
14637 tab2 = pParse->nTab++;
14638 assert( p->pOrderBy==0 );
14639
14640 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
14641 assert( p->addrOpenEphm[0] == -1 );
14642 p->addrOpenEphm[0] = addr;
14643 findRightmost(p)->selFlags |= SF_UsesEphemeral;
14644 assert( p->pEList );
14645
14646 /* Code the SELECTs to our left into temporary table "tab1".
14647 */
14648 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
14649 explainSetInteger(iSub1, pParse->iNextSelectId);
14650 rc = sqlite3Select(pParse, pPrior, &intersectdest);
14651 if( rc ){
14652 goto multi_select_end;
14653 }
14654
14655 /* Code the current SELECT into temporary table "tab2"
14656 */
14657 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
14658 assert( p->addrOpenEphm[1] == -1 );
14659 p->addrOpenEphm[1] = addr;
14660 p->pPrior = 0;
14661 pLimit = p->pLimit;
14662 p->pLimit = 0;
14663 pOffset = p->pOffset;
14664 p->pOffset = 0;
14665 intersectdest.iSDParm = tab2;
14666 explainSetInteger(iSub2, pParse->iNextSelectId);
14667 rc = sqlite3Select(pParse, p, &intersectdest);
14668 testcase( rc!=SQLITE_OK );
14669 pDelete = p->pPrior;
14670 p->pPrior = pPrior;
14671 if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
14672 sqlite3ExprDelete(db, p->pLimit);
14673 p->pLimit = pLimit;
14674 p->pOffset = pOffset;
14675
14676 /* Generate code to take the intersection of the two temporary
14677 ** tables.
14678 */
14679 assert( p->pEList );
14680 if( dest.eDest==SRT_Output ){
14681 Select *pFirst = p;
14682 while( pFirst->pPrior ) pFirst = pFirst->pPrior;
14683 generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList);
14684 }
14685 iBreak = sqlite3VdbeMakeLabel(v);
14686 iCont = sqlite3VdbeMakeLabel(v);
14687 computeLimitRegisters(pParse, p, iBreak);
14688 sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
14689 r1 = sqlite3GetTempReg(pParse);
14690 iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
14691 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
14692 sqlite3ReleaseTempReg(pParse, r1);
14693 selectInnerLoop(pParse, p, p->pEList, tab1,
14694 0, 0, &dest, iCont, iBreak);
14695 sqlite3VdbeResolveLabel(v, iCont);
14696 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
14697 sqlite3VdbeResolveLabel(v, iBreak);
14698 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
14699 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
14700 break;
14701 }
14702 }
14703
14704 explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
14705
14706 /* Compute collating sequences used by
14707 ** temporary tables needed to implement the compound select.
14708 ** Attach the KeyInfo structure to all temporary tables.
14709 **
14710 ** This section is run by the right-most SELECT statement only.
14711 ** SELECT statements to the left always skip this part. The right-most
14712 ** SELECT might also skip this part if it has no ORDER BY clause and
14713 ** no temp tables are required.
14714 */
14715 if( p->selFlags & SF_UsesEphemeral ){
14716 int i; /* Loop counter */
14717 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
14718 Select *pLoop; /* For looping through SELECT statements */
14719 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
14720 int nCol; /* Number of columns in result set */
14721
14722 assert( p->pNext==0 );
14723 nCol = p->pEList->nExpr;
14724 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
14725 if( !pKeyInfo ){
14726 rc = SQLITE_NOMEM_BKPT;
14727 goto multi_select_end;
14728 }
14729 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
14730 *apColl = multiSelectCollSeq(pParse, p, i);
14731 if( 0==*apColl ){
14732 *apColl = db->pDfltColl;
14733 }
14734 }
14735
14736 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
14737 for(i=0; i<2; i++){
14738 int addr = pLoop->addrOpenEphm[i];
14739 if( addr<0 ){
14740 /* If [0] is unused then [1] is also unused. So we can
14741 ** always safely abort as soon as the first unused slot is found */
14742 assert( pLoop->addrOpenEphm[1]<0 );
14743 break;
14744 }
14745 sqlite3VdbeChangeP2(v, addr, nCol);
14746 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
14747 P4_KEYINFO);
14748 pLoop->addrOpenEphm[i] = -1;
14749 }
14750 }
14751 sqlite3KeyInfoUnref(pKeyInfo);
14752 }
14753
14754 multi_select_end:
14755 pDest->iSdst = dest.iSdst;
14756 pDest->nSdst = dest.nSdst;
14757 sqlite3SelectDelete(db, pDelete);
14758 return rc;
14759 }
14760 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
14761
14762 /*
14763 ** Error message for when two or more terms of a compound select have different
14764 ** size result sets.
14765 */
14766 SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
14767 if( p->selFlags & SF_Values ){
14768 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
14769 }else{
14770 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
14771 " do not have the same number of result columns", selectOpName(p->op));
14772 }
14773 }
14774
14775 /*
14776 ** Code an output subroutine for a coroutine implementation of a
14777 ** SELECT statment.
14778 **
14779 ** The data to be output is contained in pIn->iSdst. There are
14780 ** pIn->nSdst columns to be output. pDest is where the output should
14781 ** be sent.
14782 **
14783 ** regReturn is the number of the register holding the subroutine
14784 ** return address.
14785 **
14786 ** If regPrev>0 then it is the first register in a vector that
14787 ** records the previous output. mem[regPrev] is a flag that is false
14788 ** if there has been no previous output. If regPrev>0 then code is
14789 ** generated to suppress duplicates. pKeyInfo is used for comparing
14790 ** keys.
14791 **
14792 ** If the LIMIT found in p->iLimit is reached, jump immediately to
14793 ** iBreak.
14794 */
14795 static int generateOutputSubroutine(
14796 Parse *pParse, /* Parsing context */
14797 Select *p, /* The SELECT statement */
14798 SelectDest *pIn, /* Coroutine supplying data */
14799 SelectDest *pDest, /* Where to send the data */
14800 int regReturn, /* The return address register */
14801 int regPrev, /* Previous result register. No uniqueness if 0 */
14802 KeyInfo *pKeyInfo, /* For comparing with previous entry */
14803 int iBreak /* Jump here if we hit the LIMIT */
14804 ){
14805 Vdbe *v = pParse->pVdbe;
14806 int iContinue;
14807 int addr;
14808
14809 addr = sqlite3VdbeCurrentAddr(v);
14810 iContinue = sqlite3VdbeMakeLabel(v);
14811
14812 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
14813 */
14814 if( regPrev ){
14815 int addr1, addr2;
14816 addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
14817 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
14818 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
14819 sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
14820 sqlite3VdbeJumpHere(v, addr1);
14821 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
14822 sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
14823 }
14824 if( pParse->db->mallocFailed ) return 0;
14825
14826 /* Suppress the first OFFSET entries if there is an OFFSET clause
14827 */
14828 codeOffset(v, p->iOffset, iContinue);
14829
14830 assert( pDest->eDest!=SRT_Exists );
14831 assert( pDest->eDest!=SRT_Table );
14832 switch( pDest->eDest ){
14833 /* Store the result as data using a unique key.
14834 */
14835 case SRT_EphemTab: {
14836 int r1 = sqlite3GetTempReg(pParse);
14837 int r2 = sqlite3GetTempReg(pParse);
14838 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
14839 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
14840 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
14841 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
14842 sqlite3ReleaseTempReg(pParse, r2);
14843 sqlite3ReleaseTempReg(pParse, r1);
14844 break;
14845 }
14846
14847 #ifndef SQLITE_OMIT_SUBQUERY
14848 /* If we are creating a set for an "expr IN (SELECT ...)".
14849 */
14850 case SRT_Set: {
14851 int r1;
14852 testcase( pIn->nSdst>1 );
14853 r1 = sqlite3GetTempReg(pParse);
14854 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
14855 r1, pDest->zAffSdst, pIn->nSdst);
14856 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
14857 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
14858 pIn->iSdst, pIn->nSdst);
14859 sqlite3ReleaseTempReg(pParse, r1);
14860 break;
14861 }
14862
14863 /* If this is a scalar select that is part of an expression, then
14864 ** store the results in the appropriate memory cell and break out
14865 ** of the scan loop.
14866 */
14867 case SRT_Mem: {
14868 assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 );
14869 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
14870 /* The LIMIT clause will jump out of the loop for us */
14871 break;
14872 }
14873 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
14874
14875 /* The results are stored in a sequence of registers
14876 ** starting at pDest->iSdst. Then the co-routine yields.
14877 */
14878 case SRT_Coroutine: {
14879 if( pDest->iSdst==0 ){
14880 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
14881 pDest->nSdst = pIn->nSdst;
14882 }
14883 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
14884 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
14885 break;
14886 }
14887
14888 /* If none of the above, then the result destination must be
14889 ** SRT_Output. This routine is never called with any other
14890 ** destination other than the ones handled above or SRT_Output.
14891 **
14892 ** For SRT_Output, results are stored in a sequence of registers.
14893 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
14894 ** return the next row of result.
14895 */
14896 default: {
14897 assert( pDest->eDest==SRT_Output );
14898 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
14899 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
14900 break;
14901 }
14902 }
14903
14904 /* Jump to the end of the loop if the LIMIT is reached.
14905 */
14906 if( p->iLimit ){
14907 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
14908 }
14909
14910 /* Generate the subroutine return
14911 */
14912 sqlite3VdbeResolveLabel(v, iContinue);
14913 sqlite3VdbeAddOp1(v, OP_Return, regReturn);
14914
14915 return addr;
14916 }
14917
14918 /*
14919 ** Alternative compound select code generator for cases when there
14920 ** is an ORDER BY clause.
14921 **
14922 ** We assume a query of the following form:
14923 **
14924 ** <selectA> <operator> <selectB> ORDER BY <orderbylist>
14925 **
14926 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
14927 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
14928 ** co-routines. Then run the co-routines in parallel and merge the results
14929 ** into the output. In addition to the two coroutines (called selectA and
14930 ** selectB) there are 7 subroutines:
14931 **
14932 ** outA: Move the output of the selectA coroutine into the output
14933 ** of the compound query.
14934 **
14935 ** outB: Move the output of the selectB coroutine into the output
14936 ** of the compound query. (Only generated for UNION and
14937 ** UNION ALL. EXCEPT and INSERTSECT never output a row that
14938 ** appears only in B.)
14939 **
14940 ** AltB: Called when there is data from both coroutines and A<B.
14941 **
14942 ** AeqB: Called when there is data from both coroutines and A==B.
14943 **
14944 ** AgtB: Called when there is data from both coroutines and A>B.
14945 **
14946 ** EofA: Called when data is exhausted from selectA.
14947 **
14948 ** EofB: Called when data is exhausted from selectB.
14949 **
14950 ** The implementation of the latter five subroutines depend on which
14951 ** <operator> is used:
14952 **
14953 **
14954 ** UNION ALL UNION EXCEPT INTERSECT
14955 ** ------------- ----------------- -------------- -----------------
14956 ** AltB: outA, nextA outA, nextA outA, nextA nextA
14957 **
14958 ** AeqB: outA, nextA nextA nextA outA, nextA
14959 **
14960 ** AgtB: outB, nextB outB, nextB nextB nextB
14961 **
14962 ** EofA: outB, nextB outB, nextB halt halt
14963 **
14964 ** EofB: outA, nextA outA, nextA outA, nextA halt
14965 **
14966 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
14967 ** causes an immediate jump to EofA and an EOF on B following nextB causes
14968 ** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
14969 ** following nextX causes a jump to the end of the select processing.
14970 **
14971 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
14972 ** within the output subroutine. The regPrev register set holds the previously
14973 ** output value. A comparison is made against this value and the output
14974 ** is skipped if the next results would be the same as the previous.
14975 **
14976 ** The implementation plan is to implement the two coroutines and seven
14977 ** subroutines first, then put the control logic at the bottom. Like this:
14978 **
14979 ** goto Init
14980 ** coA: coroutine for left query (A)
14981 ** coB: coroutine for right query (B)
14982 ** outA: output one row of A
14983 ** outB: output one row of B (UNION and UNION ALL only)
14984 ** EofA: ...
14985 ** EofB: ...
14986 ** AltB: ...
14987 ** AeqB: ...
14988 ** AgtB: ...
14989 ** Init: initialize coroutine registers
14990 ** yield coA
14991 ** if eof(A) goto EofA
14992 ** yield coB
14993 ** if eof(B) goto EofB
14994 ** Cmpr: Compare A, B
14995 ** Jump AltB, AeqB, AgtB
14996 ** End: ...
14997 **
14998 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
14999 ** actually called using Gosub and they do not Return. EofA and EofB loop
15000 ** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
15001 ** and AgtB jump to either L2 or to one of EofA or EofB.
15002 */
15003 #ifndef SQLITE_OMIT_COMPOUND_SELECT
15004 static int multiSelectOrderBy(
15005 Parse *pParse, /* Parsing context */
15006 Select *p, /* The right-most of SELECTs to be coded */
15007 SelectDest *pDest /* What to do with query results */
15008 ){
15009 int i, j; /* Loop counters */
15010 Select *pPrior; /* Another SELECT immediately to our left */
15011 Vdbe *v; /* Generate code to this VDBE */
15012 SelectDest destA; /* Destination for coroutine A */
15013 SelectDest destB; /* Destination for coroutine B */
15014 int regAddrA; /* Address register for select-A coroutine */
15015 int regAddrB; /* Address register for select-B coroutine */
15016 int addrSelectA; /* Address of the select-A coroutine */
15017 int addrSelectB; /* Address of the select-B coroutine */
15018 int regOutA; /* Address register for the output-A subroutine */
15019 int regOutB; /* Address register for the output-B subroutine */
15020 int addrOutA; /* Address of the output-A subroutine */
15021 int addrOutB = 0; /* Address of the output-B subroutine */
15022 int addrEofA; /* Address of the select-A-exhausted subroutine */
15023 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
15024 int addrEofB; /* Address of the select-B-exhausted subroutine */
15025 int addrAltB; /* Address of the A<B subroutine */
15026 int addrAeqB; /* Address of the A==B subroutine */
15027 int addrAgtB; /* Address of the A>B subroutine */
15028 int regLimitA; /* Limit register for select-A */
15029 int regLimitB; /* Limit register for select-A */
15030 int regPrev; /* A range of registers to hold previous output */
15031 int savedLimit; /* Saved value of p->iLimit */
15032 int savedOffset; /* Saved value of p->iOffset */
15033 int labelCmpr; /* Label for the start of the merge algorithm */
15034 int labelEnd; /* Label for the end of the overall SELECT stmt */
15035 int addr1; /* Jump instructions that get retargetted */
15036 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
15037 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
15038 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
15039 sqlite3 *db; /* Database connection */
15040 ExprList *pOrderBy; /* The ORDER BY clause */
15041 int nOrderBy; /* Number of terms in the ORDER BY clause */
15042 int *aPermute; /* Mapping from ORDER BY terms to result set columns */
15043 #ifndef SQLITE_OMIT_EXPLAIN
15044 int iSub1; /* EQP id of left-hand query */
15045 int iSub2; /* EQP id of right-hand query */
15046 #endif
15047
15048 assert( p->pOrderBy!=0 );
15049 assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
15050 db = pParse->db;
15051 v = pParse->pVdbe;
15052 assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
15053 labelEnd = sqlite3VdbeMakeLabel(v);
15054 labelCmpr = sqlite3VdbeMakeLabel(v);
15055
15056
15057 /* Patch up the ORDER BY clause
15058 */
15059 op = p->op;
15060 pPrior = p->pPrior;
15061 assert( pPrior->pOrderBy==0 );
15062 pOrderBy = p->pOrderBy;
15063 assert( pOrderBy );
15064 nOrderBy = pOrderBy->nExpr;
15065
15066 /* For operators other than UNION ALL we have to make sure that
15067 ** the ORDER BY clause covers every term of the result set. Add
15068 ** terms to the ORDER BY clause as necessary.
15069 */
15070 if( op!=TK_ALL ){
15071 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
15072 struct ExprList_item *pItem;
15073 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
15074 assert( pItem->u.x.iOrderByCol>0 );
15075 if( pItem->u.x.iOrderByCol==i ) break;
15076 }
15077 if( j==nOrderBy ){
15078 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
15079 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
15080 pNew->flags |= EP_IntValue;
15081 pNew->u.iValue = i;
15082 pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
15083 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
15084 }
15085 }
15086 }
15087
15088 /* Compute the comparison permutation and keyinfo that is used with
15089 ** the permutation used to determine if the next
15090 ** row of results comes from selectA or selectB. Also add explicit
15091 ** collations to the ORDER BY clause terms so that when the subqueries
15092 ** to the right and the left are evaluated, they use the correct
15093 ** collation.
15094 */
15095 aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
15096 if( aPermute ){
15097 struct ExprList_item *pItem;
15098 aPermute[0] = nOrderBy;
15099 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
15100 assert( pItem->u.x.iOrderByCol>0 );
15101 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
15102 aPermute[i] = pItem->u.x.iOrderByCol - 1;
15103 }
15104 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
15105 }else{
15106 pKeyMerge = 0;
15107 }
15108
15109 /* Reattach the ORDER BY clause to the query.
15110 */
15111 p->pOrderBy = pOrderBy;
15112 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
15113
15114 /* Allocate a range of temporary registers and the KeyInfo needed
15115 ** for the logic that removes duplicate result rows when the
15116 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
15117 */
15118 if( op==TK_ALL ){
15119 regPrev = 0;
15120 }else{
15121 int nExpr = p->pEList->nExpr;
15122 assert( nOrderBy>=nExpr || db->mallocFailed );
15123 regPrev = pParse->nMem+1;
15124 pParse->nMem += nExpr+1;
15125 sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
15126 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
15127 if( pKeyDup ){
15128 assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
15129 for(i=0; i<nExpr; i++){
15130 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
15131 pKeyDup->aSortOrder[i] = 0;
15132 }
15133 }
15134 }
15135
15136 /* Separate the left and the right query from one another
15137 */
15138 p->pPrior = 0;
15139 pPrior->pNext = 0;
15140 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
15141 if( pPrior->pPrior==0 ){
15142 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
15143 }
15144
15145 /* Compute the limit registers */
15146 computeLimitRegisters(pParse, p, labelEnd);
15147 if( p->iLimit && op==TK_ALL ){
15148 regLimitA = ++pParse->nMem;
15149 regLimitB = ++pParse->nMem;
15150 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
15151 regLimitA);
15152 sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
15153 }else{
15154 regLimitA = regLimitB = 0;
15155 }
15156 sqlite3ExprDelete(db, p->pLimit);
15157 p->pLimit = 0;
15158 sqlite3ExprDelete(db, p->pOffset);
15159 p->pOffset = 0;
15160
15161 regAddrA = ++pParse->nMem;
15162 regAddrB = ++pParse->nMem;
15163 regOutA = ++pParse->nMem;
15164 regOutB = ++pParse->nMem;
15165 sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
15166 sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
15167
15168 /* Generate a coroutine to evaluate the SELECT statement to the
15169 ** left of the compound operator - the "A" select.
15170 */
15171 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
15172 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
15173 VdbeComment((v, "left SELECT"));
15174 pPrior->iLimit = regLimitA;
15175 explainSetInteger(iSub1, pParse->iNextSelectId);
15176 sqlite3Select(pParse, pPrior, &destA);
15177 sqlite3VdbeEndCoroutine(v, regAddrA);
15178 sqlite3VdbeJumpHere(v, addr1);
15179
15180 /* Generate a coroutine to evaluate the SELECT statement on
15181 ** the right - the "B" select
15182 */
15183 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
15184 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
15185 VdbeComment((v, "right SELECT"));
15186 savedLimit = p->iLimit;
15187 savedOffset = p->iOffset;
15188 p->iLimit = regLimitB;
15189 p->iOffset = 0;
15190 explainSetInteger(iSub2, pParse->iNextSelectId);
15191 sqlite3Select(pParse, p, &destB);
15192 p->iLimit = savedLimit;
15193 p->iOffset = savedOffset;
15194 sqlite3VdbeEndCoroutine(v, regAddrB);
15195
15196 /* Generate a subroutine that outputs the current row of the A
15197 ** select as the next output row of the compound select.
15198 */
15199 VdbeNoopComment((v, "Output routine for A"));
15200 addrOutA = generateOutputSubroutine(pParse,
15201 p, &destA, pDest, regOutA,
15202 regPrev, pKeyDup, labelEnd);
15203
15204 /* Generate a subroutine that outputs the current row of the B
15205 ** select as the next output row of the compound select.
15206 */
15207 if( op==TK_ALL || op==TK_UNION ){
15208 VdbeNoopComment((v, "Output routine for B"));
15209 addrOutB = generateOutputSubroutine(pParse,
15210 p, &destB, pDest, regOutB,
15211 regPrev, pKeyDup, labelEnd);
15212 }
15213 sqlite3KeyInfoUnref(pKeyDup);
15214
15215 /* Generate a subroutine to run when the results from select A
15216 ** are exhausted and only data in select B remains.
15217 */
15218 if( op==TK_EXCEPT || op==TK_INTERSECT ){
15219 addrEofA_noB = addrEofA = labelEnd;
15220 }else{
15221 VdbeNoopComment((v, "eof-A subroutine"));
15222 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
15223 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
15224 VdbeCoverage(v);
15225 sqlite3VdbeGoto(v, addrEofA);
15226 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
15227 }
15228
15229 /* Generate a subroutine to run when the results from select B
15230 ** are exhausted and only data in select A remains.
15231 */
15232 if( op==TK_INTERSECT ){
15233 addrEofB = addrEofA;
15234 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
15235 }else{
15236 VdbeNoopComment((v, "eof-B subroutine"));
15237 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
15238 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
15239 sqlite3VdbeGoto(v, addrEofB);
15240 }
15241
15242 /* Generate code to handle the case of A<B
15243 */
15244 VdbeNoopComment((v, "A-lt-B subroutine"));
15245 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
15246 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
15247 sqlite3VdbeGoto(v, labelCmpr);
15248
15249 /* Generate code to handle the case of A==B
15250 */
15251 if( op==TK_ALL ){
15252 addrAeqB = addrAltB;
15253 }else if( op==TK_INTERSECT ){
15254 addrAeqB = addrAltB;
15255 addrAltB++;
15256 }else{
15257 VdbeNoopComment((v, "A-eq-B subroutine"));
15258 addrAeqB =
15259 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
15260 sqlite3VdbeGoto(v, labelCmpr);
15261 }
15262
15263 /* Generate code to handle the case of A>B
15264 */
15265 VdbeNoopComment((v, "A-gt-B subroutine"));
15266 addrAgtB = sqlite3VdbeCurrentAddr(v);
15267 if( op==TK_ALL || op==TK_UNION ){
15268 sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
15269 }
15270 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
15271 sqlite3VdbeGoto(v, labelCmpr);
15272
15273 /* This code runs once to initialize everything.
15274 */
15275 sqlite3VdbeJumpHere(v, addr1);
15276 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
15277 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
15278
15279 /* Implement the main merge loop
15280 */
15281 sqlite3VdbeResolveLabel(v, labelCmpr);
15282 sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
15283 sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
15284 (char*)pKeyMerge, P4_KEYINFO);
15285 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
15286 sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
15287
15288 /* Jump to the this point in order to terminate the query.
15289 */
15290 sqlite3VdbeResolveLabel(v, labelEnd);
15291
15292 /* Set the number of output columns
15293 */
15294 if( pDest->eDest==SRT_Output ){
15295 Select *pFirst = pPrior;
15296 while( pFirst->pPrior ) pFirst = pFirst->pPrior;
15297 generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList);
15298 }
15299
15300 /* Reassembly the compound query so that it will be freed correctly
15301 ** by the calling function */
15302 if( p->pPrior ){
15303 sqlite3SelectDelete(db, p->pPrior);
15304 }
15305 p->pPrior = pPrior;
15306 pPrior->pNext = p;
15307
15308 /*** TBD: Insert subroutine calls to close cursors on incomplete
15309 **** subqueries ****/
15310 explainComposite(pParse, p->op, iSub1, iSub2, 0);
15311 return pParse->nErr!=0;
15312 }
15313 #endif
15314
15315 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
15316 /* Forward Declarations */
15317 static void substExprList(Parse*, ExprList*, int, ExprList*);
15318 static void substSelect(Parse*, Select *, int, ExprList*, int);
15319
15320 /*
15321 ** Scan through the expression pExpr. Replace every reference to
15322 ** a column in table number iTable with a copy of the iColumn-th
15323 ** entry in pEList. (But leave references to the ROWID column
15324 ** unchanged.)
15325 **
15326 ** This routine is part of the flattening procedure. A subquery
15327 ** whose result set is defined by pEList appears as entry in the
15328 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
15329 ** FORM clause entry is iTable. This routine make the necessary
15330 ** changes to pExpr so that it refers directly to the source table
15331 ** of the subquery rather the result set of the subquery.
15332 */
15333 static Expr *substExpr(
15334 Parse *pParse, /* Report errors here */
15335 Expr *pExpr, /* Expr in which substitution occurs */
15336 int iTable, /* Table to be substituted */
15337 ExprList *pEList /* Substitute expressions */
15338 ){
15339 sqlite3 *db = pParse->db;
15340 if( pExpr==0 ) return 0;
15341 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
15342 if( pExpr->iColumn<0 ){
15343 pExpr->op = TK_NULL;
15344 }else{
15345 Expr *pNew;
15346 Expr *pCopy = pEList->a[pExpr->iColumn].pExpr;
15347 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
15348 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
15349 if( sqlite3ExprIsVector(pCopy) ){
15350 sqlite3VectorErrorMsg(pParse, pCopy);
15351 }else{
15352 pNew = sqlite3ExprDup(db, pCopy, 0);
15353 if( pNew && (pExpr->flags & EP_FromJoin) ){
15354 pNew->iRightJoinTable = pExpr->iRightJoinTable;
15355 pNew->flags |= EP_FromJoin;
15356 }
15357 sqlite3ExprDelete(db, pExpr);
15358 pExpr = pNew;
15359 }
15360 }
15361 }else{
15362 pExpr->pLeft = substExpr(pParse, pExpr->pLeft, iTable, pEList);
15363 pExpr->pRight = substExpr(pParse, pExpr->pRight, iTable, pEList);
15364 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
15365 substSelect(pParse, pExpr->x.pSelect, iTable, pEList, 1);
15366 }else{
15367 substExprList(pParse, pExpr->x.pList, iTable, pEList);
15368 }
15369 }
15370 return pExpr;
15371 }
15372 static void substExprList(
15373 Parse *pParse, /* Report errors here */
15374 ExprList *pList, /* List to scan and in which to make substitutes */
15375 int iTable, /* Table to be substituted */
15376 ExprList *pEList /* Substitute values */
15377 ){
15378 int i;
15379 if( pList==0 ) return;
15380 for(i=0; i<pList->nExpr; i++){
15381 pList->a[i].pExpr = substExpr(pParse, pList->a[i].pExpr, iTable, pEList);
15382 }
15383 }
15384 static void substSelect(
15385 Parse *pParse, /* Report errors here */
15386 Select *p, /* SELECT statement in which to make substitutions */
15387 int iTable, /* Table to be replaced */
15388 ExprList *pEList, /* Substitute values */
15389 int doPrior /* Do substitutes on p->pPrior too */
15390 ){
15391 SrcList *pSrc;
15392 struct SrcList_item *pItem;
15393 int i;
15394 if( !p ) return;
15395 do{
15396 substExprList(pParse, p->pEList, iTable, pEList);
15397 substExprList(pParse, p->pGroupBy, iTable, pEList);
15398 substExprList(pParse, p->pOrderBy, iTable, pEList);
15399 p->pHaving = substExpr(pParse, p->pHaving, iTable, pEList);
15400 p->pWhere = substExpr(pParse, p->pWhere, iTable, pEList);
15401 pSrc = p->pSrc;
15402 assert( pSrc!=0 );
15403 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
15404 substSelect(pParse, pItem->pSelect, iTable, pEList, 1);
15405 if( pItem->fg.isTabFunc ){
15406 substExprList(pParse, pItem->u1.pFuncArg, iTable, pEList);
15407 }
15408 }
15409 }while( doPrior && (p = p->pPrior)!=0 );
15410 }
15411 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
15412
15413 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
15414 /*
15415 ** This routine attempts to flatten subqueries as a performance optimization.
15416 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
15417 **
15418 ** To understand the concept of flattening, consider the following
15419 ** query:
15420 **
15421 ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
15422 **
15423 ** The default way of implementing this query is to execute the
15424 ** subquery first and store the results in a temporary table, then
15425 ** run the outer query on that temporary table. This requires two
15426 ** passes over the data. Furthermore, because the temporary table
15427 ** has no indices, the WHERE clause on the outer query cannot be
15428 ** optimized.
15429 **
15430 ** This routine attempts to rewrite queries such as the above into
15431 ** a single flat select, like this:
15432 **
15433 ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
15434 **
15435 ** The code generated for this simplification gives the same result
15436 ** but only has to scan the data once. And because indices might
15437 ** exist on the table t1, a complete scan of the data might be
15438 ** avoided.
15439 **
15440 ** Flattening is only attempted if all of the following are true:
15441 **
15442 ** (1) The subquery and the outer query do not both use aggregates.
15443 **
15444 ** (2) The subquery is not an aggregate or (2a) the outer query is not a join
15445 ** and (2b) the outer query does not use subqueries other than the one
15446 ** FROM-clause subquery that is a candidate for flattening. (2b is
15447 ** due to ticket [2f7170d73bf9abf80] from 2015-02-09.)
15448 **
15449 ** (3) The subquery is not the right operand of a left outer join
15450 ** (Originally ticket #306. Strengthened by ticket #3300)
15451 **
15452 ** (4) The subquery is not DISTINCT.
15453 **
15454 ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
15455 ** sub-queries that were excluded from this optimization. Restriction
15456 ** (4) has since been expanded to exclude all DISTINCT subqueries.
15457 **
15458 ** (6) The subquery does not use aggregates or the outer query is not
15459 ** DISTINCT.
15460 **
15461 ** (7) The subquery has a FROM clause. TODO: For subqueries without
15462 ** A FROM clause, consider adding a FROM close with the special
15463 ** table sqlite_once that consists of a single row containing a
15464 ** single NULL.
15465 **
15466 ** (8) The subquery does not use LIMIT or the outer query is not a join.
15467 **
15468 ** (9) The subquery does not use LIMIT or the outer query does not use
15469 ** aggregates.
15470 **
15471 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
15472 ** accidently carried the comment forward until 2014-09-15. Original
15473 ** text: "The subquery does not use aggregates or the outer query
15474 ** does not use LIMIT."
15475 **
15476 ** (11) The subquery and the outer query do not both have ORDER BY clauses.
15477 **
15478 ** (**) Not implemented. Subsumed into restriction (3). Was previously
15479 ** a separate restriction deriving from ticket #350.
15480 **
15481 ** (13) The subquery and outer query do not both use LIMIT.
15482 **
15483 ** (14) The subquery does not use OFFSET.
15484 **
15485 ** (15) The outer query is not part of a compound select or the
15486 ** subquery does not have a LIMIT clause.
15487 ** (See ticket #2339 and ticket [02a8e81d44]).
15488 **
15489 ** (16) The outer query is not an aggregate or the subquery does
15490 ** not contain ORDER BY. (Ticket #2942) This used to not matter
15491 ** until we introduced the group_concat() function.
15492 **
15493 ** (17) The sub-query is not a compound select, or it is a UNION ALL
15494 ** compound clause made up entirely of non-aggregate queries, and
15495 ** the parent query:
15496 **
15497 ** * is not itself part of a compound select,
15498 ** * is not an aggregate or DISTINCT query, and
15499 ** * is not a join
15500 **
15501 ** The parent and sub-query may contain WHERE clauses. Subject to
15502 ** rules (11), (13) and (14), they may also contain ORDER BY,
15503 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
15504 ** operator other than UNION ALL because all the other compound
15505 ** operators have an implied DISTINCT which is disallowed by
15506 ** restriction (4).
15507 **
15508 ** Also, each component of the sub-query must return the same number
15509 ** of result columns. This is actually a requirement for any compound
15510 ** SELECT statement, but all the code here does is make sure that no
15511 ** such (illegal) sub-query is flattened. The caller will detect the
15512 ** syntax error and return a detailed message.
15513 **
15514 ** (18) If the sub-query is a compound select, then all terms of the
15515 ** ORDER by clause of the parent must be simple references to
15516 ** columns of the sub-query.
15517 **
15518 ** (19) The subquery does not use LIMIT or the outer query does not
15519 ** have a WHERE clause.
15520 **
15521 ** (20) If the sub-query is a compound select, then it must not use
15522 ** an ORDER BY clause. Ticket #3773. We could relax this constraint
15523 ** somewhat by saying that the terms of the ORDER BY clause must
15524 ** appear as unmodified result columns in the outer query. But we
15525 ** have other optimizations in mind to deal with that case.
15526 **
15527 ** (21) The subquery does not use LIMIT or the outer query is not
15528 ** DISTINCT. (See ticket [752e1646fc]).
15529 **
15530 ** (22) The subquery is not a recursive CTE.
15531 **
15532 ** (23) The parent is not a recursive CTE, or the sub-query is not a
15533 ** compound query. This restriction is because transforming the
15534 ** parent to a compound query confuses the code that handles
15535 ** recursive queries in multiSelect().
15536 **
15537 ** (24) The subquery is not an aggregate that uses the built-in min() or
15538 ** or max() functions. (Without this restriction, a query like:
15539 ** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
15540 ** return the value X for which Y was maximal.)
15541 **
15542 **
15543 ** In this routine, the "p" parameter is a pointer to the outer query.
15544 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
15545 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
15546 **
15547 ** If flattening is not attempted, this routine is a no-op and returns 0.
15548 ** If flattening is attempted this routine returns 1.
15549 **
15550 ** All of the expression analysis must occur on both the outer query and
15551 ** the subquery before this routine runs.
15552 */
15553 static int flattenSubquery(
15554 Parse *pParse, /* Parsing context */
15555 Select *p, /* The parent or outer SELECT statement */
15556 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
15557 int isAgg, /* True if outer SELECT uses aggregate functions */
15558 int subqueryIsAgg /* True if the subquery uses aggregate functions */
15559 ){
15560 const char *zSavedAuthContext = pParse->zAuthContext;
15561 Select *pParent; /* Current UNION ALL term of the other query */
15562 Select *pSub; /* The inner query or "subquery" */
15563 Select *pSub1; /* Pointer to the rightmost select in sub-query */
15564 SrcList *pSrc; /* The FROM clause of the outer query */
15565 SrcList *pSubSrc; /* The FROM clause of the subquery */
15566 ExprList *pList; /* The result set of the outer query */
15567 int iParent; /* VDBE cursor number of the pSub result set temp table */
15568 int i; /* Loop counter */
15569 Expr *pWhere; /* The WHERE clause */
15570 struct SrcList_item *pSubitem; /* The subquery */
15571 sqlite3 *db = pParse->db;
15572
15573 /* Check to see if flattening is permitted. Return 0 if not.
15574 */
15575 assert( p!=0 );
15576 assert( p->pPrior==0 ); /* Unable to flatten compound queries */
15577 if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
15578 pSrc = p->pSrc;
15579 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
15580 pSubitem = &pSrc->a[iFrom];
15581 iParent = pSubitem->iCursor;
15582 pSub = pSubitem->pSelect;
15583 assert( pSub!=0 );
15584 if( subqueryIsAgg ){
15585 if( isAgg ) return 0; /* Restriction (1) */
15586 if( pSrc->nSrc>1 ) return 0; /* Restriction (2a) */
15587 if( (p->pWhere && ExprHasProperty(p->pWhere,EP_Subquery))
15588 || (sqlite3ExprListFlags(p->pEList) & EP_Subquery)!=0
15589 || (sqlite3ExprListFlags(p->pOrderBy) & EP_Subquery)!=0
15590 ){
15591 return 0; /* Restriction (2b) */
15592 }
15593 }
15594
15595 pSubSrc = pSub->pSrc;
15596 assert( pSubSrc );
15597 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
15598 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
15599 ** because they could be computed at compile-time. But when LIMIT and OFFSET
15600 ** became arbitrary expressions, we were forced to add restrictions (13)
15601 ** and (14). */
15602 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
15603 if( pSub->pOffset ) return 0; /* Restriction (14) */
15604 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
15605 return 0; /* Restriction (15) */
15606 }
15607 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
15608 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */
15609 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
15610 return 0; /* Restrictions (8)(9) */
15611 }
15612 if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
15613 return 0; /* Restriction (6) */
15614 }
15615 if( p->pOrderBy && pSub->pOrderBy ){
15616 return 0; /* Restriction (11) */
15617 }
15618 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
15619 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
15620 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
15621 return 0; /* Restriction (21) */
15622 }
15623 testcase( pSub->selFlags & SF_Recursive );
15624 testcase( pSub->selFlags & SF_MinMaxAgg );
15625 if( pSub->selFlags & (SF_Recursive|SF_MinMaxAgg) ){
15626 return 0; /* Restrictions (22) and (24) */
15627 }
15628 if( (p->selFlags & SF_Recursive) && pSub->pPrior ){
15629 return 0; /* Restriction (23) */
15630 }
15631
15632 /* OBSOLETE COMMENT 1:
15633 ** Restriction 3: If the subquery is a join, make sure the subquery is
15634 ** not used as the right operand of an outer join. Examples of why this
15635 ** is not allowed:
15636 **
15637 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
15638 **
15639 ** If we flatten the above, we would get
15640 **
15641 ** (t1 LEFT OUTER JOIN t2) JOIN t3
15642 **
15643 ** which is not at all the same thing.
15644 **
15645 ** OBSOLETE COMMENT 2:
15646 ** Restriction 12: If the subquery is the right operand of a left outer
15647 ** join, make sure the subquery has no WHERE clause.
15648 ** An examples of why this is not allowed:
15649 **
15650 ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
15651 **
15652 ** If we flatten the above, we would get
15653 **
15654 ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
15655 **
15656 ** But the t2.x>0 test will always fail on a NULL row of t2, which
15657 ** effectively converts the OUTER JOIN into an INNER JOIN.
15658 **
15659 ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
15660 ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
15661 ** is fraught with danger. Best to avoid the whole thing. If the
15662 ** subquery is the right term of a LEFT JOIN, then do not flatten.
15663 */
15664 if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
15665 return 0;
15666 }
15667
15668 /* Restriction 17: If the sub-query is a compound SELECT, then it must
15669 ** use only the UNION ALL operator. And none of the simple select queries
15670 ** that make up the compound SELECT are allowed to be aggregate or distinct
15671 ** queries.
15672 */
15673 if( pSub->pPrior ){
15674 if( pSub->pOrderBy ){
15675 return 0; /* Restriction 20 */
15676 }
15677 if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
15678 return 0;
15679 }
15680 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
15681 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
15682 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
15683 assert( pSub->pSrc!=0 );
15684 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
15685 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
15686 || (pSub1->pPrior && pSub1->op!=TK_ALL)
15687 || pSub1->pSrc->nSrc<1
15688 ){
15689 return 0;
15690 }
15691 testcase( pSub1->pSrc->nSrc>1 );
15692 }
15693
15694 /* Restriction 18. */
15695 if( p->pOrderBy ){
15696 int ii;
15697 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
15698 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
15699 }
15700 }
15701 }
15702
15703 /***** If we reach this point, flattening is permitted. *****/
15704 SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n",
15705 pSub->zSelName, pSub, iFrom));
15706
15707 /* Authorize the subquery */
15708 pParse->zAuthContext = pSubitem->zName;
15709 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
15710 testcase( i==SQLITE_DENY );
15711 pParse->zAuthContext = zSavedAuthContext;
15712
15713 /* If the sub-query is a compound SELECT statement, then (by restrictions
15714 ** 17 and 18 above) it must be a UNION ALL and the parent query must
15715 ** be of the form:
15716 **
15717 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
15718 **
15719 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
15720 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
15721 ** OFFSET clauses and joins them to the left-hand-side of the original
15722 ** using UNION ALL operators. In this case N is the number of simple
15723 ** select statements in the compound sub-query.
15724 **
15725 ** Example:
15726 **
15727 ** SELECT a+1 FROM (
15728 ** SELECT x FROM tab
15729 ** UNION ALL
15730 ** SELECT y FROM tab
15731 ** UNION ALL
15732 ** SELECT abs(z*2) FROM tab2
15733 ** ) WHERE a!=5 ORDER BY 1
15734 **
15735 ** Transformed into:
15736 **
15737 ** SELECT x+1 FROM tab WHERE x+1!=5
15738 ** UNION ALL
15739 ** SELECT y+1 FROM tab WHERE y+1!=5
15740 ** UNION ALL
15741 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
15742 ** ORDER BY 1
15743 **
15744 ** We call this the "compound-subquery flattening".
15745 */
15746 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
15747 Select *pNew;
15748 ExprList *pOrderBy = p->pOrderBy;
15749 Expr *pLimit = p->pLimit;
15750 Expr *pOffset = p->pOffset;
15751 Select *pPrior = p->pPrior;
15752 p->pOrderBy = 0;
15753 p->pSrc = 0;
15754 p->pPrior = 0;
15755 p->pLimit = 0;
15756 p->pOffset = 0;
15757 pNew = sqlite3SelectDup(db, p, 0);
15758 sqlite3SelectSetName(pNew, pSub->zSelName);
15759 p->pOffset = pOffset;
15760 p->pLimit = pLimit;
15761 p->pOrderBy = pOrderBy;
15762 p->pSrc = pSrc;
15763 p->op = TK_ALL;
15764 if( pNew==0 ){
15765 p->pPrior = pPrior;
15766 }else{
15767 pNew->pPrior = pPrior;
15768 if( pPrior ) pPrior->pNext = pNew;
15769 pNew->pNext = p;
15770 p->pPrior = pNew;
15771 SELECTTRACE(2,pParse,p,
15772 ("compound-subquery flattener creates %s.%p as peer\n",
15773 pNew->zSelName, pNew));
15774 }
15775 if( db->mallocFailed ) return 1;
15776 }
15777
15778 /* Begin flattening the iFrom-th entry of the FROM clause
15779 ** in the outer query.
15780 */
15781 pSub = pSub1 = pSubitem->pSelect;
15782
15783 /* Delete the transient table structure associated with the
15784 ** subquery
15785 */
15786 sqlite3DbFree(db, pSubitem->zDatabase);
15787 sqlite3DbFree(db, pSubitem->zName);
15788 sqlite3DbFree(db, pSubitem->zAlias);
15789 pSubitem->zDatabase = 0;
15790 pSubitem->zName = 0;
15791 pSubitem->zAlias = 0;
15792 pSubitem->pSelect = 0;
15793
15794 /* Defer deleting the Table object associated with the
15795 ** subquery until code generation is
15796 ** complete, since there may still exist Expr.pTab entries that
15797 ** refer to the subquery even after flattening. Ticket #3346.
15798 **
15799 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
15800 */
15801 if( ALWAYS(pSubitem->pTab!=0) ){
15802 Table *pTabToDel = pSubitem->pTab;
15803 if( pTabToDel->nTabRef==1 ){
15804 Parse *pToplevel = sqlite3ParseToplevel(pParse);
15805 pTabToDel->pNextZombie = pToplevel->pZombieTab;
15806 pToplevel->pZombieTab = pTabToDel;
15807 }else{
15808 pTabToDel->nTabRef--;
15809 }
15810 pSubitem->pTab = 0;
15811 }
15812
15813 /* The following loop runs once for each term in a compound-subquery
15814 ** flattening (as described above). If we are doing a different kind
15815 ** of flattening - a flattening other than a compound-subquery flattening -
15816 ** then this loop only runs once.
15817 **
15818 ** This loop moves all of the FROM elements of the subquery into the
15819 ** the FROM clause of the outer query. Before doing this, remember
15820 ** the cursor number for the original outer query FROM element in
15821 ** iParent. The iParent cursor will never be used. Subsequent code
15822 ** will scan expressions looking for iParent references and replace
15823 ** those references with expressions that resolve to the subquery FROM
15824 ** elements we are now copying in.
15825 */
15826 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
15827 int nSubSrc;
15828 u8 jointype = 0;
15829 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
15830 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
15831 pSrc = pParent->pSrc; /* FROM clause of the outer query */
15832
15833 if( pSrc ){
15834 assert( pParent==p ); /* First time through the loop */
15835 jointype = pSubitem->fg.jointype;
15836 }else{
15837 assert( pParent!=p ); /* 2nd and subsequent times through the loop */
15838 pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
15839 if( pSrc==0 ){
15840 assert( db->mallocFailed );
15841 break;
15842 }
15843 }
15844
15845 /* The subquery uses a single slot of the FROM clause of the outer
15846 ** query. If the subquery has more than one element in its FROM clause,
15847 ** then expand the outer query to make space for it to hold all elements
15848 ** of the subquery.
15849 **
15850 ** Example:
15851 **
15852 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
15853 **
15854 ** The outer query has 3 slots in its FROM clause. One slot of the
15855 ** outer query (the middle slot) is used by the subquery. The next
15856 ** block of code will expand the outer query FROM clause to 4 slots.
15857 ** The middle slot is expanded to two slots in order to make space
15858 ** for the two elements in the FROM clause of the subquery.
15859 */
15860 if( nSubSrc>1 ){
15861 pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
15862 if( db->mallocFailed ){
15863 break;
15864 }
15865 }
15866
15867 /* Transfer the FROM clause terms from the subquery into the
15868 ** outer query.
15869 */
15870 for(i=0; i<nSubSrc; i++){
15871 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
15872 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
15873 pSrc->a[i+iFrom] = pSubSrc->a[i];
15874 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
15875 }
15876 pSrc->a[iFrom].fg.jointype = jointype;
15877
15878 /* Now begin substituting subquery result set expressions for
15879 ** references to the iParent in the outer query.
15880 **
15881 ** Example:
15882 **
15883 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
15884 ** \ \_____________ subquery __________/ /
15885 ** \_____________________ outer query ______________________________/
15886 **
15887 ** We look at every expression in the outer query and every place we see
15888 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
15889 */
15890 pList = pParent->pEList;
15891 for(i=0; i<pList->nExpr; i++){
15892 if( pList->a[i].zName==0 ){
15893 char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
15894 sqlite3Dequote(zName);
15895 pList->a[i].zName = zName;
15896 }
15897 }
15898 if( pSub->pOrderBy ){
15899 /* At this point, any non-zero iOrderByCol values indicate that the
15900 ** ORDER BY column expression is identical to the iOrderByCol'th
15901 ** expression returned by SELECT statement pSub. Since these values
15902 ** do not necessarily correspond to columns in SELECT statement pParent,
15903 ** zero them before transfering the ORDER BY clause.
15904 **
15905 ** Not doing this may cause an error if a subsequent call to this
15906 ** function attempts to flatten a compound sub-query into pParent
15907 ** (the only way this can happen is if the compound sub-query is
15908 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
15909 ExprList *pOrderBy = pSub->pOrderBy;
15910 for(i=0; i<pOrderBy->nExpr; i++){
15911 pOrderBy->a[i].u.x.iOrderByCol = 0;
15912 }
15913 assert( pParent->pOrderBy==0 );
15914 assert( pSub->pPrior==0 );
15915 pParent->pOrderBy = pOrderBy;
15916 pSub->pOrderBy = 0;
15917 }
15918 pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
15919 if( subqueryIsAgg ){
15920 assert( pParent->pHaving==0 );
15921 pParent->pHaving = pParent->pWhere;
15922 pParent->pWhere = pWhere;
15923 pParent->pHaving = sqlite3ExprAnd(db,
15924 sqlite3ExprDup(db, pSub->pHaving, 0), pParent->pHaving
15925 );
15926 assert( pParent->pGroupBy==0 );
15927 pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
15928 }else{
15929 pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
15930 }
15931 substSelect(pParse, pParent, iParent, pSub->pEList, 0);
15932
15933 /* The flattened query is distinct if either the inner or the
15934 ** outer query is distinct.
15935 */
15936 pParent->selFlags |= pSub->selFlags & SF_Distinct;
15937
15938 /*
15939 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
15940 **
15941 ** One is tempted to try to add a and b to combine the limits. But this
15942 ** does not work if either limit is negative.
15943 */
15944 if( pSub->pLimit ){
15945 pParent->pLimit = pSub->pLimit;
15946 pSub->pLimit = 0;
15947 }
15948 }
15949
15950 /* Finially, delete what is left of the subquery and return
15951 ** success.
15952 */
15953 sqlite3SelectDelete(db, pSub1);
15954
15955 #if SELECTTRACE_ENABLED
15956 if( sqlite3SelectTrace & 0x100 ){
15957 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
15958 sqlite3TreeViewSelect(0, p, 0);
15959 }
15960 #endif
15961
15962 return 1;
15963 }
15964 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
15965
15966
15967
15968 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
15969 /*
15970 ** Make copies of relevant WHERE clause terms of the outer query into
15971 ** the WHERE clause of subquery. Example:
15972 **
15973 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
15974 **
15975 ** Transformed into:
15976 **
15977 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
15978 ** WHERE x=5 AND y=10;
15979 **
15980 ** The hope is that the terms added to the inner query will make it more
15981 ** efficient.
15982 **
15983 ** Do not attempt this optimization if:
15984 **
15985 ** (1) The inner query is an aggregate. (In that case, we'd really want
15986 ** to copy the outer WHERE-clause terms onto the HAVING clause of the
15987 ** inner query. But they probably won't help there so do not bother.)
15988 **
15989 ** (2) The inner query is the recursive part of a common table expression.
15990 **
15991 ** (3) The inner query has a LIMIT clause (since the changes to the WHERE
15992 ** close would change the meaning of the LIMIT).
15993 **
15994 ** (4) The inner query is the right operand of a LEFT JOIN. (The caller
15995 ** enforces this restriction since this routine does not have enough
15996 ** information to know.)
15997 **
15998 ** (5) The WHERE clause expression originates in the ON or USING clause
15999 ** of a LEFT JOIN.
16000 **
16001 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
16002 ** terms are duplicated into the subquery.
16003 */
16004 static int pushDownWhereTerms(
16005 Parse *pParse, /* Parse context (for malloc() and error reporting) */
16006 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
16007 Expr *pWhere, /* The WHERE clause of the outer query */
16008 int iCursor /* Cursor number of the subquery */
16009 ){
16010 Expr *pNew;
16011 int nChng = 0;
16012 Select *pX; /* For looping over compound SELECTs in pSubq */
16013 if( pWhere==0 ) return 0;
16014 for(pX=pSubq; pX; pX=pX->pPrior){
16015 if( (pX->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){
16016 testcase( pX->selFlags & SF_Aggregate );
16017 testcase( pX->selFlags & SF_Recursive );
16018 testcase( pX!=pSubq );
16019 return 0; /* restrictions (1) and (2) */
16020 }
16021 }
16022 if( pSubq->pLimit!=0 ){
16023 return 0; /* restriction (3) */
16024 }
16025 while( pWhere->op==TK_AND ){
16026 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
16027 pWhere = pWhere->pLeft;
16028 }
16029 if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction 5 */
16030 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
16031 nChng++;
16032 while( pSubq ){
16033 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
16034 pNew = substExpr(pParse, pNew, iCursor, pSubq->pEList);
16035 pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
16036 pSubq = pSubq->pPrior;
16037 }
16038 }
16039 return nChng;
16040 }
16041 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
16042
16043 /*
16044 ** Based on the contents of the AggInfo structure indicated by the first
16045 ** argument, this function checks if the following are true:
16046 **
16047 ** * the query contains just a single aggregate function,
16048 ** * the aggregate function is either min() or max(), and
16049 ** * the argument to the aggregate function is a column value.
16050 **
16051 ** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
16052 ** is returned as appropriate. Also, *ppMinMax is set to point to the
16053 ** list of arguments passed to the aggregate before returning.
16054 **
16055 ** Or, if the conditions above are not met, *ppMinMax is set to 0 and
16056 ** WHERE_ORDERBY_NORMAL is returned.
16057 */
16058 static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
16059 int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
16060
16061 *ppMinMax = 0;
16062 if( pAggInfo->nFunc==1 ){
16063 Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
16064 ExprList *pEList = pExpr->x.pList; /* Arguments to agg function */
16065
16066 assert( pExpr->op==TK_AGG_FUNCTION );
16067 if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
16068 const char *zFunc = pExpr->u.zToken;
16069 if( sqlite3StrICmp(zFunc, "min")==0 ){
16070 eRet = WHERE_ORDERBY_MIN;
16071 *ppMinMax = pEList;
16072 }else if( sqlite3StrICmp(zFunc, "max")==0 ){
16073 eRet = WHERE_ORDERBY_MAX;
16074 *ppMinMax = pEList;
16075 }
16076 }
16077 }
16078
16079 assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
16080 return eRet;
16081 }
16082
16083 /*
16084 ** The select statement passed as the first argument is an aggregate query.
16085 ** The second argument is the associated aggregate-info object. This
16086 ** function tests if the SELECT is of the form:
16087 **
16088 ** SELECT count(*) FROM <tbl>
16089 **
16090 ** where table is a database table, not a sub-select or view. If the query
16091 ** does match this pattern, then a pointer to the Table object representing
16092 ** <tbl> is returned. Otherwise, 0 is returned.
16093 */
16094 static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
16095 Table *pTab;
16096 Expr *pExpr;
16097
16098 assert( !p->pGroupBy );
16099
16100 if( p->pWhere || p->pEList->nExpr!=1
16101 || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
16102 ){
16103 return 0;
16104 }
16105 pTab = p->pSrc->a[0].pTab;
16106 pExpr = p->pEList->a[0].pExpr;
16107 assert( pTab && !pTab->pSelect && pExpr );
16108
16109 if( IsVirtual(pTab) ) return 0;
16110 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
16111 if( NEVER(pAggInfo->nFunc==0) ) return 0;
16112 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
16113 if( pExpr->flags&EP_Distinct ) return 0;
16114
16115 return pTab;
16116 }
16117
16118 /*
16119 ** If the source-list item passed as an argument was augmented with an
16120 ** INDEXED BY clause, then try to locate the specified index. If there
16121 ** was such a clause and the named index cannot be found, return
16122 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
16123 ** pFrom->pIndex and return SQLITE_OK.
16124 */
16125 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pF rom){
16126 if( pFrom->pTab && pFrom->fg.isIndexedBy ){
16127 Table *pTab = pFrom->pTab;
16128 char *zIndexedBy = pFrom->u1.zIndexedBy;
16129 Index *pIdx;
16130 for(pIdx=pTab->pIndex;
16131 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
16132 pIdx=pIdx->pNext
16133 );
16134 if( !pIdx ){
16135 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
16136 pParse->checkSchema = 1;
16137 return SQLITE_ERROR;
16138 }
16139 pFrom->pIBIndex = pIdx;
16140 }
16141 return SQLITE_OK;
16142 }
16143 /*
16144 ** Detect compound SELECT statements that use an ORDER BY clause with
16145 ** an alternative collating sequence.
16146 **
16147 ** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
16148 **
16149 ** These are rewritten as a subquery:
16150 **
16151 ** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
16152 ** ORDER BY ... COLLATE ...
16153 **
16154 ** This transformation is necessary because the multiSelectOrderBy() routine
16155 ** above that generates the code for a compound SELECT with an ORDER BY clause
16156 ** uses a merge algorithm that requires the same collating sequence on the
16157 ** result columns as on the ORDER BY clause. See ticket
16158 ** http://www.sqlite.org/src/info/6709574d2a
16159 **
16160 ** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
16161 ** The UNION ALL operator works fine with multiSelectOrderBy() even when
16162 ** there are COLLATE terms in the ORDER BY.
16163 */
16164 static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
16165 int i;
16166 Select *pNew;
16167 Select *pX;
16168 sqlite3 *db;
16169 struct ExprList_item *a;
16170 SrcList *pNewSrc;
16171 Parse *pParse;
16172 Token dummy;
16173
16174 if( p->pPrior==0 ) return WRC_Continue;
16175 if( p->pOrderBy==0 ) return WRC_Continue;
16176 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
16177 if( pX==0 ) return WRC_Continue;
16178 a = p->pOrderBy->a;
16179 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
16180 if( a[i].pExpr->flags & EP_Collate ) break;
16181 }
16182 if( i<0 ) return WRC_Continue;
16183
16184 /* If we reach this point, that means the transformation is required. */
16185
16186 pParse = pWalker->pParse;
16187 db = pParse->db;
16188 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
16189 if( pNew==0 ) return WRC_Abort;
16190 memset(&dummy, 0, sizeof(dummy));
16191 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
16192 if( pNewSrc==0 ) return WRC_Abort;
16193 *pNew = *p;
16194 p->pSrc = pNewSrc;
16195 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
16196 p->op = TK_SELECT;
16197 p->pWhere = 0;
16198 pNew->pGroupBy = 0;
16199 pNew->pHaving = 0;
16200 pNew->pOrderBy = 0;
16201 p->pPrior = 0;
16202 p->pNext = 0;
16203 p->pWith = 0;
16204 p->selFlags &= ~SF_Compound;
16205 assert( (p->selFlags & SF_Converted)==0 );
16206 p->selFlags |= SF_Converted;
16207 assert( pNew->pPrior!=0 );
16208 pNew->pPrior->pNext = pNew;
16209 pNew->pLimit = 0;
16210 pNew->pOffset = 0;
16211 return WRC_Continue;
16212 }
16213
16214 /*
16215 ** Check to see if the FROM clause term pFrom has table-valued function
16216 ** arguments. If it does, leave an error message in pParse and return
16217 ** non-zero, since pFrom is not allowed to be a table-valued function.
16218 */
16219 static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
16220 if( pFrom->fg.isTabFunc ){
16221 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
16222 return 1;
16223 }
16224 return 0;
16225 }
16226
16227 #ifndef SQLITE_OMIT_CTE
16228 /*
16229 ** Argument pWith (which may be NULL) points to a linked list of nested
16230 ** WITH contexts, from inner to outermost. If the table identified by
16231 ** FROM clause element pItem is really a common-table-expression (CTE)
16232 ** then return a pointer to the CTE definition for that table. Otherwise
16233 ** return NULL.
16234 **
16235 ** If a non-NULL value is returned, set *ppContext to point to the With
16236 ** object that the returned CTE belongs to.
16237 */
16238 static struct Cte *searchWith(
16239 With *pWith, /* Current innermost WITH clause */
16240 struct SrcList_item *pItem, /* FROM clause element to resolve */
16241 With **ppContext /* OUT: WITH clause return value belongs to */
16242 ){
16243 const char *zName;
16244 if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
16245 With *p;
16246 for(p=pWith; p; p=p->pOuter){
16247 int i;
16248 for(i=0; i<p->nCte; i++){
16249 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
16250 *ppContext = p;
16251 return &p->a[i];
16252 }
16253 }
16254 }
16255 }
16256 return 0;
16257 }
16258
16259 /* The code generator maintains a stack of active WITH clauses
16260 ** with the inner-most WITH clause being at the top of the stack.
16261 **
16262 ** This routine pushes the WITH clause passed as the second argument
16263 ** onto the top of the stack. If argument bFree is true, then this
16264 ** WITH clause will never be popped from the stack. In this case it
16265 ** should be freed along with the Parse object. In other cases, when
16266 ** bFree==0, the With object will be freed along with the SELECT
16267 ** statement with which it is associated.
16268 */
16269 SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
16270 assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
16271 if( pWith ){
16272 assert( pParse->pWith!=pWith );
16273 pWith->pOuter = pParse->pWith;
16274 pParse->pWith = pWith;
16275 if( bFree ) pParse->pWithToFree = pWith;
16276 }
16277 }
16278
16279 /*
16280 ** This function checks if argument pFrom refers to a CTE declared by
16281 ** a WITH clause on the stack currently maintained by the parser. And,
16282 ** if currently processing a CTE expression, if it is a recursive
16283 ** reference to the current CTE.
16284 **
16285 ** If pFrom falls into either of the two categories above, pFrom->pTab
16286 ** and other fields are populated accordingly. The caller should check
16287 ** (pFrom->pTab!=0) to determine whether or not a successful match
16288 ** was found.
16289 **
16290 ** Whether or not a match is found, SQLITE_OK is returned if no error
16291 ** occurs. If an error does occur, an error message is stored in the
16292 ** parser and some error code other than SQLITE_OK returned.
16293 */
16294 static int withExpand(
16295 Walker *pWalker,
16296 struct SrcList_item *pFrom
16297 ){
16298 Parse *pParse = pWalker->pParse;
16299 sqlite3 *db = pParse->db;
16300 struct Cte *pCte; /* Matched CTE (or NULL if no match) */
16301 With *pWith; /* WITH clause that pCte belongs to */
16302
16303 assert( pFrom->pTab==0 );
16304
16305 pCte = searchWith(pParse->pWith, pFrom, &pWith);
16306 if( pCte ){
16307 Table *pTab;
16308 ExprList *pEList;
16309 Select *pSel;
16310 Select *pLeft; /* Left-most SELECT statement */
16311 int bMayRecursive; /* True if compound joined by UNION [ALL] */
16312 With *pSavedWith; /* Initial value of pParse->pWith */
16313
16314 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
16315 ** recursive reference to CTE pCte. Leave an error in pParse and return
16316 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
16317 ** In this case, proceed. */
16318 if( pCte->zCteErr ){
16319 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
16320 return SQLITE_ERROR;
16321 }
16322 if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
16323
16324 assert( pFrom->pTab==0 );
16325 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
16326 if( pTab==0 ) return WRC_Abort;
16327 pTab->nTabRef = 1;
16328 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
16329 pTab->iPKey = -1;
16330 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
16331 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
16332 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
16333 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
16334 assert( pFrom->pSelect );
16335
16336 /* Check if this is a recursive CTE. */
16337 pSel = pFrom->pSelect;
16338 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
16339 if( bMayRecursive ){
16340 int i;
16341 SrcList *pSrc = pFrom->pSelect->pSrc;
16342 for(i=0; i<pSrc->nSrc; i++){
16343 struct SrcList_item *pItem = &pSrc->a[i];
16344 if( pItem->zDatabase==0
16345 && pItem->zName!=0
16346 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
16347 ){
16348 pItem->pTab = pTab;
16349 pItem->fg.isRecursive = 1;
16350 pTab->nTabRef++;
16351 pSel->selFlags |= SF_Recursive;
16352 }
16353 }
16354 }
16355
16356 /* Only one recursive reference is permitted. */
16357 if( pTab->nTabRef>2 ){
16358 sqlite3ErrorMsg(
16359 pParse, "multiple references to recursive table: %s", pCte->zName
16360 );
16361 return SQLITE_ERROR;
16362 }
16363 assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef= =2 ));
16364
16365 pCte->zCteErr = "circular reference: %s";
16366 pSavedWith = pParse->pWith;
16367 pParse->pWith = pWith;
16368 if( bMayRecursive ){
16369 Select *pPrior = pSel->pPrior;
16370 assert( pPrior->pWith==0 );
16371 pPrior->pWith = pSel->pWith;
16372 sqlite3WalkSelect(pWalker, pPrior);
16373 pPrior->pWith = 0;
16374 }else{
16375 sqlite3WalkSelect(pWalker, pSel);
16376 }
16377 pParse->pWith = pWith;
16378
16379 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
16380 pEList = pLeft->pEList;
16381 if( pCte->pCols ){
16382 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
16383 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
16384 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
16385 );
16386 pParse->pWith = pSavedWith;
16387 return SQLITE_ERROR;
16388 }
16389 pEList = pCte->pCols;
16390 }
16391
16392 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
16393 if( bMayRecursive ){
16394 if( pSel->selFlags & SF_Recursive ){
16395 pCte->zCteErr = "multiple recursive references: %s";
16396 }else{
16397 pCte->zCteErr = "recursive reference in a subquery: %s";
16398 }
16399 sqlite3WalkSelect(pWalker, pSel);
16400 }
16401 pCte->zCteErr = 0;
16402 pParse->pWith = pSavedWith;
16403 }
16404
16405 return SQLITE_OK;
16406 }
16407 #endif
16408
16409 #ifndef SQLITE_OMIT_CTE
16410 /*
16411 ** If the SELECT passed as the second argument has an associated WITH
16412 ** clause, pop it from the stack stored as part of the Parse object.
16413 **
16414 ** This function is used as the xSelectCallback2() callback by
16415 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
16416 ** names and other FROM clause elements.
16417 */
16418 static void selectPopWith(Walker *pWalker, Select *p){
16419 Parse *pParse = pWalker->pParse;
16420 if( pParse->pWith && p->pPrior==0 ){
16421 With *pWith = findRightmost(p)->pWith;
16422 if( pWith!=0 ){
16423 assert( pParse->pWith==pWith );
16424 pParse->pWith = pWith->pOuter;
16425 }
16426 }
16427 }
16428 #else
16429 #define selectPopWith 0
16430 #endif
16431
16432 /*
16433 ** This routine is a Walker callback for "expanding" a SELECT statement.
16434 ** "Expanding" means to do the following:
16435 **
16436 ** (1) Make sure VDBE cursor numbers have been assigned to every
16437 ** element of the FROM clause.
16438 **
16439 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
16440 ** defines FROM clause. When views appear in the FROM clause,
16441 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
16442 ** that implements the view. A copy is made of the view's SELECT
16443 ** statement so that we can freely modify or delete that statement
16444 ** without worrying about messing up the persistent representation
16445 ** of the view.
16446 **
16447 ** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
16448 ** on joins and the ON and USING clause of joins.
16449 **
16450 ** (4) Scan the list of columns in the result set (pEList) looking
16451 ** for instances of the "*" operator or the TABLE.* operator.
16452 ** If found, expand each "*" to be every column in every table
16453 ** and TABLE.* to be every column in TABLE.
16454 **
16455 */
16456 static int selectExpander(Walker *pWalker, Select *p){
16457 Parse *pParse = pWalker->pParse;
16458 int i, j, k;
16459 SrcList *pTabList;
16460 ExprList *pEList;
16461 struct SrcList_item *pFrom;
16462 sqlite3 *db = pParse->db;
16463 Expr *pE, *pRight, *pExpr;
16464 u16 selFlags = p->selFlags;
16465
16466 p->selFlags |= SF_Expanded;
16467 if( db->mallocFailed ){
16468 return WRC_Abort;
16469 }
16470 if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
16471 return WRC_Prune;
16472 }
16473 pTabList = p->pSrc;
16474 pEList = p->pEList;
16475 if( p->pWith ){
16476 sqlite3WithPush(pParse, p->pWith, 0);
16477 }
16478
16479 /* Make sure cursor numbers have been assigned to all entries in
16480 ** the FROM clause of the SELECT statement.
16481 */
16482 sqlite3SrcListAssignCursors(pParse, pTabList);
16483
16484 /* Look up every table named in the FROM clause of the select. If
16485 ** an entry of the FROM clause is a subquery instead of a table or view,
16486 ** then create a transient table structure to describe the subquery.
16487 */
16488 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
16489 Table *pTab;
16490 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
16491 if( pFrom->fg.isRecursive ) continue;
16492 assert( pFrom->pTab==0 );
16493 #ifndef SQLITE_OMIT_CTE
16494 if( withExpand(pWalker, pFrom) ) return WRC_Abort;
16495 if( pFrom->pTab ) {} else
16496 #endif
16497 if( pFrom->zName==0 ){
16498 #ifndef SQLITE_OMIT_SUBQUERY
16499 Select *pSel = pFrom->pSelect;
16500 /* A sub-query in the FROM clause of a SELECT */
16501 assert( pSel!=0 );
16502 assert( pFrom->pTab==0 );
16503 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
16504 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
16505 if( pTab==0 ) return WRC_Abort;
16506 pTab->nTabRef = 1;
16507 pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
16508 while( pSel->pPrior ){ pSel = pSel->pPrior; }
16509 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
16510 pTab->iPKey = -1;
16511 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
16512 pTab->tabFlags |= TF_Ephemeral;
16513 #endif
16514 }else{
16515 /* An ordinary table or view name in the FROM clause */
16516 assert( pFrom->pTab==0 );
16517 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
16518 if( pTab==0 ) return WRC_Abort;
16519 if( pTab->nTabRef>=0xffff ){
16520 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
16521 pTab->zName);
16522 pFrom->pTab = 0;
16523 return WRC_Abort;
16524 }
16525 pTab->nTabRef++;
16526 if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
16527 return WRC_Abort;
16528 }
16529 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
16530 if( IsVirtual(pTab) || pTab->pSelect ){
16531 i16 nCol;
16532 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
16533 assert( pFrom->pSelect==0 );
16534 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
16535 sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
16536 nCol = pTab->nCol;
16537 pTab->nCol = -1;
16538 sqlite3WalkSelect(pWalker, pFrom->pSelect);
16539 pTab->nCol = nCol;
16540 }
16541 #endif
16542 }
16543
16544 /* Locate the index named by the INDEXED BY clause, if any. */
16545 if( sqlite3IndexedByLookup(pParse, pFrom) ){
16546 return WRC_Abort;
16547 }
16548 }
16549
16550 /* Process NATURAL keywords, and ON and USING clauses of joins.
16551 */
16552 if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
16553 return WRC_Abort;
16554 }
16555
16556 /* For every "*" that occurs in the column list, insert the names of
16557 ** all columns in all tables. And for every TABLE.* insert the names
16558 ** of all columns in TABLE. The parser inserted a special expression
16559 ** with the TK_ASTERISK operator for each "*" that it found in the column
16560 ** list. The following code just has to locate the TK_ASTERISK
16561 ** expressions and expand each one to the list of all columns in
16562 ** all tables.
16563 **
16564 ** The first loop just checks to see if there are any "*" operators
16565 ** that need expanding.
16566 */
16567 for(k=0; k<pEList->nExpr; k++){
16568 pE = pEList->a[k].pExpr;
16569 if( pE->op==TK_ASTERISK ) break;
16570 assert( pE->op!=TK_DOT || pE->pRight!=0 );
16571 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
16572 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
16573 }
16574 if( k<pEList->nExpr ){
16575 /*
16576 ** If we get here it means the result set contains one or more "*"
16577 ** operators that need to be expanded. Loop through each expression
16578 ** in the result set and expand them one by one.
16579 */
16580 struct ExprList_item *a = pEList->a;
16581 ExprList *pNew = 0;
16582 int flags = pParse->db->flags;
16583 int longNames = (flags & SQLITE_FullColNames)!=0
16584 && (flags & SQLITE_ShortColNames)==0;
16585
16586 for(k=0; k<pEList->nExpr; k++){
16587 pE = a[k].pExpr;
16588 pRight = pE->pRight;
16589 assert( pE->op!=TK_DOT || pRight!=0 );
16590 if( pE->op!=TK_ASTERISK
16591 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
16592 ){
16593 /* This particular expression does not need to be expanded.
16594 */
16595 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
16596 if( pNew ){
16597 pNew->a[pNew->nExpr-1].zName = a[k].zName;
16598 pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
16599 a[k].zName = 0;
16600 a[k].zSpan = 0;
16601 }
16602 a[k].pExpr = 0;
16603 }else{
16604 /* This expression is a "*" or a "TABLE.*" and needs to be
16605 ** expanded. */
16606 int tableSeen = 0; /* Set to 1 when TABLE matches */
16607 char *zTName = 0; /* text of name of TABLE */
16608 if( pE->op==TK_DOT ){
16609 assert( pE->pLeft!=0 );
16610 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
16611 zTName = pE->pLeft->u.zToken;
16612 }
16613 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
16614 Table *pTab = pFrom->pTab;
16615 Select *pSub = pFrom->pSelect;
16616 char *zTabName = pFrom->zAlias;
16617 const char *zSchemaName = 0;
16618 int iDb;
16619 if( zTabName==0 ){
16620 zTabName = pTab->zName;
16621 }
16622 if( db->mallocFailed ) break;
16623 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
16624 pSub = 0;
16625 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
16626 continue;
16627 }
16628 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
16629 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
16630 }
16631 for(j=0; j<pTab->nCol; j++){
16632 char *zName = pTab->aCol[j].zName;
16633 char *zColname; /* The computed column name */
16634 char *zToFree; /* Malloced string that needs to be freed */
16635 Token sColname; /* Computed column name as a token */
16636
16637 assert( zName );
16638 if( zTName && pSub
16639 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
16640 ){
16641 continue;
16642 }
16643
16644 /* If a column is marked as 'hidden', omit it from the expanded
16645 ** result-set list unless the SELECT has the SF_IncludeHidden
16646 ** bit set.
16647 */
16648 if( (p->selFlags & SF_IncludeHidden)==0
16649 && IsHiddenColumn(&pTab->aCol[j])
16650 ){
16651 continue;
16652 }
16653 tableSeen = 1;
16654
16655 if( i>0 && zTName==0 ){
16656 if( (pFrom->fg.jointype & JT_NATURAL)!=0
16657 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
16658 ){
16659 /* In a NATURAL join, omit the join columns from the
16660 ** table to the right of the join */
16661 continue;
16662 }
16663 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
16664 /* In a join with a USING clause, omit columns in the
16665 ** using clause from the table on the right. */
16666 continue;
16667 }
16668 }
16669 pRight = sqlite3Expr(db, TK_ID, zName);
16670 zColname = zName;
16671 zToFree = 0;
16672 if( longNames || pTabList->nSrc>1 ){
16673 Expr *pLeft;
16674 pLeft = sqlite3Expr(db, TK_ID, zTabName);
16675 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
16676 if( zSchemaName ){
16677 pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
16678 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr);
16679 }
16680 if( longNames ){
16681 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
16682 zToFree = zColname;
16683 }
16684 }else{
16685 pExpr = pRight;
16686 }
16687 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
16688 sqlite3TokenInit(&sColname, zColname);
16689 sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
16690 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
16691 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
16692 if( pSub ){
16693 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
16694 testcase( pX->zSpan==0 );
16695 }else{
16696 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
16697 zSchemaName, zTabName, zColname);
16698 testcase( pX->zSpan==0 );
16699 }
16700 pX->bSpanIsTab = 1;
16701 }
16702 sqlite3DbFree(db, zToFree);
16703 }
16704 }
16705 if( !tableSeen ){
16706 if( zTName ){
16707 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
16708 }else{
16709 sqlite3ErrorMsg(pParse, "no tables specified");
16710 }
16711 }
16712 }
16713 }
16714 sqlite3ExprListDelete(db, pEList);
16715 p->pEList = pNew;
16716 }
16717 #if SQLITE_MAX_COLUMN
16718 if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
16719 sqlite3ErrorMsg(pParse, "too many columns in result set");
16720 return WRC_Abort;
16721 }
16722 #endif
16723 return WRC_Continue;
16724 }
16725
16726 /*
16727 ** No-op routine for the parse-tree walker.
16728 **
16729 ** When this routine is the Walker.xExprCallback then expression trees
16730 ** are walked without any actions being taken at each node. Presumably,
16731 ** when this routine is used for Walker.xExprCallback then
16732 ** Walker.xSelectCallback is set to do something useful for every
16733 ** subquery in the parser tree.
16734 */
16735 SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
16736 UNUSED_PARAMETER2(NotUsed, NotUsed2);
16737 return WRC_Continue;
16738 }
16739
16740 /*
16741 ** This routine "expands" a SELECT statement and all of its subqueries.
16742 ** For additional information on what it means to "expand" a SELECT
16743 ** statement, see the comment on the selectExpand worker callback above.
16744 **
16745 ** Expanding a SELECT statement is the first step in processing a
16746 ** SELECT statement. The SELECT statement must be expanded before
16747 ** name resolution is performed.
16748 **
16749 ** If anything goes wrong, an error message is written into pParse.
16750 ** The calling function can detect the problem by looking at pParse->nErr
16751 ** and/or pParse->db->mallocFailed.
16752 */
16753 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
16754 Walker w;
16755 memset(&w, 0, sizeof(w));
16756 w.xExprCallback = sqlite3ExprWalkNoop;
16757 w.pParse = pParse;
16758 if( pParse->hasCompound ){
16759 w.xSelectCallback = convertCompoundSelectToSubquery;
16760 sqlite3WalkSelect(&w, pSelect);
16761 }
16762 w.xSelectCallback = selectExpander;
16763 w.xSelectCallback2 = selectPopWith;
16764 sqlite3WalkSelect(&w, pSelect);
16765 }
16766
16767
16768 #ifndef SQLITE_OMIT_SUBQUERY
16769 /*
16770 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
16771 ** interface.
16772 **
16773 ** For each FROM-clause subquery, add Column.zType and Column.zColl
16774 ** information to the Table structure that represents the result set
16775 ** of that subquery.
16776 **
16777 ** The Table structure that represents the result set was constructed
16778 ** by selectExpander() but the type and collation information was omitted
16779 ** at that point because identifiers had not yet been resolved. This
16780 ** routine is called after identifier resolution.
16781 */
16782 static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
16783 Parse *pParse;
16784 int i;
16785 SrcList *pTabList;
16786 struct SrcList_item *pFrom;
16787
16788 assert( p->selFlags & SF_Resolved );
16789 assert( (p->selFlags & SF_HasTypeInfo)==0 );
16790 p->selFlags |= SF_HasTypeInfo;
16791 pParse = pWalker->pParse;
16792 pTabList = p->pSrc;
16793 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
16794 Table *pTab = pFrom->pTab;
16795 assert( pTab!=0 );
16796 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
16797 /* A sub-query in the FROM clause of a SELECT */
16798 Select *pSel = pFrom->pSelect;
16799 if( pSel ){
16800 while( pSel->pPrior ) pSel = pSel->pPrior;
16801 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel);
16802 }
16803 }
16804 }
16805 }
16806 #endif
16807
16808
16809 /*
16810 ** This routine adds datatype and collating sequence information to
16811 ** the Table structures of all FROM-clause subqueries in a
16812 ** SELECT statement.
16813 **
16814 ** Use this routine after name resolution.
16815 */
16816 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
16817 #ifndef SQLITE_OMIT_SUBQUERY
16818 Walker w;
16819 memset(&w, 0, sizeof(w));
16820 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
16821 w.xExprCallback = sqlite3ExprWalkNoop;
16822 w.pParse = pParse;
16823 sqlite3WalkSelect(&w, pSelect);
16824 #endif
16825 }
16826
16827
16828 /*
16829 ** This routine sets up a SELECT statement for processing. The
16830 ** following is accomplished:
16831 **
16832 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
16833 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
16834 ** * ON and USING clauses are shifted into WHERE statements
16835 ** * Wildcards "*" and "TABLE.*" in result sets are expanded.
16836 ** * Identifiers in expression are matched to tables.
16837 **
16838 ** This routine acts recursively on all subqueries within the SELECT.
16839 */
16840 SQLITE_PRIVATE void sqlite3SelectPrep(
16841 Parse *pParse, /* The parser context */
16842 Select *p, /* The SELECT statement being coded. */
16843 NameContext *pOuterNC /* Name context for container */
16844 ){
16845 sqlite3 *db;
16846 if( NEVER(p==0) ) return;
16847 db = pParse->db;
16848 if( db->mallocFailed ) return;
16849 if( p->selFlags & SF_HasTypeInfo ) return;
16850 sqlite3SelectExpand(pParse, p);
16851 if( pParse->nErr || db->mallocFailed ) return;
16852 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
16853 if( pParse->nErr || db->mallocFailed ) return;
16854 sqlite3SelectAddTypeInfo(pParse, p);
16855 }
16856
16857 /*
16858 ** Reset the aggregate accumulator.
16859 **
16860 ** The aggregate accumulator is a set of memory cells that hold
16861 ** intermediate results while calculating an aggregate. This
16862 ** routine generates code that stores NULLs in all of those memory
16863 ** cells.
16864 */
16865 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
16866 Vdbe *v = pParse->pVdbe;
16867 int i;
16868 struct AggInfo_func *pFunc;
16869 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
16870 if( nReg==0 ) return;
16871 #ifdef SQLITE_DEBUG
16872 /* Verify that all AggInfo registers are within the range specified by
16873 ** AggInfo.mnReg..AggInfo.mxReg */
16874 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
16875 for(i=0; i<pAggInfo->nColumn; i++){
16876 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
16877 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
16878 }
16879 for(i=0; i<pAggInfo->nFunc; i++){
16880 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
16881 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
16882 }
16883 #endif
16884 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
16885 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
16886 if( pFunc->iDistinct>=0 ){
16887 Expr *pE = pFunc->pExpr;
16888 assert( !ExprHasProperty(pE, EP_xIsSelect) );
16889 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
16890 sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
16891 "argument");
16892 pFunc->iDistinct = -1;
16893 }else{
16894 KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0, 0);
16895 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
16896 (char*)pKeyInfo, P4_KEYINFO);
16897 }
16898 }
16899 }
16900 }
16901
16902 /*
16903 ** Invoke the OP_AggFinalize opcode for every aggregate function
16904 ** in the AggInfo structure.
16905 */
16906 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
16907 Vdbe *v = pParse->pVdbe;
16908 int i;
16909 struct AggInfo_func *pF;
16910 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
16911 ExprList *pList = pF->pExpr->x.pList;
16912 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
16913 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
16914 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
16915 }
16916 }
16917
16918 /*
16919 ** Update the accumulator memory cells for an aggregate based on
16920 ** the current cursor position.
16921 */
16922 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
16923 Vdbe *v = pParse->pVdbe;
16924 int i;
16925 int regHit = 0;
16926 int addrHitTest = 0;
16927 struct AggInfo_func *pF;
16928 struct AggInfo_col *pC;
16929
16930 pAggInfo->directMode = 1;
16931 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
16932 int nArg;
16933 int addrNext = 0;
16934 int regAgg;
16935 ExprList *pList = pF->pExpr->x.pList;
16936 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
16937 if( pList ){
16938 nArg = pList->nExpr;
16939 regAgg = sqlite3GetTempRange(pParse, nArg);
16940 sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
16941 }else{
16942 nArg = 0;
16943 regAgg = 0;
16944 }
16945 if( pF->iDistinct>=0 ){
16946 addrNext = sqlite3VdbeMakeLabel(v);
16947 testcase( nArg==0 ); /* Error condition */
16948 testcase( nArg>1 ); /* Also an error */
16949 codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
16950 }
16951 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
16952 CollSeq *pColl = 0;
16953 struct ExprList_item *pItem;
16954 int j;
16955 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
16956 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
16957 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
16958 }
16959 if( !pColl ){
16960 pColl = pParse->db->pDfltColl;
16961 }
16962 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
16963 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
16964 }
16965 sqlite3VdbeAddOp3(v, OP_AggStep0, 0, regAgg, pF->iMem);
16966 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
16967 sqlite3VdbeChangeP5(v, (u8)nArg);
16968 sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
16969 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
16970 if( addrNext ){
16971 sqlite3VdbeResolveLabel(v, addrNext);
16972 sqlite3ExprCacheClear(pParse);
16973 }
16974 }
16975
16976 /* Before populating the accumulator registers, clear the column cache.
16977 ** Otherwise, if any of the required column values are already present
16978 ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
16979 ** to pC->iMem. But by the time the value is used, the original register
16980 ** may have been used, invalidating the underlying buffer holding the
16981 ** text or blob value. See ticket [883034dcb5].
16982 **
16983 ** Another solution would be to change the OP_SCopy used to copy cached
16984 ** values to an OP_Copy.
16985 */
16986 if( regHit ){
16987 addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
16988 }
16989 sqlite3ExprCacheClear(pParse);
16990 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
16991 sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
16992 }
16993 pAggInfo->directMode = 0;
16994 sqlite3ExprCacheClear(pParse);
16995 if( addrHitTest ){
16996 sqlite3VdbeJumpHere(v, addrHitTest);
16997 }
16998 }
16999
17000 /*
17001 ** Add a single OP_Explain instruction to the VDBE to explain a simple
17002 ** count(*) query ("SELECT count(*) FROM pTab").
17003 */
17004 #ifndef SQLITE_OMIT_EXPLAIN
17005 static void explainSimpleCount(
17006 Parse *pParse, /* Parse context */
17007 Table *pTab, /* Table being queried */
17008 Index *pIdx /* Index used to optimize scan, or NULL */
17009 ){
17010 if( pParse->explain==2 ){
17011 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
17012 char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
17013 pTab->zName,
17014 bCover ? " USING COVERING INDEX " : "",
17015 bCover ? pIdx->zName : ""
17016 );
17017 sqlite3VdbeAddOp4(
17018 pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
17019 );
17020 }
17021 }
17022 #else
17023 # define explainSimpleCount(a,b,c)
17024 #endif
17025
17026 /*
17027 ** Generate code for the SELECT statement given in the p argument.
17028 **
17029 ** The results are returned according to the SelectDest structure.
17030 ** See comments in sqliteInt.h for further information.
17031 **
17032 ** This routine returns the number of errors. If any errors are
17033 ** encountered, then an appropriate error message is left in
17034 ** pParse->zErrMsg.
17035 **
17036 ** This routine does NOT free the Select structure passed in. The
17037 ** calling function needs to do that.
17038 */
17039 SQLITE_PRIVATE int sqlite3Select(
17040 Parse *pParse, /* The parser context */
17041 Select *p, /* The SELECT statement being coded. */
17042 SelectDest *pDest /* What to do with the query results */
17043 ){
17044 int i, j; /* Loop counters */
17045 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
17046 Vdbe *v; /* The virtual machine under construction */
17047 int isAgg; /* True for select lists like "count(*)" */
17048 ExprList *pEList = 0; /* List of columns to extract. */
17049 SrcList *pTabList; /* List of tables to select from */
17050 Expr *pWhere; /* The WHERE clause. May be NULL */
17051 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
17052 Expr *pHaving; /* The HAVING clause. May be NULL */
17053 int rc = 1; /* Value to return from this function */
17054 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
17055 SortCtx sSort; /* Info on how to code the ORDER BY clause */
17056 AggInfo sAggInfo; /* Information used by aggregate queries */
17057 int iEnd; /* Address of the end of the query */
17058 sqlite3 *db; /* The database connection */
17059
17060 #ifndef SQLITE_OMIT_EXPLAIN
17061 int iRestoreSelectId = pParse->iSelectId;
17062 pParse->iSelectId = pParse->iNextSelectId++;
17063 #endif
17064
17065 db = pParse->db;
17066 if( p==0 || db->mallocFailed || pParse->nErr ){
17067 return 1;
17068 }
17069 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
17070 memset(&sAggInfo, 0, sizeof(sAggInfo));
17071 #if SELECTTRACE_ENABLED
17072 pParse->nSelectIndent++;
17073 SELECTTRACE(1,pParse,p, ("begin processing:\n"));
17074 if( sqlite3SelectTrace & 0x100 ){
17075 sqlite3TreeViewSelect(0, p, 0);
17076 }
17077 #endif
17078
17079 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
17080 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
17081 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
17082 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
17083 if( IgnorableOrderby(pDest) ){
17084 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
17085 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
17086 pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||
17087 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
17088 /* If ORDER BY makes no difference in the output then neither does
17089 ** DISTINCT so it can be removed too. */
17090 sqlite3ExprListDelete(db, p->pOrderBy);
17091 p->pOrderBy = 0;
17092 p->selFlags &= ~SF_Distinct;
17093 }
17094 sqlite3SelectPrep(pParse, p, 0);
17095 memset(&sSort, 0, sizeof(sSort));
17096 sSort.pOrderBy = p->pOrderBy;
17097 pTabList = p->pSrc;
17098 if( pParse->nErr || db->mallocFailed ){
17099 goto select_end;
17100 }
17101 assert( p->pEList!=0 );
17102 isAgg = (p->selFlags & SF_Aggregate)!=0;
17103 #if SELECTTRACE_ENABLED
17104 if( sqlite3SelectTrace & 0x100 ){
17105 SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
17106 sqlite3TreeViewSelect(0, p, 0);
17107 }
17108 #endif
17109
17110 /* Try to flatten subqueries in the FROM clause up into the main query
17111 */
17112 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
17113 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
17114 struct SrcList_item *pItem = &pTabList->a[i];
17115 Select *pSub = pItem->pSelect;
17116 int isAggSub;
17117 Table *pTab = pItem->pTab;
17118 if( pSub==0 ) continue;
17119
17120 /* Catch mismatch in the declared columns of a view and the number of
17121 ** columns in the SELECT on the RHS */
17122 if( pTab->nCol!=pSub->pEList->nExpr ){
17123 sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
17124 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
17125 goto select_end;
17126 }
17127
17128 isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
17129 if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
17130 /* This subquery can be absorbed into its parent. */
17131 if( isAggSub ){
17132 isAgg = 1;
17133 p->selFlags |= SF_Aggregate;
17134 }
17135 i = -1;
17136 }
17137 pTabList = p->pSrc;
17138 if( db->mallocFailed ) goto select_end;
17139 if( !IgnorableOrderby(pDest) ){
17140 sSort.pOrderBy = p->pOrderBy;
17141 }
17142 }
17143 #endif
17144
17145 /* Get a pointer the VDBE under construction, allocating a new VDBE if one
17146 ** does not already exist */
17147 v = sqlite3GetVdbe(pParse);
17148 if( v==0 ) goto select_end;
17149
17150 #ifndef SQLITE_OMIT_COMPOUND_SELECT
17151 /* Handle compound SELECT statements using the separate multiSelect()
17152 ** procedure.
17153 */
17154 if( p->pPrior ){
17155 rc = multiSelect(pParse, p, pDest);
17156 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
17157 #if SELECTTRACE_ENABLED
17158 SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
17159 pParse->nSelectIndent--;
17160 #endif
17161 return rc;
17162 }
17163 #endif
17164
17165 /* Generate code for all sub-queries in the FROM clause
17166 */
17167 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
17168 for(i=0; i<pTabList->nSrc; i++){
17169 struct SrcList_item *pItem = &pTabList->a[i];
17170 SelectDest dest;
17171 Select *pSub = pItem->pSelect;
17172 if( pSub==0 ) continue;
17173
17174 /* Sometimes the code for a subquery will be generated more than
17175 ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
17176 ** for example. In that case, do not regenerate the code to manifest
17177 ** a view or the co-routine to implement a view. The first instance
17178 ** is sufficient, though the subroutine to manifest the view does need
17179 ** to be invoked again. */
17180 if( pItem->addrFillSub ){
17181 if( pItem->fg.viaCoroutine==0 ){
17182 sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
17183 }
17184 continue;
17185 }
17186
17187 /* Increment Parse.nHeight by the height of the largest expression
17188 ** tree referred to by this, the parent select. The child select
17189 ** may contain expression trees of at most
17190 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
17191 ** more conservative than necessary, but much easier than enforcing
17192 ** an exact limit.
17193 */
17194 pParse->nHeight += sqlite3SelectExprHeight(p);
17195
17196 /* Make copies of constant WHERE-clause terms in the outer query down
17197 ** inside the subquery. This can help the subquery to run more efficiently.
17198 */
17199 if( (pItem->fg.jointype & JT_OUTER)==0
17200 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor)
17201 ){
17202 #if SELECTTRACE_ENABLED
17203 if( sqlite3SelectTrace & 0x100 ){
17204 SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
17205 sqlite3TreeViewSelect(0, p, 0);
17206 }
17207 #endif
17208 }
17209
17210 /* Generate code to implement the subquery
17211 **
17212 ** The subquery is implemented as a co-routine if all of these are true:
17213 ** (1) The subquery is guaranteed to be the outer loop (so that it
17214 ** does not need to be computed more than once)
17215 ** (2) The ALL keyword after SELECT is omitted. (Applications are
17216 ** allowed to say "SELECT ALL" instead of just "SELECT" to disable
17217 ** the use of co-routines.)
17218 ** (3) Co-routines are not disabled using sqlite3_test_control()
17219 ** with SQLITE_TESTCTRL_OPTIMIZATIONS.
17220 **
17221 ** TODO: Are there other reasons beside (1) to use a co-routine
17222 ** implementation?
17223 */
17224 if( i==0
17225 && (pTabList->nSrc==1
17226 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
17227 && (p->selFlags & SF_All)==0 /* (2) */
17228 && OptimizationEnabled(db, SQLITE_SubqCoroutine) /* (3) */
17229 ){
17230 /* Implement a co-routine that will return a single row of the result
17231 ** set on each invocation.
17232 */
17233 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
17234 pItem->regReturn = ++pParse->nMem;
17235 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
17236 VdbeComment((v, "%s", pItem->pTab->zName));
17237 pItem->addrFillSub = addrTop;
17238 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
17239 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
17240 sqlite3Select(pParse, pSub, &dest);
17241 pItem->pTab->nRowLogEst = pSub->nSelectRow;
17242 pItem->fg.viaCoroutine = 1;
17243 pItem->regResult = dest.iSdst;
17244 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
17245 sqlite3VdbeJumpHere(v, addrTop-1);
17246 sqlite3ClearTempRegCache(pParse);
17247 }else{
17248 /* Generate a subroutine that will fill an ephemeral table with
17249 ** the content of this subquery. pItem->addrFillSub will point
17250 ** to the address of the generated subroutine. pItem->regReturn
17251 ** is a register allocated to hold the subroutine return address
17252 */
17253 int topAddr;
17254 int onceAddr = 0;
17255 int retAddr;
17256 assert( pItem->addrFillSub==0 );
17257 pItem->regReturn = ++pParse->nMem;
17258 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
17259 pItem->addrFillSub = topAddr+1;
17260 if( pItem->fg.isCorrelated==0 ){
17261 /* If the subquery is not correlated and if we are not inside of
17262 ** a trigger, then we only need to compute the value of the subquery
17263 ** once. */
17264 onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
17265 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
17266 }else{
17267 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
17268 }
17269 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
17270 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
17271 sqlite3Select(pParse, pSub, &dest);
17272 pItem->pTab->nRowLogEst = pSub->nSelectRow;
17273 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
17274 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
17275 VdbeComment((v, "end %s", pItem->pTab->zName));
17276 sqlite3VdbeChangeP1(v, topAddr, retAddr);
17277 sqlite3ClearTempRegCache(pParse);
17278 }
17279 if( db->mallocFailed ) goto select_end;
17280 pParse->nHeight -= sqlite3SelectExprHeight(p);
17281 }
17282 #endif
17283
17284 /* Various elements of the SELECT copied into local variables for
17285 ** convenience */
17286 pEList = p->pEList;
17287 pWhere = p->pWhere;
17288 pGroupBy = p->pGroupBy;
17289 pHaving = p->pHaving;
17290 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
17291
17292 #if SELECTTRACE_ENABLED
17293 if( sqlite3SelectTrace & 0x400 ){
17294 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
17295 sqlite3TreeViewSelect(0, p, 0);
17296 }
17297 #endif
17298
17299 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
17300 ** if the select-list is the same as the ORDER BY list, then this query
17301 ** can be rewritten as a GROUP BY. In other words, this:
17302 **
17303 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
17304 **
17305 ** is transformed to:
17306 **
17307 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
17308 **
17309 ** The second form is preferred as a single index (or temp-table) may be
17310 ** used for both the ORDER BY and DISTINCT processing. As originally
17311 ** written the query must use a temp-table for at least one of the ORDER
17312 ** BY and DISTINCT, and an index or separate temp-table for the other.
17313 */
17314 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
17315 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
17316 ){
17317 p->selFlags &= ~SF_Distinct;
17318 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
17319 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
17320 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
17321 ** original setting of the SF_Distinct flag, not the current setting */
17322 assert( sDistinct.isTnct );
17323
17324 #if SELECTTRACE_ENABLED
17325 if( sqlite3SelectTrace & 0x400 ){
17326 SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
17327 sqlite3TreeViewSelect(0, p, 0);
17328 }
17329 #endif
17330 }
17331
17332 /* If there is an ORDER BY clause, then create an ephemeral index to
17333 ** do the sorting. But this sorting ephemeral index might end up
17334 ** being unused if the data can be extracted in pre-sorted order.
17335 ** If that is the case, then the OP_OpenEphemeral instruction will be
17336 ** changed to an OP_Noop once we figure out that the sorting index is
17337 ** not needed. The sSort.addrSortIndex variable is used to facilitate
17338 ** that change.
17339 */
17340 if( sSort.pOrderBy ){
17341 KeyInfo *pKeyInfo;
17342 pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
17343 sSort.iECursor = pParse->nTab++;
17344 sSort.addrSortIndex =
17345 sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
17346 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
17347 (char*)pKeyInfo, P4_KEYINFO
17348 );
17349 }else{
17350 sSort.addrSortIndex = -1;
17351 }
17352
17353 /* If the output is destined for a temporary table, open that table.
17354 */
17355 if( pDest->eDest==SRT_EphemTab ){
17356 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
17357 }
17358
17359 /* Set the limiter.
17360 */
17361 iEnd = sqlite3VdbeMakeLabel(v);
17362 if( (p->selFlags & SF_FixedLimit)==0 ){
17363 p->nSelectRow = 320; /* 4 billion rows */
17364 }
17365 computeLimitRegisters(pParse, p, iEnd);
17366 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
17367 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
17368 sSort.sortFlags |= SORTFLAG_UseSorter;
17369 }
17370
17371 /* Open an ephemeral index to use for the distinct set.
17372 */
17373 if( p->selFlags & SF_Distinct ){
17374 sDistinct.tabTnct = pParse->nTab++;
17375 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
17376 sDistinct.tabTnct, 0, 0,
17377 (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
17378 P4_KEYINFO);
17379 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
17380 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
17381 }else{
17382 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
17383 }
17384
17385 if( !isAgg && pGroupBy==0 ){
17386 /* No aggregate functions and no GROUP BY clause */
17387 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
17388 assert( WHERE_USE_LIMIT==SF_FixedLimit );
17389 wctrlFlags |= p->selFlags & SF_FixedLimit;
17390
17391 /* Begin the database scan. */
17392 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
17393 p->pEList, wctrlFlags, p->nSelectRow);
17394 if( pWInfo==0 ) goto select_end;
17395 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
17396 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
17397 }
17398 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
17399 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
17400 }
17401 if( sSort.pOrderBy ){
17402 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
17403 sSort.bOrderedInnerLoop = sqlite3WhereOrderedInnerLoop(pWInfo);
17404 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
17405 sSort.pOrderBy = 0;
17406 }
17407 }
17408
17409 /* If sorting index that was created by a prior OP_OpenEphemeral
17410 ** instruction ended up not being needed, then change the OP_OpenEphemeral
17411 ** into an OP_Noop.
17412 */
17413 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
17414 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
17415 }
17416
17417 /* Use the standard inner loop. */
17418 selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest,
17419 sqlite3WhereContinueLabel(pWInfo),
17420 sqlite3WhereBreakLabel(pWInfo));
17421
17422 /* End the database scan loop.
17423 */
17424 sqlite3WhereEnd(pWInfo);
17425 }else{
17426 /* This case when there exist aggregate functions or a GROUP BY clause
17427 ** or both */
17428 NameContext sNC; /* Name context for processing aggregate information */
17429 int iAMem; /* First Mem address for storing current GROUP BY */
17430 int iBMem; /* First Mem address for previous GROUP BY */
17431 int iUseFlag; /* Mem address holding flag indicating that at least
17432 ** one row of the input to the aggregator has been
17433 ** processed */
17434 int iAbortFlag; /* Mem address which causes query abort if positive */
17435 int groupBySort; /* Rows come from source in GROUP BY order */
17436 int addrEnd; /* End of processing for this SELECT */
17437 int sortPTab = 0; /* Pseudotable used to decode sorting results */
17438 int sortOut = 0; /* Output register from the sorter */
17439 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
17440
17441 /* Remove any and all aliases between the result set and the
17442 ** GROUP BY clause.
17443 */
17444 if( pGroupBy ){
17445 int k; /* Loop counter */
17446 struct ExprList_item *pItem; /* For looping over expression in a list */
17447
17448 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
17449 pItem->u.x.iAlias = 0;
17450 }
17451 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
17452 pItem->u.x.iAlias = 0;
17453 }
17454 assert( 66==sqlite3LogEst(100) );
17455 if( p->nSelectRow>66 ) p->nSelectRow = 66;
17456 }else{
17457 assert( 0==sqlite3LogEst(1) );
17458 p->nSelectRow = 0;
17459 }
17460
17461 /* If there is both a GROUP BY and an ORDER BY clause and they are
17462 ** identical, then it may be possible to disable the ORDER BY clause
17463 ** on the grounds that the GROUP BY will cause elements to come out
17464 ** in the correct order. It also may not - the GROUP BY might use a
17465 ** database index that causes rows to be grouped together as required
17466 ** but not actually sorted. Either way, record the fact that the
17467 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
17468 ** variable. */
17469 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
17470 orderByGrp = 1;
17471 }
17472
17473 /* Create a label to jump to when we want to abort the query */
17474 addrEnd = sqlite3VdbeMakeLabel(v);
17475
17476 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
17477 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
17478 ** SELECT statement.
17479 */
17480 memset(&sNC, 0, sizeof(sNC));
17481 sNC.pParse = pParse;
17482 sNC.pSrcList = pTabList;
17483 sNC.pAggInfo = &sAggInfo;
17484 sAggInfo.mnReg = pParse->nMem+1;
17485 sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
17486 sAggInfo.pGroupBy = pGroupBy;
17487 sqlite3ExprAnalyzeAggList(&sNC, pEList);
17488 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
17489 if( pHaving ){
17490 sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
17491 }
17492 sAggInfo.nAccumulator = sAggInfo.nColumn;
17493 for(i=0; i<sAggInfo.nFunc; i++){
17494 assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
17495 sNC.ncFlags |= NC_InAggFunc;
17496 sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
17497 sNC.ncFlags &= ~NC_InAggFunc;
17498 }
17499 sAggInfo.mxReg = pParse->nMem;
17500 if( db->mallocFailed ) goto select_end;
17501
17502 /* Processing for aggregates with GROUP BY is very different and
17503 ** much more complex than aggregates without a GROUP BY.
17504 */
17505 if( pGroupBy ){
17506 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
17507 int addr1; /* A-vs-B comparision jump */
17508 int addrOutputRow; /* Start of subroutine that outputs a result row */
17509 int regOutputRow; /* Return address register for output subroutine */
17510 int addrSetAbort; /* Set the abort flag and return */
17511 int addrTopOfLoop; /* Top of the input loop */
17512 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
17513 int addrReset; /* Subroutine for resetting the accumulator */
17514 int regReset; /* Return address register for reset subroutine */
17515
17516 /* If there is a GROUP BY clause we might need a sorting index to
17517 ** implement it. Allocate that sorting index now. If it turns out
17518 ** that we do not need it after all, the OP_SorterOpen instruction
17519 ** will be converted into a Noop.
17520 */
17521 sAggInfo.sortingIdx = pParse->nTab++;
17522 pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0, sAggInfo.nColumn);
17523 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
17524 sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
17525 0, (char*)pKeyInfo, P4_KEYINFO);
17526
17527 /* Initialize memory locations used by GROUP BY aggregate processing
17528 */
17529 iUseFlag = ++pParse->nMem;
17530 iAbortFlag = ++pParse->nMem;
17531 regOutputRow = ++pParse->nMem;
17532 addrOutputRow = sqlite3VdbeMakeLabel(v);
17533 regReset = ++pParse->nMem;
17534 addrReset = sqlite3VdbeMakeLabel(v);
17535 iAMem = pParse->nMem + 1;
17536 pParse->nMem += pGroupBy->nExpr;
17537 iBMem = pParse->nMem + 1;
17538 pParse->nMem += pGroupBy->nExpr;
17539 sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
17540 VdbeComment((v, "clear abort flag"));
17541 sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
17542 VdbeComment((v, "indicate accumulator empty"));
17543 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
17544
17545 /* Begin a loop that will extract all source rows in GROUP BY order.
17546 ** This might involve two separate loops with an OP_Sort in between, or
17547 ** it might be a single loop that uses an index to extract information
17548 ** in the right order to begin with.
17549 */
17550 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
17551 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
17552 WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
17553 );
17554 if( pWInfo==0 ) goto select_end;
17555 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
17556 /* The optimizer is able to deliver rows in group by order so
17557 ** we do not have to sort. The OP_OpenEphemeral table will be
17558 ** cancelled later because we still need to use the pKeyInfo
17559 */
17560 groupBySort = 0;
17561 }else{
17562 /* Rows are coming out in undetermined order. We have to push
17563 ** each row into a sorting index, terminate the first loop,
17564 ** then loop over the sorting index in order to get the output
17565 ** in sorted order
17566 */
17567 int regBase;
17568 int regRecord;
17569 int nCol;
17570 int nGroupBy;
17571
17572 explainTempTable(pParse,
17573 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
17574 "DISTINCT" : "GROUP BY");
17575
17576 groupBySort = 1;
17577 nGroupBy = pGroupBy->nExpr;
17578 nCol = nGroupBy;
17579 j = nGroupBy;
17580 for(i=0; i<sAggInfo.nColumn; i++){
17581 if( sAggInfo.aCol[i].iSorterColumn>=j ){
17582 nCol++;
17583 j++;
17584 }
17585 }
17586 regBase = sqlite3GetTempRange(pParse, nCol);
17587 sqlite3ExprCacheClear(pParse);
17588 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
17589 j = nGroupBy;
17590 for(i=0; i<sAggInfo.nColumn; i++){
17591 struct AggInfo_col *pCol = &sAggInfo.aCol[i];
17592 if( pCol->iSorterColumn>=j ){
17593 int r1 = j + regBase;
17594 sqlite3ExprCodeGetColumnToReg(pParse,
17595 pCol->pTab, pCol->iColumn, pCol->iTable, r1);
17596 j++;
17597 }
17598 }
17599 regRecord = sqlite3GetTempReg(pParse);
17600 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
17601 sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
17602 sqlite3ReleaseTempReg(pParse, regRecord);
17603 sqlite3ReleaseTempRange(pParse, regBase, nCol);
17604 sqlite3WhereEnd(pWInfo);
17605 sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
17606 sortOut = sqlite3GetTempReg(pParse);
17607 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
17608 sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
17609 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
17610 sAggInfo.useSortingIdx = 1;
17611 sqlite3ExprCacheClear(pParse);
17612
17613 }
17614
17615 /* If the index or temporary table used by the GROUP BY sort
17616 ** will naturally deliver rows in the order required by the ORDER BY
17617 ** clause, cancel the ephemeral table open coded earlier.
17618 **
17619 ** This is an optimization - the correct answer should result regardless.
17620 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
17621 ** disable this optimization for testing purposes. */
17622 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
17623 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
17624 ){
17625 sSort.pOrderBy = 0;
17626 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
17627 }
17628
17629 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
17630 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
17631 ** Then compare the current GROUP BY terms against the GROUP BY terms
17632 ** from the previous row currently stored in a0, a1, a2...
17633 */
17634 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
17635 sqlite3ExprCacheClear(pParse);
17636 if( groupBySort ){
17637 sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
17638 sortOut, sortPTab);
17639 }
17640 for(j=0; j<pGroupBy->nExpr; j++){
17641 if( groupBySort ){
17642 sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
17643 }else{
17644 sAggInfo.directMode = 1;
17645 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
17646 }
17647 }
17648 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
17649 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
17650 addr1 = sqlite3VdbeCurrentAddr(v);
17651 sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v);
17652
17653 /* Generate code that runs whenever the GROUP BY changes.
17654 ** Changes in the GROUP BY are detected by the previous code
17655 ** block. If there were no changes, this block is skipped.
17656 **
17657 ** This code copies current group by terms in b0,b1,b2,...
17658 ** over to a0,a1,a2. It then calls the output subroutine
17659 ** and resets the aggregate accumulator registers in preparation
17660 ** for the next GROUP BY batch.
17661 */
17662 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
17663 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
17664 VdbeComment((v, "output one row"));
17665 sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
17666 VdbeComment((v, "check abort flag"));
17667 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
17668 VdbeComment((v, "reset accumulator"));
17669
17670 /* Update the aggregate accumulators based on the content of
17671 ** the current row
17672 */
17673 sqlite3VdbeJumpHere(v, addr1);
17674 updateAccumulator(pParse, &sAggInfo);
17675 sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
17676 VdbeComment((v, "indicate data in accumulator"));
17677
17678 /* End of the loop
17679 */
17680 if( groupBySort ){
17681 sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
17682 VdbeCoverage(v);
17683 }else{
17684 sqlite3WhereEnd(pWInfo);
17685 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
17686 }
17687
17688 /* Output the final row of result
17689 */
17690 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
17691 VdbeComment((v, "output final row"));
17692
17693 /* Jump over the subroutines
17694 */
17695 sqlite3VdbeGoto(v, addrEnd);
17696
17697 /* Generate a subroutine that outputs a single row of the result
17698 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
17699 ** is less than or equal to zero, the subroutine is a no-op. If
17700 ** the processing calls for the query to abort, this subroutine
17701 ** increments the iAbortFlag memory location before returning in
17702 ** order to signal the caller to abort.
17703 */
17704 addrSetAbort = sqlite3VdbeCurrentAddr(v);
17705 sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
17706 VdbeComment((v, "set abort flag"));
17707 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
17708 sqlite3VdbeResolveLabel(v, addrOutputRow);
17709 addrOutputRow = sqlite3VdbeCurrentAddr(v);
17710 sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
17711 VdbeCoverage(v);
17712 VdbeComment((v, "Groupby result generator entry point"));
17713 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
17714 finalizeAggFunctions(pParse, &sAggInfo);
17715 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
17716 selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
17717 &sDistinct, pDest,
17718 addrOutputRow+1, addrSetAbort);
17719 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
17720 VdbeComment((v, "end groupby result generator"));
17721
17722 /* Generate a subroutine that will reset the group-by accumulator
17723 */
17724 sqlite3VdbeResolveLabel(v, addrReset);
17725 resetAccumulator(pParse, &sAggInfo);
17726 sqlite3VdbeAddOp1(v, OP_Return, regReset);
17727
17728 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
17729 else {
17730 ExprList *pDel = 0;
17731 #ifndef SQLITE_OMIT_BTREECOUNT
17732 Table *pTab;
17733 if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
17734 /* If isSimpleCount() returns a pointer to a Table structure, then
17735 ** the SQL statement is of the form:
17736 **
17737 ** SELECT count(*) FROM <tbl>
17738 **
17739 ** where the Table structure returned represents table <tbl>.
17740 **
17741 ** This statement is so common that it is optimized specially. The
17742 ** OP_Count instruction is executed either on the intkey table that
17743 ** contains the data for table <tbl> or on one of its indexes. It
17744 ** is better to execute the op on an index, as indexes are almost
17745 ** always spread across less pages than their corresponding tables.
17746 */
17747 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
17748 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
17749 Index *pIdx; /* Iterator variable */
17750 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
17751 Index *pBest = 0; /* Best index found so far */
17752 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
17753
17754 sqlite3CodeVerifySchema(pParse, iDb);
17755 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
17756
17757 /* Search for the index that has the lowest scan cost.
17758 **
17759 ** (2011-04-15) Do not do a full scan of an unordered index.
17760 **
17761 ** (2013-10-03) Do not count the entries in a partial index.
17762 **
17763 ** In practice the KeyInfo structure will not be used. It is only
17764 ** passed to keep OP_OpenRead happy.
17765 */
17766 if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
17767 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
17768 if( pIdx->bUnordered==0
17769 && pIdx->szIdxRow<pTab->szTabRow
17770 && pIdx->pPartIdxWhere==0
17771 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
17772 ){
17773 pBest = pIdx;
17774 }
17775 }
17776 if( pBest ){
17777 iRoot = pBest->tnum;
17778 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
17779 }
17780
17781 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
17782 sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
17783 if( pKeyInfo ){
17784 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
17785 }
17786 sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
17787 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
17788 explainSimpleCount(pParse, pTab, pBest);
17789 }else
17790 #endif /* SQLITE_OMIT_BTREECOUNT */
17791 {
17792 /* Check if the query is of one of the following forms:
17793 **
17794 ** SELECT min(x) FROM ...
17795 ** SELECT max(x) FROM ...
17796 **
17797 ** If it is, then ask the code in where.c to attempt to sort results
17798 ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause.
17799 ** If where.c is able to produce results sorted in this order, then
17800 ** add vdbe code to break out of the processing loop after the
17801 ** first iteration (since the first iteration of the loop is
17802 ** guaranteed to operate on the row with the minimum or maximum
17803 ** value of x, the only row required).
17804 **
17805 ** A special flag must be passed to sqlite3WhereBegin() to slightly
17806 ** modify behavior as follows:
17807 **
17808 ** + If the query is a "SELECT min(x)", then the loop coded by
17809 ** where.c should not iterate over any values with a NULL value
17810 ** for x.
17811 **
17812 ** + The optimizer code in where.c (the thing that decides which
17813 ** index or indices to use) should place a different priority on
17814 ** satisfying the 'ORDER BY' clause than it does in other cases.
17815 ** Refer to code and comments in where.c for details.
17816 */
17817 ExprList *pMinMax = 0;
17818 u8 flag = WHERE_ORDERBY_NORMAL;
17819
17820 assert( p->pGroupBy==0 );
17821 assert( flag==0 );
17822 if( p->pHaving==0 ){
17823 flag = minMaxQuery(&sAggInfo, &pMinMax);
17824 }
17825 assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
17826
17827 if( flag ){
17828 pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
17829 pDel = pMinMax;
17830 assert( db->mallocFailed || pMinMax!=0 );
17831 if( !db->mallocFailed ){
17832 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
17833 pMinMax->a[0].pExpr->op = TK_COLUMN;
17834 }
17835 }
17836
17837 /* This case runs if the aggregate has no GROUP BY clause. The
17838 ** processing is much simpler since there is only a single row
17839 ** of output.
17840 */
17841 resetAccumulator(pParse, &sAggInfo);
17842 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0);
17843 if( pWInfo==0 ){
17844 sqlite3ExprListDelete(db, pDel);
17845 goto select_end;
17846 }
17847 updateAccumulator(pParse, &sAggInfo);
17848 assert( pMinMax==0 || pMinMax->nExpr==1 );
17849 if( sqlite3WhereIsOrdered(pWInfo)>0 ){
17850 sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
17851 VdbeComment((v, "%s() by index",
17852 (flag==WHERE_ORDERBY_MIN?"min":"max")));
17853 }
17854 sqlite3WhereEnd(pWInfo);
17855 finalizeAggFunctions(pParse, &sAggInfo);
17856 }
17857
17858 sSort.pOrderBy = 0;
17859 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
17860 selectInnerLoop(pParse, p, p->pEList, -1, 0, 0,
17861 pDest, addrEnd, addrEnd);
17862 sqlite3ExprListDelete(db, pDel);
17863 }
17864 sqlite3VdbeResolveLabel(v, addrEnd);
17865
17866 } /* endif aggregate query */
17867
17868 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
17869 explainTempTable(pParse, "DISTINCT");
17870 }
17871
17872 /* If there is an ORDER BY clause, then we need to sort the results
17873 ** and send them to the callback one by one.
17874 */
17875 if( sSort.pOrderBy ){
17876 explainTempTable(pParse,
17877 sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
17878 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
17879 }
17880
17881 /* Jump here to skip this query
17882 */
17883 sqlite3VdbeResolveLabel(v, iEnd);
17884
17885 /* The SELECT has been coded. If there is an error in the Parse structure,
17886 ** set the return code to 1. Otherwise 0. */
17887 rc = (pParse->nErr>0);
17888
17889 /* Control jumps to here if an error is encountered above, or upon
17890 ** successful coding of the SELECT.
17891 */
17892 select_end:
17893 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
17894
17895 /* Identify column names if results of the SELECT are to be output.
17896 */
17897 if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
17898 generateColumnNames(pParse, pTabList, pEList);
17899 }
17900
17901 sqlite3DbFree(db, sAggInfo.aCol);
17902 sqlite3DbFree(db, sAggInfo.aFunc);
17903 #if SELECTTRACE_ENABLED
17904 SELECTTRACE(1,pParse,p,("end processing\n"));
17905 pParse->nSelectIndent--;
17906 #endif
17907 return rc;
17908 }
17909
17910 /************** End of select.c **********************************************/
17911 /************** Begin file table.c *******************************************/
17912 /*
17913 ** 2001 September 15
17914 **
17915 ** The author disclaims copyright to this source code. In place of
17916 ** a legal notice, here is a blessing:
17917 **
17918 ** May you do good and not evil.
17919 ** May you find forgiveness for yourself and forgive others.
17920 ** May you share freely, never taking more than you give.
17921 **
17922 *************************************************************************
17923 ** This file contains the sqlite3_get_table() and sqlite3_free_table()
17924 ** interface routines. These are just wrappers around the main
17925 ** interface routine of sqlite3_exec().
17926 **
17927 ** These routines are in a separate files so that they will not be linked
17928 ** if they are not used.
17929 */
17930 /* #include "sqliteInt.h" */
17931
17932 #ifndef SQLITE_OMIT_GET_TABLE
17933
17934 /*
17935 ** This structure is used to pass data from sqlite3_get_table() through
17936 ** to the callback function is uses to build the result.
17937 */
17938 typedef struct TabResult {
17939 char **azResult; /* Accumulated output */
17940 char *zErrMsg; /* Error message text, if an error occurs */
17941 u32 nAlloc; /* Slots allocated for azResult[] */
17942 u32 nRow; /* Number of rows in the result */
17943 u32 nColumn; /* Number of columns in the result */
17944 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
17945 int rc; /* Return code from sqlite3_exec() */
17946 } TabResult;
17947
17948 /*
17949 ** This routine is called once for each row in the result table. Its job
17950 ** is to fill in the TabResult structure appropriately, allocating new
17951 ** memory as necessary.
17952 */
17953 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
17954 TabResult *p = (TabResult*)pArg; /* Result accumulator */
17955 int need; /* Slots needed in p->azResult[] */
17956 int i; /* Loop counter */
17957 char *z; /* A single column of result */
17958
17959 /* Make sure there is enough space in p->azResult to hold everything
17960 ** we need to remember from this invocation of the callback.
17961 */
17962 if( p->nRow==0 && argv!=0 ){
17963 need = nCol*2;
17964 }else{
17965 need = nCol;
17966 }
17967 if( p->nData + need > p->nAlloc ){
17968 char **azNew;
17969 p->nAlloc = p->nAlloc*2 + need;
17970 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
17971 if( azNew==0 ) goto malloc_failed;
17972 p->azResult = azNew;
17973 }
17974
17975 /* If this is the first row, then generate an extra row containing
17976 ** the names of all columns.
17977 */
17978 if( p->nRow==0 ){
17979 p->nColumn = nCol;
17980 for(i=0; i<nCol; i++){
17981 z = sqlite3_mprintf("%s", colv[i]);
17982 if( z==0 ) goto malloc_failed;
17983 p->azResult[p->nData++] = z;
17984 }
17985 }else if( (int)p->nColumn!=nCol ){
17986 sqlite3_free(p->zErrMsg);
17987 p->zErrMsg = sqlite3_mprintf(
17988 "sqlite3_get_table() called with two or more incompatible queries"
17989 );
17990 p->rc = SQLITE_ERROR;
17991 return 1;
17992 }
17993
17994 /* Copy over the row data
17995 */
17996 if( argv!=0 ){
17997 for(i=0; i<nCol; i++){
17998 if( argv[i]==0 ){
17999 z = 0;
18000 }else{
18001 int n = sqlite3Strlen30(argv[i])+1;
18002 z = sqlite3_malloc64( n );
18003 if( z==0 ) goto malloc_failed;
18004 memcpy(z, argv[i], n);
18005 }
18006 p->azResult[p->nData++] = z;
18007 }
18008 p->nRow++;
18009 }
18010 return 0;
18011
18012 malloc_failed:
18013 p->rc = SQLITE_NOMEM_BKPT;
18014 return 1;
18015 }
18016
18017 /*
18018 ** Query the database. But instead of invoking a callback for each row,
18019 ** malloc() for space to hold the result and return the entire results
18020 ** at the conclusion of the call.
18021 **
18022 ** The result that is written to ***pazResult is held in memory obtained
18023 ** from malloc(). But the caller cannot free this memory directly.
18024 ** Instead, the entire table should be passed to sqlite3_free_table() when
18025 ** the calling procedure is finished using it.
18026 */
18027 SQLITE_API int sqlite3_get_table(
18028 sqlite3 *db, /* The database on which the SQL executes */
18029 const char *zSql, /* The SQL to be executed */
18030 char ***pazResult, /* Write the result table here */
18031 int *pnRow, /* Write the number of rows in the result here */
18032 int *pnColumn, /* Write the number of columns of result here */
18033 char **pzErrMsg /* Write error messages here */
18034 ){
18035 int rc;
18036 TabResult res;
18037
18038 #ifdef SQLITE_ENABLE_API_ARMOR
18039 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
18040 #endif
18041 *pazResult = 0;
18042 if( pnColumn ) *pnColumn = 0;
18043 if( pnRow ) *pnRow = 0;
18044 if( pzErrMsg ) *pzErrMsg = 0;
18045 res.zErrMsg = 0;
18046 res.nRow = 0;
18047 res.nColumn = 0;
18048 res.nData = 1;
18049 res.nAlloc = 20;
18050 res.rc = SQLITE_OK;
18051 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
18052 if( res.azResult==0 ){
18053 db->errCode = SQLITE_NOMEM;
18054 return SQLITE_NOMEM_BKPT;
18055 }
18056 res.azResult[0] = 0;
18057 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
18058 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
18059 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
18060 if( (rc&0xff)==SQLITE_ABORT ){
18061 sqlite3_free_table(&res.azResult[1]);
18062 if( res.zErrMsg ){
18063 if( pzErrMsg ){
18064 sqlite3_free(*pzErrMsg);
18065 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
18066 }
18067 sqlite3_free(res.zErrMsg);
18068 }
18069 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
18070 return res.rc;
18071 }
18072 sqlite3_free(res.zErrMsg);
18073 if( rc!=SQLITE_OK ){
18074 sqlite3_free_table(&res.azResult[1]);
18075 return rc;
18076 }
18077 if( res.nAlloc>res.nData ){
18078 char **azNew;
18079 azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData );
18080 if( azNew==0 ){
18081 sqlite3_free_table(&res.azResult[1]);
18082 db->errCode = SQLITE_NOMEM;
18083 return SQLITE_NOMEM_BKPT;
18084 }
18085 res.azResult = azNew;
18086 }
18087 *pazResult = &res.azResult[1];
18088 if( pnColumn ) *pnColumn = res.nColumn;
18089 if( pnRow ) *pnRow = res.nRow;
18090 return rc;
18091 }
18092
18093 /*
18094 ** This routine frees the space the sqlite3_get_table() malloced.
18095 */
18096 SQLITE_API void sqlite3_free_table(
18097 char **azResult /* Result returned from sqlite3_get_table() */
18098 ){
18099 if( azResult ){
18100 int i, n;
18101 azResult--;
18102 assert( azResult!=0 );
18103 n = SQLITE_PTR_TO_INT(azResult[0]);
18104 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
18105 sqlite3_free(azResult);
18106 }
18107 }
18108
18109 #endif /* SQLITE_OMIT_GET_TABLE */
18110
18111 /************** End of table.c ***********************************************/
18112 /************** Begin file trigger.c *****************************************/
18113 /*
18114 **
18115 ** The author disclaims copyright to this source code. In place of
18116 ** a legal notice, here is a blessing:
18117 **
18118 ** May you do good and not evil.
18119 ** May you find forgiveness for yourself and forgive others.
18120 ** May you share freely, never taking more than you give.
18121 **
18122 *************************************************************************
18123 ** This file contains the implementation for TRIGGERs
18124 */
18125 /* #include "sqliteInt.h" */
18126
18127 #ifndef SQLITE_OMIT_TRIGGER
18128 /*
18129 ** Delete a linked list of TriggerStep structures.
18130 */
18131 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerS tep){
18132 while( pTriggerStep ){
18133 TriggerStep * pTmp = pTriggerStep;
18134 pTriggerStep = pTriggerStep->pNext;
18135
18136 sqlite3ExprDelete(db, pTmp->pWhere);
18137 sqlite3ExprListDelete(db, pTmp->pExprList);
18138 sqlite3SelectDelete(db, pTmp->pSelect);
18139 sqlite3IdListDelete(db, pTmp->pIdList);
18140
18141 sqlite3DbFree(db, pTmp);
18142 }
18143 }
18144
18145 /*
18146 ** Given table pTab, return a list of all the triggers attached to
18147 ** the table. The list is connected by Trigger.pNext pointers.
18148 **
18149 ** All of the triggers on pTab that are in the same database as pTab
18150 ** are already attached to pTab->pTrigger. But there might be additional
18151 ** triggers on pTab in the TEMP schema. This routine prepends all
18152 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
18153 ** and returns the combined list.
18154 **
18155 ** To state it another way: This routine returns a list of all triggers
18156 ** that fire off of pTab. The list will include any TEMP triggers on
18157 ** pTab as well as the triggers lised in pTab->pTrigger.
18158 */
18159 SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
18160 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
18161 Trigger *pList = 0; /* List of triggers to return */
18162
18163 if( pParse->disableTriggers ){
18164 return 0;
18165 }
18166
18167 if( pTmpSchema!=pTab->pSchema ){
18168 HashElem *p;
18169 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
18170 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
18171 Trigger *pTrig = (Trigger *)sqliteHashData(p);
18172 if( pTrig->pTabSchema==pTab->pSchema
18173 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
18174 ){
18175 pTrig->pNext = (pList ? pList : pTab->pTrigger);
18176 pList = pTrig;
18177 }
18178 }
18179 }
18180
18181 return (pList ? pList : pTab->pTrigger);
18182 }
18183
18184 /*
18185 ** This is called by the parser when it sees a CREATE TRIGGER statement
18186 ** up to the point of the BEGIN before the trigger actions. A Trigger
18187 ** structure is generated based on the information available and stored
18188 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
18189 ** sqlite3FinishTrigger() function is called to complete the trigger
18190 ** construction process.
18191 */
18192 SQLITE_PRIVATE void sqlite3BeginTrigger(
18193 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
18194 Token *pName1, /* The name of the trigger */
18195 Token *pName2, /* The name of the trigger */
18196 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
18197 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
18198 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
18199 SrcList *pTableName,/* The name of the table/view the trigger applies to */
18200 Expr *pWhen, /* WHEN clause */
18201 int isTemp, /* True if the TEMPORARY keyword is present */
18202 int noErr /* Suppress errors if the trigger already exists */
18203 ){
18204 Trigger *pTrigger = 0; /* The new trigger */
18205 Table *pTab; /* Table that the trigger fires off of */
18206 char *zName = 0; /* Name of the trigger */
18207 sqlite3 *db = pParse->db; /* The database connection */
18208 int iDb; /* The database to store the trigger in */
18209 Token *pName; /* The unqualified db name */
18210 DbFixer sFix; /* State vector for the DB fixer */
18211
18212 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
18213 assert( pName2!=0 );
18214 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
18215 assert( op>0 && op<0xff );
18216 if( isTemp ){
18217 /* If TEMP was specified, then the trigger name may not be qualified. */
18218 if( pName2->n>0 ){
18219 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
18220 goto trigger_cleanup;
18221 }
18222 iDb = 1;
18223 pName = pName1;
18224 }else{
18225 /* Figure out the db that the trigger will be created in */
18226 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
18227 if( iDb<0 ){
18228 goto trigger_cleanup;
18229 }
18230 }
18231 if( !pTableName || db->mallocFailed ){
18232 goto trigger_cleanup;
18233 }
18234
18235 /* A long-standing parser bug is that this syntax was allowed:
18236 **
18237 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
18238 ** ^^^^^^^^
18239 **
18240 ** To maintain backwards compatibility, ignore the database
18241 ** name on pTableName if we are reparsing out of SQLITE_MASTER.
18242 */
18243 if( db->init.busy && iDb!=1 ){
18244 sqlite3DbFree(db, pTableName->a[0].zDatabase);
18245 pTableName->a[0].zDatabase = 0;
18246 }
18247
18248 /* If the trigger name was unqualified, and the table is a temp table,
18249 ** then set iDb to 1 to create the trigger in the temporary database.
18250 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
18251 ** exist, the error is caught by the block below.
18252 */
18253 pTab = sqlite3SrcListLookup(pParse, pTableName);
18254 if( db->init.busy==0 && pName2->n==0 && pTab
18255 && pTab->pSchema==db->aDb[1].pSchema ){
18256 iDb = 1;
18257 }
18258
18259 /* Ensure the table name matches database name and that the table exists */
18260 if( db->mallocFailed ) goto trigger_cleanup;
18261 assert( pTableName->nSrc==1 );
18262 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
18263 if( sqlite3FixSrcList(&sFix, pTableName) ){
18264 goto trigger_cleanup;
18265 }
18266 pTab = sqlite3SrcListLookup(pParse, pTableName);
18267 if( !pTab ){
18268 /* The table does not exist. */
18269 if( db->init.iDb==1 ){
18270 /* Ticket #3810.
18271 ** Normally, whenever a table is dropped, all associated triggers are
18272 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
18273 ** and the table is dropped by a different database connection, the
18274 ** trigger is not visible to the database connection that does the
18275 ** drop so the trigger cannot be dropped. This results in an
18276 ** "orphaned trigger" - a trigger whose associated table is missing.
18277 */
18278 db->init.orphanTrigger = 1;
18279 }
18280 goto trigger_cleanup;
18281 }
18282 if( IsVirtual(pTab) ){
18283 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
18284 goto trigger_cleanup;
18285 }
18286
18287 /* Check that the trigger name is not reserved and that no trigger of the
18288 ** specified name exists */
18289 zName = sqlite3NameFromToken(db, pName);
18290 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
18291 goto trigger_cleanup;
18292 }
18293 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
18294 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
18295 if( !noErr ){
18296 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
18297 }else{
18298 assert( !db->init.busy );
18299 sqlite3CodeVerifySchema(pParse, iDb);
18300 }
18301 goto trigger_cleanup;
18302 }
18303
18304 /* Do not create a trigger on a system table */
18305 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
18306 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
18307 goto trigger_cleanup;
18308 }
18309
18310 /* INSTEAD of triggers are only for views and views only support INSTEAD
18311 ** of triggers.
18312 */
18313 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
18314 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
18315 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
18316 goto trigger_cleanup;
18317 }
18318 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
18319 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
18320 " trigger on table: %S", pTableName, 0);
18321 goto trigger_cleanup;
18322 }
18323
18324 #ifndef SQLITE_OMIT_AUTHORIZATION
18325 {
18326 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
18327 int code = SQLITE_CREATE_TRIGGER;
18328 const char *zDb = db->aDb[iTabDb].zDbSName;
18329 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
18330 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
18331 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
18332 goto trigger_cleanup;
18333 }
18334 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
18335 goto trigger_cleanup;
18336 }
18337 }
18338 #endif
18339
18340 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
18341 ** cannot appear on views. So we might as well translate every
18342 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
18343 ** elsewhere.
18344 */
18345 if (tr_tm == TK_INSTEAD){
18346 tr_tm = TK_BEFORE;
18347 }
18348
18349 /* Build the Trigger object */
18350 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
18351 if( pTrigger==0 ) goto trigger_cleanup;
18352 pTrigger->zName = zName;
18353 zName = 0;
18354 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
18355 pTrigger->pSchema = db->aDb[iDb].pSchema;
18356 pTrigger->pTabSchema = pTab->pSchema;
18357 pTrigger->op = (u8)op;
18358 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
18359 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
18360 pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
18361 assert( pParse->pNewTrigger==0 );
18362 pParse->pNewTrigger = pTrigger;
18363
18364 trigger_cleanup:
18365 sqlite3DbFree(db, zName);
18366 sqlite3SrcListDelete(db, pTableName);
18367 sqlite3IdListDelete(db, pColumns);
18368 sqlite3ExprDelete(db, pWhen);
18369 if( !pParse->pNewTrigger ){
18370 sqlite3DeleteTrigger(db, pTrigger);
18371 }else{
18372 assert( pParse->pNewTrigger==pTrigger );
18373 }
18374 }
18375
18376 /*
18377 ** This routine is called after all of the trigger actions have been parsed
18378 ** in order to complete the process of building the trigger.
18379 */
18380 SQLITE_PRIVATE void sqlite3FinishTrigger(
18381 Parse *pParse, /* Parser context */
18382 TriggerStep *pStepList, /* The triggered program */
18383 Token *pAll /* Token that describes the complete CREATE TRIGGER */
18384 ){
18385 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
18386 char *zName; /* Name of trigger */
18387 sqlite3 *db = pParse->db; /* The database */
18388 DbFixer sFix; /* Fixer object */
18389 int iDb; /* Database containing the trigger */
18390 Token nameToken; /* Trigger name for error reporting */
18391
18392 pParse->pNewTrigger = 0;
18393 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
18394 zName = pTrig->zName;
18395 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
18396 pTrig->step_list = pStepList;
18397 while( pStepList ){
18398 pStepList->pTrig = pTrig;
18399 pStepList = pStepList->pNext;
18400 }
18401 sqlite3TokenInit(&nameToken, pTrig->zName);
18402 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
18403 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
18404 || sqlite3FixExpr(&sFix, pTrig->pWhen)
18405 ){
18406 goto triggerfinish_cleanup;
18407 }
18408
18409 /* if we are not initializing,
18410 ** build the sqlite_master entry
18411 */
18412 if( !db->init.busy ){
18413 Vdbe *v;
18414 char *z;
18415
18416 /* Make an entry in the sqlite_master table */
18417 v = sqlite3GetVdbe(pParse);
18418 if( v==0 ) goto triggerfinish_cleanup;
18419 sqlite3BeginWriteOperation(pParse, 0, iDb);
18420 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
18421 sqlite3NestedParse(pParse,
18422 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
18423 db->aDb[iDb].zDbSName, MASTER_NAME, zName,
18424 pTrig->table, z);
18425 sqlite3DbFree(db, z);
18426 sqlite3ChangeCookie(pParse, iDb);
18427 sqlite3VdbeAddParseSchemaOp(v, iDb,
18428 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
18429 }
18430
18431 if( db->init.busy ){
18432 Trigger *pLink = pTrig;
18433 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
18434 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
18435 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
18436 if( pTrig ){
18437 sqlite3OomFault(db);
18438 }else if( pLink->pSchema==pLink->pTabSchema ){
18439 Table *pTab;
18440 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
18441 assert( pTab!=0 );
18442 pLink->pNext = pTab->pTrigger;
18443 pTab->pTrigger = pLink;
18444 }
18445 }
18446
18447 triggerfinish_cleanup:
18448 sqlite3DeleteTrigger(db, pTrig);
18449 assert( !pParse->pNewTrigger );
18450 sqlite3DeleteTriggerStep(db, pStepList);
18451 }
18452
18453 /*
18454 ** Turn a SELECT statement (that the pSelect parameter points to) into
18455 ** a trigger step. Return a pointer to a TriggerStep structure.
18456 **
18457 ** The parser calls this routine when it finds a SELECT statement in
18458 ** body of a TRIGGER.
18459 */
18460 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelec t){
18461 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
18462 if( pTriggerStep==0 ) {
18463 sqlite3SelectDelete(db, pSelect);
18464 return 0;
18465 }
18466 pTriggerStep->op = TK_SELECT;
18467 pTriggerStep->pSelect = pSelect;
18468 pTriggerStep->orconf = OE_Default;
18469 return pTriggerStep;
18470 }
18471
18472 /*
18473 ** Allocate space to hold a new trigger step. The allocated space
18474 ** holds both the TriggerStep object and the TriggerStep.target.z string.
18475 **
18476 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
18477 */
18478 static TriggerStep *triggerStepAllocate(
18479 sqlite3 *db, /* Database connection */
18480 u8 op, /* Trigger opcode */
18481 Token *pName /* The target name */
18482 ){
18483 TriggerStep *pTriggerStep;
18484
18485 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
18486 if( pTriggerStep ){
18487 char *z = (char*)&pTriggerStep[1];
18488 memcpy(z, pName->z, pName->n);
18489 sqlite3Dequote(z);
18490 pTriggerStep->zTarget = z;
18491 pTriggerStep->op = op;
18492 }
18493 return pTriggerStep;
18494 }
18495
18496 /*
18497 ** Build a trigger step out of an INSERT statement. Return a pointer
18498 ** to the new trigger step.
18499 **
18500 ** The parser calls this routine when it sees an INSERT inside the
18501 ** body of a trigger.
18502 */
18503 SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
18504 sqlite3 *db, /* The database connection */
18505 Token *pTableName, /* Name of the table into which we insert */
18506 IdList *pColumn, /* List of columns in pTableName to insert into */
18507 Select *pSelect, /* A SELECT statement that supplies values */
18508 u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
18509 ){
18510 TriggerStep *pTriggerStep;
18511
18512 assert(pSelect != 0 || db->mallocFailed);
18513
18514 pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
18515 if( pTriggerStep ){
18516 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
18517 pTriggerStep->pIdList = pColumn;
18518 pTriggerStep->orconf = orconf;
18519 }else{
18520 sqlite3IdListDelete(db, pColumn);
18521 }
18522 sqlite3SelectDelete(db, pSelect);
18523
18524 return pTriggerStep;
18525 }
18526
18527 /*
18528 ** Construct a trigger step that implements an UPDATE statement and return
18529 ** a pointer to that trigger step. The parser calls this routine when it
18530 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
18531 */
18532 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
18533 sqlite3 *db, /* The database connection */
18534 Token *pTableName, /* Name of the table to be updated */
18535 ExprList *pEList, /* The SET clause: list of column and new values */
18536 Expr *pWhere, /* The WHERE clause */
18537 u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
18538 ){
18539 TriggerStep *pTriggerStep;
18540
18541 pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
18542 if( pTriggerStep ){
18543 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
18544 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
18545 pTriggerStep->orconf = orconf;
18546 }
18547 sqlite3ExprListDelete(db, pEList);
18548 sqlite3ExprDelete(db, pWhere);
18549 return pTriggerStep;
18550 }
18551
18552 /*
18553 ** Construct a trigger step that implements a DELETE statement and return
18554 ** a pointer to that trigger step. The parser calls this routine when it
18555 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
18556 */
18557 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
18558 sqlite3 *db, /* Database connection */
18559 Token *pTableName, /* The table from which rows are deleted */
18560 Expr *pWhere /* The WHERE clause */
18561 ){
18562 TriggerStep *pTriggerStep;
18563
18564 pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
18565 if( pTriggerStep ){
18566 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
18567 pTriggerStep->orconf = OE_Default;
18568 }
18569 sqlite3ExprDelete(db, pWhere);
18570 return pTriggerStep;
18571 }
18572
18573 /*
18574 ** Recursively delete a Trigger structure
18575 */
18576 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
18577 if( pTrigger==0 ) return;
18578 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
18579 sqlite3DbFree(db, pTrigger->zName);
18580 sqlite3DbFree(db, pTrigger->table);
18581 sqlite3ExprDelete(db, pTrigger->pWhen);
18582 sqlite3IdListDelete(db, pTrigger->pColumns);
18583 sqlite3DbFree(db, pTrigger);
18584 }
18585
18586 /*
18587 ** This function is called to drop a trigger from the database schema.
18588 **
18589 ** This may be called directly from the parser and therefore identifies
18590 ** the trigger by name. The sqlite3DropTriggerPtr() routine does the
18591 ** same job as this routine except it takes a pointer to the trigger
18592 ** instead of the trigger name.
18593 **/
18594 SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr) {
18595 Trigger *pTrigger = 0;
18596 int i;
18597 const char *zDb;
18598 const char *zName;
18599 sqlite3 *db = pParse->db;
18600
18601 if( db->mallocFailed ) goto drop_trigger_cleanup;
18602 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
18603 goto drop_trigger_cleanup;
18604 }
18605
18606 assert( pName->nSrc==1 );
18607 zDb = pName->a[0].zDatabase;
18608 zName = pName->a[0].zName;
18609 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
18610 for(i=OMIT_TEMPDB; i<db->nDb; i++){
18611 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
18612 if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
18613 assert( sqlite3SchemaMutexHeld(db, j, 0) );
18614 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
18615 if( pTrigger ) break;
18616 }
18617 if( !pTrigger ){
18618 if( !noErr ){
18619 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
18620 }else{
18621 sqlite3CodeVerifyNamedSchema(pParse, zDb);
18622 }
18623 pParse->checkSchema = 1;
18624 goto drop_trigger_cleanup;
18625 }
18626 sqlite3DropTriggerPtr(pParse, pTrigger);
18627
18628 drop_trigger_cleanup:
18629 sqlite3SrcListDelete(db, pName);
18630 }
18631
18632 /*
18633 ** Return a pointer to the Table structure for the table that a trigger
18634 ** is set on.
18635 */
18636 static Table *tableOfTrigger(Trigger *pTrigger){
18637 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
18638 }
18639
18640
18641 /*
18642 ** Drop a trigger given a pointer to that trigger.
18643 */
18644 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
18645 Table *pTable;
18646 Vdbe *v;
18647 sqlite3 *db = pParse->db;
18648 int iDb;
18649
18650 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
18651 assert( iDb>=0 && iDb<db->nDb );
18652 pTable = tableOfTrigger(pTrigger);
18653 assert( pTable );
18654 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
18655 #ifndef SQLITE_OMIT_AUTHORIZATION
18656 {
18657 int code = SQLITE_DROP_TRIGGER;
18658 const char *zDb = db->aDb[iDb].zDbSName;
18659 const char *zTab = SCHEMA_TABLE(iDb);
18660 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
18661 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
18662 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
18663 return;
18664 }
18665 }
18666 #endif
18667
18668 /* Generate code to destroy the database record of the trigger.
18669 */
18670 assert( pTable!=0 );
18671 if( (v = sqlite3GetVdbe(pParse))!=0 ){
18672 sqlite3NestedParse(pParse,
18673 "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
18674 db->aDb[iDb].zDbSName, MASTER_NAME, pTrigger->zName
18675 );
18676 sqlite3ChangeCookie(pParse, iDb);
18677 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
18678 }
18679 }
18680
18681 /*
18682 ** Remove a trigger from the hash tables of the sqlite* pointer.
18683 */
18684 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const ch ar *zName){
18685 Trigger *pTrigger;
18686 Hash *pHash;
18687
18688 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
18689 pHash = &(db->aDb[iDb].pSchema->trigHash);
18690 pTrigger = sqlite3HashInsert(pHash, zName, 0);
18691 if( ALWAYS(pTrigger) ){
18692 if( pTrigger->pSchema==pTrigger->pTabSchema ){
18693 Table *pTab = tableOfTrigger(pTrigger);
18694 Trigger **pp;
18695 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
18696 *pp = (*pp)->pNext;
18697 }
18698 sqlite3DeleteTrigger(db, pTrigger);
18699 db->flags |= SQLITE_InternChanges;
18700 }
18701 }
18702
18703 /*
18704 ** pEList is the SET clause of an UPDATE statement. Each entry
18705 ** in pEList is of the format <id>=<expr>. If any of the entries
18706 ** in pEList have an <id> which matches an identifier in pIdList,
18707 ** then return TRUE. If pIdList==NULL, then it is considered a
18708 ** wildcard that matches anything. Likewise if pEList==NULL then
18709 ** it matches anything so always return true. Return false only
18710 ** if there is no match.
18711 */
18712 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
18713 int e;
18714 if( pIdList==0 || NEVER(pEList==0) ) return 1;
18715 for(e=0; e<pEList->nExpr; e++){
18716 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
18717 }
18718 return 0;
18719 }
18720
18721 /*
18722 ** Return a list of all triggers on table pTab if there exists at least
18723 ** one trigger that must be fired when an operation of type 'op' is
18724 ** performed on the table, and, if that operation is an UPDATE, if at
18725 ** least one of the columns in pChanges is being modified.
18726 */
18727 SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
18728 Parse *pParse, /* Parse context */
18729 Table *pTab, /* The table the contains the triggers */
18730 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
18731 ExprList *pChanges, /* Columns that change in an UPDATE statement */
18732 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
18733 ){
18734 int mask = 0;
18735 Trigger *pList = 0;
18736 Trigger *p;
18737
18738 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
18739 pList = sqlite3TriggerList(pParse, pTab);
18740 }
18741 assert( pList==0 || IsVirtual(pTab)==0 );
18742 for(p=pList; p; p=p->pNext){
18743 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
18744 mask |= p->tr_tm;
18745 }
18746 }
18747 if( pMask ){
18748 *pMask = mask;
18749 }
18750 return (mask ? pList : 0);
18751 }
18752
18753 /*
18754 ** Convert the pStep->zTarget string into a SrcList and return a pointer
18755 ** to that SrcList.
18756 **
18757 ** This routine adds a specific database name, if needed, to the target when
18758 ** forming the SrcList. This prevents a trigger in one database from
18759 ** referring to a target in another database. An exception is when the
18760 ** trigger is in TEMP in which case it can refer to any other database it
18761 ** wants.
18762 */
18763 static SrcList *targetSrcList(
18764 Parse *pParse, /* The parsing context */
18765 TriggerStep *pStep /* The trigger containing the target token */
18766 ){
18767 sqlite3 *db = pParse->db;
18768 int iDb; /* Index of the database to use */
18769 SrcList *pSrc; /* SrcList to be returned */
18770
18771 pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
18772 if( pSrc ){
18773 assert( pSrc->nSrc>0 );
18774 pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
18775 iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
18776 if( iDb==0 || iDb>=2 ){
18777 const char *zDb;
18778 assert( iDb<db->nDb );
18779 zDb = db->aDb[iDb].zDbSName;
18780 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, zDb);
18781 }
18782 }
18783 return pSrc;
18784 }
18785
18786 /*
18787 ** Generate VDBE code for the statements inside the body of a single
18788 ** trigger.
18789 */
18790 static int codeTriggerProgram(
18791 Parse *pParse, /* The parser context */
18792 TriggerStep *pStepList, /* List of statements inside the trigger body */
18793 int orconf /* Conflict algorithm. (OE_Abort, etc) */
18794 ){
18795 TriggerStep *pStep;
18796 Vdbe *v = pParse->pVdbe;
18797 sqlite3 *db = pParse->db;
18798
18799 assert( pParse->pTriggerTab && pParse->pToplevel );
18800 assert( pStepList );
18801 assert( v!=0 );
18802 for(pStep=pStepList; pStep; pStep=pStep->pNext){
18803 /* Figure out the ON CONFLICT policy that will be used for this step
18804 ** of the trigger program. If the statement that caused this trigger
18805 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
18806 ** the ON CONFLICT policy that was specified as part of the trigger
18807 ** step statement. Example:
18808 **
18809 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
18810 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
18811 ** END;
18812 **
18813 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
18814 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
18815 */
18816 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
18817 assert( pParse->okConstFactor==0 );
18818
18819 switch( pStep->op ){
18820 case TK_UPDATE: {
18821 sqlite3Update(pParse,
18822 targetSrcList(pParse, pStep),
18823 sqlite3ExprListDup(db, pStep->pExprList, 0),
18824 sqlite3ExprDup(db, pStep->pWhere, 0),
18825 pParse->eOrconf
18826 );
18827 break;
18828 }
18829 case TK_INSERT: {
18830 sqlite3Insert(pParse,
18831 targetSrcList(pParse, pStep),
18832 sqlite3SelectDup(db, pStep->pSelect, 0),
18833 sqlite3IdListDup(db, pStep->pIdList),
18834 pParse->eOrconf
18835 );
18836 break;
18837 }
18838 case TK_DELETE: {
18839 sqlite3DeleteFrom(pParse,
18840 targetSrcList(pParse, pStep),
18841 sqlite3ExprDup(db, pStep->pWhere, 0)
18842 );
18843 break;
18844 }
18845 default: assert( pStep->op==TK_SELECT ); {
18846 SelectDest sDest;
18847 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
18848 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
18849 sqlite3Select(pParse, pSelect, &sDest);
18850 sqlite3SelectDelete(db, pSelect);
18851 break;
18852 }
18853 }
18854 if( pStep->op!=TK_SELECT ){
18855 sqlite3VdbeAddOp0(v, OP_ResetCount);
18856 }
18857 }
18858
18859 return 0;
18860 }
18861
18862 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
18863 /*
18864 ** This function is used to add VdbeComment() annotations to a VDBE
18865 ** program. It is not used in production code, only for debugging.
18866 */
18867 static const char *onErrorText(int onError){
18868 switch( onError ){
18869 case OE_Abort: return "abort";
18870 case OE_Rollback: return "rollback";
18871 case OE_Fail: return "fail";
18872 case OE_Replace: return "replace";
18873 case OE_Ignore: return "ignore";
18874 case OE_Default: return "default";
18875 }
18876 return "n/a";
18877 }
18878 #endif
18879
18880 /*
18881 ** Parse context structure pFrom has just been used to create a sub-vdbe
18882 ** (trigger program). If an error has occurred, transfer error information
18883 ** from pFrom to pTo.
18884 */
18885 static void transferParseError(Parse *pTo, Parse *pFrom){
18886 assert( pFrom->zErrMsg==0 || pFrom->nErr );
18887 assert( pTo->zErrMsg==0 || pTo->nErr );
18888 if( pTo->nErr==0 ){
18889 pTo->zErrMsg = pFrom->zErrMsg;
18890 pTo->nErr = pFrom->nErr;
18891 pTo->rc = pFrom->rc;
18892 }else{
18893 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
18894 }
18895 }
18896
18897 /*
18898 ** Create and populate a new TriggerPrg object with a sub-program
18899 ** implementing trigger pTrigger with ON CONFLICT policy orconf.
18900 */
18901 static TriggerPrg *codeRowTrigger(
18902 Parse *pParse, /* Current parse context */
18903 Trigger *pTrigger, /* Trigger to code */
18904 Table *pTab, /* The table pTrigger is attached to */
18905 int orconf /* ON CONFLICT policy to code trigger program with */
18906 ){
18907 Parse *pTop = sqlite3ParseToplevel(pParse);
18908 sqlite3 *db = pParse->db; /* Database handle */
18909 TriggerPrg *pPrg; /* Value to return */
18910 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
18911 Vdbe *v; /* Temporary VM */
18912 NameContext sNC; /* Name context for sub-vdbe */
18913 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
18914 Parse *pSubParse; /* Parse context for sub-vdbe */
18915 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
18916
18917 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
18918 assert( pTop->pVdbe );
18919
18920 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
18921 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
18922 ** list of the top-level Parse object sooner rather than later. */
18923 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
18924 if( !pPrg ) return 0;
18925 pPrg->pNext = pTop->pTriggerPrg;
18926 pTop->pTriggerPrg = pPrg;
18927 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
18928 if( !pProgram ) return 0;
18929 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
18930 pPrg->pTrigger = pTrigger;
18931 pPrg->orconf = orconf;
18932 pPrg->aColmask[0] = 0xffffffff;
18933 pPrg->aColmask[1] = 0xffffffff;
18934
18935 /* Allocate and populate a new Parse context to use for coding the
18936 ** trigger sub-program. */
18937 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
18938 if( !pSubParse ) return 0;
18939 memset(&sNC, 0, sizeof(sNC));
18940 sNC.pParse = pSubParse;
18941 pSubParse->db = db;
18942 pSubParse->pTriggerTab = pTab;
18943 pSubParse->pToplevel = pTop;
18944 pSubParse->zAuthContext = pTrigger->zName;
18945 pSubParse->eTriggerOp = pTrigger->op;
18946 pSubParse->nQueryLoop = pParse->nQueryLoop;
18947
18948 v = sqlite3GetVdbe(pSubParse);
18949 if( v ){
18950 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
18951 pTrigger->zName, onErrorText(orconf),
18952 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
18953 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
18954 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
18955 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
18956 pTab->zName
18957 ));
18958 #ifndef SQLITE_OMIT_TRACE
18959 sqlite3VdbeChangeP4(v, -1,
18960 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
18961 );
18962 #endif
18963
18964 /* If one was specified, code the WHEN clause. If it evaluates to false
18965 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
18966 ** OP_Halt inserted at the end of the program. */
18967 if( pTrigger->pWhen ){
18968 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
18969 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
18970 && db->mallocFailed==0
18971 ){
18972 iEndTrigger = sqlite3VdbeMakeLabel(v);
18973 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
18974 }
18975 sqlite3ExprDelete(db, pWhen);
18976 }
18977
18978 /* Code the trigger program into the sub-vdbe. */
18979 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
18980
18981 /* Insert an OP_Halt at the end of the sub-program. */
18982 if( iEndTrigger ){
18983 sqlite3VdbeResolveLabel(v, iEndTrigger);
18984 }
18985 sqlite3VdbeAddOp0(v, OP_Halt);
18986 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
18987
18988 transferParseError(pParse, pSubParse);
18989 if( db->mallocFailed==0 ){
18990 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
18991 }
18992 pProgram->nMem = pSubParse->nMem;
18993 pProgram->nCsr = pSubParse->nTab;
18994 pProgram->token = (void *)pTrigger;
18995 pPrg->aColmask[0] = pSubParse->oldmask;
18996 pPrg->aColmask[1] = pSubParse->newmask;
18997 sqlite3VdbeDelete(v);
18998 }
18999
19000 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
19001 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
19002 sqlite3ParserReset(pSubParse);
19003 sqlite3StackFree(db, pSubParse);
19004
19005 return pPrg;
19006 }
19007
19008 /*
19009 ** Return a pointer to a TriggerPrg object containing the sub-program for
19010 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
19011 ** TriggerPrg object exists, a new object is allocated and populated before
19012 ** being returned.
19013 */
19014 static TriggerPrg *getRowTrigger(
19015 Parse *pParse, /* Current parse context */
19016 Trigger *pTrigger, /* Trigger to code */
19017 Table *pTab, /* The table trigger pTrigger is attached to */
19018 int orconf /* ON CONFLICT algorithm. */
19019 ){
19020 Parse *pRoot = sqlite3ParseToplevel(pParse);
19021 TriggerPrg *pPrg;
19022
19023 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
19024
19025 /* It may be that this trigger has already been coded (or is in the
19026 ** process of being coded). If this is the case, then an entry with
19027 ** a matching TriggerPrg.pTrigger field will be present somewhere
19028 ** in the Parse.pTriggerPrg list. Search for such an entry. */
19029 for(pPrg=pRoot->pTriggerPrg;
19030 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
19031 pPrg=pPrg->pNext
19032 );
19033
19034 /* If an existing TriggerPrg could not be located, create a new one. */
19035 if( !pPrg ){
19036 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
19037 }
19038
19039 return pPrg;
19040 }
19041
19042 /*
19043 ** Generate code for the trigger program associated with trigger p on
19044 ** table pTab. The reg, orconf and ignoreJump parameters passed to this
19045 ** function are the same as those described in the header function for
19046 ** sqlite3CodeRowTrigger()
19047 */
19048 SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
19049 Parse *pParse, /* Parse context */
19050 Trigger *p, /* Trigger to code */
19051 Table *pTab, /* The table to code triggers from */
19052 int reg, /* Reg array containing OLD.* and NEW.* values */
19053 int orconf, /* ON CONFLICT policy */
19054 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
19055 ){
19056 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
19057 TriggerPrg *pPrg;
19058 pPrg = getRowTrigger(pParse, p, pTab, orconf);
19059 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
19060
19061 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
19062 ** is a pointer to the sub-vdbe containing the trigger program. */
19063 if( pPrg ){
19064 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
19065
19066 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
19067 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
19068 VdbeComment(
19069 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
19070
19071 /* Set the P5 operand of the OP_Program instruction to non-zero if
19072 ** recursive invocation of this trigger program is disallowed. Recursive
19073 ** invocation is disallowed if (a) the sub-program is really a trigger,
19074 ** not a foreign key action, and (b) the flag to enable recursive triggers
19075 ** is clear. */
19076 sqlite3VdbeChangeP5(v, (u8)bRecursive);
19077 }
19078 }
19079
19080 /*
19081 ** This is called to code the required FOR EACH ROW triggers for an operation
19082 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
19083 ** is given by the op parameter. The tr_tm parameter determines whether the
19084 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
19085 ** parameter pChanges is passed the list of columns being modified.
19086 **
19087 ** If there are no triggers that fire at the specified time for the specified
19088 ** operation on pTab, this function is a no-op.
19089 **
19090 ** The reg argument is the address of the first in an array of registers
19091 ** that contain the values substituted for the new.* and old.* references
19092 ** in the trigger program. If N is the number of columns in table pTab
19093 ** (a copy of pTab->nCol), then registers are populated as follows:
19094 **
19095 ** Register Contains
19096 ** ------------------------------------------------------
19097 ** reg+0 OLD.rowid
19098 ** reg+1 OLD.* value of left-most column of pTab
19099 ** ... ...
19100 ** reg+N OLD.* value of right-most column of pTab
19101 ** reg+N+1 NEW.rowid
19102 ** reg+N+2 OLD.* value of left-most column of pTab
19103 ** ... ...
19104 ** reg+N+N+1 NEW.* value of right-most column of pTab
19105 **
19106 ** For ON DELETE triggers, the registers containing the NEW.* values will
19107 ** never be accessed by the trigger program, so they are not allocated or
19108 ** populated by the caller (there is no data to populate them with anyway).
19109 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
19110 ** are never accessed, and so are not allocated by the caller. So, for an
19111 ** ON INSERT trigger, the value passed to this function as parameter reg
19112 ** is not a readable register, although registers (reg+N) through
19113 ** (reg+N+N+1) are.
19114 **
19115 ** Parameter orconf is the default conflict resolution algorithm for the
19116 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
19117 ** is the instruction that control should jump to if a trigger program
19118 ** raises an IGNORE exception.
19119 */
19120 SQLITE_PRIVATE void sqlite3CodeRowTrigger(
19121 Parse *pParse, /* Parse context */
19122 Trigger *pTrigger, /* List of triggers on table pTab */
19123 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
19124 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
19125 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
19126 Table *pTab, /* The table to code triggers from */
19127 int reg, /* The first in an array of registers (see above) */
19128 int orconf, /* ON CONFLICT policy */
19129 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
19130 ){
19131 Trigger *p; /* Used to iterate through pTrigger list */
19132
19133 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
19134 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
19135 assert( (op==TK_UPDATE)==(pChanges!=0) );
19136
19137 for(p=pTrigger; p; p=p->pNext){
19138
19139 /* Sanity checking: The schema for the trigger and for the table are
19140 ** always defined. The trigger must be in the same schema as the table
19141 ** or else it must be a TEMP trigger. */
19142 assert( p->pSchema!=0 );
19143 assert( p->pTabSchema!=0 );
19144 assert( p->pSchema==p->pTabSchema
19145 || p->pSchema==pParse->db->aDb[1].pSchema );
19146
19147 /* Determine whether we should code this trigger */
19148 if( p->op==op
19149 && p->tr_tm==tr_tm
19150 && checkColumnOverlap(p->pColumns, pChanges)
19151 ){
19152 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
19153 }
19154 }
19155 }
19156
19157 /*
19158 ** Triggers may access values stored in the old.* or new.* pseudo-table.
19159 ** This function returns a 32-bit bitmask indicating which columns of the
19160 ** old.* or new.* tables actually are used by triggers. This information
19161 ** may be used by the caller, for example, to avoid having to load the entire
19162 ** old.* record into memory when executing an UPDATE or DELETE command.
19163 **
19164 ** Bit 0 of the returned mask is set if the left-most column of the
19165 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
19166 ** the second leftmost column value is required, and so on. If there
19167 ** are more than 32 columns in the table, and at least one of the columns
19168 ** with an index greater than 32 may be accessed, 0xffffffff is returned.
19169 **
19170 ** It is not possible to determine if the old.rowid or new.rowid column is
19171 ** accessed by triggers. The caller must always assume that it is.
19172 **
19173 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
19174 ** applies to the old.* table. If 1, the new.* table.
19175 **
19176 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
19177 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
19178 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
19179 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
19180 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
19181 */
19182 SQLITE_PRIVATE u32 sqlite3TriggerColmask(
19183 Parse *pParse, /* Parse context */
19184 Trigger *pTrigger, /* List of triggers on table pTab */
19185 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
19186 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
19187 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
19188 Table *pTab, /* The table to code triggers from */
19189 int orconf /* Default ON CONFLICT policy for trigger steps */
19190 ){
19191 const int op = pChanges ? TK_UPDATE : TK_DELETE;
19192 u32 mask = 0;
19193 Trigger *p;
19194
19195 assert( isNew==1 || isNew==0 );
19196 for(p=pTrigger; p; p=p->pNext){
19197 if( p->op==op && (tr_tm&p->tr_tm)
19198 && checkColumnOverlap(p->pColumns,pChanges)
19199 ){
19200 TriggerPrg *pPrg;
19201 pPrg = getRowTrigger(pParse, p, pTab, orconf);
19202 if( pPrg ){
19203 mask |= pPrg->aColmask[isNew];
19204 }
19205 }
19206 }
19207
19208 return mask;
19209 }
19210
19211 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
19212
19213 /************** End of trigger.c *********************************************/
19214 /************** Begin file update.c ******************************************/
19215 /*
19216 ** 2001 September 15
19217 **
19218 ** The author disclaims copyright to this source code. In place of
19219 ** a legal notice, here is a blessing:
19220 **
19221 ** May you do good and not evil.
19222 ** May you find forgiveness for yourself and forgive others.
19223 ** May you share freely, never taking more than you give.
19224 **
19225 *************************************************************************
19226 ** This file contains C code routines that are called by the parser
19227 ** to handle UPDATE statements.
19228 */
19229 /* #include "sqliteInt.h" */
19230
19231 #ifndef SQLITE_OMIT_VIRTUALTABLE
19232 /* Forward declaration */
19233 static void updateVirtualTable(
19234 Parse *pParse, /* The parsing context */
19235 SrcList *pSrc, /* The virtual table to be modified */
19236 Table *pTab, /* The virtual table */
19237 ExprList *pChanges, /* The columns to change in the UPDATE statement */
19238 Expr *pRowidExpr, /* Expression used to recompute the rowid */
19239 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
19240 Expr *pWhere, /* WHERE clause of the UPDATE statement */
19241 int onError /* ON CONFLICT strategy */
19242 );
19243 #endif /* SQLITE_OMIT_VIRTUALTABLE */
19244
19245 /*
19246 ** The most recently coded instruction was an OP_Column to retrieve the
19247 ** i-th column of table pTab. This routine sets the P4 parameter of the
19248 ** OP_Column to the default value, if any.
19249 **
19250 ** The default value of a column is specified by a DEFAULT clause in the
19251 ** column definition. This was either supplied by the user when the table
19252 ** was created, or added later to the table definition by an ALTER TABLE
19253 ** command. If the latter, then the row-records in the table btree on disk
19254 ** may not contain a value for the column and the default value, taken
19255 ** from the P4 parameter of the OP_Column instruction, is returned instead.
19256 ** If the former, then all row-records are guaranteed to include a value
19257 ** for the column and the P4 value is not required.
19258 **
19259 ** Column definitions created by an ALTER TABLE command may only have
19260 ** literal default values specified: a number, null or a string. (If a more
19261 ** complicated default expression value was provided, it is evaluated
19262 ** when the ALTER TABLE is executed and one of the literal values written
19263 ** into the sqlite_master table.)
19264 **
19265 ** Therefore, the P4 parameter is only required if the default value for
19266 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
19267 ** function is capable of transforming these types of expressions into
19268 ** sqlite3_value objects.
19269 **
19270 ** If parameter iReg is not negative, code an OP_RealAffinity instruction
19271 ** on register iReg. This is used when an equivalent integer value is
19272 ** stored in place of an 8-byte floating point value in order to save
19273 ** space.
19274 */
19275 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
19276 assert( pTab!=0 );
19277 if( !pTab->pSelect ){
19278 sqlite3_value *pValue = 0;
19279 u8 enc = ENC(sqlite3VdbeDb(v));
19280 Column *pCol = &pTab->aCol[i];
19281 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
19282 assert( i<pTab->nCol );
19283 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
19284 pCol->affinity, &pValue);
19285 if( pValue ){
19286 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
19287 }
19288 }
19289 #ifndef SQLITE_OMIT_FLOATING_POINT
19290 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
19291 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
19292 }
19293 #endif
19294 }
19295
19296 /*
19297 ** Process an UPDATE statement.
19298 **
19299 ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
19300 ** \_______/ \________/ \______/ \________________/
19301 * onError pTabList pChanges pWhere
19302 */
19303 SQLITE_PRIVATE void sqlite3Update(
19304 Parse *pParse, /* The parser context */
19305 SrcList *pTabList, /* The table in which we should change things */
19306 ExprList *pChanges, /* Things to be changed */
19307 Expr *pWhere, /* The WHERE clause. May be null */
19308 int onError /* How to handle constraint errors */
19309 ){
19310 int i, j; /* Loop counters */
19311 Table *pTab; /* The table to be updated */
19312 int addrTop = 0; /* VDBE instruction address of the start of the loop */
19313 WhereInfo *pWInfo; /* Information about the WHERE clause */
19314 Vdbe *v; /* The virtual database engine */
19315 Index *pIdx; /* For looping over indices */
19316 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
19317 int nIdx; /* Number of indices that need updating */
19318 int iBaseCur; /* Base cursor number */
19319 int iDataCur; /* Cursor for the canonical data btree */
19320 int iIdxCur; /* Cursor for the first index */
19321 sqlite3 *db; /* The database structure */
19322 int *aRegIdx = 0; /* First register in array assigned to each index */
19323 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
19324 ** an expression for the i-th column of the table.
19325 ** aXRef[i]==-1 if the i-th column is not changed. */
19326 u8 *aToOpen; /* 1 for tables and indices to be opened */
19327 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
19328 u8 chngRowid; /* Rowid changed in a normal table */
19329 u8 chngKey; /* Either chngPk or chngRowid */
19330 Expr *pRowidExpr = 0; /* Expression defining the new record number */
19331 AuthContext sContext; /* The authorization context */
19332 NameContext sNC; /* The name-context to resolve expressions in */
19333 int iDb; /* Database containing the table being updated */
19334 int eOnePass; /* ONEPASS_XXX value from where.c */
19335 int hasFK; /* True if foreign key processing is required */
19336 int labelBreak; /* Jump here to break out of UPDATE loop */
19337 int labelContinue; /* Jump here to continue next step of UPDATE loop */
19338 int flags; /* Flags for sqlite3WhereBegin() */
19339
19340 #ifndef SQLITE_OMIT_TRIGGER
19341 int isView; /* True when updating a view (INSTEAD OF trigger) */
19342 Trigger *pTrigger; /* List of triggers on pTab, if required */
19343 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
19344 #endif
19345 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
19346 int iEph = 0; /* Ephemeral table holding all primary key values */
19347 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
19348 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
19349 int addrOpen = 0; /* Address of OP_OpenEphemeral */
19350 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
19351 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
19352 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
19353
19354 /* Register Allocations */
19355 int regRowCount = 0; /* A count of rows changed */
19356 int regOldRowid = 0; /* The old rowid */
19357 int regNewRowid = 0; /* The new rowid */
19358 int regNew = 0; /* Content of the NEW.* table in triggers */
19359 int regOld = 0; /* Content of OLD.* table in triggers */
19360 int regRowSet = 0; /* Rowset of rows to be updated */
19361 int regKey = 0; /* composite PRIMARY KEY value */
19362
19363 memset(&sContext, 0, sizeof(sContext));
19364 db = pParse->db;
19365 if( pParse->nErr || db->mallocFailed ){
19366 goto update_cleanup;
19367 }
19368 assert( pTabList->nSrc==1 );
19369
19370 /* Locate the table which we want to update.
19371 */
19372 pTab = sqlite3SrcListLookup(pParse, pTabList);
19373 if( pTab==0 ) goto update_cleanup;
19374 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
19375
19376 /* Figure out if we have any triggers and if the table being
19377 ** updated is a view.
19378 */
19379 #ifndef SQLITE_OMIT_TRIGGER
19380 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
19381 isView = pTab->pSelect!=0;
19382 assert( pTrigger || tmask==0 );
19383 #else
19384 # define pTrigger 0
19385 # define isView 0
19386 # define tmask 0
19387 #endif
19388 #ifdef SQLITE_OMIT_VIEW
19389 # undef isView
19390 # define isView 0
19391 #endif
19392
19393 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
19394 goto update_cleanup;
19395 }
19396 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
19397 goto update_cleanup;
19398 }
19399
19400 /* Allocate a cursors for the main database table and for all indices.
19401 ** The index cursors might not be used, but if they are used they
19402 ** need to occur right after the database cursor. So go ahead and
19403 ** allocate enough space, just in case.
19404 */
19405 pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
19406 iIdxCur = iDataCur+1;
19407 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
19408 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
19409 if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){
19410 iDataCur = pParse->nTab;
19411 pTabList->a[0].iCursor = iDataCur;
19412 }
19413 pParse->nTab++;
19414 }
19415
19416 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
19417 ** Initialize aXRef[] and aToOpen[] to their default values.
19418 */
19419 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
19420 if( aXRef==0 ) goto update_cleanup;
19421 aRegIdx = aXRef+pTab->nCol;
19422 aToOpen = (u8*)(aRegIdx+nIdx);
19423 memset(aToOpen, 1, nIdx+1);
19424 aToOpen[nIdx+1] = 0;
19425 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
19426
19427 /* Initialize the name-context */
19428 memset(&sNC, 0, sizeof(sNC));
19429 sNC.pParse = pParse;
19430 sNC.pSrcList = pTabList;
19431
19432 /* Resolve the column names in all the expressions of the
19433 ** of the UPDATE statement. Also find the column index
19434 ** for each column to be updated in the pChanges array. For each
19435 ** column to be updated, make sure we have authorization to change
19436 ** that column.
19437 */
19438 chngRowid = chngPk = 0;
19439 for(i=0; i<pChanges->nExpr; i++){
19440 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
19441 goto update_cleanup;
19442 }
19443 for(j=0; j<pTab->nCol; j++){
19444 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
19445 if( j==pTab->iPKey ){
19446 chngRowid = 1;
19447 pRowidExpr = pChanges->a[i].pExpr;
19448 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
19449 chngPk = 1;
19450 }
19451 aXRef[j] = i;
19452 break;
19453 }
19454 }
19455 if( j>=pTab->nCol ){
19456 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
19457 j = -1;
19458 chngRowid = 1;
19459 pRowidExpr = pChanges->a[i].pExpr;
19460 }else{
19461 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
19462 pParse->checkSchema = 1;
19463 goto update_cleanup;
19464 }
19465 }
19466 #ifndef SQLITE_OMIT_AUTHORIZATION
19467 {
19468 int rc;
19469 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
19470 j<0 ? "ROWID" : pTab->aCol[j].zName,
19471 db->aDb[iDb].zDbSName);
19472 if( rc==SQLITE_DENY ){
19473 goto update_cleanup;
19474 }else if( rc==SQLITE_IGNORE ){
19475 aXRef[j] = -1;
19476 }
19477 }
19478 #endif
19479 }
19480 assert( (chngRowid & chngPk)==0 );
19481 assert( chngRowid==0 || chngRowid==1 );
19482 assert( chngPk==0 || chngPk==1 );
19483 chngKey = chngRowid + chngPk;
19484
19485 /* The SET expressions are not actually used inside the WHERE loop.
19486 ** So reset the colUsed mask. Unless this is a virtual table. In that
19487 ** case, set all bits of the colUsed mask (to ensure that the virtual
19488 ** table implementation makes all columns available).
19489 */
19490 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
19491
19492 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
19493
19494 /* There is one entry in the aRegIdx[] array for each index on the table
19495 ** being updated. Fill in aRegIdx[] with a register number that will hold
19496 ** the key for accessing each index.
19497 **
19498 ** FIXME: Be smarter about omitting indexes that use expressions.
19499 */
19500 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
19501 int reg;
19502 if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
19503 reg = ++pParse->nMem;
19504 pParse->nMem += pIdx->nColumn;
19505 }else{
19506 reg = 0;
19507 for(i=0; i<pIdx->nKeyCol; i++){
19508 i16 iIdxCol = pIdx->aiColumn[i];
19509 if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
19510 reg = ++pParse->nMem;
19511 pParse->nMem += pIdx->nColumn;
19512 if( (onError==OE_Replace)
19513 || (onError==OE_Default && pIdx->onError==OE_Replace)
19514 ){
19515 bReplace = 1;
19516 }
19517 break;
19518 }
19519 }
19520 }
19521 if( reg==0 ) aToOpen[j+1] = 0;
19522 aRegIdx[j] = reg;
19523 }
19524 if( bReplace ){
19525 /* If REPLACE conflict resolution might be invoked, open cursors on all
19526 ** indexes in case they are needed to delete records. */
19527 memset(aToOpen, 1, nIdx+1);
19528 }
19529
19530 /* Begin generating code. */
19531 v = sqlite3GetVdbe(pParse);
19532 if( v==0 ) goto update_cleanup;
19533 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
19534 sqlite3BeginWriteOperation(pParse, 1, iDb);
19535
19536 /* Allocate required registers. */
19537 if( !IsVirtual(pTab) ){
19538 regRowSet = ++pParse->nMem;
19539 regOldRowid = regNewRowid = ++pParse->nMem;
19540 if( chngPk || pTrigger || hasFK ){
19541 regOld = pParse->nMem + 1;
19542 pParse->nMem += pTab->nCol;
19543 }
19544 if( chngKey || pTrigger || hasFK ){
19545 regNewRowid = ++pParse->nMem;
19546 }
19547 regNew = pParse->nMem + 1;
19548 pParse->nMem += pTab->nCol;
19549 }
19550
19551 /* Start the view context. */
19552 if( isView ){
19553 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
19554 }
19555
19556 /* If we are trying to update a view, realize that view into
19557 ** an ephemeral table.
19558 */
19559 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
19560 if( isView ){
19561 sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
19562 }
19563 #endif
19564
19565 /* Resolve the column names in all the expressions in the
19566 ** WHERE clause.
19567 */
19568 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
19569 goto update_cleanup;
19570 }
19571
19572 #ifndef SQLITE_OMIT_VIRTUALTABLE
19573 /* Virtual tables must be handled separately */
19574 if( IsVirtual(pTab) ){
19575 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
19576 pWhere, onError);
19577 goto update_cleanup;
19578 }
19579 #endif
19580
19581 /* Initialize the count of updated rows */
19582 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
19583 regRowCount = ++pParse->nMem;
19584 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
19585 }
19586
19587 if( HasRowid(pTab) ){
19588 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
19589 }else{
19590 assert( pPk!=0 );
19591 nPk = pPk->nKeyCol;
19592 iPk = pParse->nMem+1;
19593 pParse->nMem += nPk;
19594 regKey = ++pParse->nMem;
19595 iEph = pParse->nTab++;
19596
19597 sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
19598 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
19599 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
19600 }
19601
19602 /* Begin the database scan.
19603 **
19604 ** Do not consider a single-pass strategy for a multi-row update if
19605 ** there are any triggers or foreign keys to process, or rows may
19606 ** be deleted as a result of REPLACE conflict handling. Any of these
19607 ** things might disturb a cursor being used to scan through the table
19608 ** or index, causing a single-pass approach to malfunction. */
19609 flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
19610 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
19611 flags |= WHERE_ONEPASS_MULTIROW;
19612 }
19613 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
19614 if( pWInfo==0 ) goto update_cleanup;
19615
19616 /* A one-pass strategy that might update more than one row may not
19617 ** be used if any column of the index used for the scan is being
19618 ** updated. Otherwise, if there is an index on "b", statements like
19619 ** the following could create an infinite loop:
19620 **
19621 ** UPDATE t1 SET b=b+1 WHERE b>?
19622 **
19623 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
19624 ** strategy that uses an index for which one or more columns are being
19625 ** updated. */
19626 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
19627 if( eOnePass==ONEPASS_MULTI ){
19628 int iCur = aiCurOnePass[1];
19629 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
19630 eOnePass = ONEPASS_OFF;
19631 }
19632 assert( iCur!=iDataCur || !HasRowid(pTab) );
19633 }
19634
19635 if( HasRowid(pTab) ){
19636 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
19637 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
19638 ** leave it in register regOldRowid. */
19639 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
19640 if( eOnePass==ONEPASS_OFF ){
19641 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
19642 }
19643 }else{
19644 /* Read the PK of the current row into an array of registers. In
19645 ** ONEPASS_OFF mode, serialize the array into a record and store it in
19646 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
19647 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
19648 ** is not required) and leave the PK fields in the array of registers. */
19649 for(i=0; i<nPk; i++){
19650 assert( pPk->aiColumn[i]>=0 );
19651 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
19652 }
19653 if( eOnePass ){
19654 sqlite3VdbeChangeToNoop(v, addrOpen);
19655 nKey = nPk;
19656 regKey = iPk;
19657 }else{
19658 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
19659 sqlite3IndexAffinityStr(db, pPk), nPk);
19660 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
19661 }
19662 }
19663
19664 if( eOnePass!=ONEPASS_MULTI ){
19665 sqlite3WhereEnd(pWInfo);
19666 }
19667
19668 labelBreak = sqlite3VdbeMakeLabel(v);
19669 if( !isView ){
19670 int addrOnce = 0;
19671
19672 /* Open every index that needs updating. */
19673 if( eOnePass!=ONEPASS_OFF ){
19674 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
19675 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
19676 }
19677
19678 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
19679 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
19680 }
19681 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
19682 0, 0);
19683 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
19684 }
19685
19686 /* Top of the update loop */
19687 if( eOnePass!=ONEPASS_OFF ){
19688 if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
19689 assert( pPk );
19690 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
19691 VdbeCoverageNeverTaken(v);
19692 }
19693 if( eOnePass==ONEPASS_SINGLE ){
19694 labelContinue = labelBreak;
19695 }else{
19696 labelContinue = sqlite3VdbeMakeLabel(v);
19697 }
19698 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
19699 VdbeCoverageIf(v, pPk==0);
19700 VdbeCoverageIf(v, pPk!=0);
19701 }else if( pPk ){
19702 labelContinue = sqlite3VdbeMakeLabel(v);
19703 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
19704 addrTop = sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);
19705 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
19706 VdbeCoverage(v);
19707 }else{
19708 labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
19709 regOldRowid);
19710 VdbeCoverage(v);
19711 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
19712 VdbeCoverage(v);
19713 }
19714
19715 /* If the record number will change, set register regNewRowid to
19716 ** contain the new value. If the record number is not being modified,
19717 ** then regNewRowid is the same register as regOldRowid, which is
19718 ** already populated. */
19719 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
19720 if( chngRowid ){
19721 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
19722 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
19723 }
19724
19725 /* Compute the old pre-UPDATE content of the row being changed, if that
19726 ** information is needed */
19727 if( chngPk || hasFK || pTrigger ){
19728 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
19729 oldmask |= sqlite3TriggerColmask(pParse,
19730 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
19731 );
19732 for(i=0; i<pTab->nCol; i++){
19733 if( oldmask==0xffffffff
19734 || (i<32 && (oldmask & MASKBIT32(i))!=0)
19735 || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
19736 ){
19737 testcase( oldmask!=0xffffffff && i==31 );
19738 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
19739 }else{
19740 sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
19741 }
19742 }
19743 if( chngRowid==0 && pPk==0 ){
19744 sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
19745 }
19746 }
19747
19748 /* Populate the array of registers beginning at regNew with the new
19749 ** row data. This array is used to check constants, create the new
19750 ** table and index records, and as the values for any new.* references
19751 ** made by triggers.
19752 **
19753 ** If there are one or more BEFORE triggers, then do not populate the
19754 ** registers associated with columns that are (a) not modified by
19755 ** this UPDATE statement and (b) not accessed by new.* references. The
19756 ** values for registers not modified by the UPDATE must be reloaded from
19757 ** the database after the BEFORE triggers are fired anyway (as the trigger
19758 ** may have modified them). So not loading those that are not going to
19759 ** be used eliminates some redundant opcodes.
19760 */
19761 newmask = sqlite3TriggerColmask(
19762 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
19763 );
19764 for(i=0; i<pTab->nCol; i++){
19765 if( i==pTab->iPKey ){
19766 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
19767 }else{
19768 j = aXRef[i];
19769 if( j>=0 ){
19770 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
19771 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
19772 /* This branch loads the value of a column that will not be changed
19773 ** into a register. This is done if there are no BEFORE triggers, or
19774 ** if there are one or more BEFORE triggers that use this value via
19775 ** a new.* reference in a trigger program.
19776 */
19777 testcase( i==31 );
19778 testcase( i==32 );
19779 sqlite3ExprCodeGetColumnToReg(pParse, pTab, i, iDataCur, regNew+i);
19780 }else{
19781 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
19782 }
19783 }
19784 }
19785
19786 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
19787 ** verified. One could argue that this is wrong.
19788 */
19789 if( tmask&TRIGGER_BEFORE ){
19790 sqlite3TableAffinity(v, pTab, regNew);
19791 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
19792 TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
19793
19794 /* The row-trigger may have deleted the row being updated. In this
19795 ** case, jump to the next row. No updates or AFTER triggers are
19796 ** required. This behavior - what happens when the row being updated
19797 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
19798 ** documentation.
19799 */
19800 if( pPk ){
19801 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
19802 VdbeCoverage(v);
19803 }else{
19804 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
19805 VdbeCoverage(v);
19806 }
19807
19808 /* If it did not delete it, the row-trigger may still have modified
19809 ** some of the columns of the row being updated. Load the values for
19810 ** all columns not modified by the update statement into their
19811 ** registers in case this has happened.
19812 */
19813 for(i=0; i<pTab->nCol; i++){
19814 if( aXRef[i]<0 && i!=pTab->iPKey ){
19815 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
19816 }
19817 }
19818 }
19819
19820 if( !isView ){
19821 int addr1 = 0; /* Address of jump instruction */
19822
19823 /* Do constraint checks. */
19824 assert( regOldRowid>0 );
19825 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
19826 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
19827 aXRef);
19828
19829 /* Do FK constraint checks. */
19830 if( hasFK ){
19831 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
19832 }
19833
19834 /* Delete the index entries associated with the current record. */
19835 if( bReplace || chngKey ){
19836 if( pPk ){
19837 addr1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
19838 }else{
19839 addr1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
19840 }
19841 VdbeCoverageNeverTaken(v);
19842 }
19843 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
19844
19845 /* If changing the rowid value, or if there are foreign key constraints
19846 ** to process, delete the old record. Otherwise, add a noop OP_Delete
19847 ** to invoke the pre-update hook.
19848 **
19849 ** That (regNew==regnewRowid+1) is true is also important for the
19850 ** pre-update hook. If the caller invokes preupdate_new(), the returned
19851 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
19852 ** is the column index supplied by the user.
19853 */
19854 assert( regNew==regNewRowid+1 );
19855 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
19856 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
19857 OPFLAG_ISUPDATE | ((hasFK || chngKey) ? 0 : OPFLAG_ISNOOP),
19858 regNewRowid
19859 );
19860 if( eOnePass==ONEPASS_MULTI ){
19861 assert( hasFK==0 && chngKey==0 );
19862 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
19863 }
19864 if( !pParse->nested ){
19865 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
19866 }
19867 #else
19868 if( hasFK || chngKey ){
19869 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
19870 }
19871 #endif
19872 if( bReplace || chngKey ){
19873 sqlite3VdbeJumpHere(v, addr1);
19874 }
19875
19876 if( hasFK ){
19877 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
19878 }
19879
19880 /* Insert the new index entries and the new record. */
19881 sqlite3CompleteInsertion(
19882 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
19883 OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
19884 0, 0
19885 );
19886
19887 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
19888 ** handle rows (possibly in other tables) that refer via a foreign key
19889 ** to the row just updated. */
19890 if( hasFK ){
19891 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
19892 }
19893 }
19894
19895 /* Increment the row counter
19896 */
19897 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
19898 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
19899 }
19900
19901 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
19902 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
19903
19904 /* Repeat the above with the next record to be updated, until
19905 ** all record selected by the WHERE clause have been updated.
19906 */
19907 if( eOnePass==ONEPASS_SINGLE ){
19908 /* Nothing to do at end-of-loop for a single-pass */
19909 }else if( eOnePass==ONEPASS_MULTI ){
19910 sqlite3VdbeResolveLabel(v, labelContinue);
19911 sqlite3WhereEnd(pWInfo);
19912 }else if( pPk ){
19913 sqlite3VdbeResolveLabel(v, labelContinue);
19914 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
19915 }else{
19916 sqlite3VdbeGoto(v, labelContinue);
19917 }
19918 sqlite3VdbeResolveLabel(v, labelBreak);
19919
19920 /* Update the sqlite_sequence table by storing the content of the
19921 ** maximum rowid counter values recorded while inserting into
19922 ** autoincrement tables.
19923 */
19924 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
19925 sqlite3AutoincrementEnd(pParse);
19926 }
19927
19928 /*
19929 ** Return the number of rows that were changed. If this routine is
19930 ** generating code because of a call to sqlite3NestedParse(), do not
19931 ** invoke the callback function.
19932 */
19933 if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
19934 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
19935 sqlite3VdbeSetNumCols(v, 1);
19936 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
19937 }
19938
19939 update_cleanup:
19940 sqlite3AuthContextPop(&sContext);
19941 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
19942 sqlite3SrcListDelete(db, pTabList);
19943 sqlite3ExprListDelete(db, pChanges);
19944 sqlite3ExprDelete(db, pWhere);
19945 return;
19946 }
19947 /* Make sure "isView" and other macros defined above are undefined. Otherwise
19948 ** they may interfere with compilation of other functions in this file
19949 ** (or in another file, if this file becomes part of the amalgamation). */
19950 #ifdef isView
19951 #undef isView
19952 #endif
19953 #ifdef pTrigger
19954 #undef pTrigger
19955 #endif
19956
19957 #ifndef SQLITE_OMIT_VIRTUALTABLE
19958 /*
19959 ** Generate code for an UPDATE of a virtual table.
19960 **
19961 ** There are two possible strategies - the default and the special
19962 ** "onepass" strategy. Onepass is only used if the virtual table
19963 ** implementation indicates that pWhere may match at most one row.
19964 **
19965 ** The default strategy is to create an ephemeral table that contains
19966 ** for each row to be changed:
19967 **
19968 ** (A) The original rowid of that row.
19969 ** (B) The revised rowid for the row.
19970 ** (C) The content of every column in the row.
19971 **
19972 ** Then loop through the contents of this ephemeral table executing a
19973 ** VUpdate for each row. When finished, drop the ephemeral table.
19974 **
19975 ** The "onepass" strategy does not use an ephemeral table. Instead, it
19976 ** stores the same values (A, B and C above) in a register array and
19977 ** makes a single invocation of VUpdate.
19978 */
19979 static void updateVirtualTable(
19980 Parse *pParse, /* The parsing context */
19981 SrcList *pSrc, /* The virtual table to be modified */
19982 Table *pTab, /* The virtual table */
19983 ExprList *pChanges, /* The columns to change in the UPDATE statement */
19984 Expr *pRowid, /* Expression used to recompute the rowid */
19985 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
19986 Expr *pWhere, /* WHERE clause of the UPDATE statement */
19987 int onError /* ON CONFLICT strategy */
19988 ){
19989 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
19990 int ephemTab; /* Table holding the result of the SELECT */
19991 int i; /* Loop counter */
19992 sqlite3 *db = pParse->db; /* Database connection */
19993 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
19994 WhereInfo *pWInfo;
19995 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
19996 int regArg; /* First register in VUpdate arg array */
19997 int regRec; /* Register in which to assemble record */
19998 int regRowid; /* Register for ephem table rowid */
19999 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
20000 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
20001 int bOnePass; /* True to use onepass strategy */
20002 int addr; /* Address of OP_OpenEphemeral */
20003
20004 /* Allocate nArg registers to martial the arguments to VUpdate. Then
20005 ** create and open the ephemeral table in which the records created from
20006 ** these arguments will be temporarily stored. */
20007 assert( v );
20008 ephemTab = pParse->nTab++;
20009 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg);
20010 regArg = pParse->nMem + 1;
20011 pParse->nMem += nArg;
20012 regRec = ++pParse->nMem;
20013 regRowid = ++pParse->nMem;
20014
20015 /* Start scanning the virtual table */
20016 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0);
20017 if( pWInfo==0 ) return;
20018
20019 /* Populate the argument registers. */
20020 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
20021 if( pRowid ){
20022 sqlite3ExprCode(pParse, pRowid, regArg+1);
20023 }else{
20024 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1);
20025 }
20026 for(i=0; i<pTab->nCol; i++){
20027 if( aXRef[i]>=0 ){
20028 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
20029 }else{
20030 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
20031 }
20032 }
20033
20034 bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
20035
20036 if( bOnePass ){
20037 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
20038 ** above. Also, if this is a top-level parse (not a trigger), clear the
20039 ** multi-write flag so that the VM does not open a statement journal */
20040 sqlite3VdbeChangeToNoop(v, addr);
20041 if( sqlite3IsToplevel(pParse) ){
20042 pParse->isMultiWrite = 0;
20043 }
20044 }else{
20045 /* Create a record from the argument register contents and insert it into
20046 ** the ephemeral table. */
20047 sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
20048 sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
20049 sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
20050 }
20051
20052
20053 if( bOnePass==0 ){
20054 /* End the virtual table scan */
20055 sqlite3WhereEnd(pWInfo);
20056
20057 /* Begin scannning through the ephemeral table. */
20058 addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
20059
20060 /* Extract arguments from the current row of the ephemeral table and
20061 ** invoke the VUpdate method. */
20062 for(i=0; i<nArg; i++){
20063 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i);
20064 }
20065 }
20066 sqlite3VtabMakeWritable(pParse, pTab);
20067 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
20068 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
20069 sqlite3MayAbort(pParse);
20070
20071 /* End of the ephemeral table scan. Or, if using the onepass strategy,
20072 ** jump to here if the scan visited zero rows. */
20073 if( bOnePass==0 ){
20074 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
20075 sqlite3VdbeJumpHere(v, addr);
20076 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
20077 }else{
20078 sqlite3WhereEnd(pWInfo);
20079 }
20080 }
20081 #endif /* SQLITE_OMIT_VIRTUALTABLE */
20082
20083 /************** End of update.c **********************************************/
20084 /************** Begin file vacuum.c ******************************************/
20085 /*
20086 ** 2003 April 6
20087 **
20088 ** The author disclaims copyright to this source code. In place of
20089 ** a legal notice, here is a blessing:
20090 **
20091 ** May you do good and not evil.
20092 ** May you find forgiveness for yourself and forgive others.
20093 ** May you share freely, never taking more than you give.
20094 **
20095 *************************************************************************
20096 ** This file contains code used to implement the VACUUM command.
20097 **
20098 ** Most of the code in this file may be omitted by defining the
20099 ** SQLITE_OMIT_VACUUM macro.
20100 */
20101 /* #include "sqliteInt.h" */
20102 /* #include "vdbeInt.h" */
20103
20104 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
20105
20106 /*
20107 ** Execute zSql on database db.
20108 **
20109 ** If zSql returns rows, then each row will have exactly one
20110 ** column. (This will only happen if zSql begins with "SELECT".)
20111 ** Take each row of result and call execSql() again recursively.
20112 **
20113 ** The execSqlF() routine does the same thing, except it accepts
20114 ** a format string as its third argument
20115 */
20116 static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
20117 sqlite3_stmt *pStmt;
20118 int rc;
20119
20120 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
20121 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
20122 if( rc!=SQLITE_OK ) return rc;
20123 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
20124 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
20125 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
20126 if( zSubSql ){
20127 assert( zSubSql[0]!='S' );
20128 rc = execSql(db, pzErrMsg, zSubSql);
20129 if( rc!=SQLITE_OK ) break;
20130 }
20131 }
20132 assert( rc!=SQLITE_ROW );
20133 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
20134 if( rc ){
20135 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
20136 }
20137 (void)sqlite3_finalize(pStmt);
20138 return rc;
20139 }
20140 static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
20141 char *z;
20142 va_list ap;
20143 int rc;
20144 va_start(ap, zSql);
20145 z = sqlite3VMPrintf(db, zSql, ap);
20146 va_end(ap);
20147 if( z==0 ) return SQLITE_NOMEM;
20148 rc = execSql(db, pzErrMsg, z);
20149 sqlite3DbFree(db, z);
20150 return rc;
20151 }
20152
20153 /*
20154 ** The VACUUM command is used to clean up the database,
20155 ** collapse free space, etc. It is modelled after the VACUUM command
20156 ** in PostgreSQL. The VACUUM command works as follows:
20157 **
20158 ** (1) Create a new transient database file
20159 ** (2) Copy all content from the database being vacuumed into
20160 ** the new transient database file
20161 ** (3) Copy content from the transient database back into the
20162 ** original database.
20163 **
20164 ** The transient database requires temporary disk space approximately
20165 ** equal to the size of the original database. The copy operation of
20166 ** step (3) requires additional temporary disk space approximately equal
20167 ** to the size of the original database for the rollback journal.
20168 ** Hence, temporary disk space that is approximately 2x the size of the
20169 ** original database is required. Every page of the database is written
20170 ** approximately 3 times: Once for step (2) and twice for step (3).
20171 ** Two writes per page are required in step (3) because the original
20172 ** database content must be written into the rollback journal prior to
20173 ** overwriting the database with the vacuumed content.
20174 **
20175 ** Only 1x temporary space and only 1x writes would be required if
20176 ** the copy of step (3) were replaced by deleting the original database
20177 ** and renaming the transient database as the original. But that will
20178 ** not work if other processes are attached to the original database.
20179 ** And a power loss in between deleting the original and renaming the
20180 ** transient would cause the database file to appear to be deleted
20181 ** following reboot.
20182 */
20183 SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm){
20184 Vdbe *v = sqlite3GetVdbe(pParse);
20185 int iDb = pNm ? sqlite3TwoPartName(pParse, pNm, pNm, &pNm) : 0;
20186 if( v && (iDb>=2 || iDb==0) ){
20187 sqlite3VdbeAddOp1(v, OP_Vacuum, iDb);
20188 sqlite3VdbeUsesBtree(v, iDb);
20189 }
20190 return;
20191 }
20192
20193 /*
20194 ** This routine implements the OP_Vacuum opcode of the VDBE.
20195 */
20196 SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db, int iDb){
20197 int rc = SQLITE_OK; /* Return code from service routines */
20198 Btree *pMain; /* The database being vacuumed */
20199 Btree *pTemp; /* The temporary database we vacuum into */
20200 int saved_flags; /* Saved value of the db->flags */
20201 int saved_nChange; /* Saved value of db->nChange */
20202 int saved_nTotalChange; /* Saved value of db->nTotalChange */
20203 u8 saved_mTrace; /* Saved trace settings */
20204 Db *pDb = 0; /* Database to detach at end of vacuum */
20205 int isMemDb; /* True if vacuuming a :memory: database */
20206 int nRes; /* Bytes of reserved space at the end of each page */
20207 int nDb; /* Number of attached databases */
20208 const char *zDbMain; /* Schema name of database to vacuum */
20209
20210 if( !db->autoCommit ){
20211 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
20212 return SQLITE_ERROR;
20213 }
20214 if( db->nVdbeActive>1 ){
20215 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
20216 return SQLITE_ERROR;
20217 }
20218
20219 /* Save the current value of the database flags so that it can be
20220 ** restored before returning. Then set the writable-schema flag, and
20221 ** disable CHECK and foreign key constraints. */
20222 saved_flags = db->flags;
20223 saved_nChange = db->nChange;
20224 saved_nTotalChange = db->nTotalChange;
20225 saved_mTrace = db->mTrace;
20226 db->flags |= (SQLITE_WriteSchema | SQLITE_IgnoreChecks
20227 | SQLITE_PreferBuiltin | SQLITE_Vacuum);
20228 db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
20229 db->mTrace = 0;
20230
20231 zDbMain = db->aDb[iDb].zDbSName;
20232 pMain = db->aDb[iDb].pBt;
20233 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
20234
20235 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
20236 ** can be set to 'off' for this file, as it is not recovered if a crash
20237 ** occurs anyway. The integrity of the database is maintained by a
20238 ** (possibly synchronous) transaction opened on the main database before
20239 ** sqlite3BtreeCopyFile() is called.
20240 **
20241 ** An optimisation would be to use a non-journaled pager.
20242 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
20243 ** that actually made the VACUUM run slower. Very little journalling
20244 ** actually occurs when doing a vacuum since the vacuum_db is initially
20245 ** empty. Only the journal header is written. Apparently it takes more
20246 ** time to parse and run the PRAGMA to turn journalling off than it does
20247 ** to write the journal header file.
20248 */
20249 nDb = db->nDb;
20250 rc = execSql(db, pzErrMsg, "ATTACH''AS vacuum_db");
20251 if( rc!=SQLITE_OK ) goto end_of_vacuum;
20252 assert( (db->nDb-1)==nDb );
20253 pDb = &db->aDb[nDb];
20254 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
20255 pTemp = pDb->pBt;
20256
20257 /* The call to execSql() to attach the temp database has left the file
20258 ** locked (as there was more than one active statement when the transaction
20259 ** to read the schema was concluded. Unlock it here so that this doesn't
20260 ** cause problems for the call to BtreeSetPageSize() below. */
20261 sqlite3BtreeCommit(pTemp);
20262
20263 nRes = sqlite3BtreeGetOptimalReserve(pMain);
20264
20265 /* A VACUUM cannot change the pagesize of an encrypted database. */
20266 #ifdef SQLITE_HAS_CODEC
20267 if( db->nextPagesize ){
20268 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
20269 int nKey;
20270 char *zKey;
20271 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
20272 if( nKey ) db->nextPagesize = 0;
20273 }
20274 #endif
20275
20276 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
20277 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
20278 sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
20279
20280 /* Begin a transaction and take an exclusive lock on the main database
20281 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
20282 ** to ensure that we do not try to change the page-size on a WAL database.
20283 */
20284 rc = execSql(db, pzErrMsg, "BEGIN");
20285 if( rc!=SQLITE_OK ) goto end_of_vacuum;
20286 rc = sqlite3BtreeBeginTrans(pMain, 2);
20287 if( rc!=SQLITE_OK ) goto end_of_vacuum;
20288
20289 /* Do not attempt to change the page size for a WAL database */
20290 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
20291 ==PAGER_JOURNALMODE_WAL ){
20292 db->nextPagesize = 0;
20293 }
20294
20295 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
20296 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
20297 || NEVER(db->mallocFailed)
20298 ){
20299 rc = SQLITE_NOMEM_BKPT;
20300 goto end_of_vacuum;
20301 }
20302
20303 #ifndef SQLITE_OMIT_AUTOVACUUM
20304 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
20305 sqlite3BtreeGetAutoVacuum(pMain));
20306 #endif
20307
20308 /* Query the schema of the main database. Create a mirror schema
20309 ** in the temporary database.
20310 */
20311 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
20312 rc = execSqlF(db, pzErrMsg,
20313 "SELECT sql FROM \"%w\".sqlite_master"
20314 " WHERE type='table'AND name<>'sqlite_sequence'"
20315 " AND coalesce(rootpage,1)>0",
20316 zDbMain
20317 );
20318 if( rc!=SQLITE_OK ) goto end_of_vacuum;
20319 rc = execSqlF(db, pzErrMsg,
20320 "SELECT sql FROM \"%w\".sqlite_master"
20321 " WHERE type='index' AND length(sql)>10",
20322 zDbMain
20323 );
20324 if( rc!=SQLITE_OK ) goto end_of_vacuum;
20325 db->init.iDb = 0;
20326
20327 /* Loop through the tables in the main database. For each, do
20328 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
20329 ** the contents to the temporary database.
20330 */
20331 rc = execSqlF(db, pzErrMsg,
20332 "SELECT'INSERT INTO vacuum_db.'||quote(name)"
20333 "||' SELECT*FROM\"%w\".'||quote(name)"
20334 "FROM vacuum_db.sqlite_master "
20335 "WHERE type='table'AND coalesce(rootpage,1)>0",
20336 zDbMain
20337 );
20338 assert( (db->flags & SQLITE_Vacuum)!=0 );
20339 db->flags &= ~SQLITE_Vacuum;
20340 if( rc!=SQLITE_OK ) goto end_of_vacuum;
20341
20342 /* Copy the triggers, views, and virtual tables from the main database
20343 ** over to the temporary database. None of these objects has any
20344 ** associated storage, so all we have to do is copy their entries
20345 ** from the SQLITE_MASTER table.
20346 */
20347 rc = execSqlF(db, pzErrMsg,
20348 "INSERT INTO vacuum_db.sqlite_master"
20349 " SELECT*FROM \"%w\".sqlite_master"
20350 " WHERE type IN('view','trigger')"
20351 " OR(type='table'AND rootpage=0)",
20352 zDbMain
20353 );
20354 if( rc ) goto end_of_vacuum;
20355
20356 /* At this point, there is a write transaction open on both the
20357 ** vacuum database and the main database. Assuming no error occurs,
20358 ** both transactions are closed by this block - the main database
20359 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
20360 ** call to sqlite3BtreeCommit().
20361 */
20362 {
20363 u32 meta;
20364 int i;
20365
20366 /* This array determines which meta meta values are preserved in the
20367 ** vacuum. Even entries are the meta value number and odd entries
20368 ** are an increment to apply to the meta value after the vacuum.
20369 ** The increment is used to increase the schema cookie so that other
20370 ** connections to the same database will know to reread the schema.
20371 */
20372 static const unsigned char aCopy[] = {
20373 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
20374 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
20375 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
20376 BTREE_USER_VERSION, 0, /* Preserve the user version */
20377 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
20378 };
20379
20380 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
20381 assert( 1==sqlite3BtreeIsInTrans(pMain) );
20382
20383 /* Copy Btree meta values */
20384 for(i=0; i<ArraySize(aCopy); i+=2){
20385 /* GetMeta() and UpdateMeta() cannot fail in this context because
20386 ** we already have page 1 loaded into cache and marked dirty. */
20387 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
20388 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
20389 if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
20390 }
20391
20392 rc = sqlite3BtreeCopyFile(pMain, pTemp);
20393 if( rc!=SQLITE_OK ) goto end_of_vacuum;
20394 rc = sqlite3BtreeCommit(pTemp);
20395 if( rc!=SQLITE_OK ) goto end_of_vacuum;
20396 #ifndef SQLITE_OMIT_AUTOVACUUM
20397 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
20398 #endif
20399 }
20400
20401 assert( rc==SQLITE_OK );
20402 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
20403
20404 end_of_vacuum:
20405 /* Restore the original value of db->flags */
20406 db->init.iDb = 0;
20407 db->flags = saved_flags;
20408 db->nChange = saved_nChange;
20409 db->nTotalChange = saved_nTotalChange;
20410 db->mTrace = saved_mTrace;
20411 sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
20412
20413 /* Currently there is an SQL level transaction open on the vacuum
20414 ** database. No locks are held on any other files (since the main file
20415 ** was committed at the btree level). So it safe to end the transaction
20416 ** by manually setting the autoCommit flag to true and detaching the
20417 ** vacuum database. The vacuum_db journal file is deleted when the pager
20418 ** is closed by the DETACH.
20419 */
20420 db->autoCommit = 1;
20421
20422 if( pDb ){
20423 sqlite3BtreeClose(pDb->pBt);
20424 pDb->pBt = 0;
20425 pDb->pSchema = 0;
20426 }
20427
20428 /* This both clears the schemas and reduces the size of the db->aDb[]
20429 ** array. */
20430 sqlite3ResetAllSchemasOfConnection(db);
20431
20432 return rc;
20433 }
20434
20435 #endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
20436
20437 /************** End of vacuum.c **********************************************/
20438 /************** Begin file vtab.c ********************************************/
20439 /*
20440 ** 2006 June 10
20441 **
20442 ** The author disclaims copyright to this source code. In place of
20443 ** a legal notice, here is a blessing:
20444 **
20445 ** May you do good and not evil.
20446 ** May you find forgiveness for yourself and forgive others.
20447 ** May you share freely, never taking more than you give.
20448 **
20449 *************************************************************************
20450 ** This file contains code used to help implement virtual tables.
20451 */
20452 #ifndef SQLITE_OMIT_VIRTUALTABLE
20453 /* #include "sqliteInt.h" */
20454
20455 /*
20456 ** Before a virtual table xCreate() or xConnect() method is invoked, the
20457 ** sqlite3.pVtabCtx member variable is set to point to an instance of
20458 ** this struct allocated on the stack. It is used by the implementation of
20459 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
20460 ** are invoked only from within xCreate and xConnect methods.
20461 */
20462 struct VtabCtx {
20463 VTable *pVTable; /* The virtual table being constructed */
20464 Table *pTab; /* The Table object to which the virtual table belongs */
20465 VtabCtx *pPrior; /* Parent context (if any) */
20466 int bDeclared; /* True after sqlite3_declare_vtab() is called */
20467 };
20468
20469 /*
20470 ** Construct and install a Module object for a virtual table. When this
20471 ** routine is called, it is guaranteed that all appropriate locks are held
20472 ** and the module is not already part of the connection.
20473 */
20474 SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
20475 sqlite3 *db, /* Database in which module is registered */
20476 const char *zName, /* Name assigned to this module */
20477 const sqlite3_module *pModule, /* The definition of the module */
20478 void *pAux, /* Context pointer for xCreate/xConnect */
20479 void (*xDestroy)(void *) /* Module destructor function */
20480 ){
20481 Module *pMod;
20482 int nName = sqlite3Strlen30(zName);
20483 pMod = (Module *)sqlite3DbMallocRawNN(db, sizeof(Module) + nName + 1);
20484 if( pMod ){
20485 Module *pDel;
20486 char *zCopy = (char *)(&pMod[1]);
20487 memcpy(zCopy, zName, nName+1);
20488 pMod->zName = zCopy;
20489 pMod->pModule = pModule;
20490 pMod->pAux = pAux;
20491 pMod->xDestroy = xDestroy;
20492 pMod->pEpoTab = 0;
20493 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
20494 assert( pDel==0 || pDel==pMod );
20495 if( pDel ){
20496 sqlite3OomFault(db);
20497 sqlite3DbFree(db, pDel);
20498 pMod = 0;
20499 }
20500 }
20501 return pMod;
20502 }
20503
20504 /*
20505 ** The actual function that does the work of creating a new module.
20506 ** This function implements the sqlite3_create_module() and
20507 ** sqlite3_create_module_v2() interfaces.
20508 */
20509 static int createModule(
20510 sqlite3 *db, /* Database in which module is registered */
20511 const char *zName, /* Name assigned to this module */
20512 const sqlite3_module *pModule, /* The definition of the module */
20513 void *pAux, /* Context pointer for xCreate/xConnect */
20514 void (*xDestroy)(void *) /* Module destructor function */
20515 ){
20516 int rc = SQLITE_OK;
20517
20518 sqlite3_mutex_enter(db->mutex);
20519 if( sqlite3HashFind(&db->aModule, zName) ){
20520 rc = SQLITE_MISUSE_BKPT;
20521 }else{
20522 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
20523 }
20524 rc = sqlite3ApiExit(db, rc);
20525 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
20526 sqlite3_mutex_leave(db->mutex);
20527 return rc;
20528 }
20529
20530
20531 /*
20532 ** External API function used to create a new virtual-table module.
20533 */
20534 SQLITE_API int sqlite3_create_module(
20535 sqlite3 *db, /* Database in which module is registered */
20536 const char *zName, /* Name assigned to this module */
20537 const sqlite3_module *pModule, /* The definition of the module */
20538 void *pAux /* Context pointer for xCreate/xConnect */
20539 ){
20540 #ifdef SQLITE_ENABLE_API_ARMOR
20541 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
20542 #endif
20543 return createModule(db, zName, pModule, pAux, 0);
20544 }
20545
20546 /*
20547 ** External API function used to create a new virtual-table module.
20548 */
20549 SQLITE_API int sqlite3_create_module_v2(
20550 sqlite3 *db, /* Database in which module is registered */
20551 const char *zName, /* Name assigned to this module */
20552 const sqlite3_module *pModule, /* The definition of the module */
20553 void *pAux, /* Context pointer for xCreate/xConnect */
20554 void (*xDestroy)(void *) /* Module destructor function */
20555 ){
20556 #ifdef SQLITE_ENABLE_API_ARMOR
20557 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
20558 #endif
20559 return createModule(db, zName, pModule, pAux, xDestroy);
20560 }
20561
20562 /*
20563 ** Lock the virtual table so that it cannot be disconnected.
20564 ** Locks nest. Every lock should have a corresponding unlock.
20565 ** If an unlock is omitted, resources leaks will occur.
20566 **
20567 ** If a disconnect is attempted while a virtual table is locked,
20568 ** the disconnect is deferred until all locks have been removed.
20569 */
20570 SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
20571 pVTab->nRef++;
20572 }
20573
20574
20575 /*
20576 ** pTab is a pointer to a Table structure representing a virtual-table.
20577 ** Return a pointer to the VTable object used by connection db to access
20578 ** this virtual-table, if one has been created, or NULL otherwise.
20579 */
20580 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
20581 VTable *pVtab;
20582 assert( IsVirtual(pTab) );
20583 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
20584 return pVtab;
20585 }
20586
20587 /*
20588 ** Decrement the ref-count on a virtual table object. When the ref-count
20589 ** reaches zero, call the xDisconnect() method to delete the object.
20590 */
20591 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
20592 sqlite3 *db = pVTab->db;
20593
20594 assert( db );
20595 assert( pVTab->nRef>0 );
20596 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
20597
20598 pVTab->nRef--;
20599 if( pVTab->nRef==0 ){
20600 sqlite3_vtab *p = pVTab->pVtab;
20601 if( p ){
20602 p->pModule->xDisconnect(p);
20603 }
20604 sqlite3DbFree(db, pVTab);
20605 }
20606 }
20607
20608 /*
20609 ** Table p is a virtual table. This function moves all elements in the
20610 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
20611 ** database connections to be disconnected at the next opportunity.
20612 ** Except, if argument db is not NULL, then the entry associated with
20613 ** connection db is left in the p->pVTable list.
20614 */
20615 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
20616 VTable *pRet = 0;
20617 VTable *pVTable = p->pVTable;
20618 p->pVTable = 0;
20619
20620 /* Assert that the mutex (if any) associated with the BtShared database
20621 ** that contains table p is held by the caller. See header comments
20622 ** above function sqlite3VtabUnlockList() for an explanation of why
20623 ** this makes it safe to access the sqlite3.pDisconnect list of any
20624 ** database connection that may have an entry in the p->pVTable list.
20625 */
20626 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
20627
20628 while( pVTable ){
20629 sqlite3 *db2 = pVTable->db;
20630 VTable *pNext = pVTable->pNext;
20631 assert( db2 );
20632 if( db2==db ){
20633 pRet = pVTable;
20634 p->pVTable = pRet;
20635 pRet->pNext = 0;
20636 }else{
20637 pVTable->pNext = db2->pDisconnect;
20638 db2->pDisconnect = pVTable;
20639 }
20640 pVTable = pNext;
20641 }
20642
20643 assert( !db || pRet );
20644 return pRet;
20645 }
20646
20647 /*
20648 ** Table *p is a virtual table. This function removes the VTable object
20649 ** for table *p associated with database connection db from the linked
20650 ** list in p->pVTab. It also decrements the VTable ref count. This is
20651 ** used when closing database connection db to free all of its VTable
20652 ** objects without disturbing the rest of the Schema object (which may
20653 ** be being used by other shared-cache connections).
20654 */
20655 SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
20656 VTable **ppVTab;
20657
20658 assert( IsVirtual(p) );
20659 assert( sqlite3BtreeHoldsAllMutexes(db) );
20660 assert( sqlite3_mutex_held(db->mutex) );
20661
20662 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
20663 if( (*ppVTab)->db==db ){
20664 VTable *pVTab = *ppVTab;
20665 *ppVTab = pVTab->pNext;
20666 sqlite3VtabUnlock(pVTab);
20667 break;
20668 }
20669 }
20670 }
20671
20672
20673 /*
20674 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
20675 **
20676 ** This function may only be called when the mutexes associated with all
20677 ** shared b-tree databases opened using connection db are held by the
20678 ** caller. This is done to protect the sqlite3.pDisconnect list. The
20679 ** sqlite3.pDisconnect list is accessed only as follows:
20680 **
20681 ** 1) By this function. In this case, all BtShared mutexes and the mutex
20682 ** associated with the database handle itself must be held.
20683 **
20684 ** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
20685 ** the sqlite3.pDisconnect list. In this case either the BtShared mutex
20686 ** associated with the database the virtual table is stored in is held
20687 ** or, if the virtual table is stored in a non-sharable database, then
20688 ** the database handle mutex is held.
20689 **
20690 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
20691 ** by multiple threads. It is thread-safe.
20692 */
20693 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
20694 VTable *p = db->pDisconnect;
20695 db->pDisconnect = 0;
20696
20697 assert( sqlite3BtreeHoldsAllMutexes(db) );
20698 assert( sqlite3_mutex_held(db->mutex) );
20699
20700 if( p ){
20701 sqlite3ExpirePreparedStatements(db);
20702 do {
20703 VTable *pNext = p->pNext;
20704 sqlite3VtabUnlock(p);
20705 p = pNext;
20706 }while( p );
20707 }
20708 }
20709
20710 /*
20711 ** Clear any and all virtual-table information from the Table record.
20712 ** This routine is called, for example, just before deleting the Table
20713 ** record.
20714 **
20715 ** Since it is a virtual-table, the Table structure contains a pointer
20716 ** to the head of a linked list of VTable structures. Each VTable
20717 ** structure is associated with a single sqlite3* user of the schema.
20718 ** The reference count of the VTable structure associated with database
20719 ** connection db is decremented immediately (which may lead to the
20720 ** structure being xDisconnected and free). Any other VTable structures
20721 ** in the list are moved to the sqlite3.pDisconnect list of the associated
20722 ** database connection.
20723 */
20724 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
20725 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
20726 if( p->azModuleArg ){
20727 int i;
20728 for(i=0; i<p->nModuleArg; i++){
20729 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
20730 }
20731 sqlite3DbFree(db, p->azModuleArg);
20732 }
20733 }
20734
20735 /*
20736 ** Add a new module argument to pTable->azModuleArg[].
20737 ** The string is not copied - the pointer is stored. The
20738 ** string will be freed automatically when the table is
20739 ** deleted.
20740 */
20741 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
20742 int nBytes = sizeof(char *)*(2+pTable->nModuleArg);
20743 char **azModuleArg;
20744 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
20745 if( azModuleArg==0 ){
20746 sqlite3DbFree(db, zArg);
20747 }else{
20748 int i = pTable->nModuleArg++;
20749 azModuleArg[i] = zArg;
20750 azModuleArg[i+1] = 0;
20751 pTable->azModuleArg = azModuleArg;
20752 }
20753 }
20754
20755 /*
20756 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
20757 ** statement. The module name has been parsed, but the optional list
20758 ** of parameters that follow the module name are still pending.
20759 */
20760 SQLITE_PRIVATE void sqlite3VtabBeginParse(
20761 Parse *pParse, /* Parsing context */
20762 Token *pName1, /* Name of new table, or database name */
20763 Token *pName2, /* Name of new table or NULL */
20764 Token *pModuleName, /* Name of the module for the virtual table */
20765 int ifNotExists /* No error if the table already exists */
20766 ){
20767 int iDb; /* The database the table is being created in */
20768 Table *pTable; /* The new virtual table */
20769 sqlite3 *db; /* Database connection */
20770
20771 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
20772 pTable = pParse->pNewTable;
20773 if( pTable==0 ) return;
20774 assert( 0==pTable->pIndex );
20775
20776 db = pParse->db;
20777 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
20778 assert( iDb>=0 );
20779
20780 pTable->tabFlags |= TF_Virtual;
20781 pTable->nModuleArg = 0;
20782 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
20783 addModuleArgument(db, pTable, 0);
20784 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
20785 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
20786 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
20787 );
20788 pParse->sNameToken.n = (int)(
20789 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
20790 );
20791
20792 #ifndef SQLITE_OMIT_AUTHORIZATION
20793 /* Creating a virtual table invokes the authorization callback twice.
20794 ** The first invocation, to obtain permission to INSERT a row into the
20795 ** sqlite_master table, has already been made by sqlite3StartTable().
20796 ** The second call, to obtain permission to create the table, is made now.
20797 */
20798 if( pTable->azModuleArg ){
20799 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
20800 pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
20801 }
20802 #endif
20803 }
20804
20805 /*
20806 ** This routine takes the module argument that has been accumulating
20807 ** in pParse->zArg[] and appends it to the list of arguments on the
20808 ** virtual table currently under construction in pParse->pTable.
20809 */
20810 static void addArgumentToVtab(Parse *pParse){
20811 if( pParse->sArg.z && pParse->pNewTable ){
20812 const char *z = (const char*)pParse->sArg.z;
20813 int n = pParse->sArg.n;
20814 sqlite3 *db = pParse->db;
20815 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
20816 }
20817 }
20818
20819 /*
20820 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
20821 ** has been completely parsed.
20822 */
20823 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
20824 Table *pTab = pParse->pNewTable; /* The table being constructed */
20825 sqlite3 *db = pParse->db; /* The database connection */
20826
20827 if( pTab==0 ) return;
20828 addArgumentToVtab(pParse);
20829 pParse->sArg.z = 0;
20830 if( pTab->nModuleArg<1 ) return;
20831
20832 /* If the CREATE VIRTUAL TABLE statement is being entered for the
20833 ** first time (in other words if the virtual table is actually being
20834 ** created now instead of just being read out of sqlite_master) then
20835 ** do additional initialization work and store the statement text
20836 ** in the sqlite_master table.
20837 */
20838 if( !db->init.busy ){
20839 char *zStmt;
20840 char *zWhere;
20841 int iDb;
20842 int iReg;
20843 Vdbe *v;
20844
20845 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
20846 if( pEnd ){
20847 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
20848 }
20849 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
20850
20851 /* A slot for the record has already been allocated in the
20852 ** SQLITE_MASTER table. We just need to update that slot with all
20853 ** the information we've collected.
20854 **
20855 ** The VM register number pParse->regRowid holds the rowid of an
20856 ** entry in the sqlite_master table tht was created for this vtab
20857 ** by sqlite3StartTable().
20858 */
20859 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
20860 sqlite3NestedParse(pParse,
20861 "UPDATE %Q.%s "
20862 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
20863 "WHERE rowid=#%d",
20864 db->aDb[iDb].zDbSName, MASTER_NAME,
20865 pTab->zName,
20866 pTab->zName,
20867 zStmt,
20868 pParse->regRowid
20869 );
20870 sqlite3DbFree(db, zStmt);
20871 v = sqlite3GetVdbe(pParse);
20872 sqlite3ChangeCookie(pParse, iDb);
20873
20874 sqlite3VdbeAddOp0(v, OP_Expire);
20875 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
20876 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
20877
20878 iReg = ++pParse->nMem;
20879 sqlite3VdbeLoadString(v, iReg, pTab->zName);
20880 sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
20881 }
20882
20883 /* If we are rereading the sqlite_master table create the in-memory
20884 ** record of the table. The xConnect() method is not called until
20885 ** the first time the virtual table is used in an SQL statement. This
20886 ** allows a schema that contains virtual tables to be loaded before
20887 ** the required virtual table implementations are registered. */
20888 else {
20889 Table *pOld;
20890 Schema *pSchema = pTab->pSchema;
20891 const char *zName = pTab->zName;
20892 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
20893 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
20894 if( pOld ){
20895 sqlite3OomFault(db);
20896 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
20897 return;
20898 }
20899 pParse->pNewTable = 0;
20900 }
20901 }
20902
20903 /*
20904 ** The parser calls this routine when it sees the first token
20905 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
20906 */
20907 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
20908 addArgumentToVtab(pParse);
20909 pParse->sArg.z = 0;
20910 pParse->sArg.n = 0;
20911 }
20912
20913 /*
20914 ** The parser calls this routine for each token after the first token
20915 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
20916 */
20917 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
20918 Token *pArg = &pParse->sArg;
20919 if( pArg->z==0 ){
20920 pArg->z = p->z;
20921 pArg->n = p->n;
20922 }else{
20923 assert(pArg->z <= p->z);
20924 pArg->n = (int)(&p->z[p->n] - pArg->z);
20925 }
20926 }
20927
20928 /*
20929 ** Invoke a virtual table constructor (either xCreate or xConnect). The
20930 ** pointer to the function to invoke is passed as the fourth parameter
20931 ** to this procedure.
20932 */
20933 static int vtabCallConstructor(
20934 sqlite3 *db,
20935 Table *pTab,
20936 Module *pMod,
20937 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
20938 char **pzErr
20939 ){
20940 VtabCtx sCtx;
20941 VTable *pVTable;
20942 int rc;
20943 const char *const*azArg = (const char *const*)pTab->azModuleArg;
20944 int nArg = pTab->nModuleArg;
20945 char *zErr = 0;
20946 char *zModuleName;
20947 int iDb;
20948 VtabCtx *pCtx;
20949
20950 /* Check that the virtual-table is not already being initialized */
20951 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
20952 if( pCtx->pTab==pTab ){
20953 *pzErr = sqlite3MPrintf(db,
20954 "vtable constructor called recursively: %s", pTab->zName
20955 );
20956 return SQLITE_LOCKED;
20957 }
20958 }
20959
20960 zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
20961 if( !zModuleName ){
20962 return SQLITE_NOMEM_BKPT;
20963 }
20964
20965 pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
20966 if( !pVTable ){
20967 sqlite3DbFree(db, zModuleName);
20968 return SQLITE_NOMEM_BKPT;
20969 }
20970 pVTable->db = db;
20971 pVTable->pMod = pMod;
20972
20973 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
20974 pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
20975
20976 /* Invoke the virtual table constructor */
20977 assert( &db->pVtabCtx );
20978 assert( xConstruct );
20979 sCtx.pTab = pTab;
20980 sCtx.pVTable = pVTable;
20981 sCtx.pPrior = db->pVtabCtx;
20982 sCtx.bDeclared = 0;
20983 db->pVtabCtx = &sCtx;
20984 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
20985 db->pVtabCtx = sCtx.pPrior;
20986 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
20987 assert( sCtx.pTab==pTab );
20988
20989 if( SQLITE_OK!=rc ){
20990 if( zErr==0 ){
20991 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
20992 }else {
20993 *pzErr = sqlite3MPrintf(db, "%s", zErr);
20994 sqlite3_free(zErr);
20995 }
20996 sqlite3DbFree(db, pVTable);
20997 }else if( ALWAYS(pVTable->pVtab) ){
20998 /* Justification of ALWAYS(): A correct vtab constructor must allocate
20999 ** the sqlite3_vtab object if successful. */
21000 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
21001 pVTable->pVtab->pModule = pMod->pModule;
21002 pVTable->nRef = 1;
21003 if( sCtx.bDeclared==0 ){
21004 const char *zFormat = "vtable constructor did not declare schema: %s";
21005 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
21006 sqlite3VtabUnlock(pVTable);
21007 rc = SQLITE_ERROR;
21008 }else{
21009 int iCol;
21010 u8 oooHidden = 0;
21011 /* If everything went according to plan, link the new VTable structure
21012 ** into the linked list headed by pTab->pVTable. Then loop through the
21013 ** columns of the table to see if any of them contain the token "hidden".
21014 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
21015 ** the type string. */
21016 pVTable->pNext = pTab->pVTable;
21017 pTab->pVTable = pVTable;
21018
21019 for(iCol=0; iCol<pTab->nCol; iCol++){
21020 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
21021 int nType;
21022 int i = 0;
21023 nType = sqlite3Strlen30(zType);
21024 for(i=0; i<nType; i++){
21025 if( 0==sqlite3StrNICmp("hidden", &zType[i], 6)
21026 && (i==0 || zType[i-1]==' ')
21027 && (zType[i+6]=='\0' || zType[i+6]==' ')
21028 ){
21029 break;
21030 }
21031 }
21032 if( i<nType ){
21033 int j;
21034 int nDel = 6 + (zType[i+6] ? 1 : 0);
21035 for(j=i; (j+nDel)<=nType; j++){
21036 zType[j] = zType[j+nDel];
21037 }
21038 if( zType[i]=='\0' && i>0 ){
21039 assert(zType[i-1]==' ');
21040 zType[i-1] = '\0';
21041 }
21042 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
21043 oooHidden = TF_OOOHidden;
21044 }else{
21045 pTab->tabFlags |= oooHidden;
21046 }
21047 }
21048 }
21049 }
21050
21051 sqlite3DbFree(db, zModuleName);
21052 return rc;
21053 }
21054
21055 /*
21056 ** This function is invoked by the parser to call the xConnect() method
21057 ** of the virtual table pTab. If an error occurs, an error code is returned
21058 ** and an error left in pParse.
21059 **
21060 ** This call is a no-op if table pTab is not a virtual table.
21061 */
21062 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
21063 sqlite3 *db = pParse->db;
21064 const char *zMod;
21065 Module *pMod;
21066 int rc;
21067
21068 assert( pTab );
21069 if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
21070 return SQLITE_OK;
21071 }
21072
21073 /* Locate the required virtual table module */
21074 zMod = pTab->azModuleArg[0];
21075 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
21076
21077 if( !pMod ){
21078 const char *zModule = pTab->azModuleArg[0];
21079 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
21080 rc = SQLITE_ERROR;
21081 }else{
21082 char *zErr = 0;
21083 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
21084 if( rc!=SQLITE_OK ){
21085 sqlite3ErrorMsg(pParse, "%s", zErr);
21086 }
21087 sqlite3DbFree(db, zErr);
21088 }
21089
21090 return rc;
21091 }
21092 /*
21093 ** Grow the db->aVTrans[] array so that there is room for at least one
21094 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
21095 */
21096 static int growVTrans(sqlite3 *db){
21097 const int ARRAY_INCR = 5;
21098
21099 /* Grow the sqlite3.aVTrans array if required */
21100 if( (db->nVTrans%ARRAY_INCR)==0 ){
21101 VTable **aVTrans;
21102 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
21103 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
21104 if( !aVTrans ){
21105 return SQLITE_NOMEM_BKPT;
21106 }
21107 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
21108 db->aVTrans = aVTrans;
21109 }
21110
21111 return SQLITE_OK;
21112 }
21113
21114 /*
21115 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
21116 ** have already been reserved using growVTrans().
21117 */
21118 static void addToVTrans(sqlite3 *db, VTable *pVTab){
21119 /* Add pVtab to the end of sqlite3.aVTrans */
21120 db->aVTrans[db->nVTrans++] = pVTab;
21121 sqlite3VtabLock(pVTab);
21122 }
21123
21124 /*
21125 ** This function is invoked by the vdbe to call the xCreate method
21126 ** of the virtual table named zTab in database iDb.
21127 **
21128 ** If an error occurs, *pzErr is set to point to an English language
21129 ** description of the error and an SQLITE_XXX error code is returned.
21130 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
21131 */
21132 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
21133 int rc = SQLITE_OK;
21134 Table *pTab;
21135 Module *pMod;
21136 const char *zMod;
21137
21138 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
21139 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
21140
21141 /* Locate the required virtual table module */
21142 zMod = pTab->azModuleArg[0];
21143 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
21144
21145 /* If the module has been registered and includes a Create method,
21146 ** invoke it now. If the module has not been registered, return an
21147 ** error. Otherwise, do nothing.
21148 */
21149 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
21150 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
21151 rc = SQLITE_ERROR;
21152 }else{
21153 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
21154 }
21155
21156 /* Justification of ALWAYS(): The xConstructor method is required to
21157 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
21158 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
21159 rc = growVTrans(db);
21160 if( rc==SQLITE_OK ){
21161 addToVTrans(db, sqlite3GetVTable(db, pTab));
21162 }
21163 }
21164
21165 return rc;
21166 }
21167
21168 /*
21169 ** This function is used to set the schema of a virtual table. It is only
21170 ** valid to call this function from within the xCreate() or xConnect() of a
21171 ** virtual table module.
21172 */
21173 SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
21174 VtabCtx *pCtx;
21175 Parse *pParse;
21176 int rc = SQLITE_OK;
21177 Table *pTab;
21178 char *zErr = 0;
21179
21180 #ifdef SQLITE_ENABLE_API_ARMOR
21181 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
21182 return SQLITE_MISUSE_BKPT;
21183 }
21184 #endif
21185 sqlite3_mutex_enter(db->mutex);
21186 pCtx = db->pVtabCtx;
21187 if( !pCtx || pCtx->bDeclared ){
21188 sqlite3Error(db, SQLITE_MISUSE);
21189 sqlite3_mutex_leave(db->mutex);
21190 return SQLITE_MISUSE_BKPT;
21191 }
21192 pTab = pCtx->pTab;
21193 assert( (pTab->tabFlags & TF_Virtual)!=0 );
21194
21195 pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
21196 if( pParse==0 ){
21197 rc = SQLITE_NOMEM_BKPT;
21198 }else{
21199 pParse->declareVtab = 1;
21200 pParse->db = db;
21201 pParse->nQueryLoop = 1;
21202
21203 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
21204 && pParse->pNewTable
21205 && !db->mallocFailed
21206 && !pParse->pNewTable->pSelect
21207 && (pParse->pNewTable->tabFlags & TF_Virtual)==0
21208 ){
21209 if( !pTab->aCol ){
21210 Table *pNew = pParse->pNewTable;
21211 Index *pIdx;
21212 pTab->aCol = pNew->aCol;
21213 pTab->nCol = pNew->nCol;
21214 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
21215 pNew->nCol = 0;
21216 pNew->aCol = 0;
21217 assert( pTab->pIndex==0 );
21218 if( !HasRowid(pNew) && pCtx->pVTable->pMod->pModule->xUpdate!=0 ){
21219 rc = SQLITE_ERROR;
21220 }
21221 pIdx = pNew->pIndex;
21222 if( pIdx ){
21223 assert( pIdx->pNext==0 );
21224 pTab->pIndex = pIdx;
21225 pNew->pIndex = 0;
21226 pIdx->pTable = pTab;
21227 }
21228 }
21229 pCtx->bDeclared = 1;
21230 }else{
21231 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
21232 sqlite3DbFree(db, zErr);
21233 rc = SQLITE_ERROR;
21234 }
21235 pParse->declareVtab = 0;
21236
21237 if( pParse->pVdbe ){
21238 sqlite3VdbeFinalize(pParse->pVdbe);
21239 }
21240 sqlite3DeleteTable(db, pParse->pNewTable);
21241 sqlite3ParserReset(pParse);
21242 sqlite3StackFree(db, pParse);
21243 }
21244
21245 assert( (rc&0xff)==rc );
21246 rc = sqlite3ApiExit(db, rc);
21247 sqlite3_mutex_leave(db->mutex);
21248 return rc;
21249 }
21250
21251 /*
21252 ** This function is invoked by the vdbe to call the xDestroy method
21253 ** of the virtual table named zTab in database iDb. This occurs
21254 ** when a DROP TABLE is mentioned.
21255 **
21256 ** This call is a no-op if zTab is not a virtual table.
21257 */
21258 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab ){
21259 int rc = SQLITE_OK;
21260 Table *pTab;
21261
21262 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
21263 if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
21264 VTable *p;
21265 int (*xDestroy)(sqlite3_vtab *);
21266 for(p=pTab->pVTable; p; p=p->pNext){
21267 assert( p->pVtab );
21268 if( p->pVtab->nRef>0 ){
21269 return SQLITE_LOCKED;
21270 }
21271 }
21272 p = vtabDisconnectAll(db, pTab);
21273 xDestroy = p->pMod->pModule->xDestroy;
21274 assert( xDestroy!=0 ); /* Checked before the virtual table is created */
21275 rc = xDestroy(p->pVtab);
21276 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
21277 if( rc==SQLITE_OK ){
21278 assert( pTab->pVTable==p && p->pNext==0 );
21279 p->pVtab = 0;
21280 pTab->pVTable = 0;
21281 sqlite3VtabUnlock(p);
21282 }
21283 }
21284
21285 return rc;
21286 }
21287
21288 /*
21289 ** This function invokes either the xRollback or xCommit method
21290 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
21291 ** called is identified by the second argument, "offset", which is
21292 ** the offset of the method to call in the sqlite3_module structure.
21293 **
21294 ** The array is cleared after invoking the callbacks.
21295 */
21296 static void callFinaliser(sqlite3 *db, int offset){
21297 int i;
21298 if( db->aVTrans ){
21299 VTable **aVTrans = db->aVTrans;
21300 db->aVTrans = 0;
21301 for(i=0; i<db->nVTrans; i++){
21302 VTable *pVTab = aVTrans[i];
21303 sqlite3_vtab *p = pVTab->pVtab;
21304 if( p ){
21305 int (*x)(sqlite3_vtab *);
21306 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
21307 if( x ) x(p);
21308 }
21309 pVTab->iSavepoint = 0;
21310 sqlite3VtabUnlock(pVTab);
21311 }
21312 sqlite3DbFree(db, aVTrans);
21313 db->nVTrans = 0;
21314 }
21315 }
21316
21317 /*
21318 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
21319 ** array. Return the error code for the first error that occurs, or
21320 ** SQLITE_OK if all xSync operations are successful.
21321 **
21322 ** If an error message is available, leave it in p->zErrMsg.
21323 */
21324 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
21325 int i;
21326 int rc = SQLITE_OK;
21327 VTable **aVTrans = db->aVTrans;
21328
21329 db->aVTrans = 0;
21330 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
21331 int (*x)(sqlite3_vtab *);
21332 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
21333 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
21334 rc = x(pVtab);
21335 sqlite3VtabImportErrmsg(p, pVtab);
21336 }
21337 }
21338 db->aVTrans = aVTrans;
21339 return rc;
21340 }
21341
21342 /*
21343 ** Invoke the xRollback method of all virtual tables in the
21344 ** sqlite3.aVTrans array. Then clear the array itself.
21345 */
21346 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
21347 callFinaliser(db, offsetof(sqlite3_module,xRollback));
21348 return SQLITE_OK;
21349 }
21350
21351 /*
21352 ** Invoke the xCommit method of all virtual tables in the
21353 ** sqlite3.aVTrans array. Then clear the array itself.
21354 */
21355 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
21356 callFinaliser(db, offsetof(sqlite3_module,xCommit));
21357 return SQLITE_OK;
21358 }
21359
21360 /*
21361 ** If the virtual table pVtab supports the transaction interface
21362 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
21363 ** not currently open, invoke the xBegin method now.
21364 **
21365 ** If the xBegin call is successful, place the sqlite3_vtab pointer
21366 ** in the sqlite3.aVTrans array.
21367 */
21368 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
21369 int rc = SQLITE_OK;
21370 const sqlite3_module *pModule;
21371
21372 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
21373 ** than zero, then this function is being called from within a
21374 ** virtual module xSync() callback. It is illegal to write to
21375 ** virtual module tables in this case, so return SQLITE_LOCKED.
21376 */
21377 if( sqlite3VtabInSync(db) ){
21378 return SQLITE_LOCKED;
21379 }
21380 if( !pVTab ){
21381 return SQLITE_OK;
21382 }
21383 pModule = pVTab->pVtab->pModule;
21384
21385 if( pModule->xBegin ){
21386 int i;
21387
21388 /* If pVtab is already in the aVTrans array, return early */
21389 for(i=0; i<db->nVTrans; i++){
21390 if( db->aVTrans[i]==pVTab ){
21391 return SQLITE_OK;
21392 }
21393 }
21394
21395 /* Invoke the xBegin method. If successful, add the vtab to the
21396 ** sqlite3.aVTrans[] array. */
21397 rc = growVTrans(db);
21398 if( rc==SQLITE_OK ){
21399 rc = pModule->xBegin(pVTab->pVtab);
21400 if( rc==SQLITE_OK ){
21401 int iSvpt = db->nStatement + db->nSavepoint;
21402 addToVTrans(db, pVTab);
21403 if( iSvpt && pModule->xSavepoint ){
21404 pVTab->iSavepoint = iSvpt;
21405 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
21406 }
21407 }
21408 }
21409 }
21410 return rc;
21411 }
21412
21413 /*
21414 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
21415 ** virtual tables that currently have an open transaction. Pass iSavepoint
21416 ** as the second argument to the virtual table method invoked.
21417 **
21418 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
21419 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
21420 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
21421 ** an open transaction is invoked.
21422 **
21423 ** If any virtual table method returns an error code other than SQLITE_OK,
21424 ** processing is abandoned and the error returned to the caller of this
21425 ** function immediately. If all calls to virtual table methods are successful,
21426 ** SQLITE_OK is returned.
21427 */
21428 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
21429 int rc = SQLITE_OK;
21430
21431 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
21432 assert( iSavepoint>=-1 );
21433 if( db->aVTrans ){
21434 int i;
21435 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
21436 VTable *pVTab = db->aVTrans[i];
21437 const sqlite3_module *pMod = pVTab->pMod->pModule;
21438 if( pVTab->pVtab && pMod->iVersion>=2 ){
21439 int (*xMethod)(sqlite3_vtab *, int);
21440 switch( op ){
21441 case SAVEPOINT_BEGIN:
21442 xMethod = pMod->xSavepoint;
21443 pVTab->iSavepoint = iSavepoint+1;
21444 break;
21445 case SAVEPOINT_ROLLBACK:
21446 xMethod = pMod->xRollbackTo;
21447 break;
21448 default:
21449 xMethod = pMod->xRelease;
21450 break;
21451 }
21452 if( xMethod && pVTab->iSavepoint>iSavepoint ){
21453 rc = xMethod(pVTab->pVtab, iSavepoint);
21454 }
21455 }
21456 }
21457 }
21458 return rc;
21459 }
21460
21461 /*
21462 ** The first parameter (pDef) is a function implementation. The
21463 ** second parameter (pExpr) is the first argument to this function.
21464 ** If pExpr is a column in a virtual table, then let the virtual
21465 ** table implementation have an opportunity to overload the function.
21466 **
21467 ** This routine is used to allow virtual table implementations to
21468 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
21469 **
21470 ** Return either the pDef argument (indicating no change) or a
21471 ** new FuncDef structure that is marked as ephemeral using the
21472 ** SQLITE_FUNC_EPHEM flag.
21473 */
21474 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
21475 sqlite3 *db, /* Database connection for reporting malloc problems */
21476 FuncDef *pDef, /* Function to possibly overload */
21477 int nArg, /* Number of arguments to the function */
21478 Expr *pExpr /* First argument to the function */
21479 ){
21480 Table *pTab;
21481 sqlite3_vtab *pVtab;
21482 sqlite3_module *pMod;
21483 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
21484 void *pArg = 0;
21485 FuncDef *pNew;
21486 int rc = 0;
21487 char *zLowerName;
21488 unsigned char *z;
21489
21490
21491 /* Check to see the left operand is a column in a virtual table */
21492 if( NEVER(pExpr==0) ) return pDef;
21493 if( pExpr->op!=TK_COLUMN ) return pDef;
21494 pTab = pExpr->pTab;
21495 if( NEVER(pTab==0) ) return pDef;
21496 if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
21497 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
21498 assert( pVtab!=0 );
21499 assert( pVtab->pModule!=0 );
21500 pMod = (sqlite3_module *)pVtab->pModule;
21501 if( pMod->xFindFunction==0 ) return pDef;
21502
21503 /* Call the xFindFunction method on the virtual table implementation
21504 ** to see if the implementation wants to overload this function
21505 */
21506 zLowerName = sqlite3DbStrDup(db, pDef->zName);
21507 if( zLowerName ){
21508 for(z=(unsigned char*)zLowerName; *z; z++){
21509 *z = sqlite3UpperToLower[*z];
21510 }
21511 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg);
21512 sqlite3DbFree(db, zLowerName);
21513 }
21514 if( rc==0 ){
21515 return pDef;
21516 }
21517
21518 /* Create a new ephemeral function definition for the overloaded
21519 ** function */
21520 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
21521 + sqlite3Strlen30(pDef->zName) + 1);
21522 if( pNew==0 ){
21523 return pDef;
21524 }
21525 *pNew = *pDef;
21526 pNew->zName = (const char*)&pNew[1];
21527 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
21528 pNew->xSFunc = xSFunc;
21529 pNew->pUserData = pArg;
21530 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
21531 return pNew;
21532 }
21533
21534 /*
21535 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
21536 ** array so that an OP_VBegin will get generated for it. Add pTab to the
21537 ** array if it is missing. If pTab is already in the array, this routine
21538 ** is a no-op.
21539 */
21540 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
21541 Parse *pToplevel = sqlite3ParseToplevel(pParse);
21542 int i, n;
21543 Table **apVtabLock;
21544
21545 assert( IsVirtual(pTab) );
21546 for(i=0; i<pToplevel->nVtabLock; i++){
21547 if( pTab==pToplevel->apVtabLock[i] ) return;
21548 }
21549 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
21550 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
21551 if( apVtabLock ){
21552 pToplevel->apVtabLock = apVtabLock;
21553 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
21554 }else{
21555 sqlite3OomFault(pToplevel->db);
21556 }
21557 }
21558
21559 /*
21560 ** Check to see if virtual table module pMod can be have an eponymous
21561 ** virtual table instance. If it can, create one if one does not already
21562 ** exist. Return non-zero if the eponymous virtual table instance exists
21563 ** when this routine returns, and return zero if it does not exist.
21564 **
21565 ** An eponymous virtual table instance is one that is named after its
21566 ** module, and more importantly, does not require a CREATE VIRTUAL TABLE
21567 ** statement in order to come into existance. Eponymous virtual table
21568 ** instances always exist. They cannot be DROP-ed.
21569 **
21570 ** Any virtual table module for which xConnect and xCreate are the same
21571 ** method can have an eponymous virtual table instance.
21572 */
21573 SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
21574 const sqlite3_module *pModule = pMod->pModule;
21575 Table *pTab;
21576 char *zErr = 0;
21577 int rc;
21578 sqlite3 *db = pParse->db;
21579 if( pMod->pEpoTab ) return 1;
21580 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
21581 pTab = sqlite3DbMallocZero(db, sizeof(Table));
21582 if( pTab==0 ) return 0;
21583 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
21584 if( pTab->zName==0 ){
21585 sqlite3DbFree(db, pTab);
21586 return 0;
21587 }
21588 pMod->pEpoTab = pTab;
21589 pTab->nTabRef = 1;
21590 pTab->pSchema = db->aDb[0].pSchema;
21591 pTab->tabFlags |= TF_Virtual;
21592 pTab->nModuleArg = 0;
21593 pTab->iPKey = -1;
21594 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
21595 addModuleArgument(db, pTab, 0);
21596 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
21597 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
21598 if( rc ){
21599 sqlite3ErrorMsg(pParse, "%s", zErr);
21600 sqlite3DbFree(db, zErr);
21601 sqlite3VtabEponymousTableClear(db, pMod);
21602 return 0;
21603 }
21604 return 1;
21605 }
21606
21607 /*
21608 ** Erase the eponymous virtual table instance associated with
21609 ** virtual table module pMod, if it exists.
21610 */
21611 SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
21612 Table *pTab = pMod->pEpoTab;
21613 if( pTab!=0 ){
21614 /* Mark the table as Ephemeral prior to deleting it, so that the
21615 ** sqlite3DeleteTable() routine will know that it is not stored in
21616 ** the schema. */
21617 pTab->tabFlags |= TF_Ephemeral;
21618 sqlite3DeleteTable(db, pTab);
21619 pMod->pEpoTab = 0;
21620 }
21621 }
21622
21623 /*
21624 ** Return the ON CONFLICT resolution mode in effect for the virtual
21625 ** table update operation currently in progress.
21626 **
21627 ** The results of this routine are undefined unless it is called from
21628 ** within an xUpdate method.
21629 */
21630 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
21631 static const unsigned char aMap[] = {
21632 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
21633 };
21634 #ifdef SQLITE_ENABLE_API_ARMOR
21635 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
21636 #endif
21637 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
21638 assert( OE_Ignore==4 && OE_Replace==5 );
21639 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
21640 return (int)aMap[db->vtabOnConflict-1];
21641 }
21642
21643 /*
21644 ** Call from within the xCreate() or xConnect() methods to provide
21645 ** the SQLite core with additional information about the behavior
21646 ** of the virtual table being implemented.
21647 */
21648 SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
21649 va_list ap;
21650 int rc = SQLITE_OK;
21651
21652 #ifdef SQLITE_ENABLE_API_ARMOR
21653 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
21654 #endif
21655 sqlite3_mutex_enter(db->mutex);
21656 va_start(ap, op);
21657 switch( op ){
21658 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
21659 VtabCtx *p = db->pVtabCtx;
21660 if( !p ){
21661 rc = SQLITE_MISUSE_BKPT;
21662 }else{
21663 assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
21664 p->pVTable->bConstraint = (u8)va_arg(ap, int);
21665 }
21666 break;
21667 }
21668 default:
21669 rc = SQLITE_MISUSE_BKPT;
21670 break;
21671 }
21672 va_end(ap);
21673
21674 if( rc!=SQLITE_OK ) sqlite3Error(db, rc);
21675 sqlite3_mutex_leave(db->mutex);
21676 return rc;
21677 }
21678
21679 #endif /* SQLITE_OMIT_VIRTUALTABLE */
21680
21681 /************** End of vtab.c ************************************************/
21682
21683 /* Chain include. */
21684 #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