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