drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 12 | ** This file contains C code routines that are called by the SQLite parser |
| 13 | ** when syntax rules are reduced. The routines in this file handle the |
| 14 | ** following kinds of SQL syntax: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 15 | ** |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 16 | ** CREATE TABLE |
| 17 | ** DROP TABLE |
| 18 | ** CREATE INDEX |
| 19 | ** DROP INDEX |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 20 | ** creating ID lists |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 21 | ** BEGIN TRANSACTION |
| 22 | ** COMMIT |
| 23 | ** ROLLBACK |
| 24 | ** PRAGMA |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 25 | ** |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame^] | 26 | ** $Id: build.c,v 1.197 2004/05/28 16:00:22 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 27 | */ |
| 28 | #include "sqliteInt.h" |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 29 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 30 | |
| 31 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 32 | ** This routine is called when a new SQL statement is beginning to |
| 33 | ** be parsed. Check to see if the schema for the database needs |
| 34 | ** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables. |
| 35 | ** If it does, then read it. |
| 36 | */ |
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 | /* |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 402 | ** Open the sqlite_master table stored in database number iDb for |
| 403 | ** writing. The table is opened using cursor 0. |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 404 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 405 | void sqlite3OpenMasterTable(Vdbe *v, int iDb){ |
| 406 | sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
danielk1977 | 8e15081 | 2004-05-10 01:17:37 +0000 | [diff] [blame] | 407 | sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 408 | sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 412 | ** The token *pName contains the name of a database (either "main" or |
| 413 | ** "temp" or the name of an attached db). This routine returns the |
| 414 | ** index of the named database in db->aDb[], or -1 if the named db |
| 415 | ** does not exist. |
| 416 | */ |
| 417 | int findDb(sqlite3 *db, Token *pName){ |
| 418 | int i; |
| 419 | for(i=0; i<db->nDb; i++){ |
| 420 | if( pName->n==strlen(db->aDb[i].zName) && |
| 421 | 0==sqlite3StrNICmp(db->aDb[i].zName, pName->z, pName->n) ){ |
| 422 | return i; |
| 423 | } |
| 424 | } |
| 425 | return -1; |
| 426 | } |
| 427 | |
| 428 | static int resolveSchemaName( |
| 429 | Parse *pParse, |
| 430 | Token *pName1, |
| 431 | Token *pName2, |
| 432 | Token **pUnqual |
| 433 | ){ |
| 434 | int iDb; |
| 435 | sqlite3 *db = pParse->db; |
| 436 | |
| 437 | if( pName2 && pName2->n>0 ){ |
| 438 | assert( !db->init.busy ); |
| 439 | *pUnqual = pName2; |
| 440 | iDb = findDb(db, pName1); |
| 441 | if( iDb<0 ){ |
| 442 | sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
| 443 | pParse->nErr++; |
| 444 | return -1; |
| 445 | } |
| 446 | }else{ |
| 447 | assert( db->init.iDb==0 || db->init.busy ); |
| 448 | iDb = db->init.iDb; |
| 449 | *pUnqual = pName1; |
| 450 | } |
| 451 | return iDb; |
| 452 | } |
| 453 | |
| 454 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 455 | ** Begin constructing a new table representation in memory. This is |
| 456 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 457 | ** to a CREATE TABLE statement. In particular, this routine is called |
| 458 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 459 | ** pStart token is the CREATE and pName is the table name. The isTemp |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 460 | ** flag is true if the table should be stored in the auxiliary database |
| 461 | ** file instead of in the main database file. This is normally the case |
| 462 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 463 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 464 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 465 | ** The new table record is initialized and put in pParse->pNewTable. |
| 466 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 467 | ** routines will be called to add more information to this record. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 468 | ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 469 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 470 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 471 | void sqlite3StartTable( |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 472 | Parse *pParse, /* Parser context */ |
| 473 | Token *pStart, /* The "CREATE" token */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 474 | Token *pName1, /* First part of the name of the table or view */ |
| 475 | Token *pName2, /* Second part of the name of the table or view */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 476 | int isTemp, /* True if this is a TEMP table */ |
| 477 | int isView /* True if this is a VIEW */ |
| 478 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 479 | Table *pTable; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 480 | Index *pIdx; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 481 | char *zName; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 482 | sqlite *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 483 | Vdbe *v; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 484 | int iDb; /* Database number to create the table in */ |
| 485 | Token *pName; /* Unqualified name of the table to create */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 486 | |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 487 | /* The table or view name to create is passed to this routine via tokens |
| 488 | ** pName1 and pName2. If the table name was fully qualified, for example: |
| 489 | ** |
| 490 | ** CREATE TABLE xxx.yyy (...); |
| 491 | ** |
| 492 | ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
| 493 | ** the table name is not fully qualified, i.e.: |
| 494 | ** |
| 495 | ** CREATE TABLE yyy(...); |
| 496 | ** |
| 497 | ** Then pName1 is set to "yyy" and pName2 is "". |
| 498 | ** |
| 499 | ** The call below sets the pName pointer to point at the token (pName1 or |
| 500 | ** pName2) that stores the unqualified table name. The variable iDb is |
| 501 | ** set to the index of the database that the table or view is to be |
| 502 | ** created in. |
| 503 | */ |
| 504 | iDb = resolveSchemaName(pParse, pName1, pName2, &pName); |
| 505 | if( iDb<0 ) return; |
| 506 | if( isTemp && iDb>1 ){ |
| 507 | /* If creating a temp table, the name may not be qualified */ |
| 508 | sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 509 | pParse->nErr++; |
| 510 | return; |
| 511 | } |
| 512 | if( isTemp ) iDb = 1; |
| 513 | |
| 514 | pParse->sNameToken = *pName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 515 | zName = sqlite3TableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 516 | if( zName==0 ) return; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 517 | if( db->init.iDb==1 ) isTemp = 1; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 518 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 519 | assert( (isTemp & 1)==isTemp ); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 520 | { |
| 521 | int code; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 522 | char *zDb = db->aDb[iDb].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 523 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 524 | sqliteFree(zName); |
| 525 | return; |
| 526 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 527 | if( isView ){ |
| 528 | if( isTemp ){ |
| 529 | code = SQLITE_CREATE_TEMP_VIEW; |
| 530 | }else{ |
| 531 | code = SQLITE_CREATE_VIEW; |
| 532 | } |
| 533 | }else{ |
| 534 | if( isTemp ){ |
| 535 | code = SQLITE_CREATE_TEMP_TABLE; |
| 536 | }else{ |
| 537 | code = SQLITE_CREATE_TABLE; |
| 538 | } |
| 539 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 540 | if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){ |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 541 | sqliteFree(zName); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 542 | return; |
| 543 | } |
| 544 | } |
| 545 | #endif |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 546 | |
| 547 | /* Before trying to create a temporary table, make sure the Btree for |
| 548 | ** holding temporary tables is open. |
| 549 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 550 | if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 551 | int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 552 | if( rc!=SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 553 | sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 554 | "file for storing temporary tables"); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 555 | pParse->nErr++; |
| 556 | return; |
| 557 | } |
| 558 | if( db->flags & SQLITE_InTrans ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 559 | rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 560 | if( rc!=SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 561 | sqlite3ErrorMsg(pParse, "unable to get a write lock on " |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 562 | "the temporary database file"); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 563 | return; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* Make sure the new table name does not collide with an existing |
| 569 | ** index or table name. Issue an error message if it does. |
| 570 | ** |
| 571 | ** If we are re-reading the sqlite_master table because of a schema |
| 572 | ** change and a new permanent table is found whose name collides with |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 573 | ** an existing temporary table, that is not an error. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 574 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 575 | pTable = sqlite3FindTable(db, zName, 0); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 576 | if( pTable!=0 && (pTable->iDb==iDb || !db->init.busy) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 577 | sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 578 | sqliteFree(zName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 579 | return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 580 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 581 | if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 && |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 582 | (pIdx->iDb==0 || !db->init.busy) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 583 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 584 | sqliteFree(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 585 | return; |
| 586 | } |
| 587 | pTable = sqliteMalloc( sizeof(Table) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 588 | if( pTable==0 ){ |
| 589 | sqliteFree(zName); |
| 590 | return; |
| 591 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 592 | pTable->zName = zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 593 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 594 | pTable->aCol = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 595 | pTable->iPKey = -1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 596 | pTable->pIndex = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 597 | pTable->iDb = iDb; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 598 | if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 599 | pParse->pNewTable = pTable; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 600 | |
| 601 | /* Begin generating the code that will insert the table record into |
| 602 | ** the SQLITE_MASTER table. Note in particular that we must go ahead |
| 603 | ** and allocate the record number for the table entry now. Before any |
| 604 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
| 605 | ** indices to be created and the table record must come before the |
| 606 | ** indices. Hence, the record number for the table must be allocated |
| 607 | ** now. |
| 608 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 609 | if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 610 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 611 | if( !isTemp ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 612 | /* Every time a new table is created the file-format |
| 613 | ** and encoding meta-values are set in the database, in |
| 614 | ** case this is the first table created. |
| 615 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 616 | sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 617 | sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1); |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 618 | sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 619 | sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 620 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 621 | sqlite3OpenMasterTable(v, iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 622 | sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0); |
| 623 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
| 624 | sqlite3VdbeAddOp(v, OP_String, 0, 0); |
| 625 | sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 626 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* |
| 630 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 631 | ** |
| 632 | ** The parser calls this routine once for each column declaration |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 633 | ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 634 | ** first to get things going. Then this routine is called for each |
| 635 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 636 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 637 | void sqlite3AddColumn(Parse *pParse, Token *pName){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 638 | Table *p; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 639 | int i; |
| 640 | char *z = 0; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 641 | Column *pCol; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 642 | if( (p = pParse->pNewTable)==0 ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 643 | sqlite3SetNString(&z, pName->z, pName->n, 0); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 644 | if( z==0 ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 645 | sqlite3Dequote(z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 646 | for(i=0; i<p->nCol; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 647 | if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){ |
| 648 | sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 649 | sqliteFree(z); |
| 650 | return; |
| 651 | } |
| 652 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 653 | if( (p->nCol & 0x7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 654 | Column *aNew; |
| 655 | aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
| 656 | if( aNew==0 ) return; |
| 657 | p->aCol = aNew; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 658 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 659 | pCol = &p->aCol[p->nCol]; |
| 660 | memset(pCol, 0, sizeof(p->aCol[0])); |
| 661 | pCol->zName = z; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 662 | |
| 663 | /* If there is no type specified, columns have the default affinity |
drh | e2ea40d | 2004-05-20 12:41:19 +0000 | [diff] [blame] | 664 | ** 'NUMERIC'. If there is a type specified, then sqlite3AddColumnType() |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 665 | ** will be called next to set pCol->affinity correctly. |
| 666 | */ |
drh | e2ea40d | 2004-05-20 12:41:19 +0000 | [diff] [blame] | 667 | pCol->affinity = SQLITE_AFF_NUMERIC; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 668 | pCol->pColl = pParse->db->pDfltColl; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 669 | p->nCol++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 673 | ** This routine is called by the parser while in the middle of |
| 674 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 675 | ** been seen on a column. This routine sets the notNull flag on |
| 676 | ** the column currently under construction. |
| 677 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 678 | void sqlite3AddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 679 | Table *p; |
| 680 | int i; |
| 681 | if( (p = pParse->pNewTable)==0 ) return; |
| 682 | i = p->nCol-1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 683 | if( i>=0 ) p->aCol[i].notNull = onError; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* |
| 687 | ** This routine is called by the parser while in the middle of |
| 688 | ** parsing a CREATE TABLE statement. The pFirst token is the first |
| 689 | ** token in the sequence of tokens that describe the type of the |
| 690 | ** column currently under construction. pLast is the last token |
| 691 | ** in the sequence. Use this information to construct a string |
| 692 | ** that contains the typename of the column and store that string |
| 693 | ** in zType. |
| 694 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 695 | void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 696 | Table *p; |
| 697 | int i, j; |
| 698 | int n; |
| 699 | char *z, **pz; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 700 | Column *pCol; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 701 | if( (p = pParse->pNewTable)==0 ) return; |
| 702 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 703 | if( i<0 ) return; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 704 | pCol = &p->aCol[i]; |
| 705 | pz = &pCol->zType; |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 706 | n = pLast->n + Addr(pLast->z) - Addr(pFirst->z); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 707 | sqlite3SetNString(pz, pFirst->z, n, 0); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 708 | z = *pz; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 709 | if( z==0 ) return; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 710 | for(i=j=0; z[i]; i++){ |
| 711 | int c = z[i]; |
| 712 | if( isspace(c) ) continue; |
| 713 | z[j++] = c; |
| 714 | } |
| 715 | z[j] = 0; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 716 | // pCol->sortOrder = sqlite3CollateType(z, n); |
| 717 | pCol->affinity = sqlite3AffinityType(z, n); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 721 | ** The given token is the default value for the last column added to |
| 722 | ** the table currently under construction. If "minusFlag" is true, it |
| 723 | ** means the value token was preceded by a minus sign. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 724 | ** |
| 725 | ** This routine is called by the parser while in the middle of |
| 726 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 727 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 728 | void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 729 | Table *p; |
| 730 | int i; |
| 731 | char **pz; |
| 732 | if( (p = pParse->pNewTable)==0 ) return; |
| 733 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 734 | if( i<0 ) return; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 735 | pz = &p->aCol[i].zDflt; |
| 736 | if( minusFlag ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 737 | sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, 0); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 738 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 739 | sqlite3SetNString(pz, pVal->z, pVal->n, 0); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 740 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 741 | sqlite3Dequote(*pz); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /* |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 745 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
| 746 | ** of columns that form the primary key. If pList is NULL, then the |
| 747 | ** most recently added column of the table is the primary key. |
| 748 | ** |
| 749 | ** A table can have at most one primary key. If the table already has |
| 750 | ** a primary key (and this is the second primary key) then create an |
| 751 | ** error. |
| 752 | ** |
| 753 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
| 754 | ** then we will try to use that column as the row id. (Exception: |
| 755 | ** For backwards compatibility with older databases, do not do this |
| 756 | ** if the file format version number is less than 1.) Set the Table.iPKey |
| 757 | ** field of the table under construction to be the index of the |
| 758 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 759 | ** no INTEGER PRIMARY KEY. |
| 760 | ** |
| 761 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 762 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 763 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 764 | void sqlite3AddPrimaryKey(Parse *pParse, IdList *pList, int onError){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 765 | Table *pTab = pParse->pNewTable; |
| 766 | char *zType = 0; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 767 | int iCol = -1, i; |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 768 | if( pTab==0 ) goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 769 | if( pTab->hasPrimKey ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 770 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 771 | "table \"%s\" has more than one primary key", pTab->zName); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 772 | goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 773 | } |
| 774 | pTab->hasPrimKey = 1; |
| 775 | if( pList==0 ){ |
| 776 | iCol = pTab->nCol - 1; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 777 | pTab->aCol[iCol].isPrimKey = 1; |
| 778 | }else{ |
| 779 | for(i=0; i<pList->nId; i++){ |
| 780 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 781 | if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){ |
| 782 | break; |
| 783 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 784 | } |
| 785 | if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 786 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 787 | if( pList->nId>1 ) iCol = -1; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 788 | } |
| 789 | if( iCol>=0 && iCol<pTab->nCol ){ |
| 790 | zType = pTab->aCol[iCol].zType; |
| 791 | } |
danielk1977 | 3d68f03 | 2004-05-11 07:11:51 +0000 | [diff] [blame] | 792 | if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 793 | pTab->iPKey = iCol; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 794 | pTab->keyConf = onError; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 795 | }else{ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 796 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 797 | pList = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 798 | } |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 799 | |
| 800 | primary_key_exit: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 801 | sqlite3IdListDelete(pList); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 802 | return; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 806 | ** Return a pointer to CollSeq given the name of a collating sequence. |
| 807 | ** If the collating sequence did not previously exist, create it but |
| 808 | ** assign it an NULL comparison function. |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 809 | */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 810 | CollSeq *sqlite3CollateType(Parse *pParse, const char *zType, int nType){ |
| 811 | CollSeq *pColl; |
| 812 | sqlite *db = pParse->db; |
| 813 | |
| 814 | pColl = sqlite3HashFind(&db->aCollSeq, zType, nType); |
| 815 | if( pColl==0 ){ |
| 816 | sqlite3ChangeCollatingFunction(db, zType, nType, 0, 0); |
| 817 | pColl = sqlite3HashFind(&db->aCollSeq, zType, nType); |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 818 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 819 | return pColl; |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 823 | ** Set the collation function of the most recently parsed table column |
| 824 | ** to the CollSeq given. |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 825 | */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 826 | void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){ |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 827 | Table *p; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 828 | CollSeq *pColl; |
| 829 | sqlite *db = pParse->db; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 830 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 831 | if( (p = pParse->pNewTable)==0 ) return; |
| 832 | pColl = sqlite3HashFind(&db->aCollSeq, zType, nType); |
| 833 | if( pColl==0 ){ |
| 834 | pColl = sqlite3ChangeCollatingFunction(db, zType, nType, 0, 0); |
| 835 | } |
| 836 | if( pColl ){ |
| 837 | p->aCol[p->nCol-1].pColl = pColl; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | ** Create or modify a collating sequence entry in the sqlite.aCollSeq |
| 843 | ** table. |
| 844 | ** |
| 845 | ** Once an entry is added to the sqlite.aCollSeq table, it can never |
| 846 | ** be removed, though is comparison function or user data can be changed. |
| 847 | ** |
| 848 | ** Return a pointer to the collating function that was created or modified. |
| 849 | */ |
| 850 | CollSeq *sqlite3ChangeCollatingFunction( |
| 851 | sqlite *db, /* Database into which to insert the collation */ |
| 852 | const char *zName, /* Name of the collation */ |
| 853 | int nName, /* Number of characters in zName */ |
| 854 | void *pUser, /* First argument to xCmp */ |
| 855 | int (*xCmp)(void*,int,const void*,int,const void*) /* Comparison function */ |
| 856 | ){ |
| 857 | CollSeq *pColl; |
| 858 | |
| 859 | pColl = sqlite3HashFind(&db->aCollSeq, zName, nName); |
| 860 | if( pColl==0 ){ |
| 861 | pColl = sqliteMallocRaw( sizeof(*pColl) + nName + 1 ); |
| 862 | if( pColl==0 ){ |
| 863 | return 0; |
| 864 | } |
| 865 | pColl->zName = (char*)&pColl[1]; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 866 | memcpy(pColl->zName, zName, nName+1); |
| 867 | sqlite3HashInsert(&db->aCollSeq, pColl->zName, nName, pColl); |
| 868 | } |
| 869 | pColl->pUser = pUser; |
| 870 | pColl->xCmp = xCmp; |
| 871 | return pColl; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /* |
drh | 1ad3b9e | 2004-05-20 12:10:20 +0000 | [diff] [blame] | 875 | ** Scan the column type name zType (length nType) and return the |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 876 | ** associated affinity type. |
| 877 | */ |
| 878 | char sqlite3AffinityType(const char *zType, int nType){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 879 | int n, i; |
| 880 | struct { |
drh | 1ad3b9e | 2004-05-20 12:10:20 +0000 | [diff] [blame] | 881 | const char *zSub; /* Keywords substring to search for */ |
| 882 | int nSub; /* length of zSub */ |
| 883 | char affinity; /* Affinity to return if it matches */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 884 | } substrings[] = { |
drh | 1ad3b9e | 2004-05-20 12:10:20 +0000 | [diff] [blame] | 885 | {"INT", 3, SQLITE_AFF_INTEGER}, |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 886 | {"CHAR", 4, SQLITE_AFF_TEXT}, |
| 887 | {"CLOB", 4, SQLITE_AFF_TEXT}, |
drh | 1ad3b9e | 2004-05-20 12:10:20 +0000 | [diff] [blame] | 888 | {"TEXT", 4, SQLITE_AFF_TEXT}, |
| 889 | {"BLOB", 4, SQLITE_AFF_NONE}, |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 890 | }; |
| 891 | |
drh | 1ad3b9e | 2004-05-20 12:10:20 +0000 | [diff] [blame] | 892 | for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){ |
| 893 | int c1 = substrings[i].zSub[0]; |
| 894 | int c2 = tolower(c1); |
| 895 | int limit = nType - substrings[i].nSub; |
| 896 | const char *z = substrings[i].zSub; |
| 897 | for(n=0; n<=limit; n++){ |
| 898 | int c = zType[n]; |
| 899 | if( (c==c1 || c==c2) |
| 900 | && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 901 | return substrings[i].affinity; |
| 902 | } |
| 903 | } |
| 904 | } |
drh | 1ad3b9e | 2004-05-20 12:10:20 +0000 | [diff] [blame] | 905 | return SQLITE_AFF_NUMERIC; |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 909 | ** Come up with a new random value for the schema cookie. Make sure |
| 910 | ** the new value is different from the old. |
| 911 | ** |
| 912 | ** The schema cookie is used to determine when the schema for the |
| 913 | ** database changes. After each schema change, the cookie value |
| 914 | ** changes. When a process first reads the schema it records the |
| 915 | ** cookie. Thereafter, whenever it goes to access the database, |
| 916 | ** it checks the cookie to make sure the schema has not changed |
| 917 | ** since it was last read. |
| 918 | ** |
| 919 | ** This plan is not completely bullet-proof. It is possible for |
| 920 | ** the schema to change multiple times and for the cookie to be |
| 921 | ** set back to prior value. But schema changes are infrequent |
| 922 | ** and the probability of hitting the same cookie value is only |
| 923 | ** 1 chance in 2^32. So we're safe enough. |
| 924 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 925 | void sqlite3ChangeCookie(sqlite *db, Vdbe *v, int iDb){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 926 | if( db->next_cookie==db->aDb[0].schema_cookie ){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 927 | unsigned char r; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 928 | sqlite3Randomness(1, &r); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 929 | db->next_cookie = db->aDb[0].schema_cookie + r + 1; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 930 | db->flags |= SQLITE_InternChanges; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 931 | sqlite3VdbeAddOp(v, OP_Integer, db->next_cookie, 0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 932 | sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
| 936 | /* |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 937 | ** Measure the number of characters needed to output the given |
| 938 | ** identifier. The number returned includes any quotes used |
| 939 | ** but does not include the null terminator. |
| 940 | */ |
| 941 | static int identLength(const char *z){ |
| 942 | int n; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 943 | int needQuote = 0; |
| 944 | for(n=0; *z; n++, z++){ |
| 945 | if( *z=='\'' ){ n++; needQuote=1; } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 946 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 947 | return n + needQuote*2; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* |
| 951 | ** Write an identifier onto the end of the given string. Add |
| 952 | ** quote characters as needed. |
| 953 | */ |
| 954 | static void identPut(char *z, int *pIdx, char *zIdent){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 955 | int i, j, needQuote; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 956 | i = *pIdx; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 957 | for(j=0; zIdent[j]; j++){ |
| 958 | if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
| 959 | } |
| 960 | needQuote = zIdent[j]!=0 || isdigit(zIdent[0]) |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 961 | || sqlite3KeywordCode(zIdent, j)!=TK_ID; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 962 | if( needQuote ) z[i++] = '\''; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 963 | for(j=0; zIdent[j]; j++){ |
| 964 | z[i++] = zIdent[j]; |
| 965 | if( zIdent[j]=='\'' ) z[i++] = '\''; |
| 966 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 967 | if( needQuote ) z[i++] = '\''; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 968 | z[i] = 0; |
| 969 | *pIdx = i; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | ** Generate a CREATE TABLE statement appropriate for the given |
| 974 | ** table. Memory to hold the text of the statement is obtained |
| 975 | ** from sqliteMalloc() and must be freed by the calling function. |
| 976 | */ |
| 977 | static char *createTableStmt(Table *p){ |
| 978 | int i, k, n; |
| 979 | char *zStmt; |
| 980 | char *zSep, *zSep2, *zEnd; |
| 981 | n = 0; |
| 982 | for(i=0; i<p->nCol; i++){ |
| 983 | n += identLength(p->aCol[i].zName); |
| 984 | } |
| 985 | n += identLength(p->zName); |
| 986 | if( n<40 ){ |
| 987 | zSep = ""; |
| 988 | zSep2 = ","; |
| 989 | zEnd = ")"; |
| 990 | }else{ |
| 991 | zSep = "\n "; |
| 992 | zSep2 = ",\n "; |
| 993 | zEnd = "\n)"; |
| 994 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 995 | n += 35 + 6*p->nCol; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 996 | zStmt = sqliteMallocRaw( n ); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 997 | if( zStmt==0 ) return 0; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 998 | strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE "); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 999 | k = strlen(zStmt); |
| 1000 | identPut(zStmt, &k, p->zName); |
| 1001 | zStmt[k++] = '('; |
| 1002 | for(i=0; i<p->nCol; i++){ |
| 1003 | strcpy(&zStmt[k], zSep); |
| 1004 | k += strlen(&zStmt[k]); |
| 1005 | zSep = zSep2; |
| 1006 | identPut(zStmt, &k, p->aCol[i].zName); |
| 1007 | } |
| 1008 | strcpy(&zStmt[k], zEnd); |
| 1009 | return zStmt; |
| 1010 | } |
| 1011 | |
| 1012 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1013 | ** This routine is called to report the final ")" that terminates |
| 1014 | ** a CREATE TABLE statement. |
| 1015 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1016 | ** The table structure that other action routines have been building |
| 1017 | ** is added to the internal hash tables, assuming no errors have |
| 1018 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1019 | ** |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1020 | ** An entry for the table is made in the master table on disk, unless |
| 1021 | ** 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] | 1022 | ** it means we are reading the sqlite_master table because we just |
| 1023 | ** connected to the database or because the sqlite_master table has |
| 1024 | ** recently changes, so the entry for this table already exists in |
| 1025 | ** the sqlite_master table. We do not want to create it again. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1026 | ** |
| 1027 | ** If the pSelect argument is not NULL, it means that this routine |
| 1028 | ** was called to create a table generated from a |
| 1029 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
| 1030 | ** the new table will match the result set of the SELECT. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1031 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1032 | void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1033 | Table *p; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1034 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1035 | |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1036 | if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 1037 | p = pParse->pNewTable; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1038 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1039 | |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1040 | /* If the table is generated from a SELECT, then construct the |
| 1041 | ** list of columns and the text of the table. |
| 1042 | */ |
| 1043 | if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1044 | Table *pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect); |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1045 | if( pSelTab==0 ) return; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1046 | assert( p->aCol==0 ); |
| 1047 | p->nCol = pSelTab->nCol; |
| 1048 | p->aCol = pSelTab->aCol; |
| 1049 | pSelTab->nCol = 0; |
| 1050 | pSelTab->aCol = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1051 | sqlite3DeleteTable(0, pSelTab); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1054 | /* 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] | 1055 | ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
| 1056 | ** So do not write to the disk again. Extract the root page number |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1057 | ** for the table from the db->init.newTnum field. (The page number |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1058 | ** should have been put there by the sqliteOpenCb routine.) |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1059 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1060 | if( db->init.busy ){ |
| 1061 | p->tnum = db->init.newTnum; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1064 | /* If not initializing, then create a record for the new table |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1065 | ** in the SQLITE_MASTER table of the database. The record number |
| 1066 | ** for the new table entry should already be on the stack. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1067 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1068 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
| 1069 | ** file instead of into the main database file. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1070 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1071 | if( !db->init.busy ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1072 | int n; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1073 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1074 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1075 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1076 | if( v==0 ) return; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1077 | if( p->pSelect==0 ){ |
| 1078 | /* A regular table */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1079 | sqlite3VdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1080 | }else{ |
| 1081 | /* A view */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1082 | sqlite3VdbeAddOp(v, OP_Integer, 0, 0); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1083 | } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1084 | p->tnum = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1085 | sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
| 1086 | sqlite3VdbeOp3(v, OP_String, 0, 0, p->pSelect==0?"table":"view", P3_STATIC); |
| 1087 | sqlite3VdbeOp3(v, OP_String, 0, 0, p->zName, 0); |
| 1088 | sqlite3VdbeOp3(v, OP_String, 0, 0, p->zName, 0); |
| 1089 | sqlite3VdbeAddOp(v, OP_Dup, 4, 0); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1090 | if( pSelect ){ |
| 1091 | char *z = createTableStmt(p); |
| 1092 | n = z ? strlen(z) : 0; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1093 | sqlite3VdbeAddOp(v, OP_String, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1094 | sqlite3VdbeChangeP3(v, -1, z, n); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1095 | sqliteFree(z); |
| 1096 | }else{ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1097 | if( p->pSelect ){ |
| 1098 | sqlite3VdbeOp3(v, OP_String, 0, 0, "CREATE VIEW ", P3_STATIC); |
| 1099 | }else{ |
| 1100 | sqlite3VdbeOp3(v, OP_String, 0, 0, "CREATE TABLE ", P3_STATIC); |
| 1101 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1102 | assert( pEnd!=0 ); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1103 | n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1; |
| 1104 | sqlite3VdbeAddOp(v, OP_String, 0, 0); |
| 1105 | sqlite3VdbeChangeP3(v, -1, pParse->sNameToken.z, n); |
| 1106 | sqlite3VdbeAddOp(v, OP_Concat, 2, 0); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1107 | } |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1108 | sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1109 | sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1110 | if( !p->iDb ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1111 | sqlite3ChangeCookie(db, v, p->iDb); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1112 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1113 | sqlite3VdbeAddOp(v, OP_Close, 0, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1114 | if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1115 | sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0); |
| 1116 | sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1117 | pParse->nTab = 2; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1118 | sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1119 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1120 | sqlite3EndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1121 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1122 | |
| 1123 | /* Add the table to the in-memory representation of the database. |
| 1124 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1125 | if( pParse->explain==0 && pParse->nErr==0 ){ |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1126 | Table *pOld; |
| 1127 | FKey *pFKey; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1128 | pOld = sqlite3HashInsert(&db->aDb[p->iDb].tblHash, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1129 | p->zName, strlen(p->zName)+1, p); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1130 | if( pOld ){ |
| 1131 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 1132 | return; |
| 1133 | } |
| 1134 | for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 1135 | int nTo = strlen(pFKey->zTo) + 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1136 | pFKey->pNextTo = sqlite3HashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo); |
| 1137 | sqlite3HashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1138 | } |
| 1139 | pParse->pNewTable = 0; |
| 1140 | db->nTable++; |
| 1141 | db->flags |= SQLITE_InternChanges; |
| 1142 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1146 | ** The parser calls this routine in order to create a new VIEW |
| 1147 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1148 | void sqlite3CreateView( |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1149 | Parse *pParse, /* The parsing context */ |
| 1150 | Token *pBegin, /* The CREATE token that begins the statement */ |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 1151 | Token *pName1, /* The token that holds the name of the view */ |
| 1152 | Token *pName2, /* The token that holds the name of the view */ |
drh | 6276c1c | 2002-07-08 22:03:32 +0000 | [diff] [blame] | 1153 | Select *pSelect, /* A SELECT statement that will become the new view */ |
| 1154 | int isTemp /* TRUE for a TEMPORARY view */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1155 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1156 | Table *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1157 | int n; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1158 | const char *z; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1159 | Token sEnd; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1160 | DbFixer sFix; |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 1161 | Token *pName; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1162 | |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 1163 | sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1164 | p = pParse->pNewTable; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 1165 | if( p==0 || pParse->nErr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1166 | sqlite3SelectDelete(pSelect); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1167 | return; |
| 1168 | } |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 1169 | resolveSchemaName(pParse, pName1, pName2, &pName); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1170 | if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName) |
| 1171 | && sqlite3FixSelect(&sFix, pSelect) |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1172 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1173 | sqlite3SelectDelete(pSelect); |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1174 | return; |
| 1175 | } |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 1176 | |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1177 | /* Make a copy of the entire SELECT statement that defines the view. |
| 1178 | ** This will force all the Expr.token.z values to be dynamically |
| 1179 | ** allocated rather than point to the input string - which means that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1180 | ** they will persist after the current sqlite3_exec() call returns. |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1181 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1182 | p->pSelect = sqlite3SelectDup(pSelect); |
| 1183 | sqlite3SelectDelete(pSelect); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1184 | if( !pParse->db->init.busy ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1185 | sqlite3ViewGetColumnNames(pParse, p); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1186 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1187 | |
| 1188 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
| 1189 | ** the end. |
| 1190 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1191 | sEnd = pParse->sLastToken; |
| 1192 | if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){ |
| 1193 | sEnd.z += sEnd.n; |
| 1194 | } |
| 1195 | sEnd.n = 0; |
| 1196 | n = ((int)sEnd.z) - (int)pBegin->z; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1197 | z = pBegin->z; |
| 1198 | while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; } |
| 1199 | sEnd.z = &z[n-1]; |
| 1200 | sEnd.n = 1; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1201 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1202 | /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ |
| 1203 | sqlite3EndTable(pParse, &sEnd, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1204 | return; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1205 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1206 | |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1207 | /* |
| 1208 | ** The Table structure pTable is really a VIEW. Fill in the names of |
| 1209 | ** the columns of the view in the pTable structure. Return the number |
jplyon | cfa5684 | 2004-01-19 04:55:56 +0000 | [diff] [blame] | 1210 | ** 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] | 1211 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1212 | int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1213 | ExprList *pEList; |
| 1214 | Select *pSel; |
| 1215 | Table *pSelTab; |
| 1216 | int nErr = 0; |
| 1217 | |
| 1218 | assert( pTable ); |
| 1219 | |
| 1220 | /* A positive nCol means the columns names for this view are |
| 1221 | ** already known. |
| 1222 | */ |
| 1223 | if( pTable->nCol>0 ) return 0; |
| 1224 | |
| 1225 | /* A negative nCol is a special marker meaning that we are currently |
| 1226 | ** trying to compute the column names. If we enter this routine with |
| 1227 | ** a negative nCol, it means two or more views form a loop, like this: |
| 1228 | ** |
| 1229 | ** CREATE VIEW one AS SELECT * FROM two; |
| 1230 | ** CREATE VIEW two AS SELECT * FROM one; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 1231 | ** |
| 1232 | ** Actually, this error is caught previously and so the following test |
| 1233 | ** 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] | 1234 | */ |
| 1235 | if( pTable->nCol<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1236 | sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1237 | return 1; |
| 1238 | } |
| 1239 | |
| 1240 | /* If we get this far, it means we need to compute the table names. |
| 1241 | */ |
| 1242 | assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */ |
| 1243 | pSel = pTable->pSelect; |
| 1244 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1245 | /* Note that the call to sqlite3ResultSetOfSelect() will expand any |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1246 | ** "*" elements in this list. But we will need to restore the list |
| 1247 | ** back to its original configuration afterwards, so we save a copy of |
| 1248 | ** the original in pEList. |
| 1249 | */ |
| 1250 | pEList = pSel->pEList; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1251 | pSel->pEList = sqlite3ExprListDup(pEList); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1252 | if( pSel->pEList==0 ){ |
| 1253 | pSel->pEList = pEList; |
| 1254 | return 1; /* Malloc failed */ |
| 1255 | } |
| 1256 | pTable->nCol = -1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1257 | pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1258 | if( pSelTab ){ |
| 1259 | assert( pTable->aCol==0 ); |
| 1260 | pTable->nCol = pSelTab->nCol; |
| 1261 | pTable->aCol = pSelTab->aCol; |
| 1262 | pSelTab->nCol = 0; |
| 1263 | pSelTab->aCol = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1264 | sqlite3DeleteTable(0, pSelTab); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1265 | DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1266 | }else{ |
| 1267 | pTable->nCol = 0; |
| 1268 | nErr++; |
| 1269 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1270 | sqlite3SelectUnbind(pSel); |
| 1271 | sqlite3ExprListDelete(pSel->pEList); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1272 | pSel->pEList = pEList; |
| 1273 | return nErr; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | ** Clear the column names from the VIEW pTable. |
| 1278 | ** |
| 1279 | ** This routine is called whenever any other table or view is modified. |
| 1280 | ** The view passed into this routine might depend directly or indirectly |
| 1281 | ** on the modified or deleted table so we need to clear the old column |
| 1282 | ** names so that they will be recomputed. |
| 1283 | */ |
| 1284 | static void sqliteViewResetColumnNames(Table *pTable){ |
| 1285 | int i; |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 1286 | Column *pCol; |
| 1287 | assert( pTable!=0 && pTable->pSelect!=0 ); |
| 1288 | for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){ |
| 1289 | sqliteFree(pCol->zName); |
| 1290 | sqliteFree(pCol->zDflt); |
| 1291 | sqliteFree(pCol->zType); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1292 | } |
| 1293 | sqliteFree(pTable->aCol); |
| 1294 | pTable->aCol = 0; |
| 1295 | pTable->nCol = 0; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1299 | ** Clear the column names from every VIEW in database idx. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1300 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1301 | static void sqliteViewResetAll(sqlite *db, int idx){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1302 | HashElem *i; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1303 | if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1304 | for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1305 | Table *pTab = sqliteHashData(i); |
| 1306 | if( pTab->pSelect ){ |
| 1307 | sqliteViewResetColumnNames(pTab); |
| 1308 | } |
| 1309 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1310 | DbClearProperty(db, idx, DB_UnresetViews); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1314 | ** Given a token, look up a table with that name. If not found, leave |
| 1315 | ** an error for the parser to find and return NULL. |
| 1316 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1317 | Table *sqlite3TableFromToken(Parse *pParse, Token *pTok){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1318 | char *zName; |
| 1319 | Table *pTab; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1320 | zName = sqlite3TableNameFromToken(pTok); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1321 | if( zName==0 ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1322 | pTab = sqlite3FindTable(pParse->db, zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1323 | sqliteFree(zName); |
| 1324 | if( pTab==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1325 | sqlite3ErrorMsg(pParse, "no such table: %T", pTok); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1326 | } |
| 1327 | return pTab; |
| 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1332 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1333 | */ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1334 | void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){ |
| 1335 | Table *pTab; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1336 | Vdbe *v; |
| 1337 | int base; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1338 | sqlite *db = pParse->db; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1339 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1340 | |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1341 | if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table; |
| 1342 | assert( pName->nSrc==1 ); |
| 1343 | pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase); |
| 1344 | |
| 1345 | if( pTab==0 ) goto exit_drop_table; |
| 1346 | iDb = pTab->iDb; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1347 | assert( iDb>=0 && iDb<db->nDb ); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1348 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1349 | { |
| 1350 | int code; |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1351 | const char *zTab = SCHEMA_TABLE(pTab->iDb); |
| 1352 | const char *zDb = db->aDb[pTab->iDb].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1353 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1354 | goto exit_drop_table; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1355 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1356 | if( isView ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1357 | if( iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1358 | code = SQLITE_DROP_TEMP_VIEW; |
| 1359 | }else{ |
| 1360 | code = SQLITE_DROP_VIEW; |
| 1361 | } |
| 1362 | }else{ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1363 | if( iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1364 | code = SQLITE_DROP_TEMP_TABLE; |
| 1365 | }else{ |
| 1366 | code = SQLITE_DROP_TABLE; |
| 1367 | } |
| 1368 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1369 | if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){ |
| 1370 | goto exit_drop_table; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1371 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1372 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 1373 | goto exit_drop_table; |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 1374 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1375 | } |
| 1376 | #endif |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1377 | if( pTab->readOnly ){ |
| 1378 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1379 | pParse->nErr++; |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1380 | goto exit_drop_table; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1381 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1382 | if( isView && pTab->pSelect==0 ){ |
| 1383 | sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); |
| 1384 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1385 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1386 | if( !isView && pTab->pSelect ){ |
| 1387 | sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); |
| 1388 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1389 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1390 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1391 | /* Generate code to remove the table from the master table |
| 1392 | ** on disk. |
| 1393 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1394 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1395 | if( v ){ |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 1396 | static VdbeOpList dropTable[] = { |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1397 | { OP_Rewind, 0, ADDR(10), 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1398 | { OP_String, 0, 0, 0}, /* 1 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1399 | { OP_MemStore, 1, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1400 | { OP_MemLoad, 1, 0, 0}, /* 3 */ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1401 | { OP_Column, 0, 2, 0}, |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1402 | { OP_Ne, 0, ADDR(9), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1403 | { OP_Delete, 0, 0, 0}, |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1404 | { OP_Rewind, 0, ADDR(10), 0}, |
| 1405 | { OP_Goto, 0, ADDR(3), 0}, |
| 1406 | { OP_Next, 0, ADDR(3), 0}, /* 9 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1407 | }; |
| 1408 | Index *pIdx; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1409 | Trigger *pTrigger; |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1410 | sqlite3BeginWriteOperation(pParse, 0, pTab->iDb); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1411 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1412 | /* Drop all triggers associated with the table being dropped */ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1413 | pTrigger = pTab->pTrigger; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1414 | while( pTrigger ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1415 | assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1416 | sqlite3DropTriggerPtr(pParse, pTrigger, 1); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1417 | if( pParse->explain ){ |
| 1418 | pTrigger = pTrigger->pNext; |
| 1419 | }else{ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1420 | pTrigger = pTab->pTrigger; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1421 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1422 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1423 | |
| 1424 | /* Drop all SQLITE_MASTER entries that refer to the table */ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1425 | sqlite3OpenMasterTable(v, pTab->iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1426 | base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1427 | sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1428 | |
| 1429 | /* Drop all SQLITE_TEMP_MASTER entries that refer to the table */ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1430 | if( pTab->iDb!=1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1431 | sqlite3OpenMasterTable(v, 1); |
| 1432 | base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1433 | sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1436 | if( pTab->iDb!=1 ){ /* Temp database has no schema cookie */ |
| 1437 | sqlite3ChangeCookie(db, v, pTab->iDb); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1438 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1439 | sqlite3VdbeAddOp(v, OP_Close, 0, 0); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1440 | if( !isView ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1441 | sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb); |
| 1442 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1443 | sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1444 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1445 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1446 | sqlite3EndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1449 | /* Delete the in-memory description of the table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1450 | ** |
| 1451 | ** Exception: if the SQL statement began with the EXPLAIN keyword, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1452 | ** then no changes should be made. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1453 | */ |
| 1454 | if( !pParse->explain ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1455 | sqliteUnlinkAndDeleteTable(db, pTab); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1456 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1457 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1458 | sqliteViewResetAll(db, iDb); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1459 | |
| 1460 | exit_drop_table: |
| 1461 | sqlite3SrcListDelete(pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | /* |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1465 | ** This routine is called to create a new foreign key on the table |
| 1466 | ** currently under construction. pFromCol determines which columns |
| 1467 | ** in the current table point to the foreign key. If pFromCol==0 then |
| 1468 | ** connect the key to the last column inserted. pTo is the name of |
| 1469 | ** the table referred to. pToCol is a list of tables in the other |
| 1470 | ** pTo table that the foreign key points to. flags contains all |
| 1471 | ** information about the conflict resolution algorithms specified |
| 1472 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
| 1473 | ** |
| 1474 | ** An FKey structure is created and added to the table currently |
| 1475 | ** under construction in the pParse->pNewTable field. The new FKey |
| 1476 | ** is not linked into db->aFKey at this point - that does not happen |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1477 | ** until sqlite3EndTable(). |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1478 | ** |
| 1479 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1480 | ** to sqlite3DeferForeignKey() might change this to DEFERRED. |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1481 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1482 | void sqlite3CreateForeignKey( |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1483 | Parse *pParse, /* Parsing context */ |
| 1484 | IdList *pFromCol, /* Columns in this table that point to other table */ |
| 1485 | Token *pTo, /* Name of the other table */ |
| 1486 | IdList *pToCol, /* Columns in the other table */ |
| 1487 | int flags /* Conflict resolution algorithms. */ |
| 1488 | ){ |
| 1489 | Table *p = pParse->pNewTable; |
| 1490 | int nByte; |
| 1491 | int i; |
| 1492 | int nCol; |
| 1493 | char *z; |
| 1494 | FKey *pFKey = 0; |
| 1495 | |
| 1496 | assert( pTo!=0 ); |
| 1497 | if( p==0 || pParse->nErr ) goto fk_end; |
| 1498 | if( pFromCol==0 ){ |
| 1499 | int iCol = p->nCol-1; |
| 1500 | if( iCol<0 ) goto fk_end; |
| 1501 | if( pToCol && pToCol->nId!=1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1502 | sqlite3ErrorMsg(pParse, "foreign key on %s" |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1503 | " should reference only one column of table %T", |
| 1504 | p->aCol[iCol].zName, pTo); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1505 | goto fk_end; |
| 1506 | } |
| 1507 | nCol = 1; |
| 1508 | }else if( pToCol && pToCol->nId!=pFromCol->nId ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1509 | sqlite3ErrorMsg(pParse, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1510 | "number of columns in foreign key does not match the number of " |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1511 | "columns in the referenced table"); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1512 | goto fk_end; |
| 1513 | }else{ |
| 1514 | nCol = pFromCol->nId; |
| 1515 | } |
| 1516 | nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
| 1517 | if( pToCol ){ |
| 1518 | for(i=0; i<pToCol->nId; i++){ |
| 1519 | nByte += strlen(pToCol->a[i].zName) + 1; |
| 1520 | } |
| 1521 | } |
| 1522 | pFKey = sqliteMalloc( nByte ); |
| 1523 | if( pFKey==0 ) goto fk_end; |
| 1524 | pFKey->pFrom = p; |
| 1525 | pFKey->pNextFrom = p->pFKey; |
drh | df68f6b | 2002-09-21 15:57:57 +0000 | [diff] [blame] | 1526 | z = (char*)&pFKey[1]; |
| 1527 | pFKey->aCol = (struct sColMap*)z; |
| 1528 | z += sizeof(struct sColMap)*nCol; |
| 1529 | pFKey->zTo = z; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1530 | memcpy(z, pTo->z, pTo->n); |
| 1531 | z[pTo->n] = 0; |
| 1532 | z += pTo->n+1; |
| 1533 | pFKey->pNextTo = 0; |
| 1534 | pFKey->nCol = nCol; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1535 | if( pFromCol==0 ){ |
| 1536 | pFKey->aCol[0].iFrom = p->nCol-1; |
| 1537 | }else{ |
| 1538 | for(i=0; i<nCol; i++){ |
| 1539 | int j; |
| 1540 | for(j=0; j<p->nCol; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1541 | if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1542 | pFKey->aCol[i].iFrom = j; |
| 1543 | break; |
| 1544 | } |
| 1545 | } |
| 1546 | if( j>=p->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1547 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1548 | "unknown column \"%s\" in foreign key definition", |
| 1549 | pFromCol->a[i].zName); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1550 | goto fk_end; |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | if( pToCol ){ |
| 1555 | for(i=0; i<nCol; i++){ |
| 1556 | int n = strlen(pToCol->a[i].zName); |
| 1557 | pFKey->aCol[i].zCol = z; |
| 1558 | memcpy(z, pToCol->a[i].zName, n); |
| 1559 | z[n] = 0; |
| 1560 | z += n+1; |
| 1561 | } |
| 1562 | } |
| 1563 | pFKey->isDeferred = 0; |
| 1564 | pFKey->deleteConf = flags & 0xff; |
| 1565 | pFKey->updateConf = (flags >> 8 ) & 0xff; |
| 1566 | pFKey->insertConf = (flags >> 16 ) & 0xff; |
| 1567 | |
| 1568 | /* Link the foreign key to the table as the last step. |
| 1569 | */ |
| 1570 | p->pFKey = pFKey; |
| 1571 | pFKey = 0; |
| 1572 | |
| 1573 | fk_end: |
| 1574 | sqliteFree(pFKey); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1575 | sqlite3IdListDelete(pFromCol); |
| 1576 | sqlite3IdListDelete(pToCol); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 1581 | ** clause is seen as part of a foreign key definition. The isDeferred |
| 1582 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 1583 | ** The behavior of the most recently created foreign key is adjusted |
| 1584 | ** accordingly. |
| 1585 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1586 | void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1587 | Table *pTab; |
| 1588 | FKey *pFKey; |
| 1589 | if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; |
| 1590 | pFKey->isDeferred = isDeferred; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1594 | ** Create a new index for an SQL table. pIndex is the name of the index |
| 1595 | ** 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] | 1596 | ** be NULL for a primary key or an index that is created to satisfy a |
| 1597 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1598 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 1599 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1600 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1601 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 1602 | ** is a primary key or unique-constraint on the most recent column added |
| 1603 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1604 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1605 | void sqlite3CreateIndex( |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1606 | Parse *pParse, /* All information about this parse */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1607 | Token *pName1, /* First part of index name. May be NULL */ |
| 1608 | Token *pName2, /* Second part of index name. May be NULL */ |
| 1609 | Token *pTblName, /* Name of the table to index. Use pParse->pNewTable if 0 */ |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1610 | IdList *pList, /* A list of columns to be indexed */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1611 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1612 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
| 1613 | Token *pEnd /* The ")" that closes the CREATE INDEX statement */ |
| 1614 | ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1615 | Table *pTab = 0; /* Table to be indexed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1616 | Index *pIndex; /* The index to be created */ |
| 1617 | char *zName = 0; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 1618 | int i, j; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1619 | Token nullId; /* Fake token for an empty ID list */ |
| 1620 | DbFixer sFix; /* For assigning database names to pTable */ |
drh | 4925ca0 | 2003-11-27 00:48:57 +0000 | [diff] [blame] | 1621 | int isTemp; /* True for a temporary index */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1622 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1623 | |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1624 | int iDb; /* Index of the database that is being written */ |
| 1625 | Token *pName = 0; /* Unqualified name of the index to create */ |
| 1626 | |
| 1627 | /* |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1628 | if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1629 | if( db->init.busy |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1630 | && sqlite3FixInit(&sFix, pParse, db->init.iDb, "index", pName) |
| 1631 | && sqlite3FixSrcList(&sFix, pTable) |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1632 | ){ |
| 1633 | goto exit_create_index; |
| 1634 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1635 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1636 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1637 | /* |
| 1638 | ** Find the table that is to be indexed. Return early if not found. |
| 1639 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1640 | if( pTblName!=0 ){ |
| 1641 | char *zTblName; |
| 1642 | |
| 1643 | /* Use the two-part index name to determine the database |
| 1644 | ** to search for the table. If no database name is specified, |
| 1645 | ** iDb is set to 0. In this case search both the temp and main |
| 1646 | ** databases for the named table. |
| 1647 | */ |
| 1648 | assert( pName1 && pName2 ); |
| 1649 | iDb = resolveSchemaName(pParse, pName1, pName2, &pName); |
| 1650 | if( iDb<0 ) goto exit_create_index; |
| 1651 | |
| 1652 | /* Now search for the table in the database iDb. If iDb is |
| 1653 | ** zero, then search both the "main" and "temp" databases. |
| 1654 | */ |
| 1655 | zTblName = sqlite3TableNameFromToken(pTblName); |
| 1656 | if( !zTblName ){ |
| 1657 | pParse->nErr++; |
| 1658 | pParse->rc = SQLITE_NOMEM; |
| 1659 | goto exit_create_index; |
| 1660 | } |
| 1661 | assert( pName1!=0 ); |
| 1662 | if( iDb==0 ){ |
| 1663 | pTab = sqlite3FindTable(db, zTblName, "temp"); |
| 1664 | } |
| 1665 | if( !pTab ){ |
| 1666 | pTab = sqlite3LocateTable(pParse, zTblName, db->aDb[iDb].zName); |
| 1667 | } |
| 1668 | sqliteFree( zTblName ); |
| 1669 | if( !pTab ) goto exit_create_index; |
| 1670 | iDb = pTab->iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1671 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1672 | assert( pName==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1673 | pTab = pParse->pNewTable; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1674 | iDb = pTab->iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1675 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1676 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1677 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 1678 | if( pTab->readOnly ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1679 | sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 1680 | goto exit_create_index; |
| 1681 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1682 | /* |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1683 | if( pTab->iDb>=2 && db->init.busy==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1684 | sqlite3ErrorMsg(pParse, "table %s may not have indices added", pTab->zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1685 | goto exit_create_index; |
| 1686 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1687 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1688 | if( pTab->pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1689 | sqlite3ErrorMsg(pParse, "views may not be indexed"); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1690 | goto exit_create_index; |
| 1691 | } |
drh | 4925ca0 | 2003-11-27 00:48:57 +0000 | [diff] [blame] | 1692 | isTemp = pTab->iDb==1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1693 | |
| 1694 | /* |
| 1695 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1696 | ** index or table with the same name. |
| 1697 | ** |
| 1698 | ** Exception: If we are reading the names of permanent indices from the |
| 1699 | ** sqlite_master table (because some other process changed the schema) and |
| 1700 | ** 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] | 1701 | ** index, then we will continue to process this index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1702 | ** |
| 1703 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1704 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 1705 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1706 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1707 | if( pName && !db->init.busy ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1708 | Index *pISameName; /* Another index with the same name */ |
| 1709 | Table *pTSameName; /* A table with same name as the index */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1710 | zName = sqliteStrNDup(pName->z, pName->n); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1711 | if( zName==0 ) goto exit_create_index; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1712 | if( (pISameName = sqlite3FindIndex(db, zName, 0))!=0 ){ |
| 1713 | sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1714 | goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1715 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1716 | if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){ |
| 1717 | sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1718 | goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1719 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1720 | }else if( pName==0 ){ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1721 | char zBuf[30]; |
| 1722 | int n; |
| 1723 | Index *pLoop; |
| 1724 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
| 1725 | sprintf(zBuf,"%d)",n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1726 | zName = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1727 | sqlite3SetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1728 | if( zName==0 ) goto exit_create_index; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1729 | }else{ |
| 1730 | zName = sqliteStrNDup(pName->z, pName->n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1733 | /* Check for authorization to create an index. |
| 1734 | */ |
| 1735 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1736 | { |
| 1737 | const char *zDb = db->aDb[pTab->iDb].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1738 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1739 | goto exit_create_index; |
| 1740 | } |
| 1741 | i = SQLITE_CREATE_INDEX; |
| 1742 | if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1743 | if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1744 | goto exit_create_index; |
| 1745 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1746 | } |
| 1747 | #endif |
| 1748 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1749 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1750 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1751 | ** So create a fake list to simulate this. |
| 1752 | */ |
| 1753 | if( pList==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1754 | nullId.z = pTab->aCol[pTab->nCol-1].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1755 | nullId.n = strlen(nullId.z); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1756 | pList = sqlite3IdListAppend(0, &nullId); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1757 | if( pList==0 ) goto exit_create_index; |
| 1758 | } |
| 1759 | |
| 1760 | /* |
| 1761 | ** Allocate the index structure. |
| 1762 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 1763 | pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1764 | (sizeof(int) + sizeof(CollSeq*))*pList->nId ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1765 | if( pIndex==0 ) goto exit_create_index; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1766 | pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nId]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1767 | pIndex->zName = (char*)&pIndex->aiColumn[pList->nId]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1768 | strcpy(pIndex->zName, zName); |
| 1769 | pIndex->pTable = pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1770 | pIndex->nColumn = pList->nId; |
drh | ea1ba17 | 2003-04-20 00:00:23 +0000 | [diff] [blame] | 1771 | pIndex->onError = onError; |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1772 | pIndex->autoIndex = pName==0; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1773 | pIndex->iDb = iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1774 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1775 | /* Scan the names of the columns of the table to be indexed and |
| 1776 | ** load the column indices into the Index structure. Report an error |
| 1777 | ** if any column is not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1778 | */ |
| 1779 | for(i=0; i<pList->nId; i++){ |
| 1780 | for(j=0; j<pTab->nCol; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1781 | if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1782 | } |
| 1783 | if( j>=pTab->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1784 | sqlite3ErrorMsg(pParse, "table %s has no column named %s", |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1785 | pTab->zName, pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1786 | sqliteFree(pIndex); |
| 1787 | goto exit_create_index; |
| 1788 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1789 | pIndex->aiColumn[i] = j; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1790 | pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1791 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1792 | pIndex->keyInfo.nField = pList->nId; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1793 | |
| 1794 | /* Link the new Index structure to its table and to the other |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1795 | ** in-memory database structures. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1796 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1797 | if( !pParse->explain ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1798 | Index *p; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1799 | p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash, |
drh | 3c8bf55 | 2003-07-01 18:13:14 +0000 | [diff] [blame] | 1800 | pIndex->zName, strlen(pIndex->zName)+1, pIndex); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1801 | if( p ){ |
| 1802 | assert( p==pIndex ); /* Malloc must have failed */ |
| 1803 | sqliteFree(pIndex); |
| 1804 | goto exit_create_index; |
| 1805 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1806 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1807 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1808 | |
| 1809 | /* When adding an index to the list of indices for a table, make |
| 1810 | ** sure all indices labeled OE_Replace come after all those labeled |
| 1811 | ** OE_Ignore. This is necessary for the correct operation of UPDATE |
| 1812 | ** and INSERT. |
| 1813 | */ |
| 1814 | if( onError!=OE_Replace || pTab->pIndex==0 |
| 1815 | || pTab->pIndex->onError==OE_Replace){ |
| 1816 | pIndex->pNext = pTab->pIndex; |
| 1817 | pTab->pIndex = pIndex; |
| 1818 | }else{ |
| 1819 | Index *pOther = pTab->pIndex; |
| 1820 | while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
| 1821 | pOther = pOther->pNext; |
| 1822 | } |
| 1823 | pIndex->pNext = pOther->pNext; |
| 1824 | pOther->pNext = pIndex; |
| 1825 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1826 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1827 | /* 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] | 1828 | ** "sqlite_master" table on the disk. So do not write to the disk |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1829 | ** again. Extract the table number from the db->init.newTnum field. |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1830 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1831 | if( db->init.busy && pTblName!=0 ){ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1832 | pIndex->tnum = db->init.newTnum; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1835 | /* 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] | 1836 | ** involves writing the index into the master table and filling in the |
| 1837 | ** index with the current table contents. |
| 1838 | ** |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1839 | ** The db->init.busy is 0 when the user first enters a CREATE INDEX |
| 1840 | ** command. db->init.busy is 1 when a database is opened and |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1841 | ** CREATE INDEX statements are read out of the master table. In |
| 1842 | ** the latter case the index already exists on disk, which is why |
| 1843 | ** we don't want to recreate it. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1844 | ** |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1845 | ** If pTblName==0 it means this index is generated as a primary key |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1846 | ** or UNIQUE constraint of a CREATE TABLE statement. Since the table |
| 1847 | ** has just been created, it contains no data and the index initialization |
| 1848 | ** step can be skipped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1849 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1850 | else if( db->init.busy==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1851 | int n; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1852 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1853 | int lbl1, lbl2; |
| 1854 | int i; |
| 1855 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1856 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1857 | if( v==0 ) goto exit_create_index; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1858 | if( pTblName!=0 ){ |
| 1859 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 1860 | sqlite3OpenMasterTable(v, iDb); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1861 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1862 | sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0); |
| 1863 | sqlite3VdbeOp3(v, OP_String, 0, 0, "index", P3_STATIC); |
| 1864 | sqlite3VdbeOp3(v, OP_String, 0, 0, pIndex->zName, 0); |
| 1865 | sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->zName, 0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1866 | sqlite3VdbeOp3(v, OP_CreateIndex, 0, iDb,(char*)&pIndex->tnum,P3_POINTER); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1867 | pIndex->tnum = 0; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1868 | if( pTblName ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1869 | sqlite3VdbeCode(v, |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 1870 | OP_Dup, 0, 0, |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1871 | OP_Integer, iDb, 0, |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 1872 | 0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1873 | sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0, |
| 1874 | (char*)&pIndex->keyInfo, P3_KEYINFO); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1875 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1876 | sqlite3VdbeAddOp(v, OP_String, 0, 0); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1877 | if( pStart && pEnd ){ |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame^] | 1878 | sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1879 | sqlite3VdbeAddOp(v, OP_String, 0, 0); |
| 1880 | n = Addr(pEnd->z) - Addr(pName->z) + 1; |
| 1881 | sqlite3VdbeChangeP3(v, -1, pName->z, n); |
| 1882 | sqlite3VdbeAddOp(v, OP_Concat, 2, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1883 | } |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1884 | sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1885 | sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1886 | if( pTblName ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1887 | sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1888 | sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum); |
| 1889 | /* VdbeComment((v, "%s", pTab->zName)); */ |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1890 | sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1891 | lbl2 = sqlite3VdbeMakeLabel(v); |
| 1892 | sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2); |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame^] | 1893 | lbl1 = sqlite3VdbeCurrentAddr(v); |
| 1894 | sqlite3GenerateIndexKey(v, pIndex, 2); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1895 | sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None, |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 1896 | "indexed columns are not unique", P3_STATIC); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1897 | sqlite3VdbeAddOp(v, OP_Next, 2, lbl1); |
| 1898 | sqlite3VdbeResolveLabel(v, lbl2); |
| 1899 | sqlite3VdbeAddOp(v, OP_Close, 2, 0); |
| 1900 | sqlite3VdbeAddOp(v, OP_Close, 1, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1901 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1902 | if( pTblName!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1903 | if( !isTemp ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1904 | sqlite3ChangeCookie(db, v, iDb); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1905 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1906 | sqlite3VdbeAddOp(v, OP_Close, 0, 0); |
| 1907 | sqlite3EndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1908 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1911 | /* Clean up before exiting */ |
| 1912 | exit_create_index: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1913 | sqlite3IdListDelete(pList); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1914 | /* sqlite3SrcListDelete(pTable); */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1915 | sqliteFree(zName); |
| 1916 | return; |
| 1917 | } |
| 1918 | |
| 1919 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 1920 | ** This routine will drop an existing named index. This routine |
| 1921 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1922 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1923 | void sqlite3DropIndex(Parse *pParse, SrcList *pName){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1924 | Index *pIndex; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1925 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1926 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1927 | |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1928 | if( pParse->nErr || sqlite3_malloc_failed ) return; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1929 | assert( pName->nSrc==1 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1930 | pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1931 | if( pIndex==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1932 | sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1933 | goto exit_drop_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1934 | } |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1935 | if( pIndex->autoIndex ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1936 | sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1937 | "or PRIMARY KEY constraint cannot be dropped", 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1938 | goto exit_drop_index; |
| 1939 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1940 | /* |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1941 | if( pIndex->iDb>1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1942 | sqlite3ErrorMsg(pParse, "cannot alter schema of attached " |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1943 | "databases", 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1944 | goto exit_drop_index; |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1945 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1946 | */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1947 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 1948 | { |
| 1949 | int code = SQLITE_DROP_INDEX; |
| 1950 | Table *pTab = pIndex->pTable; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1951 | const char *zDb = db->aDb[pIndex->iDb].zName; |
| 1952 | const char *zTab = SCHEMA_TABLE(pIndex->iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1953 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1954 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1955 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1956 | if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1957 | if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1958 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1959 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 1960 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1961 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1962 | |
| 1963 | /* Generate code to remove the index and from the master table */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1964 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1965 | if( v ){ |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 1966 | static VdbeOpList dropIndex[] = { |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1967 | { OP_Rewind, 0, ADDR(9), 0}, |
| 1968 | { OP_String, 0, 0, 0}, /* 1 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1969 | { OP_MemStore, 1, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1970 | { OP_MemLoad, 1, 0, 0}, /* 3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1971 | { OP_Column, 0, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1972 | { OP_Eq, 0, ADDR(8), 0}, |
| 1973 | { OP_Next, 0, ADDR(3), 0}, |
| 1974 | { OP_Goto, 0, ADDR(9), 0}, |
| 1975 | { OP_Delete, 0, 0, 0}, /* 8 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1976 | }; |
| 1977 | int base; |
| 1978 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1979 | sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb); |
| 1980 | sqlite3OpenMasterTable(v, pIndex->iDb); |
| 1981 | base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex); |
| 1982 | sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1983 | if( pIndex->iDb!=1 ){ |
| 1984 | sqlite3ChangeCookie(db, v, pIndex->iDb); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1985 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1986 | sqlite3VdbeAddOp(v, OP_Close, 0, 0); |
| 1987 | sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb); |
| 1988 | sqlite3EndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1991 | /* Delete the in-memory description of this index. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1992 | */ |
| 1993 | if( !pParse->explain ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1994 | sqlite3UnlinkAndDeleteIndex(db, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1995 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1996 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1997 | |
| 1998 | exit_drop_index: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1999 | sqlite3SrcListDelete(pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2003 | ** Append a new element to the given IdList. Create a new IdList if |
| 2004 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2005 | ** |
| 2006 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2007 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2008 | IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2009 | if( pList==0 ){ |
| 2010 | pList = sqliteMalloc( sizeof(IdList) ); |
| 2011 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2012 | pList->nAlloc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2013 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2014 | if( pList->nId>=pList->nAlloc ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 2015 | struct IdList_item *a; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2016 | pList->nAlloc = pList->nAlloc*2 + 5; |
| 2017 | a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 2018 | if( a==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2019 | sqlite3IdListDelete(pList); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2020 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2021 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 2022 | pList->a = a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2023 | } |
| 2024 | memset(&pList->a[pList->nId], 0, sizeof(pList->a[0])); |
| 2025 | if( pToken ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2026 | char **pz = &pList->a[pList->nId].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2027 | sqlite3SetNString(pz, pToken->z, pToken->n, 0); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2028 | if( *pz==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2029 | sqlite3IdListDelete(pList); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2030 | return 0; |
| 2031 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2032 | sqlite3Dequote(*pz); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2033 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2034 | } |
| 2035 | pList->nId++; |
| 2036 | return pList; |
| 2037 | } |
| 2038 | |
| 2039 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2040 | ** Append a new table name to the given SrcList. Create a new SrcList if |
| 2041 | ** need be. A new entry is created in the SrcList even if pToken is NULL. |
| 2042 | ** |
| 2043 | ** A new SrcList is returned, or NULL if malloc() fails. |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2044 | ** |
| 2045 | ** If pDatabase is not null, it means that the table has an optional |
| 2046 | ** database name prefix. Like this: "database.table". The pDatabase |
| 2047 | ** points to the table name and the pTable points to the database name. |
| 2048 | ** The SrcList.a[].zName field is filled with the table name which might |
| 2049 | ** come from pTable (if pDatabase is NULL) or from pDatabase. |
| 2050 | ** SrcList.a[].zDatabase is filled with the database name from pTable, |
| 2051 | ** or with NULL if no database is specified. |
| 2052 | ** |
| 2053 | ** In other words, if call like this: |
| 2054 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2055 | ** sqlite3SrcListAppend(A,B,0); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2056 | ** |
| 2057 | ** Then B is a table name and the database name is unspecified. If called |
| 2058 | ** like this: |
| 2059 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2060 | ** sqlite3SrcListAppend(A,B,C); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2061 | ** |
| 2062 | ** Then C is the table name and B is the database name. |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2063 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2064 | SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2065 | if( pList==0 ){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2066 | pList = sqliteMalloc( sizeof(SrcList) ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2067 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2068 | pList->nAlloc = 1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2069 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2070 | if( pList->nSrc>=pList->nAlloc ){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2071 | SrcList *pNew; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2072 | pList->nAlloc *= 2; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2073 | pNew = sqliteRealloc(pList, |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2074 | sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) ); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2075 | if( pNew==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2076 | sqlite3SrcListDelete(pList); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2077 | return 0; |
| 2078 | } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2079 | pList = pNew; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2080 | } |
| 2081 | memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0])); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2082 | if( pDatabase && pDatabase->z==0 ){ |
| 2083 | pDatabase = 0; |
| 2084 | } |
| 2085 | if( pDatabase && pTable ){ |
| 2086 | Token *pTemp = pDatabase; |
| 2087 | pDatabase = pTable; |
| 2088 | pTable = pTemp; |
| 2089 | } |
| 2090 | if( pTable ){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2091 | char **pz = &pList->a[pList->nSrc].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2092 | sqlite3SetNString(pz, pTable->z, pTable->n, 0); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2093 | if( *pz==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2094 | sqlite3SrcListDelete(pList); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2095 | return 0; |
| 2096 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2097 | sqlite3Dequote(*pz); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2098 | } |
| 2099 | } |
| 2100 | if( pDatabase ){ |
| 2101 | char **pz = &pList->a[pList->nSrc].zDatabase; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2102 | sqlite3SetNString(pz, pDatabase->z, pDatabase->n, 0); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2103 | if( *pz==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2104 | sqlite3SrcListDelete(pList); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2105 | return 0; |
| 2106 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2107 | sqlite3Dequote(*pz); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2108 | } |
| 2109 | } |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 2110 | pList->a[pList->nSrc].iCursor = -1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2111 | pList->nSrc++; |
| 2112 | return pList; |
| 2113 | } |
| 2114 | |
| 2115 | /* |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 2116 | ** Assign cursors to all tables in a SrcList |
| 2117 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2118 | void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 2119 | int i; |
| 2120 | for(i=0; i<pList->nSrc; i++){ |
| 2121 | if( pList->a[i].iCursor<0 ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 2122 | pList->a[i].iCursor = pParse->nTab++; |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2128 | ** Add an alias to the last identifier on the given identifier list. |
| 2129 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2130 | void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2131 | if( pList && pList->nSrc>0 ){ |
| 2132 | int i = pList->nSrc - 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2133 | sqlite3SetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0); |
| 2134 | sqlite3Dequote(pList->a[i].zAlias); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2139 | ** Delete an IdList. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2140 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2141 | void sqlite3IdListDelete(IdList *pList){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2142 | int i; |
| 2143 | if( pList==0 ) return; |
| 2144 | for(i=0; i<pList->nId; i++){ |
| 2145 | sqliteFree(pList->a[i].zName); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2146 | } |
| 2147 | sqliteFree(pList->a); |
| 2148 | sqliteFree(pList); |
| 2149 | } |
| 2150 | |
| 2151 | /* |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 2152 | ** Return the index in pList of the identifier named zId. Return -1 |
| 2153 | ** if not found. |
| 2154 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2155 | int sqlite3IdListIndex(IdList *pList, const char *zName){ |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 2156 | int i; |
| 2157 | if( pList==0 ) return -1; |
| 2158 | for(i=0; i<pList->nId; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2159 | if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 2160 | } |
| 2161 | return -1; |
| 2162 | } |
| 2163 | |
| 2164 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2165 | ** Delete an entire SrcList including all its substructure. |
| 2166 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2167 | void sqlite3SrcListDelete(SrcList *pList){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2168 | int i; |
| 2169 | if( pList==0 ) return; |
| 2170 | for(i=0; i<pList->nSrc; i++){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2171 | sqliteFree(pList->a[i].zDatabase); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2172 | sqliteFree(pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2173 | sqliteFree(pList->a[i].zAlias); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 2174 | if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2175 | sqlite3DeleteTable(0, pList->a[i].pTab); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2176 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2177 | sqlite3SelectDelete(pList->a[i].pSelect); |
| 2178 | sqlite3ExprDelete(pList->a[i].pOn); |
| 2179 | sqlite3IdListDelete(pList->a[i].pUsing); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2180 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2181 | sqliteFree(pList); |
| 2182 | } |
| 2183 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 2184 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2185 | ** Begin a transaction |
| 2186 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2187 | void sqlite3BeginTransaction(Parse *pParse, int onError){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2188 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2189 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2190 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 2191 | if( pParse->nErr || sqlite3_malloc_failed ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2192 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2193 | if( db->flags & SQLITE_InTrans ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2194 | sqlite3ErrorMsg(pParse, "cannot start a transaction within a transaction"); |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2195 | return; |
| 2196 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2197 | sqlite3BeginWriteOperation(pParse, 0, 0); |
drh | 02f75f1 | 2004-02-24 01:04:11 +0000 | [diff] [blame] | 2198 | if( !pParse->explain ){ |
| 2199 | db->flags |= SQLITE_InTrans; |
| 2200 | db->onError = onError; |
| 2201 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | ** Commit a transaction |
| 2206 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2207 | void sqlite3CommitTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2208 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2209 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2210 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 2211 | if( pParse->nErr || sqlite3_malloc_failed ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2212 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2213 | if( (db->flags & SQLITE_InTrans)==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2214 | sqlite3ErrorMsg(pParse, "cannot commit - no transaction is active"); |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2215 | return; |
| 2216 | } |
drh | 02f75f1 | 2004-02-24 01:04:11 +0000 | [diff] [blame] | 2217 | if( !pParse->explain ){ |
| 2218 | db->flags &= ~SQLITE_InTrans; |
| 2219 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2220 | sqlite3EndWriteOperation(pParse); |
drh | 02f75f1 | 2004-02-24 01:04:11 +0000 | [diff] [blame] | 2221 | if( !pParse->explain ){ |
| 2222 | db->onError = OE_Default; |
| 2223 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | ** Rollback a transaction |
| 2228 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2229 | void sqlite3RollbackTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2230 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2231 | Vdbe *v; |
| 2232 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2233 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 2234 | if( pParse->nErr || sqlite3_malloc_failed ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2235 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2236 | if( (db->flags & SQLITE_InTrans)==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2237 | sqlite3ErrorMsg(pParse, "cannot rollback - no transaction is active"); |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2238 | return; |
| 2239 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2240 | v = sqlite3GetVdbe(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2241 | if( v ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2242 | sqlite3VdbeAddOp(v, OP_Rollback, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2243 | } |
drh | 02f75f1 | 2004-02-24 01:04:11 +0000 | [diff] [blame] | 2244 | if( !pParse->explain ){ |
| 2245 | db->flags &= ~SQLITE_InTrans; |
| 2246 | db->onError = OE_Default; |
| 2247 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2248 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2251 | ** Generate VDBE code that will verify the schema cookie for all |
| 2252 | ** named database files. |
| 2253 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2254 | void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2255 | sqlite *db = pParse->db; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2256 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2257 | assert( iDb>=0 && iDb<db->nDb ); |
| 2258 | assert( db->aDb[iDb].pBt!=0 ); |
| 2259 | if( iDb!=1 && !DbHasProperty(db, iDb, DB_Cookie) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2260 | sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2261 | DbSetProperty(db, iDb, DB_Cookie); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2262 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2266 | ** Generate VDBE code that prepares for doing an operation that |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 2267 | ** might change the database. |
| 2268 | ** |
| 2269 | ** This routine starts a new transaction if we are not already within |
| 2270 | ** a transaction. If we are already within a transaction, then a checkpoint |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2271 | ** is set if the setStatement parameter is true. A checkpoint should |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 2272 | ** be set for operations that might fail (due to a constraint) part of |
| 2273 | ** the way through and which will need to undo some writes without having to |
| 2274 | ** rollback the whole transaction. For operations where all constraints |
| 2275 | ** can be checked before any changes are made to the database, it is never |
| 2276 | ** necessary to undo a write and the checkpoint should not be set. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2277 | ** |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2278 | ** Only database iDb and the temp database are made writable by this call. |
| 2279 | ** If iDb==0, then the main and temp databases are made writable. If |
| 2280 | ** iDb==1 then only the temp database is made writable. If iDb>1 then the |
| 2281 | ** specified auxiliary database and the temp database are made writable. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2282 | */ |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2283 | void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2284 | Vdbe *v; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2285 | sqlite *db = pParse->db; |
| 2286 | if( DbHasProperty(db, iDb, DB_Locked) ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2287 | v = sqlite3GetVdbe(pParse); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2288 | if( v==0 ) return; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2289 | if( !db->aDb[iDb].inTrans ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2290 | sqlite3VdbeAddOp(v, OP_Transaction, iDb, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2291 | DbSetProperty(db, iDb, DB_Locked); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2292 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2293 | if( iDb!=1 ){ |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2294 | sqlite3BeginWriteOperation(pParse, setStatement, 1); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2295 | } |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2296 | }else if( setStatement ){ |
| 2297 | sqlite3VdbeAddOp(v, OP_Statement, iDb, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2298 | DbSetProperty(db, iDb, DB_Locked); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2303 | ** Generate code that concludes an operation that may have changed |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2304 | ** the database. If a statement transaction was started, then emit |
| 2305 | ** an OP_Commit that will cause the changes to be committed to disk. |
| 2306 | ** |
| 2307 | ** Note that checkpoints are automatically committed at the end of |
| 2308 | ** a statement. Note also that there can be multiple calls to |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2309 | ** sqlite3BeginWriteOperation() but there should only be a single |
| 2310 | ** call to sqlite3EndWriteOperation() at the conclusion of the statement. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2311 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2312 | void sqlite3EndWriteOperation(Parse *pParse){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2313 | Vdbe *v; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2314 | sqlite *db = pParse->db; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 2315 | if( pParse->trigStack ) return; /* if this is in a trigger */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2316 | v = sqlite3GetVdbe(pParse); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2317 | if( v==0 ) return; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2318 | if( db->flags & SQLITE_InTrans ){ |
| 2319 | /* A BEGIN has executed. Do not commit until we see an explicit |
| 2320 | ** COMMIT statement. */ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2321 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2322 | sqlite3VdbeAddOp(v, OP_Commit, 0, 0); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2323 | } |
| 2324 | } |