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

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

Issue 1636873003: Try for backport (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@zzsql_import3_10_2_websql_backport
Patch Set: Created 4 years, 10 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 analyze.c *****************************************/
2 /*
3 ** 2005-07-08
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 ** This file contains code associated with the ANALYZE command.
14 **
15 ** The ANALYZE command gather statistics about the content of tables
16 ** and indices. These statistics are made available to the query planner
17 ** to help it make better decisions about how to perform queries.
18 **
19 ** The following system tables are or have been supported:
20 **
21 ** CREATE TABLE sqlite_stat1(tbl, idx, stat);
22 ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
23 ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
24 ** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
25 **
26 ** Additional tables might be added in future releases of SQLite.
27 ** The sqlite_stat2 table is not created or used unless the SQLite version
28 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
29 ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
30 ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
31 ** created and used by SQLite versions 3.7.9 and later and with
32 ** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
33 ** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
34 ** version of sqlite_stat3 and is only available when compiled with
35 ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is
36 ** not possible to enable both STAT3 and STAT4 at the same time. If they
37 ** are both enabled, then STAT4 takes precedence.
38 **
39 ** For most applications, sqlite_stat1 provides all the statistics required
40 ** for the query planner to make good choices.
41 **
42 ** Format of sqlite_stat1:
43 **
44 ** There is normally one row per index, with the index identified by the
45 ** name in the idx column. The tbl column is the name of the table to
46 ** which the index belongs. In each such row, the stat column will be
47 ** a string consisting of a list of integers. The first integer in this
48 ** list is the number of rows in the index. (This is the same as the
49 ** number of rows in the table, except for partial indices.) The second
50 ** integer is the average number of rows in the index that have the same
51 ** value in the first column of the index. The third integer is the average
52 ** number of rows in the index that have the same value for the first two
53 ** columns. The N-th integer (for N>1) is the average number of rows in
54 ** the index which have the same value for the first N-1 columns. For
55 ** a K-column index, there will be K+1 integers in the stat column. If
56 ** the index is unique, then the last integer will be 1.
57 **
58 ** The list of integers in the stat column can optionally be followed
59 ** by the keyword "unordered". The "unordered" keyword, if it is present,
60 ** must be separated from the last integer by a single space. If the
61 ** "unordered" keyword is present, then the query planner assumes that
62 ** the index is unordered and will not use the index for a range query.
63 **
64 ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
65 ** column contains a single integer which is the (estimated) number of
66 ** rows in the table identified by sqlite_stat1.tbl.
67 **
68 ** Format of sqlite_stat2:
69 **
70 ** The sqlite_stat2 is only created and is only used if SQLite is compiled
71 ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
72 ** 3.6.18 and 3.7.8. The "stat2" table contains additional information
73 ** about the distribution of keys within an index. The index is identified by
74 ** the "idx" column and the "tbl" column is the name of the table to which
75 ** the index belongs. There are usually 10 rows in the sqlite_stat2
76 ** table for each index.
77 **
78 ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
79 ** inclusive are samples of the left-most key value in the index taken at
80 ** evenly spaced points along the index. Let the number of samples be S
81 ** (10 in the standard build) and let C be the number of rows in the index.
82 ** Then the sampled rows are given by:
83 **
84 ** rownumber = (i*C*2 + C)/(S*2)
85 **
86 ** For i between 0 and S-1. Conceptually, the index space is divided into
87 ** S uniform buckets and the samples are the middle row from each bucket.
88 **
89 ** The format for sqlite_stat2 is recorded here for legacy reference. This
90 ** version of SQLite does not support sqlite_stat2. It neither reads nor
91 ** writes the sqlite_stat2 table. This version of SQLite only supports
92 ** sqlite_stat3.
93 **
94 ** Format for sqlite_stat3:
95 **
96 ** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
97 ** sqlite_stat4 format will be described first. Further information
98 ** about sqlite_stat3 follows the sqlite_stat4 description.
99 **
100 ** Format for sqlite_stat4:
101 **
102 ** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
103 ** to aid the query planner in choosing good indices based on the values
104 ** that indexed columns are compared against in the WHERE clauses of
105 ** queries.
106 **
107 ** The sqlite_stat4 table contains multiple entries for each index.
108 ** The idx column names the index and the tbl column is the table of the
109 ** index. If the idx and tbl columns are the same, then the sample is
110 ** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
111 ** binary encoding of a key from the index. The nEq column is a
112 ** list of integers. The first integer is the approximate number
113 ** of entries in the index whose left-most column exactly matches
114 ** the left-most column of the sample. The second integer in nEq
115 ** is the approximate number of entries in the index where the
116 ** first two columns match the first two columns of the sample.
117 ** And so forth. nLt is another list of integers that show the approximate
118 ** number of entries that are strictly less than the sample. The first
119 ** integer in nLt contains the number of entries in the index where the
120 ** left-most column is less than the left-most column of the sample.
121 ** The K-th integer in the nLt entry is the number of index entries
122 ** where the first K columns are less than the first K columns of the
123 ** sample. The nDLt column is like nLt except that it contains the
124 ** number of distinct entries in the index that are less than the
125 ** sample.
126 **
127 ** There can be an arbitrary number of sqlite_stat4 entries per index.
128 ** The ANALYZE command will typically generate sqlite_stat4 tables
129 ** that contain between 10 and 40 samples which are distributed across
130 ** the key space, though not uniformly, and which include samples with
131 ** large nEq values.
132 **
133 ** Format for sqlite_stat3 redux:
134 **
135 ** The sqlite_stat3 table is like sqlite_stat4 except that it only
136 ** looks at the left-most column of the index. The sqlite_stat3.sample
137 ** column contains the actual value of the left-most column instead
138 ** of a blob encoding of the complete index key as is found in
139 ** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
140 ** all contain just a single integer which is the same as the first
141 ** integer in the equivalent columns in sqlite_stat4.
142 */
143 #ifndef SQLITE_OMIT_ANALYZE
144 /* #include "sqliteInt.h" */
145
146 #if defined(SQLITE_ENABLE_STAT4)
147 # define IsStat4 1
148 # define IsStat3 0
149 #elif defined(SQLITE_ENABLE_STAT3)
150 # define IsStat4 0
151 # define IsStat3 1
152 #else
153 # define IsStat4 0
154 # define IsStat3 0
155 # undef SQLITE_STAT4_SAMPLES
156 # define SQLITE_STAT4_SAMPLES 1
157 #endif
158 #define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */
159
160 /*
161 ** This routine generates code that opens the sqlite_statN tables.
162 ** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
163 ** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
164 ** appropriate compile-time options are provided.
165 **
166 ** If the sqlite_statN tables do not previously exist, it is created.
167 **
168 ** Argument zWhere may be a pointer to a buffer containing a table name,
169 ** or it may be a NULL pointer. If it is not NULL, then all entries in
170 ** the sqlite_statN tables associated with the named table are deleted.
171 ** If zWhere==0, then code is generated to delete all stat table entries.
172 */
173 static void openStatTable(
174 Parse *pParse, /* Parsing context */
175 int iDb, /* The database we are looking in */
176 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
177 const char *zWhere, /* Delete entries for this table or index */
178 const char *zWhereType /* Either "tbl" or "idx" */
179 ){
180 static const struct {
181 const char *zName;
182 const char *zCols;
183 } aTable[] = {
184 { "sqlite_stat1", "tbl,idx,stat" },
185 #if defined(SQLITE_ENABLE_STAT4)
186 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
187 { "sqlite_stat3", 0 },
188 #elif defined(SQLITE_ENABLE_STAT3)
189 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
190 { "sqlite_stat4", 0 },
191 #else
192 { "sqlite_stat3", 0 },
193 { "sqlite_stat4", 0 },
194 #endif
195 };
196 int i;
197 sqlite3 *db = pParse->db;
198 Db *pDb;
199 Vdbe *v = sqlite3GetVdbe(pParse);
200 int aRoot[ArraySize(aTable)];
201 u8 aCreateTbl[ArraySize(aTable)];
202
203 if( v==0 ) return;
204 assert( sqlite3BtreeHoldsAllMutexes(db) );
205 assert( sqlite3VdbeDb(v)==db );
206 pDb = &db->aDb[iDb];
207
208 /* Create new statistic tables if they do not exist, or clear them
209 ** if they do already exist.
210 */
211 for(i=0; i<ArraySize(aTable); i++){
212 const char *zTab = aTable[i].zName;
213 Table *pStat;
214 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
215 if( aTable[i].zCols ){
216 /* The sqlite_statN table does not exist. Create it. Note that a
217 ** side-effect of the CREATE TABLE statement is to leave the rootpage
218 ** of the new table in register pParse->regRoot. This is important
219 ** because the OpenWrite opcode below will be needing it. */
220 sqlite3NestedParse(pParse,
221 "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
222 );
223 aRoot[i] = pParse->regRoot;
224 aCreateTbl[i] = OPFLAG_P2ISREG;
225 }
226 }else{
227 /* The table already exists. If zWhere is not NULL, delete all entries
228 ** associated with the table zWhere. If zWhere is NULL, delete the
229 ** entire contents of the table. */
230 aRoot[i] = pStat->tnum;
231 aCreateTbl[i] = 0;
232 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
233 if( zWhere ){
234 sqlite3NestedParse(pParse,
235 "DELETE FROM %Q.%s WHERE %s=%Q",
236 pDb->zName, zTab, zWhereType, zWhere
237 );
238 }else{
239 /* The sqlite_stat[134] table already exists. Delete all rows. */
240 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
241 }
242 }
243 }
244
245 /* Open the sqlite_stat[134] tables for writing. */
246 for(i=0; aTable[i].zCols; i++){
247 assert( i<ArraySize(aTable) );
248 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
249 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
250 VdbeComment((v, aTable[i].zName));
251 }
252 }
253
254 /*
255 ** Recommended number of samples for sqlite_stat4
256 */
257 #ifndef SQLITE_STAT4_SAMPLES
258 # define SQLITE_STAT4_SAMPLES 24
259 #endif
260
261 /*
262 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
263 ** share an instance of the following structure to hold their state
264 ** information.
265 */
266 typedef struct Stat4Accum Stat4Accum;
267 typedef struct Stat4Sample Stat4Sample;
268 struct Stat4Sample {
269 tRowcnt *anEq; /* sqlite_stat4.nEq */
270 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
271 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
272 tRowcnt *anLt; /* sqlite_stat4.nLt */
273 union {
274 i64 iRowid; /* Rowid in main table of the key */
275 u8 *aRowid; /* Key for WITHOUT ROWID tables */
276 } u;
277 u32 nRowid; /* Sizeof aRowid[] */
278 u8 isPSample; /* True if a periodic sample */
279 int iCol; /* If !isPSample, the reason for inclusion */
280 u32 iHash; /* Tiebreaker hash */
281 #endif
282 };
283 struct Stat4Accum {
284 tRowcnt nRow; /* Number of rows in the entire table */
285 tRowcnt nPSample; /* How often to do a periodic sample */
286 int nCol; /* Number of columns in index + pk/rowid */
287 int nKeyCol; /* Number of index columns w/o the pk/rowid */
288 int mxSample; /* Maximum number of samples to accumulate */
289 Stat4Sample current; /* Current row as a Stat4Sample */
290 u32 iPrn; /* Pseudo-random number used for sampling */
291 Stat4Sample *aBest; /* Array of nCol best samples */
292 int iMin; /* Index in a[] of entry with minimum score */
293 int nSample; /* Current number of samples */
294 int iGet; /* Index of current sample accessed by stat_get() */
295 Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
296 sqlite3 *db; /* Database connection, for malloc() */
297 };
298
299 /* Reclaim memory used by a Stat4Sample
300 */
301 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
302 static void sampleClear(sqlite3 *db, Stat4Sample *p){
303 assert( db!=0 );
304 if( p->nRowid ){
305 sqlite3DbFree(db, p->u.aRowid);
306 p->nRowid = 0;
307 }
308 }
309 #endif
310
311 /* Initialize the BLOB value of a ROWID
312 */
313 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
314 static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
315 assert( db!=0 );
316 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
317 p->u.aRowid = sqlite3DbMallocRaw(db, n);
318 if( p->u.aRowid ){
319 p->nRowid = n;
320 memcpy(p->u.aRowid, pData, n);
321 }else{
322 p->nRowid = 0;
323 }
324 }
325 #endif
326
327 /* Initialize the INTEGER value of a ROWID.
328 */
329 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
330 static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
331 assert( db!=0 );
332 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
333 p->nRowid = 0;
334 p->u.iRowid = iRowid;
335 }
336 #endif
337
338
339 /*
340 ** Copy the contents of object (*pFrom) into (*pTo).
341 */
342 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
343 static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
344 pTo->isPSample = pFrom->isPSample;
345 pTo->iCol = pFrom->iCol;
346 pTo->iHash = pFrom->iHash;
347 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
348 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
349 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
350 if( pFrom->nRowid ){
351 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
352 }else{
353 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
354 }
355 }
356 #endif
357
358 /*
359 ** Reclaim all memory of a Stat4Accum structure.
360 */
361 static void stat4Destructor(void *pOld){
362 Stat4Accum *p = (Stat4Accum*)pOld;
363 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
364 int i;
365 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
366 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
367 sampleClear(p->db, &p->current);
368 #endif
369 sqlite3DbFree(p->db, p);
370 }
371
372 /*
373 ** Implementation of the stat_init(N,K,C) SQL function. The three parameters
374 ** are:
375 ** N: The number of columns in the index including the rowid/pk (note 1)
376 ** K: The number of columns in the index excluding the rowid/pk.
377 ** C: The number of rows in the index (note 2)
378 **
379 ** Note 1: In the special case of the covering index that implements a
380 ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
381 ** total number of columns in the table.
382 **
383 ** Note 2: C is only used for STAT3 and STAT4.
384 **
385 ** For indexes on ordinary rowid tables, N==K+1. But for indexes on
386 ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
387 ** PRIMARY KEY of the table. The covering index that implements the
388 ** original WITHOUT ROWID table as N==K as a special case.
389 **
390 ** This routine allocates the Stat4Accum object in heap memory. The return
391 ** value is a pointer to the Stat4Accum object. The datatype of the
392 ** return value is BLOB, but it is really just a pointer to the Stat4Accum
393 ** object.
394 */
395 static void statInit(
396 sqlite3_context *context,
397 int argc,
398 sqlite3_value **argv
399 ){
400 Stat4Accum *p;
401 int nCol; /* Number of columns in index being sampled */
402 int nKeyCol; /* Number of key columns */
403 int nColUp; /* nCol rounded up for alignment */
404 int n; /* Bytes of space to allocate */
405 sqlite3 *db; /* Database connection */
406 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
407 int mxSample = SQLITE_STAT4_SAMPLES;
408 #endif
409
410 /* Decode the three function arguments */
411 UNUSED_PARAMETER(argc);
412 nCol = sqlite3_value_int(argv[0]);
413 assert( nCol>0 );
414 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
415 nKeyCol = sqlite3_value_int(argv[1]);
416 assert( nKeyCol<=nCol );
417 assert( nKeyCol>0 );
418
419 /* Allocate the space required for the Stat4Accum object */
420 n = sizeof(*p)
421 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
422 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
423 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
424 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
425 + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
426 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
427 #endif
428 ;
429 db = sqlite3_context_db_handle(context);
430 p = sqlite3DbMallocZero(db, n);
431 if( p==0 ){
432 sqlite3_result_error_nomem(context);
433 return;
434 }
435
436 p->db = db;
437 p->nRow = 0;
438 p->nCol = nCol;
439 p->nKeyCol = nKeyCol;
440 p->current.anDLt = (tRowcnt*)&p[1];
441 p->current.anEq = &p->current.anDLt[nColUp];
442
443 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
444 {
445 u8 *pSpace; /* Allocated space not yet assigned */
446 int i; /* Used to iterate through p->aSample[] */
447
448 p->iGet = -1;
449 p->mxSample = mxSample;
450 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
451 p->current.anLt = &p->current.anEq[nColUp];
452 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
453
454 /* Set up the Stat4Accum.a[] and aBest[] arrays */
455 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
456 p->aBest = &p->a[mxSample];
457 pSpace = (u8*)(&p->a[mxSample+nCol]);
458 for(i=0; i<(mxSample+nCol); i++){
459 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
460 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
461 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
462 }
463 assert( (pSpace - (u8*)p)==n );
464
465 for(i=0; i<nCol; i++){
466 p->aBest[i].iCol = i;
467 }
468 }
469 #endif
470
471 /* Return a pointer to the allocated object to the caller. Note that
472 ** only the pointer (the 2nd parameter) matters. The size of the object
473 ** (given by the 3rd parameter) is never used and can be any positive
474 ** value. */
475 sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
476 }
477 static const FuncDef statInitFuncdef = {
478 2+IsStat34, /* nArg */
479 SQLITE_UTF8, /* funcFlags */
480 0, /* pUserData */
481 0, /* pNext */
482 statInit, /* xFunc */
483 0, /* xStep */
484 0, /* xFinalize */
485 "stat_init", /* zName */
486 0, /* pHash */
487 0 /* pDestructor */
488 };
489
490 #ifdef SQLITE_ENABLE_STAT4
491 /*
492 ** pNew and pOld are both candidate non-periodic samples selected for
493 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
494 ** considering only any trailing columns and the sample hash value, this
495 ** function returns true if sample pNew is to be preferred over pOld.
496 ** In other words, if we assume that the cardinalities of the selected
497 ** column for pNew and pOld are equal, is pNew to be preferred over pOld.
498 **
499 ** This function assumes that for each argument sample, the contents of
500 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
501 */
502 static int sampleIsBetterPost(
503 Stat4Accum *pAccum,
504 Stat4Sample *pNew,
505 Stat4Sample *pOld
506 ){
507 int nCol = pAccum->nCol;
508 int i;
509 assert( pNew->iCol==pOld->iCol );
510 for(i=pNew->iCol+1; i<nCol; i++){
511 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
512 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
513 }
514 if( pNew->iHash>pOld->iHash ) return 1;
515 return 0;
516 }
517 #endif
518
519 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
520 /*
521 ** Return true if pNew is to be preferred over pOld.
522 **
523 ** This function assumes that for each argument sample, the contents of
524 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
525 */
526 static int sampleIsBetter(
527 Stat4Accum *pAccum,
528 Stat4Sample *pNew,
529 Stat4Sample *pOld
530 ){
531 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
532 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
533
534 assert( pOld->isPSample==0 && pNew->isPSample==0 );
535 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
536
537 if( (nEqNew>nEqOld) ) return 1;
538 #ifdef SQLITE_ENABLE_STAT4
539 if( nEqNew==nEqOld ){
540 if( pNew->iCol<pOld->iCol ) return 1;
541 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
542 }
543 return 0;
544 #else
545 return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
546 #endif
547 }
548
549 /*
550 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
551 ** remove the least desirable sample from p->a[] to make room.
552 */
553 static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
554 Stat4Sample *pSample = 0;
555 int i;
556
557 assert( IsStat4 || nEqZero==0 );
558
559 #ifdef SQLITE_ENABLE_STAT4
560 if( pNew->isPSample==0 ){
561 Stat4Sample *pUpgrade = 0;
562 assert( pNew->anEq[pNew->iCol]>0 );
563
564 /* This sample is being added because the prefix that ends in column
565 ** iCol occurs many times in the table. However, if we have already
566 ** added a sample that shares this prefix, there is no need to add
567 ** this one. Instead, upgrade the priority of the highest priority
568 ** existing sample that shares this prefix. */
569 for(i=p->nSample-1; i>=0; i--){
570 Stat4Sample *pOld = &p->a[i];
571 if( pOld->anEq[pNew->iCol]==0 ){
572 if( pOld->isPSample ) return;
573 assert( pOld->iCol>pNew->iCol );
574 assert( sampleIsBetter(p, pNew, pOld) );
575 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
576 pUpgrade = pOld;
577 }
578 }
579 }
580 if( pUpgrade ){
581 pUpgrade->iCol = pNew->iCol;
582 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
583 goto find_new_min;
584 }
585 }
586 #endif
587
588 /* If necessary, remove sample iMin to make room for the new sample. */
589 if( p->nSample>=p->mxSample ){
590 Stat4Sample *pMin = &p->a[p->iMin];
591 tRowcnt *anEq = pMin->anEq;
592 tRowcnt *anLt = pMin->anLt;
593 tRowcnt *anDLt = pMin->anDLt;
594 sampleClear(p->db, pMin);
595 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
596 pSample = &p->a[p->nSample-1];
597 pSample->nRowid = 0;
598 pSample->anEq = anEq;
599 pSample->anDLt = anDLt;
600 pSample->anLt = anLt;
601 p->nSample = p->mxSample-1;
602 }
603
604 /* The "rows less-than" for the rowid column must be greater than that
605 ** for the last sample in the p->a[] array. Otherwise, the samples would
606 ** be out of order. */
607 #ifdef SQLITE_ENABLE_STAT4
608 assert( p->nSample==0
609 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
610 #endif
611
612 /* Insert the new sample */
613 pSample = &p->a[p->nSample];
614 sampleCopy(p, pSample, pNew);
615 p->nSample++;
616
617 /* Zero the first nEqZero entries in the anEq[] array. */
618 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
619
620 #ifdef SQLITE_ENABLE_STAT4
621 find_new_min:
622 #endif
623 if( p->nSample>=p->mxSample ){
624 int iMin = -1;
625 for(i=0; i<p->mxSample; i++){
626 if( p->a[i].isPSample ) continue;
627 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
628 iMin = i;
629 }
630 }
631 assert( iMin>=0 );
632 p->iMin = iMin;
633 }
634 }
635 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
636
637 /*
638 ** Field iChng of the index being scanned has changed. So at this point
639 ** p->current contains a sample that reflects the previous row of the
640 ** index. The value of anEq[iChng] and subsequent anEq[] elements are
641 ** correct at this point.
642 */
643 static void samplePushPrevious(Stat4Accum *p, int iChng){
644 #ifdef SQLITE_ENABLE_STAT4
645 int i;
646
647 /* Check if any samples from the aBest[] array should be pushed
648 ** into IndexSample.a[] at this point. */
649 for(i=(p->nCol-2); i>=iChng; i--){
650 Stat4Sample *pBest = &p->aBest[i];
651 pBest->anEq[i] = p->current.anEq[i];
652 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
653 sampleInsert(p, pBest, i);
654 }
655 }
656
657 /* Update the anEq[] fields of any samples already collected. */
658 for(i=p->nSample-1; i>=0; i--){
659 int j;
660 for(j=iChng; j<p->nCol; j++){
661 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
662 }
663 }
664 #endif
665
666 #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
667 if( iChng==0 ){
668 tRowcnt nLt = p->current.anLt[0];
669 tRowcnt nEq = p->current.anEq[0];
670
671 /* Check if this is to be a periodic sample. If so, add it. */
672 if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
673 p->current.isPSample = 1;
674 sampleInsert(p, &p->current, 0);
675 p->current.isPSample = 0;
676 }else
677
678 /* Or if it is a non-periodic sample. Add it in this case too. */
679 if( p->nSample<p->mxSample
680 || sampleIsBetter(p, &p->current, &p->a[p->iMin])
681 ){
682 sampleInsert(p, &p->current, 0);
683 }
684 }
685 #endif
686
687 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
688 UNUSED_PARAMETER( p );
689 UNUSED_PARAMETER( iChng );
690 #endif
691 }
692
693 /*
694 ** Implementation of the stat_push SQL function: stat_push(P,C,R)
695 ** Arguments:
696 **
697 ** P Pointer to the Stat4Accum object created by stat_init()
698 ** C Index of left-most column to differ from previous row
699 ** R Rowid for the current row. Might be a key record for
700 ** WITHOUT ROWID tables.
701 **
702 ** This SQL function always returns NULL. It's purpose it to accumulate
703 ** statistical data and/or samples in the Stat4Accum object about the
704 ** index being analyzed. The stat_get() SQL function will later be used to
705 ** extract relevant information for constructing the sqlite_statN tables.
706 **
707 ** The R parameter is only used for STAT3 and STAT4
708 */
709 static void statPush(
710 sqlite3_context *context,
711 int argc,
712 sqlite3_value **argv
713 ){
714 int i;
715
716 /* The three function arguments */
717 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
718 int iChng = sqlite3_value_int(argv[1]);
719
720 UNUSED_PARAMETER( argc );
721 UNUSED_PARAMETER( context );
722 assert( p->nCol>0 );
723 assert( iChng<p->nCol );
724
725 if( p->nRow==0 ){
726 /* This is the first call to this function. Do initialization. */
727 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
728 }else{
729 /* Second and subsequent calls get processed here */
730 samplePushPrevious(p, iChng);
731
732 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
733 ** to the current row of the index. */
734 for(i=0; i<iChng; i++){
735 p->current.anEq[i]++;
736 }
737 for(i=iChng; i<p->nCol; i++){
738 p->current.anDLt[i]++;
739 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
740 p->current.anLt[i] += p->current.anEq[i];
741 #endif
742 p->current.anEq[i] = 1;
743 }
744 }
745 p->nRow++;
746 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
747 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
748 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
749 }else{
750 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
751 sqlite3_value_blob(argv[2]));
752 }
753 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
754 #endif
755
756 #ifdef SQLITE_ENABLE_STAT4
757 {
758 tRowcnt nLt = p->current.anLt[p->nCol-1];
759
760 /* Check if this is to be a periodic sample. If so, add it. */
761 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
762 p->current.isPSample = 1;
763 p->current.iCol = 0;
764 sampleInsert(p, &p->current, p->nCol-1);
765 p->current.isPSample = 0;
766 }
767
768 /* Update the aBest[] array. */
769 for(i=0; i<(p->nCol-1); i++){
770 p->current.iCol = i;
771 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
772 sampleCopy(p, &p->aBest[i], &p->current);
773 }
774 }
775 }
776 #endif
777 }
778 static const FuncDef statPushFuncdef = {
779 2+IsStat34, /* nArg */
780 SQLITE_UTF8, /* funcFlags */
781 0, /* pUserData */
782 0, /* pNext */
783 statPush, /* xFunc */
784 0, /* xStep */
785 0, /* xFinalize */
786 "stat_push", /* zName */
787 0, /* pHash */
788 0 /* pDestructor */
789 };
790
791 #define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
792 #define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
793 #define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
794 #define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
795 #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
796
797 /*
798 ** Implementation of the stat_get(P,J) SQL function. This routine is
799 ** used to query statistical information that has been gathered into
800 ** the Stat4Accum object by prior calls to stat_push(). The P parameter
801 ** has type BLOB but it is really just a pointer to the Stat4Accum object.
802 ** The content to returned is determined by the parameter J
803 ** which is one of the STAT_GET_xxxx values defined above.
804 **
805 ** If neither STAT3 nor STAT4 are enabled, then J is always
806 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
807 ** a one-parameter function, stat_get(P), that always returns the
808 ** stat1 table entry information.
809 */
810 static void statGet(
811 sqlite3_context *context,
812 int argc,
813 sqlite3_value **argv
814 ){
815 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
816 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
817 /* STAT3 and STAT4 have a parameter on this routine. */
818 int eCall = sqlite3_value_int(argv[1]);
819 assert( argc==2 );
820 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
821 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
822 || eCall==STAT_GET_NDLT
823 );
824 if( eCall==STAT_GET_STAT1 )
825 #else
826 assert( argc==1 );
827 #endif
828 {
829 /* Return the value to store in the "stat" column of the sqlite_stat1
830 ** table for this index.
831 **
832 ** The value is a string composed of a list of integers describing
833 ** the index. The first integer in the list is the total number of
834 ** entries in the index. There is one additional integer in the list
835 ** for each indexed column. This additional integer is an estimate of
836 ** the number of rows matched by a stabbing query on the index using
837 ** a key with the corresponding number of fields. In other words,
838 ** if the index is on columns (a,b) and the sqlite_stat1 value is
839 ** "100 10 2", then SQLite estimates that:
840 **
841 ** * the index contains 100 rows,
842 ** * "WHERE a=?" matches 10 rows, and
843 ** * "WHERE a=? AND b=?" matches 2 rows.
844 **
845 ** If D is the count of distinct values and K is the total number of
846 ** rows, then each estimate is computed as:
847 **
848 ** I = (K+D-1)/D
849 */
850 char *z;
851 int i;
852
853 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
854 if( zRet==0 ){
855 sqlite3_result_error_nomem(context);
856 return;
857 }
858
859 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
860 z = zRet + sqlite3Strlen30(zRet);
861 for(i=0; i<p->nKeyCol; i++){
862 u64 nDistinct = p->current.anDLt[i] + 1;
863 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
864 sqlite3_snprintf(24, z, " %llu", iVal);
865 z += sqlite3Strlen30(z);
866 assert( p->current.anEq[i] );
867 }
868 assert( z[0]=='\0' && z>zRet );
869
870 sqlite3_result_text(context, zRet, -1, sqlite3_free);
871 }
872 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
873 else if( eCall==STAT_GET_ROWID ){
874 if( p->iGet<0 ){
875 samplePushPrevious(p, 0);
876 p->iGet = 0;
877 }
878 if( p->iGet<p->nSample ){
879 Stat4Sample *pS = p->a + p->iGet;
880 if( pS->nRowid==0 ){
881 sqlite3_result_int64(context, pS->u.iRowid);
882 }else{
883 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
884 SQLITE_TRANSIENT);
885 }
886 }
887 }else{
888 tRowcnt *aCnt = 0;
889
890 assert( p->iGet<p->nSample );
891 switch( eCall ){
892 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
893 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
894 default: {
895 aCnt = p->a[p->iGet].anDLt;
896 p->iGet++;
897 break;
898 }
899 }
900
901 if( IsStat3 ){
902 sqlite3_result_int64(context, (i64)aCnt[0]);
903 }else{
904 char *zRet = sqlite3MallocZero(p->nCol * 25);
905 if( zRet==0 ){
906 sqlite3_result_error_nomem(context);
907 }else{
908 int i;
909 char *z = zRet;
910 for(i=0; i<p->nCol; i++){
911 sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
912 z += sqlite3Strlen30(z);
913 }
914 assert( z[0]=='\0' && z>zRet );
915 z[-1] = '\0';
916 sqlite3_result_text(context, zRet, -1, sqlite3_free);
917 }
918 }
919 }
920 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
921 #ifndef SQLITE_DEBUG
922 UNUSED_PARAMETER( argc );
923 #endif
924 }
925 static const FuncDef statGetFuncdef = {
926 1+IsStat34, /* nArg */
927 SQLITE_UTF8, /* funcFlags */
928 0, /* pUserData */
929 0, /* pNext */
930 statGet, /* xFunc */
931 0, /* xStep */
932 0, /* xFinalize */
933 "stat_get", /* zName */
934 0, /* pHash */
935 0 /* pDestructor */
936 };
937
938 static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
939 assert( regOut!=regStat4 && regOut!=regStat4+1 );
940 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
941 sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
942 #elif SQLITE_DEBUG
943 assert( iParam==STAT_GET_STAT1 );
944 #else
945 UNUSED_PARAMETER( iParam );
946 #endif
947 sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4, regOut);
948 sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
949 sqlite3VdbeChangeP5(v, 1 + IsStat34);
950 }
951
952 /*
953 ** Generate code to do an analysis of all indices associated with
954 ** a single table.
955 */
956 static void analyzeOneTable(
957 Parse *pParse, /* Parser context */
958 Table *pTab, /* Table whose indices are to be analyzed */
959 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
960 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
961 int iMem, /* Available memory locations begin here */
962 int iTab /* Next available cursor */
963 ){
964 sqlite3 *db = pParse->db; /* Database handle */
965 Index *pIdx; /* An index to being analyzed */
966 int iIdxCur; /* Cursor open on index being analyzed */
967 int iTabCur; /* Table cursor */
968 Vdbe *v; /* The virtual machine being built up */
969 int i; /* Loop counter */
970 int jZeroRows = -1; /* Jump from here if number of rows is zero */
971 int iDb; /* Index of database containing pTab */
972 u8 needTableCnt = 1; /* True to count the table */
973 int regNewRowid = iMem++; /* Rowid for the inserted record */
974 int regStat4 = iMem++; /* Register to hold Stat4Accum object */
975 int regChng = iMem++; /* Index of changed index field */
976 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
977 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
978 #endif
979 int regTemp = iMem++; /* Temporary use register */
980 int regTabname = iMem++; /* Register containing table name */
981 int regIdxname = iMem++; /* Register containing index name */
982 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
983 int regPrev = iMem; /* MUST BE LAST (see below) */
984
985 pParse->nMem = MAX(pParse->nMem, iMem);
986 v = sqlite3GetVdbe(pParse);
987 if( v==0 || NEVER(pTab==0) ){
988 return;
989 }
990 if( pTab->tnum==0 ){
991 /* Do not gather statistics on views or virtual tables */
992 return;
993 }
994 if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){
995 /* Do not gather statistics on system tables */
996 return;
997 }
998 assert( sqlite3BtreeHoldsAllMutexes(db) );
999 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1000 assert( iDb>=0 );
1001 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1002 #ifndef SQLITE_OMIT_AUTHORIZATION
1003 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
1004 db->aDb[iDb].zName ) ){
1005 return;
1006 }
1007 #endif
1008
1009 /* Establish a read-lock on the table at the shared-cache level.
1010 ** Open a read-only cursor on the table. Also allocate a cursor number
1011 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
1012 ** this time though. */
1013 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
1014 iTabCur = iTab++;
1015 iIdxCur = iTab++;
1016 pParse->nTab = MAX(pParse->nTab, iTab);
1017 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
1018 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
1019
1020 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1021 int nCol; /* Number of columns in pIdx. "N" */
1022 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
1023 int addrNextRow; /* Address of "next_row:" */
1024 const char *zIdxName; /* Name of the index */
1025 int nColTest; /* Number of columns to test for changes */
1026
1027 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
1028 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
1029 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
1030 nCol = pIdx->nKeyCol;
1031 zIdxName = pTab->zName;
1032 nColTest = nCol - 1;
1033 }else{
1034 nCol = pIdx->nColumn;
1035 zIdxName = pIdx->zName;
1036 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
1037 }
1038
1039 /* Populate the register containing the index name. */
1040 sqlite3VdbeLoadString(v, regIdxname, zIdxName);
1041 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
1042
1043 /*
1044 ** Pseudo-code for loop that calls stat_push():
1045 **
1046 ** Rewind csr
1047 ** if eof(csr) goto end_of_scan;
1048 ** regChng = 0
1049 ** goto chng_addr_0;
1050 **
1051 ** next_row:
1052 ** regChng = 0
1053 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
1054 ** regChng = 1
1055 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
1056 ** ...
1057 ** regChng = N
1058 ** goto chng_addr_N
1059 **
1060 ** chng_addr_0:
1061 ** regPrev(0) = idx(0)
1062 ** chng_addr_1:
1063 ** regPrev(1) = idx(1)
1064 ** ...
1065 **
1066 ** endDistinctTest:
1067 ** regRowid = idx(rowid)
1068 ** stat_push(P, regChng, regRowid)
1069 ** Next csr
1070 ** if !eof(csr) goto next_row;
1071 **
1072 ** end_of_scan:
1073 */
1074
1075 /* Make sure there are enough memory cells allocated to accommodate
1076 ** the regPrev array and a trailing rowid (the rowid slot is required
1077 ** when building a record to insert into the sample column of
1078 ** the sqlite_stat4 table. */
1079 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
1080
1081 /* Open a read-only cursor on the index being analyzed. */
1082 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
1083 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
1084 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
1085 VdbeComment((v, "%s", pIdx->zName));
1086
1087 /* Invoke the stat_init() function. The arguments are:
1088 **
1089 ** (1) the number of columns in the index including the rowid
1090 ** (or for a WITHOUT ROWID table, the number of PK columns),
1091 ** (2) the number of columns in the key without the rowid/pk
1092 ** (3) the number of rows in the index,
1093 **
1094 **
1095 ** The third argument is only used for STAT3 and STAT4
1096 */
1097 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1098 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
1099 #endif
1100 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
1101 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
1102 sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4+1, regStat4);
1103 sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
1104 sqlite3VdbeChangeP5(v, 2+IsStat34);
1105
1106 /* Implementation of the following:
1107 **
1108 ** Rewind csr
1109 ** if eof(csr) goto end_of_scan;
1110 ** regChng = 0
1111 ** goto next_push_0;
1112 **
1113 */
1114 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
1115 VdbeCoverage(v);
1116 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
1117 addrNextRow = sqlite3VdbeCurrentAddr(v);
1118
1119 if( nColTest>0 ){
1120 int endDistinctTest = sqlite3VdbeMakeLabel(v);
1121 int *aGotoChng; /* Array of jump instruction addresses */
1122 aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*nColTest);
1123 if( aGotoChng==0 ) continue;
1124
1125 /*
1126 ** next_row:
1127 ** regChng = 0
1128 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
1129 ** regChng = 1
1130 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
1131 ** ...
1132 ** regChng = N
1133 ** goto endDistinctTest
1134 */
1135 sqlite3VdbeAddOp0(v, OP_Goto);
1136 addrNextRow = sqlite3VdbeCurrentAddr(v);
1137 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
1138 /* For a single-column UNIQUE index, once we have found a non-NULL
1139 ** row, we know that all the rest will be distinct, so skip
1140 ** subsequent distinctness tests. */
1141 sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
1142 VdbeCoverage(v);
1143 }
1144 for(i=0; i<nColTest; i++){
1145 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
1146 sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
1147 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
1148 aGotoChng[i] =
1149 sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
1150 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
1151 VdbeCoverage(v);
1152 }
1153 sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
1154 sqlite3VdbeGoto(v, endDistinctTest);
1155
1156
1157 /*
1158 ** chng_addr_0:
1159 ** regPrev(0) = idx(0)
1160 ** chng_addr_1:
1161 ** regPrev(1) = idx(1)
1162 ** ...
1163 */
1164 sqlite3VdbeJumpHere(v, addrNextRow-1);
1165 for(i=0; i<nColTest; i++){
1166 sqlite3VdbeJumpHere(v, aGotoChng[i]);
1167 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
1168 }
1169 sqlite3VdbeResolveLabel(v, endDistinctTest);
1170 sqlite3DbFree(db, aGotoChng);
1171 }
1172
1173 /*
1174 ** chng_addr_N:
1175 ** regRowid = idx(rowid) // STAT34 only
1176 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
1177 ** Next csr
1178 ** if !eof(csr) goto next_row;
1179 */
1180 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1181 assert( regRowid==(regStat4+2) );
1182 if( HasRowid(pTab) ){
1183 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
1184 }else{
1185 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
1186 int j, k, regKey;
1187 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
1188 for(j=0; j<pPk->nKeyCol; j++){
1189 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
1190 assert( k>=0 && k<pTab->nCol );
1191 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
1192 VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
1193 }
1194 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
1195 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
1196 }
1197 #endif
1198 assert( regChng==(regStat4+1) );
1199 sqlite3VdbeAddOp3(v, OP_Function0, 1, regStat4, regTemp);
1200 sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
1201 sqlite3VdbeChangeP5(v, 2+IsStat34);
1202 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
1203
1204 /* Add the entry to the stat1 table. */
1205 callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
1206 assert( "BBB"[0]==SQLITE_AFF_TEXT );
1207 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
1208 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
1209 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
1210 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
1211
1212 /* Add the entries to the stat3 or stat4 table. */
1213 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1214 {
1215 int regEq = regStat1;
1216 int regLt = regStat1+1;
1217 int regDLt = regStat1+2;
1218 int regSample = regStat1+3;
1219 int regCol = regStat1+4;
1220 int regSampleRowid = regCol + nCol;
1221 int addrNext;
1222 int addrIsNull;
1223 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
1224
1225 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
1226
1227 addrNext = sqlite3VdbeCurrentAddr(v);
1228 callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
1229 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
1230 VdbeCoverage(v);
1231 callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
1232 callStatGet(v, regStat4, STAT_GET_NLT, regLt);
1233 callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
1234 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
1235 /* We know that the regSampleRowid row exists because it was read by
1236 ** the previous loop. Thus the not-found jump of seekOp will never
1237 ** be taken */
1238 VdbeCoverageNeverTaken(v);
1239 #ifdef SQLITE_ENABLE_STAT3
1240 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample);
1241 #else
1242 for(i=0; i<nCol; i++){
1243 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
1244 }
1245 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
1246 #endif
1247 sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
1248 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
1249 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
1250 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
1251 sqlite3VdbeJumpHere(v, addrIsNull);
1252 }
1253 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
1254
1255 /* End of analysis */
1256 sqlite3VdbeJumpHere(v, addrRewind);
1257 }
1258
1259
1260 /* Create a single sqlite_stat1 entry containing NULL as the index
1261 ** name and the row count as the content.
1262 */
1263 if( pOnlyIdx==0 && needTableCnt ){
1264 VdbeComment((v, "%s", pTab->zName));
1265 sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
1266 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
1267 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
1268 assert( "BBB"[0]==SQLITE_AFF_TEXT );
1269 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
1270 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
1271 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
1272 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
1273 sqlite3VdbeJumpHere(v, jZeroRows);
1274 }
1275 }
1276
1277
1278 /*
1279 ** Generate code that will cause the most recent index analysis to
1280 ** be loaded into internal hash tables where is can be used.
1281 */
1282 static void loadAnalysis(Parse *pParse, int iDb){
1283 Vdbe *v = sqlite3GetVdbe(pParse);
1284 if( v ){
1285 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
1286 }
1287 }
1288
1289 /*
1290 ** Generate code that will do an analysis of an entire database
1291 */
1292 static void analyzeDatabase(Parse *pParse, int iDb){
1293 sqlite3 *db = pParse->db;
1294 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
1295 HashElem *k;
1296 int iStatCur;
1297 int iMem;
1298 int iTab;
1299
1300 sqlite3BeginWriteOperation(pParse, 0, iDb);
1301 iStatCur = pParse->nTab;
1302 pParse->nTab += 3;
1303 openStatTable(pParse, iDb, iStatCur, 0, 0);
1304 iMem = pParse->nMem+1;
1305 iTab = pParse->nTab;
1306 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1307 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
1308 Table *pTab = (Table*)sqliteHashData(k);
1309 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
1310 }
1311 loadAnalysis(pParse, iDb);
1312 }
1313
1314 /*
1315 ** Generate code that will do an analysis of a single table in
1316 ** a database. If pOnlyIdx is not NULL then it is a single index
1317 ** in pTab that should be analyzed.
1318 */
1319 static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
1320 int iDb;
1321 int iStatCur;
1322
1323 assert( pTab!=0 );
1324 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
1325 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1326 sqlite3BeginWriteOperation(pParse, 0, iDb);
1327 iStatCur = pParse->nTab;
1328 pParse->nTab += 3;
1329 if( pOnlyIdx ){
1330 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
1331 }else{
1332 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
1333 }
1334 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
1335 loadAnalysis(pParse, iDb);
1336 }
1337
1338 /*
1339 ** Generate code for the ANALYZE command. The parser calls this routine
1340 ** when it recognizes an ANALYZE command.
1341 **
1342 ** ANALYZE -- 1
1343 ** ANALYZE <database> -- 2
1344 ** ANALYZE ?<database>.?<tablename> -- 3
1345 **
1346 ** Form 1 causes all indices in all attached databases to be analyzed.
1347 ** Form 2 analyzes all indices the single database named.
1348 ** Form 3 analyzes all indices associated with the named table.
1349 */
1350 SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
1351 sqlite3 *db = pParse->db;
1352 int iDb;
1353 int i;
1354 char *z, *zDb;
1355 Table *pTab;
1356 Index *pIdx;
1357 Token *pTableName;
1358 Vdbe *v;
1359
1360 /* Read the database schema. If an error occurs, leave an error message
1361 ** and code in pParse and return NULL. */
1362 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
1363 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
1364 return;
1365 }
1366
1367 assert( pName2!=0 || pName1==0 );
1368 if( pName1==0 ){
1369 /* Form 1: Analyze everything */
1370 for(i=0; i<db->nDb; i++){
1371 if( i==1 ) continue; /* Do not analyze the TEMP database */
1372 analyzeDatabase(pParse, i);
1373 }
1374 }else if( pName2->n==0 ){
1375 /* Form 2: Analyze the database or table named */
1376 iDb = sqlite3FindDb(db, pName1);
1377 if( iDb>=0 ){
1378 analyzeDatabase(pParse, iDb);
1379 }else{
1380 z = sqlite3NameFromToken(db, pName1);
1381 if( z ){
1382 if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
1383 analyzeTable(pParse, pIdx->pTable, pIdx);
1384 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
1385 analyzeTable(pParse, pTab, 0);
1386 }
1387 sqlite3DbFree(db, z);
1388 }
1389 }
1390 }else{
1391 /* Form 3: Analyze the fully qualified table name */
1392 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
1393 if( iDb>=0 ){
1394 zDb = db->aDb[iDb].zName;
1395 z = sqlite3NameFromToken(db, pTableName);
1396 if( z ){
1397 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
1398 analyzeTable(pParse, pIdx->pTable, pIdx);
1399 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
1400 analyzeTable(pParse, pTab, 0);
1401 }
1402 sqlite3DbFree(db, z);
1403 }
1404 }
1405 }
1406 v = sqlite3GetVdbe(pParse);
1407 if( v ) sqlite3VdbeAddOp0(v, OP_Expire);
1408 }
1409
1410 /*
1411 ** Used to pass information from the analyzer reader through to the
1412 ** callback routine.
1413 */
1414 typedef struct analysisInfo analysisInfo;
1415 struct analysisInfo {
1416 sqlite3 *db;
1417 const char *zDatabase;
1418 };
1419
1420 /*
1421 ** The first argument points to a nul-terminated string containing a
1422 ** list of space separated integers. Read the first nOut of these into
1423 ** the array aOut[].
1424 */
1425 static void decodeIntArray(
1426 char *zIntArray, /* String containing int array to decode */
1427 int nOut, /* Number of slots in aOut[] */
1428 tRowcnt *aOut, /* Store integers here */
1429 LogEst *aLog, /* Or, if aOut==0, here */
1430 Index *pIndex /* Handle extra flags for this index, if not NULL */
1431 ){
1432 char *z = zIntArray;
1433 int c;
1434 int i;
1435 tRowcnt v;
1436
1437 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1438 if( z==0 ) z = "";
1439 #else
1440 assert( z!=0 );
1441 #endif
1442 for(i=0; *z && i<nOut; i++){
1443 v = 0;
1444 while( (c=z[0])>='0' && c<='9' ){
1445 v = v*10 + c - '0';
1446 z++;
1447 }
1448 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1449 if( aOut ) aOut[i] = v;
1450 if( aLog ) aLog[i] = sqlite3LogEst(v);
1451 #else
1452 assert( aOut==0 );
1453 UNUSED_PARAMETER(aOut);
1454 assert( aLog!=0 );
1455 aLog[i] = sqlite3LogEst(v);
1456 #endif
1457 if( *z==' ' ) z++;
1458 }
1459 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
1460 assert( pIndex!=0 ); {
1461 #else
1462 if( pIndex ){
1463 #endif
1464 pIndex->bUnordered = 0;
1465 pIndex->noSkipScan = 0;
1466 while( z[0] ){
1467 if( sqlite3_strglob("unordered*", z)==0 ){
1468 pIndex->bUnordered = 1;
1469 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
1470 pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
1471 }else if( sqlite3_strglob("noskipscan*", z)==0 ){
1472 pIndex->noSkipScan = 1;
1473 }
1474 #ifdef SQLITE_ENABLE_COSTMULT
1475 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
1476 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
1477 }
1478 #endif
1479 while( z[0]!=0 && z[0]!=' ' ) z++;
1480 while( z[0]==' ' ) z++;
1481 }
1482 }
1483 }
1484
1485 /*
1486 ** This callback is invoked once for each index when reading the
1487 ** sqlite_stat1 table.
1488 **
1489 ** argv[0] = name of the table
1490 ** argv[1] = name of the index (might be NULL)
1491 ** argv[2] = results of analysis - on integer for each column
1492 **
1493 ** Entries for which argv[1]==NULL simply record the number of rows in
1494 ** the table.
1495 */
1496 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
1497 analysisInfo *pInfo = (analysisInfo*)pData;
1498 Index *pIndex;
1499 Table *pTable;
1500 const char *z;
1501
1502 assert( argc==3 );
1503 UNUSED_PARAMETER2(NotUsed, argc);
1504
1505 if( argv==0 || argv[0]==0 || argv[2]==0 ){
1506 return 0;
1507 }
1508 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
1509 if( pTable==0 ){
1510 return 0;
1511 }
1512 if( argv[1]==0 ){
1513 pIndex = 0;
1514 }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
1515 pIndex = sqlite3PrimaryKeyIndex(pTable);
1516 }else{
1517 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
1518 }
1519 z = argv[2];
1520
1521 if( pIndex ){
1522 tRowcnt *aiRowEst = 0;
1523 int nCol = pIndex->nKeyCol+1;
1524 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1525 /* Index.aiRowEst may already be set here if there are duplicate
1526 ** sqlite_stat1 entries for this index. In that case just clobber
1527 ** the old data with the new instead of allocating a new array. */
1528 if( pIndex->aiRowEst==0 ){
1529 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
1530 if( pIndex->aiRowEst==0 ) pInfo->db->mallocFailed = 1;
1531 }
1532 aiRowEst = pIndex->aiRowEst;
1533 #endif
1534 pIndex->bUnordered = 0;
1535 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
1536 if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
1537 }else{
1538 Index fakeIdx;
1539 fakeIdx.szIdxRow = pTable->szTabRow;
1540 #ifdef SQLITE_ENABLE_COSTMULT
1541 fakeIdx.pTable = pTable;
1542 #endif
1543 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
1544 pTable->szTabRow = fakeIdx.szIdxRow;
1545 }
1546
1547 return 0;
1548 }
1549
1550 /*
1551 ** If the Index.aSample variable is not NULL, delete the aSample[] array
1552 ** and its contents.
1553 */
1554 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
1555 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1556 if( pIdx->aSample ){
1557 int j;
1558 for(j=0; j<pIdx->nSample; j++){
1559 IndexSample *p = &pIdx->aSample[j];
1560 sqlite3DbFree(db, p->p);
1561 }
1562 sqlite3DbFree(db, pIdx->aSample);
1563 }
1564 if( db && db->pnBytesFreed==0 ){
1565 pIdx->nSample = 0;
1566 pIdx->aSample = 0;
1567 }
1568 #else
1569 UNUSED_PARAMETER(db);
1570 UNUSED_PARAMETER(pIdx);
1571 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
1572 }
1573
1574 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1575 /*
1576 ** Populate the pIdx->aAvgEq[] array based on the samples currently
1577 ** stored in pIdx->aSample[].
1578 */
1579 static void initAvgEq(Index *pIdx){
1580 if( pIdx ){
1581 IndexSample *aSample = pIdx->aSample;
1582 IndexSample *pFinal = &aSample[pIdx->nSample-1];
1583 int iCol;
1584 int nCol = 1;
1585 if( pIdx->nSampleCol>1 ){
1586 /* If this is stat4 data, then calculate aAvgEq[] values for all
1587 ** sample columns except the last. The last is always set to 1, as
1588 ** once the trailing PK fields are considered all index keys are
1589 ** unique. */
1590 nCol = pIdx->nSampleCol-1;
1591 pIdx->aAvgEq[nCol] = 1;
1592 }
1593 for(iCol=0; iCol<nCol; iCol++){
1594 int nSample = pIdx->nSample;
1595 int i; /* Used to iterate through samples */
1596 tRowcnt sumEq = 0; /* Sum of the nEq values */
1597 tRowcnt avgEq = 0;
1598 tRowcnt nRow; /* Number of rows in index */
1599 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
1600 i64 nDist100; /* Number of distinct values in index */
1601
1602 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
1603 nRow = pFinal->anLt[iCol];
1604 nDist100 = (i64)100 * pFinal->anDLt[iCol];
1605 nSample--;
1606 }else{
1607 nRow = pIdx->aiRowEst[0];
1608 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
1609 }
1610 pIdx->nRowEst0 = nRow;
1611
1612 /* Set nSum to the number of distinct (iCol+1) field prefixes that
1613 ** occur in the stat4 table for this index. Set sumEq to the sum of
1614 ** the nEq values for column iCol for the same set (adding the value
1615 ** only once where there exist duplicate prefixes). */
1616 for(i=0; i<nSample; i++){
1617 if( i==(pIdx->nSample-1)
1618 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
1619 ){
1620 sumEq += aSample[i].anEq[iCol];
1621 nSum100 += 100;
1622 }
1623 }
1624
1625 if( nDist100>nSum100 ){
1626 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
1627 }
1628 if( avgEq==0 ) avgEq = 1;
1629 pIdx->aAvgEq[iCol] = avgEq;
1630 }
1631 }
1632 }
1633
1634 /*
1635 ** Look up an index by name. Or, if the name of a WITHOUT ROWID table
1636 ** is supplied instead, find the PRIMARY KEY index for that table.
1637 */
1638 static Index *findIndexOrPrimaryKey(
1639 sqlite3 *db,
1640 const char *zName,
1641 const char *zDb
1642 ){
1643 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
1644 if( pIdx==0 ){
1645 Table *pTab = sqlite3FindTable(db, zName, zDb);
1646 if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
1647 }
1648 return pIdx;
1649 }
1650
1651 /*
1652 ** Load the content from either the sqlite_stat4 or sqlite_stat3 table
1653 ** into the relevant Index.aSample[] arrays.
1654 **
1655 ** Arguments zSql1 and zSql2 must point to SQL statements that return
1656 ** data equivalent to the following (statements are different for stat3,
1657 ** see the caller of this function for details):
1658 **
1659 ** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
1660 ** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
1661 **
1662 ** where %Q is replaced with the database name before the SQL is executed.
1663 */
1664 static int loadStatTbl(
1665 sqlite3 *db, /* Database handle */
1666 int bStat3, /* Assume single column records only */
1667 const char *zSql1, /* SQL statement 1 (see above) */
1668 const char *zSql2, /* SQL statement 2 (see above) */
1669 const char *zDb /* Database name (e.g. "main") */
1670 ){
1671 int rc; /* Result codes from subroutines */
1672 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
1673 char *zSql; /* Text of the SQL statement */
1674 Index *pPrevIdx = 0; /* Previous index in the loop */
1675 IndexSample *pSample; /* A slot in pIdx->aSample[] */
1676
1677 assert( db->lookaside.bEnabled==0 );
1678 zSql = sqlite3MPrintf(db, zSql1, zDb);
1679 if( !zSql ){
1680 return SQLITE_NOMEM;
1681 }
1682 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
1683 sqlite3DbFree(db, zSql);
1684 if( rc ) return rc;
1685
1686 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1687 int nIdxCol = 1; /* Number of columns in stat4 records */
1688
1689 char *zIndex; /* Index name */
1690 Index *pIdx; /* Pointer to the index object */
1691 int nSample; /* Number of samples */
1692 int nByte; /* Bytes of space required */
1693 int i; /* Bytes of space required */
1694 tRowcnt *pSpace;
1695
1696 zIndex = (char *)sqlite3_column_text(pStmt, 0);
1697 if( zIndex==0 ) continue;
1698 nSample = sqlite3_column_int(pStmt, 1);
1699 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
1700 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
1701 /* Index.nSample is non-zero at this point if data has already been
1702 ** loaded from the stat4 table. In this case ignore stat3 data. */
1703 if( pIdx==0 || pIdx->nSample ) continue;
1704 if( bStat3==0 ){
1705 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
1706 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
1707 nIdxCol = pIdx->nKeyCol;
1708 }else{
1709 nIdxCol = pIdx->nColumn;
1710 }
1711 }
1712 pIdx->nSampleCol = nIdxCol;
1713 nByte = sizeof(IndexSample) * nSample;
1714 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
1715 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
1716
1717 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
1718 if( pIdx->aSample==0 ){
1719 sqlite3_finalize(pStmt);
1720 return SQLITE_NOMEM;
1721 }
1722 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
1723 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
1724 for(i=0; i<nSample; i++){
1725 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
1726 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
1727 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
1728 }
1729 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
1730 }
1731 rc = sqlite3_finalize(pStmt);
1732 if( rc ) return rc;
1733
1734 zSql = sqlite3MPrintf(db, zSql2, zDb);
1735 if( !zSql ){
1736 return SQLITE_NOMEM;
1737 }
1738 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
1739 sqlite3DbFree(db, zSql);
1740 if( rc ) return rc;
1741
1742 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1743 char *zIndex; /* Index name */
1744 Index *pIdx; /* Pointer to the index object */
1745 int nCol = 1; /* Number of columns in index */
1746
1747 zIndex = (char *)sqlite3_column_text(pStmt, 0);
1748 if( zIndex==0 ) continue;
1749 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
1750 if( pIdx==0 ) continue;
1751 /* This next condition is true if data has already been loaded from
1752 ** the sqlite_stat4 table. In this case ignore stat3 data. */
1753 nCol = pIdx->nSampleCol;
1754 if( bStat3 && nCol>1 ) continue;
1755 if( pIdx!=pPrevIdx ){
1756 initAvgEq(pPrevIdx);
1757 pPrevIdx = pIdx;
1758 }
1759 pSample = &pIdx->aSample[pIdx->nSample];
1760 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
1761 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
1762 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
1763
1764 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
1765 ** This is in case the sample record is corrupted. In that case, the
1766 ** sqlite3VdbeRecordCompare() may read up to two varints past the
1767 ** end of the allocated buffer before it realizes it is dealing with
1768 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
1769 ** a buffer overread. */
1770 pSample->n = sqlite3_column_bytes(pStmt, 4);
1771 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
1772 if( pSample->p==0 ){
1773 sqlite3_finalize(pStmt);
1774 return SQLITE_NOMEM;
1775 }
1776 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
1777 pIdx->nSample++;
1778 }
1779 rc = sqlite3_finalize(pStmt);
1780 if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
1781 return rc;
1782 }
1783
1784 /*
1785 ** Load content from the sqlite_stat4 and sqlite_stat3 tables into
1786 ** the Index.aSample[] arrays of all indices.
1787 */
1788 static int loadStat4(sqlite3 *db, const char *zDb){
1789 int rc = SQLITE_OK; /* Result codes from subroutines */
1790
1791 assert( db->lookaside.bEnabled==0 );
1792 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
1793 rc = loadStatTbl(db, 0,
1794 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
1795 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
1796 zDb
1797 );
1798 }
1799
1800 if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
1801 rc = loadStatTbl(db, 1,
1802 "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
1803 "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
1804 zDb
1805 );
1806 }
1807
1808 return rc;
1809 }
1810 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
1811
1812 /*
1813 ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
1814 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
1815 ** arrays. The contents of sqlite_stat3/4 are used to populate the
1816 ** Index.aSample[] arrays.
1817 **
1818 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
1819 ** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
1820 ** during compilation and the sqlite_stat3/4 table is present, no data is
1821 ** read from it.
1822 **
1823 ** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
1824 ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
1825 ** returned. However, in this case, data is read from the sqlite_stat1
1826 ** table (if it is present) before returning.
1827 **
1828 ** If an OOM error occurs, this function always sets db->mallocFailed.
1829 ** This means if the caller does not care about other errors, the return
1830 ** code may be ignored.
1831 */
1832 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
1833 analysisInfo sInfo;
1834 HashElem *i;
1835 char *zSql;
1836 int rc;
1837
1838 assert( iDb>=0 && iDb<db->nDb );
1839 assert( db->aDb[iDb].pBt!=0 );
1840
1841 /* Clear any prior statistics */
1842 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1843 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
1844 Index *pIdx = sqliteHashData(i);
1845 sqlite3DefaultRowEst(pIdx);
1846 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1847 sqlite3DeleteIndexSamples(db, pIdx);
1848 pIdx->aSample = 0;
1849 #endif
1850 }
1851
1852 /* Check to make sure the sqlite_stat1 table exists */
1853 sInfo.db = db;
1854 sInfo.zDatabase = db->aDb[iDb].zName;
1855 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
1856 return SQLITE_ERROR;
1857 }
1858
1859 /* Load new statistics out of the sqlite_stat1 table */
1860 zSql = sqlite3MPrintf(db,
1861 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
1862 if( zSql==0 ){
1863 rc = SQLITE_NOMEM;
1864 }else{
1865 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
1866 sqlite3DbFree(db, zSql);
1867 }
1868
1869
1870 /* Load the statistics from the sqlite_stat4 table. */
1871 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1872 if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
1873 int lookasideEnabled = db->lookaside.bEnabled;
1874 db->lookaside.bEnabled = 0;
1875 rc = loadStat4(db, sInfo.zDatabase);
1876 db->lookaside.bEnabled = lookasideEnabled;
1877 }
1878 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
1879 Index *pIdx = sqliteHashData(i);
1880 sqlite3_free(pIdx->aiRowEst);
1881 pIdx->aiRowEst = 0;
1882 }
1883 #endif
1884
1885 if( rc==SQLITE_NOMEM ){
1886 db->mallocFailed = 1;
1887 }
1888 return rc;
1889 }
1890
1891
1892 #endif /* SQLITE_OMIT_ANALYZE */
1893
1894 /************** End of analyze.c *********************************************/
1895 /************** Begin file attach.c ******************************************/
1896 /*
1897 ** 2003 April 6
1898 **
1899 ** The author disclaims copyright to this source code. In place of
1900 ** a legal notice, here is a blessing:
1901 **
1902 ** May you do good and not evil.
1903 ** May you find forgiveness for yourself and forgive others.
1904 ** May you share freely, never taking more than you give.
1905 **
1906 *************************************************************************
1907 ** This file contains code used to implement the ATTACH and DETACH commands.
1908 */
1909 /* #include "sqliteInt.h" */
1910
1911 #ifndef SQLITE_OMIT_ATTACH
1912 /*
1913 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
1914 ** is slightly different from resolving a normal SQL expression, because simple
1915 ** identifiers are treated as strings, not possible column names or aliases.
1916 **
1917 ** i.e. if the parser sees:
1918 **
1919 ** ATTACH DATABASE abc AS def
1920 **
1921 ** it treats the two expressions as literal strings 'abc' and 'def' instead of
1922 ** looking for columns of the same name.
1923 **
1924 ** This only applies to the root node of pExpr, so the statement:
1925 **
1926 ** ATTACH DATABASE abc||def AS 'db2'
1927 **
1928 ** will fail because neither abc or def can be resolved.
1929 */
1930 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
1931 {
1932 int rc = SQLITE_OK;
1933 if( pExpr ){
1934 if( pExpr->op!=TK_ID ){
1935 rc = sqlite3ResolveExprNames(pName, pExpr);
1936 }else{
1937 pExpr->op = TK_STRING;
1938 }
1939 }
1940 return rc;
1941 }
1942
1943 /*
1944 ** An SQL user-function registered to do the work of an ATTACH statement. The
1945 ** three arguments to the function come directly from an attach statement:
1946 **
1947 ** ATTACH DATABASE x AS y KEY z
1948 **
1949 ** SELECT sqlite_attach(x, y, z)
1950 **
1951 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
1952 ** third argument.
1953 */
1954 static void attachFunc(
1955 sqlite3_context *context,
1956 int NotUsed,
1957 sqlite3_value **argv
1958 ){
1959 int i;
1960 int rc = 0;
1961 sqlite3 *db = sqlite3_context_db_handle(context);
1962 const char *zName;
1963 const char *zFile;
1964 char *zPath = 0;
1965 char *zErr = 0;
1966 unsigned int flags;
1967 Db *aNew;
1968 char *zErrDyn = 0;
1969 sqlite3_vfs *pVfs;
1970
1971 UNUSED_PARAMETER(NotUsed);
1972
1973 zFile = (const char *)sqlite3_value_text(argv[0]);
1974 zName = (const char *)sqlite3_value_text(argv[1]);
1975 if( zFile==0 ) zFile = "";
1976 if( zName==0 ) zName = "";
1977
1978 /* Check for the following errors:
1979 **
1980 ** * Too many attached databases,
1981 ** * Transaction currently open
1982 ** * Specified database name already being used.
1983 */
1984 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
1985 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
1986 db->aLimit[SQLITE_LIMIT_ATTACHED]
1987 );
1988 goto attach_error;
1989 }
1990 if( !db->autoCommit ){
1991 zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
1992 goto attach_error;
1993 }
1994 for(i=0; i<db->nDb; i++){
1995 char *z = db->aDb[i].zName;
1996 assert( z && zName );
1997 if( sqlite3StrICmp(z, zName)==0 ){
1998 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
1999 goto attach_error;
2000 }
2001 }
2002
2003 /* Allocate the new entry in the db->aDb[] array and initialize the schema
2004 ** hash tables.
2005 */
2006 if( db->aDb==db->aDbStatic ){
2007 aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
2008 if( aNew==0 ) return;
2009 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
2010 }else{
2011 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
2012 if( aNew==0 ) return;
2013 }
2014 db->aDb = aNew;
2015 aNew = &db->aDb[db->nDb];
2016 memset(aNew, 0, sizeof(*aNew));
2017
2018 /* Open the database file. If the btree is successfully opened, use
2019 ** it to obtain the database schema. At this point the schema may
2020 ** or may not be initialized.
2021 */
2022 flags = db->openFlags;
2023 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
2024 if( rc!=SQLITE_OK ){
2025 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
2026 sqlite3_result_error(context, zErr, -1);
2027 sqlite3_free(zErr);
2028 return;
2029 }
2030 assert( pVfs );
2031 flags |= SQLITE_OPEN_MAIN_DB;
2032 rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
2033 sqlite3_free( zPath );
2034 db->nDb++;
2035 if( rc==SQLITE_CONSTRAINT ){
2036 rc = SQLITE_ERROR;
2037 zErrDyn = sqlite3MPrintf(db, "database is already attached");
2038 }else if( rc==SQLITE_OK ){
2039 Pager *pPager;
2040 aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
2041 if( !aNew->pSchema ){
2042 rc = SQLITE_NOMEM;
2043 }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
2044 zErrDyn = sqlite3MPrintf(db,
2045 "attached databases must use the same text encoding as main database");
2046 rc = SQLITE_ERROR;
2047 }
2048 sqlite3BtreeEnter(aNew->pBt);
2049 pPager = sqlite3BtreePager(aNew->pBt);
2050 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
2051 sqlite3BtreeSecureDelete(aNew->pBt,
2052 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
2053 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
2054 sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
2055 #endif
2056 sqlite3BtreeLeave(aNew->pBt);
2057 }
2058 aNew->safety_level = 3;
2059 aNew->zName = sqlite3DbStrDup(db, zName);
2060 if( rc==SQLITE_OK && aNew->zName==0 ){
2061 rc = SQLITE_NOMEM;
2062 }
2063
2064
2065 #ifdef SQLITE_HAS_CODEC
2066 if( rc==SQLITE_OK ){
2067 extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
2068 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
2069 int nKey;
2070 char *zKey;
2071 int t = sqlite3_value_type(argv[2]);
2072 switch( t ){
2073 case SQLITE_INTEGER:
2074 case SQLITE_FLOAT:
2075 zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
2076 rc = SQLITE_ERROR;
2077 break;
2078
2079 case SQLITE_TEXT:
2080 case SQLITE_BLOB:
2081 nKey = sqlite3_value_bytes(argv[2]);
2082 zKey = (char *)sqlite3_value_blob(argv[2]);
2083 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
2084 break;
2085
2086 case SQLITE_NULL:
2087 /* No key specified. Use the key from the main database */
2088 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
2089 if( nKey>0 || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
2090 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
2091 }
2092 break;
2093 }
2094 }
2095 #endif
2096
2097 /* If the file was opened successfully, read the schema for the new database.
2098 ** If this fails, or if opening the file failed, then close the file and
2099 ** remove the entry from the db->aDb[] array. i.e. put everything back the way
2100 ** we found it.
2101 */
2102 if( rc==SQLITE_OK ){
2103 sqlite3BtreeEnterAll(db);
2104 rc = sqlite3Init(db, &zErrDyn);
2105 sqlite3BtreeLeaveAll(db);
2106 }
2107 #ifdef SQLITE_USER_AUTHENTICATION
2108 if( rc==SQLITE_OK ){
2109 u8 newAuth = 0;
2110 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
2111 if( newAuth<db->auth.authLevel ){
2112 rc = SQLITE_AUTH_USER;
2113 }
2114 }
2115 #endif
2116 if( rc ){
2117 int iDb = db->nDb - 1;
2118 assert( iDb>=2 );
2119 if( db->aDb[iDb].pBt ){
2120 sqlite3BtreeClose(db->aDb[iDb].pBt);
2121 db->aDb[iDb].pBt = 0;
2122 db->aDb[iDb].pSchema = 0;
2123 }
2124 sqlite3ResetAllSchemasOfConnection(db);
2125 db->nDb = iDb;
2126 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
2127 db->mallocFailed = 1;
2128 sqlite3DbFree(db, zErrDyn);
2129 zErrDyn = sqlite3MPrintf(db, "out of memory");
2130 }else if( zErrDyn==0 ){
2131 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
2132 }
2133 goto attach_error;
2134 }
2135
2136 return;
2137
2138 attach_error:
2139 /* Return an error if we get here */
2140 if( zErrDyn ){
2141 sqlite3_result_error(context, zErrDyn, -1);
2142 sqlite3DbFree(db, zErrDyn);
2143 }
2144 if( rc ) sqlite3_result_error_code(context, rc);
2145 }
2146
2147 /*
2148 ** An SQL user-function registered to do the work of an DETACH statement. The
2149 ** three arguments to the function come directly from a detach statement:
2150 **
2151 ** DETACH DATABASE x
2152 **
2153 ** SELECT sqlite_detach(x)
2154 */
2155 static void detachFunc(
2156 sqlite3_context *context,
2157 int NotUsed,
2158 sqlite3_value **argv
2159 ){
2160 const char *zName = (const char *)sqlite3_value_text(argv[0]);
2161 sqlite3 *db = sqlite3_context_db_handle(context);
2162 int i;
2163 Db *pDb = 0;
2164 char zErr[128];
2165
2166 UNUSED_PARAMETER(NotUsed);
2167
2168 if( zName==0 ) zName = "";
2169 for(i=0; i<db->nDb; i++){
2170 pDb = &db->aDb[i];
2171 if( pDb->pBt==0 ) continue;
2172 if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
2173 }
2174
2175 if( i>=db->nDb ){
2176 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
2177 goto detach_error;
2178 }
2179 if( i<2 ){
2180 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
2181 goto detach_error;
2182 }
2183 if( !db->autoCommit ){
2184 sqlite3_snprintf(sizeof(zErr), zErr,
2185 "cannot DETACH database within transaction");
2186 goto detach_error;
2187 }
2188 if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
2189 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
2190 goto detach_error;
2191 }
2192
2193 sqlite3BtreeClose(pDb->pBt);
2194 pDb->pBt = 0;
2195 pDb->pSchema = 0;
2196 sqlite3CollapseDatabaseArray(db);
2197 return;
2198
2199 detach_error:
2200 sqlite3_result_error(context, zErr, -1);
2201 }
2202
2203 /*
2204 ** This procedure generates VDBE code for a single invocation of either the
2205 ** sqlite_detach() or sqlite_attach() SQL user functions.
2206 */
2207 static void codeAttach(
2208 Parse *pParse, /* The parser context */
2209 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
2210 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
2211 Expr *pAuthArg, /* Expression to pass to authorization callback */
2212 Expr *pFilename, /* Name of database file */
2213 Expr *pDbname, /* Name of the database to use internally */
2214 Expr *pKey /* Database key for encryption extension */
2215 ){
2216 int rc;
2217 NameContext sName;
2218 Vdbe *v;
2219 sqlite3* db = pParse->db;
2220 int regArgs;
2221
2222 memset(&sName, 0, sizeof(NameContext));
2223 sName.pParse = pParse;
2224
2225 if(
2226 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
2227 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
2228 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
2229 ){
2230 goto attach_end;
2231 }
2232
2233 #ifndef SQLITE_OMIT_AUTHORIZATION
2234 if( pAuthArg ){
2235 char *zAuthArg;
2236 if( pAuthArg->op==TK_STRING ){
2237 zAuthArg = pAuthArg->u.zToken;
2238 }else{
2239 zAuthArg = 0;
2240 }
2241 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
2242 if(rc!=SQLITE_OK ){
2243 goto attach_end;
2244 }
2245 }
2246 #endif /* SQLITE_OMIT_AUTHORIZATION */
2247
2248
2249 v = sqlite3GetVdbe(pParse);
2250 regArgs = sqlite3GetTempRange(pParse, 4);
2251 sqlite3ExprCode(pParse, pFilename, regArgs);
2252 sqlite3ExprCode(pParse, pDbname, regArgs+1);
2253 sqlite3ExprCode(pParse, pKey, regArgs+2);
2254
2255 assert( v || db->mallocFailed );
2256 if( v ){
2257 sqlite3VdbeAddOp3(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3);
2258 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
2259 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
2260 sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
2261
2262 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
2263 ** statement only). For DETACH, set it to false (expire all existing
2264 ** statements).
2265 */
2266 sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
2267 }
2268
2269 attach_end:
2270 sqlite3ExprDelete(db, pFilename);
2271 sqlite3ExprDelete(db, pDbname);
2272 sqlite3ExprDelete(db, pKey);
2273 }
2274
2275 /*
2276 ** Called by the parser to compile a DETACH statement.
2277 **
2278 ** DETACH pDbname
2279 */
2280 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
2281 static const FuncDef detach_func = {
2282 1, /* nArg */
2283 SQLITE_UTF8, /* funcFlags */
2284 0, /* pUserData */
2285 0, /* pNext */
2286 detachFunc, /* xFunc */
2287 0, /* xStep */
2288 0, /* xFinalize */
2289 "sqlite_detach", /* zName */
2290 0, /* pHash */
2291 0 /* pDestructor */
2292 };
2293 codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
2294 }
2295
2296 /*
2297 ** Called by the parser to compile an ATTACH statement.
2298 **
2299 ** ATTACH p AS pDbname KEY pKey
2300 */
2301 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *p Key){
2302 static const FuncDef attach_func = {
2303 3, /* nArg */
2304 SQLITE_UTF8, /* funcFlags */
2305 0, /* pUserData */
2306 0, /* pNext */
2307 attachFunc, /* xFunc */
2308 0, /* xStep */
2309 0, /* xFinalize */
2310 "sqlite_attach", /* zName */
2311 0, /* pHash */
2312 0 /* pDestructor */
2313 };
2314 codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
2315 }
2316 #endif /* SQLITE_OMIT_ATTACH */
2317
2318 /*
2319 ** Initialize a DbFixer structure. This routine must be called prior
2320 ** to passing the structure to one of the sqliteFixAAAA() routines below.
2321 */
2322 SQLITE_PRIVATE void sqlite3FixInit(
2323 DbFixer *pFix, /* The fixer to be initialized */
2324 Parse *pParse, /* Error messages will be written here */
2325 int iDb, /* This is the database that must be used */
2326 const char *zType, /* "view", "trigger", or "index" */
2327 const Token *pName /* Name of the view, trigger, or index */
2328 ){
2329 sqlite3 *db;
2330
2331 db = pParse->db;
2332 assert( db->nDb>iDb );
2333 pFix->pParse = pParse;
2334 pFix->zDb = db->aDb[iDb].zName;
2335 pFix->pSchema = db->aDb[iDb].pSchema;
2336 pFix->zType = zType;
2337 pFix->pName = pName;
2338 pFix->bVarOnly = (iDb==1);
2339 }
2340
2341 /*
2342 ** The following set of routines walk through the parse tree and assign
2343 ** a specific database to all table references where the database name
2344 ** was left unspecified in the original SQL statement. The pFix structure
2345 ** must have been initialized by a prior call to sqlite3FixInit().
2346 **
2347 ** These routines are used to make sure that an index, trigger, or
2348 ** view in one database does not refer to objects in a different database.
2349 ** (Exception: indices, triggers, and views in the TEMP database are
2350 ** allowed to refer to anything.) If a reference is explicitly made
2351 ** to an object in a different database, an error message is added to
2352 ** pParse->zErrMsg and these routines return non-zero. If everything
2353 ** checks out, these routines return 0.
2354 */
2355 SQLITE_PRIVATE int sqlite3FixSrcList(
2356 DbFixer *pFix, /* Context of the fixation */
2357 SrcList *pList /* The Source list to check and modify */
2358 ){
2359 int i;
2360 const char *zDb;
2361 struct SrcList_item *pItem;
2362
2363 if( NEVER(pList==0) ) return 0;
2364 zDb = pFix->zDb;
2365 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
2366 if( pFix->bVarOnly==0 ){
2367 if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
2368 sqlite3ErrorMsg(pFix->pParse,
2369 "%s %T cannot reference objects in database %s",
2370 pFix->zType, pFix->pName, pItem->zDatabase);
2371 return 1;
2372 }
2373 sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
2374 pItem->zDatabase = 0;
2375 pItem->pSchema = pFix->pSchema;
2376 }
2377 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
2378 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
2379 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
2380 #endif
2381 }
2382 return 0;
2383 }
2384 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
2385 SQLITE_PRIVATE int sqlite3FixSelect(
2386 DbFixer *pFix, /* Context of the fixation */
2387 Select *pSelect /* The SELECT statement to be fixed to one database */
2388 ){
2389 while( pSelect ){
2390 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
2391 return 1;
2392 }
2393 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
2394 return 1;
2395 }
2396 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
2397 return 1;
2398 }
2399 if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
2400 return 1;
2401 }
2402 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
2403 return 1;
2404 }
2405 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
2406 return 1;
2407 }
2408 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
2409 return 1;
2410 }
2411 if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
2412 return 1;
2413 }
2414 pSelect = pSelect->pPrior;
2415 }
2416 return 0;
2417 }
2418 SQLITE_PRIVATE int sqlite3FixExpr(
2419 DbFixer *pFix, /* Context of the fixation */
2420 Expr *pExpr /* The expression to be fixed to one database */
2421 ){
2422 while( pExpr ){
2423 if( pExpr->op==TK_VARIABLE ){
2424 if( pFix->pParse->db->init.busy ){
2425 pExpr->op = TK_NULL;
2426 }else{
2427 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
2428 return 1;
2429 }
2430 }
2431 if( ExprHasProperty(pExpr, EP_TokenOnly) ) break;
2432 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
2433 if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
2434 }else{
2435 if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
2436 }
2437 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
2438 return 1;
2439 }
2440 pExpr = pExpr->pLeft;
2441 }
2442 return 0;
2443 }
2444 SQLITE_PRIVATE int sqlite3FixExprList(
2445 DbFixer *pFix, /* Context of the fixation */
2446 ExprList *pList /* The expression to be fixed to one database */
2447 ){
2448 int i;
2449 struct ExprList_item *pItem;
2450 if( pList==0 ) return 0;
2451 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
2452 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
2453 return 1;
2454 }
2455 }
2456 return 0;
2457 }
2458 #endif
2459
2460 #ifndef SQLITE_OMIT_TRIGGER
2461 SQLITE_PRIVATE int sqlite3FixTriggerStep(
2462 DbFixer *pFix, /* Context of the fixation */
2463 TriggerStep *pStep /* The trigger step be fixed to one database */
2464 ){
2465 while( pStep ){
2466 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
2467 return 1;
2468 }
2469 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
2470 return 1;
2471 }
2472 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
2473 return 1;
2474 }
2475 pStep = pStep->pNext;
2476 }
2477 return 0;
2478 }
2479 #endif
2480
2481 /************** End of attach.c **********************************************/
2482 /************** Begin file auth.c ********************************************/
2483 /*
2484 ** 2003 January 11
2485 **
2486 ** The author disclaims copyright to this source code. In place of
2487 ** a legal notice, here is a blessing:
2488 **
2489 ** May you do good and not evil.
2490 ** May you find forgiveness for yourself and forgive others.
2491 ** May you share freely, never taking more than you give.
2492 **
2493 *************************************************************************
2494 ** This file contains code used to implement the sqlite3_set_authorizer()
2495 ** API. This facility is an optional feature of the library. Embedded
2496 ** systems that do not need this facility may omit it by recompiling
2497 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
2498 */
2499 /* #include "sqliteInt.h" */
2500
2501 /*
2502 ** All of the code in this file may be omitted by defining a single
2503 ** macro.
2504 */
2505 #ifndef SQLITE_OMIT_AUTHORIZATION
2506
2507 /*
2508 ** Set or clear the access authorization function.
2509 **
2510 ** The access authorization function is be called during the compilation
2511 ** phase to verify that the user has read and/or write access permission on
2512 ** various fields of the database. The first argument to the auth function
2513 ** is a copy of the 3rd argument to this routine. The second argument
2514 ** to the auth function is one of these constants:
2515 **
2516 ** SQLITE_CREATE_INDEX
2517 ** SQLITE_CREATE_TABLE
2518 ** SQLITE_CREATE_TEMP_INDEX
2519 ** SQLITE_CREATE_TEMP_TABLE
2520 ** SQLITE_CREATE_TEMP_TRIGGER
2521 ** SQLITE_CREATE_TEMP_VIEW
2522 ** SQLITE_CREATE_TRIGGER
2523 ** SQLITE_CREATE_VIEW
2524 ** SQLITE_DELETE
2525 ** SQLITE_DROP_INDEX
2526 ** SQLITE_DROP_TABLE
2527 ** SQLITE_DROP_TEMP_INDEX
2528 ** SQLITE_DROP_TEMP_TABLE
2529 ** SQLITE_DROP_TEMP_TRIGGER
2530 ** SQLITE_DROP_TEMP_VIEW
2531 ** SQLITE_DROP_TRIGGER
2532 ** SQLITE_DROP_VIEW
2533 ** SQLITE_INSERT
2534 ** SQLITE_PRAGMA
2535 ** SQLITE_READ
2536 ** SQLITE_SELECT
2537 ** SQLITE_TRANSACTION
2538 ** SQLITE_UPDATE
2539 **
2540 ** The third and fourth arguments to the auth function are the name of
2541 ** the table and the column that are being accessed. The auth function
2542 ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
2543 ** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
2544 ** means that the SQL statement will never-run - the sqlite3_exec() call
2545 ** will return with an error. SQLITE_IGNORE means that the SQL statement
2546 ** should run but attempts to read the specified column will return NULL
2547 ** and attempts to write the column will be ignored.
2548 **
2549 ** Setting the auth function to NULL disables this hook. The default
2550 ** setting of the auth function is NULL.
2551 */
2552 SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
2553 sqlite3 *db,
2554 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
2555 void *pArg
2556 ){
2557 #ifdef SQLITE_ENABLE_API_ARMOR
2558 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
2559 #endif
2560 sqlite3_mutex_enter(db->mutex);
2561 db->xAuth = (sqlite3_xauth)xAuth;
2562 db->pAuthArg = pArg;
2563 sqlite3ExpirePreparedStatements(db);
2564 sqlite3_mutex_leave(db->mutex);
2565 return SQLITE_OK;
2566 }
2567
2568 /*
2569 ** Write an error message into pParse->zErrMsg that explains that the
2570 ** user-supplied authorization function returned an illegal value.
2571 */
2572 static void sqliteAuthBadReturnCode(Parse *pParse){
2573 sqlite3ErrorMsg(pParse, "authorizer malfunction");
2574 pParse->rc = SQLITE_ERROR;
2575 }
2576
2577 /*
2578 ** Invoke the authorization callback for permission to read column zCol from
2579 ** table zTab in database zDb. This function assumes that an authorization
2580 ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
2581 **
2582 ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
2583 ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
2584 ** is treated as SQLITE_DENY. In this case an error is left in pParse.
2585 */
2586 SQLITE_PRIVATE int sqlite3AuthReadCol(
2587 Parse *pParse, /* The parser context */
2588 const char *zTab, /* Table name */
2589 const char *zCol, /* Column name */
2590 int iDb /* Index of containing database. */
2591 ){
2592 sqlite3 *db = pParse->db; /* Database handle */
2593 char *zDb = db->aDb[iDb].zName; /* Name of attached database */
2594 int rc; /* Auth callback return code */
2595
2596 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
2597 #ifdef SQLITE_USER_AUTHENTICATION
2598 ,db->auth.zAuthUser
2599 #endif
2600 );
2601 if( rc==SQLITE_DENY ){
2602 if( db->nDb>2 || iDb!=0 ){
2603 sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
2604 }else{
2605 sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
2606 }
2607 pParse->rc = SQLITE_AUTH;
2608 }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
2609 sqliteAuthBadReturnCode(pParse);
2610 }
2611 return rc;
2612 }
2613
2614 /*
2615 ** The pExpr should be a TK_COLUMN expression. The table referred to
2616 ** is in pTabList or else it is the NEW or OLD table of a trigger.
2617 ** Check to see if it is OK to read this particular column.
2618 **
2619 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
2620 ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
2621 ** then generate an error.
2622 */
2623 SQLITE_PRIVATE void sqlite3AuthRead(
2624 Parse *pParse, /* The parser context */
2625 Expr *pExpr, /* The expression to check authorization on */
2626 Schema *pSchema, /* The schema of the expression */
2627 SrcList *pTabList /* All table that pExpr might refer to */
2628 ){
2629 sqlite3 *db = pParse->db;
2630 Table *pTab = 0; /* The table being read */
2631 const char *zCol; /* Name of the column of the table */
2632 int iSrc; /* Index in pTabList->a[] of table being read */
2633 int iDb; /* The index of the database the expression refers to */
2634 int iCol; /* Index of column in table */
2635
2636 if( db->xAuth==0 ) return;
2637 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
2638 if( iDb<0 ){
2639 /* An attempt to read a column out of a subquery or other
2640 ** temporary table. */
2641 return;
2642 }
2643
2644 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
2645 if( pExpr->op==TK_TRIGGER ){
2646 pTab = pParse->pTriggerTab;
2647 }else{
2648 assert( pTabList );
2649 for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
2650 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
2651 pTab = pTabList->a[iSrc].pTab;
2652 break;
2653 }
2654 }
2655 }
2656 iCol = pExpr->iColumn;
2657 if( NEVER(pTab==0) ) return;
2658
2659 if( iCol>=0 ){
2660 assert( iCol<pTab->nCol );
2661 zCol = pTab->aCol[iCol].zName;
2662 }else if( pTab->iPKey>=0 ){
2663 assert( pTab->iPKey<pTab->nCol );
2664 zCol = pTab->aCol[pTab->iPKey].zName;
2665 }else{
2666 zCol = "ROWID";
2667 }
2668 assert( iDb>=0 && iDb<db->nDb );
2669 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
2670 pExpr->op = TK_NULL;
2671 }
2672 }
2673
2674 /*
2675 ** Do an authorization check using the code and arguments given. Return
2676 ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
2677 ** is returned, then the error count and error message in pParse are
2678 ** modified appropriately.
2679 */
2680 SQLITE_PRIVATE int sqlite3AuthCheck(
2681 Parse *pParse,
2682 int code,
2683 const char *zArg1,
2684 const char *zArg2,
2685 const char *zArg3
2686 ){
2687 sqlite3 *db = pParse->db;
2688 int rc;
2689
2690 /* Don't do any authorization checks if the database is initialising
2691 ** or if the parser is being invoked from within sqlite3_declare_vtab.
2692 */
2693 if( db->init.busy || IN_DECLARE_VTAB ){
2694 return SQLITE_OK;
2695 }
2696
2697 if( db->xAuth==0 ){
2698 return SQLITE_OK;
2699 }
2700 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
2701 #ifdef SQLITE_USER_AUTHENTICATION
2702 ,db->auth.zAuthUser
2703 #endif
2704 );
2705 if( rc==SQLITE_DENY ){
2706 sqlite3ErrorMsg(pParse, "not authorized");
2707 pParse->rc = SQLITE_AUTH;
2708 }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
2709 rc = SQLITE_DENY;
2710 sqliteAuthBadReturnCode(pParse);
2711 }
2712 return rc;
2713 }
2714
2715 /*
2716 ** Push an authorization context. After this routine is called, the
2717 ** zArg3 argument to authorization callbacks will be zContext until
2718 ** popped. Or if pParse==0, this routine is a no-op.
2719 */
2720 SQLITE_PRIVATE void sqlite3AuthContextPush(
2721 Parse *pParse,
2722 AuthContext *pContext,
2723 const char *zContext
2724 ){
2725 assert( pParse );
2726 pContext->pParse = pParse;
2727 pContext->zAuthContext = pParse->zAuthContext;
2728 pParse->zAuthContext = zContext;
2729 }
2730
2731 /*
2732 ** Pop an authorization context that was previously pushed
2733 ** by sqlite3AuthContextPush
2734 */
2735 SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
2736 if( pContext->pParse ){
2737 pContext->pParse->zAuthContext = pContext->zAuthContext;
2738 pContext->pParse = 0;
2739 }
2740 }
2741
2742 #endif /* SQLITE_OMIT_AUTHORIZATION */
2743
2744 /************** End of auth.c ************************************************/
2745 /************** Begin file build.c *******************************************/
2746 /*
2747 ** 2001 September 15
2748 **
2749 ** The author disclaims copyright to this source code. In place of
2750 ** a legal notice, here is a blessing:
2751 **
2752 ** May you do good and not evil.
2753 ** May you find forgiveness for yourself and forgive others.
2754 ** May you share freely, never taking more than you give.
2755 **
2756 *************************************************************************
2757 ** This file contains C code routines that are called by the SQLite parser
2758 ** when syntax rules are reduced. The routines in this file handle the
2759 ** following kinds of SQL syntax:
2760 **
2761 ** CREATE TABLE
2762 ** DROP TABLE
2763 ** CREATE INDEX
2764 ** DROP INDEX
2765 ** creating ID lists
2766 ** BEGIN TRANSACTION
2767 ** COMMIT
2768 ** ROLLBACK
2769 */
2770 /* #include "sqliteInt.h" */
2771
2772 /*
2773 ** This routine is called when a new SQL statement is beginning to
2774 ** be parsed. Initialize the pParse structure as needed.
2775 */
2776 SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
2777 pParse->explain = (u8)explainFlag;
2778 pParse->nVar = 0;
2779 }
2780
2781 #ifndef SQLITE_OMIT_SHARED_CACHE
2782 /*
2783 ** The TableLock structure is only used by the sqlite3TableLock() and
2784 ** codeTableLocks() functions.
2785 */
2786 struct TableLock {
2787 int iDb; /* The database containing the table to be locked */
2788 int iTab; /* The root page of the table to be locked */
2789 u8 isWriteLock; /* True for write lock. False for a read lock */
2790 const char *zName; /* Name of the table */
2791 };
2792
2793 /*
2794 ** Record the fact that we want to lock a table at run-time.
2795 **
2796 ** The table to be locked has root page iTab and is found in database iDb.
2797 ** A read or a write lock can be taken depending on isWritelock.
2798 **
2799 ** This routine just records the fact that the lock is desired. The
2800 ** code to make the lock occur is generated by a later call to
2801 ** codeTableLocks() which occurs during sqlite3FinishCoding().
2802 */
2803 SQLITE_PRIVATE void sqlite3TableLock(
2804 Parse *pParse, /* Parsing context */
2805 int iDb, /* Index of the database containing the table to lock */
2806 int iTab, /* Root page number of the table to be locked */
2807 u8 isWriteLock, /* True for a write lock */
2808 const char *zName /* Name of the table to be locked */
2809 ){
2810 Parse *pToplevel = sqlite3ParseToplevel(pParse);
2811 int i;
2812 int nBytes;
2813 TableLock *p;
2814 assert( iDb>=0 );
2815
2816 for(i=0; i<pToplevel->nTableLock; i++){
2817 p = &pToplevel->aTableLock[i];
2818 if( p->iDb==iDb && p->iTab==iTab ){
2819 p->isWriteLock = (p->isWriteLock || isWriteLock);
2820 return;
2821 }
2822 }
2823
2824 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
2825 pToplevel->aTableLock =
2826 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
2827 if( pToplevel->aTableLock ){
2828 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
2829 p->iDb = iDb;
2830 p->iTab = iTab;
2831 p->isWriteLock = isWriteLock;
2832 p->zName = zName;
2833 }else{
2834 pToplevel->nTableLock = 0;
2835 pToplevel->db->mallocFailed = 1;
2836 }
2837 }
2838
2839 /*
2840 ** Code an OP_TableLock instruction for each table locked by the
2841 ** statement (configured by calls to sqlite3TableLock()).
2842 */
2843 static void codeTableLocks(Parse *pParse){
2844 int i;
2845 Vdbe *pVdbe;
2846
2847 pVdbe = sqlite3GetVdbe(pParse);
2848 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
2849
2850 for(i=0; i<pParse->nTableLock; i++){
2851 TableLock *p = &pParse->aTableLock[i];
2852 int p1 = p->iDb;
2853 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
2854 p->zName, P4_STATIC);
2855 }
2856 }
2857 #else
2858 #define codeTableLocks(x)
2859 #endif
2860
2861 /*
2862 ** Return TRUE if the given yDbMask object is empty - if it contains no
2863 ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
2864 ** macros when SQLITE_MAX_ATTACHED is greater than 30.
2865 */
2866 #if SQLITE_MAX_ATTACHED>30
2867 SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
2868 int i;
2869 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
2870 return 1;
2871 }
2872 #endif
2873
2874 /*
2875 ** This routine is called after a single SQL statement has been
2876 ** parsed and a VDBE program to execute that statement has been
2877 ** prepared. This routine puts the finishing touches on the
2878 ** VDBE program and resets the pParse structure for the next
2879 ** parse.
2880 **
2881 ** Note that if an error occurred, it might be the case that
2882 ** no VDBE code was generated.
2883 */
2884 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
2885 sqlite3 *db;
2886 Vdbe *v;
2887
2888 assert( pParse->pToplevel==0 );
2889 db = pParse->db;
2890 if( pParse->nested ) return;
2891 if( db->mallocFailed || pParse->nErr ){
2892 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
2893 return;
2894 }
2895
2896 /* Begin by generating some termination code at the end of the
2897 ** vdbe program
2898 */
2899 v = sqlite3GetVdbe(pParse);
2900 assert( !pParse->isMultiWrite
2901 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
2902 if( v ){
2903 while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
2904 sqlite3VdbeAddOp0(v, OP_Halt);
2905
2906 #if SQLITE_USER_AUTHENTICATION
2907 if( pParse->nTableLock>0 && db->init.busy==0 ){
2908 sqlite3UserAuthInit(db);
2909 if( db->auth.authLevel<UAUTH_User ){
2910 pParse->rc = SQLITE_AUTH_USER;
2911 sqlite3ErrorMsg(pParse, "user not authenticated");
2912 return;
2913 }
2914 }
2915 #endif
2916
2917 /* The cookie mask contains one bit for each database file open.
2918 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
2919 ** set for each database that is used. Generate code to start a
2920 ** transaction on each used database and to verify the schema cookie
2921 ** on each used database.
2922 */
2923 if( db->mallocFailed==0
2924 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
2925 ){
2926 int iDb, i;
2927 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
2928 sqlite3VdbeJumpHere(v, 0);
2929 for(iDb=0; iDb<db->nDb; iDb++){
2930 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
2931 sqlite3VdbeUsesBtree(v, iDb);
2932 sqlite3VdbeAddOp4Int(v,
2933 OP_Transaction, /* Opcode */
2934 iDb, /* P1 */
2935 DbMaskTest(pParse->writeMask,iDb), /* P2 */
2936 pParse->cookieValue[iDb], /* P3 */
2937 db->aDb[iDb].pSchema->iGeneration /* P4 */
2938 );
2939 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
2940 VdbeComment((v,
2941 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
2942 }
2943 #ifndef SQLITE_OMIT_VIRTUALTABLE
2944 for(i=0; i<pParse->nVtabLock; i++){
2945 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
2946 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
2947 }
2948 pParse->nVtabLock = 0;
2949 #endif
2950
2951 /* Once all the cookies have been verified and transactions opened,
2952 ** obtain the required table-locks. This is a no-op unless the
2953 ** shared-cache feature is enabled.
2954 */
2955 codeTableLocks(pParse);
2956
2957 /* Initialize any AUTOINCREMENT data structures required.
2958 */
2959 sqlite3AutoincrementBegin(pParse);
2960
2961 /* Code constant expressions that where factored out of inner loops */
2962 if( pParse->pConstExpr ){
2963 ExprList *pEL = pParse->pConstExpr;
2964 pParse->okConstFactor = 0;
2965 for(i=0; i<pEL->nExpr; i++){
2966 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
2967 }
2968 }
2969
2970 /* Finally, jump back to the beginning of the executable code. */
2971 sqlite3VdbeGoto(v, 1);
2972 }
2973 }
2974
2975
2976 /* Get the VDBE program ready for execution
2977 */
2978 if( v && pParse->nErr==0 && !db->mallocFailed ){
2979 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */
2980 /* A minimum of one cursor is required if autoincrement is used
2981 * See ticket [a696379c1f08866] */
2982 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
2983 sqlite3VdbeMakeReady(v, pParse);
2984 pParse->rc = SQLITE_DONE;
2985 pParse->colNamesSet = 0;
2986 }else{
2987 pParse->rc = SQLITE_ERROR;
2988 }
2989 pParse->nTab = 0;
2990 pParse->nMem = 0;
2991 pParse->nSet = 0;
2992 pParse->nVar = 0;
2993 DbMaskZero(pParse->cookieMask);
2994 }
2995
2996 /*
2997 ** Run the parser and code generator recursively in order to generate
2998 ** code for the SQL statement given onto the end of the pParse context
2999 ** currently under construction. When the parser is run recursively
3000 ** this way, the final OP_Halt is not appended and other initialization
3001 ** and finalization steps are omitted because those are handling by the
3002 ** outermost parser.
3003 **
3004 ** Not everything is nestable. This facility is designed to permit
3005 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
3006 ** care if you decide to try to use this routine for some other purposes.
3007 */
3008 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
3009 va_list ap;
3010 char *zSql;
3011 char *zErrMsg = 0;
3012 sqlite3 *db = pParse->db;
3013 # define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar))
3014 char saveBuf[SAVE_SZ];
3015
3016 if( pParse->nErr ) return;
3017 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
3018 va_start(ap, zFormat);
3019 zSql = sqlite3VMPrintf(db, zFormat, ap);
3020 va_end(ap);
3021 if( zSql==0 ){
3022 return; /* A malloc must have failed */
3023 }
3024 pParse->nested++;
3025 memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
3026 memset(&pParse->nVar, 0, SAVE_SZ);
3027 sqlite3RunParser(pParse, zSql, &zErrMsg);
3028 sqlite3DbFree(db, zErrMsg);
3029 sqlite3DbFree(db, zSql);
3030 memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
3031 pParse->nested--;
3032 }
3033
3034 #if SQLITE_USER_AUTHENTICATION
3035 /*
3036 ** Return TRUE if zTable is the name of the system table that stores the
3037 ** list of users and their access credentials.
3038 */
3039 SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
3040 return sqlite3_stricmp(zTable, "sqlite_user")==0;
3041 }
3042 #endif
3043
3044 /*
3045 ** Locate the in-memory structure that describes a particular database
3046 ** table given the name of that table and (optionally) the name of the
3047 ** database containing the table. Return NULL if not found.
3048 **
3049 ** If zDatabase is 0, all databases are searched for the table and the
3050 ** first matching table is returned. (No checking for duplicate table
3051 ** names is done.) The search order is TEMP first, then MAIN, then any
3052 ** auxiliary databases added using the ATTACH command.
3053 **
3054 ** See also sqlite3LocateTable().
3055 */
3056 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const cha r *zDatabase){
3057 Table *p = 0;
3058 int i;
3059
3060 /* All mutexes are required for schema access. Make sure we hold them. */
3061 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
3062 #if SQLITE_USER_AUTHENTICATION
3063 /* Only the admin user is allowed to know that the sqlite_user table
3064 ** exists */
3065 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
3066 return 0;
3067 }
3068 #endif
3069 for(i=OMIT_TEMPDB; i<db->nDb; i++){
3070 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
3071 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
3072 assert( sqlite3SchemaMutexHeld(db, j, 0) );
3073 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
3074 if( p ) break;
3075 }
3076 return p;
3077 }
3078
3079 /*
3080 ** Locate the in-memory structure that describes a particular database
3081 ** table given the name of that table and (optionally) the name of the
3082 ** database containing the table. Return NULL if not found. Also leave an
3083 ** error message in pParse->zErrMsg.
3084 **
3085 ** The difference between this routine and sqlite3FindTable() is that this
3086 ** routine leaves an error message in pParse->zErrMsg where
3087 ** sqlite3FindTable() does not.
3088 */
3089 SQLITE_PRIVATE Table *sqlite3LocateTable(
3090 Parse *pParse, /* context in which to report errors */
3091 int isView, /* True if looking for a VIEW rather than a TABLE */
3092 const char *zName, /* Name of the table we are looking for */
3093 const char *zDbase /* Name of the database. Might be NULL */
3094 ){
3095 Table *p;
3096
3097 /* Read the database schema. If an error occurs, leave an error message
3098 ** and code in pParse and return NULL. */
3099 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
3100 return 0;
3101 }
3102
3103 p = sqlite3FindTable(pParse->db, zName, zDbase);
3104 if( p==0 ){
3105 const char *zMsg = isView ? "no such view" : "no such table";
3106 #ifndef SQLITE_OMIT_VIRTUALTABLE
3107 if( sqlite3FindDbName(pParse->db, zDbase)<1 ){
3108 /* If zName is the not the name of a table in the schema created using
3109 ** CREATE, then check to see if it is the name of an virtual table that
3110 ** can be an eponymous virtual table. */
3111 Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName);
3112 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
3113 return pMod->pEpoTab;
3114 }
3115 }
3116 #endif
3117 if( zDbase ){
3118 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
3119 }else{
3120 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
3121 }
3122 pParse->checkSchema = 1;
3123 }
3124
3125 return p;
3126 }
3127
3128 /*
3129 ** Locate the table identified by *p.
3130 **
3131 ** This is a wrapper around sqlite3LocateTable(). The difference between
3132 ** sqlite3LocateTable() and this function is that this function restricts
3133 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
3134 ** non-NULL if it is part of a view or trigger program definition. See
3135 ** sqlite3FixSrcList() for details.
3136 */
3137 SQLITE_PRIVATE Table *sqlite3LocateTableItem(
3138 Parse *pParse,
3139 int isView,
3140 struct SrcList_item *p
3141 ){
3142 const char *zDb;
3143 assert( p->pSchema==0 || p->zDatabase==0 );
3144 if( p->pSchema ){
3145 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
3146 zDb = pParse->db->aDb[iDb].zName;
3147 }else{
3148 zDb = p->zDatabase;
3149 }
3150 return sqlite3LocateTable(pParse, isView, p->zName, zDb);
3151 }
3152
3153 /*
3154 ** Locate the in-memory structure that describes
3155 ** a particular index given the name of that index
3156 ** and the name of the database that contains the index.
3157 ** Return NULL if not found.
3158 **
3159 ** If zDatabase is 0, all databases are searched for the
3160 ** table and the first matching index is returned. (No checking
3161 ** for duplicate index names is done.) The search order is
3162 ** TEMP first, then MAIN, then any auxiliary databases added
3163 ** using the ATTACH command.
3164 */
3165 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const cha r *zDb){
3166 Index *p = 0;
3167 int i;
3168 /* All mutexes are required for schema access. Make sure we hold them. */
3169 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
3170 for(i=OMIT_TEMPDB; i<db->nDb; i++){
3171 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
3172 Schema *pSchema = db->aDb[j].pSchema;
3173 assert( pSchema );
3174 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
3175 assert( sqlite3SchemaMutexHeld(db, j, 0) );
3176 p = sqlite3HashFind(&pSchema->idxHash, zName);
3177 if( p ) break;
3178 }
3179 return p;
3180 }
3181
3182 /*
3183 ** Reclaim the memory used by an index
3184 */
3185 static void freeIndex(sqlite3 *db, Index *p){
3186 #ifndef SQLITE_OMIT_ANALYZE
3187 sqlite3DeleteIndexSamples(db, p);
3188 #endif
3189 sqlite3ExprDelete(db, p->pPartIdxWhere);
3190 sqlite3ExprListDelete(db, p->aColExpr);
3191 sqlite3DbFree(db, p->zColAff);
3192 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
3193 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
3194 sqlite3_free(p->aiRowEst);
3195 #endif
3196 sqlite3DbFree(db, p);
3197 }
3198
3199 /*
3200 ** For the index called zIdxName which is found in the database iDb,
3201 ** unlike that index from its Table then remove the index from
3202 ** the index hash table and free all memory structures associated
3203 ** with the index.
3204 */
3205 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
3206 Index *pIndex;
3207 Hash *pHash;
3208
3209 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
3210 pHash = &db->aDb[iDb].pSchema->idxHash;
3211 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
3212 if( ALWAYS(pIndex) ){
3213 if( pIndex->pTable->pIndex==pIndex ){
3214 pIndex->pTable->pIndex = pIndex->pNext;
3215 }else{
3216 Index *p;
3217 /* Justification of ALWAYS(); The index must be on the list of
3218 ** indices. */
3219 p = pIndex->pTable->pIndex;
3220 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
3221 if( ALWAYS(p && p->pNext==pIndex) ){
3222 p->pNext = pIndex->pNext;
3223 }
3224 }
3225 freeIndex(db, pIndex);
3226 }
3227 db->flags |= SQLITE_InternChanges;
3228 }
3229
3230 /*
3231 ** Look through the list of open database files in db->aDb[] and if
3232 ** any have been closed, remove them from the list. Reallocate the
3233 ** db->aDb[] structure to a smaller size, if possible.
3234 **
3235 ** Entry 0 (the "main" database) and entry 1 (the "temp" database)
3236 ** are never candidates for being collapsed.
3237 */
3238 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
3239 int i, j;
3240 for(i=j=2; i<db->nDb; i++){
3241 struct Db *pDb = &db->aDb[i];
3242 if( pDb->pBt==0 ){
3243 sqlite3DbFree(db, pDb->zName);
3244 pDb->zName = 0;
3245 continue;
3246 }
3247 if( j<i ){
3248 db->aDb[j] = db->aDb[i];
3249 }
3250 j++;
3251 }
3252 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
3253 db->nDb = j;
3254 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
3255 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
3256 sqlite3DbFree(db, db->aDb);
3257 db->aDb = db->aDbStatic;
3258 }
3259 }
3260
3261 /*
3262 ** Reset the schema for the database at index iDb. Also reset the
3263 ** TEMP schema.
3264 */
3265 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
3266 Db *pDb;
3267 assert( iDb<db->nDb );
3268
3269 /* Case 1: Reset the single schema identified by iDb */
3270 pDb = &db->aDb[iDb];
3271 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
3272 assert( pDb->pSchema!=0 );
3273 sqlite3SchemaClear(pDb->pSchema);
3274
3275 /* If any database other than TEMP is reset, then also reset TEMP
3276 ** since TEMP might be holding triggers that reference tables in the
3277 ** other database.
3278 */
3279 if( iDb!=1 ){
3280 pDb = &db->aDb[1];
3281 assert( pDb->pSchema!=0 );
3282 sqlite3SchemaClear(pDb->pSchema);
3283 }
3284 return;
3285 }
3286
3287 /*
3288 ** Erase all schema information from all attached databases (including
3289 ** "main" and "temp") for a single database connection.
3290 */
3291 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
3292 int i;
3293 sqlite3BtreeEnterAll(db);
3294 for(i=0; i<db->nDb; i++){
3295 Db *pDb = &db->aDb[i];
3296 if( pDb->pSchema ){
3297 sqlite3SchemaClear(pDb->pSchema);
3298 }
3299 }
3300 db->flags &= ~SQLITE_InternChanges;
3301 sqlite3VtabUnlockList(db);
3302 sqlite3BtreeLeaveAll(db);
3303 sqlite3CollapseDatabaseArray(db);
3304 }
3305
3306 /*
3307 ** This routine is called when a commit occurs.
3308 */
3309 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
3310 db->flags &= ~SQLITE_InternChanges;
3311 }
3312
3313 /*
3314 ** Delete memory allocated for the column names of a table or view (the
3315 ** Table.aCol[] array).
3316 */
3317 SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
3318 int i;
3319 Column *pCol;
3320 assert( pTable!=0 );
3321 if( (pCol = pTable->aCol)!=0 ){
3322 for(i=0; i<pTable->nCol; i++, pCol++){
3323 sqlite3DbFree(db, pCol->zName);
3324 sqlite3ExprDelete(db, pCol->pDflt);
3325 sqlite3DbFree(db, pCol->zDflt);
3326 sqlite3DbFree(db, pCol->zType);
3327 sqlite3DbFree(db, pCol->zColl);
3328 }
3329 sqlite3DbFree(db, pTable->aCol);
3330 }
3331 }
3332
3333 /*
3334 ** Remove the memory data structures associated with the given
3335 ** Table. No changes are made to disk by this routine.
3336 **
3337 ** This routine just deletes the data structure. It does not unlink
3338 ** the table data structure from the hash table. But it does destroy
3339 ** memory structures of the indices and foreign keys associated with
3340 ** the table.
3341 **
3342 ** The db parameter is optional. It is needed if the Table object
3343 ** contains lookaside memory. (Table objects in the schema do not use
3344 ** lookaside memory, but some ephemeral Table objects do.) Or the
3345 ** db parameter can be used with db->pnBytesFreed to measure the memory
3346 ** used by the Table object.
3347 */
3348 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
3349 Index *pIndex, *pNext;
3350 TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
3351
3352 assert( !pTable || pTable->nRef>0 );
3353
3354 /* Do not delete the table until the reference count reaches zero. */
3355 if( !pTable ) return;
3356 if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
3357
3358 /* Record the number of outstanding lookaside allocations in schema Tables
3359 ** prior to doing any free() operations. Since schema Tables do not use
3360 ** lookaside, this number should not change. */
3361 TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
3362 db->lookaside.nOut : 0 );
3363
3364 /* Delete all indices associated with this table. */
3365 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
3366 pNext = pIndex->pNext;
3367 assert( pIndex->pSchema==pTable->pSchema );
3368 if( !db || db->pnBytesFreed==0 ){
3369 char *zName = pIndex->zName;
3370 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
3371 &pIndex->pSchema->idxHash, zName, 0
3372 );
3373 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
3374 assert( pOld==pIndex || pOld==0 );
3375 }
3376 freeIndex(db, pIndex);
3377 }
3378
3379 /* Delete any foreign keys attached to this table. */
3380 sqlite3FkDelete(db, pTable);
3381
3382 /* Delete the Table structure itself.
3383 */
3384 sqlite3DeleteColumnNames(db, pTable);
3385 sqlite3DbFree(db, pTable->zName);
3386 sqlite3DbFree(db, pTable->zColAff);
3387 sqlite3SelectDelete(db, pTable->pSelect);
3388 sqlite3ExprListDelete(db, pTable->pCheck);
3389 #ifndef SQLITE_OMIT_VIRTUALTABLE
3390 sqlite3VtabClear(db, pTable);
3391 #endif
3392 sqlite3DbFree(db, pTable);
3393
3394 /* Verify that no lookaside memory was used by schema tables */
3395 assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
3396 }
3397
3398 /*
3399 ** Unlink the given table from the hash tables and the delete the
3400 ** table structure with all its indices and foreign keys.
3401 */
3402 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
3403 Table *p;
3404 Db *pDb;
3405
3406 assert( db!=0 );
3407 assert( iDb>=0 && iDb<db->nDb );
3408 assert( zTabName );
3409 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
3410 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
3411 pDb = &db->aDb[iDb];
3412 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
3413 sqlite3DeleteTable(db, p);
3414 db->flags |= SQLITE_InternChanges;
3415 }
3416
3417 /*
3418 ** Given a token, return a string that consists of the text of that
3419 ** token. Space to hold the returned string
3420 ** is obtained from sqliteMalloc() and must be freed by the calling
3421 ** function.
3422 **
3423 ** Any quotation marks (ex: "name", 'name', [name], or `name`) that
3424 ** surround the body of the token are removed.
3425 **
3426 ** Tokens are often just pointers into the original SQL text and so
3427 ** are not \000 terminated and are not persistent. The returned string
3428 ** is \000 terminated and is persistent.
3429 */
3430 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
3431 char *zName;
3432 if( pName ){
3433 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
3434 sqlite3Dequote(zName);
3435 }else{
3436 zName = 0;
3437 }
3438 return zName;
3439 }
3440
3441 /*
3442 ** Open the sqlite_master table stored in database number iDb for
3443 ** writing. The table is opened using cursor 0.
3444 */
3445 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
3446 Vdbe *v = sqlite3GetVdbe(p);
3447 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
3448 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
3449 if( p->nTab==0 ){
3450 p->nTab = 1;
3451 }
3452 }
3453
3454 /*
3455 ** Parameter zName points to a nul-terminated buffer containing the name
3456 ** of a database ("main", "temp" or the name of an attached db). This
3457 ** function returns the index of the named database in db->aDb[], or
3458 ** -1 if the named db cannot be found.
3459 */
3460 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
3461 int i = -1; /* Database number */
3462 if( zName ){
3463 Db *pDb;
3464 int n = sqlite3Strlen30(zName);
3465 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
3466 if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) &&
3467 0==sqlite3StrICmp(pDb->zName, zName) ){
3468 break;
3469 }
3470 }
3471 }
3472 return i;
3473 }
3474
3475 /*
3476 ** The token *pName contains the name of a database (either "main" or
3477 ** "temp" or the name of an attached db). This routine returns the
3478 ** index of the named database in db->aDb[], or -1 if the named db
3479 ** does not exist.
3480 */
3481 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
3482 int i; /* Database number */
3483 char *zName; /* Name we are searching for */
3484 zName = sqlite3NameFromToken(db, pName);
3485 i = sqlite3FindDbName(db, zName);
3486 sqlite3DbFree(db, zName);
3487 return i;
3488 }
3489
3490 /* The table or view or trigger name is passed to this routine via tokens
3491 ** pName1 and pName2. If the table name was fully qualified, for example:
3492 **
3493 ** CREATE TABLE xxx.yyy (...);
3494 **
3495 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
3496 ** the table name is not fully qualified, i.e.:
3497 **
3498 ** CREATE TABLE yyy(...);
3499 **
3500 ** Then pName1 is set to "yyy" and pName2 is "".
3501 **
3502 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
3503 ** pName2) that stores the unqualified table name. The index of the
3504 ** database "xxx" is returned.
3505 */
3506 SQLITE_PRIVATE int sqlite3TwoPartName(
3507 Parse *pParse, /* Parsing and code generating context */
3508 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
3509 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
3510 Token **pUnqual /* Write the unqualified object name here */
3511 ){
3512 int iDb; /* Database holding the object */
3513 sqlite3 *db = pParse->db;
3514
3515 if( ALWAYS(pName2!=0) && pName2->n>0 ){
3516 if( db->init.busy ) {
3517 sqlite3ErrorMsg(pParse, "corrupt database");
3518 return -1;
3519 }
3520 *pUnqual = pName2;
3521 iDb = sqlite3FindDb(db, pName1);
3522 if( iDb<0 ){
3523 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
3524 return -1;
3525 }
3526 }else{
3527 assert( db->init.iDb==0 || db->init.busy );
3528 iDb = db->init.iDb;
3529 *pUnqual = pName1;
3530 }
3531 return iDb;
3532 }
3533
3534 /*
3535 ** This routine is used to check if the UTF-8 string zName is a legal
3536 ** unqualified name for a new schema object (table, index, view or
3537 ** trigger). All names are legal except those that begin with the string
3538 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
3539 ** is reserved for internal use.
3540 */
3541 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
3542 if( !pParse->db->init.busy && pParse->nested==0
3543 && (pParse->db->flags & SQLITE_WriteSchema)==0
3544 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
3545 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
3546 return SQLITE_ERROR;
3547 }
3548 return SQLITE_OK;
3549 }
3550
3551 /*
3552 ** Return the PRIMARY KEY index of a table
3553 */
3554 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
3555 Index *p;
3556 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
3557 return p;
3558 }
3559
3560 /*
3561 ** Return the column of index pIdx that corresponds to table
3562 ** column iCol. Return -1 if not found.
3563 */
3564 SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
3565 int i;
3566 for(i=0; i<pIdx->nColumn; i++){
3567 if( iCol==pIdx->aiColumn[i] ) return i;
3568 }
3569 return -1;
3570 }
3571
3572 /*
3573 ** Begin constructing a new table representation in memory. This is
3574 ** the first of several action routines that get called in response
3575 ** to a CREATE TABLE statement. In particular, this routine is called
3576 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
3577 ** flag is true if the table should be stored in the auxiliary database
3578 ** file instead of in the main database file. This is normally the case
3579 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
3580 ** CREATE and TABLE.
3581 **
3582 ** The new table record is initialized and put in pParse->pNewTable.
3583 ** As more of the CREATE TABLE statement is parsed, additional action
3584 ** routines will be called to add more information to this record.
3585 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
3586 ** is called to complete the construction of the new table record.
3587 */
3588 SQLITE_PRIVATE void sqlite3StartTable(
3589 Parse *pParse, /* Parser context */
3590 Token *pName1, /* First part of the name of the table or view */
3591 Token *pName2, /* Second part of the name of the table or view */
3592 int isTemp, /* True if this is a TEMP table */
3593 int isView, /* True if this is a VIEW */
3594 int isVirtual, /* True if this is a VIRTUAL table */
3595 int noErr /* Do nothing if table already exists */
3596 ){
3597 Table *pTable;
3598 char *zName = 0; /* The name of the new table */
3599 sqlite3 *db = pParse->db;
3600 Vdbe *v;
3601 int iDb; /* Database number to create the table in */
3602 Token *pName; /* Unqualified name of the table to create */
3603
3604 /* The table or view name to create is passed to this routine via tokens
3605 ** pName1 and pName2. If the table name was fully qualified, for example:
3606 **
3607 ** CREATE TABLE xxx.yyy (...);
3608 **
3609 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
3610 ** the table name is not fully qualified, i.e.:
3611 **
3612 ** CREATE TABLE yyy(...);
3613 **
3614 ** Then pName1 is set to "yyy" and pName2 is "".
3615 **
3616 ** The call below sets the pName pointer to point at the token (pName1 or
3617 ** pName2) that stores the unqualified table name. The variable iDb is
3618 ** set to the index of the database that the table or view is to be
3619 ** created in.
3620 */
3621 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
3622 if( iDb<0 ) return;
3623 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
3624 /* If creating a temp table, the name may not be qualified. Unless
3625 ** the database name is "temp" anyway. */
3626 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
3627 return;
3628 }
3629 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
3630
3631 pParse->sNameToken = *pName;
3632 zName = sqlite3NameFromToken(db, pName);
3633 if( zName==0 ) return;
3634 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
3635 goto begin_table_error;
3636 }
3637 if( db->init.iDb==1 ) isTemp = 1;
3638 #ifndef SQLITE_OMIT_AUTHORIZATION
3639 assert( (isTemp & 1)==isTemp );
3640 {
3641 int code;
3642 char *zDb = db->aDb[iDb].zName;
3643 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
3644 goto begin_table_error;
3645 }
3646 if( isView ){
3647 if( !OMIT_TEMPDB && isTemp ){
3648 code = SQLITE_CREATE_TEMP_VIEW;
3649 }else{
3650 code = SQLITE_CREATE_VIEW;
3651 }
3652 }else{
3653 if( !OMIT_TEMPDB && isTemp ){
3654 code = SQLITE_CREATE_TEMP_TABLE;
3655 }else{
3656 code = SQLITE_CREATE_TABLE;
3657 }
3658 }
3659 if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
3660 goto begin_table_error;
3661 }
3662 }
3663 #endif
3664
3665 /* Make sure the new table name does not collide with an existing
3666 ** index or table name in the same database. Issue an error message if
3667 ** it does. The exception is if the statement being parsed was passed
3668 ** to an sqlite3_declare_vtab() call. In that case only the column names
3669 ** and types will be used, so there is no need to test for namespace
3670 ** collisions.
3671 */
3672 if( !IN_DECLARE_VTAB ){
3673 char *zDb = db->aDb[iDb].zName;
3674 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
3675 goto begin_table_error;
3676 }
3677 pTable = sqlite3FindTable(db, zName, zDb);
3678 if( pTable ){
3679 if( !noErr ){
3680 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
3681 }else{
3682 assert( !db->init.busy || CORRUPT_DB );
3683 sqlite3CodeVerifySchema(pParse, iDb);
3684 }
3685 goto begin_table_error;
3686 }
3687 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
3688 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
3689 goto begin_table_error;
3690 }
3691 }
3692
3693 pTable = sqlite3DbMallocZero(db, sizeof(Table));
3694 if( pTable==0 ){
3695 db->mallocFailed = 1;
3696 pParse->rc = SQLITE_NOMEM;
3697 pParse->nErr++;
3698 goto begin_table_error;
3699 }
3700 pTable->zName = zName;
3701 pTable->iPKey = -1;
3702 pTable->pSchema = db->aDb[iDb].pSchema;
3703 pTable->nRef = 1;
3704 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
3705 assert( pParse->pNewTable==0 );
3706 pParse->pNewTable = pTable;
3707
3708 /* If this is the magic sqlite_sequence table used by autoincrement,
3709 ** then record a pointer to this table in the main database structure
3710 ** so that INSERT can find the table easily.
3711 */
3712 #ifndef SQLITE_OMIT_AUTOINCREMENT
3713 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
3714 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
3715 pTable->pSchema->pSeqTab = pTable;
3716 }
3717 #endif
3718
3719 /* Begin generating the code that will insert the table record into
3720 ** the SQLITE_MASTER table. Note in particular that we must go ahead
3721 ** and allocate the record number for the table entry now. Before any
3722 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
3723 ** indices to be created and the table record must come before the
3724 ** indices. Hence, the record number for the table must be allocated
3725 ** now.
3726 */
3727 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
3728 int addr1;
3729 int fileFormat;
3730 int reg1, reg2, reg3;
3731 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
3732 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
3733 sqlite3BeginWriteOperation(pParse, 1, iDb);
3734
3735 #ifndef SQLITE_OMIT_VIRTUALTABLE
3736 if( isVirtual ){
3737 sqlite3VdbeAddOp0(v, OP_VBegin);
3738 }
3739 #endif
3740
3741 /* If the file format and encoding in the database have not been set,
3742 ** set them now.
3743 */
3744 reg1 = pParse->regRowid = ++pParse->nMem;
3745 reg2 = pParse->regRoot = ++pParse->nMem;
3746 reg3 = ++pParse->nMem;
3747 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
3748 sqlite3VdbeUsesBtree(v, iDb);
3749 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
3750 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
3751 1 : SQLITE_MAX_FILE_FORMAT;
3752 sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
3753 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
3754 sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
3755 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
3756 sqlite3VdbeJumpHere(v, addr1);
3757
3758 /* This just creates a place-holder record in the sqlite_master table.
3759 ** The record created does not contain anything yet. It will be replaced
3760 ** by the real entry in code generated at sqlite3EndTable().
3761 **
3762 ** The rowid for the new entry is left in register pParse->regRowid.
3763 ** The root page number of the new table is left in reg pParse->regRoot.
3764 ** The rowid and root page number values are needed by the code that
3765 ** sqlite3EndTable will generate.
3766 */
3767 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
3768 if( isView || isVirtual ){
3769 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
3770 }else
3771 #endif
3772 {
3773 pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
3774 }
3775 sqlite3OpenMasterTable(pParse, iDb);
3776 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
3777 sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
3778 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
3779 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
3780 sqlite3VdbeAddOp0(v, OP_Close);
3781 }
3782
3783 /* Normal (non-error) return. */
3784 return;
3785
3786 /* If an error occurs, we jump here */
3787 begin_table_error:
3788 sqlite3DbFree(db, zName);
3789 return;
3790 }
3791
3792 /* Set properties of a table column based on the (magical)
3793 ** name of the column.
3794 */
3795 #if SQLITE_ENABLE_HIDDEN_COLUMNS
3796 SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
3797 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
3798 pCol->colFlags |= COLFLAG_HIDDEN;
3799 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
3800 pTab->tabFlags |= TF_OOOHidden;
3801 }
3802 }
3803 #endif
3804
3805
3806 /*
3807 ** Add a new column to the table currently being constructed.
3808 **
3809 ** The parser calls this routine once for each column declaration
3810 ** in a CREATE TABLE statement. sqlite3StartTable() gets called
3811 ** first to get things going. Then this routine is called for each
3812 ** column.
3813 */
3814 SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
3815 Table *p;
3816 int i;
3817 char *z;
3818 Column *pCol;
3819 sqlite3 *db = pParse->db;
3820 if( (p = pParse->pNewTable)==0 ) return;
3821 #if SQLITE_MAX_COLUMN
3822 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
3823 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
3824 return;
3825 }
3826 #endif
3827 z = sqlite3NameFromToken(db, pName);
3828 if( z==0 ) return;
3829 for(i=0; i<p->nCol; i++){
3830 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
3831 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
3832 sqlite3DbFree(db, z);
3833 return;
3834 }
3835 }
3836 if( (p->nCol & 0x7)==0 ){
3837 Column *aNew;
3838 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
3839 if( aNew==0 ){
3840 sqlite3DbFree(db, z);
3841 return;
3842 }
3843 p->aCol = aNew;
3844 }
3845 pCol = &p->aCol[p->nCol];
3846 memset(pCol, 0, sizeof(p->aCol[0]));
3847 pCol->zName = z;
3848 sqlite3ColumnPropertiesFromName(p, pCol);
3849
3850 /* If there is no type specified, columns have the default affinity
3851 ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will
3852 ** be called next to set pCol->affinity correctly.
3853 */
3854 pCol->affinity = SQLITE_AFF_BLOB;
3855 pCol->szEst = 1;
3856 p->nCol++;
3857 }
3858
3859 /*
3860 ** This routine is called by the parser while in the middle of
3861 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
3862 ** been seen on a column. This routine sets the notNull flag on
3863 ** the column currently under construction.
3864 */
3865 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
3866 Table *p;
3867 p = pParse->pNewTable;
3868 if( p==0 || NEVER(p->nCol<1) ) return;
3869 p->aCol[p->nCol-1].notNull = (u8)onError;
3870 }
3871
3872 /*
3873 ** Scan the column type name zType (length nType) and return the
3874 ** associated affinity type.
3875 **
3876 ** This routine does a case-independent search of zType for the
3877 ** substrings in the following table. If one of the substrings is
3878 ** found, the corresponding affinity is returned. If zType contains
3879 ** more than one of the substrings, entries toward the top of
3880 ** the table take priority. For example, if zType is 'BLOBINT',
3881 ** SQLITE_AFF_INTEGER is returned.
3882 **
3883 ** Substring | Affinity
3884 ** --------------------------------
3885 ** 'INT' | SQLITE_AFF_INTEGER
3886 ** 'CHAR' | SQLITE_AFF_TEXT
3887 ** 'CLOB' | SQLITE_AFF_TEXT
3888 ** 'TEXT' | SQLITE_AFF_TEXT
3889 ** 'BLOB' | SQLITE_AFF_BLOB
3890 ** 'REAL' | SQLITE_AFF_REAL
3891 ** 'FLOA' | SQLITE_AFF_REAL
3892 ** 'DOUB' | SQLITE_AFF_REAL
3893 **
3894 ** If none of the substrings in the above table are found,
3895 ** SQLITE_AFF_NUMERIC is returned.
3896 */
3897 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
3898 u32 h = 0;
3899 char aff = SQLITE_AFF_NUMERIC;
3900 const char *zChar = 0;
3901
3902 if( zIn==0 ) return aff;
3903 while( zIn[0] ){
3904 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
3905 zIn++;
3906 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
3907 aff = SQLITE_AFF_TEXT;
3908 zChar = zIn;
3909 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
3910 aff = SQLITE_AFF_TEXT;
3911 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
3912 aff = SQLITE_AFF_TEXT;
3913 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
3914 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
3915 aff = SQLITE_AFF_BLOB;
3916 if( zIn[0]=='(' ) zChar = zIn;
3917 #ifndef SQLITE_OMIT_FLOATING_POINT
3918 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
3919 && aff==SQLITE_AFF_NUMERIC ){
3920 aff = SQLITE_AFF_REAL;
3921 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
3922 && aff==SQLITE_AFF_NUMERIC ){
3923 aff = SQLITE_AFF_REAL;
3924 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
3925 && aff==SQLITE_AFF_NUMERIC ){
3926 aff = SQLITE_AFF_REAL;
3927 #endif
3928 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
3929 aff = SQLITE_AFF_INTEGER;
3930 break;
3931 }
3932 }
3933
3934 /* If pszEst is not NULL, store an estimate of the field size. The
3935 ** estimate is scaled so that the size of an integer is 1. */
3936 if( pszEst ){
3937 *pszEst = 1; /* default size is approx 4 bytes */
3938 if( aff<SQLITE_AFF_NUMERIC ){
3939 if( zChar ){
3940 while( zChar[0] ){
3941 if( sqlite3Isdigit(zChar[0]) ){
3942 int v = 0;
3943 sqlite3GetInt32(zChar, &v);
3944 v = v/4 + 1;
3945 if( v>255 ) v = 255;
3946 *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
3947 break;
3948 }
3949 zChar++;
3950 }
3951 }else{
3952 *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
3953 }
3954 }
3955 }
3956 return aff;
3957 }
3958
3959 /*
3960 ** This routine is called by the parser while in the middle of
3961 ** parsing a CREATE TABLE statement. The pFirst token is the first
3962 ** token in the sequence of tokens that describe the type of the
3963 ** column currently under construction. pLast is the last token
3964 ** in the sequence. Use this information to construct a string
3965 ** that contains the typename of the column and store that string
3966 ** in zType.
3967 */
3968 SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
3969 Table *p;
3970 Column *pCol;
3971
3972 p = pParse->pNewTable;
3973 if( p==0 || NEVER(p->nCol<1) ) return;
3974 pCol = &p->aCol[p->nCol-1];
3975 assert( pCol->zType==0 || CORRUPT_DB );
3976 sqlite3DbFree(pParse->db, pCol->zType);
3977 pCol->zType = sqlite3NameFromToken(pParse->db, pType);
3978 pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst);
3979 }
3980
3981 /*
3982 ** The expression is the default value for the most recently added column
3983 ** of the table currently under construction.
3984 **
3985 ** Default value expressions must be constant. Raise an exception if this
3986 ** is not the case.
3987 **
3988 ** This routine is called by the parser while in the middle of
3989 ** parsing a CREATE TABLE statement.
3990 */
3991 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
3992 Table *p;
3993 Column *pCol;
3994 sqlite3 *db = pParse->db;
3995 p = pParse->pNewTable;
3996 if( p!=0 ){
3997 pCol = &(p->aCol[p->nCol-1]);
3998 if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
3999 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
4000 pCol->zName);
4001 }else{
4002 /* A copy of pExpr is used instead of the original, as pExpr contains
4003 ** tokens that point to volatile memory. The 'span' of the expression
4004 ** is required by pragma table_info.
4005 */
4006 sqlite3ExprDelete(db, pCol->pDflt);
4007 pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
4008 sqlite3DbFree(db, pCol->zDflt);
4009 pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
4010 (int)(pSpan->zEnd - pSpan->zStart));
4011 }
4012 }
4013 sqlite3ExprDelete(db, pSpan->pExpr);
4014 }
4015
4016 /*
4017 ** Backwards Compatibility Hack:
4018 **
4019 ** Historical versions of SQLite accepted strings as column names in
4020 ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
4021 **
4022 ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
4023 ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
4024 **
4025 ** This is goofy. But to preserve backwards compatibility we continue to
4026 ** accept it. This routine does the necessary conversion. It converts
4027 ** the expression given in its argument from a TK_STRING into a TK_ID
4028 ** if the expression is just a TK_STRING with an optional COLLATE clause.
4029 ** If the epxression is anything other than TK_STRING, the expression is
4030 ** unchanged.
4031 */
4032 static void sqlite3StringToId(Expr *p){
4033 if( p->op==TK_STRING ){
4034 p->op = TK_ID;
4035 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
4036 p->pLeft->op = TK_ID;
4037 }
4038 }
4039
4040 /*
4041 ** Designate the PRIMARY KEY for the table. pList is a list of names
4042 ** of columns that form the primary key. If pList is NULL, then the
4043 ** most recently added column of the table is the primary key.
4044 **
4045 ** A table can have at most one primary key. If the table already has
4046 ** a primary key (and this is the second primary key) then create an
4047 ** error.
4048 **
4049 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
4050 ** then we will try to use that column as the rowid. Set the Table.iPKey
4051 ** field of the table under construction to be the index of the
4052 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
4053 ** no INTEGER PRIMARY KEY.
4054 **
4055 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
4056 ** index for the key. No index is created for INTEGER PRIMARY KEYs.
4057 */
4058 SQLITE_PRIVATE void sqlite3AddPrimaryKey(
4059 Parse *pParse, /* Parsing context */
4060 ExprList *pList, /* List of field names to be indexed */
4061 int onError, /* What to do with a uniqueness conflict */
4062 int autoInc, /* True if the AUTOINCREMENT keyword is present */
4063 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
4064 ){
4065 Table *pTab = pParse->pNewTable;
4066 char *zType = 0;
4067 int iCol = -1, i;
4068 int nTerm;
4069 if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
4070 if( pTab->tabFlags & TF_HasPrimaryKey ){
4071 sqlite3ErrorMsg(pParse,
4072 "table \"%s\" has more than one primary key", pTab->zName);
4073 goto primary_key_exit;
4074 }
4075 pTab->tabFlags |= TF_HasPrimaryKey;
4076 if( pList==0 ){
4077 iCol = pTab->nCol - 1;
4078 pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
4079 zType = pTab->aCol[iCol].zType;
4080 nTerm = 1;
4081 }else{
4082 nTerm = pList->nExpr;
4083 for(i=0; i<nTerm; i++){
4084 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
4085 assert( pCExpr!=0 );
4086 sqlite3StringToId(pCExpr);
4087 if( pCExpr->op==TK_ID ){
4088 const char *zCName = pCExpr->u.zToken;
4089 for(iCol=0; iCol<pTab->nCol; iCol++){
4090 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
4091 pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
4092 zType = pTab->aCol[iCol].zType;
4093 break;
4094 }
4095 }
4096 }
4097 }
4098 }
4099 if( nTerm==1
4100 && zType && sqlite3StrICmp(zType, "INTEGER")==0
4101 && sortOrder!=SQLITE_SO_DESC
4102 ){
4103 pTab->iPKey = iCol;
4104 pTab->keyConf = (u8)onError;
4105 assert( autoInc==0 || autoInc==1 );
4106 pTab->tabFlags |= autoInc*TF_Autoincrement;
4107 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
4108 }else if( autoInc ){
4109 #ifndef SQLITE_OMIT_AUTOINCREMENT
4110 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
4111 "INTEGER PRIMARY KEY");
4112 #endif
4113 }else{
4114 Index *p;
4115 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
4116 0, sortOrder, 0);
4117 if( p ){
4118 p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
4119 }
4120 pList = 0;
4121 }
4122
4123 primary_key_exit:
4124 sqlite3ExprListDelete(pParse->db, pList);
4125 return;
4126 }
4127
4128 /*
4129 ** Add a new CHECK constraint to the table currently under construction.
4130 */
4131 SQLITE_PRIVATE void sqlite3AddCheckConstraint(
4132 Parse *pParse, /* Parsing context */
4133 Expr *pCheckExpr /* The check expression */
4134 ){
4135 #ifndef SQLITE_OMIT_CHECK
4136 Table *pTab = pParse->pNewTable;
4137 sqlite3 *db = pParse->db;
4138 if( pTab && !IN_DECLARE_VTAB
4139 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
4140 ){
4141 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
4142 if( pParse->constraintName.n ){
4143 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
4144 }
4145 }else
4146 #endif
4147 {
4148 sqlite3ExprDelete(pParse->db, pCheckExpr);
4149 }
4150 }
4151
4152 /*
4153 ** Set the collation function of the most recently parsed table column
4154 ** to the CollSeq given.
4155 */
4156 SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
4157 Table *p;
4158 int i;
4159 char *zColl; /* Dequoted name of collation sequence */
4160 sqlite3 *db;
4161
4162 if( (p = pParse->pNewTable)==0 ) return;
4163 i = p->nCol-1;
4164 db = pParse->db;
4165 zColl = sqlite3NameFromToken(db, pToken);
4166 if( !zColl ) return;
4167
4168 if( sqlite3LocateCollSeq(pParse, zColl) ){
4169 Index *pIdx;
4170 sqlite3DbFree(db, p->aCol[i].zColl);
4171 p->aCol[i].zColl = zColl;
4172
4173 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
4174 ** then an index may have been created on this column before the
4175 ** collation type was added. Correct this if it is the case.
4176 */
4177 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
4178 assert( pIdx->nKeyCol==1 );
4179 if( pIdx->aiColumn[0]==i ){
4180 pIdx->azColl[0] = p->aCol[i].zColl;
4181 }
4182 }
4183 }else{
4184 sqlite3DbFree(db, zColl);
4185 }
4186 }
4187
4188 /*
4189 ** This function returns the collation sequence for database native text
4190 ** encoding identified by the string zName, length nName.
4191 **
4192 ** If the requested collation sequence is not available, or not available
4193 ** in the database native encoding, the collation factory is invoked to
4194 ** request it. If the collation factory does not supply such a sequence,
4195 ** and the sequence is available in another text encoding, then that is
4196 ** returned instead.
4197 **
4198 ** If no versions of the requested collations sequence are available, or
4199 ** another error occurs, NULL is returned and an error message written into
4200 ** pParse.
4201 **
4202 ** This routine is a wrapper around sqlite3FindCollSeq(). This routine
4203 ** invokes the collation factory if the named collation cannot be found
4204 ** and generates an error message.
4205 **
4206 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
4207 */
4208 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
4209 sqlite3 *db = pParse->db;
4210 u8 enc = ENC(db);
4211 u8 initbusy = db->init.busy;
4212 CollSeq *pColl;
4213
4214 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
4215 if( !initbusy && (!pColl || !pColl->xCmp) ){
4216 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
4217 }
4218
4219 return pColl;
4220 }
4221
4222
4223 /*
4224 ** Generate code that will increment the schema cookie.
4225 **
4226 ** The schema cookie is used to determine when the schema for the
4227 ** database changes. After each schema change, the cookie value
4228 ** changes. When a process first reads the schema it records the
4229 ** cookie. Thereafter, whenever it goes to access the database,
4230 ** it checks the cookie to make sure the schema has not changed
4231 ** since it was last read.
4232 **
4233 ** This plan is not completely bullet-proof. It is possible for
4234 ** the schema to change multiple times and for the cookie to be
4235 ** set back to prior value. But schema changes are infrequent
4236 ** and the probability of hitting the same cookie value is only
4237 ** 1 chance in 2^32. So we're safe enough.
4238 */
4239 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
4240 int r1 = sqlite3GetTempReg(pParse);
4241 sqlite3 *db = pParse->db;
4242 Vdbe *v = pParse->pVdbe;
4243 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
4244 sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
4245 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
4246 sqlite3ReleaseTempReg(pParse, r1);
4247 }
4248
4249 /*
4250 ** Measure the number of characters needed to output the given
4251 ** identifier. The number returned includes any quotes used
4252 ** but does not include the null terminator.
4253 **
4254 ** The estimate is conservative. It might be larger that what is
4255 ** really needed.
4256 */
4257 static int identLength(const char *z){
4258 int n;
4259 for(n=0; *z; n++, z++){
4260 if( *z=='"' ){ n++; }
4261 }
4262 return n + 2;
4263 }
4264
4265 /*
4266 ** The first parameter is a pointer to an output buffer. The second
4267 ** parameter is a pointer to an integer that contains the offset at
4268 ** which to write into the output buffer. This function copies the
4269 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
4270 ** to the specified offset in the buffer and updates *pIdx to refer
4271 ** to the first byte after the last byte written before returning.
4272 **
4273 ** If the string zSignedIdent consists entirely of alpha-numeric
4274 ** characters, does not begin with a digit and is not an SQL keyword,
4275 ** then it is copied to the output buffer exactly as it is. Otherwise,
4276 ** it is quoted using double-quotes.
4277 */
4278 static void identPut(char *z, int *pIdx, char *zSignedIdent){
4279 unsigned char *zIdent = (unsigned char*)zSignedIdent;
4280 int i, j, needQuote;
4281 i = *pIdx;
4282
4283 for(j=0; zIdent[j]; j++){
4284 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
4285 }
4286 needQuote = sqlite3Isdigit(zIdent[0])
4287 || sqlite3KeywordCode(zIdent, j)!=TK_ID
4288 || zIdent[j]!=0
4289 || j==0;
4290
4291 if( needQuote ) z[i++] = '"';
4292 for(j=0; zIdent[j]; j++){
4293 z[i++] = zIdent[j];
4294 if( zIdent[j]=='"' ) z[i++] = '"';
4295 }
4296 if( needQuote ) z[i++] = '"';
4297 z[i] = 0;
4298 *pIdx = i;
4299 }
4300
4301 /*
4302 ** Generate a CREATE TABLE statement appropriate for the given
4303 ** table. Memory to hold the text of the statement is obtained
4304 ** from sqliteMalloc() and must be freed by the calling function.
4305 */
4306 static char *createTableStmt(sqlite3 *db, Table *p){
4307 int i, k, n;
4308 char *zStmt;
4309 char *zSep, *zSep2, *zEnd;
4310 Column *pCol;
4311 n = 0;
4312 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
4313 n += identLength(pCol->zName) + 5;
4314 }
4315 n += identLength(p->zName);
4316 if( n<50 ){
4317 zSep = "";
4318 zSep2 = ",";
4319 zEnd = ")";
4320 }else{
4321 zSep = "\n ";
4322 zSep2 = ",\n ";
4323 zEnd = "\n)";
4324 }
4325 n += 35 + 6*p->nCol;
4326 zStmt = sqlite3DbMallocRaw(0, n);
4327 if( zStmt==0 ){
4328 db->mallocFailed = 1;
4329 return 0;
4330 }
4331 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
4332 k = sqlite3Strlen30(zStmt);
4333 identPut(zStmt, &k, p->zName);
4334 zStmt[k++] = '(';
4335 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
4336 static const char * const azType[] = {
4337 /* SQLITE_AFF_BLOB */ "",
4338 /* SQLITE_AFF_TEXT */ " TEXT",
4339 /* SQLITE_AFF_NUMERIC */ " NUM",
4340 /* SQLITE_AFF_INTEGER */ " INT",
4341 /* SQLITE_AFF_REAL */ " REAL"
4342 };
4343 int len;
4344 const char *zType;
4345
4346 sqlite3_snprintf(n-k, &zStmt[k], zSep);
4347 k += sqlite3Strlen30(&zStmt[k]);
4348 zSep = zSep2;
4349 identPut(zStmt, &k, pCol->zName);
4350 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
4351 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
4352 testcase( pCol->affinity==SQLITE_AFF_BLOB );
4353 testcase( pCol->affinity==SQLITE_AFF_TEXT );
4354 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
4355 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
4356 testcase( pCol->affinity==SQLITE_AFF_REAL );
4357
4358 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
4359 len = sqlite3Strlen30(zType);
4360 assert( pCol->affinity==SQLITE_AFF_BLOB
4361 || pCol->affinity==sqlite3AffinityType(zType, 0) );
4362 memcpy(&zStmt[k], zType, len);
4363 k += len;
4364 assert( k<=n );
4365 }
4366 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
4367 return zStmt;
4368 }
4369
4370 /*
4371 ** Resize an Index object to hold N columns total. Return SQLITE_OK
4372 ** on success and SQLITE_NOMEM on an OOM error.
4373 */
4374 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
4375 char *zExtra;
4376 int nByte;
4377 if( pIdx->nColumn>=N ) return SQLITE_OK;
4378 assert( pIdx->isResized==0 );
4379 nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
4380 zExtra = sqlite3DbMallocZero(db, nByte);
4381 if( zExtra==0 ) return SQLITE_NOMEM;
4382 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
4383 pIdx->azColl = (const char**)zExtra;
4384 zExtra += sizeof(char*)*N;
4385 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
4386 pIdx->aiColumn = (i16*)zExtra;
4387 zExtra += sizeof(i16)*N;
4388 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
4389 pIdx->aSortOrder = (u8*)zExtra;
4390 pIdx->nColumn = N;
4391 pIdx->isResized = 1;
4392 return SQLITE_OK;
4393 }
4394
4395 /*
4396 ** Estimate the total row width for a table.
4397 */
4398 static void estimateTableWidth(Table *pTab){
4399 unsigned wTable = 0;
4400 const Column *pTabCol;
4401 int i;
4402 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
4403 wTable += pTabCol->szEst;
4404 }
4405 if( pTab->iPKey<0 ) wTable++;
4406 pTab->szTabRow = sqlite3LogEst(wTable*4);
4407 }
4408
4409 /*
4410 ** Estimate the average size of a row for an index.
4411 */
4412 static void estimateIndexWidth(Index *pIdx){
4413 unsigned wIndex = 0;
4414 int i;
4415 const Column *aCol = pIdx->pTable->aCol;
4416 for(i=0; i<pIdx->nColumn; i++){
4417 i16 x = pIdx->aiColumn[i];
4418 assert( x<pIdx->pTable->nCol );
4419 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
4420 }
4421 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
4422 }
4423
4424 /* Return true if value x is found any of the first nCol entries of aiCol[]
4425 */
4426 static int hasColumn(const i16 *aiCol, int nCol, int x){
4427 while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
4428 return 0;
4429 }
4430
4431 /*
4432 ** This routine runs at the end of parsing a CREATE TABLE statement that
4433 ** has a WITHOUT ROWID clause. The job of this routine is to convert both
4434 ** internal schema data structures and the generated VDBE code so that they
4435 ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
4436 ** Changes include:
4437 **
4438 ** (1) Convert the OP_CreateTable into an OP_CreateIndex. There is
4439 ** no rowid btree for a WITHOUT ROWID. Instead, the canonical
4440 ** data storage is a covering index btree.
4441 ** (2) Bypass the creation of the sqlite_master table entry
4442 ** for the PRIMARY KEY as the primary key index is now
4443 ** identified by the sqlite_master table entry of the table itself.
4444 ** (3) Set the Index.tnum of the PRIMARY KEY Index object in the
4445 ** schema to the rootpage from the main table.
4446 ** (4) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
4447 ** (5) Add all table columns to the PRIMARY KEY Index object
4448 ** so that the PRIMARY KEY is a covering index. The surplus
4449 ** columns are part of KeyInfo.nXField and are not used for
4450 ** sorting or lookup or uniqueness checks.
4451 ** (6) Replace the rowid tail on all automatically generated UNIQUE
4452 ** indices with the PRIMARY KEY columns.
4453 */
4454 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
4455 Index *pIdx;
4456 Index *pPk;
4457 int nPk;
4458 int i, j;
4459 sqlite3 *db = pParse->db;
4460 Vdbe *v = pParse->pVdbe;
4461
4462 /* Convert the OP_CreateTable opcode that would normally create the
4463 ** root-page for the table into an OP_CreateIndex opcode. The index
4464 ** created will become the PRIMARY KEY index.
4465 */
4466 if( pParse->addrCrTab ){
4467 assert( v );
4468 sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex);
4469 }
4470
4471 /* Locate the PRIMARY KEY index. Or, if this table was originally
4472 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
4473 */
4474 if( pTab->iPKey>=0 ){
4475 ExprList *pList;
4476 Token ipkToken;
4477 ipkToken.z = pTab->aCol[pTab->iPKey].zName;
4478 ipkToken.n = sqlite3Strlen30(ipkToken.z);
4479 pList = sqlite3ExprListAppend(pParse, 0,
4480 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
4481 if( pList==0 ) return;
4482 pList->a[0].sortOrder = pParse->iPkSortOrder;
4483 assert( pParse->pNewTable==pTab );
4484 pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
4485 if( pPk==0 ) return;
4486 pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
4487 pTab->iPKey = -1;
4488 }else{
4489 pPk = sqlite3PrimaryKeyIndex(pTab);
4490
4491 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
4492 ** table entry. This is only required if currently generating VDBE
4493 ** code for a CREATE TABLE (not when parsing one as part of reading
4494 ** a database schema). */
4495 if( v ){
4496 assert( db->init.busy==0 );
4497 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto);
4498 }
4499
4500 /*
4501 ** Remove all redundant columns from the PRIMARY KEY. For example, change
4502 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
4503 ** code assumes the PRIMARY KEY contains no repeated columns.
4504 */
4505 for(i=j=1; i<pPk->nKeyCol; i++){
4506 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
4507 pPk->nColumn--;
4508 }else{
4509 pPk->aiColumn[j++] = pPk->aiColumn[i];
4510 }
4511 }
4512 pPk->nKeyCol = j;
4513 }
4514 pPk->isCovering = 1;
4515 assert( pPk!=0 );
4516 nPk = pPk->nKeyCol;
4517
4518 /* Make sure every column of the PRIMARY KEY is NOT NULL. (Except,
4519 ** do not enforce this for imposter tables.) */
4520 if( !db->init.imposterTable ){
4521 for(i=0; i<nPk; i++){
4522 pTab->aCol[pPk->aiColumn[i]].notNull = OE_Abort;
4523 }
4524 pPk->uniqNotNull = 1;
4525 }
4526
4527 /* The root page of the PRIMARY KEY is the table root page */
4528 pPk->tnum = pTab->tnum;
4529
4530 /* Update the in-memory representation of all UNIQUE indices by converting
4531 ** the final rowid column into one or more columns of the PRIMARY KEY.
4532 */
4533 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
4534 int n;
4535 if( IsPrimaryKeyIndex(pIdx) ) continue;
4536 for(i=n=0; i<nPk; i++){
4537 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
4538 }
4539 if( n==0 ){
4540 /* This index is a superset of the primary key */
4541 pIdx->nColumn = pIdx->nKeyCol;
4542 continue;
4543 }
4544 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
4545 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
4546 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
4547 pIdx->aiColumn[j] = pPk->aiColumn[i];
4548 pIdx->azColl[j] = pPk->azColl[i];
4549 j++;
4550 }
4551 }
4552 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
4553 assert( pIdx->nColumn>=j );
4554 }
4555
4556 /* Add all table columns to the PRIMARY KEY index
4557 */
4558 if( nPk<pTab->nCol ){
4559 if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
4560 for(i=0, j=nPk; i<pTab->nCol; i++){
4561 if( !hasColumn(pPk->aiColumn, j, i) ){
4562 assert( j<pPk->nColumn );
4563 pPk->aiColumn[j] = i;
4564 pPk->azColl[j] = sqlite3StrBINARY;
4565 j++;
4566 }
4567 }
4568 assert( pPk->nColumn==j );
4569 assert( pTab->nCol==j );
4570 }else{
4571 pPk->nColumn = pTab->nCol;
4572 }
4573 }
4574
4575 /*
4576 ** This routine is called to report the final ")" that terminates
4577 ** a CREATE TABLE statement.
4578 **
4579 ** The table structure that other action routines have been building
4580 ** is added to the internal hash tables, assuming no errors have
4581 ** occurred.
4582 **
4583 ** An entry for the table is made in the master table on disk, unless
4584 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
4585 ** it means we are reading the sqlite_master table because we just
4586 ** connected to the database or because the sqlite_master table has
4587 ** recently changed, so the entry for this table already exists in
4588 ** the sqlite_master table. We do not want to create it again.
4589 **
4590 ** If the pSelect argument is not NULL, it means that this routine
4591 ** was called to create a table generated from a
4592 ** "CREATE TABLE ... AS SELECT ..." statement. The column names of
4593 ** the new table will match the result set of the SELECT.
4594 */
4595 SQLITE_PRIVATE void sqlite3EndTable(
4596 Parse *pParse, /* Parse context */
4597 Token *pCons, /* The ',' token after the last column defn. */
4598 Token *pEnd, /* The ')' before options in the CREATE TABLE */
4599 u8 tabOpts, /* Extra table options. Usually 0. */
4600 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
4601 ){
4602 Table *p; /* The new table */
4603 sqlite3 *db = pParse->db; /* The database connection */
4604 int iDb; /* Database in which the table lives */
4605 Index *pIdx; /* An implied index of the table */
4606
4607 if( pEnd==0 && pSelect==0 ){
4608 return;
4609 }
4610 assert( !db->mallocFailed );
4611 p = pParse->pNewTable;
4612 if( p==0 ) return;
4613
4614 assert( !db->init.busy || !pSelect );
4615
4616 /* If the db->init.busy is 1 it means we are reading the SQL off the
4617 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
4618 ** So do not write to the disk again. Extract the root page number
4619 ** for the table from the db->init.newTnum field. (The page number
4620 ** should have been put there by the sqliteOpenCb routine.)
4621 */
4622 if( db->init.busy ){
4623 p->tnum = db->init.newTnum;
4624 }
4625
4626 /* Special processing for WITHOUT ROWID Tables */
4627 if( tabOpts & TF_WithoutRowid ){
4628 if( (p->tabFlags & TF_Autoincrement) ){
4629 sqlite3ErrorMsg(pParse,
4630 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
4631 return;
4632 }
4633 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
4634 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
4635 }else{
4636 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
4637 convertToWithoutRowidTable(pParse, p);
4638 }
4639 }
4640
4641 iDb = sqlite3SchemaToIndex(db, p->pSchema);
4642
4643 #ifndef SQLITE_OMIT_CHECK
4644 /* Resolve names in all CHECK constraint expressions.
4645 */
4646 if( p->pCheck ){
4647 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
4648 }
4649 #endif /* !defined(SQLITE_OMIT_CHECK) */
4650
4651 /* Estimate the average row size for the table and for all implied indices */
4652 estimateTableWidth(p);
4653 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
4654 estimateIndexWidth(pIdx);
4655 }
4656
4657 /* If not initializing, then create a record for the new table
4658 ** in the SQLITE_MASTER table of the database.
4659 **
4660 ** If this is a TEMPORARY table, write the entry into the auxiliary
4661 ** file instead of into the main database file.
4662 */
4663 if( !db->init.busy ){
4664 int n;
4665 Vdbe *v;
4666 char *zType; /* "view" or "table" */
4667 char *zType2; /* "VIEW" or "TABLE" */
4668 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
4669
4670 v = sqlite3GetVdbe(pParse);
4671 if( NEVER(v==0) ) return;
4672
4673 sqlite3VdbeAddOp1(v, OP_Close, 0);
4674
4675 /*
4676 ** Initialize zType for the new view or table.
4677 */
4678 if( p->pSelect==0 ){
4679 /* A regular table */
4680 zType = "table";
4681 zType2 = "TABLE";
4682 #ifndef SQLITE_OMIT_VIEW
4683 }else{
4684 /* A view */
4685 zType = "view";
4686 zType2 = "VIEW";
4687 #endif
4688 }
4689
4690 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
4691 ** statement to populate the new table. The root-page number for the
4692 ** new table is in register pParse->regRoot.
4693 **
4694 ** Once the SELECT has been coded by sqlite3Select(), it is in a
4695 ** suitable state to query for the column names and types to be used
4696 ** by the new table.
4697 **
4698 ** A shared-cache write-lock is not required to write to the new table,
4699 ** as a schema-lock must have already been obtained to create it. Since
4700 ** a schema-lock excludes all other database users, the write-lock would
4701 ** be redundant.
4702 */
4703 if( pSelect ){
4704 SelectDest dest; /* Where the SELECT should store results */
4705 int regYield; /* Register holding co-routine entry-point */
4706 int addrTop; /* Top of the co-routine */
4707 int regRec; /* A record to be insert into the new table */
4708 int regRowid; /* Rowid of the next row to insert */
4709 int addrInsLoop; /* Top of the loop for inserting rows */
4710 Table *pSelTab; /* A table that describes the SELECT results */
4711
4712 regYield = ++pParse->nMem;
4713 regRec = ++pParse->nMem;
4714 regRowid = ++pParse->nMem;
4715 assert(pParse->nTab==1);
4716 sqlite3MayAbort(pParse);
4717 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
4718 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
4719 pParse->nTab = 2;
4720 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
4721 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
4722 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
4723 sqlite3Select(pParse, pSelect, &dest);
4724 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
4725 sqlite3VdbeJumpHere(v, addrTop - 1);
4726 if( pParse->nErr ) return;
4727 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
4728 if( pSelTab==0 ) return;
4729 assert( p->aCol==0 );
4730 p->nCol = pSelTab->nCol;
4731 p->aCol = pSelTab->aCol;
4732 pSelTab->nCol = 0;
4733 pSelTab->aCol = 0;
4734 sqlite3DeleteTable(db, pSelTab);
4735 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
4736 VdbeCoverage(v);
4737 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
4738 sqlite3TableAffinity(v, p, 0);
4739 sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
4740 sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
4741 sqlite3VdbeGoto(v, addrInsLoop);
4742 sqlite3VdbeJumpHere(v, addrInsLoop);
4743 sqlite3VdbeAddOp1(v, OP_Close, 1);
4744 }
4745
4746 /* Compute the complete text of the CREATE statement */
4747 if( pSelect ){
4748 zStmt = createTableStmt(db, p);
4749 }else{
4750 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
4751 n = (int)(pEnd2->z - pParse->sNameToken.z);
4752 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
4753 zStmt = sqlite3MPrintf(db,
4754 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
4755 );
4756 }
4757
4758 /* A slot for the record has already been allocated in the
4759 ** SQLITE_MASTER table. We just need to update that slot with all
4760 ** the information we've collected.
4761 */
4762 sqlite3NestedParse(pParse,
4763 "UPDATE %Q.%s "
4764 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
4765 "WHERE rowid=#%d",
4766 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
4767 zType,
4768 p->zName,
4769 p->zName,
4770 pParse->regRoot,
4771 zStmt,
4772 pParse->regRowid
4773 );
4774 sqlite3DbFree(db, zStmt);
4775 sqlite3ChangeCookie(pParse, iDb);
4776
4777 #ifndef SQLITE_OMIT_AUTOINCREMENT
4778 /* Check to see if we need to create an sqlite_sequence table for
4779 ** keeping track of autoincrement keys.
4780 */
4781 if( p->tabFlags & TF_Autoincrement ){
4782 Db *pDb = &db->aDb[iDb];
4783 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
4784 if( pDb->pSchema->pSeqTab==0 ){
4785 sqlite3NestedParse(pParse,
4786 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
4787 pDb->zName
4788 );
4789 }
4790 }
4791 #endif
4792
4793 /* Reparse everything to update our internal data structures */
4794 sqlite3VdbeAddParseSchemaOp(v, iDb,
4795 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
4796 }
4797
4798
4799 /* Add the table to the in-memory representation of the database.
4800 */
4801 if( db->init.busy ){
4802 Table *pOld;
4803 Schema *pSchema = p->pSchema;
4804 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
4805 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
4806 if( pOld ){
4807 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
4808 db->mallocFailed = 1;
4809 return;
4810 }
4811 pParse->pNewTable = 0;
4812 db->flags |= SQLITE_InternChanges;
4813
4814 #ifndef SQLITE_OMIT_ALTERTABLE
4815 if( !p->pSelect ){
4816 const char *zName = (const char *)pParse->sNameToken.z;
4817 int nName;
4818 assert( !pSelect && pCons && pEnd );
4819 if( pCons->z==0 ){
4820 pCons = pEnd;
4821 }
4822 nName = (int)((const char *)pCons->z - zName);
4823 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
4824 }
4825 #endif
4826 }
4827 }
4828
4829 #ifndef SQLITE_OMIT_VIEW
4830 /*
4831 ** The parser calls this routine in order to create a new VIEW
4832 */
4833 SQLITE_PRIVATE void sqlite3CreateView(
4834 Parse *pParse, /* The parsing context */
4835 Token *pBegin, /* The CREATE token that begins the statement */
4836 Token *pName1, /* The token that holds the name of the view */
4837 Token *pName2, /* The token that holds the name of the view */
4838 ExprList *pCNames, /* Optional list of view column names */
4839 Select *pSelect, /* A SELECT statement that will become the new view */
4840 int isTemp, /* TRUE for a TEMPORARY view */
4841 int noErr /* Suppress error messages if VIEW already exists */
4842 ){
4843 Table *p;
4844 int n;
4845 const char *z;
4846 Token sEnd;
4847 DbFixer sFix;
4848 Token *pName = 0;
4849 int iDb;
4850 sqlite3 *db = pParse->db;
4851
4852 if( pParse->nVar>0 ){
4853 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
4854 goto create_view_fail;
4855 }
4856 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
4857 p = pParse->pNewTable;
4858 if( p==0 || pParse->nErr ) goto create_view_fail;
4859 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
4860 iDb = sqlite3SchemaToIndex(db, p->pSchema);
4861 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
4862 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
4863
4864 /* Make a copy of the entire SELECT statement that defines the view.
4865 ** This will force all the Expr.token.z values to be dynamically
4866 ** allocated rather than point to the input string - which means that
4867 ** they will persist after the current sqlite3_exec() call returns.
4868 */
4869 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
4870 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
4871 if( db->mallocFailed ) goto create_view_fail;
4872
4873 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
4874 ** the end.
4875 */
4876 sEnd = pParse->sLastToken;
4877 assert( sEnd.z[0]!=0 );
4878 if( sEnd.z[0]!=';' ){
4879 sEnd.z += sEnd.n;
4880 }
4881 sEnd.n = 0;
4882 n = (int)(sEnd.z - pBegin->z);
4883 assert( n>0 );
4884 z = pBegin->z;
4885 while( sqlite3Isspace(z[n-1]) ){ n--; }
4886 sEnd.z = &z[n-1];
4887 sEnd.n = 1;
4888
4889 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
4890 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
4891
4892 create_view_fail:
4893 sqlite3SelectDelete(db, pSelect);
4894 sqlite3ExprListDelete(db, pCNames);
4895 return;
4896 }
4897 #endif /* SQLITE_OMIT_VIEW */
4898
4899 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
4900 /*
4901 ** The Table structure pTable is really a VIEW. Fill in the names of
4902 ** the columns of the view in the pTable structure. Return the number
4903 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
4904 */
4905 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
4906 Table *pSelTab; /* A fake table from which we get the result set */
4907 Select *pSel; /* Copy of the SELECT that implements the view */
4908 int nErr = 0; /* Number of errors encountered */
4909 int n; /* Temporarily holds the number of cursors assigned */
4910 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
4911 sqlite3_xauth xAuth; /* Saved xAuth pointer */
4912 u8 bEnabledLA; /* Saved db->lookaside.bEnabled state */
4913
4914 assert( pTable );
4915
4916 #ifndef SQLITE_OMIT_VIRTUALTABLE
4917 if( sqlite3VtabCallConnect(pParse, pTable) ){
4918 return SQLITE_ERROR;
4919 }
4920 if( IsVirtual(pTable) ) return 0;
4921 #endif
4922
4923 #ifndef SQLITE_OMIT_VIEW
4924 /* A positive nCol means the columns names for this view are
4925 ** already known.
4926 */
4927 if( pTable->nCol>0 ) return 0;
4928
4929 /* A negative nCol is a special marker meaning that we are currently
4930 ** trying to compute the column names. If we enter this routine with
4931 ** a negative nCol, it means two or more views form a loop, like this:
4932 **
4933 ** CREATE VIEW one AS SELECT * FROM two;
4934 ** CREATE VIEW two AS SELECT * FROM one;
4935 **
4936 ** Actually, the error above is now caught prior to reaching this point.
4937 ** But the following test is still important as it does come up
4938 ** in the following:
4939 **
4940 ** CREATE TABLE main.ex1(a);
4941 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
4942 ** SELECT * FROM temp.ex1;
4943 */
4944 if( pTable->nCol<0 ){
4945 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
4946 return 1;
4947 }
4948 assert( pTable->nCol>=0 );
4949
4950 /* If we get this far, it means we need to compute the table names.
4951 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
4952 ** "*" elements in the results set of the view and will assign cursors
4953 ** to the elements of the FROM clause. But we do not want these changes
4954 ** to be permanent. So the computation is done on a copy of the SELECT
4955 ** statement that defines the view.
4956 */
4957 assert( pTable->pSelect );
4958 bEnabledLA = db->lookaside.bEnabled;
4959 if( pTable->pCheck ){
4960 db->lookaside.bEnabled = 0;
4961 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
4962 &pTable->nCol, &pTable->aCol);
4963 }else{
4964 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
4965 if( pSel ){
4966 n = pParse->nTab;
4967 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
4968 pTable->nCol = -1;
4969 db->lookaside.bEnabled = 0;
4970 #ifndef SQLITE_OMIT_AUTHORIZATION
4971 xAuth = db->xAuth;
4972 db->xAuth = 0;
4973 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
4974 db->xAuth = xAuth;
4975 #else
4976 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
4977 #endif
4978 pParse->nTab = n;
4979 if( pSelTab ){
4980 assert( pTable->aCol==0 );
4981 pTable->nCol = pSelTab->nCol;
4982 pTable->aCol = pSelTab->aCol;
4983 pSelTab->nCol = 0;
4984 pSelTab->aCol = 0;
4985 sqlite3DeleteTable(db, pSelTab);
4986 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
4987 }else{
4988 pTable->nCol = 0;
4989 nErr++;
4990 }
4991 sqlite3SelectDelete(db, pSel);
4992 } else {
4993 nErr++;
4994 }
4995 }
4996 db->lookaside.bEnabled = bEnabledLA;
4997 pTable->pSchema->schemaFlags |= DB_UnresetViews;
4998 #endif /* SQLITE_OMIT_VIEW */
4999 return nErr;
5000 }
5001 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
5002
5003 #ifndef SQLITE_OMIT_VIEW
5004 /*
5005 ** Clear the column names from every VIEW in database idx.
5006 */
5007 static void sqliteViewResetAll(sqlite3 *db, int idx){
5008 HashElem *i;
5009 assert( sqlite3SchemaMutexHeld(db, idx, 0) );
5010 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
5011 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
5012 Table *pTab = sqliteHashData(i);
5013 if( pTab->pSelect ){
5014 sqlite3DeleteColumnNames(db, pTab);
5015 pTab->aCol = 0;
5016 pTab->nCol = 0;
5017 }
5018 }
5019 DbClearProperty(db, idx, DB_UnresetViews);
5020 }
5021 #else
5022 # define sqliteViewResetAll(A,B)
5023 #endif /* SQLITE_OMIT_VIEW */
5024
5025 /*
5026 ** This function is called by the VDBE to adjust the internal schema
5027 ** used by SQLite when the btree layer moves a table root page. The
5028 ** root-page of a table or index in database iDb has changed from iFrom
5029 ** to iTo.
5030 **
5031 ** Ticket #1728: The symbol table might still contain information
5032 ** on tables and/or indices that are the process of being deleted.
5033 ** If you are unlucky, one of those deleted indices or tables might
5034 ** have the same rootpage number as the real table or index that is
5035 ** being moved. So we cannot stop searching after the first match
5036 ** because the first match might be for one of the deleted indices
5037 ** or tables and not the table/index that is actually being moved.
5038 ** We must continue looping until all tables and indices with
5039 ** rootpage==iFrom have been converted to have a rootpage of iTo
5040 ** in order to be certain that we got the right one.
5041 */
5042 #ifndef SQLITE_OMIT_AUTOVACUUM
5043 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iT o){
5044 HashElem *pElem;
5045 Hash *pHash;
5046 Db *pDb;
5047
5048 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
5049 pDb = &db->aDb[iDb];
5050 pHash = &pDb->pSchema->tblHash;
5051 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
5052 Table *pTab = sqliteHashData(pElem);
5053 if( pTab->tnum==iFrom ){
5054 pTab->tnum = iTo;
5055 }
5056 }
5057 pHash = &pDb->pSchema->idxHash;
5058 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
5059 Index *pIdx = sqliteHashData(pElem);
5060 if( pIdx->tnum==iFrom ){
5061 pIdx->tnum = iTo;
5062 }
5063 }
5064 }
5065 #endif
5066
5067 /*
5068 ** Write code to erase the table with root-page iTable from database iDb.
5069 ** Also write code to modify the sqlite_master table and internal schema
5070 ** if a root-page of another table is moved by the btree-layer whilst
5071 ** erasing iTable (this can happen with an auto-vacuum database).
5072 */
5073 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
5074 Vdbe *v = sqlite3GetVdbe(pParse);
5075 int r1 = sqlite3GetTempReg(pParse);
5076 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
5077 sqlite3MayAbort(pParse);
5078 #ifndef SQLITE_OMIT_AUTOVACUUM
5079 /* OP_Destroy stores an in integer r1. If this integer
5080 ** is non-zero, then it is the root page number of a table moved to
5081 ** location iTable. The following code modifies the sqlite_master table to
5082 ** reflect this.
5083 **
5084 ** The "#NNN" in the SQL is a special constant that means whatever value
5085 ** is in register NNN. See grammar rules associated with the TK_REGISTER
5086 ** token for additional information.
5087 */
5088 sqlite3NestedParse(pParse,
5089 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
5090 pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
5091 #endif
5092 sqlite3ReleaseTempReg(pParse, r1);
5093 }
5094
5095 /*
5096 ** Write VDBE code to erase table pTab and all associated indices on disk.
5097 ** Code to update the sqlite_master tables and internal schema definitions
5098 ** in case a root-page belonging to another table is moved by the btree layer
5099 ** is also added (this can happen with an auto-vacuum database).
5100 */
5101 static void destroyTable(Parse *pParse, Table *pTab){
5102 #ifdef SQLITE_OMIT_AUTOVACUUM
5103 Index *pIdx;
5104 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
5105 destroyRootPage(pParse, pTab->tnum, iDb);
5106 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
5107 destroyRootPage(pParse, pIdx->tnum, iDb);
5108 }
5109 #else
5110 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
5111 ** is not defined), then it is important to call OP_Destroy on the
5112 ** table and index root-pages in order, starting with the numerically
5113 ** largest root-page number. This guarantees that none of the root-pages
5114 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
5115 ** following were coded:
5116 **
5117 ** OP_Destroy 4 0
5118 ** ...
5119 ** OP_Destroy 5 0
5120 **
5121 ** and root page 5 happened to be the largest root-page number in the
5122 ** database, then root page 5 would be moved to page 4 by the
5123 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
5124 ** a free-list page.
5125 */
5126 int iTab = pTab->tnum;
5127 int iDestroyed = 0;
5128
5129 while( 1 ){
5130 Index *pIdx;
5131 int iLargest = 0;
5132
5133 if( iDestroyed==0 || iTab<iDestroyed ){
5134 iLargest = iTab;
5135 }
5136 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
5137 int iIdx = pIdx->tnum;
5138 assert( pIdx->pSchema==pTab->pSchema );
5139 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
5140 iLargest = iIdx;
5141 }
5142 }
5143 if( iLargest==0 ){
5144 return;
5145 }else{
5146 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
5147 assert( iDb>=0 && iDb<pParse->db->nDb );
5148 destroyRootPage(pParse, iLargest, iDb);
5149 iDestroyed = iLargest;
5150 }
5151 }
5152 #endif
5153 }
5154
5155 /*
5156 ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
5157 ** after a DROP INDEX or DROP TABLE command.
5158 */
5159 static void sqlite3ClearStatTables(
5160 Parse *pParse, /* The parsing context */
5161 int iDb, /* The database number */
5162 const char *zType, /* "idx" or "tbl" */
5163 const char *zName /* Name of index or table */
5164 ){
5165 int i;
5166 const char *zDbName = pParse->db->aDb[iDb].zName;
5167 for(i=1; i<=4; i++){
5168 char zTab[24];
5169 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
5170 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
5171 sqlite3NestedParse(pParse,
5172 "DELETE FROM %Q.%s WHERE %s=%Q",
5173 zDbName, zTab, zType, zName
5174 );
5175 }
5176 }
5177 }
5178
5179 /*
5180 ** Generate code to drop a table.
5181 */
5182 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, in t isView){
5183 Vdbe *v;
5184 sqlite3 *db = pParse->db;
5185 Trigger *pTrigger;
5186 Db *pDb = &db->aDb[iDb];
5187
5188 v = sqlite3GetVdbe(pParse);
5189 assert( v!=0 );
5190 sqlite3BeginWriteOperation(pParse, 1, iDb);
5191
5192 #ifndef SQLITE_OMIT_VIRTUALTABLE
5193 if( IsVirtual(pTab) ){
5194 sqlite3VdbeAddOp0(v, OP_VBegin);
5195 }
5196 #endif
5197
5198 /* Drop all triggers associated with the table being dropped. Code
5199 ** is generated to remove entries from sqlite_master and/or
5200 ** sqlite_temp_master if required.
5201 */
5202 pTrigger = sqlite3TriggerList(pParse, pTab);
5203 while( pTrigger ){
5204 assert( pTrigger->pSchema==pTab->pSchema ||
5205 pTrigger->pSchema==db->aDb[1].pSchema );
5206 sqlite3DropTriggerPtr(pParse, pTrigger);
5207 pTrigger = pTrigger->pNext;
5208 }
5209
5210 #ifndef SQLITE_OMIT_AUTOINCREMENT
5211 /* Remove any entries of the sqlite_sequence table associated with
5212 ** the table being dropped. This is done before the table is dropped
5213 ** at the btree level, in case the sqlite_sequence table needs to
5214 ** move as a result of the drop (can happen in auto-vacuum mode).
5215 */
5216 if( pTab->tabFlags & TF_Autoincrement ){
5217 sqlite3NestedParse(pParse,
5218 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
5219 pDb->zName, pTab->zName
5220 );
5221 }
5222 #endif
5223
5224 /* Drop all SQLITE_MASTER table and index entries that refer to the
5225 ** table. The program name loops through the master table and deletes
5226 ** every row that refers to a table of the same name as the one being
5227 ** dropped. Triggers are handled separately because a trigger can be
5228 ** created in the temp database that refers to a table in another
5229 ** database.
5230 */
5231 sqlite3NestedParse(pParse,
5232 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
5233 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
5234 if( !isView && !IsVirtual(pTab) ){
5235 destroyTable(pParse, pTab);
5236 }
5237
5238 /* Remove the table entry from SQLite's internal schema and modify
5239 ** the schema cookie.
5240 */
5241 if( IsVirtual(pTab) ){
5242 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
5243 }
5244 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
5245 sqlite3ChangeCookie(pParse, iDb);
5246 sqliteViewResetAll(db, iDb);
5247 }
5248
5249 /*
5250 ** This routine is called to do the work of a DROP TABLE statement.
5251 ** pName is the name of the table to be dropped.
5252 */
5253 SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
5254 Table *pTab;
5255 Vdbe *v;
5256 sqlite3 *db = pParse->db;
5257 int iDb;
5258
5259 if( db->mallocFailed ){
5260 goto exit_drop_table;
5261 }
5262 assert( pParse->nErr==0 );
5263 assert( pName->nSrc==1 );
5264 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
5265 if( noErr ) db->suppressErr++;
5266 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
5267 if( noErr ) db->suppressErr--;
5268
5269 if( pTab==0 ){
5270 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
5271 goto exit_drop_table;
5272 }
5273 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
5274 assert( iDb>=0 && iDb<db->nDb );
5275
5276 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
5277 ** it is initialized.
5278 */
5279 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
5280 goto exit_drop_table;
5281 }
5282 #ifndef SQLITE_OMIT_AUTHORIZATION
5283 {
5284 int code;
5285 const char *zTab = SCHEMA_TABLE(iDb);
5286 const char *zDb = db->aDb[iDb].zName;
5287 const char *zArg2 = 0;
5288 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
5289 goto exit_drop_table;
5290 }
5291 if( isView ){
5292 if( !OMIT_TEMPDB && iDb==1 ){
5293 code = SQLITE_DROP_TEMP_VIEW;
5294 }else{
5295 code = SQLITE_DROP_VIEW;
5296 }
5297 #ifndef SQLITE_OMIT_VIRTUALTABLE
5298 }else if( IsVirtual(pTab) ){
5299 code = SQLITE_DROP_VTABLE;
5300 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
5301 #endif
5302 }else{
5303 if( !OMIT_TEMPDB && iDb==1 ){
5304 code = SQLITE_DROP_TEMP_TABLE;
5305 }else{
5306 code = SQLITE_DROP_TABLE;
5307 }
5308 }
5309 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
5310 goto exit_drop_table;
5311 }
5312 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
5313 goto exit_drop_table;
5314 }
5315 }
5316 #endif
5317 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
5318 && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
5319 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
5320 goto exit_drop_table;
5321 }
5322
5323 #ifndef SQLITE_OMIT_VIEW
5324 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
5325 ** on a table.
5326 */
5327 if( isView && pTab->pSelect==0 ){
5328 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
5329 goto exit_drop_table;
5330 }
5331 if( !isView && pTab->pSelect ){
5332 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
5333 goto exit_drop_table;
5334 }
5335 #endif
5336
5337 /* Generate code to remove the table from the master table
5338 ** on disk.
5339 */
5340 v = sqlite3GetVdbe(pParse);
5341 if( v ){
5342 sqlite3BeginWriteOperation(pParse, 1, iDb);
5343 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
5344 sqlite3FkDropTable(pParse, pName, pTab);
5345 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
5346 }
5347
5348 exit_drop_table:
5349 sqlite3SrcListDelete(db, pName);
5350 }
5351
5352 /*
5353 ** This routine is called to create a new foreign key on the table
5354 ** currently under construction. pFromCol determines which columns
5355 ** in the current table point to the foreign key. If pFromCol==0 then
5356 ** connect the key to the last column inserted. pTo is the name of
5357 ** the table referred to (a.k.a the "parent" table). pToCol is a list
5358 ** of tables in the parent pTo table. flags contains all
5359 ** information about the conflict resolution algorithms specified
5360 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
5361 **
5362 ** An FKey structure is created and added to the table currently
5363 ** under construction in the pParse->pNewTable field.
5364 **
5365 ** The foreign key is set for IMMEDIATE processing. A subsequent call
5366 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
5367 */
5368 SQLITE_PRIVATE void sqlite3CreateForeignKey(
5369 Parse *pParse, /* Parsing context */
5370 ExprList *pFromCol, /* Columns in this table that point to other table */
5371 Token *pTo, /* Name of the other table */
5372 ExprList *pToCol, /* Columns in the other table */
5373 int flags /* Conflict resolution algorithms. */
5374 ){
5375 sqlite3 *db = pParse->db;
5376 #ifndef SQLITE_OMIT_FOREIGN_KEY
5377 FKey *pFKey = 0;
5378 FKey *pNextTo;
5379 Table *p = pParse->pNewTable;
5380 int nByte;
5381 int i;
5382 int nCol;
5383 char *z;
5384
5385 assert( pTo!=0 );
5386 if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
5387 if( pFromCol==0 ){
5388 int iCol = p->nCol-1;
5389 if( NEVER(iCol<0) ) goto fk_end;
5390 if( pToCol && pToCol->nExpr!=1 ){
5391 sqlite3ErrorMsg(pParse, "foreign key on %s"
5392 " should reference only one column of table %T",
5393 p->aCol[iCol].zName, pTo);
5394 goto fk_end;
5395 }
5396 nCol = 1;
5397 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
5398 sqlite3ErrorMsg(pParse,
5399 "number of columns in foreign key does not match the number of "
5400 "columns in the referenced table");
5401 goto fk_end;
5402 }else{
5403 nCol = pFromCol->nExpr;
5404 }
5405 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
5406 if( pToCol ){
5407 for(i=0; i<pToCol->nExpr; i++){
5408 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
5409 }
5410 }
5411 pFKey = sqlite3DbMallocZero(db, nByte );
5412 if( pFKey==0 ){
5413 goto fk_end;
5414 }
5415 pFKey->pFrom = p;
5416 pFKey->pNextFrom = p->pFKey;
5417 z = (char*)&pFKey->aCol[nCol];
5418 pFKey->zTo = z;
5419 memcpy(z, pTo->z, pTo->n);
5420 z[pTo->n] = 0;
5421 sqlite3Dequote(z);
5422 z += pTo->n+1;
5423 pFKey->nCol = nCol;
5424 if( pFromCol==0 ){
5425 pFKey->aCol[0].iFrom = p->nCol-1;
5426 }else{
5427 for(i=0; i<nCol; i++){
5428 int j;
5429 for(j=0; j<p->nCol; j++){
5430 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
5431 pFKey->aCol[i].iFrom = j;
5432 break;
5433 }
5434 }
5435 if( j>=p->nCol ){
5436 sqlite3ErrorMsg(pParse,
5437 "unknown column \"%s\" in foreign key definition",
5438 pFromCol->a[i].zName);
5439 goto fk_end;
5440 }
5441 }
5442 }
5443 if( pToCol ){
5444 for(i=0; i<nCol; i++){
5445 int n = sqlite3Strlen30(pToCol->a[i].zName);
5446 pFKey->aCol[i].zCol = z;
5447 memcpy(z, pToCol->a[i].zName, n);
5448 z[n] = 0;
5449 z += n+1;
5450 }
5451 }
5452 pFKey->isDeferred = 0;
5453 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
5454 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
5455
5456 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
5457 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
5458 pFKey->zTo, (void *)pFKey
5459 );
5460 if( pNextTo==pFKey ){
5461 db->mallocFailed = 1;
5462 goto fk_end;
5463 }
5464 if( pNextTo ){
5465 assert( pNextTo->pPrevTo==0 );
5466 pFKey->pNextTo = pNextTo;
5467 pNextTo->pPrevTo = pFKey;
5468 }
5469
5470 /* Link the foreign key to the table as the last step.
5471 */
5472 p->pFKey = pFKey;
5473 pFKey = 0;
5474
5475 fk_end:
5476 sqlite3DbFree(db, pFKey);
5477 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
5478 sqlite3ExprListDelete(db, pFromCol);
5479 sqlite3ExprListDelete(db, pToCol);
5480 }
5481
5482 /*
5483 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
5484 ** clause is seen as part of a foreign key definition. The isDeferred
5485 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
5486 ** The behavior of the most recently created foreign key is adjusted
5487 ** accordingly.
5488 */
5489 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
5490 #ifndef SQLITE_OMIT_FOREIGN_KEY
5491 Table *pTab;
5492 FKey *pFKey;
5493 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
5494 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
5495 pFKey->isDeferred = (u8)isDeferred;
5496 #endif
5497 }
5498
5499 /*
5500 ** Generate code that will erase and refill index *pIdx. This is
5501 ** used to initialize a newly created index or to recompute the
5502 ** content of an index in response to a REINDEX command.
5503 **
5504 ** if memRootPage is not negative, it means that the index is newly
5505 ** created. The register specified by memRootPage contains the
5506 ** root page number of the index. If memRootPage is negative, then
5507 ** the index already exists and must be cleared before being refilled and
5508 ** the root page number of the index is taken from pIndex->tnum.
5509 */
5510 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
5511 Table *pTab = pIndex->pTable; /* The table that is indexed */
5512 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
5513 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
5514 int iSorter; /* Cursor opened by OpenSorter (if in use) */
5515 int addr1; /* Address of top of loop */
5516 int addr2; /* Address to jump to for next iteration */
5517 int tnum; /* Root page of index */
5518 int iPartIdxLabel; /* Jump to this label to skip a row */
5519 Vdbe *v; /* Generate code into this virtual machine */
5520 KeyInfo *pKey; /* KeyInfo for index */
5521 int regRecord; /* Register holding assembled index record */
5522 sqlite3 *db = pParse->db; /* The database connection */
5523 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
5524
5525 #ifndef SQLITE_OMIT_AUTHORIZATION
5526 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
5527 db->aDb[iDb].zName ) ){
5528 return;
5529 }
5530 #endif
5531
5532 /* Require a write-lock on the table to perform this operation */
5533 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
5534
5535 v = sqlite3GetVdbe(pParse);
5536 if( v==0 ) return;
5537 if( memRootPage>=0 ){
5538 tnum = memRootPage;
5539 }else{
5540 tnum = pIndex->tnum;
5541 }
5542 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
5543
5544 /* Open the sorter cursor if we are to use one. */
5545 iSorter = pParse->nTab++;
5546 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
5547 sqlite3KeyInfoRef(pKey), P4_KEYINFO);
5548
5549 /* Open the table. Loop through all rows of the table, inserting index
5550 ** records into the sorter. */
5551 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
5552 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
5553 regRecord = sqlite3GetTempReg(pParse);
5554
5555 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
5556 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
5557 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
5558 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
5559 sqlite3VdbeJumpHere(v, addr1);
5560 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
5561 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
5562 (char *)pKey, P4_KEYINFO);
5563 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
5564
5565 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
5566 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
5567 if( IsUniqueIndex(pIndex) && pKey!=0 ){
5568 int j2 = sqlite3VdbeCurrentAddr(v) + 3;
5569 sqlite3VdbeGoto(v, j2);
5570 addr2 = sqlite3VdbeCurrentAddr(v);
5571 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
5572 pIndex->nKeyCol); VdbeCoverage(v);
5573 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
5574 }else{
5575 addr2 = sqlite3VdbeCurrentAddr(v);
5576 }
5577 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
5578 sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
5579 sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
5580 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
5581 sqlite3ReleaseTempReg(pParse, regRecord);
5582 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
5583 sqlite3VdbeJumpHere(v, addr1);
5584
5585 sqlite3VdbeAddOp1(v, OP_Close, iTab);
5586 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
5587 sqlite3VdbeAddOp1(v, OP_Close, iSorter);
5588 }
5589
5590 /*
5591 ** Allocate heap space to hold an Index object with nCol columns.
5592 **
5593 ** Increase the allocation size to provide an extra nExtra bytes
5594 ** of 8-byte aligned space after the Index object and return a
5595 ** pointer to this extra space in *ppExtra.
5596 */
5597 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
5598 sqlite3 *db, /* Database connection */
5599 i16 nCol, /* Total number of columns in the index */
5600 int nExtra, /* Number of bytes of extra space to alloc */
5601 char **ppExtra /* Pointer to the "extra" space */
5602 ){
5603 Index *p; /* Allocated index object */
5604 int nByte; /* Bytes of space for Index object + arrays */
5605
5606 nByte = ROUND8(sizeof(Index)) + /* Index structure */
5607 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
5608 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
5609 sizeof(i16)*nCol + /* Index.aiColumn */
5610 sizeof(u8)*nCol); /* Index.aSortOrder */
5611 p = sqlite3DbMallocZero(db, nByte + nExtra);
5612 if( p ){
5613 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
5614 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
5615 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
5616 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
5617 p->aSortOrder = (u8*)pExtra;
5618 p->nColumn = nCol;
5619 p->nKeyCol = nCol - 1;
5620 *ppExtra = ((char*)p) + nByte;
5621 }
5622 return p;
5623 }
5624
5625 /*
5626 ** Create a new index for an SQL table. pName1.pName2 is the name of the index
5627 ** and pTblList is the name of the table that is to be indexed. Both will
5628 ** be NULL for a primary key or an index that is created to satisfy a
5629 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
5630 ** as the table to be indexed. pParse->pNewTable is a table that is
5631 ** currently being constructed by a CREATE TABLE statement.
5632 **
5633 ** pList is a list of columns to be indexed. pList will be NULL if this
5634 ** is a primary key or unique-constraint on the most recent column added
5635 ** to the table currently under construction.
5636 **
5637 ** If the index is created successfully, return a pointer to the new Index
5638 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
5639 ** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY)
5640 */
5641 SQLITE_PRIVATE Index *sqlite3CreateIndex(
5642 Parse *pParse, /* All information about this parse */
5643 Token *pName1, /* First part of index name. May be NULL */
5644 Token *pName2, /* Second part of index name. May be NULL */
5645 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
5646 ExprList *pList, /* A list of columns to be indexed */
5647 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
5648 Token *pStart, /* The CREATE token that begins this statement */
5649 Expr *pPIWhere, /* WHERE clause for partial indices */
5650 int sortOrder, /* Sort order of primary key when pList==NULL */
5651 int ifNotExist /* Omit error if index already exists */
5652 ){
5653 Index *pRet = 0; /* Pointer to return */
5654 Table *pTab = 0; /* Table to be indexed */
5655 Index *pIndex = 0; /* The index to be created */
5656 char *zName = 0; /* Name of the index */
5657 int nName; /* Number of characters in zName */
5658 int i, j;
5659 DbFixer sFix; /* For assigning database names to pTable */
5660 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
5661 sqlite3 *db = pParse->db;
5662 Db *pDb; /* The specific table containing the indexed database */
5663 int iDb; /* Index of the database that is being written */
5664 Token *pName = 0; /* Unqualified name of the index to create */
5665 struct ExprList_item *pListItem; /* For looping over pList */
5666 int nExtra = 0; /* Space allocated for zExtra[] */
5667 int nExtraCol; /* Number of extra columns needed */
5668 char *zExtra = 0; /* Extra space after the Index object */
5669 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
5670
5671 if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){
5672 goto exit_create_index;
5673 }
5674 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
5675 goto exit_create_index;
5676 }
5677
5678 /*
5679 ** Find the table that is to be indexed. Return early if not found.
5680 */
5681 if( pTblName!=0 ){
5682
5683 /* Use the two-part index name to determine the database
5684 ** to search for the table. 'Fix' the table name to this db
5685 ** before looking up the table.
5686 */
5687 assert( pName1 && pName2 );
5688 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
5689 if( iDb<0 ) goto exit_create_index;
5690 assert( pName && pName->z );
5691
5692 #ifndef SQLITE_OMIT_TEMPDB
5693 /* If the index name was unqualified, check if the table
5694 ** is a temp table. If so, set the database to 1. Do not do this
5695 ** if initialising a database schema.
5696 */
5697 if( !db->init.busy ){
5698 pTab = sqlite3SrcListLookup(pParse, pTblName);
5699 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
5700 iDb = 1;
5701 }
5702 }
5703 #endif
5704
5705 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
5706 if( sqlite3FixSrcList(&sFix, pTblName) ){
5707 /* Because the parser constructs pTblName from a single identifier,
5708 ** sqlite3FixSrcList can never fail. */
5709 assert(0);
5710 }
5711 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
5712 assert( db->mallocFailed==0 || pTab==0 );
5713 if( pTab==0 ) goto exit_create_index;
5714 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
5715 sqlite3ErrorMsg(pParse,
5716 "cannot create a TEMP index on non-TEMP table \"%s\"",
5717 pTab->zName);
5718 goto exit_create_index;
5719 }
5720 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
5721 }else{
5722 assert( pName==0 );
5723 assert( pStart==0 );
5724 pTab = pParse->pNewTable;
5725 if( !pTab ) goto exit_create_index;
5726 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
5727 }
5728 pDb = &db->aDb[iDb];
5729
5730 assert( pTab!=0 );
5731 assert( pParse->nErr==0 );
5732 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
5733 && db->init.busy==0
5734 #if SQLITE_USER_AUTHENTICATION
5735 && sqlite3UserAuthTable(pTab->zName)==0
5736 #endif
5737 && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
5738 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
5739 goto exit_create_index;
5740 }
5741 #ifndef SQLITE_OMIT_VIEW
5742 if( pTab->pSelect ){
5743 sqlite3ErrorMsg(pParse, "views may not be indexed");
5744 goto exit_create_index;
5745 }
5746 #endif
5747 #ifndef SQLITE_OMIT_VIRTUALTABLE
5748 if( IsVirtual(pTab) ){
5749 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
5750 goto exit_create_index;
5751 }
5752 #endif
5753
5754 /*
5755 ** Find the name of the index. Make sure there is not already another
5756 ** index or table with the same name.
5757 **
5758 ** Exception: If we are reading the names of permanent indices from the
5759 ** sqlite_master table (because some other process changed the schema) and
5760 ** one of the index names collides with the name of a temporary table or
5761 ** index, then we will continue to process this index.
5762 **
5763 ** If pName==0 it means that we are
5764 ** dealing with a primary key or UNIQUE constraint. We have to invent our
5765 ** own name.
5766 */
5767 if( pName ){
5768 zName = sqlite3NameFromToken(db, pName);
5769 if( zName==0 ) goto exit_create_index;
5770 assert( pName->z!=0 );
5771 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
5772 goto exit_create_index;
5773 }
5774 if( !db->init.busy ){
5775 if( sqlite3FindTable(db, zName, 0)!=0 ){
5776 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
5777 goto exit_create_index;
5778 }
5779 }
5780 if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
5781 if( !ifNotExist ){
5782 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
5783 }else{
5784 assert( !db->init.busy );
5785 sqlite3CodeVerifySchema(pParse, iDb);
5786 }
5787 goto exit_create_index;
5788 }
5789 }else{
5790 int n;
5791 Index *pLoop;
5792 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
5793 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
5794 if( zName==0 ){
5795 goto exit_create_index;
5796 }
5797 }
5798
5799 /* Check for authorization to create an index.
5800 */
5801 #ifndef SQLITE_OMIT_AUTHORIZATION
5802 {
5803 const char *zDb = pDb->zName;
5804 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
5805 goto exit_create_index;
5806 }
5807 i = SQLITE_CREATE_INDEX;
5808 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
5809 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
5810 goto exit_create_index;
5811 }
5812 }
5813 #endif
5814
5815 /* If pList==0, it means this routine was called to make a primary
5816 ** key out of the last column added to the table under construction.
5817 ** So create a fake list to simulate this.
5818 */
5819 if( pList==0 ){
5820 Token prevCol;
5821 prevCol.z = pTab->aCol[pTab->nCol-1].zName;
5822 prevCol.n = sqlite3Strlen30(prevCol.z);
5823 pList = sqlite3ExprListAppend(pParse, 0,
5824 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
5825 if( pList==0 ) goto exit_create_index;
5826 assert( pList->nExpr==1 );
5827 sqlite3ExprListSetSortOrder(pList, sortOrder);
5828 }else{
5829 sqlite3ExprListCheckLength(pParse, pList, "index");
5830 }
5831
5832 /* Figure out how many bytes of space are required to store explicitly
5833 ** specified collation sequence names.
5834 */
5835 for(i=0; i<pList->nExpr; i++){
5836 Expr *pExpr = pList->a[i].pExpr;
5837 assert( pExpr!=0 );
5838 if( pExpr->op==TK_COLLATE ){
5839 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
5840 }
5841 }
5842
5843 /*
5844 ** Allocate the index structure.
5845 */
5846 nName = sqlite3Strlen30(zName);
5847 nExtraCol = pPk ? pPk->nKeyCol : 1;
5848 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
5849 nName + nExtra + 1, &zExtra);
5850 if( db->mallocFailed ){
5851 goto exit_create_index;
5852 }
5853 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
5854 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
5855 pIndex->zName = zExtra;
5856 zExtra += nName + 1;
5857 memcpy(pIndex->zName, zName, nName+1);
5858 pIndex->pTable = pTab;
5859 pIndex->onError = (u8)onError;
5860 pIndex->uniqNotNull = onError!=OE_None;
5861 pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE;
5862 pIndex->pSchema = db->aDb[iDb].pSchema;
5863 pIndex->nKeyCol = pList->nExpr;
5864 if( pPIWhere ){
5865 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
5866 pIndex->pPartIdxWhere = pPIWhere;
5867 pPIWhere = 0;
5868 }
5869 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
5870
5871 /* Check to see if we should honor DESC requests on index columns
5872 */
5873 if( pDb->pSchema->file_format>=4 ){
5874 sortOrderMask = -1; /* Honor DESC */
5875 }else{
5876 sortOrderMask = 0; /* Ignore DESC */
5877 }
5878
5879 /* Analyze the list of expressions that form the terms of the index and
5880 ** report any errors. In the common case where the expression is exactly
5881 ** a table column, store that column in aiColumn[]. For general expressions,
5882 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
5883 **
5884 ** TODO: Issue a warning if two or more columns of the index are identical.
5885 ** TODO: Issue a warning if the table primary key is used as part of the
5886 ** index key.
5887 */
5888 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
5889 Expr *pCExpr; /* The i-th index expression */
5890 int requestedSortOrder; /* ASC or DESC on the i-th expression */
5891 const char *zColl; /* Collation sequence name */
5892
5893 sqlite3StringToId(pListItem->pExpr);
5894 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
5895 if( pParse->nErr ) goto exit_create_index;
5896 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
5897 if( pCExpr->op!=TK_COLUMN ){
5898 if( pTab==pParse->pNewTable ){
5899 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
5900 "UNIQUE constraints");
5901 goto exit_create_index;
5902 }
5903 if( pIndex->aColExpr==0 ){
5904 ExprList *pCopy = sqlite3ExprListDup(db, pList, 0);
5905 pIndex->aColExpr = pCopy;
5906 if( !db->mallocFailed ){
5907 assert( pCopy!=0 );
5908 pListItem = &pCopy->a[i];
5909 }
5910 }
5911 j = XN_EXPR;
5912 pIndex->aiColumn[i] = XN_EXPR;
5913 pIndex->uniqNotNull = 0;
5914 }else{
5915 j = pCExpr->iColumn;
5916 assert( j<=0x7fff );
5917 if( j<0 ){
5918 j = pTab->iPKey;
5919 }else if( pTab->aCol[j].notNull==0 ){
5920 pIndex->uniqNotNull = 0;
5921 }
5922 pIndex->aiColumn[i] = (i16)j;
5923 }
5924 zColl = 0;
5925 if( pListItem->pExpr->op==TK_COLLATE ){
5926 int nColl;
5927 zColl = pListItem->pExpr->u.zToken;
5928 nColl = sqlite3Strlen30(zColl) + 1;
5929 assert( nExtra>=nColl );
5930 memcpy(zExtra, zColl, nColl);
5931 zColl = zExtra;
5932 zExtra += nColl;
5933 nExtra -= nColl;
5934 }else if( j>=0 ){
5935 zColl = pTab->aCol[j].zColl;
5936 }
5937 if( !zColl ) zColl = sqlite3StrBINARY;
5938 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
5939 goto exit_create_index;
5940 }
5941 pIndex->azColl[i] = zColl;
5942 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
5943 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
5944 }
5945
5946 /* Append the table key to the end of the index. For WITHOUT ROWID
5947 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
5948 ** normal tables (when pPk==0) this will be the rowid.
5949 */
5950 if( pPk ){
5951 for(j=0; j<pPk->nKeyCol; j++){
5952 int x = pPk->aiColumn[j];
5953 assert( x>=0 );
5954 if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
5955 pIndex->nColumn--;
5956 }else{
5957 pIndex->aiColumn[i] = x;
5958 pIndex->azColl[i] = pPk->azColl[j];
5959 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
5960 i++;
5961 }
5962 }
5963 assert( i==pIndex->nColumn );
5964 }else{
5965 pIndex->aiColumn[i] = XN_ROWID;
5966 pIndex->azColl[i] = sqlite3StrBINARY;
5967 }
5968 sqlite3DefaultRowEst(pIndex);
5969 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
5970
5971 if( pTab==pParse->pNewTable ){
5972 /* This routine has been called to create an automatic index as a
5973 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
5974 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
5975 ** i.e. one of:
5976 **
5977 ** CREATE TABLE t(x PRIMARY KEY, y);
5978 ** CREATE TABLE t(x, y, UNIQUE(x, y));
5979 **
5980 ** Either way, check to see if the table already has such an index. If
5981 ** so, don't bother creating this one. This only applies to
5982 ** automatically created indices. Users can do as they wish with
5983 ** explicit indices.
5984 **
5985 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
5986 ** (and thus suppressing the second one) even if they have different
5987 ** sort orders.
5988 **
5989 ** If there are different collating sequences or if the columns of
5990 ** the constraint occur in different orders, then the constraints are
5991 ** considered distinct and both result in separate indices.
5992 */
5993 Index *pIdx;
5994 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
5995 int k;
5996 assert( IsUniqueIndex(pIdx) );
5997 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
5998 assert( IsUniqueIndex(pIndex) );
5999
6000 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
6001 for(k=0; k<pIdx->nKeyCol; k++){
6002 const char *z1;
6003 const char *z2;
6004 assert( pIdx->aiColumn[k]>=0 );
6005 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
6006 z1 = pIdx->azColl[k];
6007 z2 = pIndex->azColl[k];
6008 if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
6009 }
6010 if( k==pIdx->nKeyCol ){
6011 if( pIdx->onError!=pIndex->onError ){
6012 /* This constraint creates the same index as a previous
6013 ** constraint specified somewhere in the CREATE TABLE statement.
6014 ** However the ON CONFLICT clauses are different. If both this
6015 ** constraint and the previous equivalent constraint have explicit
6016 ** ON CONFLICT clauses this is an error. Otherwise, use the
6017 ** explicitly specified behavior for the index.
6018 */
6019 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
6020 sqlite3ErrorMsg(pParse,
6021 "conflicting ON CONFLICT clauses specified", 0);
6022 }
6023 if( pIdx->onError==OE_Default ){
6024 pIdx->onError = pIndex->onError;
6025 }
6026 }
6027 pRet = pIdx;
6028 goto exit_create_index;
6029 }
6030 }
6031 }
6032
6033 /* Link the new Index structure to its table and to the other
6034 ** in-memory database structures.
6035 */
6036 assert( pParse->nErr==0 );
6037 if( db->init.busy ){
6038 Index *p;
6039 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
6040 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
6041 pIndex->zName, pIndex);
6042 if( p ){
6043 assert( p==pIndex ); /* Malloc must have failed */
6044 db->mallocFailed = 1;
6045 goto exit_create_index;
6046 }
6047 db->flags |= SQLITE_InternChanges;
6048 if( pTblName!=0 ){
6049 pIndex->tnum = db->init.newTnum;
6050 }
6051 }
6052
6053 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
6054 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
6055 ** emit code to allocate the index rootpage on disk and make an entry for
6056 ** the index in the sqlite_master table and populate the index with
6057 ** content. But, do not do this if we are simply reading the sqlite_master
6058 ** table to parse the schema, or if this index is the PRIMARY KEY index
6059 ** of a WITHOUT ROWID table.
6060 **
6061 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
6062 ** or UNIQUE index in a CREATE TABLE statement. Since the table
6063 ** has just been created, it contains no data and the index initialization
6064 ** step can be skipped.
6065 */
6066 else if( HasRowid(pTab) || pTblName!=0 ){
6067 Vdbe *v;
6068 char *zStmt;
6069 int iMem = ++pParse->nMem;
6070
6071 v = sqlite3GetVdbe(pParse);
6072 if( v==0 ) goto exit_create_index;
6073
6074 sqlite3BeginWriteOperation(pParse, 1, iDb);
6075
6076 /* Create the rootpage for the index using CreateIndex. But before
6077 ** doing so, code a Noop instruction and store its address in
6078 ** Index.tnum. This is required in case this index is actually a
6079 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
6080 ** that case the convertToWithoutRowidTable() routine will replace
6081 ** the Noop with a Goto to jump over the VDBE code generated below. */
6082 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
6083 sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
6084
6085 /* Gather the complete text of the CREATE INDEX statement into
6086 ** the zStmt variable
6087 */
6088 if( pStart ){
6089 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
6090 if( pName->z[n-1]==';' ) n--;
6091 /* A named index with an explicit CREATE INDEX statement */
6092 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
6093 onError==OE_None ? "" : " UNIQUE", n, pName->z);
6094 }else{
6095 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
6096 /* zStmt = sqlite3MPrintf(""); */
6097 zStmt = 0;
6098 }
6099
6100 /* Add an entry in sqlite_master for this index
6101 */
6102 sqlite3NestedParse(pParse,
6103 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
6104 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
6105 pIndex->zName,
6106 pTab->zName,
6107 iMem,
6108 zStmt
6109 );
6110 sqlite3DbFree(db, zStmt);
6111
6112 /* Fill the index with data and reparse the schema. Code an OP_Expire
6113 ** to invalidate all pre-compiled statements.
6114 */
6115 if( pTblName ){
6116 sqlite3RefillIndex(pParse, pIndex, iMem);
6117 sqlite3ChangeCookie(pParse, iDb);
6118 sqlite3VdbeAddParseSchemaOp(v, iDb,
6119 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
6120 sqlite3VdbeAddOp1(v, OP_Expire, 0);
6121 }
6122
6123 sqlite3VdbeJumpHere(v, pIndex->tnum);
6124 }
6125
6126 /* When adding an index to the list of indices for a table, make
6127 ** sure all indices labeled OE_Replace come after all those labeled
6128 ** OE_Ignore. This is necessary for the correct constraint check
6129 ** processing (in sqlite3GenerateConstraintChecks()) as part of
6130 ** UPDATE and INSERT statements.
6131 */
6132 if( db->init.busy || pTblName==0 ){
6133 if( onError!=OE_Replace || pTab->pIndex==0
6134 || pTab->pIndex->onError==OE_Replace){
6135 pIndex->pNext = pTab->pIndex;
6136 pTab->pIndex = pIndex;
6137 }else{
6138 Index *pOther = pTab->pIndex;
6139 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
6140 pOther = pOther->pNext;
6141 }
6142 pIndex->pNext = pOther->pNext;
6143 pOther->pNext = pIndex;
6144 }
6145 pRet = pIndex;
6146 pIndex = 0;
6147 }
6148
6149 /* Clean up before exiting */
6150 exit_create_index:
6151 if( pIndex ) freeIndex(db, pIndex);
6152 sqlite3ExprDelete(db, pPIWhere);
6153 sqlite3ExprListDelete(db, pList);
6154 sqlite3SrcListDelete(db, pTblName);
6155 sqlite3DbFree(db, zName);
6156 return pRet;
6157 }
6158
6159 /*
6160 ** Fill the Index.aiRowEst[] array with default information - information
6161 ** to be used when we have not run the ANALYZE command.
6162 **
6163 ** aiRowEst[0] is supposed to contain the number of elements in the index.
6164 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
6165 ** number of rows in the table that match any particular value of the
6166 ** first column of the index. aiRowEst[2] is an estimate of the number
6167 ** of rows that match any particular combination of the first 2 columns
6168 ** of the index. And so forth. It must always be the case that
6169 *
6170 ** aiRowEst[N]<=aiRowEst[N-1]
6171 ** aiRowEst[N]>=1
6172 **
6173 ** Apart from that, we have little to go on besides intuition as to
6174 ** how aiRowEst[] should be initialized. The numbers generated here
6175 ** are based on typical values found in actual indices.
6176 */
6177 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
6178 /* 10, 9, 8, 7, 6 */
6179 LogEst aVal[] = { 33, 32, 30, 28, 26 };
6180 LogEst *a = pIdx->aiRowLogEst;
6181 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
6182 int i;
6183
6184 /* Set the first entry (number of rows in the index) to the estimated
6185 ** number of rows in the table. Or 10, if the estimated number of rows
6186 ** in the table is less than that. */
6187 a[0] = pIdx->pTable->nRowLogEst;
6188 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) );
6189
6190 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
6191 ** 6 and each subsequent value (if any) is 5. */
6192 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
6193 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
6194 a[i] = 23; assert( 23==sqlite3LogEst(5) );
6195 }
6196
6197 assert( 0==sqlite3LogEst(1) );
6198 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
6199 }
6200
6201 /*
6202 ** This routine will drop an existing named index. This routine
6203 ** implements the DROP INDEX statement.
6204 */
6205 SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists ){
6206 Index *pIndex;
6207 Vdbe *v;
6208 sqlite3 *db = pParse->db;
6209 int iDb;
6210
6211 assert( pParse->nErr==0 ); /* Never called with prior errors */
6212 if( db->mallocFailed ){
6213 goto exit_drop_index;
6214 }
6215 assert( pName->nSrc==1 );
6216 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
6217 goto exit_drop_index;
6218 }
6219 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
6220 if( pIndex==0 ){
6221 if( !ifExists ){
6222 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
6223 }else{
6224 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
6225 }
6226 pParse->checkSchema = 1;
6227 goto exit_drop_index;
6228 }
6229 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
6230 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
6231 "or PRIMARY KEY constraint cannot be dropped", 0);
6232 goto exit_drop_index;
6233 }
6234 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
6235 #ifndef SQLITE_OMIT_AUTHORIZATION
6236 {
6237 int code = SQLITE_DROP_INDEX;
6238 Table *pTab = pIndex->pTable;
6239 const char *zDb = db->aDb[iDb].zName;
6240 const char *zTab = SCHEMA_TABLE(iDb);
6241 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
6242 goto exit_drop_index;
6243 }
6244 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
6245 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
6246 goto exit_drop_index;
6247 }
6248 }
6249 #endif
6250
6251 /* Generate code to remove the index and from the master table */
6252 v = sqlite3GetVdbe(pParse);
6253 if( v ){
6254 sqlite3BeginWriteOperation(pParse, 1, iDb);
6255 sqlite3NestedParse(pParse,
6256 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
6257 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
6258 );
6259 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
6260 sqlite3ChangeCookie(pParse, iDb);
6261 destroyRootPage(pParse, pIndex->tnum, iDb);
6262 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
6263 }
6264
6265 exit_drop_index:
6266 sqlite3SrcListDelete(db, pName);
6267 }
6268
6269 /*
6270 ** pArray is a pointer to an array of objects. Each object in the
6271 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
6272 ** to extend the array so that there is space for a new object at the end.
6273 **
6274 ** When this function is called, *pnEntry contains the current size of
6275 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
6276 ** in total).
6277 **
6278 ** If the realloc() is successful (i.e. if no OOM condition occurs), the
6279 ** space allocated for the new object is zeroed, *pnEntry updated to
6280 ** reflect the new size of the array and a pointer to the new allocation
6281 ** returned. *pIdx is set to the index of the new array entry in this case.
6282 **
6283 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
6284 ** unchanged and a copy of pArray returned.
6285 */
6286 SQLITE_PRIVATE void *sqlite3ArrayAllocate(
6287 sqlite3 *db, /* Connection to notify of malloc failures */
6288 void *pArray, /* Array of objects. Might be reallocated */
6289 int szEntry, /* Size of each object in the array */
6290 int *pnEntry, /* Number of objects currently in use */
6291 int *pIdx /* Write the index of a new slot here */
6292 ){
6293 char *z;
6294 int n = *pnEntry;
6295 if( (n & (n-1))==0 ){
6296 int sz = (n==0) ? 1 : 2*n;
6297 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
6298 if( pNew==0 ){
6299 *pIdx = -1;
6300 return pArray;
6301 }
6302 pArray = pNew;
6303 }
6304 z = (char*)pArray;
6305 memset(&z[n * szEntry], 0, szEntry);
6306 *pIdx = n;
6307 ++*pnEntry;
6308 return pArray;
6309 }
6310
6311 /*
6312 ** Append a new element to the given IdList. Create a new IdList if
6313 ** need be.
6314 **
6315 ** A new IdList is returned, or NULL if malloc() fails.
6316 */
6317 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pT oken){
6318 int i;
6319 if( pList==0 ){
6320 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
6321 if( pList==0 ) return 0;
6322 }
6323 pList->a = sqlite3ArrayAllocate(
6324 db,
6325 pList->a,
6326 sizeof(pList->a[0]),
6327 &pList->nId,
6328 &i
6329 );
6330 if( i<0 ){
6331 sqlite3IdListDelete(db, pList);
6332 return 0;
6333 }
6334 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
6335 return pList;
6336 }
6337
6338 /*
6339 ** Delete an IdList.
6340 */
6341 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
6342 int i;
6343 if( pList==0 ) return;
6344 for(i=0; i<pList->nId; i++){
6345 sqlite3DbFree(db, pList->a[i].zName);
6346 }
6347 sqlite3DbFree(db, pList->a);
6348 sqlite3DbFree(db, pList);
6349 }
6350
6351 /*
6352 ** Return the index in pList of the identifier named zId. Return -1
6353 ** if not found.
6354 */
6355 SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
6356 int i;
6357 if( pList==0 ) return -1;
6358 for(i=0; i<pList->nId; i++){
6359 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
6360 }
6361 return -1;
6362 }
6363
6364 /*
6365 ** Expand the space allocated for the given SrcList object by
6366 ** creating nExtra new slots beginning at iStart. iStart is zero based.
6367 ** New slots are zeroed.
6368 **
6369 ** For example, suppose a SrcList initially contains two entries: A,B.
6370 ** To append 3 new entries onto the end, do this:
6371 **
6372 ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
6373 **
6374 ** After the call above it would contain: A, B, nil, nil, nil.
6375 ** If the iStart argument had been 1 instead of 2, then the result
6376 ** would have been: A, nil, nil, nil, B. To prepend the new slots,
6377 ** the iStart value would be 0. The result then would
6378 ** be: nil, nil, nil, A, B.
6379 **
6380 ** If a memory allocation fails the SrcList is unchanged. The
6381 ** db->mallocFailed flag will be set to true.
6382 */
6383 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
6384 sqlite3 *db, /* Database connection to notify of OOM errors */
6385 SrcList *pSrc, /* The SrcList to be enlarged */
6386 int nExtra, /* Number of new slots to add to pSrc->a[] */
6387 int iStart /* Index in pSrc->a[] of first new slot */
6388 ){
6389 int i;
6390
6391 /* Sanity checking on calling parameters */
6392 assert( iStart>=0 );
6393 assert( nExtra>=1 );
6394 assert( pSrc!=0 );
6395 assert( iStart<=pSrc->nSrc );
6396
6397 /* Allocate additional space if needed */
6398 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
6399 SrcList *pNew;
6400 int nAlloc = pSrc->nSrc+nExtra;
6401 int nGot;
6402 pNew = sqlite3DbRealloc(db, pSrc,
6403 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
6404 if( pNew==0 ){
6405 assert( db->mallocFailed );
6406 return pSrc;
6407 }
6408 pSrc = pNew;
6409 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
6410 pSrc->nAlloc = nGot;
6411 }
6412
6413 /* Move existing slots that come after the newly inserted slots
6414 ** out of the way */
6415 for(i=pSrc->nSrc-1; i>=iStart; i--){
6416 pSrc->a[i+nExtra] = pSrc->a[i];
6417 }
6418 pSrc->nSrc += nExtra;
6419
6420 /* Zero the newly allocated slots */
6421 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
6422 for(i=iStart; i<iStart+nExtra; i++){
6423 pSrc->a[i].iCursor = -1;
6424 }
6425
6426 /* Return a pointer to the enlarged SrcList */
6427 return pSrc;
6428 }
6429
6430
6431 /*
6432 ** Append a new table name to the given SrcList. Create a new SrcList if
6433 ** need be. A new entry is created in the SrcList even if pTable is NULL.
6434 **
6435 ** A SrcList is returned, or NULL if there is an OOM error. The returned
6436 ** SrcList might be the same as the SrcList that was input or it might be
6437 ** a new one. If an OOM error does occurs, then the prior value of pList
6438 ** that is input to this routine is automatically freed.
6439 **
6440 ** If pDatabase is not null, it means that the table has an optional
6441 ** database name prefix. Like this: "database.table". The pDatabase
6442 ** points to the table name and the pTable points to the database name.
6443 ** The SrcList.a[].zName field is filled with the table name which might
6444 ** come from pTable (if pDatabase is NULL) or from pDatabase.
6445 ** SrcList.a[].zDatabase is filled with the database name from pTable,
6446 ** or with NULL if no database is specified.
6447 **
6448 ** In other words, if call like this:
6449 **
6450 ** sqlite3SrcListAppend(D,A,B,0);
6451 **
6452 ** Then B is a table name and the database name is unspecified. If called
6453 ** like this:
6454 **
6455 ** sqlite3SrcListAppend(D,A,B,C);
6456 **
6457 ** Then C is the table name and B is the database name. If C is defined
6458 ** then so is B. In other words, we never have a case where:
6459 **
6460 ** sqlite3SrcListAppend(D,A,0,C);
6461 **
6462 ** Both pTable and pDatabase are assumed to be quoted. They are dequoted
6463 ** before being added to the SrcList.
6464 */
6465 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
6466 sqlite3 *db, /* Connection to notify of malloc failures */
6467 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
6468 Token *pTable, /* Table to append */
6469 Token *pDatabase /* Database of the table */
6470 ){
6471 struct SrcList_item *pItem;
6472 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
6473 if( pList==0 ){
6474 pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
6475 if( pList==0 ) return 0;
6476 pList->nAlloc = 1;
6477 }
6478 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
6479 if( db->mallocFailed ){
6480 sqlite3SrcListDelete(db, pList);
6481 return 0;
6482 }
6483 pItem = &pList->a[pList->nSrc-1];
6484 if( pDatabase && pDatabase->z==0 ){
6485 pDatabase = 0;
6486 }
6487 if( pDatabase ){
6488 Token *pTemp = pDatabase;
6489 pDatabase = pTable;
6490 pTable = pTemp;
6491 }
6492 pItem->zName = sqlite3NameFromToken(db, pTable);
6493 pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
6494 return pList;
6495 }
6496
6497 /*
6498 ** Assign VdbeCursor index numbers to all tables in a SrcList
6499 */
6500 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
6501 int i;
6502 struct SrcList_item *pItem;
6503 assert(pList || pParse->db->mallocFailed );
6504 if( pList ){
6505 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
6506 if( pItem->iCursor>=0 ) break;
6507 pItem->iCursor = pParse->nTab++;
6508 if( pItem->pSelect ){
6509 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
6510 }
6511 }
6512 }
6513 }
6514
6515 /*
6516 ** Delete an entire SrcList including all its substructure.
6517 */
6518 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
6519 int i;
6520 struct SrcList_item *pItem;
6521 if( pList==0 ) return;
6522 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
6523 sqlite3DbFree(db, pItem->zDatabase);
6524 sqlite3DbFree(db, pItem->zName);
6525 sqlite3DbFree(db, pItem->zAlias);
6526 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
6527 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
6528 sqlite3DeleteTable(db, pItem->pTab);
6529 sqlite3SelectDelete(db, pItem->pSelect);
6530 sqlite3ExprDelete(db, pItem->pOn);
6531 sqlite3IdListDelete(db, pItem->pUsing);
6532 }
6533 sqlite3DbFree(db, pList);
6534 }
6535
6536 /*
6537 ** This routine is called by the parser to add a new term to the
6538 ** end of a growing FROM clause. The "p" parameter is the part of
6539 ** the FROM clause that has already been constructed. "p" is NULL
6540 ** if this is the first term of the FROM clause. pTable and pDatabase
6541 ** are the name of the table and database named in the FROM clause term.
6542 ** pDatabase is NULL if the database name qualifier is missing - the
6543 ** usual case. If the term has an alias, then pAlias points to the
6544 ** alias token. If the term is a subquery, then pSubquery is the
6545 ** SELECT statement that the subquery encodes. The pTable and
6546 ** pDatabase parameters are NULL for subqueries. The pOn and pUsing
6547 ** parameters are the content of the ON and USING clauses.
6548 **
6549 ** Return a new SrcList which encodes is the FROM with the new
6550 ** term added.
6551 */
6552 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
6553 Parse *pParse, /* Parsing context */
6554 SrcList *p, /* The left part of the FROM clause already seen */
6555 Token *pTable, /* Name of the table to add to the FROM clause */
6556 Token *pDatabase, /* Name of the database containing pTable */
6557 Token *pAlias, /* The right-hand side of the AS subexpression */
6558 Select *pSubquery, /* A subquery used in place of a table name */
6559 Expr *pOn, /* The ON clause of a join */
6560 IdList *pUsing /* The USING clause of a join */
6561 ){
6562 struct SrcList_item *pItem;
6563 sqlite3 *db = pParse->db;
6564 if( !p && (pOn || pUsing) ){
6565 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
6566 (pOn ? "ON" : "USING")
6567 );
6568 goto append_from_error;
6569 }
6570 p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
6571 if( p==0 || NEVER(p->nSrc==0) ){
6572 goto append_from_error;
6573 }
6574 pItem = &p->a[p->nSrc-1];
6575 assert( pAlias!=0 );
6576 if( pAlias->n ){
6577 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
6578 }
6579 pItem->pSelect = pSubquery;
6580 pItem->pOn = pOn;
6581 pItem->pUsing = pUsing;
6582 return p;
6583
6584 append_from_error:
6585 assert( p==0 );
6586 sqlite3ExprDelete(db, pOn);
6587 sqlite3IdListDelete(db, pUsing);
6588 sqlite3SelectDelete(db, pSubquery);
6589 return 0;
6590 }
6591
6592 /*
6593 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added
6594 ** element of the source-list passed as the second argument.
6595 */
6596 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pI ndexedBy){
6597 assert( pIndexedBy!=0 );
6598 if( p && ALWAYS(p->nSrc>0) ){
6599 struct SrcList_item *pItem = &p->a[p->nSrc-1];
6600 assert( pItem->fg.notIndexed==0 );
6601 assert( pItem->fg.isIndexedBy==0 );
6602 assert( pItem->fg.isTabFunc==0 );
6603 if( pIndexedBy->n==1 && !pIndexedBy->z ){
6604 /* A "NOT INDEXED" clause was supplied. See parse.y
6605 ** construct "indexed_opt" for details. */
6606 pItem->fg.notIndexed = 1;
6607 }else{
6608 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
6609 pItem->fg.isIndexedBy = (pItem->u1.zIndexedBy!=0);
6610 }
6611 }
6612 }
6613
6614 /*
6615 ** Add the list of function arguments to the SrcList entry for a
6616 ** table-valued-function.
6617 */
6618 SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList * pList){
6619 if( p ){
6620 struct SrcList_item *pItem = &p->a[p->nSrc-1];
6621 assert( pItem->fg.notIndexed==0 );
6622 assert( pItem->fg.isIndexedBy==0 );
6623 assert( pItem->fg.isTabFunc==0 );
6624 pItem->u1.pFuncArg = pList;
6625 pItem->fg.isTabFunc = 1;
6626 }else{
6627 sqlite3ExprListDelete(pParse->db, pList);
6628 }
6629 }
6630
6631 /*
6632 ** When building up a FROM clause in the parser, the join operator
6633 ** is initially attached to the left operand. But the code generator
6634 ** expects the join operator to be on the right operand. This routine
6635 ** Shifts all join operators from left to right for an entire FROM
6636 ** clause.
6637 **
6638 ** Example: Suppose the join is like this:
6639 **
6640 ** A natural cross join B
6641 **
6642 ** The operator is "natural cross join". The A and B operands are stored
6643 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
6644 ** operator with A. This routine shifts that operator over to B.
6645 */
6646 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
6647 if( p ){
6648 int i;
6649 for(i=p->nSrc-1; i>0; i--){
6650 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
6651 }
6652 p->a[0].fg.jointype = 0;
6653 }
6654 }
6655
6656 /*
6657 ** Begin a transaction
6658 */
6659 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
6660 sqlite3 *db;
6661 Vdbe *v;
6662 int i;
6663
6664 assert( pParse!=0 );
6665 db = pParse->db;
6666 assert( db!=0 );
6667 /* if( db->aDb[0].pBt==0 ) return; */
6668 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
6669 return;
6670 }
6671 v = sqlite3GetVdbe(pParse);
6672 if( !v ) return;
6673 if( type!=TK_DEFERRED ){
6674 for(i=0; i<db->nDb; i++){
6675 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
6676 sqlite3VdbeUsesBtree(v, i);
6677 }
6678 }
6679 sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
6680 }
6681
6682 /*
6683 ** Commit a transaction
6684 */
6685 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
6686 Vdbe *v;
6687
6688 assert( pParse!=0 );
6689 assert( pParse->db!=0 );
6690 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
6691 return;
6692 }
6693 v = sqlite3GetVdbe(pParse);
6694 if( v ){
6695 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
6696 }
6697 }
6698
6699 /*
6700 ** Rollback a transaction
6701 */
6702 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
6703 Vdbe *v;
6704
6705 assert( pParse!=0 );
6706 assert( pParse->db!=0 );
6707 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
6708 return;
6709 }
6710 v = sqlite3GetVdbe(pParse);
6711 if( v ){
6712 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
6713 }
6714 }
6715
6716 /*
6717 ** This function is called by the parser when it parses a command to create,
6718 ** release or rollback an SQL savepoint.
6719 */
6720 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
6721 char *zName = sqlite3NameFromToken(pParse->db, pName);
6722 if( zName ){
6723 Vdbe *v = sqlite3GetVdbe(pParse);
6724 #ifndef SQLITE_OMIT_AUTHORIZATION
6725 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
6726 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
6727 #endif
6728 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
6729 sqlite3DbFree(pParse->db, zName);
6730 return;
6731 }
6732 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
6733 }
6734 }
6735
6736 /*
6737 ** Make sure the TEMP database is open and available for use. Return
6738 ** the number of errors. Leave any error messages in the pParse structure.
6739 */
6740 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
6741 sqlite3 *db = pParse->db;
6742 if( db->aDb[1].pBt==0 && !pParse->explain ){
6743 int rc;
6744 Btree *pBt;
6745 static const int flags =
6746 SQLITE_OPEN_READWRITE |
6747 SQLITE_OPEN_CREATE |
6748 SQLITE_OPEN_EXCLUSIVE |
6749 SQLITE_OPEN_DELETEONCLOSE |
6750 SQLITE_OPEN_TEMP_DB;
6751
6752 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
6753 if( rc!=SQLITE_OK ){
6754 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
6755 "file for storing temporary tables");
6756 pParse->rc = rc;
6757 return 1;
6758 }
6759 db->aDb[1].pBt = pBt;
6760 assert( db->aDb[1].pSchema );
6761 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
6762 db->mallocFailed = 1;
6763 return 1;
6764 }
6765 }
6766 return 0;
6767 }
6768
6769 /*
6770 ** Record the fact that the schema cookie will need to be verified
6771 ** for database iDb. The code to actually verify the schema cookie
6772 ** will occur at the end of the top-level VDBE and will be generated
6773 ** later, by sqlite3FinishCoding().
6774 */
6775 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
6776 Parse *pToplevel = sqlite3ParseToplevel(pParse);
6777 sqlite3 *db = pToplevel->db;
6778
6779 assert( iDb>=0 && iDb<db->nDb );
6780 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
6781 assert( iDb<SQLITE_MAX_ATTACHED+2 );
6782 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
6783 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
6784 DbMaskSet(pToplevel->cookieMask, iDb);
6785 pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
6786 if( !OMIT_TEMPDB && iDb==1 ){
6787 sqlite3OpenTempDatabase(pToplevel);
6788 }
6789 }
6790 }
6791
6792 /*
6793 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
6794 ** attached database. Otherwise, invoke it for the database named zDb only.
6795 */
6796 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb) {
6797 sqlite3 *db = pParse->db;
6798 int i;
6799 for(i=0; i<db->nDb; i++){
6800 Db *pDb = &db->aDb[i];
6801 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
6802 sqlite3CodeVerifySchema(pParse, i);
6803 }
6804 }
6805 }
6806
6807 /*
6808 ** Generate VDBE code that prepares for doing an operation that
6809 ** might change the database.
6810 **
6811 ** This routine starts a new transaction if we are not already within
6812 ** a transaction. If we are already within a transaction, then a checkpoint
6813 ** is set if the setStatement parameter is true. A checkpoint should
6814 ** be set for operations that might fail (due to a constraint) part of
6815 ** the way through and which will need to undo some writes without having to
6816 ** rollback the whole transaction. For operations where all constraints
6817 ** can be checked before any changes are made to the database, it is never
6818 ** necessary to undo a write and the checkpoint should not be set.
6819 */
6820 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
6821 Parse *pToplevel = sqlite3ParseToplevel(pParse);
6822 sqlite3CodeVerifySchema(pParse, iDb);
6823 DbMaskSet(pToplevel->writeMask, iDb);
6824 pToplevel->isMultiWrite |= setStatement;
6825 }
6826
6827 /*
6828 ** Indicate that the statement currently under construction might write
6829 ** more than one entry (example: deleting one row then inserting another,
6830 ** inserting multiple rows in a table, or inserting a row and index entries.)
6831 ** If an abort occurs after some of these writes have completed, then it will
6832 ** be necessary to undo the completed writes.
6833 */
6834 SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
6835 Parse *pToplevel = sqlite3ParseToplevel(pParse);
6836 pToplevel->isMultiWrite = 1;
6837 }
6838
6839 /*
6840 ** The code generator calls this routine if is discovers that it is
6841 ** possible to abort a statement prior to completion. In order to
6842 ** perform this abort without corrupting the database, we need to make
6843 ** sure that the statement is protected by a statement transaction.
6844 **
6845 ** Technically, we only need to set the mayAbort flag if the
6846 ** isMultiWrite flag was previously set. There is a time dependency
6847 ** such that the abort must occur after the multiwrite. This makes
6848 ** some statements involving the REPLACE conflict resolution algorithm
6849 ** go a little faster. But taking advantage of this time dependency
6850 ** makes it more difficult to prove that the code is correct (in
6851 ** particular, it prevents us from writing an effective
6852 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
6853 ** to take the safe route and skip the optimization.
6854 */
6855 SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
6856 Parse *pToplevel = sqlite3ParseToplevel(pParse);
6857 pToplevel->mayAbort = 1;
6858 }
6859
6860 /*
6861 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
6862 ** error. The onError parameter determines which (if any) of the statement
6863 ** and/or current transaction is rolled back.
6864 */
6865 SQLITE_PRIVATE void sqlite3HaltConstraint(
6866 Parse *pParse, /* Parsing context */
6867 int errCode, /* extended error code */
6868 int onError, /* Constraint type */
6869 char *p4, /* Error message */
6870 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
6871 u8 p5Errmsg /* P5_ErrMsg type */
6872 ){
6873 Vdbe *v = sqlite3GetVdbe(pParse);
6874 assert( (errCode&0xff)==SQLITE_CONSTRAINT );
6875 if( onError==OE_Abort ){
6876 sqlite3MayAbort(pParse);
6877 }
6878 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
6879 if( p5Errmsg ) sqlite3VdbeChangeP5(v, p5Errmsg);
6880 }
6881
6882 /*
6883 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
6884 */
6885 SQLITE_PRIVATE void sqlite3UniqueConstraint(
6886 Parse *pParse, /* Parsing context */
6887 int onError, /* Constraint type */
6888 Index *pIdx /* The index that triggers the constraint */
6889 ){
6890 char *zErr;
6891 int j;
6892 StrAccum errMsg;
6893 Table *pTab = pIdx->pTable;
6894
6895 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
6896 if( pIdx->aColExpr ){
6897 sqlite3XPrintf(&errMsg, 0, "index '%q'", pIdx->zName);
6898 }else{
6899 for(j=0; j<pIdx->nKeyCol; j++){
6900 char *zCol;
6901 assert( pIdx->aiColumn[j]>=0 );
6902 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
6903 if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
6904 sqlite3XPrintf(&errMsg, 0, "%s.%s", pTab->zName, zCol);
6905 }
6906 }
6907 zErr = sqlite3StrAccumFinish(&errMsg);
6908 sqlite3HaltConstraint(pParse,
6909 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
6910 : SQLITE_CONSTRAINT_UNIQUE,
6911 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
6912 }
6913
6914
6915 /*
6916 ** Code an OP_Halt due to non-unique rowid.
6917 */
6918 SQLITE_PRIVATE void sqlite3RowidConstraint(
6919 Parse *pParse, /* Parsing context */
6920 int onError, /* Conflict resolution algorithm */
6921 Table *pTab /* The table with the non-unique rowid */
6922 ){
6923 char *zMsg;
6924 int rc;
6925 if( pTab->iPKey>=0 ){
6926 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
6927 pTab->aCol[pTab->iPKey].zName);
6928 rc = SQLITE_CONSTRAINT_PRIMARYKEY;
6929 }else{
6930 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
6931 rc = SQLITE_CONSTRAINT_ROWID;
6932 }
6933 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
6934 P5_ConstraintUnique);
6935 }
6936
6937 /*
6938 ** Check to see if pIndex uses the collating sequence pColl. Return
6939 ** true if it does and false if it does not.
6940 */
6941 #ifndef SQLITE_OMIT_REINDEX
6942 static int collationMatch(const char *zColl, Index *pIndex){
6943 int i;
6944 assert( zColl!=0 );
6945 for(i=0; i<pIndex->nColumn; i++){
6946 const char *z = pIndex->azColl[i];
6947 assert( z!=0 || pIndex->aiColumn[i]<0 );
6948 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
6949 return 1;
6950 }
6951 }
6952 return 0;
6953 }
6954 #endif
6955
6956 /*
6957 ** Recompute all indices of pTab that use the collating sequence pColl.
6958 ** If pColl==0 then recompute all indices of pTab.
6959 */
6960 #ifndef SQLITE_OMIT_REINDEX
6961 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
6962 Index *pIndex; /* An index associated with pTab */
6963
6964 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
6965 if( zColl==0 || collationMatch(zColl, pIndex) ){
6966 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
6967 sqlite3BeginWriteOperation(pParse, 0, iDb);
6968 sqlite3RefillIndex(pParse, pIndex, -1);
6969 }
6970 }
6971 }
6972 #endif
6973
6974 /*
6975 ** Recompute all indices of all tables in all databases where the
6976 ** indices use the collating sequence pColl. If pColl==0 then recompute
6977 ** all indices everywhere.
6978 */
6979 #ifndef SQLITE_OMIT_REINDEX
6980 static void reindexDatabases(Parse *pParse, char const *zColl){
6981 Db *pDb; /* A single database */
6982 int iDb; /* The database index number */
6983 sqlite3 *db = pParse->db; /* The database connection */
6984 HashElem *k; /* For looping over tables in pDb */
6985 Table *pTab; /* A table in the database */
6986
6987 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */
6988 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
6989 assert( pDb!=0 );
6990 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
6991 pTab = (Table*)sqliteHashData(k);
6992 reindexTable(pParse, pTab, zColl);
6993 }
6994 }
6995 }
6996 #endif
6997
6998 /*
6999 ** Generate code for the REINDEX command.
7000 **
7001 ** REINDEX -- 1
7002 ** REINDEX <collation> -- 2
7003 ** REINDEX ?<database>.?<tablename> -- 3
7004 ** REINDEX ?<database>.?<indexname> -- 4
7005 **
7006 ** Form 1 causes all indices in all attached databases to be rebuilt.
7007 ** Form 2 rebuilds all indices in all databases that use the named
7008 ** collating function. Forms 3 and 4 rebuild the named index or all
7009 ** indices associated with the named table.
7010 */
7011 #ifndef SQLITE_OMIT_REINDEX
7012 SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
7013 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
7014 char *z; /* Name of a table or index */
7015 const char *zDb; /* Name of the database */
7016 Table *pTab; /* A table in the database */
7017 Index *pIndex; /* An index associated with pTab */
7018 int iDb; /* The database index number */
7019 sqlite3 *db = pParse->db; /* The database connection */
7020 Token *pObjName; /* Name of the table or index to be reindexed */
7021
7022 /* Read the database schema. If an error occurs, leave an error message
7023 ** and code in pParse and return NULL. */
7024 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
7025 return;
7026 }
7027
7028 if( pName1==0 ){
7029 reindexDatabases(pParse, 0);
7030 return;
7031 }else if( NEVER(pName2==0) || pName2->z==0 ){
7032 char *zColl;
7033 assert( pName1->z );
7034 zColl = sqlite3NameFromToken(pParse->db, pName1);
7035 if( !zColl ) return;
7036 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
7037 if( pColl ){
7038 reindexDatabases(pParse, zColl);
7039 sqlite3DbFree(db, zColl);
7040 return;
7041 }
7042 sqlite3DbFree(db, zColl);
7043 }
7044 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
7045 if( iDb<0 ) return;
7046 z = sqlite3NameFromToken(db, pObjName);
7047 if( z==0 ) return;
7048 zDb = db->aDb[iDb].zName;
7049 pTab = sqlite3FindTable(db, z, zDb);
7050 if( pTab ){
7051 reindexTable(pParse, pTab, 0);
7052 sqlite3DbFree(db, z);
7053 return;
7054 }
7055 pIndex = sqlite3FindIndex(db, z, zDb);
7056 sqlite3DbFree(db, z);
7057 if( pIndex ){
7058 sqlite3BeginWriteOperation(pParse, 0, iDb);
7059 sqlite3RefillIndex(pParse, pIndex, -1);
7060 return;
7061 }
7062 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
7063 }
7064 #endif
7065
7066 /*
7067 ** Return a KeyInfo structure that is appropriate for the given Index.
7068 **
7069 ** The KeyInfo structure for an index is cached in the Index object.
7070 ** So there might be multiple references to the returned pointer. The
7071 ** caller should not try to modify the KeyInfo object.
7072 **
7073 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
7074 ** when it has finished using it.
7075 */
7076 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
7077 int i;
7078 int nCol = pIdx->nColumn;
7079 int nKey = pIdx->nKeyCol;
7080 KeyInfo *pKey;
7081 if( pParse->nErr ) return 0;
7082 if( pIdx->uniqNotNull ){
7083 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
7084 }else{
7085 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
7086 }
7087 if( pKey ){
7088 assert( sqlite3KeyInfoIsWriteable(pKey) );
7089 for(i=0; i<nCol; i++){
7090 const char *zColl = pIdx->azColl[i];
7091 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
7092 sqlite3LocateCollSeq(pParse, zColl);
7093 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
7094 }
7095 if( pParse->nErr ){
7096 sqlite3KeyInfoUnref(pKey);
7097 pKey = 0;
7098 }
7099 }
7100 return pKey;
7101 }
7102
7103 #ifndef SQLITE_OMIT_CTE
7104 /*
7105 ** This routine is invoked once per CTE by the parser while parsing a
7106 ** WITH clause.
7107 */
7108 SQLITE_PRIVATE With *sqlite3WithAdd(
7109 Parse *pParse, /* Parsing context */
7110 With *pWith, /* Existing WITH clause, or NULL */
7111 Token *pName, /* Name of the common-table */
7112 ExprList *pArglist, /* Optional column name list for the table */
7113 Select *pQuery /* Query used to initialize the table */
7114 ){
7115 sqlite3 *db = pParse->db;
7116 With *pNew;
7117 char *zName;
7118
7119 /* Check that the CTE name is unique within this WITH clause. If
7120 ** not, store an error in the Parse structure. */
7121 zName = sqlite3NameFromToken(pParse->db, pName);
7122 if( zName && pWith ){
7123 int i;
7124 for(i=0; i<pWith->nCte; i++){
7125 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
7126 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
7127 }
7128 }
7129 }
7130
7131 if( pWith ){
7132 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
7133 pNew = sqlite3DbRealloc(db, pWith, nByte);
7134 }else{
7135 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
7136 }
7137 assert( zName!=0 || pNew==0 );
7138 assert( db->mallocFailed==0 || pNew==0 );
7139
7140 if( pNew==0 ){
7141 sqlite3ExprListDelete(db, pArglist);
7142 sqlite3SelectDelete(db, pQuery);
7143 sqlite3DbFree(db, zName);
7144 pNew = pWith;
7145 }else{
7146 pNew->a[pNew->nCte].pSelect = pQuery;
7147 pNew->a[pNew->nCte].pCols = pArglist;
7148 pNew->a[pNew->nCte].zName = zName;
7149 pNew->a[pNew->nCte].zCteErr = 0;
7150 pNew->nCte++;
7151 }
7152
7153 return pNew;
7154 }
7155
7156 /*
7157 ** Free the contents of the With object passed as the second argument.
7158 */
7159 SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
7160 if( pWith ){
7161 int i;
7162 for(i=0; i<pWith->nCte; i++){
7163 struct Cte *pCte = &pWith->a[i];
7164 sqlite3ExprListDelete(db, pCte->pCols);
7165 sqlite3SelectDelete(db, pCte->pSelect);
7166 sqlite3DbFree(db, pCte->zName);
7167 }
7168 sqlite3DbFree(db, pWith);
7169 }
7170 }
7171 #endif /* !defined(SQLITE_OMIT_CTE) */
7172
7173 /************** End of build.c ***********************************************/
7174 /************** Begin file callback.c ****************************************/
7175 /*
7176 ** 2005 May 23
7177 **
7178 ** The author disclaims copyright to this source code. In place of
7179 ** a legal notice, here is a blessing:
7180 **
7181 ** May you do good and not evil.
7182 ** May you find forgiveness for yourself and forgive others.
7183 ** May you share freely, never taking more than you give.
7184 **
7185 *************************************************************************
7186 **
7187 ** This file contains functions used to access the internal hash tables
7188 ** of user defined functions and collation sequences.
7189 */
7190
7191 /* #include "sqliteInt.h" */
7192
7193 /*
7194 ** Invoke the 'collation needed' callback to request a collation sequence
7195 ** in the encoding enc of name zName, length nName.
7196 */
7197 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
7198 assert( !db->xCollNeeded || !db->xCollNeeded16 );
7199 if( db->xCollNeeded ){
7200 char *zExternal = sqlite3DbStrDup(db, zName);
7201 if( !zExternal ) return;
7202 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
7203 sqlite3DbFree(db, zExternal);
7204 }
7205 #ifndef SQLITE_OMIT_UTF16
7206 if( db->xCollNeeded16 ){
7207 char const *zExternal;
7208 sqlite3_value *pTmp = sqlite3ValueNew(db);
7209 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
7210 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
7211 if( zExternal ){
7212 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
7213 }
7214 sqlite3ValueFree(pTmp);
7215 }
7216 #endif
7217 }
7218
7219 /*
7220 ** This routine is called if the collation factory fails to deliver a
7221 ** collation function in the best encoding but there may be other versions
7222 ** of this collation function (for other text encodings) available. Use one
7223 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
7224 ** possible.
7225 */
7226 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
7227 CollSeq *pColl2;
7228 char *z = pColl->zName;
7229 int i;
7230 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
7231 for(i=0; i<3; i++){
7232 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
7233 if( pColl2->xCmp!=0 ){
7234 memcpy(pColl, pColl2, sizeof(CollSeq));
7235 pColl->xDel = 0; /* Do not copy the destructor */
7236 return SQLITE_OK;
7237 }
7238 }
7239 return SQLITE_ERROR;
7240 }
7241
7242 /*
7243 ** This function is responsible for invoking the collation factory callback
7244 ** or substituting a collation sequence of a different encoding when the
7245 ** requested collation sequence is not available in the desired encoding.
7246 **
7247 ** If it is not NULL, then pColl must point to the database native encoding
7248 ** collation sequence with name zName, length nName.
7249 **
7250 ** The return value is either the collation sequence to be used in database
7251 ** db for collation type name zName, length nName, or NULL, if no collation
7252 ** sequence can be found. If no collation is found, leave an error message.
7253 **
7254 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
7255 */
7256 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
7257 Parse *pParse, /* Parsing context */
7258 u8 enc, /* The desired encoding for the collating sequence */
7259 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
7260 const char *zName /* Collating sequence name */
7261 ){
7262 CollSeq *p;
7263 sqlite3 *db = pParse->db;
7264
7265 p = pColl;
7266 if( !p ){
7267 p = sqlite3FindCollSeq(db, enc, zName, 0);
7268 }
7269 if( !p || !p->xCmp ){
7270 /* No collation sequence of this type for this encoding is registered.
7271 ** Call the collation factory to see if it can supply us with one.
7272 */
7273 callCollNeeded(db, enc, zName);
7274 p = sqlite3FindCollSeq(db, enc, zName, 0);
7275 }
7276 if( p && !p->xCmp && synthCollSeq(db, p) ){
7277 p = 0;
7278 }
7279 assert( !p || p->xCmp );
7280 if( p==0 ){
7281 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
7282 }
7283 return p;
7284 }
7285
7286 /*
7287 ** This routine is called on a collation sequence before it is used to
7288 ** check that it is defined. An undefined collation sequence exists when
7289 ** a database is loaded that contains references to collation sequences
7290 ** that have not been defined by sqlite3_create_collation() etc.
7291 **
7292 ** If required, this routine calls the 'collation needed' callback to
7293 ** request a definition of the collating sequence. If this doesn't work,
7294 ** an equivalent collating sequence that uses a text encoding different
7295 ** from the main database is substituted, if one is available.
7296 */
7297 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
7298 if( pColl ){
7299 const char *zName = pColl->zName;
7300 sqlite3 *db = pParse->db;
7301 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
7302 if( !p ){
7303 return SQLITE_ERROR;
7304 }
7305 assert( p==pColl );
7306 }
7307 return SQLITE_OK;
7308 }
7309
7310
7311
7312 /*
7313 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
7314 ** specified by zName and nName is not found and parameter 'create' is
7315 ** true, then create a new entry. Otherwise return NULL.
7316 **
7317 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
7318 ** array of three CollSeq structures. The first is the collation sequence
7319 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
7320 **
7321 ** Stored immediately after the three collation sequences is a copy of
7322 ** the collation sequence name. A pointer to this string is stored in
7323 ** each collation sequence structure.
7324 */
7325 static CollSeq *findCollSeqEntry(
7326 sqlite3 *db, /* Database connection */
7327 const char *zName, /* Name of the collating sequence */
7328 int create /* Create a new entry if true */
7329 ){
7330 CollSeq *pColl;
7331 pColl = sqlite3HashFind(&db->aCollSeq, zName);
7332
7333 if( 0==pColl && create ){
7334 int nName = sqlite3Strlen30(zName);
7335 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1);
7336 if( pColl ){
7337 CollSeq *pDel = 0;
7338 pColl[0].zName = (char*)&pColl[3];
7339 pColl[0].enc = SQLITE_UTF8;
7340 pColl[1].zName = (char*)&pColl[3];
7341 pColl[1].enc = SQLITE_UTF16LE;
7342 pColl[2].zName = (char*)&pColl[3];
7343 pColl[2].enc = SQLITE_UTF16BE;
7344 memcpy(pColl[0].zName, zName, nName);
7345 pColl[0].zName[nName] = 0;
7346 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
7347
7348 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
7349 ** return the pColl pointer to be deleted (because it wasn't added
7350 ** to the hash table).
7351 */
7352 assert( pDel==0 || pDel==pColl );
7353 if( pDel!=0 ){
7354 db->mallocFailed = 1;
7355 sqlite3DbFree(db, pDel);
7356 pColl = 0;
7357 }
7358 }
7359 }
7360 return pColl;
7361 }
7362
7363 /*
7364 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
7365 ** Return the CollSeq* pointer for the collation sequence named zName
7366 ** for the encoding 'enc' from the database 'db'.
7367 **
7368 ** If the entry specified is not found and 'create' is true, then create a
7369 ** new entry. Otherwise return NULL.
7370 **
7371 ** A separate function sqlite3LocateCollSeq() is a wrapper around
7372 ** this routine. sqlite3LocateCollSeq() invokes the collation factory
7373 ** if necessary and generates an error message if the collating sequence
7374 ** cannot be found.
7375 **
7376 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
7377 */
7378 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
7379 sqlite3 *db,
7380 u8 enc,
7381 const char *zName,
7382 int create
7383 ){
7384 CollSeq *pColl;
7385 if( zName ){
7386 pColl = findCollSeqEntry(db, zName, create);
7387 }else{
7388 pColl = db->pDfltColl;
7389 }
7390 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
7391 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
7392 if( pColl ) pColl += enc-1;
7393 return pColl;
7394 }
7395
7396 /* During the search for the best function definition, this procedure
7397 ** is called to test how well the function passed as the first argument
7398 ** matches the request for a function with nArg arguments in a system
7399 ** that uses encoding enc. The value returned indicates how well the
7400 ** request is matched. A higher value indicates a better match.
7401 **
7402 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
7403 ** is also -1. In other words, we are searching for a function that
7404 ** takes a variable number of arguments.
7405 **
7406 ** If nArg is -2 that means that we are searching for any function
7407 ** regardless of the number of arguments it uses, so return a positive
7408 ** match score for any
7409 **
7410 ** The returned value is always between 0 and 6, as follows:
7411 **
7412 ** 0: Not a match.
7413 ** 1: UTF8/16 conversion required and function takes any number of arguments.
7414 ** 2: UTF16 byte order change required and function takes any number of args.
7415 ** 3: encoding matches and function takes any number of arguments
7416 ** 4: UTF8/16 conversion required - argument count matches exactly
7417 ** 5: UTF16 byte order conversion required - argument count matches exactly
7418 ** 6: Perfect match: encoding and argument count match exactly.
7419 **
7420 ** If nArg==(-2) then any function with a non-null xStep or xFunc is
7421 ** a perfect match and any function with both xStep and xFunc NULL is
7422 ** a non-match.
7423 */
7424 #define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
7425 static int matchQuality(
7426 FuncDef *p, /* The function we are evaluating for match quality */
7427 int nArg, /* Desired number of arguments. (-1)==any */
7428 u8 enc /* Desired text encoding */
7429 ){
7430 int match;
7431
7432 /* nArg of -2 is a special case */
7433 if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
7434
7435 /* Wrong number of arguments means "no match" */
7436 if( p->nArg!=nArg && p->nArg>=0 ) return 0;
7437
7438 /* Give a better score to a function with a specific number of arguments
7439 ** than to function that accepts any number of arguments. */
7440 if( p->nArg==nArg ){
7441 match = 4;
7442 }else{
7443 match = 1;
7444 }
7445
7446 /* Bonus points if the text encoding matches */
7447 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
7448 match += 2; /* Exact encoding match */
7449 }else if( (enc & p->funcFlags & 2)!=0 ){
7450 match += 1; /* Both are UTF16, but with different byte orders */
7451 }
7452
7453 return match;
7454 }
7455
7456 /*
7457 ** Search a FuncDefHash for a function with the given name. Return
7458 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
7459 */
7460 static FuncDef *functionSearch(
7461 FuncDefHash *pHash, /* Hash table to search */
7462 int h, /* Hash of the name */
7463 const char *zFunc, /* Name of function */
7464 int nFunc /* Number of bytes in zFunc */
7465 ){
7466 FuncDef *p;
7467 for(p=pHash->a[h]; p; p=p->pHash){
7468 if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
7469 return p;
7470 }
7471 }
7472 return 0;
7473 }
7474
7475 /*
7476 ** Insert a new FuncDef into a FuncDefHash hash table.
7477 */
7478 SQLITE_PRIVATE void sqlite3FuncDefInsert(
7479 FuncDefHash *pHash, /* The hash table into which to insert */
7480 FuncDef *pDef /* The function definition to insert */
7481 ){
7482 FuncDef *pOther;
7483 int nName = sqlite3Strlen30(pDef->zName);
7484 u8 c1 = (u8)pDef->zName[0];
7485 int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
7486 pOther = functionSearch(pHash, h, pDef->zName, nName);
7487 if( pOther ){
7488 assert( pOther!=pDef && pOther->pNext!=pDef );
7489 pDef->pNext = pOther->pNext;
7490 pOther->pNext = pDef;
7491 }else{
7492 pDef->pNext = 0;
7493 pDef->pHash = pHash->a[h];
7494 pHash->a[h] = pDef;
7495 }
7496 }
7497
7498
7499
7500 /*
7501 ** Locate a user function given a name, a number of arguments and a flag
7502 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
7503 ** pointer to the FuncDef structure that defines that function, or return
7504 ** NULL if the function does not exist.
7505 **
7506 ** If the createFlag argument is true, then a new (blank) FuncDef
7507 ** structure is created and liked into the "db" structure if a
7508 ** no matching function previously existed.
7509 **
7510 ** If nArg is -2, then the first valid function found is returned. A
7511 ** function is valid if either xFunc or xStep is non-zero. The nArg==(-2)
7512 ** case is used to see if zName is a valid function name for some number
7513 ** of arguments. If nArg is -2, then createFlag must be 0.
7514 **
7515 ** If createFlag is false, then a function with the required name and
7516 ** number of arguments may be returned even if the eTextRep flag does not
7517 ** match that requested.
7518 */
7519 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
7520 sqlite3 *db, /* An open database */
7521 const char *zName, /* Name of the function. Not null-terminated */
7522 int nName, /* Number of characters in the name */
7523 int nArg, /* Number of arguments. -1 means any number */
7524 u8 enc, /* Preferred text encoding */
7525 u8 createFlag /* Create new entry if true and does not otherwise exist */
7526 ){
7527 FuncDef *p; /* Iterator variable */
7528 FuncDef *pBest = 0; /* Best match found so far */
7529 int bestScore = 0; /* Score of best match */
7530 int h; /* Hash value */
7531
7532 assert( nArg>=(-2) );
7533 assert( nArg>=(-1) || createFlag==0 );
7534 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
7535
7536 /* First search for a match amongst the application-defined functions.
7537 */
7538 p = functionSearch(&db->aFunc, h, zName, nName);
7539 while( p ){
7540 int score = matchQuality(p, nArg, enc);
7541 if( score>bestScore ){
7542 pBest = p;
7543 bestScore = score;
7544 }
7545 p = p->pNext;
7546 }
7547
7548 /* If no match is found, search the built-in functions.
7549 **
7550 ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
7551 ** functions even if a prior app-defined function was found. And give
7552 ** priority to built-in functions.
7553 **
7554 ** Except, if createFlag is true, that means that we are trying to
7555 ** install a new function. Whatever FuncDef structure is returned it will
7556 ** have fields overwritten with new information appropriate for the
7557 ** new function. But the FuncDefs for built-in functions are read-only.
7558 ** So we must not search for built-ins when creating a new function.
7559 */
7560 if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
7561 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
7562 bestScore = 0;
7563 p = functionSearch(pHash, h, zName, nName);
7564 while( p ){
7565 int score = matchQuality(p, nArg, enc);
7566 if( score>bestScore ){
7567 pBest = p;
7568 bestScore = score;
7569 }
7570 p = p->pNext;
7571 }
7572 }
7573
7574 /* If the createFlag parameter is true and the search did not reveal an
7575 ** exact match for the name, number of arguments and encoding, then add a
7576 ** new entry to the hash table and return it.
7577 */
7578 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
7579 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
7580 pBest->zName = (char *)&pBest[1];
7581 pBest->nArg = (u16)nArg;
7582 pBest->funcFlags = enc;
7583 memcpy(pBest->zName, zName, nName);
7584 pBest->zName[nName] = 0;
7585 sqlite3FuncDefInsert(&db->aFunc, pBest);
7586 }
7587
7588 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
7589 return pBest;
7590 }
7591 return 0;
7592 }
7593
7594 /*
7595 ** Free all resources held by the schema structure. The void* argument points
7596 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
7597 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
7598 ** of the schema hash tables).
7599 **
7600 ** The Schema.cache_size variable is not cleared.
7601 */
7602 SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
7603 Hash temp1;
7604 Hash temp2;
7605 HashElem *pElem;
7606 Schema *pSchema = (Schema *)p;
7607
7608 temp1 = pSchema->tblHash;
7609 temp2 = pSchema->trigHash;
7610 sqlite3HashInit(&pSchema->trigHash);
7611 sqlite3HashClear(&pSchema->idxHash);
7612 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
7613 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
7614 }
7615 sqlite3HashClear(&temp2);
7616 sqlite3HashInit(&pSchema->tblHash);
7617 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
7618 Table *pTab = sqliteHashData(pElem);
7619 sqlite3DeleteTable(0, pTab);
7620 }
7621 sqlite3HashClear(&temp1);
7622 sqlite3HashClear(&pSchema->fkeyHash);
7623 pSchema->pSeqTab = 0;
7624 if( pSchema->schemaFlags & DB_SchemaLoaded ){
7625 pSchema->iGeneration++;
7626 pSchema->schemaFlags &= ~DB_SchemaLoaded;
7627 }
7628 }
7629
7630 /*
7631 ** Find and return the schema associated with a BTree. Create
7632 ** a new one if necessary.
7633 */
7634 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
7635 Schema * p;
7636 if( pBt ){
7637 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
7638 }else{
7639 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
7640 }
7641 if( !p ){
7642 db->mallocFailed = 1;
7643 }else if ( 0==p->file_format ){
7644 sqlite3HashInit(&p->tblHash);
7645 sqlite3HashInit(&p->idxHash);
7646 sqlite3HashInit(&p->trigHash);
7647 sqlite3HashInit(&p->fkeyHash);
7648 p->enc = SQLITE_UTF8;
7649 }
7650 return p;
7651 }
7652
7653 /************** End of callback.c ********************************************/
7654 /************** Begin file delete.c ******************************************/
7655 /*
7656 ** 2001 September 15
7657 **
7658 ** The author disclaims copyright to this source code. In place of
7659 ** a legal notice, here is a blessing:
7660 **
7661 ** May you do good and not evil.
7662 ** May you find forgiveness for yourself and forgive others.
7663 ** May you share freely, never taking more than you give.
7664 **
7665 *************************************************************************
7666 ** This file contains C code routines that are called by the parser
7667 ** in order to generate code for DELETE FROM statements.
7668 */
7669 /* #include "sqliteInt.h" */
7670
7671 /*
7672 ** While a SrcList can in general represent multiple tables and subqueries
7673 ** (as in the FROM clause of a SELECT statement) in this case it contains
7674 ** the name of a single table, as one might find in an INSERT, DELETE,
7675 ** or UPDATE statement. Look up that table in the symbol table and
7676 ** return a pointer. Set an error message and return NULL if the table
7677 ** name is not found or if any other error occurs.
7678 **
7679 ** The following fields are initialized appropriate in pSrc:
7680 **
7681 ** pSrc->a[0].pTab Pointer to the Table object
7682 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
7683 **
7684 */
7685 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
7686 struct SrcList_item *pItem = pSrc->a;
7687 Table *pTab;
7688 assert( pItem && pSrc->nSrc==1 );
7689 pTab = sqlite3LocateTableItem(pParse, 0, pItem);
7690 sqlite3DeleteTable(pParse->db, pItem->pTab);
7691 pItem->pTab = pTab;
7692 if( pTab ){
7693 pTab->nRef++;
7694 }
7695 if( sqlite3IndexedByLookup(pParse, pItem) ){
7696 pTab = 0;
7697 }
7698 return pTab;
7699 }
7700
7701 /*
7702 ** Check to make sure the given table is writable. If it is not
7703 ** writable, generate an error message and return 1. If it is
7704 ** writable return 0;
7705 */
7706 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
7707 /* A table is not writable under the following circumstances:
7708 **
7709 ** 1) It is a virtual table and no implementation of the xUpdate method
7710 ** has been provided, or
7711 ** 2) It is a system table (i.e. sqlite_master), this call is not
7712 ** part of a nested parse and writable_schema pragma has not
7713 ** been specified.
7714 **
7715 ** In either case leave an error message in pParse and return non-zero.
7716 */
7717 if( ( IsVirtual(pTab)
7718 && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
7719 || ( (pTab->tabFlags & TF_Readonly)!=0
7720 && (pParse->db->flags & SQLITE_WriteSchema)==0
7721 && pParse->nested==0 )
7722 ){
7723 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
7724 return 1;
7725 }
7726
7727 #ifndef SQLITE_OMIT_VIEW
7728 if( !viewOk && pTab->pSelect ){
7729 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
7730 return 1;
7731 }
7732 #endif
7733 return 0;
7734 }
7735
7736
7737 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
7738 /*
7739 ** Evaluate a view and store its result in an ephemeral table. The
7740 ** pWhere argument is an optional WHERE clause that restricts the
7741 ** set of rows in the view that are to be added to the ephemeral table.
7742 */
7743 SQLITE_PRIVATE void sqlite3MaterializeView(
7744 Parse *pParse, /* Parsing context */
7745 Table *pView, /* View definition */
7746 Expr *pWhere, /* Optional WHERE clause to be added */
7747 int iCur /* Cursor number for ephemeral table */
7748 ){
7749 SelectDest dest;
7750 Select *pSel;
7751 SrcList *pFrom;
7752 sqlite3 *db = pParse->db;
7753 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
7754 pWhere = sqlite3ExprDup(db, pWhere, 0);
7755 pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
7756 if( pFrom ){
7757 assert( pFrom->nSrc==1 );
7758 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
7759 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
7760 assert( pFrom->a[0].pOn==0 );
7761 assert( pFrom->a[0].pUsing==0 );
7762 }
7763 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0,
7764 SF_IncludeHidden, 0, 0);
7765 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
7766 sqlite3Select(pParse, pSel, &dest);
7767 sqlite3SelectDelete(db, pSel);
7768 }
7769 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
7770
7771 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
7772 /*
7773 ** Generate an expression tree to implement the WHERE, ORDER BY,
7774 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
7775 **
7776 ** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
7777 ** \__________________________/
7778 ** pLimitWhere (pInClause)
7779 */
7780 SQLITE_PRIVATE Expr *sqlite3LimitWhere(
7781 Parse *pParse, /* The parser context */
7782 SrcList *pSrc, /* the FROM clause -- which tables to scan */
7783 Expr *pWhere, /* The WHERE clause. May be null */
7784 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
7785 Expr *pLimit, /* The LIMIT clause. May be null */
7786 Expr *pOffset, /* The OFFSET clause. May be null */
7787 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
7788 ){
7789 Expr *pWhereRowid = NULL; /* WHERE rowid .. */
7790 Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */
7791 Expr *pSelectRowid = NULL; /* SELECT rowid ... */
7792 ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */
7793 SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */
7794 Select *pSelect = NULL; /* Complete SELECT tree */
7795
7796 /* Check that there isn't an ORDER BY without a LIMIT clause.
7797 */
7798 if( pOrderBy && (pLimit == 0) ) {
7799 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
7800 goto limit_where_cleanup_2;
7801 }
7802
7803 /* We only need to generate a select expression if there
7804 ** is a limit/offset term to enforce.
7805 */
7806 if( pLimit == 0 ) {
7807 /* if pLimit is null, pOffset will always be null as well. */
7808 assert( pOffset == 0 );
7809 return pWhere;
7810 }
7811
7812 /* Generate a select expression tree to enforce the limit/offset
7813 ** term for the DELETE or UPDATE statement. For example:
7814 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
7815 ** becomes:
7816 ** DELETE FROM table_a WHERE rowid IN (
7817 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
7818 ** );
7819 */
7820
7821 pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
7822 if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
7823 pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
7824 if( pEList == 0 ) goto limit_where_cleanup_2;
7825
7826 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
7827 ** and the SELECT subtree. */
7828 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
7829 if( pSelectSrc == 0 ) {
7830 sqlite3ExprListDelete(pParse->db, pEList);
7831 goto limit_where_cleanup_2;
7832 }
7833
7834 /* generate the SELECT expression tree. */
7835 pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
7836 pOrderBy,0,pLimit,pOffset);
7837 if( pSelect == 0 ) return 0;
7838
7839 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
7840 pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
7841 if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
7842 pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
7843 if( pInClause == 0 ) goto limit_where_cleanup_1;
7844
7845 pInClause->x.pSelect = pSelect;
7846 pInClause->flags |= EP_xIsSelect;
7847 sqlite3ExprSetHeightAndFlags(pParse, pInClause);
7848 return pInClause;
7849
7850 /* something went wrong. clean up anything allocated. */
7851 limit_where_cleanup_1:
7852 sqlite3SelectDelete(pParse->db, pSelect);
7853 return 0;
7854
7855 limit_where_cleanup_2:
7856 sqlite3ExprDelete(pParse->db, pWhere);
7857 sqlite3ExprListDelete(pParse->db, pOrderBy);
7858 sqlite3ExprDelete(pParse->db, pLimit);
7859 sqlite3ExprDelete(pParse->db, pOffset);
7860 return 0;
7861 }
7862 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
7863 /* && !defined(SQLITE_OMIT_SUBQUERY) */
7864
7865 /*
7866 ** Generate code for a DELETE FROM statement.
7867 **
7868 ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
7869 ** \________/ \________________/
7870 ** pTabList pWhere
7871 */
7872 SQLITE_PRIVATE void sqlite3DeleteFrom(
7873 Parse *pParse, /* The parser context */
7874 SrcList *pTabList, /* The table from which we should delete things */
7875 Expr *pWhere /* The WHERE clause. May be null */
7876 ){
7877 Vdbe *v; /* The virtual database engine */
7878 Table *pTab; /* The table from which records will be deleted */
7879 const char *zDb; /* Name of database holding pTab */
7880 int i; /* Loop counter */
7881 WhereInfo *pWInfo; /* Information about the WHERE clause */
7882 Index *pIdx; /* For looping over indices of the table */
7883 int iTabCur; /* Cursor number for the table */
7884 int iDataCur = 0; /* VDBE cursor for the canonical data source */
7885 int iIdxCur = 0; /* Cursor number of the first index */
7886 int nIdx; /* Number of indices */
7887 sqlite3 *db; /* Main database structure */
7888 AuthContext sContext; /* Authorization context */
7889 NameContext sNC; /* Name context to resolve expressions in */
7890 int iDb; /* Database number */
7891 int memCnt = -1; /* Memory cell used for change counting */
7892 int rcauth; /* Value returned by authorization callback */
7893 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
7894 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
7895 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
7896 Index *pPk; /* The PRIMARY KEY index on the table */
7897 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
7898 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
7899 int iKey; /* Memory cell holding key of row to be deleted */
7900 i16 nKey; /* Number of memory cells in the row key */
7901 int iEphCur = 0; /* Ephemeral table holding all primary key values */
7902 int iRowSet = 0; /* Register for rowset of rows to delete */
7903 int addrBypass = 0; /* Address of jump over the delete logic */
7904 int addrLoop = 0; /* Top of the delete loop */
7905 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
7906
7907 #ifndef SQLITE_OMIT_TRIGGER
7908 int isView; /* True if attempting to delete from a view */
7909 Trigger *pTrigger; /* List of table triggers, if required */
7910 int bComplex; /* True if there are either triggers or FKs */
7911 #endif
7912
7913 memset(&sContext, 0, sizeof(sContext));
7914 db = pParse->db;
7915 if( pParse->nErr || db->mallocFailed ){
7916 goto delete_from_cleanup;
7917 }
7918 assert( pTabList->nSrc==1 );
7919
7920 /* Locate the table which we want to delete. This table has to be
7921 ** put in an SrcList structure because some of the subroutines we
7922 ** will be calling are designed to work with multiple tables and expect
7923 ** an SrcList* parameter instead of just a Table* parameter.
7924 */
7925 pTab = sqlite3SrcListLookup(pParse, pTabList);
7926 if( pTab==0 ) goto delete_from_cleanup;
7927
7928 /* Figure out if we have any triggers and if the table being
7929 ** deleted from is a view
7930 */
7931 #ifndef SQLITE_OMIT_TRIGGER
7932 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
7933 isView = pTab->pSelect!=0;
7934 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
7935 #else
7936 # define pTrigger 0
7937 # define isView 0
7938 # define bComplex 0
7939 #endif
7940 #ifdef SQLITE_OMIT_VIEW
7941 # undef isView
7942 # define isView 0
7943 #endif
7944
7945 /* If pTab is really a view, make sure it has been initialized.
7946 */
7947 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
7948 goto delete_from_cleanup;
7949 }
7950
7951 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
7952 goto delete_from_cleanup;
7953 }
7954 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
7955 assert( iDb<db->nDb );
7956 zDb = db->aDb[iDb].zName;
7957 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
7958 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
7959 if( rcauth==SQLITE_DENY ){
7960 goto delete_from_cleanup;
7961 }
7962 assert(!isView || pTrigger);
7963
7964 /* Assign cursor numbers to the table and all its indices.
7965 */
7966 assert( pTabList->nSrc==1 );
7967 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
7968 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
7969 pParse->nTab++;
7970 }
7971
7972 /* Start the view context
7973 */
7974 if( isView ){
7975 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
7976 }
7977
7978 /* Begin generating code.
7979 */
7980 v = sqlite3GetVdbe(pParse);
7981 if( v==0 ){
7982 goto delete_from_cleanup;
7983 }
7984 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
7985 sqlite3BeginWriteOperation(pParse, 1, iDb);
7986
7987 /* If we are trying to delete from a view, realize that view into
7988 ** an ephemeral table.
7989 */
7990 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
7991 if( isView ){
7992 sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
7993 iDataCur = iIdxCur = iTabCur;
7994 }
7995 #endif
7996
7997 /* Resolve the column names in the WHERE clause.
7998 */
7999 memset(&sNC, 0, sizeof(sNC));
8000 sNC.pParse = pParse;
8001 sNC.pSrcList = pTabList;
8002 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
8003 goto delete_from_cleanup;
8004 }
8005
8006 /* Initialize the counter of the number of rows deleted, if
8007 ** we are counting rows.
8008 */
8009 if( db->flags & SQLITE_CountRows ){
8010 memCnt = ++pParse->nMem;
8011 sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
8012 }
8013
8014 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
8015 /* Special case: A DELETE without a WHERE clause deletes everything.
8016 ** It is easier just to erase the whole table. Prior to version 3.6.5,
8017 ** this optimization caused the row change count (the value returned by
8018 ** API function sqlite3_count_changes) to be set incorrectly. */
8019 if( rcauth==SQLITE_OK
8020 && pWhere==0
8021 && !bComplex
8022 && !IsVirtual(pTab)
8023 ){
8024 assert( !isView );
8025 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
8026 if( HasRowid(pTab) ){
8027 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
8028 pTab->zName, P4_STATIC);
8029 }
8030 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
8031 assert( pIdx->pSchema==pTab->pSchema );
8032 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
8033 }
8034 }else
8035 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
8036 {
8037 u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK;
8038 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
8039 if( HasRowid(pTab) ){
8040 /* For a rowid table, initialize the RowSet to an empty set */
8041 pPk = 0;
8042 nPk = 1;
8043 iRowSet = ++pParse->nMem;
8044 sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
8045 }else{
8046 /* For a WITHOUT ROWID table, create an ephemeral table used to
8047 ** hold all primary keys for rows to be deleted. */
8048 pPk = sqlite3PrimaryKeyIndex(pTab);
8049 assert( pPk!=0 );
8050 nPk = pPk->nKeyCol;
8051 iPk = pParse->nMem+1;
8052 pParse->nMem += nPk;
8053 iEphCur = pParse->nTab++;
8054 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
8055 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
8056 }
8057
8058 /* Construct a query to find the rowid or primary key for every row
8059 ** to be deleted, based on the WHERE clause. Set variable eOnePass
8060 ** to indicate the strategy used to implement this delete:
8061 **
8062 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
8063 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
8064 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
8065 */
8066 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
8067 if( pWInfo==0 ) goto delete_from_cleanup;
8068 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
8069 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
8070 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
8071
8072 /* Keep track of the number of rows to be deleted */
8073 if( db->flags & SQLITE_CountRows ){
8074 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
8075 }
8076
8077 /* Extract the rowid or primary key for the current row */
8078 if( pPk ){
8079 for(i=0; i<nPk; i++){
8080 assert( pPk->aiColumn[i]>=0 );
8081 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
8082 pPk->aiColumn[i], iPk+i);
8083 }
8084 iKey = iPk;
8085 }else{
8086 iKey = pParse->nMem + 1;
8087 iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
8088 if( iKey>pParse->nMem ) pParse->nMem = iKey;
8089 }
8090
8091 if( eOnePass!=ONEPASS_OFF ){
8092 /* For ONEPASS, no need to store the rowid/primary-key. There is only
8093 ** one, so just keep it in its register(s) and fall through to the
8094 ** delete code. */
8095 nKey = nPk; /* OP_Found will use an unpacked key */
8096 aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
8097 if( aToOpen==0 ){
8098 sqlite3WhereEnd(pWInfo);
8099 goto delete_from_cleanup;
8100 }
8101 memset(aToOpen, 1, nIdx+1);
8102 aToOpen[nIdx+1] = 0;
8103 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
8104 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
8105 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
8106 }else{
8107 if( pPk ){
8108 /* Add the PK key for this row to the temporary table */
8109 iKey = ++pParse->nMem;
8110 nKey = 0; /* Zero tells OP_Found to use a composite key */
8111 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
8112 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
8113 sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
8114 }else{
8115 /* Add the rowid of the row to be deleted to the RowSet */
8116 nKey = 1; /* OP_Seek always uses a single rowid */
8117 sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
8118 }
8119 }
8120
8121 /* If this DELETE cannot use the ONEPASS strategy, this is the
8122 ** end of the WHERE loop */
8123 if( eOnePass!=ONEPASS_OFF ){
8124 addrBypass = sqlite3VdbeMakeLabel(v);
8125 }else{
8126 sqlite3WhereEnd(pWInfo);
8127 }
8128
8129 /* Unless this is a view, open cursors for the table we are
8130 ** deleting from and all its indices. If this is a view, then the
8131 ** only effect this statement has is to fire the INSTEAD OF
8132 ** triggers.
8133 */
8134 if( !isView ){
8135 int iAddrOnce = 0;
8136 u8 p5 = (eOnePass==ONEPASS_OFF ? 0 : OPFLAG_FORDELETE);
8137 if( eOnePass==ONEPASS_MULTI ){
8138 iAddrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
8139 }
8140 testcase( IsVirtual(pTab) );
8141 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, p5, iTabCur,
8142 aToOpen, &iDataCur, &iIdxCur);
8143 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
8144 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
8145 if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
8146 }
8147
8148 /* Set up a loop over the rowids/primary-keys that were found in the
8149 ** where-clause loop above.
8150 */
8151 if( eOnePass!=ONEPASS_OFF ){
8152 assert( nKey==nPk ); /* OP_Found will use an unpacked key */
8153 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
8154 assert( pPk!=0 || pTab->pSelect!=0 );
8155 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
8156 VdbeCoverage(v);
8157 }
8158 }else if( pPk ){
8159 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
8160 sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
8161 assert( nKey==0 ); /* OP_Found will use a composite key */
8162 }else{
8163 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
8164 VdbeCoverage(v);
8165 assert( nKey==1 );
8166 }
8167
8168 /* Delete the row */
8169 #ifndef SQLITE_OMIT_VIRTUALTABLE
8170 if( IsVirtual(pTab) ){
8171 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
8172 sqlite3VtabMakeWritable(pParse, pTab);
8173 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
8174 sqlite3VdbeChangeP5(v, OE_Abort);
8175 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
8176 sqlite3MayAbort(pParse);
8177 if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){
8178 pParse->isMultiWrite = 0;
8179 }
8180 }else
8181 #endif
8182 {
8183 int count = (pParse->nested==0); /* True to count changes */
8184 int iIdxNoSeek = -1;
8185 if( bComplex==0 && aiCurOnePass[1]!=iDataCur ){
8186 iIdxNoSeek = aiCurOnePass[1];
8187 }
8188 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
8189 iKey, nKey, count, OE_Default, eOnePass, iIdxNoSeek);
8190 }
8191
8192 /* End of the loop over all rowids/primary-keys. */
8193 if( eOnePass!=ONEPASS_OFF ){
8194 sqlite3VdbeResolveLabel(v, addrBypass);
8195 sqlite3WhereEnd(pWInfo);
8196 }else if( pPk ){
8197 sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
8198 sqlite3VdbeJumpHere(v, addrLoop);
8199 }else{
8200 sqlite3VdbeGoto(v, addrLoop);
8201 sqlite3VdbeJumpHere(v, addrLoop);
8202 }
8203
8204 /* Close the cursors open on the table and its indexes. */
8205 if( !isView && !IsVirtual(pTab) ){
8206 if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
8207 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
8208 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
8209 }
8210 }
8211 } /* End non-truncate path */
8212
8213 /* Update the sqlite_sequence table by storing the content of the
8214 ** maximum rowid counter values recorded while inserting into
8215 ** autoincrement tables.
8216 */
8217 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
8218 sqlite3AutoincrementEnd(pParse);
8219 }
8220
8221 /* Return the number of rows that were deleted. If this routine is
8222 ** generating code because of a call to sqlite3NestedParse(), do not
8223 ** invoke the callback function.
8224 */
8225 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
8226 sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
8227 sqlite3VdbeSetNumCols(v, 1);
8228 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
8229 }
8230
8231 delete_from_cleanup:
8232 sqlite3AuthContextPop(&sContext);
8233 sqlite3SrcListDelete(db, pTabList);
8234 sqlite3ExprDelete(db, pWhere);
8235 sqlite3DbFree(db, aToOpen);
8236 return;
8237 }
8238 /* Make sure "isView" and other macros defined above are undefined. Otherwise
8239 ** they may interfere with compilation of other functions in this file
8240 ** (or in another file, if this file becomes part of the amalgamation). */
8241 #ifdef isView
8242 #undef isView
8243 #endif
8244 #ifdef pTrigger
8245 #undef pTrigger
8246 #endif
8247
8248 /*
8249 ** This routine generates VDBE code that causes a single row of a
8250 ** single table to be deleted. Both the original table entry and
8251 ** all indices are removed.
8252 **
8253 ** Preconditions:
8254 **
8255 ** 1. iDataCur is an open cursor on the btree that is the canonical data
8256 ** store for the table. (This will be either the table itself,
8257 ** in the case of a rowid table, or the PRIMARY KEY index in the case
8258 ** of a WITHOUT ROWID table.)
8259 **
8260 ** 2. Read/write cursors for all indices of pTab must be open as
8261 ** cursor number iIdxCur+i for the i-th index.
8262 **
8263 ** 3. The primary key for the row to be deleted must be stored in a
8264 ** sequence of nPk memory cells starting at iPk. If nPk==0 that means
8265 ** that a search record formed from OP_MakeRecord is contained in the
8266 ** single memory location iPk.
8267 **
8268 ** eMode:
8269 ** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
8270 ** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
8271 ** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
8272 ** then this function must seek iDataCur to the entry identified by iPk
8273 ** and nPk before reading from it.
8274 **
8275 ** If eMode is ONEPASS_MULTI, then this call is being made as part
8276 ** of a ONEPASS delete that affects multiple rows. In this case, if
8277 ** iIdxNoSeek is a valid cursor number (>=0), then its position should
8278 ** be preserved following the delete operation. Or, if iIdxNoSeek is not
8279 ** a valid cursor number, the position of iDataCur should be preserved
8280 ** instead.
8281 **
8282 ** iIdxNoSeek:
8283 ** If iIdxNoSeek is a valid cursor number (>=0), then it identifies an
8284 ** index cursor (from within array of cursors starting at iIdxCur) that
8285 ** already points to the index entry to be deleted.
8286 */
8287 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
8288 Parse *pParse, /* Parsing context */
8289 Table *pTab, /* Table containing the row to be deleted */
8290 Trigger *pTrigger, /* List of triggers to (potentially) fire */
8291 int iDataCur, /* Cursor from which column data is extracted */
8292 int iIdxCur, /* First index cursor */
8293 int iPk, /* First memory cell containing the PRIMARY KEY */
8294 i16 nPk, /* Number of PRIMARY KEY memory cells */
8295 u8 count, /* If non-zero, increment the row change counter */
8296 u8 onconf, /* Default ON CONFLICT policy for triggers */
8297 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
8298 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
8299 ){
8300 Vdbe *v = pParse->pVdbe; /* Vdbe */
8301 int iOld = 0; /* First register in OLD.* array */
8302 int iLabel; /* Label resolved to end of generated code */
8303 u8 opSeek; /* Seek opcode */
8304
8305 /* Vdbe is guaranteed to have been allocated by this stage. */
8306 assert( v );
8307 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
8308 iDataCur, iIdxCur, iPk, (int)nPk));
8309
8310 /* Seek cursor iCur to the row to delete. If this row no longer exists
8311 ** (this can happen if a trigger program has already deleted it), do
8312 ** not attempt to delete it or fire any DELETE triggers. */
8313 iLabel = sqlite3VdbeMakeLabel(v);
8314 opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
8315 if( eMode==ONEPASS_OFF ){
8316 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
8317 VdbeCoverageIf(v, opSeek==OP_NotExists);
8318 VdbeCoverageIf(v, opSeek==OP_NotFound);
8319 }
8320
8321 /* If there are any triggers to fire, allocate a range of registers to
8322 ** use for the old.* references in the triggers. */
8323 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
8324 u32 mask; /* Mask of OLD.* columns in use */
8325 int iCol; /* Iterator used while populating OLD.* */
8326 int addrStart; /* Start of BEFORE trigger programs */
8327
8328 /* TODO: Could use temporary registers here. Also could attempt to
8329 ** avoid copying the contents of the rowid register. */
8330 mask = sqlite3TriggerColmask(
8331 pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
8332 );
8333 mask |= sqlite3FkOldmask(pParse, pTab);
8334 iOld = pParse->nMem+1;
8335 pParse->nMem += (1 + pTab->nCol);
8336
8337 /* Populate the OLD.* pseudo-table register array. These values will be
8338 ** used by any BEFORE and AFTER triggers that exist. */
8339 sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
8340 for(iCol=0; iCol<pTab->nCol; iCol++){
8341 testcase( mask!=0xffffffff && iCol==31 );
8342 testcase( mask!=0xffffffff && iCol==32 );
8343 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
8344 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
8345 }
8346 }
8347
8348 /* Invoke BEFORE DELETE trigger programs. */
8349 addrStart = sqlite3VdbeCurrentAddr(v);
8350 sqlite3CodeRowTrigger(pParse, pTrigger,
8351 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
8352 );
8353
8354 /* If any BEFORE triggers were coded, then seek the cursor to the
8355 ** row to be deleted again. It may be that the BEFORE triggers moved
8356 ** the cursor or of already deleted the row that the cursor was
8357 ** pointing to.
8358 */
8359 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
8360 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
8361 VdbeCoverageIf(v, opSeek==OP_NotExists);
8362 VdbeCoverageIf(v, opSeek==OP_NotFound);
8363 }
8364
8365 /* Do FK processing. This call checks that any FK constraints that
8366 ** refer to this table (i.e. constraints attached to other tables)
8367 ** are not violated by deleting this row. */
8368 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
8369 }
8370
8371 /* Delete the index and table entries. Skip this step if pTab is really
8372 ** a view (in which case the only effect of the DELETE statement is to
8373 ** fire the INSTEAD OF triggers). */
8374 if( pTab->pSelect==0 ){
8375 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
8376 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
8377 if( count ){
8378 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
8379 }
8380 if( iIdxNoSeek>=0 ){
8381 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
8382 }
8383 sqlite3VdbeChangeP5(v, eMode==ONEPASS_MULTI);
8384 }
8385
8386 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
8387 ** handle rows (possibly in other tables) that refer via a foreign key
8388 ** to the row just deleted. */
8389 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
8390
8391 /* Invoke AFTER DELETE trigger programs. */
8392 sqlite3CodeRowTrigger(pParse, pTrigger,
8393 TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
8394 );
8395
8396 /* Jump here if the row had already been deleted before any BEFORE
8397 ** trigger programs were invoked. Or if a trigger program throws a
8398 ** RAISE(IGNORE) exception. */
8399 sqlite3VdbeResolveLabel(v, iLabel);
8400 VdbeModuleComment((v, "END: GenRowDel()"));
8401 }
8402
8403 /*
8404 ** This routine generates VDBE code that causes the deletion of all
8405 ** index entries associated with a single row of a single table, pTab
8406 **
8407 ** Preconditions:
8408 **
8409 ** 1. A read/write cursor "iDataCur" must be open on the canonical storage
8410 ** btree for the table pTab. (This will be either the table itself
8411 ** for rowid tables or to the primary key index for WITHOUT ROWID
8412 ** tables.)
8413 **
8414 ** 2. Read/write cursors for all indices of pTab must be open as
8415 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
8416 ** index is the 0-th index.)
8417 **
8418 ** 3. The "iDataCur" cursor must be already be positioned on the row
8419 ** that is to be deleted.
8420 */
8421 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
8422 Parse *pParse, /* Parsing and code generating context */
8423 Table *pTab, /* Table containing the row to be deleted */
8424 int iDataCur, /* Cursor of table holding data. */
8425 int iIdxCur, /* First index cursor */
8426 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
8427 int iIdxNoSeek /* Do not delete from this cursor */
8428 ){
8429 int i; /* Index loop counter */
8430 int r1 = -1; /* Register holding an index key */
8431 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
8432 Index *pIdx; /* Current index */
8433 Index *pPrior = 0; /* Prior index */
8434 Vdbe *v; /* The prepared statement under construction */
8435 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
8436
8437 v = pParse->pVdbe;
8438 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
8439 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
8440 assert( iIdxCur+i!=iDataCur || pPk==pIdx );
8441 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
8442 if( pIdx==pPk ) continue;
8443 if( iIdxCur+i==iIdxNoSeek ) continue;
8444 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
8445 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
8446 &iPartIdxLabel, pPrior, r1);
8447 sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
8448 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
8449 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
8450 pPrior = pIdx;
8451 }
8452 }
8453
8454 /*
8455 ** Generate code that will assemble an index key and stores it in register
8456 ** regOut. The key with be for index pIdx which is an index on pTab.
8457 ** iCur is the index of a cursor open on the pTab table and pointing to
8458 ** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
8459 ** iCur must be the cursor of the PRIMARY KEY index.
8460 **
8461 ** Return a register number which is the first in a block of
8462 ** registers that holds the elements of the index key. The
8463 ** block of registers has already been deallocated by the time
8464 ** this routine returns.
8465 **
8466 ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
8467 ** to that label if pIdx is a partial index that should be skipped.
8468 ** The label should be resolved using sqlite3ResolvePartIdxLabel().
8469 ** A partial index should be skipped if its WHERE clause evaluates
8470 ** to false or null. If pIdx is not a partial index, *piPartIdxLabel
8471 ** will be set to zero which is an empty label that is ignored by
8472 ** sqlite3ResolvePartIdxLabel().
8473 **
8474 ** The pPrior and regPrior parameters are used to implement a cache to
8475 ** avoid unnecessary register loads. If pPrior is not NULL, then it is
8476 ** a pointer to a different index for which an index key has just been
8477 ** computed into register regPrior. If the current pIdx index is generating
8478 ** its key into the same sequence of registers and if pPrior and pIdx share
8479 ** a column in common, then the register corresponding to that column already
8480 ** holds the correct value and the loading of that register is skipped.
8481 ** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
8482 ** on a table with multiple indices, and especially with the ROWID or
8483 ** PRIMARY KEY columns of the index.
8484 */
8485 SQLITE_PRIVATE int sqlite3GenerateIndexKey(
8486 Parse *pParse, /* Parsing context */
8487 Index *pIdx, /* The index for which to generate a key */
8488 int iDataCur, /* Cursor number from which to take column data */
8489 int regOut, /* Put the new key into this register if not 0 */
8490 int prefixOnly, /* Compute only a unique prefix of the key */
8491 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
8492 Index *pPrior, /* Previously generated index key */
8493 int regPrior /* Register holding previous generated key */
8494 ){
8495 Vdbe *v = pParse->pVdbe;
8496 int j;
8497 int regBase;
8498 int nCol;
8499
8500 if( piPartIdxLabel ){
8501 if( pIdx->pPartIdxWhere ){
8502 *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
8503 pParse->iSelfTab = iDataCur;
8504 sqlite3ExprCachePush(pParse);
8505 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
8506 SQLITE_JUMPIFNULL);
8507 }else{
8508 *piPartIdxLabel = 0;
8509 }
8510 }
8511 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
8512 regBase = sqlite3GetTempRange(pParse, nCol);
8513 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
8514 for(j=0; j<nCol; j++){
8515 if( pPrior
8516 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
8517 && pPrior->aiColumn[j]!=XN_EXPR
8518 ){
8519 /* This column was already computed by the previous index */
8520 continue;
8521 }
8522 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
8523 /* If the column affinity is REAL but the number is an integer, then it
8524 ** might be stored in the table as an integer (using a compact
8525 ** representation) then converted to REAL by an OP_RealAffinity opcode.
8526 ** But we are getting ready to store this value back into an index, where
8527 ** it should be converted by to INTEGER again. So omit the OP_RealAffinity
8528 ** opcode if it is present */
8529 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
8530 }
8531 if( regOut ){
8532 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
8533 }
8534 sqlite3ReleaseTempRange(pParse, regBase, nCol);
8535 return regBase;
8536 }
8537
8538 /*
8539 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
8540 ** because it was a partial index, then this routine should be called to
8541 ** resolve that label.
8542 */
8543 SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
8544 if( iLabel ){
8545 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
8546 sqlite3ExprCachePop(pParse);
8547 }
8548 }
8549
8550 /************** End of delete.c **********************************************/
8551 /************** Begin file func.c ********************************************/
8552 /*
8553 ** 2002 February 23
8554 **
8555 ** The author disclaims copyright to this source code. In place of
8556 ** a legal notice, here is a blessing:
8557 **
8558 ** May you do good and not evil.
8559 ** May you find forgiveness for yourself and forgive others.
8560 ** May you share freely, never taking more than you give.
8561 **
8562 *************************************************************************
8563 ** This file contains the C-language implementations for many of the SQL
8564 ** functions of SQLite. (Some function, and in particular the date and
8565 ** time functions, are implemented separately.)
8566 */
8567 /* #include "sqliteInt.h" */
8568 /* #include <stdlib.h> */
8569 /* #include <assert.h> */
8570 /* #include "vdbeInt.h" */
8571
8572 /*
8573 ** Return the collating function associated with a function.
8574 */
8575 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
8576 VdbeOp *pOp;
8577 assert( context->pVdbe!=0 );
8578 pOp = &context->pVdbe->aOp[context->iOp-1];
8579 assert( pOp->opcode==OP_CollSeq );
8580 assert( pOp->p4type==P4_COLLSEQ );
8581 return pOp->p4.pColl;
8582 }
8583
8584 /*
8585 ** Indicate that the accumulator load should be skipped on this
8586 ** iteration of the aggregate loop.
8587 */
8588 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
8589 context->skipFlag = 1;
8590 }
8591
8592 /*
8593 ** Implementation of the non-aggregate min() and max() functions
8594 */
8595 static void minmaxFunc(
8596 sqlite3_context *context,
8597 int argc,
8598 sqlite3_value **argv
8599 ){
8600 int i;
8601 int mask; /* 0 for min() or 0xffffffff for max() */
8602 int iBest;
8603 CollSeq *pColl;
8604
8605 assert( argc>1 );
8606 mask = sqlite3_user_data(context)==0 ? 0 : -1;
8607 pColl = sqlite3GetFuncCollSeq(context);
8608 assert( pColl );
8609 assert( mask==-1 || mask==0 );
8610 iBest = 0;
8611 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
8612 for(i=1; i<argc; i++){
8613 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
8614 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
8615 testcase( mask==0 );
8616 iBest = i;
8617 }
8618 }
8619 sqlite3_result_value(context, argv[iBest]);
8620 }
8621
8622 /*
8623 ** Return the type of the argument.
8624 */
8625 static void typeofFunc(
8626 sqlite3_context *context,
8627 int NotUsed,
8628 sqlite3_value **argv
8629 ){
8630 const char *z = 0;
8631 UNUSED_PARAMETER(NotUsed);
8632 switch( sqlite3_value_type(argv[0]) ){
8633 case SQLITE_INTEGER: z = "integer"; break;
8634 case SQLITE_TEXT: z = "text"; break;
8635 case SQLITE_FLOAT: z = "real"; break;
8636 case SQLITE_BLOB: z = "blob"; break;
8637 default: z = "null"; break;
8638 }
8639 sqlite3_result_text(context, z, -1, SQLITE_STATIC);
8640 }
8641
8642
8643 /*
8644 ** Implementation of the length() function
8645 */
8646 static void lengthFunc(
8647 sqlite3_context *context,
8648 int argc,
8649 sqlite3_value **argv
8650 ){
8651 int len;
8652
8653 assert( argc==1 );
8654 UNUSED_PARAMETER(argc);
8655 switch( sqlite3_value_type(argv[0]) ){
8656 case SQLITE_BLOB:
8657 case SQLITE_INTEGER:
8658 case SQLITE_FLOAT: {
8659 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
8660 break;
8661 }
8662 case SQLITE_TEXT: {
8663 const unsigned char *z = sqlite3_value_text(argv[0]);
8664 if( z==0 ) return;
8665 len = 0;
8666 while( *z ){
8667 len++;
8668 SQLITE_SKIP_UTF8(z);
8669 }
8670 sqlite3_result_int(context, len);
8671 break;
8672 }
8673 default: {
8674 sqlite3_result_null(context);
8675 break;
8676 }
8677 }
8678 }
8679
8680 /*
8681 ** Implementation of the abs() function.
8682 **
8683 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
8684 ** the numeric argument X.
8685 */
8686 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
8687 assert( argc==1 );
8688 UNUSED_PARAMETER(argc);
8689 switch( sqlite3_value_type(argv[0]) ){
8690 case SQLITE_INTEGER: {
8691 i64 iVal = sqlite3_value_int64(argv[0]);
8692 if( iVal<0 ){
8693 if( iVal==SMALLEST_INT64 ){
8694 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
8695 ** then abs(X) throws an integer overflow error since there is no
8696 ** equivalent positive 64-bit two complement value. */
8697 sqlite3_result_error(context, "integer overflow", -1);
8698 return;
8699 }
8700 iVal = -iVal;
8701 }
8702 sqlite3_result_int64(context, iVal);
8703 break;
8704 }
8705 case SQLITE_NULL: {
8706 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
8707 sqlite3_result_null(context);
8708 break;
8709 }
8710 default: {
8711 /* Because sqlite3_value_double() returns 0.0 if the argument is not
8712 ** something that can be converted into a number, we have:
8713 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
8714 ** that cannot be converted to a numeric value.
8715 */
8716 double rVal = sqlite3_value_double(argv[0]);
8717 if( rVal<0 ) rVal = -rVal;
8718 sqlite3_result_double(context, rVal);
8719 break;
8720 }
8721 }
8722 }
8723
8724 /*
8725 ** Implementation of the instr() function.
8726 **
8727 ** instr(haystack,needle) finds the first occurrence of needle
8728 ** in haystack and returns the number of previous characters plus 1,
8729 ** or 0 if needle does not occur within haystack.
8730 **
8731 ** If both haystack and needle are BLOBs, then the result is one more than
8732 ** the number of bytes in haystack prior to the first occurrence of needle,
8733 ** or 0 if needle never occurs in haystack.
8734 */
8735 static void instrFunc(
8736 sqlite3_context *context,
8737 int argc,
8738 sqlite3_value **argv
8739 ){
8740 const unsigned char *zHaystack;
8741 const unsigned char *zNeedle;
8742 int nHaystack;
8743 int nNeedle;
8744 int typeHaystack, typeNeedle;
8745 int N = 1;
8746 int isText;
8747
8748 UNUSED_PARAMETER(argc);
8749 typeHaystack = sqlite3_value_type(argv[0]);
8750 typeNeedle = sqlite3_value_type(argv[1]);
8751 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
8752 nHaystack = sqlite3_value_bytes(argv[0]);
8753 nNeedle = sqlite3_value_bytes(argv[1]);
8754 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
8755 zHaystack = sqlite3_value_blob(argv[0]);
8756 zNeedle = sqlite3_value_blob(argv[1]);
8757 isText = 0;
8758 }else{
8759 zHaystack = sqlite3_value_text(argv[0]);
8760 zNeedle = sqlite3_value_text(argv[1]);
8761 isText = 1;
8762 }
8763 while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
8764 N++;
8765 do{
8766 nHaystack--;
8767 zHaystack++;
8768 }while( isText && (zHaystack[0]&0xc0)==0x80 );
8769 }
8770 if( nNeedle>nHaystack ) N = 0;
8771 sqlite3_result_int(context, N);
8772 }
8773
8774 /*
8775 ** Implementation of the printf() function.
8776 */
8777 static void printfFunc(
8778 sqlite3_context *context,
8779 int argc,
8780 sqlite3_value **argv
8781 ){
8782 PrintfArguments x;
8783 StrAccum str;
8784 const char *zFormat;
8785 int n;
8786 sqlite3 *db = sqlite3_context_db_handle(context);
8787
8788 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
8789 x.nArg = argc-1;
8790 x.nUsed = 0;
8791 x.apArg = argv+1;
8792 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
8793 sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
8794 n = str.nChar;
8795 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
8796 SQLITE_DYNAMIC);
8797 }
8798 }
8799
8800 /*
8801 ** Implementation of the substr() function.
8802 **
8803 ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
8804 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
8805 ** of x. If x is text, then we actually count UTF-8 characters.
8806 ** If x is a blob, then we count bytes.
8807 **
8808 ** If p1 is negative, then we begin abs(p1) from the end of x[].
8809 **
8810 ** If p2 is negative, return the p2 characters preceding p1.
8811 */
8812 static void substrFunc(
8813 sqlite3_context *context,
8814 int argc,
8815 sqlite3_value **argv
8816 ){
8817 const unsigned char *z;
8818 const unsigned char *z2;
8819 int len;
8820 int p0type;
8821 i64 p1, p2;
8822 int negP2 = 0;
8823
8824 assert( argc==3 || argc==2 );
8825 if( sqlite3_value_type(argv[1])==SQLITE_NULL
8826 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
8827 ){
8828 return;
8829 }
8830 p0type = sqlite3_value_type(argv[0]);
8831 p1 = sqlite3_value_int(argv[1]);
8832 if( p0type==SQLITE_BLOB ){
8833 len = sqlite3_value_bytes(argv[0]);
8834 z = sqlite3_value_blob(argv[0]);
8835 if( z==0 ) return;
8836 assert( len==sqlite3_value_bytes(argv[0]) );
8837 }else{
8838 z = sqlite3_value_text(argv[0]);
8839 if( z==0 ) return;
8840 len = 0;
8841 if( p1<0 ){
8842 for(z2=z; *z2; len++){
8843 SQLITE_SKIP_UTF8(z2);
8844 }
8845 }
8846 }
8847 #ifdef SQLITE_SUBSTR_COMPATIBILITY
8848 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
8849 ** as substr(X,1,N) - it returns the first N characters of X. This
8850 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
8851 ** from 2009-02-02 for compatibility of applications that exploited the
8852 ** old buggy behavior. */
8853 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
8854 #endif
8855 if( argc==3 ){
8856 p2 = sqlite3_value_int(argv[2]);
8857 if( p2<0 ){
8858 p2 = -p2;
8859 negP2 = 1;
8860 }
8861 }else{
8862 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
8863 }
8864 if( p1<0 ){
8865 p1 += len;
8866 if( p1<0 ){
8867 p2 += p1;
8868 if( p2<0 ) p2 = 0;
8869 p1 = 0;
8870 }
8871 }else if( p1>0 ){
8872 p1--;
8873 }else if( p2>0 ){
8874 p2--;
8875 }
8876 if( negP2 ){
8877 p1 -= p2;
8878 if( p1<0 ){
8879 p2 += p1;
8880 p1 = 0;
8881 }
8882 }
8883 assert( p1>=0 && p2>=0 );
8884 if( p0type!=SQLITE_BLOB ){
8885 while( *z && p1 ){
8886 SQLITE_SKIP_UTF8(z);
8887 p1--;
8888 }
8889 for(z2=z; *z2 && p2; p2--){
8890 SQLITE_SKIP_UTF8(z2);
8891 }
8892 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
8893 SQLITE_UTF8);
8894 }else{
8895 if( p1+p2>len ){
8896 p2 = len-p1;
8897 if( p2<0 ) p2 = 0;
8898 }
8899 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
8900 }
8901 }
8902
8903 /*
8904 ** Implementation of the round() function
8905 */
8906 #ifndef SQLITE_OMIT_FLOATING_POINT
8907 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
8908 int n = 0;
8909 double r;
8910 char *zBuf;
8911 assert( argc==1 || argc==2 );
8912 if( argc==2 ){
8913 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
8914 n = sqlite3_value_int(argv[1]);
8915 if( n>30 ) n = 30;
8916 if( n<0 ) n = 0;
8917 }
8918 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
8919 r = sqlite3_value_double(argv[0]);
8920 /* If Y==0 and X will fit in a 64-bit int,
8921 ** handle the rounding directly,
8922 ** otherwise use printf.
8923 */
8924 if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
8925 r = (double)((sqlite_int64)(r+0.5));
8926 }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
8927 r = -(double)((sqlite_int64)((-r)+0.5));
8928 }else{
8929 zBuf = sqlite3_mprintf("%.*f",n,r);
8930 if( zBuf==0 ){
8931 sqlite3_result_error_nomem(context);
8932 return;
8933 }
8934 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
8935 sqlite3_free(zBuf);
8936 }
8937 sqlite3_result_double(context, r);
8938 }
8939 #endif
8940
8941 /*
8942 ** Allocate nByte bytes of space using sqlite3Malloc(). If the
8943 ** allocation fails, call sqlite3_result_error_nomem() to notify
8944 ** the database handle that malloc() has failed and return NULL.
8945 ** If nByte is larger than the maximum string or blob length, then
8946 ** raise an SQLITE_TOOBIG exception and return NULL.
8947 */
8948 static void *contextMalloc(sqlite3_context *context, i64 nByte){
8949 char *z;
8950 sqlite3 *db = sqlite3_context_db_handle(context);
8951 assert( nByte>0 );
8952 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
8953 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
8954 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
8955 sqlite3_result_error_toobig(context);
8956 z = 0;
8957 }else{
8958 z = sqlite3Malloc(nByte);
8959 if( !z ){
8960 sqlite3_result_error_nomem(context);
8961 }
8962 }
8963 return z;
8964 }
8965
8966 /*
8967 ** Implementation of the upper() and lower() SQL functions.
8968 */
8969 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
8970 char *z1;
8971 const char *z2;
8972 int i, n;
8973 UNUSED_PARAMETER(argc);
8974 z2 = (char*)sqlite3_value_text(argv[0]);
8975 n = sqlite3_value_bytes(argv[0]);
8976 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
8977 assert( z2==(char*)sqlite3_value_text(argv[0]) );
8978 if( z2 ){
8979 z1 = contextMalloc(context, ((i64)n)+1);
8980 if( z1 ){
8981 for(i=0; i<n; i++){
8982 z1[i] = (char)sqlite3Toupper(z2[i]);
8983 }
8984 sqlite3_result_text(context, z1, n, sqlite3_free);
8985 }
8986 }
8987 }
8988 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
8989 char *z1;
8990 const char *z2;
8991 int i, n;
8992 UNUSED_PARAMETER(argc);
8993 z2 = (char*)sqlite3_value_text(argv[0]);
8994 n = sqlite3_value_bytes(argv[0]);
8995 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
8996 assert( z2==(char*)sqlite3_value_text(argv[0]) );
8997 if( z2 ){
8998 z1 = contextMalloc(context, ((i64)n)+1);
8999 if( z1 ){
9000 for(i=0; i<n; i++){
9001 z1[i] = sqlite3Tolower(z2[i]);
9002 }
9003 sqlite3_result_text(context, z1, n, sqlite3_free);
9004 }
9005 }
9006 }
9007
9008 /*
9009 ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
9010 ** as VDBE code so that unused argument values do not have to be computed.
9011 ** However, we still need some kind of function implementation for this
9012 ** routines in the function table. The noopFunc macro provides this.
9013 ** noopFunc will never be called so it doesn't matter what the implementation
9014 ** is. We might as well use the "version()" function as a substitute.
9015 */
9016 #define noopFunc versionFunc /* Substitute function - never called */
9017
9018 /*
9019 ** Implementation of random(). Return a random integer.
9020 */
9021 static void randomFunc(
9022 sqlite3_context *context,
9023 int NotUsed,
9024 sqlite3_value **NotUsed2
9025 ){
9026 sqlite_int64 r;
9027 UNUSED_PARAMETER2(NotUsed, NotUsed2);
9028 sqlite3_randomness(sizeof(r), &r);
9029 if( r<0 ){
9030 /* We need to prevent a random number of 0x8000000000000000
9031 ** (or -9223372036854775808) since when you do abs() of that
9032 ** number of you get the same value back again. To do this
9033 ** in a way that is testable, mask the sign bit off of negative
9034 ** values, resulting in a positive value. Then take the
9035 ** 2s complement of that positive value. The end result can
9036 ** therefore be no less than -9223372036854775807.
9037 */
9038 r = -(r & LARGEST_INT64);
9039 }
9040 sqlite3_result_int64(context, r);
9041 }
9042
9043 /*
9044 ** Implementation of randomblob(N). Return a random blob
9045 ** that is N bytes long.
9046 */
9047 static void randomBlob(
9048 sqlite3_context *context,
9049 int argc,
9050 sqlite3_value **argv
9051 ){
9052 int n;
9053 unsigned char *p;
9054 assert( argc==1 );
9055 UNUSED_PARAMETER(argc);
9056 n = sqlite3_value_int(argv[0]);
9057 if( n<1 ){
9058 n = 1;
9059 }
9060 p = contextMalloc(context, n);
9061 if( p ){
9062 sqlite3_randomness(n, p);
9063 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
9064 }
9065 }
9066
9067 /*
9068 ** Implementation of the last_insert_rowid() SQL function. The return
9069 ** value is the same as the sqlite3_last_insert_rowid() API function.
9070 */
9071 static void last_insert_rowid(
9072 sqlite3_context *context,
9073 int NotUsed,
9074 sqlite3_value **NotUsed2
9075 ){
9076 sqlite3 *db = sqlite3_context_db_handle(context);
9077 UNUSED_PARAMETER2(NotUsed, NotUsed2);
9078 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
9079 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
9080 ** function. */
9081 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
9082 }
9083
9084 /*
9085 ** Implementation of the changes() SQL function.
9086 **
9087 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
9088 ** around the sqlite3_changes() C/C++ function and hence follows the same
9089 ** rules for counting changes.
9090 */
9091 static void changes(
9092 sqlite3_context *context,
9093 int NotUsed,
9094 sqlite3_value **NotUsed2
9095 ){
9096 sqlite3 *db = sqlite3_context_db_handle(context);
9097 UNUSED_PARAMETER2(NotUsed, NotUsed2);
9098 sqlite3_result_int(context, sqlite3_changes(db));
9099 }
9100
9101 /*
9102 ** Implementation of the total_changes() SQL function. The return value is
9103 ** the same as the sqlite3_total_changes() API function.
9104 */
9105 static void total_changes(
9106 sqlite3_context *context,
9107 int NotUsed,
9108 sqlite3_value **NotUsed2
9109 ){
9110 sqlite3 *db = sqlite3_context_db_handle(context);
9111 UNUSED_PARAMETER2(NotUsed, NotUsed2);
9112 /* IMP: R-52756-41993 This function is a wrapper around the
9113 ** sqlite3_total_changes() C/C++ interface. */
9114 sqlite3_result_int(context, sqlite3_total_changes(db));
9115 }
9116
9117 /*
9118 ** A structure defining how to do GLOB-style comparisons.
9119 */
9120 struct compareInfo {
9121 u8 matchAll;
9122 u8 matchOne;
9123 u8 matchSet;
9124 u8 noCase;
9125 };
9126
9127 /*
9128 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
9129 ** character is exactly one byte in size. Also, provde the Utf8Read()
9130 ** macro for fast reading of the next character in the common case where
9131 ** the next character is ASCII.
9132 */
9133 #if defined(SQLITE_EBCDIC)
9134 # define sqlite3Utf8Read(A) (*((*A)++))
9135 # define Utf8Read(A) (*(A++))
9136 #else
9137 # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
9138 #endif
9139
9140 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
9141 /* The correct SQL-92 behavior is for the LIKE operator to ignore
9142 ** case. Thus 'a' LIKE 'A' would be true. */
9143 static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
9144 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
9145 ** is case sensitive causing 'a' LIKE 'A' to be false */
9146 static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
9147
9148 /*
9149 ** Compare two UTF-8 strings for equality where the first string can
9150 ** potentially be a "glob" or "like" expression. Return true (1) if they
9151 ** are the same and false (0) if they are different.
9152 **
9153 ** Globbing rules:
9154 **
9155 ** '*' Matches any sequence of zero or more characters.
9156 **
9157 ** '?' Matches exactly one character.
9158 **
9159 ** [...] Matches one character from the enclosed list of
9160 ** characters.
9161 **
9162 ** [^...] Matches one character not in the enclosed list.
9163 **
9164 ** With the [...] and [^...] matching, a ']' character can be included
9165 ** in the list by making it the first character after '[' or '^'. A
9166 ** range of characters can be specified using '-'. Example:
9167 ** "[a-z]" matches any single lower-case letter. To match a '-', make
9168 ** it the last character in the list.
9169 **
9170 ** Like matching rules:
9171 **
9172 ** '%' Matches any sequence of zero or more characters
9173 **
9174 *** '_' Matches any one character
9175 **
9176 ** Ec Where E is the "esc" character and c is any other
9177 ** character, including '%', '_', and esc, match exactly c.
9178 **
9179 ** The comments within this routine usually assume glob matching.
9180 **
9181 ** This routine is usually quick, but can be N**2 in the worst case.
9182 */
9183 static int patternCompare(
9184 const u8 *zPattern, /* The glob pattern */
9185 const u8 *zString, /* The string to compare against the glob */
9186 const struct compareInfo *pInfo, /* Information about how to do the compare */
9187 u32 esc /* The escape character */
9188 ){
9189 u32 c, c2; /* Next pattern and input string chars */
9190 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
9191 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
9192 u32 matchOther; /* "[" or the escape character */
9193 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
9194 const u8 *zEscaped = 0; /* One past the last escaped input char */
9195
9196 /* The GLOB operator does not have an ESCAPE clause. And LIKE does not
9197 ** have the matchSet operator. So we either have to look for one or
9198 ** the other, never both. Hence the single variable matchOther is used
9199 ** to store the one we have to look for.
9200 */
9201 matchOther = esc ? esc : pInfo->matchSet;
9202
9203 while( (c = Utf8Read(zPattern))!=0 ){
9204 if( c==matchAll ){ /* Match "*" */
9205 /* Skip over multiple "*" characters in the pattern. If there
9206 ** are also "?" characters, skip those as well, but consume a
9207 ** single character of the input string for each "?" skipped */
9208 while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
9209 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
9210 return 0;
9211 }
9212 }
9213 if( c==0 ){
9214 return 1; /* "*" at the end of the pattern matches */
9215 }else if( c==matchOther ){
9216 if( esc ){
9217 c = sqlite3Utf8Read(&zPattern);
9218 if( c==0 ) return 0;
9219 }else{
9220 /* "[...]" immediately follows the "*". We have to do a slow
9221 ** recursive search in this case, but it is an unusual case. */
9222 assert( matchOther<0x80 ); /* '[' is a single-byte character */
9223 while( *zString
9224 && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
9225 SQLITE_SKIP_UTF8(zString);
9226 }
9227 return *zString!=0;
9228 }
9229 }
9230
9231 /* At this point variable c contains the first character of the
9232 ** pattern string past the "*". Search in the input string for the
9233 ** first matching character and recursively contine the match from
9234 ** that point.
9235 **
9236 ** For a case-insensitive search, set variable cx to be the same as
9237 ** c but in the other case and search the input string for either
9238 ** c or cx.
9239 */
9240 if( c<=0x80 ){
9241 u32 cx;
9242 if( noCase ){
9243 cx = sqlite3Toupper(c);
9244 c = sqlite3Tolower(c);
9245 }else{
9246 cx = c;
9247 }
9248 while( (c2 = *(zString++))!=0 ){
9249 if( c2!=c && c2!=cx ) continue;
9250 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
9251 }
9252 }else{
9253 while( (c2 = Utf8Read(zString))!=0 ){
9254 if( c2!=c ) continue;
9255 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
9256 }
9257 }
9258 return 0;
9259 }
9260 if( c==matchOther ){
9261 if( esc ){
9262 c = sqlite3Utf8Read(&zPattern);
9263 if( c==0 ) return 0;
9264 zEscaped = zPattern;
9265 }else{
9266 u32 prior_c = 0;
9267 int seen = 0;
9268 int invert = 0;
9269 c = sqlite3Utf8Read(&zString);
9270 if( c==0 ) return 0;
9271 c2 = sqlite3Utf8Read(&zPattern);
9272 if( c2=='^' ){
9273 invert = 1;
9274 c2 = sqlite3Utf8Read(&zPattern);
9275 }
9276 if( c2==']' ){
9277 if( c==']' ) seen = 1;
9278 c2 = sqlite3Utf8Read(&zPattern);
9279 }
9280 while( c2 && c2!=']' ){
9281 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
9282 c2 = sqlite3Utf8Read(&zPattern);
9283 if( c>=prior_c && c<=c2 ) seen = 1;
9284 prior_c = 0;
9285 }else{
9286 if( c==c2 ){
9287 seen = 1;
9288 }
9289 prior_c = c2;
9290 }
9291 c2 = sqlite3Utf8Read(&zPattern);
9292 }
9293 if( c2==0 || (seen ^ invert)==0 ){
9294 return 0;
9295 }
9296 continue;
9297 }
9298 }
9299 c2 = Utf8Read(zString);
9300 if( c==c2 ) continue;
9301 if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
9302 continue;
9303 }
9304 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
9305 return 0;
9306 }
9307 return *zString==0;
9308 }
9309
9310 /*
9311 ** The sqlite3_strglob() interface.
9312 */
9313 SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlobPattern, const ch ar *zString){
9314 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
9315 }
9316
9317 /*
9318 ** The sqlite3_strlike() interface.
9319 */
9320 SQLITE_API int SQLITE_STDCALL sqlite3_strlike(const char *zPattern, const char * zStr, unsigned int esc){
9321 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc)==0;
9322 }
9323
9324 /*
9325 ** Count the number of times that the LIKE operator (or GLOB which is
9326 ** just a variation of LIKE) gets called. This is used for testing
9327 ** only.
9328 */
9329 #ifdef SQLITE_TEST
9330 SQLITE_API int sqlite3_like_count = 0;
9331 #endif
9332
9333
9334 /*
9335 ** Implementation of the like() SQL function. This function implements
9336 ** the build-in LIKE operator. The first argument to the function is the
9337 ** pattern and the second argument is the string. So, the SQL statements:
9338 **
9339 ** A LIKE B
9340 **
9341 ** is implemented as like(B,A).
9342 **
9343 ** This same function (with a different compareInfo structure) computes
9344 ** the GLOB operator.
9345 */
9346 static void likeFunc(
9347 sqlite3_context *context,
9348 int argc,
9349 sqlite3_value **argv
9350 ){
9351 const unsigned char *zA, *zB;
9352 u32 escape = 0;
9353 int nPat;
9354 sqlite3 *db = sqlite3_context_db_handle(context);
9355
9356 #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
9357 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
9358 || sqlite3_value_type(argv[1])==SQLITE_BLOB
9359 ){
9360 #ifdef SQLITE_TEST
9361 sqlite3_like_count++;
9362 #endif
9363 sqlite3_result_int(context, 0);
9364 return;
9365 }
9366 #endif
9367 zB = sqlite3_value_text(argv[0]);
9368 zA = sqlite3_value_text(argv[1]);
9369
9370 /* Limit the length of the LIKE or GLOB pattern to avoid problems
9371 ** of deep recursion and N*N behavior in patternCompare().
9372 */
9373 nPat = sqlite3_value_bytes(argv[0]);
9374 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
9375 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
9376 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
9377 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
9378 return;
9379 }
9380 assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */
9381
9382 if( argc==3 ){
9383 /* The escape character string must consist of a single UTF-8 character.
9384 ** Otherwise, return an error.
9385 */
9386 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
9387 if( zEsc==0 ) return;
9388 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
9389 sqlite3_result_error(context,
9390 "ESCAPE expression must be a single character", -1);
9391 return;
9392 }
9393 escape = sqlite3Utf8Read(&zEsc);
9394 }
9395 if( zA && zB ){
9396 struct compareInfo *pInfo = sqlite3_user_data(context);
9397 #ifdef SQLITE_TEST
9398 sqlite3_like_count++;
9399 #endif
9400
9401 sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
9402 }
9403 }
9404
9405 /*
9406 ** Implementation of the NULLIF(x,y) function. The result is the first
9407 ** argument if the arguments are different. The result is NULL if the
9408 ** arguments are equal to each other.
9409 */
9410 static void nullifFunc(
9411 sqlite3_context *context,
9412 int NotUsed,
9413 sqlite3_value **argv
9414 ){
9415 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
9416 UNUSED_PARAMETER(NotUsed);
9417 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
9418 sqlite3_result_value(context, argv[0]);
9419 }
9420 }
9421
9422 /*
9423 ** Implementation of the sqlite_version() function. The result is the version
9424 ** of the SQLite library that is running.
9425 */
9426 static void versionFunc(
9427 sqlite3_context *context,
9428 int NotUsed,
9429 sqlite3_value **NotUsed2
9430 ){
9431 UNUSED_PARAMETER2(NotUsed, NotUsed2);
9432 /* IMP: R-48699-48617 This function is an SQL wrapper around the
9433 ** sqlite3_libversion() C-interface. */
9434 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
9435 }
9436
9437 /*
9438 ** Implementation of the sqlite_source_id() function. The result is a string
9439 ** that identifies the particular version of the source code used to build
9440 ** SQLite.
9441 */
9442 static void sourceidFunc(
9443 sqlite3_context *context,
9444 int NotUsed,
9445 sqlite3_value **NotUsed2
9446 ){
9447 UNUSED_PARAMETER2(NotUsed, NotUsed2);
9448 /* IMP: R-24470-31136 This function is an SQL wrapper around the
9449 ** sqlite3_sourceid() C interface. */
9450 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
9451 }
9452
9453 /*
9454 ** Implementation of the sqlite_log() function. This is a wrapper around
9455 ** sqlite3_log(). The return value is NULL. The function exists purely for
9456 ** its side-effects.
9457 */
9458 static void errlogFunc(
9459 sqlite3_context *context,
9460 int argc,
9461 sqlite3_value **argv
9462 ){
9463 UNUSED_PARAMETER(argc);
9464 UNUSED_PARAMETER(context);
9465 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
9466 }
9467
9468 /*
9469 ** Implementation of the sqlite_compileoption_used() function.
9470 ** The result is an integer that identifies if the compiler option
9471 ** was used to build SQLite.
9472 */
9473 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
9474 static void compileoptionusedFunc(
9475 sqlite3_context *context,
9476 int argc,
9477 sqlite3_value **argv
9478 ){
9479 const char *zOptName;
9480 assert( argc==1 );
9481 UNUSED_PARAMETER(argc);
9482 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
9483 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
9484 ** function.
9485 */
9486 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
9487 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
9488 }
9489 }
9490 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
9491
9492 /*
9493 ** Implementation of the sqlite_compileoption_get() function.
9494 ** The result is a string that identifies the compiler options
9495 ** used to build SQLite.
9496 */
9497 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
9498 static void compileoptiongetFunc(
9499 sqlite3_context *context,
9500 int argc,
9501 sqlite3_value **argv
9502 ){
9503 int n;
9504 assert( argc==1 );
9505 UNUSED_PARAMETER(argc);
9506 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
9507 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
9508 */
9509 n = sqlite3_value_int(argv[0]);
9510 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
9511 }
9512 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
9513
9514 /* Array for converting from half-bytes (nybbles) into ASCII hex
9515 ** digits. */
9516 static const char hexdigits[] = {
9517 '0', '1', '2', '3', '4', '5', '6', '7',
9518 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
9519 };
9520
9521 /*
9522 ** Implementation of the QUOTE() function. This function takes a single
9523 ** argument. If the argument is numeric, the return value is the same as
9524 ** the argument. If the argument is NULL, the return value is the string
9525 ** "NULL". Otherwise, the argument is enclosed in single quotes with
9526 ** single-quote escapes.
9527 */
9528 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
9529 assert( argc==1 );
9530 UNUSED_PARAMETER(argc);
9531 switch( sqlite3_value_type(argv[0]) ){
9532 case SQLITE_FLOAT: {
9533 double r1, r2;
9534 char zBuf[50];
9535 r1 = sqlite3_value_double(argv[0]);
9536 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
9537 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
9538 if( r1!=r2 ){
9539 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
9540 }
9541 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
9542 break;
9543 }
9544 case SQLITE_INTEGER: {
9545 sqlite3_result_value(context, argv[0]);
9546 break;
9547 }
9548 case SQLITE_BLOB: {
9549 char *zText = 0;
9550 char const *zBlob = sqlite3_value_blob(argv[0]);
9551 int nBlob = sqlite3_value_bytes(argv[0]);
9552 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
9553 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
9554 if( zText ){
9555 int i;
9556 for(i=0; i<nBlob; i++){
9557 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
9558 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
9559 }
9560 zText[(nBlob*2)+2] = '\'';
9561 zText[(nBlob*2)+3] = '\0';
9562 zText[0] = 'X';
9563 zText[1] = '\'';
9564 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
9565 sqlite3_free(zText);
9566 }
9567 break;
9568 }
9569 case SQLITE_TEXT: {
9570 int i,j;
9571 u64 n;
9572 const unsigned char *zArg = sqlite3_value_text(argv[0]);
9573 char *z;
9574
9575 if( zArg==0 ) return;
9576 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
9577 z = contextMalloc(context, ((i64)i)+((i64)n)+3);
9578 if( z ){
9579 z[0] = '\'';
9580 for(i=0, j=1; zArg[i]; i++){
9581 z[j++] = zArg[i];
9582 if( zArg[i]=='\'' ){
9583 z[j++] = '\'';
9584 }
9585 }
9586 z[j++] = '\'';
9587 z[j] = 0;
9588 sqlite3_result_text(context, z, j, sqlite3_free);
9589 }
9590 break;
9591 }
9592 default: {
9593 assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
9594 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
9595 break;
9596 }
9597 }
9598 }
9599
9600 /*
9601 ** The unicode() function. Return the integer unicode code-point value
9602 ** for the first character of the input string.
9603 */
9604 static void unicodeFunc(
9605 sqlite3_context *context,
9606 int argc,
9607 sqlite3_value **argv
9608 ){
9609 const unsigned char *z = sqlite3_value_text(argv[0]);
9610 (void)argc;
9611 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
9612 }
9613
9614 /*
9615 ** The char() function takes zero or more arguments, each of which is
9616 ** an integer. It constructs a string where each character of the string
9617 ** is the unicode character for the corresponding integer argument.
9618 */
9619 static void charFunc(
9620 sqlite3_context *context,
9621 int argc,
9622 sqlite3_value **argv
9623 ){
9624 unsigned char *z, *zOut;
9625 int i;
9626 zOut = z = sqlite3_malloc64( argc*4+1 );
9627 if( z==0 ){
9628 sqlite3_result_error_nomem(context);
9629 return;
9630 }
9631 for(i=0; i<argc; i++){
9632 sqlite3_int64 x;
9633 unsigned c;
9634 x = sqlite3_value_int64(argv[i]);
9635 if( x<0 || x>0x10ffff ) x = 0xfffd;
9636 c = (unsigned)(x & 0x1fffff);
9637 if( c<0x00080 ){
9638 *zOut++ = (u8)(c&0xFF);
9639 }else if( c<0x00800 ){
9640 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
9641 *zOut++ = 0x80 + (u8)(c & 0x3F);
9642 }else if( c<0x10000 ){
9643 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
9644 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
9645 *zOut++ = 0x80 + (u8)(c & 0x3F);
9646 }else{
9647 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
9648 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
9649 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
9650 *zOut++ = 0x80 + (u8)(c & 0x3F);
9651 } \
9652 }
9653 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
9654 }
9655
9656 /*
9657 ** The hex() function. Interpret the argument as a blob. Return
9658 ** a hexadecimal rendering as text.
9659 */
9660 static void hexFunc(
9661 sqlite3_context *context,
9662 int argc,
9663 sqlite3_value **argv
9664 ){
9665 int i, n;
9666 const unsigned char *pBlob;
9667 char *zHex, *z;
9668 assert( argc==1 );
9669 UNUSED_PARAMETER(argc);
9670 pBlob = sqlite3_value_blob(argv[0]);
9671 n = sqlite3_value_bytes(argv[0]);
9672 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
9673 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
9674 if( zHex ){
9675 for(i=0; i<n; i++, pBlob++){
9676 unsigned char c = *pBlob;
9677 *(z++) = hexdigits[(c>>4)&0xf];
9678 *(z++) = hexdigits[c&0xf];
9679 }
9680 *z = 0;
9681 sqlite3_result_text(context, zHex, n*2, sqlite3_free);
9682 }
9683 }
9684
9685 /*
9686 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
9687 */
9688 static void zeroblobFunc(
9689 sqlite3_context *context,
9690 int argc,
9691 sqlite3_value **argv
9692 ){
9693 i64 n;
9694 int rc;
9695 assert( argc==1 );
9696 UNUSED_PARAMETER(argc);
9697 n = sqlite3_value_int64(argv[0]);
9698 if( n<0 ) n = 0;
9699 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
9700 if( rc ){
9701 sqlite3_result_error_code(context, rc);
9702 }
9703 }
9704
9705 /*
9706 ** The replace() function. Three arguments are all strings: call
9707 ** them A, B, and C. The result is also a string which is derived
9708 ** from A by replacing every occurrence of B with C. The match
9709 ** must be exact. Collating sequences are not used.
9710 */
9711 static void replaceFunc(
9712 sqlite3_context *context,
9713 int argc,
9714 sqlite3_value **argv
9715 ){
9716 const unsigned char *zStr; /* The input string A */
9717 const unsigned char *zPattern; /* The pattern string B */
9718 const unsigned char *zRep; /* The replacement string C */
9719 unsigned char *zOut; /* The output */
9720 int nStr; /* Size of zStr */
9721 int nPattern; /* Size of zPattern */
9722 int nRep; /* Size of zRep */
9723 i64 nOut; /* Maximum size of zOut */
9724 int loopLimit; /* Last zStr[] that might match zPattern[] */
9725 int i, j; /* Loop counters */
9726
9727 assert( argc==3 );
9728 UNUSED_PARAMETER(argc);
9729 zStr = sqlite3_value_text(argv[0]);
9730 if( zStr==0 ) return;
9731 nStr = sqlite3_value_bytes(argv[0]);
9732 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
9733 zPattern = sqlite3_value_text(argv[1]);
9734 if( zPattern==0 ){
9735 assert( sqlite3_value_type(argv[1])==SQLITE_NULL
9736 || sqlite3_context_db_handle(context)->mallocFailed );
9737 return;
9738 }
9739 if( zPattern[0]==0 ){
9740 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
9741 sqlite3_result_value(context, argv[0]);
9742 return;
9743 }
9744 nPattern = sqlite3_value_bytes(argv[1]);
9745 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
9746 zRep = sqlite3_value_text(argv[2]);
9747 if( zRep==0 ) return;
9748 nRep = sqlite3_value_bytes(argv[2]);
9749 assert( zRep==sqlite3_value_text(argv[2]) );
9750 nOut = nStr + 1;
9751 assert( nOut<SQLITE_MAX_LENGTH );
9752 zOut = contextMalloc(context, (i64)nOut);
9753 if( zOut==0 ){
9754 return;
9755 }
9756 loopLimit = nStr - nPattern;
9757 for(i=j=0; i<=loopLimit; i++){
9758 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
9759 zOut[j++] = zStr[i];
9760 }else{
9761 u8 *zOld;
9762 sqlite3 *db = sqlite3_context_db_handle(context);
9763 nOut += nRep - nPattern;
9764 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
9765 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
9766 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
9767 sqlite3_result_error_toobig(context);
9768 sqlite3_free(zOut);
9769 return;
9770 }
9771 zOld = zOut;
9772 zOut = sqlite3_realloc64(zOut, (int)nOut);
9773 if( zOut==0 ){
9774 sqlite3_result_error_nomem(context);
9775 sqlite3_free(zOld);
9776 return;
9777 }
9778 memcpy(&zOut[j], zRep, nRep);
9779 j += nRep;
9780 i += nPattern-1;
9781 }
9782 }
9783 assert( j+nStr-i+1==nOut );
9784 memcpy(&zOut[j], &zStr[i], nStr-i);
9785 j += nStr - i;
9786 assert( j<=nOut );
9787 zOut[j] = 0;
9788 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
9789 }
9790
9791 /*
9792 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
9793 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
9794 */
9795 static void trimFunc(
9796 sqlite3_context *context,
9797 int argc,
9798 sqlite3_value **argv
9799 ){
9800 const unsigned char *zIn; /* Input string */
9801 const unsigned char *zCharSet; /* Set of characters to trim */
9802 int nIn; /* Number of bytes in input */
9803 int flags; /* 1: trimleft 2: trimright 3: trim */
9804 int i; /* Loop counter */
9805 unsigned char *aLen = 0; /* Length of each character in zCharSet */
9806 unsigned char **azChar = 0; /* Individual characters in zCharSet */
9807 int nChar; /* Number of characters in zCharSet */
9808
9809 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
9810 return;
9811 }
9812 zIn = sqlite3_value_text(argv[0]);
9813 if( zIn==0 ) return;
9814 nIn = sqlite3_value_bytes(argv[0]);
9815 assert( zIn==sqlite3_value_text(argv[0]) );
9816 if( argc==1 ){
9817 static const unsigned char lenOne[] = { 1 };
9818 static unsigned char * const azOne[] = { (u8*)" " };
9819 nChar = 1;
9820 aLen = (u8*)lenOne;
9821 azChar = (unsigned char **)azOne;
9822 zCharSet = 0;
9823 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
9824 return;
9825 }else{
9826 const unsigned char *z;
9827 for(z=zCharSet, nChar=0; *z; nChar++){
9828 SQLITE_SKIP_UTF8(z);
9829 }
9830 if( nChar>0 ){
9831 azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
9832 if( azChar==0 ){
9833 return;
9834 }
9835 aLen = (unsigned char*)&azChar[nChar];
9836 for(z=zCharSet, nChar=0; *z; nChar++){
9837 azChar[nChar] = (unsigned char *)z;
9838 SQLITE_SKIP_UTF8(z);
9839 aLen[nChar] = (u8)(z - azChar[nChar]);
9840 }
9841 }
9842 }
9843 if( nChar>0 ){
9844 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
9845 if( flags & 1 ){
9846 while( nIn>0 ){
9847 int len = 0;
9848 for(i=0; i<nChar; i++){
9849 len = aLen[i];
9850 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
9851 }
9852 if( i>=nChar ) break;
9853 zIn += len;
9854 nIn -= len;
9855 }
9856 }
9857 if( flags & 2 ){
9858 while( nIn>0 ){
9859 int len = 0;
9860 for(i=0; i<nChar; i++){
9861 len = aLen[i];
9862 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
9863 }
9864 if( i>=nChar ) break;
9865 nIn -= len;
9866 }
9867 }
9868 if( zCharSet ){
9869 sqlite3_free(azChar);
9870 }
9871 }
9872 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
9873 }
9874
9875
9876 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
9877 ** is only available if the SQLITE_SOUNDEX compile-time option is used
9878 ** when SQLite is built.
9879 */
9880 #ifdef SQLITE_SOUNDEX
9881 /*
9882 ** Compute the soundex encoding of a word.
9883 **
9884 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
9885 ** soundex encoding of the string X.
9886 */
9887 static void soundexFunc(
9888 sqlite3_context *context,
9889 int argc,
9890 sqlite3_value **argv
9891 ){
9892 char zResult[8];
9893 const u8 *zIn;
9894 int i, j;
9895 static const unsigned char iCode[] = {
9896 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9897 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9898 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9899 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9900 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
9901 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
9902 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
9903 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
9904 };
9905 assert( argc==1 );
9906 zIn = (u8*)sqlite3_value_text(argv[0]);
9907 if( zIn==0 ) zIn = (u8*)"";
9908 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
9909 if( zIn[i] ){
9910 u8 prevcode = iCode[zIn[i]&0x7f];
9911 zResult[0] = sqlite3Toupper(zIn[i]);
9912 for(j=1; j<4 && zIn[i]; i++){
9913 int code = iCode[zIn[i]&0x7f];
9914 if( code>0 ){
9915 if( code!=prevcode ){
9916 prevcode = code;
9917 zResult[j++] = code + '0';
9918 }
9919 }else{
9920 prevcode = 0;
9921 }
9922 }
9923 while( j<4 ){
9924 zResult[j++] = '0';
9925 }
9926 zResult[j] = 0;
9927 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
9928 }else{
9929 /* IMP: R-64894-50321 The string "?000" is returned if the argument
9930 ** is NULL or contains no ASCII alphabetic characters. */
9931 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
9932 }
9933 }
9934 #endif /* SQLITE_SOUNDEX */
9935
9936 #ifndef SQLITE_OMIT_LOAD_EXTENSION
9937 /*
9938 ** A function that loads a shared-library extension then returns NULL.
9939 */
9940 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
9941 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
9942 const char *zProc;
9943 sqlite3 *db = sqlite3_context_db_handle(context);
9944 char *zErrMsg = 0;
9945
9946 if( argc==2 ){
9947 zProc = (const char *)sqlite3_value_text(argv[1]);
9948 }else{
9949 zProc = 0;
9950 }
9951 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
9952 sqlite3_result_error(context, zErrMsg, -1);
9953 sqlite3_free(zErrMsg);
9954 }
9955 }
9956 #endif
9957
9958
9959 /*
9960 ** An instance of the following structure holds the context of a
9961 ** sum() or avg() aggregate computation.
9962 */
9963 typedef struct SumCtx SumCtx;
9964 struct SumCtx {
9965 double rSum; /* Floating point sum */
9966 i64 iSum; /* Integer sum */
9967 i64 cnt; /* Number of elements summed */
9968 u8 overflow; /* True if integer overflow seen */
9969 u8 approx; /* True if non-integer value was input to the sum */
9970 };
9971
9972 /*
9973 ** Routines used to compute the sum, average, and total.
9974 **
9975 ** The SUM() function follows the (broken) SQL standard which means
9976 ** that it returns NULL if it sums over no inputs. TOTAL returns
9977 ** 0.0 in that case. In addition, TOTAL always returns a float where
9978 ** SUM might return an integer if it never encounters a floating point
9979 ** value. TOTAL never fails, but SUM might through an exception if
9980 ** it overflows an integer.
9981 */
9982 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
9983 SumCtx *p;
9984 int type;
9985 assert( argc==1 );
9986 UNUSED_PARAMETER(argc);
9987 p = sqlite3_aggregate_context(context, sizeof(*p));
9988 type = sqlite3_value_numeric_type(argv[0]);
9989 if( p && type!=SQLITE_NULL ){
9990 p->cnt++;
9991 if( type==SQLITE_INTEGER ){
9992 i64 v = sqlite3_value_int64(argv[0]);
9993 p->rSum += v;
9994 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
9995 p->overflow = 1;
9996 }
9997 }else{
9998 p->rSum += sqlite3_value_double(argv[0]);
9999 p->approx = 1;
10000 }
10001 }
10002 }
10003 static void sumFinalize(sqlite3_context *context){
10004 SumCtx *p;
10005 p = sqlite3_aggregate_context(context, 0);
10006 if( p && p->cnt>0 ){
10007 if( p->overflow ){
10008 sqlite3_result_error(context,"integer overflow",-1);
10009 }else if( p->approx ){
10010 sqlite3_result_double(context, p->rSum);
10011 }else{
10012 sqlite3_result_int64(context, p->iSum);
10013 }
10014 }
10015 }
10016 static void avgFinalize(sqlite3_context *context){
10017 SumCtx *p;
10018 p = sqlite3_aggregate_context(context, 0);
10019 if( p && p->cnt>0 ){
10020 sqlite3_result_double(context, p->rSum/(double)p->cnt);
10021 }
10022 }
10023 static void totalFinalize(sqlite3_context *context){
10024 SumCtx *p;
10025 p = sqlite3_aggregate_context(context, 0);
10026 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
10027 sqlite3_result_double(context, p ? p->rSum : (double)0);
10028 }
10029
10030 /*
10031 ** The following structure keeps track of state information for the
10032 ** count() aggregate function.
10033 */
10034 typedef struct CountCtx CountCtx;
10035 struct CountCtx {
10036 i64 n;
10037 };
10038
10039 /*
10040 ** Routines to implement the count() aggregate function.
10041 */
10042 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
10043 CountCtx *p;
10044 p = sqlite3_aggregate_context(context, sizeof(*p));
10045 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
10046 p->n++;
10047 }
10048
10049 #ifndef SQLITE_OMIT_DEPRECATED
10050 /* The sqlite3_aggregate_count() function is deprecated. But just to make
10051 ** sure it still operates correctly, verify that its count agrees with our
10052 ** internal count when using count(*) and when the total count can be
10053 ** expressed as a 32-bit integer. */
10054 assert( argc==1 || p==0 || p->n>0x7fffffff
10055 || p->n==sqlite3_aggregate_count(context) );
10056 #endif
10057 }
10058 static void countFinalize(sqlite3_context *context){
10059 CountCtx *p;
10060 p = sqlite3_aggregate_context(context, 0);
10061 sqlite3_result_int64(context, p ? p->n : 0);
10062 }
10063
10064 /*
10065 ** Routines to implement min() and max() aggregate functions.
10066 */
10067 static void minmaxStep(
10068 sqlite3_context *context,
10069 int NotUsed,
10070 sqlite3_value **argv
10071 ){
10072 Mem *pArg = (Mem *)argv[0];
10073 Mem *pBest;
10074 UNUSED_PARAMETER(NotUsed);
10075
10076 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
10077 if( !pBest ) return;
10078
10079 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
10080 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
10081 }else if( pBest->flags ){
10082 int max;
10083 int cmp;
10084 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
10085 /* This step function is used for both the min() and max() aggregates,
10086 ** the only difference between the two being that the sense of the
10087 ** comparison is inverted. For the max() aggregate, the
10088 ** sqlite3_user_data() function returns (void *)-1. For min() it
10089 ** returns (void *)db, where db is the sqlite3* database pointer.
10090 ** Therefore the next statement sets variable 'max' to 1 for the max()
10091 ** aggregate, or 0 for min().
10092 */
10093 max = sqlite3_user_data(context)!=0;
10094 cmp = sqlite3MemCompare(pBest, pArg, pColl);
10095 if( (max && cmp<0) || (!max && cmp>0) ){
10096 sqlite3VdbeMemCopy(pBest, pArg);
10097 }else{
10098 sqlite3SkipAccumulatorLoad(context);
10099 }
10100 }else{
10101 pBest->db = sqlite3_context_db_handle(context);
10102 sqlite3VdbeMemCopy(pBest, pArg);
10103 }
10104 }
10105 static void minMaxFinalize(sqlite3_context *context){
10106 sqlite3_value *pRes;
10107 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
10108 if( pRes ){
10109 if( pRes->flags ){
10110 sqlite3_result_value(context, pRes);
10111 }
10112 sqlite3VdbeMemRelease(pRes);
10113 }
10114 }
10115
10116 /*
10117 ** group_concat(EXPR, ?SEPARATOR?)
10118 */
10119 static void groupConcatStep(
10120 sqlite3_context *context,
10121 int argc,
10122 sqlite3_value **argv
10123 ){
10124 const char *zVal;
10125 StrAccum *pAccum;
10126 const char *zSep;
10127 int nVal, nSep;
10128 assert( argc==1 || argc==2 );
10129 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
10130 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
10131
10132 if( pAccum ){
10133 sqlite3 *db = sqlite3_context_db_handle(context);
10134 int firstTerm = pAccum->mxAlloc==0;
10135 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
10136 if( !firstTerm ){
10137 if( argc==2 ){
10138 zSep = (char*)sqlite3_value_text(argv[1]);
10139 nSep = sqlite3_value_bytes(argv[1]);
10140 }else{
10141 zSep = ",";
10142 nSep = 1;
10143 }
10144 if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
10145 }
10146 zVal = (char*)sqlite3_value_text(argv[0]);
10147 nVal = sqlite3_value_bytes(argv[0]);
10148 if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
10149 }
10150 }
10151 static void groupConcatFinalize(sqlite3_context *context){
10152 StrAccum *pAccum;
10153 pAccum = sqlite3_aggregate_context(context, 0);
10154 if( pAccum ){
10155 if( pAccum->accError==STRACCUM_TOOBIG ){
10156 sqlite3_result_error_toobig(context);
10157 }else if( pAccum->accError==STRACCUM_NOMEM ){
10158 sqlite3_result_error_nomem(context);
10159 }else{
10160 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
10161 sqlite3_free);
10162 }
10163 }
10164 }
10165
10166 /*
10167 ** This routine does per-connection function registration. Most
10168 ** of the built-in functions above are part of the global function set.
10169 ** This routine only deals with those that are not global.
10170 */
10171 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
10172 int rc = sqlite3_overload_function(db, "MATCH", 2);
10173 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
10174 if( rc==SQLITE_NOMEM ){
10175 db->mallocFailed = 1;
10176 }
10177 }
10178
10179 /*
10180 ** Set the LIKEOPT flag on the 2-argument function with the given name.
10181 */
10182 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
10183 FuncDef *pDef;
10184 pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
10185 2, SQLITE_UTF8, 0);
10186 if( ALWAYS(pDef) ){
10187 pDef->funcFlags |= flagVal;
10188 }
10189 }
10190
10191 /*
10192 ** Register the built-in LIKE and GLOB functions. The caseSensitive
10193 ** parameter determines whether or not the LIKE operator is case
10194 ** sensitive. GLOB is always case sensitive.
10195 */
10196 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive) {
10197 struct compareInfo *pInfo;
10198 if( caseSensitive ){
10199 pInfo = (struct compareInfo*)&likeInfoAlt;
10200 }else{
10201 pInfo = (struct compareInfo*)&likeInfoNorm;
10202 }
10203 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
10204 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
10205 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
10206 (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
10207 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
10208 setLikeOptFlag(db, "like",
10209 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
10210 }
10211
10212 /*
10213 ** pExpr points to an expression which implements a function. If
10214 ** it is appropriate to apply the LIKE optimization to that function
10215 ** then set aWc[0] through aWc[2] to the wildcard characters and
10216 ** return TRUE. If the function is not a LIKE-style function then
10217 ** return FALSE.
10218 **
10219 ** *pIsNocase is set to true if uppercase and lowercase are equivalent for
10220 ** the function (default for LIKE). If the function makes the distinction
10221 ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
10222 ** false.
10223 */
10224 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocas e, char *aWc){
10225 FuncDef *pDef;
10226 if( pExpr->op!=TK_FUNCTION
10227 || !pExpr->x.pList
10228 || pExpr->x.pList->nExpr!=2
10229 ){
10230 return 0;
10231 }
10232 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
10233 pDef = sqlite3FindFunction(db, pExpr->u.zToken,
10234 sqlite3Strlen30(pExpr->u.zToken),
10235 2, SQLITE_UTF8, 0);
10236 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
10237 return 0;
10238 }
10239
10240 /* The memcpy() statement assumes that the wildcard characters are
10241 ** the first three statements in the compareInfo structure. The
10242 ** asserts() that follow verify that assumption
10243 */
10244 memcpy(aWc, pDef->pUserData, 3);
10245 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
10246 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
10247 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
10248 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
10249 return 1;
10250 }
10251
10252 /*
10253 ** All of the FuncDef structures in the aBuiltinFunc[] array above
10254 ** to the global function hash table. This occurs at start-time (as
10255 ** a consequence of calling sqlite3_initialize()).
10256 **
10257 ** After this routine runs
10258 */
10259 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
10260 /*
10261 ** The following array holds FuncDef structures for all of the functions
10262 ** defined in this file.
10263 **
10264 ** The array cannot be constant since changes are made to the
10265 ** FuncDef.pHash elements at start-time. The elements of this array
10266 ** are read-only after initialization is complete.
10267 */
10268 static SQLITE_WSD FuncDef aBuiltinFunc[] = {
10269 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
10270 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
10271 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
10272 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
10273 FUNCTION(trim, 1, 3, 0, trimFunc ),
10274 FUNCTION(trim, 2, 3, 0, trimFunc ),
10275 FUNCTION(min, -1, 0, 1, minmaxFunc ),
10276 FUNCTION(min, 0, 0, 1, 0 ),
10277 AGGREGATE2(min, 1, 0, 1, minmaxStep, minMaxFinalize,
10278 SQLITE_FUNC_MINMAX ),
10279 FUNCTION(max, -1, 1, 1, minmaxFunc ),
10280 FUNCTION(max, 0, 1, 1, 0 ),
10281 AGGREGATE2(max, 1, 1, 1, minmaxStep, minMaxFinalize,
10282 SQLITE_FUNC_MINMAX ),
10283 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
10284 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
10285 FUNCTION(instr, 2, 0, 0, instrFunc ),
10286 FUNCTION(substr, 2, 0, 0, substrFunc ),
10287 FUNCTION(substr, 3, 0, 0, substrFunc ),
10288 FUNCTION(printf, -1, 0, 0, printfFunc ),
10289 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
10290 FUNCTION(char, -1, 0, 0, charFunc ),
10291 FUNCTION(abs, 1, 0, 0, absFunc ),
10292 #ifndef SQLITE_OMIT_FLOATING_POINT
10293 FUNCTION(round, 1, 0, 0, roundFunc ),
10294 FUNCTION(round, 2, 0, 0, roundFunc ),
10295 #endif
10296 FUNCTION(upper, 1, 0, 0, upperFunc ),
10297 FUNCTION(lower, 1, 0, 0, lowerFunc ),
10298 FUNCTION(coalesce, 1, 0, 0, 0 ),
10299 FUNCTION(coalesce, 0, 0, 0, 0 ),
10300 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
10301 FUNCTION(hex, 1, 0, 0, hexFunc ),
10302 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
10303 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
10304 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
10305 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
10306 VFUNCTION(random, 0, 0, 0, randomFunc ),
10307 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
10308 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
10309 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
10310 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
10311 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
10312 #if SQLITE_USER_AUTHENTICATION
10313 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ),
10314 #endif
10315 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
10316 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
10317 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
10318 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
10319 FUNCTION(quote, 1, 0, 0, quoteFunc ),
10320 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
10321 VFUNCTION(changes, 0, 0, 0, changes ),
10322 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
10323 FUNCTION(replace, 3, 0, 0, replaceFunc ),
10324 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
10325 #ifdef SQLITE_SOUNDEX
10326 FUNCTION(soundex, 1, 0, 0, soundexFunc ),
10327 #endif
10328 #ifndef SQLITE_OMIT_LOAD_EXTENSION
10329 VFUNCTION(load_extension, 1, 0, 0, loadExt ),
10330 VFUNCTION(load_extension, 2, 0, 0, loadExt ),
10331 #endif
10332 AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ),
10333 AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ),
10334 AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ),
10335 AGGREGATE2(count, 0, 0, 0, countStep, countFinalize,
10336 SQLITE_FUNC_COUNT ),
10337 AGGREGATE(count, 1, 0, 0, countStep, countFinalize ),
10338 AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize),
10339 AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize),
10340
10341 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
10342 #ifdef SQLITE_CASE_SENSITIVE_LIKE
10343 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
10344 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
10345 #else
10346 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
10347 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
10348 #endif
10349 };
10350
10351 int i;
10352 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
10353 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
10354
10355 for(i=0; i<ArraySize(aBuiltinFunc); i++){
10356 sqlite3FuncDefInsert(pHash, &aFunc[i]);
10357 }
10358 sqlite3RegisterDateTimeFunctions();
10359 #ifndef SQLITE_OMIT_ALTERTABLE
10360 sqlite3AlterFunctions();
10361 #endif
10362 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
10363 sqlite3AnalyzeFunctions();
10364 #endif
10365 }
10366
10367 /************** End of func.c ************************************************/
10368 /************** Begin file fkey.c ********************************************/
10369 /*
10370 **
10371 ** The author disclaims copyright to this source code. In place of
10372 ** a legal notice, here is a blessing:
10373 **
10374 ** May you do good and not evil.
10375 ** May you find forgiveness for yourself and forgive others.
10376 ** May you share freely, never taking more than you give.
10377 **
10378 *************************************************************************
10379 ** This file contains code used by the compiler to add foreign key
10380 ** support to compiled SQL statements.
10381 */
10382 /* #include "sqliteInt.h" */
10383
10384 #ifndef SQLITE_OMIT_FOREIGN_KEY
10385 #ifndef SQLITE_OMIT_TRIGGER
10386
10387 /*
10388 ** Deferred and Immediate FKs
10389 ** --------------------------
10390 **
10391 ** Foreign keys in SQLite come in two flavours: deferred and immediate.
10392 ** If an immediate foreign key constraint is violated,
10393 ** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
10394 ** statement transaction rolled back. If a
10395 ** deferred foreign key constraint is violated, no action is taken
10396 ** immediately. However if the application attempts to commit the
10397 ** transaction before fixing the constraint violation, the attempt fails.
10398 **
10399 ** Deferred constraints are implemented using a simple counter associated
10400 ** with the database handle. The counter is set to zero each time a
10401 ** database transaction is opened. Each time a statement is executed
10402 ** that causes a foreign key violation, the counter is incremented. Each
10403 ** time a statement is executed that removes an existing violation from
10404 ** the database, the counter is decremented. When the transaction is
10405 ** committed, the commit fails if the current value of the counter is
10406 ** greater than zero. This scheme has two big drawbacks:
10407 **
10408 ** * When a commit fails due to a deferred foreign key constraint,
10409 ** there is no way to tell which foreign constraint is not satisfied,
10410 ** or which row it is not satisfied for.
10411 **
10412 ** * If the database contains foreign key violations when the
10413 ** transaction is opened, this may cause the mechanism to malfunction.
10414 **
10415 ** Despite these problems, this approach is adopted as it seems simpler
10416 ** than the alternatives.
10417 **
10418 ** INSERT operations:
10419 **
10420 ** I.1) For each FK for which the table is the child table, search
10421 ** the parent table for a match. If none is found increment the
10422 ** constraint counter.
10423 **
10424 ** I.2) For each FK for which the table is the parent table,
10425 ** search the child table for rows that correspond to the new
10426 ** row in the parent table. Decrement the counter for each row
10427 ** found (as the constraint is now satisfied).
10428 **
10429 ** DELETE operations:
10430 **
10431 ** D.1) For each FK for which the table is the child table,
10432 ** search the parent table for a row that corresponds to the
10433 ** deleted row in the child table. If such a row is not found,
10434 ** decrement the counter.
10435 **
10436 ** D.2) For each FK for which the table is the parent table, search
10437 ** the child table for rows that correspond to the deleted row
10438 ** in the parent table. For each found increment the counter.
10439 **
10440 ** UPDATE operations:
10441 **
10442 ** An UPDATE command requires that all 4 steps above are taken, but only
10443 ** for FK constraints for which the affected columns are actually
10444 ** modified (values must be compared at runtime).
10445 **
10446 ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
10447 ** This simplifies the implementation a bit.
10448 **
10449 ** For the purposes of immediate FK constraints, the OR REPLACE conflict
10450 ** resolution is considered to delete rows before the new row is inserted.
10451 ** If a delete caused by OR REPLACE violates an FK constraint, an exception
10452 ** is thrown, even if the FK constraint would be satisfied after the new
10453 ** row is inserted.
10454 **
10455 ** Immediate constraints are usually handled similarly. The only difference
10456 ** is that the counter used is stored as part of each individual statement
10457 ** object (struct Vdbe). If, after the statement has run, its immediate
10458 ** constraint counter is greater than zero,
10459 ** it returns SQLITE_CONSTRAINT_FOREIGNKEY
10460 ** and the statement transaction is rolled back. An exception is an INSERT
10461 ** statement that inserts a single row only (no triggers). In this case,
10462 ** instead of using a counter, an exception is thrown immediately if the
10463 ** INSERT violates a foreign key constraint. This is necessary as such
10464 ** an INSERT does not open a statement transaction.
10465 **
10466 ** TODO: How should dropping a table be handled? How should renaming a
10467 ** table be handled?
10468 **
10469 **
10470 ** Query API Notes
10471 ** ---------------
10472 **
10473 ** Before coding an UPDATE or DELETE row operation, the code-generator
10474 ** for those two operations needs to know whether or not the operation
10475 ** requires any FK processing and, if so, which columns of the original
10476 ** row are required by the FK processing VDBE code (i.e. if FKs were
10477 ** implemented using triggers, which of the old.* columns would be
10478 ** accessed). No information is required by the code-generator before
10479 ** coding an INSERT operation. The functions used by the UPDATE/DELETE
10480 ** generation code to query for this information are:
10481 **
10482 ** sqlite3FkRequired() - Test to see if FK processing is required.
10483 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
10484 **
10485 **
10486 ** Externally accessible module functions
10487 ** --------------------------------------
10488 **
10489 ** sqlite3FkCheck() - Check for foreign key violations.
10490 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
10491 ** sqlite3FkDelete() - Delete an FKey structure.
10492 */
10493
10494 /*
10495 ** VDBE Calling Convention
10496 ** -----------------------
10497 **
10498 ** Example:
10499 **
10500 ** For the following INSERT statement:
10501 **
10502 ** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
10503 ** INSERT INTO t1 VALUES(1, 2, 3.1);
10504 **
10505 ** Register (x): 2 (type integer)
10506 ** Register (x+1): 1 (type integer)
10507 ** Register (x+2): NULL (type NULL)
10508 ** Register (x+3): 3.1 (type real)
10509 */
10510
10511 /*
10512 ** A foreign key constraint requires that the key columns in the parent
10513 ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
10514 ** Given that pParent is the parent table for foreign key constraint pFKey,
10515 ** search the schema for a unique index on the parent key columns.
10516 **
10517 ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
10518 ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
10519 ** is set to point to the unique index.
10520 **
10521 ** If the parent key consists of a single column (the foreign key constraint
10522 ** is not a composite foreign key), output variable *paiCol is set to NULL.
10523 ** Otherwise, it is set to point to an allocated array of size N, where
10524 ** N is the number of columns in the parent key. The first element of the
10525 ** array is the index of the child table column that is mapped by the FK
10526 ** constraint to the parent table column stored in the left-most column
10527 ** of index *ppIdx. The second element of the array is the index of the
10528 ** child table column that corresponds to the second left-most column of
10529 ** *ppIdx, and so on.
10530 **
10531 ** If the required index cannot be found, either because:
10532 **
10533 ** 1) The named parent key columns do not exist, or
10534 **
10535 ** 2) The named parent key columns do exist, but are not subject to a
10536 ** UNIQUE or PRIMARY KEY constraint, or
10537 **
10538 ** 3) No parent key columns were provided explicitly as part of the
10539 ** foreign key definition, and the parent table does not have a
10540 ** PRIMARY KEY, or
10541 **
10542 ** 4) No parent key columns were provided explicitly as part of the
10543 ** foreign key definition, and the PRIMARY KEY of the parent table
10544 ** consists of a different number of columns to the child key in
10545 ** the child table.
10546 **
10547 ** then non-zero is returned, and a "foreign key mismatch" error loaded
10548 ** into pParse. If an OOM error occurs, non-zero is returned and the
10549 ** pParse->db->mallocFailed flag is set.
10550 */
10551 SQLITE_PRIVATE int sqlite3FkLocateIndex(
10552 Parse *pParse, /* Parse context to store any error in */
10553 Table *pParent, /* Parent table of FK constraint pFKey */
10554 FKey *pFKey, /* Foreign key to find index for */
10555 Index **ppIdx, /* OUT: Unique index on parent table */
10556 int **paiCol /* OUT: Map of index columns in pFKey */
10557 ){
10558 Index *pIdx = 0; /* Value to return via *ppIdx */
10559 int *aiCol = 0; /* Value to return via *paiCol */
10560 int nCol = pFKey->nCol; /* Number of columns in parent key */
10561 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
10562
10563 /* The caller is responsible for zeroing output parameters. */
10564 assert( ppIdx && *ppIdx==0 );
10565 assert( !paiCol || *paiCol==0 );
10566 assert( pParse );
10567
10568 /* If this is a non-composite (single column) foreign key, check if it
10569 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
10570 ** and *paiCol set to zero and return early.
10571 **
10572 ** Otherwise, for a composite foreign key (more than one column), allocate
10573 ** space for the aiCol array (returned via output parameter *paiCol).
10574 ** Non-composite foreign keys do not require the aiCol array.
10575 */
10576 if( nCol==1 ){
10577 /* The FK maps to the IPK if any of the following are true:
10578 **
10579 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
10580 ** mapped to the primary key of table pParent, or
10581 ** 2) The FK is explicitly mapped to a column declared as INTEGER
10582 ** PRIMARY KEY.
10583 */
10584 if( pParent->iPKey>=0 ){
10585 if( !zKey ) return 0;
10586 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
10587 }
10588 }else if( paiCol ){
10589 assert( nCol>1 );
10590 aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
10591 if( !aiCol ) return 1;
10592 *paiCol = aiCol;
10593 }
10594
10595 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
10596 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){
10597 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
10598 ** of columns. If each indexed column corresponds to a foreign key
10599 ** column of pFKey, then this index is a winner. */
10600
10601 if( zKey==0 ){
10602 /* If zKey is NULL, then this foreign key is implicitly mapped to
10603 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
10604 ** identified by the test. */
10605 if( IsPrimaryKeyIndex(pIdx) ){
10606 if( aiCol ){
10607 int i;
10608 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
10609 }
10610 break;
10611 }
10612 }else{
10613 /* If zKey is non-NULL, then this foreign key was declared to
10614 ** map to an explicit list of columns in table pParent. Check if this
10615 ** index matches those columns. Also, check that the index uses
10616 ** the default collation sequences for each column. */
10617 int i, j;
10618 for(i=0; i<nCol; i++){
10619 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
10620 const char *zDfltColl; /* Def. collation for column */
10621 char *zIdxCol; /* Name of indexed column */
10622
10623 if( iCol<0 ) break; /* No foreign keys against expression indexes */
10624
10625 /* If the index uses a collation sequence that is different from
10626 ** the default collation sequence for the column, this index is
10627 ** unusable. Bail out early in this case. */
10628 zDfltColl = pParent->aCol[iCol].zColl;
10629 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
10630 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
10631
10632 zIdxCol = pParent->aCol[iCol].zName;
10633 for(j=0; j<nCol; j++){
10634 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
10635 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
10636 break;
10637 }
10638 }
10639 if( j==nCol ) break;
10640 }
10641 if( i==nCol ) break; /* pIdx is usable */
10642 }
10643 }
10644 }
10645
10646 if( !pIdx ){
10647 if( !pParse->disableTriggers ){
10648 sqlite3ErrorMsg(pParse,
10649 "foreign key mismatch - \"%w\" referencing \"%w\"",
10650 pFKey->pFrom->zName, pFKey->zTo);
10651 }
10652 sqlite3DbFree(pParse->db, aiCol);
10653 return 1;
10654 }
10655
10656 *ppIdx = pIdx;
10657 return 0;
10658 }
10659
10660 /*
10661 ** This function is called when a row is inserted into or deleted from the
10662 ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
10663 ** on the child table of pFKey, this function is invoked twice for each row
10664 ** affected - once to "delete" the old row, and then again to "insert" the
10665 ** new row.
10666 **
10667 ** Each time it is called, this function generates VDBE code to locate the
10668 ** row in the parent table that corresponds to the row being inserted into
10669 ** or deleted from the child table. If the parent row can be found, no
10670 ** special action is taken. Otherwise, if the parent row can *not* be
10671 ** found in the parent table:
10672 **
10673 ** Operation | FK type | Action taken
10674 ** --------------------------------------------------------------------------
10675 ** INSERT immediate Increment the "immediate constraint counter".
10676 **
10677 ** DELETE immediate Decrement the "immediate constraint counter".
10678 **
10679 ** INSERT deferred Increment the "deferred constraint counter".
10680 **
10681 ** DELETE deferred Decrement the "deferred constraint counter".
10682 **
10683 ** These operations are identified in the comment at the top of this file
10684 ** (fkey.c) as "I.1" and "D.1".
10685 */
10686 static void fkLookupParent(
10687 Parse *pParse, /* Parse context */
10688 int iDb, /* Index of database housing pTab */
10689 Table *pTab, /* Parent table of FK pFKey */
10690 Index *pIdx, /* Unique index on parent key columns in pTab */
10691 FKey *pFKey, /* Foreign key constraint */
10692 int *aiCol, /* Map from parent key columns to child table columns */
10693 int regData, /* Address of array containing child table row */
10694 int nIncr, /* Increment constraint counter by this */
10695 int isIgnore /* If true, pretend pTab contains all NULL values */
10696 ){
10697 int i; /* Iterator variable */
10698 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
10699 int iCur = pParse->nTab - 1; /* Cursor number to use */
10700 int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */
10701
10702 /* If nIncr is less than zero, then check at runtime if there are any
10703 ** outstanding constraints to resolve. If there are not, there is no need
10704 ** to check if deleting this row resolves any outstanding violations.
10705 **
10706 ** Check if any of the key columns in the child table row are NULL. If
10707 ** any are, then the constraint is considered satisfied. No need to
10708 ** search for a matching row in the parent table. */
10709 if( nIncr<0 ){
10710 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
10711 VdbeCoverage(v);
10712 }
10713 for(i=0; i<pFKey->nCol; i++){
10714 int iReg = aiCol[i] + regData + 1;
10715 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
10716 }
10717
10718 if( isIgnore==0 ){
10719 if( pIdx==0 ){
10720 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
10721 ** column of the parent table (table pTab). */
10722 int iMustBeInt; /* Address of MustBeInt instruction */
10723 int regTemp = sqlite3GetTempReg(pParse);
10724
10725 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
10726 ** apply the affinity of the parent key). If this fails, then there
10727 ** is no matching parent key. Before using MustBeInt, make a copy of
10728 ** the value. Otherwise, the value inserted into the child key column
10729 ** will have INTEGER affinity applied to it, which may not be correct. */
10730 sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
10731 iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
10732 VdbeCoverage(v);
10733
10734 /* If the parent table is the same as the child table, and we are about
10735 ** to increment the constraint-counter (i.e. this is an INSERT operation),
10736 ** then check if the row being inserted matches itself. If so, do not
10737 ** increment the constraint-counter. */
10738 if( pTab==pFKey->pFrom && nIncr==1 ){
10739 sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
10740 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
10741 }
10742
10743 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
10744 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
10745 sqlite3VdbeGoto(v, iOk);
10746 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
10747 sqlite3VdbeJumpHere(v, iMustBeInt);
10748 sqlite3ReleaseTempReg(pParse, regTemp);
10749 }else{
10750 int nCol = pFKey->nCol;
10751 int regTemp = sqlite3GetTempRange(pParse, nCol);
10752 int regRec = sqlite3GetTempReg(pParse);
10753
10754 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
10755 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
10756 for(i=0; i<nCol; i++){
10757 sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
10758 }
10759
10760 /* If the parent table is the same as the child table, and we are about
10761 ** to increment the constraint-counter (i.e. this is an INSERT operation),
10762 ** then check if the row being inserted matches itself. If so, do not
10763 ** increment the constraint-counter.
10764 **
10765 ** If any of the parent-key values are NULL, then the row cannot match
10766 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
10767 ** of the parent-key values are NULL (at this point it is known that
10768 ** none of the child key values are).
10769 */
10770 if( pTab==pFKey->pFrom && nIncr==1 ){
10771 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
10772 for(i=0; i<nCol; i++){
10773 int iChild = aiCol[i]+1+regData;
10774 int iParent = pIdx->aiColumn[i]+1+regData;
10775 assert( pIdx->aiColumn[i]>=0 );
10776 assert( aiCol[i]!=pTab->iPKey );
10777 if( pIdx->aiColumn[i]==pTab->iPKey ){
10778 /* The parent key is a composite key that includes the IPK column */
10779 iParent = regData;
10780 }
10781 sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
10782 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
10783 }
10784 sqlite3VdbeGoto(v, iOk);
10785 }
10786
10787 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
10788 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
10789 sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
10790
10791 sqlite3ReleaseTempReg(pParse, regRec);
10792 sqlite3ReleaseTempRange(pParse, regTemp, nCol);
10793 }
10794 }
10795
10796 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
10797 && !pParse->pToplevel
10798 && !pParse->isMultiWrite
10799 ){
10800 /* Special case: If this is an INSERT statement that will insert exactly
10801 ** one row into the table, raise a constraint immediately instead of
10802 ** incrementing a counter. This is necessary as the VM code is being
10803 ** generated for will not open a statement transaction. */
10804 assert( nIncr==1 );
10805 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
10806 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
10807 }else{
10808 if( nIncr>0 && pFKey->isDeferred==0 ){
10809 sqlite3MayAbort(pParse);
10810 }
10811 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
10812 }
10813
10814 sqlite3VdbeResolveLabel(v, iOk);
10815 sqlite3VdbeAddOp1(v, OP_Close, iCur);
10816 }
10817
10818
10819 /*
10820 ** Return an Expr object that refers to a memory register corresponding
10821 ** to column iCol of table pTab.
10822 **
10823 ** regBase is the first of an array of register that contains the data
10824 ** for pTab. regBase itself holds the rowid. regBase+1 holds the first
10825 ** column. regBase+2 holds the second column, and so forth.
10826 */
10827 static Expr *exprTableRegister(
10828 Parse *pParse, /* Parsing and code generating context */
10829 Table *pTab, /* The table whose content is at r[regBase]... */
10830 int regBase, /* Contents of table pTab */
10831 i16 iCol /* Which column of pTab is desired */
10832 ){
10833 Expr *pExpr;
10834 Column *pCol;
10835 const char *zColl;
10836 sqlite3 *db = pParse->db;
10837
10838 pExpr = sqlite3Expr(db, TK_REGISTER, 0);
10839 if( pExpr ){
10840 if( iCol>=0 && iCol!=pTab->iPKey ){
10841 pCol = &pTab->aCol[iCol];
10842 pExpr->iTable = regBase + iCol + 1;
10843 pExpr->affinity = pCol->affinity;
10844 zColl = pCol->zColl;
10845 if( zColl==0 ) zColl = db->pDfltColl->zName;
10846 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
10847 }else{
10848 pExpr->iTable = regBase;
10849 pExpr->affinity = SQLITE_AFF_INTEGER;
10850 }
10851 }
10852 return pExpr;
10853 }
10854
10855 /*
10856 ** Return an Expr object that refers to column iCol of table pTab which
10857 ** has cursor iCur.
10858 */
10859 static Expr *exprTableColumn(
10860 sqlite3 *db, /* The database connection */
10861 Table *pTab, /* The table whose column is desired */
10862 int iCursor, /* The open cursor on the table */
10863 i16 iCol /* The column that is wanted */
10864 ){
10865 Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
10866 if( pExpr ){
10867 pExpr->pTab = pTab;
10868 pExpr->iTable = iCursor;
10869 pExpr->iColumn = iCol;
10870 }
10871 return pExpr;
10872 }
10873
10874 /*
10875 ** This function is called to generate code executed when a row is deleted
10876 ** from the parent table of foreign key constraint pFKey and, if pFKey is
10877 ** deferred, when a row is inserted into the same table. When generating
10878 ** code for an SQL UPDATE operation, this function may be called twice -
10879 ** once to "delete" the old row and once to "insert" the new row.
10880 **
10881 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
10882 ** the number of FK violations in the db) or +1 when deleting one (as this
10883 ** may increase the number of FK constraint problems).
10884 **
10885 ** The code generated by this function scans through the rows in the child
10886 ** table that correspond to the parent table row being deleted or inserted.
10887 ** For each child row found, one of the following actions is taken:
10888 **
10889 ** Operation | FK type | Action taken
10890 ** --------------------------------------------------------------------------
10891 ** DELETE immediate Increment the "immediate constraint counter".
10892 ** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
10893 ** throw a "FOREIGN KEY constraint failed" exception.
10894 **
10895 ** INSERT immediate Decrement the "immediate constraint counter".
10896 **
10897 ** DELETE deferred Increment the "deferred constraint counter".
10898 ** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
10899 ** throw a "FOREIGN KEY constraint failed" exception.
10900 **
10901 ** INSERT deferred Decrement the "deferred constraint counter".
10902 **
10903 ** These operations are identified in the comment at the top of this file
10904 ** (fkey.c) as "I.2" and "D.2".
10905 */
10906 static void fkScanChildren(
10907 Parse *pParse, /* Parse context */
10908 SrcList *pSrc, /* The child table to be scanned */
10909 Table *pTab, /* The parent table */
10910 Index *pIdx, /* Index on parent covering the foreign key */
10911 FKey *pFKey, /* The foreign key linking pSrc to pTab */
10912 int *aiCol, /* Map from pIdx cols to child table cols */
10913 int regData, /* Parent row data starts here */
10914 int nIncr /* Amount to increment deferred counter by */
10915 ){
10916 sqlite3 *db = pParse->db; /* Database handle */
10917 int i; /* Iterator variable */
10918 Expr *pWhere = 0; /* WHERE clause to scan with */
10919 NameContext sNameContext; /* Context used to resolve WHERE clause */
10920 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
10921 int iFkIfZero = 0; /* Address of OP_FkIfZero */
10922 Vdbe *v = sqlite3GetVdbe(pParse);
10923
10924 assert( pIdx==0 || pIdx->pTable==pTab );
10925 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
10926 assert( pIdx!=0 || pFKey->nCol==1 );
10927 assert( pIdx!=0 || HasRowid(pTab) );
10928
10929 if( nIncr<0 ){
10930 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
10931 VdbeCoverage(v);
10932 }
10933
10934 /* Create an Expr object representing an SQL expression like:
10935 **
10936 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
10937 **
10938 ** The collation sequence used for the comparison should be that of
10939 ** the parent key columns. The affinity of the parent key column should
10940 ** be applied to each child key value before the comparison takes place.
10941 */
10942 for(i=0; i<pFKey->nCol; i++){
10943 Expr *pLeft; /* Value from parent table row */
10944 Expr *pRight; /* Column ref to child table */
10945 Expr *pEq; /* Expression (pLeft = pRight) */
10946 i16 iCol; /* Index of column in child table */
10947 const char *zCol; /* Name of column in child table */
10948
10949 iCol = pIdx ? pIdx->aiColumn[i] : -1;
10950 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
10951 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
10952 assert( iCol>=0 );
10953 zCol = pFKey->pFrom->aCol[iCol].zName;
10954 pRight = sqlite3Expr(db, TK_ID, zCol);
10955 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
10956 pWhere = sqlite3ExprAnd(db, pWhere, pEq);
10957 }
10958
10959 /* If the child table is the same as the parent table, then add terms
10960 ** to the WHERE clause that prevent this entry from being scanned.
10961 ** The added WHERE clause terms are like this:
10962 **
10963 ** $current_rowid!=rowid
10964 ** NOT( $current_a==a AND $current_b==b AND ... )
10965 **
10966 ** The first form is used for rowid tables. The second form is used
10967 ** for WITHOUT ROWID tables. In the second form, the primary key is
10968 ** (a,b,...)
10969 */
10970 if( pTab==pFKey->pFrom && nIncr>0 ){
10971 Expr *pNe; /* Expression (pLeft != pRight) */
10972 Expr *pLeft; /* Value from parent table row */
10973 Expr *pRight; /* Column ref to child table */
10974 if( HasRowid(pTab) ){
10975 pLeft = exprTableRegister(pParse, pTab, regData, -1);
10976 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
10977 pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
10978 }else{
10979 Expr *pEq, *pAll = 0;
10980 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
10981 assert( pIdx!=0 );
10982 for(i=0; i<pPk->nKeyCol; i++){
10983 i16 iCol = pIdx->aiColumn[i];
10984 assert( iCol>=0 );
10985 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
10986 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
10987 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
10988 pAll = sqlite3ExprAnd(db, pAll, pEq);
10989 }
10990 pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
10991 }
10992 pWhere = sqlite3ExprAnd(db, pWhere, pNe);
10993 }
10994
10995 /* Resolve the references in the WHERE clause. */
10996 memset(&sNameContext, 0, sizeof(NameContext));
10997 sNameContext.pSrcList = pSrc;
10998 sNameContext.pParse = pParse;
10999 sqlite3ResolveExprNames(&sNameContext, pWhere);
11000
11001 /* Create VDBE to loop through the entries in pSrc that match the WHERE
11002 ** clause. For each row found, increment either the deferred or immediate
11003 ** foreign key constraint counter. */
11004 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
11005 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
11006 if( pWInfo ){
11007 sqlite3WhereEnd(pWInfo);
11008 }
11009
11010 /* Clean up the WHERE clause constructed above. */
11011 sqlite3ExprDelete(db, pWhere);
11012 if( iFkIfZero ){
11013 sqlite3VdbeJumpHere(v, iFkIfZero);
11014 }
11015 }
11016
11017 /*
11018 ** This function returns a linked list of FKey objects (connected by
11019 ** FKey.pNextTo) holding all children of table pTab. For example,
11020 ** given the following schema:
11021 **
11022 ** CREATE TABLE t1(a PRIMARY KEY);
11023 ** CREATE TABLE t2(b REFERENCES t1(a);
11024 **
11025 ** Calling this function with table "t1" as an argument returns a pointer
11026 ** to the FKey structure representing the foreign key constraint on table
11027 ** "t2". Calling this function with "t2" as the argument would return a
11028 ** NULL pointer (as there are no FK constraints for which t2 is the parent
11029 ** table).
11030 */
11031 SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
11032 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
11033 }
11034
11035 /*
11036 ** The second argument is a Trigger structure allocated by the
11037 ** fkActionTrigger() routine. This function deletes the Trigger structure
11038 ** and all of its sub-components.
11039 **
11040 ** The Trigger structure or any of its sub-components may be allocated from
11041 ** the lookaside buffer belonging to database handle dbMem.
11042 */
11043 static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
11044 if( p ){
11045 TriggerStep *pStep = p->step_list;
11046 sqlite3ExprDelete(dbMem, pStep->pWhere);
11047 sqlite3ExprListDelete(dbMem, pStep->pExprList);
11048 sqlite3SelectDelete(dbMem, pStep->pSelect);
11049 sqlite3ExprDelete(dbMem, p->pWhen);
11050 sqlite3DbFree(dbMem, p);
11051 }
11052 }
11053
11054 /*
11055 ** This function is called to generate code that runs when table pTab is
11056 ** being dropped from the database. The SrcList passed as the second argument
11057 ** to this function contains a single entry guaranteed to resolve to
11058 ** table pTab.
11059 **
11060 ** Normally, no code is required. However, if either
11061 **
11062 ** (a) The table is the parent table of a FK constraint, or
11063 ** (b) The table is the child table of a deferred FK constraint and it is
11064 ** determined at runtime that there are outstanding deferred FK
11065 ** constraint violations in the database,
11066 **
11067 ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
11068 ** the table from the database. Triggers are disabled while running this
11069 ** DELETE, but foreign key actions are not.
11070 */
11071 SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTa b){
11072 sqlite3 *db = pParse->db;
11073 if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
11074 int iSkip = 0;
11075 Vdbe *v = sqlite3GetVdbe(pParse);
11076
11077 assert( v ); /* VDBE has already been allocated */
11078 if( sqlite3FkReferences(pTab)==0 ){
11079 /* Search for a deferred foreign key constraint for which this table
11080 ** is the child table. If one cannot be found, return without
11081 ** generating any VDBE code. If one can be found, then jump over
11082 ** the entire DELETE if there are no outstanding deferred constraints
11083 ** when this statement is run. */
11084 FKey *p;
11085 for(p=pTab->pFKey; p; p=p->pNextFrom){
11086 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
11087 }
11088 if( !p ) return;
11089 iSkip = sqlite3VdbeMakeLabel(v);
11090 sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
11091 }
11092
11093 pParse->disableTriggers = 1;
11094 sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
11095 pParse->disableTriggers = 0;
11096
11097 /* If the DELETE has generated immediate foreign key constraint
11098 ** violations, halt the VDBE and return an error at this point, before
11099 ** any modifications to the schema are made. This is because statement
11100 ** transactions are not able to rollback schema changes.
11101 **
11102 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
11103 ** the statement transaction will not be rolled back even if FK
11104 ** constraints are violated.
11105 */
11106 if( (db->flags & SQLITE_DeferFKs)==0 ){
11107 sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
11108 VdbeCoverage(v);
11109 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
11110 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
11111 }
11112
11113 if( iSkip ){
11114 sqlite3VdbeResolveLabel(v, iSkip);
11115 }
11116 }
11117 }
11118
11119
11120 /*
11121 ** The second argument points to an FKey object representing a foreign key
11122 ** for which pTab is the child table. An UPDATE statement against pTab
11123 ** is currently being processed. For each column of the table that is
11124 ** actually updated, the corresponding element in the aChange[] array
11125 ** is zero or greater (if a column is unmodified the corresponding element
11126 ** is set to -1). If the rowid column is modified by the UPDATE statement
11127 ** the bChngRowid argument is non-zero.
11128 **
11129 ** This function returns true if any of the columns that are part of the
11130 ** child key for FK constraint *p are modified.
11131 */
11132 static int fkChildIsModified(
11133 Table *pTab, /* Table being updated */
11134 FKey *p, /* Foreign key for which pTab is the child */
11135 int *aChange, /* Array indicating modified columns */
11136 int bChngRowid /* True if rowid is modified by this update */
11137 ){
11138 int i;
11139 for(i=0; i<p->nCol; i++){
11140 int iChildKey = p->aCol[i].iFrom;
11141 if( aChange[iChildKey]>=0 ) return 1;
11142 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
11143 }
11144 return 0;
11145 }
11146
11147 /*
11148 ** The second argument points to an FKey object representing a foreign key
11149 ** for which pTab is the parent table. An UPDATE statement against pTab
11150 ** is currently being processed. For each column of the table that is
11151 ** actually updated, the corresponding element in the aChange[] array
11152 ** is zero or greater (if a column is unmodified the corresponding element
11153 ** is set to -1). If the rowid column is modified by the UPDATE statement
11154 ** the bChngRowid argument is non-zero.
11155 **
11156 ** This function returns true if any of the columns that are part of the
11157 ** parent key for FK constraint *p are modified.
11158 */
11159 static int fkParentIsModified(
11160 Table *pTab,
11161 FKey *p,
11162 int *aChange,
11163 int bChngRowid
11164 ){
11165 int i;
11166 for(i=0; i<p->nCol; i++){
11167 char *zKey = p->aCol[i].zCol;
11168 int iKey;
11169 for(iKey=0; iKey<pTab->nCol; iKey++){
11170 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
11171 Column *pCol = &pTab->aCol[iKey];
11172 if( zKey ){
11173 if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
11174 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
11175 return 1;
11176 }
11177 }
11178 }
11179 }
11180 return 0;
11181 }
11182
11183 /*
11184 ** Return true if the parser passed as the first argument is being
11185 ** used to code a trigger that is really a "SET NULL" action belonging
11186 ** to trigger pFKey.
11187 */
11188 static int isSetNullAction(Parse *pParse, FKey *pFKey){
11189 Parse *pTop = sqlite3ParseToplevel(pParse);
11190 if( pTop->pTriggerPrg ){
11191 Trigger *p = pTop->pTriggerPrg->pTrigger;
11192 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
11193 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
11194 ){
11195 return 1;
11196 }
11197 }
11198 return 0;
11199 }
11200
11201 /*
11202 ** This function is called when inserting, deleting or updating a row of
11203 ** table pTab to generate VDBE code to perform foreign key constraint
11204 ** processing for the operation.
11205 **
11206 ** For a DELETE operation, parameter regOld is passed the index of the
11207 ** first register in an array of (pTab->nCol+1) registers containing the
11208 ** rowid of the row being deleted, followed by each of the column values
11209 ** of the row being deleted, from left to right. Parameter regNew is passed
11210 ** zero in this case.
11211 **
11212 ** For an INSERT operation, regOld is passed zero and regNew is passed the
11213 ** first register of an array of (pTab->nCol+1) registers containing the new
11214 ** row data.
11215 **
11216 ** For an UPDATE operation, this function is called twice. Once before
11217 ** the original record is deleted from the table using the calling convention
11218 ** described for DELETE. Then again after the original record is deleted
11219 ** but before the new record is inserted using the INSERT convention.
11220 */
11221 SQLITE_PRIVATE void sqlite3FkCheck(
11222 Parse *pParse, /* Parse context */
11223 Table *pTab, /* Row is being deleted from this table */
11224 int regOld, /* Previous row data is stored here */
11225 int regNew, /* New row data is stored here */
11226 int *aChange, /* Array indicating UPDATEd columns (or 0) */
11227 int bChngRowid /* True if rowid is UPDATEd */
11228 ){
11229 sqlite3 *db = pParse->db; /* Database handle */
11230 FKey *pFKey; /* Used to iterate through FKs */
11231 int iDb; /* Index of database containing pTab */
11232 const char *zDb; /* Name of database containing pTab */
11233 int isIgnoreErrors = pParse->disableTriggers;
11234
11235 /* Exactly one of regOld and regNew should be non-zero. */
11236 assert( (regOld==0)!=(regNew==0) );
11237
11238 /* If foreign-keys are disabled, this function is a no-op. */
11239 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
11240
11241 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
11242 zDb = db->aDb[iDb].zName;
11243
11244 /* Loop through all the foreign key constraints for which pTab is the
11245 ** child table (the table that the foreign key definition is part of). */
11246 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
11247 Table *pTo; /* Parent table of foreign key pFKey */
11248 Index *pIdx = 0; /* Index on key columns in pTo */
11249 int *aiFree = 0;
11250 int *aiCol;
11251 int iCol;
11252 int i;
11253 int bIgnore = 0;
11254
11255 if( aChange
11256 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
11257 && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
11258 ){
11259 continue;
11260 }
11261
11262 /* Find the parent table of this foreign key. Also find a unique index
11263 ** on the parent key columns in the parent table. If either of these
11264 ** schema items cannot be located, set an error in pParse and return
11265 ** early. */
11266 if( pParse->disableTriggers ){
11267 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
11268 }else{
11269 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
11270 }
11271 if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
11272 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
11273 if( !isIgnoreErrors || db->mallocFailed ) return;
11274 if( pTo==0 ){
11275 /* If isIgnoreErrors is true, then a table is being dropped. In this
11276 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
11277 ** before actually dropping it in order to check FK constraints.
11278 ** If the parent table of an FK constraint on the current table is
11279 ** missing, behave as if it is empty. i.e. decrement the relevant
11280 ** FK counter for each row of the current table with non-NULL keys.
11281 */
11282 Vdbe *v = sqlite3GetVdbe(pParse);
11283 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
11284 for(i=0; i<pFKey->nCol; i++){
11285 int iReg = pFKey->aCol[i].iFrom + regOld + 1;
11286 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
11287 }
11288 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
11289 }
11290 continue;
11291 }
11292 assert( pFKey->nCol==1 || (aiFree && pIdx) );
11293
11294 if( aiFree ){
11295 aiCol = aiFree;
11296 }else{
11297 iCol = pFKey->aCol[0].iFrom;
11298 aiCol = &iCol;
11299 }
11300 for(i=0; i<pFKey->nCol; i++){
11301 if( aiCol[i]==pTab->iPKey ){
11302 aiCol[i] = -1;
11303 }
11304 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
11305 #ifndef SQLITE_OMIT_AUTHORIZATION
11306 /* Request permission to read the parent key columns. If the
11307 ** authorization callback returns SQLITE_IGNORE, behave as if any
11308 ** values read from the parent table are NULL. */
11309 if( db->xAuth ){
11310 int rcauth;
11311 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
11312 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
11313 bIgnore = (rcauth==SQLITE_IGNORE);
11314 }
11315 #endif
11316 }
11317
11318 /* Take a shared-cache advisory read-lock on the parent table. Allocate
11319 ** a cursor to use to search the unique index on the parent key columns
11320 ** in the parent table. */
11321 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
11322 pParse->nTab++;
11323
11324 if( regOld!=0 ){
11325 /* A row is being removed from the child table. Search for the parent.
11326 ** If the parent does not exist, removing the child row resolves an
11327 ** outstanding foreign key constraint violation. */
11328 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
11329 }
11330 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
11331 /* A row is being added to the child table. If a parent row cannot
11332 ** be found, adding the child row has violated the FK constraint.
11333 **
11334 ** If this operation is being performed as part of a trigger program
11335 ** that is actually a "SET NULL" action belonging to this very
11336 ** foreign key, then omit this scan altogether. As all child key
11337 ** values are guaranteed to be NULL, it is not possible for adding
11338 ** this row to cause an FK violation. */
11339 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
11340 }
11341
11342 sqlite3DbFree(db, aiFree);
11343 }
11344
11345 /* Loop through all the foreign key constraints that refer to this table.
11346 ** (the "child" constraints) */
11347 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
11348 Index *pIdx = 0; /* Foreign key index for pFKey */
11349 SrcList *pSrc;
11350 int *aiCol = 0;
11351
11352 if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
11353 continue;
11354 }
11355
11356 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
11357 && !pParse->pToplevel && !pParse->isMultiWrite
11358 ){
11359 assert( regOld==0 && regNew!=0 );
11360 /* Inserting a single row into a parent table cannot cause (or fix)
11361 ** an immediate foreign key violation. So do nothing in this case. */
11362 continue;
11363 }
11364
11365 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
11366 if( !isIgnoreErrors || db->mallocFailed ) return;
11367 continue;
11368 }
11369 assert( aiCol || pFKey->nCol==1 );
11370
11371 /* Create a SrcList structure containing the child table. We need the
11372 ** child table as a SrcList for sqlite3WhereBegin() */
11373 pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
11374 if( pSrc ){
11375 struct SrcList_item *pItem = pSrc->a;
11376 pItem->pTab = pFKey->pFrom;
11377 pItem->zName = pFKey->pFrom->zName;
11378 pItem->pTab->nRef++;
11379 pItem->iCursor = pParse->nTab++;
11380
11381 if( regNew!=0 ){
11382 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
11383 }
11384 if( regOld!=0 ){
11385 int eAction = pFKey->aAction[aChange!=0];
11386 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
11387 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
11388 ** action applies, then any foreign key violations caused by
11389 ** removing the parent key will be rectified by the action trigger.
11390 ** So do not set the "may-abort" flag in this case.
11391 **
11392 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
11393 ** may-abort flag will eventually be set on this statement anyway
11394 ** (when this function is called as part of processing the UPDATE
11395 ** within the action trigger).
11396 **
11397 ** Note 2: At first glance it may seem like SQLite could simply omit
11398 ** all OP_FkCounter related scans when either CASCADE or SET NULL
11399 ** applies. The trouble starts if the CASCADE or SET NULL action
11400 ** trigger causes other triggers or action rules attached to the
11401 ** child table to fire. In these cases the fk constraint counters
11402 ** might be set incorrectly if any OP_FkCounter related scans are
11403 ** omitted. */
11404 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
11405 sqlite3MayAbort(pParse);
11406 }
11407 }
11408 pItem->zName = 0;
11409 sqlite3SrcListDelete(db, pSrc);
11410 }
11411 sqlite3DbFree(db, aiCol);
11412 }
11413 }
11414
11415 #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
11416
11417 /*
11418 ** This function is called before generating code to update or delete a
11419 ** row contained in table pTab.
11420 */
11421 SQLITE_PRIVATE u32 sqlite3FkOldmask(
11422 Parse *pParse, /* Parse context */
11423 Table *pTab /* Table being modified */
11424 ){
11425 u32 mask = 0;
11426 if( pParse->db->flags&SQLITE_ForeignKeys ){
11427 FKey *p;
11428 int i;
11429 for(p=pTab->pFKey; p; p=p->pNextFrom){
11430 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
11431 }
11432 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
11433 Index *pIdx = 0;
11434 sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
11435 if( pIdx ){
11436 for(i=0; i<pIdx->nKeyCol; i++){
11437 assert( pIdx->aiColumn[i]>=0 );
11438 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
11439 }
11440 }
11441 }
11442 }
11443 return mask;
11444 }
11445
11446
11447 /*
11448 ** This function is called before generating code to update or delete a
11449 ** row contained in table pTab. If the operation is a DELETE, then
11450 ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
11451 ** to an array of size N, where N is the number of columns in table pTab.
11452 ** If the i'th column is not modified by the UPDATE, then the corresponding
11453 ** entry in the aChange[] array is set to -1. If the column is modified,
11454 ** the value is 0 or greater. Parameter chngRowid is set to true if the
11455 ** UPDATE statement modifies the rowid fields of the table.
11456 **
11457 ** If any foreign key processing will be required, this function returns
11458 ** true. If there is no foreign key related processing, this function
11459 ** returns false.
11460 */
11461 SQLITE_PRIVATE int sqlite3FkRequired(
11462 Parse *pParse, /* Parse context */
11463 Table *pTab, /* Table being modified */
11464 int *aChange, /* Non-NULL for UPDATE operations */
11465 int chngRowid /* True for UPDATE that affects rowid */
11466 ){
11467 if( pParse->db->flags&SQLITE_ForeignKeys ){
11468 if( !aChange ){
11469 /* A DELETE operation. Foreign key processing is required if the
11470 ** table in question is either the child or parent table for any
11471 ** foreign key constraint. */
11472 return (sqlite3FkReferences(pTab) || pTab->pFKey);
11473 }else{
11474 /* This is an UPDATE. Foreign key processing is only required if the
11475 ** operation modifies one or more child or parent key columns. */
11476 FKey *p;
11477
11478 /* Check if any child key columns are being modified. */
11479 for(p=pTab->pFKey; p; p=p->pNextFrom){
11480 if( fkChildIsModified(pTab, p, aChange, chngRowid) ) return 1;
11481 }
11482
11483 /* Check if any parent key columns are being modified. */
11484 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
11485 if( fkParentIsModified(pTab, p, aChange, chngRowid) ) return 1;
11486 }
11487 }
11488 }
11489 return 0;
11490 }
11491
11492 /*
11493 ** This function is called when an UPDATE or DELETE operation is being
11494 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
11495 ** If the current operation is an UPDATE, then the pChanges parameter is
11496 ** passed a pointer to the list of columns being modified. If it is a
11497 ** DELETE, pChanges is passed a NULL pointer.
11498 **
11499 ** It returns a pointer to a Trigger structure containing a trigger
11500 ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
11501 ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
11502 ** returned (these actions require no special handling by the triggers
11503 ** sub-system, code for them is created by fkScanChildren()).
11504 **
11505 ** For example, if pFKey is the foreign key and pTab is table "p" in
11506 ** the following schema:
11507 **
11508 ** CREATE TABLE p(pk PRIMARY KEY);
11509 ** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
11510 **
11511 ** then the returned trigger structure is equivalent to:
11512 **
11513 ** CREATE TRIGGER ... DELETE ON p BEGIN
11514 ** DELETE FROM c WHERE ck = old.pk;
11515 ** END;
11516 **
11517 ** The returned pointer is cached as part of the foreign key object. It
11518 ** is eventually freed along with the rest of the foreign key object by
11519 ** sqlite3FkDelete().
11520 */
11521 static Trigger *fkActionTrigger(
11522 Parse *pParse, /* Parse context */
11523 Table *pTab, /* Table being updated or deleted from */
11524 FKey *pFKey, /* Foreign key to get action for */
11525 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
11526 ){
11527 sqlite3 *db = pParse->db; /* Database handle */
11528 int action; /* One of OE_None, OE_Cascade etc. */
11529 Trigger *pTrigger; /* Trigger definition to return */
11530 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
11531
11532 action = pFKey->aAction[iAction];
11533 pTrigger = pFKey->apTrigger[iAction];
11534
11535 if( action!=OE_None && !pTrigger ){
11536 u8 enableLookaside; /* Copy of db->lookaside.bEnabled */
11537 char const *zFrom; /* Name of child table */
11538 int nFrom; /* Length in bytes of zFrom */
11539 Index *pIdx = 0; /* Parent key index for this FK */
11540 int *aiCol = 0; /* child table cols -> parent key cols */
11541 TriggerStep *pStep = 0; /* First (only) step of trigger program */
11542 Expr *pWhere = 0; /* WHERE clause of trigger step */
11543 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
11544 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
11545 int i; /* Iterator variable */
11546 Expr *pWhen = 0; /* WHEN clause for the trigger */
11547
11548 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
11549 assert( aiCol || pFKey->nCol==1 );
11550
11551 for(i=0; i<pFKey->nCol; i++){
11552 Token tOld = { "old", 3 }; /* Literal "old" token */
11553 Token tNew = { "new", 3 }; /* Literal "new" token */
11554 Token tFromCol; /* Name of column in child table */
11555 Token tToCol; /* Name of column in parent table */
11556 int iFromCol; /* Idx of column in child table */
11557 Expr *pEq; /* tFromCol = OLD.tToCol */
11558
11559 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
11560 assert( iFromCol>=0 );
11561 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
11562 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
11563 tToCol.z = pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName;
11564 tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
11565
11566 tToCol.n = sqlite3Strlen30(tToCol.z);
11567 tFromCol.n = sqlite3Strlen30(tFromCol.z);
11568
11569 /* Create the expression "OLD.zToCol = zFromCol". It is important
11570 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
11571 ** that the affinity and collation sequence associated with the
11572 ** parent table are used for the comparison. */
11573 pEq = sqlite3PExpr(pParse, TK_EQ,
11574 sqlite3PExpr(pParse, TK_DOT,
11575 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
11576 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
11577 , 0),
11578 sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
11579 , 0);
11580 pWhere = sqlite3ExprAnd(db, pWhere, pEq);
11581
11582 /* For ON UPDATE, construct the next term of the WHEN clause.
11583 ** The final WHEN clause will be like this:
11584 **
11585 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
11586 */
11587 if( pChanges ){
11588 pEq = sqlite3PExpr(pParse, TK_IS,
11589 sqlite3PExpr(pParse, TK_DOT,
11590 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
11591 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
11592 0),
11593 sqlite3PExpr(pParse, TK_DOT,
11594 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
11595 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
11596 0),
11597 0);
11598 pWhen = sqlite3ExprAnd(db, pWhen, pEq);
11599 }
11600
11601 if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
11602 Expr *pNew;
11603 if( action==OE_Cascade ){
11604 pNew = sqlite3PExpr(pParse, TK_DOT,
11605 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
11606 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
11607 , 0);
11608 }else if( action==OE_SetDflt ){
11609 Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
11610 if( pDflt ){
11611 pNew = sqlite3ExprDup(db, pDflt, 0);
11612 }else{
11613 pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
11614 }
11615 }else{
11616 pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
11617 }
11618 pList = sqlite3ExprListAppend(pParse, pList, pNew);
11619 sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
11620 }
11621 }
11622 sqlite3DbFree(db, aiCol);
11623
11624 zFrom = pFKey->pFrom->zName;
11625 nFrom = sqlite3Strlen30(zFrom);
11626
11627 if( action==OE_Restrict ){
11628 Token tFrom;
11629 Expr *pRaise;
11630
11631 tFrom.z = zFrom;
11632 tFrom.n = nFrom;
11633 pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
11634 if( pRaise ){
11635 pRaise->affinity = OE_Abort;
11636 }
11637 pSelect = sqlite3SelectNew(pParse,
11638 sqlite3ExprListAppend(pParse, 0, pRaise),
11639 sqlite3SrcListAppend(db, 0, &tFrom, 0),
11640 pWhere,
11641 0, 0, 0, 0, 0, 0
11642 );
11643 pWhere = 0;
11644 }
11645
11646 /* Disable lookaside memory allocation */
11647 enableLookaside = db->lookaside.bEnabled;
11648 db->lookaside.bEnabled = 0;
11649
11650 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
11651 sizeof(Trigger) + /* struct Trigger */
11652 sizeof(TriggerStep) + /* Single step in trigger program */
11653 nFrom + 1 /* Space for pStep->zTarget */
11654 );
11655 if( pTrigger ){
11656 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
11657 pStep->zTarget = (char *)&pStep[1];
11658 memcpy((char *)pStep->zTarget, zFrom, nFrom);
11659
11660 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
11661 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
11662 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
11663 if( pWhen ){
11664 pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
11665 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
11666 }
11667 }
11668
11669 /* Re-enable the lookaside buffer, if it was disabled earlier. */
11670 db->lookaside.bEnabled = enableLookaside;
11671
11672 sqlite3ExprDelete(db, pWhere);
11673 sqlite3ExprDelete(db, pWhen);
11674 sqlite3ExprListDelete(db, pList);
11675 sqlite3SelectDelete(db, pSelect);
11676 if( db->mallocFailed==1 ){
11677 fkTriggerDelete(db, pTrigger);
11678 return 0;
11679 }
11680 assert( pStep!=0 );
11681
11682 switch( action ){
11683 case OE_Restrict:
11684 pStep->op = TK_SELECT;
11685 break;
11686 case OE_Cascade:
11687 if( !pChanges ){
11688 pStep->op = TK_DELETE;
11689 break;
11690 }
11691 default:
11692 pStep->op = TK_UPDATE;
11693 }
11694 pStep->pTrig = pTrigger;
11695 pTrigger->pSchema = pTab->pSchema;
11696 pTrigger->pTabSchema = pTab->pSchema;
11697 pFKey->apTrigger[iAction] = pTrigger;
11698 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
11699 }
11700
11701 return pTrigger;
11702 }
11703
11704 /*
11705 ** This function is called when deleting or updating a row to implement
11706 ** any required CASCADE, SET NULL or SET DEFAULT actions.
11707 */
11708 SQLITE_PRIVATE void sqlite3FkActions(
11709 Parse *pParse, /* Parse context */
11710 Table *pTab, /* Table being updated or deleted from */
11711 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
11712 int regOld, /* Address of array containing old row */
11713 int *aChange, /* Array indicating UPDATEd columns (or 0) */
11714 int bChngRowid /* True if rowid is UPDATEd */
11715 ){
11716 /* If foreign-key support is enabled, iterate through all FKs that
11717 ** refer to table pTab. If there is an action associated with the FK
11718 ** for this operation (either update or delete), invoke the associated
11719 ** trigger sub-program. */
11720 if( pParse->db->flags&SQLITE_ForeignKeys ){
11721 FKey *pFKey; /* Iterator variable */
11722 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
11723 if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
11724 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
11725 if( pAct ){
11726 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
11727 }
11728 }
11729 }
11730 }
11731 }
11732
11733 #endif /* ifndef SQLITE_OMIT_TRIGGER */
11734
11735 /*
11736 ** Free all memory associated with foreign key definitions attached to
11737 ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
11738 ** hash table.
11739 */
11740 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
11741 FKey *pFKey; /* Iterator variable */
11742 FKey *pNext; /* Copy of pFKey->pNextFrom */
11743
11744 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
11745 for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
11746
11747 /* Remove the FK from the fkeyHash hash table. */
11748 if( !db || db->pnBytesFreed==0 ){
11749 if( pFKey->pPrevTo ){
11750 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
11751 }else{
11752 void *p = (void *)pFKey->pNextTo;
11753 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
11754 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
11755 }
11756 if( pFKey->pNextTo ){
11757 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
11758 }
11759 }
11760
11761 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
11762 ** classified as either immediate or deferred.
11763 */
11764 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
11765
11766 /* Delete any triggers created to implement actions for this FK. */
11767 #ifndef SQLITE_OMIT_TRIGGER
11768 fkTriggerDelete(db, pFKey->apTrigger[0]);
11769 fkTriggerDelete(db, pFKey->apTrigger[1]);
11770 #endif
11771
11772 pNext = pFKey->pNextFrom;
11773 sqlite3DbFree(db, pFKey);
11774 }
11775 }
11776 #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
11777
11778 /************** End of fkey.c ************************************************/
11779 /************** Begin file insert.c ******************************************/
11780 /*
11781 ** 2001 September 15
11782 **
11783 ** The author disclaims copyright to this source code. In place of
11784 ** a legal notice, here is a blessing:
11785 **
11786 ** May you do good and not evil.
11787 ** May you find forgiveness for yourself and forgive others.
11788 ** May you share freely, never taking more than you give.
11789 **
11790 *************************************************************************
11791 ** This file contains C code routines that are called by the parser
11792 ** to handle INSERT statements in SQLite.
11793 */
11794 /* #include "sqliteInt.h" */
11795
11796 /*
11797 ** Generate code that will
11798 **
11799 ** (1) acquire a lock for table pTab then
11800 ** (2) open pTab as cursor iCur.
11801 **
11802 ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
11803 ** for that table that is actually opened.
11804 */
11805 SQLITE_PRIVATE void sqlite3OpenTable(
11806 Parse *pParse, /* Generate code into this VDBE */
11807 int iCur, /* The cursor number of the table */
11808 int iDb, /* The database index in sqlite3.aDb[] */
11809 Table *pTab, /* The table to be opened */
11810 int opcode /* OP_OpenRead or OP_OpenWrite */
11811 ){
11812 Vdbe *v;
11813 assert( !IsVirtual(pTab) );
11814 v = sqlite3GetVdbe(pParse);
11815 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
11816 sqlite3TableLock(pParse, iDb, pTab->tnum,
11817 (opcode==OP_OpenWrite)?1:0, pTab->zName);
11818 if( HasRowid(pTab) ){
11819 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
11820 VdbeComment((v, "%s", pTab->zName));
11821 }else{
11822 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
11823 assert( pPk!=0 );
11824 assert( pPk->tnum==pTab->tnum );
11825 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
11826 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
11827 VdbeComment((v, "%s", pTab->zName));
11828 }
11829 }
11830
11831 /*
11832 ** Return a pointer to the column affinity string associated with index
11833 ** pIdx. A column affinity string has one character for each column in
11834 ** the table, according to the affinity of the column:
11835 **
11836 ** Character Column affinity
11837 ** ------------------------------
11838 ** 'A' BLOB
11839 ** 'B' TEXT
11840 ** 'C' NUMERIC
11841 ** 'D' INTEGER
11842 ** 'F' REAL
11843 **
11844 ** An extra 'D' is appended to the end of the string to cover the
11845 ** rowid that appears as the last column in every index.
11846 **
11847 ** Memory for the buffer containing the column index affinity string
11848 ** is managed along with the rest of the Index structure. It will be
11849 ** released when sqlite3DeleteIndex() is called.
11850 */
11851 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
11852 if( !pIdx->zColAff ){
11853 /* The first time a column affinity string for a particular index is
11854 ** required, it is allocated and populated here. It is then stored as
11855 ** a member of the Index structure for subsequent use.
11856 **
11857 ** The column affinity string will eventually be deleted by
11858 ** sqliteDeleteIndex() when the Index structure itself is cleaned
11859 ** up.
11860 */
11861 int n;
11862 Table *pTab = pIdx->pTable;
11863 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
11864 if( !pIdx->zColAff ){
11865 db->mallocFailed = 1;
11866 return 0;
11867 }
11868 for(n=0; n<pIdx->nColumn; n++){
11869 i16 x = pIdx->aiColumn[n];
11870 if( x>=0 ){
11871 pIdx->zColAff[n] = pTab->aCol[x].affinity;
11872 }else if( x==XN_ROWID ){
11873 pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
11874 }else{
11875 char aff;
11876 assert( x==XN_EXPR );
11877 assert( pIdx->aColExpr!=0 );
11878 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
11879 if( aff==0 ) aff = SQLITE_AFF_BLOB;
11880 pIdx->zColAff[n] = aff;
11881 }
11882 }
11883 pIdx->zColAff[n] = 0;
11884 }
11885
11886 return pIdx->zColAff;
11887 }
11888
11889 /*
11890 ** Compute the affinity string for table pTab, if it has not already been
11891 ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
11892 **
11893 ** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
11894 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
11895 ** for register iReg and following. Or if affinities exists and iReg==0,
11896 ** then just set the P4 operand of the previous opcode (which should be
11897 ** an OP_MakeRecord) to the affinity string.
11898 **
11899 ** A column affinity string has one character per column:
11900 **
11901 ** Character Column affinity
11902 ** ------------------------------
11903 ** 'A' BLOB
11904 ** 'B' TEXT
11905 ** 'C' NUMERIC
11906 ** 'D' INTEGER
11907 ** 'E' REAL
11908 */
11909 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
11910 int i;
11911 char *zColAff = pTab->zColAff;
11912 if( zColAff==0 ){
11913 sqlite3 *db = sqlite3VdbeDb(v);
11914 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
11915 if( !zColAff ){
11916 db->mallocFailed = 1;
11917 return;
11918 }
11919
11920 for(i=0; i<pTab->nCol; i++){
11921 zColAff[i] = pTab->aCol[i].affinity;
11922 }
11923 do{
11924 zColAff[i--] = 0;
11925 }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
11926 pTab->zColAff = zColAff;
11927 }
11928 i = sqlite3Strlen30(zColAff);
11929 if( i ){
11930 if( iReg ){
11931 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
11932 }else{
11933 sqlite3VdbeChangeP4(v, -1, zColAff, i);
11934 }
11935 }
11936 }
11937
11938 /*
11939 ** Return non-zero if the table pTab in database iDb or any of its indices
11940 ** have been opened at any point in the VDBE program. This is used to see if
11941 ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
11942 ** run without using a temporary table for the results of the SELECT.
11943 */
11944 static int readsTable(Parse *p, int iDb, Table *pTab){
11945 Vdbe *v = sqlite3GetVdbe(p);
11946 int i;
11947 int iEnd = sqlite3VdbeCurrentAddr(v);
11948 #ifndef SQLITE_OMIT_VIRTUALTABLE
11949 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
11950 #endif
11951
11952 for(i=1; i<iEnd; i++){
11953 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
11954 assert( pOp!=0 );
11955 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
11956 Index *pIndex;
11957 int tnum = pOp->p2;
11958 if( tnum==pTab->tnum ){
11959 return 1;
11960 }
11961 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
11962 if( tnum==pIndex->tnum ){
11963 return 1;
11964 }
11965 }
11966 }
11967 #ifndef SQLITE_OMIT_VIRTUALTABLE
11968 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
11969 assert( pOp->p4.pVtab!=0 );
11970 assert( pOp->p4type==P4_VTAB );
11971 return 1;
11972 }
11973 #endif
11974 }
11975 return 0;
11976 }
11977
11978 #ifndef SQLITE_OMIT_AUTOINCREMENT
11979 /*
11980 ** Locate or create an AutoincInfo structure associated with table pTab
11981 ** which is in database iDb. Return the register number for the register
11982 ** that holds the maximum rowid.
11983 **
11984 ** There is at most one AutoincInfo structure per table even if the
11985 ** same table is autoincremented multiple times due to inserts within
11986 ** triggers. A new AutoincInfo structure is created if this is the
11987 ** first use of table pTab. On 2nd and subsequent uses, the original
11988 ** AutoincInfo structure is used.
11989 **
11990 ** Three memory locations are allocated:
11991 **
11992 ** (1) Register to hold the name of the pTab table.
11993 ** (2) Register to hold the maximum ROWID of pTab.
11994 ** (3) Register to hold the rowid in sqlite_sequence of pTab
11995 **
11996 ** The 2nd register is the one that is returned. That is all the
11997 ** insert routine needs to know about.
11998 */
11999 static int autoIncBegin(
12000 Parse *pParse, /* Parsing context */
12001 int iDb, /* Index of the database holding pTab */
12002 Table *pTab /* The table we are writing to */
12003 ){
12004 int memId = 0; /* Register holding maximum rowid */
12005 if( pTab->tabFlags & TF_Autoincrement ){
12006 Parse *pToplevel = sqlite3ParseToplevel(pParse);
12007 AutoincInfo *pInfo;
12008
12009 pInfo = pToplevel->pAinc;
12010 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
12011 if( pInfo==0 ){
12012 pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
12013 if( pInfo==0 ) return 0;
12014 pInfo->pNext = pToplevel->pAinc;
12015 pToplevel->pAinc = pInfo;
12016 pInfo->pTab = pTab;
12017 pInfo->iDb = iDb;
12018 pToplevel->nMem++; /* Register to hold name of table */
12019 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
12020 pToplevel->nMem++; /* Rowid in sqlite_sequence */
12021 }
12022 memId = pInfo->regCtr;
12023 }
12024 return memId;
12025 }
12026
12027 /*
12028 ** This routine generates code that will initialize all of the
12029 ** register used by the autoincrement tracker.
12030 */
12031 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
12032 AutoincInfo *p; /* Information about an AUTOINCREMENT */
12033 sqlite3 *db = pParse->db; /* The database connection */
12034 Db *pDb; /* Database only autoinc table */
12035 int memId; /* Register holding max rowid */
12036 int addr; /* A VDBE address */
12037 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
12038
12039 /* This routine is never called during trigger-generation. It is
12040 ** only called from the top-level */
12041 assert( pParse->pTriggerTab==0 );
12042 assert( sqlite3IsToplevel(pParse) );
12043
12044 assert( v ); /* We failed long ago if this is not so */
12045 for(p = pParse->pAinc; p; p = p->pNext){
12046 pDb = &db->aDb[p->iDb];
12047 memId = p->regCtr;
12048 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
12049 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
12050 sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
12051 addr = sqlite3VdbeCurrentAddr(v);
12052 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
12053 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v);
12054 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
12055 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v);
12056 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
12057 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
12058 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
12059 sqlite3VdbeGoto(v, addr+9);
12060 sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v);
12061 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
12062 sqlite3VdbeAddOp0(v, OP_Close);
12063 }
12064 }
12065
12066 /*
12067 ** Update the maximum rowid for an autoincrement calculation.
12068 **
12069 ** This routine should be called when the top of the stack holds a
12070 ** new rowid that is about to be inserted. If that new rowid is
12071 ** larger than the maximum rowid in the memId memory cell, then the
12072 ** memory cell is updated. The stack is unchanged.
12073 */
12074 static void autoIncStep(Parse *pParse, int memId, int regRowid){
12075 if( memId>0 ){
12076 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
12077 }
12078 }
12079
12080 /*
12081 ** This routine generates the code needed to write autoincrement
12082 ** maximum rowid values back into the sqlite_sequence register.
12083 ** Every statement that might do an INSERT into an autoincrement
12084 ** table (either directly or through triggers) needs to call this
12085 ** routine just before the "exit" code.
12086 */
12087 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
12088 AutoincInfo *p;
12089 Vdbe *v = pParse->pVdbe;
12090 sqlite3 *db = pParse->db;
12091
12092 assert( v );
12093 for(p = pParse->pAinc; p; p = p->pNext){
12094 Db *pDb = &db->aDb[p->iDb];
12095 int addr1;
12096 int iRec;
12097 int memId = p->regCtr;
12098
12099 iRec = sqlite3GetTempReg(pParse);
12100 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
12101 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
12102 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v);
12103 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
12104 sqlite3VdbeJumpHere(v, addr1);
12105 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
12106 sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
12107 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
12108 sqlite3VdbeAddOp0(v, OP_Close);
12109 sqlite3ReleaseTempReg(pParse, iRec);
12110 }
12111 }
12112 #else
12113 /*
12114 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
12115 ** above are all no-ops
12116 */
12117 # define autoIncBegin(A,B,C) (0)
12118 # define autoIncStep(A,B,C)
12119 #endif /* SQLITE_OMIT_AUTOINCREMENT */
12120
12121
12122 /* Forward declaration */
12123 static int xferOptimization(
12124 Parse *pParse, /* Parser context */
12125 Table *pDest, /* The table we are inserting into */
12126 Select *pSelect, /* A SELECT statement to use as the data source */
12127 int onError, /* How to handle constraint errors */
12128 int iDbDest /* The database of pDest */
12129 );
12130
12131 /*
12132 ** This routine is called to handle SQL of the following forms:
12133 **
12134 ** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
12135 ** insert into TABLE (IDLIST) select
12136 ** insert into TABLE (IDLIST) default values
12137 **
12138 ** The IDLIST following the table name is always optional. If omitted,
12139 ** then a list of all (non-hidden) columns for the table is substituted.
12140 ** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
12141 ** is omitted.
12142 **
12143 ** For the pSelect parameter holds the values to be inserted for the
12144 ** first two forms shown above. A VALUES clause is really just short-hand
12145 ** for a SELECT statement that omits the FROM clause and everything else
12146 ** that follows. If the pSelect parameter is NULL, that means that the
12147 ** DEFAULT VALUES form of the INSERT statement is intended.
12148 **
12149 ** The code generated follows one of four templates. For a simple
12150 ** insert with data coming from a single-row VALUES clause, the code executes
12151 ** once straight down through. Pseudo-code follows (we call this
12152 ** the "1st template"):
12153 **
12154 ** open write cursor to <table> and its indices
12155 ** put VALUES clause expressions into registers
12156 ** write the resulting record into <table>
12157 ** cleanup
12158 **
12159 ** The three remaining templates assume the statement is of the form
12160 **
12161 ** INSERT INTO <table> SELECT ...
12162 **
12163 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
12164 ** in other words if the SELECT pulls all columns from a single table
12165 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
12166 ** if <table2> and <table1> are distinct tables but have identical
12167 ** schemas, including all the same indices, then a special optimization
12168 ** is invoked that copies raw records from <table2> over to <table1>.
12169 ** See the xferOptimization() function for the implementation of this
12170 ** template. This is the 2nd template.
12171 **
12172 ** open a write cursor to <table>
12173 ** open read cursor on <table2>
12174 ** transfer all records in <table2> over to <table>
12175 ** close cursors
12176 ** foreach index on <table>
12177 ** open a write cursor on the <table> index
12178 ** open a read cursor on the corresponding <table2> index
12179 ** transfer all records from the read to the write cursors
12180 ** close cursors
12181 ** end foreach
12182 **
12183 ** The 3rd template is for when the second template does not apply
12184 ** and the SELECT clause does not read from <table> at any time.
12185 ** The generated code follows this template:
12186 **
12187 ** X <- A
12188 ** goto B
12189 ** A: setup for the SELECT
12190 ** loop over the rows in the SELECT
12191 ** load values into registers R..R+n
12192 ** yield X
12193 ** end loop
12194 ** cleanup after the SELECT
12195 ** end-coroutine X
12196 ** B: open write cursor to <table> and its indices
12197 ** C: yield X, at EOF goto D
12198 ** insert the select result into <table> from R..R+n
12199 ** goto C
12200 ** D: cleanup
12201 **
12202 ** The 4th template is used if the insert statement takes its
12203 ** values from a SELECT but the data is being inserted into a table
12204 ** that is also read as part of the SELECT. In the third form,
12205 ** we have to use an intermediate table to store the results of
12206 ** the select. The template is like this:
12207 **
12208 ** X <- A
12209 ** goto B
12210 ** A: setup for the SELECT
12211 ** loop over the tables in the SELECT
12212 ** load value into register R..R+n
12213 ** yield X
12214 ** end loop
12215 ** cleanup after the SELECT
12216 ** end co-routine R
12217 ** B: open temp table
12218 ** L: yield X, at EOF goto M
12219 ** insert row from R..R+n into temp table
12220 ** goto L
12221 ** M: open write cursor to <table> and its indices
12222 ** rewind temp table
12223 ** C: loop over rows of intermediate table
12224 ** transfer values form intermediate table into <table>
12225 ** end loop
12226 ** D: cleanup
12227 */
12228 SQLITE_PRIVATE void sqlite3Insert(
12229 Parse *pParse, /* Parser context */
12230 SrcList *pTabList, /* Name of table into which we are inserting */
12231 Select *pSelect, /* A SELECT statement to use as the data source */
12232 IdList *pColumn, /* Column names corresponding to IDLIST. */
12233 int onError /* How to handle constraint errors */
12234 ){
12235 sqlite3 *db; /* The main database structure */
12236 Table *pTab; /* The table to insert into. aka TABLE */
12237 char *zTab; /* Name of the table into which we are inserting */
12238 const char *zDb; /* Name of the database holding this table */
12239 int i, j, idx; /* Loop counters */
12240 Vdbe *v; /* Generate code into this virtual machine */
12241 Index *pIdx; /* For looping over indices of the table */
12242 int nColumn; /* Number of columns in the data */
12243 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
12244 int iDataCur = 0; /* VDBE cursor that is the main data repository */
12245 int iIdxCur = 0; /* First index cursor */
12246 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
12247 int endOfLoop; /* Label for the end of the insertion loop */
12248 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
12249 int addrInsTop = 0; /* Jump to label "D" */
12250 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
12251 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
12252 int iDb; /* Index of database holding TABLE */
12253 Db *pDb; /* The database containing table being inserted into */
12254 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
12255 u8 appendFlag = 0; /* True if the insert is likely to be an append */
12256 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
12257 u8 bIdListInOrder; /* True if IDLIST is in table order */
12258 ExprList *pList = 0; /* List of VALUES() to be inserted */
12259
12260 /* Register allocations */
12261 int regFromSelect = 0;/* Base register for data coming from SELECT */
12262 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
12263 int regRowCount = 0; /* Memory cell used for the row counter */
12264 int regIns; /* Block of regs holding rowid+data being inserted */
12265 int regRowid; /* registers holding insert rowid */
12266 int regData; /* register holding first column to insert */
12267 int *aRegIdx = 0; /* One register allocated to each index */
12268
12269 #ifndef SQLITE_OMIT_TRIGGER
12270 int isView; /* True if attempting to insert into a view */
12271 Trigger *pTrigger; /* List of triggers on pTab, if required */
12272 int tmask; /* Mask of trigger times */
12273 #endif
12274
12275 db = pParse->db;
12276 memset(&dest, 0, sizeof(dest));
12277 if( pParse->nErr || db->mallocFailed ){
12278 goto insert_cleanup;
12279 }
12280
12281 /* If the Select object is really just a simple VALUES() list with a
12282 ** single row (the common case) then keep that one row of values
12283 ** and discard the other (unused) parts of the pSelect object
12284 */
12285 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
12286 pList = pSelect->pEList;
12287 pSelect->pEList = 0;
12288 sqlite3SelectDelete(db, pSelect);
12289 pSelect = 0;
12290 }
12291
12292 /* Locate the table into which we will be inserting new information.
12293 */
12294 assert( pTabList->nSrc==1 );
12295 zTab = pTabList->a[0].zName;
12296 if( NEVER(zTab==0) ) goto insert_cleanup;
12297 pTab = sqlite3SrcListLookup(pParse, pTabList);
12298 if( pTab==0 ){
12299 goto insert_cleanup;
12300 }
12301 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
12302 assert( iDb<db->nDb );
12303 pDb = &db->aDb[iDb];
12304 zDb = pDb->zName;
12305 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
12306 goto insert_cleanup;
12307 }
12308 withoutRowid = !HasRowid(pTab);
12309
12310 /* Figure out if we have any triggers and if the table being
12311 ** inserted into is a view
12312 */
12313 #ifndef SQLITE_OMIT_TRIGGER
12314 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
12315 isView = pTab->pSelect!=0;
12316 #else
12317 # define pTrigger 0
12318 # define tmask 0
12319 # define isView 0
12320 #endif
12321 #ifdef SQLITE_OMIT_VIEW
12322 # undef isView
12323 # define isView 0
12324 #endif
12325 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
12326
12327 /* If pTab is really a view, make sure it has been initialized.
12328 ** ViewGetColumnNames() is a no-op if pTab is not a view.
12329 */
12330 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
12331 goto insert_cleanup;
12332 }
12333
12334 /* Cannot insert into a read-only table.
12335 */
12336 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
12337 goto insert_cleanup;
12338 }
12339
12340 /* Allocate a VDBE
12341 */
12342 v = sqlite3GetVdbe(pParse);
12343 if( v==0 ) goto insert_cleanup;
12344 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
12345 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
12346
12347 #ifndef SQLITE_OMIT_XFER_OPT
12348 /* If the statement is of the form
12349 **
12350 ** INSERT INTO <table1> SELECT * FROM <table2>;
12351 **
12352 ** Then special optimizations can be applied that make the transfer
12353 ** very fast and which reduce fragmentation of indices.
12354 **
12355 ** This is the 2nd template.
12356 */
12357 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
12358 assert( !pTrigger );
12359 assert( pList==0 );
12360 goto insert_end;
12361 }
12362 #endif /* SQLITE_OMIT_XFER_OPT */
12363
12364 /* If this is an AUTOINCREMENT table, look up the sequence number in the
12365 ** sqlite_sequence table and store it in memory cell regAutoinc.
12366 */
12367 regAutoinc = autoIncBegin(pParse, iDb, pTab);
12368
12369 /* Allocate registers for holding the rowid of the new row,
12370 ** the content of the new row, and the assembled row record.
12371 */
12372 regRowid = regIns = pParse->nMem+1;
12373 pParse->nMem += pTab->nCol + 1;
12374 if( IsVirtual(pTab) ){
12375 regRowid++;
12376 pParse->nMem++;
12377 }
12378 regData = regRowid+1;
12379
12380 /* If the INSERT statement included an IDLIST term, then make sure
12381 ** all elements of the IDLIST really are columns of the table and
12382 ** remember the column indices.
12383 **
12384 ** If the table has an INTEGER PRIMARY KEY column and that column
12385 ** is named in the IDLIST, then record in the ipkColumn variable
12386 ** the index into IDLIST of the primary key column. ipkColumn is
12387 ** the index of the primary key as it appears in IDLIST, not as
12388 ** is appears in the original table. (The index of the INTEGER
12389 ** PRIMARY KEY in the original table is pTab->iPKey.)
12390 */
12391 bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0;
12392 if( pColumn ){
12393 for(i=0; i<pColumn->nId; i++){
12394 pColumn->a[i].idx = -1;
12395 }
12396 for(i=0; i<pColumn->nId; i++){
12397 for(j=0; j<pTab->nCol; j++){
12398 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
12399 pColumn->a[i].idx = j;
12400 if( i!=j ) bIdListInOrder = 0;
12401 if( j==pTab->iPKey ){
12402 ipkColumn = i; assert( !withoutRowid );
12403 }
12404 break;
12405 }
12406 }
12407 if( j>=pTab->nCol ){
12408 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
12409 ipkColumn = i;
12410 bIdListInOrder = 0;
12411 }else{
12412 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
12413 pTabList, 0, pColumn->a[i].zName);
12414 pParse->checkSchema = 1;
12415 goto insert_cleanup;
12416 }
12417 }
12418 }
12419 }
12420
12421 /* Figure out how many columns of data are supplied. If the data
12422 ** is coming from a SELECT statement, then generate a co-routine that
12423 ** produces a single row of the SELECT on each invocation. The
12424 ** co-routine is the common header to the 3rd and 4th templates.
12425 */
12426 if( pSelect ){
12427 /* Data is coming from a SELECT or from a multi-row VALUES clause.
12428 ** Generate a co-routine to run the SELECT. */
12429 int regYield; /* Register holding co-routine entry-point */
12430 int addrTop; /* Top of the co-routine */
12431 int rc; /* Result code */
12432
12433 regYield = ++pParse->nMem;
12434 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
12435 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
12436 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
12437 dest.iSdst = bIdListInOrder ? regData : 0;
12438 dest.nSdst = pTab->nCol;
12439 rc = sqlite3Select(pParse, pSelect, &dest);
12440 regFromSelect = dest.iSdst;
12441 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
12442 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
12443 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
12444 assert( pSelect->pEList );
12445 nColumn = pSelect->pEList->nExpr;
12446
12447 /* Set useTempTable to TRUE if the result of the SELECT statement
12448 ** should be written into a temporary table (template 4). Set to
12449 ** FALSE if each output row of the SELECT can be written directly into
12450 ** the destination table (template 3).
12451 **
12452 ** A temp table must be used if the table being updated is also one
12453 ** of the tables being read by the SELECT statement. Also use a
12454 ** temp table in the case of row triggers.
12455 */
12456 if( pTrigger || readsTable(pParse, iDb, pTab) ){
12457 useTempTable = 1;
12458 }
12459
12460 if( useTempTable ){
12461 /* Invoke the coroutine to extract information from the SELECT
12462 ** and add it to a transient table srcTab. The code generated
12463 ** here is from the 4th template:
12464 **
12465 ** B: open temp table
12466 ** L: yield X, goto M at EOF
12467 ** insert row from R..R+n into temp table
12468 ** goto L
12469 ** M: ...
12470 */
12471 int regRec; /* Register to hold packed record */
12472 int regTempRowid; /* Register to hold temp table ROWID */
12473 int addrL; /* Label "L" */
12474
12475 srcTab = pParse->nTab++;
12476 regRec = sqlite3GetTempReg(pParse);
12477 regTempRowid = sqlite3GetTempReg(pParse);
12478 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
12479 addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
12480 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
12481 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
12482 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
12483 sqlite3VdbeGoto(v, addrL);
12484 sqlite3VdbeJumpHere(v, addrL);
12485 sqlite3ReleaseTempReg(pParse, regRec);
12486 sqlite3ReleaseTempReg(pParse, regTempRowid);
12487 }
12488 }else{
12489 /* This is the case if the data for the INSERT is coming from a
12490 ** single-row VALUES clause
12491 */
12492 NameContext sNC;
12493 memset(&sNC, 0, sizeof(sNC));
12494 sNC.pParse = pParse;
12495 srcTab = -1;
12496 assert( useTempTable==0 );
12497 if( pList ){
12498 nColumn = pList->nExpr;
12499 if( sqlite3ResolveExprListNames(&sNC, pList) ){
12500 goto insert_cleanup;
12501 }
12502 }else{
12503 nColumn = 0;
12504 }
12505 }
12506
12507 /* If there is no IDLIST term but the table has an integer primary
12508 ** key, the set the ipkColumn variable to the integer primary key
12509 ** column index in the original table definition.
12510 */
12511 if( pColumn==0 && nColumn>0 ){
12512 ipkColumn = pTab->iPKey;
12513 }
12514
12515 /* Make sure the number of columns in the source data matches the number
12516 ** of columns to be inserted into the table.
12517 */
12518 for(i=0; i<pTab->nCol; i++){
12519 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
12520 }
12521 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
12522 sqlite3ErrorMsg(pParse,
12523 "table %S has %d columns but %d values were supplied",
12524 pTabList, 0, pTab->nCol-nHidden, nColumn);
12525 goto insert_cleanup;
12526 }
12527 if( pColumn!=0 && nColumn!=pColumn->nId ){
12528 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
12529 goto insert_cleanup;
12530 }
12531
12532 /* Initialize the count of rows to be inserted
12533 */
12534 if( db->flags & SQLITE_CountRows ){
12535 regRowCount = ++pParse->nMem;
12536 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
12537 }
12538
12539 /* If this is not a view, open the table and and all indices */
12540 if( !isView ){
12541 int nIdx;
12542 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
12543 &iDataCur, &iIdxCur);
12544 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
12545 if( aRegIdx==0 ){
12546 goto insert_cleanup;
12547 }
12548 for(i=0; i<nIdx; i++){
12549 aRegIdx[i] = ++pParse->nMem;
12550 }
12551 }
12552
12553 /* This is the top of the main insertion loop */
12554 if( useTempTable ){
12555 /* This block codes the top of loop only. The complete loop is the
12556 ** following pseudocode (template 4):
12557 **
12558 ** rewind temp table, if empty goto D
12559 ** C: loop over rows of intermediate table
12560 ** transfer values form intermediate table into <table>
12561 ** end loop
12562 ** D: ...
12563 */
12564 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
12565 addrCont = sqlite3VdbeCurrentAddr(v);
12566 }else if( pSelect ){
12567 /* This block codes the top of loop only. The complete loop is the
12568 ** following pseudocode (template 3):
12569 **
12570 ** C: yield X, at EOF goto D
12571 ** insert the select result into <table> from R..R+n
12572 ** goto C
12573 ** D: ...
12574 */
12575 addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
12576 VdbeCoverage(v);
12577 }
12578
12579 /* Run the BEFORE and INSTEAD OF triggers, if there are any
12580 */
12581 endOfLoop = sqlite3VdbeMakeLabel(v);
12582 if( tmask & TRIGGER_BEFORE ){
12583 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
12584
12585 /* build the NEW.* reference row. Note that if there is an INTEGER
12586 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
12587 ** translated into a unique ID for the row. But on a BEFORE trigger,
12588 ** we do not know what the unique ID will be (because the insert has
12589 ** not happened yet) so we substitute a rowid of -1
12590 */
12591 if( ipkColumn<0 ){
12592 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
12593 }else{
12594 int addr1;
12595 assert( !withoutRowid );
12596 if( useTempTable ){
12597 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
12598 }else{
12599 assert( pSelect==0 ); /* Otherwise useTempTable is true */
12600 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
12601 }
12602 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
12603 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
12604 sqlite3VdbeJumpHere(v, addr1);
12605 sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
12606 }
12607
12608 /* Cannot have triggers on a virtual table. If it were possible,
12609 ** this block would have to account for hidden column.
12610 */
12611 assert( !IsVirtual(pTab) );
12612
12613 /* Create the new column data
12614 */
12615 for(i=j=0; i<pTab->nCol; i++){
12616 if( pColumn ){
12617 for(j=0; j<pColumn->nId; j++){
12618 if( pColumn->a[j].idx==i ) break;
12619 }
12620 }
12621 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
12622 || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
12623 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
12624 }else if( useTempTable ){
12625 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
12626 }else{
12627 assert( pSelect==0 ); /* Otherwise useTempTable is true */
12628 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
12629 }
12630 if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
12631 }
12632
12633 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
12634 ** do not attempt any conversions before assembling the record.
12635 ** If this is a real table, attempt conversions as required by the
12636 ** table column affinities.
12637 */
12638 if( !isView ){
12639 sqlite3TableAffinity(v, pTab, regCols+1);
12640 }
12641
12642 /* Fire BEFORE or INSTEAD OF triggers */
12643 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
12644 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
12645
12646 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
12647 }
12648
12649 /* Compute the content of the next row to insert into a range of
12650 ** registers beginning at regIns.
12651 */
12652 if( !isView ){
12653 if( IsVirtual(pTab) ){
12654 /* The row that the VUpdate opcode will delete: none */
12655 sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
12656 }
12657 if( ipkColumn>=0 ){
12658 if( useTempTable ){
12659 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
12660 }else if( pSelect ){
12661 sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
12662 }else{
12663 VdbeOp *pOp;
12664 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
12665 pOp = sqlite3VdbeGetOp(v, -1);
12666 if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
12667 appendFlag = 1;
12668 pOp->opcode = OP_NewRowid;
12669 pOp->p1 = iDataCur;
12670 pOp->p2 = regRowid;
12671 pOp->p3 = regAutoinc;
12672 }
12673 }
12674 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
12675 ** to generate a unique primary key value.
12676 */
12677 if( !appendFlag ){
12678 int addr1;
12679 if( !IsVirtual(pTab) ){
12680 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
12681 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
12682 sqlite3VdbeJumpHere(v, addr1);
12683 }else{
12684 addr1 = sqlite3VdbeCurrentAddr(v);
12685 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
12686 }
12687 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
12688 }
12689 }else if( IsVirtual(pTab) || withoutRowid ){
12690 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
12691 }else{
12692 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
12693 appendFlag = 1;
12694 }
12695 autoIncStep(pParse, regAutoinc, regRowid);
12696
12697 /* Compute data for all columns of the new entry, beginning
12698 ** with the first column.
12699 */
12700 nHidden = 0;
12701 for(i=0; i<pTab->nCol; i++){
12702 int iRegStore = regRowid+1+i;
12703 if( i==pTab->iPKey ){
12704 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
12705 ** Whenever this column is read, the rowid will be substituted
12706 ** in its place. Hence, fill this column with a NULL to avoid
12707 ** taking up data space with information that will never be used.
12708 ** As there may be shallow copies of this value, make it a soft-NULL */
12709 sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
12710 continue;
12711 }
12712 if( pColumn==0 ){
12713 if( IsHiddenColumn(&pTab->aCol[i]) ){
12714 j = -1;
12715 nHidden++;
12716 }else{
12717 j = i - nHidden;
12718 }
12719 }else{
12720 for(j=0; j<pColumn->nId; j++){
12721 if( pColumn->a[j].idx==i ) break;
12722 }
12723 }
12724 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
12725 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
12726 }else if( useTempTable ){
12727 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
12728 }else if( pSelect ){
12729 if( regFromSelect!=regData ){
12730 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
12731 }
12732 }else{
12733 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
12734 }
12735 }
12736
12737 /* Generate code to check constraints and generate index keys and
12738 ** do the insertion.
12739 */
12740 #ifndef SQLITE_OMIT_VIRTUALTABLE
12741 if( IsVirtual(pTab) ){
12742 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
12743 sqlite3VtabMakeWritable(pParse, pTab);
12744 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
12745 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
12746 sqlite3MayAbort(pParse);
12747 }else
12748 #endif
12749 {
12750 int isReplace; /* Set to true if constraints may cause a replace */
12751 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
12752 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace
12753 );
12754 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
12755 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
12756 regIns, aRegIdx, 0, appendFlag, isReplace==0);
12757 }
12758 }
12759
12760 /* Update the count of rows that are inserted
12761 */
12762 if( (db->flags & SQLITE_CountRows)!=0 ){
12763 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
12764 }
12765
12766 if( pTrigger ){
12767 /* Code AFTER triggers */
12768 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
12769 pTab, regData-2-pTab->nCol, onError, endOfLoop);
12770 }
12771
12772 /* The bottom of the main insertion loop, if the data source
12773 ** is a SELECT statement.
12774 */
12775 sqlite3VdbeResolveLabel(v, endOfLoop);
12776 if( useTempTable ){
12777 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
12778 sqlite3VdbeJumpHere(v, addrInsTop);
12779 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
12780 }else if( pSelect ){
12781 sqlite3VdbeGoto(v, addrCont);
12782 sqlite3VdbeJumpHere(v, addrInsTop);
12783 }
12784
12785 if( !IsVirtual(pTab) && !isView ){
12786 /* Close all tables opened */
12787 if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
12788 for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
12789 sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur);
12790 }
12791 }
12792
12793 insert_end:
12794 /* Update the sqlite_sequence table by storing the content of the
12795 ** maximum rowid counter values recorded while inserting into
12796 ** autoincrement tables.
12797 */
12798 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
12799 sqlite3AutoincrementEnd(pParse);
12800 }
12801
12802 /*
12803 ** Return the number of rows inserted. If this routine is
12804 ** generating code because of a call to sqlite3NestedParse(), do not
12805 ** invoke the callback function.
12806 */
12807 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
12808 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
12809 sqlite3VdbeSetNumCols(v, 1);
12810 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
12811 }
12812
12813 insert_cleanup:
12814 sqlite3SrcListDelete(db, pTabList);
12815 sqlite3ExprListDelete(db, pList);
12816 sqlite3SelectDelete(db, pSelect);
12817 sqlite3IdListDelete(db, pColumn);
12818 sqlite3DbFree(db, aRegIdx);
12819 }
12820
12821 /* Make sure "isView" and other macros defined above are undefined. Otherwise
12822 ** they may interfere with compilation of other functions in this file
12823 ** (or in another file, if this file becomes part of the amalgamation). */
12824 #ifdef isView
12825 #undef isView
12826 #endif
12827 #ifdef pTrigger
12828 #undef pTrigger
12829 #endif
12830 #ifdef tmask
12831 #undef tmask
12832 #endif
12833
12834 /*
12835 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
12836 ** on table pTab.
12837 **
12838 ** The regNewData parameter is the first register in a range that contains
12839 ** the data to be inserted or the data after the update. There will be
12840 ** pTab->nCol+1 registers in this range. The first register (the one
12841 ** that regNewData points to) will contain the new rowid, or NULL in the
12842 ** case of a WITHOUT ROWID table. The second register in the range will
12843 ** contain the content of the first table column. The third register will
12844 ** contain the content of the second table column. And so forth.
12845 **
12846 ** The regOldData parameter is similar to regNewData except that it contains
12847 ** the data prior to an UPDATE rather than afterwards. regOldData is zero
12848 ** for an INSERT. This routine can distinguish between UPDATE and INSERT by
12849 ** checking regOldData for zero.
12850 **
12851 ** For an UPDATE, the pkChng boolean is true if the true primary key (the
12852 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
12853 ** might be modified by the UPDATE. If pkChng is false, then the key of
12854 ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
12855 **
12856 ** For an INSERT, the pkChng boolean indicates whether or not the rowid
12857 ** was explicitly specified as part of the INSERT statement. If pkChng
12858 ** is zero, it means that the either rowid is computed automatically or
12859 ** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
12860 ** pkChng will only be true if the INSERT statement provides an integer
12861 ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
12862 **
12863 ** The code generated by this routine will store new index entries into
12864 ** registers identified by aRegIdx[]. No index entry is created for
12865 ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
12866 ** the same as the order of indices on the linked list of indices
12867 ** at pTab->pIndex.
12868 **
12869 ** The caller must have already opened writeable cursors on the main
12870 ** table and all applicable indices (that is to say, all indices for which
12871 ** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
12872 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
12873 ** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
12874 ** for the first index in the pTab->pIndex list. Cursors for other indices
12875 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
12876 **
12877 ** This routine also generates code to check constraints. NOT NULL,
12878 ** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
12879 ** then the appropriate action is performed. There are five possible
12880 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
12881 **
12882 ** Constraint type Action What Happens
12883 ** --------------- ---------- ----------------------------------------
12884 ** any ROLLBACK The current transaction is rolled back and
12885 ** sqlite3_step() returns immediately with a
12886 ** return code of SQLITE_CONSTRAINT.
12887 **
12888 ** any ABORT Back out changes from the current command
12889 ** only (do not do a complete rollback) then
12890 ** cause sqlite3_step() to return immediately
12891 ** with SQLITE_CONSTRAINT.
12892 **
12893 ** any FAIL Sqlite3_step() returns immediately with a
12894 ** return code of SQLITE_CONSTRAINT. The
12895 ** transaction is not rolled back and any
12896 ** changes to prior rows are retained.
12897 **
12898 ** any IGNORE The attempt in insert or update the current
12899 ** row is skipped, without throwing an error.
12900 ** Processing continues with the next row.
12901 ** (There is an immediate jump to ignoreDest.)
12902 **
12903 ** NOT NULL REPLACE The NULL value is replace by the default
12904 ** value for that column. If the default value
12905 ** is NULL, the action is the same as ABORT.
12906 **
12907 ** UNIQUE REPLACE The other row that conflicts with the row
12908 ** being inserted is removed.
12909 **
12910 ** CHECK REPLACE Illegal. The results in an exception.
12911 **
12912 ** Which action to take is determined by the overrideError parameter.
12913 ** Or if overrideError==OE_Default, then the pParse->onError parameter
12914 ** is used. Or if pParse->onError==OE_Default then the onError value
12915 ** for the constraint is used.
12916 */
12917 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
12918 Parse *pParse, /* The parser context */
12919 Table *pTab, /* The table being inserted or updated */
12920 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
12921 int iDataCur, /* Canonical data cursor (main table or PK index) */
12922 int iIdxCur, /* First index cursor */
12923 int regNewData, /* First register in a range holding values to insert */
12924 int regOldData, /* Previous content. 0 for INSERTs */
12925 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
12926 u8 overrideError, /* Override onError to this if not OE_Default */
12927 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
12928 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */
12929 ){
12930 Vdbe *v; /* VDBE under constrution */
12931 Index *pIdx; /* Pointer to one of the indices */
12932 Index *pPk = 0; /* The PRIMARY KEY index */
12933 sqlite3 *db; /* Database connection */
12934 int i; /* loop counter */
12935 int ix; /* Index loop counter */
12936 int nCol; /* Number of columns */
12937 int onError; /* Conflict resolution strategy */
12938 int addr1; /* Address of jump instruction */
12939 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
12940 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
12941 int ipkTop = 0; /* Top of the rowid change constraint check */
12942 int ipkBottom = 0; /* Bottom of the rowid change constraint check */
12943 u8 isUpdate; /* True if this is an UPDATE operation */
12944 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
12945 int regRowid = -1; /* Register holding ROWID value */
12946
12947 isUpdate = regOldData!=0;
12948 db = pParse->db;
12949 v = sqlite3GetVdbe(pParse);
12950 assert( v!=0 );
12951 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
12952 nCol = pTab->nCol;
12953
12954 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
12955 ** normal rowid tables. nPkField is the number of key fields in the
12956 ** pPk index or 1 for a rowid table. In other words, nPkField is the
12957 ** number of fields in the true primary key of the table. */
12958 if( HasRowid(pTab) ){
12959 pPk = 0;
12960 nPkField = 1;
12961 }else{
12962 pPk = sqlite3PrimaryKeyIndex(pTab);
12963 nPkField = pPk->nKeyCol;
12964 }
12965
12966 /* Record that this module has started */
12967 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
12968 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
12969
12970 /* Test all NOT NULL constraints.
12971 */
12972 for(i=0; i<nCol; i++){
12973 if( i==pTab->iPKey ){
12974 continue;
12975 }
12976 onError = pTab->aCol[i].notNull;
12977 if( onError==OE_None ) continue;
12978 if( overrideError!=OE_Default ){
12979 onError = overrideError;
12980 }else if( onError==OE_Default ){
12981 onError = OE_Abort;
12982 }
12983 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
12984 onError = OE_Abort;
12985 }
12986 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
12987 || onError==OE_Ignore || onError==OE_Replace );
12988 switch( onError ){
12989 case OE_Abort:
12990 sqlite3MayAbort(pParse);
12991 /* Fall through */
12992 case OE_Rollback:
12993 case OE_Fail: {
12994 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
12995 pTab->aCol[i].zName);
12996 sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
12997 regNewData+1+i, zMsg, P4_DYNAMIC);
12998 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
12999 VdbeCoverage(v);
13000 break;
13001 }
13002 case OE_Ignore: {
13003 sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
13004 VdbeCoverage(v);
13005 break;
13006 }
13007 default: {
13008 assert( onError==OE_Replace );
13009 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i);
13010 VdbeCoverage(v);
13011 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
13012 sqlite3VdbeJumpHere(v, addr1);
13013 break;
13014 }
13015 }
13016 }
13017
13018 /* Test all CHECK constraints
13019 */
13020 #ifndef SQLITE_OMIT_CHECK
13021 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
13022 ExprList *pCheck = pTab->pCheck;
13023 pParse->ckBase = regNewData+1;
13024 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
13025 for(i=0; i<pCheck->nExpr; i++){
13026 int allOk = sqlite3VdbeMakeLabel(v);
13027 sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
13028 if( onError==OE_Ignore ){
13029 sqlite3VdbeGoto(v, ignoreDest);
13030 }else{
13031 char *zName = pCheck->a[i].zName;
13032 if( zName==0 ) zName = pTab->zName;
13033 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
13034 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
13035 onError, zName, P4_TRANSIENT,
13036 P5_ConstraintCheck);
13037 }
13038 sqlite3VdbeResolveLabel(v, allOk);
13039 }
13040 }
13041 #endif /* !defined(SQLITE_OMIT_CHECK) */
13042
13043 /* If rowid is changing, make sure the new rowid does not previously
13044 ** exist in the table.
13045 */
13046 if( pkChng && pPk==0 ){
13047 int addrRowidOk = sqlite3VdbeMakeLabel(v);
13048
13049 /* Figure out what action to take in case of a rowid collision */
13050 onError = pTab->keyConf;
13051 if( overrideError!=OE_Default ){
13052 onError = overrideError;
13053 }else if( onError==OE_Default ){
13054 onError = OE_Abort;
13055 }
13056
13057 if( isUpdate ){
13058 /* pkChng!=0 does not mean that the rowid has change, only that
13059 ** it might have changed. Skip the conflict logic below if the rowid
13060 ** is unchanged. */
13061 sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
13062 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
13063 VdbeCoverage(v);
13064 }
13065
13066 /* If the response to a rowid conflict is REPLACE but the response
13067 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
13068 ** to defer the running of the rowid conflict checking until after
13069 ** the UNIQUE constraints have run.
13070 */
13071 if( onError==OE_Replace && overrideError!=OE_Replace ){
13072 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
13073 if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
13074 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
13075 break;
13076 }
13077 }
13078 }
13079
13080 /* Check to see if the new rowid already exists in the table. Skip
13081 ** the following conflict logic if it does not. */
13082 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
13083 VdbeCoverage(v);
13084
13085 /* Generate code that deals with a rowid collision */
13086 switch( onError ){
13087 default: {
13088 onError = OE_Abort;
13089 /* Fall thru into the next case */
13090 }
13091 case OE_Rollback:
13092 case OE_Abort:
13093 case OE_Fail: {
13094 sqlite3RowidConstraint(pParse, onError, pTab);
13095 break;
13096 }
13097 case OE_Replace: {
13098 /* If there are DELETE triggers on this table and the
13099 ** recursive-triggers flag is set, call GenerateRowDelete() to
13100 ** remove the conflicting row from the table. This will fire
13101 ** the triggers and remove both the table and index b-tree entries.
13102 **
13103 ** Otherwise, if there are no triggers or the recursive-triggers
13104 ** flag is not set, but the table has one or more indexes, call
13105 ** GenerateRowIndexDelete(). This removes the index b-tree entries
13106 ** only. The table b-tree entry will be replaced by the new entry
13107 ** when it is inserted.
13108 **
13109 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
13110 ** also invoke MultiWrite() to indicate that this VDBE may require
13111 ** statement rollback (if the statement is aborted after the delete
13112 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
13113 ** but being more selective here allows statements like:
13114 **
13115 ** REPLACE INTO t(rowid) VALUES($newrowid)
13116 **
13117 ** to run without a statement journal if there are no indexes on the
13118 ** table.
13119 */
13120 Trigger *pTrigger = 0;
13121 if( db->flags&SQLITE_RecTriggers ){
13122 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
13123 }
13124 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
13125 sqlite3MultiWrite(pParse);
13126 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
13127 regNewData, 1, 0, OE_Replace,
13128 ONEPASS_SINGLE, -1);
13129 }else{
13130 if( pTab->pIndex ){
13131 sqlite3MultiWrite(pParse);
13132 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
13133 }
13134 }
13135 seenReplace = 1;
13136 break;
13137 }
13138 case OE_Ignore: {
13139 /*assert( seenReplace==0 );*/
13140 sqlite3VdbeGoto(v, ignoreDest);
13141 break;
13142 }
13143 }
13144 sqlite3VdbeResolveLabel(v, addrRowidOk);
13145 if( ipkTop ){
13146 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
13147 sqlite3VdbeJumpHere(v, ipkTop);
13148 }
13149 }
13150
13151 /* Test all UNIQUE constraints by creating entries for each UNIQUE
13152 ** index and making sure that duplicate entries do not already exist.
13153 ** Compute the revised record entries for indices as we go.
13154 **
13155 ** This loop also handles the case of the PRIMARY KEY index for a
13156 ** WITHOUT ROWID table.
13157 */
13158 for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
13159 int regIdx; /* Range of registers hold conent for pIdx */
13160 int regR; /* Range of registers holding conflicting PK */
13161 int iThisCur; /* Cursor for this UNIQUE index */
13162 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
13163
13164 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
13165 if( bAffinityDone==0 ){
13166 sqlite3TableAffinity(v, pTab, regNewData+1);
13167 bAffinityDone = 1;
13168 }
13169 iThisCur = iIdxCur+ix;
13170 addrUniqueOk = sqlite3VdbeMakeLabel(v);
13171
13172 /* Skip partial indices for which the WHERE clause is not true */
13173 if( pIdx->pPartIdxWhere ){
13174 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
13175 pParse->ckBase = regNewData+1;
13176 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
13177 SQLITE_JUMPIFNULL);
13178 pParse->ckBase = 0;
13179 }
13180
13181 /* Create a record for this index entry as it should appear after
13182 ** the insert or update. Store that record in the aRegIdx[ix] register
13183 */
13184 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
13185 for(i=0; i<pIdx->nColumn; i++){
13186 int iField = pIdx->aiColumn[i];
13187 int x;
13188 if( iField==XN_EXPR ){
13189 pParse->ckBase = regNewData+1;
13190 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
13191 pParse->ckBase = 0;
13192 VdbeComment((v, "%s column %d", pIdx->zName, i));
13193 }else{
13194 if( iField==XN_ROWID || iField==pTab->iPKey ){
13195 if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */
13196 x = regNewData;
13197 regRowid = pIdx->pPartIdxWhere ? -1 : regIdx+i;
13198 }else{
13199 x = iField + regNewData + 1;
13200 }
13201 sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
13202 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
13203 }
13204 }
13205 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
13206 VdbeComment((v, "for %s", pIdx->zName));
13207 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
13208
13209 /* In an UPDATE operation, if this index is the PRIMARY KEY index
13210 ** of a WITHOUT ROWID table and there has been no change the
13211 ** primary key, then no collision is possible. The collision detection
13212 ** logic below can all be skipped. */
13213 if( isUpdate && pPk==pIdx && pkChng==0 ){
13214 sqlite3VdbeResolveLabel(v, addrUniqueOk);
13215 continue;
13216 }
13217
13218 /* Find out what action to take in case there is a uniqueness conflict */
13219 onError = pIdx->onError;
13220 if( onError==OE_None ){
13221 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
13222 sqlite3VdbeResolveLabel(v, addrUniqueOk);
13223 continue; /* pIdx is not a UNIQUE index */
13224 }
13225 if( overrideError!=OE_Default ){
13226 onError = overrideError;
13227 }else if( onError==OE_Default ){
13228 onError = OE_Abort;
13229 }
13230
13231 /* Check to see if the new index entry will be unique */
13232 sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
13233 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
13234
13235 /* Generate code to handle collisions */
13236 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
13237 if( isUpdate || onError==OE_Replace ){
13238 if( HasRowid(pTab) ){
13239 sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
13240 /* Conflict only if the rowid of the existing index entry
13241 ** is different from old-rowid */
13242 if( isUpdate ){
13243 sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
13244 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
13245 VdbeCoverage(v);
13246 }
13247 }else{
13248 int x;
13249 /* Extract the PRIMARY KEY from the end of the index entry and
13250 ** store it in registers regR..regR+nPk-1 */
13251 if( pIdx!=pPk ){
13252 for(i=0; i<pPk->nKeyCol; i++){
13253 assert( pPk->aiColumn[i]>=0 );
13254 x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
13255 sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
13256 VdbeComment((v, "%s.%s", pTab->zName,
13257 pTab->aCol[pPk->aiColumn[i]].zName));
13258 }
13259 }
13260 if( isUpdate ){
13261 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
13262 ** table, only conflict if the new PRIMARY KEY values are actually
13263 ** different from the old.
13264 **
13265 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
13266 ** of the matched index row are different from the original PRIMARY
13267 ** KEY values of this row before the update. */
13268 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
13269 int op = OP_Ne;
13270 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
13271
13272 for(i=0; i<pPk->nKeyCol; i++){
13273 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
13274 x = pPk->aiColumn[i];
13275 assert( x>=0 );
13276 if( i==(pPk->nKeyCol-1) ){
13277 addrJump = addrUniqueOk;
13278 op = OP_Eq;
13279 }
13280 sqlite3VdbeAddOp4(v, op,
13281 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
13282 );
13283 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
13284 VdbeCoverageIf(v, op==OP_Eq);
13285 VdbeCoverageIf(v, op==OP_Ne);
13286 }
13287 }
13288 }
13289 }
13290
13291 /* Generate code that executes if the new index entry is not unique */
13292 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
13293 || onError==OE_Ignore || onError==OE_Replace );
13294 switch( onError ){
13295 case OE_Rollback:
13296 case OE_Abort:
13297 case OE_Fail: {
13298 sqlite3UniqueConstraint(pParse, onError, pIdx);
13299 break;
13300 }
13301 case OE_Ignore: {
13302 sqlite3VdbeGoto(v, ignoreDest);
13303 break;
13304 }
13305 default: {
13306 Trigger *pTrigger = 0;
13307 assert( onError==OE_Replace );
13308 sqlite3MultiWrite(pParse);
13309 if( db->flags&SQLITE_RecTriggers ){
13310 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
13311 }
13312 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
13313 regR, nPkField, 0, OE_Replace,
13314 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1);
13315 seenReplace = 1;
13316 break;
13317 }
13318 }
13319 sqlite3VdbeResolveLabel(v, addrUniqueOk);
13320 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
13321 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
13322 }
13323 if( ipkTop ){
13324 sqlite3VdbeGoto(v, ipkTop+1);
13325 sqlite3VdbeJumpHere(v, ipkBottom);
13326 }
13327
13328 *pbMayReplace = seenReplace;
13329 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
13330 }
13331
13332 /*
13333 ** This routine generates code to finish the INSERT or UPDATE operation
13334 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
13335 ** A consecutive range of registers starting at regNewData contains the
13336 ** rowid and the content to be inserted.
13337 **
13338 ** The arguments to this routine should be the same as the first six
13339 ** arguments to sqlite3GenerateConstraintChecks.
13340 */
13341 SQLITE_PRIVATE void sqlite3CompleteInsertion(
13342 Parse *pParse, /* The parser context */
13343 Table *pTab, /* the table into which we are inserting */
13344 int iDataCur, /* Cursor of the canonical data source */
13345 int iIdxCur, /* First index cursor */
13346 int regNewData, /* Range of content */
13347 int *aRegIdx, /* Register used by each index. 0 for unused indices */
13348 int isUpdate, /* True for UPDATE, False for INSERT */
13349 int appendBias, /* True if this is likely to be an append */
13350 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
13351 ){
13352 Vdbe *v; /* Prepared statements under construction */
13353 Index *pIdx; /* An index being inserted or updated */
13354 u8 pik_flags; /* flag values passed to the btree insert */
13355 int regData; /* Content registers (after the rowid) */
13356 int regRec; /* Register holding assembled record for the table */
13357 int i; /* Loop counter */
13358 u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
13359
13360 v = sqlite3GetVdbe(pParse);
13361 assert( v!=0 );
13362 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
13363 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
13364 if( aRegIdx[i]==0 ) continue;
13365 bAffinityDone = 1;
13366 if( pIdx->pPartIdxWhere ){
13367 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
13368 VdbeCoverage(v);
13369 }
13370 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
13371 pik_flags = 0;
13372 if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
13373 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
13374 assert( pParse->nested==0 );
13375 pik_flags |= OPFLAG_NCHANGE;
13376 }
13377 if( pik_flags ) sqlite3VdbeChangeP5(v, pik_flags);
13378 }
13379 if( !HasRowid(pTab) ) return;
13380 regData = regNewData + 1;
13381 regRec = sqlite3GetTempReg(pParse);
13382 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
13383 if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0);
13384 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
13385 if( pParse->nested ){
13386 pik_flags = 0;
13387 }else{
13388 pik_flags = OPFLAG_NCHANGE;
13389 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
13390 }
13391 if( appendBias ){
13392 pik_flags |= OPFLAG_APPEND;
13393 }
13394 if( useSeekResult ){
13395 pik_flags |= OPFLAG_USESEEKRESULT;
13396 }
13397 sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
13398 if( !pParse->nested ){
13399 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
13400 }
13401 sqlite3VdbeChangeP5(v, pik_flags);
13402 }
13403
13404 /*
13405 ** Allocate cursors for the pTab table and all its indices and generate
13406 ** code to open and initialized those cursors.
13407 **
13408 ** The cursor for the object that contains the complete data (normally
13409 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
13410 ** ROWID table) is returned in *piDataCur. The first index cursor is
13411 ** returned in *piIdxCur. The number of indices is returned.
13412 **
13413 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
13414 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
13415 ** If iBase is negative, then allocate the next available cursor.
13416 **
13417 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
13418 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
13419 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
13420 ** pTab->pIndex list.
13421 **
13422 ** If pTab is a virtual table, then this routine is a no-op and the
13423 ** *piDataCur and *piIdxCur values are left uninitialized.
13424 */
13425 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
13426 Parse *pParse, /* Parsing context */
13427 Table *pTab, /* Table to be opened */
13428 int op, /* OP_OpenRead or OP_OpenWrite */
13429 u8 p5, /* P5 value for OP_Open* instructions */
13430 int iBase, /* Use this for the table cursor, if there is one */
13431 u8 *aToOpen, /* If not NULL: boolean for each table and index */
13432 int *piDataCur, /* Write the database source cursor number here */
13433 int *piIdxCur /* Write the first index cursor number here */
13434 ){
13435 int i;
13436 int iDb;
13437 int iDataCur;
13438 Index *pIdx;
13439 Vdbe *v;
13440
13441 assert( op==OP_OpenRead || op==OP_OpenWrite );
13442 assert( op==OP_OpenWrite || p5==0 );
13443 if( IsVirtual(pTab) ){
13444 /* This routine is a no-op for virtual tables. Leave the output
13445 ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
13446 ** can detect if they are used by mistake in the caller. */
13447 return 0;
13448 }
13449 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
13450 v = sqlite3GetVdbe(pParse);
13451 assert( v!=0 );
13452 if( iBase<0 ) iBase = pParse->nTab;
13453 iDataCur = iBase++;
13454 if( piDataCur ) *piDataCur = iDataCur;
13455 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
13456 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
13457 }else{
13458 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
13459 }
13460 if( piIdxCur ) *piIdxCur = iBase;
13461 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
13462 int iIdxCur = iBase++;
13463 assert( pIdx->pSchema==pTab->pSchema );
13464 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){
13465 *piDataCur = iIdxCur;
13466 }
13467 if( aToOpen==0 || aToOpen[i+1] ){
13468 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
13469 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
13470 sqlite3VdbeChangeP5(v, p5);
13471 VdbeComment((v, "%s", pIdx->zName));
13472 }
13473 }
13474 if( iBase>pParse->nTab ) pParse->nTab = iBase;
13475 return i;
13476 }
13477
13478
13479 #ifdef SQLITE_TEST
13480 /*
13481 ** The following global variable is incremented whenever the
13482 ** transfer optimization is used. This is used for testing
13483 ** purposes only - to make sure the transfer optimization really
13484 ** is happening when it is supposed to.
13485 */
13486 SQLITE_API int sqlite3_xferopt_count;
13487 #endif /* SQLITE_TEST */
13488
13489
13490 #ifndef SQLITE_OMIT_XFER_OPT
13491 /*
13492 ** Check to see if index pSrc is compatible as a source of data
13493 ** for index pDest in an insert transfer optimization. The rules
13494 ** for a compatible index:
13495 **
13496 ** * The index is over the same set of columns
13497 ** * The same DESC and ASC markings occurs on all columns
13498 ** * The same onError processing (OE_Abort, OE_Ignore, etc)
13499 ** * The same collating sequence on each column
13500 ** * The index has the exact same WHERE clause
13501 */
13502 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
13503 int i;
13504 assert( pDest && pSrc );
13505 assert( pDest->pTable!=pSrc->pTable );
13506 if( pDest->nKeyCol!=pSrc->nKeyCol ){
13507 return 0; /* Different number of columns */
13508 }
13509 if( pDest->onError!=pSrc->onError ){
13510 return 0; /* Different conflict resolution strategies */
13511 }
13512 for(i=0; i<pSrc->nKeyCol; i++){
13513 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
13514 return 0; /* Different columns indexed */
13515 }
13516 if( pSrc->aiColumn[i]==XN_EXPR ){
13517 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
13518 if( sqlite3ExprCompare(pSrc->aColExpr->a[i].pExpr,
13519 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
13520 return 0; /* Different expressions in the index */
13521 }
13522 }
13523 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
13524 return 0; /* Different sort orders */
13525 }
13526 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
13527 return 0; /* Different collating sequences */
13528 }
13529 }
13530 if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
13531 return 0; /* Different WHERE clauses */
13532 }
13533
13534 /* If no test above fails then the indices must be compatible */
13535 return 1;
13536 }
13537
13538 /*
13539 ** Attempt the transfer optimization on INSERTs of the form
13540 **
13541 ** INSERT INTO tab1 SELECT * FROM tab2;
13542 **
13543 ** The xfer optimization transfers raw records from tab2 over to tab1.
13544 ** Columns are not decoded and reassembled, which greatly improves
13545 ** performance. Raw index records are transferred in the same way.
13546 **
13547 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
13548 ** There are lots of rules for determining compatibility - see comments
13549 ** embedded in the code for details.
13550 **
13551 ** This routine returns TRUE if the optimization is guaranteed to be used.
13552 ** Sometimes the xfer optimization will only work if the destination table
13553 ** is empty - a factor that can only be determined at run-time. In that
13554 ** case, this routine generates code for the xfer optimization but also
13555 ** does a test to see if the destination table is empty and jumps over the
13556 ** xfer optimization code if the test fails. In that case, this routine
13557 ** returns FALSE so that the caller will know to go ahead and generate
13558 ** an unoptimized transfer. This routine also returns FALSE if there
13559 ** is no chance that the xfer optimization can be applied.
13560 **
13561 ** This optimization is particularly useful at making VACUUM run faster.
13562 */
13563 static int xferOptimization(
13564 Parse *pParse, /* Parser context */
13565 Table *pDest, /* The table we are inserting into */
13566 Select *pSelect, /* A SELECT statement to use as the data source */
13567 int onError, /* How to handle constraint errors */
13568 int iDbDest /* The database of pDest */
13569 ){
13570 sqlite3 *db = pParse->db;
13571 ExprList *pEList; /* The result set of the SELECT */
13572 Table *pSrc; /* The table in the FROM clause of SELECT */
13573 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
13574 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
13575 int i; /* Loop counter */
13576 int iDbSrc; /* The database of pSrc */
13577 int iSrc, iDest; /* Cursors from source and destination */
13578 int addr1, addr2; /* Loop addresses */
13579 int emptyDestTest = 0; /* Address of test for empty pDest */
13580 int emptySrcTest = 0; /* Address of test for empty pSrc */
13581 Vdbe *v; /* The VDBE we are building */
13582 int regAutoinc; /* Memory register used by AUTOINC */
13583 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
13584 int regData, regRowid; /* Registers holding data and rowid */
13585
13586 if( pSelect==0 ){
13587 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
13588 }
13589 if( pParse->pWith || pSelect->pWith ){
13590 /* Do not attempt to process this query if there are an WITH clauses
13591 ** attached to it. Proceeding may generate a false "no such table: xxx"
13592 ** error if pSelect reads from a CTE named "xxx". */
13593 return 0;
13594 }
13595 if( sqlite3TriggerList(pParse, pDest) ){
13596 return 0; /* tab1 must not have triggers */
13597 }
13598 #ifndef SQLITE_OMIT_VIRTUALTABLE
13599 if( pDest->tabFlags & TF_Virtual ){
13600 return 0; /* tab1 must not be a virtual table */
13601 }
13602 #endif
13603 if( onError==OE_Default ){
13604 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
13605 if( onError==OE_Default ) onError = OE_Abort;
13606 }
13607 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
13608 if( pSelect->pSrc->nSrc!=1 ){
13609 return 0; /* FROM clause must have exactly one term */
13610 }
13611 if( pSelect->pSrc->a[0].pSelect ){
13612 return 0; /* FROM clause cannot contain a subquery */
13613 }
13614 if( pSelect->pWhere ){
13615 return 0; /* SELECT may not have a WHERE clause */
13616 }
13617 if( pSelect->pOrderBy ){
13618 return 0; /* SELECT may not have an ORDER BY clause */
13619 }
13620 /* Do not need to test for a HAVING clause. If HAVING is present but
13621 ** there is no ORDER BY, we will get an error. */
13622 if( pSelect->pGroupBy ){
13623 return 0; /* SELECT may not have a GROUP BY clause */
13624 }
13625 if( pSelect->pLimit ){
13626 return 0; /* SELECT may not have a LIMIT clause */
13627 }
13628 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
13629 if( pSelect->pPrior ){
13630 return 0; /* SELECT may not be a compound query */
13631 }
13632 if( pSelect->selFlags & SF_Distinct ){
13633 return 0; /* SELECT may not be DISTINCT */
13634 }
13635 pEList = pSelect->pEList;
13636 assert( pEList!=0 );
13637 if( pEList->nExpr!=1 ){
13638 return 0; /* The result set must have exactly one column */
13639 }
13640 assert( pEList->a[0].pExpr );
13641 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
13642 return 0; /* The result set must be the special operator "*" */
13643 }
13644
13645 /* At this point we have established that the statement is of the
13646 ** correct syntactic form to participate in this optimization. Now
13647 ** we have to check the semantics.
13648 */
13649 pItem = pSelect->pSrc->a;
13650 pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
13651 if( pSrc==0 ){
13652 return 0; /* FROM clause does not contain a real table */
13653 }
13654 if( pSrc==pDest ){
13655 return 0; /* tab1 and tab2 may not be the same table */
13656 }
13657 if( HasRowid(pDest)!=HasRowid(pSrc) ){
13658 return 0; /* source and destination must both be WITHOUT ROWID or not */
13659 }
13660 #ifndef SQLITE_OMIT_VIRTUALTABLE
13661 if( pSrc->tabFlags & TF_Virtual ){
13662 return 0; /* tab2 must not be a virtual table */
13663 }
13664 #endif
13665 if( pSrc->pSelect ){
13666 return 0; /* tab2 may not be a view */
13667 }
13668 if( pDest->nCol!=pSrc->nCol ){
13669 return 0; /* Number of columns must be the same in tab1 and tab2 */
13670 }
13671 if( pDest->iPKey!=pSrc->iPKey ){
13672 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
13673 }
13674 for(i=0; i<pDest->nCol; i++){
13675 Column *pDestCol = &pDest->aCol[i];
13676 Column *pSrcCol = &pSrc->aCol[i];
13677 #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
13678 if( (db->flags & SQLITE_Vacuum)==0
13679 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
13680 ){
13681 return 0; /* Neither table may have __hidden__ columns */
13682 }
13683 #endif
13684 if( pDestCol->affinity!=pSrcCol->affinity ){
13685 return 0; /* Affinity must be the same on all columns */
13686 }
13687 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
13688 return 0; /* Collating sequence must be the same on all columns */
13689 }
13690 if( pDestCol->notNull && !pSrcCol->notNull ){
13691 return 0; /* tab2 must be NOT NULL if tab1 is */
13692 }
13693 /* Default values for second and subsequent columns need to match. */
13694 if( i>0
13695 && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0)
13696 || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0))
13697 ){
13698 return 0; /* Default values must be the same for all columns */
13699 }
13700 }
13701 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
13702 if( IsUniqueIndex(pDestIdx) ){
13703 destHasUniqueIdx = 1;
13704 }
13705 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
13706 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
13707 }
13708 if( pSrcIdx==0 ){
13709 return 0; /* pDestIdx has no corresponding index in pSrc */
13710 }
13711 }
13712 #ifndef SQLITE_OMIT_CHECK
13713 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
13714 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
13715 }
13716 #endif
13717 #ifndef SQLITE_OMIT_FOREIGN_KEY
13718 /* Disallow the transfer optimization if the destination table constains
13719 ** any foreign key constraints. This is more restrictive than necessary.
13720 ** But the main beneficiary of the transfer optimization is the VACUUM
13721 ** command, and the VACUUM command disables foreign key constraints. So
13722 ** the extra complication to make this rule less restrictive is probably
13723 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
13724 */
13725 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
13726 return 0;
13727 }
13728 #endif
13729 if( (db->flags & SQLITE_CountRows)!=0 ){
13730 return 0; /* xfer opt does not play well with PRAGMA count_changes */
13731 }
13732
13733 /* If we get this far, it means that the xfer optimization is at
13734 ** least a possibility, though it might only work if the destination
13735 ** table (tab1) is initially empty.
13736 */
13737 #ifdef SQLITE_TEST
13738 sqlite3_xferopt_count++;
13739 #endif
13740 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
13741 v = sqlite3GetVdbe(pParse);
13742 sqlite3CodeVerifySchema(pParse, iDbSrc);
13743 iSrc = pParse->nTab++;
13744 iDest = pParse->nTab++;
13745 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
13746 regData = sqlite3GetTempReg(pParse);
13747 regRowid = sqlite3GetTempReg(pParse);
13748 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
13749 assert( HasRowid(pDest) || destHasUniqueIdx );
13750 if( (db->flags & SQLITE_Vacuum)==0 && (
13751 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
13752 || destHasUniqueIdx /* (2) */
13753 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
13754 )){
13755 /* In some circumstances, we are able to run the xfer optimization
13756 ** only if the destination table is initially empty. Unless the
13757 ** SQLITE_Vacuum flag is set, this block generates code to make
13758 ** that determination. If SQLITE_Vacuum is set, then the destination
13759 ** table is always empty.
13760 **
13761 ** Conditions under which the destination must be empty:
13762 **
13763 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
13764 ** (If the destination is not initially empty, the rowid fields
13765 ** of index entries might need to change.)
13766 **
13767 ** (2) The destination has a unique index. (The xfer optimization
13768 ** is unable to test uniqueness.)
13769 **
13770 ** (3) onError is something other than OE_Abort and OE_Rollback.
13771 */
13772 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
13773 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
13774 sqlite3VdbeJumpHere(v, addr1);
13775 }
13776 if( HasRowid(pSrc) ){
13777 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
13778 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
13779 if( pDest->iPKey>=0 ){
13780 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
13781 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
13782 VdbeCoverage(v);
13783 sqlite3RowidConstraint(pParse, onError, pDest);
13784 sqlite3VdbeJumpHere(v, addr2);
13785 autoIncStep(pParse, regAutoinc, regRowid);
13786 }else if( pDest->pIndex==0 ){
13787 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
13788 }else{
13789 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
13790 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
13791 }
13792 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
13793 sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
13794 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
13795 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
13796 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
13797 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
13798 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
13799 }else{
13800 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
13801 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
13802 }
13803 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
13804 u8 idxInsFlags = 0;
13805 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
13806 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
13807 }
13808 assert( pSrcIdx );
13809 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
13810 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
13811 VdbeComment((v, "%s", pSrcIdx->zName));
13812 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
13813 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
13814 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
13815 VdbeComment((v, "%s", pDestIdx->zName));
13816 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
13817 sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
13818 if( db->flags & SQLITE_Vacuum ){
13819 /* This INSERT command is part of a VACUUM operation, which guarantees
13820 ** that the destination table is empty. If all indexed columns use
13821 ** collation sequence BINARY, then it can also be assumed that the
13822 ** index will be populated by inserting keys in strictly sorted
13823 ** order. In this case, instead of seeking within the b-tree as part
13824 ** of every OP_IdxInsert opcode, an OP_Last is added before the
13825 ** OP_IdxInsert to seek to the point within the b-tree where each key
13826 ** should be inserted. This is faster.
13827 **
13828 ** If any of the indexed columns use a collation sequence other than
13829 ** BINARY, this optimization is disabled. This is because the user
13830 ** might change the definition of a collation sequence and then run
13831 ** a VACUUM command. In that case keys may not be written in strictly
13832 ** sorted order. */
13833 for(i=0; i<pSrcIdx->nColumn; i++){
13834 const char *zColl = pSrcIdx->azColl[i];
13835 assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0
13836 || sqlite3StrBINARY==zColl );
13837 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
13838 }
13839 if( i==pSrcIdx->nColumn ){
13840 idxInsFlags = OPFLAG_USESEEKRESULT;
13841 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
13842 }
13843 }
13844 if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
13845 idxInsFlags |= OPFLAG_NCHANGE;
13846 }
13847 sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
13848 sqlite3VdbeChangeP5(v, idxInsFlags);
13849 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
13850 sqlite3VdbeJumpHere(v, addr1);
13851 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
13852 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
13853 }
13854 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
13855 sqlite3ReleaseTempReg(pParse, regRowid);
13856 sqlite3ReleaseTempReg(pParse, regData);
13857 if( emptyDestTest ){
13858 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
13859 sqlite3VdbeJumpHere(v, emptyDestTest);
13860 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
13861 return 0;
13862 }else{
13863 return 1;
13864 }
13865 }
13866 #endif /* SQLITE_OMIT_XFER_OPT */
13867
13868 /************** End of insert.c **********************************************/
13869 /************** Begin file legacy.c ******************************************/
13870 /*
13871 ** 2001 September 15
13872 **
13873 ** The author disclaims copyright to this source code. In place of
13874 ** a legal notice, here is a blessing:
13875 **
13876 ** May you do good and not evil.
13877 ** May you find forgiveness for yourself and forgive others.
13878 ** May you share freely, never taking more than you give.
13879 **
13880 *************************************************************************
13881 ** Main file for the SQLite library. The routines in this file
13882 ** implement the programmer interface to the library. Routines in
13883 ** other files are for internal use by SQLite and should not be
13884 ** accessed by users of the library.
13885 */
13886
13887 /* #include "sqliteInt.h" */
13888
13889 /*
13890 ** Execute SQL code. Return one of the SQLITE_ success/failure
13891 ** codes. Also write an error message into memory obtained from
13892 ** malloc() and make *pzErrMsg point to that message.
13893 **
13894 ** If the SQL is a query, then for each row in the query result
13895 ** the xCallback() function is called. pArg becomes the first
13896 ** argument to xCallback(). If xCallback=NULL then no callback
13897 ** is invoked, even for queries.
13898 */
13899 SQLITE_API int SQLITE_STDCALL sqlite3_exec(
13900 sqlite3 *db, /* The database on which the SQL executes */
13901 const char *zSql, /* The SQL to be executed */
13902 sqlite3_callback xCallback, /* Invoke this callback routine */
13903 void *pArg, /* First argument to xCallback() */
13904 char **pzErrMsg /* Write error messages here */
13905 ){
13906 int rc = SQLITE_OK; /* Return code */
13907 const char *zLeftover; /* Tail of unprocessed SQL */
13908 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
13909 char **azCols = 0; /* Names of result columns */
13910 int callbackIsInit; /* True if callback data is initialized */
13911
13912 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
13913 if( zSql==0 ) zSql = "";
13914
13915 sqlite3_mutex_enter(db->mutex);
13916 sqlite3Error(db, SQLITE_OK);
13917 while( rc==SQLITE_OK && zSql[0] ){
13918 int nCol;
13919 char **azVals = 0;
13920
13921 pStmt = 0;
13922 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
13923 assert( rc==SQLITE_OK || pStmt==0 );
13924 if( rc!=SQLITE_OK ){
13925 continue;
13926 }
13927 if( !pStmt ){
13928 /* this happens for a comment or white-space */
13929 zSql = zLeftover;
13930 continue;
13931 }
13932
13933 callbackIsInit = 0;
13934 nCol = sqlite3_column_count(pStmt);
13935
13936 while( 1 ){
13937 int i;
13938 rc = sqlite3_step(pStmt);
13939
13940 /* Invoke the callback function if required */
13941 if( xCallback && (SQLITE_ROW==rc ||
13942 (SQLITE_DONE==rc && !callbackIsInit
13943 && db->flags&SQLITE_NullCallback)) ){
13944 if( !callbackIsInit ){
13945 azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
13946 if( azCols==0 ){
13947 goto exec_out;
13948 }
13949 for(i=0; i<nCol; i++){
13950 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
13951 /* sqlite3VdbeSetColName() installs column names as UTF8
13952 ** strings so there is no way for sqlite3_column_name() to fail. */
13953 assert( azCols[i]!=0 );
13954 }
13955 callbackIsInit = 1;
13956 }
13957 if( rc==SQLITE_ROW ){
13958 azVals = &azCols[nCol];
13959 for(i=0; i<nCol; i++){
13960 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
13961 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
13962 db->mallocFailed = 1;
13963 goto exec_out;
13964 }
13965 }
13966 }
13967 if( xCallback(pArg, nCol, azVals, azCols) ){
13968 /* EVIDENCE-OF: R-38229-40159 If the callback function to
13969 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
13970 ** return SQLITE_ABORT. */
13971 rc = SQLITE_ABORT;
13972 sqlite3VdbeFinalize((Vdbe *)pStmt);
13973 pStmt = 0;
13974 sqlite3Error(db, SQLITE_ABORT);
13975 goto exec_out;
13976 }
13977 }
13978
13979 if( rc!=SQLITE_ROW ){
13980 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
13981 pStmt = 0;
13982 zSql = zLeftover;
13983 while( sqlite3Isspace(zSql[0]) ) zSql++;
13984 break;
13985 }
13986 }
13987
13988 sqlite3DbFree(db, azCols);
13989 azCols = 0;
13990 }
13991
13992 exec_out:
13993 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
13994 sqlite3DbFree(db, azCols);
13995
13996 rc = sqlite3ApiExit(db, rc);
13997 if( rc!=SQLITE_OK && pzErrMsg ){
13998 int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
13999 *pzErrMsg = sqlite3Malloc(nErrMsg);
14000 if( *pzErrMsg ){
14001 memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
14002 }else{
14003 rc = SQLITE_NOMEM;
14004 sqlite3Error(db, SQLITE_NOMEM);
14005 }
14006 }else if( pzErrMsg ){
14007 *pzErrMsg = 0;
14008 }
14009
14010 assert( (rc&db->errMask)==rc );
14011 sqlite3_mutex_leave(db->mutex);
14012 return rc;
14013 }
14014
14015 /************** End of legacy.c **********************************************/
14016 /************** Begin file loadext.c *****************************************/
14017 /*
14018 ** 2006 June 7
14019 **
14020 ** The author disclaims copyright to this source code. In place of
14021 ** a legal notice, here is a blessing:
14022 **
14023 ** May you do good and not evil.
14024 ** May you find forgiveness for yourself and forgive others.
14025 ** May you share freely, never taking more than you give.
14026 **
14027 *************************************************************************
14028 ** This file contains code used to dynamically load extensions into
14029 ** the SQLite library.
14030 */
14031
14032 #ifndef SQLITE_CORE
14033 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
14034 #endif
14035 /************** Include sqlite3ext.h in the middle of loadext.c **************/
14036 /************** Begin file sqlite3ext.h **************************************/
14037 /*
14038 ** 2006 June 7
14039 **
14040 ** The author disclaims copyright to this source code. In place of
14041 ** a legal notice, here is a blessing:
14042 **
14043 ** May you do good and not evil.
14044 ** May you find forgiveness for yourself and forgive others.
14045 ** May you share freely, never taking more than you give.
14046 **
14047 *************************************************************************
14048 ** This header file defines the SQLite interface for use by
14049 ** shared libraries that want to be imported as extensions into
14050 ** an SQLite instance. Shared libraries that intend to be loaded
14051 ** as extensions by SQLite should #include this file instead of
14052 ** sqlite3.h.
14053 */
14054 #ifndef _SQLITE3EXT_H_
14055 #define _SQLITE3EXT_H_
14056 /* #include "sqlite3.h" */
14057
14058 typedef struct sqlite3_api_routines sqlite3_api_routines;
14059
14060 /*
14061 ** The following structure holds pointers to all of the SQLite API
14062 ** routines.
14063 **
14064 ** WARNING: In order to maintain backwards compatibility, add new
14065 ** interfaces to the end of this structure only. If you insert new
14066 ** interfaces in the middle of this structure, then older different
14067 ** versions of SQLite will not be able to load each other's shared
14068 ** libraries!
14069 */
14070 struct sqlite3_api_routines {
14071 void * (*aggregate_context)(sqlite3_context*,int nBytes);
14072 int (*aggregate_count)(sqlite3_context*);
14073 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
14074 int (*bind_double)(sqlite3_stmt*,int,double);
14075 int (*bind_int)(sqlite3_stmt*,int,int);
14076 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
14077 int (*bind_null)(sqlite3_stmt*,int);
14078 int (*bind_parameter_count)(sqlite3_stmt*);
14079 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
14080 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
14081 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
14082 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
14083 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
14084 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
14085 int (*busy_timeout)(sqlite3*,int ms);
14086 int (*changes)(sqlite3*);
14087 int (*close)(sqlite3*);
14088 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
14089 int eTextRep,const char*));
14090 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
14091 int eTextRep,const void*));
14092 const void * (*column_blob)(sqlite3_stmt*,int iCol);
14093 int (*column_bytes)(sqlite3_stmt*,int iCol);
14094 int (*column_bytes16)(sqlite3_stmt*,int iCol);
14095 int (*column_count)(sqlite3_stmt*pStmt);
14096 const char * (*column_database_name)(sqlite3_stmt*,int);
14097 const void * (*column_database_name16)(sqlite3_stmt*,int);
14098 const char * (*column_decltype)(sqlite3_stmt*,int i);
14099 const void * (*column_decltype16)(sqlite3_stmt*,int);
14100 double (*column_double)(sqlite3_stmt*,int iCol);
14101 int (*column_int)(sqlite3_stmt*,int iCol);
14102 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
14103 const char * (*column_name)(sqlite3_stmt*,int);
14104 const void * (*column_name16)(sqlite3_stmt*,int);
14105 const char * (*column_origin_name)(sqlite3_stmt*,int);
14106 const void * (*column_origin_name16)(sqlite3_stmt*,int);
14107 const char * (*column_table_name)(sqlite3_stmt*,int);
14108 const void * (*column_table_name16)(sqlite3_stmt*,int);
14109 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
14110 const void * (*column_text16)(sqlite3_stmt*,int iCol);
14111 int (*column_type)(sqlite3_stmt*,int iCol);
14112 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
14113 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
14114 int (*complete)(const char*sql);
14115 int (*complete16)(const void*sql);
14116 int (*create_collation)(sqlite3*,const char*,int,void*,
14117 int(*)(void*,int,const void*,int,const void*));
14118 int (*create_collation16)(sqlite3*,const void*,int,void*,
14119 int(*)(void*,int,const void*,int,const void*));
14120 int (*create_function)(sqlite3*,const char*,int,int,void*,
14121 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
14122 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
14123 void (*xFinal)(sqlite3_context*));
14124 int (*create_function16)(sqlite3*,const void*,int,int,void*,
14125 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
14126 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
14127 void (*xFinal)(sqlite3_context*));
14128 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
14129 int (*data_count)(sqlite3_stmt*pStmt);
14130 sqlite3 * (*db_handle)(sqlite3_stmt*);
14131 int (*declare_vtab)(sqlite3*,const char*);
14132 int (*enable_shared_cache)(int);
14133 int (*errcode)(sqlite3*db);
14134 const char * (*errmsg)(sqlite3*);
14135 const void * (*errmsg16)(sqlite3*);
14136 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
14137 int (*expired)(sqlite3_stmt*);
14138 int (*finalize)(sqlite3_stmt*pStmt);
14139 void (*free)(void*);
14140 void (*free_table)(char**result);
14141 int (*get_autocommit)(sqlite3*);
14142 void * (*get_auxdata)(sqlite3_context*,int);
14143 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
14144 int (*global_recover)(void);
14145 void (*interruptx)(sqlite3*);
14146 sqlite_int64 (*last_insert_rowid)(sqlite3*);
14147 const char * (*libversion)(void);
14148 int (*libversion_number)(void);
14149 void *(*malloc)(int);
14150 char * (*mprintf)(const char*,...);
14151 int (*open)(const char*,sqlite3**);
14152 int (*open16)(const void*,sqlite3**);
14153 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
14154 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
14155 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
14156 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
14157 void *(*realloc)(void*,int);
14158 int (*reset)(sqlite3_stmt*pStmt);
14159 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
14160 void (*result_double)(sqlite3_context*,double);
14161 void (*result_error)(sqlite3_context*,const char*,int);
14162 void (*result_error16)(sqlite3_context*,const void*,int);
14163 void (*result_int)(sqlite3_context*,int);
14164 void (*result_int64)(sqlite3_context*,sqlite_int64);
14165 void (*result_null)(sqlite3_context*);
14166 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
14167 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
14168 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
14169 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
14170 void (*result_value)(sqlite3_context*,sqlite3_value*);
14171 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
14172 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
14173 const char*,const char*),void*);
14174 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
14175 char * (*snprintf)(int,char*,const char*,...);
14176 int (*step)(sqlite3_stmt*);
14177 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
14178 char const**,char const**,int*,int*,int*);
14179 void (*thread_cleanup)(void);
14180 int (*total_changes)(sqlite3*);
14181 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
14182 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
14183 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
14184 sqlite_int64),void*);
14185 void * (*user_data)(sqlite3_context*);
14186 const void * (*value_blob)(sqlite3_value*);
14187 int (*value_bytes)(sqlite3_value*);
14188 int (*value_bytes16)(sqlite3_value*);
14189 double (*value_double)(sqlite3_value*);
14190 int (*value_int)(sqlite3_value*);
14191 sqlite_int64 (*value_int64)(sqlite3_value*);
14192 int (*value_numeric_type)(sqlite3_value*);
14193 const unsigned char * (*value_text)(sqlite3_value*);
14194 const void * (*value_text16)(sqlite3_value*);
14195 const void * (*value_text16be)(sqlite3_value*);
14196 const void * (*value_text16le)(sqlite3_value*);
14197 int (*value_type)(sqlite3_value*);
14198 char *(*vmprintf)(const char*,va_list);
14199 /* Added ??? */
14200 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
14201 /* Added by 3.3.13 */
14202 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
14203 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
14204 int (*clear_bindings)(sqlite3_stmt*);
14205 /* Added by 3.4.1 */
14206 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
14207 void (*xDestroy)(void *));
14208 /* Added by 3.5.0 */
14209 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
14210 int (*blob_bytes)(sqlite3_blob*);
14211 int (*blob_close)(sqlite3_blob*);
14212 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
14213 int,sqlite3_blob**);
14214 int (*blob_read)(sqlite3_blob*,void*,int,int);
14215 int (*blob_write)(sqlite3_blob*,const void*,int,int);
14216 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
14217 int(*)(void*,int,const void*,int,const void*),
14218 void(*)(void*));
14219 int (*file_control)(sqlite3*,const char*,int,void*);
14220 sqlite3_int64 (*memory_highwater)(int);
14221 sqlite3_int64 (*memory_used)(void);
14222 sqlite3_mutex *(*mutex_alloc)(int);
14223 void (*mutex_enter)(sqlite3_mutex*);
14224 void (*mutex_free)(sqlite3_mutex*);
14225 void (*mutex_leave)(sqlite3_mutex*);
14226 int (*mutex_try)(sqlite3_mutex*);
14227 int (*open_v2)(const char*,sqlite3**,int,const char*);
14228 int (*release_memory)(int);
14229 void (*result_error_nomem)(sqlite3_context*);
14230 void (*result_error_toobig)(sqlite3_context*);
14231 int (*sleep)(int);
14232 void (*soft_heap_limit)(int);
14233 sqlite3_vfs *(*vfs_find)(const char*);
14234 int (*vfs_register)(sqlite3_vfs*,int);
14235 int (*vfs_unregister)(sqlite3_vfs*);
14236 int (*xthreadsafe)(void);
14237 void (*result_zeroblob)(sqlite3_context*,int);
14238 void (*result_error_code)(sqlite3_context*,int);
14239 int (*test_control)(int, ...);
14240 void (*randomness)(int,void*);
14241 sqlite3 *(*context_db_handle)(sqlite3_context*);
14242 int (*extended_result_codes)(sqlite3*,int);
14243 int (*limit)(sqlite3*,int,int);
14244 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
14245 const char *(*sql)(sqlite3_stmt*);
14246 int (*status)(int,int*,int*,int);
14247 int (*backup_finish)(sqlite3_backup*);
14248 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
14249 int (*backup_pagecount)(sqlite3_backup*);
14250 int (*backup_remaining)(sqlite3_backup*);
14251 int (*backup_step)(sqlite3_backup*,int);
14252 const char *(*compileoption_get)(int);
14253 int (*compileoption_used)(const char*);
14254 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
14255 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
14256 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
14257 void (*xFinal)(sqlite3_context*),
14258 void(*xDestroy)(void*));
14259 int (*db_config)(sqlite3*,int,...);
14260 sqlite3_mutex *(*db_mutex)(sqlite3*);
14261 int (*db_status)(sqlite3*,int,int*,int*,int);
14262 int (*extended_errcode)(sqlite3*);
14263 void (*log)(int,const char*,...);
14264 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
14265 const char *(*sourceid)(void);
14266 int (*stmt_status)(sqlite3_stmt*,int,int);
14267 int (*strnicmp)(const char*,const char*,int);
14268 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
14269 int (*wal_autocheckpoint)(sqlite3*,int);
14270 int (*wal_checkpoint)(sqlite3*,const char*);
14271 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
14272 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
14273 int (*vtab_config)(sqlite3*,int op,...);
14274 int (*vtab_on_conflict)(sqlite3*);
14275 /* Version 3.7.16 and later */
14276 int (*close_v2)(sqlite3*);
14277 const char *(*db_filename)(sqlite3*,const char*);
14278 int (*db_readonly)(sqlite3*,const char*);
14279 int (*db_release_memory)(sqlite3*);
14280 const char *(*errstr)(int);
14281 int (*stmt_busy)(sqlite3_stmt*);
14282 int (*stmt_readonly)(sqlite3_stmt*);
14283 int (*stricmp)(const char*,const char*);
14284 int (*uri_boolean)(const char*,const char*,int);
14285 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
14286 const char *(*uri_parameter)(const char*,const char*);
14287 char *(*vsnprintf)(int,char*,const char*,va_list);
14288 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
14289 /* Version 3.8.7 and later */
14290 int (*auto_extension)(void(*)(void));
14291 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
14292 void(*)(void*));
14293 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
14294 void(*)(void*),unsigned char);
14295 int (*cancel_auto_extension)(void(*)(void));
14296 int (*load_extension)(sqlite3*,const char*,const char*,char**);
14297 void *(*malloc64)(sqlite3_uint64);
14298 sqlite3_uint64 (*msize)(void*);
14299 void *(*realloc64)(void*,sqlite3_uint64);
14300 void (*reset_auto_extension)(void);
14301 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
14302 void(*)(void*));
14303 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
14304 void(*)(void*), unsigned char);
14305 int (*strglob)(const char*,const char*);
14306 /* Version 3.8.11 and later */
14307 sqlite3_value *(*value_dup)(const sqlite3_value*);
14308 void (*value_free)(sqlite3_value*);
14309 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
14310 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
14311 /* Version 3.9.0 and later */
14312 unsigned int (*value_subtype)(sqlite3_value*);
14313 void (*result_subtype)(sqlite3_context*,unsigned int);
14314 /* Version 3.10.0 and later */
14315 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
14316 int (*strlike)(const char*,const char*,unsigned int);
14317 int (*db_cacheflush)(sqlite3*);
14318 };
14319
14320 /*
14321 ** The following macros redefine the API routines so that they are
14322 ** redirected through the global sqlite3_api structure.
14323 **
14324 ** This header file is also used by the loadext.c source file
14325 ** (part of the main SQLite library - not an extension) so that
14326 ** it can get access to the sqlite3_api_routines structure
14327 ** definition. But the main library does not want to redefine
14328 ** the API. So the redefinition macros are only valid if the
14329 ** SQLITE_CORE macros is undefined.
14330 */
14331 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
14332 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
14333 #ifndef SQLITE_OMIT_DEPRECATED
14334 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
14335 #endif
14336 #define sqlite3_bind_blob sqlite3_api->bind_blob
14337 #define sqlite3_bind_double sqlite3_api->bind_double
14338 #define sqlite3_bind_int sqlite3_api->bind_int
14339 #define sqlite3_bind_int64 sqlite3_api->bind_int64
14340 #define sqlite3_bind_null sqlite3_api->bind_null
14341 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
14342 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
14343 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
14344 #define sqlite3_bind_text sqlite3_api->bind_text
14345 #define sqlite3_bind_text16 sqlite3_api->bind_text16
14346 #define sqlite3_bind_value sqlite3_api->bind_value
14347 #define sqlite3_busy_handler sqlite3_api->busy_handler
14348 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
14349 #define sqlite3_changes sqlite3_api->changes
14350 #define sqlite3_close sqlite3_api->close
14351 #define sqlite3_collation_needed sqlite3_api->collation_needed
14352 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
14353 #define sqlite3_column_blob sqlite3_api->column_blob
14354 #define sqlite3_column_bytes sqlite3_api->column_bytes
14355 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
14356 #define sqlite3_column_count sqlite3_api->column_count
14357 #define sqlite3_column_database_name sqlite3_api->column_database_name
14358 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
14359 #define sqlite3_column_decltype sqlite3_api->column_decltype
14360 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
14361 #define sqlite3_column_double sqlite3_api->column_double
14362 #define sqlite3_column_int sqlite3_api->column_int
14363 #define sqlite3_column_int64 sqlite3_api->column_int64
14364 #define sqlite3_column_name sqlite3_api->column_name
14365 #define sqlite3_column_name16 sqlite3_api->column_name16
14366 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
14367 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
14368 #define sqlite3_column_table_name sqlite3_api->column_table_name
14369 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
14370 #define sqlite3_column_text sqlite3_api->column_text
14371 #define sqlite3_column_text16 sqlite3_api->column_text16
14372 #define sqlite3_column_type sqlite3_api->column_type
14373 #define sqlite3_column_value sqlite3_api->column_value
14374 #define sqlite3_commit_hook sqlite3_api->commit_hook
14375 #define sqlite3_complete sqlite3_api->complete
14376 #define sqlite3_complete16 sqlite3_api->complete16
14377 #define sqlite3_create_collation sqlite3_api->create_collation
14378 #define sqlite3_create_collation16 sqlite3_api->create_collation16
14379 #define sqlite3_create_function sqlite3_api->create_function
14380 #define sqlite3_create_function16 sqlite3_api->create_function16
14381 #define sqlite3_create_module sqlite3_api->create_module
14382 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
14383 #define sqlite3_data_count sqlite3_api->data_count
14384 #define sqlite3_db_handle sqlite3_api->db_handle
14385 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
14386 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
14387 #define sqlite3_errcode sqlite3_api->errcode
14388 #define sqlite3_errmsg sqlite3_api->errmsg
14389 #define sqlite3_errmsg16 sqlite3_api->errmsg16
14390 #define sqlite3_exec sqlite3_api->exec
14391 #ifndef SQLITE_OMIT_DEPRECATED
14392 #define sqlite3_expired sqlite3_api->expired
14393 #endif
14394 #define sqlite3_finalize sqlite3_api->finalize
14395 #define sqlite3_free sqlite3_api->free
14396 #define sqlite3_free_table sqlite3_api->free_table
14397 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
14398 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
14399 #define sqlite3_get_table sqlite3_api->get_table
14400 #ifndef SQLITE_OMIT_DEPRECATED
14401 #define sqlite3_global_recover sqlite3_api->global_recover
14402 #endif
14403 #define sqlite3_interrupt sqlite3_api->interruptx
14404 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
14405 #define sqlite3_libversion sqlite3_api->libversion
14406 #define sqlite3_libversion_number sqlite3_api->libversion_number
14407 #define sqlite3_malloc sqlite3_api->malloc
14408 #define sqlite3_mprintf sqlite3_api->mprintf
14409 #define sqlite3_open sqlite3_api->open
14410 #define sqlite3_open16 sqlite3_api->open16
14411 #define sqlite3_prepare sqlite3_api->prepare
14412 #define sqlite3_prepare16 sqlite3_api->prepare16
14413 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
14414 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
14415 #define sqlite3_profile sqlite3_api->profile
14416 #define sqlite3_progress_handler sqlite3_api->progress_handler
14417 #define sqlite3_realloc sqlite3_api->realloc
14418 #define sqlite3_reset sqlite3_api->reset
14419 #define sqlite3_result_blob sqlite3_api->result_blob
14420 #define sqlite3_result_double sqlite3_api->result_double
14421 #define sqlite3_result_error sqlite3_api->result_error
14422 #define sqlite3_result_error16 sqlite3_api->result_error16
14423 #define sqlite3_result_int sqlite3_api->result_int
14424 #define sqlite3_result_int64 sqlite3_api->result_int64
14425 #define sqlite3_result_null sqlite3_api->result_null
14426 #define sqlite3_result_text sqlite3_api->result_text
14427 #define sqlite3_result_text16 sqlite3_api->result_text16
14428 #define sqlite3_result_text16be sqlite3_api->result_text16be
14429 #define sqlite3_result_text16le sqlite3_api->result_text16le
14430 #define sqlite3_result_value sqlite3_api->result_value
14431 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
14432 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
14433 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
14434 #define sqlite3_snprintf sqlite3_api->snprintf
14435 #define sqlite3_step sqlite3_api->step
14436 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
14437 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
14438 #define sqlite3_total_changes sqlite3_api->total_changes
14439 #define sqlite3_trace sqlite3_api->trace
14440 #ifndef SQLITE_OMIT_DEPRECATED
14441 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
14442 #endif
14443 #define sqlite3_update_hook sqlite3_api->update_hook
14444 #define sqlite3_user_data sqlite3_api->user_data
14445 #define sqlite3_value_blob sqlite3_api->value_blob
14446 #define sqlite3_value_bytes sqlite3_api->value_bytes
14447 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
14448 #define sqlite3_value_double sqlite3_api->value_double
14449 #define sqlite3_value_int sqlite3_api->value_int
14450 #define sqlite3_value_int64 sqlite3_api->value_int64
14451 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
14452 #define sqlite3_value_text sqlite3_api->value_text
14453 #define sqlite3_value_text16 sqlite3_api->value_text16
14454 #define sqlite3_value_text16be sqlite3_api->value_text16be
14455 #define sqlite3_value_text16le sqlite3_api->value_text16le
14456 #define sqlite3_value_type sqlite3_api->value_type
14457 #define sqlite3_vmprintf sqlite3_api->vmprintf
14458 #define sqlite3_vsnprintf sqlite3_api->vsnprintf
14459 #define sqlite3_overload_function sqlite3_api->overload_function
14460 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
14461 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
14462 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
14463 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
14464 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
14465 #define sqlite3_blob_close sqlite3_api->blob_close
14466 #define sqlite3_blob_open sqlite3_api->blob_open
14467 #define sqlite3_blob_read sqlite3_api->blob_read
14468 #define sqlite3_blob_write sqlite3_api->blob_write
14469 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
14470 #define sqlite3_file_control sqlite3_api->file_control
14471 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
14472 #define sqlite3_memory_used sqlite3_api->memory_used
14473 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
14474 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
14475 #define sqlite3_mutex_free sqlite3_api->mutex_free
14476 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
14477 #define sqlite3_mutex_try sqlite3_api->mutex_try
14478 #define sqlite3_open_v2 sqlite3_api->open_v2
14479 #define sqlite3_release_memory sqlite3_api->release_memory
14480 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
14481 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
14482 #define sqlite3_sleep sqlite3_api->sleep
14483 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
14484 #define sqlite3_vfs_find sqlite3_api->vfs_find
14485 #define sqlite3_vfs_register sqlite3_api->vfs_register
14486 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
14487 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
14488 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
14489 #define sqlite3_result_error_code sqlite3_api->result_error_code
14490 #define sqlite3_test_control sqlite3_api->test_control
14491 #define sqlite3_randomness sqlite3_api->randomness
14492 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
14493 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
14494 #define sqlite3_limit sqlite3_api->limit
14495 #define sqlite3_next_stmt sqlite3_api->next_stmt
14496 #define sqlite3_sql sqlite3_api->sql
14497 #define sqlite3_status sqlite3_api->status
14498 #define sqlite3_backup_finish sqlite3_api->backup_finish
14499 #define sqlite3_backup_init sqlite3_api->backup_init
14500 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
14501 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
14502 #define sqlite3_backup_step sqlite3_api->backup_step
14503 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
14504 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
14505 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
14506 #define sqlite3_db_config sqlite3_api->db_config
14507 #define sqlite3_db_mutex sqlite3_api->db_mutex
14508 #define sqlite3_db_status sqlite3_api->db_status
14509 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
14510 #define sqlite3_log sqlite3_api->log
14511 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
14512 #define sqlite3_sourceid sqlite3_api->sourceid
14513 #define sqlite3_stmt_status sqlite3_api->stmt_status
14514 #define sqlite3_strnicmp sqlite3_api->strnicmp
14515 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
14516 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
14517 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
14518 #define sqlite3_wal_hook sqlite3_api->wal_hook
14519 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
14520 #define sqlite3_vtab_config sqlite3_api->vtab_config
14521 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
14522 /* Version 3.7.16 and later */
14523 #define sqlite3_close_v2 sqlite3_api->close_v2
14524 #define sqlite3_db_filename sqlite3_api->db_filename
14525 #define sqlite3_db_readonly sqlite3_api->db_readonly
14526 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
14527 #define sqlite3_errstr sqlite3_api->errstr
14528 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
14529 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
14530 #define sqlite3_stricmp sqlite3_api->stricmp
14531 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
14532 #define sqlite3_uri_int64 sqlite3_api->uri_int64
14533 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
14534 #define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf
14535 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
14536 /* Version 3.8.7 and later */
14537 #define sqlite3_auto_extension sqlite3_api->auto_extension
14538 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
14539 #define sqlite3_bind_text64 sqlite3_api->bind_text64
14540 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
14541 #define sqlite3_load_extension sqlite3_api->load_extension
14542 #define sqlite3_malloc64 sqlite3_api->malloc64
14543 #define sqlite3_msize sqlite3_api->msize
14544 #define sqlite3_realloc64 sqlite3_api->realloc64
14545 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
14546 #define sqlite3_result_blob64 sqlite3_api->result_blob64
14547 #define sqlite3_result_text64 sqlite3_api->result_text64
14548 #define sqlite3_strglob sqlite3_api->strglob
14549 /* Version 3.8.11 and later */
14550 #define sqlite3_value_dup sqlite3_api->value_dup
14551 #define sqlite3_value_free sqlite3_api->value_free
14552 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
14553 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
14554 /* Version 3.9.0 and later */
14555 #define sqlite3_value_subtype sqlite3_api->value_subtype
14556 #define sqlite3_result_subtype sqlite3_api->result_subtype
14557 /* Version 3.10.0 and later */
14558 #define sqlite3_status64 sqlite3_api->status64
14559 #define sqlite3_strlike sqlite3_api->strlike
14560 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
14561 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
14562
14563 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
14564 /* This case when the file really is being compiled as a loadable
14565 ** extension */
14566 # define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
14567 # define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
14568 # define SQLITE_EXTENSION_INIT3 \
14569 extern const sqlite3_api_routines *sqlite3_api;
14570 #else
14571 /* This case when the file is being statically linked into the
14572 ** application */
14573 # define SQLITE_EXTENSION_INIT1 /*no-op*/
14574 # define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
14575 # define SQLITE_EXTENSION_INIT3 /*no-op*/
14576 #endif
14577
14578 #endif /* _SQLITE3EXT_H_ */
14579
14580 /************** End of sqlite3ext.h ******************************************/
14581 /************** Continuing where we left off in loadext.c ********************/
14582 /* #include "sqliteInt.h" */
14583 /* #include <string.h> */
14584
14585 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14586
14587 /*
14588 ** Some API routines are omitted when various features are
14589 ** excluded from a build of SQLite. Substitute a NULL pointer
14590 ** for any missing APIs.
14591 */
14592 #ifndef SQLITE_ENABLE_COLUMN_METADATA
14593 # define sqlite3_column_database_name 0
14594 # define sqlite3_column_database_name16 0
14595 # define sqlite3_column_table_name 0
14596 # define sqlite3_column_table_name16 0
14597 # define sqlite3_column_origin_name 0
14598 # define sqlite3_column_origin_name16 0
14599 #endif
14600
14601 #ifdef SQLITE_OMIT_AUTHORIZATION
14602 # define sqlite3_set_authorizer 0
14603 #endif
14604
14605 #ifdef SQLITE_OMIT_UTF16
14606 # define sqlite3_bind_text16 0
14607 # define sqlite3_collation_needed16 0
14608 # define sqlite3_column_decltype16 0
14609 # define sqlite3_column_name16 0
14610 # define sqlite3_column_text16 0
14611 # define sqlite3_complete16 0
14612 # define sqlite3_create_collation16 0
14613 # define sqlite3_create_function16 0
14614 # define sqlite3_errmsg16 0
14615 # define sqlite3_open16 0
14616 # define sqlite3_prepare16 0
14617 # define sqlite3_prepare16_v2 0
14618 # define sqlite3_result_error16 0
14619 # define sqlite3_result_text16 0
14620 # define sqlite3_result_text16be 0
14621 # define sqlite3_result_text16le 0
14622 # define sqlite3_value_text16 0
14623 # define sqlite3_value_text16be 0
14624 # define sqlite3_value_text16le 0
14625 # define sqlite3_column_database_name16 0
14626 # define sqlite3_column_table_name16 0
14627 # define sqlite3_column_origin_name16 0
14628 #endif
14629
14630 #ifdef SQLITE_OMIT_COMPLETE
14631 # define sqlite3_complete 0
14632 # define sqlite3_complete16 0
14633 #endif
14634
14635 #ifdef SQLITE_OMIT_DECLTYPE
14636 # define sqlite3_column_decltype16 0
14637 # define sqlite3_column_decltype 0
14638 #endif
14639
14640 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
14641 # define sqlite3_progress_handler 0
14642 #endif
14643
14644 #ifdef SQLITE_OMIT_VIRTUALTABLE
14645 # define sqlite3_create_module 0
14646 # define sqlite3_create_module_v2 0
14647 # define sqlite3_declare_vtab 0
14648 # define sqlite3_vtab_config 0
14649 # define sqlite3_vtab_on_conflict 0
14650 #endif
14651
14652 #ifdef SQLITE_OMIT_SHARED_CACHE
14653 # define sqlite3_enable_shared_cache 0
14654 #endif
14655
14656 #ifdef SQLITE_OMIT_TRACE
14657 # define sqlite3_profile 0
14658 # define sqlite3_trace 0
14659 #endif
14660
14661 #ifdef SQLITE_OMIT_GET_TABLE
14662 # define sqlite3_free_table 0
14663 # define sqlite3_get_table 0
14664 #endif
14665
14666 #ifdef SQLITE_OMIT_INCRBLOB
14667 #define sqlite3_bind_zeroblob 0
14668 #define sqlite3_blob_bytes 0
14669 #define sqlite3_blob_close 0
14670 #define sqlite3_blob_open 0
14671 #define sqlite3_blob_read 0
14672 #define sqlite3_blob_write 0
14673 #define sqlite3_blob_reopen 0
14674 #endif
14675
14676 /*
14677 ** The following structure contains pointers to all SQLite API routines.
14678 ** A pointer to this structure is passed into extensions when they are
14679 ** loaded so that the extension can make calls back into the SQLite
14680 ** library.
14681 **
14682 ** When adding new APIs, add them to the bottom of this structure
14683 ** in order to preserve backwards compatibility.
14684 **
14685 ** Extensions that use newer APIs should first call the
14686 ** sqlite3_libversion_number() to make sure that the API they
14687 ** intend to use is supported by the library. Extensions should
14688 ** also check to make sure that the pointer to the function is
14689 ** not NULL before calling it.
14690 */
14691 static const sqlite3_api_routines sqlite3Apis = {
14692 sqlite3_aggregate_context,
14693 #ifndef SQLITE_OMIT_DEPRECATED
14694 sqlite3_aggregate_count,
14695 #else
14696 0,
14697 #endif
14698 sqlite3_bind_blob,
14699 sqlite3_bind_double,
14700 sqlite3_bind_int,
14701 sqlite3_bind_int64,
14702 sqlite3_bind_null,
14703 sqlite3_bind_parameter_count,
14704 sqlite3_bind_parameter_index,
14705 sqlite3_bind_parameter_name,
14706 sqlite3_bind_text,
14707 sqlite3_bind_text16,
14708 sqlite3_bind_value,
14709 sqlite3_busy_handler,
14710 sqlite3_busy_timeout,
14711 sqlite3_changes,
14712 sqlite3_close,
14713 sqlite3_collation_needed,
14714 sqlite3_collation_needed16,
14715 sqlite3_column_blob,
14716 sqlite3_column_bytes,
14717 sqlite3_column_bytes16,
14718 sqlite3_column_count,
14719 sqlite3_column_database_name,
14720 sqlite3_column_database_name16,
14721 sqlite3_column_decltype,
14722 sqlite3_column_decltype16,
14723 sqlite3_column_double,
14724 sqlite3_column_int,
14725 sqlite3_column_int64,
14726 sqlite3_column_name,
14727 sqlite3_column_name16,
14728 sqlite3_column_origin_name,
14729 sqlite3_column_origin_name16,
14730 sqlite3_column_table_name,
14731 sqlite3_column_table_name16,
14732 sqlite3_column_text,
14733 sqlite3_column_text16,
14734 sqlite3_column_type,
14735 sqlite3_column_value,
14736 sqlite3_commit_hook,
14737 sqlite3_complete,
14738 sqlite3_complete16,
14739 sqlite3_create_collation,
14740 sqlite3_create_collation16,
14741 sqlite3_create_function,
14742 sqlite3_create_function16,
14743 sqlite3_create_module,
14744 sqlite3_data_count,
14745 sqlite3_db_handle,
14746 sqlite3_declare_vtab,
14747 sqlite3_enable_shared_cache,
14748 sqlite3_errcode,
14749 sqlite3_errmsg,
14750 sqlite3_errmsg16,
14751 sqlite3_exec,
14752 #ifndef SQLITE_OMIT_DEPRECATED
14753 sqlite3_expired,
14754 #else
14755 0,
14756 #endif
14757 sqlite3_finalize,
14758 sqlite3_free,
14759 sqlite3_free_table,
14760 sqlite3_get_autocommit,
14761 sqlite3_get_auxdata,
14762 sqlite3_get_table,
14763 0, /* Was sqlite3_global_recover(), but that function is deprecated */
14764 sqlite3_interrupt,
14765 sqlite3_last_insert_rowid,
14766 sqlite3_libversion,
14767 sqlite3_libversion_number,
14768 sqlite3_malloc,
14769 sqlite3_mprintf,
14770 sqlite3_open,
14771 sqlite3_open16,
14772 sqlite3_prepare,
14773 sqlite3_prepare16,
14774 sqlite3_profile,
14775 sqlite3_progress_handler,
14776 sqlite3_realloc,
14777 sqlite3_reset,
14778 sqlite3_result_blob,
14779 sqlite3_result_double,
14780 sqlite3_result_error,
14781 sqlite3_result_error16,
14782 sqlite3_result_int,
14783 sqlite3_result_int64,
14784 sqlite3_result_null,
14785 sqlite3_result_text,
14786 sqlite3_result_text16,
14787 sqlite3_result_text16be,
14788 sqlite3_result_text16le,
14789 sqlite3_result_value,
14790 sqlite3_rollback_hook,
14791 sqlite3_set_authorizer,
14792 sqlite3_set_auxdata,
14793 sqlite3_snprintf,
14794 sqlite3_step,
14795 sqlite3_table_column_metadata,
14796 #ifndef SQLITE_OMIT_DEPRECATED
14797 sqlite3_thread_cleanup,
14798 #else
14799 0,
14800 #endif
14801 sqlite3_total_changes,
14802 sqlite3_trace,
14803 #ifndef SQLITE_OMIT_DEPRECATED
14804 sqlite3_transfer_bindings,
14805 #else
14806 0,
14807 #endif
14808 sqlite3_update_hook,
14809 sqlite3_user_data,
14810 sqlite3_value_blob,
14811 sqlite3_value_bytes,
14812 sqlite3_value_bytes16,
14813 sqlite3_value_double,
14814 sqlite3_value_int,
14815 sqlite3_value_int64,
14816 sqlite3_value_numeric_type,
14817 sqlite3_value_text,
14818 sqlite3_value_text16,
14819 sqlite3_value_text16be,
14820 sqlite3_value_text16le,
14821 sqlite3_value_type,
14822 sqlite3_vmprintf,
14823 /*
14824 ** The original API set ends here. All extensions can call any
14825 ** of the APIs above provided that the pointer is not NULL. But
14826 ** before calling APIs that follow, extension should check the
14827 ** sqlite3_libversion_number() to make sure they are dealing with
14828 ** a library that is new enough to support that API.
14829 *************************************************************************
14830 */
14831 sqlite3_overload_function,
14832
14833 /*
14834 ** Added after 3.3.13
14835 */
14836 sqlite3_prepare_v2,
14837 sqlite3_prepare16_v2,
14838 sqlite3_clear_bindings,
14839
14840 /*
14841 ** Added for 3.4.1
14842 */
14843 sqlite3_create_module_v2,
14844
14845 /*
14846 ** Added for 3.5.0
14847 */
14848 sqlite3_bind_zeroblob,
14849 sqlite3_blob_bytes,
14850 sqlite3_blob_close,
14851 sqlite3_blob_open,
14852 sqlite3_blob_read,
14853 sqlite3_blob_write,
14854 sqlite3_create_collation_v2,
14855 sqlite3_file_control,
14856 sqlite3_memory_highwater,
14857 sqlite3_memory_used,
14858 #ifdef SQLITE_MUTEX_OMIT
14859 0,
14860 0,
14861 0,
14862 0,
14863 0,
14864 #else
14865 sqlite3_mutex_alloc,
14866 sqlite3_mutex_enter,
14867 sqlite3_mutex_free,
14868 sqlite3_mutex_leave,
14869 sqlite3_mutex_try,
14870 #endif
14871 sqlite3_open_v2,
14872 sqlite3_release_memory,
14873 sqlite3_result_error_nomem,
14874 sqlite3_result_error_toobig,
14875 sqlite3_sleep,
14876 sqlite3_soft_heap_limit,
14877 sqlite3_vfs_find,
14878 sqlite3_vfs_register,
14879 sqlite3_vfs_unregister,
14880
14881 /*
14882 ** Added for 3.5.8
14883 */
14884 sqlite3_threadsafe,
14885 sqlite3_result_zeroblob,
14886 sqlite3_result_error_code,
14887 sqlite3_test_control,
14888 sqlite3_randomness,
14889 sqlite3_context_db_handle,
14890
14891 /*
14892 ** Added for 3.6.0
14893 */
14894 sqlite3_extended_result_codes,
14895 sqlite3_limit,
14896 sqlite3_next_stmt,
14897 sqlite3_sql,
14898 sqlite3_status,
14899
14900 /*
14901 ** Added for 3.7.4
14902 */
14903 sqlite3_backup_finish,
14904 sqlite3_backup_init,
14905 sqlite3_backup_pagecount,
14906 sqlite3_backup_remaining,
14907 sqlite3_backup_step,
14908 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
14909 sqlite3_compileoption_get,
14910 sqlite3_compileoption_used,
14911 #else
14912 0,
14913 0,
14914 #endif
14915 sqlite3_create_function_v2,
14916 sqlite3_db_config,
14917 sqlite3_db_mutex,
14918 sqlite3_db_status,
14919 sqlite3_extended_errcode,
14920 sqlite3_log,
14921 sqlite3_soft_heap_limit64,
14922 sqlite3_sourceid,
14923 sqlite3_stmt_status,
14924 sqlite3_strnicmp,
14925 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
14926 sqlite3_unlock_notify,
14927 #else
14928 0,
14929 #endif
14930 #ifndef SQLITE_OMIT_WAL
14931 sqlite3_wal_autocheckpoint,
14932 sqlite3_wal_checkpoint,
14933 sqlite3_wal_hook,
14934 #else
14935 0,
14936 0,
14937 0,
14938 #endif
14939 sqlite3_blob_reopen,
14940 sqlite3_vtab_config,
14941 sqlite3_vtab_on_conflict,
14942 sqlite3_close_v2,
14943 sqlite3_db_filename,
14944 sqlite3_db_readonly,
14945 sqlite3_db_release_memory,
14946 sqlite3_errstr,
14947 sqlite3_stmt_busy,
14948 sqlite3_stmt_readonly,
14949 sqlite3_stricmp,
14950 sqlite3_uri_boolean,
14951 sqlite3_uri_int64,
14952 sqlite3_uri_parameter,
14953 sqlite3_vsnprintf,
14954 sqlite3_wal_checkpoint_v2,
14955 /* Version 3.8.7 and later */
14956 sqlite3_auto_extension,
14957 sqlite3_bind_blob64,
14958 sqlite3_bind_text64,
14959 sqlite3_cancel_auto_extension,
14960 sqlite3_load_extension,
14961 sqlite3_malloc64,
14962 sqlite3_msize,
14963 sqlite3_realloc64,
14964 sqlite3_reset_auto_extension,
14965 sqlite3_result_blob64,
14966 sqlite3_result_text64,
14967 sqlite3_strglob,
14968 /* Version 3.8.11 and later */
14969 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
14970 sqlite3_value_free,
14971 sqlite3_result_zeroblob64,
14972 sqlite3_bind_zeroblob64,
14973 /* Version 3.9.0 and later */
14974 sqlite3_value_subtype,
14975 sqlite3_result_subtype,
14976 /* Version 3.10.0 and later */
14977 sqlite3_status64,
14978 sqlite3_strlike,
14979 sqlite3_db_cacheflush
14980 };
14981
14982 /*
14983 ** Attempt to load an SQLite extension library contained in the file
14984 ** zFile. The entry point is zProc. zProc may be 0 in which case a
14985 ** default entry point name (sqlite3_extension_init) is used. Use
14986 ** of the default name is recommended.
14987 **
14988 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
14989 **
14990 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
14991 ** error message text. The calling function should free this memory
14992 ** by calling sqlite3DbFree(db, ).
14993 */
14994 static int sqlite3LoadExtension(
14995 sqlite3 *db, /* Load the extension into this database connection */
14996 const char *zFile, /* Name of the shared library containing extension */
14997 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
14998 char **pzErrMsg /* Put error message here if not 0 */
14999 ){
15000 sqlite3_vfs *pVfs = db->pVfs;
15001 void *handle;
15002 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
15003 char *zErrmsg = 0;
15004 const char *zEntry;
15005 char *zAltEntry = 0;
15006 void **aHandle;
15007 u64 nMsg = 300 + sqlite3Strlen30(zFile);
15008 int ii;
15009
15010 /* Shared library endings to try if zFile cannot be loaded as written */
15011 static const char *azEndings[] = {
15012 #if SQLITE_OS_WIN
15013 "dll"
15014 #elif defined(__APPLE__)
15015 "dylib"
15016 #else
15017 "so"
15018 #endif
15019 };
15020
15021
15022 if( pzErrMsg ) *pzErrMsg = 0;
15023
15024 /* Ticket #1863. To avoid a creating security problems for older
15025 ** applications that relink against newer versions of SQLite, the
15026 ** ability to run load_extension is turned off by default. One
15027 ** must call sqlite3_enable_load_extension() to turn on extension
15028 ** loading. Otherwise you get the following error.
15029 */
15030 if( (db->flags & SQLITE_LoadExtension)==0 ){
15031 if( pzErrMsg ){
15032 *pzErrMsg = sqlite3_mprintf("not authorized");
15033 }
15034 return SQLITE_ERROR;
15035 }
15036
15037 zEntry = zProc ? zProc : "sqlite3_extension_init";
15038
15039 handle = sqlite3OsDlOpen(pVfs, zFile);
15040 #if SQLITE_OS_UNIX || SQLITE_OS_WIN
15041 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
15042 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
15043 if( zAltFile==0 ) return SQLITE_NOMEM;
15044 handle = sqlite3OsDlOpen(pVfs, zAltFile);
15045 sqlite3_free(zAltFile);
15046 }
15047 #endif
15048 if( handle==0 ){
15049 if( pzErrMsg ){
15050 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
15051 if( zErrmsg ){
15052 sqlite3_snprintf(nMsg, zErrmsg,
15053 "unable to open shared library [%s]", zFile);
15054 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
15055 }
15056 }
15057 return SQLITE_ERROR;
15058 }
15059 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
15060 sqlite3OsDlSym(pVfs, handle, zEntry);
15061
15062 /* If no entry point was specified and the default legacy
15063 ** entry point name "sqlite3_extension_init" was not found, then
15064 ** construct an entry point name "sqlite3_X_init" where the X is
15065 ** replaced by the lowercase value of every ASCII alphabetic
15066 ** character in the filename after the last "/" upto the first ".",
15067 ** and eliding the first three characters if they are "lib".
15068 ** Examples:
15069 **
15070 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
15071 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
15072 */
15073 if( xInit==0 && zProc==0 ){
15074 int iFile, iEntry, c;
15075 int ncFile = sqlite3Strlen30(zFile);
15076 zAltEntry = sqlite3_malloc64(ncFile+30);
15077 if( zAltEntry==0 ){
15078 sqlite3OsDlClose(pVfs, handle);
15079 return SQLITE_NOMEM;
15080 }
15081 memcpy(zAltEntry, "sqlite3_", 8);
15082 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
15083 iFile++;
15084 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
15085 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
15086 if( sqlite3Isalpha(c) ){
15087 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
15088 }
15089 }
15090 memcpy(zAltEntry+iEntry, "_init", 6);
15091 zEntry = zAltEntry;
15092 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
15093 sqlite3OsDlSym(pVfs, handle, zEntry);
15094 }
15095 if( xInit==0 ){
15096 if( pzErrMsg ){
15097 nMsg += sqlite3Strlen30(zEntry);
15098 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
15099 if( zErrmsg ){
15100 sqlite3_snprintf(nMsg, zErrmsg,
15101 "no entry point [%s] in shared library [%s]", zEntry, zFile);
15102 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
15103 }
15104 }
15105 sqlite3OsDlClose(pVfs, handle);
15106 sqlite3_free(zAltEntry);
15107 return SQLITE_ERROR;
15108 }
15109 sqlite3_free(zAltEntry);
15110 if( xInit(db, &zErrmsg, &sqlite3Apis) ){
15111 if( pzErrMsg ){
15112 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
15113 }
15114 sqlite3_free(zErrmsg);
15115 sqlite3OsDlClose(pVfs, handle);
15116 return SQLITE_ERROR;
15117 }
15118
15119 /* Append the new shared library handle to the db->aExtension array. */
15120 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
15121 if( aHandle==0 ){
15122 return SQLITE_NOMEM;
15123 }
15124 if( db->nExtension>0 ){
15125 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
15126 }
15127 sqlite3DbFree(db, db->aExtension);
15128 db->aExtension = aHandle;
15129
15130 db->aExtension[db->nExtension++] = handle;
15131 return SQLITE_OK;
15132 }
15133 SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
15134 sqlite3 *db, /* Load the extension into this database connection */
15135 const char *zFile, /* Name of the shared library containing extension */
15136 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
15137 char **pzErrMsg /* Put error message here if not 0 */
15138 ){
15139 int rc;
15140 sqlite3_mutex_enter(db->mutex);
15141 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
15142 rc = sqlite3ApiExit(db, rc);
15143 sqlite3_mutex_leave(db->mutex);
15144 return rc;
15145 }
15146
15147 /*
15148 ** Call this routine when the database connection is closing in order
15149 ** to clean up loaded extensions
15150 */
15151 SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
15152 int i;
15153 assert( sqlite3_mutex_held(db->mutex) );
15154 for(i=0; i<db->nExtension; i++){
15155 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
15156 }
15157 sqlite3DbFree(db, db->aExtension);
15158 }
15159
15160 /*
15161 ** Enable or disable extension loading. Extension loading is disabled by
15162 ** default so as not to open security holes in older applications.
15163 */
15164 SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int ono ff){
15165 sqlite3_mutex_enter(db->mutex);
15166 if( onoff ){
15167 db->flags |= SQLITE_LoadExtension;
15168 }else{
15169 db->flags &= ~SQLITE_LoadExtension;
15170 }
15171 sqlite3_mutex_leave(db->mutex);
15172 return SQLITE_OK;
15173 }
15174
15175 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
15176
15177 /*
15178 ** The auto-extension code added regardless of whether or not extension
15179 ** loading is supported. We need a dummy sqlite3Apis pointer for that
15180 ** code if regular extension loading is not available. This is that
15181 ** dummy pointer.
15182 */
15183 #ifdef SQLITE_OMIT_LOAD_EXTENSION
15184 static const sqlite3_api_routines sqlite3Apis = { 0 };
15185 #endif
15186
15187
15188 /*
15189 ** The following object holds the list of automatically loaded
15190 ** extensions.
15191 **
15192 ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
15193 ** mutex must be held while accessing this list.
15194 */
15195 typedef struct sqlite3AutoExtList sqlite3AutoExtList;
15196 static SQLITE_WSD struct sqlite3AutoExtList {
15197 u32 nExt; /* Number of entries in aExt[] */
15198 void (**aExt)(void); /* Pointers to the extension init functions */
15199 } sqlite3Autoext = { 0, 0 };
15200
15201 /* The "wsdAutoext" macro will resolve to the autoextension
15202 ** state vector. If writable static data is unsupported on the target,
15203 ** we have to locate the state vector at run-time. In the more common
15204 ** case where writable static data is supported, wsdStat can refer directly
15205 ** to the "sqlite3Autoext" state vector declared above.
15206 */
15207 #ifdef SQLITE_OMIT_WSD
15208 # define wsdAutoextInit \
15209 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
15210 # define wsdAutoext x[0]
15211 #else
15212 # define wsdAutoextInit
15213 # define wsdAutoext sqlite3Autoext
15214 #endif
15215
15216
15217 /*
15218 ** Register a statically linked extension that is automatically
15219 ** loaded by every new database connection.
15220 */
15221 SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xInit)(void)){
15222 int rc = SQLITE_OK;
15223 #ifndef SQLITE_OMIT_AUTOINIT
15224 rc = sqlite3_initialize();
15225 if( rc ){
15226 return rc;
15227 }else
15228 #endif
15229 {
15230 u32 i;
15231 #if SQLITE_THREADSAFE
15232 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
15233 #endif
15234 wsdAutoextInit;
15235 sqlite3_mutex_enter(mutex);
15236 for(i=0; i<wsdAutoext.nExt; i++){
15237 if( wsdAutoext.aExt[i]==xInit ) break;
15238 }
15239 if( i==wsdAutoext.nExt ){
15240 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
15241 void (**aNew)(void);
15242 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
15243 if( aNew==0 ){
15244 rc = SQLITE_NOMEM;
15245 }else{
15246 wsdAutoext.aExt = aNew;
15247 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
15248 wsdAutoext.nExt++;
15249 }
15250 }
15251 sqlite3_mutex_leave(mutex);
15252 assert( (rc&0xff)==rc );
15253 return rc;
15254 }
15255 }
15256
15257 /*
15258 ** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
15259 ** set of routines that is invoked for each new database connection, if it
15260 ** is currently on the list. If xInit is not on the list, then this
15261 ** routine is a no-op.
15262 **
15263 ** Return 1 if xInit was found on the list and removed. Return 0 if xInit
15264 ** was not on the list.
15265 */
15266 SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xInit)(void)) {
15267 #if SQLITE_THREADSAFE
15268 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
15269 #endif
15270 int i;
15271 int n = 0;
15272 wsdAutoextInit;
15273 sqlite3_mutex_enter(mutex);
15274 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
15275 if( wsdAutoext.aExt[i]==xInit ){
15276 wsdAutoext.nExt--;
15277 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
15278 n++;
15279 break;
15280 }
15281 }
15282 sqlite3_mutex_leave(mutex);
15283 return n;
15284 }
15285
15286 /*
15287 ** Reset the automatic extension loading mechanism.
15288 */
15289 SQLITE_API void SQLITE_STDCALL sqlite3_reset_auto_extension(void){
15290 #ifndef SQLITE_OMIT_AUTOINIT
15291 if( sqlite3_initialize()==SQLITE_OK )
15292 #endif
15293 {
15294 #if SQLITE_THREADSAFE
15295 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
15296 #endif
15297 wsdAutoextInit;
15298 sqlite3_mutex_enter(mutex);
15299 sqlite3_free(wsdAutoext.aExt);
15300 wsdAutoext.aExt = 0;
15301 wsdAutoext.nExt = 0;
15302 sqlite3_mutex_leave(mutex);
15303 }
15304 }
15305
15306 /*
15307 ** Load all automatic extensions.
15308 **
15309 ** If anything goes wrong, set an error in the database connection.
15310 */
15311 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
15312 u32 i;
15313 int go = 1;
15314 int rc;
15315 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
15316
15317 wsdAutoextInit;
15318 if( wsdAutoext.nExt==0 ){
15319 /* Common case: early out without every having to acquire a mutex */
15320 return;
15321 }
15322 for(i=0; go; i++){
15323 char *zErrmsg;
15324 #if SQLITE_THREADSAFE
15325 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
15326 #endif
15327 sqlite3_mutex_enter(mutex);
15328 if( i>=wsdAutoext.nExt ){
15329 xInit = 0;
15330 go = 0;
15331 }else{
15332 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
15333 wsdAutoext.aExt[i];
15334 }
15335 sqlite3_mutex_leave(mutex);
15336 zErrmsg = 0;
15337 if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
15338 sqlite3ErrorWithMsg(db, rc,
15339 "automatic extension loading failed: %s", zErrmsg);
15340 go = 0;
15341 }
15342 sqlite3_free(zErrmsg);
15343 }
15344 }
15345
15346 /************** End of loadext.c *********************************************/
15347 /************** Begin file pragma.c ******************************************/
15348 /*
15349 ** 2003 April 6
15350 **
15351 ** The author disclaims copyright to this source code. In place of
15352 ** a legal notice, here is a blessing:
15353 **
15354 ** May you do good and not evil.
15355 ** May you find forgiveness for yourself and forgive others.
15356 ** May you share freely, never taking more than you give.
15357 **
15358 *************************************************************************
15359 ** This file contains code used to implement the PRAGMA command.
15360 */
15361 /* #include "sqliteInt.h" */
15362
15363 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
15364 # if defined(__APPLE__)
15365 # define SQLITE_ENABLE_LOCKING_STYLE 1
15366 # else
15367 # define SQLITE_ENABLE_LOCKING_STYLE 0
15368 # endif
15369 #endif
15370
15371 /***************************************************************************
15372 ** The "pragma.h" include file is an automatically generated file that
15373 ** that includes the PragType_XXXX macro definitions and the aPragmaName[]
15374 ** object. This ensures that the aPragmaName[] table is arranged in
15375 ** lexicographical order to facility a binary search of the pragma name.
15376 ** Do not edit pragma.h directly. Edit and rerun the script in at
15377 ** ../tool/mkpragmatab.tcl. */
15378 /************** Include pragma.h in the middle of pragma.c *******************/
15379 /************** Begin file pragma.h ******************************************/
15380 /* DO NOT EDIT!
15381 ** This file is automatically generated by the script at
15382 ** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
15383 ** that script and rerun it.
15384 */
15385 #define PragTyp_HEADER_VALUE 0
15386 #define PragTyp_AUTO_VACUUM 1
15387 #define PragTyp_FLAG 2
15388 #define PragTyp_BUSY_TIMEOUT 3
15389 #define PragTyp_CACHE_SIZE 4
15390 #define PragTyp_CACHE_SPILL 5
15391 #define PragTyp_CASE_SENSITIVE_LIKE 6
15392 #define PragTyp_COLLATION_LIST 7
15393 #define PragTyp_COMPILE_OPTIONS 8
15394 #define PragTyp_DATA_STORE_DIRECTORY 9
15395 #define PragTyp_DATABASE_LIST 10
15396 #define PragTyp_DEFAULT_CACHE_SIZE 11
15397 #define PragTyp_ENCODING 12
15398 #define PragTyp_FOREIGN_KEY_CHECK 13
15399 #define PragTyp_FOREIGN_KEY_LIST 14
15400 #define PragTyp_INCREMENTAL_VACUUM 15
15401 #define PragTyp_INDEX_INFO 16
15402 #define PragTyp_INDEX_LIST 17
15403 #define PragTyp_INTEGRITY_CHECK 18
15404 #define PragTyp_JOURNAL_MODE 19
15405 #define PragTyp_JOURNAL_SIZE_LIMIT 20
15406 #define PragTyp_LOCK_PROXY_FILE 21
15407 #define PragTyp_LOCKING_MODE 22
15408 #define PragTyp_PAGE_COUNT 23
15409 #define PragTyp_MMAP_SIZE 24
15410 #define PragTyp_PAGE_SIZE 25
15411 #define PragTyp_SECURE_DELETE 26
15412 #define PragTyp_SHRINK_MEMORY 27
15413 #define PragTyp_SOFT_HEAP_LIMIT 28
15414 #define PragTyp_STATS 29
15415 #define PragTyp_SYNCHRONOUS 30
15416 #define PragTyp_TABLE_INFO 31
15417 #define PragTyp_TEMP_STORE 32
15418 #define PragTyp_TEMP_STORE_DIRECTORY 33
15419 #define PragTyp_THREADS 34
15420 #define PragTyp_WAL_AUTOCHECKPOINT 35
15421 #define PragTyp_WAL_CHECKPOINT 36
15422 #define PragTyp_ACTIVATE_EXTENSIONS 37
15423 #define PragTyp_HEXKEY 38
15424 #define PragTyp_KEY 39
15425 #define PragTyp_REKEY 40
15426 #define PragTyp_LOCK_STATUS 41
15427 #define PragTyp_PARSER_TRACE 42
15428 #define PragFlag_NeedSchema 0x01
15429 #define PragFlag_ReadOnly 0x02
15430 static const struct sPragmaNames {
15431 const char *const zName; /* Name of pragma */
15432 u8 ePragTyp; /* PragTyp_XXX value */
15433 u8 mPragFlag; /* Zero or more PragFlag_XXX values */
15434 u32 iArg; /* Extra argument */
15435 } aPragmaNames[] = {
15436 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
15437 { /* zName: */ "activate_extensions",
15438 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
15439 /* ePragFlag: */ 0,
15440 /* iArg: */ 0 },
15441 #endif
15442 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
15443 { /* zName: */ "application_id",
15444 /* ePragTyp: */ PragTyp_HEADER_VALUE,
15445 /* ePragFlag: */ 0,
15446 /* iArg: */ BTREE_APPLICATION_ID },
15447 #endif
15448 #if !defined(SQLITE_OMIT_AUTOVACUUM)
15449 { /* zName: */ "auto_vacuum",
15450 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
15451 /* ePragFlag: */ PragFlag_NeedSchema,
15452 /* iArg: */ 0 },
15453 #endif
15454 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15455 #if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
15456 { /* zName: */ "automatic_index",
15457 /* ePragTyp: */ PragTyp_FLAG,
15458 /* ePragFlag: */ 0,
15459 /* iArg: */ SQLITE_AutoIndex },
15460 #endif
15461 #endif
15462 { /* zName: */ "busy_timeout",
15463 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
15464 /* ePragFlag: */ 0,
15465 /* iArg: */ 0 },
15466 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
15467 { /* zName: */ "cache_size",
15468 /* ePragTyp: */ PragTyp_CACHE_SIZE,
15469 /* ePragFlag: */ PragFlag_NeedSchema,
15470 /* iArg: */ 0 },
15471 #endif
15472 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15473 { /* zName: */ "cache_spill",
15474 /* ePragTyp: */ PragTyp_CACHE_SPILL,
15475 /* ePragFlag: */ 0,
15476 /* iArg: */ 0 },
15477 #endif
15478 { /* zName: */ "case_sensitive_like",
15479 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
15480 /* ePragFlag: */ 0,
15481 /* iArg: */ 0 },
15482 { /* zName: */ "cell_size_check",
15483 /* ePragTyp: */ PragTyp_FLAG,
15484 /* ePragFlag: */ 0,
15485 /* iArg: */ SQLITE_CellSizeCk },
15486 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15487 { /* zName: */ "checkpoint_fullfsync",
15488 /* ePragTyp: */ PragTyp_FLAG,
15489 /* ePragFlag: */ 0,
15490 /* iArg: */ SQLITE_CkptFullFSync },
15491 #endif
15492 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
15493 { /* zName: */ "collation_list",
15494 /* ePragTyp: */ PragTyp_COLLATION_LIST,
15495 /* ePragFlag: */ 0,
15496 /* iArg: */ 0 },
15497 #endif
15498 #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
15499 { /* zName: */ "compile_options",
15500 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
15501 /* ePragFlag: */ 0,
15502 /* iArg: */ 0 },
15503 #endif
15504 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15505 { /* zName: */ "count_changes",
15506 /* ePragTyp: */ PragTyp_FLAG,
15507 /* ePragFlag: */ 0,
15508 /* iArg: */ SQLITE_CountRows },
15509 #endif
15510 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
15511 { /* zName: */ "data_store_directory",
15512 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
15513 /* ePragFlag: */ 0,
15514 /* iArg: */ 0 },
15515 #endif
15516 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
15517 { /* zName: */ "data_version",
15518 /* ePragTyp: */ PragTyp_HEADER_VALUE,
15519 /* ePragFlag: */ PragFlag_ReadOnly,
15520 /* iArg: */ BTREE_DATA_VERSION },
15521 #endif
15522 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
15523 { /* zName: */ "database_list",
15524 /* ePragTyp: */ PragTyp_DATABASE_LIST,
15525 /* ePragFlag: */ PragFlag_NeedSchema,
15526 /* iArg: */ 0 },
15527 #endif
15528 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
15529 { /* zName: */ "default_cache_size",
15530 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
15531 /* ePragFlag: */ PragFlag_NeedSchema,
15532 /* iArg: */ 0 },
15533 #endif
15534 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15535 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
15536 { /* zName: */ "defer_foreign_keys",
15537 /* ePragTyp: */ PragTyp_FLAG,
15538 /* ePragFlag: */ 0,
15539 /* iArg: */ SQLITE_DeferFKs },
15540 #endif
15541 #endif
15542 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15543 { /* zName: */ "empty_result_callbacks",
15544 /* ePragTyp: */ PragTyp_FLAG,
15545 /* ePragFlag: */ 0,
15546 /* iArg: */ SQLITE_NullCallback },
15547 #endif
15548 #if !defined(SQLITE_OMIT_UTF16)
15549 { /* zName: */ "encoding",
15550 /* ePragTyp: */ PragTyp_ENCODING,
15551 /* ePragFlag: */ 0,
15552 /* iArg: */ 0 },
15553 #endif
15554 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
15555 { /* zName: */ "foreign_key_check",
15556 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
15557 /* ePragFlag: */ PragFlag_NeedSchema,
15558 /* iArg: */ 0 },
15559 #endif
15560 #if !defined(SQLITE_OMIT_FOREIGN_KEY)
15561 { /* zName: */ "foreign_key_list",
15562 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
15563 /* ePragFlag: */ PragFlag_NeedSchema,
15564 /* iArg: */ 0 },
15565 #endif
15566 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15567 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
15568 { /* zName: */ "foreign_keys",
15569 /* ePragTyp: */ PragTyp_FLAG,
15570 /* ePragFlag: */ 0,
15571 /* iArg: */ SQLITE_ForeignKeys },
15572 #endif
15573 #endif
15574 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
15575 { /* zName: */ "freelist_count",
15576 /* ePragTyp: */ PragTyp_HEADER_VALUE,
15577 /* ePragFlag: */ PragFlag_ReadOnly,
15578 /* iArg: */ BTREE_FREE_PAGE_COUNT },
15579 #endif
15580 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15581 { /* zName: */ "full_column_names",
15582 /* ePragTyp: */ PragTyp_FLAG,
15583 /* ePragFlag: */ 0,
15584 /* iArg: */ SQLITE_FullColNames },
15585 { /* zName: */ "fullfsync",
15586 /* ePragTyp: */ PragTyp_FLAG,
15587 /* ePragFlag: */ 0,
15588 /* iArg: */ SQLITE_FullFSync },
15589 #endif
15590 #if defined(SQLITE_HAS_CODEC)
15591 { /* zName: */ "hexkey",
15592 /* ePragTyp: */ PragTyp_HEXKEY,
15593 /* ePragFlag: */ 0,
15594 /* iArg: */ 0 },
15595 { /* zName: */ "hexrekey",
15596 /* ePragTyp: */ PragTyp_HEXKEY,
15597 /* ePragFlag: */ 0,
15598 /* iArg: */ 0 },
15599 #endif
15600 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15601 #if !defined(SQLITE_OMIT_CHECK)
15602 { /* zName: */ "ignore_check_constraints",
15603 /* ePragTyp: */ PragTyp_FLAG,
15604 /* ePragFlag: */ 0,
15605 /* iArg: */ SQLITE_IgnoreChecks },
15606 #endif
15607 #endif
15608 #if !defined(SQLITE_OMIT_AUTOVACUUM)
15609 { /* zName: */ "incremental_vacuum",
15610 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
15611 /* ePragFlag: */ PragFlag_NeedSchema,
15612 /* iArg: */ 0 },
15613 #endif
15614 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
15615 { /* zName: */ "index_info",
15616 /* ePragTyp: */ PragTyp_INDEX_INFO,
15617 /* ePragFlag: */ PragFlag_NeedSchema,
15618 /* iArg: */ 0 },
15619 { /* zName: */ "index_list",
15620 /* ePragTyp: */ PragTyp_INDEX_LIST,
15621 /* ePragFlag: */ PragFlag_NeedSchema,
15622 /* iArg: */ 0 },
15623 { /* zName: */ "index_xinfo",
15624 /* ePragTyp: */ PragTyp_INDEX_INFO,
15625 /* ePragFlag: */ PragFlag_NeedSchema,
15626 /* iArg: */ 1 },
15627 #endif
15628 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
15629 { /* zName: */ "integrity_check",
15630 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
15631 /* ePragFlag: */ PragFlag_NeedSchema,
15632 /* iArg: */ 0 },
15633 #endif
15634 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
15635 { /* zName: */ "journal_mode",
15636 /* ePragTyp: */ PragTyp_JOURNAL_MODE,
15637 /* ePragFlag: */ PragFlag_NeedSchema,
15638 /* iArg: */ 0 },
15639 { /* zName: */ "journal_size_limit",
15640 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
15641 /* ePragFlag: */ 0,
15642 /* iArg: */ 0 },
15643 #endif
15644 #if defined(SQLITE_HAS_CODEC)
15645 { /* zName: */ "key",
15646 /* ePragTyp: */ PragTyp_KEY,
15647 /* ePragFlag: */ 0,
15648 /* iArg: */ 0 },
15649 #endif
15650 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15651 { /* zName: */ "legacy_file_format",
15652 /* ePragTyp: */ PragTyp_FLAG,
15653 /* ePragFlag: */ 0,
15654 /* iArg: */ SQLITE_LegacyFileFmt },
15655 #endif
15656 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
15657 { /* zName: */ "lock_proxy_file",
15658 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
15659 /* ePragFlag: */ 0,
15660 /* iArg: */ 0 },
15661 #endif
15662 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
15663 { /* zName: */ "lock_status",
15664 /* ePragTyp: */ PragTyp_LOCK_STATUS,
15665 /* ePragFlag: */ 0,
15666 /* iArg: */ 0 },
15667 #endif
15668 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
15669 { /* zName: */ "locking_mode",
15670 /* ePragTyp: */ PragTyp_LOCKING_MODE,
15671 /* ePragFlag: */ 0,
15672 /* iArg: */ 0 },
15673 { /* zName: */ "max_page_count",
15674 /* ePragTyp: */ PragTyp_PAGE_COUNT,
15675 /* ePragFlag: */ PragFlag_NeedSchema,
15676 /* iArg: */ 0 },
15677 { /* zName: */ "mmap_size",
15678 /* ePragTyp: */ PragTyp_MMAP_SIZE,
15679 /* ePragFlag: */ 0,
15680 /* iArg: */ 0 },
15681 { /* zName: */ "page_count",
15682 /* ePragTyp: */ PragTyp_PAGE_COUNT,
15683 /* ePragFlag: */ PragFlag_NeedSchema,
15684 /* iArg: */ 0 },
15685 { /* zName: */ "page_size",
15686 /* ePragTyp: */ PragTyp_PAGE_SIZE,
15687 /* ePragFlag: */ 0,
15688 /* iArg: */ 0 },
15689 #endif
15690 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE)
15691 { /* zName: */ "parser_trace",
15692 /* ePragTyp: */ PragTyp_PARSER_TRACE,
15693 /* ePragFlag: */ 0,
15694 /* iArg: */ 0 },
15695 #endif
15696 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15697 { /* zName: */ "query_only",
15698 /* ePragTyp: */ PragTyp_FLAG,
15699 /* ePragFlag: */ 0,
15700 /* iArg: */ SQLITE_QueryOnly },
15701 #endif
15702 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
15703 { /* zName: */ "quick_check",
15704 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
15705 /* ePragFlag: */ PragFlag_NeedSchema,
15706 /* iArg: */ 0 },
15707 #endif
15708 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15709 { /* zName: */ "read_uncommitted",
15710 /* ePragTyp: */ PragTyp_FLAG,
15711 /* ePragFlag: */ 0,
15712 /* iArg: */ SQLITE_ReadUncommitted },
15713 { /* zName: */ "recursive_triggers",
15714 /* ePragTyp: */ PragTyp_FLAG,
15715 /* ePragFlag: */ 0,
15716 /* iArg: */ SQLITE_RecTriggers },
15717 #endif
15718 #if defined(SQLITE_HAS_CODEC)
15719 { /* zName: */ "rekey",
15720 /* ePragTyp: */ PragTyp_REKEY,
15721 /* ePragFlag: */ 0,
15722 /* iArg: */ 0 },
15723 #endif
15724 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15725 { /* zName: */ "reverse_unordered_selects",
15726 /* ePragTyp: */ PragTyp_FLAG,
15727 /* ePragFlag: */ 0,
15728 /* iArg: */ SQLITE_ReverseOrder },
15729 #endif
15730 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
15731 { /* zName: */ "schema_version",
15732 /* ePragTyp: */ PragTyp_HEADER_VALUE,
15733 /* ePragFlag: */ 0,
15734 /* iArg: */ BTREE_SCHEMA_VERSION },
15735 #endif
15736 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
15737 { /* zName: */ "secure_delete",
15738 /* ePragTyp: */ PragTyp_SECURE_DELETE,
15739 /* ePragFlag: */ 0,
15740 /* iArg: */ 0 },
15741 #endif
15742 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15743 { /* zName: */ "short_column_names",
15744 /* ePragTyp: */ PragTyp_FLAG,
15745 /* ePragFlag: */ 0,
15746 /* iArg: */ SQLITE_ShortColNames },
15747 #endif
15748 { /* zName: */ "shrink_memory",
15749 /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
15750 /* ePragFlag: */ 0,
15751 /* iArg: */ 0 },
15752 { /* zName: */ "soft_heap_limit",
15753 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
15754 /* ePragFlag: */ 0,
15755 /* iArg: */ 0 },
15756 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15757 #if defined(SQLITE_DEBUG)
15758 { /* zName: */ "sql_trace",
15759 /* ePragTyp: */ PragTyp_FLAG,
15760 /* ePragFlag: */ 0,
15761 /* iArg: */ SQLITE_SqlTrace },
15762 #endif
15763 #endif
15764 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
15765 { /* zName: */ "stats",
15766 /* ePragTyp: */ PragTyp_STATS,
15767 /* ePragFlag: */ PragFlag_NeedSchema,
15768 /* iArg: */ 0 },
15769 #endif
15770 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
15771 { /* zName: */ "synchronous",
15772 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
15773 /* ePragFlag: */ PragFlag_NeedSchema,
15774 /* iArg: */ 0 },
15775 #endif
15776 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
15777 { /* zName: */ "table_info",
15778 /* ePragTyp: */ PragTyp_TABLE_INFO,
15779 /* ePragFlag: */ PragFlag_NeedSchema,
15780 /* iArg: */ 0 },
15781 #endif
15782 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
15783 { /* zName: */ "temp_store",
15784 /* ePragTyp: */ PragTyp_TEMP_STORE,
15785 /* ePragFlag: */ 0,
15786 /* iArg: */ 0 },
15787 { /* zName: */ "temp_store_directory",
15788 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
15789 /* ePragFlag: */ 0,
15790 /* iArg: */ 0 },
15791 #endif
15792 { /* zName: */ "threads",
15793 /* ePragTyp: */ PragTyp_THREADS,
15794 /* ePragFlag: */ 0,
15795 /* iArg: */ 0 },
15796 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
15797 { /* zName: */ "user_version",
15798 /* ePragTyp: */ PragTyp_HEADER_VALUE,
15799 /* ePragFlag: */ 0,
15800 /* iArg: */ BTREE_USER_VERSION },
15801 #endif
15802 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15803 #if defined(SQLITE_DEBUG)
15804 { /* zName: */ "vdbe_addoptrace",
15805 /* ePragTyp: */ PragTyp_FLAG,
15806 /* ePragFlag: */ 0,
15807 /* iArg: */ SQLITE_VdbeAddopTrace },
15808 { /* zName: */ "vdbe_debug",
15809 /* ePragTyp: */ PragTyp_FLAG,
15810 /* ePragFlag: */ 0,
15811 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
15812 { /* zName: */ "vdbe_eqp",
15813 /* ePragTyp: */ PragTyp_FLAG,
15814 /* ePragFlag: */ 0,
15815 /* iArg: */ SQLITE_VdbeEQP },
15816 { /* zName: */ "vdbe_listing",
15817 /* ePragTyp: */ PragTyp_FLAG,
15818 /* ePragFlag: */ 0,
15819 /* iArg: */ SQLITE_VdbeListing },
15820 { /* zName: */ "vdbe_trace",
15821 /* ePragTyp: */ PragTyp_FLAG,
15822 /* ePragFlag: */ 0,
15823 /* iArg: */ SQLITE_VdbeTrace },
15824 #endif
15825 #endif
15826 #if !defined(SQLITE_OMIT_WAL)
15827 { /* zName: */ "wal_autocheckpoint",
15828 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
15829 /* ePragFlag: */ 0,
15830 /* iArg: */ 0 },
15831 { /* zName: */ "wal_checkpoint",
15832 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
15833 /* ePragFlag: */ PragFlag_NeedSchema,
15834 /* iArg: */ 0 },
15835 #endif
15836 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
15837 { /* zName: */ "writable_schema",
15838 /* ePragTyp: */ PragTyp_FLAG,
15839 /* ePragFlag: */ 0,
15840 /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
15841 #endif
15842 };
15843 /* Number of pragmas: 60 on by default, 73 total. */
15844
15845 /************** End of pragma.h **********************************************/
15846 /************** Continuing where we left off in pragma.c *********************/
15847
15848 /*
15849 ** Interpret the given string as a safety level. Return 0 for OFF,
15850 ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
15851 ** unrecognized string argument. The FULL option is disallowed
15852 ** if the omitFull parameter it 1.
15853 **
15854 ** Note that the values returned are one less that the values that
15855 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
15856 ** to support legacy SQL code. The safety level used to be boolean
15857 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
15858 */
15859 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
15860 /* 123456789 123456789 */
15861 static const char zText[] = "onoffalseyestruefull";
15862 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
15863 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
15864 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
15865 int i, n;
15866 if( sqlite3Isdigit(*z) ){
15867 return (u8)sqlite3Atoi(z);
15868 }
15869 n = sqlite3Strlen30(z);
15870 for(i=0; i<ArraySize(iLength)-omitFull; i++){
15871 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
15872 return iValue[i];
15873 }
15874 }
15875 return dflt;
15876 }
15877
15878 /*
15879 ** Interpret the given string as a boolean value.
15880 */
15881 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
15882 return getSafetyLevel(z,1,dflt)!=0;
15883 }
15884
15885 /* The sqlite3GetBoolean() function is used by other modules but the
15886 ** remainder of this file is specific to PRAGMA processing. So omit
15887 ** the rest of the file if PRAGMAs are omitted from the build.
15888 */
15889 #if !defined(SQLITE_OMIT_PRAGMA)
15890
15891 /*
15892 ** Interpret the given string as a locking mode value.
15893 */
15894 static int getLockingMode(const char *z){
15895 if( z ){
15896 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
15897 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
15898 }
15899 return PAGER_LOCKINGMODE_QUERY;
15900 }
15901
15902 #ifndef SQLITE_OMIT_AUTOVACUUM
15903 /*
15904 ** Interpret the given string as an auto-vacuum mode value.
15905 **
15906 ** The following strings, "none", "full" and "incremental" are
15907 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
15908 */
15909 static int getAutoVacuum(const char *z){
15910 int i;
15911 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
15912 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
15913 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
15914 i = sqlite3Atoi(z);
15915 return (u8)((i>=0&&i<=2)?i:0);
15916 }
15917 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
15918
15919 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
15920 /*
15921 ** Interpret the given string as a temp db location. Return 1 for file
15922 ** backed temporary databases, 2 for the Red-Black tree in memory database
15923 ** and 0 to use the compile-time default.
15924 */
15925 static int getTempStore(const char *z){
15926 if( z[0]>='0' && z[0]<='2' ){
15927 return z[0] - '0';
15928 }else if( sqlite3StrICmp(z, "file")==0 ){
15929 return 1;
15930 }else if( sqlite3StrICmp(z, "memory")==0 ){
15931 return 2;
15932 }else{
15933 return 0;
15934 }
15935 }
15936 #endif /* SQLITE_PAGER_PRAGMAS */
15937
15938 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
15939 /*
15940 ** Invalidate temp storage, either when the temp storage is changed
15941 ** from default, or when 'file' and the temp_store_directory has changed
15942 */
15943 static int invalidateTempStorage(Parse *pParse){
15944 sqlite3 *db = pParse->db;
15945 if( db->aDb[1].pBt!=0 ){
15946 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
15947 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
15948 "from within a transaction");
15949 return SQLITE_ERROR;
15950 }
15951 sqlite3BtreeClose(db->aDb[1].pBt);
15952 db->aDb[1].pBt = 0;
15953 sqlite3ResetAllSchemasOfConnection(db);
15954 }
15955 return SQLITE_OK;
15956 }
15957 #endif /* SQLITE_PAGER_PRAGMAS */
15958
15959 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
15960 /*
15961 ** If the TEMP database is open, close it and mark the database schema
15962 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
15963 ** or DEFAULT_TEMP_STORE pragmas.
15964 */
15965 static int changeTempStorage(Parse *pParse, const char *zStorageType){
15966 int ts = getTempStore(zStorageType);
15967 sqlite3 *db = pParse->db;
15968 if( db->temp_store==ts ) return SQLITE_OK;
15969 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
15970 return SQLITE_ERROR;
15971 }
15972 db->temp_store = (u8)ts;
15973 return SQLITE_OK;
15974 }
15975 #endif /* SQLITE_PAGER_PRAGMAS */
15976
15977 /*
15978 ** Set the names of the first N columns to the values in azCol[]
15979 */
15980 static void setAllColumnNames(
15981 Vdbe *v, /* The query under construction */
15982 int N, /* Number of columns */
15983 const char **azCol /* Names of columns */
15984 ){
15985 int i;
15986 sqlite3VdbeSetNumCols(v, N);
15987 for(i=0; i<N; i++){
15988 sqlite3VdbeSetColName(v, i, COLNAME_NAME, azCol[i], SQLITE_STATIC);
15989 }
15990 }
15991 static void setOneColumnName(Vdbe *v, const char *z){
15992 setAllColumnNames(v, 1, &z);
15993 }
15994
15995 /*
15996 ** Generate code to return a single integer value.
15997 */
15998 static void returnSingleInt(Vdbe *v, const char *zLabel, i64 value){
15999 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
16000 setOneColumnName(v, zLabel);
16001 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
16002 }
16003
16004 /*
16005 ** Generate code to return a single text value.
16006 */
16007 static void returnSingleText(
16008 Vdbe *v, /* Prepared statement under construction */
16009 const char *zLabel, /* Name of the result column */
16010 const char *zValue /* Value to be returned */
16011 ){
16012 if( zValue ){
16013 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
16014 setOneColumnName(v, zLabel);
16015 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
16016 }
16017 }
16018
16019
16020 /*
16021 ** Set the safety_level and pager flags for pager iDb. Or if iDb<0
16022 ** set these values for all pagers.
16023 */
16024 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
16025 static void setAllPagerFlags(sqlite3 *db){
16026 if( db->autoCommit ){
16027 Db *pDb = db->aDb;
16028 int n = db->nDb;
16029 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
16030 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
16031 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
16032 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
16033 == PAGER_FLAGS_MASK );
16034 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
16035 while( (n--) > 0 ){
16036 if( pDb->pBt ){
16037 sqlite3BtreeSetPagerFlags(pDb->pBt,
16038 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
16039 }
16040 pDb++;
16041 }
16042 }
16043 }
16044 #else
16045 # define setAllPagerFlags(X) /* no-op */
16046 #endif
16047
16048
16049 /*
16050 ** Return a human-readable name for a constraint resolution action.
16051 */
16052 #ifndef SQLITE_OMIT_FOREIGN_KEY
16053 static const char *actionName(u8 action){
16054 const char *zName;
16055 switch( action ){
16056 case OE_SetNull: zName = "SET NULL"; break;
16057 case OE_SetDflt: zName = "SET DEFAULT"; break;
16058 case OE_Cascade: zName = "CASCADE"; break;
16059 case OE_Restrict: zName = "RESTRICT"; break;
16060 default: zName = "NO ACTION";
16061 assert( action==OE_None ); break;
16062 }
16063 return zName;
16064 }
16065 #endif
16066
16067
16068 /*
16069 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
16070 ** defined in pager.h. This function returns the associated lowercase
16071 ** journal-mode name.
16072 */
16073 SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
16074 static char * const azModeName[] = {
16075 "delete", "persist", "off", "truncate", "memory"
16076 #ifndef SQLITE_OMIT_WAL
16077 , "wal"
16078 #endif
16079 };
16080 assert( PAGER_JOURNALMODE_DELETE==0 );
16081 assert( PAGER_JOURNALMODE_PERSIST==1 );
16082 assert( PAGER_JOURNALMODE_OFF==2 );
16083 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
16084 assert( PAGER_JOURNALMODE_MEMORY==4 );
16085 assert( PAGER_JOURNALMODE_WAL==5 );
16086 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
16087
16088 if( eMode==ArraySize(azModeName) ) return 0;
16089 return azModeName[eMode];
16090 }
16091
16092 /*
16093 ** Process a pragma statement.
16094 **
16095 ** Pragmas are of this form:
16096 **
16097 ** PRAGMA [schema.]id [= value]
16098 **
16099 ** The identifier might also be a string. The value is a string, and
16100 ** identifier, or a number. If minusFlag is true, then the value is
16101 ** a number that was preceded by a minus sign.
16102 **
16103 ** If the left side is "database.id" then pId1 is the database name
16104 ** and pId2 is the id. If the left side is just "id" then pId1 is the
16105 ** id and pId2 is any empty string.
16106 */
16107 SQLITE_PRIVATE void sqlite3Pragma(
16108 Parse *pParse,
16109 Token *pId1, /* First part of [schema.]id field */
16110 Token *pId2, /* Second part of [schema.]id field, or NULL */
16111 Token *pValue, /* Token for <value>, or NULL */
16112 int minusFlag /* True if a '-' sign preceded <value> */
16113 ){
16114 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
16115 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
16116 const char *zDb = 0; /* The database name */
16117 Token *pId; /* Pointer to <id> token */
16118 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
16119 int iDb; /* Database index for <database> */
16120 int lwr, upr, mid = 0; /* Binary search bounds */
16121 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
16122 sqlite3 *db = pParse->db; /* The database connection */
16123 Db *pDb; /* The specific database being pragmaed */
16124 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
16125 const struct sPragmaNames *pPragma;
16126
16127 if( v==0 ) return;
16128 sqlite3VdbeRunOnlyOnce(v);
16129 pParse->nMem = 2;
16130
16131 /* Interpret the [schema.] part of the pragma statement. iDb is the
16132 ** index of the database this pragma is being applied to in db.aDb[]. */
16133 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
16134 if( iDb<0 ) return;
16135 pDb = &db->aDb[iDb];
16136
16137 /* If the temp database has been explicitly named as part of the
16138 ** pragma, make sure it is open.
16139 */
16140 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
16141 return;
16142 }
16143
16144 zLeft = sqlite3NameFromToken(db, pId);
16145 if( !zLeft ) return;
16146 if( minusFlag ){
16147 zRight = sqlite3MPrintf(db, "-%T", pValue);
16148 }else{
16149 zRight = sqlite3NameFromToken(db, pValue);
16150 }
16151
16152 assert( pId2 );
16153 zDb = pId2->n>0 ? pDb->zName : 0;
16154 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
16155 goto pragma_out;
16156 }
16157
16158 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
16159 ** connection. If it returns SQLITE_OK, then assume that the VFS
16160 ** handled the pragma and generate a no-op prepared statement.
16161 **
16162 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
16163 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
16164 ** object corresponding to the database file to which the pragma
16165 ** statement refers.
16166 **
16167 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
16168 ** file control is an array of pointers to strings (char**) in which the
16169 ** second element of the array is the name of the pragma and the third
16170 ** element is the argument to the pragma or NULL if the pragma has no
16171 ** argument.
16172 */
16173 aFcntl[0] = 0;
16174 aFcntl[1] = zLeft;
16175 aFcntl[2] = zRight;
16176 aFcntl[3] = 0;
16177 db->busyHandler.nBusy = 0;
16178 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
16179 if( rc==SQLITE_OK ){
16180 returnSingleText(v, "result", aFcntl[0]);
16181 sqlite3_free(aFcntl[0]);
16182 goto pragma_out;
16183 }
16184 if( rc!=SQLITE_NOTFOUND ){
16185 if( aFcntl[0] ){
16186 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
16187 sqlite3_free(aFcntl[0]);
16188 }
16189 pParse->nErr++;
16190 pParse->rc = rc;
16191 goto pragma_out;
16192 }
16193
16194 /* Locate the pragma in the lookup table */
16195 lwr = 0;
16196 upr = ArraySize(aPragmaNames)-1;
16197 while( lwr<=upr ){
16198 mid = (lwr+upr)/2;
16199 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
16200 if( rc==0 ) break;
16201 if( rc<0 ){
16202 upr = mid - 1;
16203 }else{
16204 lwr = mid + 1;
16205 }
16206 }
16207 if( lwr>upr ) goto pragma_out;
16208 pPragma = &aPragmaNames[mid];
16209
16210 /* Make sure the database schema is loaded if the pragma requires that */
16211 if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){
16212 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
16213 }
16214
16215 /* Jump to the appropriate pragma handler */
16216 switch( pPragma->ePragTyp ){
16217
16218 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
16219 /*
16220 ** PRAGMA [schema.]default_cache_size
16221 ** PRAGMA [schema.]default_cache_size=N
16222 **
16223 ** The first form reports the current persistent setting for the
16224 ** page cache size. The value returned is the maximum number of
16225 ** pages in the page cache. The second form sets both the current
16226 ** page cache size value and the persistent page cache size value
16227 ** stored in the database file.
16228 **
16229 ** Older versions of SQLite would set the default cache size to a
16230 ** negative number to indicate synchronous=OFF. These days, synchronous
16231 ** is always on by default regardless of the sign of the default cache
16232 ** size. But continue to take the absolute value of the default cache
16233 ** size of historical compatibility.
16234 */
16235 case PragTyp_DEFAULT_CACHE_SIZE: {
16236 static const int iLn = VDBE_OFFSET_LINENO(2);
16237 static const VdbeOpList getCacheSize[] = {
16238 { OP_Transaction, 0, 0, 0}, /* 0 */
16239 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
16240 { OP_IfPos, 1, 8, 0},
16241 { OP_Integer, 0, 2, 0},
16242 { OP_Subtract, 1, 2, 1},
16243 { OP_IfPos, 1, 8, 0},
16244 { OP_Integer, 0, 1, 0}, /* 6 */
16245 { OP_Noop, 0, 0, 0},
16246 { OP_ResultRow, 1, 1, 0},
16247 };
16248 int addr;
16249 sqlite3VdbeUsesBtree(v, iDb);
16250 if( !zRight ){
16251 setOneColumnName(v, "cache_size");
16252 pParse->nMem += 2;
16253 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
16254 sqlite3VdbeChangeP1(v, addr, iDb);
16255 sqlite3VdbeChangeP1(v, addr+1, iDb);
16256 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
16257 }else{
16258 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
16259 sqlite3BeginWriteOperation(pParse, 0, iDb);
16260 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
16261 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
16262 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
16263 pDb->pSchema->cache_size = size;
16264 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
16265 }
16266 break;
16267 }
16268 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
16269
16270 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
16271 /*
16272 ** PRAGMA [schema.]page_size
16273 ** PRAGMA [schema.]page_size=N
16274 **
16275 ** The first form reports the current setting for the
16276 ** database page size in bytes. The second form sets the
16277 ** database page size value. The value can only be set if
16278 ** the database has not yet been created.
16279 */
16280 case PragTyp_PAGE_SIZE: {
16281 Btree *pBt = pDb->pBt;
16282 assert( pBt!=0 );
16283 if( !zRight ){
16284 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
16285 returnSingleInt(v, "page_size", size);
16286 }else{
16287 /* Malloc may fail when setting the page-size, as there is an internal
16288 ** buffer that the pager module resizes using sqlite3_realloc().
16289 */
16290 db->nextPagesize = sqlite3Atoi(zRight);
16291 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
16292 db->mallocFailed = 1;
16293 }
16294 }
16295 break;
16296 }
16297
16298 /*
16299 ** PRAGMA [schema.]secure_delete
16300 ** PRAGMA [schema.]secure_delete=ON/OFF
16301 **
16302 ** The first form reports the current setting for the
16303 ** secure_delete flag. The second form changes the secure_delete
16304 ** flag setting and reports thenew value.
16305 */
16306 case PragTyp_SECURE_DELETE: {
16307 Btree *pBt = pDb->pBt;
16308 int b = -1;
16309 assert( pBt!=0 );
16310 if( zRight ){
16311 b = sqlite3GetBoolean(zRight, 0);
16312 }
16313 if( pId2->n==0 && b>=0 ){
16314 int ii;
16315 for(ii=0; ii<db->nDb; ii++){
16316 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
16317 }
16318 }
16319 b = sqlite3BtreeSecureDelete(pBt, b);
16320 returnSingleInt(v, "secure_delete", b);
16321 break;
16322 }
16323
16324 /*
16325 ** PRAGMA [schema.]max_page_count
16326 ** PRAGMA [schema.]max_page_count=N
16327 **
16328 ** The first form reports the current setting for the
16329 ** maximum number of pages in the database file. The
16330 ** second form attempts to change this setting. Both
16331 ** forms return the current setting.
16332 **
16333 ** The absolute value of N is used. This is undocumented and might
16334 ** change. The only purpose is to provide an easy way to test
16335 ** the sqlite3AbsInt32() function.
16336 **
16337 ** PRAGMA [schema.]page_count
16338 **
16339 ** Return the number of pages in the specified database.
16340 */
16341 case PragTyp_PAGE_COUNT: {
16342 int iReg;
16343 sqlite3CodeVerifySchema(pParse, iDb);
16344 iReg = ++pParse->nMem;
16345 if( sqlite3Tolower(zLeft[0])=='p' ){
16346 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
16347 }else{
16348 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
16349 sqlite3AbsInt32(sqlite3Atoi(zRight)));
16350 }
16351 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
16352 sqlite3VdbeSetNumCols(v, 1);
16353 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
16354 break;
16355 }
16356
16357 /*
16358 ** PRAGMA [schema.]locking_mode
16359 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
16360 */
16361 case PragTyp_LOCKING_MODE: {
16362 const char *zRet = "normal";
16363 int eMode = getLockingMode(zRight);
16364
16365 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
16366 /* Simple "PRAGMA locking_mode;" statement. This is a query for
16367 ** the current default locking mode (which may be different to
16368 ** the locking-mode of the main database).
16369 */
16370 eMode = db->dfltLockMode;
16371 }else{
16372 Pager *pPager;
16373 if( pId2->n==0 ){
16374 /* This indicates that no database name was specified as part
16375 ** of the PRAGMA command. In this case the locking-mode must be
16376 ** set on all attached databases, as well as the main db file.
16377 **
16378 ** Also, the sqlite3.dfltLockMode variable is set so that
16379 ** any subsequently attached databases also use the specified
16380 ** locking mode.
16381 */
16382 int ii;
16383 assert(pDb==&db->aDb[0]);
16384 for(ii=2; ii<db->nDb; ii++){
16385 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
16386 sqlite3PagerLockingMode(pPager, eMode);
16387 }
16388 db->dfltLockMode = (u8)eMode;
16389 }
16390 pPager = sqlite3BtreePager(pDb->pBt);
16391 eMode = sqlite3PagerLockingMode(pPager, eMode);
16392 }
16393
16394 assert( eMode==PAGER_LOCKINGMODE_NORMAL
16395 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
16396 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
16397 zRet = "exclusive";
16398 }
16399 returnSingleText(v, "locking_mode", zRet);
16400 break;
16401 }
16402
16403 /*
16404 ** PRAGMA [schema.]journal_mode
16405 ** PRAGMA [schema.]journal_mode =
16406 ** (delete|persist|off|truncate|memory|wal|off)
16407 */
16408 case PragTyp_JOURNAL_MODE: {
16409 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
16410 int ii; /* Loop counter */
16411
16412 setOneColumnName(v, "journal_mode");
16413 if( zRight==0 ){
16414 /* If there is no "=MODE" part of the pragma, do a query for the
16415 ** current mode */
16416 eMode = PAGER_JOURNALMODE_QUERY;
16417 }else{
16418 const char *zMode;
16419 int n = sqlite3Strlen30(zRight);
16420 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
16421 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
16422 }
16423 if( !zMode ){
16424 /* If the "=MODE" part does not match any known journal mode,
16425 ** then do a query */
16426 eMode = PAGER_JOURNALMODE_QUERY;
16427 }
16428 }
16429 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
16430 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
16431 iDb = 0;
16432 pId2->n = 1;
16433 }
16434 for(ii=db->nDb-1; ii>=0; ii--){
16435 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
16436 sqlite3VdbeUsesBtree(v, ii);
16437 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
16438 }
16439 }
16440 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
16441 break;
16442 }
16443
16444 /*
16445 ** PRAGMA [schema.]journal_size_limit
16446 ** PRAGMA [schema.]journal_size_limit=N
16447 **
16448 ** Get or set the size limit on rollback journal files.
16449 */
16450 case PragTyp_JOURNAL_SIZE_LIMIT: {
16451 Pager *pPager = sqlite3BtreePager(pDb->pBt);
16452 i64 iLimit = -2;
16453 if( zRight ){
16454 sqlite3DecOrHexToI64(zRight, &iLimit);
16455 if( iLimit<-1 ) iLimit = -1;
16456 }
16457 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
16458 returnSingleInt(v, "journal_size_limit", iLimit);
16459 break;
16460 }
16461
16462 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
16463
16464 /*
16465 ** PRAGMA [schema.]auto_vacuum
16466 ** PRAGMA [schema.]auto_vacuum=N
16467 **
16468 ** Get or set the value of the database 'auto-vacuum' parameter.
16469 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
16470 */
16471 #ifndef SQLITE_OMIT_AUTOVACUUM
16472 case PragTyp_AUTO_VACUUM: {
16473 Btree *pBt = pDb->pBt;
16474 assert( pBt!=0 );
16475 if( !zRight ){
16476 returnSingleInt(v, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
16477 }else{
16478 int eAuto = getAutoVacuum(zRight);
16479 assert( eAuto>=0 && eAuto<=2 );
16480 db->nextAutovac = (u8)eAuto;
16481 /* Call SetAutoVacuum() to set initialize the internal auto and
16482 ** incr-vacuum flags. This is required in case this connection
16483 ** creates the database file. It is important that it is created
16484 ** as an auto-vacuum capable db.
16485 */
16486 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
16487 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
16488 /* When setting the auto_vacuum mode to either "full" or
16489 ** "incremental", write the value of meta[6] in the database
16490 ** file. Before writing to meta[6], check that meta[3] indicates
16491 ** that this really is an auto-vacuum capable database.
16492 */
16493 static const int iLn = VDBE_OFFSET_LINENO(2);
16494 static const VdbeOpList setMeta6[] = {
16495 { OP_Transaction, 0, 1, 0}, /* 0 */
16496 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
16497 { OP_If, 1, 0, 0}, /* 2 */
16498 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
16499 { OP_Integer, 0, 1, 0}, /* 4 */
16500 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
16501 };
16502 int iAddr;
16503 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
16504 sqlite3VdbeChangeP1(v, iAddr, iDb);
16505 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
16506 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
16507 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
16508 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
16509 sqlite3VdbeUsesBtree(v, iDb);
16510 }
16511 }
16512 break;
16513 }
16514 #endif
16515
16516 /*
16517 ** PRAGMA [schema.]incremental_vacuum(N)
16518 **
16519 ** Do N steps of incremental vacuuming on a database.
16520 */
16521 #ifndef SQLITE_OMIT_AUTOVACUUM
16522 case PragTyp_INCREMENTAL_VACUUM: {
16523 int iLimit, addr;
16524 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
16525 iLimit = 0x7fffffff;
16526 }
16527 sqlite3BeginWriteOperation(pParse, 0, iDb);
16528 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
16529 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
16530 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
16531 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
16532 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
16533 sqlite3VdbeJumpHere(v, addr);
16534 break;
16535 }
16536 #endif
16537
16538 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
16539 /*
16540 ** PRAGMA [schema.]cache_size
16541 ** PRAGMA [schema.]cache_size=N
16542 **
16543 ** The first form reports the current local setting for the
16544 ** page cache size. The second form sets the local
16545 ** page cache size value. If N is positive then that is the
16546 ** number of pages in the cache. If N is negative, then the
16547 ** number of pages is adjusted so that the cache uses -N kibibytes
16548 ** of memory.
16549 */
16550 case PragTyp_CACHE_SIZE: {
16551 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
16552 if( !zRight ){
16553 returnSingleInt(v, "cache_size", pDb->pSchema->cache_size);
16554 }else{
16555 int size = sqlite3Atoi(zRight);
16556 pDb->pSchema->cache_size = size;
16557 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
16558 }
16559 break;
16560 }
16561
16562 /*
16563 ** PRAGMA [schema.]cache_spill
16564 ** PRAGMA cache_spill=BOOLEAN
16565 ** PRAGMA [schema.]cache_spill=N
16566 **
16567 ** The first form reports the current local setting for the
16568 ** page cache spill size. The second form turns cache spill on
16569 ** or off. When turnning cache spill on, the size is set to the
16570 ** current cache_size. The third form sets a spill size that
16571 ** may be different form the cache size.
16572 ** If N is positive then that is the
16573 ** number of pages in the cache. If N is negative, then the
16574 ** number of pages is adjusted so that the cache uses -N kibibytes
16575 ** of memory.
16576 **
16577 ** If the number of cache_spill pages is less then the number of
16578 ** cache_size pages, no spilling occurs until the page count exceeds
16579 ** the number of cache_size pages.
16580 **
16581 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
16582 ** not just the schema specified.
16583 */
16584 case PragTyp_CACHE_SPILL: {
16585 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
16586 if( !zRight ){
16587 returnSingleInt(v, "cache_spill",
16588 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
16589 sqlite3BtreeSetSpillSize(pDb->pBt,0));
16590 }else{
16591 int size = 1;
16592 if( sqlite3GetInt32(zRight, &size) ){
16593 sqlite3BtreeSetSpillSize(pDb->pBt, size);
16594 }
16595 if( sqlite3GetBoolean(zRight, size!=0) ){
16596 db->flags |= SQLITE_CacheSpill;
16597 }else{
16598 db->flags &= ~SQLITE_CacheSpill;
16599 }
16600 setAllPagerFlags(db);
16601 }
16602 break;
16603 }
16604
16605 /*
16606 ** PRAGMA [schema.]mmap_size(N)
16607 **
16608 ** Used to set mapping size limit. The mapping size limit is
16609 ** used to limit the aggregate size of all memory mapped regions of the
16610 ** database file. If this parameter is set to zero, then memory mapping
16611 ** is not used at all. If N is negative, then the default memory map
16612 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
16613 ** The parameter N is measured in bytes.
16614 **
16615 ** This value is advisory. The underlying VFS is free to memory map
16616 ** as little or as much as it wants. Except, if N is set to 0 then the
16617 ** upper layers will never invoke the xFetch interfaces to the VFS.
16618 */
16619 case PragTyp_MMAP_SIZE: {
16620 sqlite3_int64 sz;
16621 #if SQLITE_MAX_MMAP_SIZE>0
16622 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
16623 if( zRight ){
16624 int ii;
16625 sqlite3DecOrHexToI64(zRight, &sz);
16626 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
16627 if( pId2->n==0 ) db->szMmap = sz;
16628 for(ii=db->nDb-1; ii>=0; ii--){
16629 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
16630 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
16631 }
16632 }
16633 }
16634 sz = -1;
16635 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
16636 #else
16637 sz = 0;
16638 rc = SQLITE_OK;
16639 #endif
16640 if( rc==SQLITE_OK ){
16641 returnSingleInt(v, "mmap_size", sz);
16642 }else if( rc!=SQLITE_NOTFOUND ){
16643 pParse->nErr++;
16644 pParse->rc = rc;
16645 }
16646 break;
16647 }
16648
16649 /*
16650 ** PRAGMA temp_store
16651 ** PRAGMA temp_store = "default"|"memory"|"file"
16652 **
16653 ** Return or set the local value of the temp_store flag. Changing
16654 ** the local value does not make changes to the disk file and the default
16655 ** value will be restored the next time the database is opened.
16656 **
16657 ** Note that it is possible for the library compile-time options to
16658 ** override this setting
16659 */
16660 case PragTyp_TEMP_STORE: {
16661 if( !zRight ){
16662 returnSingleInt(v, "temp_store", db->temp_store);
16663 }else{
16664 changeTempStorage(pParse, zRight);
16665 }
16666 break;
16667 }
16668
16669 /*
16670 ** PRAGMA temp_store_directory
16671 ** PRAGMA temp_store_directory = ""|"directory_name"
16672 **
16673 ** Return or set the local value of the temp_store_directory flag. Changing
16674 ** the value sets a specific directory to be used for temporary files.
16675 ** Setting to a null string reverts to the default temporary directory search.
16676 ** If temporary directory is changed, then invalidateTempStorage.
16677 **
16678 */
16679 case PragTyp_TEMP_STORE_DIRECTORY: {
16680 if( !zRight ){
16681 returnSingleText(v, "temp_store_directory", sqlite3_temp_directory);
16682 }else{
16683 #ifndef SQLITE_OMIT_WSD
16684 if( zRight[0] ){
16685 int res;
16686 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
16687 if( rc!=SQLITE_OK || res==0 ){
16688 sqlite3ErrorMsg(pParse, "not a writable directory");
16689 goto pragma_out;
16690 }
16691 }
16692 if( SQLITE_TEMP_STORE==0
16693 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
16694 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
16695 ){
16696 invalidateTempStorage(pParse);
16697 }
16698 sqlite3_free(sqlite3_temp_directory);
16699 if( zRight[0] ){
16700 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
16701 }else{
16702 sqlite3_temp_directory = 0;
16703 }
16704 #endif /* SQLITE_OMIT_WSD */
16705 }
16706 break;
16707 }
16708
16709 #if SQLITE_OS_WIN
16710 /*
16711 ** PRAGMA data_store_directory
16712 ** PRAGMA data_store_directory = ""|"directory_name"
16713 **
16714 ** Return or set the local value of the data_store_directory flag. Changing
16715 ** the value sets a specific directory to be used for database files that
16716 ** were specified with a relative pathname. Setting to a null string reverts
16717 ** to the default database directory, which for database files specified with
16718 ** a relative path will probably be based on the current directory for the
16719 ** process. Database file specified with an absolute path are not impacted
16720 ** by this setting, regardless of its value.
16721 **
16722 */
16723 case PragTyp_DATA_STORE_DIRECTORY: {
16724 if( !zRight ){
16725 returnSingleText(v, "data_store_directory", sqlite3_data_directory);
16726 }else{
16727 #ifndef SQLITE_OMIT_WSD
16728 if( zRight[0] ){
16729 int res;
16730 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
16731 if( rc!=SQLITE_OK || res==0 ){
16732 sqlite3ErrorMsg(pParse, "not a writable directory");
16733 goto pragma_out;
16734 }
16735 }
16736 sqlite3_free(sqlite3_data_directory);
16737 if( zRight[0] ){
16738 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
16739 }else{
16740 sqlite3_data_directory = 0;
16741 }
16742 #endif /* SQLITE_OMIT_WSD */
16743 }
16744 break;
16745 }
16746 #endif
16747
16748 #if SQLITE_ENABLE_LOCKING_STYLE
16749 /*
16750 ** PRAGMA [schema.]lock_proxy_file
16751 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
16752 **
16753 ** Return or set the value of the lock_proxy_file flag. Changing
16754 ** the value sets a specific file to be used for database access locks.
16755 **
16756 */
16757 case PragTyp_LOCK_PROXY_FILE: {
16758 if( !zRight ){
16759 Pager *pPager = sqlite3BtreePager(pDb->pBt);
16760 char *proxy_file_path = NULL;
16761 sqlite3_file *pFile = sqlite3PagerFile(pPager);
16762 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
16763 &proxy_file_path);
16764 returnSingleText(v, "lock_proxy_file", proxy_file_path);
16765 }else{
16766 Pager *pPager = sqlite3BtreePager(pDb->pBt);
16767 sqlite3_file *pFile = sqlite3PagerFile(pPager);
16768 int res;
16769 if( zRight[0] ){
16770 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
16771 zRight);
16772 } else {
16773 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
16774 NULL);
16775 }
16776 if( res!=SQLITE_OK ){
16777 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
16778 goto pragma_out;
16779 }
16780 }
16781 break;
16782 }
16783 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
16784
16785 /*
16786 ** PRAGMA [schema.]synchronous
16787 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL
16788 **
16789 ** Return or set the local value of the synchronous flag. Changing
16790 ** the local value does not make changes to the disk file and the
16791 ** default value will be restored the next time the database is
16792 ** opened.
16793 */
16794 case PragTyp_SYNCHRONOUS: {
16795 if( !zRight ){
16796 returnSingleInt(v, "synchronous", pDb->safety_level-1);
16797 }else{
16798 if( !db->autoCommit ){
16799 sqlite3ErrorMsg(pParse,
16800 "Safety level may not be changed inside a transaction");
16801 }else{
16802 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
16803 if( iLevel==0 ) iLevel = 1;
16804 pDb->safety_level = iLevel;
16805 setAllPagerFlags(db);
16806 }
16807 }
16808 break;
16809 }
16810 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
16811
16812 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
16813 case PragTyp_FLAG: {
16814 if( zRight==0 ){
16815 returnSingleInt(v, pPragma->zName, (db->flags & pPragma->iArg)!=0 );
16816 }else{
16817 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
16818 if( db->autoCommit==0 ){
16819 /* Foreign key support may not be enabled or disabled while not
16820 ** in auto-commit mode. */
16821 mask &= ~(SQLITE_ForeignKeys);
16822 }
16823 #if SQLITE_USER_AUTHENTICATION
16824 if( db->auth.authLevel==UAUTH_User ){
16825 /* Do not allow non-admin users to modify the schema arbitrarily */
16826 mask &= ~(SQLITE_WriteSchema);
16827 }
16828 #endif
16829
16830 if( sqlite3GetBoolean(zRight, 0) ){
16831 db->flags |= mask;
16832 }else{
16833 db->flags &= ~mask;
16834 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
16835 }
16836
16837 /* Many of the flag-pragmas modify the code generated by the SQL
16838 ** compiler (eg. count_changes). So add an opcode to expire all
16839 ** compiled SQL statements after modifying a pragma value.
16840 */
16841 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
16842 setAllPagerFlags(db);
16843 }
16844 break;
16845 }
16846 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
16847
16848 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
16849 /*
16850 ** PRAGMA table_info(<table>)
16851 **
16852 ** Return a single row for each column of the named table. The columns of
16853 ** the returned data set are:
16854 **
16855 ** cid: Column id (numbered from left to right, starting at 0)
16856 ** name: Column name
16857 ** type: Column declaration type.
16858 ** notnull: True if 'NOT NULL' is part of column declaration
16859 ** dflt_value: The default value for the column, if any.
16860 */
16861 case PragTyp_TABLE_INFO: if( zRight ){
16862 Table *pTab;
16863 pTab = sqlite3FindTable(db, zRight, zDb);
16864 if( pTab ){
16865 static const char *azCol[] = {
16866 "cid", "name", "type", "notnull", "dflt_value", "pk"
16867 };
16868 int i, k;
16869 int nHidden = 0;
16870 Column *pCol;
16871 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
16872 pParse->nMem = 6;
16873 sqlite3CodeVerifySchema(pParse, iDb);
16874 setAllColumnNames(v, 6, azCol); assert( 6==ArraySize(azCol) );
16875 sqlite3ViewGetColumnNames(pParse, pTab);
16876 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
16877 if( IsHiddenColumn(pCol) ){
16878 nHidden++;
16879 continue;
16880 }
16881 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
16882 k = 0;
16883 }else if( pPk==0 ){
16884 k = 1;
16885 }else{
16886 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
16887 }
16888 sqlite3VdbeMultiLoad(v, 1, "issisi",
16889 i-nHidden,
16890 pCol->zName,
16891 pCol->zType ? pCol->zType : "",
16892 pCol->notNull ? 1 : 0,
16893 pCol->zDflt,
16894 k);
16895 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
16896 }
16897 }
16898 }
16899 break;
16900
16901 case PragTyp_STATS: {
16902 static const char *azCol[] = { "table", "index", "width", "height" };
16903 Index *pIdx;
16904 HashElem *i;
16905 v = sqlite3GetVdbe(pParse);
16906 pParse->nMem = 4;
16907 sqlite3CodeVerifySchema(pParse, iDb);
16908 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
16909 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
16910 Table *pTab = sqliteHashData(i);
16911 sqlite3VdbeMultiLoad(v, 1, "ssii",
16912 pTab->zName,
16913 0,
16914 (int)sqlite3LogEstToInt(pTab->szTabRow),
16915 (int)sqlite3LogEstToInt(pTab->nRowLogEst));
16916 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
16917 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
16918 sqlite3VdbeMultiLoad(v, 2, "sii",
16919 pIdx->zName,
16920 (int)sqlite3LogEstToInt(pIdx->szIdxRow),
16921 (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]));
16922 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
16923 }
16924 }
16925 }
16926 break;
16927
16928 case PragTyp_INDEX_INFO: if( zRight ){
16929 Index *pIdx;
16930 Table *pTab;
16931 pIdx = sqlite3FindIndex(db, zRight, zDb);
16932 if( pIdx ){
16933 static const char *azCol[] = {
16934 "seqno", "cid", "name", "desc", "coll", "key"
16935 };
16936 int i;
16937 int mx;
16938 if( pPragma->iArg ){
16939 /* PRAGMA index_xinfo (newer version with more rows and columns) */
16940 mx = pIdx->nColumn;
16941 pParse->nMem = 6;
16942 }else{
16943 /* PRAGMA index_info (legacy version) */
16944 mx = pIdx->nKeyCol;
16945 pParse->nMem = 3;
16946 }
16947 pTab = pIdx->pTable;
16948 sqlite3CodeVerifySchema(pParse, iDb);
16949 assert( pParse->nMem<=ArraySize(azCol) );
16950 setAllColumnNames(v, pParse->nMem, azCol);
16951 for(i=0; i<mx; i++){
16952 i16 cnum = pIdx->aiColumn[i];
16953 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum,
16954 cnum<0 ? 0 : pTab->aCol[cnum].zName);
16955 if( pPragma->iArg ){
16956 sqlite3VdbeMultiLoad(v, 4, "isi",
16957 pIdx->aSortOrder[i],
16958 pIdx->azColl[i],
16959 i<pIdx->nKeyCol);
16960 }
16961 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
16962 }
16963 }
16964 }
16965 break;
16966
16967 case PragTyp_INDEX_LIST: if( zRight ){
16968 Index *pIdx;
16969 Table *pTab;
16970 int i;
16971 pTab = sqlite3FindTable(db, zRight, zDb);
16972 if( pTab ){
16973 static const char *azCol[] = {
16974 "seq", "name", "unique", "origin", "partial"
16975 };
16976 v = sqlite3GetVdbe(pParse);
16977 pParse->nMem = 5;
16978 sqlite3CodeVerifySchema(pParse, iDb);
16979 setAllColumnNames(v, 5, azCol); assert( 5==ArraySize(azCol) );
16980 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
16981 const char *azOrigin[] = { "c", "u", "pk" };
16982 sqlite3VdbeMultiLoad(v, 1, "isisi",
16983 i,
16984 pIdx->zName,
16985 IsUniqueIndex(pIdx),
16986 azOrigin[pIdx->idxType],
16987 pIdx->pPartIdxWhere!=0);
16988 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
16989 }
16990 }
16991 }
16992 break;
16993
16994 case PragTyp_DATABASE_LIST: {
16995 static const char *azCol[] = { "seq", "name", "file" };
16996 int i;
16997 pParse->nMem = 3;
16998 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
16999 for(i=0; i<db->nDb; i++){
17000 if( db->aDb[i].pBt==0 ) continue;
17001 assert( db->aDb[i].zName!=0 );
17002 sqlite3VdbeMultiLoad(v, 1, "iss",
17003 i,
17004 db->aDb[i].zName,
17005 sqlite3BtreeGetFilename(db->aDb[i].pBt));
17006 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
17007 }
17008 }
17009 break;
17010
17011 case PragTyp_COLLATION_LIST: {
17012 static const char *azCol[] = { "seq", "name" };
17013 int i = 0;
17014 HashElem *p;
17015 pParse->nMem = 2;
17016 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
17017 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
17018 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
17019 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
17020 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
17021 }
17022 }
17023 break;
17024 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
17025
17026 #ifndef SQLITE_OMIT_FOREIGN_KEY
17027 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
17028 FKey *pFK;
17029 Table *pTab;
17030 pTab = sqlite3FindTable(db, zRight, zDb);
17031 if( pTab ){
17032 v = sqlite3GetVdbe(pParse);
17033 pFK = pTab->pFKey;
17034 if( pFK ){
17035 static const char *azCol[] = {
17036 "id", "seq", "table", "from", "to", "on_update", "on_delete",
17037 "match"
17038 };
17039 int i = 0;
17040 pParse->nMem = 8;
17041 sqlite3CodeVerifySchema(pParse, iDb);
17042 setAllColumnNames(v, 8, azCol); assert( 8==ArraySize(azCol) );
17043 while(pFK){
17044 int j;
17045 for(j=0; j<pFK->nCol; j++){
17046 sqlite3VdbeMultiLoad(v, 1, "iissssss",
17047 i,
17048 j,
17049 pFK->zTo,
17050 pTab->aCol[pFK->aCol[j].iFrom].zName,
17051 pFK->aCol[j].zCol,
17052 actionName(pFK->aAction[1]), /* ON UPDATE */
17053 actionName(pFK->aAction[0]), /* ON DELETE */
17054 "NONE");
17055 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
17056 }
17057 ++i;
17058 pFK = pFK->pNextFrom;
17059 }
17060 }
17061 }
17062 }
17063 break;
17064 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
17065
17066 #ifndef SQLITE_OMIT_FOREIGN_KEY
17067 #ifndef SQLITE_OMIT_TRIGGER
17068 case PragTyp_FOREIGN_KEY_CHECK: {
17069 FKey *pFK; /* A foreign key constraint */
17070 Table *pTab; /* Child table contain "REFERENCES" keyword */
17071 Table *pParent; /* Parent table that child points to */
17072 Index *pIdx; /* Index in the parent table */
17073 int i; /* Loop counter: Foreign key number for pTab */
17074 int j; /* Loop counter: Field of the foreign key */
17075 HashElem *k; /* Loop counter: Next table in schema */
17076 int x; /* result variable */
17077 int regResult; /* 3 registers to hold a result row */
17078 int regKey; /* Register to hold key for checking the FK */
17079 int regRow; /* Registers to hold a row from pTab */
17080 int addrTop; /* Top of a loop checking foreign keys */
17081 int addrOk; /* Jump here if the key is OK */
17082 int *aiCols; /* child to parent column mapping */
17083 static const char *azCol[] = { "table", "rowid", "parent", "fkid" };
17084
17085 regResult = pParse->nMem+1;
17086 pParse->nMem += 4;
17087 regKey = ++pParse->nMem;
17088 regRow = ++pParse->nMem;
17089 v = sqlite3GetVdbe(pParse);
17090 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
17091 sqlite3CodeVerifySchema(pParse, iDb);
17092 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
17093 while( k ){
17094 if( zRight ){
17095 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
17096 k = 0;
17097 }else{
17098 pTab = (Table*)sqliteHashData(k);
17099 k = sqliteHashNext(k);
17100 }
17101 if( pTab==0 || pTab->pFKey==0 ) continue;
17102 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
17103 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
17104 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
17105 sqlite3VdbeLoadString(v, regResult, pTab->zName);
17106 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
17107 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
17108 if( pParent==0 ) continue;
17109 pIdx = 0;
17110 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
17111 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
17112 if( x==0 ){
17113 if( pIdx==0 ){
17114 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
17115 }else{
17116 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
17117 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
17118 }
17119 }else{
17120 k = 0;
17121 break;
17122 }
17123 }
17124 assert( pParse->nErr>0 || pFK==0 );
17125 if( pFK ) break;
17126 if( pParse->nTab<i ) pParse->nTab = i;
17127 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
17128 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
17129 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
17130 pIdx = 0;
17131 aiCols = 0;
17132 if( pParent ){
17133 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
17134 assert( x==0 );
17135 }
17136 addrOk = sqlite3VdbeMakeLabel(v);
17137 if( pParent && pIdx==0 ){
17138 int iKey = pFK->aCol[0].iFrom;
17139 assert( iKey>=0 && iKey<pTab->nCol );
17140 if( iKey!=pTab->iPKey ){
17141 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
17142 sqlite3ColumnDefault(v, pTab, iKey, regRow);
17143 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
17144 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
17145 sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
17146 }else{
17147 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
17148 }
17149 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
17150 sqlite3VdbeGoto(v, addrOk);
17151 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
17152 }else{
17153 for(j=0; j<pFK->nCol; j++){
17154 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
17155 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
17156 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
17157 }
17158 if( pParent ){
17159 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
17160 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
17161 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
17162 VdbeCoverage(v);
17163 }
17164 }
17165 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
17166 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1);
17167 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
17168 sqlite3VdbeResolveLabel(v, addrOk);
17169 sqlite3DbFree(db, aiCols);
17170 }
17171 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
17172 sqlite3VdbeJumpHere(v, addrTop);
17173 }
17174 }
17175 break;
17176 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
17177 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
17178
17179 #ifndef NDEBUG
17180 case PragTyp_PARSER_TRACE: {
17181 if( zRight ){
17182 if( sqlite3GetBoolean(zRight, 0) ){
17183 sqlite3ParserTrace(stdout, "parser: ");
17184 }else{
17185 sqlite3ParserTrace(0, 0);
17186 }
17187 }
17188 }
17189 break;
17190 #endif
17191
17192 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
17193 ** used will be case sensitive or not depending on the RHS.
17194 */
17195 case PragTyp_CASE_SENSITIVE_LIKE: {
17196 if( zRight ){
17197 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
17198 }
17199 }
17200 break;
17201
17202 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
17203 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
17204 #endif
17205
17206 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
17207 /* Pragma "quick_check" is reduced version of
17208 ** integrity_check designed to detect most database corruption
17209 ** without most of the overhead of a full integrity-check.
17210 */
17211 case PragTyp_INTEGRITY_CHECK: {
17212 int i, j, addr, mxErr;
17213
17214 /* Code that appears at the end of the integrity check. If no error
17215 ** messages have been generated, output OK. Otherwise output the
17216 ** error message
17217 */
17218 static const int iLn = VDBE_OFFSET_LINENO(2);
17219 static const VdbeOpList endCode[] = {
17220 { OP_AddImm, 1, 0, 0}, /* 0 */
17221 { OP_If, 1, 0, 0}, /* 1 */
17222 { OP_String8, 0, 3, 0}, /* 2 */
17223 { OP_ResultRow, 3, 1, 0},
17224 };
17225
17226 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
17227
17228 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
17229 ** then iDb is set to the index of the database identified by <db>.
17230 ** In this case, the integrity of database iDb only is verified by
17231 ** the VDBE created below.
17232 **
17233 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
17234 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
17235 ** to -1 here, to indicate that the VDBE should verify the integrity
17236 ** of all attached databases. */
17237 assert( iDb>=0 );
17238 assert( iDb==0 || pId2->z );
17239 if( pId2->z==0 ) iDb = -1;
17240
17241 /* Initialize the VDBE program */
17242 pParse->nMem = 6;
17243 setOneColumnName(v, "integrity_check");
17244
17245 /* Set the maximum error count */
17246 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
17247 if( zRight ){
17248 sqlite3GetInt32(zRight, &mxErr);
17249 if( mxErr<=0 ){
17250 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
17251 }
17252 }
17253 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
17254
17255 /* Do an integrity check on each database file */
17256 for(i=0; i<db->nDb; i++){
17257 HashElem *x;
17258 Hash *pTbls;
17259 int cnt = 0;
17260
17261 if( OMIT_TEMPDB && i==1 ) continue;
17262 if( iDb>=0 && i!=iDb ) continue;
17263
17264 sqlite3CodeVerifySchema(pParse, i);
17265 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
17266 VdbeCoverage(v);
17267 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
17268 sqlite3VdbeJumpHere(v, addr);
17269
17270 /* Do an integrity check of the B-Tree
17271 **
17272 ** Begin by filling registers 2, 3, ... with the root pages numbers
17273 ** for all tables and indices in the database.
17274 */
17275 assert( sqlite3SchemaMutexHeld(db, i, 0) );
17276 pTbls = &db->aDb[i].pSchema->tblHash;
17277 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
17278 Table *pTab = sqliteHashData(x);
17279 Index *pIdx;
17280 if( HasRowid(pTab) ){
17281 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
17282 VdbeComment((v, "%s", pTab->zName));
17283 cnt++;
17284 }
17285 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
17286 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
17287 VdbeComment((v, "%s", pIdx->zName));
17288 cnt++;
17289 }
17290 }
17291
17292 /* Make sure sufficient number of registers have been allocated */
17293 pParse->nMem = MAX( pParse->nMem, cnt+8 );
17294
17295 /* Do the b-tree integrity checks */
17296 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
17297 sqlite3VdbeChangeP5(v, (u8)i);
17298 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
17299 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
17300 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
17301 P4_DYNAMIC);
17302 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
17303 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
17304 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
17305 sqlite3VdbeJumpHere(v, addr);
17306
17307 /* Make sure all the indices are constructed correctly.
17308 */
17309 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
17310 Table *pTab = sqliteHashData(x);
17311 Index *pIdx, *pPk;
17312 Index *pPrior = 0;
17313 int loopTop;
17314 int iDataCur, iIdxCur;
17315 int r1 = -1;
17316
17317 if( pTab->pIndex==0 ) continue;
17318 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
17319 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
17320 VdbeCoverage(v);
17321 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
17322 sqlite3VdbeJumpHere(v, addr);
17323 sqlite3ExprCacheClear(pParse);
17324 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
17325 1, 0, &iDataCur, &iIdxCur);
17326 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
17327 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
17328 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
17329 }
17330 pParse->nMem = MAX(pParse->nMem, 8+j);
17331 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
17332 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
17333 /* Verify that all NOT NULL columns really are NOT NULL */
17334 for(j=0; j<pTab->nCol; j++){
17335 char *zErr;
17336 int jmp2, jmp3;
17337 if( j==pTab->iPKey ) continue;
17338 if( pTab->aCol[j].notNull==0 ) continue;
17339 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
17340 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
17341 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
17342 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
17343 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
17344 pTab->aCol[j].zName);
17345 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
17346 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
17347 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
17348 sqlite3VdbeAddOp0(v, OP_Halt);
17349 sqlite3VdbeJumpHere(v, jmp2);
17350 sqlite3VdbeJumpHere(v, jmp3);
17351 }
17352 /* Validate index entries for the current row */
17353 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
17354 int jmp2, jmp3, jmp4, jmp5;
17355 int ckUniq = sqlite3VdbeMakeLabel(v);
17356 if( pPk==pIdx ) continue;
17357 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
17358 pPrior, r1);
17359 pPrior = pIdx;
17360 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
17361 /* Verify that an index entry exists for the current table row */
17362 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
17363 pIdx->nColumn); VdbeCoverage(v);
17364 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
17365 sqlite3VdbeLoadString(v, 3, "row ");
17366 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
17367 sqlite3VdbeLoadString(v, 4, " missing from index ");
17368 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
17369 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
17370 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
17371 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
17372 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
17373 sqlite3VdbeAddOp0(v, OP_Halt);
17374 sqlite3VdbeJumpHere(v, jmp2);
17375 /* For UNIQUE indexes, verify that only one entry exists with the
17376 ** current key. The entry is unique if (1) any column is NULL
17377 ** or (2) the next entry has a different key */
17378 if( IsUniqueIndex(pIdx) ){
17379 int uniqOk = sqlite3VdbeMakeLabel(v);
17380 int jmp6;
17381 int kk;
17382 for(kk=0; kk<pIdx->nKeyCol; kk++){
17383 int iCol = pIdx->aiColumn[kk];
17384 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
17385 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
17386 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
17387 VdbeCoverage(v);
17388 }
17389 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
17390 sqlite3VdbeGoto(v, uniqOk);
17391 sqlite3VdbeJumpHere(v, jmp6);
17392 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
17393 pIdx->nKeyCol); VdbeCoverage(v);
17394 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
17395 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
17396 sqlite3VdbeGoto(v, jmp5);
17397 sqlite3VdbeResolveLabel(v, uniqOk);
17398 }
17399 sqlite3VdbeJumpHere(v, jmp4);
17400 sqlite3ResolvePartIdxLabel(pParse, jmp3);
17401 }
17402 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
17403 sqlite3VdbeJumpHere(v, loopTop-1);
17404 #ifndef SQLITE_OMIT_BTREECOUNT
17405 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
17406 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
17407 if( pPk==pIdx ) continue;
17408 addr = sqlite3VdbeCurrentAddr(v);
17409 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
17410 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
17411 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
17412 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
17413 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
17414 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
17415 sqlite3VdbeLoadString(v, 3, pIdx->zName);
17416 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
17417 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
17418 }
17419 #endif /* SQLITE_OMIT_BTREECOUNT */
17420 }
17421 }
17422 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
17423 sqlite3VdbeChangeP2(v, addr, -mxErr);
17424 sqlite3VdbeJumpHere(v, addr+1);
17425 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
17426 }
17427 break;
17428 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
17429
17430 #ifndef SQLITE_OMIT_UTF16
17431 /*
17432 ** PRAGMA encoding
17433 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
17434 **
17435 ** In its first form, this pragma returns the encoding of the main
17436 ** database. If the database is not initialized, it is initialized now.
17437 **
17438 ** The second form of this pragma is a no-op if the main database file
17439 ** has not already been initialized. In this case it sets the default
17440 ** encoding that will be used for the main database file if a new file
17441 ** is created. If an existing main database file is opened, then the
17442 ** default text encoding for the existing database is used.
17443 **
17444 ** In all cases new databases created using the ATTACH command are
17445 ** created to use the same default text encoding as the main database. If
17446 ** the main database has not been initialized and/or created when ATTACH
17447 ** is executed, this is done before the ATTACH operation.
17448 **
17449 ** In the second form this pragma sets the text encoding to be used in
17450 ** new database files created using this database handle. It is only
17451 ** useful if invoked immediately after the main database i
17452 */
17453 case PragTyp_ENCODING: {
17454 static const struct EncName {
17455 char *zName;
17456 u8 enc;
17457 } encnames[] = {
17458 { "UTF8", SQLITE_UTF8 },
17459 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
17460 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
17461 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
17462 { "UTF16le", SQLITE_UTF16LE },
17463 { "UTF16be", SQLITE_UTF16BE },
17464 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
17465 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
17466 { 0, 0 }
17467 };
17468 const struct EncName *pEnc;
17469 if( !zRight ){ /* "PRAGMA encoding" */
17470 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
17471 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
17472 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
17473 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
17474 returnSingleText(v, "encoding", encnames[ENC(pParse->db)].zName);
17475 }else{ /* "PRAGMA encoding = XXX" */
17476 /* Only change the value of sqlite.enc if the database handle is not
17477 ** initialized. If the main database exists, the new sqlite.enc value
17478 ** will be overwritten when the schema is next loaded. If it does not
17479 ** already exists, it will be created to use the new encoding value.
17480 */
17481 if(
17482 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
17483 DbHasProperty(db, 0, DB_Empty)
17484 ){
17485 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
17486 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
17487 SCHEMA_ENC(db) = ENC(db) =
17488 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
17489 break;
17490 }
17491 }
17492 if( !pEnc->zName ){
17493 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
17494 }
17495 }
17496 }
17497 }
17498 break;
17499 #endif /* SQLITE_OMIT_UTF16 */
17500
17501 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
17502 /*
17503 ** PRAGMA [schema.]schema_version
17504 ** PRAGMA [schema.]schema_version = <integer>
17505 **
17506 ** PRAGMA [schema.]user_version
17507 ** PRAGMA [schema.]user_version = <integer>
17508 **
17509 ** PRAGMA [schema.]freelist_count = <integer>
17510 **
17511 ** PRAGMA [schema.]application_id
17512 ** PRAGMA [schema.]application_id = <integer>
17513 **
17514 ** The pragma's schema_version and user_version are used to set or get
17515 ** the value of the schema-version and user-version, respectively. Both
17516 ** the schema-version and the user-version are 32-bit signed integers
17517 ** stored in the database header.
17518 **
17519 ** The schema-cookie is usually only manipulated internally by SQLite. It
17520 ** is incremented by SQLite whenever the database schema is modified (by
17521 ** creating or dropping a table or index). The schema version is used by
17522 ** SQLite each time a query is executed to ensure that the internal cache
17523 ** of the schema used when compiling the SQL query matches the schema of
17524 ** the database against which the compiled query is actually executed.
17525 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
17526 ** the schema-version is potentially dangerous and may lead to program
17527 ** crashes or database corruption. Use with caution!
17528 **
17529 ** The user-version is not used internally by SQLite. It may be used by
17530 ** applications for any purpose.
17531 */
17532 case PragTyp_HEADER_VALUE: {
17533 int iCookie = pPragma->iArg; /* Which cookie to read or write */
17534 sqlite3VdbeUsesBtree(v, iDb);
17535 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
17536 /* Write the specified cookie value */
17537 static const VdbeOpList setCookie[] = {
17538 { OP_Transaction, 0, 1, 0}, /* 0 */
17539 { OP_Integer, 0, 1, 0}, /* 1 */
17540 { OP_SetCookie, 0, 0, 1}, /* 2 */
17541 };
17542 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
17543 sqlite3VdbeChangeP1(v, addr, iDb);
17544 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
17545 sqlite3VdbeChangeP1(v, addr+2, iDb);
17546 sqlite3VdbeChangeP2(v, addr+2, iCookie);
17547 }else{
17548 /* Read the specified cookie value */
17549 static const VdbeOpList readCookie[] = {
17550 { OP_Transaction, 0, 0, 0}, /* 0 */
17551 { OP_ReadCookie, 0, 1, 0}, /* 1 */
17552 { OP_ResultRow, 1, 1, 0}
17553 };
17554 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
17555 sqlite3VdbeChangeP1(v, addr, iDb);
17556 sqlite3VdbeChangeP1(v, addr+1, iDb);
17557 sqlite3VdbeChangeP3(v, addr+1, iCookie);
17558 sqlite3VdbeSetNumCols(v, 1);
17559 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
17560 }
17561 }
17562 break;
17563 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
17564
17565 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
17566 /*
17567 ** PRAGMA compile_options
17568 **
17569 ** Return the names of all compile-time options used in this build,
17570 ** one option per row.
17571 */
17572 case PragTyp_COMPILE_OPTIONS: {
17573 int i = 0;
17574 const char *zOpt;
17575 pParse->nMem = 1;
17576 setOneColumnName(v, "compile_option");
17577 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
17578 sqlite3VdbeLoadString(v, 1, zOpt);
17579 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
17580 }
17581 }
17582 break;
17583 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
17584
17585 #ifndef SQLITE_OMIT_WAL
17586 /*
17587 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
17588 **
17589 ** Checkpoint the database.
17590 */
17591 case PragTyp_WAL_CHECKPOINT: {
17592 static const char *azCol[] = { "busy", "log", "checkpointed" };
17593 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
17594 int eMode = SQLITE_CHECKPOINT_PASSIVE;
17595 if( zRight ){
17596 if( sqlite3StrICmp(zRight, "full")==0 ){
17597 eMode = SQLITE_CHECKPOINT_FULL;
17598 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
17599 eMode = SQLITE_CHECKPOINT_RESTART;
17600 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
17601 eMode = SQLITE_CHECKPOINT_TRUNCATE;
17602 }
17603 }
17604 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
17605 pParse->nMem = 3;
17606 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
17607 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
17608 }
17609 break;
17610
17611 /*
17612 ** PRAGMA wal_autocheckpoint
17613 ** PRAGMA wal_autocheckpoint = N
17614 **
17615 ** Configure a database connection to automatically checkpoint a database
17616 ** after accumulating N frames in the log. Or query for the current value
17617 ** of N.
17618 */
17619 case PragTyp_WAL_AUTOCHECKPOINT: {
17620 if( zRight ){
17621 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
17622 }
17623 returnSingleInt(v, "wal_autocheckpoint",
17624 db->xWalCallback==sqlite3WalDefaultHook ?
17625 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
17626 }
17627 break;
17628 #endif
17629
17630 /*
17631 ** PRAGMA shrink_memory
17632 **
17633 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
17634 ** connection on which it is invoked to free up as much memory as it
17635 ** can, by calling sqlite3_db_release_memory().
17636 */
17637 case PragTyp_SHRINK_MEMORY: {
17638 sqlite3_db_release_memory(db);
17639 break;
17640 }
17641
17642 /*
17643 ** PRAGMA busy_timeout
17644 ** PRAGMA busy_timeout = N
17645 **
17646 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
17647 ** if one is set. If no busy handler or a different busy handler is set
17648 ** then 0 is returned. Setting the busy_timeout to 0 or negative
17649 ** disables the timeout.
17650 */
17651 /*case PragTyp_BUSY_TIMEOUT*/ default: {
17652 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
17653 if( zRight ){
17654 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
17655 }
17656 returnSingleInt(v, "timeout", db->busyTimeout);
17657 break;
17658 }
17659
17660 /*
17661 ** PRAGMA soft_heap_limit
17662 ** PRAGMA soft_heap_limit = N
17663 **
17664 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
17665 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
17666 ** specified and is a non-negative integer.
17667 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
17668 ** returns the same integer that would be returned by the
17669 ** sqlite3_soft_heap_limit64(-1) C-language function.
17670 */
17671 case PragTyp_SOFT_HEAP_LIMIT: {
17672 sqlite3_int64 N;
17673 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
17674 sqlite3_soft_heap_limit64(N);
17675 }
17676 returnSingleInt(v, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
17677 break;
17678 }
17679
17680 /*
17681 ** PRAGMA threads
17682 ** PRAGMA threads = N
17683 **
17684 ** Configure the maximum number of worker threads. Return the new
17685 ** maximum, which might be less than requested.
17686 */
17687 case PragTyp_THREADS: {
17688 sqlite3_int64 N;
17689 if( zRight
17690 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
17691 && N>=0
17692 ){
17693 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
17694 }
17695 returnSingleInt(v, "threads",
17696 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
17697 break;
17698 }
17699
17700 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
17701 /*
17702 ** Report the current state of file logs for all databases
17703 */
17704 case PragTyp_LOCK_STATUS: {
17705 static const char *const azLockName[] = {
17706 "unlocked", "shared", "reserved", "pending", "exclusive"
17707 };
17708 static const char *azCol[] = { "database", "status" };
17709 int i;
17710 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
17711 pParse->nMem = 2;
17712 for(i=0; i<db->nDb; i++){
17713 Btree *pBt;
17714 const char *zState = "unknown";
17715 int j;
17716 if( db->aDb[i].zName==0 ) continue;
17717 pBt = db->aDb[i].pBt;
17718 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
17719 zState = "closed";
17720 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
17721 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
17722 zState = azLockName[j];
17723 }
17724 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zName, zState);
17725 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
17726 }
17727 break;
17728 }
17729 #endif
17730
17731 #ifdef SQLITE_HAS_CODEC
17732 case PragTyp_KEY: {
17733 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
17734 break;
17735 }
17736 case PragTyp_REKEY: {
17737 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
17738 break;
17739 }
17740 case PragTyp_HEXKEY: {
17741 if( zRight ){
17742 u8 iByte;
17743 int i;
17744 char zKey[40];
17745 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
17746 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
17747 if( (i&1)!=0 ) zKey[i/2] = iByte;
17748 }
17749 if( (zLeft[3] & 0xf)==0xb ){
17750 sqlite3_key_v2(db, zDb, zKey, i/2);
17751 }else{
17752 sqlite3_rekey_v2(db, zDb, zKey, i/2);
17753 }
17754 }
17755 break;
17756 }
17757 #endif
17758 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
17759 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
17760 #ifdef SQLITE_HAS_CODEC
17761 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
17762 sqlite3_activate_see(&zRight[4]);
17763 }
17764 #endif
17765 #ifdef SQLITE_ENABLE_CEROD
17766 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
17767 sqlite3_activate_cerod(&zRight[6]);
17768 }
17769 #endif
17770 }
17771 break;
17772 #endif
17773
17774 } /* End of the PRAGMA switch */
17775
17776 pragma_out:
17777 sqlite3DbFree(db, zLeft);
17778 sqlite3DbFree(db, zRight);
17779 }
17780
17781 #endif /* SQLITE_OMIT_PRAGMA */
17782
17783 /************** End of pragma.c **********************************************/
17784 /************** Begin file prepare.c *****************************************/
17785 /*
17786 ** 2005 May 25
17787 **
17788 ** The author disclaims copyright to this source code. In place of
17789 ** a legal notice, here is a blessing:
17790 **
17791 ** May you do good and not evil.
17792 ** May you find forgiveness for yourself and forgive others.
17793 ** May you share freely, never taking more than you give.
17794 **
17795 *************************************************************************
17796 ** This file contains the implementation of the sqlite3_prepare()
17797 ** interface, and routines that contribute to loading the database schema
17798 ** from disk.
17799 */
17800 /* #include "sqliteInt.h" */
17801
17802 /*
17803 ** Fill the InitData structure with an error message that indicates
17804 ** that the database is corrupt.
17805 */
17806 static void corruptSchema(
17807 InitData *pData, /* Initialization context */
17808 const char *zObj, /* Object being parsed at the point of error */
17809 const char *zExtra /* Error information */
17810 ){
17811 sqlite3 *db = pData->db;
17812 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
17813 char *z;
17814 if( zObj==0 ) zObj = "?";
17815 z = sqlite3_mprintf("malformed database schema (%s)", zObj);
17816 if( z && zExtra ) z = sqlite3_mprintf("%z - %s", z, zExtra);
17817 sqlite3DbFree(db, *pData->pzErrMsg);
17818 *pData->pzErrMsg = z;
17819 if( z==0 ) db->mallocFailed = 1;
17820 }
17821 pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
17822 }
17823
17824 /*
17825 ** This is the callback routine for the code that initializes the
17826 ** database. See sqlite3Init() below for additional information.
17827 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
17828 **
17829 ** Each callback contains the following information:
17830 **
17831 ** argv[0] = name of thing being created
17832 ** argv[1] = root page number for table or index. 0 for trigger or view.
17833 ** argv[2] = SQL text for the CREATE statement.
17834 **
17835 */
17836 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
17837 InitData *pData = (InitData*)pInit;
17838 sqlite3 *db = pData->db;
17839 int iDb = pData->iDb;
17840
17841 assert( argc==3 );
17842 UNUSED_PARAMETER2(NotUsed, argc);
17843 assert( sqlite3_mutex_held(db->mutex) );
17844 DbClearProperty(db, iDb, DB_Empty);
17845 if( db->mallocFailed ){
17846 corruptSchema(pData, argv[0], 0);
17847 return 1;
17848 }
17849
17850 assert( iDb>=0 && iDb<db->nDb );
17851 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
17852 if( argv[1]==0 ){
17853 corruptSchema(pData, argv[0], 0);
17854 }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
17855 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
17856 ** But because db->init.busy is set to 1, no VDBE code is generated
17857 ** or executed. All the parser does is build the internal data
17858 ** structures that describe the table, index, or view.
17859 */
17860 int rc;
17861 sqlite3_stmt *pStmt;
17862 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
17863
17864 assert( db->init.busy );
17865 db->init.iDb = iDb;
17866 db->init.newTnum = sqlite3Atoi(argv[1]);
17867 db->init.orphanTrigger = 0;
17868 TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
17869 rc = db->errCode;
17870 assert( (rc&0xFF)==(rcp&0xFF) );
17871 db->init.iDb = 0;
17872 if( SQLITE_OK!=rc ){
17873 if( db->init.orphanTrigger ){
17874 assert( iDb==1 );
17875 }else{
17876 pData->rc = rc;
17877 if( rc==SQLITE_NOMEM ){
17878 db->mallocFailed = 1;
17879 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
17880 corruptSchema(pData, argv[0], sqlite3_errmsg(db));
17881 }
17882 }
17883 }
17884 sqlite3_finalize(pStmt);
17885 }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
17886 corruptSchema(pData, argv[0], 0);
17887 }else{
17888 /* If the SQL column is blank it means this is an index that
17889 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
17890 ** constraint for a CREATE TABLE. The index should have already
17891 ** been created when we processed the CREATE TABLE. All we have
17892 ** to do here is record the root page number for that index.
17893 */
17894 Index *pIndex;
17895 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
17896 if( pIndex==0 ){
17897 /* This can occur if there exists an index on a TEMP table which
17898 ** has the same name as another index on a permanent index. Since
17899 ** the permanent table is hidden by the TEMP table, we can also
17900 ** safely ignore the index on the permanent table.
17901 */
17902 /* Do Nothing */;
17903 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
17904 corruptSchema(pData, argv[0], "invalid rootpage");
17905 }
17906 }
17907 return 0;
17908 }
17909
17910 /*
17911 ** Attempt to read the database schema and initialize internal
17912 ** data structures for a single database file. The index of the
17913 ** database file is given by iDb. iDb==0 is used for the main
17914 ** database. iDb==1 should never be used. iDb>=2 is used for
17915 ** auxiliary databases. Return one of the SQLITE_ error codes to
17916 ** indicate success or failure.
17917 */
17918 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
17919 int rc;
17920 int i;
17921 #ifndef SQLITE_OMIT_DEPRECATED
17922 int size;
17923 #endif
17924 Table *pTab;
17925 Db *pDb;
17926 char const *azArg[4];
17927 int meta[5];
17928 InitData initData;
17929 char const *zMasterSchema;
17930 char const *zMasterName;
17931 int openedTransaction = 0;
17932
17933 /*
17934 ** The master database table has a structure like this
17935 */
17936 static const char master_schema[] =
17937 "CREATE TABLE sqlite_master(\n"
17938 " type text,\n"
17939 " name text,\n"
17940 " tbl_name text,\n"
17941 " rootpage integer,\n"
17942 " sql text\n"
17943 ")"
17944 ;
17945 #ifndef SQLITE_OMIT_TEMPDB
17946 static const char temp_master_schema[] =
17947 "CREATE TEMP TABLE sqlite_temp_master(\n"
17948 " type text,\n"
17949 " name text,\n"
17950 " tbl_name text,\n"
17951 " rootpage integer,\n"
17952 " sql text\n"
17953 ")"
17954 ;
17955 #else
17956 #define temp_master_schema 0
17957 #endif
17958
17959 assert( iDb>=0 && iDb<db->nDb );
17960 assert( db->aDb[iDb].pSchema );
17961 assert( sqlite3_mutex_held(db->mutex) );
17962 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
17963
17964 /* zMasterSchema and zInitScript are set to point at the master schema
17965 ** and initialisation script appropriate for the database being
17966 ** initialized. zMasterName is the name of the master table.
17967 */
17968 if( !OMIT_TEMPDB && iDb==1 ){
17969 zMasterSchema = temp_master_schema;
17970 }else{
17971 zMasterSchema = master_schema;
17972 }
17973 zMasterName = SCHEMA_TABLE(iDb);
17974
17975 /* Construct the schema tables. */
17976 azArg[0] = zMasterName;
17977 azArg[1] = "1";
17978 azArg[2] = zMasterSchema;
17979 azArg[3] = 0;
17980 initData.db = db;
17981 initData.iDb = iDb;
17982 initData.rc = SQLITE_OK;
17983 initData.pzErrMsg = pzErrMsg;
17984 sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
17985 if( initData.rc ){
17986 rc = initData.rc;
17987 goto error_out;
17988 }
17989 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
17990 if( ALWAYS(pTab) ){
17991 pTab->tabFlags |= TF_Readonly;
17992 }
17993
17994 /* Create a cursor to hold the database open
17995 */
17996 pDb = &db->aDb[iDb];
17997 if( pDb->pBt==0 ){
17998 if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
17999 DbSetProperty(db, 1, DB_SchemaLoaded);
18000 }
18001 return SQLITE_OK;
18002 }
18003
18004 /* If there is not already a read-only (or read-write) transaction opened
18005 ** on the b-tree database, open one now. If a transaction is opened, it
18006 ** will be closed before this function returns. */
18007 sqlite3BtreeEnter(pDb->pBt);
18008 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
18009 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
18010 if( rc!=SQLITE_OK ){
18011 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
18012 goto initone_error_out;
18013 }
18014 openedTransaction = 1;
18015 }
18016
18017 /* Get the database meta information.
18018 **
18019 ** Meta values are as follows:
18020 ** meta[0] Schema cookie. Changes with each schema change.
18021 ** meta[1] File format of schema layer.
18022 ** meta[2] Size of the page cache.
18023 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
18024 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
18025 ** meta[5] User version
18026 ** meta[6] Incremental vacuum mode
18027 ** meta[7] unused
18028 ** meta[8] unused
18029 ** meta[9] unused
18030 **
18031 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
18032 ** the possible values of meta[4].
18033 */
18034 for(i=0; i<ArraySize(meta); i++){
18035 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
18036 }
18037 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
18038
18039 /* If opening a non-empty database, check the text encoding. For the
18040 ** main database, set sqlite3.enc to the encoding of the main database.
18041 ** For an attached db, it is an error if the encoding is not the same
18042 ** as sqlite3.enc.
18043 */
18044 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
18045 if( iDb==0 ){
18046 #ifndef SQLITE_OMIT_UTF16
18047 u8 encoding;
18048 /* If opening the main database, set ENC(db). */
18049 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
18050 if( encoding==0 ) encoding = SQLITE_UTF8;
18051 ENC(db) = encoding;
18052 #else
18053 ENC(db) = SQLITE_UTF8;
18054 #endif
18055 }else{
18056 /* If opening an attached database, the encoding much match ENC(db) */
18057 if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
18058 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
18059 " text encoding as main database");
18060 rc = SQLITE_ERROR;
18061 goto initone_error_out;
18062 }
18063 }
18064 }else{
18065 DbSetProperty(db, iDb, DB_Empty);
18066 }
18067 pDb->pSchema->enc = ENC(db);
18068
18069 if( pDb->pSchema->cache_size==0 ){
18070 #ifndef SQLITE_OMIT_DEPRECATED
18071 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
18072 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
18073 pDb->pSchema->cache_size = size;
18074 #else
18075 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
18076 #endif
18077 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
18078 }
18079
18080 /*
18081 ** file_format==1 Version 3.0.0.
18082 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
18083 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
18084 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
18085 */
18086 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
18087 if( pDb->pSchema->file_format==0 ){
18088 pDb->pSchema->file_format = 1;
18089 }
18090 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
18091 sqlite3SetString(pzErrMsg, db, "unsupported file format");
18092 rc = SQLITE_ERROR;
18093 goto initone_error_out;
18094 }
18095
18096 /* Ticket #2804: When we open a database in the newer file format,
18097 ** clear the legacy_file_format pragma flag so that a VACUUM will
18098 ** not downgrade the database and thus invalidate any descending
18099 ** indices that the user might have created.
18100 */
18101 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
18102 db->flags &= ~SQLITE_LegacyFileFmt;
18103 }
18104
18105 /* Read the schema information out of the schema tables
18106 */
18107 assert( db->init.busy );
18108 {
18109 char *zSql;
18110 zSql = sqlite3MPrintf(db,
18111 "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
18112 db->aDb[iDb].zName, zMasterName);
18113 #ifndef SQLITE_OMIT_AUTHORIZATION
18114 {
18115 sqlite3_xauth xAuth;
18116 xAuth = db->xAuth;
18117 db->xAuth = 0;
18118 #endif
18119 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
18120 #ifndef SQLITE_OMIT_AUTHORIZATION
18121 db->xAuth = xAuth;
18122 }
18123 #endif
18124 if( rc==SQLITE_OK ) rc = initData.rc;
18125 sqlite3DbFree(db, zSql);
18126 #ifndef SQLITE_OMIT_ANALYZE
18127 if( rc==SQLITE_OK ){
18128 sqlite3AnalysisLoad(db, iDb);
18129 }
18130 #endif
18131 }
18132 if( db->mallocFailed ){
18133 rc = SQLITE_NOMEM;
18134 sqlite3ResetAllSchemasOfConnection(db);
18135 }
18136 if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
18137 /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
18138 ** the schema loaded, even if errors occurred. In this situation the
18139 ** current sqlite3_prepare() operation will fail, but the following one
18140 ** will attempt to compile the supplied statement against whatever subset
18141 ** of the schema was loaded before the error occurred. The primary
18142 ** purpose of this is to allow access to the sqlite_master table
18143 ** even when its contents have been corrupted.
18144 */
18145 DbSetProperty(db, iDb, DB_SchemaLoaded);
18146 rc = SQLITE_OK;
18147 }
18148
18149 /* Jump here for an error that occurs after successfully allocating
18150 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
18151 ** before that point, jump to error_out.
18152 */
18153 initone_error_out:
18154 if( openedTransaction ){
18155 sqlite3BtreeCommit(pDb->pBt);
18156 }
18157 sqlite3BtreeLeave(pDb->pBt);
18158
18159 error_out:
18160 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
18161 db->mallocFailed = 1;
18162 }
18163 return rc;
18164 }
18165
18166 /*
18167 ** Initialize all database files - the main database file, the file
18168 ** used to store temporary tables, and any additional database files
18169 ** created using ATTACH statements. Return a success code. If an
18170 ** error occurs, write an error message into *pzErrMsg.
18171 **
18172 ** After a database is initialized, the DB_SchemaLoaded bit is set
18173 ** bit is set in the flags field of the Db structure. If the database
18174 ** file was of zero-length, then the DB_Empty flag is also set.
18175 */
18176 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
18177 int i, rc;
18178 int commit_internal = !(db->flags&SQLITE_InternChanges);
18179
18180 assert( sqlite3_mutex_held(db->mutex) );
18181 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
18182 assert( db->init.busy==0 );
18183 rc = SQLITE_OK;
18184 db->init.busy = 1;
18185 ENC(db) = SCHEMA_ENC(db);
18186 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
18187 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
18188 rc = sqlite3InitOne(db, i, pzErrMsg);
18189 if( rc ){
18190 sqlite3ResetOneSchema(db, i);
18191 }
18192 }
18193
18194 /* Once all the other databases have been initialized, load the schema
18195 ** for the TEMP database. This is loaded last, as the TEMP database
18196 ** schema may contain references to objects in other databases.
18197 */
18198 #ifndef SQLITE_OMIT_TEMPDB
18199 assert( db->nDb>1 );
18200 if( rc==SQLITE_OK && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
18201 rc = sqlite3InitOne(db, 1, pzErrMsg);
18202 if( rc ){
18203 sqlite3ResetOneSchema(db, 1);
18204 }
18205 }
18206 #endif
18207
18208 db->init.busy = 0;
18209 if( rc==SQLITE_OK && commit_internal ){
18210 sqlite3CommitInternalChanges(db);
18211 }
18212
18213 return rc;
18214 }
18215
18216 /*
18217 ** This routine is a no-op if the database schema is already initialized.
18218 ** Otherwise, the schema is loaded. An error code is returned.
18219 */
18220 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
18221 int rc = SQLITE_OK;
18222 sqlite3 *db = pParse->db;
18223 assert( sqlite3_mutex_held(db->mutex) );
18224 if( !db->init.busy ){
18225 rc = sqlite3Init(db, &pParse->zErrMsg);
18226 }
18227 if( rc!=SQLITE_OK ){
18228 pParse->rc = rc;
18229 pParse->nErr++;
18230 }
18231 return rc;
18232 }
18233
18234
18235 /*
18236 ** Check schema cookies in all databases. If any cookie is out
18237 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
18238 ** make no changes to pParse->rc.
18239 */
18240 static void schemaIsValid(Parse *pParse){
18241 sqlite3 *db = pParse->db;
18242 int iDb;
18243 int rc;
18244 int cookie;
18245
18246 assert( pParse->checkSchema );
18247 assert( sqlite3_mutex_held(db->mutex) );
18248 for(iDb=0; iDb<db->nDb; iDb++){
18249 int openedTransaction = 0; /* True if a transaction is opened */
18250 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
18251 if( pBt==0 ) continue;
18252
18253 /* If there is not already a read-only (or read-write) transaction opened
18254 ** on the b-tree database, open one now. If a transaction is opened, it
18255 ** will be closed immediately after reading the meta-value. */
18256 if( !sqlite3BtreeIsInReadTrans(pBt) ){
18257 rc = sqlite3BtreeBeginTrans(pBt, 0);
18258 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
18259 db->mallocFailed = 1;
18260 }
18261 if( rc!=SQLITE_OK ) return;
18262 openedTransaction = 1;
18263 }
18264
18265 /* Read the schema cookie from the database. If it does not match the
18266 ** value stored as part of the in-memory schema representation,
18267 ** set Parse.rc to SQLITE_SCHEMA. */
18268 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
18269 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
18270 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
18271 sqlite3ResetOneSchema(db, iDb);
18272 pParse->rc = SQLITE_SCHEMA;
18273 }
18274
18275 /* Close the transaction, if one was opened. */
18276 if( openedTransaction ){
18277 sqlite3BtreeCommit(pBt);
18278 }
18279 }
18280 }
18281
18282 /*
18283 ** Convert a schema pointer into the iDb index that indicates
18284 ** which database file in db->aDb[] the schema refers to.
18285 **
18286 ** If the same database is attached more than once, the first
18287 ** attached database is returned.
18288 */
18289 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
18290 int i = -1000000;
18291
18292 /* If pSchema is NULL, then return -1000000. This happens when code in
18293 ** expr.c is trying to resolve a reference to a transient table (i.e. one
18294 ** created by a sub-select). In this case the return value of this
18295 ** function should never be used.
18296 **
18297 ** We return -1000000 instead of the more usual -1 simply because using
18298 ** -1000000 as the incorrect index into db->aDb[] is much
18299 ** more likely to cause a segfault than -1 (of course there are assert()
18300 ** statements too, but it never hurts to play the odds).
18301 */
18302 assert( sqlite3_mutex_held(db->mutex) );
18303 if( pSchema ){
18304 for(i=0; ALWAYS(i<db->nDb); i++){
18305 if( db->aDb[i].pSchema==pSchema ){
18306 break;
18307 }
18308 }
18309 assert( i>=0 && i<db->nDb );
18310 }
18311 return i;
18312 }
18313
18314 /*
18315 ** Free all memory allocations in the pParse object
18316 */
18317 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
18318 if( pParse ){
18319 sqlite3 *db = pParse->db;
18320 sqlite3DbFree(db, pParse->aLabel);
18321 sqlite3ExprListDelete(db, pParse->pConstExpr);
18322 }
18323 }
18324
18325 /*
18326 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
18327 */
18328 static int sqlite3Prepare(
18329 sqlite3 *db, /* Database handle. */
18330 const char *zSql, /* UTF-8 encoded SQL statement. */
18331 int nBytes, /* Length of zSql in bytes. */
18332 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
18333 Vdbe *pReprepare, /* VM being reprepared */
18334 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
18335 const char **pzTail /* OUT: End of parsed string */
18336 ){
18337 Parse *pParse; /* Parsing context */
18338 char *zErrMsg = 0; /* Error message */
18339 int rc = SQLITE_OK; /* Result code */
18340 int i; /* Loop counter */
18341
18342 /* Allocate the parsing context */
18343 pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
18344 if( pParse==0 ){
18345 rc = SQLITE_NOMEM;
18346 goto end_prepare;
18347 }
18348 pParse->pReprepare = pReprepare;
18349 assert( ppStmt && *ppStmt==0 );
18350 assert( !db->mallocFailed );
18351 assert( sqlite3_mutex_held(db->mutex) );
18352
18353 /* Check to verify that it is possible to get a read lock on all
18354 ** database schemas. The inability to get a read lock indicates that
18355 ** some other database connection is holding a write-lock, which in
18356 ** turn means that the other connection has made uncommitted changes
18357 ** to the schema.
18358 **
18359 ** Were we to proceed and prepare the statement against the uncommitted
18360 ** schema changes and if those schema changes are subsequently rolled
18361 ** back and different changes are made in their place, then when this
18362 ** prepared statement goes to run the schema cookie would fail to detect
18363 ** the schema change. Disaster would follow.
18364 **
18365 ** This thread is currently holding mutexes on all Btrees (because
18366 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
18367 ** is not possible for another thread to start a new schema change
18368 ** while this routine is running. Hence, we do not need to hold
18369 ** locks on the schema, we just need to make sure nobody else is
18370 ** holding them.
18371 **
18372 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
18373 ** but it does *not* override schema lock detection, so this all still
18374 ** works even if READ_UNCOMMITTED is set.
18375 */
18376 for(i=0; i<db->nDb; i++) {
18377 Btree *pBt = db->aDb[i].pBt;
18378 if( pBt ){
18379 assert( sqlite3BtreeHoldsMutex(pBt) );
18380 rc = sqlite3BtreeSchemaLocked(pBt);
18381 if( rc ){
18382 const char *zDb = db->aDb[i].zName;
18383 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
18384 testcase( db->flags & SQLITE_ReadUncommitted );
18385 goto end_prepare;
18386 }
18387 }
18388 }
18389
18390 sqlite3VtabUnlockList(db);
18391
18392 pParse->db = db;
18393 pParse->nQueryLoop = 0; /* Logarithmic, so 0 really means 1 */
18394 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
18395 char *zSqlCopy;
18396 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
18397 testcase( nBytes==mxLen );
18398 testcase( nBytes==mxLen+1 );
18399 if( nBytes>mxLen ){
18400 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
18401 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
18402 goto end_prepare;
18403 }
18404 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
18405 if( zSqlCopy ){
18406 sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
18407 sqlite3DbFree(db, zSqlCopy);
18408 pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
18409 }else{
18410 pParse->zTail = &zSql[nBytes];
18411 }
18412 }else{
18413 sqlite3RunParser(pParse, zSql, &zErrMsg);
18414 }
18415 assert( 0==pParse->nQueryLoop );
18416
18417 if( db->mallocFailed ){
18418 pParse->rc = SQLITE_NOMEM;
18419 }
18420 if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
18421 if( pParse->checkSchema ){
18422 schemaIsValid(pParse);
18423 }
18424 if( db->mallocFailed ){
18425 pParse->rc = SQLITE_NOMEM;
18426 }
18427 if( pzTail ){
18428 *pzTail = pParse->zTail;
18429 }
18430 rc = pParse->rc;
18431
18432 #ifndef SQLITE_OMIT_EXPLAIN
18433 if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
18434 static const char * const azColName[] = {
18435 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
18436 "selectid", "order", "from", "detail"
18437 };
18438 int iFirst, mx;
18439 if( pParse->explain==2 ){
18440 sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
18441 iFirst = 8;
18442 mx = 12;
18443 }else{
18444 sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
18445 iFirst = 0;
18446 mx = 8;
18447 }
18448 for(i=iFirst; i<mx; i++){
18449 sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
18450 azColName[i], SQLITE_STATIC);
18451 }
18452 }
18453 #endif
18454
18455 if( db->init.busy==0 ){
18456 Vdbe *pVdbe = pParse->pVdbe;
18457 sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
18458 }
18459 if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
18460 sqlite3VdbeFinalize(pParse->pVdbe);
18461 assert(!(*ppStmt));
18462 }else{
18463 *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
18464 }
18465
18466 if( zErrMsg ){
18467 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
18468 sqlite3DbFree(db, zErrMsg);
18469 }else{
18470 sqlite3Error(db, rc);
18471 }
18472
18473 /* Delete any TriggerPrg structures allocated while parsing this statement. */
18474 while( pParse->pTriggerPrg ){
18475 TriggerPrg *pT = pParse->pTriggerPrg;
18476 pParse->pTriggerPrg = pT->pNext;
18477 sqlite3DbFree(db, pT);
18478 }
18479
18480 end_prepare:
18481
18482 sqlite3ParserReset(pParse);
18483 sqlite3StackFree(db, pParse);
18484 rc = sqlite3ApiExit(db, rc);
18485 assert( (rc&db->errMask)==rc );
18486 return rc;
18487 }
18488 static int sqlite3LockAndPrepare(
18489 sqlite3 *db, /* Database handle. */
18490 const char *zSql, /* UTF-8 encoded SQL statement. */
18491 int nBytes, /* Length of zSql in bytes. */
18492 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
18493 Vdbe *pOld, /* VM being reprepared */
18494 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
18495 const char **pzTail /* OUT: End of parsed string */
18496 ){
18497 int rc;
18498
18499 #ifdef SQLITE_ENABLE_API_ARMOR
18500 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
18501 #endif
18502 *ppStmt = 0;
18503 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
18504 return SQLITE_MISUSE_BKPT;
18505 }
18506 sqlite3_mutex_enter(db->mutex);
18507 sqlite3BtreeEnterAll(db);
18508 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
18509 if( rc==SQLITE_SCHEMA ){
18510 sqlite3_finalize(*ppStmt);
18511 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
18512 }
18513 sqlite3BtreeLeaveAll(db);
18514 sqlite3_mutex_leave(db->mutex);
18515 assert( rc==SQLITE_OK || *ppStmt==0 );
18516 return rc;
18517 }
18518
18519 /*
18520 ** Rerun the compilation of a statement after a schema change.
18521 **
18522 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
18523 ** if the statement cannot be recompiled because another connection has
18524 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
18525 ** occurs, return SQLITE_SCHEMA.
18526 */
18527 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
18528 int rc;
18529 sqlite3_stmt *pNew;
18530 const char *zSql;
18531 sqlite3 *db;
18532
18533 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
18534 zSql = sqlite3_sql((sqlite3_stmt *)p);
18535 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
18536 db = sqlite3VdbeDb(p);
18537 assert( sqlite3_mutex_held(db->mutex) );
18538 rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
18539 if( rc ){
18540 if( rc==SQLITE_NOMEM ){
18541 db->mallocFailed = 1;
18542 }
18543 assert( pNew==0 );
18544 return rc;
18545 }else{
18546 assert( pNew!=0 );
18547 }
18548 sqlite3VdbeSwap((Vdbe*)pNew, p);
18549 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
18550 sqlite3VdbeResetStepResult((Vdbe*)pNew);
18551 sqlite3VdbeFinalize((Vdbe*)pNew);
18552 return SQLITE_OK;
18553 }
18554
18555
18556 /*
18557 ** Two versions of the official API. Legacy and new use. In the legacy
18558 ** version, the original SQL text is not saved in the prepared statement
18559 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
18560 ** sqlite3_step(). In the new version, the original SQL text is retained
18561 ** and the statement is automatically recompiled if an schema change
18562 ** occurs.
18563 */
18564 SQLITE_API int SQLITE_STDCALL sqlite3_prepare(
18565 sqlite3 *db, /* Database handle. */
18566 const char *zSql, /* UTF-8 encoded SQL statement. */
18567 int nBytes, /* Length of zSql in bytes. */
18568 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
18569 const char **pzTail /* OUT: End of parsed string */
18570 ){
18571 int rc;
18572 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
18573 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
18574 return rc;
18575 }
18576 SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2(
18577 sqlite3 *db, /* Database handle. */
18578 const char *zSql, /* UTF-8 encoded SQL statement. */
18579 int nBytes, /* Length of zSql in bytes. */
18580 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
18581 const char **pzTail /* OUT: End of parsed string */
18582 ){
18583 int rc;
18584 rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
18585 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
18586 return rc;
18587 }
18588
18589
18590 #ifndef SQLITE_OMIT_UTF16
18591 /*
18592 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
18593 */
18594 static int sqlite3Prepare16(
18595 sqlite3 *db, /* Database handle. */
18596 const void *zSql, /* UTF-16 encoded SQL statement. */
18597 int nBytes, /* Length of zSql in bytes. */
18598 int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
18599 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
18600 const void **pzTail /* OUT: End of parsed string */
18601 ){
18602 /* This function currently works by first transforming the UTF-16
18603 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
18604 ** tricky bit is figuring out the pointer to return in *pzTail.
18605 */
18606 char *zSql8;
18607 const char *zTail8 = 0;
18608 int rc = SQLITE_OK;
18609
18610 #ifdef SQLITE_ENABLE_API_ARMOR
18611 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
18612 #endif
18613 *ppStmt = 0;
18614 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
18615 return SQLITE_MISUSE_BKPT;
18616 }
18617 if( nBytes>=0 ){
18618 int sz;
18619 const char *z = (const char*)zSql;
18620 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
18621 nBytes = sz;
18622 }
18623 sqlite3_mutex_enter(db->mutex);
18624 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
18625 if( zSql8 ){
18626 rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
18627 }
18628
18629 if( zTail8 && pzTail ){
18630 /* If sqlite3_prepare returns a tail pointer, we calculate the
18631 ** equivalent pointer into the UTF-16 string by counting the unicode
18632 ** characters between zSql8 and zTail8, and then returning a pointer
18633 ** the same number of characters into the UTF-16 string.
18634 */
18635 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
18636 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
18637 }
18638 sqlite3DbFree(db, zSql8);
18639 rc = sqlite3ApiExit(db, rc);
18640 sqlite3_mutex_leave(db->mutex);
18641 return rc;
18642 }
18643
18644 /*
18645 ** Two versions of the official API. Legacy and new use. In the legacy
18646 ** version, the original SQL text is not saved in the prepared statement
18647 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
18648 ** sqlite3_step(). In the new version, the original SQL text is retained
18649 ** and the statement is automatically recompiled if an schema change
18650 ** occurs.
18651 */
18652 SQLITE_API int SQLITE_STDCALL sqlite3_prepare16(
18653 sqlite3 *db, /* Database handle. */
18654 const void *zSql, /* UTF-16 encoded SQL statement. */
18655 int nBytes, /* Length of zSql in bytes. */
18656 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
18657 const void **pzTail /* OUT: End of parsed string */
18658 ){
18659 int rc;
18660 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
18661 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
18662 return rc;
18663 }
18664 SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2(
18665 sqlite3 *db, /* Database handle. */
18666 const void *zSql, /* UTF-16 encoded SQL statement. */
18667 int nBytes, /* Length of zSql in bytes. */
18668 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
18669 const void **pzTail /* OUT: End of parsed string */
18670 ){
18671 int rc;
18672 rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
18673 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
18674 return rc;
18675 }
18676
18677 #endif /* SQLITE_OMIT_UTF16 */
18678
18679 /************** End of prepare.c *********************************************/
18680 /************** Begin file select.c ******************************************/
18681 /*
18682 ** 2001 September 15
18683 **
18684 ** The author disclaims copyright to this source code. In place of
18685 ** a legal notice, here is a blessing:
18686 **
18687 ** May you do good and not evil.
18688 ** May you find forgiveness for yourself and forgive others.
18689 ** May you share freely, never taking more than you give.
18690 **
18691 *************************************************************************
18692 ** This file contains C code routines that are called by the parser
18693 ** to handle SELECT statements in SQLite.
18694 */
18695 /* #include "sqliteInt.h" */
18696
18697 /*
18698 ** Trace output macros
18699 */
18700 #if SELECTTRACE_ENABLED
18701 /***/ int sqlite3SelectTrace = 0;
18702 # define SELECTTRACE(K,P,S,X) \
18703 if(sqlite3SelectTrace&(K)) \
18704 sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
18705 (S)->zSelName,(S)),\
18706 sqlite3DebugPrintf X
18707 #else
18708 # define SELECTTRACE(K,P,S,X)
18709 #endif
18710
18711
18712 /*
18713 ** An instance of the following object is used to record information about
18714 ** how to process the DISTINCT keyword, to simplify passing that information
18715 ** into the selectInnerLoop() routine.
18716 */
18717 typedef struct DistinctCtx DistinctCtx;
18718 struct DistinctCtx {
18719 u8 isTnct; /* True if the DISTINCT keyword is present */
18720 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
18721 int tabTnct; /* Ephemeral table used for DISTINCT processing */
18722 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
18723 };
18724
18725 /*
18726 ** An instance of the following object is used to record information about
18727 ** the ORDER BY (or GROUP BY) clause of query is being coded.
18728 */
18729 typedef struct SortCtx SortCtx;
18730 struct SortCtx {
18731 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
18732 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
18733 int iECursor; /* Cursor number for the sorter */
18734 int regReturn; /* Register holding block-output return address */
18735 int labelBkOut; /* Start label for the block-output subroutine */
18736 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
18737 int labelDone; /* Jump here when done, ex: LIMIT reached */
18738 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
18739 };
18740 #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
18741
18742 /*
18743 ** Delete all the content of a Select structure. Deallocate the structure
18744 ** itself only if bFree is true.
18745 */
18746 static void clearSelect(sqlite3 *db, Select *p, int bFree){
18747 while( p ){
18748 Select *pPrior = p->pPrior;
18749 sqlite3ExprListDelete(db, p->pEList);
18750 sqlite3SrcListDelete(db, p->pSrc);
18751 sqlite3ExprDelete(db, p->pWhere);
18752 sqlite3ExprListDelete(db, p->pGroupBy);
18753 sqlite3ExprDelete(db, p->pHaving);
18754 sqlite3ExprListDelete(db, p->pOrderBy);
18755 sqlite3ExprDelete(db, p->pLimit);
18756 sqlite3ExprDelete(db, p->pOffset);
18757 sqlite3WithDelete(db, p->pWith);
18758 if( bFree ) sqlite3DbFree(db, p);
18759 p = pPrior;
18760 bFree = 1;
18761 }
18762 }
18763
18764 /*
18765 ** Initialize a SelectDest structure.
18766 */
18767 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iPar m){
18768 pDest->eDest = (u8)eDest;
18769 pDest->iSDParm = iParm;
18770 pDest->affSdst = 0;
18771 pDest->iSdst = 0;
18772 pDest->nSdst = 0;
18773 }
18774
18775
18776 /*
18777 ** Allocate a new Select structure and return a pointer to that
18778 ** structure.
18779 */
18780 SQLITE_PRIVATE Select *sqlite3SelectNew(
18781 Parse *pParse, /* Parsing context */
18782 ExprList *pEList, /* which columns to include in the result */
18783 SrcList *pSrc, /* the FROM clause -- which tables to scan */
18784 Expr *pWhere, /* the WHERE clause */
18785 ExprList *pGroupBy, /* the GROUP BY clause */
18786 Expr *pHaving, /* the HAVING clause */
18787 ExprList *pOrderBy, /* the ORDER BY clause */
18788 u16 selFlags, /* Flag parameters, such as SF_Distinct */
18789 Expr *pLimit, /* LIMIT value. NULL means not used */
18790 Expr *pOffset /* OFFSET value. NULL means no offset */
18791 ){
18792 Select *pNew;
18793 Select standin;
18794 sqlite3 *db = pParse->db;
18795 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
18796 if( pNew==0 ){
18797 assert( db->mallocFailed );
18798 pNew = &standin;
18799 memset(pNew, 0, sizeof(*pNew));
18800 }
18801 if( pEList==0 ){
18802 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ASTERISK,0));
18803 }
18804 pNew->pEList = pEList;
18805 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
18806 pNew->pSrc = pSrc;
18807 pNew->pWhere = pWhere;
18808 pNew->pGroupBy = pGroupBy;
18809 pNew->pHaving = pHaving;
18810 pNew->pOrderBy = pOrderBy;
18811 pNew->selFlags = selFlags;
18812 pNew->op = TK_SELECT;
18813 pNew->pLimit = pLimit;
18814 pNew->pOffset = pOffset;
18815 assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 );
18816 pNew->addrOpenEphm[0] = -1;
18817 pNew->addrOpenEphm[1] = -1;
18818 if( db->mallocFailed ) {
18819 clearSelect(db, pNew, pNew!=&standin);
18820 pNew = 0;
18821 }else{
18822 assert( pNew->pSrc!=0 || pParse->nErr>0 );
18823 }
18824 assert( pNew!=&standin );
18825 return pNew;
18826 }
18827
18828 #if SELECTTRACE_ENABLED
18829 /*
18830 ** Set the name of a Select object
18831 */
18832 SQLITE_PRIVATE void sqlite3SelectSetName(Select *p, const char *zName){
18833 if( p && zName ){
18834 sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName);
18835 }
18836 }
18837 #endif
18838
18839
18840 /*
18841 ** Delete the given Select structure and all of its substructures.
18842 */
18843 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
18844 clearSelect(db, p, 1);
18845 }
18846
18847 /*
18848 ** Return a pointer to the right-most SELECT statement in a compound.
18849 */
18850 static Select *findRightmost(Select *p){
18851 while( p->pNext ) p = p->pNext;
18852 return p;
18853 }
18854
18855 /*
18856 ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
18857 ** type of join. Return an integer constant that expresses that type
18858 ** in terms of the following bit values:
18859 **
18860 ** JT_INNER
18861 ** JT_CROSS
18862 ** JT_OUTER
18863 ** JT_NATURAL
18864 ** JT_LEFT
18865 ** JT_RIGHT
18866 **
18867 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
18868 **
18869 ** If an illegal or unsupported join type is seen, then still return
18870 ** a join type, but put an error in the pParse structure.
18871 */
18872 SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *p C){
18873 int jointype = 0;
18874 Token *apAll[3];
18875 Token *p;
18876 /* 0123456789 123456789 123456789 123 */
18877 static const char zKeyText[] = "naturaleftouterightfullinnercross";
18878 static const struct {
18879 u8 i; /* Beginning of keyword text in zKeyText[] */
18880 u8 nChar; /* Length of the keyword in characters */
18881 u8 code; /* Join type mask */
18882 } aKeyword[] = {
18883 /* natural */ { 0, 7, JT_NATURAL },
18884 /* left */ { 6, 4, JT_LEFT|JT_OUTER },
18885 /* outer */ { 10, 5, JT_OUTER },
18886 /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
18887 /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
18888 /* inner */ { 23, 5, JT_INNER },
18889 /* cross */ { 28, 5, JT_INNER|JT_CROSS },
18890 };
18891 int i, j;
18892 apAll[0] = pA;
18893 apAll[1] = pB;
18894 apAll[2] = pC;
18895 for(i=0; i<3 && apAll[i]; i++){
18896 p = apAll[i];
18897 for(j=0; j<ArraySize(aKeyword); j++){
18898 if( p->n==aKeyword[j].nChar
18899 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
18900 jointype |= aKeyword[j].code;
18901 break;
18902 }
18903 }
18904 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
18905 if( j>=ArraySize(aKeyword) ){
18906 jointype |= JT_ERROR;
18907 break;
18908 }
18909 }
18910 if(
18911 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
18912 (jointype & JT_ERROR)!=0
18913 ){
18914 const char *zSp = " ";
18915 assert( pB!=0 );
18916 if( pC==0 ){ zSp++; }
18917 sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
18918 "%T %T%s%T", pA, pB, zSp, pC);
18919 jointype = JT_INNER;
18920 }else if( (jointype & JT_OUTER)!=0
18921 && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
18922 sqlite3ErrorMsg(pParse,
18923 "RIGHT and FULL OUTER JOINs are not currently supported");
18924 jointype = JT_INNER;
18925 }
18926 return jointype;
18927 }
18928
18929 /*
18930 ** Return the index of a column in a table. Return -1 if the column
18931 ** is not contained in the table.
18932 */
18933 static int columnIndex(Table *pTab, const char *zCol){
18934 int i;
18935 for(i=0; i<pTab->nCol; i++){
18936 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
18937 }
18938 return -1;
18939 }
18940
18941 /*
18942 ** Search the first N tables in pSrc, from left to right, looking for a
18943 ** table that has a column named zCol.
18944 **
18945 ** When found, set *piTab and *piCol to the table index and column index
18946 ** of the matching column and return TRUE.
18947 **
18948 ** If not found, return FALSE.
18949 */
18950 static int tableAndColumnIndex(
18951 SrcList *pSrc, /* Array of tables to search */
18952 int N, /* Number of tables in pSrc->a[] to search */
18953 const char *zCol, /* Name of the column we are looking for */
18954 int *piTab, /* Write index of pSrc->a[] here */
18955 int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
18956 ){
18957 int i; /* For looping over tables in pSrc */
18958 int iCol; /* Index of column matching zCol */
18959
18960 assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
18961 for(i=0; i<N; i++){
18962 iCol = columnIndex(pSrc->a[i].pTab, zCol);
18963 if( iCol>=0 ){
18964 if( piTab ){
18965 *piTab = i;
18966 *piCol = iCol;
18967 }
18968 return 1;
18969 }
18970 }
18971 return 0;
18972 }
18973
18974 /*
18975 ** This function is used to add terms implied by JOIN syntax to the
18976 ** WHERE clause expression of a SELECT statement. The new term, which
18977 ** is ANDed with the existing WHERE clause, is of the form:
18978 **
18979 ** (tab1.col1 = tab2.col2)
18980 **
18981 ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
18982 ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
18983 ** column iColRight of tab2.
18984 */
18985 static void addWhereTerm(
18986 Parse *pParse, /* Parsing context */
18987 SrcList *pSrc, /* List of tables in FROM clause */
18988 int iLeft, /* Index of first table to join in pSrc */
18989 int iColLeft, /* Index of column in first table */
18990 int iRight, /* Index of second table in pSrc */
18991 int iColRight, /* Index of column in second table */
18992 int isOuterJoin, /* True if this is an OUTER join */
18993 Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
18994 ){
18995 sqlite3 *db = pParse->db;
18996 Expr *pE1;
18997 Expr *pE2;
18998 Expr *pEq;
18999
19000 assert( iLeft<iRight );
19001 assert( pSrc->nSrc>iRight );
19002 assert( pSrc->a[iLeft].pTab );
19003 assert( pSrc->a[iRight].pTab );
19004
19005 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
19006 pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
19007
19008 pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
19009 if( pEq && isOuterJoin ){
19010 ExprSetProperty(pEq, EP_FromJoin);
19011 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
19012 ExprSetVVAProperty(pEq, EP_NoReduce);
19013 pEq->iRightJoinTable = (i16)pE2->iTable;
19014 }
19015 *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
19016 }
19017
19018 /*
19019 ** Set the EP_FromJoin property on all terms of the given expression.
19020 ** And set the Expr.iRightJoinTable to iTable for every term in the
19021 ** expression.
19022 **
19023 ** The EP_FromJoin property is used on terms of an expression to tell
19024 ** the LEFT OUTER JOIN processing logic that this term is part of the
19025 ** join restriction specified in the ON or USING clause and not a part
19026 ** of the more general WHERE clause. These terms are moved over to the
19027 ** WHERE clause during join processing but we need to remember that they
19028 ** originated in the ON or USING clause.
19029 **
19030 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
19031 ** expression depends on table iRightJoinTable even if that table is not
19032 ** explicitly mentioned in the expression. That information is needed
19033 ** for cases like this:
19034 **
19035 ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
19036 **
19037 ** The where clause needs to defer the handling of the t1.x=5
19038 ** term until after the t2 loop of the join. In that way, a
19039 ** NULL t2 row will be inserted whenever t1.x!=5. If we do not
19040 ** defer the handling of t1.x=5, it will be processed immediately
19041 ** after the t1 loop and rows with t1.x!=5 will never appear in
19042 ** the output, which is incorrect.
19043 */
19044 static void setJoinExpr(Expr *p, int iTable){
19045 while( p ){
19046 ExprSetProperty(p, EP_FromJoin);
19047 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
19048 ExprSetVVAProperty(p, EP_NoReduce);
19049 p->iRightJoinTable = (i16)iTable;
19050 if( p->op==TK_FUNCTION && p->x.pList ){
19051 int i;
19052 for(i=0; i<p->x.pList->nExpr; i++){
19053 setJoinExpr(p->x.pList->a[i].pExpr, iTable);
19054 }
19055 }
19056 setJoinExpr(p->pLeft, iTable);
19057 p = p->pRight;
19058 }
19059 }
19060
19061 /*
19062 ** This routine processes the join information for a SELECT statement.
19063 ** ON and USING clauses are converted into extra terms of the WHERE clause.
19064 ** NATURAL joins also create extra WHERE clause terms.
19065 **
19066 ** The terms of a FROM clause are contained in the Select.pSrc structure.
19067 ** The left most table is the first entry in Select.pSrc. The right-most
19068 ** table is the last entry. The join operator is held in the entry to
19069 ** the left. Thus entry 0 contains the join operator for the join between
19070 ** entries 0 and 1. Any ON or USING clauses associated with the join are
19071 ** also attached to the left entry.
19072 **
19073 ** This routine returns the number of errors encountered.
19074 */
19075 static int sqliteProcessJoin(Parse *pParse, Select *p){
19076 SrcList *pSrc; /* All tables in the FROM clause */
19077 int i, j; /* Loop counters */
19078 struct SrcList_item *pLeft; /* Left table being joined */
19079 struct SrcList_item *pRight; /* Right table being joined */
19080
19081 pSrc = p->pSrc;
19082 pLeft = &pSrc->a[0];
19083 pRight = &pLeft[1];
19084 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
19085 Table *pLeftTab = pLeft->pTab;
19086 Table *pRightTab = pRight->pTab;
19087 int isOuter;
19088
19089 if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
19090 isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
19091
19092 /* When the NATURAL keyword is present, add WHERE clause terms for
19093 ** every column that the two tables have in common.
19094 */
19095 if( pRight->fg.jointype & JT_NATURAL ){
19096 if( pRight->pOn || pRight->pUsing ){
19097 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
19098 "an ON or USING clause", 0);
19099 return 1;
19100 }
19101 for(j=0; j<pRightTab->nCol; j++){
19102 char *zName; /* Name of column in the right table */
19103 int iLeft; /* Matching left table */
19104 int iLeftCol; /* Matching column in the left table */
19105
19106 zName = pRightTab->aCol[j].zName;
19107 if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
19108 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
19109 isOuter, &p->pWhere);
19110 }
19111 }
19112 }
19113
19114 /* Disallow both ON and USING clauses in the same join
19115 */
19116 if( pRight->pOn && pRight->pUsing ){
19117 sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
19118 "clauses in the same join");
19119 return 1;
19120 }
19121
19122 /* Add the ON clause to the end of the WHERE clause, connected by
19123 ** an AND operator.
19124 */
19125 if( pRight->pOn ){
19126 if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
19127 p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
19128 pRight->pOn = 0;
19129 }
19130
19131 /* Create extra terms on the WHERE clause for each column named
19132 ** in the USING clause. Example: If the two tables to be joined are
19133 ** A and B and the USING clause names X, Y, and Z, then add this
19134 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
19135 ** Report an error if any column mentioned in the USING clause is
19136 ** not contained in both tables to be joined.
19137 */
19138 if( pRight->pUsing ){
19139 IdList *pList = pRight->pUsing;
19140 for(j=0; j<pList->nId; j++){
19141 char *zName; /* Name of the term in the USING clause */
19142 int iLeft; /* Table on the left with matching column name */
19143 int iLeftCol; /* Column number of matching column on the left */
19144 int iRightCol; /* Column number of matching column on the right */
19145
19146 zName = pList->a[j].zName;
19147 iRightCol = columnIndex(pRightTab, zName);
19148 if( iRightCol<0
19149 || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
19150 ){
19151 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
19152 "not present in both tables", zName);
19153 return 1;
19154 }
19155 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
19156 isOuter, &p->pWhere);
19157 }
19158 }
19159 }
19160 return 0;
19161 }
19162
19163 /* Forward reference */
19164 static KeyInfo *keyInfoFromExprList(
19165 Parse *pParse, /* Parsing context */
19166 ExprList *pList, /* Form the KeyInfo object from this ExprList */
19167 int iStart, /* Begin with this column of pList */
19168 int nExtra /* Add this many extra columns to the end */
19169 );
19170
19171 /*
19172 ** Generate code that will push the record in registers regData
19173 ** through regData+nData-1 onto the sorter.
19174 */
19175 static void pushOntoSorter(
19176 Parse *pParse, /* Parser context */
19177 SortCtx *pSort, /* Information about the ORDER BY clause */
19178 Select *pSelect, /* The whole SELECT statement */
19179 int regData, /* First register holding data to be sorted */
19180 int regOrigData, /* First register holding data before packing */
19181 int nData, /* Number of elements in the data array */
19182 int nPrefixReg /* No. of reg prior to regData available for use */
19183 ){
19184 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
19185 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
19186 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
19187 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
19188 int regBase; /* Regs for sorter record */
19189 int regRecord = ++pParse->nMem; /* Assembled sorter record */
19190 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
19191 int op; /* Opcode to add sorter record to sorter */
19192 int iLimit; /* LIMIT counter */
19193
19194 assert( bSeq==0 || bSeq==1 );
19195 assert( nData==1 || regData==regOrigData );
19196 if( nPrefixReg ){
19197 assert( nPrefixReg==nExpr+bSeq );
19198 regBase = regData - nExpr - bSeq;
19199 }else{
19200 regBase = pParse->nMem + 1;
19201 pParse->nMem += nBase;
19202 }
19203 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
19204 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
19205 pSort->labelDone = sqlite3VdbeMakeLabel(v);
19206 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
19207 SQLITE_ECEL_DUP|SQLITE_ECEL_REF);
19208 if( bSeq ){
19209 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
19210 }
19211 if( nPrefixReg==0 ){
19212 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
19213 }
19214 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
19215 if( nOBSat>0 ){
19216 int regPrevKey; /* The first nOBSat columns of the previous row */
19217 int addrFirst; /* Address of the OP_IfNot opcode */
19218 int addrJmp; /* Address of the OP_Jump opcode */
19219 VdbeOp *pOp; /* Opcode that opens the sorter */
19220 int nKey; /* Number of sorting key columns, including OP_Sequence */
19221 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
19222
19223 regPrevKey = pParse->nMem+1;
19224 pParse->nMem += pSort->nOBSat;
19225 nKey = nExpr - pSort->nOBSat + bSeq;
19226 if( bSeq ){
19227 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
19228 }else{
19229 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
19230 }
19231 VdbeCoverage(v);
19232 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
19233 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
19234 if( pParse->db->mallocFailed ) return;
19235 pOp->p2 = nKey + nData;
19236 pKI = pOp->p4.pKeyInfo;
19237 memset(pKI->aSortOrder, 0, pKI->nField); /* Makes OP_Jump below testable */
19238 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
19239 testcase( pKI->nXField>2 );
19240 pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat,
19241 pKI->nXField-1);
19242 addrJmp = sqlite3VdbeCurrentAddr(v);
19243 sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
19244 pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
19245 pSort->regReturn = ++pParse->nMem;
19246 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
19247 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
19248 if( iLimit ){
19249 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
19250 VdbeCoverage(v);
19251 }
19252 sqlite3VdbeJumpHere(v, addrFirst);
19253 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
19254 sqlite3VdbeJumpHere(v, addrJmp);
19255 }
19256 if( pSort->sortFlags & SORTFLAG_UseSorter ){
19257 op = OP_SorterInsert;
19258 }else{
19259 op = OP_IdxInsert;
19260 }
19261 sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord);
19262 if( iLimit ){
19263 int addr;
19264 addr = sqlite3VdbeAddOp3(v, OP_IfNotZero, iLimit, 0, 1); VdbeCoverage(v);
19265 sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
19266 sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor);
19267 sqlite3VdbeJumpHere(v, addr);
19268 }
19269 }
19270
19271 /*
19272 ** Add code to implement the OFFSET
19273 */
19274 static void codeOffset(
19275 Vdbe *v, /* Generate code into this VM */
19276 int iOffset, /* Register holding the offset counter */
19277 int iContinue /* Jump here to skip the current record */
19278 ){
19279 if( iOffset>0 ){
19280 sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);
19281 VdbeComment((v, "OFFSET"));
19282 }
19283 }
19284
19285 /*
19286 ** Add code that will check to make sure the N registers starting at iMem
19287 ** form a distinct entry. iTab is a sorting index that holds previously
19288 ** seen combinations of the N values. A new entry is made in iTab
19289 ** if the current N values are new.
19290 **
19291 ** A jump to addrRepeat is made and the N+1 values are popped from the
19292 ** stack if the top N elements are not distinct.
19293 */
19294 static void codeDistinct(
19295 Parse *pParse, /* Parsing and code generating context */
19296 int iTab, /* A sorting index used to test for distinctness */
19297 int addrRepeat, /* Jump to here if not distinct */
19298 int N, /* Number of elements */
19299 int iMem /* First element */
19300 ){
19301 Vdbe *v;
19302 int r1;
19303
19304 v = pParse->pVdbe;
19305 r1 = sqlite3GetTempReg(pParse);
19306 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
19307 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
19308 sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
19309 sqlite3ReleaseTempReg(pParse, r1);
19310 }
19311
19312 #ifndef SQLITE_OMIT_SUBQUERY
19313 /*
19314 ** Generate an error message when a SELECT is used within a subexpression
19315 ** (example: "a IN (SELECT * FROM table)") but it has more than 1 result
19316 ** column. We do this in a subroutine because the error used to occur
19317 ** in multiple places. (The error only occurs in one place now, but we
19318 ** retain the subroutine to minimize code disruption.)
19319 */
19320 static int checkForMultiColumnSelectError(
19321 Parse *pParse, /* Parse context. */
19322 SelectDest *pDest, /* Destination of SELECT results */
19323 int nExpr /* Number of result columns returned by SELECT */
19324 ){
19325 int eDest = pDest->eDest;
19326 if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
19327 sqlite3ErrorMsg(pParse, "only a single result allowed for "
19328 "a SELECT that is part of an expression");
19329 return 1;
19330 }else{
19331 return 0;
19332 }
19333 }
19334 #endif
19335
19336 /*
19337 ** This routine generates the code for the inside of the inner loop
19338 ** of a SELECT.
19339 **
19340 ** If srcTab is negative, then the pEList expressions
19341 ** are evaluated in order to get the data for this row. If srcTab is
19342 ** zero or more, then data is pulled from srcTab and pEList is used only
19343 ** to get number columns and the datatype for each column.
19344 */
19345 static void selectInnerLoop(
19346 Parse *pParse, /* The parser context */
19347 Select *p, /* The complete select statement being coded */
19348 ExprList *pEList, /* List of values being extracted */
19349 int srcTab, /* Pull data from this table */
19350 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
19351 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
19352 SelectDest *pDest, /* How to dispose of the results */
19353 int iContinue, /* Jump here to continue with next row */
19354 int iBreak /* Jump here to break out of the inner loop */
19355 ){
19356 Vdbe *v = pParse->pVdbe;
19357 int i;
19358 int hasDistinct; /* True if the DISTINCT keyword is present */
19359 int regResult; /* Start of memory holding result set */
19360 int eDest = pDest->eDest; /* How to dispose of results */
19361 int iParm = pDest->iSDParm; /* First argument to disposal method */
19362 int nResultCol; /* Number of result columns */
19363 int nPrefixReg = 0; /* Number of extra registers before regResult */
19364
19365 assert( v );
19366 assert( pEList!=0 );
19367 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
19368 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
19369 if( pSort==0 && !hasDistinct ){
19370 assert( iContinue!=0 );
19371 codeOffset(v, p->iOffset, iContinue);
19372 }
19373
19374 /* Pull the requested columns.
19375 */
19376 nResultCol = pEList->nExpr;
19377
19378 if( pDest->iSdst==0 ){
19379 if( pSort ){
19380 nPrefixReg = pSort->pOrderBy->nExpr;
19381 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
19382 pParse->nMem += nPrefixReg;
19383 }
19384 pDest->iSdst = pParse->nMem+1;
19385 pParse->nMem += nResultCol;
19386 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
19387 /* This is an error condition that can result, for example, when a SELECT
19388 ** on the right-hand side of an INSERT contains more result columns than
19389 ** there are columns in the table on the left. The error will be caught
19390 ** and reported later. But we need to make sure enough memory is allocated
19391 ** to avoid other spurious errors in the meantime. */
19392 pParse->nMem += nResultCol;
19393 }
19394 pDest->nSdst = nResultCol;
19395 regResult = pDest->iSdst;
19396 if( srcTab>=0 ){
19397 for(i=0; i<nResultCol; i++){
19398 sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
19399 VdbeComment((v, "%s", pEList->a[i].zName));
19400 }
19401 }else if( eDest!=SRT_Exists ){
19402 /* If the destination is an EXISTS(...) expression, the actual
19403 ** values returned by the SELECT are not required.
19404 */
19405 u8 ecelFlags;
19406 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
19407 ecelFlags = SQLITE_ECEL_DUP;
19408 }else{
19409 ecelFlags = 0;
19410 }
19411 sqlite3ExprCodeExprList(pParse, pEList, regResult, 0, ecelFlags);
19412 }
19413
19414 /* If the DISTINCT keyword was present on the SELECT statement
19415 ** and this row has been seen before, then do not make this row
19416 ** part of the result.
19417 */
19418 if( hasDistinct ){
19419 switch( pDistinct->eTnctType ){
19420 case WHERE_DISTINCT_ORDERED: {
19421 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
19422 int iJump; /* Jump destination */
19423 int regPrev; /* Previous row content */
19424
19425 /* Allocate space for the previous row */
19426 regPrev = pParse->nMem+1;
19427 pParse->nMem += nResultCol;
19428
19429 /* Change the OP_OpenEphemeral coded earlier to an OP_Null
19430 ** sets the MEM_Cleared bit on the first register of the
19431 ** previous value. This will cause the OP_Ne below to always
19432 ** fail on the first iteration of the loop even if the first
19433 ** row is all NULLs.
19434 */
19435 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
19436 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
19437 pOp->opcode = OP_Null;
19438 pOp->p1 = 1;
19439 pOp->p2 = regPrev;
19440
19441 iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
19442 for(i=0; i<nResultCol; i++){
19443 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
19444 if( i<nResultCol-1 ){
19445 sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
19446 VdbeCoverage(v);
19447 }else{
19448 sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
19449 VdbeCoverage(v);
19450 }
19451 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
19452 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
19453 }
19454 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
19455 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
19456 break;
19457 }
19458
19459 case WHERE_DISTINCT_UNIQUE: {
19460 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
19461 break;
19462 }
19463
19464 default: {
19465 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
19466 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
19467 regResult);
19468 break;
19469 }
19470 }
19471 if( pSort==0 ){
19472 codeOffset(v, p->iOffset, iContinue);
19473 }
19474 }
19475
19476 switch( eDest ){
19477 /* In this mode, write each query result to the key of the temporary
19478 ** table iParm.
19479 */
19480 #ifndef SQLITE_OMIT_COMPOUND_SELECT
19481 case SRT_Union: {
19482 int r1;
19483 r1 = sqlite3GetTempReg(pParse);
19484 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
19485 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
19486 sqlite3ReleaseTempReg(pParse, r1);
19487 break;
19488 }
19489
19490 /* Construct a record from the query result, but instead of
19491 ** saving that record, use it as a key to delete elements from
19492 ** the temporary table iParm.
19493 */
19494 case SRT_Except: {
19495 sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
19496 break;
19497 }
19498 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
19499
19500 /* Store the result as data using a unique key.
19501 */
19502 case SRT_Fifo:
19503 case SRT_DistFifo:
19504 case SRT_Table:
19505 case SRT_EphemTab: {
19506 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
19507 testcase( eDest==SRT_Table );
19508 testcase( eDest==SRT_EphemTab );
19509 testcase( eDest==SRT_Fifo );
19510 testcase( eDest==SRT_DistFifo );
19511 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
19512 #ifndef SQLITE_OMIT_CTE
19513 if( eDest==SRT_DistFifo ){
19514 /* If the destination is DistFifo, then cursor (iParm+1) is open
19515 ** on an ephemeral index. If the current row is already present
19516 ** in the index, do not write it to the output. If not, add the
19517 ** current row to the index and proceed with writing it to the
19518 ** output table as well. */
19519 int addr = sqlite3VdbeCurrentAddr(v) + 4;
19520 sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
19521 VdbeCoverage(v);
19522 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
19523 assert( pSort==0 );
19524 }
19525 #endif
19526 if( pSort ){
19527 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg);
19528 }else{
19529 int r2 = sqlite3GetTempReg(pParse);
19530 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
19531 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
19532 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
19533 sqlite3ReleaseTempReg(pParse, r2);
19534 }
19535 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
19536 break;
19537 }
19538
19539 #ifndef SQLITE_OMIT_SUBQUERY
19540 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
19541 ** then there should be a single item on the stack. Write this
19542 ** item into the set table with bogus data.
19543 */
19544 case SRT_Set: {
19545 assert( nResultCol==1 );
19546 pDest->affSdst =
19547 sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
19548 if( pSort ){
19549 /* At first glance you would think we could optimize out the
19550 ** ORDER BY in this case since the order of entries in the set
19551 ** does not matter. But there might be a LIMIT clause, in which
19552 ** case the order does matter */
19553 pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg);
19554 }else{
19555 int r1 = sqlite3GetTempReg(pParse);
19556 sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
19557 sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
19558 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
19559 sqlite3ReleaseTempReg(pParse, r1);
19560 }
19561 break;
19562 }
19563
19564 /* If any row exist in the result set, record that fact and abort.
19565 */
19566 case SRT_Exists: {
19567 sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
19568 /* The LIMIT clause will terminate the loop for us */
19569 break;
19570 }
19571
19572 /* If this is a scalar select that is part of an expression, then
19573 ** store the results in the appropriate memory cell and break out
19574 ** of the scan loop.
19575 */
19576 case SRT_Mem: {
19577 assert( nResultCol==1 );
19578 if( pSort ){
19579 pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg);
19580 }else{
19581 assert( regResult==iParm );
19582 /* The LIMIT clause will jump out of the loop for us */
19583 }
19584 break;
19585 }
19586 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
19587
19588 case SRT_Coroutine: /* Send data to a co-routine */
19589 case SRT_Output: { /* Return the results */
19590 testcase( eDest==SRT_Coroutine );
19591 testcase( eDest==SRT_Output );
19592 if( pSort ){
19593 pushOntoSorter(pParse, pSort, p, regResult, regResult, nResultCol,
19594 nPrefixReg);
19595 }else if( eDest==SRT_Coroutine ){
19596 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
19597 }else{
19598 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
19599 sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
19600 }
19601 break;
19602 }
19603
19604 #ifndef SQLITE_OMIT_CTE
19605 /* Write the results into a priority queue that is order according to
19606 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
19607 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
19608 ** pSO->nExpr columns, then make sure all keys are unique by adding a
19609 ** final OP_Sequence column. The last column is the record as a blob.
19610 */
19611 case SRT_DistQueue:
19612 case SRT_Queue: {
19613 int nKey;
19614 int r1, r2, r3;
19615 int addrTest = 0;
19616 ExprList *pSO;
19617 pSO = pDest->pOrderBy;
19618 assert( pSO );
19619 nKey = pSO->nExpr;
19620 r1 = sqlite3GetTempReg(pParse);
19621 r2 = sqlite3GetTempRange(pParse, nKey+2);
19622 r3 = r2+nKey+1;
19623 if( eDest==SRT_DistQueue ){
19624 /* If the destination is DistQueue, then cursor (iParm+1) is open
19625 ** on a second ephemeral index that holds all values every previously
19626 ** added to the queue. */
19627 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
19628 regResult, nResultCol);
19629 VdbeCoverage(v);
19630 }
19631 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
19632 if( eDest==SRT_DistQueue ){
19633 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
19634 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
19635 }
19636 for(i=0; i<nKey; i++){
19637 sqlite3VdbeAddOp2(v, OP_SCopy,
19638 regResult + pSO->a[i].u.x.iOrderByCol - 1,
19639 r2+i);
19640 }
19641 sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
19642 sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
19643 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
19644 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
19645 sqlite3ReleaseTempReg(pParse, r1);
19646 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
19647 break;
19648 }
19649 #endif /* SQLITE_OMIT_CTE */
19650
19651
19652
19653 #if !defined(SQLITE_OMIT_TRIGGER)
19654 /* Discard the results. This is used for SELECT statements inside
19655 ** the body of a TRIGGER. The purpose of such selects is to call
19656 ** user-defined functions that have side effects. We do not care
19657 ** about the actual results of the select.
19658 */
19659 default: {
19660 assert( eDest==SRT_Discard );
19661 break;
19662 }
19663 #endif
19664 }
19665
19666 /* Jump to the end of the loop if the LIMIT is reached. Except, if
19667 ** there is a sorter, in which case the sorter has already limited
19668 ** the output for us.
19669 */
19670 if( pSort==0 && p->iLimit ){
19671 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
19672 }
19673 }
19674
19675 /*
19676 ** Allocate a KeyInfo object sufficient for an index of N key columns and
19677 ** X extra columns.
19678 */
19679 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
19680 KeyInfo *p = sqlite3DbMallocZero(0,
19681 sizeof(KeyInfo) + (N+X)*(sizeof(CollSeq*)+1));
19682 if( p ){
19683 p->aSortOrder = (u8*)&p->aColl[N+X];
19684 p->nField = (u16)N;
19685 p->nXField = (u16)X;
19686 p->enc = ENC(db);
19687 p->db = db;
19688 p->nRef = 1;
19689 }else{
19690 db->mallocFailed = 1;
19691 }
19692 return p;
19693 }
19694
19695 /*
19696 ** Deallocate a KeyInfo object
19697 */
19698 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
19699 if( p ){
19700 assert( p->nRef>0 );
19701 p->nRef--;
19702 if( p->nRef==0 ) sqlite3DbFree(0, p);
19703 }
19704 }
19705
19706 /*
19707 ** Make a new pointer to a KeyInfo object
19708 */
19709 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
19710 if( p ){
19711 assert( p->nRef>0 );
19712 p->nRef++;
19713 }
19714 return p;
19715 }
19716
19717 #ifdef SQLITE_DEBUG
19718 /*
19719 ** Return TRUE if a KeyInfo object can be change. The KeyInfo object
19720 ** can only be changed if this is just a single reference to the object.
19721 **
19722 ** This routine is used only inside of assert() statements.
19723 */
19724 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
19725 #endif /* SQLITE_DEBUG */
19726
19727 /*
19728 ** Given an expression list, generate a KeyInfo structure that records
19729 ** the collating sequence for each expression in that expression list.
19730 **
19731 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
19732 ** KeyInfo structure is appropriate for initializing a virtual index to
19733 ** implement that clause. If the ExprList is the result set of a SELECT
19734 ** then the KeyInfo structure is appropriate for initializing a virtual
19735 ** index to implement a DISTINCT test.
19736 **
19737 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
19738 ** function is responsible for seeing that this structure is eventually
19739 ** freed.
19740 */
19741 static KeyInfo *keyInfoFromExprList(
19742 Parse *pParse, /* Parsing context */
19743 ExprList *pList, /* Form the KeyInfo object from this ExprList */
19744 int iStart, /* Begin with this column of pList */
19745 int nExtra /* Add this many extra columns to the end */
19746 ){
19747 int nExpr;
19748 KeyInfo *pInfo;
19749 struct ExprList_item *pItem;
19750 sqlite3 *db = pParse->db;
19751 int i;
19752
19753 nExpr = pList->nExpr;
19754 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
19755 if( pInfo ){
19756 assert( sqlite3KeyInfoIsWriteable(pInfo) );
19757 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
19758 CollSeq *pColl;
19759 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
19760 if( !pColl ) pColl = db->pDfltColl;
19761 pInfo->aColl[i-iStart] = pColl;
19762 pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
19763 }
19764 }
19765 return pInfo;
19766 }
19767
19768 /*
19769 ** Name of the connection operator, used for error messages.
19770 */
19771 static const char *selectOpName(int id){
19772 char *z;
19773 switch( id ){
19774 case TK_ALL: z = "UNION ALL"; break;
19775 case TK_INTERSECT: z = "INTERSECT"; break;
19776 case TK_EXCEPT: z = "EXCEPT"; break;
19777 default: z = "UNION"; break;
19778 }
19779 return z;
19780 }
19781
19782 #ifndef SQLITE_OMIT_EXPLAIN
19783 /*
19784 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
19785 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
19786 ** where the caption is of the form:
19787 **
19788 ** "USE TEMP B-TREE FOR xxx"
19789 **
19790 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
19791 ** is determined by the zUsage argument.
19792 */
19793 static void explainTempTable(Parse *pParse, const char *zUsage){
19794 if( pParse->explain==2 ){
19795 Vdbe *v = pParse->pVdbe;
19796 char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
19797 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
19798 }
19799 }
19800
19801 /*
19802 ** Assign expression b to lvalue a. A second, no-op, version of this macro
19803 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
19804 ** in sqlite3Select() to assign values to structure member variables that
19805 ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
19806 ** code with #ifndef directives.
19807 */
19808 # define explainSetInteger(a, b) a = b
19809
19810 #else
19811 /* No-op versions of the explainXXX() functions and macros. */
19812 # define explainTempTable(y,z)
19813 # define explainSetInteger(y,z)
19814 #endif
19815
19816 #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
19817 /*
19818 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
19819 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
19820 ** where the caption is of one of the two forms:
19821 **
19822 ** "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
19823 ** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
19824 **
19825 ** where iSub1 and iSub2 are the integers passed as the corresponding
19826 ** function parameters, and op is the text representation of the parameter
19827 ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
19828 ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
19829 ** false, or the second form if it is true.
19830 */
19831 static void explainComposite(
19832 Parse *pParse, /* Parse context */
19833 int op, /* One of TK_UNION, TK_EXCEPT etc. */
19834 int iSub1, /* Subquery id 1 */
19835 int iSub2, /* Subquery id 2 */
19836 int bUseTmp /* True if a temp table was used */
19837 ){
19838 assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
19839 if( pParse->explain==2 ){
19840 Vdbe *v = pParse->pVdbe;
19841 char *zMsg = sqlite3MPrintf(
19842 pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
19843 bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
19844 );
19845 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
19846 }
19847 }
19848 #else
19849 /* No-op versions of the explainXXX() functions and macros. */
19850 # define explainComposite(v,w,x,y,z)
19851 #endif
19852
19853 /*
19854 ** If the inner loop was generated using a non-null pOrderBy argument,
19855 ** then the results were placed in a sorter. After the loop is terminated
19856 ** we need to run the sorter and output the results. The following
19857 ** routine generates the code needed to do that.
19858 */
19859 static void generateSortTail(
19860 Parse *pParse, /* Parsing context */
19861 Select *p, /* The SELECT statement */
19862 SortCtx *pSort, /* Information on the ORDER BY clause */
19863 int nColumn, /* Number of columns of data */
19864 SelectDest *pDest /* Write the sorted results here */
19865 ){
19866 Vdbe *v = pParse->pVdbe; /* The prepared statement */
19867 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
19868 int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */
19869 int addr;
19870 int addrOnce = 0;
19871 int iTab;
19872 ExprList *pOrderBy = pSort->pOrderBy;
19873 int eDest = pDest->eDest;
19874 int iParm = pDest->iSDParm;
19875 int regRow;
19876 int regRowid;
19877 int nKey;
19878 int iSortTab; /* Sorter cursor to read from */
19879 int nSortData; /* Trailing values to read from sorter */
19880 int i;
19881 int bSeq; /* True if sorter record includes seq. no. */
19882 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
19883 struct ExprList_item *aOutEx = p->pEList->a;
19884 #endif
19885
19886 assert( addrBreak<0 );
19887 if( pSort->labelBkOut ){
19888 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
19889 sqlite3VdbeGoto(v, addrBreak);
19890 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
19891 }
19892 iTab = pSort->iECursor;
19893 if( eDest==SRT_Output || eDest==SRT_Coroutine ){
19894 regRowid = 0;
19895 regRow = pDest->iSdst;
19896 nSortData = nColumn;
19897 }else{
19898 regRowid = sqlite3GetTempReg(pParse);
19899 regRow = sqlite3GetTempReg(pParse);
19900 nSortData = 1;
19901 }
19902 nKey = pOrderBy->nExpr - pSort->nOBSat;
19903 if( pSort->sortFlags & SORTFLAG_UseSorter ){
19904 int regSortOut = ++pParse->nMem;
19905 iSortTab = pParse->nTab++;
19906 if( pSort->labelBkOut ){
19907 addrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
19908 }
19909 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData);
19910 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
19911 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
19912 VdbeCoverage(v);
19913 codeOffset(v, p->iOffset, addrContinue);
19914 sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
19915 bSeq = 0;
19916 }else{
19917 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
19918 codeOffset(v, p->iOffset, addrContinue);
19919 iSortTab = iTab;
19920 bSeq = 1;
19921 }
19922 for(i=0; i<nSortData; i++){
19923 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i);
19924 VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
19925 }
19926 switch( eDest ){
19927 case SRT_EphemTab: {
19928 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
19929 sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
19930 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
19931 break;
19932 }
19933 #ifndef SQLITE_OMIT_SUBQUERY
19934 case SRT_Set: {
19935 assert( nColumn==1 );
19936 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
19937 &pDest->affSdst, 1);
19938 sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
19939 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
19940 break;
19941 }
19942 case SRT_Mem: {
19943 assert( nColumn==1 );
19944 sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
19945 /* The LIMIT clause will terminate the loop for us */
19946 break;
19947 }
19948 #endif
19949 default: {
19950 assert( eDest==SRT_Output || eDest==SRT_Coroutine );
19951 testcase( eDest==SRT_Output );
19952 testcase( eDest==SRT_Coroutine );
19953 if( eDest==SRT_Output ){
19954 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
19955 sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
19956 }else{
19957 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
19958 }
19959 break;
19960 }
19961 }
19962 if( regRowid ){
19963 sqlite3ReleaseTempReg(pParse, regRow);
19964 sqlite3ReleaseTempReg(pParse, regRowid);
19965 }
19966 /* The bottom of the loop
19967 */
19968 sqlite3VdbeResolveLabel(v, addrContinue);
19969 if( pSort->sortFlags & SORTFLAG_UseSorter ){
19970 sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
19971 }else{
19972 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
19973 }
19974 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
19975 sqlite3VdbeResolveLabel(v, addrBreak);
19976 }
19977
19978 /*
19979 ** Return a pointer to a string containing the 'declaration type' of the
19980 ** expression pExpr. The string may be treated as static by the caller.
19981 **
19982 ** Also try to estimate the size of the returned value and return that
19983 ** result in *pEstWidth.
19984 **
19985 ** The declaration type is the exact datatype definition extracted from the
19986 ** original CREATE TABLE statement if the expression is a column. The
19987 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
19988 ** is considered a column can be complex in the presence of subqueries. The
19989 ** result-set expression in all of the following SELECT statements is
19990 ** considered a column by this function.
19991 **
19992 ** SELECT col FROM tbl;
19993 ** SELECT (SELECT col FROM tbl;
19994 ** SELECT (SELECT col FROM tbl);
19995 ** SELECT abc FROM (SELECT col AS abc FROM tbl);
19996 **
19997 ** The declaration type for any expression other than a column is NULL.
19998 **
19999 ** This routine has either 3 or 6 parameters depending on whether or not
20000 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
20001 */
20002 #ifdef SQLITE_ENABLE_COLUMN_METADATA
20003 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
20004 #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
20005 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
20006 #endif
20007 static const char *columnTypeImpl(
20008 NameContext *pNC,
20009 Expr *pExpr,
20010 #ifdef SQLITE_ENABLE_COLUMN_METADATA
20011 const char **pzOrigDb,
20012 const char **pzOrigTab,
20013 const char **pzOrigCol,
20014 #endif
20015 u8 *pEstWidth
20016 ){
20017 char const *zType = 0;
20018 int j;
20019 u8 estWidth = 1;
20020 #ifdef SQLITE_ENABLE_COLUMN_METADATA
20021 char const *zOrigDb = 0;
20022 char const *zOrigTab = 0;
20023 char const *zOrigCol = 0;
20024 #endif
20025
20026 assert( pExpr!=0 );
20027 assert( pNC->pSrcList!=0 );
20028 switch( pExpr->op ){
20029 case TK_AGG_COLUMN:
20030 case TK_COLUMN: {
20031 /* The expression is a column. Locate the table the column is being
20032 ** extracted from in NameContext.pSrcList. This table may be real
20033 ** database table or a subquery.
20034 */
20035 Table *pTab = 0; /* Table structure column is extracted from */
20036 Select *pS = 0; /* Select the column is extracted from */
20037 int iCol = pExpr->iColumn; /* Index of column in pTab */
20038 testcase( pExpr->op==TK_AGG_COLUMN );
20039 testcase( pExpr->op==TK_COLUMN );
20040 while( pNC && !pTab ){
20041 SrcList *pTabList = pNC->pSrcList;
20042 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
20043 if( j<pTabList->nSrc ){
20044 pTab = pTabList->a[j].pTab;
20045 pS = pTabList->a[j].pSelect;
20046 }else{
20047 pNC = pNC->pNext;
20048 }
20049 }
20050
20051 if( pTab==0 ){
20052 /* At one time, code such as "SELECT new.x" within a trigger would
20053 ** cause this condition to run. Since then, we have restructured how
20054 ** trigger code is generated and so this condition is no longer
20055 ** possible. However, it can still be true for statements like
20056 ** the following:
20057 **
20058 ** CREATE TABLE t1(col INTEGER);
20059 ** SELECT (SELECT t1.col) FROM FROM t1;
20060 **
20061 ** when columnType() is called on the expression "t1.col" in the
20062 ** sub-select. In this case, set the column type to NULL, even
20063 ** though it should really be "INTEGER".
20064 **
20065 ** This is not a problem, as the column type of "t1.col" is never
20066 ** used. When columnType() is called on the expression
20067 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
20068 ** branch below. */
20069 break;
20070 }
20071
20072 assert( pTab && pExpr->pTab==pTab );
20073 if( pS ){
20074 /* The "table" is actually a sub-select or a view in the FROM clause
20075 ** of the SELECT statement. Return the declaration type and origin
20076 ** data for the result-set column of the sub-select.
20077 */
20078 if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
20079 /* If iCol is less than zero, then the expression requests the
20080 ** rowid of the sub-select or view. This expression is legal (see
20081 ** test case misc2.2.2) - it always evaluates to NULL.
20082 **
20083 ** The ALWAYS() is because iCol>=pS->pEList->nExpr will have been
20084 ** caught already by name resolution.
20085 */
20086 NameContext sNC;
20087 Expr *p = pS->pEList->a[iCol].pExpr;
20088 sNC.pSrcList = pS->pSrc;
20089 sNC.pNext = pNC;
20090 sNC.pParse = pNC->pParse;
20091 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth);
20092 }
20093 }else if( pTab->pSchema ){
20094 /* A real table */
20095 assert( !pS );
20096 if( iCol<0 ) iCol = pTab->iPKey;
20097 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
20098 #ifdef SQLITE_ENABLE_COLUMN_METADATA
20099 if( iCol<0 ){
20100 zType = "INTEGER";
20101 zOrigCol = "rowid";
20102 }else{
20103 zType = pTab->aCol[iCol].zType;
20104 zOrigCol = pTab->aCol[iCol].zName;
20105 estWidth = pTab->aCol[iCol].szEst;
20106 }
20107 zOrigTab = pTab->zName;
20108 if( pNC->pParse ){
20109 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
20110 zOrigDb = pNC->pParse->db->aDb[iDb].zName;
20111 }
20112 #else
20113 if( iCol<0 ){
20114 zType = "INTEGER";
20115 }else{
20116 zType = pTab->aCol[iCol].zType;
20117 estWidth = pTab->aCol[iCol].szEst;
20118 }
20119 #endif
20120 }
20121 break;
20122 }
20123 #ifndef SQLITE_OMIT_SUBQUERY
20124 case TK_SELECT: {
20125 /* The expression is a sub-select. Return the declaration type and
20126 ** origin info for the single column in the result set of the SELECT
20127 ** statement.
20128 */
20129 NameContext sNC;
20130 Select *pS = pExpr->x.pSelect;
20131 Expr *p = pS->pEList->a[0].pExpr;
20132 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
20133 sNC.pSrcList = pS->pSrc;
20134 sNC.pNext = pNC;
20135 sNC.pParse = pNC->pParse;
20136 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth);
20137 break;
20138 }
20139 #endif
20140 }
20141
20142 #ifdef SQLITE_ENABLE_COLUMN_METADATA
20143 if( pzOrigDb ){
20144 assert( pzOrigTab && pzOrigCol );
20145 *pzOrigDb = zOrigDb;
20146 *pzOrigTab = zOrigTab;
20147 *pzOrigCol = zOrigCol;
20148 }
20149 #endif
20150 if( pEstWidth ) *pEstWidth = estWidth;
20151 return zType;
20152 }
20153
20154 /*
20155 ** Generate code that will tell the VDBE the declaration types of columns
20156 ** in the result set.
20157 */
20158 static void generateColumnTypes(
20159 Parse *pParse, /* Parser context */
20160 SrcList *pTabList, /* List of tables */
20161 ExprList *pEList /* Expressions defining the result set */
20162 ){
20163 #ifndef SQLITE_OMIT_DECLTYPE
20164 Vdbe *v = pParse->pVdbe;
20165 int i;
20166 NameContext sNC;
20167 sNC.pSrcList = pTabList;
20168 sNC.pParse = pParse;
20169 for(i=0; i<pEList->nExpr; i++){
20170 Expr *p = pEList->a[i].pExpr;
20171 const char *zType;
20172 #ifdef SQLITE_ENABLE_COLUMN_METADATA
20173 const char *zOrigDb = 0;
20174 const char *zOrigTab = 0;
20175 const char *zOrigCol = 0;
20176 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
20177
20178 /* The vdbe must make its own copy of the column-type and other
20179 ** column specific strings, in case the schema is reset before this
20180 ** virtual machine is deleted.
20181 */
20182 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
20183 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
20184 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
20185 #else
20186 zType = columnType(&sNC, p, 0, 0, 0, 0);
20187 #endif
20188 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
20189 }
20190 #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
20191 }
20192
20193 /*
20194 ** Generate code that will tell the VDBE the names of columns
20195 ** in the result set. This information is used to provide the
20196 ** azCol[] values in the callback.
20197 */
20198 static void generateColumnNames(
20199 Parse *pParse, /* Parser context */
20200 SrcList *pTabList, /* List of tables */
20201 ExprList *pEList /* Expressions defining the result set */
20202 ){
20203 Vdbe *v = pParse->pVdbe;
20204 int i, j;
20205 sqlite3 *db = pParse->db;
20206 int fullNames, shortNames;
20207
20208 #ifndef SQLITE_OMIT_EXPLAIN
20209 /* If this is an EXPLAIN, skip this step */
20210 if( pParse->explain ){
20211 return;
20212 }
20213 #endif
20214
20215 if( pParse->colNamesSet || db->mallocFailed ) return;
20216 assert( v!=0 );
20217 assert( pTabList!=0 );
20218 pParse->colNamesSet = 1;
20219 fullNames = (db->flags & SQLITE_FullColNames)!=0;
20220 shortNames = (db->flags & SQLITE_ShortColNames)!=0;
20221 sqlite3VdbeSetNumCols(v, pEList->nExpr);
20222 for(i=0; i<pEList->nExpr; i++){
20223 Expr *p;
20224 p = pEList->a[i].pExpr;
20225 if( NEVER(p==0) ) continue;
20226 if( pEList->a[i].zName ){
20227 char *zName = pEList->a[i].zName;
20228 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
20229 }else if( p->op==TK_COLUMN || p->op==TK_AGG_COLUMN ){
20230 Table *pTab;
20231 char *zCol;
20232 int iCol = p->iColumn;
20233 for(j=0; ALWAYS(j<pTabList->nSrc); j++){
20234 if( pTabList->a[j].iCursor==p->iTable ) break;
20235 }
20236 assert( j<pTabList->nSrc );
20237 pTab = pTabList->a[j].pTab;
20238 if( iCol<0 ) iCol = pTab->iPKey;
20239 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
20240 if( iCol<0 ){
20241 zCol = "rowid";
20242 }else{
20243 zCol = pTab->aCol[iCol].zName;
20244 }
20245 if( !shortNames && !fullNames ){
20246 sqlite3VdbeSetColName(v, i, COLNAME_NAME,
20247 sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
20248 }else if( fullNames ){
20249 char *zName = 0;
20250 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
20251 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
20252 }else{
20253 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
20254 }
20255 }else{
20256 const char *z = pEList->a[i].zSpan;
20257 z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
20258 sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
20259 }
20260 }
20261 generateColumnTypes(pParse, pTabList, pEList);
20262 }
20263
20264 /*
20265 ** Given an expression list (which is really the list of expressions
20266 ** that form the result set of a SELECT statement) compute appropriate
20267 ** column names for a table that would hold the expression list.
20268 **
20269 ** All column names will be unique.
20270 **
20271 ** Only the column names are computed. Column.zType, Column.zColl,
20272 ** and other fields of Column are zeroed.
20273 **
20274 ** Return SQLITE_OK on success. If a memory allocation error occurs,
20275 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
20276 */
20277 SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
20278 Parse *pParse, /* Parsing context */
20279 ExprList *pEList, /* Expr list from which to derive column names */
20280 i16 *pnCol, /* Write the number of columns here */
20281 Column **paCol /* Write the new column list here */
20282 ){
20283 sqlite3 *db = pParse->db; /* Database connection */
20284 int i, j; /* Loop counters */
20285 u32 cnt; /* Index added to make the name unique */
20286 Column *aCol, *pCol; /* For looping over result columns */
20287 int nCol; /* Number of columns in the result set */
20288 Expr *p; /* Expression for a single result column */
20289 char *zName; /* Column name */
20290 int nName; /* Size of name in zName[] */
20291 Hash ht; /* Hash table of column names */
20292
20293 sqlite3HashInit(&ht);
20294 if( pEList ){
20295 nCol = pEList->nExpr;
20296 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
20297 testcase( aCol==0 );
20298 }else{
20299 nCol = 0;
20300 aCol = 0;
20301 }
20302 assert( nCol==(i16)nCol );
20303 *pnCol = nCol;
20304 *paCol = aCol;
20305
20306 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
20307 /* Get an appropriate name for the column
20308 */
20309 p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
20310 if( (zName = pEList->a[i].zName)!=0 ){
20311 /* If the column contains an "AS <name>" phrase, use <name> as the name */
20312 }else{
20313 Expr *pColExpr = p; /* The expression that is the result column name */
20314 Table *pTab; /* Table associated with this expression */
20315 while( pColExpr->op==TK_DOT ){
20316 pColExpr = pColExpr->pRight;
20317 assert( pColExpr!=0 );
20318 }
20319 if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
20320 /* For columns use the column name name */
20321 int iCol = pColExpr->iColumn;
20322 pTab = pColExpr->pTab;
20323 if( iCol<0 ) iCol = pTab->iPKey;
20324 zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
20325 }else if( pColExpr->op==TK_ID ){
20326 assert( !ExprHasProperty(pColExpr, EP_IntValue) );
20327 zName = pColExpr->u.zToken;
20328 }else{
20329 /* Use the original text of the column expression as its name */
20330 zName = pEList->a[i].zSpan;
20331 }
20332 }
20333 zName = sqlite3MPrintf(db, "%s", zName);
20334
20335 /* Make sure the column name is unique. If the name is not unique,
20336 ** append an integer to the name so that it becomes unique.
20337 */
20338 cnt = 0;
20339 while( zName && sqlite3HashFind(&ht, zName)!=0 ){
20340 nName = sqlite3Strlen30(zName);
20341 if( nName>0 ){
20342 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
20343 if( zName[j]==':' ) nName = j;
20344 }
20345 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
20346 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
20347 }
20348 pCol->zName = zName;
20349 sqlite3ColumnPropertiesFromName(0, pCol);
20350 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
20351 db->mallocFailed = 1;
20352 }
20353 }
20354 sqlite3HashClear(&ht);
20355 if( db->mallocFailed ){
20356 for(j=0; j<i; j++){
20357 sqlite3DbFree(db, aCol[j].zName);
20358 }
20359 sqlite3DbFree(db, aCol);
20360 *paCol = 0;
20361 *pnCol = 0;
20362 return SQLITE_NOMEM;
20363 }
20364 return SQLITE_OK;
20365 }
20366
20367 /*
20368 ** Add type and collation information to a column list based on
20369 ** a SELECT statement.
20370 **
20371 ** The column list presumably came from selectColumnNamesFromExprList().
20372 ** The column list has only names, not types or collations. This
20373 ** routine goes through and adds the types and collations.
20374 **
20375 ** This routine requires that all identifiers in the SELECT
20376 ** statement be resolved.
20377 */
20378 static void selectAddColumnTypeAndCollation(
20379 Parse *pParse, /* Parsing contexts */
20380 Table *pTab, /* Add column type information to this table */
20381 Select *pSelect /* SELECT used to determine types and collations */
20382 ){
20383 sqlite3 *db = pParse->db;
20384 NameContext sNC;
20385 Column *pCol;
20386 CollSeq *pColl;
20387 int i;
20388 Expr *p;
20389 struct ExprList_item *a;
20390 u64 szAll = 0;
20391
20392 assert( pSelect!=0 );
20393 assert( (pSelect->selFlags & SF_Resolved)!=0 );
20394 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
20395 if( db->mallocFailed ) return;
20396 memset(&sNC, 0, sizeof(sNC));
20397 sNC.pSrcList = pSelect->pSrc;
20398 a = pSelect->pEList->a;
20399 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
20400 p = a[i].pExpr;
20401 if( pCol->zType==0 ){
20402 pCol->zType = sqlite3DbStrDup(db,
20403 columnType(&sNC, p,0,0,0, &pCol->szEst));
20404 }
20405 szAll += pCol->szEst;
20406 pCol->affinity = sqlite3ExprAffinity(p);
20407 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
20408 pColl = sqlite3ExprCollSeq(pParse, p);
20409 if( pColl && pCol->zColl==0 ){
20410 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
20411 }
20412 }
20413 pTab->szTabRow = sqlite3LogEst(szAll*4);
20414 }
20415
20416 /*
20417 ** Given a SELECT statement, generate a Table structure that describes
20418 ** the result set of that SELECT.
20419 */
20420 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
20421 Table *pTab;
20422 sqlite3 *db = pParse->db;
20423 int savedFlags;
20424
20425 savedFlags = db->flags;
20426 db->flags &= ~SQLITE_FullColNames;
20427 db->flags |= SQLITE_ShortColNames;
20428 sqlite3SelectPrep(pParse, pSelect, 0);
20429 if( pParse->nErr ) return 0;
20430 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
20431 db->flags = savedFlags;
20432 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
20433 if( pTab==0 ){
20434 return 0;
20435 }
20436 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
20437 ** is disabled */
20438 assert( db->lookaside.bEnabled==0 );
20439 pTab->nRef = 1;
20440 pTab->zName = 0;
20441 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
20442 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
20443 selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
20444 pTab->iPKey = -1;
20445 if( db->mallocFailed ){
20446 sqlite3DeleteTable(db, pTab);
20447 return 0;
20448 }
20449 return pTab;
20450 }
20451
20452 /*
20453 ** Get a VDBE for the given parser context. Create a new one if necessary.
20454 ** If an error occurs, return NULL and leave a message in pParse.
20455 */
20456 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
20457 Vdbe *v = pParse->pVdbe;
20458 if( v==0 ){
20459 v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
20460 if( v ) sqlite3VdbeAddOp0(v, OP_Init);
20461 if( pParse->pToplevel==0
20462 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
20463 ){
20464 pParse->okConstFactor = 1;
20465 }
20466
20467 }
20468 return v;
20469 }
20470
20471
20472 /*
20473 ** Compute the iLimit and iOffset fields of the SELECT based on the
20474 ** pLimit and pOffset expressions. pLimit and pOffset hold the expressions
20475 ** that appear in the original SQL statement after the LIMIT and OFFSET
20476 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
20477 ** are the integer memory register numbers for counters used to compute
20478 ** the limit and offset. If there is no limit and/or offset, then
20479 ** iLimit and iOffset are negative.
20480 **
20481 ** This routine changes the values of iLimit and iOffset only if
20482 ** a limit or offset is defined by pLimit and pOffset. iLimit and
20483 ** iOffset should have been preset to appropriate default values (zero)
20484 ** prior to calling this routine.
20485 **
20486 ** The iOffset register (if it exists) is initialized to the value
20487 ** of the OFFSET. The iLimit register is initialized to LIMIT. Register
20488 ** iOffset+1 is initialized to LIMIT+OFFSET.
20489 **
20490 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
20491 ** redefined. The UNION ALL operator uses this property to force
20492 ** the reuse of the same limit and offset registers across multiple
20493 ** SELECT statements.
20494 */
20495 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
20496 Vdbe *v = 0;
20497 int iLimit = 0;
20498 int iOffset;
20499 int n;
20500 if( p->iLimit ) return;
20501
20502 /*
20503 ** "LIMIT -1" always shows all rows. There is some
20504 ** controversy about what the correct behavior should be.
20505 ** The current implementation interprets "LIMIT 0" to mean
20506 ** no rows.
20507 */
20508 sqlite3ExprCacheClear(pParse);
20509 assert( p->pOffset==0 || p->pLimit!=0 );
20510 if( p->pLimit ){
20511 p->iLimit = iLimit = ++pParse->nMem;
20512 v = sqlite3GetVdbe(pParse);
20513 assert( v!=0 );
20514 if( sqlite3ExprIsInteger(p->pLimit, &n) ){
20515 sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
20516 VdbeComment((v, "LIMIT counter"));
20517 if( n==0 ){
20518 sqlite3VdbeGoto(v, iBreak);
20519 }else if( n>=0 && p->nSelectRow>(u64)n ){
20520 p->nSelectRow = n;
20521 }
20522 }else{
20523 sqlite3ExprCode(pParse, p->pLimit, iLimit);
20524 sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
20525 VdbeComment((v, "LIMIT counter"));
20526 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
20527 }
20528 if( p->pOffset ){
20529 p->iOffset = iOffset = ++pParse->nMem;
20530 pParse->nMem++; /* Allocate an extra register for limit+offset */
20531 sqlite3ExprCode(pParse, p->pOffset, iOffset);
20532 sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
20533 VdbeComment((v, "OFFSET counter"));
20534 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, iOffset, iOffset, 0);
20535 sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
20536 VdbeComment((v, "LIMIT+OFFSET"));
20537 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, iLimit, iOffset+1, -1);
20538 }
20539 }
20540 }
20541
20542 #ifndef SQLITE_OMIT_COMPOUND_SELECT
20543 /*
20544 ** Return the appropriate collating sequence for the iCol-th column of
20545 ** the result set for the compound-select statement "p". Return NULL if
20546 ** the column has no default collating sequence.
20547 **
20548 ** The collating sequence for the compound select is taken from the
20549 ** left-most term of the select that has a collating sequence.
20550 */
20551 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
20552 CollSeq *pRet;
20553 if( p->pPrior ){
20554 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
20555 }else{
20556 pRet = 0;
20557 }
20558 assert( iCol>=0 );
20559 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
20560 ** have been thrown during name resolution and we would not have gotten
20561 ** this far */
20562 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
20563 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
20564 }
20565 return pRet;
20566 }
20567
20568 /*
20569 ** The select statement passed as the second parameter is a compound SELECT
20570 ** with an ORDER BY clause. This function allocates and returns a KeyInfo
20571 ** structure suitable for implementing the ORDER BY.
20572 **
20573 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
20574 ** function is responsible for ensuring that this structure is eventually
20575 ** freed.
20576 */
20577 static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
20578 ExprList *pOrderBy = p->pOrderBy;
20579 int nOrderBy = p->pOrderBy->nExpr;
20580 sqlite3 *db = pParse->db;
20581 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
20582 if( pRet ){
20583 int i;
20584 for(i=0; i<nOrderBy; i++){
20585 struct ExprList_item *pItem = &pOrderBy->a[i];
20586 Expr *pTerm = pItem->pExpr;
20587 CollSeq *pColl;
20588
20589 if( pTerm->flags & EP_Collate ){
20590 pColl = sqlite3ExprCollSeq(pParse, pTerm);
20591 }else{
20592 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
20593 if( pColl==0 ) pColl = db->pDfltColl;
20594 pOrderBy->a[i].pExpr =
20595 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
20596 }
20597 assert( sqlite3KeyInfoIsWriteable(pRet) );
20598 pRet->aColl[i] = pColl;
20599 pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
20600 }
20601 }
20602
20603 return pRet;
20604 }
20605
20606 #ifndef SQLITE_OMIT_CTE
20607 /*
20608 ** This routine generates VDBE code to compute the content of a WITH RECURSIVE
20609 ** query of the form:
20610 **
20611 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
20612 ** \___________/ \_______________/
20613 ** p->pPrior p
20614 **
20615 **
20616 ** There is exactly one reference to the recursive-table in the FROM clause
20617 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
20618 **
20619 ** The setup-query runs once to generate an initial set of rows that go
20620 ** into a Queue table. Rows are extracted from the Queue table one by
20621 ** one. Each row extracted from Queue is output to pDest. Then the single
20622 ** extracted row (now in the iCurrent table) becomes the content of the
20623 ** recursive-table for a recursive-query run. The output of the recursive-query
20624 ** is added back into the Queue table. Then another row is extracted from Queue
20625 ** and the iteration continues until the Queue table is empty.
20626 **
20627 ** If the compound query operator is UNION then no duplicate rows are ever
20628 ** inserted into the Queue table. The iDistinct table keeps a copy of all rows
20629 ** that have ever been inserted into Queue and causes duplicates to be
20630 ** discarded. If the operator is UNION ALL, then duplicates are allowed.
20631 **
20632 ** If the query has an ORDER BY, then entries in the Queue table are kept in
20633 ** ORDER BY order and the first entry is extracted for each cycle. Without
20634 ** an ORDER BY, the Queue table is just a FIFO.
20635 **
20636 ** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
20637 ** have been output to pDest. A LIMIT of zero means to output no rows and a
20638 ** negative LIMIT means to output all rows. If there is also an OFFSET clause
20639 ** with a positive value, then the first OFFSET outputs are discarded rather
20640 ** than being sent to pDest. The LIMIT count does not begin until after OFFSET
20641 ** rows have been skipped.
20642 */
20643 static void generateWithRecursiveQuery(
20644 Parse *pParse, /* Parsing context */
20645 Select *p, /* The recursive SELECT to be coded */
20646 SelectDest *pDest /* What to do with query results */
20647 ){
20648 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
20649 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
20650 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
20651 Select *pSetup = p->pPrior; /* The setup query */
20652 int addrTop; /* Top of the loop */
20653 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
20654 int iCurrent = 0; /* The Current table */
20655 int regCurrent; /* Register holding Current table */
20656 int iQueue; /* The Queue table */
20657 int iDistinct = 0; /* To ensure unique results if UNION */
20658 int eDest = SRT_Fifo; /* How to write to Queue */
20659 SelectDest destQueue; /* SelectDest targetting the Queue table */
20660 int i; /* Loop counter */
20661 int rc; /* Result code */
20662 ExprList *pOrderBy; /* The ORDER BY clause */
20663 Expr *pLimit, *pOffset; /* Saved LIMIT and OFFSET */
20664 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
20665
20666 /* Obtain authorization to do a recursive query */
20667 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
20668
20669 /* Process the LIMIT and OFFSET clauses, if they exist */
20670 addrBreak = sqlite3VdbeMakeLabel(v);
20671 computeLimitRegisters(pParse, p, addrBreak);
20672 pLimit = p->pLimit;
20673 pOffset = p->pOffset;
20674 regLimit = p->iLimit;
20675 regOffset = p->iOffset;
20676 p->pLimit = p->pOffset = 0;
20677 p->iLimit = p->iOffset = 0;
20678 pOrderBy = p->pOrderBy;
20679
20680 /* Locate the cursor number of the Current table */
20681 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
20682 if( pSrc->a[i].fg.isRecursive ){
20683 iCurrent = pSrc->a[i].iCursor;
20684 break;
20685 }
20686 }
20687
20688 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
20689 ** the Distinct table must be exactly one greater than Queue in order
20690 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
20691 iQueue = pParse->nTab++;
20692 if( p->op==TK_UNION ){
20693 eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
20694 iDistinct = pParse->nTab++;
20695 }else{
20696 eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
20697 }
20698 sqlite3SelectDestInit(&destQueue, eDest, iQueue);
20699
20700 /* Allocate cursors for Current, Queue, and Distinct. */
20701 regCurrent = ++pParse->nMem;
20702 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
20703 if( pOrderBy ){
20704 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
20705 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
20706 (char*)pKeyInfo, P4_KEYINFO);
20707 destQueue.pOrderBy = pOrderBy;
20708 }else{
20709 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
20710 }
20711 VdbeComment((v, "Queue table"));
20712 if( iDistinct ){
20713 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
20714 p->selFlags |= SF_UsesEphemeral;
20715 }
20716
20717 /* Detach the ORDER BY clause from the compound SELECT */
20718 p->pOrderBy = 0;
20719
20720 /* Store the results of the setup-query in Queue. */
20721 pSetup->pNext = 0;
20722 rc = sqlite3Select(pParse, pSetup, &destQueue);
20723 pSetup->pNext = p;
20724 if( rc ) goto end_of_recursive_query;
20725
20726 /* Find the next row in the Queue and output that row */
20727 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
20728
20729 /* Transfer the next row in Queue over to Current */
20730 sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
20731 if( pOrderBy ){
20732 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
20733 }else{
20734 sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
20735 }
20736 sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
20737
20738 /* Output the single row in Current */
20739 addrCont = sqlite3VdbeMakeLabel(v);
20740 codeOffset(v, regOffset, addrCont);
20741 selectInnerLoop(pParse, p, p->pEList, iCurrent,
20742 0, 0, pDest, addrCont, addrBreak);
20743 if( regLimit ){
20744 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
20745 VdbeCoverage(v);
20746 }
20747 sqlite3VdbeResolveLabel(v, addrCont);
20748
20749 /* Execute the recursive SELECT taking the single row in Current as
20750 ** the value for the recursive-table. Store the results in the Queue.
20751 */
20752 if( p->selFlags & SF_Aggregate ){
20753 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
20754 }else{
20755 p->pPrior = 0;
20756 sqlite3Select(pParse, p, &destQueue);
20757 assert( p->pPrior==0 );
20758 p->pPrior = pSetup;
20759 }
20760
20761 /* Keep running the loop until the Queue is empty */
20762 sqlite3VdbeGoto(v, addrTop);
20763 sqlite3VdbeResolveLabel(v, addrBreak);
20764
20765 end_of_recursive_query:
20766 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
20767 p->pOrderBy = pOrderBy;
20768 p->pLimit = pLimit;
20769 p->pOffset = pOffset;
20770 return;
20771 }
20772 #endif /* SQLITE_OMIT_CTE */
20773
20774 /* Forward references */
20775 static int multiSelectOrderBy(
20776 Parse *pParse, /* Parsing context */
20777 Select *p, /* The right-most of SELECTs to be coded */
20778 SelectDest *pDest /* What to do with query results */
20779 );
20780
20781 /*
20782 ** Handle the special case of a compound-select that originates from a
20783 ** VALUES clause. By handling this as a special case, we avoid deep
20784 ** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
20785 ** on a VALUES clause.
20786 **
20787 ** Because the Select object originates from a VALUES clause:
20788 ** (1) It has no LIMIT or OFFSET
20789 ** (2) All terms are UNION ALL
20790 ** (3) There is no ORDER BY clause
20791 */
20792 static int multiSelectValues(
20793 Parse *pParse, /* Parsing context */
20794 Select *p, /* The right-most of SELECTs to be coded */
20795 SelectDest *pDest /* What to do with query results */
20796 ){
20797 Select *pPrior;
20798 int nRow = 1;
20799 int rc = 0;
20800 assert( p->selFlags & SF_MultiValue );
20801 do{
20802 assert( p->selFlags & SF_Values );
20803 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
20804 assert( p->pLimit==0 );
20805 assert( p->pOffset==0 );
20806 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
20807 if( p->pPrior==0 ) break;
20808 assert( p->pPrior->pNext==p );
20809 p = p->pPrior;
20810 nRow++;
20811 }while(1);
20812 while( p ){
20813 pPrior = p->pPrior;
20814 p->pPrior = 0;
20815 rc = sqlite3Select(pParse, p, pDest);
20816 p->pPrior = pPrior;
20817 if( rc ) break;
20818 p->nSelectRow = nRow;
20819 p = p->pNext;
20820 }
20821 return rc;
20822 }
20823
20824 /*
20825 ** This routine is called to process a compound query form from
20826 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
20827 ** INTERSECT
20828 **
20829 ** "p" points to the right-most of the two queries. the query on the
20830 ** left is p->pPrior. The left query could also be a compound query
20831 ** in which case this routine will be called recursively.
20832 **
20833 ** The results of the total query are to be written into a destination
20834 ** of type eDest with parameter iParm.
20835 **
20836 ** Example 1: Consider a three-way compound SQL statement.
20837 **
20838 ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
20839 **
20840 ** This statement is parsed up as follows:
20841 **
20842 ** SELECT c FROM t3
20843 ** |
20844 ** `-----> SELECT b FROM t2
20845 ** |
20846 ** `------> SELECT a FROM t1
20847 **
20848 ** The arrows in the diagram above represent the Select.pPrior pointer.
20849 ** So if this routine is called with p equal to the t3 query, then
20850 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
20851 **
20852 ** Notice that because of the way SQLite parses compound SELECTs, the
20853 ** individual selects always group from left to right.
20854 */
20855 static int multiSelect(
20856 Parse *pParse, /* Parsing context */
20857 Select *p, /* The right-most of SELECTs to be coded */
20858 SelectDest *pDest /* What to do with query results */
20859 ){
20860 int rc = SQLITE_OK; /* Success code from a subroutine */
20861 Select *pPrior; /* Another SELECT immediately to our left */
20862 Vdbe *v; /* Generate code to this VDBE */
20863 SelectDest dest; /* Alternative data destination */
20864 Select *pDelete = 0; /* Chain of simple selects to delete */
20865 sqlite3 *db; /* Database connection */
20866 #ifndef SQLITE_OMIT_EXPLAIN
20867 int iSub1 = 0; /* EQP id of left-hand query */
20868 int iSub2 = 0; /* EQP id of right-hand query */
20869 #endif
20870
20871 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
20872 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
20873 */
20874 assert( p && p->pPrior ); /* Calling function guarantees this much */
20875 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
20876 db = pParse->db;
20877 pPrior = p->pPrior;
20878 dest = *pDest;
20879 if( pPrior->pOrderBy ){
20880 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
20881 selectOpName(p->op));
20882 rc = 1;
20883 goto multi_select_end;
20884 }
20885 if( pPrior->pLimit ){
20886 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
20887 selectOpName(p->op));
20888 rc = 1;
20889 goto multi_select_end;
20890 }
20891
20892 v = sqlite3GetVdbe(pParse);
20893 assert( v!=0 ); /* The VDBE already created by calling function */
20894
20895 /* Create the destination temporary table if necessary
20896 */
20897 if( dest.eDest==SRT_EphemTab ){
20898 assert( p->pEList );
20899 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
20900 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
20901 dest.eDest = SRT_Table;
20902 }
20903
20904 /* Special handling for a compound-select that originates as a VALUES clause.
20905 */
20906 if( p->selFlags & SF_MultiValue ){
20907 rc = multiSelectValues(pParse, p, &dest);
20908 goto multi_select_end;
20909 }
20910
20911 /* Make sure all SELECTs in the statement have the same number of elements
20912 ** in their result sets.
20913 */
20914 assert( p->pEList && pPrior->pEList );
20915 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
20916
20917 #ifndef SQLITE_OMIT_CTE
20918 if( p->selFlags & SF_Recursive ){
20919 generateWithRecursiveQuery(pParse, p, &dest);
20920 }else
20921 #endif
20922
20923 /* Compound SELECTs that have an ORDER BY clause are handled separately.
20924 */
20925 if( p->pOrderBy ){
20926 return multiSelectOrderBy(pParse, p, pDest);
20927 }else
20928
20929 /* Generate code for the left and right SELECT statements.
20930 */
20931 switch( p->op ){
20932 case TK_ALL: {
20933 int addr = 0;
20934 int nLimit;
20935 assert( !pPrior->pLimit );
20936 pPrior->iLimit = p->iLimit;
20937 pPrior->iOffset = p->iOffset;
20938 pPrior->pLimit = p->pLimit;
20939 pPrior->pOffset = p->pOffset;
20940 explainSetInteger(iSub1, pParse->iNextSelectId);
20941 rc = sqlite3Select(pParse, pPrior, &dest);
20942 p->pLimit = 0;
20943 p->pOffset = 0;
20944 if( rc ){
20945 goto multi_select_end;
20946 }
20947 p->pPrior = 0;
20948 p->iLimit = pPrior->iLimit;
20949 p->iOffset = pPrior->iOffset;
20950 if( p->iLimit ){
20951 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
20952 VdbeComment((v, "Jump ahead if LIMIT reached"));
20953 if( p->iOffset ){
20954 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, p->iOffset, p->iOffset, 0);
20955 sqlite3VdbeAddOp3(v, OP_Add, p->iLimit, p->iOffset, p->iOffset+1);
20956 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, p->iLimit, p->iOffset+1, -1);
20957 }
20958 }
20959 explainSetInteger(iSub2, pParse->iNextSelectId);
20960 rc = sqlite3Select(pParse, p, &dest);
20961 testcase( rc!=SQLITE_OK );
20962 pDelete = p->pPrior;
20963 p->pPrior = pPrior;
20964 p->nSelectRow += pPrior->nSelectRow;
20965 if( pPrior->pLimit
20966 && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
20967 && nLimit>0 && p->nSelectRow > (u64)nLimit
20968 ){
20969 p->nSelectRow = nLimit;
20970 }
20971 if( addr ){
20972 sqlite3VdbeJumpHere(v, addr);
20973 }
20974 break;
20975 }
20976 case TK_EXCEPT:
20977 case TK_UNION: {
20978 int unionTab; /* Cursor number of the temporary table holding result */
20979 u8 op = 0; /* One of the SRT_ operations to apply to self */
20980 int priorOp; /* The SRT_ operation to apply to prior selects */
20981 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
20982 int addr;
20983 SelectDest uniondest;
20984
20985 testcase( p->op==TK_EXCEPT );
20986 testcase( p->op==TK_UNION );
20987 priorOp = SRT_Union;
20988 if( dest.eDest==priorOp ){
20989 /* We can reuse a temporary table generated by a SELECT to our
20990 ** right.
20991 */
20992 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
20993 assert( p->pOffset==0 ); /* Not allowed on leftward elements */
20994 unionTab = dest.iSDParm;
20995 }else{
20996 /* We will need to create our own temporary table to hold the
20997 ** intermediate results.
20998 */
20999 unionTab = pParse->nTab++;
21000 assert( p->pOrderBy==0 );
21001 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
21002 assert( p->addrOpenEphm[0] == -1 );
21003 p->addrOpenEphm[0] = addr;
21004 findRightmost(p)->selFlags |= SF_UsesEphemeral;
21005 assert( p->pEList );
21006 }
21007
21008 /* Code the SELECT statements to our left
21009 */
21010 assert( !pPrior->pOrderBy );
21011 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
21012 explainSetInteger(iSub1, pParse->iNextSelectId);
21013 rc = sqlite3Select(pParse, pPrior, &uniondest);
21014 if( rc ){
21015 goto multi_select_end;
21016 }
21017
21018 /* Code the current SELECT statement
21019 */
21020 if( p->op==TK_EXCEPT ){
21021 op = SRT_Except;
21022 }else{
21023 assert( p->op==TK_UNION );
21024 op = SRT_Union;
21025 }
21026 p->pPrior = 0;
21027 pLimit = p->pLimit;
21028 p->pLimit = 0;
21029 pOffset = p->pOffset;
21030 p->pOffset = 0;
21031 uniondest.eDest = op;
21032 explainSetInteger(iSub2, pParse->iNextSelectId);
21033 rc = sqlite3Select(pParse, p, &uniondest);
21034 testcase( rc!=SQLITE_OK );
21035 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
21036 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
21037 sqlite3ExprListDelete(db, p->pOrderBy);
21038 pDelete = p->pPrior;
21039 p->pPrior = pPrior;
21040 p->pOrderBy = 0;
21041 if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
21042 sqlite3ExprDelete(db, p->pLimit);
21043 p->pLimit = pLimit;
21044 p->pOffset = pOffset;
21045 p->iLimit = 0;
21046 p->iOffset = 0;
21047
21048 /* Convert the data in the temporary table into whatever form
21049 ** it is that we currently need.
21050 */
21051 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
21052 if( dest.eDest!=priorOp ){
21053 int iCont, iBreak, iStart;
21054 assert( p->pEList );
21055 if( dest.eDest==SRT_Output ){
21056 Select *pFirst = p;
21057 while( pFirst->pPrior ) pFirst = pFirst->pPrior;
21058 generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList);
21059 }
21060 iBreak = sqlite3VdbeMakeLabel(v);
21061 iCont = sqlite3VdbeMakeLabel(v);
21062 computeLimitRegisters(pParse, p, iBreak);
21063 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
21064 iStart = sqlite3VdbeCurrentAddr(v);
21065 selectInnerLoop(pParse, p, p->pEList, unionTab,
21066 0, 0, &dest, iCont, iBreak);
21067 sqlite3VdbeResolveLabel(v, iCont);
21068 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
21069 sqlite3VdbeResolveLabel(v, iBreak);
21070 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
21071 }
21072 break;
21073 }
21074 default: assert( p->op==TK_INTERSECT ); {
21075 int tab1, tab2;
21076 int iCont, iBreak, iStart;
21077 Expr *pLimit, *pOffset;
21078 int addr;
21079 SelectDest intersectdest;
21080 int r1;
21081
21082 /* INTERSECT is different from the others since it requires
21083 ** two temporary tables. Hence it has its own case. Begin
21084 ** by allocating the tables we will need.
21085 */
21086 tab1 = pParse->nTab++;
21087 tab2 = pParse->nTab++;
21088 assert( p->pOrderBy==0 );
21089
21090 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
21091 assert( p->addrOpenEphm[0] == -1 );
21092 p->addrOpenEphm[0] = addr;
21093 findRightmost(p)->selFlags |= SF_UsesEphemeral;
21094 assert( p->pEList );
21095
21096 /* Code the SELECTs to our left into temporary table "tab1".
21097 */
21098 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
21099 explainSetInteger(iSub1, pParse->iNextSelectId);
21100 rc = sqlite3Select(pParse, pPrior, &intersectdest);
21101 if( rc ){
21102 goto multi_select_end;
21103 }
21104
21105 /* Code the current SELECT into temporary table "tab2"
21106 */
21107 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
21108 assert( p->addrOpenEphm[1] == -1 );
21109 p->addrOpenEphm[1] = addr;
21110 p->pPrior = 0;
21111 pLimit = p->pLimit;
21112 p->pLimit = 0;
21113 pOffset = p->pOffset;
21114 p->pOffset = 0;
21115 intersectdest.iSDParm = tab2;
21116 explainSetInteger(iSub2, pParse->iNextSelectId);
21117 rc = sqlite3Select(pParse, p, &intersectdest);
21118 testcase( rc!=SQLITE_OK );
21119 pDelete = p->pPrior;
21120 p->pPrior = pPrior;
21121 if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
21122 sqlite3ExprDelete(db, p->pLimit);
21123 p->pLimit = pLimit;
21124 p->pOffset = pOffset;
21125
21126 /* Generate code to take the intersection of the two temporary
21127 ** tables.
21128 */
21129 assert( p->pEList );
21130 if( dest.eDest==SRT_Output ){
21131 Select *pFirst = p;
21132 while( pFirst->pPrior ) pFirst = pFirst->pPrior;
21133 generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList);
21134 }
21135 iBreak = sqlite3VdbeMakeLabel(v);
21136 iCont = sqlite3VdbeMakeLabel(v);
21137 computeLimitRegisters(pParse, p, iBreak);
21138 sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
21139 r1 = sqlite3GetTempReg(pParse);
21140 iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
21141 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
21142 sqlite3ReleaseTempReg(pParse, r1);
21143 selectInnerLoop(pParse, p, p->pEList, tab1,
21144 0, 0, &dest, iCont, iBreak);
21145 sqlite3VdbeResolveLabel(v, iCont);
21146 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
21147 sqlite3VdbeResolveLabel(v, iBreak);
21148 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
21149 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
21150 break;
21151 }
21152 }
21153
21154 explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
21155
21156 /* Compute collating sequences used by
21157 ** temporary tables needed to implement the compound select.
21158 ** Attach the KeyInfo structure to all temporary tables.
21159 **
21160 ** This section is run by the right-most SELECT statement only.
21161 ** SELECT statements to the left always skip this part. The right-most
21162 ** SELECT might also skip this part if it has no ORDER BY clause and
21163 ** no temp tables are required.
21164 */
21165 if( p->selFlags & SF_UsesEphemeral ){
21166 int i; /* Loop counter */
21167 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
21168 Select *pLoop; /* For looping through SELECT statements */
21169 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
21170 int nCol; /* Number of columns in result set */
21171
21172 assert( p->pNext==0 );
21173 nCol = p->pEList->nExpr;
21174 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
21175 if( !pKeyInfo ){
21176 rc = SQLITE_NOMEM;
21177 goto multi_select_end;
21178 }
21179 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
21180 *apColl = multiSelectCollSeq(pParse, p, i);
21181 if( 0==*apColl ){
21182 *apColl = db->pDfltColl;
21183 }
21184 }
21185
21186 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
21187 for(i=0; i<2; i++){
21188 int addr = pLoop->addrOpenEphm[i];
21189 if( addr<0 ){
21190 /* If [0] is unused then [1] is also unused. So we can
21191 ** always safely abort as soon as the first unused slot is found */
21192 assert( pLoop->addrOpenEphm[1]<0 );
21193 break;
21194 }
21195 sqlite3VdbeChangeP2(v, addr, nCol);
21196 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
21197 P4_KEYINFO);
21198 pLoop->addrOpenEphm[i] = -1;
21199 }
21200 }
21201 sqlite3KeyInfoUnref(pKeyInfo);
21202 }
21203
21204 multi_select_end:
21205 pDest->iSdst = dest.iSdst;
21206 pDest->nSdst = dest.nSdst;
21207 sqlite3SelectDelete(db, pDelete);
21208 return rc;
21209 }
21210 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
21211
21212 /*
21213 ** Error message for when two or more terms of a compound select have different
21214 ** size result sets.
21215 */
21216 SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
21217 if( p->selFlags & SF_Values ){
21218 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
21219 }else{
21220 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
21221 " do not have the same number of result columns", selectOpName(p->op));
21222 }
21223 }
21224
21225 /*
21226 ** Code an output subroutine for a coroutine implementation of a
21227 ** SELECT statment.
21228 **
21229 ** The data to be output is contained in pIn->iSdst. There are
21230 ** pIn->nSdst columns to be output. pDest is where the output should
21231 ** be sent.
21232 **
21233 ** regReturn is the number of the register holding the subroutine
21234 ** return address.
21235 **
21236 ** If regPrev>0 then it is the first register in a vector that
21237 ** records the previous output. mem[regPrev] is a flag that is false
21238 ** if there has been no previous output. If regPrev>0 then code is
21239 ** generated to suppress duplicates. pKeyInfo is used for comparing
21240 ** keys.
21241 **
21242 ** If the LIMIT found in p->iLimit is reached, jump immediately to
21243 ** iBreak.
21244 */
21245 static int generateOutputSubroutine(
21246 Parse *pParse, /* Parsing context */
21247 Select *p, /* The SELECT statement */
21248 SelectDest *pIn, /* Coroutine supplying data */
21249 SelectDest *pDest, /* Where to send the data */
21250 int regReturn, /* The return address register */
21251 int regPrev, /* Previous result register. No uniqueness if 0 */
21252 KeyInfo *pKeyInfo, /* For comparing with previous entry */
21253 int iBreak /* Jump here if we hit the LIMIT */
21254 ){
21255 Vdbe *v = pParse->pVdbe;
21256 int iContinue;
21257 int addr;
21258
21259 addr = sqlite3VdbeCurrentAddr(v);
21260 iContinue = sqlite3VdbeMakeLabel(v);
21261
21262 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
21263 */
21264 if( regPrev ){
21265 int addr1, addr2;
21266 addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
21267 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
21268 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
21269 sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
21270 sqlite3VdbeJumpHere(v, addr1);
21271 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
21272 sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
21273 }
21274 if( pParse->db->mallocFailed ) return 0;
21275
21276 /* Suppress the first OFFSET entries if there is an OFFSET clause
21277 */
21278 codeOffset(v, p->iOffset, iContinue);
21279
21280 assert( pDest->eDest!=SRT_Exists );
21281 assert( pDest->eDest!=SRT_Table );
21282 switch( pDest->eDest ){
21283 /* Store the result as data using a unique key.
21284 */
21285 case SRT_EphemTab: {
21286 int r1 = sqlite3GetTempReg(pParse);
21287 int r2 = sqlite3GetTempReg(pParse);
21288 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
21289 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
21290 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
21291 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
21292 sqlite3ReleaseTempReg(pParse, r2);
21293 sqlite3ReleaseTempReg(pParse, r1);
21294 break;
21295 }
21296
21297 #ifndef SQLITE_OMIT_SUBQUERY
21298 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
21299 ** then there should be a single item on the stack. Write this
21300 ** item into the set table with bogus data.
21301 */
21302 case SRT_Set: {
21303 int r1;
21304 assert( pIn->nSdst==1 || pParse->nErr>0 );
21305 pDest->affSdst =
21306 sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
21307 r1 = sqlite3GetTempReg(pParse);
21308 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
21309 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
21310 sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
21311 sqlite3ReleaseTempReg(pParse, r1);
21312 break;
21313 }
21314
21315 /* If this is a scalar select that is part of an expression, then
21316 ** store the results in the appropriate memory cell and break out
21317 ** of the scan loop.
21318 */
21319 case SRT_Mem: {
21320 assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 );
21321 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
21322 /* The LIMIT clause will jump out of the loop for us */
21323 break;
21324 }
21325 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
21326
21327 /* The results are stored in a sequence of registers
21328 ** starting at pDest->iSdst. Then the co-routine yields.
21329 */
21330 case SRT_Coroutine: {
21331 if( pDest->iSdst==0 ){
21332 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
21333 pDest->nSdst = pIn->nSdst;
21334 }
21335 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
21336 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
21337 break;
21338 }
21339
21340 /* If none of the above, then the result destination must be
21341 ** SRT_Output. This routine is never called with any other
21342 ** destination other than the ones handled above or SRT_Output.
21343 **
21344 ** For SRT_Output, results are stored in a sequence of registers.
21345 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
21346 ** return the next row of result.
21347 */
21348 default: {
21349 assert( pDest->eDest==SRT_Output );
21350 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
21351 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
21352 break;
21353 }
21354 }
21355
21356 /* Jump to the end of the loop if the LIMIT is reached.
21357 */
21358 if( p->iLimit ){
21359 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
21360 }
21361
21362 /* Generate the subroutine return
21363 */
21364 sqlite3VdbeResolveLabel(v, iContinue);
21365 sqlite3VdbeAddOp1(v, OP_Return, regReturn);
21366
21367 return addr;
21368 }
21369
21370 /*
21371 ** Alternative compound select code generator for cases when there
21372 ** is an ORDER BY clause.
21373 **
21374 ** We assume a query of the following form:
21375 **
21376 ** <selectA> <operator> <selectB> ORDER BY <orderbylist>
21377 **
21378 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
21379 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
21380 ** co-routines. Then run the co-routines in parallel and merge the results
21381 ** into the output. In addition to the two coroutines (called selectA and
21382 ** selectB) there are 7 subroutines:
21383 **
21384 ** outA: Move the output of the selectA coroutine into the output
21385 ** of the compound query.
21386 **
21387 ** outB: Move the output of the selectB coroutine into the output
21388 ** of the compound query. (Only generated for UNION and
21389 ** UNION ALL. EXCEPT and INSERTSECT never output a row that
21390 ** appears only in B.)
21391 **
21392 ** AltB: Called when there is data from both coroutines and A<B.
21393 **
21394 ** AeqB: Called when there is data from both coroutines and A==B.
21395 **
21396 ** AgtB: Called when there is data from both coroutines and A>B.
21397 **
21398 ** EofA: Called when data is exhausted from selectA.
21399 **
21400 ** EofB: Called when data is exhausted from selectB.
21401 **
21402 ** The implementation of the latter five subroutines depend on which
21403 ** <operator> is used:
21404 **
21405 **
21406 ** UNION ALL UNION EXCEPT INTERSECT
21407 ** ------------- ----------------- -------------- -----------------
21408 ** AltB: outA, nextA outA, nextA outA, nextA nextA
21409 **
21410 ** AeqB: outA, nextA nextA nextA outA, nextA
21411 **
21412 ** AgtB: outB, nextB outB, nextB nextB nextB
21413 **
21414 ** EofA: outB, nextB outB, nextB halt halt
21415 **
21416 ** EofB: outA, nextA outA, nextA outA, nextA halt
21417 **
21418 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
21419 ** causes an immediate jump to EofA and an EOF on B following nextB causes
21420 ** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
21421 ** following nextX causes a jump to the end of the select processing.
21422 **
21423 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
21424 ** within the output subroutine. The regPrev register set holds the previously
21425 ** output value. A comparison is made against this value and the output
21426 ** is skipped if the next results would be the same as the previous.
21427 **
21428 ** The implementation plan is to implement the two coroutines and seven
21429 ** subroutines first, then put the control logic at the bottom. Like this:
21430 **
21431 ** goto Init
21432 ** coA: coroutine for left query (A)
21433 ** coB: coroutine for right query (B)
21434 ** outA: output one row of A
21435 ** outB: output one row of B (UNION and UNION ALL only)
21436 ** EofA: ...
21437 ** EofB: ...
21438 ** AltB: ...
21439 ** AeqB: ...
21440 ** AgtB: ...
21441 ** Init: initialize coroutine registers
21442 ** yield coA
21443 ** if eof(A) goto EofA
21444 ** yield coB
21445 ** if eof(B) goto EofB
21446 ** Cmpr: Compare A, B
21447 ** Jump AltB, AeqB, AgtB
21448 ** End: ...
21449 **
21450 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
21451 ** actually called using Gosub and they do not Return. EofA and EofB loop
21452 ** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
21453 ** and AgtB jump to either L2 or to one of EofA or EofB.
21454 */
21455 #ifndef SQLITE_OMIT_COMPOUND_SELECT
21456 static int multiSelectOrderBy(
21457 Parse *pParse, /* Parsing context */
21458 Select *p, /* The right-most of SELECTs to be coded */
21459 SelectDest *pDest /* What to do with query results */
21460 ){
21461 int i, j; /* Loop counters */
21462 Select *pPrior; /* Another SELECT immediately to our left */
21463 Vdbe *v; /* Generate code to this VDBE */
21464 SelectDest destA; /* Destination for coroutine A */
21465 SelectDest destB; /* Destination for coroutine B */
21466 int regAddrA; /* Address register for select-A coroutine */
21467 int regAddrB; /* Address register for select-B coroutine */
21468 int addrSelectA; /* Address of the select-A coroutine */
21469 int addrSelectB; /* Address of the select-B coroutine */
21470 int regOutA; /* Address register for the output-A subroutine */
21471 int regOutB; /* Address register for the output-B subroutine */
21472 int addrOutA; /* Address of the output-A subroutine */
21473 int addrOutB = 0; /* Address of the output-B subroutine */
21474 int addrEofA; /* Address of the select-A-exhausted subroutine */
21475 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
21476 int addrEofB; /* Address of the select-B-exhausted subroutine */
21477 int addrAltB; /* Address of the A<B subroutine */
21478 int addrAeqB; /* Address of the A==B subroutine */
21479 int addrAgtB; /* Address of the A>B subroutine */
21480 int regLimitA; /* Limit register for select-A */
21481 int regLimitB; /* Limit register for select-A */
21482 int regPrev; /* A range of registers to hold previous output */
21483 int savedLimit; /* Saved value of p->iLimit */
21484 int savedOffset; /* Saved value of p->iOffset */
21485 int labelCmpr; /* Label for the start of the merge algorithm */
21486 int labelEnd; /* Label for the end of the overall SELECT stmt */
21487 int addr1; /* Jump instructions that get retargetted */
21488 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
21489 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
21490 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
21491 sqlite3 *db; /* Database connection */
21492 ExprList *pOrderBy; /* The ORDER BY clause */
21493 int nOrderBy; /* Number of terms in the ORDER BY clause */
21494 int *aPermute; /* Mapping from ORDER BY terms to result set columns */
21495 #ifndef SQLITE_OMIT_EXPLAIN
21496 int iSub1; /* EQP id of left-hand query */
21497 int iSub2; /* EQP id of right-hand query */
21498 #endif
21499
21500 assert( p->pOrderBy!=0 );
21501 assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
21502 db = pParse->db;
21503 v = pParse->pVdbe;
21504 assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
21505 labelEnd = sqlite3VdbeMakeLabel(v);
21506 labelCmpr = sqlite3VdbeMakeLabel(v);
21507
21508
21509 /* Patch up the ORDER BY clause
21510 */
21511 op = p->op;
21512 pPrior = p->pPrior;
21513 assert( pPrior->pOrderBy==0 );
21514 pOrderBy = p->pOrderBy;
21515 assert( pOrderBy );
21516 nOrderBy = pOrderBy->nExpr;
21517
21518 /* For operators other than UNION ALL we have to make sure that
21519 ** the ORDER BY clause covers every term of the result set. Add
21520 ** terms to the ORDER BY clause as necessary.
21521 */
21522 if( op!=TK_ALL ){
21523 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
21524 struct ExprList_item *pItem;
21525 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
21526 assert( pItem->u.x.iOrderByCol>0 );
21527 if( pItem->u.x.iOrderByCol==i ) break;
21528 }
21529 if( j==nOrderBy ){
21530 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
21531 if( pNew==0 ) return SQLITE_NOMEM;
21532 pNew->flags |= EP_IntValue;
21533 pNew->u.iValue = i;
21534 pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
21535 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
21536 }
21537 }
21538 }
21539
21540 /* Compute the comparison permutation and keyinfo that is used with
21541 ** the permutation used to determine if the next
21542 ** row of results comes from selectA or selectB. Also add explicit
21543 ** collations to the ORDER BY clause terms so that when the subqueries
21544 ** to the right and the left are evaluated, they use the correct
21545 ** collation.
21546 */
21547 aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
21548 if( aPermute ){
21549 struct ExprList_item *pItem;
21550 for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
21551 assert( pItem->u.x.iOrderByCol>0 );
21552 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
21553 aPermute[i] = pItem->u.x.iOrderByCol - 1;
21554 }
21555 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
21556 }else{
21557 pKeyMerge = 0;
21558 }
21559
21560 /* Reattach the ORDER BY clause to the query.
21561 */
21562 p->pOrderBy = pOrderBy;
21563 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
21564
21565 /* Allocate a range of temporary registers and the KeyInfo needed
21566 ** for the logic that removes duplicate result rows when the
21567 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
21568 */
21569 if( op==TK_ALL ){
21570 regPrev = 0;
21571 }else{
21572 int nExpr = p->pEList->nExpr;
21573 assert( nOrderBy>=nExpr || db->mallocFailed );
21574 regPrev = pParse->nMem+1;
21575 pParse->nMem += nExpr+1;
21576 sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
21577 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
21578 if( pKeyDup ){
21579 assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
21580 for(i=0; i<nExpr; i++){
21581 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
21582 pKeyDup->aSortOrder[i] = 0;
21583 }
21584 }
21585 }
21586
21587 /* Separate the left and the right query from one another
21588 */
21589 p->pPrior = 0;
21590 pPrior->pNext = 0;
21591 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
21592 if( pPrior->pPrior==0 ){
21593 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
21594 }
21595
21596 /* Compute the limit registers */
21597 computeLimitRegisters(pParse, p, labelEnd);
21598 if( p->iLimit && op==TK_ALL ){
21599 regLimitA = ++pParse->nMem;
21600 regLimitB = ++pParse->nMem;
21601 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
21602 regLimitA);
21603 sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
21604 }else{
21605 regLimitA = regLimitB = 0;
21606 }
21607 sqlite3ExprDelete(db, p->pLimit);
21608 p->pLimit = 0;
21609 sqlite3ExprDelete(db, p->pOffset);
21610 p->pOffset = 0;
21611
21612 regAddrA = ++pParse->nMem;
21613 regAddrB = ++pParse->nMem;
21614 regOutA = ++pParse->nMem;
21615 regOutB = ++pParse->nMem;
21616 sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
21617 sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
21618
21619 /* Generate a coroutine to evaluate the SELECT statement to the
21620 ** left of the compound operator - the "A" select.
21621 */
21622 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
21623 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
21624 VdbeComment((v, "left SELECT"));
21625 pPrior->iLimit = regLimitA;
21626 explainSetInteger(iSub1, pParse->iNextSelectId);
21627 sqlite3Select(pParse, pPrior, &destA);
21628 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
21629 sqlite3VdbeJumpHere(v, addr1);
21630
21631 /* Generate a coroutine to evaluate the SELECT statement on
21632 ** the right - the "B" select
21633 */
21634 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
21635 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
21636 VdbeComment((v, "right SELECT"));
21637 savedLimit = p->iLimit;
21638 savedOffset = p->iOffset;
21639 p->iLimit = regLimitB;
21640 p->iOffset = 0;
21641 explainSetInteger(iSub2, pParse->iNextSelectId);
21642 sqlite3Select(pParse, p, &destB);
21643 p->iLimit = savedLimit;
21644 p->iOffset = savedOffset;
21645 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
21646
21647 /* Generate a subroutine that outputs the current row of the A
21648 ** select as the next output row of the compound select.
21649 */
21650 VdbeNoopComment((v, "Output routine for A"));
21651 addrOutA = generateOutputSubroutine(pParse,
21652 p, &destA, pDest, regOutA,
21653 regPrev, pKeyDup, labelEnd);
21654
21655 /* Generate a subroutine that outputs the current row of the B
21656 ** select as the next output row of the compound select.
21657 */
21658 if( op==TK_ALL || op==TK_UNION ){
21659 VdbeNoopComment((v, "Output routine for B"));
21660 addrOutB = generateOutputSubroutine(pParse,
21661 p, &destB, pDest, regOutB,
21662 regPrev, pKeyDup, labelEnd);
21663 }
21664 sqlite3KeyInfoUnref(pKeyDup);
21665
21666 /* Generate a subroutine to run when the results from select A
21667 ** are exhausted and only data in select B remains.
21668 */
21669 if( op==TK_EXCEPT || op==TK_INTERSECT ){
21670 addrEofA_noB = addrEofA = labelEnd;
21671 }else{
21672 VdbeNoopComment((v, "eof-A subroutine"));
21673 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
21674 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
21675 VdbeCoverage(v);
21676 sqlite3VdbeGoto(v, addrEofA);
21677 p->nSelectRow += pPrior->nSelectRow;
21678 }
21679
21680 /* Generate a subroutine to run when the results from select B
21681 ** are exhausted and only data in select A remains.
21682 */
21683 if( op==TK_INTERSECT ){
21684 addrEofB = addrEofA;
21685 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
21686 }else{
21687 VdbeNoopComment((v, "eof-B subroutine"));
21688 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
21689 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
21690 sqlite3VdbeGoto(v, addrEofB);
21691 }
21692
21693 /* Generate code to handle the case of A<B
21694 */
21695 VdbeNoopComment((v, "A-lt-B subroutine"));
21696 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
21697 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
21698 sqlite3VdbeGoto(v, labelCmpr);
21699
21700 /* Generate code to handle the case of A==B
21701 */
21702 if( op==TK_ALL ){
21703 addrAeqB = addrAltB;
21704 }else if( op==TK_INTERSECT ){
21705 addrAeqB = addrAltB;
21706 addrAltB++;
21707 }else{
21708 VdbeNoopComment((v, "A-eq-B subroutine"));
21709 addrAeqB =
21710 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
21711 sqlite3VdbeGoto(v, labelCmpr);
21712 }
21713
21714 /* Generate code to handle the case of A>B
21715 */
21716 VdbeNoopComment((v, "A-gt-B subroutine"));
21717 addrAgtB = sqlite3VdbeCurrentAddr(v);
21718 if( op==TK_ALL || op==TK_UNION ){
21719 sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
21720 }
21721 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
21722 sqlite3VdbeGoto(v, labelCmpr);
21723
21724 /* This code runs once to initialize everything.
21725 */
21726 sqlite3VdbeJumpHere(v, addr1);
21727 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
21728 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
21729
21730 /* Implement the main merge loop
21731 */
21732 sqlite3VdbeResolveLabel(v, labelCmpr);
21733 sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
21734 sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
21735 (char*)pKeyMerge, P4_KEYINFO);
21736 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
21737 sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
21738
21739 /* Jump to the this point in order to terminate the query.
21740 */
21741 sqlite3VdbeResolveLabel(v, labelEnd);
21742
21743 /* Set the number of output columns
21744 */
21745 if( pDest->eDest==SRT_Output ){
21746 Select *pFirst = pPrior;
21747 while( pFirst->pPrior ) pFirst = pFirst->pPrior;
21748 generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList);
21749 }
21750
21751 /* Reassembly the compound query so that it will be freed correctly
21752 ** by the calling function */
21753 if( p->pPrior ){
21754 sqlite3SelectDelete(db, p->pPrior);
21755 }
21756 p->pPrior = pPrior;
21757 pPrior->pNext = p;
21758
21759 /*** TBD: Insert subroutine calls to close cursors on incomplete
21760 **** subqueries ****/
21761 explainComposite(pParse, p->op, iSub1, iSub2, 0);
21762 return pParse->nErr!=0;
21763 }
21764 #endif
21765
21766 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
21767 /* Forward Declarations */
21768 static void substExprList(sqlite3*, ExprList*, int, ExprList*);
21769 static void substSelect(sqlite3*, Select *, int, ExprList*, int);
21770
21771 /*
21772 ** Scan through the expression pExpr. Replace every reference to
21773 ** a column in table number iTable with a copy of the iColumn-th
21774 ** entry in pEList. (But leave references to the ROWID column
21775 ** unchanged.)
21776 **
21777 ** This routine is part of the flattening procedure. A subquery
21778 ** whose result set is defined by pEList appears as entry in the
21779 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
21780 ** FORM clause entry is iTable. This routine make the necessary
21781 ** changes to pExpr so that it refers directly to the source table
21782 ** of the subquery rather the result set of the subquery.
21783 */
21784 static Expr *substExpr(
21785 sqlite3 *db, /* Report malloc errors to this connection */
21786 Expr *pExpr, /* Expr in which substitution occurs */
21787 int iTable, /* Table to be substituted */
21788 ExprList *pEList /* Substitute expressions */
21789 ){
21790 if( pExpr==0 ) return 0;
21791 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
21792 if( pExpr->iColumn<0 ){
21793 pExpr->op = TK_NULL;
21794 }else{
21795 Expr *pNew;
21796 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
21797 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
21798 pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
21799 sqlite3ExprDelete(db, pExpr);
21800 pExpr = pNew;
21801 }
21802 }else{
21803 pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
21804 pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
21805 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
21806 substSelect(db, pExpr->x.pSelect, iTable, pEList, 1);
21807 }else{
21808 substExprList(db, pExpr->x.pList, iTable, pEList);
21809 }
21810 }
21811 return pExpr;
21812 }
21813 static void substExprList(
21814 sqlite3 *db, /* Report malloc errors here */
21815 ExprList *pList, /* List to scan and in which to make substitutes */
21816 int iTable, /* Table to be substituted */
21817 ExprList *pEList /* Substitute values */
21818 ){
21819 int i;
21820 if( pList==0 ) return;
21821 for(i=0; i<pList->nExpr; i++){
21822 pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
21823 }
21824 }
21825 static void substSelect(
21826 sqlite3 *db, /* Report malloc errors here */
21827 Select *p, /* SELECT statement in which to make substitutions */
21828 int iTable, /* Table to be replaced */
21829 ExprList *pEList, /* Substitute values */
21830 int doPrior /* Do substitutes on p->pPrior too */
21831 ){
21832 SrcList *pSrc;
21833 struct SrcList_item *pItem;
21834 int i;
21835 if( !p ) return;
21836 do{
21837 substExprList(db, p->pEList, iTable, pEList);
21838 substExprList(db, p->pGroupBy, iTable, pEList);
21839 substExprList(db, p->pOrderBy, iTable, pEList);
21840 p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
21841 p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
21842 pSrc = p->pSrc;
21843 assert( pSrc!=0 );
21844 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
21845 substSelect(db, pItem->pSelect, iTable, pEList, 1);
21846 if( pItem->fg.isTabFunc ){
21847 substExprList(db, pItem->u1.pFuncArg, iTable, pEList);
21848 }
21849 }
21850 }while( doPrior && (p = p->pPrior)!=0 );
21851 }
21852 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
21853
21854 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
21855 /*
21856 ** This routine attempts to flatten subqueries as a performance optimization.
21857 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
21858 **
21859 ** To understand the concept of flattening, consider the following
21860 ** query:
21861 **
21862 ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
21863 **
21864 ** The default way of implementing this query is to execute the
21865 ** subquery first and store the results in a temporary table, then
21866 ** run the outer query on that temporary table. This requires two
21867 ** passes over the data. Furthermore, because the temporary table
21868 ** has no indices, the WHERE clause on the outer query cannot be
21869 ** optimized.
21870 **
21871 ** This routine attempts to rewrite queries such as the above into
21872 ** a single flat select, like this:
21873 **
21874 ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
21875 **
21876 ** The code generated for this simplification gives the same result
21877 ** but only has to scan the data once. And because indices might
21878 ** exist on the table t1, a complete scan of the data might be
21879 ** avoided.
21880 **
21881 ** Flattening is only attempted if all of the following are true:
21882 **
21883 ** (1) The subquery and the outer query do not both use aggregates.
21884 **
21885 ** (2) The subquery is not an aggregate or (2a) the outer query is not a join
21886 ** and (2b) the outer query does not use subqueries other than the one
21887 ** FROM-clause subquery that is a candidate for flattening. (2b is
21888 ** due to ticket [2f7170d73bf9abf80] from 2015-02-09.)
21889 **
21890 ** (3) The subquery is not the right operand of a left outer join
21891 ** (Originally ticket #306. Strengthened by ticket #3300)
21892 **
21893 ** (4) The subquery is not DISTINCT.
21894 **
21895 ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
21896 ** sub-queries that were excluded from this optimization. Restriction
21897 ** (4) has since been expanded to exclude all DISTINCT subqueries.
21898 **
21899 ** (6) The subquery does not use aggregates or the outer query is not
21900 ** DISTINCT.
21901 **
21902 ** (7) The subquery has a FROM clause. TODO: For subqueries without
21903 ** A FROM clause, consider adding a FROM close with the special
21904 ** table sqlite_once that consists of a single row containing a
21905 ** single NULL.
21906 **
21907 ** (8) The subquery does not use LIMIT or the outer query is not a join.
21908 **
21909 ** (9) The subquery does not use LIMIT or the outer query does not use
21910 ** aggregates.
21911 **
21912 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
21913 ** accidently carried the comment forward until 2014-09-15. Original
21914 ** text: "The subquery does not use aggregates or the outer query
21915 ** does not use LIMIT."
21916 **
21917 ** (11) The subquery and the outer query do not both have ORDER BY clauses.
21918 **
21919 ** (**) Not implemented. Subsumed into restriction (3). Was previously
21920 ** a separate restriction deriving from ticket #350.
21921 **
21922 ** (13) The subquery and outer query do not both use LIMIT.
21923 **
21924 ** (14) The subquery does not use OFFSET.
21925 **
21926 ** (15) The outer query is not part of a compound select or the
21927 ** subquery does not have a LIMIT clause.
21928 ** (See ticket #2339 and ticket [02a8e81d44]).
21929 **
21930 ** (16) The outer query is not an aggregate or the subquery does
21931 ** not contain ORDER BY. (Ticket #2942) This used to not matter
21932 ** until we introduced the group_concat() function.
21933 **
21934 ** (17) The sub-query is not a compound select, or it is a UNION ALL
21935 ** compound clause made up entirely of non-aggregate queries, and
21936 ** the parent query:
21937 **
21938 ** * is not itself part of a compound select,
21939 ** * is not an aggregate or DISTINCT query, and
21940 ** * is not a join
21941 **
21942 ** The parent and sub-query may contain WHERE clauses. Subject to
21943 ** rules (11), (13) and (14), they may also contain ORDER BY,
21944 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
21945 ** operator other than UNION ALL because all the other compound
21946 ** operators have an implied DISTINCT which is disallowed by
21947 ** restriction (4).
21948 **
21949 ** Also, each component of the sub-query must return the same number
21950 ** of result columns. This is actually a requirement for any compound
21951 ** SELECT statement, but all the code here does is make sure that no
21952 ** such (illegal) sub-query is flattened. The caller will detect the
21953 ** syntax error and return a detailed message.
21954 **
21955 ** (18) If the sub-query is a compound select, then all terms of the
21956 ** ORDER by clause of the parent must be simple references to
21957 ** columns of the sub-query.
21958 **
21959 ** (19) The subquery does not use LIMIT or the outer query does not
21960 ** have a WHERE clause.
21961 **
21962 ** (20) If the sub-query is a compound select, then it must not use
21963 ** an ORDER BY clause. Ticket #3773. We could relax this constraint
21964 ** somewhat by saying that the terms of the ORDER BY clause must
21965 ** appear as unmodified result columns in the outer query. But we
21966 ** have other optimizations in mind to deal with that case.
21967 **
21968 ** (21) The subquery does not use LIMIT or the outer query is not
21969 ** DISTINCT. (See ticket [752e1646fc]).
21970 **
21971 ** (22) The subquery is not a recursive CTE.
21972 **
21973 ** (23) The parent is not a recursive CTE, or the sub-query is not a
21974 ** compound query. This restriction is because transforming the
21975 ** parent to a compound query confuses the code that handles
21976 ** recursive queries in multiSelect().
21977 **
21978 ** (24) The subquery is not an aggregate that uses the built-in min() or
21979 ** or max() functions. (Without this restriction, a query like:
21980 ** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
21981 ** return the value X for which Y was maximal.)
21982 **
21983 **
21984 ** In this routine, the "p" parameter is a pointer to the outer query.
21985 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
21986 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
21987 **
21988 ** If flattening is not attempted, this routine is a no-op and returns 0.
21989 ** If flattening is attempted this routine returns 1.
21990 **
21991 ** All of the expression analysis must occur on both the outer query and
21992 ** the subquery before this routine runs.
21993 */
21994 static int flattenSubquery(
21995 Parse *pParse, /* Parsing context */
21996 Select *p, /* The parent or outer SELECT statement */
21997 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
21998 int isAgg, /* True if outer SELECT uses aggregate functions */
21999 int subqueryIsAgg /* True if the subquery uses aggregate functions */
22000 ){
22001 const char *zSavedAuthContext = pParse->zAuthContext;
22002 Select *pParent; /* Current UNION ALL term of the other query */
22003 Select *pSub; /* The inner query or "subquery" */
22004 Select *pSub1; /* Pointer to the rightmost select in sub-query */
22005 SrcList *pSrc; /* The FROM clause of the outer query */
22006 SrcList *pSubSrc; /* The FROM clause of the subquery */
22007 ExprList *pList; /* The result set of the outer query */
22008 int iParent; /* VDBE cursor number of the pSub result set temp table */
22009 int i; /* Loop counter */
22010 Expr *pWhere; /* The WHERE clause */
22011 struct SrcList_item *pSubitem; /* The subquery */
22012 sqlite3 *db = pParse->db;
22013
22014 /* Check to see if flattening is permitted. Return 0 if not.
22015 */
22016 assert( p!=0 );
22017 assert( p->pPrior==0 ); /* Unable to flatten compound queries */
22018 if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
22019 pSrc = p->pSrc;
22020 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
22021 pSubitem = &pSrc->a[iFrom];
22022 iParent = pSubitem->iCursor;
22023 pSub = pSubitem->pSelect;
22024 assert( pSub!=0 );
22025 if( subqueryIsAgg ){
22026 if( isAgg ) return 0; /* Restriction (1) */
22027 if( pSrc->nSrc>1 ) return 0; /* Restriction (2a) */
22028 if( (p->pWhere && ExprHasProperty(p->pWhere,EP_Subquery))
22029 || (sqlite3ExprListFlags(p->pEList) & EP_Subquery)!=0
22030 || (sqlite3ExprListFlags(p->pOrderBy) & EP_Subquery)!=0
22031 ){
22032 return 0; /* Restriction (2b) */
22033 }
22034 }
22035
22036 pSubSrc = pSub->pSrc;
22037 assert( pSubSrc );
22038 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
22039 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
22040 ** because they could be computed at compile-time. But when LIMIT and OFFSET
22041 ** became arbitrary expressions, we were forced to add restrictions (13)
22042 ** and (14). */
22043 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
22044 if( pSub->pOffset ) return 0; /* Restriction (14) */
22045 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
22046 return 0; /* Restriction (15) */
22047 }
22048 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
22049 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */
22050 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
22051 return 0; /* Restrictions (8)(9) */
22052 }
22053 if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
22054 return 0; /* Restriction (6) */
22055 }
22056 if( p->pOrderBy && pSub->pOrderBy ){
22057 return 0; /* Restriction (11) */
22058 }
22059 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
22060 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
22061 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
22062 return 0; /* Restriction (21) */
22063 }
22064 testcase( pSub->selFlags & SF_Recursive );
22065 testcase( pSub->selFlags & SF_MinMaxAgg );
22066 if( pSub->selFlags & (SF_Recursive|SF_MinMaxAgg) ){
22067 return 0; /* Restrictions (22) and (24) */
22068 }
22069 if( (p->selFlags & SF_Recursive) && pSub->pPrior ){
22070 return 0; /* Restriction (23) */
22071 }
22072
22073 /* OBSOLETE COMMENT 1:
22074 ** Restriction 3: If the subquery is a join, make sure the subquery is
22075 ** not used as the right operand of an outer join. Examples of why this
22076 ** is not allowed:
22077 **
22078 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
22079 **
22080 ** If we flatten the above, we would get
22081 **
22082 ** (t1 LEFT OUTER JOIN t2) JOIN t3
22083 **
22084 ** which is not at all the same thing.
22085 **
22086 ** OBSOLETE COMMENT 2:
22087 ** Restriction 12: If the subquery is the right operand of a left outer
22088 ** join, make sure the subquery has no WHERE clause.
22089 ** An examples of why this is not allowed:
22090 **
22091 ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
22092 **
22093 ** If we flatten the above, we would get
22094 **
22095 ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
22096 **
22097 ** But the t2.x>0 test will always fail on a NULL row of t2, which
22098 ** effectively converts the OUTER JOIN into an INNER JOIN.
22099 **
22100 ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
22101 ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
22102 ** is fraught with danger. Best to avoid the whole thing. If the
22103 ** subquery is the right term of a LEFT JOIN, then do not flatten.
22104 */
22105 if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
22106 return 0;
22107 }
22108
22109 /* Restriction 17: If the sub-query is a compound SELECT, then it must
22110 ** use only the UNION ALL operator. And none of the simple select queries
22111 ** that make up the compound SELECT are allowed to be aggregate or distinct
22112 ** queries.
22113 */
22114 if( pSub->pPrior ){
22115 if( pSub->pOrderBy ){
22116 return 0; /* Restriction 20 */
22117 }
22118 if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
22119 return 0;
22120 }
22121 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
22122 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
22123 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
22124 assert( pSub->pSrc!=0 );
22125 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
22126 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
22127 || (pSub1->pPrior && pSub1->op!=TK_ALL)
22128 || pSub1->pSrc->nSrc<1
22129 ){
22130 return 0;
22131 }
22132 testcase( pSub1->pSrc->nSrc>1 );
22133 }
22134
22135 /* Restriction 18. */
22136 if( p->pOrderBy ){
22137 int ii;
22138 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
22139 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
22140 }
22141 }
22142 }
22143
22144 /***** If we reach this point, flattening is permitted. *****/
22145 SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n",
22146 pSub->zSelName, pSub, iFrom));
22147
22148 /* Authorize the subquery */
22149 pParse->zAuthContext = pSubitem->zName;
22150 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
22151 testcase( i==SQLITE_DENY );
22152 pParse->zAuthContext = zSavedAuthContext;
22153
22154 /* If the sub-query is a compound SELECT statement, then (by restrictions
22155 ** 17 and 18 above) it must be a UNION ALL and the parent query must
22156 ** be of the form:
22157 **
22158 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
22159 **
22160 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
22161 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
22162 ** OFFSET clauses and joins them to the left-hand-side of the original
22163 ** using UNION ALL operators. In this case N is the number of simple
22164 ** select statements in the compound sub-query.
22165 **
22166 ** Example:
22167 **
22168 ** SELECT a+1 FROM (
22169 ** SELECT x FROM tab
22170 ** UNION ALL
22171 ** SELECT y FROM tab
22172 ** UNION ALL
22173 ** SELECT abs(z*2) FROM tab2
22174 ** ) WHERE a!=5 ORDER BY 1
22175 **
22176 ** Transformed into:
22177 **
22178 ** SELECT x+1 FROM tab WHERE x+1!=5
22179 ** UNION ALL
22180 ** SELECT y+1 FROM tab WHERE y+1!=5
22181 ** UNION ALL
22182 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
22183 ** ORDER BY 1
22184 **
22185 ** We call this the "compound-subquery flattening".
22186 */
22187 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
22188 Select *pNew;
22189 ExprList *pOrderBy = p->pOrderBy;
22190 Expr *pLimit = p->pLimit;
22191 Expr *pOffset = p->pOffset;
22192 Select *pPrior = p->pPrior;
22193 p->pOrderBy = 0;
22194 p->pSrc = 0;
22195 p->pPrior = 0;
22196 p->pLimit = 0;
22197 p->pOffset = 0;
22198 pNew = sqlite3SelectDup(db, p, 0);
22199 sqlite3SelectSetName(pNew, pSub->zSelName);
22200 p->pOffset = pOffset;
22201 p->pLimit = pLimit;
22202 p->pOrderBy = pOrderBy;
22203 p->pSrc = pSrc;
22204 p->op = TK_ALL;
22205 if( pNew==0 ){
22206 p->pPrior = pPrior;
22207 }else{
22208 pNew->pPrior = pPrior;
22209 if( pPrior ) pPrior->pNext = pNew;
22210 pNew->pNext = p;
22211 p->pPrior = pNew;
22212 SELECTTRACE(2,pParse,p,
22213 ("compound-subquery flattener creates %s.%p as peer\n",
22214 pNew->zSelName, pNew));
22215 }
22216 if( db->mallocFailed ) return 1;
22217 }
22218
22219 /* Begin flattening the iFrom-th entry of the FROM clause
22220 ** in the outer query.
22221 */
22222 pSub = pSub1 = pSubitem->pSelect;
22223
22224 /* Delete the transient table structure associated with the
22225 ** subquery
22226 */
22227 sqlite3DbFree(db, pSubitem->zDatabase);
22228 sqlite3DbFree(db, pSubitem->zName);
22229 sqlite3DbFree(db, pSubitem->zAlias);
22230 pSubitem->zDatabase = 0;
22231 pSubitem->zName = 0;
22232 pSubitem->zAlias = 0;
22233 pSubitem->pSelect = 0;
22234
22235 /* Defer deleting the Table object associated with the
22236 ** subquery until code generation is
22237 ** complete, since there may still exist Expr.pTab entries that
22238 ** refer to the subquery even after flattening. Ticket #3346.
22239 **
22240 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
22241 */
22242 if( ALWAYS(pSubitem->pTab!=0) ){
22243 Table *pTabToDel = pSubitem->pTab;
22244 if( pTabToDel->nRef==1 ){
22245 Parse *pToplevel = sqlite3ParseToplevel(pParse);
22246 pTabToDel->pNextZombie = pToplevel->pZombieTab;
22247 pToplevel->pZombieTab = pTabToDel;
22248 }else{
22249 pTabToDel->nRef--;
22250 }
22251 pSubitem->pTab = 0;
22252 }
22253
22254 /* The following loop runs once for each term in a compound-subquery
22255 ** flattening (as described above). If we are doing a different kind
22256 ** of flattening - a flattening other than a compound-subquery flattening -
22257 ** then this loop only runs once.
22258 **
22259 ** This loop moves all of the FROM elements of the subquery into the
22260 ** the FROM clause of the outer query. Before doing this, remember
22261 ** the cursor number for the original outer query FROM element in
22262 ** iParent. The iParent cursor will never be used. Subsequent code
22263 ** will scan expressions looking for iParent references and replace
22264 ** those references with expressions that resolve to the subquery FROM
22265 ** elements we are now copying in.
22266 */
22267 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
22268 int nSubSrc;
22269 u8 jointype = 0;
22270 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
22271 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
22272 pSrc = pParent->pSrc; /* FROM clause of the outer query */
22273
22274 if( pSrc ){
22275 assert( pParent==p ); /* First time through the loop */
22276 jointype = pSubitem->fg.jointype;
22277 }else{
22278 assert( pParent!=p ); /* 2nd and subsequent times through the loop */
22279 pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
22280 if( pSrc==0 ){
22281 assert( db->mallocFailed );
22282 break;
22283 }
22284 }
22285
22286 /* The subquery uses a single slot of the FROM clause of the outer
22287 ** query. If the subquery has more than one element in its FROM clause,
22288 ** then expand the outer query to make space for it to hold all elements
22289 ** of the subquery.
22290 **
22291 ** Example:
22292 **
22293 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
22294 **
22295 ** The outer query has 3 slots in its FROM clause. One slot of the
22296 ** outer query (the middle slot) is used by the subquery. The next
22297 ** block of code will expand the outer query FROM clause to 4 slots.
22298 ** The middle slot is expanded to two slots in order to make space
22299 ** for the two elements in the FROM clause of the subquery.
22300 */
22301 if( nSubSrc>1 ){
22302 pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
22303 if( db->mallocFailed ){
22304 break;
22305 }
22306 }
22307
22308 /* Transfer the FROM clause terms from the subquery into the
22309 ** outer query.
22310 */
22311 for(i=0; i<nSubSrc; i++){
22312 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
22313 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
22314 pSrc->a[i+iFrom] = pSubSrc->a[i];
22315 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
22316 }
22317 pSrc->a[iFrom].fg.jointype = jointype;
22318
22319 /* Now begin substituting subquery result set expressions for
22320 ** references to the iParent in the outer query.
22321 **
22322 ** Example:
22323 **
22324 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
22325 ** \ \_____________ subquery __________/ /
22326 ** \_____________________ outer query ______________________________/
22327 **
22328 ** We look at every expression in the outer query and every place we see
22329 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
22330 */
22331 pList = pParent->pEList;
22332 for(i=0; i<pList->nExpr; i++){
22333 if( pList->a[i].zName==0 ){
22334 char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
22335 sqlite3Dequote(zName);
22336 pList->a[i].zName = zName;
22337 }
22338 }
22339 if( pSub->pOrderBy ){
22340 /* At this point, any non-zero iOrderByCol values indicate that the
22341 ** ORDER BY column expression is identical to the iOrderByCol'th
22342 ** expression returned by SELECT statement pSub. Since these values
22343 ** do not necessarily correspond to columns in SELECT statement pParent,
22344 ** zero them before transfering the ORDER BY clause.
22345 **
22346 ** Not doing this may cause an error if a subsequent call to this
22347 ** function attempts to flatten a compound sub-query into pParent
22348 ** (the only way this can happen is if the compound sub-query is
22349 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
22350 ExprList *pOrderBy = pSub->pOrderBy;
22351 for(i=0; i<pOrderBy->nExpr; i++){
22352 pOrderBy->a[i].u.x.iOrderByCol = 0;
22353 }
22354 assert( pParent->pOrderBy==0 );
22355 assert( pSub->pPrior==0 );
22356 pParent->pOrderBy = pOrderBy;
22357 pSub->pOrderBy = 0;
22358 }
22359 pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
22360 if( subqueryIsAgg ){
22361 assert( pParent->pHaving==0 );
22362 pParent->pHaving = pParent->pWhere;
22363 pParent->pWhere = pWhere;
22364 pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving,
22365 sqlite3ExprDup(db, pSub->pHaving, 0));
22366 assert( pParent->pGroupBy==0 );
22367 pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
22368 }else{
22369 pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
22370 }
22371 substSelect(db, pParent, iParent, pSub->pEList, 0);
22372
22373 /* The flattened query is distinct if either the inner or the
22374 ** outer query is distinct.
22375 */
22376 pParent->selFlags |= pSub->selFlags & SF_Distinct;
22377
22378 /*
22379 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
22380 **
22381 ** One is tempted to try to add a and b to combine the limits. But this
22382 ** does not work if either limit is negative.
22383 */
22384 if( pSub->pLimit ){
22385 pParent->pLimit = pSub->pLimit;
22386 pSub->pLimit = 0;
22387 }
22388 }
22389
22390 /* Finially, delete what is left of the subquery and return
22391 ** success.
22392 */
22393 sqlite3SelectDelete(db, pSub1);
22394
22395 #if SELECTTRACE_ENABLED
22396 if( sqlite3SelectTrace & 0x100 ){
22397 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
22398 sqlite3TreeViewSelect(0, p, 0);
22399 }
22400 #endif
22401
22402 return 1;
22403 }
22404 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
22405
22406
22407
22408 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
22409 /*
22410 ** Make copies of relevant WHERE clause terms of the outer query into
22411 ** the WHERE clause of subquery. Example:
22412 **
22413 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
22414 **
22415 ** Transformed into:
22416 **
22417 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
22418 ** WHERE x=5 AND y=10;
22419 **
22420 ** The hope is that the terms added to the inner query will make it more
22421 ** efficient.
22422 **
22423 ** Do not attempt this optimization if:
22424 **
22425 ** (1) The inner query is an aggregate. (In that case, we'd really want
22426 ** to copy the outer WHERE-clause terms onto the HAVING clause of the
22427 ** inner query. But they probably won't help there so do not bother.)
22428 **
22429 ** (2) The inner query is the recursive part of a common table expression.
22430 **
22431 ** (3) The inner query has a LIMIT clause (since the changes to the WHERE
22432 ** close would change the meaning of the LIMIT).
22433 **
22434 ** (4) The inner query is the right operand of a LEFT JOIN. (The caller
22435 ** enforces this restriction since this routine does not have enough
22436 ** information to know.)
22437 **
22438 ** (5) The WHERE clause expression originates in the ON or USING clause
22439 ** of a LEFT JOIN.
22440 **
22441 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
22442 ** terms are duplicated into the subquery.
22443 */
22444 static int pushDownWhereTerms(
22445 sqlite3 *db, /* The database connection (for malloc()) */
22446 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
22447 Expr *pWhere, /* The WHERE clause of the outer query */
22448 int iCursor /* Cursor number of the subquery */
22449 ){
22450 Expr *pNew;
22451 int nChng = 0;
22452 if( pWhere==0 ) return 0;
22453 if( (pSubq->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){
22454 return 0; /* restrictions (1) and (2) */
22455 }
22456 if( pSubq->pLimit!=0 ){
22457 return 0; /* restriction (3) */
22458 }
22459 while( pWhere->op==TK_AND ){
22460 nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor);
22461 pWhere = pWhere->pLeft;
22462 }
22463 if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction 5 */
22464 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
22465 nChng++;
22466 while( pSubq ){
22467 pNew = sqlite3ExprDup(db, pWhere, 0);
22468 pNew = substExpr(db, pNew, iCursor, pSubq->pEList);
22469 pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew);
22470 pSubq = pSubq->pPrior;
22471 }
22472 }
22473 return nChng;
22474 }
22475 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
22476
22477 /*
22478 ** Based on the contents of the AggInfo structure indicated by the first
22479 ** argument, this function checks if the following are true:
22480 **
22481 ** * the query contains just a single aggregate function,
22482 ** * the aggregate function is either min() or max(), and
22483 ** * the argument to the aggregate function is a column value.
22484 **
22485 ** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
22486 ** is returned as appropriate. Also, *ppMinMax is set to point to the
22487 ** list of arguments passed to the aggregate before returning.
22488 **
22489 ** Or, if the conditions above are not met, *ppMinMax is set to 0 and
22490 ** WHERE_ORDERBY_NORMAL is returned.
22491 */
22492 static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
22493 int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
22494
22495 *ppMinMax = 0;
22496 if( pAggInfo->nFunc==1 ){
22497 Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
22498 ExprList *pEList = pExpr->x.pList; /* Arguments to agg function */
22499
22500 assert( pExpr->op==TK_AGG_FUNCTION );
22501 if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
22502 const char *zFunc = pExpr->u.zToken;
22503 if( sqlite3StrICmp(zFunc, "min")==0 ){
22504 eRet = WHERE_ORDERBY_MIN;
22505 *ppMinMax = pEList;
22506 }else if( sqlite3StrICmp(zFunc, "max")==0 ){
22507 eRet = WHERE_ORDERBY_MAX;
22508 *ppMinMax = pEList;
22509 }
22510 }
22511 }
22512
22513 assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
22514 return eRet;
22515 }
22516
22517 /*
22518 ** The select statement passed as the first argument is an aggregate query.
22519 ** The second argument is the associated aggregate-info object. This
22520 ** function tests if the SELECT is of the form:
22521 **
22522 ** SELECT count(*) FROM <tbl>
22523 **
22524 ** where table is a database table, not a sub-select or view. If the query
22525 ** does match this pattern, then a pointer to the Table object representing
22526 ** <tbl> is returned. Otherwise, 0 is returned.
22527 */
22528 static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
22529 Table *pTab;
22530 Expr *pExpr;
22531
22532 assert( !p->pGroupBy );
22533
22534 if( p->pWhere || p->pEList->nExpr!=1
22535 || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
22536 ){
22537 return 0;
22538 }
22539 pTab = p->pSrc->a[0].pTab;
22540 pExpr = p->pEList->a[0].pExpr;
22541 assert( pTab && !pTab->pSelect && pExpr );
22542
22543 if( IsVirtual(pTab) ) return 0;
22544 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
22545 if( NEVER(pAggInfo->nFunc==0) ) return 0;
22546 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
22547 if( pExpr->flags&EP_Distinct ) return 0;
22548
22549 return pTab;
22550 }
22551
22552 /*
22553 ** If the source-list item passed as an argument was augmented with an
22554 ** INDEXED BY clause, then try to locate the specified index. If there
22555 ** was such a clause and the named index cannot be found, return
22556 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
22557 ** pFrom->pIndex and return SQLITE_OK.
22558 */
22559 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pF rom){
22560 if( pFrom->pTab && pFrom->fg.isIndexedBy ){
22561 Table *pTab = pFrom->pTab;
22562 char *zIndexedBy = pFrom->u1.zIndexedBy;
22563 Index *pIdx;
22564 for(pIdx=pTab->pIndex;
22565 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
22566 pIdx=pIdx->pNext
22567 );
22568 if( !pIdx ){
22569 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
22570 pParse->checkSchema = 1;
22571 return SQLITE_ERROR;
22572 }
22573 pFrom->pIBIndex = pIdx;
22574 }
22575 return SQLITE_OK;
22576 }
22577 /*
22578 ** Detect compound SELECT statements that use an ORDER BY clause with
22579 ** an alternative collating sequence.
22580 **
22581 ** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
22582 **
22583 ** These are rewritten as a subquery:
22584 **
22585 ** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
22586 ** ORDER BY ... COLLATE ...
22587 **
22588 ** This transformation is necessary because the multiSelectOrderBy() routine
22589 ** above that generates the code for a compound SELECT with an ORDER BY clause
22590 ** uses a merge algorithm that requires the same collating sequence on the
22591 ** result columns as on the ORDER BY clause. See ticket
22592 ** http://www.sqlite.org/src/info/6709574d2a
22593 **
22594 ** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
22595 ** The UNION ALL operator works fine with multiSelectOrderBy() even when
22596 ** there are COLLATE terms in the ORDER BY.
22597 */
22598 static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
22599 int i;
22600 Select *pNew;
22601 Select *pX;
22602 sqlite3 *db;
22603 struct ExprList_item *a;
22604 SrcList *pNewSrc;
22605 Parse *pParse;
22606 Token dummy;
22607
22608 if( p->pPrior==0 ) return WRC_Continue;
22609 if( p->pOrderBy==0 ) return WRC_Continue;
22610 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
22611 if( pX==0 ) return WRC_Continue;
22612 a = p->pOrderBy->a;
22613 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
22614 if( a[i].pExpr->flags & EP_Collate ) break;
22615 }
22616 if( i<0 ) return WRC_Continue;
22617
22618 /* If we reach this point, that means the transformation is required. */
22619
22620 pParse = pWalker->pParse;
22621 db = pParse->db;
22622 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
22623 if( pNew==0 ) return WRC_Abort;
22624 memset(&dummy, 0, sizeof(dummy));
22625 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
22626 if( pNewSrc==0 ) return WRC_Abort;
22627 *pNew = *p;
22628 p->pSrc = pNewSrc;
22629 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
22630 p->op = TK_SELECT;
22631 p->pWhere = 0;
22632 pNew->pGroupBy = 0;
22633 pNew->pHaving = 0;
22634 pNew->pOrderBy = 0;
22635 p->pPrior = 0;
22636 p->pNext = 0;
22637 p->pWith = 0;
22638 p->selFlags &= ~SF_Compound;
22639 assert( (p->selFlags & SF_Converted)==0 );
22640 p->selFlags |= SF_Converted;
22641 assert( pNew->pPrior!=0 );
22642 pNew->pPrior->pNext = pNew;
22643 pNew->pLimit = 0;
22644 pNew->pOffset = 0;
22645 return WRC_Continue;
22646 }
22647
22648 /*
22649 ** Check to see if the FROM clause term pFrom has table-valued function
22650 ** arguments. If it does, leave an error message in pParse and return
22651 ** non-zero, since pFrom is not allowed to be a table-valued function.
22652 */
22653 static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
22654 if( pFrom->fg.isTabFunc ){
22655 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
22656 return 1;
22657 }
22658 return 0;
22659 }
22660
22661 #ifndef SQLITE_OMIT_CTE
22662 /*
22663 ** Argument pWith (which may be NULL) points to a linked list of nested
22664 ** WITH contexts, from inner to outermost. If the table identified by
22665 ** FROM clause element pItem is really a common-table-expression (CTE)
22666 ** then return a pointer to the CTE definition for that table. Otherwise
22667 ** return NULL.
22668 **
22669 ** If a non-NULL value is returned, set *ppContext to point to the With
22670 ** object that the returned CTE belongs to.
22671 */
22672 static struct Cte *searchWith(
22673 With *pWith, /* Current innermost WITH clause */
22674 struct SrcList_item *pItem, /* FROM clause element to resolve */
22675 With **ppContext /* OUT: WITH clause return value belongs to */
22676 ){
22677 const char *zName;
22678 if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
22679 With *p;
22680 for(p=pWith; p; p=p->pOuter){
22681 int i;
22682 for(i=0; i<p->nCte; i++){
22683 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
22684 *ppContext = p;
22685 return &p->a[i];
22686 }
22687 }
22688 }
22689 }
22690 return 0;
22691 }
22692
22693 /* The code generator maintains a stack of active WITH clauses
22694 ** with the inner-most WITH clause being at the top of the stack.
22695 **
22696 ** This routine pushes the WITH clause passed as the second argument
22697 ** onto the top of the stack. If argument bFree is true, then this
22698 ** WITH clause will never be popped from the stack. In this case it
22699 ** should be freed along with the Parse object. In other cases, when
22700 ** bFree==0, the With object will be freed along with the SELECT
22701 ** statement with which it is associated.
22702 */
22703 SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
22704 assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
22705 if( pWith ){
22706 assert( pParse->pWith!=pWith );
22707 pWith->pOuter = pParse->pWith;
22708 pParse->pWith = pWith;
22709 if( bFree ) pParse->pWithToFree = pWith;
22710 }
22711 }
22712
22713 /*
22714 ** This function checks if argument pFrom refers to a CTE declared by
22715 ** a WITH clause on the stack currently maintained by the parser. And,
22716 ** if currently processing a CTE expression, if it is a recursive
22717 ** reference to the current CTE.
22718 **
22719 ** If pFrom falls into either of the two categories above, pFrom->pTab
22720 ** and other fields are populated accordingly. The caller should check
22721 ** (pFrom->pTab!=0) to determine whether or not a successful match
22722 ** was found.
22723 **
22724 ** Whether or not a match is found, SQLITE_OK is returned if no error
22725 ** occurs. If an error does occur, an error message is stored in the
22726 ** parser and some error code other than SQLITE_OK returned.
22727 */
22728 static int withExpand(
22729 Walker *pWalker,
22730 struct SrcList_item *pFrom
22731 ){
22732 Parse *pParse = pWalker->pParse;
22733 sqlite3 *db = pParse->db;
22734 struct Cte *pCte; /* Matched CTE (or NULL if no match) */
22735 With *pWith; /* WITH clause that pCte belongs to */
22736
22737 assert( pFrom->pTab==0 );
22738
22739 pCte = searchWith(pParse->pWith, pFrom, &pWith);
22740 if( pCte ){
22741 Table *pTab;
22742 ExprList *pEList;
22743 Select *pSel;
22744 Select *pLeft; /* Left-most SELECT statement */
22745 int bMayRecursive; /* True if compound joined by UNION [ALL] */
22746 With *pSavedWith; /* Initial value of pParse->pWith */
22747
22748 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
22749 ** recursive reference to CTE pCte. Leave an error in pParse and return
22750 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
22751 ** In this case, proceed. */
22752 if( pCte->zCteErr ){
22753 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
22754 return SQLITE_ERROR;
22755 }
22756 if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
22757
22758 assert( pFrom->pTab==0 );
22759 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
22760 if( pTab==0 ) return WRC_Abort;
22761 pTab->nRef = 1;
22762 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
22763 pTab->iPKey = -1;
22764 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
22765 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
22766 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
22767 if( db->mallocFailed ) return SQLITE_NOMEM;
22768 assert( pFrom->pSelect );
22769
22770 /* Check if this is a recursive CTE. */
22771 pSel = pFrom->pSelect;
22772 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
22773 if( bMayRecursive ){
22774 int i;
22775 SrcList *pSrc = pFrom->pSelect->pSrc;
22776 for(i=0; i<pSrc->nSrc; i++){
22777 struct SrcList_item *pItem = &pSrc->a[i];
22778 if( pItem->zDatabase==0
22779 && pItem->zName!=0
22780 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
22781 ){
22782 pItem->pTab = pTab;
22783 pItem->fg.isRecursive = 1;
22784 pTab->nRef++;
22785 pSel->selFlags |= SF_Recursive;
22786 }
22787 }
22788 }
22789
22790 /* Only one recursive reference is permitted. */
22791 if( pTab->nRef>2 ){
22792 sqlite3ErrorMsg(
22793 pParse, "multiple references to recursive table: %s", pCte->zName
22794 );
22795 return SQLITE_ERROR;
22796 }
22797 assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 ));
22798
22799 pCte->zCteErr = "circular reference: %s";
22800 pSavedWith = pParse->pWith;
22801 pParse->pWith = pWith;
22802 sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
22803 pParse->pWith = pWith;
22804
22805 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
22806 pEList = pLeft->pEList;
22807 if( pCte->pCols ){
22808 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
22809 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
22810 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
22811 );
22812 pParse->pWith = pSavedWith;
22813 return SQLITE_ERROR;
22814 }
22815 pEList = pCte->pCols;
22816 }
22817
22818 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
22819 if( bMayRecursive ){
22820 if( pSel->selFlags & SF_Recursive ){
22821 pCte->zCteErr = "multiple recursive references: %s";
22822 }else{
22823 pCte->zCteErr = "recursive reference in a subquery: %s";
22824 }
22825 sqlite3WalkSelect(pWalker, pSel);
22826 }
22827 pCte->zCteErr = 0;
22828 pParse->pWith = pSavedWith;
22829 }
22830
22831 return SQLITE_OK;
22832 }
22833 #endif
22834
22835 #ifndef SQLITE_OMIT_CTE
22836 /*
22837 ** If the SELECT passed as the second argument has an associated WITH
22838 ** clause, pop it from the stack stored as part of the Parse object.
22839 **
22840 ** This function is used as the xSelectCallback2() callback by
22841 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
22842 ** names and other FROM clause elements.
22843 */
22844 static void selectPopWith(Walker *pWalker, Select *p){
22845 Parse *pParse = pWalker->pParse;
22846 With *pWith = findRightmost(p)->pWith;
22847 if( pWith!=0 ){
22848 assert( pParse->pWith==pWith );
22849 pParse->pWith = pWith->pOuter;
22850 }
22851 }
22852 #else
22853 #define selectPopWith 0
22854 #endif
22855
22856 /*
22857 ** This routine is a Walker callback for "expanding" a SELECT statement.
22858 ** "Expanding" means to do the following:
22859 **
22860 ** (1) Make sure VDBE cursor numbers have been assigned to every
22861 ** element of the FROM clause.
22862 **
22863 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
22864 ** defines FROM clause. When views appear in the FROM clause,
22865 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
22866 ** that implements the view. A copy is made of the view's SELECT
22867 ** statement so that we can freely modify or delete that statement
22868 ** without worrying about messing up the persistent representation
22869 ** of the view.
22870 **
22871 ** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
22872 ** on joins and the ON and USING clause of joins.
22873 **
22874 ** (4) Scan the list of columns in the result set (pEList) looking
22875 ** for instances of the "*" operator or the TABLE.* operator.
22876 ** If found, expand each "*" to be every column in every table
22877 ** and TABLE.* to be every column in TABLE.
22878 **
22879 */
22880 static int selectExpander(Walker *pWalker, Select *p){
22881 Parse *pParse = pWalker->pParse;
22882 int i, j, k;
22883 SrcList *pTabList;
22884 ExprList *pEList;
22885 struct SrcList_item *pFrom;
22886 sqlite3 *db = pParse->db;
22887 Expr *pE, *pRight, *pExpr;
22888 u16 selFlags = p->selFlags;
22889
22890 p->selFlags |= SF_Expanded;
22891 if( db->mallocFailed ){
22892 return WRC_Abort;
22893 }
22894 if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
22895 return WRC_Prune;
22896 }
22897 pTabList = p->pSrc;
22898 pEList = p->pEList;
22899 if( pWalker->xSelectCallback2==selectPopWith ){
22900 sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
22901 }
22902
22903 /* Make sure cursor numbers have been assigned to all entries in
22904 ** the FROM clause of the SELECT statement.
22905 */
22906 sqlite3SrcListAssignCursors(pParse, pTabList);
22907
22908 /* Look up every table named in the FROM clause of the select. If
22909 ** an entry of the FROM clause is a subquery instead of a table or view,
22910 ** then create a transient table structure to describe the subquery.
22911 */
22912 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
22913 Table *pTab;
22914 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
22915 if( pFrom->fg.isRecursive ) continue;
22916 assert( pFrom->pTab==0 );
22917 #ifndef SQLITE_OMIT_CTE
22918 if( withExpand(pWalker, pFrom) ) return WRC_Abort;
22919 if( pFrom->pTab ) {} else
22920 #endif
22921 if( pFrom->zName==0 ){
22922 #ifndef SQLITE_OMIT_SUBQUERY
22923 Select *pSel = pFrom->pSelect;
22924 /* A sub-query in the FROM clause of a SELECT */
22925 assert( pSel!=0 );
22926 assert( pFrom->pTab==0 );
22927 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
22928 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
22929 if( pTab==0 ) return WRC_Abort;
22930 pTab->nRef = 1;
22931 pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
22932 while( pSel->pPrior ){ pSel = pSel->pPrior; }
22933 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
22934 pTab->iPKey = -1;
22935 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
22936 pTab->tabFlags |= TF_Ephemeral;
22937 #endif
22938 }else{
22939 /* An ordinary table or view name in the FROM clause */
22940 assert( pFrom->pTab==0 );
22941 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
22942 if( pTab==0 ) return WRC_Abort;
22943 if( pTab->nRef==0xffff ){
22944 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
22945 pTab->zName);
22946 pFrom->pTab = 0;
22947 return WRC_Abort;
22948 }
22949 pTab->nRef++;
22950 if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
22951 return WRC_Abort;
22952 }
22953 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
22954 if( IsVirtual(pTab) || pTab->pSelect ){
22955 i16 nCol;
22956 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
22957 assert( pFrom->pSelect==0 );
22958 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
22959 sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
22960 nCol = pTab->nCol;
22961 pTab->nCol = -1;
22962 sqlite3WalkSelect(pWalker, pFrom->pSelect);
22963 pTab->nCol = nCol;
22964 }
22965 #endif
22966 }
22967
22968 /* Locate the index named by the INDEXED BY clause, if any. */
22969 if( sqlite3IndexedByLookup(pParse, pFrom) ){
22970 return WRC_Abort;
22971 }
22972 }
22973
22974 /* Process NATURAL keywords, and ON and USING clauses of joins.
22975 */
22976 if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
22977 return WRC_Abort;
22978 }
22979
22980 /* For every "*" that occurs in the column list, insert the names of
22981 ** all columns in all tables. And for every TABLE.* insert the names
22982 ** of all columns in TABLE. The parser inserted a special expression
22983 ** with the TK_ASTERISK operator for each "*" that it found in the column
22984 ** list. The following code just has to locate the TK_ASTERISK
22985 ** expressions and expand each one to the list of all columns in
22986 ** all tables.
22987 **
22988 ** The first loop just checks to see if there are any "*" operators
22989 ** that need expanding.
22990 */
22991 for(k=0; k<pEList->nExpr; k++){
22992 pE = pEList->a[k].pExpr;
22993 if( pE->op==TK_ASTERISK ) break;
22994 assert( pE->op!=TK_DOT || pE->pRight!=0 );
22995 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
22996 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
22997 }
22998 if( k<pEList->nExpr ){
22999 /*
23000 ** If we get here it means the result set contains one or more "*"
23001 ** operators that need to be expanded. Loop through each expression
23002 ** in the result set and expand them one by one.
23003 */
23004 struct ExprList_item *a = pEList->a;
23005 ExprList *pNew = 0;
23006 int flags = pParse->db->flags;
23007 int longNames = (flags & SQLITE_FullColNames)!=0
23008 && (flags & SQLITE_ShortColNames)==0;
23009
23010 for(k=0; k<pEList->nExpr; k++){
23011 pE = a[k].pExpr;
23012 pRight = pE->pRight;
23013 assert( pE->op!=TK_DOT || pRight!=0 );
23014 if( pE->op!=TK_ASTERISK
23015 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
23016 ){
23017 /* This particular expression does not need to be expanded.
23018 */
23019 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
23020 if( pNew ){
23021 pNew->a[pNew->nExpr-1].zName = a[k].zName;
23022 pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
23023 a[k].zName = 0;
23024 a[k].zSpan = 0;
23025 }
23026 a[k].pExpr = 0;
23027 }else{
23028 /* This expression is a "*" or a "TABLE.*" and needs to be
23029 ** expanded. */
23030 int tableSeen = 0; /* Set to 1 when TABLE matches */
23031 char *zTName = 0; /* text of name of TABLE */
23032 if( pE->op==TK_DOT ){
23033 assert( pE->pLeft!=0 );
23034 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
23035 zTName = pE->pLeft->u.zToken;
23036 }
23037 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
23038 Table *pTab = pFrom->pTab;
23039 Select *pSub = pFrom->pSelect;
23040 char *zTabName = pFrom->zAlias;
23041 const char *zSchemaName = 0;
23042 int iDb;
23043 if( zTabName==0 ){
23044 zTabName = pTab->zName;
23045 }
23046 if( db->mallocFailed ) break;
23047 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
23048 pSub = 0;
23049 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
23050 continue;
23051 }
23052 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
23053 zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
23054 }
23055 for(j=0; j<pTab->nCol; j++){
23056 char *zName = pTab->aCol[j].zName;
23057 char *zColname; /* The computed column name */
23058 char *zToFree; /* Malloced string that needs to be freed */
23059 Token sColname; /* Computed column name as a token */
23060
23061 assert( zName );
23062 if( zTName && pSub
23063 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
23064 ){
23065 continue;
23066 }
23067
23068 /* If a column is marked as 'hidden', omit it from the expanded
23069 ** result-set list unless the SELECT has the SF_IncludeHidden
23070 ** bit set.
23071 */
23072 if( (p->selFlags & SF_IncludeHidden)==0
23073 && IsHiddenColumn(&pTab->aCol[j])
23074 ){
23075 continue;
23076 }
23077 tableSeen = 1;
23078
23079 if( i>0 && zTName==0 ){
23080 if( (pFrom->fg.jointype & JT_NATURAL)!=0
23081 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
23082 ){
23083 /* In a NATURAL join, omit the join columns from the
23084 ** table to the right of the join */
23085 continue;
23086 }
23087 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
23088 /* In a join with a USING clause, omit columns in the
23089 ** using clause from the table on the right. */
23090 continue;
23091 }
23092 }
23093 pRight = sqlite3Expr(db, TK_ID, zName);
23094 zColname = zName;
23095 zToFree = 0;
23096 if( longNames || pTabList->nSrc>1 ){
23097 Expr *pLeft;
23098 pLeft = sqlite3Expr(db, TK_ID, zTabName);
23099 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
23100 if( zSchemaName ){
23101 pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
23102 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
23103 }
23104 if( longNames ){
23105 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
23106 zToFree = zColname;
23107 }
23108 }else{
23109 pExpr = pRight;
23110 }
23111 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
23112 sColname.z = zColname;
23113 sColname.n = sqlite3Strlen30(zColname);
23114 sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
23115 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
23116 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
23117 if( pSub ){
23118 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
23119 testcase( pX->zSpan==0 );
23120 }else{
23121 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
23122 zSchemaName, zTabName, zColname);
23123 testcase( pX->zSpan==0 );
23124 }
23125 pX->bSpanIsTab = 1;
23126 }
23127 sqlite3DbFree(db, zToFree);
23128 }
23129 }
23130 if( !tableSeen ){
23131 if( zTName ){
23132 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
23133 }else{
23134 sqlite3ErrorMsg(pParse, "no tables specified");
23135 }
23136 }
23137 }
23138 }
23139 sqlite3ExprListDelete(db, pEList);
23140 p->pEList = pNew;
23141 }
23142 #if SQLITE_MAX_COLUMN
23143 if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
23144 sqlite3ErrorMsg(pParse, "too many columns in result set");
23145 return WRC_Abort;
23146 }
23147 #endif
23148 return WRC_Continue;
23149 }
23150
23151 /*
23152 ** No-op routine for the parse-tree walker.
23153 **
23154 ** When this routine is the Walker.xExprCallback then expression trees
23155 ** are walked without any actions being taken at each node. Presumably,
23156 ** when this routine is used for Walker.xExprCallback then
23157 ** Walker.xSelectCallback is set to do something useful for every
23158 ** subquery in the parser tree.
23159 */
23160 SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
23161 UNUSED_PARAMETER2(NotUsed, NotUsed2);
23162 return WRC_Continue;
23163 }
23164
23165 /*
23166 ** This routine "expands" a SELECT statement and all of its subqueries.
23167 ** For additional information on what it means to "expand" a SELECT
23168 ** statement, see the comment on the selectExpand worker callback above.
23169 **
23170 ** Expanding a SELECT statement is the first step in processing a
23171 ** SELECT statement. The SELECT statement must be expanded before
23172 ** name resolution is performed.
23173 **
23174 ** If anything goes wrong, an error message is written into pParse.
23175 ** The calling function can detect the problem by looking at pParse->nErr
23176 ** and/or pParse->db->mallocFailed.
23177 */
23178 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
23179 Walker w;
23180 memset(&w, 0, sizeof(w));
23181 w.xExprCallback = sqlite3ExprWalkNoop;
23182 w.pParse = pParse;
23183 if( pParse->hasCompound ){
23184 w.xSelectCallback = convertCompoundSelectToSubquery;
23185 sqlite3WalkSelect(&w, pSelect);
23186 }
23187 w.xSelectCallback = selectExpander;
23188 if( (pSelect->selFlags & SF_MultiValue)==0 ){
23189 w.xSelectCallback2 = selectPopWith;
23190 }
23191 sqlite3WalkSelect(&w, pSelect);
23192 }
23193
23194
23195 #ifndef SQLITE_OMIT_SUBQUERY
23196 /*
23197 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
23198 ** interface.
23199 **
23200 ** For each FROM-clause subquery, add Column.zType and Column.zColl
23201 ** information to the Table structure that represents the result set
23202 ** of that subquery.
23203 **
23204 ** The Table structure that represents the result set was constructed
23205 ** by selectExpander() but the type and collation information was omitted
23206 ** at that point because identifiers had not yet been resolved. This
23207 ** routine is called after identifier resolution.
23208 */
23209 static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
23210 Parse *pParse;
23211 int i;
23212 SrcList *pTabList;
23213 struct SrcList_item *pFrom;
23214
23215 assert( p->selFlags & SF_Resolved );
23216 assert( (p->selFlags & SF_HasTypeInfo)==0 );
23217 p->selFlags |= SF_HasTypeInfo;
23218 pParse = pWalker->pParse;
23219 pTabList = p->pSrc;
23220 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
23221 Table *pTab = pFrom->pTab;
23222 assert( pTab!=0 );
23223 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
23224 /* A sub-query in the FROM clause of a SELECT */
23225 Select *pSel = pFrom->pSelect;
23226 if( pSel ){
23227 while( pSel->pPrior ) pSel = pSel->pPrior;
23228 selectAddColumnTypeAndCollation(pParse, pTab, pSel);
23229 }
23230 }
23231 }
23232 }
23233 #endif
23234
23235
23236 /*
23237 ** This routine adds datatype and collating sequence information to
23238 ** the Table structures of all FROM-clause subqueries in a
23239 ** SELECT statement.
23240 **
23241 ** Use this routine after name resolution.
23242 */
23243 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
23244 #ifndef SQLITE_OMIT_SUBQUERY
23245 Walker w;
23246 memset(&w, 0, sizeof(w));
23247 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
23248 w.xExprCallback = sqlite3ExprWalkNoop;
23249 w.pParse = pParse;
23250 sqlite3WalkSelect(&w, pSelect);
23251 #endif
23252 }
23253
23254
23255 /*
23256 ** This routine sets up a SELECT statement for processing. The
23257 ** following is accomplished:
23258 **
23259 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
23260 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
23261 ** * ON and USING clauses are shifted into WHERE statements
23262 ** * Wildcards "*" and "TABLE.*" in result sets are expanded.
23263 ** * Identifiers in expression are matched to tables.
23264 **
23265 ** This routine acts recursively on all subqueries within the SELECT.
23266 */
23267 SQLITE_PRIVATE void sqlite3SelectPrep(
23268 Parse *pParse, /* The parser context */
23269 Select *p, /* The SELECT statement being coded. */
23270 NameContext *pOuterNC /* Name context for container */
23271 ){
23272 sqlite3 *db;
23273 if( NEVER(p==0) ) return;
23274 db = pParse->db;
23275 if( db->mallocFailed ) return;
23276 if( p->selFlags & SF_HasTypeInfo ) return;
23277 sqlite3SelectExpand(pParse, p);
23278 if( pParse->nErr || db->mallocFailed ) return;
23279 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
23280 if( pParse->nErr || db->mallocFailed ) return;
23281 sqlite3SelectAddTypeInfo(pParse, p);
23282 }
23283
23284 /*
23285 ** Reset the aggregate accumulator.
23286 **
23287 ** The aggregate accumulator is a set of memory cells that hold
23288 ** intermediate results while calculating an aggregate. This
23289 ** routine generates code that stores NULLs in all of those memory
23290 ** cells.
23291 */
23292 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
23293 Vdbe *v = pParse->pVdbe;
23294 int i;
23295 struct AggInfo_func *pFunc;
23296 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
23297 if( nReg==0 ) return;
23298 #ifdef SQLITE_DEBUG
23299 /* Verify that all AggInfo registers are within the range specified by
23300 ** AggInfo.mnReg..AggInfo.mxReg */
23301 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
23302 for(i=0; i<pAggInfo->nColumn; i++){
23303 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
23304 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
23305 }
23306 for(i=0; i<pAggInfo->nFunc; i++){
23307 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
23308 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
23309 }
23310 #endif
23311 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
23312 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
23313 if( pFunc->iDistinct>=0 ){
23314 Expr *pE = pFunc->pExpr;
23315 assert( !ExprHasProperty(pE, EP_xIsSelect) );
23316 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
23317 sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
23318 "argument");
23319 pFunc->iDistinct = -1;
23320 }else{
23321 KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0, 0);
23322 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
23323 (char*)pKeyInfo, P4_KEYINFO);
23324 }
23325 }
23326 }
23327 }
23328
23329 /*
23330 ** Invoke the OP_AggFinalize opcode for every aggregate function
23331 ** in the AggInfo structure.
23332 */
23333 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
23334 Vdbe *v = pParse->pVdbe;
23335 int i;
23336 struct AggInfo_func *pF;
23337 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
23338 ExprList *pList = pF->pExpr->x.pList;
23339 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
23340 sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
23341 (void*)pF->pFunc, P4_FUNCDEF);
23342 }
23343 }
23344
23345 /*
23346 ** Update the accumulator memory cells for an aggregate based on
23347 ** the current cursor position.
23348 */
23349 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
23350 Vdbe *v = pParse->pVdbe;
23351 int i;
23352 int regHit = 0;
23353 int addrHitTest = 0;
23354 struct AggInfo_func *pF;
23355 struct AggInfo_col *pC;
23356
23357 pAggInfo->directMode = 1;
23358 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
23359 int nArg;
23360 int addrNext = 0;
23361 int regAgg;
23362 ExprList *pList = pF->pExpr->x.pList;
23363 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
23364 if( pList ){
23365 nArg = pList->nExpr;
23366 regAgg = sqlite3GetTempRange(pParse, nArg);
23367 sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
23368 }else{
23369 nArg = 0;
23370 regAgg = 0;
23371 }
23372 if( pF->iDistinct>=0 ){
23373 addrNext = sqlite3VdbeMakeLabel(v);
23374 testcase( nArg==0 ); /* Error condition */
23375 testcase( nArg>1 ); /* Also an error */
23376 codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
23377 }
23378 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
23379 CollSeq *pColl = 0;
23380 struct ExprList_item *pItem;
23381 int j;
23382 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
23383 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
23384 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
23385 }
23386 if( !pColl ){
23387 pColl = pParse->db->pDfltColl;
23388 }
23389 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
23390 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
23391 }
23392 sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem,
23393 (void*)pF->pFunc, P4_FUNCDEF);
23394 sqlite3VdbeChangeP5(v, (u8)nArg);
23395 sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
23396 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
23397 if( addrNext ){
23398 sqlite3VdbeResolveLabel(v, addrNext);
23399 sqlite3ExprCacheClear(pParse);
23400 }
23401 }
23402
23403 /* Before populating the accumulator registers, clear the column cache.
23404 ** Otherwise, if any of the required column values are already present
23405 ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
23406 ** to pC->iMem. But by the time the value is used, the original register
23407 ** may have been used, invalidating the underlying buffer holding the
23408 ** text or blob value. See ticket [883034dcb5].
23409 **
23410 ** Another solution would be to change the OP_SCopy used to copy cached
23411 ** values to an OP_Copy.
23412 */
23413 if( regHit ){
23414 addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
23415 }
23416 sqlite3ExprCacheClear(pParse);
23417 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
23418 sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
23419 }
23420 pAggInfo->directMode = 0;
23421 sqlite3ExprCacheClear(pParse);
23422 if( addrHitTest ){
23423 sqlite3VdbeJumpHere(v, addrHitTest);
23424 }
23425 }
23426
23427 /*
23428 ** Add a single OP_Explain instruction to the VDBE to explain a simple
23429 ** count(*) query ("SELECT count(*) FROM pTab").
23430 */
23431 #ifndef SQLITE_OMIT_EXPLAIN
23432 static void explainSimpleCount(
23433 Parse *pParse, /* Parse context */
23434 Table *pTab, /* Table being queried */
23435 Index *pIdx /* Index used to optimize scan, or NULL */
23436 ){
23437 if( pParse->explain==2 ){
23438 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
23439 char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
23440 pTab->zName,
23441 bCover ? " USING COVERING INDEX " : "",
23442 bCover ? pIdx->zName : ""
23443 );
23444 sqlite3VdbeAddOp4(
23445 pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
23446 );
23447 }
23448 }
23449 #else
23450 # define explainSimpleCount(a,b,c)
23451 #endif
23452
23453 /*
23454 ** Generate code for the SELECT statement given in the p argument.
23455 **
23456 ** The results are returned according to the SelectDest structure.
23457 ** See comments in sqliteInt.h for further information.
23458 **
23459 ** This routine returns the number of errors. If any errors are
23460 ** encountered, then an appropriate error message is left in
23461 ** pParse->zErrMsg.
23462 **
23463 ** This routine does NOT free the Select structure passed in. The
23464 ** calling function needs to do that.
23465 */
23466 SQLITE_PRIVATE int sqlite3Select(
23467 Parse *pParse, /* The parser context */
23468 Select *p, /* The SELECT statement being coded. */
23469 SelectDest *pDest /* What to do with the query results */
23470 ){
23471 int i, j; /* Loop counters */
23472 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
23473 Vdbe *v; /* The virtual machine under construction */
23474 int isAgg; /* True for select lists like "count(*)" */
23475 ExprList *pEList = 0; /* List of columns to extract. */
23476 SrcList *pTabList; /* List of tables to select from */
23477 Expr *pWhere; /* The WHERE clause. May be NULL */
23478 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
23479 Expr *pHaving; /* The HAVING clause. May be NULL */
23480 int rc = 1; /* Value to return from this function */
23481 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
23482 SortCtx sSort; /* Info on how to code the ORDER BY clause */
23483 AggInfo sAggInfo; /* Information used by aggregate queries */
23484 int iEnd; /* Address of the end of the query */
23485 sqlite3 *db; /* The database connection */
23486
23487 #ifndef SQLITE_OMIT_EXPLAIN
23488 int iRestoreSelectId = pParse->iSelectId;
23489 pParse->iSelectId = pParse->iNextSelectId++;
23490 #endif
23491
23492 db = pParse->db;
23493 if( p==0 || db->mallocFailed || pParse->nErr ){
23494 return 1;
23495 }
23496 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
23497 memset(&sAggInfo, 0, sizeof(sAggInfo));
23498 #if SELECTTRACE_ENABLED
23499 pParse->nSelectIndent++;
23500 SELECTTRACE(1,pParse,p, ("begin processing:\n"));
23501 if( sqlite3SelectTrace & 0x100 ){
23502 sqlite3TreeViewSelect(0, p, 0);
23503 }
23504 #endif
23505
23506 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
23507 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
23508 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
23509 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
23510 if( IgnorableOrderby(pDest) ){
23511 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
23512 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
23513 pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||
23514 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
23515 /* If ORDER BY makes no difference in the output then neither does
23516 ** DISTINCT so it can be removed too. */
23517 sqlite3ExprListDelete(db, p->pOrderBy);
23518 p->pOrderBy = 0;
23519 p->selFlags &= ~SF_Distinct;
23520 }
23521 sqlite3SelectPrep(pParse, p, 0);
23522 memset(&sSort, 0, sizeof(sSort));
23523 sSort.pOrderBy = p->pOrderBy;
23524 pTabList = p->pSrc;
23525 if( pParse->nErr || db->mallocFailed ){
23526 goto select_end;
23527 }
23528 assert( p->pEList!=0 );
23529 isAgg = (p->selFlags & SF_Aggregate)!=0;
23530 #if SELECTTRACE_ENABLED
23531 if( sqlite3SelectTrace & 0x100 ){
23532 SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
23533 sqlite3TreeViewSelect(0, p, 0);
23534 }
23535 #endif
23536
23537
23538 /* If writing to memory or generating a set
23539 ** only a single column may be output.
23540 */
23541 #ifndef SQLITE_OMIT_SUBQUERY
23542 if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){
23543 goto select_end;
23544 }
23545 #endif
23546
23547 /* Try to flatten subqueries in the FROM clause up into the main query
23548 */
23549 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
23550 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
23551 struct SrcList_item *pItem = &pTabList->a[i];
23552 Select *pSub = pItem->pSelect;
23553 int isAggSub;
23554 Table *pTab = pItem->pTab;
23555 if( pSub==0 ) continue;
23556
23557 /* Catch mismatch in the declared columns of a view and the number of
23558 ** columns in the SELECT on the RHS */
23559 if( pTab->nCol!=pSub->pEList->nExpr ){
23560 sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
23561 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
23562 goto select_end;
23563 }
23564
23565 isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
23566 if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
23567 /* This subquery can be absorbed into its parent. */
23568 if( isAggSub ){
23569 isAgg = 1;
23570 p->selFlags |= SF_Aggregate;
23571 }
23572 i = -1;
23573 }
23574 pTabList = p->pSrc;
23575 if( db->mallocFailed ) goto select_end;
23576 if( !IgnorableOrderby(pDest) ){
23577 sSort.pOrderBy = p->pOrderBy;
23578 }
23579 }
23580 #endif
23581
23582 /* Get a pointer the VDBE under construction, allocating a new VDBE if one
23583 ** does not already exist */
23584 v = sqlite3GetVdbe(pParse);
23585 if( v==0 ) goto select_end;
23586
23587 #ifndef SQLITE_OMIT_COMPOUND_SELECT
23588 /* Handle compound SELECT statements using the separate multiSelect()
23589 ** procedure.
23590 */
23591 if( p->pPrior ){
23592 rc = multiSelect(pParse, p, pDest);
23593 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
23594 #if SELECTTRACE_ENABLED
23595 SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
23596 pParse->nSelectIndent--;
23597 #endif
23598 return rc;
23599 }
23600 #endif
23601
23602 /* Generate code for all sub-queries in the FROM clause
23603 */
23604 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
23605 for(i=0; i<pTabList->nSrc; i++){
23606 struct SrcList_item *pItem = &pTabList->a[i];
23607 SelectDest dest;
23608 Select *pSub = pItem->pSelect;
23609 if( pSub==0 ) continue;
23610
23611 /* Sometimes the code for a subquery will be generated more than
23612 ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
23613 ** for example. In that case, do not regenerate the code to manifest
23614 ** a view or the co-routine to implement a view. The first instance
23615 ** is sufficient, though the subroutine to manifest the view does need
23616 ** to be invoked again. */
23617 if( pItem->addrFillSub ){
23618 if( pItem->fg.viaCoroutine==0 ){
23619 sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
23620 }
23621 continue;
23622 }
23623
23624 /* Increment Parse.nHeight by the height of the largest expression
23625 ** tree referred to by this, the parent select. The child select
23626 ** may contain expression trees of at most
23627 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
23628 ** more conservative than necessary, but much easier than enforcing
23629 ** an exact limit.
23630 */
23631 pParse->nHeight += sqlite3SelectExprHeight(p);
23632
23633 /* Make copies of constant WHERE-clause terms in the outer query down
23634 ** inside the subquery. This can help the subquery to run more efficiently.
23635 */
23636 if( (pItem->fg.jointype & JT_OUTER)==0
23637 && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor)
23638 ){
23639 #if SELECTTRACE_ENABLED
23640 if( sqlite3SelectTrace & 0x100 ){
23641 SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
23642 sqlite3TreeViewSelect(0, p, 0);
23643 }
23644 #endif
23645 }
23646
23647 /* Generate code to implement the subquery
23648 */
23649 if( pTabList->nSrc==1
23650 && (p->selFlags & SF_All)==0
23651 && OptimizationEnabled(db, SQLITE_SubqCoroutine)
23652 ){
23653 /* Implement a co-routine that will return a single row of the result
23654 ** set on each invocation.
23655 */
23656 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
23657 pItem->regReturn = ++pParse->nMem;
23658 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
23659 VdbeComment((v, "%s", pItem->pTab->zName));
23660 pItem->addrFillSub = addrTop;
23661 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
23662 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
23663 sqlite3Select(pParse, pSub, &dest);
23664 pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
23665 pItem->fg.viaCoroutine = 1;
23666 pItem->regResult = dest.iSdst;
23667 sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
23668 sqlite3VdbeJumpHere(v, addrTop-1);
23669 sqlite3ClearTempRegCache(pParse);
23670 }else{
23671 /* Generate a subroutine that will fill an ephemeral table with
23672 ** the content of this subquery. pItem->addrFillSub will point
23673 ** to the address of the generated subroutine. pItem->regReturn
23674 ** is a register allocated to hold the subroutine return address
23675 */
23676 int topAddr;
23677 int onceAddr = 0;
23678 int retAddr;
23679 assert( pItem->addrFillSub==0 );
23680 pItem->regReturn = ++pParse->nMem;
23681 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
23682 pItem->addrFillSub = topAddr+1;
23683 if( pItem->fg.isCorrelated==0 ){
23684 /* If the subquery is not correlated and if we are not inside of
23685 ** a trigger, then we only need to compute the value of the subquery
23686 ** once. */
23687 onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
23688 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
23689 }else{
23690 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
23691 }
23692 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
23693 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
23694 sqlite3Select(pParse, pSub, &dest);
23695 pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
23696 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
23697 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
23698 VdbeComment((v, "end %s", pItem->pTab->zName));
23699 sqlite3VdbeChangeP1(v, topAddr, retAddr);
23700 sqlite3ClearTempRegCache(pParse);
23701 }
23702 if( db->mallocFailed ) goto select_end;
23703 pParse->nHeight -= sqlite3SelectExprHeight(p);
23704 }
23705 #endif
23706
23707 /* Various elements of the SELECT copied into local variables for
23708 ** convenience */
23709 pEList = p->pEList;
23710 pWhere = p->pWhere;
23711 pGroupBy = p->pGroupBy;
23712 pHaving = p->pHaving;
23713 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
23714
23715 #if SELECTTRACE_ENABLED
23716 if( sqlite3SelectTrace & 0x400 ){
23717 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
23718 sqlite3TreeViewSelect(0, p, 0);
23719 }
23720 #endif
23721
23722 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
23723 ** if the select-list is the same as the ORDER BY list, then this query
23724 ** can be rewritten as a GROUP BY. In other words, this:
23725 **
23726 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
23727 **
23728 ** is transformed to:
23729 **
23730 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
23731 **
23732 ** The second form is preferred as a single index (or temp-table) may be
23733 ** used for both the ORDER BY and DISTINCT processing. As originally
23734 ** written the query must use a temp-table for at least one of the ORDER
23735 ** BY and DISTINCT, and an index or separate temp-table for the other.
23736 */
23737 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
23738 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
23739 ){
23740 p->selFlags &= ~SF_Distinct;
23741 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
23742 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
23743 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
23744 ** original setting of the SF_Distinct flag, not the current setting */
23745 assert( sDistinct.isTnct );
23746 }
23747
23748 /* If there is an ORDER BY clause, then create an ephemeral index to
23749 ** do the sorting. But this sorting ephemeral index might end up
23750 ** being unused if the data can be extracted in pre-sorted order.
23751 ** If that is the case, then the OP_OpenEphemeral instruction will be
23752 ** changed to an OP_Noop once we figure out that the sorting index is
23753 ** not needed. The sSort.addrSortIndex variable is used to facilitate
23754 ** that change.
23755 */
23756 if( sSort.pOrderBy ){
23757 KeyInfo *pKeyInfo;
23758 pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
23759 sSort.iECursor = pParse->nTab++;
23760 sSort.addrSortIndex =
23761 sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
23762 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
23763 (char*)pKeyInfo, P4_KEYINFO
23764 );
23765 }else{
23766 sSort.addrSortIndex = -1;
23767 }
23768
23769 /* If the output is destined for a temporary table, open that table.
23770 */
23771 if( pDest->eDest==SRT_EphemTab ){
23772 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
23773 }
23774
23775 /* Set the limiter.
23776 */
23777 iEnd = sqlite3VdbeMakeLabel(v);
23778 p->nSelectRow = LARGEST_INT64;
23779 computeLimitRegisters(pParse, p, iEnd);
23780 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
23781 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
23782 sSort.sortFlags |= SORTFLAG_UseSorter;
23783 }
23784
23785 /* Open an ephemeral index to use for the distinct set.
23786 */
23787 if( p->selFlags & SF_Distinct ){
23788 sDistinct.tabTnct = pParse->nTab++;
23789 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
23790 sDistinct.tabTnct, 0, 0,
23791 (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
23792 P4_KEYINFO);
23793 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
23794 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
23795 }else{
23796 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
23797 }
23798
23799 if( !isAgg && pGroupBy==0 ){
23800 /* No aggregate functions and no GROUP BY clause */
23801 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
23802
23803 /* Begin the database scan. */
23804 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
23805 p->pEList, wctrlFlags, 0);
23806 if( pWInfo==0 ) goto select_end;
23807 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
23808 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
23809 }
23810 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
23811 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
23812 }
23813 if( sSort.pOrderBy ){
23814 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
23815 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
23816 sSort.pOrderBy = 0;
23817 }
23818 }
23819
23820 /* If sorting index that was created by a prior OP_OpenEphemeral
23821 ** instruction ended up not being needed, then change the OP_OpenEphemeral
23822 ** into an OP_Noop.
23823 */
23824 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
23825 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
23826 }
23827
23828 /* Use the standard inner loop. */
23829 selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest,
23830 sqlite3WhereContinueLabel(pWInfo),
23831 sqlite3WhereBreakLabel(pWInfo));
23832
23833 /* End the database scan loop.
23834 */
23835 sqlite3WhereEnd(pWInfo);
23836 }else{
23837 /* This case when there exist aggregate functions or a GROUP BY clause
23838 ** or both */
23839 NameContext sNC; /* Name context for processing aggregate information */
23840 int iAMem; /* First Mem address for storing current GROUP BY */
23841 int iBMem; /* First Mem address for previous GROUP BY */
23842 int iUseFlag; /* Mem address holding flag indicating that at least
23843 ** one row of the input to the aggregator has been
23844 ** processed */
23845 int iAbortFlag; /* Mem address which causes query abort if positive */
23846 int groupBySort; /* Rows come from source in GROUP BY order */
23847 int addrEnd; /* End of processing for this SELECT */
23848 int sortPTab = 0; /* Pseudotable used to decode sorting results */
23849 int sortOut = 0; /* Output register from the sorter */
23850 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
23851
23852 /* Remove any and all aliases between the result set and the
23853 ** GROUP BY clause.
23854 */
23855 if( pGroupBy ){
23856 int k; /* Loop counter */
23857 struct ExprList_item *pItem; /* For looping over expression in a list */
23858
23859 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
23860 pItem->u.x.iAlias = 0;
23861 }
23862 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
23863 pItem->u.x.iAlias = 0;
23864 }
23865 if( p->nSelectRow>100 ) p->nSelectRow = 100;
23866 }else{
23867 p->nSelectRow = 1;
23868 }
23869
23870 /* If there is both a GROUP BY and an ORDER BY clause and they are
23871 ** identical, then it may be possible to disable the ORDER BY clause
23872 ** on the grounds that the GROUP BY will cause elements to come out
23873 ** in the correct order. It also may not - the GROUP BY might use a
23874 ** database index that causes rows to be grouped together as required
23875 ** but not actually sorted. Either way, record the fact that the
23876 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
23877 ** variable. */
23878 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
23879 orderByGrp = 1;
23880 }
23881
23882 /* Create a label to jump to when we want to abort the query */
23883 addrEnd = sqlite3VdbeMakeLabel(v);
23884
23885 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
23886 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
23887 ** SELECT statement.
23888 */
23889 memset(&sNC, 0, sizeof(sNC));
23890 sNC.pParse = pParse;
23891 sNC.pSrcList = pTabList;
23892 sNC.pAggInfo = &sAggInfo;
23893 sAggInfo.mnReg = pParse->nMem+1;
23894 sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
23895 sAggInfo.pGroupBy = pGroupBy;
23896 sqlite3ExprAnalyzeAggList(&sNC, pEList);
23897 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
23898 if( pHaving ){
23899 sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
23900 }
23901 sAggInfo.nAccumulator = sAggInfo.nColumn;
23902 for(i=0; i<sAggInfo.nFunc; i++){
23903 assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
23904 sNC.ncFlags |= NC_InAggFunc;
23905 sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
23906 sNC.ncFlags &= ~NC_InAggFunc;
23907 }
23908 sAggInfo.mxReg = pParse->nMem;
23909 if( db->mallocFailed ) goto select_end;
23910
23911 /* Processing for aggregates with GROUP BY is very different and
23912 ** much more complex than aggregates without a GROUP BY.
23913 */
23914 if( pGroupBy ){
23915 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
23916 int addr1; /* A-vs-B comparision jump */
23917 int addrOutputRow; /* Start of subroutine that outputs a result row */
23918 int regOutputRow; /* Return address register for output subroutine */
23919 int addrSetAbort; /* Set the abort flag and return */
23920 int addrTopOfLoop; /* Top of the input loop */
23921 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
23922 int addrReset; /* Subroutine for resetting the accumulator */
23923 int regReset; /* Return address register for reset subroutine */
23924
23925 /* If there is a GROUP BY clause we might need a sorting index to
23926 ** implement it. Allocate that sorting index now. If it turns out
23927 ** that we do not need it after all, the OP_SorterOpen instruction
23928 ** will be converted into a Noop.
23929 */
23930 sAggInfo.sortingIdx = pParse->nTab++;
23931 pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0, sAggInfo.nColumn);
23932 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
23933 sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
23934 0, (char*)pKeyInfo, P4_KEYINFO);
23935
23936 /* Initialize memory locations used by GROUP BY aggregate processing
23937 */
23938 iUseFlag = ++pParse->nMem;
23939 iAbortFlag = ++pParse->nMem;
23940 regOutputRow = ++pParse->nMem;
23941 addrOutputRow = sqlite3VdbeMakeLabel(v);
23942 regReset = ++pParse->nMem;
23943 addrReset = sqlite3VdbeMakeLabel(v);
23944 iAMem = pParse->nMem + 1;
23945 pParse->nMem += pGroupBy->nExpr;
23946 iBMem = pParse->nMem + 1;
23947 pParse->nMem += pGroupBy->nExpr;
23948 sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
23949 VdbeComment((v, "clear abort flag"));
23950 sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
23951 VdbeComment((v, "indicate accumulator empty"));
23952 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
23953
23954 /* Begin a loop that will extract all source rows in GROUP BY order.
23955 ** This might involve two separate loops with an OP_Sort in between, or
23956 ** it might be a single loop that uses an index to extract information
23957 ** in the right order to begin with.
23958 */
23959 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
23960 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
23961 WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
23962 );
23963 if( pWInfo==0 ) goto select_end;
23964 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
23965 /* The optimizer is able to deliver rows in group by order so
23966 ** we do not have to sort. The OP_OpenEphemeral table will be
23967 ** cancelled later because we still need to use the pKeyInfo
23968 */
23969 groupBySort = 0;
23970 }else{
23971 /* Rows are coming out in undetermined order. We have to push
23972 ** each row into a sorting index, terminate the first loop,
23973 ** then loop over the sorting index in order to get the output
23974 ** in sorted order
23975 */
23976 int regBase;
23977 int regRecord;
23978 int nCol;
23979 int nGroupBy;
23980
23981 explainTempTable(pParse,
23982 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
23983 "DISTINCT" : "GROUP BY");
23984
23985 groupBySort = 1;
23986 nGroupBy = pGroupBy->nExpr;
23987 nCol = nGroupBy;
23988 j = nGroupBy;
23989 for(i=0; i<sAggInfo.nColumn; i++){
23990 if( sAggInfo.aCol[i].iSorterColumn>=j ){
23991 nCol++;
23992 j++;
23993 }
23994 }
23995 regBase = sqlite3GetTempRange(pParse, nCol);
23996 sqlite3ExprCacheClear(pParse);
23997 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
23998 j = nGroupBy;
23999 for(i=0; i<sAggInfo.nColumn; i++){
24000 struct AggInfo_col *pCol = &sAggInfo.aCol[i];
24001 if( pCol->iSorterColumn>=j ){
24002 int r1 = j + regBase;
24003 sqlite3ExprCodeGetColumnToReg(pParse,
24004 pCol->pTab, pCol->iColumn, pCol->iTable, r1);
24005 j++;
24006 }
24007 }
24008 regRecord = sqlite3GetTempReg(pParse);
24009 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
24010 sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
24011 sqlite3ReleaseTempReg(pParse, regRecord);
24012 sqlite3ReleaseTempRange(pParse, regBase, nCol);
24013 sqlite3WhereEnd(pWInfo);
24014 sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
24015 sortOut = sqlite3GetTempReg(pParse);
24016 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
24017 sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
24018 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
24019 sAggInfo.useSortingIdx = 1;
24020 sqlite3ExprCacheClear(pParse);
24021
24022 }
24023
24024 /* If the index or temporary table used by the GROUP BY sort
24025 ** will naturally deliver rows in the order required by the ORDER BY
24026 ** clause, cancel the ephemeral table open coded earlier.
24027 **
24028 ** This is an optimization - the correct answer should result regardless.
24029 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
24030 ** disable this optimization for testing purposes. */
24031 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
24032 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
24033 ){
24034 sSort.pOrderBy = 0;
24035 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
24036 }
24037
24038 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
24039 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
24040 ** Then compare the current GROUP BY terms against the GROUP BY terms
24041 ** from the previous row currently stored in a0, a1, a2...
24042 */
24043 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
24044 sqlite3ExprCacheClear(pParse);
24045 if( groupBySort ){
24046 sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
24047 sortOut, sortPTab);
24048 }
24049 for(j=0; j<pGroupBy->nExpr; j++){
24050 if( groupBySort ){
24051 sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
24052 }else{
24053 sAggInfo.directMode = 1;
24054 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
24055 }
24056 }
24057 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
24058 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
24059 addr1 = sqlite3VdbeCurrentAddr(v);
24060 sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v);
24061
24062 /* Generate code that runs whenever the GROUP BY changes.
24063 ** Changes in the GROUP BY are detected by the previous code
24064 ** block. If there were no changes, this block is skipped.
24065 **
24066 ** This code copies current group by terms in b0,b1,b2,...
24067 ** over to a0,a1,a2. It then calls the output subroutine
24068 ** and resets the aggregate accumulator registers in preparation
24069 ** for the next GROUP BY batch.
24070 */
24071 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
24072 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
24073 VdbeComment((v, "output one row"));
24074 sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
24075 VdbeComment((v, "check abort flag"));
24076 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
24077 VdbeComment((v, "reset accumulator"));
24078
24079 /* Update the aggregate accumulators based on the content of
24080 ** the current row
24081 */
24082 sqlite3VdbeJumpHere(v, addr1);
24083 updateAccumulator(pParse, &sAggInfo);
24084 sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
24085 VdbeComment((v, "indicate data in accumulator"));
24086
24087 /* End of the loop
24088 */
24089 if( groupBySort ){
24090 sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
24091 VdbeCoverage(v);
24092 }else{
24093 sqlite3WhereEnd(pWInfo);
24094 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
24095 }
24096
24097 /* Output the final row of result
24098 */
24099 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
24100 VdbeComment((v, "output final row"));
24101
24102 /* Jump over the subroutines
24103 */
24104 sqlite3VdbeGoto(v, addrEnd);
24105
24106 /* Generate a subroutine that outputs a single row of the result
24107 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
24108 ** is less than or equal to zero, the subroutine is a no-op. If
24109 ** the processing calls for the query to abort, this subroutine
24110 ** increments the iAbortFlag memory location before returning in
24111 ** order to signal the caller to abort.
24112 */
24113 addrSetAbort = sqlite3VdbeCurrentAddr(v);
24114 sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
24115 VdbeComment((v, "set abort flag"));
24116 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
24117 sqlite3VdbeResolveLabel(v, addrOutputRow);
24118 addrOutputRow = sqlite3VdbeCurrentAddr(v);
24119 sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
24120 VdbeCoverage(v);
24121 VdbeComment((v, "Groupby result generator entry point"));
24122 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
24123 finalizeAggFunctions(pParse, &sAggInfo);
24124 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
24125 selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
24126 &sDistinct, pDest,
24127 addrOutputRow+1, addrSetAbort);
24128 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
24129 VdbeComment((v, "end groupby result generator"));
24130
24131 /* Generate a subroutine that will reset the group-by accumulator
24132 */
24133 sqlite3VdbeResolveLabel(v, addrReset);
24134 resetAccumulator(pParse, &sAggInfo);
24135 sqlite3VdbeAddOp1(v, OP_Return, regReset);
24136
24137 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
24138 else {
24139 ExprList *pDel = 0;
24140 #ifndef SQLITE_OMIT_BTREECOUNT
24141 Table *pTab;
24142 if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
24143 /* If isSimpleCount() returns a pointer to a Table structure, then
24144 ** the SQL statement is of the form:
24145 **
24146 ** SELECT count(*) FROM <tbl>
24147 **
24148 ** where the Table structure returned represents table <tbl>.
24149 **
24150 ** This statement is so common that it is optimized specially. The
24151 ** OP_Count instruction is executed either on the intkey table that
24152 ** contains the data for table <tbl> or on one of its indexes. It
24153 ** is better to execute the op on an index, as indexes are almost
24154 ** always spread across less pages than their corresponding tables.
24155 */
24156 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
24157 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
24158 Index *pIdx; /* Iterator variable */
24159 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
24160 Index *pBest = 0; /* Best index found so far */
24161 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
24162
24163 sqlite3CodeVerifySchema(pParse, iDb);
24164 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
24165
24166 /* Search for the index that has the lowest scan cost.
24167 **
24168 ** (2011-04-15) Do not do a full scan of an unordered index.
24169 **
24170 ** (2013-10-03) Do not count the entries in a partial index.
24171 **
24172 ** In practice the KeyInfo structure will not be used. It is only
24173 ** passed to keep OP_OpenRead happy.
24174 */
24175 if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
24176 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
24177 if( pIdx->bUnordered==0
24178 && pIdx->szIdxRow<pTab->szTabRow
24179 && pIdx->pPartIdxWhere==0
24180 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
24181 ){
24182 pBest = pIdx;
24183 }
24184 }
24185 if( pBest ){
24186 iRoot = pBest->tnum;
24187 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
24188 }
24189
24190 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
24191 sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
24192 if( pKeyInfo ){
24193 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
24194 }
24195 sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
24196 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
24197 explainSimpleCount(pParse, pTab, pBest);
24198 }else
24199 #endif /* SQLITE_OMIT_BTREECOUNT */
24200 {
24201 /* Check if the query is of one of the following forms:
24202 **
24203 ** SELECT min(x) FROM ...
24204 ** SELECT max(x) FROM ...
24205 **
24206 ** If it is, then ask the code in where.c to attempt to sort results
24207 ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause.
24208 ** If where.c is able to produce results sorted in this order, then
24209 ** add vdbe code to break out of the processing loop after the
24210 ** first iteration (since the first iteration of the loop is
24211 ** guaranteed to operate on the row with the minimum or maximum
24212 ** value of x, the only row required).
24213 **
24214 ** A special flag must be passed to sqlite3WhereBegin() to slightly
24215 ** modify behavior as follows:
24216 **
24217 ** + If the query is a "SELECT min(x)", then the loop coded by
24218 ** where.c should not iterate over any values with a NULL value
24219 ** for x.
24220 **
24221 ** + The optimizer code in where.c (the thing that decides which
24222 ** index or indices to use) should place a different priority on
24223 ** satisfying the 'ORDER BY' clause than it does in other cases.
24224 ** Refer to code and comments in where.c for details.
24225 */
24226 ExprList *pMinMax = 0;
24227 u8 flag = WHERE_ORDERBY_NORMAL;
24228
24229 assert( p->pGroupBy==0 );
24230 assert( flag==0 );
24231 if( p->pHaving==0 ){
24232 flag = minMaxQuery(&sAggInfo, &pMinMax);
24233 }
24234 assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
24235
24236 if( flag ){
24237 pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
24238 pDel = pMinMax;
24239 if( pMinMax && !db->mallocFailed ){
24240 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
24241 pMinMax->a[0].pExpr->op = TK_COLUMN;
24242 }
24243 }
24244
24245 /* This case runs if the aggregate has no GROUP BY clause. The
24246 ** processing is much simpler since there is only a single row
24247 ** of output.
24248 */
24249 resetAccumulator(pParse, &sAggInfo);
24250 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
24251 if( pWInfo==0 ){
24252 sqlite3ExprListDelete(db, pDel);
24253 goto select_end;
24254 }
24255 updateAccumulator(pParse, &sAggInfo);
24256 assert( pMinMax==0 || pMinMax->nExpr==1 );
24257 if( sqlite3WhereIsOrdered(pWInfo)>0 ){
24258 sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
24259 VdbeComment((v, "%s() by index",
24260 (flag==WHERE_ORDERBY_MIN?"min":"max")));
24261 }
24262 sqlite3WhereEnd(pWInfo);
24263 finalizeAggFunctions(pParse, &sAggInfo);
24264 }
24265
24266 sSort.pOrderBy = 0;
24267 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
24268 selectInnerLoop(pParse, p, p->pEList, -1, 0, 0,
24269 pDest, addrEnd, addrEnd);
24270 sqlite3ExprListDelete(db, pDel);
24271 }
24272 sqlite3VdbeResolveLabel(v, addrEnd);
24273
24274 } /* endif aggregate query */
24275
24276 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
24277 explainTempTable(pParse, "DISTINCT");
24278 }
24279
24280 /* If there is an ORDER BY clause, then we need to sort the results
24281 ** and send them to the callback one by one.
24282 */
24283 if( sSort.pOrderBy ){
24284 explainTempTable(pParse,
24285 sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
24286 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
24287 }
24288
24289 /* Jump here to skip this query
24290 */
24291 sqlite3VdbeResolveLabel(v, iEnd);
24292
24293 /* The SELECT has been coded. If there is an error in the Parse structure,
24294 ** set the return code to 1. Otherwise 0. */
24295 rc = (pParse->nErr>0);
24296
24297 /* Control jumps to here if an error is encountered above, or upon
24298 ** successful coding of the SELECT.
24299 */
24300 select_end:
24301 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
24302
24303 /* Identify column names if results of the SELECT are to be output.
24304 */
24305 if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
24306 generateColumnNames(pParse, pTabList, pEList);
24307 }
24308
24309 sqlite3DbFree(db, sAggInfo.aCol);
24310 sqlite3DbFree(db, sAggInfo.aFunc);
24311 #if SELECTTRACE_ENABLED
24312 SELECTTRACE(1,pParse,p,("end processing\n"));
24313 pParse->nSelectIndent--;
24314 #endif
24315 return rc;
24316 }
24317
24318 /************** End of select.c **********************************************/
24319 /************** Begin file table.c *******************************************/
24320 /*
24321 ** 2001 September 15
24322 **
24323 ** The author disclaims copyright to this source code. In place of
24324 ** a legal notice, here is a blessing:
24325 **
24326 ** May you do good and not evil.
24327 ** May you find forgiveness for yourself and forgive others.
24328 ** May you share freely, never taking more than you give.
24329 **
24330 *************************************************************************
24331 ** This file contains the sqlite3_get_table() and sqlite3_free_table()
24332 ** interface routines. These are just wrappers around the main
24333 ** interface routine of sqlite3_exec().
24334 **
24335 ** These routines are in a separate files so that they will not be linked
24336 ** if they are not used.
24337 */
24338 /* #include "sqliteInt.h" */
24339 /* #include <stdlib.h> */
24340 /* #include <string.h> */
24341
24342 #ifndef SQLITE_OMIT_GET_TABLE
24343
24344 /*
24345 ** This structure is used to pass data from sqlite3_get_table() through
24346 ** to the callback function is uses to build the result.
24347 */
24348 typedef struct TabResult {
24349 char **azResult; /* Accumulated output */
24350 char *zErrMsg; /* Error message text, if an error occurs */
24351 u32 nAlloc; /* Slots allocated for azResult[] */
24352 u32 nRow; /* Number of rows in the result */
24353 u32 nColumn; /* Number of columns in the result */
24354 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
24355 int rc; /* Return code from sqlite3_exec() */
24356 } TabResult;
24357
24358 /*
24359 ** This routine is called once for each row in the result table. Its job
24360 ** is to fill in the TabResult structure appropriately, allocating new
24361 ** memory as necessary.
24362 */
24363 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
24364 TabResult *p = (TabResult*)pArg; /* Result accumulator */
24365 int need; /* Slots needed in p->azResult[] */
24366 int i; /* Loop counter */
24367 char *z; /* A single column of result */
24368
24369 /* Make sure there is enough space in p->azResult to hold everything
24370 ** we need to remember from this invocation of the callback.
24371 */
24372 if( p->nRow==0 && argv!=0 ){
24373 need = nCol*2;
24374 }else{
24375 need = nCol;
24376 }
24377 if( p->nData + need > p->nAlloc ){
24378 char **azNew;
24379 p->nAlloc = p->nAlloc*2 + need;
24380 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
24381 if( azNew==0 ) goto malloc_failed;
24382 p->azResult = azNew;
24383 }
24384
24385 /* If this is the first row, then generate an extra row containing
24386 ** the names of all columns.
24387 */
24388 if( p->nRow==0 ){
24389 p->nColumn = nCol;
24390 for(i=0; i<nCol; i++){
24391 z = sqlite3_mprintf("%s", colv[i]);
24392 if( z==0 ) goto malloc_failed;
24393 p->azResult[p->nData++] = z;
24394 }
24395 }else if( (int)p->nColumn!=nCol ){
24396 sqlite3_free(p->zErrMsg);
24397 p->zErrMsg = sqlite3_mprintf(
24398 "sqlite3_get_table() called with two or more incompatible queries"
24399 );
24400 p->rc = SQLITE_ERROR;
24401 return 1;
24402 }
24403
24404 /* Copy over the row data
24405 */
24406 if( argv!=0 ){
24407 for(i=0; i<nCol; i++){
24408 if( argv[i]==0 ){
24409 z = 0;
24410 }else{
24411 int n = sqlite3Strlen30(argv[i])+1;
24412 z = sqlite3_malloc64( n );
24413 if( z==0 ) goto malloc_failed;
24414 memcpy(z, argv[i], n);
24415 }
24416 p->azResult[p->nData++] = z;
24417 }
24418 p->nRow++;
24419 }
24420 return 0;
24421
24422 malloc_failed:
24423 p->rc = SQLITE_NOMEM;
24424 return 1;
24425 }
24426
24427 /*
24428 ** Query the database. But instead of invoking a callback for each row,
24429 ** malloc() for space to hold the result and return the entire results
24430 ** at the conclusion of the call.
24431 **
24432 ** The result that is written to ***pazResult is held in memory obtained
24433 ** from malloc(). But the caller cannot free this memory directly.
24434 ** Instead, the entire table should be passed to sqlite3_free_table() when
24435 ** the calling procedure is finished using it.
24436 */
24437 SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
24438 sqlite3 *db, /* The database on which the SQL executes */
24439 const char *zSql, /* The SQL to be executed */
24440 char ***pazResult, /* Write the result table here */
24441 int *pnRow, /* Write the number of rows in the result here */
24442 int *pnColumn, /* Write the number of columns of result here */
24443 char **pzErrMsg /* Write error messages here */
24444 ){
24445 int rc;
24446 TabResult res;
24447
24448 #ifdef SQLITE_ENABLE_API_ARMOR
24449 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
24450 #endif
24451 *pazResult = 0;
24452 if( pnColumn ) *pnColumn = 0;
24453 if( pnRow ) *pnRow = 0;
24454 if( pzErrMsg ) *pzErrMsg = 0;
24455 res.zErrMsg = 0;
24456 res.nRow = 0;
24457 res.nColumn = 0;
24458 res.nData = 1;
24459 res.nAlloc = 20;
24460 res.rc = SQLITE_OK;
24461 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
24462 if( res.azResult==0 ){
24463 db->errCode = SQLITE_NOMEM;
24464 return SQLITE_NOMEM;
24465 }
24466 res.azResult[0] = 0;
24467 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
24468 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
24469 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
24470 if( (rc&0xff)==SQLITE_ABORT ){
24471 sqlite3_free_table(&res.azResult[1]);
24472 if( res.zErrMsg ){
24473 if( pzErrMsg ){
24474 sqlite3_free(*pzErrMsg);
24475 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
24476 }
24477 sqlite3_free(res.zErrMsg);
24478 }
24479 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
24480 return res.rc;
24481 }
24482 sqlite3_free(res.zErrMsg);
24483 if( rc!=SQLITE_OK ){
24484 sqlite3_free_table(&res.azResult[1]);
24485 return rc;
24486 }
24487 if( res.nAlloc>res.nData ){
24488 char **azNew;
24489 azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData );
24490 if( azNew==0 ){
24491 sqlite3_free_table(&res.azResult[1]);
24492 db->errCode = SQLITE_NOMEM;
24493 return SQLITE_NOMEM;
24494 }
24495 res.azResult = azNew;
24496 }
24497 *pazResult = &res.azResult[1];
24498 if( pnColumn ) *pnColumn = res.nColumn;
24499 if( pnRow ) *pnRow = res.nRow;
24500 return rc;
24501 }
24502
24503 /*
24504 ** This routine frees the space the sqlite3_get_table() malloced.
24505 */
24506 SQLITE_API void SQLITE_STDCALL sqlite3_free_table(
24507 char **azResult /* Result returned from sqlite3_get_table() */
24508 ){
24509 if( azResult ){
24510 int i, n;
24511 azResult--;
24512 assert( azResult!=0 );
24513 n = SQLITE_PTR_TO_INT(azResult[0]);
24514 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
24515 sqlite3_free(azResult);
24516 }
24517 }
24518
24519 #endif /* SQLITE_OMIT_GET_TABLE */
24520
24521 /************** End of table.c ***********************************************/
24522
24523 /* Chain include. */
24524 #include "sqlite3.05.c"
OLDNEW
« no previous file with comments | « third_party/sqlite/amalgamation/sqlite3.03.c ('k') | third_party/sqlite/amalgamation/sqlite3.05.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698