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