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