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 |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 24 | ** |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 25 | ** $Id: build.c,v 1.377 2006/01/11 21:41:22 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 26 | */ |
| 27 | #include "sqliteInt.h" |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 29 | |
| 30 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 31 | ** This routine is called when a new SQL statement is beginning to |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 32 | ** be parsed. Initialize the pParse structure as needed. |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 33 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 34 | void sqlite3BeginParse(Parse *pParse, int explainFlag){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 35 | pParse->explain = explainFlag; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 36 | pParse->nVar = 0; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 37 | } |
| 38 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 39 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 40 | /* |
| 41 | ** The TableLock structure is only used by the sqlite3TableLock() and |
| 42 | ** codeTableLocks() functions. |
| 43 | */ |
| 44 | struct TableLock { |
| 45 | int iDb; |
| 46 | int iTab; |
| 47 | u8 isWriteLock; |
| 48 | const char *zName; |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | ** Have the compiled statement lock the table with rootpage iTab in database |
| 53 | ** iDb at the shared-cache level when executed. The isWriteLock argument |
| 54 | ** is zero for a read-lock, or non-zero for a write-lock. |
| 55 | ** |
| 56 | ** The zName parameter should point to the unqualified table name. This is |
| 57 | ** used to provide a more informative error message should the lock fail. |
| 58 | */ |
| 59 | void sqlite3TableLock( |
| 60 | Parse *pParse, |
| 61 | int iDb, |
| 62 | int iTab, |
| 63 | u8 isWriteLock, |
| 64 | const char *zName |
| 65 | ){ |
| 66 | int i; |
| 67 | int nBytes; |
| 68 | TableLock *p; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 69 | |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 70 | if( 0==sqlite3ThreadDataReadOnly()->useSharedData || iDb<0 ){ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
| 74 | for(i=0; i<pParse->nTableLock; i++){ |
| 75 | p = &pParse->aTableLock[i]; |
| 76 | if( p->iDb==iDb && p->iTab==iTab ){ |
| 77 | p->isWriteLock = (p->isWriteLock || isWriteLock); |
| 78 | return; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | nBytes = sizeof(TableLock) * (pParse->nTableLock+1); |
| 83 | sqliteReallocOrFree((void **)&pParse->aTableLock, nBytes); |
| 84 | if( pParse->aTableLock ){ |
| 85 | p = &pParse->aTableLock[pParse->nTableLock++]; |
| 86 | p->iDb = iDb; |
| 87 | p->iTab = iTab; |
| 88 | p->isWriteLock = isWriteLock; |
| 89 | p->zName = zName; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | ** Code an OP_TableLock instruction for each table locked by the |
| 95 | ** statement (configured by calls to sqlite3TableLock()). |
| 96 | */ |
| 97 | static void codeTableLocks(Parse *pParse){ |
| 98 | int i; |
| 99 | Vdbe *pVdbe; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 100 | assert( sqlite3ThreadDataReadOnly()->useSharedData || pParse->nTableLock==0 ); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 101 | |
| 102 | if( 0==(pVdbe = sqlite3GetVdbe(pParse)) ){ |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | for(i=0; i<pParse->nTableLock; i++){ |
| 107 | TableLock *p = &pParse->aTableLock[i]; |
| 108 | int p1 = p->iDb; |
| 109 | if( p->isWriteLock ){ |
| 110 | p1 = -1*(p1+1); |
| 111 | } |
| 112 | sqlite3VdbeOp3(pVdbe, OP_TableLock, p1, p->iTab, p->zName, P3_STATIC); |
| 113 | } |
| 114 | } |
| 115 | #else |
| 116 | #define codeTableLocks(x) |
| 117 | #endif |
| 118 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 119 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 120 | ** This routine is called after a single SQL statement has been |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 121 | ** parsed and a VDBE program to execute that statement has been |
| 122 | ** prepared. This routine puts the finishing touches on the |
| 123 | ** VDBE program and resets the pParse structure for the next |
| 124 | ** parse. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 125 | ** |
| 126 | ** Note that if an error occurred, it might be the case that |
| 127 | ** no VDBE code was generated. |
| 128 | */ |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 129 | void sqlite3FinishCoding(Parse *pParse){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 130 | sqlite3 *db; |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 131 | Vdbe *v; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 132 | |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 133 | if( sqlite3ThreadDataReadOnly()->mallocFailed ) return; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 134 | if( pParse->nested ) return; |
danielk1977 | 441daf6 | 2005-02-01 03:46:43 +0000 | [diff] [blame] | 135 | if( !pParse->pVdbe ){ |
danielk1977 | c30f9e7 | 2005-02-09 07:05:46 +0000 | [diff] [blame] | 136 | if( pParse->rc==SQLITE_OK && pParse->nErr ){ |
| 137 | pParse->rc = SQLITE_ERROR; |
| 138 | } |
danielk1977 | 441daf6 | 2005-02-01 03:46:43 +0000 | [diff] [blame] | 139 | return; |
| 140 | } |
danielk1977 | 48d0d86 | 2005-02-01 03:09:52 +0000 | [diff] [blame] | 141 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 142 | /* Begin by generating some termination code at the end of the |
| 143 | ** vdbe program |
| 144 | */ |
| 145 | db = pParse->db; |
| 146 | v = sqlite3GetVdbe(pParse); |
| 147 | if( v ){ |
| 148 | sqlite3VdbeAddOp(v, OP_Halt, 0, 0); |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 149 | |
| 150 | /* The cookie mask contains one bit for each database file open. |
| 151 | ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
| 152 | ** set for each database that is used. Generate code to start a |
| 153 | ** transaction on each used database and to verify the schema cookie |
| 154 | ** on each used database. |
| 155 | */ |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 156 | if( pParse->cookieGoto>0 ){ |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 157 | u32 mask; |
| 158 | int iDb; |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 159 | sqlite3VdbeJumpHere(v, pParse->cookieGoto-1); |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 160 | for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){ |
| 161 | if( (mask & pParse->cookieMask)==0 ) continue; |
| 162 | sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 163 | sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]); |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 164 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 165 | |
| 166 | /* Once all the cookies have been verified and transactions opened, |
| 167 | ** obtain the required table-locks. This is a no-op unless the |
| 168 | ** shared-cache feature is enabled. |
| 169 | */ |
| 170 | codeTableLocks(pParse); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 171 | sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto); |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 172 | } |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 173 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 174 | #ifndef SQLITE_OMIT_TRACE |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 175 | /* Add a No-op that contains the complete text of the compiled SQL |
| 176 | ** statement as its P3 argument. This does not change the functionality |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 177 | ** of the program. |
| 178 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 179 | ** This is used to implement sqlite3_trace(). |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 180 | */ |
| 181 | sqlite3VdbeOp3(v, OP_Noop, 0, 0, pParse->zSql, pParse->zTail-pParse->zSql); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 182 | #endif /* SQLITE_OMIT_TRACE */ |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 183 | } |
| 184 | |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 185 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 186 | /* Get the VDBE program ready for execution |
| 187 | */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 188 | if( v && pParse->nErr==0 ){ |
| 189 | FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 190 | sqlite3VdbeTrace(v, trace); |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 191 | sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem+3, |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 192 | pParse->nTab+3, pParse->explain); |
danielk1977 | 441daf6 | 2005-02-01 03:46:43 +0000 | [diff] [blame] | 193 | pParse->rc = SQLITE_DONE; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 194 | pParse->colNamesSet = 0; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 195 | }else if( pParse->rc==SQLITE_OK ){ |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 196 | pParse->rc = SQLITE_ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 197 | } |
drh | a226d05 | 2002-09-25 19:04:07 +0000 | [diff] [blame] | 198 | pParse->nTab = 0; |
| 199 | pParse->nMem = 0; |
| 200 | pParse->nSet = 0; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 201 | pParse->nVar = 0; |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 202 | pParse->cookieMask = 0; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 203 | pParse->cookieGoto = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 207 | ** Run the parser and code generator recursively in order to generate |
| 208 | ** code for the SQL statement given onto the end of the pParse context |
| 209 | ** currently under construction. When the parser is run recursively |
| 210 | ** this way, the final OP_Halt is not appended and other initialization |
| 211 | ** and finalization steps are omitted because those are handling by the |
| 212 | ** outermost parser. |
| 213 | ** |
| 214 | ** Not everything is nestable. This facility is designed to permit |
| 215 | ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 216 | ** care if you decide to try to use this routine for some other purposes. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 217 | */ |
| 218 | void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ |
| 219 | va_list ap; |
| 220 | char *zSql; |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 221 | # define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar)) |
| 222 | char saveBuf[SAVE_SZ]; |
| 223 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 224 | if( pParse->nErr ) return; |
| 225 | assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ |
| 226 | va_start(ap, zFormat); |
| 227 | zSql = sqlite3VMPrintf(zFormat, ap); |
| 228 | va_end(ap); |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 229 | if( zSql==0 ){ |
| 230 | return; /* A malloc must have failed */ |
| 231 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 232 | pParse->nested++; |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 233 | memcpy(saveBuf, &pParse->nVar, SAVE_SZ); |
| 234 | memset(&pParse->nVar, 0, SAVE_SZ); |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 235 | sqlite3RunParser(pParse, zSql, 0); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 236 | sqliteFree(zSql); |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 237 | memcpy(&pParse->nVar, saveBuf, SAVE_SZ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 238 | pParse->nested--; |
| 239 | } |
| 240 | |
| 241 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 242 | ** Locate the in-memory structure that describes a particular database |
| 243 | ** table given the name of that table and (optionally) the name of the |
| 244 | ** database containing the table. Return NULL if not found. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 245 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 246 | ** If zDatabase is 0, all databases are searched for the table and the |
| 247 | ** first matching table is returned. (No checking for duplicate table |
| 248 | ** names is done.) The search order is TEMP first, then MAIN, then any |
| 249 | ** auxiliary databases added using the ATTACH command. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 250 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 251 | ** See also sqlite3LocateTable(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 252 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 253 | Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 254 | Table *p = 0; |
| 255 | int i; |
drh | 645f63e | 2004-06-22 13:22:40 +0000 | [diff] [blame] | 256 | assert( zName!=0 ); |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 257 | for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 258 | int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 259 | if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 260 | p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, strlen(zName)+1); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 261 | if( p ) break; |
| 262 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 263 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 267 | ** Locate the in-memory structure that describes a particular database |
| 268 | ** table given the name of that table and (optionally) the name of the |
| 269 | ** database containing the table. Return NULL if not found. Also leave an |
| 270 | ** error message in pParse->zErrMsg. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 271 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 272 | ** The difference between this routine and sqlite3FindTable() is that this |
| 273 | ** routine leaves an error message in pParse->zErrMsg where |
| 274 | ** sqlite3FindTable() does not. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 275 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 276 | Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){ |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 277 | Table *p; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 278 | |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 279 | /* Read the database schema. If an error occurs, leave an error message |
| 280 | ** and code in pParse and return NULL. */ |
| 281 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 282 | return 0; |
| 283 | } |
| 284 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 285 | p = sqlite3FindTable(pParse->db, zName, zDbase); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 286 | if( p==0 ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 287 | if( zDbase ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 288 | sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 289 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 290 | sqlite3ErrorMsg(pParse, "no such table: %s", zName); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 291 | } |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 292 | pParse->checkSchema = 1; |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 293 | } |
| 294 | return p; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | ** Locate the in-memory structure that describes |
| 299 | ** a particular index given the name of that index |
| 300 | ** and the name of the database that contains the index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 301 | ** Return NULL if not found. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 302 | ** |
| 303 | ** If zDatabase is 0, all databases are searched for the |
| 304 | ** table and the first matching index is returned. (No checking |
| 305 | ** for duplicate index names is done.) The search order is |
| 306 | ** TEMP first, then MAIN, then any auxiliary databases added |
| 307 | ** using the ATTACH command. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 308 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 309 | Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 310 | Index *p = 0; |
| 311 | int i; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 312 | for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 313 | int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 314 | Schema *pSchema = db->aDb[j].pSchema; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 315 | if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 316 | assert( pSchema || (j==1 && !db->aDb[1].pBt) ); |
| 317 | if( pSchema ){ |
| 318 | p = sqlite3HashFind(&pSchema->idxHash, zName, strlen(zName)+1); |
| 319 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 320 | if( p ) break; |
| 321 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 322 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 326 | ** Reclaim the memory used by an index |
| 327 | */ |
| 328 | static void freeIndex(Index *p){ |
| 329 | sqliteFree(p->zColAff); |
| 330 | sqliteFree(p); |
| 331 | } |
| 332 | |
| 333 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 334 | ** Remove the given index from the index hash table, and free |
| 335 | ** its memory structures. |
| 336 | ** |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 337 | ** The index is removed from the database hash tables but |
| 338 | ** it is not unlinked from the Table that it indexes. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 339 | ** Unlinking from the Table must be done by the calling function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 340 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 341 | static void sqliteDeleteIndex(sqlite3 *db, Index *p){ |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 342 | Index *pOld; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 343 | const char *zName = p->zName; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 344 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 345 | pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName, strlen( zName)+1, 0); |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 346 | assert( pOld==0 || pOld==p ); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 347 | freeIndex(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 351 | ** For the index called zIdxName which is found in the database iDb, |
| 352 | ** unlike that index from its Table then remove the index from |
| 353 | ** the index hash table and free all memory structures associated |
| 354 | ** with the index. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 355 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 356 | void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 357 | Index *pIndex; |
| 358 | int len; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 359 | Hash *pHash = &db->aDb[iDb].pSchema->idxHash; |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 360 | |
| 361 | len = strlen(zIdxName); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 362 | pIndex = sqlite3HashInsert(pHash, zIdxName, len+1, 0); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 363 | if( pIndex ){ |
| 364 | if( pIndex->pTable->pIndex==pIndex ){ |
| 365 | pIndex->pTable->pIndex = pIndex->pNext; |
| 366 | }else{ |
| 367 | Index *p; |
| 368 | for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
| 369 | if( p && p->pNext==pIndex ){ |
| 370 | p->pNext = pIndex->pNext; |
| 371 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 372 | } |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 373 | freeIndex(pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 374 | } |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 375 | db->flags |= SQLITE_InternChanges; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 379 | ** Erase all schema information from the in-memory hash tables of |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 380 | ** a single database. This routine is called to reclaim memory |
| 381 | ** before the database closes. It is also called during a rollback |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 382 | ** if there were schema changes during the transaction or if a |
| 383 | ** schema-cookie mismatch occurs. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 384 | ** |
| 385 | ** If iDb<=0 then reset the internal schema tables for all database |
| 386 | ** files. If iDb>=2 then reset the internal schema for only the |
jplyon | cfa5684 | 2004-01-19 04:55:56 +0000 | [diff] [blame] | 387 | ** single file indicated. |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 388 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 389 | void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 390 | int i, j; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 391 | |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 392 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 393 | for(i=iDb; i<db->nDb; i++){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 394 | Db *pDb = &db->aDb[i]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 395 | if( pDb->pSchema ){ |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 396 | sqlite3SchemaFree(pDb->pSchema); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 397 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 398 | if( iDb>0 ) return; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 399 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 400 | assert( iDb==0 ); |
| 401 | db->flags &= ~SQLITE_InternChanges; |
| 402 | |
| 403 | /* If one or more of the auxiliary database files has been closed, |
danielk1977 | 311019b | 2006-01-10 07:14:23 +0000 | [diff] [blame] | 404 | ** then remove them from the auxiliary database list. We take the |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 405 | ** opportunity to do this here since we have just deleted all of the |
| 406 | ** schema hash tables and therefore do not have to make any changes |
| 407 | ** to any of those tables. |
| 408 | */ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 409 | for(i=0; i<db->nDb; i++){ |
| 410 | struct Db *pDb = &db->aDb[i]; |
| 411 | if( pDb->pBt==0 ){ |
| 412 | if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux); |
| 413 | pDb->pAux = 0; |
| 414 | } |
| 415 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 416 | for(i=j=2; i<db->nDb; i++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 417 | struct Db *pDb = &db->aDb[i]; |
| 418 | if( pDb->pBt==0 ){ |
| 419 | sqliteFree(pDb->zName); |
| 420 | pDb->zName = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 421 | continue; |
| 422 | } |
| 423 | if( j<i ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 424 | db->aDb[j] = db->aDb[i]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 425 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 426 | j++; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 427 | } |
| 428 | memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j])); |
| 429 | db->nDb = j; |
| 430 | if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ |
| 431 | memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); |
| 432 | sqliteFree(db->aDb); |
| 433 | db->aDb = db->aDbStatic; |
| 434 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* |
| 438 | ** This routine is called whenever a rollback occurs. If there were |
| 439 | ** schema changes during the transaction, then we have to reset the |
| 440 | ** internal hash tables and reload them from disk. |
| 441 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 442 | void sqlite3RollbackInternalChanges(sqlite3 *db){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 443 | if( db->flags & SQLITE_InternChanges ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 444 | sqlite3ResetInternalSchema(db, 0); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | ** This routine is called when a commit occurs. |
| 450 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 451 | void sqlite3CommitInternalChanges(sqlite3 *db){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 452 | db->flags &= ~SQLITE_InternChanges; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 456 | ** Clear the column names from a table or view. |
| 457 | */ |
| 458 | static void sqliteResetColumnNames(Table *pTable){ |
| 459 | int i; |
| 460 | Column *pCol; |
| 461 | assert( pTable!=0 ); |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 462 | if( (pCol = pTable->aCol)!=0 ){ |
| 463 | for(i=0; i<pTable->nCol; i++, pCol++){ |
| 464 | sqliteFree(pCol->zName); |
| 465 | sqlite3ExprDelete(pCol->pDflt); |
| 466 | sqliteFree(pCol->zType); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 467 | sqliteFree(pCol->zColl); |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 468 | } |
| 469 | sqliteFree(pTable->aCol); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 470 | } |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 471 | pTable->aCol = 0; |
| 472 | pTable->nCol = 0; |
| 473 | } |
| 474 | |
| 475 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 476 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 477 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 478 | ** |
| 479 | ** This routine just deletes the data structure. It does not unlink |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 480 | ** the table data structure from the hash table. Nor does it remove |
| 481 | ** foreign keys from the sqlite.aFKey hash table. But it does destroy |
| 482 | ** memory structures of the indices and foreign keys associated with |
| 483 | ** the table. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 484 | ** |
| 485 | ** Indices associated with the table are unlinked from the "db" |
| 486 | ** data structure if db!=NULL. If db==NULL, indices attached to |
| 487 | ** the table are deleted, but it is assumed they have already been |
| 488 | ** unlinked. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 489 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 490 | void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 491 | Index *pIndex, *pNext; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 492 | FKey *pFKey, *pNextFKey; |
| 493 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 494 | db = 0; |
| 495 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 496 | if( pTable==0 ) return; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 497 | |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 498 | /* Do not delete the table until the reference count reaches zero. */ |
| 499 | pTable->nRef--; |
| 500 | if( pTable->nRef>0 ){ |
| 501 | return; |
| 502 | } |
| 503 | assert( pTable->nRef==0 ); |
| 504 | |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 505 | /* Delete all indices associated with this table |
| 506 | */ |
| 507 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 508 | pNext = pIndex->pNext; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 509 | assert( pIndex->pSchema==pTable->pSchema ); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 510 | sqliteDeleteIndex(db, pIndex); |
| 511 | } |
| 512 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 513 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 514 | /* Delete all foreign keys associated with this table. The keys |
| 515 | ** should have already been unlinked from the db->aFKey hash table |
| 516 | */ |
| 517 | for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){ |
| 518 | pNextFKey = pFKey->pNextFrom; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 519 | assert( sqlite3HashFind(&pTable->pSchema->aFKey, |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 520 | pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey ); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 521 | sqliteFree(pFKey); |
| 522 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 523 | #endif |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 524 | |
| 525 | /* Delete the Table structure itself. |
| 526 | */ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 527 | sqliteResetColumnNames(pTable); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 528 | sqliteFree(pTable->zName); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 529 | sqliteFree(pTable->zColAff); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 530 | sqlite3SelectDelete(pTable->pSelect); |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 531 | #ifndef SQLITE_OMIT_CHECK |
| 532 | sqlite3ExprDelete(pTable->pCheck); |
| 533 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 534 | sqliteFree(pTable); |
| 535 | } |
| 536 | |
| 537 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 538 | ** Unlink the given table from the hash tables and the delete the |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 539 | ** table structure with all its indices and foreign keys. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 540 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 541 | void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 542 | Table *p; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 543 | FKey *pF1, *pF2; |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 544 | Db *pDb; |
| 545 | |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 546 | assert( db!=0 ); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 547 | assert( iDb>=0 && iDb<db->nDb ); |
| 548 | assert( zTabName && zTabName[0] ); |
| 549 | pDb = &db->aDb[iDb]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 550 | p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, strlen(zTabName)+1,0); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 551 | if( p ){ |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 552 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 553 | for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){ |
| 554 | int nTo = strlen(pF1->zTo) + 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 555 | pF2 = sqlite3HashFind(&pDb->pSchema->aFKey, pF1->zTo, nTo); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 556 | if( pF2==pF1 ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 557 | sqlite3HashInsert(&pDb->pSchema->aFKey, pF1->zTo, nTo, pF1->pNextTo); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 558 | }else{ |
| 559 | while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; } |
| 560 | if( pF2 ){ |
| 561 | pF2->pNextTo = pF1->pNextTo; |
| 562 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 565 | #endif |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 566 | sqlite3DeleteTable(db, p); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 567 | } |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 568 | db->flags |= SQLITE_InternChanges; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 572 | ** Given a token, return a string that consists of the text of that |
| 573 | ** token with any quotations removed. Space to hold the returned string |
| 574 | ** is obtained from sqliteMalloc() and must be freed by the calling |
| 575 | ** function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 576 | ** |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 577 | ** Tokens are often just pointers into the original SQL text and so |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 578 | ** are not \000 terminated and are not persistent. The returned string |
| 579 | ** is \000 terminated and is persistent. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 580 | */ |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 581 | char *sqlite3NameFromToken(Token *pName){ |
| 582 | char *zName; |
| 583 | if( pName ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 584 | zName = sqliteStrNDup((char*)pName->z, pName->n); |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 585 | sqlite3Dequote(zName); |
| 586 | }else{ |
| 587 | zName = 0; |
| 588 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 589 | return zName; |
| 590 | } |
| 591 | |
| 592 | /* |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 593 | ** Open the sqlite_master table stored in database number iDb for |
| 594 | ** writing. The table is opened using cursor 0. |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 595 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 596 | void sqlite3OpenMasterTable(Parse *p, int iDb){ |
| 597 | Vdbe *v = sqlite3GetVdbe(p); |
| 598 | sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb)); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 599 | sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
danielk1977 | 8e15081 | 2004-05-10 01:17:37 +0000 | [diff] [blame] | 600 | sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 601 | sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 605 | ** The token *pName contains the name of a database (either "main" or |
| 606 | ** "temp" or the name of an attached db). This routine returns the |
| 607 | ** index of the named database in db->aDb[], or -1 if the named db |
| 608 | ** does not exist. |
| 609 | */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 610 | int sqlite3FindDb(sqlite3 *db, Token *pName){ |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 611 | int i = -1; /* Database number */ |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 612 | int n; /* Number of characters in the name */ |
| 613 | Db *pDb; /* A database whose name space is being searched */ |
| 614 | char *zName; /* Name we are searching for */ |
| 615 | |
| 616 | zName = sqlite3NameFromToken(pName); |
| 617 | if( zName ){ |
| 618 | n = strlen(zName); |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 619 | for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 620 | if( (!OMIT_TEMPDB || i!=1 ) && n==strlen(pDb->zName) && |
| 621 | 0==sqlite3StrICmp(pDb->zName, zName) ){ |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 622 | break; |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 623 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 624 | } |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 625 | sqliteFree(zName); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 626 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 627 | return i; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 628 | } |
| 629 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 630 | /* The table or view or trigger name is passed to this routine via tokens |
| 631 | ** pName1 and pName2. If the table name was fully qualified, for example: |
| 632 | ** |
| 633 | ** CREATE TABLE xxx.yyy (...); |
| 634 | ** |
| 635 | ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
| 636 | ** the table name is not fully qualified, i.e.: |
| 637 | ** |
| 638 | ** CREATE TABLE yyy(...); |
| 639 | ** |
| 640 | ** Then pName1 is set to "yyy" and pName2 is "". |
| 641 | ** |
| 642 | ** This routine sets the *ppUnqual pointer to point at the token (pName1 or |
| 643 | ** pName2) that stores the unqualified table name. The index of the |
| 644 | ** database "xxx" is returned. |
| 645 | */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 646 | int sqlite3TwoPartName( |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 647 | Parse *pParse, /* Parsing and code generating context */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 648 | Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 649 | Token *pName2, /* The "yyy" in the name "xxx.yyy" */ |
| 650 | Token **pUnqual /* Write the unqualified object name here */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 651 | ){ |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 652 | int iDb; /* Database holding the object */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 653 | sqlite3 *db = pParse->db; |
| 654 | |
| 655 | if( pName2 && pName2->n>0 ){ |
| 656 | assert( !db->init.busy ); |
| 657 | *pUnqual = pName2; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 658 | iDb = sqlite3FindDb(db, pName1); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 659 | if( iDb<0 ){ |
| 660 | sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
| 661 | pParse->nErr++; |
| 662 | return -1; |
| 663 | } |
| 664 | }else{ |
| 665 | assert( db->init.iDb==0 || db->init.busy ); |
| 666 | iDb = db->init.iDb; |
| 667 | *pUnqual = pName1; |
| 668 | } |
| 669 | return iDb; |
| 670 | } |
| 671 | |
| 672 | /* |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 673 | ** This routine is used to check if the UTF-8 string zName is a legal |
| 674 | ** unqualified name for a new schema object (table, index, view or |
| 675 | ** trigger). All names are legal except those that begin with the string |
| 676 | ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace |
| 677 | ** is reserved for internal use. |
| 678 | */ |
| 679 | int sqlite3CheckObjectName(Parse *pParse, const char *zName){ |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 680 | if( !pParse->db->init.busy && pParse->nested==0 |
danielk1977 | 3a3f38e | 2005-05-22 06:49:56 +0000 | [diff] [blame] | 681 | && (pParse->db->flags & SQLITE_WriteSchema)==0 |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 682 | && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 683 | sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName); |
| 684 | return SQLITE_ERROR; |
| 685 | } |
| 686 | return SQLITE_OK; |
| 687 | } |
| 688 | |
| 689 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 690 | ** Begin constructing a new table representation in memory. This is |
| 691 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 692 | ** to a CREATE TABLE statement. In particular, this routine is called |
| 693 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 694 | ** pStart token is the CREATE and pName is the table name. The isTemp |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 695 | ** flag is true if the table should be stored in the auxiliary database |
| 696 | ** file instead of in the main database file. This is normally the case |
| 697 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 698 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 699 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 700 | ** The new table record is initialized and put in pParse->pNewTable. |
| 701 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 702 | ** routines will be called to add more information to this record. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 703 | ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 704 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 705 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 706 | void sqlite3StartTable( |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 707 | Parse *pParse, /* Parser context */ |
| 708 | Token *pStart, /* The "CREATE" token */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 709 | Token *pName1, /* First part of the name of the table or view */ |
| 710 | Token *pName2, /* Second part of the name of the table or view */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 711 | int isTemp, /* True if this is a TEMP table */ |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 712 | int isView, /* True if this is a VIEW */ |
| 713 | int noErr /* Do nothing if table already exists */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 714 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 715 | Table *pTable; |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 716 | char *zName = 0; /* The name of the new table */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 717 | sqlite3 *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 718 | Vdbe *v; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 719 | int iDb; /* Database number to create the table in */ |
| 720 | Token *pName; /* Unqualified name of the table to create */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 721 | |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 722 | /* The table or view name to create is passed to this routine via tokens |
| 723 | ** pName1 and pName2. If the table name was fully qualified, for example: |
| 724 | ** |
| 725 | ** CREATE TABLE xxx.yyy (...); |
| 726 | ** |
| 727 | ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
| 728 | ** the table name is not fully qualified, i.e.: |
| 729 | ** |
| 730 | ** CREATE TABLE yyy(...); |
| 731 | ** |
| 732 | ** Then pName1 is set to "yyy" and pName2 is "". |
| 733 | ** |
| 734 | ** The call below sets the pName pointer to point at the token (pName1 or |
| 735 | ** pName2) that stores the unqualified table name. The variable iDb is |
| 736 | ** set to the index of the database that the table or view is to be |
| 737 | ** created in. |
| 738 | */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 739 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 740 | if( iDb<0 ) return; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 741 | if( !OMIT_TEMPDB && isTemp && iDb>1 ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 742 | /* If creating a temp table, the name may not be qualified */ |
| 743 | sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 744 | return; |
| 745 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 746 | if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 747 | |
| 748 | pParse->sNameToken = *pName; |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 749 | zName = sqlite3NameFromToken(pName); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 750 | if( zName==0 ) return; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 751 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 752 | goto begin_table_error; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 753 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 754 | if( db->init.iDb==1 ) isTemp = 1; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 755 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 756 | assert( (isTemp & 1)==isTemp ); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 757 | { |
| 758 | int code; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 759 | char *zDb = db->aDb[iDb].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 760 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 761 | goto begin_table_error; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 762 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 763 | if( isView ){ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 764 | if( !OMIT_TEMPDB && isTemp ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 765 | code = SQLITE_CREATE_TEMP_VIEW; |
| 766 | }else{ |
| 767 | code = SQLITE_CREATE_VIEW; |
| 768 | } |
| 769 | }else{ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 770 | if( !OMIT_TEMPDB && isTemp ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 771 | code = SQLITE_CREATE_TEMP_TABLE; |
| 772 | }else{ |
| 773 | code = SQLITE_CREATE_TABLE; |
| 774 | } |
| 775 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 776 | if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 777 | goto begin_table_error; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | #endif |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 781 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 782 | /* Make sure the new table name does not collide with an existing |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 783 | ** index or table name in the same database. Issue an error message if |
| 784 | ** it does. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 785 | */ |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 786 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 787 | goto begin_table_error; |
| 788 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 789 | pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName); |
| 790 | if( pTable ){ |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 791 | if( !noErr ){ |
| 792 | sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
| 793 | } |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 794 | goto begin_table_error; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 795 | } |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 796 | if( sqlite3FindIndex(db, zName, 0)!=0 && (iDb==0 || !db->init.busy) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 797 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 798 | goto begin_table_error; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 799 | } |
| 800 | pTable = sqliteMalloc( sizeof(Table) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 801 | if( pTable==0 ){ |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 802 | pParse->rc = SQLITE_NOMEM; |
| 803 | pParse->nErr++; |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 804 | goto begin_table_error; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 805 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 806 | pTable->zName = zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 807 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 808 | pTable->aCol = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 809 | pTable->iPKey = -1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 810 | pTable->pIndex = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 811 | pTable->pSchema = db->aDb[iDb].pSchema; |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 812 | pTable->nRef = 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 813 | if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 814 | pParse->pNewTable = pTable; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 815 | |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 816 | /* If this is the magic sqlite_sequence table used by autoincrement, |
| 817 | ** then record a pointer to this table in the main database structure |
| 818 | ** so that INSERT can find the table easily. |
| 819 | */ |
| 820 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 78776ec | 2005-06-14 02:12:46 +0000 | [diff] [blame] | 821 | if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 822 | pTable->pSchema->pSeqTab = pTable; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 823 | } |
| 824 | #endif |
| 825 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 826 | /* Begin generating the code that will insert the table record into |
| 827 | ** the SQLITE_MASTER table. Note in particular that we must go ahead |
| 828 | ** and allocate the record number for the table entry now. Before any |
| 829 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
| 830 | ** indices to be created and the table record must come before the |
| 831 | ** indices. Hence, the record number for the table must be allocated |
| 832 | ** now. |
| 833 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 834 | if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 835 | int lbl; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 836 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 837 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 838 | /* If the file format and encoding in the database have not been set, |
| 839 | ** set them now. |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 840 | */ |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 841 | sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1); /* file_format */ |
| 842 | lbl = sqlite3VdbeMakeLabel(v); |
| 843 | sqlite3VdbeAddOp(v, OP_If, 0, lbl); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 844 | sqlite3VdbeAddOp(v, OP_Integer, SQLITE_DEFAULT_FILE_FORMAT, 0); |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 845 | sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 846 | sqlite3VdbeAddOp(v, OP_Integer, ENC(db), 0); |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 847 | sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 848 | sqlite3VdbeResolveLabel(v, lbl); |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 849 | |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 850 | /* This just creates a place-holder record in the sqlite_master table. |
| 851 | ** The record created does not contain anything yet. It will be replaced |
| 852 | ** by the real entry in code generated at sqlite3EndTable(). |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 853 | ** |
| 854 | ** The rowid for the new entry is left on the top of the stack. |
| 855 | ** The rowid value is needed by the code that sqlite3EndTable will |
| 856 | ** generate. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 857 | */ |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 858 | #ifndef SQLITE_OMIT_VIEW |
| 859 | if( isView ){ |
| 860 | sqlite3VdbeAddOp(v, OP_Integer, 0, 0); |
| 861 | }else |
| 862 | #endif |
| 863 | { |
| 864 | sqlite3VdbeAddOp(v, OP_CreateTable, iDb, 0); |
| 865 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 866 | sqlite3OpenMasterTable(pParse, iDb); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 867 | sqlite3VdbeAddOp(v, OP_NewRowid, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 868 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 869 | sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
| 870 | sqlite3VdbeAddOp(v, OP_Insert, 0, 0); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 871 | sqlite3VdbeAddOp(v, OP_Close, 0, 0); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 872 | sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 873 | } |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 874 | |
| 875 | /* Normal (non-error) return. */ |
| 876 | return; |
| 877 | |
| 878 | /* If an error occurs, we jump here */ |
| 879 | begin_table_error: |
| 880 | sqliteFree(zName); |
| 881 | return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /* |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame] | 885 | ** This macro is used to compare two strings in a case-insensitive manner. |
| 886 | ** It is slightly faster than calling sqlite3StrICmp() directly, but |
| 887 | ** produces larger code. |
| 888 | ** |
| 889 | ** WARNING: This macro is not compatible with the strcmp() family. It |
| 890 | ** returns true if the two strings are equal, otherwise false. |
| 891 | */ |
| 892 | #define STRICMP(x, y) (\ |
| 893 | sqlite3UpperToLower[*(unsigned char *)(x)]== \ |
| 894 | sqlite3UpperToLower[*(unsigned char *)(y)] \ |
| 895 | && sqlite3StrICmp((x)+1,(y)+1)==0 ) |
| 896 | |
| 897 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 898 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 899 | ** |
| 900 | ** The parser calls this routine once for each column declaration |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 901 | ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 902 | ** first to get things going. Then this routine is called for each |
| 903 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 904 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 905 | void sqlite3AddColumn(Parse *pParse, Token *pName){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 906 | Table *p; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 907 | int i; |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 908 | char *z; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 909 | Column *pCol; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 910 | if( (p = pParse->pNewTable)==0 ) return; |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 911 | z = sqlite3NameFromToken(pName); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 912 | if( z==0 ) return; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 913 | for(i=0; i<p->nCol; i++){ |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame] | 914 | if( STRICMP(z, p->aCol[i].zName) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 915 | sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 916 | sqliteFree(z); |
| 917 | return; |
| 918 | } |
| 919 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 920 | if( (p->nCol & 0x7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 921 | Column *aNew; |
| 922 | aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 923 | if( aNew==0 ){ |
| 924 | sqliteFree(z); |
| 925 | return; |
| 926 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 927 | p->aCol = aNew; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 928 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 929 | pCol = &p->aCol[p->nCol]; |
| 930 | memset(pCol, 0, sizeof(p->aCol[0])); |
| 931 | pCol->zName = z; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 932 | |
| 933 | /* If there is no type specified, columns have the default affinity |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 934 | ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will |
| 935 | ** be called next to set pCol->affinity correctly. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 936 | */ |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 937 | pCol->affinity = SQLITE_AFF_NONE; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 938 | p->nCol++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 942 | ** This routine is called by the parser while in the middle of |
| 943 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 944 | ** been seen on a column. This routine sets the notNull flag on |
| 945 | ** the column currently under construction. |
| 946 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 947 | void sqlite3AddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 948 | Table *p; |
| 949 | int i; |
| 950 | if( (p = pParse->pNewTable)==0 ) return; |
| 951 | i = p->nCol-1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 952 | if( i>=0 ) p->aCol[i].notNull = onError; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /* |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 956 | ** Scan the column type name zType (length nType) and return the |
| 957 | ** associated affinity type. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 958 | ** |
| 959 | ** This routine does a case-independent search of zType for the |
| 960 | ** substrings in the following table. If one of the substrings is |
| 961 | ** found, the corresponding affinity is returned. If zType contains |
| 962 | ** more than one of the substrings, entries toward the top of |
| 963 | ** the table take priority. For example, if zType is 'BLOBINT', |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 964 | ** SQLITE_AFF_INTEGER is returned. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 965 | ** |
| 966 | ** Substring | Affinity |
| 967 | ** -------------------------------- |
| 968 | ** 'INT' | SQLITE_AFF_INTEGER |
| 969 | ** 'CHAR' | SQLITE_AFF_TEXT |
| 970 | ** 'CLOB' | SQLITE_AFF_TEXT |
| 971 | ** 'TEXT' | SQLITE_AFF_TEXT |
| 972 | ** 'BLOB' | SQLITE_AFF_NONE |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 973 | ** 'REAL' | SQLITE_AFF_REAL |
| 974 | ** 'FLOA' | SQLITE_AFF_REAL |
| 975 | ** 'DOUB' | SQLITE_AFF_REAL |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 976 | ** |
| 977 | ** If none of the substrings in the above table are found, |
| 978 | ** SQLITE_AFF_NUMERIC is returned. |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 979 | */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 980 | char sqlite3AffinityType(const Token *pType){ |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 981 | u32 h = 0; |
| 982 | char aff = SQLITE_AFF_NUMERIC; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 983 | const unsigned char *zIn = pType->z; |
| 984 | const unsigned char *zEnd = &pType->z[pType->n]; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 985 | |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 986 | while( zIn!=zEnd ){ |
| 987 | h = (h<<8) + sqlite3UpperToLower[*zIn]; |
| 988 | zIn++; |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 989 | if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
| 990 | aff = SQLITE_AFF_TEXT; |
| 991 | }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
| 992 | aff = SQLITE_AFF_TEXT; |
| 993 | }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
| 994 | aff = SQLITE_AFF_TEXT; |
| 995 | }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 996 | && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 997 | aff = SQLITE_AFF_NONE; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 998 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 999 | }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
| 1000 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1001 | aff = SQLITE_AFF_REAL; |
| 1002 | }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ |
| 1003 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1004 | aff = SQLITE_AFF_REAL; |
| 1005 | }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ |
| 1006 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1007 | aff = SQLITE_AFF_REAL; |
| 1008 | #endif |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1009 | }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1010 | aff = SQLITE_AFF_INTEGER; |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1011 | break; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1012 | } |
| 1013 | } |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1014 | |
| 1015 | return aff; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1019 | ** This routine is called by the parser while in the middle of |
| 1020 | ** parsing a CREATE TABLE statement. The pFirst token is the first |
| 1021 | ** token in the sequence of tokens that describe the type of the |
| 1022 | ** column currently under construction. pLast is the last token |
| 1023 | ** in the sequence. Use this information to construct a string |
| 1024 | ** that contains the typename of the column and store that string |
| 1025 | ** in zType. |
| 1026 | */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1027 | void sqlite3AddColumnType(Parse *pParse, Token *pType){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1028 | Table *p; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1029 | int i; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1030 | Column *pCol; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1031 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1032 | if( (p = pParse->pNewTable)==0 ) return; |
| 1033 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1034 | if( i<0 ) return; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1035 | pCol = &p->aCol[i]; |
drh | d891967 | 2005-09-10 15:35:06 +0000 | [diff] [blame] | 1036 | sqliteFree(pCol->zType); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1037 | pCol->zType = sqlite3NameFromToken(pType); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1038 | pCol->affinity = sqlite3AffinityType(pType); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | /* |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1042 | ** The expression is the default value for the most recently added column |
| 1043 | ** of the table currently under construction. |
| 1044 | ** |
| 1045 | ** Default value expressions must be constant. Raise an exception if this |
| 1046 | ** is not the case. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1047 | ** |
| 1048 | ** This routine is called by the parser while in the middle of |
| 1049 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1050 | */ |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1051 | void sqlite3AddDefaultValue(Parse *pParse, Expr *pExpr){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1052 | Table *p; |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1053 | Column *pCol; |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1054 | if( (p = pParse->pNewTable)!=0 ){ |
| 1055 | pCol = &(p->aCol[p->nCol-1]); |
| 1056 | if( !sqlite3ExprIsConstantOrFunction(pExpr) ){ |
| 1057 | sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
| 1058 | pCol->zName); |
| 1059 | }else{ |
| 1060 | sqlite3ExprDelete(pCol->pDflt); |
| 1061 | pCol->pDflt = sqlite3ExprDup(pExpr); |
| 1062 | } |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1063 | } |
| 1064 | sqlite3ExprDelete(pExpr); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /* |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1068 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
| 1069 | ** of columns that form the primary key. If pList is NULL, then the |
| 1070 | ** most recently added column of the table is the primary key. |
| 1071 | ** |
| 1072 | ** A table can have at most one primary key. If the table already has |
| 1073 | ** a primary key (and this is the second primary key) then create an |
| 1074 | ** error. |
| 1075 | ** |
| 1076 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1077 | ** then we will try to use that column as the rowid. Set the Table.iPKey |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1078 | ** field of the table under construction to be the index of the |
| 1079 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 1080 | ** no INTEGER PRIMARY KEY. |
| 1081 | ** |
| 1082 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 1083 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 1084 | */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1085 | void sqlite3AddPrimaryKey( |
| 1086 | Parse *pParse, /* Parsing context */ |
| 1087 | ExprList *pList, /* List of field names to be indexed */ |
| 1088 | int onError, /* What to do with a uniqueness conflict */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 1089 | int autoInc, /* True if the AUTOINCREMENT keyword is present */ |
| 1090 | int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1091 | ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1092 | Table *pTab = pParse->pNewTable; |
| 1093 | char *zType = 0; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1094 | int iCol = -1, i; |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1095 | if( pTab==0 ) goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1096 | if( pTab->hasPrimKey ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1097 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1098 | "table \"%s\" has more than one primary key", pTab->zName); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1099 | goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1100 | } |
| 1101 | pTab->hasPrimKey = 1; |
| 1102 | if( pList==0 ){ |
| 1103 | iCol = pTab->nCol - 1; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1104 | pTab->aCol[iCol].isPrimKey = 1; |
| 1105 | }else{ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1106 | for(i=0; i<pList->nExpr; i++){ |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1107 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1108 | if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){ |
| 1109 | break; |
| 1110 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1111 | } |
drh | 6e4fc2c | 2005-09-15 21:24:51 +0000 | [diff] [blame] | 1112 | if( iCol<pTab->nCol ){ |
drh | 688c9f0 | 2005-09-16 02:48:01 +0000 | [diff] [blame] | 1113 | pTab->aCol[iCol].isPrimKey = 1; |
drh | 6e4fc2c | 2005-09-15 21:24:51 +0000 | [diff] [blame] | 1114 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1115 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1116 | if( pList->nExpr>1 ) iCol = -1; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1117 | } |
| 1118 | if( iCol>=0 && iCol<pTab->nCol ){ |
| 1119 | zType = pTab->aCol[iCol].zType; |
| 1120 | } |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 1121 | if( zType && sqlite3StrICmp(zType, "INTEGER")==0 |
| 1122 | && sortOrder==SQLITE_SO_ASC ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1123 | pTab->iPKey = iCol; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1124 | pTab->keyConf = onError; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1125 | pTab->autoInc = autoInc; |
| 1126 | }else if( autoInc ){ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1127 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1128 | sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
| 1129 | "INTEGER PRIMARY KEY"); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1130 | #endif |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1131 | }else{ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 1132 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1133 | pList = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1134 | } |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1135 | |
| 1136 | primary_key_exit: |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1137 | sqlite3ExprListDelete(pList); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1138 | return; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /* |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1142 | ** Add a new CHECK constraint to the table currently under construction. |
| 1143 | */ |
| 1144 | void sqlite3AddCheckConstraint( |
| 1145 | Parse *pParse, /* Parsing context */ |
| 1146 | Expr *pCheckExpr /* The check expression */ |
| 1147 | ){ |
| 1148 | #ifndef SQLITE_OMIT_CHECK |
| 1149 | Table *pTab = pParse->pNewTable; |
| 1150 | if( pTab ){ |
| 1151 | /* The CHECK expression must be duplicated so that tokens refer |
| 1152 | ** to malloced space and not the (ephemeral) text of the CREATE TABLE |
| 1153 | ** statement */ |
| 1154 | pTab->pCheck = sqlite3ExprAnd(pTab->pCheck, sqlite3ExprDup(pCheckExpr)); |
| 1155 | } |
| 1156 | #endif |
| 1157 | sqlite3ExprDelete(pCheckExpr); |
| 1158 | } |
| 1159 | |
| 1160 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1161 | ** Set the collation function of the most recently parsed table column |
| 1162 | ** to the CollSeq given. |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 1163 | */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1164 | void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){ |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 1165 | Table *p; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1166 | int i; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1167 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1168 | if( (p = pParse->pNewTable)==0 ) return; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1169 | i = p->nCol-1; |
| 1170 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1171 | if( sqlite3LocateCollSeq(pParse, zType, nType) ){ |
| 1172 | Index *pIdx; |
| 1173 | p->aCol[i].zColl = sqlite3StrNDup(zType, nType); |
| 1174 | |
| 1175 | /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
| 1176 | ** then an index may have been created on this column before the |
| 1177 | ** collation type was added. Correct this if it is the case. |
| 1178 | */ |
| 1179 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1180 | assert( pIdx->nColumn==1 ); |
| 1181 | if( pIdx->aiColumn[0]==i ){ |
| 1182 | pIdx->azColl[0] = p->aCol[i].zColl; |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1188 | /* |
| 1189 | ** This function returns the collation sequence for database native text |
| 1190 | ** encoding identified by the string zName, length nName. |
| 1191 | ** |
| 1192 | ** If the requested collation sequence is not available, or not available |
| 1193 | ** in the database native encoding, the collation factory is invoked to |
| 1194 | ** request it. If the collation factory does not supply such a sequence, |
| 1195 | ** and the sequence is available in another text encoding, then that is |
| 1196 | ** returned instead. |
| 1197 | ** |
| 1198 | ** If no versions of the requested collations sequence are available, or |
| 1199 | ** another error occurs, NULL is returned and an error message written into |
| 1200 | ** pParse. |
| 1201 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1202 | CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){ |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 1203 | sqlite3 *db = pParse->db; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 1204 | u8 enc = ENC(db); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 1205 | u8 initbusy = db->init.busy; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1206 | CollSeq *pColl; |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 1207 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1208 | pColl = sqlite3FindCollSeq(db, enc, zName, nName, initbusy); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1209 | if( !initbusy && (!pColl || !pColl->xCmp) ){ |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 1210 | pColl = sqlite3GetCollSeq(db, pColl, zName, nName); |
| 1211 | if( !pColl ){ |
| 1212 | if( nName<0 ){ |
| 1213 | nName = strlen(zName); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1214 | } |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 1215 | sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", nName, zName); |
| 1216 | pColl = 0; |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1217 | } |
| 1218 | } |
| 1219 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1220 | return pColl; |
| 1221 | } |
| 1222 | |
| 1223 | |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 1224 | /* |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 1225 | ** Generate code that will increment the schema cookie. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1226 | ** |
| 1227 | ** The schema cookie is used to determine when the schema for the |
| 1228 | ** database changes. After each schema change, the cookie value |
| 1229 | ** changes. When a process first reads the schema it records the |
| 1230 | ** cookie. Thereafter, whenever it goes to access the database, |
| 1231 | ** it checks the cookie to make sure the schema has not changed |
| 1232 | ** since it was last read. |
| 1233 | ** |
| 1234 | ** This plan is not completely bullet-proof. It is possible for |
| 1235 | ** the schema to change multiple times and for the cookie to be |
| 1236 | ** set back to prior value. But schema changes are infrequent |
| 1237 | ** and the probability of hitting the same cookie value is only |
| 1238 | ** 1 chance in 2^32. So we're safe enough. |
| 1239 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1240 | void sqlite3ChangeCookie(sqlite3 *db, Vdbe *v, int iDb){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1241 | sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, 0); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1242 | sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | /* |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1246 | ** Measure the number of characters needed to output the given |
| 1247 | ** identifier. The number returned includes any quotes used |
| 1248 | ** but does not include the null terminator. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1249 | ** |
| 1250 | ** The estimate is conservative. It might be larger that what is |
| 1251 | ** really needed. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1252 | */ |
| 1253 | static int identLength(const char *z){ |
| 1254 | int n; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1255 | for(n=0; *z; n++, z++){ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1256 | if( *z=='"' ){ n++; } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1257 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1258 | return n + 2; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | ** Write an identifier onto the end of the given string. Add |
| 1263 | ** quote characters as needed. |
| 1264 | */ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1265 | static void identPut(char *z, int *pIdx, char *zSignedIdent){ |
| 1266 | unsigned char *zIdent = (unsigned char*)zSignedIdent; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1267 | int i, j, needQuote; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1268 | i = *pIdx; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1269 | for(j=0; zIdent[j]; j++){ |
| 1270 | if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
| 1271 | } |
| 1272 | needQuote = zIdent[j]!=0 || isdigit(zIdent[0]) |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1273 | || sqlite3KeywordCode(zIdent, j)!=TK_ID; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1274 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1275 | for(j=0; zIdent[j]; j++){ |
| 1276 | z[i++] = zIdent[j]; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1277 | if( zIdent[j]=='"' ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1278 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1279 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1280 | z[i] = 0; |
| 1281 | *pIdx = i; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | ** Generate a CREATE TABLE statement appropriate for the given |
| 1286 | ** table. Memory to hold the text of the statement is obtained |
| 1287 | ** from sqliteMalloc() and must be freed by the calling function. |
| 1288 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1289 | static char *createTableStmt(Table *p, int isTemp){ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1290 | int i, k, n; |
| 1291 | char *zStmt; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1292 | char *zSep, *zSep2, *zEnd, *z; |
| 1293 | Column *pCol; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1294 | n = 0; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1295 | for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){ |
| 1296 | n += identLength(pCol->zName); |
| 1297 | z = pCol->zType; |
| 1298 | if( z ){ |
| 1299 | n += (strlen(z) + 1); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1300 | } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1301 | } |
| 1302 | n += identLength(p->zName); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1303 | if( n<50 ){ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1304 | zSep = ""; |
| 1305 | zSep2 = ","; |
| 1306 | zEnd = ")"; |
| 1307 | }else{ |
| 1308 | zSep = "\n "; |
| 1309 | zSep2 = ",\n "; |
| 1310 | zEnd = "\n)"; |
| 1311 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1312 | n += 35 + 6*p->nCol; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1313 | zStmt = sqliteMallocRaw( n ); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1314 | if( zStmt==0 ) return 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1315 | strcpy(zStmt, !OMIT_TEMPDB&&isTemp ? "CREATE TEMP TABLE ":"CREATE TABLE "); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1316 | k = strlen(zStmt); |
| 1317 | identPut(zStmt, &k, p->zName); |
| 1318 | zStmt[k++] = '('; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1319 | for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1320 | strcpy(&zStmt[k], zSep); |
| 1321 | k += strlen(&zStmt[k]); |
| 1322 | zSep = zSep2; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1323 | identPut(zStmt, &k, pCol->zName); |
| 1324 | if( (z = pCol->zType)!=0 ){ |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1325 | zStmt[k++] = ' '; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1326 | strcpy(&zStmt[k], z); |
| 1327 | k += strlen(z); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1328 | } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1329 | } |
| 1330 | strcpy(&zStmt[k], zEnd); |
| 1331 | return zStmt; |
| 1332 | } |
| 1333 | |
| 1334 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1335 | ** This routine is called to report the final ")" that terminates |
| 1336 | ** a CREATE TABLE statement. |
| 1337 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1338 | ** The table structure that other action routines have been building |
| 1339 | ** is added to the internal hash tables, assuming no errors have |
| 1340 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1341 | ** |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1342 | ** An entry for the table is made in the master table on disk, unless |
| 1343 | ** 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] | 1344 | ** it means we are reading the sqlite_master table because we just |
| 1345 | ** connected to the database or because the sqlite_master table has |
drh | ddba9e5 | 2005-03-19 01:41:21 +0000 | [diff] [blame] | 1346 | ** recently changed, so the entry for this table already exists in |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1347 | ** the sqlite_master table. We do not want to create it again. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1348 | ** |
| 1349 | ** If the pSelect argument is not NULL, it means that this routine |
| 1350 | ** was called to create a table generated from a |
| 1351 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
| 1352 | ** the new table will match the result set of the SELECT. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1353 | */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 1354 | void sqlite3EndTable( |
| 1355 | Parse *pParse, /* Parse context */ |
| 1356 | Token *pCons, /* The ',' token after the last column defn. */ |
| 1357 | Token *pEnd, /* The final ')' token in the CREATE TABLE */ |
| 1358 | Select *pSelect /* Select from a "CREATE ... AS SELECT" */ |
| 1359 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1360 | Table *p; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1361 | sqlite3 *db = pParse->db; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1362 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1363 | |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 1364 | if( (pEnd==0 && pSelect==0) || |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1365 | pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) { |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1366 | return; |
| 1367 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 1368 | p = pParse->pNewTable; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1369 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1370 | |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1371 | assert( !db->init.busy || !pSelect ); |
| 1372 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1373 | iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); |
| 1374 | |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1375 | #ifndef SQLITE_OMIT_CHECK |
| 1376 | /* Resolve names in all CHECK constraint expressions. |
| 1377 | */ |
| 1378 | if( p->pCheck ){ |
| 1379 | SrcList sSrc; /* Fake SrcList for pParse->pNewTable */ |
| 1380 | NameContext sNC; /* Name context for pParse->pNewTable */ |
| 1381 | |
| 1382 | memset(&sNC, 0, sizeof(sNC)); |
| 1383 | memset(&sSrc, 0, sizeof(sSrc)); |
| 1384 | sSrc.nSrc = 1; |
| 1385 | sSrc.a[0].zName = p->zName; |
| 1386 | sSrc.a[0].pTab = p; |
| 1387 | sSrc.a[0].iCursor = -1; |
| 1388 | sNC.pParse = pParse; |
| 1389 | sNC.pSrcList = &sSrc; |
drh | 06f6541 | 2005-11-03 02:03:13 +0000 | [diff] [blame] | 1390 | sNC.isCheck = 1; |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1391 | if( sqlite3ExprResolveNames(&sNC, p->pCheck) ){ |
| 1392 | return; |
| 1393 | } |
| 1394 | } |
| 1395 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
| 1396 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1397 | /* 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] | 1398 | ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
| 1399 | ** So do not write to the disk again. Extract the root page number |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1400 | ** for the table from the db->init.newTnum field. (The page number |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1401 | ** should have been put there by the sqliteOpenCb routine.) |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1402 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1403 | if( db->init.busy ){ |
| 1404 | p->tnum = db->init.newTnum; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1407 | /* If not initializing, then create a record for the new table |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1408 | ** in the SQLITE_MASTER table of the database. The record number |
| 1409 | ** for the new table entry should already be on the stack. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1410 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1411 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
| 1412 | ** file instead of into the main database file. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1413 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1414 | if( !db->init.busy ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1415 | int n; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1416 | Vdbe *v; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1417 | char *zType; /* "view" or "table" */ |
| 1418 | char *zType2; /* "VIEW" or "TABLE" */ |
| 1419 | char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1420 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1421 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1422 | if( v==0 ) return; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1423 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 1424 | sqlite3VdbeAddOp(v, OP_Close, 0, 0); |
| 1425 | |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1426 | /* Create the rootpage for the new table and push it onto the stack. |
| 1427 | ** A view has no rootpage, so just push a zero onto the stack for |
| 1428 | ** views. Initialize zType at the same time. |
| 1429 | */ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1430 | if( p->pSelect==0 ){ |
| 1431 | /* A regular table */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1432 | zType = "table"; |
| 1433 | zType2 = "TABLE"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1434 | #ifndef SQLITE_OMIT_VIEW |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1435 | }else{ |
| 1436 | /* A view */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1437 | zType = "view"; |
| 1438 | zType2 = "VIEW"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1439 | #endif |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1440 | } |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1441 | |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1442 | /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT |
| 1443 | ** statement to populate the new table. The root-page number for the |
| 1444 | ** new table is on the top of the vdbe stack. |
| 1445 | ** |
| 1446 | ** Once the SELECT has been coded by sqlite3Select(), it is in a |
| 1447 | ** suitable state to query for the column names and types to be used |
| 1448 | ** by the new table. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 1449 | ** |
| 1450 | ** A shared-cache write-lock is not required to write to the new table, |
| 1451 | ** as a schema-lock must have already been obtained to create it. Since |
| 1452 | ** a schema-lock excludes all other database users, the write-lock would |
| 1453 | ** be redundant. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1454 | */ |
| 1455 | if( pSelect ){ |
| 1456 | Table *pSelTab; |
| 1457 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1458 | sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1459 | sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0); |
| 1460 | pParse->nTab = 2; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1461 | sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1462 | sqlite3VdbeAddOp(v, OP_Close, 1, 0); |
| 1463 | if( pParse->nErr==0 ){ |
| 1464 | pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect); |
| 1465 | if( pSelTab==0 ) return; |
| 1466 | assert( p->aCol==0 ); |
| 1467 | p->nCol = pSelTab->nCol; |
| 1468 | p->aCol = pSelTab->aCol; |
| 1469 | pSelTab->nCol = 0; |
| 1470 | pSelTab->aCol = 0; |
| 1471 | sqlite3DeleteTable(0, pSelTab); |
| 1472 | } |
| 1473 | } |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1474 | |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1475 | /* Compute the complete text of the CREATE statement */ |
| 1476 | if( pSelect ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1477 | zStmt = createTableStmt(p, p->pSchema==pParse->db->aDb[1].pSchema); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1478 | }else{ |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 1479 | n = pEnd->z - pParse->sNameToken.z + 1; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1480 | zStmt = sqlite3MPrintf("CREATE %s %.*s", zType2, n, pParse->sNameToken.z); |
| 1481 | } |
| 1482 | |
| 1483 | /* A slot for the record has already been allocated in the |
| 1484 | ** SQLITE_MASTER table. We just need to update that slot with all |
| 1485 | ** the information we've collected. The rowid for the preallocated |
| 1486 | ** slot is the 2nd item on the stack. The top of the stack is the |
| 1487 | ** root page for the new table (or a 0 if this is a view). |
| 1488 | */ |
| 1489 | sqlite3NestedParse(pParse, |
| 1490 | "UPDATE %Q.%s " |
| 1491 | "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#0, sql=%Q " |
| 1492 | "WHERE rowid=#1", |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1493 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1494 | zType, |
| 1495 | p->zName, |
| 1496 | p->zName, |
| 1497 | zStmt |
| 1498 | ); |
| 1499 | sqliteFree(zStmt); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1500 | sqlite3ChangeCookie(db, v, iDb); |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1501 | |
| 1502 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 1503 | /* Check to see if we need to create an sqlite_sequence table for |
| 1504 | ** keeping track of autoincrement keys. |
| 1505 | */ |
| 1506 | if( p->autoInc ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1507 | Db *pDb = &db->aDb[iDb]; |
| 1508 | if( pDb->pSchema->pSeqTab==0 ){ |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1509 | sqlite3NestedParse(pParse, |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 1510 | "CREATE TABLE %Q.sqlite_sequence(name,seq)", |
| 1511 | pDb->zName |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1512 | ); |
| 1513 | } |
| 1514 | } |
| 1515 | #endif |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1516 | |
| 1517 | /* Reparse everything to update our internal data structures */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1518 | sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1519 | sqlite3MPrintf("tbl_name='%q'",p->zName), P3_DYNAMIC); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1520 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1521 | |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1522 | |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1523 | /* Add the table to the in-memory representation of the database. |
| 1524 | */ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1525 | if( db->init.busy && pParse->nErr==0 ){ |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1526 | Table *pOld; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1527 | FKey *pFKey; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 1528 | Schema *pSchema = p->pSchema; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1529 | pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, strlen(p->zName)+1,p); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1530 | if( pOld ){ |
| 1531 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 1532 | return; |
| 1533 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1534 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1535 | for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 1536 | int nTo = strlen(pFKey->zTo) + 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1537 | pFKey->pNextTo = sqlite3HashFind(&pSchema->aFKey, pFKey->zTo, nTo); |
| 1538 | sqlite3HashInsert(&pSchema->aFKey, pFKey->zTo, nTo, pFKey); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1539 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1540 | #endif |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1541 | pParse->pNewTable = 0; |
| 1542 | db->nTable++; |
| 1543 | db->flags |= SQLITE_InternChanges; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 1544 | |
| 1545 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 1546 | if( !p->pSelect ){ |
| 1547 | assert( !pSelect && pCons && pEnd ); |
| 1548 | if( pCons->z==0 ) pCons = pEnd; |
| 1549 | p->addColOffset = 13 + (pCons->z - pParse->sNameToken.z); |
| 1550 | } |
| 1551 | #endif |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 1552 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1555 | #ifndef SQLITE_OMIT_VIEW |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1556 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1557 | ** The parser calls this routine in order to create a new VIEW |
| 1558 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1559 | void sqlite3CreateView( |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1560 | Parse *pParse, /* The parsing context */ |
| 1561 | Token *pBegin, /* The CREATE token that begins the statement */ |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 1562 | Token *pName1, /* The token that holds the name of the view */ |
| 1563 | Token *pName2, /* The token that holds the name of the view */ |
drh | 6276c1c | 2002-07-08 22:03:32 +0000 | [diff] [blame] | 1564 | Select *pSelect, /* A SELECT statement that will become the new view */ |
| 1565 | int isTemp /* TRUE for a TEMPORARY view */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1566 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1567 | Table *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1568 | int n; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1569 | const unsigned char *z; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1570 | Token sEnd; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1571 | DbFixer sFix; |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 1572 | Token *pName; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1573 | int iDb; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1574 | |
drh | 7c3d64f | 2005-06-06 15:32:08 +0000 | [diff] [blame] | 1575 | if( pParse->nVar>0 ){ |
| 1576 | sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
| 1577 | sqlite3SelectDelete(pSelect); |
| 1578 | return; |
| 1579 | } |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1580 | sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1581 | p = pParse->pNewTable; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 1582 | if( p==0 || pParse->nErr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1583 | sqlite3SelectDelete(pSelect); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1584 | return; |
| 1585 | } |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 1586 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1587 | iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); |
| 1588 | if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName) |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1589 | && sqlite3FixSelect(&sFix, pSelect) |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1590 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1591 | sqlite3SelectDelete(pSelect); |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1592 | return; |
| 1593 | } |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 1594 | |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1595 | /* Make a copy of the entire SELECT statement that defines the view. |
| 1596 | ** This will force all the Expr.token.z values to be dynamically |
| 1597 | ** allocated rather than point to the input string - which means that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1598 | ** they will persist after the current sqlite3_exec() call returns. |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1599 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1600 | p->pSelect = sqlite3SelectDup(pSelect); |
| 1601 | sqlite3SelectDelete(pSelect); |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1602 | if( sqlite3ThreadDataReadOnly()->mallocFailed ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1603 | return; |
| 1604 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1605 | if( !pParse->db->init.busy ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1606 | sqlite3ViewGetColumnNames(pParse, p); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1607 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1608 | |
| 1609 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
| 1610 | ** the end. |
| 1611 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1612 | sEnd = pParse->sLastToken; |
| 1613 | if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){ |
| 1614 | sEnd.z += sEnd.n; |
| 1615 | } |
| 1616 | sEnd.n = 0; |
drh | b089c0b | 2004-06-26 14:46:39 +0000 | [diff] [blame] | 1617 | n = sEnd.z - pBegin->z; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1618 | z = (const unsigned char*)pBegin->z; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1619 | while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; } |
| 1620 | sEnd.z = &z[n-1]; |
| 1621 | sEnd.n = 1; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1622 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1623 | /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 1624 | sqlite3EndTable(pParse, 0, &sEnd, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1625 | return; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1626 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1627 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1628 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1629 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1630 | /* |
| 1631 | ** The Table structure pTable is really a VIEW. Fill in the names of |
| 1632 | ** the columns of the view in the pTable structure. Return the number |
jplyon | cfa5684 | 2004-01-19 04:55:56 +0000 | [diff] [blame] | 1633 | ** 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] | 1634 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1635 | int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
drh | 9b3187e | 2005-01-18 14:45:47 +0000 | [diff] [blame] | 1636 | Table *pSelTab; /* A fake table from which we get the result set */ |
| 1637 | Select *pSel; /* Copy of the SELECT that implements the view */ |
| 1638 | int nErr = 0; /* Number of errors encountered */ |
| 1639 | int n; /* Temporarily holds the number of cursors assigned */ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1640 | |
| 1641 | assert( pTable ); |
| 1642 | |
| 1643 | /* A positive nCol means the columns names for this view are |
| 1644 | ** already known. |
| 1645 | */ |
| 1646 | if( pTable->nCol>0 ) return 0; |
| 1647 | |
| 1648 | /* A negative nCol is a special marker meaning that we are currently |
| 1649 | ** trying to compute the column names. If we enter this routine with |
| 1650 | ** a negative nCol, it means two or more views form a loop, like this: |
| 1651 | ** |
| 1652 | ** CREATE VIEW one AS SELECT * FROM two; |
| 1653 | ** CREATE VIEW two AS SELECT * FROM one; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 1654 | ** |
| 1655 | ** Actually, this error is caught previously and so the following test |
| 1656 | ** 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] | 1657 | */ |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 1658 | #if 0 |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1659 | if( pTable->nCol<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1660 | sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1661 | return 1; |
| 1662 | } |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 1663 | #endif |
| 1664 | assert( pTable->nCol>=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1665 | |
| 1666 | /* If we get this far, it means we need to compute the table names. |
drh | 9b3187e | 2005-01-18 14:45:47 +0000 | [diff] [blame] | 1667 | ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
| 1668 | ** "*" elements in the results set of the view and will assign cursors |
| 1669 | ** to the elements of the FROM clause. But we do not want these changes |
| 1670 | ** to be permanent. So the computation is done on a copy of the SELECT |
| 1671 | ** statement that defines the view. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1672 | */ |
drh | 9b3187e | 2005-01-18 14:45:47 +0000 | [diff] [blame] | 1673 | assert( pTable->pSelect ); |
| 1674 | pSel = sqlite3SelectDup(pTable->pSelect); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1675 | if( pSel ){ |
| 1676 | n = pParse->nTab; |
| 1677 | sqlite3SrcListAssignCursors(pParse, pSel->pSrc); |
| 1678 | pTable->nCol = -1; |
| 1679 | pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel); |
| 1680 | pParse->nTab = n; |
| 1681 | if( pSelTab ){ |
| 1682 | assert( pTable->aCol==0 ); |
| 1683 | pTable->nCol = pSelTab->nCol; |
| 1684 | pTable->aCol = pSelTab->aCol; |
| 1685 | pSelTab->nCol = 0; |
| 1686 | pSelTab->aCol = 0; |
| 1687 | sqlite3DeleteTable(0, pSelTab); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1688 | pTable->pSchema->flags |= DB_UnresetViews; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1689 | }else{ |
| 1690 | pTable->nCol = 0; |
| 1691 | nErr++; |
| 1692 | } |
| 1693 | sqlite3SelectDelete(pSel); |
| 1694 | } else { |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1695 | nErr++; |
| 1696 | } |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1697 | return nErr; |
| 1698 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1699 | #endif /* SQLITE_OMIT_VIEW */ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1700 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1701 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1702 | /* |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1703 | ** Clear the column names from every VIEW in database idx. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1704 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1705 | static void sqliteViewResetAll(sqlite3 *db, int idx){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1706 | HashElem *i; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1707 | if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1708 | for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1709 | Table *pTab = sqliteHashData(i); |
| 1710 | if( pTab->pSelect ){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 1711 | sqliteResetColumnNames(pTab); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1712 | } |
| 1713 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1714 | DbClearProperty(db, idx, DB_UnresetViews); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1715 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1716 | #else |
| 1717 | # define sqliteViewResetAll(A,B) |
| 1718 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1719 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1720 | /* |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1721 | ** This function is called by the VDBE to adjust the internal schema |
| 1722 | ** used by SQLite when the btree layer moves a table root page. The |
| 1723 | ** root-page of a table or index in database iDb has changed from iFrom |
| 1724 | ** to iTo. |
| 1725 | */ |
| 1726 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1727 | void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){ |
| 1728 | HashElem *pElem; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1729 | Hash *pHash; |
| 1730 | |
| 1731 | pHash = &pDb->pSchema->tblHash; |
| 1732 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1733 | Table *pTab = sqliteHashData(pElem); |
| 1734 | if( pTab->tnum==iFrom ){ |
| 1735 | pTab->tnum = iTo; |
| 1736 | return; |
| 1737 | } |
| 1738 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1739 | pHash = &pDb->pSchema->idxHash; |
| 1740 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1741 | Index *pIdx = sqliteHashData(pElem); |
| 1742 | if( pIdx->tnum==iFrom ){ |
| 1743 | pIdx->tnum = iTo; |
| 1744 | return; |
| 1745 | } |
| 1746 | } |
| 1747 | assert(0); |
| 1748 | } |
| 1749 | #endif |
| 1750 | |
| 1751 | /* |
| 1752 | ** Write code to erase the table with root-page iTable from database iDb. |
| 1753 | ** Also write code to modify the sqlite_master table and internal schema |
| 1754 | ** if a root-page of another table is moved by the btree-layer whilst |
| 1755 | ** erasing iTable (this can happen with an auto-vacuum database). |
| 1756 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 1757 | static void destroyRootPage(Parse *pParse, int iTable, int iDb){ |
| 1758 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | 40e016e | 2004-11-04 14:47:11 +0000 | [diff] [blame] | 1759 | sqlite3VdbeAddOp(v, OP_Destroy, iTable, iDb); |
| 1760 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 1761 | /* OP_Destroy pushes an integer onto the stack. If this integer |
| 1762 | ** is non-zero, then it is the root page number of a table moved to |
drh | 81db88e | 2004-12-07 12:29:17 +0000 | [diff] [blame] | 1763 | ** location iTable. The following code modifies the sqlite_master table to |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 1764 | ** reflect this. |
| 1765 | ** |
| 1766 | ** The "#0" in the SQL is a special constant that means whatever value |
| 1767 | ** is on the top of the stack. See sqlite3RegisterExpr(). |
| 1768 | */ |
danielk1977 | 63e3e9f | 2004-11-05 09:19:27 +0000 | [diff] [blame] | 1769 | sqlite3NestedParse(pParse, |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1770 | "UPDATE %Q.%s SET rootpage=%d WHERE #0 AND rootpage=#0", |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 1771 | pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1772 | #endif |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | ** Write VDBE code to erase table pTab and all associated indices on disk. |
| 1777 | ** Code to update the sqlite_master tables and internal schema definitions |
| 1778 | ** in case a root-page belonging to another table is moved by the btree layer |
| 1779 | ** is also added (this can happen with an auto-vacuum database). |
| 1780 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 1781 | static void destroyTable(Parse *pParse, Table *pTab){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1782 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1783 | Index *pIdx; |
drh | 29c636b | 2006-01-09 23:40:25 +0000 | [diff] [blame] | 1784 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 1785 | destroyRootPage(pParse, pTab->tnum, iDb); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1786 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 29c636b | 2006-01-09 23:40:25 +0000 | [diff] [blame] | 1787 | destroyRootPage(pParse, pIdx->tnum, iDb); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1788 | } |
| 1789 | #else |
| 1790 | /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM |
| 1791 | ** is not defined), then it is important to call OP_Destroy on the |
| 1792 | ** table and index root-pages in order, starting with the numerically |
| 1793 | ** largest root-page number. This guarantees that none of the root-pages |
| 1794 | ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the |
| 1795 | ** following were coded: |
| 1796 | ** |
| 1797 | ** OP_Destroy 4 0 |
| 1798 | ** ... |
| 1799 | ** OP_Destroy 5 0 |
| 1800 | ** |
| 1801 | ** and root page 5 happened to be the largest root-page number in the |
| 1802 | ** database, then root page 5 would be moved to page 4 by the |
| 1803 | ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit |
| 1804 | ** a free-list page. |
| 1805 | */ |
| 1806 | int iTab = pTab->tnum; |
| 1807 | int iDestroyed = 0; |
| 1808 | |
| 1809 | while( 1 ){ |
| 1810 | Index *pIdx; |
| 1811 | int iLargest = 0; |
| 1812 | |
| 1813 | if( iDestroyed==0 || iTab<iDestroyed ){ |
| 1814 | iLargest = iTab; |
| 1815 | } |
| 1816 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1817 | int iIdx = pIdx->tnum; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1818 | assert( pIdx->pSchema==pTab->pSchema ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1819 | if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){ |
| 1820 | iLargest = iIdx; |
| 1821 | } |
| 1822 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1823 | if( iLargest==0 ){ |
| 1824 | return; |
| 1825 | }else{ |
| 1826 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 1827 | destroyRootPage(pParse, iLargest, iDb); |
| 1828 | iDestroyed = iLargest; |
| 1829 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1830 | } |
| 1831 | #endif |
| 1832 | } |
| 1833 | |
| 1834 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1835 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1836 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1837 | */ |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 1838 | void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1839 | Table *pTab; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1840 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1841 | sqlite3 *db = pParse->db; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1842 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1843 | |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1844 | if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){ |
| 1845 | goto exit_drop_table; |
| 1846 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1847 | assert( pName->nSrc==1 ); |
| 1848 | pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase); |
| 1849 | |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 1850 | if( pTab==0 ){ |
| 1851 | if( noErr ){ |
| 1852 | sqlite3ErrorClear(pParse); |
| 1853 | } |
| 1854 | goto exit_drop_table; |
| 1855 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1856 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1857 | assert( iDb>=0 && iDb<db->nDb ); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1858 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1859 | { |
| 1860 | int code; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1861 | const char *zTab = SCHEMA_TABLE(iDb); |
| 1862 | const char *zDb = db->aDb[iDb].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1863 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1864 | goto exit_drop_table; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1865 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1866 | if( isView ){ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 1867 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1868 | code = SQLITE_DROP_TEMP_VIEW; |
| 1869 | }else{ |
| 1870 | code = SQLITE_DROP_VIEW; |
| 1871 | } |
| 1872 | }else{ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 1873 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1874 | code = SQLITE_DROP_TEMP_TABLE; |
| 1875 | }else{ |
| 1876 | code = SQLITE_DROP_TABLE; |
| 1877 | } |
| 1878 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1879 | if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){ |
| 1880 | goto exit_drop_table; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1881 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1882 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 1883 | goto exit_drop_table; |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 1884 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1885 | } |
| 1886 | #endif |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1887 | if( pTab->readOnly || pTab==db->aDb[iDb].pSchema->pSeqTab ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1888 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1889 | goto exit_drop_table; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1890 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1891 | |
| 1892 | #ifndef SQLITE_OMIT_VIEW |
| 1893 | /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used |
| 1894 | ** on a table. |
| 1895 | */ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1896 | if( isView && pTab->pSelect==0 ){ |
| 1897 | sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); |
| 1898 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1899 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1900 | if( !isView && pTab->pSelect ){ |
| 1901 | sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); |
| 1902 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1903 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1904 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1905 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1906 | /* Generate code to remove the table from the master table |
| 1907 | ** on disk. |
| 1908 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1909 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1910 | if( v ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1911 | Trigger *pTrigger; |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1912 | Db *pDb = &db->aDb[iDb]; |
| 1913 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1914 | |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1915 | /* Drop all triggers associated with the table being dropped. Code |
| 1916 | ** is generated to remove entries from sqlite_master and/or |
| 1917 | ** sqlite_temp_master if required. |
| 1918 | */ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1919 | pTrigger = pTab->pTrigger; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1920 | while( pTrigger ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1921 | assert( pTrigger->pSchema==pTab->pSchema || |
| 1922 | pTrigger->pSchema==db->aDb[1].pSchema ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1923 | sqlite3DropTriggerPtr(pParse, pTrigger, 1); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 1924 | pTrigger = pTrigger->pNext; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1925 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 1926 | |
danielk1977 | 4d36b81 | 2004-11-19 07:07:30 +0000 | [diff] [blame] | 1927 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 1928 | /* Remove any entries of the sqlite_sequence table associated with |
| 1929 | ** the table being dropped. This is done before the table is dropped |
| 1930 | ** at the btree level, in case the sqlite_sequence table needs to |
| 1931 | ** move as a result of the drop (can happen in auto-vacuum mode). |
| 1932 | */ |
| 1933 | if( pTab->autoInc ){ |
| 1934 | sqlite3NestedParse(pParse, |
| 1935 | "DELETE FROM %s.sqlite_sequence WHERE name=%Q", |
| 1936 | pDb->zName, pTab->zName |
| 1937 | ); |
| 1938 | } |
| 1939 | #endif |
| 1940 | |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1941 | /* Drop all SQLITE_MASTER table and index entries that refer to the |
| 1942 | ** table. The program name loops through the master table and deletes |
| 1943 | ** every row that refers to a table of the same name as the one being |
| 1944 | ** dropped. Triggers are handled seperately because a trigger can be |
| 1945 | ** created in the temp database that refers to a table in another |
| 1946 | ** database. |
| 1947 | */ |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 1948 | sqlite3NestedParse(pParse, |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1949 | "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1950 | pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1951 | if( !isView ){ |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 1952 | destroyTable(pParse, pTab); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1953 | } |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1954 | |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1955 | /* Remove the table entry from SQLite's internal schema and modify |
| 1956 | ** the schema cookie. |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1957 | */ |
| 1958 | sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1959 | sqlite3ChangeCookie(db, v, iDb); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1960 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1961 | sqliteViewResetAll(db, iDb); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 1962 | |
| 1963 | exit_drop_table: |
| 1964 | sqlite3SrcListDelete(pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | /* |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1968 | ** This routine is called to create a new foreign key on the table |
| 1969 | ** currently under construction. pFromCol determines which columns |
| 1970 | ** in the current table point to the foreign key. If pFromCol==0 then |
| 1971 | ** connect the key to the last column inserted. pTo is the name of |
| 1972 | ** the table referred to. pToCol is a list of tables in the other |
| 1973 | ** pTo table that the foreign key points to. flags contains all |
| 1974 | ** information about the conflict resolution algorithms specified |
| 1975 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
| 1976 | ** |
| 1977 | ** An FKey structure is created and added to the table currently |
| 1978 | ** under construction in the pParse->pNewTable field. The new FKey |
| 1979 | ** is not linked into db->aFKey at this point - that does not happen |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1980 | ** until sqlite3EndTable(). |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1981 | ** |
| 1982 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1983 | ** to sqlite3DeferForeignKey() might change this to DEFERRED. |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1984 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1985 | void sqlite3CreateForeignKey( |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1986 | Parse *pParse, /* Parsing context */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1987 | ExprList *pFromCol, /* Columns in this table that point to other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1988 | Token *pTo, /* Name of the other table */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1989 | ExprList *pToCol, /* Columns in the other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1990 | int flags /* Conflict resolution algorithms. */ |
| 1991 | ){ |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1992 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 40e016e | 2004-11-04 14:47:11 +0000 | [diff] [blame] | 1993 | FKey *pFKey = 0; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1994 | Table *p = pParse->pNewTable; |
| 1995 | int nByte; |
| 1996 | int i; |
| 1997 | int nCol; |
| 1998 | char *z; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1999 | |
| 2000 | assert( pTo!=0 ); |
| 2001 | if( p==0 || pParse->nErr ) goto fk_end; |
| 2002 | if( pFromCol==0 ){ |
| 2003 | int iCol = p->nCol-1; |
| 2004 | if( iCol<0 ) goto fk_end; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2005 | if( pToCol && pToCol->nExpr!=1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2006 | sqlite3ErrorMsg(pParse, "foreign key on %s" |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 2007 | " should reference only one column of table %T", |
| 2008 | p->aCol[iCol].zName, pTo); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2009 | goto fk_end; |
| 2010 | } |
| 2011 | nCol = 1; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2012 | }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2013 | sqlite3ErrorMsg(pParse, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2014 | "number of columns in foreign key does not match the number of " |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 2015 | "columns in the referenced table"); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2016 | goto fk_end; |
| 2017 | }else{ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2018 | nCol = pFromCol->nExpr; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2019 | } |
| 2020 | nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
| 2021 | if( pToCol ){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2022 | for(i=0; i<pToCol->nExpr; i++){ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2023 | nByte += strlen(pToCol->a[i].zName) + 1; |
| 2024 | } |
| 2025 | } |
| 2026 | pFKey = sqliteMalloc( nByte ); |
| 2027 | if( pFKey==0 ) goto fk_end; |
| 2028 | pFKey->pFrom = p; |
| 2029 | pFKey->pNextFrom = p->pFKey; |
drh | df68f6b | 2002-09-21 15:57:57 +0000 | [diff] [blame] | 2030 | z = (char*)&pFKey[1]; |
| 2031 | pFKey->aCol = (struct sColMap*)z; |
| 2032 | z += sizeof(struct sColMap)*nCol; |
| 2033 | pFKey->zTo = z; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2034 | memcpy(z, pTo->z, pTo->n); |
| 2035 | z[pTo->n] = 0; |
| 2036 | z += pTo->n+1; |
| 2037 | pFKey->pNextTo = 0; |
| 2038 | pFKey->nCol = nCol; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2039 | if( pFromCol==0 ){ |
| 2040 | pFKey->aCol[0].iFrom = p->nCol-1; |
| 2041 | }else{ |
| 2042 | for(i=0; i<nCol; i++){ |
| 2043 | int j; |
| 2044 | for(j=0; j<p->nCol; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2045 | if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2046 | pFKey->aCol[i].iFrom = j; |
| 2047 | break; |
| 2048 | } |
| 2049 | } |
| 2050 | if( j>=p->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2051 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 2052 | "unknown column \"%s\" in foreign key definition", |
| 2053 | pFromCol->a[i].zName); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2054 | goto fk_end; |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | if( pToCol ){ |
| 2059 | for(i=0; i<nCol; i++){ |
| 2060 | int n = strlen(pToCol->a[i].zName); |
| 2061 | pFKey->aCol[i].zCol = z; |
| 2062 | memcpy(z, pToCol->a[i].zName, n); |
| 2063 | z[n] = 0; |
| 2064 | z += n+1; |
| 2065 | } |
| 2066 | } |
| 2067 | pFKey->isDeferred = 0; |
| 2068 | pFKey->deleteConf = flags & 0xff; |
| 2069 | pFKey->updateConf = (flags >> 8 ) & 0xff; |
| 2070 | pFKey->insertConf = (flags >> 16 ) & 0xff; |
| 2071 | |
| 2072 | /* Link the foreign key to the table as the last step. |
| 2073 | */ |
| 2074 | p->pFKey = pFKey; |
| 2075 | pFKey = 0; |
| 2076 | |
| 2077 | fk_end: |
| 2078 | sqliteFree(pFKey); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2079 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2080 | sqlite3ExprListDelete(pFromCol); |
| 2081 | sqlite3ExprListDelete(pToCol); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | /* |
| 2085 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 2086 | ** clause is seen as part of a foreign key definition. The isDeferred |
| 2087 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 2088 | ** The behavior of the most recently created foreign key is adjusted |
| 2089 | ** accordingly. |
| 2090 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2091 | void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2092 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2093 | Table *pTab; |
| 2094 | FKey *pFKey; |
| 2095 | if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; |
| 2096 | pFKey->isDeferred = isDeferred; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2097 | #endif |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | /* |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2101 | ** Generate code that will erase and refill index *pIdx. This is |
| 2102 | ** used to initialize a newly created index or to recompute the |
| 2103 | ** content of an index in response to a REINDEX command. |
| 2104 | ** |
| 2105 | ** if memRootPage is not negative, it means that the index is newly |
| 2106 | ** created. The memory cell specified by memRootPage contains the |
| 2107 | ** root page number of the index. If memRootPage is negative, then |
| 2108 | ** the index already exists and must be cleared before being refilled and |
| 2109 | ** the root page number of the index is taken from pIndex->tnum. |
| 2110 | */ |
| 2111 | static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
| 2112 | Table *pTab = pIndex->pTable; /* The table that is indexed */ |
| 2113 | int iTab = pParse->nTab; /* Btree cursor used for pTab */ |
| 2114 | int iIdx = pParse->nTab+1; /* Btree cursor used for pIndex */ |
| 2115 | int addr1; /* Address of top of loop */ |
| 2116 | int tnum; /* Root page of index */ |
| 2117 | Vdbe *v; /* Generate code into this virtual machine */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2118 | KeyInfo *pKey; /* KeyInfo for index */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2119 | int iDb = sqlite3SchemaToIndex(pParse->db, pIndex->pSchema); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2120 | |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 2121 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 2122 | if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2123 | pParse->db->aDb[iDb].zName ) ){ |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 2124 | return; |
| 2125 | } |
| 2126 | #endif |
| 2127 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 2128 | /* Require a write-lock on the table to perform this operation */ |
| 2129 | sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
| 2130 | |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2131 | v = sqlite3GetVdbe(pParse); |
| 2132 | if( v==0 ) return; |
| 2133 | if( memRootPage>=0 ){ |
| 2134 | sqlite3VdbeAddOp(v, OP_MemLoad, memRootPage, 0); |
| 2135 | tnum = 0; |
| 2136 | }else{ |
| 2137 | tnum = pIndex->tnum; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2138 | sqlite3VdbeAddOp(v, OP_Clear, tnum, iDb); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2139 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2140 | sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2141 | pKey = sqlite3IndexKeyinfo(pParse, pIndex); |
| 2142 | sqlite3VdbeOp3(v, OP_OpenWrite, iIdx, tnum, (char *)pKey, P3_KEYINFO_HANDOFF); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 2143 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2144 | addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0); |
| 2145 | sqlite3GenerateIndexKey(v, pIndex, iTab); |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2146 | if( pIndex->onError!=OE_None ){ |
| 2147 | int curaddr = sqlite3VdbeCurrentAddr(v); |
| 2148 | int addr2 = curaddr+4; |
| 2149 | sqlite3VdbeChangeP2(v, curaddr-1, addr2); |
| 2150 | sqlite3VdbeAddOp(v, OP_Rowid, iTab, 0); |
| 2151 | sqlite3VdbeAddOp(v, OP_AddImm, 1, 0); |
| 2152 | sqlite3VdbeAddOp(v, OP_IsUnique, iIdx, addr2); |
| 2153 | sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort, |
| 2154 | "indexed columns are not unique", P3_STATIC); |
| 2155 | assert( addr2==sqlite3VdbeCurrentAddr(v) ); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 2156 | } |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2157 | sqlite3VdbeAddOp(v, OP_IdxInsert, iIdx, 0); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2158 | sqlite3VdbeAddOp(v, OP_Next, iTab, addr1+1); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 2159 | sqlite3VdbeJumpHere(v, addr1); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2160 | sqlite3VdbeAddOp(v, OP_Close, iTab, 0); |
| 2161 | sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); |
| 2162 | } |
| 2163 | |
| 2164 | /* |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2165 | ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
| 2166 | ** and pTblList is the name of the table that is to be indexed. Both will |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 2167 | ** be NULL for a primary key or an index that is created to satisfy a |
| 2168 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2169 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 2170 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2171 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2172 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 2173 | ** is a primary key or unique-constraint on the most recent column added |
| 2174 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2175 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2176 | void sqlite3CreateIndex( |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2177 | Parse *pParse, /* All information about this parse */ |
| 2178 | Token *pName1, /* First part of index name. May be NULL */ |
| 2179 | Token *pName2, /* Second part of index name. May be NULL */ |
| 2180 | SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2181 | ExprList *pList, /* A list of columns to be indexed */ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2182 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 2183 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2184 | Token *pEnd, /* The ")" that closes the CREATE INDEX statement */ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 2185 | int sortOrder, /* Sort order of primary key when pList==NULL */ |
| 2186 | int ifNotExist /* Omit error if index already exists */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2187 | ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2188 | Table *pTab = 0; /* Table to be indexed */ |
| 2189 | Index *pIndex = 0; /* The index to be created */ |
| 2190 | char *zName = 0; /* Name of the index */ |
| 2191 | int nName; /* Number of characters in zName */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 2192 | int i, j; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2193 | Token nullId; /* Fake token for an empty ID list */ |
| 2194 | DbFixer sFix; /* For assigning database names to pTable */ |
| 2195 | int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2196 | sqlite3 *db = pParse->db; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2197 | Db *pDb; /* The specific table containing the indexed database */ |
| 2198 | int iDb; /* Index of the database that is being written */ |
| 2199 | Token *pName = 0; /* Unqualified name of the index to create */ |
| 2200 | struct ExprList_item *pListItem; /* For looping over pList */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2201 | int nCol; |
| 2202 | int nExtra = 0; |
| 2203 | char *zExtra; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2204 | |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2205 | if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 2206 | goto exit_create_index; |
| 2207 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2208 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2209 | /* |
| 2210 | ** Find the table that is to be indexed. Return early if not found. |
| 2211 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2212 | if( pTblName!=0 ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2213 | |
| 2214 | /* Use the two-part index name to determine the database |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2215 | ** to search for the table. 'Fix' the table name to this db |
| 2216 | ** before looking up the table. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2217 | */ |
| 2218 | assert( pName1 && pName2 ); |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2219 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2220 | if( iDb<0 ) goto exit_create_index; |
| 2221 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2222 | #ifndef SQLITE_OMIT_TEMPDB |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2223 | /* If the index name was unqualified, check if the the table |
| 2224 | ** is a temp table. If so, set the database to 1. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2225 | */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2226 | pTab = sqlite3SrcListLookup(pParse, pTblName); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2227 | if( pName2 && pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2228 | iDb = 1; |
| 2229 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2230 | #endif |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2231 | |
| 2232 | if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) && |
| 2233 | sqlite3FixSrcList(&sFix, pTblName) |
| 2234 | ){ |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 2235 | /* Because the parser constructs pTblName from a single identifier, |
| 2236 | ** sqlite3FixSrcList can never fail. */ |
| 2237 | assert(0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2238 | } |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2239 | pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName, |
| 2240 | pTblName->a[0].zDatabase); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2241 | if( !pTab ) goto exit_create_index; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2242 | assert( db->aDb[iDb].pSchema==pTab->pSchema ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2243 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 2244 | assert( pName==0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2245 | pTab = pParse->pNewTable; |
drh | a6370df | 2006-01-04 21:40:06 +0000 | [diff] [blame] | 2246 | if( !pTab ) goto exit_create_index; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2247 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2248 | } |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2249 | pDb = &db->aDb[iDb]; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2250 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2251 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 2252 | if( pTab->readOnly ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2253 | sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 2254 | goto exit_create_index; |
| 2255 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2256 | #ifndef SQLITE_OMIT_VIEW |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2257 | if( pTab->pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2258 | sqlite3ErrorMsg(pParse, "views may not be indexed"); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2259 | goto exit_create_index; |
| 2260 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2261 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2262 | |
| 2263 | /* |
| 2264 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2265 | ** index or table with the same name. |
| 2266 | ** |
| 2267 | ** Exception: If we are reading the names of permanent indices from the |
| 2268 | ** sqlite_master table (because some other process changed the schema) and |
| 2269 | ** 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] | 2270 | ** index, then we will continue to process this index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2271 | ** |
| 2272 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 2273 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 2274 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2275 | */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2276 | if( pName ){ |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 2277 | zName = sqlite3NameFromToken(pName); |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 2278 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 2279 | if( zName==0 ) goto exit_create_index; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2280 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 2281 | goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 2282 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2283 | if( !db->init.busy ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 2284 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2285 | if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 2286 | if( !ifNotExist ){ |
| 2287 | sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
| 2288 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2289 | goto exit_create_index; |
| 2290 | } |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 2291 | if( sqlite3FindTable(db, zName, 0)!=0 ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2292 | sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
| 2293 | goto exit_create_index; |
| 2294 | } |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 2295 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 2296 | }else{ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 2297 | char zBuf[30]; |
| 2298 | int n; |
| 2299 | Index *pLoop; |
| 2300 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2301 | sprintf(zBuf,"_%d",n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2302 | zName = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2303 | sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 2304 | if( zName==0 ) goto exit_create_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 2307 | /* Check for authorization to create an index. |
| 2308 | */ |
| 2309 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 2310 | { |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2311 | const char *zDb = pDb->zName; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2312 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 2313 | goto exit_create_index; |
| 2314 | } |
| 2315 | i = SQLITE_CREATE_INDEX; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2316 | if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2317 | if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 2318 | goto exit_create_index; |
| 2319 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 2320 | } |
| 2321 | #endif |
| 2322 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2323 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 2324 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2325 | ** So create a fake list to simulate this. |
| 2326 | */ |
| 2327 | if( pList==0 ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2328 | nullId.z = (u8*)pTab->aCol[pTab->nCol-1].zName; |
| 2329 | nullId.n = strlen((char*)nullId.z); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2330 | pList = sqlite3ExprListAppend(0, 0, &nullId); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2331 | if( pList==0 ) goto exit_create_index; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2332 | pList->a[0].sortOrder = sortOrder; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2335 | /* Figure out how many bytes of space are required to store explicitly |
| 2336 | ** specified collation sequence names. |
| 2337 | */ |
| 2338 | for(i=0; i<pList->nExpr; i++){ |
| 2339 | Expr *pExpr = pList->a[i].pExpr; |
| 2340 | if( pExpr ){ |
| 2341 | nExtra += (1 + strlen(pExpr->pColl->zName)); |
| 2342 | } |
| 2343 | } |
| 2344 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2345 | /* |
| 2346 | ** Allocate the index structure. |
| 2347 | */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2348 | nName = strlen(zName); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2349 | nCol = pList->nExpr; |
| 2350 | pIndex = sqliteMalloc( |
| 2351 | sizeof(Index) + /* Index structure */ |
| 2352 | sizeof(int)*nCol + /* Index.aiColumn */ |
| 2353 | sizeof(int)*(nCol+1) + /* Index.aiRowEst */ |
| 2354 | sizeof(char *)*nCol + /* Index.azColl */ |
| 2355 | sizeof(u8)*nCol + /* Index.aSortOrder */ |
| 2356 | nName + 1 + /* Index.zName */ |
| 2357 | nExtra /* Collation sequence names */ |
| 2358 | ); |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2359 | if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto exit_create_index; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2360 | pIndex->aiColumn = (int *)(&pIndex[1]); |
| 2361 | pIndex->aiRowEst = (int *)(&pIndex->aiColumn[nCol]); |
| 2362 | pIndex->azColl = (char **)(&pIndex->aiRowEst[nCol+1]); |
| 2363 | pIndex->aSortOrder = (u8 *)(&pIndex->azColl[nCol]); |
| 2364 | pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]); |
| 2365 | zExtra = (char *)(&pIndex->zName[nName+1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2366 | strcpy(pIndex->zName, zName); |
| 2367 | pIndex->pTable = pTab; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2368 | pIndex->nColumn = pList->nExpr; |
drh | ea1ba17 | 2003-04-20 00:00:23 +0000 | [diff] [blame] | 2369 | pIndex->onError = onError; |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 2370 | pIndex->autoIndex = pName==0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2371 | pIndex->pSchema = db->aDb[iDb].pSchema; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2372 | |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2373 | /* Check to see if we should honor DESC requests on index columns |
| 2374 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2375 | if( pDb->pSchema->file_format>=4 ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2376 | sortOrderMask = -1; /* Honor DESC */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2377 | }else{ |
| 2378 | sortOrderMask = 0; /* Ignore DESC */ |
| 2379 | } |
| 2380 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 2381 | /* Scan the names of the columns of the table to be indexed and |
| 2382 | ** load the column indices into the Index structure. Report an error |
| 2383 | ** if any column is not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2384 | */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2385 | for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){ |
| 2386 | const char *zColName = pListItem->zName; |
| 2387 | Column *pTabCol; |
drh | 85eeb69 | 2005-12-21 03:16:42 +0000 | [diff] [blame] | 2388 | int requestedSortOrder; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2389 | char *zColl; /* Collation sequence */ |
| 2390 | |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2391 | for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){ |
| 2392 | if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2393 | } |
| 2394 | if( j>=pTab->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2395 | sqlite3ErrorMsg(pParse, "table %s has no column named %s", |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2396 | pTab->zName, zColName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2397 | goto exit_create_index; |
| 2398 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 2399 | pIndex->aiColumn[i] = j; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2400 | if( pListItem->pExpr ){ |
| 2401 | assert( pListItem->pExpr->pColl ); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2402 | zColl = zExtra; |
| 2403 | strcpy(zExtra, pListItem->pExpr->pColl->zName); |
| 2404 | zExtra += (strlen(zColl) + 1); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2405 | }else{ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2406 | zColl = pTab->aCol[j].zColl; |
| 2407 | if( !zColl ){ |
| 2408 | zColl = db->pDfltColl->zName; |
| 2409 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2410 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2411 | if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl, -1) ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2412 | goto exit_create_index; |
| 2413 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2414 | pIndex->azColl[i] = zColl; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2415 | requestedSortOrder = pListItem->sortOrder & sortOrderMask; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2416 | pIndex->aSortOrder[i] = requestedSortOrder; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2417 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2418 | sqlite3DefaultRowEst(pIndex); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2419 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2420 | if( pTab==pParse->pNewTable ){ |
| 2421 | /* This routine has been called to create an automatic index as a |
| 2422 | ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
| 2423 | ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
| 2424 | ** i.e. one of: |
| 2425 | ** |
| 2426 | ** CREATE TABLE t(x PRIMARY KEY, y); |
| 2427 | ** CREATE TABLE t(x, y, UNIQUE(x, y)); |
| 2428 | ** |
| 2429 | ** Either way, check to see if the table already has such an index. If |
| 2430 | ** so, don't bother creating this one. This only applies to |
| 2431 | ** automatically created indices. Users can do as they wish with |
| 2432 | ** explicit indices. |
| 2433 | */ |
| 2434 | Index *pIdx; |
| 2435 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 2436 | int k; |
| 2437 | assert( pIdx->onError!=OE_None ); |
| 2438 | assert( pIdx->autoIndex ); |
| 2439 | assert( pIndex->onError!=OE_None ); |
| 2440 | |
| 2441 | if( pIdx->nColumn!=pIndex->nColumn ) continue; |
| 2442 | for(k=0; k<pIdx->nColumn; k++){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2443 | const char *z1 = pIdx->azColl[k]; |
| 2444 | const char *z2 = pIndex->azColl[k]; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2445 | if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2446 | if( pIdx->aSortOrder[k]!=pIndex->aSortOrder[k] ) break; |
| 2447 | if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2448 | } |
| 2449 | if( k==pIdx->nColumn ){ |
danielk1977 | f736b77 | 2004-06-17 06:13:34 +0000 | [diff] [blame] | 2450 | if( pIdx->onError!=pIndex->onError ){ |
| 2451 | /* This constraint creates the same index as a previous |
| 2452 | ** constraint specified somewhere in the CREATE TABLE statement. |
| 2453 | ** However the ON CONFLICT clauses are different. If both this |
| 2454 | ** constraint and the previous equivalent constraint have explicit |
| 2455 | ** ON CONFLICT clauses this is an error. Otherwise, use the |
| 2456 | ** explicitly specified behaviour for the index. |
| 2457 | */ |
| 2458 | if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ |
| 2459 | sqlite3ErrorMsg(pParse, |
| 2460 | "conflicting ON CONFLICT clauses specified", 0); |
| 2461 | } |
| 2462 | if( pIdx->onError==OE_Default ){ |
| 2463 | pIdx->onError = pIndex->onError; |
| 2464 | } |
| 2465 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2466 | goto exit_create_index; |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2471 | /* Link the new Index structure to its table and to the other |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 2472 | ** in-memory database structures. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2473 | */ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2474 | if( db->init.busy ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 2475 | Index *p; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2476 | p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
drh | 3c8bf55 | 2003-07-01 18:13:14 +0000 | [diff] [blame] | 2477 | pIndex->zName, strlen(pIndex->zName)+1, pIndex); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 2478 | if( p ){ |
| 2479 | assert( p==pIndex ); /* Malloc must have failed */ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 2480 | goto exit_create_index; |
| 2481 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2482 | db->flags |= SQLITE_InternChanges; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2483 | if( pTblName!=0 ){ |
| 2484 | pIndex->tnum = db->init.newTnum; |
| 2485 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 2488 | /* 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] | 2489 | ** involves writing the index into the master table and filling in the |
| 2490 | ** index with the current table contents. |
| 2491 | ** |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 2492 | ** The db->init.busy is 0 when the user first enters a CREATE INDEX |
| 2493 | ** command. db->init.busy is 1 when a database is opened and |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2494 | ** CREATE INDEX statements are read out of the master table. In |
| 2495 | ** the latter case the index already exists on disk, which is why |
| 2496 | ** we don't want to recreate it. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2497 | ** |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2498 | ** If pTblName==0 it means this index is generated as a primary key |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2499 | ** or UNIQUE constraint of a CREATE TABLE statement. Since the table |
| 2500 | ** has just been created, it contains no data and the index initialization |
| 2501 | ** step can be skipped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2502 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 2503 | else if( db->init.busy==0 ){ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 2504 | Vdbe *v; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2505 | char *zStmt; |
| 2506 | int iMem = pParse->nMem++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2507 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2508 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2509 | if( v==0 ) goto exit_create_index; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2510 | |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 2511 | |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2512 | /* Create the rootpage for the index |
| 2513 | */ |
drh | aee128d | 2005-02-14 20:48:18 +0000 | [diff] [blame] | 2514 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2515 | sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2516 | sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); |
| 2517 | |
| 2518 | /* Gather the complete text of the CREATE INDEX statement into |
| 2519 | ** the zStmt variable |
| 2520 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2521 | if( pStart && pEnd ){ |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2522 | /* A named index with an explicit CREATE INDEX statement */ |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 2523 | zStmt = sqlite3MPrintf("CREATE%s INDEX %.*s", |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2524 | onError==OE_None ? "" : " UNIQUE", |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 2525 | pEnd->z - pName->z + 1, |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2526 | pName->z); |
| 2527 | }else{ |
| 2528 | /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ |
drh | e497f00 | 2004-11-07 13:01:49 +0000 | [diff] [blame] | 2529 | /* zStmt = sqlite3MPrintf(""); */ |
| 2530 | zStmt = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2531 | } |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2532 | |
| 2533 | /* Add an entry in sqlite_master for this index |
| 2534 | */ |
| 2535 | sqlite3NestedParse(pParse, |
drh | e497f00 | 2004-11-07 13:01:49 +0000 | [diff] [blame] | 2536 | "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#0,%Q);", |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2537 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
| 2538 | pIndex->zName, |
| 2539 | pTab->zName, |
| 2540 | zStmt |
| 2541 | ); |
| 2542 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
| 2543 | sqliteFree(zStmt); |
| 2544 | |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 2545 | /* Fill the index with data and reparse the schema. Code an OP_Expire |
| 2546 | ** to invalidate all pre-compiled statements. |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2547 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 2548 | if( pTblName ){ |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 2549 | sqlite3RefillIndex(pParse, pIndex, iMem); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2550 | sqlite3ChangeCookie(db, v, iDb); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2551 | sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, |
| 2552 | sqlite3MPrintf("name='%q'", pIndex->zName), P3_DYNAMIC); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 2553 | sqlite3VdbeAddOp(v, OP_Expire, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2554 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2557 | /* When adding an index to the list of indices for a table, make |
| 2558 | ** sure all indices labeled OE_Replace come after all those labeled |
| 2559 | ** OE_Ignore. This is necessary for the correct operation of UPDATE |
| 2560 | ** and INSERT. |
| 2561 | */ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2562 | if( db->init.busy || pTblName==0 ){ |
| 2563 | if( onError!=OE_Replace || pTab->pIndex==0 |
| 2564 | || pTab->pIndex->onError==OE_Replace){ |
| 2565 | pIndex->pNext = pTab->pIndex; |
| 2566 | pTab->pIndex = pIndex; |
| 2567 | }else{ |
| 2568 | Index *pOther = pTab->pIndex; |
| 2569 | while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
| 2570 | pOther = pOther->pNext; |
| 2571 | } |
| 2572 | pIndex->pNext = pOther->pNext; |
| 2573 | pOther->pNext = pIndex; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2574 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2575 | pIndex = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2576 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2577 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2578 | /* Clean up before exiting */ |
| 2579 | exit_create_index: |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 2580 | if( pIndex ){ |
| 2581 | freeIndex(pIndex); |
| 2582 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2583 | sqlite3ExprListDelete(pList); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 2584 | sqlite3SrcListDelete(pTblName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2585 | sqliteFree(zName); |
| 2586 | return; |
| 2587 | } |
| 2588 | |
| 2589 | /* |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 2590 | ** Generate code to make sure the file format number is at least minFormat. |
| 2591 | ** The generated code will increase the file format number if necessary. |
| 2592 | */ |
| 2593 | void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){ |
| 2594 | Vdbe *v; |
| 2595 | v = sqlite3GetVdbe(pParse); |
| 2596 | if( v ){ |
| 2597 | sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1); |
| 2598 | sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0); |
| 2599 | sqlite3VdbeAddOp(v, OP_Ge, 0, sqlite3VdbeCurrentAddr(v)+3); |
| 2600 | sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0); |
| 2601 | sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1); |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2606 | ** Fill the Index.aiRowEst[] array with default information - information |
drh | 91124b3 | 2005-08-18 18:15:05 +0000 | [diff] [blame] | 2607 | ** to be used when we have not run the ANALYZE command. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2608 | ** |
| 2609 | ** aiRowEst[0] is suppose to contain the number of elements in the index. |
| 2610 | ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
| 2611 | ** number of rows in the table that match any particular value of the |
| 2612 | ** first column of the index. aiRowEst[2] is an estimate of the number |
| 2613 | ** of rows that match any particular combiniation of the first 2 columns |
| 2614 | ** of the index. And so forth. It must always be the case that |
| 2615 | * |
| 2616 | ** aiRowEst[N]<=aiRowEst[N-1] |
| 2617 | ** aiRowEst[N]>=1 |
| 2618 | ** |
| 2619 | ** Apart from that, we have little to go on besides intuition as to |
| 2620 | ** how aiRowEst[] should be initialized. The numbers generated here |
| 2621 | ** are based on typical values found in actual indices. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2622 | */ |
| 2623 | void sqlite3DefaultRowEst(Index *pIdx){ |
drh | 37108e1 | 2005-08-31 13:13:31 +0000 | [diff] [blame] | 2624 | unsigned *a = pIdx->aiRowEst; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2625 | int i; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2626 | assert( a!=0 ); |
| 2627 | a[0] = 1000000; |
drh | 3adc9ce | 2005-07-28 16:51:51 +0000 | [diff] [blame] | 2628 | for(i=pIdx->nColumn; i>=1; i--){ |
| 2629 | a[i] = 10; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2630 | } |
| 2631 | if( pIdx->onError!=OE_None ){ |
| 2632 | a[pIdx->nColumn] = 1; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 2637 | ** This routine will drop an existing named index. This routine |
| 2638 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2639 | */ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 2640 | void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2641 | Index *pIndex; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2642 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2643 | sqlite3 *db = pParse->db; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2644 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2645 | |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2646 | if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 2647 | goto exit_drop_index; |
| 2648 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 2649 | assert( pName->nSrc==1 ); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 2650 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 2651 | goto exit_drop_index; |
| 2652 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2653 | pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2654 | if( pIndex==0 ){ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 2655 | if( !ifExists ){ |
| 2656 | sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0); |
| 2657 | } |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 2658 | pParse->checkSchema = 1; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 2659 | goto exit_drop_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2660 | } |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 2661 | if( pIndex->autoIndex ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2662 | sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 2663 | "or PRIMARY KEY constraint cannot be dropped", 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 2664 | goto exit_drop_index; |
| 2665 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2666 | iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 2667 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 2668 | { |
| 2669 | int code = SQLITE_DROP_INDEX; |
| 2670 | Table *pTab = pIndex->pTable; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2671 | const char *zDb = db->aDb[iDb].zName; |
| 2672 | const char *zTab = SCHEMA_TABLE(iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2673 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 2674 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 2675 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2676 | if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2677 | if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 2678 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 2679 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 2680 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 2681 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2682 | |
| 2683 | /* Generate code to remove the index and from the master table */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2684 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2685 | if( v ){ |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 2686 | sqlite3NestedParse(pParse, |
| 2687 | "DELETE FROM %Q.%s WHERE name=%Q", |
| 2688 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
| 2689 | pIndex->zName |
| 2690 | ); |
| 2691 | sqlite3ChangeCookie(db, v, iDb); |
| 2692 | destroyRootPage(pParse, pIndex->tnum, iDb); |
| 2693 | sqlite3VdbeOp3(v, OP_DropIndex, iDb, 0, pIndex->zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 2696 | exit_drop_index: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2697 | sqlite3SrcListDelete(pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | /* |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2701 | ** ppArray points into a structure where there is an array pointer |
| 2702 | ** followed by two integers. The first integer is the |
| 2703 | ** number of elements in the structure array. The second integer |
| 2704 | ** is the number of allocated slots in the array. |
| 2705 | ** |
| 2706 | ** In other words, the structure looks something like this: |
| 2707 | ** |
| 2708 | ** struct Example1 { |
| 2709 | ** struct subElem *aEntry; |
| 2710 | ** int nEntry; |
| 2711 | ** int nAlloc; |
| 2712 | ** } |
| 2713 | ** |
| 2714 | ** The pnEntry parameter points to the equivalent of Example1.nEntry. |
| 2715 | ** |
| 2716 | ** This routine allocates a new slot in the array, zeros it out, |
| 2717 | ** and returns its index. If malloc fails a negative number is returned. |
| 2718 | ** |
| 2719 | ** szEntry is the sizeof of a single array entry. initSize is the |
| 2720 | ** number of array entries allocated on the initial allocation. |
| 2721 | */ |
| 2722 | int sqlite3ArrayAllocate(void **ppArray, int szEntry, int initSize){ |
| 2723 | char *p; |
| 2724 | int *an = (int*)&ppArray[1]; |
| 2725 | if( an[0]>=an[1] ){ |
| 2726 | void *pNew; |
drh | 5360ad3 | 2005-09-08 00:13:27 +0000 | [diff] [blame] | 2727 | int newSize; |
| 2728 | newSize = an[1]*2 + initSize; |
| 2729 | pNew = sqliteRealloc(*ppArray, newSize*szEntry); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2730 | if( pNew==0 ){ |
| 2731 | return -1; |
| 2732 | } |
drh | 5360ad3 | 2005-09-08 00:13:27 +0000 | [diff] [blame] | 2733 | an[1] = newSize; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2734 | *ppArray = pNew; |
| 2735 | } |
| 2736 | p = *ppArray; |
| 2737 | memset(&p[an[0]*szEntry], 0, szEntry); |
| 2738 | return an[0]++; |
| 2739 | } |
| 2740 | |
| 2741 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2742 | ** Append a new element to the given IdList. Create a new IdList if |
| 2743 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2744 | ** |
| 2745 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2746 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2747 | IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2748 | int i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2749 | if( pList==0 ){ |
| 2750 | pList = sqliteMalloc( sizeof(IdList) ); |
| 2751 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2752 | pList->nAlloc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2753 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2754 | i = sqlite3ArrayAllocate((void**)&pList->a, sizeof(pList->a[0]), 5); |
| 2755 | if( i<0 ){ |
| 2756 | sqlite3IdListDelete(pList); |
| 2757 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2758 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2759 | pList->a[i].zName = sqlite3NameFromToken(pToken); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2760 | return pList; |
| 2761 | } |
| 2762 | |
| 2763 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 2764 | ** Delete an IdList. |
| 2765 | */ |
| 2766 | void sqlite3IdListDelete(IdList *pList){ |
| 2767 | int i; |
| 2768 | if( pList==0 ) return; |
| 2769 | for(i=0; i<pList->nId; i++){ |
| 2770 | sqliteFree(pList->a[i].zName); |
| 2771 | } |
| 2772 | sqliteFree(pList->a); |
| 2773 | sqliteFree(pList); |
| 2774 | } |
| 2775 | |
| 2776 | /* |
| 2777 | ** Return the index in pList of the identifier named zId. Return -1 |
| 2778 | ** if not found. |
| 2779 | */ |
| 2780 | int sqlite3IdListIndex(IdList *pList, const char *zName){ |
| 2781 | int i; |
| 2782 | if( pList==0 ) return -1; |
| 2783 | for(i=0; i<pList->nId; i++){ |
| 2784 | if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; |
| 2785 | } |
| 2786 | return -1; |
| 2787 | } |
| 2788 | |
| 2789 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2790 | ** Append a new table name to the given SrcList. Create a new SrcList if |
| 2791 | ** need be. A new entry is created in the SrcList even if pToken is NULL. |
| 2792 | ** |
| 2793 | ** A new SrcList is returned, or NULL if malloc() fails. |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2794 | ** |
| 2795 | ** If pDatabase is not null, it means that the table has an optional |
| 2796 | ** database name prefix. Like this: "database.table". The pDatabase |
| 2797 | ** points to the table name and the pTable points to the database name. |
| 2798 | ** The SrcList.a[].zName field is filled with the table name which might |
| 2799 | ** come from pTable (if pDatabase is NULL) or from pDatabase. |
| 2800 | ** SrcList.a[].zDatabase is filled with the database name from pTable, |
| 2801 | ** or with NULL if no database is specified. |
| 2802 | ** |
| 2803 | ** In other words, if call like this: |
| 2804 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2805 | ** sqlite3SrcListAppend(A,B,0); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2806 | ** |
| 2807 | ** Then B is a table name and the database name is unspecified. If called |
| 2808 | ** like this: |
| 2809 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2810 | ** sqlite3SrcListAppend(A,B,C); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2811 | ** |
| 2812 | ** Then C is the table name and B is the database name. |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2813 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2814 | SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){ |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 2815 | struct SrcList_item *pItem; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2816 | if( pList==0 ){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2817 | pList = sqliteMalloc( sizeof(SrcList) ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2818 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2819 | pList->nAlloc = 1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2820 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2821 | if( pList->nSrc>=pList->nAlloc ){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2822 | SrcList *pNew; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2823 | pList->nAlloc *= 2; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2824 | pNew = sqliteRealloc(pList, |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 2825 | sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) ); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2826 | if( pNew==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2827 | sqlite3SrcListDelete(pList); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2828 | return 0; |
| 2829 | } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2830 | pList = pNew; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2831 | } |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 2832 | pItem = &pList->a[pList->nSrc]; |
| 2833 | memset(pItem, 0, sizeof(pList->a[0])); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2834 | if( pDatabase && pDatabase->z==0 ){ |
| 2835 | pDatabase = 0; |
| 2836 | } |
| 2837 | if( pDatabase && pTable ){ |
| 2838 | Token *pTemp = pDatabase; |
| 2839 | pDatabase = pTable; |
| 2840 | pTable = pTemp; |
| 2841 | } |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 2842 | pItem->zName = sqlite3NameFromToken(pTable); |
| 2843 | pItem->zDatabase = sqlite3NameFromToken(pDatabase); |
| 2844 | pItem->iCursor = -1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2845 | pList->nSrc++; |
| 2846 | return pList; |
| 2847 | } |
| 2848 | |
| 2849 | /* |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 2850 | ** Assign cursors to all tables in a SrcList |
| 2851 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2852 | void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 2853 | int i; |
drh | 9b3187e | 2005-01-18 14:45:47 +0000 | [diff] [blame] | 2854 | struct SrcList_item *pItem; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2855 | assert(pList || sqlite3ThreadDataReadOnly()->mallocFailed); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2856 | if( pList ){ |
| 2857 | for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
| 2858 | if( pItem->iCursor>=0 ) break; |
| 2859 | pItem->iCursor = pParse->nTab++; |
| 2860 | if( pItem->pSelect ){ |
| 2861 | sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); |
| 2862 | } |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2868 | ** Add an alias to the last identifier on the given identifier list. |
| 2869 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2870 | void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2871 | if( pList && pList->nSrc>0 ){ |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 2872 | pList->a[pList->nSrc-1].zAlias = sqlite3NameFromToken(pToken); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2873 | } |
| 2874 | } |
| 2875 | |
| 2876 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2877 | ** Delete an entire SrcList including all its substructure. |
| 2878 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2879 | void sqlite3SrcListDelete(SrcList *pList){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2880 | int i; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2881 | struct SrcList_item *pItem; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2882 | if( pList==0 ) return; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2883 | for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
| 2884 | sqliteFree(pItem->zDatabase); |
| 2885 | sqliteFree(pItem->zName); |
| 2886 | sqliteFree(pItem->zAlias); |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 2887 | sqlite3DeleteTable(0, pItem->pTab); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2888 | sqlite3SelectDelete(pItem->pSelect); |
| 2889 | sqlite3ExprDelete(pItem->pOn); |
| 2890 | sqlite3IdListDelete(pItem->pUsing); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2891 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2892 | sqliteFree(pList); |
| 2893 | } |
| 2894 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 2895 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2896 | ** Begin a transaction |
| 2897 | */ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2898 | void sqlite3BeginTransaction(Parse *pParse, int type){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2899 | sqlite3 *db; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2900 | Vdbe *v; |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2901 | int i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2902 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2903 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2904 | if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2905 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2906 | |
| 2907 | v = sqlite3GetVdbe(pParse); |
| 2908 | if( !v ) return; |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2909 | if( type!=TK_DEFERRED ){ |
| 2910 | for(i=0; i<db->nDb; i++){ |
| 2911 | sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); |
| 2912 | } |
| 2913 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2914 | sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | /* |
| 2918 | ** Commit a transaction |
| 2919 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2920 | void sqlite3CommitTransaction(Parse *pParse){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2921 | sqlite3 *db; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2922 | Vdbe *v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2923 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2924 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2925 | if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2926 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2927 | |
| 2928 | v = sqlite3GetVdbe(pParse); |
| 2929 | if( v ){ |
| 2930 | sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0); |
drh | 02f75f1 | 2004-02-24 01:04:11 +0000 | [diff] [blame] | 2931 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | /* |
| 2935 | ** Rollback a transaction |
| 2936 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2937 | void sqlite3RollbackTransaction(Parse *pParse){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2938 | sqlite3 *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2939 | Vdbe *v; |
| 2940 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2941 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2942 | if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2943 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2944 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2945 | v = sqlite3GetVdbe(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2946 | if( v ){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2947 | sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1); |
drh | 02f75f1 | 2004-02-24 01:04:11 +0000 | [diff] [blame] | 2948 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2949 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2950 | |
| 2951 | /* |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 2952 | ** Make sure the TEMP database is open and available for use. Return |
| 2953 | ** the number of errors. Leave any error messages in the pParse structure. |
| 2954 | */ |
| 2955 | static int sqlite3OpenTempDatabase(Parse *pParse){ |
| 2956 | sqlite3 *db = pParse->db; |
| 2957 | if( db->aDb[1].pBt==0 && !pParse->explain ){ |
| 2958 | int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt); |
| 2959 | if( rc!=SQLITE_OK ){ |
| 2960 | sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 2961 | "file for storing temporary tables"); |
| 2962 | pParse->rc = rc; |
| 2963 | return 1; |
| 2964 | } |
| 2965 | if( db->flags & !db->autoCommit ){ |
| 2966 | rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1); |
| 2967 | if( rc!=SQLITE_OK ){ |
| 2968 | sqlite3ErrorMsg(pParse, "unable to get a write lock on " |
| 2969 | "the temporary database file"); |
| 2970 | pParse->rc = rc; |
| 2971 | return 1; |
| 2972 | } |
| 2973 | } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2974 | assert( db->aDb[1].pSchema ); |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 2975 | } |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
| 2979 | /* |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2980 | ** Generate VDBE code that will verify the schema cookie and start |
| 2981 | ** a read-transaction for all named database files. |
| 2982 | ** |
| 2983 | ** It is important that all schema cookies be verified and all |
| 2984 | ** read transactions be started before anything else happens in |
| 2985 | ** the VDBE program. But this routine can be called after much other |
| 2986 | ** code has been generated. So here is what we do: |
| 2987 | ** |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2988 | ** The first time this routine is called, we code an OP_Goto that |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2989 | ** will jump to a subroutine at the end of the program. Then we |
| 2990 | ** record every database that needs its schema verified in the |
| 2991 | ** pParse->cookieMask field. Later, after all other code has been |
| 2992 | ** generated, the subroutine that does the cookie verifications and |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2993 | ** starts the transactions will be coded and the OP_Goto P2 value |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2994 | ** will be made to point to that subroutine. The generation of the |
| 2995 | ** cookie verification subroutine code happens in sqlite3FinishCoding(). |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2996 | ** |
| 2997 | ** If iDb<0 then code the OP_Goto only - don't set flag to verify the |
| 2998 | ** schema on any databases. This can be used to position the OP_Goto |
| 2999 | ** 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] | 3000 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3001 | void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3002 | sqlite3 *db; |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 3003 | Vdbe *v; |
| 3004 | int mask; |
| 3005 | |
| 3006 | v = sqlite3GetVdbe(pParse); |
| 3007 | if( v==0 ) return; /* This only happens if there was a prior error */ |
| 3008 | db = pParse->db; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 3009 | if( pParse->cookieGoto==0 ){ |
| 3010 | pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1; |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 3011 | } |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 3012 | if( iDb>=0 ){ |
| 3013 | assert( iDb<db->nDb ); |
| 3014 | assert( db->aDb[iDb].pBt!=0 || iDb==1 ); |
| 3015 | assert( iDb<32 ); |
| 3016 | mask = 1<<iDb; |
| 3017 | if( (pParse->cookieMask & mask)==0 ){ |
| 3018 | pParse->cookieMask |= mask; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3019 | pParse->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3020 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 3021 | sqlite3OpenTempDatabase(pParse); |
| 3022 | } |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 3023 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3024 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 3028 | ** Generate VDBE code that prepares for doing an operation that |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 3029 | ** might change the database. |
| 3030 | ** |
| 3031 | ** This routine starts a new transaction if we are not already within |
| 3032 | ** a transaction. If we are already within a transaction, then a checkpoint |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 3033 | ** is set if the setStatement parameter is true. A checkpoint should |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 3034 | ** be set for operations that might fail (due to a constraint) part of |
| 3035 | ** the way through and which will need to undo some writes without having to |
| 3036 | ** rollback the whole transaction. For operations where all constraints |
| 3037 | ** can be checked before any changes are made to the database, it is never |
| 3038 | ** necessary to undo a write and the checkpoint should not be set. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 3039 | ** |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 3040 | ** Only database iDb and the temp database are made writable by this call. |
| 3041 | ** If iDb==0, then the main and temp databases are made writable. If |
| 3042 | ** iDb==1 then only the temp database is made writable. If iDb>1 then the |
| 3043 | ** specified auxiliary database and the temp database are made writable. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 3044 | */ |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 3045 | void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3046 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3047 | if( v==0 ) return; |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 3048 | sqlite3CodeVerifySchema(pParse, iDb); |
| 3049 | pParse->writeMask |= 1<<iDb; |
danielk1977 | 63e3e9f | 2004-11-05 09:19:27 +0000 | [diff] [blame] | 3050 | if( setStatement && pParse->nested==0 ){ |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 3051 | sqlite3VdbeAddOp(v, OP_Statement, iDb, 0); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3052 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3053 | if( (OMIT_TEMPDB || iDb!=1) && pParse->db->aDb[1].pBt!=0 ){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3054 | sqlite3BeginWriteOperation(pParse, setStatement, 1); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3055 | } |
| 3056 | } |
| 3057 | |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3058 | /* |
| 3059 | ** Check to see if pIndex uses the collating sequence pColl. Return |
| 3060 | ** true if it does and false if it does not. |
| 3061 | */ |
| 3062 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3063 | static int collationMatch(const char *zColl, Index *pIndex){ |
| 3064 | int i; |
| 3065 | for(i=0; i<pIndex->nColumn; i++){ |
| 3066 | const char *z = pIndex->azColl[i]; |
| 3067 | if( z==zColl || (z && zColl && 0==sqlite3StrICmp(z, zColl)) ){ |
| 3068 | return 1; |
| 3069 | } |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3070 | } |
| 3071 | return 0; |
| 3072 | } |
| 3073 | #endif |
| 3074 | |
| 3075 | /* |
| 3076 | ** Recompute all indices of pTab that use the collating sequence pColl. |
| 3077 | ** If pColl==0 then recompute all indices of pTab. |
| 3078 | */ |
| 3079 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3080 | static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3081 | Index *pIndex; /* An index associated with pTab */ |
| 3082 | |
| 3083 | for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3084 | if( zColl==0 || collationMatch(zColl, pIndex) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3085 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 3086 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3087 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 3088 | } |
| 3089 | } |
| 3090 | } |
| 3091 | #endif |
| 3092 | |
| 3093 | /* |
| 3094 | ** Recompute all indices of all tables in all databases where the |
| 3095 | ** indices use the collating sequence pColl. If pColl==0 then recompute |
| 3096 | ** all indices everywhere. |
| 3097 | */ |
| 3098 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3099 | static void reindexDatabases(Parse *pParse, char const *zColl){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3100 | Db *pDb; /* A single database */ |
| 3101 | int iDb; /* The database index number */ |
| 3102 | sqlite3 *db = pParse->db; /* The database connection */ |
| 3103 | HashElem *k; /* For looping over tables in pDb */ |
| 3104 | Table *pTab; /* A table in the database */ |
| 3105 | |
| 3106 | for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ |
| 3107 | if( pDb==0 ) continue; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3108 | for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3109 | pTab = (Table*)sqliteHashData(k); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3110 | reindexTable(pParse, pTab, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3111 | } |
| 3112 | } |
| 3113 | } |
| 3114 | #endif |
| 3115 | |
| 3116 | /* |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 3117 | ** Generate code for the REINDEX command. |
| 3118 | ** |
| 3119 | ** REINDEX -- 1 |
| 3120 | ** REINDEX <collation> -- 2 |
| 3121 | ** REINDEX ?<database>.?<tablename> -- 3 |
| 3122 | ** REINDEX ?<database>.?<indexname> -- 4 |
| 3123 | ** |
| 3124 | ** Form 1 causes all indices in all attached databases to be rebuilt. |
| 3125 | ** Form 2 rebuilds all indices in all databases that use the named |
| 3126 | ** collating function. Forms 3 and 4 rebuild the named index or all |
| 3127 | ** indices associated with the named table. |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3128 | */ |
| 3129 | #ifndef SQLITE_OMIT_REINDEX |
| 3130 | void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ |
| 3131 | CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ |
| 3132 | char *z; /* Name of a table or index */ |
| 3133 | const char *zDb; /* Name of the database */ |
| 3134 | Table *pTab; /* A table in the database */ |
| 3135 | Index *pIndex; /* An index associated with pTab */ |
| 3136 | int iDb; /* The database index number */ |
| 3137 | sqlite3 *db = pParse->db; /* The database connection */ |
| 3138 | Token *pObjName; /* Name of the table or index to be reindexed */ |
| 3139 | |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 +0000 | [diff] [blame] | 3140 | /* Read the database schema. If an error occurs, leave an error message |
| 3141 | ** and code in pParse and return NULL. */ |
| 3142 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
danielk1977 | e63739a | 2005-01-27 00:33:37 +0000 | [diff] [blame] | 3143 | return; |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
drh | e497f00 | 2004-11-07 13:01:49 +0000 | [diff] [blame] | 3146 | if( pName1==0 || pName1->z==0 ){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3147 | reindexDatabases(pParse, 0); |
| 3148 | return; |
drh | e497f00 | 2004-11-07 13:01:49 +0000 | [diff] [blame] | 3149 | }else if( pName2==0 || pName2->z==0 ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3150 | assert( pName1->z ); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 3151 | pColl = sqlite3FindCollSeq(db, ENC(db), (char*)pName1->z, pName1->n, 0); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3152 | if( pColl ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3153 | char *z = sqlite3StrNDup(pName1->z, pName1->n); |
| 3154 | if( z ){ |
| 3155 | reindexDatabases(pParse, z); |
| 3156 | sqliteFree(z); |
| 3157 | } |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 3158 | return; |
| 3159 | } |
| 3160 | } |
| 3161 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
| 3162 | if( iDb<0 ) return; |
| 3163 | z = sqlite3NameFromToken(pObjName); |
| 3164 | zDb = db->aDb[iDb].zName; |
| 3165 | pTab = sqlite3FindTable(db, z, zDb); |
| 3166 | if( pTab ){ |
| 3167 | reindexTable(pParse, pTab, 0); |
| 3168 | sqliteFree(z); |
| 3169 | return; |
| 3170 | } |
| 3171 | pIndex = sqlite3FindIndex(db, z, zDb); |
| 3172 | sqliteFree(z); |
| 3173 | if( pIndex ){ |
| 3174 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 3175 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 3176 | return; |
| 3177 | } |
| 3178 | sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
| 3179 | } |
| 3180 | #endif |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3181 | |
| 3182 | /* |
| 3183 | ** Return a dynamicly allocated KeyInfo structure that can be used |
| 3184 | ** with OP_OpenRead or OP_OpenWrite to access database index pIdx. |
| 3185 | ** |
| 3186 | ** If successful, a pointer to the new structure is returned. In this case |
| 3187 | ** the caller is responsible for calling sqliteFree() on the returned |
| 3188 | ** pointer. If an error occurs (out of memory or missing collation |
| 3189 | ** sequence), NULL is returned and the state of pParse updated to reflect |
| 3190 | ** the error. |
| 3191 | */ |
| 3192 | KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){ |
| 3193 | int i; |
| 3194 | int nCol = pIdx->nColumn; |
| 3195 | int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol; |
| 3196 | KeyInfo *pKey = (KeyInfo *)sqliteMalloc(nBytes); |
| 3197 | |
| 3198 | if( pKey ){ |
| 3199 | pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]); |
| 3200 | assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) ); |
| 3201 | for(i=0; i<nCol; i++){ |
| 3202 | char *zColl = pIdx->azColl[i]; |
| 3203 | assert( zColl ); |
| 3204 | pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl, -1); |
| 3205 | pKey->aSortOrder[i] = pIdx->aSortOrder[i]; |
| 3206 | } |
| 3207 | pKey->nField = nCol; |
| 3208 | } |
| 3209 | |
| 3210 | if( pParse->nErr ){ |
| 3211 | sqliteFree(pKey); |
| 3212 | pKey = 0; |
| 3213 | } |
| 3214 | return pKey; |
| 3215 | } |