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