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