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