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