blob: ef59bb03c117c91d2d03171b972822831e789104 [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
20** creating expressions and ID lists
21** 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**
drhf57b3392001-10-08 13:22:32 +000028** $Id: build.c,v 1.45 2001/10/08 13:22:32 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/*
34** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000035** parsed and we want to execute the VDBE code to implement
36** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000037** constructed VDBE code to do the work of the SQL statement.
38** This routine just has to execute the VDBE code.
39**
40** Note that if an error occurred, it might be the case that
41** no VDBE code was generated.
42*/
43void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000044 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000045 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000046 if( sqlite_malloc_failed ) return;
drh3fc190c2001-09-14 03:24:23 +000047 if( pParse->pVdbe && pParse->nErr==0 ){
drh75897232000-05-29 14:26:00 +000048 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000049 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
50 &pParse->zErrMsg);
drh75897232000-05-29 14:26:00 +000051 }else{
drh3fc190c2001-09-14 03:24:23 +000052 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
drh75897232000-05-29 14:26:00 +000053 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000054 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000055 &pParse->zErrMsg, db->pBusyArg,
56 db->xBusyCallback);
drhecdc7532001-09-23 02:35:53 +000057 if( rc ) pParse->nErr++;
drh75897232000-05-29 14:26:00 +000058 }
59 sqliteVdbeDelete(pParse->pVdbe);
60 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000061 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000062 pParse->rc = rc;
drh50e5dad2001-09-15 00:57:28 +000063 pParse->schemaVerified = 0;
drh75897232000-05-29 14:26:00 +000064 }
65}
66
67/*
drhf57b3392001-10-08 13:22:32 +000068** Construct a new expression node and return a pointer to it. Memory
69** for this node is obtained from sqliteMalloc(). The calling function
70** is responsible for making sure the node eventually gets freed.
drh75897232000-05-29 14:26:00 +000071*/
72Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
73 Expr *pNew;
74 pNew = sqliteMalloc( sizeof(Expr) );
75 if( pNew==0 ) return 0;
76 pNew->op = op;
77 pNew->pLeft = pLeft;
78 pNew->pRight = pRight;
79 if( pToken ){
80 pNew->token = *pToken;
81 }else{
82 pNew->token.z = "";
83 pNew->token.n = 0;
84 }
drhe1b6a5b2000-07-29 13:06:59 +000085 if( pLeft && pRight ){
86 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
87 }else{
88 pNew->span = pNew->token;
89 }
drh75897232000-05-29 14:26:00 +000090 return pNew;
91}
92
93/*
drhe1b6a5b2000-07-29 13:06:59 +000094** Set the Expr.token field of the given expression to span all
95** text between the two given tokens.
96*/
97void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drhdaffd0e2001-04-11 14:28:42 +000098 if( pExpr ){
99 pExpr->span.z = pLeft->z;
100 pExpr->span.n = pRight->n + (int)pRight->z - (int)pLeft->z;
101 }
drhe1b6a5b2000-07-29 13:06:59 +0000102}
103
104/*
drh75897232000-05-29 14:26:00 +0000105** Construct a new expression node for a function with multiple
106** arguments.
107*/
108Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
109 Expr *pNew;
110 pNew = sqliteMalloc( sizeof(Expr) );
111 if( pNew==0 ) return 0;
112 pNew->op = TK_FUNCTION;
113 pNew->pList = pList;
114 if( pToken ){
115 pNew->token = *pToken;
116 }else{
117 pNew->token.z = "";
118 pNew->token.n = 0;
119 }
120 return pNew;
121}
122
123/*
124** Recursively delete an expression tree.
125*/
126void sqliteExprDelete(Expr *p){
127 if( p==0 ) return;
128 if( p->pLeft ) sqliteExprDelete(p->pLeft);
129 if( p->pRight ) sqliteExprDelete(p->pRight);
drh19a775c2000-06-05 18:54:46 +0000130 if( p->pList ) sqliteExprListDelete(p->pList);
131 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
drh75897232000-05-29 14:26:00 +0000132 sqliteFree(p);
133}
134
135/*
drhf57b3392001-10-08 13:22:32 +0000136** Locate the in-memory structure that describes
137** a particular database table given the name
drh75897232000-05-29 14:26:00 +0000138** of that table. Return NULL if not found.
139*/
140Table *sqliteFindTable(sqlite *db, char *zName){
drhafa4a022001-09-24 03:12:39 +0000141 Table *p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
142 return (p==0 || p->isDelete) ? 0 : p;
drh75897232000-05-29 14:26:00 +0000143}
144
145/*
drhf57b3392001-10-08 13:22:32 +0000146** Locate the in-memory structure that describes
147** a particular index given the name of that index.
148** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000149*/
150Index *sqliteFindIndex(sqlite *db, char *zName){
drhafa4a022001-09-24 03:12:39 +0000151 Index *p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
152 return (p==0 || p->isDelete) ? 0 : p;
drh75897232000-05-29 14:26:00 +0000153}
154
155/*
156** Remove the given index from the index hash table, and free
157** its memory structures.
158**
drhdaffd0e2001-04-11 14:28:42 +0000159** The index is removed from the database hash table if db!=NULL.
drhf57b3392001-10-08 13:22:32 +0000160** But the index is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000161** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000162*/
163static void sqliteDeleteIndex(sqlite *db, Index *pIndex){
drhdaffd0e2001-04-11 14:28:42 +0000164 if( pIndex->zName && db ){
drhbeae3192001-09-22 18:12:08 +0000165 sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(pIndex->zName)+1, 0);
drh75897232000-05-29 14:26:00 +0000166 }
167 sqliteFree(pIndex);
168}
169
170/*
drhbeae3192001-09-22 18:12:08 +0000171** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000172** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000173** structures.
174*/
175static void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
176 if( pIndex->pTable->pIndex==pIndex ){
177 pIndex->pTable->pIndex = pIndex->pNext;
178 }else{
179 Index *p;
180 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
181 if( p && p->pNext==pIndex ){
182 p->pNext = pIndex->pNext;
183 }
184 }
185 sqliteDeleteIndex(db, pIndex);
186}
187
188/*
drh75897232000-05-29 14:26:00 +0000189** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000190** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000191**
192** This routine just deletes the data structure. It does not unlink
drhd9b02572001-04-15 00:37:09 +0000193** the table data structure from the hash table. But it does destroy
drh75897232000-05-29 14:26:00 +0000194** memory structures of the indices associated with the table.
drhdaffd0e2001-04-11 14:28:42 +0000195**
196** Indices associated with the table are unlinked from the "db"
197** data structure if db!=NULL. If db==NULL, indices attached to
198** the table are deleted, but it is assumed they have already been
199** unlinked.
drh75897232000-05-29 14:26:00 +0000200*/
201void sqliteDeleteTable(sqlite *db, Table *pTable){
202 int i;
203 Index *pIndex, *pNext;
204 if( pTable==0 ) return;
205 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000206 sqliteFree(pTable->aCol[i].zName);
207 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000208 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000209 }
210 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
211 pNext = pIndex->pNext;
212 sqliteDeleteIndex(db, pIndex);
213 }
drh6e142f52000-06-08 13:36:40 +0000214 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000215 sqliteFree(pTable->aCol);
drh75897232000-05-29 14:26:00 +0000216 sqliteFree(pTable);
217}
218
219/*
drh5edc3122001-09-13 21:53:09 +0000220** Unlink the given table from the hash tables and the delete the
221** table structure and all its indices.
222*/
223static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *pTable){
224 if( pTable->zName && db ){
drhbeae3192001-09-22 18:12:08 +0000225 sqliteHashInsert(&db->tblHash, pTable->zName, strlen(pTable->zName)+1, 0);
drh5edc3122001-09-13 21:53:09 +0000226 }
227 sqliteDeleteTable(db, pTable);
228}
229
230/*
drh5e00f6c2001-09-13 13:46:56 +0000231** Check all Tables and Indexes in the internal hash table and commit
232** any additions or deletions to those hash tables.
233**
234** When executing CREATE TABLE and CREATE INDEX statements, the Table
235** and Index structures are created and added to the hash tables, but
236** the "isCommit" field is not set. This routine sets those fields.
237** When executing DROP TABLE and DROP INDEX, the "isDelete" fields of
238** Table and Index structures is set but the structures are not unlinked
239** from the hash tables nor deallocated. This routine handles that
240** deallocation.
241**
242** See also: sqliteRollbackInternalChanges()
243*/
244void sqliteCommitInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000245 Hash toDelete;
246 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000247 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drhbeae3192001-09-22 18:12:08 +0000248 sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0);
drh50e5dad2001-09-15 00:57:28 +0000249 db->schema_cookie = db->next_cookie;
drhbeae3192001-09-22 18:12:08 +0000250 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
251 Table *pTable = sqliteHashData(pElem);
252 if( pTable->isDelete ){
253 sqliteHashInsert(&toDelete, pTable, 0, pTable);
254 }else{
255 pTable->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000256 }
257 }
drhbeae3192001-09-22 18:12:08 +0000258 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
259 Table *pTable = sqliteHashData(pElem);
260 sqliteUnlinkAndDeleteTable(db, pTable);
261 }
262 sqliteHashClear(&toDelete);
263 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
264 Table *pIndex = sqliteHashData(pElem);
265 if( pIndex->isDelete ){
266 sqliteHashInsert(&toDelete, pIndex, 0, pIndex);
267 }else{
268 pIndex->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000269 }
270 }
drhbeae3192001-09-22 18:12:08 +0000271 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
272 Index *pIndex = sqliteHashData(pElem);
273 sqliteUnlinkAndDeleteIndex(db, pIndex);
274 }
275 sqliteHashClear(&toDelete);
drh5e00f6c2001-09-13 13:46:56 +0000276 db->flags &= ~SQLITE_InternChanges;
277}
278
279/*
280** This routine runs when one or more CREATE TABLE, CREATE INDEX,
drhf57b3392001-10-08 13:22:32 +0000281** DROP TABLE, or DROP INDEX statements gets rolled back. The
drh5e00f6c2001-09-13 13:46:56 +0000282** additions or deletions of Table and Index structures in the
283** internal hash tables are undone.
284**
285** See also: sqliteCommitInternalChanges()
286*/
287void sqliteRollbackInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000288 Hash toDelete;
289 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000290 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drhbeae3192001-09-22 18:12:08 +0000291 sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0);
drh50e5dad2001-09-15 00:57:28 +0000292 db->next_cookie = db->schema_cookie;
drhbeae3192001-09-22 18:12:08 +0000293 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
294 Table *pTable = sqliteHashData(pElem);
295 if( !pTable->isCommit ){
296 sqliteHashInsert(&toDelete, pTable, 0, pTable);
297 }else{
298 pTable->isDelete = 0;
drh5e00f6c2001-09-13 13:46:56 +0000299 }
300 }
drhbeae3192001-09-22 18:12:08 +0000301 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
302 Table *pTable = sqliteHashData(pElem);
303 sqliteUnlinkAndDeleteTable(db, pTable);
304 }
305 sqliteHashClear(&toDelete);
306 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
307 Table *pIndex = sqliteHashData(pElem);
308 if( !pIndex->isCommit ){
309 sqliteHashInsert(&toDelete, pIndex, 0, pIndex);
310 }else{
311 pIndex->isDelete = 0;
drh5e00f6c2001-09-13 13:46:56 +0000312 }
313 }
drhbeae3192001-09-22 18:12:08 +0000314 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
315 Index *pIndex = sqliteHashData(pElem);
316 sqliteUnlinkAndDeleteIndex(db, pIndex);
317 }
318 sqliteHashClear(&toDelete);
drh5e00f6c2001-09-13 13:46:56 +0000319 db->flags &= ~SQLITE_InternChanges;
320}
321
322/*
drh1ccde152000-06-17 13:12:39 +0000323** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000324**
325** Space to hold the name is obtained from sqliteMalloc() and must
326** be freed by the calling function.
327*/
drhcce7d172000-05-31 15:34:51 +0000328char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000329 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000330 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000331 return zName;
332}
333
334/*
335** Begin constructing a new table representation in memory. This is
336** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000337** to a CREATE TABLE statement. In particular, this routine is called
338** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000339** pStart token is the CREATE and pName is the table name. The isTemp
340** flag is true if the "TEMP" or "TEMPORARY" keyword occurs in between
341** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000342**
drhf57b3392001-10-08 13:22:32 +0000343** The new table record is initialized and put in pParse->pNewTable.
344** As more of the CREATE TABLE statement is parsed, additional action
345** routines will be called to add more information to this record.
346** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
347** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000348*/
drhf57b3392001-10-08 13:22:32 +0000349void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){
drh75897232000-05-29 14:26:00 +0000350 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000351 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000352 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000353 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000354 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000355
356 pParse->sFirstToken = *pStart;
357 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000358 if( zName==0 ) return;
drhf57b3392001-10-08 13:22:32 +0000359
360 /* Before trying to create a temporary table, make sure the Btree for
361 ** holding temporary tables is open.
362 */
363 if( isTemp && db->pBeTemp==0 ){
364 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp);
365 if( rc!=SQLITE_OK ){
366 sqliteSetNString(&pParse->zErrMsg, "unable to open a temporary database "
367 "file for storing temporary tables", 0);
368 pParse->nErr++;
369 return;
370 }
371 if( db->flags & SQLITE_InTrans ){
372 rc = sqliteBtreeBeginTrans(db->pBeTemp);
373 if( rc!=SQLITE_OK ){
374 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
375 "the temporary datbase file", 0);
376 pParse->nErr++;
377 return;
378 }
379 }
380 }
381
382 /* Make sure the new table name does not collide with an existing
383 ** index or table name. Issue an error message if it does.
384 **
385 ** If we are re-reading the sqlite_master table because of a schema
386 ** change and a new permanent table is found whose name collides with
387 ** an existing temporary table, then ignore the new permanent table.
388 ** We will continue parsing, but the pParse->nameClash flag will be set
389 ** so we will know to discard the table record once parsing has finished.
390 */
drhbe0072d2001-09-13 14:46:09 +0000391 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000392 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000393 if( pTable->isTemp && pParse->initFlag ){
394 pParse->nameClash = 1;
395 }else{
396 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
397 " already exists", 0, 0);
398 sqliteFree(zName);
399 pParse->nErr++;
400 return;
401 }
402 }else{
403 pParse->nameClash = 0;
drh75897232000-05-29 14:26:00 +0000404 }
drhf57b3392001-10-08 13:22:32 +0000405 if( (pIdx = sqliteFindIndex(db, zName))!=0 &&
406 (!pIdx->pTable->isTemp || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000407 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
408 zName, 0);
drh75897232000-05-29 14:26:00 +0000409 sqliteFree(zName);
410 pParse->nErr++;
411 return;
412 }
413 pTable = sqliteMalloc( sizeof(Table) );
drhdaffd0e2001-04-11 14:28:42 +0000414 if( pTable==0 ) return;
drh75897232000-05-29 14:26:00 +0000415 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000416 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000417 pTable->aCol = 0;
drh75897232000-05-29 14:26:00 +0000418 pTable->pIndex = 0;
drhf57b3392001-10-08 13:22:32 +0000419 pTable->isTemp = isTemp;
drhbe0072d2001-09-13 14:46:09 +0000420 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000421 pParse->pNewTable = pTable;
drhadbca9c2001-09-27 15:11:53 +0000422 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
423 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000424 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000425 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +0000426 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +0000427 }
drhf57b3392001-10-08 13:22:32 +0000428 if( !isTemp ){
429 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2, MASTER_NAME, 0);
430 }
drh5e00f6c2001-09-13 13:46:56 +0000431 }
drh75897232000-05-29 14:26:00 +0000432}
433
434/*
435** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000436**
437** The parser calls this routine once for each column declaration
438** in a CREATE TABLE statement. sqliteStartTable() gets called
439** first to get things going. Then this routine is called for each
440** column.
drh75897232000-05-29 14:26:00 +0000441*/
442void sqliteAddColumn(Parse *pParse, Token *pName){
443 Table *p;
444 char **pz;
445 if( (p = pParse->pNewTable)==0 ) return;
446 if( (p->nCol & 0x7)==0 ){
drh7020f652000-06-03 18:06:52 +0000447 p->aCol = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
drhdaffd0e2001-04-11 14:28:42 +0000448 if( p->aCol==0 ){
449 p->nCol = 0;
450 return;
451 }
drh75897232000-05-29 14:26:00 +0000452 }
drh7020f652000-06-03 18:06:52 +0000453 memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0]));
454 pz = &p->aCol[p->nCol++].zName;
drh75897232000-05-29 14:26:00 +0000455 sqliteSetNString(pz, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000456 sqliteDequote(*pz);
drh75897232000-05-29 14:26:00 +0000457}
458
459/*
drh382c0242001-10-06 16:33:02 +0000460** This routine is called by the parser while in the middle of
461** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
462** been seen on a column. This routine sets the notNull flag on
463** the column currently under construction.
464*/
465void sqliteAddNotNull(Parse *pParse){
466 Table *p;
467 int i;
468 if( (p = pParse->pNewTable)==0 ) return;
469 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000470 if( i>=0 ) p->aCol[i].notNull = 1;
drh382c0242001-10-06 16:33:02 +0000471}
472
473/*
474** This routine is called by the parser while in the middle of
475** parsing a CREATE TABLE statement. The pFirst token is the first
476** token in the sequence of tokens that describe the type of the
477** column currently under construction. pLast is the last token
478** in the sequence. Use this information to construct a string
479** that contains the typename of the column and store that string
480** in zType.
481*/
482void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
483 Table *p;
484 int i, j;
485 int n;
486 char *z, **pz;
487 if( (p = pParse->pNewTable)==0 ) return;
488 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000489 if( i<0 ) return;
drh382c0242001-10-06 16:33:02 +0000490 pz = &p->aCol[i].zType;
491 n = pLast->n + ((int)pLast->z) - (int)pFirst->z;
492 sqliteSetNString(pz, pFirst->z, n, 0);
493 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000494 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000495 for(i=j=0; z[i]; i++){
496 int c = z[i];
497 if( isspace(c) ) continue;
498 z[j++] = c;
499 }
500 z[j] = 0;
501}
502
503/*
drh7020f652000-06-03 18:06:52 +0000504** The given token is the default value for the last column added to
505** the table currently under construction. If "minusFlag" is true, it
506** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000507**
508** This routine is called by the parser while in the middle of
509** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000510*/
511void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
512 Table *p;
513 int i;
514 char **pz;
515 if( (p = pParse->pNewTable)==0 ) return;
516 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000517 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000518 pz = &p->aCol[i].zDflt;
519 if( minusFlag ){
520 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
521 }else{
522 sqliteSetNString(pz, pVal->z, pVal->n, 0);
523 }
524 sqliteDequote(*pz);
525}
526
527/*
drh50e5dad2001-09-15 00:57:28 +0000528** Come up with a new random value for the schema cookie. Make sure
529** the new value is different from the old.
530**
531** The schema cookie is used to determine when the schema for the
532** database changes. After each schema change, the cookie value
533** changes. When a process first reads the schema it records the
534** cookie. Thereafter, whenever it goes to access the database,
535** it checks the cookie to make sure the schema has not changed
536** since it was last read.
537**
538** This plan is not completely bullet-proof. It is possible for
539** the schema to change multiple times and for the cookie to be
540** set back to prior value. But schema changes are infrequent
541** and the probability of hitting the same cookie value is only
542** 1 chance in 2^32. So we're safe enough.
543*/
544static void changeCookie(sqlite *db){
545 if( db->next_cookie==db->schema_cookie ){
drh90bfcda2001-09-23 19:46:51 +0000546 db->next_cookie = db->schema_cookie + sqliteRandomByte(db) + 1;
drh50e5dad2001-09-15 00:57:28 +0000547 db->flags |= SQLITE_InternChanges;
548 }
549}
550
551/*
drh75897232000-05-29 14:26:00 +0000552** This routine is called to report the final ")" that terminates
553** a CREATE TABLE statement.
554**
drhf57b3392001-10-08 13:22:32 +0000555** The table structure that other action routines have been building
556** is added to the internal hash tables, assuming no errors have
557** occurred.
drh75897232000-05-29 14:26:00 +0000558**
drh1ccde152000-06-17 13:12:39 +0000559** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000560** unless this is a temporary table or initFlag==1. When initFlag==1,
561** it means we are reading the sqlite_master table because we just
562** connected to the database or because the sqlite_master table has
563** recently changes, so the entry for this table already exists in
564** the sqlite_master table. We do not want to create it again.
drh75897232000-05-29 14:26:00 +0000565*/
566void sqliteEndTable(Parse *pParse, Token *pEnd){
567 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000568 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000569
drhdaffd0e2001-04-11 14:28:42 +0000570 if( pEnd==0 || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000571 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000572 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000573
drhf57b3392001-10-08 13:22:32 +0000574 /* Add the table to the in-memory representation of the database.
drh75897232000-05-29 14:26:00 +0000575 */
drhf57b3392001-10-08 13:22:32 +0000576 assert( pParse->nameClash==0 || pParse->initFlag==0 );
577 if( pParse->explain==0 && pParse->nameClash==0 ){
drhbeae3192001-09-22 18:12:08 +0000578 sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
drh75897232000-05-29 14:26:00 +0000579 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000580 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000581 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000582 }
583
drhd78eeee2001-09-13 16:18:53 +0000584 /* If the initFlag is 1 it means we are reading the SQL off the
585 ** "sqlite_master" table on the disk. So do not write to the disk
drhe3c41372001-09-17 20:25:58 +0000586 ** again. Extract the root page number for the table from the
587 ** pParse->newTnum field. (The page number should have been put
drh382c0242001-10-06 16:33:02 +0000588 ** there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000589 */
590 if( pParse->initFlag ){
591 p->tnum = pParse->newTnum;
592 }
593
drhe3c41372001-09-17 20:25:58 +0000594 /* If not initializing, then create a record for the new table
595 ** in the SQLITE_MASTER table of the database.
drhf57b3392001-10-08 13:22:32 +0000596 **
597 ** If this is a TEMPORARY table, then just create the table. Do not
598 ** make an entry in SQLITE_MASTER.
drh75897232000-05-29 14:26:00 +0000599 */
600 if( !pParse->initFlag ){
drhadbca9c2001-09-27 15:11:53 +0000601 int n, addr;
drhd8bc7082000-06-07 23:51:50 +0000602 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000603
drhd8bc7082000-06-07 23:51:50 +0000604 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000605 if( v==0 ) return;
606 n = (int)pEnd->z - (int)pParse->sFirstToken.z + 1;
drhf57b3392001-10-08 13:22:32 +0000607 if( !p->isTemp ){
608 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0);
609 sqliteVdbeAddOp(v, OP_String, 0, 0, "table", 0);
610 sqliteVdbeAddOp(v, OP_String, 0, 0, p->zName, 0);
611 sqliteVdbeAddOp(v, OP_String, 0, 0, p->zName, 0);
612 }
drhadbca9c2001-09-27 15:11:53 +0000613 addr = sqliteVdbeAddOp(v, OP_CreateTable, 0, 0, 0, 0);
614 sqliteVdbeChangeP3(v, addr, (char *)&p->tnum, -1);
615 p->tnum = 0;
drhf57b3392001-10-08 13:22:32 +0000616 if( !p->isTemp ){
617 addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
618 sqliteVdbeChangeP3(v, addr, pParse->sFirstToken.z, n);
619 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0, 0, 0);
620 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
621 changeCookie(db);
622 sqliteVdbeAddOp(v, OP_SetCookie, db->next_cookie, 0, 0, 0);
623 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
624 }
drhbe0072d2001-09-13 14:46:09 +0000625 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000626 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
627 }
drh75897232000-05-29 14:26:00 +0000628 }
629}
630
631/*
632** Given a token, look up a table with that name. If not found, leave
633** an error for the parser to find and return NULL.
634*/
drhcce7d172000-05-31 15:34:51 +0000635Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +0000636 char *zName;
637 Table *pTab;
638 zName = sqliteTableNameFromToken(pTok);
639 if( zName==0 ) return 0;
640 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +0000641 sqliteFree(zName);
642 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +0000643 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
644 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +0000645 pParse->nErr++;
646 }
647 return pTab;
648}
649
650/*
651** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +0000652** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +0000653*/
654void sqliteDropTable(Parse *pParse, Token *pName){
655 Table *pTable;
drh75897232000-05-29 14:26:00 +0000656 Vdbe *v;
657 int base;
drh5edc3122001-09-13 21:53:09 +0000658 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000659
drhdaffd0e2001-04-11 14:28:42 +0000660 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000661 pTable = sqliteTableFromToken(pParse, pName);
662 if( pTable==0 ) return;
663 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +0000664 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
665 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +0000666 pParse->nErr++;
667 return;
668 }
669
drh1ccde152000-06-17 13:12:39 +0000670 /* Generate code to remove the table from the master table
671 ** on disk.
672 */
drhd8bc7082000-06-07 23:51:50 +0000673 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000674 if( v ){
675 static VdbeOp dropTable[] = {
drhecdc7532001-09-23 02:35:53 +0000676 { OP_OpenWrite, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +0000677 { OP_Rewind, 0, 0, 0},
678 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +0000679 { OP_Next, 0, ADDR(9), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +0000680 { OP_Dup, 0, 0, 0},
drhe3c41372001-09-17 20:25:58 +0000681 { OP_Column, 0, 2, 0},
drhd78eeee2001-09-13 16:18:53 +0000682 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +0000683 { OP_Delete, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000684 { OP_Goto, 0, ADDR(3), 0},
drhf57b3392001-10-08 13:22:32 +0000685 { OP_SetCookie, 0, 0, 0}, /* 9 */
drh75897232000-05-29 14:26:00 +0000686 { OP_Close, 0, 0, 0},
687 };
688 Index *pIdx;
drh5edc3122001-09-13 21:53:09 +0000689 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000690 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000691 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +0000692 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +0000693 }
drhf57b3392001-10-08 13:22:32 +0000694 if( !pTable->isTemp ){
695 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
696 sqliteVdbeChangeP3(v, base+2, pTable->zName, 0);
697 changeCookie(db);
698 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
699 }
700 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp, 0, 0);
drh75897232000-05-29 14:26:00 +0000701 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drhf57b3392001-10-08 13:22:32 +0000702 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000703 }
drh5edc3122001-09-13 21:53:09 +0000704 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000705 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000706 }
707 }
708
drh5e00f6c2001-09-13 13:46:56 +0000709 /* Mark the in-memory Table structure as being deleted. The actually
710 ** deletion occurs inside of sqliteCommitInternalChanges().
drh75897232000-05-29 14:26:00 +0000711 **
712 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +0000713 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +0000714 */
715 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +0000716 pTable->isDelete = 1;
drh5edc3122001-09-13 21:53:09 +0000717 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000718 }
719}
720
721/*
722** Create a new index for an SQL table. pIndex is the name of the index
723** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +0000724** be NULL for a primary key or an index that is created to satisfy a
725** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +0000726** as the table to be indexed. pParse->pNewTable is a table that is
727** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +0000728**
drh382c0242001-10-06 16:33:02 +0000729** pList is a list of columns to be indexed. pList will be NULL if this
730** is a primary key or unique-constraint on the most recent column added
731** to the table currently under construction.
drh75897232000-05-29 14:26:00 +0000732*/
733void sqliteCreateIndex(
734 Parse *pParse, /* All information about this parse */
735 Token *pName, /* Name of the index. May be NULL */
736 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +0000737 IdList *pList, /* A list of columns to be indexed */
drh717e6402001-09-27 03:22:32 +0000738 int isUnique, /* True if all entries in this index must be unique */
drh75897232000-05-29 14:26:00 +0000739 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
740 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
741){
742 Table *pTab; /* Table to be indexed */
743 Index *pIndex; /* The index to be created */
744 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +0000745 int i, j;
drhf57b3392001-10-08 13:22:32 +0000746 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +0000747 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +0000748 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +0000749
drhdaffd0e2001-04-11 14:28:42 +0000750 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
751
drh75897232000-05-29 14:26:00 +0000752 /*
753 ** Find the table that is to be indexed. Return early if not found.
754 */
755 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +0000756 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +0000757 pTab = sqliteTableFromToken(pParse, pTable);
758 }else{
drhe3c41372001-09-17 20:25:58 +0000759 assert( pName==0 );
drh75897232000-05-29 14:26:00 +0000760 pTab = pParse->pNewTable;
761 }
762 if( pTab==0 || pParse->nErr ) goto exit_create_index;
763 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +0000764 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
765 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +0000766 pParse->nErr++;
767 goto exit_create_index;
768 }
769
drhf57b3392001-10-08 13:22:32 +0000770 /* If this index is created while re-reading the schema from sqlite_master
771 ** but the table associated with this index is a temporary table, it can
772 ** only mean that the table this index is really associated with is one
773 ** whose name is hidden behind a temporary table with the same name.
774 ** Since its table has been suppressed, we need to also suppress the
775 ** index.
776 */
777 if( pParse->initFlag && pTab->isTemp ){
778 goto exit_create_index;
779 }
780
drh75897232000-05-29 14:26:00 +0000781 /*
782 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +0000783 ** index or table with the same name.
784 **
785 ** Exception: If we are reading the names of permanent indices from the
786 ** sqlite_master table (because some other process changed the schema) and
787 ** one of the index names collides with the name of a temporary table or
788 ** index, then we will continue to process this index, but we will not
789 ** store its name in the hash table. Set the hideName flag to accomplish
790 ** this.
791 **
792 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +0000793 ** dealing with a primary key or UNIQUE constraint. We have to invent our
794 ** own name.
drh75897232000-05-29 14:26:00 +0000795 */
796 if( pName ){
drhf57b3392001-10-08 13:22:32 +0000797 Index *pISameName; /* Another index with the same name */
798 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +0000799 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +0000800 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +0000801 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
802 if( pISameName->pTable->isTemp && pParse->initFlag ){
803 hideName = 1;
804 }else{
805 sqliteSetString(&pParse->zErrMsg, "index ", zName,
806 " already exists", 0);
807 pParse->nErr++;
808 goto exit_create_index;
809 }
drhe3c41372001-09-17 20:25:58 +0000810 }
drhf57b3392001-10-08 13:22:32 +0000811 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
812 if( pTSameName->isTemp && pParse->initFlag ){
813 hideName = 1;
814 }else{
815 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
816 zName, 0);
817 pParse->nErr++;
818 goto exit_create_index;
819 }
drhe3c41372001-09-17 20:25:58 +0000820 }
drh75897232000-05-29 14:26:00 +0000821 }else{
drhadbca9c2001-09-27 15:11:53 +0000822 char zBuf[30];
823 int n;
824 Index *pLoop;
825 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
826 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +0000827 zName = 0;
drhadbca9c2001-09-27 15:11:53 +0000828 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +0000829 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +0000830 }
831
832 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +0000833 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +0000834 ** So create a fake list to simulate this.
835 */
836 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +0000837 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +0000838 nullId.n = strlen(nullId.z);
839 pList = sqliteIdListAppend(0, &nullId);
840 if( pList==0 ) goto exit_create_index;
841 }
842
843 /*
844 ** Allocate the index structure.
845 */
drhdcc581c2000-05-30 13:44:19 +0000846 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +0000847 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +0000848 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +0000849 pIndex->aiColumn = (int*)&pIndex[1];
850 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +0000851 strcpy(pIndex->zName, zName);
852 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +0000853 pIndex->nColumn = pList->nId;
drh717e6402001-09-27 03:22:32 +0000854 pIndex->isUnique = isUnique;
drh75897232000-05-29 14:26:00 +0000855
drh1ccde152000-06-17 13:12:39 +0000856 /* Scan the names of the columns of the table to be indexed and
857 ** load the column indices into the Index structure. Report an error
858 ** if any column is not found.
drh75897232000-05-29 14:26:00 +0000859 */
860 for(i=0; i<pList->nId; i++){
861 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000862 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +0000863 }
864 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +0000865 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +0000866 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +0000867 pParse->nErr++;
868 sqliteFree(pIndex);
869 goto exit_create_index;
870 }
drh967e8b72000-06-21 13:59:10 +0000871 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +0000872 }
873
874 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +0000875 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +0000876 */
drhadbca9c2001-09-27 15:11:53 +0000877 pIndex->pNext = pTab->pIndex;
878 pTab->pIndex = pIndex;
drhf57b3392001-10-08 13:22:32 +0000879 if( !pParse->explain && !hideName ){
drhadbca9c2001-09-27 15:11:53 +0000880 sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000881 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000882 }
883
drhd78eeee2001-09-13 16:18:53 +0000884 /* If the initFlag is 1 it means we are reading the SQL off the
885 ** "sqlite_master" table on the disk. So do not write to the disk
886 ** again. Extract the table number from the pParse->newTnum field.
887 */
drhadbca9c2001-09-27 15:11:53 +0000888 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +0000889 pIndex->tnum = pParse->newTnum;
890 }
891
drh75897232000-05-29 14:26:00 +0000892 /* If the initFlag is 0 then create the index on disk. This
893 ** involves writing the index into the master table and filling in the
894 ** index with the current table contents.
895 **
896 ** The initFlag is 0 when the user first enters a CREATE INDEX
897 ** command. The initFlag is 1 when a database is opened and
898 ** CREATE INDEX statements are read out of the master table. In
899 ** the latter case the index already exists on disk, which is why
900 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +0000901 **
902 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +0000903 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
904 ** has just been created, it contains no data and the index initialization
905 ** step can be skipped.
drh75897232000-05-29 14:26:00 +0000906 */
drhadbca9c2001-09-27 15:11:53 +0000907 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +0000908 int n;
drhadbca9c2001-09-27 15:11:53 +0000909 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000910 int lbl1, lbl2;
911 int i;
drhadbca9c2001-09-27 15:11:53 +0000912 int addr;
drhf57b3392001-10-08 13:22:32 +0000913 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +0000914
drhd8bc7082000-06-07 23:51:50 +0000915 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000916 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +0000917 if( pTable!=0 ){
918 if( (db->flags & SQLITE_InTrans)==0 ){
919 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
920 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
921 pParse->schemaVerified = 1;
922 }
drhf57b3392001-10-08 13:22:32 +0000923 if( !isTemp ){
924 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2, MASTER_NAME, 0);
925 }
drhadbca9c2001-09-27 15:11:53 +0000926 }
drhf57b3392001-10-08 13:22:32 +0000927 if( !isTemp ){
928 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0);
929 sqliteVdbeAddOp(v, OP_String, 0, 0, "index", 0);
930 sqliteVdbeAddOp(v, OP_String, 0, 0, pIndex->zName, 0);
931 sqliteVdbeAddOp(v, OP_String, 0, 0, pTab->zName, 0);
932 }
933 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp, 0, 0);
drhadbca9c2001-09-27 15:11:53 +0000934 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, -1);
935 pIndex->tnum = 0;
936 if( pTable ){
drhf57b3392001-10-08 13:22:32 +0000937 if( isTemp ){
938 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0, 0, 0);
939 }else{
940 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
941 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0, 0, 0);
942 }
drh5e00f6c2001-09-13 13:46:56 +0000943 }
drhf57b3392001-10-08 13:22:32 +0000944 if( !isTemp ){
945 addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
946 if( pStart && pEnd ){
947 n = (int)pEnd->z - (int)pStart->z + 1;
948 sqliteVdbeChangeP3(v, addr, pStart->z, n);
949 }
950 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0, 0, 0);
951 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000952 }
drhadbca9c2001-09-27 15:11:53 +0000953 if( pTable ){
drhf57b3392001-10-08 13:22:32 +0000954 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open,
955 2, pTab->tnum, pTab->zName, 0);
drhadbca9c2001-09-27 15:11:53 +0000956 lbl1 = sqliteVdbeMakeLabel(v);
957 lbl2 = sqliteVdbeMakeLabel(v);
958 sqliteVdbeAddOp(v, OP_Rewind, 2, 0, 0, 0);
959 sqliteVdbeAddOp(v, OP_Next, 2, lbl2, 0, lbl1);
960 sqliteVdbeAddOp(v, OP_Recno, 2, 0, 0, 0);
961 for(i=0; i<pIndex->nColumn; i++){
962 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i], 0, 0);
963 }
964 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0, 0, 0);
965 sqliteVdbeAddOp(v, OP_PutIdx, 1, pIndex->isUnique, 0, 0);
966 sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0);
967 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2);
968 sqliteVdbeAddOp(v, OP_Close, 2, 0, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000969 sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000970 }
drhadbca9c2001-09-27 15:11:53 +0000971 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000972 if( !isTemp ){
973 changeCookie(db);
974 sqliteVdbeAddOp(v, OP_SetCookie, db->next_cookie, 0, 0, 0);
975 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
976 }
drhadbca9c2001-09-27 15:11:53 +0000977 if( (db->flags & SQLITE_InTrans)==0 ){
978 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
979 }
drh5e00f6c2001-09-13 13:46:56 +0000980 }
drh75897232000-05-29 14:26:00 +0000981 }
982
drh75897232000-05-29 14:26:00 +0000983 /* Clean up before exiting */
984exit_create_index:
985 sqliteIdListDelete(pList);
986 sqliteFree(zName);
987 return;
988}
989
990/*
991** This routine will drop an existing named index.
992*/
993void sqliteDropIndex(Parse *pParse, Token *pName){
994 Index *pIndex;
995 char *zName;
996 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +0000997 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000998
drhdaffd0e2001-04-11 14:28:42 +0000999 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001000 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001001 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001002 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001003 sqliteFree(zName);
1004 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001005 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1006 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001007 pParse->nErr++;
1008 return;
1009 }
1010
1011 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001012 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001013 if( v ){
1014 static VdbeOp dropIndex[] = {
drhecdc7532001-09-23 02:35:53 +00001015 { OP_OpenWrite, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +00001016 { OP_Rewind, 0, 0, 0},
1017 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +00001018 { OP_Next, 0, ADDR(8), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +00001019 { OP_Dup, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +00001020 { OP_Column, 0, 1, 0},
drhd78eeee2001-09-13 16:18:53 +00001021 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +00001022 { OP_Delete, 0, 0, 0},
drh5edc3122001-09-13 21:53:09 +00001023 { OP_Destroy, 0, 0, 0}, /* 8 */
drh50e5dad2001-09-15 00:57:28 +00001024 { OP_SetCookie, 0, 0, 0}, /* 9 */
drh75897232000-05-29 14:26:00 +00001025 { OP_Close, 0, 0, 0},
1026 };
1027 int base;
drhf57b3392001-10-08 13:22:32 +00001028 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001029
drhbe0072d2001-09-13 14:46:09 +00001030 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001031 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001032 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +00001033 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +00001034 }
drhf57b3392001-10-08 13:22:32 +00001035 if( !pTab->isTemp ){
1036 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1037 sqliteVdbeChangeP3(v, base+2, pIndex->zName, 0);
1038 changeCookie(db);
1039 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
1040 }
1041 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp, 0, 0);
drhbe0072d2001-09-13 14:46:09 +00001042 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001043 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1044 }
drh75897232000-05-29 14:26:00 +00001045 }
1046
drh5e00f6c2001-09-13 13:46:56 +00001047 /* Mark the internal Index structure for deletion by the
1048 ** sqliteCommitInternalChanges routine.
drh75897232000-05-29 14:26:00 +00001049 */
1050 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +00001051 pIndex->isDelete = 1;
1052 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001053 }
1054}
1055
1056/*
1057** Add a new element to the end of an expression list. If pList is
1058** initially NULL, then create a new expression list.
1059*/
1060ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
1061 int i;
1062 if( pList==0 ){
1063 pList = sqliteMalloc( sizeof(ExprList) );
drhdaffd0e2001-04-11 14:28:42 +00001064 if( pList==0 ) return 0;
drh75897232000-05-29 14:26:00 +00001065 }
drh75897232000-05-29 14:26:00 +00001066 if( (pList->nExpr & 7)==0 ){
1067 int n = pList->nExpr + 8;
1068 pList->a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
1069 if( pList->a==0 ){
1070 pList->nExpr = 0;
1071 return pList;
1072 }
1073 }
1074 i = pList->nExpr++;
1075 pList->a[i].pExpr = pExpr;
1076 pList->a[i].zName = 0;
1077 if( pName ){
1078 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +00001079 sqliteDequote(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001080 }
1081 return pList;
1082}
1083
1084/*
1085** Delete an entire expression list.
1086*/
1087void sqliteExprListDelete(ExprList *pList){
1088 int i;
1089 if( pList==0 ) return;
1090 for(i=0; i<pList->nExpr; i++){
1091 sqliteExprDelete(pList->a[i].pExpr);
1092 sqliteFree(pList->a[i].zName);
1093 }
1094 sqliteFree(pList->a);
1095 sqliteFree(pList);
1096}
1097
1098/*
1099** Append a new element to the given IdList. Create a new IdList if
1100** need be.
drhdaffd0e2001-04-11 14:28:42 +00001101**
1102** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001103*/
1104IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1105 if( pList==0 ){
1106 pList = sqliteMalloc( sizeof(IdList) );
1107 if( pList==0 ) return 0;
1108 }
1109 if( (pList->nId & 7)==0 ){
1110 pList->a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1111 if( pList->a==0 ){
1112 pList->nId = 0;
drhdaffd0e2001-04-11 14:28:42 +00001113 sqliteIdListDelete(pList);
1114 return 0;
drh75897232000-05-29 14:26:00 +00001115 }
1116 }
1117 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1118 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001119 char **pz = &pList->a[pList->nId].zName;
1120 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1121 if( *pz==0 ){
1122 sqliteIdListDelete(pList);
1123 return 0;
1124 }else{
1125 sqliteDequote(*pz);
1126 }
drh75897232000-05-29 14:26:00 +00001127 }
1128 pList->nId++;
1129 return pList;
1130}
1131
1132/*
1133** Add an alias to the last identifier on the given identifier list.
1134*/
1135void sqliteIdListAddAlias(IdList *pList, Token *pToken){
1136 if( pList && pList->nId>0 ){
1137 int i = pList->nId - 1;
1138 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001139 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001140 }
1141}
1142
1143/*
1144** Delete an entire IdList
1145*/
1146void sqliteIdListDelete(IdList *pList){
1147 int i;
1148 if( pList==0 ) return;
1149 for(i=0; i<pList->nId; i++){
1150 sqliteFree(pList->a[i].zName);
1151 sqliteFree(pList->a[i].zAlias);
drhdaffd0e2001-04-11 14:28:42 +00001152 if( pList->a[i].pSelect ){
1153 sqliteFree(pList->a[i].zName);
1154 sqliteSelectDelete(pList->a[i].pSelect);
1155 sqliteDeleteTable(0, pList->a[i].pTab);
1156 }
drh75897232000-05-29 14:26:00 +00001157 }
1158 sqliteFree(pList->a);
1159 sqliteFree(pList);
1160}
1161
drh982cef72000-05-30 16:27:03 +00001162
1163/*
1164** The COPY command is for compatibility with PostgreSQL and specificially
1165** for the ability to read the output of pg_dump. The format is as
1166** follows:
1167**
1168** COPY table FROM file [USING DELIMITERS string]
1169**
1170** "table" is an existing table name. We will read lines of code from
1171** file to fill this table with data. File might be "stdin". The optional
1172** delimiter string identifies the field separators. The default is a tab.
1173*/
1174void sqliteCopy(
1175 Parse *pParse, /* The parser context */
1176 Token *pTableName, /* The name of the table into which we will insert */
1177 Token *pFilename, /* The file from which to obtain information */
1178 Token *pDelimiter /* Use this as the field delimiter */
1179){
1180 Table *pTab;
1181 char *zTab;
1182 int i, j;
1183 Vdbe *v;
1184 int addr, end;
1185 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001186 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001187
1188 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001189 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001190 pTab = sqliteFindTable(db, zTab);
drh982cef72000-05-30 16:27:03 +00001191 sqliteFree(zTab);
1192 if( pTab==0 ){
1193 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1194 pTableName->z, pTableName->n, 0);
1195 pParse->nErr++;
1196 goto copy_cleanup;
1197 }
1198 if( pTab->readOnly ){
1199 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1200 " may not be modified", 0);
1201 pParse->nErr++;
1202 goto copy_cleanup;
1203 }
drhd8bc7082000-06-07 23:51:50 +00001204 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001205 if( v ){
drhf57b3392001-10-08 13:22:32 +00001206 int openOp;
drhbe0072d2001-09-13 14:46:09 +00001207 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001208 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001209 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +00001210 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +00001211 }
drh982cef72000-05-30 16:27:03 +00001212 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0, 0, 0);
1213 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001214 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001215 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
1216 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum, pTab->zName, 0);
drh982cef72000-05-30 16:27:03 +00001217 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhf57b3392001-10-08 13:22:32 +00001218 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum, pIdx->zName, 0);
drh982cef72000-05-30 16:27:03 +00001219 }
1220 end = sqliteVdbeMakeLabel(v);
1221 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end, 0, 0);
1222 if( pDelimiter ){
1223 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1224 sqliteVdbeDequoteP3(v, addr);
1225 }else{
1226 sqliteVdbeChangeP3(v, addr, "\t", 1);
1227 }
drh5e00f6c2001-09-13 13:46:56 +00001228 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001229 if( pTab->pIndex ){
1230 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1231 }
1232 for(i=0; i<pTab->nCol; i++){
drh5e00f6c2001-09-13 13:46:56 +00001233 sqliteVdbeAddOp(v, OP_FileColumn, i, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001234 }
1235 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
1236 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
1237 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1238 if( pIdx->pNext ){
1239 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1240 }
drh967e8b72000-06-21 13:59:10 +00001241 for(j=0; j<pIdx->nColumn; j++){
drh5e00f6c2001-09-13 13:46:56 +00001242 sqliteVdbeAddOp(v, OP_FileColumn, pIdx->aiColumn[j], 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001243 }
drh5e00f6c2001-09-13 13:46:56 +00001244 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
drh717e6402001-09-27 03:22:32 +00001245 sqliteVdbeAddOp(v, OP_PutIdx, i, pIdx->isUnique, 0, 0);
drh982cef72000-05-30 16:27:03 +00001246 }
1247 sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
1248 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, end);
drhbe0072d2001-09-13 14:46:09 +00001249 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001250 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1251 }
drh982cef72000-05-30 16:27:03 +00001252 }
1253
1254copy_cleanup:
1255 return;
1256}
drhdce2cbe2000-05-31 02:27:49 +00001257
1258/*
1259** The non-standard VACUUM command is used to clean up the database,
1260** collapse free space, etc. It is modelled after the VACUUM command
1261** in PostgreSQL.
1262*/
1263void sqliteVacuum(Parse *pParse, Token *pTableName){
1264 char *zName;
1265 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001266 sqlite *db = pParse->db;
drhdce2cbe2000-05-31 02:27:49 +00001267
drhdaffd0e2001-04-11 14:28:42 +00001268 if( pParse->nErr || sqlite_malloc_failed ) return;
drhdce2cbe2000-05-31 02:27:49 +00001269 if( pTableName ){
1270 zName = sqliteTableNameFromToken(pTableName);
1271 }else{
1272 zName = 0;
1273 }
drhbe0072d2001-09-13 14:46:09 +00001274 if( zName && sqliteFindIndex(db, zName)==0
1275 && sqliteFindTable(db, zName)==0 ){
drhdce2cbe2000-05-31 02:27:49 +00001276 sqliteSetString(&pParse->zErrMsg, "no such table or index: ", zName, 0);
1277 pParse->nErr++;
1278 goto vacuum_cleanup;
1279 }
drhd8bc7082000-06-07 23:51:50 +00001280 v = sqliteGetVdbe(pParse);
drhdce2cbe2000-05-31 02:27:49 +00001281 if( v==0 ) goto vacuum_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001282 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001283 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001284 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +00001285 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +00001286 }
drhdce2cbe2000-05-31 02:27:49 +00001287 if( zName ){
1288 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, zName, 0);
1289 }else{
drhdce2cbe2000-05-31 02:27:49 +00001290 Table *pTab;
1291 Index *pIdx;
drhbeae3192001-09-22 18:12:08 +00001292 HashElem *pE;
1293 for(pE=sqliteHashFirst(&db->tblHash); pE; pE=sqliteHashNext(pE)){
1294 pTab = sqliteHashData(pE);
1295 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pTab->zName, 0);
1296 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1297 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pIdx->zName, 0);
drhdce2cbe2000-05-31 02:27:49 +00001298 }
1299 }
1300 }
drhbe0072d2001-09-13 14:46:09 +00001301 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001302 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1303 }
drhdce2cbe2000-05-31 02:27:49 +00001304
1305vacuum_cleanup:
1306 sqliteFree(zName);
1307 return;
1308}
drhc4a3c772001-04-04 11:48:57 +00001309
1310/*
1311** Begin a transaction
1312*/
1313void sqliteBeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001314 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001315 Vdbe *v;
1316
drhc4a3c772001-04-04 11:48:57 +00001317 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001318 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001319 if( db->flags & SQLITE_InTrans ) return;
drh5e00f6c2001-09-13 13:46:56 +00001320 v = sqliteGetVdbe(pParse);
1321 if( v ){
1322 sqliteVdbeAddOp(v, OP_Transaction, 1, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001323 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +00001324 pParse->schemaVerified = 1;
drhc4a3c772001-04-04 11:48:57 +00001325 }
drh5e00f6c2001-09-13 13:46:56 +00001326 db->flags |= SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001327}
1328
1329/*
1330** Commit a transaction
1331*/
1332void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001333 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001334 Vdbe *v;
1335
drhc4a3c772001-04-04 11:48:57 +00001336 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001337 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001338 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001339 v = sqliteGetVdbe(pParse);
1340 if( v ){
1341 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001342 }
drh5e00f6c2001-09-13 13:46:56 +00001343 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001344}
1345
1346/*
1347** Rollback a transaction
1348*/
1349void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001350 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001351 Vdbe *v;
1352
drhc4a3c772001-04-04 11:48:57 +00001353 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001354 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001355 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001356 v = sqliteGetVdbe(pParse);
1357 if( v ){
1358 sqliteVdbeAddOp(v, OP_Rollback, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001359 }
drh5e00f6c2001-09-13 13:46:56 +00001360 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001361}
drhf57b14a2001-09-14 18:54:08 +00001362
1363/*
1364** Interpret the given string as a boolean value.
1365*/
1366static int getBoolean(char *z){
1367 static char *azTrue[] = { "yes", "on", "true" };
1368 int i;
1369 if( z[0]==0 ) return 0;
1370 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
1371 return atoi(z);
1372 }
1373 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
1374 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
1375 }
1376 return 0;
1377}
1378
1379/*
1380** Process a pragma statement.
1381**
1382** Pragmas are of this form:
1383**
1384** PRAGMA id = value
1385**
1386** The identifier might also be a string. The value is a string, and
1387** identifier, or a number. If minusFlag is true, then the value is
1388** a number that was preceded by a minus sign.
1389*/
1390void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1391 char *zLeft = 0;
1392 char *zRight = 0;
1393 sqlite *db = pParse->db;
1394
1395 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1396 sqliteDequote(zLeft);
1397 if( minusFlag ){
1398 zRight = 0;
1399 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1400 }else{
1401 zRight = sqliteStrNDup(pRight->z, pRight->n);
1402 sqliteDequote(zRight);
1403 }
1404
1405 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
1406 int size = atoi(zRight);
1407 sqliteBtreeSetCacheSize(db->pBe, size);
1408 }else
1409
1410 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
1411 if( getBoolean(zRight) ){
1412 db->flags |= SQLITE_VdbeTrace;
1413 }else{
1414 db->flags &= ~SQLITE_VdbeTrace;
1415 }
1416 }else
1417
drh382c0242001-10-06 16:33:02 +00001418 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
1419 if( getBoolean(zRight) ){
1420 db->flags |= SQLITE_FullColNames;
1421 }else{
1422 db->flags &= ~SQLITE_FullColNames;
1423 }
1424 }else
1425
1426 if( sqliteStrICmp(zLeft, "table_info")==0 ){
1427 Table *pTab;
1428 Vdbe *v;
1429 pTab = sqliteFindTable(db, zRight);
1430 if( pTab ) v = sqliteGetVdbe(pParse);
1431 if( pTab && v ){
1432 static VdbeOp tableInfoPreface[] = {
1433 { OP_ColumnCount, 5, 0, 0},
1434 { OP_ColumnName, 0, 0, "cid"},
1435 { OP_ColumnName, 1, 0, "name"},
1436 { OP_ColumnName, 2, 0, "type"},
1437 { OP_ColumnName, 3, 0, "notnull"},
1438 { OP_ColumnName, 4, 0, "dflt_value"},
1439 };
1440 int i;
1441 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
1442 for(i=0; i<pTab->nCol; i++){
1443 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
1444 sqliteVdbeAddOp(v, OP_String, 0, 0, pTab->aCol[i].zName, 0);
1445 sqliteVdbeAddOp(v, OP_String, 0, 0,
1446 pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", 0);
1447 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0, 0, 0);
1448 sqliteVdbeAddOp(v, OP_String, 0, 0, pTab->aCol[i].zDflt, 0);
1449 sqliteVdbeAddOp(v, OP_Callback, 5, 0, 0, 0);
1450 }
1451 }
1452 }else
1453
1454 if( sqliteStrICmp(zLeft, "index_info")==0 ){
1455 Index *pIdx;
1456 Table *pTab;
1457 Vdbe *v;
1458 pIdx = sqliteFindIndex(db, zRight);
1459 if( pIdx ) v = sqliteGetVdbe(pParse);
1460 if( pIdx && v ){
1461 static VdbeOp tableInfoPreface[] = {
1462 { OP_ColumnCount, 3, 0, 0},
1463 { OP_ColumnName, 0, 0, "seqno"},
1464 { OP_ColumnName, 1, 0, "cid"},
1465 { OP_ColumnName, 2, 0, "name"},
1466 };
1467 int i;
1468 pTab = pIdx->pTable;
1469 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
1470 for(i=0; i<pIdx->nColumn; i++){
1471 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
1472 sqliteVdbeAddOp(v, OP_Integer, pIdx->aiColumn[i], 0, 0, 0);
1473 sqliteVdbeAddOp(v, OP_String, 0, 0,
1474 pTab->aCol[pIdx->aiColumn[i]].zName, 0);
1475 sqliteVdbeAddOp(v, OP_Callback, 3, 0, 0, 0);
1476 }
1477 }
1478 }else
1479
drhf57b14a2001-09-14 18:54:08 +00001480#ifndef NDEBUG
1481 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
1482 extern void sqliteParserTrace(FILE*, char *);
1483 if( getBoolean(zRight) ){
1484 sqliteParserTrace(stdout, "parser: ");
1485 }else{
1486 sqliteParserTrace(0, 0);
1487 }
1488 }else
1489#endif
1490
drhf57b3392001-10-08 13:22:32 +00001491 {}
1492 sqliteFree(zLeft);
1493 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00001494}