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 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
17 # if defined(__APPLE__)
18 # define SQLITE_ENABLE_LOCKING_STYLE 1
20 # define SQLITE_ENABLE_LOCKING_STYLE 0
24 /***************************************************************************
25 ** The "pragma.h" include file is an automatically generated file that
26 ** that includes the PragType_XXXX macro definitions and the aPragmaName[]
27 ** object. This ensures that the aPragmaName[] table is arranged in
28 ** lexicographical order to facility a binary search of the pragma name.
29 ** Do not edit pragma.h directly. Edit and rerun the script in at
30 ** ../tool/mkpragmatab.tcl. */
34 ** When the 0x10 bit of PRAGMA optimize is set, any ANALYZE commands
35 ** will be run with an analysis_limit set to the lessor of the value of
36 ** the following macro or to the actual analysis_limit if it is non-zero,
37 ** in order to prevent PRAGMA optimize from running for too long.
39 ** The value of 2000 is chosen emperically so that the worst-case run-time
40 ** for PRAGMA optimize does not exceed 100 milliseconds against a variety
41 ** of test databases on a RaspberryPI-4 compiled using -Os and without
42 ** -DSQLITE_DEBUG. Of course, your mileage may vary. For the purpose of
43 ** this paragraph, "worst-case" means that ANALYZE ends up being
44 ** run on every table in the database. The worst case typically only
45 ** happens if PRAGMA optimize is run on a database file for which ANALYZE
46 ** has not been previously run and the 0x10000 flag is included so that
47 ** all tables are analyzed. The usual case for PRAGMA optimize is that
48 ** no ANALYZE commands will be run at all, or if any ANALYZE happens it
49 ** will be against a single table, so that expected timing for PRAGMA
50 ** optimize on a PI-4 is more like 1 millisecond or less with the 0x10000
51 ** flag or less than 100 microseconds without the 0x10000 flag.
53 ** An analysis limit of 2000 is almost always sufficient for the query
54 ** planner to fully characterize an index. The additional accuracy from
55 ** a larger analysis is not usually helpful.
57 #ifndef SQLITE_DEFAULT_OPTIMIZE_LIMIT
58 # define SQLITE_DEFAULT_OPTIMIZE_LIMIT 2000
62 ** Interpret the given string as a safety level. Return 0 for OFF,
63 ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
64 ** unrecognized string argument. The FULL and EXTRA option is disallowed
65 ** if the omitFull parameter it 1.
67 ** Note that the values returned are one less that the values that
68 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
69 ** to support legacy SQL code. The safety level used to be boolean
70 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
72 static u8
getSafetyLevel(const char *z
, int omitFull
, u8 dflt
){
73 /* 123456789 123456789 123 */
74 static const char zText
[] = "onoffalseyestruextrafull";
75 static const u8 iOffset
[] = {0, 1, 2, 4, 9, 12, 15, 20};
76 static const u8 iLength
[] = {2, 2, 3, 5, 3, 4, 5, 4};
77 static const u8 iValue
[] = {1, 0, 0, 0, 1, 1, 3, 2};
78 /* on no off false yes true extra full */
80 if( sqlite3Isdigit(*z
) ){
81 return (u8
)sqlite3Atoi(z
);
83 n
= sqlite3Strlen30(z
);
84 for(i
=0; i
<ArraySize(iLength
); i
++){
85 if( iLength
[i
]==n
&& sqlite3StrNICmp(&zText
[iOffset
[i
]],z
,n
)==0
86 && (!omitFull
|| iValue
[i
]<=1)
95 ** Interpret the given string as a boolean value.
97 u8
sqlite3GetBoolean(const char *z
, u8 dflt
){
98 return getSafetyLevel(z
,1,dflt
)!=0;
101 /* The sqlite3GetBoolean() function is used by other modules but the
102 ** remainder of this file is specific to PRAGMA processing. So omit
103 ** the rest of the file if PRAGMAs are omitted from the build.
105 #if !defined(SQLITE_OMIT_PRAGMA)
108 ** Interpret the given string as a locking mode value.
110 static int getLockingMode(const char *z
){
112 if( 0==sqlite3StrICmp(z
, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE
;
113 if( 0==sqlite3StrICmp(z
, "normal") ) return PAGER_LOCKINGMODE_NORMAL
;
115 return PAGER_LOCKINGMODE_QUERY
;
118 #ifndef SQLITE_OMIT_AUTOVACUUM
120 ** Interpret the given string as an auto-vacuum mode value.
122 ** The following strings, "none", "full" and "incremental" are
123 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
125 static int getAutoVacuum(const char *z
){
127 if( 0==sqlite3StrICmp(z
, "none") ) return BTREE_AUTOVACUUM_NONE
;
128 if( 0==sqlite3StrICmp(z
, "full") ) return BTREE_AUTOVACUUM_FULL
;
129 if( 0==sqlite3StrICmp(z
, "incremental") ) return BTREE_AUTOVACUUM_INCR
;
131 return (u8
)((i
>=0&&i
<=2)?i
:0);
133 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
135 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
137 ** Interpret the given string as a temp db location. Return 1 for file
138 ** backed temporary databases, 2 for the Red-Black tree in memory database
139 ** and 0 to use the compile-time default.
141 static int getTempStore(const char *z
){
142 if( z
[0]>='0' && z
[0]<='2' ){
144 }else if( sqlite3StrICmp(z
, "file")==0 ){
146 }else if( sqlite3StrICmp(z
, "memory")==0 ){
152 #endif /* SQLITE_PAGER_PRAGMAS */
154 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
156 ** Invalidate temp storage, either when the temp storage is changed
157 ** from default, or when 'file' and the temp_store_directory has changed
159 static int invalidateTempStorage(Parse
*pParse
){
160 sqlite3
*db
= pParse
->db
;
161 if( db
->aDb
[1].pBt
!=0 ){
163 || sqlite3BtreeTxnState(db
->aDb
[1].pBt
)!=SQLITE_TXN_NONE
165 sqlite3ErrorMsg(pParse
, "temporary storage cannot be changed "
166 "from within a transaction");
169 sqlite3BtreeClose(db
->aDb
[1].pBt
);
171 sqlite3ResetAllSchemasOfConnection(db
);
175 #endif /* SQLITE_PAGER_PRAGMAS */
177 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
179 ** If the TEMP database is open, close it and mark the database schema
180 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
181 ** or DEFAULT_TEMP_STORE pragmas.
183 static int changeTempStorage(Parse
*pParse
, const char *zStorageType
){
184 int ts
= getTempStore(zStorageType
);
185 sqlite3
*db
= pParse
->db
;
186 if( db
->temp_store
==ts
) return SQLITE_OK
;
187 if( invalidateTempStorage( pParse
) != SQLITE_OK
){
190 db
->temp_store
= (u8
)ts
;
193 #endif /* SQLITE_PAGER_PRAGMAS */
196 ** Set result column names for a pragma.
198 static void setPragmaResultColumnNames(
199 Vdbe
*v
, /* The query under construction */
200 const PragmaName
*pPragma
/* The pragma */
202 u8 n
= pPragma
->nPragCName
;
203 sqlite3VdbeSetNumCols(v
, n
==0 ? 1 : n
);
205 sqlite3VdbeSetColName(v
, 0, COLNAME_NAME
, pPragma
->zName
, SQLITE_STATIC
);
208 for(i
=0, j
=pPragma
->iPragCName
; i
<n
; i
++, j
++){
209 sqlite3VdbeSetColName(v
, i
, COLNAME_NAME
, pragCName
[j
], SQLITE_STATIC
);
215 ** Generate code to return a single integer value.
217 static void returnSingleInt(Vdbe
*v
, i64 value
){
218 sqlite3VdbeAddOp4Dup8(v
, OP_Int64
, 0, 1, 0, (const u8
*)&value
, P4_INT64
);
219 sqlite3VdbeAddOp2(v
, OP_ResultRow
, 1, 1);
223 ** Generate code to return a single text value.
225 static void returnSingleText(
226 Vdbe
*v
, /* Prepared statement under construction */
227 const char *zValue
/* Value to be returned */
230 sqlite3VdbeLoadString(v
, 1, (const char*)zValue
);
231 sqlite3VdbeAddOp2(v
, OP_ResultRow
, 1, 1);
237 ** Set the safety_level and pager flags for pager iDb. Or if iDb<0
238 ** set these values for all pagers.
240 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
241 static void setAllPagerFlags(sqlite3
*db
){
242 if( db
->autoCommit
){
245 assert( SQLITE_FullFSync
==PAGER_FULLFSYNC
);
246 assert( SQLITE_CkptFullFSync
==PAGER_CKPT_FULLFSYNC
);
247 assert( SQLITE_CacheSpill
==PAGER_CACHESPILL
);
248 assert( (PAGER_FULLFSYNC
| PAGER_CKPT_FULLFSYNC
| PAGER_CACHESPILL
)
249 == PAGER_FLAGS_MASK
);
250 assert( (pDb
->safety_level
& PAGER_SYNCHRONOUS_MASK
)==pDb
->safety_level
);
253 sqlite3BtreeSetPagerFlags(pDb
->pBt
,
254 pDb
->safety_level
| (db
->flags
& PAGER_FLAGS_MASK
) );
261 # define setAllPagerFlags(X) /* no-op */
266 ** Return a human-readable name for a constraint resolution action.
268 #ifndef SQLITE_OMIT_FOREIGN_KEY
269 static const char *actionName(u8 action
){
272 case OE_SetNull
: zName
= "SET NULL"; break;
273 case OE_SetDflt
: zName
= "SET DEFAULT"; break;
274 case OE_Cascade
: zName
= "CASCADE"; break;
275 case OE_Restrict
: zName
= "RESTRICT"; break;
276 default: zName
= "NO ACTION";
277 assert( action
==OE_None
); break;
285 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
286 ** defined in pager.h. This function returns the associated lowercase
287 ** journal-mode name.
289 const char *sqlite3JournalModename(int eMode
){
290 static char * const azModeName
[] = {
291 "delete", "persist", "off", "truncate", "memory"
292 #ifndef SQLITE_OMIT_WAL
296 assert( PAGER_JOURNALMODE_DELETE
==0 );
297 assert( PAGER_JOURNALMODE_PERSIST
==1 );
298 assert( PAGER_JOURNALMODE_OFF
==2 );
299 assert( PAGER_JOURNALMODE_TRUNCATE
==3 );
300 assert( PAGER_JOURNALMODE_MEMORY
==4 );
301 assert( PAGER_JOURNALMODE_WAL
==5 );
302 assert( eMode
>=0 && eMode
<=ArraySize(azModeName
) );
304 if( eMode
==ArraySize(azModeName
) ) return 0;
305 return azModeName
[eMode
];
309 ** Locate a pragma in the aPragmaName[] array.
311 static const PragmaName
*pragmaLocate(const char *zName
){
312 int upr
, lwr
, mid
= 0, rc
;
314 upr
= ArraySize(aPragmaName
)-1;
317 rc
= sqlite3_stricmp(zName
, aPragmaName
[mid
].zName
);
325 return lwr
>upr
? 0 : &aPragmaName
[mid
];
329 ** Create zero or more entries in the output for the SQL functions
330 ** defined by FuncDef p.
332 static void pragmaFunclistLine(
333 Vdbe
*v
, /* The prepared statement being created */
334 FuncDef
*p
, /* A particular function definition */
335 int isBuiltin
, /* True if this is a built-in function */
336 int showInternFuncs
/* True if showing internal functions */
339 SQLITE_DETERMINISTIC
|
345 if( showInternFuncs
) mask
= 0xffffffff;
346 for(; p
; p
=p
->pNext
){
348 static const char *azEnc
[] = { 0, "utf8", "utf16le", "utf16be" };
350 assert( SQLITE_FUNC_ENCMASK
==0x3 );
351 assert( strcmp(azEnc
[SQLITE_UTF8
],"utf8")==0 );
352 assert( strcmp(azEnc
[SQLITE_UTF16LE
],"utf16le")==0 );
353 assert( strcmp(azEnc
[SQLITE_UTF16BE
],"utf16be")==0 );
355 if( p
->xSFunc
==0 ) continue;
356 if( (p
->funcFlags
& SQLITE_FUNC_INTERNAL
)!=0
357 && showInternFuncs
==0
363 }else if( p
->xFinalize
!=0 ){
368 sqlite3VdbeMultiLoad(v
, 1, "sissii",
370 zType
, azEnc
[p
->funcFlags
&SQLITE_FUNC_ENCMASK
],
372 (p
->funcFlags
& mask
) ^ SQLITE_INNOCUOUS
379 ** Helper subroutine for PRAGMA integrity_check:
381 ** Generate code to output a single-column result row with a value of the
382 ** string held in register 3. Decrement the result count in register 1
383 ** and halt if the maximum number of result rows have been issued.
385 static int integrityCheckResultRow(Vdbe
*v
){
387 sqlite3VdbeAddOp2(v
, OP_ResultRow
, 3, 1);
388 addr
= sqlite3VdbeAddOp3(v
, OP_IfPos
, 1, sqlite3VdbeCurrentAddr(v
)+2, 1);
390 sqlite3VdbeAddOp0(v
, OP_Halt
);
395 ** Process a pragma statement.
397 ** Pragmas are of this form:
399 ** PRAGMA [schema.]id [= value]
401 ** The identifier might also be a string. The value is a string, and
402 ** identifier, or a number. If minusFlag is true, then the value is
403 ** a number that was preceded by a minus sign.
405 ** If the left side is "database.id" then pId1 is the database name
406 ** and pId2 is the id. If the left side is just "id" then pId1 is the
407 ** id and pId2 is any empty string.
411 Token
*pId1
, /* First part of [schema.]id field */
412 Token
*pId2
, /* Second part of [schema.]id field, or NULL */
413 Token
*pValue
, /* Token for <value>, or NULL */
414 int minusFlag
/* True if a '-' sign preceded <value> */
416 char *zLeft
= 0; /* Nul-terminated UTF-8 string <id> */
417 char *zRight
= 0; /* Nul-terminated UTF-8 string <value>, or NULL */
418 const char *zDb
= 0; /* The database name */
419 Token
*pId
; /* Pointer to <id> token */
420 char *aFcntl
[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
421 int iDb
; /* Database index for <database> */
422 int rc
; /* return value form SQLITE_FCNTL_PRAGMA */
423 sqlite3
*db
= pParse
->db
; /* The database connection */
424 Db
*pDb
; /* The specific database being pragmaed */
425 Vdbe
*v
= sqlite3GetVdbe(pParse
); /* Prepared statement */
426 const PragmaName
*pPragma
; /* The pragma */
429 sqlite3VdbeRunOnlyOnce(v
);
432 /* Interpret the [schema.] part of the pragma statement. iDb is the
433 ** index of the database this pragma is being applied to in db.aDb[]. */
434 iDb
= sqlite3TwoPartName(pParse
, pId1
, pId2
, &pId
);
438 /* If the temp database has been explicitly named as part of the
439 ** pragma, make sure it is open.
441 if( iDb
==1 && sqlite3OpenTempDatabase(pParse
) ){
445 zLeft
= sqlite3NameFromToken(db
, pId
);
448 zRight
= sqlite3MPrintf(db
, "-%T", pValue
);
450 zRight
= sqlite3NameFromToken(db
, pValue
);
454 zDb
= pId2
->n
>0 ? pDb
->zDbSName
: 0;
455 if( sqlite3AuthCheck(pParse
, SQLITE_PRAGMA
, zLeft
, zRight
, zDb
) ){
459 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
460 ** connection. If it returns SQLITE_OK, then assume that the VFS
461 ** handled the pragma and generate a no-op prepared statement.
463 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
464 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
465 ** object corresponding to the database file to which the pragma
468 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
469 ** file control is an array of pointers to strings (char**) in which the
470 ** second element of the array is the name of the pragma and the third
471 ** element is the argument to the pragma or NULL if the pragma has no
478 db
->busyHandler
.nBusy
= 0;
479 rc
= sqlite3_file_control(db
, zDb
, SQLITE_FCNTL_PRAGMA
, (void*)aFcntl
);
481 sqlite3VdbeSetNumCols(v
, 1);
482 sqlite3VdbeSetColName(v
, 0, COLNAME_NAME
, aFcntl
[0], SQLITE_TRANSIENT
);
483 returnSingleText(v
, aFcntl
[0]);
484 sqlite3_free(aFcntl
[0]);
487 if( rc
!=SQLITE_NOTFOUND
){
489 sqlite3ErrorMsg(pParse
, "%s", aFcntl
[0]);
490 sqlite3_free(aFcntl
[0]);
497 /* Locate the pragma in the lookup table */
498 pPragma
= pragmaLocate(zLeft
);
500 /* IMP: R-43042-22504 No error messages are generated if an
501 ** unknown pragma is issued. */
505 /* Make sure the database schema is loaded if the pragma requires that */
506 if( (pPragma
->mPragFlg
& PragFlg_NeedSchema
)!=0 ){
507 if( sqlite3ReadSchema(pParse
) ) goto pragma_out
;
510 /* Register the result column names for pragmas that return results */
511 if( (pPragma
->mPragFlg
& PragFlg_NoColumns
)==0
512 && ((pPragma
->mPragFlg
& PragFlg_NoColumns1
)==0 || zRight
==0)
514 setPragmaResultColumnNames(v
, pPragma
);
517 /* Jump to the appropriate pragma handler */
518 switch( pPragma
->ePragTyp
){
520 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
522 ** PRAGMA [schema.]default_cache_size
523 ** PRAGMA [schema.]default_cache_size=N
525 ** The first form reports the current persistent setting for the
526 ** page cache size. The value returned is the maximum number of
527 ** pages in the page cache. The second form sets both the current
528 ** page cache size value and the persistent page cache size value
529 ** stored in the database file.
531 ** Older versions of SQLite would set the default cache size to a
532 ** negative number to indicate synchronous=OFF. These days, synchronous
533 ** is always on by default regardless of the sign of the default cache
534 ** size. But continue to take the absolute value of the default cache
535 ** size of historical compatibility.
537 case PragTyp_DEFAULT_CACHE_SIZE
: {
538 static const int iLn
= VDBE_OFFSET_LINENO(2);
539 static const VdbeOpList getCacheSize
[] = {
540 { OP_Transaction
, 0, 0, 0}, /* 0 */
541 { OP_ReadCookie
, 0, 1, BTREE_DEFAULT_CACHE_SIZE
}, /* 1 */
542 { OP_IfPos
, 1, 8, 0},
543 { OP_Integer
, 0, 2, 0},
544 { OP_Subtract
, 1, 2, 1},
545 { OP_IfPos
, 1, 8, 0},
546 { OP_Integer
, 0, 1, 0}, /* 6 */
548 { OP_ResultRow
, 1, 1, 0},
551 sqlite3VdbeUsesBtree(v
, iDb
);
554 sqlite3VdbeVerifyNoMallocRequired(v
, ArraySize(getCacheSize
));
555 aOp
= sqlite3VdbeAddOpList(v
, ArraySize(getCacheSize
), getCacheSize
, iLn
);
556 if( ONLY_IF_REALLOC_STRESS(aOp
==0) ) break;
559 aOp
[6].p1
= SQLITE_DEFAULT_CACHE_SIZE
;
561 int size
= sqlite3AbsInt32(sqlite3Atoi(zRight
));
562 sqlite3BeginWriteOperation(pParse
, 0, iDb
);
563 sqlite3VdbeAddOp3(v
, OP_SetCookie
, iDb
, BTREE_DEFAULT_CACHE_SIZE
, size
);
564 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
565 pDb
->pSchema
->cache_size
= size
;
566 sqlite3BtreeSetCacheSize(pDb
->pBt
, pDb
->pSchema
->cache_size
);
570 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
572 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
574 ** PRAGMA [schema.]page_size
575 ** PRAGMA [schema.]page_size=N
577 ** The first form reports the current setting for the
578 ** database page size in bytes. The second form sets the
579 ** database page size value. The value can only be set if
580 ** the database has not yet been created.
582 case PragTyp_PAGE_SIZE
: {
583 Btree
*pBt
= pDb
->pBt
;
586 int size
= ALWAYS(pBt
) ? sqlite3BtreeGetPageSize(pBt
) : 0;
587 returnSingleInt(v
, size
);
589 /* Malloc may fail when setting the page-size, as there is an internal
590 ** buffer that the pager module resizes using sqlite3_realloc().
592 db
->nextPagesize
= sqlite3Atoi(zRight
);
593 if( SQLITE_NOMEM
==sqlite3BtreeSetPageSize(pBt
, db
->nextPagesize
,0,0) ){
601 ** PRAGMA [schema.]secure_delete
602 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
604 ** The first form reports the current setting for the
605 ** secure_delete flag. The second form changes the secure_delete
606 ** flag setting and reports the new value.
608 case PragTyp_SECURE_DELETE
: {
609 Btree
*pBt
= pDb
->pBt
;
613 if( sqlite3_stricmp(zRight
, "fast")==0 ){
616 b
= sqlite3GetBoolean(zRight
, 0);
619 if( pId2
->n
==0 && b
>=0 ){
621 for(ii
=0; ii
<db
->nDb
; ii
++){
622 sqlite3BtreeSecureDelete(db
->aDb
[ii
].pBt
, b
);
625 b
= sqlite3BtreeSecureDelete(pBt
, b
);
626 returnSingleInt(v
, b
);
631 ** PRAGMA [schema.]max_page_count
632 ** PRAGMA [schema.]max_page_count=N
634 ** The first form reports the current setting for the
635 ** maximum number of pages in the database file. The
636 ** second form attempts to change this setting. Both
637 ** forms return the current setting.
639 ** The absolute value of N is used. This is undocumented and might
640 ** change. The only purpose is to provide an easy way to test
641 ** the sqlite3AbsInt32() function.
643 ** PRAGMA [schema.]page_count
645 ** Return the number of pages in the specified database.
647 case PragTyp_PAGE_COUNT
: {
650 sqlite3CodeVerifySchema(pParse
, iDb
);
651 iReg
= ++pParse
->nMem
;
652 if( sqlite3Tolower(zLeft
[0])=='p' ){
653 sqlite3VdbeAddOp2(v
, OP_Pagecount
, iDb
, iReg
);
655 if( zRight
&& sqlite3DecOrHexToI64(zRight
,&x
)==0 ){
657 else if( x
>0xfffffffe ) x
= 0xfffffffe;
661 sqlite3VdbeAddOp3(v
, OP_MaxPgcnt
, iDb
, iReg
, (int)x
);
663 sqlite3VdbeAddOp2(v
, OP_ResultRow
, iReg
, 1);
668 ** PRAGMA [schema.]locking_mode
669 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
671 case PragTyp_LOCKING_MODE
: {
672 const char *zRet
= "normal";
673 int eMode
= getLockingMode(zRight
);
675 if( pId2
->n
==0 && eMode
==PAGER_LOCKINGMODE_QUERY
){
676 /* Simple "PRAGMA locking_mode;" statement. This is a query for
677 ** the current default locking mode (which may be different to
678 ** the locking-mode of the main database).
680 eMode
= db
->dfltLockMode
;
684 /* This indicates that no database name was specified as part
685 ** of the PRAGMA command. In this case the locking-mode must be
686 ** set on all attached databases, as well as the main db file.
688 ** Also, the sqlite3.dfltLockMode variable is set so that
689 ** any subsequently attached databases also use the specified
693 assert(pDb
==&db
->aDb
[0]);
694 for(ii
=2; ii
<db
->nDb
; ii
++){
695 pPager
= sqlite3BtreePager(db
->aDb
[ii
].pBt
);
696 sqlite3PagerLockingMode(pPager
, eMode
);
698 db
->dfltLockMode
= (u8
)eMode
;
700 pPager
= sqlite3BtreePager(pDb
->pBt
);
701 eMode
= sqlite3PagerLockingMode(pPager
, eMode
);
704 assert( eMode
==PAGER_LOCKINGMODE_NORMAL
705 || eMode
==PAGER_LOCKINGMODE_EXCLUSIVE
);
706 if( eMode
==PAGER_LOCKINGMODE_EXCLUSIVE
){
709 returnSingleText(v
, zRet
);
714 ** PRAGMA [schema.]journal_mode
715 ** PRAGMA [schema.]journal_mode =
716 ** (delete|persist|off|truncate|memory|wal|off)
718 case PragTyp_JOURNAL_MODE
: {
719 int eMode
; /* One of the PAGER_JOURNALMODE_XXX symbols */
720 int ii
; /* Loop counter */
723 /* If there is no "=MODE" part of the pragma, do a query for the
725 eMode
= PAGER_JOURNALMODE_QUERY
;
728 int n
= sqlite3Strlen30(zRight
);
729 for(eMode
=0; (zMode
= sqlite3JournalModename(eMode
))!=0; eMode
++){
730 if( sqlite3StrNICmp(zRight
, zMode
, n
)==0 ) break;
733 /* If the "=MODE" part does not match any known journal mode,
734 ** then do a query */
735 eMode
= PAGER_JOURNALMODE_QUERY
;
737 if( eMode
==PAGER_JOURNALMODE_OFF
&& (db
->flags
& SQLITE_Defensive
)!=0 ){
738 /* Do not allow journal-mode "OFF" in defensive since the database
739 ** can become corrupted using ordinary SQL when the journal is off */
740 eMode
= PAGER_JOURNALMODE_QUERY
;
743 if( eMode
==PAGER_JOURNALMODE_QUERY
&& pId2
->n
==0 ){
744 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
748 for(ii
=db
->nDb
-1; ii
>=0; ii
--){
749 if( db
->aDb
[ii
].pBt
&& (ii
==iDb
|| pId2
->n
==0) ){
750 sqlite3VdbeUsesBtree(v
, ii
);
751 sqlite3VdbeAddOp3(v
, OP_JournalMode
, ii
, 1, eMode
);
754 sqlite3VdbeAddOp2(v
, OP_ResultRow
, 1, 1);
759 ** PRAGMA [schema.]journal_size_limit
760 ** PRAGMA [schema.]journal_size_limit=N
762 ** Get or set the size limit on rollback journal files.
764 case PragTyp_JOURNAL_SIZE_LIMIT
: {
765 Pager
*pPager
= sqlite3BtreePager(pDb
->pBt
);
768 sqlite3DecOrHexToI64(zRight
, &iLimit
);
769 if( iLimit
<-1 ) iLimit
= -1;
771 iLimit
= sqlite3PagerJournalSizeLimit(pPager
, iLimit
);
772 returnSingleInt(v
, iLimit
);
776 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
779 ** PRAGMA [schema.]auto_vacuum
780 ** PRAGMA [schema.]auto_vacuum=N
782 ** Get or set the value of the database 'auto-vacuum' parameter.
783 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
785 #ifndef SQLITE_OMIT_AUTOVACUUM
786 case PragTyp_AUTO_VACUUM
: {
787 Btree
*pBt
= pDb
->pBt
;
790 returnSingleInt(v
, sqlite3BtreeGetAutoVacuum(pBt
));
792 int eAuto
= getAutoVacuum(zRight
);
793 assert( eAuto
>=0 && eAuto
<=2 );
794 db
->nextAutovac
= (u8
)eAuto
;
795 /* Call SetAutoVacuum() to set initialize the internal auto and
796 ** incr-vacuum flags. This is required in case this connection
797 ** creates the database file. It is important that it is created
798 ** as an auto-vacuum capable db.
800 rc
= sqlite3BtreeSetAutoVacuum(pBt
, eAuto
);
801 if( rc
==SQLITE_OK
&& (eAuto
==1 || eAuto
==2) ){
802 /* When setting the auto_vacuum mode to either "full" or
803 ** "incremental", write the value of meta[6] in the database
804 ** file. Before writing to meta[6], check that meta[3] indicates
805 ** that this really is an auto-vacuum capable database.
807 static const int iLn
= VDBE_OFFSET_LINENO(2);
808 static const VdbeOpList setMeta6
[] = {
809 { OP_Transaction
, 0, 1, 0}, /* 0 */
810 { OP_ReadCookie
, 0, 1, BTREE_LARGEST_ROOT_PAGE
},
811 { OP_If
, 1, 0, 0}, /* 2 */
812 { OP_Halt
, SQLITE_OK
, OE_Abort
, 0}, /* 3 */
813 { OP_SetCookie
, 0, BTREE_INCR_VACUUM
, 0}, /* 4 */
816 int iAddr
= sqlite3VdbeCurrentAddr(v
);
817 sqlite3VdbeVerifyNoMallocRequired(v
, ArraySize(setMeta6
));
818 aOp
= sqlite3VdbeAddOpList(v
, ArraySize(setMeta6
), setMeta6
, iLn
);
819 if( ONLY_IF_REALLOC_STRESS(aOp
==0) ) break;
824 aOp
[4].p3
= eAuto
- 1;
825 sqlite3VdbeUsesBtree(v
, iDb
);
833 ** PRAGMA [schema.]incremental_vacuum(N)
835 ** Do N steps of incremental vacuuming on a database.
837 #ifndef SQLITE_OMIT_AUTOVACUUM
838 case PragTyp_INCREMENTAL_VACUUM
: {
839 int iLimit
= 0, addr
;
840 if( zRight
==0 || !sqlite3GetInt32(zRight
, &iLimit
) || iLimit
<=0 ){
843 sqlite3BeginWriteOperation(pParse
, 0, iDb
);
844 sqlite3VdbeAddOp2(v
, OP_Integer
, iLimit
, 1);
845 addr
= sqlite3VdbeAddOp1(v
, OP_IncrVacuum
, iDb
); VdbeCoverage(v
);
846 sqlite3VdbeAddOp1(v
, OP_ResultRow
, 1);
847 sqlite3VdbeAddOp2(v
, OP_AddImm
, 1, -1);
848 sqlite3VdbeAddOp2(v
, OP_IfPos
, 1, addr
); VdbeCoverage(v
);
849 sqlite3VdbeJumpHere(v
, addr
);
854 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
856 ** PRAGMA [schema.]cache_size
857 ** PRAGMA [schema.]cache_size=N
859 ** The first form reports the current local setting for the
860 ** page cache size. The second form sets the local
861 ** page cache size value. If N is positive then that is the
862 ** number of pages in the cache. If N is negative, then the
863 ** number of pages is adjusted so that the cache uses -N kibibytes
866 case PragTyp_CACHE_SIZE
: {
867 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
869 returnSingleInt(v
, pDb
->pSchema
->cache_size
);
871 int size
= sqlite3Atoi(zRight
);
872 pDb
->pSchema
->cache_size
= size
;
873 sqlite3BtreeSetCacheSize(pDb
->pBt
, pDb
->pSchema
->cache_size
);
879 ** PRAGMA [schema.]cache_spill
880 ** PRAGMA cache_spill=BOOLEAN
881 ** PRAGMA [schema.]cache_spill=N
883 ** The first form reports the current local setting for the
884 ** page cache spill size. The second form turns cache spill on
885 ** or off. When turning cache spill on, the size is set to the
886 ** current cache_size. The third form sets a spill size that
887 ** may be different form the cache size.
888 ** If N is positive then that is the
889 ** number of pages in the cache. If N is negative, then the
890 ** number of pages is adjusted so that the cache uses -N kibibytes
893 ** If the number of cache_spill pages is less then the number of
894 ** cache_size pages, no spilling occurs until the page count exceeds
895 ** the number of cache_size pages.
897 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
898 ** not just the schema specified.
900 case PragTyp_CACHE_SPILL
: {
901 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
904 (db
->flags
& SQLITE_CacheSpill
)==0 ? 0 :
905 sqlite3BtreeSetSpillSize(pDb
->pBt
,0));
908 if( sqlite3GetInt32(zRight
, &size
) ){
909 sqlite3BtreeSetSpillSize(pDb
->pBt
, size
);
911 if( sqlite3GetBoolean(zRight
, size
!=0) ){
912 db
->flags
|= SQLITE_CacheSpill
;
914 db
->flags
&= ~(u64
)SQLITE_CacheSpill
;
916 setAllPagerFlags(db
);
922 ** PRAGMA [schema.]mmap_size(N)
924 ** Used to set mapping size limit. The mapping size limit is
925 ** used to limit the aggregate size of all memory mapped regions of the
926 ** database file. If this parameter is set to zero, then memory mapping
927 ** is not used at all. If N is negative, then the default memory map
928 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
929 ** The parameter N is measured in bytes.
931 ** This value is advisory. The underlying VFS is free to memory map
932 ** as little or as much as it wants. Except, if N is set to 0 then the
933 ** upper layers will never invoke the xFetch interfaces to the VFS.
935 case PragTyp_MMAP_SIZE
: {
937 #if SQLITE_MAX_MMAP_SIZE>0
938 assert( sqlite3SchemaMutexHeld(db
, iDb
, 0) );
941 sqlite3DecOrHexToI64(zRight
, &sz
);
942 if( sz
<0 ) sz
= sqlite3GlobalConfig
.szMmap
;
943 if( pId2
->n
==0 ) db
->szMmap
= sz
;
944 for(ii
=db
->nDb
-1; ii
>=0; ii
--){
945 if( db
->aDb
[ii
].pBt
&& (ii
==iDb
|| pId2
->n
==0) ){
946 sqlite3BtreeSetMmapLimit(db
->aDb
[ii
].pBt
, sz
);
951 rc
= sqlite3_file_control(db
, zDb
, SQLITE_FCNTL_MMAP_SIZE
, &sz
);
957 returnSingleInt(v
, sz
);
958 }else if( rc
!=SQLITE_NOTFOUND
){
967 ** PRAGMA temp_store = "default"|"memory"|"file"
969 ** Return or set the local value of the temp_store flag. Changing
970 ** the local value does not make changes to the disk file and the default
971 ** value will be restored the next time the database is opened.
973 ** Note that it is possible for the library compile-time options to
974 ** override this setting
976 case PragTyp_TEMP_STORE
: {
978 returnSingleInt(v
, db
->temp_store
);
980 changeTempStorage(pParse
, zRight
);
986 ** PRAGMA temp_store_directory
987 ** PRAGMA temp_store_directory = ""|"directory_name"
989 ** Return or set the local value of the temp_store_directory flag. Changing
990 ** the value sets a specific directory to be used for temporary files.
991 ** Setting to a null string reverts to the default temporary directory search.
992 ** If temporary directory is changed, then invalidateTempStorage.
995 case PragTyp_TEMP_STORE_DIRECTORY
: {
996 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR
));
998 returnSingleText(v
, sqlite3_temp_directory
);
1000 #ifndef SQLITE_OMIT_WSD
1003 rc
= sqlite3OsAccess(db
->pVfs
, zRight
, SQLITE_ACCESS_READWRITE
, &res
);
1004 if( rc
!=SQLITE_OK
|| res
==0 ){
1005 sqlite3ErrorMsg(pParse
, "not a writable directory");
1006 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR
));
1010 if( SQLITE_TEMP_STORE
==0
1011 || (SQLITE_TEMP_STORE
==1 && db
->temp_store
<=1)
1012 || (SQLITE_TEMP_STORE
==2 && db
->temp_store
==1)
1014 invalidateTempStorage(pParse
);
1016 sqlite3_free(sqlite3_temp_directory
);
1018 sqlite3_temp_directory
= sqlite3_mprintf("%s", zRight
);
1020 sqlite3_temp_directory
= 0;
1022 #endif /* SQLITE_OMIT_WSD */
1024 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR
));
1030 ** PRAGMA data_store_directory
1031 ** PRAGMA data_store_directory = ""|"directory_name"
1033 ** Return or set the local value of the data_store_directory flag. Changing
1034 ** the value sets a specific directory to be used for database files that
1035 ** were specified with a relative pathname. Setting to a null string reverts
1036 ** to the default database directory, which for database files specified with
1037 ** a relative path will probably be based on the current directory for the
1038 ** process. Database file specified with an absolute path are not impacted
1039 ** by this setting, regardless of its value.
1042 case PragTyp_DATA_STORE_DIRECTORY
: {
1043 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR
));
1045 returnSingleText(v
, sqlite3_data_directory
);
1047 #ifndef SQLITE_OMIT_WSD
1050 rc
= sqlite3OsAccess(db
->pVfs
, zRight
, SQLITE_ACCESS_READWRITE
, &res
);
1051 if( rc
!=SQLITE_OK
|| res
==0 ){
1052 sqlite3ErrorMsg(pParse
, "not a writable directory");
1053 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR
));
1057 sqlite3_free(sqlite3_data_directory
);
1059 sqlite3_data_directory
= sqlite3_mprintf("%s", zRight
);
1061 sqlite3_data_directory
= 0;
1063 #endif /* SQLITE_OMIT_WSD */
1065 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR
));
1070 #if SQLITE_ENABLE_LOCKING_STYLE
1072 ** PRAGMA [schema.]lock_proxy_file
1073 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
1075 ** Return or set the value of the lock_proxy_file flag. Changing
1076 ** the value sets a specific file to be used for database access locks.
1079 case PragTyp_LOCK_PROXY_FILE
: {
1081 Pager
*pPager
= sqlite3BtreePager(pDb
->pBt
);
1082 char *proxy_file_path
= NULL
;
1083 sqlite3_file
*pFile
= sqlite3PagerFile(pPager
);
1084 sqlite3OsFileControlHint(pFile
, SQLITE_GET_LOCKPROXYFILE
,
1086 returnSingleText(v
, proxy_file_path
);
1088 Pager
*pPager
= sqlite3BtreePager(pDb
->pBt
);
1089 sqlite3_file
*pFile
= sqlite3PagerFile(pPager
);
1092 res
=sqlite3OsFileControl(pFile
, SQLITE_SET_LOCKPROXYFILE
,
1095 res
=sqlite3OsFileControl(pFile
, SQLITE_SET_LOCKPROXYFILE
,
1098 if( res
!=SQLITE_OK
){
1099 sqlite3ErrorMsg(pParse
, "failed to set lock proxy file");
1105 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
1108 ** PRAGMA [schema.]synchronous
1109 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
1111 ** Return or set the local value of the synchronous flag. Changing
1112 ** the local value does not make changes to the disk file and the
1113 ** default value will be restored the next time the database is
1116 case PragTyp_SYNCHRONOUS
: {
1118 returnSingleInt(v
, pDb
->safety_level
-1);
1120 if( !db
->autoCommit
){
1121 sqlite3ErrorMsg(pParse
,
1122 "Safety level may not be changed inside a transaction");
1124 int iLevel
= (getSafetyLevel(zRight
,0,1)+1) & PAGER_SYNCHRONOUS_MASK
;
1125 if( iLevel
==0 ) iLevel
= 1;
1126 pDb
->safety_level
= iLevel
;
1128 setAllPagerFlags(db
);
1133 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
1135 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
1136 case PragTyp_FLAG
: {
1138 setPragmaResultColumnNames(v
, pPragma
);
1139 returnSingleInt(v
, (db
->flags
& pPragma
->iArg
)!=0 );
1141 u64 mask
= pPragma
->iArg
; /* Mask of bits to set or clear. */
1142 if( db
->autoCommit
==0 ){
1143 /* Foreign key support may not be enabled or disabled while not
1144 ** in auto-commit mode. */
1145 mask
&= ~(SQLITE_ForeignKeys
);
1147 #if SQLITE_USER_AUTHENTICATION
1148 if( db
->auth
.authLevel
==UAUTH_User
){
1149 /* Do not allow non-admin users to modify the schema arbitrarily */
1150 mask
&= ~(SQLITE_WriteSchema
);
1154 if( sqlite3GetBoolean(zRight
, 0) ){
1155 if( (mask
& SQLITE_WriteSchema
)==0
1156 || (db
->flags
& SQLITE_Defensive
)==0
1162 if( mask
==SQLITE_DeferFKs
) db
->nDeferredImmCons
= 0;
1163 if( (mask
& SQLITE_WriteSchema
)!=0
1164 && sqlite3_stricmp(zRight
, "reset")==0
1166 /* IMP: R-60817-01178 If the argument is "RESET" then schema
1167 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
1168 ** in addition, the schema is reloaded. */
1169 sqlite3ResetAllSchemasOfConnection(db
);
1173 /* Many of the flag-pragmas modify the code generated by the SQL
1174 ** compiler (eg. count_changes). So add an opcode to expire all
1175 ** compiled SQL statements after modifying a pragma value.
1177 sqlite3VdbeAddOp0(v
, OP_Expire
);
1178 setAllPagerFlags(db
);
1182 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
1184 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
1186 ** PRAGMA table_info(<table>)
1188 ** Return a single row for each column of the named table. The columns of
1189 ** the returned data set are:
1191 ** cid: Column id (numbered from left to right, starting at 0)
1192 ** name: Column name
1193 ** type: Column declaration type.
1194 ** notnull: True if 'NOT NULL' is part of column declaration
1195 ** dflt_value: The default value for the column, if any.
1196 ** pk: Non-zero for PK fields.
1198 case PragTyp_TABLE_INFO
: if( zRight
){
1200 sqlite3CodeVerifyNamedSchema(pParse
, zDb
);
1201 pTab
= sqlite3LocateTable(pParse
, LOCATE_NOERR
, zRight
, zDb
);
1206 Index
*pPk
= sqlite3PrimaryKeyIndex(pTab
);
1208 sqlite3ViewGetColumnNames(pParse
, pTab
);
1209 for(i
=0, pCol
=pTab
->aCol
; i
<pTab
->nCol
; i
++, pCol
++){
1211 const Expr
*pColExpr
;
1212 if( pCol
->colFlags
& COLFLAG_NOINSERT
){
1213 if( pPragma
->iArg
==0 ){
1217 if( pCol
->colFlags
& COLFLAG_VIRTUAL
){
1218 isHidden
= 2; /* GENERATED ALWAYS AS ... VIRTUAL */
1219 }else if( pCol
->colFlags
& COLFLAG_STORED
){
1220 isHidden
= 3; /* GENERATED ALWAYS AS ... STORED */
1221 }else{ assert( pCol
->colFlags
& COLFLAG_HIDDEN
);
1222 isHidden
= 1; /* HIDDEN */
1225 if( (pCol
->colFlags
& COLFLAG_PRIMKEY
)==0 ){
1230 for(k
=1; k
<=pTab
->nCol
&& pPk
->aiColumn
[k
-1]!=i
; k
++){}
1232 pColExpr
= sqlite3ColumnExpr(pTab
,pCol
);
1233 assert( pColExpr
==0 || pColExpr
->op
==TK_SPAN
|| isHidden
>=2 );
1234 assert( pColExpr
==0 || !ExprHasProperty(pColExpr
, EP_IntValue
)
1236 sqlite3VdbeMultiLoad(v
, 1, pPragma
->iArg
? "issisii" : "issisi",
1239 sqlite3ColumnType(pCol
,""),
1240 pCol
->notNull
? 1 : 0,
1241 (isHidden
>=2 || pColExpr
==0) ? 0 : pColExpr
->u
.zToken
,
1250 ** PRAGMA table_list
1252 ** Return a single row for each table, virtual table, or view in the
1255 ** schema: Name of attached database hold this table
1256 ** name: Name of the table itself
1257 ** type: "table", "view", "virtual", "shadow"
1258 ** ncol: Number of columns
1259 ** wr: True for a WITHOUT ROWID table
1260 ** strict: True for a STRICT table
1262 case PragTyp_TABLE_LIST
: {
1265 sqlite3CodeVerifyNamedSchema(pParse
, zDb
);
1266 for(ii
=0; ii
<db
->nDb
; ii
++){
1270 if( zDb
&& sqlite3_stricmp(zDb
, db
->aDb
[ii
].zDbSName
)!=0 ) continue;
1272 /* Ensure that the Table.nCol field is initialized for all views
1273 ** and virtual tables. Each time we initialize a Table.nCol value
1274 ** for a table, that can potentially disrupt the hash table, so restart
1275 ** the initialization scan.
1277 pHash
= &db
->aDb
[ii
].pSchema
->tblHash
;
1278 initNCol
= sqliteHashCount(pHash
);
1279 while( initNCol
-- ){
1280 for(k
=sqliteHashFirst(pHash
); 1; k
=sqliteHashNext(k
) ){
1282 if( k
==0 ){ initNCol
= 0; break; }
1283 pTab
= sqliteHashData(k
);
1284 if( pTab
->nCol
==0 ){
1285 char *zSql
= sqlite3MPrintf(db
, "SELECT*FROM\"%w\"", pTab
->zName
);
1287 sqlite3_stmt
*pDummy
= 0;
1288 (void)sqlite3_prepare(db
, zSql
, -1, &pDummy
, 0);
1289 (void)sqlite3_finalize(pDummy
);
1290 sqlite3DbFree(db
, zSql
);
1292 if( db
->mallocFailed
){
1293 sqlite3ErrorMsg(db
->pParse
, "out of memory");
1294 db
->pParse
->rc
= SQLITE_NOMEM_BKPT
;
1296 pHash
= &db
->aDb
[ii
].pSchema
->tblHash
;
1302 for(k
=sqliteHashFirst(pHash
); k
; k
=sqliteHashNext(k
) ){
1303 Table
*pTab
= sqliteHashData(k
);
1305 if( zRight
&& sqlite3_stricmp(zRight
, pTab
->zName
)!=0 ) continue;
1308 }else if( IsVirtual(pTab
) ){
1310 }else if( pTab
->tabFlags
& TF_Shadow
){
1315 sqlite3VdbeMultiLoad(v
, 1, "sssiii",
1316 db
->aDb
[ii
].zDbSName
,
1317 sqlite3PreferredTableName(pTab
->zName
),
1320 (pTab
->tabFlags
& TF_WithoutRowid
)!=0,
1321 (pTab
->tabFlags
& TF_Strict
)!=0
1329 case PragTyp_STATS
: {
1333 sqlite3CodeVerifySchema(pParse
, iDb
);
1334 for(i
=sqliteHashFirst(&pDb
->pSchema
->tblHash
); i
; i
=sqliteHashNext(i
)){
1335 Table
*pTab
= sqliteHashData(i
);
1336 sqlite3VdbeMultiLoad(v
, 1, "ssiii",
1337 sqlite3PreferredTableName(pTab
->zName
),
1342 for(pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
1343 sqlite3VdbeMultiLoad(v
, 2, "siiiX",
1346 pIdx
->aiRowLogEst
[0],
1348 sqlite3VdbeAddOp2(v
, OP_ResultRow
, 1, 5);
1355 case PragTyp_INDEX_INFO
: if( zRight
){
1358 pIdx
= sqlite3FindIndex(db
, zRight
, zDb
);
1360 /* If there is no index named zRight, check to see if there is a
1361 ** WITHOUT ROWID table named zRight, and if there is, show the
1362 ** structure of the PRIMARY KEY index for that table. */
1363 pTab
= sqlite3LocateTable(pParse
, LOCATE_NOERR
, zRight
, zDb
);
1364 if( pTab
&& !HasRowid(pTab
) ){
1365 pIdx
= sqlite3PrimaryKeyIndex(pTab
);
1369 int iIdxDb
= sqlite3SchemaToIndex(db
, pIdx
->pSchema
);
1372 if( pPragma
->iArg
){
1373 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1377 /* PRAGMA index_info (legacy version) */
1381 pTab
= pIdx
->pTable
;
1382 sqlite3CodeVerifySchema(pParse
, iIdxDb
);
1383 assert( pParse
->nMem
<=pPragma
->nPragCName
);
1384 for(i
=0; i
<mx
; i
++){
1385 i16 cnum
= pIdx
->aiColumn
[i
];
1386 sqlite3VdbeMultiLoad(v
, 1, "iisX", i
, cnum
,
1387 cnum
<0 ? 0 : pTab
->aCol
[cnum
].zCnName
);
1388 if( pPragma
->iArg
){
1389 sqlite3VdbeMultiLoad(v
, 4, "isiX",
1390 pIdx
->aSortOrder
[i
],
1394 sqlite3VdbeAddOp2(v
, OP_ResultRow
, 1, pParse
->nMem
);
1400 case PragTyp_INDEX_LIST
: if( zRight
){
1404 pTab
= sqlite3FindTable(db
, zRight
, zDb
);
1406 int iTabDb
= sqlite3SchemaToIndex(db
, pTab
->pSchema
);
1408 sqlite3CodeVerifySchema(pParse
, iTabDb
);
1409 for(pIdx
=pTab
->pIndex
, i
=0; pIdx
; pIdx
=pIdx
->pNext
, i
++){
1410 const char *azOrigin
[] = { "c", "u", "pk" };
1411 sqlite3VdbeMultiLoad(v
, 1, "isisi",
1414 IsUniqueIndex(pIdx
),
1415 azOrigin
[pIdx
->idxType
],
1416 pIdx
->pPartIdxWhere
!=0);
1422 case PragTyp_DATABASE_LIST
: {
1425 for(i
=0; i
<db
->nDb
; i
++){
1426 if( db
->aDb
[i
].pBt
==0 ) continue;
1427 assert( db
->aDb
[i
].zDbSName
!=0 );
1428 sqlite3VdbeMultiLoad(v
, 1, "iss",
1430 db
->aDb
[i
].zDbSName
,
1431 sqlite3BtreeGetFilename(db
->aDb
[i
].pBt
));
1436 case PragTyp_COLLATION_LIST
: {
1440 for(p
=sqliteHashFirst(&db
->aCollSeq
); p
; p
=sqliteHashNext(p
)){
1441 CollSeq
*pColl
= (CollSeq
*)sqliteHashData(p
);
1442 sqlite3VdbeMultiLoad(v
, 1, "is", i
++, pColl
->zName
);
1447 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
1448 case PragTyp_FUNCTION_LIST
: {
1452 int showInternFunc
= (db
->mDbFlags
& DBFLAG_InternalFunc
)!=0;
1454 for(i
=0; i
<SQLITE_FUNC_HASH_SZ
; i
++){
1455 for(p
=sqlite3BuiltinFunctions
.a
[i
]; p
; p
=p
->u
.pHash
){
1456 assert( p
->funcFlags
& SQLITE_FUNC_BUILTIN
);
1457 pragmaFunclistLine(v
, p
, 1, showInternFunc
);
1460 for(j
=sqliteHashFirst(&db
->aFunc
); j
; j
=sqliteHashNext(j
)){
1461 p
= (FuncDef
*)sqliteHashData(j
);
1462 assert( (p
->funcFlags
& SQLITE_FUNC_BUILTIN
)==0 );
1463 pragmaFunclistLine(v
, p
, 0, showInternFunc
);
1468 #ifndef SQLITE_OMIT_VIRTUALTABLE
1469 case PragTyp_MODULE_LIST
: {
1472 for(j
=sqliteHashFirst(&db
->aModule
); j
; j
=sqliteHashNext(j
)){
1473 Module
*pMod
= (Module
*)sqliteHashData(j
);
1474 sqlite3VdbeMultiLoad(v
, 1, "s", pMod
->zName
);
1478 #endif /* SQLITE_OMIT_VIRTUALTABLE */
1480 case PragTyp_PRAGMA_LIST
: {
1482 for(i
=0; i
<ArraySize(aPragmaName
); i
++){
1483 sqlite3VdbeMultiLoad(v
, 1, "s", aPragmaName
[i
].zName
);
1487 #endif /* SQLITE_INTROSPECTION_PRAGMAS */
1489 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1491 #ifndef SQLITE_OMIT_FOREIGN_KEY
1492 case PragTyp_FOREIGN_KEY_LIST
: if( zRight
){
1495 pTab
= sqlite3FindTable(db
, zRight
, zDb
);
1496 if( pTab
&& IsOrdinaryTable(pTab
) ){
1497 pFK
= pTab
->u
.tab
.pFKey
;
1499 int iTabDb
= sqlite3SchemaToIndex(db
, pTab
->pSchema
);
1502 sqlite3CodeVerifySchema(pParse
, iTabDb
);
1505 for(j
=0; j
<pFK
->nCol
; j
++){
1506 sqlite3VdbeMultiLoad(v
, 1, "iissssss",
1510 pTab
->aCol
[pFK
->aCol
[j
].iFrom
].zCnName
,
1512 actionName(pFK
->aAction
[1]), /* ON UPDATE */
1513 actionName(pFK
->aAction
[0]), /* ON DELETE */
1517 pFK
= pFK
->pNextFrom
;
1523 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1525 #ifndef SQLITE_OMIT_FOREIGN_KEY
1526 #ifndef SQLITE_OMIT_TRIGGER
1527 case PragTyp_FOREIGN_KEY_CHECK
: {
1528 FKey
*pFK
; /* A foreign key constraint */
1529 Table
*pTab
; /* Child table contain "REFERENCES" keyword */
1530 Table
*pParent
; /* Parent table that child points to */
1531 Index
*pIdx
; /* Index in the parent table */
1532 int i
; /* Loop counter: Foreign key number for pTab */
1533 int j
; /* Loop counter: Field of the foreign key */
1534 HashElem
*k
; /* Loop counter: Next table in schema */
1535 int x
; /* result variable */
1536 int regResult
; /* 3 registers to hold a result row */
1537 int regRow
; /* Registers to hold a row from pTab */
1538 int addrTop
; /* Top of a loop checking foreign keys */
1539 int addrOk
; /* Jump here if the key is OK */
1540 int *aiCols
; /* child to parent column mapping */
1542 regResult
= pParse
->nMem
+1;
1544 regRow
= ++pParse
->nMem
;
1545 k
= sqliteHashFirst(&db
->aDb
[iDb
].pSchema
->tblHash
);
1548 pTab
= sqlite3LocateTable(pParse
, 0, zRight
, zDb
);
1551 pTab
= (Table
*)sqliteHashData(k
);
1552 k
= sqliteHashNext(k
);
1554 if( pTab
==0 || !IsOrdinaryTable(pTab
) || pTab
->u
.tab
.pFKey
==0 ) continue;
1555 iDb
= sqlite3SchemaToIndex(db
, pTab
->pSchema
);
1556 zDb
= db
->aDb
[iDb
].zDbSName
;
1557 sqlite3CodeVerifySchema(pParse
, iDb
);
1558 sqlite3TableLock(pParse
, iDb
, pTab
->tnum
, 0, pTab
->zName
);
1559 sqlite3TouchRegister(pParse
, pTab
->nCol
+regRow
);
1560 sqlite3OpenTable(pParse
, 0, iDb
, pTab
, OP_OpenRead
);
1561 sqlite3VdbeLoadString(v
, regResult
, pTab
->zName
);
1562 assert( IsOrdinaryTable(pTab
) );
1563 for(i
=1, pFK
=pTab
->u
.tab
.pFKey
; pFK
; i
++, pFK
=pFK
->pNextFrom
){
1564 pParent
= sqlite3FindTable(db
, pFK
->zTo
, zDb
);
1565 if( pParent
==0 ) continue;
1567 sqlite3TableLock(pParse
, iDb
, pParent
->tnum
, 0, pParent
->zName
);
1568 x
= sqlite3FkLocateIndex(pParse
, pParent
, pFK
, &pIdx
, 0);
1571 sqlite3OpenTable(pParse
, i
, iDb
, pParent
, OP_OpenRead
);
1573 sqlite3VdbeAddOp3(v
, OP_OpenRead
, i
, pIdx
->tnum
, iDb
);
1574 sqlite3VdbeSetP4KeyInfo(pParse
, pIdx
);
1581 assert( pParse
->nErr
>0 || pFK
==0 );
1583 if( pParse
->nTab
<i
) pParse
->nTab
= i
;
1584 addrTop
= sqlite3VdbeAddOp1(v
, OP_Rewind
, 0); VdbeCoverage(v
);
1585 assert( IsOrdinaryTable(pTab
) );
1586 for(i
=1, pFK
=pTab
->u
.tab
.pFKey
; pFK
; i
++, pFK
=pFK
->pNextFrom
){
1587 pParent
= sqlite3FindTable(db
, pFK
->zTo
, zDb
);
1591 x
= sqlite3FkLocateIndex(pParse
, pParent
, pFK
, &pIdx
, &aiCols
);
1592 assert( x
==0 || db
->mallocFailed
);
1594 addrOk
= sqlite3VdbeMakeLabel(pParse
);
1596 /* Generate code to read the child key values into registers
1597 ** regRow..regRow+n. If any of the child key values are NULL, this
1598 ** row cannot cause an FK violation. Jump directly to addrOk in
1600 sqlite3TouchRegister(pParse
, regRow
+ pFK
->nCol
);
1601 for(j
=0; j
<pFK
->nCol
; j
++){
1602 int iCol
= aiCols
? aiCols
[j
] : pFK
->aCol
[j
].iFrom
;
1603 sqlite3ExprCodeGetColumnOfTable(v
, pTab
, 0, iCol
, regRow
+j
);
1604 sqlite3VdbeAddOp2(v
, OP_IsNull
, regRow
+j
, addrOk
); VdbeCoverage(v
);
1607 /* Generate code to query the parent index for a matching parent
1608 ** key. If a match is found, jump to addrOk. */
1610 sqlite3VdbeAddOp4(v
, OP_Affinity
, regRow
, pFK
->nCol
, 0,
1611 sqlite3IndexAffinityStr(db
,pIdx
), pFK
->nCol
);
1612 sqlite3VdbeAddOp4Int(v
, OP_Found
, i
, addrOk
, regRow
, pFK
->nCol
);
1614 }else if( pParent
){
1615 int jmp
= sqlite3VdbeCurrentAddr(v
)+2;
1616 sqlite3VdbeAddOp3(v
, OP_SeekRowid
, i
, jmp
, regRow
); VdbeCoverage(v
);
1617 sqlite3VdbeGoto(v
, addrOk
);
1618 assert( pFK
->nCol
==1 || db
->mallocFailed
);
1621 /* Generate code to report an FK violation to the caller. */
1622 if( HasRowid(pTab
) ){
1623 sqlite3VdbeAddOp2(v
, OP_Rowid
, 0, regResult
+1);
1625 sqlite3VdbeAddOp2(v
, OP_Null
, 0, regResult
+1);
1627 sqlite3VdbeMultiLoad(v
, regResult
+2, "siX", pFK
->zTo
, i
-1);
1628 sqlite3VdbeAddOp2(v
, OP_ResultRow
, regResult
, 4);
1629 sqlite3VdbeResolveLabel(v
, addrOk
);
1630 sqlite3DbFree(db
, aiCols
);
1632 sqlite3VdbeAddOp2(v
, OP_Next
, 0, addrTop
+1); VdbeCoverage(v
);
1633 sqlite3VdbeJumpHere(v
, addrTop
);
1637 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
1638 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1640 #ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
1641 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1642 ** used will be case sensitive or not depending on the RHS.
1644 case PragTyp_CASE_SENSITIVE_LIKE
: {
1646 sqlite3RegisterLikeFunctions(db
, sqlite3GetBoolean(zRight
, 0));
1650 #endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
1652 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1653 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1656 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
1657 /* PRAGMA integrity_check
1658 ** PRAGMA integrity_check(N)
1659 ** PRAGMA quick_check
1660 ** PRAGMA quick_check(N)
1662 ** Verify the integrity of the database.
1664 ** The "quick_check" is reduced version of
1665 ** integrity_check designed to detect most database corruption
1666 ** without the overhead of cross-checking indexes. Quick_check
1667 ** is linear time whereas integrity_check is O(NlogN).
1669 ** The maximum number of errors is 100 by default. A different default
1670 ** can be specified using a numeric parameter N.
1672 ** Or, the parameter N can be the name of a table. In that case, only
1673 ** the one table named is verified. The freelist is only verified if
1674 ** the named table is "sqlite_schema" (or one of its aliases).
1676 ** All schemas are checked by default. To check just a single
1677 ** schema, use the form:
1679 ** PRAGMA schema.integrity_check;
1681 case PragTyp_INTEGRITY_CHECK
: {
1682 int i
, j
, addr
, mxErr
;
1683 Table
*pObjTab
= 0; /* Check only this one table, if not NULL */
1685 int isQuick
= (sqlite3Tolower(zLeft
[0])=='q');
1687 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1688 ** then iDb is set to the index of the database identified by <db>.
1689 ** In this case, the integrity of database iDb only is verified by
1690 ** the VDBE created below.
1692 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1693 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1694 ** to -1 here, to indicate that the VDBE should verify the integrity
1695 ** of all attached databases. */
1697 assert( iDb
==0 || pId2
->z
);
1698 if( pId2
->z
==0 ) iDb
= -1;
1700 /* Initialize the VDBE program */
1703 /* Set the maximum error count */
1704 mxErr
= SQLITE_INTEGRITY_CHECK_ERROR_MAX
;
1706 if( sqlite3GetInt32(pValue
->z
, &mxErr
) ){
1708 mxErr
= SQLITE_INTEGRITY_CHECK_ERROR_MAX
;
1711 pObjTab
= sqlite3LocateTable(pParse
, 0, zRight
,
1712 iDb
>=0 ? db
->aDb
[iDb
].zDbSName
: 0);
1715 sqlite3VdbeAddOp2(v
, OP_Integer
, mxErr
-1, 1); /* reg[1] holds errors left */
1717 /* Do an integrity check on each database file */
1718 for(i
=0; i
<db
->nDb
; i
++){
1719 HashElem
*x
; /* For looping over tables in the schema */
1720 Hash
*pTbls
; /* Set of all tables in the schema */
1721 int *aRoot
; /* Array of root page numbers of all btrees */
1722 int cnt
= 0; /* Number of entries in aRoot[] */
1724 if( OMIT_TEMPDB
&& i
==1 ) continue;
1725 if( iDb
>=0 && i
!=iDb
) continue;
1727 sqlite3CodeVerifySchema(pParse
, i
);
1728 pParse
->okConstFactor
= 0; /* tag-20230327-1 */
1730 /* Do an integrity check of the B-Tree
1732 ** Begin by finding the root pages numbers
1733 ** for all tables and indices in the database.
1735 assert( sqlite3SchemaMutexHeld(db
, i
, 0) );
1736 pTbls
= &db
->aDb
[i
].pSchema
->tblHash
;
1737 for(cnt
=0, x
=sqliteHashFirst(pTbls
); x
; x
=sqliteHashNext(x
)){
1738 Table
*pTab
= sqliteHashData(x
); /* Current table */
1739 Index
*pIdx
; /* An index on pTab */
1740 int nIdx
; /* Number of indexes on pTab */
1741 if( pObjTab
&& pObjTab
!=pTab
) continue;
1742 if( HasRowid(pTab
) ) cnt
++;
1743 for(nIdx
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, nIdx
++){ cnt
++; }
1745 if( cnt
==0 ) continue;
1746 if( pObjTab
) cnt
++;
1747 aRoot
= sqlite3DbMallocRawNN(db
, sizeof(int)*(cnt
+1));
1748 if( aRoot
==0 ) break;
1750 if( pObjTab
) aRoot
[++cnt
] = 0;
1751 for(x
=sqliteHashFirst(pTbls
); x
; x
=sqliteHashNext(x
)){
1752 Table
*pTab
= sqliteHashData(x
);
1754 if( pObjTab
&& pObjTab
!=pTab
) continue;
1755 if( HasRowid(pTab
) ) aRoot
[++cnt
] = pTab
->tnum
;
1756 for(pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
1757 aRoot
[++cnt
] = pIdx
->tnum
;
1762 /* Make sure sufficient number of registers have been allocated */
1763 sqlite3TouchRegister(pParse
, 8+cnt
);
1764 sqlite3ClearTempRegCache(pParse
);
1766 /* Do the b-tree integrity checks */
1767 sqlite3VdbeAddOp4(v
, OP_IntegrityCk
, 1, cnt
, 8, (char*)aRoot
,P4_INTARRAY
);
1768 sqlite3VdbeChangeP5(v
, (u8
)i
);
1769 addr
= sqlite3VdbeAddOp1(v
, OP_IsNull
, 2); VdbeCoverage(v
);
1770 sqlite3VdbeAddOp4(v
, OP_String8
, 0, 3, 0,
1771 sqlite3MPrintf(db
, "*** in database %s ***\n", db
->aDb
[i
].zDbSName
),
1773 sqlite3VdbeAddOp3(v
, OP_Concat
, 2, 3, 3);
1774 integrityCheckResultRow(v
);
1775 sqlite3VdbeJumpHere(v
, addr
);
1777 /* Check that the indexes all have the right number of rows */
1778 cnt
= pObjTab
? 1 : 0;
1779 sqlite3VdbeLoadString(v
, 2, "wrong # of entries in index ");
1780 for(x
=sqliteHashFirst(pTbls
); x
; x
=sqliteHashNext(x
)){
1782 Table
*pTab
= sqliteHashData(x
);
1784 if( pObjTab
&& pObjTab
!=pTab
) continue;
1785 if( HasRowid(pTab
) ){
1789 for(pIdx
=pTab
->pIndex
; ALWAYS(pIdx
); pIdx
=pIdx
->pNext
){
1790 if( IsPrimaryKeyIndex(pIdx
) ) break;
1794 for(pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
1795 if( pIdx
->pPartIdxWhere
==0 ){
1796 addr
= sqlite3VdbeAddOp3(v
, OP_Eq
, 8+cnt
, 0, 8+iTab
);
1797 VdbeCoverageNeverNull(v
);
1798 sqlite3VdbeLoadString(v
, 4, pIdx
->zName
);
1799 sqlite3VdbeAddOp3(v
, OP_Concat
, 4, 2, 3);
1800 integrityCheckResultRow(v
);
1801 sqlite3VdbeJumpHere(v
, addr
);
1807 /* Make sure all the indices are constructed correctly.
1809 for(x
=sqliteHashFirst(pTbls
); x
; x
=sqliteHashNext(x
)){
1810 Table
*pTab
= sqliteHashData(x
);
1812 Index
*pPrior
= 0; /* Previous index */
1814 int iDataCur
, iIdxCur
;
1816 int bStrict
; /* True for a STRICT table */
1817 int r2
; /* Previous key for WITHOUT ROWID tables */
1818 int mxCol
; /* Maximum non-virtual column number */
1820 if( pObjTab
&& pObjTab
!=pTab
) continue;
1821 if( !IsOrdinaryTable(pTab
) ) continue;
1822 if( isQuick
|| HasRowid(pTab
) ){
1826 pPk
= sqlite3PrimaryKeyIndex(pTab
);
1827 r2
= sqlite3GetTempRange(pParse
, pPk
->nKeyCol
);
1828 sqlite3VdbeAddOp3(v
, OP_Null
, 1, r2
, r2
+pPk
->nKeyCol
-1);
1830 sqlite3OpenTableAndIndices(pParse
, pTab
, OP_OpenRead
, 0,
1831 1, 0, &iDataCur
, &iIdxCur
);
1832 /* reg[7] counts the number of entries in the table.
1833 ** reg[8+i] counts the number of entries in the i-th index
1835 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, 7);
1836 for(j
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, j
++){
1837 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, 8+j
); /* index entries counter */
1839 assert( pParse
->nMem
>=8+j
);
1840 assert( sqlite3NoTempsInRange(pParse
,1,7+j
) );
1841 sqlite3VdbeAddOp2(v
, OP_Rewind
, iDataCur
, 0); VdbeCoverage(v
);
1842 loopTop
= sqlite3VdbeAddOp2(v
, OP_AddImm
, 7, 1);
1844 /* Fetch the right-most column from the table. This will cause
1845 ** the entire record header to be parsed and sanity checked. It
1846 ** will also prepopulate the cursor column cache that is used
1847 ** by the OP_IsType code, so it is a required step.
1849 assert( !IsVirtual(pTab
) );
1850 if( HasRowid(pTab
) ){
1852 for(j
=0; j
<pTab
->nCol
; j
++){
1853 if( (pTab
->aCol
[j
].colFlags
& COLFLAG_VIRTUAL
)==0 ) mxCol
++;
1855 if( mxCol
==pTab
->iPKey
) mxCol
--;
1857 /* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID
1858 ** PK index column-count, so there is no need to account for them
1860 mxCol
= sqlite3PrimaryKeyIndex(pTab
)->nColumn
-1;
1863 sqlite3VdbeAddOp3(v
, OP_Column
, iDataCur
, mxCol
, 3);
1864 sqlite3VdbeTypeofColumn(v
, 3);
1869 /* Verify WITHOUT ROWID keys are in ascending order */
1872 a1
= sqlite3VdbeAddOp4Int(v
, OP_IdxGT
, iDataCur
, 0,r2
,pPk
->nKeyCol
);
1874 sqlite3VdbeAddOp1(v
, OP_IsNull
, r2
); VdbeCoverage(v
);
1875 zErr
= sqlite3MPrintf(db
,
1876 "row not in PRIMARY KEY order for %s",
1878 sqlite3VdbeAddOp4(v
, OP_String8
, 0, 3, 0, zErr
, P4_DYNAMIC
);
1879 integrityCheckResultRow(v
);
1880 sqlite3VdbeJumpHere(v
, a1
);
1881 sqlite3VdbeJumpHere(v
, a1
+1);
1882 for(j
=0; j
<pPk
->nKeyCol
; j
++){
1883 sqlite3ExprCodeLoadIndexColumn(pParse
, pPk
, iDataCur
, j
, r2
+j
);
1887 /* Verify datatypes for all columns:
1889 ** (1) NOT NULL columns may not contain a NULL
1890 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
1891 ** (3) Datatype for TEXT columns in non-STRICT tables must be
1892 ** NULL, TEXT, or BLOB.
1893 ** (4) Datatype for numeric columns in non-STRICT tables must not
1894 ** be a TEXT value that can be losslessly converted to numeric.
1896 bStrict
= (pTab
->tabFlags
& TF_Strict
)!=0;
1897 for(j
=0; j
<pTab
->nCol
; j
++){
1899 Column
*pCol
= pTab
->aCol
+ j
; /* The column to be checked */
1900 int labelError
; /* Jump here to report an error */
1901 int labelOk
; /* Jump here if all looks ok */
1902 int p1
, p3
, p4
; /* Operands to the OP_IsType opcode */
1903 int doTypeCheck
; /* Check datatypes (besides NOT NULL) */
1905 if( j
==pTab
->iPKey
) continue;
1907 doTypeCheck
= pCol
->eCType
>COLTYPE_ANY
;
1909 doTypeCheck
= pCol
->affinity
>SQLITE_AFF_BLOB
;
1911 if( pCol
->notNull
==0 && !doTypeCheck
) continue;
1913 /* Compute the operands that will be needed for OP_IsType */
1915 if( pCol
->colFlags
& COLFLAG_VIRTUAL
){
1916 sqlite3ExprCodeGetColumnOfTable(v
, pTab
, iDataCur
, j
, 3);
1921 sqlite3_value
*pDfltValue
= 0;
1922 sqlite3ValueFromExpr(db
, sqlite3ColumnExpr(pTab
,pCol
), ENC(db
),
1923 pCol
->affinity
, &pDfltValue
);
1925 p4
= sqlite3_value_type(pDfltValue
);
1926 sqlite3ValueFree(pDfltValue
);
1930 if( !HasRowid(pTab
) ){
1931 testcase( j
!=sqlite3TableColumnToStorage(pTab
, j
) );
1932 p3
= sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab
), j
);
1934 p3
= sqlite3TableColumnToStorage(pTab
,j
);
1939 labelError
= sqlite3VdbeMakeLabel(pParse
);
1940 labelOk
= sqlite3VdbeMakeLabel(pParse
);
1941 if( pCol
->notNull
){
1942 /* (1) NOT NULL columns may not contain a NULL */
1944 int jmp2
= sqlite3VdbeAddOp4Int(v
, OP_IsType
, p1
, labelOk
, p3
, p4
);
1947 sqlite3VdbeChangeP5(v
, 0x0f); /* INT, REAL, TEXT, or BLOB */
1950 sqlite3VdbeChangeP5(v
, 0x0d); /* INT, TEXT, or BLOB */
1951 /* OP_IsType does not detect NaN values in the database file
1952 ** which should be treated as a NULL. So if the header type
1953 ** is REAL, we have to load the actual data using OP_Column
1954 ** to reliably determine if the value is a NULL. */
1955 sqlite3VdbeAddOp3(v
, OP_Column
, p1
, p3
, 3);
1956 sqlite3ColumnDefault(v
, pTab
, j
, 3);
1957 jmp3
= sqlite3VdbeAddOp2(v
, OP_NotNull
, 3, labelOk
);
1960 zErr
= sqlite3MPrintf(db
, "NULL value in %s.%s", pTab
->zName
,
1962 sqlite3VdbeAddOp4(v
, OP_String8
, 0, 3, 0, zErr
, P4_DYNAMIC
);
1964 sqlite3VdbeGoto(v
, labelError
);
1965 sqlite3VdbeJumpHere(v
, jmp2
);
1966 sqlite3VdbeJumpHere(v
, jmp3
);
1968 /* VDBE byte code will fall thru */
1971 if( bStrict
&& doTypeCheck
){
1972 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
1973 static unsigned char aStdTypeMask
[] = {
1981 sqlite3VdbeAddOp4Int(v
, OP_IsType
, p1
, labelOk
, p3
, p4
);
1982 assert( pCol
->eCType
>=1 && pCol
->eCType
<=sizeof(aStdTypeMask
) );
1983 sqlite3VdbeChangeP5(v
, aStdTypeMask
[pCol
->eCType
-1]);
1985 zErr
= sqlite3MPrintf(db
, "non-%s value in %s.%s",
1986 sqlite3StdType
[pCol
->eCType
-1],
1987 pTab
->zName
, pTab
->aCol
[j
].zCnName
);
1988 sqlite3VdbeAddOp4(v
, OP_String8
, 0, 3, 0, zErr
, P4_DYNAMIC
);
1989 }else if( !bStrict
&& pCol
->affinity
==SQLITE_AFF_TEXT
){
1990 /* (3) Datatype for TEXT columns in non-STRICT tables must be
1991 ** NULL, TEXT, or BLOB. */
1992 sqlite3VdbeAddOp4Int(v
, OP_IsType
, p1
, labelOk
, p3
, p4
);
1993 sqlite3VdbeChangeP5(v
, 0x1c); /* NULL, TEXT, or BLOB */
1995 zErr
= sqlite3MPrintf(db
, "NUMERIC value in %s.%s",
1996 pTab
->zName
, pTab
->aCol
[j
].zCnName
);
1997 sqlite3VdbeAddOp4(v
, OP_String8
, 0, 3, 0, zErr
, P4_DYNAMIC
);
1998 }else if( !bStrict
&& pCol
->affinity
>=SQLITE_AFF_NUMERIC
){
1999 /* (4) Datatype for numeric columns in non-STRICT tables must not
2000 ** be a TEXT value that can be converted to numeric. */
2001 sqlite3VdbeAddOp4Int(v
, OP_IsType
, p1
, labelOk
, p3
, p4
);
2002 sqlite3VdbeChangeP5(v
, 0x1b); /* NULL, INT, FLOAT, or BLOB */
2005 sqlite3ExprCodeGetColumnOfTable(v
, pTab
, iDataCur
, j
, 3);
2007 sqlite3VdbeAddOp4(v
, OP_Affinity
, 3, 1, 0, "C", P4_STATIC
);
2008 sqlite3VdbeAddOp4Int(v
, OP_IsType
, -1, labelOk
, 3, p4
);
2009 sqlite3VdbeChangeP5(v
, 0x1c); /* NULL, TEXT, or BLOB */
2011 zErr
= sqlite3MPrintf(db
, "TEXT value in %s.%s",
2012 pTab
->zName
, pTab
->aCol
[j
].zCnName
);
2013 sqlite3VdbeAddOp4(v
, OP_String8
, 0, 3, 0, zErr
, P4_DYNAMIC
);
2015 sqlite3VdbeResolveLabel(v
, labelError
);
2016 integrityCheckResultRow(v
);
2017 sqlite3VdbeResolveLabel(v
, labelOk
);
2019 /* Verify CHECK constraints */
2020 if( pTab
->pCheck
&& (db
->flags
& SQLITE_IgnoreChecks
)==0 ){
2021 ExprList
*pCheck
= sqlite3ExprListDup(db
, pTab
->pCheck
, 0);
2022 if( db
->mallocFailed
==0 ){
2023 int addrCkFault
= sqlite3VdbeMakeLabel(pParse
);
2024 int addrCkOk
= sqlite3VdbeMakeLabel(pParse
);
2027 pParse
->iSelfTab
= iDataCur
+ 1;
2028 for(k
=pCheck
->nExpr
-1; k
>0; k
--){
2029 sqlite3ExprIfFalse(pParse
, pCheck
->a
[k
].pExpr
, addrCkFault
, 0);
2031 sqlite3ExprIfTrue(pParse
, pCheck
->a
[0].pExpr
, addrCkOk
,
2033 sqlite3VdbeResolveLabel(v
, addrCkFault
);
2034 pParse
->iSelfTab
= 0;
2035 zErr
= sqlite3MPrintf(db
, "CHECK constraint failed in %s",
2037 sqlite3VdbeAddOp4(v
, OP_String8
, 0, 3, 0, zErr
, P4_DYNAMIC
);
2038 integrityCheckResultRow(v
);
2039 sqlite3VdbeResolveLabel(v
, addrCkOk
);
2041 sqlite3ExprListDelete(db
, pCheck
);
2043 if( !isQuick
){ /* Omit the remaining tests for quick_check */
2044 /* Validate index entries for the current row */
2045 for(j
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, j
++){
2046 int jmp2
, jmp3
, jmp4
, jmp5
, label6
;
2048 int ckUniq
= sqlite3VdbeMakeLabel(pParse
);
2049 if( pPk
==pIdx
) continue;
2050 r1
= sqlite3GenerateIndexKey(pParse
, pIdx
, iDataCur
, 0, 0, &jmp3
,
2053 sqlite3VdbeAddOp2(v
, OP_AddImm
, 8+j
, 1);/* increment entry count */
2054 /* Verify that an index entry exists for the current table row */
2055 jmp2
= sqlite3VdbeAddOp4Int(v
, OP_Found
, iIdxCur
+j
, ckUniq
, r1
,
2056 pIdx
->nColumn
); VdbeCoverage(v
);
2057 sqlite3VdbeLoadString(v
, 3, "row ");
2058 sqlite3VdbeAddOp3(v
, OP_Concat
, 7, 3, 3);
2059 sqlite3VdbeLoadString(v
, 4, " missing from index ");
2060 sqlite3VdbeAddOp3(v
, OP_Concat
, 4, 3, 3);
2061 jmp5
= sqlite3VdbeLoadString(v
, 4, pIdx
->zName
);
2062 sqlite3VdbeAddOp3(v
, OP_Concat
, 4, 3, 3);
2063 jmp4
= integrityCheckResultRow(v
);
2064 sqlite3VdbeJumpHere(v
, jmp2
);
2066 /* The OP_IdxRowid opcode is an optimized version of OP_Column
2067 ** that extracts the rowid off the end of the index record.
2068 ** But it only works correctly if index record does not have
2069 ** any extra bytes at the end. Verify that this is the case. */
2070 if( HasRowid(pTab
) ){
2072 sqlite3VdbeAddOp2(v
, OP_IdxRowid
, iIdxCur
+j
, 3);
2073 jmp7
= sqlite3VdbeAddOp3(v
, OP_Eq
, 3, 0, r1
+pIdx
->nColumn
-1);
2074 VdbeCoverageNeverNull(v
);
2075 sqlite3VdbeLoadString(v
, 3,
2076 "rowid not at end-of-record for row ");
2077 sqlite3VdbeAddOp3(v
, OP_Concat
, 7, 3, 3);
2078 sqlite3VdbeLoadString(v
, 4, " of index ");
2079 sqlite3VdbeGoto(v
, jmp5
-1);
2080 sqlite3VdbeJumpHere(v
, jmp7
);
2083 /* Any indexed columns with non-BINARY collations must still hold
2084 ** the exact same text value as the table. */
2086 for(kk
=0; kk
<pIdx
->nKeyCol
; kk
++){
2087 if( pIdx
->azColl
[kk
]==sqlite3StrBINARY
) continue;
2088 if( label6
==0 ) label6
= sqlite3VdbeMakeLabel(pParse
);
2089 sqlite3VdbeAddOp3(v
, OP_Column
, iIdxCur
+j
, kk
, 3);
2090 sqlite3VdbeAddOp3(v
, OP_Ne
, 3, label6
, r1
+kk
); VdbeCoverage(v
);
2093 int jmp6
= sqlite3VdbeAddOp0(v
, OP_Goto
);
2094 sqlite3VdbeResolveLabel(v
, label6
);
2095 sqlite3VdbeLoadString(v
, 3, "row ");
2096 sqlite3VdbeAddOp3(v
, OP_Concat
, 7, 3, 3);
2097 sqlite3VdbeLoadString(v
, 4, " values differ from index ");
2098 sqlite3VdbeGoto(v
, jmp5
-1);
2099 sqlite3VdbeJumpHere(v
, jmp6
);
2102 /* For UNIQUE indexes, verify that only one entry exists with the
2103 ** current key. The entry is unique if (1) any column is NULL
2104 ** or (2) the next entry has a different key */
2105 if( IsUniqueIndex(pIdx
) ){
2106 int uniqOk
= sqlite3VdbeMakeLabel(pParse
);
2108 for(kk
=0; kk
<pIdx
->nKeyCol
; kk
++){
2109 int iCol
= pIdx
->aiColumn
[kk
];
2110 assert( iCol
!=XN_ROWID
&& iCol
<pTab
->nCol
);
2111 if( iCol
>=0 && pTab
->aCol
[iCol
].notNull
) continue;
2112 sqlite3VdbeAddOp2(v
, OP_IsNull
, r1
+kk
, uniqOk
);
2115 jmp6
= sqlite3VdbeAddOp1(v
, OP_Next
, iIdxCur
+j
); VdbeCoverage(v
);
2116 sqlite3VdbeGoto(v
, uniqOk
);
2117 sqlite3VdbeJumpHere(v
, jmp6
);
2118 sqlite3VdbeAddOp4Int(v
, OP_IdxGT
, iIdxCur
+j
, uniqOk
, r1
,
2119 pIdx
->nKeyCol
); VdbeCoverage(v
);
2120 sqlite3VdbeLoadString(v
, 3, "non-unique entry in index ");
2121 sqlite3VdbeGoto(v
, jmp5
);
2122 sqlite3VdbeResolveLabel(v
, uniqOk
);
2124 sqlite3VdbeJumpHere(v
, jmp4
);
2125 sqlite3ResolvePartIdxLabel(pParse
, jmp3
);
2128 sqlite3VdbeAddOp2(v
, OP_Next
, iDataCur
, loopTop
); VdbeCoverage(v
);
2129 sqlite3VdbeJumpHere(v
, loopTop
-1);
2132 sqlite3ReleaseTempRange(pParse
, r2
, pPk
->nKeyCol
);
2136 #ifndef SQLITE_OMIT_VIRTUALTABLE
2137 /* Second pass to invoke the xIntegrity method on all virtual
2140 for(x
=sqliteHashFirst(pTbls
); x
; x
=sqliteHashNext(x
)){
2141 Table
*pTab
= sqliteHashData(x
);
2142 sqlite3_vtab
*pVTab
;
2144 if( pObjTab
&& pObjTab
!=pTab
) continue;
2145 if( IsOrdinaryTable(pTab
) ) continue;
2146 if( !IsVirtual(pTab
) ) continue;
2147 if( pTab
->nCol
<=0 ){
2148 const char *zMod
= pTab
->u
.vtab
.azArg
[0];
2149 if( sqlite3HashFind(&db
->aModule
, zMod
)==0 ) continue;
2151 sqlite3ViewGetColumnNames(pParse
, pTab
);
2152 if( pTab
->u
.vtab
.p
==0 ) continue;
2153 pVTab
= pTab
->u
.vtab
.p
->pVtab
;
2154 if( NEVER(pVTab
==0) ) continue;
2155 if( NEVER(pVTab
->pModule
==0) ) continue;
2156 if( pVTab
->pModule
->iVersion
<4 ) continue;
2157 if( pVTab
->pModule
->xIntegrity
==0 ) continue;
2158 sqlite3VdbeAddOp3(v
, OP_VCheck
, i
, 3, isQuick
);
2160 sqlite3VdbeAppendP4(v
, pTab
, P4_TABLEREF
);
2161 a1
= sqlite3VdbeAddOp1(v
, OP_IsNull
, 3); VdbeCoverage(v
);
2162 integrityCheckResultRow(v
);
2163 sqlite3VdbeJumpHere(v
, a1
);
2169 static const int iLn
= VDBE_OFFSET_LINENO(2);
2170 static const VdbeOpList endCode
[] = {
2171 { OP_AddImm
, 1, 0, 0}, /* 0 */
2172 { OP_IfNotZero
, 1, 4, 0}, /* 1 */
2173 { OP_String8
, 0, 3, 0}, /* 2 */
2174 { OP_ResultRow
, 3, 1, 0}, /* 3 */
2175 { OP_Halt
, 0, 0, 0}, /* 4 */
2176 { OP_String8
, 0, 3, 0}, /* 5 */
2177 { OP_Goto
, 0, 3, 0}, /* 6 */
2181 aOp
= sqlite3VdbeAddOpList(v
, ArraySize(endCode
), endCode
, iLn
);
2183 aOp
[0].p2
= 1-mxErr
;
2184 aOp
[2].p4type
= P4_STATIC
;
2186 aOp
[5].p4type
= P4_STATIC
;
2187 aOp
[5].p4
.z
= (char*)sqlite3ErrStr(SQLITE_CORRUPT
);
2189 sqlite3VdbeChangeP3(v
, 0, sqlite3VdbeCurrentAddr(v
)-2);
2193 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
2195 #ifndef SQLITE_OMIT_UTF16
2198 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
2200 ** In its first form, this pragma returns the encoding of the main
2201 ** database. If the database is not initialized, it is initialized now.
2203 ** The second form of this pragma is a no-op if the main database file
2204 ** has not already been initialized. In this case it sets the default
2205 ** encoding that will be used for the main database file if a new file
2206 ** is created. If an existing main database file is opened, then the
2207 ** default text encoding for the existing database is used.
2209 ** In all cases new databases created using the ATTACH command are
2210 ** created to use the same default text encoding as the main database. If
2211 ** the main database has not been initialized and/or created when ATTACH
2212 ** is executed, this is done before the ATTACH operation.
2214 ** In the second form this pragma sets the text encoding to be used in
2215 ** new database files created using this database handle. It is only
2216 ** useful if invoked immediately after the main database i
2218 case PragTyp_ENCODING
: {
2219 static const struct EncName
{
2223 { "UTF8", SQLITE_UTF8
},
2224 { "UTF-8", SQLITE_UTF8
}, /* Must be element [1] */
2225 { "UTF-16le", SQLITE_UTF16LE
}, /* Must be element [2] */
2226 { "UTF-16be", SQLITE_UTF16BE
}, /* Must be element [3] */
2227 { "UTF16le", SQLITE_UTF16LE
},
2228 { "UTF16be", SQLITE_UTF16BE
},
2229 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
2230 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
2233 const struct EncName
*pEnc
;
2234 if( !zRight
){ /* "PRAGMA encoding" */
2235 if( sqlite3ReadSchema(pParse
) ) goto pragma_out
;
2236 assert( encnames
[SQLITE_UTF8
].enc
==SQLITE_UTF8
);
2237 assert( encnames
[SQLITE_UTF16LE
].enc
==SQLITE_UTF16LE
);
2238 assert( encnames
[SQLITE_UTF16BE
].enc
==SQLITE_UTF16BE
);
2239 returnSingleText(v
, encnames
[ENC(pParse
->db
)].zName
);
2240 }else{ /* "PRAGMA encoding = XXX" */
2241 /* Only change the value of sqlite.enc if the database handle is not
2242 ** initialized. If the main database exists, the new sqlite.enc value
2243 ** will be overwritten when the schema is next loaded. If it does not
2244 ** already exists, it will be created to use the new encoding value.
2246 if( (db
->mDbFlags
& DBFLAG_EncodingFixed
)==0 ){
2247 for(pEnc
=&encnames
[0]; pEnc
->zName
; pEnc
++){
2248 if( 0==sqlite3StrICmp(zRight
, pEnc
->zName
) ){
2249 u8 enc
= pEnc
->enc
? pEnc
->enc
: SQLITE_UTF16NATIVE
;
2250 SCHEMA_ENC(db
) = enc
;
2251 sqlite3SetTextEncoding(db
, enc
);
2256 sqlite3ErrorMsg(pParse
, "unsupported encoding: %s", zRight
);
2262 #endif /* SQLITE_OMIT_UTF16 */
2264 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
2266 ** PRAGMA [schema.]schema_version
2267 ** PRAGMA [schema.]schema_version = <integer>
2269 ** PRAGMA [schema.]user_version
2270 ** PRAGMA [schema.]user_version = <integer>
2272 ** PRAGMA [schema.]freelist_count
2274 ** PRAGMA [schema.]data_version
2276 ** PRAGMA [schema.]application_id
2277 ** PRAGMA [schema.]application_id = <integer>
2279 ** The pragma's schema_version and user_version are used to set or get
2280 ** the value of the schema-version and user-version, respectively. Both
2281 ** the schema-version and the user-version are 32-bit signed integers
2282 ** stored in the database header.
2284 ** The schema-cookie is usually only manipulated internally by SQLite. It
2285 ** is incremented by SQLite whenever the database schema is modified (by
2286 ** creating or dropping a table or index). The schema version is used by
2287 ** SQLite each time a query is executed to ensure that the internal cache
2288 ** of the schema used when compiling the SQL query matches the schema of
2289 ** the database against which the compiled query is actually executed.
2290 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2291 ** the schema-version is potentially dangerous and may lead to program
2292 ** crashes or database corruption. Use with caution!
2294 ** The user-version is not used internally by SQLite. It may be used by
2295 ** applications for any purpose.
2297 case PragTyp_HEADER_VALUE
: {
2298 int iCookie
= pPragma
->iArg
; /* Which cookie to read or write */
2299 sqlite3VdbeUsesBtree(v
, iDb
);
2300 if( zRight
&& (pPragma
->mPragFlg
& PragFlg_ReadOnly
)==0 ){
2301 /* Write the specified cookie value */
2302 static const VdbeOpList setCookie
[] = {
2303 { OP_Transaction
, 0, 1, 0}, /* 0 */
2304 { OP_SetCookie
, 0, 0, 0}, /* 1 */
2307 sqlite3VdbeVerifyNoMallocRequired(v
, ArraySize(setCookie
));
2308 aOp
= sqlite3VdbeAddOpList(v
, ArraySize(setCookie
), setCookie
, 0);
2309 if( ONLY_IF_REALLOC_STRESS(aOp
==0) ) break;
2312 aOp
[1].p2
= iCookie
;
2313 aOp
[1].p3
= sqlite3Atoi(zRight
);
2315 if( iCookie
==BTREE_SCHEMA_VERSION
&& (db
->flags
& SQLITE_Defensive
)!=0 ){
2316 /* Do not allow the use of PRAGMA schema_version=VALUE in defensive
2317 ** mode. Change the OP_SetCookie opcode into a no-op. */
2318 aOp
[1].opcode
= OP_Noop
;
2321 /* Read the specified cookie value */
2322 static const VdbeOpList readCookie
[] = {
2323 { OP_Transaction
, 0, 0, 0}, /* 0 */
2324 { OP_ReadCookie
, 0, 1, 0}, /* 1 */
2325 { OP_ResultRow
, 1, 1, 0}
2328 sqlite3VdbeVerifyNoMallocRequired(v
, ArraySize(readCookie
));
2329 aOp
= sqlite3VdbeAddOpList(v
, ArraySize(readCookie
),readCookie
,0);
2330 if( ONLY_IF_REALLOC_STRESS(aOp
==0) ) break;
2333 aOp
[1].p3
= iCookie
;
2334 sqlite3VdbeReusable(v
);
2338 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
2340 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2342 ** PRAGMA compile_options
2344 ** Return the names of all compile-time options used in this build,
2345 ** one option per row.
2347 case PragTyp_COMPILE_OPTIONS
: {
2351 while( (zOpt
= sqlite3_compileoption_get(i
++))!=0 ){
2352 sqlite3VdbeLoadString(v
, 1, zOpt
);
2353 sqlite3VdbeAddOp2(v
, OP_ResultRow
, 1, 1);
2355 sqlite3VdbeReusable(v
);
2358 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2360 #ifndef SQLITE_OMIT_WAL
2362 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
2364 ** Checkpoint the database.
2366 case PragTyp_WAL_CHECKPOINT
: {
2367 int iBt
= (pId2
->z
?iDb
:SQLITE_MAX_DB
);
2368 int eMode
= SQLITE_CHECKPOINT_PASSIVE
;
2370 if( sqlite3StrICmp(zRight
, "full")==0 ){
2371 eMode
= SQLITE_CHECKPOINT_FULL
;
2372 }else if( sqlite3StrICmp(zRight
, "restart")==0 ){
2373 eMode
= SQLITE_CHECKPOINT_RESTART
;
2374 }else if( sqlite3StrICmp(zRight
, "truncate")==0 ){
2375 eMode
= SQLITE_CHECKPOINT_TRUNCATE
;
2379 sqlite3VdbeAddOp3(v
, OP_Checkpoint
, iBt
, eMode
, 1);
2380 sqlite3VdbeAddOp2(v
, OP_ResultRow
, 1, 3);
2385 ** PRAGMA wal_autocheckpoint
2386 ** PRAGMA wal_autocheckpoint = N
2388 ** Configure a database connection to automatically checkpoint a database
2389 ** after accumulating N frames in the log. Or query for the current value
2392 case PragTyp_WAL_AUTOCHECKPOINT
: {
2394 sqlite3_wal_autocheckpoint(db
, sqlite3Atoi(zRight
));
2397 db
->xWalCallback
==sqlite3WalDefaultHook
?
2398 SQLITE_PTR_TO_INT(db
->pWalArg
) : 0);
2404 ** PRAGMA shrink_memory
2406 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
2407 ** connection on which it is invoked to free up as much memory as it
2408 ** can, by calling sqlite3_db_release_memory().
2410 case PragTyp_SHRINK_MEMORY
: {
2411 sqlite3_db_release_memory(db
);
2417 ** PRAGMA optimize(MASK)
2418 ** PRAGMA schema.optimize
2419 ** PRAGMA schema.optimize(MASK)
2421 ** Attempt to optimize the database. All schemas are optimized in the first
2422 ** two forms, and only the specified schema is optimized in the latter two.
2424 ** The details of optimizations performed by this pragma are expected
2425 ** to change and improve over time. Applications should anticipate that
2426 ** this pragma will perform new optimizations in future releases.
2428 ** The optional argument is a bitmask of optimizations to perform:
2430 ** 0x00001 Debugging mode. Do not actually perform any optimizations
2431 ** but instead return one line of text for each optimization
2432 ** that would have been done. Off by default.
2434 ** 0x00002 Run ANALYZE on tables that might benefit. On by default.
2435 ** See below for additional information.
2437 ** 0x00010 Run all ANALYZE operations using an analysis_limit that
2438 ** is the lessor of the current analysis_limit and the
2439 ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option.
2440 ** The default value of SQLITE_DEFAULT_OPTIMIZE_LIMIT is
2441 ** currently (2024-02-19) set to 2000, which is such that
2442 ** the worst case run-time for PRAGMA optimize on a 100MB
2443 ** database will usually be less than 100 milliseconds on
2444 ** a RaspberryPI-4 class machine. On by default.
2446 ** 0x10000 Look at tables to see if they need to be reanalyzed
2447 ** due to growth or shrinkage even if they have not been
2448 ** queried during the current connection. Off by default.
2450 ** The default MASK is and always shall be 0x0fffe. In the current
2451 ** implementation, the default mask only covers the 0x00002 optimization,
2452 ** though additional optimizations that are covered by 0x0fffe might be
2453 ** added in the future. Optimizations that are off by default and must
2454 ** be explicitly requested have masks of 0x10000 or greater.
2456 ** DETERMINATION OF WHEN TO RUN ANALYZE
2458 ** In the current implementation, a table is analyzed if only if all of
2459 ** the following are true:
2461 ** (1) MASK bit 0x00002 is set.
2463 ** (2) The table is an ordinary table, not a virtual table or view.
2465 ** (3) The table name does not begin with "sqlite_".
2467 ** (4) One or more of the following is true:
2468 ** (4a) The 0x10000 MASK bit is set.
2469 ** (4b) One or more indexes on the table lacks an entry
2470 ** in the sqlite_stat1 table.
2471 ** (4c) The query planner used sqlite_stat1-style statistics for one
2472 ** or more indexes of the table at some point during the lifetime
2473 ** of the current connection.
2475 ** (5) One or more of the following is true:
2476 ** (5a) One or more indexes on the table lacks an entry
2477 ** in the sqlite_stat1 table. (Same as 4a)
2478 ** (5b) The number of rows in the table has increased or decreased by
2479 ** 10-fold. In other words, the current size of the table is
2480 ** 10 times larger than the size in sqlite_stat1 or else the
2481 ** current size is less than 1/10th the size in sqlite_stat1.
2483 ** The rules for when tables are analyzed are likely to change in
2484 ** future releases. Future versions of SQLite might accept a string
2485 ** literal argument to this pragma that contains a mnemonic description
2486 ** of the options rather than a bitmap.
2488 case PragTyp_OPTIMIZE
: {
2489 int iDbLast
; /* Loop termination point for the schema loop */
2490 int iTabCur
; /* Cursor for a table whose size needs checking */
2491 HashElem
*k
; /* Loop over tables of a schema */
2492 Schema
*pSchema
; /* The current schema */
2493 Table
*pTab
; /* A table in the schema */
2494 Index
*pIdx
; /* An index of the table */
2495 LogEst szThreshold
; /* Size threshold above which reanalysis needed */
2496 char *zSubSql
; /* SQL statement for the OP_SqlExec opcode */
2497 u32 opMask
; /* Mask of operations to perform */
2498 int nLimit
; /* Analysis limit to use */
2499 int nCheck
= 0; /* Number of tables to be optimized */
2500 int nBtree
= 0; /* Number of btrees to scan */
2501 int nIndex
; /* Number of indexes on the current table */
2504 opMask
= (u32
)sqlite3Atoi(zRight
);
2505 if( (opMask
& 0x02)==0 ) break;
2509 if( (opMask
& 0x10)==0 ){
2511 }else if( db
->nAnalysisLimit
>0
2512 && db
->nAnalysisLimit
<SQLITE_DEFAULT_OPTIMIZE_LIMIT
){
2515 nLimit
= SQLITE_DEFAULT_OPTIMIZE_LIMIT
;
2517 iTabCur
= pParse
->nTab
++;
2518 for(iDbLast
= zDb
?iDb
:db
->nDb
-1; iDb
<=iDbLast
; iDb
++){
2519 if( iDb
==1 ) continue;
2520 sqlite3CodeVerifySchema(pParse
, iDb
);
2521 pSchema
= db
->aDb
[iDb
].pSchema
;
2522 for(k
=sqliteHashFirst(&pSchema
->tblHash
); k
; k
=sqliteHashNext(k
)){
2523 pTab
= (Table
*)sqliteHashData(k
);
2525 /* This only works for ordinary tables */
2526 if( !IsOrdinaryTable(pTab
) ) continue;
2528 /* Do not scan system tables */
2529 if( 0==sqlite3StrNICmp(pTab
->zName
, "sqlite_", 7) ) continue;
2531 /* Find the size of the table as last recorded in sqlite_stat1.
2532 ** If any index is unanalyzed, then the threshold is -1 to
2533 ** indicate a new, unanalyzed index
2535 szThreshold
= pTab
->nRowLogEst
;
2537 for(pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
2539 if( !pIdx
->hasStat1
){
2540 szThreshold
= -1; /* Always analyze if any index lacks statistics */
2544 /* If table pTab has not been used in a way that would benefit from
2545 ** having analysis statistics during the current session, then skip it,
2546 ** unless the 0x10000 MASK bit is set. */
2547 if( (pTab
->tabFlags
& TF_MaybeReanalyze
)!=0 ){
2548 /* Check for size change if stat1 has been used for a query */
2549 }else if( opMask
& 0x10000 ){
2550 /* Check for size change if 0x10000 is set */
2551 }else if( pTab
->pIndex
!=0 && szThreshold
<0 ){
2552 /* Do analysis if unanalyzed indexes exists */
2554 /* Otherwise, we can skip this table */
2560 /* If ANALYZE might be invoked two or more times, hold a write
2561 ** transaction for efficiency */
2562 sqlite3BeginWriteOperation(pParse
, 0, iDb
);
2566 /* Reanalyze if the table is 10 times larger or smaller than
2567 ** the last analysis. Unconditional reanalysis if there are
2568 ** unanalyzed indexes. */
2569 sqlite3OpenTable(pParse
, iTabCur
, iDb
, pTab
, OP_OpenRead
);
2570 if( szThreshold
>=0 ){
2571 const LogEst iRange
= 33; /* 10x size change */
2572 sqlite3VdbeAddOp4Int(v
, OP_IfSizeBetween
, iTabCur
,
2573 sqlite3VdbeCurrentAddr(v
)+2+(opMask
&1),
2574 szThreshold
>=iRange
? szThreshold
-iRange
: -1,
2575 szThreshold
+iRange
);
2578 sqlite3VdbeAddOp2(v
, OP_Rewind
, iTabCur
,
2579 sqlite3VdbeCurrentAddr(v
)+2+(opMask
&1));
2582 zSubSql
= sqlite3MPrintf(db
, "ANALYZE \"%w\".\"%w\"",
2583 db
->aDb
[iDb
].zDbSName
, pTab
->zName
);
2584 if( opMask
& 0x01 ){
2585 int r1
= sqlite3GetTempReg(pParse
);
2586 sqlite3VdbeAddOp4(v
, OP_String8
, 0, r1
, 0, zSubSql
, P4_DYNAMIC
);
2587 sqlite3VdbeAddOp2(v
, OP_ResultRow
, r1
, 1);
2589 sqlite3VdbeAddOp4(v
, OP_SqlExec
, nLimit
? 0x02 : 00, nLimit
, 0,
2590 zSubSql
, P4_DYNAMIC
);
2594 sqlite3VdbeAddOp0(v
, OP_Expire
);
2596 /* In a schema with a large number of tables and indexes, scale back
2597 ** the analysis_limit to avoid excess run-time in the worst case.
2599 if( !db
->mallocFailed
&& nLimit
>0 && nBtree
>100 ){
2602 nLimit
= 100*nLimit
/nBtree
;
2603 if( nLimit
<100 ) nLimit
= 100;
2604 aOp
= sqlite3VdbeGetOp(v
, 0);
2605 iEnd
= sqlite3VdbeCurrentAddr(v
);
2606 for(iAddr
=0; iAddr
<iEnd
; iAddr
++){
2607 if( aOp
[iAddr
].opcode
==OP_SqlExec
) aOp
[iAddr
].p2
= nLimit
;
2614 ** PRAGMA busy_timeout
2615 ** PRAGMA busy_timeout = N
2617 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
2618 ** if one is set. If no busy handler or a different busy handler is set
2619 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2620 ** disables the timeout.
2622 /*case PragTyp_BUSY_TIMEOUT*/ default: {
2623 assert( pPragma
->ePragTyp
==PragTyp_BUSY_TIMEOUT
);
2625 sqlite3_busy_timeout(db
, sqlite3Atoi(zRight
));
2627 returnSingleInt(v
, db
->busyTimeout
);
2632 ** PRAGMA soft_heap_limit
2633 ** PRAGMA soft_heap_limit = N
2635 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
2636 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
2637 ** specified and is a non-negative integer.
2638 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
2639 ** returns the same integer that would be returned by the
2640 ** sqlite3_soft_heap_limit64(-1) C-language function.
2642 case PragTyp_SOFT_HEAP_LIMIT
: {
2644 if( zRight
&& sqlite3DecOrHexToI64(zRight
, &N
)==SQLITE_OK
){
2645 sqlite3_soft_heap_limit64(N
);
2647 returnSingleInt(v
, sqlite3_soft_heap_limit64(-1));
2652 ** PRAGMA hard_heap_limit
2653 ** PRAGMA hard_heap_limit = N
2655 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
2656 ** limit. The hard heap limit can be activated or lowered by this
2657 ** pragma, but not raised or deactivated. Only the
2658 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
2659 ** the hard heap limit. This allows an application to set a heap limit
2660 ** constraint that cannot be relaxed by an untrusted SQL script.
2662 case PragTyp_HARD_HEAP_LIMIT
: {
2664 if( zRight
&& sqlite3DecOrHexToI64(zRight
, &N
)==SQLITE_OK
){
2665 sqlite3_int64 iPrior
= sqlite3_hard_heap_limit64(-1);
2666 if( N
>0 && (iPrior
==0 || iPrior
>N
) ) sqlite3_hard_heap_limit64(N
);
2668 returnSingleInt(v
, sqlite3_hard_heap_limit64(-1));
2674 ** PRAGMA threads = N
2676 ** Configure the maximum number of worker threads. Return the new
2677 ** maximum, which might be less than requested.
2679 case PragTyp_THREADS
: {
2682 && sqlite3DecOrHexToI64(zRight
, &N
)==SQLITE_OK
2685 sqlite3_limit(db
, SQLITE_LIMIT_WORKER_THREADS
, (int)(N
&0x7fffffff));
2687 returnSingleInt(v
, sqlite3_limit(db
, SQLITE_LIMIT_WORKER_THREADS
, -1));
2692 ** PRAGMA analysis_limit
2693 ** PRAGMA analysis_limit = N
2695 ** Configure the maximum number of rows that ANALYZE will examine
2696 ** in each index that it looks at. Return the new limit.
2698 case PragTyp_ANALYSIS_LIMIT
: {
2701 && sqlite3DecOrHexToI64(zRight
, &N
)==SQLITE_OK
/* IMP: R-40975-20399 */
2704 db
->nAnalysisLimit
= (int)(N
&0x7fffffff);
2706 returnSingleInt(v
, db
->nAnalysisLimit
); /* IMP: R-57594-65522 */
2710 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
2712 ** Report the current state of file logs for all databases
2714 case PragTyp_LOCK_STATUS
: {
2715 static const char *const azLockName
[] = {
2716 "unlocked", "shared", "reserved", "pending", "exclusive"
2720 for(i
=0; i
<db
->nDb
; i
++){
2722 const char *zState
= "unknown";
2724 if( db
->aDb
[i
].zDbSName
==0 ) continue;
2725 pBt
= db
->aDb
[i
].pBt
;
2726 if( pBt
==0 || sqlite3BtreePager(pBt
)==0 ){
2728 }else if( sqlite3_file_control(db
, i
? db
->aDb
[i
].zDbSName
: 0,
2729 SQLITE_FCNTL_LOCKSTATE
, &j
)==SQLITE_OK
){
2730 zState
= azLockName
[j
];
2732 sqlite3VdbeMultiLoad(v
, 1, "ss", db
->aDb
[i
].zDbSName
, zState
);
2738 #if defined(SQLITE_ENABLE_CEROD)
2739 case PragTyp_ACTIVATE_EXTENSIONS
: if( zRight
){
2740 if( sqlite3StrNICmp(zRight
, "cerod-", 6)==0 ){
2741 sqlite3_activate_cerod(&zRight
[6]);
2747 } /* End of the PRAGMA switch */
2749 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
2750 ** purpose is to execute assert() statements to verify that if the
2751 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
2752 ** to the PRAGMA, the implementation has not added any OP_ResultRow
2753 ** instructions to the VM. */
2754 if( (pPragma
->mPragFlg
& PragFlg_NoColumns1
) && zRight
){
2755 sqlite3VdbeVerifyNoResultRow(v
);
2759 sqlite3DbFree(db
, zLeft
);
2760 sqlite3DbFree(db
, zRight
);
2762 #ifndef SQLITE_OMIT_VIRTUALTABLE
2763 /*****************************************************************************
2764 ** Implementation of an eponymous virtual table that runs a pragma.
2767 typedef struct PragmaVtab PragmaVtab
;
2768 typedef struct PragmaVtabCursor PragmaVtabCursor
;
2770 sqlite3_vtab base
; /* Base class. Must be first */
2771 sqlite3
*db
; /* The database connection to which it belongs */
2772 const PragmaName
*pName
; /* Name of the pragma */
2773 u8 nHidden
; /* Number of hidden columns */
2774 u8 iHidden
; /* Index of the first hidden column */
2776 struct PragmaVtabCursor
{
2777 sqlite3_vtab_cursor base
; /* Base class. Must be first */
2778 sqlite3_stmt
*pPragma
; /* The pragma statement to run */
2779 sqlite_int64 iRowid
; /* Current rowid */
2780 char *azArg
[2]; /* Value of the argument and schema */
2784 ** Pragma virtual table module xConnect method.
2786 static int pragmaVtabConnect(
2789 int argc
, const char *const*argv
,
2790 sqlite3_vtab
**ppVtab
,
2793 const PragmaName
*pPragma
= (const PragmaName
*)pAux
;
2794 PragmaVtab
*pTab
= 0;
2801 UNUSED_PARAMETER(argc
);
2802 UNUSED_PARAMETER(argv
);
2803 sqlite3StrAccumInit(&acc
, 0, zBuf
, sizeof(zBuf
), 0);
2804 sqlite3_str_appendall(&acc
, "CREATE TABLE x");
2805 for(i
=0, j
=pPragma
->iPragCName
; i
<pPragma
->nPragCName
; i
++, j
++){
2806 sqlite3_str_appendf(&acc
, "%c\"%s\"", cSep
, pragCName
[j
]);
2810 sqlite3_str_appendf(&acc
, "(\"%s\"", pPragma
->zName
);
2814 if( pPragma
->mPragFlg
& PragFlg_Result1
){
2815 sqlite3_str_appendall(&acc
, ",arg HIDDEN");
2818 if( pPragma
->mPragFlg
& (PragFlg_SchemaOpt
|PragFlg_SchemaReq
) ){
2819 sqlite3_str_appendall(&acc
, ",schema HIDDEN");
2822 sqlite3_str_append(&acc
, ")", 1);
2823 sqlite3StrAccumFinish(&acc
);
2824 assert( strlen(zBuf
) < sizeof(zBuf
)-1 );
2825 rc
= sqlite3_declare_vtab(db
, zBuf
);
2826 if( rc
==SQLITE_OK
){
2827 pTab
= (PragmaVtab
*)sqlite3_malloc(sizeof(PragmaVtab
));
2831 memset(pTab
, 0, sizeof(PragmaVtab
));
2832 pTab
->pName
= pPragma
;
2838 *pzErr
= sqlite3_mprintf("%s", sqlite3_errmsg(db
));
2841 *ppVtab
= (sqlite3_vtab
*)pTab
;
2846 ** Pragma virtual table module xDisconnect method.
2848 static int pragmaVtabDisconnect(sqlite3_vtab
*pVtab
){
2849 PragmaVtab
*pTab
= (PragmaVtab
*)pVtab
;
2854 /* Figure out the best index to use to search a pragma virtual table.
2856 ** There are not really any index choices. But we want to encourage the
2857 ** query planner to give == constraints on as many hidden parameters as
2858 ** possible, and especially on the first hidden parameter. So return a
2859 ** high cost if hidden parameters are unconstrained.
2861 static int pragmaVtabBestIndex(sqlite3_vtab
*tab
, sqlite3_index_info
*pIdxInfo
){
2862 PragmaVtab
*pTab
= (PragmaVtab
*)tab
;
2863 const struct sqlite3_index_constraint
*pConstraint
;
2867 pIdxInfo
->estimatedCost
= (double)1;
2868 if( pTab
->nHidden
==0 ){ return SQLITE_OK
; }
2869 pConstraint
= pIdxInfo
->aConstraint
;
2872 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++, pConstraint
++){
2873 if( pConstraint
->iColumn
< pTab
->iHidden
) continue;
2874 if( pConstraint
->op
!=SQLITE_INDEX_CONSTRAINT_EQ
) continue;
2875 if( pConstraint
->usable
==0 ) return SQLITE_CONSTRAINT
;
2876 j
= pConstraint
->iColumn
- pTab
->iHidden
;
2881 pIdxInfo
->estimatedCost
= (double)2147483647;
2882 pIdxInfo
->estimatedRows
= 2147483647;
2886 pIdxInfo
->aConstraintUsage
[j
].argvIndex
= 1;
2887 pIdxInfo
->aConstraintUsage
[j
].omit
= 1;
2888 pIdxInfo
->estimatedCost
= (double)20;
2889 pIdxInfo
->estimatedRows
= 20;
2892 pIdxInfo
->aConstraintUsage
[j
].argvIndex
= 2;
2893 pIdxInfo
->aConstraintUsage
[j
].omit
= 1;
2898 /* Create a new cursor for the pragma virtual table */
2899 static int pragmaVtabOpen(sqlite3_vtab
*pVtab
, sqlite3_vtab_cursor
**ppCursor
){
2900 PragmaVtabCursor
*pCsr
;
2901 pCsr
= (PragmaVtabCursor
*)sqlite3_malloc(sizeof(*pCsr
));
2902 if( pCsr
==0 ) return SQLITE_NOMEM
;
2903 memset(pCsr
, 0, sizeof(PragmaVtabCursor
));
2904 pCsr
->base
.pVtab
= pVtab
;
2905 *ppCursor
= &pCsr
->base
;
2909 /* Clear all content from pragma virtual table cursor. */
2910 static void pragmaVtabCursorClear(PragmaVtabCursor
*pCsr
){
2912 sqlite3_finalize(pCsr
->pPragma
);
2915 for(i
=0; i
<ArraySize(pCsr
->azArg
); i
++){
2916 sqlite3_free(pCsr
->azArg
[i
]);
2921 /* Close a pragma virtual table cursor */
2922 static int pragmaVtabClose(sqlite3_vtab_cursor
*cur
){
2923 PragmaVtabCursor
*pCsr
= (PragmaVtabCursor
*)cur
;
2924 pragmaVtabCursorClear(pCsr
);
2929 /* Advance the pragma virtual table cursor to the next row */
2930 static int pragmaVtabNext(sqlite3_vtab_cursor
*pVtabCursor
){
2931 PragmaVtabCursor
*pCsr
= (PragmaVtabCursor
*)pVtabCursor
;
2934 /* Increment the xRowid value */
2936 assert( pCsr
->pPragma
);
2937 if( SQLITE_ROW
!=sqlite3_step(pCsr
->pPragma
) ){
2938 rc
= sqlite3_finalize(pCsr
->pPragma
);
2940 pragmaVtabCursorClear(pCsr
);
2946 ** Pragma virtual table module xFilter method.
2948 static int pragmaVtabFilter(
2949 sqlite3_vtab_cursor
*pVtabCursor
,
2950 int idxNum
, const char *idxStr
,
2951 int argc
, sqlite3_value
**argv
2953 PragmaVtabCursor
*pCsr
= (PragmaVtabCursor
*)pVtabCursor
;
2954 PragmaVtab
*pTab
= (PragmaVtab
*)(pVtabCursor
->pVtab
);
2960 UNUSED_PARAMETER(idxNum
);
2961 UNUSED_PARAMETER(idxStr
);
2962 pragmaVtabCursorClear(pCsr
);
2963 j
= (pTab
->pName
->mPragFlg
& PragFlg_Result1
)!=0 ? 0 : 1;
2964 for(i
=0; i
<argc
; i
++, j
++){
2965 const char *zText
= (const char*)sqlite3_value_text(argv
[i
]);
2966 assert( j
<ArraySize(pCsr
->azArg
) );
2967 assert( pCsr
->azArg
[j
]==0 );
2969 pCsr
->azArg
[j
] = sqlite3_mprintf("%s", zText
);
2970 if( pCsr
->azArg
[j
]==0 ){
2971 return SQLITE_NOMEM
;
2975 sqlite3StrAccumInit(&acc
, 0, 0, 0, pTab
->db
->aLimit
[SQLITE_LIMIT_SQL_LENGTH
]);
2976 sqlite3_str_appendall(&acc
, "PRAGMA ");
2977 if( pCsr
->azArg
[1] ){
2978 sqlite3_str_appendf(&acc
, "%Q.", pCsr
->azArg
[1]);
2980 sqlite3_str_appendall(&acc
, pTab
->pName
->zName
);
2981 if( pCsr
->azArg
[0] ){
2982 sqlite3_str_appendf(&acc
, "=%Q", pCsr
->azArg
[0]);
2984 zSql
= sqlite3StrAccumFinish(&acc
);
2985 if( zSql
==0 ) return SQLITE_NOMEM
;
2986 rc
= sqlite3_prepare_v2(pTab
->db
, zSql
, -1, &pCsr
->pPragma
, 0);
2988 if( rc
!=SQLITE_OK
){
2989 pTab
->base
.zErrMsg
= sqlite3_mprintf("%s", sqlite3_errmsg(pTab
->db
));
2992 return pragmaVtabNext(pVtabCursor
);
2996 ** Pragma virtual table module xEof method.
2998 static int pragmaVtabEof(sqlite3_vtab_cursor
*pVtabCursor
){
2999 PragmaVtabCursor
*pCsr
= (PragmaVtabCursor
*)pVtabCursor
;
3000 return (pCsr
->pPragma
==0);
3003 /* The xColumn method simply returns the corresponding column from
3006 static int pragmaVtabColumn(
3007 sqlite3_vtab_cursor
*pVtabCursor
,
3008 sqlite3_context
*ctx
,
3011 PragmaVtabCursor
*pCsr
= (PragmaVtabCursor
*)pVtabCursor
;
3012 PragmaVtab
*pTab
= (PragmaVtab
*)(pVtabCursor
->pVtab
);
3013 if( i
<pTab
->iHidden
){
3014 sqlite3_result_value(ctx
, sqlite3_column_value(pCsr
->pPragma
, i
));
3016 sqlite3_result_text(ctx
, pCsr
->azArg
[i
-pTab
->iHidden
],-1,SQLITE_TRANSIENT
);
3022 ** Pragma virtual table module xRowid method.
3024 static int pragmaVtabRowid(sqlite3_vtab_cursor
*pVtabCursor
, sqlite_int64
*p
){
3025 PragmaVtabCursor
*pCsr
= (PragmaVtabCursor
*)pVtabCursor
;
3030 /* The pragma virtual table object */
3031 static const sqlite3_module pragmaVtabModule
= {
3033 0, /* xCreate - create a table */
3034 pragmaVtabConnect
, /* xConnect - connect to an existing table */
3035 pragmaVtabBestIndex
, /* xBestIndex - Determine search strategy */
3036 pragmaVtabDisconnect
, /* xDisconnect - Disconnect from a table */
3037 0, /* xDestroy - Drop a table */
3038 pragmaVtabOpen
, /* xOpen - open a cursor */
3039 pragmaVtabClose
, /* xClose - close a cursor */
3040 pragmaVtabFilter
, /* xFilter - configure scan constraints */
3041 pragmaVtabNext
, /* xNext - advance a cursor */
3042 pragmaVtabEof
, /* xEof */
3043 pragmaVtabColumn
, /* xColumn - read data */
3044 pragmaVtabRowid
, /* xRowid - read data */
3045 0, /* xUpdate - write data */
3046 0, /* xBegin - begin transaction */
3047 0, /* xSync - sync transaction */
3048 0, /* xCommit - commit transaction */
3049 0, /* xRollback - rollback transaction */
3050 0, /* xFindFunction - function overloading */
3051 0, /* xRename - rename the table */
3054 0, /* xRollbackTo */
3055 0, /* xShadowName */
3060 ** Check to see if zTabName is really the name of a pragma. If it is,
3061 ** then register an eponymous virtual table for that pragma and return
3062 ** a pointer to the Module object for the new virtual table.
3064 Module
*sqlite3PragmaVtabRegister(sqlite3
*db
, const char *zName
){
3065 const PragmaName
*pName
;
3066 assert( sqlite3_strnicmp(zName
, "pragma_", 7)==0 );
3067 pName
= pragmaLocate(zName
+7);
3068 if( pName
==0 ) return 0;
3069 if( (pName
->mPragFlg
& (PragFlg_Result0
|PragFlg_Result1
))==0 ) return 0;
3070 assert( sqlite3HashFind(&db
->aModule
, zName
)==0 );
3071 return sqlite3VtabCreateModule(db
, zName
, &pragmaVtabModule
, (void*)pName
, 0);
3074 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3076 #endif /* SQLITE_OMIT_PRAGMA */