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