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