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