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