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