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 assert( p->pSchema==0 || p->zDatabase==0 ); 000501 if( p->pSchema ){ 000502 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); 000503 zDb = pParse->db->aDb[iDb].zDbSName; 000504 }else{ 000505 zDb = p->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 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; 003491 if( noErr ) db->suppressErr++; 003492 assert( isView==0 || isView==LOCATE_VIEW ); 003493 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); 003494 if( noErr ) db->suppressErr--; 003495 003496 if( pTab==0 ){ 003497 if( noErr ){ 003498 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); 003499 sqlite3ForceNotReadOnly(pParse); 003500 } 003501 goto exit_drop_table; 003502 } 003503 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 003504 assert( iDb>=0 && iDb<db->nDb ); 003505 003506 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure 003507 ** it is initialized. 003508 */ 003509 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ 003510 goto exit_drop_table; 003511 } 003512 #ifndef SQLITE_OMIT_AUTHORIZATION 003513 { 003514 int code; 003515 const char *zTab = SCHEMA_TABLE(iDb); 003516 const char *zDb = db->aDb[iDb].zDbSName; 003517 const char *zArg2 = 0; 003518 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ 003519 goto exit_drop_table; 003520 } 003521 if( isView ){ 003522 if( !OMIT_TEMPDB && iDb==1 ){ 003523 code = SQLITE_DROP_TEMP_VIEW; 003524 }else{ 003525 code = SQLITE_DROP_VIEW; 003526 } 003527 #ifndef SQLITE_OMIT_VIRTUALTABLE 003528 }else if( IsVirtual(pTab) ){ 003529 code = SQLITE_DROP_VTABLE; 003530 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; 003531 #endif 003532 }else{ 003533 if( !OMIT_TEMPDB && iDb==1 ){ 003534 code = SQLITE_DROP_TEMP_TABLE; 003535 }else{ 003536 code = SQLITE_DROP_TABLE; 003537 } 003538 } 003539 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ 003540 goto exit_drop_table; 003541 } 003542 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ 003543 goto exit_drop_table; 003544 } 003545 } 003546 #endif 003547 if( tableMayNotBeDropped(db, pTab) ){ 003548 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); 003549 goto exit_drop_table; 003550 } 003551 003552 #ifndef SQLITE_OMIT_VIEW 003553 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used 003554 ** on a table. 003555 */ 003556 if( isView && !IsView(pTab) ){ 003557 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); 003558 goto exit_drop_table; 003559 } 003560 if( !isView && IsView(pTab) ){ 003561 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); 003562 goto exit_drop_table; 003563 } 003564 #endif 003565 003566 /* Generate code to remove the table from the schema table 003567 ** on disk. 003568 */ 003569 v = sqlite3GetVdbe(pParse); 003570 if( v ){ 003571 sqlite3BeginWriteOperation(pParse, 1, iDb); 003572 if( !isView ){ 003573 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); 003574 sqlite3FkDropTable(pParse, pName, pTab); 003575 } 003576 sqlite3CodeDropTable(pParse, pTab, iDb, isView); 003577 } 003578 003579 exit_drop_table: 003580 sqlite3SrcListDelete(db, pName); 003581 } 003582 003583 /* 003584 ** This routine is called to create a new foreign key on the table 003585 ** currently under construction. pFromCol determines which columns 003586 ** in the current table point to the foreign key. If pFromCol==0 then 003587 ** connect the key to the last column inserted. pTo is the name of 003588 ** the table referred to (a.k.a the "parent" table). pToCol is a list 003589 ** of tables in the parent pTo table. flags contains all 003590 ** information about the conflict resolution algorithms specified 003591 ** in the ON DELETE, ON UPDATE and ON INSERT clauses. 003592 ** 003593 ** An FKey structure is created and added to the table currently 003594 ** under construction in the pParse->pNewTable field. 003595 ** 003596 ** The foreign key is set for IMMEDIATE processing. A subsequent call 003597 ** to sqlite3DeferForeignKey() might change this to DEFERRED. 003598 */ 003599 void sqlite3CreateForeignKey( 003600 Parse *pParse, /* Parsing context */ 003601 ExprList *pFromCol, /* Columns in this table that point to other table */ 003602 Token *pTo, /* Name of the other table */ 003603 ExprList *pToCol, /* Columns in the other table */ 003604 int flags /* Conflict resolution algorithms. */ 003605 ){ 003606 sqlite3 *db = pParse->db; 003607 #ifndef SQLITE_OMIT_FOREIGN_KEY 003608 FKey *pFKey = 0; 003609 FKey *pNextTo; 003610 Table *p = pParse->pNewTable; 003611 i64 nByte; 003612 int i; 003613 int nCol; 003614 char *z; 003615 003616 assert( pTo!=0 ); 003617 if( p==0 || IN_DECLARE_VTAB ) goto fk_end; 003618 if( pFromCol==0 ){ 003619 int iCol = p->nCol-1; 003620 if( NEVER(iCol<0) ) goto fk_end; 003621 if( pToCol && pToCol->nExpr!=1 ){ 003622 sqlite3ErrorMsg(pParse, "foreign key on %s" 003623 " should reference only one column of table %T", 003624 p->aCol[iCol].zCnName, pTo); 003625 goto fk_end; 003626 } 003627 nCol = 1; 003628 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ 003629 sqlite3ErrorMsg(pParse, 003630 "number of columns in foreign key does not match the number of " 003631 "columns in the referenced table"); 003632 goto fk_end; 003633 }else{ 003634 nCol = pFromCol->nExpr; 003635 } 003636 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; 003637 if( pToCol ){ 003638 for(i=0; i<pToCol->nExpr; i++){ 003639 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1; 003640 } 003641 } 003642 pFKey = sqlite3DbMallocZero(db, nByte ); 003643 if( pFKey==0 ){ 003644 goto fk_end; 003645 } 003646 pFKey->pFrom = p; 003647 assert( IsOrdinaryTable(p) ); 003648 pFKey->pNextFrom = p->u.tab.pFKey; 003649 z = (char*)&pFKey->aCol[nCol]; 003650 pFKey->zTo = z; 003651 if( IN_RENAME_OBJECT ){ 003652 sqlite3RenameTokenMap(pParse, (void*)z, pTo); 003653 } 003654 memcpy(z, pTo->z, pTo->n); 003655 z[pTo->n] = 0; 003656 sqlite3Dequote(z); 003657 z += pTo->n+1; 003658 pFKey->nCol = nCol; 003659 if( pFromCol==0 ){ 003660 pFKey->aCol[0].iFrom = p->nCol-1; 003661 }else{ 003662 for(i=0; i<nCol; i++){ 003663 int j; 003664 for(j=0; j<p->nCol; j++){ 003665 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){ 003666 pFKey->aCol[i].iFrom = j; 003667 break; 003668 } 003669 } 003670 if( j>=p->nCol ){ 003671 sqlite3ErrorMsg(pParse, 003672 "unknown column \"%s\" in foreign key definition", 003673 pFromCol->a[i].zEName); 003674 goto fk_end; 003675 } 003676 if( IN_RENAME_OBJECT ){ 003677 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName); 003678 } 003679 } 003680 } 003681 if( pToCol ){ 003682 for(i=0; i<nCol; i++){ 003683 int n = sqlite3Strlen30(pToCol->a[i].zEName); 003684 pFKey->aCol[i].zCol = z; 003685 if( IN_RENAME_OBJECT ){ 003686 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName); 003687 } 003688 memcpy(z, pToCol->a[i].zEName, n); 003689 z[n] = 0; 003690 z += n+1; 003691 } 003692 } 003693 pFKey->isDeferred = 0; 003694 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ 003695 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ 003696 003697 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); 003698 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 003699 pFKey->zTo, (void *)pFKey 003700 ); 003701 if( pNextTo==pFKey ){ 003702 sqlite3OomFault(db); 003703 goto fk_end; 003704 } 003705 if( pNextTo ){ 003706 assert( pNextTo->pPrevTo==0 ); 003707 pFKey->pNextTo = pNextTo; 003708 pNextTo->pPrevTo = pFKey; 003709 } 003710 003711 /* Link the foreign key to the table as the last step. 003712 */ 003713 assert( IsOrdinaryTable(p) ); 003714 p->u.tab.pFKey = pFKey; 003715 pFKey = 0; 003716 003717 fk_end: 003718 sqlite3DbFree(db, pFKey); 003719 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ 003720 sqlite3ExprListDelete(db, pFromCol); 003721 sqlite3ExprListDelete(db, pToCol); 003722 } 003723 003724 /* 003725 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED 003726 ** clause is seen as part of a foreign key definition. The isDeferred 003727 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. 003728 ** The behavior of the most recently created foreign key is adjusted 003729 ** accordingly. 003730 */ 003731 void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ 003732 #ifndef SQLITE_OMIT_FOREIGN_KEY 003733 Table *pTab; 003734 FKey *pFKey; 003735 if( (pTab = pParse->pNewTable)==0 ) return; 003736 if( NEVER(!IsOrdinaryTable(pTab)) ) return; 003737 if( (pFKey = pTab->u.tab.pFKey)==0 ) return; 003738 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ 003739 pFKey->isDeferred = (u8)isDeferred; 003740 #endif 003741 } 003742 003743 /* 003744 ** Generate code that will erase and refill index *pIdx. This is 003745 ** used to initialize a newly created index or to recompute the 003746 ** content of an index in response to a REINDEX command. 003747 ** 003748 ** if memRootPage is not negative, it means that the index is newly 003749 ** created. The register specified by memRootPage contains the 003750 ** root page number of the index. If memRootPage is negative, then 003751 ** the index already exists and must be cleared before being refilled and 003752 ** the root page number of the index is taken from pIndex->tnum. 003753 */ 003754 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ 003755 Table *pTab = pIndex->pTable; /* The table that is indexed */ 003756 int iTab = pParse->nTab++; /* Btree cursor used for pTab */ 003757 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ 003758 int iSorter; /* Cursor opened by OpenSorter (if in use) */ 003759 int addr1; /* Address of top of loop */ 003760 int addr2; /* Address to jump to for next iteration */ 003761 Pgno tnum; /* Root page of index */ 003762 int iPartIdxLabel; /* Jump to this label to skip a row */ 003763 Vdbe *v; /* Generate code into this virtual machine */ 003764 KeyInfo *pKey; /* KeyInfo for index */ 003765 int regRecord; /* Register holding assembled index record */ 003766 sqlite3 *db = pParse->db; /* The database connection */ 003767 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); 003768 003769 #ifndef SQLITE_OMIT_AUTHORIZATION 003770 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, 003771 db->aDb[iDb].zDbSName ) ){ 003772 return; 003773 } 003774 #endif 003775 003776 /* Require a write-lock on the table to perform this operation */ 003777 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); 003778 003779 v = sqlite3GetVdbe(pParse); 003780 if( v==0 ) return; 003781 if( memRootPage>=0 ){ 003782 tnum = (Pgno)memRootPage; 003783 }else{ 003784 tnum = pIndex->tnum; 003785 } 003786 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); 003787 assert( pKey!=0 || pParse->nErr ); 003788 003789 /* Open the sorter cursor if we are to use one. */ 003790 iSorter = pParse->nTab++; 003791 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) 003792 sqlite3KeyInfoRef(pKey), P4_KEYINFO); 003793 003794 /* Open the table. Loop through all rows of the table, inserting index 003795 ** records into the sorter. */ 003796 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 003797 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); 003798 regRecord = sqlite3GetTempReg(pParse); 003799 sqlite3MultiWrite(pParse); 003800 003801 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); 003802 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); 003803 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); 003804 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); 003805 sqlite3VdbeJumpHere(v, addr1); 003806 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); 003807 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb, 003808 (char *)pKey, P4_KEYINFO); 003809 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); 003810 003811 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); 003812 if( IsUniqueIndex(pIndex) ){ 003813 int j2 = sqlite3VdbeGoto(v, 1); 003814 addr2 = sqlite3VdbeCurrentAddr(v); 003815 sqlite3VdbeVerifyAbortable(v, OE_Abort); 003816 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, 003817 pIndex->nKeyCol); VdbeCoverage(v); 003818 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); 003819 sqlite3VdbeJumpHere(v, j2); 003820 }else{ 003821 /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not 003822 ** abort. The exception is if one of the indexed expressions contains a 003823 ** user function that throws an exception when it is evaluated. But the 003824 ** overhead of adding a statement journal to a CREATE INDEX statement is 003825 ** very small (since most of the pages written do not contain content that 003826 ** needs to be restored if the statement aborts), so we call 003827 ** sqlite3MayAbort() for all CREATE INDEX statements. */ 003828 sqlite3MayAbort(pParse); 003829 addr2 = sqlite3VdbeCurrentAddr(v); 003830 } 003831 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); 003832 if( !pIndex->bAscKeyBug ){ 003833 /* This OP_SeekEnd opcode makes index insert for a REINDEX go much 003834 ** faster by avoiding unnecessary seeks. But the optimization does 003835 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables 003836 ** with DESC primary keys, since those indexes have there keys in 003837 ** a different order from the main table. 003838 ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf 003839 */ 003840 sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx); 003841 } 003842 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); 003843 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); 003844 sqlite3ReleaseTempReg(pParse, regRecord); 003845 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); 003846 sqlite3VdbeJumpHere(v, addr1); 003847 003848 sqlite3VdbeAddOp1(v, OP_Close, iTab); 003849 sqlite3VdbeAddOp1(v, OP_Close, iIdx); 003850 sqlite3VdbeAddOp1(v, OP_Close, iSorter); 003851 } 003852 003853 /* 003854 ** Allocate heap space to hold an Index object with nCol columns. 003855 ** 003856 ** Increase the allocation size to provide an extra nExtra bytes 003857 ** of 8-byte aligned space after the Index object and return a 003858 ** pointer to this extra space in *ppExtra. 003859 */ 003860 Index *sqlite3AllocateIndexObject( 003861 sqlite3 *db, /* Database connection */ 003862 i16 nCol, /* Total number of columns in the index */ 003863 int nExtra, /* Number of bytes of extra space to alloc */ 003864 char **ppExtra /* Pointer to the "extra" space */ 003865 ){ 003866 Index *p; /* Allocated index object */ 003867 int nByte; /* Bytes of space for Index object + arrays */ 003868 003869 nByte = ROUND8(sizeof(Index)) + /* Index structure */ 003870 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ 003871 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ 003872 sizeof(i16)*nCol + /* Index.aiColumn */ 003873 sizeof(u8)*nCol); /* Index.aSortOrder */ 003874 p = sqlite3DbMallocZero(db, nByte + nExtra); 003875 if( p ){ 003876 char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); 003877 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); 003878 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); 003879 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; 003880 p->aSortOrder = (u8*)pExtra; 003881 p->nColumn = nCol; 003882 p->nKeyCol = nCol - 1; 003883 *ppExtra = ((char*)p) + nByte; 003884 } 003885 return p; 003886 } 003887 003888 /* 003889 ** If expression list pList contains an expression that was parsed with 003890 ** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in 003891 ** pParse and return non-zero. Otherwise, return zero. 003892 */ 003893 int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){ 003894 if( pList ){ 003895 int i; 003896 for(i=0; i<pList->nExpr; i++){ 003897 if( pList->a[i].fg.bNulls ){ 003898 u8 sf = pList->a[i].fg.sortFlags; 003899 sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s", 003900 (sf==0 || sf==3) ? "FIRST" : "LAST" 003901 ); 003902 return 1; 003903 } 003904 } 003905 } 003906 return 0; 003907 } 003908 003909 /* 003910 ** Create a new index for an SQL table. pName1.pName2 is the name of the index 003911 ** and pTblList is the name of the table that is to be indexed. Both will 003912 ** be NULL for a primary key or an index that is created to satisfy a 003913 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable 003914 ** as the table to be indexed. pParse->pNewTable is a table that is 003915 ** currently being constructed by a CREATE TABLE statement. 003916 ** 003917 ** pList is a list of columns to be indexed. pList will be NULL if this 003918 ** is a primary key or unique-constraint on the most recent column added 003919 ** to the table currently under construction. 003920 */ 003921 void sqlite3CreateIndex( 003922 Parse *pParse, /* All information about this parse */ 003923 Token *pName1, /* First part of index name. May be NULL */ 003924 Token *pName2, /* Second part of index name. May be NULL */ 003925 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ 003926 ExprList *pList, /* A list of columns to be indexed */ 003927 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ 003928 Token *pStart, /* The CREATE token that begins this statement */ 003929 Expr *pPIWhere, /* WHERE clause for partial indices */ 003930 int sortOrder, /* Sort order of primary key when pList==NULL */ 003931 int ifNotExist, /* Omit error if index already exists */ 003932 u8 idxType /* The index type */ 003933 ){ 003934 Table *pTab = 0; /* Table to be indexed */ 003935 Index *pIndex = 0; /* The index to be created */ 003936 char *zName = 0; /* Name of the index */ 003937 int nName; /* Number of characters in zName */ 003938 int i, j; 003939 DbFixer sFix; /* For assigning database names to pTable */ 003940 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ 003941 sqlite3 *db = pParse->db; 003942 Db *pDb; /* The specific table containing the indexed database */ 003943 int iDb; /* Index of the database that is being written */ 003944 Token *pName = 0; /* Unqualified name of the index to create */ 003945 struct ExprList_item *pListItem; /* For looping over pList */ 003946 int nExtra = 0; /* Space allocated for zExtra[] */ 003947 int nExtraCol; /* Number of extra columns needed */ 003948 char *zExtra = 0; /* Extra space after the Index object */ 003949 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ 003950 003951 assert( db->pParse==pParse ); 003952 if( pParse->nErr ){ 003953 goto exit_create_index; 003954 } 003955 assert( db->mallocFailed==0 ); 003956 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ 003957 goto exit_create_index; 003958 } 003959 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 003960 goto exit_create_index; 003961 } 003962 if( sqlite3HasExplicitNulls(pParse, pList) ){ 003963 goto exit_create_index; 003964 } 003965 003966 /* 003967 ** Find the table that is to be indexed. Return early if not found. 003968 */ 003969 if( pTblName!=0 ){ 003970 003971 /* Use the two-part index name to determine the database 003972 ** to search for the table. 'Fix' the table name to this db 003973 ** before looking up the table. 003974 */ 003975 assert( pName1 && pName2 ); 003976 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); 003977 if( iDb<0 ) goto exit_create_index; 003978 assert( pName && pName->z ); 003979 003980 #ifndef SQLITE_OMIT_TEMPDB 003981 /* If the index name was unqualified, check if the table 003982 ** is a temp table. If so, set the database to 1. Do not do this 003983 ** if initializing a database schema. 003984 */ 003985 if( !db->init.busy ){ 003986 pTab = sqlite3SrcListLookup(pParse, pTblName); 003987 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ 003988 iDb = 1; 003989 } 003990 } 003991 #endif 003992 003993 sqlite3FixInit(&sFix, pParse, iDb, "index", pName); 003994 if( sqlite3FixSrcList(&sFix, pTblName) ){ 003995 /* Because the parser constructs pTblName from a single identifier, 003996 ** sqlite3FixSrcList can never fail. */ 003997 assert(0); 003998 } 003999 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); 004000 assert( db->mallocFailed==0 || pTab==0 ); 004001 if( pTab==0 ) goto exit_create_index; 004002 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ 004003 sqlite3ErrorMsg(pParse, 004004 "cannot create a TEMP index on non-TEMP table \"%s\"", 004005 pTab->zName); 004006 goto exit_create_index; 004007 } 004008 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab); 004009 }else{ 004010 assert( pName==0 ); 004011 assert( pStart==0 ); 004012 pTab = pParse->pNewTable; 004013 if( !pTab ) goto exit_create_index; 004014 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 004015 } 004016 pDb = &db->aDb[iDb]; 004017 004018 assert( pTab!=0 ); 004019 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 004020 && db->init.busy==0 004021 && pTblName!=0 004022 #if SQLITE_USER_AUTHENTICATION 004023 && sqlite3UserAuthTable(pTab->zName)==0 004024 #endif 004025 ){ 004026 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); 004027 goto exit_create_index; 004028 } 004029 #ifndef SQLITE_OMIT_VIEW 004030 if( IsView(pTab) ){ 004031 sqlite3ErrorMsg(pParse, "views may not be indexed"); 004032 goto exit_create_index; 004033 } 004034 #endif 004035 #ifndef SQLITE_OMIT_VIRTUALTABLE 004036 if( IsVirtual(pTab) ){ 004037 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); 004038 goto exit_create_index; 004039 } 004040 #endif 004041 004042 /* 004043 ** Find the name of the index. Make sure there is not already another 004044 ** index or table with the same name. 004045 ** 004046 ** Exception: If we are reading the names of permanent indices from the 004047 ** sqlite_schema table (because some other process changed the schema) and 004048 ** one of the index names collides with the name of a temporary table or 004049 ** index, then we will continue to process this index. 004050 ** 004051 ** If pName==0 it means that we are 004052 ** dealing with a primary key or UNIQUE constraint. We have to invent our 004053 ** own name. 004054 */ 004055 if( pName ){ 004056 zName = sqlite3NameFromToken(db, pName); 004057 if( zName==0 ) goto exit_create_index; 004058 assert( pName->z!=0 ); 004059 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){ 004060 goto exit_create_index; 004061 } 004062 if( !IN_RENAME_OBJECT ){ 004063 if( !db->init.busy ){ 004064 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){ 004065 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); 004066 goto exit_create_index; 004067 } 004068 } 004069 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ 004070 if( !ifNotExist ){ 004071 sqlite3ErrorMsg(pParse, "index %s already exists", zName); 004072 }else{ 004073 assert( !db->init.busy ); 004074 sqlite3CodeVerifySchema(pParse, iDb); 004075 sqlite3ForceNotReadOnly(pParse); 004076 } 004077 goto exit_create_index; 004078 } 004079 } 004080 }else{ 004081 int n; 004082 Index *pLoop; 004083 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} 004084 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); 004085 if( zName==0 ){ 004086 goto exit_create_index; 004087 } 004088 004089 /* Automatic index names generated from within sqlite3_declare_vtab() 004090 ** must have names that are distinct from normal automatic index names. 004091 ** The following statement converts "sqlite3_autoindex..." into 004092 ** "sqlite3_butoindex..." in order to make the names distinct. 004093 ** The "vtab_err.test" test demonstrates the need of this statement. */ 004094 if( IN_SPECIAL_PARSE ) zName[7]++; 004095 } 004096 004097 /* Check for authorization to create an index. 004098 */ 004099 #ifndef SQLITE_OMIT_AUTHORIZATION 004100 if( !IN_RENAME_OBJECT ){ 004101 const char *zDb = pDb->zDbSName; 004102 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ 004103 goto exit_create_index; 004104 } 004105 i = SQLITE_CREATE_INDEX; 004106 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; 004107 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ 004108 goto exit_create_index; 004109 } 004110 } 004111 #endif 004112 004113 /* If pList==0, it means this routine was called to make a primary 004114 ** key out of the last column added to the table under construction. 004115 ** So create a fake list to simulate this. 004116 */ 004117 if( pList==0 ){ 004118 Token prevCol; 004119 Column *pCol = &pTab->aCol[pTab->nCol-1]; 004120 pCol->colFlags |= COLFLAG_UNIQUE; 004121 sqlite3TokenInit(&prevCol, pCol->zCnName); 004122 pList = sqlite3ExprListAppend(pParse, 0, 004123 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); 004124 if( pList==0 ) goto exit_create_index; 004125 assert( pList->nExpr==1 ); 004126 sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED); 004127 }else{ 004128 sqlite3ExprListCheckLength(pParse, pList, "index"); 004129 if( pParse->nErr ) goto exit_create_index; 004130 } 004131 004132 /* Figure out how many bytes of space are required to store explicitly 004133 ** specified collation sequence names. 004134 */ 004135 for(i=0; i<pList->nExpr; i++){ 004136 Expr *pExpr = pList->a[i].pExpr; 004137 assert( pExpr!=0 ); 004138 if( pExpr->op==TK_COLLATE ){ 004139 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 004140 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); 004141 } 004142 } 004143 004144 /* 004145 ** Allocate the index structure. 004146 */ 004147 nName = sqlite3Strlen30(zName); 004148 nExtraCol = pPk ? pPk->nKeyCol : 1; 004149 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ ); 004150 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, 004151 nName + nExtra + 1, &zExtra); 004152 if( db->mallocFailed ){ 004153 goto exit_create_index; 004154 } 004155 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); 004156 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); 004157 pIndex->zName = zExtra; 004158 zExtra += nName + 1; 004159 memcpy(pIndex->zName, zName, nName+1); 004160 pIndex->pTable = pTab; 004161 pIndex->onError = (u8)onError; 004162 pIndex->uniqNotNull = onError!=OE_None; 004163 pIndex->idxType = idxType; 004164 pIndex->pSchema = db->aDb[iDb].pSchema; 004165 pIndex->nKeyCol = pList->nExpr; 004166 if( pPIWhere ){ 004167 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); 004168 pIndex->pPartIdxWhere = pPIWhere; 004169 pPIWhere = 0; 004170 } 004171 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 004172 004173 /* Check to see if we should honor DESC requests on index columns 004174 */ 004175 if( pDb->pSchema->file_format>=4 ){ 004176 sortOrderMask = -1; /* Honor DESC */ 004177 }else{ 004178 sortOrderMask = 0; /* Ignore DESC */ 004179 } 004180 004181 /* Analyze the list of expressions that form the terms of the index and 004182 ** report any errors. In the common case where the expression is exactly 004183 ** a table column, store that column in aiColumn[]. For general expressions, 004184 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. 004185 ** 004186 ** TODO: Issue a warning if two or more columns of the index are identical. 004187 ** TODO: Issue a warning if the table primary key is used as part of the 004188 ** index key. 004189 */ 004190 pListItem = pList->a; 004191 if( IN_RENAME_OBJECT ){ 004192 pIndex->aColExpr = pList; 004193 pList = 0; 004194 } 004195 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){ 004196 Expr *pCExpr; /* The i-th index expression */ 004197 int requestedSortOrder; /* ASC or DESC on the i-th expression */ 004198 const char *zColl; /* Collation sequence name */ 004199 004200 sqlite3StringToId(pListItem->pExpr); 004201 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); 004202 if( pParse->nErr ) goto exit_create_index; 004203 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); 004204 if( pCExpr->op!=TK_COLUMN ){ 004205 if( pTab==pParse->pNewTable ){ 004206 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " 004207 "UNIQUE constraints"); 004208 goto exit_create_index; 004209 } 004210 if( pIndex->aColExpr==0 ){ 004211 pIndex->aColExpr = pList; 004212 pList = 0; 004213 } 004214 j = XN_EXPR; 004215 pIndex->aiColumn[i] = XN_EXPR; 004216 pIndex->uniqNotNull = 0; 004217 pIndex->bHasExpr = 1; 004218 }else{ 004219 j = pCExpr->iColumn; 004220 assert( j<=0x7fff ); 004221 if( j<0 ){ 004222 j = pTab->iPKey; 004223 }else{ 004224 if( pTab->aCol[j].notNull==0 ){ 004225 pIndex->uniqNotNull = 0; 004226 } 004227 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){ 004228 pIndex->bHasVCol = 1; 004229 pIndex->bHasExpr = 1; 004230 } 004231 } 004232 pIndex->aiColumn[i] = (i16)j; 004233 } 004234 zColl = 0; 004235 if( pListItem->pExpr->op==TK_COLLATE ){ 004236 int nColl; 004237 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) ); 004238 zColl = pListItem->pExpr->u.zToken; 004239 nColl = sqlite3Strlen30(zColl) + 1; 004240 assert( nExtra>=nColl ); 004241 memcpy(zExtra, zColl, nColl); 004242 zColl = zExtra; 004243 zExtra += nColl; 004244 nExtra -= nColl; 004245 }else if( j>=0 ){ 004246 zColl = sqlite3ColumnColl(&pTab->aCol[j]); 004247 } 004248 if( !zColl ) zColl = sqlite3StrBINARY; 004249 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ 004250 goto exit_create_index; 004251 } 004252 pIndex->azColl[i] = zColl; 004253 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask; 004254 pIndex->aSortOrder[i] = (u8)requestedSortOrder; 004255 } 004256 004257 /* Append the table key to the end of the index. For WITHOUT ROWID 004258 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For 004259 ** normal tables (when pPk==0) this will be the rowid. 004260 */ 004261 if( pPk ){ 004262 for(j=0; j<pPk->nKeyCol; j++){ 004263 int x = pPk->aiColumn[j]; 004264 assert( x>=0 ); 004265 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){ 004266 pIndex->nColumn--; 004267 }else{ 004268 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) ); 004269 pIndex->aiColumn[i] = x; 004270 pIndex->azColl[i] = pPk->azColl[j]; 004271 pIndex->aSortOrder[i] = pPk->aSortOrder[j]; 004272 i++; 004273 } 004274 } 004275 assert( i==pIndex->nColumn ); 004276 }else{ 004277 pIndex->aiColumn[i] = XN_ROWID; 004278 pIndex->azColl[i] = sqlite3StrBINARY; 004279 } 004280 sqlite3DefaultRowEst(pIndex); 004281 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); 004282 004283 /* If this index contains every column of its table, then mark 004284 ** it as a covering index */ 004285 assert( HasRowid(pTab) 004286 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 ); 004287 recomputeColumnsNotIndexed(pIndex); 004288 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ 004289 pIndex->isCovering = 1; 004290 for(j=0; j<pTab->nCol; j++){ 004291 if( j==pTab->iPKey ) continue; 004292 if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue; 004293 pIndex->isCovering = 0; 004294 break; 004295 } 004296 } 004297 004298 if( pTab==pParse->pNewTable ){ 004299 /* This routine has been called to create an automatic index as a 004300 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or 004301 ** a PRIMARY KEY or UNIQUE clause following the column definitions. 004302 ** i.e. one of: 004303 ** 004304 ** CREATE TABLE t(x PRIMARY KEY, y); 004305 ** CREATE TABLE t(x, y, UNIQUE(x, y)); 004306 ** 004307 ** Either way, check to see if the table already has such an index. If 004308 ** so, don't bother creating this one. This only applies to 004309 ** automatically created indices. Users can do as they wish with 004310 ** explicit indices. 004311 ** 004312 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent 004313 ** (and thus suppressing the second one) even if they have different 004314 ** sort orders. 004315 ** 004316 ** If there are different collating sequences or if the columns of 004317 ** the constraint occur in different orders, then the constraints are 004318 ** considered distinct and both result in separate indices. 004319 */ 004320 Index *pIdx; 004321 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 004322 int k; 004323 assert( IsUniqueIndex(pIdx) ); 004324 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); 004325 assert( IsUniqueIndex(pIndex) ); 004326 004327 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; 004328 for(k=0; k<pIdx->nKeyCol; k++){ 004329 const char *z1; 004330 const char *z2; 004331 assert( pIdx->aiColumn[k]>=0 ); 004332 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; 004333 z1 = pIdx->azColl[k]; 004334 z2 = pIndex->azColl[k]; 004335 if( sqlite3StrICmp(z1, z2) ) break; 004336 } 004337 if( k==pIdx->nKeyCol ){ 004338 if( pIdx->onError!=pIndex->onError ){ 004339 /* This constraint creates the same index as a previous 004340 ** constraint specified somewhere in the CREATE TABLE statement. 004341 ** However the ON CONFLICT clauses are different. If both this 004342 ** constraint and the previous equivalent constraint have explicit 004343 ** ON CONFLICT clauses this is an error. Otherwise, use the 004344 ** explicitly specified behavior for the index. 004345 */ 004346 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ 004347 sqlite3ErrorMsg(pParse, 004348 "conflicting ON CONFLICT clauses specified", 0); 004349 } 004350 if( pIdx->onError==OE_Default ){ 004351 pIdx->onError = pIndex->onError; 004352 } 004353 } 004354 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; 004355 if( IN_RENAME_OBJECT ){ 004356 pIndex->pNext = pParse->pNewIndex; 004357 pParse->pNewIndex = pIndex; 004358 pIndex = 0; 004359 } 004360 goto exit_create_index; 004361 } 004362 } 004363 } 004364 004365 if( !IN_RENAME_OBJECT ){ 004366 004367 /* Link the new Index structure to its table and to the other 004368 ** in-memory database structures. 004369 */ 004370 assert( pParse->nErr==0 ); 004371 if( db->init.busy ){ 004372 Index *p; 004373 assert( !IN_SPECIAL_PARSE ); 004374 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); 004375 if( pTblName!=0 ){ 004376 pIndex->tnum = db->init.newTnum; 004377 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){ 004378 sqlite3ErrorMsg(pParse, "invalid rootpage"); 004379 pParse->rc = SQLITE_CORRUPT_BKPT; 004380 goto exit_create_index; 004381 } 004382 } 004383 p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 004384 pIndex->zName, pIndex); 004385 if( p ){ 004386 assert( p==pIndex ); /* Malloc must have failed */ 004387 sqlite3OomFault(db); 004388 goto exit_create_index; 004389 } 004390 db->mDbFlags |= DBFLAG_SchemaChange; 004391 } 004392 004393 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the 004394 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then 004395 ** emit code to allocate the index rootpage on disk and make an entry for 004396 ** the index in the sqlite_schema table and populate the index with 004397 ** content. But, do not do this if we are simply reading the sqlite_schema 004398 ** table to parse the schema, or if this index is the PRIMARY KEY index 004399 ** of a WITHOUT ROWID table. 004400 ** 004401 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY 004402 ** or UNIQUE index in a CREATE TABLE statement. Since the table 004403 ** has just been created, it contains no data and the index initialization 004404 ** step can be skipped. 004405 */ 004406 else if( HasRowid(pTab) || pTblName!=0 ){ 004407 Vdbe *v; 004408 char *zStmt; 004409 int iMem = ++pParse->nMem; 004410 004411 v = sqlite3GetVdbe(pParse); 004412 if( v==0 ) goto exit_create_index; 004413 004414 sqlite3BeginWriteOperation(pParse, 1, iDb); 004415 004416 /* Create the rootpage for the index using CreateIndex. But before 004417 ** doing so, code a Noop instruction and store its address in 004418 ** Index.tnum. This is required in case this index is actually a 004419 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 004420 ** that case the convertToWithoutRowidTable() routine will replace 004421 ** the Noop with a Goto to jump over the VDBE code generated below. */ 004422 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop); 004423 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY); 004424 004425 /* Gather the complete text of the CREATE INDEX statement into 004426 ** the zStmt variable 004427 */ 004428 assert( pName!=0 || pStart==0 ); 004429 if( pStart ){ 004430 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; 004431 if( pName->z[n-1]==';' ) n--; 004432 /* A named index with an explicit CREATE INDEX statement */ 004433 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", 004434 onError==OE_None ? "" : " UNIQUE", n, pName->z); 004435 }else{ 004436 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ 004437 /* zStmt = sqlite3MPrintf(""); */ 004438 zStmt = 0; 004439 } 004440 004441 /* Add an entry in sqlite_schema for this index 004442 */ 004443 sqlite3NestedParse(pParse, 004444 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);", 004445 db->aDb[iDb].zDbSName, 004446 pIndex->zName, 004447 pTab->zName, 004448 iMem, 004449 zStmt 004450 ); 004451 sqlite3DbFree(db, zStmt); 004452 004453 /* Fill the index with data and reparse the schema. Code an OP_Expire 004454 ** to invalidate all pre-compiled statements. 004455 */ 004456 if( pTblName ){ 004457 sqlite3RefillIndex(pParse, pIndex, iMem); 004458 sqlite3ChangeCookie(pParse, iDb); 004459 sqlite3VdbeAddParseSchemaOp(v, iDb, 004460 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0); 004461 sqlite3VdbeAddOp2(v, OP_Expire, 0, 1); 004462 } 004463 004464 sqlite3VdbeJumpHere(v, (int)pIndex->tnum); 004465 } 004466 } 004467 if( db->init.busy || pTblName==0 ){ 004468 pIndex->pNext = pTab->pIndex; 004469 pTab->pIndex = pIndex; 004470 pIndex = 0; 004471 } 004472 else if( IN_RENAME_OBJECT ){ 004473 assert( pParse->pNewIndex==0 ); 004474 pParse->pNewIndex = pIndex; 004475 pIndex = 0; 004476 } 004477 004478 /* Clean up before exiting */ 004479 exit_create_index: 004480 if( pIndex ) sqlite3FreeIndex(db, pIndex); 004481 if( pTab ){ 004482 /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list. 004483 ** The list was already ordered when this routine was entered, so at this 004484 ** point at most a single index (the newly added index) will be out of 004485 ** order. So we have to reorder at most one index. */ 004486 Index **ppFrom; 004487 Index *pThis; 004488 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){ 004489 Index *pNext; 004490 if( pThis->onError!=OE_Replace ) continue; 004491 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){ 004492 *ppFrom = pNext; 004493 pThis->pNext = pNext->pNext; 004494 pNext->pNext = pThis; 004495 ppFrom = &pNext->pNext; 004496 } 004497 break; 004498 } 004499 #ifdef SQLITE_DEBUG 004500 /* Verify that all REPLACE indexes really are now at the end 004501 ** of the index list. In other words, no other index type ever 004502 ** comes after a REPLACE index on the list. */ 004503 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){ 004504 assert( pThis->onError!=OE_Replace 004505 || pThis->pNext==0 004506 || pThis->pNext->onError==OE_Replace ); 004507 } 004508 #endif 004509 } 004510 sqlite3ExprDelete(db, pPIWhere); 004511 sqlite3ExprListDelete(db, pList); 004512 sqlite3SrcListDelete(db, pTblName); 004513 sqlite3DbFree(db, zName); 004514 } 004515 004516 /* 004517 ** Fill the Index.aiRowEst[] array with default information - information 004518 ** to be used when we have not run the ANALYZE command. 004519 ** 004520 ** aiRowEst[0] is supposed to contain the number of elements in the index. 004521 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the 004522 ** number of rows in the table that match any particular value of the 004523 ** first column of the index. aiRowEst[2] is an estimate of the number 004524 ** of rows that match any particular combination of the first 2 columns 004525 ** of the index. And so forth. It must always be the case that 004526 * 004527 ** aiRowEst[N]<=aiRowEst[N-1] 004528 ** aiRowEst[N]>=1 004529 ** 004530 ** Apart from that, we have little to go on besides intuition as to 004531 ** how aiRowEst[] should be initialized. The numbers generated here 004532 ** are based on typical values found in actual indices. 004533 */ 004534 void sqlite3DefaultRowEst(Index *pIdx){ 004535 /* 10, 9, 8, 7, 6 */ 004536 static const LogEst aVal[] = { 33, 32, 30, 28, 26 }; 004537 LogEst *a = pIdx->aiRowLogEst; 004538 LogEst x; 004539 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); 004540 int i; 004541 004542 /* Indexes with default row estimates should not have stat1 data */ 004543 assert( !pIdx->hasStat1 ); 004544 004545 /* Set the first entry (number of rows in the index) to the estimated 004546 ** number of rows in the table, or half the number of rows in the table 004547 ** for a partial index. 004548 ** 004549 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1 004550 ** table but other parts we are having to guess at, then do not let the 004551 ** estimated number of rows in the table be less than 1000 (LogEst 99). 004552 ** Failure to do this can cause the indexes for which we do not have 004553 ** stat1 data to be ignored by the query planner. 004554 */ 004555 x = pIdx->pTable->nRowLogEst; 004556 assert( 99==sqlite3LogEst(1000) ); 004557 if( x<99 ){ 004558 pIdx->pTable->nRowLogEst = x = 99; 004559 } 004560 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); } 004561 a[0] = x; 004562 004563 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is 004564 ** 6 and each subsequent value (if any) is 5. */ 004565 memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); 004566 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ 004567 a[i] = 23; assert( 23==sqlite3LogEst(5) ); 004568 } 004569 004570 assert( 0==sqlite3LogEst(1) ); 004571 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; 004572 } 004573 004574 /* 004575 ** This routine will drop an existing named index. This routine 004576 ** implements the DROP INDEX statement. 004577 */ 004578 void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ 004579 Index *pIndex; 004580 Vdbe *v; 004581 sqlite3 *db = pParse->db; 004582 int iDb; 004583 004584 if( db->mallocFailed ){ 004585 goto exit_drop_index; 004586 } 004587 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */ 004588 assert( pName->nSrc==1 ); 004589 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 004590 goto exit_drop_index; 004591 } 004592 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); 004593 if( pIndex==0 ){ 004594 if( !ifExists ){ 004595 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a); 004596 }else{ 004597 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); 004598 sqlite3ForceNotReadOnly(pParse); 004599 } 004600 pParse->checkSchema = 1; 004601 goto exit_drop_index; 004602 } 004603 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ 004604 sqlite3ErrorMsg(pParse, "index associated with UNIQUE " 004605 "or PRIMARY KEY constraint cannot be dropped", 0); 004606 goto exit_drop_index; 004607 } 004608 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); 004609 #ifndef SQLITE_OMIT_AUTHORIZATION 004610 { 004611 int code = SQLITE_DROP_INDEX; 004612 Table *pTab = pIndex->pTable; 004613 const char *zDb = db->aDb[iDb].zDbSName; 004614 const char *zTab = SCHEMA_TABLE(iDb); 004615 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ 004616 goto exit_drop_index; 004617 } 004618 if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX; 004619 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ 004620 goto exit_drop_index; 004621 } 004622 } 004623 #endif 004624 004625 /* Generate code to remove the index and from the schema table */ 004626 v = sqlite3GetVdbe(pParse); 004627 if( v ){ 004628 sqlite3BeginWriteOperation(pParse, 1, iDb); 004629 sqlite3NestedParse(pParse, 004630 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'", 004631 db->aDb[iDb].zDbSName, pIndex->zName 004632 ); 004633 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); 004634 sqlite3ChangeCookie(pParse, iDb); 004635 destroyRootPage(pParse, pIndex->tnum, iDb); 004636 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); 004637 } 004638 004639 exit_drop_index: 004640 sqlite3SrcListDelete(db, pName); 004641 } 004642 004643 /* 004644 ** pArray is a pointer to an array of objects. Each object in the 004645 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() 004646 ** to extend the array so that there is space for a new object at the end. 004647 ** 004648 ** When this function is called, *pnEntry contains the current size of 004649 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes 004650 ** in total). 004651 ** 004652 ** If the realloc() is successful (i.e. if no OOM condition occurs), the 004653 ** space allocated for the new object is zeroed, *pnEntry updated to 004654 ** reflect the new size of the array and a pointer to the new allocation 004655 ** returned. *pIdx is set to the index of the new array entry in this case. 004656 ** 004657 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains 004658 ** unchanged and a copy of pArray returned. 004659 */ 004660 void *sqlite3ArrayAllocate( 004661 sqlite3 *db, /* Connection to notify of malloc failures */ 004662 void *pArray, /* Array of objects. Might be reallocated */ 004663 int szEntry, /* Size of each object in the array */ 004664 int *pnEntry, /* Number of objects currently in use */ 004665 int *pIdx /* Write the index of a new slot here */ 004666 ){ 004667 char *z; 004668 sqlite3_int64 n = *pIdx = *pnEntry; 004669 if( (n & (n-1))==0 ){ 004670 sqlite3_int64 sz = (n==0) ? 1 : 2*n; 004671 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); 004672 if( pNew==0 ){ 004673 *pIdx = -1; 004674 return pArray; 004675 } 004676 pArray = pNew; 004677 } 004678 z = (char*)pArray; 004679 memset(&z[n * szEntry], 0, szEntry); 004680 ++*pnEntry; 004681 return pArray; 004682 } 004683 004684 /* 004685 ** Append a new element to the given IdList. Create a new IdList if 004686 ** need be. 004687 ** 004688 ** A new IdList is returned, or NULL if malloc() fails. 004689 */ 004690 IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){ 004691 sqlite3 *db = pParse->db; 004692 int i; 004693 if( pList==0 ){ 004694 pList = sqlite3DbMallocZero(db, sizeof(IdList) ); 004695 if( pList==0 ) return 0; 004696 }else{ 004697 IdList *pNew; 004698 pNew = sqlite3DbRealloc(db, pList, 004699 sizeof(IdList) + pList->nId*sizeof(pList->a)); 004700 if( pNew==0 ){ 004701 sqlite3IdListDelete(db, pList); 004702 return 0; 004703 } 004704 pList = pNew; 004705 } 004706 i = pList->nId++; 004707 pList->a[i].zName = sqlite3NameFromToken(db, pToken); 004708 if( IN_RENAME_OBJECT && pList->a[i].zName ){ 004709 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken); 004710 } 004711 return pList; 004712 } 004713 004714 /* 004715 ** Delete an IdList. 004716 */ 004717 void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ 004718 int i; 004719 assert( db!=0 ); 004720 if( pList==0 ) return; 004721 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */ 004722 for(i=0; i<pList->nId; i++){ 004723 sqlite3DbFree(db, pList->a[i].zName); 004724 } 004725 sqlite3DbNNFreeNN(db, pList); 004726 } 004727 004728 /* 004729 ** Return the index in pList of the identifier named zId. Return -1 004730 ** if not found. 004731 */ 004732 int sqlite3IdListIndex(IdList *pList, const char *zName){ 004733 int i; 004734 assert( pList!=0 ); 004735 for(i=0; i<pList->nId; i++){ 004736 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; 004737 } 004738 return -1; 004739 } 004740 004741 /* 004742 ** Maximum size of a SrcList object. 004743 ** The SrcList object is used to represent the FROM clause of a 004744 ** SELECT statement, and the query planner cannot deal with more 004745 ** than 64 tables in a join. So any value larger than 64 here 004746 ** is sufficient for most uses. Smaller values, like say 10, are 004747 ** appropriate for small and memory-limited applications. 004748 */ 004749 #ifndef SQLITE_MAX_SRCLIST 004750 # define SQLITE_MAX_SRCLIST 200 004751 #endif 004752 004753 /* 004754 ** Expand the space allocated for the given SrcList object by 004755 ** creating nExtra new slots beginning at iStart. iStart is zero based. 004756 ** New slots are zeroed. 004757 ** 004758 ** For example, suppose a SrcList initially contains two entries: A,B. 004759 ** To append 3 new entries onto the end, do this: 004760 ** 004761 ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); 004762 ** 004763 ** After the call above it would contain: A, B, nil, nil, nil. 004764 ** If the iStart argument had been 1 instead of 2, then the result 004765 ** would have been: A, nil, nil, nil, B. To prepend the new slots, 004766 ** the iStart value would be 0. The result then would 004767 ** be: nil, nil, nil, A, B. 004768 ** 004769 ** If a memory allocation fails or the SrcList becomes too large, leave 004770 ** the original SrcList unchanged, return NULL, and leave an error message 004771 ** in pParse. 004772 */ 004773 SrcList *sqlite3SrcListEnlarge( 004774 Parse *pParse, /* Parsing context into which errors are reported */ 004775 SrcList *pSrc, /* The SrcList to be enlarged */ 004776 int nExtra, /* Number of new slots to add to pSrc->a[] */ 004777 int iStart /* Index in pSrc->a[] of first new slot */ 004778 ){ 004779 int i; 004780 004781 /* Sanity checking on calling parameters */ 004782 assert( iStart>=0 ); 004783 assert( nExtra>=1 ); 004784 assert( pSrc!=0 ); 004785 assert( iStart<=pSrc->nSrc ); 004786 004787 /* Allocate additional space if needed */ 004788 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ 004789 SrcList *pNew; 004790 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra; 004791 sqlite3 *db = pParse->db; 004792 004793 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){ 004794 sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d", 004795 SQLITE_MAX_SRCLIST); 004796 return 0; 004797 } 004798 if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST; 004799 pNew = sqlite3DbRealloc(db, pSrc, 004800 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); 004801 if( pNew==0 ){ 004802 assert( db->mallocFailed ); 004803 return 0; 004804 } 004805 pSrc = pNew; 004806 pSrc->nAlloc = nAlloc; 004807 } 004808 004809 /* Move existing slots that come after the newly inserted slots 004810 ** out of the way */ 004811 for(i=pSrc->nSrc-1; i>=iStart; i--){ 004812 pSrc->a[i+nExtra] = pSrc->a[i]; 004813 } 004814 pSrc->nSrc += nExtra; 004815 004816 /* Zero the newly allocated slots */ 004817 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); 004818 for(i=iStart; i<iStart+nExtra; i++){ 004819 pSrc->a[i].iCursor = -1; 004820 } 004821 004822 /* Return a pointer to the enlarged SrcList */ 004823 return pSrc; 004824 } 004825 004826 004827 /* 004828 ** Append a new table name to the given SrcList. Create a new SrcList if 004829 ** need be. A new entry is created in the SrcList even if pTable is NULL. 004830 ** 004831 ** A SrcList is returned, or NULL if there is an OOM error or if the 004832 ** SrcList grows to large. The returned 004833 ** SrcList might be the same as the SrcList that was input or it might be 004834 ** a new one. If an OOM error does occurs, then the prior value of pList 004835 ** that is input to this routine is automatically freed. 004836 ** 004837 ** If pDatabase is not null, it means that the table has an optional 004838 ** database name prefix. Like this: "database.table". The pDatabase 004839 ** points to the table name and the pTable points to the database name. 004840 ** The SrcList.a[].zName field is filled with the table name which might 004841 ** come from pTable (if pDatabase is NULL) or from pDatabase. 004842 ** SrcList.a[].zDatabase is filled with the database name from pTable, 004843 ** or with NULL if no database is specified. 004844 ** 004845 ** In other words, if call like this: 004846 ** 004847 ** sqlite3SrcListAppend(D,A,B,0); 004848 ** 004849 ** Then B is a table name and the database name is unspecified. If called 004850 ** like this: 004851 ** 004852 ** sqlite3SrcListAppend(D,A,B,C); 004853 ** 004854 ** Then C is the table name and B is the database name. If C is defined 004855 ** then so is B. In other words, we never have a case where: 004856 ** 004857 ** sqlite3SrcListAppend(D,A,0,C); 004858 ** 004859 ** Both pTable and pDatabase are assumed to be quoted. They are dequoted 004860 ** before being added to the SrcList. 004861 */ 004862 SrcList *sqlite3SrcListAppend( 004863 Parse *pParse, /* Parsing context, in which errors are reported */ 004864 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ 004865 Token *pTable, /* Table to append */ 004866 Token *pDatabase /* Database of the table */ 004867 ){ 004868 SrcItem *pItem; 004869 sqlite3 *db; 004870 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ 004871 assert( pParse!=0 ); 004872 assert( pParse->db!=0 ); 004873 db = pParse->db; 004874 if( pList==0 ){ 004875 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) ); 004876 if( pList==0 ) return 0; 004877 pList->nAlloc = 1; 004878 pList->nSrc = 1; 004879 memset(&pList->a[0], 0, sizeof(pList->a[0])); 004880 pList->a[0].iCursor = -1; 004881 }else{ 004882 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc); 004883 if( pNew==0 ){ 004884 sqlite3SrcListDelete(db, pList); 004885 return 0; 004886 }else{ 004887 pList = pNew; 004888 } 004889 } 004890 pItem = &pList->a[pList->nSrc-1]; 004891 if( pDatabase && pDatabase->z==0 ){ 004892 pDatabase = 0; 004893 } 004894 if( pDatabase ){ 004895 pItem->zName = sqlite3NameFromToken(db, pDatabase); 004896 pItem->zDatabase = sqlite3NameFromToken(db, pTable); 004897 }else{ 004898 pItem->zName = sqlite3NameFromToken(db, pTable); 004899 pItem->zDatabase = 0; 004900 } 004901 return pList; 004902 } 004903 004904 /* 004905 ** Assign VdbeCursor index numbers to all tables in a SrcList 004906 */ 004907 void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ 004908 int i; 004909 SrcItem *pItem; 004910 assert( pList || pParse->db->mallocFailed ); 004911 if( ALWAYS(pList) ){ 004912 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ 004913 if( pItem->iCursor>=0 ) continue; 004914 pItem->iCursor = pParse->nTab++; 004915 if( pItem->pSelect ){ 004916 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); 004917 } 004918 } 004919 } 004920 } 004921 004922 /* 004923 ** Delete an entire SrcList including all its substructure. 004924 */ 004925 void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ 004926 int i; 004927 SrcItem *pItem; 004928 assert( db!=0 ); 004929 if( pList==0 ) return; 004930 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ 004931 if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase); 004932 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName); 004933 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias); 004934 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); 004935 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); 004936 sqlite3DeleteTable(db, pItem->pTab); 004937 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect); 004938 if( pItem->fg.isUsing ){ 004939 sqlite3IdListDelete(db, pItem->u3.pUsing); 004940 }else if( pItem->u3.pOn ){ 004941 sqlite3ExprDelete(db, pItem->u3.pOn); 004942 } 004943 } 004944 sqlite3DbNNFreeNN(db, pList); 004945 } 004946 004947 /* 004948 ** This routine is called by the parser to add a new term to the 004949 ** end of a growing FROM clause. The "p" parameter is the part of 004950 ** the FROM clause that has already been constructed. "p" is NULL 004951 ** if this is the first term of the FROM clause. pTable and pDatabase 004952 ** are the name of the table and database named in the FROM clause term. 004953 ** pDatabase is NULL if the database name qualifier is missing - the 004954 ** usual case. If the term has an alias, then pAlias points to the 004955 ** alias token. If the term is a subquery, then pSubquery is the 004956 ** SELECT statement that the subquery encodes. The pTable and 004957 ** pDatabase parameters are NULL for subqueries. The pOn and pUsing 004958 ** parameters are the content of the ON and USING clauses. 004959 ** 004960 ** Return a new SrcList which encodes is the FROM with the new 004961 ** term added. 004962 */ 004963 SrcList *sqlite3SrcListAppendFromTerm( 004964 Parse *pParse, /* Parsing context */ 004965 SrcList *p, /* The left part of the FROM clause already seen */ 004966 Token *pTable, /* Name of the table to add to the FROM clause */ 004967 Token *pDatabase, /* Name of the database containing pTable */ 004968 Token *pAlias, /* The right-hand side of the AS subexpression */ 004969 Select *pSubquery, /* A subquery used in place of a table name */ 004970 OnOrUsing *pOnUsing /* Either the ON clause or the USING clause */ 004971 ){ 004972 SrcItem *pItem; 004973 sqlite3 *db = pParse->db; 004974 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){ 004975 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 004976 (pOnUsing->pOn ? "ON" : "USING") 004977 ); 004978 goto append_from_error; 004979 } 004980 p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase); 004981 if( p==0 ){ 004982 goto append_from_error; 004983 } 004984 assert( p->nSrc>0 ); 004985 pItem = &p->a[p->nSrc-1]; 004986 assert( (pTable==0)==(pDatabase==0) ); 004987 assert( pItem->zName==0 || pDatabase!=0 ); 004988 if( IN_RENAME_OBJECT && pItem->zName ){ 004989 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable; 004990 sqlite3RenameTokenMap(pParse, pItem->zName, pToken); 004991 } 004992 assert( pAlias!=0 ); 004993 if( pAlias->n ){ 004994 pItem->zAlias = sqlite3NameFromToken(db, pAlias); 004995 } 004996 if( pSubquery ){ 004997 pItem->pSelect = pSubquery; 004998 if( pSubquery->selFlags & SF_NestedFrom ){ 004999 pItem->fg.isNestedFrom = 1; 005000 } 005001 } 005002 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 ); 005003 assert( pItem->fg.isUsing==0 ); 005004 if( pOnUsing==0 ){ 005005 pItem->u3.pOn = 0; 005006 }else if( pOnUsing->pUsing ){ 005007 pItem->fg.isUsing = 1; 005008 pItem->u3.pUsing = pOnUsing->pUsing; 005009 }else{ 005010 pItem->u3.pOn = pOnUsing->pOn; 005011 } 005012 return p; 005013 005014 append_from_error: 005015 assert( p==0 ); 005016 sqlite3ClearOnOrUsing(db, pOnUsing); 005017 sqlite3SelectDelete(db, pSubquery); 005018 return 0; 005019 } 005020 005021 /* 005022 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 005023 ** element of the source-list passed as the second argument. 005024 */ 005025 void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ 005026 assert( pIndexedBy!=0 ); 005027 if( p && pIndexedBy->n>0 ){ 005028 SrcItem *pItem; 005029 assert( p->nSrc>0 ); 005030 pItem = &p->a[p->nSrc-1]; 005031 assert( pItem->fg.notIndexed==0 ); 005032 assert( pItem->fg.isIndexedBy==0 ); 005033 assert( pItem->fg.isTabFunc==0 ); 005034 if( pIndexedBy->n==1 && !pIndexedBy->z ){ 005035 /* A "NOT INDEXED" clause was supplied. See parse.y 005036 ** construct "indexed_opt" for details. */ 005037 pItem->fg.notIndexed = 1; 005038 }else{ 005039 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); 005040 pItem->fg.isIndexedBy = 1; 005041 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */ 005042 } 005043 } 005044 } 005045 005046 /* 005047 ** Append the contents of SrcList p2 to SrcList p1 and return the resulting 005048 ** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2 005049 ** are deleted by this function. 005050 */ 005051 SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){ 005052 assert( p1 && p1->nSrc==1 ); 005053 if( p2 ){ 005054 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1); 005055 if( pNew==0 ){ 005056 sqlite3SrcListDelete(pParse->db, p2); 005057 }else{ 005058 p1 = pNew; 005059 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem)); 005060 sqlite3DbFree(pParse->db, p2); 005061 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype); 005062 } 005063 } 005064 return p1; 005065 } 005066 005067 /* 005068 ** Add the list of function arguments to the SrcList entry for a 005069 ** table-valued-function. 005070 */ 005071 void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ 005072 if( p ){ 005073 SrcItem *pItem = &p->a[p->nSrc-1]; 005074 assert( pItem->fg.notIndexed==0 ); 005075 assert( pItem->fg.isIndexedBy==0 ); 005076 assert( pItem->fg.isTabFunc==0 ); 005077 pItem->u1.pFuncArg = pList; 005078 pItem->fg.isTabFunc = 1; 005079 }else{ 005080 sqlite3ExprListDelete(pParse->db, pList); 005081 } 005082 } 005083 005084 /* 005085 ** When building up a FROM clause in the parser, the join operator 005086 ** is initially attached to the left operand. But the code generator 005087 ** expects the join operator to be on the right operand. This routine 005088 ** Shifts all join operators from left to right for an entire FROM 005089 ** clause. 005090 ** 005091 ** Example: Suppose the join is like this: 005092 ** 005093 ** A natural cross join B 005094 ** 005095 ** The operator is "natural cross join". The A and B operands are stored 005096 ** in p->a[0] and p->a[1], respectively. The parser initially stores the 005097 ** operator with A. This routine shifts that operator over to B. 005098 ** 005099 ** Additional changes: 005100 ** 005101 ** * All tables to the left of the right-most RIGHT JOIN are tagged with 005102 ** JT_LTORJ (mnemonic: Left Table Of Right Join) so that the 005103 ** code generator can easily tell that the table is part of 005104 ** the left operand of at least one RIGHT JOIN. 005105 */ 005106 void sqlite3SrcListShiftJoinType(Parse *pParse, SrcList *p){ 005107 (void)pParse; 005108 if( p && p->nSrc>1 ){ 005109 int i = p->nSrc-1; 005110 u8 allFlags = 0; 005111 do{ 005112 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype; 005113 }while( (--i)>0 ); 005114 p->a[0].fg.jointype = 0; 005115 005116 /* All terms to the left of a RIGHT JOIN should be tagged with the 005117 ** JT_LTORJ flags */ 005118 if( allFlags & JT_RIGHT ){ 005119 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){} 005120 i--; 005121 assert( i>=0 ); 005122 do{ 005123 p->a[i].fg.jointype |= JT_LTORJ; 005124 }while( (--i)>=0 ); 005125 } 005126 } 005127 } 005128 005129 /* 005130 ** Generate VDBE code for a BEGIN statement. 005131 */ 005132 void sqlite3BeginTransaction(Parse *pParse, int type){ 005133 sqlite3 *db; 005134 Vdbe *v; 005135 int i; 005136 005137 assert( pParse!=0 ); 005138 db = pParse->db; 005139 assert( db!=0 ); 005140 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ 005141 return; 005142 } 005143 v = sqlite3GetVdbe(pParse); 005144 if( !v ) return; 005145 if( type!=TK_DEFERRED ){ 005146 for(i=0; i<db->nDb; i++){ 005147 int eTxnType; 005148 Btree *pBt = db->aDb[i].pBt; 005149 if( pBt && sqlite3BtreeIsReadonly(pBt) ){ 005150 eTxnType = 0; /* Read txn */ 005151 }else if( type==TK_EXCLUSIVE ){ 005152 eTxnType = 2; /* Exclusive txn */ 005153 }else{ 005154 eTxnType = 1; /* Write txn */ 005155 } 005156 sqlite3VdbeAddOp2(v, OP_Transaction, i, eTxnType); 005157 sqlite3VdbeUsesBtree(v, i); 005158 } 005159 } 005160 sqlite3VdbeAddOp0(v, OP_AutoCommit); 005161 } 005162 005163 /* 005164 ** Generate VDBE code for a COMMIT or ROLLBACK statement. 005165 ** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise 005166 ** code is generated for a COMMIT. 005167 */ 005168 void sqlite3EndTransaction(Parse *pParse, int eType){ 005169 Vdbe *v; 005170 int isRollback; 005171 005172 assert( pParse!=0 ); 005173 assert( pParse->db!=0 ); 005174 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK ); 005175 isRollback = eType==TK_ROLLBACK; 005176 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, 005177 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){ 005178 return; 005179 } 005180 v = sqlite3GetVdbe(pParse); 005181 if( v ){ 005182 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback); 005183 } 005184 } 005185 005186 /* 005187 ** This function is called by the parser when it parses a command to create, 005188 ** release or rollback an SQL savepoint. 005189 */ 005190 void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ 005191 char *zName = sqlite3NameFromToken(pParse->db, pName); 005192 if( zName ){ 005193 Vdbe *v = sqlite3GetVdbe(pParse); 005194 #ifndef SQLITE_OMIT_AUTHORIZATION 005195 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; 005196 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); 005197 #endif 005198 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ 005199 sqlite3DbFree(pParse->db, zName); 005200 return; 005201 } 005202 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); 005203 } 005204 } 005205 005206 /* 005207 ** Make sure the TEMP database is open and available for use. Return 005208 ** the number of errors. Leave any error messages in the pParse structure. 005209 */ 005210 int sqlite3OpenTempDatabase(Parse *pParse){ 005211 sqlite3 *db = pParse->db; 005212 if( db->aDb[1].pBt==0 && !pParse->explain ){ 005213 int rc; 005214 Btree *pBt; 005215 static const int flags = 005216 SQLITE_OPEN_READWRITE | 005217 SQLITE_OPEN_CREATE | 005218 SQLITE_OPEN_EXCLUSIVE | 005219 SQLITE_OPEN_DELETEONCLOSE | 005220 SQLITE_OPEN_TEMP_DB; 005221 005222 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); 005223 if( rc!=SQLITE_OK ){ 005224 sqlite3ErrorMsg(pParse, "unable to open a temporary database " 005225 "file for storing temporary tables"); 005226 pParse->rc = rc; 005227 return 1; 005228 } 005229 db->aDb[1].pBt = pBt; 005230 assert( db->aDb[1].pSchema ); 005231 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){ 005232 sqlite3OomFault(db); 005233 return 1; 005234 } 005235 } 005236 return 0; 005237 } 005238 005239 /* 005240 ** Record the fact that the schema cookie will need to be verified 005241 ** for database iDb. The code to actually verify the schema cookie 005242 ** will occur at the end of the top-level VDBE and will be generated 005243 ** later, by sqlite3FinishCoding(). 005244 */ 005245 static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){ 005246 assert( iDb>=0 && iDb<pToplevel->db->nDb ); 005247 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 ); 005248 assert( iDb<SQLITE_MAX_DB ); 005249 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) ); 005250 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ 005251 DbMaskSet(pToplevel->cookieMask, iDb); 005252 if( !OMIT_TEMPDB && iDb==1 ){ 005253 sqlite3OpenTempDatabase(pToplevel); 005254 } 005255 } 005256 } 005257 void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ 005258 sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb); 005259 } 005260 005261 005262 /* 005263 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 005264 ** attached database. Otherwise, invoke it for the database named zDb only. 005265 */ 005266 void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ 005267 sqlite3 *db = pParse->db; 005268 int i; 005269 for(i=0; i<db->nDb; i++){ 005270 Db *pDb = &db->aDb[i]; 005271 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ 005272 sqlite3CodeVerifySchema(pParse, i); 005273 } 005274 } 005275 } 005276 005277 /* 005278 ** Generate VDBE code that prepares for doing an operation that 005279 ** might change the database. 005280 ** 005281 ** This routine starts a new transaction if we are not already within 005282 ** a transaction. If we are already within a transaction, then a checkpoint 005283 ** is set if the setStatement parameter is true. A checkpoint should 005284 ** be set for operations that might fail (due to a constraint) part of 005285 ** the way through and which will need to undo some writes without having to 005286 ** rollback the whole transaction. For operations where all constraints 005287 ** can be checked before any changes are made to the database, it is never 005288 ** necessary to undo a write and the checkpoint should not be set. 005289 */ 005290 void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ 005291 Parse *pToplevel = sqlite3ParseToplevel(pParse); 005292 sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb); 005293 DbMaskSet(pToplevel->writeMask, iDb); 005294 pToplevel->isMultiWrite |= setStatement; 005295 } 005296 005297 /* 005298 ** Indicate that the statement currently under construction might write 005299 ** more than one entry (example: deleting one row then inserting another, 005300 ** inserting multiple rows in a table, or inserting a row and index entries.) 005301 ** If an abort occurs after some of these writes have completed, then it will 005302 ** be necessary to undo the completed writes. 005303 */ 005304 void sqlite3MultiWrite(Parse *pParse){ 005305 Parse *pToplevel = sqlite3ParseToplevel(pParse); 005306 pToplevel->isMultiWrite = 1; 005307 } 005308 005309 /* 005310 ** The code generator calls this routine if is discovers that it is 005311 ** possible to abort a statement prior to completion. In order to 005312 ** perform this abort without corrupting the database, we need to make 005313 ** sure that the statement is protected by a statement transaction. 005314 ** 005315 ** Technically, we only need to set the mayAbort flag if the 005316 ** isMultiWrite flag was previously set. There is a time dependency 005317 ** such that the abort must occur after the multiwrite. This makes 005318 ** some statements involving the REPLACE conflict resolution algorithm 005319 ** go a little faster. But taking advantage of this time dependency 005320 ** makes it more difficult to prove that the code is correct (in 005321 ** particular, it prevents us from writing an effective 005322 ** implementation of sqlite3AssertMayAbort()) and so we have chosen 005323 ** to take the safe route and skip the optimization. 005324 */ 005325 void sqlite3MayAbort(Parse *pParse){ 005326 Parse *pToplevel = sqlite3ParseToplevel(pParse); 005327 pToplevel->mayAbort = 1; 005328 } 005329 005330 /* 005331 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT 005332 ** error. The onError parameter determines which (if any) of the statement 005333 ** and/or current transaction is rolled back. 005334 */ 005335 void sqlite3HaltConstraint( 005336 Parse *pParse, /* Parsing context */ 005337 int errCode, /* extended error code */ 005338 int onError, /* Constraint type */ 005339 char *p4, /* Error message */ 005340 i8 p4type, /* P4_STATIC or P4_TRANSIENT */ 005341 u8 p5Errmsg /* P5_ErrMsg type */ 005342 ){ 005343 Vdbe *v; 005344 assert( pParse->pVdbe!=0 ); 005345 v = sqlite3GetVdbe(pParse); 005346 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested ); 005347 if( onError==OE_Abort ){ 005348 sqlite3MayAbort(pParse); 005349 } 005350 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); 005351 sqlite3VdbeChangeP5(v, p5Errmsg); 005352 } 005353 005354 /* 005355 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. 005356 */ 005357 void sqlite3UniqueConstraint( 005358 Parse *pParse, /* Parsing context */ 005359 int onError, /* Constraint type */ 005360 Index *pIdx /* The index that triggers the constraint */ 005361 ){ 005362 char *zErr; 005363 int j; 005364 StrAccum errMsg; 005365 Table *pTab = pIdx->pTable; 005366 005367 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 005368 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]); 005369 if( pIdx->aColExpr ){ 005370 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName); 005371 }else{ 005372 for(j=0; j<pIdx->nKeyCol; j++){ 005373 char *zCol; 005374 assert( pIdx->aiColumn[j]>=0 ); 005375 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName; 005376 if( j ) sqlite3_str_append(&errMsg, ", ", 2); 005377 sqlite3_str_appendall(&errMsg, pTab->zName); 005378 sqlite3_str_append(&errMsg, ".", 1); 005379 sqlite3_str_appendall(&errMsg, zCol); 005380 } 005381 } 005382 zErr = sqlite3StrAccumFinish(&errMsg); 005383 sqlite3HaltConstraint(pParse, 005384 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY 005385 : SQLITE_CONSTRAINT_UNIQUE, 005386 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); 005387 } 005388 005389 005390 /* 005391 ** Code an OP_Halt due to non-unique rowid. 005392 */ 005393 void sqlite3RowidConstraint( 005394 Parse *pParse, /* Parsing context */ 005395 int onError, /* Conflict resolution algorithm */ 005396 Table *pTab /* The table with the non-unique rowid */ 005397 ){ 005398 char *zMsg; 005399 int rc; 005400 if( pTab->iPKey>=0 ){ 005401 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, 005402 pTab->aCol[pTab->iPKey].zCnName); 005403 rc = SQLITE_CONSTRAINT_PRIMARYKEY; 005404 }else{ 005405 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); 005406 rc = SQLITE_CONSTRAINT_ROWID; 005407 } 005408 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC, 005409 P5_ConstraintUnique); 005410 } 005411 005412 /* 005413 ** Check to see if pIndex uses the collating sequence pColl. Return 005414 ** true if it does and false if it does not. 005415 */ 005416 #ifndef SQLITE_OMIT_REINDEX 005417 static int collationMatch(const char *zColl, Index *pIndex){ 005418 int i; 005419 assert( zColl!=0 ); 005420 for(i=0; i<pIndex->nColumn; i++){ 005421 const char *z = pIndex->azColl[i]; 005422 assert( z!=0 || pIndex->aiColumn[i]<0 ); 005423 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){ 005424 return 1; 005425 } 005426 } 005427 return 0; 005428 } 005429 #endif 005430 005431 /* 005432 ** Recompute all indices of pTab that use the collating sequence pColl. 005433 ** If pColl==0 then recompute all indices of pTab. 005434 */ 005435 #ifndef SQLITE_OMIT_REINDEX 005436 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ 005437 if( !IsVirtual(pTab) ){ 005438 Index *pIndex; /* An index associated with pTab */ 005439 005440 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ 005441 if( zColl==0 || collationMatch(zColl, pIndex) ){ 005442 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 005443 sqlite3BeginWriteOperation(pParse, 0, iDb); 005444 sqlite3RefillIndex(pParse, pIndex, -1); 005445 } 005446 } 005447 } 005448 } 005449 #endif 005450 005451 /* 005452 ** Recompute all indices of all tables in all databases where the 005453 ** indices use the collating sequence pColl. If pColl==0 then recompute 005454 ** all indices everywhere. 005455 */ 005456 #ifndef SQLITE_OMIT_REINDEX 005457 static void reindexDatabases(Parse *pParse, char const *zColl){ 005458 Db *pDb; /* A single database */ 005459 int iDb; /* The database index number */ 005460 sqlite3 *db = pParse->db; /* The database connection */ 005461 HashElem *k; /* For looping over tables in pDb */ 005462 Table *pTab; /* A table in the database */ 005463 005464 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */ 005465 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ 005466 assert( pDb!=0 ); 005467 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ 005468 pTab = (Table*)sqliteHashData(k); 005469 reindexTable(pParse, pTab, zColl); 005470 } 005471 } 005472 } 005473 #endif 005474 005475 /* 005476 ** Generate code for the REINDEX command. 005477 ** 005478 ** REINDEX -- 1 005479 ** REINDEX <collation> -- 2 005480 ** REINDEX ?<database>.?<tablename> -- 3 005481 ** REINDEX ?<database>.?<indexname> -- 4 005482 ** 005483 ** Form 1 causes all indices in all attached databases to be rebuilt. 005484 ** Form 2 rebuilds all indices in all databases that use the named 005485 ** collating function. Forms 3 and 4 rebuild the named index or all 005486 ** indices associated with the named table. 005487 */ 005488 #ifndef SQLITE_OMIT_REINDEX 005489 void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ 005490 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ 005491 char *z; /* Name of a table or index */ 005492 const char *zDb; /* Name of the database */ 005493 Table *pTab; /* A table in the database */ 005494 Index *pIndex; /* An index associated with pTab */ 005495 int iDb; /* The database index number */ 005496 sqlite3 *db = pParse->db; /* The database connection */ 005497 Token *pObjName; /* Name of the table or index to be reindexed */ 005498 005499 /* Read the database schema. If an error occurs, leave an error message 005500 ** and code in pParse and return NULL. */ 005501 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 005502 return; 005503 } 005504 005505 if( pName1==0 ){ 005506 reindexDatabases(pParse, 0); 005507 return; 005508 }else if( NEVER(pName2==0) || pName2->z==0 ){ 005509 char *zColl; 005510 assert( pName1->z ); 005511 zColl = sqlite3NameFromToken(pParse->db, pName1); 005512 if( !zColl ) return; 005513 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); 005514 if( pColl ){ 005515 reindexDatabases(pParse, zColl); 005516 sqlite3DbFree(db, zColl); 005517 return; 005518 } 005519 sqlite3DbFree(db, zColl); 005520 } 005521 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); 005522 if( iDb<0 ) return; 005523 z = sqlite3NameFromToken(db, pObjName); 005524 if( z==0 ) return; 005525 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0; 005526 pTab = sqlite3FindTable(db, z, zDb); 005527 if( pTab ){ 005528 reindexTable(pParse, pTab, 0); 005529 sqlite3DbFree(db, z); 005530 return; 005531 } 005532 pIndex = sqlite3FindIndex(db, z, zDb); 005533 sqlite3DbFree(db, z); 005534 if( pIndex ){ 005535 iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema); 005536 sqlite3BeginWriteOperation(pParse, 0, iDb); 005537 sqlite3RefillIndex(pParse, pIndex, -1); 005538 return; 005539 } 005540 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); 005541 } 005542 #endif 005543 005544 /* 005545 ** Return a KeyInfo structure that is appropriate for the given Index. 005546 ** 005547 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object 005548 ** when it has finished using it. 005549 */ 005550 KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ 005551 int i; 005552 int nCol = pIdx->nColumn; 005553 int nKey = pIdx->nKeyCol; 005554 KeyInfo *pKey; 005555 if( pParse->nErr ) return 0; 005556 if( pIdx->uniqNotNull ){ 005557 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); 005558 }else{ 005559 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); 005560 } 005561 if( pKey ){ 005562 assert( sqlite3KeyInfoIsWriteable(pKey) ); 005563 for(i=0; i<nCol; i++){ 005564 const char *zColl = pIdx->azColl[i]; 005565 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : 005566 sqlite3LocateCollSeq(pParse, zColl); 005567 pKey->aSortFlags[i] = pIdx->aSortOrder[i]; 005568 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) ); 005569 } 005570 if( pParse->nErr ){ 005571 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ ); 005572 if( pIdx->bNoQuery==0 ){ 005573 /* Deactivate the index because it contains an unknown collating 005574 ** sequence. The only way to reactive the index is to reload the 005575 ** schema. Adding the missing collating sequence later does not 005576 ** reactive the index. The application had the chance to register 005577 ** the missing index using the collation-needed callback. For 005578 ** simplicity, SQLite will not give the application a second chance. 005579 */ 005580 pIdx->bNoQuery = 1; 005581 pParse->rc = SQLITE_ERROR_RETRY; 005582 } 005583 sqlite3KeyInfoUnref(pKey); 005584 pKey = 0; 005585 } 005586 } 005587 return pKey; 005588 } 005589 005590 #ifndef SQLITE_OMIT_CTE 005591 /* 005592 ** Create a new CTE object 005593 */ 005594 Cte *sqlite3CteNew( 005595 Parse *pParse, /* Parsing context */ 005596 Token *pName, /* Name of the common-table */ 005597 ExprList *pArglist, /* Optional column name list for the table */ 005598 Select *pQuery, /* Query used to initialize the table */ 005599 u8 eM10d /* The MATERIALIZED flag */ 005600 ){ 005601 Cte *pNew; 005602 sqlite3 *db = pParse->db; 005603 005604 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)); 005605 assert( pNew!=0 || db->mallocFailed ); 005606 005607 if( db->mallocFailed ){ 005608 sqlite3ExprListDelete(db, pArglist); 005609 sqlite3SelectDelete(db, pQuery); 005610 }else{ 005611 pNew->pSelect = pQuery; 005612 pNew->pCols = pArglist; 005613 pNew->zName = sqlite3NameFromToken(pParse->db, pName); 005614 pNew->eM10d = eM10d; 005615 } 005616 return pNew; 005617 } 005618 005619 /* 005620 ** Clear information from a Cte object, but do not deallocate storage 005621 ** for the object itself. 005622 */ 005623 static void cteClear(sqlite3 *db, Cte *pCte){ 005624 assert( pCte!=0 ); 005625 sqlite3ExprListDelete(db, pCte->pCols); 005626 sqlite3SelectDelete(db, pCte->pSelect); 005627 sqlite3DbFree(db, pCte->zName); 005628 } 005629 005630 /* 005631 ** Free the contents of the CTE object passed as the second argument. 005632 */ 005633 void sqlite3CteDelete(sqlite3 *db, Cte *pCte){ 005634 assert( pCte!=0 ); 005635 cteClear(db, pCte); 005636 sqlite3DbFree(db, pCte); 005637 } 005638 005639 /* 005640 ** This routine is invoked once per CTE by the parser while parsing a 005641 ** WITH clause. The CTE described by the third argument is added to 005642 ** the WITH clause of the second argument. If the second argument is 005643 ** NULL, then a new WITH argument is created. 005644 */ 005645 With *sqlite3WithAdd( 005646 Parse *pParse, /* Parsing context */ 005647 With *pWith, /* Existing WITH clause, or NULL */ 005648 Cte *pCte /* CTE to add to the WITH clause */ 005649 ){ 005650 sqlite3 *db = pParse->db; 005651 With *pNew; 005652 char *zName; 005653 005654 if( pCte==0 ){ 005655 return pWith; 005656 } 005657 005658 /* Check that the CTE name is unique within this WITH clause. If 005659 ** not, store an error in the Parse structure. */ 005660 zName = pCte->zName; 005661 if( zName && pWith ){ 005662 int i; 005663 for(i=0; i<pWith->nCte; i++){ 005664 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){ 005665 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName); 005666 } 005667 } 005668 } 005669 005670 if( pWith ){ 005671 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); 005672 pNew = sqlite3DbRealloc(db, pWith, nByte); 005673 }else{ 005674 pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); 005675 } 005676 assert( (pNew!=0 && zName!=0) || db->mallocFailed ); 005677 005678 if( db->mallocFailed ){ 005679 sqlite3CteDelete(db, pCte); 005680 pNew = pWith; 005681 }else{ 005682 pNew->a[pNew->nCte++] = *pCte; 005683 sqlite3DbFree(db, pCte); 005684 } 005685 005686 return pNew; 005687 } 005688 005689 /* 005690 ** Free the contents of the With object passed as the second argument. 005691 */ 005692 void sqlite3WithDelete(sqlite3 *db, With *pWith){ 005693 if( pWith ){ 005694 int i; 005695 for(i=0; i<pWith->nCte; i++){ 005696 cteClear(db, &pWith->a[i]); 005697 } 005698 sqlite3DbFree(db, pWith); 005699 } 005700 } 005701 void sqlite3WithDeleteGeneric(sqlite3 *db, void *pWith){ 005702 sqlite3WithDelete(db, (With*)pWith); 005703 } 005704 #endif /* !defined(SQLITE_OMIT_CTE) */