ds@1952: /* ds@1952: ** 2003 April 6 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains code used to implement the PRAGMA command. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: ds@1952: /* Ignore this whole file if pragmas are disabled ds@1952: */ ds@1952: #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER) ds@1952: ds@1952: /* ds@1952: ** Interpret the given string as a safety level. Return 0 for OFF, ds@1952: ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or ds@1952: ** unrecognized string argument. ds@1952: ** ds@1952: ** Note that the values returned are one less that the values that ds@1952: ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done ds@1952: ** to support legacy SQL code. The safety level used to be boolean ds@1952: ** and older scripts may have used numbers 0 for OFF and 1 for ON. ds@1952: */ ds@1952: static int getSafetyLevel(const char *z){ ds@1952: /* 123456789 123456789 */ ds@1952: static const char zText[] = "onoffalseyestruefull"; ds@1952: static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16}; ds@1952: static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4}; ds@1952: static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2}; ds@1952: int i, n; ds@1952: if( isdigit(*z) ){ ds@1952: return atoi(z); ds@1952: } ds@1952: n = strlen(z); ds@1952: for(i=0; i=0&&i<=2)?i:0); ds@1952: } ds@1952: #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_PAGER_PRAGMAS ds@1952: /* ds@1952: ** Interpret the given string as a temp db location. Return 1 for file ds@1952: ** backed temporary databases, 2 for the Red-Black tree in memory database ds@1952: ** and 0 to use the compile-time default. ds@1952: */ ds@1952: static int getTempStore(const char *z){ ds@1952: if( z[0]>='0' && z[0]<='2' ){ ds@1952: return z[0] - '0'; ds@1952: }else if( sqlite3StrICmp(z, "file")==0 ){ ds@1952: return 1; ds@1952: }else if( sqlite3StrICmp(z, "memory")==0 ){ ds@1952: return 2; ds@1952: }else{ ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: #endif /* SQLITE_PAGER_PRAGMAS */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_PAGER_PRAGMAS ds@1952: /* ds@1952: ** Invalidate temp storage, either when the temp storage is changed ds@1952: ** from default, or when 'file' and the temp_store_directory has changed ds@1952: */ ds@1952: static int invalidateTempStorage(Parse *pParse){ ds@1952: sqlite3 *db = pParse->db; ds@1952: if( db->aDb[1].pBt!=0 ){ ds@1952: if( !db->autoCommit ){ ds@1952: sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " ds@1952: "from within a transaction"); ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: sqlite3BtreeClose(db->aDb[1].pBt); ds@1952: db->aDb[1].pBt = 0; ds@1952: sqlite3ResetInternalSchema(db, 0); ds@1952: } ds@1952: return SQLITE_OK; ds@1952: } ds@1952: #endif /* SQLITE_PAGER_PRAGMAS */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_PAGER_PRAGMAS ds@1952: /* ds@1952: ** If the TEMP database is open, close it and mark the database schema ds@1952: ** as needing reloading. This must be done when using the TEMP_STORE ds@1952: ** or DEFAULT_TEMP_STORE pragmas. ds@1952: */ ds@1952: static int changeTempStorage(Parse *pParse, const char *zStorageType){ ds@1952: int ts = getTempStore(zStorageType); ds@1952: sqlite3 *db = pParse->db; ds@1952: if( db->temp_store==ts ) return SQLITE_OK; ds@1952: if( invalidateTempStorage( pParse ) != SQLITE_OK ){ ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: db->temp_store = ts; ds@1952: return SQLITE_OK; ds@1952: } ds@1952: #endif /* SQLITE_PAGER_PRAGMAS */ ds@1952: ds@1952: /* ds@1952: ** Generate code to return a single integer value. ds@1952: */ ds@1952: static void returnSingleInt(Parse *pParse, const char *zLabel, int value){ ds@1952: Vdbe *v = sqlite3GetVdbe(pParse); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, value, 0); ds@1952: if( pParse->explain==0 ){ ds@1952: sqlite3VdbeSetNumCols(v, 1); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P3_STATIC); ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 1, 0); ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_FLAG_PRAGMAS ds@1952: /* ds@1952: ** Check to see if zRight and zLeft refer to a pragma that queries ds@1952: ** or changes one of the flags in db->flags. Return 1 if so and 0 if not. ds@1952: ** Also, implement the pragma. ds@1952: */ ds@1952: static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){ ds@1952: static const struct sPragmaType { ds@1952: const char *zName; /* Name of the pragma */ ds@1952: int mask; /* Mask for the db->flags value */ ds@1952: } aPragma[] = { ds@1952: { "full_column_names", SQLITE_FullColNames }, ds@1952: { "short_column_names", SQLITE_ShortColNames }, ds@1952: { "count_changes", SQLITE_CountRows }, ds@1952: { "empty_result_callbacks", SQLITE_NullCallback }, ds@1952: { "legacy_file_format", SQLITE_LegacyFileFmt }, ds@1952: { "fullfsync", SQLITE_FullFSync }, ds@1952: #ifdef SQLITE_DEBUG ds@1952: { "sql_trace", SQLITE_SqlTrace }, ds@1952: { "vdbe_listing", SQLITE_VdbeListing }, ds@1952: { "vdbe_trace", SQLITE_VdbeTrace }, ds@1952: #endif ds@1952: #ifndef SQLITE_OMIT_CHECK ds@1952: { "ignore_check_constraints", SQLITE_IgnoreChecks }, ds@1952: #endif ds@1952: /* The following is VERY experimental */ ds@1952: { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode }, ds@1952: { "omit_readlock", SQLITE_NoReadlock }, ds@1952: ds@1952: /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted ds@1952: ** flag if there are any active statements. */ ds@1952: { "read_uncommitted", SQLITE_ReadUncommitted }, ds@1952: }; ds@1952: int i; ds@1952: const struct sPragmaType *p; ds@1952: for(i=0, p=aPragma; izName)==0 ){ ds@1952: sqlite3 *db = pParse->db; ds@1952: Vdbe *v; ds@1952: v = sqlite3GetVdbe(pParse); ds@1952: if( v ){ ds@1952: if( zRight==0 ){ ds@1952: returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 ); ds@1952: }else{ ds@1952: if( getBoolean(zRight) ){ ds@1952: db->flags |= p->mask; ds@1952: }else{ ds@1952: db->flags &= ~p->mask; ds@1952: } ds@1952: } ds@1952: } ds@1952: return 1; ds@1952: } ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ ds@1952: ds@1952: /* ds@1952: ** Process a pragma statement. ds@1952: ** ds@1952: ** Pragmas are of this form: ds@1952: ** ds@1952: ** PRAGMA [database.]id [= value] ds@1952: ** ds@1952: ** The identifier might also be a string. The value is a string, and ds@1952: ** identifier, or a number. If minusFlag is true, then the value is ds@1952: ** a number that was preceded by a minus sign. ds@1952: ** ds@1952: ** If the left side is "database.id" then pId1 is the database name ds@1952: ** and pId2 is the id. If the left side is just "id" then pId1 is the ds@1952: ** id and pId2 is any empty string. ds@1952: */ ds@1952: void sqlite3Pragma( ds@1952: Parse *pParse, ds@1952: Token *pId1, /* First part of [database.]id field */ ds@1952: Token *pId2, /* Second part of [database.]id field, or NULL */ ds@1952: Token *pValue, /* Token for , or NULL */ ds@1952: int minusFlag /* True if a '-' sign preceded */ ds@1952: ){ ds@1952: char *zLeft = 0; /* Nul-terminated UTF-8 string */ ds@1952: char *zRight = 0; /* Nul-terminated UTF-8 string , or NULL */ ds@1952: const char *zDb = 0; /* The database name */ ds@1952: Token *pId; /* Pointer to token */ ds@1952: int iDb; /* Database index for */ ds@1952: sqlite3 *db = pParse->db; ds@1952: Db *pDb; ds@1952: Vdbe *v = sqlite3GetVdbe(pParse); ds@1952: if( v==0 ) return; ds@1952: ds@1952: /* Interpret the [database.] part of the pragma statement. iDb is the ds@1952: ** index of the database this pragma is being applied to in db.aDb[]. */ ds@1952: iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId); ds@1952: if( iDb<0 ) return; ds@1952: pDb = &db->aDb[iDb]; ds@1952: ds@1952: /* If the temp database has been explicitly named as part of the ds@1952: ** pragma, make sure it is open. ds@1952: */ ds@1952: if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){ ds@1952: return; ds@1952: } ds@1952: ds@1952: zLeft = sqlite3NameFromToken(db, pId); ds@1952: if( !zLeft ) return; ds@1952: if( minusFlag ){ ds@1952: zRight = sqlite3MPrintf(db, "-%T", pValue); ds@1952: }else{ ds@1952: zRight = sqlite3NameFromToken(db, pValue); ds@1952: } ds@1952: ds@1952: zDb = ((iDb>0)?pDb->zName:0); ds@1952: if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ ds@1952: goto pragma_out; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_PAGER_PRAGMAS ds@1952: /* ds@1952: ** PRAGMA [database.]default_cache_size ds@1952: ** PRAGMA [database.]default_cache_size=N ds@1952: ** ds@1952: ** The first form reports the current persistent setting for the ds@1952: ** page cache size. The value returned is the maximum number of ds@1952: ** pages in the page cache. The second form sets both the current ds@1952: ** page cache size value and the persistent page cache size value ds@1952: ** stored in the database file. ds@1952: ** ds@1952: ** The default cache size is stored in meta-value 2 of page 1 of the ds@1952: ** database file. The cache size is actually the absolute value of ds@1952: ** this memory location. The sign of meta-value 2 determines the ds@1952: ** synchronous setting. A negative value means synchronous is off ds@1952: ** and a positive value means synchronous is on. ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){ ds@1952: static const VdbeOpList getCacheSize[] = { ds@1952: { OP_ReadCookie, 0, 2, 0}, /* 0 */ ds@1952: { OP_AbsValue, 0, 0, 0}, ds@1952: { OP_Dup, 0, 0, 0}, ds@1952: { OP_Integer, 0, 0, 0}, ds@1952: { OP_Ne, 0, 6, 0}, ds@1952: { OP_Integer, 0, 0, 0}, /* 5 */ ds@1952: { OP_Callback, 1, 0, 0}, ds@1952: }; ds@1952: int addr; ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: sqlite3VdbeUsesBtree(v, iDb); ds@1952: if( !zRight ){ ds@1952: sqlite3VdbeSetNumCols(v, 1); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P3_STATIC); ds@1952: addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); ds@1952: sqlite3VdbeChangeP1(v, addr, iDb); ds@1952: sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE); ds@1952: }else{ ds@1952: int size = atoi(zRight); ds@1952: if( size<0 ) size = -size; ds@1952: sqlite3BeginWriteOperation(pParse, 0, iDb); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, size, 0); ds@1952: sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2); ds@1952: addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3); ds@1952: sqlite3VdbeAddOp(v, OP_Negative, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2); ds@1952: pDb->pSchema->cache_size = size; ds@1952: sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); ds@1952: } ds@1952: }else ds@1952: ds@1952: /* ds@1952: ** PRAGMA [database.]page_size ds@1952: ** PRAGMA [database.]page_size=N ds@1952: ** ds@1952: ** The first form reports the current setting for the ds@1952: ** database page size in bytes. The second form sets the ds@1952: ** database page size value. The value can only be set if ds@1952: ** the database has not yet been created. ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft,"page_size")==0 ){ ds@1952: Btree *pBt = pDb->pBt; ds@1952: if( !zRight ){ ds@1952: int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0; ds@1952: returnSingleInt(pParse, "page_size", size); ds@1952: }else{ ds@1952: /* Malloc may fail when setting the page-size, as there is an internal ds@1952: ** buffer that the pager module resizes using sqlite3_realloc(). ds@1952: */ ds@1952: if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1) ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: } ds@1952: }else ds@1952: ds@1952: /* ds@1952: ** PRAGMA [database.]max_page_count ds@1952: ** PRAGMA [database.]max_page_count=N ds@1952: ** ds@1952: ** The first form reports the current setting for the ds@1952: ** maximum number of pages in the database file. The ds@1952: ** second form attempts to change this setting. Both ds@1952: ** forms return the current setting. ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){ ds@1952: Btree *pBt = pDb->pBt; ds@1952: int newMax = 0; ds@1952: if( zRight ){ ds@1952: newMax = atoi(zRight); ds@1952: } ds@1952: if( pBt ){ ds@1952: newMax = sqlite3BtreeMaxPageCount(pBt, newMax); ds@1952: } ds@1952: returnSingleInt(pParse, "max_page_count", newMax); ds@1952: }else ds@1952: ds@1952: /* ds@1952: ** PRAGMA [database.]locking_mode ds@1952: ** PRAGMA [database.]locking_mode = (normal|exclusive) ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){ ds@1952: const char *zRet = "normal"; ds@1952: int eMode = getLockingMode(zRight); ds@1952: ds@1952: if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){ ds@1952: /* Simple "PRAGMA locking_mode;" statement. This is a query for ds@1952: ** the current default locking mode (which may be different to ds@1952: ** the locking-mode of the main database). ds@1952: */ ds@1952: eMode = db->dfltLockMode; ds@1952: }else{ ds@1952: Pager *pPager; ds@1952: if( pId2->n==0 ){ ds@1952: /* This indicates that no database name was specified as part ds@1952: ** of the PRAGMA command. In this case the locking-mode must be ds@1952: ** set on all attached databases, as well as the main db file. ds@1952: ** ds@1952: ** Also, the sqlite3.dfltLockMode variable is set so that ds@1952: ** any subsequently attached databases also use the specified ds@1952: ** locking mode. ds@1952: */ ds@1952: int ii; ds@1952: assert(pDb==&db->aDb[0]); ds@1952: for(ii=2; iinDb; ii++){ ds@1952: pPager = sqlite3BtreePager(db->aDb[ii].pBt); ds@1952: sqlite3PagerLockingMode(pPager, eMode); ds@1952: } ds@1952: db->dfltLockMode = eMode; ds@1952: } ds@1952: pPager = sqlite3BtreePager(pDb->pBt); ds@1952: eMode = sqlite3PagerLockingMode(pPager, eMode); ds@1952: } ds@1952: ds@1952: assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE); ds@1952: if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ ds@1952: zRet = "exclusive"; ds@1952: } ds@1952: sqlite3VdbeSetNumCols(v, 1); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P3_STATIC); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, zRet, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 1, 0); ds@1952: }else ds@1952: #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ ds@1952: ds@1952: /* ds@1952: ** PRAGMA [database.]auto_vacuum ds@1952: ** PRAGMA [database.]auto_vacuum=N ds@1952: ** ds@1952: ** Get or set the (boolean) value of the database 'auto-vacuum' parameter. ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_AUTOVACUUM ds@1952: if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){ ds@1952: Btree *pBt = pDb->pBt; ds@1952: if( sqlite3ReadSchema(pParse) ){ ds@1952: goto pragma_out; ds@1952: } ds@1952: if( !zRight ){ ds@1952: int auto_vacuum = ds@1952: pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM; ds@1952: returnSingleInt(pParse, "auto_vacuum", auto_vacuum); ds@1952: }else{ ds@1952: int eAuto = getAutoVacuum(zRight); ds@1952: if( eAuto>=0 ){ ds@1952: /* Call SetAutoVacuum() to set initialize the internal auto and ds@1952: ** incr-vacuum flags. This is required in case this connection ds@1952: ** creates the database file. It is important that it is created ds@1952: ** as an auto-vacuum capable db. ds@1952: */ ds@1952: int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); ds@1952: if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ ds@1952: /* When setting the auto_vacuum mode to either "full" or ds@1952: ** "incremental", write the value of meta[6] in the database ds@1952: ** file. Before writing to meta[6], check that meta[3] indicates ds@1952: ** that this really is an auto-vacuum capable database. ds@1952: */ ds@1952: static const VdbeOpList setMeta6[] = { ds@1952: { OP_Transaction, 0, 1, 0}, /* 0 */ ds@1952: { OP_ReadCookie, 0, 3, 0}, /* 1 */ ds@1952: { OP_If, 0, 0, 0}, /* 2 */ ds@1952: { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ ds@1952: { OP_Integer, 0, 0, 0}, /* 4 */ ds@1952: { OP_SetCookie, 0, 6, 0}, /* 5 */ ds@1952: }; ds@1952: int iAddr; ds@1952: iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6); ds@1952: sqlite3VdbeChangeP1(v, iAddr, iDb); ds@1952: sqlite3VdbeChangeP1(v, iAddr+1, iDb); ds@1952: sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4); ds@1952: sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1); ds@1952: sqlite3VdbeChangeP1(v, iAddr+5, iDb); ds@1952: sqlite3VdbeUsesBtree(v, iDb); ds@1952: } ds@1952: } ds@1952: } ds@1952: }else ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** PRAGMA [database.]incremental_vacuum(N) ds@1952: ** ds@1952: ** Do N steps of incremental vacuuming on a database. ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_AUTOVACUUM ds@1952: if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){ ds@1952: int iLimit, addr; ds@1952: if( sqlite3ReadSchema(pParse) ){ ds@1952: goto pragma_out; ds@1952: } ds@1952: if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ ds@1952: iLimit = 0x7fffffff; ds@1952: } ds@1952: sqlite3BeginWriteOperation(pParse, 0, iDb); ds@1952: sqlite3VdbeAddOp(v, OP_MemInt, iLimit, 0); ds@1952: addr = sqlite3VdbeAddOp(v, OP_IncrVacuum, iDb, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_MemIncr, -1, 0); ds@1952: sqlite3VdbeAddOp(v, OP_IfMemPos, 0, addr); ds@1952: sqlite3VdbeJumpHere(v, addr); ds@1952: }else ds@1952: #endif ds@1952: ds@1952: #ifndef SQLITE_OMIT_PAGER_PRAGMAS ds@1952: /* ds@1952: ** PRAGMA [database.]cache_size ds@1952: ** PRAGMA [database.]cache_size=N ds@1952: ** ds@1952: ** The first form reports the current local setting for the ds@1952: ** page cache size. The local setting can be different from ds@1952: ** the persistent cache size value that is stored in the database ds@1952: ** file itself. The value returned is the maximum number of ds@1952: ** pages in the page cache. The second form sets the local ds@1952: ** page cache size value. It does not change the persistent ds@1952: ** cache size stored on the disk so the cache size will revert ds@1952: ** to its default value when the database is closed and reopened. ds@1952: ** N should be a positive integer. ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft,"cache_size")==0 ){ ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: if( !zRight ){ ds@1952: returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size); ds@1952: }else{ ds@1952: int size = atoi(zRight); ds@1952: if( size<0 ) size = -size; ds@1952: pDb->pSchema->cache_size = size; ds@1952: sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); ds@1952: } ds@1952: }else ds@1952: ds@1952: /* ds@1952: ** PRAGMA temp_store ds@1952: ** PRAGMA temp_store = "default"|"memory"|"file" ds@1952: ** ds@1952: ** Return or set the local value of the temp_store flag. Changing ds@1952: ** the local value does not make changes to the disk file and the default ds@1952: ** value will be restored the next time the database is opened. ds@1952: ** ds@1952: ** Note that it is possible for the library compile-time options to ds@1952: ** override this setting ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft, "temp_store")==0 ){ ds@1952: if( !zRight ){ ds@1952: returnSingleInt(pParse, "temp_store", db->temp_store); ds@1952: }else{ ds@1952: changeTempStorage(pParse, zRight); ds@1952: } ds@1952: }else ds@1952: ds@1952: /* ds@1952: ** PRAGMA temp_store_directory ds@1952: ** PRAGMA temp_store_directory = ""|"directory_name" ds@1952: ** ds@1952: ** Return or set the local value of the temp_store_directory flag. Changing ds@1952: ** the value sets a specific directory to be used for temporary files. ds@1952: ** Setting to a null string reverts to the default temporary directory search. ds@1952: ** If temporary directory is changed, then invalidateTempStorage. ds@1952: ** ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){ ds@1952: if( !zRight ){ ds@1952: if( sqlite3_temp_directory ){ ds@1952: sqlite3VdbeSetNumCols(v, 1); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, ds@1952: "temp_store_directory", P3_STATIC); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 1, 0); ds@1952: } ds@1952: }else{ ds@1952: if( zRight[0] ds@1952: && !sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE) ds@1952: ){ ds@1952: sqlite3ErrorMsg(pParse, "not a writable directory"); ds@1952: goto pragma_out; ds@1952: } ds@1952: if( TEMP_STORE==0 ds@1952: || (TEMP_STORE==1 && db->temp_store<=1) ds@1952: || (TEMP_STORE==2 && db->temp_store==1) ds@1952: ){ ds@1952: invalidateTempStorage(pParse); ds@1952: } ds@1952: sqlite3_free(sqlite3_temp_directory); ds@1952: if( zRight[0] ){ ds@1952: sqlite3_temp_directory = zRight; ds@1952: zRight = 0; ds@1952: }else{ ds@1952: sqlite3_temp_directory = 0; ds@1952: } ds@1952: } ds@1952: }else ds@1952: ds@1952: /* ds@1952: ** PRAGMA [database.]synchronous ds@1952: ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL ds@1952: ** ds@1952: ** Return or set the local value of the synchronous flag. Changing ds@1952: ** the local value does not make changes to the disk file and the ds@1952: ** default value will be restored the next time the database is ds@1952: ** opened. ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft,"synchronous")==0 ){ ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: if( !zRight ){ ds@1952: returnSingleInt(pParse, "synchronous", pDb->safety_level-1); ds@1952: }else{ ds@1952: if( !db->autoCommit ){ ds@1952: sqlite3ErrorMsg(pParse, ds@1952: "Safety level may not be changed inside a transaction"); ds@1952: }else{ ds@1952: pDb->safety_level = getSafetyLevel(zRight)+1; ds@1952: } ds@1952: } ds@1952: }else ds@1952: #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_FLAG_PRAGMAS ds@1952: if( flagPragma(pParse, zLeft, zRight) ){ ds@1952: /* The flagPragma() subroutine also generates any necessary code ds@1952: ** there is nothing more to do here */ ds@1952: }else ds@1952: #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS ds@1952: /* ds@1952: ** PRAGMA table_info() ds@1952: ** ds@1952: ** Return a single row for each column of the named table. The columns of ds@1952: ** the returned data set are: ds@1952: ** ds@1952: ** cid: Column id (numbered from left to right, starting at 0) ds@1952: ** name: Column name ds@1952: ** type: Column declaration type. ds@1952: ** notnull: True if 'NOT NULL' is part of column declaration ds@1952: ** dflt_value: The default value for the column, if any. ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){ ds@1952: Table *pTab; ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: pTab = sqlite3FindTable(db, zRight, zDb); ds@1952: if( pTab ){ ds@1952: int i; ds@1952: int nHidden = 0; ds@1952: Column *pCol; ds@1952: sqlite3VdbeSetNumCols(v, 6); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P3_STATIC); ds@1952: sqlite3ViewGetColumnNames(pParse, pTab); ds@1952: for(i=0, pCol=pTab->aCol; inCol; i++, pCol++){ ds@1952: const Token *pDflt; ds@1952: if( IsHiddenColumn(pCol) ){ ds@1952: nHidden++; ds@1952: continue; ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_Integer, i-nHidden, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, pCol->zName, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, ds@1952: pCol->zType ? pCol->zType : "", 0); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, pCol->notNull, 0); ds@1952: if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){ ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, (char*)pDflt->z, pDflt->n); ds@1952: }else{ ds@1952: sqlite3VdbeAddOp(v, OP_Null, 0, 0); ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_Integer, pCol->isPrimKey, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 6, 0); ds@1952: } ds@1952: } ds@1952: }else ds@1952: ds@1952: if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){ ds@1952: Index *pIdx; ds@1952: Table *pTab; ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: pIdx = sqlite3FindIndex(db, zRight, zDb); ds@1952: if( pIdx ){ ds@1952: int i; ds@1952: pTab = pIdx->pTable; ds@1952: sqlite3VdbeSetNumCols(v, 3); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P3_STATIC); ds@1952: for(i=0; inColumn; i++){ ds@1952: int cnum = pIdx->aiColumn[i]; ds@1952: sqlite3VdbeAddOp(v, OP_Integer, i, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, cnum, 0); ds@1952: assert( pTab->nCol>cnum ); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 3, 0); ds@1952: } ds@1952: } ds@1952: }else ds@1952: ds@1952: if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){ ds@1952: Index *pIdx; ds@1952: Table *pTab; ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: pTab = sqlite3FindTable(db, zRight, zDb); ds@1952: if( pTab ){ ds@1952: v = sqlite3GetVdbe(pParse); ds@1952: pIdx = pTab->pIndex; ds@1952: if( pIdx ){ ds@1952: int i = 0; ds@1952: sqlite3VdbeSetNumCols(v, 3); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P3_STATIC); ds@1952: while(pIdx){ ds@1952: sqlite3VdbeAddOp(v, OP_Integer, i, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 3, 0); ds@1952: ++i; ds@1952: pIdx = pIdx->pNext; ds@1952: } ds@1952: } ds@1952: } ds@1952: }else ds@1952: ds@1952: if( sqlite3StrICmp(zLeft, "database_list")==0 ){ ds@1952: int i; ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: sqlite3VdbeSetNumCols(v, 3); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P3_STATIC); ds@1952: for(i=0; inDb; i++){ ds@1952: if( db->aDb[i].pBt==0 ) continue; ds@1952: assert( db->aDb[i].zName!=0 ); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, i, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, ds@1952: sqlite3BtreeGetFilename(db->aDb[i].pBt), 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 3, 0); ds@1952: } ds@1952: }else ds@1952: ds@1952: if( sqlite3StrICmp(zLeft, "collation_list")==0 ){ ds@1952: int i = 0; ds@1952: HashElem *p; ds@1952: sqlite3VdbeSetNumCols(v, 2); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC); ds@1952: for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ ds@1952: CollSeq *pColl = (CollSeq *)sqliteHashData(p); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, i++, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, pColl->zName, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 2, 0); ds@1952: } ds@1952: }else ds@1952: #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_FOREIGN_KEY ds@1952: if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){ ds@1952: FKey *pFK; ds@1952: Table *pTab; ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: pTab = sqlite3FindTable(db, zRight, zDb); ds@1952: if( pTab ){ ds@1952: v = sqlite3GetVdbe(pParse); ds@1952: pFK = pTab->pFKey; ds@1952: if( pFK ){ ds@1952: int i = 0; ds@1952: sqlite3VdbeSetNumCols(v, 5); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P3_STATIC); ds@1952: while(pFK){ ds@1952: int j; ds@1952: for(j=0; jnCol; j++){ ds@1952: char *zCol = pFK->aCol[j].zCol; ds@1952: sqlite3VdbeAddOp(v, OP_Integer, i, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, j, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, ds@1952: pTab->aCol[pFK->aCol[j].iFrom].zName, 0); ds@1952: sqlite3VdbeOp3(v, zCol ? OP_String8 : OP_Null, 0, 0, zCol, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 5, 0); ds@1952: } ds@1952: ++i; ds@1952: pFK = pFK->pNextFrom; ds@1952: } ds@1952: } ds@1952: } ds@1952: }else ds@1952: #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ ds@1952: ds@1952: #ifndef NDEBUG ds@1952: if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){ ds@1952: if( zRight ){ ds@1952: if( getBoolean(zRight) ){ ds@1952: sqlite3ParserTrace(stderr, "parser: "); ds@1952: }else{ ds@1952: sqlite3ParserTrace(0, 0); ds@1952: } ds@1952: } ds@1952: }else ds@1952: #endif ds@1952: ds@1952: /* Reinstall the LIKE and GLOB functions. The variant of LIKE ds@1952: ** used will be case sensitive or not depending on the RHS. ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){ ds@1952: if( zRight ){ ds@1952: sqlite3RegisterLikeFunctions(db, getBoolean(zRight)); ds@1952: } ds@1952: }else ds@1952: ds@1952: #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX ds@1952: # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 ds@1952: #endif ds@1952: ds@1952: #ifndef SQLITE_OMIT_INTEGRITY_CHECK ds@1952: if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){ ds@1952: int i, j, addr, mxErr; ds@1952: ds@1952: /* Code that appears at the end of the integrity check. If no error ds@1952: ** messages have been generated, output OK. Otherwise output the ds@1952: ** error message ds@1952: */ ds@1952: static const VdbeOpList endCode[] = { ds@1952: { OP_MemLoad, 0, 0, 0}, ds@1952: { OP_Integer, 0, 0, 0}, ds@1952: { OP_Ne, 0, 0, 0}, /* 2 */ ds@1952: { OP_String8, 0, 0, "ok"}, ds@1952: { OP_Callback, 1, 0, 0}, ds@1952: }; ds@1952: ds@1952: /* Initialize the VDBE program */ ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: sqlite3VdbeSetNumCols(v, 1); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P3_STATIC); ds@1952: ds@1952: /* Set the maximum error count */ ds@1952: mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; ds@1952: if( zRight ){ ds@1952: mxErr = atoi(zRight); ds@1952: if( mxErr<=0 ){ ds@1952: mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; ds@1952: } ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_MemInt, mxErr, 0); ds@1952: ds@1952: /* Do an integrity check on each database file */ ds@1952: for(i=0; inDb; i++){ ds@1952: HashElem *x; ds@1952: Hash *pTbls; ds@1952: int cnt = 0; ds@1952: ds@1952: if( OMIT_TEMPDB && i==1 ) continue; ds@1952: ds@1952: sqlite3CodeVerifySchema(pParse, i); ds@1952: addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Halt, 0, 0); ds@1952: sqlite3VdbeJumpHere(v, addr); ds@1952: ds@1952: /* Do an integrity check of the B-Tree ds@1952: */ ds@1952: pTbls = &db->aDb[i].pSchema->tblHash; ds@1952: for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ ds@1952: Table *pTab = sqliteHashData(x); ds@1952: Index *pIdx; ds@1952: sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0); ds@1952: cnt++; ds@1952: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ ds@1952: sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0); ds@1952: cnt++; ds@1952: } ds@1952: } ds@1952: if( cnt==0 ) continue; ds@1952: sqlite3VdbeAddOp(v, OP_IntegrityCk, 0, i); ds@1952: addr = sqlite3VdbeAddOp(v, OP_IsNull, -1, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, ds@1952: sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName), ds@1952: P3_DYNAMIC); ds@1952: sqlite3VdbeAddOp(v, OP_Pull, 1, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Concat, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 1, 0); ds@1952: sqlite3VdbeJumpHere(v, addr); ds@1952: ds@1952: /* Make sure all the indices are constructed correctly. ds@1952: */ ds@1952: for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ ds@1952: Table *pTab = sqliteHashData(x); ds@1952: Index *pIdx; ds@1952: int loopTop; ds@1952: ds@1952: if( pTab->pIndex==0 ) continue; ds@1952: addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Halt, 0, 0); ds@1952: sqlite3VdbeJumpHere(v, addr); ds@1952: sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead); ds@1952: sqlite3VdbeAddOp(v, OP_MemInt, 0, 1); ds@1952: loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0); ds@1952: sqlite3VdbeAddOp(v, OP_MemIncr, 1, 1); ds@1952: for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ ds@1952: int jmp2; ds@1952: static const VdbeOpList idxErr[] = { ds@1952: { OP_MemIncr, -1, 0, 0}, ds@1952: { OP_String8, 0, 0, "rowid "}, ds@1952: { OP_Rowid, 1, 0, 0}, ds@1952: { OP_String8, 0, 0, " missing from index "}, ds@1952: { OP_String8, 0, 0, 0}, /* 4 */ ds@1952: { OP_Concat, 2, 0, 0}, ds@1952: { OP_Callback, 1, 0, 0}, ds@1952: }; ds@1952: sqlite3GenerateIndexKey(v, pIdx, 1); ds@1952: jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0); ds@1952: addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr); ds@1952: sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC); ds@1952: sqlite3VdbeJumpHere(v, jmp2); ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1); ds@1952: sqlite3VdbeJumpHere(v, loopTop); ds@1952: for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ ds@1952: static const VdbeOpList cntIdx[] = { ds@1952: { OP_MemInt, 0, 2, 0}, ds@1952: { OP_Rewind, 0, 0, 0}, /* 1 */ ds@1952: { OP_MemIncr, 1, 2, 0}, ds@1952: { OP_Next, 0, 0, 0}, /* 3 */ ds@1952: { OP_MemLoad, 1, 0, 0}, ds@1952: { OP_MemLoad, 2, 0, 0}, ds@1952: { OP_Eq, 0, 0, 0}, /* 6 */ ds@1952: { OP_MemIncr, -1, 0, 0}, ds@1952: { OP_String8, 0, 0, "wrong # of entries in index "}, ds@1952: { OP_String8, 0, 0, 0}, /* 9 */ ds@1952: { OP_Concat, 0, 0, 0}, ds@1952: { OP_Callback, 1, 0, 0}, ds@1952: }; ds@1952: if( pIdx->tnum==0 ) continue; ds@1952: addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Halt, 0, 0); ds@1952: sqlite3VdbeJumpHere(v, addr); ds@1952: addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx); ds@1952: sqlite3VdbeChangeP1(v, addr+1, j+2); ds@1952: sqlite3VdbeChangeP2(v, addr+1, addr+4); ds@1952: sqlite3VdbeChangeP1(v, addr+3, j+2); ds@1952: sqlite3VdbeChangeP2(v, addr+3, addr+2); ds@1952: sqlite3VdbeJumpHere(v, addr+6); ds@1952: sqlite3VdbeChangeP3(v, addr+9, pIdx->zName, P3_STATIC); ds@1952: } ds@1952: } ds@1952: } ds@1952: addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode); ds@1952: sqlite3VdbeChangeP1(v, addr+1, mxErr); ds@1952: sqlite3VdbeJumpHere(v, addr+2); ds@1952: }else ds@1952: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: /* ds@1952: ** PRAGMA encoding ds@1952: ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" ds@1952: ** ds@1952: ** In it's first form, this pragma returns the encoding of the main ds@1952: ** database. If the database is not initialized, it is initialized now. ds@1952: ** ds@1952: ** The second form of this pragma is a no-op if the main database file ds@1952: ** has not already been initialized. In this case it sets the default ds@1952: ** encoding that will be used for the main database file if a new file ds@1952: ** is created. If an existing main database file is opened, then the ds@1952: ** default text encoding for the existing database is used. ds@1952: ** ds@1952: ** In all cases new databases created using the ATTACH command are ds@1952: ** created to use the same default text encoding as the main database. If ds@1952: ** the main database has not been initialized and/or created when ATTACH ds@1952: ** is executed, this is done before the ATTACH operation. ds@1952: ** ds@1952: ** In the second form this pragma sets the text encoding to be used in ds@1952: ** new database files created using this database handle. It is only ds@1952: ** useful if invoked immediately after the main database i ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft, "encoding")==0 ){ ds@1952: static const struct EncName { ds@1952: char *zName; ds@1952: u8 enc; ds@1952: } encnames[] = { ds@1952: { "UTF-8", SQLITE_UTF8 }, ds@1952: { "UTF8", SQLITE_UTF8 }, ds@1952: { "UTF-16le", SQLITE_UTF16LE }, ds@1952: { "UTF16le", SQLITE_UTF16LE }, ds@1952: { "UTF-16be", SQLITE_UTF16BE }, ds@1952: { "UTF16be", SQLITE_UTF16BE }, ds@1952: { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */ ds@1952: { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */ ds@1952: { 0, 0 } ds@1952: }; ds@1952: const struct EncName *pEnc; ds@1952: if( !zRight ){ /* "PRAGMA encoding" */ ds@1952: if( sqlite3ReadSchema(pParse) ) goto pragma_out; ds@1952: sqlite3VdbeSetNumCols(v, 1); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P3_STATIC); ds@1952: sqlite3VdbeAddOp(v, OP_String8, 0, 0); ds@1952: for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ ds@1952: if( pEnc->enc==ENC(pParse->db) ){ ds@1952: sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC); ds@1952: break; ds@1952: } ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 1, 0); ds@1952: }else{ /* "PRAGMA encoding = XXX" */ ds@1952: /* Only change the value of sqlite.enc if the database handle is not ds@1952: ** initialized. If the main database exists, the new sqlite.enc value ds@1952: ** will be overwritten when the schema is next loaded. If it does not ds@1952: ** already exists, it will be created to use the new encoding value. ds@1952: */ ds@1952: if( ds@1952: !(DbHasProperty(db, 0, DB_SchemaLoaded)) || ds@1952: DbHasProperty(db, 0, DB_Empty) ds@1952: ){ ds@1952: for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ ds@1952: if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ ds@1952: ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; ds@1952: break; ds@1952: } ds@1952: } ds@1952: if( !pEnc->zName ){ ds@1952: sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); ds@1952: } ds@1952: } ds@1952: } ds@1952: }else ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS ds@1952: /* ds@1952: ** PRAGMA [database.]schema_version ds@1952: ** PRAGMA [database.]schema_version = ds@1952: ** ds@1952: ** PRAGMA [database.]user_version ds@1952: ** PRAGMA [database.]user_version = ds@1952: ** ds@1952: ** The pragma's schema_version and user_version are used to set or get ds@1952: ** the value of the schema-version and user-version, respectively. Both ds@1952: ** the schema-version and the user-version are 32-bit signed integers ds@1952: ** stored in the database header. ds@1952: ** ds@1952: ** The schema-cookie is usually only manipulated internally by SQLite. It ds@1952: ** is incremented by SQLite whenever the database schema is modified (by ds@1952: ** creating or dropping a table or index). The schema version is used by ds@1952: ** SQLite each time a query is executed to ensure that the internal cache ds@1952: ** of the schema used when compiling the SQL query matches the schema of ds@1952: ** the database against which the compiled query is actually executed. ds@1952: ** Subverting this mechanism by using "PRAGMA schema_version" to modify ds@1952: ** the schema-version is potentially dangerous and may lead to program ds@1952: ** crashes or database corruption. Use with caution! ds@1952: ** ds@1952: ** The user-version is not used internally by SQLite. It may be used by ds@1952: ** applications for any purpose. ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft, "schema_version")==0 ds@1952: || sqlite3StrICmp(zLeft, "user_version")==0 ds@1952: || sqlite3StrICmp(zLeft, "freelist_count")==0 ds@1952: ){ ds@1952: ds@1952: int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */ ds@1952: sqlite3VdbeUsesBtree(v, iDb); ds@1952: switch( zLeft[0] ){ ds@1952: case 's': case 'S': ds@1952: iCookie = 0; ds@1952: break; ds@1952: case 'f': case 'F': ds@1952: iCookie = 1; ds@1952: iDb = (-1*(iDb+1)); ds@1952: assert(iDb<=0); ds@1952: break; ds@1952: default: ds@1952: iCookie = 5; ds@1952: break; ds@1952: } ds@1952: ds@1952: if( zRight && iDb>=0 ){ ds@1952: /* Write the specified cookie value */ ds@1952: static const VdbeOpList setCookie[] = { ds@1952: { OP_Transaction, 0, 1, 0}, /* 0 */ ds@1952: { OP_Integer, 0, 0, 0}, /* 1 */ ds@1952: { OP_SetCookie, 0, 0, 0}, /* 2 */ ds@1952: }; ds@1952: int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie); ds@1952: sqlite3VdbeChangeP1(v, addr, iDb); ds@1952: sqlite3VdbeChangeP1(v, addr+1, atoi(zRight)); ds@1952: sqlite3VdbeChangeP1(v, addr+2, iDb); ds@1952: sqlite3VdbeChangeP2(v, addr+2, iCookie); ds@1952: }else{ ds@1952: /* Read the specified cookie value */ ds@1952: static const VdbeOpList readCookie[] = { ds@1952: { OP_ReadCookie, 0, 0, 0}, /* 0 */ ds@1952: { OP_Callback, 1, 0, 0} ds@1952: }; ds@1952: int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie); ds@1952: sqlite3VdbeChangeP1(v, addr, iDb); ds@1952: sqlite3VdbeChangeP2(v, addr, iCookie); ds@1952: sqlite3VdbeSetNumCols(v, 1); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P3_TRANSIENT); ds@1952: } ds@1952: }else ds@1952: #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ ds@1952: ds@1952: #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) ds@1952: /* ds@1952: ** Report the current state of file logs for all databases ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft, "lock_status")==0 ){ ds@1952: static const char *const azLockName[] = { ds@1952: "unlocked", "shared", "reserved", "pending", "exclusive" ds@1952: }; ds@1952: int i; ds@1952: Vdbe *v = sqlite3GetVdbe(pParse); ds@1952: sqlite3VdbeSetNumCols(v, 2); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P3_STATIC); ds@1952: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P3_STATIC); ds@1952: for(i=0; inDb; i++){ ds@1952: Btree *pBt; ds@1952: Pager *pPager; ds@1952: const char *zState = "unknown"; ds@1952: int j; ds@1952: if( db->aDb[i].zName==0 ) continue; ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, P3_STATIC); ds@1952: pBt = db->aDb[i].pBt; ds@1952: if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){ ds@1952: zState = "closed"; ds@1952: }else if( sqlite3_file_control(db, db->aDb[i].zName, ds@1952: SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ ds@1952: zState = azLockName[j]; ds@1952: } ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, zState, P3_STATIC); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 2, 0); ds@1952: } ds@1952: }else ds@1952: #endif ds@1952: ds@1952: #ifdef SQLITE_SSE ds@1952: /* ds@1952: ** Check to see if the sqlite_statements table exists. Create it ds@1952: ** if it does not. ds@1952: */ ds@1952: if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){ ds@1952: extern int sqlite3CreateStatementsTable(Parse*); ds@1952: sqlite3CreateStatementsTable(pParse); ds@1952: }else ds@1952: #endif ds@1952: ds@1952: #if SQLITE_HAS_CODEC ds@1952: if( sqlite3StrICmp(zLeft, "key")==0 ){ ds@1952: sqlite3_key(db, zRight, strlen(zRight)); ds@1952: }else ds@1952: #endif ds@1952: #if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD) ds@1952: if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){ ds@1952: #if SQLITE_HAS_CODEC ds@1952: if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){ ds@1952: extern void sqlite3_activate_see(const char*); ds@1952: sqlite3_activate_see(&zRight[4]); ds@1952: } ds@1952: #endif ds@1952: #ifdef SQLITE_ENABLE_CEROD ds@1952: if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ ds@1952: extern void sqlite3_activate_cerod(const char*); ds@1952: sqlite3_activate_cerod(&zRight[6]); ds@1952: } ds@1952: #endif ds@1952: } ds@1952: #endif ds@1952: ds@1952: {} ds@1952: ds@1952: if( v ){ ds@1952: /* Code an OP_Expire at the end of each PRAGMA program to cause ds@1952: ** the VDBE implementing the pragma to expire. Most (all?) pragmas ds@1952: ** are only valid for a single execution. ds@1952: */ ds@1952: sqlite3VdbeAddOp(v, OP_Expire, 1, 0); ds@1952: ds@1952: /* ds@1952: ** Reset the safety level, in case the fullfsync flag or synchronous ds@1952: ** setting changed. ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_PAGER_PRAGMAS ds@1952: if( db->autoCommit ){ ds@1952: sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level, ds@1952: (db->flags&SQLITE_FullFSync)!=0); ds@1952: } ds@1952: #endif ds@1952: } ds@1952: pragma_out: ds@1952: sqlite3_free(zLeft); ds@1952: sqlite3_free(zRight); ds@1952: } ds@1952: ds@1952: #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */