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