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