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