drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 12 | ** 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: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 15 | ** |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 16 | ** CREATE TABLE |
| 17 | ** DROP TABLE |
| 18 | ** CREATE INDEX |
| 19 | ** DROP INDEX |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 20 | ** creating ID lists |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 21 | ** BEGIN TRANSACTION |
| 22 | ** COMMIT |
| 23 | ** ROLLBACK |
| 24 | ** PRAGMA |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 25 | ** |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame^] | 26 | ** $Id: build.c,v 1.174 2004/02/22 20:05:01 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 27 | */ |
| 28 | #include "sqliteInt.h" |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 29 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 30 | |
| 31 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 32 | ** This routine is called when a new SQL statement is beginning to |
| 33 | ** be parsed. Check to see if the schema for the database needs |
| 34 | ** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables. |
| 35 | ** If it does, then read it. |
| 36 | */ |
| 37 | void sqliteBeginParse(Parse *pParse, int explainFlag){ |
| 38 | sqlite *db = pParse->db; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 39 | int i; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 40 | pParse->explain = explainFlag; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 41 | if((db->flags & SQLITE_Initialized)==0 && db->init.busy==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 42 | int rc = sqliteInit(db, &pParse->zErrMsg); |
| 43 | if( rc!=SQLITE_OK ){ |
| 44 | pParse->rc = rc; |
| 45 | pParse->nErr++; |
| 46 | } |
| 47 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 48 | for(i=0; i<db->nDb; i++){ |
| 49 | DbClearProperty(db, i, DB_Locked); |
| 50 | if( !db->aDb[i].inTrans ){ |
| 51 | DbClearProperty(db, i, DB_Cookie); |
| 52 | } |
| 53 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 54 | pParse->nVar = 0; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 58 | ** This routine is called after a single SQL statement has been |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 59 | ** parsed and we want to execute the VDBE code to implement |
| 60 | ** that statement. Prior action routines should have already |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 61 | ** constructed VDBE code to do the work of the SQL statement. |
| 62 | ** This routine just has to execute the VDBE code. |
| 63 | ** |
| 64 | ** Note that if an error occurred, it might be the case that |
| 65 | ** no VDBE code was generated. |
| 66 | */ |
| 67 | void sqliteExec(Parse *pParse){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 68 | sqlite *db = pParse->db; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 69 | Vdbe *v = pParse->pVdbe; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 70 | |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 71 | if( v==0 && (v = sqliteGetVdbe(pParse))!=0 ){ |
| 72 | sqliteVdbeAddOp(v, OP_Halt, 0, 0); |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 73 | } |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 74 | if( sqlite_malloc_failed ) return; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 75 | if( v && pParse->nErr==0 ){ |
| 76 | FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; |
| 77 | sqliteVdbeTrace(v, trace); |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 78 | sqliteVdbeMakeReady(v, pParse->nVar, pParse->explain); |
| 79 | pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 80 | pParse->colNamesSet = 0; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 81 | }else if( pParse->rc==SQLITE_OK ){ |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 82 | pParse->rc = SQLITE_ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 83 | } |
drh | a226d05 | 2002-09-25 19:04:07 +0000 | [diff] [blame] | 84 | pParse->nTab = 0; |
| 85 | pParse->nMem = 0; |
| 86 | pParse->nSet = 0; |
| 87 | pParse->nAgg = 0; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 88 | pParse->nVar = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 92 | ** Locate the in-memory structure that describes |
| 93 | ** a particular database table given the name |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 94 | ** of that table and (optionally) the name of the database |
| 95 | ** containing the table. Return NULL if not found. |
| 96 | ** |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 97 | ** If zDatabase is 0, all databases are searched for the |
| 98 | ** table and the first matching table is returned. (No checking |
| 99 | ** for duplicate table names is done.) The search order is |
| 100 | ** TEMP first, then MAIN, then any auxiliary databases added |
| 101 | ** using the ATTACH command. |
| 102 | ** |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 103 | ** See also sqliteLocateTable(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 104 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 105 | Table *sqliteFindTable(sqlite *db, const char *zName, const char *zDatabase){ |
| 106 | Table *p = 0; |
| 107 | int i; |
| 108 | for(i=0; i<db->nDb; i++){ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 109 | int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
| 110 | if( zDatabase!=0 && sqliteStrICmp(zDatabase, db->aDb[j].zName) ) continue; |
| 111 | p = sqliteHashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 112 | if( p ) break; |
| 113 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 114 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 118 | ** Locate the in-memory structure that describes |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 119 | ** a particular database table given the name |
| 120 | ** of that table and (optionally) the name of the database |
| 121 | ** containing the table. Return NULL if not found. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 122 | ** Also leave an error message in pParse->zErrMsg. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 123 | ** |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 124 | ** The difference between this routine and sqliteFindTable() |
| 125 | ** is that this routine leaves an error message in pParse->zErrMsg |
| 126 | ** where sqliteFindTable() does not. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 127 | */ |
| 128 | Table *sqliteLocateTable(Parse *pParse, const char *zName, const char *zDbase){ |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 129 | Table *p; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 130 | |
| 131 | p = sqliteFindTable(pParse->db, zName, zDbase); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 132 | if( p==0 ){ |
| 133 | if( zDbase ){ |
| 134 | sqliteErrorMsg(pParse, "no such table: %s.%s", zDbase, zName); |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 135 | }else if( sqliteFindTable(pParse->db, zName, 0)!=0 ){ |
drh | f0f258b | 2003-04-21 18:48:45 +0000 | [diff] [blame] | 136 | sqliteErrorMsg(pParse, "table \"%s\" is not in database \"%s\"", |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 137 | zName, zDbase); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 138 | }else{ |
| 139 | sqliteErrorMsg(pParse, "no such table: %s", zName); |
| 140 | } |
| 141 | } |
| 142 | return p; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | ** Locate the in-memory structure that describes |
| 147 | ** a particular index given the name of that index |
| 148 | ** and the name of the database that contains the index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 149 | ** Return NULL if not found. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 150 | ** |
| 151 | ** If zDatabase is 0, all databases are searched for the |
| 152 | ** table and the first matching index is returned. (No checking |
| 153 | ** for duplicate index names is done.) The search order is |
| 154 | ** TEMP first, then MAIN, then any auxiliary databases added |
| 155 | ** using the ATTACH command. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 156 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 157 | Index *sqliteFindIndex(sqlite *db, const char *zName, const char *zDb){ |
| 158 | Index *p = 0; |
| 159 | int i; |
| 160 | for(i=0; i<db->nDb; i++){ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 161 | int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
| 162 | if( zDb && sqliteStrICmp(zDb, db->aDb[j].zName) ) continue; |
| 163 | p = sqliteHashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 164 | if( p ) break; |
| 165 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 166 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
| 170 | ** Remove the given index from the index hash table, and free |
| 171 | ** its memory structures. |
| 172 | ** |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 173 | ** The index is removed from the database hash tables but |
| 174 | ** it is not unlinked from the Table that it indexes. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 175 | ** Unlinking from the Table must be done by the calling function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 176 | */ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 177 | static void sqliteDeleteIndex(sqlite *db, Index *p){ |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 178 | Index *pOld; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 179 | |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 180 | assert( db!=0 && p->zName!=0 ); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 181 | pOld = sqliteHashInsert(&db->aDb[p->iDb].idxHash, p->zName, |
| 182 | strlen(p->zName)+1, 0); |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 183 | if( pOld!=0 && pOld!=p ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 184 | sqliteHashInsert(&db->aDb[p->iDb].idxHash, pOld->zName, |
| 185 | strlen(pOld->zName)+1, pOld); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 186 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 187 | sqliteFree(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 191 | ** Unlink the given index from its table, then remove |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 192 | ** the index from the index hash table and free its memory |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 193 | ** structures. |
| 194 | */ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 195 | void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 196 | if( pIndex->pTable->pIndex==pIndex ){ |
| 197 | pIndex->pTable->pIndex = pIndex->pNext; |
| 198 | }else{ |
| 199 | Index *p; |
| 200 | for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
| 201 | if( p && p->pNext==pIndex ){ |
| 202 | p->pNext = pIndex->pNext; |
| 203 | } |
| 204 | } |
| 205 | sqliteDeleteIndex(db, pIndex); |
| 206 | } |
| 207 | |
| 208 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 209 | ** Erase all schema information from the in-memory hash tables of |
| 210 | ** database connection. This routine is called to reclaim memory |
| 211 | ** before the connection closes. It is also called during a rollback |
| 212 | ** if there were schema changes during the transaction. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 213 | ** |
| 214 | ** If iDb<=0 then reset the internal schema tables for all database |
| 215 | ** files. If iDb>=2 then reset the internal schema for only the |
jplyon | cfa5684 | 2004-01-19 04:55:56 +0000 | [diff] [blame] | 216 | ** single file indicated. |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 217 | */ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 218 | void sqliteResetInternalSchema(sqlite *db, int iDb){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 219 | HashElem *pElem; |
| 220 | Hash temp1; |
| 221 | Hash temp2; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 222 | int i, j; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 223 | |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 224 | assert( iDb>=0 && iDb<db->nDb ); |
| 225 | db->flags &= ~SQLITE_Initialized; |
| 226 | for(i=iDb; i<db->nDb; i++){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 227 | Db *pDb = &db->aDb[i]; |
| 228 | temp1 = pDb->tblHash; |
| 229 | temp2 = pDb->trigHash; |
| 230 | sqliteHashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0); |
| 231 | sqliteHashClear(&pDb->aFKey); |
| 232 | sqliteHashClear(&pDb->idxHash); |
| 233 | for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ |
| 234 | Trigger *pTrigger = sqliteHashData(pElem); |
| 235 | sqliteDeleteTrigger(pTrigger); |
| 236 | } |
| 237 | sqliteHashClear(&temp2); |
| 238 | sqliteHashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0); |
| 239 | for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ |
| 240 | Table *pTab = sqliteHashData(pElem); |
| 241 | sqliteDeleteTable(db, pTab); |
| 242 | } |
| 243 | sqliteHashClear(&temp1); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 244 | DbClearProperty(db, i, DB_SchemaLoaded); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 245 | if( iDb>0 ) return; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 246 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 247 | assert( iDb==0 ); |
| 248 | db->flags &= ~SQLITE_InternChanges; |
| 249 | |
| 250 | /* If one or more of the auxiliary database files has been closed, |
| 251 | ** then remove then from the auxiliary database list. We take the |
| 252 | ** opportunity to do this here since we have just deleted all of the |
| 253 | ** schema hash tables and therefore do not have to make any changes |
| 254 | ** to any of those tables. |
| 255 | */ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 256 | for(i=0; i<db->nDb; i++){ |
| 257 | struct Db *pDb = &db->aDb[i]; |
| 258 | if( pDb->pBt==0 ){ |
| 259 | if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux); |
| 260 | pDb->pAux = 0; |
| 261 | } |
| 262 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 263 | for(i=j=2; i<db->nDb; i++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 264 | struct Db *pDb = &db->aDb[i]; |
| 265 | if( pDb->pBt==0 ){ |
| 266 | sqliteFree(pDb->zName); |
| 267 | pDb->zName = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 268 | continue; |
| 269 | } |
| 270 | if( j<i ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 271 | db->aDb[j] = db->aDb[i]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 272 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 273 | j++; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 274 | } |
| 275 | memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j])); |
| 276 | db->nDb = j; |
| 277 | if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ |
| 278 | memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); |
| 279 | sqliteFree(db->aDb); |
| 280 | db->aDb = db->aDbStatic; |
| 281 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
| 285 | ** This routine is called whenever a rollback occurs. If there were |
| 286 | ** schema changes during the transaction, then we have to reset the |
| 287 | ** internal hash tables and reload them from disk. |
| 288 | */ |
| 289 | void sqliteRollbackInternalChanges(sqlite *db){ |
| 290 | if( db->flags & SQLITE_InternChanges ){ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 291 | sqliteResetInternalSchema(db, 0); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | ** This routine is called when a commit occurs. |
| 297 | */ |
| 298 | void sqliteCommitInternalChanges(sqlite *db){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 299 | db->aDb[0].schema_cookie = db->next_cookie; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 300 | db->flags &= ~SQLITE_InternChanges; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 305 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 306 | ** |
| 307 | ** This routine just deletes the data structure. It does not unlink |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 308 | ** the table data structure from the hash table. Nor does it remove |
| 309 | ** foreign keys from the sqlite.aFKey hash table. But it does destroy |
| 310 | ** memory structures of the indices and foreign keys associated with |
| 311 | ** the table. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 312 | ** |
| 313 | ** Indices associated with the table are unlinked from the "db" |
| 314 | ** data structure if db!=NULL. If db==NULL, indices attached to |
| 315 | ** the table are deleted, but it is assumed they have already been |
| 316 | ** unlinked. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 317 | */ |
| 318 | void sqliteDeleteTable(sqlite *db, Table *pTable){ |
| 319 | int i; |
| 320 | Index *pIndex, *pNext; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 321 | FKey *pFKey, *pNextFKey; |
| 322 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 323 | if( pTable==0 ) return; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 324 | |
| 325 | /* Delete all indices associated with this table |
| 326 | */ |
| 327 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 328 | pNext = pIndex->pNext; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 329 | assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) ); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 330 | sqliteDeleteIndex(db, pIndex); |
| 331 | } |
| 332 | |
| 333 | /* Delete all foreign keys associated with this table. The keys |
| 334 | ** should have already been unlinked from the db->aFKey hash table |
| 335 | */ |
| 336 | for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){ |
| 337 | pNextFKey = pFKey->pNextFrom; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 338 | assert( pTable->iDb<db->nDb ); |
| 339 | assert( sqliteHashFind(&db->aDb[pTable->iDb].aFKey, |
| 340 | pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey ); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 341 | sqliteFree(pFKey); |
| 342 | } |
| 343 | |
| 344 | /* Delete the Table structure itself. |
| 345 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 346 | for(i=0; i<pTable->nCol; i++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 347 | sqliteFree(pTable->aCol[i].zName); |
| 348 | sqliteFree(pTable->aCol[i].zDflt); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 349 | sqliteFree(pTable->aCol[i].zType); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 350 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 351 | sqliteFree(pTable->zName); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 352 | sqliteFree(pTable->aCol); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 353 | sqliteSelectDelete(pTable->pSelect); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 354 | sqliteFree(pTable); |
| 355 | } |
| 356 | |
| 357 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 358 | ** Unlink the given table from the hash tables and the delete the |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 359 | ** table structure with all its indices and foreign keys. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 360 | */ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 361 | static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){ |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 362 | Table *pOld; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 363 | FKey *pF1, *pF2; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 364 | int i = p->iDb; |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 365 | assert( db!=0 ); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 366 | pOld = sqliteHashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0); |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 367 | assert( pOld==0 || pOld==p ); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 368 | for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){ |
| 369 | int nTo = strlen(pF1->zTo) + 1; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 370 | pF2 = sqliteHashFind(&db->aDb[i].aFKey, pF1->zTo, nTo); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 371 | if( pF2==pF1 ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 372 | sqliteHashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 373 | }else{ |
| 374 | while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; } |
| 375 | if( pF2 ){ |
| 376 | pF2->pNextTo = pF1->pNextTo; |
| 377 | } |
| 378 | } |
| 379 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 380 | sqliteDeleteTable(db, p); |
| 381 | } |
| 382 | |
| 383 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 384 | ** Construct the name of a user table or index from a token. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 385 | ** |
| 386 | ** Space to hold the name is obtained from sqliteMalloc() and must |
| 387 | ** be freed by the calling function. |
| 388 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 389 | char *sqliteTableNameFromToken(Token *pName){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 390 | char *zName = sqliteStrNDup(pName->z, pName->n); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 391 | sqliteDequote(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 392 | return zName; |
| 393 | } |
| 394 | |
| 395 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 396 | ** Generate code to open the appropriate master table. The table |
| 397 | ** opened will be SQLITE_MASTER for persistent tables and |
| 398 | ** SQLITE_TEMP_MASTER for temporary tables. The table is opened |
| 399 | ** on cursor 0. |
| 400 | */ |
| 401 | void sqliteOpenMasterTable(Vdbe *v, int isTemp){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 402 | sqliteVdbeAddOp(v, OP_Integer, isTemp, 0); |
| 403 | sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 407 | ** Begin constructing a new table representation in memory. This is |
| 408 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 409 | ** to a CREATE TABLE statement. In particular, this routine is called |
| 410 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 411 | ** pStart token is the CREATE and pName is the table name. The isTemp |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 412 | ** flag is true if the table should be stored in the auxiliary database |
| 413 | ** file instead of in the main database file. This is normally the case |
| 414 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 415 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 416 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 417 | ** The new table record is initialized and put in pParse->pNewTable. |
| 418 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 419 | ** routines will be called to add more information to this record. |
| 420 | ** At the end of the CREATE TABLE statement, the sqliteEndTable() routine |
| 421 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 422 | */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 423 | void sqliteStartTable( |
| 424 | Parse *pParse, /* Parser context */ |
| 425 | Token *pStart, /* The "CREATE" token */ |
| 426 | Token *pName, /* Name of table or view to create */ |
| 427 | int isTemp, /* True if this is a TEMP table */ |
| 428 | int isView /* True if this is a VIEW */ |
| 429 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 430 | Table *pTable; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 431 | Index *pIdx; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 432 | char *zName; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 433 | sqlite *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 434 | Vdbe *v; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 435 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 436 | |
| 437 | pParse->sFirstToken = *pStart; |
| 438 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 439 | if( zName==0 ) return; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 440 | if( db->init.iDb==1 ) isTemp = 1; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 441 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 442 | assert( (isTemp & 1)==isTemp ); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 443 | { |
| 444 | int code; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 445 | char *zDb = isTemp ? "temp" : "main"; |
| 446 | if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
| 447 | sqliteFree(zName); |
| 448 | return; |
| 449 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 450 | if( isView ){ |
| 451 | if( isTemp ){ |
| 452 | code = SQLITE_CREATE_TEMP_VIEW; |
| 453 | }else{ |
| 454 | code = SQLITE_CREATE_VIEW; |
| 455 | } |
| 456 | }else{ |
| 457 | if( isTemp ){ |
| 458 | code = SQLITE_CREATE_TEMP_TABLE; |
| 459 | }else{ |
| 460 | code = SQLITE_CREATE_TABLE; |
| 461 | } |
| 462 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 463 | if( sqliteAuthCheck(pParse, code, zName, 0, zDb) ){ |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 464 | sqliteFree(zName); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | } |
| 468 | #endif |
| 469 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 470 | |
| 471 | /* Before trying to create a temporary table, make sure the Btree for |
| 472 | ** holding temporary tables is open. |
| 473 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 474 | if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){ |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 475 | int rc = sqliteBtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 476 | if( rc!=SQLITE_OK ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 477 | sqliteErrorMsg(pParse, "unable to open a temporary database " |
| 478 | "file for storing temporary tables"); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 479 | pParse->nErr++; |
| 480 | return; |
| 481 | } |
| 482 | if( db->flags & SQLITE_InTrans ){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 483 | rc = sqliteBtreeBeginTrans(db->aDb[1].pBt); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 484 | if( rc!=SQLITE_OK ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 485 | sqliteErrorMsg(pParse, "unable to get a write lock on " |
| 486 | "the temporary database file"); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 487 | pParse->nErr++; |
| 488 | return; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /* Make sure the new table name does not collide with an existing |
| 494 | ** index or table name. Issue an error message if it does. |
| 495 | ** |
| 496 | ** If we are re-reading the sqlite_master table because of a schema |
| 497 | ** change and a new permanent table is found whose name collides with |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 498 | ** an existing temporary table, that is not an error. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 499 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 500 | pTable = sqliteFindTable(db, zName, 0); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 501 | iDb = isTemp ? 1 : db->init.iDb; |
| 502 | if( pTable!=0 && (pTable->iDb==iDb || !db->init.busy) ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 503 | sqliteErrorMsg(pParse, "table %T already exists", pName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 504 | sqliteFree(zName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 505 | return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 506 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 507 | if( (pIdx = sqliteFindIndex(db, zName, 0))!=0 && |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 508 | (pIdx->iDb==0 || !db->init.busy) ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 509 | sqliteErrorMsg(pParse, "there is already an index named %s", zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 510 | sqliteFree(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 511 | return; |
| 512 | } |
| 513 | pTable = sqliteMalloc( sizeof(Table) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 514 | if( pTable==0 ){ |
| 515 | sqliteFree(zName); |
| 516 | return; |
| 517 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 518 | pTable->zName = zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 519 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 520 | pTable->aCol = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 521 | pTable->iPKey = -1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 522 | pTable->pIndex = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 523 | pTable->iDb = iDb; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 524 | if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 525 | pParse->pNewTable = pTable; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 526 | |
| 527 | /* Begin generating the code that will insert the table record into |
| 528 | ** the SQLITE_MASTER table. Note in particular that we must go ahead |
| 529 | ** and allocate the record number for the table entry now. Before any |
| 530 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
| 531 | ** indices to be created and the table record must come before the |
| 532 | ** indices. Hence, the record number for the table must be allocated |
| 533 | ** now. |
| 534 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 535 | if( !db->init.busy && (v = sqliteGetVdbe(pParse))!=0 ){ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 536 | sqliteBeginWriteOperation(pParse, 0, isTemp); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 537 | if( !isTemp ){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 538 | sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0); |
| 539 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 1); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 540 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 541 | sqliteOpenMasterTable(v, isTemp); |
| 542 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 543 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 544 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 545 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 546 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* |
| 550 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 551 | ** |
| 552 | ** The parser calls this routine once for each column declaration |
| 553 | ** in a CREATE TABLE statement. sqliteStartTable() gets called |
| 554 | ** first to get things going. Then this routine is called for each |
| 555 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 556 | */ |
| 557 | void sqliteAddColumn(Parse *pParse, Token *pName){ |
| 558 | Table *p; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 559 | int i; |
| 560 | char *z = 0; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 561 | Column *pCol; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 562 | if( (p = pParse->pNewTable)==0 ) return; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 563 | sqliteSetNString(&z, pName->z, pName->n, 0); |
| 564 | if( z==0 ) return; |
| 565 | sqliteDequote(z); |
| 566 | for(i=0; i<p->nCol; i++){ |
| 567 | if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 568 | sqliteErrorMsg(pParse, "duplicate column name: %s", z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 569 | sqliteFree(z); |
| 570 | return; |
| 571 | } |
| 572 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 573 | if( (p->nCol & 0x7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 574 | Column *aNew; |
| 575 | aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
| 576 | if( aNew==0 ) return; |
| 577 | p->aCol = aNew; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 578 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 579 | pCol = &p->aCol[p->nCol]; |
| 580 | memset(pCol, 0, sizeof(p->aCol[0])); |
| 581 | pCol->zName = z; |
| 582 | pCol->sortOrder = SQLITE_SO_NUM; |
| 583 | p->nCol++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 587 | ** This routine is called by the parser while in the middle of |
| 588 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 589 | ** been seen on a column. This routine sets the notNull flag on |
| 590 | ** the column currently under construction. |
| 591 | */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 592 | void sqliteAddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 593 | Table *p; |
| 594 | int i; |
| 595 | if( (p = pParse->pNewTable)==0 ) return; |
| 596 | i = p->nCol-1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 597 | if( i>=0 ) p->aCol[i].notNull = onError; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* |
| 601 | ** This routine is called by the parser while in the middle of |
| 602 | ** parsing a CREATE TABLE statement. The pFirst token is the first |
| 603 | ** token in the sequence of tokens that describe the type of the |
| 604 | ** column currently under construction. pLast is the last token |
| 605 | ** in the sequence. Use this information to construct a string |
| 606 | ** that contains the typename of the column and store that string |
| 607 | ** in zType. |
| 608 | */ |
| 609 | void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){ |
| 610 | Table *p; |
| 611 | int i, j; |
| 612 | int n; |
| 613 | char *z, **pz; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 614 | Column *pCol; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 615 | if( (p = pParse->pNewTable)==0 ) return; |
| 616 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 617 | if( i<0 ) return; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 618 | pCol = &p->aCol[i]; |
| 619 | pz = &pCol->zType; |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 620 | n = pLast->n + Addr(pLast->z) - Addr(pFirst->z); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 621 | sqliteSetNString(pz, pFirst->z, n, 0); |
| 622 | z = *pz; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 623 | if( z==0 ) return; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 624 | for(i=j=0; z[i]; i++){ |
| 625 | int c = z[i]; |
| 626 | if( isspace(c) ) continue; |
| 627 | z[j++] = c; |
| 628 | } |
| 629 | z[j] = 0; |
drh | 3d037a9 | 2002-08-15 01:26:09 +0000 | [diff] [blame] | 630 | if( pParse->db->file_format>=4 ){ |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 631 | pCol->sortOrder = sqliteCollateType(z, n); |
| 632 | }else{ |
| 633 | pCol->sortOrder = SQLITE_SO_NUM; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 634 | } |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 638 | ** The given token is the default value for the last column added to |
| 639 | ** the table currently under construction. If "minusFlag" is true, it |
| 640 | ** means the value token was preceded by a minus sign. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 641 | ** |
| 642 | ** This routine is called by the parser while in the middle of |
| 643 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 644 | */ |
| 645 | void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ |
| 646 | Table *p; |
| 647 | int i; |
| 648 | char **pz; |
| 649 | if( (p = pParse->pNewTable)==0 ) return; |
| 650 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 651 | if( i<0 ) return; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 652 | pz = &p->aCol[i].zDflt; |
| 653 | if( minusFlag ){ |
| 654 | sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0); |
| 655 | }else{ |
| 656 | sqliteSetNString(pz, pVal->z, pVal->n, 0); |
| 657 | } |
| 658 | sqliteDequote(*pz); |
| 659 | } |
| 660 | |
| 661 | /* |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 662 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
| 663 | ** of columns that form the primary key. If pList is NULL, then the |
| 664 | ** most recently added column of the table is the primary key. |
| 665 | ** |
| 666 | ** A table can have at most one primary key. If the table already has |
| 667 | ** a primary key (and this is the second primary key) then create an |
| 668 | ** error. |
| 669 | ** |
| 670 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
| 671 | ** then we will try to use that column as the row id. (Exception: |
| 672 | ** For backwards compatibility with older databases, do not do this |
| 673 | ** if the file format version number is less than 1.) Set the Table.iPKey |
| 674 | ** field of the table under construction to be the index of the |
| 675 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 676 | ** no INTEGER PRIMARY KEY. |
| 677 | ** |
| 678 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 679 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 680 | */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 681 | void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 682 | Table *pTab = pParse->pNewTable; |
| 683 | char *zType = 0; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 684 | int iCol = -1, i; |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 685 | if( pTab==0 ) goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 686 | if( pTab->hasPrimKey ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 687 | sqliteErrorMsg(pParse, |
| 688 | "table \"%s\" has more than one primary key", pTab->zName); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 689 | goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 690 | } |
| 691 | pTab->hasPrimKey = 1; |
| 692 | if( pList==0 ){ |
| 693 | iCol = pTab->nCol - 1; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 694 | pTab->aCol[iCol].isPrimKey = 1; |
| 695 | }else{ |
| 696 | for(i=0; i<pList->nId; i++){ |
| 697 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
drh | 3ca6515 | 2003-08-24 16:38:17 +0000 | [diff] [blame] | 698 | if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ) break; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 699 | } |
| 700 | if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 701 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 702 | if( pList->nId>1 ) iCol = -1; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 703 | } |
| 704 | if( iCol>=0 && iCol<pTab->nCol ){ |
| 705 | zType = pTab->aCol[iCol].zType; |
| 706 | } |
| 707 | if( pParse->db->file_format>=1 && |
| 708 | zType && sqliteStrICmp(zType, "INTEGER")==0 ){ |
| 709 | pTab->iPKey = iCol; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 710 | pTab->keyConf = onError; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 711 | }else{ |
drh | 4925ca0 | 2003-11-27 00:48:57 +0000 | [diff] [blame] | 712 | sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 713 | pList = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 714 | } |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 715 | |
| 716 | primary_key_exit: |
| 717 | sqliteIdListDelete(pList); |
| 718 | return; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /* |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 722 | ** Return the appropriate collating type given a type name. |
| 723 | ** |
| 724 | ** The collation type is text (SQLITE_SO_TEXT) if the type |
| 725 | ** name contains the character stream "text" or "blob" or |
| 726 | ** "clob". Any other type name is collated as numeric |
| 727 | ** (SQLITE_SO_NUM). |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 728 | */ |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 729 | int sqliteCollateType(const char *zType, int nType){ |
| 730 | int i; |
drh | d383401 | 2004-02-22 18:56:49 +0000 | [diff] [blame] | 731 | for(i=0; i<nType-3; i++){ |
| 732 | int c = *(zType++) | 0x60; |
| 733 | if( (c=='b' || c=='c') && sqliteStrNICmp(zType, "lob", 3)==0 ){ |
| 734 | return SQLITE_SO_TEXT; |
| 735 | } |
| 736 | if( c=='c' && sqliteStrNICmp(zType, "har", 3)==0 ){ |
| 737 | return SQLITE_SO_TEXT; |
| 738 | } |
| 739 | if( c=='t' && sqliteStrNICmp(zType, "ext", 3)==0 ){ |
| 740 | return SQLITE_SO_TEXT; |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 741 | } |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 742 | } |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 743 | return SQLITE_SO_NUM; |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* |
| 747 | ** This routine is called by the parser while in the middle of |
| 748 | ** parsing a CREATE TABLE statement. A "COLLATE" clause has |
| 749 | ** been seen on a column. This routine sets the Column.sortOrder on |
| 750 | ** the column currently under construction. |
| 751 | */ |
| 752 | void sqliteAddCollateType(Parse *pParse, int collType){ |
| 753 | Table *p; |
| 754 | int i; |
| 755 | if( (p = pParse->pNewTable)==0 ) return; |
| 756 | i = p->nCol-1; |
| 757 | if( i>=0 ) p->aCol[i].sortOrder = collType; |
| 758 | } |
| 759 | |
| 760 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 761 | ** Come up with a new random value for the schema cookie. Make sure |
| 762 | ** the new value is different from the old. |
| 763 | ** |
| 764 | ** The schema cookie is used to determine when the schema for the |
| 765 | ** database changes. After each schema change, the cookie value |
| 766 | ** changes. When a process first reads the schema it records the |
| 767 | ** cookie. Thereafter, whenever it goes to access the database, |
| 768 | ** it checks the cookie to make sure the schema has not changed |
| 769 | ** since it was last read. |
| 770 | ** |
| 771 | ** This plan is not completely bullet-proof. It is possible for |
| 772 | ** the schema to change multiple times and for the cookie to be |
| 773 | ** set back to prior value. But schema changes are infrequent |
| 774 | ** and the probability of hitting the same cookie value is only |
| 775 | ** 1 chance in 2^32. So we're safe enough. |
| 776 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 777 | void sqliteChangeCookie(sqlite *db, Vdbe *v){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 778 | if( db->next_cookie==db->aDb[0].schema_cookie ){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 779 | unsigned char r; |
| 780 | sqliteRandomness(1, &r); |
| 781 | db->next_cookie = db->aDb[0].schema_cookie + r + 1; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 782 | db->flags |= SQLITE_InternChanges; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 783 | sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0); |
| 784 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | /* |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 789 | ** Measure the number of characters needed to output the given |
| 790 | ** identifier. The number returned includes any quotes used |
| 791 | ** but does not include the null terminator. |
| 792 | */ |
| 793 | static int identLength(const char *z){ |
| 794 | int n; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 795 | int needQuote = 0; |
| 796 | for(n=0; *z; n++, z++){ |
| 797 | if( *z=='\'' ){ n++; needQuote=1; } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 798 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 799 | return n + needQuote*2; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /* |
| 803 | ** Write an identifier onto the end of the given string. Add |
| 804 | ** quote characters as needed. |
| 805 | */ |
| 806 | static void identPut(char *z, int *pIdx, char *zIdent){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 807 | int i, j, needQuote; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 808 | i = *pIdx; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 809 | for(j=0; zIdent[j]; j++){ |
| 810 | if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
| 811 | } |
| 812 | needQuote = zIdent[j]!=0 || isdigit(zIdent[0]) |
| 813 | || sqliteKeywordCode(zIdent, j)!=TK_ID; |
| 814 | if( needQuote ) z[i++] = '\''; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 815 | for(j=0; zIdent[j]; j++){ |
| 816 | z[i++] = zIdent[j]; |
| 817 | if( zIdent[j]=='\'' ) z[i++] = '\''; |
| 818 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 819 | if( needQuote ) z[i++] = '\''; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 820 | z[i] = 0; |
| 821 | *pIdx = i; |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | ** Generate a CREATE TABLE statement appropriate for the given |
| 826 | ** table. Memory to hold the text of the statement is obtained |
| 827 | ** from sqliteMalloc() and must be freed by the calling function. |
| 828 | */ |
| 829 | static char *createTableStmt(Table *p){ |
| 830 | int i, k, n; |
| 831 | char *zStmt; |
| 832 | char *zSep, *zSep2, *zEnd; |
| 833 | n = 0; |
| 834 | for(i=0; i<p->nCol; i++){ |
| 835 | n += identLength(p->aCol[i].zName); |
| 836 | } |
| 837 | n += identLength(p->zName); |
| 838 | if( n<40 ){ |
| 839 | zSep = ""; |
| 840 | zSep2 = ","; |
| 841 | zEnd = ")"; |
| 842 | }else{ |
| 843 | zSep = "\n "; |
| 844 | zSep2 = ",\n "; |
| 845 | zEnd = "\n)"; |
| 846 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 847 | n += 35 + 6*p->nCol; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 848 | zStmt = sqliteMallocRaw( n ); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 849 | if( zStmt==0 ) return 0; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 850 | strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE "); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 851 | k = strlen(zStmt); |
| 852 | identPut(zStmt, &k, p->zName); |
| 853 | zStmt[k++] = '('; |
| 854 | for(i=0; i<p->nCol; i++){ |
| 855 | strcpy(&zStmt[k], zSep); |
| 856 | k += strlen(&zStmt[k]); |
| 857 | zSep = zSep2; |
| 858 | identPut(zStmt, &k, p->aCol[i].zName); |
| 859 | } |
| 860 | strcpy(&zStmt[k], zEnd); |
| 861 | return zStmt; |
| 862 | } |
| 863 | |
| 864 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 865 | ** This routine is called to report the final ")" that terminates |
| 866 | ** a CREATE TABLE statement. |
| 867 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 868 | ** The table structure that other action routines have been building |
| 869 | ** is added to the internal hash tables, assuming no errors have |
| 870 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 871 | ** |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 872 | ** An entry for the table is made in the master table on disk, unless |
| 873 | ** this is a temporary table or db->init.busy==1. When db->init.busy==1 |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 874 | ** it means we are reading the sqlite_master table because we just |
| 875 | ** connected to the database or because the sqlite_master table has |
| 876 | ** recently changes, so the entry for this table already exists in |
| 877 | ** the sqlite_master table. We do not want to create it again. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 878 | ** |
| 879 | ** If the pSelect argument is not NULL, it means that this routine |
| 880 | ** was called to create a table generated from a |
| 881 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
| 882 | ** the new table will match the result set of the SELECT. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 883 | */ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 884 | void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 885 | Table *p; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 886 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 887 | |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 888 | if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 889 | p = pParse->pNewTable; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 890 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 891 | |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 892 | /* If the table is generated from a SELECT, then construct the |
| 893 | ** list of columns and the text of the table. |
| 894 | */ |
| 895 | if( pSelect ){ |
| 896 | Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect); |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 897 | if( pSelTab==0 ) return; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 898 | assert( p->aCol==0 ); |
| 899 | p->nCol = pSelTab->nCol; |
| 900 | p->aCol = pSelTab->aCol; |
| 901 | pSelTab->nCol = 0; |
| 902 | pSelTab->aCol = 0; |
| 903 | sqliteDeleteTable(0, pSelTab); |
| 904 | } |
| 905 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 906 | /* If the db->init.busy is 1 it means we are reading the SQL off the |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 907 | ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
| 908 | ** So do not write to the disk again. Extract the root page number |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 909 | ** for the table from the db->init.newTnum field. (The page number |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 910 | ** should have been put there by the sqliteOpenCb routine.) |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 911 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 912 | if( db->init.busy ){ |
| 913 | p->tnum = db->init.newTnum; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 914 | } |
| 915 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 916 | /* If not initializing, then create a record for the new table |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 917 | ** in the SQLITE_MASTER table of the database. The record number |
| 918 | ** for the new table entry should already be on the stack. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 919 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 920 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
| 921 | ** file instead of into the main database file. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 922 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 923 | if( !db->init.busy ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 924 | int n; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 925 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 926 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 927 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 928 | if( v==0 ) return; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 929 | if( p->pSelect==0 ){ |
| 930 | /* A regular table */ |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame^] | 931 | sqliteVdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 932 | }else{ |
| 933 | /* A view */ |
| 934 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 935 | } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 936 | p->tnum = 0; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 937 | sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame^] | 938 | sqliteVdbeOp3(v, OP_String, 0, 0, p->pSelect==0?"table":"view", P3_STATIC); |
| 939 | sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0); |
| 940 | sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 941 | sqliteVdbeAddOp(v, OP_Dup, 4, 0); |
| 942 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 943 | if( pSelect ){ |
| 944 | char *z = createTableStmt(p); |
| 945 | n = z ? strlen(z) : 0; |
| 946 | sqliteVdbeChangeP3(v, -1, z, n); |
| 947 | sqliteFree(z); |
| 948 | }else{ |
| 949 | assert( pEnd!=0 ); |
| 950 | n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1; |
| 951 | sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n); |
| 952 | } |
| 953 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0); |
| 954 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 955 | if( !p->iDb ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 956 | sqliteChangeCookie(db, v); |
| 957 | } |
| 958 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 959 | if( pSelect ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 960 | sqliteVdbeAddOp(v, OP_Integer, p->iDb, 0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 961 | sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 962 | pParse->nTab = 2; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 963 | sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 964 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 965 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 966 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 967 | |
| 968 | /* Add the table to the in-memory representation of the database. |
| 969 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 970 | if( pParse->explain==0 && pParse->nErr==0 ){ |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 971 | Table *pOld; |
| 972 | FKey *pFKey; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 973 | pOld = sqliteHashInsert(&db->aDb[p->iDb].tblHash, |
| 974 | p->zName, strlen(p->zName)+1, p); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 975 | if( pOld ){ |
| 976 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 977 | return; |
| 978 | } |
| 979 | for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 980 | int nTo = strlen(pFKey->zTo) + 1; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 981 | pFKey->pNextTo = sqliteHashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo); |
| 982 | sqliteHashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 983 | } |
| 984 | pParse->pNewTable = 0; |
| 985 | db->nTable++; |
| 986 | db->flags |= SQLITE_InternChanges; |
| 987 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 991 | ** The parser calls this routine in order to create a new VIEW |
| 992 | */ |
| 993 | void sqliteCreateView( |
| 994 | Parse *pParse, /* The parsing context */ |
| 995 | Token *pBegin, /* The CREATE token that begins the statement */ |
| 996 | Token *pName, /* The token that holds the name of the view */ |
drh | 6276c1c | 2002-07-08 22:03:32 +0000 | [diff] [blame] | 997 | Select *pSelect, /* A SELECT statement that will become the new view */ |
| 998 | int isTemp /* TRUE for a TEMPORARY view */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 999 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1000 | Table *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1001 | int n; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1002 | const char *z; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1003 | Token sEnd; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1004 | DbFixer sFix; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1005 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1006 | sqliteStartTable(pParse, pBegin, pName, isTemp, 1); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1007 | p = pParse->pNewTable; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 1008 | if( p==0 || pParse->nErr ){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1009 | sqliteSelectDelete(pSelect); |
| 1010 | return; |
| 1011 | } |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1012 | if( sqliteFixInit(&sFix, pParse, p->iDb, "view", pName) |
| 1013 | && sqliteFixSelect(&sFix, pSelect) |
| 1014 | ){ |
| 1015 | sqliteSelectDelete(pSelect); |
| 1016 | return; |
| 1017 | } |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 1018 | |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1019 | /* Make a copy of the entire SELECT statement that defines the view. |
| 1020 | ** This will force all the Expr.token.z values to be dynamically |
| 1021 | ** allocated rather than point to the input string - which means that |
| 1022 | ** they will persist after the current sqlite_exec() call returns. |
| 1023 | */ |
| 1024 | p->pSelect = sqliteSelectDup(pSelect); |
| 1025 | sqliteSelectDelete(pSelect); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1026 | if( !pParse->db->init.busy ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1027 | sqliteViewGetColumnNames(pParse, p); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1028 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1029 | |
| 1030 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
| 1031 | ** the end. |
| 1032 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1033 | sEnd = pParse->sLastToken; |
| 1034 | if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){ |
| 1035 | sEnd.z += sEnd.n; |
| 1036 | } |
| 1037 | sEnd.n = 0; |
| 1038 | n = ((int)sEnd.z) - (int)pBegin->z; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1039 | z = pBegin->z; |
| 1040 | while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; } |
| 1041 | sEnd.z = &z[n-1]; |
| 1042 | sEnd.n = 1; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1043 | |
| 1044 | /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */ |
| 1045 | sqliteEndTable(pParse, &sEnd, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1046 | return; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1047 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1048 | |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1049 | /* |
| 1050 | ** The Table structure pTable is really a VIEW. Fill in the names of |
| 1051 | ** the columns of the view in the pTable structure. Return the number |
jplyon | cfa5684 | 2004-01-19 04:55:56 +0000 | [diff] [blame] | 1052 | ** of errors. If an error is seen leave an error message in pParse->zErrMsg. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1053 | */ |
| 1054 | int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){ |
| 1055 | ExprList *pEList; |
| 1056 | Select *pSel; |
| 1057 | Table *pSelTab; |
| 1058 | int nErr = 0; |
| 1059 | |
| 1060 | assert( pTable ); |
| 1061 | |
| 1062 | /* A positive nCol means the columns names for this view are |
| 1063 | ** already known. |
| 1064 | */ |
| 1065 | if( pTable->nCol>0 ) return 0; |
| 1066 | |
| 1067 | /* A negative nCol is a special marker meaning that we are currently |
| 1068 | ** trying to compute the column names. If we enter this routine with |
| 1069 | ** a negative nCol, it means two or more views form a loop, like this: |
| 1070 | ** |
| 1071 | ** CREATE VIEW one AS SELECT * FROM two; |
| 1072 | ** CREATE VIEW two AS SELECT * FROM one; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 1073 | ** |
| 1074 | ** Actually, this error is caught previously and so the following test |
| 1075 | ** should always fail. But we will leave it in place just to be safe. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1076 | */ |
| 1077 | if( pTable->nCol<0 ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1078 | sqliteErrorMsg(pParse, "view %s is circularly defined", pTable->zName); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1079 | return 1; |
| 1080 | } |
| 1081 | |
| 1082 | /* If we get this far, it means we need to compute the table names. |
| 1083 | */ |
| 1084 | assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */ |
| 1085 | pSel = pTable->pSelect; |
| 1086 | |
| 1087 | /* Note that the call to sqliteResultSetOfSelect() will expand any |
| 1088 | ** "*" elements in this list. But we will need to restore the list |
| 1089 | ** back to its original configuration afterwards, so we save a copy of |
| 1090 | ** the original in pEList. |
| 1091 | */ |
| 1092 | pEList = pSel->pEList; |
| 1093 | pSel->pEList = sqliteExprListDup(pEList); |
| 1094 | if( pSel->pEList==0 ){ |
| 1095 | pSel->pEList = pEList; |
| 1096 | return 1; /* Malloc failed */ |
| 1097 | } |
| 1098 | pTable->nCol = -1; |
| 1099 | pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel); |
| 1100 | if( pSelTab ){ |
| 1101 | assert( pTable->aCol==0 ); |
| 1102 | pTable->nCol = pSelTab->nCol; |
| 1103 | pTable->aCol = pSelTab->aCol; |
| 1104 | pSelTab->nCol = 0; |
| 1105 | pSelTab->aCol = 0; |
| 1106 | sqliteDeleteTable(0, pSelTab); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1107 | DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1108 | }else{ |
| 1109 | pTable->nCol = 0; |
| 1110 | nErr++; |
| 1111 | } |
| 1112 | sqliteSelectUnbind(pSel); |
| 1113 | sqliteExprListDelete(pSel->pEList); |
| 1114 | pSel->pEList = pEList; |
| 1115 | return nErr; |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | ** Clear the column names from the VIEW pTable. |
| 1120 | ** |
| 1121 | ** This routine is called whenever any other table or view is modified. |
| 1122 | ** The view passed into this routine might depend directly or indirectly |
| 1123 | ** on the modified or deleted table so we need to clear the old column |
| 1124 | ** names so that they will be recomputed. |
| 1125 | */ |
| 1126 | static void sqliteViewResetColumnNames(Table *pTable){ |
| 1127 | int i; |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame^] | 1128 | Column *pCol; |
| 1129 | assert( pTable!=0 && pTable->pSelect!=0 ); |
| 1130 | for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){ |
| 1131 | sqliteFree(pCol->zName); |
| 1132 | sqliteFree(pCol->zDflt); |
| 1133 | sqliteFree(pCol->zType); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1134 | } |
| 1135 | sqliteFree(pTable->aCol); |
| 1136 | pTable->aCol = 0; |
| 1137 | pTable->nCol = 0; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1141 | ** Clear the column names from every VIEW in database idx. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1142 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1143 | static void sqliteViewResetAll(sqlite *db, int idx){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1144 | HashElem *i; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1145 | if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1146 | for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1147 | Table *pTab = sqliteHashData(i); |
| 1148 | if( pTab->pSelect ){ |
| 1149 | sqliteViewResetColumnNames(pTab); |
| 1150 | } |
| 1151 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1152 | DbClearProperty(db, idx, DB_UnresetViews); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1156 | ** Given a token, look up a table with that name. If not found, leave |
| 1157 | ** an error for the parser to find and return NULL. |
| 1158 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1159 | Table *sqliteTableFromToken(Parse *pParse, Token *pTok){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1160 | char *zName; |
| 1161 | Table *pTab; |
| 1162 | zName = sqliteTableNameFromToken(pTok); |
| 1163 | if( zName==0 ) return 0; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1164 | pTab = sqliteFindTable(pParse->db, zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1165 | sqliteFree(zName); |
| 1166 | if( pTab==0 ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1167 | sqliteErrorMsg(pParse, "no such table: %T", pTok); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1168 | } |
| 1169 | return pTab; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1174 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1175 | */ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1176 | void sqliteDropTable(Parse *pParse, Token *pName, int isView){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1177 | Table *pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1178 | Vdbe *v; |
| 1179 | int base; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1180 | sqlite *db = pParse->db; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1181 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1182 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1183 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1184 | pTable = sqliteTableFromToken(pParse, pName); |
| 1185 | if( pTable==0 ) return; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1186 | iDb = pTable->iDb; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1187 | assert( iDb>=0 && iDb<db->nDb ); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1188 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1189 | { |
| 1190 | int code; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1191 | const char *zTab = SCHEMA_TABLE(pTable->iDb); |
| 1192 | const char *zDb = db->aDb[pTable->iDb].zName; |
| 1193 | if( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
| 1194 | return; |
| 1195 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1196 | if( isView ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1197 | if( iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1198 | code = SQLITE_DROP_TEMP_VIEW; |
| 1199 | }else{ |
| 1200 | code = SQLITE_DROP_VIEW; |
| 1201 | } |
| 1202 | }else{ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1203 | if( iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1204 | code = SQLITE_DROP_TEMP_TABLE; |
| 1205 | }else{ |
| 1206 | code = SQLITE_DROP_TABLE; |
| 1207 | } |
| 1208 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1209 | if( sqliteAuthCheck(pParse, code, pTable->zName, 0, zDb) ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1210 | return; |
| 1211 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1212 | if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0, zDb) ){ |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 1213 | return; |
| 1214 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1215 | } |
| 1216 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1217 | if( pTable->readOnly ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1218 | sqliteErrorMsg(pParse, "table %s may not be dropped", pTable->zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1219 | pParse->nErr++; |
| 1220 | return; |
| 1221 | } |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1222 | if( isView && pTable->pSelect==0 ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1223 | sqliteErrorMsg(pParse, "use DROP TABLE to delete table %s", pTable->zName); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1224 | return; |
| 1225 | } |
| 1226 | if( !isView && pTable->pSelect ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1227 | sqliteErrorMsg(pParse, "use DROP VIEW to delete view %s", pTable->zName); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1228 | return; |
| 1229 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1230 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1231 | /* Generate code to remove the table from the master table |
| 1232 | ** on disk. |
| 1233 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1234 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1235 | if( v ){ |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 1236 | static VdbeOpList dropTable[] = { |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1237 | { OP_Rewind, 0, ADDR(8), 0}, |
| 1238 | { OP_String, 0, 0, 0}, /* 1 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1239 | { OP_MemStore, 1, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1240 | { OP_MemLoad, 1, 0, 0}, /* 3 */ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1241 | { OP_Column, 0, 2, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1242 | { OP_Ne, 0, ADDR(7), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1243 | { OP_Delete, 0, 0, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1244 | { OP_Next, 0, ADDR(3), 0}, /* 7 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1245 | }; |
| 1246 | Index *pIdx; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1247 | Trigger *pTrigger; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1248 | sqliteBeginWriteOperation(pParse, 0, pTable->iDb); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1249 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1250 | /* Drop all triggers associated with the table being dropped */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1251 | pTrigger = pTable->pTrigger; |
| 1252 | while( pTrigger ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1253 | assert( pTrigger->iDb==pTable->iDb || pTrigger->iDb==1 ); |
drh | 79a519c | 2003-05-17 19:04:03 +0000 | [diff] [blame] | 1254 | sqliteDropTriggerPtr(pParse, pTrigger, 1); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1255 | if( pParse->explain ){ |
| 1256 | pTrigger = pTrigger->pNext; |
| 1257 | }else{ |
| 1258 | pTrigger = pTable->pTrigger; |
| 1259 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1260 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* Drop all SQLITE_MASTER entries that refer to the table */ |
| 1263 | sqliteOpenMasterTable(v, pTable->iDb); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1264 | base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); |
| 1265 | sqliteVdbeChangeP3(v, base+1, pTable->zName, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1266 | |
| 1267 | /* Drop all SQLITE_TEMP_MASTER entries that refer to the table */ |
| 1268 | if( pTable->iDb!=1 ){ |
| 1269 | sqliteOpenMasterTable(v, 1); |
| 1270 | base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); |
| 1271 | sqliteVdbeChangeP3(v, base+1, pTable->zName, 0); |
| 1272 | } |
| 1273 | |
| 1274 | if( pTable->iDb==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1275 | sqliteChangeCookie(db, v); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1276 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1277 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1278 | if( !isView ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1279 | sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1280 | for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1281 | sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1282 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1283 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1284 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1287 | /* Delete the in-memory description of the table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1288 | ** |
| 1289 | ** Exception: if the SQL statement began with the EXPLAIN keyword, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1290 | ** then no changes should be made. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1291 | */ |
| 1292 | if( !pParse->explain ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1293 | sqliteUnlinkAndDeleteTable(db, pTable); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1294 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1295 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1296 | sqliteViewResetAll(db, iDb); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /* |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 1300 | ** This routine constructs a P3 string suitable for an OP_MakeIdxKey |
| 1301 | ** opcode and adds that P3 string to the most recently inserted instruction |
| 1302 | ** in the virtual machine. The P3 string consists of a single character |
| 1303 | ** for each column in the index pIdx of table pTab. If the column uses |
| 1304 | ** a numeric sort order, then the P3 string character corresponding to |
| 1305 | ** that column is 'n'. If the column uses a text sort order, then the |
| 1306 | ** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for |
| 1307 | ** additional information. See also the sqliteAddKeyType() routine. |
| 1308 | */ |
| 1309 | void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){ |
| 1310 | char *zType; |
| 1311 | Table *pTab; |
| 1312 | int i, n; |
| 1313 | assert( pIdx!=0 && pIdx->pTable!=0 ); |
| 1314 | pTab = pIdx->pTable; |
| 1315 | n = pIdx->nColumn; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1316 | zType = sqliteMallocRaw( n+1 ); |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 1317 | if( zType==0 ) return; |
| 1318 | for(i=0; i<n; i++){ |
| 1319 | int iCol = pIdx->aiColumn[i]; |
| 1320 | assert( iCol>=0 && iCol<pTab->nCol ); |
| 1321 | if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){ |
| 1322 | zType[i] = 't'; |
| 1323 | }else{ |
| 1324 | zType[i] = 'n'; |
| 1325 | } |
| 1326 | } |
| 1327 | zType[n] = 0; |
| 1328 | sqliteVdbeChangeP3(v, -1, zType, n); |
| 1329 | sqliteFree(zType); |
| 1330 | } |
| 1331 | |
| 1332 | /* |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1333 | ** This routine is called to create a new foreign key on the table |
| 1334 | ** currently under construction. pFromCol determines which columns |
| 1335 | ** in the current table point to the foreign key. If pFromCol==0 then |
| 1336 | ** connect the key to the last column inserted. pTo is the name of |
| 1337 | ** the table referred to. pToCol is a list of tables in the other |
| 1338 | ** pTo table that the foreign key points to. flags contains all |
| 1339 | ** information about the conflict resolution algorithms specified |
| 1340 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
| 1341 | ** |
| 1342 | ** An FKey structure is created and added to the table currently |
| 1343 | ** under construction in the pParse->pNewTable field. The new FKey |
| 1344 | ** is not linked into db->aFKey at this point - that does not happen |
| 1345 | ** until sqliteEndTable(). |
| 1346 | ** |
| 1347 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
| 1348 | ** to sqliteDeferForeignKey() might change this to DEFERRED. |
| 1349 | */ |
| 1350 | void sqliteCreateForeignKey( |
| 1351 | Parse *pParse, /* Parsing context */ |
| 1352 | IdList *pFromCol, /* Columns in this table that point to other table */ |
| 1353 | Token *pTo, /* Name of the other table */ |
| 1354 | IdList *pToCol, /* Columns in the other table */ |
| 1355 | int flags /* Conflict resolution algorithms. */ |
| 1356 | ){ |
| 1357 | Table *p = pParse->pNewTable; |
| 1358 | int nByte; |
| 1359 | int i; |
| 1360 | int nCol; |
| 1361 | char *z; |
| 1362 | FKey *pFKey = 0; |
| 1363 | |
| 1364 | assert( pTo!=0 ); |
| 1365 | if( p==0 || pParse->nErr ) goto fk_end; |
| 1366 | if( pFromCol==0 ){ |
| 1367 | int iCol = p->nCol-1; |
| 1368 | if( iCol<0 ) goto fk_end; |
| 1369 | if( pToCol && pToCol->nId!=1 ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1370 | sqliteErrorMsg(pParse, "foreign key on %s" |
| 1371 | " should reference only one column of table %T", |
| 1372 | p->aCol[iCol].zName, pTo); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1373 | goto fk_end; |
| 1374 | } |
| 1375 | nCol = 1; |
| 1376 | }else if( pToCol && pToCol->nId!=pFromCol->nId ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1377 | sqliteErrorMsg(pParse, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1378 | "number of columns in foreign key does not match the number of " |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1379 | "columns in the referenced table"); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1380 | goto fk_end; |
| 1381 | }else{ |
| 1382 | nCol = pFromCol->nId; |
| 1383 | } |
| 1384 | nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
| 1385 | if( pToCol ){ |
| 1386 | for(i=0; i<pToCol->nId; i++){ |
| 1387 | nByte += strlen(pToCol->a[i].zName) + 1; |
| 1388 | } |
| 1389 | } |
| 1390 | pFKey = sqliteMalloc( nByte ); |
| 1391 | if( pFKey==0 ) goto fk_end; |
| 1392 | pFKey->pFrom = p; |
| 1393 | pFKey->pNextFrom = p->pFKey; |
drh | df68f6b | 2002-09-21 15:57:57 +0000 | [diff] [blame] | 1394 | z = (char*)&pFKey[1]; |
| 1395 | pFKey->aCol = (struct sColMap*)z; |
| 1396 | z += sizeof(struct sColMap)*nCol; |
| 1397 | pFKey->zTo = z; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1398 | memcpy(z, pTo->z, pTo->n); |
| 1399 | z[pTo->n] = 0; |
| 1400 | z += pTo->n+1; |
| 1401 | pFKey->pNextTo = 0; |
| 1402 | pFKey->nCol = nCol; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1403 | if( pFromCol==0 ){ |
| 1404 | pFKey->aCol[0].iFrom = p->nCol-1; |
| 1405 | }else{ |
| 1406 | for(i=0; i<nCol; i++){ |
| 1407 | int j; |
| 1408 | for(j=0; j<p->nCol; j++){ |
| 1409 | if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ |
| 1410 | pFKey->aCol[i].iFrom = j; |
| 1411 | break; |
| 1412 | } |
| 1413 | } |
| 1414 | if( j>=p->nCol ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1415 | sqliteErrorMsg(pParse, |
| 1416 | "unknown column \"%s\" in foreign key definition", |
| 1417 | pFromCol->a[i].zName); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1418 | goto fk_end; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | if( pToCol ){ |
| 1423 | for(i=0; i<nCol; i++){ |
| 1424 | int n = strlen(pToCol->a[i].zName); |
| 1425 | pFKey->aCol[i].zCol = z; |
| 1426 | memcpy(z, pToCol->a[i].zName, n); |
| 1427 | z[n] = 0; |
| 1428 | z += n+1; |
| 1429 | } |
| 1430 | } |
| 1431 | pFKey->isDeferred = 0; |
| 1432 | pFKey->deleteConf = flags & 0xff; |
| 1433 | pFKey->updateConf = (flags >> 8 ) & 0xff; |
| 1434 | pFKey->insertConf = (flags >> 16 ) & 0xff; |
| 1435 | |
| 1436 | /* Link the foreign key to the table as the last step. |
| 1437 | */ |
| 1438 | p->pFKey = pFKey; |
| 1439 | pFKey = 0; |
| 1440 | |
| 1441 | fk_end: |
| 1442 | sqliteFree(pFKey); |
| 1443 | sqliteIdListDelete(pFromCol); |
| 1444 | sqliteIdListDelete(pToCol); |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 1449 | ** clause is seen as part of a foreign key definition. The isDeferred |
| 1450 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 1451 | ** The behavior of the most recently created foreign key is adjusted |
| 1452 | ** accordingly. |
| 1453 | */ |
| 1454 | void sqliteDeferForeignKey(Parse *pParse, int isDeferred){ |
| 1455 | Table *pTab; |
| 1456 | FKey *pFKey; |
| 1457 | if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; |
| 1458 | pFKey->isDeferred = isDeferred; |
| 1459 | } |
| 1460 | |
| 1461 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1462 | ** Create a new index for an SQL table. pIndex is the name of the index |
| 1463 | ** and pTable is the name of the table that is to be indexed. Both will |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1464 | ** be NULL for a primary key or an index that is created to satisfy a |
| 1465 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1466 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 1467 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1468 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1469 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 1470 | ** is a primary key or unique-constraint on the most recent column added |
| 1471 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1472 | */ |
| 1473 | void sqliteCreateIndex( |
| 1474 | Parse *pParse, /* All information about this parse */ |
| 1475 | Token *pName, /* Name of the index. May be NULL */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1476 | SrcList *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */ |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1477 | IdList *pList, /* A list of columns to be indexed */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1478 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1479 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
| 1480 | Token *pEnd /* The ")" that closes the CREATE INDEX statement */ |
| 1481 | ){ |
| 1482 | Table *pTab; /* Table to be indexed */ |
| 1483 | Index *pIndex; /* The index to be created */ |
| 1484 | char *zName = 0; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 1485 | int i, j; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1486 | Token nullId; /* Fake token for an empty ID list */ |
| 1487 | DbFixer sFix; /* For assigning database names to pTable */ |
drh | 4925ca0 | 2003-11-27 00:48:57 +0000 | [diff] [blame] | 1488 | int isTemp; /* True for a temporary index */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1489 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1490 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1491 | if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1492 | if( db->init.busy |
| 1493 | && sqliteFixInit(&sFix, pParse, db->init.iDb, "index", pName) |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1494 | && sqliteFixSrcList(&sFix, pTable) |
| 1495 | ){ |
| 1496 | goto exit_create_index; |
| 1497 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1498 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1499 | /* |
| 1500 | ** Find the table that is to be indexed. Return early if not found. |
| 1501 | */ |
| 1502 | if( pTable!=0 ){ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1503 | assert( pName!=0 ); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1504 | assert( pTable->nSrc==1 ); |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 1505 | pTab = sqliteSrcListLookup(pParse, pTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1506 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1507 | assert( pName==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1508 | pTab = pParse->pNewTable; |
| 1509 | } |
| 1510 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 1511 | if( pTab->readOnly ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1512 | sqliteErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 1513 | goto exit_create_index; |
| 1514 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1515 | if( pTab->iDb>=2 && db->init.busy==0 ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1516 | sqliteErrorMsg(pParse, "table %s may not have indices added", pTab->zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1517 | goto exit_create_index; |
| 1518 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1519 | if( pTab->pSelect ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1520 | sqliteErrorMsg(pParse, "views may not be indexed"); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1521 | goto exit_create_index; |
| 1522 | } |
drh | 4925ca0 | 2003-11-27 00:48:57 +0000 | [diff] [blame] | 1523 | isTemp = pTab->iDb==1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1524 | |
| 1525 | /* |
| 1526 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1527 | ** index or table with the same name. |
| 1528 | ** |
| 1529 | ** Exception: If we are reading the names of permanent indices from the |
| 1530 | ** sqlite_master table (because some other process changed the schema) and |
| 1531 | ** one of the index names collides with the name of a temporary table or |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1532 | ** index, then we will continue to process this index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1533 | ** |
| 1534 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1535 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 1536 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1537 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1538 | if( pName && !db->init.busy ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1539 | Index *pISameName; /* Another index with the same name */ |
| 1540 | Table *pTSameName; /* A table with same name as the index */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1541 | zName = sqliteStrNDup(pName->z, pName->n); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1542 | if( zName==0 ) goto exit_create_index; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1543 | if( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1544 | sqliteErrorMsg(pParse, "index %s already exists", zName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1545 | goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1546 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1547 | if( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1548 | sqliteErrorMsg(pParse, "there is already a table named %s", zName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1549 | goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1550 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1551 | }else if( pName==0 ){ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1552 | char zBuf[30]; |
| 1553 | int n; |
| 1554 | Index *pLoop; |
| 1555 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
| 1556 | sprintf(zBuf,"%d)",n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1557 | zName = 0; |
drh | 4174398 | 2003-12-06 21:43:55 +0000 | [diff] [blame] | 1558 | sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1559 | if( zName==0 ) goto exit_create_index; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1560 | }else{ |
| 1561 | zName = sqliteStrNDup(pName->z, pName->n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1564 | /* Check for authorization to create an index. |
| 1565 | */ |
| 1566 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1567 | { |
| 1568 | const char *zDb = db->aDb[pTab->iDb].zName; |
| 1569 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1570 | assert( pTab->iDb==db->init.iDb || isTemp ); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1571 | if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
| 1572 | goto exit_create_index; |
| 1573 | } |
| 1574 | i = SQLITE_CREATE_INDEX; |
| 1575 | if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX; |
| 1576 | if( sqliteAuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
| 1577 | goto exit_create_index; |
| 1578 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1579 | } |
| 1580 | #endif |
| 1581 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1582 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1583 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1584 | ** So create a fake list to simulate this. |
| 1585 | */ |
| 1586 | if( pList==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1587 | nullId.z = pTab->aCol[pTab->nCol-1].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1588 | nullId.n = strlen(nullId.z); |
| 1589 | pList = sqliteIdListAppend(0, &nullId); |
| 1590 | if( pList==0 ) goto exit_create_index; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | ** Allocate the index structure. |
| 1595 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 1596 | pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1597 | sizeof(int)*pList->nId ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1598 | if( pIndex==0 ) goto exit_create_index; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1599 | pIndex->aiColumn = (int*)&pIndex[1]; |
| 1600 | pIndex->zName = (char*)&pIndex->aiColumn[pList->nId]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1601 | strcpy(pIndex->zName, zName); |
| 1602 | pIndex->pTable = pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1603 | pIndex->nColumn = pList->nId; |
drh | ea1ba17 | 2003-04-20 00:00:23 +0000 | [diff] [blame] | 1604 | pIndex->onError = onError; |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1605 | pIndex->autoIndex = pName==0; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1606 | pIndex->iDb = isTemp ? 1 : db->init.iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1607 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1608 | /* Scan the names of the columns of the table to be indexed and |
| 1609 | ** load the column indices into the Index structure. Report an error |
| 1610 | ** if any column is not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1611 | */ |
| 1612 | for(i=0; i<pList->nId; i++){ |
| 1613 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1614 | if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1615 | } |
| 1616 | if( j>=pTab->nCol ){ |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1617 | sqliteErrorMsg(pParse, "table %s has no column named %s", |
| 1618 | pTab->zName, pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1619 | sqliteFree(pIndex); |
| 1620 | goto exit_create_index; |
| 1621 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1622 | pIndex->aiColumn[i] = j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* Link the new Index structure to its table and to the other |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1626 | ** in-memory database structures. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1627 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1628 | if( !pParse->explain ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1629 | Index *p; |
drh | 3c8bf55 | 2003-07-01 18:13:14 +0000 | [diff] [blame] | 1630 | p = sqliteHashInsert(&db->aDb[pIndex->iDb].idxHash, |
| 1631 | pIndex->zName, strlen(pIndex->zName)+1, pIndex); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1632 | if( p ){ |
| 1633 | assert( p==pIndex ); /* Malloc must have failed */ |
| 1634 | sqliteFree(pIndex); |
| 1635 | goto exit_create_index; |
| 1636 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1637 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1638 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1639 | |
| 1640 | /* When adding an index to the list of indices for a table, make |
| 1641 | ** sure all indices labeled OE_Replace come after all those labeled |
| 1642 | ** OE_Ignore. This is necessary for the correct operation of UPDATE |
| 1643 | ** and INSERT. |
| 1644 | */ |
| 1645 | if( onError!=OE_Replace || pTab->pIndex==0 |
| 1646 | || pTab->pIndex->onError==OE_Replace){ |
| 1647 | pIndex->pNext = pTab->pIndex; |
| 1648 | pTab->pIndex = pIndex; |
| 1649 | }else{ |
| 1650 | Index *pOther = pTab->pIndex; |
| 1651 | while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
| 1652 | pOther = pOther->pNext; |
| 1653 | } |
| 1654 | pIndex->pNext = pOther->pNext; |
| 1655 | pOther->pNext = pIndex; |
| 1656 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1657 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1658 | /* If the db->init.busy is 1 it means we are reading the SQL off the |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1659 | ** "sqlite_master" table on the disk. So do not write to the disk |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1660 | ** again. Extract the table number from the db->init.newTnum field. |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1661 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1662 | if( db->init.busy && pTable!=0 ){ |
| 1663 | pIndex->tnum = db->init.newTnum; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1666 | /* If the db->init.busy is 0 then create the index on disk. This |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1667 | ** involves writing the index into the master table and filling in the |
| 1668 | ** index with the current table contents. |
| 1669 | ** |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1670 | ** The db->init.busy is 0 when the user first enters a CREATE INDEX |
| 1671 | ** command. db->init.busy is 1 when a database is opened and |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1672 | ** CREATE INDEX statements are read out of the master table. In |
| 1673 | ** the latter case the index already exists on disk, which is why |
| 1674 | ** we don't want to recreate it. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1675 | ** |
| 1676 | ** If pTable==0 it means this index is generated as a primary key |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1677 | ** or UNIQUE constraint of a CREATE TABLE statement. Since the table |
| 1678 | ** has just been created, it contains no data and the index initialization |
| 1679 | ** step can be skipped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1680 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1681 | else if( db->init.busy==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1682 | int n; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1683 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1684 | int lbl1, lbl2; |
| 1685 | int i; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1686 | int addr; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1687 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1688 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1689 | if( v==0 ) goto exit_create_index; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1690 | if( pTable!=0 ){ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1691 | sqliteBeginWriteOperation(pParse, 0, isTemp); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1692 | sqliteOpenMasterTable(v, isTemp); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1693 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1694 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame^] | 1695 | sqliteVdbeOp3(v, OP_String, 0, 0, "index", P3_STATIC); |
| 1696 | sqliteVdbeOp3(v, OP_String, 0, 0, pIndex->zName, 0); |
| 1697 | sqliteVdbeOp3(v, OP_String, 0, 0, pTab->zName, 0); |
| 1698 | sqliteVdbeOp3(v, OP_CreateIndex, 0, isTemp,(char*)&pIndex->tnum,P3_POINTER); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1699 | pIndex->tnum = 0; |
| 1700 | if( pTable ){ |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame^] | 1701 | sqliteVdbeCode(v, |
| 1702 | OP_Dup, 0, 0, |
| 1703 | OP_Integer, isTemp, 0, |
| 1704 | OP_OpenWrite, 1, 0, |
| 1705 | 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1706 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1707 | addr = sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1708 | if( pStart && pEnd ){ |
| 1709 | n = Addr(pEnd->z) - Addr(pStart->z) + 1; |
| 1710 | sqliteVdbeChangeP3(v, addr, pStart->z, n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1711 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1712 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0); |
| 1713 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1714 | if( pTable ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1715 | sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame^] | 1716 | sqliteVdbeOp3(v, OP_OpenRead, 2, pTab->tnum, pTab->zName, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1717 | lbl2 = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1718 | sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2); |
| 1719 | lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1720 | for(i=0; i<pIndex->nColumn; i++){ |
drh | 56e452c | 2003-05-01 16:56:03 +0000 | [diff] [blame] | 1721 | int iCol = pIndex->aiColumn[i]; |
| 1722 | if( pTab->iPKey==iCol ){ |
| 1723 | sqliteVdbeAddOp(v, OP_Dup, i, 0); |
| 1724 | }else{ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1725 | sqliteVdbeAddOp(v, OP_Column, 2, iCol); |
drh | 56e452c | 2003-05-01 16:56:03 +0000 | [diff] [blame] | 1726 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1727 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1728 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0); |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 1729 | if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex); |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame^] | 1730 | sqliteVdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None, |
| 1731 | "indexed columns are not unique", P3_STATIC); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1732 | sqliteVdbeAddOp(v, OP_Next, 2, lbl1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1733 | sqliteVdbeResolveLabel(v, lbl2); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1734 | sqliteVdbeAddOp(v, OP_Close, 2, 0); |
| 1735 | sqliteVdbeAddOp(v, OP_Close, 1, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1736 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1737 | if( pTable!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1738 | if( !isTemp ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1739 | sqliteChangeCookie(db, v); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1740 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1741 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1742 | sqliteEndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1743 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1746 | /* Clean up before exiting */ |
| 1747 | exit_create_index: |
| 1748 | sqliteIdListDelete(pList); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1749 | sqliteSrcListDelete(pTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1750 | sqliteFree(zName); |
| 1751 | return; |
| 1752 | } |
| 1753 | |
| 1754 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 1755 | ** This routine will drop an existing named index. This routine |
| 1756 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1757 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1758 | void sqliteDropIndex(Parse *pParse, SrcList *pName){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1759 | Index *pIndex; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1760 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1761 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1762 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1763 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1764 | assert( pName->nSrc==1 ); |
| 1765 | pIndex = sqliteFindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1766 | if( pIndex==0 ){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 1767 | sqliteErrorMsg(pParse, "no such index: %S", pName, 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1768 | goto exit_drop_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1769 | } |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1770 | if( pIndex->autoIndex ){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 1771 | sqliteErrorMsg(pParse, "index associated with UNIQUE " |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1772 | "or PRIMARY KEY constraint cannot be dropped", 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1773 | goto exit_drop_index; |
| 1774 | } |
| 1775 | if( pIndex->iDb>1 ){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 1776 | sqliteErrorMsg(pParse, "cannot alter schema of attached " |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1777 | "databases", 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1778 | goto exit_drop_index; |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1779 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1780 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 1781 | { |
| 1782 | int code = SQLITE_DROP_INDEX; |
| 1783 | Table *pTab = pIndex->pTable; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1784 | const char *zDb = db->aDb[pIndex->iDb].zName; |
| 1785 | const char *zTab = SCHEMA_TABLE(pIndex->iDb); |
| 1786 | if( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1787 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1788 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1789 | if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1790 | if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1791 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1792 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 1793 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1794 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1795 | |
| 1796 | /* Generate code to remove the index and from the master table */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1797 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1798 | if( v ){ |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 1799 | static VdbeOpList dropIndex[] = { |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1800 | { OP_Rewind, 0, ADDR(9), 0}, |
| 1801 | { OP_String, 0, 0, 0}, /* 1 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1802 | { OP_MemStore, 1, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1803 | { OP_MemLoad, 1, 0, 0}, /* 3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1804 | { OP_Column, 0, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1805 | { OP_Eq, 0, ADDR(8), 0}, |
| 1806 | { OP_Next, 0, ADDR(3), 0}, |
| 1807 | { OP_Goto, 0, ADDR(9), 0}, |
| 1808 | { OP_Delete, 0, 0, 0}, /* 8 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1809 | }; |
| 1810 | int base; |
| 1811 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1812 | sqliteBeginWriteOperation(pParse, 0, pIndex->iDb); |
| 1813 | sqliteOpenMasterTable(v, pIndex->iDb); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1814 | base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); |
| 1815 | sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1816 | if( pIndex->iDb==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1817 | sqliteChangeCookie(db, v); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1818 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1819 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1820 | sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1821 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1824 | /* Delete the in-memory description of this index. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1825 | */ |
| 1826 | if( !pParse->explain ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1827 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1828 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1829 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1830 | |
| 1831 | exit_drop_index: |
| 1832 | sqliteSrcListDelete(pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1836 | ** Append a new element to the given IdList. Create a new IdList if |
| 1837 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1838 | ** |
| 1839 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1840 | */ |
| 1841 | IdList *sqliteIdListAppend(IdList *pList, Token *pToken){ |
| 1842 | if( pList==0 ){ |
| 1843 | pList = sqliteMalloc( sizeof(IdList) ); |
| 1844 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 1845 | pList->nAlloc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1846 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 1847 | if( pList->nId>=pList->nAlloc ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1848 | struct IdList_item *a; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 1849 | pList->nAlloc = pList->nAlloc*2 + 5; |
| 1850 | a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1851 | if( a==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1852 | sqliteIdListDelete(pList); |
| 1853 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1854 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1855 | pList->a = a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1856 | } |
| 1857 | memset(&pList->a[pList->nId], 0, sizeof(pList->a[0])); |
| 1858 | if( pToken ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1859 | char **pz = &pList->a[pList->nId].zName; |
| 1860 | sqliteSetNString(pz, pToken->z, pToken->n, 0); |
| 1861 | if( *pz==0 ){ |
| 1862 | sqliteIdListDelete(pList); |
| 1863 | return 0; |
| 1864 | }else{ |
| 1865 | sqliteDequote(*pz); |
| 1866 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1867 | } |
| 1868 | pList->nId++; |
| 1869 | return pList; |
| 1870 | } |
| 1871 | |
| 1872 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1873 | ** Append a new table name to the given SrcList. Create a new SrcList if |
| 1874 | ** need be. A new entry is created in the SrcList even if pToken is NULL. |
| 1875 | ** |
| 1876 | ** A new SrcList is returned, or NULL if malloc() fails. |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1877 | ** |
| 1878 | ** If pDatabase is not null, it means that the table has an optional |
| 1879 | ** database name prefix. Like this: "database.table". The pDatabase |
| 1880 | ** points to the table name and the pTable points to the database name. |
| 1881 | ** The SrcList.a[].zName field is filled with the table name which might |
| 1882 | ** come from pTable (if pDatabase is NULL) or from pDatabase. |
| 1883 | ** SrcList.a[].zDatabase is filled with the database name from pTable, |
| 1884 | ** or with NULL if no database is specified. |
| 1885 | ** |
| 1886 | ** In other words, if call like this: |
| 1887 | ** |
| 1888 | ** sqliteSrcListAppend(A,B,0); |
| 1889 | ** |
| 1890 | ** Then B is a table name and the database name is unspecified. If called |
| 1891 | ** like this: |
| 1892 | ** |
| 1893 | ** sqliteSrcListAppend(A,B,C); |
| 1894 | ** |
| 1895 | ** Then C is the table name and B is the database name. |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1896 | */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1897 | SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1898 | if( pList==0 ){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1899 | pList = sqliteMalloc( sizeof(SrcList) ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1900 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 1901 | pList->nAlloc = 1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1902 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 1903 | if( pList->nSrc>=pList->nAlloc ){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1904 | SrcList *pNew; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 1905 | pList->nAlloc *= 2; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1906 | pNew = sqliteRealloc(pList, |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 1907 | sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) ); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1908 | if( pNew==0 ){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1909 | sqliteSrcListDelete(pList); |
| 1910 | return 0; |
| 1911 | } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1912 | pList = pNew; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1913 | } |
| 1914 | memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0])); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1915 | if( pDatabase && pDatabase->z==0 ){ |
| 1916 | pDatabase = 0; |
| 1917 | } |
| 1918 | if( pDatabase && pTable ){ |
| 1919 | Token *pTemp = pDatabase; |
| 1920 | pDatabase = pTable; |
| 1921 | pTable = pTemp; |
| 1922 | } |
| 1923 | if( pTable ){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1924 | char **pz = &pList->a[pList->nSrc].zName; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1925 | sqliteSetNString(pz, pTable->z, pTable->n, 0); |
| 1926 | if( *pz==0 ){ |
| 1927 | sqliteSrcListDelete(pList); |
| 1928 | return 0; |
| 1929 | }else{ |
| 1930 | sqliteDequote(*pz); |
| 1931 | } |
| 1932 | } |
| 1933 | if( pDatabase ){ |
| 1934 | char **pz = &pList->a[pList->nSrc].zDatabase; |
| 1935 | sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1936 | if( *pz==0 ){ |
| 1937 | sqliteSrcListDelete(pList); |
| 1938 | return 0; |
| 1939 | }else{ |
| 1940 | sqliteDequote(*pz); |
| 1941 | } |
| 1942 | } |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 1943 | pList->a[pList->nSrc].iCursor = -1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1944 | pList->nSrc++; |
| 1945 | return pList; |
| 1946 | } |
| 1947 | |
| 1948 | /* |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 1949 | ** Assign cursors to all tables in a SrcList |
| 1950 | */ |
| 1951 | void sqliteSrcListAssignCursors(Parse *pParse, SrcList *pList){ |
| 1952 | int i; |
| 1953 | for(i=0; i<pList->nSrc; i++){ |
| 1954 | if( pList->a[i].iCursor<0 ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1955 | pList->a[i].iCursor = pParse->nTab++; |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1961 | ** Add an alias to the last identifier on the given identifier list. |
| 1962 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1963 | void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){ |
| 1964 | if( pList && pList->nSrc>0 ){ |
| 1965 | int i = pList->nSrc - 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1966 | sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1967 | sqliteDequote(pList->a[i].zAlias); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1972 | ** Delete an IdList. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1973 | */ |
| 1974 | void sqliteIdListDelete(IdList *pList){ |
| 1975 | int i; |
| 1976 | if( pList==0 ) return; |
| 1977 | for(i=0; i<pList->nId; i++){ |
| 1978 | sqliteFree(pList->a[i].zName); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1979 | } |
| 1980 | sqliteFree(pList->a); |
| 1981 | sqliteFree(pList); |
| 1982 | } |
| 1983 | |
| 1984 | /* |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1985 | ** Return the index in pList of the identifier named zId. Return -1 |
| 1986 | ** if not found. |
| 1987 | */ |
| 1988 | int sqliteIdListIndex(IdList *pList, const char *zName){ |
| 1989 | int i; |
| 1990 | if( pList==0 ) return -1; |
| 1991 | for(i=0; i<pList->nId; i++){ |
| 1992 | if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i; |
| 1993 | } |
| 1994 | return -1; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1998 | ** Delete an entire SrcList including all its substructure. |
| 1999 | */ |
| 2000 | void sqliteSrcListDelete(SrcList *pList){ |
| 2001 | int i; |
| 2002 | if( pList==0 ) return; |
| 2003 | for(i=0; i<pList->nSrc; i++){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2004 | sqliteFree(pList->a[i].zDatabase); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2005 | sqliteFree(pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2006 | sqliteFree(pList->a[i].zAlias); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 2007 | if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2008 | sqliteDeleteTable(0, pList->a[i].pTab); |
| 2009 | } |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 2010 | sqliteSelectDelete(pList->a[i].pSelect); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2011 | sqliteExprDelete(pList->a[i].pOn); |
| 2012 | sqliteIdListDelete(pList->a[i].pUsing); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2013 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2014 | sqliteFree(pList); |
| 2015 | } |
| 2016 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 2017 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2018 | ** Begin a transaction |
| 2019 | */ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2020 | void sqliteBeginTransaction(Parse *pParse, int onError){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2021 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2022 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2023 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2024 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 2025 | if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2026 | if( db->flags & SQLITE_InTrans ){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2027 | sqliteErrorMsg(pParse, "cannot start a transaction within a transaction"); |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2028 | return; |
| 2029 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2030 | sqliteBeginWriteOperation(pParse, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2031 | db->flags |= SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2032 | db->onError = onError; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | /* |
| 2036 | ** Commit a transaction |
| 2037 | */ |
| 2038 | void sqliteCommitTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2039 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2040 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2041 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2042 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 2043 | if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2044 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2045 | sqliteErrorMsg(pParse, "cannot commit - no transaction is active"); |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2046 | return; |
| 2047 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2048 | db->flags &= ~SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2049 | sqliteEndWriteOperation(pParse); |
| 2050 | db->onError = OE_Default; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | /* |
| 2054 | ** Rollback a transaction |
| 2055 | */ |
| 2056 | void sqliteRollbackTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2057 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2058 | Vdbe *v; |
| 2059 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2060 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2061 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 2062 | if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2063 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2064 | sqliteErrorMsg(pParse, "cannot rollback - no transaction is active"); |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2065 | return; |
| 2066 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2067 | v = sqliteGetVdbe(pParse); |
| 2068 | if( v ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2069 | sqliteVdbeAddOp(v, OP_Rollback, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2070 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2071 | db->flags &= ~SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2072 | db->onError = OE_Default; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2073 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2074 | |
| 2075 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2076 | ** Generate VDBE code that will verify the schema cookie for all |
| 2077 | ** named database files. |
| 2078 | */ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2079 | void sqliteCodeVerifySchema(Parse *pParse, int iDb){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2080 | sqlite *db = pParse->db; |
| 2081 | Vdbe *v = sqliteGetVdbe(pParse); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2082 | assert( iDb>=0 && iDb<db->nDb ); |
| 2083 | assert( db->aDb[iDb].pBt!=0 ); |
| 2084 | if( iDb!=1 && !DbHasProperty(db, iDb, DB_Cookie) ){ |
| 2085 | sqliteVdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie); |
| 2086 | DbSetProperty(db, iDb, DB_Cookie); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2087 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2091 | ** Generate VDBE code that prepares for doing an operation that |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 2092 | ** might change the database. |
| 2093 | ** |
| 2094 | ** This routine starts a new transaction if we are not already within |
| 2095 | ** a transaction. If we are already within a transaction, then a checkpoint |
| 2096 | ** is set if the setCheckpoint parameter is true. A checkpoint should |
| 2097 | ** be set for operations that might fail (due to a constraint) part of |
| 2098 | ** the way through and which will need to undo some writes without having to |
| 2099 | ** rollback the whole transaction. For operations where all constraints |
| 2100 | ** can be checked before any changes are made to the database, it is never |
| 2101 | ** necessary to undo a write and the checkpoint should not be set. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2102 | ** |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2103 | ** Only database iDb and the temp database are made writable by this call. |
| 2104 | ** If iDb==0, then the main and temp databases are made writable. If |
| 2105 | ** iDb==1 then only the temp database is made writable. If iDb>1 then the |
| 2106 | ** specified auxiliary database and the temp database are made writable. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2107 | */ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2108 | void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int iDb){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2109 | Vdbe *v; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2110 | sqlite *db = pParse->db; |
| 2111 | if( DbHasProperty(db, iDb, DB_Locked) ) return; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2112 | v = sqliteGetVdbe(pParse); |
| 2113 | if( v==0 ) return; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2114 | if( !db->aDb[iDb].inTrans ){ |
| 2115 | sqliteVdbeAddOp(v, OP_Transaction, iDb, 0); |
| 2116 | DbSetProperty(db, iDb, DB_Locked); |
| 2117 | sqliteCodeVerifySchema(pParse, iDb); |
| 2118 | if( iDb!=1 ){ |
| 2119 | sqliteBeginWriteOperation(pParse, setCheckpoint, 1); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2120 | } |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 2121 | }else if( setCheckpoint ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2122 | sqliteVdbeAddOp(v, OP_Checkpoint, iDb, 0); |
| 2123 | DbSetProperty(db, iDb, DB_Locked); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2128 | ** Generate code that concludes an operation that may have changed |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2129 | ** the database. If a statement transaction was started, then emit |
| 2130 | ** an OP_Commit that will cause the changes to be committed to disk. |
| 2131 | ** |
| 2132 | ** Note that checkpoints are automatically committed at the end of |
| 2133 | ** a statement. Note also that there can be multiple calls to |
| 2134 | ** sqliteBeginWriteOperation() but there should only be a single |
| 2135 | ** call to sqliteEndWriteOperation() at the conclusion of the statement. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2136 | */ |
| 2137 | void sqliteEndWriteOperation(Parse *pParse){ |
| 2138 | Vdbe *v; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2139 | sqlite *db = pParse->db; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 2140 | if( pParse->trigStack ) return; /* if this is in a trigger */ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2141 | v = sqliteGetVdbe(pParse); |
| 2142 | if( v==0 ) return; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2143 | if( db->flags & SQLITE_InTrans ){ |
| 2144 | /* A BEGIN has executed. Do not commit until we see an explicit |
| 2145 | ** COMMIT statement. */ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2146 | }else{ |
| 2147 | sqliteVdbeAddOp(v, OP_Commit, 0, 0); |
| 2148 | } |
| 2149 | } |