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