Update contrib.
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains code used to implement the PRAGMA command.
14 ** $Id: pragma.c,v 1.189 2008/10/10 17:47:21 danielk1977 Exp $
16 #include "sqliteInt.h"
19 /* Ignore this whole file if pragmas are disabled
21 #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
24 ** Interpret the given string as a safety level. Return 0 for OFF,
25 ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
26 ** unrecognized string argument.
28 ** Note that the values returned are one less that the values that
29 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
30 ** to support legacy SQL code. The safety level used to be boolean
31 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
33 static int getSafetyLevel(const char *z){
34 /* 123456789 123456789 */
35 static const char zText[] = "onoffalseyestruefull";
36 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
37 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
38 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
44 for(i=0; i<sizeof(iLength); i++){
45 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
53 ** Interpret the given string as a boolean value.
55 static int getBoolean(const char *z){
56 return getSafetyLevel(z)&1;
60 ** Interpret the given string as a locking mode value.
62 static int getLockingMode(const char *z){
64 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
65 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
67 return PAGER_LOCKINGMODE_QUERY;
70 #ifndef SQLITE_OMIT_AUTOVACUUM
72 ** Interpret the given string as an auto-vacuum mode value.
74 ** The following strings, "none", "full" and "incremental" are
75 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
77 static int getAutoVacuum(const char *z){
79 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
80 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
81 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
83 return ((i>=0&&i<=2)?i:0);
85 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
87 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
89 ** Interpret the given string as a temp db location. Return 1 for file
90 ** backed temporary databases, 2 for the Red-Black tree in memory database
91 ** and 0 to use the compile-time default.
93 static int getTempStore(const char *z){
94 if( z[0]>='0' && z[0]<='2' ){
96 }else if( sqlite3StrICmp(z, "file")==0 ){
98 }else if( sqlite3StrICmp(z, "memory")==0 ){
104 #endif /* SQLITE_PAGER_PRAGMAS */
106 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
108 ** Invalidate temp storage, either when the temp storage is changed
109 ** from default, or when 'file' and the temp_store_directory has changed
111 static int invalidateTempStorage(Parse *pParse){
112 sqlite3 *db = pParse->db;
113 if( db->aDb[1].pBt!=0 ){
114 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
115 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
116 "from within a transaction");
119 sqlite3BtreeClose(db->aDb[1].pBt);
121 sqlite3ResetInternalSchema(db, 0);
125 #endif /* SQLITE_PAGER_PRAGMAS */
127 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
129 ** If the TEMP database is open, close it and mark the database schema
130 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
131 ** or DEFAULT_TEMP_STORE pragmas.
133 static int changeTempStorage(Parse *pParse, const char *zStorageType){
134 int ts = getTempStore(zStorageType);
135 sqlite3 *db = pParse->db;
136 if( db->temp_store==ts ) return SQLITE_OK;
137 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
143 #endif /* SQLITE_PAGER_PRAGMAS */
146 ** Generate code to return a single integer value.
148 static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
149 Vdbe *v = sqlite3GetVdbe(pParse);
150 int mem = ++pParse->nMem;
151 sqlite3VdbeAddOp2(v, OP_Integer, value, mem);
152 if( pParse->explain==0 ){
153 sqlite3VdbeSetNumCols(v, 1);
154 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P4_STATIC);
156 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
159 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
161 ** Check to see if zRight and zLeft refer to a pragma that queries
162 ** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
163 ** Also, implement the pragma.
165 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
166 static const struct sPragmaType {
167 const char *zName; /* Name of the pragma */
168 int mask; /* Mask for the db->flags value */
170 { "full_column_names", SQLITE_FullColNames },
171 { "short_column_names", SQLITE_ShortColNames },
172 { "count_changes", SQLITE_CountRows },
173 { "empty_result_callbacks", SQLITE_NullCallback },
174 { "legacy_file_format", SQLITE_LegacyFileFmt },
175 { "fullfsync", SQLITE_FullFSync },
177 { "sql_trace", SQLITE_SqlTrace },
178 { "vdbe_listing", SQLITE_VdbeListing },
179 { "vdbe_trace", SQLITE_VdbeTrace },
181 #ifndef SQLITE_OMIT_CHECK
182 { "ignore_check_constraints", SQLITE_IgnoreChecks },
184 /* The following is VERY experimental */
185 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
186 { "omit_readlock", SQLITE_NoReadlock },
188 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
189 ** flag if there are any active statements. */
190 { "read_uncommitted", SQLITE_ReadUncommitted },
193 const struct sPragmaType *p;
194 for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
195 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
196 sqlite3 *db = pParse->db;
198 v = sqlite3GetVdbe(pParse);
201 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
203 if( getBoolean(zRight) ){
204 db->flags |= p->mask;
206 db->flags &= ~p->mask;
209 /* Many of the flag-pragmas modify the code generated by the SQL
210 ** compiler (eg. count_changes). So add an opcode to expire all
211 ** compiled SQL statements after modifying a pragma value.
213 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
222 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
224 static const char *actionName(u8 action){
226 case OE_SetNull: return "SET NULL";
227 case OE_SetDflt: return "SET DEFAULT";
228 case OE_Restrict: return "RESTRICT";
229 case OE_Cascade: return "CASCADE";
235 ** Process a pragma statement.
237 ** Pragmas are of this form:
239 ** PRAGMA [database.]id [= value]
241 ** The identifier might also be a string. The value is a string, and
242 ** identifier, or a number. If minusFlag is true, then the value is
243 ** a number that was preceded by a minus sign.
245 ** If the left side is "database.id" then pId1 is the database name
246 ** and pId2 is the id. If the left side is just "id" then pId1 is the
247 ** id and pId2 is any empty string.
251 Token *pId1, /* First part of [database.]id field */
252 Token *pId2, /* Second part of [database.]id field, or NULL */
253 Token *pValue, /* Token for <value>, or NULL */
254 int minusFlag /* True if a '-' sign preceded <value> */
256 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
257 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
258 const char *zDb = 0; /* The database name */
259 Token *pId; /* Pointer to <id> token */
260 int iDb; /* Database index for <database> */
261 sqlite3 *db = pParse->db;
263 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
267 /* Interpret the [database.] part of the pragma statement. iDb is the
268 ** index of the database this pragma is being applied to in db.aDb[]. */
269 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
273 /* If the temp database has been explicitly named as part of the
274 ** pragma, make sure it is open.
276 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
280 zLeft = sqlite3NameFromToken(db, pId);
283 zRight = sqlite3MPrintf(db, "-%T", pValue);
285 zRight = sqlite3NameFromToken(db, pValue);
288 zDb = ((pId2 && pId2->n>0)?pDb->zName:0);
289 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
293 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
295 ** PRAGMA [database.]default_cache_size
296 ** PRAGMA [database.]default_cache_size=N
298 ** The first form reports the current persistent setting for the
299 ** page cache size. The value returned is the maximum number of
300 ** pages in the page cache. The second form sets both the current
301 ** page cache size value and the persistent page cache size value
302 ** stored in the database file.
304 ** The default cache size is stored in meta-value 2 of page 1 of the
305 ** database file. The cache size is actually the absolute value of
306 ** this memory location. The sign of meta-value 2 determines the
307 ** synchronous setting. A negative value means synchronous is off
308 ** and a positive value means synchronous is on.
310 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
311 static const VdbeOpList getCacheSize[] = {
312 { OP_ReadCookie, 0, 1, 2}, /* 0 */
313 { OP_IfPos, 1, 6, 0},
314 { OP_Integer, 0, 2, 0},
315 { OP_Subtract, 1, 2, 1},
316 { OP_IfPos, 1, 6, 0},
317 { OP_Integer, 0, 1, 0}, /* 5 */
318 { OP_ResultRow, 1, 1, 0},
321 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
322 sqlite3VdbeUsesBtree(v, iDb);
324 sqlite3VdbeSetNumCols(v, 1);
325 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P4_STATIC);
327 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
328 sqlite3VdbeChangeP1(v, addr, iDb);
329 sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE);
331 int size = atoi(zRight);
332 if( size<0 ) size = -size;
333 sqlite3BeginWriteOperation(pParse, 0, iDb);
334 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
335 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, 2);
336 addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
337 sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
338 sqlite3VdbeJumpHere(v, addr);
339 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 2, 1);
340 pDb->pSchema->cache_size = size;
341 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
346 ** PRAGMA [database.]page_size
347 ** PRAGMA [database.]page_size=N
349 ** The first form reports the current setting for the
350 ** database page size in bytes. The second form sets the
351 ** database page size value. The value can only be set if
352 ** the database has not yet been created.
354 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
355 Btree *pBt = pDb->pBt;
357 int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
358 returnSingleInt(pParse, "page_size", size);
360 /* Malloc may fail when setting the page-size, as there is an internal
361 ** buffer that the pager module resizes using sqlite3_realloc().
363 db->nextPagesize = atoi(zRight);
364 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1) ){
365 db->mallocFailed = 1;
371 ** PRAGMA [database.]max_page_count
372 ** PRAGMA [database.]max_page_count=N
374 ** The first form reports the current setting for the
375 ** maximum number of pages in the database file. The
376 ** second form attempts to change this setting. Both
377 ** forms return the current setting.
379 if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
380 Btree *pBt = pDb->pBt;
383 newMax = atoi(zRight);
386 newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
388 returnSingleInt(pParse, "max_page_count", newMax);
392 ** PRAGMA [database.]page_count
394 ** Return the number of pages in the specified database.
396 if( sqlite3StrICmp(zLeft,"page_count")==0 ){
399 v = sqlite3GetVdbe(pParse);
400 if( !v || sqlite3ReadSchema(pParse) ) goto pragma_out;
401 sqlite3CodeVerifySchema(pParse, iDb);
402 iReg = ++pParse->nMem;
403 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
404 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
405 sqlite3VdbeSetNumCols(v, 1);
406 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", P4_STATIC);
410 ** PRAGMA [database.]locking_mode
411 ** PRAGMA [database.]locking_mode = (normal|exclusive)
413 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
414 const char *zRet = "normal";
415 int eMode = getLockingMode(zRight);
417 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
418 /* Simple "PRAGMA locking_mode;" statement. This is a query for
419 ** the current default locking mode (which may be different to
420 ** the locking-mode of the main database).
422 eMode = db->dfltLockMode;
426 /* This indicates that no database name was specified as part
427 ** of the PRAGMA command. In this case the locking-mode must be
428 ** set on all attached databases, as well as the main db file.
430 ** Also, the sqlite3.dfltLockMode variable is set so that
431 ** any subsequently attached databases also use the specified
435 assert(pDb==&db->aDb[0]);
436 for(ii=2; ii<db->nDb; ii++){
437 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
438 sqlite3PagerLockingMode(pPager, eMode);
440 db->dfltLockMode = eMode;
442 pPager = sqlite3BtreePager(pDb->pBt);
443 eMode = sqlite3PagerLockingMode(pPager, eMode);
446 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
447 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
450 sqlite3VdbeSetNumCols(v, 1);
451 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P4_STATIC);
452 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
453 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
457 ** PRAGMA [database.]journal_mode
458 ** PRAGMA [database.]journal_mode = (delete|persist|off)
460 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
462 static char * const azModeName[] = {"delete", "persist", "off", "truncate"};
465 eMode = PAGER_JOURNALMODE_QUERY;
467 int n = strlen(zRight);
468 eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1;
469 while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
473 if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
474 /* Simple "PRAGMA journal_mode;" statement. This is a query for
475 ** the current default journal mode (which may be different to
476 ** the journal-mode of the main database).
478 eMode = db->dfltJournalMode;
482 /* This indicates that no database name was specified as part
483 ** of the PRAGMA command. In this case the journal-mode must be
484 ** set on all attached databases, as well as the main db file.
486 ** Also, the sqlite3.dfltJournalMode variable is set so that
487 ** any subsequently attached databases also use the specified
491 assert(pDb==&db->aDb[0]);
492 for(ii=1; ii<db->nDb; ii++){
493 if( db->aDb[ii].pBt ){
494 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
495 sqlite3PagerJournalMode(pPager, eMode);
498 db->dfltJournalMode = eMode;
500 pPager = sqlite3BtreePager(pDb->pBt);
501 eMode = sqlite3PagerJournalMode(pPager, eMode);
503 assert( eMode==PAGER_JOURNALMODE_DELETE
504 || eMode==PAGER_JOURNALMODE_TRUNCATE
505 || eMode==PAGER_JOURNALMODE_PERSIST
506 || eMode==PAGER_JOURNALMODE_OFF );
507 sqlite3VdbeSetNumCols(v, 1);
508 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", P4_STATIC);
509 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0,
510 azModeName[eMode], P4_STATIC);
511 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
515 ** PRAGMA [database.]journal_size_limit
516 ** PRAGMA [database.]journal_size_limit=N
518 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
520 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
521 Pager *pPager = sqlite3BtreePager(pDb->pBt);
524 int iLimit32 = atoi(zRight);
530 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
531 returnSingleInt(pParse, "journal_size_limit", (int)iLimit);
534 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
537 ** PRAGMA [database.]auto_vacuum
538 ** PRAGMA [database.]auto_vacuum=N
540 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
542 #ifndef SQLITE_OMIT_AUTOVACUUM
543 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
544 Btree *pBt = pDb->pBt;
545 if( sqlite3ReadSchema(pParse) ){
550 pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
551 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
553 int eAuto = getAutoVacuum(zRight);
554 db->nextAutovac = eAuto;
556 /* Call SetAutoVacuum() to set initialize the internal auto and
557 ** incr-vacuum flags. This is required in case this connection
558 ** creates the database file. It is important that it is created
559 ** as an auto-vacuum capable db.
561 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
562 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
563 /* When setting the auto_vacuum mode to either "full" or
564 ** "incremental", write the value of meta[6] in the database
565 ** file. Before writing to meta[6], check that meta[3] indicates
566 ** that this really is an auto-vacuum capable database.
568 static const VdbeOpList setMeta6[] = {
569 { OP_Transaction, 0, 1, 0}, /* 0 */
570 { OP_ReadCookie, 0, 1, 3}, /* 1 */
571 { OP_If, 1, 0, 0}, /* 2 */
572 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
573 { OP_Integer, 0, 1, 0}, /* 4 */
574 { OP_SetCookie, 0, 6, 1}, /* 5 */
577 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
578 sqlite3VdbeChangeP1(v, iAddr, iDb);
579 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
580 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
581 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
582 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
583 sqlite3VdbeUsesBtree(v, iDb);
591 ** PRAGMA [database.]incremental_vacuum(N)
593 ** Do N steps of incremental vacuuming on a database.
595 #ifndef SQLITE_OMIT_AUTOVACUUM
596 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
598 if( sqlite3ReadSchema(pParse) ){
601 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
604 sqlite3BeginWriteOperation(pParse, 0, iDb);
605 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
606 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
607 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
608 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
609 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
610 sqlite3VdbeJumpHere(v, addr);
614 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
616 ** PRAGMA [database.]cache_size
617 ** PRAGMA [database.]cache_size=N
619 ** The first form reports the current local setting for the
620 ** page cache size. The local setting can be different from
621 ** the persistent cache size value that is stored in the database
622 ** file itself. The value returned is the maximum number of
623 ** pages in the page cache. The second form sets the local
624 ** page cache size value. It does not change the persistent
625 ** cache size stored on the disk so the cache size will revert
626 ** to its default value when the database is closed and reopened.
627 ** N should be a positive integer.
629 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
630 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
632 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
634 int size = atoi(zRight);
635 if( size<0 ) size = -size;
636 pDb->pSchema->cache_size = size;
637 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
643 ** PRAGMA temp_store = "default"|"memory"|"file"
645 ** Return or set the local value of the temp_store flag. Changing
646 ** the local value does not make changes to the disk file and the default
647 ** value will be restored the next time the database is opened.
649 ** Note that it is possible for the library compile-time options to
650 ** override this setting
652 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
654 returnSingleInt(pParse, "temp_store", db->temp_store);
656 changeTempStorage(pParse, zRight);
661 ** PRAGMA temp_store_directory
662 ** PRAGMA temp_store_directory = ""|"directory_name"
664 ** Return or set the local value of the temp_store_directory flag. Changing
665 ** the value sets a specific directory to be used for temporary files.
666 ** Setting to a null string reverts to the default temporary directory search.
667 ** If temporary directory is changed, then invalidateTempStorage.
670 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
672 if( sqlite3_temp_directory ){
673 sqlite3VdbeSetNumCols(v, 1);
674 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
675 "temp_store_directory", P4_STATIC);
676 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
677 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
680 #ifndef SQLITE_OMIT_WSD
684 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
685 if( rc!=SQLITE_OK || res==0 ){
686 sqlite3ErrorMsg(pParse, "not a writable directory");
690 if( SQLITE_TEMP_STORE==0
691 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
692 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
694 invalidateTempStorage(pParse);
696 sqlite3_free(sqlite3_temp_directory);
698 sqlite3_temp_directory = sqlite3DbStrDup(0, zRight);
700 sqlite3_temp_directory = 0;
702 #endif /* SQLITE_OMIT_WSD */
707 ** PRAGMA [database.]synchronous
708 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
710 ** Return or set the local value of the synchronous flag. Changing
711 ** the local value does not make changes to the disk file and the
712 ** default value will be restored the next time the database is
715 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
716 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
718 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
720 if( !db->autoCommit ){
721 sqlite3ErrorMsg(pParse,
722 "Safety level may not be changed inside a transaction");
724 pDb->safety_level = getSafetyLevel(zRight)+1;
728 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
730 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
731 if( flagPragma(pParse, zLeft, zRight) ){
732 /* The flagPragma() subroutine also generates any necessary code
733 ** there is nothing more to do here */
735 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
737 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
739 ** PRAGMA table_info(<table>)
741 ** Return a single row for each column of the named table. The columns of
742 ** the returned data set are:
744 ** cid: Column id (numbered from left to right, starting at 0)
746 ** type: Column declaration type.
747 ** notnull: True if 'NOT NULL' is part of column declaration
748 ** dflt_value: The default value for the column, if any.
750 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
752 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
753 pTab = sqlite3FindTable(db, zRight, zDb);
758 sqlite3VdbeSetNumCols(v, 6);
760 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P4_STATIC);
761 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
762 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P4_STATIC);
763 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P4_STATIC);
764 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P4_STATIC);
765 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P4_STATIC);
766 sqlite3ViewGetColumnNames(pParse, pTab);
767 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
769 if( IsHiddenColumn(pCol) ){
773 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
774 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
775 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
776 pCol->zType ? pCol->zType : "", 0);
777 sqlite3VdbeAddOp2(v, OP_Integer, pCol->notNull, 4);
778 if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){
779 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pDflt->z, pDflt->n);
781 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
783 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
784 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
789 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
792 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
793 pIdx = sqlite3FindIndex(db, zRight, zDb);
797 sqlite3VdbeSetNumCols(v, 3);
799 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P4_STATIC);
800 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P4_STATIC);
801 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P4_STATIC);
802 for(i=0; i<pIdx->nColumn; i++){
803 int cnum = pIdx->aiColumn[i];
804 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
805 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
806 assert( pTab->nCol>cnum );
807 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
808 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
813 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
816 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
817 pTab = sqlite3FindTable(db, zRight, zDb);
819 v = sqlite3GetVdbe(pParse);
823 sqlite3VdbeSetNumCols(v, 3);
825 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
826 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
827 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P4_STATIC);
829 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
830 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
831 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
832 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
840 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
842 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
843 sqlite3VdbeSetNumCols(v, 3);
845 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
846 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
847 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P4_STATIC);
848 for(i=0; i<db->nDb; i++){
849 if( db->aDb[i].pBt==0 ) continue;
850 assert( db->aDb[i].zName!=0 );
851 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
852 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
853 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
854 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
855 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
859 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
862 sqlite3VdbeSetNumCols(v, 2);
864 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
865 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
866 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
867 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
868 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
869 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
870 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
873 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
875 #ifndef SQLITE_OMIT_FOREIGN_KEY
876 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
879 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
880 pTab = sqlite3FindTable(db, zRight, zDb);
882 v = sqlite3GetVdbe(pParse);
886 sqlite3VdbeSetNumCols(v, 8);
888 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P4_STATIC);
889 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P4_STATIC);
890 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P4_STATIC);
891 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P4_STATIC);
892 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P4_STATIC);
893 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", P4_STATIC);
894 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", P4_STATIC);
895 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", P4_STATIC);
898 for(j=0; j<pFK->nCol; j++){
899 char *zCol = pFK->aCol[j].zCol;
900 char *zOnUpdate = (char *)actionName(pFK->updateConf);
901 char *zOnDelete = (char *)actionName(pFK->deleteConf);
902 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
903 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
904 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
905 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
906 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
907 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
908 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
909 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
910 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
911 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
914 pFK = pFK->pNextFrom;
919 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
922 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
924 if( getBoolean(zRight) ){
925 sqlite3ParserTrace(stderr, "parser: ");
927 sqlite3ParserTrace(0, 0);
933 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
934 ** used will be case sensitive or not depending on the RHS.
936 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
938 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
942 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
943 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
946 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
947 /* Pragma "quick_check" is an experimental reduced version of
948 ** integrity_check designed to detect most database corruption
949 ** without most of the overhead of a full integrity-check.
951 if( sqlite3StrICmp(zLeft, "integrity_check")==0
952 || sqlite3StrICmp(zLeft, "quick_check")==0
954 int i, j, addr, mxErr;
956 /* Code that appears at the end of the integrity check. If no error
957 ** messages have been generated, output OK. Otherwise output the
960 static const VdbeOpList endCode[] = {
961 { OP_AddImm, 1, 0, 0}, /* 0 */
962 { OP_IfNeg, 1, 0, 0}, /* 1 */
963 { OP_String8, 0, 3, 0}, /* 2 */
964 { OP_ResultRow, 3, 1, 0},
967 int isQuick = (zLeft[0]=='q');
969 /* Initialize the VDBE program */
970 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
972 sqlite3VdbeSetNumCols(v, 1);
973 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P4_STATIC);
975 /* Set the maximum error count */
976 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
978 mxErr = atoi(zRight);
980 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
983 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
985 /* Do an integrity check on each database file */
986 for(i=0; i<db->nDb; i++){
991 if( OMIT_TEMPDB && i==1 ) continue;
993 sqlite3CodeVerifySchema(pParse, i);
994 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
995 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
996 sqlite3VdbeJumpHere(v, addr);
998 /* Do an integrity check of the B-Tree
1000 ** Begin by filling registers 2, 3, ... with the root pages numbers
1001 ** for all tables and indices in the database.
1003 pTbls = &db->aDb[i].pSchema->tblHash;
1004 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1005 Table *pTab = sqliteHashData(x);
1007 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
1009 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1010 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
1014 if( cnt==0 ) continue;
1016 /* Make sure sufficient number of registers have been allocated */
1017 if( pParse->nMem < cnt+4 ){
1018 pParse->nMem = cnt+4;
1021 /* Do the b-tree integrity checks */
1022 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
1023 sqlite3VdbeChangeP5(v, i);
1024 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1025 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
1026 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
1028 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
1029 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
1030 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
1031 sqlite3VdbeJumpHere(v, addr);
1033 /* Make sure all the indices are constructed correctly.
1035 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
1036 Table *pTab = sqliteHashData(x);
1040 if( pTab->pIndex==0 ) continue;
1041 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
1042 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1043 sqlite3VdbeJumpHere(v, addr);
1044 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
1045 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
1046 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
1047 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
1048 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1050 static const VdbeOpList idxErr[] = {
1051 { OP_AddImm, 1, -1, 0},
1052 { OP_String8, 0, 3, 0}, /* 1 */
1053 { OP_Rowid, 1, 4, 0},
1054 { OP_String8, 0, 5, 0}, /* 3 */
1055 { OP_String8, 0, 6, 0}, /* 4 */
1056 { OP_Concat, 4, 3, 3},
1057 { OP_Concat, 5, 3, 3},
1058 { OP_Concat, 6, 3, 3},
1059 { OP_ResultRow, 3, 1, 0},
1060 { OP_IfPos, 1, 0, 0}, /* 9 */
1061 { OP_Halt, 0, 0, 0},
1063 sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 1);
1064 jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3);
1065 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
1066 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1067 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
1068 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
1069 sqlite3VdbeJumpHere(v, addr+9);
1070 sqlite3VdbeJumpHere(v, jmp2);
1072 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
1073 sqlite3VdbeJumpHere(v, loopTop);
1074 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1075 static const VdbeOpList cntIdx[] = {
1076 { OP_Integer, 0, 3, 0},
1077 { OP_Rewind, 0, 0, 0}, /* 1 */
1078 { OP_AddImm, 3, 1, 0},
1079 { OP_Next, 0, 0, 0}, /* 3 */
1080 { OP_Eq, 2, 0, 3}, /* 4 */
1081 { OP_AddImm, 1, -1, 0},
1082 { OP_String8, 0, 2, 0}, /* 6 */
1083 { OP_String8, 0, 3, 0}, /* 7 */
1084 { OP_Concat, 3, 2, 2},
1085 { OP_ResultRow, 2, 1, 0},
1087 if( pIdx->tnum==0 ) continue;
1088 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
1089 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1090 sqlite3VdbeJumpHere(v, addr);
1091 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
1092 sqlite3VdbeChangeP1(v, addr+1, j+2);
1093 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1094 sqlite3VdbeChangeP1(v, addr+3, j+2);
1095 sqlite3VdbeChangeP2(v, addr+3, addr+2);
1096 sqlite3VdbeJumpHere(v, addr+4);
1097 sqlite3VdbeChangeP4(v, addr+6,
1098 "wrong # of entries in index ", P4_STATIC);
1099 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
1103 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
1104 sqlite3VdbeChangeP2(v, addr, -mxErr);
1105 sqlite3VdbeJumpHere(v, addr+1);
1106 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
1108 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1110 #ifndef SQLITE_OMIT_UTF16
1113 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1115 ** In its first form, this pragma returns the encoding of the main
1116 ** database. If the database is not initialized, it is initialized now.
1118 ** The second form of this pragma is a no-op if the main database file
1119 ** has not already been initialized. In this case it sets the default
1120 ** encoding that will be used for the main database file if a new file
1121 ** is created. If an existing main database file is opened, then the
1122 ** default text encoding for the existing database is used.
1124 ** In all cases new databases created using the ATTACH command are
1125 ** created to use the same default text encoding as the main database. If
1126 ** the main database has not been initialized and/or created when ATTACH
1127 ** is executed, this is done before the ATTACH operation.
1129 ** In the second form this pragma sets the text encoding to be used in
1130 ** new database files created using this database handle. It is only
1131 ** useful if invoked immediately after the main database i
1133 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
1134 static const struct EncName {
1138 { "UTF-8", SQLITE_UTF8 },
1139 { "UTF8", SQLITE_UTF8 },
1140 { "UTF-16le", SQLITE_UTF16LE },
1141 { "UTF16le", SQLITE_UTF16LE },
1142 { "UTF-16be", SQLITE_UTF16BE },
1143 { "UTF16be", SQLITE_UTF16BE },
1144 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1145 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
1148 const struct EncName *pEnc;
1149 if( !zRight ){ /* "PRAGMA encoding" */
1150 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
1151 sqlite3VdbeSetNumCols(v, 1);
1152 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P4_STATIC);
1153 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
1154 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1155 if( pEnc->enc==ENC(pParse->db) ){
1156 sqlite3VdbeChangeP4(v, -1, pEnc->zName, P4_STATIC);
1160 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1161 }else{ /* "PRAGMA encoding = XXX" */
1162 /* Only change the value of sqlite.enc if the database handle is not
1163 ** initialized. If the main database exists, the new sqlite.enc value
1164 ** will be overwritten when the schema is next loaded. If it does not
1165 ** already exists, it will be created to use the new encoding value.
1168 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1169 DbHasProperty(db, 0, DB_Empty)
1171 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1172 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
1173 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
1178 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
1183 #endif /* SQLITE_OMIT_UTF16 */
1185 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
1187 ** PRAGMA [database.]schema_version
1188 ** PRAGMA [database.]schema_version = <integer>
1190 ** PRAGMA [database.]user_version
1191 ** PRAGMA [database.]user_version = <integer>
1193 ** The pragma's schema_version and user_version are used to set or get
1194 ** the value of the schema-version and user-version, respectively. Both
1195 ** the schema-version and the user-version are 32-bit signed integers
1196 ** stored in the database header.
1198 ** The schema-cookie is usually only manipulated internally by SQLite. It
1199 ** is incremented by SQLite whenever the database schema is modified (by
1200 ** creating or dropping a table or index). The schema version is used by
1201 ** SQLite each time a query is executed to ensure that the internal cache
1202 ** of the schema used when compiling the SQL query matches the schema of
1203 ** the database against which the compiled query is actually executed.
1204 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1205 ** the schema-version is potentially dangerous and may lead to program
1206 ** crashes or database corruption. Use with caution!
1208 ** The user-version is not used internally by SQLite. It may be used by
1209 ** applications for any purpose.
1211 if( sqlite3StrICmp(zLeft, "schema_version")==0
1212 || sqlite3StrICmp(zLeft, "user_version")==0
1213 || sqlite3StrICmp(zLeft, "freelist_count")==0
1215 int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
1216 sqlite3VdbeUsesBtree(v, iDb);
1231 if( zRight && iDb>=0 ){
1232 /* Write the specified cookie value */
1233 static const VdbeOpList setCookie[] = {
1234 { OP_Transaction, 0, 1, 0}, /* 0 */
1235 { OP_Integer, 0, 1, 0}, /* 1 */
1236 { OP_SetCookie, 0, 0, 1}, /* 2 */
1238 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1239 sqlite3VdbeChangeP1(v, addr, iDb);
1240 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
1241 sqlite3VdbeChangeP1(v, addr+2, iDb);
1242 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1244 /* Read the specified cookie value */
1245 static const VdbeOpList readCookie[] = {
1246 { OP_ReadCookie, 0, 1, 0}, /* 0 */
1247 { OP_ResultRow, 1, 1, 0}
1249 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1250 sqlite3VdbeChangeP1(v, addr, iDb);
1251 sqlite3VdbeChangeP3(v, addr, iCookie);
1252 sqlite3VdbeSetNumCols(v, 1);
1253 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P4_TRANSIENT);
1256 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
1258 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
1260 ** Report the current state of file logs for all databases
1262 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
1263 static const char *const azLockName[] = {
1264 "unlocked", "shared", "reserved", "pending", "exclusive"
1267 Vdbe *v = sqlite3GetVdbe(pParse);
1268 sqlite3VdbeSetNumCols(v, 2);
1270 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P4_STATIC);
1271 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P4_STATIC);
1272 for(i=0; i<db->nDb; i++){
1275 const char *zState = "unknown";
1277 if( db->aDb[i].zName==0 ) continue;
1278 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
1279 pBt = db->aDb[i].pBt;
1280 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
1282 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
1283 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1284 zState = azLockName[j];
1286 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1287 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
1295 ** Check to see if the sqlite_statements table exists. Create it
1298 if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
1299 extern int sqlite3CreateStatementsTable(Parse*);
1300 sqlite3CreateStatementsTable(pParse);
1304 #if SQLITE_HAS_CODEC
1305 if( sqlite3StrICmp(zLeft, "key")==0 ){
1306 sqlite3_key(db, zRight, strlen(zRight));
1309 #if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
1310 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
1311 #if SQLITE_HAS_CODEC
1312 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
1313 extern void sqlite3_activate_see(const char*);
1314 sqlite3_activate_see(&zRight[4]);
1317 #ifdef SQLITE_ENABLE_CEROD
1318 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
1319 extern void sqlite3_activate_cerod(const char*);
1320 sqlite3_activate_cerod(&zRight[6]);
1329 /* Code an OP_Expire at the end of each PRAGMA program to cause
1330 ** the VDBE implementing the pragma to expire. Most (all?) pragmas
1331 ** are only valid for a single execution.
1333 sqlite3VdbeAddOp2(v, OP_Expire, 1, 0);
1336 ** Reset the safety level, in case the fullfsync flag or synchronous
1339 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
1340 if( db->autoCommit ){
1341 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
1342 (db->flags&SQLITE_FullFSync)!=0);
1347 sqlite3DbFree(db, zLeft);
1348 sqlite3DbFree(db, zRight);
1351 #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */