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 |
| 26 | ** the following kinds of rules: |
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 | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame^] | 36 | ** $Id: build.c,v 1.15 2000/06/05 18:54:46 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 37 | */ |
| 38 | #include "sqliteInt.h" |
| 39 | |
| 40 | /* |
| 41 | ** This routine is called after a single SQL statement has been |
| 42 | ** parsed and we want to execute the code to implement |
| 43 | ** the statement. Prior action routines should have already |
| 44 | ** constructed VDBE code to do the work of the SQL statement. |
| 45 | ** This routine just has to execute the VDBE code. |
| 46 | ** |
| 47 | ** Note that if an error occurred, it might be the case that |
| 48 | ** no VDBE code was generated. |
| 49 | */ |
| 50 | void sqliteExec(Parse *pParse){ |
| 51 | if( pParse->pVdbe ){ |
| 52 | if( pParse->explain ){ |
| 53 | sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg, |
| 54 | &pParse->zErrMsg); |
| 55 | }else{ |
| 56 | FILE *trace = (pParse->db->flags & SQLITE_VdbeTrace)!=0 ? stderr : 0; |
| 57 | sqliteVdbeTrace(pParse->pVdbe, trace); |
| 58 | sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg, |
| 59 | &pParse->zErrMsg); |
| 60 | } |
| 61 | sqliteVdbeDelete(pParse->pVdbe); |
| 62 | pParse->pVdbe = 0; |
| 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 | } |
| 82 | return pNew; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | ** Construct a new expression node for a function with multiple |
| 87 | ** arguments. |
| 88 | */ |
| 89 | Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ |
| 90 | Expr *pNew; |
| 91 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 92 | if( pNew==0 ) return 0; |
| 93 | pNew->op = TK_FUNCTION; |
| 94 | pNew->pList = pList; |
| 95 | if( pToken ){ |
| 96 | pNew->token = *pToken; |
| 97 | }else{ |
| 98 | pNew->token.z = ""; |
| 99 | pNew->token.n = 0; |
| 100 | } |
| 101 | return pNew; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | ** Recursively delete an expression tree. |
| 106 | */ |
| 107 | void sqliteExprDelete(Expr *p){ |
| 108 | if( p==0 ) return; |
| 109 | if( p->pLeft ) sqliteExprDelete(p->pLeft); |
| 110 | if( p->pRight ) sqliteExprDelete(p->pRight); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame^] | 111 | if( p->pList ) sqliteExprListDelete(p->pList); |
| 112 | if( p->pSelect ) sqliteSelectDelete(p->pSelect); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 113 | sqliteFree(p); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | ** Locate the in-memory structure that describes the |
| 118 | ** format of a particular database table given the name |
| 119 | ** of that table. Return NULL if not found. |
| 120 | */ |
| 121 | Table *sqliteFindTable(sqlite *db, char *zName){ |
| 122 | Table *pTable; |
| 123 | int h; |
| 124 | |
| 125 | h = sqliteHashNoCase(zName, 0) % N_HASH; |
| 126 | for(pTable=db->apTblHash[h]; pTable; pTable=pTable->pHash){ |
| 127 | if( sqliteStrICmp(pTable->zName, zName)==0 ) return pTable; |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | ** Locate the in-memory structure that describes the |
| 134 | ** format of a particular index table given the name |
| 135 | ** of that table. Return NULL if not found. |
| 136 | */ |
| 137 | Index *sqliteFindIndex(sqlite *db, char *zName){ |
| 138 | Index *p; |
| 139 | int h; |
| 140 | |
| 141 | h = sqliteHashNoCase(zName, 0) % N_HASH; |
| 142 | for(p=db->apIdxHash[h]; p; p=p->pHash){ |
| 143 | if( sqliteStrICmp(p->zName, zName)==0 ) return p; |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | ** Remove the given index from the index hash table, and free |
| 150 | ** its memory structures. |
| 151 | ** |
| 152 | ** The index is removed from the database hash table, but it is |
| 153 | ** not unlinked from the table that is being indexed. Unlinking |
| 154 | ** from the table must be done by the calling function. |
| 155 | */ |
| 156 | static void sqliteDeleteIndex(sqlite *db, Index *pIndex){ |
| 157 | int h; |
| 158 | if( pIndex->zName ){ |
| 159 | h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH; |
| 160 | if( db->apIdxHash[h]==pIndex ){ |
| 161 | db->apIdxHash[h] = pIndex->pHash; |
| 162 | }else{ |
| 163 | Index *p; |
| 164 | for(p=db->apIdxHash[h]; p && p->pHash!=pIndex; p=p->pHash){} |
| 165 | if( p && p->pHash==pIndex ){ |
| 166 | p->pHash = pIndex->pHash; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | sqliteFree(pIndex); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | ** Remove the memory data structures associated with the given |
| 175 | ** table. No changes are made to disk by this routine. |
| 176 | ** |
| 177 | ** This routine just deletes the data structure. It does not unlink |
| 178 | ** the table data structure from the hash table. But does it destroy |
| 179 | ** memory structures of the indices associated with the table. |
| 180 | */ |
| 181 | void sqliteDeleteTable(sqlite *db, Table *pTable){ |
| 182 | int i; |
| 183 | Index *pIndex, *pNext; |
| 184 | if( pTable==0 ) return; |
| 185 | for(i=0; i<pTable->nCol; i++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 186 | sqliteFree(pTable->aCol[i].zName); |
| 187 | sqliteFree(pTable->aCol[i].zDflt); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 188 | } |
| 189 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 190 | pNext = pIndex->pNext; |
| 191 | sqliteDeleteIndex(db, pIndex); |
| 192 | } |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 193 | sqliteFree(pTable->aCol); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 194 | sqliteFree(pTable); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | ** Construct the name of a user table from a token. |
| 199 | ** |
| 200 | ** Space to hold the name is obtained from sqliteMalloc() and must |
| 201 | ** be freed by the calling function. |
| 202 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 203 | char *sqliteTableNameFromToken(Token *pName){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 204 | char *zName = 0; |
| 205 | sqliteSetNString(&zName, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 206 | sqliteDequote(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 207 | return zName; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | ** Begin constructing a new table representation in memory. This is |
| 212 | ** the first of several action routines that get called in response |
| 213 | ** to a CREATE TABLE statement. |
| 214 | */ |
| 215 | void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName){ |
| 216 | Table *pTable; |
| 217 | char *zName; |
| 218 | |
| 219 | pParse->sFirstToken = *pStart; |
| 220 | zName = sqliteTableNameFromToken(pName); |
| 221 | pTable = sqliteFindTable(pParse->db, zName); |
| 222 | if( pTable!=0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 223 | sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n, |
| 224 | " already exists", 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 225 | sqliteFree(zName); |
| 226 | pParse->nErr++; |
| 227 | return; |
| 228 | } |
| 229 | if( sqliteFindIndex(pParse->db, zName) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 230 | sqliteSetString(&pParse->zErrMsg, "there is already an index named ", |
| 231 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 232 | sqliteFree(zName); |
| 233 | pParse->nErr++; |
| 234 | return; |
| 235 | } |
| 236 | pTable = sqliteMalloc( sizeof(Table) ); |
| 237 | if( pTable==0 ){ |
| 238 | sqliteSetString(&pParse->zErrMsg, "out of memory", 0); |
| 239 | pParse->nErr++; |
| 240 | return; |
| 241 | } |
| 242 | pTable->zName = zName; |
| 243 | pTable->pHash = 0; |
| 244 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 245 | pTable->aCol = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 246 | pTable->pIndex = 0; |
| 247 | if( pParse->pNewTable ) sqliteDeleteTable(pParse->db, pParse->pNewTable); |
| 248 | pParse->pNewTable = pTable; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | ** Add a new column to the table currently being constructed. |
| 253 | */ |
| 254 | void sqliteAddColumn(Parse *pParse, Token *pName){ |
| 255 | Table *p; |
| 256 | char **pz; |
| 257 | if( (p = pParse->pNewTable)==0 ) return; |
| 258 | if( (p->nCol & 0x7)==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 259 | p->aCol = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 260 | } |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 261 | if( p->aCol==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 262 | p->nCol = 0; |
| 263 | return; |
| 264 | } |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 265 | memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0])); |
| 266 | pz = &p->aCol[p->nCol++].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 267 | sqliteSetNString(pz, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 268 | sqliteDequote(*pz); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 272 | ** The given token is the default value for the last column added to |
| 273 | ** the table currently under construction. If "minusFlag" is true, it |
| 274 | ** means the value token was preceded by a minus sign. |
| 275 | */ |
| 276 | void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ |
| 277 | Table *p; |
| 278 | int i; |
| 279 | char **pz; |
| 280 | if( (p = pParse->pNewTable)==0 ) return; |
| 281 | i = p->nCol-1; |
| 282 | pz = &p->aCol[i].zDflt; |
| 283 | if( minusFlag ){ |
| 284 | sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0); |
| 285 | }else{ |
| 286 | sqliteSetNString(pz, pVal->z, pVal->n, 0); |
| 287 | } |
| 288 | sqliteDequote(*pz); |
| 289 | } |
| 290 | |
| 291 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 292 | ** This routine is called to report the final ")" that terminates |
| 293 | ** a CREATE TABLE statement. |
| 294 | ** |
| 295 | ** The table structure is added to the internal hash tables. |
| 296 | ** |
| 297 | ** An entry for the table is made in the master table, unless |
| 298 | ** initFlag==1. When initFlag==1, it means we are reading the |
| 299 | ** master table because we just connected to the database, so |
| 300 | ** the entry for this table already exists in the master table. |
| 301 | ** We do not want to create it again. |
| 302 | */ |
| 303 | void sqliteEndTable(Parse *pParse, Token *pEnd){ |
| 304 | Table *p; |
| 305 | int h; |
| 306 | |
| 307 | if( pParse->nErr ) return; |
| 308 | |
| 309 | /* Add the table to the in-memory representation of the database |
| 310 | */ |
| 311 | if( (p = pParse->pNewTable)!=0 && pParse->explain==0 ){ |
| 312 | h = sqliteHashNoCase(p->zName, 0) % N_HASH; |
| 313 | p->pHash = pParse->db->apTblHash[h]; |
| 314 | pParse->db->apTblHash[h] = p; |
| 315 | pParse->pNewTable = 0; |
| 316 | } |
| 317 | |
| 318 | /* If not initializing, then create the table on disk. |
| 319 | */ |
| 320 | if( !pParse->initFlag ){ |
| 321 | static VdbeOp addTable[] = { |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 322 | { OP_Open, 0, 1, MASTER_NAME }, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 323 | { OP_New, 0, 0, 0}, |
| 324 | { OP_String, 0, 0, "table" }, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 325 | { OP_String, 0, 0, 0}, /* 3 */ |
| 326 | { OP_String, 0, 0, 0}, /* 4 */ |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 327 | { OP_String, 0, 0, 0}, /* 5 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 328 | { OP_MakeRecord, 4, 0, 0}, |
| 329 | { OP_Put, 0, 0, 0}, |
| 330 | { OP_Close, 0, 0, 0}, |
| 331 | }; |
| 332 | int n, base; |
| 333 | Vdbe *v = pParse->pVdbe; |
| 334 | |
| 335 | if( v==0 ){ |
| 336 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); |
| 337 | } |
| 338 | if( v==0 ) return; |
| 339 | n = (int)pEnd->z - (int)pParse->sFirstToken.z + 1; |
| 340 | base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 341 | sqliteVdbeChangeP3(v, base+3, p->zName, 0); |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 342 | sqliteVdbeChangeP3(v, base+4, p->zName, 0); |
| 343 | sqliteVdbeChangeP3(v, base+5, pParse->sFirstToken.z, n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | ** Given a token, look up a table with that name. If not found, leave |
| 349 | ** an error for the parser to find and return NULL. |
| 350 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 351 | Table *sqliteTableFromToken(Parse *pParse, Token *pTok){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 352 | char *zName = sqliteTableNameFromToken(pTok); |
| 353 | Table *pTab = sqliteFindTable(pParse->db, zName); |
| 354 | sqliteFree(zName); |
| 355 | if( pTab==0 ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 356 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 357 | pTok->z, pTok->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 358 | pParse->nErr++; |
| 359 | } |
| 360 | return pTab; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | ** This routine is called to do the work of a DROP TABLE statement. |
| 365 | */ |
| 366 | void sqliteDropTable(Parse *pParse, Token *pName){ |
| 367 | Table *pTable; |
| 368 | int h; |
| 369 | Vdbe *v; |
| 370 | int base; |
| 371 | |
| 372 | pTable = sqliteTableFromToken(pParse, pName); |
| 373 | if( pTable==0 ) return; |
| 374 | if( pTable->readOnly ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 375 | sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName, |
| 376 | " may not be dropped", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 377 | pParse->nErr++; |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | /* Generate code to remove the table and its reference in sys_master */ |
| 382 | v = pParse->pVdbe; |
| 383 | if( v==0 ){ |
| 384 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); |
| 385 | } |
| 386 | if( v ){ |
| 387 | static VdbeOp dropTable[] = { |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 388 | { OP_Open, 0, 1, MASTER_NAME }, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 389 | { OP_ListOpen, 0, 0, 0}, |
| 390 | { OP_String, 0, 0, 0}, /* 2 */ |
| 391 | { OP_Next, 0, ADDR(10), 0}, /* 3 */ |
| 392 | { OP_Dup, 0, 0, 0}, |
| 393 | { OP_Field, 0, 2, 0}, |
| 394 | { OP_Ne, 0, ADDR(3), 0}, |
| 395 | { OP_Key, 0, 0, 0}, |
| 396 | { OP_ListWrite, 0, 0, 0}, |
| 397 | { OP_Goto, 0, ADDR(3), 0}, |
| 398 | { OP_ListRewind, 0, 0, 0}, /* 10 */ |
| 399 | { OP_ListRead, 0, ADDR(14), 0}, /* 11 */ |
| 400 | { OP_Delete, 0, 0, 0}, |
| 401 | { OP_Goto, 0, ADDR(11), 0}, |
| 402 | { OP_Destroy, 0, 0, 0}, /* 14 */ |
| 403 | { OP_Close, 0, 0, 0}, |
| 404 | }; |
| 405 | Index *pIdx; |
| 406 | base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); |
| 407 | sqliteVdbeChangeP3(v, base+2, pTable->zName, 0); |
| 408 | sqliteVdbeChangeP3(v, base+14, pTable->zName, 0); |
| 409 | for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 410 | sqliteVdbeAddOp(v, OP_Destroy, 0, 0, pIdx->zName, 0); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /* Remove the table structure and free its memory. |
| 415 | ** |
| 416 | ** Exception: if the SQL statement began with the EXPLAIN keyword, |
| 417 | ** then no changes are made. |
| 418 | */ |
| 419 | if( !pParse->explain ){ |
| 420 | h = sqliteHashNoCase(pTable->zName, 0) % N_HASH; |
| 421 | if( pParse->db->apTblHash[h]==pTable ){ |
| 422 | pParse->db->apTblHash[h] = pTable->pHash; |
| 423 | }else{ |
| 424 | Table *p; |
| 425 | for(p=pParse->db->apTblHash[h]; p && p->pHash!=pTable; p=p->pHash){} |
| 426 | if( p && p->pHash==pTable ){ |
| 427 | p->pHash = pTable->pHash; |
| 428 | } |
| 429 | } |
| 430 | sqliteDeleteTable(pParse->db, pTable); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | ** Create a new index for an SQL table. pIndex is the name of the index |
| 436 | ** and pTable is the name of the table that is to be indexed. Both will |
| 437 | ** be NULL for a primary key. In that case, use pParse->pNewTable as the |
| 438 | ** table to be indexed. |
| 439 | ** |
| 440 | ** pList is a list of fields to be indexed. pList will be NULL if the |
| 441 | ** most recently added field of the table is labeled as the primary key. |
| 442 | */ |
| 443 | void sqliteCreateIndex( |
| 444 | Parse *pParse, /* All information about this parse */ |
| 445 | Token *pName, /* Name of the index. May be NULL */ |
| 446 | Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */ |
| 447 | IdList *pList, /* A list of fields to be indexed */ |
| 448 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
| 449 | Token *pEnd /* The ")" that closes the CREATE INDEX statement */ |
| 450 | ){ |
| 451 | Table *pTab; /* Table to be indexed */ |
| 452 | Index *pIndex; /* The index to be created */ |
| 453 | char *zName = 0; |
| 454 | int i, j, h; |
| 455 | Token nullId; /* Fake token for an empty ID list */ |
| 456 | |
| 457 | /* |
| 458 | ** Find the table that is to be indexed. Return early if not found. |
| 459 | */ |
| 460 | if( pTable!=0 ){ |
| 461 | pTab = sqliteTableFromToken(pParse, pTable); |
| 462 | }else{ |
| 463 | pTab = pParse->pNewTable; |
| 464 | } |
| 465 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
| 466 | if( pTab->readOnly ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 467 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 468 | " may not have new indices added", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 469 | pParse->nErr++; |
| 470 | goto exit_create_index; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | ** Find the name of the index. Make sure there is not already another |
| 475 | ** index or table with the same name. |
| 476 | */ |
| 477 | if( pName ){ |
| 478 | zName = sqliteTableNameFromToken(pName); |
| 479 | }else{ |
| 480 | zName = 0; |
| 481 | sqliteSetString(&zName, pTab->zName, "__primary_key", 0); |
| 482 | } |
| 483 | if( sqliteFindIndex(pParse->db, zName) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 484 | sqliteSetString(&pParse->zErrMsg, "index ", zName, |
| 485 | " already exists", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 486 | pParse->nErr++; |
| 487 | goto exit_create_index; |
| 488 | } |
| 489 | if( sqliteFindTable(pParse->db, zName) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 490 | sqliteSetString(&pParse->zErrMsg, "there is already a table named ", |
| 491 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 492 | pParse->nErr++; |
| 493 | goto exit_create_index; |
| 494 | } |
| 495 | |
| 496 | /* If pList==0, it means this routine was called to make a primary |
| 497 | ** key out of the last field added to the table under construction. |
| 498 | ** So create a fake list to simulate this. |
| 499 | */ |
| 500 | if( pList==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 501 | nullId.z = pTab->aCol[pTab->nCol-1].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 502 | nullId.n = strlen(nullId.z); |
| 503 | pList = sqliteIdListAppend(0, &nullId); |
| 504 | if( pList==0 ) goto exit_create_index; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | ** Allocate the index structure. |
| 509 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 510 | pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 511 | sizeof(int)*pList->nId ); |
| 512 | if( pIndex==0 ){ |
| 513 | sqliteSetString(&pParse->zErrMsg, "out of memory", 0); |
| 514 | pParse->nErr++; |
| 515 | goto exit_create_index; |
| 516 | } |
| 517 | pIndex->aiField = (int*)&pIndex[1]; |
| 518 | pIndex->zName = (char*)&pIndex->aiField[pList->nId]; |
| 519 | strcpy(pIndex->zName, zName); |
| 520 | pIndex->pTable = pTab; |
| 521 | pIndex->nField = pList->nId; |
| 522 | |
| 523 | /* Scan the names of the fields of the table to be indexed and |
| 524 | ** load the field indices into the Index structure. Report an error |
| 525 | ** if any field is not found. |
| 526 | */ |
| 527 | for(i=0; i<pList->nId; i++){ |
| 528 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 529 | if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 530 | } |
| 531 | if( j>=pTab->nCol ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 532 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 533 | " has no field named ", pList->a[i].zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 534 | pParse->nErr++; |
| 535 | sqliteFree(pIndex); |
| 536 | goto exit_create_index; |
| 537 | } |
| 538 | pIndex->aiField[i] = j; |
| 539 | } |
| 540 | |
| 541 | /* Link the new Index structure to its table and to the other |
| 542 | ** in-memory database structures. |
| 543 | */ |
| 544 | if( pParse->explain==0 ){ |
| 545 | h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH; |
| 546 | pIndex->pHash = pParse->db->apIdxHash[h]; |
| 547 | pParse->db->apIdxHash[h] = pIndex; |
| 548 | pIndex->pNext = pTab->pIndex; |
| 549 | pTab->pIndex = pIndex; |
| 550 | } |
| 551 | |
| 552 | /* If the initFlag is 0 then create the index on disk. This |
| 553 | ** involves writing the index into the master table and filling in the |
| 554 | ** index with the current table contents. |
| 555 | ** |
| 556 | ** The initFlag is 0 when the user first enters a CREATE INDEX |
| 557 | ** command. The initFlag is 1 when a database is opened and |
| 558 | ** CREATE INDEX statements are read out of the master table. In |
| 559 | ** the latter case the index already exists on disk, which is why |
| 560 | ** we don't want to recreate it. |
| 561 | */ |
| 562 | if( pParse->initFlag==0 ){ |
| 563 | static VdbeOp addTable[] = { |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 564 | { OP_Open, 2, 1, MASTER_NAME}, |
| 565 | { OP_New, 2, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 566 | { OP_String, 0, 0, "index"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 567 | { OP_String, 0, 0, 0}, /* 3 */ |
| 568 | { OP_String, 0, 0, 0}, /* 4 */ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 569 | { OP_String, 0, 0, 0}, /* 5 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 570 | { OP_MakeRecord, 4, 0, 0}, |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 571 | { OP_Put, 2, 0, 0}, |
| 572 | { OP_Close, 2, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 573 | }; |
| 574 | int n; |
| 575 | Vdbe *v = pParse->pVdbe; |
| 576 | int lbl1, lbl2; |
| 577 | int i; |
| 578 | |
| 579 | if( v==0 ){ |
| 580 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); |
| 581 | } |
| 582 | if( v==0 ) goto exit_create_index; |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 583 | sqliteVdbeAddOp(v, OP_Open, 0, 0, pTab->zName, 0); |
| 584 | sqliteVdbeAddOp(v, OP_Open, 1, 1, pIndex->zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 585 | if( pStart && pEnd ){ |
| 586 | int base; |
| 587 | n = (int)pEnd->z - (int)pStart->z + 1; |
| 588 | base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable); |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 589 | sqliteVdbeChangeP3(v, base+3, pIndex->zName, 0); |
| 590 | sqliteVdbeChangeP3(v, base+4, pTab->zName, 0); |
| 591 | sqliteVdbeChangeP3(v, base+5, pStart->z, n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 592 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 593 | lbl1 = sqliteVdbeMakeLabel(v); |
| 594 | lbl2 = sqliteVdbeMakeLabel(v); |
| 595 | sqliteVdbeAddOp(v, OP_Next, 0, lbl2, 0, lbl1); |
| 596 | sqliteVdbeAddOp(v, OP_Key, 0, 0, 0, 0); |
| 597 | for(i=0; i<pIndex->nField; i++){ |
| 598 | sqliteVdbeAddOp(v, OP_Field, 0, pIndex->aiField[i], 0, 0); |
| 599 | } |
| 600 | sqliteVdbeAddOp(v, OP_MakeKey, pIndex->nField, 0, 0, 0); |
| 601 | sqliteVdbeAddOp(v, OP_PutIdx, 1, 0, 0, 0); |
| 602 | sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0); |
| 603 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 604 | sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 605 | sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* Reclaim memory on an EXPLAIN call. |
| 609 | */ |
| 610 | if( pParse->explain ){ |
| 611 | sqliteFree(pIndex); |
| 612 | } |
| 613 | |
| 614 | /* Clean up before exiting */ |
| 615 | exit_create_index: |
| 616 | sqliteIdListDelete(pList); |
| 617 | sqliteFree(zName); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | ** This routine will drop an existing named index. |
| 623 | */ |
| 624 | void sqliteDropIndex(Parse *pParse, Token *pName){ |
| 625 | Index *pIndex; |
| 626 | char *zName; |
| 627 | Vdbe *v; |
| 628 | |
| 629 | zName = sqliteTableNameFromToken(pName); |
| 630 | pIndex = sqliteFindIndex(pParse->db, zName); |
| 631 | sqliteFree(zName); |
| 632 | if( pIndex==0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 633 | sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0, |
| 634 | pName->z, pName->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 635 | pParse->nErr++; |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | /* Generate code to remove the index and from the master table */ |
| 640 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); |
| 641 | if( v ){ |
| 642 | static VdbeOp dropIndex[] = { |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 643 | { OP_Open, 0, 1, MASTER_NAME}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 644 | { OP_ListOpen, 0, 0, 0}, |
| 645 | { OP_String, 0, 0, 0}, /* 2 */ |
| 646 | { OP_Next, 0, ADDR(9), 0}, /* 3 */ |
| 647 | { OP_Dup, 0, 0, 0}, |
| 648 | { OP_Field, 0, 1, 0}, |
| 649 | { OP_Ne, 0, ADDR(3), 0}, |
| 650 | { OP_Key, 0, 0, 0}, |
| 651 | { OP_Delete, 0, 0, 0}, |
| 652 | { OP_Destroy, 0, 0, 0}, /* 9 */ |
| 653 | { OP_Close, 0, 0, 0}, |
| 654 | }; |
| 655 | int base; |
| 656 | |
| 657 | base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); |
| 658 | sqliteVdbeChangeP3(v, base+2, pIndex->zName, 0); |
| 659 | sqliteVdbeChangeP3(v, base+9, pIndex->zName, 0); |
| 660 | } |
| 661 | |
| 662 | /* Remove the index structure and free its memory. Except if the |
| 663 | ** EXPLAIN keyword is present, no changes are made. |
| 664 | */ |
| 665 | if( !pParse->explain ){ |
| 666 | if( pIndex->pTable->pIndex==pIndex ){ |
| 667 | pIndex->pTable->pIndex = pIndex->pNext; |
| 668 | }else{ |
| 669 | Index *p; |
| 670 | for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
| 671 | if( p && p->pNext==pIndex ){ |
| 672 | p->pNext = pIndex->pNext; |
| 673 | } |
| 674 | } |
| 675 | sqliteDeleteIndex(pParse->db, pIndex); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | ** Add a new element to the end of an expression list. If pList is |
| 681 | ** initially NULL, then create a new expression list. |
| 682 | */ |
| 683 | ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ |
| 684 | int i; |
| 685 | if( pList==0 ){ |
| 686 | pList = sqliteMalloc( sizeof(ExprList) ); |
| 687 | } |
| 688 | if( pList==0 ) return 0; |
| 689 | if( (pList->nExpr & 7)==0 ){ |
| 690 | int n = pList->nExpr + 8; |
| 691 | pList->a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); |
| 692 | if( pList->a==0 ){ |
| 693 | pList->nExpr = 0; |
| 694 | return pList; |
| 695 | } |
| 696 | } |
| 697 | i = pList->nExpr++; |
| 698 | pList->a[i].pExpr = pExpr; |
| 699 | pList->a[i].zName = 0; |
| 700 | if( pName ){ |
| 701 | sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 702 | sqliteDequote(pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 703 | } |
| 704 | return pList; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | ** Delete an entire expression list. |
| 709 | */ |
| 710 | void sqliteExprListDelete(ExprList *pList){ |
| 711 | int i; |
| 712 | if( pList==0 ) return; |
| 713 | for(i=0; i<pList->nExpr; i++){ |
| 714 | sqliteExprDelete(pList->a[i].pExpr); |
| 715 | sqliteFree(pList->a[i].zName); |
| 716 | } |
| 717 | sqliteFree(pList->a); |
| 718 | sqliteFree(pList); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | ** Append a new element to the given IdList. Create a new IdList if |
| 723 | ** need be. |
| 724 | */ |
| 725 | IdList *sqliteIdListAppend(IdList *pList, Token *pToken){ |
| 726 | if( pList==0 ){ |
| 727 | pList = sqliteMalloc( sizeof(IdList) ); |
| 728 | if( pList==0 ) return 0; |
| 729 | } |
| 730 | if( (pList->nId & 7)==0 ){ |
| 731 | pList->a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) ); |
| 732 | if( pList->a==0 ){ |
| 733 | pList->nId = 0; |
| 734 | return pList; |
| 735 | } |
| 736 | } |
| 737 | memset(&pList->a[pList->nId], 0, sizeof(pList->a[0])); |
| 738 | if( pToken ){ |
| 739 | sqliteSetNString(&pList->a[pList->nId].zName, pToken->z, pToken->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 740 | sqliteDequote(pList->a[pList->nId].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 741 | } |
| 742 | pList->nId++; |
| 743 | return pList; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | ** Add an alias to the last identifier on the given identifier list. |
| 748 | */ |
| 749 | void sqliteIdListAddAlias(IdList *pList, Token *pToken){ |
| 750 | if( pList && pList->nId>0 ){ |
| 751 | int i = pList->nId - 1; |
| 752 | sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 753 | sqliteDequote(pList->a[i].zAlias); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | ** Delete an entire IdList |
| 759 | */ |
| 760 | void sqliteIdListDelete(IdList *pList){ |
| 761 | int i; |
| 762 | if( pList==0 ) return; |
| 763 | for(i=0; i<pList->nId; i++){ |
| 764 | sqliteFree(pList->a[i].zName); |
| 765 | sqliteFree(pList->a[i].zAlias); |
| 766 | } |
| 767 | sqliteFree(pList->a); |
| 768 | sqliteFree(pList); |
| 769 | } |
| 770 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 771 | |
| 772 | /* |
| 773 | ** The COPY command is for compatibility with PostgreSQL and specificially |
| 774 | ** for the ability to read the output of pg_dump. The format is as |
| 775 | ** follows: |
| 776 | ** |
| 777 | ** COPY table FROM file [USING DELIMITERS string] |
| 778 | ** |
| 779 | ** "table" is an existing table name. We will read lines of code from |
| 780 | ** file to fill this table with data. File might be "stdin". The optional |
| 781 | ** delimiter string identifies the field separators. The default is a tab. |
| 782 | */ |
| 783 | void sqliteCopy( |
| 784 | Parse *pParse, /* The parser context */ |
| 785 | Token *pTableName, /* The name of the table into which we will insert */ |
| 786 | Token *pFilename, /* The file from which to obtain information */ |
| 787 | Token *pDelimiter /* Use this as the field delimiter */ |
| 788 | ){ |
| 789 | Table *pTab; |
| 790 | char *zTab; |
| 791 | int i, j; |
| 792 | Vdbe *v; |
| 793 | int addr, end; |
| 794 | Index *pIdx; |
| 795 | |
| 796 | zTab = sqliteTableNameFromToken(pTableName); |
| 797 | pTab = sqliteFindTable(pParse->db, zTab); |
| 798 | sqliteFree(zTab); |
| 799 | if( pTab==0 ){ |
| 800 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 801 | pTableName->z, pTableName->n, 0); |
| 802 | pParse->nErr++; |
| 803 | goto copy_cleanup; |
| 804 | } |
| 805 | if( pTab->readOnly ){ |
| 806 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 807 | " may not be modified", 0); |
| 808 | pParse->nErr++; |
| 809 | goto copy_cleanup; |
| 810 | } |
| 811 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); |
| 812 | if( v ){ |
| 813 | addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0, 0, 0); |
| 814 | sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n); |
drh | b766599 | 2000-05-30 17:30:35 +0000 | [diff] [blame] | 815 | sqliteVdbeDequoteP3(v, addr); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 816 | sqliteVdbeAddOp(v, OP_Open, 0, 1, pTab->zName, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 817 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 818 | sqliteVdbeAddOp(v, OP_Open, i, 1, pIdx->zName, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 819 | } |
| 820 | end = sqliteVdbeMakeLabel(v); |
| 821 | addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end, 0, 0); |
| 822 | if( pDelimiter ){ |
| 823 | sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n); |
| 824 | sqliteVdbeDequoteP3(v, addr); |
| 825 | }else{ |
| 826 | sqliteVdbeChangeP3(v, addr, "\t", 1); |
| 827 | } |
| 828 | sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0); |
| 829 | if( pTab->pIndex ){ |
| 830 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 831 | } |
| 832 | for(i=0; i<pTab->nCol; i++){ |
| 833 | sqliteVdbeAddOp(v, OP_FileField, i, 0, 0, 0); |
| 834 | } |
| 835 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0); |
| 836 | sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0); |
| 837 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 838 | if( pIdx->pNext ){ |
| 839 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 840 | } |
| 841 | for(j=0; j<pIdx->nField; j++){ |
| 842 | sqliteVdbeAddOp(v, OP_FileField, pIdx->aiField[j], 0, 0, 0); |
| 843 | } |
| 844 | sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0); |
| 845 | sqliteVdbeAddOp(v, OP_PutIdx, i, 0, 0, 0); |
| 846 | } |
| 847 | sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0); |
| 848 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, end); |
| 849 | } |
| 850 | |
| 851 | copy_cleanup: |
| 852 | return; |
| 853 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 854 | |
| 855 | /* |
| 856 | ** The non-standard VACUUM command is used to clean up the database, |
| 857 | ** collapse free space, etc. It is modelled after the VACUUM command |
| 858 | ** in PostgreSQL. |
| 859 | */ |
| 860 | void sqliteVacuum(Parse *pParse, Token *pTableName){ |
| 861 | char *zName; |
| 862 | Vdbe *v; |
| 863 | |
| 864 | if( pTableName ){ |
| 865 | zName = sqliteTableNameFromToken(pTableName); |
| 866 | }else{ |
| 867 | zName = 0; |
| 868 | } |
| 869 | if( zName && sqliteFindIndex(pParse->db, zName)==0 |
| 870 | && sqliteFindTable(pParse->db, zName)==0 ){ |
| 871 | sqliteSetString(&pParse->zErrMsg, "no such table or index: ", zName, 0); |
| 872 | pParse->nErr++; |
| 873 | goto vacuum_cleanup; |
| 874 | } |
| 875 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); |
| 876 | if( v==0 ) goto vacuum_cleanup; |
| 877 | if( zName ){ |
| 878 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, zName, 0); |
| 879 | }else{ |
| 880 | int h; |
| 881 | Table *pTab; |
| 882 | Index *pIdx; |
| 883 | for(h=0; h<N_HASH; h++){ |
| 884 | for(pTab=pParse->db->apTblHash[h]; pTab; pTab=pTab->pHash){ |
| 885 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pTab->zName, 0); |
| 886 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 887 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pIdx->zName, 0); |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | vacuum_cleanup: |
| 894 | sqliteFree(zName); |
| 895 | return; |
| 896 | } |