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