Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 2b4d465e7166e89e8cd7c2b4042f48cb33c7d7b4 [file] [log] [blame]
drhc11d4f92003-04-06 21:08:241/*
2** 2003 April 6
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
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.
10**
11*************************************************************************
12** This file contains code used to implement the PRAGMA command.
drhc11d4f92003-04-06 21:08:2413*/
14#include "sqliteInt.h"
15
drh9ccd8652013-09-13 16:36:4616#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
17# if defined(__APPLE__)
18# define SQLITE_ENABLE_LOCKING_STYLE 1
19# else
20# define SQLITE_ENABLE_LOCKING_STYLE 0
21# endif
22#endif
23
24/***************************************************************************
drh67e65e52015-02-02 21:34:5425** 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.
larrybrbc917382023-06-07 08:40:3129** Do not edit pragma.h directly. Edit and rerun the script in at
drh67e65e52015-02-02 21:34:5430** ../tool/mkpragmatab.tcl. */
31#include "pragma.h"
drh9ccd8652013-09-13 16:36:4632
drhc11d4f92003-04-06 21:08:2433/*
drh9f34a052024-02-19 13:06:2734** 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.
38**
stephan5d60f472025-02-25 20:55:1439** The value of 2000 is chosen empirically so that the worst-case run-time
drh9f34a052024-02-19 13:06:2740** 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
drh9591a9f2024-02-21 20:21:4643** this paragraph, "worst-case" means that ANALYZE ends up being
drh9f34a052024-02-19 13:06:2744** 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.
52**
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.
56*/
57#ifndef SQLITE_DEFAULT_OPTIMIZE_LIMIT
58# define SQLITE_DEFAULT_OPTIMIZE_LIMIT 2000
59#endif
60
61/*
drhc11d4f92003-04-06 21:08:2462** Interpret the given string as a safety level. Return 0 for OFF,
larrybrbc917382023-06-07 08:40:3163** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
drh6841b1c2016-02-03 19:20:1564** unrecognized string argument. The FULL and EXTRA option is disallowed
drh908c0052012-01-30 18:40:5565** if the omitFull parameter it 1.
drhc11d4f92003-04-06 21:08:2466**
67** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:1968** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:2469** 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.
71*/
drheac5bd72014-07-25 21:35:3972static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
drh6841b1c2016-02-03 19:20:1573 /* 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 */
drh722e95a2004-10-25 20:33:4479 int i, n;
danielk197778ca0e72009-01-20 16:53:3980 if( sqlite3Isdigit(*z) ){
drh60ac3f42010-11-23 18:59:2781 return (u8)sqlite3Atoi(z);
drhc11d4f92003-04-06 21:08:2482 }
drhea678832008-12-10 19:26:2283 n = sqlite3Strlen30(z);
drh6841b1c2016-02-03 19:20:1584 for(i=0; i<ArraySize(iLength); i++){
85 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
86 && (!omitFull || iValue[i]<=1)
87 ){
drh722e95a2004-10-25 20:33:4488 return iValue[i];
89 }
drhc11d4f92003-04-06 21:08:2490 }
drh908c0052012-01-30 18:40:5591 return dflt;
drhc11d4f92003-04-06 21:08:2492}
93
94/*
drh722e95a2004-10-25 20:33:4495** Interpret the given string as a boolean value.
96*/
drheac5bd72014-07-25 21:35:3997u8 sqlite3GetBoolean(const char *z, u8 dflt){
drh38d9c612012-01-31 14:24:4798 return getSafetyLevel(z,1,dflt)!=0;
drh722e95a2004-10-25 20:33:4499}
100
drhc7dc9bf2011-06-03 13:02:57101/* 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.
104*/
105#if !defined(SQLITE_OMIT_PRAGMA)
106
danielk197741483462007-03-24 16:45:04107/*
108** Interpret the given string as a locking mode value.
109*/
110static int getLockingMode(const char *z){
111 if( z ){
112 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
113 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
114 }
115 return PAGER_LOCKINGMODE_QUERY;
116}
117
danielk1977dddbcdc2007-04-26 14:42:34118#ifndef SQLITE_OMIT_AUTOVACUUM
119/*
120** Interpret the given string as an auto-vacuum mode value.
121**
larrybrbc917382023-06-07 08:40:31122** The following strings, "none", "full" and "incremental" are
danielk1977dddbcdc2007-04-26 14:42:34123** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
124*/
125static int getAutoVacuum(const char *z){
126 int i;
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;
drh60ac3f42010-11-23 18:59:27130 i = sqlite3Atoi(z);
drh4f21c4a2008-12-10 22:15:00131 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34132}
133#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
134
danielk1977b84f96f2005-01-20 11:32:23135#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44136/*
drh90f5ecb2004-07-22 01:19:35137** 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.
140*/
141static int getTempStore(const char *z){
142 if( z[0]>='0' && z[0]<='2' ){
143 return z[0] - '0';
144 }else if( sqlite3StrICmp(z, "file")==0 ){
145 return 1;
146 }else if( sqlite3StrICmp(z, "memory")==0 ){
147 return 2;
148 }else{
149 return 0;
150 }
151}
drhbf216272005-02-26 18:10:44152#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35153
drhbf216272005-02-26 18:10:44154#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35155/*
tpoindex9a09a3c2004-12-20 19:01:32156** Invalidate temp storage, either when the temp storage is changed
157** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35158*/
tpoindex9a09a3c2004-12-20 19:01:32159static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11160 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35161 if( db->aDb[1].pBt!=0 ){
drh99744fa2020-08-25 19:09:07162 if( !db->autoCommit
163 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
164 ){
drh90f5ecb2004-07-22 01:19:35165 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
166 "from within a transaction");
167 return SQLITE_ERROR;
168 }
169 sqlite3BtreeClose(db->aDb[1].pBt);
170 db->aDb[1].pBt = 0;
drh81028a42012-05-15 18:28:27171 sqlite3ResetAllSchemasOfConnection(db);
drh90f5ecb2004-07-22 01:19:35172 }
tpoindex9a09a3c2004-12-20 19:01:32173 return SQLITE_OK;
174}
drhbf216272005-02-26 18:10:44175#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32176
drhbf216272005-02-26 18:10:44177#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32178/*
179** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12180** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32181** or DEFAULT_TEMP_STORE pragmas.
182*/
183static 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 ){
188 return SQLITE_ERROR;
189 }
drh4f21c4a2008-12-10 22:15:00190 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35191 return SQLITE_OK;
192}
drhbf216272005-02-26 18:10:44193#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35194
195/*
drhc232aca2016-12-15 16:01:17196** Set result column names for a pragma.
drhb460e522015-09-03 03:29:51197*/
drhc232aca2016-12-15 16:01:17198static void setPragmaResultColumnNames(
drh2fcc1592016-12-15 20:59:03199 Vdbe *v, /* The query under construction */
200 const PragmaName *pPragma /* The pragma */
drhb460e522015-09-03 03:29:51201){
drhc232aca2016-12-15 16:01:17202 u8 n = pPragma->nPragCName;
203 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
204 if( n==0 ){
205 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
206 }else{
207 int i, j;
208 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
209 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
210 }
drhb460e522015-09-03 03:29:51211 }
212}
drhb460e522015-09-03 03:29:51213
214/*
drh90f5ecb2004-07-22 01:19:35215** Generate code to return a single integer value.
216*/
drhc232aca2016-12-15 16:01:17217static void returnSingleInt(Vdbe *v, i64 value){
drh7cc023c2015-09-03 04:28:25218 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
drh7cc023c2015-09-03 04:28:25219 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
220}
221
222/*
223** Generate code to return a single text value.
224*/
225static void returnSingleText(
226 Vdbe *v, /* Prepared statement under construction */
drh7cc023c2015-09-03 04:28:25227 const char *zValue /* Value to be returned */
228){
229 if( zValue ){
drh076e85f2015-09-03 13:46:12230 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
drh7cc023c2015-09-03 04:28:25231 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
232 }
drh90f5ecb2004-07-22 01:19:35233}
234
drhd3605a42013-08-17 15:42:29235
236/*
237** Set the safety_level and pager flags for pager iDb. Or if iDb<0
238** set these values for all pagers.
239*/
240#ifndef SQLITE_OMIT_PAGER_PRAGMAS
241static void setAllPagerFlags(sqlite3 *db){
242 if( db->autoCommit ){
243 Db *pDb = db->aDb;
244 int n = db->nDb;
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 );
251 while( (n--) > 0 ){
252 if( pDb->pBt ){
253 sqlite3BtreeSetPagerFlags(pDb->pBt,
254 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
255 }
256 pDb++;
257 }
258 }
259}
drhfeb56e02013-08-23 17:33:46260#else
261# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29262#endif
263
264
drhd2cb50b2009-01-09 21:41:17265/*
266** Return a human-readable name for a constraint resolution action.
267*/
danba9108b2009-09-22 07:13:42268#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21269static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17270 const char *zName;
danielk197750af3e12008-10-10 17:47:21271 switch( action ){
dan1da40a32009-09-19 17:00:31272 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;
larrybrbc917382023-06-07 08:40:31276 default: zName = "NO ACTION";
dan1da40a32009-09-19 17:00:31277 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21278 }
drhd2cb50b2009-01-09 21:41:17279 return zName;
danielk197750af3e12008-10-10 17:47:21280}
danba9108b2009-09-22 07:13:42281#endif
danielk197750af3e12008-10-10 17:47:21282
dane04dc882010-04-20 18:53:15283
284/*
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.
288*/
289const char *sqlite3JournalModename(int eMode){
290 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20291 "delete", "persist", "off", "truncate", "memory"
292#ifndef SQLITE_OMIT_WAL
293 , "wal"
294#endif
dane04dc882010-04-20 18:53:15295 };
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) );
303
304 if( eMode==ArraySize(azModeName) ) return 0;
305 return azModeName[eMode];
306}
307
drh87223182004-02-21 14:00:29308/*
drh2fcc1592016-12-15 20:59:03309** Locate a pragma in the aPragmaName[] array.
310*/
311static const PragmaName *pragmaLocate(const char *zName){
mistachkin2e525322017-02-01 22:43:08312 int upr, lwr, mid = 0, rc;
drh2fcc1592016-12-15 20:59:03313 lwr = 0;
314 upr = ArraySize(aPragmaName)-1;
315 while( lwr<=upr ){
316 mid = (lwr+upr)/2;
317 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
318 if( rc==0 ) break;
319 if( rc<0 ){
320 upr = mid - 1;
321 }else{
322 lwr = mid + 1;
323 }
324 }
325 return lwr>upr ? 0 : &aPragmaName[mid];
326}
327
328/*
drh79d5bc82020-01-04 01:43:02329** Create zero or more entries in the output for the SQL functions
330** defined by FuncDef p.
331*/
drh337ca512020-01-04 19:58:28332static 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 */
337){
larrybrbc917382023-06-07 08:40:31338 u32 mask =
drh9606a132022-02-25 01:23:17339 SQLITE_DETERMINISTIC |
340 SQLITE_DIRECTONLY |
341 SQLITE_SUBTYPE |
342 SQLITE_INNOCUOUS |
343 SQLITE_FUNC_INTERNAL
344 ;
drhceff7612022-03-02 01:02:16345 if( showInternFuncs ) mask = 0xffffffff;
drh79d5bc82020-01-04 01:43:02346 for(; p; p=p->pNext){
347 const char *zType;
drhb84fda32020-01-09 16:28:50348 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
349
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 );
drh337ca512020-01-04 19:58:28354
drh79d5bc82020-01-04 01:43:02355 if( p->xSFunc==0 ) continue;
drh337ca512020-01-04 19:58:28356 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
357 && showInternFuncs==0
358 ){
359 continue;
larrybrbc917382023-06-07 08:40:31360 }
drh79d5bc82020-01-04 01:43:02361 if( p->xValue!=0 ){
362 zType = "w";
363 }else if( p->xFinalize!=0 ){
364 zType = "a";
365 }else{
366 zType = "s";
367 }
drh79d5bc82020-01-04 01:43:02368 sqlite3VdbeMultiLoad(v, 1, "sissii",
369 p->zName, isBuiltin,
drhb84fda32020-01-09 16:28:50370 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
drh79d5bc82020-01-04 01:43:02371 p->nArg,
372 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
373 );
374 }
375}
376
377
378/*
drh66accfc2017-02-22 18:04:42379** Helper subroutine for PRAGMA integrity_check:
380**
drh9ecd7082017-09-10 01:06:05381** 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.
drh66accfc2017-02-22 18:04:42384*/
drh9ecd7082017-09-10 01:06:05385static int integrityCheckResultRow(Vdbe *v){
drh66accfc2017-02-22 18:04:42386 int addr;
drh9ecd7082017-09-10 01:06:05387 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh66accfc2017-02-22 18:04:42388 addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
389 VdbeCoverage(v);
drh9ecd7082017-09-10 01:06:05390 sqlite3VdbeAddOp0(v, OP_Halt);
drh66accfc2017-02-22 18:04:42391 return addr;
392}
393
394/*
larrybrbc917382023-06-07 08:40:31395** Process a pragma statement.
drhc11d4f92003-04-06 21:08:24396**
397** Pragmas are of this form:
398**
drh9b0cf342015-11-12 14:57:19399** PRAGMA [schema.]id [= value]
drhc11d4f92003-04-06 21:08:24400**
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.
drh90f5ecb2004-07-22 01:19:35404**
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.
drhc11d4f92003-04-06 21:08:24408*/
danielk197791cf71b2004-06-26 06:37:06409void sqlite3Pragma(
larrybrbc917382023-06-07 08:40:31410 Parse *pParse,
drh9b0cf342015-11-12 14:57:19411 Token *pId1, /* First part of [schema.]id field */
412 Token *pId2, /* Second part of [schema.]id field, or NULL */
danielk197791cf71b2004-06-26 06:37:06413 Token *pValue, /* Token for <value>, or NULL */
414 int minusFlag /* True if a '-' sign preceded <value> */
415){
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 */
drh3fa97302012-02-22 16:58:36420 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46421 int iDb; /* Database index for <database> */
drh06fd5d62012-02-22 14:45:19422 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
423 sqlite3 *db = pParse->db; /* The database connection */
424 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18425 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drh2fcc1592016-12-15 20:59:03426 const PragmaName *pPragma; /* The pragma */
drh06fd5d62012-02-22 14:45:19427
drhc11d4f92003-04-06 21:08:24428 if( v==0 ) return;
drh4611d922010-02-25 14:47:01429 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13430 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24431
drh9b0cf342015-11-12 14:57:19432 /* Interpret the [schema.] part of the pragma statement. iDb is the
danielk197791cf71b2004-06-26 06:37:06433 ** index of the database this pragma is being applied to in db.aDb[]. */
434 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
435 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35436 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06437
larrybrbc917382023-06-07 08:40:31438 /* If the temp database has been explicitly named as part of the
439 ** pragma, make sure it is open.
danielk1977ddfb2f02006-02-17 12:25:14440 */
441 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
442 return;
443 }
444
drh17435752007-08-16 04:30:38445 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22446 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24447 if( minusFlag ){
drh17435752007-08-16 04:30:38448 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24449 }else{
drh17435752007-08-16 04:30:38450 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24451 }
danielk197791cf71b2004-06-26 06:37:06452
drhd2cb50b2009-01-09 21:41:17453 assert( pId2 );
drh69c33822016-08-18 14:33:11454 zDb = pId2->n>0 ? pDb->zDbSName : 0;
danielk197791cf71b2004-06-26 06:37:06455 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01456 goto pragma_out;
drhc11d4f92003-04-06 21:08:24457 }
drh06fd5d62012-02-22 14:45:19458
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.
drh8dd7a6a2015-03-06 04:37:26462 **
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
466 ** statement refers.
467 **
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
472 ** argument.
drh06fd5d62012-02-22 14:45:19473 */
drh3fa97302012-02-22 16:58:36474 aFcntl[0] = 0;
475 aFcntl[1] = zLeft;
476 aFcntl[2] = zRight;
477 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33478 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19479 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
480 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17481 sqlite3VdbeSetNumCols(v, 1);
482 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
483 returnSingleText(v, aFcntl[0]);
drh7cc023c2015-09-03 04:28:25484 sqlite3_free(aFcntl[0]);
drh9ccd8652013-09-13 16:36:46485 goto pragma_out;
486 }
487 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17488 if( aFcntl[0] ){
489 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
490 sqlite3_free(aFcntl[0]);
491 }
492 pParse->nErr++;
493 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46494 goto pragma_out;
495 }
496
497 /* Locate the pragma in the lookup table */
drh2fcc1592016-12-15 20:59:03498 pPragma = pragmaLocate(zLeft);
drhbc98f902021-10-14 17:30:32499 if( pPragma==0 ){
500 /* IMP: R-43042-22504 No error messages are generated if an
501 ** unknown pragma is issued. */
502 goto pragma_out;
503 }
drh9ccd8652013-09-13 16:36:46504
drhf63936e2013-10-03 14:08:07505 /* Make sure the database schema is loaded if the pragma requires that */
drhc232aca2016-12-15 16:01:17506 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
drhf63936e2013-10-03 14:08:07507 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
508 }
509
drhc232aca2016-12-15 16:01:17510 /* Register the result column names for pragmas that return results */
larrybrbc917382023-06-07 08:40:31511 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
dan9e1ab1a2017-01-05 19:32:48512 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
513 ){
drhc232aca2016-12-15 16:01:17514 setPragmaResultColumnNames(v, pPragma);
515 }
516
drh9ccd8652013-09-13 16:36:46517 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01518 switch( pPragma->ePragTyp ){
larrybrbc917382023-06-07 08:40:31519
drhe73c9142011-11-09 16:12:24520#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24521 /*
drh9b0cf342015-11-12 14:57:19522 ** PRAGMA [schema.]default_cache_size
523 ** PRAGMA [schema.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24524 **
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.
530 **
drh93791ea2010-04-26 17:36:35531 ** 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.
drhc11d4f92003-04-06 21:08:24536 */
drh9ccd8652013-09-13 16:36:46537 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09538 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16539 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33540 { OP_Transaction, 0, 0, 0}, /* 0 */
541 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18542 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38543 { OP_Integer, 0, 2, 0},
544 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18545 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33546 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18547 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38548 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24549 };
drh2ce18652016-01-16 20:50:21550 VdbeOp *aOp;
drhfb982642007-08-30 01:19:59551 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06552 if( !zRight ){
drh3c84ddf2008-01-09 02:15:38553 pParse->nMem += 2;
drhdad300d2016-01-18 00:20:26554 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
drh2ce18652016-01-16 20:50:21555 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
drhdad300d2016-01-18 00:20:26556 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21557 aOp[0].p1 = iDb;
558 aOp[1].p1 = iDb;
559 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
drhc11d4f92003-04-06 21:08:24560 }else{
drhd50ffc42011-03-08 02:38:28561 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06562 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh1861afc2016-02-01 21:48:34563 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
drh21206082011-04-04 18:22:02564 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04565 pDb->pSchema->cache_size = size;
566 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24567 }
drh9ccd8652013-09-13 16:36:46568 break;
569 }
drhe73c9142011-11-09 16:12:24570#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24571
drhe73c9142011-11-09 16:12:24572#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24573 /*
drh9b0cf342015-11-12 14:57:19574 ** PRAGMA [schema.]page_size
575 ** PRAGMA [schema.]page_size=N
drh90f5ecb2004-07-22 01:19:35576 **
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.
581 */
drh9ccd8652013-09-13 16:36:46582 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35583 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17584 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35585 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17586 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drhc232aca2016-12-15 16:01:17587 returnSingleInt(v, size);
drh90f5ecb2004-07-22 01:19:35588 }else{
danielk1977992772c2007-08-30 10:07:38589 /* Malloc may fail when setting the page-size, as there is an internal
590 ** buffer that the pager module resizes using sqlite3_realloc().
591 */
drh60ac3f42010-11-23 18:59:27592 db->nextPagesize = sqlite3Atoi(zRight);
drhe937df82020-05-07 01:56:57593 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
drh4a642b62016-02-05 01:55:27594 sqlite3OomFault(db);
danielk1977992772c2007-08-30 10:07:38595 }
drh90f5ecb2004-07-22 01:19:35596 }
drh9ccd8652013-09-13 16:36:46597 break;
598 }
danielk197741483462007-03-24 16:45:04599
600 /*
drh9b0cf342015-11-12 14:57:19601 ** PRAGMA [schema.]secure_delete
drha5907a82017-06-19 11:44:22602 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
drh5b47efa2010-02-12 18:18:39603 **
604 ** The first form reports the current setting for the
605 ** secure_delete flag. The second form changes the secure_delete
drha5907a82017-06-19 11:44:22606 ** flag setting and reports the new value.
drh5b47efa2010-02-12 18:18:39607 */
drh9ccd8652013-09-13 16:36:46608 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39609 Btree *pBt = pDb->pBt;
610 int b = -1;
611 assert( pBt!=0 );
612 if( zRight ){
drha5907a82017-06-19 11:44:22613 if( sqlite3_stricmp(zRight, "fast")==0 ){
614 b = 2;
615 }else{
616 b = sqlite3GetBoolean(zRight, 0);
617 }
drh5b47efa2010-02-12 18:18:39618 }
drhaf034ed2010-02-12 19:46:26619 if( pId2->n==0 && b>=0 ){
620 int ii;
621 for(ii=0; ii<db->nDb; ii++){
622 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
623 }
624 }
drh5b47efa2010-02-12 18:18:39625 b = sqlite3BtreeSecureDelete(pBt, b);
drhc232aca2016-12-15 16:01:17626 returnSingleInt(v, b);
drh9ccd8652013-09-13 16:36:46627 break;
628 }
drh5b47efa2010-02-12 18:18:39629
630 /*
drh9b0cf342015-11-12 14:57:19631 ** PRAGMA [schema.]max_page_count
632 ** PRAGMA [schema.]max_page_count=N
drh60ac3f42010-11-23 18:59:27633 **
634 ** The first form reports the current setting for the
larrybrbc917382023-06-07 08:40:31635 ** maximum number of pages in the database file. The
drh60ac3f42010-11-23 18:59:27636 ** second form attempts to change this setting. Both
637 ** forms return the current setting.
638 **
drhe73c9142011-11-09 16:12:24639 ** 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.
642 **
drh9b0cf342015-11-12 14:57:19643 ** PRAGMA [schema.]page_count
danielk197759a93792008-05-15 17:48:20644 **
645 ** Return the number of pages in the specified database.
646 */
drh9ccd8652013-09-13 16:36:46647 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20648 int iReg;
drhe9261db2020-07-20 12:47:32649 i64 x = 0;
danielk197759a93792008-05-15 17:48:20650 sqlite3CodeVerifySchema(pParse, iDb);
651 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01652 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27653 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
654 }else{
drhe9261db2020-07-20 12:47:32655 if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){
656 if( x<0 ) x = 0;
657 else if( x>0xfffffffe ) x = 0xfffffffe;
658 }else{
659 x = 0;
660 }
661 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, (int)x);
drh60ac3f42010-11-23 18:59:27662 }
danielk197759a93792008-05-15 17:48:20663 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
drh9ccd8652013-09-13 16:36:46664 break;
665 }
danielk197759a93792008-05-15 17:48:20666
667 /*
drh9b0cf342015-11-12 14:57:19668 ** PRAGMA [schema.]locking_mode
669 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
danielk197741483462007-03-24 16:45:04670 */
drh9ccd8652013-09-13 16:36:46671 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04672 const char *zRet = "normal";
673 int eMode = getLockingMode(zRight);
674
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).
679 */
680 eMode = db->dfltLockMode;
681 }else{
682 Pager *pPager;
683 if( pId2->n==0 ){
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.
687 **
688 ** Also, the sqlite3.dfltLockMode variable is set so that
689 ** any subsequently attached databases also use the specified
690 ** locking mode.
691 */
692 int ii;
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);
697 }
drh4f21c4a2008-12-10 22:15:00698 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04699 }
700 pPager = sqlite3BtreePager(pDb->pBt);
701 eMode = sqlite3PagerLockingMode(pPager, eMode);
702 }
703
drh9ccd8652013-09-13 16:36:46704 assert( eMode==PAGER_LOCKINGMODE_NORMAL
705 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04706 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
707 zRet = "exclusive";
708 }
drhc232aca2016-12-15 16:01:17709 returnSingleText(v, zRet);
drh9ccd8652013-09-13 16:36:46710 break;
711 }
drh3b020132008-04-17 17:02:01712
713 /*
drh9b0cf342015-11-12 14:57:19714 ** PRAGMA [schema.]journal_mode
715 ** PRAGMA [schema.]journal_mode =
drh3ebaee92010-05-06 21:37:22716 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01717 */
drh9ccd8652013-09-13 16:36:46718 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37719 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
720 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15721
drh3b020132008-04-17 17:02:01722 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37723 /* If there is no "=MODE" part of the pragma, do a query for the
724 ** current mode */
drh3b020132008-04-17 17:02:01725 eMode = PAGER_JOURNALMODE_QUERY;
726 }else{
dane04dc882010-04-20 18:53:15727 const char *zMode;
drhea678832008-12-10 19:26:22728 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09729 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15730 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
731 }
732 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37733 /* If the "=MODE" part does not match any known journal mode,
734 ** then do a query */
dane04dc882010-04-20 18:53:15735 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01736 }
drh6c35b302019-05-17 20:37:17737 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;
741 }
drh3b020132008-04-17 17:02:01742 }
drhc6b2a0f2010-07-08 17:40:37743 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
744 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
745 iDb = 0;
746 pId2->n = 1;
747 }
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);
dane04dc882010-04-20 18:53:15752 }
drh3b020132008-04-17 17:02:01753 }
drh3b020132008-04-17 17:02:01754 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46755 break;
756 }
danielk1977b53e4962008-06-04 06:45:59757
758 /*
drh9b0cf342015-11-12 14:57:19759 ** PRAGMA [schema.]journal_size_limit
760 ** PRAGMA [schema.]journal_size_limit=N
danielk1977b53e4962008-06-04 06:45:59761 **
drha9e364f2009-01-13 20:14:15762 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59763 */
drh9ccd8652013-09-13 16:36:46764 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59765 Pager *pPager = sqlite3BtreePager(pDb->pBt);
766 i64 iLimit = -2;
767 if( zRight ){
drh9296c182014-07-23 13:40:49768 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32769 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59770 }
771 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drhc232aca2016-12-15 16:01:17772 returnSingleInt(v, iLimit);
drh9ccd8652013-09-13 16:36:46773 break;
774 }
danielk1977b53e4962008-06-04 06:45:59775
drh13d70422004-11-13 15:59:14776#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35777
778 /*
drh9b0cf342015-11-12 14:57:19779 ** PRAGMA [schema.]auto_vacuum
780 ** PRAGMA [schema.]auto_vacuum=N
danielk1977951af802004-11-05 15:45:09781 **
drha9e364f2009-01-13 20:14:15782 ** Get or set the value of the database 'auto-vacuum' parameter.
783 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09784 */
785#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46786 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09787 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17788 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09789 if( !zRight ){
drhc232aca2016-12-15 16:01:17790 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09791 }else{
danielk1977dddbcdc2007-04-26 14:42:34792 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17793 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00794 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07795 /* 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.
799 */
800 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
801 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
larrybrbc917382023-06-07 08:40:31802 /* When setting the auto_vacuum mode to either "full" or
drhf63936e2013-10-03 14:08:07803 ** "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.
danielk197727b1f952007-06-25 08:16:58806 */
drhb06a4ec2014-03-10 18:03:09807 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07808 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 */
drh1861afc2016-02-01 21:48:34813 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
drhf63936e2013-10-03 14:08:07814 };
drh2ce18652016-01-16 20:50:21815 VdbeOp *aOp;
816 int iAddr = sqlite3VdbeCurrentAddr(v);
drhdad300d2016-01-18 00:20:26817 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
drh2ce18652016-01-16 20:50:21818 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhdad300d2016-01-18 00:20:26819 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21820 aOp[0].p1 = iDb;
821 aOp[1].p1 = iDb;
822 aOp[2].p2 = iAddr+4;
drh1861afc2016-02-01 21:48:34823 aOp[4].p1 = iDb;
824 aOp[4].p3 = eAuto - 1;
drhf63936e2013-10-03 14:08:07825 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34826 }
danielk1977951af802004-11-05 15:45:09827 }
drh9ccd8652013-09-13 16:36:46828 break;
829 }
danielk1977951af802004-11-05 15:45:09830#endif
831
drhca5557f2007-05-04 18:30:40832 /*
drh9b0cf342015-11-12 14:57:19833 ** PRAGMA [schema.]incremental_vacuum(N)
drhca5557f2007-05-04 18:30:40834 **
835 ** Do N steps of incremental vacuuming on a database.
836 */
837#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46838 case PragTyp_INCREMENTAL_VACUUM: {
drh00946d72022-04-01 16:22:41839 int iLimit = 0, addr;
drhca5557f2007-05-04 18:30:40840 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
841 iLimit = 0x7fffffff;
842 }
843 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03844 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43845 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11846 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10847 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43848 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40849 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46850 break;
851 }
drhca5557f2007-05-04 18:30:40852#endif
853
drh13d70422004-11-13 15:59:14854#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09855 /*
drh9b0cf342015-11-12 14:57:19856 ** PRAGMA [schema.]cache_size
857 ** PRAGMA [schema.]cache_size=N
drhc11d4f92003-04-06 21:08:24858 **
859 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04860 ** 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
864 ** of memory.
drhc11d4f92003-04-06 21:08:24865 */
drh9ccd8652013-09-13 16:36:46866 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02867 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06868 if( !zRight ){
drhc232aca2016-12-15 16:01:17869 returnSingleInt(v, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24870 }else{
drh3b42abb2011-11-09 14:23:04871 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04872 pDb->pSchema->cache_size = size;
873 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24874 }
drh9ccd8652013-09-13 16:36:46875 break;
876 }
drhc11d4f92003-04-06 21:08:24877
878 /*
drh9b0cf342015-11-12 14:57:19879 ** PRAGMA [schema.]cache_spill
880 ** PRAGMA cache_spill=BOOLEAN
881 ** PRAGMA [schema.]cache_spill=N
882 **
883 ** The first form reports the current local setting for the
884 ** page cache spill size. The second form turns cache spill on
larrybrbc917382023-06-07 08:40:31885 ** or off. When turning cache spill on, the size is set to the
drh9b0cf342015-11-12 14:57:19886 ** 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
891 ** of memory.
892 **
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.
896 **
897 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
898 ** not just the schema specified.
899 */
900 case PragTyp_CACHE_SPILL: {
drh9b0cf342015-11-12 14:57:19901 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
902 if( !zRight ){
drhc232aca2016-12-15 16:01:17903 returnSingleInt(v,
larrybrbc917382023-06-07 08:40:31904 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
drh9b0cf342015-11-12 14:57:19905 sqlite3BtreeSetSpillSize(pDb->pBt,0));
906 }else{
drh4f9c8ec2015-11-12 15:47:48907 int size = 1;
drh9b0cf342015-11-12 14:57:19908 if( sqlite3GetInt32(zRight, &size) ){
909 sqlite3BtreeSetSpillSize(pDb->pBt, size);
910 }
drh4f9c8ec2015-11-12 15:47:48911 if( sqlite3GetBoolean(zRight, size!=0) ){
drh9b0cf342015-11-12 14:57:19912 db->flags |= SQLITE_CacheSpill;
913 }else{
drhd5b44d62018-12-06 17:06:02914 db->flags &= ~(u64)SQLITE_CacheSpill;
drh9b0cf342015-11-12 14:57:19915 }
drh4f9c8ec2015-11-12 15:47:48916 setAllPagerFlags(db);
drh9b0cf342015-11-12 14:57:19917 }
918 break;
919 }
920
921 /*
922 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06923 **
drh0d0614b2013-03-25 23:09:28924 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06925 ** 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
drha1f42c72013-04-01 22:38:06927 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42928 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06929 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06930 **
drh0d0614b2013-03-25 23:09:28931 ** 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.
dan5d8a1372013-03-19 19:28:06934 */
drh9ccd8652013-09-13 16:36:46935 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42936 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24937#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06938 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28939 if( zRight ){
drha1f42c72013-04-01 22:38:06940 int ii;
drh9296c182014-07-23 13:40:49941 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42942 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
943 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06944 for(ii=db->nDb-1; ii>=0; ii--){
945 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42946 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06947 }
948 }
dan5d8a1372013-03-19 19:28:06949 }
drh9b4c59f2013-04-15 17:03:42950 sz = -1;
dan3719f5f2013-05-23 10:13:18951 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24952#else
dan3719f5f2013-05-23 10:13:18953 sz = 0;
mistachkine98844f2013-08-24 00:59:24954 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49955#endif
dan3719f5f2013-05-23 10:13:18956 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17957 returnSingleInt(v, sz);
dan3719f5f2013-05-23 10:13:18958 }else if( rc!=SQLITE_NOTFOUND ){
959 pParse->nErr++;
960 pParse->rc = rc;
drh34f74902013-04-03 13:09:18961 }
drh9ccd8652013-09-13 16:36:46962 break;
963 }
dan5d8a1372013-03-19 19:28:06964
965 /*
drh90f5ecb2004-07-22 01:19:35966 ** PRAGMA temp_store
967 ** PRAGMA temp_store = "default"|"memory"|"file"
968 **
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.
972 **
973 ** Note that it is possible for the library compile-time options to
974 ** override this setting
975 */
drh9ccd8652013-09-13 16:36:46976 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35977 if( !zRight ){
drhc232aca2016-12-15 16:01:17978 returnSingleInt(v, db->temp_store);
drh90f5ecb2004-07-22 01:19:35979 }else{
980 changeTempStorage(pParse, zRight);
981 }
drh9ccd8652013-09-13 16:36:46982 break;
983 }
drh90f5ecb2004-07-22 01:19:35984
985 /*
tpoindex9a09a3c2004-12-20 19:01:32986 ** PRAGMA temp_store_directory
987 ** PRAGMA temp_store_directory = ""|"directory_name"
988 **
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.
993 **
994 */
drh9ccd8652013-09-13 16:36:46995 case PragTyp_TEMP_STORE_DIRECTORY: {
drh18a3a482022-09-02 00:36:16996 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
tpoindex9a09a3c2004-12-20 19:01:32997 if( !zRight ){
drhc232aca2016-12-15 16:01:17998 returnSingleText(v, sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32999 }else{
drh78f82d12008-09-02 00:52:521000#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:111001 if( zRight[0] ){
1002 int res;
danielk1977fab11272008-09-16 14:38:021003 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1004 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:111005 sqlite3ErrorMsg(pParse, "not a writable directory");
drh18a3a482022-09-02 00:36:161006 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
danielk1977861f7452008-06-05 11:39:111007 goto pragma_out;
1008 }
drh268283b2005-01-08 15:44:251009 }
danielk1977b06a0b62008-06-26 10:54:121010 if( SQLITE_TEMP_STORE==0
1011 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
1012 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:251013 ){
1014 invalidateTempStorage(pParse);
1015 }
drh17435752007-08-16 04:30:381016 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:251017 if( zRight[0] ){
drhb9755982010-07-24 16:34:371018 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:321019 }else{
drh268283b2005-01-08 15:44:251020 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:321021 }
drh78f82d12008-09-02 00:52:521022#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:321023 }
drh18a3a482022-09-02 00:36:161024 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
drh9ccd8652013-09-13 16:36:461025 break;
1026 }
tpoindex9a09a3c2004-12-20 19:01:321027
drhcc716452012-06-06 23:23:231028#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:011029 /*
1030 ** PRAGMA data_store_directory
1031 ** PRAGMA data_store_directory = ""|"directory_name"
1032 **
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.
1040 **
1041 */
drh9ccd8652013-09-13 16:36:461042 case PragTyp_DATA_STORE_DIRECTORY: {
drh18a3a482022-09-02 00:36:161043 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
mistachkina112d142012-03-14 00:44:011044 if( !zRight ){
drhc232aca2016-12-15 16:01:171045 returnSingleText(v, sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:011046 }else{
1047#ifndef SQLITE_OMIT_WSD
1048 if( zRight[0] ){
1049 int res;
1050 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1051 if( rc!=SQLITE_OK || res==0 ){
1052 sqlite3ErrorMsg(pParse, "not a writable directory");
drh18a3a482022-09-02 00:36:161053 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
mistachkina112d142012-03-14 00:44:011054 goto pragma_out;
1055 }
1056 }
1057 sqlite3_free(sqlite3_data_directory);
1058 if( zRight[0] ){
1059 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1060 }else{
1061 sqlite3_data_directory = 0;
1062 }
1063#endif /* SQLITE_OMIT_WSD */
1064 }
drh18a3a482022-09-02 00:36:161065 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
drh9ccd8652013-09-13 16:36:461066 break;
1067 }
drhcc716452012-06-06 23:23:231068#endif
mistachkina112d142012-03-14 00:44:011069
drhd2cb50b2009-01-09 21:41:171070#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:321071 /*
drh9b0cf342015-11-12 14:57:191072 ** PRAGMA [schema.]lock_proxy_file
1073 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:461074 **
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.
1077 **
1078 */
1079 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:351080 if( !zRight ){
1081 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1082 char *proxy_file_path = NULL;
1083 sqlite3_file *pFile = sqlite3PagerFile(pPager);
larrybrbc917382023-06-07 08:40:311084 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:351085 &proxy_file_path);
drhc232aca2016-12-15 16:01:171086 returnSingleText(v, proxy_file_path);
aswiftaebf4132008-11-21 00:10:351087 }else{
1088 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1089 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1090 int res;
1091 if( zRight[0] ){
larrybrbc917382023-06-07 08:40:311092 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:351093 zRight);
1094 } else {
larrybrbc917382023-06-07 08:40:311095 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:351096 NULL);
1097 }
1098 if( res!=SQLITE_OK ){
1099 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1100 goto pragma_out;
1101 }
1102 }
drh9ccd8652013-09-13 16:36:461103 break;
1104 }
larrybrbc917382023-06-07 08:40:311105#endif /* SQLITE_ENABLE_LOCKING_STYLE */
1106
aswiftaebf4132008-11-21 00:10:351107 /*
drh9b0cf342015-11-12 14:57:191108 ** PRAGMA [schema.]synchronous
drh6841b1c2016-02-03 19:20:151109 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
drhc11d4f92003-04-06 21:08:241110 **
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
1114 ** opened.
1115 */
drh9ccd8652013-09-13 16:36:461116 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:061117 if( !zRight ){
drhc232aca2016-12-15 16:01:171118 returnSingleInt(v, pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:241119 }else{
danielk197791cf71b2004-06-26 06:37:061120 if( !db->autoCommit ){
larrybrbc917382023-06-07 08:40:311121 sqlite3ErrorMsg(pParse,
danielk197791cf71b2004-06-26 06:37:061122 "Safety level may not be changed inside a transaction");
drh7553c822017-03-15 14:04:031123 }else if( iDb!=1 ){
drhd99d2832015-04-17 15:58:331124 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
1125 if( iLevel==0 ) iLevel = 1;
1126 pDb->safety_level = iLevel;
drh50a1a5a2016-03-08 14:40:111127 pDb->bSyncSet = 1;
drhd3605a42013-08-17 15:42:291128 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:061129 }
drhc11d4f92003-04-06 21:08:241130 }
drh9ccd8652013-09-13 16:36:461131 break;
1132 }
drh13d70422004-11-13 15:59:141133#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:241134
drhbf216272005-02-26 18:10:441135#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:461136 case PragTyp_FLAG: {
1137 if( zRight==0 ){
drhc232aca2016-12-15 16:01:171138 setPragmaResultColumnNames(v, pPragma);
1139 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:461140 }else{
drhfd748c62018-10-30 16:25:351141 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:461142 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);
1146 }
drh9ccd8652013-09-13 16:36:461147
1148 if( sqlite3GetBoolean(zRight, 0) ){
drh98170652023-10-13 12:57:231149 if( (mask & SQLITE_WriteSchema)==0
1150 || (db->flags & SQLITE_Defensive)==0
1151 ){
1152 db->flags |= mask;
1153 }
drh9ccd8652013-09-13 16:36:461154 }else{
1155 db->flags &= ~mask;
dan5087eac2025-02-13 14:47:251156 if( mask==SQLITE_DeferFKs ){
1157 db->nDeferredImmCons = 0;
1158 db->nDeferredCons = 0;
1159 }
drh635e6a92021-10-08 16:39:331160 if( (mask & SQLITE_WriteSchema)!=0
1161 && sqlite3_stricmp(zRight, "reset")==0
1162 ){
drhbc98f902021-10-14 17:30:321163 /* IMP: R-60817-01178 If the argument is "RESET" then schema
1164 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
1165 ** in addition, the schema is reloaded. */
drh635e6a92021-10-08 16:39:331166 sqlite3ResetAllSchemasOfConnection(db);
1167 }
drh9ccd8652013-09-13 16:36:461168 }
1169
larrybrbc917382023-06-07 08:40:311170 /* Many of the flag-pragmas modify the code generated by the SQL
drh9ccd8652013-09-13 16:36:461171 ** compiler (eg. count_changes). So add an opcode to expire all
1172 ** compiled SQL statements after modifying a pragma value.
1173 */
drh01c736d2016-05-20 15:15:071174 sqlite3VdbeAddOp0(v, OP_Expire);
drh9ccd8652013-09-13 16:36:461175 setAllPagerFlags(db);
1176 }
1177 break;
1178 }
drhbf216272005-02-26 18:10:441179#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:241180
drh13d70422004-11-13 15:59:141181#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:061182 /*
1183 ** PRAGMA table_info(<table>)
1184 **
1185 ** Return a single row for each column of the named table. The columns of
1186 ** the returned data set are:
1187 **
1188 ** cid: Column id (numbered from left to right, starting at 0)
1189 ** name: Column name
1190 ** type: Column declaration type.
1191 ** notnull: True if 'NOT NULL' is part of column declaration
1192 ** dflt_value: The default value for the column, if any.
dan3e6ac162016-02-11 21:01:161193 ** pk: Non-zero for PK fields.
danielk197791cf71b2004-06-26 06:37:061194 */
drh9ccd8652013-09-13 16:36:461195 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:241196 Table *pTab;
drha78d2c02020-07-04 20:29:561197 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drh4d249e62016-06-10 22:49:011198 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
drhc11d4f92003-04-06 21:08:241199 if( pTab ){
drh384b7fe2013-01-01 13:55:311200 int i, k;
danielk1977034ca142007-06-26 10:38:541201 int nHidden = 0;
drhf7eece62006-02-06 21:34:271202 Column *pCol;
drh44156282013-10-23 22:23:031203 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drhd7dc0a32018-10-01 18:28:421204 pParse->nMem = 7;
danielk19774adee202004-05-08 08:23:191205 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:271206 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
drhab3c5f22019-10-17 13:15:401207 int isHidden = 0;
drhf9751072021-10-07 13:40:291208 const Expr *pColExpr;
drhab3c5f22019-10-17 13:15:401209 if( pCol->colFlags & COLFLAG_NOINSERT ){
drh676fa252019-10-17 14:21:071210 if( pPragma->iArg==0 ){
1211 nHidden++;
1212 continue;
1213 }
drhab3c5f22019-10-17 13:15:401214 if( pCol->colFlags & COLFLAG_VIRTUAL ){
1215 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
drhc1431142019-10-17 17:54:051216 }else if( pCol->colFlags & COLFLAG_STORED ){
drhab3c5f22019-10-17 13:15:401217 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
drhc1431142019-10-17 17:54:051218 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
drhab3c5f22019-10-17 13:15:401219 isHidden = 1; /* HIDDEN */
1220 }
danielk1977034ca142007-06-26 10:38:541221 }
drh384b7fe2013-01-01 13:55:311222 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1223 k = 0;
1224 }else if( pPk==0 ){
1225 k = 1;
1226 }else{
drh1b678962015-04-15 07:19:271227 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:311228 }
drhf9751072021-10-07 13:40:291229 pColExpr = sqlite3ColumnExpr(pTab,pCol);
1230 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
drh9d43db52021-10-07 14:19:321231 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)
1232 || isHidden>=2 );
drhd7dc0a32018-10-01 18:28:421233 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
drh076e85f2015-09-03 13:46:121234 i-nHidden,
drhcf9d36d2021-08-02 18:03:431235 pCol->zCnName,
drhd7564862016-03-22 20:05:091236 sqlite3ColumnType(pCol,""),
drh076e85f2015-09-03 13:46:121237 pCol->notNull ? 1 : 0,
drhf9751072021-10-07 13:40:291238 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
drhd7dc0a32018-10-01 18:28:421239 k,
1240 isHidden);
drhc11d4f92003-04-06 21:08:241241 }
1242 }
drh9ccd8652013-09-13 16:36:461243 }
1244 break;
drhc11d4f92003-04-06 21:08:241245
drh2e50f672021-09-21 17:26:231246 /*
1247 ** PRAGMA table_list
1248 **
1249 ** Return a single row for each table, virtual table, or view in the
1250 ** entire schema.
1251 **
1252 ** schema: Name of attached database hold this table
1253 ** name: Name of the table itself
1254 ** type: "table", "view", "virtual", "shadow"
1255 ** ncol: Number of columns
1256 ** wr: True for a WITHOUT ROWID table
1257 ** strict: True for a STRICT table
1258 */
1259 case PragTyp_TABLE_LIST: {
1260 int ii;
1261 pParse->nMem = 6;
1262 sqlite3CodeVerifyNamedSchema(pParse, zDb);
1263 for(ii=0; ii<db->nDb; ii++){
1264 HashElem *k;
1265 Hash *pHash;
drhdc88b402021-10-21 19:48:141266 int initNCol;
drh2e50f672021-09-21 17:26:231267 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
drhdc88b402021-10-21 19:48:141268
1269 /* Ensure that the Table.nCol field is initialized for all views
1270 ** and virtual tables. Each time we initialize a Table.nCol value
1271 ** for a table, that can potentially disrupt the hash table, so restart
1272 ** the initialization scan.
1273 */
drh2e50f672021-09-21 17:26:231274 pHash = &db->aDb[ii].pSchema->tblHash;
drhdc88b402021-10-21 19:48:141275 initNCol = sqliteHashCount(pHash);
1276 while( initNCol-- ){
1277 for(k=sqliteHashFirst(pHash); 1; k=sqliteHashNext(k) ){
1278 Table *pTab;
1279 if( k==0 ){ initNCol = 0; break; }
1280 pTab = sqliteHashData(k);
1281 if( pTab->nCol==0 ){
1282 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
1283 if( zSql ){
1284 sqlite3_stmt *pDummy = 0;
drhef636cc2024-12-06 18:35:161285 (void)sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_DONT_LOG,
1286 &pDummy, 0);
drhdc88b402021-10-21 19:48:141287 (void)sqlite3_finalize(pDummy);
1288 sqlite3DbFree(db, zSql);
1289 }
drh0c7d3d32022-01-24 16:47:121290 if( db->mallocFailed ){
1291 sqlite3ErrorMsg(db->pParse, "out of memory");
1292 db->pParse->rc = SQLITE_NOMEM_BKPT;
1293 }
drhdc88b402021-10-21 19:48:141294 pHash = &db->aDb[ii].pSchema->tblHash;
1295 break;
1296 }
1297 }
1298 }
1299
drh2e50f672021-09-21 17:26:231300 for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k) ){
1301 Table *pTab = sqliteHashData(k);
1302 const char *zType;
1303 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
1304 if( IsView(pTab) ){
1305 zType = "view";
1306 }else if( IsVirtual(pTab) ){
1307 zType = "virtual";
1308 }else if( pTab->tabFlags & TF_Shadow ){
1309 zType = "shadow";
1310 }else{
1311 zType = "table";
1312 }
1313 sqlite3VdbeMultiLoad(v, 1, "sssiii",
1314 db->aDb[ii].zDbSName,
drha4a871c2021-11-04 14:04:201315 sqlite3PreferredTableName(pTab->zName),
drh2e50f672021-09-21 17:26:231316 zType,
1317 pTab->nCol,
1318 (pTab->tabFlags & TF_WithoutRowid)!=0,
1319 (pTab->tabFlags & TF_Strict)!=0
1320 );
1321 }
1322 }
1323 }
1324 break;
1325
drh33bec3f2017-02-17 13:38:151326#ifdef SQLITE_DEBUG
drh3ef26152013-10-12 20:22:001327 case PragTyp_STATS: {
1328 Index *pIdx;
1329 HashElem *i;
drh33bec3f2017-02-17 13:38:151330 pParse->nMem = 5;
drh3ef26152013-10-12 20:22:001331 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:001332 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1333 Table *pTab = sqliteHashData(i);
drh33bec3f2017-02-17 13:38:151334 sqlite3VdbeMultiLoad(v, 1, "ssiii",
drha4a871c2021-11-04 14:04:201335 sqlite3PreferredTableName(pTab->zName),
drh076e85f2015-09-03 13:46:121336 0,
drhd566c952016-02-25 21:19:031337 pTab->szTabRow,
drh33bec3f2017-02-17 13:38:151338 pTab->nRowLogEst,
1339 pTab->tabFlags);
drh3ef26152013-10-12 20:22:001340 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh40cf27c2017-07-07 16:00:531341 sqlite3VdbeMultiLoad(v, 2, "siiiX",
drh076e85f2015-09-03 13:46:121342 pIdx->zName,
drhd566c952016-02-25 21:19:031343 pIdx->szIdxRow,
drh33bec3f2017-02-17 13:38:151344 pIdx->aiRowLogEst[0],
1345 pIdx->hasStat1);
1346 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drh3ef26152013-10-12 20:22:001347 }
1348 }
1349 }
1350 break;
drh33bec3f2017-02-17 13:38:151351#endif
drh3ef26152013-10-12 20:22:001352
drh9ccd8652013-09-13 16:36:461353 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:241354 Index *pIdx;
1355 Table *pTab;
drh2783e4b2004-10-05 15:42:531356 pIdx = sqlite3FindIndex(db, zRight, zDb);
drh7b1904e2019-07-17 11:01:111357 if( pIdx==0 ){
1358 /* If there is no index named zRight, check to see if there is a
1359 ** WITHOUT ROWID table named zRight, and if there is, show the
1360 ** structure of the PRIMARY KEY index for that table. */
1361 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
1362 if( pTab && !HasRowid(pTab) ){
1363 pIdx = sqlite3PrimaryKeyIndex(pTab);
1364 }
1365 }
drhc11d4f92003-04-06 21:08:241366 if( pIdx ){
dan3c425482018-11-20 18:09:591367 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
drhc11d4f92003-04-06 21:08:241368 int i;
drh5e7028c2015-03-05 14:29:021369 int mx;
1370 if( pPragma->iArg ){
1371 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1372 mx = pIdx->nColumn;
1373 pParse->nMem = 6;
1374 }else{
1375 /* PRAGMA index_info (legacy version) */
1376 mx = pIdx->nKeyCol;
1377 pParse->nMem = 3;
1378 }
drhc11d4f92003-04-06 21:08:241379 pTab = pIdx->pTable;
dan3c425482018-11-20 18:09:591380 sqlite3CodeVerifySchema(pParse, iIdxDb);
drhc232aca2016-12-15 16:01:171381 assert( pParse->nMem<=pPragma->nPragCName );
drhc228be52015-01-31 02:00:011382 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:401383 i16 cnum = pIdx->aiColumn[i];
drh40cf27c2017-07-07 16:00:531384 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
drhcf9d36d2021-08-02 18:03:431385 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
drh5e7028c2015-03-05 14:29:021386 if( pPragma->iArg ){
drh40cf27c2017-07-07 16:00:531387 sqlite3VdbeMultiLoad(v, 4, "isiX",
drh076e85f2015-09-03 13:46:121388 pIdx->aSortOrder[i],
1389 pIdx->azColl[i],
1390 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:021391 }
1392 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:241393 }
1394 }
drh9ccd8652013-09-13 16:36:461395 }
1396 break;
drhc11d4f92003-04-06 21:08:241397
drh9ccd8652013-09-13 16:36:461398 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:241399 Index *pIdx;
1400 Table *pTab;
drhe13e9f52013-10-05 19:18:001401 int i;
drh2783e4b2004-10-05 15:42:531402 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:241403 if( pTab ){
dan3c425482018-11-20 18:09:591404 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhc228be52015-01-31 02:00:011405 pParse->nMem = 5;
dan3c425482018-11-20 18:09:591406 sqlite3CodeVerifySchema(pParse, iTabDb);
drh3ef26152013-10-12 20:22:001407 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:011408 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:121409 sqlite3VdbeMultiLoad(v, 1, "isisi",
1410 i,
1411 pIdx->zName,
1412 IsUniqueIndex(pIdx),
1413 azOrigin[pIdx->idxType],
1414 pIdx->pPartIdxWhere!=0);
drhc11d4f92003-04-06 21:08:241415 }
1416 }
drh9ccd8652013-09-13 16:36:461417 }
1418 break;
drhc11d4f92003-04-06 21:08:241419
drh9ccd8652013-09-13 16:36:461420 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:141421 int i;
drh2d401ab2008-01-10 23:50:111422 pParse->nMem = 3;
drh13d70422004-11-13 15:59:141423 for(i=0; i<db->nDb; i++){
1424 if( db->aDb[i].pBt==0 ) continue;
drh69c33822016-08-18 14:33:111425 assert( db->aDb[i].zDbSName!=0 );
drh076e85f2015-09-03 13:46:121426 sqlite3VdbeMultiLoad(v, 1, "iss",
1427 i,
drh69c33822016-08-18 14:33:111428 db->aDb[i].zDbSName,
drh076e85f2015-09-03 13:46:121429 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh13d70422004-11-13 15:59:141430 }
drh9ccd8652013-09-13 16:36:461431 }
1432 break;
danielk197748af65a2005-02-09 03:20:371433
drh9ccd8652013-09-13 16:36:461434 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:371435 int i = 0;
1436 HashElem *p;
drh2d401ab2008-01-10 23:50:111437 pParse->nMem = 2;
danielk197748af65a2005-02-09 03:20:371438 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1439 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:121440 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
danielk197748af65a2005-02-09 03:20:371441 }
drh9ccd8652013-09-13 16:36:461442 }
1443 break;
drhab53bb62017-07-07 15:43:221444
drhcc3f3d12019-08-17 15:27:581445#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
drhab53bb62017-07-07 15:43:221446 case PragTyp_FUNCTION_LIST: {
1447 int i;
1448 HashElem *j;
1449 FuncDef *p;
drh337ca512020-01-04 19:58:281450 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
drh79d5bc82020-01-04 01:43:021451 pParse->nMem = 6;
drhab53bb62017-07-07 15:43:221452 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
1453 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
drhf9751072021-10-07 13:40:291454 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
drh337ca512020-01-04 19:58:281455 pragmaFunclistLine(v, p, 1, showInternFunc);
drhab53bb62017-07-07 15:43:221456 }
1457 }
1458 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
1459 p = (FuncDef*)sqliteHashData(j);
drhf9751072021-10-07 13:40:291460 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
drh337ca512020-01-04 19:58:281461 pragmaFunclistLine(v, p, 0, showInternFunc);
drhab53bb62017-07-07 15:43:221462 }
1463 }
1464 break;
1465
1466#ifndef SQLITE_OMIT_VIRTUALTABLE
1467 case PragTyp_MODULE_LIST: {
1468 HashElem *j;
1469 pParse->nMem = 1;
1470 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
1471 Module *pMod = (Module*)sqliteHashData(j);
1472 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
drhab53bb62017-07-07 15:43:221473 }
1474 }
1475 break;
1476#endif /* SQLITE_OMIT_VIRTUALTABLE */
1477
drh8ae11aa2017-07-07 17:33:071478 case PragTyp_PRAGMA_LIST: {
1479 int i;
1480 for(i=0; i<ArraySize(aPragmaName); i++){
1481 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
drh8ae11aa2017-07-07 17:33:071482 }
1483 }
1484 break;
1485#endif /* SQLITE_INTROSPECTION_PRAGMAS */
drhab53bb62017-07-07 15:43:221486
drh13d70422004-11-13 15:59:141487#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1488
drhb7f91642004-10-31 02:22:471489#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:461490 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:531491 FKey *pFK;
1492 Table *pTab;
drh2783e4b2004-10-05 15:42:531493 pTab = sqlite3FindTable(db, zRight, zDb);
drh78b2fa82021-10-07 12:11:201494 if( pTab && IsOrdinaryTable(pTab) ){
drhf38524d2021-08-02 16:41:571495 pFK = pTab->u.tab.pFKey;
danielk1977742f9472004-06-16 12:02:431496 if( pFK ){
dan3c425482018-11-20 18:09:591497 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
larrybrbc917382023-06-07 08:40:311498 int i = 0;
danielk197750af3e12008-10-10 17:47:211499 pParse->nMem = 8;
dan3c425482018-11-20 18:09:591500 sqlite3CodeVerifySchema(pParse, iTabDb);
danielk1977742f9472004-06-16 12:02:431501 while(pFK){
1502 int j;
1503 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:121504 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1505 i,
1506 j,
1507 pFK->zTo,
drhcf9d36d2021-08-02 18:03:431508 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
drh076e85f2015-09-03 13:46:121509 pFK->aCol[j].zCol,
1510 actionName(pFK->aAction[1]), /* ON UPDATE */
1511 actionName(pFK->aAction[0]), /* ON DELETE */
1512 "NONE");
danielk1977742f9472004-06-16 12:02:431513 }
1514 ++i;
1515 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:531516 }
drh78100cc2003-08-23 22:40:531517 }
1518 }
drh9ccd8652013-09-13 16:36:461519 }
1520 break;
drhb7f91642004-10-31 02:22:471521#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:531522
drh6c5b9152012-12-17 16:46:371523#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:031524#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:461525 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:021526 FKey *pFK; /* A foreign key constraint */
1527 Table *pTab; /* Child table contain "REFERENCES" keyword */
1528 Table *pParent; /* Parent table that child points to */
1529 Index *pIdx; /* Index in the parent table */
1530 int i; /* Loop counter: Foreign key number for pTab */
1531 int j; /* Loop counter: Field of the foreign key */
1532 HashElem *k; /* Loop counter: Next table in schema */
1533 int x; /* result variable */
1534 int regResult; /* 3 registers to hold a result row */
drh613028b2012-12-17 18:43:021535 int regRow; /* Registers to hold a row from pTab */
1536 int addrTop; /* Top of a loop checking foreign keys */
1537 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:141538 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:371539
drh613028b2012-12-17 18:43:021540 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:151541 pParse->nMem += 4;
drh613028b2012-12-17 18:43:021542 regRow = ++pParse->nMem;
drh613028b2012-12-17 18:43:021543 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1544 while( k ){
1545 if( zRight ){
1546 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1547 k = 0;
1548 }else{
1549 pTab = (Table*)sqliteHashData(k);
1550 k = sqliteHashNext(k);
1551 }
drh78b2fa82021-10-07 12:11:201552 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
drhec1650a2020-07-03 12:15:591553 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1554 zDb = db->aDb[iDb].zDbSName;
1555 sqlite3CodeVerifySchema(pParse, iDb);
1556 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drhaa9192e2023-03-26 16:36:271557 sqlite3TouchRegister(pParse, pTab->nCol+regRow);
drhec1650a2020-07-03 12:15:591558 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:121559 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh78b2fa82021-10-07 12:11:201560 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:571561 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:481562 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1563 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:371564 pIdx = 0;
drhec1650a2020-07-03 12:15:591565 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:021566 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:371567 if( x==0 ){
1568 if( pIdx==0 ){
drhec1650a2020-07-03 12:15:591569 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
drh6c5b9152012-12-17 16:46:371570 }else{
drhec1650a2020-07-03 12:15:591571 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:231572 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:371573 }
1574 }else{
drh613028b2012-12-17 18:43:021575 k = 0;
drh6c5b9152012-12-17 16:46:371576 break;
1577 }
drh6c5b9152012-12-17 16:46:371578 }
dan5e878302013-10-12 19:06:481579 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:021580 if( pFK ) break;
1581 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:431582 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh78b2fa82021-10-07 12:11:201583 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:571584 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:481585 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:021586 pIdx = 0;
drh7d22a4d2012-12-17 22:32:141587 aiCols = 0;
dan5e878302013-10-12 19:06:481588 if( pParent ){
1589 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
drhd96e3822020-09-16 19:48:231590 assert( x==0 || db->mallocFailed );
dan5e878302013-10-12 19:06:481591 }
drhec4ccdb2018-12-29 02:26:591592 addrOk = sqlite3VdbeMakeLabel(pParse);
dan4bee5592017-04-17 18:42:331593
1594 /* Generate code to read the child key values into registers
larrybrbc917382023-06-07 08:40:311595 ** regRow..regRow+n. If any of the child key values are NULL, this
1596 ** row cannot cause an FK violation. Jump directly to addrOk in
dan4bee5592017-04-17 18:42:331597 ** this case. */
drhaa9192e2023-03-26 16:36:271598 sqlite3TouchRegister(pParse, regRow + pFK->nCol);
dan4bee5592017-04-17 18:42:331599 for(j=0; j<pFK->nCol; j++){
1600 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
1601 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
1602 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:371603 }
dan4bee5592017-04-17 18:42:331604
1605 /* Generate code to query the parent index for a matching parent
1606 ** key. If a match is found, jump to addrOk. */
1607 if( pIdx ){
drh36d2d092022-04-04 18:17:591608 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
dan4bee5592017-04-17 18:42:331609 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
drh36d2d092022-04-04 18:17:591610 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
dan4bee5592017-04-17 18:42:331611 VdbeCoverage(v);
1612 }else if( pParent ){
1613 int jmp = sqlite3VdbeCurrentAddr(v)+2;
1614 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
1615 sqlite3VdbeGoto(v, addrOk);
drhd96e3822020-09-16 19:48:231616 assert( pFK->nCol==1 || db->mallocFailed );
dan4bee5592017-04-17 18:42:331617 }
1618
1619 /* Generate code to report an FK violation to the caller. */
dan940464b2017-04-17 18:02:411620 if( HasRowid(pTab) ){
1621 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
1622 }else{
1623 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
1624 }
drh40cf27c2017-07-07 16:00:531625 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:151626 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:021627 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:141628 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:371629 }
drh688852a2014-02-17 22:40:431630 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:021631 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:371632 }
drh9ccd8652013-09-13 16:36:461633 }
1634 break;
dan09ff9e12013-03-11 11:49:031635#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:371636#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1637
drh08652b52019-05-08 17:27:181638#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
drh55ef4d92005-08-14 01:20:371639 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1640 ** used will be case sensitive or not depending on the RHS.
1641 */
drh9ccd8652013-09-13 16:36:461642 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:371643 if( zRight ){
drh38d9c612012-01-31 14:24:471644 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:371645 }
drh9ccd8652013-09-13 16:36:461646 }
1647 break;
drh08652b52019-05-08 17:27:181648#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
drh55ef4d92005-08-14 01:20:371649
drh1dcdbc02007-01-27 02:24:541650#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1651# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1652#endif
1653
drhb7f91642004-10-31 02:22:471654#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh8b174f22017-02-22 15:11:361655 /* PRAGMA integrity_check
1656 ** PRAGMA integrity_check(N)
1657 ** PRAGMA quick_check
1658 ** PRAGMA quick_check(N)
1659 **
1660 ** Verify the integrity of the database.
1661 **
larrybrbc917382023-06-07 08:40:311662 ** The "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:191663 ** integrity_check designed to detect most database corruption
drh8b174f22017-02-22 15:11:361664 ** without the overhead of cross-checking indexes. Quick_check
larrybr55be2162023-06-07 17:03:221665 ** is linear time whereas integrity_check is O(NlogN).
drh17d2d592020-07-23 00:45:061666 **
larrybrbc917382023-06-07 08:40:311667 ** The maximum number of errors is 100 by default. A different default
drh17d2d592020-07-23 00:45:061668 ** can be specified using a numeric parameter N.
1669 **
1670 ** Or, the parameter N can be the name of a table. In that case, only
1671 ** the one table named is verified. The freelist is only verified if
1672 ** the named table is "sqlite_schema" (or one of its aliases).
1673 **
1674 ** All schemas are checked by default. To check just a single
1675 ** schema, use the form:
1676 **
1677 ** PRAGMA schema.integrity_check;
danielk197741c58b72007-12-29 13:39:191678 */
drh9ccd8652013-09-13 16:36:461679 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:541680 int i, j, addr, mxErr;
drh17d2d592020-07-23 00:45:061681 Table *pObjTab = 0; /* Check only this one table, if not NULL */
drhed717fe2003-06-15 23:42:241682
drhc5227312011-10-13 17:09:011683 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:191684
dan5885e762012-07-16 10:06:121685 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1686 ** then iDb is set to the index of the database identified by <db>.
1687 ** In this case, the integrity of database iDb only is verified by
1688 ** the VDBE created below.
1689 **
1690 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1691 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1692 ** to -1 here, to indicate that the VDBE should verify the integrity
1693 ** of all attached databases. */
1694 assert( iDb>=0 );
1695 assert( iDb==0 || pId2->z );
1696 if( pId2->z==0 ) iDb = -1;
1697
drhed717fe2003-06-15 23:42:241698 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:111699 pParse->nMem = 6;
drh1dcdbc02007-01-27 02:24:541700
1701 /* Set the maximum error count */
1702 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1703 if( zRight ){
drh78bc1332024-05-02 11:52:311704 if( sqlite3GetInt32(pValue->z, &mxErr) ){
drh17d2d592020-07-23 00:45:061705 if( mxErr<=0 ){
1706 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1707 }
1708 }else{
1709 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
1710 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
drh1dcdbc02007-01-27 02:24:541711 }
1712 }
drh66accfc2017-02-22 18:04:421713 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:241714
1715 /* Do an integrity check on each database file */
1716 for(i=0; i<db->nDb; i++){
drh9ecd7082017-09-10 01:06:051717 HashElem *x; /* For looping over tables in the schema */
1718 Hash *pTbls; /* Set of all tables in the schema */
1719 int *aRoot; /* Array of root page numbers of all btrees */
1720 int cnt = 0; /* Number of entries in aRoot[] */
drhed717fe2003-06-15 23:42:241721
danielk197753c0f742005-03-29 03:10:591722 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:121723 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:591724
drh80242052004-06-09 00:48:121725 sqlite3CodeVerifySchema(pParse, i);
drh3594b2b2023-03-27 13:24:021726 pParse->okConstFactor = 0; /* tag-20230327-1 */
drh80242052004-06-09 00:48:121727
drhed717fe2003-06-15 23:42:241728 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:111729 **
drh98968b22016-03-15 22:00:391730 ** Begin by finding the root pages numbers
drh2d401ab2008-01-10 23:50:111731 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:241732 */
dan5885e762012-07-16 10:06:121733 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:321734 pTbls = &db->aDb[i].pSchema->tblHash;
drh98968b22016-03-15 22:00:391735 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh9ecd7082017-09-10 01:06:051736 Table *pTab = sqliteHashData(x); /* Current table */
1737 Index *pIdx; /* An index on pTab */
1738 int nIdx; /* Number of indexes on pTab */
drh17d2d592020-07-23 00:45:061739 if( pObjTab && pObjTab!=pTab ) continue;
drh98968b22016-03-15 22:00:391740 if( HasRowid(pTab) ) cnt++;
drhbb9b5f22016-03-19 00:35:021741 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
drh98968b22016-03-15 22:00:391742 }
drh17d2d592020-07-23 00:45:061743 if( cnt==0 ) continue;
1744 if( pObjTab ) cnt++;
drh98968b22016-03-15 22:00:391745 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
1746 if( aRoot==0 ) break;
drh17d2d592020-07-23 00:45:061747 cnt = 0;
1748 if( pObjTab ) aRoot[++cnt] = 0;
1749 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh98968b22016-03-15 22:00:391750 Table *pTab = sqliteHashData(x);
1751 Index *pIdx;
drh17d2d592020-07-23 00:45:061752 if( pObjTab && pObjTab!=pTab ) continue;
drhb5c10632017-09-21 00:49:151753 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
drh79069752004-05-22 21:30:401754 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhb5c10632017-09-21 00:49:151755 aRoot[++cnt] = pIdx->tnum;
drh79069752004-05-22 21:30:401756 }
1757 }
drhb5c10632017-09-21 00:49:151758 aRoot[0] = cnt;
drh2d401ab2008-01-10 23:50:111759
1760 /* Make sure sufficient number of registers have been allocated */
dand90ecb52024-02-02 16:51:241761 sqlite3TouchRegister(pParse, 8+cnt);
dan29f97642024-10-07 11:47:051762 sqlite3VdbeAddOp3(v, OP_Null, 0, 8, 8+cnt);
drh3963e582017-07-15 20:33:191763 sqlite3ClearTempRegCache(pParse);
drh2d401ab2008-01-10 23:50:111764
1765 /* Do the b-tree integrity checks */
dand90ecb52024-02-02 16:51:241766 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 1, cnt, 8, (char*)aRoot,P4_INTARRAY);
drh35d302c2024-12-12 15:11:271767 sqlite3VdbeChangeP5(v, (u16)i);
drh688852a2014-02-17 22:40:431768 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:121769 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh69c33822016-08-18 14:33:111770 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
drh66a51672008-01-03 00:01:231771 P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:051772 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
1773 integrityCheckResultRow(v);
drh1dcdbc02007-01-27 02:24:541774 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:241775
dand90ecb52024-02-02 16:51:241776 /* Check that the indexes all have the right number of rows */
1777 cnt = pObjTab ? 1 : 0;
1778 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
1779 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1780 int iTab = 0;
1781 Table *pTab = sqliteHashData(x);
1782 Index *pIdx;
1783 if( pObjTab && pObjTab!=pTab ) continue;
1784 if( HasRowid(pTab) ){
1785 iTab = cnt++;
1786 }else{
1787 iTab = cnt;
drh39670a52024-02-27 15:33:541788 for(pIdx=pTab->pIndex; ALWAYS(pIdx); pIdx=pIdx->pNext){
dand90ecb52024-02-02 16:51:241789 if( IsPrimaryKeyIndex(pIdx) ) break;
1790 iTab++;
1791 }
1792 }
1793 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1794 if( pIdx->pPartIdxWhere==0 ){
1795 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+cnt, 0, 8+iTab);
drh2aea6082024-02-27 16:36:401796 VdbeCoverageNeverNull(v);
dand90ecb52024-02-02 16:51:241797 sqlite3VdbeLoadString(v, 4, pIdx->zName);
1798 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
1799 integrityCheckResultRow(v);
1800 sqlite3VdbeJumpHere(v, addr);
1801 }
1802 cnt++;
1803 }
1804 }
1805
drhed717fe2003-06-15 23:42:241806 /* Make sure all the indices are constructed correctly.
1807 */
drh8b174f22017-02-22 15:11:361808 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:241809 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:551810 Index *pIdx, *pPk;
drh16b03c02022-08-17 18:07:521811 Index *pPrior = 0; /* Previous index */
drhed717fe2003-06-15 23:42:241812 int loopTop;
drh26198bb2013-10-31 11:15:091813 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:051814 int r1 = -1;
drhdb6940a2022-10-10 19:38:011815 int bStrict; /* True for a STRICT table */
drh16b03c02022-08-17 18:07:521816 int r2; /* Previous key for WITHOUT ROWID tables */
drhdb6940a2022-10-10 19:38:011817 int mxCol; /* Maximum non-virtual column number */
drhed717fe2003-06-15 23:42:241818
drh17d2d592020-07-23 00:45:061819 if( pObjTab && pObjTab!=pTab ) continue;
drh64b76c02024-02-01 14:57:241820 if( !IsOrdinaryTable(pTab) ) continue;
drh16b03c02022-08-17 18:07:521821 if( isQuick || HasRowid(pTab) ){
1822 pPk = 0;
1823 r2 = 0;
1824 }else{
1825 pPk = sqlite3PrimaryKeyIndex(pTab);
1826 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
1827 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
1828 }
danfd261ec2015-10-22 20:54:331829 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:391830 1, 0, &iDataCur, &iIdxCur);
drh9ecd7082017-09-10 01:06:051831 /* reg[7] counts the number of entries in the table.
larrybrbc917382023-06-07 08:40:311832 ** reg[8+i] counts the number of entries in the i-th index
drh9ecd7082017-09-10 01:06:051833 */
drh6fbe41a2013-10-30 20:22:551834 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:591835 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:551836 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:591837 }
drhbb9b5f22016-03-19 00:35:021838 assert( pParse->nMem>=8+j );
1839 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
drh688852a2014-02-17 22:40:431840 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:551841 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhdb6940a2022-10-10 19:38:011842
1843 /* Fetch the right-most column from the table. This will cause
1844 ** the entire record header to be parsed and sanity checked. It
drhc9ef12f2022-10-10 21:21:041845 ** will also prepopulate the cursor column cache that is used
1846 ** by the OP_IsType code, so it is a required step.
1847 */
dan20438432023-01-28 17:37:371848 assert( !IsVirtual(pTab) );
1849 if( HasRowid(pTab) ){
1850 mxCol = -1;
1851 for(j=0; j<pTab->nCol; j++){
1852 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++;
1853 }
1854 if( mxCol==pTab->iPKey ) mxCol--;
1855 }else{
1856 /* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID
larrybrbc917382023-06-07 08:40:311857 ** PK index column-count, so there is no need to account for them
dan20438432023-01-28 17:37:371858 ** in this case. */
1859 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1;
1860 }
drhdb6940a2022-10-10 19:38:011861 if( mxCol>=0 ){
dan20438432023-01-28 17:37:371862 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, mxCol, 3);
drhe995d2c2022-10-13 12:47:331863 sqlite3VdbeTypeofColumn(v, 3);
drhdb6940a2022-10-10 19:38:011864 }
drhc9ef12f2022-10-10 21:21:041865
drh4011c442018-06-06 19:48:191866 if( !isQuick ){
drh16b03c02022-08-17 18:07:521867 if( pPk ){
1868 /* Verify WITHOUT ROWID keys are in ascending order */
1869 int a1;
1870 char *zErr;
1871 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
1872 VdbeCoverage(v);
1873 sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v);
1874 zErr = sqlite3MPrintf(db,
1875 "row not in PRIMARY KEY order for %s",
1876 pTab->zName);
1877 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1878 integrityCheckResultRow(v);
1879 sqlite3VdbeJumpHere(v, a1);
1880 sqlite3VdbeJumpHere(v, a1+1);
1881 for(j=0; j<pPk->nKeyCol; j++){
1882 sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
1883 }
1884 }
drh4011c442018-06-06 19:48:191885 }
drh49d77ee2022-10-10 18:25:051886 /* Verify datatypes for all columns:
1887 **
1888 ** (1) NOT NULL columns may not contain a NULL
1889 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
1890 ** (3) Datatype for TEXT columns in non-STRICT tables must be
1891 ** NULL, TEXT, or BLOB.
1892 ** (4) Datatype for numeric columns in non-STRICT tables must not
1893 ** be a TEXT value that can be losslessly converted to numeric.
1894 */
drh9e1209d2021-08-19 02:58:151895 bStrict = (pTab->tabFlags & TF_Strict)!=0;
drhcefc87f2014-08-01 01:40:331896 for(j=0; j<pTab->nCol; j++){
1897 char *zErr;
drh49d77ee2022-10-10 18:25:051898 Column *pCol = pTab->aCol + j; /* The column to be checked */
drhc9ef12f2022-10-10 21:21:041899 int labelError; /* Jump here to report an error */
1900 int labelOk; /* Jump here if all looks ok */
1901 int p1, p3, p4; /* Operands to the OP_IsType opcode */
1902 int doTypeCheck; /* Check datatypes (besides NOT NULL) */
drh49d77ee2022-10-10 18:25:051903
drhcefc87f2014-08-01 01:40:331904 if( j==pTab->iPKey ) continue;
drh49d77ee2022-10-10 18:25:051905 if( bStrict ){
1906 doTypeCheck = pCol->eCType>COLTYPE_ANY;
1907 }else{
1908 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
drhebd70ee2019-12-09 15:52:071909 }
drh49d77ee2022-10-10 18:25:051910 if( pCol->notNull==0 && !doTypeCheck ) continue;
drhc9ef12f2022-10-10 21:21:041911
1912 /* Compute the operands that will be needed for OP_IsType */
drhdb6940a2022-10-10 19:38:011913 p4 = SQLITE_NULL;
drh49d77ee2022-10-10 18:25:051914 if( pCol->colFlags & COLFLAG_VIRTUAL ){
1915 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1916 p1 = -1;
1917 p3 = 3;
1918 }else{
1919 if( pCol->iDflt ){
1920 sqlite3_value *pDfltValue = 0;
1921 sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db),
1922 pCol->affinity, &pDfltValue);
1923 if( pDfltValue ){
1924 p4 = sqlite3_value_type(pDfltValue);
1925 sqlite3ValueFree(pDfltValue);
1926 }
1927 }
1928 p1 = iDataCur;
1929 if( !HasRowid(pTab) ){
1930 testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
1931 p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j);
1932 }else{
1933 p3 = sqlite3TableColumnToStorage(pTab,j);
1934 testcase( p3!=j);
1935 }
1936 }
drhc9ef12f2022-10-10 21:21:041937
1938 labelError = sqlite3VdbeMakeLabel(pParse);
1939 labelOk = sqlite3VdbeMakeLabel(pParse);
drh9e1209d2021-08-19 02:58:151940 if( pCol->notNull ){
drh49d77ee2022-10-10 18:25:051941 /* (1) NOT NULL columns may not contain a NULL */
drhdf542e02023-03-29 11:36:241942 int jmp3;
drhc9ef12f2022-10-10 21:21:041943 int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
drh49d77ee2022-10-10 18:25:051944 VdbeCoverage(v);
drhdf542e02023-03-29 11:36:241945 if( p1<0 ){
1946 sqlite3VdbeChangeP5(v, 0x0f); /* INT, REAL, TEXT, or BLOB */
1947 jmp3 = jmp2;
1948 }else{
1949 sqlite3VdbeChangeP5(v, 0x0d); /* INT, TEXT, or BLOB */
1950 /* OP_IsType does not detect NaN values in the database file
1951 ** which should be treated as a NULL. So if the header type
1952 ** is REAL, we have to load the actual data using OP_Column
1953 ** to reliably determine if the value is a NULL. */
1954 sqlite3VdbeAddOp3(v, OP_Column, p1, p3, 3);
drh95b52952024-02-13 18:41:461955 sqlite3ColumnDefault(v, pTab, j, 3);
drhdf542e02023-03-29 11:36:241956 jmp3 = sqlite3VdbeAddOp2(v, OP_NotNull, 3, labelOk);
1957 VdbeCoverage(v);
larrybrbc917382023-06-07 08:40:311958 }
drh9e1209d2021-08-19 02:58:151959 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1960 pCol->zCnName);
1961 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drhc9ef12f2022-10-10 21:21:041962 if( doTypeCheck ){
1963 sqlite3VdbeGoto(v, labelError);
1964 sqlite3VdbeJumpHere(v, jmp2);
drhdf542e02023-03-29 11:36:241965 sqlite3VdbeJumpHere(v, jmp3);
drh71c770f2021-08-19 16:29:331966 }else{
drhc9ef12f2022-10-10 21:21:041967 /* VDBE byte code will fall thru */
drh71c770f2021-08-19 16:29:331968 }
drh9e1209d2021-08-19 02:58:151969 }
drh49d77ee2022-10-10 18:25:051970 if( bStrict && doTypeCheck ){
1971 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
1972 static unsigned char aStdTypeMask[] = {
1973 0x1f, /* ANY */
1974 0x18, /* BLOB */
1975 0x11, /* INT */
1976 0x11, /* INTEGER */
1977 0x13, /* REAL */
1978 0x14 /* TEXT */
1979 };
drhc9ef12f2022-10-10 21:21:041980 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
drh49d77ee2022-10-10 18:25:051981 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
1982 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
drh9e1209d2021-08-19 02:58:151983 VdbeCoverage(v);
1984 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
1985 sqlite3StdType[pCol->eCType-1],
1986 pTab->zName, pTab->aCol[j].zCnName);
1987 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drhc9ef12f2022-10-10 21:21:041988 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
drh49d77ee2022-10-10 18:25:051989 /* (3) Datatype for TEXT columns in non-STRICT tables must be
1990 ** NULL, TEXT, or BLOB. */
drhc9ef12f2022-10-10 21:21:041991 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
drh49d77ee2022-10-10 18:25:051992 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
1993 VdbeCoverage(v);
1994 zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s",
1995 pTab->zName, pTab->aCol[j].zCnName);
1996 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drhc9ef12f2022-10-10 21:21:041997 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
drh49d77ee2022-10-10 18:25:051998 /* (4) Datatype for numeric columns in non-STRICT tables must not
1999 ** be a TEXT value that can be converted to numeric. */
drhc9ef12f2022-10-10 21:21:042000 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
drh49d77ee2022-10-10 18:25:052001 sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
2002 VdbeCoverage(v);
2003 if( p1>=0 ){
2004 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
2005 }
2006 sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC);
drhc9ef12f2022-10-10 21:21:042007 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
drh49d77ee2022-10-10 18:25:052008 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
2009 VdbeCoverage(v);
2010 zErr = sqlite3MPrintf(db, "TEXT value in %s.%s",
2011 pTab->zName, pTab->aCol[j].zCnName);
2012 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh49d77ee2022-10-10 18:25:052013 }
drhc9ef12f2022-10-10 21:21:042014 sqlite3VdbeResolveLabel(v, labelError);
2015 integrityCheckResultRow(v);
2016 sqlite3VdbeResolveLabel(v, labelOk);
drhcefc87f2014-08-01 01:40:332017 }
drh8a284dc2017-02-22 14:15:372018 /* Verify CHECK constraints */
2019 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
dan75f95582017-04-04 19:58:542020 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
2021 if( db->mallocFailed==0 ){
drhec4ccdb2018-12-29 02:26:592022 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
2023 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
dan75f95582017-04-04 19:58:542024 char *zErr;
2025 int k;
drh6e97f8e2017-07-20 13:17:082026 pParse->iSelfTab = iDataCur + 1;
dan75f95582017-04-04 19:58:542027 for(k=pCheck->nExpr-1; k>0; k--){
2028 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
2029 }
larrybrbc917382023-06-07 08:40:312030 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
dan75f95582017-04-04 19:58:542031 SQLITE_JUMPIFNULL);
2032 sqlite3VdbeResolveLabel(v, addrCkFault);
drh3e34eab2017-07-19 19:48:402033 pParse->iSelfTab = 0;
dan75f95582017-04-04 19:58:542034 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
2035 pTab->zName);
2036 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:052037 integrityCheckResultRow(v);
dan75f95582017-04-04 19:58:542038 sqlite3VdbeResolveLabel(v, addrCkOk);
drh8a284dc2017-02-22 14:15:372039 }
dan75f95582017-04-04 19:58:542040 sqlite3ExprListDelete(db, pCheck);
drhcefc87f2014-08-01 01:40:332041 }
drh226cef42017-09-09 20:38:492042 if( !isQuick ){ /* Omit the remaining tests for quick_check */
drh226cef42017-09-09 20:38:492043 /* Validate index entries for the current row */
2044 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhd0fe0fc2023-01-04 15:18:522045 int jmp2, jmp3, jmp4, jmp5, label6;
2046 int kk;
drhec4ccdb2018-12-29 02:26:592047 int ckUniq = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:492048 if( pPk==pIdx ) continue;
2049 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
2050 pPrior, r1);
2051 pPrior = pIdx;
2052 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
2053 /* Verify that an index entry exists for the current table row */
2054 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
2055 pIdx->nColumn); VdbeCoverage(v);
2056 sqlite3VdbeLoadString(v, 3, "row ");
2057 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
2058 sqlite3VdbeLoadString(v, 4, " missing from index ");
2059 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
2060 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
2061 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh9ecd7082017-09-10 01:06:052062 jmp4 = integrityCheckResultRow(v);
drh226cef42017-09-09 20:38:492063 sqlite3VdbeJumpHere(v, jmp2);
drhd0fe0fc2023-01-04 15:18:522064
drh1b9db7f2023-03-03 18:35:002065 /* The OP_IdxRowid opcode is an optimized version of OP_Column
2066 ** that extracts the rowid off the end of the index record.
2067 ** But it only works correctly if index record does not have
2068 ** any extra bytes at the end. Verify that this is the case. */
2069 if( HasRowid(pTab) ){
2070 int jmp7;
2071 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur+j, 3);
2072 jmp7 = sqlite3VdbeAddOp3(v, OP_Eq, 3, 0, r1+pIdx->nColumn-1);
drhfe526152023-03-03 19:56:192073 VdbeCoverageNeverNull(v);
drh1b9db7f2023-03-03 18:35:002074 sqlite3VdbeLoadString(v, 3,
2075 "rowid not at end-of-record for row ");
2076 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
2077 sqlite3VdbeLoadString(v, 4, " of index ");
2078 sqlite3VdbeGoto(v, jmp5-1);
2079 sqlite3VdbeJumpHere(v, jmp7);
2080 }
2081
drhd0fe0fc2023-01-04 15:18:522082 /* Any indexed columns with non-BINARY collations must still hold
2083 ** the exact same text value as the table. */
2084 label6 = 0;
2085 for(kk=0; kk<pIdx->nKeyCol; kk++){
2086 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue;
2087 if( label6==0 ) label6 = sqlite3VdbeMakeLabel(pParse);
2088 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur+j, kk, 3);
2089 sqlite3VdbeAddOp3(v, OP_Ne, 3, label6, r1+kk); VdbeCoverage(v);
2090 }
2091 if( label6 ){
2092 int jmp6 = sqlite3VdbeAddOp0(v, OP_Goto);
2093 sqlite3VdbeResolveLabel(v, label6);
2094 sqlite3VdbeLoadString(v, 3, "row ");
2095 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
2096 sqlite3VdbeLoadString(v, 4, " values differ from index ");
2097 sqlite3VdbeGoto(v, jmp5-1);
2098 sqlite3VdbeJumpHere(v, jmp6);
2099 }
larrybrbc917382023-06-07 08:40:312100
drh226cef42017-09-09 20:38:492101 /* For UNIQUE indexes, verify that only one entry exists with the
2102 ** current key. The entry is unique if (1) any column is NULL
2103 ** or (2) the next entry has a different key */
2104 if( IsUniqueIndex(pIdx) ){
drhec4ccdb2018-12-29 02:26:592105 int uniqOk = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:492106 int jmp6;
drh226cef42017-09-09 20:38:492107 for(kk=0; kk<pIdx->nKeyCol; kk++){
2108 int iCol = pIdx->aiColumn[kk];
2109 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
2110 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
2111 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
2112 VdbeCoverage(v);
2113 }
2114 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
2115 sqlite3VdbeGoto(v, uniqOk);
2116 sqlite3VdbeJumpHere(v, jmp6);
2117 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
2118 pIdx->nKeyCol); VdbeCoverage(v);
2119 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
2120 sqlite3VdbeGoto(v, jmp5);
2121 sqlite3VdbeResolveLabel(v, uniqOk);
drhcefc87f2014-08-01 01:40:332122 }
drh226cef42017-09-09 20:38:492123 sqlite3VdbeJumpHere(v, jmp4);
2124 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhcefc87f2014-08-01 01:40:332125 }
drhed717fe2003-06-15 23:42:242126 }
drh688852a2014-02-17 22:40:432127 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:592128 sqlite3VdbeJumpHere(v, loopTop-1);
dand90ecb52024-02-02 16:51:242129 if( pPk ){
2130 assert( !isQuick );
2131 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
drhed717fe2003-06-15 23:42:242132 }
larrybrbc917382023-06-07 08:40:312133 }
drh64b76c02024-02-01 14:57:242134
2135#ifndef SQLITE_OMIT_VIRTUALTABLE
2136 /* Second pass to invoke the xIntegrity method on all virtual
2137 ** tables.
2138 */
2139 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
2140 Table *pTab = sqliteHashData(x);
2141 sqlite3_vtab *pVTab;
2142 int a1;
2143 if( pObjTab && pObjTab!=pTab ) continue;
2144 if( IsOrdinaryTable(pTab) ) continue;
2145 if( !IsVirtual(pTab) ) continue;
2146 if( pTab->nCol<=0 ){
2147 const char *zMod = pTab->u.vtab.azArg[0];
2148 if( sqlite3HashFind(&db->aModule, zMod)==0 ) continue;
2149 }
2150 sqlite3ViewGetColumnNames(pParse, pTab);
2151 if( pTab->u.vtab.p==0 ) continue;
2152 pVTab = pTab->u.vtab.p->pVtab;
2153 if( NEVER(pVTab==0) ) continue;
2154 if( NEVER(pVTab->pModule==0) ) continue;
2155 if( pVTab->pModule->iVersion<4 ) continue;
2156 if( pVTab->pModule->xIntegrity==0 ) continue;
2157 sqlite3VdbeAddOp3(v, OP_VCheck, i, 3, isQuick);
2158 pTab->nTabRef++;
2159 sqlite3VdbeAppendP4(v, pTab, P4_TABLEREF);
2160 a1 = sqlite3VdbeAddOp1(v, OP_IsNull, 3); VdbeCoverage(v);
2161 integrityCheckResultRow(v);
2162 sqlite3VdbeJumpHere(v, a1);
drh64b76c02024-02-01 14:57:242163 continue;
2164 }
drh0f777cd2024-02-07 20:45:382165#endif
drhed717fe2003-06-15 23:42:242166 }
drh2ce18652016-01-16 20:50:212167 {
2168 static const int iLn = VDBE_OFFSET_LINENO(2);
2169 static const VdbeOpList endCode[] = {
2170 { OP_AddImm, 1, 0, 0}, /* 0 */
drh66accfc2017-02-22 18:04:422171 { OP_IfNotZero, 1, 4, 0}, /* 1 */
drh2ce18652016-01-16 20:50:212172 { OP_String8, 0, 3, 0}, /* 2 */
drh1b325542016-02-03 01:55:442173 { OP_ResultRow, 3, 1, 0}, /* 3 */
drh74588ce2017-09-13 00:13:052174 { OP_Halt, 0, 0, 0}, /* 4 */
2175 { OP_String8, 0, 3, 0}, /* 5 */
2176 { OP_Goto, 0, 3, 0}, /* 6 */
drh2ce18652016-01-16 20:50:212177 };
2178 VdbeOp *aOp;
2179
2180 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
2181 if( aOp ){
drh66accfc2017-02-22 18:04:422182 aOp[0].p2 = 1-mxErr;
drh2ce18652016-01-16 20:50:212183 aOp[2].p4type = P4_STATIC;
2184 aOp[2].p4.z = "ok";
drh74588ce2017-09-13 00:13:052185 aOp[5].p4type = P4_STATIC;
2186 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
drh2ce18652016-01-16 20:50:212187 }
drh74588ce2017-09-13 00:13:052188 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
drh2ce18652016-01-16 20:50:212189 }
drh9ccd8652013-09-13 16:36:462190 }
2191 break;
drhb7f91642004-10-31 02:22:472192#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
2193
drh13d70422004-11-13 15:59:142194#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:172195 /*
2196 ** PRAGMA encoding
2197 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
2198 **
drh85b623f2007-12-13 21:54:092199 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:172200 ** database. If the database is not initialized, it is initialized now.
2201 **
2202 ** The second form of this pragma is a no-op if the main database file
2203 ** has not already been initialized. In this case it sets the default
2204 ** encoding that will be used for the main database file if a new file
2205 ** is created. If an existing main database file is opened, then the
2206 ** default text encoding for the existing database is used.
larrybrbc917382023-06-07 08:40:312207 **
danielk19778e227872004-06-07 07:52:172208 ** In all cases new databases created using the ATTACH command are
2209 ** created to use the same default text encoding as the main database. If
2210 ** the main database has not been initialized and/or created when ATTACH
2211 ** is executed, this is done before the ATTACH operation.
2212 **
2213 ** In the second form this pragma sets the text encoding to be used in
2214 ** new database files created using this database handle. It is only
2215 ** useful if invoked immediately after the main database i
2216 */
drh9ccd8652013-09-13 16:36:462217 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:432218 static const struct EncName {
danielk19778e227872004-06-07 07:52:172219 char *zName;
2220 u8 enc;
2221 } encnames[] = {
drh998da3a2004-06-19 15:22:562222 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:172223 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
2224 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
2225 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:562226 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:562227 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:432228 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
2229 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:172230 { 0, 0 }
2231 };
drh0f7eb612006-08-08 13:51:432232 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:062233 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:352234 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:172235 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
2236 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
2237 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drhc232aca2016-12-15 16:01:172238 returnSingleText(v, encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:172239 }else{ /* "PRAGMA encoding = XXX" */
2240 /* Only change the value of sqlite.enc if the database handle is not
2241 ** initialized. If the main database exists, the new sqlite.enc value
2242 ** will be overwritten when the schema is next loaded. If it does not
2243 ** already exists, it will be created to use the new encoding value.
2244 */
dan0ea2d422020-03-05 18:04:092245 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
danielk19778e227872004-06-07 07:52:172246 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
2247 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh42a630b2020-03-05 16:13:242248 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
2249 SCHEMA_ENC(db) = enc;
2250 sqlite3SetTextEncoding(db, enc);
danielk19778e227872004-06-07 07:52:172251 break;
2252 }
2253 }
2254 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:292255 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:172256 }
2257 }
2258 }
drh9ccd8652013-09-13 16:36:462259 }
2260 break;
drh13d70422004-11-13 15:59:142261#endif /* SQLITE_OMIT_UTF16 */
2262
2263#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:432264 /*
drh9b0cf342015-11-12 14:57:192265 ** PRAGMA [schema.]schema_version
2266 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:432267 **
drh9b0cf342015-11-12 14:57:192268 ** PRAGMA [schema.]user_version
2269 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:432270 **
drhe459bd42016-03-16 20:05:572271 ** PRAGMA [schema.]freelist_count
2272 **
2273 ** PRAGMA [schema.]data_version
drh4ee09b42013-05-01 19:49:272274 **
drh9b0cf342015-11-12 14:57:192275 ** PRAGMA [schema.]application_id
2276 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:272277 **
danielk1977b92b70b2004-11-12 16:11:592278 ** The pragma's schema_version and user_version are used to set or get
2279 ** the value of the schema-version and user-version, respectively. Both
2280 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:432281 ** stored in the database header.
2282 **
2283 ** The schema-cookie is usually only manipulated internally by SQLite. It
2284 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:592285 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:432286 ** SQLite each time a query is executed to ensure that the internal cache
2287 ** of the schema used when compiling the SQL query matches the schema of
2288 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:592289 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2290 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:432291 ** crashes or database corruption. Use with caution!
2292 **
danielk1977b92b70b2004-11-12 16:11:592293 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:432294 ** applications for any purpose.
2295 */
drh9ccd8652013-09-13 16:36:462296 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:012297 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:592298 sqlite3VdbeUsesBtree(v, iDb);
drhc232aca2016-12-15 16:01:172299 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:432300 /* Write the specified cookie value */
2301 static const VdbeOpList setCookie[] = {
2302 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1861afc2016-02-01 21:48:342303 { OP_SetCookie, 0, 0, 0}, /* 1 */
danielk1977dae24952004-11-11 05:10:432304 };
drh2ce18652016-01-16 20:50:212305 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:262306 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
drh2ce18652016-01-16 20:50:212307 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
drhdad300d2016-01-18 00:20:262308 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:212309 aOp[0].p1 = iDb;
drh1861afc2016-02-01 21:48:342310 aOp[1].p1 = iDb;
2311 aOp[1].p2 = iCookie;
2312 aOp[1].p3 = sqlite3Atoi(zRight);
drhe3863b52020-07-01 16:19:142313 aOp[1].p5 = 1;
drh7e475e52022-11-12 17:17:012314 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
2315 /* Do not allow the use of PRAGMA schema_version=VALUE in defensive
2316 ** mode. Change the OP_SetCookie opcode into a no-op. */
2317 aOp[1].opcode = OP_Noop;
2318 }
danielk1977dae24952004-11-11 05:10:432319 }else{
2320 /* Read the specified cookie value */
2321 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:332322 { OP_Transaction, 0, 0, 0}, /* 0 */
2323 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:112324 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:432325 };
drh2ce18652016-01-16 20:50:212326 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:262327 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
drh2ce18652016-01-16 20:50:212328 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
drhdad300d2016-01-18 00:20:262329 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:212330 aOp[0].p1 = iDb;
2331 aOp[1].p1 = iDb;
2332 aOp[1].p3 = iCookie;
drhf71a3662016-03-16 20:44:452333 sqlite3VdbeReusable(v);
danielk1977dae24952004-11-11 05:10:432334 }
drh9ccd8652013-09-13 16:36:462335 }
2336 break;
drh13d70422004-11-13 15:59:142337#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:242338
shanehdc97a8c2010-02-23 20:08:352339#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2340 /*
2341 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:352342 **
drh71caabf2010-02-26 15:39:242343 ** Return the names of all compile-time options used in this build,
2344 ** one option per row.
shanehdc97a8c2010-02-23 20:08:352345 */
drh9ccd8652013-09-13 16:36:462346 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:352347 int i = 0;
2348 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:352349 pParse->nMem = 1;
shanehdc97a8c2010-02-23 20:08:352350 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:122351 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:352352 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2353 }
drhf71a3662016-03-16 20:44:452354 sqlite3VdbeReusable(v);
drh9ccd8652013-09-13 16:36:462355 }
2356 break;
shanehdc97a8c2010-02-23 20:08:352357#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2358
dan5cf53532010-05-01 16:40:202359#ifndef SQLITE_OMIT_WAL
2360 /*
drh9b0cf342015-11-12 14:57:192361 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:202362 **
2363 ** Checkpoint the database.
2364 */
drh9ccd8652013-09-13 16:36:462365 case PragTyp_WAL_CHECKPOINT: {
drh099b3852021-03-10 16:35:372366 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
dancdc1f042010-11-18 12:11:052367 int eMode = SQLITE_CHECKPOINT_PASSIVE;
2368 if( zRight ){
2369 if( sqlite3StrICmp(zRight, "full")==0 ){
2370 eMode = SQLITE_CHECKPOINT_FULL;
2371 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2372 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:542373 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
2374 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:052375 }
2376 }
dancdc1f042010-11-18 12:11:052377 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:012378 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:052379 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:462380 }
2381 break;
dan5a299f92010-05-03 11:05:082382
2383 /*
2384 ** PRAGMA wal_autocheckpoint
2385 ** PRAGMA wal_autocheckpoint = N
2386 **
2387 ** Configure a database connection to automatically checkpoint a database
2388 ** after accumulating N frames in the log. Or query for the current value
2389 ** of N.
2390 */
drh9ccd8652013-09-13 16:36:462391 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:082392 if( zRight ){
drh60ac3f42010-11-23 18:59:272393 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:082394 }
larrybrbc917382023-06-07 08:40:312395 returnSingleInt(v,
2396 db->xWalCallback==sqlite3WalDefaultHook ?
drhb033d8b2010-05-03 13:37:302397 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:462398 }
2399 break;
dan5cf53532010-05-01 16:40:202400#endif
dan7c246102010-04-12 19:00:292401
drh09419b42011-11-16 19:29:172402 /*
2403 ** PRAGMA shrink_memory
2404 **
drh51a74d42015-02-28 01:04:272405 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
2406 ** connection on which it is invoked to free up as much memory as it
2407 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:172408 */
drh9ccd8652013-09-13 16:36:462409 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:172410 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:462411 break;
2412 }
drh09419b42011-11-16 19:29:172413
drhf3603962012-09-07 16:46:592414 /*
drh2ead47c2017-02-22 20:24:102415 ** PRAGMA optimize
drh1cfaf8e2017-03-02 14:17:212416 ** PRAGMA optimize(MASK)
drh2ead47c2017-02-22 20:24:102417 ** PRAGMA schema.optimize
drh1cfaf8e2017-03-02 14:17:212418 ** PRAGMA schema.optimize(MASK)
drh99d5b4c2017-02-18 22:52:402419 **
drh2ead47c2017-02-22 20:24:102420 ** Attempt to optimize the database. All schemas are optimized in the first
drh1cfaf8e2017-03-02 14:17:212421 ** two forms, and only the specified schema is optimized in the latter two.
drh2ead47c2017-02-22 20:24:102422 **
drhe1f1c082017-03-06 20:44:132423 ** The details of optimizations performed by this pragma are expected
drh2ead47c2017-02-22 20:24:102424 ** to change and improve over time. Applications should anticipate that
2425 ** this pragma will perform new optimizations in future releases.
2426 **
drh1cfaf8e2017-03-02 14:17:212427 ** The optional argument is a bitmask of optimizations to perform:
drh2ead47c2017-02-22 20:24:102428 **
drh42eb6a92024-02-17 16:39:522429 ** 0x00001 Debugging mode. Do not actually perform any optimizations
2430 ** but instead return one line of text for each optimization
2431 ** that would have been done. Off by default.
drh99d5b4c2017-02-18 22:52:402432 **
drh42eb6a92024-02-17 16:39:522433 ** 0x00002 Run ANALYZE on tables that might benefit. On by default.
2434 ** See below for additional information.
drh1cfaf8e2017-03-02 14:17:212435 **
drh9f34a052024-02-19 13:06:272436 ** 0x00010 Run all ANALYZE operations using an analysis_limit that
2437 ** is the lessor of the current analysis_limit and the
2438 ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option.
2439 ** The default value of SQLITE_DEFAULT_OPTIMIZE_LIMIT is
2440 ** currently (2024-02-19) set to 2000, which is such that
2441 ** the worst case run-time for PRAGMA optimize on a 100MB
2442 ** database will usually be less than 100 milliseconds on
drh6c6356f2024-02-19 13:50:092443 ** a RaspberryPI-4 class machine. On by default.
2444 **
drh42eb6a92024-02-17 16:39:522445 ** 0x10000 Look at tables to see if they need to be reanalyzed
drh6c6356f2024-02-19 13:50:092446 ** due to growth or shrinkage even if they have not been
2447 ** queried during the current connection. Off by default.
drh1cfaf8e2017-03-02 14:17:212448 **
drh42eb6a92024-02-17 16:39:522449 ** The default MASK is and always shall be 0x0fffe. In the current
2450 ** implementation, the default mask only covers the 0x00002 optimization,
2451 ** though additional optimizations that are covered by 0x0fffe might be
2452 ** added in the future. Optimizations that are off by default and must
2453 ** be explicitly requested have masks of 0x10000 or greater.
drh1cfaf8e2017-03-02 14:17:212454 **
2455 ** DETERMINATION OF WHEN TO RUN ANALYZE
2456 **
2457 ** In the current implementation, a table is analyzed if only if all of
drh2ead47c2017-02-22 20:24:102458 ** the following are true:
drh99d5b4c2017-02-18 22:52:402459 **
drh42eb6a92024-02-17 16:39:522460 ** (1) MASK bit 0x00002 is set.
drh1cfaf8e2017-03-02 14:17:212461 **
drh6c6356f2024-02-19 13:50:092462 ** (2) The table is an ordinary table, not a virtual table or view.
drh99d5b4c2017-02-18 22:52:402463 **
drh6c6356f2024-02-19 13:50:092464 ** (3) The table name does not begin with "sqlite_".
drh2ead47c2017-02-22 20:24:102465 **
drh6c6356f2024-02-19 13:50:092466 ** (4) One or more of the following is true:
2467 ** (4a) The 0x10000 MASK bit is set.
drhe7bdb212024-02-19 16:22:582468 ** (4b) One or more indexes on the table lacks an entry
drh6c6356f2024-02-19 13:50:092469 ** in the sqlite_stat1 table.
2470 ** (4c) The query planner used sqlite_stat1-style statistics for one
drh21eda692024-02-20 15:38:362471 ** or more indexes of the table at some point during the lifetime
drh6c6356f2024-02-19 13:50:092472 ** of the current connection.
drh42eb6a92024-02-17 16:39:522473 **
drh6c6356f2024-02-19 13:50:092474 ** (5) One or more of the following is true:
drh74b0aad2024-02-19 19:56:402475 ** (5a) One or more indexes on the table lacks an entry
drh6c6356f2024-02-19 13:50:092476 ** in the sqlite_stat1 table. (Same as 4a)
2477 ** (5b) The number of rows in the table has increased or decreased by
2478 ** 10-fold. In other words, the current size of the table is
2479 ** 10 times larger than the size in sqlite_stat1 or else the
2480 ** current size is less than 1/10th the size in sqlite_stat1.
drh42eb6a92024-02-17 16:39:522481 **
drh2ead47c2017-02-22 20:24:102482 ** The rules for when tables are analyzed are likely to change in
drh42eb6a92024-02-17 16:39:522483 ** future releases. Future versions of SQLite might accept a string
2484 ** literal argument to this pragma that contains a mnemonic description
2485 ** of the options rather than a bitmap.
drh72052a72017-02-17 16:26:342486 */
drh2ead47c2017-02-22 20:24:102487 case PragTyp_OPTIMIZE: {
drh99d5b4c2017-02-18 22:52:402488 int iDbLast; /* Loop termination point for the schema loop */
2489 int iTabCur; /* Cursor for a table whose size needs checking */
2490 HashElem *k; /* Loop over tables of a schema */
2491 Schema *pSchema; /* The current schema */
2492 Table *pTab; /* A table in the schema */
2493 Index *pIdx; /* An index of the table */
larrybrbc917382023-06-07 08:40:312494 LogEst szThreshold; /* Size threshold above which reanalysis needed */
drh99d5b4c2017-02-18 22:52:402495 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
drh1cfaf8e2017-03-02 14:17:212496 u32 opMask; /* Mask of operations to perform */
drh42eb6a92024-02-17 16:39:522497 int nLimit; /* Analysis limit to use */
drh6c6356f2024-02-19 13:50:092498 int nCheck = 0; /* Number of tables to be optimized */
drh74b0aad2024-02-19 19:56:402499 int nBtree = 0; /* Number of btrees to scan */
2500 int nIndex; /* Number of indexes on the current table */
drh42eb6a92024-02-17 16:39:522501
drh1cfaf8e2017-03-02 14:17:212502 if( zRight ){
2503 opMask = (u32)sqlite3Atoi(zRight);
2504 if( (opMask & 0x02)==0 ) break;
2505 }else{
drh59dbe3a2017-03-06 23:51:162506 opMask = 0xfffe;
drh1cfaf8e2017-03-02 14:17:212507 }
drh9f34a052024-02-19 13:06:272508 if( (opMask & 0x10)==0 ){
drh42eb6a92024-02-17 16:39:522509 nLimit = 0;
2510 }else if( db->nAnalysisLimit>0
2511 && db->nAnalysisLimit<SQLITE_DEFAULT_OPTIMIZE_LIMIT ){
2512 nLimit = 0;
2513 }else{
2514 nLimit = SQLITE_DEFAULT_OPTIMIZE_LIMIT;
2515 }
drh4a54bb52017-02-18 15:58:522516 iTabCur = pParse->nTab++;
2517 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
2518 if( iDb==1 ) continue;
2519 sqlite3CodeVerifySchema(pParse, iDb);
2520 pSchema = db->aDb[iDb].pSchema;
2521 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
2522 pTab = (Table*)sqliteHashData(k);
drh99d5b4c2017-02-18 22:52:402523
drh42eb6a92024-02-17 16:39:522524 /* This only works for ordinary tables */
2525 if( !IsOrdinaryTable(pTab) ) continue;
2526
2527 /* Do not scan system tables */
2528 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ) continue;
2529
drh6c6356f2024-02-19 13:50:092530 /* Find the size of the table as last recorded in sqlite_stat1.
drhe7bdb212024-02-19 16:22:582531 ** If any index is unanalyzed, then the threshold is -1 to
2532 ** indicate a new, unanalyzed index
drh6c6356f2024-02-19 13:50:092533 */
drh837efb42024-02-17 01:12:582534 szThreshold = pTab->nRowLogEst;
drh74b0aad2024-02-19 19:56:402535 nIndex = 0;
drh4a54bb52017-02-18 15:58:522536 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh74b0aad2024-02-19 19:56:402537 nIndex++;
drh4b2eeb22024-02-20 13:10:462538 if( !pIdx->hasStat1 ){
drh837efb42024-02-17 01:12:582539 szThreshold = -1; /* Always analyze if any index lacks statistics */
drh4a54bb52017-02-18 15:58:522540 }
2541 }
drh6c6356f2024-02-19 13:50:092542
2543 /* If table pTab has not been used in a way that would benefit from
2544 ** having analysis statistics during the current session, then skip it,
2545 ** unless the 0x10000 MASK bit is set. */
2546 if( (pTab->tabFlags & TF_MaybeReanalyze)!=0 ){
2547 /* Check for size change if stat1 has been used for a query */
2548 }else if( opMask & 0x10000 ){
2549 /* Check for size change if 0x10000 is set */
2550 }else if( pTab->pIndex!=0 && szThreshold<0 ){
drhe7bdb212024-02-19 16:22:582551 /* Do analysis if unanalyzed indexes exists */
drh6c6356f2024-02-19 13:50:092552 }else{
2553 /* Otherwise, we can skip this table */
2554 continue;
2555 }
2556
2557 nCheck++;
2558 if( nCheck==2 ){
2559 /* If ANALYZE might be invoked two or more times, hold a write
2560 ** transaction for efficiency */
2561 sqlite3BeginWriteOperation(pParse, 0, iDb);
2562 }
drh74b0aad2024-02-19 19:56:402563 nBtree += nIndex+1;
drh6c6356f2024-02-19 13:50:092564
2565 /* Reanalyze if the table is 10 times larger or smaller than
2566 ** the last analysis. Unconditional reanalysis if there are
drhe7bdb212024-02-19 16:22:582567 ** unanalyzed indexes. */
drh4189c442024-02-20 12:14:072568 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
drh837efb42024-02-17 01:12:582569 if( szThreshold>=0 ){
drh9a283112024-02-19 20:12:302570 const LogEst iRange = 33; /* 10x size change */
drh837efb42024-02-17 01:12:582571 sqlite3VdbeAddOp4Int(v, OP_IfSizeBetween, iTabCur,
drh9a283112024-02-19 20:12:302572 sqlite3VdbeCurrentAddr(v)+2+(opMask&1),
drh42eb6a92024-02-17 16:39:522573 szThreshold>=iRange ? szThreshold-iRange : -1,
2574 szThreshold+iRange);
drh4a54bb52017-02-18 15:58:522575 VdbeCoverage(v);
drh4189c442024-02-20 12:14:072576 }else{
drh9a283112024-02-19 20:12:302577 sqlite3VdbeAddOp2(v, OP_Rewind, iTabCur,
2578 sqlite3VdbeCurrentAddr(v)+2+(opMask&1));
drhae71fa52024-02-19 23:58:262579 VdbeCoverage(v);
drh4a54bb52017-02-18 15:58:522580 }
2581 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
2582 db->aDb[iDb].zDbSName, pTab->zName);
drh1cfaf8e2017-03-02 14:17:212583 if( opMask & 0x01 ){
2584 int r1 = sqlite3GetTempReg(pParse);
2585 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
2586 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
2587 }else{
drh42eb6a92024-02-17 16:39:522588 sqlite3VdbeAddOp4(v, OP_SqlExec, nLimit ? 0x02 : 00, nLimit, 0,
2589 zSubSql, P4_DYNAMIC);
drh1cfaf8e2017-03-02 14:17:212590 }
drh4a54bb52017-02-18 15:58:522591 }
2592 }
drhbce04142017-02-23 00:58:362593 sqlite3VdbeAddOp0(v, OP_Expire);
drh74b0aad2024-02-19 19:56:402594
2595 /* In a schema with a large number of tables and indexes, scale back
2596 ** the analysis_limit to avoid excess run-time in the worst case.
2597 */
drhae71fa52024-02-19 23:58:262598 if( !db->mallocFailed && nLimit>0 && nBtree>100 ){
drh74b0aad2024-02-19 19:56:402599 int iAddr, iEnd;
2600 VdbeOp *aOp;
drhae71fa52024-02-19 23:58:262601 nLimit = 100*nLimit/nBtree;
drh74b0aad2024-02-19 19:56:402602 if( nLimit<100 ) nLimit = 100;
2603 aOp = sqlite3VdbeGetOp(v, 0);
2604 iEnd = sqlite3VdbeCurrentAddr(v);
2605 for(iAddr=0; iAddr<iEnd; iAddr++){
2606 if( aOp[iAddr].opcode==OP_SqlExec ) aOp[iAddr].p2 = nLimit;
2607 }
2608 }
drh72052a72017-02-17 16:26:342609 break;
2610 }
2611
2612 /*
drhf3603962012-09-07 16:46:592613 ** PRAGMA busy_timeout
2614 ** PRAGMA busy_timeout = N
2615 **
2616 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:572617 ** if one is set. If no busy handler or a different busy handler is set
2618 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2619 ** disables the timeout.
drhf3603962012-09-07 16:46:592620 */
drhd49c3582013-09-13 19:00:062621 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:012622 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:592623 if( zRight ){
2624 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2625 }
drhc232aca2016-12-15 16:01:172626 returnSingleInt(v, db->busyTimeout);
drh9ccd8652013-09-13 16:36:462627 break;
2628 }
drhf3603962012-09-07 16:46:592629
drh55e85ca2013-09-13 21:01:562630 /*
2631 ** PRAGMA soft_heap_limit
2632 ** PRAGMA soft_heap_limit = N
2633 **
drh51a74d42015-02-28 01:04:272634 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
2635 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
2636 ** specified and is a non-negative integer.
2637 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
2638 ** returns the same integer that would be returned by the
2639 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:562640 */
2641 case PragTyp_SOFT_HEAP_LIMIT: {
2642 sqlite3_int64 N;
drh9296c182014-07-23 13:40:492643 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:562644 sqlite3_soft_heap_limit64(N);
2645 }
drhc232aca2016-12-15 16:01:172646 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:562647 break;
2648 }
2649
drh03459612014-08-25 15:13:222650 /*
drh10c0e712019-04-25 18:15:382651 ** PRAGMA hard_heap_limit
2652 ** PRAGMA hard_heap_limit = N
2653 **
2654 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
2655 ** limit. The hard heap limit can be activated or lowered by this
2656 ** pragma, but not raised or deactivated. Only the
2657 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
2658 ** the hard heap limit. This allows an application to set a heap limit
2659 ** constraint that cannot be relaxed by an untrusted SQL script.
2660 */
2661 case PragTyp_HARD_HEAP_LIMIT: {
2662 sqlite3_int64 N;
2663 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
2664 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
2665 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
2666 }
drh31999c52019-11-14 17:46:322667 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
drh10c0e712019-04-25 18:15:382668 break;
2669 }
2670
2671 /*
drh03459612014-08-25 15:13:222672 ** PRAGMA threads
2673 ** PRAGMA threads = N
2674 **
2675 ** Configure the maximum number of worker threads. Return the new
2676 ** maximum, which might be less than requested.
2677 */
2678 case PragTyp_THREADS: {
2679 sqlite3_int64 N;
drh111544c2014-08-29 16:20:472680 if( zRight
drh03459612014-08-25 15:13:222681 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
2682 && N>=0
2683 ){
drh111544c2014-08-29 16:20:472684 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:222685 }
drhc232aca2016-12-15 16:01:172686 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:222687 break;
2688 }
2689
drh49a76a82020-03-31 20:57:062690 /*
2691 ** PRAGMA analysis_limit
2692 ** PRAGMA analysis_limit = N
2693 **
2694 ** Configure the maximum number of rows that ANALYZE will examine
2695 ** in each index that it looks at. Return the new limit.
2696 */
2697 case PragTyp_ANALYSIS_LIMIT: {
2698 sqlite3_int64 N;
2699 if( zRight
drhbc98f902021-10-14 17:30:322700 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
drh49a76a82020-03-31 20:57:062701 && N>=0
2702 ){
2703 db->nAnalysisLimit = (int)(N&0x7fffffff);
2704 }
drhbc98f902021-10-14 17:30:322705 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
drh49a76a82020-03-31 20:57:062706 break;
2707 }
2708
dougcurrie81c95ef2004-06-18 23:21:472709#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:202710 /*
2711 ** Report the current state of file logs for all databases
2712 */
drh9ccd8652013-09-13 16:36:462713 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:162714 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:202715 "unlocked", "shared", "reserved", "pending", "exclusive"
2716 };
2717 int i;
drh2d401ab2008-01-10 23:50:112718 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:202719 for(i=0; i<db->nDb; i++){
2720 Btree *pBt;
drh9e33c2c2007-08-31 18:34:592721 const char *zState = "unknown";
2722 int j;
drh69c33822016-08-18 14:33:112723 if( db->aDb[i].zDbSName==0 ) continue;
drh89ac8c12004-06-09 14:17:202724 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:442725 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:592726 zState = "closed";
larrybrbc917382023-06-07 08:40:312727 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
drh9e33c2c2007-08-31 18:34:592728 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2729 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:202730 }
drh69c33822016-08-18 14:33:112731 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
drh89ac8c12004-06-09 14:17:202732 }
drh9ccd8652013-09-13 16:36:462733 break;
2734 }
drh89ac8c12004-06-09 14:17:202735#endif
2736
drhb48c0d52020-02-07 01:12:532737#if defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:462738 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
drh21e2cab2006-09-25 18:01:572739 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:572740 sqlite3_activate_cerod(&zRight[6]);
2741 }
drh9ccd8652013-09-13 16:36:462742 }
2743 break;
drh21e2cab2006-09-25 18:01:572744#endif
drh3c4f2a42005-12-08 18:12:562745
drh9ccd8652013-09-13 16:36:462746 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:592747
dan9e1ab1a2017-01-05 19:32:482748 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
2749 ** purpose is to execute assert() statements to verify that if the
2750 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
larrybrbc917382023-06-07 08:40:312751 ** to the PRAGMA, the implementation has not added any OP_ResultRow
dan9e1ab1a2017-01-05 19:32:482752 ** instructions to the VM. */
2753 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
2754 sqlite3VdbeVerifyNoResultRow(v);
2755 }
2756
danielk1977e0048402004-06-15 16:51:012757pragma_out:
drh633e6d52008-07-28 19:34:532758 sqlite3DbFree(db, zLeft);
2759 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:242760}
drh2fcc1592016-12-15 20:59:032761#ifndef SQLITE_OMIT_VIRTUALTABLE
2762/*****************************************************************************
2763** Implementation of an eponymous virtual table that runs a pragma.
2764**
2765*/
2766typedef struct PragmaVtab PragmaVtab;
2767typedef struct PragmaVtabCursor PragmaVtabCursor;
2768struct PragmaVtab {
2769 sqlite3_vtab base; /* Base class. Must be first */
2770 sqlite3 *db; /* The database connection to which it belongs */
2771 const PragmaName *pName; /* Name of the pragma */
2772 u8 nHidden; /* Number of hidden columns */
2773 u8 iHidden; /* Index of the first hidden column */
2774};
2775struct PragmaVtabCursor {
2776 sqlite3_vtab_cursor base; /* Base class. Must be first */
2777 sqlite3_stmt *pPragma; /* The pragma statement to run */
2778 sqlite_int64 iRowid; /* Current rowid */
2779 char *azArg[2]; /* Value of the argument and schema */
2780};
2781
larrybrbc917382023-06-07 08:40:312782/*
drh2fcc1592016-12-15 20:59:032783** Pragma virtual table module xConnect method.
2784*/
2785static int pragmaVtabConnect(
2786 sqlite3 *db,
2787 void *pAux,
2788 int argc, const char *const*argv,
2789 sqlite3_vtab **ppVtab,
2790 char **pzErr
2791){
2792 const PragmaName *pPragma = (const PragmaName*)pAux;
2793 PragmaVtab *pTab = 0;
2794 int rc;
2795 int i, j;
2796 char cSep = '(';
2797 StrAccum acc;
2798 char zBuf[200];
2799
drh344a1bf2016-12-22 14:53:252800 UNUSED_PARAMETER(argc);
2801 UNUSED_PARAMETER(argv);
drh2fcc1592016-12-15 20:59:032802 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drh0cdbe1a2018-05-09 13:46:262803 sqlite3_str_appendall(&acc, "CREATE TABLE x");
drh2fcc1592016-12-15 20:59:032804 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
drh0cdbe1a2018-05-09 13:46:262805 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
drh2fcc1592016-12-15 20:59:032806 cSep = ',';
2807 }
drh9a63f092016-12-16 02:14:152808 if( i==0 ){
drh0cdbe1a2018-05-09 13:46:262809 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
drh9a63f092016-12-16 02:14:152810 i++;
2811 }
drh2fcc1592016-12-15 20:59:032812 j = 0;
2813 if( pPragma->mPragFlg & PragFlg_Result1 ){
drh0cdbe1a2018-05-09 13:46:262814 sqlite3_str_appendall(&acc, ",arg HIDDEN");
drh2fcc1592016-12-15 20:59:032815 j++;
2816 }
2817 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
drh0cdbe1a2018-05-09 13:46:262818 sqlite3_str_appendall(&acc, ",schema HIDDEN");
drh2fcc1592016-12-15 20:59:032819 j++;
2820 }
drh0cdbe1a2018-05-09 13:46:262821 sqlite3_str_append(&acc, ")", 1);
drh2fcc1592016-12-15 20:59:032822 sqlite3StrAccumFinish(&acc);
2823 assert( strlen(zBuf) < sizeof(zBuf)-1 );
2824 rc = sqlite3_declare_vtab(db, zBuf);
2825 if( rc==SQLITE_OK ){
2826 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
2827 if( pTab==0 ){
2828 rc = SQLITE_NOMEM;
2829 }else{
2830 memset(pTab, 0, sizeof(PragmaVtab));
2831 pTab->pName = pPragma;
2832 pTab->db = db;
2833 pTab->iHidden = i;
2834 pTab->nHidden = j;
2835 }
2836 }else{
2837 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2838 }
2839
2840 *ppVtab = (sqlite3_vtab*)pTab;
2841 return rc;
2842}
2843
larrybrbc917382023-06-07 08:40:312844/*
drh2fcc1592016-12-15 20:59:032845** Pragma virtual table module xDisconnect method.
2846*/
2847static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
2848 PragmaVtab *pTab = (PragmaVtab*)pVtab;
2849 sqlite3_free(pTab);
2850 return SQLITE_OK;
2851}
2852
2853/* Figure out the best index to use to search a pragma virtual table.
2854**
2855** There are not really any index choices. But we want to encourage the
2856** query planner to give == constraints on as many hidden parameters as
2857** possible, and especially on the first hidden parameter. So return a
2858** high cost if hidden parameters are unconstrained.
2859*/
2860static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2861 PragmaVtab *pTab = (PragmaVtab*)tab;
2862 const struct sqlite3_index_constraint *pConstraint;
2863 int i, j;
2864 int seen[2];
2865
drhae7045c2016-12-15 21:33:552866 pIdxInfo->estimatedCost = (double)1;
drh2fcc1592016-12-15 20:59:032867 if( pTab->nHidden==0 ){ return SQLITE_OK; }
2868 pConstraint = pIdxInfo->aConstraint;
2869 seen[0] = 0;
2870 seen[1] = 0;
2871 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
drh2fcc1592016-12-15 20:59:032872 if( pConstraint->iColumn < pTab->iHidden ) continue;
drh41c99452024-03-25 11:34:422873 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2874 if( pConstraint->usable==0 ) return SQLITE_CONSTRAINT;
drh2fcc1592016-12-15 20:59:032875 j = pConstraint->iColumn - pTab->iHidden;
2876 assert( j < 2 );
drhd7175eb2016-12-15 21:11:152877 seen[j] = i+1;
drh2fcc1592016-12-15 20:59:032878 }
2879 if( seen[0]==0 ){
2880 pIdxInfo->estimatedCost = (double)2147483647;
2881 pIdxInfo->estimatedRows = 2147483647;
2882 return SQLITE_OK;
2883 }
drhd7175eb2016-12-15 21:11:152884 j = seen[0]-1;
drh2fcc1592016-12-15 20:59:032885 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
2886 pIdxInfo->aConstraintUsage[j].omit = 1;
drh2fcc1592016-12-15 20:59:032887 pIdxInfo->estimatedCost = (double)20;
2888 pIdxInfo->estimatedRows = 20;
drh41c99452024-03-25 11:34:422889 if( seen[1] ){
2890 j = seen[1]-1;
2891 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
2892 pIdxInfo->aConstraintUsage[j].omit = 1;
2893 }
drh2fcc1592016-12-15 20:59:032894 return SQLITE_OK;
2895}
2896
2897/* Create a new cursor for the pragma virtual table */
2898static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
2899 PragmaVtabCursor *pCsr;
2900 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
2901 if( pCsr==0 ) return SQLITE_NOMEM;
2902 memset(pCsr, 0, sizeof(PragmaVtabCursor));
2903 pCsr->base.pVtab = pVtab;
2904 *ppCursor = &pCsr->base;
2905 return SQLITE_OK;
2906}
2907
2908/* Clear all content from pragma virtual table cursor. */
2909static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
2910 int i;
2911 sqlite3_finalize(pCsr->pPragma);
2912 pCsr->pPragma = 0;
dana40cae72024-05-07 17:58:072913 pCsr->iRowid = 0;
drh2fcc1592016-12-15 20:59:032914 for(i=0; i<ArraySize(pCsr->azArg); i++){
2915 sqlite3_free(pCsr->azArg[i]);
2916 pCsr->azArg[i] = 0;
2917 }
2918}
2919
2920/* Close a pragma virtual table cursor */
2921static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
2922 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
2923 pragmaVtabCursorClear(pCsr);
drh9a63f092016-12-16 02:14:152924 sqlite3_free(pCsr);
drh2fcc1592016-12-15 20:59:032925 return SQLITE_OK;
2926}
2927
2928/* Advance the pragma virtual table cursor to the next row */
2929static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
2930 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2931 int rc = SQLITE_OK;
2932
2933 /* Increment the xRowid value */
2934 pCsr->iRowid++;
drh9a63f092016-12-16 02:14:152935 assert( pCsr->pPragma );
2936 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
2937 rc = sqlite3_finalize(pCsr->pPragma);
2938 pCsr->pPragma = 0;
2939 pragmaVtabCursorClear(pCsr);
drh2fcc1592016-12-15 20:59:032940 }
2941 return rc;
2942}
2943
larrybrbc917382023-06-07 08:40:312944/*
drh2fcc1592016-12-15 20:59:032945** Pragma virtual table module xFilter method.
2946*/
2947static int pragmaVtabFilter(
larrybrbc917382023-06-07 08:40:312948 sqlite3_vtab_cursor *pVtabCursor,
drh2fcc1592016-12-15 20:59:032949 int idxNum, const char *idxStr,
2950 int argc, sqlite3_value **argv
2951){
2952 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2953 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2954 int rc;
drhd8b72002016-12-16 04:20:272955 int i, j;
drh2fcc1592016-12-15 20:59:032956 StrAccum acc;
2957 char *zSql;
2958
drh344a1bf2016-12-22 14:53:252959 UNUSED_PARAMETER(idxNum);
2960 UNUSED_PARAMETER(idxStr);
drh2fcc1592016-12-15 20:59:032961 pragmaVtabCursorClear(pCsr);
drhd8b72002016-12-16 04:20:272962 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
2963 for(i=0; i<argc; i++, j++){
dand8ecefa2017-07-15 20:48:302964 const char *zText = (const char*)sqlite3_value_text(argv[i]);
drhd8b72002016-12-16 04:20:272965 assert( j<ArraySize(pCsr->azArg) );
dand8ecefa2017-07-15 20:48:302966 assert( pCsr->azArg[j]==0 );
2967 if( zText ){
2968 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
2969 if( pCsr->azArg[j]==0 ){
2970 return SQLITE_NOMEM;
2971 }
drh2fcc1592016-12-15 20:59:032972 }
2973 }
drhd7175eb2016-12-15 21:11:152974 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
drh0cdbe1a2018-05-09 13:46:262975 sqlite3_str_appendall(&acc, "PRAGMA ");
drh2fcc1592016-12-15 20:59:032976 if( pCsr->azArg[1] ){
drh0cdbe1a2018-05-09 13:46:262977 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
drh2fcc1592016-12-15 20:59:032978 }
drh0cdbe1a2018-05-09 13:46:262979 sqlite3_str_appendall(&acc, pTab->pName->zName);
drh2fcc1592016-12-15 20:59:032980 if( pCsr->azArg[0] ){
drh0cdbe1a2018-05-09 13:46:262981 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
drh2fcc1592016-12-15 20:59:032982 }
2983 zSql = sqlite3StrAccumFinish(&acc);
2984 if( zSql==0 ) return SQLITE_NOMEM;
2985 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
2986 sqlite3_free(zSql);
2987 if( rc!=SQLITE_OK ){
2988 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
2989 return rc;
2990 }
2991 return pragmaVtabNext(pVtabCursor);
2992}
2993
2994/*
2995** Pragma virtual table module xEof method.
2996*/
2997static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
2998 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2999 return (pCsr->pPragma==0);
3000}
3001
3002/* The xColumn method simply returns the corresponding column from
larrybrbc917382023-06-07 08:40:313003** the PRAGMA.
drh2fcc1592016-12-15 20:59:033004*/
3005static int pragmaVtabColumn(
larrybrbc917382023-06-07 08:40:313006 sqlite3_vtab_cursor *pVtabCursor,
3007 sqlite3_context *ctx,
drh2fcc1592016-12-15 20:59:033008 int i
3009){
3010 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
3011 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
3012 if( i<pTab->iHidden ){
3013 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
3014 }else{
3015 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
3016 }
3017 return SQLITE_OK;
3018}
3019
larrybrbc917382023-06-07 08:40:313020/*
drh2fcc1592016-12-15 20:59:033021** Pragma virtual table module xRowid method.
3022*/
3023static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
3024 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
3025 *p = pCsr->iRowid;
3026 return SQLITE_OK;
3027}
3028
3029/* The pragma virtual table object */
3030static const sqlite3_module pragmaVtabModule = {
3031 0, /* iVersion */
3032 0, /* xCreate - create a table */
3033 pragmaVtabConnect, /* xConnect - connect to an existing table */
3034 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
3035 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
3036 0, /* xDestroy - Drop a table */
3037 pragmaVtabOpen, /* xOpen - open a cursor */
3038 pragmaVtabClose, /* xClose - close a cursor */
3039 pragmaVtabFilter, /* xFilter - configure scan constraints */
3040 pragmaVtabNext, /* xNext - advance a cursor */
3041 pragmaVtabEof, /* xEof */
3042 pragmaVtabColumn, /* xColumn - read data */
3043 pragmaVtabRowid, /* xRowid - read data */
3044 0, /* xUpdate - write data */
3045 0, /* xBegin - begin transaction */
3046 0, /* xSync - sync transaction */
3047 0, /* xCommit - commit transaction */
3048 0, /* xRollback - rollback transaction */
3049 0, /* xFindFunction - function overloading */
3050 0, /* xRename - rename the table */
3051 0, /* xSavepoint */
3052 0, /* xRelease */
drh84c501b2018-11-05 23:01:453053 0, /* xRollbackTo */
drh19358872023-10-06 12:51:053054 0, /* xShadowName */
3055 0 /* xIntegrity */
drh2fcc1592016-12-15 20:59:033056};
3057
3058/*
3059** Check to see if zTabName is really the name of a pragma. If it is,
3060** then register an eponymous virtual table for that pragma and return
3061** a pointer to the Module object for the new virtual table.
3062*/
3063Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
3064 const PragmaName *pName;
3065 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
3066 pName = pragmaLocate(zName+7);
3067 if( pName==0 ) return 0;
3068 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
3069 assert( sqlite3HashFind(&db->aModule, zName)==0 );
drhd7175eb2016-12-15 21:11:153070 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
drh2fcc1592016-12-15 20:59:033071}
3072
3073#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh13d70422004-11-13 15:59:143074
drh8bfdf722009-06-19 14:06:033075#endif /* SQLITE_OMIT_PRAGMA */