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