blob: 4900e92316b4cc004b655e7b8ebd63b792dbd7c0 [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
drhbed86902000-06-02 13:27:59 +000021** COPY
22** VACUUM
drhb19a2bc2001-09-16 00:13:26 +000023** BEGIN TRANSACTION
24** COMMIT
25** ROLLBACK
26** PRAGMA
drhbed86902000-06-02 13:27:59 +000027**
drhd24cc422003-03-27 12:51:24 +000028** $Id: build.c,v 1.134 2003/03/27 12:51:24 drh Exp $
drh75897232000-05-29 14:26:00 +000029*/
30#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000031#include <ctype.h>
drh75897232000-05-29 14:26:00 +000032
33/*
drhe0bc4042002-06-25 01:09:11 +000034** This routine is called when a new SQL statement is beginning to
35** be parsed. Check to see if the schema for the database needs
36** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
37** If it does, then read it.
38*/
39void sqliteBeginParse(Parse *pParse, int explainFlag){
40 sqlite *db = pParse->db;
41 pParse->explain = explainFlag;
42 if((db->flags & SQLITE_Initialized)==0 && pParse->initFlag==0 ){
43 int rc = sqliteInit(db, &pParse->zErrMsg);
44 if( rc!=SQLITE_OK ){
45 pParse->rc = rc;
46 pParse->nErr++;
47 }
48 }
49}
50
51/*
drhb86ccfb2003-01-28 23:13:10 +000052** This is a fake callback procedure used when sqlite_exec() is
53** invoked with a NULL callback pointer. If we pass a NULL callback
54** pointer into sqliteVdbeExec() it will return at every OP_Callback,
55** which we do not want it to do. So we substitute a pointer to this
56** procedure in place of the NULL.
57*/
58static int fakeCallback(void *NotUsed, int n, char **az1, char **az2){
59 return 0;
60}
61
62/*
drh75897232000-05-29 14:26:00 +000063** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000064** parsed and we want to execute the VDBE code to implement
65** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000066** constructed VDBE code to do the work of the SQL statement.
67** This routine just has to execute the VDBE code.
68**
69** Note that if an error occurred, it might be the case that
70** no VDBE code was generated.
71*/
72void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000073 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000074 sqlite *db = pParse->db;
drhb86ccfb2003-01-28 23:13:10 +000075 Vdbe *v = pParse->pVdbe;
76 int (*xCallback)(void*,int,char**,char**);
77
drhdaffd0e2001-04-11 14:28:42 +000078 if( sqlite_malloc_failed ) return;
drhb86ccfb2003-01-28 23:13:10 +000079 xCallback = pParse->xCallback;
80 if( xCallback==0 && pParse->useCallback ) xCallback = fakeCallback;
81 if( v && pParse->nErr==0 ){
82 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
83 sqliteVdbeTrace(v, trace);
84 sqliteVdbeMakeReady(v, xCallback, pParse->pArg, pParse->explain);
85 if( pParse->useCallback ){
86 if( pParse->explain ){
87 rc = sqliteVdbeList(v);
drh001bbcb2003-03-19 03:14:00 +000088 db->next_cookie = db->aDb[0].schema_cookie;
drhb86ccfb2003-01-28 23:13:10 +000089 }else{
90 sqliteVdbeExec(v);
91 }
92 rc = sqliteVdbeFinalize(v, &pParse->zErrMsg);
drhecdc7532001-09-23 02:35:53 +000093 if( rc ) pParse->nErr++;
drhb86ccfb2003-01-28 23:13:10 +000094 pParse->pVdbe = 0;
95 pParse->rc = rc;
96 if( rc ) pParse->nErr++;
97 }else{
98 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drh75897232000-05-29 14:26:00 +000099 }
drhd8bc7082000-06-07 23:51:50 +0000100 pParse->colNamesSet = 0;
drh50e5dad2001-09-15 00:57:28 +0000101 pParse->schemaVerified = 0;
drh483750b2003-01-29 18:46:51 +0000102 }else if( pParse->useCallback==0 ){
103 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000104 }
drha226d052002-09-25 19:04:07 +0000105 pParse->nTab = 0;
106 pParse->nMem = 0;
107 pParse->nSet = 0;
108 pParse->nAgg = 0;
drh75897232000-05-29 14:26:00 +0000109}
110
111/*
drhf57b3392001-10-08 13:22:32 +0000112** Locate the in-memory structure that describes
113** a particular database table given the name
drh75897232000-05-29 14:26:00 +0000114** of that table. Return NULL if not found.
115*/
drhd24cc422003-03-27 12:51:24 +0000116Table *sqliteFindTable(sqlite *db, const char *zName, const char *zDatabase){
117 Table *p = 0;
118 int i;
119 for(i=0; i<db->nDb; i++){
120 if( zDatabase!=0 && sqliteStrICmp(zDatabase, db->aDb[i].zName) ) continue;
121 p = sqliteHashFind(&db->aDb[i].tblHash, zName, strlen(zName)+1);
122 if( p ) break;
123 }
drh74e24cd2002-01-09 03:19:59 +0000124 return p;
drh75897232000-05-29 14:26:00 +0000125}
126
127/*
drhf57b3392001-10-08 13:22:32 +0000128** Locate the in-memory structure that describes
129** a particular index given the name of that index.
130** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000131*/
drhd24cc422003-03-27 12:51:24 +0000132Index *sqliteFindIndex(sqlite *db, const char *zName, const char *zDb){
133 Index *p = 0;
134 int i;
135 for(i=0; i<db->nDb; i++){
136 if( zDb && sqliteStrICmp(zDb, db->aDb[i].zName) ) continue;
137 p = sqliteHashFind(&db->aDb[i].idxHash, zName, strlen(zName)+1);
138 if( p ) break;
139 }
drh74e24cd2002-01-09 03:19:59 +0000140 return p;
drh75897232000-05-29 14:26:00 +0000141}
142
143/*
144** Remove the given index from the index hash table, and free
145** its memory structures.
146**
drhd229ca92002-01-09 13:30:41 +0000147** The index is removed from the database hash tables but
148** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000149** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000150*/
drh74e24cd2002-01-09 03:19:59 +0000151static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000152 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000153
drhd229ca92002-01-09 13:30:41 +0000154 assert( db!=0 && p->zName!=0 );
drhd24cc422003-03-27 12:51:24 +0000155 pOld = sqliteHashInsert(&db->aDb[p->iDb].idxHash, p->zName,
156 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000157 if( pOld!=0 && pOld!=p ){
drhd24cc422003-03-27 12:51:24 +0000158 sqliteHashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
159 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000160 }
drh74e24cd2002-01-09 03:19:59 +0000161 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000162}
163
164/*
drhbeae3192001-09-22 18:12:08 +0000165** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000166** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000167** structures.
168*/
drh6d4abfb2001-10-22 02:58:08 +0000169void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000170 if( pIndex->pTable->pIndex==pIndex ){
171 pIndex->pTable->pIndex = pIndex->pNext;
172 }else{
173 Index *p;
174 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
175 if( p && p->pNext==pIndex ){
176 p->pNext = pIndex->pNext;
177 }
178 }
179 sqliteDeleteIndex(db, pIndex);
180}
181
182/*
drhe0bc4042002-06-25 01:09:11 +0000183** Erase all schema information from the in-memory hash tables of
184** database connection. This routine is called to reclaim memory
185** before the connection closes. It is also called during a rollback
186** if there were schema changes during the transaction.
drh74e24cd2002-01-09 03:19:59 +0000187*/
drhe0bc4042002-06-25 01:09:11 +0000188void sqliteResetInternalSchema(sqlite *db){
189 HashElem *pElem;
190 Hash temp1;
191 Hash temp2;
drhd24cc422003-03-27 12:51:24 +0000192 int i;
drhe0bc4042002-06-25 01:09:11 +0000193
drhd24cc422003-03-27 12:51:24 +0000194 for(i=0; i<db->nDb; i++){
195 Db *pDb = &db->aDb[i];
196 temp1 = pDb->tblHash;
197 temp2 = pDb->trigHash;
198 sqliteHashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
199 sqliteHashClear(&pDb->aFKey);
200 sqliteHashClear(&pDb->idxHash);
201 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
202 Trigger *pTrigger = sqliteHashData(pElem);
203 sqliteDeleteTrigger(pTrigger);
204 }
205 sqliteHashClear(&temp2);
206 sqliteHashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
207 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
208 Table *pTab = sqliteHashData(pElem);
209 sqliteDeleteTable(db, pTab);
210 }
211 sqliteHashClear(&temp1);
drh74e24cd2002-01-09 03:19:59 +0000212 }
drhe0bc4042002-06-25 01:09:11 +0000213 db->flags &= ~(SQLITE_Initialized|SQLITE_InternChanges);
214}
215
216/*
217** This routine is called whenever a rollback occurs. If there were
218** schema changes during the transaction, then we have to reset the
219** internal hash tables and reload them from disk.
220*/
221void sqliteRollbackInternalChanges(sqlite *db){
222 if( db->flags & SQLITE_InternChanges ){
223 sqliteResetInternalSchema(db);
224 }
225}
226
227/*
228** This routine is called when a commit occurs.
229*/
230void sqliteCommitInternalChanges(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000231 db->aDb[0].schema_cookie = db->next_cookie;
drhe0bc4042002-06-25 01:09:11 +0000232 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000233}
234
235/*
drh75897232000-05-29 14:26:00 +0000236** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000237** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000238**
239** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000240** the table data structure from the hash table. Nor does it remove
241** foreign keys from the sqlite.aFKey hash table. But it does destroy
242** memory structures of the indices and foreign keys associated with
243** the table.
drhdaffd0e2001-04-11 14:28:42 +0000244**
245** Indices associated with the table are unlinked from the "db"
246** data structure if db!=NULL. If db==NULL, indices attached to
247** the table are deleted, but it is assumed they have already been
248** unlinked.
drh75897232000-05-29 14:26:00 +0000249*/
250void sqliteDeleteTable(sqlite *db, Table *pTable){
251 int i;
252 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000253 FKey *pFKey, *pNextFKey;
254
drh75897232000-05-29 14:26:00 +0000255 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000256
257 /* Delete all indices associated with this table
258 */
259 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
260 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000261 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000262 sqliteDeleteIndex(db, pIndex);
263 }
264
265 /* Delete all foreign keys associated with this table. The keys
266 ** should have already been unlinked from the db->aFKey hash table
267 */
268 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
269 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000270 assert( pTable->iDb<db->nDb );
271 assert( sqliteHashFind(&db->aDb[pTable->iDb].aFKey,
272 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000273 sqliteFree(pFKey);
274 }
275
276 /* Delete the Table structure itself.
277 */
drh75897232000-05-29 14:26:00 +0000278 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000279 sqliteFree(pTable->aCol[i].zName);
280 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000281 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000282 }
drh6e142f52000-06-08 13:36:40 +0000283 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000284 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000285 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000286 sqliteFree(pTable);
287}
288
289/*
drh5edc3122001-09-13 21:53:09 +0000290** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000291** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000292*/
drh74e24cd2002-01-09 03:19:59 +0000293static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000294 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000295 FKey *pF1, *pF2;
drhd24cc422003-03-27 12:51:24 +0000296 int i = p->iDb;
drhd229ca92002-01-09 13:30:41 +0000297 assert( db!=0 );
drhd24cc422003-03-27 12:51:24 +0000298 pOld = sqliteHashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000299 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000300 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
301 int nTo = strlen(pF1->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +0000302 pF2 = sqliteHashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
drhc2eef3b2002-08-31 18:53:06 +0000303 if( pF2==pF1 ){
drhd24cc422003-03-27 12:51:24 +0000304 sqliteHashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
drhc2eef3b2002-08-31 18:53:06 +0000305 }else{
306 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
307 if( pF2 ){
308 pF2->pNextTo = pF1->pNextTo;
309 }
310 }
311 }
drh74e24cd2002-01-09 03:19:59 +0000312 sqliteDeleteTable(db, p);
313}
314
315/*
drh1ccde152000-06-17 13:12:39 +0000316** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000317**
318** Space to hold the name is obtained from sqliteMalloc() and must
319** be freed by the calling function.
320*/
drhcce7d172000-05-31 15:34:51 +0000321char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000322 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000323 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000324 return zName;
325}
326
327/*
drhe0bc4042002-06-25 01:09:11 +0000328** Generate code to open the appropriate master table. The table
329** opened will be SQLITE_MASTER for persistent tables and
330** SQLITE_TEMP_MASTER for temporary tables. The table is opened
331** on cursor 0.
332*/
333void sqliteOpenMasterTable(Vdbe *v, int isTemp){
drh001bbcb2003-03-19 03:14:00 +0000334 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
335 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
drhe0bc4042002-06-25 01:09:11 +0000336}
337
338/*
drh75897232000-05-29 14:26:00 +0000339** Begin constructing a new table representation in memory. This is
340** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000341** to a CREATE TABLE statement. In particular, this routine is called
342** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000343** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000344** flag is true if the table should be stored in the auxiliary database
345** file instead of in the main database file. This is normally the case
346** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000347** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000348**
drhf57b3392001-10-08 13:22:32 +0000349** The new table record is initialized and put in pParse->pNewTable.
350** As more of the CREATE TABLE statement is parsed, additional action
351** routines will be called to add more information to this record.
352** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
353** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000354*/
drhe5f9c642003-01-13 23:27:31 +0000355void sqliteStartTable(
356 Parse *pParse, /* Parser context */
357 Token *pStart, /* The "CREATE" token */
358 Token *pName, /* Name of table or view to create */
359 int isTemp, /* True if this is a TEMP table */
360 int isView /* True if this is a VIEW */
361){
drh75897232000-05-29 14:26:00 +0000362 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000363 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000364 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000365 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000366 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000367
368 pParse->sFirstToken = *pStart;
369 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000370 if( zName==0 ) return;
drhd24cc422003-03-27 12:51:24 +0000371 if( pParse->iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000372#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000373 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000374 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drh77ad4e42003-01-14 02:49:27 +0000375 sqliteFree(zName);
drhed6c8672003-01-12 18:02:16 +0000376 return;
377 }
drhe5f9c642003-01-13 23:27:31 +0000378 {
379 int code;
380 if( isView ){
381 if( isTemp ){
382 code = SQLITE_CREATE_TEMP_VIEW;
383 }else{
384 code = SQLITE_CREATE_VIEW;
385 }
386 }else{
387 if( isTemp ){
388 code = SQLITE_CREATE_TEMP_TABLE;
389 }else{
390 code = SQLITE_CREATE_TABLE;
391 }
392 }
393 if( sqliteAuthCheck(pParse, code, zName, 0) ){
drh77ad4e42003-01-14 02:49:27 +0000394 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000395 return;
396 }
397 }
398#endif
399
drhf57b3392001-10-08 13:22:32 +0000400
401 /* Before trying to create a temporary table, make sure the Btree for
402 ** holding temporary tables is open.
403 */
drhd24cc422003-03-27 12:51:24 +0000404 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
drh001bbcb2003-03-19 03:14:00 +0000405 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000406 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000407 sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
drhf57b3392001-10-08 13:22:32 +0000408 "file for storing temporary tables", 0);
409 pParse->nErr++;
410 return;
411 }
412 if( db->flags & SQLITE_InTrans ){
drh001bbcb2003-03-19 03:14:00 +0000413 rc = sqliteBtreeBeginTrans(db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000414 if( rc!=SQLITE_OK ){
415 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000416 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000417 pParse->nErr++;
418 return;
419 }
420 }
421 }
422
423 /* Make sure the new table name does not collide with an existing
424 ** index or table name. Issue an error message if it does.
425 **
426 ** If we are re-reading the sqlite_master table because of a schema
427 ** change and a new permanent table is found whose name collides with
drhd24cc422003-03-27 12:51:24 +0000428 ** an existing temporary table, that is not an error.
drhf57b3392001-10-08 13:22:32 +0000429 */
drhd24cc422003-03-27 12:51:24 +0000430 pTable = sqliteFindTable(db, zName, 0);
431 if( pTable!=0 && (pTable->iDb==isTemp || !pParse->initFlag) ){
432 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
433 " already exists", 0, 0);
434 sqliteFree(zName);
435 pParse->nErr++;
436 return;
drh75897232000-05-29 14:26:00 +0000437 }
drhd24cc422003-03-27 12:51:24 +0000438 if( (pIdx = sqliteFindIndex(db, zName, 0))!=0 &&
439 (pIdx->iDb==0 || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000440 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
441 zName, 0);
drh75897232000-05-29 14:26:00 +0000442 sqliteFree(zName);
443 pParse->nErr++;
444 return;
445 }
446 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000447 if( pTable==0 ){
448 sqliteFree(zName);
449 return;
450 }
drh75897232000-05-29 14:26:00 +0000451 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000452 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000453 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000454 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000455 pTable->pIndex = 0;
drhd24cc422003-03-27 12:51:24 +0000456 pTable->iDb = isTemp ? 1 : pParse->iDb;
drhbe0072d2001-09-13 14:46:09 +0000457 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000458 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000459
460 /* Begin generating the code that will insert the table record into
461 ** the SQLITE_MASTER table. Note in particular that we must go ahead
462 ** and allocate the record number for the table entry now. Before any
463 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
464 ** indices to be created and the table record must come before the
465 ** indices. Hence, the record number for the table must be allocated
466 ** now.
467 */
drhadbca9c2001-09-27 15:11:53 +0000468 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drhcabb0812002-09-14 13:47:32 +0000469 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000470 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000471 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
472 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000473 }
drhe0bc4042002-06-25 01:09:11 +0000474 sqliteOpenMasterTable(v, isTemp);
475 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
476 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
477 sqliteVdbeAddOp(v, OP_String, 0, 0);
478 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000479 }
drh75897232000-05-29 14:26:00 +0000480}
481
482/*
483** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000484**
485** The parser calls this routine once for each column declaration
486** in a CREATE TABLE statement. sqliteStartTable() gets called
487** first to get things going. Then this routine is called for each
488** column.
drh75897232000-05-29 14:26:00 +0000489*/
490void sqliteAddColumn(Parse *pParse, Token *pName){
491 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000492 int i;
493 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000494 Column *pCol;
drh75897232000-05-29 14:26:00 +0000495 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000496 sqliteSetNString(&z, pName->z, pName->n, 0);
497 if( z==0 ) return;
498 sqliteDequote(z);
499 for(i=0; i<p->nCol; i++){
500 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
501 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
502 pParse->nErr++;
503 sqliteFree(z);
504 return;
505 }
506 }
drh75897232000-05-29 14:26:00 +0000507 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000508 Column *aNew;
509 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
510 if( aNew==0 ) return;
511 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000512 }
drhc9b84a12002-06-20 11:36:48 +0000513 pCol = &p->aCol[p->nCol];
514 memset(pCol, 0, sizeof(p->aCol[0]));
515 pCol->zName = z;
516 pCol->sortOrder = SQLITE_SO_NUM;
517 p->nCol++;
drh75897232000-05-29 14:26:00 +0000518}
519
520/*
drh382c0242001-10-06 16:33:02 +0000521** This routine is called by the parser while in the middle of
522** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
523** been seen on a column. This routine sets the notNull flag on
524** the column currently under construction.
525*/
drh9cfcf5d2002-01-29 18:41:24 +0000526void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000527 Table *p;
528 int i;
529 if( (p = pParse->pNewTable)==0 ) return;
530 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000531 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000532}
533
534/*
535** This routine is called by the parser while in the middle of
536** parsing a CREATE TABLE statement. The pFirst token is the first
537** token in the sequence of tokens that describe the type of the
538** column currently under construction. pLast is the last token
539** in the sequence. Use this information to construct a string
540** that contains the typename of the column and store that string
541** in zType.
542*/
543void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
544 Table *p;
545 int i, j;
546 int n;
547 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000548 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000549 if( (p = pParse->pNewTable)==0 ) return;
550 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000551 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000552 pCol = &p->aCol[i];
553 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000554 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000555 sqliteSetNString(pz, pFirst->z, n, 0);
556 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000557 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000558 for(i=j=0; z[i]; i++){
559 int c = z[i];
560 if( isspace(c) ) continue;
561 z[j++] = c;
562 }
563 z[j] = 0;
drh3d037a92002-08-15 01:26:09 +0000564 if( pParse->db->file_format>=4 ){
drhfcb78a42003-01-18 20:11:05 +0000565 pCol->sortOrder = sqliteCollateType(z, n);
566 }else{
567 pCol->sortOrder = SQLITE_SO_NUM;
drhc9b84a12002-06-20 11:36:48 +0000568 }
drh382c0242001-10-06 16:33:02 +0000569}
570
571/*
drh7020f652000-06-03 18:06:52 +0000572** The given token is the default value for the last column added to
573** the table currently under construction. If "minusFlag" is true, it
574** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000575**
576** This routine is called by the parser while in the middle of
577** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000578*/
579void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
580 Table *p;
581 int i;
582 char **pz;
583 if( (p = pParse->pNewTable)==0 ) return;
584 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000585 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000586 pz = &p->aCol[i].zDflt;
587 if( minusFlag ){
588 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
589 }else{
590 sqliteSetNString(pz, pVal->z, pVal->n, 0);
591 }
592 sqliteDequote(*pz);
593}
594
595/*
drh4a324312001-12-21 14:30:42 +0000596** Designate the PRIMARY KEY for the table. pList is a list of names
597** of columns that form the primary key. If pList is NULL, then the
598** most recently added column of the table is the primary key.
599**
600** A table can have at most one primary key. If the table already has
601** a primary key (and this is the second primary key) then create an
602** error.
603**
604** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
605** then we will try to use that column as the row id. (Exception:
606** For backwards compatibility with older databases, do not do this
607** if the file format version number is less than 1.) Set the Table.iPKey
608** field of the table under construction to be the index of the
609** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
610** no INTEGER PRIMARY KEY.
611**
612** If the key is not an INTEGER PRIMARY KEY, then create a unique
613** index for the key. No index is created for INTEGER PRIMARY KEYs.
614*/
drh9cfcf5d2002-01-29 18:41:24 +0000615void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000616 Table *pTab = pParse->pNewTable;
617 char *zType = 0;
618 int iCol = -1;
drhe0194f22003-02-26 13:52:51 +0000619 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000620 if( pTab->hasPrimKey ){
621 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
622 "\" has more than one primary key", 0);
623 pParse->nErr++;
drhe0194f22003-02-26 13:52:51 +0000624 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000625 }
626 pTab->hasPrimKey = 1;
627 if( pList==0 ){
628 iCol = pTab->nCol - 1;
629 }else if( pList->nId==1 ){
630 for(iCol=0; iCol<pTab->nCol; iCol++){
631 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
632 }
633 }
634 if( iCol>=0 && iCol<pTab->nCol ){
635 zType = pTab->aCol[iCol].zType;
636 }
637 if( pParse->db->file_format>=1 &&
638 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
639 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000640 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000641 }else{
drhd24cc422003-03-27 12:51:24 +0000642 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000643 pList = 0;
drh4a324312001-12-21 14:30:42 +0000644 }
drhe0194f22003-02-26 13:52:51 +0000645
646primary_key_exit:
647 sqliteIdListDelete(pList);
648 return;
drh4a324312001-12-21 14:30:42 +0000649}
650
651/*
drhfcb78a42003-01-18 20:11:05 +0000652** Return the appropriate collating type given a type name.
653**
654** The collation type is text (SQLITE_SO_TEXT) if the type
655** name contains the character stream "text" or "blob" or
656** "clob". Any other type name is collated as numeric
657** (SQLITE_SO_NUM).
drh8e2ca022002-06-17 17:07:19 +0000658*/
drhfcb78a42003-01-18 20:11:05 +0000659int sqliteCollateType(const char *zType, int nType){
660 int i;
drhfcb78a42003-01-18 20:11:05 +0000661 for(i=0; i<nType-1; i++){
662 switch( zType[i] ){
663 case 'b':
664 case 'B': {
665 if( i<nType-3 && sqliteStrNICmp(&zType[i],"blob",4)==0 ){
666 return SQLITE_SO_TEXT;
667 }
668 break;
669 }
670 case 'c':
671 case 'C': {
672 if( i<nType-3 && (sqliteStrNICmp(&zType[i],"char",4)==0 ||
673 sqliteStrNICmp(&zType[i],"clob",4)==0)
674 ){
675 return SQLITE_SO_TEXT;
676 }
677 break;
678 }
679 case 'x':
680 case 'X': {
681 if( i>=2 && sqliteStrNICmp(&zType[i-2],"text",4)==0 ){
682 return SQLITE_SO_TEXT;
683 }
684 break;
685 }
686 default: {
687 break;
688 }
689 }
drh8e2ca022002-06-17 17:07:19 +0000690 }
drhfcb78a42003-01-18 20:11:05 +0000691 return SQLITE_SO_NUM;
drh8e2ca022002-06-17 17:07:19 +0000692}
693
694/*
695** This routine is called by the parser while in the middle of
696** parsing a CREATE TABLE statement. A "COLLATE" clause has
697** been seen on a column. This routine sets the Column.sortOrder on
698** the column currently under construction.
699*/
700void sqliteAddCollateType(Parse *pParse, int collType){
701 Table *p;
702 int i;
703 if( (p = pParse->pNewTable)==0 ) return;
704 i = p->nCol-1;
705 if( i>=0 ) p->aCol[i].sortOrder = collType;
706}
707
708/*
drh50e5dad2001-09-15 00:57:28 +0000709** Come up with a new random value for the schema cookie. Make sure
710** the new value is different from the old.
711**
712** The schema cookie is used to determine when the schema for the
713** database changes. After each schema change, the cookie value
714** changes. When a process first reads the schema it records the
715** cookie. Thereafter, whenever it goes to access the database,
716** it checks the cookie to make sure the schema has not changed
717** since it was last read.
718**
719** This plan is not completely bullet-proof. It is possible for
720** the schema to change multiple times and for the cookie to be
721** set back to prior value. But schema changes are infrequent
722** and the probability of hitting the same cookie value is only
723** 1 chance in 2^32. So we're safe enough.
724*/
drhe0bc4042002-06-25 01:09:11 +0000725void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh001bbcb2003-03-19 03:14:00 +0000726 if( db->next_cookie==db->aDb[0].schema_cookie ){
727 db->next_cookie = db->aDb[0].schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000728 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000729 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
730 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000731 }
732}
733
734/*
drh969fa7c2002-02-18 18:30:32 +0000735** Measure the number of characters needed to output the given
736** identifier. The number returned includes any quotes used
737** but does not include the null terminator.
738*/
739static int identLength(const char *z){
740 int n;
drh17f71932002-02-21 12:01:27 +0000741 int needQuote = 0;
742 for(n=0; *z; n++, z++){
743 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000744 }
drh17f71932002-02-21 12:01:27 +0000745 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000746}
747
748/*
749** Write an identifier onto the end of the given string. Add
750** quote characters as needed.
751*/
752static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000753 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000754 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000755 for(j=0; zIdent[j]; j++){
756 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
757 }
758 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
759 || sqliteKeywordCode(zIdent, j)!=TK_ID;
760 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000761 for(j=0; zIdent[j]; j++){
762 z[i++] = zIdent[j];
763 if( zIdent[j]=='\'' ) z[i++] = '\'';
764 }
drh17f71932002-02-21 12:01:27 +0000765 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000766 z[i] = 0;
767 *pIdx = i;
768}
769
770/*
771** Generate a CREATE TABLE statement appropriate for the given
772** table. Memory to hold the text of the statement is obtained
773** from sqliteMalloc() and must be freed by the calling function.
774*/
775static char *createTableStmt(Table *p){
776 int i, k, n;
777 char *zStmt;
778 char *zSep, *zSep2, *zEnd;
779 n = 0;
780 for(i=0; i<p->nCol; i++){
781 n += identLength(p->aCol[i].zName);
782 }
783 n += identLength(p->zName);
784 if( n<40 ){
785 zSep = "";
786 zSep2 = ",";
787 zEnd = ")";
788 }else{
789 zSep = "\n ";
790 zSep2 = ",\n ";
791 zEnd = "\n)";
792 }
drhe0bc4042002-06-25 01:09:11 +0000793 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +0000794 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +0000795 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +0000796 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000797 k = strlen(zStmt);
798 identPut(zStmt, &k, p->zName);
799 zStmt[k++] = '(';
800 for(i=0; i<p->nCol; i++){
801 strcpy(&zStmt[k], zSep);
802 k += strlen(&zStmt[k]);
803 zSep = zSep2;
804 identPut(zStmt, &k, p->aCol[i].zName);
805 }
806 strcpy(&zStmt[k], zEnd);
807 return zStmt;
808}
809
810/*
drh75897232000-05-29 14:26:00 +0000811** This routine is called to report the final ")" that terminates
812** a CREATE TABLE statement.
813**
drhf57b3392001-10-08 13:22:32 +0000814** The table structure that other action routines have been building
815** is added to the internal hash tables, assuming no errors have
816** occurred.
drh75897232000-05-29 14:26:00 +0000817**
drh1ccde152000-06-17 13:12:39 +0000818** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000819** unless this is a temporary table or initFlag==1. When initFlag==1,
820** it means we are reading the sqlite_master table because we just
821** connected to the database or because the sqlite_master table has
822** recently changes, so the entry for this table already exists in
823** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000824**
825** If the pSelect argument is not NULL, it means that this routine
826** was called to create a table generated from a
827** "CREATE TABLE ... AS SELECT ..." statement. The column names of
828** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000829*/
drh969fa7c2002-02-18 18:30:32 +0000830void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000831 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000832 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000833
drh969fa7c2002-02-18 18:30:32 +0000834 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000835 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000836 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000837
drh969fa7c2002-02-18 18:30:32 +0000838 /* If the table is generated from a SELECT, then construct the
839 ** list of columns and the text of the table.
840 */
841 if( pSelect ){
842 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000843 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000844 assert( p->aCol==0 );
845 p->nCol = pSelTab->nCol;
846 p->aCol = pSelTab->aCol;
847 pSelTab->nCol = 0;
848 pSelTab->aCol = 0;
849 sqliteDeleteTable(0, pSelTab);
850 }
851
drhd78eeee2001-09-13 16:18:53 +0000852 /* If the initFlag is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000853 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
854 ** So do not write to the disk again. Extract the root page number
855 ** for the table from the pParse->newTnum field. (The page number
856 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000857 */
858 if( pParse->initFlag ){
859 p->tnum = pParse->newTnum;
860 }
861
drhe3c41372001-09-17 20:25:58 +0000862 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000863 ** in the SQLITE_MASTER table of the database. The record number
864 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000865 **
drhe0bc4042002-06-25 01:09:11 +0000866 ** If this is a TEMPORARY table, write the entry into the auxiliary
867 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000868 */
869 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000870 int n;
drhd8bc7082000-06-07 23:51:50 +0000871 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000872
drhd8bc7082000-06-07 23:51:50 +0000873 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000874 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000875 if( p->pSelect==0 ){
876 /* A regular table */
drhd24cc422003-03-27 12:51:24 +0000877 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->iDb);
drh4ff6dfa2002-03-03 23:06:00 +0000878 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
879 }else{
880 /* A view */
881 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
882 }
drh969fa7c2002-02-18 18:30:32 +0000883 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000884 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
885 sqliteVdbeAddOp(v, OP_String, 0, 0);
886 if( p->pSelect==0 ){
887 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
888 }else{
889 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000890 }
drhe0bc4042002-06-25 01:09:11 +0000891 sqliteVdbeAddOp(v, OP_String, 0, 0);
892 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
893 sqliteVdbeAddOp(v, OP_String, 0, 0);
894 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
895 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
896 sqliteVdbeAddOp(v, OP_String, 0, 0);
897 if( pSelect ){
898 char *z = createTableStmt(p);
899 n = z ? strlen(z) : 0;
900 sqliteVdbeChangeP3(v, -1, z, n);
901 sqliteFree(z);
902 }else{
903 assert( pEnd!=0 );
904 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
905 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
906 }
907 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
908 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhd24cc422003-03-27 12:51:24 +0000909 if( !p->iDb ){
drhe0bc4042002-06-25 01:09:11 +0000910 sqliteChangeCookie(db, v);
911 }
912 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000913 if( pSelect ){
drhd24cc422003-03-27 12:51:24 +0000914 sqliteVdbeAddOp(v, OP_Integer, p->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000915 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh969fa7c2002-02-18 18:30:32 +0000916 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000917 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000918 }
drh1c928532002-01-31 15:54:21 +0000919 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000920 }
drh17e9e292003-02-01 13:53:28 +0000921
922 /* Add the table to the in-memory representation of the database.
923 */
drhd24cc422003-03-27 12:51:24 +0000924 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +0000925 Table *pOld;
926 FKey *pFKey;
drhd24cc422003-03-27 12:51:24 +0000927 pOld = sqliteHashInsert(&db->aDb[p->iDb].tblHash,
928 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +0000929 if( pOld ){
930 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
931 return;
932 }
933 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
934 int nTo = strlen(pFKey->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +0000935 pFKey->pNextTo = sqliteHashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
936 sqliteHashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +0000937 }
938 pParse->pNewTable = 0;
939 db->nTable++;
940 db->flags |= SQLITE_InternChanges;
941 }
drh75897232000-05-29 14:26:00 +0000942}
943
944/*
drha76b5df2002-02-23 02:32:10 +0000945** The parser calls this routine in order to create a new VIEW
946*/
947void sqliteCreateView(
948 Parse *pParse, /* The parsing context */
949 Token *pBegin, /* The CREATE token that begins the statement */
950 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +0000951 Select *pSelect, /* A SELECT statement that will become the new view */
952 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +0000953){
drha76b5df2002-02-23 02:32:10 +0000954 Table *p;
drh4b59ab52002-08-24 18:24:51 +0000955 int n;
drh4ff6dfa2002-03-03 23:06:00 +0000956 const char *z;
drh4b59ab52002-08-24 18:24:51 +0000957 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000958
drhe5f9c642003-01-13 23:27:31 +0000959 sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +0000960 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +0000961 if( p==0 || pParse->nErr ){
drh417be792002-03-03 18:59:40 +0000962 sqliteSelectDelete(pSelect);
963 return;
964 }
drh174b6192002-12-03 02:22:52 +0000965
drh4b59ab52002-08-24 18:24:51 +0000966 /* Make a copy of the entire SELECT statement that defines the view.
967 ** This will force all the Expr.token.z values to be dynamically
968 ** allocated rather than point to the input string - which means that
969 ** they will persist after the current sqlite_exec() call returns.
970 */
971 p->pSelect = sqliteSelectDup(pSelect);
972 sqliteSelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +0000973 if( !pParse->initFlag ){
drh4b59ab52002-08-24 18:24:51 +0000974 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +0000975 }
drh4b59ab52002-08-24 18:24:51 +0000976
977 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
978 ** the end.
979 */
drha76b5df2002-02-23 02:32:10 +0000980 sEnd = pParse->sLastToken;
981 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
982 sEnd.z += sEnd.n;
983 }
984 sEnd.n = 0;
985 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000986 z = pBegin->z;
987 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
988 sEnd.z = &z[n-1];
989 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +0000990
991 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
992 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +0000993 return;
drh417be792002-03-03 18:59:40 +0000994}
drha76b5df2002-02-23 02:32:10 +0000995
drh417be792002-03-03 18:59:40 +0000996/*
997** The Table structure pTable is really a VIEW. Fill in the names of
998** the columns of the view in the pTable structure. Return the number
999** of errors. If an error is seen leave an error message in pPare->zErrMsg.
1000*/
1001int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
1002 ExprList *pEList;
1003 Select *pSel;
1004 Table *pSelTab;
1005 int nErr = 0;
1006
1007 assert( pTable );
1008
1009 /* A positive nCol means the columns names for this view are
1010 ** already known.
1011 */
1012 if( pTable->nCol>0 ) return 0;
1013
1014 /* A negative nCol is a special marker meaning that we are currently
1015 ** trying to compute the column names. If we enter this routine with
1016 ** a negative nCol, it means two or more views form a loop, like this:
1017 **
1018 ** CREATE VIEW one AS SELECT * FROM two;
1019 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001020 **
1021 ** Actually, this error is caught previously and so the following test
1022 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001023 */
1024 if( pTable->nCol<0 ){
1025 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
1026 " is circularly defined", 0);
1027 pParse->nErr++;
1028 return 1;
1029 }
1030
1031 /* If we get this far, it means we need to compute the table names.
1032 */
1033 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1034 pSel = pTable->pSelect;
1035
1036 /* Note that the call to sqliteResultSetOfSelect() will expand any
1037 ** "*" elements in this list. But we will need to restore the list
1038 ** back to its original configuration afterwards, so we save a copy of
1039 ** the original in pEList.
1040 */
1041 pEList = pSel->pEList;
1042 pSel->pEList = sqliteExprListDup(pEList);
1043 if( pSel->pEList==0 ){
1044 pSel->pEList = pEList;
1045 return 1; /* Malloc failed */
1046 }
1047 pTable->nCol = -1;
1048 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
1049 if( pSelTab ){
1050 assert( pTable->aCol==0 );
1051 pTable->nCol = pSelTab->nCol;
1052 pTable->aCol = pSelTab->aCol;
1053 pSelTab->nCol = 0;
1054 pSelTab->aCol = 0;
1055 sqliteDeleteTable(0, pSelTab);
drhd24cc422003-03-27 12:51:24 +00001056 pParse->db->aDb[pTable->iDb].flags |= SQLITE_UnresetViews;
drh417be792002-03-03 18:59:40 +00001057 }else{
1058 pTable->nCol = 0;
1059 nErr++;
1060 }
1061 sqliteSelectUnbind(pSel);
1062 sqliteExprListDelete(pSel->pEList);
1063 pSel->pEList = pEList;
1064 return nErr;
1065}
1066
1067/*
1068** Clear the column names from the VIEW pTable.
1069**
1070** This routine is called whenever any other table or view is modified.
1071** The view passed into this routine might depend directly or indirectly
1072** on the modified or deleted table so we need to clear the old column
1073** names so that they will be recomputed.
1074*/
1075static void sqliteViewResetColumnNames(Table *pTable){
1076 int i;
1077 if( pTable==0 || pTable->pSelect==0 ) return;
1078 if( pTable->nCol==0 ) return;
1079 for(i=0; i<pTable->nCol; i++){
1080 sqliteFree(pTable->aCol[i].zName);
1081 sqliteFree(pTable->aCol[i].zDflt);
1082 sqliteFree(pTable->aCol[i].zType);
1083 }
1084 sqliteFree(pTable->aCol);
1085 pTable->aCol = 0;
1086 pTable->nCol = 0;
1087}
1088
1089/*
1090** Clear the column names from every VIEW.
1091*/
drhd24cc422003-03-27 12:51:24 +00001092static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001093 HashElem *i;
drhd24cc422003-03-27 12:51:24 +00001094 if( (db->aDb[idx].flags & SQLITE_UnresetViews)==0 ) return;
1095 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001096 Table *pTab = sqliteHashData(i);
1097 if( pTab->pSelect ){
1098 sqliteViewResetColumnNames(pTab);
1099 }
1100 }
drhd24cc422003-03-27 12:51:24 +00001101 db->aDb[idx].flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001102}
1103
1104/*
drh75897232000-05-29 14:26:00 +00001105** Given a token, look up a table with that name. If not found, leave
1106** an error for the parser to find and return NULL.
1107*/
drhcce7d172000-05-31 15:34:51 +00001108Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001109 char *zName;
1110 Table *pTab;
1111 zName = sqliteTableNameFromToken(pTok);
1112 if( zName==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001113 pTab = sqliteFindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001114 sqliteFree(zName);
1115 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001116 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1117 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001118 pParse->nErr++;
1119 }
1120 return pTab;
1121}
1122
1123/*
1124** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001125** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001126*/
drh4ff6dfa2002-03-03 23:06:00 +00001127void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001128 Table *pTable;
drh75897232000-05-29 14:26:00 +00001129 Vdbe *v;
1130 int base;
drh5edc3122001-09-13 21:53:09 +00001131 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001132 int iDb;
drh75897232000-05-29 14:26:00 +00001133
drhdaffd0e2001-04-11 14:28:42 +00001134 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001135 pTable = sqliteTableFromToken(pParse, pName);
1136 if( pTable==0 ) return;
drhd24cc422003-03-27 12:51:24 +00001137 iDb = pTable->iDb;
drhe5f9c642003-01-13 23:27:31 +00001138#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +00001139 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTable->iDb),0)){
drhed6c8672003-01-12 18:02:16 +00001140 return;
1141 }
drhe5f9c642003-01-13 23:27:31 +00001142 {
1143 int code;
1144 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001145 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001146 code = SQLITE_DROP_TEMP_VIEW;
1147 }else{
1148 code = SQLITE_DROP_VIEW;
1149 }
1150 }else{
drhd24cc422003-03-27 12:51:24 +00001151 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001152 code = SQLITE_DROP_TEMP_TABLE;
1153 }else{
1154 code = SQLITE_DROP_TABLE;
1155 }
1156 }
1157 if( sqliteAuthCheck(pParse, code, pTable->zName, 0) ){
1158 return;
1159 }
drh77ad4e42003-01-14 02:49:27 +00001160 if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0) ){
1161 return;
1162 }
drhe5f9c642003-01-13 23:27:31 +00001163 }
1164#endif
drh75897232000-05-29 14:26:00 +00001165 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001166 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1167 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001168 pParse->nErr++;
1169 return;
1170 }
drh4ff6dfa2002-03-03 23:06:00 +00001171 if( isView && pTable->pSelect==0 ){
1172 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1173 pTable->zName, 0);
1174 pParse->nErr++;
1175 return;
1176 }
1177 if( !isView && pTable->pSelect ){
1178 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1179 pTable->zName, 0);
1180 pParse->nErr++;
1181 return;
1182 }
drh75897232000-05-29 14:26:00 +00001183
drh1ccde152000-06-17 13:12:39 +00001184 /* Generate code to remove the table from the master table
1185 ** on disk.
1186 */
drhd8bc7082000-06-07 23:51:50 +00001187 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001188 if( v ){
1189 static VdbeOp dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001190 { OP_Rewind, 0, ADDR(8), 0},
1191 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001192 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001193 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001194 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001195 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001196 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001197 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001198 };
1199 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001200 Trigger *pTrigger;
drhd24cc422003-03-27 12:51:24 +00001201 sqliteBeginWriteOperation(pParse, 0, pTable->iDb);
1202 sqliteOpenMasterTable(v, pTable->iDb);
danielk1977c3f9bad2002-05-15 08:30:12 +00001203 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001204 pTrigger = pTable->pTrigger;
1205 while( pTrigger ){
drhd24cc422003-03-27 12:51:24 +00001206 SrcList *pNm;
1207 assert( pTrigger->iDb==pTable->iDb );
1208 pNm = sqliteSrcListAppend(0, 0, 0);
1209 pNm->a[0].zName = sqliteStrDup(pTrigger->name);
1210 pNm->a[0].zDatabase = sqliteStrDup(db->aDb[pTable->iDb].zName);
1211 sqliteDropTrigger(pParse, pNm, 1);
drhe0bc4042002-06-25 01:09:11 +00001212 if( pParse->explain ){
1213 pTrigger = pTrigger->pNext;
1214 }else{
1215 pTrigger = pTable->pTrigger;
1216 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001217 }
drhe0bc4042002-06-25 01:09:11 +00001218 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1219 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001220 if( !pTable->iDb ){
drhe0bc4042002-06-25 01:09:11 +00001221 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001222 }
drhe0bc4042002-06-25 01:09:11 +00001223 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001224 if( !isView ){
drhd24cc422003-03-27 12:51:24 +00001225 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001226 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drhd24cc422003-03-27 12:51:24 +00001227 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001228 }
drh5e00f6c2001-09-13 13:46:56 +00001229 }
drh1c928532002-01-31 15:54:21 +00001230 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001231 }
1232
drhe0bc4042002-06-25 01:09:11 +00001233 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001234 **
1235 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001236 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001237 */
1238 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001239 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001240 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001241 }
drhd24cc422003-03-27 12:51:24 +00001242 sqliteViewResetAll(db, iDb);
drh75897232000-05-29 14:26:00 +00001243}
1244
1245/*
drh38640e12002-07-05 21:42:36 +00001246** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1247** opcode and adds that P3 string to the most recently inserted instruction
1248** in the virtual machine. The P3 string consists of a single character
1249** for each column in the index pIdx of table pTab. If the column uses
1250** a numeric sort order, then the P3 string character corresponding to
1251** that column is 'n'. If the column uses a text sort order, then the
1252** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1253** additional information. See also the sqliteAddKeyType() routine.
1254*/
1255void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1256 char *zType;
1257 Table *pTab;
1258 int i, n;
1259 assert( pIdx!=0 && pIdx->pTable!=0 );
1260 pTab = pIdx->pTable;
1261 n = pIdx->nColumn;
drh8c1238a2003-01-02 14:43:55 +00001262 zType = sqliteMallocRaw( n+1 );
drh38640e12002-07-05 21:42:36 +00001263 if( zType==0 ) return;
1264 for(i=0; i<n; i++){
1265 int iCol = pIdx->aiColumn[i];
1266 assert( iCol>=0 && iCol<pTab->nCol );
1267 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1268 zType[i] = 't';
1269 }else{
1270 zType[i] = 'n';
1271 }
1272 }
1273 zType[n] = 0;
1274 sqliteVdbeChangeP3(v, -1, zType, n);
1275 sqliteFree(zType);
1276}
1277
1278/*
drhc2eef3b2002-08-31 18:53:06 +00001279** This routine is called to create a new foreign key on the table
1280** currently under construction. pFromCol determines which columns
1281** in the current table point to the foreign key. If pFromCol==0 then
1282** connect the key to the last column inserted. pTo is the name of
1283** the table referred to. pToCol is a list of tables in the other
1284** pTo table that the foreign key points to. flags contains all
1285** information about the conflict resolution algorithms specified
1286** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1287**
1288** An FKey structure is created and added to the table currently
1289** under construction in the pParse->pNewTable field. The new FKey
1290** is not linked into db->aFKey at this point - that does not happen
1291** until sqliteEndTable().
1292**
1293** The foreign key is set for IMMEDIATE processing. A subsequent call
1294** to sqliteDeferForeignKey() might change this to DEFERRED.
1295*/
1296void sqliteCreateForeignKey(
1297 Parse *pParse, /* Parsing context */
1298 IdList *pFromCol, /* Columns in this table that point to other table */
1299 Token *pTo, /* Name of the other table */
1300 IdList *pToCol, /* Columns in the other table */
1301 int flags /* Conflict resolution algorithms. */
1302){
1303 Table *p = pParse->pNewTable;
1304 int nByte;
1305 int i;
1306 int nCol;
1307 char *z;
1308 FKey *pFKey = 0;
1309
1310 assert( pTo!=0 );
1311 if( p==0 || pParse->nErr ) goto fk_end;
1312 if( pFromCol==0 ){
1313 int iCol = p->nCol-1;
1314 if( iCol<0 ) goto fk_end;
1315 if( pToCol && pToCol->nId!=1 ){
1316 sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1,
1317 p->aCol[iCol].zName, -1,
1318 " should reference only one column of table ", -1,
1319 pTo->z, pTo->n, 0);
1320 pParse->nErr++;
1321 goto fk_end;
1322 }
1323 nCol = 1;
1324 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
1325 sqliteSetString(&pParse->zErrMsg,
1326 "number of columns in foreign key does not match the number of "
1327 "columns in the referenced table", 0);
1328 pParse->nErr++;
1329 goto fk_end;
1330 }else{
1331 nCol = pFromCol->nId;
1332 }
1333 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1334 if( pToCol ){
1335 for(i=0; i<pToCol->nId; i++){
1336 nByte += strlen(pToCol->a[i].zName) + 1;
1337 }
1338 }
1339 pFKey = sqliteMalloc( nByte );
1340 if( pFKey==0 ) goto fk_end;
1341 pFKey->pFrom = p;
1342 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001343 z = (char*)&pFKey[1];
1344 pFKey->aCol = (struct sColMap*)z;
1345 z += sizeof(struct sColMap)*nCol;
1346 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001347 memcpy(z, pTo->z, pTo->n);
1348 z[pTo->n] = 0;
1349 z += pTo->n+1;
1350 pFKey->pNextTo = 0;
1351 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001352 if( pFromCol==0 ){
1353 pFKey->aCol[0].iFrom = p->nCol-1;
1354 }else{
1355 for(i=0; i<nCol; i++){
1356 int j;
1357 for(j=0; j<p->nCol; j++){
1358 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1359 pFKey->aCol[i].iFrom = j;
1360 break;
1361 }
1362 }
1363 if( j>=p->nCol ){
1364 sqliteSetString(&pParse->zErrMsg, "unknown column \"",
1365 pFromCol->a[i].zName, "\" in foreign key definition", 0);
1366 pParse->nErr++;
1367 goto fk_end;
1368 }
1369 }
1370 }
1371 if( pToCol ){
1372 for(i=0; i<nCol; i++){
1373 int n = strlen(pToCol->a[i].zName);
1374 pFKey->aCol[i].zCol = z;
1375 memcpy(z, pToCol->a[i].zName, n);
1376 z[n] = 0;
1377 z += n+1;
1378 }
1379 }
1380 pFKey->isDeferred = 0;
1381 pFKey->deleteConf = flags & 0xff;
1382 pFKey->updateConf = (flags >> 8 ) & 0xff;
1383 pFKey->insertConf = (flags >> 16 ) & 0xff;
1384
1385 /* Link the foreign key to the table as the last step.
1386 */
1387 p->pFKey = pFKey;
1388 pFKey = 0;
1389
1390fk_end:
1391 sqliteFree(pFKey);
1392 sqliteIdListDelete(pFromCol);
1393 sqliteIdListDelete(pToCol);
1394}
1395
1396/*
1397** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1398** clause is seen as part of a foreign key definition. The isDeferred
1399** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1400** The behavior of the most recently created foreign key is adjusted
1401** accordingly.
1402*/
1403void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1404 Table *pTab;
1405 FKey *pFKey;
1406 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1407 pFKey->isDeferred = isDeferred;
1408}
1409
1410/*
drh75897232000-05-29 14:26:00 +00001411** Create a new index for an SQL table. pIndex is the name of the index
1412** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001413** be NULL for a primary key or an index that is created to satisfy a
1414** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001415** as the table to be indexed. pParse->pNewTable is a table that is
1416** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001417**
drh382c0242001-10-06 16:33:02 +00001418** pList is a list of columns to be indexed. pList will be NULL if this
1419** is a primary key or unique-constraint on the most recent column added
1420** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001421*/
1422void sqliteCreateIndex(
1423 Parse *pParse, /* All information about this parse */
1424 Token *pName, /* Name of the index. May be NULL */
drhd24cc422003-03-27 12:51:24 +00001425 SrcList *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001426 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001427 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drhd24cc422003-03-27 12:51:24 +00001428 int isTemp, /* True if this is a temporary index */
drh75897232000-05-29 14:26:00 +00001429 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1430 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1431){
1432 Table *pTab; /* Table to be indexed */
1433 Index *pIndex; /* The index to be created */
1434 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001435 int i, j;
drhf57b3392001-10-08 13:22:32 +00001436 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001437 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001438
drhdaffd0e2001-04-11 14:28:42 +00001439 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1440
drh75897232000-05-29 14:26:00 +00001441 /*
1442 ** Find the table that is to be indexed. Return early if not found.
1443 */
1444 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001445 assert( pName!=0 );
drhd24cc422003-03-27 12:51:24 +00001446 assert( pTable->nSrc==1 );
1447 pTab = sqliteTableNameToTable(pParse,
1448 pTable->a[0].zName, pTable->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001449 }else{
drhe3c41372001-09-17 20:25:58 +00001450 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001451 pTab = pParse->pNewTable;
1452 }
1453 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001454 if( !isTemp && (pTab->readOnly || pTab->iDb>=2) ){
drhb24fcbe2000-05-29 23:30:50 +00001455 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drhd24cc422003-03-27 12:51:24 +00001456 " may not have non-temporary indices added", 0);
drh75897232000-05-29 14:26:00 +00001457 pParse->nErr++;
1458 goto exit_create_index;
1459 }
drha76b5df2002-02-23 02:32:10 +00001460 if( pTab->pSelect ){
1461 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1462 pParse->nErr++;
1463 goto exit_create_index;
1464 }
drhd24cc422003-03-27 12:51:24 +00001465 if( pTab->iDb==1 ){
1466 isTemp = 1;
1467 }
drh75897232000-05-29 14:26:00 +00001468
drhd24cc422003-03-27 12:51:24 +00001469
1470#if 0
drhf57b3392001-10-08 13:22:32 +00001471 /* If this index is created while re-reading the schema from sqlite_master
1472 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001473 ** only mean that the table that this index is really associated with is
1474 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001475 ** Since its table has been suppressed, we need to also suppress the
1476 ** index.
1477 */
drhd24cc422003-03-27 12:51:24 +00001478 if( pParse->initFlag && !pParse->isTemp && pTab->iDb ){
drhf57b3392001-10-08 13:22:32 +00001479 goto exit_create_index;
1480 }
drhd24cc422003-03-27 12:51:24 +00001481#endif
drhf57b3392001-10-08 13:22:32 +00001482
drh75897232000-05-29 14:26:00 +00001483 /*
1484 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001485 ** index or table with the same name.
1486 **
1487 ** Exception: If we are reading the names of permanent indices from the
1488 ** sqlite_master table (because some other process changed the schema) and
1489 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001490 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001491 **
1492 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001493 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1494 ** own name.
drh75897232000-05-29 14:26:00 +00001495 */
drhd24cc422003-03-27 12:51:24 +00001496 if( pName && !pParse->initFlag ){
drhf57b3392001-10-08 13:22:32 +00001497 Index *pISameName; /* Another index with the same name */
1498 Table *pTSameName; /* A table with same name as the index */
drhd24cc422003-03-27 12:51:24 +00001499 zName = sqliteStrNDup(pName->z, pName->n);
drhe3c41372001-09-17 20:25:58 +00001500 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001501 if( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){
1502 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1503 " already exists", 0);
1504 pParse->nErr++;
1505 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001506 }
drhd24cc422003-03-27 12:51:24 +00001507 if( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){
1508 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1509 zName, 0);
1510 pParse->nErr++;
1511 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001512 }
drhd24cc422003-03-27 12:51:24 +00001513 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001514 char zBuf[30];
1515 int n;
1516 Index *pLoop;
1517 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1518 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001519 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001520 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001521 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001522 }else{
1523 zName = sqliteStrNDup(pName->z, pName->n);
drh75897232000-05-29 14:26:00 +00001524 }
1525
drhe5f9c642003-01-13 23:27:31 +00001526 /* Check for authorization to create an index.
1527 */
1528#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +00001529 assert( isTemp==0 || isTemp==1 );
1530 assert( pTab->iDb==pParse->iDb || isTemp==1 );
1531 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drhe5f9c642003-01-13 23:27:31 +00001532 goto exit_create_index;
1533 }
1534 i = SQLITE_CREATE_INDEX;
drhd24cc422003-03-27 12:51:24 +00001535 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
drhe5f9c642003-01-13 23:27:31 +00001536 if( sqliteAuthCheck(pParse, i, zName, pTab->zName) ){
1537 goto exit_create_index;
1538 }
1539#endif
1540
drh75897232000-05-29 14:26:00 +00001541 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001542 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001543 ** So create a fake list to simulate this.
1544 */
1545 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001546 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001547 nullId.n = strlen(nullId.z);
1548 pList = sqliteIdListAppend(0, &nullId);
1549 if( pList==0 ) goto exit_create_index;
1550 }
1551
1552 /*
1553 ** Allocate the index structure.
1554 */
drhdcc581c2000-05-30 13:44:19 +00001555 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001556 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001557 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001558 pIndex->aiColumn = (int*)&pIndex[1];
1559 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001560 strcpy(pIndex->zName, zName);
1561 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001562 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001563 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001564 pIndex->autoIndex = pName==0;
drhd24cc422003-03-27 12:51:24 +00001565 pIndex->iDb = isTemp ? 1 : pParse->iDb;
drh75897232000-05-29 14:26:00 +00001566
drh1ccde152000-06-17 13:12:39 +00001567 /* Scan the names of the columns of the table to be indexed and
1568 ** load the column indices into the Index structure. Report an error
1569 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001570 */
1571 for(i=0; i<pList->nId; i++){
1572 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001573 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001574 }
1575 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001576 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001577 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001578 pParse->nErr++;
1579 sqliteFree(pIndex);
1580 goto exit_create_index;
1581 }
drh967e8b72000-06-21 13:59:10 +00001582 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001583 }
1584
1585 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001586 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001587 */
drhd24cc422003-03-27 12:51:24 +00001588 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00001589 Index *p;
drhd24cc422003-03-27 12:51:24 +00001590 p = sqliteHashInsert(&db->aDb[isTemp].idxHash,
1591 pIndex->zName, strlen(zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00001592 if( p ){
1593 assert( p==pIndex ); /* Malloc must have failed */
1594 sqliteFree(pIndex);
1595 goto exit_create_index;
1596 }
drh5e00f6c2001-09-13 13:46:56 +00001597 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001598 }
drh9cfcf5d2002-01-29 18:41:24 +00001599
1600 /* When adding an index to the list of indices for a table, make
1601 ** sure all indices labeled OE_Replace come after all those labeled
1602 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1603 ** and INSERT.
1604 */
1605 if( onError!=OE_Replace || pTab->pIndex==0
1606 || pTab->pIndex->onError==OE_Replace){
1607 pIndex->pNext = pTab->pIndex;
1608 pTab->pIndex = pIndex;
1609 }else{
1610 Index *pOther = pTab->pIndex;
1611 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1612 pOther = pOther->pNext;
1613 }
1614 pIndex->pNext = pOther->pNext;
1615 pOther->pNext = pIndex;
1616 }
drh75897232000-05-29 14:26:00 +00001617
drhd78eeee2001-09-13 16:18:53 +00001618 /* If the initFlag is 1 it means we are reading the SQL off the
1619 ** "sqlite_master" table on the disk. So do not write to the disk
1620 ** again. Extract the table number from the pParse->newTnum field.
1621 */
drhadbca9c2001-09-27 15:11:53 +00001622 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001623 pIndex->tnum = pParse->newTnum;
1624 }
1625
drh75897232000-05-29 14:26:00 +00001626 /* If the initFlag is 0 then create the index on disk. This
1627 ** involves writing the index into the master table and filling in the
1628 ** index with the current table contents.
1629 **
1630 ** The initFlag is 0 when the user first enters a CREATE INDEX
1631 ** command. The initFlag is 1 when a database is opened and
1632 ** CREATE INDEX statements are read out of the master table. In
1633 ** the latter case the index already exists on disk, which is why
1634 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001635 **
1636 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001637 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1638 ** has just been created, it contains no data and the index initialization
1639 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001640 */
drhadbca9c2001-09-27 15:11:53 +00001641 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001642 int n;
drhadbca9c2001-09-27 15:11:53 +00001643 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001644 int lbl1, lbl2;
1645 int i;
drhadbca9c2001-09-27 15:11:53 +00001646 int addr;
drh75897232000-05-29 14:26:00 +00001647
drhd8bc7082000-06-07 23:51:50 +00001648 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001649 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001650 if( pTable!=0 ){
drhcabb0812002-09-14 13:47:32 +00001651 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhe0bc4042002-06-25 01:09:11 +00001652 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001653 }
drhe0bc4042002-06-25 01:09:11 +00001654 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1655 sqliteVdbeAddOp(v, OP_String, 0, 0);
1656 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1657 sqliteVdbeAddOp(v, OP_String, 0, 0);
1658 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1659 sqliteVdbeAddOp(v, OP_String, 0, 0);
1660 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001661 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1662 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001663 pIndex->tnum = 0;
1664 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001665 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00001666 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
1667 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +00001668 }
drhe0bc4042002-06-25 01:09:11 +00001669 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1670 if( pStart && pEnd ){
1671 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1672 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001673 }
drhe0bc4042002-06-25 01:09:11 +00001674 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1675 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001676 if( pTable ){
drhd24cc422003-03-27 12:51:24 +00001677 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00001678 sqliteVdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +00001679 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001680 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001681 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1682 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001683 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001684 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001685 }
drh99fcd712001-10-13 01:06:47 +00001686 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001687 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001688 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh483750b2003-01-29 18:46:51 +00001689 sqliteVdbeChangeP3(v, -1, "indexed columns are not unique", P3_STATIC);
drh6b563442001-11-07 16:48:26 +00001690 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001691 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001692 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1693 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001694 }
drhadbca9c2001-09-27 15:11:53 +00001695 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001696 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001697 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001698 }
drhe0bc4042002-06-25 01:09:11 +00001699 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001700 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001701 }
drh75897232000-05-29 14:26:00 +00001702 }
1703
drh75897232000-05-29 14:26:00 +00001704 /* Clean up before exiting */
1705exit_create_index:
1706 sqliteIdListDelete(pList);
drhd24cc422003-03-27 12:51:24 +00001707 sqliteSrcListDelete(pTable);
drh75897232000-05-29 14:26:00 +00001708 sqliteFree(zName);
1709 return;
1710}
1711
1712/*
drh74e24cd2002-01-09 03:19:59 +00001713** This routine will drop an existing named index. This routine
1714** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001715*/
drhd24cc422003-03-27 12:51:24 +00001716void sqliteDropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00001717 Index *pIndex;
drh75897232000-05-29 14:26:00 +00001718 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001719 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001720
drhdaffd0e2001-04-11 14:28:42 +00001721 if( pParse->nErr || sqlite_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00001722 assert( pName->nSrc==1 );
1723 pIndex = sqliteFindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001724 if( pIndex==0 ){
drhd24cc422003-03-27 12:51:24 +00001725 sqliteSetString(&pParse->zErrMsg, "no such index: ", pName->a[0].zName, 0);
drh75897232000-05-29 14:26:00 +00001726 pParse->nErr++;
drhd24cc422003-03-27 12:51:24 +00001727 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00001728 }
drh485b39b2002-07-13 03:11:52 +00001729 if( pIndex->autoIndex ){
1730 sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE "
1731 "or PRIMARY KEY constraint cannot be dropped", 0);
1732 pParse->nErr++;
drhd24cc422003-03-27 12:51:24 +00001733 goto exit_drop_index;
1734 }
1735 if( pIndex->iDb>1 ){
1736 sqliteSetString(&pParse->zErrMsg, "cannot alter schema of attached "
1737 "databases", 0);
1738 pParse->nErr++;
1739 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00001740 }
drhe5f9c642003-01-13 23:27:31 +00001741#ifndef SQLITE_OMIT_AUTHORIZATION
1742 {
1743 int code = SQLITE_DROP_INDEX;
1744 Table *pTab = pIndex->pTable;
drhd24cc422003-03-27 12:51:24 +00001745 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pIndex->iDb), 0) ){
1746 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001747 }
drhd24cc422003-03-27 12:51:24 +00001748 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
drh77ad4e42003-01-14 02:49:27 +00001749 if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName) ){
drhd24cc422003-03-27 12:51:24 +00001750 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001751 }
drhed6c8672003-01-12 18:02:16 +00001752 }
drhe5f9c642003-01-13 23:27:31 +00001753#endif
drh75897232000-05-29 14:26:00 +00001754
1755 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001756 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001757 if( v ){
1758 static VdbeOp dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001759 { OP_Rewind, 0, ADDR(9), 0},
1760 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001761 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001762 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001763 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001764 { OP_Eq, 0, ADDR(8), 0},
1765 { OP_Next, 0, ADDR(3), 0},
1766 { OP_Goto, 0, ADDR(9), 0},
1767 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001768 };
1769 int base;
1770
drhd24cc422003-03-27 12:51:24 +00001771 sqliteBeginWriteOperation(pParse, 0, pIndex->iDb);
1772 sqliteOpenMasterTable(v, pIndex->iDb);
drhe0bc4042002-06-25 01:09:11 +00001773 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1774 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001775 if( pIndex->iDb==0 ){
drhe0bc4042002-06-25 01:09:11 +00001776 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001777 }
drhe0bc4042002-06-25 01:09:11 +00001778 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhd24cc422003-03-27 12:51:24 +00001779 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
drh1c928532002-01-31 15:54:21 +00001780 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001781 }
1782
drhe0bc4042002-06-25 01:09:11 +00001783 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001784 */
1785 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001786 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001787 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001788 }
drhd24cc422003-03-27 12:51:24 +00001789
1790exit_drop_index:
1791 sqliteSrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001792}
1793
1794/*
drh75897232000-05-29 14:26:00 +00001795** Append a new element to the given IdList. Create a new IdList if
1796** need be.
drhdaffd0e2001-04-11 14:28:42 +00001797**
1798** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001799*/
1800IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1801 if( pList==0 ){
1802 pList = sqliteMalloc( sizeof(IdList) );
1803 if( pList==0 ) return 0;
1804 }
1805 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001806 struct IdList_item *a;
1807 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1808 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001809 sqliteIdListDelete(pList);
1810 return 0;
drh75897232000-05-29 14:26:00 +00001811 }
drh6d4abfb2001-10-22 02:58:08 +00001812 pList->a = a;
drh75897232000-05-29 14:26:00 +00001813 }
1814 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1815 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001816 char **pz = &pList->a[pList->nId].zName;
1817 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1818 if( *pz==0 ){
1819 sqliteIdListDelete(pList);
1820 return 0;
1821 }else{
1822 sqliteDequote(*pz);
1823 }
drh75897232000-05-29 14:26:00 +00001824 }
1825 pList->nId++;
1826 return pList;
1827}
1828
1829/*
drhad3cab52002-05-24 02:04:32 +00001830** Append a new table name to the given SrcList. Create a new SrcList if
1831** need be. A new entry is created in the SrcList even if pToken is NULL.
1832**
1833** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00001834**
1835** If pDatabase is not null, it means that the table has an optional
1836** database name prefix. Like this: "database.table". The pDatabase
1837** points to the table name and the pTable points to the database name.
1838** The SrcList.a[].zName field is filled with the table name which might
1839** come from pTable (if pDatabase is NULL) or from pDatabase.
1840** SrcList.a[].zDatabase is filled with the database name from pTable,
1841** or with NULL if no database is specified.
1842**
1843** In other words, if call like this:
1844**
1845** sqliteSrcListAppend(A,B,0);
1846**
1847** Then B is a table name and the database name is unspecified. If called
1848** like this:
1849**
1850** sqliteSrcListAppend(A,B,C);
1851**
1852** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00001853*/
drh113088e2003-03-20 01:16:58 +00001854SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00001855 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00001856 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00001857 if( pList==0 ) return 0;
1858 }
drh113088e2003-03-20 01:16:58 +00001859 if( (pList->nSrc & 7)==1 ){
1860 SrcList *pNew;
1861 pNew = sqliteRealloc(pList,
1862 sizeof(*pList) + (pList->nSrc+8)*sizeof(pList->a[0]) );
1863 if( pNew==0 ){
drhad3cab52002-05-24 02:04:32 +00001864 sqliteSrcListDelete(pList);
1865 return 0;
1866 }
drh113088e2003-03-20 01:16:58 +00001867 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00001868 }
1869 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00001870 if( pDatabase && pDatabase->z==0 ){
1871 pDatabase = 0;
1872 }
1873 if( pDatabase && pTable ){
1874 Token *pTemp = pDatabase;
1875 pDatabase = pTable;
1876 pTable = pTemp;
1877 }
1878 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00001879 char **pz = &pList->a[pList->nSrc].zName;
drh113088e2003-03-20 01:16:58 +00001880 sqliteSetNString(pz, pTable->z, pTable->n, 0);
1881 if( *pz==0 ){
1882 sqliteSrcListDelete(pList);
1883 return 0;
1884 }else{
1885 sqliteDequote(*pz);
1886 }
1887 }
1888 if( pDatabase ){
1889 char **pz = &pList->a[pList->nSrc].zDatabase;
1890 sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00001891 if( *pz==0 ){
1892 sqliteSrcListDelete(pList);
1893 return 0;
1894 }else{
1895 sqliteDequote(*pz);
1896 }
1897 }
1898 pList->nSrc++;
1899 return pList;
1900}
1901
1902/*
drh75897232000-05-29 14:26:00 +00001903** Add an alias to the last identifier on the given identifier list.
1904*/
drhad3cab52002-05-24 02:04:32 +00001905void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1906 if( pList && pList->nSrc>0 ){
1907 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001908 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001909 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001910 }
1911}
1912
1913/*
drhad3cab52002-05-24 02:04:32 +00001914** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001915*/
1916void sqliteIdListDelete(IdList *pList){
1917 int i;
1918 if( pList==0 ) return;
1919 for(i=0; i<pList->nId; i++){
1920 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001921 }
1922 sqliteFree(pList->a);
1923 sqliteFree(pList);
1924}
1925
1926/*
drhad2d8302002-05-24 20:31:36 +00001927** Return the index in pList of the identifier named zId. Return -1
1928** if not found.
1929*/
1930int sqliteIdListIndex(IdList *pList, const char *zName){
1931 int i;
1932 if( pList==0 ) return -1;
1933 for(i=0; i<pList->nId; i++){
1934 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1935 }
1936 return -1;
1937}
1938
1939/*
drhad3cab52002-05-24 02:04:32 +00001940** Delete an entire SrcList including all its substructure.
1941*/
1942void sqliteSrcListDelete(SrcList *pList){
1943 int i;
1944 if( pList==0 ) return;
1945 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00001946 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00001947 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001948 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001949 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001950 sqliteDeleteTable(0, pList->a[i].pTab);
1951 }
drhff78bd22002-02-27 01:47:11 +00001952 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00001953 sqliteExprDelete(pList->a[i].pOn);
1954 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00001955 }
drh75897232000-05-29 14:26:00 +00001956 sqliteFree(pList);
1957}
1958
drh982cef72000-05-30 16:27:03 +00001959/*
1960** The COPY command is for compatibility with PostgreSQL and specificially
1961** for the ability to read the output of pg_dump. The format is as
1962** follows:
1963**
1964** COPY table FROM file [USING DELIMITERS string]
1965**
1966** "table" is an existing table name. We will read lines of code from
1967** file to fill this table with data. File might be "stdin". The optional
1968** delimiter string identifies the field separators. The default is a tab.
1969*/
1970void sqliteCopy(
1971 Parse *pParse, /* The parser context */
drhd24cc422003-03-27 12:51:24 +00001972 SrcList *pTableName, /* The name of the table into which we will insert */
drh982cef72000-05-30 16:27:03 +00001973 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001974 Token *pDelimiter, /* Use this as the field delimiter */
1975 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001976){
1977 Table *pTab;
drh1c928532002-01-31 15:54:21 +00001978 int i;
drh982cef72000-05-30 16:27:03 +00001979 Vdbe *v;
1980 int addr, end;
1981 Index *pIdx;
drh77ad4e42003-01-14 02:49:27 +00001982 char *zFile = 0;
drhbe0072d2001-09-13 14:46:09 +00001983 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001984
drh77ad4e42003-01-14 02:49:27 +00001985
drhd24cc422003-03-27 12:51:24 +00001986 if( sqlite_malloc_failed ) goto copy_cleanup;
1987 assert( pTableName->nSrc==1 );
1988 pTab = sqliteTableNameToTable(pParse, pTableName->a[0].zName,
1989 pTableName->a[0].zDatabase);
drhef2daf52002-03-04 02:26:15 +00001990 if( pTab==0 ) goto copy_cleanup;
drh77ad4e42003-01-14 02:49:27 +00001991 zFile = sqliteStrNDup(pFilename->z, pFilename->n);
1992 sqliteDequote(zFile);
1993 if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, zFile)
1994 || sqliteAuthCheck(pParse, SQLITE_COPY, pTab->zName, zFile) ){
drhed6c8672003-01-12 18:02:16 +00001995 goto copy_cleanup;
1996 }
drhd8bc7082000-06-07 23:51:50 +00001997 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001998 if( v ){
drhd24cc422003-03-27 12:51:24 +00001999 sqliteBeginWriteOperation(pParse, 1, pTab->iDb==1);
drh99fcd712001-10-13 01:06:47 +00002000 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00002001 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00002002 sqliteVdbeDequoteP3(v, addr);
drhd24cc422003-03-27 12:51:24 +00002003 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00002004 sqliteVdbeAddOp(v, OP_OpenWrite, 0, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +00002005 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00002006 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhd24cc422003-03-27 12:51:24 +00002007 assert( pIdx->iDb==1 || pIdx->iDb==pTab->iDb );
2008 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00002009 sqliteVdbeAddOp(v, OP_OpenWrite, i, pIdx->tnum);
drh99fcd712001-10-13 01:06:47 +00002010 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00002011 }
drhb419a922002-01-30 16:17:23 +00002012 if( db->flags & SQLITE_CountRows ){
2013 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
2014 }
drh982cef72000-05-30 16:27:03 +00002015 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00002016 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00002017 if( pDelimiter ){
2018 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
2019 sqliteVdbeDequoteP3(v, addr);
2020 }else{
2021 sqliteVdbeChangeP3(v, addr, "\t", 1);
2022 }
drh8aff1012001-12-22 14:49:24 +00002023 if( pTab->iPKey>=0 ){
2024 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
2025 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
2026 }else{
2027 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
2028 }
drh982cef72000-05-30 16:27:03 +00002029 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00002030 if( i==pTab->iPKey ){
2031 /* The integer primary key column is filled with NULL since its
2032 ** value is always pulled from the record number */
2033 sqliteVdbeAddOp(v, OP_String, 0, 0);
2034 }else{
2035 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
2036 }
drh982cef72000-05-30 16:27:03 +00002037 }
drhb419a922002-01-30 16:17:23 +00002038 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
2039 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
2040 if( (db->flags & SQLITE_CountRows)!=0 ){
2041 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00002042 }
drh99fcd712001-10-13 01:06:47 +00002043 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
2044 sqliteVdbeResolveLabel(v, end);
2045 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00002046 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00002047 if( db->flags & SQLITE_CountRows ){
drhb419a922002-01-30 16:17:23 +00002048 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
2049 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
2050 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
2051 }
drh982cef72000-05-30 16:27:03 +00002052 }
2053
2054copy_cleanup:
drhd24cc422003-03-27 12:51:24 +00002055 sqliteSrcListDelete(pTableName);
drh77ad4e42003-01-14 02:49:27 +00002056 sqliteFree(zFile);
drh982cef72000-05-30 16:27:03 +00002057 return;
2058}
drhdce2cbe2000-05-31 02:27:49 +00002059
2060/*
2061** The non-standard VACUUM command is used to clean up the database,
2062** collapse free space, etc. It is modelled after the VACUUM command
2063** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00002064**
drh1bffb9c2002-02-03 17:37:36 +00002065** In version 1.0.x of SQLite, the VACUUM command would call
2066** gdbm_reorganize() on all the database tables. But beginning
2067** with 2.0.0, SQLite no longer uses GDBM so this command has
2068** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00002069*/
2070void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00002071 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00002072}
drhc4a3c772001-04-04 11:48:57 +00002073
2074/*
2075** Begin a transaction
2076*/
drh1c928532002-01-31 15:54:21 +00002077void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00002078 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002079
drh001bbcb2003-03-19 03:14:00 +00002080 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002081 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002082 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002083 if( db->flags & SQLITE_InTrans ){
2084 pParse->nErr++;
2085 sqliteSetString(&pParse->zErrMsg, "cannot start a transaction "
2086 "within a transaction", 0);
2087 return;
2088 }
drhcabb0812002-09-14 13:47:32 +00002089 sqliteBeginWriteOperation(pParse, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00002090 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002091 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00002092}
2093
2094/*
2095** Commit a transaction
2096*/
2097void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002098 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002099
drh001bbcb2003-03-19 03:14:00 +00002100 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002101 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002102 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002103 if( (db->flags & SQLITE_InTrans)==0 ){
2104 pParse->nErr++;
2105 sqliteSetString(&pParse->zErrMsg,
2106 "cannot commit - no transaction is active", 0);
2107 return;
2108 }
drh5e00f6c2001-09-13 13:46:56 +00002109 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002110 sqliteEndWriteOperation(pParse);
2111 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002112}
2113
2114/*
2115** Rollback a transaction
2116*/
2117void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002118 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002119 Vdbe *v;
2120
drh001bbcb2003-03-19 03:14:00 +00002121 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002122 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002123 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002124 if( (db->flags & SQLITE_InTrans)==0 ){
2125 pParse->nErr++;
2126 sqliteSetString(&pParse->zErrMsg,
2127 "cannot rollback - no transaction is active", 0);
2128 return;
2129 }
drh5e00f6c2001-09-13 13:46:56 +00002130 v = sqliteGetVdbe(pParse);
2131 if( v ){
drh99fcd712001-10-13 01:06:47 +00002132 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002133 }
drh5e00f6c2001-09-13 13:46:56 +00002134 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002135 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002136}
drhf57b14a2001-09-14 18:54:08 +00002137
2138/*
drh001bbcb2003-03-19 03:14:00 +00002139** Generate VDBE code that will verify the schema cookie for all
2140** named database files.
2141*/
2142void sqliteCodeVerifySchema(Parse *pParse){
2143 int i;
2144 sqlite *db = pParse->db;
2145 Vdbe *v = sqliteGetVdbe(pParse);
2146 for(i=0; i<db->nDb; i++){
drh113088e2003-03-20 01:16:58 +00002147 if( i==1 || db->aDb[i].pBt==0 ) continue;
drh001bbcb2003-03-19 03:14:00 +00002148 sqliteVdbeAddOp(v, OP_VerifyCookie, 0, db->aDb[i].schema_cookie);
2149 }
2150 pParse->schemaVerified = 1;
2151}
2152
2153/*
drh1c928532002-01-31 15:54:21 +00002154** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002155** might change the database.
2156**
2157** This routine starts a new transaction if we are not already within
2158** a transaction. If we are already within a transaction, then a checkpoint
2159** is set if the setCheckpoint parameter is true. A checkpoint should
2160** be set for operations that might fail (due to a constraint) part of
2161** the way through and which will need to undo some writes without having to
2162** rollback the whole transaction. For operations where all constraints
2163** can be checked before any changes are made to the database, it is never
2164** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002165**
2166** The tempOnly flag indicates that only temporary tables will be changed
2167** during this write operation. The primary database table is not
2168** write-locked. Only the temporary database file gets a write lock.
2169** Other processes can continue to read or write the primary database file.
drh1c928532002-01-31 15:54:21 +00002170*/
drhcabb0812002-09-14 13:47:32 +00002171void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){
drh663fc632002-02-02 18:49:19 +00002172 Vdbe *v;
2173 v = sqliteGetVdbe(pParse);
2174 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00002175 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00002176 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drh001bbcb2003-03-19 03:14:00 +00002177 sqliteVdbeAddOp(v, OP_Transaction, 1, 0);
drhcabb0812002-09-14 13:47:32 +00002178 if( !tempOnly ){
drh001bbcb2003-03-19 03:14:00 +00002179 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
2180 sqliteCodeVerifySchema(pParse);
drhcabb0812002-09-14 13:47:32 +00002181 }
drhc977f7f2002-05-21 11:38:11 +00002182 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00002183 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00002184 sqliteVdbeAddOp(v, OP_Checkpoint, 1, 0);
drh663fc632002-02-02 18:49:19 +00002185 }
2186}
2187
2188/*
drh1c928532002-01-31 15:54:21 +00002189** Generate code that concludes an operation that may have changed
2190** the database. This is a companion function to BeginWriteOperation().
2191** If a transaction was started, then commit it. If a checkpoint was
2192** started then commit that.
2193*/
2194void sqliteEndWriteOperation(Parse *pParse){
2195 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00002196 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00002197 v = sqliteGetVdbe(pParse);
2198 if( v==0 ) return;
2199 if( pParse->db->flags & SQLITE_InTrans ){
2200 /* Do Nothing */
2201 }else{
2202 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2203 }
2204}
2205
2206
2207/*
drhf57b14a2001-09-14 18:54:08 +00002208** Interpret the given string as a boolean value.
2209*/
2210static int getBoolean(char *z){
2211 static char *azTrue[] = { "yes", "on", "true" };
2212 int i;
2213 if( z[0]==0 ) return 0;
2214 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2215 return atoi(z);
2216 }
2217 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
2218 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
2219 }
2220 return 0;
2221}
2222
2223/*
drh973b6e32003-02-12 14:09:42 +00002224** Interpret the given string as a safety level. Return 0 for OFF,
2225** 1 for ON or NORMAL and 2 for FULL.
2226**
2227** Note that the values returned are one less that the values that
2228** should be passed into sqliteBtreeSetSafetyLevel(). The is done
2229** to support legacy SQL code. The safety level used to be boolean
2230** and older scripts may have used numbers 0 for OFF and 1 for ON.
2231*/
2232static int getSafetyLevel(char *z){
2233 static const struct {
2234 const char *zWord;
2235 int val;
2236 } aKey[] = {
2237 { "no", 0 },
2238 { "off", 0 },
2239 { "false", 0 },
2240 { "yes", 1 },
2241 { "on", 1 },
2242 { "true", 1 },
2243 { "full", 2 },
2244 };
2245 int i;
2246 if( z[0]==0 ) return 1;
2247 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2248 return atoi(z);
2249 }
2250 for(i=0; i<sizeof(aKey)/sizeof(aKey[0]); i++){
2251 if( sqliteStrICmp(z,aKey[i].zWord)==0 ) return aKey[i].val;
2252 }
2253 return 1;
2254}
2255
2256/*
drhf57b14a2001-09-14 18:54:08 +00002257** Process a pragma statement.
2258**
2259** Pragmas are of this form:
2260**
2261** PRAGMA id = value
2262**
2263** The identifier might also be a string. The value is a string, and
2264** identifier, or a number. If minusFlag is true, then the value is
2265** a number that was preceded by a minus sign.
2266*/
2267void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
2268 char *zLeft = 0;
2269 char *zRight = 0;
2270 sqlite *db = pParse->db;
drhdde85d92003-03-01 19:45:34 +00002271 Vdbe *v = sqliteGetVdbe(pParse);
2272 if( v==0 ) return;
drhf57b14a2001-09-14 18:54:08 +00002273
2274 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
2275 sqliteDequote(zLeft);
2276 if( minusFlag ){
2277 zRight = 0;
2278 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
2279 }else{
2280 zRight = sqliteStrNDup(pRight->z, pRight->n);
2281 sqliteDequote(zRight);
2282 }
drhbf0c78a2003-01-14 02:54:08 +00002283 if( sqliteAuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight) ){
2284 sqliteFree(zLeft);
2285 sqliteFree(zRight);
2286 return;
2287 }
drhf57b14a2001-09-14 18:54:08 +00002288
drhcd61c282002-03-06 22:01:34 +00002289 /*
2290 ** PRAGMA default_cache_size
2291 ** PRAGMA default_cache_size=N
2292 **
2293 ** The first form reports the current persistent setting for the
2294 ** page cache size. The value returned is the maximum number of
2295 ** pages in the page cache. The second form sets both the current
2296 ** page cache size value and the persistent page cache size value
2297 ** stored in the database file.
2298 **
2299 ** The default cache size is stored in meta-value 2 of page 1 of the
2300 ** database file. The cache size is actually the absolute value of
2301 ** this memory location. The sign of meta-value 2 determines the
2302 ** synchronous setting. A negative value means synchronous is off
2303 ** and a positive value means synchronous is on.
2304 */
2305 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00002306 static VdbeOp getCacheSize[] = {
2307 { OP_ReadCookie, 0, 2, 0},
2308 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00002309 { OP_Dup, 0, 0, 0},
2310 { OP_Integer, 0, 0, 0},
2311 { OP_Ne, 0, 6, 0},
2312 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00002313 { OP_ColumnName, 0, 0, "cache_size"},
2314 { OP_Callback, 1, 0, 0},
2315 };
drh603240c2002-03-05 01:11:12 +00002316 if( pRight->z==pLeft->z ){
2317 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2318 }else{
2319 int addr;
2320 int size = atoi(zRight);
2321 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002322 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002323 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2324 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
2325 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2326 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
2327 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
2328 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2329 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002330 db->cache_size = db->cache_size<0 ? -size : size;
drh001bbcb2003-03-19 03:14:00 +00002331 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002332 }
2333 }else
2334
drhcd61c282002-03-06 22:01:34 +00002335 /*
2336 ** PRAGMA cache_size
2337 ** PRAGMA cache_size=N
2338 **
2339 ** The first form reports the current local setting for the
2340 ** page cache size. The local setting can be different from
2341 ** the persistent cache size value that is stored in the database
2342 ** file itself. The value returned is the maximum number of
2343 ** pages in the page cache. The second form sets the local
2344 ** page cache size value. It does not change the persistent
2345 ** cache size stored on the disk so the cache size will revert
2346 ** to its default value when the database is closed and reopened.
2347 ** N should be a positive integer.
2348 */
2349 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
2350 static VdbeOp getCacheSize[] = {
drhcd61c282002-03-06 22:01:34 +00002351 { OP_ColumnName, 0, 0, "cache_size"},
2352 { OP_Callback, 1, 0, 0},
2353 };
drhcd61c282002-03-06 22:01:34 +00002354 if( pRight->z==pLeft->z ){
2355 int size = db->cache_size;;
2356 if( size<0 ) size = -size;
2357 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2358 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2359 }else{
2360 int size = atoi(zRight);
2361 if( size<0 ) size = -size;
2362 if( db->cache_size<0 ) size = -size;
2363 db->cache_size = size;
drh001bbcb2003-03-19 03:14:00 +00002364 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
drhcd61c282002-03-06 22:01:34 +00002365 }
2366 }else
2367
2368 /*
2369 ** PRAGMA default_synchronous
drh973b6e32003-02-12 14:09:42 +00002370 ** PRAGMA default_synchronous=ON|OFF|NORMAL|FULL
drhcd61c282002-03-06 22:01:34 +00002371 **
2372 ** The first form returns the persistent value of the "synchronous" setting
2373 ** that is stored in the database. This is the synchronous setting that
2374 ** is used whenever the database is opened unless overridden by a separate
2375 ** "synchronous" pragma. The second form changes the persistent and the
2376 ** local synchronous setting to the value given.
2377 **
drh973b6e32003-02-12 14:09:42 +00002378 ** If synchronous is OFF, SQLite does not attempt any fsync() systems calls
2379 ** to make sure data is committed to disk. Write operations are very fast,
2380 ** but a power failure can leave the database in an inconsistent state.
2381 ** If synchronous is ON or NORMAL, SQLite will do an fsync() system call to
2382 ** make sure data is being written to disk. The risk of corruption due to
2383 ** a power loss in this mode is negligible but non-zero. If synchronous
2384 ** is FULL, extra fsync()s occur to reduce the risk of corruption to near
2385 ** zero, but with a write performance penalty. The default mode is NORMAL.
drhcd61c282002-03-06 22:01:34 +00002386 */
2387 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00002388 static VdbeOp getSync[] = {
drh973b6e32003-02-12 14:09:42 +00002389 { OP_ColumnName, 0, 0, "synchronous"},
2390 { OP_ReadCookie, 0, 3, 0},
2391 { OP_Dup, 0, 0, 0},
2392 { OP_If, 0, 0, 0}, /* 3 */
drh603240c2002-03-05 01:11:12 +00002393 { OP_ReadCookie, 0, 2, 0},
2394 { OP_Integer, 0, 0, 0},
2395 { OP_Lt, 0, 5, 0},
2396 { OP_AddImm, 1, 0, 0},
drh603240c2002-03-05 01:11:12 +00002397 { OP_Callback, 1, 0, 0},
drh973b6e32003-02-12 14:09:42 +00002398 { OP_Halt, 0, 0, 0},
2399 { OP_AddImm, -1, 0, 0}, /* 10 */
2400 { OP_Callback, 1, 0, 0}
drh603240c2002-03-05 01:11:12 +00002401 };
drh603240c2002-03-05 01:11:12 +00002402 if( pRight->z==pLeft->z ){
drh973b6e32003-02-12 14:09:42 +00002403 int addr = sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2404 sqliteVdbeChangeP2(v, addr+3, addr+10);
drh603240c2002-03-05 01:11:12 +00002405 }else{
2406 int addr;
drhcd61c282002-03-06 22:01:34 +00002407 int size = db->cache_size;
2408 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002409 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002410 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00002411 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2412 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2413 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
2414 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00002415 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
drh973b6e32003-02-12 14:09:42 +00002416 db->safety_level = getSafetyLevel(zRight)+1;
2417 if( db->safety_level==1 ){
drh603240c2002-03-05 01:11:12 +00002418 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00002419 size = -size;
drh603240c2002-03-05 01:11:12 +00002420 }
2421 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
drh973b6e32003-02-12 14:09:42 +00002422 sqliteVdbeAddOp(v, OP_Integer, db->safety_level, 0);
2423 sqliteVdbeAddOp(v, OP_SetCookie, 0, 3);
drh603240c2002-03-05 01:11:12 +00002424 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002425 db->cache_size = size;
drh001bbcb2003-03-19 03:14:00 +00002426 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
2427 sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
drhcd61c282002-03-06 22:01:34 +00002428 }
2429 }else
2430
2431 /*
2432 ** PRAGMA synchronous
drh973b6e32003-02-12 14:09:42 +00002433 ** PRAGMA synchronous=OFF|ON|NORMAL|FULL
drhcd61c282002-03-06 22:01:34 +00002434 **
2435 ** Return or set the local value of the synchronous flag. Changing
2436 ** the local value does not make changes to the disk file and the
2437 ** default value will be restored the next time the database is
2438 ** opened.
2439 */
2440 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
2441 static VdbeOp getSync[] = {
drhcd61c282002-03-06 22:01:34 +00002442 { OP_ColumnName, 0, 0, "synchronous"},
2443 { OP_Callback, 1, 0, 0},
2444 };
drhcd61c282002-03-06 22:01:34 +00002445 if( pRight->z==pLeft->z ){
drh973b6e32003-02-12 14:09:42 +00002446 sqliteVdbeAddOp(v, OP_Integer, db->safety_level-1, 0);
drhcd61c282002-03-06 22:01:34 +00002447 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2448 }else{
2449 int size = db->cache_size;
2450 if( size<0 ) size = -size;
drh973b6e32003-02-12 14:09:42 +00002451 db->safety_level = getSafetyLevel(zRight)+1;
2452 if( db->safety_level==1 ) size = -size;
drhcd61c282002-03-06 22:01:34 +00002453 db->cache_size = size;
drh001bbcb2003-03-19 03:14:00 +00002454 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
2455 sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
drh603240c2002-03-05 01:11:12 +00002456 }
drhf57b14a2001-09-14 18:54:08 +00002457 }else
2458
danielk1977c3f9bad2002-05-15 08:30:12 +00002459 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
2460 if( getBoolean(zRight) ){
2461 always_code_trigger_setup = 1;
2462 }else{
2463 always_code_trigger_setup = 0;
2464 }
2465 }else
2466
drhf57b14a2001-09-14 18:54:08 +00002467 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
2468 if( getBoolean(zRight) ){
2469 db->flags |= SQLITE_VdbeTrace;
2470 }else{
2471 db->flags &= ~SQLITE_VdbeTrace;
2472 }
2473 }else
2474
drh382c0242001-10-06 16:33:02 +00002475 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
2476 if( getBoolean(zRight) ){
2477 db->flags |= SQLITE_FullColNames;
2478 }else{
2479 db->flags &= ~SQLITE_FullColNames;
2480 }
2481 }else
2482
drh5080aaa2002-07-11 12:18:16 +00002483 if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
2484 if( getBoolean(zRight) ){
2485 db->flags |= SQLITE_ReportTypes;
2486 }else{
2487 db->flags &= ~SQLITE_ReportTypes;
2488 }
2489 }else
2490
drhc3a64ba2001-11-22 00:01:27 +00002491 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2492 if( getBoolean(zRight) ){
2493 db->flags |= SQLITE_ResultDetails;
2494 }else{
2495 db->flags &= ~SQLITE_ResultDetails;
2496 }
2497 }else
2498
drh1bee3d72001-10-15 00:44:35 +00002499 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2500 if( getBoolean(zRight) ){
2501 db->flags |= SQLITE_CountRows;
2502 }else{
2503 db->flags &= ~SQLITE_CountRows;
2504 }
2505 }else
2506
drh6a535342001-10-19 16:44:56 +00002507 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2508 if( getBoolean(zRight) ){
2509 db->flags |= SQLITE_NullCallback;
2510 }else{
2511 db->flags &= ~SQLITE_NullCallback;
2512 }
2513 }else
2514
drh382c0242001-10-06 16:33:02 +00002515 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2516 Table *pTab;
drhd24cc422003-03-27 12:51:24 +00002517 pTab = sqliteFindTable(db, zRight, 0);
drhdde85d92003-03-01 19:45:34 +00002518 if( pTab ){
drh382c0242001-10-06 16:33:02 +00002519 static VdbeOp tableInfoPreface[] = {
drh382c0242001-10-06 16:33:02 +00002520 { OP_ColumnName, 0, 0, "cid"},
2521 { OP_ColumnName, 1, 0, "name"},
2522 { OP_ColumnName, 2, 0, "type"},
2523 { OP_ColumnName, 3, 0, "notnull"},
2524 { OP_ColumnName, 4, 0, "dflt_value"},
2525 };
2526 int i;
2527 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002528 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002529 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00002530 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2531 sqliteVdbeAddOp(v, OP_String, 0, 0);
2532 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2533 sqliteVdbeAddOp(v, OP_String, 0, 0);
2534 sqliteVdbeChangeP3(v, -1,
drh3c2007a2002-10-20 16:00:27 +00002535 pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00002536 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2537 sqliteVdbeAddOp(v, OP_String, 0, 0);
2538 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2539 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002540 }
2541 }
2542 }else
2543
2544 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2545 Index *pIdx;
2546 Table *pTab;
drhd24cc422003-03-27 12:51:24 +00002547 pIdx = sqliteFindIndex(db, zRight, 0);
drhdde85d92003-03-01 19:45:34 +00002548 if( pIdx ){
drh382c0242001-10-06 16:33:02 +00002549 static VdbeOp tableInfoPreface[] = {
drh382c0242001-10-06 16:33:02 +00002550 { OP_ColumnName, 0, 0, "seqno"},
2551 { OP_ColumnName, 1, 0, "cid"},
2552 { OP_ColumnName, 2, 0, "name"},
2553 };
2554 int i;
2555 pTab = pIdx->pTable;
2556 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2557 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002558 int cnum = pIdx->aiColumn[i];
2559 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2560 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2561 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002562 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002563 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2564 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002565 }
2566 }
2567 }else
2568
drh81a20f22001-10-12 17:30:04 +00002569 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2570 Index *pIdx;
2571 Table *pTab;
drhd24cc422003-03-27 12:51:24 +00002572 pTab = sqliteFindTable(db, zRight, 0);
drh81a20f22001-10-12 17:30:04 +00002573 if( pTab ){
2574 v = sqliteGetVdbe(pParse);
2575 pIdx = pTab->pIndex;
2576 }
drhdde85d92003-03-01 19:45:34 +00002577 if( pTab && pIdx ){
drh81a20f22001-10-12 17:30:04 +00002578 int i = 0;
2579 static VdbeOp indexListPreface[] = {
drh81a20f22001-10-12 17:30:04 +00002580 { OP_ColumnName, 0, 0, "seq"},
2581 { OP_ColumnName, 1, 0, "name"},
2582 { OP_ColumnName, 2, 0, "unique"},
2583 };
2584
2585 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2586 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002587 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2588 sqliteVdbeAddOp(v, OP_String, 0, 0);
2589 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002590 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002591 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002592 ++i;
2593 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002594 }
2595 }
2596 }else
2597
drhf57b14a2001-09-14 18:54:08 +00002598#ifndef NDEBUG
2599 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2600 extern void sqliteParserTrace(FILE*, char *);
2601 if( getBoolean(zRight) ){
2602 sqliteParserTrace(stdout, "parser: ");
2603 }else{
2604 sqliteParserTrace(0, 0);
2605 }
2606 }else
2607#endif
2608
drhaaab5722002-02-19 13:39:21 +00002609 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002610 static VdbeOp checkDb[] = {
2611 { OP_SetInsert, 0, 0, "2"},
drh001bbcb2003-03-19 03:14:00 +00002612 { OP_Integer, 0, 0, 0},
2613 { OP_OpenRead, 0, 2, 0},
2614 { OP_Rewind, 0, 7, 0},
2615 { OP_Column, 0, 3, 0}, /* 4 */
drh1bffb9c2002-02-03 17:37:36 +00002616 { OP_SetInsert, 0, 0, 0},
drh001bbcb2003-03-19 03:14:00 +00002617 { OP_Next, 0, 4, 0},
2618 { OP_IntegrityCk, 0, 0, 0}, /* 7 */
drh4ff6dfa2002-03-03 23:06:00 +00002619 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002620 { OP_Callback, 1, 0, 0},
drh21504322002-06-25 13:16:02 +00002621 { OP_SetInsert, 1, 0, "2"},
drh001bbcb2003-03-19 03:14:00 +00002622 { OP_Integer, 1, 0, 0},
2623 { OP_OpenRead, 1, 2, 0},
2624 { OP_Rewind, 1, 17, 0},
2625 { OP_Column, 1, 3, 0}, /* 14 */
drh21504322002-06-25 13:16:02 +00002626 { OP_SetInsert, 1, 0, 0},
drh001bbcb2003-03-19 03:14:00 +00002627 { OP_Next, 1, 14, 0},
2628 { OP_IntegrityCk, 1, 1, 0}, /* 17 */
drh21504322002-06-25 13:16:02 +00002629 { OP_Callback, 1, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002630 };
drh1bffb9c2002-02-03 17:37:36 +00002631 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2632 }else
drh1bffb9c2002-02-03 17:37:36 +00002633
drhf57b3392001-10-08 13:22:32 +00002634 {}
2635 sqliteFree(zLeft);
2636 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002637}