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