blob: ba84af757551dc962d33a8d64c7451cd258dec3c [file] [log] [blame]
drhc11d4f92003-04-06 21:08:24 +00001/*
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:24 +000013*/
14#include "sqliteInt.h"
15
drh9ccd8652013-09-13 16:36:46 +000016#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:54 +000025** The "pragma.h" include file is an automatically generated file that
26** that includes the PragType_XXXX macro definitions and the aPragmaName[]
27** object. This ensures that the aPragmaName[] table is arranged in
28** lexicographical order to facility a binary search of the pragma name.
29** Do not edit pragma.h directly. Edit and rerun the script in at
30** ../tool/mkpragmatab.tcl. */
31#include "pragma.h"
drh9ccd8652013-09-13 16:36:46 +000032
drhc11d4f92003-04-06 21:08:24 +000033/*
drhc11d4f92003-04-06 21:08:24 +000034** Interpret the given string as a safety level. Return 0 for OFF,
drh6841b1c2016-02-03 19:20:15 +000035** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
36** unrecognized string argument. The FULL and EXTRA option is disallowed
drh908c0052012-01-30 18:40:55 +000037** if the omitFull parameter it 1.
drhc11d4f92003-04-06 21:08:24 +000038**
39** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000040** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000041** to support legacy SQL code. The safety level used to be boolean
42** and older scripts may have used numbers 0 for OFF and 1 for ON.
43*/
drheac5bd72014-07-25 21:35:39 +000044static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
drh6841b1c2016-02-03 19:20:15 +000045 /* 123456789 123456789 123 */
46 static const char zText[] = "onoffalseyestruextrafull";
47 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
48 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
49 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
50 /* on no off false yes true extra full */
drh722e95a2004-10-25 20:33:44 +000051 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +000052 if( sqlite3Isdigit(*z) ){
drh60ac3f42010-11-23 18:59:27 +000053 return (u8)sqlite3Atoi(z);
drhc11d4f92003-04-06 21:08:24 +000054 }
drhea678832008-12-10 19:26:22 +000055 n = sqlite3Strlen30(z);
drh6841b1c2016-02-03 19:20:15 +000056 for(i=0; i<ArraySize(iLength); i++){
57 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
58 && (!omitFull || iValue[i]<=1)
59 ){
drh722e95a2004-10-25 20:33:44 +000060 return iValue[i];
61 }
drhc11d4f92003-04-06 21:08:24 +000062 }
drh908c0052012-01-30 18:40:55 +000063 return dflt;
drhc11d4f92003-04-06 21:08:24 +000064}
65
66/*
drh722e95a2004-10-25 20:33:44 +000067** Interpret the given string as a boolean value.
68*/
drheac5bd72014-07-25 21:35:39 +000069u8 sqlite3GetBoolean(const char *z, u8 dflt){
drh38d9c612012-01-31 14:24:47 +000070 return getSafetyLevel(z,1,dflt)!=0;
drh722e95a2004-10-25 20:33:44 +000071}
72
drhc7dc9bf2011-06-03 13:02:57 +000073/* The sqlite3GetBoolean() function is used by other modules but the
74** remainder of this file is specific to PRAGMA processing. So omit
75** the rest of the file if PRAGMAs are omitted from the build.
76*/
77#if !defined(SQLITE_OMIT_PRAGMA)
78
danielk197741483462007-03-24 16:45:04 +000079/*
80** Interpret the given string as a locking mode value.
81*/
82static int getLockingMode(const char *z){
83 if( z ){
84 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
85 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
86 }
87 return PAGER_LOCKINGMODE_QUERY;
88}
89
danielk1977dddbcdc2007-04-26 14:42:34 +000090#ifndef SQLITE_OMIT_AUTOVACUUM
91/*
92** Interpret the given string as an auto-vacuum mode value.
93**
94** The following strings, "none", "full" and "incremental" are
95** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
96*/
97static int getAutoVacuum(const char *z){
98 int i;
99 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
100 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
101 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
drh60ac3f42010-11-23 18:59:27 +0000102 i = sqlite3Atoi(z);
drh4f21c4a2008-12-10 22:15:00 +0000103 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +0000104}
105#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
106
danielk1977b84f96f2005-01-20 11:32:23 +0000107#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +0000108/*
drh90f5ecb2004-07-22 01:19:35 +0000109** Interpret the given string as a temp db location. Return 1 for file
110** backed temporary databases, 2 for the Red-Black tree in memory database
111** and 0 to use the compile-time default.
112*/
113static int getTempStore(const char *z){
114 if( z[0]>='0' && z[0]<='2' ){
115 return z[0] - '0';
116 }else if( sqlite3StrICmp(z, "file")==0 ){
117 return 1;
118 }else if( sqlite3StrICmp(z, "memory")==0 ){
119 return 2;
120 }else{
121 return 0;
122 }
123}
drhbf216272005-02-26 18:10:44 +0000124#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000125
drhbf216272005-02-26 18:10:44 +0000126#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000127/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000128** Invalidate temp storage, either when the temp storage is changed
129** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000130*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000131static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000132 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000133 if( db->aDb[1].pBt!=0 ){
drh99744fa2020-08-25 19:09:07 +0000134 if( !db->autoCommit
135 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
136 ){
drh90f5ecb2004-07-22 01:19:35 +0000137 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
138 "from within a transaction");
139 return SQLITE_ERROR;
140 }
141 sqlite3BtreeClose(db->aDb[1].pBt);
142 db->aDb[1].pBt = 0;
drh81028a42012-05-15 18:28:27 +0000143 sqlite3ResetAllSchemasOfConnection(db);
drh90f5ecb2004-07-22 01:19:35 +0000144 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000145 return SQLITE_OK;
146}
drhbf216272005-02-26 18:10:44 +0000147#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000148
drhbf216272005-02-26 18:10:44 +0000149#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000150/*
151** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000152** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000153** or DEFAULT_TEMP_STORE pragmas.
154*/
155static int changeTempStorage(Parse *pParse, const char *zStorageType){
156 int ts = getTempStore(zStorageType);
157 sqlite3 *db = pParse->db;
158 if( db->temp_store==ts ) return SQLITE_OK;
159 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
160 return SQLITE_ERROR;
161 }
drh4f21c4a2008-12-10 22:15:00 +0000162 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000163 return SQLITE_OK;
164}
drhbf216272005-02-26 18:10:44 +0000165#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000166
167/*
drhc232aca2016-12-15 16:01:17 +0000168** Set result column names for a pragma.
drhb460e522015-09-03 03:29:51 +0000169*/
drhc232aca2016-12-15 16:01:17 +0000170static void setPragmaResultColumnNames(
drh2fcc1592016-12-15 20:59:03 +0000171 Vdbe *v, /* The query under construction */
172 const PragmaName *pPragma /* The pragma */
drhb460e522015-09-03 03:29:51 +0000173){
drhc232aca2016-12-15 16:01:17 +0000174 u8 n = pPragma->nPragCName;
175 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
176 if( n==0 ){
177 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
178 }else{
179 int i, j;
180 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
181 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
182 }
drhb460e522015-09-03 03:29:51 +0000183 }
184}
drhb460e522015-09-03 03:29:51 +0000185
186/*
drh90f5ecb2004-07-22 01:19:35 +0000187** Generate code to return a single integer value.
188*/
drhc232aca2016-12-15 16:01:17 +0000189static void returnSingleInt(Vdbe *v, i64 value){
drh7cc023c2015-09-03 04:28:25 +0000190 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
drh7cc023c2015-09-03 04:28:25 +0000191 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
192}
193
194/*
195** Generate code to return a single text value.
196*/
197static void returnSingleText(
198 Vdbe *v, /* Prepared statement under construction */
drh7cc023c2015-09-03 04:28:25 +0000199 const char *zValue /* Value to be returned */
200){
201 if( zValue ){
drh076e85f2015-09-03 13:46:12 +0000202 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
drh7cc023c2015-09-03 04:28:25 +0000203 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
204 }
drh90f5ecb2004-07-22 01:19:35 +0000205}
206
drhd3605a42013-08-17 15:42:29 +0000207
208/*
209** Set the safety_level and pager flags for pager iDb. Or if iDb<0
210** set these values for all pagers.
211*/
212#ifndef SQLITE_OMIT_PAGER_PRAGMAS
213static void setAllPagerFlags(sqlite3 *db){
214 if( db->autoCommit ){
215 Db *pDb = db->aDb;
216 int n = db->nDb;
217 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
218 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
219 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
220 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
221 == PAGER_FLAGS_MASK );
222 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
223 while( (n--) > 0 ){
224 if( pDb->pBt ){
225 sqlite3BtreeSetPagerFlags(pDb->pBt,
226 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
227 }
228 pDb++;
229 }
230 }
231}
drhfeb56e02013-08-23 17:33:46 +0000232#else
233# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29 +0000234#endif
235
236
drhd2cb50b2009-01-09 21:41:17 +0000237/*
238** Return a human-readable name for a constraint resolution action.
239*/
danba9108b2009-09-22 07:13:42 +0000240#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000241static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000242 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000243 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000244 case OE_SetNull: zName = "SET NULL"; break;
245 case OE_SetDflt: zName = "SET DEFAULT"; break;
246 case OE_Cascade: zName = "CASCADE"; break;
247 case OE_Restrict: zName = "RESTRICT"; break;
248 default: zName = "NO ACTION";
249 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000250 }
drhd2cb50b2009-01-09 21:41:17 +0000251 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000252}
danba9108b2009-09-22 07:13:42 +0000253#endif
danielk197750af3e12008-10-10 17:47:21 +0000254
dane04dc882010-04-20 18:53:15 +0000255
256/*
257** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
258** defined in pager.h. This function returns the associated lowercase
259** journal-mode name.
260*/
261const char *sqlite3JournalModename(int eMode){
262 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000263 "delete", "persist", "off", "truncate", "memory"
264#ifndef SQLITE_OMIT_WAL
265 , "wal"
266#endif
dane04dc882010-04-20 18:53:15 +0000267 };
268 assert( PAGER_JOURNALMODE_DELETE==0 );
269 assert( PAGER_JOURNALMODE_PERSIST==1 );
270 assert( PAGER_JOURNALMODE_OFF==2 );
271 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
272 assert( PAGER_JOURNALMODE_MEMORY==4 );
273 assert( PAGER_JOURNALMODE_WAL==5 );
274 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
275
276 if( eMode==ArraySize(azModeName) ) return 0;
277 return azModeName[eMode];
278}
279
drh87223182004-02-21 14:00:29 +0000280/*
drh2fcc1592016-12-15 20:59:03 +0000281** Locate a pragma in the aPragmaName[] array.
282*/
283static const PragmaName *pragmaLocate(const char *zName){
mistachkin2e525322017-02-01 22:43:08 +0000284 int upr, lwr, mid = 0, rc;
drh2fcc1592016-12-15 20:59:03 +0000285 lwr = 0;
286 upr = ArraySize(aPragmaName)-1;
287 while( lwr<=upr ){
288 mid = (lwr+upr)/2;
289 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
290 if( rc==0 ) break;
291 if( rc<0 ){
292 upr = mid - 1;
293 }else{
294 lwr = mid + 1;
295 }
296 }
297 return lwr>upr ? 0 : &aPragmaName[mid];
298}
299
300/*
drh79d5bc82020-01-04 01:43:02 +0000301** Create zero or more entries in the output for the SQL functions
302** defined by FuncDef p.
303*/
drh337ca512020-01-04 19:58:28 +0000304static void pragmaFunclistLine(
305 Vdbe *v, /* The prepared statement being created */
306 FuncDef *p, /* A particular function definition */
307 int isBuiltin, /* True if this is a built-in function */
308 int showInternFuncs /* True if showing internal functions */
309){
drh79d5bc82020-01-04 01:43:02 +0000310 for(; p; p=p->pNext){
311 const char *zType;
drh79d5bc82020-01-04 01:43:02 +0000312 static const u32 mask =
313 SQLITE_DETERMINISTIC |
314 SQLITE_DIRECTONLY |
315 SQLITE_SUBTYPE |
drh337ca512020-01-04 19:58:28 +0000316 SQLITE_INNOCUOUS |
317 SQLITE_FUNC_INTERNAL
drh79d5bc82020-01-04 01:43:02 +0000318 ;
drhb84fda32020-01-09 16:28:50 +0000319 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
320
321 assert( SQLITE_FUNC_ENCMASK==0x3 );
322 assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 );
323 assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 );
324 assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 );
drh337ca512020-01-04 19:58:28 +0000325
drh79d5bc82020-01-04 01:43:02 +0000326 if( p->xSFunc==0 ) continue;
drh337ca512020-01-04 19:58:28 +0000327 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
328 && showInternFuncs==0
329 ){
330 continue;
331 }
drh79d5bc82020-01-04 01:43:02 +0000332 if( p->xValue!=0 ){
333 zType = "w";
334 }else if( p->xFinalize!=0 ){
335 zType = "a";
336 }else{
337 zType = "s";
338 }
drh79d5bc82020-01-04 01:43:02 +0000339 sqlite3VdbeMultiLoad(v, 1, "sissii",
340 p->zName, isBuiltin,
drhb84fda32020-01-09 16:28:50 +0000341 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
drh79d5bc82020-01-04 01:43:02 +0000342 p->nArg,
343 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
344 );
345 }
346}
347
348
349/*
drh66accfc2017-02-22 18:04:42 +0000350** Helper subroutine for PRAGMA integrity_check:
351**
drh9ecd7082017-09-10 01:06:05 +0000352** Generate code to output a single-column result row with a value of the
353** string held in register 3. Decrement the result count in register 1
354** and halt if the maximum number of result rows have been issued.
drh66accfc2017-02-22 18:04:42 +0000355*/
drh9ecd7082017-09-10 01:06:05 +0000356static int integrityCheckResultRow(Vdbe *v){
drh66accfc2017-02-22 18:04:42 +0000357 int addr;
drh9ecd7082017-09-10 01:06:05 +0000358 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh66accfc2017-02-22 18:04:42 +0000359 addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
360 VdbeCoverage(v);
drh9ecd7082017-09-10 01:06:05 +0000361 sqlite3VdbeAddOp0(v, OP_Halt);
drh66accfc2017-02-22 18:04:42 +0000362 return addr;
363}
364
365/*
drhc11d4f92003-04-06 21:08:24 +0000366** Process a pragma statement.
367**
368** Pragmas are of this form:
369**
drh9b0cf342015-11-12 14:57:19 +0000370** PRAGMA [schema.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000371**
372** The identifier might also be a string. The value is a string, and
373** identifier, or a number. If minusFlag is true, then the value is
374** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000375**
376** If the left side is "database.id" then pId1 is the database name
377** and pId2 is the id. If the left side is just "id" then pId1 is the
378** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000379*/
danielk197791cf71b2004-06-26 06:37:06 +0000380void sqlite3Pragma(
381 Parse *pParse,
drh9b0cf342015-11-12 14:57:19 +0000382 Token *pId1, /* First part of [schema.]id field */
383 Token *pId2, /* Second part of [schema.]id field, or NULL */
danielk197791cf71b2004-06-26 06:37:06 +0000384 Token *pValue, /* Token for <value>, or NULL */
385 int minusFlag /* True if a '-' sign preceded <value> */
386){
387 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
388 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
389 const char *zDb = 0; /* The database name */
390 Token *pId; /* Pointer to <id> token */
drh3fa97302012-02-22 16:58:36 +0000391 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000392 int iDb; /* Database index for <database> */
drh06fd5d62012-02-22 14:45:19 +0000393 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
394 sqlite3 *db = pParse->db; /* The database connection */
395 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000396 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drh2fcc1592016-12-15 20:59:03 +0000397 const PragmaName *pPragma; /* The pragma */
drh06fd5d62012-02-22 14:45:19 +0000398
drhc11d4f92003-04-06 21:08:24 +0000399 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000400 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000401 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000402
drh9b0cf342015-11-12 14:57:19 +0000403 /* Interpret the [schema.] part of the pragma statement. iDb is the
danielk197791cf71b2004-06-26 06:37:06 +0000404 ** index of the database this pragma is being applied to in db.aDb[]. */
405 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
406 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000407 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000408
danielk1977ddfb2f02006-02-17 12:25:14 +0000409 /* If the temp database has been explicitly named as part of the
410 ** pragma, make sure it is open.
411 */
412 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
413 return;
414 }
415
drh17435752007-08-16 04:30:38 +0000416 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000417 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000418 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000419 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000420 }else{
drh17435752007-08-16 04:30:38 +0000421 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000422 }
danielk197791cf71b2004-06-26 06:37:06 +0000423
drhd2cb50b2009-01-09 21:41:17 +0000424 assert( pId2 );
drh69c33822016-08-18 14:33:11 +0000425 zDb = pId2->n>0 ? pDb->zDbSName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000426 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000427 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000428 }
drh06fd5d62012-02-22 14:45:19 +0000429
430 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
431 ** connection. If it returns SQLITE_OK, then assume that the VFS
432 ** handled the pragma and generate a no-op prepared statement.
drh8dd7a6a2015-03-06 04:37:26 +0000433 **
434 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
435 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
436 ** object corresponding to the database file to which the pragma
437 ** statement refers.
438 **
439 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
440 ** file control is an array of pointers to strings (char**) in which the
441 ** second element of the array is the name of the pragma and the third
442 ** element is the argument to the pragma or NULL if the pragma has no
443 ** argument.
drh06fd5d62012-02-22 14:45:19 +0000444 */
drh3fa97302012-02-22 16:58:36 +0000445 aFcntl[0] = 0;
446 aFcntl[1] = zLeft;
447 aFcntl[2] = zRight;
448 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000449 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000450 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
451 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000452 sqlite3VdbeSetNumCols(v, 1);
453 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
454 returnSingleText(v, aFcntl[0]);
drh7cc023c2015-09-03 04:28:25 +0000455 sqlite3_free(aFcntl[0]);
drh9ccd8652013-09-13 16:36:46 +0000456 goto pragma_out;
457 }
458 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000459 if( aFcntl[0] ){
460 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
461 sqlite3_free(aFcntl[0]);
462 }
463 pParse->nErr++;
464 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000465 goto pragma_out;
466 }
467
468 /* Locate the pragma in the lookup table */
drh2fcc1592016-12-15 20:59:03 +0000469 pPragma = pragmaLocate(zLeft);
drhbc98f902021-10-14 17:30:32 +0000470 if( pPragma==0 ){
471 /* IMP: R-43042-22504 No error messages are generated if an
472 ** unknown pragma is issued. */
473 goto pragma_out;
474 }
drh9ccd8652013-09-13 16:36:46 +0000475
drhf63936e2013-10-03 14:08:07 +0000476 /* Make sure the database schema is loaded if the pragma requires that */
drhc232aca2016-12-15 16:01:17 +0000477 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
drhf63936e2013-10-03 14:08:07 +0000478 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
479 }
480
drhc232aca2016-12-15 16:01:17 +0000481 /* Register the result column names for pragmas that return results */
dan9e1ab1a2017-01-05 19:32:48 +0000482 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
483 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
484 ){
drhc232aca2016-12-15 16:01:17 +0000485 setPragmaResultColumnNames(v, pPragma);
486 }
487
drh9ccd8652013-09-13 16:36:46 +0000488 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01 +0000489 switch( pPragma->ePragTyp ){
drh9ccd8652013-09-13 16:36:46 +0000490
drhe73c9142011-11-09 16:12:24 +0000491#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000492 /*
drh9b0cf342015-11-12 14:57:19 +0000493 ** PRAGMA [schema.]default_cache_size
494 ** PRAGMA [schema.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000495 **
496 ** The first form reports the current persistent setting for the
497 ** page cache size. The value returned is the maximum number of
498 ** pages in the page cache. The second form sets both the current
499 ** page cache size value and the persistent page cache size value
500 ** stored in the database file.
501 **
drh93791ea2010-04-26 17:36:35 +0000502 ** Older versions of SQLite would set the default cache size to a
503 ** negative number to indicate synchronous=OFF. These days, synchronous
504 ** is always on by default regardless of the sign of the default cache
505 ** size. But continue to take the absolute value of the default cache
506 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000507 */
drh9ccd8652013-09-13 16:36:46 +0000508 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09 +0000509 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000510 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000511 { OP_Transaction, 0, 0, 0}, /* 0 */
512 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000513 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000514 { OP_Integer, 0, 2, 0},
515 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000516 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000517 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000518 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000519 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000520 };
drh2ce18652016-01-16 20:50:21 +0000521 VdbeOp *aOp;
drhfb982642007-08-30 01:19:59 +0000522 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000523 if( !zRight ){
drh3c84ddf2008-01-09 02:15:38 +0000524 pParse->nMem += 2;
drhdad300d2016-01-18 00:20:26 +0000525 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
drh2ce18652016-01-16 20:50:21 +0000526 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
drhdad300d2016-01-18 00:20:26 +0000527 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000528 aOp[0].p1 = iDb;
529 aOp[1].p1 = iDb;
530 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
drhc11d4f92003-04-06 21:08:24 +0000531 }else{
drhd50ffc42011-03-08 02:38:28 +0000532 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000533 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh1861afc2016-02-01 21:48:34 +0000534 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
drh21206082011-04-04 18:22:02 +0000535 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000536 pDb->pSchema->cache_size = size;
537 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000538 }
drh9ccd8652013-09-13 16:36:46 +0000539 break;
540 }
drhe73c9142011-11-09 16:12:24 +0000541#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000542
drhe73c9142011-11-09 16:12:24 +0000543#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000544 /*
drh9b0cf342015-11-12 14:57:19 +0000545 ** PRAGMA [schema.]page_size
546 ** PRAGMA [schema.]page_size=N
drh90f5ecb2004-07-22 01:19:35 +0000547 **
548 ** The first form reports the current setting for the
549 ** database page size in bytes. The second form sets the
550 ** database page size value. The value can only be set if
551 ** the database has not yet been created.
552 */
drh9ccd8652013-09-13 16:36:46 +0000553 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000554 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000555 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000556 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000557 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drhc232aca2016-12-15 16:01:17 +0000558 returnSingleInt(v, size);
drh90f5ecb2004-07-22 01:19:35 +0000559 }else{
danielk1977992772c2007-08-30 10:07:38 +0000560 /* Malloc may fail when setting the page-size, as there is an internal
561 ** buffer that the pager module resizes using sqlite3_realloc().
562 */
drh60ac3f42010-11-23 18:59:27 +0000563 db->nextPagesize = sqlite3Atoi(zRight);
drhe937df82020-05-07 01:56:57 +0000564 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
drh4a642b62016-02-05 01:55:27 +0000565 sqlite3OomFault(db);
danielk1977992772c2007-08-30 10:07:38 +0000566 }
drh90f5ecb2004-07-22 01:19:35 +0000567 }
drh9ccd8652013-09-13 16:36:46 +0000568 break;
569 }
danielk197741483462007-03-24 16:45:04 +0000570
571 /*
drh9b0cf342015-11-12 14:57:19 +0000572 ** PRAGMA [schema.]secure_delete
drha5907a82017-06-19 11:44:22 +0000573 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
drh5b47efa2010-02-12 18:18:39 +0000574 **
575 ** The first form reports the current setting for the
576 ** secure_delete flag. The second form changes the secure_delete
drha5907a82017-06-19 11:44:22 +0000577 ** flag setting and reports the new value.
drh5b47efa2010-02-12 18:18:39 +0000578 */
drh9ccd8652013-09-13 16:36:46 +0000579 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000580 Btree *pBt = pDb->pBt;
581 int b = -1;
582 assert( pBt!=0 );
583 if( zRight ){
drha5907a82017-06-19 11:44:22 +0000584 if( sqlite3_stricmp(zRight, "fast")==0 ){
585 b = 2;
586 }else{
587 b = sqlite3GetBoolean(zRight, 0);
588 }
drh5b47efa2010-02-12 18:18:39 +0000589 }
drhaf034ed2010-02-12 19:46:26 +0000590 if( pId2->n==0 && b>=0 ){
591 int ii;
592 for(ii=0; ii<db->nDb; ii++){
593 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
594 }
595 }
drh5b47efa2010-02-12 18:18:39 +0000596 b = sqlite3BtreeSecureDelete(pBt, b);
drhc232aca2016-12-15 16:01:17 +0000597 returnSingleInt(v, b);
drh9ccd8652013-09-13 16:36:46 +0000598 break;
599 }
drh5b47efa2010-02-12 18:18:39 +0000600
601 /*
drh9b0cf342015-11-12 14:57:19 +0000602 ** PRAGMA [schema.]max_page_count
603 ** PRAGMA [schema.]max_page_count=N
drh60ac3f42010-11-23 18:59:27 +0000604 **
605 ** The first form reports the current setting for the
606 ** maximum number of pages in the database file. The
607 ** second form attempts to change this setting. Both
608 ** forms return the current setting.
609 **
drhe73c9142011-11-09 16:12:24 +0000610 ** The absolute value of N is used. This is undocumented and might
611 ** change. The only purpose is to provide an easy way to test
612 ** the sqlite3AbsInt32() function.
613 **
drh9b0cf342015-11-12 14:57:19 +0000614 ** PRAGMA [schema.]page_count
danielk197759a93792008-05-15 17:48:20 +0000615 **
616 ** Return the number of pages in the specified database.
617 */
drh9ccd8652013-09-13 16:36:46 +0000618 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000619 int iReg;
drhe9261db2020-07-20 12:47:32 +0000620 i64 x = 0;
danielk197759a93792008-05-15 17:48:20 +0000621 sqlite3CodeVerifySchema(pParse, iDb);
622 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000623 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000624 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
625 }else{
drhe9261db2020-07-20 12:47:32 +0000626 if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){
627 if( x<0 ) x = 0;
628 else if( x>0xfffffffe ) x = 0xfffffffe;
629 }else{
630 x = 0;
631 }
632 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, (int)x);
drh60ac3f42010-11-23 18:59:27 +0000633 }
danielk197759a93792008-05-15 17:48:20 +0000634 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
drh9ccd8652013-09-13 16:36:46 +0000635 break;
636 }
danielk197759a93792008-05-15 17:48:20 +0000637
638 /*
drh9b0cf342015-11-12 14:57:19 +0000639 ** PRAGMA [schema.]locking_mode
640 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
danielk197741483462007-03-24 16:45:04 +0000641 */
drh9ccd8652013-09-13 16:36:46 +0000642 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000643 const char *zRet = "normal";
644 int eMode = getLockingMode(zRight);
645
646 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
647 /* Simple "PRAGMA locking_mode;" statement. This is a query for
648 ** the current default locking mode (which may be different to
649 ** the locking-mode of the main database).
650 */
651 eMode = db->dfltLockMode;
652 }else{
653 Pager *pPager;
654 if( pId2->n==0 ){
655 /* This indicates that no database name was specified as part
656 ** of the PRAGMA command. In this case the locking-mode must be
657 ** set on all attached databases, as well as the main db file.
658 **
659 ** Also, the sqlite3.dfltLockMode variable is set so that
660 ** any subsequently attached databases also use the specified
661 ** locking mode.
662 */
663 int ii;
664 assert(pDb==&db->aDb[0]);
665 for(ii=2; ii<db->nDb; ii++){
666 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
667 sqlite3PagerLockingMode(pPager, eMode);
668 }
drh4f21c4a2008-12-10 22:15:00 +0000669 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000670 }
671 pPager = sqlite3BtreePager(pDb->pBt);
672 eMode = sqlite3PagerLockingMode(pPager, eMode);
673 }
674
drh9ccd8652013-09-13 16:36:46 +0000675 assert( eMode==PAGER_LOCKINGMODE_NORMAL
676 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000677 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
678 zRet = "exclusive";
679 }
drhc232aca2016-12-15 16:01:17 +0000680 returnSingleText(v, zRet);
drh9ccd8652013-09-13 16:36:46 +0000681 break;
682 }
drh3b020132008-04-17 17:02:01 +0000683
684 /*
drh9b0cf342015-11-12 14:57:19 +0000685 ** PRAGMA [schema.]journal_mode
686 ** PRAGMA [schema.]journal_mode =
drh3ebaee92010-05-06 21:37:22 +0000687 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000688 */
drh9ccd8652013-09-13 16:36:46 +0000689 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000690 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
691 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000692
drh3b020132008-04-17 17:02:01 +0000693 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000694 /* If there is no "=MODE" part of the pragma, do a query for the
695 ** current mode */
drh3b020132008-04-17 17:02:01 +0000696 eMode = PAGER_JOURNALMODE_QUERY;
697 }else{
dane04dc882010-04-20 18:53:15 +0000698 const char *zMode;
drhea678832008-12-10 19:26:22 +0000699 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000700 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000701 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
702 }
703 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000704 /* If the "=MODE" part does not match any known journal mode,
705 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000706 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000707 }
drh6c35b302019-05-17 20:37:17 +0000708 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
709 /* Do not allow journal-mode "OFF" in defensive since the database
710 ** can become corrupted using ordinary SQL when the journal is off */
711 eMode = PAGER_JOURNALMODE_QUERY;
712 }
drh3b020132008-04-17 17:02:01 +0000713 }
drhc6b2a0f2010-07-08 17:40:37 +0000714 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
715 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
716 iDb = 0;
717 pId2->n = 1;
718 }
719 for(ii=db->nDb-1; ii>=0; ii--){
720 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
721 sqlite3VdbeUsesBtree(v, ii);
722 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000723 }
drh3b020132008-04-17 17:02:01 +0000724 }
drh3b020132008-04-17 17:02:01 +0000725 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000726 break;
727 }
danielk1977b53e4962008-06-04 06:45:59 +0000728
729 /*
drh9b0cf342015-11-12 14:57:19 +0000730 ** PRAGMA [schema.]journal_size_limit
731 ** PRAGMA [schema.]journal_size_limit=N
danielk1977b53e4962008-06-04 06:45:59 +0000732 **
drha9e364f2009-01-13 20:14:15 +0000733 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000734 */
drh9ccd8652013-09-13 16:36:46 +0000735 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000736 Pager *pPager = sqlite3BtreePager(pDb->pBt);
737 i64 iLimit = -2;
738 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000739 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000740 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000741 }
742 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drhc232aca2016-12-15 16:01:17 +0000743 returnSingleInt(v, iLimit);
drh9ccd8652013-09-13 16:36:46 +0000744 break;
745 }
danielk1977b53e4962008-06-04 06:45:59 +0000746
drh13d70422004-11-13 15:59:14 +0000747#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000748
749 /*
drh9b0cf342015-11-12 14:57:19 +0000750 ** PRAGMA [schema.]auto_vacuum
751 ** PRAGMA [schema.]auto_vacuum=N
danielk1977951af802004-11-05 15:45:09 +0000752 **
drha9e364f2009-01-13 20:14:15 +0000753 ** Get or set the value of the database 'auto-vacuum' parameter.
754 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000755 */
756#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000757 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000758 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000759 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000760 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000761 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000762 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000763 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000764 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000765 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000766 /* Call SetAutoVacuum() to set initialize the internal auto and
767 ** incr-vacuum flags. This is required in case this connection
768 ** creates the database file. It is important that it is created
769 ** as an auto-vacuum capable db.
770 */
771 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
772 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
773 /* When setting the auto_vacuum mode to either "full" or
774 ** "incremental", write the value of meta[6] in the database
775 ** file. Before writing to meta[6], check that meta[3] indicates
776 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000777 */
drhb06a4ec2014-03-10 18:03:09 +0000778 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000779 static const VdbeOpList setMeta6[] = {
780 { OP_Transaction, 0, 1, 0}, /* 0 */
781 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
782 { OP_If, 1, 0, 0}, /* 2 */
783 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
drh1861afc2016-02-01 21:48:34 +0000784 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
drhf63936e2013-10-03 14:08:07 +0000785 };
drh2ce18652016-01-16 20:50:21 +0000786 VdbeOp *aOp;
787 int iAddr = sqlite3VdbeCurrentAddr(v);
drhdad300d2016-01-18 00:20:26 +0000788 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
drh2ce18652016-01-16 20:50:21 +0000789 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhdad300d2016-01-18 00:20:26 +0000790 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000791 aOp[0].p1 = iDb;
792 aOp[1].p1 = iDb;
793 aOp[2].p2 = iAddr+4;
drh1861afc2016-02-01 21:48:34 +0000794 aOp[4].p1 = iDb;
795 aOp[4].p3 = eAuto - 1;
drhf63936e2013-10-03 14:08:07 +0000796 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000797 }
danielk1977951af802004-11-05 15:45:09 +0000798 }
drh9ccd8652013-09-13 16:36:46 +0000799 break;
800 }
danielk1977951af802004-11-05 15:45:09 +0000801#endif
802
drhca5557f2007-05-04 18:30:40 +0000803 /*
drh9b0cf342015-11-12 14:57:19 +0000804 ** PRAGMA [schema.]incremental_vacuum(N)
drhca5557f2007-05-04 18:30:40 +0000805 **
806 ** Do N steps of incremental vacuuming on a database.
807 */
808#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000809 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000810 int iLimit, addr;
811 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
812 iLimit = 0x7fffffff;
813 }
814 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000815 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000816 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000817 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000818 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000819 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000820 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000821 break;
822 }
drhca5557f2007-05-04 18:30:40 +0000823#endif
824
drh13d70422004-11-13 15:59:14 +0000825#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000826 /*
drh9b0cf342015-11-12 14:57:19 +0000827 ** PRAGMA [schema.]cache_size
828 ** PRAGMA [schema.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000829 **
830 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000831 ** page cache size. The second form sets the local
832 ** page cache size value. If N is positive then that is the
833 ** number of pages in the cache. If N is negative, then the
834 ** number of pages is adjusted so that the cache uses -N kibibytes
835 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000836 */
drh9ccd8652013-09-13 16:36:46 +0000837 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000838 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000839 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000840 returnSingleInt(v, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000841 }else{
drh3b42abb2011-11-09 14:23:04 +0000842 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000843 pDb->pSchema->cache_size = size;
844 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000845 }
drh9ccd8652013-09-13 16:36:46 +0000846 break;
847 }
drhc11d4f92003-04-06 21:08:24 +0000848
849 /*
drh9b0cf342015-11-12 14:57:19 +0000850 ** PRAGMA [schema.]cache_spill
851 ** PRAGMA cache_spill=BOOLEAN
852 ** PRAGMA [schema.]cache_spill=N
853 **
854 ** The first form reports the current local setting for the
855 ** page cache spill size. The second form turns cache spill on
856 ** or off. When turnning cache spill on, the size is set to the
857 ** current cache_size. The third form sets a spill size that
858 ** may be different form the cache size.
859 ** If N is positive then that is the
860 ** number of pages in the cache. If N is negative, then the
861 ** number of pages is adjusted so that the cache uses -N kibibytes
862 ** of memory.
863 **
864 ** If the number of cache_spill pages is less then the number of
865 ** cache_size pages, no spilling occurs until the page count exceeds
866 ** the number of cache_size pages.
867 **
868 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
869 ** not just the schema specified.
870 */
871 case PragTyp_CACHE_SPILL: {
drh9b0cf342015-11-12 14:57:19 +0000872 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
873 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000874 returnSingleInt(v,
drh9b0cf342015-11-12 14:57:19 +0000875 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
876 sqlite3BtreeSetSpillSize(pDb->pBt,0));
877 }else{
drh4f9c8ec2015-11-12 15:47:48 +0000878 int size = 1;
drh9b0cf342015-11-12 14:57:19 +0000879 if( sqlite3GetInt32(zRight, &size) ){
880 sqlite3BtreeSetSpillSize(pDb->pBt, size);
881 }
drh4f9c8ec2015-11-12 15:47:48 +0000882 if( sqlite3GetBoolean(zRight, size!=0) ){
drh9b0cf342015-11-12 14:57:19 +0000883 db->flags |= SQLITE_CacheSpill;
884 }else{
drhd5b44d62018-12-06 17:06:02 +0000885 db->flags &= ~(u64)SQLITE_CacheSpill;
drh9b0cf342015-11-12 14:57:19 +0000886 }
drh4f9c8ec2015-11-12 15:47:48 +0000887 setAllPagerFlags(db);
drh9b0cf342015-11-12 14:57:19 +0000888 }
889 break;
890 }
891
892 /*
893 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000894 **
drh0d0614b2013-03-25 23:09:28 +0000895 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000896 ** used to limit the aggregate size of all memory mapped regions of the
897 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000898 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000899 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000900 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000901 **
drh0d0614b2013-03-25 23:09:28 +0000902 ** This value is advisory. The underlying VFS is free to memory map
903 ** as little or as much as it wants. Except, if N is set to 0 then the
904 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000905 */
drh9ccd8652013-09-13 16:36:46 +0000906 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000907 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000908#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000909 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000910 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000911 int ii;
drh9296c182014-07-23 13:40:49 +0000912 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000913 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
914 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000915 for(ii=db->nDb-1; ii>=0; ii--){
916 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000917 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000918 }
919 }
dan5d8a1372013-03-19 19:28:06 +0000920 }
drh9b4c59f2013-04-15 17:03:42 +0000921 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000922 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000923#else
dan3719f5f2013-05-23 10:13:18 +0000924 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000925 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000926#endif
dan3719f5f2013-05-23 10:13:18 +0000927 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000928 returnSingleInt(v, sz);
dan3719f5f2013-05-23 10:13:18 +0000929 }else if( rc!=SQLITE_NOTFOUND ){
930 pParse->nErr++;
931 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000932 }
drh9ccd8652013-09-13 16:36:46 +0000933 break;
934 }
dan5d8a1372013-03-19 19:28:06 +0000935
936 /*
drh90f5ecb2004-07-22 01:19:35 +0000937 ** PRAGMA temp_store
938 ** PRAGMA temp_store = "default"|"memory"|"file"
939 **
940 ** Return or set the local value of the temp_store flag. Changing
941 ** the local value does not make changes to the disk file and the default
942 ** value will be restored the next time the database is opened.
943 **
944 ** Note that it is possible for the library compile-time options to
945 ** override this setting
946 */
drh9ccd8652013-09-13 16:36:46 +0000947 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000948 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000949 returnSingleInt(v, db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000950 }else{
951 changeTempStorage(pParse, zRight);
952 }
drh9ccd8652013-09-13 16:36:46 +0000953 break;
954 }
drh90f5ecb2004-07-22 01:19:35 +0000955
956 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000957 ** PRAGMA temp_store_directory
958 ** PRAGMA temp_store_directory = ""|"directory_name"
959 **
960 ** Return or set the local value of the temp_store_directory flag. Changing
961 ** the value sets a specific directory to be used for temporary files.
962 ** Setting to a null string reverts to the default temporary directory search.
963 ** If temporary directory is changed, then invalidateTempStorage.
964 **
965 */
drh9ccd8652013-09-13 16:36:46 +0000966 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000967 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000968 returnSingleText(v, sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000969 }else{
drh78f82d12008-09-02 00:52:52 +0000970#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000971 if( zRight[0] ){
972 int res;
danielk1977fab11272008-09-16 14:38:02 +0000973 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
974 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000975 sqlite3ErrorMsg(pParse, "not a writable directory");
976 goto pragma_out;
977 }
drh268283b2005-01-08 15:44:25 +0000978 }
danielk1977b06a0b62008-06-26 10:54:12 +0000979 if( SQLITE_TEMP_STORE==0
980 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
981 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000982 ){
983 invalidateTempStorage(pParse);
984 }
drh17435752007-08-16 04:30:38 +0000985 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000986 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000987 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000988 }else{
drh268283b2005-01-08 15:44:25 +0000989 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000990 }
drh78f82d12008-09-02 00:52:52 +0000991#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000992 }
drh9ccd8652013-09-13 16:36:46 +0000993 break;
994 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000995
drhcc716452012-06-06 23:23:23 +0000996#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000997 /*
998 ** PRAGMA data_store_directory
999 ** PRAGMA data_store_directory = ""|"directory_name"
1000 **
1001 ** Return or set the local value of the data_store_directory flag. Changing
1002 ** the value sets a specific directory to be used for database files that
1003 ** were specified with a relative pathname. Setting to a null string reverts
1004 ** to the default database directory, which for database files specified with
1005 ** a relative path will probably be based on the current directory for the
1006 ** process. Database file specified with an absolute path are not impacted
1007 ** by this setting, regardless of its value.
1008 **
1009 */
drh9ccd8652013-09-13 16:36:46 +00001010 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +00001011 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +00001012 returnSingleText(v, sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +00001013 }else{
1014#ifndef SQLITE_OMIT_WSD
1015 if( zRight[0] ){
1016 int res;
1017 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1018 if( rc!=SQLITE_OK || res==0 ){
1019 sqlite3ErrorMsg(pParse, "not a writable directory");
1020 goto pragma_out;
1021 }
1022 }
1023 sqlite3_free(sqlite3_data_directory);
1024 if( zRight[0] ){
1025 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1026 }else{
1027 sqlite3_data_directory = 0;
1028 }
1029#endif /* SQLITE_OMIT_WSD */
1030 }
drh9ccd8652013-09-13 16:36:46 +00001031 break;
1032 }
drhcc716452012-06-06 23:23:23 +00001033#endif
mistachkina112d142012-03-14 00:44:01 +00001034
drhd2cb50b2009-01-09 21:41:17 +00001035#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +00001036 /*
drh9b0cf342015-11-12 14:57:19 +00001037 ** PRAGMA [schema.]lock_proxy_file
1038 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +00001039 **
1040 ** Return or set the value of the lock_proxy_file flag. Changing
1041 ** the value sets a specific file to be used for database access locks.
1042 **
1043 */
1044 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +00001045 if( !zRight ){
1046 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1047 char *proxy_file_path = NULL;
1048 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +00001049 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +00001050 &proxy_file_path);
drhc232aca2016-12-15 16:01:17 +00001051 returnSingleText(v, proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +00001052 }else{
1053 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1054 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1055 int res;
1056 if( zRight[0] ){
1057 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1058 zRight);
1059 } else {
1060 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1061 NULL);
1062 }
1063 if( res!=SQLITE_OK ){
1064 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1065 goto pragma_out;
1066 }
1067 }
drh9ccd8652013-09-13 16:36:46 +00001068 break;
1069 }
drhd2cb50b2009-01-09 21:41:17 +00001070#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +00001071
1072 /*
drh9b0cf342015-11-12 14:57:19 +00001073 ** PRAGMA [schema.]synchronous
drh6841b1c2016-02-03 19:20:15 +00001074 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
drhc11d4f92003-04-06 21:08:24 +00001075 **
1076 ** Return or set the local value of the synchronous flag. Changing
1077 ** the local value does not make changes to the disk file and the
1078 ** default value will be restored the next time the database is
1079 ** opened.
1080 */
drh9ccd8652013-09-13 16:36:46 +00001081 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +00001082 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +00001083 returnSingleInt(v, pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +00001084 }else{
danielk197791cf71b2004-06-26 06:37:06 +00001085 if( !db->autoCommit ){
1086 sqlite3ErrorMsg(pParse,
1087 "Safety level may not be changed inside a transaction");
drh7553c822017-03-15 14:04:03 +00001088 }else if( iDb!=1 ){
drhd99d2832015-04-17 15:58:33 +00001089 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
1090 if( iLevel==0 ) iLevel = 1;
1091 pDb->safety_level = iLevel;
drh50a1a5a2016-03-08 14:40:11 +00001092 pDb->bSyncSet = 1;
drhd3605a42013-08-17 15:42:29 +00001093 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001094 }
drhc11d4f92003-04-06 21:08:24 +00001095 }
drh9ccd8652013-09-13 16:36:46 +00001096 break;
1097 }
drh13d70422004-11-13 15:59:14 +00001098#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001099
drhbf216272005-02-26 18:10:44 +00001100#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001101 case PragTyp_FLAG: {
1102 if( zRight==0 ){
drhc232aca2016-12-15 16:01:17 +00001103 setPragmaResultColumnNames(v, pPragma);
1104 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001105 }else{
drhfd748c62018-10-30 16:25:35 +00001106 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001107 if( db->autoCommit==0 ){
1108 /* Foreign key support may not be enabled or disabled while not
1109 ** in auto-commit mode. */
1110 mask &= ~(SQLITE_ForeignKeys);
1111 }
drhd4530972014-09-09 14:47:53 +00001112#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001113 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001114 /* Do not allow non-admin users to modify the schema arbitrarily */
1115 mask &= ~(SQLITE_WriteSchema);
1116 }
1117#endif
drh9ccd8652013-09-13 16:36:46 +00001118
1119 if( sqlite3GetBoolean(zRight, 0) ){
drh2eeca202020-01-08 20:37:45 +00001120 db->flags |= mask;
drh9ccd8652013-09-13 16:36:46 +00001121 }else{
1122 db->flags &= ~mask;
1123 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
drh635e6a92021-10-08 16:39:33 +00001124 if( (mask & SQLITE_WriteSchema)!=0
1125 && sqlite3_stricmp(zRight, "reset")==0
1126 ){
drhbc98f902021-10-14 17:30:32 +00001127 /* IMP: R-60817-01178 If the argument is "RESET" then schema
1128 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
1129 ** in addition, the schema is reloaded. */
drh635e6a92021-10-08 16:39:33 +00001130 sqlite3ResetAllSchemasOfConnection(db);
1131 }
drh9ccd8652013-09-13 16:36:46 +00001132 }
1133
1134 /* Many of the flag-pragmas modify the code generated by the SQL
1135 ** compiler (eg. count_changes). So add an opcode to expire all
1136 ** compiled SQL statements after modifying a pragma value.
1137 */
drh01c736d2016-05-20 15:15:07 +00001138 sqlite3VdbeAddOp0(v, OP_Expire);
drh9ccd8652013-09-13 16:36:46 +00001139 setAllPagerFlags(db);
1140 }
1141 break;
1142 }
drhbf216272005-02-26 18:10:44 +00001143#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001144
drh13d70422004-11-13 15:59:14 +00001145#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001146 /*
1147 ** PRAGMA table_info(<table>)
1148 **
1149 ** Return a single row for each column of the named table. The columns of
1150 ** the returned data set are:
1151 **
1152 ** cid: Column id (numbered from left to right, starting at 0)
1153 ** name: Column name
1154 ** type: Column declaration type.
1155 ** notnull: True if 'NOT NULL' is part of column declaration
1156 ** dflt_value: The default value for the column, if any.
dan3e6ac162016-02-11 21:01:16 +00001157 ** pk: Non-zero for PK fields.
danielk197791cf71b2004-06-26 06:37:06 +00001158 */
drh9ccd8652013-09-13 16:36:46 +00001159 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001160 Table *pTab;
drha78d2c02020-07-04 20:29:56 +00001161 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drh4d249e62016-06-10 22:49:01 +00001162 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001163 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001164 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001165 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001166 Column *pCol;
drh44156282013-10-23 22:23:03 +00001167 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drhd7dc0a32018-10-01 18:28:42 +00001168 pParse->nMem = 7;
danielk19774adee202004-05-08 08:23:19 +00001169 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001170 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
drhab3c5f22019-10-17 13:15:40 +00001171 int isHidden = 0;
drhf9751072021-10-07 13:40:29 +00001172 const Expr *pColExpr;
drhab3c5f22019-10-17 13:15:40 +00001173 if( pCol->colFlags & COLFLAG_NOINSERT ){
drh676fa252019-10-17 14:21:07 +00001174 if( pPragma->iArg==0 ){
1175 nHidden++;
1176 continue;
1177 }
drhab3c5f22019-10-17 13:15:40 +00001178 if( pCol->colFlags & COLFLAG_VIRTUAL ){
1179 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
drhc1431142019-10-17 17:54:05 +00001180 }else if( pCol->colFlags & COLFLAG_STORED ){
drhab3c5f22019-10-17 13:15:40 +00001181 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
drhc1431142019-10-17 17:54:05 +00001182 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
drhab3c5f22019-10-17 13:15:40 +00001183 isHidden = 1; /* HIDDEN */
1184 }
danielk1977034ca142007-06-26 10:38:54 +00001185 }
drh384b7fe2013-01-01 13:55:31 +00001186 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1187 k = 0;
1188 }else if( pPk==0 ){
1189 k = 1;
1190 }else{
drh1b678962015-04-15 07:19:27 +00001191 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001192 }
drhf9751072021-10-07 13:40:29 +00001193 pColExpr = sqlite3ColumnExpr(pTab,pCol);
1194 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
drh9d43db52021-10-07 14:19:32 +00001195 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)
1196 || isHidden>=2 );
drhd7dc0a32018-10-01 18:28:42 +00001197 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
drh076e85f2015-09-03 13:46:12 +00001198 i-nHidden,
drhcf9d36d2021-08-02 18:03:43 +00001199 pCol->zCnName,
drhd7564862016-03-22 20:05:09 +00001200 sqlite3ColumnType(pCol,""),
drh076e85f2015-09-03 13:46:12 +00001201 pCol->notNull ? 1 : 0,
drhf9751072021-10-07 13:40:29 +00001202 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
drhd7dc0a32018-10-01 18:28:42 +00001203 k,
1204 isHidden);
drhc11d4f92003-04-06 21:08:24 +00001205 }
1206 }
drh9ccd8652013-09-13 16:36:46 +00001207 }
1208 break;
drhc11d4f92003-04-06 21:08:24 +00001209
drh2e50f672021-09-21 17:26:23 +00001210 /*
1211 ** PRAGMA table_list
1212 **
1213 ** Return a single row for each table, virtual table, or view in the
1214 ** entire schema.
1215 **
1216 ** schema: Name of attached database hold this table
1217 ** name: Name of the table itself
1218 ** type: "table", "view", "virtual", "shadow"
1219 ** ncol: Number of columns
1220 ** wr: True for a WITHOUT ROWID table
1221 ** strict: True for a STRICT table
1222 */
1223 case PragTyp_TABLE_LIST: {
1224 int ii;
1225 pParse->nMem = 6;
1226 sqlite3CodeVerifyNamedSchema(pParse, zDb);
1227 for(ii=0; ii<db->nDb; ii++){
1228 HashElem *k;
1229 Hash *pHash;
drhdc88b402021-10-21 19:48:14 +00001230 int initNCol;
drh2e50f672021-09-21 17:26:23 +00001231 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
drhdc88b402021-10-21 19:48:14 +00001232
1233 /* Ensure that the Table.nCol field is initialized for all views
1234 ** and virtual tables. Each time we initialize a Table.nCol value
1235 ** for a table, that can potentially disrupt the hash table, so restart
1236 ** the initialization scan.
1237 */
drh2e50f672021-09-21 17:26:23 +00001238 pHash = &db->aDb[ii].pSchema->tblHash;
drhdc88b402021-10-21 19:48:14 +00001239 initNCol = sqliteHashCount(pHash);
1240 while( initNCol-- ){
1241 for(k=sqliteHashFirst(pHash); 1; k=sqliteHashNext(k) ){
1242 Table *pTab;
1243 if( k==0 ){ initNCol = 0; break; }
1244 pTab = sqliteHashData(k);
1245 if( pTab->nCol==0 ){
1246 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
1247 if( zSql ){
1248 sqlite3_stmt *pDummy = 0;
1249 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
1250 (void)sqlite3_finalize(pDummy);
1251 sqlite3DbFree(db, zSql);
1252 }
1253 pHash = &db->aDb[ii].pSchema->tblHash;
1254 break;
1255 }
1256 }
1257 }
1258
drh2e50f672021-09-21 17:26:23 +00001259 for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k) ){
1260 Table *pTab = sqliteHashData(k);
1261 const char *zType;
1262 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
1263 if( IsView(pTab) ){
1264 zType = "view";
1265 }else if( IsVirtual(pTab) ){
1266 zType = "virtual";
1267 }else if( pTab->tabFlags & TF_Shadow ){
1268 zType = "shadow";
1269 }else{
1270 zType = "table";
1271 }
1272 sqlite3VdbeMultiLoad(v, 1, "sssiii",
1273 db->aDb[ii].zDbSName,
drha4a871c2021-11-04 14:04:20 +00001274 sqlite3PreferredTableName(pTab->zName),
drh2e50f672021-09-21 17:26:23 +00001275 zType,
1276 pTab->nCol,
1277 (pTab->tabFlags & TF_WithoutRowid)!=0,
1278 (pTab->tabFlags & TF_Strict)!=0
1279 );
1280 }
1281 }
1282 }
1283 break;
1284
drh33bec3f2017-02-17 13:38:15 +00001285#ifdef SQLITE_DEBUG
drh3ef26152013-10-12 20:22:00 +00001286 case PragTyp_STATS: {
1287 Index *pIdx;
1288 HashElem *i;
drh33bec3f2017-02-17 13:38:15 +00001289 pParse->nMem = 5;
drh3ef26152013-10-12 20:22:00 +00001290 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001291 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1292 Table *pTab = sqliteHashData(i);
drh33bec3f2017-02-17 13:38:15 +00001293 sqlite3VdbeMultiLoad(v, 1, "ssiii",
drha4a871c2021-11-04 14:04:20 +00001294 sqlite3PreferredTableName(pTab->zName),
drh076e85f2015-09-03 13:46:12 +00001295 0,
drhd566c952016-02-25 21:19:03 +00001296 pTab->szTabRow,
drh33bec3f2017-02-17 13:38:15 +00001297 pTab->nRowLogEst,
1298 pTab->tabFlags);
drh3ef26152013-10-12 20:22:00 +00001299 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh40cf27c2017-07-07 16:00:53 +00001300 sqlite3VdbeMultiLoad(v, 2, "siiiX",
drh076e85f2015-09-03 13:46:12 +00001301 pIdx->zName,
drhd566c952016-02-25 21:19:03 +00001302 pIdx->szIdxRow,
drh33bec3f2017-02-17 13:38:15 +00001303 pIdx->aiRowLogEst[0],
1304 pIdx->hasStat1);
1305 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drh3ef26152013-10-12 20:22:00 +00001306 }
1307 }
1308 }
1309 break;
drh33bec3f2017-02-17 13:38:15 +00001310#endif
drh3ef26152013-10-12 20:22:00 +00001311
drh9ccd8652013-09-13 16:36:46 +00001312 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001313 Index *pIdx;
1314 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001315 pIdx = sqlite3FindIndex(db, zRight, zDb);
drh7b1904e2019-07-17 11:01:11 +00001316 if( pIdx==0 ){
1317 /* If there is no index named zRight, check to see if there is a
1318 ** WITHOUT ROWID table named zRight, and if there is, show the
1319 ** structure of the PRIMARY KEY index for that table. */
1320 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
1321 if( pTab && !HasRowid(pTab) ){
1322 pIdx = sqlite3PrimaryKeyIndex(pTab);
1323 }
1324 }
drhc11d4f92003-04-06 21:08:24 +00001325 if( pIdx ){
dan3c425482018-11-20 18:09:59 +00001326 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
drhc11d4f92003-04-06 21:08:24 +00001327 int i;
drh5e7028c2015-03-05 14:29:02 +00001328 int mx;
1329 if( pPragma->iArg ){
1330 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1331 mx = pIdx->nColumn;
1332 pParse->nMem = 6;
1333 }else{
1334 /* PRAGMA index_info (legacy version) */
1335 mx = pIdx->nKeyCol;
1336 pParse->nMem = 3;
1337 }
drhc11d4f92003-04-06 21:08:24 +00001338 pTab = pIdx->pTable;
dan3c425482018-11-20 18:09:59 +00001339 sqlite3CodeVerifySchema(pParse, iIdxDb);
drhc232aca2016-12-15 16:01:17 +00001340 assert( pParse->nMem<=pPragma->nPragCName );
drhc228be52015-01-31 02:00:01 +00001341 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001342 i16 cnum = pIdx->aiColumn[i];
drh40cf27c2017-07-07 16:00:53 +00001343 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
drhcf9d36d2021-08-02 18:03:43 +00001344 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
drh5e7028c2015-03-05 14:29:02 +00001345 if( pPragma->iArg ){
drh40cf27c2017-07-07 16:00:53 +00001346 sqlite3VdbeMultiLoad(v, 4, "isiX",
drh076e85f2015-09-03 13:46:12 +00001347 pIdx->aSortOrder[i],
1348 pIdx->azColl[i],
1349 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001350 }
1351 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001352 }
1353 }
drh9ccd8652013-09-13 16:36:46 +00001354 }
1355 break;
drhc11d4f92003-04-06 21:08:24 +00001356
drh9ccd8652013-09-13 16:36:46 +00001357 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001358 Index *pIdx;
1359 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001360 int i;
drh2783e4b2004-10-05 15:42:53 +00001361 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001362 if( pTab ){
dan3c425482018-11-20 18:09:59 +00001363 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhc228be52015-01-31 02:00:01 +00001364 pParse->nMem = 5;
dan3c425482018-11-20 18:09:59 +00001365 sqlite3CodeVerifySchema(pParse, iTabDb);
drh3ef26152013-10-12 20:22:00 +00001366 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001367 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001368 sqlite3VdbeMultiLoad(v, 1, "isisi",
1369 i,
1370 pIdx->zName,
1371 IsUniqueIndex(pIdx),
1372 azOrigin[pIdx->idxType],
1373 pIdx->pPartIdxWhere!=0);
drhc11d4f92003-04-06 21:08:24 +00001374 }
1375 }
drh9ccd8652013-09-13 16:36:46 +00001376 }
1377 break;
drhc11d4f92003-04-06 21:08:24 +00001378
drh9ccd8652013-09-13 16:36:46 +00001379 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001380 int i;
drh2d401ab2008-01-10 23:50:11 +00001381 pParse->nMem = 3;
drh13d70422004-11-13 15:59:14 +00001382 for(i=0; i<db->nDb; i++){
1383 if( db->aDb[i].pBt==0 ) continue;
drh69c33822016-08-18 14:33:11 +00001384 assert( db->aDb[i].zDbSName!=0 );
drh076e85f2015-09-03 13:46:12 +00001385 sqlite3VdbeMultiLoad(v, 1, "iss",
1386 i,
drh69c33822016-08-18 14:33:11 +00001387 db->aDb[i].zDbSName,
drh076e85f2015-09-03 13:46:12 +00001388 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh13d70422004-11-13 15:59:14 +00001389 }
drh9ccd8652013-09-13 16:36:46 +00001390 }
1391 break;
danielk197748af65a2005-02-09 03:20:37 +00001392
drh9ccd8652013-09-13 16:36:46 +00001393 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001394 int i = 0;
1395 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001396 pParse->nMem = 2;
danielk197748af65a2005-02-09 03:20:37 +00001397 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1398 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001399 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
danielk197748af65a2005-02-09 03:20:37 +00001400 }
drh9ccd8652013-09-13 16:36:46 +00001401 }
1402 break;
drhab53bb62017-07-07 15:43:22 +00001403
drhcc3f3d12019-08-17 15:27:58 +00001404#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
drhab53bb62017-07-07 15:43:22 +00001405 case PragTyp_FUNCTION_LIST: {
1406 int i;
1407 HashElem *j;
1408 FuncDef *p;
drh337ca512020-01-04 19:58:28 +00001409 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
drh79d5bc82020-01-04 01:43:02 +00001410 pParse->nMem = 6;
drhab53bb62017-07-07 15:43:22 +00001411 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
1412 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
drhf9751072021-10-07 13:40:29 +00001413 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
drh337ca512020-01-04 19:58:28 +00001414 pragmaFunclistLine(v, p, 1, showInternFunc);
drhab53bb62017-07-07 15:43:22 +00001415 }
1416 }
1417 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
1418 p = (FuncDef*)sqliteHashData(j);
drhf9751072021-10-07 13:40:29 +00001419 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
drh337ca512020-01-04 19:58:28 +00001420 pragmaFunclistLine(v, p, 0, showInternFunc);
drhab53bb62017-07-07 15:43:22 +00001421 }
1422 }
1423 break;
1424
1425#ifndef SQLITE_OMIT_VIRTUALTABLE
1426 case PragTyp_MODULE_LIST: {
1427 HashElem *j;
1428 pParse->nMem = 1;
1429 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
1430 Module *pMod = (Module*)sqliteHashData(j);
1431 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
drhab53bb62017-07-07 15:43:22 +00001432 }
1433 }
1434 break;
1435#endif /* SQLITE_OMIT_VIRTUALTABLE */
1436
drh8ae11aa2017-07-07 17:33:07 +00001437 case PragTyp_PRAGMA_LIST: {
1438 int i;
1439 for(i=0; i<ArraySize(aPragmaName); i++){
1440 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
drh8ae11aa2017-07-07 17:33:07 +00001441 }
1442 }
1443 break;
1444#endif /* SQLITE_INTROSPECTION_PRAGMAS */
drhab53bb62017-07-07 15:43:22 +00001445
drh13d70422004-11-13 15:59:14 +00001446#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1447
drhb7f91642004-10-31 02:22:47 +00001448#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001449 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001450 FKey *pFK;
1451 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001452 pTab = sqlite3FindTable(db, zRight, zDb);
drh78b2fa82021-10-07 12:11:20 +00001453 if( pTab && IsOrdinaryTable(pTab) ){
drhf38524d2021-08-02 16:41:57 +00001454 pFK = pTab->u.tab.pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001455 if( pFK ){
dan3c425482018-11-20 18:09:59 +00001456 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977742f9472004-06-16 12:02:43 +00001457 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001458 pParse->nMem = 8;
dan3c425482018-11-20 18:09:59 +00001459 sqlite3CodeVerifySchema(pParse, iTabDb);
danielk1977742f9472004-06-16 12:02:43 +00001460 while(pFK){
1461 int j;
1462 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001463 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1464 i,
1465 j,
1466 pFK->zTo,
drhcf9d36d2021-08-02 18:03:43 +00001467 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
drh076e85f2015-09-03 13:46:12 +00001468 pFK->aCol[j].zCol,
1469 actionName(pFK->aAction[1]), /* ON UPDATE */
1470 actionName(pFK->aAction[0]), /* ON DELETE */
1471 "NONE");
danielk1977742f9472004-06-16 12:02:43 +00001472 }
1473 ++i;
1474 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001475 }
drh78100cc2003-08-23 22:40:53 +00001476 }
1477 }
drh9ccd8652013-09-13 16:36:46 +00001478 }
1479 break;
drhb7f91642004-10-31 02:22:47 +00001480#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001481
drh6c5b9152012-12-17 16:46:37 +00001482#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001483#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001484 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001485 FKey *pFK; /* A foreign key constraint */
1486 Table *pTab; /* Child table contain "REFERENCES" keyword */
1487 Table *pParent; /* Parent table that child points to */
1488 Index *pIdx; /* Index in the parent table */
1489 int i; /* Loop counter: Foreign key number for pTab */
1490 int j; /* Loop counter: Field of the foreign key */
1491 HashElem *k; /* Loop counter: Next table in schema */
1492 int x; /* result variable */
1493 int regResult; /* 3 registers to hold a result row */
1494 int regKey; /* Register to hold key for checking the FK */
1495 int regRow; /* Registers to hold a row from pTab */
1496 int addrTop; /* Top of a loop checking foreign keys */
1497 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001498 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001499
drh613028b2012-12-17 18:43:02 +00001500 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001501 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001502 regKey = ++pParse->nMem;
1503 regRow = ++pParse->nMem;
drh613028b2012-12-17 18:43:02 +00001504 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1505 while( k ){
1506 if( zRight ){
1507 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1508 k = 0;
1509 }else{
1510 pTab = (Table*)sqliteHashData(k);
1511 k = sqliteHashNext(k);
1512 }
drh78b2fa82021-10-07 12:11:20 +00001513 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
drhec1650a2020-07-03 12:15:59 +00001514 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1515 zDb = db->aDb[iDb].zDbSName;
1516 sqlite3CodeVerifySchema(pParse, iDb);
1517 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001518 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drhec1650a2020-07-03 12:15:59 +00001519 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001520 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh78b2fa82021-10-07 12:11:20 +00001521 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:57 +00001522 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001523 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1524 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001525 pIdx = 0;
drhec1650a2020-07-03 12:15:59 +00001526 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001527 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001528 if( x==0 ){
1529 if( pIdx==0 ){
drhec1650a2020-07-03 12:15:59 +00001530 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
drh6c5b9152012-12-17 16:46:37 +00001531 }else{
drhec1650a2020-07-03 12:15:59 +00001532 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001533 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001534 }
1535 }else{
drh613028b2012-12-17 18:43:02 +00001536 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001537 break;
1538 }
drh6c5b9152012-12-17 16:46:37 +00001539 }
dan5e878302013-10-12 19:06:48 +00001540 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001541 if( pFK ) break;
1542 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001543 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh78b2fa82021-10-07 12:11:20 +00001544 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:57 +00001545 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001546 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001547 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001548 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001549 if( pParent ){
1550 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
drhd96e3822020-09-16 19:48:23 +00001551 assert( x==0 || db->mallocFailed );
dan5e878302013-10-12 19:06:48 +00001552 }
drhec4ccdb2018-12-29 02:26:59 +00001553 addrOk = sqlite3VdbeMakeLabel(pParse);
dan4bee5592017-04-17 18:42:33 +00001554
1555 /* Generate code to read the child key values into registers
1556 ** regRow..regRow+n. If any of the child key values are NULL, this
1557 ** row cannot cause an FK violation. Jump directly to addrOk in
1558 ** this case. */
drh4f16ff92021-07-07 16:48:24 +00001559 if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
dan4bee5592017-04-17 18:42:33 +00001560 for(j=0; j<pFK->nCol; j++){
1561 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
1562 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
1563 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001564 }
dan4bee5592017-04-17 18:42:33 +00001565
1566 /* Generate code to query the parent index for a matching parent
1567 ** key. If a match is found, jump to addrOk. */
1568 if( pIdx ){
1569 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
1570 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
1571 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
1572 VdbeCoverage(v);
1573 }else if( pParent ){
1574 int jmp = sqlite3VdbeCurrentAddr(v)+2;
1575 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
1576 sqlite3VdbeGoto(v, addrOk);
drhd96e3822020-09-16 19:48:23 +00001577 assert( pFK->nCol==1 || db->mallocFailed );
dan4bee5592017-04-17 18:42:33 +00001578 }
1579
1580 /* Generate code to report an FK violation to the caller. */
dan940464b2017-04-17 18:02:41 +00001581 if( HasRowid(pTab) ){
1582 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
1583 }else{
1584 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
1585 }
drh40cf27c2017-07-07 16:00:53 +00001586 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001587 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001588 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001589 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001590 }
drh688852a2014-02-17 22:40:43 +00001591 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001592 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001593 }
drh9ccd8652013-09-13 16:36:46 +00001594 }
1595 break;
dan09ff9e12013-03-11 11:49:03 +00001596#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001597#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1598
drh08652b52019-05-08 17:27:18 +00001599#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
drh55ef4d92005-08-14 01:20:37 +00001600 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1601 ** used will be case sensitive or not depending on the RHS.
1602 */
drh9ccd8652013-09-13 16:36:46 +00001603 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001604 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001605 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001606 }
drh9ccd8652013-09-13 16:36:46 +00001607 }
1608 break;
drh08652b52019-05-08 17:27:18 +00001609#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
drh55ef4d92005-08-14 01:20:37 +00001610
drh1dcdbc02007-01-27 02:24:54 +00001611#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1612# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1613#endif
1614
drhb7f91642004-10-31 02:22:47 +00001615#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh8b174f22017-02-22 15:11:36 +00001616 /* PRAGMA integrity_check
1617 ** PRAGMA integrity_check(N)
1618 ** PRAGMA quick_check
1619 ** PRAGMA quick_check(N)
1620 **
1621 ** Verify the integrity of the database.
1622 **
1623 ** The "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001624 ** integrity_check designed to detect most database corruption
drh8b174f22017-02-22 15:11:36 +00001625 ** without the overhead of cross-checking indexes. Quick_check
1626 ** is linear time wherease integrity_check is O(NlogN).
drh17d2d592020-07-23 00:45:06 +00001627 **
1628 ** The maximum nubmer of errors is 100 by default. A different default
1629 ** can be specified using a numeric parameter N.
1630 **
1631 ** Or, the parameter N can be the name of a table. In that case, only
1632 ** the one table named is verified. The freelist is only verified if
1633 ** the named table is "sqlite_schema" (or one of its aliases).
1634 **
1635 ** All schemas are checked by default. To check just a single
1636 ** schema, use the form:
1637 **
1638 ** PRAGMA schema.integrity_check;
danielk197741c58b72007-12-29 13:39:19 +00001639 */
drh9ccd8652013-09-13 16:36:46 +00001640 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001641 int i, j, addr, mxErr;
drh17d2d592020-07-23 00:45:06 +00001642 Table *pObjTab = 0; /* Check only this one table, if not NULL */
drhed717fe2003-06-15 23:42:24 +00001643
drhc5227312011-10-13 17:09:01 +00001644 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001645
dan5885e762012-07-16 10:06:12 +00001646 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1647 ** then iDb is set to the index of the database identified by <db>.
1648 ** In this case, the integrity of database iDb only is verified by
1649 ** the VDBE created below.
1650 **
1651 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1652 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1653 ** to -1 here, to indicate that the VDBE should verify the integrity
1654 ** of all attached databases. */
1655 assert( iDb>=0 );
1656 assert( iDb==0 || pId2->z );
1657 if( pId2->z==0 ) iDb = -1;
1658
drhed717fe2003-06-15 23:42:24 +00001659 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001660 pParse->nMem = 6;
drh1dcdbc02007-01-27 02:24:54 +00001661
1662 /* Set the maximum error count */
1663 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1664 if( zRight ){
drh17d2d592020-07-23 00:45:06 +00001665 if( sqlite3GetInt32(zRight, &mxErr) ){
1666 if( mxErr<=0 ){
1667 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1668 }
1669 }else{
1670 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
1671 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
drh1dcdbc02007-01-27 02:24:54 +00001672 }
1673 }
drh66accfc2017-02-22 18:04:42 +00001674 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001675
1676 /* Do an integrity check on each database file */
1677 for(i=0; i<db->nDb; i++){
drh9ecd7082017-09-10 01:06:05 +00001678 HashElem *x; /* For looping over tables in the schema */
1679 Hash *pTbls; /* Set of all tables in the schema */
1680 int *aRoot; /* Array of root page numbers of all btrees */
1681 int cnt = 0; /* Number of entries in aRoot[] */
1682 int mxIdx = 0; /* Maximum number of indexes for any table */
drhed717fe2003-06-15 23:42:24 +00001683
danielk197753c0f742005-03-29 03:10:59 +00001684 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001685 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001686
drh80242052004-06-09 00:48:12 +00001687 sqlite3CodeVerifySchema(pParse, i);
1688
drhed717fe2003-06-15 23:42:24 +00001689 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001690 **
drh98968b22016-03-15 22:00:39 +00001691 ** Begin by finding the root pages numbers
drh2d401ab2008-01-10 23:50:11 +00001692 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001693 */
dan5885e762012-07-16 10:06:12 +00001694 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001695 pTbls = &db->aDb[i].pSchema->tblHash;
drh98968b22016-03-15 22:00:39 +00001696 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh9ecd7082017-09-10 01:06:05 +00001697 Table *pTab = sqliteHashData(x); /* Current table */
1698 Index *pIdx; /* An index on pTab */
1699 int nIdx; /* Number of indexes on pTab */
drh17d2d592020-07-23 00:45:06 +00001700 if( pObjTab && pObjTab!=pTab ) continue;
drh98968b22016-03-15 22:00:39 +00001701 if( HasRowid(pTab) ) cnt++;
drhbb9b5f22016-03-19 00:35:02 +00001702 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
1703 if( nIdx>mxIdx ) mxIdx = nIdx;
drh98968b22016-03-15 22:00:39 +00001704 }
drh17d2d592020-07-23 00:45:06 +00001705 if( cnt==0 ) continue;
1706 if( pObjTab ) cnt++;
drh98968b22016-03-15 22:00:39 +00001707 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
1708 if( aRoot==0 ) break;
drh17d2d592020-07-23 00:45:06 +00001709 cnt = 0;
1710 if( pObjTab ) aRoot[++cnt] = 0;
1711 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh98968b22016-03-15 22:00:39 +00001712 Table *pTab = sqliteHashData(x);
1713 Index *pIdx;
drh17d2d592020-07-23 00:45:06 +00001714 if( pObjTab && pObjTab!=pTab ) continue;
drhb5c10632017-09-21 00:49:15 +00001715 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
drh79069752004-05-22 21:30:40 +00001716 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhb5c10632017-09-21 00:49:15 +00001717 aRoot[++cnt] = pIdx->tnum;
drh79069752004-05-22 21:30:40 +00001718 }
1719 }
drhb5c10632017-09-21 00:49:15 +00001720 aRoot[0] = cnt;
drh2d401ab2008-01-10 23:50:11 +00001721
1722 /* Make sure sufficient number of registers have been allocated */
drhbb9b5f22016-03-19 00:35:02 +00001723 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
drh3963e582017-07-15 20:33:19 +00001724 sqlite3ClearTempRegCache(pParse);
drh2d401ab2008-01-10 23:50:11 +00001725
1726 /* Do the b-tree integrity checks */
drh98968b22016-03-15 22:00:39 +00001727 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
drh4f21c4a2008-12-10 22:15:00 +00001728 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001729 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001730 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh69c33822016-08-18 14:33:11 +00001731 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
drh66a51672008-01-03 00:01:23 +00001732 P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:05 +00001733 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
1734 integrityCheckResultRow(v);
drh1dcdbc02007-01-27 02:24:54 +00001735 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001736
1737 /* Make sure all the indices are constructed correctly.
1738 */
drh8b174f22017-02-22 15:11:36 +00001739 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001740 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001741 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001742 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001743 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001744 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001745 int r1 = -1;
drh9e1209d2021-08-19 02:58:15 +00001746 int bStrict;
drhed717fe2003-06-15 23:42:24 +00001747
drh99666212021-10-11 15:21:42 +00001748 if( !IsOrdinaryTable(pTab) ) continue;
drh17d2d592020-07-23 00:45:06 +00001749 if( pObjTab && pObjTab!=pTab ) continue;
drh26198bb2013-10-31 11:15:09 +00001750 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
danfd261ec2015-10-22 20:54:33 +00001751 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001752 1, 0, &iDataCur, &iIdxCur);
drh9ecd7082017-09-10 01:06:05 +00001753 /* reg[7] counts the number of entries in the table.
1754 ** reg[8+i] counts the number of entries in the i-th index
1755 */
drh6fbe41a2013-10-30 20:22:55 +00001756 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001757 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001758 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001759 }
drhbb9b5f22016-03-19 00:35:02 +00001760 assert( pParse->nMem>=8+j );
1761 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
drh688852a2014-02-17 22:40:43 +00001762 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001763 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drh4011c442018-06-06 19:48:19 +00001764 if( !isQuick ){
1765 /* Sanity check on record header decoding */
drh0b0b3a92019-10-17 18:35:57 +00001766 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
drh4011c442018-06-06 19:48:19 +00001767 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
drh71c770f2021-08-19 16:29:33 +00001768 VdbeComment((v, "(right-most column)"));
drh4011c442018-06-06 19:48:19 +00001769 }
drh9e1209d2021-08-19 02:58:15 +00001770 /* Verify that all NOT NULL columns really are NOT NULL. At the
1771 ** same time verify the type of the content of STRICT tables */
1772 bStrict = (pTab->tabFlags & TF_Strict)!=0;
drhcefc87f2014-08-01 01:40:33 +00001773 for(j=0; j<pTab->nCol; j++){
1774 char *zErr;
drh9e1209d2021-08-19 02:58:15 +00001775 Column *pCol = pTab->aCol + j;
drh71c770f2021-08-19 16:29:33 +00001776 int doError, jmp2;
drhcefc87f2014-08-01 01:40:33 +00001777 if( j==pTab->iPKey ) continue;
drh9e1209d2021-08-19 02:58:15 +00001778 if( pCol->notNull==0 && !bStrict ) continue;
drh71c770f2021-08-19 16:29:33 +00001779 doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0;
drhcefc87f2014-08-01 01:40:33 +00001780 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
drhebd70ee2019-12-09 15:52:07 +00001781 if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){
1782 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1783 }
drh9e1209d2021-08-19 02:58:15 +00001784 if( pCol->notNull ){
drh9e1209d2021-08-19 02:58:15 +00001785 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1786 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1787 pCol->zCnName);
1788 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh5c929042021-11-03 11:43:09 +00001789 if( bStrict && pCol->eCType!=COLTYPE_ANY ){
drh71c770f2021-08-19 16:29:33 +00001790 sqlite3VdbeGoto(v, doError);
1791 }else{
1792 integrityCheckResultRow(v);
1793 }
drh9e1209d2021-08-19 02:58:15 +00001794 sqlite3VdbeJumpHere(v, jmp2);
1795 }
drh94b70d82021-09-08 19:25:21 +00001796 if( (pTab->tabFlags & TF_Strict)!=0
1797 && pCol->eCType!=COLTYPE_ANY
1798 ){
drh71c770f2021-08-19 16:29:33 +00001799 jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0,
drh9e1209d2021-08-19 02:58:15 +00001800 sqlite3StdTypeMap[pCol->eCType-1]);
1801 VdbeCoverage(v);
1802 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
1803 sqlite3StdType[pCol->eCType-1],
1804 pTab->zName, pTab->aCol[j].zCnName);
1805 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh71c770f2021-08-19 16:29:33 +00001806 sqlite3VdbeResolveLabel(v, doError);
drh9e1209d2021-08-19 02:58:15 +00001807 integrityCheckResultRow(v);
drh71c770f2021-08-19 16:29:33 +00001808 sqlite3VdbeJumpHere(v, jmp2);
drh9e1209d2021-08-19 02:58:15 +00001809 }
drhcefc87f2014-08-01 01:40:33 +00001810 }
drh8a284dc2017-02-22 14:15:37 +00001811 /* Verify CHECK constraints */
1812 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
dan75f95582017-04-04 19:58:54 +00001813 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
1814 if( db->mallocFailed==0 ){
drhec4ccdb2018-12-29 02:26:59 +00001815 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
1816 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
dan75f95582017-04-04 19:58:54 +00001817 char *zErr;
1818 int k;
drh6e97f8e2017-07-20 13:17:08 +00001819 pParse->iSelfTab = iDataCur + 1;
dan75f95582017-04-04 19:58:54 +00001820 for(k=pCheck->nExpr-1; k>0; k--){
1821 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
1822 }
1823 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
1824 SQLITE_JUMPIFNULL);
1825 sqlite3VdbeResolveLabel(v, addrCkFault);
drh3e34eab2017-07-19 19:48:40 +00001826 pParse->iSelfTab = 0;
dan75f95582017-04-04 19:58:54 +00001827 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
1828 pTab->zName);
1829 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:05 +00001830 integrityCheckResultRow(v);
dan75f95582017-04-04 19:58:54 +00001831 sqlite3VdbeResolveLabel(v, addrCkOk);
drh8a284dc2017-02-22 14:15:37 +00001832 }
dan75f95582017-04-04 19:58:54 +00001833 sqlite3ExprListDelete(db, pCheck);
drhcefc87f2014-08-01 01:40:33 +00001834 }
drh226cef42017-09-09 20:38:49 +00001835 if( !isQuick ){ /* Omit the remaining tests for quick_check */
drh226cef42017-09-09 20:38:49 +00001836 /* Validate index entries for the current row */
1837 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1838 int jmp2, jmp3, jmp4, jmp5;
drhec4ccdb2018-12-29 02:26:59 +00001839 int ckUniq = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:49 +00001840 if( pPk==pIdx ) continue;
1841 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1842 pPrior, r1);
1843 pPrior = pIdx;
1844 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
1845 /* Verify that an index entry exists for the current table row */
1846 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
1847 pIdx->nColumn); VdbeCoverage(v);
1848 sqlite3VdbeLoadString(v, 3, "row ");
1849 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1850 sqlite3VdbeLoadString(v, 4, " missing from index ");
1851 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1852 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
1853 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh9ecd7082017-09-10 01:06:05 +00001854 jmp4 = integrityCheckResultRow(v);
drh226cef42017-09-09 20:38:49 +00001855 sqlite3VdbeJumpHere(v, jmp2);
1856 /* For UNIQUE indexes, verify that only one entry exists with the
1857 ** current key. The entry is unique if (1) any column is NULL
1858 ** or (2) the next entry has a different key */
1859 if( IsUniqueIndex(pIdx) ){
drhec4ccdb2018-12-29 02:26:59 +00001860 int uniqOk = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:49 +00001861 int jmp6;
1862 int kk;
1863 for(kk=0; kk<pIdx->nKeyCol; kk++){
1864 int iCol = pIdx->aiColumn[kk];
1865 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
1866 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
1867 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1868 VdbeCoverage(v);
1869 }
1870 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
1871 sqlite3VdbeGoto(v, uniqOk);
1872 sqlite3VdbeJumpHere(v, jmp6);
1873 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1874 pIdx->nKeyCol); VdbeCoverage(v);
1875 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1876 sqlite3VdbeGoto(v, jmp5);
1877 sqlite3VdbeResolveLabel(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00001878 }
drh226cef42017-09-09 20:38:49 +00001879 sqlite3VdbeJumpHere(v, jmp4);
1880 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhcefc87f2014-08-01 01:40:33 +00001881 }
drhed717fe2003-06-15 23:42:24 +00001882 }
drh688852a2014-02-17 22:40:43 +00001883 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001884 sqlite3VdbeJumpHere(v, loopTop-1);
drh8b174f22017-02-22 15:11:36 +00001885 if( !isQuick ){
1886 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
1887 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1888 if( pPk==pIdx ) continue;
drh8b174f22017-02-22 15:11:36 +00001889 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh66accfc2017-02-22 18:04:42 +00001890 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
drh8b174f22017-02-22 15:11:36 +00001891 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh9ecd7082017-09-10 01:06:05 +00001892 sqlite3VdbeLoadString(v, 4, pIdx->zName);
1893 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
1894 integrityCheckResultRow(v);
drh66accfc2017-02-22 18:04:42 +00001895 sqlite3VdbeJumpHere(v, addr);
drh8b174f22017-02-22 15:11:36 +00001896 }
drhed717fe2003-06-15 23:42:24 +00001897 }
1898 }
1899 }
drh2ce18652016-01-16 20:50:21 +00001900 {
1901 static const int iLn = VDBE_OFFSET_LINENO(2);
1902 static const VdbeOpList endCode[] = {
1903 { OP_AddImm, 1, 0, 0}, /* 0 */
drh66accfc2017-02-22 18:04:42 +00001904 { OP_IfNotZero, 1, 4, 0}, /* 1 */
drh2ce18652016-01-16 20:50:21 +00001905 { OP_String8, 0, 3, 0}, /* 2 */
drh1b325542016-02-03 01:55:44 +00001906 { OP_ResultRow, 3, 1, 0}, /* 3 */
drh74588ce2017-09-13 00:13:05 +00001907 { OP_Halt, 0, 0, 0}, /* 4 */
1908 { OP_String8, 0, 3, 0}, /* 5 */
1909 { OP_Goto, 0, 3, 0}, /* 6 */
drh2ce18652016-01-16 20:50:21 +00001910 };
1911 VdbeOp *aOp;
1912
1913 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
1914 if( aOp ){
drh66accfc2017-02-22 18:04:42 +00001915 aOp[0].p2 = 1-mxErr;
drh2ce18652016-01-16 20:50:21 +00001916 aOp[2].p4type = P4_STATIC;
1917 aOp[2].p4.z = "ok";
drh74588ce2017-09-13 00:13:05 +00001918 aOp[5].p4type = P4_STATIC;
1919 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
drh2ce18652016-01-16 20:50:21 +00001920 }
drh74588ce2017-09-13 00:13:05 +00001921 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
drh2ce18652016-01-16 20:50:21 +00001922 }
drh9ccd8652013-09-13 16:36:46 +00001923 }
1924 break;
drhb7f91642004-10-31 02:22:47 +00001925#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1926
drh13d70422004-11-13 15:59:14 +00001927#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001928 /*
1929 ** PRAGMA encoding
1930 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1931 **
drh85b623f2007-12-13 21:54:09 +00001932 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001933 ** database. If the database is not initialized, it is initialized now.
1934 **
1935 ** The second form of this pragma is a no-op if the main database file
1936 ** has not already been initialized. In this case it sets the default
1937 ** encoding that will be used for the main database file if a new file
1938 ** is created. If an existing main database file is opened, then the
1939 ** default text encoding for the existing database is used.
1940 **
1941 ** In all cases new databases created using the ATTACH command are
1942 ** created to use the same default text encoding as the main database. If
1943 ** the main database has not been initialized and/or created when ATTACH
1944 ** is executed, this is done before the ATTACH operation.
1945 **
1946 ** In the second form this pragma sets the text encoding to be used in
1947 ** new database files created using this database handle. It is only
1948 ** useful if invoked immediately after the main database i
1949 */
drh9ccd8652013-09-13 16:36:46 +00001950 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001951 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001952 char *zName;
1953 u8 enc;
1954 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001955 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001956 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1957 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1958 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001959 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001960 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001961 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1962 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001963 { 0, 0 }
1964 };
drh0f7eb612006-08-08 13:51:43 +00001965 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001966 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001967 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00001968 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1969 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1970 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drhc232aca2016-12-15 16:01:17 +00001971 returnSingleText(v, encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00001972 }else{ /* "PRAGMA encoding = XXX" */
1973 /* Only change the value of sqlite.enc if the database handle is not
1974 ** initialized. If the main database exists, the new sqlite.enc value
1975 ** will be overwritten when the schema is next loaded. If it does not
1976 ** already exists, it will be created to use the new encoding value.
1977 */
dan0ea2d422020-03-05 18:04:09 +00001978 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
danielk19778e227872004-06-07 07:52:17 +00001979 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1980 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh42a630b2020-03-05 16:13:24 +00001981 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
1982 SCHEMA_ENC(db) = enc;
1983 sqlite3SetTextEncoding(db, enc);
danielk19778e227872004-06-07 07:52:17 +00001984 break;
1985 }
1986 }
1987 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001988 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001989 }
1990 }
1991 }
drh9ccd8652013-09-13 16:36:46 +00001992 }
1993 break;
drh13d70422004-11-13 15:59:14 +00001994#endif /* SQLITE_OMIT_UTF16 */
1995
1996#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001997 /*
drh9b0cf342015-11-12 14:57:19 +00001998 ** PRAGMA [schema.]schema_version
1999 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002000 **
drh9b0cf342015-11-12 14:57:19 +00002001 ** PRAGMA [schema.]user_version
2002 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002003 **
drhe459bd42016-03-16 20:05:57 +00002004 ** PRAGMA [schema.]freelist_count
2005 **
2006 ** PRAGMA [schema.]data_version
drh4ee09b42013-05-01 19:49:27 +00002007 **
drh9b0cf342015-11-12 14:57:19 +00002008 ** PRAGMA [schema.]application_id
2009 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00002010 **
danielk1977b92b70b2004-11-12 16:11:59 +00002011 ** The pragma's schema_version and user_version are used to set or get
2012 ** the value of the schema-version and user-version, respectively. Both
2013 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00002014 ** stored in the database header.
2015 **
2016 ** The schema-cookie is usually only manipulated internally by SQLite. It
2017 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00002018 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00002019 ** SQLite each time a query is executed to ensure that the internal cache
2020 ** of the schema used when compiling the SQL query matches the schema of
2021 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00002022 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2023 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00002024 ** crashes or database corruption. Use with caution!
2025 **
danielk1977b92b70b2004-11-12 16:11:59 +00002026 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00002027 ** applications for any purpose.
2028 */
drh9ccd8652013-09-13 16:36:46 +00002029 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00002030 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00002031 sqlite3VdbeUsesBtree(v, iDb);
drhc232aca2016-12-15 16:01:17 +00002032 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00002033 /* Write the specified cookie value */
2034 static const VdbeOpList setCookie[] = {
2035 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1861afc2016-02-01 21:48:34 +00002036 { OP_SetCookie, 0, 0, 0}, /* 1 */
danielk1977dae24952004-11-11 05:10:43 +00002037 };
drh2ce18652016-01-16 20:50:21 +00002038 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00002039 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
drh2ce18652016-01-16 20:50:21 +00002040 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
drhdad300d2016-01-18 00:20:26 +00002041 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00002042 aOp[0].p1 = iDb;
drh1861afc2016-02-01 21:48:34 +00002043 aOp[1].p1 = iDb;
2044 aOp[1].p2 = iCookie;
2045 aOp[1].p3 = sqlite3Atoi(zRight);
drhe3863b52020-07-01 16:19:14 +00002046 aOp[1].p5 = 1;
danielk1977dae24952004-11-11 05:10:43 +00002047 }else{
2048 /* Read the specified cookie value */
2049 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00002050 { OP_Transaction, 0, 0, 0}, /* 0 */
2051 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00002052 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00002053 };
drh2ce18652016-01-16 20:50:21 +00002054 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00002055 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
drh2ce18652016-01-16 20:50:21 +00002056 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
drhdad300d2016-01-18 00:20:26 +00002057 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00002058 aOp[0].p1 = iDb;
2059 aOp[1].p1 = iDb;
2060 aOp[1].p3 = iCookie;
drhf71a3662016-03-16 20:44:45 +00002061 sqlite3VdbeReusable(v);
danielk1977dae24952004-11-11 05:10:43 +00002062 }
drh9ccd8652013-09-13 16:36:46 +00002063 }
2064 break;
drh13d70422004-11-13 15:59:14 +00002065#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00002066
shanehdc97a8c2010-02-23 20:08:35 +00002067#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2068 /*
2069 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00002070 **
drh71caabf2010-02-26 15:39:24 +00002071 ** Return the names of all compile-time options used in this build,
2072 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00002073 */
drh9ccd8652013-09-13 16:36:46 +00002074 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00002075 int i = 0;
2076 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00002077 pParse->nMem = 1;
shanehdc97a8c2010-02-23 20:08:35 +00002078 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00002079 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00002080 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2081 }
drhf71a3662016-03-16 20:44:45 +00002082 sqlite3VdbeReusable(v);
drh9ccd8652013-09-13 16:36:46 +00002083 }
2084 break;
shanehdc97a8c2010-02-23 20:08:35 +00002085#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2086
dan5cf53532010-05-01 16:40:20 +00002087#ifndef SQLITE_OMIT_WAL
2088 /*
drh9b0cf342015-11-12 14:57:19 +00002089 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00002090 **
2091 ** Checkpoint the database.
2092 */
drh9ccd8652013-09-13 16:36:46 +00002093 case PragTyp_WAL_CHECKPOINT: {
drh099b3852021-03-10 16:35:37 +00002094 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
dancdc1f042010-11-18 12:11:05 +00002095 int eMode = SQLITE_CHECKPOINT_PASSIVE;
2096 if( zRight ){
2097 if( sqlite3StrICmp(zRight, "full")==0 ){
2098 eMode = SQLITE_CHECKPOINT_FULL;
2099 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2100 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00002101 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
2102 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00002103 }
2104 }
dancdc1f042010-11-18 12:11:05 +00002105 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00002106 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00002107 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00002108 }
2109 break;
dan5a299f92010-05-03 11:05:08 +00002110
2111 /*
2112 ** PRAGMA wal_autocheckpoint
2113 ** PRAGMA wal_autocheckpoint = N
2114 **
2115 ** Configure a database connection to automatically checkpoint a database
2116 ** after accumulating N frames in the log. Or query for the current value
2117 ** of N.
2118 */
drh9ccd8652013-09-13 16:36:46 +00002119 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00002120 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00002121 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00002122 }
drhc232aca2016-12-15 16:01:17 +00002123 returnSingleInt(v,
drhb033d8b2010-05-03 13:37:30 +00002124 db->xWalCallback==sqlite3WalDefaultHook ?
2125 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00002126 }
2127 break;
dan5cf53532010-05-01 16:40:20 +00002128#endif
dan7c246102010-04-12 19:00:29 +00002129
drh09419b42011-11-16 19:29:17 +00002130 /*
2131 ** PRAGMA shrink_memory
2132 **
drh51a74d42015-02-28 01:04:27 +00002133 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
2134 ** connection on which it is invoked to free up as much memory as it
2135 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00002136 */
drh9ccd8652013-09-13 16:36:46 +00002137 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00002138 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00002139 break;
2140 }
drh09419b42011-11-16 19:29:17 +00002141
drhf3603962012-09-07 16:46:59 +00002142 /*
drh2ead47c2017-02-22 20:24:10 +00002143 ** PRAGMA optimize
drh1cfaf8e2017-03-02 14:17:21 +00002144 ** PRAGMA optimize(MASK)
drh2ead47c2017-02-22 20:24:10 +00002145 ** PRAGMA schema.optimize
drh1cfaf8e2017-03-02 14:17:21 +00002146 ** PRAGMA schema.optimize(MASK)
drh99d5b4c2017-02-18 22:52:40 +00002147 **
drh2ead47c2017-02-22 20:24:10 +00002148 ** Attempt to optimize the database. All schemas are optimized in the first
drh1cfaf8e2017-03-02 14:17:21 +00002149 ** two forms, and only the specified schema is optimized in the latter two.
drh2ead47c2017-02-22 20:24:10 +00002150 **
drhe1f1c082017-03-06 20:44:13 +00002151 ** The details of optimizations performed by this pragma are expected
drh2ead47c2017-02-22 20:24:10 +00002152 ** to change and improve over time. Applications should anticipate that
2153 ** this pragma will perform new optimizations in future releases.
2154 **
drh1cfaf8e2017-03-02 14:17:21 +00002155 ** The optional argument is a bitmask of optimizations to perform:
drh2ead47c2017-02-22 20:24:10 +00002156 **
drh1cfaf8e2017-03-02 14:17:21 +00002157 ** 0x0001 Debugging mode. Do not actually perform any optimizations
2158 ** but instead return one line of text for each optimization
2159 ** that would have been done. Off by default.
drh99d5b4c2017-02-18 22:52:40 +00002160 **
drh1cfaf8e2017-03-02 14:17:21 +00002161 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
2162 ** See below for additional information.
2163 **
2164 ** 0x0004 (Not yet implemented) Record usage and performance
2165 ** information from the current session in the
2166 ** database file so that it will be available to "optimize"
2167 ** pragmas run by future database connections.
2168 **
2169 ** 0x0008 (Not yet implemented) Create indexes that might have
2170 ** been helpful to recent queries
2171 **
drhe7c6f972017-07-15 20:25:22 +00002172 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
2173 ** of the optimizations listed above except Debug Mode, including new
drh59dbe3a2017-03-06 23:51:16 +00002174 ** optimizations that have not yet been invented. If new optimizations are
2175 ** ever added that should be off by default, those off-by-default
2176 ** optimizations will have bitmasks of 0x10000 or larger.
drh1cfaf8e2017-03-02 14:17:21 +00002177 **
2178 ** DETERMINATION OF WHEN TO RUN ANALYZE
2179 **
2180 ** In the current implementation, a table is analyzed if only if all of
drh2ead47c2017-02-22 20:24:10 +00002181 ** the following are true:
drh99d5b4c2017-02-18 22:52:40 +00002182 **
drh1cfaf8e2017-03-02 14:17:21 +00002183 ** (1) MASK bit 0x02 is set.
2184 **
2185 ** (2) The query planner used sqlite_stat1-style statistics for one or
drh99d5b4c2017-02-18 22:52:40 +00002186 ** more indexes of the table at some point during the lifetime of
2187 ** the current connection.
2188 **
drh1cfaf8e2017-03-02 14:17:21 +00002189 ** (3) One or more indexes of the table are currently unanalyzed OR
drh99d5b4c2017-02-18 22:52:40 +00002190 ** the number of rows in the table has increased by 25 times or more
2191 ** since the last time ANALYZE was run.
drh2ead47c2017-02-22 20:24:10 +00002192 **
2193 ** The rules for when tables are analyzed are likely to change in
2194 ** future releases.
drh72052a72017-02-17 16:26:34 +00002195 */
drh2ead47c2017-02-22 20:24:10 +00002196 case PragTyp_OPTIMIZE: {
drh99d5b4c2017-02-18 22:52:40 +00002197 int iDbLast; /* Loop termination point for the schema loop */
2198 int iTabCur; /* Cursor for a table whose size needs checking */
2199 HashElem *k; /* Loop over tables of a schema */
2200 Schema *pSchema; /* The current schema */
2201 Table *pTab; /* A table in the schema */
2202 Index *pIdx; /* An index of the table */
2203 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
2204 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
drh1cfaf8e2017-03-02 14:17:21 +00002205 u32 opMask; /* Mask of operations to perform */
drh4a54bb52017-02-18 15:58:52 +00002206
drh1cfaf8e2017-03-02 14:17:21 +00002207 if( zRight ){
2208 opMask = (u32)sqlite3Atoi(zRight);
2209 if( (opMask & 0x02)==0 ) break;
2210 }else{
drh59dbe3a2017-03-06 23:51:16 +00002211 opMask = 0xfffe;
drh1cfaf8e2017-03-02 14:17:21 +00002212 }
drh4a54bb52017-02-18 15:58:52 +00002213 iTabCur = pParse->nTab++;
2214 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
2215 if( iDb==1 ) continue;
2216 sqlite3CodeVerifySchema(pParse, iDb);
2217 pSchema = db->aDb[iDb].pSchema;
2218 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
2219 pTab = (Table*)sqliteHashData(k);
drh99d5b4c2017-02-18 22:52:40 +00002220
2221 /* If table pTab has not been used in a way that would benefit from
2222 ** having analysis statistics during the current session, then skip it.
2223 ** This also has the effect of skipping virtual tables and views */
drh4a54bb52017-02-18 15:58:52 +00002224 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
drh99d5b4c2017-02-18 22:52:40 +00002225
2226 /* Reanalyze if the table is 25 times larger than the last analysis */
drh4a54bb52017-02-18 15:58:52 +00002227 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
2228 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2229 if( !pIdx->hasStat1 ){
drh99d5b4c2017-02-18 22:52:40 +00002230 szThreshold = 0; /* Always analyze if any index lacks statistics */
drh4a54bb52017-02-18 15:58:52 +00002231 break;
2232 }
2233 }
2234 if( szThreshold ){
2235 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
2236 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
drh1cfaf8e2017-03-02 14:17:21 +00002237 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
drh4a54bb52017-02-18 15:58:52 +00002238 VdbeCoverage(v);
2239 }
2240 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
2241 db->aDb[iDb].zDbSName, pTab->zName);
drh1cfaf8e2017-03-02 14:17:21 +00002242 if( opMask & 0x01 ){
2243 int r1 = sqlite3GetTempReg(pParse);
2244 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
2245 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
2246 }else{
2247 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
2248 }
drh4a54bb52017-02-18 15:58:52 +00002249 }
2250 }
drhbce04142017-02-23 00:58:36 +00002251 sqlite3VdbeAddOp0(v, OP_Expire);
drh72052a72017-02-17 16:26:34 +00002252 break;
2253 }
2254
2255 /*
drhf3603962012-09-07 16:46:59 +00002256 ** PRAGMA busy_timeout
2257 ** PRAGMA busy_timeout = N
2258 **
2259 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00002260 ** if one is set. If no busy handler or a different busy handler is set
2261 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2262 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00002263 */
drhd49c3582013-09-13 19:00:06 +00002264 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00002265 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00002266 if( zRight ){
2267 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2268 }
drhc232aca2016-12-15 16:01:17 +00002269 returnSingleInt(v, db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00002270 break;
2271 }
drhf3603962012-09-07 16:46:59 +00002272
drh55e85ca2013-09-13 21:01:56 +00002273 /*
2274 ** PRAGMA soft_heap_limit
2275 ** PRAGMA soft_heap_limit = N
2276 **
drh51a74d42015-02-28 01:04:27 +00002277 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
2278 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
2279 ** specified and is a non-negative integer.
2280 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
2281 ** returns the same integer that would be returned by the
2282 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00002283 */
2284 case PragTyp_SOFT_HEAP_LIMIT: {
2285 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00002286 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00002287 sqlite3_soft_heap_limit64(N);
2288 }
drhc232aca2016-12-15 16:01:17 +00002289 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00002290 break;
2291 }
2292
drh03459612014-08-25 15:13:22 +00002293 /*
drh10c0e712019-04-25 18:15:38 +00002294 ** PRAGMA hard_heap_limit
2295 ** PRAGMA hard_heap_limit = N
2296 **
2297 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
2298 ** limit. The hard heap limit can be activated or lowered by this
2299 ** pragma, but not raised or deactivated. Only the
2300 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
2301 ** the hard heap limit. This allows an application to set a heap limit
2302 ** constraint that cannot be relaxed by an untrusted SQL script.
2303 */
2304 case PragTyp_HARD_HEAP_LIMIT: {
2305 sqlite3_int64 N;
2306 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
2307 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
2308 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
2309 }
drh31999c52019-11-14 17:46:32 +00002310 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
drh10c0e712019-04-25 18:15:38 +00002311 break;
2312 }
2313
2314 /*
drh03459612014-08-25 15:13:22 +00002315 ** PRAGMA threads
2316 ** PRAGMA threads = N
2317 **
2318 ** Configure the maximum number of worker threads. Return the new
2319 ** maximum, which might be less than requested.
2320 */
2321 case PragTyp_THREADS: {
2322 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00002323 if( zRight
drh03459612014-08-25 15:13:22 +00002324 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
2325 && N>=0
2326 ){
drh111544c2014-08-29 16:20:47 +00002327 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00002328 }
drhc232aca2016-12-15 16:01:17 +00002329 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00002330 break;
2331 }
2332
drh49a76a82020-03-31 20:57:06 +00002333 /*
2334 ** PRAGMA analysis_limit
2335 ** PRAGMA analysis_limit = N
2336 **
2337 ** Configure the maximum number of rows that ANALYZE will examine
2338 ** in each index that it looks at. Return the new limit.
2339 */
2340 case PragTyp_ANALYSIS_LIMIT: {
2341 sqlite3_int64 N;
2342 if( zRight
drhbc98f902021-10-14 17:30:32 +00002343 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
drh49a76a82020-03-31 20:57:06 +00002344 && N>=0
2345 ){
2346 db->nAnalysisLimit = (int)(N&0x7fffffff);
2347 }
drhbc98f902021-10-14 17:30:32 +00002348 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
drh49a76a82020-03-31 20:57:06 +00002349 break;
2350 }
2351
dougcurrie81c95ef2004-06-18 23:21:47 +00002352#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00002353 /*
2354 ** Report the current state of file logs for all databases
2355 */
drh9ccd8652013-09-13 16:36:46 +00002356 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00002357 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00002358 "unlocked", "shared", "reserved", "pending", "exclusive"
2359 };
2360 int i;
drh2d401ab2008-01-10 23:50:11 +00002361 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00002362 for(i=0; i<db->nDb; i++){
2363 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00002364 const char *zState = "unknown";
2365 int j;
drh69c33822016-08-18 14:33:11 +00002366 if( db->aDb[i].zDbSName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00002367 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00002368 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00002369 zState = "closed";
drh69c33822016-08-18 14:33:11 +00002370 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
drh9e33c2c2007-08-31 18:34:59 +00002371 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2372 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00002373 }
drh69c33822016-08-18 14:33:11 +00002374 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
drh89ac8c12004-06-09 14:17:20 +00002375 }
drh9ccd8652013-09-13 16:36:46 +00002376 break;
2377 }
drh89ac8c12004-06-09 14:17:20 +00002378#endif
2379
drhb48c0d52020-02-07 01:12:53 +00002380#if defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00002381 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
drh21e2cab2006-09-25 18:01:57 +00002382 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002383 sqlite3_activate_cerod(&zRight[6]);
2384 }
drh9ccd8652013-09-13 16:36:46 +00002385 }
2386 break;
drh21e2cab2006-09-25 18:01:57 +00002387#endif
drh3c4f2a42005-12-08 18:12:56 +00002388
drh9ccd8652013-09-13 16:36:46 +00002389 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00002390
dan9e1ab1a2017-01-05 19:32:48 +00002391 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
2392 ** purpose is to execute assert() statements to verify that if the
2393 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
2394 ** to the PRAGMA, the implementation has not added any OP_ResultRow
2395 ** instructions to the VM. */
2396 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
2397 sqlite3VdbeVerifyNoResultRow(v);
2398 }
2399
danielk1977e0048402004-06-15 16:51:01 +00002400pragma_out:
drh633e6d52008-07-28 19:34:53 +00002401 sqlite3DbFree(db, zLeft);
2402 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00002403}
drh2fcc1592016-12-15 20:59:03 +00002404#ifndef SQLITE_OMIT_VIRTUALTABLE
2405/*****************************************************************************
2406** Implementation of an eponymous virtual table that runs a pragma.
2407**
2408*/
2409typedef struct PragmaVtab PragmaVtab;
2410typedef struct PragmaVtabCursor PragmaVtabCursor;
2411struct PragmaVtab {
2412 sqlite3_vtab base; /* Base class. Must be first */
2413 sqlite3 *db; /* The database connection to which it belongs */
2414 const PragmaName *pName; /* Name of the pragma */
2415 u8 nHidden; /* Number of hidden columns */
2416 u8 iHidden; /* Index of the first hidden column */
2417};
2418struct PragmaVtabCursor {
2419 sqlite3_vtab_cursor base; /* Base class. Must be first */
2420 sqlite3_stmt *pPragma; /* The pragma statement to run */
2421 sqlite_int64 iRowid; /* Current rowid */
2422 char *azArg[2]; /* Value of the argument and schema */
2423};
2424
2425/*
2426** Pragma virtual table module xConnect method.
2427*/
2428static int pragmaVtabConnect(
2429 sqlite3 *db,
2430 void *pAux,
2431 int argc, const char *const*argv,
2432 sqlite3_vtab **ppVtab,
2433 char **pzErr
2434){
2435 const PragmaName *pPragma = (const PragmaName*)pAux;
2436 PragmaVtab *pTab = 0;
2437 int rc;
2438 int i, j;
2439 char cSep = '(';
2440 StrAccum acc;
2441 char zBuf[200];
2442
drh344a1bf2016-12-22 14:53:25 +00002443 UNUSED_PARAMETER(argc);
2444 UNUSED_PARAMETER(argv);
drh2fcc1592016-12-15 20:59:03 +00002445 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drh0cdbe1a2018-05-09 13:46:26 +00002446 sqlite3_str_appendall(&acc, "CREATE TABLE x");
drh2fcc1592016-12-15 20:59:03 +00002447 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
drh0cdbe1a2018-05-09 13:46:26 +00002448 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
drh2fcc1592016-12-15 20:59:03 +00002449 cSep = ',';
2450 }
drh9a63f092016-12-16 02:14:15 +00002451 if( i==0 ){
drh0cdbe1a2018-05-09 13:46:26 +00002452 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
drh9a63f092016-12-16 02:14:15 +00002453 i++;
2454 }
drh2fcc1592016-12-15 20:59:03 +00002455 j = 0;
2456 if( pPragma->mPragFlg & PragFlg_Result1 ){
drh0cdbe1a2018-05-09 13:46:26 +00002457 sqlite3_str_appendall(&acc, ",arg HIDDEN");
drh2fcc1592016-12-15 20:59:03 +00002458 j++;
2459 }
2460 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
drh0cdbe1a2018-05-09 13:46:26 +00002461 sqlite3_str_appendall(&acc, ",schema HIDDEN");
drh2fcc1592016-12-15 20:59:03 +00002462 j++;
2463 }
drh0cdbe1a2018-05-09 13:46:26 +00002464 sqlite3_str_append(&acc, ")", 1);
drh2fcc1592016-12-15 20:59:03 +00002465 sqlite3StrAccumFinish(&acc);
2466 assert( strlen(zBuf) < sizeof(zBuf)-1 );
2467 rc = sqlite3_declare_vtab(db, zBuf);
2468 if( rc==SQLITE_OK ){
2469 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
2470 if( pTab==0 ){
2471 rc = SQLITE_NOMEM;
2472 }else{
2473 memset(pTab, 0, sizeof(PragmaVtab));
2474 pTab->pName = pPragma;
2475 pTab->db = db;
2476 pTab->iHidden = i;
2477 pTab->nHidden = j;
2478 }
2479 }else{
2480 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2481 }
2482
2483 *ppVtab = (sqlite3_vtab*)pTab;
2484 return rc;
2485}
2486
2487/*
2488** Pragma virtual table module xDisconnect method.
2489*/
2490static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
2491 PragmaVtab *pTab = (PragmaVtab*)pVtab;
2492 sqlite3_free(pTab);
2493 return SQLITE_OK;
2494}
2495
2496/* Figure out the best index to use to search a pragma virtual table.
2497**
2498** There are not really any index choices. But we want to encourage the
2499** query planner to give == constraints on as many hidden parameters as
2500** possible, and especially on the first hidden parameter. So return a
2501** high cost if hidden parameters are unconstrained.
2502*/
2503static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2504 PragmaVtab *pTab = (PragmaVtab*)tab;
2505 const struct sqlite3_index_constraint *pConstraint;
2506 int i, j;
2507 int seen[2];
2508
drhae7045c2016-12-15 21:33:55 +00002509 pIdxInfo->estimatedCost = (double)1;
drh2fcc1592016-12-15 20:59:03 +00002510 if( pTab->nHidden==0 ){ return SQLITE_OK; }
2511 pConstraint = pIdxInfo->aConstraint;
2512 seen[0] = 0;
2513 seen[1] = 0;
2514 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2515 if( pConstraint->usable==0 ) continue;
2516 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2517 if( pConstraint->iColumn < pTab->iHidden ) continue;
2518 j = pConstraint->iColumn - pTab->iHidden;
2519 assert( j < 2 );
drhd7175eb2016-12-15 21:11:15 +00002520 seen[j] = i+1;
drh2fcc1592016-12-15 20:59:03 +00002521 }
2522 if( seen[0]==0 ){
2523 pIdxInfo->estimatedCost = (double)2147483647;
2524 pIdxInfo->estimatedRows = 2147483647;
2525 return SQLITE_OK;
2526 }
drhd7175eb2016-12-15 21:11:15 +00002527 j = seen[0]-1;
drh2fcc1592016-12-15 20:59:03 +00002528 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
2529 pIdxInfo->aConstraintUsage[j].omit = 1;
2530 if( seen[1]==0 ) return SQLITE_OK;
2531 pIdxInfo->estimatedCost = (double)20;
2532 pIdxInfo->estimatedRows = 20;
drhd7175eb2016-12-15 21:11:15 +00002533 j = seen[1]-1;
drh2fcc1592016-12-15 20:59:03 +00002534 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
2535 pIdxInfo->aConstraintUsage[j].omit = 1;
2536 return SQLITE_OK;
2537}
2538
2539/* Create a new cursor for the pragma virtual table */
2540static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
2541 PragmaVtabCursor *pCsr;
2542 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
2543 if( pCsr==0 ) return SQLITE_NOMEM;
2544 memset(pCsr, 0, sizeof(PragmaVtabCursor));
2545 pCsr->base.pVtab = pVtab;
2546 *ppCursor = &pCsr->base;
2547 return SQLITE_OK;
2548}
2549
2550/* Clear all content from pragma virtual table cursor. */
2551static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
2552 int i;
2553 sqlite3_finalize(pCsr->pPragma);
2554 pCsr->pPragma = 0;
2555 for(i=0; i<ArraySize(pCsr->azArg); i++){
2556 sqlite3_free(pCsr->azArg[i]);
2557 pCsr->azArg[i] = 0;
2558 }
2559}
2560
2561/* Close a pragma virtual table cursor */
2562static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
2563 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
2564 pragmaVtabCursorClear(pCsr);
drh9a63f092016-12-16 02:14:15 +00002565 sqlite3_free(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002566 return SQLITE_OK;
2567}
2568
2569/* Advance the pragma virtual table cursor to the next row */
2570static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
2571 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2572 int rc = SQLITE_OK;
2573
2574 /* Increment the xRowid value */
2575 pCsr->iRowid++;
drh9a63f092016-12-16 02:14:15 +00002576 assert( pCsr->pPragma );
2577 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
2578 rc = sqlite3_finalize(pCsr->pPragma);
2579 pCsr->pPragma = 0;
2580 pragmaVtabCursorClear(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002581 }
2582 return rc;
2583}
2584
2585/*
2586** Pragma virtual table module xFilter method.
2587*/
2588static int pragmaVtabFilter(
2589 sqlite3_vtab_cursor *pVtabCursor,
2590 int idxNum, const char *idxStr,
2591 int argc, sqlite3_value **argv
2592){
2593 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2594 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2595 int rc;
drhd8b72002016-12-16 04:20:27 +00002596 int i, j;
drh2fcc1592016-12-15 20:59:03 +00002597 StrAccum acc;
2598 char *zSql;
2599
drh344a1bf2016-12-22 14:53:25 +00002600 UNUSED_PARAMETER(idxNum);
2601 UNUSED_PARAMETER(idxStr);
drh2fcc1592016-12-15 20:59:03 +00002602 pragmaVtabCursorClear(pCsr);
drhd8b72002016-12-16 04:20:27 +00002603 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
2604 for(i=0; i<argc; i++, j++){
dand8ecefa2017-07-15 20:48:30 +00002605 const char *zText = (const char*)sqlite3_value_text(argv[i]);
drhd8b72002016-12-16 04:20:27 +00002606 assert( j<ArraySize(pCsr->azArg) );
dand8ecefa2017-07-15 20:48:30 +00002607 assert( pCsr->azArg[j]==0 );
2608 if( zText ){
2609 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
2610 if( pCsr->azArg[j]==0 ){
2611 return SQLITE_NOMEM;
2612 }
drh2fcc1592016-12-15 20:59:03 +00002613 }
2614 }
drhd7175eb2016-12-15 21:11:15 +00002615 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
drh0cdbe1a2018-05-09 13:46:26 +00002616 sqlite3_str_appendall(&acc, "PRAGMA ");
drh2fcc1592016-12-15 20:59:03 +00002617 if( pCsr->azArg[1] ){
drh0cdbe1a2018-05-09 13:46:26 +00002618 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
drh2fcc1592016-12-15 20:59:03 +00002619 }
drh0cdbe1a2018-05-09 13:46:26 +00002620 sqlite3_str_appendall(&acc, pTab->pName->zName);
drh2fcc1592016-12-15 20:59:03 +00002621 if( pCsr->azArg[0] ){
drh0cdbe1a2018-05-09 13:46:26 +00002622 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
drh2fcc1592016-12-15 20:59:03 +00002623 }
2624 zSql = sqlite3StrAccumFinish(&acc);
2625 if( zSql==0 ) return SQLITE_NOMEM;
2626 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
2627 sqlite3_free(zSql);
2628 if( rc!=SQLITE_OK ){
2629 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
2630 return rc;
2631 }
2632 return pragmaVtabNext(pVtabCursor);
2633}
2634
2635/*
2636** Pragma virtual table module xEof method.
2637*/
2638static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
2639 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2640 return (pCsr->pPragma==0);
2641}
2642
2643/* The xColumn method simply returns the corresponding column from
2644** the PRAGMA.
2645*/
2646static int pragmaVtabColumn(
2647 sqlite3_vtab_cursor *pVtabCursor,
2648 sqlite3_context *ctx,
2649 int i
2650){
2651 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2652 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2653 if( i<pTab->iHidden ){
2654 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
2655 }else{
2656 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
2657 }
2658 return SQLITE_OK;
2659}
2660
2661/*
2662** Pragma virtual table module xRowid method.
2663*/
2664static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
2665 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2666 *p = pCsr->iRowid;
2667 return SQLITE_OK;
2668}
2669
2670/* The pragma virtual table object */
2671static const sqlite3_module pragmaVtabModule = {
2672 0, /* iVersion */
2673 0, /* xCreate - create a table */
2674 pragmaVtabConnect, /* xConnect - connect to an existing table */
2675 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
2676 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
2677 0, /* xDestroy - Drop a table */
2678 pragmaVtabOpen, /* xOpen - open a cursor */
2679 pragmaVtabClose, /* xClose - close a cursor */
2680 pragmaVtabFilter, /* xFilter - configure scan constraints */
2681 pragmaVtabNext, /* xNext - advance a cursor */
2682 pragmaVtabEof, /* xEof */
2683 pragmaVtabColumn, /* xColumn - read data */
2684 pragmaVtabRowid, /* xRowid - read data */
2685 0, /* xUpdate - write data */
2686 0, /* xBegin - begin transaction */
2687 0, /* xSync - sync transaction */
2688 0, /* xCommit - commit transaction */
2689 0, /* xRollback - rollback transaction */
2690 0, /* xFindFunction - function overloading */
2691 0, /* xRename - rename the table */
2692 0, /* xSavepoint */
2693 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002694 0, /* xRollbackTo */
2695 0 /* xShadowName */
drh2fcc1592016-12-15 20:59:03 +00002696};
2697
2698/*
2699** Check to see if zTabName is really the name of a pragma. If it is,
2700** then register an eponymous virtual table for that pragma and return
2701** a pointer to the Module object for the new virtual table.
2702*/
2703Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
2704 const PragmaName *pName;
2705 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
2706 pName = pragmaLocate(zName+7);
2707 if( pName==0 ) return 0;
2708 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
2709 assert( sqlite3HashFind(&db->aModule, zName)==0 );
drhd7175eb2016-12-15 21:11:15 +00002710 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
drh2fcc1592016-12-15 20:59:03 +00002711}
2712
2713#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh13d70422004-11-13 15:59:14 +00002714
drh8bfdf722009-06-19 14:06:03 +00002715#endif /* SQLITE_OMIT_PRAGMA */