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