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