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