1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains C code routines that are called by the SQLite parser 13 ** when syntax rules are reduced. The routines in this file handle the 14 ** following kinds of SQL syntax: 15 ** 16 ** CREATE TABLE 17 ** DROP TABLE 18 ** CREATE INDEX 19 ** DROP INDEX 20 ** creating ID lists 21 ** BEGIN TRANSACTION 22 ** COMMIT 23 ** ROLLBACK 24 */ 25 #include "sqliteInt.h" 26 27 #ifndef SQLITE_OMIT_SHARED_CACHE 28 /* 29 ** The TableLock structure is only used by the sqlite3TableLock() and 30 ** codeTableLocks() functions. 31 */ 32 struct TableLock { 33 int iDb; /* The database containing the table to be locked */ 34 int iTab; /* The root page of the table to be locked */ 35 u8 isWriteLock; /* True for write lock. False for a read lock */ 36 const char *zLockName; /* Name of the table */ 37 }; 38 39 /* 40 ** Record the fact that we want to lock a table at run-time. 41 ** 42 ** The table to be locked has root page iTab and is found in database iDb. 43 ** A read or a write lock can be taken depending on isWritelock. 44 ** 45 ** This routine just records the fact that the lock is desired. The 46 ** code to make the lock occur is generated by a later call to 47 ** codeTableLocks() which occurs during sqlite3FinishCoding(). 48 */ 49 void sqlite3TableLock( 50 Parse *pParse, /* Parsing context */ 51 int iDb, /* Index of the database containing the table to lock */ 52 int iTab, /* Root page number of the table to be locked */ 53 u8 isWriteLock, /* True for a write lock */ 54 const char *zName /* Name of the table to be locked */ 55 ){ 56 Parse *pToplevel = sqlite3ParseToplevel(pParse); 57 int i; 58 int nBytes; 59 TableLock *p; 60 assert( iDb>=0 ); 61 62 if( iDb==1 ) return; 63 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return; 64 for(i=0; i<pToplevel->nTableLock; i++){ 65 p = &pToplevel->aTableLock[i]; 66 if( p->iDb==iDb && p->iTab==iTab ){ 67 p->isWriteLock = (p->isWriteLock || isWriteLock); 68 return; 69 } 70 } 71 72 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); 73 pToplevel->aTableLock = 74 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); 75 if( pToplevel->aTableLock ){ 76 p = &pToplevel->aTableLock[pToplevel->nTableLock++]; 77 p->iDb = iDb; 78 p->iTab = iTab; 79 p->isWriteLock = isWriteLock; 80 p->zLockName = zName; 81 }else{ 82 pToplevel->nTableLock = 0; 83 sqlite3OomFault(pToplevel->db); 84 } 85 } 86 87 /* 88 ** Code an OP_TableLock instruction for each table locked by the 89 ** statement (configured by calls to sqlite3TableLock()). 90 */ 91 static void codeTableLocks(Parse *pParse){ 92 int i; 93 Vdbe *pVdbe; 94 95 pVdbe = sqlite3GetVdbe(pParse); 96 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */ 97 98 for(i=0; i<pParse->nTableLock; i++){ 99 TableLock *p = &pParse->aTableLock[i]; 100 int p1 = p->iDb; 101 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, 102 p->zLockName, P4_STATIC); 103 } 104 } 105 #else 106 #define codeTableLocks(x) 107 #endif 108 109 /* 110 ** Return TRUE if the given yDbMask object is empty - if it contains no 111 ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() 112 ** macros when SQLITE_MAX_ATTACHED is greater than 30. 113 */ 114 #if SQLITE_MAX_ATTACHED>30 115 int sqlite3DbMaskAllZero(yDbMask m){ 116 int i; 117 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0; 118 return 1; 119 } 120 #endif 121 122 /* 123 ** This routine is called after a single SQL statement has been 124 ** parsed and a VDBE program to execute that statement has been 125 ** prepared. This routine puts the finishing touches on the 126 ** VDBE program and resets the pParse structure for the next 127 ** parse. 128 ** 129 ** Note that if an error occurred, it might be the case that 130 ** no VDBE code was generated. 131 */ 132 void sqlite3FinishCoding(Parse *pParse){ 133 sqlite3 *db; 134 Vdbe *v; 135 136 assert( pParse->pToplevel==0 ); 137 db = pParse->db; 138 if( pParse->nested ) return; 139 if( db->mallocFailed || pParse->nErr ){ 140 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR; 141 return; 142 } 143 144 /* Begin by generating some termination code at the end of the 145 ** vdbe program 146 */ 147 v = sqlite3GetVdbe(pParse); 148 assert( !pParse->isMultiWrite 149 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); 150 if( v ){ 151 sqlite3VdbeAddOp0(v, OP_Halt); 152 153 #if SQLITE_USER_AUTHENTICATION 154 if( pParse->nTableLock>0 && db->init.busy==0 ){ 155 sqlite3UserAuthInit(db); 156 if( db->auth.authLevel<UAUTH_User ){ 157 sqlite3ErrorMsg(pParse, "user not authenticated"); 158 pParse->rc = SQLITE_AUTH_USER; 159 return; 160 } 161 } 162 #endif 163 164 /* The cookie mask contains one bit for each database file open. 165 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are 166 ** set for each database that is used. Generate code to start a 167 ** transaction on each used database and to verify the schema cookie 168 ** on each used database. 169 */ 170 if( db->mallocFailed==0 171 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) 172 ){ 173 int iDb, i; 174 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); 175 sqlite3VdbeJumpHere(v, 0); 176 for(iDb=0; iDb<db->nDb; iDb++){ 177 Schema *pSchema; 178 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; 179 sqlite3VdbeUsesBtree(v, iDb); 180 pSchema = db->aDb[iDb].pSchema; 181 sqlite3VdbeAddOp4Int(v, 182 OP_Transaction, /* Opcode */ 183 iDb, /* P1 */ 184 DbMaskTest(pParse->writeMask,iDb), /* P2 */ 185 pSchema->schema_cookie, /* P3 */ 186 pSchema->iGeneration /* P4 */ 187 ); 188 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); 189 VdbeComment((v, 190 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); 191 } 192 #ifndef SQLITE_OMIT_VIRTUALTABLE 193 for(i=0; i<pParse->nVtabLock; i++){ 194 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); 195 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); 196 } 197 pParse->nVtabLock = 0; 198 #endif 199 200 /* Once all the cookies have been verified and transactions opened, 201 ** obtain the required table-locks. This is a no-op unless the 202 ** shared-cache feature is enabled. 203 */ 204 codeTableLocks(pParse); 205 206 /* Initialize any AUTOINCREMENT data structures required. 207 */ 208 sqlite3AutoincrementBegin(pParse); 209 210 /* Code constant expressions that where factored out of inner loops */ 211 if( pParse->pConstExpr ){ 212 ExprList *pEL = pParse->pConstExpr; 213 pParse->okConstFactor = 0; 214 for(i=0; i<pEL->nExpr; i++){ 215 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); 216 } 217 } 218 219 /* Finally, jump back to the beginning of the executable code. */ 220 sqlite3VdbeGoto(v, 1); 221 } 222 } 223 224 225 /* Get the VDBE program ready for execution 226 */ 227 if( v && pParse->nErr==0 && !db->mallocFailed ){ 228 /* A minimum of one cursor is required if autoincrement is used 229 * See ticket [a696379c1f08866] */ 230 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; 231 sqlite3VdbeMakeReady(v, pParse); 232 pParse->rc = SQLITE_DONE; 233 }else{ 234 pParse->rc = SQLITE_ERROR; 235 } 236 } 237 238 /* 239 ** Run the parser and code generator recursively in order to generate 240 ** code for the SQL statement given onto the end of the pParse context 241 ** currently under construction. When the parser is run recursively 242 ** this way, the final OP_Halt is not appended and other initialization 243 ** and finalization steps are omitted because those are handling by the 244 ** outermost parser. 245 ** 246 ** Not everything is nestable. This facility is designed to permit 247 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use 248 ** care if you decide to try to use this routine for some other purposes. 249 */ 250 void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ 251 va_list ap; 252 char *zSql; 253 char *zErrMsg = 0; 254 sqlite3 *db = pParse->db; 255 char saveBuf[PARSE_TAIL_SZ]; 256 257 if( pParse->nErr ) return; 258 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ 259 va_start(ap, zFormat); 260 zSql = sqlite3VMPrintf(db, zFormat, ap); 261 va_end(ap); 262 if( zSql==0 ){ 263 return; /* A malloc must have failed */ 264 } 265 pParse->nested++; 266 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); 267 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); 268 sqlite3RunParser(pParse, zSql, &zErrMsg); 269 sqlite3DbFree(db, zErrMsg); 270 sqlite3DbFree(db, zSql); 271 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); 272 pParse->nested--; 273 } 274 275 #if SQLITE_USER_AUTHENTICATION 276 /* 277 ** Return TRUE if zTable is the name of the system table that stores the 278 ** list of users and their access credentials. 279 */ 280 int sqlite3UserAuthTable(const char *zTable){ 281 return sqlite3_stricmp(zTable, "sqlite_user")==0; 282 } 283 #endif 284 285 /* 286 ** Locate the in-memory structure that describes a particular database 287 ** table given the name of that table and (optionally) the name of the 288 ** database containing the table. Return NULL if not found. 289 ** 290 ** If zDatabase is 0, all databases are searched for the table and the 291 ** first matching table is returned. (No checking for duplicate table 292 ** names is done.) The search order is TEMP first, then MAIN, then any 293 ** auxiliary databases added using the ATTACH command. 294 ** 295 ** See also sqlite3LocateTable(). 296 */ 297 Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ 298 Table *p = 0; 299 int i; 300 301 /* All mutexes are required for schema access. Make sure we hold them. */ 302 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); 303 #if SQLITE_USER_AUTHENTICATION 304 /* Only the admin user is allowed to know that the sqlite_user table 305 ** exists */ 306 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ 307 return 0; 308 } 309 #endif 310 while(1){ 311 for(i=OMIT_TEMPDB; i<db->nDb; i++){ 312 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ 313 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){ 314 assert( sqlite3SchemaMutexHeld(db, j, 0) ); 315 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName); 316 if( p ) return p; 317 } 318 } 319 /* Not found. If the name we were looking for was temp.sqlite_master 320 ** then change the name to sqlite_temp_master and try again. */ 321 if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break; 322 if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break; 323 zName = TEMP_MASTER_NAME; 324 } 325 return 0; 326 } 327 328 /* 329 ** Locate the in-memory structure that describes a particular database 330 ** table given the name of that table and (optionally) the name of the 331 ** database containing the table. Return NULL if not found. Also leave an 332 ** error message in pParse->zErrMsg. 333 ** 334 ** The difference between this routine and sqlite3FindTable() is that this 335 ** routine leaves an error message in pParse->zErrMsg where 336 ** sqlite3FindTable() does not. 337 */ 338 Table *sqlite3LocateTable( 339 Parse *pParse, /* context in which to report errors */ 340 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */ 341 const char *zName, /* Name of the table we are looking for */ 342 const char *zDbase /* Name of the database. Might be NULL */ 343 ){ 344 Table *p; 345 sqlite3 *db = pParse->db; 346 347 /* Read the database schema. If an error occurs, leave an error message 348 ** and code in pParse and return NULL. */ 349 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 350 && SQLITE_OK!=sqlite3ReadSchema(pParse) 351 ){ 352 return 0; 353 } 354 355 p = sqlite3FindTable(db, zName, zDbase); 356 if( p==0 ){ 357 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; 358 #ifndef SQLITE_OMIT_VIRTUALTABLE 359 /* If zName is the not the name of a table in the schema created using 360 ** CREATE, then check to see if it is the name of an virtual table that 361 ** can be an eponymous virtual table. */ 362 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); 363 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ 364 pMod = sqlite3PragmaVtabRegister(db, zName); 365 } 366 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ 367 return pMod->pEpoTab; 368 } 369 #endif 370 if( (flags & LOCATE_NOERR)==0 ){ 371 if( zDbase ){ 372 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); 373 }else{ 374 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); 375 } 376 pParse->checkSchema = 1; 377 } 378 } 379 380 return p; 381 } 382 383 /* 384 ** Locate the table identified by *p. 385 ** 386 ** This is a wrapper around sqlite3LocateTable(). The difference between 387 ** sqlite3LocateTable() and this function is that this function restricts 388 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be 389 ** non-NULL if it is part of a view or trigger program definition. See 390 ** sqlite3FixSrcList() for details. 391 */ 392 Table *sqlite3LocateTableItem( 393 Parse *pParse, 394 u32 flags, 395 struct SrcList_item *p 396 ){ 397 const char *zDb; 398 assert( p->pSchema==0 || p->zDatabase==0 ); 399 if( p->pSchema ){ 400 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); 401 zDb = pParse->db->aDb[iDb].zDbSName; 402 }else{ 403 zDb = p->zDatabase; 404 } 405 return sqlite3LocateTable(pParse, flags, p->zName, zDb); 406 } 407 408 /* 409 ** Locate the in-memory structure that describes 410 ** a particular index given the name of that index 411 ** and the name of the database that contains the index. 412 ** Return NULL if not found. 413 ** 414 ** If zDatabase is 0, all databases are searched for the 415 ** table and the first matching index is returned. (No checking 416 ** for duplicate index names is done.) The search order is 417 ** TEMP first, then MAIN, then any auxiliary databases added 418 ** using the ATTACH command. 419 */ 420 Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ 421 Index *p = 0; 422 int i; 423 /* All mutexes are required for schema access. Make sure we hold them. */ 424 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); 425 for(i=OMIT_TEMPDB; i<db->nDb; i++){ 426 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ 427 Schema *pSchema = db->aDb[j].pSchema; 428 assert( pSchema ); 429 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue; 430 assert( sqlite3SchemaMutexHeld(db, j, 0) ); 431 p = sqlite3HashFind(&pSchema->idxHash, zName); 432 if( p ) break; 433 } 434 return p; 435 } 436 437 /* 438 ** Reclaim the memory used by an index 439 */ 440 void sqlite3FreeIndex(sqlite3 *db, Index *p){ 441 #ifndef SQLITE_OMIT_ANALYZE 442 sqlite3DeleteIndexSamples(db, p); 443 #endif 444 sqlite3ExprDelete(db, p->pPartIdxWhere); 445 sqlite3ExprListDelete(db, p->aColExpr); 446 sqlite3DbFree(db, p->zColAff); 447 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); 448 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 449 sqlite3_free(p->aiRowEst); 450 #endif 451 sqlite3DbFree(db, p); 452 } 453 454 /* 455 ** For the index called zIdxName which is found in the database iDb, 456 ** unlike that index from its Table then remove the index from 457 ** the index hash table and free all memory structures associated 458 ** with the index. 459 */ 460 void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ 461 Index *pIndex; 462 Hash *pHash; 463 464 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 465 pHash = &db->aDb[iDb].pSchema->idxHash; 466 pIndex = sqlite3HashInsert(pHash, zIdxName, 0); 467 if( ALWAYS(pIndex) ){ 468 if( pIndex->pTable->pIndex==pIndex ){ 469 pIndex->pTable->pIndex = pIndex->pNext; 470 }else{ 471 Index *p; 472 /* Justification of ALWAYS(); The index must be on the list of 473 ** indices. */ 474 p = pIndex->pTable->pIndex; 475 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } 476 if( ALWAYS(p && p->pNext==pIndex) ){ 477 p->pNext = pIndex->pNext; 478 } 479 } 480 sqlite3FreeIndex(db, pIndex); 481 } 482 db->mDbFlags |= DBFLAG_SchemaChange; 483 } 484 485 /* 486 ** Look through the list of open database files in db->aDb[] and if 487 ** any have been closed, remove them from the list. Reallocate the 488 ** db->aDb[] structure to a smaller size, if possible. 489 ** 490 ** Entry 0 (the "main" database) and entry 1 (the "temp" database) 491 ** are never candidates for being collapsed. 492 */ 493 void sqlite3CollapseDatabaseArray(sqlite3 *db){ 494 int i, j; 495 for(i=j=2; i<db->nDb; i++){ 496 struct Db *pDb = &db->aDb[i]; 497 if( pDb->pBt==0 ){ 498 sqlite3DbFree(db, pDb->zDbSName); 499 pDb->zDbSName = 0; 500 continue; 501 } 502 if( j<i ){ 503 db->aDb[j] = db->aDb[i]; 504 } 505 j++; 506 } 507 db->nDb = j; 508 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ 509 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); 510 sqlite3DbFree(db, db->aDb); 511 db->aDb = db->aDbStatic; 512 } 513 } 514 515 /* 516 ** Reset the schema for the database at index iDb. Also reset the 517 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero. 518 ** Deferred resets may be run by calling with iDb<0. 519 */ 520 void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ 521 int i; 522 assert( iDb<db->nDb ); 523 524 if( iDb>=0 ){ 525 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 526 DbSetProperty(db, iDb, DB_ResetWanted); 527 DbSetProperty(db, 1, DB_ResetWanted); 528 db->mDbFlags &= ~DBFLAG_SchemaKnownOk; 529 } 530 531 if( db->nSchemaLock==0 ){ 532 for(i=0; i<db->nDb; i++){ 533 if( DbHasProperty(db, i, DB_ResetWanted) ){ 534 sqlite3SchemaClear(db->aDb[i].pSchema); 535 } 536 } 537 } 538 } 539 540 /* 541 ** Erase all schema information from all attached databases (including 542 ** "main" and "temp") for a single database connection. 543 */ 544 void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ 545 int i; 546 sqlite3BtreeEnterAll(db); 547 assert( db->nSchemaLock==0 ); 548 for(i=0; i<db->nDb; i++){ 549 Db *pDb = &db->aDb[i]; 550 if( pDb->pSchema ){ 551 sqlite3SchemaClear(pDb->pSchema); 552 } 553 } 554 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk); 555 sqlite3VtabUnlockList(db); 556 sqlite3BtreeLeaveAll(db); 557 sqlite3CollapseDatabaseArray(db); 558 } 559 560 /* 561 ** This routine is called when a commit occurs. 562 */ 563 void sqlite3CommitInternalChanges(sqlite3 *db){ 564 db->mDbFlags &= ~DBFLAG_SchemaChange; 565 } 566 567 /* 568 ** Delete memory allocated for the column names of a table or view (the 569 ** Table.aCol[] array). 570 */ 571 void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ 572 int i; 573 Column *pCol; 574 assert( pTable!=0 ); 575 if( (pCol = pTable->aCol)!=0 ){ 576 for(i=0; i<pTable->nCol; i++, pCol++){ 577 sqlite3DbFree(db, pCol->zName); 578 sqlite3ExprDelete(db, pCol->pDflt); 579 sqlite3DbFree(db, pCol->zColl); 580 } 581 sqlite3DbFree(db, pTable->aCol); 582 } 583 } 584 585 /* 586 ** Remove the memory data structures associated with the given 587 ** Table. No changes are made to disk by this routine. 588 ** 589 ** This routine just deletes the data structure. It does not unlink 590 ** the table data structure from the hash table. But it does destroy 591 ** memory structures of the indices and foreign keys associated with 592 ** the table. 593 ** 594 ** The db parameter is optional. It is needed if the Table object 595 ** contains lookaside memory. (Table objects in the schema do not use 596 ** lookaside memory, but some ephemeral Table objects do.) Or the 597 ** db parameter can be used with db->pnBytesFreed to measure the memory 598 ** used by the Table object. 599 */ 600 static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ 601 Index *pIndex, *pNext; 602 603 #ifdef SQLITE_DEBUG 604 /* Record the number of outstanding lookaside allocations in schema Tables 605 ** prior to doing any free() operations. Since schema Tables do not use 606 ** lookaside, this number should not change. */ 607 int nLookaside = 0; 608 if( db && (pTable->tabFlags & TF_Ephemeral)==0 ){ 609 nLookaside = sqlite3LookasideUsed(db, 0); 610 } 611 #endif 612 613 /* Delete all indices associated with this table. */ 614 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ 615 pNext = pIndex->pNext; 616 assert( pIndex->pSchema==pTable->pSchema 617 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); 618 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){ 619 char *zName = pIndex->zName; 620 TESTONLY ( Index *pOld = ) sqlite3HashInsert( 621 &pIndex->pSchema->idxHash, zName, 0 622 ); 623 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); 624 assert( pOld==pIndex || pOld==0 ); 625 } 626 sqlite3FreeIndex(db, pIndex); 627 } 628 629 /* Delete any foreign keys attached to this table. */ 630 sqlite3FkDelete(db, pTable); 631 632 /* Delete the Table structure itself. 633 */ 634 #ifdef SQLITE_ENABLE_NORMALIZE 635 if( pTable->pColHash ){ 636 sqlite3HashClear(pTable->pColHash); 637 sqlite3_free(pTable->pColHash); 638 } 639 #endif 640 sqlite3DeleteColumnNames(db, pTable); 641 sqlite3DbFree(db, pTable->zName); 642 sqlite3DbFree(db, pTable->zColAff); 643 sqlite3SelectDelete(db, pTable->pSelect); 644 sqlite3ExprListDelete(db, pTable->pCheck); 645 #ifndef SQLITE_OMIT_VIRTUALTABLE 646 sqlite3VtabClear(db, pTable); 647 #endif 648 sqlite3DbFree(db, pTable); 649 650 /* Verify that no lookaside memory was used by schema tables */ 651 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) ); 652 } 653 void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ 654 /* Do not delete the table until the reference count reaches zero. */ 655 if( !pTable ) return; 656 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return; 657 deleteTable(db, pTable); 658 } 659 660 661 /* 662 ** Unlink the given table from the hash tables and the delete the 663 ** table structure with all its indices and foreign keys. 664 */ 665 void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ 666 Table *p; 667 Db *pDb; 668 669 assert( db!=0 ); 670 assert( iDb>=0 && iDb<db->nDb ); 671 assert( zTabName ); 672 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 673 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ 674 pDb = &db->aDb[iDb]; 675 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0); 676 sqlite3DeleteTable(db, p); 677 db->mDbFlags |= DBFLAG_SchemaChange; 678 } 679 680 /* 681 ** Given a token, return a string that consists of the text of that 682 ** token. Space to hold the returned string 683 ** is obtained from sqliteMalloc() and must be freed by the calling 684 ** function. 685 ** 686 ** Any quotation marks (ex: "name", 'name', [name], or `name`) that 687 ** surround the body of the token are removed. 688 ** 689 ** Tokens are often just pointers into the original SQL text and so 690 ** are not \000 terminated and are not persistent. The returned string 691 ** is \000 terminated and is persistent. 692 */ 693 char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ 694 char *zName; 695 if( pName ){ 696 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n); 697 sqlite3Dequote(zName); 698 }else{ 699 zName = 0; 700 } 701 return zName; 702 } 703 704 /* 705 ** Open the sqlite_master table stored in database number iDb for 706 ** writing. The table is opened using cursor 0. 707 */ 708 void sqlite3OpenMasterTable(Parse *p, int iDb){ 709 Vdbe *v = sqlite3GetVdbe(p); 710 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME); 711 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5); 712 if( p->nTab==0 ){ 713 p->nTab = 1; 714 } 715 } 716 717 /* 718 ** Parameter zName points to a nul-terminated buffer containing the name 719 ** of a database ("main", "temp" or the name of an attached db). This 720 ** function returns the index of the named database in db->aDb[], or 721 ** -1 if the named db cannot be found. 722 */ 723 int sqlite3FindDbName(sqlite3 *db, const char *zName){ 724 int i = -1; /* Database number */ 725 if( zName ){ 726 Db *pDb; 727 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ 728 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break; 729 /* "main" is always an acceptable alias for the primary database 730 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ 731 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break; 732 } 733 } 734 return i; 735 } 736 737 /* 738 ** The token *pName contains the name of a database (either "main" or 739 ** "temp" or the name of an attached db). This routine returns the 740 ** index of the named database in db->aDb[], or -1 if the named db 741 ** does not exist. 742 */ 743 int sqlite3FindDb(sqlite3 *db, Token *pName){ 744 int i; /* Database number */ 745 char *zName; /* Name we are searching for */ 746 zName = sqlite3NameFromToken(db, pName); 747 i = sqlite3FindDbName(db, zName); 748 sqlite3DbFree(db, zName); 749 return i; 750 } 751 752 /* The table or view or trigger name is passed to this routine via tokens 753 ** pName1 and pName2. If the table name was fully qualified, for example: 754 ** 755 ** CREATE TABLE xxx.yyy (...); 756 ** 757 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if 758 ** the table name is not fully qualified, i.e.: 759 ** 760 ** CREATE TABLE yyy(...); 761 ** 762 ** Then pName1 is set to "yyy" and pName2 is "". 763 ** 764 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or 765 ** pName2) that stores the unqualified table name. The index of the 766 ** database "xxx" is returned. 767 */ 768 int sqlite3TwoPartName( 769 Parse *pParse, /* Parsing and code generating context */ 770 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ 771 Token *pName2, /* The "yyy" in the name "xxx.yyy" */ 772 Token **pUnqual /* Write the unqualified object name here */ 773 ){ 774 int iDb; /* Database holding the object */ 775 sqlite3 *db = pParse->db; 776 777 assert( pName2!=0 ); 778 if( pName2->n>0 ){ 779 if( db->init.busy ) { 780 sqlite3ErrorMsg(pParse, "corrupt database"); 781 return -1; 782 } 783 *pUnqual = pName2; 784 iDb = sqlite3FindDb(db, pName1); 785 if( iDb<0 ){ 786 sqlite3ErrorMsg(pParse, "unknown database %T", pName1); 787 return -1; 788 } 789 }else{ 790 assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT 791 || (db->mDbFlags & DBFLAG_Vacuum)!=0); 792 iDb = db->init.iDb; 793 *pUnqual = pName1; 794 } 795 return iDb; 796 } 797 798 /* 799 ** True if PRAGMA writable_schema is ON 800 */ 801 int sqlite3WritableSchema(sqlite3 *db){ 802 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 ); 803 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== 804 SQLITE_WriteSchema ); 805 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== 806 SQLITE_Defensive ); 807 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== 808 (SQLITE_WriteSchema|SQLITE_Defensive) ); 809 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema; 810 } 811 812 /* 813 ** This routine is used to check if the UTF-8 string zName is a legal 814 ** unqualified name for a new schema object (table, index, view or 815 ** trigger). All names are legal except those that begin with the string 816 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace 817 ** is reserved for internal use. 818 */ 819 int sqlite3CheckObjectName(Parse *pParse, const char *zName){ 820 if( !pParse->db->init.busy && pParse->nested==0 821 && sqlite3WritableSchema(pParse->db)==0 822 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ 823 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName); 824 return SQLITE_ERROR; 825 } 826 return SQLITE_OK; 827 } 828 829 /* 830 ** Return the PRIMARY KEY index of a table 831 */ 832 Index *sqlite3PrimaryKeyIndex(Table *pTab){ 833 Index *p; 834 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){} 835 return p; 836 } 837 838 /* 839 ** Return the column of index pIdx that corresponds to table 840 ** column iCol. Return -1 if not found. 841 */ 842 i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){ 843 int i; 844 for(i=0; i<pIdx->nColumn; i++){ 845 if( iCol==pIdx->aiColumn[i] ) return i; 846 } 847 return -1; 848 } 849 850 /* 851 ** Begin constructing a new table representation in memory. This is 852 ** the first of several action routines that get called in response 853 ** to a CREATE TABLE statement. In particular, this routine is called 854 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp 855 ** flag is true if the table should be stored in the auxiliary database 856 ** file instead of in the main database file. This is normally the case 857 ** when the "TEMP" or "TEMPORARY" keyword occurs in between 858 ** CREATE and TABLE. 859 ** 860 ** The new table record is initialized and put in pParse->pNewTable. 861 ** As more of the CREATE TABLE statement is parsed, additional action 862 ** routines will be called to add more information to this record. 863 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine 864 ** is called to complete the construction of the new table record. 865 */ 866 void sqlite3StartTable( 867 Parse *pParse, /* Parser context */ 868 Token *pName1, /* First part of the name of the table or view */ 869 Token *pName2, /* Second part of the name of the table or view */ 870 int isTemp, /* True if this is a TEMP table */ 871 int isView, /* True if this is a VIEW */ 872 int isVirtual, /* True if this is a VIRTUAL table */ 873 int noErr /* Do nothing if table already exists */ 874 ){ 875 Table *pTable; 876 char *zName = 0; /* The name of the new table */ 877 sqlite3 *db = pParse->db; 878 Vdbe *v; 879 int iDb; /* Database number to create the table in */ 880 Token *pName; /* Unqualified name of the table to create */ 881 882 if( db->init.busy && db->init.newTnum==1 ){ 883 /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */ 884 iDb = db->init.iDb; 885 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); 886 pName = pName1; 887 }else{ 888 /* The common case */ 889 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); 890 if( iDb<0 ) return; 891 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ 892 /* If creating a temp table, the name may not be qualified. Unless 893 ** the database name is "temp" anyway. */ 894 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); 895 return; 896 } 897 if( !OMIT_TEMPDB && isTemp ) iDb = 1; 898 zName = sqlite3NameFromToken(db, pName); 899 if( IN_RENAME_OBJECT ){ 900 sqlite3RenameTokenMap(pParse, (void*)zName, pName); 901 } 902 } 903 pParse->sNameToken = *pName; 904 if( zName==0 ) return; 905 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ 906 goto begin_table_error; 907 } 908 if( db->init.iDb==1 ) isTemp = 1; 909 #ifndef SQLITE_OMIT_AUTHORIZATION 910 assert( isTemp==0 || isTemp==1 ); 911 assert( isView==0 || isView==1 ); 912 { 913 static const u8 aCode[] = { 914 SQLITE_CREATE_TABLE, 915 SQLITE_CREATE_TEMP_TABLE, 916 SQLITE_CREATE_VIEW, 917 SQLITE_CREATE_TEMP_VIEW 918 }; 919 char *zDb = db->aDb[iDb].zDbSName; 920 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ 921 goto begin_table_error; 922 } 923 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView], 924 zName, 0, zDb) ){ 925 goto begin_table_error; 926 } 927 } 928 #endif 929 930 /* Make sure the new table name does not collide with an existing 931 ** index or table name in the same database. Issue an error message if 932 ** it does. The exception is if the statement being parsed was passed 933 ** to an sqlite3_declare_vtab() call. In that case only the column names 934 ** and types will be used, so there is no need to test for namespace 935 ** collisions. 936 */ 937 if( !IN_SPECIAL_PARSE ){ 938 char *zDb = db->aDb[iDb].zDbSName; 939 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 940 goto begin_table_error; 941 } 942 pTable = sqlite3FindTable(db, zName, zDb); 943 if( pTable ){ 944 if( !noErr ){ 945 sqlite3ErrorMsg(pParse, "table %T already exists", pName); 946 }else{ 947 assert( !db->init.busy || CORRUPT_DB ); 948 sqlite3CodeVerifySchema(pParse, iDb); 949 } 950 goto begin_table_error; 951 } 952 if( sqlite3FindIndex(db, zName, zDb)!=0 ){ 953 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); 954 goto begin_table_error; 955 } 956 } 957 958 pTable = sqlite3DbMallocZero(db, sizeof(Table)); 959 if( pTable==0 ){ 960 assert( db->mallocFailed ); 961 pParse->rc = SQLITE_NOMEM_BKPT; 962 pParse->nErr++; 963 goto begin_table_error; 964 } 965 pTable->zName = zName; 966 pTable->iPKey = -1; 967 pTable->pSchema = db->aDb[iDb].pSchema; 968 pTable->nTabRef = 1; 969 #ifdef SQLITE_DEFAULT_ROWEST 970 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST); 971 #else 972 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); 973 #endif 974 assert( pParse->pNewTable==0 ); 975 pParse->pNewTable = pTable; 976 977 /* If this is the magic sqlite_sequence table used by autoincrement, 978 ** then record a pointer to this table in the main database structure 979 ** so that INSERT can find the table easily. 980 */ 981 #ifndef SQLITE_OMIT_AUTOINCREMENT 982 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ 983 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 984 pTable->pSchema->pSeqTab = pTable; 985 } 986 #endif 987 988 /* Begin generating the code that will insert the table record into 989 ** the SQLITE_MASTER table. Note in particular that we must go ahead 990 ** and allocate the record number for the table entry now. Before any 991 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause 992 ** indices to be created and the table record must come before the 993 ** indices. Hence, the record number for the table must be allocated 994 ** now. 995 */ 996 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ 997 int addr1; 998 int fileFormat; 999 int reg1, reg2, reg3; 1000 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ 1001 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 }; 1002 sqlite3BeginWriteOperation(pParse, 1, iDb); 1003 1004 #ifndef SQLITE_OMIT_VIRTUALTABLE 1005 if( isVirtual ){ 1006 sqlite3VdbeAddOp0(v, OP_VBegin); 1007 } 1008 #endif 1009 1010 /* If the file format and encoding in the database have not been set, 1011 ** set them now. 1012 */ 1013 reg1 = pParse->regRowid = ++pParse->nMem; 1014 reg2 = pParse->regRoot = ++pParse->nMem; 1015 reg3 = ++pParse->nMem; 1016 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); 1017 sqlite3VdbeUsesBtree(v, iDb); 1018 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); 1019 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? 1020 1 : SQLITE_MAX_FILE_FORMAT; 1021 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat); 1022 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); 1023 sqlite3VdbeJumpHere(v, addr1); 1024 1025 /* This just creates a place-holder record in the sqlite_master table. 1026 ** The record created does not contain anything yet. It will be replaced 1027 ** by the real entry in code generated at sqlite3EndTable(). 1028 ** 1029 ** The rowid for the new entry is left in register pParse->regRowid. 1030 ** The root page number of the new table is left in reg pParse->regRoot. 1031 ** The rowid and root page number values are needed by the code that 1032 ** sqlite3EndTable will generate. 1033 */ 1034 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) 1035 if( isView || isVirtual ){ 1036 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); 1037 }else 1038 #endif 1039 { 1040 pParse->addrCrTab = 1041 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY); 1042 } 1043 sqlite3OpenMasterTable(pParse, iDb); 1044 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); 1045 sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); 1046 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); 1047 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); 1048 sqlite3VdbeAddOp0(v, OP_Close); 1049 } 1050 1051 /* Normal (non-error) return. */ 1052 return; 1053 1054 /* If an error occurs, we jump here */ 1055 begin_table_error: 1056 sqlite3DbFree(db, zName); 1057 return; 1058 } 1059 1060 /* Set properties of a table column based on the (magical) 1061 ** name of the column. 1062 */ 1063 #if SQLITE_ENABLE_HIDDEN_COLUMNS 1064 void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ 1065 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){ 1066 pCol->colFlags |= COLFLAG_HIDDEN; 1067 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ 1068 pTab->tabFlags |= TF_OOOHidden; 1069 } 1070 } 1071 #endif 1072 1073 1074 /* 1075 ** Add a new column to the table currently being constructed. 1076 ** 1077 ** The parser calls this routine once for each column declaration 1078 ** in a CREATE TABLE statement. sqlite3StartTable() gets called 1079 ** first to get things going. Then this routine is called for each 1080 ** column. 1081 */ 1082 void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){ 1083 Table *p; 1084 int i; 1085 char *z; 1086 char *zType; 1087 Column *pCol; 1088 sqlite3 *db = pParse->db; 1089 if( (p = pParse->pNewTable)==0 ) return; 1090 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ 1091 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); 1092 return; 1093 } 1094 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2); 1095 if( z==0 ) return; 1096 if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName); 1097 memcpy(z, pName->z, pName->n); 1098 z[pName->n] = 0; 1099 sqlite3Dequote(z); 1100 for(i=0; i<p->nCol; i++){ 1101 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){ 1102 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); 1103 sqlite3DbFree(db, z); 1104 return; 1105 } 1106 } 1107 if( (p->nCol & 0x7)==0 ){ 1108 Column *aNew; 1109 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); 1110 if( aNew==0 ){ 1111 sqlite3DbFree(db, z); 1112 return; 1113 } 1114 p->aCol = aNew; 1115 } 1116 pCol = &p->aCol[p->nCol]; 1117 memset(pCol, 0, sizeof(p->aCol[0])); 1118 pCol->zName = z; 1119 sqlite3ColumnPropertiesFromName(p, pCol); 1120 1121 if( pType->n==0 ){ 1122 /* If there is no type specified, columns have the default affinity 1123 ** 'BLOB' with a default size of 4 bytes. */ 1124 pCol->affinity = SQLITE_AFF_BLOB; 1125 pCol->szEst = 1; 1126 #ifdef SQLITE_ENABLE_SORTER_REFERENCES 1127 if( 4>=sqlite3GlobalConfig.szSorterRef ){ 1128 pCol->colFlags |= COLFLAG_SORTERREF; 1129 } 1130 #endif 1131 }else{ 1132 zType = z + sqlite3Strlen30(z) + 1; 1133 memcpy(zType, pType->z, pType->n); 1134 zType[pType->n] = 0; 1135 sqlite3Dequote(zType); 1136 pCol->affinity = sqlite3AffinityType(zType, pCol); 1137 pCol->colFlags |= COLFLAG_HASTYPE; 1138 } 1139 p->nCol++; 1140 pParse->constraintName.n = 0; 1141 } 1142 1143 /* 1144 ** This routine is called by the parser while in the middle of 1145 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has 1146 ** been seen on a column. This routine sets the notNull flag on 1147 ** the column currently under construction. 1148 */ 1149 void sqlite3AddNotNull(Parse *pParse, int onError){ 1150 Table *p; 1151 Column *pCol; 1152 p = pParse->pNewTable; 1153 if( p==0 || NEVER(p->nCol<1) ) return; 1154 pCol = &p->aCol[p->nCol-1]; 1155 pCol->notNull = (u8)onError; 1156 p->tabFlags |= TF_HasNotNull; 1157 1158 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created 1159 ** on this column. */ 1160 if( pCol->colFlags & COLFLAG_UNIQUE ){ 1161 Index *pIdx; 1162 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ 1163 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None ); 1164 if( pIdx->aiColumn[0]==p->nCol-1 ){ 1165 pIdx->uniqNotNull = 1; 1166 } 1167 } 1168 } 1169 } 1170 1171 /* 1172 ** Scan the column type name zType (length nType) and return the 1173 ** associated affinity type. 1174 ** 1175 ** This routine does a case-independent search of zType for the 1176 ** substrings in the following table. If one of the substrings is 1177 ** found, the corresponding affinity is returned. If zType contains 1178 ** more than one of the substrings, entries toward the top of 1179 ** the table take priority. For example, if zType is 'BLOBINT', 1180 ** SQLITE_AFF_INTEGER is returned. 1181 ** 1182 ** Substring | Affinity 1183 ** -------------------------------- 1184 ** 'INT' | SQLITE_AFF_INTEGER 1185 ** 'CHAR' | SQLITE_AFF_TEXT 1186 ** 'CLOB' | SQLITE_AFF_TEXT 1187 ** 'TEXT' | SQLITE_AFF_TEXT 1188 ** 'BLOB' | SQLITE_AFF_BLOB 1189 ** 'REAL' | SQLITE_AFF_REAL 1190 ** 'FLOA' | SQLITE_AFF_REAL 1191 ** 'DOUB' | SQLITE_AFF_REAL 1192 ** 1193 ** If none of the substrings in the above table are found, 1194 ** SQLITE_AFF_NUMERIC is returned. 1195 */ 1196 char sqlite3AffinityType(const char *zIn, Column *pCol){ 1197 u32 h = 0; 1198 char aff = SQLITE_AFF_NUMERIC; 1199 const char *zChar = 0; 1200 1201 assert( zIn!=0 ); 1202 while( zIn[0] ){ 1203 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; 1204 zIn++; 1205 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ 1206 aff = SQLITE_AFF_TEXT; 1207 zChar = zIn; 1208 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ 1209 aff = SQLITE_AFF_TEXT; 1210 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ 1211 aff = SQLITE_AFF_TEXT; 1212 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ 1213 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ 1214 aff = SQLITE_AFF_BLOB; 1215 if( zIn[0]=='(' ) zChar = zIn; 1216 #ifndef SQLITE_OMIT_FLOATING_POINT 1217 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ 1218 && aff==SQLITE_AFF_NUMERIC ){ 1219 aff = SQLITE_AFF_REAL; 1220 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ 1221 && aff==SQLITE_AFF_NUMERIC ){ 1222 aff = SQLITE_AFF_REAL; 1223 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ 1224 && aff==SQLITE_AFF_NUMERIC ){ 1225 aff = SQLITE_AFF_REAL; 1226 #endif 1227 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ 1228 aff = SQLITE_AFF_INTEGER; 1229 break; 1230 } 1231 } 1232 1233 /* If pCol is not NULL, store an estimate of the field size. The 1234 ** estimate is scaled so that the size of an integer is 1. */ 1235 if( pCol ){ 1236 int v = 0; /* default size is approx 4 bytes */ 1237 if( aff<SQLITE_AFF_NUMERIC ){ 1238 if( zChar ){ 1239 while( zChar[0] ){ 1240 if( sqlite3Isdigit(zChar[0]) ){ 1241 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */ 1242 sqlite3GetInt32(zChar, &v); 1243 break; 1244 } 1245 zChar++; 1246 } 1247 }else{ 1248 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ 1249 } 1250 } 1251 #ifdef SQLITE_ENABLE_SORTER_REFERENCES 1252 if( v>=sqlite3GlobalConfig.szSorterRef ){ 1253 pCol->colFlags |= COLFLAG_SORTERREF; 1254 } 1255 #endif 1256 v = v/4 + 1; 1257 if( v>255 ) v = 255; 1258 pCol->szEst = v; 1259 } 1260 return aff; 1261 } 1262 1263 /* 1264 ** The expression is the default value for the most recently added column 1265 ** of the table currently under construction. 1266 ** 1267 ** Default value expressions must be constant. Raise an exception if this 1268 ** is not the case. 1269 ** 1270 ** This routine is called by the parser while in the middle of 1271 ** parsing a CREATE TABLE statement. 1272 */ 1273 void sqlite3AddDefaultValue( 1274 Parse *pParse, /* Parsing context */ 1275 Expr *pExpr, /* The parsed expression of the default value */ 1276 const char *zStart, /* Start of the default value text */ 1277 const char *zEnd /* First character past end of defaut value text */ 1278 ){ 1279 Table *p; 1280 Column *pCol; 1281 sqlite3 *db = pParse->db; 1282 p = pParse->pNewTable; 1283 if( p!=0 ){ 1284 pCol = &(p->aCol[p->nCol-1]); 1285 if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){ 1286 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", 1287 pCol->zName); 1288 }else{ 1289 /* A copy of pExpr is used instead of the original, as pExpr contains 1290 ** tokens that point to volatile memory. 1291 */ 1292 Expr x; 1293 sqlite3ExprDelete(db, pCol->pDflt); 1294 memset(&x, 0, sizeof(x)); 1295 x.op = TK_SPAN; 1296 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd); 1297 x.pLeft = pExpr; 1298 x.flags = EP_Skip; 1299 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); 1300 sqlite3DbFree(db, x.u.zToken); 1301 } 1302 } 1303 if( IN_RENAME_OBJECT ){ 1304 sqlite3RenameExprUnmap(pParse, pExpr); 1305 } 1306 sqlite3ExprDelete(db, pExpr); 1307 } 1308 1309 /* 1310 ** Backwards Compatibility Hack: 1311 ** 1312 ** Historical versions of SQLite accepted strings as column names in 1313 ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: 1314 ** 1315 ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) 1316 ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); 1317 ** 1318 ** This is goofy. But to preserve backwards compatibility we continue to 1319 ** accept it. This routine does the necessary conversion. It converts 1320 ** the expression given in its argument from a TK_STRING into a TK_ID 1321 ** if the expression is just a TK_STRING with an optional COLLATE clause. 1322 ** If the epxression is anything other than TK_STRING, the expression is 1323 ** unchanged. 1324 */ 1325 static void sqlite3StringToId(Expr *p){ 1326 if( p->op==TK_STRING ){ 1327 p->op = TK_ID; 1328 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){ 1329 p->pLeft->op = TK_ID; 1330 } 1331 } 1332 1333 /* 1334 ** Designate the PRIMARY KEY for the table. pList is a list of names 1335 ** of columns that form the primary key. If pList is NULL, then the 1336 ** most recently added column of the table is the primary key. 1337 ** 1338 ** A table can have at most one primary key. If the table already has 1339 ** a primary key (and this is the second primary key) then create an 1340 ** error. 1341 ** 1342 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, 1343 ** then we will try to use that column as the rowid. Set the Table.iPKey 1344 ** field of the table under construction to be the index of the 1345 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is 1346 ** no INTEGER PRIMARY KEY. 1347 ** 1348 ** If the key is not an INTEGER PRIMARY KEY, then create a unique 1349 ** index for the key. No index is created for INTEGER PRIMARY KEYs. 1350 */ 1351 void sqlite3AddPrimaryKey( 1352 Parse *pParse, /* Parsing context */ 1353 ExprList *pList, /* List of field names to be indexed */ 1354 int onError, /* What to do with a uniqueness conflict */ 1355 int autoInc, /* True if the AUTOINCREMENT keyword is present */ 1356 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ 1357 ){ 1358 Table *pTab = pParse->pNewTable; 1359 Column *pCol = 0; 1360 int iCol = -1, i; 1361 int nTerm; 1362 if( pTab==0 ) goto primary_key_exit; 1363 if( pTab->tabFlags & TF_HasPrimaryKey ){ 1364 sqlite3ErrorMsg(pParse, 1365 "table \"%s\" has more than one primary key", pTab->zName); 1366 goto primary_key_exit; 1367 } 1368 pTab->tabFlags |= TF_HasPrimaryKey; 1369 if( pList==0 ){ 1370 iCol = pTab->nCol - 1; 1371 pCol = &pTab->aCol[iCol]; 1372 pCol->colFlags |= COLFLAG_PRIMKEY; 1373 nTerm = 1; 1374 }else{ 1375 nTerm = pList->nExpr; 1376 for(i=0; i<nTerm; i++){ 1377 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); 1378 assert( pCExpr!=0 ); 1379 sqlite3StringToId(pCExpr); 1380 if( pCExpr->op==TK_ID ){ 1381 const char *zCName = pCExpr->u.zToken; 1382 for(iCol=0; iCol<pTab->nCol; iCol++){ 1383 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){ 1384 pCol = &pTab->aCol[iCol]; 1385 pCol->colFlags |= COLFLAG_PRIMKEY; 1386 break; 1387 } 1388 } 1389 } 1390 } 1391 } 1392 if( nTerm==1 1393 && pCol 1394 && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0 1395 && sortOrder!=SQLITE_SO_DESC 1396 ){ 1397 if( IN_RENAME_OBJECT && pList ){ 1398 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr); 1399 } 1400 pTab->iPKey = iCol; 1401 pTab->keyConf = (u8)onError; 1402 assert( autoInc==0 || autoInc==1 ); 1403 pTab->tabFlags |= autoInc*TF_Autoincrement; 1404 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder; 1405 }else if( autoInc ){ 1406 #ifndef SQLITE_OMIT_AUTOINCREMENT 1407 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " 1408 "INTEGER PRIMARY KEY"); 1409 #endif 1410 }else{ 1411 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 1412 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY); 1413 pList = 0; 1414 } 1415 1416 primary_key_exit: 1417 sqlite3ExprListDelete(pParse->db, pList); 1418 return; 1419 } 1420 1421 /* 1422 ** Add a new CHECK constraint to the table currently under construction. 1423 */ 1424 void sqlite3AddCheckConstraint( 1425 Parse *pParse, /* Parsing context */ 1426 Expr *pCheckExpr /* The check expression */ 1427 ){ 1428 #ifndef SQLITE_OMIT_CHECK 1429 Table *pTab = pParse->pNewTable; 1430 sqlite3 *db = pParse->db; 1431 if( pTab && !IN_DECLARE_VTAB 1432 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt) 1433 ){ 1434 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr); 1435 if( pParse->constraintName.n ){ 1436 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1); 1437 } 1438 }else 1439 #endif 1440 { 1441 sqlite3ExprDelete(pParse->db, pCheckExpr); 1442 } 1443 } 1444 1445 /* 1446 ** Set the collation function of the most recently parsed table column 1447 ** to the CollSeq given. 1448 */ 1449 void sqlite3AddCollateType(Parse *pParse, Token *pToken){ 1450 Table *p; 1451 int i; 1452 char *zColl; /* Dequoted name of collation sequence */ 1453 sqlite3 *db; 1454 1455 if( (p = pParse->pNewTable)==0 ) return; 1456 i = p->nCol-1; 1457 db = pParse->db; 1458 zColl = sqlite3NameFromToken(db, pToken); 1459 if( !zColl ) return; 1460 1461 if( sqlite3LocateCollSeq(pParse, zColl) ){ 1462 Index *pIdx; 1463 sqlite3DbFree(db, p->aCol[i].zColl); 1464 p->aCol[i].zColl = zColl; 1465 1466 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", 1467 ** then an index may have been created on this column before the 1468 ** collation type was added. Correct this if it is the case. 1469 */ 1470 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ 1471 assert( pIdx->nKeyCol==1 ); 1472 if( pIdx->aiColumn[0]==i ){ 1473 pIdx->azColl[0] = p->aCol[i].zColl; 1474 } 1475 } 1476 }else{ 1477 sqlite3DbFree(db, zColl); 1478 } 1479 } 1480 1481 /* 1482 ** This function returns the collation sequence for database native text 1483 ** encoding identified by the string zName, length nName. 1484 ** 1485 ** If the requested collation sequence is not available, or not available 1486 ** in the database native encoding, the collation factory is invoked to 1487 ** request it. If the collation factory does not supply such a sequence, 1488 ** and the sequence is available in another text encoding, then that is 1489 ** returned instead. 1490 ** 1491 ** If no versions of the requested collations sequence are available, or 1492 ** another error occurs, NULL is returned and an error message written into 1493 ** pParse. 1494 ** 1495 ** This routine is a wrapper around sqlite3FindCollSeq(). This routine 1496 ** invokes the collation factory if the named collation cannot be found 1497 ** and generates an error message. 1498 ** 1499 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq() 1500 */ 1501 CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){ 1502 sqlite3 *db = pParse->db; 1503 u8 enc = ENC(db); 1504 u8 initbusy = db->init.busy; 1505 CollSeq *pColl; 1506 1507 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy); 1508 if( !initbusy && (!pColl || !pColl->xCmp) ){ 1509 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName); 1510 } 1511 1512 return pColl; 1513 } 1514 1515 1516 /* 1517 ** Generate code that will increment the schema cookie. 1518 ** 1519 ** The schema cookie is used to determine when the schema for the 1520 ** database changes. After each schema change, the cookie value 1521 ** changes. When a process first reads the schema it records the 1522 ** cookie. Thereafter, whenever it goes to access the database, 1523 ** it checks the cookie to make sure the schema has not changed 1524 ** since it was last read. 1525 ** 1526 ** This plan is not completely bullet-proof. It is possible for 1527 ** the schema to change multiple times and for the cookie to be 1528 ** set back to prior value. But schema changes are infrequent 1529 ** and the probability of hitting the same cookie value is only 1530 ** 1 chance in 2^32. So we're safe enough. 1531 ** 1532 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments 1533 ** the schema-version whenever the schema changes. 1534 */ 1535 void sqlite3ChangeCookie(Parse *pParse, int iDb){ 1536 sqlite3 *db = pParse->db; 1537 Vdbe *v = pParse->pVdbe; 1538 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 1539 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, 1540 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie)); 1541 } 1542 1543 /* 1544 ** Measure the number of characters needed to output the given 1545 ** identifier. The number returned includes any quotes used 1546 ** but does not include the null terminator. 1547 ** 1548 ** The estimate is conservative. It might be larger that what is 1549 ** really needed. 1550 */ 1551 static int identLength(const char *z){ 1552 int n; 1553 for(n=0; *z; n++, z++){ 1554 if( *z=='"' ){ n++; } 1555 } 1556 return n + 2; 1557 } 1558 1559 /* 1560 ** The first parameter is a pointer to an output buffer. The second 1561 ** parameter is a pointer to an integer that contains the offset at 1562 ** which to write into the output buffer. This function copies the 1563 ** nul-terminated string pointed to by the third parameter, zSignedIdent, 1564 ** to the specified offset in the buffer and updates *pIdx to refer 1565 ** to the first byte after the last byte written before returning. 1566 ** 1567 ** If the string zSignedIdent consists entirely of alpha-numeric 1568 ** characters, does not begin with a digit and is not an SQL keyword, 1569 ** then it is copied to the output buffer exactly as it is. Otherwise, 1570 ** it is quoted using double-quotes. 1571 */ 1572 static void identPut(char *z, int *pIdx, char *zSignedIdent){ 1573 unsigned char *zIdent = (unsigned char*)zSignedIdent; 1574 int i, j, needQuote; 1575 i = *pIdx; 1576 1577 for(j=0; zIdent[j]; j++){ 1578 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break; 1579 } 1580 needQuote = sqlite3Isdigit(zIdent[0]) 1581 || sqlite3KeywordCode(zIdent, j)!=TK_ID 1582 || zIdent[j]!=0 1583 || j==0; 1584 1585 if( needQuote ) z[i++] = '"'; 1586 for(j=0; zIdent[j]; j++){ 1587 z[i++] = zIdent[j]; 1588 if( zIdent[j]=='"' ) z[i++] = '"'; 1589 } 1590 if( needQuote ) z[i++] = '"'; 1591 z[i] = 0; 1592 *pIdx = i; 1593 } 1594 1595 /* 1596 ** Generate a CREATE TABLE statement appropriate for the given 1597 ** table. Memory to hold the text of the statement is obtained 1598 ** from sqliteMalloc() and must be freed by the calling function. 1599 */ 1600 static char *createTableStmt(sqlite3 *db, Table *p){ 1601 int i, k, n; 1602 char *zStmt; 1603 char *zSep, *zSep2, *zEnd; 1604 Column *pCol; 1605 n = 0; 1606 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){ 1607 n += identLength(pCol->zName) + 5; 1608 } 1609 n += identLength(p->zName); 1610 if( n<50 ){ 1611 zSep = ""; 1612 zSep2 = ","; 1613 zEnd = ")"; 1614 }else{ 1615 zSep = "\n "; 1616 zSep2 = ",\n "; 1617 zEnd = "\n)"; 1618 } 1619 n += 35 + 6*p->nCol; 1620 zStmt = sqlite3DbMallocRaw(0, n); 1621 if( zStmt==0 ){ 1622 sqlite3OomFault(db); 1623 return 0; 1624 } 1625 sqlite3_snprintf(n, zStmt, "CREATE TABLE "); 1626 k = sqlite3Strlen30(zStmt); 1627 identPut(zStmt, &k, p->zName); 1628 zStmt[k++] = '('; 1629 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ 1630 static const char * const azType[] = { 1631 /* SQLITE_AFF_BLOB */ "", 1632 /* SQLITE_AFF_TEXT */ " TEXT", 1633 /* SQLITE_AFF_NUMERIC */ " NUM", 1634 /* SQLITE_AFF_INTEGER */ " INT", 1635 /* SQLITE_AFF_REAL */ " REAL" 1636 }; 1637 int len; 1638 const char *zType; 1639 1640 sqlite3_snprintf(n-k, &zStmt[k], zSep); 1641 k += sqlite3Strlen30(&zStmt[k]); 1642 zSep = zSep2; 1643 identPut(zStmt, &k, pCol->zName); 1644 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); 1645 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); 1646 testcase( pCol->affinity==SQLITE_AFF_BLOB ); 1647 testcase( pCol->affinity==SQLITE_AFF_TEXT ); 1648 testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); 1649 testcase( pCol->affinity==SQLITE_AFF_INTEGER ); 1650 testcase( pCol->affinity==SQLITE_AFF_REAL ); 1651 1652 zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; 1653 len = sqlite3Strlen30(zType); 1654 assert( pCol->affinity==SQLITE_AFF_BLOB 1655 || pCol->affinity==sqlite3AffinityType(zType, 0) ); 1656 memcpy(&zStmt[k], zType, len); 1657 k += len; 1658 assert( k<=n ); 1659 } 1660 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); 1661 return zStmt; 1662 } 1663 1664 /* 1665 ** Resize an Index object to hold N columns total. Return SQLITE_OK 1666 ** on success and SQLITE_NOMEM on an OOM error. 1667 */ 1668 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ 1669 char *zExtra; 1670 int nByte; 1671 if( pIdx->nColumn>=N ) return SQLITE_OK; 1672 assert( pIdx->isResized==0 ); 1673 nByte = (sizeof(char*) + sizeof(i16) + 1)*N; 1674 zExtra = sqlite3DbMallocZero(db, nByte); 1675 if( zExtra==0 ) return SQLITE_NOMEM_BKPT; 1676 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); 1677 pIdx->azColl = (const char**)zExtra; 1678 zExtra += sizeof(char*)*N; 1679 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); 1680 pIdx->aiColumn = (i16*)zExtra; 1681 zExtra += sizeof(i16)*N; 1682 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); 1683 pIdx->aSortOrder = (u8*)zExtra; 1684 pIdx->nColumn = N; 1685 pIdx->isResized = 1; 1686 return SQLITE_OK; 1687 } 1688 1689 /* 1690 ** Estimate the total row width for a table. 1691 */ 1692 static void estimateTableWidth(Table *pTab){ 1693 unsigned wTable = 0; 1694 const Column *pTabCol; 1695 int i; 1696 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){ 1697 wTable += pTabCol->szEst; 1698 } 1699 if( pTab->iPKey<0 ) wTable++; 1700 pTab->szTabRow = sqlite3LogEst(wTable*4); 1701 } 1702 1703 /* 1704 ** Estimate the average size of a row for an index. 1705 */ 1706 static void estimateIndexWidth(Index *pIdx){ 1707 unsigned wIndex = 0; 1708 int i; 1709 const Column *aCol = pIdx->pTable->aCol; 1710 for(i=0; i<pIdx->nColumn; i++){ 1711 i16 x = pIdx->aiColumn[i]; 1712 assert( x<pIdx->pTable->nCol ); 1713 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst; 1714 } 1715 pIdx->szIdxRow = sqlite3LogEst(wIndex*4); 1716 } 1717 1718 /* Return true if value x is found any of the first nCol entries of aiCol[] 1719 */ 1720 static int hasColumn(const i16 *aiCol, int nCol, int x){ 1721 while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1; 1722 return 0; 1723 } 1724 1725 /* Recompute the colNotIdxed field of the Index. 1726 ** 1727 ** colNotIdxed is a bitmask that has a 0 bit representing each indexed 1728 ** columns that are within the first 63 columns of the table. The 1729 ** high-order bit of colNotIdxed is always 1. All unindexed columns 1730 ** of the table have a 1. 1731 ** 1732 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask 1733 ** to determine if the index is covering index. 1734 */ 1735 static void recomputeColumnsNotIndexed(Index *pIdx){ 1736 Bitmask m = 0; 1737 int j; 1738 for(j=pIdx->nColumn-1; j>=0; j--){ 1739 int x = pIdx->aiColumn[j]; 1740 if( x>=0 ){ 1741 testcase( x==BMS-1 ); 1742 testcase( x==BMS-2 ); 1743 if( x<BMS-1 ) m |= MASKBIT(x); 1744 } 1745 } 1746 pIdx->colNotIdxed = ~m; 1747 assert( (pIdx->colNotIdxed>>63)==1 ); 1748 } 1749 1750 /* 1751 ** This routine runs at the end of parsing a CREATE TABLE statement that 1752 ** has a WITHOUT ROWID clause. The job of this routine is to convert both 1753 ** internal schema data structures and the generated VDBE code so that they 1754 ** are appropriate for a WITHOUT ROWID table instead of a rowid table. 1755 ** Changes include: 1756 ** 1757 ** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL. 1758 ** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY 1759 ** into BTREE_BLOBKEY. 1760 ** (3) Bypass the creation of the sqlite_master table entry 1761 ** for the PRIMARY KEY as the primary key index is now 1762 ** identified by the sqlite_master table entry of the table itself. 1763 ** (4) Set the Index.tnum of the PRIMARY KEY Index object in the 1764 ** schema to the rootpage from the main table. 1765 ** (5) Add all table columns to the PRIMARY KEY Index object 1766 ** so that the PRIMARY KEY is a covering index. The surplus 1767 ** columns are part of KeyInfo.nAllField and are not used for 1768 ** sorting or lookup or uniqueness checks. 1769 ** (6) Replace the rowid tail on all automatically generated UNIQUE 1770 ** indices with the PRIMARY KEY columns. 1771 ** 1772 ** For virtual tables, only (1) is performed. 1773 */ 1774 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ 1775 Index *pIdx; 1776 Index *pPk; 1777 int nPk; 1778 int i, j; 1779 sqlite3 *db = pParse->db; 1780 Vdbe *v = pParse->pVdbe; 1781 1782 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) 1783 */ 1784 if( !db->init.imposterTable ){ 1785 for(i=0; i<pTab->nCol; i++){ 1786 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){ 1787 pTab->aCol[i].notNull = OE_Abort; 1788 } 1789 } 1790 } 1791 1792 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY 1793 ** into BTREE_BLOBKEY. 1794 */ 1795 if( pParse->addrCrTab ){ 1796 assert( v ); 1797 sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY); 1798 } 1799 1800 /* Locate the PRIMARY KEY index. Or, if this table was originally 1801 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. 1802 */ 1803 if( pTab->iPKey>=0 ){ 1804 ExprList *pList; 1805 Token ipkToken; 1806 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName); 1807 pList = sqlite3ExprListAppend(pParse, 0, 1808 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); 1809 if( pList==0 ) return; 1810 pList->a[0].sortOrder = pParse->iPkSortOrder; 1811 assert( pParse->pNewTable==pTab ); 1812 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, 1813 SQLITE_IDXTYPE_PRIMARYKEY); 1814 if( db->mallocFailed || pParse->nErr ) return; 1815 pPk = sqlite3PrimaryKeyIndex(pTab); 1816 pTab->iPKey = -1; 1817 }else{ 1818 pPk = sqlite3PrimaryKeyIndex(pTab); 1819 1820 /* 1821 ** Remove all redundant columns from the PRIMARY KEY. For example, change 1822 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later 1823 ** code assumes the PRIMARY KEY contains no repeated columns. 1824 */ 1825 for(i=j=1; i<pPk->nKeyCol; i++){ 1826 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){ 1827 pPk->nColumn--; 1828 }else{ 1829 pPk->aiColumn[j++] = pPk->aiColumn[i]; 1830 } 1831 } 1832 pPk->nKeyCol = j; 1833 } 1834 assert( pPk!=0 ); 1835 pPk->isCovering = 1; 1836 if( !db->init.imposterTable ) pPk->uniqNotNull = 1; 1837 nPk = pPk->nKeyCol; 1838 1839 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master 1840 ** table entry. This is only required if currently generating VDBE 1841 ** code for a CREATE TABLE (not when parsing one as part of reading 1842 ** a database schema). */ 1843 if( v && pPk->tnum>0 ){ 1844 assert( db->init.busy==0 ); 1845 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto); 1846 } 1847 1848 /* The root page of the PRIMARY KEY is the table root page */ 1849 pPk->tnum = pTab->tnum; 1850 1851 /* Update the in-memory representation of all UNIQUE indices by converting 1852 ** the final rowid column into one or more columns of the PRIMARY KEY. 1853 */ 1854 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 1855 int n; 1856 if( IsPrimaryKeyIndex(pIdx) ) continue; 1857 for(i=n=0; i<nPk; i++){ 1858 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++; 1859 } 1860 if( n==0 ){ 1861 /* This index is a superset of the primary key */ 1862 pIdx->nColumn = pIdx->nKeyCol; 1863 continue; 1864 } 1865 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return; 1866 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){ 1867 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){ 1868 pIdx->aiColumn[j] = pPk->aiColumn[i]; 1869 pIdx->azColl[j] = pPk->azColl[i]; 1870 j++; 1871 } 1872 } 1873 assert( pIdx->nColumn>=pIdx->nKeyCol+n ); 1874 assert( pIdx->nColumn>=j ); 1875 } 1876 1877 /* Add all table columns to the PRIMARY KEY index 1878 */ 1879 if( nPk<pTab->nCol ){ 1880 if( resizeIndexObject(db, pPk, pTab->nCol) ) return; 1881 for(i=0, j=nPk; i<pTab->nCol; i++){ 1882 if( !hasColumn(pPk->aiColumn, j, i) ){ 1883 assert( j<pPk->nColumn ); 1884 pPk->aiColumn[j] = i; 1885 pPk->azColl[j] = sqlite3StrBINARY; 1886 j++; 1887 } 1888 } 1889 assert( pPk->nColumn==j ); 1890 assert( pTab->nCol==j ); 1891 }else{ 1892 pPk->nColumn = pTab->nCol; 1893 } 1894 recomputeColumnsNotIndexed(pPk); 1895 } 1896 1897 /* 1898 ** Return true if zName is a shadow table name in the current database 1899 ** connection. 1900 ** 1901 ** zName is temporarily modified while this routine is running, but is 1902 ** restored to its original value prior to this routine returning. 1903 */ 1904 static int isShadowTableName(sqlite3 *db, char *zName){ 1905 char *zTail; /* Pointer to the last "_" in zName */ 1906 Table *pTab; /* Table that zName is a shadow of */ 1907 VTable *pVTab; /* Virtual table corresponding to pTab */ 1908 const sqlite3_module *pMod; /* module methods for pVTab */ 1909 zTail = strrchr(zName, '_'); 1910 if( zTail==0 ) return 0; 1911 *zTail = 0; 1912 pTab = sqlite3FindTable(db, zName, 0); 1913 *zTail = '_'; 1914 if( pTab==0 ) return 0; 1915 if( !IsVirtual(pTab) ) return 0; 1916 pVTab = sqlite3GetVTable(db, pTab); 1917 if( pVTab==0 ) return 0; 1918 if( pVTab->pMod==0 ) return 0; 1919 pMod = pVTab->pMod->pModule; 1920 assert( pMod!=0 ); 1921 if( pMod->iVersion<3 ) return 0; 1922 if( pMod->xShadowName==0 ) return 0; 1923 return pMod->xShadowName(zTail+1); 1924 } 1925 1926 /* 1927 ** This routine is called to report the final ")" that terminates 1928 ** a CREATE TABLE statement. 1929 ** 1930 ** The table structure that other action routines have been building 1931 ** is added to the internal hash tables, assuming no errors have 1932 ** occurred. 1933 ** 1934 ** An entry for the table is made in the master table on disk, unless 1935 ** this is a temporary table or db->init.busy==1. When db->init.busy==1 1936 ** it means we are reading the sqlite_master table because we just 1937 ** connected to the database or because the sqlite_master table has 1938 ** recently changed, so the entry for this table already exists in 1939 ** the sqlite_master table. We do not want to create it again. 1940 ** 1941 ** If the pSelect argument is not NULL, it means that this routine 1942 ** was called to create a table generated from a 1943 ** "CREATE TABLE ... AS SELECT ..." statement. The column names of 1944 ** the new table will match the result set of the SELECT. 1945 */ 1946 void sqlite3EndTable( 1947 Parse *pParse, /* Parse context */ 1948 Token *pCons, /* The ',' token after the last column defn. */ 1949 Token *pEnd, /* The ')' before options in the CREATE TABLE */ 1950 u8 tabOpts, /* Extra table options. Usually 0. */ 1951 Select *pSelect /* Select from a "CREATE ... AS SELECT" */ 1952 ){ 1953 Table *p; /* The new table */ 1954 sqlite3 *db = pParse->db; /* The database connection */ 1955 int iDb; /* Database in which the table lives */ 1956 Index *pIdx; /* An implied index of the table */ 1957 1958 if( pEnd==0 && pSelect==0 ){ 1959 return; 1960 } 1961 assert( !db->mallocFailed ); 1962 p = pParse->pNewTable; 1963 if( p==0 ) return; 1964 1965 if( pSelect==0 && isShadowTableName(db, p->zName) ){ 1966 p->tabFlags |= TF_Shadow; 1967 } 1968 1969 /* If the db->init.busy is 1 it means we are reading the SQL off the 1970 ** "sqlite_master" or "sqlite_temp_master" table on the disk. 1971 ** So do not write to the disk again. Extract the root page number 1972 ** for the table from the db->init.newTnum field. (The page number 1973 ** should have been put there by the sqliteOpenCb routine.) 1974 ** 1975 ** If the root page number is 1, that means this is the sqlite_master 1976 ** table itself. So mark it read-only. 1977 */ 1978 if( db->init.busy ){ 1979 if( pSelect ){ 1980 sqlite3ErrorMsg(pParse, ""); 1981 return; 1982 } 1983 p->tnum = db->init.newTnum; 1984 if( p->tnum==1 ) p->tabFlags |= TF_Readonly; 1985 } 1986 1987 /* Special processing for WITHOUT ROWID Tables */ 1988 if( tabOpts & TF_WithoutRowid ){ 1989 if( (p->tabFlags & TF_Autoincrement) ){ 1990 sqlite3ErrorMsg(pParse, 1991 "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); 1992 return; 1993 } 1994 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ 1995 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); 1996 }else{ 1997 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; 1998 convertToWithoutRowidTable(pParse, p); 1999 } 2000 } 2001 2002 iDb = sqlite3SchemaToIndex(db, p->pSchema); 2003 2004 #ifndef SQLITE_OMIT_CHECK 2005 /* Resolve names in all CHECK constraint expressions. 2006 */ 2007 if( p->pCheck ){ 2008 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck); 2009 } 2010 #endif /* !defined(SQLITE_OMIT_CHECK) */ 2011 2012 /* Estimate the average row size for the table and for all implied indices */ 2013 estimateTableWidth(p); 2014 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ 2015 estimateIndexWidth(pIdx); 2016 } 2017 2018 /* If not initializing, then create a record for the new table 2019 ** in the SQLITE_MASTER table of the database. 2020 ** 2021 ** If this is a TEMPORARY table, write the entry into the auxiliary 2022 ** file instead of into the main database file. 2023 */ 2024 if( !db->init.busy ){ 2025 int n; 2026 Vdbe *v; 2027 char *zType; /* "view" or "table" */ 2028 char *zType2; /* "VIEW" or "TABLE" */ 2029 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ 2030 2031 v = sqlite3GetVdbe(pParse); 2032 if( NEVER(v==0) ) return; 2033 2034 sqlite3VdbeAddOp1(v, OP_Close, 0); 2035 2036 /* 2037 ** Initialize zType for the new view or table. 2038 */ 2039 if( p->pSelect==0 ){ 2040 /* A regular table */ 2041 zType = "table"; 2042 zType2 = "TABLE"; 2043 #ifndef SQLITE_OMIT_VIEW 2044 }else{ 2045 /* A view */ 2046 zType = "view"; 2047 zType2 = "VIEW"; 2048 #endif 2049 } 2050 2051 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT 2052 ** statement to populate the new table. The root-page number for the 2053 ** new table is in register pParse->regRoot. 2054 ** 2055 ** Once the SELECT has been coded by sqlite3Select(), it is in a 2056 ** suitable state to query for the column names and types to be used 2057 ** by the new table. 2058 ** 2059 ** A shared-cache write-lock is not required to write to the new table, 2060 ** as a schema-lock must have already been obtained to create it. Since 2061 ** a schema-lock excludes all other database users, the write-lock would 2062 ** be redundant. 2063 */ 2064 if( pSelect ){ 2065 SelectDest dest; /* Where the SELECT should store results */ 2066 int regYield; /* Register holding co-routine entry-point */ 2067 int addrTop; /* Top of the co-routine */ 2068 int regRec; /* A record to be insert into the new table */ 2069 int regRowid; /* Rowid of the next row to insert */ 2070 int addrInsLoop; /* Top of the loop for inserting rows */ 2071 Table *pSelTab; /* A table that describes the SELECT results */ 2072 2073 regYield = ++pParse->nMem; 2074 regRec = ++pParse->nMem; 2075 regRowid = ++pParse->nMem; 2076 assert(pParse->nTab==1); 2077 sqlite3MayAbort(pParse); 2078 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); 2079 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); 2080 pParse->nTab = 2; 2081 addrTop = sqlite3VdbeCurrentAddr(v) + 1; 2082 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); 2083 if( pParse->nErr ) return; 2084 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); 2085 if( pSelTab==0 ) return; 2086 assert( p->aCol==0 ); 2087 p->nCol = pSelTab->nCol; 2088 p->aCol = pSelTab->aCol; 2089 pSelTab->nCol = 0; 2090 pSelTab->aCol = 0; 2091 sqlite3DeleteTable(db, pSelTab); 2092 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); 2093 sqlite3Select(pParse, pSelect, &dest); 2094 if( pParse->nErr ) return; 2095 sqlite3VdbeEndCoroutine(v, regYield); 2096 sqlite3VdbeJumpHere(v, addrTop - 1); 2097 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); 2098 VdbeCoverage(v); 2099 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); 2100 sqlite3TableAffinity(v, p, 0); 2101 sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); 2102 sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); 2103 sqlite3VdbeGoto(v, addrInsLoop); 2104 sqlite3VdbeJumpHere(v, addrInsLoop); 2105 sqlite3VdbeAddOp1(v, OP_Close, 1); 2106 } 2107 2108 /* Compute the complete text of the CREATE statement */ 2109 if( pSelect ){ 2110 zStmt = createTableStmt(db, p); 2111 }else{ 2112 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; 2113 n = (int)(pEnd2->z - pParse->sNameToken.z); 2114 if( pEnd2->z[0]!=';' ) n += pEnd2->n; 2115 zStmt = sqlite3MPrintf(db, 2116 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z 2117 ); 2118 } 2119 2120 /* A slot for the record has already been allocated in the 2121 ** SQLITE_MASTER table. We just need to update that slot with all 2122 ** the information we've collected. 2123 */ 2124 sqlite3NestedParse(pParse, 2125 "UPDATE %Q.%s " 2126 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " 2127 "WHERE rowid=#%d", 2128 db->aDb[iDb].zDbSName, MASTER_NAME, 2129 zType, 2130 p->zName, 2131 p->zName, 2132 pParse->regRoot, 2133 zStmt, 2134 pParse->regRowid 2135 ); 2136 sqlite3DbFree(db, zStmt); 2137 sqlite3ChangeCookie(pParse, iDb); 2138 2139 #ifndef SQLITE_OMIT_AUTOINCREMENT 2140 /* Check to see if we need to create an sqlite_sequence table for 2141 ** keeping track of autoincrement keys. 2142 */ 2143 if( (p->tabFlags & TF_Autoincrement)!=0 ){ 2144 Db *pDb = &db->aDb[iDb]; 2145 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 2146 if( pDb->pSchema->pSeqTab==0 ){ 2147 sqlite3NestedParse(pParse, 2148 "CREATE TABLE %Q.sqlite_sequence(name,seq)", 2149 pDb->zDbSName 2150 ); 2151 } 2152 } 2153 #endif 2154 2155 /* Reparse everything to update our internal data structures */ 2156 sqlite3VdbeAddParseSchemaOp(v, iDb, 2157 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName)); 2158 } 2159 2160 2161 /* Add the table to the in-memory representation of the database. 2162 */ 2163 if( db->init.busy ){ 2164 Table *pOld; 2165 Schema *pSchema = p->pSchema; 2166 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 2167 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); 2168 if( pOld ){ 2169 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ 2170 sqlite3OomFault(db); 2171 return; 2172 } 2173 pParse->pNewTable = 0; 2174 db->mDbFlags |= DBFLAG_SchemaChange; 2175 2176 #ifndef SQLITE_OMIT_ALTERTABLE 2177 if( !p->pSelect ){ 2178 const char *zName = (const char *)pParse->sNameToken.z; 2179 int nName; 2180 assert( !pSelect && pCons && pEnd ); 2181 if( pCons->z==0 ){ 2182 pCons = pEnd; 2183 } 2184 nName = (int)((const char *)pCons->z - zName); 2185 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName); 2186 } 2187 #endif 2188 } 2189 } 2190 2191 #ifndef SQLITE_OMIT_VIEW 2192 /* 2193 ** The parser calls this routine in order to create a new VIEW 2194 */ 2195 void sqlite3CreateView( 2196 Parse *pParse, /* The parsing context */ 2197 Token *pBegin, /* The CREATE token that begins the statement */ 2198 Token *pName1, /* The token that holds the name of the view */ 2199 Token *pName2, /* The token that holds the name of the view */ 2200 ExprList *pCNames, /* Optional list of view column names */ 2201 Select *pSelect, /* A SELECT statement that will become the new view */ 2202 int isTemp, /* TRUE for a TEMPORARY view */ 2203 int noErr /* Suppress error messages if VIEW already exists */ 2204 ){ 2205 Table *p; 2206 int n; 2207 const char *z; 2208 Token sEnd; 2209 DbFixer sFix; 2210 Token *pName = 0; 2211 int iDb; 2212 sqlite3 *db = pParse->db; 2213 2214 if( pParse->nVar>0 ){ 2215 sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); 2216 goto create_view_fail; 2217 } 2218 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); 2219 p = pParse->pNewTable; 2220 if( p==0 || pParse->nErr ) goto create_view_fail; 2221 sqlite3TwoPartName(pParse, pName1, pName2, &pName); 2222 iDb = sqlite3SchemaToIndex(db, p->pSchema); 2223 sqlite3FixInit(&sFix, pParse, iDb, "view", pName); 2224 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; 2225 2226 /* Make a copy of the entire SELECT statement that defines the view. 2227 ** This will force all the Expr.token.z values to be dynamically 2228 ** allocated rather than point to the input string - which means that 2229 ** they will persist after the current sqlite3_exec() call returns. 2230 */ 2231 if( IN_RENAME_OBJECT ){ 2232 p->pSelect = pSelect; 2233 pSelect = 0; 2234 }else{ 2235 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); 2236 } 2237 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); 2238 if( db->mallocFailed ) goto create_view_fail; 2239 2240 /* Locate the end of the CREATE VIEW statement. Make sEnd point to 2241 ** the end. 2242 */ 2243 sEnd = pParse->sLastToken; 2244 assert( sEnd.z[0]!=0 || sEnd.n==0 ); 2245 if( sEnd.z[0]!=';' ){ 2246 sEnd.z += sEnd.n; 2247 } 2248 sEnd.n = 0; 2249 n = (int)(sEnd.z - pBegin->z); 2250 assert( n>0 ); 2251 z = pBegin->z; 2252 while( sqlite3Isspace(z[n-1]) ){ n--; } 2253 sEnd.z = &z[n-1]; 2254 sEnd.n = 1; 2255 2256 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ 2257 sqlite3EndTable(pParse, 0, &sEnd, 0, 0); 2258 2259 create_view_fail: 2260 sqlite3SelectDelete(db, pSelect); 2261 if( IN_RENAME_OBJECT ){ 2262 sqlite3RenameExprlistUnmap(pParse, pCNames); 2263 } 2264 sqlite3ExprListDelete(db, pCNames); 2265 return; 2266 } 2267 #endif /* SQLITE_OMIT_VIEW */ 2268 2269 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) 2270 /* 2271 ** The Table structure pTable is really a VIEW. Fill in the names of 2272 ** the columns of the view in the pTable structure. Return the number 2273 ** of errors. If an error is seen leave an error message in pParse->zErrMsg. 2274 */ 2275 int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ 2276 Table *pSelTab; /* A fake table from which we get the result set */ 2277 Select *pSel; /* Copy of the SELECT that implements the view */ 2278 int nErr = 0; /* Number of errors encountered */ 2279 int n; /* Temporarily holds the number of cursors assigned */ 2280 sqlite3 *db = pParse->db; /* Database connection for malloc errors */ 2281 #ifndef SQLITE_OMIT_VIRTUALTABLE 2282 int rc; 2283 #endif 2284 #ifndef SQLITE_OMIT_AUTHORIZATION 2285 sqlite3_xauth xAuth; /* Saved xAuth pointer */ 2286 #endif 2287 2288 assert( pTable ); 2289 2290 #ifndef SQLITE_OMIT_VIRTUALTABLE 2291 db->nSchemaLock++; 2292 rc = sqlite3VtabCallConnect(pParse, pTable); 2293 db->nSchemaLock--; 2294 if( rc ){ 2295 return 1; 2296 } 2297 if( IsVirtual(pTable) ) return 0; 2298 #endif 2299 2300 #ifndef SQLITE_OMIT_VIEW 2301 /* A positive nCol means the columns names for this view are 2302 ** already known. 2303 */ 2304 if( pTable->nCol>0 ) return 0; 2305 2306 /* A negative nCol is a special marker meaning that we are currently 2307 ** trying to compute the column names. If we enter this routine with 2308 ** a negative nCol, it means two or more views form a loop, like this: 2309 ** 2310 ** CREATE VIEW one AS SELECT * FROM two; 2311 ** CREATE VIEW two AS SELECT * FROM one; 2312 ** 2313 ** Actually, the error above is now caught prior to reaching this point. 2314 ** But the following test is still important as it does come up 2315 ** in the following: 2316 ** 2317 ** CREATE TABLE main.ex1(a); 2318 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; 2319 ** SELECT * FROM temp.ex1; 2320 */ 2321 if( pTable->nCol<0 ){ 2322 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); 2323 return 1; 2324 } 2325 assert( pTable->nCol>=0 ); 2326 2327 /* If we get this far, it means we need to compute the table names. 2328 ** Note that the call to sqlite3ResultSetOfSelect() will expand any 2329 ** "*" elements in the results set of the view and will assign cursors 2330 ** to the elements of the FROM clause. But we do not want these changes 2331 ** to be permanent. So the computation is done on a copy of the SELECT 2332 ** statement that defines the view. 2333 */ 2334 assert( pTable->pSelect ); 2335 pSel = sqlite3SelectDup(db, pTable->pSelect, 0); 2336 if( pSel ){ 2337 #ifndef SQLITE_OMIT_ALTERTABLE 2338 u8 eParseMode = pParse->eParseMode; 2339 pParse->eParseMode = PARSE_MODE_NORMAL; 2340 #endif 2341 n = pParse->nTab; 2342 sqlite3SrcListAssignCursors(pParse, pSel->pSrc); 2343 pTable->nCol = -1; 2344 db->lookaside.bDisable++; 2345 #ifndef SQLITE_OMIT_AUTHORIZATION 2346 xAuth = db->xAuth; 2347 db->xAuth = 0; 2348 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); 2349 db->xAuth = xAuth; 2350 #else 2351 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); 2352 #endif 2353 pParse->nTab = n; 2354 if( pTable->pCheck ){ 2355 /* CREATE VIEW name(arglist) AS ... 2356 ** The names of the columns in the table are taken from 2357 ** arglist which is stored in pTable->pCheck. The pCheck field 2358 ** normally holds CHECK constraints on an ordinary table, but for 2359 ** a VIEW it holds the list of column names. 2360 */ 2361 sqlite3ColumnsFromExprList(pParse, pTable->pCheck, 2362 &pTable->nCol, &pTable->aCol); 2363 if( db->mallocFailed==0 2364 && pParse->nErr==0 2365 && pTable->nCol==pSel->pEList->nExpr 2366 ){ 2367 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel); 2368 } 2369 }else if( pSelTab ){ 2370 /* CREATE VIEW name AS... without an argument list. Construct 2371 ** the column names from the SELECT statement that defines the view. 2372 */ 2373 assert( pTable->aCol==0 ); 2374 pTable->nCol = pSelTab->nCol; 2375 pTable->aCol = pSelTab->aCol; 2376 pSelTab->nCol = 0; 2377 pSelTab->aCol = 0; 2378 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); 2379 }else{ 2380 pTable->nCol = 0; 2381 nErr++; 2382 } 2383 sqlite3DeleteTable(db, pSelTab); 2384 sqlite3SelectDelete(db, pSel); 2385 db->lookaside.bDisable--; 2386 #ifndef SQLITE_OMIT_ALTERTABLE 2387 pParse->eParseMode = eParseMode; 2388 #endif 2389 } else { 2390 nErr++; 2391 } 2392 pTable->pSchema->schemaFlags |= DB_UnresetViews; 2393 if( db->mallocFailed ){ 2394 sqlite3DeleteColumnNames(db, pTable); 2395 pTable->aCol = 0; 2396 pTable->nCol = 0; 2397 } 2398 #endif /* SQLITE_OMIT_VIEW */ 2399 return nErr; 2400 } 2401 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ 2402 2403 #ifndef SQLITE_OMIT_VIEW 2404 /* 2405 ** Clear the column names from every VIEW in database idx. 2406 */ 2407 static void sqliteViewResetAll(sqlite3 *db, int idx){ 2408 HashElem *i; 2409 assert( sqlite3SchemaMutexHeld(db, idx, 0) ); 2410 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; 2411 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ 2412 Table *pTab = sqliteHashData(i); 2413 if( pTab->pSelect ){ 2414 sqlite3DeleteColumnNames(db, pTab); 2415 pTab->aCol = 0; 2416 pTab->nCol = 0; 2417 } 2418 } 2419 DbClearProperty(db, idx, DB_UnresetViews); 2420 } 2421 #else 2422 # define sqliteViewResetAll(A,B) 2423 #endif /* SQLITE_OMIT_VIEW */ 2424 2425 /* 2426 ** This function is called by the VDBE to adjust the internal schema 2427 ** used by SQLite when the btree layer moves a table root page. The 2428 ** root-page of a table or index in database iDb has changed from iFrom 2429 ** to iTo. 2430 ** 2431 ** Ticket #1728: The symbol table might still contain information 2432 ** on tables and/or indices that are the process of being deleted. 2433 ** If you are unlucky, one of those deleted indices or tables might 2434 ** have the same rootpage number as the real table or index that is 2435 ** being moved. So we cannot stop searching after the first match 2436 ** because the first match might be for one of the deleted indices 2437 ** or tables and not the table/index that is actually being moved. 2438 ** We must continue looping until all tables and indices with 2439 ** rootpage==iFrom have been converted to have a rootpage of iTo 2440 ** in order to be certain that we got the right one. 2441 */ 2442 #ifndef SQLITE_OMIT_AUTOVACUUM 2443 void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){ 2444 HashElem *pElem; 2445 Hash *pHash; 2446 Db *pDb; 2447 2448 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 2449 pDb = &db->aDb[iDb]; 2450 pHash = &pDb->pSchema->tblHash; 2451 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ 2452 Table *pTab = sqliteHashData(pElem); 2453 if( pTab->tnum==iFrom ){ 2454 pTab->tnum = iTo; 2455 } 2456 } 2457 pHash = &pDb->pSchema->idxHash; 2458 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ 2459 Index *pIdx = sqliteHashData(pElem); 2460 if( pIdx->tnum==iFrom ){ 2461 pIdx->tnum = iTo; 2462 } 2463 } 2464 } 2465 #endif 2466 2467 /* 2468 ** Write code to erase the table with root-page iTable from database iDb. 2469 ** Also write code to modify the sqlite_master table and internal schema 2470 ** if a root-page of another table is moved by the btree-layer whilst 2471 ** erasing iTable (this can happen with an auto-vacuum database). 2472 */ 2473 static void destroyRootPage(Parse *pParse, int iTable, int iDb){ 2474 Vdbe *v = sqlite3GetVdbe(pParse); 2475 int r1 = sqlite3GetTempReg(pParse); 2476 if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema"); 2477 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); 2478 sqlite3MayAbort(pParse); 2479 #ifndef SQLITE_OMIT_AUTOVACUUM 2480 /* OP_Destroy stores an in integer r1. If this integer 2481 ** is non-zero, then it is the root page number of a table moved to 2482 ** location iTable. The following code modifies the sqlite_master table to 2483 ** reflect this. 2484 ** 2485 ** The "#NNN" in the SQL is a special constant that means whatever value 2486 ** is in register NNN. See grammar rules associated with the TK_REGISTER 2487 ** token for additional information. 2488 */ 2489 sqlite3NestedParse(pParse, 2490 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", 2491 pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1); 2492 #endif 2493 sqlite3ReleaseTempReg(pParse, r1); 2494 } 2495 2496 /* 2497 ** Write VDBE code to erase table pTab and all associated indices on disk. 2498 ** Code to update the sqlite_master tables and internal schema definitions 2499 ** in case a root-page belonging to another table is moved by the btree layer 2500 ** is also added (this can happen with an auto-vacuum database). 2501 */ 2502 static void destroyTable(Parse *pParse, Table *pTab){ 2503 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM 2504 ** is not defined), then it is important to call OP_Destroy on the 2505 ** table and index root-pages in order, starting with the numerically 2506 ** largest root-page number. This guarantees that none of the root-pages 2507 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the 2508 ** following were coded: 2509 ** 2510 ** OP_Destroy 4 0 2511 ** ... 2512 ** OP_Destroy 5 0 2513 ** 2514 ** and root page 5 happened to be the largest root-page number in the 2515 ** database, then root page 5 would be moved to page 4 by the 2516 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit 2517 ** a free-list page. 2518 */ 2519 int iTab = pTab->tnum; 2520 int iDestroyed = 0; 2521 2522 while( 1 ){ 2523 Index *pIdx; 2524 int iLargest = 0; 2525 2526 if( iDestroyed==0 || iTab<iDestroyed ){ 2527 iLargest = iTab; 2528 } 2529 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 2530 int iIdx = pIdx->tnum; 2531 assert( pIdx->pSchema==pTab->pSchema ); 2532 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){ 2533 iLargest = iIdx; 2534 } 2535 } 2536 if( iLargest==0 ){ 2537 return; 2538 }else{ 2539 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 2540 assert( iDb>=0 && iDb<pParse->db->nDb ); 2541 destroyRootPage(pParse, iLargest, iDb); 2542 iDestroyed = iLargest; 2543 } 2544 } 2545 } 2546 2547 /* 2548 ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) 2549 ** after a DROP INDEX or DROP TABLE command. 2550 */ 2551 static void sqlite3ClearStatTables( 2552 Parse *pParse, /* The parsing context */ 2553 int iDb, /* The database number */ 2554 const char *zType, /* "idx" or "tbl" */ 2555 const char *zName /* Name of index or table */ 2556 ){ 2557 int i; 2558 const char *zDbName = pParse->db->aDb[iDb].zDbSName; 2559 for(i=1; i<=4; i++){ 2560 char zTab[24]; 2561 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); 2562 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ 2563 sqlite3NestedParse(pParse, 2564 "DELETE FROM %Q.%s WHERE %s=%Q", 2565 zDbName, zTab, zType, zName 2566 ); 2567 } 2568 } 2569 } 2570 2571 /* 2572 ** Generate code to drop a table. 2573 */ 2574 void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ 2575 Vdbe *v; 2576 sqlite3 *db = pParse->db; 2577 Trigger *pTrigger; 2578 Db *pDb = &db->aDb[iDb]; 2579 2580 v = sqlite3GetVdbe(pParse); 2581 assert( v!=0 ); 2582 sqlite3BeginWriteOperation(pParse, 1, iDb); 2583 2584 #ifndef SQLITE_OMIT_VIRTUALTABLE 2585 if( IsVirtual(pTab) ){ 2586 sqlite3VdbeAddOp0(v, OP_VBegin); 2587 } 2588 #endif 2589 2590 /* Drop all triggers associated with the table being dropped. Code 2591 ** is generated to remove entries from sqlite_master and/or 2592 ** sqlite_temp_master if required. 2593 */ 2594 pTrigger = sqlite3TriggerList(pParse, pTab); 2595 while( pTrigger ){ 2596 assert( pTrigger->pSchema==pTab->pSchema || 2597 pTrigger->pSchema==db->aDb[1].pSchema ); 2598 sqlite3DropTriggerPtr(pParse, pTrigger); 2599 pTrigger = pTrigger->pNext; 2600 } 2601 2602 #ifndef SQLITE_OMIT_AUTOINCREMENT 2603 /* Remove any entries of the sqlite_sequence table associated with 2604 ** the table being dropped. This is done before the table is dropped 2605 ** at the btree level, in case the sqlite_sequence table needs to 2606 ** move as a result of the drop (can happen in auto-vacuum mode). 2607 */ 2608 if( pTab->tabFlags & TF_Autoincrement ){ 2609 sqlite3NestedParse(pParse, 2610 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", 2611 pDb->zDbSName, pTab->zName 2612 ); 2613 } 2614 #endif 2615 2616 /* Drop all SQLITE_MASTER table and index entries that refer to the 2617 ** table. The program name loops through the master table and deletes 2618 ** every row that refers to a table of the same name as the one being 2619 ** dropped. Triggers are handled separately because a trigger can be 2620 ** created in the temp database that refers to a table in another 2621 ** database. 2622 */ 2623 sqlite3NestedParse(pParse, 2624 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", 2625 pDb->zDbSName, MASTER_NAME, pTab->zName); 2626 if( !isView && !IsVirtual(pTab) ){ 2627 destroyTable(pParse, pTab); 2628 } 2629 2630 /* Remove the table entry from SQLite's internal schema and modify 2631 ** the schema cookie. 2632 */ 2633 if( IsVirtual(pTab) ){ 2634 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); 2635 } 2636 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); 2637 sqlite3ChangeCookie(pParse, iDb); 2638 sqliteViewResetAll(db, iDb); 2639 } 2640 2641 /* 2642 ** This routine is called to do the work of a DROP TABLE statement. 2643 ** pName is the name of the table to be dropped. 2644 */ 2645 void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ 2646 Table *pTab; 2647 Vdbe *v; 2648 sqlite3 *db = pParse->db; 2649 int iDb; 2650 2651 if( db->mallocFailed ){ 2652 goto exit_drop_table; 2653 } 2654 assert( pParse->nErr==0 ); 2655 assert( pName->nSrc==1 ); 2656 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; 2657 if( noErr ) db->suppressErr++; 2658 assert( isView==0 || isView==LOCATE_VIEW ); 2659 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); 2660 if( noErr ) db->suppressErr--; 2661 2662 if( pTab==0 ){ 2663 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); 2664 goto exit_drop_table; 2665 } 2666 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 2667 assert( iDb>=0 && iDb<db->nDb ); 2668 2669 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure 2670 ** it is initialized. 2671 */ 2672 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ 2673 goto exit_drop_table; 2674 } 2675 #ifndef SQLITE_OMIT_AUTHORIZATION 2676 { 2677 int code; 2678 const char *zTab = SCHEMA_TABLE(iDb); 2679 const char *zDb = db->aDb[iDb].zDbSName; 2680 const char *zArg2 = 0; 2681 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ 2682 goto exit_drop_table; 2683 } 2684 if( isView ){ 2685 if( !OMIT_TEMPDB && iDb==1 ){ 2686 code = SQLITE_DROP_TEMP_VIEW; 2687 }else{ 2688 code = SQLITE_DROP_VIEW; 2689 } 2690 #ifndef SQLITE_OMIT_VIRTUALTABLE 2691 }else if( IsVirtual(pTab) ){ 2692 code = SQLITE_DROP_VTABLE; 2693 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; 2694 #endif 2695 }else{ 2696 if( !OMIT_TEMPDB && iDb==1 ){ 2697 code = SQLITE_DROP_TEMP_TABLE; 2698 }else{ 2699 code = SQLITE_DROP_TABLE; 2700 } 2701 } 2702 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ 2703 goto exit_drop_table; 2704 } 2705 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ 2706 goto exit_drop_table; 2707 } 2708 } 2709 #endif 2710 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 2711 && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){ 2712 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); 2713 goto exit_drop_table; 2714 } 2715 2716 #ifndef SQLITE_OMIT_VIEW 2717 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used 2718 ** on a table. 2719 */ 2720 if( isView && pTab->pSelect==0 ){ 2721 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); 2722 goto exit_drop_table; 2723 } 2724 if( !isView && pTab->pSelect ){ 2725 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); 2726 goto exit_drop_table; 2727 } 2728 #endif 2729 2730 /* Generate code to remove the table from the master table 2731 ** on disk. 2732 */ 2733 v = sqlite3GetVdbe(pParse); 2734 if( v ){ 2735 sqlite3BeginWriteOperation(pParse, 1, iDb); 2736 if( !isView ){ 2737 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); 2738 sqlite3FkDropTable(pParse, pName, pTab); 2739 } 2740 sqlite3CodeDropTable(pParse, pTab, iDb, isView); 2741 } 2742 2743 exit_drop_table: 2744 sqlite3SrcListDelete(db, pName); 2745 } 2746 2747 /* 2748 ** This routine is called to create a new foreign key on the table 2749 ** currently under construction. pFromCol determines which columns 2750 ** in the current table point to the foreign key. If pFromCol==0 then 2751 ** connect the key to the last column inserted. pTo is the name of 2752 ** the table referred to (a.k.a the "parent" table). pToCol is a list 2753 ** of tables in the parent pTo table. flags contains all 2754 ** information about the conflict resolution algorithms specified 2755 ** in the ON DELETE, ON UPDATE and ON INSERT clauses. 2756 ** 2757 ** An FKey structure is created and added to the table currently 2758 ** under construction in the pParse->pNewTable field. 2759 ** 2760 ** The foreign key is set for IMMEDIATE processing. A subsequent call 2761 ** to sqlite3DeferForeignKey() might change this to DEFERRED. 2762 */ 2763 void sqlite3CreateForeignKey( 2764 Parse *pParse, /* Parsing context */ 2765 ExprList *pFromCol, /* Columns in this table that point to other table */ 2766 Token *pTo, /* Name of the other table */ 2767 ExprList *pToCol, /* Columns in the other table */ 2768 int flags /* Conflict resolution algorithms. */ 2769 ){ 2770 sqlite3 *db = pParse->db; 2771 #ifndef SQLITE_OMIT_FOREIGN_KEY 2772 FKey *pFKey = 0; 2773 FKey *pNextTo; 2774 Table *p = pParse->pNewTable; 2775 int nByte; 2776 int i; 2777 int nCol; 2778 char *z; 2779 2780 assert( pTo!=0 ); 2781 if( p==0 || IN_DECLARE_VTAB ) goto fk_end; 2782 if( pFromCol==0 ){ 2783 int iCol = p->nCol-1; 2784 if( NEVER(iCol<0) ) goto fk_end; 2785 if( pToCol && pToCol->nExpr!=1 ){ 2786 sqlite3ErrorMsg(pParse, "foreign key on %s" 2787 " should reference only one column of table %T", 2788 p->aCol[iCol].zName, pTo); 2789 goto fk_end; 2790 } 2791 nCol = 1; 2792 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ 2793 sqlite3ErrorMsg(pParse, 2794 "number of columns in foreign key does not match the number of " 2795 "columns in the referenced table"); 2796 goto fk_end; 2797 }else{ 2798 nCol = pFromCol->nExpr; 2799 } 2800 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; 2801 if( pToCol ){ 2802 for(i=0; i<pToCol->nExpr; i++){ 2803 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1; 2804 } 2805 } 2806 pFKey = sqlite3DbMallocZero(db, nByte ); 2807 if( pFKey==0 ){ 2808 goto fk_end; 2809 } 2810 pFKey->pFrom = p; 2811 pFKey->pNextFrom = p->pFKey; 2812 z = (char*)&pFKey->aCol[nCol]; 2813 pFKey->zTo = z; 2814 if( IN_RENAME_OBJECT ){ 2815 sqlite3RenameTokenMap(pParse, (void*)z, pTo); 2816 } 2817 memcpy(z, pTo->z, pTo->n); 2818 z[pTo->n] = 0; 2819 sqlite3Dequote(z); 2820 z += pTo->n+1; 2821 pFKey->nCol = nCol; 2822 if( pFromCol==0 ){ 2823 pFKey->aCol[0].iFrom = p->nCol-1; 2824 }else{ 2825 for(i=0; i<nCol; i++){ 2826 int j; 2827 for(j=0; j<p->nCol; j++){ 2828 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ 2829 pFKey->aCol[i].iFrom = j; 2830 break; 2831 } 2832 } 2833 if( j>=p->nCol ){ 2834 sqlite3ErrorMsg(pParse, 2835 "unknown column \"%s\" in foreign key definition", 2836 pFromCol->a[i].zName); 2837 goto fk_end; 2838 } 2839 if( IN_RENAME_OBJECT ){ 2840 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName); 2841 } 2842 } 2843 } 2844 if( pToCol ){ 2845 for(i=0; i<nCol; i++){ 2846 int n = sqlite3Strlen30(pToCol->a[i].zName); 2847 pFKey->aCol[i].zCol = z; 2848 if( IN_RENAME_OBJECT ){ 2849 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName); 2850 } 2851 memcpy(z, pToCol->a[i].zName, n); 2852 z[n] = 0; 2853 z += n+1; 2854 } 2855 } 2856 pFKey->isDeferred = 0; 2857 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ 2858 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ 2859 2860 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); 2861 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 2862 pFKey->zTo, (void *)pFKey 2863 ); 2864 if( pNextTo==pFKey ){ 2865 sqlite3OomFault(db); 2866 goto fk_end; 2867 } 2868 if( pNextTo ){ 2869 assert( pNextTo->pPrevTo==0 ); 2870 pFKey->pNextTo = pNextTo; 2871 pNextTo->pPrevTo = pFKey; 2872 } 2873 2874 /* Link the foreign key to the table as the last step. 2875 */ 2876 p->pFKey = pFKey; 2877 pFKey = 0; 2878 2879 fk_end: 2880 sqlite3DbFree(db, pFKey); 2881 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ 2882 sqlite3ExprListDelete(db, pFromCol); 2883 sqlite3ExprListDelete(db, pToCol); 2884 } 2885 2886 /* 2887 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED 2888 ** clause is seen as part of a foreign key definition. The isDeferred 2889 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. 2890 ** The behavior of the most recently created foreign key is adjusted 2891 ** accordingly. 2892 */ 2893 void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ 2894 #ifndef SQLITE_OMIT_FOREIGN_KEY 2895 Table *pTab; 2896 FKey *pFKey; 2897 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; 2898 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ 2899 pFKey->isDeferred = (u8)isDeferred; 2900 #endif 2901 } 2902 2903 /* 2904 ** Generate code that will erase and refill index *pIdx. This is 2905 ** used to initialize a newly created index or to recompute the 2906 ** content of an index in response to a REINDEX command. 2907 ** 2908 ** if memRootPage is not negative, it means that the index is newly 2909 ** created. The register specified by memRootPage contains the 2910 ** root page number of the index. If memRootPage is negative, then 2911 ** the index already exists and must be cleared before being refilled and 2912 ** the root page number of the index is taken from pIndex->tnum. 2913 */ 2914 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ 2915 Table *pTab = pIndex->pTable; /* The table that is indexed */ 2916 int iTab = pParse->nTab++; /* Btree cursor used for pTab */ 2917 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ 2918 int iSorter; /* Cursor opened by OpenSorter (if in use) */ 2919 int addr1; /* Address of top of loop */ 2920 int addr2; /* Address to jump to for next iteration */ 2921 int tnum; /* Root page of index */ 2922 int iPartIdxLabel; /* Jump to this label to skip a row */ 2923 Vdbe *v; /* Generate code into this virtual machine */ 2924 KeyInfo *pKey; /* KeyInfo for index */ 2925 int regRecord; /* Register holding assembled index record */ 2926 sqlite3 *db = pParse->db; /* The database connection */ 2927 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); 2928 2929 #ifndef SQLITE_OMIT_AUTHORIZATION 2930 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, 2931 db->aDb[iDb].zDbSName ) ){ 2932 return; 2933 } 2934 #endif 2935 2936 /* Require a write-lock on the table to perform this operation */ 2937 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); 2938 2939 v = sqlite3GetVdbe(pParse); 2940 if( v==0 ) return; 2941 if( memRootPage>=0 ){ 2942 tnum = memRootPage; 2943 }else{ 2944 tnum = pIndex->tnum; 2945 } 2946 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); 2947 assert( pKey!=0 || db->mallocFailed || pParse->nErr ); 2948 2949 /* Open the sorter cursor if we are to use one. */ 2950 iSorter = pParse->nTab++; 2951 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) 2952 sqlite3KeyInfoRef(pKey), P4_KEYINFO); 2953 2954 /* Open the table. Loop through all rows of the table, inserting index 2955 ** records into the sorter. */ 2956 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 2957 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); 2958 regRecord = sqlite3GetTempReg(pParse); 2959 sqlite3MultiWrite(pParse); 2960 2961 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); 2962 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); 2963 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); 2964 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); 2965 sqlite3VdbeJumpHere(v, addr1); 2966 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); 2967 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 2968 (char *)pKey, P4_KEYINFO); 2969 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); 2970 2971 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); 2972 if( IsUniqueIndex(pIndex) ){ 2973 int j2 = sqlite3VdbeGoto(v, 1); 2974 addr2 = sqlite3VdbeCurrentAddr(v); 2975 sqlite3VdbeVerifyAbortable(v, OE_Abort); 2976 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, 2977 pIndex->nKeyCol); VdbeCoverage(v); 2978 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); 2979 sqlite3VdbeJumpHere(v, j2); 2980 }else{ 2981 addr2 = sqlite3VdbeCurrentAddr(v); 2982 } 2983 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); 2984 sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx); 2985 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); 2986 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); 2987 sqlite3ReleaseTempReg(pParse, regRecord); 2988 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); 2989 sqlite3VdbeJumpHere(v, addr1); 2990 2991 sqlite3VdbeAddOp1(v, OP_Close, iTab); 2992 sqlite3VdbeAddOp1(v, OP_Close, iIdx); 2993 sqlite3VdbeAddOp1(v, OP_Close, iSorter); 2994 } 2995 2996 /* 2997 ** Allocate heap space to hold an Index object with nCol columns. 2998 ** 2999 ** Increase the allocation size to provide an extra nExtra bytes 3000 ** of 8-byte aligned space after the Index object and return a 3001 ** pointer to this extra space in *ppExtra. 3002 */ 3003 Index *sqlite3AllocateIndexObject( 3004 sqlite3 *db, /* Database connection */ 3005 i16 nCol, /* Total number of columns in the index */ 3006 int nExtra, /* Number of bytes of extra space to alloc */ 3007 char **ppExtra /* Pointer to the "extra" space */ 3008 ){ 3009 Index *p; /* Allocated index object */ 3010 int nByte; /* Bytes of space for Index object + arrays */ 3011 3012 nByte = ROUND8(sizeof(Index)) + /* Index structure */ 3013 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ 3014 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ 3015 sizeof(i16)*nCol + /* Index.aiColumn */ 3016 sizeof(u8)*nCol); /* Index.aSortOrder */ 3017 p = sqlite3DbMallocZero(db, nByte + nExtra); 3018 if( p ){ 3019 char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); 3020 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); 3021 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); 3022 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; 3023 p->aSortOrder = (u8*)pExtra; 3024 p->nColumn = nCol; 3025 p->nKeyCol = nCol - 1; 3026 *ppExtra = ((char*)p) + nByte; 3027 } 3028 return p; 3029 } 3030 3031 /* 3032 ** Create a new index for an SQL table. pName1.pName2 is the name of the index 3033 ** and pTblList is the name of the table that is to be indexed. Both will 3034 ** be NULL for a primary key or an index that is created to satisfy a 3035 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable 3036 ** as the table to be indexed. pParse->pNewTable is a table that is 3037 ** currently being constructed by a CREATE TABLE statement. 3038 ** 3039 ** pList is a list of columns to be indexed. pList will be NULL if this 3040 ** is a primary key or unique-constraint on the most recent column added 3041 ** to the table currently under construction. 3042 */ 3043 void sqlite3CreateIndex( 3044 Parse *pParse, /* All information about this parse */ 3045 Token *pName1, /* First part of index name. May be NULL */ 3046 Token *pName2, /* Second part of index name. May be NULL */ 3047 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ 3048 ExprList *pList, /* A list of columns to be indexed */ 3049 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ 3050 Token *pStart, /* The CREATE token that begins this statement */ 3051 Expr *pPIWhere, /* WHERE clause for partial indices */ 3052 int sortOrder, /* Sort order of primary key when pList==NULL */ 3053 int ifNotExist, /* Omit error if index already exists */ 3054 u8 idxType /* The index type */ 3055 ){ 3056 Table *pTab = 0; /* Table to be indexed */ 3057 Index *pIndex = 0; /* The index to be created */ 3058 char *zName = 0; /* Name of the index */ 3059 int nName; /* Number of characters in zName */ 3060 int i, j; 3061 DbFixer sFix; /* For assigning database names to pTable */ 3062 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ 3063 sqlite3 *db = pParse->db; 3064 Db *pDb; /* The specific table containing the indexed database */ 3065 int iDb; /* Index of the database that is being written */ 3066 Token *pName = 0; /* Unqualified name of the index to create */ 3067 struct ExprList_item *pListItem; /* For looping over pList */ 3068 int nExtra = 0; /* Space allocated for zExtra[] */ 3069 int nExtraCol; /* Number of extra columns needed */ 3070 char *zExtra = 0; /* Extra space after the Index object */ 3071 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ 3072 3073 if( db->mallocFailed || pParse->nErr>0 ){ 3074 goto exit_create_index; 3075 } 3076 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ 3077 goto exit_create_index; 3078 } 3079 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 3080 goto exit_create_index; 3081 } 3082 3083 /* 3084 ** Find the table that is to be indexed. Return early if not found. 3085 */ 3086 if( pTblName!=0 ){ 3087 3088 /* Use the two-part index name to determine the database 3089 ** to search for the table. 'Fix' the table name to this db 3090 ** before looking up the table. 3091 */ 3092 assert( pName1 && pName2 ); 3093 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); 3094 if( iDb<0 ) goto exit_create_index; 3095 assert( pName && pName->z ); 3096 3097 #ifndef SQLITE_OMIT_TEMPDB 3098 /* If the index name was unqualified, check if the table 3099 ** is a temp table. If so, set the database to 1. Do not do this 3100 ** if initialising a database schema. 3101 */ 3102 if( !db->init.busy ){ 3103 pTab = sqlite3SrcListLookup(pParse, pTblName); 3104 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ 3105 iDb = 1; 3106 } 3107 } 3108 #endif 3109 3110 sqlite3FixInit(&sFix, pParse, iDb, "index", pName); 3111 if( sqlite3FixSrcList(&sFix, pTblName) ){ 3112 /* Because the parser constructs pTblName from a single identifier, 3113 ** sqlite3FixSrcList can never fail. */ 3114 assert(0); 3115 } 3116 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); 3117 assert( db->mallocFailed==0 || pTab==0 ); 3118 if( pTab==0 ) goto exit_create_index; 3119 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ 3120 sqlite3ErrorMsg(pParse, 3121 "cannot create a TEMP index on non-TEMP table \"%s\"", 3122 pTab->zName); 3123 goto exit_create_index; 3124 } 3125 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab); 3126 }else{ 3127 assert( pName==0 ); 3128 assert( pStart==0 ); 3129 pTab = pParse->pNewTable; 3130 if( !pTab ) goto exit_create_index; 3131 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 3132 } 3133 pDb = &db->aDb[iDb]; 3134 3135 assert( pTab!=0 ); 3136 assert( pParse->nErr==0 ); 3137 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 3138 && db->init.busy==0 3139 #if SQLITE_USER_AUTHENTICATION 3140 && sqlite3UserAuthTable(pTab->zName)==0 3141 #endif 3142 #ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX 3143 && sqlite3StrICmp(&pTab->zName[7],"master")!=0 3144 #endif 3145 && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 3146 ){ 3147 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); 3148 goto exit_create_index; 3149 } 3150 #ifndef SQLITE_OMIT_VIEW 3151 if( pTab->pSelect ){ 3152 sqlite3ErrorMsg(pParse, "views may not be indexed"); 3153 goto exit_create_index; 3154 } 3155 #endif 3156 #ifndef SQLITE_OMIT_VIRTUALTABLE 3157 if( IsVirtual(pTab) ){ 3158 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); 3159 goto exit_create_index; 3160 } 3161 #endif 3162 3163 /* 3164 ** Find the name of the index. Make sure there is not already another 3165 ** index or table with the same name. 3166 ** 3167 ** Exception: If we are reading the names of permanent indices from the 3168 ** sqlite_master table (because some other process changed the schema) and 3169 ** one of the index names collides with the name of a temporary table or 3170 ** index, then we will continue to process this index. 3171 ** 3172 ** If pName==0 it means that we are 3173 ** dealing with a primary key or UNIQUE constraint. We have to invent our 3174 ** own name. 3175 */ 3176 if( pName ){ 3177 zName = sqlite3NameFromToken(db, pName); 3178 if( zName==0 ) goto exit_create_index; 3179 assert( pName->z!=0 ); 3180 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ 3181 goto exit_create_index; 3182 } 3183 if( !IN_RENAME_OBJECT ){ 3184 if( !db->init.busy ){ 3185 if( sqlite3FindTable(db, zName, 0)!=0 ){ 3186 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); 3187 goto exit_create_index; 3188 } 3189 } 3190 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ 3191 if( !ifNotExist ){ 3192 sqlite3ErrorMsg(pParse, "index %s already exists", zName); 3193 }else{ 3194 assert( !db->init.busy ); 3195 sqlite3CodeVerifySchema(pParse, iDb); 3196 } 3197 goto exit_create_index; 3198 } 3199 } 3200 }else{ 3201 int n; 3202 Index *pLoop; 3203 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} 3204 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); 3205 if( zName==0 ){ 3206 goto exit_create_index; 3207 } 3208 3209 /* Automatic index names generated from within sqlite3_declare_vtab() 3210 ** must have names that are distinct from normal automatic index names. 3211 ** The following statement converts "sqlite3_autoindex..." into 3212 ** "sqlite3_butoindex..." in order to make the names distinct. 3213 ** The "vtab_err.test" test demonstrates the need of this statement. */ 3214 if( IN_SPECIAL_PARSE ) zName[7]++; 3215 } 3216 3217 /* Check for authorization to create an index. 3218 */ 3219 #ifndef SQLITE_OMIT_AUTHORIZATION 3220 if( !IN_RENAME_OBJECT ){ 3221 const char *zDb = pDb->zDbSName; 3222 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ 3223 goto exit_create_index; 3224 } 3225 i = SQLITE_CREATE_INDEX; 3226 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; 3227 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ 3228 goto exit_create_index; 3229 } 3230 } 3231 #endif 3232 3233 /* If pList==0, it means this routine was called to make a primary 3234 ** key out of the last column added to the table under construction. 3235 ** So create a fake list to simulate this. 3236 */ 3237 if( pList==0 ){ 3238 Token prevCol; 3239 Column *pCol = &pTab->aCol[pTab->nCol-1]; 3240 pCol->colFlags |= COLFLAG_UNIQUE; 3241 sqlite3TokenInit(&prevCol, pCol->zName); 3242 pList = sqlite3ExprListAppend(pParse, 0, 3243 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); 3244 if( pList==0 ) goto exit_create_index; 3245 assert( pList->nExpr==1 ); 3246 sqlite3ExprListSetSortOrder(pList, sortOrder); 3247 }else{ 3248 sqlite3ExprListCheckLength(pParse, pList, "index"); 3249 } 3250 3251 /* Figure out how many bytes of space are required to store explicitly 3252 ** specified collation sequence names. 3253 */ 3254 for(i=0; i<pList->nExpr; i++){ 3255 Expr *pExpr = pList->a[i].pExpr; 3256 assert( pExpr!=0 ); 3257 if( pExpr->op==TK_COLLATE ){ 3258 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); 3259 } 3260 } 3261 3262 /* 3263 ** Allocate the index structure. 3264 */ 3265 nName = sqlite3Strlen30(zName); 3266 nExtraCol = pPk ? pPk->nKeyCol : 1; 3267 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, 3268 nName + nExtra + 1, &zExtra); 3269 if( db->mallocFailed ){ 3270 goto exit_create_index; 3271 } 3272 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); 3273 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); 3274 pIndex->zName = zExtra; 3275 zExtra += nName + 1; 3276 memcpy(pIndex->zName, zName, nName+1); 3277 pIndex->pTable = pTab; 3278 pIndex->onError = (u8)onError; 3279 pIndex->uniqNotNull = onError!=OE_None; 3280 pIndex->idxType = idxType; 3281 pIndex->pSchema = db->aDb[iDb].pSchema; 3282 pIndex->nKeyCol = pList->nExpr; 3283 if( pPIWhere ){ 3284 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); 3285 pIndex->pPartIdxWhere = pPIWhere; 3286 pPIWhere = 0; 3287 } 3288 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 3289 3290 /* Check to see if we should honor DESC requests on index columns 3291 */ 3292 if( pDb->pSchema->file_format>=4 ){ 3293 sortOrderMask = -1; /* Honor DESC */ 3294 }else{ 3295 sortOrderMask = 0; /* Ignore DESC */ 3296 } 3297 3298 /* Analyze the list of expressions that form the terms of the index and 3299 ** report any errors. In the common case where the expression is exactly 3300 ** a table column, store that column in aiColumn[]. For general expressions, 3301 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. 3302 ** 3303 ** TODO: Issue a warning if two or more columns of the index are identical. 3304 ** TODO: Issue a warning if the table primary key is used as part of the 3305 ** index key. 3306 */ 3307 pListItem = pList->a; 3308 if( IN_RENAME_OBJECT ){ 3309 pIndex->aColExpr = pList; 3310 pList = 0; 3311 } 3312 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){ 3313 Expr *pCExpr; /* The i-th index expression */ 3314 int requestedSortOrder; /* ASC or DESC on the i-th expression */ 3315 const char *zColl; /* Collation sequence name */ 3316 3317 sqlite3StringToId(pListItem->pExpr); 3318 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); 3319 if( pParse->nErr ) goto exit_create_index; 3320 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); 3321 if( pCExpr->op!=TK_COLUMN ){ 3322 if( pTab==pParse->pNewTable ){ 3323 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " 3324 "UNIQUE constraints"); 3325 goto exit_create_index; 3326 } 3327 if( pIndex->aColExpr==0 ){ 3328 pIndex->aColExpr = pList; 3329 pList = 0; 3330 } 3331 j = XN_EXPR; 3332 pIndex->aiColumn[i] = XN_EXPR; 3333 pIndex->uniqNotNull = 0; 3334 }else{ 3335 j = pCExpr->iColumn; 3336 assert( j<=0x7fff ); 3337 if( j<0 ){ 3338 j = pTab->iPKey; 3339 }else if( pTab->aCol[j].notNull==0 ){ 3340 pIndex->uniqNotNull = 0; 3341 } 3342 pIndex->aiColumn[i] = (i16)j; 3343 } 3344 zColl = 0; 3345 if( pListItem->pExpr->op==TK_COLLATE ){ 3346 int nColl; 3347 zColl = pListItem->pExpr->u.zToken; 3348 nColl = sqlite3Strlen30(zColl) + 1; 3349 assert( nExtra>=nColl ); 3350 memcpy(zExtra, zColl, nColl); 3351 zColl = zExtra; 3352 zExtra += nColl; 3353 nExtra -= nColl; 3354 }else if( j>=0 ){ 3355 zColl = pTab->aCol[j].zColl; 3356 } 3357 if( !zColl ) zColl = sqlite3StrBINARY; 3358 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ 3359 goto exit_create_index; 3360 } 3361 pIndex->azColl[i] = zColl; 3362 requestedSortOrder = pListItem->sortOrder & sortOrderMask; 3363 pIndex->aSortOrder[i] = (u8)requestedSortOrder; 3364 } 3365 3366 /* Append the table key to the end of the index. For WITHOUT ROWID 3367 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For 3368 ** normal tables (when pPk==0) this will be the rowid. 3369 */ 3370 if( pPk ){ 3371 for(j=0; j<pPk->nKeyCol; j++){ 3372 int x = pPk->aiColumn[j]; 3373 assert( x>=0 ); 3374 if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){ 3375 pIndex->nColumn--; 3376 }else{ 3377 pIndex->aiColumn[i] = x; 3378 pIndex->azColl[i] = pPk->azColl[j]; 3379 pIndex->aSortOrder[i] = pPk->aSortOrder[j]; 3380 i++; 3381 } 3382 } 3383 assert( i==pIndex->nColumn ); 3384 }else{ 3385 pIndex->aiColumn[i] = XN_ROWID; 3386 pIndex->azColl[i] = sqlite3StrBINARY; 3387 } 3388 sqlite3DefaultRowEst(pIndex); 3389 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); 3390 3391 /* If this index contains every column of its table, then mark 3392 ** it as a covering index */ 3393 assert( HasRowid(pTab) 3394 || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 ); 3395 recomputeColumnsNotIndexed(pIndex); 3396 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ 3397 pIndex->isCovering = 1; 3398 for(j=0; j<pTab->nCol; j++){ 3399 if( j==pTab->iPKey ) continue; 3400 if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue; 3401 pIndex->isCovering = 0; 3402 break; 3403 } 3404 } 3405 3406 if( pTab==pParse->pNewTable ){ 3407 /* This routine has been called to create an automatic index as a 3408 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or 3409 ** a PRIMARY KEY or UNIQUE clause following the column definitions. 3410 ** i.e. one of: 3411 ** 3412 ** CREATE TABLE t(x PRIMARY KEY, y); 3413 ** CREATE TABLE t(x, y, UNIQUE(x, y)); 3414 ** 3415 ** Either way, check to see if the table already has such an index. If 3416 ** so, don't bother creating this one. This only applies to 3417 ** automatically created indices. Users can do as they wish with 3418 ** explicit indices. 3419 ** 3420 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent 3421 ** (and thus suppressing the second one) even if they have different 3422 ** sort orders. 3423 ** 3424 ** If there are different collating sequences or if the columns of 3425 ** the constraint occur in different orders, then the constraints are 3426 ** considered distinct and both result in separate indices. 3427 */ 3428 Index *pIdx; 3429 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 3430 int k; 3431 assert( IsUniqueIndex(pIdx) ); 3432 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); 3433 assert( IsUniqueIndex(pIndex) ); 3434 3435 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; 3436 for(k=0; k<pIdx->nKeyCol; k++){ 3437 const char *z1; 3438 const char *z2; 3439 assert( pIdx->aiColumn[k]>=0 ); 3440 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; 3441 z1 = pIdx->azColl[k]; 3442 z2 = pIndex->azColl[k]; 3443 if( sqlite3StrICmp(z1, z2) ) break; 3444 } 3445 if( k==pIdx->nKeyCol ){ 3446 if( pIdx->onError!=pIndex->onError ){ 3447 /* This constraint creates the same index as a previous 3448 ** constraint specified somewhere in the CREATE TABLE statement. 3449 ** However the ON CONFLICT clauses are different. If both this 3450 ** constraint and the previous equivalent constraint have explicit 3451 ** ON CONFLICT clauses this is an error. Otherwise, use the 3452 ** explicitly specified behavior for the index. 3453 */ 3454 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ 3455 sqlite3ErrorMsg(pParse, 3456 "conflicting ON CONFLICT clauses specified", 0); 3457 } 3458 if( pIdx->onError==OE_Default ){ 3459 pIdx->onError = pIndex->onError; 3460 } 3461 } 3462 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; 3463 goto exit_create_index; 3464 } 3465 } 3466 } 3467 3468 if( !IN_RENAME_OBJECT ){ 3469 3470 /* Link the new Index structure to its table and to the other 3471 ** in-memory database structures. 3472 */ 3473 assert( pParse->nErr==0 ); 3474 if( db->init.busy ){ 3475 Index *p; 3476 assert( !IN_SPECIAL_PARSE ); 3477 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); 3478 p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 3479 pIndex->zName, pIndex); 3480 if( p ){ 3481 assert( p==pIndex ); /* Malloc must have failed */ 3482 sqlite3OomFault(db); 3483 goto exit_create_index; 3484 } 3485 db->mDbFlags |= DBFLAG_SchemaChange; 3486 if( pTblName!=0 ){ 3487 pIndex->tnum = db->init.newTnum; 3488 } 3489 } 3490 3491 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the 3492 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then 3493 ** emit code to allocate the index rootpage on disk and make an entry for 3494 ** the index in the sqlite_master table and populate the index with 3495 ** content. But, do not do this if we are simply reading the sqlite_master 3496 ** table to parse the schema, or if this index is the PRIMARY KEY index 3497 ** of a WITHOUT ROWID table. 3498 ** 3499 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY 3500 ** or UNIQUE index in a CREATE TABLE statement. Since the table 3501 ** has just been created, it contains no data and the index initialization 3502 ** step can be skipped. 3503 */ 3504 else if( HasRowid(pTab) || pTblName!=0 ){ 3505 Vdbe *v; 3506 char *zStmt; 3507 int iMem = ++pParse->nMem; 3508 3509 v = sqlite3GetVdbe(pParse); 3510 if( v==0 ) goto exit_create_index; 3511 3512 sqlite3BeginWriteOperation(pParse, 1, iDb); 3513 3514 /* Create the rootpage for the index using CreateIndex. But before 3515 ** doing so, code a Noop instruction and store its address in 3516 ** Index.tnum. This is required in case this index is actually a 3517 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 3518 ** that case the convertToWithoutRowidTable() routine will replace 3519 ** the Noop with a Goto to jump over the VDBE code generated below. */ 3520 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop); 3521 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY); 3522 3523 /* Gather the complete text of the CREATE INDEX statement into 3524 ** the zStmt variable 3525 */ 3526 if( pStart ){ 3527 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; 3528 if( pName->z[n-1]==';' ) n--; 3529 /* A named index with an explicit CREATE INDEX statement */ 3530 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", 3531 onError==OE_None ? "" : " UNIQUE", n, pName->z); 3532 }else{ 3533 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ 3534 /* zStmt = sqlite3MPrintf(""); */ 3535 zStmt = 0; 3536 } 3537 3538 /* Add an entry in sqlite_master for this index 3539 */ 3540 sqlite3NestedParse(pParse, 3541 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", 3542 db->aDb[iDb].zDbSName, MASTER_NAME, 3543 pIndex->zName, 3544 pTab->zName, 3545 iMem, 3546 zStmt 3547 ); 3548 sqlite3DbFree(db, zStmt); 3549 3550 /* Fill the index with data and reparse the schema. Code an OP_Expire 3551 ** to invalidate all pre-compiled statements. 3552 */ 3553 if( pTblName ){ 3554 sqlite3RefillIndex(pParse, pIndex, iMem); 3555 sqlite3ChangeCookie(pParse, iDb); 3556 sqlite3VdbeAddParseSchemaOp(v, iDb, 3557 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); 3558 sqlite3VdbeAddOp2(v, OP_Expire, 0, 1); 3559 } 3560 3561 sqlite3VdbeJumpHere(v, pIndex->tnum); 3562 } 3563 } 3564 3565 /* When adding an index to the list of indices for a table, make 3566 ** sure all indices labeled OE_Replace come after all those labeled 3567 ** OE_Ignore. This is necessary for the correct constraint check 3568 ** processing (in sqlite3GenerateConstraintChecks()) as part of 3569 ** UPDATE and INSERT statements. 3570 */ 3571 if( db->init.busy || pTblName==0 ){ 3572 if( onError!=OE_Replace || pTab->pIndex==0 3573 || pTab->pIndex->onError==OE_Replace){ 3574 pIndex->pNext = pTab->pIndex; 3575 pTab->pIndex = pIndex; 3576 }else{ 3577 Index *pOther = pTab->pIndex; 3578 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ 3579 pOther = pOther->pNext; 3580 } 3581 pIndex->pNext = pOther->pNext; 3582 pOther->pNext = pIndex; 3583 } 3584 pIndex = 0; 3585 } 3586 else if( IN_RENAME_OBJECT ){ 3587 assert( pParse->pNewIndex==0 ); 3588 pParse->pNewIndex = pIndex; 3589 pIndex = 0; 3590 } 3591 3592 /* Clean up before exiting */ 3593 exit_create_index: 3594 if( pIndex ) sqlite3FreeIndex(db, pIndex); 3595 sqlite3ExprDelete(db, pPIWhere); 3596 sqlite3ExprListDelete(db, pList); 3597 sqlite3SrcListDelete(db, pTblName); 3598 sqlite3DbFree(db, zName); 3599 } 3600 3601 /* 3602 ** Fill the Index.aiRowEst[] array with default information - information 3603 ** to be used when we have not run the ANALYZE command. 3604 ** 3605 ** aiRowEst[0] is supposed to contain the number of elements in the index. 3606 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the 3607 ** number of rows in the table that match any particular value of the 3608 ** first column of the index. aiRowEst[2] is an estimate of the number 3609 ** of rows that match any particular combination of the first 2 columns 3610 ** of the index. And so forth. It must always be the case that 3611 * 3612 ** aiRowEst[N]<=aiRowEst[N-1] 3613 ** aiRowEst[N]>=1 3614 ** 3615 ** Apart from that, we have little to go on besides intuition as to 3616 ** how aiRowEst[] should be initialized. The numbers generated here 3617 ** are based on typical values found in actual indices. 3618 */ 3619 void sqlite3DefaultRowEst(Index *pIdx){ 3620 /* 10, 9, 8, 7, 6 */ 3621 LogEst aVal[] = { 33, 32, 30, 28, 26 }; 3622 LogEst *a = pIdx->aiRowLogEst; 3623 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); 3624 int i; 3625 3626 /* Indexes with default row estimates should not have stat1 data */ 3627 assert( !pIdx->hasStat1 ); 3628 3629 /* Set the first entry (number of rows in the index) to the estimated 3630 ** number of rows in the table, or half the number of rows in the table 3631 ** for a partial index. But do not let the estimate drop below 10. */ 3632 a[0] = pIdx->pTable->nRowLogEst; 3633 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) ); 3634 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) ); 3635 3636 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is 3637 ** 6 and each subsequent value (if any) is 5. */ 3638 memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); 3639 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ 3640 a[i] = 23; assert( 23==sqlite3LogEst(5) ); 3641 } 3642 3643 assert( 0==sqlite3LogEst(1) ); 3644 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; 3645 } 3646 3647 /* 3648 ** This routine will drop an existing named index. This routine 3649 ** implements the DROP INDEX statement. 3650 */ 3651 void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ 3652 Index *pIndex; 3653 Vdbe *v; 3654 sqlite3 *db = pParse->db; 3655 int iDb; 3656 3657 assert( pParse->nErr==0 ); /* Never called with prior errors */ 3658 if( db->mallocFailed ){ 3659 goto exit_drop_index; 3660 } 3661 assert( pName->nSrc==1 ); 3662 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 3663 goto exit_drop_index; 3664 } 3665 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); 3666 if( pIndex==0 ){ 3667 if( !ifExists ){ 3668 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0); 3669 }else{ 3670 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); 3671 } 3672 pParse->checkSchema = 1; 3673 goto exit_drop_index; 3674 } 3675 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ 3676 sqlite3ErrorMsg(pParse, "index associated with UNIQUE " 3677 "or PRIMARY KEY constraint cannot be dropped", 0); 3678 goto exit_drop_index; 3679 } 3680 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); 3681 #ifndef SQLITE_OMIT_AUTHORIZATION 3682 { 3683 int code = SQLITE_DROP_INDEX; 3684 Table *pTab = pIndex->pTable; 3685 const char *zDb = db->aDb[iDb].zDbSName; 3686 const char *zTab = SCHEMA_TABLE(iDb); 3687 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ 3688 goto exit_drop_index; 3689 } 3690 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; 3691 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ 3692 goto exit_drop_index; 3693 } 3694 } 3695 #endif 3696 3697 /* Generate code to remove the index and from the master table */ 3698 v = sqlite3GetVdbe(pParse); 3699 if( v ){ 3700 sqlite3BeginWriteOperation(pParse, 1, iDb); 3701 sqlite3NestedParse(pParse, 3702 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", 3703 db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName 3704 ); 3705 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); 3706 sqlite3ChangeCookie(pParse, iDb); 3707 destroyRootPage(pParse, pIndex->tnum, iDb); 3708 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); 3709 } 3710 3711 exit_drop_index: 3712 sqlite3SrcListDelete(db, pName); 3713 } 3714 3715 /* 3716 ** pArray is a pointer to an array of objects. Each object in the 3717 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() 3718 ** to extend the array so that there is space for a new object at the end. 3719 ** 3720 ** When this function is called, *pnEntry contains the current size of 3721 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes 3722 ** in total). 3723 ** 3724 ** If the realloc() is successful (i.e. if no OOM condition occurs), the 3725 ** space allocated for the new object is zeroed, *pnEntry updated to 3726 ** reflect the new size of the array and a pointer to the new allocation 3727 ** returned. *pIdx is set to the index of the new array entry in this case. 3728 ** 3729 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains 3730 ** unchanged and a copy of pArray returned. 3731 */ 3732 void *sqlite3ArrayAllocate( 3733 sqlite3 *db, /* Connection to notify of malloc failures */ 3734 void *pArray, /* Array of objects. Might be reallocated */ 3735 int szEntry, /* Size of each object in the array */ 3736 int *pnEntry, /* Number of objects currently in use */ 3737 int *pIdx /* Write the index of a new slot here */ 3738 ){ 3739 char *z; 3740 int n = *pnEntry; 3741 if( (n & (n-1))==0 ){ 3742 int sz = (n==0) ? 1 : 2*n; 3743 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); 3744 if( pNew==0 ){ 3745 *pIdx = -1; 3746 return pArray; 3747 } 3748 pArray = pNew; 3749 } 3750 z = (char*)pArray; 3751 memset(&z[n * szEntry], 0, szEntry); 3752 *pIdx = n; 3753 ++*pnEntry; 3754 return pArray; 3755 } 3756 3757 /* 3758 ** Append a new element to the given IdList. Create a new IdList if 3759 ** need be. 3760 ** 3761 ** A new IdList is returned, or NULL if malloc() fails. 3762 */ 3763 IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){ 3764 sqlite3 *db = pParse->db; 3765 int i; 3766 if( pList==0 ){ 3767 pList = sqlite3DbMallocZero(db, sizeof(IdList) ); 3768 if( pList==0 ) return 0; 3769 } 3770 pList->a = sqlite3ArrayAllocate( 3771 db, 3772 pList->a, 3773 sizeof(pList->a[0]), 3774 &pList->nId, 3775 &i 3776 ); 3777 if( i<0 ){ 3778 sqlite3IdListDelete(db, pList); 3779 return 0; 3780 } 3781 pList->a[i].zName = sqlite3NameFromToken(db, pToken); 3782 if( IN_RENAME_OBJECT && pList->a[i].zName ){ 3783 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken); 3784 } 3785 return pList; 3786 } 3787 3788 /* 3789 ** Delete an IdList. 3790 */ 3791 void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ 3792 int i; 3793 if( pList==0 ) return; 3794 for(i=0; i<pList->nId; i++){ 3795 sqlite3DbFree(db, pList->a[i].zName); 3796 } 3797 sqlite3DbFree(db, pList->a); 3798 sqlite3DbFreeNN(db, pList); 3799 } 3800 3801 /* 3802 ** Return the index in pList of the identifier named zId. Return -1 3803 ** if not found. 3804 */ 3805 int sqlite3IdListIndex(IdList *pList, const char *zName){ 3806 int i; 3807 if( pList==0 ) return -1; 3808 for(i=0; i<pList->nId; i++){ 3809 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; 3810 } 3811 return -1; 3812 } 3813 3814 /* 3815 ** Expand the space allocated for the given SrcList object by 3816 ** creating nExtra new slots beginning at iStart. iStart is zero based. 3817 ** New slots are zeroed. 3818 ** 3819 ** For example, suppose a SrcList initially contains two entries: A,B. 3820 ** To append 3 new entries onto the end, do this: 3821 ** 3822 ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); 3823 ** 3824 ** After the call above it would contain: A, B, nil, nil, nil. 3825 ** If the iStart argument had been 1 instead of 2, then the result 3826 ** would have been: A, nil, nil, nil, B. To prepend the new slots, 3827 ** the iStart value would be 0. The result then would 3828 ** be: nil, nil, nil, A, B. 3829 ** 3830 ** If a memory allocation fails the SrcList is unchanged. The 3831 ** db->mallocFailed flag will be set to true. 3832 */ 3833 SrcList *sqlite3SrcListEnlarge( 3834 sqlite3 *db, /* Database connection to notify of OOM errors */ 3835 SrcList *pSrc, /* The SrcList to be enlarged */ 3836 int nExtra, /* Number of new slots to add to pSrc->a[] */ 3837 int iStart /* Index in pSrc->a[] of first new slot */ 3838 ){ 3839 int i; 3840 3841 /* Sanity checking on calling parameters */ 3842 assert( iStart>=0 ); 3843 assert( nExtra>=1 ); 3844 assert( pSrc!=0 ); 3845 assert( iStart<=pSrc->nSrc ); 3846 3847 /* Allocate additional space if needed */ 3848 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ 3849 SrcList *pNew; 3850 int nAlloc = pSrc->nSrc*2+nExtra; 3851 int nGot; 3852 pNew = sqlite3DbRealloc(db, pSrc, 3853 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); 3854 if( pNew==0 ){ 3855 assert( db->mallocFailed ); 3856 return pSrc; 3857 } 3858 pSrc = pNew; 3859 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; 3860 pSrc->nAlloc = nGot; 3861 } 3862 3863 /* Move existing slots that come after the newly inserted slots 3864 ** out of the way */ 3865 for(i=pSrc->nSrc-1; i>=iStart; i--){ 3866 pSrc->a[i+nExtra] = pSrc->a[i]; 3867 } 3868 pSrc->nSrc += nExtra; 3869 3870 /* Zero the newly allocated slots */ 3871 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); 3872 for(i=iStart; i<iStart+nExtra; i++){ 3873 pSrc->a[i].iCursor = -1; 3874 } 3875 3876 /* Return a pointer to the enlarged SrcList */ 3877 return pSrc; 3878 } 3879 3880 3881 /* 3882 ** Append a new table name to the given SrcList. Create a new SrcList if 3883 ** need be. A new entry is created in the SrcList even if pTable is NULL. 3884 ** 3885 ** A SrcList is returned, or NULL if there is an OOM error. The returned 3886 ** SrcList might be the same as the SrcList that was input or it might be 3887 ** a new one. If an OOM error does occurs, then the prior value of pList 3888 ** that is input to this routine is automatically freed. 3889 ** 3890 ** If pDatabase is not null, it means that the table has an optional 3891 ** database name prefix. Like this: "database.table". The pDatabase 3892 ** points to the table name and the pTable points to the database name. 3893 ** The SrcList.a[].zName field is filled with the table name which might 3894 ** come from pTable (if pDatabase is NULL) or from pDatabase. 3895 ** SrcList.a[].zDatabase is filled with the database name from pTable, 3896 ** or with NULL if no database is specified. 3897 ** 3898 ** In other words, if call like this: 3899 ** 3900 ** sqlite3SrcListAppend(D,A,B,0); 3901 ** 3902 ** Then B is a table name and the database name is unspecified. If called 3903 ** like this: 3904 ** 3905 ** sqlite3SrcListAppend(D,A,B,C); 3906 ** 3907 ** Then C is the table name and B is the database name. If C is defined 3908 ** then so is B. In other words, we never have a case where: 3909 ** 3910 ** sqlite3SrcListAppend(D,A,0,C); 3911 ** 3912 ** Both pTable and pDatabase are assumed to be quoted. They are dequoted 3913 ** before being added to the SrcList. 3914 */ 3915 SrcList *sqlite3SrcListAppend( 3916 sqlite3 *db, /* Connection to notify of malloc failures */ 3917 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ 3918 Token *pTable, /* Table to append */ 3919 Token *pDatabase /* Database of the table */ 3920 ){ 3921 struct SrcList_item *pItem; 3922 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ 3923 assert( db!=0 ); 3924 if( pList==0 ){ 3925 pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) ); 3926 if( pList==0 ) return 0; 3927 pList->nAlloc = 1; 3928 pList->nSrc = 1; 3929 memset(&pList->a[0], 0, sizeof(pList->a[0])); 3930 pList->a[0].iCursor = -1; 3931 }else{ 3932 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc); 3933 } 3934 if( db->mallocFailed ){ 3935 sqlite3SrcListDelete(db, pList); 3936 return 0; 3937 } 3938 pItem = &pList->a[pList->nSrc-1]; 3939 if( pDatabase && pDatabase->z==0 ){ 3940 pDatabase = 0; 3941 } 3942 if( pDatabase ){ 3943 pItem->zName = sqlite3NameFromToken(db, pDatabase); 3944 pItem->zDatabase = sqlite3NameFromToken(db, pTable); 3945 }else{ 3946 pItem->zName = sqlite3NameFromToken(db, pTable); 3947 pItem->zDatabase = 0; 3948 } 3949 return pList; 3950 } 3951 3952 /* 3953 ** Assign VdbeCursor index numbers to all tables in a SrcList 3954 */ 3955 void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ 3956 int i; 3957 struct SrcList_item *pItem; 3958 assert(pList || pParse->db->mallocFailed ); 3959 if( pList ){ 3960 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ 3961 if( pItem->iCursor>=0 ) break; 3962 pItem->iCursor = pParse->nTab++; 3963 if( pItem->pSelect ){ 3964 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); 3965 } 3966 } 3967 } 3968 } 3969 3970 /* 3971 ** Delete an entire SrcList including all its substructure. 3972 */ 3973 void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ 3974 int i; 3975 struct SrcList_item *pItem; 3976 if( pList==0 ) return; 3977 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ 3978 sqlite3DbFree(db, pItem->zDatabase); 3979 sqlite3DbFree(db, pItem->zName); 3980 sqlite3DbFree(db, pItem->zAlias); 3981 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); 3982 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); 3983 sqlite3DeleteTable(db, pItem->pTab); 3984 sqlite3SelectDelete(db, pItem->pSelect); 3985 sqlite3ExprDelete(db, pItem->pOn); 3986 sqlite3IdListDelete(db, pItem->pUsing); 3987 } 3988 sqlite3DbFreeNN(db, pList); 3989 } 3990 3991 /* 3992 ** This routine is called by the parser to add a new term to the 3993 ** end of a growing FROM clause. The "p" parameter is the part of 3994 ** the FROM clause that has already been constructed. "p" is NULL 3995 ** if this is the first term of the FROM clause. pTable and pDatabase 3996 ** are the name of the table and database named in the FROM clause term. 3997 ** pDatabase is NULL if the database name qualifier is missing - the 3998 ** usual case. If the term has an alias, then pAlias points to the 3999 ** alias token. If the term is a subquery, then pSubquery is the 4000 ** SELECT statement that the subquery encodes. The pTable and 4001 ** pDatabase parameters are NULL for subqueries. The pOn and pUsing 4002 ** parameters are the content of the ON and USING clauses. 4003 ** 4004 ** Return a new SrcList which encodes is the FROM with the new 4005 ** term added. 4006 */ 4007 SrcList *sqlite3SrcListAppendFromTerm( 4008 Parse *pParse, /* Parsing context */ 4009 SrcList *p, /* The left part of the FROM clause already seen */ 4010 Token *pTable, /* Name of the table to add to the FROM clause */ 4011 Token *pDatabase, /* Name of the database containing pTable */ 4012 Token *pAlias, /* The right-hand side of the AS subexpression */ 4013 Select *pSubquery, /* A subquery used in place of a table name */ 4014 Expr *pOn, /* The ON clause of a join */ 4015 IdList *pUsing /* The USING clause of a join */ 4016 ){ 4017 struct SrcList_item *pItem; 4018 sqlite3 *db = pParse->db; 4019 if( !p && (pOn || pUsing) ){ 4020 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 4021 (pOn ? "ON" : "USING") 4022 ); 4023 goto append_from_error; 4024 } 4025 p = sqlite3SrcListAppend(db, p, pTable, pDatabase); 4026 if( p==0 ){ 4027 goto append_from_error; 4028 } 4029 assert( p->nSrc>0 ); 4030 pItem = &p->a[p->nSrc-1]; 4031 assert( (pTable==0)==(pDatabase==0) ); 4032 assert( pItem->zName==0 || pDatabase!=0 ); 4033 if( IN_RENAME_OBJECT && pItem->zName ){ 4034 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable; 4035 sqlite3RenameTokenMap(pParse, pItem->zName, pToken); 4036 } 4037 assert( pAlias!=0 ); 4038 if( pAlias->n ){ 4039 pItem->zAlias = sqlite3NameFromToken(db, pAlias); 4040 } 4041 pItem->pSelect = pSubquery; 4042 pItem->pOn = pOn; 4043 pItem->pUsing = pUsing; 4044 return p; 4045 4046 append_from_error: 4047 assert( p==0 ); 4048 sqlite3ExprDelete(db, pOn); 4049 sqlite3IdListDelete(db, pUsing); 4050 sqlite3SelectDelete(db, pSubquery); 4051 return 0; 4052 } 4053 4054 /* 4055 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 4056 ** element of the source-list passed as the second argument. 4057 */ 4058 void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ 4059 assert( pIndexedBy!=0 ); 4060 if( p && pIndexedBy->n>0 ){ 4061 struct SrcList_item *pItem; 4062 assert( p->nSrc>0 ); 4063 pItem = &p->a[p->nSrc-1]; 4064 assert( pItem->fg.notIndexed==0 ); 4065 assert( pItem->fg.isIndexedBy==0 ); 4066 assert( pItem->fg.isTabFunc==0 ); 4067 if( pIndexedBy->n==1 && !pIndexedBy->z ){ 4068 /* A "NOT INDEXED" clause was supplied. See parse.y 4069 ** construct "indexed_opt" for details. */ 4070 pItem->fg.notIndexed = 1; 4071 }else{ 4072 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); 4073 pItem->fg.isIndexedBy = 1; 4074 } 4075 } 4076 } 4077 4078 /* 4079 ** Add the list of function arguments to the SrcList entry for a 4080 ** table-valued-function. 4081 */ 4082 void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ 4083 if( p ){ 4084 struct SrcList_item *pItem = &p->a[p->nSrc-1]; 4085 assert( pItem->fg.notIndexed==0 ); 4086 assert( pItem->fg.isIndexedBy==0 ); 4087 assert( pItem->fg.isTabFunc==0 ); 4088 pItem->u1.pFuncArg = pList; 4089 pItem->fg.isTabFunc = 1; 4090 }else{ 4091 sqlite3ExprListDelete(pParse->db, pList); 4092 } 4093 } 4094 4095 /* 4096 ** When building up a FROM clause in the parser, the join operator 4097 ** is initially attached to the left operand. But the code generator 4098 ** expects the join operator to be on the right operand. This routine 4099 ** Shifts all join operators from left to right for an entire FROM 4100 ** clause. 4101 ** 4102 ** Example: Suppose the join is like this: 4103 ** 4104 ** A natural cross join B 4105 ** 4106 ** The operator is "natural cross join". The A and B operands are stored 4107 ** in p->a[0] and p->a[1], respectively. The parser initially stores the 4108 ** operator with A. This routine shifts that operator over to B. 4109 */ 4110 void sqlite3SrcListShiftJoinType(SrcList *p){ 4111 if( p ){ 4112 int i; 4113 for(i=p->nSrc-1; i>0; i--){ 4114 p->a[i].fg.jointype = p->a[i-1].fg.jointype; 4115 } 4116 p->a[0].fg.jointype = 0; 4117 } 4118 } 4119 4120 /* 4121 ** Generate VDBE code for a BEGIN statement. 4122 */ 4123 void sqlite3BeginTransaction(Parse *pParse, int type){ 4124 sqlite3 *db; 4125 Vdbe *v; 4126 int i; 4127 4128 assert( pParse!=0 ); 4129 db = pParse->db; 4130 assert( db!=0 ); 4131 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ 4132 return; 4133 } 4134 v = sqlite3GetVdbe(pParse); 4135 if( !v ) return; 4136 if( type!=TK_DEFERRED ){ 4137 for(i=0; i<db->nDb; i++){ 4138 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); 4139 sqlite3VdbeUsesBtree(v, i); 4140 } 4141 } 4142 sqlite3VdbeAddOp0(v, OP_AutoCommit); 4143 } 4144 4145 /* 4146 ** Generate VDBE code for a COMMIT or ROLLBACK statement. 4147 ** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise 4148 ** code is generated for a COMMIT. 4149 */ 4150 void sqlite3EndTransaction(Parse *pParse, int eType){ 4151 Vdbe *v; 4152 int isRollback; 4153 4154 assert( pParse!=0 ); 4155 assert( pParse->db!=0 ); 4156 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK ); 4157 isRollback = eType==TK_ROLLBACK; 4158 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, 4159 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){ 4160 return; 4161 } 4162 v = sqlite3GetVdbe(pParse); 4163 if( v ){ 4164 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback); 4165 } 4166 } 4167 4168 /* 4169 ** This function is called by the parser when it parses a command to create, 4170 ** release or rollback an SQL savepoint. 4171 */ 4172 void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ 4173 char *zName = sqlite3NameFromToken(pParse->db, pName); 4174 if( zName ){ 4175 Vdbe *v = sqlite3GetVdbe(pParse); 4176 #ifndef SQLITE_OMIT_AUTHORIZATION 4177 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; 4178 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); 4179 #endif 4180 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ 4181 sqlite3DbFree(pParse->db, zName); 4182 return; 4183 } 4184 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); 4185 } 4186 } 4187 4188 /* 4189 ** Make sure the TEMP database is open and available for use. Return 4190 ** the number of errors. Leave any error messages in the pParse structure. 4191 */ 4192 int sqlite3OpenTempDatabase(Parse *pParse){ 4193 sqlite3 *db = pParse->db; 4194 if( db->aDb[1].pBt==0 && !pParse->explain ){ 4195 int rc; 4196 Btree *pBt; 4197 static const int flags = 4198 SQLITE_OPEN_READWRITE | 4199 SQLITE_OPEN_CREATE | 4200 SQLITE_OPEN_EXCLUSIVE | 4201 SQLITE_OPEN_DELETEONCLOSE | 4202 SQLITE_OPEN_TEMP_DB; 4203 4204 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); 4205 if( rc!=SQLITE_OK ){ 4206 sqlite3ErrorMsg(pParse, "unable to open a temporary database " 4207 "file for storing temporary tables"); 4208 pParse->rc = rc; 4209 return 1; 4210 } 4211 db->aDb[1].pBt = pBt; 4212 assert( db->aDb[1].pSchema ); 4213 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ 4214 sqlite3OomFault(db); 4215 return 1; 4216 } 4217 } 4218 return 0; 4219 } 4220 4221 /* 4222 ** Record the fact that the schema cookie will need to be verified 4223 ** for database iDb. The code to actually verify the schema cookie 4224 ** will occur at the end of the top-level VDBE and will be generated 4225 ** later, by sqlite3FinishCoding(). 4226 */ 4227 void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ 4228 Parse *pToplevel = sqlite3ParseToplevel(pParse); 4229 4230 assert( iDb>=0 && iDb<pParse->db->nDb ); 4231 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 ); 4232 assert( iDb<SQLITE_MAX_ATTACHED+2 ); 4233 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) ); 4234 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ 4235 DbMaskSet(pToplevel->cookieMask, iDb); 4236 if( !OMIT_TEMPDB && iDb==1 ){ 4237 sqlite3OpenTempDatabase(pToplevel); 4238 } 4239 } 4240 } 4241 4242 /* 4243 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 4244 ** attached database. Otherwise, invoke it for the database named zDb only. 4245 */ 4246 void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ 4247 sqlite3 *db = pParse->db; 4248 int i; 4249 for(i=0; i<db->nDb; i++){ 4250 Db *pDb = &db->aDb[i]; 4251 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ 4252 sqlite3CodeVerifySchema(pParse, i); 4253 } 4254 } 4255 } 4256 4257 /* 4258 ** Generate VDBE code that prepares for doing an operation that 4259 ** might change the database. 4260 ** 4261 ** This routine starts a new transaction if we are not already within 4262 ** a transaction. If we are already within a transaction, then a checkpoint 4263 ** is set if the setStatement parameter is true. A checkpoint should 4264 ** be set for operations that might fail (due to a constraint) part of 4265 ** the way through and which will need to undo some writes without having to 4266 ** rollback the whole transaction. For operations where all constraints 4267 ** can be checked before any changes are made to the database, it is never 4268 ** necessary to undo a write and the checkpoint should not be set. 4269 */ 4270 void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ 4271 Parse *pToplevel = sqlite3ParseToplevel(pParse); 4272 sqlite3CodeVerifySchema(pParse, iDb); 4273 DbMaskSet(pToplevel->writeMask, iDb); 4274 pToplevel->isMultiWrite |= setStatement; 4275 } 4276 4277 /* 4278 ** Indicate that the statement currently under construction might write 4279 ** more than one entry (example: deleting one row then inserting another, 4280 ** inserting multiple rows in a table, or inserting a row and index entries.) 4281 ** If an abort occurs after some of these writes have completed, then it will 4282 ** be necessary to undo the completed writes. 4283 */ 4284 void sqlite3MultiWrite(Parse *pParse){ 4285 Parse *pToplevel = sqlite3ParseToplevel(pParse); 4286 pToplevel->isMultiWrite = 1; 4287 } 4288 4289 /* 4290 ** The code generator calls this routine if is discovers that it is 4291 ** possible to abort a statement prior to completion. In order to 4292 ** perform this abort without corrupting the database, we need to make 4293 ** sure that the statement is protected by a statement transaction. 4294 ** 4295 ** Technically, we only need to set the mayAbort flag if the 4296 ** isMultiWrite flag was previously set. There is a time dependency 4297 ** such that the abort must occur after the multiwrite. This makes 4298 ** some statements involving the REPLACE conflict resolution algorithm 4299 ** go a little faster. But taking advantage of this time dependency 4300 ** makes it more difficult to prove that the code is correct (in 4301 ** particular, it prevents us from writing an effective 4302 ** implementation of sqlite3AssertMayAbort()) and so we have chosen 4303 ** to take the safe route and skip the optimization. 4304 */ 4305 void sqlite3MayAbort(Parse *pParse){ 4306 Parse *pToplevel = sqlite3ParseToplevel(pParse); 4307 pToplevel->mayAbort = 1; 4308 } 4309 4310 /* 4311 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT 4312 ** error. The onError parameter determines which (if any) of the statement 4313 ** and/or current transaction is rolled back. 4314 */ 4315 void sqlite3HaltConstraint( 4316 Parse *pParse, /* Parsing context */ 4317 int errCode, /* extended error code */ 4318 int onError, /* Constraint type */ 4319 char *p4, /* Error message */ 4320 i8 p4type, /* P4_STATIC or P4_TRANSIENT */ 4321 u8 p5Errmsg /* P5_ErrMsg type */ 4322 ){ 4323 Vdbe *v = sqlite3GetVdbe(pParse); 4324 assert( (errCode&0xff)==SQLITE_CONSTRAINT ); 4325 if( onError==OE_Abort ){ 4326 sqlite3MayAbort(pParse); 4327 } 4328 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); 4329 sqlite3VdbeChangeP5(v, p5Errmsg); 4330 } 4331 4332 /* 4333 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. 4334 */ 4335 void sqlite3UniqueConstraint( 4336 Parse *pParse, /* Parsing context */ 4337 int onError, /* Constraint type */ 4338 Index *pIdx /* The index that triggers the constraint */ 4339 ){ 4340 char *zErr; 4341 int j; 4342 StrAccum errMsg; 4343 Table *pTab = pIdx->pTable; 4344 4345 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200); 4346 if( pIdx->aColExpr ){ 4347 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName); 4348 }else{ 4349 for(j=0; j<pIdx->nKeyCol; j++){ 4350 char *zCol; 4351 assert( pIdx->aiColumn[j]>=0 ); 4352 zCol = pTab->aCol[pIdx->aiColumn[j]].zName; 4353 if( j ) sqlite3_str_append(&errMsg, ", ", 2); 4354 sqlite3_str_appendall(&errMsg, pTab->zName); 4355 sqlite3_str_append(&errMsg, ".", 1); 4356 sqlite3_str_appendall(&errMsg, zCol); 4357 } 4358 } 4359 zErr = sqlite3StrAccumFinish(&errMsg); 4360 sqlite3HaltConstraint(pParse, 4361 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY 4362 : SQLITE_CONSTRAINT_UNIQUE, 4363 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); 4364 } 4365 4366 4367 /* 4368 ** Code an OP_Halt due to non-unique rowid. 4369 */ 4370 void sqlite3RowidConstraint( 4371 Parse *pParse, /* Parsing context */ 4372 int onError, /* Conflict resolution algorithm */ 4373 Table *pTab /* The table with the non-unique rowid */ 4374 ){ 4375 char *zMsg; 4376 int rc; 4377 if( pTab->iPKey>=0 ){ 4378 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, 4379 pTab->aCol[pTab->iPKey].zName); 4380 rc = SQLITE_CONSTRAINT_PRIMARYKEY; 4381 }else{ 4382 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); 4383 rc = SQLITE_CONSTRAINT_ROWID; 4384 } 4385 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC, 4386 P5_ConstraintUnique); 4387 } 4388 4389 /* 4390 ** Check to see if pIndex uses the collating sequence pColl. Return 4391 ** true if it does and false if it does not. 4392 */ 4393 #ifndef SQLITE_OMIT_REINDEX 4394 static int collationMatch(const char *zColl, Index *pIndex){ 4395 int i; 4396 assert( zColl!=0 ); 4397 for(i=0; i<pIndex->nColumn; i++){ 4398 const char *z = pIndex->azColl[i]; 4399 assert( z!=0 || pIndex->aiColumn[i]<0 ); 4400 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){ 4401 return 1; 4402 } 4403 } 4404 return 0; 4405 } 4406 #endif 4407 4408 /* 4409 ** Recompute all indices of pTab that use the collating sequence pColl. 4410 ** If pColl==0 then recompute all indices of pTab. 4411 */ 4412 #ifndef SQLITE_OMIT_REINDEX 4413 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ 4414 Index *pIndex; /* An index associated with pTab */ 4415 4416 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ 4417 if( zColl==0 || collationMatch(zColl, pIndex) ){ 4418 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 4419 sqlite3BeginWriteOperation(pParse, 0, iDb); 4420 sqlite3RefillIndex(pParse, pIndex, -1); 4421 } 4422 } 4423 } 4424 #endif 4425 4426 /* 4427 ** Recompute all indices of all tables in all databases where the 4428 ** indices use the collating sequence pColl. If pColl==0 then recompute 4429 ** all indices everywhere. 4430 */ 4431 #ifndef SQLITE_OMIT_REINDEX 4432 static void reindexDatabases(Parse *pParse, char const *zColl){ 4433 Db *pDb; /* A single database */ 4434 int iDb; /* The database index number */ 4435 sqlite3 *db = pParse->db; /* The database connection */ 4436 HashElem *k; /* For looping over tables in pDb */ 4437 Table *pTab; /* A table in the database */ 4438 4439 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */ 4440 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ 4441 assert( pDb!=0 ); 4442 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ 4443 pTab = (Table*)sqliteHashData(k); 4444 reindexTable(pParse, pTab, zColl); 4445 } 4446 } 4447 } 4448 #endif 4449 4450 /* 4451 ** Generate code for the REINDEX command. 4452 ** 4453 ** REINDEX -- 1 4454 ** REINDEX <collation> -- 2 4455 ** REINDEX ?<database>.?<tablename> -- 3 4456 ** REINDEX ?<database>.?<indexname> -- 4 4457 ** 4458 ** Form 1 causes all indices in all attached databases to be rebuilt. 4459 ** Form 2 rebuilds all indices in all databases that use the named 4460 ** collating function. Forms 3 and 4 rebuild the named index or all 4461 ** indices associated with the named table. 4462 */ 4463 #ifndef SQLITE_OMIT_REINDEX 4464 void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ 4465 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ 4466 char *z; /* Name of a table or index */ 4467 const char *zDb; /* Name of the database */ 4468 Table *pTab; /* A table in the database */ 4469 Index *pIndex; /* An index associated with pTab */ 4470 int iDb; /* The database index number */ 4471 sqlite3 *db = pParse->db; /* The database connection */ 4472 Token *pObjName; /* Name of the table or index to be reindexed */ 4473 4474 /* Read the database schema. If an error occurs, leave an error message 4475 ** and code in pParse and return NULL. */ 4476 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 4477 return; 4478 } 4479 4480 if( pName1==0 ){ 4481 reindexDatabases(pParse, 0); 4482 return; 4483 }else if( NEVER(pName2==0) || pName2->z==0 ){ 4484 char *zColl; 4485 assert( pName1->z ); 4486 zColl = sqlite3NameFromToken(pParse->db, pName1); 4487 if( !zColl ) return; 4488 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); 4489 if( pColl ){ 4490 reindexDatabases(pParse, zColl); 4491 sqlite3DbFree(db, zColl); 4492 return; 4493 } 4494 sqlite3DbFree(db, zColl); 4495 } 4496 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); 4497 if( iDb<0 ) return; 4498 z = sqlite3NameFromToken(db, pObjName); 4499 if( z==0 ) return; 4500 zDb = db->aDb[iDb].zDbSName; 4501 pTab = sqlite3FindTable(db, z, zDb); 4502 if( pTab ){ 4503 reindexTable(pParse, pTab, 0); 4504 sqlite3DbFree(db, z); 4505 return; 4506 } 4507 pIndex = sqlite3FindIndex(db, z, zDb); 4508 sqlite3DbFree(db, z); 4509 if( pIndex ){ 4510 sqlite3BeginWriteOperation(pParse, 0, iDb); 4511 sqlite3RefillIndex(pParse, pIndex, -1); 4512 return; 4513 } 4514 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); 4515 } 4516 #endif 4517 4518 /* 4519 ** Return a KeyInfo structure that is appropriate for the given Index. 4520 ** 4521 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object 4522 ** when it has finished using it. 4523 */ 4524 KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ 4525 int i; 4526 int nCol = pIdx->nColumn; 4527 int nKey = pIdx->nKeyCol; 4528 KeyInfo *pKey; 4529 if( pParse->nErr ) return 0; 4530 if( pIdx->uniqNotNull ){ 4531 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); 4532 }else{ 4533 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); 4534 } 4535 if( pKey ){ 4536 assert( sqlite3KeyInfoIsWriteable(pKey) ); 4537 for(i=0; i<nCol; i++){ 4538 const char *zColl = pIdx->azColl[i]; 4539 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : 4540 sqlite3LocateCollSeq(pParse, zColl); 4541 pKey->aSortOrder[i] = pIdx->aSortOrder[i]; 4542 } 4543 if( pParse->nErr ){ 4544 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ ); 4545 if( pIdx->bNoQuery==0 ){ 4546 /* Deactivate the index because it contains an unknown collating 4547 ** sequence. The only way to reactive the index is to reload the 4548 ** schema. Adding the missing collating sequence later does not 4549 ** reactive the index. The application had the chance to register 4550 ** the missing index using the collation-needed callback. For 4551 ** simplicity, SQLite will not give the application a second chance. 4552 */ 4553 pIdx->bNoQuery = 1; 4554 pParse->rc = SQLITE_ERROR_RETRY; 4555 } 4556 sqlite3KeyInfoUnref(pKey); 4557 pKey = 0; 4558 } 4559 } 4560 return pKey; 4561 } 4562 4563 #ifndef SQLITE_OMIT_CTE 4564 /* 4565 ** This routine is invoked once per CTE by the parser while parsing a 4566 ** WITH clause. 4567 */ 4568 With *sqlite3WithAdd( 4569 Parse *pParse, /* Parsing context */ 4570 With *pWith, /* Existing WITH clause, or NULL */ 4571 Token *pName, /* Name of the common-table */ 4572 ExprList *pArglist, /* Optional column name list for the table */ 4573 Select *pQuery /* Query used to initialize the table */ 4574 ){ 4575 sqlite3 *db = pParse->db; 4576 With *pNew; 4577 char *zName; 4578 4579 /* Check that the CTE name is unique within this WITH clause. If 4580 ** not, store an error in the Parse structure. */ 4581 zName = sqlite3NameFromToken(pParse->db, pName); 4582 if( zName && pWith ){ 4583 int i; 4584 for(i=0; i<pWith->nCte; i++){ 4585 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){ 4586 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName); 4587 } 4588 } 4589 } 4590 4591 if( pWith ){ 4592 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); 4593 pNew = sqlite3DbRealloc(db, pWith, nByte); 4594 }else{ 4595 pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); 4596 } 4597 assert( (pNew!=0 && zName!=0) || db->mallocFailed ); 4598 4599 if( db->mallocFailed ){ 4600 sqlite3ExprListDelete(db, pArglist); 4601 sqlite3SelectDelete(db, pQuery); 4602 sqlite3DbFree(db, zName); 4603 pNew = pWith; 4604 }else{ 4605 pNew->a[pNew->nCte].pSelect = pQuery; 4606 pNew->a[pNew->nCte].pCols = pArglist; 4607 pNew->a[pNew->nCte].zName = zName; 4608 pNew->a[pNew->nCte].zCteErr = 0; 4609 pNew->nCte++; 4610 } 4611 4612 return pNew; 4613 } 4614 4615 /* 4616 ** Free the contents of the With object passed as the second argument. 4617 */ 4618 void sqlite3WithDelete(sqlite3 *db, With *pWith){ 4619 if( pWith ){ 4620 int i; 4621 for(i=0; i<pWith->nCte; i++){ 4622 struct Cte *pCte = &pWith->a[i]; 4623 sqlite3ExprListDelete(db, pCte->pCols); 4624 sqlite3SelectDelete(db, pCte->pSelect); 4625 sqlite3DbFree(db, pCte->zName); 4626 } 4627 sqlite3DbFree(db, pWith); 4628 } 4629 } 4630 #endif /* !defined(SQLITE_OMIT_CTE) */ 4631