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