blob: 141d15f14b21e784a9c63d3e9758dab9ce4c1ebc [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**
drh0be9df02003-03-30 00:19:49 +000028** $Id: build.c,v 1.136 2003/03/30 00:19:50 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++){
drh812d7a22003-03-27 13:50:00 +0000120 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
121 if( zDatabase!=0 && sqliteStrICmp(zDatabase, db->aDb[j].zName) ) continue;
122 p = sqliteHashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000123 if( p ) break;
124 }
drh74e24cd2002-01-09 03:19:59 +0000125 return p;
drh75897232000-05-29 14:26:00 +0000126}
127
128/*
drhf57b3392001-10-08 13:22:32 +0000129** Locate the in-memory structure that describes
130** a particular index given the name of that index.
131** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000132*/
drhd24cc422003-03-27 12:51:24 +0000133Index *sqliteFindIndex(sqlite *db, const char *zName, const char *zDb){
134 Index *p = 0;
135 int i;
136 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000137 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
138 if( zDb && sqliteStrICmp(zDb, db->aDb[j].zName) ) continue;
139 p = sqliteHashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000140 if( p ) break;
141 }
drh74e24cd2002-01-09 03:19:59 +0000142 return p;
drh75897232000-05-29 14:26:00 +0000143}
144
145/*
146** Remove the given index from the index hash table, and free
147** its memory structures.
148**
drhd229ca92002-01-09 13:30:41 +0000149** The index is removed from the database hash tables but
150** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000151** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000152*/
drh74e24cd2002-01-09 03:19:59 +0000153static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000154 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000155
drhd229ca92002-01-09 13:30:41 +0000156 assert( db!=0 && p->zName!=0 );
drhd24cc422003-03-27 12:51:24 +0000157 pOld = sqliteHashInsert(&db->aDb[p->iDb].idxHash, p->zName,
158 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000159 if( pOld!=0 && pOld!=p ){
drhd24cc422003-03-27 12:51:24 +0000160 sqliteHashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
161 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000162 }
drh74e24cd2002-01-09 03:19:59 +0000163 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000164}
165
166/*
drhbeae3192001-09-22 18:12:08 +0000167** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000168** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000169** structures.
170*/
drh6d4abfb2001-10-22 02:58:08 +0000171void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000172 if( pIndex->pTable->pIndex==pIndex ){
173 pIndex->pTable->pIndex = pIndex->pNext;
174 }else{
175 Index *p;
176 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
177 if( p && p->pNext==pIndex ){
178 p->pNext = pIndex->pNext;
179 }
180 }
181 sqliteDeleteIndex(db, pIndex);
182}
183
184/*
drhe0bc4042002-06-25 01:09:11 +0000185** Erase all schema information from the in-memory hash tables of
186** database connection. This routine is called to reclaim memory
187** before the connection closes. It is also called during a rollback
188** if there were schema changes during the transaction.
drh74e24cd2002-01-09 03:19:59 +0000189*/
drhe0bc4042002-06-25 01:09:11 +0000190void sqliteResetInternalSchema(sqlite *db){
191 HashElem *pElem;
192 Hash temp1;
193 Hash temp2;
drhd24cc422003-03-27 12:51:24 +0000194 int i;
drhe0bc4042002-06-25 01:09:11 +0000195
drhd24cc422003-03-27 12:51:24 +0000196 for(i=0; i<db->nDb; i++){
197 Db *pDb = &db->aDb[i];
198 temp1 = pDb->tblHash;
199 temp2 = pDb->trigHash;
200 sqliteHashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
201 sqliteHashClear(&pDb->aFKey);
202 sqliteHashClear(&pDb->idxHash);
203 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
204 Trigger *pTrigger = sqliteHashData(pElem);
205 sqliteDeleteTrigger(pTrigger);
206 }
207 sqliteHashClear(&temp2);
208 sqliteHashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
209 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
210 Table *pTab = sqliteHashData(pElem);
211 sqliteDeleteTable(db, pTab);
212 }
213 sqliteHashClear(&temp1);
drh74e24cd2002-01-09 03:19:59 +0000214 }
drhe0bc4042002-06-25 01:09:11 +0000215 db->flags &= ~(SQLITE_Initialized|SQLITE_InternChanges);
216}
217
218/*
219** This routine is called whenever a rollback occurs. If there were
220** schema changes during the transaction, then we have to reset the
221** internal hash tables and reload them from disk.
222*/
223void sqliteRollbackInternalChanges(sqlite *db){
224 if( db->flags & SQLITE_InternChanges ){
225 sqliteResetInternalSchema(db);
226 }
227}
228
229/*
230** This routine is called when a commit occurs.
231*/
232void sqliteCommitInternalChanges(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000233 db->aDb[0].schema_cookie = db->next_cookie;
drhe0bc4042002-06-25 01:09:11 +0000234 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000235}
236
237/*
drh75897232000-05-29 14:26:00 +0000238** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000239** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000240**
241** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000242** the table data structure from the hash table. Nor does it remove
243** foreign keys from the sqlite.aFKey hash table. But it does destroy
244** memory structures of the indices and foreign keys associated with
245** the table.
drhdaffd0e2001-04-11 14:28:42 +0000246**
247** Indices associated with the table are unlinked from the "db"
248** data structure if db!=NULL. If db==NULL, indices attached to
249** the table are deleted, but it is assumed they have already been
250** unlinked.
drh75897232000-05-29 14:26:00 +0000251*/
252void sqliteDeleteTable(sqlite *db, Table *pTable){
253 int i;
254 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000255 FKey *pFKey, *pNextFKey;
256
drh75897232000-05-29 14:26:00 +0000257 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000258
259 /* Delete all indices associated with this table
260 */
261 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
262 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000263 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000264 sqliteDeleteIndex(db, pIndex);
265 }
266
267 /* Delete all foreign keys associated with this table. The keys
268 ** should have already been unlinked from the db->aFKey hash table
269 */
270 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
271 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000272 assert( pTable->iDb<db->nDb );
273 assert( sqliteHashFind(&db->aDb[pTable->iDb].aFKey,
274 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000275 sqliteFree(pFKey);
276 }
277
278 /* Delete the Table structure itself.
279 */
drh75897232000-05-29 14:26:00 +0000280 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000281 sqliteFree(pTable->aCol[i].zName);
282 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000283 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000284 }
drh6e142f52000-06-08 13:36:40 +0000285 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000286 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000287 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000288 sqliteFree(pTable);
289}
290
291/*
drh5edc3122001-09-13 21:53:09 +0000292** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000293** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000294*/
drh74e24cd2002-01-09 03:19:59 +0000295static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000296 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000297 FKey *pF1, *pF2;
drhd24cc422003-03-27 12:51:24 +0000298 int i = p->iDb;
drhd229ca92002-01-09 13:30:41 +0000299 assert( db!=0 );
drhd24cc422003-03-27 12:51:24 +0000300 pOld = sqliteHashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000301 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000302 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
303 int nTo = strlen(pF1->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +0000304 pF2 = sqliteHashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
drhc2eef3b2002-08-31 18:53:06 +0000305 if( pF2==pF1 ){
drhd24cc422003-03-27 12:51:24 +0000306 sqliteHashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
drhc2eef3b2002-08-31 18:53:06 +0000307 }else{
308 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
309 if( pF2 ){
310 pF2->pNextTo = pF1->pNextTo;
311 }
312 }
313 }
drh74e24cd2002-01-09 03:19:59 +0000314 sqliteDeleteTable(db, p);
315}
316
317/*
drh1ccde152000-06-17 13:12:39 +0000318** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000319**
320** Space to hold the name is obtained from sqliteMalloc() and must
321** be freed by the calling function.
322*/
drhcce7d172000-05-31 15:34:51 +0000323char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000324 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000325 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000326 return zName;
327}
328
329/*
drhe0bc4042002-06-25 01:09:11 +0000330** Generate code to open the appropriate master table. The table
331** opened will be SQLITE_MASTER for persistent tables and
332** SQLITE_TEMP_MASTER for temporary tables. The table is opened
333** on cursor 0.
334*/
335void sqliteOpenMasterTable(Vdbe *v, int isTemp){
drh001bbcb2003-03-19 03:14:00 +0000336 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
337 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
drhe0bc4042002-06-25 01:09:11 +0000338}
339
340/*
drh75897232000-05-29 14:26:00 +0000341** Begin constructing a new table representation in memory. This is
342** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000343** to a CREATE TABLE statement. In particular, this routine is called
344** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000345** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000346** flag is true if the table should be stored in the auxiliary database
347** file instead of in the main database file. This is normally the case
348** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000349** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000350**
drhf57b3392001-10-08 13:22:32 +0000351** The new table record is initialized and put in pParse->pNewTable.
352** As more of the CREATE TABLE statement is parsed, additional action
353** routines will be called to add more information to this record.
354** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
355** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000356*/
drhe5f9c642003-01-13 23:27:31 +0000357void sqliteStartTable(
358 Parse *pParse, /* Parser context */
359 Token *pStart, /* The "CREATE" token */
360 Token *pName, /* Name of table or view to create */
361 int isTemp, /* True if this is a TEMP table */
362 int isView /* True if this is a VIEW */
363){
drh75897232000-05-29 14:26:00 +0000364 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000365 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000366 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000367 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000368 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000369
370 pParse->sFirstToken = *pStart;
371 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000372 if( zName==0 ) return;
drhd24cc422003-03-27 12:51:24 +0000373 if( pParse->iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000374#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000375 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000376 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drh77ad4e42003-01-14 02:49:27 +0000377 sqliteFree(zName);
drhed6c8672003-01-12 18:02:16 +0000378 return;
379 }
drhe5f9c642003-01-13 23:27:31 +0000380 {
381 int code;
382 if( isView ){
383 if( isTemp ){
384 code = SQLITE_CREATE_TEMP_VIEW;
385 }else{
386 code = SQLITE_CREATE_VIEW;
387 }
388 }else{
389 if( isTemp ){
390 code = SQLITE_CREATE_TEMP_TABLE;
391 }else{
392 code = SQLITE_CREATE_TABLE;
393 }
394 }
395 if( sqliteAuthCheck(pParse, code, zName, 0) ){
drh77ad4e42003-01-14 02:49:27 +0000396 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000397 return;
398 }
399 }
400#endif
401
drhf57b3392001-10-08 13:22:32 +0000402
403 /* Before trying to create a temporary table, make sure the Btree for
404 ** holding temporary tables is open.
405 */
drhd24cc422003-03-27 12:51:24 +0000406 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
drh001bbcb2003-03-19 03:14:00 +0000407 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000408 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000409 sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
drhf57b3392001-10-08 13:22:32 +0000410 "file for storing temporary tables", 0);
411 pParse->nErr++;
412 return;
413 }
414 if( db->flags & SQLITE_InTrans ){
drh001bbcb2003-03-19 03:14:00 +0000415 rc = sqliteBtreeBeginTrans(db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000416 if( rc!=SQLITE_OK ){
417 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000418 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000419 pParse->nErr++;
420 return;
421 }
422 }
423 }
424
425 /* Make sure the new table name does not collide with an existing
426 ** index or table name. Issue an error message if it does.
427 **
428 ** If we are re-reading the sqlite_master table because of a schema
429 ** change and a new permanent table is found whose name collides with
drhd24cc422003-03-27 12:51:24 +0000430 ** an existing temporary table, that is not an error.
drhf57b3392001-10-08 13:22:32 +0000431 */
drhd24cc422003-03-27 12:51:24 +0000432 pTable = sqliteFindTable(db, zName, 0);
433 if( pTable!=0 && (pTable->iDb==isTemp || !pParse->initFlag) ){
434 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
435 " already exists", 0, 0);
436 sqliteFree(zName);
437 pParse->nErr++;
438 return;
drh75897232000-05-29 14:26:00 +0000439 }
drhd24cc422003-03-27 12:51:24 +0000440 if( (pIdx = sqliteFindIndex(db, zName, 0))!=0 &&
441 (pIdx->iDb==0 || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000442 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
443 zName, 0);
drh75897232000-05-29 14:26:00 +0000444 sqliteFree(zName);
445 pParse->nErr++;
446 return;
447 }
448 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000449 if( pTable==0 ){
450 sqliteFree(zName);
451 return;
452 }
drh75897232000-05-29 14:26:00 +0000453 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000454 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000455 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000456 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000457 pTable->pIndex = 0;
drhd24cc422003-03-27 12:51:24 +0000458 pTable->iDb = isTemp ? 1 : pParse->iDb;
drhbe0072d2001-09-13 14:46:09 +0000459 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000460 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000461
462 /* Begin generating the code that will insert the table record into
463 ** the SQLITE_MASTER table. Note in particular that we must go ahead
464 ** and allocate the record number for the table entry now. Before any
465 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
466 ** indices to be created and the table record must come before the
467 ** indices. Hence, the record number for the table must be allocated
468 ** now.
469 */
drhadbca9c2001-09-27 15:11:53 +0000470 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drhcabb0812002-09-14 13:47:32 +0000471 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000472 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000473 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
474 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000475 }
drhe0bc4042002-06-25 01:09:11 +0000476 sqliteOpenMasterTable(v, isTemp);
477 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
478 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
479 sqliteVdbeAddOp(v, OP_String, 0, 0);
480 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000481 }
drh75897232000-05-29 14:26:00 +0000482}
483
484/*
485** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000486**
487** The parser calls this routine once for each column declaration
488** in a CREATE TABLE statement. sqliteStartTable() gets called
489** first to get things going. Then this routine is called for each
490** column.
drh75897232000-05-29 14:26:00 +0000491*/
492void sqliteAddColumn(Parse *pParse, Token *pName){
493 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000494 int i;
495 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000496 Column *pCol;
drh75897232000-05-29 14:26:00 +0000497 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000498 sqliteSetNString(&z, pName->z, pName->n, 0);
499 if( z==0 ) return;
500 sqliteDequote(z);
501 for(i=0; i<p->nCol; i++){
502 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
503 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
504 pParse->nErr++;
505 sqliteFree(z);
506 return;
507 }
508 }
drh75897232000-05-29 14:26:00 +0000509 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000510 Column *aNew;
511 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
512 if( aNew==0 ) return;
513 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000514 }
drhc9b84a12002-06-20 11:36:48 +0000515 pCol = &p->aCol[p->nCol];
516 memset(pCol, 0, sizeof(p->aCol[0]));
517 pCol->zName = z;
518 pCol->sortOrder = SQLITE_SO_NUM;
519 p->nCol++;
drh75897232000-05-29 14:26:00 +0000520}
521
522/*
drh382c0242001-10-06 16:33:02 +0000523** This routine is called by the parser while in the middle of
524** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
525** been seen on a column. This routine sets the notNull flag on
526** the column currently under construction.
527*/
drh9cfcf5d2002-01-29 18:41:24 +0000528void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000529 Table *p;
530 int i;
531 if( (p = pParse->pNewTable)==0 ) return;
532 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000533 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000534}
535
536/*
537** This routine is called by the parser while in the middle of
538** parsing a CREATE TABLE statement. The pFirst token is the first
539** token in the sequence of tokens that describe the type of the
540** column currently under construction. pLast is the last token
541** in the sequence. Use this information to construct a string
542** that contains the typename of the column and store that string
543** in zType.
544*/
545void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
546 Table *p;
547 int i, j;
548 int n;
549 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000550 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000551 if( (p = pParse->pNewTable)==0 ) return;
552 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000553 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000554 pCol = &p->aCol[i];
555 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000556 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000557 sqliteSetNString(pz, pFirst->z, n, 0);
558 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000559 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000560 for(i=j=0; z[i]; i++){
561 int c = z[i];
562 if( isspace(c) ) continue;
563 z[j++] = c;
564 }
565 z[j] = 0;
drh3d037a92002-08-15 01:26:09 +0000566 if( pParse->db->file_format>=4 ){
drhfcb78a42003-01-18 20:11:05 +0000567 pCol->sortOrder = sqliteCollateType(z, n);
568 }else{
569 pCol->sortOrder = SQLITE_SO_NUM;
drhc9b84a12002-06-20 11:36:48 +0000570 }
drh382c0242001-10-06 16:33:02 +0000571}
572
573/*
drh7020f652000-06-03 18:06:52 +0000574** The given token is the default value for the last column added to
575** the table currently under construction. If "minusFlag" is true, it
576** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000577**
578** This routine is called by the parser while in the middle of
579** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000580*/
581void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
582 Table *p;
583 int i;
584 char **pz;
585 if( (p = pParse->pNewTable)==0 ) return;
586 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000587 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000588 pz = &p->aCol[i].zDflt;
589 if( minusFlag ){
590 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
591 }else{
592 sqliteSetNString(pz, pVal->z, pVal->n, 0);
593 }
594 sqliteDequote(*pz);
595}
596
597/*
drh4a324312001-12-21 14:30:42 +0000598** Designate the PRIMARY KEY for the table. pList is a list of names
599** of columns that form the primary key. If pList is NULL, then the
600** most recently added column of the table is the primary key.
601**
602** A table can have at most one primary key. If the table already has
603** a primary key (and this is the second primary key) then create an
604** error.
605**
606** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
607** then we will try to use that column as the row id. (Exception:
608** For backwards compatibility with older databases, do not do this
609** if the file format version number is less than 1.) Set the Table.iPKey
610** field of the table under construction to be the index of the
611** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
612** no INTEGER PRIMARY KEY.
613**
614** If the key is not an INTEGER PRIMARY KEY, then create a unique
615** index for the key. No index is created for INTEGER PRIMARY KEYs.
616*/
drh9cfcf5d2002-01-29 18:41:24 +0000617void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000618 Table *pTab = pParse->pNewTable;
619 char *zType = 0;
620 int iCol = -1;
drhe0194f22003-02-26 13:52:51 +0000621 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000622 if( pTab->hasPrimKey ){
623 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
624 "\" has more than one primary key", 0);
625 pParse->nErr++;
drhe0194f22003-02-26 13:52:51 +0000626 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000627 }
628 pTab->hasPrimKey = 1;
629 if( pList==0 ){
630 iCol = pTab->nCol - 1;
631 }else if( pList->nId==1 ){
632 for(iCol=0; iCol<pTab->nCol; iCol++){
633 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
634 }
635 }
636 if( iCol>=0 && iCol<pTab->nCol ){
637 zType = pTab->aCol[iCol].zType;
638 }
639 if( pParse->db->file_format>=1 &&
640 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
641 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000642 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000643 }else{
drhd24cc422003-03-27 12:51:24 +0000644 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000645 pList = 0;
drh4a324312001-12-21 14:30:42 +0000646 }
drhe0194f22003-02-26 13:52:51 +0000647
648primary_key_exit:
649 sqliteIdListDelete(pList);
650 return;
drh4a324312001-12-21 14:30:42 +0000651}
652
653/*
drhfcb78a42003-01-18 20:11:05 +0000654** Return the appropriate collating type given a type name.
655**
656** The collation type is text (SQLITE_SO_TEXT) if the type
657** name contains the character stream "text" or "blob" or
658** "clob". Any other type name is collated as numeric
659** (SQLITE_SO_NUM).
drh8e2ca022002-06-17 17:07:19 +0000660*/
drhfcb78a42003-01-18 20:11:05 +0000661int sqliteCollateType(const char *zType, int nType){
662 int i;
drhfcb78a42003-01-18 20:11:05 +0000663 for(i=0; i<nType-1; i++){
664 switch( zType[i] ){
665 case 'b':
666 case 'B': {
667 if( i<nType-3 && sqliteStrNICmp(&zType[i],"blob",4)==0 ){
668 return SQLITE_SO_TEXT;
669 }
670 break;
671 }
672 case 'c':
673 case 'C': {
674 if( i<nType-3 && (sqliteStrNICmp(&zType[i],"char",4)==0 ||
675 sqliteStrNICmp(&zType[i],"clob",4)==0)
676 ){
677 return SQLITE_SO_TEXT;
678 }
679 break;
680 }
681 case 'x':
682 case 'X': {
683 if( i>=2 && sqliteStrNICmp(&zType[i-2],"text",4)==0 ){
684 return SQLITE_SO_TEXT;
685 }
686 break;
687 }
688 default: {
689 break;
690 }
691 }
drh8e2ca022002-06-17 17:07:19 +0000692 }
drhfcb78a42003-01-18 20:11:05 +0000693 return SQLITE_SO_NUM;
drh8e2ca022002-06-17 17:07:19 +0000694}
695
696/*
697** This routine is called by the parser while in the middle of
698** parsing a CREATE TABLE statement. A "COLLATE" clause has
699** been seen on a column. This routine sets the Column.sortOrder on
700** the column currently under construction.
701*/
702void sqliteAddCollateType(Parse *pParse, int collType){
703 Table *p;
704 int i;
705 if( (p = pParse->pNewTable)==0 ) return;
706 i = p->nCol-1;
707 if( i>=0 ) p->aCol[i].sortOrder = collType;
708}
709
710/*
drh50e5dad2001-09-15 00:57:28 +0000711** Come up with a new random value for the schema cookie. Make sure
712** the new value is different from the old.
713**
714** The schema cookie is used to determine when the schema for the
715** database changes. After each schema change, the cookie value
716** changes. When a process first reads the schema it records the
717** cookie. Thereafter, whenever it goes to access the database,
718** it checks the cookie to make sure the schema has not changed
719** since it was last read.
720**
721** This plan is not completely bullet-proof. It is possible for
722** the schema to change multiple times and for the cookie to be
723** set back to prior value. But schema changes are infrequent
724** and the probability of hitting the same cookie value is only
725** 1 chance in 2^32. So we're safe enough.
726*/
drhe0bc4042002-06-25 01:09:11 +0000727void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh001bbcb2003-03-19 03:14:00 +0000728 if( db->next_cookie==db->aDb[0].schema_cookie ){
729 db->next_cookie = db->aDb[0].schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000730 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000731 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
732 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000733 }
734}
735
736/*
drh969fa7c2002-02-18 18:30:32 +0000737** Measure the number of characters needed to output the given
738** identifier. The number returned includes any quotes used
739** but does not include the null terminator.
740*/
741static int identLength(const char *z){
742 int n;
drh17f71932002-02-21 12:01:27 +0000743 int needQuote = 0;
744 for(n=0; *z; n++, z++){
745 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000746 }
drh17f71932002-02-21 12:01:27 +0000747 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000748}
749
750/*
751** Write an identifier onto the end of the given string. Add
752** quote characters as needed.
753*/
754static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000755 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000756 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000757 for(j=0; zIdent[j]; j++){
758 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
759 }
760 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
761 || sqliteKeywordCode(zIdent, j)!=TK_ID;
762 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000763 for(j=0; zIdent[j]; j++){
764 z[i++] = zIdent[j];
765 if( zIdent[j]=='\'' ) z[i++] = '\'';
766 }
drh17f71932002-02-21 12:01:27 +0000767 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000768 z[i] = 0;
769 *pIdx = i;
770}
771
772/*
773** Generate a CREATE TABLE statement appropriate for the given
774** table. Memory to hold the text of the statement is obtained
775** from sqliteMalloc() and must be freed by the calling function.
776*/
777static char *createTableStmt(Table *p){
778 int i, k, n;
779 char *zStmt;
780 char *zSep, *zSep2, *zEnd;
781 n = 0;
782 for(i=0; i<p->nCol; i++){
783 n += identLength(p->aCol[i].zName);
784 }
785 n += identLength(p->zName);
786 if( n<40 ){
787 zSep = "";
788 zSep2 = ",";
789 zEnd = ")";
790 }else{
791 zSep = "\n ";
792 zSep2 = ",\n ";
793 zEnd = "\n)";
794 }
drhe0bc4042002-06-25 01:09:11 +0000795 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +0000796 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +0000797 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +0000798 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000799 k = strlen(zStmt);
800 identPut(zStmt, &k, p->zName);
801 zStmt[k++] = '(';
802 for(i=0; i<p->nCol; i++){
803 strcpy(&zStmt[k], zSep);
804 k += strlen(&zStmt[k]);
805 zSep = zSep2;
806 identPut(zStmt, &k, p->aCol[i].zName);
807 }
808 strcpy(&zStmt[k], zEnd);
809 return zStmt;
810}
811
812/*
drh75897232000-05-29 14:26:00 +0000813** This routine is called to report the final ")" that terminates
814** a CREATE TABLE statement.
815**
drhf57b3392001-10-08 13:22:32 +0000816** The table structure that other action routines have been building
817** is added to the internal hash tables, assuming no errors have
818** occurred.
drh75897232000-05-29 14:26:00 +0000819**
drh1ccde152000-06-17 13:12:39 +0000820** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000821** unless this is a temporary table or initFlag==1. When initFlag==1,
822** it means we are reading the sqlite_master table because we just
823** connected to the database or because the sqlite_master table has
824** recently changes, so the entry for this table already exists in
825** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000826**
827** If the pSelect argument is not NULL, it means that this routine
828** was called to create a table generated from a
829** "CREATE TABLE ... AS SELECT ..." statement. The column names of
830** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000831*/
drh969fa7c2002-02-18 18:30:32 +0000832void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000833 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000834 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000835
drh969fa7c2002-02-18 18:30:32 +0000836 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000837 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000838 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000839
drh969fa7c2002-02-18 18:30:32 +0000840 /* If the table is generated from a SELECT, then construct the
841 ** list of columns and the text of the table.
842 */
843 if( pSelect ){
844 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000845 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000846 assert( p->aCol==0 );
847 p->nCol = pSelTab->nCol;
848 p->aCol = pSelTab->aCol;
849 pSelTab->nCol = 0;
850 pSelTab->aCol = 0;
851 sqliteDeleteTable(0, pSelTab);
852 }
853
drhd78eeee2001-09-13 16:18:53 +0000854 /* If the initFlag is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000855 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
856 ** So do not write to the disk again. Extract the root page number
857 ** for the table from the pParse->newTnum field. (The page number
858 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000859 */
860 if( pParse->initFlag ){
861 p->tnum = pParse->newTnum;
862 }
863
drhe3c41372001-09-17 20:25:58 +0000864 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000865 ** in the SQLITE_MASTER table of the database. The record number
866 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000867 **
drhe0bc4042002-06-25 01:09:11 +0000868 ** If this is a TEMPORARY table, write the entry into the auxiliary
869 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000870 */
871 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000872 int n;
drhd8bc7082000-06-07 23:51:50 +0000873 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000874
drhd8bc7082000-06-07 23:51:50 +0000875 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000876 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000877 if( p->pSelect==0 ){
878 /* A regular table */
drhd24cc422003-03-27 12:51:24 +0000879 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->iDb);
drh4ff6dfa2002-03-03 23:06:00 +0000880 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
881 }else{
882 /* A view */
883 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
884 }
drh969fa7c2002-02-18 18:30:32 +0000885 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000886 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
887 sqliteVdbeAddOp(v, OP_String, 0, 0);
888 if( p->pSelect==0 ){
889 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
890 }else{
891 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000892 }
drhe0bc4042002-06-25 01:09:11 +0000893 sqliteVdbeAddOp(v, OP_String, 0, 0);
894 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
895 sqliteVdbeAddOp(v, OP_String, 0, 0);
896 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
897 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
898 sqliteVdbeAddOp(v, OP_String, 0, 0);
899 if( pSelect ){
900 char *z = createTableStmt(p);
901 n = z ? strlen(z) : 0;
902 sqliteVdbeChangeP3(v, -1, z, n);
903 sqliteFree(z);
904 }else{
905 assert( pEnd!=0 );
906 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
907 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
908 }
909 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
910 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhd24cc422003-03-27 12:51:24 +0000911 if( !p->iDb ){
drhe0bc4042002-06-25 01:09:11 +0000912 sqliteChangeCookie(db, v);
913 }
914 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000915 if( pSelect ){
drhd24cc422003-03-27 12:51:24 +0000916 sqliteVdbeAddOp(v, OP_Integer, p->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000917 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh969fa7c2002-02-18 18:30:32 +0000918 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000919 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000920 }
drh1c928532002-01-31 15:54:21 +0000921 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000922 }
drh17e9e292003-02-01 13:53:28 +0000923
924 /* Add the table to the in-memory representation of the database.
925 */
drhd24cc422003-03-27 12:51:24 +0000926 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +0000927 Table *pOld;
928 FKey *pFKey;
drhd24cc422003-03-27 12:51:24 +0000929 pOld = sqliteHashInsert(&db->aDb[p->iDb].tblHash,
930 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +0000931 if( pOld ){
932 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
933 return;
934 }
935 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
936 int nTo = strlen(pFKey->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +0000937 pFKey->pNextTo = sqliteHashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
938 sqliteHashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +0000939 }
940 pParse->pNewTable = 0;
941 db->nTable++;
942 db->flags |= SQLITE_InternChanges;
943 }
drh75897232000-05-29 14:26:00 +0000944}
945
946/*
drha76b5df2002-02-23 02:32:10 +0000947** The parser calls this routine in order to create a new VIEW
948*/
949void sqliteCreateView(
950 Parse *pParse, /* The parsing context */
951 Token *pBegin, /* The CREATE token that begins the statement */
952 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +0000953 Select *pSelect, /* A SELECT statement that will become the new view */
954 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +0000955){
drha76b5df2002-02-23 02:32:10 +0000956 Table *p;
drh4b59ab52002-08-24 18:24:51 +0000957 int n;
drh4ff6dfa2002-03-03 23:06:00 +0000958 const char *z;
drh4b59ab52002-08-24 18:24:51 +0000959 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000960
drhe5f9c642003-01-13 23:27:31 +0000961 sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +0000962 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +0000963 if( p==0 || pParse->nErr ){
drh417be792002-03-03 18:59:40 +0000964 sqliteSelectDelete(pSelect);
965 return;
966 }
drh174b6192002-12-03 02:22:52 +0000967
drh4b59ab52002-08-24 18:24:51 +0000968 /* Make a copy of the entire SELECT statement that defines the view.
969 ** This will force all the Expr.token.z values to be dynamically
970 ** allocated rather than point to the input string - which means that
971 ** they will persist after the current sqlite_exec() call returns.
972 */
973 p->pSelect = sqliteSelectDup(pSelect);
974 sqliteSelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +0000975 if( !pParse->initFlag ){
drh4b59ab52002-08-24 18:24:51 +0000976 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +0000977 }
drh4b59ab52002-08-24 18:24:51 +0000978
979 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
980 ** the end.
981 */
drha76b5df2002-02-23 02:32:10 +0000982 sEnd = pParse->sLastToken;
983 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
984 sEnd.z += sEnd.n;
985 }
986 sEnd.n = 0;
987 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000988 z = pBegin->z;
989 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
990 sEnd.z = &z[n-1];
991 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +0000992
993 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
994 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +0000995 return;
drh417be792002-03-03 18:59:40 +0000996}
drha76b5df2002-02-23 02:32:10 +0000997
drh417be792002-03-03 18:59:40 +0000998/*
999** The Table structure pTable is really a VIEW. Fill in the names of
1000** the columns of the view in the pTable structure. Return the number
1001** of errors. If an error is seen leave an error message in pPare->zErrMsg.
1002*/
1003int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
1004 ExprList *pEList;
1005 Select *pSel;
1006 Table *pSelTab;
1007 int nErr = 0;
1008
1009 assert( pTable );
1010
1011 /* A positive nCol means the columns names for this view are
1012 ** already known.
1013 */
1014 if( pTable->nCol>0 ) return 0;
1015
1016 /* A negative nCol is a special marker meaning that we are currently
1017 ** trying to compute the column names. If we enter this routine with
1018 ** a negative nCol, it means two or more views form a loop, like this:
1019 **
1020 ** CREATE VIEW one AS SELECT * FROM two;
1021 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001022 **
1023 ** Actually, this error is caught previously and so the following test
1024 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001025 */
1026 if( pTable->nCol<0 ){
1027 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
1028 " is circularly defined", 0);
1029 pParse->nErr++;
1030 return 1;
1031 }
1032
1033 /* If we get this far, it means we need to compute the table names.
1034 */
1035 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1036 pSel = pTable->pSelect;
1037
1038 /* Note that the call to sqliteResultSetOfSelect() will expand any
1039 ** "*" elements in this list. But we will need to restore the list
1040 ** back to its original configuration afterwards, so we save a copy of
1041 ** the original in pEList.
1042 */
1043 pEList = pSel->pEList;
1044 pSel->pEList = sqliteExprListDup(pEList);
1045 if( pSel->pEList==0 ){
1046 pSel->pEList = pEList;
1047 return 1; /* Malloc failed */
1048 }
1049 pTable->nCol = -1;
1050 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
1051 if( pSelTab ){
1052 assert( pTable->aCol==0 );
1053 pTable->nCol = pSelTab->nCol;
1054 pTable->aCol = pSelTab->aCol;
1055 pSelTab->nCol = 0;
1056 pSelTab->aCol = 0;
1057 sqliteDeleteTable(0, pSelTab);
drhd24cc422003-03-27 12:51:24 +00001058 pParse->db->aDb[pTable->iDb].flags |= SQLITE_UnresetViews;
drh417be792002-03-03 18:59:40 +00001059 }else{
1060 pTable->nCol = 0;
1061 nErr++;
1062 }
1063 sqliteSelectUnbind(pSel);
1064 sqliteExprListDelete(pSel->pEList);
1065 pSel->pEList = pEList;
1066 return nErr;
1067}
1068
1069/*
1070** Clear the column names from the VIEW pTable.
1071**
1072** This routine is called whenever any other table or view is modified.
1073** The view passed into this routine might depend directly or indirectly
1074** on the modified or deleted table so we need to clear the old column
1075** names so that they will be recomputed.
1076*/
1077static void sqliteViewResetColumnNames(Table *pTable){
1078 int i;
1079 if( pTable==0 || pTable->pSelect==0 ) return;
1080 if( pTable->nCol==0 ) return;
1081 for(i=0; i<pTable->nCol; i++){
1082 sqliteFree(pTable->aCol[i].zName);
1083 sqliteFree(pTable->aCol[i].zDflt);
1084 sqliteFree(pTable->aCol[i].zType);
1085 }
1086 sqliteFree(pTable->aCol);
1087 pTable->aCol = 0;
1088 pTable->nCol = 0;
1089}
1090
1091/*
1092** Clear the column names from every VIEW.
1093*/
drhd24cc422003-03-27 12:51:24 +00001094static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001095 HashElem *i;
drhd24cc422003-03-27 12:51:24 +00001096 if( (db->aDb[idx].flags & SQLITE_UnresetViews)==0 ) return;
1097 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001098 Table *pTab = sqliteHashData(i);
1099 if( pTab->pSelect ){
1100 sqliteViewResetColumnNames(pTab);
1101 }
1102 }
drhd24cc422003-03-27 12:51:24 +00001103 db->aDb[idx].flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001104}
1105
1106/*
drh75897232000-05-29 14:26:00 +00001107** Given a token, look up a table with that name. If not found, leave
1108** an error for the parser to find and return NULL.
1109*/
drhcce7d172000-05-31 15:34:51 +00001110Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001111 char *zName;
1112 Table *pTab;
1113 zName = sqliteTableNameFromToken(pTok);
1114 if( zName==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001115 pTab = sqliteFindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001116 sqliteFree(zName);
1117 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001118 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1119 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001120 pParse->nErr++;
1121 }
1122 return pTab;
1123}
1124
1125/*
1126** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001127** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001128*/
drh4ff6dfa2002-03-03 23:06:00 +00001129void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001130 Table *pTable;
drh75897232000-05-29 14:26:00 +00001131 Vdbe *v;
1132 int base;
drh5edc3122001-09-13 21:53:09 +00001133 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001134 int iDb;
drh75897232000-05-29 14:26:00 +00001135
drhdaffd0e2001-04-11 14:28:42 +00001136 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001137 pTable = sqliteTableFromToken(pParse, pName);
1138 if( pTable==0 ) return;
drhd24cc422003-03-27 12:51:24 +00001139 iDb = pTable->iDb;
drhe5f9c642003-01-13 23:27:31 +00001140#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +00001141 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTable->iDb),0)){
drhed6c8672003-01-12 18:02:16 +00001142 return;
1143 }
drhe5f9c642003-01-13 23:27:31 +00001144 {
1145 int code;
1146 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001147 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001148 code = SQLITE_DROP_TEMP_VIEW;
1149 }else{
1150 code = SQLITE_DROP_VIEW;
1151 }
1152 }else{
drhd24cc422003-03-27 12:51:24 +00001153 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001154 code = SQLITE_DROP_TEMP_TABLE;
1155 }else{
1156 code = SQLITE_DROP_TABLE;
1157 }
1158 }
1159 if( sqliteAuthCheck(pParse, code, pTable->zName, 0) ){
1160 return;
1161 }
drh77ad4e42003-01-14 02:49:27 +00001162 if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0) ){
1163 return;
1164 }
drhe5f9c642003-01-13 23:27:31 +00001165 }
1166#endif
drh75897232000-05-29 14:26:00 +00001167 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001168 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1169 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001170 pParse->nErr++;
1171 return;
1172 }
drh4ff6dfa2002-03-03 23:06:00 +00001173 if( isView && pTable->pSelect==0 ){
1174 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1175 pTable->zName, 0);
1176 pParse->nErr++;
1177 return;
1178 }
1179 if( !isView && pTable->pSelect ){
1180 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1181 pTable->zName, 0);
1182 pParse->nErr++;
1183 return;
1184 }
drh75897232000-05-29 14:26:00 +00001185
drh1ccde152000-06-17 13:12:39 +00001186 /* Generate code to remove the table from the master table
1187 ** on disk.
1188 */
drhd8bc7082000-06-07 23:51:50 +00001189 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001190 if( v ){
1191 static VdbeOp dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001192 { OP_Rewind, 0, ADDR(8), 0},
1193 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001194 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001195 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001196 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001197 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001198 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001199 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001200 };
1201 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001202 Trigger *pTrigger;
drhd24cc422003-03-27 12:51:24 +00001203 sqliteBeginWriteOperation(pParse, 0, pTable->iDb);
1204 sqliteOpenMasterTable(v, pTable->iDb);
danielk1977c3f9bad2002-05-15 08:30:12 +00001205 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001206 pTrigger = pTable->pTrigger;
1207 while( pTrigger ){
drhd24cc422003-03-27 12:51:24 +00001208 SrcList *pNm;
1209 assert( pTrigger->iDb==pTable->iDb );
1210 pNm = sqliteSrcListAppend(0, 0, 0);
1211 pNm->a[0].zName = sqliteStrDup(pTrigger->name);
1212 pNm->a[0].zDatabase = sqliteStrDup(db->aDb[pTable->iDb].zName);
1213 sqliteDropTrigger(pParse, pNm, 1);
drhe0bc4042002-06-25 01:09:11 +00001214 if( pParse->explain ){
1215 pTrigger = pTrigger->pNext;
1216 }else{
1217 pTrigger = pTable->pTrigger;
1218 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001219 }
drhe0bc4042002-06-25 01:09:11 +00001220 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1221 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001222 if( !pTable->iDb ){
drhe0bc4042002-06-25 01:09:11 +00001223 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001224 }
drhe0bc4042002-06-25 01:09:11 +00001225 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001226 if( !isView ){
drhd24cc422003-03-27 12:51:24 +00001227 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001228 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drhd24cc422003-03-27 12:51:24 +00001229 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001230 }
drh5e00f6c2001-09-13 13:46:56 +00001231 }
drh1c928532002-01-31 15:54:21 +00001232 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001233 }
1234
drhe0bc4042002-06-25 01:09:11 +00001235 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001236 **
1237 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001238 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001239 */
1240 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001241 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001242 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001243 }
drhd24cc422003-03-27 12:51:24 +00001244 sqliteViewResetAll(db, iDb);
drh75897232000-05-29 14:26:00 +00001245}
1246
1247/*
drh38640e12002-07-05 21:42:36 +00001248** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1249** opcode and adds that P3 string to the most recently inserted instruction
1250** in the virtual machine. The P3 string consists of a single character
1251** for each column in the index pIdx of table pTab. If the column uses
1252** a numeric sort order, then the P3 string character corresponding to
1253** that column is 'n'. If the column uses a text sort order, then the
1254** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1255** additional information. See also the sqliteAddKeyType() routine.
1256*/
1257void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1258 char *zType;
1259 Table *pTab;
1260 int i, n;
1261 assert( pIdx!=0 && pIdx->pTable!=0 );
1262 pTab = pIdx->pTable;
1263 n = pIdx->nColumn;
drh8c1238a2003-01-02 14:43:55 +00001264 zType = sqliteMallocRaw( n+1 );
drh38640e12002-07-05 21:42:36 +00001265 if( zType==0 ) return;
1266 for(i=0; i<n; i++){
1267 int iCol = pIdx->aiColumn[i];
1268 assert( iCol>=0 && iCol<pTab->nCol );
1269 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1270 zType[i] = 't';
1271 }else{
1272 zType[i] = 'n';
1273 }
1274 }
1275 zType[n] = 0;
1276 sqliteVdbeChangeP3(v, -1, zType, n);
1277 sqliteFree(zType);
1278}
1279
1280/*
drhc2eef3b2002-08-31 18:53:06 +00001281** This routine is called to create a new foreign key on the table
1282** currently under construction. pFromCol determines which columns
1283** in the current table point to the foreign key. If pFromCol==0 then
1284** connect the key to the last column inserted. pTo is the name of
1285** the table referred to. pToCol is a list of tables in the other
1286** pTo table that the foreign key points to. flags contains all
1287** information about the conflict resolution algorithms specified
1288** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1289**
1290** An FKey structure is created and added to the table currently
1291** under construction in the pParse->pNewTable field. The new FKey
1292** is not linked into db->aFKey at this point - that does not happen
1293** until sqliteEndTable().
1294**
1295** The foreign key is set for IMMEDIATE processing. A subsequent call
1296** to sqliteDeferForeignKey() might change this to DEFERRED.
1297*/
1298void sqliteCreateForeignKey(
1299 Parse *pParse, /* Parsing context */
1300 IdList *pFromCol, /* Columns in this table that point to other table */
1301 Token *pTo, /* Name of the other table */
1302 IdList *pToCol, /* Columns in the other table */
1303 int flags /* Conflict resolution algorithms. */
1304){
1305 Table *p = pParse->pNewTable;
1306 int nByte;
1307 int i;
1308 int nCol;
1309 char *z;
1310 FKey *pFKey = 0;
1311
1312 assert( pTo!=0 );
1313 if( p==0 || pParse->nErr ) goto fk_end;
1314 if( pFromCol==0 ){
1315 int iCol = p->nCol-1;
1316 if( iCol<0 ) goto fk_end;
1317 if( pToCol && pToCol->nId!=1 ){
1318 sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1,
1319 p->aCol[iCol].zName, -1,
1320 " should reference only one column of table ", -1,
1321 pTo->z, pTo->n, 0);
1322 pParse->nErr++;
1323 goto fk_end;
1324 }
1325 nCol = 1;
1326 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
1327 sqliteSetString(&pParse->zErrMsg,
1328 "number of columns in foreign key does not match the number of "
1329 "columns in the referenced table", 0);
1330 pParse->nErr++;
1331 goto fk_end;
1332 }else{
1333 nCol = pFromCol->nId;
1334 }
1335 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1336 if( pToCol ){
1337 for(i=0; i<pToCol->nId; i++){
1338 nByte += strlen(pToCol->a[i].zName) + 1;
1339 }
1340 }
1341 pFKey = sqliteMalloc( nByte );
1342 if( pFKey==0 ) goto fk_end;
1343 pFKey->pFrom = p;
1344 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001345 z = (char*)&pFKey[1];
1346 pFKey->aCol = (struct sColMap*)z;
1347 z += sizeof(struct sColMap)*nCol;
1348 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001349 memcpy(z, pTo->z, pTo->n);
1350 z[pTo->n] = 0;
1351 z += pTo->n+1;
1352 pFKey->pNextTo = 0;
1353 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001354 if( pFromCol==0 ){
1355 pFKey->aCol[0].iFrom = p->nCol-1;
1356 }else{
1357 for(i=0; i<nCol; i++){
1358 int j;
1359 for(j=0; j<p->nCol; j++){
1360 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1361 pFKey->aCol[i].iFrom = j;
1362 break;
1363 }
1364 }
1365 if( j>=p->nCol ){
1366 sqliteSetString(&pParse->zErrMsg, "unknown column \"",
1367 pFromCol->a[i].zName, "\" in foreign key definition", 0);
1368 pParse->nErr++;
1369 goto fk_end;
1370 }
1371 }
1372 }
1373 if( pToCol ){
1374 for(i=0; i<nCol; i++){
1375 int n = strlen(pToCol->a[i].zName);
1376 pFKey->aCol[i].zCol = z;
1377 memcpy(z, pToCol->a[i].zName, n);
1378 z[n] = 0;
1379 z += n+1;
1380 }
1381 }
1382 pFKey->isDeferred = 0;
1383 pFKey->deleteConf = flags & 0xff;
1384 pFKey->updateConf = (flags >> 8 ) & 0xff;
1385 pFKey->insertConf = (flags >> 16 ) & 0xff;
1386
1387 /* Link the foreign key to the table as the last step.
1388 */
1389 p->pFKey = pFKey;
1390 pFKey = 0;
1391
1392fk_end:
1393 sqliteFree(pFKey);
1394 sqliteIdListDelete(pFromCol);
1395 sqliteIdListDelete(pToCol);
1396}
1397
1398/*
1399** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1400** clause is seen as part of a foreign key definition. The isDeferred
1401** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1402** The behavior of the most recently created foreign key is adjusted
1403** accordingly.
1404*/
1405void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1406 Table *pTab;
1407 FKey *pFKey;
1408 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1409 pFKey->isDeferred = isDeferred;
1410}
1411
1412/*
drh75897232000-05-29 14:26:00 +00001413** Create a new index for an SQL table. pIndex is the name of the index
1414** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001415** be NULL for a primary key or an index that is created to satisfy a
1416** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001417** as the table to be indexed. pParse->pNewTable is a table that is
1418** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001419**
drh382c0242001-10-06 16:33:02 +00001420** pList is a list of columns to be indexed. pList will be NULL if this
1421** is a primary key or unique-constraint on the most recent column added
1422** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001423*/
1424void sqliteCreateIndex(
1425 Parse *pParse, /* All information about this parse */
1426 Token *pName, /* Name of the index. May be NULL */
drhd24cc422003-03-27 12:51:24 +00001427 SrcList *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001428 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001429 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drhd24cc422003-03-27 12:51:24 +00001430 int isTemp, /* True if this is a temporary index */
drh75897232000-05-29 14:26:00 +00001431 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1432 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1433){
1434 Table *pTab; /* Table to be indexed */
1435 Index *pIndex; /* The index to be created */
1436 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001437 int i, j;
drhf57b3392001-10-08 13:22:32 +00001438 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001439 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001440
drhdaffd0e2001-04-11 14:28:42 +00001441 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1442
drh75897232000-05-29 14:26:00 +00001443 /*
1444 ** Find the table that is to be indexed. Return early if not found.
1445 */
1446 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001447 assert( pName!=0 );
drhd24cc422003-03-27 12:51:24 +00001448 assert( pTable->nSrc==1 );
drh812d7a22003-03-27 13:50:00 +00001449 pTab = sqliteSrcListLookup(pParse, pTable);
drh75897232000-05-29 14:26:00 +00001450 }else{
drhe3c41372001-09-17 20:25:58 +00001451 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001452 pTab = pParse->pNewTable;
1453 }
1454 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001455 if( pTab->readOnly ){
1456 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1457 " may not be indexed", 0);
1458 pParse->nErr++;
1459 goto exit_create_index;
1460 }
1461 if( !isTemp && pTab->iDb>=2 ){
drhb24fcbe2000-05-29 23:30:50 +00001462 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drhd24cc422003-03-27 12:51:24 +00001463 " may not have non-temporary indices added", 0);
drh75897232000-05-29 14:26:00 +00001464 pParse->nErr++;
1465 goto exit_create_index;
1466 }
drha76b5df2002-02-23 02:32:10 +00001467 if( pTab->pSelect ){
1468 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1469 pParse->nErr++;
1470 goto exit_create_index;
1471 }
drhd24cc422003-03-27 12:51:24 +00001472 if( pTab->iDb==1 ){
1473 isTemp = 1;
1474 }
drh75897232000-05-29 14:26:00 +00001475
drhd24cc422003-03-27 12:51:24 +00001476
1477#if 0
drhf57b3392001-10-08 13:22:32 +00001478 /* If this index is created while re-reading the schema from sqlite_master
1479 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001480 ** only mean that the table that this index is really associated with is
1481 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001482 ** Since its table has been suppressed, we need to also suppress the
1483 ** index.
1484 */
drhd24cc422003-03-27 12:51:24 +00001485 if( pParse->initFlag && !pParse->isTemp && pTab->iDb ){
drhf57b3392001-10-08 13:22:32 +00001486 goto exit_create_index;
1487 }
drhd24cc422003-03-27 12:51:24 +00001488#endif
drhf57b3392001-10-08 13:22:32 +00001489
drh75897232000-05-29 14:26:00 +00001490 /*
1491 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001492 ** index or table with the same name.
1493 **
1494 ** Exception: If we are reading the names of permanent indices from the
1495 ** sqlite_master table (because some other process changed the schema) and
1496 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001497 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001498 **
1499 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001500 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1501 ** own name.
drh75897232000-05-29 14:26:00 +00001502 */
drhd24cc422003-03-27 12:51:24 +00001503 if( pName && !pParse->initFlag ){
drhf57b3392001-10-08 13:22:32 +00001504 Index *pISameName; /* Another index with the same name */
1505 Table *pTSameName; /* A table with same name as the index */
drhd24cc422003-03-27 12:51:24 +00001506 zName = sqliteStrNDup(pName->z, pName->n);
drhe3c41372001-09-17 20:25:58 +00001507 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001508 if( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){
1509 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1510 " already exists", 0);
1511 pParse->nErr++;
1512 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001513 }
drhd24cc422003-03-27 12:51:24 +00001514 if( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){
1515 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1516 zName, 0);
1517 pParse->nErr++;
1518 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001519 }
drhd24cc422003-03-27 12:51:24 +00001520 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001521 char zBuf[30];
1522 int n;
1523 Index *pLoop;
1524 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1525 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001526 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001527 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001528 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001529 }else{
1530 zName = sqliteStrNDup(pName->z, pName->n);
drh75897232000-05-29 14:26:00 +00001531 }
1532
drhe5f9c642003-01-13 23:27:31 +00001533 /* Check for authorization to create an index.
1534 */
1535#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +00001536 assert( isTemp==0 || isTemp==1 );
1537 assert( pTab->iDb==pParse->iDb || isTemp==1 );
1538 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drhe5f9c642003-01-13 23:27:31 +00001539 goto exit_create_index;
1540 }
1541 i = SQLITE_CREATE_INDEX;
drhd24cc422003-03-27 12:51:24 +00001542 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
drhe5f9c642003-01-13 23:27:31 +00001543 if( sqliteAuthCheck(pParse, i, zName, pTab->zName) ){
1544 goto exit_create_index;
1545 }
1546#endif
1547
drh75897232000-05-29 14:26:00 +00001548 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001549 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001550 ** So create a fake list to simulate this.
1551 */
1552 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001553 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001554 nullId.n = strlen(nullId.z);
1555 pList = sqliteIdListAppend(0, &nullId);
1556 if( pList==0 ) goto exit_create_index;
1557 }
1558
1559 /*
1560 ** Allocate the index structure.
1561 */
drhdcc581c2000-05-30 13:44:19 +00001562 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001563 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001564 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001565 pIndex->aiColumn = (int*)&pIndex[1];
1566 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001567 strcpy(pIndex->zName, zName);
1568 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001569 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001570 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001571 pIndex->autoIndex = pName==0;
drhd24cc422003-03-27 12:51:24 +00001572 pIndex->iDb = isTemp ? 1 : pParse->iDb;
drh75897232000-05-29 14:26:00 +00001573
drh1ccde152000-06-17 13:12:39 +00001574 /* Scan the names of the columns of the table to be indexed and
1575 ** load the column indices into the Index structure. Report an error
1576 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001577 */
1578 for(i=0; i<pList->nId; i++){
1579 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001580 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001581 }
1582 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001583 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001584 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001585 pParse->nErr++;
1586 sqliteFree(pIndex);
1587 goto exit_create_index;
1588 }
drh967e8b72000-06-21 13:59:10 +00001589 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001590 }
1591
1592 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001593 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001594 */
drhd24cc422003-03-27 12:51:24 +00001595 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00001596 Index *p;
drhd24cc422003-03-27 12:51:24 +00001597 p = sqliteHashInsert(&db->aDb[isTemp].idxHash,
1598 pIndex->zName, strlen(zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00001599 if( p ){
1600 assert( p==pIndex ); /* Malloc must have failed */
1601 sqliteFree(pIndex);
1602 goto exit_create_index;
1603 }
drh5e00f6c2001-09-13 13:46:56 +00001604 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001605 }
drh9cfcf5d2002-01-29 18:41:24 +00001606
1607 /* When adding an index to the list of indices for a table, make
1608 ** sure all indices labeled OE_Replace come after all those labeled
1609 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1610 ** and INSERT.
1611 */
1612 if( onError!=OE_Replace || pTab->pIndex==0
1613 || pTab->pIndex->onError==OE_Replace){
1614 pIndex->pNext = pTab->pIndex;
1615 pTab->pIndex = pIndex;
1616 }else{
1617 Index *pOther = pTab->pIndex;
1618 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1619 pOther = pOther->pNext;
1620 }
1621 pIndex->pNext = pOther->pNext;
1622 pOther->pNext = pIndex;
1623 }
drh75897232000-05-29 14:26:00 +00001624
drhd78eeee2001-09-13 16:18:53 +00001625 /* If the initFlag is 1 it means we are reading the SQL off the
1626 ** "sqlite_master" table on the disk. So do not write to the disk
1627 ** again. Extract the table number from the pParse->newTnum field.
1628 */
drhadbca9c2001-09-27 15:11:53 +00001629 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001630 pIndex->tnum = pParse->newTnum;
1631 }
1632
drh75897232000-05-29 14:26:00 +00001633 /* If the initFlag is 0 then create the index on disk. This
1634 ** involves writing the index into the master table and filling in the
1635 ** index with the current table contents.
1636 **
1637 ** The initFlag is 0 when the user first enters a CREATE INDEX
1638 ** command. The initFlag is 1 when a database is opened and
1639 ** CREATE INDEX statements are read out of the master table. In
1640 ** the latter case the index already exists on disk, which is why
1641 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001642 **
1643 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001644 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1645 ** has just been created, it contains no data and the index initialization
1646 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001647 */
drhadbca9c2001-09-27 15:11:53 +00001648 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001649 int n;
drhadbca9c2001-09-27 15:11:53 +00001650 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001651 int lbl1, lbl2;
1652 int i;
drhadbca9c2001-09-27 15:11:53 +00001653 int addr;
drh75897232000-05-29 14:26:00 +00001654
drhd8bc7082000-06-07 23:51:50 +00001655 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001656 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001657 if( pTable!=0 ){
drhcabb0812002-09-14 13:47:32 +00001658 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhe0bc4042002-06-25 01:09:11 +00001659 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001660 }
drhe0bc4042002-06-25 01:09:11 +00001661 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1662 sqliteVdbeAddOp(v, OP_String, 0, 0);
1663 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1664 sqliteVdbeAddOp(v, OP_String, 0, 0);
1665 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1666 sqliteVdbeAddOp(v, OP_String, 0, 0);
1667 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001668 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1669 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001670 pIndex->tnum = 0;
1671 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001672 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00001673 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
1674 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +00001675 }
drhe0bc4042002-06-25 01:09:11 +00001676 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1677 if( pStart && pEnd ){
1678 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1679 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001680 }
drhe0bc4042002-06-25 01:09:11 +00001681 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1682 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001683 if( pTable ){
drhd24cc422003-03-27 12:51:24 +00001684 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00001685 sqliteVdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +00001686 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001687 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001688 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1689 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001690 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001691 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001692 }
drh99fcd712001-10-13 01:06:47 +00001693 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001694 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001695 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh483750b2003-01-29 18:46:51 +00001696 sqliteVdbeChangeP3(v, -1, "indexed columns are not unique", P3_STATIC);
drh6b563442001-11-07 16:48:26 +00001697 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001698 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001699 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1700 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001701 }
drhadbca9c2001-09-27 15:11:53 +00001702 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001703 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001704 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001705 }
drhe0bc4042002-06-25 01:09:11 +00001706 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001707 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001708 }
drh75897232000-05-29 14:26:00 +00001709 }
1710
drh75897232000-05-29 14:26:00 +00001711 /* Clean up before exiting */
1712exit_create_index:
1713 sqliteIdListDelete(pList);
drhd24cc422003-03-27 12:51:24 +00001714 sqliteSrcListDelete(pTable);
drh75897232000-05-29 14:26:00 +00001715 sqliteFree(zName);
1716 return;
1717}
1718
1719/*
drh74e24cd2002-01-09 03:19:59 +00001720** This routine will drop an existing named index. This routine
1721** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001722*/
drhd24cc422003-03-27 12:51:24 +00001723void sqliteDropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00001724 Index *pIndex;
drh75897232000-05-29 14:26:00 +00001725 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001726 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001727
drhdaffd0e2001-04-11 14:28:42 +00001728 if( pParse->nErr || sqlite_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00001729 assert( pName->nSrc==1 );
1730 pIndex = sqliteFindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001731 if( pIndex==0 ){
drhd24cc422003-03-27 12:51:24 +00001732 sqliteSetString(&pParse->zErrMsg, "no such index: ", pName->a[0].zName, 0);
drh75897232000-05-29 14:26:00 +00001733 pParse->nErr++;
drhd24cc422003-03-27 12:51:24 +00001734 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00001735 }
drh485b39b2002-07-13 03:11:52 +00001736 if( pIndex->autoIndex ){
1737 sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE "
1738 "or PRIMARY KEY constraint cannot be dropped", 0);
1739 pParse->nErr++;
drhd24cc422003-03-27 12:51:24 +00001740 goto exit_drop_index;
1741 }
1742 if( pIndex->iDb>1 ){
1743 sqliteSetString(&pParse->zErrMsg, "cannot alter schema of attached "
1744 "databases", 0);
1745 pParse->nErr++;
1746 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00001747 }
drhe5f9c642003-01-13 23:27:31 +00001748#ifndef SQLITE_OMIT_AUTHORIZATION
1749 {
1750 int code = SQLITE_DROP_INDEX;
1751 Table *pTab = pIndex->pTable;
drhd24cc422003-03-27 12:51:24 +00001752 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pIndex->iDb), 0) ){
1753 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001754 }
drhd24cc422003-03-27 12:51:24 +00001755 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
drh77ad4e42003-01-14 02:49:27 +00001756 if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName) ){
drhd24cc422003-03-27 12:51:24 +00001757 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001758 }
drhed6c8672003-01-12 18:02:16 +00001759 }
drhe5f9c642003-01-13 23:27:31 +00001760#endif
drh75897232000-05-29 14:26:00 +00001761
1762 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001763 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001764 if( v ){
1765 static VdbeOp dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001766 { OP_Rewind, 0, ADDR(9), 0},
1767 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001768 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001769 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001770 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001771 { OP_Eq, 0, ADDR(8), 0},
1772 { OP_Next, 0, ADDR(3), 0},
1773 { OP_Goto, 0, ADDR(9), 0},
1774 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001775 };
1776 int base;
1777
drhd24cc422003-03-27 12:51:24 +00001778 sqliteBeginWriteOperation(pParse, 0, pIndex->iDb);
1779 sqliteOpenMasterTable(v, pIndex->iDb);
drhe0bc4042002-06-25 01:09:11 +00001780 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1781 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001782 if( pIndex->iDb==0 ){
drhe0bc4042002-06-25 01:09:11 +00001783 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001784 }
drhe0bc4042002-06-25 01:09:11 +00001785 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhd24cc422003-03-27 12:51:24 +00001786 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
drh1c928532002-01-31 15:54:21 +00001787 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001788 }
1789
drhe0bc4042002-06-25 01:09:11 +00001790 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001791 */
1792 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001793 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001794 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001795 }
drhd24cc422003-03-27 12:51:24 +00001796
1797exit_drop_index:
1798 sqliteSrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001799}
1800
1801/*
drh75897232000-05-29 14:26:00 +00001802** Append a new element to the given IdList. Create a new IdList if
1803** need be.
drhdaffd0e2001-04-11 14:28:42 +00001804**
1805** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001806*/
1807IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1808 if( pList==0 ){
1809 pList = sqliteMalloc( sizeof(IdList) );
1810 if( pList==0 ) return 0;
1811 }
1812 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001813 struct IdList_item *a;
1814 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1815 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001816 sqliteIdListDelete(pList);
1817 return 0;
drh75897232000-05-29 14:26:00 +00001818 }
drh6d4abfb2001-10-22 02:58:08 +00001819 pList->a = a;
drh75897232000-05-29 14:26:00 +00001820 }
1821 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1822 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001823 char **pz = &pList->a[pList->nId].zName;
1824 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1825 if( *pz==0 ){
1826 sqliteIdListDelete(pList);
1827 return 0;
1828 }else{
1829 sqliteDequote(*pz);
1830 }
drh75897232000-05-29 14:26:00 +00001831 }
1832 pList->nId++;
1833 return pList;
1834}
1835
1836/*
drhad3cab52002-05-24 02:04:32 +00001837** Append a new table name to the given SrcList. Create a new SrcList if
1838** need be. A new entry is created in the SrcList even if pToken is NULL.
1839**
1840** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00001841**
1842** If pDatabase is not null, it means that the table has an optional
1843** database name prefix. Like this: "database.table". The pDatabase
1844** points to the table name and the pTable points to the database name.
1845** The SrcList.a[].zName field is filled with the table name which might
1846** come from pTable (if pDatabase is NULL) or from pDatabase.
1847** SrcList.a[].zDatabase is filled with the database name from pTable,
1848** or with NULL if no database is specified.
1849**
1850** In other words, if call like this:
1851**
1852** sqliteSrcListAppend(A,B,0);
1853**
1854** Then B is a table name and the database name is unspecified. If called
1855** like this:
1856**
1857** sqliteSrcListAppend(A,B,C);
1858**
1859** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00001860*/
drh113088e2003-03-20 01:16:58 +00001861SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00001862 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00001863 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00001864 if( pList==0 ) return 0;
1865 }
drh113088e2003-03-20 01:16:58 +00001866 if( (pList->nSrc & 7)==1 ){
1867 SrcList *pNew;
1868 pNew = sqliteRealloc(pList,
1869 sizeof(*pList) + (pList->nSrc+8)*sizeof(pList->a[0]) );
1870 if( pNew==0 ){
drhad3cab52002-05-24 02:04:32 +00001871 sqliteSrcListDelete(pList);
1872 return 0;
1873 }
drh113088e2003-03-20 01:16:58 +00001874 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00001875 }
1876 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00001877 if( pDatabase && pDatabase->z==0 ){
1878 pDatabase = 0;
1879 }
1880 if( pDatabase && pTable ){
1881 Token *pTemp = pDatabase;
1882 pDatabase = pTable;
1883 pTable = pTemp;
1884 }
1885 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00001886 char **pz = &pList->a[pList->nSrc].zName;
drh113088e2003-03-20 01:16:58 +00001887 sqliteSetNString(pz, pTable->z, pTable->n, 0);
1888 if( *pz==0 ){
1889 sqliteSrcListDelete(pList);
1890 return 0;
1891 }else{
1892 sqliteDequote(*pz);
1893 }
1894 }
1895 if( pDatabase ){
1896 char **pz = &pList->a[pList->nSrc].zDatabase;
1897 sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00001898 if( *pz==0 ){
1899 sqliteSrcListDelete(pList);
1900 return 0;
1901 }else{
1902 sqliteDequote(*pz);
1903 }
1904 }
1905 pList->nSrc++;
1906 return pList;
1907}
1908
1909/*
drh75897232000-05-29 14:26:00 +00001910** Add an alias to the last identifier on the given identifier list.
1911*/
drhad3cab52002-05-24 02:04:32 +00001912void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1913 if( pList && pList->nSrc>0 ){
1914 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001915 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001916 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001917 }
1918}
1919
1920/*
drhad3cab52002-05-24 02:04:32 +00001921** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001922*/
1923void sqliteIdListDelete(IdList *pList){
1924 int i;
1925 if( pList==0 ) return;
1926 for(i=0; i<pList->nId; i++){
1927 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001928 }
1929 sqliteFree(pList->a);
1930 sqliteFree(pList);
1931}
1932
1933/*
drhad2d8302002-05-24 20:31:36 +00001934** Return the index in pList of the identifier named zId. Return -1
1935** if not found.
1936*/
1937int sqliteIdListIndex(IdList *pList, const char *zName){
1938 int i;
1939 if( pList==0 ) return -1;
1940 for(i=0; i<pList->nId; i++){
1941 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1942 }
1943 return -1;
1944}
1945
1946/*
drhad3cab52002-05-24 02:04:32 +00001947** Delete an entire SrcList including all its substructure.
1948*/
1949void sqliteSrcListDelete(SrcList *pList){
1950 int i;
1951 if( pList==0 ) return;
1952 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00001953 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00001954 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001955 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001956 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001957 sqliteDeleteTable(0, pList->a[i].pTab);
1958 }
drhff78bd22002-02-27 01:47:11 +00001959 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00001960 sqliteExprDelete(pList->a[i].pOn);
1961 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00001962 }
drh75897232000-05-29 14:26:00 +00001963 sqliteFree(pList);
1964}
1965
drh982cef72000-05-30 16:27:03 +00001966/*
1967** The COPY command is for compatibility with PostgreSQL and specificially
1968** for the ability to read the output of pg_dump. The format is as
1969** follows:
1970**
1971** COPY table FROM file [USING DELIMITERS string]
1972**
1973** "table" is an existing table name. We will read lines of code from
1974** file to fill this table with data. File might be "stdin". The optional
1975** delimiter string identifies the field separators. The default is a tab.
1976*/
1977void sqliteCopy(
1978 Parse *pParse, /* The parser context */
drhd24cc422003-03-27 12:51:24 +00001979 SrcList *pTableName, /* The name of the table into which we will insert */
drh982cef72000-05-30 16:27:03 +00001980 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001981 Token *pDelimiter, /* Use this as the field delimiter */
1982 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001983){
1984 Table *pTab;
drh1c928532002-01-31 15:54:21 +00001985 int i;
drh982cef72000-05-30 16:27:03 +00001986 Vdbe *v;
1987 int addr, end;
1988 Index *pIdx;
drh77ad4e42003-01-14 02:49:27 +00001989 char *zFile = 0;
drhbe0072d2001-09-13 14:46:09 +00001990 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001991
drh77ad4e42003-01-14 02:49:27 +00001992
drhd24cc422003-03-27 12:51:24 +00001993 if( sqlite_malloc_failed ) goto copy_cleanup;
1994 assert( pTableName->nSrc==1 );
drh812d7a22003-03-27 13:50:00 +00001995 pTab = sqliteSrcListLookup(pParse, pTableName);
1996 if( pTab==0 || sqliteIsReadOnly(pParse, pTab) ) goto copy_cleanup;
drh77ad4e42003-01-14 02:49:27 +00001997 zFile = sqliteStrNDup(pFilename->z, pFilename->n);
1998 sqliteDequote(zFile);
1999 if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, zFile)
2000 || sqliteAuthCheck(pParse, SQLITE_COPY, pTab->zName, zFile) ){
drhed6c8672003-01-12 18:02:16 +00002001 goto copy_cleanup;
2002 }
drhd8bc7082000-06-07 23:51:50 +00002003 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00002004 if( v ){
drhd24cc422003-03-27 12:51:24 +00002005 sqliteBeginWriteOperation(pParse, 1, pTab->iDb==1);
drh99fcd712001-10-13 01:06:47 +00002006 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00002007 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00002008 sqliteVdbeDequoteP3(v, addr);
drhd24cc422003-03-27 12:51:24 +00002009 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00002010 sqliteVdbeAddOp(v, OP_OpenWrite, 0, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +00002011 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00002012 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhd24cc422003-03-27 12:51:24 +00002013 assert( pIdx->iDb==1 || pIdx->iDb==pTab->iDb );
2014 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00002015 sqliteVdbeAddOp(v, OP_OpenWrite, i, pIdx->tnum);
drh99fcd712001-10-13 01:06:47 +00002016 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00002017 }
drhb419a922002-01-30 16:17:23 +00002018 if( db->flags & SQLITE_CountRows ){
2019 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
2020 }
drh982cef72000-05-30 16:27:03 +00002021 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00002022 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00002023 if( pDelimiter ){
2024 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
2025 sqliteVdbeDequoteP3(v, addr);
2026 }else{
2027 sqliteVdbeChangeP3(v, addr, "\t", 1);
2028 }
drh8aff1012001-12-22 14:49:24 +00002029 if( pTab->iPKey>=0 ){
2030 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
2031 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
2032 }else{
2033 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
2034 }
drh982cef72000-05-30 16:27:03 +00002035 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00002036 if( i==pTab->iPKey ){
2037 /* The integer primary key column is filled with NULL since its
2038 ** value is always pulled from the record number */
2039 sqliteVdbeAddOp(v, OP_String, 0, 0);
2040 }else{
2041 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
2042 }
drh982cef72000-05-30 16:27:03 +00002043 }
drhb419a922002-01-30 16:17:23 +00002044 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
2045 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
2046 if( (db->flags & SQLITE_CountRows)!=0 ){
2047 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00002048 }
drh99fcd712001-10-13 01:06:47 +00002049 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
2050 sqliteVdbeResolveLabel(v, end);
2051 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00002052 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00002053 if( db->flags & SQLITE_CountRows ){
drhb419a922002-01-30 16:17:23 +00002054 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
2055 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
2056 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
2057 }
drh982cef72000-05-30 16:27:03 +00002058 }
2059
2060copy_cleanup:
drhd24cc422003-03-27 12:51:24 +00002061 sqliteSrcListDelete(pTableName);
drh77ad4e42003-01-14 02:49:27 +00002062 sqliteFree(zFile);
drh982cef72000-05-30 16:27:03 +00002063 return;
2064}
drhdce2cbe2000-05-31 02:27:49 +00002065
2066/*
2067** The non-standard VACUUM command is used to clean up the database,
2068** collapse free space, etc. It is modelled after the VACUUM command
2069** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00002070**
drh1bffb9c2002-02-03 17:37:36 +00002071** In version 1.0.x of SQLite, the VACUUM command would call
2072** gdbm_reorganize() on all the database tables. But beginning
2073** with 2.0.0, SQLite no longer uses GDBM so this command has
2074** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00002075*/
2076void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00002077 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00002078}
drhc4a3c772001-04-04 11:48:57 +00002079
2080/*
2081** Begin a transaction
2082*/
drh1c928532002-01-31 15:54:21 +00002083void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00002084 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002085
drh001bbcb2003-03-19 03:14:00 +00002086 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002087 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002088 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002089 if( db->flags & SQLITE_InTrans ){
2090 pParse->nErr++;
2091 sqliteSetString(&pParse->zErrMsg, "cannot start a transaction "
2092 "within a transaction", 0);
2093 return;
2094 }
drhcabb0812002-09-14 13:47:32 +00002095 sqliteBeginWriteOperation(pParse, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00002096 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002097 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00002098}
2099
2100/*
2101** Commit a transaction
2102*/
2103void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002104 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002105
drh001bbcb2003-03-19 03:14:00 +00002106 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002107 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002108 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002109 if( (db->flags & SQLITE_InTrans)==0 ){
2110 pParse->nErr++;
2111 sqliteSetString(&pParse->zErrMsg,
2112 "cannot commit - no transaction is active", 0);
2113 return;
2114 }
drh5e00f6c2001-09-13 13:46:56 +00002115 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002116 sqliteEndWriteOperation(pParse);
2117 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002118}
2119
2120/*
2121** Rollback a transaction
2122*/
2123void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002124 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002125 Vdbe *v;
2126
drh001bbcb2003-03-19 03:14:00 +00002127 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002128 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002129 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002130 if( (db->flags & SQLITE_InTrans)==0 ){
2131 pParse->nErr++;
2132 sqliteSetString(&pParse->zErrMsg,
2133 "cannot rollback - no transaction is active", 0);
2134 return;
2135 }
drh5e00f6c2001-09-13 13:46:56 +00002136 v = sqliteGetVdbe(pParse);
2137 if( v ){
drh99fcd712001-10-13 01:06:47 +00002138 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002139 }
drh5e00f6c2001-09-13 13:46:56 +00002140 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002141 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002142}
drhf57b14a2001-09-14 18:54:08 +00002143
2144/*
drh001bbcb2003-03-19 03:14:00 +00002145** Generate VDBE code that will verify the schema cookie for all
2146** named database files.
2147*/
2148void sqliteCodeVerifySchema(Parse *pParse){
2149 int i;
2150 sqlite *db = pParse->db;
2151 Vdbe *v = sqliteGetVdbe(pParse);
2152 for(i=0; i<db->nDb; i++){
drh113088e2003-03-20 01:16:58 +00002153 if( i==1 || db->aDb[i].pBt==0 ) continue;
drh001bbcb2003-03-19 03:14:00 +00002154 sqliteVdbeAddOp(v, OP_VerifyCookie, 0, db->aDb[i].schema_cookie);
2155 }
2156 pParse->schemaVerified = 1;
2157}
2158
2159/*
drh1c928532002-01-31 15:54:21 +00002160** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002161** might change the database.
2162**
2163** This routine starts a new transaction if we are not already within
2164** a transaction. If we are already within a transaction, then a checkpoint
2165** is set if the setCheckpoint parameter is true. A checkpoint should
2166** be set for operations that might fail (due to a constraint) part of
2167** the way through and which will need to undo some writes without having to
2168** rollback the whole transaction. For operations where all constraints
2169** can be checked before any changes are made to the database, it is never
2170** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002171**
2172** The tempOnly flag indicates that only temporary tables will be changed
2173** during this write operation. The primary database table is not
2174** write-locked. Only the temporary database file gets a write lock.
2175** Other processes can continue to read or write the primary database file.
drh1c928532002-01-31 15:54:21 +00002176*/
drhcabb0812002-09-14 13:47:32 +00002177void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){
drh663fc632002-02-02 18:49:19 +00002178 Vdbe *v;
2179 v = sqliteGetVdbe(pParse);
2180 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00002181 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00002182 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drh001bbcb2003-03-19 03:14:00 +00002183 sqliteVdbeAddOp(v, OP_Transaction, 1, 0);
drhcabb0812002-09-14 13:47:32 +00002184 if( !tempOnly ){
drh001bbcb2003-03-19 03:14:00 +00002185 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
2186 sqliteCodeVerifySchema(pParse);
drhcabb0812002-09-14 13:47:32 +00002187 }
drhc977f7f2002-05-21 11:38:11 +00002188 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00002189 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00002190 sqliteVdbeAddOp(v, OP_Checkpoint, 1, 0);
drh663fc632002-02-02 18:49:19 +00002191 }
2192}
2193
2194/*
drh1c928532002-01-31 15:54:21 +00002195** Generate code that concludes an operation that may have changed
2196** the database. This is a companion function to BeginWriteOperation().
2197** If a transaction was started, then commit it. If a checkpoint was
2198** started then commit that.
2199*/
2200void sqliteEndWriteOperation(Parse *pParse){
2201 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00002202 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00002203 v = sqliteGetVdbe(pParse);
2204 if( v==0 ) return;
2205 if( pParse->db->flags & SQLITE_InTrans ){
2206 /* Do Nothing */
2207 }else{
2208 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2209 }
2210}
2211
2212
2213/*
drhf57b14a2001-09-14 18:54:08 +00002214** Interpret the given string as a boolean value.
2215*/
2216static int getBoolean(char *z){
2217 static char *azTrue[] = { "yes", "on", "true" };
2218 int i;
2219 if( z[0]==0 ) return 0;
2220 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2221 return atoi(z);
2222 }
2223 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
2224 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
2225 }
2226 return 0;
2227}
2228
2229/*
drh973b6e32003-02-12 14:09:42 +00002230** Interpret the given string as a safety level. Return 0 for OFF,
2231** 1 for ON or NORMAL and 2 for FULL.
2232**
2233** Note that the values returned are one less that the values that
2234** should be passed into sqliteBtreeSetSafetyLevel(). The is done
2235** to support legacy SQL code. The safety level used to be boolean
2236** and older scripts may have used numbers 0 for OFF and 1 for ON.
2237*/
2238static int getSafetyLevel(char *z){
2239 static const struct {
2240 const char *zWord;
2241 int val;
2242 } aKey[] = {
2243 { "no", 0 },
2244 { "off", 0 },
2245 { "false", 0 },
2246 { "yes", 1 },
2247 { "on", 1 },
2248 { "true", 1 },
2249 { "full", 2 },
2250 };
2251 int i;
2252 if( z[0]==0 ) return 1;
2253 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2254 return atoi(z);
2255 }
2256 for(i=0; i<sizeof(aKey)/sizeof(aKey[0]); i++){
2257 if( sqliteStrICmp(z,aKey[i].zWord)==0 ) return aKey[i].val;
2258 }
2259 return 1;
2260}
2261
2262/*
drhf57b14a2001-09-14 18:54:08 +00002263** Process a pragma statement.
2264**
2265** Pragmas are of this form:
2266**
2267** PRAGMA id = value
2268**
2269** The identifier might also be a string. The value is a string, and
2270** identifier, or a number. If minusFlag is true, then the value is
2271** a number that was preceded by a minus sign.
2272*/
2273void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
2274 char *zLeft = 0;
2275 char *zRight = 0;
2276 sqlite *db = pParse->db;
drhdde85d92003-03-01 19:45:34 +00002277 Vdbe *v = sqliteGetVdbe(pParse);
2278 if( v==0 ) return;
drhf57b14a2001-09-14 18:54:08 +00002279
2280 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
2281 sqliteDequote(zLeft);
2282 if( minusFlag ){
2283 zRight = 0;
2284 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
2285 }else{
2286 zRight = sqliteStrNDup(pRight->z, pRight->n);
2287 sqliteDequote(zRight);
2288 }
drhbf0c78a2003-01-14 02:54:08 +00002289 if( sqliteAuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight) ){
2290 sqliteFree(zLeft);
2291 sqliteFree(zRight);
2292 return;
2293 }
drhf57b14a2001-09-14 18:54:08 +00002294
drhcd61c282002-03-06 22:01:34 +00002295 /*
2296 ** PRAGMA default_cache_size
2297 ** PRAGMA default_cache_size=N
2298 **
2299 ** The first form reports the current persistent setting for the
2300 ** page cache size. The value returned is the maximum number of
2301 ** pages in the page cache. The second form sets both the current
2302 ** page cache size value and the persistent page cache size value
2303 ** stored in the database file.
2304 **
2305 ** The default cache size is stored in meta-value 2 of page 1 of the
2306 ** database file. The cache size is actually the absolute value of
2307 ** this memory location. The sign of meta-value 2 determines the
2308 ** synchronous setting. A negative value means synchronous is off
2309 ** and a positive value means synchronous is on.
2310 */
2311 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00002312 static VdbeOp getCacheSize[] = {
2313 { OP_ReadCookie, 0, 2, 0},
2314 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00002315 { OP_Dup, 0, 0, 0},
2316 { OP_Integer, 0, 0, 0},
2317 { OP_Ne, 0, 6, 0},
2318 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00002319 { OP_ColumnName, 0, 0, "cache_size"},
2320 { OP_Callback, 1, 0, 0},
2321 };
drh603240c2002-03-05 01:11:12 +00002322 if( pRight->z==pLeft->z ){
2323 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2324 }else{
2325 int addr;
2326 int size = atoi(zRight);
2327 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002328 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002329 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2330 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
2331 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2332 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
2333 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
2334 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2335 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002336 db->cache_size = db->cache_size<0 ? -size : size;
drh001bbcb2003-03-19 03:14:00 +00002337 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002338 }
2339 }else
2340
drhcd61c282002-03-06 22:01:34 +00002341 /*
2342 ** PRAGMA cache_size
2343 ** PRAGMA cache_size=N
2344 **
2345 ** The first form reports the current local setting for the
2346 ** page cache size. The local setting can be different from
2347 ** the persistent cache size value that is stored in the database
2348 ** file itself. The value returned is the maximum number of
2349 ** pages in the page cache. The second form sets the local
2350 ** page cache size value. It does not change the persistent
2351 ** cache size stored on the disk so the cache size will revert
2352 ** to its default value when the database is closed and reopened.
2353 ** N should be a positive integer.
2354 */
2355 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
2356 static VdbeOp getCacheSize[] = {
drhcd61c282002-03-06 22:01:34 +00002357 { OP_ColumnName, 0, 0, "cache_size"},
2358 { OP_Callback, 1, 0, 0},
2359 };
drhcd61c282002-03-06 22:01:34 +00002360 if( pRight->z==pLeft->z ){
2361 int size = db->cache_size;;
2362 if( size<0 ) size = -size;
2363 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2364 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2365 }else{
2366 int size = atoi(zRight);
2367 if( size<0 ) size = -size;
2368 if( db->cache_size<0 ) size = -size;
2369 db->cache_size = size;
drh001bbcb2003-03-19 03:14:00 +00002370 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
drhcd61c282002-03-06 22:01:34 +00002371 }
2372 }else
2373
2374 /*
2375 ** PRAGMA default_synchronous
drh973b6e32003-02-12 14:09:42 +00002376 ** PRAGMA default_synchronous=ON|OFF|NORMAL|FULL
drhcd61c282002-03-06 22:01:34 +00002377 **
2378 ** The first form returns the persistent value of the "synchronous" setting
2379 ** that is stored in the database. This is the synchronous setting that
2380 ** is used whenever the database is opened unless overridden by a separate
2381 ** "synchronous" pragma. The second form changes the persistent and the
2382 ** local synchronous setting to the value given.
2383 **
drh973b6e32003-02-12 14:09:42 +00002384 ** If synchronous is OFF, SQLite does not attempt any fsync() systems calls
2385 ** to make sure data is committed to disk. Write operations are very fast,
2386 ** but a power failure can leave the database in an inconsistent state.
2387 ** If synchronous is ON or NORMAL, SQLite will do an fsync() system call to
2388 ** make sure data is being written to disk. The risk of corruption due to
2389 ** a power loss in this mode is negligible but non-zero. If synchronous
2390 ** is FULL, extra fsync()s occur to reduce the risk of corruption to near
2391 ** zero, but with a write performance penalty. The default mode is NORMAL.
drhcd61c282002-03-06 22:01:34 +00002392 */
2393 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00002394 static VdbeOp getSync[] = {
drh973b6e32003-02-12 14:09:42 +00002395 { OP_ColumnName, 0, 0, "synchronous"},
2396 { OP_ReadCookie, 0, 3, 0},
2397 { OP_Dup, 0, 0, 0},
2398 { OP_If, 0, 0, 0}, /* 3 */
drh603240c2002-03-05 01:11:12 +00002399 { OP_ReadCookie, 0, 2, 0},
2400 { OP_Integer, 0, 0, 0},
2401 { OP_Lt, 0, 5, 0},
2402 { OP_AddImm, 1, 0, 0},
drh603240c2002-03-05 01:11:12 +00002403 { OP_Callback, 1, 0, 0},
drh973b6e32003-02-12 14:09:42 +00002404 { OP_Halt, 0, 0, 0},
2405 { OP_AddImm, -1, 0, 0}, /* 10 */
2406 { OP_Callback, 1, 0, 0}
drh603240c2002-03-05 01:11:12 +00002407 };
drh603240c2002-03-05 01:11:12 +00002408 if( pRight->z==pLeft->z ){
drh973b6e32003-02-12 14:09:42 +00002409 int addr = sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2410 sqliteVdbeChangeP2(v, addr+3, addr+10);
drh603240c2002-03-05 01:11:12 +00002411 }else{
2412 int addr;
drhcd61c282002-03-06 22:01:34 +00002413 int size = db->cache_size;
2414 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002415 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002416 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00002417 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2418 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2419 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
2420 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00002421 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
drh973b6e32003-02-12 14:09:42 +00002422 db->safety_level = getSafetyLevel(zRight)+1;
2423 if( db->safety_level==1 ){
drh603240c2002-03-05 01:11:12 +00002424 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00002425 size = -size;
drh603240c2002-03-05 01:11:12 +00002426 }
2427 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
drh973b6e32003-02-12 14:09:42 +00002428 sqliteVdbeAddOp(v, OP_Integer, db->safety_level, 0);
2429 sqliteVdbeAddOp(v, OP_SetCookie, 0, 3);
drh603240c2002-03-05 01:11:12 +00002430 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002431 db->cache_size = size;
drh001bbcb2003-03-19 03:14:00 +00002432 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
2433 sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
drhcd61c282002-03-06 22:01:34 +00002434 }
2435 }else
2436
2437 /*
2438 ** PRAGMA synchronous
drh973b6e32003-02-12 14:09:42 +00002439 ** PRAGMA synchronous=OFF|ON|NORMAL|FULL
drhcd61c282002-03-06 22:01:34 +00002440 **
2441 ** Return or set the local value of the synchronous flag. Changing
2442 ** the local value does not make changes to the disk file and the
2443 ** default value will be restored the next time the database is
2444 ** opened.
2445 */
2446 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
2447 static VdbeOp getSync[] = {
drhcd61c282002-03-06 22:01:34 +00002448 { OP_ColumnName, 0, 0, "synchronous"},
2449 { OP_Callback, 1, 0, 0},
2450 };
drhcd61c282002-03-06 22:01:34 +00002451 if( pRight->z==pLeft->z ){
drh973b6e32003-02-12 14:09:42 +00002452 sqliteVdbeAddOp(v, OP_Integer, db->safety_level-1, 0);
drhcd61c282002-03-06 22:01:34 +00002453 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2454 }else{
2455 int size = db->cache_size;
2456 if( size<0 ) size = -size;
drh973b6e32003-02-12 14:09:42 +00002457 db->safety_level = getSafetyLevel(zRight)+1;
2458 if( db->safety_level==1 ) size = -size;
drhcd61c282002-03-06 22:01:34 +00002459 db->cache_size = size;
drh001bbcb2003-03-19 03:14:00 +00002460 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
2461 sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
drh603240c2002-03-05 01:11:12 +00002462 }
drhf57b14a2001-09-14 18:54:08 +00002463 }else
2464
danielk1977c3f9bad2002-05-15 08:30:12 +00002465 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
2466 if( getBoolean(zRight) ){
2467 always_code_trigger_setup = 1;
2468 }else{
2469 always_code_trigger_setup = 0;
2470 }
2471 }else
2472
drhf57b14a2001-09-14 18:54:08 +00002473 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
2474 if( getBoolean(zRight) ){
2475 db->flags |= SQLITE_VdbeTrace;
2476 }else{
2477 db->flags &= ~SQLITE_VdbeTrace;
2478 }
2479 }else
2480
drh382c0242001-10-06 16:33:02 +00002481 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
2482 if( getBoolean(zRight) ){
2483 db->flags |= SQLITE_FullColNames;
2484 }else{
2485 db->flags &= ~SQLITE_FullColNames;
2486 }
2487 }else
2488
drh5080aaa2002-07-11 12:18:16 +00002489 if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
2490 if( getBoolean(zRight) ){
2491 db->flags |= SQLITE_ReportTypes;
2492 }else{
2493 db->flags &= ~SQLITE_ReportTypes;
2494 }
2495 }else
2496
drhc3a64ba2001-11-22 00:01:27 +00002497 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2498 if( getBoolean(zRight) ){
2499 db->flags |= SQLITE_ResultDetails;
2500 }else{
2501 db->flags &= ~SQLITE_ResultDetails;
2502 }
2503 }else
2504
drh1bee3d72001-10-15 00:44:35 +00002505 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2506 if( getBoolean(zRight) ){
2507 db->flags |= SQLITE_CountRows;
2508 }else{
2509 db->flags &= ~SQLITE_CountRows;
2510 }
2511 }else
2512
drh6a535342001-10-19 16:44:56 +00002513 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2514 if( getBoolean(zRight) ){
2515 db->flags |= SQLITE_NullCallback;
2516 }else{
2517 db->flags &= ~SQLITE_NullCallback;
2518 }
2519 }else
2520
drh382c0242001-10-06 16:33:02 +00002521 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2522 Table *pTab;
drhd24cc422003-03-27 12:51:24 +00002523 pTab = sqliteFindTable(db, zRight, 0);
drhdde85d92003-03-01 19:45:34 +00002524 if( pTab ){
drh382c0242001-10-06 16:33:02 +00002525 static VdbeOp tableInfoPreface[] = {
drh382c0242001-10-06 16:33:02 +00002526 { OP_ColumnName, 0, 0, "cid"},
2527 { OP_ColumnName, 1, 0, "name"},
2528 { OP_ColumnName, 2, 0, "type"},
2529 { OP_ColumnName, 3, 0, "notnull"},
2530 { OP_ColumnName, 4, 0, "dflt_value"},
2531 };
2532 int i;
2533 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002534 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002535 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00002536 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2537 sqliteVdbeAddOp(v, OP_String, 0, 0);
2538 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2539 sqliteVdbeAddOp(v, OP_String, 0, 0);
2540 sqliteVdbeChangeP3(v, -1,
drh3c2007a2002-10-20 16:00:27 +00002541 pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00002542 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2543 sqliteVdbeAddOp(v, OP_String, 0, 0);
2544 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2545 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002546 }
2547 }
2548 }else
2549
2550 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2551 Index *pIdx;
2552 Table *pTab;
drhd24cc422003-03-27 12:51:24 +00002553 pIdx = sqliteFindIndex(db, zRight, 0);
drhdde85d92003-03-01 19:45:34 +00002554 if( pIdx ){
drh382c0242001-10-06 16:33:02 +00002555 static VdbeOp tableInfoPreface[] = {
drh382c0242001-10-06 16:33:02 +00002556 { OP_ColumnName, 0, 0, "seqno"},
2557 { OP_ColumnName, 1, 0, "cid"},
2558 { OP_ColumnName, 2, 0, "name"},
2559 };
2560 int i;
2561 pTab = pIdx->pTable;
2562 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2563 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002564 int cnum = pIdx->aiColumn[i];
2565 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2566 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2567 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002568 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002569 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2570 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002571 }
2572 }
2573 }else
2574
drh81a20f22001-10-12 17:30:04 +00002575 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2576 Index *pIdx;
2577 Table *pTab;
drhd24cc422003-03-27 12:51:24 +00002578 pTab = sqliteFindTable(db, zRight, 0);
drh81a20f22001-10-12 17:30:04 +00002579 if( pTab ){
2580 v = sqliteGetVdbe(pParse);
2581 pIdx = pTab->pIndex;
2582 }
drhdde85d92003-03-01 19:45:34 +00002583 if( pTab && pIdx ){
drh81a20f22001-10-12 17:30:04 +00002584 int i = 0;
2585 static VdbeOp indexListPreface[] = {
drh81a20f22001-10-12 17:30:04 +00002586 { OP_ColumnName, 0, 0, "seq"},
2587 { OP_ColumnName, 1, 0, "name"},
2588 { OP_ColumnName, 2, 0, "unique"},
2589 };
2590
2591 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2592 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002593 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2594 sqliteVdbeAddOp(v, OP_String, 0, 0);
2595 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002596 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002597 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002598 ++i;
2599 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002600 }
2601 }
2602 }else
2603
drhf57b14a2001-09-14 18:54:08 +00002604#ifndef NDEBUG
2605 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2606 extern void sqliteParserTrace(FILE*, char *);
2607 if( getBoolean(zRight) ){
2608 sqliteParserTrace(stdout, "parser: ");
2609 }else{
2610 sqliteParserTrace(0, 0);
2611 }
2612 }else
2613#endif
2614
drhaaab5722002-02-19 13:39:21 +00002615 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002616 static VdbeOp checkDb[] = {
2617 { OP_SetInsert, 0, 0, "2"},
drh001bbcb2003-03-19 03:14:00 +00002618 { OP_Integer, 0, 0, 0},
2619 { OP_OpenRead, 0, 2, 0},
2620 { OP_Rewind, 0, 7, 0},
2621 { OP_Column, 0, 3, 0}, /* 4 */
drh1bffb9c2002-02-03 17:37:36 +00002622 { OP_SetInsert, 0, 0, 0},
drh001bbcb2003-03-19 03:14:00 +00002623 { OP_Next, 0, 4, 0},
2624 { OP_IntegrityCk, 0, 0, 0}, /* 7 */
drh4ff6dfa2002-03-03 23:06:00 +00002625 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002626 { OP_Callback, 1, 0, 0},
drh21504322002-06-25 13:16:02 +00002627 { OP_SetInsert, 1, 0, "2"},
drh001bbcb2003-03-19 03:14:00 +00002628 { OP_Integer, 1, 0, 0},
2629 { OP_OpenRead, 1, 2, 0},
2630 { OP_Rewind, 1, 17, 0},
2631 { OP_Column, 1, 3, 0}, /* 14 */
drh21504322002-06-25 13:16:02 +00002632 { OP_SetInsert, 1, 0, 0},
drh001bbcb2003-03-19 03:14:00 +00002633 { OP_Next, 1, 14, 0},
2634 { OP_IntegrityCk, 1, 1, 0}, /* 17 */
drh21504322002-06-25 13:16:02 +00002635 { OP_Callback, 1, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002636 };
drh1bffb9c2002-02-03 17:37:36 +00002637 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2638 }else
drh1bffb9c2002-02-03 17:37:36 +00002639
drhf57b3392001-10-08 13:22:32 +00002640 {}
2641 sqliteFree(zLeft);
2642 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002643}