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