blob: a68137ded099014aa344dcd9e8cafe6ed2240dc7 [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
drhbed86902000-06-02 13:27:59 +000024**
drhf3a65f72007-08-22 20:18:21 +000025** $Id: build.c,v 1.438 2007/08/22 20:18:22 drh Exp $
drh75897232000-05-29 14:26:00 +000026*/
27#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000028#include <ctype.h>
drh75897232000-05-29 14:26:00 +000029
30/*
drhe0bc4042002-06-25 01:09:11 +000031** This routine is called when a new SQL statement is beginning to
drh23bf66d2004-12-14 03:34:34 +000032** be parsed. Initialize the pParse structure as needed.
drhe0bc4042002-06-25 01:09:11 +000033*/
danielk19774adee202004-05-08 08:23:19 +000034void sqlite3BeginParse(Parse *pParse, int explainFlag){
drhe0bc4042002-06-25 01:09:11 +000035 pParse->explain = explainFlag;
drh7c972de2003-09-06 22:18:07 +000036 pParse->nVar = 0;
drhe0bc4042002-06-25 01:09:11 +000037}
38
danielk1977c00da102006-01-07 13:21:04 +000039#ifndef SQLITE_OMIT_SHARED_CACHE
40/*
41** The TableLock structure is only used by the sqlite3TableLock() and
42** codeTableLocks() functions.
43*/
44struct TableLock {
drhd698bc12006-03-23 23:33:26 +000045 int iDb; /* The database containing the table to be locked */
46 int iTab; /* The root page of the table to be locked */
47 u8 isWriteLock; /* True for write lock. False for a read lock */
48 const char *zName; /* Name of the table */
danielk1977c00da102006-01-07 13:21:04 +000049};
50
51/*
drhd698bc12006-03-23 23:33:26 +000052** Record the fact that we want to lock a table at run-time.
danielk1977c00da102006-01-07 13:21:04 +000053**
drhd698bc12006-03-23 23:33:26 +000054** The table to be locked has root page iTab and is found in database iDb.
55** A read or a write lock can be taken depending on isWritelock.
56**
57** This routine just records the fact that the lock is desired. The
58** code to make the lock occur is generated by a later call to
59** codeTableLocks() which occurs during sqlite3FinishCoding().
danielk1977c00da102006-01-07 13:21:04 +000060*/
61void sqlite3TableLock(
drhd698bc12006-03-23 23:33:26 +000062 Parse *pParse, /* Parsing context */
63 int iDb, /* Index of the database containing the table to lock */
64 int iTab, /* Root page number of the table to be locked */
65 u8 isWriteLock, /* True for a write lock */
66 const char *zName /* Name of the table to be locked */
danielk1977c00da102006-01-07 13:21:04 +000067){
68 int i;
69 int nBytes;
70 TableLock *p;
danielk1977c00da102006-01-07 13:21:04 +000071
drhe53831d2007-08-17 01:14:38 +000072 if( iDb<0 ){
danielk1977c00da102006-01-07 13:21:04 +000073 return;
74 }
75
76 for(i=0; i<pParse->nTableLock; i++){
77 p = &pParse->aTableLock[i];
78 if( p->iDb==iDb && p->iTab==iTab ){
79 p->isWriteLock = (p->isWriteLock || isWriteLock);
80 return;
81 }
82 }
83
84 nBytes = sizeof(TableLock) * (pParse->nTableLock+1);
drh17435752007-08-16 04:30:38 +000085 pParse->aTableLock =
86 sqlite3DbReallocOrFree(pParse->db, pParse->aTableLock, nBytes);
danielk1977c00da102006-01-07 13:21:04 +000087 if( pParse->aTableLock ){
88 p = &pParse->aTableLock[pParse->nTableLock++];
89 p->iDb = iDb;
90 p->iTab = iTab;
91 p->isWriteLock = isWriteLock;
92 p->zName = zName;
drhf3a65f72007-08-22 20:18:21 +000093 }else{
94 pParse->nTableLock = 0;
95 pParse->db->mallocFailed = 1;
danielk1977c00da102006-01-07 13:21:04 +000096 }
97}
98
99/*
100** Code an OP_TableLock instruction for each table locked by the
101** statement (configured by calls to sqlite3TableLock()).
102*/
103static void codeTableLocks(Parse *pParse){
104 int i;
105 Vdbe *pVdbe;
danielk1977c00da102006-01-07 13:21:04 +0000106
107 if( 0==(pVdbe = sqlite3GetVdbe(pParse)) ){
108 return;
109 }
110
111 for(i=0; i<pParse->nTableLock; i++){
112 TableLock *p = &pParse->aTableLock[i];
113 int p1 = p->iDb;
114 if( p->isWriteLock ){
115 p1 = -1*(p1+1);
116 }
117 sqlite3VdbeOp3(pVdbe, OP_TableLock, p1, p->iTab, p->zName, P3_STATIC);
118 }
119}
120#else
121 #define codeTableLocks(x)
122#endif
123
drhe0bc4042002-06-25 01:09:11 +0000124/*
drh75897232000-05-29 14:26:00 +0000125** This routine is called after a single SQL statement has been
drh80242052004-06-09 00:48:12 +0000126** parsed and a VDBE program to execute that statement has been
127** prepared. This routine puts the finishing touches on the
128** VDBE program and resets the pParse structure for the next
129** parse.
drh75897232000-05-29 14:26:00 +0000130**
131** Note that if an error occurred, it might be the case that
132** no VDBE code was generated.
133*/
drh80242052004-06-09 00:48:12 +0000134void sqlite3FinishCoding(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000135 sqlite3 *db;
drh80242052004-06-09 00:48:12 +0000136 Vdbe *v;
drhb86ccfb2003-01-28 23:13:10 +0000137
drh17435752007-08-16 04:30:38 +0000138 db = pParse->db;
139 if( db->mallocFailed ) return;
drh205f48e2004-11-05 00:43:11 +0000140 if( pParse->nested ) return;
danielk1977441daf62005-02-01 03:46:43 +0000141 if( !pParse->pVdbe ){
danielk1977c30f9e72005-02-09 07:05:46 +0000142 if( pParse->rc==SQLITE_OK && pParse->nErr ){
143 pParse->rc = SQLITE_ERROR;
drhe134ff12006-02-18 16:36:45 +0000144 return;
danielk1977c30f9e72005-02-09 07:05:46 +0000145 }
danielk1977441daf62005-02-01 03:46:43 +0000146 }
danielk197748d0d862005-02-01 03:09:52 +0000147
drh80242052004-06-09 00:48:12 +0000148 /* Begin by generating some termination code at the end of the
149 ** vdbe program
150 */
drh80242052004-06-09 00:48:12 +0000151 v = sqlite3GetVdbe(pParse);
152 if( v ){
153 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
drh0e3d7472004-06-19 17:33:07 +0000154
155 /* The cookie mask contains one bit for each database file open.
156 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
157 ** set for each database that is used. Generate code to start a
158 ** transaction on each used database and to verify the schema cookie
159 ** on each used database.
160 */
drhc275b4e2004-07-19 17:25:24 +0000161 if( pParse->cookieGoto>0 ){
drh80242052004-06-09 00:48:12 +0000162 u32 mask;
163 int iDb;
drhd654be82005-09-20 17:42:23 +0000164 sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
drh80242052004-06-09 00:48:12 +0000165 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
166 if( (mask & pParse->cookieMask)==0 ) continue;
167 sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
drhc275b4e2004-07-19 17:25:24 +0000168 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
drh80242052004-06-09 00:48:12 +0000169 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000170#ifndef SQLITE_OMIT_VIRTUALTABLE
171 if( pParse->pVirtualLock ){
172 char *vtab = (char *)pParse->pVirtualLock->pVtab;
173 sqlite3VdbeOp3(v, OP_VBegin, 0, 0, vtab, P3_VTAB);
174 }
175#endif
danielk1977c00da102006-01-07 13:21:04 +0000176
177 /* Once all the cookies have been verified and transactions opened,
178 ** obtain the required table-locks. This is a no-op unless the
179 ** shared-cache feature is enabled.
180 */
181 codeTableLocks(pParse);
drhc275b4e2004-07-19 17:25:24 +0000182 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto);
drh80242052004-06-09 00:48:12 +0000183 }
drh80242052004-06-09 00:48:12 +0000184
drh19e2d372005-08-29 23:00:03 +0000185#ifndef SQLITE_OMIT_TRACE
drh71c697e2004-08-08 23:39:19 +0000186 /* Add a No-op that contains the complete text of the compiled SQL
187 ** statement as its P3 argument. This does not change the functionality
drhc16a03b2004-09-15 13:38:10 +0000188 ** of the program.
189 **
drh23bf66d2004-12-14 03:34:34 +0000190 ** This is used to implement sqlite3_trace().
drh71c697e2004-08-08 23:39:19 +0000191 */
192 sqlite3VdbeOp3(v, OP_Noop, 0, 0, pParse->zSql, pParse->zTail-pParse->zSql);
drh19e2d372005-08-29 23:00:03 +0000193#endif /* SQLITE_OMIT_TRACE */
drh71c697e2004-08-08 23:39:19 +0000194 }
195
drh3f7d4e42004-07-24 14:35:58 +0000196
drh80242052004-06-09 00:48:12 +0000197 /* Get the VDBE program ready for execution
198 */
drh17435752007-08-16 04:30:38 +0000199 if( v && pParse->nErr==0 && !db->mallocFailed ){
drhcf1023c2007-05-08 20:59:49 +0000200#ifdef SQLITE_DEBUG
drhb86ccfb2003-01-28 23:13:10 +0000201 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
danielk19774adee202004-05-08 08:23:19 +0000202 sqlite3VdbeTrace(v, trace);
drhcf1023c2007-05-08 20:59:49 +0000203#endif
drh290c1942004-08-21 17:54:45 +0000204 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem+3,
drh13449892005-09-07 21:22:45 +0000205 pParse->nTab+3, pParse->explain);
danielk1977441daf62005-02-01 03:46:43 +0000206 pParse->rc = SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +0000207 pParse->colNamesSet = 0;
drh826fb5a2004-02-14 23:59:57 +0000208 }else if( pParse->rc==SQLITE_OK ){
drh483750b2003-01-29 18:46:51 +0000209 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000210 }
drha226d052002-09-25 19:04:07 +0000211 pParse->nTab = 0;
212 pParse->nMem = 0;
213 pParse->nSet = 0;
drh7c972de2003-09-06 22:18:07 +0000214 pParse->nVar = 0;
drh80242052004-06-09 00:48:12 +0000215 pParse->cookieMask = 0;
drhc275b4e2004-07-19 17:25:24 +0000216 pParse->cookieGoto = 0;
drh75897232000-05-29 14:26:00 +0000217}
218
219/*
drh205f48e2004-11-05 00:43:11 +0000220** Run the parser and code generator recursively in order to generate
221** code for the SQL statement given onto the end of the pParse context
222** currently under construction. When the parser is run recursively
223** this way, the final OP_Halt is not appended and other initialization
224** and finalization steps are omitted because those are handling by the
225** outermost parser.
226**
227** Not everything is nestable. This facility is designed to permit
228** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
drhf1974842004-11-05 03:56:00 +0000229** care if you decide to try to use this routine for some other purposes.
drh205f48e2004-11-05 00:43:11 +0000230*/
231void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
232 va_list ap;
233 char *zSql;
drhf1974842004-11-05 03:56:00 +0000234# define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar))
235 char saveBuf[SAVE_SZ];
236
drh205f48e2004-11-05 00:43:11 +0000237 if( pParse->nErr ) return;
238 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
239 va_start(ap, zFormat);
danielk19771e536952007-08-16 10:09:01 +0000240 zSql = sqlite3VMPrintf(pParse->db, zFormat, ap);
drh205f48e2004-11-05 00:43:11 +0000241 va_end(ap);
drh73c42a12004-11-20 18:13:10 +0000242 if( zSql==0 ){
243 return; /* A malloc must have failed */
244 }
drh205f48e2004-11-05 00:43:11 +0000245 pParse->nested++;
drhf1974842004-11-05 03:56:00 +0000246 memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
247 memset(&pParse->nVar, 0, SAVE_SZ);
drh4f26bb62005-09-08 14:17:20 +0000248 sqlite3RunParser(pParse, zSql, 0);
drh17435752007-08-16 04:30:38 +0000249 sqlite3_free(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;
drh645f63e2004-06-22 13:22:40 +0000269 assert( zName!=0 );
danielk197753c0f742005-03-29 03:10:59 +0000270 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000271 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000272 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
danielk1977da184232006-01-05 11:34:32 +0000273 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000274 if( p ) break;
275 }
drh74e24cd2002-01-09 03:19:59 +0000276 return p;
drh75897232000-05-29 14:26:00 +0000277}
278
279/*
danielk19778a414492004-06-29 08:59:35 +0000280** Locate the in-memory structure that describes a particular database
281** table given the name of that table and (optionally) the name of the
282** database containing the table. Return NULL if not found. Also leave an
283** error message in pParse->zErrMsg.
drha69d9162003-04-17 22:57:53 +0000284**
danielk19778a414492004-06-29 08:59:35 +0000285** The difference between this routine and sqlite3FindTable() is that this
286** routine leaves an error message in pParse->zErrMsg where
287** sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53 +0000288*/
danielk19774adee202004-05-08 08:23:19 +0000289Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){
drha69d9162003-04-17 22:57:53 +0000290 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000291
danielk19778a414492004-06-29 08:59:35 +0000292 /* Read the database schema. If an error occurs, leave an error message
293 ** and code in pParse and return NULL. */
294 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
295 return 0;
296 }
297
danielk19774adee202004-05-08 08:23:19 +0000298 p = sqlite3FindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000299 if( p==0 ){
danielk19778a414492004-06-29 08:59:35 +0000300 if( zDbase ){
danielk19774adee202004-05-08 08:23:19 +0000301 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
drha69d9162003-04-17 22:57:53 +0000302 }else{
danielk19774adee202004-05-08 08:23:19 +0000303 sqlite3ErrorMsg(pParse, "no such table: %s", zName);
drha69d9162003-04-17 22:57:53 +0000304 }
drha6ecd332004-06-10 00:29:09 +0000305 pParse->checkSchema = 1;
drha69d9162003-04-17 22:57:53 +0000306 }
307 return p;
308}
309
310/*
311** Locate the in-memory structure that describes
312** a particular index given the name of that index
313** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000314** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000315**
316** If zDatabase is 0, all databases are searched for the
317** table and the first matching index is returned. (No checking
318** for duplicate index names is done.) The search order is
319** TEMP first, then MAIN, then any auxiliary databases added
320** using the ATTACH command.
drh75897232000-05-29 14:26:00 +0000321*/
drh9bb575f2004-09-06 17:24:11 +0000322Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24 +0000323 Index *p = 0;
324 int i;
danielk197753c0f742005-03-29 03:10:59 +0000325 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000326 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk1977e501b892006-01-09 06:29:47 +0000327 Schema *pSchema = db->aDb[j].pSchema;
danielk19774adee202004-05-08 08:23:19 +0000328 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
danielk1977da184232006-01-05 11:34:32 +0000329 assert( pSchema || (j==1 && !db->aDb[1].pBt) );
330 if( pSchema ){
331 p = sqlite3HashFind(&pSchema->idxHash, zName, strlen(zName)+1);
332 }
drhd24cc422003-03-27 12:51:24 +0000333 if( p ) break;
334 }
drh74e24cd2002-01-09 03:19:59 +0000335 return p;
drh75897232000-05-29 14:26:00 +0000336}
337
338/*
drh956bc922004-07-24 17:38:29 +0000339** Reclaim the memory used by an index
340*/
341static void freeIndex(Index *p){
drh17435752007-08-16 04:30:38 +0000342 sqlite3_free(p->zColAff);
343 sqlite3_free(p);
drh956bc922004-07-24 17:38:29 +0000344}
345
346/*
drh75897232000-05-29 14:26:00 +0000347** Remove the given index from the index hash table, and free
348** its memory structures.
349**
drhd229ca92002-01-09 13:30:41 +0000350** The index is removed from the database hash tables but
351** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000352** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000353*/
drh74161702006-02-24 02:53:49 +0000354static void sqliteDeleteIndex(Index *p){
drhd229ca92002-01-09 13:30:41 +0000355 Index *pOld;
danielk1977da184232006-01-05 11:34:32 +0000356 const char *zName = p->zName;
drhd24cc422003-03-27 12:51:24 +0000357
danielk1977da184232006-01-05 11:34:32 +0000358 pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName, strlen( zName)+1, 0);
drh85c23c62005-08-20 03:03:04 +0000359 assert( pOld==0 || pOld==p );
drh956bc922004-07-24 17:38:29 +0000360 freeIndex(p);
drh75897232000-05-29 14:26:00 +0000361}
362
363/*
drhc96d8532005-05-03 12:30:33 +0000364** For the index called zIdxName which is found in the database iDb,
365** unlike that index from its Table then remove the index from
366** the index hash table and free all memory structures associated
367** with the index.
drh5e00f6c2001-09-13 13:46:56 +0000368*/
drh9bb575f2004-09-06 17:24:11 +0000369void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
drh956bc922004-07-24 17:38:29 +0000370 Index *pIndex;
371 int len;
danielk1977da184232006-01-05 11:34:32 +0000372 Hash *pHash = &db->aDb[iDb].pSchema->idxHash;
drh956bc922004-07-24 17:38:29 +0000373
374 len = strlen(zIdxName);
danielk1977da184232006-01-05 11:34:32 +0000375 pIndex = sqlite3HashInsert(pHash, zIdxName, len+1, 0);
drh956bc922004-07-24 17:38:29 +0000376 if( pIndex ){
377 if( pIndex->pTable->pIndex==pIndex ){
378 pIndex->pTable->pIndex = pIndex->pNext;
379 }else{
380 Index *p;
381 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
382 if( p && p->pNext==pIndex ){
383 p->pNext = pIndex->pNext;
384 }
drh5e00f6c2001-09-13 13:46:56 +0000385 }
drh956bc922004-07-24 17:38:29 +0000386 freeIndex(pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000387 }
drh956bc922004-07-24 17:38:29 +0000388 db->flags |= SQLITE_InternChanges;
drh5e00f6c2001-09-13 13:46:56 +0000389}
390
391/*
drhe0bc4042002-06-25 01:09:11 +0000392** Erase all schema information from the in-memory hash tables of
drh234c39d2004-07-24 03:30:47 +0000393** a single database. This routine is called to reclaim memory
394** before the database closes. It is also called during a rollback
danielk1977e0d4b062004-06-28 01:11:46 +0000395** if there were schema changes during the transaction or if a
396** schema-cookie mismatch occurs.
drh1c2d8412003-03-31 00:30:47 +0000397**
398** If iDb<=0 then reset the internal schema tables for all database
399** files. If iDb>=2 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000400** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000401*/
drh9bb575f2004-09-06 17:24:11 +0000402void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
drh1c2d8412003-03-31 00:30:47 +0000403 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000404
drh1c2d8412003-03-31 00:30:47 +0000405 assert( iDb>=0 && iDb<db->nDb );
drh1c2d8412003-03-31 00:30:47 +0000406 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000407 Db *pDb = &db->aDb[i];
danielk1977da184232006-01-05 11:34:32 +0000408 if( pDb->pSchema ){
danielk1977de0fe3e2006-01-06 06:33:12 +0000409 sqlite3SchemaFree(pDb->pSchema);
drhd24cc422003-03-27 12:51:24 +0000410 }
drh1c2d8412003-03-31 00:30:47 +0000411 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000412 }
drh1c2d8412003-03-31 00:30:47 +0000413 assert( iDb==0 );
414 db->flags &= ~SQLITE_InternChanges;
415
416 /* If one or more of the auxiliary database files has been closed,
danielk1977311019b2006-01-10 07:14:23 +0000417 ** then remove them from the auxiliary database list. We take the
drh1c2d8412003-03-31 00:30:47 +0000418 ** opportunity to do this here since we have just deleted all of the
419 ** schema hash tables and therefore do not have to make any changes
420 ** to any of those tables.
421 */
drh4d189ca2004-02-12 18:46:38 +0000422 for(i=0; i<db->nDb; i++){
423 struct Db *pDb = &db->aDb[i];
424 if( pDb->pBt==0 ){
425 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
426 pDb->pAux = 0;
427 }
428 }
drh1c2d8412003-03-31 00:30:47 +0000429 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000430 struct Db *pDb = &db->aDb[i];
431 if( pDb->pBt==0 ){
drh17435752007-08-16 04:30:38 +0000432 sqlite3_free(pDb->zName);
drh4d189ca2004-02-12 18:46:38 +0000433 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000434 continue;
435 }
436 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000437 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000438 }
drh8bf8dc92003-05-17 17:35:10 +0000439 j++;
drh1c2d8412003-03-31 00:30:47 +0000440 }
441 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
442 db->nDb = j;
443 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
444 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
drh17435752007-08-16 04:30:38 +0000445 sqlite3_free(db->aDb);
drh1c2d8412003-03-31 00:30:47 +0000446 db->aDb = db->aDbStatic;
447 }
drhe0bc4042002-06-25 01:09:11 +0000448}
449
450/*
drhe0bc4042002-06-25 01:09:11 +0000451** This routine is called when a commit occurs.
452*/
drh9bb575f2004-09-06 17:24:11 +0000453void sqlite3CommitInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000454 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000455}
456
457/*
drh956bc922004-07-24 17:38:29 +0000458** Clear the column names from a table or view.
459*/
460static void sqliteResetColumnNames(Table *pTable){
461 int i;
462 Column *pCol;
463 assert( pTable!=0 );
drhdd5b2fa2005-03-28 03:39:55 +0000464 if( (pCol = pTable->aCol)!=0 ){
465 for(i=0; i<pTable->nCol; i++, pCol++){
drh17435752007-08-16 04:30:38 +0000466 sqlite3_free(pCol->zName);
drhdd5b2fa2005-03-28 03:39:55 +0000467 sqlite3ExprDelete(pCol->pDflt);
drh17435752007-08-16 04:30:38 +0000468 sqlite3_free(pCol->zType);
469 sqlite3_free(pCol->zColl);
drhdd5b2fa2005-03-28 03:39:55 +0000470 }
drh17435752007-08-16 04:30:38 +0000471 sqlite3_free(pTable->aCol);
drh956bc922004-07-24 17:38:29 +0000472 }
drh956bc922004-07-24 17:38:29 +0000473 pTable->aCol = 0;
474 pTable->nCol = 0;
475}
476
477/*
drh75897232000-05-29 14:26:00 +0000478** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000479** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000480**
481** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000482** the table data structure from the hash table. Nor does it remove
483** foreign keys from the sqlite.aFKey hash table. But it does destroy
484** memory structures of the indices and foreign keys associated with
485** the table.
drh75897232000-05-29 14:26:00 +0000486*/
danielk1977a04a34f2007-04-16 15:06:25 +0000487void sqlite3DeleteTable(Table *pTable){
drh75897232000-05-29 14:26:00 +0000488 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000489 FKey *pFKey, *pNextFKey;
490
drh75897232000-05-29 14:26:00 +0000491 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000492
drhed8a3bb2005-06-06 21:19:56 +0000493 /* Do not delete the table until the reference count reaches zero. */
494 pTable->nRef--;
495 if( pTable->nRef>0 ){
496 return;
497 }
498 assert( pTable->nRef==0 );
499
drhc2eef3b2002-08-31 18:53:06 +0000500 /* Delete all indices associated with this table
501 */
502 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
503 pNext = pIndex->pNext;
danielk1977da184232006-01-05 11:34:32 +0000504 assert( pIndex->pSchema==pTable->pSchema );
drh74161702006-02-24 02:53:49 +0000505 sqliteDeleteIndex(pIndex);
drhc2eef3b2002-08-31 18:53:06 +0000506 }
507
danielk1977576ec6b2005-01-21 11:55:25 +0000508#ifndef SQLITE_OMIT_FOREIGN_KEY
drhc2eef3b2002-08-31 18:53:06 +0000509 /* Delete all foreign keys associated with this table. The keys
danielk1977a04a34f2007-04-16 15:06:25 +0000510 ** should have already been unlinked from the pSchema->aFKey hash table
drhc2eef3b2002-08-31 18:53:06 +0000511 */
512 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
513 pNextFKey = pFKey->pNextFrom;
danielk1977da184232006-01-05 11:34:32 +0000514 assert( sqlite3HashFind(&pTable->pSchema->aFKey,
drhd24cc422003-03-27 12:51:24 +0000515 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drh17435752007-08-16 04:30:38 +0000516 sqlite3_free(pFKey);
drhc2eef3b2002-08-31 18:53:06 +0000517 }
danielk1977576ec6b2005-01-21 11:55:25 +0000518#endif
drhc2eef3b2002-08-31 18:53:06 +0000519
520 /* Delete the Table structure itself.
521 */
drh956bc922004-07-24 17:38:29 +0000522 sqliteResetColumnNames(pTable);
drh17435752007-08-16 04:30:38 +0000523 sqlite3_free(pTable->zName);
524 sqlite3_free(pTable->zColAff);
danielk19774adee202004-05-08 08:23:19 +0000525 sqlite3SelectDelete(pTable->pSelect);
drhffe07b22005-11-03 00:41:17 +0000526#ifndef SQLITE_OMIT_CHECK
527 sqlite3ExprDelete(pTable->pCheck);
528#endif
drhb9bb7c12006-06-11 23:41:55 +0000529 sqlite3VtabClear(pTable);
drh17435752007-08-16 04:30:38 +0000530 sqlite3_free(pTable);
drh75897232000-05-29 14:26:00 +0000531}
532
533/*
drh5edc3122001-09-13 21:53:09 +0000534** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000535** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000536*/
drh9bb575f2004-09-06 17:24:11 +0000537void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
drh956bc922004-07-24 17:38:29 +0000538 Table *p;
drhc2eef3b2002-08-31 18:53:06 +0000539 FKey *pF1, *pF2;
drh956bc922004-07-24 17:38:29 +0000540 Db *pDb;
541
drhd229ca92002-01-09 13:30:41 +0000542 assert( db!=0 );
drh956bc922004-07-24 17:38:29 +0000543 assert( iDb>=0 && iDb<db->nDb );
544 assert( zTabName && zTabName[0] );
545 pDb = &db->aDb[iDb];
danielk1977da184232006-01-05 11:34:32 +0000546 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, strlen(zTabName)+1,0);
drh956bc922004-07-24 17:38:29 +0000547 if( p ){
danielk1977576ec6b2005-01-21 11:55:25 +0000548#ifndef SQLITE_OMIT_FOREIGN_KEY
drh956bc922004-07-24 17:38:29 +0000549 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
550 int nTo = strlen(pF1->zTo) + 1;
danielk1977da184232006-01-05 11:34:32 +0000551 pF2 = sqlite3HashFind(&pDb->pSchema->aFKey, pF1->zTo, nTo);
drh956bc922004-07-24 17:38:29 +0000552 if( pF2==pF1 ){
danielk1977da184232006-01-05 11:34:32 +0000553 sqlite3HashInsert(&pDb->pSchema->aFKey, pF1->zTo, nTo, pF1->pNextTo);
drh956bc922004-07-24 17:38:29 +0000554 }else{
555 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
556 if( pF2 ){
557 pF2->pNextTo = pF1->pNextTo;
558 }
drhc2eef3b2002-08-31 18:53:06 +0000559 }
560 }
danielk1977576ec6b2005-01-21 11:55:25 +0000561#endif
danielk1977a04a34f2007-04-16 15:06:25 +0000562 sqlite3DeleteTable(p);
drhc2eef3b2002-08-31 18:53:06 +0000563 }
drh956bc922004-07-24 17:38:29 +0000564 db->flags |= SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000565}
566
567/*
drha99db3b2004-06-19 14:49:12 +0000568** Given a token, return a string that consists of the text of that
569** token with any quotations removed. Space to hold the returned string
570** is obtained from sqliteMalloc() and must be freed by the calling
571** function.
drh75897232000-05-29 14:26:00 +0000572**
drhc96d8532005-05-03 12:30:33 +0000573** Tokens are often just pointers into the original SQL text and so
drha99db3b2004-06-19 14:49:12 +0000574** are not \000 terminated and are not persistent. The returned string
575** is \000 terminated and is persistent.
drh75897232000-05-29 14:26:00 +0000576*/
drh17435752007-08-16 04:30:38 +0000577char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
drha99db3b2004-06-19 14:49:12 +0000578 char *zName;
579 if( pName ){
drh17435752007-08-16 04:30:38 +0000580 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
drha99db3b2004-06-19 14:49:12 +0000581 sqlite3Dequote(zName);
582 }else{
583 zName = 0;
584 }
drh75897232000-05-29 14:26:00 +0000585 return zName;
586}
587
588/*
danielk1977cbb18d22004-05-28 11:37:27 +0000589** Open the sqlite_master table stored in database number iDb for
590** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000591*/
danielk1977c00da102006-01-07 13:21:04 +0000592void sqlite3OpenMasterTable(Parse *p, int iDb){
593 Vdbe *v = sqlite3GetVdbe(p);
594 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
danielk1977cbb18d22004-05-28 11:37:27 +0000595 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk19778e150812004-05-10 01:17:37 +0000596 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000597 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000598}
599
600/*
danielk1977cbb18d22004-05-28 11:37:27 +0000601** The token *pName contains the name of a database (either "main" or
602** "temp" or the name of an attached db). This routine returns the
603** index of the named database in db->aDb[], or -1 if the named db
604** does not exist.
605*/
drhff2d5ea2005-07-23 00:41:48 +0000606int sqlite3FindDb(sqlite3 *db, Token *pName){
danielk1977576ec6b2005-01-21 11:55:25 +0000607 int i = -1; /* Database number */
drh73c42a12004-11-20 18:13:10 +0000608 int n; /* Number of characters in the name */
609 Db *pDb; /* A database whose name space is being searched */
610 char *zName; /* Name we are searching for */
611
drh17435752007-08-16 04:30:38 +0000612 zName = sqlite3NameFromToken(db, pName);
drh73c42a12004-11-20 18:13:10 +0000613 if( zName ){
614 n = strlen(zName);
danielk1977576ec6b2005-01-21 11:55:25 +0000615 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
danielk197753c0f742005-03-29 03:10:59 +0000616 if( (!OMIT_TEMPDB || i!=1 ) && n==strlen(pDb->zName) &&
617 0==sqlite3StrICmp(pDb->zName, zName) ){
danielk1977576ec6b2005-01-21 11:55:25 +0000618 break;
drh73c42a12004-11-20 18:13:10 +0000619 }
danielk1977cbb18d22004-05-28 11:37:27 +0000620 }
drh17435752007-08-16 04:30:38 +0000621 sqlite3_free(zName);
danielk1977cbb18d22004-05-28 11:37:27 +0000622 }
danielk1977576ec6b2005-01-21 11:55:25 +0000623 return i;
danielk1977cbb18d22004-05-28 11:37:27 +0000624}
625
drh0e3d7472004-06-19 17:33:07 +0000626/* The table or view or trigger name is passed to this routine via tokens
627** pName1 and pName2. If the table name was fully qualified, for example:
628**
629** CREATE TABLE xxx.yyy (...);
630**
631** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
632** the table name is not fully qualified, i.e.:
633**
634** CREATE TABLE yyy(...);
635**
636** Then pName1 is set to "yyy" and pName2 is "".
637**
638** This routine sets the *ppUnqual pointer to point at the token (pName1 or
639** pName2) that stores the unqualified table name. The index of the
640** database "xxx" is returned.
641*/
danielk1977ef2cb632004-05-29 02:37:19 +0000642int sqlite3TwoPartName(
drh0e3d7472004-06-19 17:33:07 +0000643 Parse *pParse, /* Parsing and code generating context */
drh90f5ecb2004-07-22 01:19:35 +0000644 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
drh0e3d7472004-06-19 17:33:07 +0000645 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
646 Token **pUnqual /* Write the unqualified object name here */
danielk1977cbb18d22004-05-28 11:37:27 +0000647){
drh0e3d7472004-06-19 17:33:07 +0000648 int iDb; /* Database holding the object */
danielk1977cbb18d22004-05-28 11:37:27 +0000649 sqlite3 *db = pParse->db;
650
651 if( pName2 && pName2->n>0 ){
652 assert( !db->init.busy );
653 *pUnqual = pName2;
drhff2d5ea2005-07-23 00:41:48 +0000654 iDb = sqlite3FindDb(db, pName1);
danielk1977cbb18d22004-05-28 11:37:27 +0000655 if( iDb<0 ){
656 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
657 pParse->nErr++;
658 return -1;
659 }
660 }else{
661 assert( db->init.iDb==0 || db->init.busy );
662 iDb = db->init.iDb;
663 *pUnqual = pName1;
664 }
665 return iDb;
666}
667
668/*
danielk1977d8123362004-06-12 09:25:12 +0000669** This routine is used to check if the UTF-8 string zName is a legal
670** unqualified name for a new schema object (table, index, view or
671** trigger). All names are legal except those that begin with the string
672** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
673** is reserved for internal use.
674*/
675int sqlite3CheckObjectName(Parse *pParse, const char *zName){
drhf1974842004-11-05 03:56:00 +0000676 if( !pParse->db->init.busy && pParse->nested==0
danielk19773a3f38e2005-05-22 06:49:56 +0000677 && (pParse->db->flags & SQLITE_WriteSchema)==0
drhf1974842004-11-05 03:56:00 +0000678 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
danielk1977d8123362004-06-12 09:25:12 +0000679 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
680 return SQLITE_ERROR;
681 }
682 return SQLITE_OK;
683}
684
685/*
drh75897232000-05-29 14:26:00 +0000686** Begin constructing a new table representation in memory. This is
687** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000688** to a CREATE TABLE statement. In particular, this routine is called
drh74161702006-02-24 02:53:49 +0000689** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000690** flag is true if the table should be stored in the auxiliary database
691** file instead of in the main database file. This is normally the case
692** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000693** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000694**
drhf57b3392001-10-08 13:22:32 +0000695** The new table record is initialized and put in pParse->pNewTable.
696** As more of the CREATE TABLE statement is parsed, additional action
697** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000698** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000699** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000700*/
danielk19774adee202004-05-08 08:23:19 +0000701void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000702 Parse *pParse, /* Parser context */
danielk1977cbb18d22004-05-28 11:37:27 +0000703 Token *pName1, /* First part of the name of the table or view */
704 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000705 int isTemp, /* True if this is a TEMP table */
drhfaa59552005-12-29 23:33:54 +0000706 int isView, /* True if this is a VIEW */
danielk1977f1a381e2006-06-16 08:01:02 +0000707 int isVirtual, /* True if this is a VIRTUAL table */
drhfaa59552005-12-29 23:33:54 +0000708 int noErr /* Do nothing if table already exists */
drhe5f9c642003-01-13 23:27:31 +0000709){
drh75897232000-05-29 14:26:00 +0000710 Table *pTable;
drh23bf66d2004-12-14 03:34:34 +0000711 char *zName = 0; /* The name of the new table */
drh9bb575f2004-09-06 17:24:11 +0000712 sqlite3 *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000713 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000714 int iDb; /* Database number to create the table in */
715 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000716
danielk1977cbb18d22004-05-28 11:37:27 +0000717 /* The table or view name to create is passed to this routine via tokens
718 ** pName1 and pName2. If the table name was fully qualified, for example:
719 **
720 ** CREATE TABLE xxx.yyy (...);
721 **
722 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
723 ** the table name is not fully qualified, i.e.:
724 **
725 ** CREATE TABLE yyy(...);
726 **
727 ** Then pName1 is set to "yyy" and pName2 is "".
728 **
729 ** The call below sets the pName pointer to point at the token (pName1 or
730 ** pName2) that stores the unqualified table name. The variable iDb is
731 ** set to the index of the database that the table or view is to be
732 ** created in.
733 */
danielk1977ef2cb632004-05-29 02:37:19 +0000734 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000735 if( iDb<0 ) return;
danielk197753c0f742005-03-29 03:10:59 +0000736 if( !OMIT_TEMPDB && isTemp && iDb>1 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000737 /* If creating a temp table, the name may not be qualified */
738 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
danielk1977cbb18d22004-05-28 11:37:27 +0000739 return;
740 }
danielk197753c0f742005-03-29 03:10:59 +0000741 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
danielk1977cbb18d22004-05-28 11:37:27 +0000742
743 pParse->sNameToken = *pName;
drh17435752007-08-16 04:30:38 +0000744 zName = sqlite3NameFromToken(db, pName);
danielk1977e0048402004-06-15 16:51:01 +0000745 if( zName==0 ) return;
danielk1977d8123362004-06-12 09:25:12 +0000746 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drh23bf66d2004-12-14 03:34:34 +0000747 goto begin_table_error;
danielk1977d8123362004-06-12 09:25:12 +0000748 }
drh1d85d932004-02-14 23:05:52 +0000749 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000750#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000751 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000752 {
753 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000754 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000755 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drh23bf66d2004-12-14 03:34:34 +0000756 goto begin_table_error;
drhe22a3342003-04-22 20:30:37 +0000757 }
drhe5f9c642003-01-13 23:27:31 +0000758 if( isView ){
danielk197753c0f742005-03-29 03:10:59 +0000759 if( !OMIT_TEMPDB && isTemp ){
drhe5f9c642003-01-13 23:27:31 +0000760 code = SQLITE_CREATE_TEMP_VIEW;
761 }else{
762 code = SQLITE_CREATE_VIEW;
763 }
764 }else{
danielk197753c0f742005-03-29 03:10:59 +0000765 if( !OMIT_TEMPDB && isTemp ){
drhe5f9c642003-01-13 23:27:31 +0000766 code = SQLITE_CREATE_TEMP_TABLE;
767 }else{
768 code = SQLITE_CREATE_TABLE;
769 }
770 }
danielk1977f1a381e2006-06-16 08:01:02 +0000771 if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh23bf66d2004-12-14 03:34:34 +0000772 goto begin_table_error;
drhe5f9c642003-01-13 23:27:31 +0000773 }
774 }
775#endif
drhf57b3392001-10-08 13:22:32 +0000776
drhf57b3392001-10-08 13:22:32 +0000777 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000778 ** index or table name in the same database. Issue an error message if
danielk19777e6ebfb2006-06-12 11:24:37 +0000779 ** it does. The exception is if the statement being parsed was passed
780 ** to an sqlite3_declare_vtab() call. In that case only the column names
781 ** and types will be used, so there is no need to test for namespace
782 ** collisions.
drhf57b3392001-10-08 13:22:32 +0000783 */
danielk19777e6ebfb2006-06-12 11:24:37 +0000784 if( !IN_DECLARE_VTAB ){
785 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
786 goto begin_table_error;
drhfaa59552005-12-29 23:33:54 +0000787 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000788 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
789 if( pTable ){
790 if( !noErr ){
791 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
792 }
793 goto begin_table_error;
794 }
795 if( sqlite3FindIndex(db, zName, 0)!=0 && (iDb==0 || !db->init.busy) ){
796 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
797 goto begin_table_error;
798 }
drh75897232000-05-29 14:26:00 +0000799 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000800
drh17435752007-08-16 04:30:38 +0000801 pTable = sqlite3MallocZero( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000802 if( pTable==0 ){
drh17435752007-08-16 04:30:38 +0000803 db->mallocFailed = 1;
danielk1977e0048402004-06-15 16:51:01 +0000804 pParse->rc = SQLITE_NOMEM;
805 pParse->nErr++;
drh23bf66d2004-12-14 03:34:34 +0000806 goto begin_table_error;
drh6d4abfb2001-10-22 02:58:08 +0000807 }
drh75897232000-05-29 14:26:00 +0000808 pTable->zName = zName;
drh4a324312001-12-21 14:30:42 +0000809 pTable->iPKey = -1;
danielk1977da184232006-01-05 11:34:32 +0000810 pTable->pSchema = db->aDb[iDb].pSchema;
drhed8a3bb2005-06-06 21:19:56 +0000811 pTable->nRef = 1;
danielk1977a04a34f2007-04-16 15:06:25 +0000812 if( pParse->pNewTable ) sqlite3DeleteTable(pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000813 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000814
drh4794f732004-11-05 17:17:50 +0000815 /* If this is the magic sqlite_sequence table used by autoincrement,
816 ** then record a pointer to this table in the main database structure
817 ** so that INSERT can find the table easily.
818 */
819#ifndef SQLITE_OMIT_AUTOINCREMENT
drh78776ec2005-06-14 02:12:46 +0000820 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
danielk1977da184232006-01-05 11:34:32 +0000821 pTable->pSchema->pSeqTab = pTable;
drh4794f732004-11-05 17:17:50 +0000822 }
823#endif
824
drh17f71932002-02-21 12:01:27 +0000825 /* Begin generating the code that will insert the table record into
826 ** the SQLITE_MASTER table. Note in particular that we must go ahead
827 ** and allocate the record number for the table entry now. Before any
828 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
829 ** indices to be created and the table record must come before the
830 ** indices. Hence, the record number for the table must be allocated
831 ** now.
832 */
danielk19774adee202004-05-08 08:23:19 +0000833 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk197736963fd2005-02-19 08:18:05 +0000834 int lbl;
drhe321c292006-01-12 01:56:43 +0000835 int fileFormat;
danielk1977cbb18d22004-05-28 11:37:27 +0000836 sqlite3BeginWriteOperation(pParse, 0, iDb);
drhb17131a2004-11-05 22:18:49 +0000837
danielk197720b1eaf2006-07-26 16:22:14 +0000838#ifndef SQLITE_OMIT_VIRTUALTABLE
839 if( isVirtual ){
840 sqlite3VdbeAddOp(v, OP_VBegin, 0, 0);
841 }
842#endif
843
danielk197736963fd2005-02-19 08:18:05 +0000844 /* If the file format and encoding in the database have not been set,
845 ** set them now.
danielk1977d008cfe2004-06-19 02:22:10 +0000846 */
danielk197736963fd2005-02-19 08:18:05 +0000847 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1); /* file_format */
848 lbl = sqlite3VdbeMakeLabel(v);
849 sqlite3VdbeAddOp(v, OP_If, 0, lbl);
drhe321c292006-01-12 01:56:43 +0000850 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
drh76fe8032006-07-11 14:17:51 +0000851 1 : SQLITE_MAX_FILE_FORMAT;
drhe321c292006-01-12 01:56:43 +0000852 sqlite3VdbeAddOp(v, OP_Integer, fileFormat, 0);
danielk1977d008cfe2004-06-19 02:22:10 +0000853 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
danielk197714db2662006-01-09 16:12:04 +0000854 sqlite3VdbeAddOp(v, OP_Integer, ENC(db), 0);
danielk1977d008cfe2004-06-19 02:22:10 +0000855 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
danielk197736963fd2005-02-19 08:18:05 +0000856 sqlite3VdbeResolveLabel(v, lbl);
danielk1977d008cfe2004-06-19 02:22:10 +0000857
drh4794f732004-11-05 17:17:50 +0000858 /* This just creates a place-holder record in the sqlite_master table.
859 ** The record created does not contain anything yet. It will be replaced
860 ** by the real entry in code generated at sqlite3EndTable().
drhb17131a2004-11-05 22:18:49 +0000861 **
862 ** The rowid for the new entry is left on the top of the stack.
863 ** The rowid value is needed by the code that sqlite3EndTable will
864 ** generate.
drh4794f732004-11-05 17:17:50 +0000865 */
danielk1977f1a381e2006-06-16 08:01:02 +0000866#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
867 if( isView || isVirtual ){
danielk1977a21c6b62005-01-24 10:25:59 +0000868 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
869 }else
870#endif
871 {
872 sqlite3VdbeAddOp(v, OP_CreateTable, iDb, 0);
873 }
danielk1977c00da102006-01-07 13:21:04 +0000874 sqlite3OpenMasterTable(pParse, iDb);
drhf0863fe2005-06-12 21:35:51 +0000875 sqlite3VdbeAddOp(v, OP_NewRowid, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000876 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhf0863fe2005-06-12 21:35:51 +0000877 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drhe4d90812007-03-29 05:51:49 +0000878 sqlite3VdbeAddOp(v, OP_Insert, 0, OPFLAG_APPEND);
danielk1977e6efa742004-11-10 11:55:10 +0000879 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
danielk1977a21c6b62005-01-24 10:25:59 +0000880 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +0000881 }
drh23bf66d2004-12-14 03:34:34 +0000882
883 /* Normal (non-error) return. */
884 return;
885
886 /* If an error occurs, we jump here */
887begin_table_error:
drh17435752007-08-16 04:30:38 +0000888 sqlite3_free(zName);
drh23bf66d2004-12-14 03:34:34 +0000889 return;
drh75897232000-05-29 14:26:00 +0000890}
891
892/*
danielk1977c60e9b82005-01-31 12:42:29 +0000893** This macro is used to compare two strings in a case-insensitive manner.
894** It is slightly faster than calling sqlite3StrICmp() directly, but
895** produces larger code.
896**
897** WARNING: This macro is not compatible with the strcmp() family. It
898** returns true if the two strings are equal, otherwise false.
899*/
900#define STRICMP(x, y) (\
901sqlite3UpperToLower[*(unsigned char *)(x)]== \
902sqlite3UpperToLower[*(unsigned char *)(y)] \
903&& sqlite3StrICmp((x)+1,(y)+1)==0 )
904
905/*
drh75897232000-05-29 14:26:00 +0000906** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000907**
908** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000909** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000910** first to get things going. Then this routine is called for each
911** column.
drh75897232000-05-29 14:26:00 +0000912*/
danielk19774adee202004-05-08 08:23:19 +0000913void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000914 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000915 int i;
drha99db3b2004-06-19 14:49:12 +0000916 char *z;
drhc9b84a12002-06-20 11:36:48 +0000917 Column *pCol;
drh75897232000-05-29 14:26:00 +0000918 if( (p = pParse->pNewTable)==0 ) return;
drhe5c941b2007-05-08 13:58:26 +0000919 if( p->nCol+1>SQLITE_MAX_COLUMN ){
920 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
921 return;
922 }
drh17435752007-08-16 04:30:38 +0000923 z = sqlite3NameFromToken(pParse->db, pName);
drh97fc3d02002-05-22 21:27:03 +0000924 if( z==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000925 for(i=0; i<p->nCol; i++){
danielk1977c60e9b82005-01-31 12:42:29 +0000926 if( STRICMP(z, p->aCol[i].zName) ){
danielk19774adee202004-05-08 08:23:19 +0000927 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh17435752007-08-16 04:30:38 +0000928 sqlite3_free(z);
drh97fc3d02002-05-22 21:27:03 +0000929 return;
930 }
931 }
drh75897232000-05-29 14:26:00 +0000932 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000933 Column *aNew;
drh17435752007-08-16 04:30:38 +0000934 aNew = sqlite3_realloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000935 if( aNew==0 ){
drh17435752007-08-16 04:30:38 +0000936 pParse->db->mallocFailed = 1;
937 sqlite3_free(z);
danielk1977d5d56522005-03-16 12:15:20 +0000938 return;
939 }
drh6d4abfb2001-10-22 02:58:08 +0000940 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000941 }
drhc9b84a12002-06-20 11:36:48 +0000942 pCol = &p->aCol[p->nCol];
943 memset(pCol, 0, sizeof(p->aCol[0]));
944 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000945
946 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000947 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
948 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000949 */
danielk19774f057f92004-06-08 00:02:33 +0000950 pCol->affinity = SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +0000951 p->nCol++;
drh75897232000-05-29 14:26:00 +0000952}
953
954/*
drh382c0242001-10-06 16:33:02 +0000955** This routine is called by the parser while in the middle of
956** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
957** been seen on a column. This routine sets the notNull flag on
958** the column currently under construction.
959*/
danielk19774adee202004-05-08 08:23:19 +0000960void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000961 Table *p;
962 int i;
963 if( (p = pParse->pNewTable)==0 ) return;
964 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000965 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000966}
967
968/*
danielk197752a83fb2005-01-31 12:56:44 +0000969** Scan the column type name zType (length nType) and return the
970** associated affinity type.
danielk1977b3dff962005-02-01 01:21:55 +0000971**
972** This routine does a case-independent search of zType for the
973** substrings in the following table. If one of the substrings is
974** found, the corresponding affinity is returned. If zType contains
975** more than one of the substrings, entries toward the top of
976** the table take priority. For example, if zType is 'BLOBINT',
drh8a512562005-11-14 22:29:05 +0000977** SQLITE_AFF_INTEGER is returned.
danielk1977b3dff962005-02-01 01:21:55 +0000978**
979** Substring | Affinity
980** --------------------------------
981** 'INT' | SQLITE_AFF_INTEGER
982** 'CHAR' | SQLITE_AFF_TEXT
983** 'CLOB' | SQLITE_AFF_TEXT
984** 'TEXT' | SQLITE_AFF_TEXT
985** 'BLOB' | SQLITE_AFF_NONE
drh8a512562005-11-14 22:29:05 +0000986** 'REAL' | SQLITE_AFF_REAL
987** 'FLOA' | SQLITE_AFF_REAL
988** 'DOUB' | SQLITE_AFF_REAL
danielk1977b3dff962005-02-01 01:21:55 +0000989**
990** If none of the substrings in the above table are found,
991** SQLITE_AFF_NUMERIC is returned.
danielk197752a83fb2005-01-31 12:56:44 +0000992*/
drh8a512562005-11-14 22:29:05 +0000993char sqlite3AffinityType(const Token *pType){
danielk1977b3dff962005-02-01 01:21:55 +0000994 u32 h = 0;
995 char aff = SQLITE_AFF_NUMERIC;
drh487e2622005-06-25 18:42:14 +0000996 const unsigned char *zIn = pType->z;
997 const unsigned char *zEnd = &pType->z[pType->n];
danielk197752a83fb2005-01-31 12:56:44 +0000998
danielk1977b3dff962005-02-01 01:21:55 +0000999 while( zIn!=zEnd ){
1000 h = (h<<8) + sqlite3UpperToLower[*zIn];
1001 zIn++;
danielk1977201f7162005-02-01 02:13:29 +00001002 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
1003 aff = SQLITE_AFF_TEXT;
1004 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
1005 aff = SQLITE_AFF_TEXT;
1006 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
1007 aff = SQLITE_AFF_TEXT;
1008 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
drh8a512562005-11-14 22:29:05 +00001009 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
danielk1977b3dff962005-02-01 01:21:55 +00001010 aff = SQLITE_AFF_NONE;
drh8a512562005-11-14 22:29:05 +00001011#ifndef SQLITE_OMIT_FLOATING_POINT
1012 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
1013 && aff==SQLITE_AFF_NUMERIC ){
1014 aff = SQLITE_AFF_REAL;
1015 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
1016 && aff==SQLITE_AFF_NUMERIC ){
1017 aff = SQLITE_AFF_REAL;
1018 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
1019 && aff==SQLITE_AFF_NUMERIC ){
1020 aff = SQLITE_AFF_REAL;
1021#endif
danielk1977201f7162005-02-01 02:13:29 +00001022 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
drh8a512562005-11-14 22:29:05 +00001023 aff = SQLITE_AFF_INTEGER;
danielk1977b3dff962005-02-01 01:21:55 +00001024 break;
danielk197752a83fb2005-01-31 12:56:44 +00001025 }
1026 }
danielk1977b3dff962005-02-01 01:21:55 +00001027
1028 return aff;
danielk197752a83fb2005-01-31 12:56:44 +00001029}
1030
1031/*
drh382c0242001-10-06 16:33:02 +00001032** This routine is called by the parser while in the middle of
1033** parsing a CREATE TABLE statement. The pFirst token is the first
1034** token in the sequence of tokens that describe the type of the
1035** column currently under construction. pLast is the last token
1036** in the sequence. Use this information to construct a string
1037** that contains the typename of the column and store that string
1038** in zType.
1039*/
drh487e2622005-06-25 18:42:14 +00001040void sqlite3AddColumnType(Parse *pParse, Token *pType){
drh382c0242001-10-06 16:33:02 +00001041 Table *p;
drh487e2622005-06-25 18:42:14 +00001042 int i;
drhc9b84a12002-06-20 11:36:48 +00001043 Column *pCol;
drh487e2622005-06-25 18:42:14 +00001044
drh382c0242001-10-06 16:33:02 +00001045 if( (p = pParse->pNewTable)==0 ) return;
1046 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +00001047 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +00001048 pCol = &p->aCol[i];
drh17435752007-08-16 04:30:38 +00001049 sqlite3_free(pCol->zType);
1050 pCol->zType = sqlite3NameFromToken(pParse->db, pType);
drh8a512562005-11-14 22:29:05 +00001051 pCol->affinity = sqlite3AffinityType(pType);
drh382c0242001-10-06 16:33:02 +00001052}
1053
1054/*
danielk19777977a172004-11-09 12:44:37 +00001055** The expression is the default value for the most recently added column
1056** of the table currently under construction.
1057**
1058** Default value expressions must be constant. Raise an exception if this
1059** is not the case.
drhd9b02572001-04-15 00:37:09 +00001060**
1061** This routine is called by the parser while in the middle of
1062** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +00001063*/
danielk19777977a172004-11-09 12:44:37 +00001064void sqlite3AddDefaultValue(Parse *pParse, Expr *pExpr){
drh7020f652000-06-03 18:06:52 +00001065 Table *p;
danielk19777977a172004-11-09 12:44:37 +00001066 Column *pCol;
drh42b9d7c2005-08-13 00:56:27 +00001067 if( (p = pParse->pNewTable)!=0 ){
1068 pCol = &(p->aCol[p->nCol-1]);
1069 if( !sqlite3ExprIsConstantOrFunction(pExpr) ){
1070 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
1071 pCol->zName);
1072 }else{
drh417ec632006-08-14 14:23:41 +00001073 Expr *pCopy;
drh17435752007-08-16 04:30:38 +00001074 sqlite3 *db = pParse->db;
drh42b9d7c2005-08-13 00:56:27 +00001075 sqlite3ExprDelete(pCol->pDflt);
drh17435752007-08-16 04:30:38 +00001076 pCol->pDflt = pCopy = sqlite3ExprDup(db, pExpr);
drh417ec632006-08-14 14:23:41 +00001077 if( pCopy ){
drh17435752007-08-16 04:30:38 +00001078 sqlite3TokenCopy(db, &pCopy->span, &pExpr->span);
drh417ec632006-08-14 14:23:41 +00001079 }
drh42b9d7c2005-08-13 00:56:27 +00001080 }
danielk19777977a172004-11-09 12:44:37 +00001081 }
1082 sqlite3ExprDelete(pExpr);
drh7020f652000-06-03 18:06:52 +00001083}
1084
1085/*
drh4a324312001-12-21 14:30:42 +00001086** Designate the PRIMARY KEY for the table. pList is a list of names
1087** of columns that form the primary key. If pList is NULL, then the
1088** most recently added column of the table is the primary key.
1089**
1090** A table can have at most one primary key. If the table already has
1091** a primary key (and this is the second primary key) then create an
1092** error.
1093**
1094** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
drh23bf66d2004-12-14 03:34:34 +00001095** then we will try to use that column as the rowid. Set the Table.iPKey
drh4a324312001-12-21 14:30:42 +00001096** field of the table under construction to be the index of the
1097** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
1098** no INTEGER PRIMARY KEY.
1099**
1100** If the key is not an INTEGER PRIMARY KEY, then create a unique
1101** index for the key. No index is created for INTEGER PRIMARY KEYs.
1102*/
drh205f48e2004-11-05 00:43:11 +00001103void sqlite3AddPrimaryKey(
1104 Parse *pParse, /* Parsing context */
1105 ExprList *pList, /* List of field names to be indexed */
1106 int onError, /* What to do with a uniqueness conflict */
drhfdd6e852005-12-16 01:06:16 +00001107 int autoInc, /* True if the AUTOINCREMENT keyword is present */
1108 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
drh205f48e2004-11-05 00:43:11 +00001109){
drh4a324312001-12-21 14:30:42 +00001110 Table *pTab = pParse->pNewTable;
1111 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +00001112 int iCol = -1, i;
danielk1977c7d54102006-06-15 07:29:00 +00001113 if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +00001114 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +00001115 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001116 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +00001117 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +00001118 }
1119 pTab->hasPrimKey = 1;
1120 if( pList==0 ){
1121 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +00001122 pTab->aCol[iCol].isPrimKey = 1;
1123 }else{
danielk19770202b292004-06-09 09:55:16 +00001124 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +00001125 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +00001126 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
1127 break;
1128 }
drh78100cc2003-08-23 22:40:53 +00001129 }
drh6e4fc2c2005-09-15 21:24:51 +00001130 if( iCol<pTab->nCol ){
drh688c9f02005-09-16 02:48:01 +00001131 pTab->aCol[iCol].isPrimKey = 1;
drh6e4fc2c2005-09-15 21:24:51 +00001132 }
drh4a324312001-12-21 14:30:42 +00001133 }
danielk19770202b292004-06-09 09:55:16 +00001134 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +00001135 }
1136 if( iCol>=0 && iCol<pTab->nCol ){
1137 zType = pTab->aCol[iCol].zType;
1138 }
drhfdd6e852005-12-16 01:06:16 +00001139 if( zType && sqlite3StrICmp(zType, "INTEGER")==0
1140 && sortOrder==SQLITE_SO_ASC ){
drh4a324312001-12-21 14:30:42 +00001141 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +00001142 pTab->keyConf = onError;
drh205f48e2004-11-05 00:43:11 +00001143 pTab->autoInc = autoInc;
1144 }else if( autoInc ){
drh4794f732004-11-05 17:17:50 +00001145#ifndef SQLITE_OMIT_AUTOINCREMENT
drh205f48e2004-11-05 00:43:11 +00001146 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
1147 "INTEGER PRIMARY KEY");
drh4794f732004-11-05 17:17:50 +00001148#endif
drh4a324312001-12-21 14:30:42 +00001149 }else{
drh4d91a702006-01-04 15:54:36 +00001150 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
drhe0194f22003-02-26 13:52:51 +00001151 pList = 0;
drh4a324312001-12-21 14:30:42 +00001152 }
drhe0194f22003-02-26 13:52:51 +00001153
1154primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +00001155 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +00001156 return;
drh4a324312001-12-21 14:30:42 +00001157}
1158
1159/*
drhffe07b22005-11-03 00:41:17 +00001160** Add a new CHECK constraint to the table currently under construction.
1161*/
1162void sqlite3AddCheckConstraint(
1163 Parse *pParse, /* Parsing context */
1164 Expr *pCheckExpr /* The check expression */
1165){
1166#ifndef SQLITE_OMIT_CHECK
1167 Table *pTab = pParse->pNewTable;
drh17435752007-08-16 04:30:38 +00001168 sqlite3 *db = pParse->db;
danielk1977c7d54102006-06-15 07:29:00 +00001169 if( pTab && !IN_DECLARE_VTAB ){
drhffe07b22005-11-03 00:41:17 +00001170 /* The CHECK expression must be duplicated so that tokens refer
1171 ** to malloced space and not the (ephemeral) text of the CREATE TABLE
1172 ** statement */
drh17435752007-08-16 04:30:38 +00001173 pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck,
1174 sqlite3ExprDup(db, pCheckExpr));
drhffe07b22005-11-03 00:41:17 +00001175 }
1176#endif
1177 sqlite3ExprDelete(pCheckExpr);
1178}
1179
1180/*
drhd3d39e92004-05-20 22:16:29 +00001181** Set the collation function of the most recently parsed table column
1182** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +00001183*/
drhd3d39e92004-05-20 22:16:29 +00001184void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +00001185 Table *p;
danielk19770202b292004-06-09 09:55:16 +00001186 int i;
danielk1977a37cdde2004-05-16 11:15:36 +00001187
danielk1977b8cbb872006-06-19 05:33:45 +00001188 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +00001189 i = p->nCol-1;
1190
danielk1977b3bf5562006-01-10 17:58:23 +00001191 if( sqlite3LocateCollSeq(pParse, zType, nType) ){
1192 Index *pIdx;
drh17435752007-08-16 04:30:38 +00001193 p->aCol[i].zColl = sqlite3DbStrNDup(pParse->db, zType, nType);
danielk1977b3bf5562006-01-10 17:58:23 +00001194
1195 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
1196 ** then an index may have been created on this column before the
1197 ** collation type was added. Correct this if it is the case.
1198 */
1199 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
1200 assert( pIdx->nColumn==1 );
1201 if( pIdx->aiColumn[0]==i ){
1202 pIdx->azColl[0] = p->aCol[i].zColl;
danielk19777cedc8d2004-06-10 10:50:08 +00001203 }
1204 }
1205 }
danielk19777cedc8d2004-06-10 10:50:08 +00001206}
1207
danielk1977466be562004-06-10 02:16:01 +00001208/*
1209** This function returns the collation sequence for database native text
1210** encoding identified by the string zName, length nName.
1211**
1212** If the requested collation sequence is not available, or not available
1213** in the database native encoding, the collation factory is invoked to
1214** request it. If the collation factory does not supply such a sequence,
1215** and the sequence is available in another text encoding, then that is
1216** returned instead.
1217**
1218** If no versions of the requested collations sequence are available, or
1219** another error occurs, NULL is returned and an error message written into
1220** pParse.
drha34001c2007-02-02 12:44:37 +00001221**
1222** This routine is a wrapper around sqlite3FindCollSeq(). This routine
1223** invokes the collation factory if the named collation cannot be found
1224** and generates an error message.
danielk1977466be562004-06-10 02:16:01 +00001225*/
danielk19770202b292004-06-09 09:55:16 +00001226CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
danielk19774dade032005-05-25 10:45:10 +00001227 sqlite3 *db = pParse->db;
danielk197714db2662006-01-09 16:12:04 +00001228 u8 enc = ENC(db);
danielk19774dade032005-05-25 10:45:10 +00001229 u8 initbusy = db->init.busy;
danielk1977b3bf5562006-01-10 17:58:23 +00001230 CollSeq *pColl;
danielk19774dade032005-05-25 10:45:10 +00001231
danielk1977b3bf5562006-01-10 17:58:23 +00001232 pColl = sqlite3FindCollSeq(db, enc, zName, nName, initbusy);
danielk19777cedc8d2004-06-10 10:50:08 +00001233 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk19774dade032005-05-25 10:45:10 +00001234 pColl = sqlite3GetCollSeq(db, pColl, zName, nName);
1235 if( !pColl ){
1236 if( nName<0 ){
1237 nName = strlen(zName);
danielk1977466be562004-06-10 02:16:01 +00001238 }
danielk19774dade032005-05-25 10:45:10 +00001239 sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", nName, zName);
1240 pColl = 0;
danielk1977466be562004-06-10 02:16:01 +00001241 }
1242 }
1243
danielk19770202b292004-06-09 09:55:16 +00001244 return pColl;
1245}
1246
1247
drh8e2ca022002-06-17 17:07:19 +00001248/*
drh3f7d4e42004-07-24 14:35:58 +00001249** Generate code that will increment the schema cookie.
drh50e5dad2001-09-15 00:57:28 +00001250**
1251** The schema cookie is used to determine when the schema for the
1252** database changes. After each schema change, the cookie value
1253** changes. When a process first reads the schema it records the
1254** cookie. Thereafter, whenever it goes to access the database,
1255** it checks the cookie to make sure the schema has not changed
1256** since it was last read.
1257**
1258** This plan is not completely bullet-proof. It is possible for
1259** the schema to change multiple times and for the cookie to be
1260** set back to prior value. But schema changes are infrequent
1261** and the probability of hitting the same cookie value is only
1262** 1 chance in 2^32. So we're safe enough.
1263*/
drh9bb575f2004-09-06 17:24:11 +00001264void sqlite3ChangeCookie(sqlite3 *db, Vdbe *v, int iDb){
danielk1977da184232006-01-05 11:34:32 +00001265 sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, 0);
danielk19771d850a72004-05-31 08:26:49 +00001266 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +00001267}
1268
1269/*
drh969fa7c2002-02-18 18:30:32 +00001270** Measure the number of characters needed to output the given
1271** identifier. The number returned includes any quotes used
1272** but does not include the null terminator.
drh234c39d2004-07-24 03:30:47 +00001273**
1274** The estimate is conservative. It might be larger that what is
1275** really needed.
drh969fa7c2002-02-18 18:30:32 +00001276*/
1277static int identLength(const char *z){
1278 int n;
drh17f71932002-02-21 12:01:27 +00001279 for(n=0; *z; n++, z++){
drh234c39d2004-07-24 03:30:47 +00001280 if( *z=='"' ){ n++; }
drh969fa7c2002-02-18 18:30:32 +00001281 }
drh234c39d2004-07-24 03:30:47 +00001282 return n + 2;
drh969fa7c2002-02-18 18:30:32 +00001283}
1284
1285/*
1286** Write an identifier onto the end of the given string. Add
1287** quote characters as needed.
1288*/
drh4c755c02004-08-08 20:22:17 +00001289static void identPut(char *z, int *pIdx, char *zSignedIdent){
1290 unsigned char *zIdent = (unsigned char*)zSignedIdent;
drh17f71932002-02-21 12:01:27 +00001291 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001292 i = *pIdx;
drh17f71932002-02-21 12:01:27 +00001293 for(j=0; zIdent[j]; j++){
1294 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1295 }
1296 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +00001297 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh234c39d2004-07-24 03:30:47 +00001298 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001299 for(j=0; zIdent[j]; j++){
1300 z[i++] = zIdent[j];
drh234c39d2004-07-24 03:30:47 +00001301 if( zIdent[j]=='"' ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001302 }
drh234c39d2004-07-24 03:30:47 +00001303 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001304 z[i] = 0;
1305 *pIdx = i;
1306}
1307
1308/*
1309** Generate a CREATE TABLE statement appropriate for the given
1310** table. Memory to hold the text of the statement is obtained
1311** from sqliteMalloc() and must be freed by the calling function.
1312*/
danielk1977da184232006-01-05 11:34:32 +00001313static char *createTableStmt(Table *p, int isTemp){
drh969fa7c2002-02-18 18:30:32 +00001314 int i, k, n;
1315 char *zStmt;
drh234c39d2004-07-24 03:30:47 +00001316 char *zSep, *zSep2, *zEnd, *z;
1317 Column *pCol;
drh969fa7c2002-02-18 18:30:32 +00001318 n = 0;
drh234c39d2004-07-24 03:30:47 +00001319 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1320 n += identLength(pCol->zName);
1321 z = pCol->zType;
1322 if( z ){
1323 n += (strlen(z) + 1);
danielk1977517eb642004-06-07 10:00:31 +00001324 }
drh969fa7c2002-02-18 18:30:32 +00001325 }
1326 n += identLength(p->zName);
drh234c39d2004-07-24 03:30:47 +00001327 if( n<50 ){
drh969fa7c2002-02-18 18:30:32 +00001328 zSep = "";
1329 zSep2 = ",";
1330 zEnd = ")";
1331 }else{
1332 zSep = "\n ";
1333 zSep2 = ",\n ";
1334 zEnd = "\n)";
1335 }
drhe0bc4042002-06-25 01:09:11 +00001336 n += 35 + 6*p->nCol;
drh17435752007-08-16 04:30:38 +00001337 zStmt = sqlite3_malloc( n );
drh969fa7c2002-02-18 18:30:32 +00001338 if( zStmt==0 ) return 0;
drh5bb3eb92007-05-04 13:15:55 +00001339 sqlite3_snprintf(n, zStmt,
1340 !OMIT_TEMPDB&&isTemp ? "CREATE TEMP TABLE ":"CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001341 k = strlen(zStmt);
1342 identPut(zStmt, &k, p->zName);
1343 zStmt[k++] = '(';
drh234c39d2004-07-24 03:30:47 +00001344 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
drh5bb3eb92007-05-04 13:15:55 +00001345 sqlite3_snprintf(n-k, &zStmt[k], zSep);
drh969fa7c2002-02-18 18:30:32 +00001346 k += strlen(&zStmt[k]);
1347 zSep = zSep2;
drh234c39d2004-07-24 03:30:47 +00001348 identPut(zStmt, &k, pCol->zName);
1349 if( (z = pCol->zType)!=0 ){
danielk1977517eb642004-06-07 10:00:31 +00001350 zStmt[k++] = ' ';
drh5bb3eb92007-05-04 13:15:55 +00001351 assert( strlen(z)+k+1<=n );
1352 sqlite3_snprintf(n-k, &zStmt[k], "%s", z);
drh234c39d2004-07-24 03:30:47 +00001353 k += strlen(z);
danielk1977517eb642004-06-07 10:00:31 +00001354 }
drh969fa7c2002-02-18 18:30:32 +00001355 }
drh5bb3eb92007-05-04 13:15:55 +00001356 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
drh969fa7c2002-02-18 18:30:32 +00001357 return zStmt;
1358}
1359
1360/*
drh75897232000-05-29 14:26:00 +00001361** This routine is called to report the final ")" that terminates
1362** a CREATE TABLE statement.
1363**
drhf57b3392001-10-08 13:22:32 +00001364** The table structure that other action routines have been building
1365** is added to the internal hash tables, assuming no errors have
1366** occurred.
drh75897232000-05-29 14:26:00 +00001367**
drh1d85d932004-02-14 23:05:52 +00001368** An entry for the table is made in the master table on disk, unless
1369** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001370** it means we are reading the sqlite_master table because we just
1371** connected to the database or because the sqlite_master table has
drhddba9e52005-03-19 01:41:21 +00001372** recently changed, so the entry for this table already exists in
drhf57b3392001-10-08 13:22:32 +00001373** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001374**
1375** If the pSelect argument is not NULL, it means that this routine
1376** was called to create a table generated from a
1377** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1378** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001379*/
danielk197719a8e7e2005-03-17 05:03:38 +00001380void sqlite3EndTable(
1381 Parse *pParse, /* Parse context */
1382 Token *pCons, /* The ',' token after the last column defn. */
1383 Token *pEnd, /* The final ')' token in the CREATE TABLE */
1384 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
1385){
drh75897232000-05-29 14:26:00 +00001386 Table *p;
drh9bb575f2004-09-06 17:24:11 +00001387 sqlite3 *db = pParse->db;
danielk1977da184232006-01-05 11:34:32 +00001388 int iDb;
drh75897232000-05-29 14:26:00 +00001389
drh17435752007-08-16 04:30:38 +00001390 if( (pEnd==0 && pSelect==0) || pParse->nErr || db->mallocFailed ) {
danielk1977261919c2005-12-06 12:52:59 +00001391 return;
1392 }
drh28037572000-08-02 13:47:41 +00001393 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001394 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001395
danielk1977517eb642004-06-07 10:00:31 +00001396 assert( !db->init.busy || !pSelect );
1397
drhb9bb7c12006-06-11 23:41:55 +00001398 iDb = sqlite3SchemaToIndex(db, p->pSchema);
danielk1977da184232006-01-05 11:34:32 +00001399
drhffe07b22005-11-03 00:41:17 +00001400#ifndef SQLITE_OMIT_CHECK
1401 /* Resolve names in all CHECK constraint expressions.
1402 */
1403 if( p->pCheck ){
1404 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
1405 NameContext sNC; /* Name context for pParse->pNewTable */
1406
1407 memset(&sNC, 0, sizeof(sNC));
1408 memset(&sSrc, 0, sizeof(sSrc));
1409 sSrc.nSrc = 1;
1410 sSrc.a[0].zName = p->zName;
1411 sSrc.a[0].pTab = p;
1412 sSrc.a[0].iCursor = -1;
1413 sNC.pParse = pParse;
1414 sNC.pSrcList = &sSrc;
drh06f65412005-11-03 02:03:13 +00001415 sNC.isCheck = 1;
drhffe07b22005-11-03 00:41:17 +00001416 if( sqlite3ExprResolveNames(&sNC, p->pCheck) ){
1417 return;
1418 }
1419 }
1420#endif /* !defined(SQLITE_OMIT_CHECK) */
1421
drh1d85d932004-02-14 23:05:52 +00001422 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001423 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1424 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001425 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001426 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001427 */
drh1d85d932004-02-14 23:05:52 +00001428 if( db->init.busy ){
1429 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001430 }
1431
drhe3c41372001-09-17 20:25:58 +00001432 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001433 ** in the SQLITE_MASTER table of the database. The record number
1434 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001435 **
drhe0bc4042002-06-25 01:09:11 +00001436 ** If this is a TEMPORARY table, write the entry into the auxiliary
1437 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001438 */
drh1d85d932004-02-14 23:05:52 +00001439 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001440 int n;
drhd8bc7082000-06-07 23:51:50 +00001441 Vdbe *v;
drh4794f732004-11-05 17:17:50 +00001442 char *zType; /* "view" or "table" */
1443 char *zType2; /* "VIEW" or "TABLE" */
1444 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
drh75897232000-05-29 14:26:00 +00001445
danielk19774adee202004-05-08 08:23:19 +00001446 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001447 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001448
danielk1977e6efa742004-11-10 11:55:10 +00001449 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1450
drh4794f732004-11-05 17:17:50 +00001451 /* Create the rootpage for the new table and push it onto the stack.
1452 ** A view has no rootpage, so just push a zero onto the stack for
1453 ** views. Initialize zType at the same time.
1454 */
drh4ff6dfa2002-03-03 23:06:00 +00001455 if( p->pSelect==0 ){
1456 /* A regular table */
drh4794f732004-11-05 17:17:50 +00001457 zType = "table";
1458 zType2 = "TABLE";
danielk1977576ec6b2005-01-21 11:55:25 +00001459#ifndef SQLITE_OMIT_VIEW
drh4ff6dfa2002-03-03 23:06:00 +00001460 }else{
1461 /* A view */
drh4794f732004-11-05 17:17:50 +00001462 zType = "view";
1463 zType2 = "VIEW";
danielk1977576ec6b2005-01-21 11:55:25 +00001464#endif
drh4ff6dfa2002-03-03 23:06:00 +00001465 }
danielk1977517eb642004-06-07 10:00:31 +00001466
danielk1977517eb642004-06-07 10:00:31 +00001467 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1468 ** statement to populate the new table. The root-page number for the
1469 ** new table is on the top of the vdbe stack.
1470 **
1471 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1472 ** suitable state to query for the column names and types to be used
1473 ** by the new table.
danielk1977c00da102006-01-07 13:21:04 +00001474 **
1475 ** A shared-cache write-lock is not required to write to the new table,
1476 ** as a schema-lock must have already been obtained to create it. Since
1477 ** a schema-lock excludes all other database users, the write-lock would
1478 ** be redundant.
danielk1977517eb642004-06-07 10:00:31 +00001479 */
1480 if( pSelect ){
1481 Table *pSelTab;
1482 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk1977da184232006-01-05 11:34:32 +00001483 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk1977517eb642004-06-07 10:00:31 +00001484 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1485 pParse->nTab = 2;
danielk1977b3bce662005-01-29 08:32:43 +00001486 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
danielk1977517eb642004-06-07 10:00:31 +00001487 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1488 if( pParse->nErr==0 ){
1489 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1490 if( pSelTab==0 ) return;
1491 assert( p->aCol==0 );
1492 p->nCol = pSelTab->nCol;
1493 p->aCol = pSelTab->aCol;
1494 pSelTab->nCol = 0;
1495 pSelTab->aCol = 0;
danielk1977a04a34f2007-04-16 15:06:25 +00001496 sqlite3DeleteTable(pSelTab);
danielk1977517eb642004-06-07 10:00:31 +00001497 }
1498 }
drh4794f732004-11-05 17:17:50 +00001499
drh4794f732004-11-05 17:17:50 +00001500 /* Compute the complete text of the CREATE statement */
1501 if( pSelect ){
danielk19771e536952007-08-16 10:09:01 +00001502 zStmt = createTableStmt(p, p->pSchema==db->aDb[1].pSchema);
drh4794f732004-11-05 17:17:50 +00001503 }else{
drh97903fe2005-05-24 20:19:57 +00001504 n = pEnd->z - pParse->sNameToken.z + 1;
danielk19771e536952007-08-16 10:09:01 +00001505 zStmt = sqlite3MPrintf(db,
1506 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
1507 );
drh4794f732004-11-05 17:17:50 +00001508 }
1509
1510 /* A slot for the record has already been allocated in the
1511 ** SQLITE_MASTER table. We just need to update that slot with all
1512 ** the information we've collected. The rowid for the preallocated
1513 ** slot is the 2nd item on the stack. The top of the stack is the
1514 ** root page for the new table (or a 0 if this is a view).
1515 */
1516 sqlite3NestedParse(pParse,
1517 "UPDATE %Q.%s "
1518 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#0, sql=%Q "
1519 "WHERE rowid=#1",
danielk1977da184232006-01-05 11:34:32 +00001520 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
drh4794f732004-11-05 17:17:50 +00001521 zType,
1522 p->zName,
1523 p->zName,
1524 zStmt
1525 );
drh17435752007-08-16 04:30:38 +00001526 sqlite3_free(zStmt);
danielk1977da184232006-01-05 11:34:32 +00001527 sqlite3ChangeCookie(db, v, iDb);
drh2958a4e2004-11-12 03:56:15 +00001528
1529#ifndef SQLITE_OMIT_AUTOINCREMENT
1530 /* Check to see if we need to create an sqlite_sequence table for
1531 ** keeping track of autoincrement keys.
1532 */
1533 if( p->autoInc ){
danielk1977da184232006-01-05 11:34:32 +00001534 Db *pDb = &db->aDb[iDb];
1535 if( pDb->pSchema->pSeqTab==0 ){
drh2958a4e2004-11-12 03:56:15 +00001536 sqlite3NestedParse(pParse,
drhf3388142004-11-13 03:48:06 +00001537 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
1538 pDb->zName
drh2958a4e2004-11-12 03:56:15 +00001539 );
1540 }
1541 }
1542#endif
drh4794f732004-11-05 17:17:50 +00001543
1544 /* Reparse everything to update our internal data structures */
danielk1977da184232006-01-05 11:34:32 +00001545 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
danielk19771e536952007-08-16 10:09:01 +00001546 sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P3_DYNAMIC);
drh75897232000-05-29 14:26:00 +00001547 }
drh17e9e292003-02-01 13:53:28 +00001548
drh2958a4e2004-11-12 03:56:15 +00001549
drh17e9e292003-02-01 13:53:28 +00001550 /* Add the table to the in-memory representation of the database.
1551 */
drh234c39d2004-07-24 03:30:47 +00001552 if( db->init.busy && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001553 Table *pOld;
drhbe5c89a2004-07-26 00:31:09 +00001554 FKey *pFKey;
danielk1977e501b892006-01-09 06:29:47 +00001555 Schema *pSchema = p->pSchema;
danielk1977da184232006-01-05 11:34:32 +00001556 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, strlen(p->zName)+1,p);
drh17e9e292003-02-01 13:53:28 +00001557 if( pOld ){
1558 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh17435752007-08-16 04:30:38 +00001559 db->mallocFailed = 1;
drh17e9e292003-02-01 13:53:28 +00001560 return;
1561 }
danielk1977576ec6b2005-01-21 11:55:25 +00001562#ifndef SQLITE_OMIT_FOREIGN_KEY
drh17e9e292003-02-01 13:53:28 +00001563 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1564 int nTo = strlen(pFKey->zTo) + 1;
danielk1977da184232006-01-05 11:34:32 +00001565 pFKey->pNextTo = sqlite3HashFind(&pSchema->aFKey, pFKey->zTo, nTo);
1566 sqlite3HashInsert(&pSchema->aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001567 }
danielk1977576ec6b2005-01-21 11:55:25 +00001568#endif
drh17e9e292003-02-01 13:53:28 +00001569 pParse->pNewTable = 0;
1570 db->nTable++;
1571 db->flags |= SQLITE_InternChanges;
danielk197719a8e7e2005-03-17 05:03:38 +00001572
1573#ifndef SQLITE_OMIT_ALTERTABLE
1574 if( !p->pSelect ){
danielk1977bab45c62006-01-16 15:14:27 +00001575 const char *zName = (const char *)pParse->sNameToken.z;
drh9a087a92007-05-15 14:34:32 +00001576 int nName;
danielk197719a8e7e2005-03-17 05:03:38 +00001577 assert( !pSelect && pCons && pEnd );
danielk1977bab45c62006-01-16 15:14:27 +00001578 if( pCons->z==0 ){
1579 pCons = pEnd;
1580 }
1581 nName = (const char *)pCons->z - zName;
drh9a087a92007-05-15 14:34:32 +00001582 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
danielk197719a8e7e2005-03-17 05:03:38 +00001583 }
1584#endif
drh17e9e292003-02-01 13:53:28 +00001585 }
drh75897232000-05-29 14:26:00 +00001586}
1587
drhb7f91642004-10-31 02:22:47 +00001588#ifndef SQLITE_OMIT_VIEW
drh75897232000-05-29 14:26:00 +00001589/*
drha76b5df2002-02-23 02:32:10 +00001590** The parser calls this routine in order to create a new VIEW
1591*/
danielk19774adee202004-05-08 08:23:19 +00001592void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001593 Parse *pParse, /* The parsing context */
1594 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001595 Token *pName1, /* The token that holds the name of the view */
1596 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001597 Select *pSelect, /* A SELECT statement that will become the new view */
drhfdd48a72006-09-11 23:45:48 +00001598 int isTemp, /* TRUE for a TEMPORARY view */
1599 int noErr /* Suppress error messages if VIEW already exists */
drha76b5df2002-02-23 02:32:10 +00001600){
drha76b5df2002-02-23 02:32:10 +00001601 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001602 int n;
drh4c755c02004-08-08 20:22:17 +00001603 const unsigned char *z;
drh4b59ab52002-08-24 18:24:51 +00001604 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001605 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001606 Token *pName;
danielk1977da184232006-01-05 11:34:32 +00001607 int iDb;
drh17435752007-08-16 04:30:38 +00001608 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +00001609
drh7c3d64f2005-06-06 15:32:08 +00001610 if( pParse->nVar>0 ){
1611 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
1612 sqlite3SelectDelete(pSelect);
1613 return;
1614 }
drhfdd48a72006-09-11 23:45:48 +00001615 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
drha76b5df2002-02-23 02:32:10 +00001616 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001617 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001618 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001619 return;
1620 }
danielk1977ef2cb632004-05-29 02:37:19 +00001621 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
drh17435752007-08-16 04:30:38 +00001622 iDb = sqlite3SchemaToIndex(db, p->pSchema);
danielk1977da184232006-01-05 11:34:32 +00001623 if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
danielk19774adee202004-05-08 08:23:19 +00001624 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001625 ){
danielk19774adee202004-05-08 08:23:19 +00001626 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001627 return;
1628 }
drh174b6192002-12-03 02:22:52 +00001629
drh4b59ab52002-08-24 18:24:51 +00001630 /* Make a copy of the entire SELECT statement that defines the view.
1631 ** This will force all the Expr.token.z values to be dynamically
1632 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001633 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001634 */
drh17435752007-08-16 04:30:38 +00001635 p->pSelect = sqlite3SelectDup(db, pSelect);
danielk19774adee202004-05-08 08:23:19 +00001636 sqlite3SelectDelete(pSelect);
drh17435752007-08-16 04:30:38 +00001637 if( db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001638 return;
1639 }
drh17435752007-08-16 04:30:38 +00001640 if( !db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001641 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001642 }
drh4b59ab52002-08-24 18:24:51 +00001643
1644 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1645 ** the end.
1646 */
drha76b5df2002-02-23 02:32:10 +00001647 sEnd = pParse->sLastToken;
1648 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1649 sEnd.z += sEnd.n;
1650 }
1651 sEnd.n = 0;
drhb089c0b2004-06-26 14:46:39 +00001652 n = sEnd.z - pBegin->z;
drh4c755c02004-08-08 20:22:17 +00001653 z = (const unsigned char*)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001654 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1655 sEnd.z = &z[n-1];
1656 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001657
danielk19774adee202004-05-08 08:23:19 +00001658 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
danielk197719a8e7e2005-03-17 05:03:38 +00001659 sqlite3EndTable(pParse, 0, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001660 return;
drh417be792002-03-03 18:59:40 +00001661}
drhb7f91642004-10-31 02:22:47 +00001662#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001663
danielk1977fe3fcbe22006-06-12 12:08:45 +00001664#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
drh417be792002-03-03 18:59:40 +00001665/*
1666** The Table structure pTable is really a VIEW. Fill in the names of
1667** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001668** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001669*/
danielk19774adee202004-05-08 08:23:19 +00001670int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh9b3187e2005-01-18 14:45:47 +00001671 Table *pSelTab; /* A fake table from which we get the result set */
1672 Select *pSel; /* Copy of the SELECT that implements the view */
1673 int nErr = 0; /* Number of errors encountered */
1674 int n; /* Temporarily holds the number of cursors assigned */
drh17435752007-08-16 04:30:38 +00001675 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
drh417be792002-03-03 18:59:40 +00001676
1677 assert( pTable );
1678
danielk1977fe3fcbe22006-06-12 12:08:45 +00001679#ifndef SQLITE_OMIT_VIRTUALTABLE
1680 if( sqlite3VtabCallConnect(pParse, pTable) ){
1681 return SQLITE_ERROR;
1682 }
drh4cbdda92006-06-14 19:00:20 +00001683 if( IsVirtual(pTable) ) return 0;
danielk1977fe3fcbe22006-06-12 12:08:45 +00001684#endif
1685
1686#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001687 /* A positive nCol means the columns names for this view are
1688 ** already known.
1689 */
1690 if( pTable->nCol>0 ) return 0;
1691
1692 /* A negative nCol is a special marker meaning that we are currently
1693 ** trying to compute the column names. If we enter this routine with
1694 ** a negative nCol, it means two or more views form a loop, like this:
1695 **
1696 ** CREATE VIEW one AS SELECT * FROM two;
1697 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001698 **
1699 ** Actually, this error is caught previously and so the following test
1700 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001701 */
1702 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001703 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001704 return 1;
1705 }
drh85c23c62005-08-20 03:03:04 +00001706 assert( pTable->nCol>=0 );
drh417be792002-03-03 18:59:40 +00001707
1708 /* If we get this far, it means we need to compute the table names.
drh9b3187e2005-01-18 14:45:47 +00001709 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
1710 ** "*" elements in the results set of the view and will assign cursors
1711 ** to the elements of the FROM clause. But we do not want these changes
1712 ** to be permanent. So the computation is done on a copy of the SELECT
1713 ** statement that defines the view.
drh417be792002-03-03 18:59:40 +00001714 */
drh9b3187e2005-01-18 14:45:47 +00001715 assert( pTable->pSelect );
drh17435752007-08-16 04:30:38 +00001716 pSel = sqlite3SelectDup(db, pTable->pSelect);
danielk1977261919c2005-12-06 12:52:59 +00001717 if( pSel ){
1718 n = pParse->nTab;
1719 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
1720 pTable->nCol = -1;
1721 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
1722 pParse->nTab = n;
1723 if( pSelTab ){
1724 assert( pTable->aCol==0 );
1725 pTable->nCol = pSelTab->nCol;
1726 pTable->aCol = pSelTab->aCol;
1727 pSelTab->nCol = 0;
1728 pSelTab->aCol = 0;
danielk1977a04a34f2007-04-16 15:06:25 +00001729 sqlite3DeleteTable(pSelTab);
danielk1977da184232006-01-05 11:34:32 +00001730 pTable->pSchema->flags |= DB_UnresetViews;
danielk1977261919c2005-12-06 12:52:59 +00001731 }else{
1732 pTable->nCol = 0;
1733 nErr++;
1734 }
1735 sqlite3SelectDelete(pSel);
1736 } else {
drh417be792002-03-03 18:59:40 +00001737 nErr++;
1738 }
drhb7f91642004-10-31 02:22:47 +00001739#endif /* SQLITE_OMIT_VIEW */
danielk19774b2688a2006-06-20 11:01:07 +00001740 return nErr;
danielk1977fe3fcbe22006-06-12 12:08:45 +00001741}
1742#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
drh417be792002-03-03 18:59:40 +00001743
drhb7f91642004-10-31 02:22:47 +00001744#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001745/*
drh8bf8dc92003-05-17 17:35:10 +00001746** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001747*/
drh9bb575f2004-09-06 17:24:11 +00001748static void sqliteViewResetAll(sqlite3 *db, int idx){
drh417be792002-03-03 18:59:40 +00001749 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001750 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
danielk1977da184232006-01-05 11:34:32 +00001751 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001752 Table *pTab = sqliteHashData(i);
1753 if( pTab->pSelect ){
drh956bc922004-07-24 17:38:29 +00001754 sqliteResetColumnNames(pTab);
drh417be792002-03-03 18:59:40 +00001755 }
1756 }
drh8bf8dc92003-05-17 17:35:10 +00001757 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001758}
drhb7f91642004-10-31 02:22:47 +00001759#else
1760# define sqliteViewResetAll(A,B)
1761#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001762
drh75897232000-05-29 14:26:00 +00001763/*
danielk1977a0bf2652004-11-04 14:30:04 +00001764** This function is called by the VDBE to adjust the internal schema
1765** used by SQLite when the btree layer moves a table root page. The
1766** root-page of a table or index in database iDb has changed from iFrom
1767** to iTo.
drh6205d4a2006-03-24 03:36:26 +00001768**
1769** Ticket #1728: The symbol table might still contain information
1770** on tables and/or indices that are the process of being deleted.
1771** If you are unlucky, one of those deleted indices or tables might
1772** have the same rootpage number as the real table or index that is
1773** being moved. So we cannot stop searching after the first match
1774** because the first match might be for one of the deleted indices
1775** or tables and not the table/index that is actually being moved.
1776** We must continue looping until all tables and indices with
1777** rootpage==iFrom have been converted to have a rootpage of iTo
1778** in order to be certain that we got the right one.
danielk1977a0bf2652004-11-04 14:30:04 +00001779*/
1780#ifndef SQLITE_OMIT_AUTOVACUUM
1781void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){
1782 HashElem *pElem;
danielk1977da184232006-01-05 11:34:32 +00001783 Hash *pHash;
1784
1785 pHash = &pDb->pSchema->tblHash;
1786 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
danielk1977a0bf2652004-11-04 14:30:04 +00001787 Table *pTab = sqliteHashData(pElem);
1788 if( pTab->tnum==iFrom ){
1789 pTab->tnum = iTo;
danielk1977a0bf2652004-11-04 14:30:04 +00001790 }
1791 }
danielk1977da184232006-01-05 11:34:32 +00001792 pHash = &pDb->pSchema->idxHash;
1793 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
danielk1977a0bf2652004-11-04 14:30:04 +00001794 Index *pIdx = sqliteHashData(pElem);
1795 if( pIdx->tnum==iFrom ){
1796 pIdx->tnum = iTo;
danielk1977a0bf2652004-11-04 14:30:04 +00001797 }
1798 }
danielk1977a0bf2652004-11-04 14:30:04 +00001799}
1800#endif
1801
1802/*
1803** Write code to erase the table with root-page iTable from database iDb.
1804** Also write code to modify the sqlite_master table and internal schema
1805** if a root-page of another table is moved by the btree-layer whilst
1806** erasing iTable (this can happen with an auto-vacuum database).
1807*/
drh4e0cff62004-11-05 05:10:28 +00001808static void destroyRootPage(Parse *pParse, int iTable, int iDb){
1809 Vdbe *v = sqlite3GetVdbe(pParse);
drh40e016e2004-11-04 14:47:11 +00001810 sqlite3VdbeAddOp(v, OP_Destroy, iTable, iDb);
1811#ifndef SQLITE_OMIT_AUTOVACUUM
drh4e0cff62004-11-05 05:10:28 +00001812 /* OP_Destroy pushes an integer onto the stack. If this integer
1813 ** is non-zero, then it is the root page number of a table moved to
drh81db88e2004-12-07 12:29:17 +00001814 ** location iTable. The following code modifies the sqlite_master table to
drh4e0cff62004-11-05 05:10:28 +00001815 ** reflect this.
1816 **
1817 ** The "#0" in the SQL is a special constant that means whatever value
1818 ** is on the top of the stack. See sqlite3RegisterExpr().
1819 */
danielk197763e3e9f2004-11-05 09:19:27 +00001820 sqlite3NestedParse(pParse,
drh4794f732004-11-05 17:17:50 +00001821 "UPDATE %Q.%s SET rootpage=%d WHERE #0 AND rootpage=#0",
drh4e0cff62004-11-05 05:10:28 +00001822 pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable);
danielk1977a0bf2652004-11-04 14:30:04 +00001823#endif
1824}
1825
1826/*
1827** Write VDBE code to erase table pTab and all associated indices on disk.
1828** Code to update the sqlite_master tables and internal schema definitions
1829** in case a root-page belonging to another table is moved by the btree layer
1830** is also added (this can happen with an auto-vacuum database).
1831*/
drh4e0cff62004-11-05 05:10:28 +00001832static void destroyTable(Parse *pParse, Table *pTab){
danielk1977a0bf2652004-11-04 14:30:04 +00001833#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001834 Index *pIdx;
drh29c636b2006-01-09 23:40:25 +00001835 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1836 destroyRootPage(pParse, pTab->tnum, iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001837 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh29c636b2006-01-09 23:40:25 +00001838 destroyRootPage(pParse, pIdx->tnum, iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001839 }
1840#else
1841 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
1842 ** is not defined), then it is important to call OP_Destroy on the
1843 ** table and index root-pages in order, starting with the numerically
1844 ** largest root-page number. This guarantees that none of the root-pages
1845 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
1846 ** following were coded:
1847 **
1848 ** OP_Destroy 4 0
1849 ** ...
1850 ** OP_Destroy 5 0
1851 **
1852 ** and root page 5 happened to be the largest root-page number in the
1853 ** database, then root page 5 would be moved to page 4 by the
1854 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
1855 ** a free-list page.
1856 */
1857 int iTab = pTab->tnum;
1858 int iDestroyed = 0;
1859
1860 while( 1 ){
1861 Index *pIdx;
1862 int iLargest = 0;
1863
1864 if( iDestroyed==0 || iTab<iDestroyed ){
1865 iLargest = iTab;
1866 }
1867 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1868 int iIdx = pIdx->tnum;
danielk1977da184232006-01-05 11:34:32 +00001869 assert( pIdx->pSchema==pTab->pSchema );
danielk1977a0bf2652004-11-04 14:30:04 +00001870 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
1871 iLargest = iIdx;
1872 }
1873 }
danielk1977da184232006-01-05 11:34:32 +00001874 if( iLargest==0 ){
1875 return;
1876 }else{
1877 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1878 destroyRootPage(pParse, iLargest, iDb);
1879 iDestroyed = iLargest;
1880 }
danielk1977a0bf2652004-11-04 14:30:04 +00001881 }
1882#endif
1883}
1884
1885/*
drh75897232000-05-29 14:26:00 +00001886** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001887** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001888*/
drha0733842005-12-29 01:11:36 +00001889void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
danielk1977a8858102004-05-28 12:11:21 +00001890 Table *pTab;
drh75897232000-05-29 14:26:00 +00001891 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00001892 sqlite3 *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001893 int iDb;
drh75897232000-05-29 14:26:00 +00001894
drh17435752007-08-16 04:30:38 +00001895 if( pParse->nErr || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +00001896 goto exit_drop_table;
1897 }
danielk1977a8858102004-05-28 12:11:21 +00001898 assert( pName->nSrc==1 );
1899 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1900
drha0733842005-12-29 01:11:36 +00001901 if( pTab==0 ){
1902 if( noErr ){
1903 sqlite3ErrorClear(pParse);
1904 }
1905 goto exit_drop_table;
1906 }
danielk1977da184232006-01-05 11:34:32 +00001907 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhe22a3342003-04-22 20:30:37 +00001908 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001909#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001910 {
1911 int code;
danielk1977da184232006-01-05 11:34:32 +00001912 const char *zTab = SCHEMA_TABLE(iDb);
1913 const char *zDb = db->aDb[iDb].zName;
danielk1977f1a381e2006-06-16 08:01:02 +00001914 const char *zArg2 = 0;
danielk19774adee202004-05-08 08:23:19 +00001915 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001916 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001917 }
drhe5f9c642003-01-13 23:27:31 +00001918 if( isView ){
danielk197753c0f742005-03-29 03:10:59 +00001919 if( !OMIT_TEMPDB && iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001920 code = SQLITE_DROP_TEMP_VIEW;
1921 }else{
1922 code = SQLITE_DROP_VIEW;
1923 }
danielk19774b2688a2006-06-20 11:01:07 +00001924#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977f1a381e2006-06-16 08:01:02 +00001925 }else if( IsVirtual(pTab) ){
danielk19779d1b2a22006-06-21 07:34:11 +00001926 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
1927 goto exit_drop_table;
1928 }
danielk1977f1a381e2006-06-16 08:01:02 +00001929 code = SQLITE_DROP_VTABLE;
1930 zArg2 = pTab->pMod->zName;
danielk19774b2688a2006-06-20 11:01:07 +00001931#endif
drhe5f9c642003-01-13 23:27:31 +00001932 }else{
danielk197753c0f742005-03-29 03:10:59 +00001933 if( !OMIT_TEMPDB && iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001934 code = SQLITE_DROP_TEMP_TABLE;
1935 }else{
1936 code = SQLITE_DROP_TABLE;
1937 }
1938 }
danielk1977f1a381e2006-06-16 08:01:02 +00001939 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
danielk1977a8858102004-05-28 12:11:21 +00001940 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001941 }
danielk1977a8858102004-05-28 12:11:21 +00001942 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1943 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001944 }
drhe5f9c642003-01-13 23:27:31 +00001945 }
1946#endif
danielk1977da184232006-01-05 11:34:32 +00001947 if( pTab->readOnly || pTab==db->aDb[iDb].pSchema->pSeqTab ){
danielk1977a8858102004-05-28 12:11:21 +00001948 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
danielk1977a8858102004-05-28 12:11:21 +00001949 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001950 }
danielk1977576ec6b2005-01-21 11:55:25 +00001951
1952#ifndef SQLITE_OMIT_VIEW
1953 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
1954 ** on a table.
1955 */
danielk1977a8858102004-05-28 12:11:21 +00001956 if( isView && pTab->pSelect==0 ){
1957 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1958 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001959 }
danielk1977a8858102004-05-28 12:11:21 +00001960 if( !isView && pTab->pSelect ){
1961 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1962 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001963 }
danielk1977576ec6b2005-01-21 11:55:25 +00001964#endif
drh75897232000-05-29 14:26:00 +00001965
drh1ccde152000-06-17 13:12:39 +00001966 /* Generate code to remove the table from the master table
1967 ** on disk.
1968 */
danielk19774adee202004-05-08 08:23:19 +00001969 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001970 if( v ){
drhe0bc4042002-06-25 01:09:11 +00001971 Trigger *pTrigger;
drh2958a4e2004-11-12 03:56:15 +00001972 Db *pDb = &db->aDb[iDb];
1973 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh8bf8dc92003-05-17 17:35:10 +00001974
danielk197720b1eaf2006-07-26 16:22:14 +00001975#ifndef SQLITE_OMIT_VIRTUALTABLE
1976 if( IsVirtual(pTab) ){
1977 Vdbe *v = sqlite3GetVdbe(pParse);
1978 if( v ){
1979 sqlite3VdbeAddOp(v, OP_VBegin, 0, 0);
1980 }
1981 }
1982#endif
1983
danielk19778e227872004-06-07 07:52:17 +00001984 /* Drop all triggers associated with the table being dropped. Code
1985 ** is generated to remove entries from sqlite_master and/or
1986 ** sqlite_temp_master if required.
1987 */
danielk1977a8858102004-05-28 12:11:21 +00001988 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001989 while( pTrigger ){
danielk1977da184232006-01-05 11:34:32 +00001990 assert( pTrigger->pSchema==pTab->pSchema ||
1991 pTrigger->pSchema==db->aDb[1].pSchema );
drh74161702006-02-24 02:53:49 +00001992 sqlite3DropTriggerPtr(pParse, pTrigger);
drh956bc922004-07-24 17:38:29 +00001993 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +00001994 }
drh8bf8dc92003-05-17 17:35:10 +00001995
danielk19774d36b812004-11-19 07:07:30 +00001996#ifndef SQLITE_OMIT_AUTOINCREMENT
1997 /* Remove any entries of the sqlite_sequence table associated with
1998 ** the table being dropped. This is done before the table is dropped
1999 ** at the btree level, in case the sqlite_sequence table needs to
2000 ** move as a result of the drop (can happen in auto-vacuum mode).
2001 */
2002 if( pTab->autoInc ){
2003 sqlite3NestedParse(pParse,
2004 "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
2005 pDb->zName, pTab->zName
2006 );
2007 }
2008#endif
2009
danielk19778e227872004-06-07 07:52:17 +00002010 /* Drop all SQLITE_MASTER table and index entries that refer to the
2011 ** table. The program name loops through the master table and deletes
2012 ** every row that refers to a table of the same name as the one being
2013 ** dropped. Triggers are handled seperately because a trigger can be
2014 ** created in the temp database that refers to a table in another
2015 ** database.
2016 */
drhf1974842004-11-05 03:56:00 +00002017 sqlite3NestedParse(pParse,
drh4794f732004-11-05 17:17:50 +00002018 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
drh2958a4e2004-11-12 03:56:15 +00002019 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
danielk19774b2688a2006-06-20 11:01:07 +00002020 if( !isView && !IsVirtual(pTab) ){
drh4e0cff62004-11-05 05:10:28 +00002021 destroyTable(pParse, pTab);
drh5e00f6c2001-09-13 13:46:56 +00002022 }
drh2958a4e2004-11-12 03:56:15 +00002023
danielk1977a21c6b62005-01-24 10:25:59 +00002024 /* Remove the table entry from SQLite's internal schema and modify
2025 ** the schema cookie.
drh2958a4e2004-11-12 03:56:15 +00002026 */
drh4cbdda92006-06-14 19:00:20 +00002027 if( IsVirtual(pTab) ){
drhb9bb7c12006-06-11 23:41:55 +00002028 sqlite3VdbeOp3(v, OP_VDestroy, iDb, 0, pTab->zName, 0);
drhb9bb7c12006-06-11 23:41:55 +00002029 }
danielk19779e39ce82006-06-12 16:01:21 +00002030 sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
danielk1977a21c6b62005-01-24 10:25:59 +00002031 sqlite3ChangeCookie(db, v, iDb);
drh75897232000-05-29 14:26:00 +00002032 }
drhd24cc422003-03-27 12:51:24 +00002033 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00002034
2035exit_drop_table:
2036 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002037}
2038
2039/*
drhc2eef3b2002-08-31 18:53:06 +00002040** This routine is called to create a new foreign key on the table
2041** currently under construction. pFromCol determines which columns
2042** in the current table point to the foreign key. If pFromCol==0 then
2043** connect the key to the last column inserted. pTo is the name of
2044** the table referred to. pToCol is a list of tables in the other
2045** pTo table that the foreign key points to. flags contains all
2046** information about the conflict resolution algorithms specified
2047** in the ON DELETE, ON UPDATE and ON INSERT clauses.
2048**
2049** An FKey structure is created and added to the table currently
2050** under construction in the pParse->pNewTable field. The new FKey
2051** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00002052** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00002053**
2054** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00002055** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00002056*/
danielk19774adee202004-05-08 08:23:19 +00002057void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00002058 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00002059 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00002060 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00002061 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00002062 int flags /* Conflict resolution algorithms. */
2063){
drhb7f91642004-10-31 02:22:47 +00002064#ifndef SQLITE_OMIT_FOREIGN_KEY
drh40e016e2004-11-04 14:47:11 +00002065 FKey *pFKey = 0;
drhc2eef3b2002-08-31 18:53:06 +00002066 Table *p = pParse->pNewTable;
2067 int nByte;
2068 int i;
2069 int nCol;
2070 char *z;
drhc2eef3b2002-08-31 18:53:06 +00002071
2072 assert( pTo!=0 );
danielk1977c7d54102006-06-15 07:29:00 +00002073 if( p==0 || pParse->nErr || IN_DECLARE_VTAB ) goto fk_end;
drhc2eef3b2002-08-31 18:53:06 +00002074 if( pFromCol==0 ){
2075 int iCol = p->nCol-1;
2076 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00002077 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00002078 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00002079 " should reference only one column of table %T",
2080 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00002081 goto fk_end;
2082 }
2083 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00002084 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00002085 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00002086 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00002087 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00002088 goto fk_end;
2089 }else{
danielk19770202b292004-06-09 09:55:16 +00002090 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00002091 }
2092 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
2093 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00002094 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00002095 nByte += strlen(pToCol->a[i].zName) + 1;
2096 }
2097 }
drh17435752007-08-16 04:30:38 +00002098 pFKey = sqlite3DbMallocZero(pParse->db, nByte );
2099 if( pFKey==0 ){
2100 goto fk_end;
2101 }
drhc2eef3b2002-08-31 18:53:06 +00002102 pFKey->pFrom = p;
2103 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00002104 z = (char*)&pFKey[1];
2105 pFKey->aCol = (struct sColMap*)z;
2106 z += sizeof(struct sColMap)*nCol;
2107 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00002108 memcpy(z, pTo->z, pTo->n);
2109 z[pTo->n] = 0;
2110 z += pTo->n+1;
2111 pFKey->pNextTo = 0;
2112 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00002113 if( pFromCol==0 ){
2114 pFKey->aCol[0].iFrom = p->nCol-1;
2115 }else{
2116 for(i=0; i<nCol; i++){
2117 int j;
2118 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00002119 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00002120 pFKey->aCol[i].iFrom = j;
2121 break;
2122 }
2123 }
2124 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002125 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00002126 "unknown column \"%s\" in foreign key definition",
2127 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00002128 goto fk_end;
2129 }
2130 }
2131 }
2132 if( pToCol ){
2133 for(i=0; i<nCol; i++){
2134 int n = strlen(pToCol->a[i].zName);
2135 pFKey->aCol[i].zCol = z;
2136 memcpy(z, pToCol->a[i].zName, n);
2137 z[n] = 0;
2138 z += n+1;
2139 }
2140 }
2141 pFKey->isDeferred = 0;
2142 pFKey->deleteConf = flags & 0xff;
2143 pFKey->updateConf = (flags >> 8 ) & 0xff;
2144 pFKey->insertConf = (flags >> 16 ) & 0xff;
2145
2146 /* Link the foreign key to the table as the last step.
2147 */
2148 p->pFKey = pFKey;
2149 pFKey = 0;
2150
2151fk_end:
drh17435752007-08-16 04:30:38 +00002152 sqlite3_free(pFKey);
drhb7f91642004-10-31 02:22:47 +00002153#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
danielk19770202b292004-06-09 09:55:16 +00002154 sqlite3ExprListDelete(pFromCol);
2155 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00002156}
2157
2158/*
2159** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
2160** clause is seen as part of a foreign key definition. The isDeferred
2161** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
2162** The behavior of the most recently created foreign key is adjusted
2163** accordingly.
2164*/
danielk19774adee202004-05-08 08:23:19 +00002165void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhb7f91642004-10-31 02:22:47 +00002166#ifndef SQLITE_OMIT_FOREIGN_KEY
drhc2eef3b2002-08-31 18:53:06 +00002167 Table *pTab;
2168 FKey *pFKey;
2169 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
2170 pFKey->isDeferred = isDeferred;
drhb7f91642004-10-31 02:22:47 +00002171#endif
drhc2eef3b2002-08-31 18:53:06 +00002172}
2173
2174/*
drh063336a2004-11-05 20:58:39 +00002175** Generate code that will erase and refill index *pIdx. This is
2176** used to initialize a newly created index or to recompute the
2177** content of an index in response to a REINDEX command.
2178**
2179** if memRootPage is not negative, it means that the index is newly
2180** created. The memory cell specified by memRootPage contains the
2181** root page number of the index. If memRootPage is negative, then
2182** the index already exists and must be cleared before being refilled and
2183** the root page number of the index is taken from pIndex->tnum.
2184*/
2185static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
2186 Table *pTab = pIndex->pTable; /* The table that is indexed */
2187 int iTab = pParse->nTab; /* Btree cursor used for pTab */
2188 int iIdx = pParse->nTab+1; /* Btree cursor used for pIndex */
2189 int addr1; /* Address of top of loop */
2190 int tnum; /* Root page of index */
2191 Vdbe *v; /* Generate code into this virtual machine */
danielk1977b3bf5562006-01-10 17:58:23 +00002192 KeyInfo *pKey; /* KeyInfo for index */
drh17435752007-08-16 04:30:38 +00002193 sqlite3 *db = pParse->db; /* The database connection */
2194 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
drh063336a2004-11-05 20:58:39 +00002195
danielk19771d54df82004-11-23 15:41:16 +00002196#ifndef SQLITE_OMIT_AUTHORIZATION
2197 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
drh17435752007-08-16 04:30:38 +00002198 db->aDb[iDb].zName ) ){
danielk19771d54df82004-11-23 15:41:16 +00002199 return;
2200 }
2201#endif
2202
danielk1977c00da102006-01-07 13:21:04 +00002203 /* Require a write-lock on the table to perform this operation */
2204 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
2205
drh063336a2004-11-05 20:58:39 +00002206 v = sqlite3GetVdbe(pParse);
2207 if( v==0 ) return;
2208 if( memRootPage>=0 ){
2209 sqlite3VdbeAddOp(v, OP_MemLoad, memRootPage, 0);
2210 tnum = 0;
2211 }else{
2212 tnum = pIndex->tnum;
danielk1977da184232006-01-05 11:34:32 +00002213 sqlite3VdbeAddOp(v, OP_Clear, tnum, iDb);
drh063336a2004-11-05 20:58:39 +00002214 }
danielk1977da184232006-01-05 11:34:32 +00002215 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk1977b3bf5562006-01-10 17:58:23 +00002216 pKey = sqlite3IndexKeyinfo(pParse, pIndex);
2217 sqlite3VdbeOp3(v, OP_OpenWrite, iIdx, tnum, (char *)pKey, P3_KEYINFO_HANDOFF);
danielk1977c00da102006-01-07 13:21:04 +00002218 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
drh063336a2004-11-05 20:58:39 +00002219 addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0);
2220 sqlite3GenerateIndexKey(v, pIndex, iTab);
drh7f057c92005-06-24 03:53:06 +00002221 if( pIndex->onError!=OE_None ){
2222 int curaddr = sqlite3VdbeCurrentAddr(v);
2223 int addr2 = curaddr+4;
2224 sqlite3VdbeChangeP2(v, curaddr-1, addr2);
2225 sqlite3VdbeAddOp(v, OP_Rowid, iTab, 0);
2226 sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
2227 sqlite3VdbeAddOp(v, OP_IsUnique, iIdx, addr2);
2228 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort,
2229 "indexed columns are not unique", P3_STATIC);
drh17435752007-08-16 04:30:38 +00002230 assert( db->mallocFailed || addr2==sqlite3VdbeCurrentAddr(v) );
drh4343fea2004-11-05 23:46:15 +00002231 }
drh7f057c92005-06-24 03:53:06 +00002232 sqlite3VdbeAddOp(v, OP_IdxInsert, iIdx, 0);
drh063336a2004-11-05 20:58:39 +00002233 sqlite3VdbeAddOp(v, OP_Next, iTab, addr1+1);
drhd654be82005-09-20 17:42:23 +00002234 sqlite3VdbeJumpHere(v, addr1);
drh063336a2004-11-05 20:58:39 +00002235 sqlite3VdbeAddOp(v, OP_Close, iTab, 0);
2236 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
2237}
2238
2239/*
drh23bf66d2004-12-14 03:34:34 +00002240** Create a new index for an SQL table. pName1.pName2 is the name of the index
2241** and pTblList is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00002242** be NULL for a primary key or an index that is created to satisfy a
2243** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00002244** as the table to be indexed. pParse->pNewTable is a table that is
2245** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00002246**
drh382c0242001-10-06 16:33:02 +00002247** pList is a list of columns to be indexed. pList will be NULL if this
2248** is a primary key or unique-constraint on the most recent column added
2249** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00002250*/
danielk19774adee202004-05-08 08:23:19 +00002251void sqlite3CreateIndex(
drh23bf66d2004-12-14 03:34:34 +00002252 Parse *pParse, /* All information about this parse */
2253 Token *pName1, /* First part of index name. May be NULL */
2254 Token *pName2, /* Second part of index name. May be NULL */
2255 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
danielk19770202b292004-06-09 09:55:16 +00002256 ExprList *pList, /* A list of columns to be indexed */
drh23bf66d2004-12-14 03:34:34 +00002257 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh1c55ba02007-07-02 19:31:27 +00002258 Token *pStart, /* The CREATE token that begins this statement */
drhfdd6e852005-12-16 01:06:16 +00002259 Token *pEnd, /* The ")" that closes the CREATE INDEX statement */
drh4d91a702006-01-04 15:54:36 +00002260 int sortOrder, /* Sort order of primary key when pList==NULL */
2261 int ifNotExist /* Omit error if index already exists */
drh75897232000-05-29 14:26:00 +00002262){
drhfdd6e852005-12-16 01:06:16 +00002263 Table *pTab = 0; /* Table to be indexed */
2264 Index *pIndex = 0; /* The index to be created */
2265 char *zName = 0; /* Name of the index */
2266 int nName; /* Number of characters in zName */
drhbeae3192001-09-22 18:12:08 +00002267 int i, j;
drhfdd6e852005-12-16 01:06:16 +00002268 Token nullId; /* Fake token for an empty ID list */
2269 DbFixer sFix; /* For assigning database names to pTable */
2270 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
drh9bb575f2004-09-06 17:24:11 +00002271 sqlite3 *db = pParse->db;
drhfdd6e852005-12-16 01:06:16 +00002272 Db *pDb; /* The specific table containing the indexed database */
2273 int iDb; /* Index of the database that is being written */
2274 Token *pName = 0; /* Unqualified name of the index to create */
2275 struct ExprList_item *pListItem; /* For looping over pList */
danielk1977b3bf5562006-01-10 17:58:23 +00002276 int nCol;
2277 int nExtra = 0;
2278 char *zExtra;
danielk1977cbb18d22004-05-28 11:37:27 +00002279
drh17435752007-08-16 04:30:38 +00002280 if( pParse->nErr || db->mallocFailed || IN_DECLARE_VTAB ){
danielk1977e501b892006-01-09 06:29:47 +00002281 goto exit_create_index;
2282 }
drhdaffd0e2001-04-11 14:28:42 +00002283
drh75897232000-05-29 14:26:00 +00002284 /*
2285 ** Find the table that is to be indexed. Return early if not found.
2286 */
danielk1977cbb18d22004-05-28 11:37:27 +00002287 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00002288
2289 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00002290 ** to search for the table. 'Fix' the table name to this db
2291 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00002292 */
2293 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00002294 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00002295 if( iDb<0 ) goto exit_create_index;
2296
danielk197753c0f742005-03-29 03:10:59 +00002297#ifndef SQLITE_OMIT_TEMPDB
danielk1977ef2cb632004-05-29 02:37:19 +00002298 /* If the index name was unqualified, check if the the table
2299 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00002300 */
danielk1977ef2cb632004-05-29 02:37:19 +00002301 pTab = sqlite3SrcListLookup(pParse, pTblName);
danielk1977da184232006-01-05 11:34:32 +00002302 if( pName2 && pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977ef2cb632004-05-29 02:37:19 +00002303 iDb = 1;
2304 }
danielk197753c0f742005-03-29 03:10:59 +00002305#endif
danielk1977ef2cb632004-05-29 02:37:19 +00002306
2307 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
2308 sqlite3FixSrcList(&sFix, pTblName)
2309 ){
drh85c23c62005-08-20 03:03:04 +00002310 /* Because the parser constructs pTblName from a single identifier,
2311 ** sqlite3FixSrcList can never fail. */
2312 assert(0);
danielk1977cbb18d22004-05-28 11:37:27 +00002313 }
danielk1977ef2cb632004-05-29 02:37:19 +00002314 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
2315 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00002316 if( !pTab ) goto exit_create_index;
danielk1977da184232006-01-05 11:34:32 +00002317 assert( db->aDb[iDb].pSchema==pTab->pSchema );
drh75897232000-05-29 14:26:00 +00002318 }else{
drhe3c41372001-09-17 20:25:58 +00002319 assert( pName==0 );
danielk1977da184232006-01-05 11:34:32 +00002320 pTab = pParse->pNewTable;
drha6370df2006-01-04 21:40:06 +00002321 if( !pTab ) goto exit_create_index;
danielk1977da184232006-01-05 11:34:32 +00002322 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drh75897232000-05-29 14:26:00 +00002323 }
drhfdd6e852005-12-16 01:06:16 +00002324 pDb = &db->aDb[iDb];
danielk1977cbb18d22004-05-28 11:37:27 +00002325
drh75897232000-05-29 14:26:00 +00002326 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00002327 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00002328 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00002329 goto exit_create_index;
2330 }
danielk1977576ec6b2005-01-21 11:55:25 +00002331#ifndef SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +00002332 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00002333 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00002334 goto exit_create_index;
2335 }
danielk1977576ec6b2005-01-21 11:55:25 +00002336#endif
danielk19775ee9d692006-06-21 12:36:25 +00002337#ifndef SQLITE_OMIT_VIRTUALTABLE
2338 if( IsVirtual(pTab) ){
2339 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
2340 goto exit_create_index;
2341 }
2342#endif
drh75897232000-05-29 14:26:00 +00002343
2344 /*
2345 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00002346 ** index or table with the same name.
2347 **
2348 ** Exception: If we are reading the names of permanent indices from the
2349 ** sqlite_master table (because some other process changed the schema) and
2350 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00002351 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00002352 **
2353 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00002354 ** dealing with a primary key or UNIQUE constraint. We have to invent our
2355 ** own name.
drh75897232000-05-29 14:26:00 +00002356 */
danielk1977d8123362004-06-12 09:25:12 +00002357 if( pName ){
drh17435752007-08-16 04:30:38 +00002358 zName = sqlite3NameFromToken(db, pName);
danielk19778a414492004-06-29 08:59:35 +00002359 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00002360 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00002361 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00002362 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00002363 }
danielk1977d8123362004-06-12 09:25:12 +00002364 if( !db->init.busy ){
danielk19778a414492004-06-29 08:59:35 +00002365 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
danielk1977d45a0312007-03-13 16:32:25 +00002366 if( sqlite3FindTable(db, zName, 0)!=0 ){
2367 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
2368 goto exit_create_index;
2369 }
2370 }
danielk197759a33f92007-03-17 10:26:59 +00002371 if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
2372 if( !ifNotExist ){
2373 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
danielk1977d8123362004-06-12 09:25:12 +00002374 }
danielk197759a33f92007-03-17 10:26:59 +00002375 goto exit_create_index;
2376 }
danielk1977a21c6b62005-01-24 10:25:59 +00002377 }else{
drhadbca9c2001-09-27 15:11:53 +00002378 char zBuf[30];
2379 int n;
2380 Index *pLoop;
2381 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
drh5bb3eb92007-05-04 13:15:55 +00002382 sqlite3_snprintf(sizeof(zBuf),zBuf,"_%d",n);
drh75897232000-05-29 14:26:00 +00002383 zName = 0;
danielk1977d8123362004-06-12 09:25:12 +00002384 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00002385 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +00002386 }
2387
drhe5f9c642003-01-13 23:27:31 +00002388 /* Check for authorization to create an index.
2389 */
2390#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00002391 {
drhfdd6e852005-12-16 01:06:16 +00002392 const char *zDb = pDb->zName;
danielk197753c0f742005-03-29 03:10:59 +00002393 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002394 goto exit_create_index;
2395 }
2396 i = SQLITE_CREATE_INDEX;
danielk197753c0f742005-03-29 03:10:59 +00002397 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002398 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002399 goto exit_create_index;
2400 }
drhe5f9c642003-01-13 23:27:31 +00002401 }
2402#endif
2403
drh75897232000-05-29 14:26:00 +00002404 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00002405 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00002406 ** So create a fake list to simulate this.
2407 */
2408 if( pList==0 ){
drh2646da72005-12-09 20:02:05 +00002409 nullId.z = (u8*)pTab->aCol[pTab->nCol-1].zName;
2410 nullId.n = strlen((char*)nullId.z);
danielk19771e536952007-08-16 10:09:01 +00002411 pList = sqlite3ExprListAppend(pParse, 0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00002412 if( pList==0 ) goto exit_create_index;
drhfdd6e852005-12-16 01:06:16 +00002413 pList->a[0].sortOrder = sortOrder;
drh75897232000-05-29 14:26:00 +00002414 }
2415
danielk1977b3bf5562006-01-10 17:58:23 +00002416 /* Figure out how many bytes of space are required to store explicitly
2417 ** specified collation sequence names.
2418 */
2419 for(i=0; i<pList->nExpr; i++){
2420 Expr *pExpr = pList->a[i].pExpr;
2421 if( pExpr ){
2422 nExtra += (1 + strlen(pExpr->pColl->zName));
2423 }
2424 }
2425
drh75897232000-05-29 14:26:00 +00002426 /*
2427 ** Allocate the index structure.
2428 */
drhfdd6e852005-12-16 01:06:16 +00002429 nName = strlen(zName);
danielk1977b3bf5562006-01-10 17:58:23 +00002430 nCol = pList->nExpr;
drh17435752007-08-16 04:30:38 +00002431 pIndex = sqlite3DbMallocZero(db,
danielk1977b3bf5562006-01-10 17:58:23 +00002432 sizeof(Index) + /* Index structure */
2433 sizeof(int)*nCol + /* Index.aiColumn */
2434 sizeof(int)*(nCol+1) + /* Index.aiRowEst */
2435 sizeof(char *)*nCol + /* Index.azColl */
2436 sizeof(u8)*nCol + /* Index.aSortOrder */
2437 nName + 1 + /* Index.zName */
2438 nExtra /* Collation sequence names */
2439 );
drh17435752007-08-16 04:30:38 +00002440 if( db->mallocFailed ){
2441 goto exit_create_index;
2442 }
drh78aecb72006-02-10 18:08:09 +00002443 pIndex->azColl = (char**)(&pIndex[1]);
2444 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
danielk1977bab45c62006-01-16 15:14:27 +00002445 pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
drh78aecb72006-02-10 18:08:09 +00002446 pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
danielk1977b3bf5562006-01-10 17:58:23 +00002447 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
2448 zExtra = (char *)(&pIndex->zName[nName+1]);
drh5bb3eb92007-05-04 13:15:55 +00002449 memcpy(pIndex->zName, zName, nName+1);
drh75897232000-05-29 14:26:00 +00002450 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00002451 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00002452 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00002453 pIndex->autoIndex = pName==0;
danielk1977da184232006-01-05 11:34:32 +00002454 pIndex->pSchema = db->aDb[iDb].pSchema;
drh75897232000-05-29 14:26:00 +00002455
drhfdd6e852005-12-16 01:06:16 +00002456 /* Check to see if we should honor DESC requests on index columns
2457 */
danielk1977da184232006-01-05 11:34:32 +00002458 if( pDb->pSchema->file_format>=4 ){
drhfdd6e852005-12-16 01:06:16 +00002459 sortOrderMask = -1; /* Honor DESC */
drhfdd6e852005-12-16 01:06:16 +00002460 }else{
2461 sortOrderMask = 0; /* Ignore DESC */
2462 }
2463
drh1ccde152000-06-17 13:12:39 +00002464 /* Scan the names of the columns of the table to be indexed and
2465 ** load the column indices into the Index structure. Report an error
2466 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00002467 */
drhfdd6e852005-12-16 01:06:16 +00002468 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
2469 const char *zColName = pListItem->zName;
2470 Column *pTabCol;
drh85eeb692005-12-21 03:16:42 +00002471 int requestedSortOrder;
drha34001c2007-02-02 12:44:37 +00002472 char *zColl; /* Collation sequence name */
danielk1977b3bf5562006-01-10 17:58:23 +00002473
drhfdd6e852005-12-16 01:06:16 +00002474 for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
2475 if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00002476 }
2477 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002478 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhfdd6e852005-12-16 01:06:16 +00002479 pTab->zName, zColName);
drh75897232000-05-29 14:26:00 +00002480 goto exit_create_index;
2481 }
drha34001c2007-02-02 12:44:37 +00002482 /* TODO: Add a test to make sure that the same column is not named
2483 ** more than once within the same index. Only the first instance of
2484 ** the column will ever be used by the optimizer. Note that using the
2485 ** same column more than once cannot be an error because that would
2486 ** break backwards compatibility - it needs to be a warning.
2487 */
drh967e8b72000-06-21 13:59:10 +00002488 pIndex->aiColumn[i] = j;
drhfdd6e852005-12-16 01:06:16 +00002489 if( pListItem->pExpr ){
2490 assert( pListItem->pExpr->pColl );
danielk1977b3bf5562006-01-10 17:58:23 +00002491 zColl = zExtra;
drh5bb3eb92007-05-04 13:15:55 +00002492 sqlite3_snprintf(nExtra, zExtra, "%s", pListItem->pExpr->pColl->zName);
danielk1977b3bf5562006-01-10 17:58:23 +00002493 zExtra += (strlen(zColl) + 1);
danielk19770202b292004-06-09 09:55:16 +00002494 }else{
danielk1977b3bf5562006-01-10 17:58:23 +00002495 zColl = pTab->aCol[j].zColl;
2496 if( !zColl ){
2497 zColl = db->pDfltColl->zName;
2498 }
danielk19770202b292004-06-09 09:55:16 +00002499 }
danielk1977b3bf5562006-01-10 17:58:23 +00002500 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl, -1) ){
danielk19777cedc8d2004-06-10 10:50:08 +00002501 goto exit_create_index;
2502 }
danielk1977b3bf5562006-01-10 17:58:23 +00002503 pIndex->azColl[i] = zColl;
drhd946db02005-12-29 19:23:06 +00002504 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
danielk1977b3bf5562006-01-10 17:58:23 +00002505 pIndex->aSortOrder[i] = requestedSortOrder;
drh75897232000-05-29 14:26:00 +00002506 }
drh51147ba2005-07-23 22:59:55 +00002507 sqlite3DefaultRowEst(pIndex);
drh75897232000-05-29 14:26:00 +00002508
danielk1977d8123362004-06-12 09:25:12 +00002509 if( pTab==pParse->pNewTable ){
2510 /* This routine has been called to create an automatic index as a
2511 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2512 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2513 ** i.e. one of:
2514 **
2515 ** CREATE TABLE t(x PRIMARY KEY, y);
2516 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2517 **
2518 ** Either way, check to see if the table already has such an index. If
2519 ** so, don't bother creating this one. This only applies to
2520 ** automatically created indices. Users can do as they wish with
2521 ** explicit indices.
2522 */
2523 Index *pIdx;
2524 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2525 int k;
2526 assert( pIdx->onError!=OE_None );
2527 assert( pIdx->autoIndex );
2528 assert( pIndex->onError!=OE_None );
2529
2530 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2531 for(k=0; k<pIdx->nColumn; k++){
danielk1977b3bf5562006-01-10 17:58:23 +00002532 const char *z1 = pIdx->azColl[k];
2533 const char *z2 = pIndex->azColl[k];
danielk1977d8123362004-06-12 09:25:12 +00002534 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
danielk1977b3bf5562006-01-10 17:58:23 +00002535 if( pIdx->aSortOrder[k]!=pIndex->aSortOrder[k] ) break;
2536 if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
danielk1977d8123362004-06-12 09:25:12 +00002537 }
2538 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002539 if( pIdx->onError!=pIndex->onError ){
2540 /* This constraint creates the same index as a previous
2541 ** constraint specified somewhere in the CREATE TABLE statement.
2542 ** However the ON CONFLICT clauses are different. If both this
2543 ** constraint and the previous equivalent constraint have explicit
2544 ** ON CONFLICT clauses this is an error. Otherwise, use the
2545 ** explicitly specified behaviour for the index.
2546 */
2547 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2548 sqlite3ErrorMsg(pParse,
2549 "conflicting ON CONFLICT clauses specified", 0);
2550 }
2551 if( pIdx->onError==OE_Default ){
2552 pIdx->onError = pIndex->onError;
2553 }
2554 }
danielk1977d8123362004-06-12 09:25:12 +00002555 goto exit_create_index;
2556 }
2557 }
2558 }
2559
drh75897232000-05-29 14:26:00 +00002560 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002561 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002562 */
drh234c39d2004-07-24 03:30:47 +00002563 if( db->init.busy ){
drh6d4abfb2001-10-22 02:58:08 +00002564 Index *p;
danielk1977da184232006-01-05 11:34:32 +00002565 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
drh3c8bf552003-07-01 18:13:14 +00002566 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002567 if( p ){
2568 assert( p==pIndex ); /* Malloc must have failed */
drh17435752007-08-16 04:30:38 +00002569 db->mallocFailed = 1;
drh6d4abfb2001-10-22 02:58:08 +00002570 goto exit_create_index;
2571 }
drh5e00f6c2001-09-13 13:46:56 +00002572 db->flags |= SQLITE_InternChanges;
drh234c39d2004-07-24 03:30:47 +00002573 if( pTblName!=0 ){
2574 pIndex->tnum = db->init.newTnum;
2575 }
drhd78eeee2001-09-13 16:18:53 +00002576 }
2577
drh1d85d932004-02-14 23:05:52 +00002578 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002579 ** involves writing the index into the master table and filling in the
2580 ** index with the current table contents.
2581 **
drh1d85d932004-02-14 23:05:52 +00002582 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2583 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002584 ** CREATE INDEX statements are read out of the master table. In
2585 ** the latter case the index already exists on disk, which is why
2586 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002587 **
danielk1977cbb18d22004-05-28 11:37:27 +00002588 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002589 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2590 ** has just been created, it contains no data and the index initialization
2591 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002592 */
drh1d85d932004-02-14 23:05:52 +00002593 else if( db->init.busy==0 ){
drhadbca9c2001-09-27 15:11:53 +00002594 Vdbe *v;
drh063336a2004-11-05 20:58:39 +00002595 char *zStmt;
2596 int iMem = pParse->nMem++;
drh75897232000-05-29 14:26:00 +00002597
danielk19774adee202004-05-08 08:23:19 +00002598 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002599 if( v==0 ) goto exit_create_index;
drh063336a2004-11-05 20:58:39 +00002600
drhfdd6e852005-12-16 01:06:16 +00002601
drh063336a2004-11-05 20:58:39 +00002602 /* Create the rootpage for the index
2603 */
drhaee128d2005-02-14 20:48:18 +00002604 sqlite3BeginWriteOperation(pParse, 1, iDb);
drh234c39d2004-07-24 03:30:47 +00002605 sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0);
drh063336a2004-11-05 20:58:39 +00002606 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2607
2608 /* Gather the complete text of the CREATE INDEX statement into
2609 ** the zStmt variable
2610 */
drhe0bc4042002-06-25 01:09:11 +00002611 if( pStart && pEnd ){
drh063336a2004-11-05 20:58:39 +00002612 /* A named index with an explicit CREATE INDEX statement */
danielk19771e536952007-08-16 10:09:01 +00002613 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
drh063336a2004-11-05 20:58:39 +00002614 onError==OE_None ? "" : " UNIQUE",
drh97903fe2005-05-24 20:19:57 +00002615 pEnd->z - pName->z + 1,
drh063336a2004-11-05 20:58:39 +00002616 pName->z);
2617 }else{
2618 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
drhe497f002004-11-07 13:01:49 +00002619 /* zStmt = sqlite3MPrintf(""); */
2620 zStmt = 0;
drh75897232000-05-29 14:26:00 +00002621 }
drh063336a2004-11-05 20:58:39 +00002622
2623 /* Add an entry in sqlite_master for this index
2624 */
2625 sqlite3NestedParse(pParse,
drhe497f002004-11-07 13:01:49 +00002626 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#0,%Q);",
drh063336a2004-11-05 20:58:39 +00002627 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2628 pIndex->zName,
2629 pTab->zName,
2630 zStmt
2631 );
2632 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17435752007-08-16 04:30:38 +00002633 sqlite3_free(zStmt);
drh063336a2004-11-05 20:58:39 +00002634
danielk1977a21c6b62005-01-24 10:25:59 +00002635 /* Fill the index with data and reparse the schema. Code an OP_Expire
2636 ** to invalidate all pre-compiled statements.
drh063336a2004-11-05 20:58:39 +00002637 */
danielk1977cbb18d22004-05-28 11:37:27 +00002638 if( pTblName ){
drh063336a2004-11-05 20:58:39 +00002639 sqlite3RefillIndex(pParse, pIndex, iMem);
drhc275b4e2004-07-19 17:25:24 +00002640 sqlite3ChangeCookie(db, v, iDb);
drh234c39d2004-07-24 03:30:47 +00002641 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
danielk19771e536952007-08-16 10:09:01 +00002642 sqlite3MPrintf(db, "name='%q'", pIndex->zName), P3_DYNAMIC);
danielk1977a21c6b62005-01-24 10:25:59 +00002643 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00002644 }
drh75897232000-05-29 14:26:00 +00002645 }
2646
danielk1977d8123362004-06-12 09:25:12 +00002647 /* When adding an index to the list of indices for a table, make
2648 ** sure all indices labeled OE_Replace come after all those labeled
2649 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2650 ** and INSERT.
2651 */
drh234c39d2004-07-24 03:30:47 +00002652 if( db->init.busy || pTblName==0 ){
2653 if( onError!=OE_Replace || pTab->pIndex==0
2654 || pTab->pIndex->onError==OE_Replace){
2655 pIndex->pNext = pTab->pIndex;
2656 pTab->pIndex = pIndex;
2657 }else{
2658 Index *pOther = pTab->pIndex;
2659 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2660 pOther = pOther->pNext;
2661 }
2662 pIndex->pNext = pOther->pNext;
2663 pOther->pNext = pIndex;
danielk1977d8123362004-06-12 09:25:12 +00002664 }
drh234c39d2004-07-24 03:30:47 +00002665 pIndex = 0;
danielk1977d8123362004-06-12 09:25:12 +00002666 }
danielk1977d8123362004-06-12 09:25:12 +00002667
drh75897232000-05-29 14:26:00 +00002668 /* Clean up before exiting */
2669exit_create_index:
drh956bc922004-07-24 17:38:29 +00002670 if( pIndex ){
2671 freeIndex(pIndex);
2672 }
danielk19770202b292004-06-09 09:55:16 +00002673 sqlite3ExprListDelete(pList);
danielk1977e0048402004-06-15 16:51:01 +00002674 sqlite3SrcListDelete(pTblName);
drh17435752007-08-16 04:30:38 +00002675 sqlite3_free(zName);
drh75897232000-05-29 14:26:00 +00002676 return;
2677}
2678
2679/*
drhd28bcb32005-12-21 14:43:11 +00002680** Generate code to make sure the file format number is at least minFormat.
2681** The generated code will increase the file format number if necessary.
2682*/
2683void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
2684 Vdbe *v;
2685 v = sqlite3GetVdbe(pParse);
2686 if( v ){
2687 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1);
2688 sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
2689 sqlite3VdbeAddOp(v, OP_Ge, 0, sqlite3VdbeCurrentAddr(v)+3);
2690 sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
2691 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
2692 }
2693}
2694
2695/*
drh51147ba2005-07-23 22:59:55 +00002696** Fill the Index.aiRowEst[] array with default information - information
drh91124b32005-08-18 18:15:05 +00002697** to be used when we have not run the ANALYZE command.
drh28c4cf42005-07-27 20:41:43 +00002698**
2699** aiRowEst[0] is suppose to contain the number of elements in the index.
2700** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
2701** number of rows in the table that match any particular value of the
2702** first column of the index. aiRowEst[2] is an estimate of the number
2703** of rows that match any particular combiniation of the first 2 columns
2704** of the index. And so forth. It must always be the case that
2705*
2706** aiRowEst[N]<=aiRowEst[N-1]
2707** aiRowEst[N]>=1
2708**
2709** Apart from that, we have little to go on besides intuition as to
2710** how aiRowEst[] should be initialized. The numbers generated here
2711** are based on typical values found in actual indices.
drh51147ba2005-07-23 22:59:55 +00002712*/
2713void sqlite3DefaultRowEst(Index *pIdx){
drh37108e12005-08-31 13:13:31 +00002714 unsigned *a = pIdx->aiRowEst;
drh51147ba2005-07-23 22:59:55 +00002715 int i;
drh28c4cf42005-07-27 20:41:43 +00002716 assert( a!=0 );
2717 a[0] = 1000000;
drhdb513882006-05-11 23:14:59 +00002718 for(i=pIdx->nColumn; i>=5; i--){
2719 a[i] = 5;
2720 }
2721 while( i>=1 ){
2722 a[i] = 11 - i;
2723 i--;
drh28c4cf42005-07-27 20:41:43 +00002724 }
2725 if( pIdx->onError!=OE_None ){
2726 a[pIdx->nColumn] = 1;
drh51147ba2005-07-23 22:59:55 +00002727 }
2728}
2729
2730/*
drh74e24cd2002-01-09 03:19:59 +00002731** This routine will drop an existing named index. This routine
2732** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002733*/
drh4d91a702006-01-04 15:54:36 +00002734void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
drh75897232000-05-29 14:26:00 +00002735 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002736 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00002737 sqlite3 *db = pParse->db;
danielk1977da184232006-01-05 11:34:32 +00002738 int iDb;
drh75897232000-05-29 14:26:00 +00002739
drh17435752007-08-16 04:30:38 +00002740 if( pParse->nErr || db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00002741 goto exit_drop_index;
2742 }
drhd24cc422003-03-27 12:51:24 +00002743 assert( pName->nSrc==1 );
danielk1977d5d56522005-03-16 12:15:20 +00002744 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
2745 goto exit_drop_index;
2746 }
danielk19774adee202004-05-08 08:23:19 +00002747 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002748 if( pIndex==0 ){
drh4d91a702006-01-04 15:54:36 +00002749 if( !ifExists ){
2750 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
2751 }
drha6ecd332004-06-10 00:29:09 +00002752 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002753 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002754 }
drh485b39b2002-07-13 03:11:52 +00002755 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002756 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002757 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002758 goto exit_drop_index;
2759 }
danielk1977da184232006-01-05 11:34:32 +00002760 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
drhe5f9c642003-01-13 23:27:31 +00002761#ifndef SQLITE_OMIT_AUTHORIZATION
2762 {
2763 int code = SQLITE_DROP_INDEX;
2764 Table *pTab = pIndex->pTable;
danielk1977da184232006-01-05 11:34:32 +00002765 const char *zDb = db->aDb[iDb].zName;
2766 const char *zTab = SCHEMA_TABLE(iDb);
danielk19774adee202004-05-08 08:23:19 +00002767 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002768 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002769 }
danielk1977da184232006-01-05 11:34:32 +00002770 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002771 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002772 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002773 }
drhed6c8672003-01-12 18:02:16 +00002774 }
drhe5f9c642003-01-13 23:27:31 +00002775#endif
drh75897232000-05-29 14:26:00 +00002776
2777 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002778 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002779 if( v ){
drhb17131a2004-11-05 22:18:49 +00002780 sqlite3NestedParse(pParse,
2781 "DELETE FROM %Q.%s WHERE name=%Q",
2782 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2783 pIndex->zName
2784 );
2785 sqlite3ChangeCookie(db, v, iDb);
2786 destroyRootPage(pParse, pIndex->tnum, iDb);
2787 sqlite3VdbeOp3(v, OP_DropIndex, iDb, 0, pIndex->zName, 0);
drh75897232000-05-29 14:26:00 +00002788 }
2789
drhd24cc422003-03-27 12:51:24 +00002790exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002791 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002792}
2793
2794/*
drhcf643722007-03-27 13:36:37 +00002795** pArray is a pointer to an array of objects. Each object in the
2796** array is szEntry bytes in size. This routine allocates a new
2797** object on the end of the array.
drh13449892005-09-07 21:22:45 +00002798**
drhcf643722007-03-27 13:36:37 +00002799** *pnEntry is the number of entries already in use. *pnAlloc is
2800** the previously allocated size of the array. initSize is the
2801** suggested initial array size allocation.
drh13449892005-09-07 21:22:45 +00002802**
drhcf643722007-03-27 13:36:37 +00002803** The index of the new entry is returned in *pIdx.
drh13449892005-09-07 21:22:45 +00002804**
drhcf643722007-03-27 13:36:37 +00002805** This routine returns a pointer to the array of objects. This
2806** might be the same as the pArray parameter or it might be a different
2807** pointer if the array was resized.
drh13449892005-09-07 21:22:45 +00002808*/
drhcf643722007-03-27 13:36:37 +00002809void *sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002810 sqlite3 *db, /* Connection to notify of malloc failures */
drhcf643722007-03-27 13:36:37 +00002811 void *pArray, /* Array of objects. Might be reallocated */
2812 int szEntry, /* Size of each object in the array */
2813 int initSize, /* Suggested initial allocation, in elements */
2814 int *pnEntry, /* Number of objects currently in use */
2815 int *pnAlloc, /* Current size of the allocation, in elements */
2816 int *pIdx /* Write the index of a new slot here */
2817){
2818 char *z;
2819 if( *pnEntry >= *pnAlloc ){
drh13449892005-09-07 21:22:45 +00002820 void *pNew;
drh5360ad32005-09-08 00:13:27 +00002821 int newSize;
drhcf643722007-03-27 13:36:37 +00002822 newSize = (*pnAlloc)*2 + initSize;
drh17435752007-08-16 04:30:38 +00002823 pNew = sqlite3_realloc(pArray, newSize*szEntry);
drh13449892005-09-07 21:22:45 +00002824 if( pNew==0 ){
drh17435752007-08-16 04:30:38 +00002825 db->mallocFailed = 1;
drhcf643722007-03-27 13:36:37 +00002826 *pIdx = -1;
2827 return pArray;
drh13449892005-09-07 21:22:45 +00002828 }
drhcf643722007-03-27 13:36:37 +00002829 *pnAlloc = newSize;
2830 pArray = pNew;
drh13449892005-09-07 21:22:45 +00002831 }
drhcf643722007-03-27 13:36:37 +00002832 z = (char*)pArray;
2833 memset(&z[*pnEntry * szEntry], 0, szEntry);
2834 *pIdx = *pnEntry;
2835 ++*pnEntry;
2836 return pArray;
drh13449892005-09-07 21:22:45 +00002837}
2838
2839/*
drh75897232000-05-29 14:26:00 +00002840** Append a new element to the given IdList. Create a new IdList if
2841** need be.
drhdaffd0e2001-04-11 14:28:42 +00002842**
2843** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002844*/
drh17435752007-08-16 04:30:38 +00002845IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
drh13449892005-09-07 21:22:45 +00002846 int i;
drh75897232000-05-29 14:26:00 +00002847 if( pList==0 ){
drh17435752007-08-16 04:30:38 +00002848 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
drh75897232000-05-29 14:26:00 +00002849 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002850 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002851 }
drhcf643722007-03-27 13:36:37 +00002852 pList->a = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002853 db,
drhcf643722007-03-27 13:36:37 +00002854 pList->a,
2855 sizeof(pList->a[0]),
2856 5,
2857 &pList->nId,
2858 &pList->nAlloc,
2859 &i
2860 );
drh13449892005-09-07 21:22:45 +00002861 if( i<0 ){
2862 sqlite3IdListDelete(pList);
2863 return 0;
drh75897232000-05-29 14:26:00 +00002864 }
drh17435752007-08-16 04:30:38 +00002865 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
drh75897232000-05-29 14:26:00 +00002866 return pList;
2867}
2868
2869/*
drhfe05af82005-07-21 03:14:59 +00002870** Delete an IdList.
2871*/
2872void sqlite3IdListDelete(IdList *pList){
2873 int i;
2874 if( pList==0 ) return;
2875 for(i=0; i<pList->nId; i++){
drh17435752007-08-16 04:30:38 +00002876 sqlite3_free(pList->a[i].zName);
drhfe05af82005-07-21 03:14:59 +00002877 }
drh17435752007-08-16 04:30:38 +00002878 sqlite3_free(pList->a);
2879 sqlite3_free(pList);
drhfe05af82005-07-21 03:14:59 +00002880}
2881
2882/*
2883** Return the index in pList of the identifier named zId. Return -1
2884** if not found.
2885*/
2886int sqlite3IdListIndex(IdList *pList, const char *zName){
2887 int i;
2888 if( pList==0 ) return -1;
2889 for(i=0; i<pList->nId; i++){
2890 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
2891 }
2892 return -1;
2893}
2894
2895/*
drhad3cab52002-05-24 02:04:32 +00002896** Append a new table name to the given SrcList. Create a new SrcList if
2897** need be. A new entry is created in the SrcList even if pToken is NULL.
2898**
2899** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002900**
2901** If pDatabase is not null, it means that the table has an optional
2902** database name prefix. Like this: "database.table". The pDatabase
2903** points to the table name and the pTable points to the database name.
2904** The SrcList.a[].zName field is filled with the table name which might
2905** come from pTable (if pDatabase is NULL) or from pDatabase.
2906** SrcList.a[].zDatabase is filled with the database name from pTable,
2907** or with NULL if no database is specified.
2908**
2909** In other words, if call like this:
2910**
drh17435752007-08-16 04:30:38 +00002911** sqlite3SrcListAppend(D,A,B,0);
drh113088e2003-03-20 01:16:58 +00002912**
2913** Then B is a table name and the database name is unspecified. If called
2914** like this:
2915**
drh17435752007-08-16 04:30:38 +00002916** sqlite3SrcListAppend(D,A,B,C);
drh113088e2003-03-20 01:16:58 +00002917**
2918** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002919*/
drh17435752007-08-16 04:30:38 +00002920SrcList *sqlite3SrcListAppend(
2921 sqlite3 *db, /* Connection to notify of malloc failures */
2922 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
2923 Token *pTable, /* Table to append */
2924 Token *pDatabase /* Database of the table */
2925){
drha99db3b2004-06-19 14:49:12 +00002926 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002927 if( pList==0 ){
drh17435752007-08-16 04:30:38 +00002928 pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002929 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002930 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002931 }
drh4305d102003-07-30 12:34:12 +00002932 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002933 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002934 pList->nAlloc *= 2;
drh17435752007-08-16 04:30:38 +00002935 pNew = sqlite3_realloc(pList,
drh4305d102003-07-30 12:34:12 +00002936 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002937 if( pNew==0 ){
drh17435752007-08-16 04:30:38 +00002938 db->mallocFailed = 1;
danielk19774adee202004-05-08 08:23:19 +00002939 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002940 return 0;
2941 }
drh113088e2003-03-20 01:16:58 +00002942 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002943 }
drha99db3b2004-06-19 14:49:12 +00002944 pItem = &pList->a[pList->nSrc];
2945 memset(pItem, 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002946 if( pDatabase && pDatabase->z==0 ){
2947 pDatabase = 0;
2948 }
2949 if( pDatabase && pTable ){
2950 Token *pTemp = pDatabase;
2951 pDatabase = pTable;
2952 pTable = pTemp;
2953 }
drh17435752007-08-16 04:30:38 +00002954 pItem->zName = sqlite3NameFromToken(db, pTable);
2955 pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
drha99db3b2004-06-19 14:49:12 +00002956 pItem->iCursor = -1;
danielk19771787cca2006-02-10 07:07:14 +00002957 pItem->isPopulated = 0;
drhad3cab52002-05-24 02:04:32 +00002958 pList->nSrc++;
2959 return pList;
2960}
2961
2962/*
drh63eb5f22003-04-29 16:20:44 +00002963** Assign cursors to all tables in a SrcList
2964*/
danielk19774adee202004-05-08 08:23:19 +00002965void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002966 int i;
drh9b3187e2005-01-18 14:45:47 +00002967 struct SrcList_item *pItem;
drh17435752007-08-16 04:30:38 +00002968 assert(pList || pParse->db->mallocFailed );
danielk1977261919c2005-12-06 12:52:59 +00002969 if( pList ){
2970 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
2971 if( pItem->iCursor>=0 ) break;
2972 pItem->iCursor = pParse->nTab++;
2973 if( pItem->pSelect ){
2974 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
2975 }
drh63eb5f22003-04-29 16:20:44 +00002976 }
2977 }
2978}
2979
2980/*
drhad3cab52002-05-24 02:04:32 +00002981** Delete an entire SrcList including all its substructure.
2982*/
danielk19774adee202004-05-08 08:23:19 +00002983void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002984 int i;
drhbe5c89a2004-07-26 00:31:09 +00002985 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002986 if( pList==0 ) return;
drhbe5c89a2004-07-26 00:31:09 +00002987 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
drh17435752007-08-16 04:30:38 +00002988 sqlite3_free(pItem->zDatabase);
2989 sqlite3_free(pItem->zName);
2990 sqlite3_free(pItem->zAlias);
danielk1977a04a34f2007-04-16 15:06:25 +00002991 sqlite3DeleteTable(pItem->pTab);
drhbe5c89a2004-07-26 00:31:09 +00002992 sqlite3SelectDelete(pItem->pSelect);
2993 sqlite3ExprDelete(pItem->pOn);
2994 sqlite3IdListDelete(pItem->pUsing);
drh75897232000-05-29 14:26:00 +00002995 }
drh17435752007-08-16 04:30:38 +00002996 sqlite3_free(pList);
drh75897232000-05-29 14:26:00 +00002997}
2998
drh982cef72000-05-30 16:27:03 +00002999/*
drh61dfc312006-12-16 16:25:15 +00003000** This routine is called by the parser to add a new term to the
3001** end of a growing FROM clause. The "p" parameter is the part of
3002** the FROM clause that has already been constructed. "p" is NULL
3003** if this is the first term of the FROM clause. pTable and pDatabase
3004** are the name of the table and database named in the FROM clause term.
3005** pDatabase is NULL if the database name qualifier is missing - the
3006** usual case. If the term has a alias, then pAlias points to the
3007** alias token. If the term is a subquery, then pSubquery is the
3008** SELECT statement that the subquery encodes. The pTable and
3009** pDatabase parameters are NULL for subqueries. The pOn and pUsing
3010** parameters are the content of the ON and USING clauses.
3011**
3012** Return a new SrcList which encodes is the FROM with the new
3013** term added.
3014*/
3015SrcList *sqlite3SrcListAppendFromTerm(
drh17435752007-08-16 04:30:38 +00003016 Parse *pParse, /* Parsing context */
drh61dfc312006-12-16 16:25:15 +00003017 SrcList *p, /* The left part of the FROM clause already seen */
3018 Token *pTable, /* Name of the table to add to the FROM clause */
3019 Token *pDatabase, /* Name of the database containing pTable */
3020 Token *pAlias, /* The right-hand side of the AS subexpression */
3021 Select *pSubquery, /* A subquery used in place of a table name */
3022 Expr *pOn, /* The ON clause of a join */
3023 IdList *pUsing /* The USING clause of a join */
3024){
3025 struct SrcList_item *pItem;
drh17435752007-08-16 04:30:38 +00003026 sqlite3 *db = pParse->db;
3027 p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
drh61dfc312006-12-16 16:25:15 +00003028 if( p==0 || p->nSrc==0 ){
3029 sqlite3ExprDelete(pOn);
3030 sqlite3IdListDelete(pUsing);
3031 sqlite3SelectDelete(pSubquery);
3032 return p;
3033 }
3034 pItem = &p->a[p->nSrc-1];
3035 if( pAlias && pAlias->n ){
drh17435752007-08-16 04:30:38 +00003036 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
drh61dfc312006-12-16 16:25:15 +00003037 }
3038 pItem->pSelect = pSubquery;
3039 pItem->pOn = pOn;
3040 pItem->pUsing = pUsing;
3041 return p;
3042}
3043
3044/*
3045** When building up a FROM clause in the parser, the join operator
3046** is initially attached to the left operand. But the code generator
3047** expects the join operator to be on the right operand. This routine
3048** Shifts all join operators from left to right for an entire FROM
3049** clause.
3050**
3051** Example: Suppose the join is like this:
3052**
3053** A natural cross join B
3054**
3055** The operator is "natural cross join". The A and B operands are stored
3056** in p->a[0] and p->a[1], respectively. The parser initially stores the
3057** operator with A. This routine shifts that operator over to B.
3058*/
3059void sqlite3SrcListShiftJoinType(SrcList *p){
3060 if( p && p->a ){
3061 int i;
3062 for(i=p->nSrc-1; i>0; i--){
3063 p->a[i].jointype = p->a[i-1].jointype;
3064 }
3065 p->a[0].jointype = 0;
3066 }
3067}
3068
3069/*
drhc4a3c772001-04-04 11:48:57 +00003070** Begin a transaction
3071*/
drh684917c2004-10-05 02:41:42 +00003072void sqlite3BeginTransaction(Parse *pParse, int type){
drh9bb575f2004-09-06 17:24:11 +00003073 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00003074 Vdbe *v;
drh684917c2004-10-05 02:41:42 +00003075 int i;
drh5e00f6c2001-09-13 13:46:56 +00003076
drh001bbcb2003-03-19 03:14:00 +00003077 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drh17435752007-08-16 04:30:38 +00003078 if( pParse->nErr || db->mallocFailed ) return;
danielk19774adee202004-05-08 08:23:19 +00003079 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00003080
3081 v = sqlite3GetVdbe(pParse);
3082 if( !v ) return;
drh684917c2004-10-05 02:41:42 +00003083 if( type!=TK_DEFERRED ){
3084 for(i=0; i<db->nDb; i++){
3085 sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
3086 }
3087 }
danielk19771d850a72004-05-31 08:26:49 +00003088 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00003089}
3090
3091/*
3092** Commit a transaction
3093*/
danielk19774adee202004-05-08 08:23:19 +00003094void sqlite3CommitTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00003095 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00003096 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00003097
drh001bbcb2003-03-19 03:14:00 +00003098 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drh17435752007-08-16 04:30:38 +00003099 if( pParse->nErr || db->mallocFailed ) return;
danielk19774adee202004-05-08 08:23:19 +00003100 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00003101
3102 v = sqlite3GetVdbe(pParse);
3103 if( v ){
3104 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00003105 }
drhc4a3c772001-04-04 11:48:57 +00003106}
3107
3108/*
3109** Rollback a transaction
3110*/
danielk19774adee202004-05-08 08:23:19 +00003111void sqlite3RollbackTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00003112 sqlite3 *db;
drh5e00f6c2001-09-13 13:46:56 +00003113 Vdbe *v;
3114
drh001bbcb2003-03-19 03:14:00 +00003115 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drh17435752007-08-16 04:30:38 +00003116 if( pParse->nErr || db->mallocFailed ) return;
danielk19774adee202004-05-08 08:23:19 +00003117 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00003118
danielk19774adee202004-05-08 08:23:19 +00003119 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00003120 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00003121 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00003122 }
drhc4a3c772001-04-04 11:48:57 +00003123}
drhf57b14a2001-09-14 18:54:08 +00003124
3125/*
drhdc3ff9c2004-08-18 02:10:15 +00003126** Make sure the TEMP database is open and available for use. Return
3127** the number of errors. Leave any error messages in the pParse structure.
3128*/
danielk1977ddfb2f02006-02-17 12:25:14 +00003129int sqlite3OpenTempDatabase(Parse *pParse){
drhdc3ff9c2004-08-18 02:10:15 +00003130 sqlite3 *db = pParse->db;
3131 if( db->aDb[1].pBt==0 && !pParse->explain ){
drhc797d4d2007-05-08 01:08:49 +00003132 int rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE,
3133 &db->aDb[1].pBt);
drhdc3ff9c2004-08-18 02:10:15 +00003134 if( rc!=SQLITE_OK ){
3135 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
3136 "file for storing temporary tables");
3137 pParse->rc = rc;
3138 return 1;
3139 }
3140 if( db->flags & !db->autoCommit ){
3141 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1);
3142 if( rc!=SQLITE_OK ){
3143 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
3144 "the temporary database file");
3145 pParse->rc = rc;
3146 return 1;
3147 }
3148 }
danielk197714db2662006-01-09 16:12:04 +00003149 assert( db->aDb[1].pSchema );
drhdc3ff9c2004-08-18 02:10:15 +00003150 }
3151 return 0;
3152}
3153
3154/*
drh80242052004-06-09 00:48:12 +00003155** Generate VDBE code that will verify the schema cookie and start
3156** a read-transaction for all named database files.
3157**
3158** It is important that all schema cookies be verified and all
3159** read transactions be started before anything else happens in
3160** the VDBE program. But this routine can be called after much other
3161** code has been generated. So here is what we do:
3162**
drhc275b4e2004-07-19 17:25:24 +00003163** The first time this routine is called, we code an OP_Goto that
drh80242052004-06-09 00:48:12 +00003164** will jump to a subroutine at the end of the program. Then we
3165** record every database that needs its schema verified in the
3166** pParse->cookieMask field. Later, after all other code has been
3167** generated, the subroutine that does the cookie verifications and
drhc275b4e2004-07-19 17:25:24 +00003168** starts the transactions will be coded and the OP_Goto P2 value
drh80242052004-06-09 00:48:12 +00003169** will be made to point to that subroutine. The generation of the
3170** cookie verification subroutine code happens in sqlite3FinishCoding().
drhc275b4e2004-07-19 17:25:24 +00003171**
3172** If iDb<0 then code the OP_Goto only - don't set flag to verify the
3173** schema on any databases. This can be used to position the OP_Goto
3174** early in the code, before we know if any database tables will be used.
drh001bbcb2003-03-19 03:14:00 +00003175*/
danielk19774adee202004-05-08 08:23:19 +00003176void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh9bb575f2004-09-06 17:24:11 +00003177 sqlite3 *db;
drh80242052004-06-09 00:48:12 +00003178 Vdbe *v;
3179 int mask;
3180
3181 v = sqlite3GetVdbe(pParse);
3182 if( v==0 ) return; /* This only happens if there was a prior error */
3183 db = pParse->db;
drhc275b4e2004-07-19 17:25:24 +00003184 if( pParse->cookieGoto==0 ){
3185 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1;
drh80242052004-06-09 00:48:12 +00003186 }
drhc275b4e2004-07-19 17:25:24 +00003187 if( iDb>=0 ){
3188 assert( iDb<db->nDb );
3189 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
drhc797d4d2007-05-08 01:08:49 +00003190 assert( iDb<SQLITE_MAX_ATTACHED+2 );
drhc275b4e2004-07-19 17:25:24 +00003191 mask = 1<<iDb;
3192 if( (pParse->cookieMask & mask)==0 ){
3193 pParse->cookieMask |= mask;
danielk1977da184232006-01-05 11:34:32 +00003194 pParse->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
danielk197753c0f742005-03-29 03:10:59 +00003195 if( !OMIT_TEMPDB && iDb==1 ){
drhdc3ff9c2004-08-18 02:10:15 +00003196 sqlite3OpenTempDatabase(pParse);
3197 }
drhc275b4e2004-07-19 17:25:24 +00003198 }
drh001bbcb2003-03-19 03:14:00 +00003199 }
drh001bbcb2003-03-19 03:14:00 +00003200}
3201
3202/*
drh1c928532002-01-31 15:54:21 +00003203** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00003204** might change the database.
3205**
3206** This routine starts a new transaction if we are not already within
3207** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00003208** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00003209** be set for operations that might fail (due to a constraint) part of
3210** the way through and which will need to undo some writes without having to
3211** rollback the whole transaction. For operations where all constraints
3212** can be checked before any changes are made to the database, it is never
3213** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00003214**
drh8bf8dc92003-05-17 17:35:10 +00003215** Only database iDb and the temp database are made writable by this call.
3216** If iDb==0, then the main and temp databases are made writable. If
3217** iDb==1 then only the temp database is made writable. If iDb>1 then the
3218** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00003219*/
drh7f0f12e2004-05-21 13:39:50 +00003220void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00003221 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00003222 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00003223 sqlite3CodeVerifySchema(pParse, iDb);
3224 pParse->writeMask |= 1<<iDb;
danielk197763e3e9f2004-11-05 09:19:27 +00003225 if( setStatement && pParse->nested==0 ){
drh7f0f12e2004-05-21 13:39:50 +00003226 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00003227 }
danielk197753c0f742005-03-29 03:10:59 +00003228 if( (OMIT_TEMPDB || iDb!=1) && pParse->db->aDb[1].pBt!=0 ){
danielk19771d850a72004-05-31 08:26:49 +00003229 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00003230 }
3231}
3232
drh4343fea2004-11-05 23:46:15 +00003233/*
3234** Check to see if pIndex uses the collating sequence pColl. Return
3235** true if it does and false if it does not.
3236*/
3237#ifndef SQLITE_OMIT_REINDEX
danielk1977b3bf5562006-01-10 17:58:23 +00003238static int collationMatch(const char *zColl, Index *pIndex){
3239 int i;
3240 for(i=0; i<pIndex->nColumn; i++){
3241 const char *z = pIndex->azColl[i];
3242 if( z==zColl || (z && zColl && 0==sqlite3StrICmp(z, zColl)) ){
3243 return 1;
3244 }
drh4343fea2004-11-05 23:46:15 +00003245 }
3246 return 0;
3247}
3248#endif
3249
3250/*
3251** Recompute all indices of pTab that use the collating sequence pColl.
3252** If pColl==0 then recompute all indices of pTab.
3253*/
3254#ifndef SQLITE_OMIT_REINDEX
danielk1977b3bf5562006-01-10 17:58:23 +00003255static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
drh4343fea2004-11-05 23:46:15 +00003256 Index *pIndex; /* An index associated with pTab */
3257
3258 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
danielk1977b3bf5562006-01-10 17:58:23 +00003259 if( zColl==0 || collationMatch(zColl, pIndex) ){
danielk1977da184232006-01-05 11:34:32 +00003260 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
3261 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4343fea2004-11-05 23:46:15 +00003262 sqlite3RefillIndex(pParse, pIndex, -1);
3263 }
3264 }
3265}
3266#endif
3267
3268/*
3269** Recompute all indices of all tables in all databases where the
3270** indices use the collating sequence pColl. If pColl==0 then recompute
3271** all indices everywhere.
3272*/
3273#ifndef SQLITE_OMIT_REINDEX
danielk1977b3bf5562006-01-10 17:58:23 +00003274static void reindexDatabases(Parse *pParse, char const *zColl){
drh4343fea2004-11-05 23:46:15 +00003275 Db *pDb; /* A single database */
3276 int iDb; /* The database index number */
3277 sqlite3 *db = pParse->db; /* The database connection */
3278 HashElem *k; /* For looping over tables in pDb */
3279 Table *pTab; /* A table in the database */
3280
3281 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
drh43617e92006-03-06 20:55:46 +00003282 assert( pDb!=0 );
danielk1977da184232006-01-05 11:34:32 +00003283 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
drh4343fea2004-11-05 23:46:15 +00003284 pTab = (Table*)sqliteHashData(k);
danielk1977b3bf5562006-01-10 17:58:23 +00003285 reindexTable(pParse, pTab, zColl);
drh4343fea2004-11-05 23:46:15 +00003286 }
3287 }
3288}
3289#endif
3290
3291/*
drheee46cf2004-11-06 00:02:48 +00003292** Generate code for the REINDEX command.
3293**
3294** REINDEX -- 1
3295** REINDEX <collation> -- 2
3296** REINDEX ?<database>.?<tablename> -- 3
3297** REINDEX ?<database>.?<indexname> -- 4
3298**
3299** Form 1 causes all indices in all attached databases to be rebuilt.
3300** Form 2 rebuilds all indices in all databases that use the named
3301** collating function. Forms 3 and 4 rebuild the named index or all
3302** indices associated with the named table.
drh4343fea2004-11-05 23:46:15 +00003303*/
3304#ifndef SQLITE_OMIT_REINDEX
3305void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
3306 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
3307 char *z; /* Name of a table or index */
3308 const char *zDb; /* Name of the database */
3309 Table *pTab; /* A table in the database */
3310 Index *pIndex; /* An index associated with pTab */
3311 int iDb; /* The database index number */
3312 sqlite3 *db = pParse->db; /* The database connection */
3313 Token *pObjName; /* Name of the table or index to be reindexed */
3314
danielk197733a5edc2005-01-27 00:22:02 +00003315 /* Read the database schema. If an error occurs, leave an error message
3316 ** and code in pParse and return NULL. */
3317 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977e63739a2005-01-27 00:33:37 +00003318 return;
danielk197733a5edc2005-01-27 00:22:02 +00003319 }
3320
drhe497f002004-11-07 13:01:49 +00003321 if( pName1==0 || pName1->z==0 ){
drh4343fea2004-11-05 23:46:15 +00003322 reindexDatabases(pParse, 0);
3323 return;
drhe497f002004-11-07 13:01:49 +00003324 }else if( pName2==0 || pName2->z==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00003325 assert( pName1->z );
danielk197714db2662006-01-09 16:12:04 +00003326 pColl = sqlite3FindCollSeq(db, ENC(db), (char*)pName1->z, pName1->n, 0);
drh4343fea2004-11-05 23:46:15 +00003327 if( pColl ){
drh17435752007-08-16 04:30:38 +00003328 char *zColl = sqlite3DbStrNDup(db, (const char *)pName1->z, pName1->n);
danielk1977f0113002006-01-24 12:09:17 +00003329 if( zColl ){
3330 reindexDatabases(pParse, zColl);
drh17435752007-08-16 04:30:38 +00003331 sqlite3_free(zColl);
danielk1977b3bf5562006-01-10 17:58:23 +00003332 }
drh4343fea2004-11-05 23:46:15 +00003333 return;
3334 }
3335 }
3336 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
3337 if( iDb<0 ) return;
drh17435752007-08-16 04:30:38 +00003338 z = sqlite3NameFromToken(db, pObjName);
drh84f31122007-05-12 15:00:14 +00003339 if( z==0 ) return;
drh4343fea2004-11-05 23:46:15 +00003340 zDb = db->aDb[iDb].zName;
3341 pTab = sqlite3FindTable(db, z, zDb);
3342 if( pTab ){
3343 reindexTable(pParse, pTab, 0);
drh17435752007-08-16 04:30:38 +00003344 sqlite3_free(z);
drh4343fea2004-11-05 23:46:15 +00003345 return;
3346 }
3347 pIndex = sqlite3FindIndex(db, z, zDb);
drh17435752007-08-16 04:30:38 +00003348 sqlite3_free(z);
drh4343fea2004-11-05 23:46:15 +00003349 if( pIndex ){
3350 sqlite3BeginWriteOperation(pParse, 0, iDb);
3351 sqlite3RefillIndex(pParse, pIndex, -1);
3352 return;
3353 }
3354 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
3355}
3356#endif
danielk1977b3bf5562006-01-10 17:58:23 +00003357
3358/*
3359** Return a dynamicly allocated KeyInfo structure that can be used
3360** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
3361**
3362** If successful, a pointer to the new structure is returned. In this case
drh17435752007-08-16 04:30:38 +00003363** the caller is responsible for calling sqlite3_free() on the returned
danielk1977b3bf5562006-01-10 17:58:23 +00003364** pointer. If an error occurs (out of memory or missing collation
3365** sequence), NULL is returned and the state of pParse updated to reflect
3366** the error.
3367*/
3368KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
3369 int i;
3370 int nCol = pIdx->nColumn;
3371 int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
danielk19771e536952007-08-16 10:09:01 +00003372 KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(pParse->db, nBytes);
danielk1977b3bf5562006-01-10 17:58:23 +00003373
3374 if( pKey ){
drhb21c8cd2007-08-21 19:33:56 +00003375 pKey->db = pParse->db;
danielk1977b3bf5562006-01-10 17:58:23 +00003376 pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
3377 assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
3378 for(i=0; i<nCol; i++){
3379 char *zColl = pIdx->azColl[i];
3380 assert( zColl );
3381 pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl, -1);
3382 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
3383 }
3384 pKey->nField = nCol;
3385 }
3386
3387 if( pParse->nErr ){
drh17435752007-08-16 04:30:38 +00003388 sqlite3_free(pKey);
danielk1977b3bf5562006-01-10 17:58:23 +00003389 pKey = 0;
3390 }
3391 return pKey;
3392}