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