000001 /* 000002 ** 2001 September 15 000003 ** 000004 ** The author disclaims copyright to this source code. In place of 000005 ** a legal notice, here is a blessing: 000006 ** 000007 ** May you do good and not evil. 000008 ** May you find forgiveness for yourself and forgive others. 000009 ** May you share freely, never taking more than you give. 000010 ** 000011 ************************************************************************* 000012 ** This file contains C code routines that are called by the SQLite parser 000013 ** when syntax rules are reduced. The routines in this file handle the 000014 ** following kinds of SQL syntax: 000015 ** 000016 ** CREATE TABLE 000017 ** DROP TABLE 000018 ** CREATE INDEX 000019 ** DROP INDEX 000020 ** creating ID lists 000021 ** BEGIN TRANSACTION 000022 ** COMMIT 000023 ** ROLLBACK 000024 */ 000025 #include "sqliteInt.h" 000026 000027 #ifndef SQLITE_OMIT_SHARED_CACHE 000028 /* 000029 ** The TableLock structure is only used by the sqlite3TableLock() and 000030 ** codeTableLocks() functions. 000031 */ 000032 struct TableLock { 000033 int iDb; /* The database containing the table to be locked */ 000034 int iTab; /* The root page of the table to be locked */ 000035 u8 isWriteLock; /* True for write lock. False for a read lock */ 000036 const char *zLockName; /* Name of the table */ 000037 }; 000038 000039 /* 000040 ** Record the fact that we want to lock a table at run-time. 000041 ** 000042 ** The table to be locked has root page iTab and is found in database iDb. 000043 ** A read or a write lock can be taken depending on isWritelock. 000044 ** 000045 ** This routine just records the fact that the lock is desired. The 000046 ** code to make the lock occur is generated by a later call to 000047 ** codeTableLocks() which occurs during sqlite3FinishCoding(). 000048 */ 000049 void sqlite3TableLock( 000050 Parse *pParse, /* Parsing context */ 000051 int iDb, /* Index of the database containing the table to lock */ 000052 int iTab, /* Root page number of the table to be locked */ 000053 u8 isWriteLock, /* True for a write lock */ 000054 const char *zName /* Name of the table to be locked */ 000055 ){ 000056 Parse *pToplevel = sqlite3ParseToplevel(pParse); 000057 int i; 000058 int nBytes; 000059 TableLock *p; 000060 assert( iDb>=0 ); 000061 000062 if( iDb==1 ) return; 000063 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return; 000064 for(i=0; i<pToplevel->nTableLock; i++){ 000065 p = &pToplevel->aTableLock[i]; 000066 if( p->iDb==iDb && p->iTab==iTab ){ 000067 p->isWriteLock = (p->isWriteLock || isWriteLock); 000068 return; 000069 } 000070 } 000071 000072 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); 000073 pToplevel->aTableLock = 000074 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); 000075 if( pToplevel->aTableLock ){ 000076 p = &pToplevel->aTableLock[pToplevel->nTableLock++]; 000077 p->iDb = iDb; 000078 p->iTab = iTab; 000079 p->isWriteLock = isWriteLock; 000080 p->zLockName = zName; 000081 }else{ 000082 pToplevel->nTableLock = 0; 000083 sqlite3OomFault(pToplevel->db); 000084 } 000085 } 000086 000087 /* 000088 ** Code an OP_TableLock instruction for each table locked by the 000089 ** statement (configured by calls to sqlite3TableLock()). 000090 */ 000091 static void codeTableLocks(Parse *pParse){ 000092 int i; 000093 Vdbe *pVdbe; 000094 000095 pVdbe = sqlite3GetVdbe(pParse); 000096 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */ 000097 000098 for(i=0; i<pParse->nTableLock; i++){ 000099 TableLock *p = &pParse->aTableLock[i]; 000100 int p1 = p->iDb; 000101 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, 000102 p->zLockName, P4_STATIC); 000103 } 000104 } 000105 #else 000106 #define codeTableLocks(x) 000107 #endif 000108 000109 /* 000110 ** Return TRUE if the given yDbMask object is empty - if it contains no 000111 ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() 000112 ** macros when SQLITE_MAX_ATTACHED is greater than 30. 000113 */ 000114 #if SQLITE_MAX_ATTACHED>30 000115 int sqlite3DbMaskAllZero(yDbMask m){ 000116 int i; 000117 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0; 000118 return 1; 000119 } 000120 #endif 000121 000122 /* 000123 ** This routine is called after a single SQL statement has been 000124 ** parsed and a VDBE program to execute that statement has been 000125 ** prepared. This routine puts the finishing touches on the 000126 ** VDBE program and resets the pParse structure for the next 000127 ** parse. 000128 ** 000129 ** Note that if an error occurred, it might be the case that 000130 ** no VDBE code was generated. 000131 */ 000132 void sqlite3FinishCoding(Parse *pParse){ 000133 sqlite3 *db; 000134 Vdbe *v; 000135 000136 assert( pParse->pToplevel==0 ); 000137 db = pParse->db; 000138 if( pParse->nested ) return; 000139 if( db->mallocFailed || pParse->nErr ){ 000140 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR; 000141 return; 000142 } 000143 000144 /* Begin by generating some termination code at the end of the 000145 ** vdbe program 000146 */ 000147 v = sqlite3GetVdbe(pParse); 000148 assert( !pParse->isMultiWrite 000149 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); 000150 if( v ){ 000151 sqlite3VdbeAddOp0(v, OP_Halt); 000152 000153 #if SQLITE_USER_AUTHENTICATION 000154 if( pParse->nTableLock>0 && db->init.busy==0 ){ 000155 sqlite3UserAuthInit(db); 000156 if( db->auth.authLevel<UAUTH_User ){ 000157 sqlite3ErrorMsg(pParse, "user not authenticated"); 000158 pParse->rc = SQLITE_AUTH_USER; 000159 return; 000160 } 000161 } 000162 #endif 000163 000164 /* The cookie mask contains one bit for each database file open. 000165 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are 000166 ** set for each database that is used. Generate code to start a 000167 ** transaction on each used database and to verify the schema cookie 000168 ** on each used database. 000169 */ 000170 if( db->mallocFailed==0 000171 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) 000172 ){ 000173 int iDb, i; 000174 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); 000175 sqlite3VdbeJumpHere(v, 0); 000176 for(iDb=0; iDb<db->nDb; iDb++){ 000177 Schema *pSchema; 000178 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; 000179 sqlite3VdbeUsesBtree(v, iDb); 000180 pSchema = db->aDb[iDb].pSchema; 000181 sqlite3VdbeAddOp4Int(v, 000182 OP_Transaction, /* Opcode */ 000183 iDb, /* P1 */ 000184 DbMaskTest(pParse->writeMask,iDb), /* P2 */ 000185 pSchema->schema_cookie, /* P3 */ 000186 pSchema->iGeneration /* P4 */ 000187 ); 000188 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); 000189 VdbeComment((v, 000190 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); 000191 } 000192 #ifndef SQLITE_OMIT_VIRTUALTABLE 000193 for(i=0; i<pParse->nVtabLock; i++){ 000194 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); 000195 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); 000196 } 000197 pParse->nVtabLock = 0; 000198 #endif 000199 000200 /* Once all the cookies have been verified and transactions opened, 000201 ** obtain the required table-locks. This is a no-op unless the 000202 ** shared-cache feature is enabled. 000203 */ 000204 codeTableLocks(pParse); 000205 000206 /* Initialize any AUTOINCREMENT data structures required. 000207 */ 000208 sqlite3AutoincrementBegin(pParse); 000209 000210 /* Code constant expressions that where factored out of inner loops */ 000211 if( pParse->pConstExpr ){ 000212 ExprList *pEL = pParse->pConstExpr; 000213 pParse->okConstFactor = 0; 000214 for(i=0; i<pEL->nExpr; i++){ 000215 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); 000216 } 000217 } 000218 000219 /* Finally, jump back to the beginning of the executable code. */ 000220 sqlite3VdbeGoto(v, 1); 000221 } 000222 } 000223 000224 000225 /* Get the VDBE program ready for execution 000226 */ 000227 if( v && pParse->nErr==0 && !db->mallocFailed ){ 000228 /* A minimum of one cursor is required if autoincrement is used 000229 * See ticket [a696379c1f08866] */ 000230 assert( pParse->pAinc==0 || pParse->nTab>0 ); 000231 sqlite3VdbeMakeReady(v, pParse); 000232 pParse->rc = SQLITE_DONE; 000233 }else{ 000234 pParse->rc = SQLITE_ERROR; 000235 } 000236 } 000237 000238 /* 000239 ** Run the parser and code generator recursively in order to generate 000240 ** code for the SQL statement given onto the end of the pParse context 000241 ** currently under construction. When the parser is run recursively 000242 ** this way, the final OP_Halt is not appended and other initialization 000243 ** and finalization steps are omitted because those are handling by the 000244 ** outermost parser. 000245 ** 000246 ** Not everything is nestable. This facility is designed to permit 000247 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use 000248 ** care if you decide to try to use this routine for some other purposes. 000249 */ 000250 void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ 000251 va_list ap; 000252 char *zSql; 000253 char *zErrMsg = 0; 000254 sqlite3 *db = pParse->db; 000255 char saveBuf[PARSE_TAIL_SZ]; 000256 000257 if( pParse->nErr ) return; 000258 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ 000259 va_start(ap, zFormat); 000260 zSql = sqlite3VMPrintf(db, zFormat, ap); 000261 va_end(ap); 000262 if( zSql==0 ){ 000263 /* This can result either from an OOM or because the formatted string 000264 ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set 000265 ** an error */ 000266 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG; 000267 pParse->nErr++; 000268 return; 000269 } 000270 pParse->nested++; 000271 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); 000272 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); 000273 sqlite3RunParser(pParse, zSql, &zErrMsg); 000274 sqlite3DbFree(db, zErrMsg); 000275 sqlite3DbFree(db, zSql); 000276 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); 000277 pParse->nested--; 000278 } 000279 000280 #if SQLITE_USER_AUTHENTICATION 000281 /* 000282 ** Return TRUE if zTable is the name of the system table that stores the 000283 ** list of users and their access credentials. 000284 */ 000285 int sqlite3UserAuthTable(const char *zTable){ 000286 return sqlite3_stricmp(zTable, "sqlite_user")==0; 000287 } 000288 #endif 000289 000290 /* 000291 ** Locate the in-memory structure that describes a particular database 000292 ** table given the name of that table and (optionally) the name of the 000293 ** database containing the table. Return NULL if not found. 000294 ** 000295 ** If zDatabase is 0, all databases are searched for the table and the 000296 ** first matching table is returned. (No checking for duplicate table 000297 ** names is done.) The search order is TEMP first, then MAIN, then any 000298 ** auxiliary databases added using the ATTACH command. 000299 ** 000300 ** See also sqlite3LocateTable(). 000301 */ 000302 Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ 000303 Table *p = 0; 000304 int i; 000305 000306 /* All mutexes are required for schema access. Make sure we hold them. */ 000307 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); 000308 #if SQLITE_USER_AUTHENTICATION 000309 /* Only the admin user is allowed to know that the sqlite_user table 000310 ** exists */ 000311 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ 000312 return 0; 000313 } 000314 #endif 000315 while(1){ 000316 for(i=OMIT_TEMPDB; i<db->nDb; i++){ 000317 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ 000318 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){ 000319 assert( sqlite3SchemaMutexHeld(db, j, 0) ); 000320 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName); 000321 if( p ) return p; 000322 } 000323 } 000324 /* Not found. If the name we were looking for was temp.sqlite_master 000325 ** then change the name to sqlite_temp_master and try again. */ 000326 if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break; 000327 if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break; 000328 zName = TEMP_MASTER_NAME; 000329 } 000330 return 0; 000331 } 000332 000333 /* 000334 ** Locate the in-memory structure that describes a particular database 000335 ** table given the name of that table and (optionally) the name of the 000336 ** database containing the table. Return NULL if not found. Also leave an 000337 ** error message in pParse->zErrMsg. 000338 ** 000339 ** The difference between this routine and sqlite3FindTable() is that this 000340 ** routine leaves an error message in pParse->zErrMsg where 000341 ** sqlite3FindTable() does not. 000342 */ 000343 Table *sqlite3LocateTable( 000344 Parse *pParse, /* context in which to report errors */ 000345 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */ 000346 const char *zName, /* Name of the table we are looking for */ 000347 const char *zDbase /* Name of the database. Might be NULL */ 000348 ){ 000349 Table *p; 000350 sqlite3 *db = pParse->db; 000351 000352 /* Read the database schema. If an error occurs, leave an error message 000353 ** and code in pParse and return NULL. */ 000354 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 000355 && SQLITE_OK!=sqlite3ReadSchema(pParse) 000356 ){ 000357 return 0; 000358 } 000359 000360 p = sqlite3FindTable(db, zName, zDbase); 000361 if( p==0 ){ 000362 #ifndef SQLITE_OMIT_VIRTUALTABLE 000363 /* If zName is the not the name of a table in the schema created using 000364 ** CREATE, then check to see if it is the name of an virtual table that 000365 ** can be an eponymous virtual table. */ 000366 if( pParse->disableVtab==0 ){ 000367 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); 000368 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ 000369 pMod = sqlite3PragmaVtabRegister(db, zName); 000370 } 000371 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ 000372 return pMod->pEpoTab; 000373 } 000374 } 000375 #endif 000376 if( flags & LOCATE_NOERR ) return 0; 000377 pParse->checkSchema = 1; 000378 }else if( IsVirtual(p) && pParse->disableVtab ){ 000379 p = 0; 000380 } 000381 000382 if( p==0 ){ 000383 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; 000384 if( zDbase ){ 000385 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); 000386 }else{ 000387 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); 000388 } 000389 } 000390 000391 return p; 000392 } 000393 000394 /* 000395 ** Locate the table identified by *p. 000396 ** 000397 ** This is a wrapper around sqlite3LocateTable(). The difference between 000398 ** sqlite3LocateTable() and this function is that this function restricts 000399 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be 000400 ** non-NULL if it is part of a view or trigger program definition. See 000401 ** sqlite3FixSrcList() for details. 000402 */ 000403 Table *sqlite3LocateTableItem( 000404 Parse *pParse, 000405 u32 flags, 000406 struct SrcList_item *p 000407 ){ 000408 const char *zDb; 000409 assert( p->pSchema==0 || p->zDatabase==0 ); 000410 if( p->pSchema ){ 000411 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); 000412 zDb = pParse->db->aDb[iDb].zDbSName; 000413 }else{ 000414 zDb = p->zDatabase; 000415 } 000416 return sqlite3LocateTable(pParse, flags, p->zName, zDb); 000417 } 000418 000419 /* 000420 ** Locate the in-memory structure that describes 000421 ** a particular index given the name of that index 000422 ** and the name of the database that contains the index. 000423 ** Return NULL if not found. 000424 ** 000425 ** If zDatabase is 0, all databases are searched for the 000426 ** table and the first matching index is returned. (No checking 000427 ** for duplicate index names is done.) The search order is 000428 ** TEMP first, then MAIN, then any auxiliary databases added 000429 ** using the ATTACH command. 000430 */ 000431 Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ 000432 Index *p = 0; 000433 int i; 000434 /* All mutexes are required for schema access. Make sure we hold them. */ 000435 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); 000436 for(i=OMIT_TEMPDB; i<db->nDb; i++){ 000437 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ 000438 Schema *pSchema = db->aDb[j].pSchema; 000439 assert( pSchema ); 000440 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue; 000441 assert( sqlite3SchemaMutexHeld(db, j, 0) ); 000442 p = sqlite3HashFind(&pSchema->idxHash, zName); 000443 if( p ) break; 000444 } 000445 return p; 000446 } 000447 000448 /* 000449 ** Reclaim the memory used by an index 000450 */ 000451 void sqlite3FreeIndex(sqlite3 *db, Index *p){ 000452 #ifndef SQLITE_OMIT_ANALYZE 000453 sqlite3DeleteIndexSamples(db, p); 000454 #endif 000455 sqlite3ExprDelete(db, p->pPartIdxWhere); 000456 sqlite3ExprListDelete(db, p->aColExpr); 000457 sqlite3DbFree(db, p->zColAff); 000458 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); 000459 #ifdef SQLITE_ENABLE_STAT4 000460 sqlite3_free(p->aiRowEst); 000461 #endif 000462 sqlite3DbFree(db, p); 000463 } 000464 000465 /* 000466 ** For the index called zIdxName which is found in the database iDb, 000467 ** unlike that index from its Table then remove the index from 000468 ** the index hash table and free all memory structures associated 000469 ** with the index. 000470 */ 000471 void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ 000472 Index *pIndex; 000473 Hash *pHash; 000474 000475 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 000476 pHash = &db->aDb[iDb].pSchema->idxHash; 000477 pIndex = sqlite3HashInsert(pHash, zIdxName, 0); 000478 if( ALWAYS(pIndex) ){ 000479 if( pIndex->pTable->pIndex==pIndex ){ 000480 pIndex->pTable->pIndex = pIndex->pNext; 000481 }else{ 000482 Index *p; 000483 /* Justification of ALWAYS(); The index must be on the list of 000484 ** indices. */ 000485 p = pIndex->pTable->pIndex; 000486 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } 000487 if( ALWAYS(p && p->pNext==pIndex) ){ 000488 p->pNext = pIndex->pNext; 000489 } 000490 } 000491 sqlite3FreeIndex(db, pIndex); 000492 } 000493 db->mDbFlags |= DBFLAG_SchemaChange; 000494 } 000495 000496 /* 000497 ** Look through the list of open database files in db->aDb[] and if 000498 ** any have been closed, remove them from the list. Reallocate the 000499 ** db->aDb[] structure to a smaller size, if possible. 000500 ** 000501 ** Entry 0 (the "main" database) and entry 1 (the "temp" database) 000502 ** are never candidates for being collapsed. 000503 */ 000504 void sqlite3CollapseDatabaseArray(sqlite3 *db){ 000505 int i, j; 000506 for(i=j=2; i<db->nDb; i++){ 000507 struct Db *pDb = &db->aDb[i]; 000508 if( pDb->pBt==0 ){ 000509 sqlite3DbFree(db, pDb->zDbSName); 000510 pDb->zDbSName = 0; 000511 continue; 000512 } 000513 if( j<i ){ 000514 db->aDb[j] = db->aDb[i]; 000515 } 000516 j++; 000517 } 000518 db->nDb = j; 000519 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ 000520 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); 000521 sqlite3DbFree(db, db->aDb); 000522 db->aDb = db->aDbStatic; 000523 } 000524 } 000525 000526 /* 000527 ** Reset the schema for the database at index iDb. Also reset the 000528 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero. 000529 ** Deferred resets may be run by calling with iDb<0. 000530 */ 000531 void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ 000532 int i; 000533 assert( iDb<db->nDb ); 000534 000535 if( iDb>=0 ){ 000536 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 000537 DbSetProperty(db, iDb, DB_ResetWanted); 000538 DbSetProperty(db, 1, DB_ResetWanted); 000539 db->mDbFlags &= ~DBFLAG_SchemaKnownOk; 000540 } 000541 000542 if( db->nSchemaLock==0 ){ 000543 for(i=0; i<db->nDb; i++){ 000544 if( DbHasProperty(db, i, DB_ResetWanted) ){ 000545 sqlite3SchemaClear(db->aDb[i].pSchema); 000546 } 000547 } 000548 } 000549 } 000550 000551 /* 000552 ** Erase all schema information from all attached databases (including 000553 ** "main" and "temp") for a single database connection. 000554 */ 000555 void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ 000556 int i; 000557 sqlite3BtreeEnterAll(db); 000558 for(i=0; i<db->nDb; i++){ 000559 Db *pDb = &db->aDb[i]; 000560 if( pDb->pSchema ){ 000561 if( db->nSchemaLock==0 ){ 000562 sqlite3SchemaClear(pDb->pSchema); 000563 }else{ 000564 DbSetProperty(db, i, DB_ResetWanted); 000565 } 000566 } 000567 } 000568 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk); 000569 sqlite3VtabUnlockList(db); 000570 sqlite3BtreeLeaveAll(db); 000571 if( db->nSchemaLock==0 ){ 000572 sqlite3CollapseDatabaseArray(db); 000573 } 000574 } 000575 000576 /* 000577 ** This routine is called when a commit occurs. 000578 */ 000579 void sqlite3CommitInternalChanges(sqlite3 *db){ 000580 db->mDbFlags &= ~DBFLAG_SchemaChange; 000581 } 000582 000583 /* 000584 ** Delete memory allocated for the column names of a table or view (the 000585 ** Table.aCol[] array). 000586 */ 000587 void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ 000588 int i; 000589 Column *pCol; 000590 assert( pTable!=0 ); 000591 if( (pCol = pTable->aCol)!=0 ){ 000592 for(i=0; i<pTable->nCol; i++, pCol++){ 000593 sqlite3DbFree(db, pCol->zName); 000594 sqlite3ExprDelete(db, pCol->pDflt); 000595 sqlite3DbFree(db, pCol->zColl); 000596 } 000597 sqlite3DbFree(db, pTable->aCol); 000598 } 000599 } 000600 000601 /* 000602 ** Remove the memory data structures associated with the given 000603 ** Table. No changes are made to disk by this routine. 000604 ** 000605 ** This routine just deletes the data structure. It does not unlink 000606 ** the table data structure from the hash table. But it does destroy 000607 ** memory structures of the indices and foreign keys associated with 000608 ** the table. 000609 ** 000610 ** The db parameter is optional. It is needed if the Table object 000611 ** contains lookaside memory. (Table objects in the schema do not use 000612 ** lookaside memory, but some ephemeral Table objects do.) Or the 000613 ** db parameter can be used with db->pnBytesFreed to measure the memory 000614 ** used by the Table object. 000615 */ 000616 static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ 000617 Index *pIndex, *pNext; 000618 000619 #ifdef SQLITE_DEBUG 000620 /* Record the number of outstanding lookaside allocations in schema Tables 000621 ** prior to doing any free() operations. Since schema Tables do not use 000622 ** lookaside, this number should not change. 000623 ** 000624 ** If malloc has already failed, it may be that it failed while allocating 000625 ** a Table object that was going to be marked ephemeral. So do not check 000626 ** that no lookaside memory is used in this case either. */ 000627 int nLookaside = 0; 000628 if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){ 000629 nLookaside = sqlite3LookasideUsed(db, 0); 000630 } 000631 #endif 000632 000633 /* Delete all indices associated with this table. */ 000634 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ 000635 pNext = pIndex->pNext; 000636 assert( pIndex->pSchema==pTable->pSchema 000637 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); 000638 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){ 000639 char *zName = pIndex->zName; 000640 TESTONLY ( Index *pOld = ) sqlite3HashInsert( 000641 &pIndex->pSchema->idxHash, zName, 0 000642 ); 000643 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); 000644 assert( pOld==pIndex || pOld==0 ); 000645 } 000646 sqlite3FreeIndex(db, pIndex); 000647 } 000648 000649 /* Delete any foreign keys attached to this table. */ 000650 sqlite3FkDelete(db, pTable); 000651 000652 /* Delete the Table structure itself. 000653 */ 000654 sqlite3DeleteColumnNames(db, pTable); 000655 sqlite3DbFree(db, pTable->zName); 000656 sqlite3DbFree(db, pTable->zColAff); 000657 sqlite3SelectDelete(db, pTable->pSelect); 000658 sqlite3ExprListDelete(db, pTable->pCheck); 000659 #ifndef SQLITE_OMIT_VIRTUALTABLE 000660 sqlite3VtabClear(db, pTable); 000661 #endif 000662 sqlite3DbFree(db, pTable); 000663 000664 /* Verify that no lookaside memory was used by schema tables */ 000665 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) ); 000666 } 000667 void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ 000668 /* Do not delete the table until the reference count reaches zero. */ 000669 if( !pTable ) return; 000670 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return; 000671 deleteTable(db, pTable); 000672 } 000673 000674 000675 /* 000676 ** Unlink the given table from the hash tables and the delete the 000677 ** table structure with all its indices and foreign keys. 000678 */ 000679 void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ 000680 Table *p; 000681 Db *pDb; 000682 000683 assert( db!=0 ); 000684 assert( iDb>=0 && iDb<db->nDb ); 000685 assert( zTabName ); 000686 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 000687 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ 000688 pDb = &db->aDb[iDb]; 000689 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0); 000690 sqlite3DeleteTable(db, p); 000691 db->mDbFlags |= DBFLAG_SchemaChange; 000692 } 000693 000694 /* 000695 ** Given a token, return a string that consists of the text of that 000696 ** token. Space to hold the returned string 000697 ** is obtained from sqliteMalloc() and must be freed by the calling 000698 ** function. 000699 ** 000700 ** Any quotation marks (ex: "name", 'name', [name], or `name`) that 000701 ** surround the body of the token are removed. 000702 ** 000703 ** Tokens are often just pointers into the original SQL text and so 000704 ** are not \000 terminated and are not persistent. The returned string 000705 ** is \000 terminated and is persistent. 000706 */ 000707 char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ 000708 char *zName; 000709 if( pName ){ 000710 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n); 000711 sqlite3Dequote(zName); 000712 }else{ 000713 zName = 0; 000714 } 000715 return zName; 000716 } 000717 000718 /* 000719 ** Open the sqlite_master table stored in database number iDb for 000720 ** writing. The table is opened using cursor 0. 000721 */ 000722 void sqlite3OpenMasterTable(Parse *p, int iDb){ 000723 Vdbe *v = sqlite3GetVdbe(p); 000724 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME); 000725 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5); 000726 if( p->nTab==0 ){ 000727 p->nTab = 1; 000728 } 000729 } 000730 000731 /* 000732 ** Parameter zName points to a nul-terminated buffer containing the name 000733 ** of a database ("main", "temp" or the name of an attached db). This 000734 ** function returns the index of the named database in db->aDb[], or 000735 ** -1 if the named db cannot be found. 000736 */ 000737 int sqlite3FindDbName(sqlite3 *db, const char *zName){ 000738 int i = -1; /* Database number */ 000739 if( zName ){ 000740 Db *pDb; 000741 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ 000742 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break; 000743 /* "main" is always an acceptable alias for the primary database 000744 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ 000745 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break; 000746 } 000747 } 000748 return i; 000749 } 000750 000751 /* 000752 ** The token *pName contains the name of a database (either "main" or 000753 ** "temp" or the name of an attached db). This routine returns the 000754 ** index of the named database in db->aDb[], or -1 if the named db 000755 ** does not exist. 000756 */ 000757 int sqlite3FindDb(sqlite3 *db, Token *pName){ 000758 int i; /* Database number */ 000759 char *zName; /* Name we are searching for */ 000760 zName = sqlite3NameFromToken(db, pName); 000761 i = sqlite3FindDbName(db, zName); 000762 sqlite3DbFree(db, zName); 000763 return i; 000764 } 000765 000766 /* The table or view or trigger name is passed to this routine via tokens 000767 ** pName1 and pName2. If the table name was fully qualified, for example: 000768 ** 000769 ** CREATE TABLE xxx.yyy (...); 000770 ** 000771 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if 000772 ** the table name is not fully qualified, i.e.: 000773 ** 000774 ** CREATE TABLE yyy(...); 000775 ** 000776 ** Then pName1 is set to "yyy" and pName2 is "". 000777 ** 000778 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or 000779 ** pName2) that stores the unqualified table name. The index of the 000780 ** database "xxx" is returned. 000781 */ 000782 int sqlite3TwoPartName( 000783 Parse *pParse, /* Parsing and code generating context */ 000784 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ 000785 Token *pName2, /* The "yyy" in the name "xxx.yyy" */ 000786 Token **pUnqual /* Write the unqualified object name here */ 000787 ){ 000788 int iDb; /* Database holding the object */ 000789 sqlite3 *db = pParse->db; 000790 000791 assert( pName2!=0 ); 000792 if( pName2->n>0 ){ 000793 if( db->init.busy ) { 000794 sqlite3ErrorMsg(pParse, "corrupt database"); 000795 return -1; 000796 } 000797 *pUnqual = pName2; 000798 iDb = sqlite3FindDb(db, pName1); 000799 if( iDb<0 ){ 000800 sqlite3ErrorMsg(pParse, "unknown database %T", pName1); 000801 return -1; 000802 } 000803 }else{ 000804 assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT 000805 || (db->mDbFlags & DBFLAG_Vacuum)!=0); 000806 iDb = db->init.iDb; 000807 *pUnqual = pName1; 000808 } 000809 return iDb; 000810 } 000811 000812 /* 000813 ** True if PRAGMA writable_schema is ON 000814 */ 000815 int sqlite3WritableSchema(sqlite3 *db){ 000816 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 ); 000817 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== 000818 SQLITE_WriteSchema ); 000819 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== 000820 SQLITE_Defensive ); 000821 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== 000822 (SQLITE_WriteSchema|SQLITE_Defensive) ); 000823 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema; 000824 } 000825 000826 /* 000827 ** This routine is used to check if the UTF-8 string zName is a legal 000828 ** unqualified name for a new schema object (table, index, view or 000829 ** trigger). All names are legal except those that begin with the string 000830 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace 000831 ** is reserved for internal use. 000832 ** 000833 ** When parsing the sqlite_master table, this routine also checks to 000834 ** make sure the "type", "name", and "tbl_name" columns are consistent 000835 ** with the SQL. 000836 */ 000837 int sqlite3CheckObjectName( 000838 Parse *pParse, /* Parsing context */ 000839 const char *zName, /* Name of the object to check */ 000840 const char *zType, /* Type of this object */ 000841 const char *zTblName /* Parent table name for triggers and indexes */ 000842 ){ 000843 sqlite3 *db = pParse->db; 000844 if( sqlite3WritableSchema(db) || db->init.imposterTable ){ 000845 /* Skip these error checks for writable_schema=ON */ 000846 return SQLITE_OK; 000847 } 000848 if( db->init.busy ){ 000849 if( sqlite3_stricmp(zType, db->init.azInit[0]) 000850 || sqlite3_stricmp(zName, db->init.azInit[1]) 000851 || sqlite3_stricmp(zTblName, db->init.azInit[2]) 000852 ){ 000853 if( sqlite3Config.bExtraSchemaChecks ){ 000854 sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */ 000855 return SQLITE_ERROR; 000856 } 000857 } 000858 }else{ 000859 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7)) 000860 || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName)) 000861 ){ 000862 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", 000863 zName); 000864 return SQLITE_ERROR; 000865 } 000866 000867 } 000868 return SQLITE_OK; 000869 } 000870 000871 /* 000872 ** Return the PRIMARY KEY index of a table 000873 */ 000874 Index *sqlite3PrimaryKeyIndex(Table *pTab){ 000875 Index *p; 000876 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){} 000877 return p; 000878 } 000879 000880 /* 000881 ** Convert an table column number into a index column number. That is, 000882 ** for the column iCol in the table (as defined by the CREATE TABLE statement) 000883 ** find the (first) offset of that column in index pIdx. Or return -1 000884 ** if column iCol is not used in index pIdx. 000885 */ 000886 i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){ 000887 int i; 000888 for(i=0; i<pIdx->nColumn; i++){ 000889 if( iCol==pIdx->aiColumn[i] ) return i; 000890 } 000891 return -1; 000892 } 000893 000894 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 000895 /* Convert a storage column number into a table column number. 000896 ** 000897 ** The storage column number (0,1,2,....) is the index of the value 000898 ** as it appears in the record on disk. The true column number 000899 ** is the index (0,1,2,...) of the column in the CREATE TABLE statement. 000900 ** 000901 ** The storage column number is less than the table column number if 000902 ** and only there are VIRTUAL columns to the left. 000903 ** 000904 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro. 000905 */ 000906 i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){ 000907 if( pTab->tabFlags & TF_HasVirtual ){ 000908 int i; 000909 for(i=0; i<=iCol; i++){ 000910 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++; 000911 } 000912 } 000913 return iCol; 000914 } 000915 #endif 000916 000917 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 000918 /* Convert a table column number into a storage column number. 000919 ** 000920 ** The storage column number (0,1,2,....) is the index of the value 000921 ** as it appears in the record on disk. Or, if the input column is 000922 ** the N-th virtual column (zero-based) then the storage number is 000923 ** the number of non-virtual columns in the table plus N. 000924 ** 000925 ** The true column number is the index (0,1,2,...) of the column in 000926 ** the CREATE TABLE statement. 000927 ** 000928 ** If the input column is a VIRTUAL column, then it should not appear 000929 ** in storage. But the value sometimes is cached in registers that 000930 ** follow the range of registers used to construct storage. This 000931 ** avoids computing the same VIRTUAL column multiple times, and provides 000932 ** values for use by OP_Param opcodes in triggers. Hence, if the 000933 ** input column is a VIRTUAL table, put it after all the other columns. 000934 ** 000935 ** In the following, N means "normal column", S means STORED, and 000936 ** V means VIRTUAL. Suppose the CREATE TABLE has columns like this: 000937 ** 000938 ** CREATE TABLE ex(N,S,V,N,S,V,N,S,V); 000939 ** -- 0 1 2 3 4 5 6 7 8 000940 ** 000941 ** Then the mapping from this function is as follows: 000942 ** 000943 ** INPUTS: 0 1 2 3 4 5 6 7 8 000944 ** OUTPUTS: 0 1 6 2 3 7 4 5 8 000945 ** 000946 ** So, in other words, this routine shifts all the virtual columns to 000947 ** the end. 000948 ** 000949 ** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and 000950 ** this routine is a no-op macro. If the pTab does not have any virtual 000951 ** columns, then this routine is no-op that always return iCol. If iCol 000952 ** is negative (indicating the ROWID column) then this routine return iCol. 000953 */ 000954 i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){ 000955 int i; 000956 i16 n; 000957 assert( iCol<pTab->nCol ); 000958 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol; 000959 for(i=0, n=0; i<iCol; i++){ 000960 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++; 000961 } 000962 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){ 000963 /* iCol is a virtual column itself */ 000964 return pTab->nNVCol + i - n; 000965 }else{ 000966 /* iCol is a normal or stored column */ 000967 return n; 000968 } 000969 } 000970 #endif 000971 000972 /* 000973 ** Begin constructing a new table representation in memory. This is 000974 ** the first of several action routines that get called in response 000975 ** to a CREATE TABLE statement. In particular, this routine is called 000976 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp 000977 ** flag is true if the table should be stored in the auxiliary database 000978 ** file instead of in the main database file. This is normally the case 000979 ** when the "TEMP" or "TEMPORARY" keyword occurs in between 000980 ** CREATE and TABLE. 000981 ** 000982 ** The new table record is initialized and put in pParse->pNewTable. 000983 ** As more of the CREATE TABLE statement is parsed, additional action 000984 ** routines will be called to add more information to this record. 000985 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine 000986 ** is called to complete the construction of the new table record. 000987 */ 000988 void sqlite3StartTable( 000989 Parse *pParse, /* Parser context */ 000990 Token *pName1, /* First part of the name of the table or view */ 000991 Token *pName2, /* Second part of the name of the table or view */ 000992 int isTemp, /* True if this is a TEMP table */ 000993 int isView, /* True if this is a VIEW */ 000994 int isVirtual, /* True if this is a VIRTUAL table */ 000995 int noErr /* Do nothing if table already exists */ 000996 ){ 000997 Table *pTable; 000998 char *zName = 0; /* The name of the new table */ 000999 sqlite3 *db = pParse->db; 001000 Vdbe *v; 001001 int iDb; /* Database number to create the table in */ 001002 Token *pName; /* Unqualified name of the table to create */ 001003 001004 if( db->init.busy && db->init.newTnum==1 ){ 001005 /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */ 001006 iDb = db->init.iDb; 001007 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); 001008 pName = pName1; 001009 }else{ 001010 /* The common case */ 001011 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); 001012 if( iDb<0 ) return; 001013 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ 001014 /* If creating a temp table, the name may not be qualified. Unless 001015 ** the database name is "temp" anyway. */ 001016 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); 001017 return; 001018 } 001019 if( !OMIT_TEMPDB && isTemp ) iDb = 1; 001020 zName = sqlite3NameFromToken(db, pName); 001021 if( IN_RENAME_OBJECT ){ 001022 sqlite3RenameTokenMap(pParse, (void*)zName, pName); 001023 } 001024 } 001025 pParse->sNameToken = *pName; 001026 if( zName==0 ) return; 001027 if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){ 001028 goto begin_table_error; 001029 } 001030 if( db->init.iDb==1 ) isTemp = 1; 001031 #ifndef SQLITE_OMIT_AUTHORIZATION 001032 assert( isTemp==0 || isTemp==1 ); 001033 assert( isView==0 || isView==1 ); 001034 { 001035 static const u8 aCode[] = { 001036 SQLITE_CREATE_TABLE, 001037 SQLITE_CREATE_TEMP_TABLE, 001038 SQLITE_CREATE_VIEW, 001039 SQLITE_CREATE_TEMP_VIEW 001040 }; 001041 char *zDb = db->aDb[iDb].zDbSName; 001042 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ 001043 goto begin_table_error; 001044 } 001045 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView], 001046 zName, 0, zDb) ){ 001047 goto begin_table_error; 001048 } 001049 } 001050 #endif 001051 001052 /* Make sure the new table name does not collide with an existing 001053 ** index or table name in the same database. Issue an error message if 001054 ** it does. The exception is if the statement being parsed was passed 001055 ** to an sqlite3_declare_vtab() call. In that case only the column names 001056 ** and types will be used, so there is no need to test for namespace 001057 ** collisions. 001058 */ 001059 if( !IN_SPECIAL_PARSE ){ 001060 char *zDb = db->aDb[iDb].zDbSName; 001061 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 001062 goto begin_table_error; 001063 } 001064 pTable = sqlite3FindTable(db, zName, zDb); 001065 if( pTable ){ 001066 if( !noErr ){ 001067 sqlite3ErrorMsg(pParse, "table %T already exists", pName); 001068 }else{ 001069 assert( !db->init.busy || CORRUPT_DB ); 001070 sqlite3CodeVerifySchema(pParse, iDb); 001071 } 001072 goto begin_table_error; 001073 } 001074 if( sqlite3FindIndex(db, zName, zDb)!=0 ){ 001075 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); 001076 goto begin_table_error; 001077 } 001078 } 001079 001080 pTable = sqlite3DbMallocZero(db, sizeof(Table)); 001081 if( pTable==0 ){ 001082 assert( db->mallocFailed ); 001083 pParse->rc = SQLITE_NOMEM_BKPT; 001084 pParse->nErr++; 001085 goto begin_table_error; 001086 } 001087 pTable->zName = zName; 001088 pTable->iPKey = -1; 001089 pTable->pSchema = db->aDb[iDb].pSchema; 001090 pTable->nTabRef = 1; 001091 #ifdef SQLITE_DEFAULT_ROWEST 001092 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST); 001093 #else 001094 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); 001095 #endif 001096 assert( pParse->pNewTable==0 ); 001097 pParse->pNewTable = pTable; 001098 001099 /* If this is the magic sqlite_sequence table used by autoincrement, 001100 ** then record a pointer to this table in the main database structure 001101 ** so that INSERT can find the table easily. 001102 */ 001103 #ifndef SQLITE_OMIT_AUTOINCREMENT 001104 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ 001105 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 001106 pTable->pSchema->pSeqTab = pTable; 001107 } 001108 #endif 001109 001110 /* Begin generating the code that will insert the table record into 001111 ** the SQLITE_MASTER table. Note in particular that we must go ahead 001112 ** and allocate the record number for the table entry now. Before any 001113 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause 001114 ** indices to be created and the table record must come before the 001115 ** indices. Hence, the record number for the table must be allocated 001116 ** now. 001117 */ 001118 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ 001119 int addr1; 001120 int fileFormat; 001121 int reg1, reg2, reg3; 001122 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ 001123 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 }; 001124 sqlite3BeginWriteOperation(pParse, 1, iDb); 001125 001126 #ifndef SQLITE_OMIT_VIRTUALTABLE 001127 if( isVirtual ){ 001128 sqlite3VdbeAddOp0(v, OP_VBegin); 001129 } 001130 #endif 001131 001132 /* If the file format and encoding in the database have not been set, 001133 ** set them now. 001134 */ 001135 reg1 = pParse->regRowid = ++pParse->nMem; 001136 reg2 = pParse->regRoot = ++pParse->nMem; 001137 reg3 = ++pParse->nMem; 001138 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); 001139 sqlite3VdbeUsesBtree(v, iDb); 001140 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); 001141 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? 001142 1 : SQLITE_MAX_FILE_FORMAT; 001143 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat); 001144 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); 001145 sqlite3VdbeJumpHere(v, addr1); 001146 001147 /* This just creates a place-holder record in the sqlite_master table. 001148 ** The record created does not contain anything yet. It will be replaced 001149 ** by the real entry in code generated at sqlite3EndTable(). 001150 ** 001151 ** The rowid for the new entry is left in register pParse->regRowid. 001152 ** The root page number of the new table is left in reg pParse->regRoot. 001153 ** The rowid and root page number values are needed by the code that 001154 ** sqlite3EndTable will generate. 001155 */ 001156 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) 001157 if( isView || isVirtual ){ 001158 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); 001159 }else 001160 #endif 001161 { 001162 pParse->addrCrTab = 001163 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY); 001164 } 001165 sqlite3OpenMasterTable(pParse, iDb); 001166 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); 001167 sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); 001168 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); 001169 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); 001170 sqlite3VdbeAddOp0(v, OP_Close); 001171 } 001172 001173 /* Normal (non-error) return. */ 001174 return; 001175 001176 /* If an error occurs, we jump here */ 001177 begin_table_error: 001178 sqlite3DbFree(db, zName); 001179 return; 001180 } 001181 001182 /* Set properties of a table column based on the (magical) 001183 ** name of the column. 001184 */ 001185 #if SQLITE_ENABLE_HIDDEN_COLUMNS 001186 void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ 001187 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){ 001188 pCol->colFlags |= COLFLAG_HIDDEN; 001189 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ 001190 pTab->tabFlags |= TF_OOOHidden; 001191 } 001192 } 001193 #endif 001194 001195 001196 /* 001197 ** Add a new column to the table currently being constructed. 001198 ** 001199 ** The parser calls this routine once for each column declaration 001200 ** in a CREATE TABLE statement. sqlite3StartTable() gets called 001201 ** first to get things going. Then this routine is called for each 001202 ** column. 001203 */ 001204 void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){ 001205 Table *p; 001206 int i; 001207 char *z; 001208 char *zType; 001209 Column *pCol; 001210 sqlite3 *db = pParse->db; 001211 if( (p = pParse->pNewTable)==0 ) return; 001212 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ 001213 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); 001214 return; 001215 } 001216 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2); 001217 if( z==0 ) return; 001218 if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName); 001219 memcpy(z, pName->z, pName->n); 001220 z[pName->n] = 0; 001221 sqlite3Dequote(z); 001222 for(i=0; i<p->nCol; i++){ 001223 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){ 001224 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); 001225 sqlite3DbFree(db, z); 001226 return; 001227 } 001228 } 001229 if( (p->nCol & 0x7)==0 ){ 001230 Column *aNew; 001231 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); 001232 if( aNew==0 ){ 001233 sqlite3DbFree(db, z); 001234 return; 001235 } 001236 p->aCol = aNew; 001237 } 001238 pCol = &p->aCol[p->nCol]; 001239 memset(pCol, 0, sizeof(p->aCol[0])); 001240 pCol->zName = z; 001241 sqlite3ColumnPropertiesFromName(p, pCol); 001242 001243 if( pType->n==0 ){ 001244 /* If there is no type specified, columns have the default affinity 001245 ** 'BLOB' with a default size of 4 bytes. */ 001246 pCol->affinity = SQLITE_AFF_BLOB; 001247 pCol->szEst = 1; 001248 #ifdef SQLITE_ENABLE_SORTER_REFERENCES 001249 if( 4>=sqlite3GlobalConfig.szSorterRef ){ 001250 pCol->colFlags |= COLFLAG_SORTERREF; 001251 } 001252 #endif 001253 }else{ 001254 zType = z + sqlite3Strlen30(z) + 1; 001255 memcpy(zType, pType->z, pType->n); 001256 zType[pType->n] = 0; 001257 sqlite3Dequote(zType); 001258 pCol->affinity = sqlite3AffinityType(zType, pCol); 001259 pCol->colFlags |= COLFLAG_HASTYPE; 001260 } 001261 p->nCol++; 001262 p->nNVCol++; 001263 pParse->constraintName.n = 0; 001264 } 001265 001266 /* 001267 ** This routine is called by the parser while in the middle of 001268 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has 001269 ** been seen on a column. This routine sets the notNull flag on 001270 ** the column currently under construction. 001271 */ 001272 void sqlite3AddNotNull(Parse *pParse, int onError){ 001273 Table *p; 001274 Column *pCol; 001275 p = pParse->pNewTable; 001276 if( p==0 || NEVER(p->nCol<1) ) return; 001277 pCol = &p->aCol[p->nCol-1]; 001278 pCol->notNull = (u8)onError; 001279 p->tabFlags |= TF_HasNotNull; 001280 001281 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created 001282 ** on this column. */ 001283 if( pCol->colFlags & COLFLAG_UNIQUE ){ 001284 Index *pIdx; 001285 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ 001286 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None ); 001287 if( pIdx->aiColumn[0]==p->nCol-1 ){ 001288 pIdx->uniqNotNull = 1; 001289 } 001290 } 001291 } 001292 } 001293 001294 /* 001295 ** Scan the column type name zType (length nType) and return the 001296 ** associated affinity type. 001297 ** 001298 ** This routine does a case-independent search of zType for the 001299 ** substrings in the following table. If one of the substrings is 001300 ** found, the corresponding affinity is returned. If zType contains 001301 ** more than one of the substrings, entries toward the top of 001302 ** the table take priority. For example, if zType is 'BLOBINT', 001303 ** SQLITE_AFF_INTEGER is returned. 001304 ** 001305 ** Substring | Affinity 001306 ** -------------------------------- 001307 ** 'INT' | SQLITE_AFF_INTEGER 001308 ** 'CHAR' | SQLITE_AFF_TEXT 001309 ** 'CLOB' | SQLITE_AFF_TEXT 001310 ** 'TEXT' | SQLITE_AFF_TEXT 001311 ** 'BLOB' | SQLITE_AFF_BLOB 001312 ** 'REAL' | SQLITE_AFF_REAL 001313 ** 'FLOA' | SQLITE_AFF_REAL 001314 ** 'DOUB' | SQLITE_AFF_REAL 001315 ** 001316 ** If none of the substrings in the above table are found, 001317 ** SQLITE_AFF_NUMERIC is returned. 001318 */ 001319 char sqlite3AffinityType(const char *zIn, Column *pCol){ 001320 u32 h = 0; 001321 char aff = SQLITE_AFF_NUMERIC; 001322 const char *zChar = 0; 001323 001324 assert( zIn!=0 ); 001325 while( zIn[0] ){ 001326 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; 001327 zIn++; 001328 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ 001329 aff = SQLITE_AFF_TEXT; 001330 zChar = zIn; 001331 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ 001332 aff = SQLITE_AFF_TEXT; 001333 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ 001334 aff = SQLITE_AFF_TEXT; 001335 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ 001336 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ 001337 aff = SQLITE_AFF_BLOB; 001338 if( zIn[0]=='(' ) zChar = zIn; 001339 #ifndef SQLITE_OMIT_FLOATING_POINT 001340 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ 001341 && aff==SQLITE_AFF_NUMERIC ){ 001342 aff = SQLITE_AFF_REAL; 001343 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ 001344 && aff==SQLITE_AFF_NUMERIC ){ 001345 aff = SQLITE_AFF_REAL; 001346 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ 001347 && aff==SQLITE_AFF_NUMERIC ){ 001348 aff = SQLITE_AFF_REAL; 001349 #endif 001350 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ 001351 aff = SQLITE_AFF_INTEGER; 001352 break; 001353 } 001354 } 001355 001356 /* If pCol is not NULL, store an estimate of the field size. The 001357 ** estimate is scaled so that the size of an integer is 1. */ 001358 if( pCol ){ 001359 int v = 0; /* default size is approx 4 bytes */ 001360 if( aff<SQLITE_AFF_NUMERIC ){ 001361 if( zChar ){ 001362 while( zChar[0] ){ 001363 if( sqlite3Isdigit(zChar[0]) ){ 001364 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */ 001365 sqlite3GetInt32(zChar, &v); 001366 break; 001367 } 001368 zChar++; 001369 } 001370 }else{ 001371 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ 001372 } 001373 } 001374 #ifdef SQLITE_ENABLE_SORTER_REFERENCES 001375 if( v>=sqlite3GlobalConfig.szSorterRef ){ 001376 pCol->colFlags |= COLFLAG_SORTERREF; 001377 } 001378 #endif 001379 v = v/4 + 1; 001380 if( v>255 ) v = 255; 001381 pCol->szEst = v; 001382 } 001383 return aff; 001384 } 001385 001386 /* 001387 ** The expression is the default value for the most recently added column 001388 ** of the table currently under construction. 001389 ** 001390 ** Default value expressions must be constant. Raise an exception if this 001391 ** is not the case. 001392 ** 001393 ** This routine is called by the parser while in the middle of 001394 ** parsing a CREATE TABLE statement. 001395 */ 001396 void sqlite3AddDefaultValue( 001397 Parse *pParse, /* Parsing context */ 001398 Expr *pExpr, /* The parsed expression of the default value */ 001399 const char *zStart, /* Start of the default value text */ 001400 const char *zEnd /* First character past end of defaut value text */ 001401 ){ 001402 Table *p; 001403 Column *pCol; 001404 sqlite3 *db = pParse->db; 001405 p = pParse->pNewTable; 001406 if( p!=0 ){ 001407 pCol = &(p->aCol[p->nCol-1]); 001408 if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){ 001409 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", 001410 pCol->zName); 001411 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 001412 }else if( pCol->colFlags & COLFLAG_GENERATED ){ 001413 testcase( pCol->colFlags & COLFLAG_VIRTUAL ); 001414 testcase( pCol->colFlags & COLFLAG_STORED ); 001415 sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column"); 001416 #endif 001417 }else{ 001418 /* A copy of pExpr is used instead of the original, as pExpr contains 001419 ** tokens that point to volatile memory. 001420 */ 001421 Expr x; 001422 sqlite3ExprDelete(db, pCol->pDflt); 001423 memset(&x, 0, sizeof(x)); 001424 x.op = TK_SPAN; 001425 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd); 001426 x.pLeft = pExpr; 001427 x.flags = EP_Skip; 001428 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); 001429 sqlite3DbFree(db, x.u.zToken); 001430 } 001431 } 001432 if( IN_RENAME_OBJECT ){ 001433 sqlite3RenameExprUnmap(pParse, pExpr); 001434 } 001435 sqlite3ExprDelete(db, pExpr); 001436 } 001437 001438 /* 001439 ** Backwards Compatibility Hack: 001440 ** 001441 ** Historical versions of SQLite accepted strings as column names in 001442 ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: 001443 ** 001444 ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) 001445 ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); 001446 ** 001447 ** This is goofy. But to preserve backwards compatibility we continue to 001448 ** accept it. This routine does the necessary conversion. It converts 001449 ** the expression given in its argument from a TK_STRING into a TK_ID 001450 ** if the expression is just a TK_STRING with an optional COLLATE clause. 001451 ** If the expression is anything other than TK_STRING, the expression is 001452 ** unchanged. 001453 */ 001454 static void sqlite3StringToId(Expr *p){ 001455 if( p->op==TK_STRING ){ 001456 p->op = TK_ID; 001457 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){ 001458 p->pLeft->op = TK_ID; 001459 } 001460 } 001461 001462 /* 001463 ** Tag the given column as being part of the PRIMARY KEY 001464 */ 001465 static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){ 001466 pCol->colFlags |= COLFLAG_PRIMKEY; 001467 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 001468 if( pCol->colFlags & COLFLAG_GENERATED ){ 001469 testcase( pCol->colFlags & COLFLAG_VIRTUAL ); 001470 testcase( pCol->colFlags & COLFLAG_STORED ); 001471 sqlite3ErrorMsg(pParse, 001472 "generated columns cannot be part of the PRIMARY KEY"); 001473 } 001474 #endif 001475 } 001476 001477 /* 001478 ** Designate the PRIMARY KEY for the table. pList is a list of names 001479 ** of columns that form the primary key. If pList is NULL, then the 001480 ** most recently added column of the table is the primary key. 001481 ** 001482 ** A table can have at most one primary key. If the table already has 001483 ** a primary key (and this is the second primary key) then create an 001484 ** error. 001485 ** 001486 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, 001487 ** then we will try to use that column as the rowid. Set the Table.iPKey 001488 ** field of the table under construction to be the index of the 001489 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is 001490 ** no INTEGER PRIMARY KEY. 001491 ** 001492 ** If the key is not an INTEGER PRIMARY KEY, then create a unique 001493 ** index for the key. No index is created for INTEGER PRIMARY KEYs. 001494 */ 001495 void sqlite3AddPrimaryKey( 001496 Parse *pParse, /* Parsing context */ 001497 ExprList *pList, /* List of field names to be indexed */ 001498 int onError, /* What to do with a uniqueness conflict */ 001499 int autoInc, /* True if the AUTOINCREMENT keyword is present */ 001500 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ 001501 ){ 001502 Table *pTab = pParse->pNewTable; 001503 Column *pCol = 0; 001504 int iCol = -1, i; 001505 int nTerm; 001506 if( pTab==0 ) goto primary_key_exit; 001507 if( pTab->tabFlags & TF_HasPrimaryKey ){ 001508 sqlite3ErrorMsg(pParse, 001509 "table \"%s\" has more than one primary key", pTab->zName); 001510 goto primary_key_exit; 001511 } 001512 pTab->tabFlags |= TF_HasPrimaryKey; 001513 if( pList==0 ){ 001514 iCol = pTab->nCol - 1; 001515 pCol = &pTab->aCol[iCol]; 001516 makeColumnPartOfPrimaryKey(pParse, pCol); 001517 nTerm = 1; 001518 }else{ 001519 nTerm = pList->nExpr; 001520 for(i=0; i<nTerm; i++){ 001521 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); 001522 assert( pCExpr!=0 ); 001523 sqlite3StringToId(pCExpr); 001524 if( pCExpr->op==TK_ID ){ 001525 const char *zCName = pCExpr->u.zToken; 001526 for(iCol=0; iCol<pTab->nCol; iCol++){ 001527 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){ 001528 pCol = &pTab->aCol[iCol]; 001529 makeColumnPartOfPrimaryKey(pParse, pCol); 001530 break; 001531 } 001532 } 001533 } 001534 } 001535 } 001536 if( nTerm==1 001537 && pCol 001538 && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0 001539 && sortOrder!=SQLITE_SO_DESC 001540 ){ 001541 if( IN_RENAME_OBJECT && pList ){ 001542 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr); 001543 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr); 001544 } 001545 pTab->iPKey = iCol; 001546 pTab->keyConf = (u8)onError; 001547 assert( autoInc==0 || autoInc==1 ); 001548 pTab->tabFlags |= autoInc*TF_Autoincrement; 001549 if( pList ) pParse->iPkSortOrder = pList->a[0].sortFlags; 001550 (void)sqlite3HasExplicitNulls(pParse, pList); 001551 }else if( autoInc ){ 001552 #ifndef SQLITE_OMIT_AUTOINCREMENT 001553 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " 001554 "INTEGER PRIMARY KEY"); 001555 #endif 001556 }else{ 001557 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 001558 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY); 001559 pList = 0; 001560 } 001561 001562 primary_key_exit: 001563 sqlite3ExprListDelete(pParse->db, pList); 001564 return; 001565 } 001566 001567 /* 001568 ** Add a new CHECK constraint to the table currently under construction. 001569 */ 001570 void sqlite3AddCheckConstraint( 001571 Parse *pParse, /* Parsing context */ 001572 Expr *pCheckExpr /* The check expression */ 001573 ){ 001574 #ifndef SQLITE_OMIT_CHECK 001575 Table *pTab = pParse->pNewTable; 001576 sqlite3 *db = pParse->db; 001577 if( pTab && !IN_DECLARE_VTAB 001578 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt) 001579 ){ 001580 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr); 001581 if( pParse->constraintName.n ){ 001582 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1); 001583 } 001584 }else 001585 #endif 001586 { 001587 sqlite3ExprDelete(pParse->db, pCheckExpr); 001588 } 001589 } 001590 001591 /* 001592 ** Set the collation function of the most recently parsed table column 001593 ** to the CollSeq given. 001594 */ 001595 void sqlite3AddCollateType(Parse *pParse, Token *pToken){ 001596 Table *p; 001597 int i; 001598 char *zColl; /* Dequoted name of collation sequence */ 001599 sqlite3 *db; 001600 001601 if( (p = pParse->pNewTable)==0 ) return; 001602 i = p->nCol-1; 001603 db = pParse->db; 001604 zColl = sqlite3NameFromToken(db, pToken); 001605 if( !zColl ) return; 001606 001607 if( sqlite3LocateCollSeq(pParse, zColl) ){ 001608 Index *pIdx; 001609 sqlite3DbFree(db, p->aCol[i].zColl); 001610 p->aCol[i].zColl = zColl; 001611 001612 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", 001613 ** then an index may have been created on this column before the 001614 ** collation type was added. Correct this if it is the case. 001615 */ 001616 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ 001617 assert( pIdx->nKeyCol==1 ); 001618 if( pIdx->aiColumn[0]==i ){ 001619 pIdx->azColl[0] = p->aCol[i].zColl; 001620 } 001621 } 001622 }else{ 001623 sqlite3DbFree(db, zColl); 001624 } 001625 } 001626 001627 /* Change the most recently parsed column to be a GENERATED ALWAYS AS 001628 ** column. 001629 */ 001630 void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){ 001631 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 001632 u8 eType = COLFLAG_VIRTUAL; 001633 Table *pTab = pParse->pNewTable; 001634 Column *pCol; 001635 if( pTab==0 ){ 001636 /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */ 001637 goto generated_done; 001638 } 001639 pCol = &(pTab->aCol[pTab->nCol-1]); 001640 if( IN_DECLARE_VTAB ){ 001641 sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns"); 001642 goto generated_done; 001643 } 001644 if( pCol->pDflt ) goto generated_error; 001645 if( pType ){ 001646 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){ 001647 /* no-op */ 001648 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){ 001649 eType = COLFLAG_STORED; 001650 }else{ 001651 goto generated_error; 001652 } 001653 } 001654 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--; 001655 pCol->colFlags |= eType; 001656 assert( TF_HasVirtual==COLFLAG_VIRTUAL ); 001657 assert( TF_HasStored==COLFLAG_STORED ); 001658 pTab->tabFlags |= eType; 001659 if( pCol->colFlags & COLFLAG_PRIMKEY ){ 001660 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */ 001661 } 001662 pCol->pDflt = pExpr; 001663 pExpr = 0; 001664 goto generated_done; 001665 001666 generated_error: 001667 sqlite3ErrorMsg(pParse, "error in generated column \"%s\"", 001668 pCol->zName); 001669 generated_done: 001670 sqlite3ExprDelete(pParse->db, pExpr); 001671 #else 001672 /* Throw and error for the GENERATED ALWAYS AS clause if the 001673 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */ 001674 sqlite3ErrorMsg(pParse, "generated columns not supported"); 001675 sqlite3ExprDelete(pParse->db, pExpr); 001676 #endif 001677 } 001678 001679 /* 001680 ** Generate code that will increment the schema cookie. 001681 ** 001682 ** The schema cookie is used to determine when the schema for the 001683 ** database changes. After each schema change, the cookie value 001684 ** changes. When a process first reads the schema it records the 001685 ** cookie. Thereafter, whenever it goes to access the database, 001686 ** it checks the cookie to make sure the schema has not changed 001687 ** since it was last read. 001688 ** 001689 ** This plan is not completely bullet-proof. It is possible for 001690 ** the schema to change multiple times and for the cookie to be 001691 ** set back to prior value. But schema changes are infrequent 001692 ** and the probability of hitting the same cookie value is only 001693 ** 1 chance in 2^32. So we're safe enough. 001694 ** 001695 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments 001696 ** the schema-version whenever the schema changes. 001697 */ 001698 void sqlite3ChangeCookie(Parse *pParse, int iDb){ 001699 sqlite3 *db = pParse->db; 001700 Vdbe *v = pParse->pVdbe; 001701 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 001702 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, 001703 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie)); 001704 } 001705 001706 /* 001707 ** Measure the number of characters needed to output the given 001708 ** identifier. The number returned includes any quotes used 001709 ** but does not include the null terminator. 001710 ** 001711 ** The estimate is conservative. It might be larger that what is 001712 ** really needed. 001713 */ 001714 static int identLength(const char *z){ 001715 int n; 001716 for(n=0; *z; n++, z++){ 001717 if( *z=='"' ){ n++; } 001718 } 001719 return n + 2; 001720 } 001721 001722 /* 001723 ** The first parameter is a pointer to an output buffer. The second 001724 ** parameter is a pointer to an integer that contains the offset at 001725 ** which to write into the output buffer. This function copies the 001726 ** nul-terminated string pointed to by the third parameter, zSignedIdent, 001727 ** to the specified offset in the buffer and updates *pIdx to refer 001728 ** to the first byte after the last byte written before returning. 001729 ** 001730 ** If the string zSignedIdent consists entirely of alpha-numeric 001731 ** characters, does not begin with a digit and is not an SQL keyword, 001732 ** then it is copied to the output buffer exactly as it is. Otherwise, 001733 ** it is quoted using double-quotes. 001734 */ 001735 static void identPut(char *z, int *pIdx, char *zSignedIdent){ 001736 unsigned char *zIdent = (unsigned char*)zSignedIdent; 001737 int i, j, needQuote; 001738 i = *pIdx; 001739 001740 for(j=0; zIdent[j]; j++){ 001741 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break; 001742 } 001743 needQuote = sqlite3Isdigit(zIdent[0]) 001744 || sqlite3KeywordCode(zIdent, j)!=TK_ID 001745 || zIdent[j]!=0 001746 || j==0; 001747 001748 if( needQuote ) z[i++] = '"'; 001749 for(j=0; zIdent[j]; j++){ 001750 z[i++] = zIdent[j]; 001751 if( zIdent[j]=='"' ) z[i++] = '"'; 001752 } 001753 if( needQuote ) z[i++] = '"'; 001754 z[i] = 0; 001755 *pIdx = i; 001756 } 001757 001758 /* 001759 ** Generate a CREATE TABLE statement appropriate for the given 001760 ** table. Memory to hold the text of the statement is obtained 001761 ** from sqliteMalloc() and must be freed by the calling function. 001762 */ 001763 static char *createTableStmt(sqlite3 *db, Table *p){ 001764 int i, k, n; 001765 char *zStmt; 001766 char *zSep, *zSep2, *zEnd; 001767 Column *pCol; 001768 n = 0; 001769 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){ 001770 n += identLength(pCol->zName) + 5; 001771 } 001772 n += identLength(p->zName); 001773 if( n<50 ){ 001774 zSep = ""; 001775 zSep2 = ","; 001776 zEnd = ")"; 001777 }else{ 001778 zSep = "\n "; 001779 zSep2 = ",\n "; 001780 zEnd = "\n)"; 001781 } 001782 n += 35 + 6*p->nCol; 001783 zStmt = sqlite3DbMallocRaw(0, n); 001784 if( zStmt==0 ){ 001785 sqlite3OomFault(db); 001786 return 0; 001787 } 001788 sqlite3_snprintf(n, zStmt, "CREATE TABLE "); 001789 k = sqlite3Strlen30(zStmt); 001790 identPut(zStmt, &k, p->zName); 001791 zStmt[k++] = '('; 001792 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ 001793 static const char * const azType[] = { 001794 /* SQLITE_AFF_BLOB */ "", 001795 /* SQLITE_AFF_TEXT */ " TEXT", 001796 /* SQLITE_AFF_NUMERIC */ " NUM", 001797 /* SQLITE_AFF_INTEGER */ " INT", 001798 /* SQLITE_AFF_REAL */ " REAL" 001799 }; 001800 int len; 001801 const char *zType; 001802 001803 sqlite3_snprintf(n-k, &zStmt[k], zSep); 001804 k += sqlite3Strlen30(&zStmt[k]); 001805 zSep = zSep2; 001806 identPut(zStmt, &k, pCol->zName); 001807 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); 001808 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); 001809 testcase( pCol->affinity==SQLITE_AFF_BLOB ); 001810 testcase( pCol->affinity==SQLITE_AFF_TEXT ); 001811 testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); 001812 testcase( pCol->affinity==SQLITE_AFF_INTEGER ); 001813 testcase( pCol->affinity==SQLITE_AFF_REAL ); 001814 001815 zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; 001816 len = sqlite3Strlen30(zType); 001817 assert( pCol->affinity==SQLITE_AFF_BLOB 001818 || pCol->affinity==sqlite3AffinityType(zType, 0) ); 001819 memcpy(&zStmt[k], zType, len); 001820 k += len; 001821 assert( k<=n ); 001822 } 001823 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); 001824 return zStmt; 001825 } 001826 001827 /* 001828 ** Resize an Index object to hold N columns total. Return SQLITE_OK 001829 ** on success and SQLITE_NOMEM on an OOM error. 001830 */ 001831 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ 001832 char *zExtra; 001833 int nByte; 001834 if( pIdx->nColumn>=N ) return SQLITE_OK; 001835 assert( pIdx->isResized==0 ); 001836 nByte = (sizeof(char*) + sizeof(i16) + 1)*N; 001837 zExtra = sqlite3DbMallocZero(db, nByte); 001838 if( zExtra==0 ) return SQLITE_NOMEM_BKPT; 001839 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); 001840 pIdx->azColl = (const char**)zExtra; 001841 zExtra += sizeof(char*)*N; 001842 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); 001843 pIdx->aiColumn = (i16*)zExtra; 001844 zExtra += sizeof(i16)*N; 001845 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); 001846 pIdx->aSortOrder = (u8*)zExtra; 001847 pIdx->nColumn = N; 001848 pIdx->isResized = 1; 001849 return SQLITE_OK; 001850 } 001851 001852 /* 001853 ** Estimate the total row width for a table. 001854 */ 001855 static void estimateTableWidth(Table *pTab){ 001856 unsigned wTable = 0; 001857 const Column *pTabCol; 001858 int i; 001859 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){ 001860 wTable += pTabCol->szEst; 001861 } 001862 if( pTab->iPKey<0 ) wTable++; 001863 pTab->szTabRow = sqlite3LogEst(wTable*4); 001864 } 001865 001866 /* 001867 ** Estimate the average size of a row for an index. 001868 */ 001869 static void estimateIndexWidth(Index *pIdx){ 001870 unsigned wIndex = 0; 001871 int i; 001872 const Column *aCol = pIdx->pTable->aCol; 001873 for(i=0; i<pIdx->nColumn; i++){ 001874 i16 x = pIdx->aiColumn[i]; 001875 assert( x<pIdx->pTable->nCol ); 001876 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst; 001877 } 001878 pIdx->szIdxRow = sqlite3LogEst(wIndex*4); 001879 } 001880 001881 /* Return true if column number x is any of the first nCol entries of aiCol[]. 001882 ** This is used to determine if the column number x appears in any of the 001883 ** first nCol entries of an index. 001884 */ 001885 static int hasColumn(const i16 *aiCol, int nCol, int x){ 001886 while( nCol-- > 0 ){ 001887 assert( aiCol[0]>=0 ); 001888 if( x==*(aiCol++) ){ 001889 return 1; 001890 } 001891 } 001892 return 0; 001893 } 001894 001895 /* 001896 ** Return true if any of the first nKey entries of index pIdx exactly 001897 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID 001898 ** PRIMARY KEY index. pIdx is an index on the same table. pIdx may 001899 ** or may not be the same index as pPk. 001900 ** 001901 ** The first nKey entries of pIdx are guaranteed to be ordinary columns, 001902 ** not a rowid or expression. 001903 ** 001904 ** This routine differs from hasColumn() in that both the column and the 001905 ** collating sequence must match for this routine, but for hasColumn() only 001906 ** the column name must match. 001907 */ 001908 static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){ 001909 int i, j; 001910 assert( nKey<=pIdx->nColumn ); 001911 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) ); 001912 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY ); 001913 assert( pPk->pTable->tabFlags & TF_WithoutRowid ); 001914 assert( pPk->pTable==pIdx->pTable ); 001915 testcase( pPk==pIdx ); 001916 j = pPk->aiColumn[iCol]; 001917 assert( j!=XN_ROWID && j!=XN_EXPR ); 001918 for(i=0; i<nKey; i++){ 001919 assert( pIdx->aiColumn[i]>=0 || j>=0 ); 001920 if( pIdx->aiColumn[i]==j 001921 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0 001922 ){ 001923 return 1; 001924 } 001925 } 001926 return 0; 001927 } 001928 001929 /* Recompute the colNotIdxed field of the Index. 001930 ** 001931 ** colNotIdxed is a bitmask that has a 0 bit representing each indexed 001932 ** columns that are within the first 63 columns of the table. The 001933 ** high-order bit of colNotIdxed is always 1. All unindexed columns 001934 ** of the table have a 1. 001935 ** 001936 ** 2019-10-24: For the purpose of this computation, virtual columns are 001937 ** not considered to be covered by the index, even if they are in the 001938 ** index, because we do not trust the logic in whereIndexExprTrans() to be 001939 ** able to find all instances of a reference to the indexed table column 001940 ** and convert them into references to the index. Hence we always want 001941 ** the actual table at hand in order to recompute the virtual column, if 001942 ** necessary. 001943 ** 001944 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask 001945 ** to determine if the index is covering index. 001946 */ 001947 static void recomputeColumnsNotIndexed(Index *pIdx){ 001948 Bitmask m = 0; 001949 int j; 001950 Table *pTab = pIdx->pTable; 001951 for(j=pIdx->nColumn-1; j>=0; j--){ 001952 int x = pIdx->aiColumn[j]; 001953 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){ 001954 testcase( x==BMS-1 ); 001955 testcase( x==BMS-2 ); 001956 if( x<BMS-1 ) m |= MASKBIT(x); 001957 } 001958 } 001959 pIdx->colNotIdxed = ~m; 001960 assert( (pIdx->colNotIdxed>>63)==1 ); 001961 } 001962 001963 /* 001964 ** This routine runs at the end of parsing a CREATE TABLE statement that 001965 ** has a WITHOUT ROWID clause. The job of this routine is to convert both 001966 ** internal schema data structures and the generated VDBE code so that they 001967 ** are appropriate for a WITHOUT ROWID table instead of a rowid table. 001968 ** Changes include: 001969 ** 001970 ** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL. 001971 ** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY 001972 ** into BTREE_BLOBKEY. 001973 ** (3) Bypass the creation of the sqlite_master table entry 001974 ** for the PRIMARY KEY as the primary key index is now 001975 ** identified by the sqlite_master table entry of the table itself. 001976 ** (4) Set the Index.tnum of the PRIMARY KEY Index object in the 001977 ** schema to the rootpage from the main table. 001978 ** (5) Add all table columns to the PRIMARY KEY Index object 001979 ** so that the PRIMARY KEY is a covering index. The surplus 001980 ** columns are part of KeyInfo.nAllField and are not used for 001981 ** sorting or lookup or uniqueness checks. 001982 ** (6) Replace the rowid tail on all automatically generated UNIQUE 001983 ** indices with the PRIMARY KEY columns. 001984 ** 001985 ** For virtual tables, only (1) is performed. 001986 */ 001987 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ 001988 Index *pIdx; 001989 Index *pPk; 001990 int nPk; 001991 int nExtra; 001992 int i, j; 001993 sqlite3 *db = pParse->db; 001994 Vdbe *v = pParse->pVdbe; 001995 001996 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) 001997 */ 001998 if( !db->init.imposterTable ){ 001999 for(i=0; i<pTab->nCol; i++){ 002000 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){ 002001 pTab->aCol[i].notNull = OE_Abort; 002002 } 002003 } 002004 pTab->tabFlags |= TF_HasNotNull; 002005 } 002006 002007 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY 002008 ** into BTREE_BLOBKEY. 002009 */ 002010 if( pParse->addrCrTab ){ 002011 assert( v ); 002012 sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY); 002013 } 002014 002015 /* Locate the PRIMARY KEY index. Or, if this table was originally 002016 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. 002017 */ 002018 if( pTab->iPKey>=0 ){ 002019 ExprList *pList; 002020 Token ipkToken; 002021 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName); 002022 pList = sqlite3ExprListAppend(pParse, 0, 002023 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); 002024 if( pList==0 ) return; 002025 if( IN_RENAME_OBJECT ){ 002026 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey); 002027 } 002028 pList->a[0].sortFlags = pParse->iPkSortOrder; 002029 assert( pParse->pNewTable==pTab ); 002030 pTab->iPKey = -1; 002031 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, 002032 SQLITE_IDXTYPE_PRIMARYKEY); 002033 if( db->mallocFailed || pParse->nErr ) return; 002034 pPk = sqlite3PrimaryKeyIndex(pTab); 002035 assert( pPk->nKeyCol==1 ); 002036 }else{ 002037 pPk = sqlite3PrimaryKeyIndex(pTab); 002038 assert( pPk!=0 ); 002039 002040 /* 002041 ** Remove all redundant columns from the PRIMARY KEY. For example, change 002042 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later 002043 ** code assumes the PRIMARY KEY contains no repeated columns. 002044 */ 002045 for(i=j=1; i<pPk->nKeyCol; i++){ 002046 if( isDupColumn(pPk, j, pPk, i) ){ 002047 pPk->nColumn--; 002048 }else{ 002049 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ); 002050 pPk->azColl[j] = pPk->azColl[i]; 002051 pPk->aSortOrder[j] = pPk->aSortOrder[i]; 002052 pPk->aiColumn[j++] = pPk->aiColumn[i]; 002053 } 002054 } 002055 pPk->nKeyCol = j; 002056 } 002057 assert( pPk!=0 ); 002058 pPk->isCovering = 1; 002059 if( !db->init.imposterTable ) pPk->uniqNotNull = 1; 002060 nPk = pPk->nColumn = pPk->nKeyCol; 002061 002062 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master 002063 ** table entry. This is only required if currently generating VDBE 002064 ** code for a CREATE TABLE (not when parsing one as part of reading 002065 ** a database schema). */ 002066 if( v && pPk->tnum>0 ){ 002067 assert( db->init.busy==0 ); 002068 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto); 002069 } 002070 002071 /* The root page of the PRIMARY KEY is the table root page */ 002072 pPk->tnum = pTab->tnum; 002073 002074 /* Update the in-memory representation of all UNIQUE indices by converting 002075 ** the final rowid column into one or more columns of the PRIMARY KEY. 002076 */ 002077 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 002078 int n; 002079 if( IsPrimaryKeyIndex(pIdx) ) continue; 002080 for(i=n=0; i<nPk; i++){ 002081 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ 002082 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); 002083 n++; 002084 } 002085 } 002086 if( n==0 ){ 002087 /* This index is a superset of the primary key */ 002088 pIdx->nColumn = pIdx->nKeyCol; 002089 continue; 002090 } 002091 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return; 002092 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){ 002093 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ 002094 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); 002095 pIdx->aiColumn[j] = pPk->aiColumn[i]; 002096 pIdx->azColl[j] = pPk->azColl[i]; 002097 if( pPk->aSortOrder[i] ){ 002098 /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */ 002099 pIdx->bAscKeyBug = 1; 002100 } 002101 j++; 002102 } 002103 } 002104 assert( pIdx->nColumn>=pIdx->nKeyCol+n ); 002105 assert( pIdx->nColumn>=j ); 002106 } 002107 002108 /* Add all table columns to the PRIMARY KEY index 002109 */ 002110 nExtra = 0; 002111 for(i=0; i<pTab->nCol; i++){ 002112 if( !hasColumn(pPk->aiColumn, nPk, i) 002113 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++; 002114 } 002115 if( resizeIndexObject(db, pPk, nPk+nExtra) ) return; 002116 for(i=0, j=nPk; i<pTab->nCol; i++){ 002117 if( !hasColumn(pPk->aiColumn, j, i) 002118 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 002119 ){ 002120 assert( j<pPk->nColumn ); 002121 pPk->aiColumn[j] = i; 002122 pPk->azColl[j] = sqlite3StrBINARY; 002123 j++; 002124 } 002125 } 002126 assert( pPk->nColumn==j ); 002127 assert( pTab->nNVCol<=j ); 002128 recomputeColumnsNotIndexed(pPk); 002129 } 002130 002131 #ifndef SQLITE_OMIT_VIRTUALTABLE 002132 /* 002133 ** Return true if zName is a shadow table name in the current database 002134 ** connection. 002135 ** 002136 ** zName is temporarily modified while this routine is running, but is 002137 ** restored to its original value prior to this routine returning. 002138 */ 002139 int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ 002140 char *zTail; /* Pointer to the last "_" in zName */ 002141 Table *pTab; /* Table that zName is a shadow of */ 002142 Module *pMod; /* Module for the virtual table */ 002143 002144 zTail = strrchr(zName, '_'); 002145 if( zTail==0 ) return 0; 002146 *zTail = 0; 002147 pTab = sqlite3FindTable(db, zName, 0); 002148 *zTail = '_'; 002149 if( pTab==0 ) return 0; 002150 if( !IsVirtual(pTab) ) return 0; 002151 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]); 002152 if( pMod==0 ) return 0; 002153 if( pMod->pModule->iVersion<3 ) return 0; 002154 if( pMod->pModule->xShadowName==0 ) return 0; 002155 return pMod->pModule->xShadowName(zTail+1); 002156 } 002157 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ 002158 002159 /* 002160 ** This routine is called to report the final ")" that terminates 002161 ** a CREATE TABLE statement. 002162 ** 002163 ** The table structure that other action routines have been building 002164 ** is added to the internal hash tables, assuming no errors have 002165 ** occurred. 002166 ** 002167 ** An entry for the table is made in the master table on disk, unless 002168 ** this is a temporary table or db->init.busy==1. When db->init.busy==1 002169 ** it means we are reading the sqlite_master table because we just 002170 ** connected to the database or because the sqlite_master table has 002171 ** recently changed, so the entry for this table already exists in 002172 ** the sqlite_master table. We do not want to create it again. 002173 ** 002174 ** If the pSelect argument is not NULL, it means that this routine 002175 ** was called to create a table generated from a 002176 ** "CREATE TABLE ... AS SELECT ..." statement. The column names of 002177 ** the new table will match the result set of the SELECT. 002178 */ 002179 void sqlite3EndTable( 002180 Parse *pParse, /* Parse context */ 002181 Token *pCons, /* The ',' token after the last column defn. */ 002182 Token *pEnd, /* The ')' before options in the CREATE TABLE */ 002183 u8 tabOpts, /* Extra table options. Usually 0. */ 002184 Select *pSelect /* Select from a "CREATE ... AS SELECT" */ 002185 ){ 002186 Table *p; /* The new table */ 002187 sqlite3 *db = pParse->db; /* The database connection */ 002188 int iDb; /* Database in which the table lives */ 002189 Index *pIdx; /* An implied index of the table */ 002190 002191 if( pEnd==0 && pSelect==0 ){ 002192 return; 002193 } 002194 assert( !db->mallocFailed ); 002195 p = pParse->pNewTable; 002196 if( p==0 ) return; 002197 002198 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){ 002199 p->tabFlags |= TF_Shadow; 002200 } 002201 002202 /* If the db->init.busy is 1 it means we are reading the SQL off the 002203 ** "sqlite_master" or "sqlite_temp_master" table on the disk. 002204 ** So do not write to the disk again. Extract the root page number 002205 ** for the table from the db->init.newTnum field. (The page number 002206 ** should have been put there by the sqliteOpenCb routine.) 002207 ** 002208 ** If the root page number is 1, that means this is the sqlite_master 002209 ** table itself. So mark it read-only. 002210 */ 002211 if( db->init.busy ){ 002212 if( pSelect ){ 002213 sqlite3ErrorMsg(pParse, ""); 002214 return; 002215 } 002216 p->tnum = db->init.newTnum; 002217 if( p->tnum==1 ) p->tabFlags |= TF_Readonly; 002218 } 002219 002220 assert( (p->tabFlags & TF_HasPrimaryKey)==0 002221 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 ); 002222 assert( (p->tabFlags & TF_HasPrimaryKey)!=0 002223 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) ); 002224 002225 /* Special processing for WITHOUT ROWID Tables */ 002226 if( tabOpts & TF_WithoutRowid ){ 002227 if( (p->tabFlags & TF_Autoincrement) ){ 002228 sqlite3ErrorMsg(pParse, 002229 "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); 002230 return; 002231 } 002232 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ 002233 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); 002234 return; 002235 } 002236 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; 002237 convertToWithoutRowidTable(pParse, p); 002238 } 002239 iDb = sqlite3SchemaToIndex(db, p->pSchema); 002240 002241 #ifndef SQLITE_OMIT_CHECK 002242 /* Resolve names in all CHECK constraint expressions. 002243 */ 002244 if( p->pCheck ){ 002245 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck); 002246 if( pParse->nErr ){ 002247 /* If errors are seen, delete the CHECK constraints now, else they might 002248 ** actually be used if PRAGMA writable_schema=ON is set. */ 002249 sqlite3ExprListDelete(db, p->pCheck); 002250 p->pCheck = 0; 002251 } 002252 } 002253 #endif /* !defined(SQLITE_OMIT_CHECK) */ 002254 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 002255 if( p->tabFlags & TF_HasGenerated ){ 002256 int ii, nNG = 0; 002257 testcase( p->tabFlags & TF_HasVirtual ); 002258 testcase( p->tabFlags & TF_HasStored ); 002259 for(ii=0; ii<p->nCol; ii++){ 002260 u32 colFlags = p->aCol[ii].colFlags; 002261 if( (colFlags & COLFLAG_GENERATED)!=0 ){ 002262 Expr *pX = p->aCol[ii].pDflt; 002263 testcase( colFlags & COLFLAG_VIRTUAL ); 002264 testcase( colFlags & COLFLAG_STORED ); 002265 if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol, pX, 0) ){ 002266 /* If there are errors in resolving the expression, change the 002267 ** expression to a NULL. This prevents code generators that operate 002268 ** on the expression from inserting extra parts into the expression 002269 ** tree that have been allocated from lookaside memory, which is 002270 ** illegal in a schema and will lead to errors heap corruption when 002271 ** the database connection closes. */ 002272 sqlite3ExprDelete(db, pX); 002273 p->aCol[ii].pDflt = sqlite3ExprAlloc(db, TK_NULL, 0, 0); 002274 } 002275 }else{ 002276 nNG++; 002277 } 002278 } 002279 if( nNG==0 ){ 002280 sqlite3ErrorMsg(pParse, "must have at least one non-generated column"); 002281 return; 002282 } 002283 } 002284 #endif 002285 002286 /* Estimate the average row size for the table and for all implied indices */ 002287 estimateTableWidth(p); 002288 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ 002289 estimateIndexWidth(pIdx); 002290 } 002291 002292 /* If not initializing, then create a record for the new table 002293 ** in the SQLITE_MASTER table of the database. 002294 ** 002295 ** If this is a TEMPORARY table, write the entry into the auxiliary 002296 ** file instead of into the main database file. 002297 */ 002298 if( !db->init.busy ){ 002299 int n; 002300 Vdbe *v; 002301 char *zType; /* "view" or "table" */ 002302 char *zType2; /* "VIEW" or "TABLE" */ 002303 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ 002304 002305 v = sqlite3GetVdbe(pParse); 002306 if( NEVER(v==0) ) return; 002307 002308 sqlite3VdbeAddOp1(v, OP_Close, 0); 002309 002310 /* 002311 ** Initialize zType for the new view or table. 002312 */ 002313 if( p->pSelect==0 ){ 002314 /* A regular table */ 002315 zType = "table"; 002316 zType2 = "TABLE"; 002317 #ifndef SQLITE_OMIT_VIEW 002318 }else{ 002319 /* A view */ 002320 zType = "view"; 002321 zType2 = "VIEW"; 002322 #endif 002323 } 002324 002325 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT 002326 ** statement to populate the new table. The root-page number for the 002327 ** new table is in register pParse->regRoot. 002328 ** 002329 ** Once the SELECT has been coded by sqlite3Select(), it is in a 002330 ** suitable state to query for the column names and types to be used 002331 ** by the new table. 002332 ** 002333 ** A shared-cache write-lock is not required to write to the new table, 002334 ** as a schema-lock must have already been obtained to create it. Since 002335 ** a schema-lock excludes all other database users, the write-lock would 002336 ** be redundant. 002337 */ 002338 if( pSelect ){ 002339 SelectDest dest; /* Where the SELECT should store results */ 002340 int regYield; /* Register holding co-routine entry-point */ 002341 int addrTop; /* Top of the co-routine */ 002342 int regRec; /* A record to be insert into the new table */ 002343 int regRowid; /* Rowid of the next row to insert */ 002344 int addrInsLoop; /* Top of the loop for inserting rows */ 002345 Table *pSelTab; /* A table that describes the SELECT results */ 002346 002347 regYield = ++pParse->nMem; 002348 regRec = ++pParse->nMem; 002349 regRowid = ++pParse->nMem; 002350 assert(pParse->nTab==1); 002351 sqlite3MayAbort(pParse); 002352 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); 002353 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); 002354 pParse->nTab = 2; 002355 addrTop = sqlite3VdbeCurrentAddr(v) + 1; 002356 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); 002357 if( pParse->nErr ) return; 002358 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB); 002359 if( pSelTab==0 ) return; 002360 assert( p->aCol==0 ); 002361 p->nCol = p->nNVCol = pSelTab->nCol; 002362 p->aCol = pSelTab->aCol; 002363 pSelTab->nCol = 0; 002364 pSelTab->aCol = 0; 002365 sqlite3DeleteTable(db, pSelTab); 002366 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); 002367 sqlite3Select(pParse, pSelect, &dest); 002368 if( pParse->nErr ) return; 002369 sqlite3VdbeEndCoroutine(v, regYield); 002370 sqlite3VdbeJumpHere(v, addrTop - 1); 002371 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); 002372 VdbeCoverage(v); 002373 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); 002374 sqlite3TableAffinity(v, p, 0); 002375 sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); 002376 sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); 002377 sqlite3VdbeGoto(v, addrInsLoop); 002378 sqlite3VdbeJumpHere(v, addrInsLoop); 002379 sqlite3VdbeAddOp1(v, OP_Close, 1); 002380 } 002381 002382 /* Compute the complete text of the CREATE statement */ 002383 if( pSelect ){ 002384 zStmt = createTableStmt(db, p); 002385 }else{ 002386 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; 002387 n = (int)(pEnd2->z - pParse->sNameToken.z); 002388 if( pEnd2->z[0]!=';' ) n += pEnd2->n; 002389 zStmt = sqlite3MPrintf(db, 002390 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z 002391 ); 002392 } 002393 002394 /* A slot for the record has already been allocated in the 002395 ** SQLITE_MASTER table. We just need to update that slot with all 002396 ** the information we've collected. 002397 */ 002398 sqlite3NestedParse(pParse, 002399 "UPDATE %Q.%s " 002400 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " 002401 "WHERE rowid=#%d", 002402 db->aDb[iDb].zDbSName, MASTER_NAME, 002403 zType, 002404 p->zName, 002405 p->zName, 002406 pParse->regRoot, 002407 zStmt, 002408 pParse->regRowid 002409 ); 002410 sqlite3DbFree(db, zStmt); 002411 sqlite3ChangeCookie(pParse, iDb); 002412 002413 #ifndef SQLITE_OMIT_AUTOINCREMENT 002414 /* Check to see if we need to create an sqlite_sequence table for 002415 ** keeping track of autoincrement keys. 002416 */ 002417 if( (p->tabFlags & TF_Autoincrement)!=0 ){ 002418 Db *pDb = &db->aDb[iDb]; 002419 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 002420 if( pDb->pSchema->pSeqTab==0 ){ 002421 sqlite3NestedParse(pParse, 002422 "CREATE TABLE %Q.sqlite_sequence(name,seq)", 002423 pDb->zDbSName 002424 ); 002425 } 002426 } 002427 #endif 002428 002429 /* Reparse everything to update our internal data structures */ 002430 sqlite3VdbeAddParseSchemaOp(v, iDb, 002431 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName)); 002432 } 002433 002434 /* Add the table to the in-memory representation of the database. 002435 */ 002436 if( db->init.busy ){ 002437 Table *pOld; 002438 Schema *pSchema = p->pSchema; 002439 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 002440 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); 002441 if( pOld ){ 002442 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ 002443 sqlite3OomFault(db); 002444 return; 002445 } 002446 pParse->pNewTable = 0; 002447 db->mDbFlags |= DBFLAG_SchemaChange; 002448 002449 #ifndef SQLITE_OMIT_ALTERTABLE 002450 if( !p->pSelect ){ 002451 const char *zName = (const char *)pParse->sNameToken.z; 002452 int nName; 002453 assert( !pSelect && pCons && pEnd ); 002454 if( pCons->z==0 ){ 002455 pCons = pEnd; 002456 } 002457 nName = (int)((const char *)pCons->z - zName); 002458 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName); 002459 } 002460 #endif 002461 } 002462 } 002463 002464 #ifndef SQLITE_OMIT_VIEW 002465 /* 002466 ** The parser calls this routine in order to create a new VIEW 002467 */ 002468 void sqlite3CreateView( 002469 Parse *pParse, /* The parsing context */ 002470 Token *pBegin, /* The CREATE token that begins the statement */ 002471 Token *pName1, /* The token that holds the name of the view */ 002472 Token *pName2, /* The token that holds the name of the view */ 002473 ExprList *pCNames, /* Optional list of view column names */ 002474 Select *pSelect, /* A SELECT statement that will become the new view */ 002475 int isTemp, /* TRUE for a TEMPORARY view */ 002476 int noErr /* Suppress error messages if VIEW already exists */ 002477 ){ 002478 Table *p; 002479 int n; 002480 const char *z; 002481 Token sEnd; 002482 DbFixer sFix; 002483 Token *pName = 0; 002484 int iDb; 002485 sqlite3 *db = pParse->db; 002486 002487 if( pParse->nVar>0 ){ 002488 sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); 002489 goto create_view_fail; 002490 } 002491 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); 002492 p = pParse->pNewTable; 002493 if( p==0 || pParse->nErr ) goto create_view_fail; 002494 sqlite3TwoPartName(pParse, pName1, pName2, &pName); 002495 iDb = sqlite3SchemaToIndex(db, p->pSchema); 002496 sqlite3FixInit(&sFix, pParse, iDb, "view", pName); 002497 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; 002498 002499 /* Make a copy of the entire SELECT statement that defines the view. 002500 ** This will force all the Expr.token.z values to be dynamically 002501 ** allocated rather than point to the input string - which means that 002502 ** they will persist after the current sqlite3_exec() call returns. 002503 */ 002504 pSelect->selFlags |= SF_View; 002505 if( IN_RENAME_OBJECT ){ 002506 p->pSelect = pSelect; 002507 pSelect = 0; 002508 }else{ 002509 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); 002510 } 002511 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); 002512 if( db->mallocFailed ) goto create_view_fail; 002513 002514 /* Locate the end of the CREATE VIEW statement. Make sEnd point to 002515 ** the end. 002516 */ 002517 sEnd = pParse->sLastToken; 002518 assert( sEnd.z[0]!=0 || sEnd.n==0 ); 002519 if( sEnd.z[0]!=';' ){ 002520 sEnd.z += sEnd.n; 002521 } 002522 sEnd.n = 0; 002523 n = (int)(sEnd.z - pBegin->z); 002524 assert( n>0 ); 002525 z = pBegin->z; 002526 while( sqlite3Isspace(z[n-1]) ){ n--; } 002527 sEnd.z = &z[n-1]; 002528 sEnd.n = 1; 002529 002530 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ 002531 sqlite3EndTable(pParse, 0, &sEnd, 0, 0); 002532 002533 create_view_fail: 002534 sqlite3SelectDelete(db, pSelect); 002535 if( IN_RENAME_OBJECT ){ 002536 sqlite3RenameExprlistUnmap(pParse, pCNames); 002537 } 002538 sqlite3ExprListDelete(db, pCNames); 002539 return; 002540 } 002541 #endif /* SQLITE_OMIT_VIEW */ 002542 002543 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) 002544 /* 002545 ** The Table structure pTable is really a VIEW. Fill in the names of 002546 ** the columns of the view in the pTable structure. Return the number 002547 ** of errors. If an error is seen leave an error message in pParse->zErrMsg. 002548 */ 002549 int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ 002550 Table *pSelTab; /* A fake table from which we get the result set */ 002551 Select *pSel; /* Copy of the SELECT that implements the view */ 002552 int nErr = 0; /* Number of errors encountered */ 002553 int n; /* Temporarily holds the number of cursors assigned */ 002554 sqlite3 *db = pParse->db; /* Database connection for malloc errors */ 002555 #ifndef SQLITE_OMIT_VIRTUALTABLE 002556 int rc; 002557 #endif 002558 #ifndef SQLITE_OMIT_AUTHORIZATION 002559 sqlite3_xauth xAuth; /* Saved xAuth pointer */ 002560 #endif 002561 002562 assert( pTable ); 002563 002564 #ifndef SQLITE_OMIT_VIRTUALTABLE 002565 db->nSchemaLock++; 002566 rc = sqlite3VtabCallConnect(pParse, pTable); 002567 db->nSchemaLock--; 002568 if( rc ){ 002569 return 1; 002570 } 002571 if( IsVirtual(pTable) ) return 0; 002572 #endif 002573 002574 #ifndef SQLITE_OMIT_VIEW 002575 /* A positive nCol means the columns names for this view are 002576 ** already known. 002577 */ 002578 if( pTable->nCol>0 ) return 0; 002579 002580 /* A negative nCol is a special marker meaning that we are currently 002581 ** trying to compute the column names. If we enter this routine with 002582 ** a negative nCol, it means two or more views form a loop, like this: 002583 ** 002584 ** CREATE VIEW one AS SELECT * FROM two; 002585 ** CREATE VIEW two AS SELECT * FROM one; 002586 ** 002587 ** Actually, the error above is now caught prior to reaching this point. 002588 ** But the following test is still important as it does come up 002589 ** in the following: 002590 ** 002591 ** CREATE TABLE main.ex1(a); 002592 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; 002593 ** SELECT * FROM temp.ex1; 002594 */ 002595 if( pTable->nCol<0 ){ 002596 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); 002597 return 1; 002598 } 002599 assert( pTable->nCol>=0 ); 002600 002601 /* If we get this far, it means we need to compute the table names. 002602 ** Note that the call to sqlite3ResultSetOfSelect() will expand any 002603 ** "*" elements in the results set of the view and will assign cursors 002604 ** to the elements of the FROM clause. But we do not want these changes 002605 ** to be permanent. So the computation is done on a copy of the SELECT 002606 ** statement that defines the view. 002607 */ 002608 assert( pTable->pSelect ); 002609 pSel = sqlite3SelectDup(db, pTable->pSelect, 0); 002610 if( pSel ){ 002611 #ifndef SQLITE_OMIT_ALTERTABLE 002612 u8 eParseMode = pParse->eParseMode; 002613 pParse->eParseMode = PARSE_MODE_NORMAL; 002614 #endif 002615 n = pParse->nTab; 002616 sqlite3SrcListAssignCursors(pParse, pSel->pSrc); 002617 pTable->nCol = -1; 002618 DisableLookaside; 002619 #ifndef SQLITE_OMIT_AUTHORIZATION 002620 xAuth = db->xAuth; 002621 db->xAuth = 0; 002622 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); 002623 db->xAuth = xAuth; 002624 #else 002625 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); 002626 #endif 002627 pParse->nTab = n; 002628 if( pSelTab==0 ){ 002629 pTable->nCol = 0; 002630 nErr++; 002631 }else if( pTable->pCheck ){ 002632 /* CREATE VIEW name(arglist) AS ... 002633 ** The names of the columns in the table are taken from 002634 ** arglist which is stored in pTable->pCheck. The pCheck field 002635 ** normally holds CHECK constraints on an ordinary table, but for 002636 ** a VIEW it holds the list of column names. 002637 */ 002638 sqlite3ColumnsFromExprList(pParse, pTable->pCheck, 002639 &pTable->nCol, &pTable->aCol); 002640 if( db->mallocFailed==0 002641 && pParse->nErr==0 002642 && pTable->nCol==pSel->pEList->nExpr 002643 ){ 002644 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel, 002645 SQLITE_AFF_NONE); 002646 } 002647 }else{ 002648 /* CREATE VIEW name AS... without an argument list. Construct 002649 ** the column names from the SELECT statement that defines the view. 002650 */ 002651 assert( pTable->aCol==0 ); 002652 pTable->nCol = pSelTab->nCol; 002653 pTable->aCol = pSelTab->aCol; 002654 pSelTab->nCol = 0; 002655 pSelTab->aCol = 0; 002656 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); 002657 } 002658 pTable->nNVCol = pTable->nCol; 002659 sqlite3DeleteTable(db, pSelTab); 002660 sqlite3SelectDelete(db, pSel); 002661 EnableLookaside; 002662 #ifndef SQLITE_OMIT_ALTERTABLE 002663 pParse->eParseMode = eParseMode; 002664 #endif 002665 } else { 002666 nErr++; 002667 } 002668 pTable->pSchema->schemaFlags |= DB_UnresetViews; 002669 if( db->mallocFailed ){ 002670 sqlite3DeleteColumnNames(db, pTable); 002671 pTable->aCol = 0; 002672 pTable->nCol = 0; 002673 } 002674 #endif /* SQLITE_OMIT_VIEW */ 002675 return nErr; 002676 } 002677 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ 002678 002679 #ifndef SQLITE_OMIT_VIEW 002680 /* 002681 ** Clear the column names from every VIEW in database idx. 002682 */ 002683 static void sqliteViewResetAll(sqlite3 *db, int idx){ 002684 HashElem *i; 002685 assert( sqlite3SchemaMutexHeld(db, idx, 0) ); 002686 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; 002687 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ 002688 Table *pTab = sqliteHashData(i); 002689 if( pTab->pSelect ){ 002690 sqlite3DeleteColumnNames(db, pTab); 002691 pTab->aCol = 0; 002692 pTab->nCol = 0; 002693 } 002694 } 002695 DbClearProperty(db, idx, DB_UnresetViews); 002696 } 002697 #else 002698 # define sqliteViewResetAll(A,B) 002699 #endif /* SQLITE_OMIT_VIEW */ 002700 002701 /* 002702 ** This function is called by the VDBE to adjust the internal schema 002703 ** used by SQLite when the btree layer moves a table root page. The 002704 ** root-page of a table or index in database iDb has changed from iFrom 002705 ** to iTo. 002706 ** 002707 ** Ticket #1728: The symbol table might still contain information 002708 ** on tables and/or indices that are the process of being deleted. 002709 ** If you are unlucky, one of those deleted indices or tables might 002710 ** have the same rootpage number as the real table or index that is 002711 ** being moved. So we cannot stop searching after the first match 002712 ** because the first match might be for one of the deleted indices 002713 ** or tables and not the table/index that is actually being moved. 002714 ** We must continue looping until all tables and indices with 002715 ** rootpage==iFrom have been converted to have a rootpage of iTo 002716 ** in order to be certain that we got the right one. 002717 */ 002718 #ifndef SQLITE_OMIT_AUTOVACUUM 002719 void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){ 002720 HashElem *pElem; 002721 Hash *pHash; 002722 Db *pDb; 002723 002724 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 002725 pDb = &db->aDb[iDb]; 002726 pHash = &pDb->pSchema->tblHash; 002727 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ 002728 Table *pTab = sqliteHashData(pElem); 002729 if( pTab->tnum==iFrom ){ 002730 pTab->tnum = iTo; 002731 } 002732 } 002733 pHash = &pDb->pSchema->idxHash; 002734 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ 002735 Index *pIdx = sqliteHashData(pElem); 002736 if( pIdx->tnum==iFrom ){ 002737 pIdx->tnum = iTo; 002738 } 002739 } 002740 } 002741 #endif 002742 002743 /* 002744 ** Write code to erase the table with root-page iTable from database iDb. 002745 ** Also write code to modify the sqlite_master table and internal schema 002746 ** if a root-page of another table is moved by the btree-layer whilst 002747 ** erasing iTable (this can happen with an auto-vacuum database). 002748 */ 002749 static void destroyRootPage(Parse *pParse, int iTable, int iDb){ 002750 Vdbe *v = sqlite3GetVdbe(pParse); 002751 int r1 = sqlite3GetTempReg(pParse); 002752 if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema"); 002753 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); 002754 sqlite3MayAbort(pParse); 002755 #ifndef SQLITE_OMIT_AUTOVACUUM 002756 /* OP_Destroy stores an in integer r1. If this integer 002757 ** is non-zero, then it is the root page number of a table moved to 002758 ** location iTable. The following code modifies the sqlite_master table to 002759 ** reflect this. 002760 ** 002761 ** The "#NNN" in the SQL is a special constant that means whatever value 002762 ** is in register NNN. See grammar rules associated with the TK_REGISTER 002763 ** token for additional information. 002764 */ 002765 sqlite3NestedParse(pParse, 002766 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", 002767 pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1); 002768 #endif 002769 sqlite3ReleaseTempReg(pParse, r1); 002770 } 002771 002772 /* 002773 ** Write VDBE code to erase table pTab and all associated indices on disk. 002774 ** Code to update the sqlite_master tables and internal schema definitions 002775 ** in case a root-page belonging to another table is moved by the btree layer 002776 ** is also added (this can happen with an auto-vacuum database). 002777 */ 002778 static void destroyTable(Parse *pParse, Table *pTab){ 002779 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM 002780 ** is not defined), then it is important to call OP_Destroy on the 002781 ** table and index root-pages in order, starting with the numerically 002782 ** largest root-page number. This guarantees that none of the root-pages 002783 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the 002784 ** following were coded: 002785 ** 002786 ** OP_Destroy 4 0 002787 ** ... 002788 ** OP_Destroy 5 0 002789 ** 002790 ** and root page 5 happened to be the largest root-page number in the 002791 ** database, then root page 5 would be moved to page 4 by the 002792 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit 002793 ** a free-list page. 002794 */ 002795 int iTab = pTab->tnum; 002796 int iDestroyed = 0; 002797 002798 while( 1 ){ 002799 Index *pIdx; 002800 int iLargest = 0; 002801 002802 if( iDestroyed==0 || iTab<iDestroyed ){ 002803 iLargest = iTab; 002804 } 002805 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 002806 int iIdx = pIdx->tnum; 002807 assert( pIdx->pSchema==pTab->pSchema ); 002808 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){ 002809 iLargest = iIdx; 002810 } 002811 } 002812 if( iLargest==0 ){ 002813 return; 002814 }else{ 002815 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 002816 assert( iDb>=0 && iDb<pParse->db->nDb ); 002817 destroyRootPage(pParse, iLargest, iDb); 002818 iDestroyed = iLargest; 002819 } 002820 } 002821 } 002822 002823 /* 002824 ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) 002825 ** after a DROP INDEX or DROP TABLE command. 002826 */ 002827 static void sqlite3ClearStatTables( 002828 Parse *pParse, /* The parsing context */ 002829 int iDb, /* The database number */ 002830 const char *zType, /* "idx" or "tbl" */ 002831 const char *zName /* Name of index or table */ 002832 ){ 002833 int i; 002834 const char *zDbName = pParse->db->aDb[iDb].zDbSName; 002835 for(i=1; i<=4; i++){ 002836 char zTab[24]; 002837 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); 002838 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ 002839 sqlite3NestedParse(pParse, 002840 "DELETE FROM %Q.%s WHERE %s=%Q", 002841 zDbName, zTab, zType, zName 002842 ); 002843 } 002844 } 002845 } 002846 002847 /* 002848 ** Generate code to drop a table. 002849 */ 002850 void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ 002851 Vdbe *v; 002852 sqlite3 *db = pParse->db; 002853 Trigger *pTrigger; 002854 Db *pDb = &db->aDb[iDb]; 002855 002856 v = sqlite3GetVdbe(pParse); 002857 assert( v!=0 ); 002858 sqlite3BeginWriteOperation(pParse, 1, iDb); 002859 002860 #ifndef SQLITE_OMIT_VIRTUALTABLE 002861 if( IsVirtual(pTab) ){ 002862 sqlite3VdbeAddOp0(v, OP_VBegin); 002863 } 002864 #endif 002865 002866 /* Drop all triggers associated with the table being dropped. Code 002867 ** is generated to remove entries from sqlite_master and/or 002868 ** sqlite_temp_master if required. 002869 */ 002870 pTrigger = sqlite3TriggerList(pParse, pTab); 002871 while( pTrigger ){ 002872 assert( pTrigger->pSchema==pTab->pSchema || 002873 pTrigger->pSchema==db->aDb[1].pSchema ); 002874 sqlite3DropTriggerPtr(pParse, pTrigger); 002875 pTrigger = pTrigger->pNext; 002876 } 002877 002878 #ifndef SQLITE_OMIT_AUTOINCREMENT 002879 /* Remove any entries of the sqlite_sequence table associated with 002880 ** the table being dropped. This is done before the table is dropped 002881 ** at the btree level, in case the sqlite_sequence table needs to 002882 ** move as a result of the drop (can happen in auto-vacuum mode). 002883 */ 002884 if( pTab->tabFlags & TF_Autoincrement ){ 002885 sqlite3NestedParse(pParse, 002886 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", 002887 pDb->zDbSName, pTab->zName 002888 ); 002889 } 002890 #endif 002891 002892 /* Drop all SQLITE_MASTER table and index entries that refer to the 002893 ** table. The program name loops through the master table and deletes 002894 ** every row that refers to a table of the same name as the one being 002895 ** dropped. Triggers are handled separately because a trigger can be 002896 ** created in the temp database that refers to a table in another 002897 ** database. 002898 */ 002899 sqlite3NestedParse(pParse, 002900 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", 002901 pDb->zDbSName, MASTER_NAME, pTab->zName); 002902 if( !isView && !IsVirtual(pTab) ){ 002903 destroyTable(pParse, pTab); 002904 } 002905 002906 /* Remove the table entry from SQLite's internal schema and modify 002907 ** the schema cookie. 002908 */ 002909 if( IsVirtual(pTab) ){ 002910 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); 002911 sqlite3MayAbort(pParse); 002912 } 002913 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); 002914 sqlite3ChangeCookie(pParse, iDb); 002915 sqliteViewResetAll(db, iDb); 002916 } 002917 002918 /* 002919 ** Return TRUE if shadow tables should be read-only in the current 002920 ** context. 002921 */ 002922 int sqlite3ReadOnlyShadowTables(sqlite3 *db){ 002923 #ifndef SQLITE_OMIT_VIRTUALTABLE 002924 if( (db->flags & SQLITE_Defensive)!=0 002925 && db->pVtabCtx==0 002926 && db->nVdbeExec==0 002927 ){ 002928 return 1; 002929 } 002930 #endif 002931 return 0; 002932 } 002933 002934 /* 002935 ** Return true if it is not allowed to drop the given table 002936 */ 002937 static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){ 002938 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ 002939 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0; 002940 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0; 002941 return 1; 002942 } 002943 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){ 002944 return 1; 002945 } 002946 return 0; 002947 } 002948 002949 /* 002950 ** This routine is called to do the work of a DROP TABLE statement. 002951 ** pName is the name of the table to be dropped. 002952 */ 002953 void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ 002954 Table *pTab; 002955 Vdbe *v; 002956 sqlite3 *db = pParse->db; 002957 int iDb; 002958 002959 if( db->mallocFailed ){ 002960 goto exit_drop_table; 002961 } 002962 assert( pParse->nErr==0 ); 002963 assert( pName->nSrc==1 ); 002964 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; 002965 if( noErr ) db->suppressErr++; 002966 assert( isView==0 || isView==LOCATE_VIEW ); 002967 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); 002968 if( noErr ) db->suppressErr--; 002969 002970 if( pTab==0 ){ 002971 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); 002972 goto exit_drop_table; 002973 } 002974 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 002975 assert( iDb>=0 && iDb<db->nDb ); 002976 002977 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure 002978 ** it is initialized. 002979 */ 002980 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ 002981 goto exit_drop_table; 002982 } 002983 #ifndef SQLITE_OMIT_AUTHORIZATION 002984 { 002985 int code; 002986 const char *zTab = SCHEMA_TABLE(iDb); 002987 const char *zDb = db->aDb[iDb].zDbSName; 002988 const char *zArg2 = 0; 002989 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ 002990 goto exit_drop_table; 002991 } 002992 if( isView ){ 002993 if( !OMIT_TEMPDB && iDb==1 ){ 002994 code = SQLITE_DROP_TEMP_VIEW; 002995 }else{ 002996 code = SQLITE_DROP_VIEW; 002997 } 002998 #ifndef SQLITE_OMIT_VIRTUALTABLE 002999 }else if( IsVirtual(pTab) ){ 003000 code = SQLITE_DROP_VTABLE; 003001 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; 003002 #endif 003003 }else{ 003004 if( !OMIT_TEMPDB && iDb==1 ){ 003005 code = SQLITE_DROP_TEMP_TABLE; 003006 }else{ 003007 code = SQLITE_DROP_TABLE; 003008 } 003009 } 003010 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ 003011 goto exit_drop_table; 003012 } 003013 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ 003014 goto exit_drop_table; 003015 } 003016 } 003017 #endif 003018 if( tableMayNotBeDropped(db, pTab) ){ 003019 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); 003020 goto exit_drop_table; 003021 } 003022 003023 #ifndef SQLITE_OMIT_VIEW 003024 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used 003025 ** on a table. 003026 */ 003027 if( isView && pTab->pSelect==0 ){ 003028 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); 003029 goto exit_drop_table; 003030 } 003031 if( !isView && pTab->pSelect ){ 003032 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); 003033 goto exit_drop_table; 003034 } 003035 #endif 003036 003037 /* Generate code to remove the table from the master table 003038 ** on disk. 003039 */ 003040 v = sqlite3GetVdbe(pParse); 003041 if( v ){ 003042 sqlite3BeginWriteOperation(pParse, 1, iDb); 003043 if( !isView ){ 003044 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); 003045 sqlite3FkDropTable(pParse, pName, pTab); 003046 } 003047 sqlite3CodeDropTable(pParse, pTab, iDb, isView); 003048 } 003049 003050 exit_drop_table: 003051 sqlite3SrcListDelete(db, pName); 003052 } 003053 003054 /* 003055 ** This routine is called to create a new foreign key on the table 003056 ** currently under construction. pFromCol determines which columns 003057 ** in the current table point to the foreign key. If pFromCol==0 then 003058 ** connect the key to the last column inserted. pTo is the name of 003059 ** the table referred to (a.k.a the "parent" table). pToCol is a list 003060 ** of tables in the parent pTo table. flags contains all 003061 ** information about the conflict resolution algorithms specified 003062 ** in the ON DELETE, ON UPDATE and ON INSERT clauses. 003063 ** 003064 ** An FKey structure is created and added to the table currently 003065 ** under construction in the pParse->pNewTable field. 003066 ** 003067 ** The foreign key is set for IMMEDIATE processing. A subsequent call 003068 ** to sqlite3DeferForeignKey() might change this to DEFERRED. 003069 */ 003070 void sqlite3CreateForeignKey( 003071 Parse *pParse, /* Parsing context */ 003072 ExprList *pFromCol, /* Columns in this table that point to other table */ 003073 Token *pTo, /* Name of the other table */ 003074 ExprList *pToCol, /* Columns in the other table */ 003075 int flags /* Conflict resolution algorithms. */ 003076 ){ 003077 sqlite3 *db = pParse->db; 003078 #ifndef SQLITE_OMIT_FOREIGN_KEY 003079 FKey *pFKey = 0; 003080 FKey *pNextTo; 003081 Table *p = pParse->pNewTable; 003082 int nByte; 003083 int i; 003084 int nCol; 003085 char *z; 003086 003087 assert( pTo!=0 ); 003088 if( p==0 || IN_DECLARE_VTAB ) goto fk_end; 003089 if( pFromCol==0 ){ 003090 int iCol = p->nCol-1; 003091 if( NEVER(iCol<0) ) goto fk_end; 003092 if( pToCol && pToCol->nExpr!=1 ){ 003093 sqlite3ErrorMsg(pParse, "foreign key on %s" 003094 " should reference only one column of table %T", 003095 p->aCol[iCol].zName, pTo); 003096 goto fk_end; 003097 } 003098 nCol = 1; 003099 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ 003100 sqlite3ErrorMsg(pParse, 003101 "number of columns in foreign key does not match the number of " 003102 "columns in the referenced table"); 003103 goto fk_end; 003104 }else{ 003105 nCol = pFromCol->nExpr; 003106 } 003107 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; 003108 if( pToCol ){ 003109 for(i=0; i<pToCol->nExpr; i++){ 003110 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1; 003111 } 003112 } 003113 pFKey = sqlite3DbMallocZero(db, nByte ); 003114 if( pFKey==0 ){ 003115 goto fk_end; 003116 } 003117 pFKey->pFrom = p; 003118 pFKey->pNextFrom = p->pFKey; 003119 z = (char*)&pFKey->aCol[nCol]; 003120 pFKey->zTo = z; 003121 if( IN_RENAME_OBJECT ){ 003122 sqlite3RenameTokenMap(pParse, (void*)z, pTo); 003123 } 003124 memcpy(z, pTo->z, pTo->n); 003125 z[pTo->n] = 0; 003126 sqlite3Dequote(z); 003127 z += pTo->n+1; 003128 pFKey->nCol = nCol; 003129 if( pFromCol==0 ){ 003130 pFKey->aCol[0].iFrom = p->nCol-1; 003131 }else{ 003132 for(i=0; i<nCol; i++){ 003133 int j; 003134 for(j=0; j<p->nCol; j++){ 003135 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ 003136 pFKey->aCol[i].iFrom = j; 003137 break; 003138 } 003139 } 003140 if( j>=p->nCol ){ 003141 sqlite3ErrorMsg(pParse, 003142 "unknown column \"%s\" in foreign key definition", 003143 pFromCol->a[i].zName); 003144 goto fk_end; 003145 } 003146 if( IN_RENAME_OBJECT ){ 003147 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName); 003148 } 003149 } 003150 } 003151 if( pToCol ){ 003152 for(i=0; i<nCol; i++){ 003153 int n = sqlite3Strlen30(pToCol->a[i].zName); 003154 pFKey->aCol[i].zCol = z; 003155 if( IN_RENAME_OBJECT ){ 003156 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName); 003157 } 003158 memcpy(z, pToCol->a[i].zName, n); 003159 z[n] = 0; 003160 z += n+1; 003161 } 003162 } 003163 pFKey->isDeferred = 0; 003164 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ 003165 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ 003166 003167 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); 003168 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 003169 pFKey->zTo, (void *)pFKey 003170 ); 003171 if( pNextTo==pFKey ){ 003172 sqlite3OomFault(db); 003173 goto fk_end; 003174 } 003175 if( pNextTo ){ 003176 assert( pNextTo->pPrevTo==0 ); 003177 pFKey->pNextTo = pNextTo; 003178 pNextTo->pPrevTo = pFKey; 003179 } 003180 003181 /* Link the foreign key to the table as the last step. 003182 */ 003183 p->pFKey = pFKey; 003184 pFKey = 0; 003185 003186 fk_end: 003187 sqlite3DbFree(db, pFKey); 003188 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ 003189 sqlite3ExprListDelete(db, pFromCol); 003190 sqlite3ExprListDelete(db, pToCol); 003191 } 003192 003193 /* 003194 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED 003195 ** clause is seen as part of a foreign key definition. The isDeferred 003196 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. 003197 ** The behavior of the most recently created foreign key is adjusted 003198 ** accordingly. 003199 */ 003200 void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ 003201 #ifndef SQLITE_OMIT_FOREIGN_KEY 003202 Table *pTab; 003203 FKey *pFKey; 003204 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; 003205 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ 003206 pFKey->isDeferred = (u8)isDeferred; 003207 #endif 003208 } 003209 003210 /* 003211 ** Generate code that will erase and refill index *pIdx. This is 003212 ** used to initialize a newly created index or to recompute the 003213 ** content of an index in response to a REINDEX command. 003214 ** 003215 ** if memRootPage is not negative, it means that the index is newly 003216 ** created. The register specified by memRootPage contains the 003217 ** root page number of the index. If memRootPage is negative, then 003218 ** the index already exists and must be cleared before being refilled and 003219 ** the root page number of the index is taken from pIndex->tnum. 003220 */ 003221 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ 003222 Table *pTab = pIndex->pTable; /* The table that is indexed */ 003223 int iTab = pParse->nTab++; /* Btree cursor used for pTab */ 003224 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ 003225 int iSorter; /* Cursor opened by OpenSorter (if in use) */ 003226 int addr1; /* Address of top of loop */ 003227 int addr2; /* Address to jump to for next iteration */ 003228 int tnum; /* Root page of index */ 003229 int iPartIdxLabel; /* Jump to this label to skip a row */ 003230 Vdbe *v; /* Generate code into this virtual machine */ 003231 KeyInfo *pKey; /* KeyInfo for index */ 003232 int regRecord; /* Register holding assembled index record */ 003233 sqlite3 *db = pParse->db; /* The database connection */ 003234 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); 003235 003236 #ifndef SQLITE_OMIT_AUTHORIZATION 003237 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, 003238 db->aDb[iDb].zDbSName ) ){ 003239 return; 003240 } 003241 #endif 003242 003243 /* Require a write-lock on the table to perform this operation */ 003244 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); 003245 003246 v = sqlite3GetVdbe(pParse); 003247 if( v==0 ) return; 003248 if( memRootPage>=0 ){ 003249 tnum = memRootPage; 003250 }else{ 003251 tnum = pIndex->tnum; 003252 } 003253 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); 003254 assert( pKey!=0 || db->mallocFailed || pParse->nErr ); 003255 003256 /* Open the sorter cursor if we are to use one. */ 003257 iSorter = pParse->nTab++; 003258 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) 003259 sqlite3KeyInfoRef(pKey), P4_KEYINFO); 003260 003261 /* Open the table. Loop through all rows of the table, inserting index 003262 ** records into the sorter. */ 003263 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 003264 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); 003265 regRecord = sqlite3GetTempReg(pParse); 003266 sqlite3MultiWrite(pParse); 003267 003268 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); 003269 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); 003270 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); 003271 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); 003272 sqlite3VdbeJumpHere(v, addr1); 003273 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); 003274 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 003275 (char *)pKey, P4_KEYINFO); 003276 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); 003277 003278 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); 003279 if( IsUniqueIndex(pIndex) ){ 003280 int j2 = sqlite3VdbeGoto(v, 1); 003281 addr2 = sqlite3VdbeCurrentAddr(v); 003282 sqlite3VdbeVerifyAbortable(v, OE_Abort); 003283 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, 003284 pIndex->nKeyCol); VdbeCoverage(v); 003285 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); 003286 sqlite3VdbeJumpHere(v, j2); 003287 }else{ 003288 /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not 003289 ** abort. The exception is if one of the indexed expressions contains a 003290 ** user function that throws an exception when it is evaluated. But the 003291 ** overhead of adding a statement journal to a CREATE INDEX statement is 003292 ** very small (since most of the pages written do not contain content that 003293 ** needs to be restored if the statement aborts), so we call 003294 ** sqlite3MayAbort() for all CREATE INDEX statements. */ 003295 sqlite3MayAbort(pParse); 003296 addr2 = sqlite3VdbeCurrentAddr(v); 003297 } 003298 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); 003299 if( !pIndex->bAscKeyBug ){ 003300 /* This OP_SeekEnd opcode makes index insert for a REINDEX go much 003301 ** faster by avoiding unnecessary seeks. But the optimization does 003302 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables 003303 ** with DESC primary keys, since those indexes have there keys in 003304 ** a different order from the main table. 003305 ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf 003306 */ 003307 sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx); 003308 } 003309 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); 003310 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); 003311 sqlite3ReleaseTempReg(pParse, regRecord); 003312 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); 003313 sqlite3VdbeJumpHere(v, addr1); 003314 003315 sqlite3VdbeAddOp1(v, OP_Close, iTab); 003316 sqlite3VdbeAddOp1(v, OP_Close, iIdx); 003317 sqlite3VdbeAddOp1(v, OP_Close, iSorter); 003318 } 003319 003320 /* 003321 ** Allocate heap space to hold an Index object with nCol columns. 003322 ** 003323 ** Increase the allocation size to provide an extra nExtra bytes 003324 ** of 8-byte aligned space after the Index object and return a 003325 ** pointer to this extra space in *ppExtra. 003326 */ 003327 Index *sqlite3AllocateIndexObject( 003328 sqlite3 *db, /* Database connection */ 003329 i16 nCol, /* Total number of columns in the index */ 003330 int nExtra, /* Number of bytes of extra space to alloc */ 003331 char **ppExtra /* Pointer to the "extra" space */ 003332 ){ 003333 Index *p; /* Allocated index object */ 003334 int nByte; /* Bytes of space for Index object + arrays */ 003335 003336 nByte = ROUND8(sizeof(Index)) + /* Index structure */ 003337 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ 003338 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ 003339 sizeof(i16)*nCol + /* Index.aiColumn */ 003340 sizeof(u8)*nCol); /* Index.aSortOrder */ 003341 p = sqlite3DbMallocZero(db, nByte + nExtra); 003342 if( p ){ 003343 char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); 003344 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); 003345 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); 003346 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; 003347 p->aSortOrder = (u8*)pExtra; 003348 p->nColumn = nCol; 003349 p->nKeyCol = nCol - 1; 003350 *ppExtra = ((char*)p) + nByte; 003351 } 003352 return p; 003353 } 003354 003355 /* 003356 ** If expression list pList contains an expression that was parsed with 003357 ** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in 003358 ** pParse and return non-zero. Otherwise, return zero. 003359 */ 003360 int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){ 003361 if( pList ){ 003362 int i; 003363 for(i=0; i<pList->nExpr; i++){ 003364 if( pList->a[i].bNulls ){ 003365 u8 sf = pList->a[i].sortFlags; 003366 sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s", 003367 (sf==0 || sf==3) ? "FIRST" : "LAST" 003368 ); 003369 return 1; 003370 } 003371 } 003372 } 003373 return 0; 003374 } 003375 003376 /* 003377 ** Create a new index for an SQL table. pName1.pName2 is the name of the index 003378 ** and pTblList is the name of the table that is to be indexed. Both will 003379 ** be NULL for a primary key or an index that is created to satisfy a 003380 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable 003381 ** as the table to be indexed. pParse->pNewTable is a table that is 003382 ** currently being constructed by a CREATE TABLE statement. 003383 ** 003384 ** pList is a list of columns to be indexed. pList will be NULL if this 003385 ** is a primary key or unique-constraint on the most recent column added 003386 ** to the table currently under construction. 003387 */ 003388 void sqlite3CreateIndex( 003389 Parse *pParse, /* All information about this parse */ 003390 Token *pName1, /* First part of index name. May be NULL */ 003391 Token *pName2, /* Second part of index name. May be NULL */ 003392 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ 003393 ExprList *pList, /* A list of columns to be indexed */ 003394 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ 003395 Token *pStart, /* The CREATE token that begins this statement */ 003396 Expr *pPIWhere, /* WHERE clause for partial indices */ 003397 int sortOrder, /* Sort order of primary key when pList==NULL */ 003398 int ifNotExist, /* Omit error if index already exists */ 003399 u8 idxType /* The index type */ 003400 ){ 003401 Table *pTab = 0; /* Table to be indexed */ 003402 Index *pIndex = 0; /* The index to be created */ 003403 char *zName = 0; /* Name of the index */ 003404 int nName; /* Number of characters in zName */ 003405 int i, j; 003406 DbFixer sFix; /* For assigning database names to pTable */ 003407 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ 003408 sqlite3 *db = pParse->db; 003409 Db *pDb; /* The specific table containing the indexed database */ 003410 int iDb; /* Index of the database that is being written */ 003411 Token *pName = 0; /* Unqualified name of the index to create */ 003412 struct ExprList_item *pListItem; /* For looping over pList */ 003413 int nExtra = 0; /* Space allocated for zExtra[] */ 003414 int nExtraCol; /* Number of extra columns needed */ 003415 char *zExtra = 0; /* Extra space after the Index object */ 003416 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ 003417 003418 if( db->mallocFailed || pParse->nErr>0 ){ 003419 goto exit_create_index; 003420 } 003421 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ 003422 goto exit_create_index; 003423 } 003424 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 003425 goto exit_create_index; 003426 } 003427 if( sqlite3HasExplicitNulls(pParse, pList) ){ 003428 goto exit_create_index; 003429 } 003430 003431 /* 003432 ** Find the table that is to be indexed. Return early if not found. 003433 */ 003434 if( pTblName!=0 ){ 003435 003436 /* Use the two-part index name to determine the database 003437 ** to search for the table. 'Fix' the table name to this db 003438 ** before looking up the table. 003439 */ 003440 assert( pName1 && pName2 ); 003441 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); 003442 if( iDb<0 ) goto exit_create_index; 003443 assert( pName && pName->z ); 003444 003445 #ifndef SQLITE_OMIT_TEMPDB 003446 /* If the index name was unqualified, check if the table 003447 ** is a temp table. If so, set the database to 1. Do not do this 003448 ** if initialising a database schema. 003449 */ 003450 if( !db->init.busy ){ 003451 pTab = sqlite3SrcListLookup(pParse, pTblName); 003452 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ 003453 iDb = 1; 003454 } 003455 } 003456 #endif 003457 003458 sqlite3FixInit(&sFix, pParse, iDb, "index", pName); 003459 if( sqlite3FixSrcList(&sFix, pTblName) ){ 003460 /* Because the parser constructs pTblName from a single identifier, 003461 ** sqlite3FixSrcList can never fail. */ 003462 assert(0); 003463 } 003464 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); 003465 assert( db->mallocFailed==0 || pTab==0 ); 003466 if( pTab==0 ) goto exit_create_index; 003467 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ 003468 sqlite3ErrorMsg(pParse, 003469 "cannot create a TEMP index on non-TEMP table \"%s\"", 003470 pTab->zName); 003471 goto exit_create_index; 003472 } 003473 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab); 003474 }else{ 003475 assert( pName==0 ); 003476 assert( pStart==0 ); 003477 pTab = pParse->pNewTable; 003478 if( !pTab ) goto exit_create_index; 003479 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 003480 } 003481 pDb = &db->aDb[iDb]; 003482 003483 assert( pTab!=0 ); 003484 assert( pParse->nErr==0 ); 003485 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 003486 && db->init.busy==0 003487 && pTblName!=0 003488 #if SQLITE_USER_AUTHENTICATION 003489 && sqlite3UserAuthTable(pTab->zName)==0 003490 #endif 003491 #ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX 003492 && sqlite3StrICmp(&pTab->zName[7],"master")!=0 003493 #endif 003494 ){ 003495 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); 003496 goto exit_create_index; 003497 } 003498 #ifndef SQLITE_OMIT_VIEW 003499 if( pTab->pSelect ){ 003500 sqlite3ErrorMsg(pParse, "views may not be indexed"); 003501 goto exit_create_index; 003502 } 003503 #endif 003504 #ifndef SQLITE_OMIT_VIRTUALTABLE 003505 if( IsVirtual(pTab) ){ 003506 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); 003507 goto exit_create_index; 003508 } 003509 #endif 003510 003511 /* 003512 ** Find the name of the index. Make sure there is not already another 003513 ** index or table with the same name. 003514 ** 003515 ** Exception: If we are reading the names of permanent indices from the 003516 ** sqlite_master table (because some other process changed the schema) and 003517 ** one of the index names collides with the name of a temporary table or 003518 ** index, then we will continue to process this index. 003519 ** 003520 ** If pName==0 it means that we are 003521 ** dealing with a primary key or UNIQUE constraint. We have to invent our 003522 ** own name. 003523 */ 003524 if( pName ){ 003525 zName = sqlite3NameFromToken(db, pName); 003526 if( zName==0 ) goto exit_create_index; 003527 assert( pName->z!=0 ); 003528 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){ 003529 goto exit_create_index; 003530 } 003531 if( !IN_RENAME_OBJECT ){ 003532 if( !db->init.busy ){ 003533 if( sqlite3FindTable(db, zName, 0)!=0 ){ 003534 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); 003535 goto exit_create_index; 003536 } 003537 } 003538 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ 003539 if( !ifNotExist ){ 003540 sqlite3ErrorMsg(pParse, "index %s already exists", zName); 003541 }else{ 003542 assert( !db->init.busy ); 003543 sqlite3CodeVerifySchema(pParse, iDb); 003544 } 003545 goto exit_create_index; 003546 } 003547 } 003548 }else{ 003549 int n; 003550 Index *pLoop; 003551 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} 003552 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); 003553 if( zName==0 ){ 003554 goto exit_create_index; 003555 } 003556 003557 /* Automatic index names generated from within sqlite3_declare_vtab() 003558 ** must have names that are distinct from normal automatic index names. 003559 ** The following statement converts "sqlite3_autoindex..." into 003560 ** "sqlite3_butoindex..." in order to make the names distinct. 003561 ** The "vtab_err.test" test demonstrates the need of this statement. */ 003562 if( IN_SPECIAL_PARSE ) zName[7]++; 003563 } 003564 003565 /* Check for authorization to create an index. 003566 */ 003567 #ifndef SQLITE_OMIT_AUTHORIZATION 003568 if( !IN_RENAME_OBJECT ){ 003569 const char *zDb = pDb->zDbSName; 003570 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ 003571 goto exit_create_index; 003572 } 003573 i = SQLITE_CREATE_INDEX; 003574 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; 003575 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ 003576 goto exit_create_index; 003577 } 003578 } 003579 #endif 003580 003581 /* If pList==0, it means this routine was called to make a primary 003582 ** key out of the last column added to the table under construction. 003583 ** So create a fake list to simulate this. 003584 */ 003585 if( pList==0 ){ 003586 Token prevCol; 003587 Column *pCol = &pTab->aCol[pTab->nCol-1]; 003588 pCol->colFlags |= COLFLAG_UNIQUE; 003589 sqlite3TokenInit(&prevCol, pCol->zName); 003590 pList = sqlite3ExprListAppend(pParse, 0, 003591 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); 003592 if( pList==0 ) goto exit_create_index; 003593 assert( pList->nExpr==1 ); 003594 sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED); 003595 }else{ 003596 sqlite3ExprListCheckLength(pParse, pList, "index"); 003597 if( pParse->nErr ) goto exit_create_index; 003598 } 003599 003600 /* Figure out how many bytes of space are required to store explicitly 003601 ** specified collation sequence names. 003602 */ 003603 for(i=0; i<pList->nExpr; i++){ 003604 Expr *pExpr = pList->a[i].pExpr; 003605 assert( pExpr!=0 ); 003606 if( pExpr->op==TK_COLLATE ){ 003607 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); 003608 } 003609 } 003610 003611 /* 003612 ** Allocate the index structure. 003613 */ 003614 nName = sqlite3Strlen30(zName); 003615 nExtraCol = pPk ? pPk->nKeyCol : 1; 003616 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ ); 003617 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, 003618 nName + nExtra + 1, &zExtra); 003619 if( db->mallocFailed ){ 003620 goto exit_create_index; 003621 } 003622 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); 003623 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); 003624 pIndex->zName = zExtra; 003625 zExtra += nName + 1; 003626 memcpy(pIndex->zName, zName, nName+1); 003627 pIndex->pTable = pTab; 003628 pIndex->onError = (u8)onError; 003629 pIndex->uniqNotNull = onError!=OE_None; 003630 pIndex->idxType = idxType; 003631 pIndex->pSchema = db->aDb[iDb].pSchema; 003632 pIndex->nKeyCol = pList->nExpr; 003633 if( pPIWhere ){ 003634 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); 003635 pIndex->pPartIdxWhere = pPIWhere; 003636 pPIWhere = 0; 003637 } 003638 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 003639 003640 /* Check to see if we should honor DESC requests on index columns 003641 */ 003642 if( pDb->pSchema->file_format>=4 ){ 003643 sortOrderMask = -1; /* Honor DESC */ 003644 }else{ 003645 sortOrderMask = 0; /* Ignore DESC */ 003646 } 003647 003648 /* Analyze the list of expressions that form the terms of the index and 003649 ** report any errors. In the common case where the expression is exactly 003650 ** a table column, store that column in aiColumn[]. For general expressions, 003651 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. 003652 ** 003653 ** TODO: Issue a warning if two or more columns of the index are identical. 003654 ** TODO: Issue a warning if the table primary key is used as part of the 003655 ** index key. 003656 */ 003657 pListItem = pList->a; 003658 if( IN_RENAME_OBJECT ){ 003659 pIndex->aColExpr = pList; 003660 pList = 0; 003661 } 003662 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){ 003663 Expr *pCExpr; /* The i-th index expression */ 003664 int requestedSortOrder; /* ASC or DESC on the i-th expression */ 003665 const char *zColl; /* Collation sequence name */ 003666 003667 sqlite3StringToId(pListItem->pExpr); 003668 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); 003669 if( pParse->nErr ) goto exit_create_index; 003670 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); 003671 if( pCExpr->op!=TK_COLUMN ){ 003672 if( pTab==pParse->pNewTable ){ 003673 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " 003674 "UNIQUE constraints"); 003675 goto exit_create_index; 003676 } 003677 if( pIndex->aColExpr==0 ){ 003678 pIndex->aColExpr = pList; 003679 pList = 0; 003680 } 003681 j = XN_EXPR; 003682 pIndex->aiColumn[i] = XN_EXPR; 003683 pIndex->uniqNotNull = 0; 003684 }else{ 003685 j = pCExpr->iColumn; 003686 assert( j<=0x7fff ); 003687 if( j<0 ){ 003688 j = pTab->iPKey; 003689 }else{ 003690 if( pTab->aCol[j].notNull==0 ){ 003691 pIndex->uniqNotNull = 0; 003692 } 003693 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){ 003694 pIndex->bHasVCol = 1; 003695 } 003696 } 003697 pIndex->aiColumn[i] = (i16)j; 003698 } 003699 zColl = 0; 003700 if( pListItem->pExpr->op==TK_COLLATE ){ 003701 int nColl; 003702 zColl = pListItem->pExpr->u.zToken; 003703 nColl = sqlite3Strlen30(zColl) + 1; 003704 assert( nExtra>=nColl ); 003705 memcpy(zExtra, zColl, nColl); 003706 zColl = zExtra; 003707 zExtra += nColl; 003708 nExtra -= nColl; 003709 }else if( j>=0 ){ 003710 zColl = pTab->aCol[j].zColl; 003711 } 003712 if( !zColl ) zColl = sqlite3StrBINARY; 003713 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ 003714 goto exit_create_index; 003715 } 003716 pIndex->azColl[i] = zColl; 003717 requestedSortOrder = pListItem->sortFlags & sortOrderMask; 003718 pIndex->aSortOrder[i] = (u8)requestedSortOrder; 003719 } 003720 003721 /* Append the table key to the end of the index. For WITHOUT ROWID 003722 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For 003723 ** normal tables (when pPk==0) this will be the rowid. 003724 */ 003725 if( pPk ){ 003726 for(j=0; j<pPk->nKeyCol; j++){ 003727 int x = pPk->aiColumn[j]; 003728 assert( x>=0 ); 003729 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){ 003730 pIndex->nColumn--; 003731 }else{ 003732 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) ); 003733 pIndex->aiColumn[i] = x; 003734 pIndex->azColl[i] = pPk->azColl[j]; 003735 pIndex->aSortOrder[i] = pPk->aSortOrder[j]; 003736 i++; 003737 } 003738 } 003739 assert( i==pIndex->nColumn ); 003740 }else{ 003741 pIndex->aiColumn[i] = XN_ROWID; 003742 pIndex->azColl[i] = sqlite3StrBINARY; 003743 } 003744 sqlite3DefaultRowEst(pIndex); 003745 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); 003746 003747 /* If this index contains every column of its table, then mark 003748 ** it as a covering index */ 003749 assert( HasRowid(pTab) 003750 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 ); 003751 recomputeColumnsNotIndexed(pIndex); 003752 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ 003753 pIndex->isCovering = 1; 003754 for(j=0; j<pTab->nCol; j++){ 003755 if( j==pTab->iPKey ) continue; 003756 if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue; 003757 pIndex->isCovering = 0; 003758 break; 003759 } 003760 } 003761 003762 if( pTab==pParse->pNewTable ){ 003763 /* This routine has been called to create an automatic index as a 003764 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or 003765 ** a PRIMARY KEY or UNIQUE clause following the column definitions. 003766 ** i.e. one of: 003767 ** 003768 ** CREATE TABLE t(x PRIMARY KEY, y); 003769 ** CREATE TABLE t(x, y, UNIQUE(x, y)); 003770 ** 003771 ** Either way, check to see if the table already has such an index. If 003772 ** so, don't bother creating this one. This only applies to 003773 ** automatically created indices. Users can do as they wish with 003774 ** explicit indices. 003775 ** 003776 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent 003777 ** (and thus suppressing the second one) even if they have different 003778 ** sort orders. 003779 ** 003780 ** If there are different collating sequences or if the columns of 003781 ** the constraint occur in different orders, then the constraints are 003782 ** considered distinct and both result in separate indices. 003783 */ 003784 Index *pIdx; 003785 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 003786 int k; 003787 assert( IsUniqueIndex(pIdx) ); 003788 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); 003789 assert( IsUniqueIndex(pIndex) ); 003790 003791 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; 003792 for(k=0; k<pIdx->nKeyCol; k++){ 003793 const char *z1; 003794 const char *z2; 003795 assert( pIdx->aiColumn[k]>=0 ); 003796 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; 003797 z1 = pIdx->azColl[k]; 003798 z2 = pIndex->azColl[k]; 003799 if( sqlite3StrICmp(z1, z2) ) break; 003800 } 003801 if( k==pIdx->nKeyCol ){ 003802 if( pIdx->onError!=pIndex->onError ){ 003803 /* This constraint creates the same index as a previous 003804 ** constraint specified somewhere in the CREATE TABLE statement. 003805 ** However the ON CONFLICT clauses are different. If both this 003806 ** constraint and the previous equivalent constraint have explicit 003807 ** ON CONFLICT clauses this is an error. Otherwise, use the 003808 ** explicitly specified behavior for the index. 003809 */ 003810 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ 003811 sqlite3ErrorMsg(pParse, 003812 "conflicting ON CONFLICT clauses specified", 0); 003813 } 003814 if( pIdx->onError==OE_Default ){ 003815 pIdx->onError = pIndex->onError; 003816 } 003817 } 003818 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; 003819 if( IN_RENAME_OBJECT ){ 003820 pIndex->pNext = pParse->pNewIndex; 003821 pParse->pNewIndex = pIndex; 003822 pIndex = 0; 003823 } 003824 goto exit_create_index; 003825 } 003826 } 003827 } 003828 003829 if( !IN_RENAME_OBJECT ){ 003830 003831 /* Link the new Index structure to its table and to the other 003832 ** in-memory database structures. 003833 */ 003834 assert( pParse->nErr==0 ); 003835 if( db->init.busy ){ 003836 Index *p; 003837 assert( !IN_SPECIAL_PARSE ); 003838 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); 003839 if( pTblName!=0 ){ 003840 pIndex->tnum = db->init.newTnum; 003841 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){ 003842 sqlite3ErrorMsg(pParse, "invalid rootpage"); 003843 pParse->rc = SQLITE_CORRUPT_BKPT; 003844 goto exit_create_index; 003845 } 003846 } 003847 p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 003848 pIndex->zName, pIndex); 003849 if( p ){ 003850 assert( p==pIndex ); /* Malloc must have failed */ 003851 sqlite3OomFault(db); 003852 goto exit_create_index; 003853 } 003854 db->mDbFlags |= DBFLAG_SchemaChange; 003855 } 003856 003857 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the 003858 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then 003859 ** emit code to allocate the index rootpage on disk and make an entry for 003860 ** the index in the sqlite_master table and populate the index with 003861 ** content. But, do not do this if we are simply reading the sqlite_master 003862 ** table to parse the schema, or if this index is the PRIMARY KEY index 003863 ** of a WITHOUT ROWID table. 003864 ** 003865 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY 003866 ** or UNIQUE index in a CREATE TABLE statement. Since the table 003867 ** has just been created, it contains no data and the index initialization 003868 ** step can be skipped. 003869 */ 003870 else if( HasRowid(pTab) || pTblName!=0 ){ 003871 Vdbe *v; 003872 char *zStmt; 003873 int iMem = ++pParse->nMem; 003874 003875 v = sqlite3GetVdbe(pParse); 003876 if( v==0 ) goto exit_create_index; 003877 003878 sqlite3BeginWriteOperation(pParse, 1, iDb); 003879 003880 /* Create the rootpage for the index using CreateIndex. But before 003881 ** doing so, code a Noop instruction and store its address in 003882 ** Index.tnum. This is required in case this index is actually a 003883 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 003884 ** that case the convertToWithoutRowidTable() routine will replace 003885 ** the Noop with a Goto to jump over the VDBE code generated below. */ 003886 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop); 003887 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY); 003888 003889 /* Gather the complete text of the CREATE INDEX statement into 003890 ** the zStmt variable 003891 */ 003892 assert( pName!=0 || pStart==0 ); 003893 if( pStart ){ 003894 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; 003895 if( pName->z[n-1]==';' ) n--; 003896 /* A named index with an explicit CREATE INDEX statement */ 003897 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", 003898 onError==OE_None ? "" : " UNIQUE", n, pName->z); 003899 }else{ 003900 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ 003901 /* zStmt = sqlite3MPrintf(""); */ 003902 zStmt = 0; 003903 } 003904 003905 /* Add an entry in sqlite_master for this index 003906 */ 003907 sqlite3NestedParse(pParse, 003908 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", 003909 db->aDb[iDb].zDbSName, MASTER_NAME, 003910 pIndex->zName, 003911 pTab->zName, 003912 iMem, 003913 zStmt 003914 ); 003915 sqlite3DbFree(db, zStmt); 003916 003917 /* Fill the index with data and reparse the schema. Code an OP_Expire 003918 ** to invalidate all pre-compiled statements. 003919 */ 003920 if( pTblName ){ 003921 sqlite3RefillIndex(pParse, pIndex, iMem); 003922 sqlite3ChangeCookie(pParse, iDb); 003923 sqlite3VdbeAddParseSchemaOp(v, iDb, 003924 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); 003925 sqlite3VdbeAddOp2(v, OP_Expire, 0, 1); 003926 } 003927 003928 sqlite3VdbeJumpHere(v, pIndex->tnum); 003929 } 003930 } 003931 if( db->init.busy || pTblName==0 ){ 003932 pIndex->pNext = pTab->pIndex; 003933 pTab->pIndex = pIndex; 003934 pIndex = 0; 003935 } 003936 else if( IN_RENAME_OBJECT ){ 003937 assert( pParse->pNewIndex==0 ); 003938 pParse->pNewIndex = pIndex; 003939 pIndex = 0; 003940 } 003941 003942 /* Clean up before exiting */ 003943 exit_create_index: 003944 if( pIndex ) sqlite3FreeIndex(db, pIndex); 003945 if( pTab ){ /* Ensure all REPLACE indexes are at the end of the list */ 003946 Index **ppFrom = &pTab->pIndex; 003947 Index *pThis; 003948 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){ 003949 Index *pNext; 003950 if( pThis->onError!=OE_Replace ) continue; 003951 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){ 003952 *ppFrom = pNext; 003953 pThis->pNext = pNext->pNext; 003954 pNext->pNext = pThis; 003955 ppFrom = &pNext->pNext; 003956 } 003957 break; 003958 } 003959 } 003960 sqlite3ExprDelete(db, pPIWhere); 003961 sqlite3ExprListDelete(db, pList); 003962 sqlite3SrcListDelete(db, pTblName); 003963 sqlite3DbFree(db, zName); 003964 } 003965 003966 /* 003967 ** Fill the Index.aiRowEst[] array with default information - information 003968 ** to be used when we have not run the ANALYZE command. 003969 ** 003970 ** aiRowEst[0] is supposed to contain the number of elements in the index. 003971 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the 003972 ** number of rows in the table that match any particular value of the 003973 ** first column of the index. aiRowEst[2] is an estimate of the number 003974 ** of rows that match any particular combination of the first 2 columns 003975 ** of the index. And so forth. It must always be the case that 003976 * 003977 ** aiRowEst[N]<=aiRowEst[N-1] 003978 ** aiRowEst[N]>=1 003979 ** 003980 ** Apart from that, we have little to go on besides intuition as to 003981 ** how aiRowEst[] should be initialized. The numbers generated here 003982 ** are based on typical values found in actual indices. 003983 */ 003984 void sqlite3DefaultRowEst(Index *pIdx){ 003985 /* 10, 9, 8, 7, 6 */ 003986 LogEst aVal[] = { 33, 32, 30, 28, 26 }; 003987 LogEst *a = pIdx->aiRowLogEst; 003988 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); 003989 int i; 003990 003991 /* Indexes with default row estimates should not have stat1 data */ 003992 assert( !pIdx->hasStat1 ); 003993 003994 /* Set the first entry (number of rows in the index) to the estimated 003995 ** number of rows in the table, or half the number of rows in the table 003996 ** for a partial index. But do not let the estimate drop below 10. */ 003997 a[0] = pIdx->pTable->nRowLogEst; 003998 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) ); 003999 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) ); 004000 004001 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is 004002 ** 6 and each subsequent value (if any) is 5. */ 004003 memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); 004004 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ 004005 a[i] = 23; assert( 23==sqlite3LogEst(5) ); 004006 } 004007 004008 assert( 0==sqlite3LogEst(1) ); 004009 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; 004010 } 004011 004012 /* 004013 ** This routine will drop an existing named index. This routine 004014 ** implements the DROP INDEX statement. 004015 */ 004016 void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ 004017 Index *pIndex; 004018 Vdbe *v; 004019 sqlite3 *db = pParse->db; 004020 int iDb; 004021 004022 assert( pParse->nErr==0 ); /* Never called with prior errors */ 004023 if( db->mallocFailed ){ 004024 goto exit_drop_index; 004025 } 004026 assert( pName->nSrc==1 ); 004027 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 004028 goto exit_drop_index; 004029 } 004030 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); 004031 if( pIndex==0 ){ 004032 if( !ifExists ){ 004033 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0); 004034 }else{ 004035 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); 004036 } 004037 pParse->checkSchema = 1; 004038 goto exit_drop_index; 004039 } 004040 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ 004041 sqlite3ErrorMsg(pParse, "index associated with UNIQUE " 004042 "or PRIMARY KEY constraint cannot be dropped", 0); 004043 goto exit_drop_index; 004044 } 004045 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); 004046 #ifndef SQLITE_OMIT_AUTHORIZATION 004047 { 004048 int code = SQLITE_DROP_INDEX; 004049 Table *pTab = pIndex->pTable; 004050 const char *zDb = db->aDb[iDb].zDbSName; 004051 const char *zTab = SCHEMA_TABLE(iDb); 004052 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ 004053 goto exit_drop_index; 004054 } 004055 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; 004056 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ 004057 goto exit_drop_index; 004058 } 004059 } 004060 #endif 004061 004062 /* Generate code to remove the index and from the master table */ 004063 v = sqlite3GetVdbe(pParse); 004064 if( v ){ 004065 sqlite3BeginWriteOperation(pParse, 1, iDb); 004066 sqlite3NestedParse(pParse, 004067 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", 004068 db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName 004069 ); 004070 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); 004071 sqlite3ChangeCookie(pParse, iDb); 004072 destroyRootPage(pParse, pIndex->tnum, iDb); 004073 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); 004074 } 004075 004076 exit_drop_index: 004077 sqlite3SrcListDelete(db, pName); 004078 } 004079 004080 /* 004081 ** pArray is a pointer to an array of objects. Each object in the 004082 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() 004083 ** to extend the array so that there is space for a new object at the end. 004084 ** 004085 ** When this function is called, *pnEntry contains the current size of 004086 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes 004087 ** in total). 004088 ** 004089 ** If the realloc() is successful (i.e. if no OOM condition occurs), the 004090 ** space allocated for the new object is zeroed, *pnEntry updated to 004091 ** reflect the new size of the array and a pointer to the new allocation 004092 ** returned. *pIdx is set to the index of the new array entry in this case. 004093 ** 004094 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains 004095 ** unchanged and a copy of pArray returned. 004096 */ 004097 void *sqlite3ArrayAllocate( 004098 sqlite3 *db, /* Connection to notify of malloc failures */ 004099 void *pArray, /* Array of objects. Might be reallocated */ 004100 int szEntry, /* Size of each object in the array */ 004101 int *pnEntry, /* Number of objects currently in use */ 004102 int *pIdx /* Write the index of a new slot here */ 004103 ){ 004104 char *z; 004105 sqlite3_int64 n = *pIdx = *pnEntry; 004106 if( (n & (n-1))==0 ){ 004107 sqlite3_int64 sz = (n==0) ? 1 : 2*n; 004108 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); 004109 if( pNew==0 ){ 004110 *pIdx = -1; 004111 return pArray; 004112 } 004113 pArray = pNew; 004114 } 004115 z = (char*)pArray; 004116 memset(&z[n * szEntry], 0, szEntry); 004117 ++*pnEntry; 004118 return pArray; 004119 } 004120 004121 /* 004122 ** Append a new element to the given IdList. Create a new IdList if 004123 ** need be. 004124 ** 004125 ** A new IdList is returned, or NULL if malloc() fails. 004126 */ 004127 IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){ 004128 sqlite3 *db = pParse->db; 004129 int i; 004130 if( pList==0 ){ 004131 pList = sqlite3DbMallocZero(db, sizeof(IdList) ); 004132 if( pList==0 ) return 0; 004133 } 004134 pList->a = sqlite3ArrayAllocate( 004135 db, 004136 pList->a, 004137 sizeof(pList->a[0]), 004138 &pList->nId, 004139 &i 004140 ); 004141 if( i<0 ){ 004142 sqlite3IdListDelete(db, pList); 004143 return 0; 004144 } 004145 pList->a[i].zName = sqlite3NameFromToken(db, pToken); 004146 if( IN_RENAME_OBJECT && pList->a[i].zName ){ 004147 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken); 004148 } 004149 return pList; 004150 } 004151 004152 /* 004153 ** Delete an IdList. 004154 */ 004155 void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ 004156 int i; 004157 if( pList==0 ) return; 004158 for(i=0; i<pList->nId; i++){ 004159 sqlite3DbFree(db, pList->a[i].zName); 004160 } 004161 sqlite3DbFree(db, pList->a); 004162 sqlite3DbFreeNN(db, pList); 004163 } 004164 004165 /* 004166 ** Return the index in pList of the identifier named zId. Return -1 004167 ** if not found. 004168 */ 004169 int sqlite3IdListIndex(IdList *pList, const char *zName){ 004170 int i; 004171 if( pList==0 ) return -1; 004172 for(i=0; i<pList->nId; i++){ 004173 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; 004174 } 004175 return -1; 004176 } 004177 004178 /* 004179 ** Maximum size of a SrcList object. 004180 ** The SrcList object is used to represent the FROM clause of a 004181 ** SELECT statement, and the query planner cannot deal with more 004182 ** than 64 tables in a join. So any value larger than 64 here 004183 ** is sufficient for most uses. Smaller values, like say 10, are 004184 ** appropriate for small and memory-limited applications. 004185 */ 004186 #ifndef SQLITE_MAX_SRCLIST 004187 # define SQLITE_MAX_SRCLIST 200 004188 #endif 004189 004190 /* 004191 ** Expand the space allocated for the given SrcList object by 004192 ** creating nExtra new slots beginning at iStart. iStart is zero based. 004193 ** New slots are zeroed. 004194 ** 004195 ** For example, suppose a SrcList initially contains two entries: A,B. 004196 ** To append 3 new entries onto the end, do this: 004197 ** 004198 ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); 004199 ** 004200 ** After the call above it would contain: A, B, nil, nil, nil. 004201 ** If the iStart argument had been 1 instead of 2, then the result 004202 ** would have been: A, nil, nil, nil, B. To prepend the new slots, 004203 ** the iStart value would be 0. The result then would 004204 ** be: nil, nil, nil, A, B. 004205 ** 004206 ** If a memory allocation fails or the SrcList becomes too large, leave 004207 ** the original SrcList unchanged, return NULL, and leave an error message 004208 ** in pParse. 004209 */ 004210 SrcList *sqlite3SrcListEnlarge( 004211 Parse *pParse, /* Parsing context into which errors are reported */ 004212 SrcList *pSrc, /* The SrcList to be enlarged */ 004213 int nExtra, /* Number of new slots to add to pSrc->a[] */ 004214 int iStart /* Index in pSrc->a[] of first new slot */ 004215 ){ 004216 int i; 004217 004218 /* Sanity checking on calling parameters */ 004219 assert( iStart>=0 ); 004220 assert( nExtra>=1 ); 004221 assert( pSrc!=0 ); 004222 assert( iStart<=pSrc->nSrc ); 004223 004224 /* Allocate additional space if needed */ 004225 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ 004226 SrcList *pNew; 004227 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra; 004228 sqlite3 *db = pParse->db; 004229 004230 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){ 004231 sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d", 004232 SQLITE_MAX_SRCLIST); 004233 return 0; 004234 } 004235 if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST; 004236 pNew = sqlite3DbRealloc(db, pSrc, 004237 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); 004238 if( pNew==0 ){ 004239 assert( db->mallocFailed ); 004240 return 0; 004241 } 004242 pSrc = pNew; 004243 pSrc->nAlloc = nAlloc; 004244 } 004245 004246 /* Move existing slots that come after the newly inserted slots 004247 ** out of the way */ 004248 for(i=pSrc->nSrc-1; i>=iStart; i--){ 004249 pSrc->a[i+nExtra] = pSrc->a[i]; 004250 } 004251 pSrc->nSrc += nExtra; 004252 004253 /* Zero the newly allocated slots */ 004254 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); 004255 for(i=iStart; i<iStart+nExtra; i++){ 004256 pSrc->a[i].iCursor = -1; 004257 } 004258 004259 /* Return a pointer to the enlarged SrcList */ 004260 return pSrc; 004261 } 004262 004263 004264 /* 004265 ** Append a new table name to the given SrcList. Create a new SrcList if 004266 ** need be. A new entry is created in the SrcList even if pTable is NULL. 004267 ** 004268 ** A SrcList is returned, or NULL if there is an OOM error or if the 004269 ** SrcList grows to large. The returned 004270 ** SrcList might be the same as the SrcList that was input or it might be 004271 ** a new one. If an OOM error does occurs, then the prior value of pList 004272 ** that is input to this routine is automatically freed. 004273 ** 004274 ** If pDatabase is not null, it means that the table has an optional 004275 ** database name prefix. Like this: "database.table". The pDatabase 004276 ** points to the table name and the pTable points to the database name. 004277 ** The SrcList.a[].zName field is filled with the table name which might 004278 ** come from pTable (if pDatabase is NULL) or from pDatabase. 004279 ** SrcList.a[].zDatabase is filled with the database name from pTable, 004280 ** or with NULL if no database is specified. 004281 ** 004282 ** In other words, if call like this: 004283 ** 004284 ** sqlite3SrcListAppend(D,A,B,0); 004285 ** 004286 ** Then B is a table name and the database name is unspecified. If called 004287 ** like this: 004288 ** 004289 ** sqlite3SrcListAppend(D,A,B,C); 004290 ** 004291 ** Then C is the table name and B is the database name. If C is defined 004292 ** then so is B. In other words, we never have a case where: 004293 ** 004294 ** sqlite3SrcListAppend(D,A,0,C); 004295 ** 004296 ** Both pTable and pDatabase are assumed to be quoted. They are dequoted 004297 ** before being added to the SrcList. 004298 */ 004299 SrcList *sqlite3SrcListAppend( 004300 Parse *pParse, /* Parsing context, in which errors are reported */ 004301 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ 004302 Token *pTable, /* Table to append */ 004303 Token *pDatabase /* Database of the table */ 004304 ){ 004305 struct SrcList_item *pItem; 004306 sqlite3 *db; 004307 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ 004308 assert( pParse!=0 ); 004309 assert( pParse->db!=0 ); 004310 db = pParse->db; 004311 if( pList==0 ){ 004312 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) ); 004313 if( pList==0 ) return 0; 004314 pList->nAlloc = 1; 004315 pList->nSrc = 1; 004316 memset(&pList->a[0], 0, sizeof(pList->a[0])); 004317 pList->a[0].iCursor = -1; 004318 }else{ 004319 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc); 004320 if( pNew==0 ){ 004321 sqlite3SrcListDelete(db, pList); 004322 return 0; 004323 }else{ 004324 pList = pNew; 004325 } 004326 } 004327 pItem = &pList->a[pList->nSrc-1]; 004328 if( pDatabase && pDatabase->z==0 ){ 004329 pDatabase = 0; 004330 } 004331 if( pDatabase ){ 004332 pItem->zName = sqlite3NameFromToken(db, pDatabase); 004333 pItem->zDatabase = sqlite3NameFromToken(db, pTable); 004334 }else{ 004335 pItem->zName = sqlite3NameFromToken(db, pTable); 004336 pItem->zDatabase = 0; 004337 } 004338 return pList; 004339 } 004340 004341 /* 004342 ** Assign VdbeCursor index numbers to all tables in a SrcList 004343 */ 004344 void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ 004345 int i; 004346 struct SrcList_item *pItem; 004347 assert(pList || pParse->db->mallocFailed ); 004348 if( pList ){ 004349 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ 004350 if( pItem->iCursor>=0 ) break; 004351 pItem->iCursor = pParse->nTab++; 004352 if( pItem->pSelect ){ 004353 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); 004354 } 004355 } 004356 } 004357 } 004358 004359 /* 004360 ** Delete an entire SrcList including all its substructure. 004361 */ 004362 void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ 004363 int i; 004364 struct SrcList_item *pItem; 004365 if( pList==0 ) return; 004366 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ 004367 sqlite3DbFree(db, pItem->zDatabase); 004368 sqlite3DbFree(db, pItem->zName); 004369 sqlite3DbFree(db, pItem->zAlias); 004370 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); 004371 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); 004372 sqlite3DeleteTable(db, pItem->pTab); 004373 sqlite3SelectDelete(db, pItem->pSelect); 004374 sqlite3ExprDelete(db, pItem->pOn); 004375 sqlite3IdListDelete(db, pItem->pUsing); 004376 } 004377 sqlite3DbFreeNN(db, pList); 004378 } 004379 004380 /* 004381 ** This routine is called by the parser to add a new term to the 004382 ** end of a growing FROM clause. The "p" parameter is the part of 004383 ** the FROM clause that has already been constructed. "p" is NULL 004384 ** if this is the first term of the FROM clause. pTable and pDatabase 004385 ** are the name of the table and database named in the FROM clause term. 004386 ** pDatabase is NULL if the database name qualifier is missing - the 004387 ** usual case. If the term has an alias, then pAlias points to the 004388 ** alias token. If the term is a subquery, then pSubquery is the 004389 ** SELECT statement that the subquery encodes. The pTable and 004390 ** pDatabase parameters are NULL for subqueries. The pOn and pUsing 004391 ** parameters are the content of the ON and USING clauses. 004392 ** 004393 ** Return a new SrcList which encodes is the FROM with the new 004394 ** term added. 004395 */ 004396 SrcList *sqlite3SrcListAppendFromTerm( 004397 Parse *pParse, /* Parsing context */ 004398 SrcList *p, /* The left part of the FROM clause already seen */ 004399 Token *pTable, /* Name of the table to add to the FROM clause */ 004400 Token *pDatabase, /* Name of the database containing pTable */ 004401 Token *pAlias, /* The right-hand side of the AS subexpression */ 004402 Select *pSubquery, /* A subquery used in place of a table name */ 004403 Expr *pOn, /* The ON clause of a join */ 004404 IdList *pUsing /* The USING clause of a join */ 004405 ){ 004406 struct SrcList_item *pItem; 004407 sqlite3 *db = pParse->db; 004408 if( !p && (pOn || pUsing) ){ 004409 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 004410 (pOn ? "ON" : "USING") 004411 ); 004412 goto append_from_error; 004413 } 004414 p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase); 004415 if( p==0 ){ 004416 goto append_from_error; 004417 } 004418 assert( p->nSrc>0 ); 004419 pItem = &p->a[p->nSrc-1]; 004420 assert( (pTable==0)==(pDatabase==0) ); 004421 assert( pItem->zName==0 || pDatabase!=0 ); 004422 if( IN_RENAME_OBJECT && pItem->zName ){ 004423 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable; 004424 sqlite3RenameTokenMap(pParse, pItem->zName, pToken); 004425 } 004426 assert( pAlias!=0 ); 004427 if( pAlias->n ){ 004428 pItem->zAlias = sqlite3NameFromToken(db, pAlias); 004429 } 004430 pItem->pSelect = pSubquery; 004431 pItem->pOn = pOn; 004432 pItem->pUsing = pUsing; 004433 return p; 004434 004435 append_from_error: 004436 assert( p==0 ); 004437 sqlite3ExprDelete(db, pOn); 004438 sqlite3IdListDelete(db, pUsing); 004439 sqlite3SelectDelete(db, pSubquery); 004440 return 0; 004441 } 004442 004443 /* 004444 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 004445 ** element of the source-list passed as the second argument. 004446 */ 004447 void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ 004448 assert( pIndexedBy!=0 ); 004449 if( p && pIndexedBy->n>0 ){ 004450 struct SrcList_item *pItem; 004451 assert( p->nSrc>0 ); 004452 pItem = &p->a[p->nSrc-1]; 004453 assert( pItem->fg.notIndexed==0 ); 004454 assert( pItem->fg.isIndexedBy==0 ); 004455 assert( pItem->fg.isTabFunc==0 ); 004456 if( pIndexedBy->n==1 && !pIndexedBy->z ){ 004457 /* A "NOT INDEXED" clause was supplied. See parse.y 004458 ** construct "indexed_opt" for details. */ 004459 pItem->fg.notIndexed = 1; 004460 }else{ 004461 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); 004462 pItem->fg.isIndexedBy = 1; 004463 } 004464 } 004465 } 004466 004467 /* 004468 ** Add the list of function arguments to the SrcList entry for a 004469 ** table-valued-function. 004470 */ 004471 void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ 004472 if( p ){ 004473 struct SrcList_item *pItem = &p->a[p->nSrc-1]; 004474 assert( pItem->fg.notIndexed==0 ); 004475 assert( pItem->fg.isIndexedBy==0 ); 004476 assert( pItem->fg.isTabFunc==0 ); 004477 pItem->u1.pFuncArg = pList; 004478 pItem->fg.isTabFunc = 1; 004479 }else{ 004480 sqlite3ExprListDelete(pParse->db, pList); 004481 } 004482 } 004483 004484 /* 004485 ** When building up a FROM clause in the parser, the join operator 004486 ** is initially attached to the left operand. But the code generator 004487 ** expects the join operator to be on the right operand. This routine 004488 ** Shifts all join operators from left to right for an entire FROM 004489 ** clause. 004490 ** 004491 ** Example: Suppose the join is like this: 004492 ** 004493 ** A natural cross join B 004494 ** 004495 ** The operator is "natural cross join". The A and B operands are stored 004496 ** in p->a[0] and p->a[1], respectively. The parser initially stores the 004497 ** operator with A. This routine shifts that operator over to B. 004498 */ 004499 void sqlite3SrcListShiftJoinType(SrcList *p){ 004500 if( p ){ 004501 int i; 004502 for(i=p->nSrc-1; i>0; i--){ 004503 p->a[i].fg.jointype = p->a[i-1].fg.jointype; 004504 } 004505 p->a[0].fg.jointype = 0; 004506 } 004507 } 004508 004509 /* 004510 ** Generate VDBE code for a BEGIN statement. 004511 */ 004512 void sqlite3BeginTransaction(Parse *pParse, int type){ 004513 sqlite3 *db; 004514 Vdbe *v; 004515 int i; 004516 004517 assert( pParse!=0 ); 004518 db = pParse->db; 004519 assert( db!=0 ); 004520 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ 004521 return; 004522 } 004523 v = sqlite3GetVdbe(pParse); 004524 if( !v ) return; 004525 if( type!=TK_DEFERRED ){ 004526 for(i=0; i<db->nDb; i++){ 004527 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); 004528 sqlite3VdbeUsesBtree(v, i); 004529 } 004530 } 004531 sqlite3VdbeAddOp0(v, OP_AutoCommit); 004532 } 004533 004534 /* 004535 ** Generate VDBE code for a COMMIT or ROLLBACK statement. 004536 ** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise 004537 ** code is generated for a COMMIT. 004538 */ 004539 void sqlite3EndTransaction(Parse *pParse, int eType){ 004540 Vdbe *v; 004541 int isRollback; 004542 004543 assert( pParse!=0 ); 004544 assert( pParse->db!=0 ); 004545 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK ); 004546 isRollback = eType==TK_ROLLBACK; 004547 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, 004548 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){ 004549 return; 004550 } 004551 v = sqlite3GetVdbe(pParse); 004552 if( v ){ 004553 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback); 004554 } 004555 } 004556 004557 /* 004558 ** This function is called by the parser when it parses a command to create, 004559 ** release or rollback an SQL savepoint. 004560 */ 004561 void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ 004562 char *zName = sqlite3NameFromToken(pParse->db, pName); 004563 if( zName ){ 004564 Vdbe *v = sqlite3GetVdbe(pParse); 004565 #ifndef SQLITE_OMIT_AUTHORIZATION 004566 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; 004567 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); 004568 #endif 004569 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ 004570 sqlite3DbFree(pParse->db, zName); 004571 return; 004572 } 004573 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); 004574 } 004575 } 004576 004577 /* 004578 ** Make sure the TEMP database is open and available for use. Return 004579 ** the number of errors. Leave any error messages in the pParse structure. 004580 */ 004581 int sqlite3OpenTempDatabase(Parse *pParse){ 004582 sqlite3 *db = pParse->db; 004583 if( db->aDb[1].pBt==0 && !pParse->explain ){ 004584 int rc; 004585 Btree *pBt; 004586 static const int flags = 004587 SQLITE_OPEN_READWRITE | 004588 SQLITE_OPEN_CREATE | 004589 SQLITE_OPEN_EXCLUSIVE | 004590 SQLITE_OPEN_DELETEONCLOSE | 004591 SQLITE_OPEN_TEMP_DB; 004592 004593 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); 004594 if( rc!=SQLITE_OK ){ 004595 sqlite3ErrorMsg(pParse, "unable to open a temporary database " 004596 "file for storing temporary tables"); 004597 pParse->rc = rc; 004598 return 1; 004599 } 004600 db->aDb[1].pBt = pBt; 004601 assert( db->aDb[1].pSchema ); 004602 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ 004603 sqlite3OomFault(db); 004604 return 1; 004605 } 004606 } 004607 return 0; 004608 } 004609 004610 /* 004611 ** Record the fact that the schema cookie will need to be verified 004612 ** for database iDb. The code to actually verify the schema cookie 004613 ** will occur at the end of the top-level VDBE and will be generated 004614 ** later, by sqlite3FinishCoding(). 004615 */ 004616 void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ 004617 Parse *pToplevel = sqlite3ParseToplevel(pParse); 004618 004619 assert( iDb>=0 && iDb<pParse->db->nDb ); 004620 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 ); 004621 assert( iDb<SQLITE_MAX_ATTACHED+2 ); 004622 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) ); 004623 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ 004624 DbMaskSet(pToplevel->cookieMask, iDb); 004625 if( !OMIT_TEMPDB && iDb==1 ){ 004626 sqlite3OpenTempDatabase(pToplevel); 004627 } 004628 } 004629 } 004630 004631 /* 004632 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 004633 ** attached database. Otherwise, invoke it for the database named zDb only. 004634 */ 004635 void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ 004636 sqlite3 *db = pParse->db; 004637 int i; 004638 for(i=0; i<db->nDb; i++){ 004639 Db *pDb = &db->aDb[i]; 004640 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ 004641 sqlite3CodeVerifySchema(pParse, i); 004642 } 004643 } 004644 } 004645 004646 /* 004647 ** Generate VDBE code that prepares for doing an operation that 004648 ** might change the database. 004649 ** 004650 ** This routine starts a new transaction if we are not already within 004651 ** a transaction. If we are already within a transaction, then a checkpoint 004652 ** is set if the setStatement parameter is true. A checkpoint should 004653 ** be set for operations that might fail (due to a constraint) part of 004654 ** the way through and which will need to undo some writes without having to 004655 ** rollback the whole transaction. For operations where all constraints 004656 ** can be checked before any changes are made to the database, it is never 004657 ** necessary to undo a write and the checkpoint should not be set. 004658 */ 004659 void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ 004660 Parse *pToplevel = sqlite3ParseToplevel(pParse); 004661 sqlite3CodeVerifySchema(pParse, iDb); 004662 DbMaskSet(pToplevel->writeMask, iDb); 004663 pToplevel->isMultiWrite |= setStatement; 004664 } 004665 004666 /* 004667 ** Indicate that the statement currently under construction might write 004668 ** more than one entry (example: deleting one row then inserting another, 004669 ** inserting multiple rows in a table, or inserting a row and index entries.) 004670 ** If an abort occurs after some of these writes have completed, then it will 004671 ** be necessary to undo the completed writes. 004672 */ 004673 void sqlite3MultiWrite(Parse *pParse){ 004674 Parse *pToplevel = sqlite3ParseToplevel(pParse); 004675 pToplevel->isMultiWrite = 1; 004676 } 004677 004678 /* 004679 ** The code generator calls this routine if is discovers that it is 004680 ** possible to abort a statement prior to completion. In order to 004681 ** perform this abort without corrupting the database, we need to make 004682 ** sure that the statement is protected by a statement transaction. 004683 ** 004684 ** Technically, we only need to set the mayAbort flag if the 004685 ** isMultiWrite flag was previously set. There is a time dependency 004686 ** such that the abort must occur after the multiwrite. This makes 004687 ** some statements involving the REPLACE conflict resolution algorithm 004688 ** go a little faster. But taking advantage of this time dependency 004689 ** makes it more difficult to prove that the code is correct (in 004690 ** particular, it prevents us from writing an effective 004691 ** implementation of sqlite3AssertMayAbort()) and so we have chosen 004692 ** to take the safe route and skip the optimization. 004693 */ 004694 void sqlite3MayAbort(Parse *pParse){ 004695 Parse *pToplevel = sqlite3ParseToplevel(pParse); 004696 pToplevel->mayAbort = 1; 004697 } 004698 004699 /* 004700 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT 004701 ** error. The onError parameter determines which (if any) of the statement 004702 ** and/or current transaction is rolled back. 004703 */ 004704 void sqlite3HaltConstraint( 004705 Parse *pParse, /* Parsing context */ 004706 int errCode, /* extended error code */ 004707 int onError, /* Constraint type */ 004708 char *p4, /* Error message */ 004709 i8 p4type, /* P4_STATIC or P4_TRANSIENT */ 004710 u8 p5Errmsg /* P5_ErrMsg type */ 004711 ){ 004712 Vdbe *v = sqlite3GetVdbe(pParse); 004713 assert( (errCode&0xff)==SQLITE_CONSTRAINT ); 004714 if( onError==OE_Abort ){ 004715 sqlite3MayAbort(pParse); 004716 } 004717 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); 004718 sqlite3VdbeChangeP5(v, p5Errmsg); 004719 } 004720 004721 /* 004722 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. 004723 */ 004724 void sqlite3UniqueConstraint( 004725 Parse *pParse, /* Parsing context */ 004726 int onError, /* Constraint type */ 004727 Index *pIdx /* The index that triggers the constraint */ 004728 ){ 004729 char *zErr; 004730 int j; 004731 StrAccum errMsg; 004732 Table *pTab = pIdx->pTable; 004733 004734 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 004735 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]); 004736 if( pIdx->aColExpr ){ 004737 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName); 004738 }else{ 004739 for(j=0; j<pIdx->nKeyCol; j++){ 004740 char *zCol; 004741 assert( pIdx->aiColumn[j]>=0 ); 004742 zCol = pTab->aCol[pIdx->aiColumn[j]].zName; 004743 if( j ) sqlite3_str_append(&errMsg, ", ", 2); 004744 sqlite3_str_appendall(&errMsg, pTab->zName); 004745 sqlite3_str_append(&errMsg, ".", 1); 004746 sqlite3_str_appendall(&errMsg, zCol); 004747 } 004748 } 004749 zErr = sqlite3StrAccumFinish(&errMsg); 004750 sqlite3HaltConstraint(pParse, 004751 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY 004752 : SQLITE_CONSTRAINT_UNIQUE, 004753 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); 004754 } 004755 004756 004757 /* 004758 ** Code an OP_Halt due to non-unique rowid. 004759 */ 004760 void sqlite3RowidConstraint( 004761 Parse *pParse, /* Parsing context */ 004762 int onError, /* Conflict resolution algorithm */ 004763 Table *pTab /* The table with the non-unique rowid */ 004764 ){ 004765 char *zMsg; 004766 int rc; 004767 if( pTab->iPKey>=0 ){ 004768 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, 004769 pTab->aCol[pTab->iPKey].zName); 004770 rc = SQLITE_CONSTRAINT_PRIMARYKEY; 004771 }else{ 004772 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); 004773 rc = SQLITE_CONSTRAINT_ROWID; 004774 } 004775 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC, 004776 P5_ConstraintUnique); 004777 } 004778 004779 /* 004780 ** Check to see if pIndex uses the collating sequence pColl. Return 004781 ** true if it does and false if it does not. 004782 */ 004783 #ifndef SQLITE_OMIT_REINDEX 004784 static int collationMatch(const char *zColl, Index *pIndex){ 004785 int i; 004786 assert( zColl!=0 ); 004787 for(i=0; i<pIndex->nColumn; i++){ 004788 const char *z = pIndex->azColl[i]; 004789 assert( z!=0 || pIndex->aiColumn[i]<0 ); 004790 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){ 004791 return 1; 004792 } 004793 } 004794 return 0; 004795 } 004796 #endif 004797 004798 /* 004799 ** Recompute all indices of pTab that use the collating sequence pColl. 004800 ** If pColl==0 then recompute all indices of pTab. 004801 */ 004802 #ifndef SQLITE_OMIT_REINDEX 004803 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ 004804 if( !IsVirtual(pTab) ){ 004805 Index *pIndex; /* An index associated with pTab */ 004806 004807 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ 004808 if( zColl==0 || collationMatch(zColl, pIndex) ){ 004809 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 004810 sqlite3BeginWriteOperation(pParse, 0, iDb); 004811 sqlite3RefillIndex(pParse, pIndex, -1); 004812 } 004813 } 004814 } 004815 } 004816 #endif 004817 004818 /* 004819 ** Recompute all indices of all tables in all databases where the 004820 ** indices use the collating sequence pColl. If pColl==0 then recompute 004821 ** all indices everywhere. 004822 */ 004823 #ifndef SQLITE_OMIT_REINDEX 004824 static void reindexDatabases(Parse *pParse, char const *zColl){ 004825 Db *pDb; /* A single database */ 004826 int iDb; /* The database index number */ 004827 sqlite3 *db = pParse->db; /* The database connection */ 004828 HashElem *k; /* For looping over tables in pDb */ 004829 Table *pTab; /* A table in the database */ 004830 004831 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */ 004832 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ 004833 assert( pDb!=0 ); 004834 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ 004835 pTab = (Table*)sqliteHashData(k); 004836 reindexTable(pParse, pTab, zColl); 004837 } 004838 } 004839 } 004840 #endif 004841 004842 /* 004843 ** Generate code for the REINDEX command. 004844 ** 004845 ** REINDEX -- 1 004846 ** REINDEX <collation> -- 2 004847 ** REINDEX ?<database>.?<tablename> -- 3 004848 ** REINDEX ?<database>.?<indexname> -- 4 004849 ** 004850 ** Form 1 causes all indices in all attached databases to be rebuilt. 004851 ** Form 2 rebuilds all indices in all databases that use the named 004852 ** collating function. Forms 3 and 4 rebuild the named index or all 004853 ** indices associated with the named table. 004854 */ 004855 #ifndef SQLITE_OMIT_REINDEX 004856 void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ 004857 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ 004858 char *z; /* Name of a table or index */ 004859 const char *zDb; /* Name of the database */ 004860 Table *pTab; /* A table in the database */ 004861 Index *pIndex; /* An index associated with pTab */ 004862 int iDb; /* The database index number */ 004863 sqlite3 *db = pParse->db; /* The database connection */ 004864 Token *pObjName; /* Name of the table or index to be reindexed */ 004865 004866 /* Read the database schema. If an error occurs, leave an error message 004867 ** and code in pParse and return NULL. */ 004868 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 004869 return; 004870 } 004871 004872 if( pName1==0 ){ 004873 reindexDatabases(pParse, 0); 004874 return; 004875 }else if( NEVER(pName2==0) || pName2->z==0 ){ 004876 char *zColl; 004877 assert( pName1->z ); 004878 zColl = sqlite3NameFromToken(pParse->db, pName1); 004879 if( !zColl ) return; 004880 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); 004881 if( pColl ){ 004882 reindexDatabases(pParse, zColl); 004883 sqlite3DbFree(db, zColl); 004884 return; 004885 } 004886 sqlite3DbFree(db, zColl); 004887 } 004888 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); 004889 if( iDb<0 ) return; 004890 z = sqlite3NameFromToken(db, pObjName); 004891 if( z==0 ) return; 004892 zDb = db->aDb[iDb].zDbSName; 004893 pTab = sqlite3FindTable(db, z, zDb); 004894 if( pTab ){ 004895 reindexTable(pParse, pTab, 0); 004896 sqlite3DbFree(db, z); 004897 return; 004898 } 004899 pIndex = sqlite3FindIndex(db, z, zDb); 004900 sqlite3DbFree(db, z); 004901 if( pIndex ){ 004902 sqlite3BeginWriteOperation(pParse, 0, iDb); 004903 sqlite3RefillIndex(pParse, pIndex, -1); 004904 return; 004905 } 004906 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); 004907 } 004908 #endif 004909 004910 /* 004911 ** Return a KeyInfo structure that is appropriate for the given Index. 004912 ** 004913 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object 004914 ** when it has finished using it. 004915 */ 004916 KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ 004917 int i; 004918 int nCol = pIdx->nColumn; 004919 int nKey = pIdx->nKeyCol; 004920 KeyInfo *pKey; 004921 if( pParse->nErr ) return 0; 004922 if( pIdx->uniqNotNull ){ 004923 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); 004924 }else{ 004925 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); 004926 } 004927 if( pKey ){ 004928 assert( sqlite3KeyInfoIsWriteable(pKey) ); 004929 for(i=0; i<nCol; i++){ 004930 const char *zColl = pIdx->azColl[i]; 004931 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : 004932 sqlite3LocateCollSeq(pParse, zColl); 004933 pKey->aSortFlags[i] = pIdx->aSortOrder[i]; 004934 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) ); 004935 } 004936 if( pParse->nErr ){ 004937 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ ); 004938 if( pIdx->bNoQuery==0 ){ 004939 /* Deactivate the index because it contains an unknown collating 004940 ** sequence. The only way to reactive the index is to reload the 004941 ** schema. Adding the missing collating sequence later does not 004942 ** reactive the index. The application had the chance to register 004943 ** the missing index using the collation-needed callback. For 004944 ** simplicity, SQLite will not give the application a second chance. 004945 */ 004946 pIdx->bNoQuery = 1; 004947 pParse->rc = SQLITE_ERROR_RETRY; 004948 } 004949 sqlite3KeyInfoUnref(pKey); 004950 pKey = 0; 004951 } 004952 } 004953 return pKey; 004954 } 004955 004956 #ifndef SQLITE_OMIT_CTE 004957 /* 004958 ** This routine is invoked once per CTE by the parser while parsing a 004959 ** WITH clause. 004960 */ 004961 With *sqlite3WithAdd( 004962 Parse *pParse, /* Parsing context */ 004963 With *pWith, /* Existing WITH clause, or NULL */ 004964 Token *pName, /* Name of the common-table */ 004965 ExprList *pArglist, /* Optional column name list for the table */ 004966 Select *pQuery /* Query used to initialize the table */ 004967 ){ 004968 sqlite3 *db = pParse->db; 004969 With *pNew; 004970 char *zName; 004971 004972 /* Check that the CTE name is unique within this WITH clause. If 004973 ** not, store an error in the Parse structure. */ 004974 zName = sqlite3NameFromToken(pParse->db, pName); 004975 if( zName && pWith ){ 004976 int i; 004977 for(i=0; i<pWith->nCte; i++){ 004978 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){ 004979 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName); 004980 } 004981 } 004982 } 004983 004984 if( pWith ){ 004985 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); 004986 pNew = sqlite3DbRealloc(db, pWith, nByte); 004987 }else{ 004988 pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); 004989 } 004990 assert( (pNew!=0 && zName!=0) || db->mallocFailed ); 004991 004992 if( db->mallocFailed ){ 004993 sqlite3ExprListDelete(db, pArglist); 004994 sqlite3SelectDelete(db, pQuery); 004995 sqlite3DbFree(db, zName); 004996 pNew = pWith; 004997 }else{ 004998 pNew->a[pNew->nCte].pSelect = pQuery; 004999 pNew->a[pNew->nCte].pCols = pArglist; 005000 pNew->a[pNew->nCte].zName = zName; 005001 pNew->a[pNew->nCte].zCteErr = 0; 005002 pNew->nCte++; 005003 } 005004 005005 return pNew; 005006 } 005007 005008 /* 005009 ** Free the contents of the With object passed as the second argument. 005010 */ 005011 void sqlite3WithDelete(sqlite3 *db, With *pWith){ 005012 if( pWith ){ 005013 int i; 005014 for(i=0; i<pWith->nCte; i++){ 005015 struct Cte *pCte = &pWith->a[i]; 005016 sqlite3ExprListDelete(db, pCte->pCols); 005017 sqlite3SelectDelete(db, pCte->pSelect); 005018 sqlite3DbFree(db, pCte->zName); 005019 } 005020 sqlite3DbFree(db, pWith); 005021 } 005022 } 005023 #endif /* !defined(SQLITE_OMIT_CTE) */