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