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