4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains C code routines that are called by the SQLite parser
13 ** when syntax rules are reduced. The routines in this file handle the
14 ** following kinds of SQL syntax:
25 #include "sqliteInt.h"
28 ** This routine is called when a new SQL statement is beginning to
29 ** be parsed. Initialize the pParse structure as needed.
31 void sqlite3BeginParse(Parse
*pParse
, int explainFlag
){
32 pParse
->explain
= (u8
)explainFlag
;
36 #ifndef SQLITE_OMIT_SHARED_CACHE
38 ** The TableLock structure is only used by the sqlite3TableLock() and
39 ** codeTableLocks() functions.
42 int iDb
; /* The database containing the table to be locked */
43 int iTab
; /* The root page of the table to be locked */
44 u8 isWriteLock
; /* True for write lock. False for a read lock */
45 const char *zName
; /* Name of the table */
49 ** Record the fact that we want to lock a table at run-time.
51 ** The table to be locked has root page iTab and is found in database iDb.
52 ** A read or a write lock can be taken depending on isWritelock.
54 ** This routine just records the fact that the lock is desired. The
55 ** code to make the lock occur is generated by a later call to
56 ** codeTableLocks() which occurs during sqlite3FinishCoding().
58 void sqlite3TableLock(
59 Parse
*pParse
, /* Parsing context */
60 int iDb
, /* Index of the database containing the table to lock */
61 int iTab
, /* Root page number of the table to be locked */
62 u8 isWriteLock
, /* True for a write lock */
63 const char *zName
/* Name of the table to be locked */
65 Parse
*pToplevel
= sqlite3ParseToplevel(pParse
);
71 for(i
=0; i
<pToplevel
->nTableLock
; i
++){
72 p
= &pToplevel
->aTableLock
[i
];
73 if( p
->iDb
==iDb
&& p
->iTab
==iTab
){
74 p
->isWriteLock
= (p
->isWriteLock
|| isWriteLock
);
79 nBytes
= sizeof(TableLock
) * (pToplevel
->nTableLock
+1);
80 pToplevel
->aTableLock
=
81 sqlite3DbReallocOrFree(pToplevel
->db
, pToplevel
->aTableLock
, nBytes
);
82 if( pToplevel
->aTableLock
){
83 p
= &pToplevel
->aTableLock
[pToplevel
->nTableLock
++];
86 p
->isWriteLock
= isWriteLock
;
89 pToplevel
->nTableLock
= 0;
90 pToplevel
->db
->mallocFailed
= 1;
95 ** Code an OP_TableLock instruction for each table locked by the
96 ** statement (configured by calls to sqlite3TableLock()).
98 static void codeTableLocks(Parse
*pParse
){
102 pVdbe
= sqlite3GetVdbe(pParse
);
103 assert( pVdbe
!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
105 for(i
=0; i
<pParse
->nTableLock
; i
++){
106 TableLock
*p
= &pParse
->aTableLock
[i
];
108 sqlite3VdbeAddOp4(pVdbe
, OP_TableLock
, p1
, p
->iTab
, p
->isWriteLock
,
109 p
->zName
, P4_STATIC
);
113 #define codeTableLocks(x)
117 ** This routine is called after a single SQL statement has been
118 ** parsed and a VDBE program to execute that statement has been
119 ** prepared. This routine puts the finishing touches on the
120 ** VDBE program and resets the pParse structure for the next
123 ** Note that if an error occurred, it might be the case that
124 ** no VDBE code was generated.
126 void sqlite3FinishCoding(Parse
*pParse
){
131 if( db
->mallocFailed
) return;
132 if( pParse
->nested
) return;
133 if( pParse
->nErr
) return;
135 /* Begin by generating some termination code at the end of the
138 v
= sqlite3GetVdbe(pParse
);
139 assert( !pParse
->isMultiWrite
140 || sqlite3VdbeAssertMayAbort(v
, pParse
->mayAbort
));
142 sqlite3VdbeAddOp0(v
, OP_Halt
);
144 /* The cookie mask contains one bit for each database file open.
145 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
146 ** set for each database that is used. Generate code to start a
147 ** transaction on each used database and to verify the schema cookie
148 ** on each used database.
150 if( pParse
->cookieGoto
>0 ){
153 sqlite3VdbeJumpHere(v
, pParse
->cookieGoto
-1);
154 for(iDb
=0, mask
=1; iDb
<db
->nDb
; mask
<<=1, iDb
++){
155 if( (mask
& pParse
->cookieMask
)==0 ) continue;
156 sqlite3VdbeUsesBtree(v
, iDb
);
157 sqlite3VdbeAddOp2(v
,OP_Transaction
, iDb
, (mask
& pParse
->writeMask
)!=0);
158 if( db
->init
.busy
==0 ){
159 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
160 sqlite3VdbeAddOp3(v
, OP_VerifyCookie
,
161 iDb
, pParse
->cookieValue
[iDb
],
162 db
->aDb
[iDb
].pSchema
->iGeneration
);
165 #ifndef SQLITE_OMIT_VIRTUALTABLE
168 for(i
=0; i
<pParse
->nVtabLock
; i
++){
169 char *vtab
= (char *)sqlite3GetVTable(db
, pParse
->apVtabLock
[i
]);
170 sqlite3VdbeAddOp4(v
, OP_VBegin
, 0, 0, 0, vtab
, P4_VTAB
);
172 pParse
->nVtabLock
= 0;
176 /* Once all the cookies have been verified and transactions opened,
177 ** obtain the required table-locks. This is a no-op unless the
178 ** shared-cache feature is enabled.
180 codeTableLocks(pParse
);
182 /* Initialize any AUTOINCREMENT data structures required.
184 sqlite3AutoincrementBegin(pParse
);
186 /* Finally, jump back to the beginning of the executable code. */
187 sqlite3VdbeAddOp2(v
, OP_Goto
, 0, pParse
->cookieGoto
);
192 /* Get the VDBE program ready for execution
194 if( v
&& ALWAYS(pParse
->nErr
==0) && !db
->mallocFailed
){
196 FILE *trace
= (db
->flags
& SQLITE_VdbeTrace
)!=0 ? stdout
: 0;
197 sqlite3VdbeTrace(v
, trace
);
199 assert( pParse
->iCacheLevel
==0 ); /* Disables and re-enables match */
200 /* A minimum of one cursor is required if autoincrement is used
201 * See ticket [a696379c1f08866] */
202 if( pParse
->pAinc
!=0 && pParse
->nTab
==0 ) pParse
->nTab
= 1;
203 sqlite3VdbeMakeReady(v
, pParse
->nVar
, pParse
->nMem
,
204 pParse
->nTab
, pParse
->nMaxArg
, pParse
->explain
,
205 pParse
->isMultiWrite
&& pParse
->mayAbort
);
206 pParse
->rc
= SQLITE_DONE
;
207 pParse
->colNamesSet
= 0;
209 pParse
->rc
= SQLITE_ERROR
;
215 pParse
->cookieMask
= 0;
216 pParse
->cookieGoto
= 0;
220 ** Run the parser and code generator recursively in order to generate
221 ** code for the SQL statement given onto the end of the pParse context
222 ** currently under construction. When the parser is run recursively
223 ** this way, the final OP_Halt is not appended and other initialization
224 ** and finalization steps are omitted because those are handling by the
227 ** Not everything is nestable. This facility is designed to permit
228 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
229 ** care if you decide to try to use this routine for some other purposes.
231 void sqlite3NestedParse(Parse
*pParse
, const char *zFormat
, ...){
235 sqlite3
*db
= pParse
->db
;
236 # define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar))
237 char saveBuf
[SAVE_SZ
];
239 if( pParse
->nErr
) return;
240 assert( pParse
->nested
<10 ); /* Nesting should only be of limited depth */
241 va_start(ap
, zFormat
);
242 zSql
= sqlite3VMPrintf(db
, zFormat
, ap
);
245 return; /* A malloc must have failed */
248 memcpy(saveBuf
, &pParse
->nVar
, SAVE_SZ
);
249 memset(&pParse
->nVar
, 0, SAVE_SZ
);
250 sqlite3RunParser(pParse
, zSql
, &zErrMsg
);
251 sqlite3DbFree(db
, zErrMsg
);
252 sqlite3DbFree(db
, zSql
);
253 memcpy(&pParse
->nVar
, saveBuf
, SAVE_SZ
);
258 ** Locate the in-memory structure that describes a particular database
259 ** table given the name of that table and (optionally) the name of the
260 ** database containing the table. Return NULL if not found.
262 ** If zDatabase is 0, all databases are searched for the table and the
263 ** first matching table is returned. (No checking for duplicate table
264 ** names is done.) The search order is TEMP first, then MAIN, then any
265 ** auxiliary databases added using the ATTACH command.
267 ** See also sqlite3LocateTable().
269 Table
*sqlite3FindTable(sqlite3
*db
, const char *zName
, const char *zDatabase
){
274 nName
= sqlite3Strlen30(zName
);
275 /* All mutexes are required for schema access. Make sure we hold them. */
276 assert( zDatabase
!=0 || sqlite3BtreeHoldsAllMutexes(db
) );
277 for(i
=OMIT_TEMPDB
; i
<db
->nDb
; i
++){
278 int j
= (i
<2) ? i
^1 : i
; /* Search TEMP before MAIN */
279 if( zDatabase
!=0 && sqlite3StrICmp(zDatabase
, db
->aDb
[j
].zName
) ) continue;
280 assert( sqlite3SchemaMutexHeld(db
, j
, 0) );
281 p
= sqlite3HashFind(&db
->aDb
[j
].pSchema
->tblHash
, zName
, nName
);
288 ** Locate the in-memory structure that describes a particular database
289 ** table given the name of that table and (optionally) the name of the
290 ** database containing the table. Return NULL if not found. Also leave an
291 ** error message in pParse->zErrMsg.
293 ** The difference between this routine and sqlite3FindTable() is that this
294 ** routine leaves an error message in pParse->zErrMsg where
295 ** sqlite3FindTable() does not.
297 Table
*sqlite3LocateTable(
298 Parse
*pParse
, /* context in which to report errors */
299 int isView
, /* True if looking for a VIEW rather than a TABLE */
300 const char *zName
, /* Name of the table we are looking for */
301 const char *zDbase
/* Name of the database. Might be NULL */
305 /* Read the database schema. If an error occurs, leave an error message
306 ** and code in pParse and return NULL. */
307 if( SQLITE_OK
!=sqlite3ReadSchema(pParse
) ){
311 p
= sqlite3FindTable(pParse
->db
, zName
, zDbase
);
313 const char *zMsg
= isView
? "no such view" : "no such table";
315 sqlite3ErrorMsg(pParse
, "%s: %s.%s", zMsg
, zDbase
, zName
);
317 sqlite3ErrorMsg(pParse
, "%s: %s", zMsg
, zName
);
319 pParse
->checkSchema
= 1;
325 ** Locate the in-memory structure that describes
326 ** a particular index given the name of that index
327 ** and the name of the database that contains the index.
328 ** Return NULL if not found.
330 ** If zDatabase is 0, all databases are searched for the
331 ** table and the first matching index is returned. (No checking
332 ** for duplicate index names is done.) The search order is
333 ** TEMP first, then MAIN, then any auxiliary databases added
334 ** using the ATTACH command.
336 Index
*sqlite3FindIndex(sqlite3
*db
, const char *zName
, const char *zDb
){
339 int nName
= sqlite3Strlen30(zName
);
340 /* All mutexes are required for schema access. Make sure we hold them. */
341 assert( zDb
!=0 || sqlite3BtreeHoldsAllMutexes(db
) );
342 for(i
=OMIT_TEMPDB
; i
<db
->nDb
; i
++){
343 int j
= (i
<2) ? i
^1 : i
; /* Search TEMP before MAIN */
344 Schema
*pSchema
= db
->aDb
[j
].pSchema
;
346 if( zDb
&& sqlite3StrICmp(zDb
, db
->aDb
[j
].zName
) ) continue;
347 assert( sqlite3SchemaMutexHeld(db
, j
, 0) );
348 p
= sqlite3HashFind(&pSchema
->idxHash
, zName
, nName
);
355 ** Reclaim the memory used by an index
357 static void freeIndex(sqlite3
*db
, Index
*p
){
358 #ifndef SQLITE_OMIT_ANALYZE
359 sqlite3DeleteIndexSamples(db
, p
);
361 sqlite3DbFree(db
, p
->zColAff
);
362 sqlite3DbFree(db
, p
);
366 ** For the index called zIdxName which is found in the database iDb,
367 ** unlike that index from its Table then remove the index from
368 ** the index hash table and free all memory structures associated
371 void sqlite3UnlinkAndDeleteIndex(sqlite3
*db
, int iDb
, const char *zIdxName
){
376 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
377 pHash
= &db
->aDb
[iDb
].pSchema
->idxHash
;
378 len
= sqlite3Strlen30(zIdxName
);
379 pIndex
= sqlite3HashInsert(pHash
, zIdxName
, len
, 0);
380 if( ALWAYS(pIndex
) ){
381 if( pIndex
->pTable
->pIndex
==pIndex
){
382 pIndex
->pTable
->pIndex
= pIndex
->pNext
;
385 /* Justification of ALWAYS(); The index must be on the list of
387 p
= pIndex
->pTable
->pIndex
;
388 while( ALWAYS(p
) && p
->pNext
!=pIndex
){ p
= p
->pNext
; }
389 if( ALWAYS(p
&& p
->pNext
==pIndex
) ){
390 p
->pNext
= pIndex
->pNext
;
393 freeIndex(db
, pIndex
);
395 db
->flags
|= SQLITE_InternChanges
;
399 ** Erase all schema information from the in-memory hash tables of
400 ** a single database. This routine is called to reclaim memory
401 ** before the database closes. It is also called during a rollback
402 ** if there were schema changes during the transaction or if a
403 ** schema-cookie mismatch occurs.
405 ** If iDb<0 then reset the internal schema tables for all database
406 ** files. If iDb>=0 then reset the internal schema for only the
407 ** single file indicated.
409 void sqlite3ResetInternalSchema(sqlite3
*db
, int iDb
){
411 assert( iDb
<db
->nDb
);
414 /* Case 1: Reset the single schema identified by iDb */
415 Db
*pDb
= &db
->aDb
[iDb
];
416 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
417 assert( pDb
->pSchema
!=0 );
418 sqlite3SchemaClear(pDb
->pSchema
);
420 /* If any database other than TEMP is reset, then also reset TEMP
421 ** since TEMP might be holding triggers that reference tables in the
426 assert( pDb
->pSchema
!=0 );
427 sqlite3SchemaClear(pDb
->pSchema
);
431 /* Case 2 (from here to the end): Reset all schemas for all attached
434 sqlite3BtreeEnterAll(db
);
435 for(i
=0; i
<db
->nDb
; i
++){
436 Db
*pDb
= &db
->aDb
[i
];
438 sqlite3SchemaClear(pDb
->pSchema
);
441 db
->flags
&= ~SQLITE_InternChanges
;
442 sqlite3VtabUnlockList(db
);
443 sqlite3BtreeLeaveAll(db
);
445 /* If one or more of the auxiliary database files has been closed,
446 ** then remove them from the auxiliary database list. We take the
447 ** opportunity to do this here since we have just deleted all of the
448 ** schema hash tables and therefore do not have to make any changes
449 ** to any of those tables.
451 for(i
=j
=2; i
<db
->nDb
; i
++){
452 struct Db
*pDb
= &db
->aDb
[i
];
454 sqlite3DbFree(db
, pDb
->zName
);
459 db
->aDb
[j
] = db
->aDb
[i
];
463 memset(&db
->aDb
[j
], 0, (db
->nDb
-j
)*sizeof(db
->aDb
[j
]));
465 if( db
->nDb
<=2 && db
->aDb
!=db
->aDbStatic
){
466 memcpy(db
->aDbStatic
, db
->aDb
, 2*sizeof(db
->aDb
[0]));
467 sqlite3DbFree(db
, db
->aDb
);
468 db
->aDb
= db
->aDbStatic
;
473 ** This routine is called when a commit occurs.
475 void sqlite3CommitInternalChanges(sqlite3
*db
){
476 db
->flags
&= ~SQLITE_InternChanges
;
480 ** Delete memory allocated for the column names of a table or view (the
481 ** Table.aCol[] array).
483 static void sqliteDeleteColumnNames(sqlite3
*db
, Table
*pTable
){
487 if( (pCol
= pTable
->aCol
)!=0 ){
488 for(i
=0; i
<pTable
->nCol
; i
++, pCol
++){
489 sqlite3DbFree(db
, pCol
->zName
);
490 sqlite3ExprDelete(db
, pCol
->pDflt
);
491 sqlite3DbFree(db
, pCol
->zDflt
);
492 sqlite3DbFree(db
, pCol
->zType
);
493 sqlite3DbFree(db
, pCol
->zColl
);
495 sqlite3DbFree(db
, pTable
->aCol
);
500 ** Remove the memory data structures associated with the given
501 ** Table. No changes are made to disk by this routine.
503 ** This routine just deletes the data structure. It does not unlink
504 ** the table data structure from the hash table. But it does destroy
505 ** memory structures of the indices and foreign keys associated with
508 void sqlite3DeleteTable(sqlite3
*db
, Table
*pTable
){
509 Index
*pIndex
, *pNext
;
511 assert( !pTable
|| pTable
->nRef
>0 );
513 /* Do not delete the table until the reference count reaches zero. */
514 if( !pTable
) return;
515 if( ((!db
|| db
->pnBytesFreed
==0) && (--pTable
->nRef
)>0) ) return;
517 /* Delete all indices associated with this table. */
518 for(pIndex
= pTable
->pIndex
; pIndex
; pIndex
=pNext
){
519 pNext
= pIndex
->pNext
;
520 assert( pIndex
->pSchema
==pTable
->pSchema
);
521 if( !db
|| db
->pnBytesFreed
==0 ){
522 char *zName
= pIndex
->zName
;
523 TESTONLY ( Index
*pOld
= ) sqlite3HashInsert(
524 &pIndex
->pSchema
->idxHash
, zName
, sqlite3Strlen30(zName
), 0
526 assert( db
==0 || sqlite3SchemaMutexHeld(db
, 0, pIndex
->pSchema
) );
527 assert( pOld
==pIndex
|| pOld
==0 );
529 freeIndex(db
, pIndex
);
532 /* Delete any foreign keys attached to this table. */
533 sqlite3FkDelete(db
, pTable
);
535 /* Delete the Table structure itself.
537 sqliteDeleteColumnNames(db
, pTable
);
538 sqlite3DbFree(db
, pTable
->zName
);
539 sqlite3DbFree(db
, pTable
->zColAff
);
540 sqlite3SelectDelete(db
, pTable
->pSelect
);
541 #ifndef SQLITE_OMIT_CHECK
542 sqlite3ExprDelete(db
, pTable
->pCheck
);
544 #ifndef SQLITE_OMIT_VIRTUALTABLE
545 sqlite3VtabClear(db
, pTable
);
547 sqlite3DbFree(db
, pTable
);
551 ** Unlink the given table from the hash tables and the delete the
552 ** table structure with all its indices and foreign keys.
554 void sqlite3UnlinkAndDeleteTable(sqlite3
*db
, int iDb
, const char *zTabName
){
559 assert( iDb
>=0 && iDb
<db
->nDb
);
561 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
562 testcase( zTabName
[0]==0 ); /* Zero-length table names are allowed */
564 p
= sqlite3HashInsert(&pDb
->pSchema
->tblHash
, zTabName
,
565 sqlite3Strlen30(zTabName
),0);
566 sqlite3DeleteTable(db
, p
);
567 db
->flags
|= SQLITE_InternChanges
;
571 ** Given a token, return a string that consists of the text of that
572 ** token. Space to hold the returned string
573 ** is obtained from sqliteMalloc() and must be freed by the calling
576 ** Any quotation marks (ex: "name", 'name', [name], or `name`) that
577 ** surround the body of the token are removed.
579 ** Tokens are often just pointers into the original SQL text and so
580 ** are not \000 terminated and are not persistent. The returned string
581 ** is \000 terminated and is persistent.
583 char *sqlite3NameFromToken(sqlite3
*db
, Token
*pName
){
586 zName
= sqlite3DbStrNDup(db
, (char*)pName
->z
, pName
->n
);
587 sqlite3Dequote(zName
);
595 ** Open the sqlite_master table stored in database number iDb for
596 ** writing. The table is opened using cursor 0.
598 void sqlite3OpenMasterTable(Parse
*p
, int iDb
){
599 Vdbe
*v
= sqlite3GetVdbe(p
);
600 sqlite3TableLock(p
, iDb
, MASTER_ROOT
, 1, SCHEMA_TABLE(iDb
));
601 sqlite3VdbeAddOp3(v
, OP_OpenWrite
, 0, MASTER_ROOT
, iDb
);
602 sqlite3VdbeChangeP4(v
, -1, (char *)5, P4_INT32
); /* 5 column table */
609 ** Parameter zName points to a nul-terminated buffer containing the name
610 ** of a database ("main", "temp" or the name of an attached db). This
611 ** function returns the index of the named database in db->aDb[], or
612 ** -1 if the named db cannot be found.
614 int sqlite3FindDbName(sqlite3
*db
, const char *zName
){
615 int i
= -1; /* Database number */
618 int n
= sqlite3Strlen30(zName
);
619 for(i
=(db
->nDb
-1), pDb
=&db
->aDb
[i
]; i
>=0; i
--, pDb
--){
620 if( (!OMIT_TEMPDB
|| i
!=1 ) && n
==sqlite3Strlen30(pDb
->zName
) &&
621 0==sqlite3StrICmp(pDb
->zName
, zName
) ){
630 ** The token *pName contains the name of a database (either "main" or
631 ** "temp" or the name of an attached db). This routine returns the
632 ** index of the named database in db->aDb[], or -1 if the named db
635 int sqlite3FindDb(sqlite3
*db
, Token
*pName
){
636 int i
; /* Database number */
637 char *zName
; /* Name we are searching for */
638 zName
= sqlite3NameFromToken(db
, pName
);
639 i
= sqlite3FindDbName(db
, zName
);
640 sqlite3DbFree(db
, zName
);
644 /* The table or view or trigger name is passed to this routine via tokens
645 ** pName1 and pName2. If the table name was fully qualified, for example:
647 ** CREATE TABLE xxx.yyy (...);
649 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
650 ** the table name is not fully qualified, i.e.:
652 ** CREATE TABLE yyy(...);
654 ** Then pName1 is set to "yyy" and pName2 is "".
656 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
657 ** pName2) that stores the unqualified table name. The index of the
658 ** database "xxx" is returned.
660 int sqlite3TwoPartName(
661 Parse
*pParse
, /* Parsing and code generating context */
662 Token
*pName1
, /* The "xxx" in the name "xxx.yyy" or "xxx" */
663 Token
*pName2
, /* The "yyy" in the name "xxx.yyy" */
664 Token
**pUnqual
/* Write the unqualified object name here */
666 int iDb
; /* Database holding the object */
667 sqlite3
*db
= pParse
->db
;
669 if( ALWAYS(pName2
!=0) && pName2
->n
>0 ){
670 if( db
->init
.busy
) {
671 sqlite3ErrorMsg(pParse
, "corrupt database");
676 iDb
= sqlite3FindDb(db
, pName1
);
678 sqlite3ErrorMsg(pParse
, "unknown database %T", pName1
);
683 assert( db
->init
.iDb
==0 || db
->init
.busy
);
691 ** This routine is used to check if the UTF-8 string zName is a legal
692 ** unqualified name for a new schema object (table, index, view or
693 ** trigger). All names are legal except those that begin with the string
694 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
695 ** is reserved for internal use.
697 int sqlite3CheckObjectName(Parse
*pParse
, const char *zName
){
698 if( !pParse
->db
->init
.busy
&& pParse
->nested
==0
699 && (pParse
->db
->flags
& SQLITE_WriteSchema
)==0
700 && 0==sqlite3StrNICmp(zName
, "sqlite_", 7) ){
701 sqlite3ErrorMsg(pParse
, "object name reserved for internal use: %s", zName
);
708 ** Begin constructing a new table representation in memory. This is
709 ** the first of several action routines that get called in response
710 ** to a CREATE TABLE statement. In particular, this routine is called
711 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
712 ** flag is true if the table should be stored in the auxiliary database
713 ** file instead of in the main database file. This is normally the case
714 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
717 ** The new table record is initialized and put in pParse->pNewTable.
718 ** As more of the CREATE TABLE statement is parsed, additional action
719 ** routines will be called to add more information to this record.
720 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
721 ** is called to complete the construction of the new table record.
723 void sqlite3StartTable(
724 Parse
*pParse
, /* Parser context */
725 Token
*pName1
, /* First part of the name of the table or view */
726 Token
*pName2
, /* Second part of the name of the table or view */
727 int isTemp
, /* True if this is a TEMP table */
728 int isView
, /* True if this is a VIEW */
729 int isVirtual
, /* True if this is a VIRTUAL table */
730 int noErr
/* Do nothing if table already exists */
733 char *zName
= 0; /* The name of the new table */
734 sqlite3
*db
= pParse
->db
;
736 int iDb
; /* Database number to create the table in */
737 Token
*pName
; /* Unqualified name of the table to create */
739 /* The table or view name to create is passed to this routine via tokens
740 ** pName1 and pName2. If the table name was fully qualified, for example:
742 ** CREATE TABLE xxx.yyy (...);
744 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
745 ** the table name is not fully qualified, i.e.:
747 ** CREATE TABLE yyy(...);
749 ** Then pName1 is set to "yyy" and pName2 is "".
751 ** The call below sets the pName pointer to point at the token (pName1 or
752 ** pName2) that stores the unqualified table name. The variable iDb is
753 ** set to the index of the database that the table or view is to be
756 iDb
= sqlite3TwoPartName(pParse
, pName1
, pName2
, &pName
);
758 if( !OMIT_TEMPDB
&& isTemp
&& pName2
->n
>0 && iDb
!=1 ){
759 /* If creating a temp table, the name may not be qualified. Unless
760 ** the database name is "temp" anyway. */
761 sqlite3ErrorMsg(pParse
, "temporary table name must be unqualified");
764 if( !OMIT_TEMPDB
&& isTemp
) iDb
= 1;
766 pParse
->sNameToken
= *pName
;
767 zName
= sqlite3NameFromToken(db
, pName
);
768 if( zName
==0 ) return;
769 if( SQLITE_OK
!=sqlite3CheckObjectName(pParse
, zName
) ){
770 goto begin_table_error
;
772 if( db
->init
.iDb
==1 ) isTemp
= 1;
773 #ifndef SQLITE_OMIT_AUTHORIZATION
774 assert( (isTemp
& 1)==isTemp
);
777 char *zDb
= db
->aDb
[iDb
].zName
;
778 if( sqlite3AuthCheck(pParse
, SQLITE_INSERT
, SCHEMA_TABLE(isTemp
), 0, zDb
) ){
779 goto begin_table_error
;
782 if( !OMIT_TEMPDB
&& isTemp
){
783 code
= SQLITE_CREATE_TEMP_VIEW
;
785 code
= SQLITE_CREATE_VIEW
;
788 if( !OMIT_TEMPDB
&& isTemp
){
789 code
= SQLITE_CREATE_TEMP_TABLE
;
791 code
= SQLITE_CREATE_TABLE
;
794 if( !isVirtual
&& sqlite3AuthCheck(pParse
, code
, zName
, 0, zDb
) ){
795 goto begin_table_error
;
800 /* Make sure the new table name does not collide with an existing
801 ** index or table name in the same database. Issue an error message if
802 ** it does. The exception is if the statement being parsed was passed
803 ** to an sqlite3_declare_vtab() call. In that case only the column names
804 ** and types will be used, so there is no need to test for namespace
807 if( !IN_DECLARE_VTAB
){
808 char *zDb
= db
->aDb
[iDb
].zName
;
809 if( SQLITE_OK
!=sqlite3ReadSchema(pParse
) ){
810 goto begin_table_error
;
812 pTable
= sqlite3FindTable(db
, zName
, zDb
);
815 sqlite3ErrorMsg(pParse
, "table %T already exists", pName
);
817 assert( !db
->init
.busy
);
818 sqlite3CodeVerifySchema(pParse
, iDb
);
820 goto begin_table_error
;
822 if( sqlite3FindIndex(db
, zName
, zDb
)!=0 ){
823 sqlite3ErrorMsg(pParse
, "there is already an index named %s", zName
);
824 goto begin_table_error
;
828 pTable
= sqlite3DbMallocZero(db
, sizeof(Table
));
830 db
->mallocFailed
= 1;
831 pParse
->rc
= SQLITE_NOMEM
;
833 goto begin_table_error
;
835 pTable
->zName
= zName
;
837 pTable
->pSchema
= db
->aDb
[iDb
].pSchema
;
839 pTable
->nRowEst
= 1000000;
840 assert( pParse
->pNewTable
==0 );
841 pParse
->pNewTable
= pTable
;
843 /* If this is the magic sqlite_sequence table used by autoincrement,
844 ** then record a pointer to this table in the main database structure
845 ** so that INSERT can find the table easily.
847 #ifndef SQLITE_OMIT_AUTOINCREMENT
848 if( !pParse
->nested
&& strcmp(zName
, "sqlite_sequence")==0 ){
849 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
850 pTable
->pSchema
->pSeqTab
= pTable
;
854 /* Begin generating the code that will insert the table record into
855 ** the SQLITE_MASTER table. Note in particular that we must go ahead
856 ** and allocate the record number for the table entry now. Before any
857 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
858 ** indices to be created and the table record must come before the
859 ** indices. Hence, the record number for the table must be allocated
862 if( !db
->init
.busy
&& (v
= sqlite3GetVdbe(pParse
))!=0 ){
865 int reg1
, reg2
, reg3
;
866 sqlite3BeginWriteOperation(pParse
, 0, iDb
);
868 #ifndef SQLITE_OMIT_VIRTUALTABLE
870 sqlite3VdbeAddOp0(v
, OP_VBegin
);
874 /* If the file format and encoding in the database have not been set,
877 reg1
= pParse
->regRowid
= ++pParse
->nMem
;
878 reg2
= pParse
->regRoot
= ++pParse
->nMem
;
879 reg3
= ++pParse
->nMem
;
880 sqlite3VdbeAddOp3(v
, OP_ReadCookie
, iDb
, reg3
, BTREE_FILE_FORMAT
);
881 sqlite3VdbeUsesBtree(v
, iDb
);
882 j1
= sqlite3VdbeAddOp1(v
, OP_If
, reg3
);
883 fileFormat
= (db
->flags
& SQLITE_LegacyFileFmt
)!=0 ?
884 1 : SQLITE_MAX_FILE_FORMAT
;
885 sqlite3VdbeAddOp2(v
, OP_Integer
, fileFormat
, reg3
);
886 sqlite3VdbeAddOp3(v
, OP_SetCookie
, iDb
, BTREE_FILE_FORMAT
, reg3
);
887 sqlite3VdbeAddOp2(v
, OP_Integer
, ENC(db
), reg3
);
888 sqlite3VdbeAddOp3(v
, OP_SetCookie
, iDb
, BTREE_TEXT_ENCODING
, reg3
);
889 sqlite3VdbeJumpHere(v
, j1
);
891 /* This just creates a place-holder record in the sqlite_master table.
892 ** The record created does not contain anything yet. It will be replaced
893 ** by the real entry in code generated at sqlite3EndTable().
895 ** The rowid for the new entry is left in register pParse->regRowid.
896 ** The root page number of the new table is left in reg pParse->regRoot.
897 ** The rowid and root page number values are needed by the code that
898 ** sqlite3EndTable will generate.
900 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
901 if( isView
|| isVirtual
){
902 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, reg2
);
906 sqlite3VdbeAddOp2(v
, OP_CreateTable
, iDb
, reg2
);
908 sqlite3OpenMasterTable(pParse
, iDb
);
909 sqlite3VdbeAddOp2(v
, OP_NewRowid
, 0, reg1
);
910 sqlite3VdbeAddOp2(v
, OP_Null
, 0, reg3
);
911 sqlite3VdbeAddOp3(v
, OP_Insert
, 0, reg3
, reg1
);
912 sqlite3VdbeChangeP5(v
, OPFLAG_APPEND
);
913 sqlite3VdbeAddOp0(v
, OP_Close
);
916 /* Normal (non-error) return. */
919 /* If an error occurs, we jump here */
921 sqlite3DbFree(db
, zName
);
926 ** This macro is used to compare two strings in a case-insensitive manner.
927 ** It is slightly faster than calling sqlite3StrICmp() directly, but
928 ** produces larger code.
930 ** WARNING: This macro is not compatible with the strcmp() family. It
931 ** returns true if the two strings are equal, otherwise false.
933 #define STRICMP(x, y) (\
934 sqlite3UpperToLower[*(unsigned char *)(x)]== \
935 sqlite3UpperToLower[*(unsigned char *)(y)] \
936 && sqlite3StrICmp((x)+1,(y)+1)==0 )
939 ** Add a new column to the table currently being constructed.
941 ** The parser calls this routine once for each column declaration
942 ** in a CREATE TABLE statement. sqlite3StartTable() gets called
943 ** first to get things going. Then this routine is called for each
946 void sqlite3AddColumn(Parse
*pParse
, Token
*pName
){
951 sqlite3
*db
= pParse
->db
;
952 if( (p
= pParse
->pNewTable
)==0 ) return;
953 #if SQLITE_MAX_COLUMN
954 if( p
->nCol
+1>db
->aLimit
[SQLITE_LIMIT_COLUMN
] ){
955 sqlite3ErrorMsg(pParse
, "too many columns on %s", p
->zName
);
959 z
= sqlite3NameFromToken(db
, pName
);
961 for(i
=0; i
<p
->nCol
; i
++){
962 if( STRICMP(z
, p
->aCol
[i
].zName
) ){
963 sqlite3ErrorMsg(pParse
, "duplicate column name: %s", z
);
964 sqlite3DbFree(db
, z
);
968 if( (p
->nCol
& 0x7)==0 ){
970 aNew
= sqlite3DbRealloc(db
,p
->aCol
,(p
->nCol
+8)*sizeof(p
->aCol
[0]));
972 sqlite3DbFree(db
, z
);
977 pCol
= &p
->aCol
[p
->nCol
];
978 memset(pCol
, 0, sizeof(p
->aCol
[0]));
981 /* If there is no type specified, columns have the default affinity
982 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
983 ** be called next to set pCol->affinity correctly.
985 pCol
->affinity
= SQLITE_AFF_NONE
;
990 ** This routine is called by the parser while in the middle of
991 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
992 ** been seen on a column. This routine sets the notNull flag on
993 ** the column currently under construction.
995 void sqlite3AddNotNull(Parse
*pParse
, int onError
){
997 p
= pParse
->pNewTable
;
998 if( p
==0 || NEVER(p
->nCol
<1) ) return;
999 p
->aCol
[p
->nCol
-1].notNull
= (u8
)onError
;
1003 ** Scan the column type name zType (length nType) and return the
1004 ** associated affinity type.
1006 ** This routine does a case-independent search of zType for the
1007 ** substrings in the following table. If one of the substrings is
1008 ** found, the corresponding affinity is returned. If zType contains
1009 ** more than one of the substrings, entries toward the top of
1010 ** the table take priority. For example, if zType is 'BLOBINT',
1011 ** SQLITE_AFF_INTEGER is returned.
1013 ** Substring | Affinity
1014 ** --------------------------------
1015 ** 'INT' | SQLITE_AFF_INTEGER
1016 ** 'CHAR' | SQLITE_AFF_TEXT
1017 ** 'CLOB' | SQLITE_AFF_TEXT
1018 ** 'TEXT' | SQLITE_AFF_TEXT
1019 ** 'BLOB' | SQLITE_AFF_NONE
1020 ** 'REAL' | SQLITE_AFF_REAL
1021 ** 'FLOA' | SQLITE_AFF_REAL
1022 ** 'DOUB' | SQLITE_AFF_REAL
1024 ** If none of the substrings in the above table are found,
1025 ** SQLITE_AFF_NUMERIC is returned.
1027 char sqlite3AffinityType(const char *zIn
){
1029 char aff
= SQLITE_AFF_NUMERIC
;
1031 if( zIn
) while( zIn
[0] ){
1032 h
= (h
<<8) + sqlite3UpperToLower
[(*zIn
)&0xff];
1034 if( h
==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
1035 aff
= SQLITE_AFF_TEXT
;
1036 }else if( h
==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
1037 aff
= SQLITE_AFF_TEXT
;
1038 }else if( h
==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
1039 aff
= SQLITE_AFF_TEXT
;
1040 }else if( h
==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
1041 && (aff
==SQLITE_AFF_NUMERIC
|| aff
==SQLITE_AFF_REAL
) ){
1042 aff
= SQLITE_AFF_NONE
;
1043 #ifndef SQLITE_OMIT_FLOATING_POINT
1044 }else if( h
==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
1045 && aff
==SQLITE_AFF_NUMERIC
){
1046 aff
= SQLITE_AFF_REAL
;
1047 }else if( h
==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
1048 && aff
==SQLITE_AFF_NUMERIC
){
1049 aff
= SQLITE_AFF_REAL
;
1050 }else if( h
==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
1051 && aff
==SQLITE_AFF_NUMERIC
){
1052 aff
= SQLITE_AFF_REAL
;
1054 }else if( (h
&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
1055 aff
= SQLITE_AFF_INTEGER
;
1064 ** This routine is called by the parser while in the middle of
1065 ** parsing a CREATE TABLE statement. The pFirst token is the first
1066 ** token in the sequence of tokens that describe the type of the
1067 ** column currently under construction. pLast is the last token
1068 ** in the sequence. Use this information to construct a string
1069 ** that contains the typename of the column and store that string
1072 void sqlite3AddColumnType(Parse
*pParse
, Token
*pType
){
1076 p
= pParse
->pNewTable
;
1077 if( p
==0 || NEVER(p
->nCol
<1) ) return;
1078 pCol
= &p
->aCol
[p
->nCol
-1];
1079 assert( pCol
->zType
==0 );
1080 pCol
->zType
= sqlite3NameFromToken(pParse
->db
, pType
);
1081 pCol
->affinity
= sqlite3AffinityType(pCol
->zType
);
1085 ** The expression is the default value for the most recently added column
1086 ** of the table currently under construction.
1088 ** Default value expressions must be constant. Raise an exception if this
1091 ** This routine is called by the parser while in the middle of
1092 ** parsing a CREATE TABLE statement.
1094 void sqlite3AddDefaultValue(Parse
*pParse
, ExprSpan
*pSpan
){
1097 sqlite3
*db
= pParse
->db
;
1098 p
= pParse
->pNewTable
;
1100 pCol
= &(p
->aCol
[p
->nCol
-1]);
1101 if( !sqlite3ExprIsConstantOrFunction(pSpan
->pExpr
) ){
1102 sqlite3ErrorMsg(pParse
, "default value of column [%s] is not constant",
1105 /* A copy of pExpr is used instead of the original, as pExpr contains
1106 ** tokens that point to volatile memory. The 'span' of the expression
1107 ** is required by pragma table_info.
1109 sqlite3ExprDelete(db
, pCol
->pDflt
);
1110 pCol
->pDflt
= sqlite3ExprDup(db
, pSpan
->pExpr
, EXPRDUP_REDUCE
);
1111 sqlite3DbFree(db
, pCol
->zDflt
);
1112 pCol
->zDflt
= sqlite3DbStrNDup(db
, (char*)pSpan
->zStart
,
1113 (int)(pSpan
->zEnd
- pSpan
->zStart
));
1116 sqlite3ExprDelete(db
, pSpan
->pExpr
);
1120 ** Designate the PRIMARY KEY for the table. pList is a list of names
1121 ** of columns that form the primary key. If pList is NULL, then the
1122 ** most recently added column of the table is the primary key.
1124 ** A table can have at most one primary key. If the table already has
1125 ** a primary key (and this is the second primary key) then create an
1128 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
1129 ** then we will try to use that column as the rowid. Set the Table.iPKey
1130 ** field of the table under construction to be the index of the
1131 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
1132 ** no INTEGER PRIMARY KEY.
1134 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
1135 ** index for the key. No index is created for INTEGER PRIMARY KEYs.
1137 void sqlite3AddPrimaryKey(
1138 Parse
*pParse
, /* Parsing context */
1139 ExprList
*pList
, /* List of field names to be indexed */
1140 int onError
, /* What to do with a uniqueness conflict */
1141 int autoInc
, /* True if the AUTOINCREMENT keyword is present */
1142 int sortOrder
/* SQLITE_SO_ASC or SQLITE_SO_DESC */
1144 Table
*pTab
= pParse
->pNewTable
;
1147 if( pTab
==0 || IN_DECLARE_VTAB
) goto primary_key_exit
;
1148 if( pTab
->tabFlags
& TF_HasPrimaryKey
){
1149 sqlite3ErrorMsg(pParse
,
1150 "table \"%s\" has more than one primary key", pTab
->zName
);
1151 goto primary_key_exit
;
1153 pTab
->tabFlags
|= TF_HasPrimaryKey
;
1155 iCol
= pTab
->nCol
- 1;
1156 pTab
->aCol
[iCol
].isPrimKey
= 1;
1158 for(i
=0; i
<pList
->nExpr
; i
++){
1159 for(iCol
=0; iCol
<pTab
->nCol
; iCol
++){
1160 if( sqlite3StrICmp(pList
->a
[i
].zName
, pTab
->aCol
[iCol
].zName
)==0 ){
1164 if( iCol
<pTab
->nCol
){
1165 pTab
->aCol
[iCol
].isPrimKey
= 1;
1168 if( pList
->nExpr
>1 ) iCol
= -1;
1170 if( iCol
>=0 && iCol
<pTab
->nCol
){
1171 zType
= pTab
->aCol
[iCol
].zType
;
1173 if( zType
&& sqlite3StrICmp(zType
, "INTEGER")==0
1174 && sortOrder
==SQLITE_SO_ASC
){
1176 pTab
->keyConf
= (u8
)onError
;
1177 assert( autoInc
==0 || autoInc
==1 );
1178 pTab
->tabFlags
|= autoInc
*TF_Autoincrement
;
1179 }else if( autoInc
){
1180 #ifndef SQLITE_OMIT_AUTOINCREMENT
1181 sqlite3ErrorMsg(pParse
, "AUTOINCREMENT is only allowed on an "
1182 "INTEGER PRIMARY KEY");
1186 p
= sqlite3CreateIndex(pParse
, 0, 0, 0, pList
, onError
, 0, 0, sortOrder
, 0);
1194 sqlite3ExprListDelete(pParse
->db
, pList
);
1199 ** Add a new CHECK constraint to the table currently under construction.
1201 void sqlite3AddCheckConstraint(
1202 Parse
*pParse
, /* Parsing context */
1203 Expr
*pCheckExpr
/* The check expression */
1205 sqlite3
*db
= pParse
->db
;
1206 #ifndef SQLITE_OMIT_CHECK
1207 Table
*pTab
= pParse
->pNewTable
;
1208 if( pTab
&& !IN_DECLARE_VTAB
){
1209 pTab
->pCheck
= sqlite3ExprAnd(db
, pTab
->pCheck
, pCheckExpr
);
1213 sqlite3ExprDelete(db
, pCheckExpr
);
1218 ** Set the collation function of the most recently parsed table column
1219 ** to the CollSeq given.
1221 void sqlite3AddCollateType(Parse
*pParse
, Token
*pToken
){
1224 char *zColl
; /* Dequoted name of collation sequence */
1227 if( (p
= pParse
->pNewTable
)==0 ) return;
1230 zColl
= sqlite3NameFromToken(db
, pToken
);
1231 if( !zColl
) return;
1233 if( sqlite3LocateCollSeq(pParse
, zColl
) ){
1235 p
->aCol
[i
].zColl
= zColl
;
1237 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
1238 ** then an index may have been created on this column before the
1239 ** collation type was added. Correct this if it is the case.
1241 for(pIdx
=p
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
1242 assert( pIdx
->nColumn
==1 );
1243 if( pIdx
->aiColumn
[0]==i
){
1244 pIdx
->azColl
[0] = p
->aCol
[i
].zColl
;
1248 sqlite3DbFree(db
, zColl
);
1253 ** This function returns the collation sequence for database native text
1254 ** encoding identified by the string zName, length nName.
1256 ** If the requested collation sequence is not available, or not available
1257 ** in the database native encoding, the collation factory is invoked to
1258 ** request it. If the collation factory does not supply such a sequence,
1259 ** and the sequence is available in another text encoding, then that is
1260 ** returned instead.
1262 ** If no versions of the requested collations sequence are available, or
1263 ** another error occurs, NULL is returned and an error message written into
1266 ** This routine is a wrapper around sqlite3FindCollSeq(). This routine
1267 ** invokes the collation factory if the named collation cannot be found
1268 ** and generates an error message.
1270 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
1272 CollSeq
*sqlite3LocateCollSeq(Parse
*pParse
, const char *zName
){
1273 sqlite3
*db
= pParse
->db
;
1275 u8 initbusy
= db
->init
.busy
;
1278 pColl
= sqlite3FindCollSeq(db
, enc
, zName
, initbusy
);
1279 if( !initbusy
&& (!pColl
|| !pColl
->xCmp
) ){
1280 pColl
= sqlite3GetCollSeq(db
, enc
, pColl
, zName
);
1282 sqlite3ErrorMsg(pParse
, "no such collation sequence: %s", zName
);
1291 ** Generate code that will increment the schema cookie.
1293 ** The schema cookie is used to determine when the schema for the
1294 ** database changes. After each schema change, the cookie value
1295 ** changes. When a process first reads the schema it records the
1296 ** cookie. Thereafter, whenever it goes to access the database,
1297 ** it checks the cookie to make sure the schema has not changed
1298 ** since it was last read.
1300 ** This plan is not completely bullet-proof. It is possible for
1301 ** the schema to change multiple times and for the cookie to be
1302 ** set back to prior value. But schema changes are infrequent
1303 ** and the probability of hitting the same cookie value is only
1304 ** 1 chance in 2^32. So we're safe enough.
1306 void sqlite3ChangeCookie(Parse
*pParse
, int iDb
){
1307 int r1
= sqlite3GetTempReg(pParse
);
1308 sqlite3
*db
= pParse
->db
;
1309 Vdbe
*v
= pParse
->pVdbe
;
1310 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
1311 sqlite3VdbeAddOp2(v
, OP_Integer
, db
->aDb
[iDb
].pSchema
->schema_cookie
+1, r1
);
1312 sqlite3VdbeAddOp3(v
, OP_SetCookie
, iDb
, BTREE_SCHEMA_VERSION
, r1
);
1313 sqlite3ReleaseTempReg(pParse
, r1
);
1317 ** Measure the number of characters needed to output the given
1318 ** identifier. The number returned includes any quotes used
1319 ** but does not include the null terminator.
1321 ** The estimate is conservative. It might be larger that what is
1324 static int identLength(const char *z
){
1326 for(n
=0; *z
; n
++, z
++){
1327 if( *z
=='"' ){ n
++; }
1333 ** The first parameter is a pointer to an output buffer. The second
1334 ** parameter is a pointer to an integer that contains the offset at
1335 ** which to write into the output buffer. This function copies the
1336 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
1337 ** to the specified offset in the buffer and updates *pIdx to refer
1338 ** to the first byte after the last byte written before returning.
1340 ** If the string zSignedIdent consists entirely of alpha-numeric
1341 ** characters, does not begin with a digit and is not an SQL keyword,
1342 ** then it is copied to the output buffer exactly as it is. Otherwise,
1343 ** it is quoted using double-quotes.
1345 static void identPut(char *z
, int *pIdx
, char *zSignedIdent
){
1346 unsigned char *zIdent
= (unsigned char*)zSignedIdent
;
1347 int i
, j
, needQuote
;
1350 for(j
=0; zIdent
[j
]; j
++){
1351 if( !sqlite3Isalnum(zIdent
[j
]) && zIdent
[j
]!='_' ) break;
1353 needQuote
= sqlite3Isdigit(zIdent
[0]) || sqlite3KeywordCode(zIdent
, j
)!=TK_ID
;
1355 needQuote
= zIdent
[j
];
1358 if( needQuote
) z
[i
++] = '"';
1359 for(j
=0; zIdent
[j
]; j
++){
1361 if( zIdent
[j
]=='"' ) z
[i
++] = '"';
1363 if( needQuote
) z
[i
++] = '"';
1369 ** Generate a CREATE TABLE statement appropriate for the given
1370 ** table. Memory to hold the text of the statement is obtained
1371 ** from sqliteMalloc() and must be freed by the calling function.
1373 static char *createTableStmt(sqlite3
*db
, Table
*p
){
1376 char *zSep
, *zSep2
, *zEnd
;
1379 for(pCol
= p
->aCol
, i
=0; i
<p
->nCol
; i
++, pCol
++){
1380 n
+= identLength(pCol
->zName
) + 5;
1382 n
+= identLength(p
->zName
);
1392 n
+= 35 + 6*p
->nCol
;
1393 zStmt
= sqlite3DbMallocRaw(0, n
);
1395 db
->mallocFailed
= 1;
1398 sqlite3_snprintf(n
, zStmt
, "CREATE TABLE ");
1399 k
= sqlite3Strlen30(zStmt
);
1400 identPut(zStmt
, &k
, p
->zName
);
1402 for(pCol
=p
->aCol
, i
=0; i
<p
->nCol
; i
++, pCol
++){
1403 static const char * const azType
[] = {
1404 /* SQLITE_AFF_TEXT */ " TEXT",
1405 /* SQLITE_AFF_NONE */ "",
1406 /* SQLITE_AFF_NUMERIC */ " NUM",
1407 /* SQLITE_AFF_INTEGER */ " INT",
1408 /* SQLITE_AFF_REAL */ " REAL"
1413 sqlite3_snprintf(n
-k
, &zStmt
[k
], zSep
);
1414 k
+= sqlite3Strlen30(&zStmt
[k
]);
1416 identPut(zStmt
, &k
, pCol
->zName
);
1417 assert( pCol
->affinity
-SQLITE_AFF_TEXT
>= 0 );
1418 assert( pCol
->affinity
-SQLITE_AFF_TEXT
< ArraySize(azType
) );
1419 testcase( pCol
->affinity
==SQLITE_AFF_TEXT
);
1420 testcase( pCol
->affinity
==SQLITE_AFF_NONE
);
1421 testcase( pCol
->affinity
==SQLITE_AFF_NUMERIC
);
1422 testcase( pCol
->affinity
==SQLITE_AFF_INTEGER
);
1423 testcase( pCol
->affinity
==SQLITE_AFF_REAL
);
1425 zType
= azType
[pCol
->affinity
- SQLITE_AFF_TEXT
];
1426 len
= sqlite3Strlen30(zType
);
1427 assert( pCol
->affinity
==SQLITE_AFF_NONE
1428 || pCol
->affinity
==sqlite3AffinityType(zType
) );
1429 memcpy(&zStmt
[k
], zType
, len
);
1433 sqlite3_snprintf(n
-k
, &zStmt
[k
], "%s", zEnd
);
1438 ** This routine is called to report the final ")" that terminates
1439 ** a CREATE TABLE statement.
1441 ** The table structure that other action routines have been building
1442 ** is added to the internal hash tables, assuming no errors have
1445 ** An entry for the table is made in the master table on disk, unless
1446 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
1447 ** it means we are reading the sqlite_master table because we just
1448 ** connected to the database or because the sqlite_master table has
1449 ** recently changed, so the entry for this table already exists in
1450 ** the sqlite_master table. We do not want to create it again.
1452 ** If the pSelect argument is not NULL, it means that this routine
1453 ** was called to create a table generated from a
1454 ** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1455 ** the new table will match the result set of the SELECT.
1457 void sqlite3EndTable(
1458 Parse
*pParse
, /* Parse context */
1459 Token
*pCons
, /* The ',' token after the last column defn. */
1460 Token
*pEnd
, /* The final ')' token in the CREATE TABLE */
1461 Select
*pSelect
/* Select from a "CREATE ... AS SELECT" */
1464 sqlite3
*db
= pParse
->db
;
1467 if( (pEnd
==0 && pSelect
==0) || db
->mallocFailed
){
1470 p
= pParse
->pNewTable
;
1473 assert( !db
->init
.busy
|| !pSelect
);
1475 iDb
= sqlite3SchemaToIndex(db
, p
->pSchema
);
1477 #ifndef SQLITE_OMIT_CHECK
1478 /* Resolve names in all CHECK constraint expressions.
1481 SrcList sSrc
; /* Fake SrcList for pParse->pNewTable */
1482 NameContext sNC
; /* Name context for pParse->pNewTable */
1484 memset(&sNC
, 0, sizeof(sNC
));
1485 memset(&sSrc
, 0, sizeof(sSrc
));
1487 sSrc
.a
[0].zName
= p
->zName
;
1489 sSrc
.a
[0].iCursor
= -1;
1490 sNC
.pParse
= pParse
;
1491 sNC
.pSrcList
= &sSrc
;
1493 if( sqlite3ResolveExprNames(&sNC
, p
->pCheck
) ){
1497 #endif /* !defined(SQLITE_OMIT_CHECK) */
1499 /* If the db->init.busy is 1 it means we are reading the SQL off the
1500 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1501 ** So do not write to the disk again. Extract the root page number
1502 ** for the table from the db->init.newTnum field. (The page number
1503 ** should have been put there by the sqliteOpenCb routine.)
1505 if( db
->init
.busy
){
1506 p
->tnum
= db
->init
.newTnum
;
1509 /* If not initializing, then create a record for the new table
1510 ** in the SQLITE_MASTER table of the database.
1512 ** If this is a TEMPORARY table, write the entry into the auxiliary
1513 ** file instead of into the main database file.
1515 if( !db
->init
.busy
){
1518 char *zType
; /* "view" or "table" */
1519 char *zType2
; /* "VIEW" or "TABLE" */
1520 char *zStmt
; /* Text of the CREATE TABLE or CREATE VIEW statement */
1522 v
= sqlite3GetVdbe(pParse
);
1523 if( NEVER(v
==0) ) return;
1525 sqlite3VdbeAddOp1(v
, OP_Close
, 0);
1528 ** Initialize zType for the new view or table.
1530 if( p
->pSelect
==0 ){
1531 /* A regular table */
1534 #ifndef SQLITE_OMIT_VIEW
1542 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1543 ** statement to populate the new table. The root-page number for the
1544 ** new table is in register pParse->regRoot.
1546 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1547 ** suitable state to query for the column names and types to be used
1548 ** by the new table.
1550 ** A shared-cache write-lock is not required to write to the new table,
1551 ** as a schema-lock must have already been obtained to create it. Since
1552 ** a schema-lock excludes all other database users, the write-lock would
1559 assert(pParse
->nTab
==1);
1560 sqlite3VdbeAddOp3(v
, OP_OpenWrite
, 1, pParse
->regRoot
, iDb
);
1561 sqlite3VdbeChangeP5(v
, 1);
1563 sqlite3SelectDestInit(&dest
, SRT_Table
, 1);
1564 sqlite3Select(pParse
, pSelect
, &dest
);
1565 sqlite3VdbeAddOp1(v
, OP_Close
, 1);
1566 if( pParse
->nErr
==0 ){
1567 pSelTab
= sqlite3ResultSetOfSelect(pParse
, pSelect
);
1568 if( pSelTab
==0 ) return;
1569 assert( p
->aCol
==0 );
1570 p
->nCol
= pSelTab
->nCol
;
1571 p
->aCol
= pSelTab
->aCol
;
1574 sqlite3DeleteTable(db
, pSelTab
);
1578 /* Compute the complete text of the CREATE statement */
1580 zStmt
= createTableStmt(db
, p
);
1582 n
= (int)(pEnd
->z
- pParse
->sNameToken
.z
) + 1;
1583 zStmt
= sqlite3MPrintf(db
,
1584 "CREATE %s %.*s", zType2
, n
, pParse
->sNameToken
.z
1588 /* A slot for the record has already been allocated in the
1589 ** SQLITE_MASTER table. We just need to update that slot with all
1590 ** the information we've collected.
1592 sqlite3NestedParse(pParse
,
1594 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
1596 db
->aDb
[iDb
].zName
, SCHEMA_TABLE(iDb
),
1604 sqlite3DbFree(db
, zStmt
);
1605 sqlite3ChangeCookie(pParse
, iDb
);
1607 #ifndef SQLITE_OMIT_AUTOINCREMENT
1608 /* Check to see if we need to create an sqlite_sequence table for
1609 ** keeping track of autoincrement keys.
1611 if( p
->tabFlags
& TF_Autoincrement
){
1612 Db
*pDb
= &db
->aDb
[iDb
];
1613 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
1614 if( pDb
->pSchema
->pSeqTab
==0 ){
1615 sqlite3NestedParse(pParse
,
1616 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
1623 /* Reparse everything to update our internal data structures */
1624 sqlite3VdbeAddOp4(v
, OP_ParseSchema
, iDb
, 0, 0,
1625 sqlite3MPrintf(db
, "tbl_name='%q'",p
->zName
), P4_DYNAMIC
);
1629 /* Add the table to the in-memory representation of the database.
1631 if( db
->init
.busy
){
1633 Schema
*pSchema
= p
->pSchema
;
1634 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
1635 pOld
= sqlite3HashInsert(&pSchema
->tblHash
, p
->zName
,
1636 sqlite3Strlen30(p
->zName
),p
);
1638 assert( p
==pOld
); /* Malloc must have failed inside HashInsert() */
1639 db
->mallocFailed
= 1;
1642 pParse
->pNewTable
= 0;
1644 db
->flags
|= SQLITE_InternChanges
;
1646 #ifndef SQLITE_OMIT_ALTERTABLE
1648 const char *zName
= (const char *)pParse
->sNameToken
.z
;
1650 assert( !pSelect
&& pCons
&& pEnd
);
1654 nName
= (int)((const char *)pCons
->z
- zName
);
1655 p
->addColOffset
= 13 + sqlite3Utf8CharLen(zName
, nName
);
1661 #ifndef SQLITE_OMIT_VIEW
1663 ** The parser calls this routine in order to create a new VIEW
1665 void sqlite3CreateView(
1666 Parse
*pParse
, /* The parsing context */
1667 Token
*pBegin
, /* The CREATE token that begins the statement */
1668 Token
*pName1
, /* The token that holds the name of the view */
1669 Token
*pName2
, /* The token that holds the name of the view */
1670 Select
*pSelect
, /* A SELECT statement that will become the new view */
1671 int isTemp
, /* TRUE for a TEMPORARY view */
1672 int noErr
/* Suppress error messages if VIEW already exists */
1681 sqlite3
*db
= pParse
->db
;
1683 if( pParse
->nVar
>0 ){
1684 sqlite3ErrorMsg(pParse
, "parameters are not allowed in views");
1685 sqlite3SelectDelete(db
, pSelect
);
1688 sqlite3StartTable(pParse
, pName1
, pName2
, isTemp
, 1, 0, noErr
);
1689 p
= pParse
->pNewTable
;
1690 if( p
==0 || pParse
->nErr
){
1691 sqlite3SelectDelete(db
, pSelect
);
1694 sqlite3TwoPartName(pParse
, pName1
, pName2
, &pName
);
1695 iDb
= sqlite3SchemaToIndex(db
, p
->pSchema
);
1696 if( sqlite3FixInit(&sFix
, pParse
, iDb
, "view", pName
)
1697 && sqlite3FixSelect(&sFix
, pSelect
)
1699 sqlite3SelectDelete(db
, pSelect
);
1703 /* Make a copy of the entire SELECT statement that defines the view.
1704 ** This will force all the Expr.token.z values to be dynamically
1705 ** allocated rather than point to the input string - which means that
1706 ** they will persist after the current sqlite3_exec() call returns.
1708 p
->pSelect
= sqlite3SelectDup(db
, pSelect
, EXPRDUP_REDUCE
);
1709 sqlite3SelectDelete(db
, pSelect
);
1710 if( db
->mallocFailed
){
1713 if( !db
->init
.busy
){
1714 sqlite3ViewGetColumnNames(pParse
, p
);
1717 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1720 sEnd
= pParse
->sLastToken
;
1721 if( ALWAYS(sEnd
.z
[0]!=0) && sEnd
.z
[0]!=';' ){
1725 n
= (int)(sEnd
.z
- pBegin
->z
);
1727 while( ALWAYS(n
>0) && sqlite3Isspace(z
[n
-1]) ){ n
--; }
1731 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1732 sqlite3EndTable(pParse
, 0, &sEnd
, 0);
1735 #endif /* SQLITE_OMIT_VIEW */
1737 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
1739 ** The Table structure pTable is really a VIEW. Fill in the names of
1740 ** the columns of the view in the pTable structure. Return the number
1741 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
1743 int sqlite3ViewGetColumnNames(Parse
*pParse
, Table
*pTable
){
1744 Table
*pSelTab
; /* A fake table from which we get the result set */
1745 Select
*pSel
; /* Copy of the SELECT that implements the view */
1746 int nErr
= 0; /* Number of errors encountered */
1747 int n
; /* Temporarily holds the number of cursors assigned */
1748 sqlite3
*db
= pParse
->db
; /* Database connection for malloc errors */
1749 int (*xAuth
)(void*,int,const char*,const char*,const char*,const char*);
1753 #ifndef SQLITE_OMIT_VIRTUALTABLE
1754 if( sqlite3VtabCallConnect(pParse
, pTable
) ){
1755 return SQLITE_ERROR
;
1757 if( IsVirtual(pTable
) ) return 0;
1760 #ifndef SQLITE_OMIT_VIEW
1761 /* A positive nCol means the columns names for this view are
1764 if( pTable
->nCol
>0 ) return 0;
1766 /* A negative nCol is a special marker meaning that we are currently
1767 ** trying to compute the column names. If we enter this routine with
1768 ** a negative nCol, it means two or more views form a loop, like this:
1770 ** CREATE VIEW one AS SELECT * FROM two;
1771 ** CREATE VIEW two AS SELECT * FROM one;
1773 ** Actually, the error above is now caught prior to reaching this point.
1774 ** But the following test is still important as it does come up
1775 ** in the following:
1777 ** CREATE TABLE main.ex1(a);
1778 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
1779 ** SELECT * FROM temp.ex1;
1781 if( pTable
->nCol
<0 ){
1782 sqlite3ErrorMsg(pParse
, "view %s is circularly defined", pTable
->zName
);
1785 assert( pTable
->nCol
>=0 );
1787 /* If we get this far, it means we need to compute the table names.
1788 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
1789 ** "*" elements in the results set of the view and will assign cursors
1790 ** to the elements of the FROM clause. But we do not want these changes
1791 ** to be permanent. So the computation is done on a copy of the SELECT
1792 ** statement that defines the view.
1794 assert( pTable
->pSelect
);
1795 pSel
= sqlite3SelectDup(db
, pTable
->pSelect
, 0);
1797 u8 enableLookaside
= db
->lookaside
.bEnabled
;
1799 sqlite3SrcListAssignCursors(pParse
, pSel
->pSrc
);
1801 db
->lookaside
.bEnabled
= 0;
1802 #ifndef SQLITE_OMIT_AUTHORIZATION
1805 pSelTab
= sqlite3ResultSetOfSelect(pParse
, pSel
);
1808 pSelTab
= sqlite3ResultSetOfSelect(pParse
, pSel
);
1810 db
->lookaside
.bEnabled
= enableLookaside
;
1813 assert( pTable
->aCol
==0 );
1814 pTable
->nCol
= pSelTab
->nCol
;
1815 pTable
->aCol
= pSelTab
->aCol
;
1818 sqlite3DeleteTable(db
, pSelTab
);
1819 assert( sqlite3SchemaMutexHeld(db
, 0, pTable
->pSchema
) );
1820 pTable
->pSchema
->flags
|= DB_UnresetViews
;
1825 sqlite3SelectDelete(db
, pSel
);
1829 #endif /* SQLITE_OMIT_VIEW */
1832 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
1834 #ifndef SQLITE_OMIT_VIEW
1836 ** Clear the column names from every VIEW in database idx.
1838 static void sqliteViewResetAll(sqlite3
*db
, int idx
){
1840 assert( sqlite3SchemaMutexHeld(db
, idx
, 0) );
1841 if( !DbHasProperty(db
, idx
, DB_UnresetViews
) ) return;
1842 for(i
=sqliteHashFirst(&db
->aDb
[idx
].pSchema
->tblHash
); i
;i
=sqliteHashNext(i
)){
1843 Table
*pTab
= sqliteHashData(i
);
1844 if( pTab
->pSelect
){
1845 sqliteDeleteColumnNames(db
, pTab
);
1850 DbClearProperty(db
, idx
, DB_UnresetViews
);
1853 # define sqliteViewResetAll(A,B)
1854 #endif /* SQLITE_OMIT_VIEW */
1857 ** This function is called by the VDBE to adjust the internal schema
1858 ** used by SQLite when the btree layer moves a table root page. The
1859 ** root-page of a table or index in database iDb has changed from iFrom
1862 ** Ticket #1728: The symbol table might still contain information
1863 ** on tables and/or indices that are the process of being deleted.
1864 ** If you are unlucky, one of those deleted indices or tables might
1865 ** have the same rootpage number as the real table or index that is
1866 ** being moved. So we cannot stop searching after the first match
1867 ** because the first match might be for one of the deleted indices
1868 ** or tables and not the table/index that is actually being moved.
1869 ** We must continue looping until all tables and indices with
1870 ** rootpage==iFrom have been converted to have a rootpage of iTo
1871 ** in order to be certain that we got the right one.
1873 #ifndef SQLITE_OMIT_AUTOVACUUM
1874 void sqlite3RootPageMoved(sqlite3
*db
, int iDb
, int iFrom
, int iTo
){
1879 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
1880 pDb
= &db
->aDb
[iDb
];
1881 pHash
= &pDb
->pSchema
->tblHash
;
1882 for(pElem
=sqliteHashFirst(pHash
); pElem
; pElem
=sqliteHashNext(pElem
)){
1883 Table
*pTab
= sqliteHashData(pElem
);
1884 if( pTab
->tnum
==iFrom
){
1888 pHash
= &pDb
->pSchema
->idxHash
;
1889 for(pElem
=sqliteHashFirst(pHash
); pElem
; pElem
=sqliteHashNext(pElem
)){
1890 Index
*pIdx
= sqliteHashData(pElem
);
1891 if( pIdx
->tnum
==iFrom
){
1899 ** Write code to erase the table with root-page iTable from database iDb.
1900 ** Also write code to modify the sqlite_master table and internal schema
1901 ** if a root-page of another table is moved by the btree-layer whilst
1902 ** erasing iTable (this can happen with an auto-vacuum database).
1904 static void destroyRootPage(Parse
*pParse
, int iTable
, int iDb
){
1905 Vdbe
*v
= sqlite3GetVdbe(pParse
);
1906 int r1
= sqlite3GetTempReg(pParse
);
1907 sqlite3VdbeAddOp3(v
, OP_Destroy
, iTable
, r1
, iDb
);
1908 sqlite3MayAbort(pParse
);
1909 #ifndef SQLITE_OMIT_AUTOVACUUM
1910 /* OP_Destroy stores an in integer r1. If this integer
1911 ** is non-zero, then it is the root page number of a table moved to
1912 ** location iTable. The following code modifies the sqlite_master table to
1915 ** The "#NNN" in the SQL is a special constant that means whatever value
1916 ** is in register NNN. See grammar rules associated with the TK_REGISTER
1917 ** token for additional information.
1919 sqlite3NestedParse(pParse
,
1920 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
1921 pParse
->db
->aDb
[iDb
].zName
, SCHEMA_TABLE(iDb
), iTable
, r1
, r1
);
1923 sqlite3ReleaseTempReg(pParse
, r1
);
1927 ** Write VDBE code to erase table pTab and all associated indices on disk.
1928 ** Code to update the sqlite_master tables and internal schema definitions
1929 ** in case a root-page belonging to another table is moved by the btree layer
1930 ** is also added (this can happen with an auto-vacuum database).
1932 static void destroyTable(Parse
*pParse
, Table
*pTab
){
1933 #ifdef SQLITE_OMIT_AUTOVACUUM
1935 int iDb
= sqlite3SchemaToIndex(pParse
->db
, pTab
->pSchema
);
1936 destroyRootPage(pParse
, pTab
->tnum
, iDb
);
1937 for(pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
1938 destroyRootPage(pParse
, pIdx
->tnum
, iDb
);
1941 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
1942 ** is not defined), then it is important to call OP_Destroy on the
1943 ** table and index root-pages in order, starting with the numerically
1944 ** largest root-page number. This guarantees that none of the root-pages
1945 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
1946 ** following were coded:
1952 ** and root page 5 happened to be the largest root-page number in the
1953 ** database, then root page 5 would be moved to page 4 by the
1954 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
1955 ** a free-list page.
1957 int iTab
= pTab
->tnum
;
1964 if( iDestroyed
==0 || iTab
<iDestroyed
){
1967 for(pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
1968 int iIdx
= pIdx
->tnum
;
1969 assert( pIdx
->pSchema
==pTab
->pSchema
);
1970 if( (iDestroyed
==0 || (iIdx
<iDestroyed
)) && iIdx
>iLargest
){
1977 int iDb
= sqlite3SchemaToIndex(pParse
->db
, pTab
->pSchema
);
1978 destroyRootPage(pParse
, iLargest
, iDb
);
1979 iDestroyed
= iLargest
;
1986 ** This routine is called to do the work of a DROP TABLE statement.
1987 ** pName is the name of the table to be dropped.
1989 void sqlite3DropTable(Parse
*pParse
, SrcList
*pName
, int isView
, int noErr
){
1992 sqlite3
*db
= pParse
->db
;
1995 if( db
->mallocFailed
){
1996 goto exit_drop_table
;
1998 assert( pParse
->nErr
==0 );
1999 assert( pName
->nSrc
==1 );
2000 if( noErr
) db
->suppressErr
++;
2001 pTab
= sqlite3LocateTable(pParse
, isView
,
2002 pName
->a
[0].zName
, pName
->a
[0].zDatabase
);
2003 if( noErr
) db
->suppressErr
--;
2006 if( noErr
) sqlite3CodeVerifyNamedSchema(pParse
, pName
->a
[0].zDatabase
);
2007 goto exit_drop_table
;
2009 iDb
= sqlite3SchemaToIndex(db
, pTab
->pSchema
);
2010 assert( iDb
>=0 && iDb
<db
->nDb
);
2012 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
2013 ** it is initialized.
2015 if( IsVirtual(pTab
) && sqlite3ViewGetColumnNames(pParse
, pTab
) ){
2016 goto exit_drop_table
;
2018 #ifndef SQLITE_OMIT_AUTHORIZATION
2021 const char *zTab
= SCHEMA_TABLE(iDb
);
2022 const char *zDb
= db
->aDb
[iDb
].zName
;
2023 const char *zArg2
= 0;
2024 if( sqlite3AuthCheck(pParse
, SQLITE_DELETE
, zTab
, 0, zDb
)){
2025 goto exit_drop_table
;
2028 if( !OMIT_TEMPDB
&& iDb
==1 ){
2029 code
= SQLITE_DROP_TEMP_VIEW
;
2031 code
= SQLITE_DROP_VIEW
;
2033 #ifndef SQLITE_OMIT_VIRTUALTABLE
2034 }else if( IsVirtual(pTab
) ){
2035 code
= SQLITE_DROP_VTABLE
;
2036 zArg2
= sqlite3GetVTable(db
, pTab
)->pMod
->zName
;
2039 if( !OMIT_TEMPDB
&& iDb
==1 ){
2040 code
= SQLITE_DROP_TEMP_TABLE
;
2042 code
= SQLITE_DROP_TABLE
;
2045 if( sqlite3AuthCheck(pParse
, code
, pTab
->zName
, zArg2
, zDb
) ){
2046 goto exit_drop_table
;
2048 if( sqlite3AuthCheck(pParse
, SQLITE_DELETE
, pTab
->zName
, 0, zDb
) ){
2049 goto exit_drop_table
;
2053 if( sqlite3StrNICmp(pTab
->zName
, "sqlite_", 7)==0 ){
2054 sqlite3ErrorMsg(pParse
, "table %s may not be dropped", pTab
->zName
);
2055 goto exit_drop_table
;
2058 #ifndef SQLITE_OMIT_VIEW
2059 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
2062 if( isView
&& pTab
->pSelect
==0 ){
2063 sqlite3ErrorMsg(pParse
, "use DROP TABLE to delete table %s", pTab
->zName
);
2064 goto exit_drop_table
;
2066 if( !isView
&& pTab
->pSelect
){
2067 sqlite3ErrorMsg(pParse
, "use DROP VIEW to delete view %s", pTab
->zName
);
2068 goto exit_drop_table
;
2072 /* Generate code to remove the table from the master table
2075 v
= sqlite3GetVdbe(pParse
);
2078 Db
*pDb
= &db
->aDb
[iDb
];
2079 sqlite3BeginWriteOperation(pParse
, 1, iDb
);
2081 #ifndef SQLITE_OMIT_VIRTUALTABLE
2082 if( IsVirtual(pTab
) ){
2083 sqlite3VdbeAddOp0(v
, OP_VBegin
);
2086 sqlite3FkDropTable(pParse
, pName
, pTab
);
2088 /* Drop all triggers associated with the table being dropped. Code
2089 ** is generated to remove entries from sqlite_master and/or
2090 ** sqlite_temp_master if required.
2092 pTrigger
= sqlite3TriggerList(pParse
, pTab
);
2094 assert( pTrigger
->pSchema
==pTab
->pSchema
||
2095 pTrigger
->pSchema
==db
->aDb
[1].pSchema
);
2096 sqlite3DropTriggerPtr(pParse
, pTrigger
);
2097 pTrigger
= pTrigger
->pNext
;
2100 #ifndef SQLITE_OMIT_AUTOINCREMENT
2101 /* Remove any entries of the sqlite_sequence table associated with
2102 ** the table being dropped. This is done before the table is dropped
2103 ** at the btree level, in case the sqlite_sequence table needs to
2104 ** move as a result of the drop (can happen in auto-vacuum mode).
2106 if( pTab
->tabFlags
& TF_Autoincrement
){
2107 sqlite3NestedParse(pParse
,
2108 "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
2109 pDb
->zName
, pTab
->zName
2114 /* Drop all SQLITE_MASTER table and index entries that refer to the
2115 ** table. The program name loops through the master table and deletes
2116 ** every row that refers to a table of the same name as the one being
2117 ** dropped. Triggers are handled seperately because a trigger can be
2118 ** created in the temp database that refers to a table in another
2121 sqlite3NestedParse(pParse
,
2122 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
2123 pDb
->zName
, SCHEMA_TABLE(iDb
), pTab
->zName
);
2125 /* Drop any statistics from the sqlite_stat1 table, if it exists */
2126 if( sqlite3FindTable(db
, "sqlite_stat1", db
->aDb
[iDb
].zName
) ){
2127 sqlite3NestedParse(pParse
,
2128 "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", pDb
->zName
, pTab
->zName
2132 if( !isView
&& !IsVirtual(pTab
) ){
2133 destroyTable(pParse
, pTab
);
2136 /* Remove the table entry from SQLite's internal schema and modify
2137 ** the schema cookie.
2139 if( IsVirtual(pTab
) ){
2140 sqlite3VdbeAddOp4(v
, OP_VDestroy
, iDb
, 0, 0, pTab
->zName
, 0);
2142 sqlite3VdbeAddOp4(v
, OP_DropTable
, iDb
, 0, 0, pTab
->zName
, 0);
2143 sqlite3ChangeCookie(pParse
, iDb
);
2145 sqliteViewResetAll(db
, iDb
);
2148 sqlite3SrcListDelete(db
, pName
);
2152 ** This routine is called to create a new foreign key on the table
2153 ** currently under construction. pFromCol determines which columns
2154 ** in the current table point to the foreign key. If pFromCol==0 then
2155 ** connect the key to the last column inserted. pTo is the name of
2156 ** the table referred to. pToCol is a list of tables in the other
2157 ** pTo table that the foreign key points to. flags contains all
2158 ** information about the conflict resolution algorithms specified
2159 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
2161 ** An FKey structure is created and added to the table currently
2162 ** under construction in the pParse->pNewTable field.
2164 ** The foreign key is set for IMMEDIATE processing. A subsequent call
2165 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
2167 void sqlite3CreateForeignKey(
2168 Parse
*pParse
, /* Parsing context */
2169 ExprList
*pFromCol
, /* Columns in this table that point to other table */
2170 Token
*pTo
, /* Name of the other table */
2171 ExprList
*pToCol
, /* Columns in the other table */
2172 int flags
/* Conflict resolution algorithms. */
2174 sqlite3
*db
= pParse
->db
;
2175 #ifndef SQLITE_OMIT_FOREIGN_KEY
2178 Table
*p
= pParse
->pNewTable
;
2185 if( p
==0 || IN_DECLARE_VTAB
) goto fk_end
;
2187 int iCol
= p
->nCol
-1;
2188 if( NEVER(iCol
<0) ) goto fk_end
;
2189 if( pToCol
&& pToCol
->nExpr
!=1 ){
2190 sqlite3ErrorMsg(pParse
, "foreign key on %s"
2191 " should reference only one column of table %T",
2192 p
->aCol
[iCol
].zName
, pTo
);
2196 }else if( pToCol
&& pToCol
->nExpr
!=pFromCol
->nExpr
){
2197 sqlite3ErrorMsg(pParse
,
2198 "number of columns in foreign key does not match the number of "
2199 "columns in the referenced table");
2202 nCol
= pFromCol
->nExpr
;
2204 nByte
= sizeof(*pFKey
) + (nCol
-1)*sizeof(pFKey
->aCol
[0]) + pTo
->n
+ 1;
2206 for(i
=0; i
<pToCol
->nExpr
; i
++){
2207 nByte
+= sqlite3Strlen30(pToCol
->a
[i
].zName
) + 1;
2210 pFKey
= sqlite3DbMallocZero(db
, nByte
);
2215 pFKey
->pNextFrom
= p
->pFKey
;
2216 z
= (char*)&pFKey
->aCol
[nCol
];
2218 memcpy(z
, pTo
->z
, pTo
->n
);
2224 pFKey
->aCol
[0].iFrom
= p
->nCol
-1;
2226 for(i
=0; i
<nCol
; i
++){
2228 for(j
=0; j
<p
->nCol
; j
++){
2229 if( sqlite3StrICmp(p
->aCol
[j
].zName
, pFromCol
->a
[i
].zName
)==0 ){
2230 pFKey
->aCol
[i
].iFrom
= j
;
2235 sqlite3ErrorMsg(pParse
,
2236 "unknown column \"%s\" in foreign key definition",
2237 pFromCol
->a
[i
].zName
);
2243 for(i
=0; i
<nCol
; i
++){
2244 int n
= sqlite3Strlen30(pToCol
->a
[i
].zName
);
2245 pFKey
->aCol
[i
].zCol
= z
;
2246 memcpy(z
, pToCol
->a
[i
].zName
, n
);
2251 pFKey
->isDeferred
= 0;
2252 pFKey
->aAction
[0] = (u8
)(flags
& 0xff); /* ON DELETE action */
2253 pFKey
->aAction
[1] = (u8
)((flags
>> 8 ) & 0xff); /* ON UPDATE action */
2255 assert( sqlite3SchemaMutexHeld(db
, 0, p
->pSchema
) );
2256 pNextTo
= (FKey
*)sqlite3HashInsert(&p
->pSchema
->fkeyHash
,
2257 pFKey
->zTo
, sqlite3Strlen30(pFKey
->zTo
), (void *)pFKey
2259 if( pNextTo
==pFKey
){
2260 db
->mallocFailed
= 1;
2264 assert( pNextTo
->pPrevTo
==0 );
2265 pFKey
->pNextTo
= pNextTo
;
2266 pNextTo
->pPrevTo
= pFKey
;
2269 /* Link the foreign key to the table as the last step.
2275 sqlite3DbFree(db
, pFKey
);
2276 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
2277 sqlite3ExprListDelete(db
, pFromCol
);
2278 sqlite3ExprListDelete(db
, pToCol
);
2282 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
2283 ** clause is seen as part of a foreign key definition. The isDeferred
2284 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
2285 ** The behavior of the most recently created foreign key is adjusted
2288 void sqlite3DeferForeignKey(Parse
*pParse
, int isDeferred
){
2289 #ifndef SQLITE_OMIT_FOREIGN_KEY
2292 if( (pTab
= pParse
->pNewTable
)==0 || (pFKey
= pTab
->pFKey
)==0 ) return;
2293 assert( isDeferred
==0 || isDeferred
==1 ); /* EV: R-30323-21917 */
2294 pFKey
->isDeferred
= (u8
)isDeferred
;
2299 ** Generate code that will erase and refill index *pIdx. This is
2300 ** used to initialize a newly created index or to recompute the
2301 ** content of an index in response to a REINDEX command.
2303 ** if memRootPage is not negative, it means that the index is newly
2304 ** created. The register specified by memRootPage contains the
2305 ** root page number of the index. If memRootPage is negative, then
2306 ** the index already exists and must be cleared before being refilled and
2307 ** the root page number of the index is taken from pIndex->tnum.
2309 static void sqlite3RefillIndex(Parse
*pParse
, Index
*pIndex
, int memRootPage
){
2310 Table
*pTab
= pIndex
->pTable
; /* The table that is indexed */
2311 int iTab
= pParse
->nTab
++; /* Btree cursor used for pTab */
2312 int iIdx
= pParse
->nTab
++; /* Btree cursor used for pIndex */
2313 int addr1
; /* Address of top of loop */
2314 int tnum
; /* Root page of index */
2315 Vdbe
*v
; /* Generate code into this virtual machine */
2316 KeyInfo
*pKey
; /* KeyInfo for index */
2317 int regIdxKey
; /* Registers containing the index key */
2318 int regRecord
; /* Register holding assemblied index record */
2319 sqlite3
*db
= pParse
->db
; /* The database connection */
2320 int iDb
= sqlite3SchemaToIndex(db
, pIndex
->pSchema
);
2322 #ifndef SQLITE_OMIT_AUTHORIZATION
2323 if( sqlite3AuthCheck(pParse
, SQLITE_REINDEX
, pIndex
->zName
, 0,
2324 db
->aDb
[iDb
].zName
) ){
2329 /* Require a write-lock on the table to perform this operation */
2330 sqlite3TableLock(pParse
, iDb
, pTab
->tnum
, 1, pTab
->zName
);
2332 v
= sqlite3GetVdbe(pParse
);
2334 if( memRootPage
>=0 ){
2337 tnum
= pIndex
->tnum
;
2338 sqlite3VdbeAddOp2(v
, OP_Clear
, tnum
, iDb
);
2340 pKey
= sqlite3IndexKeyinfo(pParse
, pIndex
);
2341 sqlite3VdbeAddOp4(v
, OP_OpenWrite
, iIdx
, tnum
, iDb
,
2342 (char *)pKey
, P4_KEYINFO_HANDOFF
);
2343 if( memRootPage
>=0 ){
2344 sqlite3VdbeChangeP5(v
, 1);
2346 sqlite3OpenTable(pParse
, iTab
, iDb
, pTab
, OP_OpenRead
);
2347 addr1
= sqlite3VdbeAddOp2(v
, OP_Rewind
, iTab
, 0);
2348 regRecord
= sqlite3GetTempReg(pParse
);
2349 regIdxKey
= sqlite3GenerateIndexKey(pParse
, pIndex
, iTab
, regRecord
, 1);
2350 if( pIndex
->onError
!=OE_None
){
2351 const int regRowid
= regIdxKey
+ pIndex
->nColumn
;
2352 const int j2
= sqlite3VdbeCurrentAddr(v
) + 2;
2353 void * const pRegKey
= SQLITE_INT_TO_PTR(regIdxKey
);
2355 /* The registers accessed by the OP_IsUnique opcode were allocated
2356 ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
2357 ** call above. Just before that function was freed they were released
2358 ** (made available to the compiler for reuse) using
2359 ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
2360 ** opcode use the values stored within seems dangerous. However, since
2361 ** we can be sure that no other temp registers have been allocated
2362 ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
2364 sqlite3VdbeAddOp4(v
, OP_IsUnique
, iIdx
, j2
, regRowid
, pRegKey
, P4_INT32
);
2365 sqlite3HaltConstraint(
2366 pParse
, OE_Abort
, "indexed columns are not unique", P4_STATIC
);
2368 sqlite3VdbeAddOp2(v
, OP_IdxInsert
, iIdx
, regRecord
);
2369 sqlite3VdbeChangeP5(v
, OPFLAG_USESEEKRESULT
);
2370 sqlite3ReleaseTempReg(pParse
, regRecord
);
2371 sqlite3VdbeAddOp2(v
, OP_Next
, iTab
, addr1
+1);
2372 sqlite3VdbeJumpHere(v
, addr1
);
2373 sqlite3VdbeAddOp1(v
, OP_Close
, iTab
);
2374 sqlite3VdbeAddOp1(v
, OP_Close
, iIdx
);
2378 ** Create a new index for an SQL table. pName1.pName2 is the name of the index
2379 ** and pTblList is the name of the table that is to be indexed. Both will
2380 ** be NULL for a primary key or an index that is created to satisfy a
2381 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
2382 ** as the table to be indexed. pParse->pNewTable is a table that is
2383 ** currently being constructed by a CREATE TABLE statement.
2385 ** pList is a list of columns to be indexed. pList will be NULL if this
2386 ** is a primary key or unique-constraint on the most recent column added
2387 ** to the table currently under construction.
2389 ** If the index is created successfully, return a pointer to the new Index
2390 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
2391 ** as the tables primary key (Index.autoIndex==2).
2393 Index
*sqlite3CreateIndex(
2394 Parse
*pParse
, /* All information about this parse */
2395 Token
*pName1
, /* First part of index name. May be NULL */
2396 Token
*pName2
, /* Second part of index name. May be NULL */
2397 SrcList
*pTblName
, /* Table to index. Use pParse->pNewTable if 0 */
2398 ExprList
*pList
, /* A list of columns to be indexed */
2399 int onError
, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
2400 Token
*pStart
, /* The CREATE token that begins this statement */
2401 Token
*pEnd
, /* The ")" that closes the CREATE INDEX statement */
2402 int sortOrder
, /* Sort order of primary key when pList==NULL */
2403 int ifNotExist
/* Omit error if index already exists */
2405 Index
*pRet
= 0; /* Pointer to return */
2406 Table
*pTab
= 0; /* Table to be indexed */
2407 Index
*pIndex
= 0; /* The index to be created */
2408 char *zName
= 0; /* Name of the index */
2409 int nName
; /* Number of characters in zName */
2411 Token nullId
; /* Fake token for an empty ID list */
2412 DbFixer sFix
; /* For assigning database names to pTable */
2413 int sortOrderMask
; /* 1 to honor DESC in index. 0 to ignore. */
2414 sqlite3
*db
= pParse
->db
;
2415 Db
*pDb
; /* The specific table containing the indexed database */
2416 int iDb
; /* Index of the database that is being written */
2417 Token
*pName
= 0; /* Unqualified name of the index to create */
2418 struct ExprList_item
*pListItem
; /* For looping over pList */
2423 assert( pStart
==0 || pEnd
!=0 ); /* pEnd must be non-NULL if pStart is */
2424 assert( pParse
->nErr
==0 ); /* Never called with prior errors */
2425 if( db
->mallocFailed
|| IN_DECLARE_VTAB
){
2426 goto exit_create_index
;
2428 if( SQLITE_OK
!=sqlite3ReadSchema(pParse
) ){
2429 goto exit_create_index
;
2433 ** Find the table that is to be indexed. Return early if not found.
2437 /* Use the two-part index name to determine the database
2438 ** to search for the table. 'Fix' the table name to this db
2439 ** before looking up the table.
2441 assert( pName1
&& pName2
);
2442 iDb
= sqlite3TwoPartName(pParse
, pName1
, pName2
, &pName
);
2443 if( iDb
<0 ) goto exit_create_index
;
2445 #ifndef SQLITE_OMIT_TEMPDB
2446 /* If the index name was unqualified, check if the the table
2447 ** is a temp table. If so, set the database to 1. Do not do this
2448 ** if initialising a database schema.
2450 if( !db
->init
.busy
){
2451 pTab
= sqlite3SrcListLookup(pParse
, pTblName
);
2452 if( pName2
->n
==0 && pTab
&& pTab
->pSchema
==db
->aDb
[1].pSchema
){
2458 if( sqlite3FixInit(&sFix
, pParse
, iDb
, "index", pName
) &&
2459 sqlite3FixSrcList(&sFix
, pTblName
)
2461 /* Because the parser constructs pTblName from a single identifier,
2462 ** sqlite3FixSrcList can never fail. */
2465 pTab
= sqlite3LocateTable(pParse
, 0, pTblName
->a
[0].zName
,
2466 pTblName
->a
[0].zDatabase
);
2467 if( !pTab
|| db
->mallocFailed
) goto exit_create_index
;
2468 assert( db
->aDb
[iDb
].pSchema
==pTab
->pSchema
);
2471 pTab
= pParse
->pNewTable
;
2472 if( !pTab
) goto exit_create_index
;
2473 iDb
= sqlite3SchemaToIndex(db
, pTab
->pSchema
);
2475 pDb
= &db
->aDb
[iDb
];
2478 assert( pParse
->nErr
==0 );
2479 if( sqlite3StrNICmp(pTab
->zName
, "sqlite_", 7)==0
2480 && memcmp(&pTab
->zName
[7],"altertab_",9)!=0 ){
2481 sqlite3ErrorMsg(pParse
, "table %s may not be indexed", pTab
->zName
);
2482 goto exit_create_index
;
2484 #ifndef SQLITE_OMIT_VIEW
2485 if( pTab
->pSelect
){
2486 sqlite3ErrorMsg(pParse
, "views may not be indexed");
2487 goto exit_create_index
;
2490 #ifndef SQLITE_OMIT_VIRTUALTABLE
2491 if( IsVirtual(pTab
) ){
2492 sqlite3ErrorMsg(pParse
, "virtual tables may not be indexed");
2493 goto exit_create_index
;
2498 ** Find the name of the index. Make sure there is not already another
2499 ** index or table with the same name.
2501 ** Exception: If we are reading the names of permanent indices from the
2502 ** sqlite_master table (because some other process changed the schema) and
2503 ** one of the index names collides with the name of a temporary table or
2504 ** index, then we will continue to process this index.
2506 ** If pName==0 it means that we are
2507 ** dealing with a primary key or UNIQUE constraint. We have to invent our
2511 zName
= sqlite3NameFromToken(db
, pName
);
2512 if( zName
==0 ) goto exit_create_index
;
2513 if( SQLITE_OK
!=sqlite3CheckObjectName(pParse
, zName
) ){
2514 goto exit_create_index
;
2516 if( !db
->init
.busy
){
2517 if( sqlite3FindTable(db
, zName
, 0)!=0 ){
2518 sqlite3ErrorMsg(pParse
, "there is already a table named %s", zName
);
2519 goto exit_create_index
;
2522 if( sqlite3FindIndex(db
, zName
, pDb
->zName
)!=0 ){
2524 sqlite3ErrorMsg(pParse
, "index %s already exists", zName
);
2526 assert( !db
->init
.busy
);
2527 sqlite3CodeVerifySchema(pParse
, iDb
);
2529 goto exit_create_index
;
2534 for(pLoop
=pTab
->pIndex
, n
=1; pLoop
; pLoop
=pLoop
->pNext
, n
++){}
2535 zName
= sqlite3MPrintf(db
, "sqlite_autoindex_%s_%d", pTab
->zName
, n
);
2537 goto exit_create_index
;
2541 /* Check for authorization to create an index.
2543 #ifndef SQLITE_OMIT_AUTHORIZATION
2545 const char *zDb
= pDb
->zName
;
2546 if( sqlite3AuthCheck(pParse
, SQLITE_INSERT
, SCHEMA_TABLE(iDb
), 0, zDb
) ){
2547 goto exit_create_index
;
2549 i
= SQLITE_CREATE_INDEX
;
2550 if( !OMIT_TEMPDB
&& iDb
==1 ) i
= SQLITE_CREATE_TEMP_INDEX
;
2551 if( sqlite3AuthCheck(pParse
, i
, zName
, pTab
->zName
, zDb
) ){
2552 goto exit_create_index
;
2557 /* If pList==0, it means this routine was called to make a primary
2558 ** key out of the last column added to the table under construction.
2559 ** So create a fake list to simulate this.
2562 nullId
.z
= pTab
->aCol
[pTab
->nCol
-1].zName
;
2563 nullId
.n
= sqlite3Strlen30((char*)nullId
.z
);
2564 pList
= sqlite3ExprListAppend(pParse
, 0, 0);
2565 if( pList
==0 ) goto exit_create_index
;
2566 sqlite3ExprListSetName(pParse
, pList
, &nullId
, 0);
2567 pList
->a
[0].sortOrder
= (u8
)sortOrder
;
2570 /* Figure out how many bytes of space are required to store explicitly
2571 ** specified collation sequence names.
2573 for(i
=0; i
<pList
->nExpr
; i
++){
2574 Expr
*pExpr
= pList
->a
[i
].pExpr
;
2576 CollSeq
*pColl
= pExpr
->pColl
;
2577 /* Either pColl!=0 or there was an OOM failure. But if an OOM
2578 ** failure we have quit before reaching this point. */
2579 if( ALWAYS(pColl
) ){
2580 nExtra
+= (1 + sqlite3Strlen30(pColl
->zName
));
2586 ** Allocate the index structure.
2588 nName
= sqlite3Strlen30(zName
);
2589 nCol
= pList
->nExpr
;
2590 pIndex
= sqlite3DbMallocZero(db
,
2591 sizeof(Index
) + /* Index structure */
2592 sizeof(int)*nCol
+ /* Index.aiColumn */
2593 sizeof(int)*(nCol
+1) + /* Index.aiRowEst */
2594 sizeof(char *)*nCol
+ /* Index.azColl */
2595 sizeof(u8
)*nCol
+ /* Index.aSortOrder */
2596 nName
+ 1 + /* Index.zName */
2597 nExtra
/* Collation sequence names */
2599 if( db
->mallocFailed
){
2600 goto exit_create_index
;
2602 pIndex
->azColl
= (char**)(&pIndex
[1]);
2603 pIndex
->aiColumn
= (int *)(&pIndex
->azColl
[nCol
]);
2604 pIndex
->aiRowEst
= (unsigned *)(&pIndex
->aiColumn
[nCol
]);
2605 pIndex
->aSortOrder
= (u8
*)(&pIndex
->aiRowEst
[nCol
+1]);
2606 pIndex
->zName
= (char *)(&pIndex
->aSortOrder
[nCol
]);
2607 zExtra
= (char *)(&pIndex
->zName
[nName
+1]);
2608 memcpy(pIndex
->zName
, zName
, nName
+1);
2609 pIndex
->pTable
= pTab
;
2610 pIndex
->nColumn
= pList
->nExpr
;
2611 pIndex
->onError
= (u8
)onError
;
2612 pIndex
->autoIndex
= (u8
)(pName
==0);
2613 pIndex
->pSchema
= db
->aDb
[iDb
].pSchema
;
2614 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
2616 /* Check to see if we should honor DESC requests on index columns
2618 if( pDb
->pSchema
->file_format
>=4 ){
2619 sortOrderMask
= -1; /* Honor DESC */
2621 sortOrderMask
= 0; /* Ignore DESC */
2624 /* Scan the names of the columns of the table to be indexed and
2625 ** load the column indices into the Index structure. Report an error
2626 ** if any column is not found.
2628 ** TODO: Add a test to make sure that the same column is not named
2629 ** more than once within the same index. Only the first instance of
2630 ** the column will ever be used by the optimizer. Note that using the
2631 ** same column more than once cannot be an error because that would
2632 ** break backwards compatibility - it needs to be a warning.
2634 for(i
=0, pListItem
=pList
->a
; i
<pList
->nExpr
; i
++, pListItem
++){
2635 const char *zColName
= pListItem
->zName
;
2637 int requestedSortOrder
;
2638 char *zColl
; /* Collation sequence name */
2640 for(j
=0, pTabCol
=pTab
->aCol
; j
<pTab
->nCol
; j
++, pTabCol
++){
2641 if( sqlite3StrICmp(zColName
, pTabCol
->zName
)==0 ) break;
2643 if( j
>=pTab
->nCol
){
2644 sqlite3ErrorMsg(pParse
, "table %s has no column named %s",
2645 pTab
->zName
, zColName
);
2646 pParse
->checkSchema
= 1;
2647 goto exit_create_index
;
2649 pIndex
->aiColumn
[i
] = j
;
2650 /* Justification of the ALWAYS(pListItem->pExpr->pColl): Because of
2651 ** the way the "idxlist" non-terminal is constructed by the parser,
2652 ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
2653 ** must exist or else there must have been an OOM error. But if there
2654 ** was an OOM error, we would never reach this point. */
2655 if( pListItem
->pExpr
&& ALWAYS(pListItem
->pExpr
->pColl
) ){
2657 zColl
= pListItem
->pExpr
->pColl
->zName
;
2658 nColl
= sqlite3Strlen30(zColl
) + 1;
2659 assert( nExtra
>=nColl
);
2660 memcpy(zExtra
, zColl
, nColl
);
2665 zColl
= pTab
->aCol
[j
].zColl
;
2667 zColl
= db
->pDfltColl
->zName
;
2670 if( !db
->init
.busy
&& !sqlite3LocateCollSeq(pParse
, zColl
) ){
2671 goto exit_create_index
;
2673 pIndex
->azColl
[i
] = zColl
;
2674 requestedSortOrder
= pListItem
->sortOrder
& sortOrderMask
;
2675 pIndex
->aSortOrder
[i
] = (u8
)requestedSortOrder
;
2677 sqlite3DefaultRowEst(pIndex
);
2679 if( pTab
==pParse
->pNewTable
){
2680 /* This routine has been called to create an automatic index as a
2681 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2682 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2685 ** CREATE TABLE t(x PRIMARY KEY, y);
2686 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2688 ** Either way, check to see if the table already has such an index. If
2689 ** so, don't bother creating this one. This only applies to
2690 ** automatically created indices. Users can do as they wish with
2691 ** explicit indices.
2693 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
2694 ** (and thus suppressing the second one) even if they have different
2697 ** If there are different collating sequences or if the columns of
2698 ** the constraint occur in different orders, then the constraints are
2699 ** considered distinct and both result in separate indices.
2702 for(pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
2704 assert( pIdx
->onError
!=OE_None
);
2705 assert( pIdx
->autoIndex
);
2706 assert( pIndex
->onError
!=OE_None
);
2708 if( pIdx
->nColumn
!=pIndex
->nColumn
) continue;
2709 for(k
=0; k
<pIdx
->nColumn
; k
++){
2712 if( pIdx
->aiColumn
[k
]!=pIndex
->aiColumn
[k
] ) break;
2713 z1
= pIdx
->azColl
[k
];
2714 z2
= pIndex
->azColl
[k
];
2715 if( z1
!=z2
&& sqlite3StrICmp(z1
, z2
) ) break;
2717 if( k
==pIdx
->nColumn
){
2718 if( pIdx
->onError
!=pIndex
->onError
){
2719 /* This constraint creates the same index as a previous
2720 ** constraint specified somewhere in the CREATE TABLE statement.
2721 ** However the ON CONFLICT clauses are different. If both this
2722 ** constraint and the previous equivalent constraint have explicit
2723 ** ON CONFLICT clauses this is an error. Otherwise, use the
2724 ** explicitly specified behaviour for the index.
2726 if( !(pIdx
->onError
==OE_Default
|| pIndex
->onError
==OE_Default
) ){
2727 sqlite3ErrorMsg(pParse
,
2728 "conflicting ON CONFLICT clauses specified", 0);
2730 if( pIdx
->onError
==OE_Default
){
2731 pIdx
->onError
= pIndex
->onError
;
2734 goto exit_create_index
;
2739 /* Link the new Index structure to its table and to the other
2740 ** in-memory database structures.
2742 if( db
->init
.busy
){
2744 assert( sqlite3SchemaMutexHeld(db
, 0, pIndex
->pSchema
) );
2745 p
= sqlite3HashInsert(&pIndex
->pSchema
->idxHash
,
2746 pIndex
->zName
, sqlite3Strlen30(pIndex
->zName
),
2749 assert( p
==pIndex
); /* Malloc must have failed */
2750 db
->mallocFailed
= 1;
2751 goto exit_create_index
;
2753 db
->flags
|= SQLITE_InternChanges
;
2755 pIndex
->tnum
= db
->init
.newTnum
;
2759 /* If the db->init.busy is 0 then create the index on disk. This
2760 ** involves writing the index into the master table and filling in the
2761 ** index with the current table contents.
2763 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2764 ** command. db->init.busy is 1 when a database is opened and
2765 ** CREATE INDEX statements are read out of the master table. In
2766 ** the latter case the index already exists on disk, which is why
2767 ** we don't want to recreate it.
2769 ** If pTblName==0 it means this index is generated as a primary key
2770 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2771 ** has just been created, it contains no data and the index initialization
2772 ** step can be skipped.
2774 else{ /* if( db->init.busy==0 ) */
2777 int iMem
= ++pParse
->nMem
;
2779 v
= sqlite3GetVdbe(pParse
);
2780 if( v
==0 ) goto exit_create_index
;
2783 /* Create the rootpage for the index
2785 sqlite3BeginWriteOperation(pParse
, 1, iDb
);
2786 sqlite3VdbeAddOp2(v
, OP_CreateIndex
, iDb
, iMem
);
2788 /* Gather the complete text of the CREATE INDEX statement into
2789 ** the zStmt variable
2793 /* A named index with an explicit CREATE INDEX statement */
2794 zStmt
= sqlite3MPrintf(db
, "CREATE%s INDEX %.*s",
2795 onError
==OE_None
? "" : " UNIQUE",
2796 pEnd
->z
- pName
->z
+ 1,
2799 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
2800 /* zStmt = sqlite3MPrintf(""); */
2804 /* Add an entry in sqlite_master for this index
2806 sqlite3NestedParse(pParse
,
2807 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
2808 db
->aDb
[iDb
].zName
, SCHEMA_TABLE(iDb
),
2814 sqlite3DbFree(db
, zStmt
);
2816 /* Fill the index with data and reparse the schema. Code an OP_Expire
2817 ** to invalidate all pre-compiled statements.
2820 sqlite3RefillIndex(pParse
, pIndex
, iMem
);
2821 sqlite3ChangeCookie(pParse
, iDb
);
2822 sqlite3VdbeAddOp4(v
, OP_ParseSchema
, iDb
, 0, 0,
2823 sqlite3MPrintf(db
, "name='%q' AND type='index'", pIndex
->zName
),
2825 sqlite3VdbeAddOp1(v
, OP_Expire
, 0);
2829 /* When adding an index to the list of indices for a table, make
2830 ** sure all indices labeled OE_Replace come after all those labeled
2831 ** OE_Ignore. This is necessary for the correct constraint check
2832 ** processing (in sqlite3GenerateConstraintChecks()) as part of
2833 ** UPDATE and INSERT statements.
2835 if( db
->init
.busy
|| pTblName
==0 ){
2836 if( onError
!=OE_Replace
|| pTab
->pIndex
==0
2837 || pTab
->pIndex
->onError
==OE_Replace
){
2838 pIndex
->pNext
= pTab
->pIndex
;
2839 pTab
->pIndex
= pIndex
;
2841 Index
*pOther
= pTab
->pIndex
;
2842 while( pOther
->pNext
&& pOther
->pNext
->onError
!=OE_Replace
){
2843 pOther
= pOther
->pNext
;
2845 pIndex
->pNext
= pOther
->pNext
;
2846 pOther
->pNext
= pIndex
;
2852 /* Clean up before exiting */
2855 sqlite3DbFree(db
, pIndex
->zColAff
);
2856 sqlite3DbFree(db
, pIndex
);
2858 sqlite3ExprListDelete(db
, pList
);
2859 sqlite3SrcListDelete(db
, pTblName
);
2860 sqlite3DbFree(db
, zName
);
2865 ** Fill the Index.aiRowEst[] array with default information - information
2866 ** to be used when we have not run the ANALYZE command.
2868 ** aiRowEst[0] is suppose to contain the number of elements in the index.
2869 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
2870 ** number of rows in the table that match any particular value of the
2871 ** first column of the index. aiRowEst[2] is an estimate of the number
2872 ** of rows that match any particular combiniation of the first 2 columns
2873 ** of the index. And so forth. It must always be the case that
2875 ** aiRowEst[N]<=aiRowEst[N-1]
2878 ** Apart from that, we have little to go on besides intuition as to
2879 ** how aiRowEst[] should be initialized. The numbers generated here
2880 ** are based on typical values found in actual indices.
2882 void sqlite3DefaultRowEst(Index
*pIdx
){
2883 unsigned *a
= pIdx
->aiRowEst
;
2887 a
[0] = pIdx
->pTable
->nRowEst
;
2888 if( a
[0]<10 ) a
[0] = 10;
2890 for(i
=1; i
<=pIdx
->nColumn
; i
++){
2894 if( pIdx
->onError
!=OE_None
){
2895 a
[pIdx
->nColumn
] = 1;
2900 ** This routine will drop an existing named index. This routine
2901 ** implements the DROP INDEX statement.
2903 void sqlite3DropIndex(Parse
*pParse
, SrcList
*pName
, int ifExists
){
2906 sqlite3
*db
= pParse
->db
;
2909 assert( pParse
->nErr
==0 ); /* Never called with prior errors */
2910 if( db
->mallocFailed
){
2911 goto exit_drop_index
;
2913 assert( pName
->nSrc
==1 );
2914 if( SQLITE_OK
!=sqlite3ReadSchema(pParse
) ){
2915 goto exit_drop_index
;
2917 pIndex
= sqlite3FindIndex(db
, pName
->a
[0].zName
, pName
->a
[0].zDatabase
);
2920 sqlite3ErrorMsg(pParse
, "no such index: %S", pName
, 0);
2922 sqlite3CodeVerifyNamedSchema(pParse
, pName
->a
[0].zDatabase
);
2924 pParse
->checkSchema
= 1;
2925 goto exit_drop_index
;
2927 if( pIndex
->autoIndex
){
2928 sqlite3ErrorMsg(pParse
, "index associated with UNIQUE "
2929 "or PRIMARY KEY constraint cannot be dropped", 0);
2930 goto exit_drop_index
;
2932 iDb
= sqlite3SchemaToIndex(db
, pIndex
->pSchema
);
2933 #ifndef SQLITE_OMIT_AUTHORIZATION
2935 int code
= SQLITE_DROP_INDEX
;
2936 Table
*pTab
= pIndex
->pTable
;
2937 const char *zDb
= db
->aDb
[iDb
].zName
;
2938 const char *zTab
= SCHEMA_TABLE(iDb
);
2939 if( sqlite3AuthCheck(pParse
, SQLITE_DELETE
, zTab
, 0, zDb
) ){
2940 goto exit_drop_index
;
2942 if( !OMIT_TEMPDB
&& iDb
) code
= SQLITE_DROP_TEMP_INDEX
;
2943 if( sqlite3AuthCheck(pParse
, code
, pIndex
->zName
, pTab
->zName
, zDb
) ){
2944 goto exit_drop_index
;
2949 /* Generate code to remove the index and from the master table */
2950 v
= sqlite3GetVdbe(pParse
);
2952 sqlite3BeginWriteOperation(pParse
, 1, iDb
);
2953 sqlite3NestedParse(pParse
,
2954 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
2955 db
->aDb
[iDb
].zName
, SCHEMA_TABLE(iDb
),
2958 if( sqlite3FindTable(db
, "sqlite_stat1", db
->aDb
[iDb
].zName
) ){
2959 sqlite3NestedParse(pParse
,
2960 "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q",
2961 db
->aDb
[iDb
].zName
, pIndex
->zName
2964 sqlite3ChangeCookie(pParse
, iDb
);
2965 destroyRootPage(pParse
, pIndex
->tnum
, iDb
);
2966 sqlite3VdbeAddOp4(v
, OP_DropIndex
, iDb
, 0, 0, pIndex
->zName
, 0);
2970 sqlite3SrcListDelete(db
, pName
);
2974 ** pArray is a pointer to an array of objects. Each object in the
2975 ** array is szEntry bytes in size. This routine allocates a new
2976 ** object on the end of the array.
2978 ** *pnEntry is the number of entries already in use. *pnAlloc is
2979 ** the previously allocated size of the array. initSize is the
2980 ** suggested initial array size allocation.
2982 ** The index of the new entry is returned in *pIdx.
2984 ** This routine returns a pointer to the array of objects. This
2985 ** might be the same as the pArray parameter or it might be a different
2986 ** pointer if the array was resized.
2988 void *sqlite3ArrayAllocate(
2989 sqlite3
*db
, /* Connection to notify of malloc failures */
2990 void *pArray
, /* Array of objects. Might be reallocated */
2991 int szEntry
, /* Size of each object in the array */
2992 int initSize
, /* Suggested initial allocation, in elements */
2993 int *pnEntry
, /* Number of objects currently in use */
2994 int *pnAlloc
, /* Current size of the allocation, in elements */
2995 int *pIdx
/* Write the index of a new slot here */
2998 if( *pnEntry
>= *pnAlloc
){
3001 newSize
= (*pnAlloc
)*2 + initSize
;
3002 pNew
= sqlite3DbRealloc(db
, pArray
, newSize
*szEntry
);
3007 *pnAlloc
= sqlite3DbMallocSize(db
, pNew
)/szEntry
;
3011 memset(&z
[*pnEntry
* szEntry
], 0, szEntry
);
3018 ** Append a new element to the given IdList. Create a new IdList if
3021 ** A new IdList is returned, or NULL if malloc() fails.
3023 IdList
*sqlite3IdListAppend(sqlite3
*db
, IdList
*pList
, Token
*pToken
){
3026 pList
= sqlite3DbMallocZero(db
, sizeof(IdList
) );
3027 if( pList
==0 ) return 0;
3030 pList
->a
= sqlite3ArrayAllocate(
3033 sizeof(pList
->a
[0]),
3040 sqlite3IdListDelete(db
, pList
);
3043 pList
->a
[i
].zName
= sqlite3NameFromToken(db
, pToken
);
3048 ** Delete an IdList.
3050 void sqlite3IdListDelete(sqlite3
*db
, IdList
*pList
){
3052 if( pList
==0 ) return;
3053 for(i
=0; i
<pList
->nId
; i
++){
3054 sqlite3DbFree(db
, pList
->a
[i
].zName
);
3056 sqlite3DbFree(db
, pList
->a
);
3057 sqlite3DbFree(db
, pList
);
3061 ** Return the index in pList of the identifier named zId. Return -1
3064 int sqlite3IdListIndex(IdList
*pList
, const char *zName
){
3066 if( pList
==0 ) return -1;
3067 for(i
=0; i
<pList
->nId
; i
++){
3068 if( sqlite3StrICmp(pList
->a
[i
].zName
, zName
)==0 ) return i
;
3074 ** Expand the space allocated for the given SrcList object by
3075 ** creating nExtra new slots beginning at iStart. iStart is zero based.
3076 ** New slots are zeroed.
3078 ** For example, suppose a SrcList initially contains two entries: A,B.
3079 ** To append 3 new entries onto the end, do this:
3081 ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
3083 ** After the call above it would contain: A, B, nil, nil, nil.
3084 ** If the iStart argument had been 1 instead of 2, then the result
3085 ** would have been: A, nil, nil, nil, B. To prepend the new slots,
3086 ** the iStart value would be 0. The result then would
3087 ** be: nil, nil, nil, A, B.
3089 ** If a memory allocation fails the SrcList is unchanged. The
3090 ** db->mallocFailed flag will be set to true.
3092 SrcList
*sqlite3SrcListEnlarge(
3093 sqlite3
*db
, /* Database connection to notify of OOM errors */
3094 SrcList
*pSrc
, /* The SrcList to be enlarged */
3095 int nExtra
, /* Number of new slots to add to pSrc->a[] */
3096 int iStart
/* Index in pSrc->a[] of first new slot */
3100 /* Sanity checking on calling parameters */
3101 assert( iStart
>=0 );
3102 assert( nExtra
>=1 );
3104 assert( iStart
<=pSrc
->nSrc
);
3106 /* Allocate additional space if needed */
3107 if( pSrc
->nSrc
+nExtra
>pSrc
->nAlloc
){
3109 int nAlloc
= pSrc
->nSrc
+nExtra
;
3111 pNew
= sqlite3DbRealloc(db
, pSrc
,
3112 sizeof(*pSrc
) + (nAlloc
-1)*sizeof(pSrc
->a
[0]) );
3114 assert( db
->mallocFailed
);
3118 nGot
= (sqlite3DbMallocSize(db
, pNew
) - sizeof(*pSrc
))/sizeof(pSrc
->a
[0])+1;
3119 pSrc
->nAlloc
= (u16
)nGot
;
3122 /* Move existing slots that come after the newly inserted slots
3123 ** out of the way */
3124 for(i
=pSrc
->nSrc
-1; i
>=iStart
; i
--){
3125 pSrc
->a
[i
+nExtra
] = pSrc
->a
[i
];
3127 pSrc
->nSrc
+= (i16
)nExtra
;
3129 /* Zero the newly allocated slots */
3130 memset(&pSrc
->a
[iStart
], 0, sizeof(pSrc
->a
[0])*nExtra
);
3131 for(i
=iStart
; i
<iStart
+nExtra
; i
++){
3132 pSrc
->a
[i
].iCursor
= -1;
3135 /* Return a pointer to the enlarged SrcList */
3141 ** Append a new table name to the given SrcList. Create a new SrcList if
3142 ** need be. A new entry is created in the SrcList even if pTable is NULL.
3144 ** A SrcList is returned, or NULL if there is an OOM error. The returned
3145 ** SrcList might be the same as the SrcList that was input or it might be
3146 ** a new one. If an OOM error does occurs, then the prior value of pList
3147 ** that is input to this routine is automatically freed.
3149 ** If pDatabase is not null, it means that the table has an optional
3150 ** database name prefix. Like this: "database.table". The pDatabase
3151 ** points to the table name and the pTable points to the database name.
3152 ** The SrcList.a[].zName field is filled with the table name which might
3153 ** come from pTable (if pDatabase is NULL) or from pDatabase.
3154 ** SrcList.a[].zDatabase is filled with the database name from pTable,
3155 ** or with NULL if no database is specified.
3157 ** In other words, if call like this:
3159 ** sqlite3SrcListAppend(D,A,B,0);
3161 ** Then B is a table name and the database name is unspecified. If called
3164 ** sqlite3SrcListAppend(D,A,B,C);
3166 ** Then C is the table name and B is the database name. If C is defined
3167 ** then so is B. In other words, we never have a case where:
3169 ** sqlite3SrcListAppend(D,A,0,C);
3171 ** Both pTable and pDatabase are assumed to be quoted. They are dequoted
3172 ** before being added to the SrcList.
3174 SrcList
*sqlite3SrcListAppend(
3175 sqlite3
*db
, /* Connection to notify of malloc failures */
3176 SrcList
*pList
, /* Append to this SrcList. NULL creates a new SrcList */
3177 Token
*pTable
, /* Table to append */
3178 Token
*pDatabase
/* Database of the table */
3180 struct SrcList_item
*pItem
;
3181 assert( pDatabase
==0 || pTable
!=0 ); /* Cannot have C without B */
3183 pList
= sqlite3DbMallocZero(db
, sizeof(SrcList
) );
3184 if( pList
==0 ) return 0;
3187 pList
= sqlite3SrcListEnlarge(db
, pList
, 1, pList
->nSrc
);
3188 if( db
->mallocFailed
){
3189 sqlite3SrcListDelete(db
, pList
);
3192 pItem
= &pList
->a
[pList
->nSrc
-1];
3193 if( pDatabase
&& pDatabase
->z
==0 ){
3197 Token
*pTemp
= pDatabase
;
3201 pItem
->zName
= sqlite3NameFromToken(db
, pTable
);
3202 pItem
->zDatabase
= sqlite3NameFromToken(db
, pDatabase
);
3207 ** Assign VdbeCursor index numbers to all tables in a SrcList
3209 void sqlite3SrcListAssignCursors(Parse
*pParse
, SrcList
*pList
){
3211 struct SrcList_item
*pItem
;
3212 assert(pList
|| pParse
->db
->mallocFailed
);
3214 for(i
=0, pItem
=pList
->a
; i
<pList
->nSrc
; i
++, pItem
++){
3215 if( pItem
->iCursor
>=0 ) break;
3216 pItem
->iCursor
= pParse
->nTab
++;
3217 if( pItem
->pSelect
){
3218 sqlite3SrcListAssignCursors(pParse
, pItem
->pSelect
->pSrc
);
3225 ** Delete an entire SrcList including all its substructure.
3227 void sqlite3SrcListDelete(sqlite3
*db
, SrcList
*pList
){
3229 struct SrcList_item
*pItem
;
3230 if( pList
==0 ) return;
3231 for(pItem
=pList
->a
, i
=0; i
<pList
->nSrc
; i
++, pItem
++){
3232 sqlite3DbFree(db
, pItem
->zDatabase
);
3233 sqlite3DbFree(db
, pItem
->zName
);
3234 sqlite3DbFree(db
, pItem
->zAlias
);
3235 sqlite3DbFree(db
, pItem
->zIndex
);
3236 sqlite3DeleteTable(db
, pItem
->pTab
);
3237 sqlite3SelectDelete(db
, pItem
->pSelect
);
3238 sqlite3ExprDelete(db
, pItem
->pOn
);
3239 sqlite3IdListDelete(db
, pItem
->pUsing
);
3241 sqlite3DbFree(db
, pList
);
3245 ** This routine is called by the parser to add a new term to the
3246 ** end of a growing FROM clause. The "p" parameter is the part of
3247 ** the FROM clause that has already been constructed. "p" is NULL
3248 ** if this is the first term of the FROM clause. pTable and pDatabase
3249 ** are the name of the table and database named in the FROM clause term.
3250 ** pDatabase is NULL if the database name qualifier is missing - the
3251 ** usual case. If the term has a alias, then pAlias points to the
3252 ** alias token. If the term is a subquery, then pSubquery is the
3253 ** SELECT statement that the subquery encodes. The pTable and
3254 ** pDatabase parameters are NULL for subqueries. The pOn and pUsing
3255 ** parameters are the content of the ON and USING clauses.
3257 ** Return a new SrcList which encodes is the FROM with the new
3260 SrcList
*sqlite3SrcListAppendFromTerm(
3261 Parse
*pParse
, /* Parsing context */
3262 SrcList
*p
, /* The left part of the FROM clause already seen */
3263 Token
*pTable
, /* Name of the table to add to the FROM clause */
3264 Token
*pDatabase
, /* Name of the database containing pTable */
3265 Token
*pAlias
, /* The right-hand side of the AS subexpression */
3266 Select
*pSubquery
, /* A subquery used in place of a table name */
3267 Expr
*pOn
, /* The ON clause of a join */
3268 IdList
*pUsing
/* The USING clause of a join */
3270 struct SrcList_item
*pItem
;
3271 sqlite3
*db
= pParse
->db
;
3272 if( !p
&& (pOn
|| pUsing
) ){
3273 sqlite3ErrorMsg(pParse
, "a JOIN clause is required before %s",
3274 (pOn
? "ON" : "USING")
3276 goto append_from_error
;
3278 p
= sqlite3SrcListAppend(db
, p
, pTable
, pDatabase
);
3279 if( p
==0 || NEVER(p
->nSrc
==0) ){
3280 goto append_from_error
;
3282 pItem
= &p
->a
[p
->nSrc
-1];
3283 assert( pAlias
!=0 );
3285 pItem
->zAlias
= sqlite3NameFromToken(db
, pAlias
);
3287 pItem
->pSelect
= pSubquery
;
3289 pItem
->pUsing
= pUsing
;
3294 sqlite3ExprDelete(db
, pOn
);
3295 sqlite3IdListDelete(db
, pUsing
);
3296 sqlite3SelectDelete(db
, pSubquery
);
3301 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added
3302 ** element of the source-list passed as the second argument.
3304 void sqlite3SrcListIndexedBy(Parse
*pParse
, SrcList
*p
, Token
*pIndexedBy
){
3305 assert( pIndexedBy
!=0 );
3306 if( p
&& ALWAYS(p
->nSrc
>0) ){
3307 struct SrcList_item
*pItem
= &p
->a
[p
->nSrc
-1];
3308 assert( pItem
->notIndexed
==0 && pItem
->zIndex
==0 );
3309 if( pIndexedBy
->n
==1 && !pIndexedBy
->z
){
3310 /* A "NOT INDEXED" clause was supplied. See parse.y
3311 ** construct "indexed_opt" for details. */
3312 pItem
->notIndexed
= 1;
3314 pItem
->zIndex
= sqlite3NameFromToken(pParse
->db
, pIndexedBy
);
3320 ** When building up a FROM clause in the parser, the join operator
3321 ** is initially attached to the left operand. But the code generator
3322 ** expects the join operator to be on the right operand. This routine
3323 ** Shifts all join operators from left to right for an entire FROM
3326 ** Example: Suppose the join is like this:
3328 ** A natural cross join B
3330 ** The operator is "natural cross join". The A and B operands are stored
3331 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
3332 ** operator with A. This routine shifts that operator over to B.
3334 void sqlite3SrcListShiftJoinType(SrcList
*p
){
3337 for(i
=p
->nSrc
-1; i
>0; i
--){
3338 p
->a
[i
].jointype
= p
->a
[i
-1].jointype
;
3340 p
->a
[0].jointype
= 0;
3345 ** Begin a transaction
3347 void sqlite3BeginTransaction(Parse
*pParse
, int type
){
3352 assert( pParse
!=0 );
3355 /* if( db->aDb[0].pBt==0 ) return; */
3356 if( sqlite3AuthCheck(pParse
, SQLITE_TRANSACTION
, "BEGIN", 0, 0) ){
3359 v
= sqlite3GetVdbe(pParse
);
3361 if( type
!=TK_DEFERRED
){
3362 for(i
=0; i
<db
->nDb
; i
++){
3363 sqlite3VdbeAddOp2(v
, OP_Transaction
, i
, (type
==TK_EXCLUSIVE
)+1);
3364 sqlite3VdbeUsesBtree(v
, i
);
3367 sqlite3VdbeAddOp2(v
, OP_AutoCommit
, 0, 0);
3371 ** Commit a transaction
3373 void sqlite3CommitTransaction(Parse
*pParse
){
3377 assert( pParse
!=0 );
3380 /* if( db->aDb[0].pBt==0 ) return; */
3381 if( sqlite3AuthCheck(pParse
, SQLITE_TRANSACTION
, "COMMIT", 0, 0) ){
3384 v
= sqlite3GetVdbe(pParse
);
3386 sqlite3VdbeAddOp2(v
, OP_AutoCommit
, 1, 0);
3391 ** Rollback a transaction
3393 void sqlite3RollbackTransaction(Parse
*pParse
){
3397 assert( pParse
!=0 );
3400 /* if( db->aDb[0].pBt==0 ) return; */
3401 if( sqlite3AuthCheck(pParse
, SQLITE_TRANSACTION
, "ROLLBACK", 0, 0) ){
3404 v
= sqlite3GetVdbe(pParse
);
3406 sqlite3VdbeAddOp2(v
, OP_AutoCommit
, 1, 1);
3411 ** This function is called by the parser when it parses a command to create,
3412 ** release or rollback an SQL savepoint.
3414 void sqlite3Savepoint(Parse
*pParse
, int op
, Token
*pName
){
3415 char *zName
= sqlite3NameFromToken(pParse
->db
, pName
);
3417 Vdbe
*v
= sqlite3GetVdbe(pParse
);
3418 #ifndef SQLITE_OMIT_AUTHORIZATION
3419 static const char * const az
[] = { "BEGIN", "RELEASE", "ROLLBACK" };
3420 assert( !SAVEPOINT_BEGIN
&& SAVEPOINT_RELEASE
==1 && SAVEPOINT_ROLLBACK
==2 );
3422 if( !v
|| sqlite3AuthCheck(pParse
, SQLITE_SAVEPOINT
, az
[op
], zName
, 0) ){
3423 sqlite3DbFree(pParse
->db
, zName
);
3426 sqlite3VdbeAddOp4(v
, OP_Savepoint
, op
, 0, 0, zName
, P4_DYNAMIC
);
3431 ** Make sure the TEMP database is open and available for use. Return
3432 ** the number of errors. Leave any error messages in the pParse structure.
3434 int sqlite3OpenTempDatabase(Parse
*pParse
){
3435 sqlite3
*db
= pParse
->db
;
3436 if( db
->aDb
[1].pBt
==0 && !pParse
->explain
){
3439 static const int flags
=
3440 SQLITE_OPEN_READWRITE
|
3441 SQLITE_OPEN_CREATE
|
3442 SQLITE_OPEN_EXCLUSIVE
|
3443 SQLITE_OPEN_DELETEONCLOSE
|
3444 SQLITE_OPEN_TEMP_DB
;
3446 rc
= sqlite3BtreeOpen(0, db
, &pBt
, 0, flags
);
3447 if( rc
!=SQLITE_OK
){
3448 sqlite3ErrorMsg(pParse
, "unable to open a temporary database "
3449 "file for storing temporary tables");
3453 db
->aDb
[1].pBt
= pBt
;
3454 assert( db
->aDb
[1].pSchema
);
3455 if( SQLITE_NOMEM
==sqlite3BtreeSetPageSize(pBt
, db
->nextPagesize
, -1, 0) ){
3456 db
->mallocFailed
= 1;
3464 ** Generate VDBE code that will verify the schema cookie and start
3465 ** a read-transaction for all named database files.
3467 ** It is important that all schema cookies be verified and all
3468 ** read transactions be started before anything else happens in
3469 ** the VDBE program. But this routine can be called after much other
3470 ** code has been generated. So here is what we do:
3472 ** The first time this routine is called, we code an OP_Goto that
3473 ** will jump to a subroutine at the end of the program. Then we
3474 ** record every database that needs its schema verified in the
3475 ** pParse->cookieMask field. Later, after all other code has been
3476 ** generated, the subroutine that does the cookie verifications and
3477 ** starts the transactions will be coded and the OP_Goto P2 value
3478 ** will be made to point to that subroutine. The generation of the
3479 ** cookie verification subroutine code happens in sqlite3FinishCoding().
3481 ** If iDb<0 then code the OP_Goto only - don't set flag to verify the
3482 ** schema on any databases. This can be used to position the OP_Goto
3483 ** early in the code, before we know if any database tables will be used.
3485 void sqlite3CodeVerifySchema(Parse
*pParse
, int iDb
){
3486 Parse
*pToplevel
= sqlite3ParseToplevel(pParse
);
3488 if( pToplevel
->cookieGoto
==0 ){
3489 Vdbe
*v
= sqlite3GetVdbe(pToplevel
);
3490 if( v
==0 ) return; /* This only happens if there was a prior error */
3491 pToplevel
->cookieGoto
= sqlite3VdbeAddOp2(v
, OP_Goto
, 0, 0)+1;
3494 sqlite3
*db
= pToplevel
->db
;
3497 assert( iDb
<db
->nDb
);
3498 assert( db
->aDb
[iDb
].pBt
!=0 || iDb
==1 );
3499 assert( iDb
<SQLITE_MAX_ATTACHED
+2 );
3500 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
3501 mask
= ((yDbMask
)1)<<iDb
;
3502 if( (pToplevel
->cookieMask
& mask
)==0 ){
3503 pToplevel
->cookieMask
|= mask
;
3504 pToplevel
->cookieValue
[iDb
] = db
->aDb
[iDb
].pSchema
->schema_cookie
;
3505 if( !OMIT_TEMPDB
&& iDb
==1 ){
3506 sqlite3OpenTempDatabase(pToplevel
);
3513 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
3514 ** attached database. Otherwise, invoke it for the database named zDb only.
3516 void sqlite3CodeVerifyNamedSchema(Parse
*pParse
, const char *zDb
){
3517 sqlite3
*db
= pParse
->db
;
3519 for(i
=0; i
<db
->nDb
; i
++){
3520 Db
*pDb
= &db
->aDb
[i
];
3521 if( pDb
->pBt
&& (!zDb
|| 0==sqlite3StrICmp(zDb
, pDb
->zName
)) ){
3522 sqlite3CodeVerifySchema(pParse
, i
);
3528 ** Generate VDBE code that prepares for doing an operation that
3529 ** might change the database.
3531 ** This routine starts a new transaction if we are not already within
3532 ** a transaction. If we are already within a transaction, then a checkpoint
3533 ** is set if the setStatement parameter is true. A checkpoint should
3534 ** be set for operations that might fail (due to a constraint) part of
3535 ** the way through and which will need to undo some writes without having to
3536 ** rollback the whole transaction. For operations where all constraints
3537 ** can be checked before any changes are made to the database, it is never
3538 ** necessary to undo a write and the checkpoint should not be set.
3540 void sqlite3BeginWriteOperation(Parse
*pParse
, int setStatement
, int iDb
){
3541 Parse
*pToplevel
= sqlite3ParseToplevel(pParse
);
3542 sqlite3CodeVerifySchema(pParse
, iDb
);
3543 pToplevel
->writeMask
|= ((yDbMask
)1)<<iDb
;
3544 pToplevel
->isMultiWrite
|= setStatement
;
3548 ** Indicate that the statement currently under construction might write
3549 ** more than one entry (example: deleting one row then inserting another,
3550 ** inserting multiple rows in a table, or inserting a row and index entries.)
3551 ** If an abort occurs after some of these writes have completed, then it will
3552 ** be necessary to undo the completed writes.
3554 void sqlite3MultiWrite(Parse
*pParse
){
3555 Parse
*pToplevel
= sqlite3ParseToplevel(pParse
);
3556 pToplevel
->isMultiWrite
= 1;
3560 ** The code generator calls this routine if is discovers that it is
3561 ** possible to abort a statement prior to completion. In order to
3562 ** perform this abort without corrupting the database, we need to make
3563 ** sure that the statement is protected by a statement transaction.
3565 ** Technically, we only need to set the mayAbort flag if the
3566 ** isMultiWrite flag was previously set. There is a time dependency
3567 ** such that the abort must occur after the multiwrite. This makes
3568 ** some statements involving the REPLACE conflict resolution algorithm
3569 ** go a little faster. But taking advantage of this time dependency
3570 ** makes it more difficult to prove that the code is correct (in
3571 ** particular, it prevents us from writing an effective
3572 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
3573 ** to take the safe route and skip the optimization.
3575 void sqlite3MayAbort(Parse
*pParse
){
3576 Parse
*pToplevel
= sqlite3ParseToplevel(pParse
);
3577 pToplevel
->mayAbort
= 1;
3581 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
3582 ** error. The onError parameter determines which (if any) of the statement
3583 ** and/or current transaction is rolled back.
3585 void sqlite3HaltConstraint(Parse
*pParse
, int onError
, char *p4
, int p4type
){
3586 Vdbe
*v
= sqlite3GetVdbe(pParse
);
3587 if( onError
==OE_Abort
){
3588 sqlite3MayAbort(pParse
);
3590 sqlite3VdbeAddOp4(v
, OP_Halt
, SQLITE_CONSTRAINT
, onError
, 0, p4
, p4type
);
3594 ** Check to see if pIndex uses the collating sequence pColl. Return
3595 ** true if it does and false if it does not.
3597 #ifndef SQLITE_OMIT_REINDEX
3598 static int collationMatch(const char *zColl
, Index
*pIndex
){
3601 for(i
=0; i
<pIndex
->nColumn
; i
++){
3602 const char *z
= pIndex
->azColl
[i
];
3604 if( 0==sqlite3StrICmp(z
, zColl
) ){
3613 ** Recompute all indices of pTab that use the collating sequence pColl.
3614 ** If pColl==0 then recompute all indices of pTab.
3616 #ifndef SQLITE_OMIT_REINDEX
3617 static void reindexTable(Parse
*pParse
, Table
*pTab
, char const *zColl
){
3618 Index
*pIndex
; /* An index associated with pTab */
3620 for(pIndex
=pTab
->pIndex
; pIndex
; pIndex
=pIndex
->pNext
){
3621 if( zColl
==0 || collationMatch(zColl
, pIndex
) ){
3622 int iDb
= sqlite3SchemaToIndex(pParse
->db
, pTab
->pSchema
);
3623 sqlite3BeginWriteOperation(pParse
, 0, iDb
);
3624 sqlite3RefillIndex(pParse
, pIndex
, -1);
3631 ** Recompute all indices of all tables in all databases where the
3632 ** indices use the collating sequence pColl. If pColl==0 then recompute
3633 ** all indices everywhere.
3635 #ifndef SQLITE_OMIT_REINDEX
3636 static void reindexDatabases(Parse
*pParse
, char const *zColl
){
3637 Db
*pDb
; /* A single database */
3638 int iDb
; /* The database index number */
3639 sqlite3
*db
= pParse
->db
; /* The database connection */
3640 HashElem
*k
; /* For looping over tables in pDb */
3641 Table
*pTab
; /* A table in the database */
3643 assert( sqlite3BtreeHoldsAllMutexes(db
) ); /* Needed for schema access */
3644 for(iDb
=0, pDb
=db
->aDb
; iDb
<db
->nDb
; iDb
++, pDb
++){
3646 for(k
=sqliteHashFirst(&pDb
->pSchema
->tblHash
); k
; k
=sqliteHashNext(k
)){
3647 pTab
= (Table
*)sqliteHashData(k
);
3648 reindexTable(pParse
, pTab
, zColl
);
3655 ** Generate code for the REINDEX command.
3658 ** REINDEX <collation> -- 2
3659 ** REINDEX ?<database>.?<tablename> -- 3
3660 ** REINDEX ?<database>.?<indexname> -- 4
3662 ** Form 1 causes all indices in all attached databases to be rebuilt.
3663 ** Form 2 rebuilds all indices in all databases that use the named
3664 ** collating function. Forms 3 and 4 rebuild the named index or all
3665 ** indices associated with the named table.
3667 #ifndef SQLITE_OMIT_REINDEX
3668 void sqlite3Reindex(Parse
*pParse
, Token
*pName1
, Token
*pName2
){
3669 CollSeq
*pColl
; /* Collating sequence to be reindexed, or NULL */
3670 char *z
; /* Name of a table or index */
3671 const char *zDb
; /* Name of the database */
3672 Table
*pTab
; /* A table in the database */
3673 Index
*pIndex
; /* An index associated with pTab */
3674 int iDb
; /* The database index number */
3675 sqlite3
*db
= pParse
->db
; /* The database connection */
3676 Token
*pObjName
; /* Name of the table or index to be reindexed */
3678 /* Read the database schema. If an error occurs, leave an error message
3679 ** and code in pParse and return NULL. */
3680 if( SQLITE_OK
!=sqlite3ReadSchema(pParse
) ){
3685 reindexDatabases(pParse
, 0);
3687 }else if( NEVER(pName2
==0) || pName2
->z
==0 ){
3689 assert( pName1
->z
);
3690 zColl
= sqlite3NameFromToken(pParse
->db
, pName1
);
3691 if( !zColl
) return;
3692 pColl
= sqlite3FindCollSeq(db
, ENC(db
), zColl
, 0);
3694 reindexDatabases(pParse
, zColl
);
3695 sqlite3DbFree(db
, zColl
);
3698 sqlite3DbFree(db
, zColl
);
3700 iDb
= sqlite3TwoPartName(pParse
, pName1
, pName2
, &pObjName
);
3702 z
= sqlite3NameFromToken(db
, pObjName
);
3704 zDb
= db
->aDb
[iDb
].zName
;
3705 pTab
= sqlite3FindTable(db
, z
, zDb
);
3707 reindexTable(pParse
, pTab
, 0);
3708 sqlite3DbFree(db
, z
);
3711 pIndex
= sqlite3FindIndex(db
, z
, zDb
);
3712 sqlite3DbFree(db
, z
);
3714 sqlite3BeginWriteOperation(pParse
, 0, iDb
);
3715 sqlite3RefillIndex(pParse
, pIndex
, -1);
3718 sqlite3ErrorMsg(pParse
, "unable to identify the object to be reindexed");
3723 ** Return a dynamicly allocated KeyInfo structure that can be used
3724 ** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
3726 ** If successful, a pointer to the new structure is returned. In this case
3727 ** the caller is responsible for calling sqlite3DbFree(db, ) on the returned
3728 ** pointer. If an error occurs (out of memory or missing collation
3729 ** sequence), NULL is returned and the state of pParse updated to reflect
3732 KeyInfo
*sqlite3IndexKeyinfo(Parse
*pParse
, Index
*pIdx
){
3734 int nCol
= pIdx
->nColumn
;
3735 int nBytes
= sizeof(KeyInfo
) + (nCol
-1)*sizeof(CollSeq
*) + nCol
;
3736 sqlite3
*db
= pParse
->db
;
3737 KeyInfo
*pKey
= (KeyInfo
*)sqlite3DbMallocZero(db
, nBytes
);
3740 pKey
->db
= pParse
->db
;
3741 pKey
->aSortOrder
= (u8
*)&(pKey
->aColl
[nCol
]);
3742 assert( &pKey
->aSortOrder
[nCol
]==&(((u8
*)pKey
)[nBytes
]) );
3743 for(i
=0; i
<nCol
; i
++){
3744 char *zColl
= pIdx
->azColl
[i
];
3746 pKey
->aColl
[i
] = sqlite3LocateCollSeq(pParse
, zColl
);
3747 pKey
->aSortOrder
[i
] = pIdx
->aSortOrder
[i
];
3749 pKey
->nField
= (u16
)nCol
;
3753 sqlite3DbFree(db
, pKey
);