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 |
| 20 | ** creating expressions and ID lists |
| 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 | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame^] | 28 | ** $Id: build.c,v 1.74 2002/02/03 17:37:36 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 | ** Construct a new expression node and return a pointer to it. Memory |
| 69 | ** for this node is obtained from sqliteMalloc(). The calling function |
| 70 | ** is responsible for making sure the node eventually gets freed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 71 | */ |
| 72 | Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ |
| 73 | Expr *pNew; |
| 74 | pNew = sqliteMalloc( sizeof(Expr) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 75 | if( pNew==0 ){ |
| 76 | sqliteExprDelete(pLeft); |
| 77 | sqliteExprDelete(pRight); |
| 78 | return 0; |
| 79 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 80 | pNew->op = op; |
| 81 | pNew->pLeft = pLeft; |
| 82 | pNew->pRight = pRight; |
| 83 | if( pToken ){ |
| 84 | pNew->token = *pToken; |
| 85 | }else{ |
| 86 | pNew->token.z = ""; |
| 87 | pNew->token.n = 0; |
| 88 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 89 | if( pLeft && pRight ){ |
| 90 | sqliteExprSpan(pNew, &pLeft->span, &pRight->span); |
| 91 | }else{ |
| 92 | pNew->span = pNew->token; |
| 93 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 94 | return pNew; |
| 95 | } |
| 96 | |
| 97 | /* |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 98 | ** Set the Expr.token field of the given expression to span all |
| 99 | ** text between the two given tokens. |
| 100 | */ |
| 101 | void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 102 | if( pExpr ){ |
| 103 | pExpr->span.z = pLeft->z; |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 104 | pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 105 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 109 | ** Construct a new expression node for a function with multiple |
| 110 | ** arguments. |
| 111 | */ |
| 112 | Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ |
| 113 | Expr *pNew; |
| 114 | pNew = sqliteMalloc( sizeof(Expr) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 115 | if( pNew==0 ){ |
| 116 | sqliteExprListDelete(pList); |
| 117 | return 0; |
| 118 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 119 | pNew->op = TK_FUNCTION; |
| 120 | pNew->pList = pList; |
| 121 | if( pToken ){ |
| 122 | pNew->token = *pToken; |
| 123 | }else{ |
| 124 | pNew->token.z = ""; |
| 125 | pNew->token.n = 0; |
| 126 | } |
| 127 | return pNew; |
| 128 | } |
| 129 | |
| 130 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 131 | ** Locate the in-memory structure that describes |
| 132 | ** a particular database table given the name |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 133 | ** of that table. Return NULL if not found. |
| 134 | */ |
| 135 | Table *sqliteFindTable(sqlite *db, char *zName){ |
drh | afa4a02 | 2001-09-24 03:12:39 +0000 | [diff] [blame] | 136 | Table *p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 137 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 141 | ** Locate the in-memory structure that describes |
| 142 | ** a particular index given the name of that index. |
| 143 | ** Return NULL if not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 144 | */ |
| 145 | Index *sqliteFindIndex(sqlite *db, char *zName){ |
drh | afa4a02 | 2001-09-24 03:12:39 +0000 | [diff] [blame] | 146 | Index *p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 147 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* |
| 151 | ** Remove the given index from the index hash table, and free |
| 152 | ** its memory structures. |
| 153 | ** |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 154 | ** The index is removed from the database hash tables but |
| 155 | ** it is not unlinked from the Table that it indexes. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 156 | ** Unlinking from the Table must be done by the calling function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 157 | */ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 158 | static void sqliteDeleteIndex(sqlite *db, Index *p){ |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 159 | Index *pOld; |
| 160 | assert( db!=0 && p->zName!=0 ); |
| 161 | pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0); |
| 162 | if( pOld!=0 && pOld!=p ){ |
| 163 | sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 164 | } |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 165 | sqliteHashInsert(&db->idxDrop, p, 0, 0); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 166 | sqliteFree(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 170 | ** Unlink the given index from its table, then remove |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 171 | ** the index from the index hash table and free its memory |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 172 | ** structures. |
| 173 | */ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 174 | void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 175 | if( pIndex->pTable->pIndex==pIndex ){ |
| 176 | pIndex->pTable->pIndex = pIndex->pNext; |
| 177 | }else{ |
| 178 | Index *p; |
| 179 | for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
| 180 | if( p && p->pNext==pIndex ){ |
| 181 | p->pNext = pIndex->pNext; |
| 182 | } |
| 183 | } |
| 184 | sqliteDeleteIndex(db, pIndex); |
| 185 | } |
| 186 | |
| 187 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 188 | ** Move the given index to the pending DROP INDEX queue if it has |
| 189 | ** been committed. If this index was never committed, then just |
| 190 | ** delete it. |
| 191 | ** |
| 192 | ** Indices on the pending drop queue are deleted when a COMMIT is |
| 193 | ** executed. If a ROLLBACK occurs, the indices are moved back into |
| 194 | ** the main index hash table. |
| 195 | */ |
drh | da9e034 | 2002-01-10 14:31:48 +0000 | [diff] [blame] | 196 | static void sqlitePendingDropIndex(sqlite *db, Index *p){ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 197 | if( !p->isCommit ){ |
| 198 | sqliteUnlinkAndDeleteIndex(db, p); |
| 199 | }else{ |
| 200 | Index *pOld; |
| 201 | pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0); |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 202 | if( pOld!=0 && pOld!=p ){ |
| 203 | sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld); |
| 204 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 205 | sqliteHashInsert(&db->idxDrop, p, 0, p); |
| 206 | p->isDropped = 1; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 212 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 213 | ** |
| 214 | ** This routine just deletes the data structure. It does not unlink |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 215 | ** the table data structure from the hash table. But it does destroy |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 216 | ** memory structures of the indices associated with the table. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 217 | ** |
| 218 | ** Indices associated with the table are unlinked from the "db" |
| 219 | ** data structure if db!=NULL. If db==NULL, indices attached to |
| 220 | ** the table are deleted, but it is assumed they have already been |
| 221 | ** unlinked. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 222 | */ |
| 223 | void sqliteDeleteTable(sqlite *db, Table *pTable){ |
| 224 | int i; |
| 225 | Index *pIndex, *pNext; |
| 226 | if( pTable==0 ) return; |
| 227 | for(i=0; i<pTable->nCol; i++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 228 | sqliteFree(pTable->aCol[i].zName); |
| 229 | sqliteFree(pTable->aCol[i].zDflt); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 230 | sqliteFree(pTable->aCol[i].zType); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 231 | } |
| 232 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 233 | pNext = pIndex->pNext; |
| 234 | sqliteDeleteIndex(db, pIndex); |
| 235 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 236 | sqliteFree(pTable->zName); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 237 | sqliteFree(pTable->aCol); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 238 | sqliteFree(pTable); |
| 239 | } |
| 240 | |
| 241 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 242 | ** Unlink the given table from the hash tables and the delete the |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 243 | ** table structure with all its indices. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 244 | */ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 245 | static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){ |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 246 | Table *pOld; |
| 247 | assert( db!=0 ); |
| 248 | pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0); |
| 249 | assert( pOld==0 || pOld==p ); |
| 250 | sqliteHashInsert(&db->tblDrop, p, 0, 0); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 251 | sqliteDeleteTable(db, p); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | ** Move the given table to the pending DROP TABLE queue if it has |
| 256 | ** been committed. If this table was never committed, then just |
| 257 | ** delete it. Do the same for all its indices. |
| 258 | ** |
| 259 | ** Table on the drop queue are not actually deleted until a COMMIT |
| 260 | ** statement is executed. If a ROLLBACK occurs instead of a COMMIT, |
| 261 | ** then the tables on the drop queue are moved back into the main |
| 262 | ** hash table. |
| 263 | */ |
drh | da9e034 | 2002-01-10 14:31:48 +0000 | [diff] [blame] | 264 | static void sqlitePendingDropTable(sqlite *db, Table *pTbl){ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 265 | if( !pTbl->isCommit ){ |
| 266 | sqliteUnlinkAndDeleteTable(db, pTbl); |
| 267 | }else{ |
| 268 | Table *pOld; |
| 269 | Index *pIndex, *pNext; |
| 270 | pOld = sqliteHashInsert(&db->tblHash, pTbl->zName, strlen(pTbl->zName)+1,0); |
| 271 | assert( pOld==pTbl ); |
| 272 | sqliteHashInsert(&db->tblDrop, pTbl, 0, pTbl); |
| 273 | for(pIndex = pTbl->pIndex; pIndex; pIndex=pNext){ |
| 274 | pNext = pIndex->pNext; |
| 275 | sqlitePendingDropIndex(db, pIndex); |
| 276 | } |
| 277 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 281 | ** Check all Tables and Indexes in the internal hash table and commit |
| 282 | ** any additions or deletions to those hash tables. |
| 283 | ** |
| 284 | ** When executing CREATE TABLE and CREATE INDEX statements, the Table |
| 285 | ** and Index structures are created and added to the hash tables, but |
| 286 | ** the "isCommit" field is not set. This routine sets those fields. |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 287 | ** When executing DROP TABLE and DROP INDEX, the table or index structures |
| 288 | ** are moved out of tblHash and idxHash into tblDrop and idxDrop. This |
| 289 | ** routine deletes the structure in tblDrop and idxDrop. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 290 | ** |
| 291 | ** See also: sqliteRollbackInternalChanges() |
| 292 | */ |
| 293 | void sqliteCommitInternalChanges(sqlite *db){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 294 | HashElem *pElem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 295 | if( (db->flags & SQLITE_InternChanges)==0 ) return; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 296 | db->schema_cookie = db->next_cookie; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 297 | for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){ |
| 298 | Table *pTable = sqliteHashData(pElem); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 299 | pTable->isCommit = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 300 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 301 | for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 302 | Table *pTable = sqliteHashData(pElem); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 303 | sqliteDeleteTable(db, pTable); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 304 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 305 | sqliteHashClear(&db->tblDrop); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 306 | for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 307 | Index *pIndex = sqliteHashData(pElem); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 308 | pIndex->isCommit = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 309 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 310 | while( (pElem=sqliteHashFirst(&db->idxDrop))!=0 ){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 311 | Index *pIndex = sqliteHashData(pElem); |
| 312 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
| 313 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 314 | sqliteHashClear(&db->idxDrop); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 315 | db->flags &= ~SQLITE_InternChanges; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | ** This routine runs when one or more CREATE TABLE, CREATE INDEX, |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 320 | ** DROP TABLE, or DROP INDEX statements gets rolled back. The |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 321 | ** additions or deletions of Table and Index structures in the |
| 322 | ** internal hash tables are undone. |
| 323 | ** |
| 324 | ** See also: sqliteCommitInternalChanges() |
| 325 | */ |
| 326 | void sqliteRollbackInternalChanges(sqlite *db){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 327 | Hash toDelete; |
| 328 | HashElem *pElem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 329 | if( (db->flags & SQLITE_InternChanges)==0 ) return; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 330 | sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 331 | db->next_cookie = db->schema_cookie; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 332 | for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){ |
| 333 | Table *pTable = sqliteHashData(pElem); |
| 334 | if( !pTable->isCommit ){ |
| 335 | sqliteHashInsert(&toDelete, pTable, 0, pTable); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 338 | for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){ |
| 339 | Table *pTable = sqliteHashData(pElem); |
| 340 | sqliteUnlinkAndDeleteTable(db, pTable); |
| 341 | } |
| 342 | sqliteHashClear(&toDelete); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 343 | for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){ |
| 344 | Table *pOld, *p = sqliteHashData(pElem); |
| 345 | assert( p->isCommit ); |
| 346 | pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p); |
| 347 | assert( pOld==0 || pOld==p ); |
| 348 | } |
| 349 | sqliteHashClear(&db->tblDrop); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 350 | for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 351 | Index *pIndex = sqliteHashData(pElem); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 352 | if( !pIndex->isCommit ){ |
| 353 | sqliteHashInsert(&toDelete, pIndex, 0, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 356 | for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){ |
| 357 | Index *pIndex = sqliteHashData(pElem); |
| 358 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
| 359 | } |
| 360 | sqliteHashClear(&toDelete); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 361 | for(pElem=sqliteHashFirst(&db->idxDrop); pElem; pElem=sqliteHashNext(pElem)){ |
| 362 | Index *pOld, *p = sqliteHashData(pElem); |
| 363 | assert( p->isCommit ); |
| 364 | p->isDropped = 0; |
| 365 | pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, p); |
| 366 | assert( pOld==0 || pOld==p ); |
| 367 | } |
| 368 | sqliteHashClear(&db->idxDrop); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 369 | db->flags &= ~SQLITE_InternChanges; |
| 370 | } |
| 371 | |
| 372 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 373 | ** Construct the name of a user table or index from a token. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 374 | ** |
| 375 | ** Space to hold the name is obtained from sqliteMalloc() and must |
| 376 | ** be freed by the calling function. |
| 377 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 378 | char *sqliteTableNameFromToken(Token *pName){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 379 | char *zName = sqliteStrNDup(pName->z, pName->n); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 380 | sqliteDequote(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 381 | return zName; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | ** Begin constructing a new table representation in memory. This is |
| 386 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 387 | ** to a CREATE TABLE statement. In particular, this routine is called |
| 388 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 389 | ** pStart token is the CREATE and pName is the table name. The isTemp |
| 390 | ** flag is true if the "TEMP" or "TEMPORARY" keyword occurs in between |
| 391 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 392 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 393 | ** The new table record is initialized and put in pParse->pNewTable. |
| 394 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 395 | ** routines will be called to add more information to this record. |
| 396 | ** At the end of the CREATE TABLE statement, the sqliteEndTable() routine |
| 397 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 398 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 399 | void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 400 | Table *pTable; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 401 | Index *pIdx; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 402 | char *zName; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 403 | sqlite *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 404 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 405 | |
| 406 | pParse->sFirstToken = *pStart; |
| 407 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 408 | if( zName==0 ) return; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 409 | |
| 410 | /* Before trying to create a temporary table, make sure the Btree for |
| 411 | ** holding temporary tables is open. |
| 412 | */ |
| 413 | if( isTemp && db->pBeTemp==0 ){ |
| 414 | int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp); |
| 415 | if( rc!=SQLITE_OK ){ |
| 416 | sqliteSetNString(&pParse->zErrMsg, "unable to open a temporary database " |
| 417 | "file for storing temporary tables", 0); |
| 418 | pParse->nErr++; |
| 419 | return; |
| 420 | } |
| 421 | if( db->flags & SQLITE_InTrans ){ |
| 422 | rc = sqliteBtreeBeginTrans(db->pBeTemp); |
| 423 | if( rc!=SQLITE_OK ){ |
| 424 | sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on " |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 425 | "the temporary database file", 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 426 | pParse->nErr++; |
| 427 | return; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* Make sure the new table name does not collide with an existing |
| 433 | ** index or table name. Issue an error message if it does. |
| 434 | ** |
| 435 | ** If we are re-reading the sqlite_master table because of a schema |
| 436 | ** change and a new permanent table is found whose name collides with |
| 437 | ** an existing temporary table, then ignore the new permanent table. |
| 438 | ** We will continue parsing, but the pParse->nameClash flag will be set |
| 439 | ** so we will know to discard the table record once parsing has finished. |
| 440 | */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 441 | pTable = sqliteFindTable(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 442 | if( pTable!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 443 | if( pTable->isTemp && pParse->initFlag ){ |
| 444 | pParse->nameClash = 1; |
| 445 | }else{ |
| 446 | sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n, |
| 447 | " already exists", 0, 0); |
| 448 | sqliteFree(zName); |
| 449 | pParse->nErr++; |
| 450 | return; |
| 451 | } |
| 452 | }else{ |
| 453 | pParse->nameClash = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 454 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 455 | if( (pIdx = sqliteFindIndex(db, zName))!=0 && |
| 456 | (!pIdx->pTable->isTemp || !pParse->initFlag) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 457 | sqliteSetString(&pParse->zErrMsg, "there is already an index named ", |
| 458 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 459 | sqliteFree(zName); |
| 460 | pParse->nErr++; |
| 461 | return; |
| 462 | } |
| 463 | pTable = sqliteMalloc( sizeof(Table) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 464 | if( pTable==0 ){ |
| 465 | sqliteFree(zName); |
| 466 | return; |
| 467 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 468 | pTable->zName = zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 469 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 470 | pTable->aCol = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 471 | pTable->iPKey = -1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 472 | pTable->pIndex = 0; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 473 | pTable->isTemp = isTemp; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 474 | if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 475 | pParse->pNewTable = pTable; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 476 | if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 477 | sqliteBeginWriteOperation(pParse); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 478 | if( !isTemp ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 479 | sqliteVdbeAddOp(v, OP_SetCookie, db->file_format, 1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 480 | sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2); |
| 481 | sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 482 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 483 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* |
| 487 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 488 | ** |
| 489 | ** The parser calls this routine once for each column declaration |
| 490 | ** in a CREATE TABLE statement. sqliteStartTable() gets called |
| 491 | ** first to get things going. Then this routine is called for each |
| 492 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 493 | */ |
| 494 | void sqliteAddColumn(Parse *pParse, Token *pName){ |
| 495 | Table *p; |
| 496 | char **pz; |
| 497 | if( (p = pParse->pNewTable)==0 ) return; |
| 498 | if( (p->nCol & 0x7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 499 | Column *aNew; |
| 500 | aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
| 501 | if( aNew==0 ) return; |
| 502 | p->aCol = aNew; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 503 | } |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 504 | memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0])); |
| 505 | pz = &p->aCol[p->nCol++].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 506 | sqliteSetNString(pz, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 507 | sqliteDequote(*pz); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 511 | ** This routine is called by the parser while in the middle of |
| 512 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 513 | ** been seen on a column. This routine sets the notNull flag on |
| 514 | ** the column currently under construction. |
| 515 | */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 516 | void sqliteAddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 517 | Table *p; |
| 518 | int i; |
| 519 | if( (p = pParse->pNewTable)==0 ) return; |
| 520 | i = p->nCol-1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 521 | if( i>=0 ) p->aCol[i].notNull = onError; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* |
| 525 | ** This routine is called by the parser while in the middle of |
| 526 | ** parsing a CREATE TABLE statement. The pFirst token is the first |
| 527 | ** token in the sequence of tokens that describe the type of the |
| 528 | ** column currently under construction. pLast is the last token |
| 529 | ** in the sequence. Use this information to construct a string |
| 530 | ** that contains the typename of the column and store that string |
| 531 | ** in zType. |
| 532 | */ |
| 533 | void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){ |
| 534 | Table *p; |
| 535 | int i, j; |
| 536 | int n; |
| 537 | char *z, **pz; |
| 538 | if( (p = pParse->pNewTable)==0 ) return; |
| 539 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 540 | if( i<0 ) return; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 541 | pz = &p->aCol[i].zType; |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 542 | n = pLast->n + Addr(pLast->z) - Addr(pFirst->z); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 543 | sqliteSetNString(pz, pFirst->z, n, 0); |
| 544 | z = *pz; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 545 | if( z==0 ) return; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 546 | for(i=j=0; z[i]; i++){ |
| 547 | int c = z[i]; |
| 548 | if( isspace(c) ) continue; |
| 549 | z[j++] = c; |
| 550 | } |
| 551 | z[j] = 0; |
| 552 | } |
| 553 | |
| 554 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 555 | ** The given token is the default value for the last column added to |
| 556 | ** the table currently under construction. If "minusFlag" is true, it |
| 557 | ** means the value token was preceded by a minus sign. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 558 | ** |
| 559 | ** This routine is called by the parser while in the middle of |
| 560 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 561 | */ |
| 562 | void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ |
| 563 | Table *p; |
| 564 | int i; |
| 565 | char **pz; |
| 566 | if( (p = pParse->pNewTable)==0 ) return; |
| 567 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 568 | if( i<0 ) return; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 569 | pz = &p->aCol[i].zDflt; |
| 570 | if( minusFlag ){ |
| 571 | sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0); |
| 572 | }else{ |
| 573 | sqliteSetNString(pz, pVal->z, pVal->n, 0); |
| 574 | } |
| 575 | sqliteDequote(*pz); |
| 576 | } |
| 577 | |
| 578 | /* |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 579 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
| 580 | ** of columns that form the primary key. If pList is NULL, then the |
| 581 | ** most recently added column of the table is the primary key. |
| 582 | ** |
| 583 | ** A table can have at most one primary key. If the table already has |
| 584 | ** a primary key (and this is the second primary key) then create an |
| 585 | ** error. |
| 586 | ** |
| 587 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
| 588 | ** then we will try to use that column as the row id. (Exception: |
| 589 | ** For backwards compatibility with older databases, do not do this |
| 590 | ** if the file format version number is less than 1.) Set the Table.iPKey |
| 591 | ** field of the table under construction to be the index of the |
| 592 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 593 | ** no INTEGER PRIMARY KEY. |
| 594 | ** |
| 595 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 596 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 597 | */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 598 | void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 599 | Table *pTab = pParse->pNewTable; |
| 600 | char *zType = 0; |
| 601 | int iCol = -1; |
| 602 | if( pTab==0 ) return; |
| 603 | if( pTab->hasPrimKey ){ |
| 604 | sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName, |
| 605 | "\" has more than one primary key", 0); |
| 606 | pParse->nErr++; |
| 607 | return; |
| 608 | } |
| 609 | pTab->hasPrimKey = 1; |
| 610 | if( pList==0 ){ |
| 611 | iCol = pTab->nCol - 1; |
| 612 | }else if( pList->nId==1 ){ |
| 613 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 614 | if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break; |
| 615 | } |
| 616 | } |
| 617 | if( iCol>=0 && iCol<pTab->nCol ){ |
| 618 | zType = pTab->aCol[iCol].zType; |
| 619 | } |
| 620 | if( pParse->db->file_format>=1 && |
| 621 | zType && sqliteStrICmp(zType, "INTEGER")==0 ){ |
| 622 | pTab->iPKey = iCol; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 623 | pTab->keyConf = onError; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 624 | }else{ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 625 | sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 630 | ** Come up with a new random value for the schema cookie. Make sure |
| 631 | ** the new value is different from the old. |
| 632 | ** |
| 633 | ** The schema cookie is used to determine when the schema for the |
| 634 | ** database changes. After each schema change, the cookie value |
| 635 | ** changes. When a process first reads the schema it records the |
| 636 | ** cookie. Thereafter, whenever it goes to access the database, |
| 637 | ** it checks the cookie to make sure the schema has not changed |
| 638 | ** since it was last read. |
| 639 | ** |
| 640 | ** This plan is not completely bullet-proof. It is possible for |
| 641 | ** the schema to change multiple times and for the cookie to be |
| 642 | ** set back to prior value. But schema changes are infrequent |
| 643 | ** and the probability of hitting the same cookie value is only |
| 644 | ** 1 chance in 2^32. So we're safe enough. |
| 645 | */ |
| 646 | static void changeCookie(sqlite *db){ |
| 647 | if( db->next_cookie==db->schema_cookie ){ |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 648 | db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 649 | db->flags |= SQLITE_InternChanges; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 654 | ** This routine is called to report the final ")" that terminates |
| 655 | ** a CREATE TABLE statement. |
| 656 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 657 | ** The table structure that other action routines have been building |
| 658 | ** is added to the internal hash tables, assuming no errors have |
| 659 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 660 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 661 | ** An entry for the table is made in the master table on disk, |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 662 | ** unless this is a temporary table or initFlag==1. When initFlag==1, |
| 663 | ** it means we are reading the sqlite_master table because we just |
| 664 | ** connected to the database or because the sqlite_master table has |
| 665 | ** recently changes, so the entry for this table already exists in |
| 666 | ** the sqlite_master table. We do not want to create it again. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 667 | */ |
| 668 | void sqliteEndTable(Parse *pParse, Token *pEnd){ |
| 669 | Table *p; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 670 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 671 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 672 | if( pEnd==0 || pParse->nErr || sqlite_malloc_failed ) return; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 673 | p = pParse->pNewTable; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 674 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 675 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 676 | /* Add the table to the in-memory representation of the database. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 677 | */ |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 678 | assert( pParse->nameClash==0 || pParse->initFlag==1 ); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 679 | if( pParse->explain==0 && pParse->nameClash==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 680 | Table *pOld; |
| 681 | pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p); |
| 682 | if( pOld ){ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 683 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 684 | return; |
| 685 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 686 | pParse->pNewTable = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 687 | db->nTable++; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 688 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 689 | } |
| 690 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 691 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 692 | ** "sqlite_master" table on the disk. So do not write to the disk |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 693 | ** again. Extract the root page number for the table from the |
| 694 | ** pParse->newTnum field. (The page number should have been put |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 695 | ** there by the sqliteOpenCb routine.) |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 696 | */ |
| 697 | if( pParse->initFlag ){ |
| 698 | p->tnum = pParse->newTnum; |
| 699 | } |
| 700 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 701 | /* If not initializing, then create a record for the new table |
| 702 | ** in the SQLITE_MASTER table of the database. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 703 | ** |
| 704 | ** If this is a TEMPORARY table, then just create the table. Do not |
| 705 | ** make an entry in SQLITE_MASTER. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 706 | */ |
| 707 | if( !pParse->initFlag ){ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 708 | int n, addr; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 709 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 710 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 711 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 712 | if( v==0 ) return; |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 713 | n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 714 | if( !p->isTemp ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 715 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 716 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 717 | sqliteVdbeChangeP3(v, -1, "table", P3_STATIC); |
| 718 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 719 | sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC); |
| 720 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 721 | sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 722 | } |
drh | 653851f | 2001-12-15 02:35:59 +0000 | [diff] [blame] | 723 | addr = sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 724 | sqliteVdbeChangeP3(v, addr, (char *)&p->tnum, P3_POINTER); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 725 | p->tnum = 0; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 726 | if( !p->isTemp ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 727 | addr = sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 728 | sqliteVdbeChangeP3(v, addr, pParse->sFirstToken.z, n); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 729 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 730 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 731 | changeCookie(db); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 732 | sqliteVdbeAddOp(v, OP_SetCookie, db->next_cookie, 0); |
| 733 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 734 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 735 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | ** Given a token, look up a table with that name. If not found, leave |
| 741 | ** an error for the parser to find and return NULL. |
| 742 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 743 | Table *sqliteTableFromToken(Parse *pParse, Token *pTok){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 744 | char *zName; |
| 745 | Table *pTab; |
| 746 | zName = sqliteTableNameFromToken(pTok); |
| 747 | if( zName==0 ) return 0; |
| 748 | pTab = sqliteFindTable(pParse->db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 749 | sqliteFree(zName); |
| 750 | if( pTab==0 ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 751 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 752 | pTok->z, pTok->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 753 | pParse->nErr++; |
| 754 | } |
| 755 | return pTab; |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 760 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 761 | */ |
| 762 | void sqliteDropTable(Parse *pParse, Token *pName){ |
| 763 | Table *pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 764 | Vdbe *v; |
| 765 | int base; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 766 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 767 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 768 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 769 | pTable = sqliteTableFromToken(pParse, pName); |
| 770 | if( pTable==0 ) return; |
| 771 | if( pTable->readOnly ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 772 | sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName, |
| 773 | " may not be dropped", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 774 | pParse->nErr++; |
| 775 | return; |
| 776 | } |
| 777 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 778 | /* Generate code to remove the table from the master table |
| 779 | ** on disk. |
| 780 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 781 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 782 | if( v ){ |
| 783 | static VdbeOp dropTable[] = { |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 784 | { OP_OpenWrite, 0, 2, MASTER_NAME}, |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 785 | { OP_Rewind, 0, ADDR(9), 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 786 | { OP_String, 0, 0, 0}, /* 2 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 787 | { OP_MemStore, 1, 1, 0}, |
| 788 | { OP_MemLoad, 1, 0, 0}, /* 4 */ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 789 | { OP_Column, 0, 2, 0}, |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 790 | { OP_Ne, 0, ADDR(8), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 791 | { OP_Delete, 0, 0, 0}, |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 792 | { OP_Next, 0, ADDR(4), 0}, /* 8 */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 793 | { OP_SetCookie, 0, 0, 0}, /* 9 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 794 | { OP_Close, 0, 0, 0}, |
| 795 | }; |
| 796 | Index *pIdx; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 797 | sqliteBeginWriteOperation(pParse); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 798 | if( !pTable->isTemp ){ |
| 799 | base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 800 | sqliteVdbeChangeP3(v, base+2, pTable->zName, P3_STATIC); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 801 | changeCookie(db); |
| 802 | sqliteVdbeChangeP1(v, base+9, db->next_cookie); |
| 803 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 804 | sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 805 | for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 806 | sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 807 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 808 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 809 | } |
| 810 | |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 811 | /* Move the table (and all its indices) to the pending DROP queue. |
| 812 | ** Or, if the table was never committed, just delete it. If the table |
| 813 | ** has been committed and is placed on the pending DROP queue, then the |
| 814 | ** delete will occur when sqliteCommitInternalChanges() executes. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 815 | ** |
| 816 | ** Exception: if the SQL statement began with the EXPLAIN keyword, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 817 | ** then no changes should be made. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 818 | */ |
| 819 | if( !pParse->explain ){ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 820 | sqlitePendingDropTable(db, pTable); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 821 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | ** Create a new index for an SQL table. pIndex is the name of the index |
| 827 | ** 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] | 828 | ** be NULL for a primary key or an index that is created to satisfy a |
| 829 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 830 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 831 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 832 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 833 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 834 | ** is a primary key or unique-constraint on the most recent column added |
| 835 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 836 | */ |
| 837 | void sqliteCreateIndex( |
| 838 | Parse *pParse, /* All information about this parse */ |
| 839 | Token *pName, /* Name of the index. May be NULL */ |
| 840 | Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */ |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 841 | IdList *pList, /* A list of columns to be indexed */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 842 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 843 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
| 844 | Token *pEnd /* The ")" that closes the CREATE INDEX statement */ |
| 845 | ){ |
| 846 | Table *pTab; /* Table to be indexed */ |
| 847 | Index *pIndex; /* The index to be created */ |
| 848 | char *zName = 0; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 849 | int i, j; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 850 | Token nullId; /* Fake token for an empty ID list */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 851 | sqlite *db = pParse->db; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 852 | int hideName = 0; /* Do not put table name in the hash table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 853 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 854 | if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index; |
| 855 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 856 | /* |
| 857 | ** Find the table that is to be indexed. Return early if not found. |
| 858 | */ |
| 859 | if( pTable!=0 ){ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 860 | assert( pName!=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 861 | pTab = sqliteTableFromToken(pParse, pTable); |
| 862 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 863 | assert( pName==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 864 | pTab = pParse->pNewTable; |
| 865 | } |
| 866 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
| 867 | if( pTab->readOnly ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 868 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 869 | " may not have new indices added", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 870 | pParse->nErr++; |
| 871 | goto exit_create_index; |
| 872 | } |
| 873 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 874 | /* If this index is created while re-reading the schema from sqlite_master |
| 875 | ** but the table associated with this index is a temporary table, it can |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 876 | ** only mean that the table that this index is really associated with is |
| 877 | ** one whose name is hidden behind a temporary table with the same name. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 878 | ** Since its table has been suppressed, we need to also suppress the |
| 879 | ** index. |
| 880 | */ |
| 881 | if( pParse->initFlag && pTab->isTemp ){ |
| 882 | goto exit_create_index; |
| 883 | } |
| 884 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 885 | /* |
| 886 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 887 | ** index or table with the same name. |
| 888 | ** |
| 889 | ** Exception: If we are reading the names of permanent indices from the |
| 890 | ** sqlite_master table (because some other process changed the schema) and |
| 891 | ** one of the index names collides with the name of a temporary table or |
| 892 | ** index, then we will continue to process this index, but we will not |
| 893 | ** store its name in the hash table. Set the hideName flag to accomplish |
| 894 | ** this. |
| 895 | ** |
| 896 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 897 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 898 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 899 | */ |
| 900 | if( pName ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 901 | Index *pISameName; /* Another index with the same name */ |
| 902 | Table *pTSameName; /* A table with same name as the index */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 903 | zName = sqliteTableNameFromToken(pName); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 904 | if( zName==0 ) goto exit_create_index; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 905 | if( (pISameName = sqliteFindIndex(db, zName))!=0 ){ |
| 906 | if( pISameName->pTable->isTemp && pParse->initFlag ){ |
| 907 | hideName = 1; |
| 908 | }else{ |
| 909 | sqliteSetString(&pParse->zErrMsg, "index ", zName, |
| 910 | " already exists", 0); |
| 911 | pParse->nErr++; |
| 912 | goto exit_create_index; |
| 913 | } |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 914 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 915 | if( (pTSameName = sqliteFindTable(db, zName))!=0 ){ |
| 916 | if( pTSameName->isTemp && pParse->initFlag ){ |
| 917 | hideName = 1; |
| 918 | }else{ |
| 919 | sqliteSetString(&pParse->zErrMsg, "there is already a table named ", |
| 920 | zName, 0); |
| 921 | pParse->nErr++; |
| 922 | goto exit_create_index; |
| 923 | } |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 924 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 925 | }else{ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 926 | char zBuf[30]; |
| 927 | int n; |
| 928 | Index *pLoop; |
| 929 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
| 930 | sprintf(zBuf,"%d)",n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 931 | zName = 0; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 932 | sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 933 | if( zName==0 ) goto exit_create_index; |
drh | da9e034 | 2002-01-10 14:31:48 +0000 | [diff] [blame] | 934 | hideName = sqliteFindIndex(db, zName)!=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 938 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 939 | ** So create a fake list to simulate this. |
| 940 | */ |
| 941 | if( pList==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 942 | nullId.z = pTab->aCol[pTab->nCol-1].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 943 | nullId.n = strlen(nullId.z); |
| 944 | pList = sqliteIdListAppend(0, &nullId); |
| 945 | if( pList==0 ) goto exit_create_index; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | ** Allocate the index structure. |
| 950 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 951 | pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 952 | sizeof(int)*pList->nId ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 953 | if( pIndex==0 ) goto exit_create_index; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 954 | pIndex->aiColumn = (int*)&pIndex[1]; |
| 955 | pIndex->zName = (char*)&pIndex->aiColumn[pList->nId]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 956 | strcpy(pIndex->zName, zName); |
| 957 | pIndex->pTable = pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 958 | pIndex->nColumn = pList->nId; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 959 | pIndex->onError = pIndex->isUnique = onError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 960 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 961 | /* Scan the names of the columns of the table to be indexed and |
| 962 | ** load the column indices into the Index structure. Report an error |
| 963 | ** if any column is not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 964 | */ |
| 965 | for(i=0; i<pList->nId; i++){ |
| 966 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 967 | if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 968 | } |
| 969 | if( j>=pTab->nCol ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 970 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 971 | " has no column named ", pList->a[i].zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 972 | pParse->nErr++; |
| 973 | sqliteFree(pIndex); |
| 974 | goto exit_create_index; |
| 975 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 976 | pIndex->aiColumn[i] = j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /* Link the new Index structure to its table and to the other |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 980 | ** in-memory database structures. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 981 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 982 | if( !pParse->explain && !hideName ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 983 | Index *p; |
| 984 | p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex); |
| 985 | if( p ){ |
| 986 | assert( p==pIndex ); /* Malloc must have failed */ |
| 987 | sqliteFree(pIndex); |
| 988 | goto exit_create_index; |
| 989 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 990 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 991 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 992 | |
| 993 | /* When adding an index to the list of indices for a table, make |
| 994 | ** sure all indices labeled OE_Replace come after all those labeled |
| 995 | ** OE_Ignore. This is necessary for the correct operation of UPDATE |
| 996 | ** and INSERT. |
| 997 | */ |
| 998 | if( onError!=OE_Replace || pTab->pIndex==0 |
| 999 | || pTab->pIndex->onError==OE_Replace){ |
| 1000 | pIndex->pNext = pTab->pIndex; |
| 1001 | pTab->pIndex = pIndex; |
| 1002 | }else{ |
| 1003 | Index *pOther = pTab->pIndex; |
| 1004 | while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
| 1005 | pOther = pOther->pNext; |
| 1006 | } |
| 1007 | pIndex->pNext = pOther->pNext; |
| 1008 | pOther->pNext = pIndex; |
| 1009 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1010 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1011 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 1012 | ** "sqlite_master" table on the disk. So do not write to the disk |
| 1013 | ** again. Extract the table number from the pParse->newTnum field. |
| 1014 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1015 | if( pParse->initFlag && pTable!=0 ){ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1016 | pIndex->tnum = pParse->newTnum; |
| 1017 | } |
| 1018 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1019 | /* If the initFlag is 0 then create the index on disk. This |
| 1020 | ** involves writing the index into the master table and filling in the |
| 1021 | ** index with the current table contents. |
| 1022 | ** |
| 1023 | ** The initFlag is 0 when the user first enters a CREATE INDEX |
| 1024 | ** command. The initFlag is 1 when a database is opened and |
| 1025 | ** CREATE INDEX statements are read out of the master table. In |
| 1026 | ** the latter case the index already exists on disk, which is why |
| 1027 | ** we don't want to recreate it. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1028 | ** |
| 1029 | ** If pTable==0 it means this index is generated as a primary key |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1030 | ** or UNIQUE constraint of a CREATE TABLE statement. Since the table |
| 1031 | ** has just been created, it contains no data and the index initialization |
| 1032 | ** step can be skipped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1033 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1034 | else if( pParse->initFlag==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1035 | int n; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1036 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1037 | int lbl1, lbl2; |
| 1038 | int i; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1039 | int addr; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1040 | int isTemp = pTab->isTemp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1041 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1042 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1043 | if( v==0 ) goto exit_create_index; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1044 | if( pTable!=0 ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1045 | sqliteBeginWriteOperation(pParse); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1046 | if( !isTemp ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1047 | sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2); |
| 1048 | sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1049 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1050 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1051 | if( !isTemp ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1052 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 1053 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1054 | sqliteVdbeChangeP3(v, -1, "index", P3_STATIC); |
| 1055 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1056 | sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC); |
| 1057 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1058 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1059 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1060 | addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp); |
| 1061 | sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1062 | pIndex->tnum = 0; |
| 1063 | if( pTable ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1064 | if( isTemp ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1065 | sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1066 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1067 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 1068 | sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1069 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1070 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1071 | if( !isTemp ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1072 | addr = sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1073 | if( pStart && pEnd ){ |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 1074 | n = Addr(pEnd->z) - Addr(pStart->z) + 1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1075 | sqliteVdbeChangeP3(v, addr, pStart->z, n); |
| 1076 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1077 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 1078 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1079 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1080 | if( pTable ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1081 | sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum); |
| 1082 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1083 | lbl2 = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1084 | sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2); |
| 1085 | lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1086 | for(i=0; i<pIndex->nColumn; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1087 | sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1088 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1089 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1090 | sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1091 | sqliteVdbeAddOp(v, OP_Next, 2, lbl1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1092 | sqliteVdbeResolveLabel(v, lbl2); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1093 | sqliteVdbeAddOp(v, OP_Close, 2, 0); |
| 1094 | sqliteVdbeAddOp(v, OP_Close, 1, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1095 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1096 | if( pTable!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1097 | if( !isTemp ){ |
| 1098 | changeCookie(db); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1099 | sqliteVdbeAddOp(v, OP_SetCookie, db->next_cookie, 0); |
| 1100 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1101 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1102 | sqliteEndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1103 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1106 | /* Clean up before exiting */ |
| 1107 | exit_create_index: |
| 1108 | sqliteIdListDelete(pList); |
| 1109 | sqliteFree(zName); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 1114 | ** This routine will drop an existing named index. This routine |
| 1115 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1116 | */ |
| 1117 | void sqliteDropIndex(Parse *pParse, Token *pName){ |
| 1118 | Index *pIndex; |
| 1119 | char *zName; |
| 1120 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1121 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1122 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1123 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1124 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1125 | if( zName==0 ) return; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1126 | pIndex = sqliteFindIndex(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1127 | sqliteFree(zName); |
| 1128 | if( pIndex==0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 1129 | sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0, |
| 1130 | pName->z, pName->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1131 | pParse->nErr++; |
| 1132 | return; |
| 1133 | } |
| 1134 | |
| 1135 | /* Generate code to remove the index and from the master table */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1136 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1137 | if( v ){ |
| 1138 | static VdbeOp dropIndex[] = { |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1139 | { OP_OpenWrite, 0, 2, MASTER_NAME}, |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1140 | { OP_Rewind, 0, ADDR(10),0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1141 | { OP_String, 0, 0, 0}, /* 2 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1142 | { OP_MemStore, 1, 1, 0}, |
| 1143 | { OP_MemLoad, 1, 0, 0}, /* 4 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1144 | { OP_Column, 0, 1, 0}, |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1145 | { OP_Eq, 0, ADDR(9), 0}, |
| 1146 | { OP_Next, 0, ADDR(4), 0}, |
| 1147 | { OP_Goto, 0, ADDR(10),0}, |
| 1148 | { OP_Delete, 0, 0, 0}, /* 9 */ |
| 1149 | { OP_SetCookie, 0, 0, 0}, /* 10 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1150 | { OP_Close, 0, 0, 0}, |
| 1151 | }; |
| 1152 | int base; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1153 | Table *pTab = pIndex->pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1154 | |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1155 | sqliteBeginWriteOperation(pParse); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1156 | if( !pTab->isTemp ){ |
| 1157 | base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1158 | sqliteVdbeChangeP3(v, base+2, pIndex->zName, P3_STATIC); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1159 | changeCookie(db); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1160 | sqliteVdbeChangeP1(v, base+10, db->next_cookie); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1161 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1162 | sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1163 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 1166 | /* Move the index onto the pending DROP queue. Or, if the index was |
| 1167 | ** never committed, just delete it. Indices on the pending DROP queue |
| 1168 | ** get deleted by sqliteCommitInternalChanges() when the user executes |
| 1169 | ** a COMMIT. Or if a rollback occurs, the elements of the DROP queue |
| 1170 | ** are moved back into the main hash table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1171 | */ |
| 1172 | if( !pParse->explain ){ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 1173 | sqlitePendingDropIndex(db, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1174 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | ** Add a new element to the end of an expression list. If pList is |
| 1180 | ** initially NULL, then create a new expression list. |
| 1181 | */ |
| 1182 | ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ |
| 1183 | int i; |
| 1184 | if( pList==0 ){ |
| 1185 | pList = sqliteMalloc( sizeof(ExprList) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1186 | if( pList==0 ){ |
| 1187 | sqliteExprDelete(pExpr); |
| 1188 | return 0; |
| 1189 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1190 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1191 | if( (pList->nExpr & 7)==0 ){ |
| 1192 | int n = pList->nExpr + 8; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1193 | struct ExprList_item *a; |
| 1194 | a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); |
| 1195 | if( a==0 ){ |
| 1196 | sqliteExprDelete(pExpr); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1197 | return pList; |
| 1198 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1199 | pList->a = a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1200 | } |
drh | 17e24df | 2001-11-06 14:10:41 +0000 | [diff] [blame] | 1201 | if( pExpr || pName ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1202 | i = pList->nExpr++; |
| 1203 | pList->a[i].pExpr = pExpr; |
| 1204 | pList->a[i].zName = 0; |
| 1205 | if( pName ){ |
| 1206 | sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); |
| 1207 | sqliteDequote(pList->a[i].zName); |
| 1208 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1209 | } |
| 1210 | return pList; |
| 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | ** Delete an entire expression list. |
| 1215 | */ |
| 1216 | void sqliteExprListDelete(ExprList *pList){ |
| 1217 | int i; |
| 1218 | if( pList==0 ) return; |
| 1219 | for(i=0; i<pList->nExpr; i++){ |
| 1220 | sqliteExprDelete(pList->a[i].pExpr); |
| 1221 | sqliteFree(pList->a[i].zName); |
| 1222 | } |
| 1223 | sqliteFree(pList->a); |
| 1224 | sqliteFree(pList); |
| 1225 | } |
| 1226 | |
| 1227 | /* |
| 1228 | ** Append a new element to the given IdList. Create a new IdList if |
| 1229 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1230 | ** |
| 1231 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1232 | */ |
| 1233 | IdList *sqliteIdListAppend(IdList *pList, Token *pToken){ |
| 1234 | if( pList==0 ){ |
| 1235 | pList = sqliteMalloc( sizeof(IdList) ); |
| 1236 | if( pList==0 ) return 0; |
| 1237 | } |
| 1238 | if( (pList->nId & 7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1239 | struct IdList_item *a; |
| 1240 | a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) ); |
| 1241 | if( a==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1242 | sqliteIdListDelete(pList); |
| 1243 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1244 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1245 | pList->a = a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1246 | } |
| 1247 | memset(&pList->a[pList->nId], 0, sizeof(pList->a[0])); |
| 1248 | if( pToken ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1249 | char **pz = &pList->a[pList->nId].zName; |
| 1250 | sqliteSetNString(pz, pToken->z, pToken->n, 0); |
| 1251 | if( *pz==0 ){ |
| 1252 | sqliteIdListDelete(pList); |
| 1253 | return 0; |
| 1254 | }else{ |
| 1255 | sqliteDequote(*pz); |
| 1256 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1257 | } |
| 1258 | pList->nId++; |
| 1259 | return pList; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | ** Add an alias to the last identifier on the given identifier list. |
| 1264 | */ |
| 1265 | void sqliteIdListAddAlias(IdList *pList, Token *pToken){ |
| 1266 | if( pList && pList->nId>0 ){ |
| 1267 | int i = pList->nId - 1; |
| 1268 | sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1269 | sqliteDequote(pList->a[i].zAlias); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | ** Delete an entire IdList |
| 1275 | */ |
| 1276 | void sqliteIdListDelete(IdList *pList){ |
| 1277 | int i; |
| 1278 | if( pList==0 ) return; |
| 1279 | for(i=0; i<pList->nId; i++){ |
| 1280 | sqliteFree(pList->a[i].zName); |
| 1281 | sqliteFree(pList->a[i].zAlias); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1282 | if( pList->a[i].pSelect ){ |
| 1283 | sqliteFree(pList->a[i].zName); |
| 1284 | sqliteSelectDelete(pList->a[i].pSelect); |
| 1285 | sqliteDeleteTable(0, pList->a[i].pTab); |
| 1286 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1287 | } |
| 1288 | sqliteFree(pList->a); |
| 1289 | sqliteFree(pList); |
| 1290 | } |
| 1291 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1292 | |
| 1293 | /* |
| 1294 | ** The COPY command is for compatibility with PostgreSQL and specificially |
| 1295 | ** for the ability to read the output of pg_dump. The format is as |
| 1296 | ** follows: |
| 1297 | ** |
| 1298 | ** COPY table FROM file [USING DELIMITERS string] |
| 1299 | ** |
| 1300 | ** "table" is an existing table name. We will read lines of code from |
| 1301 | ** file to fill this table with data. File might be "stdin". The optional |
| 1302 | ** delimiter string identifies the field separators. The default is a tab. |
| 1303 | */ |
| 1304 | void sqliteCopy( |
| 1305 | Parse *pParse, /* The parser context */ |
| 1306 | Token *pTableName, /* The name of the table into which we will insert */ |
| 1307 | Token *pFilename, /* The file from which to obtain information */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1308 | Token *pDelimiter, /* Use this as the field delimiter */ |
| 1309 | int onError /* What to do if a constraint fails */ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1310 | ){ |
| 1311 | Table *pTab; |
| 1312 | char *zTab; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1313 | int i; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1314 | Vdbe *v; |
| 1315 | int addr, end; |
| 1316 | Index *pIdx; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1317 | sqlite *db = pParse->db; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1318 | |
| 1319 | zTab = sqliteTableNameFromToken(pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1320 | if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1321 | pTab = sqliteFindTable(db, zTab); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1322 | sqliteFree(zTab); |
| 1323 | if( pTab==0 ){ |
| 1324 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 1325 | pTableName->z, pTableName->n, 0); |
| 1326 | pParse->nErr++; |
| 1327 | goto copy_cleanup; |
| 1328 | } |
| 1329 | if( pTab->readOnly ){ |
| 1330 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 1331 | " may not be modified", 0); |
| 1332 | pParse->nErr++; |
| 1333 | goto copy_cleanup; |
| 1334 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1335 | v = sqliteGetVdbe(pParse); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1336 | if( v ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1337 | int openOp; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1338 | sqliteBeginMultiWriteOperation(pParse); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1339 | addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1340 | sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n); |
drh | b766599 | 2000-05-30 17:30:35 +0000 | [diff] [blame] | 1341 | sqliteVdbeDequoteP3(v, addr); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1342 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1343 | sqliteVdbeAddOp(v, openOp, 0, pTab->tnum); |
| 1344 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1345 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1346 | sqliteVdbeAddOp(v, openOp, i, pIdx->tnum); |
| 1347 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1348 | } |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1349 | if( db->flags & SQLITE_CountRows ){ |
| 1350 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */ |
| 1351 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1352 | end = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1353 | addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1354 | if( pDelimiter ){ |
| 1355 | sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n); |
| 1356 | sqliteVdbeDequoteP3(v, addr); |
| 1357 | }else{ |
| 1358 | sqliteVdbeChangeP3(v, addr, "\t", 1); |
| 1359 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1360 | if( pTab->iPKey>=0 ){ |
| 1361 | sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0); |
| 1362 | sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); |
| 1363 | }else{ |
| 1364 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 1365 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1366 | for(i=0; i<pTab->nCol; i++){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1367 | if( i==pTab->iPKey ){ |
| 1368 | /* The integer primary key column is filled with NULL since its |
| 1369 | ** value is always pulled from the record number */ |
| 1370 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1371 | }else{ |
| 1372 | sqliteVdbeAddOp(v, OP_FileColumn, i, 0); |
| 1373 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1374 | } |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1375 | sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr); |
| 1376 | sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0); |
| 1377 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
| 1378 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1379 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1380 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 1381 | sqliteVdbeResolveLabel(v, end); |
| 1382 | sqliteVdbeAddOp(v, OP_Noop, 0, 0); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1383 | sqliteEndWriteOperation(pParse); |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1384 | if( db->flags & SQLITE_CountRows ){ |
| 1385 | sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); |
| 1386 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 1387 | sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); |
| 1388 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 1389 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | copy_cleanup: |
| 1393 | return; |
| 1394 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1395 | |
| 1396 | /* |
| 1397 | ** The non-standard VACUUM command is used to clean up the database, |
| 1398 | ** collapse free space, etc. It is modelled after the VACUUM command |
| 1399 | ** in PostgreSQL. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 1400 | ** |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame^] | 1401 | ** In version 1.0.x of SQLite, the VACUUM command would call |
| 1402 | ** gdbm_reorganize() on all the database tables. But beginning |
| 1403 | ** with 2.0.0, SQLite no longer uses GDBM so this command has |
| 1404 | ** become a no-op. |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1405 | */ |
| 1406 | void sqliteVacuum(Parse *pParse, Token *pTableName){ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame^] | 1407 | /* Do nothing */ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1408 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1409 | |
| 1410 | /* |
| 1411 | ** Begin a transaction |
| 1412 | */ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1413 | void sqliteBeginTransaction(Parse *pParse, int onError){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1414 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1415 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1416 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1417 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1418 | if( db->flags & SQLITE_InTrans ) return; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1419 | sqliteBeginWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1420 | db->flags |= SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1421 | db->onError = onError; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | ** Commit a transaction |
| 1426 | */ |
| 1427 | void sqliteCommitTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1428 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1429 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1430 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1431 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1432 | if( (db->flags & SQLITE_InTrans)==0 ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1433 | db->flags &= ~SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1434 | sqliteEndWriteOperation(pParse); |
| 1435 | db->onError = OE_Default; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | ** Rollback a transaction |
| 1440 | */ |
| 1441 | void sqliteRollbackTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1442 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1443 | Vdbe *v; |
| 1444 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1445 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1446 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1447 | if( (db->flags & SQLITE_InTrans)==0 ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1448 | v = sqliteGetVdbe(pParse); |
| 1449 | if( v ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1450 | sqliteVdbeAddOp(v, OP_Rollback, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1451 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1452 | db->flags &= ~SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1453 | db->onError = OE_Default; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1454 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1455 | |
| 1456 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1457 | ** Generate VDBE code that prepares for doing an operation that |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1458 | ** might change the database. The operation will be atomic in the |
| 1459 | ** sense that it will either do its changes completely or not at |
| 1460 | ** all. So there is not need to set a checkpoint is a transaction |
| 1461 | ** is already in effect. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1462 | */ |
| 1463 | void sqliteBeginWriteOperation(Parse *pParse){ |
| 1464 | Vdbe *v; |
| 1465 | v = sqliteGetVdbe(pParse); |
| 1466 | if( v==0 ) return; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1467 | if( (pParse->db->flags & SQLITE_InTrans)==0 ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1468 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0); |
| 1469 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); |
| 1470 | pParse->schemaVerified = 1; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | /* |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1475 | ** Generate VDBE code that prepares for doing an operation that |
| 1476 | ** might change the database. The operation might not be atomic in |
| 1477 | ** the sense that an error may be discovered and the operation might |
| 1478 | ** abort after some changes have been made. If we are in the middle |
| 1479 | ** of a transaction, then this sets a checkpoint. If we are not in |
| 1480 | ** a transaction, then start a transaction. |
| 1481 | */ |
| 1482 | void sqliteBeginMultiWriteOperation(Parse *pParse){ |
| 1483 | Vdbe *v; |
| 1484 | v = sqliteGetVdbe(pParse); |
| 1485 | if( v==0 ) return; |
| 1486 | if( (pParse->db->flags & SQLITE_InTrans)==0 ){ |
| 1487 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0); |
| 1488 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); |
| 1489 | pParse->schemaVerified = 1; |
| 1490 | }else{ |
| 1491 | sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0); |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1496 | ** Generate code that concludes an operation that may have changed |
| 1497 | ** the database. This is a companion function to BeginWriteOperation(). |
| 1498 | ** If a transaction was started, then commit it. If a checkpoint was |
| 1499 | ** started then commit that. |
| 1500 | */ |
| 1501 | void sqliteEndWriteOperation(Parse *pParse){ |
| 1502 | Vdbe *v; |
| 1503 | v = sqliteGetVdbe(pParse); |
| 1504 | if( v==0 ) return; |
| 1505 | if( pParse->db->flags & SQLITE_InTrans ){ |
| 1506 | /* Do Nothing */ |
| 1507 | }else{ |
| 1508 | sqliteVdbeAddOp(v, OP_Commit, 0, 0); |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | |
| 1513 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1514 | ** Interpret the given string as a boolean value. |
| 1515 | */ |
| 1516 | static int getBoolean(char *z){ |
| 1517 | static char *azTrue[] = { "yes", "on", "true" }; |
| 1518 | int i; |
| 1519 | if( z[0]==0 ) return 0; |
| 1520 | if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){ |
| 1521 | return atoi(z); |
| 1522 | } |
| 1523 | for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){ |
| 1524 | if( sqliteStrICmp(z,azTrue[i])==0 ) return 1; |
| 1525 | } |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | /* |
| 1530 | ** Process a pragma statement. |
| 1531 | ** |
| 1532 | ** Pragmas are of this form: |
| 1533 | ** |
| 1534 | ** PRAGMA id = value |
| 1535 | ** |
| 1536 | ** The identifier might also be a string. The value is a string, and |
| 1537 | ** identifier, or a number. If minusFlag is true, then the value is |
| 1538 | ** a number that was preceded by a minus sign. |
| 1539 | */ |
| 1540 | void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){ |
| 1541 | char *zLeft = 0; |
| 1542 | char *zRight = 0; |
| 1543 | sqlite *db = pParse->db; |
| 1544 | |
| 1545 | zLeft = sqliteStrNDup(pLeft->z, pLeft->n); |
| 1546 | sqliteDequote(zLeft); |
| 1547 | if( minusFlag ){ |
| 1548 | zRight = 0; |
| 1549 | sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0); |
| 1550 | }else{ |
| 1551 | zRight = sqliteStrNDup(pRight->z, pRight->n); |
| 1552 | sqliteDequote(zRight); |
| 1553 | } |
| 1554 | |
| 1555 | if( sqliteStrICmp(zLeft,"cache_size")==0 ){ |
| 1556 | int size = atoi(zRight); |
| 1557 | sqliteBtreeSetCacheSize(db->pBe, size); |
| 1558 | }else |
| 1559 | |
| 1560 | if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){ |
| 1561 | if( getBoolean(zRight) ){ |
| 1562 | db->flags |= SQLITE_VdbeTrace; |
| 1563 | }else{ |
| 1564 | db->flags &= ~SQLITE_VdbeTrace; |
| 1565 | } |
| 1566 | }else |
| 1567 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1568 | if( sqliteStrICmp(zLeft, "full_column_names")==0 ){ |
| 1569 | if( getBoolean(zRight) ){ |
| 1570 | db->flags |= SQLITE_FullColNames; |
| 1571 | }else{ |
| 1572 | db->flags &= ~SQLITE_FullColNames; |
| 1573 | } |
| 1574 | }else |
| 1575 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 1576 | if( sqliteStrICmp(zLeft, "result_set_details")==0 ){ |
| 1577 | if( getBoolean(zRight) ){ |
| 1578 | db->flags |= SQLITE_ResultDetails; |
| 1579 | }else{ |
| 1580 | db->flags &= ~SQLITE_ResultDetails; |
| 1581 | } |
| 1582 | }else |
| 1583 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1584 | if( sqliteStrICmp(zLeft, "count_changes")==0 ){ |
| 1585 | if( getBoolean(zRight) ){ |
| 1586 | db->flags |= SQLITE_CountRows; |
| 1587 | }else{ |
| 1588 | db->flags &= ~SQLITE_CountRows; |
| 1589 | } |
| 1590 | }else |
| 1591 | |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1592 | if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){ |
| 1593 | if( getBoolean(zRight) ){ |
| 1594 | db->flags |= SQLITE_NullCallback; |
| 1595 | }else{ |
| 1596 | db->flags &= ~SQLITE_NullCallback; |
| 1597 | } |
| 1598 | }else |
| 1599 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1600 | if( sqliteStrICmp(zLeft, "table_info")==0 ){ |
| 1601 | Table *pTab; |
| 1602 | Vdbe *v; |
| 1603 | pTab = sqliteFindTable(db, zRight); |
| 1604 | if( pTab ) v = sqliteGetVdbe(pParse); |
| 1605 | if( pTab && v ){ |
| 1606 | static VdbeOp tableInfoPreface[] = { |
| 1607 | { OP_ColumnCount, 5, 0, 0}, |
| 1608 | { OP_ColumnName, 0, 0, "cid"}, |
| 1609 | { OP_ColumnName, 1, 0, "name"}, |
| 1610 | { OP_ColumnName, 2, 0, "type"}, |
| 1611 | { OP_ColumnName, 3, 0, "notnull"}, |
| 1612 | { OP_ColumnName, 4, 0, "dflt_value"}, |
| 1613 | }; |
| 1614 | int i; |
| 1615 | sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface); |
| 1616 | for(i=0; i<pTab->nCol; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1617 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
| 1618 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1619 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC); |
| 1620 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1621 | sqliteVdbeChangeP3(v, -1, |
| 1622 | pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", P3_STATIC); |
| 1623 | sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0); |
| 1624 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1625 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); |
| 1626 | sqliteVdbeAddOp(v, OP_Callback, 5, 0); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | }else |
| 1630 | |
| 1631 | if( sqliteStrICmp(zLeft, "index_info")==0 ){ |
| 1632 | Index *pIdx; |
| 1633 | Table *pTab; |
| 1634 | Vdbe *v; |
| 1635 | pIdx = sqliteFindIndex(db, zRight); |
| 1636 | if( pIdx ) v = sqliteGetVdbe(pParse); |
| 1637 | if( pIdx && v ){ |
| 1638 | static VdbeOp tableInfoPreface[] = { |
| 1639 | { OP_ColumnCount, 3, 0, 0}, |
| 1640 | { OP_ColumnName, 0, 0, "seqno"}, |
| 1641 | { OP_ColumnName, 1, 0, "cid"}, |
| 1642 | { OP_ColumnName, 2, 0, "name"}, |
| 1643 | }; |
| 1644 | int i; |
| 1645 | pTab = pIdx->pTable; |
| 1646 | sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface); |
| 1647 | for(i=0; i<pIdx->nColumn; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1648 | int cnum = pIdx->aiColumn[i]; |
| 1649 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
| 1650 | sqliteVdbeAddOp(v, OP_Integer, cnum, 0); |
| 1651 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1652 | sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC); |
| 1653 | sqliteVdbeAddOp(v, OP_Callback, 3, 0); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1654 | } |
| 1655 | } |
| 1656 | }else |
| 1657 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 1658 | if( sqliteStrICmp(zLeft, "index_list")==0 ){ |
| 1659 | Index *pIdx; |
| 1660 | Table *pTab; |
| 1661 | Vdbe *v; |
| 1662 | pTab = sqliteFindTable(db, zRight); |
| 1663 | if( pTab ){ |
| 1664 | v = sqliteGetVdbe(pParse); |
| 1665 | pIdx = pTab->pIndex; |
| 1666 | } |
| 1667 | if( pTab && pIdx && v ){ |
| 1668 | int i = 0; |
| 1669 | static VdbeOp indexListPreface[] = { |
| 1670 | { OP_ColumnCount, 3, 0, 0}, |
| 1671 | { OP_ColumnName, 0, 0, "seq"}, |
| 1672 | { OP_ColumnName, 1, 0, "name"}, |
| 1673 | { OP_ColumnName, 2, 0, "unique"}, |
| 1674 | }; |
| 1675 | |
| 1676 | sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface); |
| 1677 | while(pIdx){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1678 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
| 1679 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1680 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1681 | sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1682 | sqliteVdbeAddOp(v, OP_Callback, 3, 0); |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 1683 | ++i; |
| 1684 | pIdx = pIdx->pNext; |
| 1685 | } |
| 1686 | } |
| 1687 | }else |
| 1688 | |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1689 | #ifndef NDEBUG |
| 1690 | if( sqliteStrICmp(zLeft, "parser_trace")==0 ){ |
| 1691 | extern void sqliteParserTrace(FILE*, char *); |
| 1692 | if( getBoolean(zRight) ){ |
| 1693 | sqliteParserTrace(stdout, "parser: "); |
| 1694 | }else{ |
| 1695 | sqliteParserTrace(0, 0); |
| 1696 | } |
| 1697 | }else |
| 1698 | #endif |
| 1699 | |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame^] | 1700 | #ifndef NDEBUG |
| 1701 | if( sqliteStrICmp(zLeft, "sanity_check")==0 ){ |
| 1702 | static VdbeOp checkDb[] = { |
| 1703 | { OP_SetInsert, 0, 0, "2"}, |
| 1704 | { OP_Open, 0, 2, 0}, |
| 1705 | { OP_Rewind, 0, 6, 0}, |
| 1706 | { OP_Column, 0, 3, 0}, |
| 1707 | { OP_SetInsert, 0, 0, 0}, |
| 1708 | { OP_Next, 0, 3, 0}, |
| 1709 | { OP_SanityCheck, 0, 0, 0}, |
| 1710 | { OP_ColumnCount, 1, 0, 0}, |
| 1711 | { OP_ColumnName, 0, 0, "sanity_check"}, |
| 1712 | { OP_Callback, 1, 0, 0}, |
| 1713 | }; |
| 1714 | Vdbe *v = sqliteGetVdbe(pParse); |
| 1715 | if( v==0 ) return; |
| 1716 | sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb); |
| 1717 | }else |
| 1718 | #endif |
| 1719 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1720 | {} |
| 1721 | sqliteFree(zLeft); |
| 1722 | sqliteFree(zRight); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1723 | } |