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 | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 21 | ** COPY |
| 22 | ** VACUUM |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 23 | ** BEGIN TRANSACTION |
| 24 | ** COMMIT |
| 25 | ** ROLLBACK |
| 26 | ** PRAGMA |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 27 | ** |
drh | df68f6b | 2002-09-21 15:57:57 +0000 | [diff] [blame^] | 28 | ** $Id: build.c,v 1.113 2002/09/21 15:57:57 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 29 | */ |
| 30 | #include "sqliteInt.h" |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 31 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 32 | |
| 33 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 34 | ** This routine is called when a new SQL statement is beginning to |
| 35 | ** be parsed. Check to see if the schema for the database needs |
| 36 | ** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables. |
| 37 | ** If it does, then read it. |
| 38 | */ |
| 39 | void sqliteBeginParse(Parse *pParse, int explainFlag){ |
| 40 | sqlite *db = pParse->db; |
| 41 | pParse->explain = explainFlag; |
| 42 | if((db->flags & SQLITE_Initialized)==0 && pParse->initFlag==0 ){ |
| 43 | int rc = sqliteInit(db, &pParse->zErrMsg); |
| 44 | if( rc!=SQLITE_OK ){ |
| 45 | pParse->rc = rc; |
| 46 | pParse->nErr++; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 52 | ** This routine is called after a single SQL statement has been |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 53 | ** parsed and we want to execute the VDBE code to implement |
| 54 | ** that statement. Prior action routines should have already |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 55 | ** constructed VDBE code to do the work of the SQL statement. |
| 56 | ** This routine just has to execute the VDBE code. |
| 57 | ** |
| 58 | ** Note that if an error occurred, it might be the case that |
| 59 | ** no VDBE code was generated. |
| 60 | */ |
| 61 | void sqliteExec(Parse *pParse){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 62 | int rc = SQLITE_OK; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 63 | sqlite *db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 64 | if( sqlite_malloc_failed ) return; |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 65 | if( pParse->pVdbe && pParse->nErr==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 66 | if( pParse->explain ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 67 | rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg, |
| 68 | &pParse->zErrMsg); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 69 | db->next_cookie = db->schema_cookie; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 70 | }else{ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 71 | FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 72 | sqliteVdbeTrace(pParse->pVdbe, trace); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 73 | rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg, |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 74 | &pParse->zErrMsg, db->pBusyArg, |
| 75 | db->xBusyCallback); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 76 | if( rc ) pParse->nErr++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 77 | } |
| 78 | sqliteVdbeDelete(pParse->pVdbe); |
| 79 | pParse->pVdbe = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 80 | pParse->colNamesSet = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 81 | pParse->rc = rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 82 | pParse->schemaVerified = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 87 | ** Locate the in-memory structure that describes |
| 88 | ** a particular database table given the name |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 89 | ** of that table. Return NULL if not found. |
| 90 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 91 | Table *sqliteFindTable(sqlite *db, const char *zName){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 92 | Table *p; |
| 93 | p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 94 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 98 | ** Locate the in-memory structure that describes |
| 99 | ** a particular index given the name of that index. |
| 100 | ** Return NULL if not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 101 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 102 | Index *sqliteFindIndex(sqlite *db, const char *zName){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 103 | Index *p; |
| 104 | p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 105 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
| 109 | ** Remove the given index from the index hash table, and free |
| 110 | ** its memory structures. |
| 111 | ** |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 112 | ** The index is removed from the database hash tables but |
| 113 | ** it is not unlinked from the Table that it indexes. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 114 | ** Unlinking from the Table must be done by the calling function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 115 | */ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 116 | static void sqliteDeleteIndex(sqlite *db, Index *p){ |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 117 | Index *pOld; |
| 118 | assert( db!=0 && p->zName!=0 ); |
| 119 | pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0); |
| 120 | if( pOld!=0 && pOld!=p ){ |
| 121 | sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 122 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 123 | sqliteFree(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 127 | ** Unlink the given index from its table, then remove |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 128 | ** the index from the index hash table and free its memory |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 129 | ** structures. |
| 130 | */ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 131 | void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 132 | if( pIndex->pTable->pIndex==pIndex ){ |
| 133 | pIndex->pTable->pIndex = pIndex->pNext; |
| 134 | }else{ |
| 135 | Index *p; |
| 136 | for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
| 137 | if( p && p->pNext==pIndex ){ |
| 138 | p->pNext = pIndex->pNext; |
| 139 | } |
| 140 | } |
| 141 | sqliteDeleteIndex(db, pIndex); |
| 142 | } |
| 143 | |
| 144 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 145 | ** Erase all schema information from the in-memory hash tables of |
| 146 | ** database connection. This routine is called to reclaim memory |
| 147 | ** before the connection closes. It is also called during a rollback |
| 148 | ** if there were schema changes during the transaction. |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 149 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 150 | void sqliteResetInternalSchema(sqlite *db){ |
| 151 | HashElem *pElem; |
| 152 | Hash temp1; |
| 153 | Hash temp2; |
| 154 | |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 155 | sqliteHashClear(&db->aFKey); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 156 | temp1 = db->tblHash; |
| 157 | temp2 = db->trigHash; |
| 158 | sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0); |
| 159 | sqliteHashClear(&db->idxHash); |
| 160 | for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ |
| 161 | Trigger *pTrigger = sqliteHashData(pElem); |
| 162 | sqliteDeleteTrigger(pTrigger); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 163 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 164 | sqliteHashClear(&temp2); |
| 165 | sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0); |
| 166 | for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ |
| 167 | Table *pTab = sqliteHashData(pElem); |
| 168 | sqliteDeleteTable(db, pTab); |
| 169 | } |
| 170 | sqliteHashClear(&temp1); |
| 171 | db->flags &= ~(SQLITE_Initialized|SQLITE_InternChanges); |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | ** This routine is called whenever a rollback occurs. If there were |
| 176 | ** schema changes during the transaction, then we have to reset the |
| 177 | ** internal hash tables and reload them from disk. |
| 178 | */ |
| 179 | void sqliteRollbackInternalChanges(sqlite *db){ |
| 180 | if( db->flags & SQLITE_InternChanges ){ |
| 181 | sqliteResetInternalSchema(db); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | ** This routine is called when a commit occurs. |
| 187 | */ |
| 188 | void sqliteCommitInternalChanges(sqlite *db){ |
| 189 | db->schema_cookie = db->next_cookie; |
| 190 | db->flags &= ~SQLITE_InternChanges; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 194 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 195 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 196 | ** |
| 197 | ** This routine just deletes the data structure. It does not unlink |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 198 | ** the table data structure from the hash table. Nor does it remove |
| 199 | ** foreign keys from the sqlite.aFKey hash table. But it does destroy |
| 200 | ** memory structures of the indices and foreign keys associated with |
| 201 | ** the table. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 202 | ** |
| 203 | ** Indices associated with the table are unlinked from the "db" |
| 204 | ** data structure if db!=NULL. If db==NULL, indices attached to |
| 205 | ** the table are deleted, but it is assumed they have already been |
| 206 | ** unlinked. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 207 | */ |
| 208 | void sqliteDeleteTable(sqlite *db, Table *pTable){ |
| 209 | int i; |
| 210 | Index *pIndex, *pNext; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 211 | FKey *pFKey, *pNextFKey; |
| 212 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 213 | if( pTable==0 ) return; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 214 | |
| 215 | /* Delete all indices associated with this table |
| 216 | */ |
| 217 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 218 | pNext = pIndex->pNext; |
| 219 | sqliteDeleteIndex(db, pIndex); |
| 220 | } |
| 221 | |
| 222 | /* Delete all foreign keys associated with this table. The keys |
| 223 | ** should have already been unlinked from the db->aFKey hash table |
| 224 | */ |
| 225 | for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){ |
| 226 | pNextFKey = pFKey->pNextFrom; |
| 227 | assert( sqliteHashFind(&db->aFKey,pFKey->zTo,strlen(pFKey->zTo)+1)!=pFKey ); |
| 228 | sqliteFree(pFKey); |
| 229 | } |
| 230 | |
| 231 | /* Delete the Table structure itself. |
| 232 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 233 | for(i=0; i<pTable->nCol; i++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 234 | sqliteFree(pTable->aCol[i].zName); |
| 235 | sqliteFree(pTable->aCol[i].zDflt); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 236 | sqliteFree(pTable->aCol[i].zType); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 237 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 238 | sqliteFree(pTable->zName); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 239 | sqliteFree(pTable->aCol); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 240 | sqliteSelectDelete(pTable->pSelect); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 241 | sqliteFree(pTable); |
| 242 | } |
| 243 | |
| 244 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 245 | ** Unlink the given table from the hash tables and the delete the |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 246 | ** table structure with all its indices and foreign keys. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 247 | */ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 248 | static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){ |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 249 | Table *pOld; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 250 | FKey *pF1, *pF2; |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 251 | assert( db!=0 ); |
| 252 | pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0); |
| 253 | assert( pOld==0 || pOld==p ); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 254 | for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){ |
| 255 | int nTo = strlen(pF1->zTo) + 1; |
| 256 | pF2 = sqliteHashFind(&db->aFKey, pF1->zTo, nTo); |
| 257 | if( pF2==pF1 ){ |
| 258 | sqliteHashInsert(&db->aFKey, pF1->zTo, nTo, pF1->pNextTo); |
| 259 | }else{ |
| 260 | while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; } |
| 261 | if( pF2 ){ |
| 262 | pF2->pNextTo = pF1->pNextTo; |
| 263 | } |
| 264 | } |
| 265 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 266 | sqliteDeleteTable(db, p); |
| 267 | } |
| 268 | |
| 269 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 270 | ** Construct the name of a user table or index from a token. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 271 | ** |
| 272 | ** Space to hold the name is obtained from sqliteMalloc() and must |
| 273 | ** be freed by the calling function. |
| 274 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 275 | char *sqliteTableNameFromToken(Token *pName){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 276 | char *zName = sqliteStrNDup(pName->z, pName->n); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 277 | sqliteDequote(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 278 | return zName; |
| 279 | } |
| 280 | |
| 281 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 282 | ** Generate code to open the appropriate master table. The table |
| 283 | ** opened will be SQLITE_MASTER for persistent tables and |
| 284 | ** SQLITE_TEMP_MASTER for temporary tables. The table is opened |
| 285 | ** on cursor 0. |
| 286 | */ |
| 287 | void sqliteOpenMasterTable(Vdbe *v, int isTemp){ |
| 288 | if( isTemp ){ |
| 289 | sqliteVdbeAddOp(v, OP_OpenWrAux, 0, 2); |
| 290 | sqliteVdbeChangeP3(v, -1, TEMP_MASTER_NAME, P3_STATIC); |
| 291 | }else{ |
| 292 | sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2); |
| 293 | sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 298 | ** Begin constructing a new table representation in memory. This is |
| 299 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 300 | ** to a CREATE TABLE statement. In particular, this routine is called |
| 301 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 302 | ** pStart token is the CREATE and pName is the table name. The isTemp |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 303 | ** flag is true if the table should be stored in the auxiliary database |
| 304 | ** file instead of in the main database file. This is normally the case |
| 305 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 306 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 307 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 308 | ** The new table record is initialized and put in pParse->pNewTable. |
| 309 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 310 | ** routines will be called to add more information to this record. |
| 311 | ** At the end of the CREATE TABLE statement, the sqliteEndTable() routine |
| 312 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 313 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 314 | void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 315 | Table *pTable; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 316 | Index *pIdx; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 317 | char *zName; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 318 | sqlite *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 319 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 320 | |
| 321 | pParse->sFirstToken = *pStart; |
| 322 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 323 | if( zName==0 ) return; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 324 | |
| 325 | /* Before trying to create a temporary table, make sure the Btree for |
| 326 | ** holding temporary tables is open. |
| 327 | */ |
| 328 | if( isTemp && db->pBeTemp==0 ){ |
| 329 | int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp); |
| 330 | if( rc!=SQLITE_OK ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 331 | sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database " |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 332 | "file for storing temporary tables", 0); |
| 333 | pParse->nErr++; |
| 334 | return; |
| 335 | } |
| 336 | if( db->flags & SQLITE_InTrans ){ |
| 337 | rc = sqliteBtreeBeginTrans(db->pBeTemp); |
| 338 | if( rc!=SQLITE_OK ){ |
| 339 | sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on " |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 340 | "the temporary database file", 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 341 | pParse->nErr++; |
| 342 | return; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /* Make sure the new table name does not collide with an existing |
| 348 | ** index or table name. Issue an error message if it does. |
| 349 | ** |
| 350 | ** If we are re-reading the sqlite_master table because of a schema |
| 351 | ** change and a new permanent table is found whose name collides with |
| 352 | ** an existing temporary table, then ignore the new permanent table. |
| 353 | ** We will continue parsing, but the pParse->nameClash flag will be set |
| 354 | ** so we will know to discard the table record once parsing has finished. |
| 355 | */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 356 | pTable = sqliteFindTable(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 357 | if( pTable!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 358 | if( pTable->isTemp && pParse->initFlag ){ |
| 359 | pParse->nameClash = 1; |
| 360 | }else{ |
| 361 | sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n, |
| 362 | " already exists", 0, 0); |
| 363 | sqliteFree(zName); |
| 364 | pParse->nErr++; |
| 365 | return; |
| 366 | } |
| 367 | }else{ |
| 368 | pParse->nameClash = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 369 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 370 | if( (pIdx = sqliteFindIndex(db, zName))!=0 && |
| 371 | (!pIdx->pTable->isTemp || !pParse->initFlag) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 372 | sqliteSetString(&pParse->zErrMsg, "there is already an index named ", |
| 373 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 374 | sqliteFree(zName); |
| 375 | pParse->nErr++; |
| 376 | return; |
| 377 | } |
| 378 | pTable = sqliteMalloc( sizeof(Table) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 379 | if( pTable==0 ){ |
| 380 | sqliteFree(zName); |
| 381 | return; |
| 382 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 383 | pTable->zName = zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 384 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 385 | pTable->aCol = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 386 | pTable->iPKey = -1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 387 | pTable->pIndex = 0; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 388 | pTable->isTemp = isTemp; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 389 | if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 390 | pParse->pNewTable = pTable; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 391 | |
| 392 | /* Begin generating the code that will insert the table record into |
| 393 | ** the SQLITE_MASTER table. Note in particular that we must go ahead |
| 394 | ** and allocate the record number for the table entry now. Before any |
| 395 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
| 396 | ** indices to be created and the table record must come before the |
| 397 | ** indices. Hence, the record number for the table must be allocated |
| 398 | ** now. |
| 399 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 400 | if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 401 | sqliteBeginWriteOperation(pParse, 0, isTemp); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 402 | if( !isTemp ){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 403 | sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0); |
| 404 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 1); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 405 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 406 | sqliteOpenMasterTable(v, isTemp); |
| 407 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 408 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 409 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 410 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 411 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
| 415 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 416 | ** |
| 417 | ** The parser calls this routine once for each column declaration |
| 418 | ** in a CREATE TABLE statement. sqliteStartTable() gets called |
| 419 | ** first to get things going. Then this routine is called for each |
| 420 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 421 | */ |
| 422 | void sqliteAddColumn(Parse *pParse, Token *pName){ |
| 423 | Table *p; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 424 | int i; |
| 425 | char *z = 0; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 426 | Column *pCol; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 427 | if( (p = pParse->pNewTable)==0 ) return; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 428 | sqliteSetNString(&z, pName->z, pName->n, 0); |
| 429 | if( z==0 ) return; |
| 430 | sqliteDequote(z); |
| 431 | for(i=0; i<p->nCol; i++){ |
| 432 | if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){ |
| 433 | sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0); |
| 434 | pParse->nErr++; |
| 435 | sqliteFree(z); |
| 436 | return; |
| 437 | } |
| 438 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 439 | if( (p->nCol & 0x7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 440 | Column *aNew; |
| 441 | aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
| 442 | if( aNew==0 ) return; |
| 443 | p->aCol = aNew; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 444 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 445 | pCol = &p->aCol[p->nCol]; |
| 446 | memset(pCol, 0, sizeof(p->aCol[0])); |
| 447 | pCol->zName = z; |
| 448 | pCol->sortOrder = SQLITE_SO_NUM; |
| 449 | p->nCol++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 453 | ** This routine is called by the parser while in the middle of |
| 454 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 455 | ** been seen on a column. This routine sets the notNull flag on |
| 456 | ** the column currently under construction. |
| 457 | */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 458 | void sqliteAddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 459 | Table *p; |
| 460 | int i; |
| 461 | if( (p = pParse->pNewTable)==0 ) return; |
| 462 | i = p->nCol-1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 463 | if( i>=0 ) p->aCol[i].notNull = onError; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* |
| 467 | ** This routine is called by the parser while in the middle of |
| 468 | ** parsing a CREATE TABLE statement. The pFirst token is the first |
| 469 | ** token in the sequence of tokens that describe the type of the |
| 470 | ** column currently under construction. pLast is the last token |
| 471 | ** in the sequence. Use this information to construct a string |
| 472 | ** that contains the typename of the column and store that string |
| 473 | ** in zType. |
| 474 | */ |
| 475 | void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){ |
| 476 | Table *p; |
| 477 | int i, j; |
| 478 | int n; |
| 479 | char *z, **pz; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 480 | Column *pCol; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 481 | if( (p = pParse->pNewTable)==0 ) return; |
| 482 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 483 | if( i<0 ) return; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 484 | pCol = &p->aCol[i]; |
| 485 | pz = &pCol->zType; |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 486 | n = pLast->n + Addr(pLast->z) - Addr(pFirst->z); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 487 | sqliteSetNString(pz, pFirst->z, n, 0); |
| 488 | z = *pz; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 489 | if( z==0 ) return; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 490 | for(i=j=0; z[i]; i++){ |
| 491 | int c = z[i]; |
| 492 | if( isspace(c) ) continue; |
| 493 | z[j++] = c; |
| 494 | } |
| 495 | z[j] = 0; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 496 | pCol->sortOrder = SQLITE_SO_NUM; |
drh | 3d037a9 | 2002-08-15 01:26:09 +0000 | [diff] [blame] | 497 | if( pParse->db->file_format>=4 ){ |
| 498 | for(i=0; z[i]; i++){ |
| 499 | switch( z[i] ){ |
| 500 | case 'b': |
| 501 | case 'B': { |
| 502 | if( sqliteStrNICmp(&z[i],"blob",4)==0 ){ |
| 503 | pCol->sortOrder = SQLITE_SO_TEXT; |
| 504 | return; |
| 505 | } |
| 506 | break; |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 507 | } |
drh | 3d037a9 | 2002-08-15 01:26:09 +0000 | [diff] [blame] | 508 | case 'c': |
| 509 | case 'C': { |
| 510 | if( sqliteStrNICmp(&z[i],"char",4)==0 || |
| 511 | sqliteStrNICmp(&z[i],"clob",4)==0 ){ |
| 512 | pCol->sortOrder = SQLITE_SO_TEXT; |
| 513 | return; |
| 514 | } |
| 515 | break; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 516 | } |
drh | 3d037a9 | 2002-08-15 01:26:09 +0000 | [diff] [blame] | 517 | case 'x': |
| 518 | case 'X': { |
| 519 | if( i>=2 && sqliteStrNICmp(&z[i-2],"text",4)==0 ){ |
| 520 | pCol->sortOrder = SQLITE_SO_TEXT; |
| 521 | return; |
| 522 | } |
| 523 | break; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 524 | } |
drh | 3d037a9 | 2002-08-15 01:26:09 +0000 | [diff] [blame] | 525 | default: { |
| 526 | break; |
| 527 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 534 | ** The given token is the default value for the last column added to |
| 535 | ** the table currently under construction. If "minusFlag" is true, it |
| 536 | ** means the value token was preceded by a minus sign. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 537 | ** |
| 538 | ** This routine is called by the parser while in the middle of |
| 539 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 540 | */ |
| 541 | void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ |
| 542 | Table *p; |
| 543 | int i; |
| 544 | char **pz; |
| 545 | if( (p = pParse->pNewTable)==0 ) return; |
| 546 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 547 | if( i<0 ) return; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 548 | pz = &p->aCol[i].zDflt; |
| 549 | if( minusFlag ){ |
| 550 | sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0); |
| 551 | }else{ |
| 552 | sqliteSetNString(pz, pVal->z, pVal->n, 0); |
| 553 | } |
| 554 | sqliteDequote(*pz); |
| 555 | } |
| 556 | |
| 557 | /* |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 558 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
| 559 | ** of columns that form the primary key. If pList is NULL, then the |
| 560 | ** most recently added column of the table is the primary key. |
| 561 | ** |
| 562 | ** A table can have at most one primary key. If the table already has |
| 563 | ** a primary key (and this is the second primary key) then create an |
| 564 | ** error. |
| 565 | ** |
| 566 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
| 567 | ** then we will try to use that column as the row id. (Exception: |
| 568 | ** For backwards compatibility with older databases, do not do this |
| 569 | ** if the file format version number is less than 1.) Set the Table.iPKey |
| 570 | ** field of the table under construction to be the index of the |
| 571 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 572 | ** no INTEGER PRIMARY KEY. |
| 573 | ** |
| 574 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 575 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 576 | */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 577 | void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 578 | Table *pTab = pParse->pNewTable; |
| 579 | char *zType = 0; |
| 580 | int iCol = -1; |
| 581 | if( pTab==0 ) return; |
| 582 | if( pTab->hasPrimKey ){ |
| 583 | sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName, |
| 584 | "\" has more than one primary key", 0); |
| 585 | pParse->nErr++; |
| 586 | return; |
| 587 | } |
| 588 | pTab->hasPrimKey = 1; |
| 589 | if( pList==0 ){ |
| 590 | iCol = pTab->nCol - 1; |
| 591 | }else if( pList->nId==1 ){ |
| 592 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 593 | if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break; |
| 594 | } |
| 595 | } |
| 596 | if( iCol>=0 && iCol<pTab->nCol ){ |
| 597 | zType = pTab->aCol[iCol].zType; |
| 598 | } |
| 599 | if( pParse->db->file_format>=1 && |
| 600 | zType && sqliteStrICmp(zType, "INTEGER")==0 ){ |
| 601 | pTab->iPKey = iCol; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 602 | pTab->keyConf = onError; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 603 | }else{ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 604 | sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
| 608 | /* |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 609 | ** Return the appropriate collating type given the collation type token. |
| 610 | ** Report an error if the type is undefined. |
| 611 | */ |
| 612 | int sqliteCollateType(Parse *pParse, Token *pType){ |
| 613 | if( pType==0 ) return SQLITE_SO_UNK; |
| 614 | if( pType->n==4 && sqliteStrNICmp(pType->z, "text", 4)==0 ){ |
| 615 | return SQLITE_SO_TEXT; |
| 616 | } |
| 617 | if( pType->n==7 && sqliteStrNICmp(pType->z, "numeric", 7)==0 ){ |
| 618 | return SQLITE_SO_NUM; |
| 619 | } |
| 620 | sqliteSetNString(&pParse->zErrMsg, "unknown collating type: ", -1, |
| 621 | pType->z, pType->n, 0); |
| 622 | pParse->nErr++; |
| 623 | return SQLITE_SO_UNK; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | ** This routine is called by the parser while in the middle of |
| 628 | ** parsing a CREATE TABLE statement. A "COLLATE" clause has |
| 629 | ** been seen on a column. This routine sets the Column.sortOrder on |
| 630 | ** the column currently under construction. |
| 631 | */ |
| 632 | void sqliteAddCollateType(Parse *pParse, int collType){ |
| 633 | Table *p; |
| 634 | int i; |
| 635 | if( (p = pParse->pNewTable)==0 ) return; |
| 636 | i = p->nCol-1; |
| 637 | if( i>=0 ) p->aCol[i].sortOrder = collType; |
| 638 | } |
| 639 | |
| 640 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 641 | ** Come up with a new random value for the schema cookie. Make sure |
| 642 | ** the new value is different from the old. |
| 643 | ** |
| 644 | ** The schema cookie is used to determine when the schema for the |
| 645 | ** database changes. After each schema change, the cookie value |
| 646 | ** changes. When a process first reads the schema it records the |
| 647 | ** cookie. Thereafter, whenever it goes to access the database, |
| 648 | ** it checks the cookie to make sure the schema has not changed |
| 649 | ** since it was last read. |
| 650 | ** |
| 651 | ** This plan is not completely bullet-proof. It is possible for |
| 652 | ** the schema to change multiple times and for the cookie to be |
| 653 | ** set back to prior value. But schema changes are infrequent |
| 654 | ** and the probability of hitting the same cookie value is only |
| 655 | ** 1 chance in 2^32. So we're safe enough. |
| 656 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 657 | void sqliteChangeCookie(sqlite *db, Vdbe *v){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 658 | if( db->next_cookie==db->schema_cookie ){ |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 659 | db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 660 | db->flags |= SQLITE_InternChanges; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 661 | sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0); |
| 662 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | /* |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 667 | ** Measure the number of characters needed to output the given |
| 668 | ** identifier. The number returned includes any quotes used |
| 669 | ** but does not include the null terminator. |
| 670 | */ |
| 671 | static int identLength(const char *z){ |
| 672 | int n; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 673 | int needQuote = 0; |
| 674 | for(n=0; *z; n++, z++){ |
| 675 | if( *z=='\'' ){ n++; needQuote=1; } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 676 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 677 | return n + needQuote*2; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* |
| 681 | ** Write an identifier onto the end of the given string. Add |
| 682 | ** quote characters as needed. |
| 683 | */ |
| 684 | static void identPut(char *z, int *pIdx, char *zIdent){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 685 | int i, j, needQuote; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 686 | i = *pIdx; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 687 | for(j=0; zIdent[j]; j++){ |
| 688 | if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
| 689 | } |
| 690 | needQuote = zIdent[j]!=0 || isdigit(zIdent[0]) |
| 691 | || sqliteKeywordCode(zIdent, j)!=TK_ID; |
| 692 | if( needQuote ) z[i++] = '\''; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 693 | for(j=0; zIdent[j]; j++){ |
| 694 | z[i++] = zIdent[j]; |
| 695 | if( zIdent[j]=='\'' ) z[i++] = '\''; |
| 696 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 697 | if( needQuote ) z[i++] = '\''; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 698 | z[i] = 0; |
| 699 | *pIdx = i; |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | ** Generate a CREATE TABLE statement appropriate for the given |
| 704 | ** table. Memory to hold the text of the statement is obtained |
| 705 | ** from sqliteMalloc() and must be freed by the calling function. |
| 706 | */ |
| 707 | static char *createTableStmt(Table *p){ |
| 708 | int i, k, n; |
| 709 | char *zStmt; |
| 710 | char *zSep, *zSep2, *zEnd; |
| 711 | n = 0; |
| 712 | for(i=0; i<p->nCol; i++){ |
| 713 | n += identLength(p->aCol[i].zName); |
| 714 | } |
| 715 | n += identLength(p->zName); |
| 716 | if( n<40 ){ |
| 717 | zSep = ""; |
| 718 | zSep2 = ","; |
| 719 | zEnd = ")"; |
| 720 | }else{ |
| 721 | zSep = "\n "; |
| 722 | zSep2 = ",\n "; |
| 723 | zEnd = "\n)"; |
| 724 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 725 | n += 35 + 6*p->nCol; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 726 | zStmt = sqliteMalloc( n ); |
| 727 | if( zStmt==0 ) return 0; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 728 | strcpy(zStmt, p->isTemp ? "CREATE TEMP TABLE " : "CREATE TABLE "); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 729 | k = strlen(zStmt); |
| 730 | identPut(zStmt, &k, p->zName); |
| 731 | zStmt[k++] = '('; |
| 732 | for(i=0; i<p->nCol; i++){ |
| 733 | strcpy(&zStmt[k], zSep); |
| 734 | k += strlen(&zStmt[k]); |
| 735 | zSep = zSep2; |
| 736 | identPut(zStmt, &k, p->aCol[i].zName); |
| 737 | } |
| 738 | strcpy(&zStmt[k], zEnd); |
| 739 | return zStmt; |
| 740 | } |
| 741 | |
| 742 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 743 | ** This routine is called to report the final ")" that terminates |
| 744 | ** a CREATE TABLE statement. |
| 745 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 746 | ** The table structure that other action routines have been building |
| 747 | ** is added to the internal hash tables, assuming no errors have |
| 748 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 749 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 750 | ** An entry for the table is made in the master table on disk, |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 751 | ** unless this is a temporary table or initFlag==1. When initFlag==1, |
| 752 | ** it means we are reading the sqlite_master table because we just |
| 753 | ** connected to the database or because the sqlite_master table has |
| 754 | ** recently changes, so the entry for this table already exists in |
| 755 | ** the sqlite_master table. We do not want to create it again. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 756 | ** |
| 757 | ** If the pSelect argument is not NULL, it means that this routine |
| 758 | ** was called to create a table generated from a |
| 759 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
| 760 | ** the new table will match the result set of the SELECT. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 761 | */ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 762 | void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 763 | Table *p; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 764 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 765 | |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 766 | if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 767 | p = pParse->pNewTable; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 768 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 769 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 770 | /* Add the table to the in-memory representation of the database. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 771 | */ |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 772 | assert( pParse->nameClash==0 || pParse->initFlag==1 ); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 773 | if( pParse->explain==0 && pParse->nameClash==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 774 | Table *pOld; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 775 | FKey *pFKey; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 776 | pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p); |
| 777 | if( pOld ){ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 778 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 779 | return; |
| 780 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 781 | for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 782 | int nTo = strlen(pFKey->zTo) + 1; |
| 783 | pFKey->pNextTo = sqliteHashFind(&db->aFKey, pFKey->zTo, nTo); |
| 784 | sqliteHashInsert(&db->aFKey, pFKey->zTo, nTo, pFKey); |
| 785 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 786 | pParse->pNewTable = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 787 | db->nTable++; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 788 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 789 | } |
| 790 | |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 791 | /* If the table is generated from a SELECT, then construct the |
| 792 | ** list of columns and the text of the table. |
| 793 | */ |
| 794 | if( pSelect ){ |
| 795 | Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect); |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 796 | if( pSelTab==0 ) return; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 797 | assert( p->aCol==0 ); |
| 798 | p->nCol = pSelTab->nCol; |
| 799 | p->aCol = pSelTab->aCol; |
| 800 | pSelTab->nCol = 0; |
| 801 | pSelTab->aCol = 0; |
| 802 | sqliteDeleteTable(0, pSelTab); |
| 803 | } |
| 804 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 805 | /* If the initFlag is 1 it means we are reading the SQL off the |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 806 | ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
| 807 | ** So do not write to the disk again. Extract the root page number |
| 808 | ** for the table from the pParse->newTnum field. (The page number |
| 809 | ** should have been put there by the sqliteOpenCb routine.) |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 810 | */ |
| 811 | if( pParse->initFlag ){ |
| 812 | p->tnum = pParse->newTnum; |
| 813 | } |
| 814 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 815 | /* If not initializing, then create a record for the new table |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 816 | ** in the SQLITE_MASTER table of the database. The record number |
| 817 | ** for the new table entry should already be on the stack. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 818 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 819 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
| 820 | ** file instead of into the main database file. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 821 | */ |
| 822 | if( !pParse->initFlag ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 823 | int n; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 824 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 825 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 826 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 827 | if( v==0 ) return; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 828 | if( p->pSelect==0 ){ |
| 829 | /* A regular table */ |
| 830 | sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp); |
| 831 | sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER); |
| 832 | }else{ |
| 833 | /* A view */ |
| 834 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 835 | } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 836 | p->tnum = 0; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 837 | sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
| 838 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 839 | if( p->pSelect==0 ){ |
| 840 | sqliteVdbeChangeP3(v, -1, "table", P3_STATIC); |
| 841 | }else{ |
| 842 | sqliteVdbeChangeP3(v, -1, "view", P3_STATIC); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 843 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 844 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 845 | sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC); |
| 846 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 847 | sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC); |
| 848 | sqliteVdbeAddOp(v, OP_Dup, 4, 0); |
| 849 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 850 | if( pSelect ){ |
| 851 | char *z = createTableStmt(p); |
| 852 | n = z ? strlen(z) : 0; |
| 853 | sqliteVdbeChangeP3(v, -1, z, n); |
| 854 | sqliteFree(z); |
| 855 | }else{ |
| 856 | assert( pEnd!=0 ); |
| 857 | n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1; |
| 858 | sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n); |
| 859 | } |
| 860 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0); |
| 861 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
| 862 | if( !p->isTemp ){ |
| 863 | sqliteChangeCookie(db, v); |
| 864 | } |
| 865 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 866 | if( pSelect ){ |
| 867 | int op = p->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
| 868 | sqliteVdbeAddOp(v, op, 1, 0); |
| 869 | pParse->nTab = 2; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 870 | sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 871 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 872 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 877 | ** The parser calls this routine in order to create a new VIEW |
| 878 | */ |
| 879 | void sqliteCreateView( |
| 880 | Parse *pParse, /* The parsing context */ |
| 881 | Token *pBegin, /* The CREATE token that begins the statement */ |
| 882 | Token *pName, /* The token that holds the name of the view */ |
drh | 6276c1c | 2002-07-08 22:03:32 +0000 | [diff] [blame] | 883 | Select *pSelect, /* A SELECT statement that will become the new view */ |
| 884 | int isTemp /* TRUE for a TEMPORARY view */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 885 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 886 | Table *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 887 | int n; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 888 | const char *z; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 889 | Token sEnd; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 890 | |
drh | 6276c1c | 2002-07-08 22:03:32 +0000 | [diff] [blame] | 891 | sqliteStartTable(pParse, pBegin, pName, isTemp); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 892 | p = pParse->pNewTable; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 893 | if( p==0 ){ |
| 894 | sqliteSelectDelete(pSelect); |
| 895 | return; |
| 896 | } |
drh | 0f18b45 | 2002-05-08 21:30:15 +0000 | [diff] [blame] | 897 | /* Ignore ORDER BY clauses on a SELECT */ |
| 898 | if( pSelect->pOrderBy ){ |
| 899 | sqliteExprListDelete(pSelect->pOrderBy); |
| 900 | pSelect->pOrderBy = 0; |
| 901 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 902 | /* Make a copy of the entire SELECT statement that defines the view. |
| 903 | ** This will force all the Expr.token.z values to be dynamically |
| 904 | ** allocated rather than point to the input string - which means that |
| 905 | ** they will persist after the current sqlite_exec() call returns. |
| 906 | */ |
| 907 | p->pSelect = sqliteSelectDup(pSelect); |
| 908 | sqliteSelectDelete(pSelect); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 909 | if( !pParse->initFlag ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 910 | sqliteViewGetColumnNames(pParse, p); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 911 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 912 | |
| 913 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
| 914 | ** the end. |
| 915 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 916 | sEnd = pParse->sLastToken; |
| 917 | if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){ |
| 918 | sEnd.z += sEnd.n; |
| 919 | } |
| 920 | sEnd.n = 0; |
| 921 | n = ((int)sEnd.z) - (int)pBegin->z; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 922 | z = pBegin->z; |
| 923 | while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; } |
| 924 | sEnd.z = &z[n-1]; |
| 925 | sEnd.n = 1; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 926 | |
| 927 | /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */ |
| 928 | sqliteEndTable(pParse, &sEnd, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 929 | return; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 930 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 931 | |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 932 | /* |
| 933 | ** The Table structure pTable is really a VIEW. Fill in the names of |
| 934 | ** the columns of the view in the pTable structure. Return the number |
| 935 | ** of errors. If an error is seen leave an error message in pPare->zErrMsg. |
| 936 | */ |
| 937 | int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){ |
| 938 | ExprList *pEList; |
| 939 | Select *pSel; |
| 940 | Table *pSelTab; |
| 941 | int nErr = 0; |
| 942 | |
| 943 | assert( pTable ); |
| 944 | |
| 945 | /* A positive nCol means the columns names for this view are |
| 946 | ** already known. |
| 947 | */ |
| 948 | if( pTable->nCol>0 ) return 0; |
| 949 | |
| 950 | /* A negative nCol is a special marker meaning that we are currently |
| 951 | ** trying to compute the column names. If we enter this routine with |
| 952 | ** a negative nCol, it means two or more views form a loop, like this: |
| 953 | ** |
| 954 | ** CREATE VIEW one AS SELECT * FROM two; |
| 955 | ** CREATE VIEW two AS SELECT * FROM one; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 956 | ** |
| 957 | ** Actually, this error is caught previously and so the following test |
| 958 | ** 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] | 959 | */ |
| 960 | if( pTable->nCol<0 ){ |
| 961 | sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName, |
| 962 | " is circularly defined", 0); |
| 963 | pParse->nErr++; |
| 964 | return 1; |
| 965 | } |
| 966 | |
| 967 | /* If we get this far, it means we need to compute the table names. |
| 968 | */ |
| 969 | assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */ |
| 970 | pSel = pTable->pSelect; |
| 971 | |
| 972 | /* Note that the call to sqliteResultSetOfSelect() will expand any |
| 973 | ** "*" elements in this list. But we will need to restore the list |
| 974 | ** back to its original configuration afterwards, so we save a copy of |
| 975 | ** the original in pEList. |
| 976 | */ |
| 977 | pEList = pSel->pEList; |
| 978 | pSel->pEList = sqliteExprListDup(pEList); |
| 979 | if( pSel->pEList==0 ){ |
| 980 | pSel->pEList = pEList; |
| 981 | return 1; /* Malloc failed */ |
| 982 | } |
| 983 | pTable->nCol = -1; |
| 984 | pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel); |
| 985 | if( pSelTab ){ |
| 986 | assert( pTable->aCol==0 ); |
| 987 | pTable->nCol = pSelTab->nCol; |
| 988 | pTable->aCol = pSelTab->aCol; |
| 989 | pSelTab->nCol = 0; |
| 990 | pSelTab->aCol = 0; |
| 991 | sqliteDeleteTable(0, pSelTab); |
| 992 | pParse->db->flags |= SQLITE_UnresetViews; |
| 993 | }else{ |
| 994 | pTable->nCol = 0; |
| 995 | nErr++; |
| 996 | } |
| 997 | sqliteSelectUnbind(pSel); |
| 998 | sqliteExprListDelete(pSel->pEList); |
| 999 | pSel->pEList = pEList; |
| 1000 | return nErr; |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | ** Clear the column names from the VIEW pTable. |
| 1005 | ** |
| 1006 | ** This routine is called whenever any other table or view is modified. |
| 1007 | ** The view passed into this routine might depend directly or indirectly |
| 1008 | ** on the modified or deleted table so we need to clear the old column |
| 1009 | ** names so that they will be recomputed. |
| 1010 | */ |
| 1011 | static void sqliteViewResetColumnNames(Table *pTable){ |
| 1012 | int i; |
| 1013 | if( pTable==0 || pTable->pSelect==0 ) return; |
| 1014 | if( pTable->nCol==0 ) return; |
| 1015 | for(i=0; i<pTable->nCol; i++){ |
| 1016 | sqliteFree(pTable->aCol[i].zName); |
| 1017 | sqliteFree(pTable->aCol[i].zDflt); |
| 1018 | sqliteFree(pTable->aCol[i].zType); |
| 1019 | } |
| 1020 | sqliteFree(pTable->aCol); |
| 1021 | pTable->aCol = 0; |
| 1022 | pTable->nCol = 0; |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | ** Clear the column names from every VIEW. |
| 1027 | */ |
| 1028 | void sqliteViewResetAll(sqlite *db){ |
| 1029 | HashElem *i; |
| 1030 | if( (db->flags & SQLITE_UnresetViews)==0 ) return; |
| 1031 | for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){ |
| 1032 | Table *pTab = sqliteHashData(i); |
| 1033 | if( pTab->pSelect ){ |
| 1034 | sqliteViewResetColumnNames(pTab); |
| 1035 | } |
| 1036 | } |
| 1037 | db->flags &= ~SQLITE_UnresetViews; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1041 | ** Given a token, look up a table with that name. If not found, leave |
| 1042 | ** an error for the parser to find and return NULL. |
| 1043 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1044 | Table *sqliteTableFromToken(Parse *pParse, Token *pTok){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1045 | char *zName; |
| 1046 | Table *pTab; |
| 1047 | zName = sqliteTableNameFromToken(pTok); |
| 1048 | if( zName==0 ) return 0; |
| 1049 | pTab = sqliteFindTable(pParse->db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1050 | sqliteFree(zName); |
| 1051 | if( pTab==0 ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 1052 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 1053 | pTok->z, pTok->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1054 | pParse->nErr++; |
| 1055 | } |
| 1056 | return pTab; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1061 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1062 | */ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1063 | void sqliteDropTable(Parse *pParse, Token *pName, int isView){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1064 | Table *pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1065 | Vdbe *v; |
| 1066 | int base; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1067 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1068 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1069 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1070 | pTable = sqliteTableFromToken(pParse, pName); |
| 1071 | if( pTable==0 ) return; |
| 1072 | if( pTable->readOnly ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 1073 | sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName, |
| 1074 | " may not be dropped", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1075 | pParse->nErr++; |
| 1076 | return; |
| 1077 | } |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1078 | if( isView && pTable->pSelect==0 ){ |
| 1079 | sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ", |
| 1080 | pTable->zName, 0); |
| 1081 | pParse->nErr++; |
| 1082 | return; |
| 1083 | } |
| 1084 | if( !isView && pTable->pSelect ){ |
| 1085 | sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ", |
| 1086 | pTable->zName, 0); |
| 1087 | pParse->nErr++; |
| 1088 | return; |
| 1089 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1090 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1091 | /* Generate code to remove the table from the master table |
| 1092 | ** on disk. |
| 1093 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1094 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1095 | if( v ){ |
| 1096 | static VdbeOp dropTable[] = { |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1097 | { OP_Rewind, 0, ADDR(8), 0}, |
| 1098 | { OP_String, 0, 0, 0}, /* 1 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1099 | { OP_MemStore, 1, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1100 | { OP_MemLoad, 1, 0, 0}, /* 3 */ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1101 | { OP_Column, 0, 2, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1102 | { OP_Ne, 0, ADDR(7), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1103 | { OP_Delete, 0, 0, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1104 | { OP_Next, 0, ADDR(3), 0}, /* 7 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1105 | }; |
| 1106 | Index *pIdx; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1107 | Trigger *pTrigger; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1108 | sqliteBeginWriteOperation(pParse, 0, pTable->isTemp); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1109 | sqliteOpenMasterTable(v, pTable->isTemp); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1110 | /* Drop all triggers associated with the table being dropped */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1111 | pTrigger = pTable->pTrigger; |
| 1112 | while( pTrigger ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1113 | Token tt; |
| 1114 | tt.z = pTable->pTrigger->name; |
| 1115 | tt.n = strlen(pTable->pTrigger->name); |
| 1116 | sqliteDropTrigger(pParse, &tt, 1); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1117 | if( pParse->explain ){ |
| 1118 | pTrigger = pTrigger->pNext; |
| 1119 | }else{ |
| 1120 | pTrigger = pTable->pTrigger; |
| 1121 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1122 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1123 | base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); |
| 1124 | sqliteVdbeChangeP3(v, base+1, pTable->zName, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1125 | if( !pTable->isTemp ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1126 | sqliteChangeCookie(db, v); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1127 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1128 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1129 | if( !isView ){ |
| 1130 | sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp); |
| 1131 | for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1132 | sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp); |
| 1133 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1134 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1135 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1138 | /* Delete the in-memory description of the table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1139 | ** |
| 1140 | ** Exception: if the SQL statement began with the EXPLAIN keyword, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1141 | ** then no changes should be made. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1142 | */ |
| 1143 | if( !pParse->explain ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1144 | sqliteUnlinkAndDeleteTable(db, pTable); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1145 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1146 | } |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1147 | sqliteViewResetAll(db); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | /* |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 1151 | ** This routine constructs a P3 string suitable for an OP_MakeIdxKey |
| 1152 | ** opcode and adds that P3 string to the most recently inserted instruction |
| 1153 | ** in the virtual machine. The P3 string consists of a single character |
| 1154 | ** for each column in the index pIdx of table pTab. If the column uses |
| 1155 | ** a numeric sort order, then the P3 string character corresponding to |
| 1156 | ** that column is 'n'. If the column uses a text sort order, then the |
| 1157 | ** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for |
| 1158 | ** additional information. See also the sqliteAddKeyType() routine. |
| 1159 | */ |
| 1160 | void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){ |
| 1161 | char *zType; |
| 1162 | Table *pTab; |
| 1163 | int i, n; |
| 1164 | assert( pIdx!=0 && pIdx->pTable!=0 ); |
| 1165 | pTab = pIdx->pTable; |
| 1166 | n = pIdx->nColumn; |
| 1167 | zType = sqliteMalloc( n+1 ); |
| 1168 | if( zType==0 ) return; |
| 1169 | for(i=0; i<n; i++){ |
| 1170 | int iCol = pIdx->aiColumn[i]; |
| 1171 | assert( iCol>=0 && iCol<pTab->nCol ); |
| 1172 | if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){ |
| 1173 | zType[i] = 't'; |
| 1174 | }else{ |
| 1175 | zType[i] = 'n'; |
| 1176 | } |
| 1177 | } |
| 1178 | zType[n] = 0; |
| 1179 | sqliteVdbeChangeP3(v, -1, zType, n); |
| 1180 | sqliteFree(zType); |
| 1181 | } |
| 1182 | |
| 1183 | /* |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1184 | ** This routine is called to create a new foreign key on the table |
| 1185 | ** currently under construction. pFromCol determines which columns |
| 1186 | ** in the current table point to the foreign key. If pFromCol==0 then |
| 1187 | ** connect the key to the last column inserted. pTo is the name of |
| 1188 | ** the table referred to. pToCol is a list of tables in the other |
| 1189 | ** pTo table that the foreign key points to. flags contains all |
| 1190 | ** information about the conflict resolution algorithms specified |
| 1191 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
| 1192 | ** |
| 1193 | ** An FKey structure is created and added to the table currently |
| 1194 | ** under construction in the pParse->pNewTable field. The new FKey |
| 1195 | ** is not linked into db->aFKey at this point - that does not happen |
| 1196 | ** until sqliteEndTable(). |
| 1197 | ** |
| 1198 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
| 1199 | ** to sqliteDeferForeignKey() might change this to DEFERRED. |
| 1200 | */ |
| 1201 | void sqliteCreateForeignKey( |
| 1202 | Parse *pParse, /* Parsing context */ |
| 1203 | IdList *pFromCol, /* Columns in this table that point to other table */ |
| 1204 | Token *pTo, /* Name of the other table */ |
| 1205 | IdList *pToCol, /* Columns in the other table */ |
| 1206 | int flags /* Conflict resolution algorithms. */ |
| 1207 | ){ |
| 1208 | Table *p = pParse->pNewTable; |
| 1209 | int nByte; |
| 1210 | int i; |
| 1211 | int nCol; |
| 1212 | char *z; |
| 1213 | FKey *pFKey = 0; |
| 1214 | |
| 1215 | assert( pTo!=0 ); |
| 1216 | if( p==0 || pParse->nErr ) goto fk_end; |
| 1217 | if( pFromCol==0 ){ |
| 1218 | int iCol = p->nCol-1; |
| 1219 | if( iCol<0 ) goto fk_end; |
| 1220 | if( pToCol && pToCol->nId!=1 ){ |
| 1221 | sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1, |
| 1222 | p->aCol[iCol].zName, -1, |
| 1223 | " should reference only one column of table ", -1, |
| 1224 | pTo->z, pTo->n, 0); |
| 1225 | pParse->nErr++; |
| 1226 | goto fk_end; |
| 1227 | } |
| 1228 | nCol = 1; |
| 1229 | }else if( pToCol && pToCol->nId!=pFromCol->nId ){ |
| 1230 | sqliteSetString(&pParse->zErrMsg, |
| 1231 | "number of columns in foreign key does not match the number of " |
| 1232 | "columns in the referenced table", 0); |
| 1233 | pParse->nErr++; |
| 1234 | goto fk_end; |
| 1235 | }else{ |
| 1236 | nCol = pFromCol->nId; |
| 1237 | } |
| 1238 | nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
| 1239 | if( pToCol ){ |
| 1240 | for(i=0; i<pToCol->nId; i++){ |
| 1241 | nByte += strlen(pToCol->a[i].zName) + 1; |
| 1242 | } |
| 1243 | } |
| 1244 | pFKey = sqliteMalloc( nByte ); |
| 1245 | if( pFKey==0 ) goto fk_end; |
| 1246 | pFKey->pFrom = p; |
| 1247 | pFKey->pNextFrom = p->pFKey; |
drh | df68f6b | 2002-09-21 15:57:57 +0000 | [diff] [blame^] | 1248 | z = (char*)&pFKey[1]; |
| 1249 | pFKey->aCol = (struct sColMap*)z; |
| 1250 | z += sizeof(struct sColMap)*nCol; |
| 1251 | pFKey->zTo = z; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1252 | memcpy(z, pTo->z, pTo->n); |
| 1253 | z[pTo->n] = 0; |
| 1254 | z += pTo->n+1; |
| 1255 | pFKey->pNextTo = 0; |
| 1256 | pFKey->nCol = nCol; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1257 | if( pFromCol==0 ){ |
| 1258 | pFKey->aCol[0].iFrom = p->nCol-1; |
| 1259 | }else{ |
| 1260 | for(i=0; i<nCol; i++){ |
| 1261 | int j; |
| 1262 | for(j=0; j<p->nCol; j++){ |
| 1263 | if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ |
| 1264 | pFKey->aCol[i].iFrom = j; |
| 1265 | break; |
| 1266 | } |
| 1267 | } |
| 1268 | if( j>=p->nCol ){ |
| 1269 | sqliteSetString(&pParse->zErrMsg, "unknown column \"", |
| 1270 | pFromCol->a[i].zName, "\" in foreign key definition", 0); |
| 1271 | pParse->nErr++; |
| 1272 | goto fk_end; |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | if( pToCol ){ |
| 1277 | for(i=0; i<nCol; i++){ |
| 1278 | int n = strlen(pToCol->a[i].zName); |
| 1279 | pFKey->aCol[i].zCol = z; |
| 1280 | memcpy(z, pToCol->a[i].zName, n); |
| 1281 | z[n] = 0; |
| 1282 | z += n+1; |
| 1283 | } |
| 1284 | } |
| 1285 | pFKey->isDeferred = 0; |
| 1286 | pFKey->deleteConf = flags & 0xff; |
| 1287 | pFKey->updateConf = (flags >> 8 ) & 0xff; |
| 1288 | pFKey->insertConf = (flags >> 16 ) & 0xff; |
| 1289 | |
| 1290 | /* Link the foreign key to the table as the last step. |
| 1291 | */ |
| 1292 | p->pFKey = pFKey; |
| 1293 | pFKey = 0; |
| 1294 | |
| 1295 | fk_end: |
| 1296 | sqliteFree(pFKey); |
| 1297 | sqliteIdListDelete(pFromCol); |
| 1298 | sqliteIdListDelete(pToCol); |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 1303 | ** clause is seen as part of a foreign key definition. The isDeferred |
| 1304 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 1305 | ** The behavior of the most recently created foreign key is adjusted |
| 1306 | ** accordingly. |
| 1307 | */ |
| 1308 | void sqliteDeferForeignKey(Parse *pParse, int isDeferred){ |
| 1309 | Table *pTab; |
| 1310 | FKey *pFKey; |
| 1311 | if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; |
| 1312 | pFKey->isDeferred = isDeferred; |
| 1313 | } |
| 1314 | |
| 1315 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1316 | ** Create a new index for an SQL table. pIndex is the name of the index |
| 1317 | ** 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] | 1318 | ** be NULL for a primary key or an index that is created to satisfy a |
| 1319 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1320 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 1321 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1322 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1323 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 1324 | ** is a primary key or unique-constraint on the most recent column added |
| 1325 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1326 | */ |
| 1327 | void sqliteCreateIndex( |
| 1328 | Parse *pParse, /* All information about this parse */ |
| 1329 | Token *pName, /* Name of the index. May be NULL */ |
| 1330 | Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */ |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1331 | IdList *pList, /* A list of columns to be indexed */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1332 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1333 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
| 1334 | Token *pEnd /* The ")" that closes the CREATE INDEX statement */ |
| 1335 | ){ |
| 1336 | Table *pTab; /* Table to be indexed */ |
| 1337 | Index *pIndex; /* The index to be created */ |
| 1338 | char *zName = 0; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 1339 | int i, j; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1340 | Token nullId; /* Fake token for an empty ID list */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1341 | sqlite *db = pParse->db; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1342 | int hideName = 0; /* Do not put table name in the hash table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1343 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1344 | if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index; |
| 1345 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1346 | /* |
| 1347 | ** Find the table that is to be indexed. Return early if not found. |
| 1348 | */ |
| 1349 | if( pTable!=0 ){ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1350 | assert( pName!=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1351 | pTab = sqliteTableFromToken(pParse, pTable); |
| 1352 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1353 | assert( pName==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1354 | pTab = pParse->pNewTable; |
| 1355 | } |
| 1356 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
| 1357 | if( pTab->readOnly ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 1358 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 1359 | " may not have new indices added", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1360 | pParse->nErr++; |
| 1361 | goto exit_create_index; |
| 1362 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1363 | if( pTab->pSelect ){ |
| 1364 | sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0); |
| 1365 | pParse->nErr++; |
| 1366 | goto exit_create_index; |
| 1367 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1368 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1369 | /* If this index is created while re-reading the schema from sqlite_master |
| 1370 | ** but the table associated with this index is a temporary table, it can |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1371 | ** only mean that the table that this index is really associated with is |
| 1372 | ** one whose name is hidden behind a temporary table with the same name. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1373 | ** Since its table has been suppressed, we need to also suppress the |
| 1374 | ** index. |
| 1375 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1376 | if( pParse->initFlag && !pParse->isTemp && pTab->isTemp ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1377 | goto exit_create_index; |
| 1378 | } |
| 1379 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1380 | /* |
| 1381 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1382 | ** index or table with the same name. |
| 1383 | ** |
| 1384 | ** Exception: If we are reading the names of permanent indices from the |
| 1385 | ** sqlite_master table (because some other process changed the schema) and |
| 1386 | ** one of the index names collides with the name of a temporary table or |
| 1387 | ** index, then we will continue to process this index, but we will not |
| 1388 | ** store its name in the hash table. Set the hideName flag to accomplish |
| 1389 | ** this. |
| 1390 | ** |
| 1391 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1392 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 1393 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1394 | */ |
| 1395 | if( pName ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1396 | Index *pISameName; /* Another index with the same name */ |
| 1397 | Table *pTSameName; /* A table with same name as the index */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1398 | zName = sqliteTableNameFromToken(pName); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1399 | if( zName==0 ) goto exit_create_index; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1400 | if( (pISameName = sqliteFindIndex(db, zName))!=0 ){ |
| 1401 | if( pISameName->pTable->isTemp && pParse->initFlag ){ |
| 1402 | hideName = 1; |
| 1403 | }else{ |
| 1404 | sqliteSetString(&pParse->zErrMsg, "index ", zName, |
| 1405 | " already exists", 0); |
| 1406 | pParse->nErr++; |
| 1407 | goto exit_create_index; |
| 1408 | } |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1409 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1410 | if( (pTSameName = sqliteFindTable(db, zName))!=0 ){ |
| 1411 | if( pTSameName->isTemp && pParse->initFlag ){ |
| 1412 | hideName = 1; |
| 1413 | }else{ |
| 1414 | sqliteSetString(&pParse->zErrMsg, "there is already a table named ", |
| 1415 | zName, 0); |
| 1416 | pParse->nErr++; |
| 1417 | goto exit_create_index; |
| 1418 | } |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1419 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1420 | }else{ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1421 | char zBuf[30]; |
| 1422 | int n; |
| 1423 | Index *pLoop; |
| 1424 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
| 1425 | sprintf(zBuf,"%d)",n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1426 | zName = 0; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1427 | sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1428 | if( zName==0 ) goto exit_create_index; |
drh | da9e034 | 2002-01-10 14:31:48 +0000 | [diff] [blame] | 1429 | hideName = sqliteFindIndex(db, zName)!=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1433 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1434 | ** So create a fake list to simulate this. |
| 1435 | */ |
| 1436 | if( pList==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1437 | nullId.z = pTab->aCol[pTab->nCol-1].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1438 | nullId.n = strlen(nullId.z); |
| 1439 | pList = sqliteIdListAppend(0, &nullId); |
| 1440 | if( pList==0 ) goto exit_create_index; |
| 1441 | } |
| 1442 | |
| 1443 | /* |
| 1444 | ** Allocate the index structure. |
| 1445 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 1446 | pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1447 | sizeof(int)*pList->nId ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1448 | if( pIndex==0 ) goto exit_create_index; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1449 | pIndex->aiColumn = (int*)&pIndex[1]; |
| 1450 | pIndex->zName = (char*)&pIndex->aiColumn[pList->nId]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1451 | strcpy(pIndex->zName, zName); |
| 1452 | pIndex->pTable = pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1453 | pIndex->nColumn = pList->nId; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1454 | pIndex->onError = pIndex->isUnique = onError; |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1455 | pIndex->autoIndex = pName==0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1456 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1457 | /* Scan the names of the columns of the table to be indexed and |
| 1458 | ** load the column indices into the Index structure. Report an error |
| 1459 | ** if any column is not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1460 | */ |
| 1461 | for(i=0; i<pList->nId; i++){ |
| 1462 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1463 | if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1464 | } |
| 1465 | if( j>=pTab->nCol ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 1466 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1467 | " has no column named ", pList->a[i].zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1468 | pParse->nErr++; |
| 1469 | sqliteFree(pIndex); |
| 1470 | goto exit_create_index; |
| 1471 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1472 | pIndex->aiColumn[i] = j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /* Link the new Index structure to its table and to the other |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1476 | ** in-memory database structures. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1477 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1478 | if( !pParse->explain && !hideName ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1479 | Index *p; |
| 1480 | p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex); |
| 1481 | if( p ){ |
| 1482 | assert( p==pIndex ); /* Malloc must have failed */ |
| 1483 | sqliteFree(pIndex); |
| 1484 | goto exit_create_index; |
| 1485 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1486 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1487 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1488 | |
| 1489 | /* When adding an index to the list of indices for a table, make |
| 1490 | ** sure all indices labeled OE_Replace come after all those labeled |
| 1491 | ** OE_Ignore. This is necessary for the correct operation of UPDATE |
| 1492 | ** and INSERT. |
| 1493 | */ |
| 1494 | if( onError!=OE_Replace || pTab->pIndex==0 |
| 1495 | || pTab->pIndex->onError==OE_Replace){ |
| 1496 | pIndex->pNext = pTab->pIndex; |
| 1497 | pTab->pIndex = pIndex; |
| 1498 | }else{ |
| 1499 | Index *pOther = pTab->pIndex; |
| 1500 | while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
| 1501 | pOther = pOther->pNext; |
| 1502 | } |
| 1503 | pIndex->pNext = pOther->pNext; |
| 1504 | pOther->pNext = pIndex; |
| 1505 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1506 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1507 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 1508 | ** "sqlite_master" table on the disk. So do not write to the disk |
| 1509 | ** again. Extract the table number from the pParse->newTnum field. |
| 1510 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1511 | if( pParse->initFlag && pTable!=0 ){ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1512 | pIndex->tnum = pParse->newTnum; |
| 1513 | } |
| 1514 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1515 | /* If the initFlag is 0 then create the index on disk. This |
| 1516 | ** involves writing the index into the master table and filling in the |
| 1517 | ** index with the current table contents. |
| 1518 | ** |
| 1519 | ** The initFlag is 0 when the user first enters a CREATE INDEX |
| 1520 | ** command. The initFlag is 1 when a database is opened and |
| 1521 | ** CREATE INDEX statements are read out of the master table. In |
| 1522 | ** the latter case the index already exists on disk, which is why |
| 1523 | ** we don't want to recreate it. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1524 | ** |
| 1525 | ** If pTable==0 it means this index is generated as a primary key |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1526 | ** or UNIQUE constraint of a CREATE TABLE statement. Since the table |
| 1527 | ** has just been created, it contains no data and the index initialization |
| 1528 | ** step can be skipped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1529 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1530 | else if( pParse->initFlag==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1531 | int n; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1532 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1533 | int lbl1, lbl2; |
| 1534 | int i; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1535 | int addr; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1536 | int isTemp = pTab->isTemp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1537 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1538 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1539 | if( v==0 ) goto exit_create_index; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1540 | if( pTable!=0 ){ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1541 | sqliteBeginWriteOperation(pParse, 0, isTemp); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1542 | sqliteOpenMasterTable(v, isTemp); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1543 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1544 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 1545 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1546 | sqliteVdbeChangeP3(v, -1, "index", P3_STATIC); |
| 1547 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1548 | sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC); |
| 1549 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1550 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1551 | addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp); |
| 1552 | sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1553 | pIndex->tnum = 0; |
| 1554 | if( pTable ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1555 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1556 | if( isTemp ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1557 | sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1558 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1559 | sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1560 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1561 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1562 | addr = sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1563 | if( pStart && pEnd ){ |
| 1564 | n = Addr(pEnd->z) - Addr(pStart->z) + 1; |
| 1565 | sqliteVdbeChangeP3(v, addr, pStart->z, n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1566 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1567 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0); |
| 1568 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1569 | if( pTable ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1570 | sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum); |
| 1571 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1572 | lbl2 = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1573 | sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2); |
| 1574 | lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1575 | for(i=0; i<pIndex->nColumn; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1576 | sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1577 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1578 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0); |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 1579 | if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1580 | sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1581 | sqliteVdbeAddOp(v, OP_Next, 2, lbl1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1582 | sqliteVdbeResolveLabel(v, lbl2); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1583 | sqliteVdbeAddOp(v, OP_Close, 2, 0); |
| 1584 | sqliteVdbeAddOp(v, OP_Close, 1, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1585 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1586 | if( pTable!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1587 | if( !isTemp ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1588 | sqliteChangeCookie(db, v); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1589 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1590 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1591 | sqliteEndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1592 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1595 | /* Clean up before exiting */ |
| 1596 | exit_create_index: |
| 1597 | sqliteIdListDelete(pList); |
| 1598 | sqliteFree(zName); |
| 1599 | return; |
| 1600 | } |
| 1601 | |
| 1602 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 1603 | ** This routine will drop an existing named index. This routine |
| 1604 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1605 | */ |
| 1606 | void sqliteDropIndex(Parse *pParse, Token *pName){ |
| 1607 | Index *pIndex; |
| 1608 | char *zName; |
| 1609 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1610 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1611 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1612 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1613 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1614 | if( zName==0 ) return; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1615 | pIndex = sqliteFindIndex(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1616 | sqliteFree(zName); |
| 1617 | if( pIndex==0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 1618 | sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0, |
| 1619 | pName->z, pName->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1620 | pParse->nErr++; |
| 1621 | return; |
| 1622 | } |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1623 | if( pIndex->autoIndex ){ |
| 1624 | sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE " |
| 1625 | "or PRIMARY KEY constraint cannot be dropped", 0); |
| 1626 | pParse->nErr++; |
| 1627 | return; |
| 1628 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1629 | |
| 1630 | /* Generate code to remove the index and from the master table */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1631 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1632 | if( v ){ |
| 1633 | static VdbeOp dropIndex[] = { |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1634 | { OP_Rewind, 0, ADDR(9), 0}, |
| 1635 | { OP_String, 0, 0, 0}, /* 1 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1636 | { OP_MemStore, 1, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1637 | { OP_MemLoad, 1, 0, 0}, /* 3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1638 | { OP_Column, 0, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1639 | { OP_Eq, 0, ADDR(8), 0}, |
| 1640 | { OP_Next, 0, ADDR(3), 0}, |
| 1641 | { OP_Goto, 0, ADDR(9), 0}, |
| 1642 | { OP_Delete, 0, 0, 0}, /* 8 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1643 | }; |
| 1644 | int base; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1645 | Table *pTab = pIndex->pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1646 | |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1647 | sqliteBeginWriteOperation(pParse, 0, pTab->isTemp); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1648 | sqliteOpenMasterTable(v, pTab->isTemp); |
| 1649 | base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); |
| 1650 | sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1651 | if( !pTab->isTemp ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1652 | sqliteChangeCookie(db, v); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1653 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1654 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1655 | sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1656 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1659 | /* Delete the in-memory description of this index. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1660 | */ |
| 1661 | if( !pParse->explain ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1662 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1663 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1668 | ** Append a new element to the given IdList. Create a new IdList if |
| 1669 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1670 | ** |
| 1671 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1672 | */ |
| 1673 | IdList *sqliteIdListAppend(IdList *pList, Token *pToken){ |
| 1674 | if( pList==0 ){ |
| 1675 | pList = sqliteMalloc( sizeof(IdList) ); |
| 1676 | if( pList==0 ) return 0; |
| 1677 | } |
| 1678 | if( (pList->nId & 7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1679 | struct IdList_item *a; |
| 1680 | a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) ); |
| 1681 | if( a==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1682 | sqliteIdListDelete(pList); |
| 1683 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1684 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1685 | pList->a = a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1686 | } |
| 1687 | memset(&pList->a[pList->nId], 0, sizeof(pList->a[0])); |
| 1688 | if( pToken ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1689 | char **pz = &pList->a[pList->nId].zName; |
| 1690 | sqliteSetNString(pz, pToken->z, pToken->n, 0); |
| 1691 | if( *pz==0 ){ |
| 1692 | sqliteIdListDelete(pList); |
| 1693 | return 0; |
| 1694 | }else{ |
| 1695 | sqliteDequote(*pz); |
| 1696 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1697 | } |
| 1698 | pList->nId++; |
| 1699 | return pList; |
| 1700 | } |
| 1701 | |
| 1702 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1703 | ** Append a new table name to the given SrcList. Create a new SrcList if |
| 1704 | ** need be. A new entry is created in the SrcList even if pToken is NULL. |
| 1705 | ** |
| 1706 | ** A new SrcList is returned, or NULL if malloc() fails. |
| 1707 | */ |
| 1708 | SrcList *sqliteSrcListAppend(SrcList *pList, Token *pToken){ |
| 1709 | if( pList==0 ){ |
| 1710 | pList = sqliteMalloc( sizeof(IdList) ); |
| 1711 | if( pList==0 ) return 0; |
| 1712 | } |
| 1713 | if( (pList->nSrc & 7)==0 ){ |
| 1714 | struct SrcList_item *a; |
| 1715 | a = sqliteRealloc(pList->a, (pList->nSrc+8)*sizeof(pList->a[0]) ); |
| 1716 | if( a==0 ){ |
| 1717 | sqliteSrcListDelete(pList); |
| 1718 | return 0; |
| 1719 | } |
| 1720 | pList->a = a; |
| 1721 | } |
| 1722 | memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0])); |
| 1723 | if( pToken ){ |
| 1724 | char **pz = &pList->a[pList->nSrc].zName; |
| 1725 | sqliteSetNString(pz, pToken->z, pToken->n, 0); |
| 1726 | if( *pz==0 ){ |
| 1727 | sqliteSrcListDelete(pList); |
| 1728 | return 0; |
| 1729 | }else{ |
| 1730 | sqliteDequote(*pz); |
| 1731 | } |
| 1732 | } |
| 1733 | pList->nSrc++; |
| 1734 | return pList; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1738 | ** Add an alias to the last identifier on the given identifier list. |
| 1739 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1740 | void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){ |
| 1741 | if( pList && pList->nSrc>0 ){ |
| 1742 | int i = pList->nSrc - 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1743 | sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1744 | sqliteDequote(pList->a[i].zAlias); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1749 | ** Delete an IdList. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1750 | */ |
| 1751 | void sqliteIdListDelete(IdList *pList){ |
| 1752 | int i; |
| 1753 | if( pList==0 ) return; |
| 1754 | for(i=0; i<pList->nId; i++){ |
| 1755 | sqliteFree(pList->a[i].zName); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1756 | } |
| 1757 | sqliteFree(pList->a); |
| 1758 | sqliteFree(pList); |
| 1759 | } |
| 1760 | |
| 1761 | /* |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1762 | ** Return the index in pList of the identifier named zId. Return -1 |
| 1763 | ** if not found. |
| 1764 | */ |
| 1765 | int sqliteIdListIndex(IdList *pList, const char *zName){ |
| 1766 | int i; |
| 1767 | if( pList==0 ) return -1; |
| 1768 | for(i=0; i<pList->nId; i++){ |
| 1769 | if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i; |
| 1770 | } |
| 1771 | return -1; |
| 1772 | } |
| 1773 | |
| 1774 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1775 | ** Delete an entire SrcList including all its substructure. |
| 1776 | */ |
| 1777 | void sqliteSrcListDelete(SrcList *pList){ |
| 1778 | int i; |
| 1779 | if( pList==0 ) return; |
| 1780 | for(i=0; i<pList->nSrc; i++){ |
| 1781 | sqliteFree(pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1782 | sqliteFree(pList->a[i].zAlias); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1783 | if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1784 | sqliteDeleteTable(0, pList->a[i].pTab); |
| 1785 | } |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1786 | sqliteSelectDelete(pList->a[i].pSelect); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1787 | sqliteExprDelete(pList->a[i].pOn); |
| 1788 | sqliteIdListDelete(pList->a[i].pUsing); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1789 | } |
| 1790 | sqliteFree(pList->a); |
| 1791 | sqliteFree(pList); |
| 1792 | } |
| 1793 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1794 | /* |
| 1795 | ** The COPY command is for compatibility with PostgreSQL and specificially |
| 1796 | ** for the ability to read the output of pg_dump. The format is as |
| 1797 | ** follows: |
| 1798 | ** |
| 1799 | ** COPY table FROM file [USING DELIMITERS string] |
| 1800 | ** |
| 1801 | ** "table" is an existing table name. We will read lines of code from |
| 1802 | ** file to fill this table with data. File might be "stdin". The optional |
| 1803 | ** delimiter string identifies the field separators. The default is a tab. |
| 1804 | */ |
| 1805 | void sqliteCopy( |
| 1806 | Parse *pParse, /* The parser context */ |
| 1807 | Token *pTableName, /* The name of the table into which we will insert */ |
| 1808 | Token *pFilename, /* The file from which to obtain information */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1809 | Token *pDelimiter, /* Use this as the field delimiter */ |
| 1810 | int onError /* What to do if a constraint fails */ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1811 | ){ |
| 1812 | Table *pTab; |
| 1813 | char *zTab; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1814 | int i; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1815 | Vdbe *v; |
| 1816 | int addr, end; |
| 1817 | Index *pIdx; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1818 | sqlite *db = pParse->db; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1819 | |
| 1820 | zTab = sqliteTableNameFromToken(pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1821 | if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup; |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 1822 | pTab = sqliteTableNameToTable(pParse, zTab); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1823 | sqliteFree(zTab); |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 1824 | if( pTab==0 ) goto copy_cleanup; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1825 | v = sqliteGetVdbe(pParse); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1826 | if( v ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1827 | int openOp; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1828 | sqliteBeginWriteOperation(pParse, 1, pTab->isTemp); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1829 | addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1830 | sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n); |
drh | b766599 | 2000-05-30 17:30:35 +0000 | [diff] [blame] | 1831 | sqliteVdbeDequoteP3(v, addr); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1832 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1833 | sqliteVdbeAddOp(v, openOp, 0, pTab->tnum); |
| 1834 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1835 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1836 | sqliteVdbeAddOp(v, openOp, i, pIdx->tnum); |
| 1837 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1838 | } |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1839 | if( db->flags & SQLITE_CountRows ){ |
| 1840 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */ |
| 1841 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1842 | end = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1843 | addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1844 | if( pDelimiter ){ |
| 1845 | sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n); |
| 1846 | sqliteVdbeDequoteP3(v, addr); |
| 1847 | }else{ |
| 1848 | sqliteVdbeChangeP3(v, addr, "\t", 1); |
| 1849 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1850 | if( pTab->iPKey>=0 ){ |
| 1851 | sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0); |
| 1852 | sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); |
| 1853 | }else{ |
| 1854 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 1855 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1856 | for(i=0; i<pTab->nCol; i++){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1857 | if( i==pTab->iPKey ){ |
| 1858 | /* The integer primary key column is filled with NULL since its |
| 1859 | ** value is always pulled from the record number */ |
| 1860 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1861 | }else{ |
| 1862 | sqliteVdbeAddOp(v, OP_FileColumn, i, 0); |
| 1863 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1864 | } |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1865 | sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr); |
| 1866 | sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0); |
| 1867 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
| 1868 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1869 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1870 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 1871 | sqliteVdbeResolveLabel(v, end); |
| 1872 | sqliteVdbeAddOp(v, OP_Noop, 0, 0); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1873 | sqliteEndWriteOperation(pParse); |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1874 | if( db->flags & SQLITE_CountRows ){ |
| 1875 | sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); |
| 1876 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 1877 | sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); |
| 1878 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 1879 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | copy_cleanup: |
| 1883 | return; |
| 1884 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1885 | |
| 1886 | /* |
| 1887 | ** The non-standard VACUUM command is used to clean up the database, |
| 1888 | ** collapse free space, etc. It is modelled after the VACUUM command |
| 1889 | ** in PostgreSQL. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 1890 | ** |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 1891 | ** In version 1.0.x of SQLite, the VACUUM command would call |
| 1892 | ** gdbm_reorganize() on all the database tables. But beginning |
| 1893 | ** with 2.0.0, SQLite no longer uses GDBM so this command has |
| 1894 | ** become a no-op. |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1895 | */ |
| 1896 | void sqliteVacuum(Parse *pParse, Token *pTableName){ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 1897 | /* Do nothing */ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1898 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1899 | |
| 1900 | /* |
| 1901 | ** Begin a transaction |
| 1902 | */ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1903 | void sqliteBeginTransaction(Parse *pParse, int onError){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1904 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1905 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1906 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1907 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 1908 | if( db->flags & SQLITE_InTrans ){ |
| 1909 | pParse->nErr++; |
| 1910 | sqliteSetString(&pParse->zErrMsg, "cannot start a transaction " |
| 1911 | "within a transaction", 0); |
| 1912 | return; |
| 1913 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1914 | sqliteBeginWriteOperation(pParse, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1915 | db->flags |= SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1916 | db->onError = onError; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | /* |
| 1920 | ** Commit a transaction |
| 1921 | */ |
| 1922 | void sqliteCommitTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1923 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1924 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1925 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1926 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 1927 | if( (db->flags & SQLITE_InTrans)==0 ){ |
| 1928 | pParse->nErr++; |
| 1929 | sqliteSetString(&pParse->zErrMsg, |
| 1930 | "cannot commit - no transaction is active", 0); |
| 1931 | return; |
| 1932 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1933 | db->flags &= ~SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1934 | sqliteEndWriteOperation(pParse); |
| 1935 | db->onError = OE_Default; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | /* |
| 1939 | ** Rollback a transaction |
| 1940 | */ |
| 1941 | void sqliteRollbackTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1942 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1943 | Vdbe *v; |
| 1944 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1945 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1946 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 1947 | if( (db->flags & SQLITE_InTrans)==0 ){ |
| 1948 | pParse->nErr++; |
| 1949 | sqliteSetString(&pParse->zErrMsg, |
| 1950 | "cannot rollback - no transaction is active", 0); |
| 1951 | return; |
| 1952 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1953 | v = sqliteGetVdbe(pParse); |
| 1954 | if( v ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1955 | sqliteVdbeAddOp(v, OP_Rollback, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1956 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1957 | db->flags &= ~SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1958 | db->onError = OE_Default; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1959 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1960 | |
| 1961 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1962 | ** Generate VDBE code that prepares for doing an operation that |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 1963 | ** might change the database. |
| 1964 | ** |
| 1965 | ** This routine starts a new transaction if we are not already within |
| 1966 | ** a transaction. If we are already within a transaction, then a checkpoint |
| 1967 | ** is set if the setCheckpoint parameter is true. A checkpoint should |
| 1968 | ** be set for operations that might fail (due to a constraint) part of |
| 1969 | ** the way through and which will need to undo some writes without having to |
| 1970 | ** rollback the whole transaction. For operations where all constraints |
| 1971 | ** can be checked before any changes are made to the database, it is never |
| 1972 | ** necessary to undo a write and the checkpoint should not be set. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1973 | ** |
| 1974 | ** The tempOnly flag indicates that only temporary tables will be changed |
| 1975 | ** during this write operation. The primary database table is not |
| 1976 | ** write-locked. Only the temporary database file gets a write lock. |
| 1977 | ** Other processes can continue to read or write the primary database file. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1978 | */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1979 | void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1980 | Vdbe *v; |
| 1981 | v = sqliteGetVdbe(pParse); |
| 1982 | if( v==0 ) return; |
drh | dc37945 | 2002-05-15 12:45:43 +0000 | [diff] [blame] | 1983 | if( pParse->trigStack ) return; /* if this is in a trigger */ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1984 | if( (pParse->db->flags & SQLITE_InTrans)==0 ){ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1985 | sqliteVdbeAddOp(v, OP_Transaction, tempOnly, 0); |
| 1986 | if( !tempOnly ){ |
| 1987 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); |
| 1988 | pParse->schemaVerified = 1; |
| 1989 | } |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 1990 | }else if( setCheckpoint ){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1991 | sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0); |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1996 | ** Generate code that concludes an operation that may have changed |
| 1997 | ** the database. This is a companion function to BeginWriteOperation(). |
| 1998 | ** If a transaction was started, then commit it. If a checkpoint was |
| 1999 | ** started then commit that. |
| 2000 | */ |
| 2001 | void sqliteEndWriteOperation(Parse *pParse){ |
| 2002 | Vdbe *v; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 2003 | if( pParse->trigStack ) return; /* if this is in a trigger */ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2004 | v = sqliteGetVdbe(pParse); |
| 2005 | if( v==0 ) return; |
| 2006 | if( pParse->db->flags & SQLITE_InTrans ){ |
| 2007 | /* Do Nothing */ |
| 2008 | }else{ |
| 2009 | sqliteVdbeAddOp(v, OP_Commit, 0, 0); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | |
| 2014 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2015 | ** Interpret the given string as a boolean value. |
| 2016 | */ |
| 2017 | static int getBoolean(char *z){ |
| 2018 | static char *azTrue[] = { "yes", "on", "true" }; |
| 2019 | int i; |
| 2020 | if( z[0]==0 ) return 0; |
| 2021 | if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){ |
| 2022 | return atoi(z); |
| 2023 | } |
| 2024 | for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){ |
| 2025 | if( sqliteStrICmp(z,azTrue[i])==0 ) return 1; |
| 2026 | } |
| 2027 | return 0; |
| 2028 | } |
| 2029 | |
| 2030 | /* |
| 2031 | ** Process a pragma statement. |
| 2032 | ** |
| 2033 | ** Pragmas are of this form: |
| 2034 | ** |
| 2035 | ** PRAGMA id = value |
| 2036 | ** |
| 2037 | ** The identifier might also be a string. The value is a string, and |
| 2038 | ** identifier, or a number. If minusFlag is true, then the value is |
| 2039 | ** a number that was preceded by a minus sign. |
| 2040 | */ |
| 2041 | void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){ |
| 2042 | char *zLeft = 0; |
| 2043 | char *zRight = 0; |
| 2044 | sqlite *db = pParse->db; |
| 2045 | |
| 2046 | zLeft = sqliteStrNDup(pLeft->z, pLeft->n); |
| 2047 | sqliteDequote(zLeft); |
| 2048 | if( minusFlag ){ |
| 2049 | zRight = 0; |
| 2050 | sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0); |
| 2051 | }else{ |
| 2052 | zRight = sqliteStrNDup(pRight->z, pRight->n); |
| 2053 | sqliteDequote(zRight); |
| 2054 | } |
| 2055 | |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2056 | /* |
| 2057 | ** PRAGMA default_cache_size |
| 2058 | ** PRAGMA default_cache_size=N |
| 2059 | ** |
| 2060 | ** The first form reports the current persistent setting for the |
| 2061 | ** page cache size. The value returned is the maximum number of |
| 2062 | ** pages in the page cache. The second form sets both the current |
| 2063 | ** page cache size value and the persistent page cache size value |
| 2064 | ** stored in the database file. |
| 2065 | ** |
| 2066 | ** The default cache size is stored in meta-value 2 of page 1 of the |
| 2067 | ** database file. The cache size is actually the absolute value of |
| 2068 | ** this memory location. The sign of meta-value 2 determines the |
| 2069 | ** synchronous setting. A negative value means synchronous is off |
| 2070 | ** and a positive value means synchronous is on. |
| 2071 | */ |
| 2072 | if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2073 | static VdbeOp getCacheSize[] = { |
| 2074 | { OP_ReadCookie, 0, 2, 0}, |
| 2075 | { OP_AbsValue, 0, 0, 0}, |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2076 | { OP_Dup, 0, 0, 0}, |
| 2077 | { OP_Integer, 0, 0, 0}, |
| 2078 | { OP_Ne, 0, 6, 0}, |
| 2079 | { OP_Integer, MAX_PAGES,0, 0}, |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2080 | { OP_ColumnCount, 1, 0, 0}, |
| 2081 | { OP_ColumnName, 0, 0, "cache_size"}, |
| 2082 | { OP_Callback, 1, 0, 0}, |
| 2083 | }; |
| 2084 | Vdbe *v = sqliteGetVdbe(pParse); |
| 2085 | if( v==0 ) return; |
| 2086 | if( pRight->z==pLeft->z ){ |
| 2087 | sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); |
| 2088 | }else{ |
| 2089 | int addr; |
| 2090 | int size = atoi(zRight); |
| 2091 | if( size<0 ) size = -size; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2092 | sqliteBeginWriteOperation(pParse, 0, 0); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2093 | sqliteVdbeAddOp(v, OP_Integer, size, 0); |
| 2094 | sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2); |
| 2095 | addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 2096 | sqliteVdbeAddOp(v, OP_Ge, 0, addr+3); |
| 2097 | sqliteVdbeAddOp(v, OP_Negative, 0, 0); |
| 2098 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 2); |
| 2099 | sqliteEndWriteOperation(pParse); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2100 | db->cache_size = db->cache_size<0 ? -size : size; |
| 2101 | sqliteBtreeSetCacheSize(db->pBe, db->cache_size); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2102 | } |
| 2103 | }else |
| 2104 | |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2105 | /* |
| 2106 | ** PRAGMA cache_size |
| 2107 | ** PRAGMA cache_size=N |
| 2108 | ** |
| 2109 | ** The first form reports the current local setting for the |
| 2110 | ** page cache size. The local setting can be different from |
| 2111 | ** the persistent cache size value that is stored in the database |
| 2112 | ** file itself. The value returned is the maximum number of |
| 2113 | ** pages in the page cache. The second form sets the local |
| 2114 | ** page cache size value. It does not change the persistent |
| 2115 | ** cache size stored on the disk so the cache size will revert |
| 2116 | ** to its default value when the database is closed and reopened. |
| 2117 | ** N should be a positive integer. |
| 2118 | */ |
| 2119 | if( sqliteStrICmp(zLeft,"cache_size")==0 ){ |
| 2120 | static VdbeOp getCacheSize[] = { |
| 2121 | { OP_ColumnCount, 1, 0, 0}, |
| 2122 | { OP_ColumnName, 0, 0, "cache_size"}, |
| 2123 | { OP_Callback, 1, 0, 0}, |
| 2124 | }; |
| 2125 | Vdbe *v = sqliteGetVdbe(pParse); |
| 2126 | if( v==0 ) return; |
| 2127 | if( pRight->z==pLeft->z ){ |
| 2128 | int size = db->cache_size;; |
| 2129 | if( size<0 ) size = -size; |
| 2130 | sqliteVdbeAddOp(v, OP_Integer, size, 0); |
| 2131 | sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); |
| 2132 | }else{ |
| 2133 | int size = atoi(zRight); |
| 2134 | if( size<0 ) size = -size; |
| 2135 | if( db->cache_size<0 ) size = -size; |
| 2136 | db->cache_size = size; |
| 2137 | sqliteBtreeSetCacheSize(db->pBe, db->cache_size); |
| 2138 | } |
| 2139 | }else |
| 2140 | |
| 2141 | /* |
| 2142 | ** PRAGMA default_synchronous |
| 2143 | ** PRAGMA default_synchronous=BOOLEAN |
| 2144 | ** |
| 2145 | ** The first form returns the persistent value of the "synchronous" setting |
| 2146 | ** that is stored in the database. This is the synchronous setting that |
| 2147 | ** is used whenever the database is opened unless overridden by a separate |
| 2148 | ** "synchronous" pragma. The second form changes the persistent and the |
| 2149 | ** local synchronous setting to the value given. |
| 2150 | ** |
| 2151 | ** If synchronous is on, SQLite will do an fsync() system call at strategic |
| 2152 | ** points to insure that all previously written data has actually been |
| 2153 | ** written onto the disk surface before continuing. This mode insures that |
| 2154 | ** the database will always be in a consistent state event if the operating |
| 2155 | ** system crashes or power to the computer is interrupted unexpectedly. |
| 2156 | ** When synchronous is off, SQLite will not wait for changes to actually |
| 2157 | ** be written to the disk before continuing. As soon as it hands changes |
| 2158 | ** to the operating system, it assumes that the changes are permanent and |
| 2159 | ** it continues going. The database cannot be corrupted by a program crash |
| 2160 | ** even with synchronous off, but an operating system crash or power loss |
| 2161 | ** could potentially corrupt data. On the other hand, synchronous off is |
| 2162 | ** faster than synchronous on. |
| 2163 | */ |
| 2164 | if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2165 | static VdbeOp getSync[] = { |
| 2166 | { OP_Integer, 0, 0, 0}, |
| 2167 | { OP_ReadCookie, 0, 2, 0}, |
| 2168 | { OP_Integer, 0, 0, 0}, |
| 2169 | { OP_Lt, 0, 5, 0}, |
| 2170 | { OP_AddImm, 1, 0, 0}, |
| 2171 | { OP_ColumnCount, 1, 0, 0}, |
| 2172 | { OP_ColumnName, 0, 0, "synchronous"}, |
| 2173 | { OP_Callback, 1, 0, 0}, |
| 2174 | }; |
| 2175 | Vdbe *v = sqliteGetVdbe(pParse); |
| 2176 | if( v==0 ) return; |
| 2177 | if( pRight->z==pLeft->z ){ |
| 2178 | sqliteVdbeAddOpList(v, ArraySize(getSync), getSync); |
| 2179 | }else{ |
| 2180 | int addr; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2181 | int size = db->cache_size; |
| 2182 | if( size<0 ) size = -size; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2183 | sqliteBeginWriteOperation(pParse, 0, 0); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2184 | sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2185 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 2186 | addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 2187 | sqliteVdbeAddOp(v, OP_Ne, 0, addr+3); |
| 2188 | sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2189 | sqliteVdbeAddOp(v, OP_AbsValue, 0, 0); |
| 2190 | if( !getBoolean(zRight) ){ |
| 2191 | sqliteVdbeAddOp(v, OP_Negative, 0, 0); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2192 | size = -size; |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2193 | } |
| 2194 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 2); |
| 2195 | sqliteEndWriteOperation(pParse); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2196 | db->cache_size = size; |
| 2197 | sqliteBtreeSetCacheSize(db->pBe, db->cache_size); |
| 2198 | } |
| 2199 | }else |
| 2200 | |
| 2201 | /* |
| 2202 | ** PRAGMA synchronous |
| 2203 | ** PRAGMA synchronous=BOOLEAN |
| 2204 | ** |
| 2205 | ** Return or set the local value of the synchronous flag. Changing |
| 2206 | ** the local value does not make changes to the disk file and the |
| 2207 | ** default value will be restored the next time the database is |
| 2208 | ** opened. |
| 2209 | */ |
| 2210 | if( sqliteStrICmp(zLeft,"synchronous")==0 ){ |
| 2211 | static VdbeOp getSync[] = { |
| 2212 | { OP_ColumnCount, 1, 0, 0}, |
| 2213 | { OP_ColumnName, 0, 0, "synchronous"}, |
| 2214 | { OP_Callback, 1, 0, 0}, |
| 2215 | }; |
| 2216 | Vdbe *v = sqliteGetVdbe(pParse); |
| 2217 | if( v==0 ) return; |
| 2218 | if( pRight->z==pLeft->z ){ |
| 2219 | sqliteVdbeAddOp(v, OP_Integer, db->cache_size>=0, 0); |
| 2220 | sqliteVdbeAddOpList(v, ArraySize(getSync), getSync); |
| 2221 | }else{ |
| 2222 | int size = db->cache_size; |
| 2223 | if( size<0 ) size = -size; |
| 2224 | if( !getBoolean(zRight) ) size = -size; |
| 2225 | db->cache_size = size; |
| 2226 | sqliteBtreeSetCacheSize(db->pBe, db->cache_size); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2227 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2228 | }else |
| 2229 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 2230 | if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){ |
| 2231 | if( getBoolean(zRight) ){ |
| 2232 | always_code_trigger_setup = 1; |
| 2233 | }else{ |
| 2234 | always_code_trigger_setup = 0; |
| 2235 | } |
| 2236 | }else |
| 2237 | |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2238 | if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){ |
| 2239 | if( getBoolean(zRight) ){ |
| 2240 | db->flags |= SQLITE_VdbeTrace; |
| 2241 | }else{ |
| 2242 | db->flags &= ~SQLITE_VdbeTrace; |
| 2243 | } |
| 2244 | }else |
| 2245 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2246 | if( sqliteStrICmp(zLeft, "full_column_names")==0 ){ |
| 2247 | if( getBoolean(zRight) ){ |
| 2248 | db->flags |= SQLITE_FullColNames; |
| 2249 | }else{ |
| 2250 | db->flags &= ~SQLITE_FullColNames; |
| 2251 | } |
| 2252 | }else |
| 2253 | |
drh | 5080aaa | 2002-07-11 12:18:16 +0000 | [diff] [blame] | 2254 | if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){ |
| 2255 | if( getBoolean(zRight) ){ |
| 2256 | db->flags |= SQLITE_ReportTypes; |
| 2257 | }else{ |
| 2258 | db->flags &= ~SQLITE_ReportTypes; |
| 2259 | } |
| 2260 | }else |
| 2261 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 2262 | if( sqliteStrICmp(zLeft, "result_set_details")==0 ){ |
| 2263 | if( getBoolean(zRight) ){ |
| 2264 | db->flags |= SQLITE_ResultDetails; |
| 2265 | }else{ |
| 2266 | db->flags &= ~SQLITE_ResultDetails; |
| 2267 | } |
| 2268 | }else |
| 2269 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2270 | if( sqliteStrICmp(zLeft, "count_changes")==0 ){ |
| 2271 | if( getBoolean(zRight) ){ |
| 2272 | db->flags |= SQLITE_CountRows; |
| 2273 | }else{ |
| 2274 | db->flags &= ~SQLITE_CountRows; |
| 2275 | } |
| 2276 | }else |
| 2277 | |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2278 | if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){ |
| 2279 | if( getBoolean(zRight) ){ |
| 2280 | db->flags |= SQLITE_NullCallback; |
| 2281 | }else{ |
| 2282 | db->flags &= ~SQLITE_NullCallback; |
| 2283 | } |
| 2284 | }else |
| 2285 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2286 | if( sqliteStrICmp(zLeft, "table_info")==0 ){ |
| 2287 | Table *pTab; |
| 2288 | Vdbe *v; |
| 2289 | pTab = sqliteFindTable(db, zRight); |
| 2290 | if( pTab ) v = sqliteGetVdbe(pParse); |
| 2291 | if( pTab && v ){ |
| 2292 | static VdbeOp tableInfoPreface[] = { |
| 2293 | { OP_ColumnCount, 5, 0, 0}, |
| 2294 | { OP_ColumnName, 0, 0, "cid"}, |
| 2295 | { OP_ColumnName, 1, 0, "name"}, |
| 2296 | { OP_ColumnName, 2, 0, "type"}, |
| 2297 | { OP_ColumnName, 3, 0, "notnull"}, |
| 2298 | { OP_ColumnName, 4, 0, "dflt_value"}, |
| 2299 | }; |
| 2300 | int i; |
| 2301 | sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2302 | sqliteViewGetColumnNames(pParse, pTab); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2303 | for(i=0; i<pTab->nCol; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2304 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
| 2305 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 2306 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC); |
| 2307 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 2308 | sqliteVdbeChangeP3(v, -1, |
| 2309 | pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", P3_STATIC); |
| 2310 | sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0); |
| 2311 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 2312 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); |
| 2313 | sqliteVdbeAddOp(v, OP_Callback, 5, 0); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | }else |
| 2317 | |
| 2318 | if( sqliteStrICmp(zLeft, "index_info")==0 ){ |
| 2319 | Index *pIdx; |
| 2320 | Table *pTab; |
| 2321 | Vdbe *v; |
| 2322 | pIdx = sqliteFindIndex(db, zRight); |
| 2323 | if( pIdx ) v = sqliteGetVdbe(pParse); |
| 2324 | if( pIdx && v ){ |
| 2325 | static VdbeOp tableInfoPreface[] = { |
| 2326 | { OP_ColumnCount, 3, 0, 0}, |
| 2327 | { OP_ColumnName, 0, 0, "seqno"}, |
| 2328 | { OP_ColumnName, 1, 0, "cid"}, |
| 2329 | { OP_ColumnName, 2, 0, "name"}, |
| 2330 | }; |
| 2331 | int i; |
| 2332 | pTab = pIdx->pTable; |
| 2333 | sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface); |
| 2334 | for(i=0; i<pIdx->nColumn; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2335 | int cnum = pIdx->aiColumn[i]; |
| 2336 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
| 2337 | sqliteVdbeAddOp(v, OP_Integer, cnum, 0); |
| 2338 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2339 | assert( pTab->nCol>cnum ); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2340 | sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC); |
| 2341 | sqliteVdbeAddOp(v, OP_Callback, 3, 0); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2342 | } |
| 2343 | } |
| 2344 | }else |
| 2345 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2346 | if( sqliteStrICmp(zLeft, "index_list")==0 ){ |
| 2347 | Index *pIdx; |
| 2348 | Table *pTab; |
| 2349 | Vdbe *v; |
| 2350 | pTab = sqliteFindTable(db, zRight); |
| 2351 | if( pTab ){ |
| 2352 | v = sqliteGetVdbe(pParse); |
| 2353 | pIdx = pTab->pIndex; |
| 2354 | } |
| 2355 | if( pTab && pIdx && v ){ |
| 2356 | int i = 0; |
| 2357 | static VdbeOp indexListPreface[] = { |
| 2358 | { OP_ColumnCount, 3, 0, 0}, |
| 2359 | { OP_ColumnName, 0, 0, "seq"}, |
| 2360 | { OP_ColumnName, 1, 0, "name"}, |
| 2361 | { OP_ColumnName, 2, 0, "unique"}, |
| 2362 | }; |
| 2363 | |
| 2364 | sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface); |
| 2365 | while(pIdx){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2366 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
| 2367 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 2368 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2369 | sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2370 | sqliteVdbeAddOp(v, OP_Callback, 3, 0); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 2371 | ++i; |
| 2372 | pIdx = pIdx->pNext; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | }else |
| 2376 | |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2377 | #ifndef NDEBUG |
| 2378 | if( sqliteStrICmp(zLeft, "parser_trace")==0 ){ |
| 2379 | extern void sqliteParserTrace(FILE*, char *); |
| 2380 | if( getBoolean(zRight) ){ |
| 2381 | sqliteParserTrace(stdout, "parser: "); |
| 2382 | }else{ |
| 2383 | sqliteParserTrace(0, 0); |
| 2384 | } |
| 2385 | }else |
| 2386 | #endif |
| 2387 | |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2388 | if( sqliteStrICmp(zLeft, "integrity_check")==0 ){ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2389 | static VdbeOp checkDb[] = { |
| 2390 | { OP_SetInsert, 0, 0, "2"}, |
| 2391 | { OP_Open, 0, 2, 0}, |
| 2392 | { OP_Rewind, 0, 6, 0}, |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 2393 | { OP_Column, 0, 3, 0}, /* 3 */ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2394 | { OP_SetInsert, 0, 0, 0}, |
| 2395 | { OP_Next, 0, 3, 0}, |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 2396 | { OP_IntegrityCk, 0, 0, 0}, /* 6 */ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2397 | { OP_ColumnCount, 1, 0, 0}, |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2398 | { OP_ColumnName, 0, 0, "integrity_check"}, |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2399 | { OP_Callback, 1, 0, 0}, |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 2400 | { OP_SetInsert, 1, 0, "2"}, |
| 2401 | { OP_OpenAux, 1, 2, 0}, |
| 2402 | { OP_Rewind, 1, 16, 0}, |
| 2403 | { OP_Column, 1, 3, 0}, /* 13 */ |
| 2404 | { OP_SetInsert, 1, 0, 0}, |
| 2405 | { OP_Next, 1, 13, 0}, |
| 2406 | { OP_IntegrityCk, 1, 1, 0}, /* 16 */ |
| 2407 | { OP_Callback, 1, 0, 0}, |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2408 | }; |
| 2409 | Vdbe *v = sqliteGetVdbe(pParse); |
| 2410 | if( v==0 ) return; |
| 2411 | sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb); |
| 2412 | }else |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2413 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2414 | {} |
| 2415 | sqliteFree(zLeft); |
| 2416 | sqliteFree(zRight); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2417 | } |