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 | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 28 | ** $Id: build.c,v 1.36 2001/09/16 00:13:26 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 | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 57 | } |
| 58 | sqliteVdbeDelete(pParse->pVdbe); |
| 59 | pParse->pVdbe = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 60 | pParse->colNamesSet = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 61 | pParse->rc = rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 62 | pParse->schemaVerified = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | ** Construct a new expression node and return a pointer to it. |
| 68 | */ |
| 69 | Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ |
| 70 | Expr *pNew; |
| 71 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 72 | if( pNew==0 ) return 0; |
| 73 | pNew->op = op; |
| 74 | pNew->pLeft = pLeft; |
| 75 | pNew->pRight = pRight; |
| 76 | if( pToken ){ |
| 77 | pNew->token = *pToken; |
| 78 | }else{ |
| 79 | pNew->token.z = ""; |
| 80 | pNew->token.n = 0; |
| 81 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 82 | if( pLeft && pRight ){ |
| 83 | sqliteExprSpan(pNew, &pLeft->span, &pRight->span); |
| 84 | }else{ |
| 85 | pNew->span = pNew->token; |
| 86 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 87 | return pNew; |
| 88 | } |
| 89 | |
| 90 | /* |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 91 | ** Set the Expr.token field of the given expression to span all |
| 92 | ** text between the two given tokens. |
| 93 | */ |
| 94 | void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 95 | if( pExpr ){ |
| 96 | pExpr->span.z = pLeft->z; |
| 97 | pExpr->span.n = pRight->n + (int)pRight->z - (int)pLeft->z; |
| 98 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 102 | ** Construct a new expression node for a function with multiple |
| 103 | ** arguments. |
| 104 | */ |
| 105 | Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ |
| 106 | Expr *pNew; |
| 107 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 108 | if( pNew==0 ) return 0; |
| 109 | pNew->op = TK_FUNCTION; |
| 110 | pNew->pList = pList; |
| 111 | if( pToken ){ |
| 112 | pNew->token = *pToken; |
| 113 | }else{ |
| 114 | pNew->token.z = ""; |
| 115 | pNew->token.n = 0; |
| 116 | } |
| 117 | return pNew; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | ** Recursively delete an expression tree. |
| 122 | */ |
| 123 | void sqliteExprDelete(Expr *p){ |
| 124 | if( p==0 ) return; |
| 125 | if( p->pLeft ) sqliteExprDelete(p->pLeft); |
| 126 | if( p->pRight ) sqliteExprDelete(p->pRight); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 127 | if( p->pList ) sqliteExprListDelete(p->pList); |
| 128 | if( p->pSelect ) sqliteSelectDelete(p->pSelect); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 129 | sqliteFree(p); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | ** Locate the in-memory structure that describes the |
| 134 | ** format of a particular database table given the name |
| 135 | ** of that table. Return NULL if not found. |
| 136 | */ |
| 137 | Table *sqliteFindTable(sqlite *db, char *zName){ |
| 138 | Table *pTable; |
| 139 | int h; |
| 140 | |
| 141 | h = sqliteHashNoCase(zName, 0) % N_HASH; |
| 142 | for(pTable=db->apTblHash[h]; pTable; pTable=pTable->pHash){ |
| 143 | if( sqliteStrICmp(pTable->zName, zName)==0 ) return pTable; |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | ** Locate the in-memory structure that describes the |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 150 | ** format of a particular index given the name |
| 151 | ** of that index. Return NULL if not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 152 | */ |
| 153 | Index *sqliteFindIndex(sqlite *db, char *zName){ |
| 154 | Index *p; |
| 155 | int h; |
| 156 | |
| 157 | h = sqliteHashNoCase(zName, 0) % N_HASH; |
| 158 | for(p=db->apIdxHash[h]; p; p=p->pHash){ |
| 159 | if( sqliteStrICmp(p->zName, zName)==0 ) return p; |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | ** Remove the given index from the index hash table, and free |
| 166 | ** its memory structures. |
| 167 | ** |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 168 | ** The index is removed from the database hash table if db!=NULL. |
| 169 | ** But it is not unlinked from the Table that is being indexed. |
| 170 | ** Unlinking from the Table must be done by the calling function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 171 | */ |
| 172 | static void sqliteDeleteIndex(sqlite *db, Index *pIndex){ |
| 173 | int h; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 174 | if( pIndex->zName && db ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 175 | h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH; |
| 176 | if( db->apIdxHash[h]==pIndex ){ |
| 177 | db->apIdxHash[h] = pIndex->pHash; |
| 178 | }else{ |
| 179 | Index *p; |
| 180 | for(p=db->apIdxHash[h]; p && p->pHash!=pIndex; p=p->pHash){} |
| 181 | if( p && p->pHash==pIndex ){ |
| 182 | p->pHash = pIndex->pHash; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | sqliteFree(pIndex); |
| 187 | } |
| 188 | |
| 189 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 190 | ** Unlink the given index from its table, then remove |
| 191 | ** the index from the index hash table, and free its memory |
| 192 | ** structures. |
| 193 | */ |
| 194 | static void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){ |
| 195 | if( pIndex->pTable->pIndex==pIndex ){ |
| 196 | pIndex->pTable->pIndex = pIndex->pNext; |
| 197 | }else{ |
| 198 | Index *p; |
| 199 | for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
| 200 | if( p && p->pNext==pIndex ){ |
| 201 | p->pNext = pIndex->pNext; |
| 202 | } |
| 203 | } |
| 204 | sqliteDeleteIndex(db, pIndex); |
| 205 | } |
| 206 | |
| 207 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 208 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 209 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 210 | ** |
| 211 | ** This routine just deletes the data structure. It does not unlink |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 212 | ** the table data structure from the hash table. But it does destroy |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 213 | ** memory structures of the indices associated with the table. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 214 | ** |
| 215 | ** Indices associated with the table are unlinked from the "db" |
| 216 | ** data structure if db!=NULL. If db==NULL, indices attached to |
| 217 | ** the table are deleted, but it is assumed they have already been |
| 218 | ** unlinked. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 219 | */ |
| 220 | void sqliteDeleteTable(sqlite *db, Table *pTable){ |
| 221 | int i; |
| 222 | Index *pIndex, *pNext; |
| 223 | if( pTable==0 ) return; |
| 224 | for(i=0; i<pTable->nCol; i++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 225 | sqliteFree(pTable->aCol[i].zName); |
| 226 | sqliteFree(pTable->aCol[i].zDflt); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 227 | } |
| 228 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 229 | pNext = pIndex->pNext; |
| 230 | sqliteDeleteIndex(db, pIndex); |
| 231 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 232 | sqliteFree(pTable->zName); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 233 | sqliteFree(pTable->aCol); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 234 | sqliteFree(pTable); |
| 235 | } |
| 236 | |
| 237 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 238 | ** Unlink the given table from the hash tables and the delete the |
| 239 | ** table structure and all its indices. |
| 240 | */ |
| 241 | static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *pTable){ |
| 242 | if( pTable->zName && db ){ |
| 243 | int h = sqliteHashNoCase(pTable->zName, 0) % N_HASH; |
| 244 | if( db->apTblHash[h]==pTable ){ |
| 245 | db->apTblHash[h] = pTable->pHash; |
| 246 | }else{ |
| 247 | Table *p; |
| 248 | for(p=db->apTblHash[h]; p && p->pHash!=pTable; p=p->pHash){} |
| 249 | if( p && p->pHash==pTable ){ |
| 250 | p->pHash = pTable->pHash; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | sqliteDeleteTable(db, pTable); |
| 255 | } |
| 256 | |
| 257 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 258 | ** Check all Tables and Indexes in the internal hash table and commit |
| 259 | ** any additions or deletions to those hash tables. |
| 260 | ** |
| 261 | ** When executing CREATE TABLE and CREATE INDEX statements, the Table |
| 262 | ** and Index structures are created and added to the hash tables, but |
| 263 | ** the "isCommit" field is not set. This routine sets those fields. |
| 264 | ** When executing DROP TABLE and DROP INDEX, the "isDelete" fields of |
| 265 | ** Table and Index structures is set but the structures are not unlinked |
| 266 | ** from the hash tables nor deallocated. This routine handles that |
| 267 | ** deallocation. |
| 268 | ** |
| 269 | ** See also: sqliteRollbackInternalChanges() |
| 270 | */ |
| 271 | void sqliteCommitInternalChanges(sqlite *db){ |
| 272 | int i; |
| 273 | if( (db->flags & SQLITE_InternChanges)==0 ) return; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 274 | db->schema_cookie = db->next_cookie; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 275 | for(i=0; i<N_HASH; i++){ |
| 276 | Table *pTable, *pNext; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 277 | for(pTable = db->apTblHash[i]; pTable; pTable=pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 278 | pNext = pTable->pHash; |
| 279 | if( pTable->isDelete ){ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 280 | sqliteUnlinkAndDeleteTable(db, pTable); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 281 | }else if( pTable->isCommit==0 ){ |
| 282 | pTable->isCommit = 1; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | for(i=0; i<N_HASH; i++){ |
| 287 | Index *pIndex, *pNext; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 288 | for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 289 | pNext = pIndex->pHash; |
| 290 | if( pIndex->isDelete ){ |
| 291 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
| 292 | }else if( pIndex->isCommit==0 ){ |
| 293 | pIndex->isCommit = 1; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | db->flags &= ~SQLITE_InternChanges; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | ** This routine runs when one or more CREATE TABLE, CREATE INDEX, |
| 302 | ** DROP TABLE, or DROP INDEX statements get rolled back. The |
| 303 | ** additions or deletions of Table and Index structures in the |
| 304 | ** internal hash tables are undone. |
| 305 | ** |
| 306 | ** See also: sqliteCommitInternalChanges() |
| 307 | */ |
| 308 | void sqliteRollbackInternalChanges(sqlite *db){ |
| 309 | int i; |
| 310 | if( (db->flags & SQLITE_InternChanges)==0 ) return; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 311 | db->next_cookie = db->schema_cookie; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 312 | for(i=0; i<N_HASH; i++){ |
| 313 | Table *pTable, *pNext; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 314 | for(pTable = db->apTblHash[i]; pTable; pTable=pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 315 | pNext = pTable->pHash; |
| 316 | if( !pTable->isCommit ){ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 317 | sqliteUnlinkAndDeleteTable(db, pTable); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 318 | }else if( pTable->isDelete ){ |
| 319 | pTable->isDelete = 0; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | for(i=0; i<N_HASH; i++){ |
| 324 | Index *pIndex, *pNext; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 325 | for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 326 | pNext = pIndex->pHash; |
| 327 | if( !pIndex->isCommit ){ |
| 328 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
| 329 | }else if( pIndex->isDelete ){ |
| 330 | pIndex->isDelete = 0; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | db->flags &= ~SQLITE_InternChanges; |
| 335 | } |
| 336 | |
| 337 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 338 | ** Construct the name of a user table or index from a token. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 339 | ** |
| 340 | ** Space to hold the name is obtained from sqliteMalloc() and must |
| 341 | ** be freed by the calling function. |
| 342 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 343 | char *sqliteTableNameFromToken(Token *pName){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 344 | char *zName = sqliteStrNDup(pName->z, pName->n); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 345 | sqliteDequote(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 346 | return zName; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | ** Begin constructing a new table representation in memory. This is |
| 351 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 352 | ** to a CREATE TABLE statement. In particular, this routine is called |
| 353 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The |
| 354 | ** pStart token is the CREATE and pName is the table name. |
| 355 | ** |
| 356 | ** The new table is constructed in files of the pParse structure. As |
| 357 | ** more of the CREATE TABLE statement is parsed, additional action |
| 358 | ** routines are called to build up more of the table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 359 | */ |
| 360 | void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName){ |
| 361 | Table *pTable; |
| 362 | char *zName; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 363 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 364 | |
| 365 | pParse->sFirstToken = *pStart; |
| 366 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 367 | if( zName==0 ) return; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 368 | pTable = sqliteFindTable(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 369 | if( pTable!=0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 370 | sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n, |
| 371 | " already exists", 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 372 | sqliteFree(zName); |
| 373 | pParse->nErr++; |
| 374 | return; |
| 375 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 376 | if( sqliteFindIndex(db, zName) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 377 | sqliteSetString(&pParse->zErrMsg, "there is already an index named ", |
| 378 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 379 | sqliteFree(zName); |
| 380 | pParse->nErr++; |
| 381 | return; |
| 382 | } |
| 383 | pTable = sqliteMalloc( sizeof(Table) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 384 | if( pTable==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 385 | pTable->zName = zName; |
| 386 | pTable->pHash = 0; |
| 387 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 388 | pTable->aCol = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 389 | pTable->pIndex = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 390 | if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 391 | pParse->pNewTable = pTable; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 392 | if( !pParse->initFlag && (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 393 | Vdbe *v = sqliteGetVdbe(pParse); |
| 394 | if( v ){ |
| 395 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 396 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* |
| 402 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 403 | ** |
| 404 | ** The parser calls this routine once for each column declaration |
| 405 | ** in a CREATE TABLE statement. sqliteStartTable() gets called |
| 406 | ** first to get things going. Then this routine is called for each |
| 407 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 408 | */ |
| 409 | void sqliteAddColumn(Parse *pParse, Token *pName){ |
| 410 | Table *p; |
| 411 | char **pz; |
| 412 | if( (p = pParse->pNewTable)==0 ) return; |
| 413 | if( (p->nCol & 0x7)==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 414 | p->aCol = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 415 | if( p->aCol==0 ){ |
| 416 | p->nCol = 0; |
| 417 | return; |
| 418 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 419 | } |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 420 | memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0])); |
| 421 | pz = &p->aCol[p->nCol++].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 422 | sqliteSetNString(pz, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 423 | sqliteDequote(*pz); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 427 | ** The given token is the default value for the last column added to |
| 428 | ** the table currently under construction. If "minusFlag" is true, it |
| 429 | ** means the value token was preceded by a minus sign. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 430 | ** |
| 431 | ** This routine is called by the parser while in the middle of |
| 432 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 433 | */ |
| 434 | void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ |
| 435 | Table *p; |
| 436 | int i; |
| 437 | char **pz; |
| 438 | if( (p = pParse->pNewTable)==0 ) return; |
| 439 | i = p->nCol-1; |
| 440 | pz = &p->aCol[i].zDflt; |
| 441 | if( minusFlag ){ |
| 442 | sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0); |
| 443 | }else{ |
| 444 | sqliteSetNString(pz, pVal->z, pVal->n, 0); |
| 445 | } |
| 446 | sqliteDequote(*pz); |
| 447 | } |
| 448 | |
| 449 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 450 | ** Come up with a new random value for the schema cookie. Make sure |
| 451 | ** the new value is different from the old. |
| 452 | ** |
| 453 | ** The schema cookie is used to determine when the schema for the |
| 454 | ** database changes. After each schema change, the cookie value |
| 455 | ** changes. When a process first reads the schema it records the |
| 456 | ** cookie. Thereafter, whenever it goes to access the database, |
| 457 | ** it checks the cookie to make sure the schema has not changed |
| 458 | ** since it was last read. |
| 459 | ** |
| 460 | ** This plan is not completely bullet-proof. It is possible for |
| 461 | ** the schema to change multiple times and for the cookie to be |
| 462 | ** set back to prior value. But schema changes are infrequent |
| 463 | ** and the probability of hitting the same cookie value is only |
| 464 | ** 1 chance in 2^32. So we're safe enough. |
| 465 | */ |
| 466 | static void changeCookie(sqlite *db){ |
| 467 | if( db->next_cookie==db->schema_cookie ){ |
| 468 | db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1; |
| 469 | db->flags |= SQLITE_InternChanges; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 474 | ** This routine is called to report the final ")" that terminates |
| 475 | ** a CREATE TABLE statement. |
| 476 | ** |
| 477 | ** The table structure is added to the internal hash tables. |
| 478 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 479 | ** An entry for the table is made in the master table on disk, |
| 480 | ** unless initFlag==1. When initFlag==1, it means we are reading |
| 481 | ** the master table because we just connected to the database, so |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 482 | ** the entry for this table already exists in the master table. |
| 483 | ** We do not want to create it again. |
| 484 | */ |
| 485 | void sqliteEndTable(Parse *pParse, Token *pEnd){ |
| 486 | Table *p; |
| 487 | int h; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 488 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 489 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 490 | if( pEnd==0 || pParse->nErr || sqlite_malloc_failed ) return; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 491 | p = pParse->pNewTable; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 492 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 493 | |
| 494 | /* Add the table to the in-memory representation of the database |
| 495 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 496 | if( pParse->explain==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 497 | h = sqliteHashNoCase(p->zName, 0) % N_HASH; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 498 | p->pHash = db->apTblHash[h]; |
| 499 | db->apTblHash[h] = p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 500 | pParse->pNewTable = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 501 | db->nTable++; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 502 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 503 | } |
| 504 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 505 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 506 | ** "sqlite_master" table on the disk. So do not write to the disk |
| 507 | ** again. Extract the table number from the pParse->newTnum field. |
| 508 | */ |
| 509 | if( pParse->initFlag ){ |
| 510 | p->tnum = pParse->newTnum; |
| 511 | } |
| 512 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 513 | /* If not initializing, then create the table on disk. |
| 514 | */ |
| 515 | if( !pParse->initFlag ){ |
| 516 | static VdbeOp addTable[] = { |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 517 | { OP_Open, 0, 2, MASTER_NAME}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 518 | { OP_NewRecno, 0, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 519 | { OP_String, 0, 0, "table" }, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 520 | { OP_String, 0, 0, 0}, /* 3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 521 | { OP_CreateTable, 0, 0, 0}, |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 522 | { OP_String, 0, 0, 0}, /* 5 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 523 | { OP_String, 0, 0, 0}, /* 6 */ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 524 | { OP_MakeRecord, 5, 0, 0}, |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 525 | { OP_Put, 0, 0, 0}, |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 526 | { OP_SetCookie, 0, 0, 0}, /* 9 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 527 | }; |
| 528 | int n, base; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 529 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 530 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 531 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 532 | if( v==0 ) return; |
| 533 | n = (int)pEnd->z - (int)pParse->sFirstToken.z + 1; |
| 534 | base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 535 | sqliteVdbeChangeP3(v, base+3, p->zName, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 536 | sqliteVdbeTableRootAddr(v, &p->tnum); |
| 537 | sqliteVdbeChangeP3(v, base+5, p->zName, 0); |
| 538 | sqliteVdbeChangeP3(v, base+6, pParse->sFirstToken.z, n); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 539 | changeCookie(db); |
| 540 | sqliteVdbeChangeP1(v, base+9, db->next_cookie); |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 541 | sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 542 | if( p->pIndex ){ |
| 543 | /* If the table has a primary key, create an index in the database |
| 544 | ** for that key. */ |
| 545 | Index *pIndex = p->pIndex; |
| 546 | assert( pIndex->pNext==0 ); |
| 547 | assert( pIndex->tnum==0 ); |
| 548 | sqliteVdbeAddOp(v, OP_CreateIndex, 0, 0, 0, 0), |
| 549 | sqliteVdbeIndexRootAddr(v, &pIndex->tnum); |
| 550 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 551 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 552 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 553 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | ** Given a token, look up a table with that name. If not found, leave |
| 559 | ** an error for the parser to find and return NULL. |
| 560 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 561 | Table *sqliteTableFromToken(Parse *pParse, Token *pTok){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 562 | char *zName; |
| 563 | Table *pTab; |
| 564 | zName = sqliteTableNameFromToken(pTok); |
| 565 | if( zName==0 ) return 0; |
| 566 | pTab = sqliteFindTable(pParse->db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 567 | sqliteFree(zName); |
| 568 | if( pTab==0 ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 569 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 570 | pTok->z, pTok->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 571 | pParse->nErr++; |
| 572 | } |
| 573 | return pTab; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 578 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 579 | */ |
| 580 | void sqliteDropTable(Parse *pParse, Token *pName){ |
| 581 | Table *pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 582 | Vdbe *v; |
| 583 | int base; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 584 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 585 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 586 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 587 | pTable = sqliteTableFromToken(pParse, pName); |
| 588 | if( pTable==0 ) return; |
| 589 | if( pTable->readOnly ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 590 | sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName, |
| 591 | " may not be dropped", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 592 | pParse->nErr++; |
| 593 | return; |
| 594 | } |
| 595 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 596 | /* Generate code to remove the table from the master table |
| 597 | ** on disk. |
| 598 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 599 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 600 | if( v ){ |
| 601 | static VdbeOp dropTable[] = { |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 602 | { OP_Open, 0, 2, MASTER_NAME}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 603 | { OP_Rewind, 0, 0, 0}, |
| 604 | { OP_String, 0, 0, 0}, /* 2 */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 605 | { OP_Next, 0, ADDR(9), 0}, /* 3 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 606 | { OP_Dup, 0, 0, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 607 | { OP_Column, 0, 3, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 608 | { OP_Ne, 0, ADDR(3), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 609 | { OP_Delete, 0, 0, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 610 | { OP_Goto, 0, ADDR(3), 0}, |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 611 | { OP_Destroy, 0, 0, 0}, /* 9 */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 612 | { OP_SetCookie, 0, 0, 0}, /* 10 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 613 | { OP_Close, 0, 0, 0}, |
| 614 | }; |
| 615 | Index *pIdx; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 616 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 617 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 618 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 619 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 620 | base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 621 | sqliteVdbeChangeP3(v, base+2, pTable->zName, 0); |
| 622 | sqliteVdbeChangeP1(v, base+9, pTable->tnum); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 623 | changeCookie(db); |
| 624 | sqliteVdbeChangeP1(v, base+10, db->next_cookie); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 625 | for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 626 | sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, 0, 0, 0); |
| 627 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 628 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 629 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 633 | /* Mark the in-memory Table structure as being deleted. The actually |
| 634 | ** deletion occurs inside of sqliteCommitInternalChanges(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 635 | ** |
| 636 | ** Exception: if the SQL statement began with the EXPLAIN keyword, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 637 | ** then no changes should be made. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 638 | */ |
| 639 | if( !pParse->explain ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 640 | pTable->isDelete = 1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 641 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | ** Create a new index for an SQL table. pIndex is the name of the index |
| 647 | ** and pTable is the name of the table that is to be indexed. Both will |
| 648 | ** be NULL for a primary key. In that case, use pParse->pNewTable as the |
| 649 | ** table to be indexed. |
| 650 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 651 | ** pList is a list of columns to be indexed. pList will be NULL if the |
| 652 | ** most recently added column of the table is labeled as the primary key. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 653 | */ |
| 654 | void sqliteCreateIndex( |
| 655 | Parse *pParse, /* All information about this parse */ |
| 656 | Token *pName, /* Name of the index. May be NULL */ |
| 657 | Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */ |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 658 | IdList *pList, /* A list of columns to be indexed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 659 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
| 660 | Token *pEnd /* The ")" that closes the CREATE INDEX statement */ |
| 661 | ){ |
| 662 | Table *pTab; /* Table to be indexed */ |
| 663 | Index *pIndex; /* The index to be created */ |
| 664 | char *zName = 0; |
| 665 | int i, j, h; |
| 666 | Token nullId; /* Fake token for an empty ID list */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 667 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 668 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 669 | if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index; |
| 670 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 671 | /* |
| 672 | ** Find the table that is to be indexed. Return early if not found. |
| 673 | */ |
| 674 | if( pTable!=0 ){ |
| 675 | pTab = sqliteTableFromToken(pParse, pTable); |
| 676 | }else{ |
| 677 | pTab = pParse->pNewTable; |
| 678 | } |
| 679 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
| 680 | if( pTab->readOnly ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 681 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 682 | " may not have new indices added", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 683 | pParse->nErr++; |
| 684 | goto exit_create_index; |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | ** Find the name of the index. Make sure there is not already another |
| 689 | ** index or table with the same name. |
| 690 | */ |
| 691 | if( pName ){ |
| 692 | zName = sqliteTableNameFromToken(pName); |
| 693 | }else{ |
| 694 | zName = 0; |
| 695 | sqliteSetString(&zName, pTab->zName, "__primary_key", 0); |
| 696 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 697 | if( zName==0 ) goto exit_create_index; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 698 | if( sqliteFindIndex(db, zName) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 699 | sqliteSetString(&pParse->zErrMsg, "index ", zName, |
| 700 | " already exists", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 701 | pParse->nErr++; |
| 702 | goto exit_create_index; |
| 703 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 704 | if( sqliteFindTable(db, zName) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 705 | sqliteSetString(&pParse->zErrMsg, "there is already a table named ", |
| 706 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 707 | pParse->nErr++; |
| 708 | goto exit_create_index; |
| 709 | } |
| 710 | |
| 711 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 712 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 713 | ** So create a fake list to simulate this. |
| 714 | */ |
| 715 | if( pList==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 716 | nullId.z = pTab->aCol[pTab->nCol-1].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 717 | nullId.n = strlen(nullId.z); |
| 718 | pList = sqliteIdListAppend(0, &nullId); |
| 719 | if( pList==0 ) goto exit_create_index; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | ** Allocate the index structure. |
| 724 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 725 | pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 726 | sizeof(int)*pList->nId ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 727 | if( pIndex==0 ) goto exit_create_index; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 728 | pIndex->aiColumn = (int*)&pIndex[1]; |
| 729 | pIndex->zName = (char*)&pIndex->aiColumn[pList->nId]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 730 | strcpy(pIndex->zName, zName); |
| 731 | pIndex->pTable = pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 732 | pIndex->nColumn = pList->nId; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 733 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 734 | /* Scan the names of the columns of the table to be indexed and |
| 735 | ** load the column indices into the Index structure. Report an error |
| 736 | ** if any column is not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 737 | */ |
| 738 | for(i=0; i<pList->nId; i++){ |
| 739 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 740 | if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 741 | } |
| 742 | if( j>=pTab->nCol ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 743 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 744 | " has no column named ", pList->a[i].zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 745 | pParse->nErr++; |
| 746 | sqliteFree(pIndex); |
| 747 | goto exit_create_index; |
| 748 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 749 | pIndex->aiColumn[i] = j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* Link the new Index structure to its table and to the other |
| 753 | ** in-memory database structures. |
| 754 | */ |
| 755 | if( pParse->explain==0 ){ |
| 756 | h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 757 | pIndex->pHash = db->apIdxHash[h]; |
| 758 | db->apIdxHash[h] = pIndex; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 759 | pIndex->pNext = pTab->pIndex; |
| 760 | pTab->pIndex = pIndex; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 761 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 762 | } |
| 763 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 764 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 765 | ** "sqlite_master" table on the disk. So do not write to the disk |
| 766 | ** again. Extract the table number from the pParse->newTnum field. |
| 767 | */ |
| 768 | if( pParse->initFlag ){ |
| 769 | pIndex->tnum = pParse->newTnum; |
| 770 | } |
| 771 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 772 | /* If the initFlag is 0 then create the index on disk. This |
| 773 | ** involves writing the index into the master table and filling in the |
| 774 | ** index with the current table contents. |
| 775 | ** |
| 776 | ** The initFlag is 0 when the user first enters a CREATE INDEX |
| 777 | ** command. The initFlag is 1 when a database is opened and |
| 778 | ** CREATE INDEX statements are read out of the master table. In |
| 779 | ** the latter case the index already exists on disk, which is why |
| 780 | ** we don't want to recreate it. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 781 | ** |
| 782 | ** If pTable==0 it means this index is generated as a primary key |
| 783 | ** and those does not have a CREATE INDEX statement to add to the |
| 784 | ** master table. Also, since primary keys are created at the same |
| 785 | ** time as tables, the table will be empty so there is no need to |
| 786 | ** initialize the index. Hence, skip all the code generation if |
| 787 | ** pTable==0. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 788 | */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 789 | else if( pParse->initFlag==0 && pTable!=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 790 | static VdbeOp addTable[] = { |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 791 | { OP_Open, 2, 2, MASTER_NAME}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 792 | { OP_NewRecno, 2, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 793 | { OP_String, 0, 0, "index"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 794 | { OP_String, 0, 0, 0}, /* 3 */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 795 | { OP_CreateIndex, 1, 0, 0}, |
| 796 | { OP_Dup, 0, 0, 0}, |
| 797 | { OP_Open, 1, 0, 0}, /* 6 */ |
| 798 | { OP_String, 0, 0, 0}, /* 7 */ |
| 799 | { OP_String, 0, 0, 0}, /* 8 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 800 | { OP_MakeRecord, 5, 0, 0}, |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 801 | { OP_Put, 2, 0, 0}, |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 802 | { OP_SetCookie, 0, 0, 0}, /* 11 */ |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 803 | { OP_Close, 2, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 804 | }; |
| 805 | int n; |
| 806 | Vdbe *v = pParse->pVdbe; |
| 807 | int lbl1, lbl2; |
| 808 | int i; |
| 809 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 810 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 811 | if( v==0 ) goto exit_create_index; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 812 | if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 813 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 814 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 815 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 816 | if( pStart && pEnd ){ |
| 817 | int base; |
| 818 | n = (int)pEnd->z - (int)pStart->z + 1; |
| 819 | base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable); |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 820 | sqliteVdbeChangeP3(v, base+3, pIndex->zName, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 821 | sqliteVdbeIndexRootAddr(v, &pIndex->tnum); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 822 | sqliteVdbeChangeP3(v, base+6, pIndex->zName, 0); |
| 823 | sqliteVdbeChangeP3(v, base+7, pTab->zName, 0); |
| 824 | sqliteVdbeChangeP3(v, base+8, pStart->z, n); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 825 | changeCookie(db); |
| 826 | sqliteVdbeChangeP1(v, base+11, db->next_cookie); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 827 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 828 | sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 829 | lbl1 = sqliteVdbeMakeLabel(v); |
| 830 | lbl2 = sqliteVdbeMakeLabel(v); |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 831 | sqliteVdbeAddOp(v, OP_Rewind, 0, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 832 | sqliteVdbeAddOp(v, OP_Next, 0, lbl2, 0, lbl1); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 833 | sqliteVdbeAddOp(v, OP_Recno, 0, 0, 0, 0); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 834 | for(i=0; i<pIndex->nColumn; i++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 835 | sqliteVdbeAddOp(v, OP_Column, 0, pIndex->aiColumn[i], 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 836 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 837 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 838 | sqliteVdbeAddOp(v, OP_PutIdx, 1, 0, 0, 0); |
| 839 | sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0); |
| 840 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 841 | sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 842 | sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 843 | if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 844 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 845 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | /* Reclaim memory on an EXPLAIN call. |
| 849 | */ |
| 850 | if( pParse->explain ){ |
| 851 | sqliteFree(pIndex); |
| 852 | } |
| 853 | |
| 854 | /* Clean up before exiting */ |
| 855 | exit_create_index: |
| 856 | sqliteIdListDelete(pList); |
| 857 | sqliteFree(zName); |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | ** This routine will drop an existing named index. |
| 863 | */ |
| 864 | void sqliteDropIndex(Parse *pParse, Token *pName){ |
| 865 | Index *pIndex; |
| 866 | char *zName; |
| 867 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 868 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 869 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 870 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 871 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 872 | if( zName==0 ) return; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 873 | pIndex = sqliteFindIndex(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 874 | sqliteFree(zName); |
| 875 | if( pIndex==0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 876 | sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0, |
| 877 | pName->z, pName->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 878 | pParse->nErr++; |
| 879 | return; |
| 880 | } |
| 881 | |
| 882 | /* Generate code to remove the index and from the master table */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 883 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 884 | if( v ){ |
| 885 | static VdbeOp dropIndex[] = { |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 886 | { OP_Open, 0, 2, MASTER_NAME}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 887 | { OP_Rewind, 0, 0, 0}, |
| 888 | { OP_String, 0, 0, 0}, /* 2 */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 889 | { OP_Next, 0, ADDR(8), 0}, /* 3 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 890 | { OP_Dup, 0, 0, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 891 | { OP_Column, 0, 1, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 892 | { OP_Ne, 0, ADDR(3), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 893 | { OP_Delete, 0, 0, 0}, |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 894 | { OP_Destroy, 0, 0, 0}, /* 8 */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 895 | { OP_SetCookie, 0, 0, 0}, /* 9 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 896 | { OP_Close, 0, 0, 0}, |
| 897 | }; |
| 898 | int base; |
| 899 | |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 900 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 901 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 902 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 903 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 904 | base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 905 | sqliteVdbeChangeP3(v, base+2, pIndex->zName, 0); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 906 | sqliteVdbeChangeP1(v, base+8, pIndex->tnum); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 907 | changeCookie(db); |
| 908 | sqliteVdbeChangeP1(v, base+9, db->next_cookie); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 909 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 910 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 911 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 912 | } |
| 913 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 914 | /* Mark the internal Index structure for deletion by the |
| 915 | ** sqliteCommitInternalChanges routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 916 | */ |
| 917 | if( !pParse->explain ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 918 | pIndex->isDelete = 1; |
| 919 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | ** Add a new element to the end of an expression list. If pList is |
| 925 | ** initially NULL, then create a new expression list. |
| 926 | */ |
| 927 | ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ |
| 928 | int i; |
| 929 | if( pList==0 ){ |
| 930 | pList = sqliteMalloc( sizeof(ExprList) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 931 | if( pList==0 ) return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 932 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 933 | if( (pList->nExpr & 7)==0 ){ |
| 934 | int n = pList->nExpr + 8; |
| 935 | pList->a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); |
| 936 | if( pList->a==0 ){ |
| 937 | pList->nExpr = 0; |
| 938 | return pList; |
| 939 | } |
| 940 | } |
| 941 | i = pList->nExpr++; |
| 942 | pList->a[i].pExpr = pExpr; |
| 943 | pList->a[i].zName = 0; |
| 944 | if( pName ){ |
| 945 | sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 946 | sqliteDequote(pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 947 | } |
| 948 | return pList; |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | ** Delete an entire expression list. |
| 953 | */ |
| 954 | void sqliteExprListDelete(ExprList *pList){ |
| 955 | int i; |
| 956 | if( pList==0 ) return; |
| 957 | for(i=0; i<pList->nExpr; i++){ |
| 958 | sqliteExprDelete(pList->a[i].pExpr); |
| 959 | sqliteFree(pList->a[i].zName); |
| 960 | } |
| 961 | sqliteFree(pList->a); |
| 962 | sqliteFree(pList); |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | ** Append a new element to the given IdList. Create a new IdList if |
| 967 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 968 | ** |
| 969 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 970 | */ |
| 971 | IdList *sqliteIdListAppend(IdList *pList, Token *pToken){ |
| 972 | if( pList==0 ){ |
| 973 | pList = sqliteMalloc( sizeof(IdList) ); |
| 974 | if( pList==0 ) return 0; |
| 975 | } |
| 976 | if( (pList->nId & 7)==0 ){ |
| 977 | pList->a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) ); |
| 978 | if( pList->a==0 ){ |
| 979 | pList->nId = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 980 | sqliteIdListDelete(pList); |
| 981 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | memset(&pList->a[pList->nId], 0, sizeof(pList->a[0])); |
| 985 | if( pToken ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 986 | char **pz = &pList->a[pList->nId].zName; |
| 987 | sqliteSetNString(pz, pToken->z, pToken->n, 0); |
| 988 | if( *pz==0 ){ |
| 989 | sqliteIdListDelete(pList); |
| 990 | return 0; |
| 991 | }else{ |
| 992 | sqliteDequote(*pz); |
| 993 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 994 | } |
| 995 | pList->nId++; |
| 996 | return pList; |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | ** Add an alias to the last identifier on the given identifier list. |
| 1001 | */ |
| 1002 | void sqliteIdListAddAlias(IdList *pList, Token *pToken){ |
| 1003 | if( pList && pList->nId>0 ){ |
| 1004 | int i = pList->nId - 1; |
| 1005 | sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1006 | sqliteDequote(pList->a[i].zAlias); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | ** Delete an entire IdList |
| 1012 | */ |
| 1013 | void sqliteIdListDelete(IdList *pList){ |
| 1014 | int i; |
| 1015 | if( pList==0 ) return; |
| 1016 | for(i=0; i<pList->nId; i++){ |
| 1017 | sqliteFree(pList->a[i].zName); |
| 1018 | sqliteFree(pList->a[i].zAlias); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1019 | if( pList->a[i].pSelect ){ |
| 1020 | sqliteFree(pList->a[i].zName); |
| 1021 | sqliteSelectDelete(pList->a[i].pSelect); |
| 1022 | sqliteDeleteTable(0, pList->a[i].pTab); |
| 1023 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1024 | } |
| 1025 | sqliteFree(pList->a); |
| 1026 | sqliteFree(pList); |
| 1027 | } |
| 1028 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1029 | |
| 1030 | /* |
| 1031 | ** The COPY command is for compatibility with PostgreSQL and specificially |
| 1032 | ** for the ability to read the output of pg_dump. The format is as |
| 1033 | ** follows: |
| 1034 | ** |
| 1035 | ** COPY table FROM file [USING DELIMITERS string] |
| 1036 | ** |
| 1037 | ** "table" is an existing table name. We will read lines of code from |
| 1038 | ** file to fill this table with data. File might be "stdin". The optional |
| 1039 | ** delimiter string identifies the field separators. The default is a tab. |
| 1040 | */ |
| 1041 | void sqliteCopy( |
| 1042 | Parse *pParse, /* The parser context */ |
| 1043 | Token *pTableName, /* The name of the table into which we will insert */ |
| 1044 | Token *pFilename, /* The file from which to obtain information */ |
| 1045 | Token *pDelimiter /* Use this as the field delimiter */ |
| 1046 | ){ |
| 1047 | Table *pTab; |
| 1048 | char *zTab; |
| 1049 | int i, j; |
| 1050 | Vdbe *v; |
| 1051 | int addr, end; |
| 1052 | Index *pIdx; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1053 | sqlite *db = pParse->db; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1054 | |
| 1055 | zTab = sqliteTableNameFromToken(pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1056 | if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1057 | pTab = sqliteFindTable(db, zTab); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1058 | sqliteFree(zTab); |
| 1059 | if( pTab==0 ){ |
| 1060 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 1061 | pTableName->z, pTableName->n, 0); |
| 1062 | pParse->nErr++; |
| 1063 | goto copy_cleanup; |
| 1064 | } |
| 1065 | if( pTab->readOnly ){ |
| 1066 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 1067 | " may not be modified", 0); |
| 1068 | pParse->nErr++; |
| 1069 | goto copy_cleanup; |
| 1070 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1071 | v = sqliteGetVdbe(pParse); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1072 | if( v ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1073 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1074 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1075 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1076 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1077 | addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0, 0, 0); |
| 1078 | sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n); |
drh | b766599 | 2000-05-30 17:30:35 +0000 | [diff] [blame] | 1079 | sqliteVdbeDequoteP3(v, addr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1080 | sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1081 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1082 | sqliteVdbeAddOp(v, OP_Open, i, pIdx->tnum, pIdx->zName, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1083 | } |
| 1084 | end = sqliteVdbeMakeLabel(v); |
| 1085 | addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end, 0, 0); |
| 1086 | if( pDelimiter ){ |
| 1087 | sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n); |
| 1088 | sqliteVdbeDequoteP3(v, addr); |
| 1089 | }else{ |
| 1090 | sqliteVdbeChangeP3(v, addr, "\t", 1); |
| 1091 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1092 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1093 | if( pTab->pIndex ){ |
| 1094 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 1095 | } |
| 1096 | for(i=0; i<pTab->nCol; i++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1097 | sqliteVdbeAddOp(v, OP_FileColumn, i, 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1098 | } |
| 1099 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0); |
| 1100 | sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0); |
| 1101 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 1102 | if( pIdx->pNext ){ |
| 1103 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 1104 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1105 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1106 | sqliteVdbeAddOp(v, OP_FileColumn, pIdx->aiColumn[j], 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1107 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1108 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1109 | sqliteVdbeAddOp(v, OP_PutIdx, i, 0, 0, 0); |
| 1110 | } |
| 1111 | sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0); |
| 1112 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, end); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1113 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1114 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 1115 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | copy_cleanup: |
| 1119 | return; |
| 1120 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1121 | |
| 1122 | /* |
| 1123 | ** The non-standard VACUUM command is used to clean up the database, |
| 1124 | ** collapse free space, etc. It is modelled after the VACUUM command |
| 1125 | ** in PostgreSQL. |
| 1126 | */ |
| 1127 | void sqliteVacuum(Parse *pParse, Token *pTableName){ |
| 1128 | char *zName; |
| 1129 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1130 | sqlite *db = pParse->db; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1131 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1132 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1133 | if( pTableName ){ |
| 1134 | zName = sqliteTableNameFromToken(pTableName); |
| 1135 | }else{ |
| 1136 | zName = 0; |
| 1137 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1138 | if( zName && sqliteFindIndex(db, zName)==0 |
| 1139 | && sqliteFindTable(db, zName)==0 ){ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1140 | sqliteSetString(&pParse->zErrMsg, "no such table or index: ", zName, 0); |
| 1141 | pParse->nErr++; |
| 1142 | goto vacuum_cleanup; |
| 1143 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1144 | v = sqliteGetVdbe(pParse); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1145 | if( v==0 ) goto vacuum_cleanup; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1146 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1147 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1148 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1149 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1150 | if( zName ){ |
| 1151 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, zName, 0); |
| 1152 | }else{ |
| 1153 | int h; |
| 1154 | Table *pTab; |
| 1155 | Index *pIdx; |
| 1156 | for(h=0; h<N_HASH; h++){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1157 | for(pTab=db->apTblHash[h]; pTab; pTab=pTab->pHash){ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1158 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pTab->zName, 0); |
| 1159 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1160 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pIdx->zName, 0); |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1165 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1166 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 1167 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1168 | |
| 1169 | vacuum_cleanup: |
| 1170 | sqliteFree(zName); |
| 1171 | return; |
| 1172 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1173 | |
| 1174 | /* |
| 1175 | ** Begin a transaction |
| 1176 | */ |
| 1177 | void sqliteBeginTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1178 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1179 | Vdbe *v; |
| 1180 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1181 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1182 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1183 | if( db->flags & SQLITE_InTrans ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1184 | v = sqliteGetVdbe(pParse); |
| 1185 | if( v ){ |
| 1186 | sqliteVdbeAddOp(v, OP_Transaction, 1, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1187 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1188 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1189 | db->flags |= SQLITE_InTrans; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | ** Commit a transaction |
| 1194 | */ |
| 1195 | void sqliteCommitTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1196 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1197 | Vdbe *v; |
| 1198 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1199 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1200 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1201 | if( (db->flags & SQLITE_InTrans)==0 ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1202 | v = sqliteGetVdbe(pParse); |
| 1203 | if( v ){ |
| 1204 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1205 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1206 | db->flags &= ~SQLITE_InTrans; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | ** Rollback a transaction |
| 1211 | */ |
| 1212 | void sqliteRollbackTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1213 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1214 | Vdbe *v; |
| 1215 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1216 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1217 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1218 | if( (db->flags & SQLITE_InTrans)==0 ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1219 | v = sqliteGetVdbe(pParse); |
| 1220 | if( v ){ |
| 1221 | sqliteVdbeAddOp(v, OP_Rollback, 0, 0, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1222 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1223 | db->flags &= ~SQLITE_InTrans; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1224 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1225 | |
| 1226 | /* |
| 1227 | ** Interpret the given string as a boolean value. |
| 1228 | */ |
| 1229 | static int getBoolean(char *z){ |
| 1230 | static char *azTrue[] = { "yes", "on", "true" }; |
| 1231 | int i; |
| 1232 | if( z[0]==0 ) return 0; |
| 1233 | if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){ |
| 1234 | return atoi(z); |
| 1235 | } |
| 1236 | for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){ |
| 1237 | if( sqliteStrICmp(z,azTrue[i])==0 ) return 1; |
| 1238 | } |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | ** Process a pragma statement. |
| 1244 | ** |
| 1245 | ** Pragmas are of this form: |
| 1246 | ** |
| 1247 | ** PRAGMA id = value |
| 1248 | ** |
| 1249 | ** The identifier might also be a string. The value is a string, and |
| 1250 | ** identifier, or a number. If minusFlag is true, then the value is |
| 1251 | ** a number that was preceded by a minus sign. |
| 1252 | */ |
| 1253 | void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){ |
| 1254 | char *zLeft = 0; |
| 1255 | char *zRight = 0; |
| 1256 | sqlite *db = pParse->db; |
| 1257 | |
| 1258 | zLeft = sqliteStrNDup(pLeft->z, pLeft->n); |
| 1259 | sqliteDequote(zLeft); |
| 1260 | if( minusFlag ){ |
| 1261 | zRight = 0; |
| 1262 | sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0); |
| 1263 | }else{ |
| 1264 | zRight = sqliteStrNDup(pRight->z, pRight->n); |
| 1265 | sqliteDequote(zRight); |
| 1266 | } |
| 1267 | |
| 1268 | if( sqliteStrICmp(zLeft,"cache_size")==0 ){ |
| 1269 | int size = atoi(zRight); |
| 1270 | sqliteBtreeSetCacheSize(db->pBe, size); |
| 1271 | }else |
| 1272 | |
| 1273 | if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){ |
| 1274 | if( getBoolean(zRight) ){ |
| 1275 | db->flags |= SQLITE_VdbeTrace; |
| 1276 | }else{ |
| 1277 | db->flags &= ~SQLITE_VdbeTrace; |
| 1278 | } |
| 1279 | }else |
| 1280 | |
| 1281 | #ifndef NDEBUG |
| 1282 | if( sqliteStrICmp(zLeft, "parser_trace")==0 ){ |
| 1283 | extern void sqliteParserTrace(FILE*, char *); |
| 1284 | if( getBoolean(zRight) ){ |
| 1285 | sqliteParserTrace(stdout, "parser: "); |
| 1286 | }else{ |
| 1287 | sqliteParserTrace(0, 0); |
| 1288 | } |
| 1289 | }else |
| 1290 | #endif |
| 1291 | |
| 1292 | if( zLeft ) sqliteFree(zLeft); |
| 1293 | if( zRight ) sqliteFree(zRight); |
| 1294 | } |