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 #include "sqliteInt.h"
16 /* Ignore this whole file if pragmas are disabled
18 #if !defined(SQLITE_OMIT_PRAGMA)
21 ** Interpret the given string as a safety level. Return 0 for OFF,
22 ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
23 ** unrecognized string argument.
25 ** Note that the values returned are one less that the values that
26 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
27 ** to support legacy SQL code. The safety level used to be boolean
28 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
30 static u8 getSafetyLevel(const char *z){
31 /* 123456789 123456789 */
32 static const char zText[] = "onoffalseyestruefull";
33 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
34 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
35 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
37 if( sqlite3Isdigit(*z) ){
38 return (u8)sqlite3Atoi(z);
40 n = sqlite3Strlen30(z);
41 for(i=0; i<ArraySize(iLength); i++){
42 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
50 ** Interpret the given string as a boolean value.
52 static u8 getBoolean(const char *z){
53 return getSafetyLevel(z)&1;
57 ** Interpret the given string as a locking mode value.
59 static int getLockingMode(const char *z){
61 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
62 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
64 return PAGER_LOCKINGMODE_QUERY;
67 #ifndef SQLITE_OMIT_AUTOVACUUM
69 ** Interpret the given string as an auto-vacuum mode value.
71 ** The following strings, "none", "full" and "incremental" are
72 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
74 static int getAutoVacuum(const char *z){
76 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
77 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
78 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
80 return (u8)((i>=0&&i<=2)?i:0);
82 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
84 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
86 ** Interpret the given string as a temp db location. Return 1 for file
87 ** backed temporary databases, 2 for the Red-Black tree in memory database
88 ** and 0 to use the compile-time default.
90 static int getTempStore(const char *z){
91 if( z[0]>='0' && z[0]<='2' ){
93 }else if( sqlite3StrICmp(z, "file")==0 ){
95 }else if( sqlite3StrICmp(z, "memory")==0 ){
101 #endif /* SQLITE_PAGER_PRAGMAS */
103 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
105 ** Invalidate temp storage, either when the temp storage is changed
106 ** from default, or when 'file' and the temp_store_directory has changed
108 static int invalidateTempStorage(Parse *pParse){
109 sqlite3 *db = pParse->db;
110 if( db->aDb[1].pBt!=0 ){
111 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
112 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
113 "from within a transaction");
116 sqlite3BtreeClose(db->aDb[1].pBt);
118 sqlite3ResetInternalSchema(db, -1);
122 #endif /* SQLITE_PAGER_PRAGMAS */
124 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
126 ** If the TEMP database is open, close it and mark the database schema
127 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
128 ** or DEFAULT_TEMP_STORE pragmas.
130 static int changeTempStorage(Parse *pParse, const char *zStorageType){
131 int ts = getTempStore(zStorageType);
132 sqlite3 *db = pParse->db;
133 if( db->temp_store==ts ) return SQLITE_OK;
134 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
137 db->temp_store = (u8)ts;
140 #endif /* SQLITE_PAGER_PRAGMAS */
143 ** Generate code to return a single integer value.
145 static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
146 Vdbe *v = sqlite3GetVdbe(pParse);
147 int mem = ++pParse->nMem;
148 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
150 memcpy(pI64, &value, sizeof(value));
152 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
153 sqlite3VdbeSetNumCols(v, 1);
154 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
155 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
158 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
160 ** Check to see if zRight and zLeft refer to a pragma that queries
161 ** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
162 ** Also, implement the pragma.
164 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
165 static const struct sPragmaType {
166 const char *zName; /* Name of the pragma */
167 int mask; /* Mask for the db->flags value */
169 { "full_column_names", SQLITE_FullColNames },
170 { "short_column_names", SQLITE_ShortColNames },
171 { "count_changes", SQLITE_CountRows },
172 { "empty_result_callbacks", SQLITE_NullCallback },
173 { "legacy_file_format", SQLITE_LegacyFileFmt },
174 { "fullfsync", SQLITE_FullFSync },
175 { "checkpoint_fullfsync", SQLITE_CkptFullFSync },
176 { "reverse_unordered_selects", SQLITE_ReverseOrder },
177 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
178 { "automatic_index", SQLITE_AutoIndex },
181 { "sql_trace", SQLITE_SqlTrace },
182 { "vdbe_listing", SQLITE_VdbeListing },
183 { "vdbe_trace", SQLITE_VdbeTrace },
185 #ifndef SQLITE_OMIT_CHECK
186 { "ignore_check_constraints", SQLITE_IgnoreChecks },
188 /* The following is VERY experimental */
189 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
190 { "omit_readlock", SQLITE_NoReadlock },
192 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
193 ** flag if there are any active statements. */
194 { "read_uncommitted", SQLITE_ReadUncommitted },
195 { "recursive_triggers", SQLITE_RecTriggers },
197 /* This flag may only be set if both foreign-key and trigger support
198 ** are present in the build. */
199 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
200 { "foreign_keys", SQLITE_ForeignKeys },
204 const struct sPragmaType *p;
205 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
206 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
207 sqlite3 *db = pParse->db;
209 v = sqlite3GetVdbe(pParse);
210 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
213 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
215 int mask = p->mask; /* Mask of bits to set or clear. */
216 if( db->autoCommit==0 ){
217 /* Foreign key support may not be enabled or disabled while not
218 ** in auto-commit mode. */
219 mask &= ~(SQLITE_ForeignKeys);
222 if( getBoolean(zRight) ){
228 /* Many of the flag-pragmas modify the code generated by the SQL
229 ** compiler (eg. count_changes). So add an opcode to expire all
230 ** compiled SQL statements after modifying a pragma value.
232 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
241 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
244 ** Return a human-readable name for a constraint resolution action.
246 #ifndef SQLITE_OMIT_FOREIGN_KEY
247 static const char *actionName(u8 action){
250 case OE_SetNull: zName = "SET NULL"; break;
251 case OE_SetDflt: zName = "SET DEFAULT"; break;
252 case OE_Cascade: zName = "CASCADE"; break;
253 case OE_Restrict: zName = "RESTRICT"; break;
254 default: zName = "NO ACTION";
255 assert( action==OE_None ); break;
263 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
264 ** defined in pager.h. This function returns the associated lowercase
265 ** journal-mode name.
267 const char *sqlite3JournalModename(int eMode){
268 static char * const azModeName[] = {
269 "delete", "persist", "off", "truncate", "memory"
270 #ifndef SQLITE_OMIT_WAL
274 assert( PAGER_JOURNALMODE_DELETE==0 );
275 assert( PAGER_JOURNALMODE_PERSIST==1 );
276 assert( PAGER_JOURNALMODE_OFF==2 );
277 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
278 assert( PAGER_JOURNALMODE_MEMORY==4 );
279 assert( PAGER_JOURNALMODE_WAL==5 );
280 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
282 if( eMode==ArraySize(azModeName) ) return 0;
283 return azModeName[eMode];
287 ** Process a pragma statement.
289 ** Pragmas are of this form:
291 ** PRAGMA [database.]id [= value]
293 ** The identifier might also be a string. The value is a string, and
294 ** identifier, or a number. If minusFlag is true, then the value is
295 ** a number that was preceded by a minus sign.
297 ** If the left side is "database.id" then pId1 is the database name
298 ** and pId2 is the id. If the left side is just "id" then pId1 is the
299 ** id and pId2 is any empty string.
303 Token *pId1, /* First part of [database.]id field */
304 Token *pId2, /* Second part of [database.]id field, or NULL */
305 Token *pValue, /* Token for <value>, or NULL */
306 int minusFlag /* True if a '-' sign preceded <value> */
308 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
309 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
310 const char *zDb = 0; /* The database name */
311 Token *pId; /* Pointer to <id> token */
312 int iDb; /* Database index for <database> */
313 sqlite3 *db = pParse->db;
315 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
317 sqlite3VdbeRunOnlyOnce(v);
320 /* Interpret the [database.] part of the pragma statement. iDb is the
321 ** index of the database this pragma is being applied to in db.aDb[]. */
322 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
326 /* If the temp database has been explicitly named as part of the
327 ** pragma, make sure it is open.
329 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
333 zLeft = sqlite3NameFromToken(db, pId);
336 zRight = sqlite3MPrintf(db, "-%T", pValue);
338 zRight = sqlite3NameFromToken(db, pValue);
342 zDb = pId2->n>0 ? pDb->zName : 0;
343 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
347 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
349 ** PRAGMA [database.]default_cache_size
350 ** PRAGMA [database.]default_cache_size=N
352 ** The first form reports the current persistent setting for the
353 ** page cache size. The value returned is the maximum number of
354 ** pages in the page cache. The second form sets both the current
355 ** page cache size value and the persistent page cache size value
356 ** stored in the database file.
358 ** Older versions of SQLite would set the default cache size to a
359 ** negative number to indicate synchronous=OFF. These days, synchronous
360 ** is always on by default regardless of the sign of the default cache
361 ** size. But continue to take the absolute value of the default cache
362 ** size of historical compatibility.
364 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
365 static const VdbeOpList getCacheSize[] = {
366 { OP_Transaction, 0, 0, 0}, /* 0 */
367 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
368 { OP_IfPos, 1, 7, 0},
369 { OP_Integer, 0, 2, 0},
370 { OP_Subtract, 1, 2, 1},
371 { OP_IfPos, 1, 7, 0},
372 { OP_Integer, 0, 1, 0}, /* 6 */
373 { OP_ResultRow, 1, 1, 0},
376 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
377 sqlite3VdbeUsesBtree(v, iDb);
379 sqlite3VdbeSetNumCols(v, 1);
380 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
382 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
383 sqlite3VdbeChangeP1(v, addr, iDb);
384 sqlite3VdbeChangeP1(v, addr+1, iDb);
385 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
387 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
388 sqlite3BeginWriteOperation(pParse, 0, iDb);
389 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
390 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
391 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
392 pDb->pSchema->cache_size = size;
393 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
398 ** PRAGMA [database.]page_size
399 ** PRAGMA [database.]page_size=N
401 ** The first form reports the current setting for the
402 ** database page size in bytes. The second form sets the
403 ** database page size value. The value can only be set if
404 ** the database has not yet been created.
406 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
407 Btree *pBt = pDb->pBt;
410 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
411 returnSingleInt(pParse, "page_size", size);
413 /* Malloc may fail when setting the page-size, as there is an internal
414 ** buffer that the pager module resizes using sqlite3_realloc().
416 db->nextPagesize = sqlite3Atoi(zRight);
417 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
418 db->mallocFailed = 1;
424 ** PRAGMA [database.]secure_delete
425 ** PRAGMA [database.]secure_delete=ON/OFF
427 ** The first form reports the current setting for the
428 ** secure_delete flag. The second form changes the secure_delete
429 ** flag setting and reports thenew value.
431 if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
432 Btree *pBt = pDb->pBt;
436 b = getBoolean(zRight);
438 if( pId2->n==0 && b>=0 ){
440 for(ii=0; ii<db->nDb; ii++){
441 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
444 b = sqlite3BtreeSecureDelete(pBt, b);
445 returnSingleInt(pParse, "secure_delete", b);
449 ** PRAGMA [database.]max_page_count
450 ** PRAGMA [database.]max_page_count=N
452 ** The first form reports the current setting for the
453 ** maximum number of pages in the database file. The
454 ** second form attempts to change this setting. Both
455 ** forms return the current setting.
457 ** PRAGMA [database.]page_count
459 ** Return the number of pages in the specified database.
461 if( sqlite3StrICmp(zLeft,"page_count")==0
462 || sqlite3StrICmp(zLeft,"max_page_count")==0
465 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
466 sqlite3CodeVerifySchema(pParse, iDb);
467 iReg = ++pParse->nMem;
469 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
471 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
473 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
474 sqlite3VdbeSetNumCols(v, 1);
475 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
479 ** PRAGMA [database.]locking_mode
480 ** PRAGMA [database.]locking_mode = (normal|exclusive)
482 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
483 const char *zRet = "normal";
484 int eMode = getLockingMode(zRight);
486 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
487 /* Simple "PRAGMA locking_mode;" statement. This is a query for
488 ** the current default locking mode (which may be different to
489 ** the locking-mode of the main database).
491 eMode = db->dfltLockMode;
495 /* This indicates that no database name was specified as part
496 ** of the PRAGMA command. In this case the locking-mode must be
497 ** set on all attached databases, as well as the main db file.
499 ** Also, the sqlite3.dfltLockMode variable is set so that
500 ** any subsequently attached databases also use the specified
504 assert(pDb==&db->aDb[0]);
505 for(ii=2; ii<db->nDb; ii++){
506 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
507 sqlite3PagerLockingMode(pPager, eMode);
509 db->dfltLockMode = (u8)eMode;
511 pPager = sqlite3BtreePager(pDb->pBt);
512 eMode = sqlite3PagerLockingMode(pPager, eMode);
515 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
516 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
519 sqlite3VdbeSetNumCols(v, 1);
520 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
521 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
522 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
526 ** PRAGMA [database.]journal_mode
527 ** PRAGMA [database.]journal_mode =
528 ** (delete|persist|off|truncate|memory|wal|off)
530 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
531 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
532 int ii; /* Loop counter */
534 /* Force the schema to be loaded on all databases. This cases all
535 ** database files to be opened and the journal_modes set. */
536 if( sqlite3ReadSchema(pParse) ){
540 sqlite3VdbeSetNumCols(v, 1);
541 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
544 /* If there is no "=MODE" part of the pragma, do a query for the
546 eMode = PAGER_JOURNALMODE_QUERY;
549 int n = sqlite3Strlen30(zRight);
550 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
551 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
554 /* If the "=MODE" part does not match any known journal mode,
555 ** then do a query */
556 eMode = PAGER_JOURNALMODE_QUERY;
559 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
560 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
564 for(ii=db->nDb-1; ii>=0; ii--){
565 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
566 sqlite3VdbeUsesBtree(v, ii);
567 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
570 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
574 ** PRAGMA [database.]journal_size_limit
575 ** PRAGMA [database.]journal_size_limit=N
577 ** Get or set the size limit on rollback journal files.
579 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
580 Pager *pPager = sqlite3BtreePager(pDb->pBt);
583 sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
584 if( iLimit<-1 ) iLimit = -1;
586 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
587 returnSingleInt(pParse, "journal_size_limit", iLimit);
590 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
593 ** PRAGMA [database.]auto_vacuum
594 ** PRAGMA [database.]auto_vacuum=N
596 ** Get or set the value of the database 'auto-vacuum' parameter.
597 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
599 #ifndef SQLITE_OMIT_AUTOVACUUM
600 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
601 Btree *pBt = pDb->pBt;
603 if( sqlite3ReadSchema(pParse) ){
609 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
611 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
613 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
615 int eAuto = getAutoVacuum(zRight);
616 assert( eAuto>=0 && eAuto<=2 );
617 db->nextAutovac = (u8)eAuto;
618 if( ALWAYS(eAuto>=0) ){
619 /* Call SetAutoVacuum() to set initialize the internal auto and
620 ** incr-vacuum flags. This is required in case this connection
621 ** creates the database file. It is important that it is created
622 ** as an auto-vacuum capable db.
624 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
625 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
626 /* When setting the auto_vacuum mode to either "full" or
627 ** "incremental", write the value of meta[6] in the database
628 ** file. Before writing to meta[6], check that meta[3] indicates
629 ** that this really is an auto-vacuum capable database.
631 static const VdbeOpList setMeta6[] = {
632 { OP_Transaction, 0, 1, 0}, /* 0 */
633 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
634 { OP_If, 1, 0, 0}, /* 2 */
635 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
636 { OP_Integer, 0, 1, 0}, /* 4 */
637 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
640 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
641 sqlite3VdbeChangeP1(v, iAddr, iDb);
642 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
643 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
644 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
645 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
646 sqlite3VdbeUsesBtree(v, iDb);
654 ** PRAGMA [database.]incremental_vacuum(N)
656 ** Do N steps of incremental vacuuming on a database.
658 #ifndef SQLITE_OMIT_AUTOVACUUM
659 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
661 if( sqlite3ReadSchema(pParse) ){
664 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
667 sqlite3BeginWriteOperation(pParse, 0, iDb);
668 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
669 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
670 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
671 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
672 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
673 sqlite3VdbeJumpHere(v, addr);
677 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
679 ** PRAGMA [database.]cache_size
680 ** PRAGMA [database.]cache_size=N
682 ** The first form reports the current local setting for the
683 ** page cache size. The local setting can be different from
684 ** the persistent cache size value that is stored in the database
685 ** file itself. The value returned is the maximum number of
686 ** pages in the page cache. The second form sets the local
687 ** page cache size value. It does not change the persistent
688 ** cache size stored on the disk so the cache size will revert
689 ** to its default value when the database is closed and reopened.
690 ** N should be a positive integer.
692 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
693 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
694 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
696 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
698 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
699 pDb->pSchema->cache_size = size;
700 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
706 ** PRAGMA temp_store = "default"|"memory"|"file"
708 ** Return or set the local value of the temp_store flag. Changing
709 ** the local value does not make changes to the disk file and the default
710 ** value will be restored the next time the database is opened.
712 ** Note that it is possible for the library compile-time options to
713 ** override this setting
715 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
717 returnSingleInt(pParse, "temp_store", db->temp_store);
719 changeTempStorage(pParse, zRight);
724 ** PRAGMA temp_store_directory
725 ** PRAGMA temp_store_directory = ""|"directory_name"
727 ** Return or set the local value of the temp_store_directory flag. Changing
728 ** the value sets a specific directory to be used for temporary files.
729 ** Setting to a null string reverts to the default temporary directory search.
730 ** If temporary directory is changed, then invalidateTempStorage.
733 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
735 if( sqlite3_temp_directory ){
736 sqlite3VdbeSetNumCols(v, 1);
737 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
738 "temp_store_directory", SQLITE_STATIC);
739 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
740 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
743 #ifndef SQLITE_OMIT_WSD
747 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
748 if( rc!=SQLITE_OK || res==0 ){
749 sqlite3ErrorMsg(pParse, "not a writable directory");
753 if( SQLITE_TEMP_STORE==0
754 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
755 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
757 invalidateTempStorage(pParse);
759 sqlite3_free(sqlite3_temp_directory);
761 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
763 sqlite3_temp_directory = 0;
765 #endif /* SQLITE_OMIT_WSD */
769 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
770 # if defined(__APPLE__)
771 # define SQLITE_ENABLE_LOCKING_STYLE 1
773 # define SQLITE_ENABLE_LOCKING_STYLE 0
776 #if SQLITE_ENABLE_LOCKING_STYLE
778 ** PRAGMA [database.]lock_proxy_file
779 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
781 ** Return or set the value of the lock_proxy_file flag. Changing
782 ** the value sets a specific file to be used for database access locks.
785 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
787 Pager *pPager = sqlite3BtreePager(pDb->pBt);
788 char *proxy_file_path = NULL;
789 sqlite3_file *pFile = sqlite3PagerFile(pPager);
790 sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
793 if( proxy_file_path ){
794 sqlite3VdbeSetNumCols(v, 1);
795 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
796 "lock_proxy_file", SQLITE_STATIC);
797 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
798 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
801 Pager *pPager = sqlite3BtreePager(pDb->pBt);
802 sqlite3_file *pFile = sqlite3PagerFile(pPager);
805 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
808 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
811 if( res!=SQLITE_OK ){
812 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
817 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
820 ** PRAGMA [database.]synchronous
821 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
823 ** Return or set the local value of the synchronous flag. Changing
824 ** the local value does not make changes to the disk file and the
825 ** default value will be restored the next time the database is
828 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
829 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
831 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
833 if( !db->autoCommit ){
834 sqlite3ErrorMsg(pParse,
835 "Safety level may not be changed inside a transaction");
837 pDb->safety_level = getSafetyLevel(zRight)+1;
841 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
843 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
844 if( flagPragma(pParse, zLeft, zRight) ){
845 /* The flagPragma() subroutine also generates any necessary code
846 ** there is nothing more to do here */
848 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
850 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
852 ** PRAGMA table_info(<table>)
854 ** Return a single row for each column of the named table. The columns of
855 ** the returned data set are:
857 ** cid: Column id (numbered from left to right, starting at 0)
859 ** type: Column declaration type.
860 ** notnull: True if 'NOT NULL' is part of column declaration
861 ** dflt_value: The default value for the column, if any.
863 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
865 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
866 pTab = sqlite3FindTable(db, zRight, zDb);
871 sqlite3VdbeSetNumCols(v, 6);
873 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
874 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
875 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
876 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
877 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
878 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
879 sqlite3ViewGetColumnNames(pParse, pTab);
880 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
881 if( IsHiddenColumn(pCol) ){
885 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
886 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
887 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
888 pCol->zType ? pCol->zType : "", 0);
889 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
891 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
893 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
895 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
896 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
901 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
904 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
905 pIdx = sqlite3FindIndex(db, zRight, zDb);
909 sqlite3VdbeSetNumCols(v, 3);
911 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
912 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
913 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
914 for(i=0; i<pIdx->nColumn; i++){
915 int cnum = pIdx->aiColumn[i];
916 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
917 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
918 assert( pTab->nCol>cnum );
919 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
920 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
925 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
928 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
929 pTab = sqlite3FindTable(db, zRight, zDb);
931 v = sqlite3GetVdbe(pParse);
935 sqlite3VdbeSetNumCols(v, 3);
937 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
938 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
939 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
941 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
942 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
943 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
944 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
952 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
954 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
955 sqlite3VdbeSetNumCols(v, 3);
957 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
958 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
959 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
960 for(i=0; i<db->nDb; i++){
961 if( db->aDb[i].pBt==0 ) continue;
962 assert( db->aDb[i].zName!=0 );
963 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
964 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
965 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
966 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
967 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
971 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
974 sqlite3VdbeSetNumCols(v, 2);
976 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
977 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
978 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
979 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
980 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
981 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
982 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
985 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
987 #ifndef SQLITE_OMIT_FOREIGN_KEY
988 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
991 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
992 pTab = sqlite3FindTable(db, zRight, zDb);
994 v = sqlite3GetVdbe(pParse);
998 sqlite3VdbeSetNumCols(v, 8);
1000 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1001 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1002 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1003 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1004 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1005 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1006 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1007 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
1010 for(j=0; j<pFK->nCol; j++){
1011 char *zCol = pFK->aCol[j].zCol;
1012 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1013 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
1014 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1015 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1016 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1017 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
1018 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
1019 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
1020 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1021 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1022 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1023 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
1026 pFK = pFK->pNextFrom;
1031 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1034 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
1036 if( getBoolean(zRight) ){
1037 sqlite3ParserTrace(stderr, "parser: ");
1039 sqlite3ParserTrace(0, 0);
1045 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1046 ** used will be case sensitive or not depending on the RHS.
1048 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
1050 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
1054 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1055 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1058 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
1059 /* Pragma "quick_check" is an experimental reduced version of
1060 ** integrity_check designed to detect most database corruption
1061 ** without most of the overhead of a full integrity-check.
1063 if( sqlite3StrICmp(zLeft, "integrity_check")==0
1064 || sqlite3StrICmp(zLeft, "quick_check")==0
1066 int i, j, addr, mxErr;
1068 /* Code that appears at the end of the integrity check. If no error
1069 ** messages have been generated, output OK. Otherwise output the
1072 static const VdbeOpList endCode[] = {
1073 { OP_AddImm, 1, 0, 0}, /* 0 */
1074 { OP_IfNeg, 1, 0, 0}, /* 1 */
1075 { OP_String8, 0, 3, 0}, /* 2 */
1076 { OP_ResultRow, 3, 1, 0},
1079 int isQuick = (zLeft[0]=='q');
1081 /* Initialize the VDBE program */
1082 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
1084 sqlite3VdbeSetNumCols(v, 1);
1085 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
1087 /* Set the maximum error count */
1088 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1090 sqlite3GetInt32(zRight, &mxErr);
1092 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1095 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
1097 /* Do an integrity check on each database file */
1098 for(i=0; i<db->nDb; i++){
1103 if( OMIT_TEMPDB && i==1 ) continue;
1105 sqlite3CodeVerifySchema(pParse, i);
1106 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
1107 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1108 sqlite3VdbeJumpHere(v, addr);
1110 /* Do an integrity check of the B-Tree
1112 ** Begin by filling registers 2, 3, ... with the root pages numbers
1113 ** for all tables and indices in the database.
1115 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1116 pTbls = &db->aDb[i].pSchema->tblHash;
1117 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1118 Table *pTab = sqliteHashData(x);
1120 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
1122 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1123 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
1128 /* Make sure sufficient number of registers have been allocated */
1129 if( pParse->nMem < cnt+4 ){
1130 pParse->nMem = cnt+4;
1133 /* Do the b-tree integrity checks */
1134 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
1135 sqlite3VdbeChangeP5(v, (u8)i);
1136 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1137 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
1138 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
1140 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
1141 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
1142 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
1143 sqlite3VdbeJumpHere(v, addr);
1145 /* Make sure all the indices are constructed correctly.
1147 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
1148 Table *pTab = sqliteHashData(x);
1152 if( pTab->pIndex==0 ) continue;
1153 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
1154 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1155 sqlite3VdbeJumpHere(v, addr);
1156 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
1157 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
1158 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
1159 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
1160 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1163 static const VdbeOpList idxErr[] = {
1164 { OP_AddImm, 1, -1, 0},
1165 { OP_String8, 0, 3, 0}, /* 1 */
1166 { OP_Rowid, 1, 4, 0},
1167 { OP_String8, 0, 5, 0}, /* 3 */
1168 { OP_String8, 0, 6, 0}, /* 4 */
1169 { OP_Concat, 4, 3, 3},
1170 { OP_Concat, 5, 3, 3},
1171 { OP_Concat, 6, 3, 3},
1172 { OP_ResultRow, 3, 1, 0},
1173 { OP_IfPos, 1, 0, 0}, /* 9 */
1174 { OP_Halt, 0, 0, 0},
1176 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
1177 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
1178 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
1179 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1180 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
1181 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
1182 sqlite3VdbeJumpHere(v, addr+9);
1183 sqlite3VdbeJumpHere(v, jmp2);
1185 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
1186 sqlite3VdbeJumpHere(v, loopTop);
1187 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1188 static const VdbeOpList cntIdx[] = {
1189 { OP_Integer, 0, 3, 0},
1190 { OP_Rewind, 0, 0, 0}, /* 1 */
1191 { OP_AddImm, 3, 1, 0},
1192 { OP_Next, 0, 0, 0}, /* 3 */
1193 { OP_Eq, 2, 0, 3}, /* 4 */
1194 { OP_AddImm, 1, -1, 0},
1195 { OP_String8, 0, 2, 0}, /* 6 */
1196 { OP_String8, 0, 3, 0}, /* 7 */
1197 { OP_Concat, 3, 2, 2},
1198 { OP_ResultRow, 2, 1, 0},
1200 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
1201 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1202 sqlite3VdbeJumpHere(v, addr);
1203 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
1204 sqlite3VdbeChangeP1(v, addr+1, j+2);
1205 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1206 sqlite3VdbeChangeP1(v, addr+3, j+2);
1207 sqlite3VdbeChangeP2(v, addr+3, addr+2);
1208 sqlite3VdbeJumpHere(v, addr+4);
1209 sqlite3VdbeChangeP4(v, addr+6,
1210 "wrong # of entries in index ", P4_STATIC);
1211 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
1215 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
1216 sqlite3VdbeChangeP2(v, addr, -mxErr);
1217 sqlite3VdbeJumpHere(v, addr+1);
1218 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
1220 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1222 #ifndef SQLITE_OMIT_UTF16
1225 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1227 ** In its first form, this pragma returns the encoding of the main
1228 ** database. If the database is not initialized, it is initialized now.
1230 ** The second form of this pragma is a no-op if the main database file
1231 ** has not already been initialized. In this case it sets the default
1232 ** encoding that will be used for the main database file if a new file
1233 ** is created. If an existing main database file is opened, then the
1234 ** default text encoding for the existing database is used.
1236 ** In all cases new databases created using the ATTACH command are
1237 ** created to use the same default text encoding as the main database. If
1238 ** the main database has not been initialized and/or created when ATTACH
1239 ** is executed, this is done before the ATTACH operation.
1241 ** In the second form this pragma sets the text encoding to be used in
1242 ** new database files created using this database handle. It is only
1243 ** useful if invoked immediately after the main database i
1245 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
1246 static const struct EncName {
1250 { "UTF8", SQLITE_UTF8 },
1251 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1252 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1253 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
1254 { "UTF16le", SQLITE_UTF16LE },
1255 { "UTF16be", SQLITE_UTF16BE },
1256 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1257 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
1260 const struct EncName *pEnc;
1261 if( !zRight ){ /* "PRAGMA encoding" */
1262 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
1263 sqlite3VdbeSetNumCols(v, 1);
1264 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
1265 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
1266 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1267 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1268 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1269 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
1270 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1271 }else{ /* "PRAGMA encoding = XXX" */
1272 /* Only change the value of sqlite.enc if the database handle is not
1273 ** initialized. If the main database exists, the new sqlite.enc value
1274 ** will be overwritten when the schema is next loaded. If it does not
1275 ** already exists, it will be created to use the new encoding value.
1278 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1279 DbHasProperty(db, 0, DB_Empty)
1281 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1282 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
1283 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
1288 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
1293 #endif /* SQLITE_OMIT_UTF16 */
1295 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
1297 ** PRAGMA [database.]schema_version
1298 ** PRAGMA [database.]schema_version = <integer>
1300 ** PRAGMA [database.]user_version
1301 ** PRAGMA [database.]user_version = <integer>
1303 ** The pragma's schema_version and user_version are used to set or get
1304 ** the value of the schema-version and user-version, respectively. Both
1305 ** the schema-version and the user-version are 32-bit signed integers
1306 ** stored in the database header.
1308 ** The schema-cookie is usually only manipulated internally by SQLite. It
1309 ** is incremented by SQLite whenever the database schema is modified (by
1310 ** creating or dropping a table or index). The schema version is used by
1311 ** SQLite each time a query is executed to ensure that the internal cache
1312 ** of the schema used when compiling the SQL query matches the schema of
1313 ** the database against which the compiled query is actually executed.
1314 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1315 ** the schema-version is potentially dangerous and may lead to program
1316 ** crashes or database corruption. Use with caution!
1318 ** The user-version is not used internally by SQLite. It may be used by
1319 ** applications for any purpose.
1321 if( sqlite3StrICmp(zLeft, "schema_version")==0
1322 || sqlite3StrICmp(zLeft, "user_version")==0
1323 || sqlite3StrICmp(zLeft, "freelist_count")==0
1325 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
1326 sqlite3VdbeUsesBtree(v, iDb);
1329 iCookie = BTREE_FREE_PAGE_COUNT;
1332 iCookie = BTREE_SCHEMA_VERSION;
1335 iCookie = BTREE_USER_VERSION;
1339 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
1340 /* Write the specified cookie value */
1341 static const VdbeOpList setCookie[] = {
1342 { OP_Transaction, 0, 1, 0}, /* 0 */
1343 { OP_Integer, 0, 1, 0}, /* 1 */
1344 { OP_SetCookie, 0, 0, 1}, /* 2 */
1346 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1347 sqlite3VdbeChangeP1(v, addr, iDb);
1348 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
1349 sqlite3VdbeChangeP1(v, addr+2, iDb);
1350 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1352 /* Read the specified cookie value */
1353 static const VdbeOpList readCookie[] = {
1354 { OP_Transaction, 0, 0, 0}, /* 0 */
1355 { OP_ReadCookie, 0, 1, 0}, /* 1 */
1356 { OP_ResultRow, 1, 1, 0}
1358 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1359 sqlite3VdbeChangeP1(v, addr, iDb);
1360 sqlite3VdbeChangeP1(v, addr+1, iDb);
1361 sqlite3VdbeChangeP3(v, addr+1, iCookie);
1362 sqlite3VdbeSetNumCols(v, 1);
1363 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
1366 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
1368 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1370 ** PRAGMA compile_options
1372 ** Return the names of all compile-time options used in this build,
1373 ** one option per row.
1375 if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
1378 sqlite3VdbeSetNumCols(v, 1);
1380 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1381 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1382 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1383 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1386 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1388 #ifndef SQLITE_OMIT_WAL
1390 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
1392 ** Checkpoint the database.
1394 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
1395 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
1396 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1398 if( sqlite3StrICmp(zRight, "full")==0 ){
1399 eMode = SQLITE_CHECKPOINT_FULL;
1400 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1401 eMode = SQLITE_CHECKPOINT_RESTART;
1404 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
1405 sqlite3VdbeSetNumCols(v, 3);
1407 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
1408 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
1409 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
1411 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
1412 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
1416 ** PRAGMA wal_autocheckpoint
1417 ** PRAGMA wal_autocheckpoint = N
1419 ** Configure a database connection to automatically checkpoint a database
1420 ** after accumulating N frames in the log. Or query for the current value
1423 if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
1425 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
1427 returnSingleInt(pParse, "wal_autocheckpoint",
1428 db->xWalCallback==sqlite3WalDefaultHook ?
1429 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
1433 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
1435 ** Report the current state of file logs for all databases
1437 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
1438 static const char *const azLockName[] = {
1439 "unlocked", "shared", "reserved", "pending", "exclusive"
1442 sqlite3VdbeSetNumCols(v, 2);
1444 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1445 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
1446 for(i=0; i<db->nDb; i++){
1449 const char *zState = "unknown";
1451 if( db->aDb[i].zName==0 ) continue;
1452 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
1453 pBt = db->aDb[i].pBt;
1454 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
1456 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
1457 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1458 zState = azLockName[j];
1460 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1461 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
1467 #ifdef SQLITE_HAS_CODEC
1468 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
1469 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
1471 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
1472 sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
1474 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
1475 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
1478 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
1479 h1 += 9*(1&(h1>>6));
1480 h2 += 9*(1&(h2>>6));
1481 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
1483 if( (zLeft[3] & 0xf)==0xb ){
1484 sqlite3_key(db, zKey, i/2);
1486 sqlite3_rekey(db, zKey, i/2);
1490 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
1491 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
1492 #ifdef SQLITE_HAS_CODEC
1493 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
1494 sqlite3_activate_see(&zRight[4]);
1497 #ifdef SQLITE_ENABLE_CEROD
1498 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
1499 sqlite3_activate_cerod(&zRight[6]);
1506 {/* Empty ELSE clause */}
1509 ** Reset the safety level, in case the fullfsync flag or synchronous
1512 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
1513 if( db->autoCommit ){
1514 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
1515 (db->flags&SQLITE_FullFSync)!=0,
1516 (db->flags&SQLITE_CkptFullFSync)!=0);
1520 sqlite3DbFree(db, zLeft);
1521 sqlite3DbFree(db, zRight);
1524 #endif /* SQLITE_OMIT_PRAGMA */