blob: ce3e614843ed589288ccb211b6e1bd6b8d70e16c [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** 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:
drh75897232000-05-29 14:26:00 +000015**
drhbed86902000-06-02 13:27:59 +000016** CREATE TABLE
17** DROP TABLE
18** CREATE INDEX
19** DROP INDEX
drh832508b2002-03-02 17:04:07 +000020** creating ID lists
drhb19a2bc2001-09-16 00:13:26 +000021** BEGIN TRANSACTION
22** COMMIT
23** ROLLBACK
drh75897232000-05-29 14:26:00 +000024*/
25#include "sqliteInt.h"
26
27/*
drhe0bc4042002-06-25 01:09:11 +000028** This routine is called when a new SQL statement is beginning to
drh23bf66d2004-12-14 03:34:34 +000029** be parsed. Initialize the pParse structure as needed.
drhe0bc4042002-06-25 01:09:11 +000030*/
danielk19774adee202004-05-08 08:23:19 +000031void sqlite3BeginParse(Parse *pParse, int explainFlag){
drh1bd10f82008-12-10 21:19:56 +000032 pParse->explain = (u8)explainFlag;
drh7c972de2003-09-06 22:18:07 +000033 pParse->nVar = 0;
drhe0bc4042002-06-25 01:09:11 +000034}
35
danielk1977c00da102006-01-07 13:21:04 +000036#ifndef SQLITE_OMIT_SHARED_CACHE
37/*
38** The TableLock structure is only used by the sqlite3TableLock() and
39** codeTableLocks() functions.
40*/
41struct TableLock {
drhd698bc12006-03-23 23:33:26 +000042 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 */
danielk1977c00da102006-01-07 13:21:04 +000046};
47
48/*
drhd698bc12006-03-23 23:33:26 +000049** Record the fact that we want to lock a table at run-time.
danielk1977c00da102006-01-07 13:21:04 +000050**
drhd698bc12006-03-23 23:33:26 +000051** 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.
53**
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().
danielk1977c00da102006-01-07 13:21:04 +000057*/
58void sqlite3TableLock(
drhd698bc12006-03-23 23:33:26 +000059 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 */
danielk1977c00da102006-01-07 13:21:04 +000064){
dan65a7cd12009-09-01 12:16:01 +000065 Parse *pToplevel = sqlite3ParseToplevel(pParse);
danielk1977c00da102006-01-07 13:21:04 +000066 int i;
67 int nBytes;
68 TableLock *p;
drh8af73d42009-05-13 22:58:28 +000069 assert( iDb>=0 );
dan165921a2009-08-28 18:53:45 +000070
dan65a7cd12009-09-01 12:16:01 +000071 for(i=0; i<pToplevel->nTableLock; i++){
72 p = &pToplevel->aTableLock[i];
danielk1977c00da102006-01-07 13:21:04 +000073 if( p->iDb==iDb && p->iTab==iTab ){
74 p->isWriteLock = (p->isWriteLock || isWriteLock);
75 return;
76 }
77 }
78
dan65a7cd12009-09-01 12:16:01 +000079 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++];
danielk1977c00da102006-01-07 13:21:04 +000084 p->iDb = iDb;
85 p->iTab = iTab;
86 p->isWriteLock = isWriteLock;
87 p->zName = zName;
drhf3a65f72007-08-22 20:18:21 +000088 }else{
dan65a7cd12009-09-01 12:16:01 +000089 pToplevel->nTableLock = 0;
90 pToplevel->db->mallocFailed = 1;
danielk1977c00da102006-01-07 13:21:04 +000091 }
92}
93
94/*
95** Code an OP_TableLock instruction for each table locked by the
96** statement (configured by calls to sqlite3TableLock()).
97*/
98static void codeTableLocks(Parse *pParse){
99 int i;
100 Vdbe *pVdbe;
danielk1977c00da102006-01-07 13:21:04 +0000101
drh04491712009-05-13 17:21:13 +0000102 pVdbe = sqlite3GetVdbe(pParse);
103 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
danielk1977c00da102006-01-07 13:21:04 +0000104
105 for(i=0; i<pParse->nTableLock; i++){
106 TableLock *p = &pParse->aTableLock[i];
107 int p1 = p->iDb;
drh6a9ad3d2008-04-02 16:29:30 +0000108 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
109 p->zName, P4_STATIC);
danielk1977c00da102006-01-07 13:21:04 +0000110 }
111}
112#else
113 #define codeTableLocks(x)
114#endif
115
drhe0bc4042002-06-25 01:09:11 +0000116/*
drh75897232000-05-29 14:26:00 +0000117** This routine is called after a single SQL statement has been
drh80242052004-06-09 00:48:12 +0000118** 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
121** parse.
drh75897232000-05-29 14:26:00 +0000122**
123** Note that if an error occurred, it might be the case that
124** no VDBE code was generated.
125*/
drh80242052004-06-09 00:48:12 +0000126void sqlite3FinishCoding(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000127 sqlite3 *db;
drh80242052004-06-09 00:48:12 +0000128 Vdbe *v;
drhb86ccfb2003-01-28 23:13:10 +0000129
drh17435752007-08-16 04:30:38 +0000130 db = pParse->db;
131 if( db->mallocFailed ) return;
drh205f48e2004-11-05 00:43:11 +0000132 if( pParse->nested ) return;
drhc4dd3fd2008-01-22 01:48:05 +0000133 if( pParse->nErr ) return;
danielk197748d0d862005-02-01 03:09:52 +0000134
drh80242052004-06-09 00:48:12 +0000135 /* Begin by generating some termination code at the end of the
136 ** vdbe program
137 */
drh80242052004-06-09 00:48:12 +0000138 v = sqlite3GetVdbe(pParse);
danf3677212009-09-10 16:14:50 +0000139 assert( !pParse->isMultiWrite
140 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
drh80242052004-06-09 00:48:12 +0000141 if( v ){
drh66a51672008-01-03 00:01:23 +0000142 sqlite3VdbeAddOp0(v, OP_Halt);
drh0e3d7472004-06-19 17:33:07 +0000143
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.
149 */
drhc275b4e2004-07-19 17:25:24 +0000150 if( pParse->cookieGoto>0 ){
drh80242052004-06-09 00:48:12 +0000151 u32 mask;
drh26e4a8b2008-05-01 17:16:52 +0000152 int iDb;
drhd654be82005-09-20 17:42:23 +0000153 sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
drh80242052004-06-09 00:48:12 +0000154 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
155 if( (mask & pParse->cookieMask)==0 ) continue;
drhfb982642007-08-30 01:19:59 +0000156 sqlite3VdbeUsesBtree(v, iDb);
drh66a51672008-01-03 00:01:23 +0000157 sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
drh8a939192009-04-20 17:43:03 +0000158 if( db->init.busy==0 ){
159 sqlite3VdbeAddOp2(v,OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
160 }
drh80242052004-06-09 00:48:12 +0000161 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000162#ifndef SQLITE_OMIT_VIRTUALTABLE
drh26e4a8b2008-05-01 17:16:52 +0000163 {
164 int i;
165 for(i=0; i<pParse->nVtabLock; i++){
danielk1977595a5232009-07-24 17:58:53 +0000166 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
drh26e4a8b2008-05-01 17:16:52 +0000167 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
168 }
169 pParse->nVtabLock = 0;
danielk1977f9e7dda2006-06-16 16:08:53 +0000170 }
171#endif
danielk1977c00da102006-01-07 13:21:04 +0000172
173 /* Once all the cookies have been verified and transactions opened,
174 ** obtain the required table-locks. This is a no-op unless the
175 ** shared-cache feature is enabled.
176 */
177 codeTableLocks(pParse);
drh0b9f50d2009-06-23 20:28:53 +0000178
179 /* Initialize any AUTOINCREMENT data structures required.
180 */
181 sqlite3AutoincrementBegin(pParse);
182
183 /* Finally, jump back to the beginning of the executable code. */
drh66a51672008-01-03 00:01:23 +0000184 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
drh80242052004-06-09 00:48:12 +0000185 }
drh71c697e2004-08-08 23:39:19 +0000186 }
187
drh3f7d4e42004-07-24 14:35:58 +0000188
drh80242052004-06-09 00:48:12 +0000189 /* Get the VDBE program ready for execution
190 */
drh04491712009-05-13 17:21:13 +0000191 if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
drhcf1023c2007-05-08 20:59:49 +0000192#ifdef SQLITE_DEBUG
drhb86ccfb2003-01-28 23:13:10 +0000193 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
danielk19774adee202004-05-08 08:23:19 +0000194 sqlite3VdbeTrace(v, trace);
drhcf1023c2007-05-08 20:59:49 +0000195#endif
drhceea3322009-04-23 13:22:42 +0000196 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */
drh3492dd72009-09-14 23:47:24 +0000197 /* A minimum of one cursor is required if autoincrement is used
198 * See ticket [a696379c1f08866] */
199 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
danielk19776ab3a2e2009-02-19 14:39:25 +0000200 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem,
dane0af83a2009-09-08 19:15:01 +0000201 pParse->nTab, pParse->nMaxArg, pParse->explain,
202 pParse->isMultiWrite && pParse->mayAbort);
danielk1977441daf62005-02-01 03:46:43 +0000203 pParse->rc = SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +0000204 pParse->colNamesSet = 0;
drhe294da02010-02-25 23:44:15 +0000205 }else{
drh483750b2003-01-29 18:46:51 +0000206 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000207 }
drha226d052002-09-25 19:04:07 +0000208 pParse->nTab = 0;
209 pParse->nMem = 0;
210 pParse->nSet = 0;
drh7c972de2003-09-06 22:18:07 +0000211 pParse->nVar = 0;
drh80242052004-06-09 00:48:12 +0000212 pParse->cookieMask = 0;
drhc275b4e2004-07-19 17:25:24 +0000213 pParse->cookieGoto = 0;
drh75897232000-05-29 14:26:00 +0000214}
215
216/*
drh205f48e2004-11-05 00:43:11 +0000217** Run the parser and code generator recursively in order to generate
218** code for the SQL statement given onto the end of the pParse context
219** currently under construction. When the parser is run recursively
220** this way, the final OP_Halt is not appended and other initialization
221** and finalization steps are omitted because those are handling by the
222** outermost parser.
223**
224** Not everything is nestable. This facility is designed to permit
225** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
drhf1974842004-11-05 03:56:00 +0000226** care if you decide to try to use this routine for some other purposes.
drh205f48e2004-11-05 00:43:11 +0000227*/
228void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
229 va_list ap;
230 char *zSql;
drhfb45d8c2008-07-08 00:06:49 +0000231 char *zErrMsg = 0;
drh633e6d52008-07-28 19:34:53 +0000232 sqlite3 *db = pParse->db;
drhf1974842004-11-05 03:56:00 +0000233# define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar))
234 char saveBuf[SAVE_SZ];
235
drh205f48e2004-11-05 00:43:11 +0000236 if( pParse->nErr ) return;
237 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
238 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000239 zSql = sqlite3VMPrintf(db, zFormat, ap);
drh205f48e2004-11-05 00:43:11 +0000240 va_end(ap);
drh73c42a12004-11-20 18:13:10 +0000241 if( zSql==0 ){
242 return; /* A malloc must have failed */
243 }
drh205f48e2004-11-05 00:43:11 +0000244 pParse->nested++;
drhf1974842004-11-05 03:56:00 +0000245 memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
246 memset(&pParse->nVar, 0, SAVE_SZ);
drhfb45d8c2008-07-08 00:06:49 +0000247 sqlite3RunParser(pParse, zSql, &zErrMsg);
drh633e6d52008-07-28 19:34:53 +0000248 sqlite3DbFree(db, zErrMsg);
249 sqlite3DbFree(db, zSql);
drhf1974842004-11-05 03:56:00 +0000250 memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
drh205f48e2004-11-05 00:43:11 +0000251 pParse->nested--;
252}
253
254/*
danielk19778a414492004-06-29 08:59:35 +0000255** Locate the in-memory structure that describes a particular database
256** table given the name of that table and (optionally) the name of the
257** database containing the table. Return NULL if not found.
drha69d9162003-04-17 22:57:53 +0000258**
danielk19778a414492004-06-29 08:59:35 +0000259** If zDatabase is 0, all databases are searched for the table and the
260** first matching table is returned. (No checking for duplicate table
261** names is done.) The search order is TEMP first, then MAIN, then any
262** auxiliary databases added using the ATTACH command.
drhf26e09c2003-05-31 16:21:12 +0000263**
danielk19774adee202004-05-08 08:23:19 +0000264** See also sqlite3LocateTable().
drh75897232000-05-29 14:26:00 +0000265*/
drh9bb575f2004-09-06 17:24:11 +0000266Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
drhd24cc422003-03-27 12:51:24 +0000267 Table *p = 0;
268 int i;
drh0a687d12008-07-08 14:52:07 +0000269 int nName;
drh645f63e2004-06-22 13:22:40 +0000270 assert( zName!=0 );
drhdee0e402009-05-03 20:23:53 +0000271 nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000272 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000273 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000274 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
drh0a687d12008-07-08 14:52:07 +0000275 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
drhd24cc422003-03-27 12:51:24 +0000276 if( p ) break;
277 }
drh74e24cd2002-01-09 03:19:59 +0000278 return p;
drh75897232000-05-29 14:26:00 +0000279}
280
281/*
danielk19778a414492004-06-29 08:59:35 +0000282** Locate the in-memory structure that describes a particular database
283** table given the name of that table and (optionally) the name of the
284** database containing the table. Return NULL if not found. Also leave an
285** error message in pParse->zErrMsg.
drha69d9162003-04-17 22:57:53 +0000286**
danielk19778a414492004-06-29 08:59:35 +0000287** The difference between this routine and sqlite3FindTable() is that this
288** routine leaves an error message in pParse->zErrMsg where
289** sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53 +0000290*/
drhca424112008-01-25 15:04:48 +0000291Table *sqlite3LocateTable(
292 Parse *pParse, /* context in which to report errors */
293 int isView, /* True if looking for a VIEW rather than a TABLE */
294 const char *zName, /* Name of the table we are looking for */
295 const char *zDbase /* Name of the database. Might be NULL */
296){
drha69d9162003-04-17 22:57:53 +0000297 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000298
danielk19778a414492004-06-29 08:59:35 +0000299 /* Read the database schema. If an error occurs, leave an error message
300 ** and code in pParse and return NULL. */
301 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
302 return 0;
303 }
304
danielk19774adee202004-05-08 08:23:19 +0000305 p = sqlite3FindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000306 if( p==0 ){
drhca424112008-01-25 15:04:48 +0000307 const char *zMsg = isView ? "no such view" : "no such table";
danielk19778a414492004-06-29 08:59:35 +0000308 if( zDbase ){
drhca424112008-01-25 15:04:48 +0000309 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
drha69d9162003-04-17 22:57:53 +0000310 }else{
drhca424112008-01-25 15:04:48 +0000311 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
drha69d9162003-04-17 22:57:53 +0000312 }
drha6ecd332004-06-10 00:29:09 +0000313 pParse->checkSchema = 1;
drha69d9162003-04-17 22:57:53 +0000314 }
315 return p;
316}
317
318/*
319** Locate the in-memory structure that describes
320** a particular index given the name of that index
321** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000322** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000323**
324** If zDatabase is 0, all databases are searched for the
325** table and the first matching index is returned. (No checking
326** for duplicate index names is done.) The search order is
327** TEMP first, then MAIN, then any auxiliary databases added
328** using the ATTACH command.
drh75897232000-05-29 14:26:00 +0000329*/
drh9bb575f2004-09-06 17:24:11 +0000330Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24 +0000331 Index *p = 0;
332 int i;
drhdee0e402009-05-03 20:23:53 +0000333 int nName = sqlite3Strlen30(zName);
danielk197753c0f742005-03-29 03:10:59 +0000334 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000335 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk1977e501b892006-01-09 06:29:47 +0000336 Schema *pSchema = db->aDb[j].pSchema;
drh04491712009-05-13 17:21:13 +0000337 assert( pSchema );
danielk19774adee202004-05-08 08:23:19 +0000338 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
drh04491712009-05-13 17:21:13 +0000339 p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
drhd24cc422003-03-27 12:51:24 +0000340 if( p ) break;
341 }
drh74e24cd2002-01-09 03:19:59 +0000342 return p;
drh75897232000-05-29 14:26:00 +0000343}
344
345/*
drh956bc922004-07-24 17:38:29 +0000346** Reclaim the memory used by an index
347*/
348static void freeIndex(Index *p){
drhd9da78a2009-03-24 15:08:09 +0000349 sqlite3 *db = p->pTable->dbMem;
drh92aa5ea2009-09-11 14:05:06 +0000350#ifndef SQLITE_OMIT_ANALYZE
dan85c165c2009-08-19 14:34:54 +0000351 sqlite3DeleteIndexSamples(p);
drh92aa5ea2009-09-11 14:05:06 +0000352#endif
drh633e6d52008-07-28 19:34:53 +0000353 sqlite3DbFree(db, p->zColAff);
354 sqlite3DbFree(db, p);
drh956bc922004-07-24 17:38:29 +0000355}
356
357/*
drh75897232000-05-29 14:26:00 +0000358** Remove the given index from the index hash table, and free
359** its memory structures.
360**
drhd229ca92002-01-09 13:30:41 +0000361** The index is removed from the database hash tables but
362** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000363** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000364*/
drh03881232009-02-13 03:43:31 +0000365static void sqlite3DeleteIndex(Index *p){
drhd229ca92002-01-09 13:30:41 +0000366 Index *pOld;
danielk1977da184232006-01-05 11:34:32 +0000367 const char *zName = p->zName;
drhd24cc422003-03-27 12:51:24 +0000368
drhea678832008-12-10 19:26:22 +0000369 pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName,
drha83ccca2009-04-28 13:01:09 +0000370 sqlite3Strlen30(zName), 0);
drh85c23c62005-08-20 03:03:04 +0000371 assert( pOld==0 || pOld==p );
drh956bc922004-07-24 17:38:29 +0000372 freeIndex(p);
drh75897232000-05-29 14:26:00 +0000373}
374
375/*
drhc96d8532005-05-03 12:30:33 +0000376** For the index called zIdxName which is found in the database iDb,
377** unlike that index from its Table then remove the index from
378** the index hash table and free all memory structures associated
379** with the index.
drh5e00f6c2001-09-13 13:46:56 +0000380*/
drh9bb575f2004-09-06 17:24:11 +0000381void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
drh956bc922004-07-24 17:38:29 +0000382 Index *pIndex;
383 int len;
danielk1977da184232006-01-05 11:34:32 +0000384 Hash *pHash = &db->aDb[iDb].pSchema->idxHash;
drh956bc922004-07-24 17:38:29 +0000385
drhdee0e402009-05-03 20:23:53 +0000386 len = sqlite3Strlen30(zIdxName);
drha83ccca2009-04-28 13:01:09 +0000387 pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
drh9635cc72009-06-25 11:50:21 +0000388 if( pIndex ){
drh956bc922004-07-24 17:38:29 +0000389 if( pIndex->pTable->pIndex==pIndex ){
390 pIndex->pTable->pIndex = pIndex->pNext;
391 }else{
392 Index *p;
drh04491712009-05-13 17:21:13 +0000393 /* Justification of ALWAYS(); The index must be on the list of
394 ** indices. */
395 p = pIndex->pTable->pIndex;
396 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
397 if( ALWAYS(p && p->pNext==pIndex) ){
drh956bc922004-07-24 17:38:29 +0000398 p->pNext = pIndex->pNext;
399 }
drh5e00f6c2001-09-13 13:46:56 +0000400 }
drh956bc922004-07-24 17:38:29 +0000401 freeIndex(pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000402 }
drh956bc922004-07-24 17:38:29 +0000403 db->flags |= SQLITE_InternChanges;
drh5e00f6c2001-09-13 13:46:56 +0000404}
405
406/*
drhe0bc4042002-06-25 01:09:11 +0000407** Erase all schema information from the in-memory hash tables of
drh234c39d2004-07-24 03:30:47 +0000408** a single database. This routine is called to reclaim memory
409** before the database closes. It is also called during a rollback
danielk1977e0d4b062004-06-28 01:11:46 +0000410** if there were schema changes during the transaction or if a
411** schema-cookie mismatch occurs.
drh1c2d8412003-03-31 00:30:47 +0000412**
drh62312862009-02-03 22:17:42 +0000413** If iDb==0 then reset the internal schema tables for all database
414** files. If iDb>=1 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000415** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000416*/
drh9bb575f2004-09-06 17:24:11 +0000417void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
drh1c2d8412003-03-31 00:30:47 +0000418 int i, j;
drh1c2d8412003-03-31 00:30:47 +0000419 assert( iDb>=0 && iDb<db->nDb );
danielk19774eab8b72007-12-27 15:12:16 +0000420
421 if( iDb==0 ){
422 sqlite3BtreeEnterAll(db);
423 }
drh1c2d8412003-03-31 00:30:47 +0000424 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000425 Db *pDb = &db->aDb[i];
danielk1977da184232006-01-05 11:34:32 +0000426 if( pDb->pSchema ){
danielk19774eab8b72007-12-27 15:12:16 +0000427 assert(i==1 || (pDb->pBt && sqlite3BtreeHoldsMutex(pDb->pBt)));
danielk1977de0fe3e2006-01-06 06:33:12 +0000428 sqlite3SchemaFree(pDb->pSchema);
drhd24cc422003-03-27 12:51:24 +0000429 }
drh1c2d8412003-03-31 00:30:47 +0000430 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000431 }
drh1c2d8412003-03-31 00:30:47 +0000432 assert( iDb==0 );
433 db->flags &= ~SQLITE_InternChanges;
danielk1977595a5232009-07-24 17:58:53 +0000434 sqlite3VtabUnlockList(db);
danielk19774eab8b72007-12-27 15:12:16 +0000435 sqlite3BtreeLeaveAll(db);
drh1c2d8412003-03-31 00:30:47 +0000436
437 /* If one or more of the auxiliary database files has been closed,
danielk1977311019b2006-01-10 07:14:23 +0000438 ** then remove them from the auxiliary database list. We take the
drh1c2d8412003-03-31 00:30:47 +0000439 ** opportunity to do this here since we have just deleted all of the
440 ** schema hash tables and therefore do not have to make any changes
441 ** to any of those tables.
442 */
443 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000444 struct Db *pDb = &db->aDb[i];
445 if( pDb->pBt==0 ){
drh633e6d52008-07-28 19:34:53 +0000446 sqlite3DbFree(db, pDb->zName);
drh4d189ca2004-02-12 18:46:38 +0000447 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000448 continue;
449 }
450 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000451 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000452 }
drh8bf8dc92003-05-17 17:35:10 +0000453 j++;
drh1c2d8412003-03-31 00:30:47 +0000454 }
455 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
456 db->nDb = j;
457 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
458 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
drh633e6d52008-07-28 19:34:53 +0000459 sqlite3DbFree(db, db->aDb);
drh1c2d8412003-03-31 00:30:47 +0000460 db->aDb = db->aDbStatic;
461 }
drhe0bc4042002-06-25 01:09:11 +0000462}
463
464/*
drhe0bc4042002-06-25 01:09:11 +0000465** This routine is called when a commit occurs.
466*/
drh9bb575f2004-09-06 17:24:11 +0000467void sqlite3CommitInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000468 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000469}
470
471/*
drh956bc922004-07-24 17:38:29 +0000472** Clear the column names from a table or view.
473*/
474static void sqliteResetColumnNames(Table *pTable){
475 int i;
476 Column *pCol;
drhd9da78a2009-03-24 15:08:09 +0000477 sqlite3 *db = pTable->dbMem;
drhc4a64fa2009-05-11 20:53:28 +0000478 testcase( db==0 );
drh956bc922004-07-24 17:38:29 +0000479 assert( pTable!=0 );
drhdd5b2fa2005-03-28 03:39:55 +0000480 if( (pCol = pTable->aCol)!=0 ){
481 for(i=0; i<pTable->nCol; i++, pCol++){
drh633e6d52008-07-28 19:34:53 +0000482 sqlite3DbFree(db, pCol->zName);
483 sqlite3ExprDelete(db, pCol->pDflt);
drhb7916a72009-05-27 10:31:29 +0000484 sqlite3DbFree(db, pCol->zDflt);
drh633e6d52008-07-28 19:34:53 +0000485 sqlite3DbFree(db, pCol->zType);
486 sqlite3DbFree(db, pCol->zColl);
drhdd5b2fa2005-03-28 03:39:55 +0000487 }
drh633e6d52008-07-28 19:34:53 +0000488 sqlite3DbFree(db, pTable->aCol);
drh956bc922004-07-24 17:38:29 +0000489 }
drh956bc922004-07-24 17:38:29 +0000490 pTable->aCol = 0;
491 pTable->nCol = 0;
492}
493
494/*
drh75897232000-05-29 14:26:00 +0000495** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000496** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000497**
498** This routine just deletes the data structure. It does not unlink
drhe61922a2009-05-02 13:29:37 +0000499** the table data structure from the hash table. But it does destroy
drhc2eef3b2002-08-31 18:53:06 +0000500** memory structures of the indices and foreign keys associated with
501** the table.
drh75897232000-05-29 14:26:00 +0000502*/
danielk1977a04a34f2007-04-16 15:06:25 +0000503void sqlite3DeleteTable(Table *pTable){
drh75897232000-05-29 14:26:00 +0000504 Index *pIndex, *pNext;
drh633e6d52008-07-28 19:34:53 +0000505 sqlite3 *db;
drhc2eef3b2002-08-31 18:53:06 +0000506
drh75897232000-05-29 14:26:00 +0000507 if( pTable==0 ) return;
drhd9da78a2009-03-24 15:08:09 +0000508 db = pTable->dbMem;
drhc4a64fa2009-05-11 20:53:28 +0000509 testcase( db==0 );
drhc2eef3b2002-08-31 18:53:06 +0000510
drhed8a3bb2005-06-06 21:19:56 +0000511 /* Do not delete the table until the reference count reaches zero. */
512 pTable->nRef--;
513 if( pTable->nRef>0 ){
514 return;
515 }
516 assert( pTable->nRef==0 );
517
drhc2eef3b2002-08-31 18:53:06 +0000518 /* Delete all indices associated with this table
519 */
520 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
521 pNext = pIndex->pNext;
danielk1977da184232006-01-05 11:34:32 +0000522 assert( pIndex->pSchema==pTable->pSchema );
drh03881232009-02-13 03:43:31 +0000523 sqlite3DeleteIndex(pIndex);
drhc2eef3b2002-08-31 18:53:06 +0000524 }
525
dan1da40a32009-09-19 17:00:31 +0000526 /* Delete any foreign keys attached to this table. */
527 sqlite3FkDelete(pTable);
drhc2eef3b2002-08-31 18:53:06 +0000528
529 /* Delete the Table structure itself.
530 */
drh956bc922004-07-24 17:38:29 +0000531 sqliteResetColumnNames(pTable);
drh633e6d52008-07-28 19:34:53 +0000532 sqlite3DbFree(db, pTable->zName);
533 sqlite3DbFree(db, pTable->zColAff);
534 sqlite3SelectDelete(db, pTable->pSelect);
drhffe07b22005-11-03 00:41:17 +0000535#ifndef SQLITE_OMIT_CHECK
drh633e6d52008-07-28 19:34:53 +0000536 sqlite3ExprDelete(db, pTable->pCheck);
drhffe07b22005-11-03 00:41:17 +0000537#endif
drhb9bb7c12006-06-11 23:41:55 +0000538 sqlite3VtabClear(pTable);
drh633e6d52008-07-28 19:34:53 +0000539 sqlite3DbFree(db, pTable);
drh75897232000-05-29 14:26:00 +0000540}
541
542/*
drh5edc3122001-09-13 21:53:09 +0000543** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000544** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000545*/
drh9bb575f2004-09-06 17:24:11 +0000546void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
drh956bc922004-07-24 17:38:29 +0000547 Table *p;
drh956bc922004-07-24 17:38:29 +0000548 Db *pDb;
549
drhd229ca92002-01-09 13:30:41 +0000550 assert( db!=0 );
drh956bc922004-07-24 17:38:29 +0000551 assert( iDb>=0 && iDb<db->nDb );
drh972a2312009-12-08 14:34:08 +0000552 assert( zTabName );
553 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
drh956bc922004-07-24 17:38:29 +0000554 pDb = &db->aDb[iDb];
drhea678832008-12-10 19:26:22 +0000555 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
drha83ccca2009-04-28 13:01:09 +0000556 sqlite3Strlen30(zTabName),0);
drhe61922a2009-05-02 13:29:37 +0000557 sqlite3DeleteTable(p);
drh956bc922004-07-24 17:38:29 +0000558 db->flags |= SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000559}
560
561/*
drha99db3b2004-06-19 14:49:12 +0000562** Given a token, return a string that consists of the text of that
drh24fb6272009-05-01 21:13:36 +0000563** token. Space to hold the returned string
drha99db3b2004-06-19 14:49:12 +0000564** is obtained from sqliteMalloc() and must be freed by the calling
565** function.
drh75897232000-05-29 14:26:00 +0000566**
drh24fb6272009-05-01 21:13:36 +0000567** Any quotation marks (ex: "name", 'name', [name], or `name`) that
568** surround the body of the token are removed.
569**
drhc96d8532005-05-03 12:30:33 +0000570** Tokens are often just pointers into the original SQL text and so
drha99db3b2004-06-19 14:49:12 +0000571** are not \000 terminated and are not persistent. The returned string
572** is \000 terminated and is persistent.
drh75897232000-05-29 14:26:00 +0000573*/
drh17435752007-08-16 04:30:38 +0000574char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
drha99db3b2004-06-19 14:49:12 +0000575 char *zName;
576 if( pName ){
drh17435752007-08-16 04:30:38 +0000577 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
drhb7916a72009-05-27 10:31:29 +0000578 sqlite3Dequote(zName);
drha99db3b2004-06-19 14:49:12 +0000579 }else{
580 zName = 0;
581 }
drh75897232000-05-29 14:26:00 +0000582 return zName;
583}
584
585/*
danielk1977cbb18d22004-05-28 11:37:27 +0000586** Open the sqlite_master table stored in database number iDb for
587** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000588*/
danielk1977c00da102006-01-07 13:21:04 +0000589void sqlite3OpenMasterTable(Parse *p, int iDb){
590 Vdbe *v = sqlite3GetVdbe(p);
591 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
danielk1977207872a2008-01-03 07:54:23 +0000592 sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
danielk1977d336e222009-02-20 10:58:41 +0000593 sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32); /* 5 column table */
danielk19776ab3a2e2009-02-19 14:39:25 +0000594 if( p->nTab==0 ){
595 p->nTab = 1;
596 }
drhe0bc4042002-06-25 01:09:11 +0000597}
598
599/*
danielk197704103022009-02-03 16:51:24 +0000600** Parameter zName points to a nul-terminated buffer containing the name
601** of a database ("main", "temp" or the name of an attached db). This
602** function returns the index of the named database in db->aDb[], or
603** -1 if the named db cannot be found.
danielk1977cbb18d22004-05-28 11:37:27 +0000604*/
danielk197704103022009-02-03 16:51:24 +0000605int sqlite3FindDbName(sqlite3 *db, const char *zName){
606 int i = -1; /* Database number */
drh73c42a12004-11-20 18:13:10 +0000607 if( zName ){
danielk197704103022009-02-03 16:51:24 +0000608 Db *pDb;
609 int n = sqlite3Strlen30(zName);
danielk1977576ec6b2005-01-21 11:55:25 +0000610 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
drhea678832008-12-10 19:26:22 +0000611 if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) &&
danielk197753c0f742005-03-29 03:10:59 +0000612 0==sqlite3StrICmp(pDb->zName, zName) ){
danielk1977576ec6b2005-01-21 11:55:25 +0000613 break;
drh73c42a12004-11-20 18:13:10 +0000614 }
danielk1977cbb18d22004-05-28 11:37:27 +0000615 }
616 }
danielk1977576ec6b2005-01-21 11:55:25 +0000617 return i;
danielk1977cbb18d22004-05-28 11:37:27 +0000618}
619
danielk197704103022009-02-03 16:51:24 +0000620/*
621** The token *pName contains the name of a database (either "main" or
622** "temp" or the name of an attached db). This routine returns the
623** index of the named database in db->aDb[], or -1 if the named db
624** does not exist.
625*/
626int sqlite3FindDb(sqlite3 *db, Token *pName){
627 int i; /* Database number */
628 char *zName; /* Name we are searching for */
629 zName = sqlite3NameFromToken(db, pName);
630 i = sqlite3FindDbName(db, zName);
631 sqlite3DbFree(db, zName);
632 return i;
633}
634
drh0e3d7472004-06-19 17:33:07 +0000635/* The table or view or trigger name is passed to this routine via tokens
636** pName1 and pName2. If the table name was fully qualified, for example:
637**
638** CREATE TABLE xxx.yyy (...);
639**
640** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
641** the table name is not fully qualified, i.e.:
642**
643** CREATE TABLE yyy(...);
644**
645** Then pName1 is set to "yyy" and pName2 is "".
646**
647** This routine sets the *ppUnqual pointer to point at the token (pName1 or
648** pName2) that stores the unqualified table name. The index of the
649** database "xxx" is returned.
650*/
danielk1977ef2cb632004-05-29 02:37:19 +0000651int sqlite3TwoPartName(
drh0e3d7472004-06-19 17:33:07 +0000652 Parse *pParse, /* Parsing and code generating context */
drh90f5ecb2004-07-22 01:19:35 +0000653 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
drh0e3d7472004-06-19 17:33:07 +0000654 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
655 Token **pUnqual /* Write the unqualified object name here */
danielk1977cbb18d22004-05-28 11:37:27 +0000656){
drh0e3d7472004-06-19 17:33:07 +0000657 int iDb; /* Database holding the object */
danielk1977cbb18d22004-05-28 11:37:27 +0000658 sqlite3 *db = pParse->db;
659
drhc4a64fa2009-05-11 20:53:28 +0000660 if( ALWAYS(pName2!=0) && pName2->n>0 ){
shanedcc50b72008-11-13 18:29:50 +0000661 if( db->init.busy ) {
662 sqlite3ErrorMsg(pParse, "corrupt database");
663 pParse->nErr++;
664 return -1;
665 }
danielk1977cbb18d22004-05-28 11:37:27 +0000666 *pUnqual = pName2;
drhff2d5ea2005-07-23 00:41:48 +0000667 iDb = sqlite3FindDb(db, pName1);
danielk1977cbb18d22004-05-28 11:37:27 +0000668 if( iDb<0 ){
669 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
670 pParse->nErr++;
671 return -1;
672 }
673 }else{
674 assert( db->init.iDb==0 || db->init.busy );
675 iDb = db->init.iDb;
676 *pUnqual = pName1;
677 }
678 return iDb;
679}
680
681/*
danielk1977d8123362004-06-12 09:25:12 +0000682** This routine is used to check if the UTF-8 string zName is a legal
683** unqualified name for a new schema object (table, index, view or
684** trigger). All names are legal except those that begin with the string
685** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
686** is reserved for internal use.
687*/
688int sqlite3CheckObjectName(Parse *pParse, const char *zName){
drhf1974842004-11-05 03:56:00 +0000689 if( !pParse->db->init.busy && pParse->nested==0
danielk19773a3f38e2005-05-22 06:49:56 +0000690 && (pParse->db->flags & SQLITE_WriteSchema)==0
drhf1974842004-11-05 03:56:00 +0000691 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
danielk1977d8123362004-06-12 09:25:12 +0000692 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
693 return SQLITE_ERROR;
694 }
695 return SQLITE_OK;
696}
697
698/*
drh75897232000-05-29 14:26:00 +0000699** Begin constructing a new table representation in memory. This is
700** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000701** to a CREATE TABLE statement. In particular, this routine is called
drh74161702006-02-24 02:53:49 +0000702** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000703** flag is true if the table should be stored in the auxiliary database
704** file instead of in the main database file. This is normally the case
705** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000706** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000707**
drhf57b3392001-10-08 13:22:32 +0000708** The new table record is initialized and put in pParse->pNewTable.
709** As more of the CREATE TABLE statement is parsed, additional action
710** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000711** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000712** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000713*/
danielk19774adee202004-05-08 08:23:19 +0000714void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000715 Parse *pParse, /* Parser context */
danielk1977cbb18d22004-05-28 11:37:27 +0000716 Token *pName1, /* First part of the name of the table or view */
717 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000718 int isTemp, /* True if this is a TEMP table */
drhfaa59552005-12-29 23:33:54 +0000719 int isView, /* True if this is a VIEW */
danielk1977f1a381e2006-06-16 08:01:02 +0000720 int isVirtual, /* True if this is a VIRTUAL table */
drhfaa59552005-12-29 23:33:54 +0000721 int noErr /* Do nothing if table already exists */
drhe5f9c642003-01-13 23:27:31 +0000722){
drh75897232000-05-29 14:26:00 +0000723 Table *pTable;
drh23bf66d2004-12-14 03:34:34 +0000724 char *zName = 0; /* The name of the new table */
drh9bb575f2004-09-06 17:24:11 +0000725 sqlite3 *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000726 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000727 int iDb; /* Database number to create the table in */
728 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000729
danielk1977cbb18d22004-05-28 11:37:27 +0000730 /* The table or view name to create is passed to this routine via tokens
731 ** pName1 and pName2. If the table name was fully qualified, for example:
732 **
733 ** CREATE TABLE xxx.yyy (...);
734 **
735 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
736 ** the table name is not fully qualified, i.e.:
737 **
738 ** CREATE TABLE yyy(...);
739 **
740 ** Then pName1 is set to "yyy" and pName2 is "".
741 **
742 ** The call below sets the pName pointer to point at the token (pName1 or
743 ** pName2) that stores the unqualified table name. The variable iDb is
744 ** set to the index of the database that the table or view is to be
745 ** created in.
746 */
danielk1977ef2cb632004-05-29 02:37:19 +0000747 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000748 if( iDb<0 ) return;
danielk197753c0f742005-03-29 03:10:59 +0000749 if( !OMIT_TEMPDB && isTemp && iDb>1 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000750 /* If creating a temp table, the name may not be qualified */
751 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
danielk1977cbb18d22004-05-28 11:37:27 +0000752 return;
753 }
danielk197753c0f742005-03-29 03:10:59 +0000754 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
danielk1977cbb18d22004-05-28 11:37:27 +0000755
756 pParse->sNameToken = *pName;
drh17435752007-08-16 04:30:38 +0000757 zName = sqlite3NameFromToken(db, pName);
danielk1977e0048402004-06-15 16:51:01 +0000758 if( zName==0 ) return;
danielk1977d8123362004-06-12 09:25:12 +0000759 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drh23bf66d2004-12-14 03:34:34 +0000760 goto begin_table_error;
danielk1977d8123362004-06-12 09:25:12 +0000761 }
drh1d85d932004-02-14 23:05:52 +0000762 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000763#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000764 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000765 {
766 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000767 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000768 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drh23bf66d2004-12-14 03:34:34 +0000769 goto begin_table_error;
drhe22a3342003-04-22 20:30:37 +0000770 }
drhe5f9c642003-01-13 23:27:31 +0000771 if( isView ){
danielk197753c0f742005-03-29 03:10:59 +0000772 if( !OMIT_TEMPDB && isTemp ){
drhe5f9c642003-01-13 23:27:31 +0000773 code = SQLITE_CREATE_TEMP_VIEW;
774 }else{
775 code = SQLITE_CREATE_VIEW;
776 }
777 }else{
danielk197753c0f742005-03-29 03:10:59 +0000778 if( !OMIT_TEMPDB && isTemp ){
drhe5f9c642003-01-13 23:27:31 +0000779 code = SQLITE_CREATE_TEMP_TABLE;
780 }else{
781 code = SQLITE_CREATE_TABLE;
782 }
783 }
danielk1977f1a381e2006-06-16 08:01:02 +0000784 if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh23bf66d2004-12-14 03:34:34 +0000785 goto begin_table_error;
drhe5f9c642003-01-13 23:27:31 +0000786 }
787 }
788#endif
drhf57b3392001-10-08 13:22:32 +0000789
drhf57b3392001-10-08 13:22:32 +0000790 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000791 ** index or table name in the same database. Issue an error message if
danielk19777e6ebfb2006-06-12 11:24:37 +0000792 ** it does. The exception is if the statement being parsed was passed
793 ** to an sqlite3_declare_vtab() call. In that case only the column names
794 ** and types will be used, so there is no need to test for namespace
795 ** collisions.
drhf57b3392001-10-08 13:22:32 +0000796 */
danielk19777e6ebfb2006-06-12 11:24:37 +0000797 if( !IN_DECLARE_VTAB ){
798 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
799 goto begin_table_error;
drhfaa59552005-12-29 23:33:54 +0000800 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000801 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
802 if( pTable ){
803 if( !noErr ){
804 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
805 }
806 goto begin_table_error;
807 }
808 if( sqlite3FindIndex(db, zName, 0)!=0 && (iDb==0 || !db->init.busy) ){
809 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
810 goto begin_table_error;
811 }
drh75897232000-05-29 14:26:00 +0000812 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000813
danielk197726783a52007-08-29 14:06:22 +0000814 pTable = sqlite3DbMallocZero(db, sizeof(Table));
drh6d4abfb2001-10-22 02:58:08 +0000815 if( pTable==0 ){
drh17435752007-08-16 04:30:38 +0000816 db->mallocFailed = 1;
danielk1977e0048402004-06-15 16:51:01 +0000817 pParse->rc = SQLITE_NOMEM;
818 pParse->nErr++;
drh23bf66d2004-12-14 03:34:34 +0000819 goto begin_table_error;
drh6d4abfb2001-10-22 02:58:08 +0000820 }
drh75897232000-05-29 14:26:00 +0000821 pTable->zName = zName;
drh4a324312001-12-21 14:30:42 +0000822 pTable->iPKey = -1;
danielk1977da184232006-01-05 11:34:32 +0000823 pTable->pSchema = db->aDb[iDb].pSchema;
drhed8a3bb2005-06-06 21:19:56 +0000824 pTable->nRef = 1;
drhc4a64fa2009-05-11 20:53:28 +0000825 pTable->dbMem = 0;
826 assert( pParse->pNewTable==0 );
drh75897232000-05-29 14:26:00 +0000827 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000828
drh4794f732004-11-05 17:17:50 +0000829 /* If this is the magic sqlite_sequence table used by autoincrement,
830 ** then record a pointer to this table in the main database structure
831 ** so that INSERT can find the table easily.
832 */
833#ifndef SQLITE_OMIT_AUTOINCREMENT
drh78776ec2005-06-14 02:12:46 +0000834 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
danielk1977da184232006-01-05 11:34:32 +0000835 pTable->pSchema->pSeqTab = pTable;
drh4794f732004-11-05 17:17:50 +0000836 }
837#endif
838
drh17f71932002-02-21 12:01:27 +0000839 /* Begin generating the code that will insert the table record into
840 ** the SQLITE_MASTER table. Note in particular that we must go ahead
841 ** and allocate the record number for the table entry now. Before any
842 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
843 ** indices to be created and the table record must come before the
844 ** indices. Hence, the record number for the table must be allocated
845 ** now.
846 */
danielk19774adee202004-05-08 08:23:19 +0000847 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
drhb7654112008-01-12 12:48:07 +0000848 int j1;
drhe321c292006-01-12 01:56:43 +0000849 int fileFormat;
drhb7654112008-01-12 12:48:07 +0000850 int reg1, reg2, reg3;
danielk1977cbb18d22004-05-28 11:37:27 +0000851 sqlite3BeginWriteOperation(pParse, 0, iDb);
drhb17131a2004-11-05 22:18:49 +0000852
danielk197720b1eaf2006-07-26 16:22:14 +0000853#ifndef SQLITE_OMIT_VIRTUALTABLE
854 if( isVirtual ){
drh66a51672008-01-03 00:01:23 +0000855 sqlite3VdbeAddOp0(v, OP_VBegin);
danielk197720b1eaf2006-07-26 16:22:14 +0000856 }
857#endif
858
danielk197736963fd2005-02-19 08:18:05 +0000859 /* If the file format and encoding in the database have not been set,
860 ** set them now.
danielk1977d008cfe2004-06-19 02:22:10 +0000861 */
drhb7654112008-01-12 12:48:07 +0000862 reg1 = pParse->regRowid = ++pParse->nMem;
863 reg2 = pParse->regRoot = ++pParse->nMem;
864 reg3 = ++pParse->nMem;
danielk19770d19f7a2009-06-03 11:25:07 +0000865 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
drhfb982642007-08-30 01:19:59 +0000866 sqlite3VdbeUsesBtree(v, iDb);
drhb7654112008-01-12 12:48:07 +0000867 j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
drhe321c292006-01-12 01:56:43 +0000868 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
drh76fe8032006-07-11 14:17:51 +0000869 1 : SQLITE_MAX_FILE_FORMAT;
drhb7654112008-01-12 12:48:07 +0000870 sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
danielk19770d19f7a2009-06-03 11:25:07 +0000871 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
drhb7654112008-01-12 12:48:07 +0000872 sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
danielk19770d19f7a2009-06-03 11:25:07 +0000873 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
drhb7654112008-01-12 12:48:07 +0000874 sqlite3VdbeJumpHere(v, j1);
danielk1977d008cfe2004-06-19 02:22:10 +0000875
drh4794f732004-11-05 17:17:50 +0000876 /* This just creates a place-holder record in the sqlite_master table.
877 ** The record created does not contain anything yet. It will be replaced
878 ** by the real entry in code generated at sqlite3EndTable().
drhb17131a2004-11-05 22:18:49 +0000879 **
drh0fa991b2009-03-21 16:19:26 +0000880 ** The rowid for the new entry is left in register pParse->regRowid.
881 ** The root page number of the new table is left in reg pParse->regRoot.
882 ** The rowid and root page number values are needed by the code that
883 ** sqlite3EndTable will generate.
drh4794f732004-11-05 17:17:50 +0000884 */
danielk1977f1a381e2006-06-16 08:01:02 +0000885#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
886 if( isView || isVirtual ){
drhb7654112008-01-12 12:48:07 +0000887 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
danielk1977a21c6b62005-01-24 10:25:59 +0000888 }else
889#endif
890 {
drhb7654112008-01-12 12:48:07 +0000891 sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
danielk1977a21c6b62005-01-24 10:25:59 +0000892 }
danielk1977c00da102006-01-07 13:21:04 +0000893 sqlite3OpenMasterTable(pParse, iDb);
drhb7654112008-01-12 12:48:07 +0000894 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
895 sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
896 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
897 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
drh66a51672008-01-03 00:01:23 +0000898 sqlite3VdbeAddOp0(v, OP_Close);
drh5e00f6c2001-09-13 13:46:56 +0000899 }
drh23bf66d2004-12-14 03:34:34 +0000900
901 /* Normal (non-error) return. */
902 return;
903
904 /* If an error occurs, we jump here */
905begin_table_error:
drh633e6d52008-07-28 19:34:53 +0000906 sqlite3DbFree(db, zName);
drh23bf66d2004-12-14 03:34:34 +0000907 return;
drh75897232000-05-29 14:26:00 +0000908}
909
910/*
danielk1977c60e9b82005-01-31 12:42:29 +0000911** This macro is used to compare two strings in a case-insensitive manner.
912** It is slightly faster than calling sqlite3StrICmp() directly, but
913** produces larger code.
914**
915** WARNING: This macro is not compatible with the strcmp() family. It
916** returns true if the two strings are equal, otherwise false.
917*/
918#define STRICMP(x, y) (\
919sqlite3UpperToLower[*(unsigned char *)(x)]== \
920sqlite3UpperToLower[*(unsigned char *)(y)] \
921&& sqlite3StrICmp((x)+1,(y)+1)==0 )
922
923/*
drh75897232000-05-29 14:26:00 +0000924** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000925**
926** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000927** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000928** first to get things going. Then this routine is called for each
929** column.
drh75897232000-05-29 14:26:00 +0000930*/
danielk19774adee202004-05-08 08:23:19 +0000931void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000932 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000933 int i;
drha99db3b2004-06-19 14:49:12 +0000934 char *z;
drhc9b84a12002-06-20 11:36:48 +0000935 Column *pCol;
drhbb4957f2008-03-20 14:03:29 +0000936 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000937 if( (p = pParse->pNewTable)==0 ) return;
drhbb4957f2008-03-20 14:03:29 +0000938#if SQLITE_MAX_COLUMN
939 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
drhe5c941b2007-05-08 13:58:26 +0000940 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
941 return;
942 }
drhbb4957f2008-03-20 14:03:29 +0000943#endif
danielk1977ae74e032008-12-23 11:11:51 +0000944 z = sqlite3NameFromToken(db, pName);
drh97fc3d02002-05-22 21:27:03 +0000945 if( z==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000946 for(i=0; i<p->nCol; i++){
danielk1977c60e9b82005-01-31 12:42:29 +0000947 if( STRICMP(z, p->aCol[i].zName) ){
danielk19774adee202004-05-08 08:23:19 +0000948 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh633e6d52008-07-28 19:34:53 +0000949 sqlite3DbFree(db, z);
drh97fc3d02002-05-22 21:27:03 +0000950 return;
951 }
952 }
drh75897232000-05-29 14:26:00 +0000953 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000954 Column *aNew;
danielk1977ae74e032008-12-23 11:11:51 +0000955 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000956 if( aNew==0 ){
drh633e6d52008-07-28 19:34:53 +0000957 sqlite3DbFree(db, z);
danielk1977d5d56522005-03-16 12:15:20 +0000958 return;
959 }
drh6d4abfb2001-10-22 02:58:08 +0000960 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000961 }
drhc9b84a12002-06-20 11:36:48 +0000962 pCol = &p->aCol[p->nCol];
963 memset(pCol, 0, sizeof(p->aCol[0]));
964 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000965
966 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000967 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
968 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000969 */
danielk19774f057f92004-06-08 00:02:33 +0000970 pCol->affinity = SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +0000971 p->nCol++;
drh75897232000-05-29 14:26:00 +0000972}
973
974/*
drh382c0242001-10-06 16:33:02 +0000975** This routine is called by the parser while in the middle of
976** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
977** been seen on a column. This routine sets the notNull flag on
978** the column currently under construction.
979*/
danielk19774adee202004-05-08 08:23:19 +0000980void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000981 Table *p;
drhc4a64fa2009-05-11 20:53:28 +0000982 p = pParse->pNewTable;
983 if( p==0 || NEVER(p->nCol<1) ) return;
984 p->aCol[p->nCol-1].notNull = (u8)onError;
drh382c0242001-10-06 16:33:02 +0000985}
986
987/*
danielk197752a83fb2005-01-31 12:56:44 +0000988** Scan the column type name zType (length nType) and return the
989** associated affinity type.
danielk1977b3dff962005-02-01 01:21:55 +0000990**
991** This routine does a case-independent search of zType for the
992** substrings in the following table. If one of the substrings is
993** found, the corresponding affinity is returned. If zType contains
994** more than one of the substrings, entries toward the top of
995** the table take priority. For example, if zType is 'BLOBINT',
drh8a512562005-11-14 22:29:05 +0000996** SQLITE_AFF_INTEGER is returned.
danielk1977b3dff962005-02-01 01:21:55 +0000997**
998** Substring | Affinity
999** --------------------------------
1000** 'INT' | SQLITE_AFF_INTEGER
1001** 'CHAR' | SQLITE_AFF_TEXT
1002** 'CLOB' | SQLITE_AFF_TEXT
1003** 'TEXT' | SQLITE_AFF_TEXT
1004** 'BLOB' | SQLITE_AFF_NONE
drh8a512562005-11-14 22:29:05 +00001005** 'REAL' | SQLITE_AFF_REAL
1006** 'FLOA' | SQLITE_AFF_REAL
1007** 'DOUB' | SQLITE_AFF_REAL
danielk1977b3dff962005-02-01 01:21:55 +00001008**
1009** If none of the substrings in the above table are found,
1010** SQLITE_AFF_NUMERIC is returned.
danielk197752a83fb2005-01-31 12:56:44 +00001011*/
drhb7916a72009-05-27 10:31:29 +00001012char sqlite3AffinityType(const char *zIn){
danielk1977b3dff962005-02-01 01:21:55 +00001013 u32 h = 0;
1014 char aff = SQLITE_AFF_NUMERIC;
danielk197752a83fb2005-01-31 12:56:44 +00001015
drhb7916a72009-05-27 10:31:29 +00001016 if( zIn ) while( zIn[0] ){
1017 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
danielk1977b3dff962005-02-01 01:21:55 +00001018 zIn++;
danielk1977201f7162005-02-01 02:13:29 +00001019 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
1020 aff = SQLITE_AFF_TEXT;
1021 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
1022 aff = SQLITE_AFF_TEXT;
1023 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
1024 aff = SQLITE_AFF_TEXT;
1025 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
drh8a512562005-11-14 22:29:05 +00001026 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
danielk1977b3dff962005-02-01 01:21:55 +00001027 aff = SQLITE_AFF_NONE;
drh8a512562005-11-14 22:29:05 +00001028#ifndef SQLITE_OMIT_FLOATING_POINT
1029 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
1030 && aff==SQLITE_AFF_NUMERIC ){
1031 aff = SQLITE_AFF_REAL;
1032 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
1033 && aff==SQLITE_AFF_NUMERIC ){
1034 aff = SQLITE_AFF_REAL;
1035 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
1036 && aff==SQLITE_AFF_NUMERIC ){
1037 aff = SQLITE_AFF_REAL;
1038#endif
danielk1977201f7162005-02-01 02:13:29 +00001039 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
drh8a512562005-11-14 22:29:05 +00001040 aff = SQLITE_AFF_INTEGER;
danielk1977b3dff962005-02-01 01:21:55 +00001041 break;
danielk197752a83fb2005-01-31 12:56:44 +00001042 }
1043 }
danielk1977b3dff962005-02-01 01:21:55 +00001044
1045 return aff;
danielk197752a83fb2005-01-31 12:56:44 +00001046}
1047
1048/*
drh382c0242001-10-06 16:33:02 +00001049** This routine is called by the parser while in the middle of
1050** parsing a CREATE TABLE statement. The pFirst token is the first
1051** token in the sequence of tokens that describe the type of the
1052** column currently under construction. pLast is the last token
1053** in the sequence. Use this information to construct a string
1054** that contains the typename of the column and store that string
1055** in zType.
1056*/
drh487e2622005-06-25 18:42:14 +00001057void sqlite3AddColumnType(Parse *pParse, Token *pType){
drh382c0242001-10-06 16:33:02 +00001058 Table *p;
drhc9b84a12002-06-20 11:36:48 +00001059 Column *pCol;
drh487e2622005-06-25 18:42:14 +00001060
drhc4a64fa2009-05-11 20:53:28 +00001061 p = pParse->pNewTable;
1062 if( p==0 || NEVER(p->nCol<1) ) return;
1063 pCol = &p->aCol[p->nCol-1];
1064 assert( pCol->zType==0 );
1065 pCol->zType = sqlite3NameFromToken(pParse->db, pType);
drhb7916a72009-05-27 10:31:29 +00001066 pCol->affinity = sqlite3AffinityType(pCol->zType);
drh382c0242001-10-06 16:33:02 +00001067}
1068
1069/*
danielk19777977a172004-11-09 12:44:37 +00001070** The expression is the default value for the most recently added column
1071** of the table currently under construction.
1072**
1073** Default value expressions must be constant. Raise an exception if this
1074** is not the case.
drhd9b02572001-04-15 00:37:09 +00001075**
1076** This routine is called by the parser while in the middle of
1077** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +00001078*/
drhb7916a72009-05-27 10:31:29 +00001079void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
drh7020f652000-06-03 18:06:52 +00001080 Table *p;
danielk19777977a172004-11-09 12:44:37 +00001081 Column *pCol;
drh633e6d52008-07-28 19:34:53 +00001082 sqlite3 *db = pParse->db;
drhc4a64fa2009-05-11 20:53:28 +00001083 p = pParse->pNewTable;
1084 if( p!=0 ){
drh42b9d7c2005-08-13 00:56:27 +00001085 pCol = &(p->aCol[p->nCol-1]);
drhb7916a72009-05-27 10:31:29 +00001086 if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
drh42b9d7c2005-08-13 00:56:27 +00001087 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
1088 pCol->zName);
1089 }else{
danielk19776ab3a2e2009-02-19 14:39:25 +00001090 /* A copy of pExpr is used instead of the original, as pExpr contains
1091 ** tokens that point to volatile memory. The 'span' of the expression
1092 ** is required by pragma table_info.
1093 */
drh633e6d52008-07-28 19:34:53 +00001094 sqlite3ExprDelete(db, pCol->pDflt);
drhb7916a72009-05-27 10:31:29 +00001095 pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
1096 sqlite3DbFree(db, pCol->zDflt);
1097 pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
shanecf697392009-06-01 16:53:09 +00001098 (int)(pSpan->zEnd - pSpan->zStart));
drh42b9d7c2005-08-13 00:56:27 +00001099 }
danielk19777977a172004-11-09 12:44:37 +00001100 }
drhb7916a72009-05-27 10:31:29 +00001101 sqlite3ExprDelete(db, pSpan->pExpr);
drh7020f652000-06-03 18:06:52 +00001102}
1103
1104/*
drh4a324312001-12-21 14:30:42 +00001105** Designate the PRIMARY KEY for the table. pList is a list of names
1106** of columns that form the primary key. If pList is NULL, then the
1107** most recently added column of the table is the primary key.
1108**
1109** A table can have at most one primary key. If the table already has
1110** a primary key (and this is the second primary key) then create an
1111** error.
1112**
1113** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
drh23bf66d2004-12-14 03:34:34 +00001114** then we will try to use that column as the rowid. Set the Table.iPKey
drh4a324312001-12-21 14:30:42 +00001115** field of the table under construction to be the index of the
1116** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
1117** no INTEGER PRIMARY KEY.
1118**
1119** If the key is not an INTEGER PRIMARY KEY, then create a unique
1120** index for the key. No index is created for INTEGER PRIMARY KEYs.
1121*/
drh205f48e2004-11-05 00:43:11 +00001122void sqlite3AddPrimaryKey(
1123 Parse *pParse, /* Parsing context */
1124 ExprList *pList, /* List of field names to be indexed */
1125 int onError, /* What to do with a uniqueness conflict */
drhfdd6e852005-12-16 01:06:16 +00001126 int autoInc, /* True if the AUTOINCREMENT keyword is present */
1127 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
drh205f48e2004-11-05 00:43:11 +00001128){
drh4a324312001-12-21 14:30:42 +00001129 Table *pTab = pParse->pNewTable;
1130 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +00001131 int iCol = -1, i;
danielk1977c7d54102006-06-15 07:29:00 +00001132 if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
drh7d10d5a2008-08-20 16:35:10 +00001133 if( pTab->tabFlags & TF_HasPrimaryKey ){
danielk19774adee202004-05-08 08:23:19 +00001134 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001135 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +00001136 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +00001137 }
drh7d10d5a2008-08-20 16:35:10 +00001138 pTab->tabFlags |= TF_HasPrimaryKey;
drh4a324312001-12-21 14:30:42 +00001139 if( pList==0 ){
1140 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +00001141 pTab->aCol[iCol].isPrimKey = 1;
1142 }else{
danielk19770202b292004-06-09 09:55:16 +00001143 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +00001144 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +00001145 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
1146 break;
1147 }
drh78100cc2003-08-23 22:40:53 +00001148 }
drh6e4fc2c2005-09-15 21:24:51 +00001149 if( iCol<pTab->nCol ){
drh688c9f02005-09-16 02:48:01 +00001150 pTab->aCol[iCol].isPrimKey = 1;
drh6e4fc2c2005-09-15 21:24:51 +00001151 }
drh4a324312001-12-21 14:30:42 +00001152 }
danielk19770202b292004-06-09 09:55:16 +00001153 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +00001154 }
1155 if( iCol>=0 && iCol<pTab->nCol ){
1156 zType = pTab->aCol[iCol].zType;
1157 }
drhfdd6e852005-12-16 01:06:16 +00001158 if( zType && sqlite3StrICmp(zType, "INTEGER")==0
1159 && sortOrder==SQLITE_SO_ASC ){
drh4a324312001-12-21 14:30:42 +00001160 pTab->iPKey = iCol;
drh1bd10f82008-12-10 21:19:56 +00001161 pTab->keyConf = (u8)onError;
drh7d10d5a2008-08-20 16:35:10 +00001162 assert( autoInc==0 || autoInc==1 );
1163 pTab->tabFlags |= autoInc*TF_Autoincrement;
drh205f48e2004-11-05 00:43:11 +00001164 }else if( autoInc ){
drh4794f732004-11-05 17:17:50 +00001165#ifndef SQLITE_OMIT_AUTOINCREMENT
drh205f48e2004-11-05 00:43:11 +00001166 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
1167 "INTEGER PRIMARY KEY");
drh4794f732004-11-05 17:17:50 +00001168#endif
drh4a324312001-12-21 14:30:42 +00001169 }else{
dan1da40a32009-09-19 17:00:31 +00001170 Index *p;
1171 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
1172 if( p ){
1173 p->autoIndex = 2;
1174 }
drhe0194f22003-02-26 13:52:51 +00001175 pList = 0;
drh4a324312001-12-21 14:30:42 +00001176 }
drhe0194f22003-02-26 13:52:51 +00001177
1178primary_key_exit:
drh633e6d52008-07-28 19:34:53 +00001179 sqlite3ExprListDelete(pParse->db, pList);
drhe0194f22003-02-26 13:52:51 +00001180 return;
drh4a324312001-12-21 14:30:42 +00001181}
1182
1183/*
drhffe07b22005-11-03 00:41:17 +00001184** Add a new CHECK constraint to the table currently under construction.
1185*/
1186void sqlite3AddCheckConstraint(
1187 Parse *pParse, /* Parsing context */
1188 Expr *pCheckExpr /* The check expression */
1189){
drh633e6d52008-07-28 19:34:53 +00001190 sqlite3 *db = pParse->db;
drhffe07b22005-11-03 00:41:17 +00001191#ifndef SQLITE_OMIT_CHECK
1192 Table *pTab = pParse->pNewTable;
danielk1977c7d54102006-06-15 07:29:00 +00001193 if( pTab && !IN_DECLARE_VTAB ){
drh33e619f2009-05-28 01:00:55 +00001194 pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr);
1195 }else
drhffe07b22005-11-03 00:41:17 +00001196#endif
drh33e619f2009-05-28 01:00:55 +00001197 {
1198 sqlite3ExprDelete(db, pCheckExpr);
1199 }
drhffe07b22005-11-03 00:41:17 +00001200}
1201
1202/*
drhd3d39e92004-05-20 22:16:29 +00001203** Set the collation function of the most recently parsed table column
1204** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +00001205*/
danielk197739002502007-11-12 09:50:26 +00001206void sqlite3AddCollateType(Parse *pParse, Token *pToken){
drh8e2ca022002-06-17 17:07:19 +00001207 Table *p;
danielk19770202b292004-06-09 09:55:16 +00001208 int i;
danielk197739002502007-11-12 09:50:26 +00001209 char *zColl; /* Dequoted name of collation sequence */
drh633e6d52008-07-28 19:34:53 +00001210 sqlite3 *db;
danielk1977a37cdde2004-05-16 11:15:36 +00001211
danielk1977b8cbb872006-06-19 05:33:45 +00001212 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +00001213 i = p->nCol-1;
drh633e6d52008-07-28 19:34:53 +00001214 db = pParse->db;
1215 zColl = sqlite3NameFromToken(db, pToken);
danielk197739002502007-11-12 09:50:26 +00001216 if( !zColl ) return;
1217
drhc4a64fa2009-05-11 20:53:28 +00001218 if( sqlite3LocateCollSeq(pParse, zColl) ){
danielk1977b3bf5562006-01-10 17:58:23 +00001219 Index *pIdx;
danielk197739002502007-11-12 09:50:26 +00001220 p->aCol[i].zColl = zColl;
danielk1977b3bf5562006-01-10 17:58:23 +00001221
1222 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
1223 ** then an index may have been created on this column before the
1224 ** collation type was added. Correct this if it is the case.
1225 */
1226 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
1227 assert( pIdx->nColumn==1 );
1228 if( pIdx->aiColumn[0]==i ){
1229 pIdx->azColl[0] = p->aCol[i].zColl;
danielk19777cedc8d2004-06-10 10:50:08 +00001230 }
1231 }
danielk197739002502007-11-12 09:50:26 +00001232 }else{
drh633e6d52008-07-28 19:34:53 +00001233 sqlite3DbFree(db, zColl);
danielk19777cedc8d2004-06-10 10:50:08 +00001234 }
danielk19777cedc8d2004-06-10 10:50:08 +00001235}
1236
danielk1977466be562004-06-10 02:16:01 +00001237/*
1238** This function returns the collation sequence for database native text
1239** encoding identified by the string zName, length nName.
1240**
1241** If the requested collation sequence is not available, or not available
1242** in the database native encoding, the collation factory is invoked to
1243** request it. If the collation factory does not supply such a sequence,
1244** and the sequence is available in another text encoding, then that is
1245** returned instead.
1246**
1247** If no versions of the requested collations sequence are available, or
1248** another error occurs, NULL is returned and an error message written into
1249** pParse.
drha34001c2007-02-02 12:44:37 +00001250**
1251** This routine is a wrapper around sqlite3FindCollSeq(). This routine
1252** invokes the collation factory if the named collation cannot be found
1253** and generates an error message.
drhc4a64fa2009-05-11 20:53:28 +00001254**
1255** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
danielk1977466be562004-06-10 02:16:01 +00001256*/
drhc4a64fa2009-05-11 20:53:28 +00001257CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
danielk19774dade032005-05-25 10:45:10 +00001258 sqlite3 *db = pParse->db;
danielk197714db2662006-01-09 16:12:04 +00001259 u8 enc = ENC(db);
danielk19774dade032005-05-25 10:45:10 +00001260 u8 initbusy = db->init.busy;
danielk1977b3bf5562006-01-10 17:58:23 +00001261 CollSeq *pColl;
danielk19774dade032005-05-25 10:45:10 +00001262
drhc4a64fa2009-05-11 20:53:28 +00001263 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
danielk19777cedc8d2004-06-10 10:50:08 +00001264 if( !initbusy && (!pColl || !pColl->xCmp) ){
drh9aeda792009-08-20 02:34:15 +00001265 pColl = sqlite3GetCollSeq(db, enc, pColl, zName);
danielk19774dade032005-05-25 10:45:10 +00001266 if( !pColl ){
drhc4a64fa2009-05-11 20:53:28 +00001267 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
danielk1977466be562004-06-10 02:16:01 +00001268 }
1269 }
1270
danielk19770202b292004-06-09 09:55:16 +00001271 return pColl;
1272}
1273
1274
drh8e2ca022002-06-17 17:07:19 +00001275/*
drh3f7d4e42004-07-24 14:35:58 +00001276** Generate code that will increment the schema cookie.
drh50e5dad2001-09-15 00:57:28 +00001277**
1278** The schema cookie is used to determine when the schema for the
1279** database changes. After each schema change, the cookie value
1280** changes. When a process first reads the schema it records the
1281** cookie. Thereafter, whenever it goes to access the database,
1282** it checks the cookie to make sure the schema has not changed
1283** since it was last read.
1284**
1285** This plan is not completely bullet-proof. It is possible for
1286** the schema to change multiple times and for the cookie to be
1287** set back to prior value. But schema changes are infrequent
1288** and the probability of hitting the same cookie value is only
1289** 1 chance in 2^32. So we're safe enough.
1290*/
drh9cbf3422008-01-17 16:22:13 +00001291void sqlite3ChangeCookie(Parse *pParse, int iDb){
1292 int r1 = sqlite3GetTempReg(pParse);
1293 sqlite3 *db = pParse->db;
1294 Vdbe *v = pParse->pVdbe;
1295 sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
danielk19770d19f7a2009-06-03 11:25:07 +00001296 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
drh9cbf3422008-01-17 16:22:13 +00001297 sqlite3ReleaseTempReg(pParse, r1);
drh50e5dad2001-09-15 00:57:28 +00001298}
1299
1300/*
drh969fa7c2002-02-18 18:30:32 +00001301** Measure the number of characters needed to output the given
1302** identifier. The number returned includes any quotes used
1303** but does not include the null terminator.
drh234c39d2004-07-24 03:30:47 +00001304**
1305** The estimate is conservative. It might be larger that what is
1306** really needed.
drh969fa7c2002-02-18 18:30:32 +00001307*/
1308static int identLength(const char *z){
1309 int n;
drh17f71932002-02-21 12:01:27 +00001310 for(n=0; *z; n++, z++){
drh234c39d2004-07-24 03:30:47 +00001311 if( *z=='"' ){ n++; }
drh969fa7c2002-02-18 18:30:32 +00001312 }
drh234c39d2004-07-24 03:30:47 +00001313 return n + 2;
drh969fa7c2002-02-18 18:30:32 +00001314}
1315
1316/*
danielk19771b870de2009-03-14 08:37:23 +00001317** The first parameter is a pointer to an output buffer. The second
1318** parameter is a pointer to an integer that contains the offset at
1319** which to write into the output buffer. This function copies the
1320** nul-terminated string pointed to by the third parameter, zSignedIdent,
1321** to the specified offset in the buffer and updates *pIdx to refer
1322** to the first byte after the last byte written before returning.
1323**
1324** If the string zSignedIdent consists entirely of alpha-numeric
1325** characters, does not begin with a digit and is not an SQL keyword,
1326** then it is copied to the output buffer exactly as it is. Otherwise,
1327** it is quoted using double-quotes.
1328*/
drhc4a64fa2009-05-11 20:53:28 +00001329static void identPut(char *z, int *pIdx, char *zSignedIdent){
drh4c755c02004-08-08 20:22:17 +00001330 unsigned char *zIdent = (unsigned char*)zSignedIdent;
drh17f71932002-02-21 12:01:27 +00001331 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001332 i = *pIdx;
danielk19771b870de2009-03-14 08:37:23 +00001333
drh17f71932002-02-21 12:01:27 +00001334 for(j=0; zIdent[j]; j++){
danielk197778ca0e72009-01-20 16:53:39 +00001335 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
drh17f71932002-02-21 12:01:27 +00001336 }
danielk19771b870de2009-03-14 08:37:23 +00001337 needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
1338 if( !needQuote ){
drhc4a64fa2009-05-11 20:53:28 +00001339 needQuote = zIdent[j];
danielk19771b870de2009-03-14 08:37:23 +00001340 }
1341
drh234c39d2004-07-24 03:30:47 +00001342 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001343 for(j=0; zIdent[j]; j++){
1344 z[i++] = zIdent[j];
drh234c39d2004-07-24 03:30:47 +00001345 if( zIdent[j]=='"' ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001346 }
drh234c39d2004-07-24 03:30:47 +00001347 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001348 z[i] = 0;
1349 *pIdx = i;
1350}
1351
1352/*
1353** Generate a CREATE TABLE statement appropriate for the given
1354** table. Memory to hold the text of the statement is obtained
1355** from sqliteMalloc() and must be freed by the calling function.
1356*/
drh1d34fde2009-02-03 15:50:33 +00001357static char *createTableStmt(sqlite3 *db, Table *p){
drh969fa7c2002-02-18 18:30:32 +00001358 int i, k, n;
1359 char *zStmt;
drhc4a64fa2009-05-11 20:53:28 +00001360 char *zSep, *zSep2, *zEnd;
drh234c39d2004-07-24 03:30:47 +00001361 Column *pCol;
drh969fa7c2002-02-18 18:30:32 +00001362 n = 0;
drh234c39d2004-07-24 03:30:47 +00001363 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
drhc4a64fa2009-05-11 20:53:28 +00001364 n += identLength(pCol->zName) + 5;
drh969fa7c2002-02-18 18:30:32 +00001365 }
1366 n += identLength(p->zName);
drhc4a64fa2009-05-11 20:53:28 +00001367 if( n<50 ){
drh969fa7c2002-02-18 18:30:32 +00001368 zSep = "";
1369 zSep2 = ",";
1370 zEnd = ")";
1371 }else{
1372 zSep = "\n ";
1373 zSep2 = ",\n ";
1374 zEnd = "\n)";
1375 }
drhe0bc4042002-06-25 01:09:11 +00001376 n += 35 + 6*p->nCol;
drhe5ae5732008-06-15 02:51:47 +00001377 zStmt = sqlite3Malloc( n );
drh820a9062008-01-31 13:35:48 +00001378 if( zStmt==0 ){
1379 db->mallocFailed = 1;
1380 return 0;
1381 }
drhbdb339f2009-02-02 18:03:21 +00001382 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
drhea678832008-12-10 19:26:22 +00001383 k = sqlite3Strlen30(zStmt);
drhc4a64fa2009-05-11 20:53:28 +00001384 identPut(zStmt, &k, p->zName);
drh969fa7c2002-02-18 18:30:32 +00001385 zStmt[k++] = '(';
drh234c39d2004-07-24 03:30:47 +00001386 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
drhc4a64fa2009-05-11 20:53:28 +00001387 static const char * const azType[] = {
1388 /* SQLITE_AFF_TEXT */ " TEXT",
1389 /* SQLITE_AFF_NONE */ "",
1390 /* SQLITE_AFF_NUMERIC */ " NUM",
1391 /* SQLITE_AFF_INTEGER */ " INT",
1392 /* SQLITE_AFF_REAL */ " REAL"
1393 };
1394 int len;
1395 const char *zType;
1396
drh5bb3eb92007-05-04 13:15:55 +00001397 sqlite3_snprintf(n-k, &zStmt[k], zSep);
drhea678832008-12-10 19:26:22 +00001398 k += sqlite3Strlen30(&zStmt[k]);
drh969fa7c2002-02-18 18:30:32 +00001399 zSep = zSep2;
drhc4a64fa2009-05-11 20:53:28 +00001400 identPut(zStmt, &k, pCol->zName);
1401 assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
1402 assert( pCol->affinity-SQLITE_AFF_TEXT < sizeof(azType)/sizeof(azType[0]) );
1403 testcase( pCol->affinity==SQLITE_AFF_TEXT );
1404 testcase( pCol->affinity==SQLITE_AFF_NONE );
1405 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
1406 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
1407 testcase( pCol->affinity==SQLITE_AFF_REAL );
1408
1409 zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
1410 len = sqlite3Strlen30(zType);
drhb7916a72009-05-27 10:31:29 +00001411 assert( pCol->affinity==SQLITE_AFF_NONE
1412 || pCol->affinity==sqlite3AffinityType(zType) );
drhc4a64fa2009-05-11 20:53:28 +00001413 memcpy(&zStmt[k], zType, len);
1414 k += len;
1415 assert( k<=n );
drh969fa7c2002-02-18 18:30:32 +00001416 }
drh5bb3eb92007-05-04 13:15:55 +00001417 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
drh969fa7c2002-02-18 18:30:32 +00001418 return zStmt;
1419}
1420
1421/*
drh75897232000-05-29 14:26:00 +00001422** This routine is called to report the final ")" that terminates
1423** a CREATE TABLE statement.
1424**
drhf57b3392001-10-08 13:22:32 +00001425** The table structure that other action routines have been building
1426** is added to the internal hash tables, assuming no errors have
1427** occurred.
drh75897232000-05-29 14:26:00 +00001428**
drh1d85d932004-02-14 23:05:52 +00001429** An entry for the table is made in the master table on disk, unless
1430** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001431** it means we are reading the sqlite_master table because we just
1432** connected to the database or because the sqlite_master table has
drhddba9e52005-03-19 01:41:21 +00001433** recently changed, so the entry for this table already exists in
drhf57b3392001-10-08 13:22:32 +00001434** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001435**
1436** If the pSelect argument is not NULL, it means that this routine
1437** was called to create a table generated from a
1438** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1439** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001440*/
danielk197719a8e7e2005-03-17 05:03:38 +00001441void sqlite3EndTable(
1442 Parse *pParse, /* Parse context */
1443 Token *pCons, /* The ',' token after the last column defn. */
1444 Token *pEnd, /* The final ')' token in the CREATE TABLE */
1445 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
1446){
drh75897232000-05-29 14:26:00 +00001447 Table *p;
drh9bb575f2004-09-06 17:24:11 +00001448 sqlite3 *db = pParse->db;
danielk1977da184232006-01-05 11:34:32 +00001449 int iDb;
drh75897232000-05-29 14:26:00 +00001450
drh8af73d42009-05-13 22:58:28 +00001451 if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001452 return;
1453 }
drh28037572000-08-02 13:47:41 +00001454 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001455 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001456
danielk1977517eb642004-06-07 10:00:31 +00001457 assert( !db->init.busy || !pSelect );
1458
drhb9bb7c12006-06-11 23:41:55 +00001459 iDb = sqlite3SchemaToIndex(db, p->pSchema);
danielk1977da184232006-01-05 11:34:32 +00001460
drhffe07b22005-11-03 00:41:17 +00001461#ifndef SQLITE_OMIT_CHECK
1462 /* Resolve names in all CHECK constraint expressions.
1463 */
1464 if( p->pCheck ){
1465 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
1466 NameContext sNC; /* Name context for pParse->pNewTable */
1467
1468 memset(&sNC, 0, sizeof(sNC));
1469 memset(&sSrc, 0, sizeof(sSrc));
1470 sSrc.nSrc = 1;
1471 sSrc.a[0].zName = p->zName;
1472 sSrc.a[0].pTab = p;
1473 sSrc.a[0].iCursor = -1;
1474 sNC.pParse = pParse;
1475 sNC.pSrcList = &sSrc;
drh06f65412005-11-03 02:03:13 +00001476 sNC.isCheck = 1;
drh7d10d5a2008-08-20 16:35:10 +00001477 if( sqlite3ResolveExprNames(&sNC, p->pCheck) ){
drhffe07b22005-11-03 00:41:17 +00001478 return;
1479 }
1480 }
1481#endif /* !defined(SQLITE_OMIT_CHECK) */
1482
drh1d85d932004-02-14 23:05:52 +00001483 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001484 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1485 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001486 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001487 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001488 */
drh1d85d932004-02-14 23:05:52 +00001489 if( db->init.busy ){
1490 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001491 }
1492
drhe3c41372001-09-17 20:25:58 +00001493 /* If not initializing, then create a record for the new table
drh0fa991b2009-03-21 16:19:26 +00001494 ** in the SQLITE_MASTER table of the database.
drhf57b3392001-10-08 13:22:32 +00001495 **
drhe0bc4042002-06-25 01:09:11 +00001496 ** If this is a TEMPORARY table, write the entry into the auxiliary
1497 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001498 */
drh1d85d932004-02-14 23:05:52 +00001499 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001500 int n;
drhd8bc7082000-06-07 23:51:50 +00001501 Vdbe *v;
drh4794f732004-11-05 17:17:50 +00001502 char *zType; /* "view" or "table" */
1503 char *zType2; /* "VIEW" or "TABLE" */
1504 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
drh75897232000-05-29 14:26:00 +00001505
danielk19774adee202004-05-08 08:23:19 +00001506 v = sqlite3GetVdbe(pParse);
drh768578e2009-05-12 00:40:12 +00001507 if( NEVER(v==0) ) return;
danielk1977517eb642004-06-07 10:00:31 +00001508
drh66a51672008-01-03 00:01:23 +00001509 sqlite3VdbeAddOp1(v, OP_Close, 0);
danielk1977e6efa742004-11-10 11:55:10 +00001510
drh0fa991b2009-03-21 16:19:26 +00001511 /*
1512 ** Initialize zType for the new view or table.
drh4794f732004-11-05 17:17:50 +00001513 */
drh4ff6dfa2002-03-03 23:06:00 +00001514 if( p->pSelect==0 ){
1515 /* A regular table */
drh4794f732004-11-05 17:17:50 +00001516 zType = "table";
1517 zType2 = "TABLE";
danielk1977576ec6b2005-01-21 11:55:25 +00001518#ifndef SQLITE_OMIT_VIEW
drh4ff6dfa2002-03-03 23:06:00 +00001519 }else{
1520 /* A view */
drh4794f732004-11-05 17:17:50 +00001521 zType = "view";
1522 zType2 = "VIEW";
danielk1977576ec6b2005-01-21 11:55:25 +00001523#endif
drh4ff6dfa2002-03-03 23:06:00 +00001524 }
danielk1977517eb642004-06-07 10:00:31 +00001525
danielk1977517eb642004-06-07 10:00:31 +00001526 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1527 ** statement to populate the new table. The root-page number for the
drh0fa991b2009-03-21 16:19:26 +00001528 ** new table is in register pParse->regRoot.
danielk1977517eb642004-06-07 10:00:31 +00001529 **
1530 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1531 ** suitable state to query for the column names and types to be used
1532 ** by the new table.
danielk1977c00da102006-01-07 13:21:04 +00001533 **
1534 ** A shared-cache write-lock is not required to write to the new table,
1535 ** as a schema-lock must have already been obtained to create it. Since
1536 ** a schema-lock excludes all other database users, the write-lock would
1537 ** be redundant.
danielk1977517eb642004-06-07 10:00:31 +00001538 */
1539 if( pSelect ){
drh1013c932008-01-06 00:25:21 +00001540 SelectDest dest;
danielk1977517eb642004-06-07 10:00:31 +00001541 Table *pSelTab;
drh1013c932008-01-06 00:25:21 +00001542
danielk19776ab3a2e2009-02-19 14:39:25 +00001543 assert(pParse->nTab==1);
drhb7654112008-01-12 12:48:07 +00001544 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
drh98757152008-01-09 23:04:12 +00001545 sqlite3VdbeChangeP5(v, 1);
danielk1977517eb642004-06-07 10:00:31 +00001546 pParse->nTab = 2;
drh1013c932008-01-06 00:25:21 +00001547 sqlite3SelectDestInit(&dest, SRT_Table, 1);
drh7d10d5a2008-08-20 16:35:10 +00001548 sqlite3Select(pParse, pSelect, &dest);
drh66a51672008-01-03 00:01:23 +00001549 sqlite3VdbeAddOp1(v, OP_Close, 1);
danielk1977517eb642004-06-07 10:00:31 +00001550 if( pParse->nErr==0 ){
drh7d10d5a2008-08-20 16:35:10 +00001551 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
danielk1977517eb642004-06-07 10:00:31 +00001552 if( pSelTab==0 ) return;
1553 assert( p->aCol==0 );
1554 p->nCol = pSelTab->nCol;
1555 p->aCol = pSelTab->aCol;
1556 pSelTab->nCol = 0;
1557 pSelTab->aCol = 0;
danielk1977a04a34f2007-04-16 15:06:25 +00001558 sqlite3DeleteTable(pSelTab);
danielk1977517eb642004-06-07 10:00:31 +00001559 }
1560 }
drh4794f732004-11-05 17:17:50 +00001561
drh4794f732004-11-05 17:17:50 +00001562 /* Compute the complete text of the CREATE statement */
1563 if( pSelect ){
drh1d34fde2009-02-03 15:50:33 +00001564 zStmt = createTableStmt(db, p);
drh4794f732004-11-05 17:17:50 +00001565 }else{
drh1bd10f82008-12-10 21:19:56 +00001566 n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
danielk19771e536952007-08-16 10:09:01 +00001567 zStmt = sqlite3MPrintf(db,
1568 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
1569 );
drh4794f732004-11-05 17:17:50 +00001570 }
1571
1572 /* A slot for the record has already been allocated in the
1573 ** SQLITE_MASTER table. We just need to update that slot with all
drh0fa991b2009-03-21 16:19:26 +00001574 ** the information we've collected.
drh4794f732004-11-05 17:17:50 +00001575 */
1576 sqlite3NestedParse(pParse,
1577 "UPDATE %Q.%s "
drhb7654112008-01-12 12:48:07 +00001578 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
1579 "WHERE rowid=#%d",
danielk1977da184232006-01-05 11:34:32 +00001580 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
drh4794f732004-11-05 17:17:50 +00001581 zType,
1582 p->zName,
1583 p->zName,
drhb7654112008-01-12 12:48:07 +00001584 pParse->regRoot,
1585 zStmt,
1586 pParse->regRowid
drh4794f732004-11-05 17:17:50 +00001587 );
drh633e6d52008-07-28 19:34:53 +00001588 sqlite3DbFree(db, zStmt);
drh9cbf3422008-01-17 16:22:13 +00001589 sqlite3ChangeCookie(pParse, iDb);
drh2958a4e2004-11-12 03:56:15 +00001590
1591#ifndef SQLITE_OMIT_AUTOINCREMENT
1592 /* Check to see if we need to create an sqlite_sequence table for
1593 ** keeping track of autoincrement keys.
1594 */
drh7d10d5a2008-08-20 16:35:10 +00001595 if( p->tabFlags & TF_Autoincrement ){
danielk1977da184232006-01-05 11:34:32 +00001596 Db *pDb = &db->aDb[iDb];
1597 if( pDb->pSchema->pSeqTab==0 ){
drh2958a4e2004-11-12 03:56:15 +00001598 sqlite3NestedParse(pParse,
drhf3388142004-11-13 03:48:06 +00001599 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
1600 pDb->zName
drh2958a4e2004-11-12 03:56:15 +00001601 );
1602 }
1603 }
1604#endif
drh4794f732004-11-05 17:17:50 +00001605
1606 /* Reparse everything to update our internal data structures */
drh66a51672008-01-03 00:01:23 +00001607 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0,
1608 sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P4_DYNAMIC);
drh75897232000-05-29 14:26:00 +00001609 }
drh17e9e292003-02-01 13:53:28 +00001610
drh2958a4e2004-11-12 03:56:15 +00001611
drh17e9e292003-02-01 13:53:28 +00001612 /* Add the table to the in-memory representation of the database.
1613 */
drh8af73d42009-05-13 22:58:28 +00001614 if( db->init.busy ){
drh17e9e292003-02-01 13:53:28 +00001615 Table *pOld;
danielk1977e501b892006-01-09 06:29:47 +00001616 Schema *pSchema = p->pSchema;
drhea678832008-12-10 19:26:22 +00001617 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
drha83ccca2009-04-28 13:01:09 +00001618 sqlite3Strlen30(p->zName),p);
drh17e9e292003-02-01 13:53:28 +00001619 if( pOld ){
1620 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh17435752007-08-16 04:30:38 +00001621 db->mallocFailed = 1;
drh17e9e292003-02-01 13:53:28 +00001622 return;
1623 }
drh17e9e292003-02-01 13:53:28 +00001624 pParse->pNewTable = 0;
1625 db->nTable++;
1626 db->flags |= SQLITE_InternChanges;
danielk197719a8e7e2005-03-17 05:03:38 +00001627
1628#ifndef SQLITE_OMIT_ALTERTABLE
1629 if( !p->pSelect ){
danielk1977bab45c62006-01-16 15:14:27 +00001630 const char *zName = (const char *)pParse->sNameToken.z;
drh9a087a92007-05-15 14:34:32 +00001631 int nName;
danielk197719a8e7e2005-03-17 05:03:38 +00001632 assert( !pSelect && pCons && pEnd );
danielk1977bab45c62006-01-16 15:14:27 +00001633 if( pCons->z==0 ){
1634 pCons = pEnd;
1635 }
drh1bd10f82008-12-10 21:19:56 +00001636 nName = (int)((const char *)pCons->z - zName);
drh9a087a92007-05-15 14:34:32 +00001637 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
danielk197719a8e7e2005-03-17 05:03:38 +00001638 }
1639#endif
drh17e9e292003-02-01 13:53:28 +00001640 }
drh75897232000-05-29 14:26:00 +00001641}
1642
drhb7f91642004-10-31 02:22:47 +00001643#ifndef SQLITE_OMIT_VIEW
drh75897232000-05-29 14:26:00 +00001644/*
drha76b5df2002-02-23 02:32:10 +00001645** The parser calls this routine in order to create a new VIEW
1646*/
danielk19774adee202004-05-08 08:23:19 +00001647void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001648 Parse *pParse, /* The parsing context */
1649 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001650 Token *pName1, /* The token that holds the name of the view */
1651 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001652 Select *pSelect, /* A SELECT statement that will become the new view */
drhfdd48a72006-09-11 23:45:48 +00001653 int isTemp, /* TRUE for a TEMPORARY view */
1654 int noErr /* Suppress error messages if VIEW already exists */
drha76b5df2002-02-23 02:32:10 +00001655){
drha76b5df2002-02-23 02:32:10 +00001656 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001657 int n;
drhb7916a72009-05-27 10:31:29 +00001658 const char *z;
drh4b59ab52002-08-24 18:24:51 +00001659 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001660 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001661 Token *pName;
danielk1977da184232006-01-05 11:34:32 +00001662 int iDb;
drh17435752007-08-16 04:30:38 +00001663 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +00001664
drh7c3d64f2005-06-06 15:32:08 +00001665 if( pParse->nVar>0 ){
1666 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
drh633e6d52008-07-28 19:34:53 +00001667 sqlite3SelectDelete(db, pSelect);
drh7c3d64f2005-06-06 15:32:08 +00001668 return;
1669 }
drhfdd48a72006-09-11 23:45:48 +00001670 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
drha76b5df2002-02-23 02:32:10 +00001671 p = pParse->pNewTable;
drh8af73d42009-05-13 22:58:28 +00001672 if( p==0 ){
drh633e6d52008-07-28 19:34:53 +00001673 sqlite3SelectDelete(db, pSelect);
drh417be792002-03-03 18:59:40 +00001674 return;
1675 }
drh8af73d42009-05-13 22:58:28 +00001676 assert( pParse->nErr==0 ); /* If sqlite3StartTable return non-NULL then
1677 ** there could not have been an error */
danielk1977ef2cb632004-05-29 02:37:19 +00001678 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
drh17435752007-08-16 04:30:38 +00001679 iDb = sqlite3SchemaToIndex(db, p->pSchema);
danielk1977da184232006-01-05 11:34:32 +00001680 if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
danielk19774adee202004-05-08 08:23:19 +00001681 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001682 ){
drh633e6d52008-07-28 19:34:53 +00001683 sqlite3SelectDelete(db, pSelect);
drhf26e09c2003-05-31 16:21:12 +00001684 return;
1685 }
drh174b6192002-12-03 02:22:52 +00001686
drh4b59ab52002-08-24 18:24:51 +00001687 /* Make a copy of the entire SELECT statement that defines the view.
1688 ** This will force all the Expr.token.z values to be dynamically
1689 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001690 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001691 */
danielk19776ab3a2e2009-02-19 14:39:25 +00001692 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
drh633e6d52008-07-28 19:34:53 +00001693 sqlite3SelectDelete(db, pSelect);
drh17435752007-08-16 04:30:38 +00001694 if( db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001695 return;
1696 }
drh17435752007-08-16 04:30:38 +00001697 if( !db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001698 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001699 }
drh4b59ab52002-08-24 18:24:51 +00001700
1701 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1702 ** the end.
1703 */
drha76b5df2002-02-23 02:32:10 +00001704 sEnd = pParse->sLastToken;
drh768578e2009-05-12 00:40:12 +00001705 if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
drha76b5df2002-02-23 02:32:10 +00001706 sEnd.z += sEnd.n;
1707 }
1708 sEnd.n = 0;
drh1bd10f82008-12-10 21:19:56 +00001709 n = (int)(sEnd.z - pBegin->z);
drhb7916a72009-05-27 10:31:29 +00001710 z = pBegin->z;
drh768578e2009-05-12 00:40:12 +00001711 while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
drh4ff6dfa2002-03-03 23:06:00 +00001712 sEnd.z = &z[n-1];
1713 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001714
danielk19774adee202004-05-08 08:23:19 +00001715 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
danielk197719a8e7e2005-03-17 05:03:38 +00001716 sqlite3EndTable(pParse, 0, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001717 return;
drh417be792002-03-03 18:59:40 +00001718}
drhb7f91642004-10-31 02:22:47 +00001719#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001720
danielk1977fe3fcbe22006-06-12 12:08:45 +00001721#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
drh417be792002-03-03 18:59:40 +00001722/*
1723** The Table structure pTable is really a VIEW. Fill in the names of
1724** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001725** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001726*/
danielk19774adee202004-05-08 08:23:19 +00001727int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh9b3187e2005-01-18 14:45:47 +00001728 Table *pSelTab; /* A fake table from which we get the result set */
1729 Select *pSel; /* Copy of the SELECT that implements the view */
1730 int nErr = 0; /* Number of errors encountered */
1731 int n; /* Temporarily holds the number of cursors assigned */
drh17435752007-08-16 04:30:38 +00001732 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
drha6d0ffc2007-10-12 20:42:28 +00001733 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
drh417be792002-03-03 18:59:40 +00001734
1735 assert( pTable );
1736
danielk1977fe3fcbe22006-06-12 12:08:45 +00001737#ifndef SQLITE_OMIT_VIRTUALTABLE
1738 if( sqlite3VtabCallConnect(pParse, pTable) ){
1739 return SQLITE_ERROR;
1740 }
drh4cbdda92006-06-14 19:00:20 +00001741 if( IsVirtual(pTable) ) return 0;
danielk1977fe3fcbe22006-06-12 12:08:45 +00001742#endif
1743
1744#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001745 /* A positive nCol means the columns names for this view are
1746 ** already known.
1747 */
1748 if( pTable->nCol>0 ) return 0;
1749
1750 /* A negative nCol is a special marker meaning that we are currently
1751 ** trying to compute the column names. If we enter this routine with
1752 ** a negative nCol, it means two or more views form a loop, like this:
1753 **
1754 ** CREATE VIEW one AS SELECT * FROM two;
1755 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001756 **
drh768578e2009-05-12 00:40:12 +00001757 ** Actually, the error above is now caught prior to reaching this point.
1758 ** But the following test is still important as it does come up
1759 ** in the following:
1760 **
1761 ** CREATE TABLE main.ex1(a);
1762 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
1763 ** SELECT * FROM temp.ex1;
drh417be792002-03-03 18:59:40 +00001764 */
1765 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001766 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001767 return 1;
1768 }
drh85c23c62005-08-20 03:03:04 +00001769 assert( pTable->nCol>=0 );
drh417be792002-03-03 18:59:40 +00001770
1771 /* If we get this far, it means we need to compute the table names.
drh9b3187e2005-01-18 14:45:47 +00001772 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
1773 ** "*" elements in the results set of the view and will assign cursors
1774 ** to the elements of the FROM clause. But we do not want these changes
1775 ** to be permanent. So the computation is done on a copy of the SELECT
1776 ** statement that defines the view.
drh417be792002-03-03 18:59:40 +00001777 */
drh9b3187e2005-01-18 14:45:47 +00001778 assert( pTable->pSelect );
danielk19776ab3a2e2009-02-19 14:39:25 +00001779 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
danielk1977261919c2005-12-06 12:52:59 +00001780 if( pSel ){
shaneb08a67a2009-03-31 03:41:56 +00001781 u8 enableLookaside = db->lookaside.bEnabled;
danielk1977261919c2005-12-06 12:52:59 +00001782 n = pParse->nTab;
1783 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
1784 pTable->nCol = -1;
drhd9da78a2009-03-24 15:08:09 +00001785 db->lookaside.bEnabled = 0;
danielk1977db2d2862007-10-15 07:08:44 +00001786#ifndef SQLITE_OMIT_AUTHORIZATION
drha6d0ffc2007-10-12 20:42:28 +00001787 xAuth = db->xAuth;
1788 db->xAuth = 0;
drh7d10d5a2008-08-20 16:35:10 +00001789 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
drha6d0ffc2007-10-12 20:42:28 +00001790 db->xAuth = xAuth;
danielk1977db2d2862007-10-15 07:08:44 +00001791#else
drh7d10d5a2008-08-20 16:35:10 +00001792 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
danielk1977db2d2862007-10-15 07:08:44 +00001793#endif
drhd9da78a2009-03-24 15:08:09 +00001794 db->lookaside.bEnabled = enableLookaside;
danielk1977261919c2005-12-06 12:52:59 +00001795 pParse->nTab = n;
1796 if( pSelTab ){
1797 assert( pTable->aCol==0 );
1798 pTable->nCol = pSelTab->nCol;
1799 pTable->aCol = pSelTab->aCol;
1800 pSelTab->nCol = 0;
1801 pSelTab->aCol = 0;
danielk1977a04a34f2007-04-16 15:06:25 +00001802 sqlite3DeleteTable(pSelTab);
danielk1977da184232006-01-05 11:34:32 +00001803 pTable->pSchema->flags |= DB_UnresetViews;
danielk1977261919c2005-12-06 12:52:59 +00001804 }else{
1805 pTable->nCol = 0;
1806 nErr++;
1807 }
drh633e6d52008-07-28 19:34:53 +00001808 sqlite3SelectDelete(db, pSel);
danielk1977261919c2005-12-06 12:52:59 +00001809 } else {
drh417be792002-03-03 18:59:40 +00001810 nErr++;
1811 }
drhb7f91642004-10-31 02:22:47 +00001812#endif /* SQLITE_OMIT_VIEW */
danielk19774b2688a2006-06-20 11:01:07 +00001813 return nErr;
danielk1977fe3fcbe22006-06-12 12:08:45 +00001814}
1815#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
drh417be792002-03-03 18:59:40 +00001816
drhb7f91642004-10-31 02:22:47 +00001817#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001818/*
drh8bf8dc92003-05-17 17:35:10 +00001819** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001820*/
drh9bb575f2004-09-06 17:24:11 +00001821static void sqliteViewResetAll(sqlite3 *db, int idx){
drh417be792002-03-03 18:59:40 +00001822 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001823 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
danielk1977da184232006-01-05 11:34:32 +00001824 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001825 Table *pTab = sqliteHashData(i);
1826 if( pTab->pSelect ){
drh956bc922004-07-24 17:38:29 +00001827 sqliteResetColumnNames(pTab);
drh417be792002-03-03 18:59:40 +00001828 }
1829 }
drh8bf8dc92003-05-17 17:35:10 +00001830 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001831}
drhb7f91642004-10-31 02:22:47 +00001832#else
1833# define sqliteViewResetAll(A,B)
1834#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001835
drh75897232000-05-29 14:26:00 +00001836/*
danielk1977a0bf2652004-11-04 14:30:04 +00001837** This function is called by the VDBE to adjust the internal schema
1838** used by SQLite when the btree layer moves a table root page. The
1839** root-page of a table or index in database iDb has changed from iFrom
1840** to iTo.
drh6205d4a2006-03-24 03:36:26 +00001841**
1842** Ticket #1728: The symbol table might still contain information
1843** on tables and/or indices that are the process of being deleted.
1844** If you are unlucky, one of those deleted indices or tables might
1845** have the same rootpage number as the real table or index that is
1846** being moved. So we cannot stop searching after the first match
1847** because the first match might be for one of the deleted indices
1848** or tables and not the table/index that is actually being moved.
1849** We must continue looping until all tables and indices with
1850** rootpage==iFrom have been converted to have a rootpage of iTo
1851** in order to be certain that we got the right one.
danielk1977a0bf2652004-11-04 14:30:04 +00001852*/
1853#ifndef SQLITE_OMIT_AUTOVACUUM
1854void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){
1855 HashElem *pElem;
danielk1977da184232006-01-05 11:34:32 +00001856 Hash *pHash;
1857
1858 pHash = &pDb->pSchema->tblHash;
1859 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
danielk1977a0bf2652004-11-04 14:30:04 +00001860 Table *pTab = sqliteHashData(pElem);
1861 if( pTab->tnum==iFrom ){
1862 pTab->tnum = iTo;
danielk1977a0bf2652004-11-04 14:30:04 +00001863 }
1864 }
danielk1977da184232006-01-05 11:34:32 +00001865 pHash = &pDb->pSchema->idxHash;
1866 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
danielk1977a0bf2652004-11-04 14:30:04 +00001867 Index *pIdx = sqliteHashData(pElem);
1868 if( pIdx->tnum==iFrom ){
1869 pIdx->tnum = iTo;
danielk1977a0bf2652004-11-04 14:30:04 +00001870 }
1871 }
danielk1977a0bf2652004-11-04 14:30:04 +00001872}
1873#endif
1874
1875/*
1876** Write code to erase the table with root-page iTable from database iDb.
1877** Also write code to modify the sqlite_master table and internal schema
1878** if a root-page of another table is moved by the btree-layer whilst
1879** erasing iTable (this can happen with an auto-vacuum database).
1880*/
drh4e0cff62004-11-05 05:10:28 +00001881static void destroyRootPage(Parse *pParse, int iTable, int iDb){
1882 Vdbe *v = sqlite3GetVdbe(pParse);
drhb7654112008-01-12 12:48:07 +00001883 int r1 = sqlite3GetTempReg(pParse);
1884 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
dane0af83a2009-09-08 19:15:01 +00001885 sqlite3MayAbort(pParse);
drh40e016e2004-11-04 14:47:11 +00001886#ifndef SQLITE_OMIT_AUTOVACUUM
drhb7654112008-01-12 12:48:07 +00001887 /* OP_Destroy stores an in integer r1. If this integer
drh4e0cff62004-11-05 05:10:28 +00001888 ** is non-zero, then it is the root page number of a table moved to
drh81db88e2004-12-07 12:29:17 +00001889 ** location iTable. The following code modifies the sqlite_master table to
drh4e0cff62004-11-05 05:10:28 +00001890 ** reflect this.
1891 **
drh0fa991b2009-03-21 16:19:26 +00001892 ** The "#NNN" in the SQL is a special constant that means whatever value
drhb74b1012009-05-28 21:04:37 +00001893 ** is in register NNN. See grammar rules associated with the TK_REGISTER
1894 ** token for additional information.
drh4e0cff62004-11-05 05:10:28 +00001895 */
danielk197763e3e9f2004-11-05 09:19:27 +00001896 sqlite3NestedParse(pParse,
drhb7654112008-01-12 12:48:07 +00001897 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
1898 pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
danielk1977a0bf2652004-11-04 14:30:04 +00001899#endif
drhb7654112008-01-12 12:48:07 +00001900 sqlite3ReleaseTempReg(pParse, r1);
danielk1977a0bf2652004-11-04 14:30:04 +00001901}
1902
1903/*
1904** Write VDBE code to erase table pTab and all associated indices on disk.
1905** Code to update the sqlite_master tables and internal schema definitions
1906** in case a root-page belonging to another table is moved by the btree layer
1907** is also added (this can happen with an auto-vacuum database).
1908*/
drh4e0cff62004-11-05 05:10:28 +00001909static void destroyTable(Parse *pParse, Table *pTab){
danielk1977a0bf2652004-11-04 14:30:04 +00001910#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001911 Index *pIdx;
drh29c636b2006-01-09 23:40:25 +00001912 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1913 destroyRootPage(pParse, pTab->tnum, iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001914 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh29c636b2006-01-09 23:40:25 +00001915 destroyRootPage(pParse, pIdx->tnum, iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001916 }
1917#else
1918 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
1919 ** is not defined), then it is important to call OP_Destroy on the
1920 ** table and index root-pages in order, starting with the numerically
1921 ** largest root-page number. This guarantees that none of the root-pages
1922 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
1923 ** following were coded:
1924 **
1925 ** OP_Destroy 4 0
1926 ** ...
1927 ** OP_Destroy 5 0
1928 **
1929 ** and root page 5 happened to be the largest root-page number in the
1930 ** database, then root page 5 would be moved to page 4 by the
1931 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
1932 ** a free-list page.
1933 */
1934 int iTab = pTab->tnum;
1935 int iDestroyed = 0;
1936
1937 while( 1 ){
1938 Index *pIdx;
1939 int iLargest = 0;
1940
1941 if( iDestroyed==0 || iTab<iDestroyed ){
1942 iLargest = iTab;
1943 }
1944 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1945 int iIdx = pIdx->tnum;
danielk1977da184232006-01-05 11:34:32 +00001946 assert( pIdx->pSchema==pTab->pSchema );
danielk1977a0bf2652004-11-04 14:30:04 +00001947 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
1948 iLargest = iIdx;
1949 }
1950 }
danielk1977da184232006-01-05 11:34:32 +00001951 if( iLargest==0 ){
1952 return;
1953 }else{
1954 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1955 destroyRootPage(pParse, iLargest, iDb);
1956 iDestroyed = iLargest;
1957 }
danielk1977a0bf2652004-11-04 14:30:04 +00001958 }
1959#endif
1960}
1961
1962/*
drh75897232000-05-29 14:26:00 +00001963** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001964** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001965*/
drha0733842005-12-29 01:11:36 +00001966void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
danielk1977a8858102004-05-28 12:11:21 +00001967 Table *pTab;
drh75897232000-05-29 14:26:00 +00001968 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00001969 sqlite3 *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001970 int iDb;
drh75897232000-05-29 14:26:00 +00001971
drh8af73d42009-05-13 22:58:28 +00001972 if( db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +00001973 goto exit_drop_table;
1974 }
drh8af73d42009-05-13 22:58:28 +00001975 assert( pParse->nErr==0 );
danielk1977a8858102004-05-28 12:11:21 +00001976 assert( pName->nSrc==1 );
drha7564662010-02-22 19:32:31 +00001977 if( noErr ) db->suppressErr++;
drhca424112008-01-25 15:04:48 +00001978 pTab = sqlite3LocateTable(pParse, isView,
1979 pName->a[0].zName, pName->a[0].zDatabase);
drha7564662010-02-22 19:32:31 +00001980 if( noErr ) db->suppressErr--;
danielk1977a8858102004-05-28 12:11:21 +00001981
drha0733842005-12-29 01:11:36 +00001982 if( pTab==0 ){
drha0733842005-12-29 01:11:36 +00001983 goto exit_drop_table;
1984 }
danielk1977da184232006-01-05 11:34:32 +00001985 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhe22a3342003-04-22 20:30:37 +00001986 assert( iDb>=0 && iDb<db->nDb );
danielk1977b5258c32007-10-04 18:11:15 +00001987
1988 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
1989 ** it is initialized.
1990 */
1991 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
1992 goto exit_drop_table;
1993 }
drhe5f9c642003-01-13 23:27:31 +00001994#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001995 {
1996 int code;
danielk1977da184232006-01-05 11:34:32 +00001997 const char *zTab = SCHEMA_TABLE(iDb);
1998 const char *zDb = db->aDb[iDb].zName;
danielk1977f1a381e2006-06-16 08:01:02 +00001999 const char *zArg2 = 0;
danielk19774adee202004-05-08 08:23:19 +00002000 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00002001 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00002002 }
drhe5f9c642003-01-13 23:27:31 +00002003 if( isView ){
danielk197753c0f742005-03-29 03:10:59 +00002004 if( !OMIT_TEMPDB && iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00002005 code = SQLITE_DROP_TEMP_VIEW;
2006 }else{
2007 code = SQLITE_DROP_VIEW;
2008 }
danielk19774b2688a2006-06-20 11:01:07 +00002009#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977f1a381e2006-06-16 08:01:02 +00002010 }else if( IsVirtual(pTab) ){
2011 code = SQLITE_DROP_VTABLE;
danielk1977595a5232009-07-24 17:58:53 +00002012 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
danielk19774b2688a2006-06-20 11:01:07 +00002013#endif
drhe5f9c642003-01-13 23:27:31 +00002014 }else{
danielk197753c0f742005-03-29 03:10:59 +00002015 if( !OMIT_TEMPDB && iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00002016 code = SQLITE_DROP_TEMP_TABLE;
2017 }else{
2018 code = SQLITE_DROP_TABLE;
2019 }
2020 }
danielk1977f1a381e2006-06-16 08:01:02 +00002021 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
danielk1977a8858102004-05-28 12:11:21 +00002022 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00002023 }
danielk1977a8858102004-05-28 12:11:21 +00002024 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
2025 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00002026 }
drhe5f9c642003-01-13 23:27:31 +00002027 }
2028#endif
drhc456e572008-08-11 18:44:58 +00002029 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
danielk1977a8858102004-05-28 12:11:21 +00002030 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
danielk1977a8858102004-05-28 12:11:21 +00002031 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00002032 }
danielk1977576ec6b2005-01-21 11:55:25 +00002033
2034#ifndef SQLITE_OMIT_VIEW
2035 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
2036 ** on a table.
2037 */
danielk1977a8858102004-05-28 12:11:21 +00002038 if( isView && pTab->pSelect==0 ){
2039 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
2040 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00002041 }
danielk1977a8858102004-05-28 12:11:21 +00002042 if( !isView && pTab->pSelect ){
2043 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
2044 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00002045 }
danielk1977576ec6b2005-01-21 11:55:25 +00002046#endif
drh75897232000-05-29 14:26:00 +00002047
drh1ccde152000-06-17 13:12:39 +00002048 /* Generate code to remove the table from the master table
2049 ** on disk.
2050 */
danielk19774adee202004-05-08 08:23:19 +00002051 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002052 if( v ){
drhe0bc4042002-06-25 01:09:11 +00002053 Trigger *pTrigger;
drh2958a4e2004-11-12 03:56:15 +00002054 Db *pDb = &db->aDb[iDb];
drh77658e22007-12-04 16:54:52 +00002055 sqlite3BeginWriteOperation(pParse, 1, iDb);
drh8bf8dc92003-05-17 17:35:10 +00002056
danielk197720b1eaf2006-07-26 16:22:14 +00002057#ifndef SQLITE_OMIT_VIRTUALTABLE
2058 if( IsVirtual(pTab) ){
drh768578e2009-05-12 00:40:12 +00002059 sqlite3VdbeAddOp0(v, OP_VBegin);
danielk197720b1eaf2006-07-26 16:22:14 +00002060 }
2061#endif
dand66c8302009-09-28 14:49:01 +00002062 sqlite3FkDropTable(pParse, pName, pTab);
danielk197720b1eaf2006-07-26 16:22:14 +00002063
danielk19778e227872004-06-07 07:52:17 +00002064 /* Drop all triggers associated with the table being dropped. Code
2065 ** is generated to remove entries from sqlite_master and/or
2066 ** sqlite_temp_master if required.
2067 */
danielk19772f886d12009-02-28 10:47:41 +00002068 pTrigger = sqlite3TriggerList(pParse, pTab);
drhe0bc4042002-06-25 01:09:11 +00002069 while( pTrigger ){
danielk1977da184232006-01-05 11:34:32 +00002070 assert( pTrigger->pSchema==pTab->pSchema ||
2071 pTrigger->pSchema==db->aDb[1].pSchema );
drh74161702006-02-24 02:53:49 +00002072 sqlite3DropTriggerPtr(pParse, pTrigger);
drh956bc922004-07-24 17:38:29 +00002073 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +00002074 }
drh8bf8dc92003-05-17 17:35:10 +00002075
danielk19774d36b812004-11-19 07:07:30 +00002076#ifndef SQLITE_OMIT_AUTOINCREMENT
2077 /* Remove any entries of the sqlite_sequence table associated with
2078 ** the table being dropped. This is done before the table is dropped
2079 ** at the btree level, in case the sqlite_sequence table needs to
2080 ** move as a result of the drop (can happen in auto-vacuum mode).
2081 */
drh7d10d5a2008-08-20 16:35:10 +00002082 if( pTab->tabFlags & TF_Autoincrement ){
danielk19774d36b812004-11-19 07:07:30 +00002083 sqlite3NestedParse(pParse,
2084 "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
2085 pDb->zName, pTab->zName
2086 );
2087 }
2088#endif
2089
danielk19778e227872004-06-07 07:52:17 +00002090 /* Drop all SQLITE_MASTER table and index entries that refer to the
2091 ** table. The program name loops through the master table and deletes
2092 ** every row that refers to a table of the same name as the one being
2093 ** dropped. Triggers are handled seperately because a trigger can be
2094 ** created in the temp database that refers to a table in another
2095 ** database.
2096 */
drhf1974842004-11-05 03:56:00 +00002097 sqlite3NestedParse(pParse,
drh4794f732004-11-05 17:17:50 +00002098 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
drh2958a4e2004-11-12 03:56:15 +00002099 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
danielk1977006015d2008-04-11 17:11:26 +00002100
2101 /* Drop any statistics from the sqlite_stat1 table, if it exists */
2102 if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
2103 sqlite3NestedParse(pParse,
2104 "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", pDb->zName, pTab->zName
2105 );
2106 }
2107
danielk19774b2688a2006-06-20 11:01:07 +00002108 if( !isView && !IsVirtual(pTab) ){
drh4e0cff62004-11-05 05:10:28 +00002109 destroyTable(pParse, pTab);
drh5e00f6c2001-09-13 13:46:56 +00002110 }
drh2958a4e2004-11-12 03:56:15 +00002111
danielk1977a21c6b62005-01-24 10:25:59 +00002112 /* Remove the table entry from SQLite's internal schema and modify
2113 ** the schema cookie.
drh2958a4e2004-11-12 03:56:15 +00002114 */
drh4cbdda92006-06-14 19:00:20 +00002115 if( IsVirtual(pTab) ){
drh66a51672008-01-03 00:01:23 +00002116 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
drhb9bb7c12006-06-11 23:41:55 +00002117 }
drh66a51672008-01-03 00:01:23 +00002118 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
drh9cbf3422008-01-17 16:22:13 +00002119 sqlite3ChangeCookie(pParse, iDb);
drh75897232000-05-29 14:26:00 +00002120 }
drhd24cc422003-03-27 12:51:24 +00002121 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00002122
2123exit_drop_table:
drh633e6d52008-07-28 19:34:53 +00002124 sqlite3SrcListDelete(db, pName);
drh75897232000-05-29 14:26:00 +00002125}
2126
2127/*
drhc2eef3b2002-08-31 18:53:06 +00002128** This routine is called to create a new foreign key on the table
2129** currently under construction. pFromCol determines which columns
2130** in the current table point to the foreign key. If pFromCol==0 then
2131** connect the key to the last column inserted. pTo is the name of
2132** the table referred to. pToCol is a list of tables in the other
2133** pTo table that the foreign key points to. flags contains all
2134** information about the conflict resolution algorithms specified
2135** in the ON DELETE, ON UPDATE and ON INSERT clauses.
2136**
2137** An FKey structure is created and added to the table currently
drhe61922a2009-05-02 13:29:37 +00002138** under construction in the pParse->pNewTable field.
drhc2eef3b2002-08-31 18:53:06 +00002139**
2140** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00002141** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00002142*/
danielk19774adee202004-05-08 08:23:19 +00002143void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00002144 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00002145 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00002146 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00002147 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00002148 int flags /* Conflict resolution algorithms. */
2149){
danielk197718576932008-08-06 13:47:40 +00002150 sqlite3 *db = pParse->db;
drhb7f91642004-10-31 02:22:47 +00002151#ifndef SQLITE_OMIT_FOREIGN_KEY
drh40e016e2004-11-04 14:47:11 +00002152 FKey *pFKey = 0;
dan1da40a32009-09-19 17:00:31 +00002153 FKey *pNextTo;
drhc2eef3b2002-08-31 18:53:06 +00002154 Table *p = pParse->pNewTable;
2155 int nByte;
2156 int i;
2157 int nCol;
2158 char *z;
drhc2eef3b2002-08-31 18:53:06 +00002159
2160 assert( pTo!=0 );
drh8af73d42009-05-13 22:58:28 +00002161 if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
drhc2eef3b2002-08-31 18:53:06 +00002162 if( pFromCol==0 ){
2163 int iCol = p->nCol-1;
drhd3001712009-05-12 17:46:53 +00002164 if( NEVER(iCol<0) ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00002165 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00002166 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00002167 " should reference only one column of table %T",
2168 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00002169 goto fk_end;
2170 }
2171 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00002172 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00002173 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00002174 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00002175 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00002176 goto fk_end;
2177 }else{
danielk19770202b292004-06-09 09:55:16 +00002178 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00002179 }
drhe61922a2009-05-02 13:29:37 +00002180 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
drhc2eef3b2002-08-31 18:53:06 +00002181 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00002182 for(i=0; i<pToCol->nExpr; i++){
drhea678832008-12-10 19:26:22 +00002183 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
drhc2eef3b2002-08-31 18:53:06 +00002184 }
2185 }
drh633e6d52008-07-28 19:34:53 +00002186 pFKey = sqlite3DbMallocZero(db, nByte );
drh17435752007-08-16 04:30:38 +00002187 if( pFKey==0 ){
2188 goto fk_end;
2189 }
drhc2eef3b2002-08-31 18:53:06 +00002190 pFKey->pFrom = p;
2191 pFKey->pNextFrom = p->pFKey;
drhe61922a2009-05-02 13:29:37 +00002192 z = (char*)&pFKey->aCol[nCol];
drhdf68f6b2002-09-21 15:57:57 +00002193 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00002194 memcpy(z, pTo->z, pTo->n);
2195 z[pTo->n] = 0;
danielk197770d9e9c2009-04-24 18:06:09 +00002196 sqlite3Dequote(z);
drhc2eef3b2002-08-31 18:53:06 +00002197 z += pTo->n+1;
drhc2eef3b2002-08-31 18:53:06 +00002198 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00002199 if( pFromCol==0 ){
2200 pFKey->aCol[0].iFrom = p->nCol-1;
2201 }else{
2202 for(i=0; i<nCol; i++){
2203 int j;
2204 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00002205 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00002206 pFKey->aCol[i].iFrom = j;
2207 break;
2208 }
2209 }
2210 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002211 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00002212 "unknown column \"%s\" in foreign key definition",
2213 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00002214 goto fk_end;
2215 }
2216 }
2217 }
2218 if( pToCol ){
2219 for(i=0; i<nCol; i++){
drhea678832008-12-10 19:26:22 +00002220 int n = sqlite3Strlen30(pToCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00002221 pFKey->aCol[i].zCol = z;
2222 memcpy(z, pToCol->a[i].zName, n);
2223 z[n] = 0;
2224 z += n+1;
2225 }
2226 }
2227 pFKey->isDeferred = 0;
dan8099ce62009-09-23 08:43:35 +00002228 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
2229 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
drhc2eef3b2002-08-31 18:53:06 +00002230
dan1da40a32009-09-19 17:00:31 +00002231 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
2232 pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
2233 );
danf59c5ca2009-09-22 16:55:38 +00002234 if( pNextTo==pFKey ){
2235 db->mallocFailed = 1;
2236 goto fk_end;
2237 }
dan1da40a32009-09-19 17:00:31 +00002238 if( pNextTo ){
2239 assert( pNextTo->pPrevTo==0 );
2240 pFKey->pNextTo = pNextTo;
2241 pNextTo->pPrevTo = pFKey;
2242 }
2243
drhc2eef3b2002-08-31 18:53:06 +00002244 /* Link the foreign key to the table as the last step.
2245 */
2246 p->pFKey = pFKey;
2247 pFKey = 0;
2248
2249fk_end:
drh633e6d52008-07-28 19:34:53 +00002250 sqlite3DbFree(db, pFKey);
drhb7f91642004-10-31 02:22:47 +00002251#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh633e6d52008-07-28 19:34:53 +00002252 sqlite3ExprListDelete(db, pFromCol);
2253 sqlite3ExprListDelete(db, pToCol);
drhc2eef3b2002-08-31 18:53:06 +00002254}
2255
2256/*
2257** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
2258** clause is seen as part of a foreign key definition. The isDeferred
2259** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
2260** The behavior of the most recently created foreign key is adjusted
2261** accordingly.
2262*/
danielk19774adee202004-05-08 08:23:19 +00002263void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhb7f91642004-10-31 02:22:47 +00002264#ifndef SQLITE_OMIT_FOREIGN_KEY
drhc2eef3b2002-08-31 18:53:06 +00002265 Table *pTab;
2266 FKey *pFKey;
2267 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
drh4c429832009-10-12 22:30:49 +00002268 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
drh1bd10f82008-12-10 21:19:56 +00002269 pFKey->isDeferred = (u8)isDeferred;
drhb7f91642004-10-31 02:22:47 +00002270#endif
drhc2eef3b2002-08-31 18:53:06 +00002271}
2272
2273/*
drh063336a2004-11-05 20:58:39 +00002274** Generate code that will erase and refill index *pIdx. This is
2275** used to initialize a newly created index or to recompute the
2276** content of an index in response to a REINDEX command.
2277**
2278** if memRootPage is not negative, it means that the index is newly
drh1db639c2008-01-17 02:36:28 +00002279** created. The register specified by memRootPage contains the
drh063336a2004-11-05 20:58:39 +00002280** root page number of the index. If memRootPage is negative, then
2281** the index already exists and must be cleared before being refilled and
2282** the root page number of the index is taken from pIndex->tnum.
2283*/
2284static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
2285 Table *pTab = pIndex->pTable; /* The table that is indexed */
danielk19776ab3a2e2009-02-19 14:39:25 +00002286 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
2287 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
drh063336a2004-11-05 20:58:39 +00002288 int addr1; /* Address of top of loop */
2289 int tnum; /* Root page of index */
2290 Vdbe *v; /* Generate code into this virtual machine */
danielk1977b3bf5562006-01-10 17:58:23 +00002291 KeyInfo *pKey; /* KeyInfo for index */
drh2d401ab2008-01-10 23:50:11 +00002292 int regIdxKey; /* Registers containing the index key */
2293 int regRecord; /* Register holding assemblied index record */
drh17435752007-08-16 04:30:38 +00002294 sqlite3 *db = pParse->db; /* The database connection */
2295 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
drh063336a2004-11-05 20:58:39 +00002296
danielk19771d54df82004-11-23 15:41:16 +00002297#ifndef SQLITE_OMIT_AUTHORIZATION
2298 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
drh17435752007-08-16 04:30:38 +00002299 db->aDb[iDb].zName ) ){
danielk19771d54df82004-11-23 15:41:16 +00002300 return;
2301 }
2302#endif
2303
danielk1977c00da102006-01-07 13:21:04 +00002304 /* Require a write-lock on the table to perform this operation */
2305 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
2306
drh063336a2004-11-05 20:58:39 +00002307 v = sqlite3GetVdbe(pParse);
2308 if( v==0 ) return;
2309 if( memRootPage>=0 ){
drh1db639c2008-01-17 02:36:28 +00002310 tnum = memRootPage;
drh063336a2004-11-05 20:58:39 +00002311 }else{
2312 tnum = pIndex->tnum;
drh66a51672008-01-03 00:01:23 +00002313 sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
drh063336a2004-11-05 20:58:39 +00002314 }
danielk1977b3bf5562006-01-10 17:58:23 +00002315 pKey = sqlite3IndexKeyinfo(pParse, pIndex);
danielk1977207872a2008-01-03 07:54:23 +00002316 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
drh66a51672008-01-03 00:01:23 +00002317 (char *)pKey, P4_KEYINFO_HANDOFF);
drh98757152008-01-09 23:04:12 +00002318 if( memRootPage>=0 ){
2319 sqlite3VdbeChangeP5(v, 1);
2320 }
danielk1977c00da102006-01-07 13:21:04 +00002321 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
drh66a51672008-01-03 00:01:23 +00002322 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
drh2d401ab2008-01-10 23:50:11 +00002323 regRecord = sqlite3GetTempReg(pParse);
drhe14006d2008-03-25 17:23:32 +00002324 regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
drh7f057c92005-06-24 03:53:06 +00002325 if( pIndex->onError!=OE_None ){
danielk1977de630352009-05-04 11:42:29 +00002326 const int regRowid = regIdxKey + pIndex->nColumn;
2327 const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
2328 void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
drh2d401ab2008-01-10 23:50:11 +00002329
danielk1977de630352009-05-04 11:42:29 +00002330 /* The registers accessed by the OP_IsUnique opcode were allocated
2331 ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
2332 ** call above. Just before that function was freed they were released
2333 ** (made available to the compiler for reuse) using
2334 ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
2335 ** opcode use the values stored within seems dangerous. However, since
2336 ** we can be sure that no other temp registers have been allocated
2337 ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
2338 */
2339 sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
dane0af83a2009-09-08 19:15:01 +00002340 sqlite3HaltConstraint(
2341 pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
drh4343fea2004-11-05 23:46:15 +00002342 }
drh2d401ab2008-01-10 23:50:11 +00002343 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
danielk1977de630352009-05-04 11:42:29 +00002344 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
drh2d401ab2008-01-10 23:50:11 +00002345 sqlite3ReleaseTempReg(pParse, regRecord);
drh66a51672008-01-03 00:01:23 +00002346 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
drhd654be82005-09-20 17:42:23 +00002347 sqlite3VdbeJumpHere(v, addr1);
drh66a51672008-01-03 00:01:23 +00002348 sqlite3VdbeAddOp1(v, OP_Close, iTab);
2349 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
drh063336a2004-11-05 20:58:39 +00002350}
2351
2352/*
drh23bf66d2004-12-14 03:34:34 +00002353** Create a new index for an SQL table. pName1.pName2 is the name of the index
2354** and pTblList is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00002355** be NULL for a primary key or an index that is created to satisfy a
2356** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00002357** as the table to be indexed. pParse->pNewTable is a table that is
2358** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00002359**
drh382c0242001-10-06 16:33:02 +00002360** pList is a list of columns to be indexed. pList will be NULL if this
2361** is a primary key or unique-constraint on the most recent column added
2362** to the table currently under construction.
dan1da40a32009-09-19 17:00:31 +00002363**
2364** If the index is created successfully, return a pointer to the new Index
2365** structure. This is used by sqlite3AddPrimaryKey() to mark the index
2366** as the tables primary key (Index.autoIndex==2).
drh75897232000-05-29 14:26:00 +00002367*/
dan1da40a32009-09-19 17:00:31 +00002368Index *sqlite3CreateIndex(
drh23bf66d2004-12-14 03:34:34 +00002369 Parse *pParse, /* All information about this parse */
2370 Token *pName1, /* First part of index name. May be NULL */
2371 Token *pName2, /* Second part of index name. May be NULL */
2372 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
danielk19770202b292004-06-09 09:55:16 +00002373 ExprList *pList, /* A list of columns to be indexed */
drh23bf66d2004-12-14 03:34:34 +00002374 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh1c55ba02007-07-02 19:31:27 +00002375 Token *pStart, /* The CREATE token that begins this statement */
drhfdd6e852005-12-16 01:06:16 +00002376 Token *pEnd, /* The ")" that closes the CREATE INDEX statement */
drh4d91a702006-01-04 15:54:36 +00002377 int sortOrder, /* Sort order of primary key when pList==NULL */
2378 int ifNotExist /* Omit error if index already exists */
drh75897232000-05-29 14:26:00 +00002379){
dan1da40a32009-09-19 17:00:31 +00002380 Index *pRet = 0; /* Pointer to return */
drhfdd6e852005-12-16 01:06:16 +00002381 Table *pTab = 0; /* Table to be indexed */
2382 Index *pIndex = 0; /* The index to be created */
2383 char *zName = 0; /* Name of the index */
2384 int nName; /* Number of characters in zName */
drhbeae3192001-09-22 18:12:08 +00002385 int i, j;
drhfdd6e852005-12-16 01:06:16 +00002386 Token nullId; /* Fake token for an empty ID list */
2387 DbFixer sFix; /* For assigning database names to pTable */
2388 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
drh9bb575f2004-09-06 17:24:11 +00002389 sqlite3 *db = pParse->db;
drhfdd6e852005-12-16 01:06:16 +00002390 Db *pDb; /* The specific table containing the indexed database */
2391 int iDb; /* Index of the database that is being written */
2392 Token *pName = 0; /* Unqualified name of the index to create */
2393 struct ExprList_item *pListItem; /* For looping over pList */
danielk1977b3bf5562006-01-10 17:58:23 +00002394 int nCol;
2395 int nExtra = 0;
2396 char *zExtra;
danielk1977cbb18d22004-05-28 11:37:27 +00002397
drhd3001712009-05-12 17:46:53 +00002398 assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
drh8af73d42009-05-13 22:58:28 +00002399 assert( pParse->nErr==0 ); /* Never called with prior errors */
2400 if( db->mallocFailed || IN_DECLARE_VTAB ){
drhd3001712009-05-12 17:46:53 +00002401 goto exit_create_index;
2402 }
2403 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977e501b892006-01-09 06:29:47 +00002404 goto exit_create_index;
2405 }
drhdaffd0e2001-04-11 14:28:42 +00002406
drh75897232000-05-29 14:26:00 +00002407 /*
2408 ** Find the table that is to be indexed. Return early if not found.
2409 */
danielk1977cbb18d22004-05-28 11:37:27 +00002410 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00002411
2412 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00002413 ** to search for the table. 'Fix' the table name to this db
2414 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00002415 */
2416 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00002417 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00002418 if( iDb<0 ) goto exit_create_index;
2419
danielk197753c0f742005-03-29 03:10:59 +00002420#ifndef SQLITE_OMIT_TEMPDB
danielk1977ef2cb632004-05-29 02:37:19 +00002421 /* If the index name was unqualified, check if the the table
danielk1977fe910332007-12-02 11:46:34 +00002422 ** is a temp table. If so, set the database to 1. Do not do this
2423 ** if initialising a database schema.
danielk1977cbb18d22004-05-28 11:37:27 +00002424 */
danielk1977fe910332007-12-02 11:46:34 +00002425 if( !db->init.busy ){
2426 pTab = sqlite3SrcListLookup(pParse, pTblName);
drhd3001712009-05-12 17:46:53 +00002427 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977fe910332007-12-02 11:46:34 +00002428 iDb = 1;
2429 }
danielk1977ef2cb632004-05-29 02:37:19 +00002430 }
danielk197753c0f742005-03-29 03:10:59 +00002431#endif
danielk1977ef2cb632004-05-29 02:37:19 +00002432
2433 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
2434 sqlite3FixSrcList(&sFix, pTblName)
2435 ){
drh85c23c62005-08-20 03:03:04 +00002436 /* Because the parser constructs pTblName from a single identifier,
2437 ** sqlite3FixSrcList can never fail. */
2438 assert(0);
danielk1977cbb18d22004-05-28 11:37:27 +00002439 }
drhca424112008-01-25 15:04:48 +00002440 pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName,
danielk1977ef2cb632004-05-29 02:37:19 +00002441 pTblName->a[0].zDatabase);
danielk1977d207d802008-10-22 10:45:37 +00002442 if( !pTab || db->mallocFailed ) goto exit_create_index;
danielk1977da184232006-01-05 11:34:32 +00002443 assert( db->aDb[iDb].pSchema==pTab->pSchema );
drh75897232000-05-29 14:26:00 +00002444 }else{
drhe3c41372001-09-17 20:25:58 +00002445 assert( pName==0 );
danielk1977da184232006-01-05 11:34:32 +00002446 pTab = pParse->pNewTable;
drha6370df2006-01-04 21:40:06 +00002447 if( !pTab ) goto exit_create_index;
danielk1977da184232006-01-05 11:34:32 +00002448 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drh75897232000-05-29 14:26:00 +00002449 }
drhfdd6e852005-12-16 01:06:16 +00002450 pDb = &db->aDb[iDb];
danielk1977cbb18d22004-05-28 11:37:27 +00002451
drhd3001712009-05-12 17:46:53 +00002452 assert( pTab!=0 );
2453 assert( pParse->nErr==0 );
drh03881232009-02-13 03:43:31 +00002454 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
2455 && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00002456 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00002457 goto exit_create_index;
2458 }
danielk1977576ec6b2005-01-21 11:55:25 +00002459#ifndef SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +00002460 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00002461 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00002462 goto exit_create_index;
2463 }
danielk1977576ec6b2005-01-21 11:55:25 +00002464#endif
danielk19775ee9d692006-06-21 12:36:25 +00002465#ifndef SQLITE_OMIT_VIRTUALTABLE
2466 if( IsVirtual(pTab) ){
2467 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
2468 goto exit_create_index;
2469 }
2470#endif
drh75897232000-05-29 14:26:00 +00002471
2472 /*
2473 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00002474 ** index or table with the same name.
2475 **
2476 ** Exception: If we are reading the names of permanent indices from the
2477 ** sqlite_master table (because some other process changed the schema) and
2478 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00002479 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00002480 **
2481 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00002482 ** dealing with a primary key or UNIQUE constraint. We have to invent our
2483 ** own name.
drh75897232000-05-29 14:26:00 +00002484 */
danielk1977d8123362004-06-12 09:25:12 +00002485 if( pName ){
drh17435752007-08-16 04:30:38 +00002486 zName = sqlite3NameFromToken(db, pName);
drhe3c41372001-09-17 20:25:58 +00002487 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00002488 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00002489 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00002490 }
danielk1977d8123362004-06-12 09:25:12 +00002491 if( !db->init.busy ){
danielk1977d45a0312007-03-13 16:32:25 +00002492 if( sqlite3FindTable(db, zName, 0)!=0 ){
2493 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
2494 goto exit_create_index;
2495 }
2496 }
danielk197759a33f92007-03-17 10:26:59 +00002497 if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
2498 if( !ifNotExist ){
2499 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
danielk1977d8123362004-06-12 09:25:12 +00002500 }
danielk197759a33f92007-03-17 10:26:59 +00002501 goto exit_create_index;
2502 }
danielk1977a21c6b62005-01-24 10:25:59 +00002503 }else{
drhadbca9c2001-09-27 15:11:53 +00002504 int n;
2505 Index *pLoop;
2506 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
drhf089aa42008-07-08 19:34:06 +00002507 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
danielk1977a1644fd2007-08-29 12:31:25 +00002508 if( zName==0 ){
danielk1977a1644fd2007-08-29 12:31:25 +00002509 goto exit_create_index;
2510 }
drh75897232000-05-29 14:26:00 +00002511 }
2512
drhe5f9c642003-01-13 23:27:31 +00002513 /* Check for authorization to create an index.
2514 */
2515#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00002516 {
drhfdd6e852005-12-16 01:06:16 +00002517 const char *zDb = pDb->zName;
danielk197753c0f742005-03-29 03:10:59 +00002518 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002519 goto exit_create_index;
2520 }
2521 i = SQLITE_CREATE_INDEX;
danielk197753c0f742005-03-29 03:10:59 +00002522 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002523 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002524 goto exit_create_index;
2525 }
drhe5f9c642003-01-13 23:27:31 +00002526 }
2527#endif
2528
drh75897232000-05-29 14:26:00 +00002529 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00002530 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00002531 ** So create a fake list to simulate this.
2532 */
2533 if( pList==0 ){
drhb7916a72009-05-27 10:31:29 +00002534 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drhea678832008-12-10 19:26:22 +00002535 nullId.n = sqlite3Strlen30((char*)nullId.z);
drhb7916a72009-05-27 10:31:29 +00002536 pList = sqlite3ExprListAppend(pParse, 0, 0);
drh75897232000-05-29 14:26:00 +00002537 if( pList==0 ) goto exit_create_index;
drhb7916a72009-05-27 10:31:29 +00002538 sqlite3ExprListSetName(pParse, pList, &nullId, 0);
drh1bd10f82008-12-10 21:19:56 +00002539 pList->a[0].sortOrder = (u8)sortOrder;
drh75897232000-05-29 14:26:00 +00002540 }
2541
danielk1977b3bf5562006-01-10 17:58:23 +00002542 /* Figure out how many bytes of space are required to store explicitly
2543 ** specified collation sequence names.
2544 */
2545 for(i=0; i<pList->nExpr; i++){
drhd3001712009-05-12 17:46:53 +00002546 Expr *pExpr = pList->a[i].pExpr;
2547 if( pExpr ){
2548 CollSeq *pColl = pExpr->pColl;
2549 /* Either pColl!=0 or there was an OOM failure. But if an OOM
2550 ** failure we have quit before reaching this point. */
2551 if( ALWAYS(pColl) ){
2552 nExtra += (1 + sqlite3Strlen30(pColl->zName));
2553 }
danielk1977b3bf5562006-01-10 17:58:23 +00002554 }
2555 }
2556
drh75897232000-05-29 14:26:00 +00002557 /*
2558 ** Allocate the index structure.
2559 */
drhea678832008-12-10 19:26:22 +00002560 nName = sqlite3Strlen30(zName);
danielk1977b3bf5562006-01-10 17:58:23 +00002561 nCol = pList->nExpr;
drh17435752007-08-16 04:30:38 +00002562 pIndex = sqlite3DbMallocZero(db,
danielk1977b3bf5562006-01-10 17:58:23 +00002563 sizeof(Index) + /* Index structure */
2564 sizeof(int)*nCol + /* Index.aiColumn */
2565 sizeof(int)*(nCol+1) + /* Index.aiRowEst */
2566 sizeof(char *)*nCol + /* Index.azColl */
2567 sizeof(u8)*nCol + /* Index.aSortOrder */
2568 nName + 1 + /* Index.zName */
2569 nExtra /* Collation sequence names */
2570 );
drh17435752007-08-16 04:30:38 +00002571 if( db->mallocFailed ){
2572 goto exit_create_index;
2573 }
drh78aecb72006-02-10 18:08:09 +00002574 pIndex->azColl = (char**)(&pIndex[1]);
2575 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
danielk1977bab45c62006-01-16 15:14:27 +00002576 pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
drh78aecb72006-02-10 18:08:09 +00002577 pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
danielk1977b3bf5562006-01-10 17:58:23 +00002578 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
2579 zExtra = (char *)(&pIndex->zName[nName+1]);
drh5bb3eb92007-05-04 13:15:55 +00002580 memcpy(pIndex->zName, zName, nName+1);
drh75897232000-05-29 14:26:00 +00002581 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00002582 pIndex->nColumn = pList->nExpr;
drh1bd10f82008-12-10 21:19:56 +00002583 pIndex->onError = (u8)onError;
2584 pIndex->autoIndex = (u8)(pName==0);
danielk1977da184232006-01-05 11:34:32 +00002585 pIndex->pSchema = db->aDb[iDb].pSchema;
drh75897232000-05-29 14:26:00 +00002586
drhfdd6e852005-12-16 01:06:16 +00002587 /* Check to see if we should honor DESC requests on index columns
2588 */
danielk1977da184232006-01-05 11:34:32 +00002589 if( pDb->pSchema->file_format>=4 ){
drhfdd6e852005-12-16 01:06:16 +00002590 sortOrderMask = -1; /* Honor DESC */
drhfdd6e852005-12-16 01:06:16 +00002591 }else{
2592 sortOrderMask = 0; /* Ignore DESC */
2593 }
2594
drh1ccde152000-06-17 13:12:39 +00002595 /* Scan the names of the columns of the table to be indexed and
2596 ** load the column indices into the Index structure. Report an error
2597 ** if any column is not found.
drhd3001712009-05-12 17:46:53 +00002598 **
2599 ** TODO: Add a test to make sure that the same column is not named
2600 ** more than once within the same index. Only the first instance of
2601 ** the column will ever be used by the optimizer. Note that using the
2602 ** same column more than once cannot be an error because that would
2603 ** break backwards compatibility - it needs to be a warning.
drh75897232000-05-29 14:26:00 +00002604 */
drhfdd6e852005-12-16 01:06:16 +00002605 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
2606 const char *zColName = pListItem->zName;
2607 Column *pTabCol;
drh85eeb692005-12-21 03:16:42 +00002608 int requestedSortOrder;
drha34001c2007-02-02 12:44:37 +00002609 char *zColl; /* Collation sequence name */
danielk1977b3bf5562006-01-10 17:58:23 +00002610
drhfdd6e852005-12-16 01:06:16 +00002611 for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
2612 if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00002613 }
2614 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002615 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhfdd6e852005-12-16 01:06:16 +00002616 pTab->zName, zColName);
dan1db95102010-06-28 10:15:19 +00002617 pParse->checkSchema = 1;
drh75897232000-05-29 14:26:00 +00002618 goto exit_create_index;
2619 }
drh967e8b72000-06-21 13:59:10 +00002620 pIndex->aiColumn[i] = j;
drhd3001712009-05-12 17:46:53 +00002621 /* Justification of the ALWAYS(pListItem->pExpr->pColl): Because of
2622 ** the way the "idxlist" non-terminal is constructed by the parser,
2623 ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
2624 ** must exist or else there must have been an OOM error. But if there
2625 ** was an OOM error, we would never reach this point. */
2626 if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
2627 int nColl;
2628 zColl = pListItem->pExpr->pColl->zName;
2629 nColl = sqlite3Strlen30(zColl) + 1;
2630 assert( nExtra>=nColl );
2631 memcpy(zExtra, zColl, nColl);
danielk1977b3bf5562006-01-10 17:58:23 +00002632 zColl = zExtra;
drhd3001712009-05-12 17:46:53 +00002633 zExtra += nColl;
2634 nExtra -= nColl;
danielk19770202b292004-06-09 09:55:16 +00002635 }else{
danielk1977b3bf5562006-01-10 17:58:23 +00002636 zColl = pTab->aCol[j].zColl;
2637 if( !zColl ){
2638 zColl = db->pDfltColl->zName;
2639 }
danielk19770202b292004-06-09 09:55:16 +00002640 }
drhb7f24de2009-05-13 17:35:23 +00002641 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
danielk19777cedc8d2004-06-10 10:50:08 +00002642 goto exit_create_index;
2643 }
danielk1977b3bf5562006-01-10 17:58:23 +00002644 pIndex->azColl[i] = zColl;
drhd946db02005-12-29 19:23:06 +00002645 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
drh1bd10f82008-12-10 21:19:56 +00002646 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
drh75897232000-05-29 14:26:00 +00002647 }
drh51147ba2005-07-23 22:59:55 +00002648 sqlite3DefaultRowEst(pIndex);
drh75897232000-05-29 14:26:00 +00002649
danielk1977d8123362004-06-12 09:25:12 +00002650 if( pTab==pParse->pNewTable ){
2651 /* This routine has been called to create an automatic index as a
2652 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2653 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2654 ** i.e. one of:
2655 **
2656 ** CREATE TABLE t(x PRIMARY KEY, y);
2657 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2658 **
2659 ** Either way, check to see if the table already has such an index. If
2660 ** so, don't bother creating this one. This only applies to
2661 ** automatically created indices. Users can do as they wish with
2662 ** explicit indices.
drhd3001712009-05-12 17:46:53 +00002663 **
2664 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
2665 ** (and thus suppressing the second one) even if they have different
2666 ** sort orders.
2667 **
2668 ** If there are different collating sequences or if the columns of
2669 ** the constraint occur in different orders, then the constraints are
2670 ** considered distinct and both result in separate indices.
danielk1977d8123362004-06-12 09:25:12 +00002671 */
2672 Index *pIdx;
2673 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2674 int k;
2675 assert( pIdx->onError!=OE_None );
2676 assert( pIdx->autoIndex );
2677 assert( pIndex->onError!=OE_None );
2678
2679 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2680 for(k=0; k<pIdx->nColumn; k++){
drhd3001712009-05-12 17:46:53 +00002681 const char *z1;
2682 const char *z2;
danielk1977d8123362004-06-12 09:25:12 +00002683 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
drhd3001712009-05-12 17:46:53 +00002684 z1 = pIdx->azColl[k];
2685 z2 = pIndex->azColl[k];
danielk1977b3bf5562006-01-10 17:58:23 +00002686 if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
danielk1977d8123362004-06-12 09:25:12 +00002687 }
2688 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002689 if( pIdx->onError!=pIndex->onError ){
2690 /* This constraint creates the same index as a previous
2691 ** constraint specified somewhere in the CREATE TABLE statement.
2692 ** However the ON CONFLICT clauses are different. If both this
2693 ** constraint and the previous equivalent constraint have explicit
2694 ** ON CONFLICT clauses this is an error. Otherwise, use the
2695 ** explicitly specified behaviour for the index.
2696 */
2697 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2698 sqlite3ErrorMsg(pParse,
2699 "conflicting ON CONFLICT clauses specified", 0);
2700 }
2701 if( pIdx->onError==OE_Default ){
2702 pIdx->onError = pIndex->onError;
2703 }
2704 }
danielk1977d8123362004-06-12 09:25:12 +00002705 goto exit_create_index;
2706 }
2707 }
2708 }
2709
drh75897232000-05-29 14:26:00 +00002710 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002711 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002712 */
drh234c39d2004-07-24 03:30:47 +00002713 if( db->init.busy ){
drh6d4abfb2001-10-22 02:58:08 +00002714 Index *p;
danielk1977da184232006-01-05 11:34:32 +00002715 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
drha83ccca2009-04-28 13:01:09 +00002716 pIndex->zName, sqlite3Strlen30(pIndex->zName),
drhea678832008-12-10 19:26:22 +00002717 pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002718 if( p ){
2719 assert( p==pIndex ); /* Malloc must have failed */
drh17435752007-08-16 04:30:38 +00002720 db->mallocFailed = 1;
drh6d4abfb2001-10-22 02:58:08 +00002721 goto exit_create_index;
2722 }
drh5e00f6c2001-09-13 13:46:56 +00002723 db->flags |= SQLITE_InternChanges;
drh234c39d2004-07-24 03:30:47 +00002724 if( pTblName!=0 ){
2725 pIndex->tnum = db->init.newTnum;
2726 }
drhd78eeee2001-09-13 16:18:53 +00002727 }
2728
drh1d85d932004-02-14 23:05:52 +00002729 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002730 ** involves writing the index into the master table and filling in the
2731 ** index with the current table contents.
2732 **
drh1d85d932004-02-14 23:05:52 +00002733 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2734 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002735 ** CREATE INDEX statements are read out of the master table. In
2736 ** the latter case the index already exists on disk, which is why
2737 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002738 **
danielk1977cbb18d22004-05-28 11:37:27 +00002739 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002740 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2741 ** has just been created, it contains no data and the index initialization
2742 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002743 */
drhd3001712009-05-12 17:46:53 +00002744 else{ /* if( db->init.busy==0 ) */
drhadbca9c2001-09-27 15:11:53 +00002745 Vdbe *v;
drh063336a2004-11-05 20:58:39 +00002746 char *zStmt;
drh0a07c102008-01-03 18:03:08 +00002747 int iMem = ++pParse->nMem;
drh75897232000-05-29 14:26:00 +00002748
danielk19774adee202004-05-08 08:23:19 +00002749 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002750 if( v==0 ) goto exit_create_index;
drh063336a2004-11-05 20:58:39 +00002751
drhfdd6e852005-12-16 01:06:16 +00002752
drh063336a2004-11-05 20:58:39 +00002753 /* Create the rootpage for the index
2754 */
drhaee128d2005-02-14 20:48:18 +00002755 sqlite3BeginWriteOperation(pParse, 1, iDb);
drhb7654112008-01-12 12:48:07 +00002756 sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
drh063336a2004-11-05 20:58:39 +00002757
2758 /* Gather the complete text of the CREATE INDEX statement into
2759 ** the zStmt variable
2760 */
drhd3001712009-05-12 17:46:53 +00002761 if( pStart ){
2762 assert( pEnd!=0 );
drh063336a2004-11-05 20:58:39 +00002763 /* A named index with an explicit CREATE INDEX statement */
danielk19771e536952007-08-16 10:09:01 +00002764 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
drh063336a2004-11-05 20:58:39 +00002765 onError==OE_None ? "" : " UNIQUE",
drh97903fe2005-05-24 20:19:57 +00002766 pEnd->z - pName->z + 1,
drh063336a2004-11-05 20:58:39 +00002767 pName->z);
2768 }else{
2769 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
drhe497f002004-11-07 13:01:49 +00002770 /* zStmt = sqlite3MPrintf(""); */
2771 zStmt = 0;
drh75897232000-05-29 14:26:00 +00002772 }
drh063336a2004-11-05 20:58:39 +00002773
2774 /* Add an entry in sqlite_master for this index
2775 */
2776 sqlite3NestedParse(pParse,
drhb7654112008-01-12 12:48:07 +00002777 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
drh063336a2004-11-05 20:58:39 +00002778 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2779 pIndex->zName,
2780 pTab->zName,
drhb7654112008-01-12 12:48:07 +00002781 iMem,
drh063336a2004-11-05 20:58:39 +00002782 zStmt
2783 );
drh633e6d52008-07-28 19:34:53 +00002784 sqlite3DbFree(db, zStmt);
drh063336a2004-11-05 20:58:39 +00002785
danielk1977a21c6b62005-01-24 10:25:59 +00002786 /* Fill the index with data and reparse the schema. Code an OP_Expire
2787 ** to invalidate all pre-compiled statements.
drh063336a2004-11-05 20:58:39 +00002788 */
danielk1977cbb18d22004-05-28 11:37:27 +00002789 if( pTblName ){
drh063336a2004-11-05 20:58:39 +00002790 sqlite3RefillIndex(pParse, pIndex, iMem);
drh9cbf3422008-01-17 16:22:13 +00002791 sqlite3ChangeCookie(pParse, iDb);
drh66a51672008-01-03 00:01:23 +00002792 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0,
2793 sqlite3MPrintf(db, "name='%q'", pIndex->zName), P4_DYNAMIC);
2794 sqlite3VdbeAddOp1(v, OP_Expire, 0);
drh5e00f6c2001-09-13 13:46:56 +00002795 }
drh75897232000-05-29 14:26:00 +00002796 }
2797
danielk1977d8123362004-06-12 09:25:12 +00002798 /* When adding an index to the list of indices for a table, make
2799 ** sure all indices labeled OE_Replace come after all those labeled
drhd3001712009-05-12 17:46:53 +00002800 ** OE_Ignore. This is necessary for the correct constraint check
2801 ** processing (in sqlite3GenerateConstraintChecks()) as part of
2802 ** UPDATE and INSERT statements.
danielk1977d8123362004-06-12 09:25:12 +00002803 */
drh234c39d2004-07-24 03:30:47 +00002804 if( db->init.busy || pTblName==0 ){
2805 if( onError!=OE_Replace || pTab->pIndex==0
2806 || pTab->pIndex->onError==OE_Replace){
2807 pIndex->pNext = pTab->pIndex;
2808 pTab->pIndex = pIndex;
2809 }else{
2810 Index *pOther = pTab->pIndex;
2811 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2812 pOther = pOther->pNext;
2813 }
2814 pIndex->pNext = pOther->pNext;
2815 pOther->pNext = pIndex;
danielk1977d8123362004-06-12 09:25:12 +00002816 }
dan1da40a32009-09-19 17:00:31 +00002817 pRet = pIndex;
drh234c39d2004-07-24 03:30:47 +00002818 pIndex = 0;
danielk1977d8123362004-06-12 09:25:12 +00002819 }
danielk1977d8123362004-06-12 09:25:12 +00002820
drh75897232000-05-29 14:26:00 +00002821 /* Clean up before exiting */
2822exit_create_index:
drh956bc922004-07-24 17:38:29 +00002823 if( pIndex ){
danielk1977cb9d8d82009-03-18 18:43:36 +00002824 sqlite3_free(pIndex->zColAff);
2825 sqlite3DbFree(db, pIndex);
drh956bc922004-07-24 17:38:29 +00002826 }
drh633e6d52008-07-28 19:34:53 +00002827 sqlite3ExprListDelete(db, pList);
2828 sqlite3SrcListDelete(db, pTblName);
2829 sqlite3DbFree(db, zName);
dan1da40a32009-09-19 17:00:31 +00002830 return pRet;
drh75897232000-05-29 14:26:00 +00002831}
2832
2833/*
drh51147ba2005-07-23 22:59:55 +00002834** Fill the Index.aiRowEst[] array with default information - information
drh91124b32005-08-18 18:15:05 +00002835** to be used when we have not run the ANALYZE command.
drh28c4cf42005-07-27 20:41:43 +00002836**
2837** aiRowEst[0] is suppose to contain the number of elements in the index.
2838** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
2839** number of rows in the table that match any particular value of the
2840** first column of the index. aiRowEst[2] is an estimate of the number
2841** of rows that match any particular combiniation of the first 2 columns
2842** of the index. And so forth. It must always be the case that
2843*
2844** aiRowEst[N]<=aiRowEst[N-1]
2845** aiRowEst[N]>=1
2846**
2847** Apart from that, we have little to go on besides intuition as to
2848** how aiRowEst[] should be initialized. The numbers generated here
2849** are based on typical values found in actual indices.
drh51147ba2005-07-23 22:59:55 +00002850*/
2851void sqlite3DefaultRowEst(Index *pIdx){
drh37108e12005-08-31 13:13:31 +00002852 unsigned *a = pIdx->aiRowEst;
drh51147ba2005-07-23 22:59:55 +00002853 int i;
drh28c4cf42005-07-27 20:41:43 +00002854 assert( a!=0 );
2855 a[0] = 1000000;
drhdb513882006-05-11 23:14:59 +00002856 for(i=pIdx->nColumn; i>=5; i--){
2857 a[i] = 5;
2858 }
2859 while( i>=1 ){
2860 a[i] = 11 - i;
2861 i--;
drh28c4cf42005-07-27 20:41:43 +00002862 }
2863 if( pIdx->onError!=OE_None ){
2864 a[pIdx->nColumn] = 1;
drh51147ba2005-07-23 22:59:55 +00002865 }
2866}
2867
2868/*
drh74e24cd2002-01-09 03:19:59 +00002869** This routine will drop an existing named index. This routine
2870** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002871*/
drh4d91a702006-01-04 15:54:36 +00002872void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
drh75897232000-05-29 14:26:00 +00002873 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002874 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00002875 sqlite3 *db = pParse->db;
danielk1977da184232006-01-05 11:34:32 +00002876 int iDb;
drh75897232000-05-29 14:26:00 +00002877
drh8af73d42009-05-13 22:58:28 +00002878 assert( pParse->nErr==0 ); /* Never called with prior errors */
2879 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00002880 goto exit_drop_index;
2881 }
drhd24cc422003-03-27 12:51:24 +00002882 assert( pName->nSrc==1 );
danielk1977d5d56522005-03-16 12:15:20 +00002883 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
2884 goto exit_drop_index;
2885 }
danielk19774adee202004-05-08 08:23:19 +00002886 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002887 if( pIndex==0 ){
drh4d91a702006-01-04 15:54:36 +00002888 if( !ifExists ){
2889 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
2890 }
drha6ecd332004-06-10 00:29:09 +00002891 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002892 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002893 }
drh485b39b2002-07-13 03:11:52 +00002894 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002895 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002896 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002897 goto exit_drop_index;
2898 }
danielk1977da184232006-01-05 11:34:32 +00002899 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
drhe5f9c642003-01-13 23:27:31 +00002900#ifndef SQLITE_OMIT_AUTHORIZATION
2901 {
2902 int code = SQLITE_DROP_INDEX;
2903 Table *pTab = pIndex->pTable;
danielk1977da184232006-01-05 11:34:32 +00002904 const char *zDb = db->aDb[iDb].zName;
2905 const char *zTab = SCHEMA_TABLE(iDb);
danielk19774adee202004-05-08 08:23:19 +00002906 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002907 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002908 }
danielk1977da184232006-01-05 11:34:32 +00002909 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002910 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002911 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002912 }
drhed6c8672003-01-12 18:02:16 +00002913 }
drhe5f9c642003-01-13 23:27:31 +00002914#endif
drh75897232000-05-29 14:26:00 +00002915
2916 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002917 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002918 if( v ){
drh77658e22007-12-04 16:54:52 +00002919 sqlite3BeginWriteOperation(pParse, 1, iDb);
drhb17131a2004-11-05 22:18:49 +00002920 sqlite3NestedParse(pParse,
2921 "DELETE FROM %Q.%s WHERE name=%Q",
2922 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2923 pIndex->zName
2924 );
danielk1977006015d2008-04-11 17:11:26 +00002925 if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
2926 sqlite3NestedParse(pParse,
2927 "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q",
2928 db->aDb[iDb].zName, pIndex->zName
2929 );
2930 }
drh9cbf3422008-01-17 16:22:13 +00002931 sqlite3ChangeCookie(pParse, iDb);
drhb17131a2004-11-05 22:18:49 +00002932 destroyRootPage(pParse, pIndex->tnum, iDb);
drh66a51672008-01-03 00:01:23 +00002933 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
drh75897232000-05-29 14:26:00 +00002934 }
2935
drhd24cc422003-03-27 12:51:24 +00002936exit_drop_index:
drh633e6d52008-07-28 19:34:53 +00002937 sqlite3SrcListDelete(db, pName);
drh75897232000-05-29 14:26:00 +00002938}
2939
2940/*
drhcf643722007-03-27 13:36:37 +00002941** pArray is a pointer to an array of objects. Each object in the
2942** array is szEntry bytes in size. This routine allocates a new
2943** object on the end of the array.
drh13449892005-09-07 21:22:45 +00002944**
drhcf643722007-03-27 13:36:37 +00002945** *pnEntry is the number of entries already in use. *pnAlloc is
2946** the previously allocated size of the array. initSize is the
2947** suggested initial array size allocation.
drh13449892005-09-07 21:22:45 +00002948**
drhcf643722007-03-27 13:36:37 +00002949** The index of the new entry is returned in *pIdx.
drh13449892005-09-07 21:22:45 +00002950**
drhcf643722007-03-27 13:36:37 +00002951** This routine returns a pointer to the array of objects. This
2952** might be the same as the pArray parameter or it might be a different
2953** pointer if the array was resized.
drh13449892005-09-07 21:22:45 +00002954*/
drhcf643722007-03-27 13:36:37 +00002955void *sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002956 sqlite3 *db, /* Connection to notify of malloc failures */
drhcf643722007-03-27 13:36:37 +00002957 void *pArray, /* Array of objects. Might be reallocated */
2958 int szEntry, /* Size of each object in the array */
2959 int initSize, /* Suggested initial allocation, in elements */
2960 int *pnEntry, /* Number of objects currently in use */
2961 int *pnAlloc, /* Current size of the allocation, in elements */
2962 int *pIdx /* Write the index of a new slot here */
2963){
2964 char *z;
2965 if( *pnEntry >= *pnAlloc ){
drh13449892005-09-07 21:22:45 +00002966 void *pNew;
drh5360ad32005-09-08 00:13:27 +00002967 int newSize;
drhcf643722007-03-27 13:36:37 +00002968 newSize = (*pnAlloc)*2 + initSize;
danielk197726783a52007-08-29 14:06:22 +00002969 pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry);
drh13449892005-09-07 21:22:45 +00002970 if( pNew==0 ){
drhcf643722007-03-27 13:36:37 +00002971 *pIdx = -1;
2972 return pArray;
drh13449892005-09-07 21:22:45 +00002973 }
drh6a1e0712008-12-05 15:24:15 +00002974 *pnAlloc = sqlite3DbMallocSize(db, pNew)/szEntry;
drhcf643722007-03-27 13:36:37 +00002975 pArray = pNew;
drh13449892005-09-07 21:22:45 +00002976 }
drhcf643722007-03-27 13:36:37 +00002977 z = (char*)pArray;
2978 memset(&z[*pnEntry * szEntry], 0, szEntry);
2979 *pIdx = *pnEntry;
2980 ++*pnEntry;
2981 return pArray;
drh13449892005-09-07 21:22:45 +00002982}
2983
2984/*
drh75897232000-05-29 14:26:00 +00002985** Append a new element to the given IdList. Create a new IdList if
2986** need be.
drhdaffd0e2001-04-11 14:28:42 +00002987**
2988** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002989*/
drh17435752007-08-16 04:30:38 +00002990IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
drh13449892005-09-07 21:22:45 +00002991 int i;
drh75897232000-05-29 14:26:00 +00002992 if( pList==0 ){
drh17435752007-08-16 04:30:38 +00002993 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
drh75897232000-05-29 14:26:00 +00002994 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002995 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002996 }
drhcf643722007-03-27 13:36:37 +00002997 pList->a = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002998 db,
drhcf643722007-03-27 13:36:37 +00002999 pList->a,
3000 sizeof(pList->a[0]),
3001 5,
3002 &pList->nId,
3003 &pList->nAlloc,
3004 &i
3005 );
drh13449892005-09-07 21:22:45 +00003006 if( i<0 ){
drh633e6d52008-07-28 19:34:53 +00003007 sqlite3IdListDelete(db, pList);
drh13449892005-09-07 21:22:45 +00003008 return 0;
drh75897232000-05-29 14:26:00 +00003009 }
drh17435752007-08-16 04:30:38 +00003010 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
drh75897232000-05-29 14:26:00 +00003011 return pList;
3012}
3013
3014/*
drhfe05af82005-07-21 03:14:59 +00003015** Delete an IdList.
3016*/
drh633e6d52008-07-28 19:34:53 +00003017void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
drhfe05af82005-07-21 03:14:59 +00003018 int i;
3019 if( pList==0 ) return;
3020 for(i=0; i<pList->nId; i++){
drh633e6d52008-07-28 19:34:53 +00003021 sqlite3DbFree(db, pList->a[i].zName);
drhfe05af82005-07-21 03:14:59 +00003022 }
drh633e6d52008-07-28 19:34:53 +00003023 sqlite3DbFree(db, pList->a);
3024 sqlite3DbFree(db, pList);
drhfe05af82005-07-21 03:14:59 +00003025}
3026
3027/*
3028** Return the index in pList of the identifier named zId. Return -1
3029** if not found.
3030*/
3031int sqlite3IdListIndex(IdList *pList, const char *zName){
3032 int i;
3033 if( pList==0 ) return -1;
3034 for(i=0; i<pList->nId; i++){
3035 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
3036 }
3037 return -1;
3038}
3039
3040/*
drha78c22c2008-11-11 18:28:58 +00003041** Expand the space allocated for the given SrcList object by
3042** creating nExtra new slots beginning at iStart. iStart is zero based.
3043** New slots are zeroed.
3044**
3045** For example, suppose a SrcList initially contains two entries: A,B.
3046** To append 3 new entries onto the end, do this:
3047**
3048** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
3049**
3050** After the call above it would contain: A, B, nil, nil, nil.
3051** If the iStart argument had been 1 instead of 2, then the result
3052** would have been: A, nil, nil, nil, B. To prepend the new slots,
3053** the iStart value would be 0. The result then would
3054** be: nil, nil, nil, A, B.
3055**
3056** If a memory allocation fails the SrcList is unchanged. The
3057** db->mallocFailed flag will be set to true.
3058*/
3059SrcList *sqlite3SrcListEnlarge(
3060 sqlite3 *db, /* Database connection to notify of OOM errors */
3061 SrcList *pSrc, /* The SrcList to be enlarged */
3062 int nExtra, /* Number of new slots to add to pSrc->a[] */
3063 int iStart /* Index in pSrc->a[] of first new slot */
3064){
3065 int i;
3066
3067 /* Sanity checking on calling parameters */
3068 assert( iStart>=0 );
3069 assert( nExtra>=1 );
drh8af73d42009-05-13 22:58:28 +00003070 assert( pSrc!=0 );
3071 assert( iStart<=pSrc->nSrc );
drha78c22c2008-11-11 18:28:58 +00003072
3073 /* Allocate additional space if needed */
3074 if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
3075 SrcList *pNew;
3076 int nAlloc = pSrc->nSrc+nExtra;
drh6a1e0712008-12-05 15:24:15 +00003077 int nGot;
drha78c22c2008-11-11 18:28:58 +00003078 pNew = sqlite3DbRealloc(db, pSrc,
3079 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
3080 if( pNew==0 ){
3081 assert( db->mallocFailed );
3082 return pSrc;
3083 }
3084 pSrc = pNew;
drh6a1e0712008-12-05 15:24:15 +00003085 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
drh1bd10f82008-12-10 21:19:56 +00003086 pSrc->nAlloc = (u16)nGot;
drha78c22c2008-11-11 18:28:58 +00003087 }
3088
3089 /* Move existing slots that come after the newly inserted slots
3090 ** out of the way */
3091 for(i=pSrc->nSrc-1; i>=iStart; i--){
3092 pSrc->a[i+nExtra] = pSrc->a[i];
3093 }
shane18e526c2008-12-10 22:30:24 +00003094 pSrc->nSrc += (i16)nExtra;
drha78c22c2008-11-11 18:28:58 +00003095
3096 /* Zero the newly allocated slots */
3097 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
3098 for(i=iStart; i<iStart+nExtra; i++){
3099 pSrc->a[i].iCursor = -1;
3100 }
3101
3102 /* Return a pointer to the enlarged SrcList */
3103 return pSrc;
3104}
3105
3106
3107/*
drhad3cab52002-05-24 02:04:32 +00003108** Append a new table name to the given SrcList. Create a new SrcList if
drhb7916a72009-05-27 10:31:29 +00003109** need be. A new entry is created in the SrcList even if pTable is NULL.
drhad3cab52002-05-24 02:04:32 +00003110**
drha78c22c2008-11-11 18:28:58 +00003111** A SrcList is returned, or NULL if there is an OOM error. The returned
3112** SrcList might be the same as the SrcList that was input or it might be
3113** a new one. If an OOM error does occurs, then the prior value of pList
3114** that is input to this routine is automatically freed.
drh113088e2003-03-20 01:16:58 +00003115**
3116** If pDatabase is not null, it means that the table has an optional
3117** database name prefix. Like this: "database.table". The pDatabase
3118** points to the table name and the pTable points to the database name.
3119** The SrcList.a[].zName field is filled with the table name which might
3120** come from pTable (if pDatabase is NULL) or from pDatabase.
3121** SrcList.a[].zDatabase is filled with the database name from pTable,
3122** or with NULL if no database is specified.
3123**
3124** In other words, if call like this:
3125**
drh17435752007-08-16 04:30:38 +00003126** sqlite3SrcListAppend(D,A,B,0);
drh113088e2003-03-20 01:16:58 +00003127**
3128** Then B is a table name and the database name is unspecified. If called
3129** like this:
3130**
drh17435752007-08-16 04:30:38 +00003131** sqlite3SrcListAppend(D,A,B,C);
drh113088e2003-03-20 01:16:58 +00003132**
drhd3001712009-05-12 17:46:53 +00003133** Then C is the table name and B is the database name. If C is defined
3134** then so is B. In other words, we never have a case where:
3135**
3136** sqlite3SrcListAppend(D,A,0,C);
drhb7916a72009-05-27 10:31:29 +00003137**
3138** Both pTable and pDatabase are assumed to be quoted. They are dequoted
3139** before being added to the SrcList.
drhad3cab52002-05-24 02:04:32 +00003140*/
drh17435752007-08-16 04:30:38 +00003141SrcList *sqlite3SrcListAppend(
3142 sqlite3 *db, /* Connection to notify of malloc failures */
3143 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
3144 Token *pTable, /* Table to append */
3145 Token *pDatabase /* Database of the table */
3146){
drha99db3b2004-06-19 14:49:12 +00003147 struct SrcList_item *pItem;
drhd3001712009-05-12 17:46:53 +00003148 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
drhad3cab52002-05-24 02:04:32 +00003149 if( pList==0 ){
drh17435752007-08-16 04:30:38 +00003150 pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00003151 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00003152 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00003153 }
drha78c22c2008-11-11 18:28:58 +00003154 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
3155 if( db->mallocFailed ){
3156 sqlite3SrcListDelete(db, pList);
3157 return 0;
drhad3cab52002-05-24 02:04:32 +00003158 }
drha78c22c2008-11-11 18:28:58 +00003159 pItem = &pList->a[pList->nSrc-1];
drh113088e2003-03-20 01:16:58 +00003160 if( pDatabase && pDatabase->z==0 ){
3161 pDatabase = 0;
3162 }
drhd3001712009-05-12 17:46:53 +00003163 if( pDatabase ){
drh113088e2003-03-20 01:16:58 +00003164 Token *pTemp = pDatabase;
3165 pDatabase = pTable;
3166 pTable = pTemp;
3167 }
drh17435752007-08-16 04:30:38 +00003168 pItem->zName = sqlite3NameFromToken(db, pTable);
3169 pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
drhad3cab52002-05-24 02:04:32 +00003170 return pList;
3171}
3172
3173/*
drhdfe88ec2008-11-03 20:55:06 +00003174** Assign VdbeCursor index numbers to all tables in a SrcList
drh63eb5f22003-04-29 16:20:44 +00003175*/
danielk19774adee202004-05-08 08:23:19 +00003176void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00003177 int i;
drh9b3187e2005-01-18 14:45:47 +00003178 struct SrcList_item *pItem;
drh17435752007-08-16 04:30:38 +00003179 assert(pList || pParse->db->mallocFailed );
danielk1977261919c2005-12-06 12:52:59 +00003180 if( pList ){
3181 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
3182 if( pItem->iCursor>=0 ) break;
3183 pItem->iCursor = pParse->nTab++;
3184 if( pItem->pSelect ){
3185 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
3186 }
drh63eb5f22003-04-29 16:20:44 +00003187 }
3188 }
3189}
3190
3191/*
drhad3cab52002-05-24 02:04:32 +00003192** Delete an entire SrcList including all its substructure.
3193*/
drh633e6d52008-07-28 19:34:53 +00003194void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00003195 int i;
drhbe5c89a2004-07-26 00:31:09 +00003196 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00003197 if( pList==0 ) return;
drhbe5c89a2004-07-26 00:31:09 +00003198 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
drh633e6d52008-07-28 19:34:53 +00003199 sqlite3DbFree(db, pItem->zDatabase);
3200 sqlite3DbFree(db, pItem->zName);
3201 sqlite3DbFree(db, pItem->zAlias);
danielk197785574e32008-10-06 05:32:18 +00003202 sqlite3DbFree(db, pItem->zIndex);
danielk1977a04a34f2007-04-16 15:06:25 +00003203 sqlite3DeleteTable(pItem->pTab);
drh633e6d52008-07-28 19:34:53 +00003204 sqlite3SelectDelete(db, pItem->pSelect);
3205 sqlite3ExprDelete(db, pItem->pOn);
3206 sqlite3IdListDelete(db, pItem->pUsing);
drh75897232000-05-29 14:26:00 +00003207 }
drh633e6d52008-07-28 19:34:53 +00003208 sqlite3DbFree(db, pList);
drh75897232000-05-29 14:26:00 +00003209}
3210
drh982cef72000-05-30 16:27:03 +00003211/*
drh61dfc312006-12-16 16:25:15 +00003212** This routine is called by the parser to add a new term to the
3213** end of a growing FROM clause. The "p" parameter is the part of
3214** the FROM clause that has already been constructed. "p" is NULL
3215** if this is the first term of the FROM clause. pTable and pDatabase
3216** are the name of the table and database named in the FROM clause term.
3217** pDatabase is NULL if the database name qualifier is missing - the
3218** usual case. If the term has a alias, then pAlias points to the
3219** alias token. If the term is a subquery, then pSubquery is the
3220** SELECT statement that the subquery encodes. The pTable and
3221** pDatabase parameters are NULL for subqueries. The pOn and pUsing
3222** parameters are the content of the ON and USING clauses.
3223**
3224** Return a new SrcList which encodes is the FROM with the new
3225** term added.
3226*/
3227SrcList *sqlite3SrcListAppendFromTerm(
drh17435752007-08-16 04:30:38 +00003228 Parse *pParse, /* Parsing context */
drh61dfc312006-12-16 16:25:15 +00003229 SrcList *p, /* The left part of the FROM clause already seen */
3230 Token *pTable, /* Name of the table to add to the FROM clause */
3231 Token *pDatabase, /* Name of the database containing pTable */
3232 Token *pAlias, /* The right-hand side of the AS subexpression */
3233 Select *pSubquery, /* A subquery used in place of a table name */
3234 Expr *pOn, /* The ON clause of a join */
3235 IdList *pUsing /* The USING clause of a join */
3236){
3237 struct SrcList_item *pItem;
drh17435752007-08-16 04:30:38 +00003238 sqlite3 *db = pParse->db;
danielk1977bd1a0a42009-07-01 16:12:07 +00003239 if( !p && (pOn || pUsing) ){
3240 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
3241 (pOn ? "ON" : "USING")
3242 );
3243 goto append_from_error;
3244 }
drh17435752007-08-16 04:30:38 +00003245 p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
drh8af73d42009-05-13 22:58:28 +00003246 if( p==0 || NEVER(p->nSrc==0) ){
danielk1977bd1a0a42009-07-01 16:12:07 +00003247 goto append_from_error;
drh61dfc312006-12-16 16:25:15 +00003248 }
3249 pItem = &p->a[p->nSrc-1];
drh8af73d42009-05-13 22:58:28 +00003250 assert( pAlias!=0 );
3251 if( pAlias->n ){
drh17435752007-08-16 04:30:38 +00003252 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
drh61dfc312006-12-16 16:25:15 +00003253 }
3254 pItem->pSelect = pSubquery;
danielk1977bd1a0a42009-07-01 16:12:07 +00003255 pItem->pOn = pOn;
3256 pItem->pUsing = pUsing;
drh61dfc312006-12-16 16:25:15 +00003257 return p;
danielk1977bd1a0a42009-07-01 16:12:07 +00003258
3259 append_from_error:
3260 assert( p==0 );
3261 sqlite3ExprDelete(db, pOn);
3262 sqlite3IdListDelete(db, pUsing);
3263 sqlite3SelectDelete(db, pSubquery);
3264 return 0;
drh61dfc312006-12-16 16:25:15 +00003265}
3266
3267/*
danielk1977b1c685b2008-10-06 16:18:39 +00003268** Add an INDEXED BY or NOT INDEXED clause to the most recently added
3269** element of the source-list passed as the second argument.
3270*/
3271void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
drh8af73d42009-05-13 22:58:28 +00003272 assert( pIndexedBy!=0 );
3273 if( p && ALWAYS(p->nSrc>0) ){
danielk1977b1c685b2008-10-06 16:18:39 +00003274 struct SrcList_item *pItem = &p->a[p->nSrc-1];
3275 assert( pItem->notIndexed==0 && pItem->zIndex==0 );
3276 if( pIndexedBy->n==1 && !pIndexedBy->z ){
3277 /* A "NOT INDEXED" clause was supplied. See parse.y
3278 ** construct "indexed_opt" for details. */
3279 pItem->notIndexed = 1;
3280 }else{
3281 pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
3282 }
3283 }
3284}
3285
3286/*
drh61dfc312006-12-16 16:25:15 +00003287** When building up a FROM clause in the parser, the join operator
3288** is initially attached to the left operand. But the code generator
3289** expects the join operator to be on the right operand. This routine
3290** Shifts all join operators from left to right for an entire FROM
3291** clause.
3292**
3293** Example: Suppose the join is like this:
3294**
3295** A natural cross join B
3296**
3297** The operator is "natural cross join". The A and B operands are stored
3298** in p->a[0] and p->a[1], respectively. The parser initially stores the
3299** operator with A. This routine shifts that operator over to B.
3300*/
3301void sqlite3SrcListShiftJoinType(SrcList *p){
3302 if( p && p->a ){
3303 int i;
3304 for(i=p->nSrc-1; i>0; i--){
3305 p->a[i].jointype = p->a[i-1].jointype;
3306 }
3307 p->a[0].jointype = 0;
3308 }
3309}
3310
3311/*
drhc4a3c772001-04-04 11:48:57 +00003312** Begin a transaction
3313*/
drh684917c2004-10-05 02:41:42 +00003314void sqlite3BeginTransaction(Parse *pParse, int type){
drh9bb575f2004-09-06 17:24:11 +00003315 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00003316 Vdbe *v;
drh684917c2004-10-05 02:41:42 +00003317 int i;
drh5e00f6c2001-09-13 13:46:56 +00003318
drhd3001712009-05-12 17:46:53 +00003319 assert( pParse!=0 );
3320 db = pParse->db;
3321 assert( db!=0 );
drh04491712009-05-13 17:21:13 +00003322/* if( db->aDb[0].pBt==0 ) return; */
drhd3001712009-05-12 17:46:53 +00003323 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
3324 return;
3325 }
danielk19771d850a72004-05-31 08:26:49 +00003326 v = sqlite3GetVdbe(pParse);
3327 if( !v ) return;
drh684917c2004-10-05 02:41:42 +00003328 if( type!=TK_DEFERRED ){
3329 for(i=0; i<db->nDb; i++){
drh66a51672008-01-03 00:01:23 +00003330 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
drhfb982642007-08-30 01:19:59 +00003331 sqlite3VdbeUsesBtree(v, i);
drh684917c2004-10-05 02:41:42 +00003332 }
3333 }
drh66a51672008-01-03 00:01:23 +00003334 sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00003335}
3336
3337/*
3338** Commit a transaction
3339*/
danielk19774adee202004-05-08 08:23:19 +00003340void sqlite3CommitTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00003341 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00003342 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00003343
drhd3001712009-05-12 17:46:53 +00003344 assert( pParse!=0 );
3345 db = pParse->db;
3346 assert( db!=0 );
drh04491712009-05-13 17:21:13 +00003347/* if( db->aDb[0].pBt==0 ) return; */
drhd3001712009-05-12 17:46:53 +00003348 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
3349 return;
3350 }
danielk19771d850a72004-05-31 08:26:49 +00003351 v = sqlite3GetVdbe(pParse);
3352 if( v ){
drh66a51672008-01-03 00:01:23 +00003353 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00003354 }
drhc4a3c772001-04-04 11:48:57 +00003355}
3356
3357/*
3358** Rollback a transaction
3359*/
danielk19774adee202004-05-08 08:23:19 +00003360void sqlite3RollbackTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00003361 sqlite3 *db;
drh5e00f6c2001-09-13 13:46:56 +00003362 Vdbe *v;
3363
drhd3001712009-05-12 17:46:53 +00003364 assert( pParse!=0 );
3365 db = pParse->db;
3366 assert( db!=0 );
drh04491712009-05-13 17:21:13 +00003367/* if( db->aDb[0].pBt==0 ) return; */
drhd3001712009-05-12 17:46:53 +00003368 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
3369 return;
3370 }
danielk19774adee202004-05-08 08:23:19 +00003371 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00003372 if( v ){
drh66a51672008-01-03 00:01:23 +00003373 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00003374 }
drhc4a3c772001-04-04 11:48:57 +00003375}
drhf57b14a2001-09-14 18:54:08 +00003376
3377/*
danielk1977fd7f0452008-12-17 17:30:26 +00003378** This function is called by the parser when it parses a command to create,
3379** release or rollback an SQL savepoint.
3380*/
3381void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
danielk1977ab9b7032008-12-30 06:24:58 +00003382 char *zName = sqlite3NameFromToken(pParse->db, pName);
3383 if( zName ){
3384 Vdbe *v = sqlite3GetVdbe(pParse);
3385#ifndef SQLITE_OMIT_AUTHORIZATION
dan558814f2010-06-02 05:53:53 +00003386 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
danielk1977ab9b7032008-12-30 06:24:58 +00003387 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
3388#endif
3389 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
3390 sqlite3DbFree(pParse->db, zName);
3391 return;
3392 }
3393 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
danielk1977fd7f0452008-12-17 17:30:26 +00003394 }
3395}
3396
3397/*
drhdc3ff9c2004-08-18 02:10:15 +00003398** Make sure the TEMP database is open and available for use. Return
3399** the number of errors. Leave any error messages in the pParse structure.
3400*/
danielk1977ddfb2f02006-02-17 12:25:14 +00003401int sqlite3OpenTempDatabase(Parse *pParse){
drhdc3ff9c2004-08-18 02:10:15 +00003402 sqlite3 *db = pParse->db;
3403 if( db->aDb[1].pBt==0 && !pParse->explain ){
drh33f4e022007-09-03 15:19:34 +00003404 int rc;
drh10a76c92010-01-26 01:25:26 +00003405 Btree *pBt;
drh33f4e022007-09-03 15:19:34 +00003406 static const int flags =
3407 SQLITE_OPEN_READWRITE |
3408 SQLITE_OPEN_CREATE |
3409 SQLITE_OPEN_EXCLUSIVE |
3410 SQLITE_OPEN_DELETEONCLOSE |
3411 SQLITE_OPEN_TEMP_DB;
3412
drh10a76c92010-01-26 01:25:26 +00003413 rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE, flags, &pBt);
drhdc3ff9c2004-08-18 02:10:15 +00003414 if( rc!=SQLITE_OK ){
3415 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
3416 "file for storing temporary tables");
3417 pParse->rc = rc;
3418 return 1;
3419 }
drh10a76c92010-01-26 01:25:26 +00003420 db->aDb[1].pBt = pBt;
danielk197714db2662006-01-09 16:12:04 +00003421 assert( db->aDb[1].pSchema );
drh10a76c92010-01-26 01:25:26 +00003422 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
3423 db->mallocFailed = 1;
drh7c9c9862010-01-31 14:18:21 +00003424 return 1;
drh10a76c92010-01-26 01:25:26 +00003425 }
drh0b9b4302010-06-11 17:01:24 +00003426 sqlite3PagerSetJournalMode(sqlite3BtreePager(pBt), db->dfltJournalMode);
drhdc3ff9c2004-08-18 02:10:15 +00003427 }
3428 return 0;
3429}
3430
3431/*
drh80242052004-06-09 00:48:12 +00003432** Generate VDBE code that will verify the schema cookie and start
3433** a read-transaction for all named database files.
3434**
3435** It is important that all schema cookies be verified and all
3436** read transactions be started before anything else happens in
3437** the VDBE program. But this routine can be called after much other
3438** code has been generated. So here is what we do:
3439**
drhc275b4e2004-07-19 17:25:24 +00003440** The first time this routine is called, we code an OP_Goto that
drh80242052004-06-09 00:48:12 +00003441** will jump to a subroutine at the end of the program. Then we
3442** record every database that needs its schema verified in the
3443** pParse->cookieMask field. Later, after all other code has been
3444** generated, the subroutine that does the cookie verifications and
drhc275b4e2004-07-19 17:25:24 +00003445** starts the transactions will be coded and the OP_Goto P2 value
drh80242052004-06-09 00:48:12 +00003446** will be made to point to that subroutine. The generation of the
3447** cookie verification subroutine code happens in sqlite3FinishCoding().
drhc275b4e2004-07-19 17:25:24 +00003448**
3449** If iDb<0 then code the OP_Goto only - don't set flag to verify the
3450** schema on any databases. This can be used to position the OP_Goto
3451** early in the code, before we know if any database tables will be used.
drh001bbcb2003-03-19 03:14:00 +00003452*/
danielk19774adee202004-05-08 08:23:19 +00003453void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
dan65a7cd12009-09-01 12:16:01 +00003454 Parse *pToplevel = sqlite3ParseToplevel(pParse);
drh80242052004-06-09 00:48:12 +00003455
dan65a7cd12009-09-01 12:16:01 +00003456 if( pToplevel->cookieGoto==0 ){
3457 Vdbe *v = sqlite3GetVdbe(pToplevel);
3458 if( v==0 ) return; /* This only happens if there was a prior error */
3459 pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
drh80242052004-06-09 00:48:12 +00003460 }
drhc275b4e2004-07-19 17:25:24 +00003461 if( iDb>=0 ){
dan65a7cd12009-09-01 12:16:01 +00003462 sqlite3 *db = pToplevel->db;
3463 int mask;
3464
drhc275b4e2004-07-19 17:25:24 +00003465 assert( iDb<db->nDb );
3466 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
drhc797d4d2007-05-08 01:08:49 +00003467 assert( iDb<SQLITE_MAX_ATTACHED+2 );
drhc275b4e2004-07-19 17:25:24 +00003468 mask = 1<<iDb;
dan65a7cd12009-09-01 12:16:01 +00003469 if( (pToplevel->cookieMask & mask)==0 ){
3470 pToplevel->cookieMask |= mask;
3471 pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
danielk197753c0f742005-03-29 03:10:59 +00003472 if( !OMIT_TEMPDB && iDb==1 ){
dan65a7cd12009-09-01 12:16:01 +00003473 sqlite3OpenTempDatabase(pToplevel);
drhdc3ff9c2004-08-18 02:10:15 +00003474 }
drhc275b4e2004-07-19 17:25:24 +00003475 }
drh001bbcb2003-03-19 03:14:00 +00003476 }
drh001bbcb2003-03-19 03:14:00 +00003477}
3478
3479/*
drh1c928532002-01-31 15:54:21 +00003480** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00003481** might change the database.
3482**
3483** This routine starts a new transaction if we are not already within
3484** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00003485** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00003486** be set for operations that might fail (due to a constraint) part of
3487** the way through and which will need to undo some writes without having to
3488** rollback the whole transaction. For operations where all constraints
3489** can be checked before any changes are made to the database, it is never
3490** necessary to undo a write and the checkpoint should not be set.
drh1c928532002-01-31 15:54:21 +00003491*/
drh7f0f12e2004-05-21 13:39:50 +00003492void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
dan65a7cd12009-09-01 12:16:01 +00003493 Parse *pToplevel = sqlite3ParseToplevel(pParse);
drh80242052004-06-09 00:48:12 +00003494 sqlite3CodeVerifySchema(pParse, iDb);
dan65a7cd12009-09-01 12:16:01 +00003495 pToplevel->writeMask |= 1<<iDb;
dane0af83a2009-09-08 19:15:01 +00003496 pToplevel->isMultiWrite |= setStatement;
3497}
3498
drhff738bc2009-09-24 00:09:58 +00003499/*
3500** Indicate that the statement currently under construction might write
3501** more than one entry (example: deleting one row then inserting another,
3502** inserting multiple rows in a table, or inserting a row and index entries.)
3503** If an abort occurs after some of these writes have completed, then it will
3504** be necessary to undo the completed writes.
3505*/
3506void sqlite3MultiWrite(Parse *pParse){
3507 Parse *pToplevel = sqlite3ParseToplevel(pParse);
3508 pToplevel->isMultiWrite = 1;
3509}
3510
dane0af83a2009-09-08 19:15:01 +00003511/*
drhff738bc2009-09-24 00:09:58 +00003512** The code generator calls this routine if is discovers that it is
3513** possible to abort a statement prior to completion. In order to
3514** perform this abort without corrupting the database, we need to make
3515** sure that the statement is protected by a statement transaction.
3516**
3517** Technically, we only need to set the mayAbort flag if the
3518** isMultiWrite flag was previously set. There is a time dependency
3519** such that the abort must occur after the multiwrite. This makes
3520** some statements involving the REPLACE conflict resolution algorithm
3521** go a little faster. But taking advantage of this time dependency
3522** makes it more difficult to prove that the code is correct (in
3523** particular, it prevents us from writing an effective
3524** implementation of sqlite3AssertMayAbort()) and so we have chosen
3525** to take the safe route and skip the optimization.
dane0af83a2009-09-08 19:15:01 +00003526*/
3527void sqlite3MayAbort(Parse *pParse){
3528 Parse *pToplevel = sqlite3ParseToplevel(pParse);
3529 pToplevel->mayAbort = 1;
3530}
3531
3532/*
3533** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
3534** error. The onError parameter determines which (if any) of the statement
3535** and/or current transaction is rolled back.
3536*/
3537void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
3538 Vdbe *v = sqlite3GetVdbe(pParse);
3539 if( onError==OE_Abort ){
3540 sqlite3MayAbort(pParse);
danielk19771d850a72004-05-31 08:26:49 +00003541 }
dane0af83a2009-09-08 19:15:01 +00003542 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
drh663fc632002-02-02 18:49:19 +00003543}
3544
drh4343fea2004-11-05 23:46:15 +00003545/*
3546** Check to see if pIndex uses the collating sequence pColl. Return
3547** true if it does and false if it does not.
3548*/
3549#ifndef SQLITE_OMIT_REINDEX
danielk1977b3bf5562006-01-10 17:58:23 +00003550static int collationMatch(const char *zColl, Index *pIndex){
3551 int i;
drh04491712009-05-13 17:21:13 +00003552 assert( zColl!=0 );
danielk1977b3bf5562006-01-10 17:58:23 +00003553 for(i=0; i<pIndex->nColumn; i++){
3554 const char *z = pIndex->azColl[i];
drh04491712009-05-13 17:21:13 +00003555 assert( z!=0 );
3556 if( 0==sqlite3StrICmp(z, zColl) ){
danielk1977b3bf5562006-01-10 17:58:23 +00003557 return 1;
3558 }
drh4343fea2004-11-05 23:46:15 +00003559 }
3560 return 0;
3561}
3562#endif
3563
3564/*
3565** Recompute all indices of pTab that use the collating sequence pColl.
3566** If pColl==0 then recompute all indices of pTab.
3567*/
3568#ifndef SQLITE_OMIT_REINDEX
danielk1977b3bf5562006-01-10 17:58:23 +00003569static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
drh4343fea2004-11-05 23:46:15 +00003570 Index *pIndex; /* An index associated with pTab */
3571
3572 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
danielk1977b3bf5562006-01-10 17:58:23 +00003573 if( zColl==0 || collationMatch(zColl, pIndex) ){
danielk1977da184232006-01-05 11:34:32 +00003574 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
3575 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4343fea2004-11-05 23:46:15 +00003576 sqlite3RefillIndex(pParse, pIndex, -1);
3577 }
3578 }
3579}
3580#endif
3581
3582/*
3583** Recompute all indices of all tables in all databases where the
3584** indices use the collating sequence pColl. If pColl==0 then recompute
3585** all indices everywhere.
3586*/
3587#ifndef SQLITE_OMIT_REINDEX
danielk1977b3bf5562006-01-10 17:58:23 +00003588static void reindexDatabases(Parse *pParse, char const *zColl){
drh4343fea2004-11-05 23:46:15 +00003589 Db *pDb; /* A single database */
3590 int iDb; /* The database index number */
3591 sqlite3 *db = pParse->db; /* The database connection */
3592 HashElem *k; /* For looping over tables in pDb */
3593 Table *pTab; /* A table in the database */
3594
3595 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
drh43617e92006-03-06 20:55:46 +00003596 assert( pDb!=0 );
danielk1977da184232006-01-05 11:34:32 +00003597 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
drh4343fea2004-11-05 23:46:15 +00003598 pTab = (Table*)sqliteHashData(k);
danielk1977b3bf5562006-01-10 17:58:23 +00003599 reindexTable(pParse, pTab, zColl);
drh4343fea2004-11-05 23:46:15 +00003600 }
3601 }
3602}
3603#endif
3604
3605/*
drheee46cf2004-11-06 00:02:48 +00003606** Generate code for the REINDEX command.
3607**
3608** REINDEX -- 1
3609** REINDEX <collation> -- 2
3610** REINDEX ?<database>.?<tablename> -- 3
3611** REINDEX ?<database>.?<indexname> -- 4
3612**
3613** Form 1 causes all indices in all attached databases to be rebuilt.
3614** Form 2 rebuilds all indices in all databases that use the named
3615** collating function. Forms 3 and 4 rebuild the named index or all
3616** indices associated with the named table.
drh4343fea2004-11-05 23:46:15 +00003617*/
3618#ifndef SQLITE_OMIT_REINDEX
3619void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
3620 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
3621 char *z; /* Name of a table or index */
3622 const char *zDb; /* Name of the database */
3623 Table *pTab; /* A table in the database */
3624 Index *pIndex; /* An index associated with pTab */
3625 int iDb; /* The database index number */
3626 sqlite3 *db = pParse->db; /* The database connection */
3627 Token *pObjName; /* Name of the table or index to be reindexed */
3628
danielk197733a5edc2005-01-27 00:22:02 +00003629 /* Read the database schema. If an error occurs, leave an error message
3630 ** and code in pParse and return NULL. */
3631 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977e63739a2005-01-27 00:33:37 +00003632 return;
danielk197733a5edc2005-01-27 00:22:02 +00003633 }
3634
drh8af73d42009-05-13 22:58:28 +00003635 if( pName1==0 ){
drh4343fea2004-11-05 23:46:15 +00003636 reindexDatabases(pParse, 0);
3637 return;
drhd3001712009-05-12 17:46:53 +00003638 }else if( NEVER(pName2==0) || pName2->z==0 ){
danielk197739002502007-11-12 09:50:26 +00003639 char *zColl;
danielk1977b3bf5562006-01-10 17:58:23 +00003640 assert( pName1->z );
danielk197739002502007-11-12 09:50:26 +00003641 zColl = sqlite3NameFromToken(pParse->db, pName1);
3642 if( !zColl ) return;
drhc4a64fa2009-05-11 20:53:28 +00003643 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
drh4343fea2004-11-05 23:46:15 +00003644 if( pColl ){
drhd3001712009-05-12 17:46:53 +00003645 reindexDatabases(pParse, zColl);
3646 sqlite3DbFree(db, zColl);
drh4343fea2004-11-05 23:46:15 +00003647 return;
3648 }
drh633e6d52008-07-28 19:34:53 +00003649 sqlite3DbFree(db, zColl);
drh4343fea2004-11-05 23:46:15 +00003650 }
3651 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
3652 if( iDb<0 ) return;
drh17435752007-08-16 04:30:38 +00003653 z = sqlite3NameFromToken(db, pObjName);
drh84f31122007-05-12 15:00:14 +00003654 if( z==0 ) return;
drh4343fea2004-11-05 23:46:15 +00003655 zDb = db->aDb[iDb].zName;
3656 pTab = sqlite3FindTable(db, z, zDb);
3657 if( pTab ){
3658 reindexTable(pParse, pTab, 0);
drh633e6d52008-07-28 19:34:53 +00003659 sqlite3DbFree(db, z);
drh4343fea2004-11-05 23:46:15 +00003660 return;
3661 }
3662 pIndex = sqlite3FindIndex(db, z, zDb);
drh633e6d52008-07-28 19:34:53 +00003663 sqlite3DbFree(db, z);
drh4343fea2004-11-05 23:46:15 +00003664 if( pIndex ){
3665 sqlite3BeginWriteOperation(pParse, 0, iDb);
3666 sqlite3RefillIndex(pParse, pIndex, -1);
3667 return;
3668 }
3669 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
3670}
3671#endif
danielk1977b3bf5562006-01-10 17:58:23 +00003672
3673/*
3674** Return a dynamicly allocated KeyInfo structure that can be used
3675** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
3676**
3677** If successful, a pointer to the new structure is returned. In this case
drh633e6d52008-07-28 19:34:53 +00003678** the caller is responsible for calling sqlite3DbFree(db, ) on the returned
danielk1977b3bf5562006-01-10 17:58:23 +00003679** pointer. If an error occurs (out of memory or missing collation
3680** sequence), NULL is returned and the state of pParse updated to reflect
3681** the error.
3682*/
3683KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
3684 int i;
3685 int nCol = pIdx->nColumn;
3686 int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
drh633e6d52008-07-28 19:34:53 +00003687 sqlite3 *db = pParse->db;
3688 KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
danielk1977b3bf5562006-01-10 17:58:23 +00003689
3690 if( pKey ){
drhb21c8cd2007-08-21 19:33:56 +00003691 pKey->db = pParse->db;
danielk1977b3bf5562006-01-10 17:58:23 +00003692 pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
3693 assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
3694 for(i=0; i<nCol; i++){
3695 char *zColl = pIdx->azColl[i];
3696 assert( zColl );
drhc4a64fa2009-05-11 20:53:28 +00003697 pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
danielk1977b3bf5562006-01-10 17:58:23 +00003698 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
3699 }
drh1bd10f82008-12-10 21:19:56 +00003700 pKey->nField = (u16)nCol;
danielk1977b3bf5562006-01-10 17:58:23 +00003701 }
3702
3703 if( pParse->nErr ){
drh633e6d52008-07-28 19:34:53 +00003704 sqlite3DbFree(db, pKey);
danielk1977b3bf5562006-01-10 17:58:23 +00003705 pKey = 0;
3706 }
3707 return pKey;
3708}