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