blob: 0e87524dd584dee72736aa0de00c26e7070abc12 [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){
drh9606a132022-02-25 01:23:17 +0000310 u32 mask =
311 SQLITE_DETERMINISTIC |
312 SQLITE_DIRECTONLY |
313 SQLITE_SUBTYPE |
314 SQLITE_INNOCUOUS |
315 SQLITE_FUNC_INTERNAL
316 ;
317 if( showInternFuncs ) mask = -1;
drh79d5bc82020-01-04 01:43:02 +0000318 for(; p; p=p->pNext){
319 const char *zType;
drhb84fda32020-01-09 16:28:50 +0000320 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
321
322 assert( SQLITE_FUNC_ENCMASK==0x3 );
323 assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 );
324 assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 );
325 assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 );
drh337ca512020-01-04 19:58:28 +0000326
drh79d5bc82020-01-04 01:43:02 +0000327 if( p->xSFunc==0 ) continue;
drh337ca512020-01-04 19:58:28 +0000328 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
329 && showInternFuncs==0
330 ){
331 continue;
332 }
drh79d5bc82020-01-04 01:43:02 +0000333 if( p->xValue!=0 ){
334 zType = "w";
335 }else if( p->xFinalize!=0 ){
336 zType = "a";
337 }else{
338 zType = "s";
339 }
drh79d5bc82020-01-04 01:43:02 +0000340 sqlite3VdbeMultiLoad(v, 1, "sissii",
341 p->zName, isBuiltin,
drhb84fda32020-01-09 16:28:50 +0000342 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
drh79d5bc82020-01-04 01:43:02 +0000343 p->nArg,
344 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
345 );
346 }
347}
348
349
350/*
drh66accfc2017-02-22 18:04:42 +0000351** Helper subroutine for PRAGMA integrity_check:
352**
drh9ecd7082017-09-10 01:06:05 +0000353** Generate code to output a single-column result row with a value of the
354** string held in register 3. Decrement the result count in register 1
355** and halt if the maximum number of result rows have been issued.
drh66accfc2017-02-22 18:04:42 +0000356*/
drh9ecd7082017-09-10 01:06:05 +0000357static int integrityCheckResultRow(Vdbe *v){
drh66accfc2017-02-22 18:04:42 +0000358 int addr;
drh9ecd7082017-09-10 01:06:05 +0000359 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh66accfc2017-02-22 18:04:42 +0000360 addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
361 VdbeCoverage(v);
drh9ecd7082017-09-10 01:06:05 +0000362 sqlite3VdbeAddOp0(v, OP_Halt);
drh66accfc2017-02-22 18:04:42 +0000363 return addr;
364}
365
366/*
drhc11d4f92003-04-06 21:08:24 +0000367** Process a pragma statement.
368**
369** Pragmas are of this form:
370**
drh9b0cf342015-11-12 14:57:19 +0000371** PRAGMA [schema.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000372**
373** The identifier might also be a string. The value is a string, and
374** identifier, or a number. If minusFlag is true, then the value is
375** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000376**
377** If the left side is "database.id" then pId1 is the database name
378** and pId2 is the id. If the left side is just "id" then pId1 is the
379** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000380*/
danielk197791cf71b2004-06-26 06:37:06 +0000381void sqlite3Pragma(
382 Parse *pParse,
drh9b0cf342015-11-12 14:57:19 +0000383 Token *pId1, /* First part of [schema.]id field */
384 Token *pId2, /* Second part of [schema.]id field, or NULL */
danielk197791cf71b2004-06-26 06:37:06 +0000385 Token *pValue, /* Token for <value>, or NULL */
386 int minusFlag /* True if a '-' sign preceded <value> */
387){
388 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
389 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
390 const char *zDb = 0; /* The database name */
391 Token *pId; /* Pointer to <id> token */
drh3fa97302012-02-22 16:58:36 +0000392 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000393 int iDb; /* Database index for <database> */
drh06fd5d62012-02-22 14:45:19 +0000394 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
395 sqlite3 *db = pParse->db; /* The database connection */
396 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000397 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drh2fcc1592016-12-15 20:59:03 +0000398 const PragmaName *pPragma; /* The pragma */
drh06fd5d62012-02-22 14:45:19 +0000399
drhc11d4f92003-04-06 21:08:24 +0000400 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000401 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000402 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000403
drh9b0cf342015-11-12 14:57:19 +0000404 /* Interpret the [schema.] part of the pragma statement. iDb is the
danielk197791cf71b2004-06-26 06:37:06 +0000405 ** index of the database this pragma is being applied to in db.aDb[]. */
406 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
407 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000408 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000409
danielk1977ddfb2f02006-02-17 12:25:14 +0000410 /* If the temp database has been explicitly named as part of the
411 ** pragma, make sure it is open.
412 */
413 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
414 return;
415 }
416
drh17435752007-08-16 04:30:38 +0000417 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000418 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000419 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000420 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000421 }else{
drh17435752007-08-16 04:30:38 +0000422 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000423 }
danielk197791cf71b2004-06-26 06:37:06 +0000424
drhd2cb50b2009-01-09 21:41:17 +0000425 assert( pId2 );
drh69c33822016-08-18 14:33:11 +0000426 zDb = pId2->n>0 ? pDb->zDbSName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000427 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000428 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000429 }
drh06fd5d62012-02-22 14:45:19 +0000430
431 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
432 ** connection. If it returns SQLITE_OK, then assume that the VFS
433 ** handled the pragma and generate a no-op prepared statement.
drh8dd7a6a2015-03-06 04:37:26 +0000434 **
435 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
436 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
437 ** object corresponding to the database file to which the pragma
438 ** statement refers.
439 **
440 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
441 ** file control is an array of pointers to strings (char**) in which the
442 ** second element of the array is the name of the pragma and the third
443 ** element is the argument to the pragma or NULL if the pragma has no
444 ** argument.
drh06fd5d62012-02-22 14:45:19 +0000445 */
drh3fa97302012-02-22 16:58:36 +0000446 aFcntl[0] = 0;
447 aFcntl[1] = zLeft;
448 aFcntl[2] = zRight;
449 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000450 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000451 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
452 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000453 sqlite3VdbeSetNumCols(v, 1);
454 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
455 returnSingleText(v, aFcntl[0]);
drh7cc023c2015-09-03 04:28:25 +0000456 sqlite3_free(aFcntl[0]);
drh9ccd8652013-09-13 16:36:46 +0000457 goto pragma_out;
458 }
459 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000460 if( aFcntl[0] ){
461 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
462 sqlite3_free(aFcntl[0]);
463 }
464 pParse->nErr++;
465 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000466 goto pragma_out;
467 }
468
469 /* Locate the pragma in the lookup table */
drh2fcc1592016-12-15 20:59:03 +0000470 pPragma = pragmaLocate(zLeft);
drhbc98f902021-10-14 17:30:32 +0000471 if( pPragma==0 ){
472 /* IMP: R-43042-22504 No error messages are generated if an
473 ** unknown pragma is issued. */
474 goto pragma_out;
475 }
drh9ccd8652013-09-13 16:36:46 +0000476
drhf63936e2013-10-03 14:08:07 +0000477 /* Make sure the database schema is loaded if the pragma requires that */
drhc232aca2016-12-15 16:01:17 +0000478 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
drhf63936e2013-10-03 14:08:07 +0000479 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
480 }
481
drhc232aca2016-12-15 16:01:17 +0000482 /* Register the result column names for pragmas that return results */
dan9e1ab1a2017-01-05 19:32:48 +0000483 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
484 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
485 ){
drhc232aca2016-12-15 16:01:17 +0000486 setPragmaResultColumnNames(v, pPragma);
487 }
488
drh9ccd8652013-09-13 16:36:46 +0000489 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01 +0000490 switch( pPragma->ePragTyp ){
drh9ccd8652013-09-13 16:36:46 +0000491
drhe73c9142011-11-09 16:12:24 +0000492#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000493 /*
drh9b0cf342015-11-12 14:57:19 +0000494 ** PRAGMA [schema.]default_cache_size
495 ** PRAGMA [schema.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000496 **
497 ** The first form reports the current persistent setting for the
498 ** page cache size. The value returned is the maximum number of
499 ** pages in the page cache. The second form sets both the current
500 ** page cache size value and the persistent page cache size value
501 ** stored in the database file.
502 **
drh93791ea2010-04-26 17:36:35 +0000503 ** Older versions of SQLite would set the default cache size to a
504 ** negative number to indicate synchronous=OFF. These days, synchronous
505 ** is always on by default regardless of the sign of the default cache
506 ** size. But continue to take the absolute value of the default cache
507 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000508 */
drh9ccd8652013-09-13 16:36:46 +0000509 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09 +0000510 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000511 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000512 { OP_Transaction, 0, 0, 0}, /* 0 */
513 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000514 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000515 { OP_Integer, 0, 2, 0},
516 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000517 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000518 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000519 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000520 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000521 };
drh2ce18652016-01-16 20:50:21 +0000522 VdbeOp *aOp;
drhfb982642007-08-30 01:19:59 +0000523 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000524 if( !zRight ){
drh3c84ddf2008-01-09 02:15:38 +0000525 pParse->nMem += 2;
drhdad300d2016-01-18 00:20:26 +0000526 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
drh2ce18652016-01-16 20:50:21 +0000527 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
drhdad300d2016-01-18 00:20:26 +0000528 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000529 aOp[0].p1 = iDb;
530 aOp[1].p1 = iDb;
531 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
drhc11d4f92003-04-06 21:08:24 +0000532 }else{
drhd50ffc42011-03-08 02:38:28 +0000533 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000534 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh1861afc2016-02-01 21:48:34 +0000535 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
drh21206082011-04-04 18:22:02 +0000536 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000537 pDb->pSchema->cache_size = size;
538 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000539 }
drh9ccd8652013-09-13 16:36:46 +0000540 break;
541 }
drhe73c9142011-11-09 16:12:24 +0000542#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000543
drhe73c9142011-11-09 16:12:24 +0000544#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000545 /*
drh9b0cf342015-11-12 14:57:19 +0000546 ** PRAGMA [schema.]page_size
547 ** PRAGMA [schema.]page_size=N
drh90f5ecb2004-07-22 01:19:35 +0000548 **
549 ** The first form reports the current setting for the
550 ** database page size in bytes. The second form sets the
551 ** database page size value. The value can only be set if
552 ** the database has not yet been created.
553 */
drh9ccd8652013-09-13 16:36:46 +0000554 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000555 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000556 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000557 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000558 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drhc232aca2016-12-15 16:01:17 +0000559 returnSingleInt(v, size);
drh90f5ecb2004-07-22 01:19:35 +0000560 }else{
danielk1977992772c2007-08-30 10:07:38 +0000561 /* Malloc may fail when setting the page-size, as there is an internal
562 ** buffer that the pager module resizes using sqlite3_realloc().
563 */
drh60ac3f42010-11-23 18:59:27 +0000564 db->nextPagesize = sqlite3Atoi(zRight);
drhe937df82020-05-07 01:56:57 +0000565 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
drh4a642b62016-02-05 01:55:27 +0000566 sqlite3OomFault(db);
danielk1977992772c2007-08-30 10:07:38 +0000567 }
drh90f5ecb2004-07-22 01:19:35 +0000568 }
drh9ccd8652013-09-13 16:36:46 +0000569 break;
570 }
danielk197741483462007-03-24 16:45:04 +0000571
572 /*
drh9b0cf342015-11-12 14:57:19 +0000573 ** PRAGMA [schema.]secure_delete
drha5907a82017-06-19 11:44:22 +0000574 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
drh5b47efa2010-02-12 18:18:39 +0000575 **
576 ** The first form reports the current setting for the
577 ** secure_delete flag. The second form changes the secure_delete
drha5907a82017-06-19 11:44:22 +0000578 ** flag setting and reports the new value.
drh5b47efa2010-02-12 18:18:39 +0000579 */
drh9ccd8652013-09-13 16:36:46 +0000580 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000581 Btree *pBt = pDb->pBt;
582 int b = -1;
583 assert( pBt!=0 );
584 if( zRight ){
drha5907a82017-06-19 11:44:22 +0000585 if( sqlite3_stricmp(zRight, "fast")==0 ){
586 b = 2;
587 }else{
588 b = sqlite3GetBoolean(zRight, 0);
589 }
drh5b47efa2010-02-12 18:18:39 +0000590 }
drhaf034ed2010-02-12 19:46:26 +0000591 if( pId2->n==0 && b>=0 ){
592 int ii;
593 for(ii=0; ii<db->nDb; ii++){
594 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
595 }
596 }
drh5b47efa2010-02-12 18:18:39 +0000597 b = sqlite3BtreeSecureDelete(pBt, b);
drhc232aca2016-12-15 16:01:17 +0000598 returnSingleInt(v, b);
drh9ccd8652013-09-13 16:36:46 +0000599 break;
600 }
drh5b47efa2010-02-12 18:18:39 +0000601
602 /*
drh9b0cf342015-11-12 14:57:19 +0000603 ** PRAGMA [schema.]max_page_count
604 ** PRAGMA [schema.]max_page_count=N
drh60ac3f42010-11-23 18:59:27 +0000605 **
606 ** The first form reports the current setting for the
607 ** maximum number of pages in the database file. The
608 ** second form attempts to change this setting. Both
609 ** forms return the current setting.
610 **
drhe73c9142011-11-09 16:12:24 +0000611 ** The absolute value of N is used. This is undocumented and might
612 ** change. The only purpose is to provide an easy way to test
613 ** the sqlite3AbsInt32() function.
614 **
drh9b0cf342015-11-12 14:57:19 +0000615 ** PRAGMA [schema.]page_count
danielk197759a93792008-05-15 17:48:20 +0000616 **
617 ** Return the number of pages in the specified database.
618 */
drh9ccd8652013-09-13 16:36:46 +0000619 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000620 int iReg;
drhe9261db2020-07-20 12:47:32 +0000621 i64 x = 0;
danielk197759a93792008-05-15 17:48:20 +0000622 sqlite3CodeVerifySchema(pParse, iDb);
623 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000624 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000625 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
626 }else{
drhe9261db2020-07-20 12:47:32 +0000627 if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){
628 if( x<0 ) x = 0;
629 else if( x>0xfffffffe ) x = 0xfffffffe;
630 }else{
631 x = 0;
632 }
633 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, (int)x);
drh60ac3f42010-11-23 18:59:27 +0000634 }
danielk197759a93792008-05-15 17:48:20 +0000635 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
drh9ccd8652013-09-13 16:36:46 +0000636 break;
637 }
danielk197759a93792008-05-15 17:48:20 +0000638
639 /*
drh9b0cf342015-11-12 14:57:19 +0000640 ** PRAGMA [schema.]locking_mode
641 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
danielk197741483462007-03-24 16:45:04 +0000642 */
drh9ccd8652013-09-13 16:36:46 +0000643 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000644 const char *zRet = "normal";
645 int eMode = getLockingMode(zRight);
646
647 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
648 /* Simple "PRAGMA locking_mode;" statement. This is a query for
649 ** the current default locking mode (which may be different to
650 ** the locking-mode of the main database).
651 */
652 eMode = db->dfltLockMode;
653 }else{
654 Pager *pPager;
655 if( pId2->n==0 ){
656 /* This indicates that no database name was specified as part
657 ** of the PRAGMA command. In this case the locking-mode must be
658 ** set on all attached databases, as well as the main db file.
659 **
660 ** Also, the sqlite3.dfltLockMode variable is set so that
661 ** any subsequently attached databases also use the specified
662 ** locking mode.
663 */
664 int ii;
665 assert(pDb==&db->aDb[0]);
666 for(ii=2; ii<db->nDb; ii++){
667 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
668 sqlite3PagerLockingMode(pPager, eMode);
669 }
drh4f21c4a2008-12-10 22:15:00 +0000670 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000671 }
672 pPager = sqlite3BtreePager(pDb->pBt);
673 eMode = sqlite3PagerLockingMode(pPager, eMode);
674 }
675
drh9ccd8652013-09-13 16:36:46 +0000676 assert( eMode==PAGER_LOCKINGMODE_NORMAL
677 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000678 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
679 zRet = "exclusive";
680 }
drhc232aca2016-12-15 16:01:17 +0000681 returnSingleText(v, zRet);
drh9ccd8652013-09-13 16:36:46 +0000682 break;
683 }
drh3b020132008-04-17 17:02:01 +0000684
685 /*
drh9b0cf342015-11-12 14:57:19 +0000686 ** PRAGMA [schema.]journal_mode
687 ** PRAGMA [schema.]journal_mode =
drh3ebaee92010-05-06 21:37:22 +0000688 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000689 */
drh9ccd8652013-09-13 16:36:46 +0000690 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000691 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
692 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000693
drh3b020132008-04-17 17:02:01 +0000694 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000695 /* If there is no "=MODE" part of the pragma, do a query for the
696 ** current mode */
drh3b020132008-04-17 17:02:01 +0000697 eMode = PAGER_JOURNALMODE_QUERY;
698 }else{
dane04dc882010-04-20 18:53:15 +0000699 const char *zMode;
drhea678832008-12-10 19:26:22 +0000700 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000701 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000702 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
703 }
704 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000705 /* If the "=MODE" part does not match any known journal mode,
706 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000707 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000708 }
drh6c35b302019-05-17 20:37:17 +0000709 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
710 /* Do not allow journal-mode "OFF" in defensive since the database
711 ** can become corrupted using ordinary SQL when the journal is off */
712 eMode = PAGER_JOURNALMODE_QUERY;
713 }
drh3b020132008-04-17 17:02:01 +0000714 }
drhc6b2a0f2010-07-08 17:40:37 +0000715 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
716 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
717 iDb = 0;
718 pId2->n = 1;
719 }
720 for(ii=db->nDb-1; ii>=0; ii--){
721 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
722 sqlite3VdbeUsesBtree(v, ii);
723 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000724 }
drh3b020132008-04-17 17:02:01 +0000725 }
drh3b020132008-04-17 17:02:01 +0000726 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000727 break;
728 }
danielk1977b53e4962008-06-04 06:45:59 +0000729
730 /*
drh9b0cf342015-11-12 14:57:19 +0000731 ** PRAGMA [schema.]journal_size_limit
732 ** PRAGMA [schema.]journal_size_limit=N
danielk1977b53e4962008-06-04 06:45:59 +0000733 **
drha9e364f2009-01-13 20:14:15 +0000734 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000735 */
drh9ccd8652013-09-13 16:36:46 +0000736 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000737 Pager *pPager = sqlite3BtreePager(pDb->pBt);
738 i64 iLimit = -2;
739 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000740 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000741 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000742 }
743 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drhc232aca2016-12-15 16:01:17 +0000744 returnSingleInt(v, iLimit);
drh9ccd8652013-09-13 16:36:46 +0000745 break;
746 }
danielk1977b53e4962008-06-04 06:45:59 +0000747
drh13d70422004-11-13 15:59:14 +0000748#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000749
750 /*
drh9b0cf342015-11-12 14:57:19 +0000751 ** PRAGMA [schema.]auto_vacuum
752 ** PRAGMA [schema.]auto_vacuum=N
danielk1977951af802004-11-05 15:45:09 +0000753 **
drha9e364f2009-01-13 20:14:15 +0000754 ** Get or set the value of the database 'auto-vacuum' parameter.
755 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000756 */
757#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000758 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000759 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000760 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000761 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000762 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000763 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000764 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000765 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000766 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000767 /* Call SetAutoVacuum() to set initialize the internal auto and
768 ** incr-vacuum flags. This is required in case this connection
769 ** creates the database file. It is important that it is created
770 ** as an auto-vacuum capable db.
771 */
772 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
773 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
774 /* When setting the auto_vacuum mode to either "full" or
775 ** "incremental", write the value of meta[6] in the database
776 ** file. Before writing to meta[6], check that meta[3] indicates
777 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000778 */
drhb06a4ec2014-03-10 18:03:09 +0000779 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000780 static const VdbeOpList setMeta6[] = {
781 { OP_Transaction, 0, 1, 0}, /* 0 */
782 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
783 { OP_If, 1, 0, 0}, /* 2 */
784 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
drh1861afc2016-02-01 21:48:34 +0000785 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
drhf63936e2013-10-03 14:08:07 +0000786 };
drh2ce18652016-01-16 20:50:21 +0000787 VdbeOp *aOp;
788 int iAddr = sqlite3VdbeCurrentAddr(v);
drhdad300d2016-01-18 00:20:26 +0000789 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
drh2ce18652016-01-16 20:50:21 +0000790 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhdad300d2016-01-18 00:20:26 +0000791 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000792 aOp[0].p1 = iDb;
793 aOp[1].p1 = iDb;
794 aOp[2].p2 = iAddr+4;
drh1861afc2016-02-01 21:48:34 +0000795 aOp[4].p1 = iDb;
796 aOp[4].p3 = eAuto - 1;
drhf63936e2013-10-03 14:08:07 +0000797 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000798 }
danielk1977951af802004-11-05 15:45:09 +0000799 }
drh9ccd8652013-09-13 16:36:46 +0000800 break;
801 }
danielk1977951af802004-11-05 15:45:09 +0000802#endif
803
drhca5557f2007-05-04 18:30:40 +0000804 /*
drh9b0cf342015-11-12 14:57:19 +0000805 ** PRAGMA [schema.]incremental_vacuum(N)
drhca5557f2007-05-04 18:30:40 +0000806 **
807 ** Do N steps of incremental vacuuming on a database.
808 */
809#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000810 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000811 int iLimit, addr;
812 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
813 iLimit = 0x7fffffff;
814 }
815 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000816 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000817 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000818 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000819 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000820 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000821 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000822 break;
823 }
drhca5557f2007-05-04 18:30:40 +0000824#endif
825
drh13d70422004-11-13 15:59:14 +0000826#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000827 /*
drh9b0cf342015-11-12 14:57:19 +0000828 ** PRAGMA [schema.]cache_size
829 ** PRAGMA [schema.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000830 **
831 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000832 ** page cache size. The second form sets the local
833 ** page cache size value. If N is positive then that is the
834 ** number of pages in the cache. If N is negative, then the
835 ** number of pages is adjusted so that the cache uses -N kibibytes
836 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000837 */
drh9ccd8652013-09-13 16:36:46 +0000838 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000839 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000840 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000841 returnSingleInt(v, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000842 }else{
drh3b42abb2011-11-09 14:23:04 +0000843 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000844 pDb->pSchema->cache_size = size;
845 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000846 }
drh9ccd8652013-09-13 16:36:46 +0000847 break;
848 }
drhc11d4f92003-04-06 21:08:24 +0000849
850 /*
drh9b0cf342015-11-12 14:57:19 +0000851 ** PRAGMA [schema.]cache_spill
852 ** PRAGMA cache_spill=BOOLEAN
853 ** PRAGMA [schema.]cache_spill=N
854 **
855 ** The first form reports the current local setting for the
856 ** page cache spill size. The second form turns cache spill on
857 ** or off. When turnning cache spill on, the size is set to the
858 ** current cache_size. The third form sets a spill size that
859 ** may be different form the cache size.
860 ** If N is positive then that is the
861 ** number of pages in the cache. If N is negative, then the
862 ** number of pages is adjusted so that the cache uses -N kibibytes
863 ** of memory.
864 **
865 ** If the number of cache_spill pages is less then the number of
866 ** cache_size pages, no spilling occurs until the page count exceeds
867 ** the number of cache_size pages.
868 **
869 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
870 ** not just the schema specified.
871 */
872 case PragTyp_CACHE_SPILL: {
drh9b0cf342015-11-12 14:57:19 +0000873 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
874 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000875 returnSingleInt(v,
drh9b0cf342015-11-12 14:57:19 +0000876 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
877 sqlite3BtreeSetSpillSize(pDb->pBt,0));
878 }else{
drh4f9c8ec2015-11-12 15:47:48 +0000879 int size = 1;
drh9b0cf342015-11-12 14:57:19 +0000880 if( sqlite3GetInt32(zRight, &size) ){
881 sqlite3BtreeSetSpillSize(pDb->pBt, size);
882 }
drh4f9c8ec2015-11-12 15:47:48 +0000883 if( sqlite3GetBoolean(zRight, size!=0) ){
drh9b0cf342015-11-12 14:57:19 +0000884 db->flags |= SQLITE_CacheSpill;
885 }else{
drhd5b44d62018-12-06 17:06:02 +0000886 db->flags &= ~(u64)SQLITE_CacheSpill;
drh9b0cf342015-11-12 14:57:19 +0000887 }
drh4f9c8ec2015-11-12 15:47:48 +0000888 setAllPagerFlags(db);
drh9b0cf342015-11-12 14:57:19 +0000889 }
890 break;
891 }
892
893 /*
894 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000895 **
drh0d0614b2013-03-25 23:09:28 +0000896 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000897 ** used to limit the aggregate size of all memory mapped regions of the
898 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000899 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000900 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000901 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000902 **
drh0d0614b2013-03-25 23:09:28 +0000903 ** This value is advisory. The underlying VFS is free to memory map
904 ** as little or as much as it wants. Except, if N is set to 0 then the
905 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000906 */
drh9ccd8652013-09-13 16:36:46 +0000907 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000908 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000909#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000910 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000911 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000912 int ii;
drh9296c182014-07-23 13:40:49 +0000913 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000914 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
915 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000916 for(ii=db->nDb-1; ii>=0; ii--){
917 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000918 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000919 }
920 }
dan5d8a1372013-03-19 19:28:06 +0000921 }
drh9b4c59f2013-04-15 17:03:42 +0000922 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000923 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000924#else
dan3719f5f2013-05-23 10:13:18 +0000925 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000926 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000927#endif
dan3719f5f2013-05-23 10:13:18 +0000928 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000929 returnSingleInt(v, sz);
dan3719f5f2013-05-23 10:13:18 +0000930 }else if( rc!=SQLITE_NOTFOUND ){
931 pParse->nErr++;
932 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000933 }
drh9ccd8652013-09-13 16:36:46 +0000934 break;
935 }
dan5d8a1372013-03-19 19:28:06 +0000936
937 /*
drh90f5ecb2004-07-22 01:19:35 +0000938 ** PRAGMA temp_store
939 ** PRAGMA temp_store = "default"|"memory"|"file"
940 **
941 ** Return or set the local value of the temp_store flag. Changing
942 ** the local value does not make changes to the disk file and the default
943 ** value will be restored the next time the database is opened.
944 **
945 ** Note that it is possible for the library compile-time options to
946 ** override this setting
947 */
drh9ccd8652013-09-13 16:36:46 +0000948 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000949 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000950 returnSingleInt(v, db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000951 }else{
952 changeTempStorage(pParse, zRight);
953 }
drh9ccd8652013-09-13 16:36:46 +0000954 break;
955 }
drh90f5ecb2004-07-22 01:19:35 +0000956
957 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000958 ** PRAGMA temp_store_directory
959 ** PRAGMA temp_store_directory = ""|"directory_name"
960 **
961 ** Return or set the local value of the temp_store_directory flag. Changing
962 ** the value sets a specific directory to be used for temporary files.
963 ** Setting to a null string reverts to the default temporary directory search.
964 ** If temporary directory is changed, then invalidateTempStorage.
965 **
966 */
drh9ccd8652013-09-13 16:36:46 +0000967 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000968 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000969 returnSingleText(v, sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000970 }else{
drh78f82d12008-09-02 00:52:52 +0000971#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000972 if( zRight[0] ){
973 int res;
danielk1977fab11272008-09-16 14:38:02 +0000974 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
975 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000976 sqlite3ErrorMsg(pParse, "not a writable directory");
977 goto pragma_out;
978 }
drh268283b2005-01-08 15:44:25 +0000979 }
danielk1977b06a0b62008-06-26 10:54:12 +0000980 if( SQLITE_TEMP_STORE==0
981 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
982 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000983 ){
984 invalidateTempStorage(pParse);
985 }
drh17435752007-08-16 04:30:38 +0000986 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000987 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000988 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000989 }else{
drh268283b2005-01-08 15:44:25 +0000990 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000991 }
drh78f82d12008-09-02 00:52:52 +0000992#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000993 }
drh9ccd8652013-09-13 16:36:46 +0000994 break;
995 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000996
drhcc716452012-06-06 23:23:23 +0000997#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000998 /*
999 ** PRAGMA data_store_directory
1000 ** PRAGMA data_store_directory = ""|"directory_name"
1001 **
1002 ** Return or set the local value of the data_store_directory flag. Changing
1003 ** the value sets a specific directory to be used for database files that
1004 ** were specified with a relative pathname. Setting to a null string reverts
1005 ** to the default database directory, which for database files specified with
1006 ** a relative path will probably be based on the current directory for the
1007 ** process. Database file specified with an absolute path are not impacted
1008 ** by this setting, regardless of its value.
1009 **
1010 */
drh9ccd8652013-09-13 16:36:46 +00001011 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +00001012 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +00001013 returnSingleText(v, sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +00001014 }else{
1015#ifndef SQLITE_OMIT_WSD
1016 if( zRight[0] ){
1017 int res;
1018 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1019 if( rc!=SQLITE_OK || res==0 ){
1020 sqlite3ErrorMsg(pParse, "not a writable directory");
1021 goto pragma_out;
1022 }
1023 }
1024 sqlite3_free(sqlite3_data_directory);
1025 if( zRight[0] ){
1026 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1027 }else{
1028 sqlite3_data_directory = 0;
1029 }
1030#endif /* SQLITE_OMIT_WSD */
1031 }
drh9ccd8652013-09-13 16:36:46 +00001032 break;
1033 }
drhcc716452012-06-06 23:23:23 +00001034#endif
mistachkina112d142012-03-14 00:44:01 +00001035
drhd2cb50b2009-01-09 21:41:17 +00001036#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +00001037 /*
drh9b0cf342015-11-12 14:57:19 +00001038 ** PRAGMA [schema.]lock_proxy_file
1039 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +00001040 **
1041 ** Return or set the value of the lock_proxy_file flag. Changing
1042 ** the value sets a specific file to be used for database access locks.
1043 **
1044 */
1045 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +00001046 if( !zRight ){
1047 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1048 char *proxy_file_path = NULL;
1049 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +00001050 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +00001051 &proxy_file_path);
drhc232aca2016-12-15 16:01:17 +00001052 returnSingleText(v, proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +00001053 }else{
1054 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1055 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1056 int res;
1057 if( zRight[0] ){
1058 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1059 zRight);
1060 } else {
1061 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1062 NULL);
1063 }
1064 if( res!=SQLITE_OK ){
1065 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1066 goto pragma_out;
1067 }
1068 }
drh9ccd8652013-09-13 16:36:46 +00001069 break;
1070 }
drhd2cb50b2009-01-09 21:41:17 +00001071#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +00001072
1073 /*
drh9b0cf342015-11-12 14:57:19 +00001074 ** PRAGMA [schema.]synchronous
drh6841b1c2016-02-03 19:20:15 +00001075 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
drhc11d4f92003-04-06 21:08:24 +00001076 **
1077 ** Return or set the local value of the synchronous flag. Changing
1078 ** the local value does not make changes to the disk file and the
1079 ** default value will be restored the next time the database is
1080 ** opened.
1081 */
drh9ccd8652013-09-13 16:36:46 +00001082 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +00001083 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +00001084 returnSingleInt(v, pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +00001085 }else{
danielk197791cf71b2004-06-26 06:37:06 +00001086 if( !db->autoCommit ){
1087 sqlite3ErrorMsg(pParse,
1088 "Safety level may not be changed inside a transaction");
drh7553c822017-03-15 14:04:03 +00001089 }else if( iDb!=1 ){
drhd99d2832015-04-17 15:58:33 +00001090 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
1091 if( iLevel==0 ) iLevel = 1;
1092 pDb->safety_level = iLevel;
drh50a1a5a2016-03-08 14:40:11 +00001093 pDb->bSyncSet = 1;
drhd3605a42013-08-17 15:42:29 +00001094 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001095 }
drhc11d4f92003-04-06 21:08:24 +00001096 }
drh9ccd8652013-09-13 16:36:46 +00001097 break;
1098 }
drh13d70422004-11-13 15:59:14 +00001099#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001100
drhbf216272005-02-26 18:10:44 +00001101#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001102 case PragTyp_FLAG: {
1103 if( zRight==0 ){
drhc232aca2016-12-15 16:01:17 +00001104 setPragmaResultColumnNames(v, pPragma);
1105 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001106 }else{
drhfd748c62018-10-30 16:25:35 +00001107 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001108 if( db->autoCommit==0 ){
1109 /* Foreign key support may not be enabled or disabled while not
1110 ** in auto-commit mode. */
1111 mask &= ~(SQLITE_ForeignKeys);
1112 }
drhd4530972014-09-09 14:47:53 +00001113#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001114 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001115 /* Do not allow non-admin users to modify the schema arbitrarily */
1116 mask &= ~(SQLITE_WriteSchema);
1117 }
1118#endif
drh9ccd8652013-09-13 16:36:46 +00001119
1120 if( sqlite3GetBoolean(zRight, 0) ){
drh2eeca202020-01-08 20:37:45 +00001121 db->flags |= mask;
drh9ccd8652013-09-13 16:36:46 +00001122 }else{
1123 db->flags &= ~mask;
1124 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
drh635e6a92021-10-08 16:39:33 +00001125 if( (mask & SQLITE_WriteSchema)!=0
1126 && sqlite3_stricmp(zRight, "reset")==0
1127 ){
drhbc98f902021-10-14 17:30:32 +00001128 /* IMP: R-60817-01178 If the argument is "RESET" then schema
1129 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
1130 ** in addition, the schema is reloaded. */
drh635e6a92021-10-08 16:39:33 +00001131 sqlite3ResetAllSchemasOfConnection(db);
1132 }
drh9ccd8652013-09-13 16:36:46 +00001133 }
1134
1135 /* Many of the flag-pragmas modify the code generated by the SQL
1136 ** compiler (eg. count_changes). So add an opcode to expire all
1137 ** compiled SQL statements after modifying a pragma value.
1138 */
drh01c736d2016-05-20 15:15:07 +00001139 sqlite3VdbeAddOp0(v, OP_Expire);
drh9ccd8652013-09-13 16:36:46 +00001140 setAllPagerFlags(db);
1141 }
1142 break;
1143 }
drhbf216272005-02-26 18:10:44 +00001144#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001145
drh13d70422004-11-13 15:59:14 +00001146#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001147 /*
1148 ** PRAGMA table_info(<table>)
1149 **
1150 ** Return a single row for each column of the named table. The columns of
1151 ** the returned data set are:
1152 **
1153 ** cid: Column id (numbered from left to right, starting at 0)
1154 ** name: Column name
1155 ** type: Column declaration type.
1156 ** notnull: True if 'NOT NULL' is part of column declaration
1157 ** dflt_value: The default value for the column, if any.
dan3e6ac162016-02-11 21:01:16 +00001158 ** pk: Non-zero for PK fields.
danielk197791cf71b2004-06-26 06:37:06 +00001159 */
drh9ccd8652013-09-13 16:36:46 +00001160 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001161 Table *pTab;
drha78d2c02020-07-04 20:29:56 +00001162 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drh4d249e62016-06-10 22:49:01 +00001163 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001164 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001165 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001166 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001167 Column *pCol;
drh44156282013-10-23 22:23:03 +00001168 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drhd7dc0a32018-10-01 18:28:42 +00001169 pParse->nMem = 7;
danielk19774adee202004-05-08 08:23:19 +00001170 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001171 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
drhab3c5f22019-10-17 13:15:40 +00001172 int isHidden = 0;
drhf9751072021-10-07 13:40:29 +00001173 const Expr *pColExpr;
drhab3c5f22019-10-17 13:15:40 +00001174 if( pCol->colFlags & COLFLAG_NOINSERT ){
drh676fa252019-10-17 14:21:07 +00001175 if( pPragma->iArg==0 ){
1176 nHidden++;
1177 continue;
1178 }
drhab3c5f22019-10-17 13:15:40 +00001179 if( pCol->colFlags & COLFLAG_VIRTUAL ){
1180 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
drhc1431142019-10-17 17:54:05 +00001181 }else if( pCol->colFlags & COLFLAG_STORED ){
drhab3c5f22019-10-17 13:15:40 +00001182 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
drhc1431142019-10-17 17:54:05 +00001183 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
drhab3c5f22019-10-17 13:15:40 +00001184 isHidden = 1; /* HIDDEN */
1185 }
danielk1977034ca142007-06-26 10:38:54 +00001186 }
drh384b7fe2013-01-01 13:55:31 +00001187 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1188 k = 0;
1189 }else if( pPk==0 ){
1190 k = 1;
1191 }else{
drh1b678962015-04-15 07:19:27 +00001192 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001193 }
drhf9751072021-10-07 13:40:29 +00001194 pColExpr = sqlite3ColumnExpr(pTab,pCol);
1195 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
drh9d43db52021-10-07 14:19:32 +00001196 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)
1197 || isHidden>=2 );
drhd7dc0a32018-10-01 18:28:42 +00001198 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
drh076e85f2015-09-03 13:46:12 +00001199 i-nHidden,
drhcf9d36d2021-08-02 18:03:43 +00001200 pCol->zCnName,
drhd7564862016-03-22 20:05:09 +00001201 sqlite3ColumnType(pCol,""),
drh076e85f2015-09-03 13:46:12 +00001202 pCol->notNull ? 1 : 0,
drhf9751072021-10-07 13:40:29 +00001203 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
drhd7dc0a32018-10-01 18:28:42 +00001204 k,
1205 isHidden);
drhc11d4f92003-04-06 21:08:24 +00001206 }
1207 }
drh9ccd8652013-09-13 16:36:46 +00001208 }
1209 break;
drhc11d4f92003-04-06 21:08:24 +00001210
drh2e50f672021-09-21 17:26:23 +00001211 /*
1212 ** PRAGMA table_list
1213 **
1214 ** Return a single row for each table, virtual table, or view in the
1215 ** entire schema.
1216 **
1217 ** schema: Name of attached database hold this table
1218 ** name: Name of the table itself
1219 ** type: "table", "view", "virtual", "shadow"
1220 ** ncol: Number of columns
1221 ** wr: True for a WITHOUT ROWID table
1222 ** strict: True for a STRICT table
1223 */
1224 case PragTyp_TABLE_LIST: {
1225 int ii;
1226 pParse->nMem = 6;
1227 sqlite3CodeVerifyNamedSchema(pParse, zDb);
1228 for(ii=0; ii<db->nDb; ii++){
1229 HashElem *k;
1230 Hash *pHash;
drhdc88b402021-10-21 19:48:14 +00001231 int initNCol;
drh2e50f672021-09-21 17:26:23 +00001232 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
drhdc88b402021-10-21 19:48:14 +00001233
1234 /* Ensure that the Table.nCol field is initialized for all views
1235 ** and virtual tables. Each time we initialize a Table.nCol value
1236 ** for a table, that can potentially disrupt the hash table, so restart
1237 ** the initialization scan.
1238 */
drh2e50f672021-09-21 17:26:23 +00001239 pHash = &db->aDb[ii].pSchema->tblHash;
drhdc88b402021-10-21 19:48:14 +00001240 initNCol = sqliteHashCount(pHash);
1241 while( initNCol-- ){
1242 for(k=sqliteHashFirst(pHash); 1; k=sqliteHashNext(k) ){
1243 Table *pTab;
1244 if( k==0 ){ initNCol = 0; break; }
1245 pTab = sqliteHashData(k);
1246 if( pTab->nCol==0 ){
1247 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
1248 if( zSql ){
1249 sqlite3_stmt *pDummy = 0;
1250 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
1251 (void)sqlite3_finalize(pDummy);
1252 sqlite3DbFree(db, zSql);
1253 }
drh0c7d3d32022-01-24 16:47:12 +00001254 if( db->mallocFailed ){
1255 sqlite3ErrorMsg(db->pParse, "out of memory");
1256 db->pParse->rc = SQLITE_NOMEM_BKPT;
1257 }
drhdc88b402021-10-21 19:48:14 +00001258 pHash = &db->aDb[ii].pSchema->tblHash;
1259 break;
1260 }
1261 }
1262 }
1263
drh2e50f672021-09-21 17:26:23 +00001264 for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k) ){
1265 Table *pTab = sqliteHashData(k);
1266 const char *zType;
1267 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
1268 if( IsView(pTab) ){
1269 zType = "view";
1270 }else if( IsVirtual(pTab) ){
1271 zType = "virtual";
1272 }else if( pTab->tabFlags & TF_Shadow ){
1273 zType = "shadow";
1274 }else{
1275 zType = "table";
1276 }
1277 sqlite3VdbeMultiLoad(v, 1, "sssiii",
1278 db->aDb[ii].zDbSName,
drha4a871c2021-11-04 14:04:20 +00001279 sqlite3PreferredTableName(pTab->zName),
drh2e50f672021-09-21 17:26:23 +00001280 zType,
1281 pTab->nCol,
1282 (pTab->tabFlags & TF_WithoutRowid)!=0,
1283 (pTab->tabFlags & TF_Strict)!=0
1284 );
1285 }
1286 }
1287 }
1288 break;
1289
drh33bec3f2017-02-17 13:38:15 +00001290#ifdef SQLITE_DEBUG
drh3ef26152013-10-12 20:22:00 +00001291 case PragTyp_STATS: {
1292 Index *pIdx;
1293 HashElem *i;
drh33bec3f2017-02-17 13:38:15 +00001294 pParse->nMem = 5;
drh3ef26152013-10-12 20:22:00 +00001295 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001296 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1297 Table *pTab = sqliteHashData(i);
drh33bec3f2017-02-17 13:38:15 +00001298 sqlite3VdbeMultiLoad(v, 1, "ssiii",
drha4a871c2021-11-04 14:04:20 +00001299 sqlite3PreferredTableName(pTab->zName),
drh076e85f2015-09-03 13:46:12 +00001300 0,
drhd566c952016-02-25 21:19:03 +00001301 pTab->szTabRow,
drh33bec3f2017-02-17 13:38:15 +00001302 pTab->nRowLogEst,
1303 pTab->tabFlags);
drh3ef26152013-10-12 20:22:00 +00001304 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh40cf27c2017-07-07 16:00:53 +00001305 sqlite3VdbeMultiLoad(v, 2, "siiiX",
drh076e85f2015-09-03 13:46:12 +00001306 pIdx->zName,
drhd566c952016-02-25 21:19:03 +00001307 pIdx->szIdxRow,
drh33bec3f2017-02-17 13:38:15 +00001308 pIdx->aiRowLogEst[0],
1309 pIdx->hasStat1);
1310 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drh3ef26152013-10-12 20:22:00 +00001311 }
1312 }
1313 }
1314 break;
drh33bec3f2017-02-17 13:38:15 +00001315#endif
drh3ef26152013-10-12 20:22:00 +00001316
drh9ccd8652013-09-13 16:36:46 +00001317 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001318 Index *pIdx;
1319 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001320 pIdx = sqlite3FindIndex(db, zRight, zDb);
drh7b1904e2019-07-17 11:01:11 +00001321 if( pIdx==0 ){
1322 /* If there is no index named zRight, check to see if there is a
1323 ** WITHOUT ROWID table named zRight, and if there is, show the
1324 ** structure of the PRIMARY KEY index for that table. */
1325 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
1326 if( pTab && !HasRowid(pTab) ){
1327 pIdx = sqlite3PrimaryKeyIndex(pTab);
1328 }
1329 }
drhc11d4f92003-04-06 21:08:24 +00001330 if( pIdx ){
dan3c425482018-11-20 18:09:59 +00001331 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
drhc11d4f92003-04-06 21:08:24 +00001332 int i;
drh5e7028c2015-03-05 14:29:02 +00001333 int mx;
1334 if( pPragma->iArg ){
1335 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1336 mx = pIdx->nColumn;
1337 pParse->nMem = 6;
1338 }else{
1339 /* PRAGMA index_info (legacy version) */
1340 mx = pIdx->nKeyCol;
1341 pParse->nMem = 3;
1342 }
drhc11d4f92003-04-06 21:08:24 +00001343 pTab = pIdx->pTable;
dan3c425482018-11-20 18:09:59 +00001344 sqlite3CodeVerifySchema(pParse, iIdxDb);
drhc232aca2016-12-15 16:01:17 +00001345 assert( pParse->nMem<=pPragma->nPragCName );
drhc228be52015-01-31 02:00:01 +00001346 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001347 i16 cnum = pIdx->aiColumn[i];
drh40cf27c2017-07-07 16:00:53 +00001348 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
drhcf9d36d2021-08-02 18:03:43 +00001349 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
drh5e7028c2015-03-05 14:29:02 +00001350 if( pPragma->iArg ){
drh40cf27c2017-07-07 16:00:53 +00001351 sqlite3VdbeMultiLoad(v, 4, "isiX",
drh076e85f2015-09-03 13:46:12 +00001352 pIdx->aSortOrder[i],
1353 pIdx->azColl[i],
1354 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001355 }
1356 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001357 }
1358 }
drh9ccd8652013-09-13 16:36:46 +00001359 }
1360 break;
drhc11d4f92003-04-06 21:08:24 +00001361
drh9ccd8652013-09-13 16:36:46 +00001362 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001363 Index *pIdx;
1364 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001365 int i;
drh2783e4b2004-10-05 15:42:53 +00001366 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001367 if( pTab ){
dan3c425482018-11-20 18:09:59 +00001368 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhc228be52015-01-31 02:00:01 +00001369 pParse->nMem = 5;
dan3c425482018-11-20 18:09:59 +00001370 sqlite3CodeVerifySchema(pParse, iTabDb);
drh3ef26152013-10-12 20:22:00 +00001371 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001372 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001373 sqlite3VdbeMultiLoad(v, 1, "isisi",
1374 i,
1375 pIdx->zName,
1376 IsUniqueIndex(pIdx),
1377 azOrigin[pIdx->idxType],
1378 pIdx->pPartIdxWhere!=0);
drhc11d4f92003-04-06 21:08:24 +00001379 }
1380 }
drh9ccd8652013-09-13 16:36:46 +00001381 }
1382 break;
drhc11d4f92003-04-06 21:08:24 +00001383
drh9ccd8652013-09-13 16:36:46 +00001384 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001385 int i;
drh2d401ab2008-01-10 23:50:11 +00001386 pParse->nMem = 3;
drh13d70422004-11-13 15:59:14 +00001387 for(i=0; i<db->nDb; i++){
1388 if( db->aDb[i].pBt==0 ) continue;
drh69c33822016-08-18 14:33:11 +00001389 assert( db->aDb[i].zDbSName!=0 );
drh076e85f2015-09-03 13:46:12 +00001390 sqlite3VdbeMultiLoad(v, 1, "iss",
1391 i,
drh69c33822016-08-18 14:33:11 +00001392 db->aDb[i].zDbSName,
drh076e85f2015-09-03 13:46:12 +00001393 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh13d70422004-11-13 15:59:14 +00001394 }
drh9ccd8652013-09-13 16:36:46 +00001395 }
1396 break;
danielk197748af65a2005-02-09 03:20:37 +00001397
drh9ccd8652013-09-13 16:36:46 +00001398 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001399 int i = 0;
1400 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001401 pParse->nMem = 2;
danielk197748af65a2005-02-09 03:20:37 +00001402 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1403 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001404 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
danielk197748af65a2005-02-09 03:20:37 +00001405 }
drh9ccd8652013-09-13 16:36:46 +00001406 }
1407 break;
drhab53bb62017-07-07 15:43:22 +00001408
drhcc3f3d12019-08-17 15:27:58 +00001409#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
drhab53bb62017-07-07 15:43:22 +00001410 case PragTyp_FUNCTION_LIST: {
1411 int i;
1412 HashElem *j;
1413 FuncDef *p;
drh337ca512020-01-04 19:58:28 +00001414 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
drh79d5bc82020-01-04 01:43:02 +00001415 pParse->nMem = 6;
drhab53bb62017-07-07 15:43:22 +00001416 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
1417 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
drhf9751072021-10-07 13:40:29 +00001418 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
drh337ca512020-01-04 19:58:28 +00001419 pragmaFunclistLine(v, p, 1, showInternFunc);
drhab53bb62017-07-07 15:43:22 +00001420 }
1421 }
1422 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
1423 p = (FuncDef*)sqliteHashData(j);
drhf9751072021-10-07 13:40:29 +00001424 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
drh337ca512020-01-04 19:58:28 +00001425 pragmaFunclistLine(v, p, 0, showInternFunc);
drhab53bb62017-07-07 15:43:22 +00001426 }
1427 }
1428 break;
1429
1430#ifndef SQLITE_OMIT_VIRTUALTABLE
1431 case PragTyp_MODULE_LIST: {
1432 HashElem *j;
1433 pParse->nMem = 1;
1434 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
1435 Module *pMod = (Module*)sqliteHashData(j);
1436 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
drhab53bb62017-07-07 15:43:22 +00001437 }
1438 }
1439 break;
1440#endif /* SQLITE_OMIT_VIRTUALTABLE */
1441
drh8ae11aa2017-07-07 17:33:07 +00001442 case PragTyp_PRAGMA_LIST: {
1443 int i;
1444 for(i=0; i<ArraySize(aPragmaName); i++){
1445 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
drh8ae11aa2017-07-07 17:33:07 +00001446 }
1447 }
1448 break;
1449#endif /* SQLITE_INTROSPECTION_PRAGMAS */
drhab53bb62017-07-07 15:43:22 +00001450
drh13d70422004-11-13 15:59:14 +00001451#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1452
drhb7f91642004-10-31 02:22:47 +00001453#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001454 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001455 FKey *pFK;
1456 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001457 pTab = sqlite3FindTable(db, zRight, zDb);
drh78b2fa82021-10-07 12:11:20 +00001458 if( pTab && IsOrdinaryTable(pTab) ){
drhf38524d2021-08-02 16:41:57 +00001459 pFK = pTab->u.tab.pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001460 if( pFK ){
dan3c425482018-11-20 18:09:59 +00001461 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977742f9472004-06-16 12:02:43 +00001462 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001463 pParse->nMem = 8;
dan3c425482018-11-20 18:09:59 +00001464 sqlite3CodeVerifySchema(pParse, iTabDb);
danielk1977742f9472004-06-16 12:02:43 +00001465 while(pFK){
1466 int j;
1467 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001468 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1469 i,
1470 j,
1471 pFK->zTo,
drhcf9d36d2021-08-02 18:03:43 +00001472 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
drh076e85f2015-09-03 13:46:12 +00001473 pFK->aCol[j].zCol,
1474 actionName(pFK->aAction[1]), /* ON UPDATE */
1475 actionName(pFK->aAction[0]), /* ON DELETE */
1476 "NONE");
danielk1977742f9472004-06-16 12:02:43 +00001477 }
1478 ++i;
1479 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001480 }
drh78100cc2003-08-23 22:40:53 +00001481 }
1482 }
drh9ccd8652013-09-13 16:36:46 +00001483 }
1484 break;
drhb7f91642004-10-31 02:22:47 +00001485#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001486
drh6c5b9152012-12-17 16:46:37 +00001487#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001488#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001489 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001490 FKey *pFK; /* A foreign key constraint */
1491 Table *pTab; /* Child table contain "REFERENCES" keyword */
1492 Table *pParent; /* Parent table that child points to */
1493 Index *pIdx; /* Index in the parent table */
1494 int i; /* Loop counter: Foreign key number for pTab */
1495 int j; /* Loop counter: Field of the foreign key */
1496 HashElem *k; /* Loop counter: Next table in schema */
1497 int x; /* result variable */
1498 int regResult; /* 3 registers to hold a result row */
1499 int regKey; /* Register to hold key for checking the FK */
1500 int regRow; /* Registers to hold a row from pTab */
1501 int addrTop; /* Top of a loop checking foreign keys */
1502 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001503 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001504
drh613028b2012-12-17 18:43:02 +00001505 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001506 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001507 regKey = ++pParse->nMem;
1508 regRow = ++pParse->nMem;
drh613028b2012-12-17 18:43:02 +00001509 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1510 while( k ){
1511 if( zRight ){
1512 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1513 k = 0;
1514 }else{
1515 pTab = (Table*)sqliteHashData(k);
1516 k = sqliteHashNext(k);
1517 }
drh78b2fa82021-10-07 12:11:20 +00001518 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
drhec1650a2020-07-03 12:15:59 +00001519 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1520 zDb = db->aDb[iDb].zDbSName;
1521 sqlite3CodeVerifySchema(pParse, iDb);
1522 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001523 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drhec1650a2020-07-03 12:15:59 +00001524 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001525 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh78b2fa82021-10-07 12:11:20 +00001526 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:57 +00001527 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001528 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1529 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001530 pIdx = 0;
drhec1650a2020-07-03 12:15:59 +00001531 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001532 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001533 if( x==0 ){
1534 if( pIdx==0 ){
drhec1650a2020-07-03 12:15:59 +00001535 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
drh6c5b9152012-12-17 16:46:37 +00001536 }else{
drhec1650a2020-07-03 12:15:59 +00001537 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001538 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001539 }
1540 }else{
drh613028b2012-12-17 18:43:02 +00001541 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001542 break;
1543 }
drh6c5b9152012-12-17 16:46:37 +00001544 }
dan5e878302013-10-12 19:06:48 +00001545 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001546 if( pFK ) break;
1547 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001548 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh78b2fa82021-10-07 12:11:20 +00001549 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:57 +00001550 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001551 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001552 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001553 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001554 if( pParent ){
1555 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
drhd96e3822020-09-16 19:48:23 +00001556 assert( x==0 || db->mallocFailed );
dan5e878302013-10-12 19:06:48 +00001557 }
drhec4ccdb2018-12-29 02:26:59 +00001558 addrOk = sqlite3VdbeMakeLabel(pParse);
dan4bee5592017-04-17 18:42:33 +00001559
1560 /* Generate code to read the child key values into registers
1561 ** regRow..regRow+n. If any of the child key values are NULL, this
1562 ** row cannot cause an FK violation. Jump directly to addrOk in
1563 ** this case. */
drh4f16ff92021-07-07 16:48:24 +00001564 if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
dan4bee5592017-04-17 18:42:33 +00001565 for(j=0; j<pFK->nCol; j++){
1566 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
1567 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
1568 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001569 }
dan4bee5592017-04-17 18:42:33 +00001570
1571 /* Generate code to query the parent index for a matching parent
1572 ** key. If a match is found, jump to addrOk. */
1573 if( pIdx ){
1574 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
1575 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
1576 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
1577 VdbeCoverage(v);
1578 }else if( pParent ){
1579 int jmp = sqlite3VdbeCurrentAddr(v)+2;
1580 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
1581 sqlite3VdbeGoto(v, addrOk);
drhd96e3822020-09-16 19:48:23 +00001582 assert( pFK->nCol==1 || db->mallocFailed );
dan4bee5592017-04-17 18:42:33 +00001583 }
1584
1585 /* Generate code to report an FK violation to the caller. */
dan940464b2017-04-17 18:02:41 +00001586 if( HasRowid(pTab) ){
1587 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
1588 }else{
1589 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
1590 }
drh40cf27c2017-07-07 16:00:53 +00001591 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001592 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001593 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001594 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001595 }
drh688852a2014-02-17 22:40:43 +00001596 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001597 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001598 }
drh9ccd8652013-09-13 16:36:46 +00001599 }
1600 break;
dan09ff9e12013-03-11 11:49:03 +00001601#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001602#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1603
drh08652b52019-05-08 17:27:18 +00001604#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
drh55ef4d92005-08-14 01:20:37 +00001605 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1606 ** used will be case sensitive or not depending on the RHS.
1607 */
drh9ccd8652013-09-13 16:36:46 +00001608 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001609 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001610 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001611 }
drh9ccd8652013-09-13 16:36:46 +00001612 }
1613 break;
drh08652b52019-05-08 17:27:18 +00001614#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
drh55ef4d92005-08-14 01:20:37 +00001615
drh1dcdbc02007-01-27 02:24:54 +00001616#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1617# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1618#endif
1619
drhb7f91642004-10-31 02:22:47 +00001620#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh8b174f22017-02-22 15:11:36 +00001621 /* PRAGMA integrity_check
1622 ** PRAGMA integrity_check(N)
1623 ** PRAGMA quick_check
1624 ** PRAGMA quick_check(N)
1625 **
1626 ** Verify the integrity of the database.
1627 **
1628 ** The "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001629 ** integrity_check designed to detect most database corruption
drh8b174f22017-02-22 15:11:36 +00001630 ** without the overhead of cross-checking indexes. Quick_check
1631 ** is linear time wherease integrity_check is O(NlogN).
drh17d2d592020-07-23 00:45:06 +00001632 **
1633 ** The maximum nubmer of errors is 100 by default. A different default
1634 ** can be specified using a numeric parameter N.
1635 **
1636 ** Or, the parameter N can be the name of a table. In that case, only
1637 ** the one table named is verified. The freelist is only verified if
1638 ** the named table is "sqlite_schema" (or one of its aliases).
1639 **
1640 ** All schemas are checked by default. To check just a single
1641 ** schema, use the form:
1642 **
1643 ** PRAGMA schema.integrity_check;
danielk197741c58b72007-12-29 13:39:19 +00001644 */
drh9ccd8652013-09-13 16:36:46 +00001645 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001646 int i, j, addr, mxErr;
drh17d2d592020-07-23 00:45:06 +00001647 Table *pObjTab = 0; /* Check only this one table, if not NULL */
drhed717fe2003-06-15 23:42:24 +00001648
drhc5227312011-10-13 17:09:01 +00001649 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001650
dan5885e762012-07-16 10:06:12 +00001651 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1652 ** then iDb is set to the index of the database identified by <db>.
1653 ** In this case, the integrity of database iDb only is verified by
1654 ** the VDBE created below.
1655 **
1656 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1657 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1658 ** to -1 here, to indicate that the VDBE should verify the integrity
1659 ** of all attached databases. */
1660 assert( iDb>=0 );
1661 assert( iDb==0 || pId2->z );
1662 if( pId2->z==0 ) iDb = -1;
1663
drhed717fe2003-06-15 23:42:24 +00001664 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001665 pParse->nMem = 6;
drh1dcdbc02007-01-27 02:24:54 +00001666
1667 /* Set the maximum error count */
1668 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1669 if( zRight ){
drh17d2d592020-07-23 00:45:06 +00001670 if( sqlite3GetInt32(zRight, &mxErr) ){
1671 if( mxErr<=0 ){
1672 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1673 }
1674 }else{
1675 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
1676 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
drh1dcdbc02007-01-27 02:24:54 +00001677 }
1678 }
drh66accfc2017-02-22 18:04:42 +00001679 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001680
1681 /* Do an integrity check on each database file */
1682 for(i=0; i<db->nDb; i++){
drh9ecd7082017-09-10 01:06:05 +00001683 HashElem *x; /* For looping over tables in the schema */
1684 Hash *pTbls; /* Set of all tables in the schema */
1685 int *aRoot; /* Array of root page numbers of all btrees */
1686 int cnt = 0; /* Number of entries in aRoot[] */
1687 int mxIdx = 0; /* Maximum number of indexes for any table */
drhed717fe2003-06-15 23:42:24 +00001688
danielk197753c0f742005-03-29 03:10:59 +00001689 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001690 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001691
drh80242052004-06-09 00:48:12 +00001692 sqlite3CodeVerifySchema(pParse, i);
1693
drhed717fe2003-06-15 23:42:24 +00001694 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001695 **
drh98968b22016-03-15 22:00:39 +00001696 ** Begin by finding the root pages numbers
drh2d401ab2008-01-10 23:50:11 +00001697 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001698 */
dan5885e762012-07-16 10:06:12 +00001699 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001700 pTbls = &db->aDb[i].pSchema->tblHash;
drh98968b22016-03-15 22:00:39 +00001701 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh9ecd7082017-09-10 01:06:05 +00001702 Table *pTab = sqliteHashData(x); /* Current table */
1703 Index *pIdx; /* An index on pTab */
1704 int nIdx; /* Number of indexes on pTab */
drh17d2d592020-07-23 00:45:06 +00001705 if( pObjTab && pObjTab!=pTab ) continue;
drh98968b22016-03-15 22:00:39 +00001706 if( HasRowid(pTab) ) cnt++;
drhbb9b5f22016-03-19 00:35:02 +00001707 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
1708 if( nIdx>mxIdx ) mxIdx = nIdx;
drh98968b22016-03-15 22:00:39 +00001709 }
drh17d2d592020-07-23 00:45:06 +00001710 if( cnt==0 ) continue;
1711 if( pObjTab ) cnt++;
drh98968b22016-03-15 22:00:39 +00001712 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
1713 if( aRoot==0 ) break;
drh17d2d592020-07-23 00:45:06 +00001714 cnt = 0;
1715 if( pObjTab ) aRoot[++cnt] = 0;
1716 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh98968b22016-03-15 22:00:39 +00001717 Table *pTab = sqliteHashData(x);
1718 Index *pIdx;
drh17d2d592020-07-23 00:45:06 +00001719 if( pObjTab && pObjTab!=pTab ) continue;
drhb5c10632017-09-21 00:49:15 +00001720 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
drh79069752004-05-22 21:30:40 +00001721 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhb5c10632017-09-21 00:49:15 +00001722 aRoot[++cnt] = pIdx->tnum;
drh79069752004-05-22 21:30:40 +00001723 }
1724 }
drhb5c10632017-09-21 00:49:15 +00001725 aRoot[0] = cnt;
drh2d401ab2008-01-10 23:50:11 +00001726
1727 /* Make sure sufficient number of registers have been allocated */
drhbb9b5f22016-03-19 00:35:02 +00001728 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
drh3963e582017-07-15 20:33:19 +00001729 sqlite3ClearTempRegCache(pParse);
drh2d401ab2008-01-10 23:50:11 +00001730
1731 /* Do the b-tree integrity checks */
drh98968b22016-03-15 22:00:39 +00001732 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
drh4f21c4a2008-12-10 22:15:00 +00001733 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001734 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001735 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh69c33822016-08-18 14:33:11 +00001736 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
drh66a51672008-01-03 00:01:23 +00001737 P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:05 +00001738 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
1739 integrityCheckResultRow(v);
drh1dcdbc02007-01-27 02:24:54 +00001740 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001741
1742 /* Make sure all the indices are constructed correctly.
1743 */
drh8b174f22017-02-22 15:11:36 +00001744 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001745 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001746 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001747 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001748 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001749 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001750 int r1 = -1;
drh9e1209d2021-08-19 02:58:15 +00001751 int bStrict;
drhed717fe2003-06-15 23:42:24 +00001752
drh99666212021-10-11 15:21:42 +00001753 if( !IsOrdinaryTable(pTab) ) continue;
drh17d2d592020-07-23 00:45:06 +00001754 if( pObjTab && pObjTab!=pTab ) continue;
drh26198bb2013-10-31 11:15:09 +00001755 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
danfd261ec2015-10-22 20:54:33 +00001756 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001757 1, 0, &iDataCur, &iIdxCur);
drh9ecd7082017-09-10 01:06:05 +00001758 /* reg[7] counts the number of entries in the table.
1759 ** reg[8+i] counts the number of entries in the i-th index
1760 */
drh6fbe41a2013-10-30 20:22:55 +00001761 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001762 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001763 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001764 }
drhbb9b5f22016-03-19 00:35:02 +00001765 assert( pParse->nMem>=8+j );
1766 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
drh688852a2014-02-17 22:40:43 +00001767 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001768 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drh4011c442018-06-06 19:48:19 +00001769 if( !isQuick ){
1770 /* Sanity check on record header decoding */
drh0b0b3a92019-10-17 18:35:57 +00001771 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
drh4011c442018-06-06 19:48:19 +00001772 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
drh71c770f2021-08-19 16:29:33 +00001773 VdbeComment((v, "(right-most column)"));
drh4011c442018-06-06 19:48:19 +00001774 }
drh9e1209d2021-08-19 02:58:15 +00001775 /* Verify that all NOT NULL columns really are NOT NULL. At the
1776 ** same time verify the type of the content of STRICT tables */
1777 bStrict = (pTab->tabFlags & TF_Strict)!=0;
drhcefc87f2014-08-01 01:40:33 +00001778 for(j=0; j<pTab->nCol; j++){
1779 char *zErr;
drh9e1209d2021-08-19 02:58:15 +00001780 Column *pCol = pTab->aCol + j;
drh71c770f2021-08-19 16:29:33 +00001781 int doError, jmp2;
drhcefc87f2014-08-01 01:40:33 +00001782 if( j==pTab->iPKey ) continue;
drh9e1209d2021-08-19 02:58:15 +00001783 if( pCol->notNull==0 && !bStrict ) continue;
drh71c770f2021-08-19 16:29:33 +00001784 doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0;
drhcefc87f2014-08-01 01:40:33 +00001785 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
drhebd70ee2019-12-09 15:52:07 +00001786 if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){
1787 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1788 }
drh9e1209d2021-08-19 02:58:15 +00001789 if( pCol->notNull ){
drh9e1209d2021-08-19 02:58:15 +00001790 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1791 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1792 pCol->zCnName);
1793 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh5c929042021-11-03 11:43:09 +00001794 if( bStrict && pCol->eCType!=COLTYPE_ANY ){
drh71c770f2021-08-19 16:29:33 +00001795 sqlite3VdbeGoto(v, doError);
1796 }else{
1797 integrityCheckResultRow(v);
1798 }
drh9e1209d2021-08-19 02:58:15 +00001799 sqlite3VdbeJumpHere(v, jmp2);
1800 }
drh94b70d82021-09-08 19:25:21 +00001801 if( (pTab->tabFlags & TF_Strict)!=0
1802 && pCol->eCType!=COLTYPE_ANY
1803 ){
drh71c770f2021-08-19 16:29:33 +00001804 jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0,
drh9e1209d2021-08-19 02:58:15 +00001805 sqlite3StdTypeMap[pCol->eCType-1]);
1806 VdbeCoverage(v);
1807 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
1808 sqlite3StdType[pCol->eCType-1],
1809 pTab->zName, pTab->aCol[j].zCnName);
1810 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh71c770f2021-08-19 16:29:33 +00001811 sqlite3VdbeResolveLabel(v, doError);
drh9e1209d2021-08-19 02:58:15 +00001812 integrityCheckResultRow(v);
drh71c770f2021-08-19 16:29:33 +00001813 sqlite3VdbeJumpHere(v, jmp2);
drh9e1209d2021-08-19 02:58:15 +00001814 }
drhcefc87f2014-08-01 01:40:33 +00001815 }
drh8a284dc2017-02-22 14:15:37 +00001816 /* Verify CHECK constraints */
1817 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
dan75f95582017-04-04 19:58:54 +00001818 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
1819 if( db->mallocFailed==0 ){
drhec4ccdb2018-12-29 02:26:59 +00001820 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
1821 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
dan75f95582017-04-04 19:58:54 +00001822 char *zErr;
1823 int k;
drh6e97f8e2017-07-20 13:17:08 +00001824 pParse->iSelfTab = iDataCur + 1;
dan75f95582017-04-04 19:58:54 +00001825 for(k=pCheck->nExpr-1; k>0; k--){
1826 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
1827 }
1828 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
1829 SQLITE_JUMPIFNULL);
1830 sqlite3VdbeResolveLabel(v, addrCkFault);
drh3e34eab2017-07-19 19:48:40 +00001831 pParse->iSelfTab = 0;
dan75f95582017-04-04 19:58:54 +00001832 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
1833 pTab->zName);
1834 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:05 +00001835 integrityCheckResultRow(v);
dan75f95582017-04-04 19:58:54 +00001836 sqlite3VdbeResolveLabel(v, addrCkOk);
drh8a284dc2017-02-22 14:15:37 +00001837 }
dan75f95582017-04-04 19:58:54 +00001838 sqlite3ExprListDelete(db, pCheck);
drhcefc87f2014-08-01 01:40:33 +00001839 }
drh226cef42017-09-09 20:38:49 +00001840 if( !isQuick ){ /* Omit the remaining tests for quick_check */
drh226cef42017-09-09 20:38:49 +00001841 /* Validate index entries for the current row */
1842 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1843 int jmp2, jmp3, jmp4, jmp5;
drhec4ccdb2018-12-29 02:26:59 +00001844 int ckUniq = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:49 +00001845 if( pPk==pIdx ) continue;
1846 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1847 pPrior, r1);
1848 pPrior = pIdx;
1849 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
1850 /* Verify that an index entry exists for the current table row */
1851 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
1852 pIdx->nColumn); VdbeCoverage(v);
1853 sqlite3VdbeLoadString(v, 3, "row ");
1854 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1855 sqlite3VdbeLoadString(v, 4, " missing from index ");
1856 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1857 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
1858 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh9ecd7082017-09-10 01:06:05 +00001859 jmp4 = integrityCheckResultRow(v);
drh226cef42017-09-09 20:38:49 +00001860 sqlite3VdbeJumpHere(v, jmp2);
1861 /* For UNIQUE indexes, verify that only one entry exists with the
1862 ** current key. The entry is unique if (1) any column is NULL
1863 ** or (2) the next entry has a different key */
1864 if( IsUniqueIndex(pIdx) ){
drhec4ccdb2018-12-29 02:26:59 +00001865 int uniqOk = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:49 +00001866 int jmp6;
1867 int kk;
1868 for(kk=0; kk<pIdx->nKeyCol; kk++){
1869 int iCol = pIdx->aiColumn[kk];
1870 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
1871 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
1872 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1873 VdbeCoverage(v);
1874 }
1875 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
1876 sqlite3VdbeGoto(v, uniqOk);
1877 sqlite3VdbeJumpHere(v, jmp6);
1878 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1879 pIdx->nKeyCol); VdbeCoverage(v);
1880 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1881 sqlite3VdbeGoto(v, jmp5);
1882 sqlite3VdbeResolveLabel(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00001883 }
drh226cef42017-09-09 20:38:49 +00001884 sqlite3VdbeJumpHere(v, jmp4);
1885 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhcefc87f2014-08-01 01:40:33 +00001886 }
drhed717fe2003-06-15 23:42:24 +00001887 }
drh688852a2014-02-17 22:40:43 +00001888 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001889 sqlite3VdbeJumpHere(v, loopTop-1);
drh8b174f22017-02-22 15:11:36 +00001890 if( !isQuick ){
1891 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
1892 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1893 if( pPk==pIdx ) continue;
drh8b174f22017-02-22 15:11:36 +00001894 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh66accfc2017-02-22 18:04:42 +00001895 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
drh8b174f22017-02-22 15:11:36 +00001896 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh9ecd7082017-09-10 01:06:05 +00001897 sqlite3VdbeLoadString(v, 4, pIdx->zName);
1898 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
1899 integrityCheckResultRow(v);
drh66accfc2017-02-22 18:04:42 +00001900 sqlite3VdbeJumpHere(v, addr);
drh8b174f22017-02-22 15:11:36 +00001901 }
drhed717fe2003-06-15 23:42:24 +00001902 }
1903 }
1904 }
drh2ce18652016-01-16 20:50:21 +00001905 {
1906 static const int iLn = VDBE_OFFSET_LINENO(2);
1907 static const VdbeOpList endCode[] = {
1908 { OP_AddImm, 1, 0, 0}, /* 0 */
drh66accfc2017-02-22 18:04:42 +00001909 { OP_IfNotZero, 1, 4, 0}, /* 1 */
drh2ce18652016-01-16 20:50:21 +00001910 { OP_String8, 0, 3, 0}, /* 2 */
drh1b325542016-02-03 01:55:44 +00001911 { OP_ResultRow, 3, 1, 0}, /* 3 */
drh74588ce2017-09-13 00:13:05 +00001912 { OP_Halt, 0, 0, 0}, /* 4 */
1913 { OP_String8, 0, 3, 0}, /* 5 */
1914 { OP_Goto, 0, 3, 0}, /* 6 */
drh2ce18652016-01-16 20:50:21 +00001915 };
1916 VdbeOp *aOp;
1917
1918 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
1919 if( aOp ){
drh66accfc2017-02-22 18:04:42 +00001920 aOp[0].p2 = 1-mxErr;
drh2ce18652016-01-16 20:50:21 +00001921 aOp[2].p4type = P4_STATIC;
1922 aOp[2].p4.z = "ok";
drh74588ce2017-09-13 00:13:05 +00001923 aOp[5].p4type = P4_STATIC;
1924 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
drh2ce18652016-01-16 20:50:21 +00001925 }
drh74588ce2017-09-13 00:13:05 +00001926 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
drh2ce18652016-01-16 20:50:21 +00001927 }
drh9ccd8652013-09-13 16:36:46 +00001928 }
1929 break;
drhb7f91642004-10-31 02:22:47 +00001930#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1931
drh13d70422004-11-13 15:59:14 +00001932#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001933 /*
1934 ** PRAGMA encoding
1935 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1936 **
drh85b623f2007-12-13 21:54:09 +00001937 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001938 ** database. If the database is not initialized, it is initialized now.
1939 **
1940 ** The second form of this pragma is a no-op if the main database file
1941 ** has not already been initialized. In this case it sets the default
1942 ** encoding that will be used for the main database file if a new file
1943 ** is created. If an existing main database file is opened, then the
1944 ** default text encoding for the existing database is used.
1945 **
1946 ** In all cases new databases created using the ATTACH command are
1947 ** created to use the same default text encoding as the main database. If
1948 ** the main database has not been initialized and/or created when ATTACH
1949 ** is executed, this is done before the ATTACH operation.
1950 **
1951 ** In the second form this pragma sets the text encoding to be used in
1952 ** new database files created using this database handle. It is only
1953 ** useful if invoked immediately after the main database i
1954 */
drh9ccd8652013-09-13 16:36:46 +00001955 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001956 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001957 char *zName;
1958 u8 enc;
1959 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001960 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001961 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1962 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1963 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001964 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001965 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001966 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1967 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001968 { 0, 0 }
1969 };
drh0f7eb612006-08-08 13:51:43 +00001970 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001971 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001972 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00001973 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1974 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1975 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drhc232aca2016-12-15 16:01:17 +00001976 returnSingleText(v, encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00001977 }else{ /* "PRAGMA encoding = XXX" */
1978 /* Only change the value of sqlite.enc if the database handle is not
1979 ** initialized. If the main database exists, the new sqlite.enc value
1980 ** will be overwritten when the schema is next loaded. If it does not
1981 ** already exists, it will be created to use the new encoding value.
1982 */
dan0ea2d422020-03-05 18:04:09 +00001983 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
danielk19778e227872004-06-07 07:52:17 +00001984 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1985 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh42a630b2020-03-05 16:13:24 +00001986 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
1987 SCHEMA_ENC(db) = enc;
1988 sqlite3SetTextEncoding(db, enc);
danielk19778e227872004-06-07 07:52:17 +00001989 break;
1990 }
1991 }
1992 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001993 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001994 }
1995 }
1996 }
drh9ccd8652013-09-13 16:36:46 +00001997 }
1998 break;
drh13d70422004-11-13 15:59:14 +00001999#endif /* SQLITE_OMIT_UTF16 */
2000
2001#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00002002 /*
drh9b0cf342015-11-12 14:57:19 +00002003 ** PRAGMA [schema.]schema_version
2004 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002005 **
drh9b0cf342015-11-12 14:57:19 +00002006 ** PRAGMA [schema.]user_version
2007 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002008 **
drhe459bd42016-03-16 20:05:57 +00002009 ** PRAGMA [schema.]freelist_count
2010 **
2011 ** PRAGMA [schema.]data_version
drh4ee09b42013-05-01 19:49:27 +00002012 **
drh9b0cf342015-11-12 14:57:19 +00002013 ** PRAGMA [schema.]application_id
2014 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00002015 **
danielk1977b92b70b2004-11-12 16:11:59 +00002016 ** The pragma's schema_version and user_version are used to set or get
2017 ** the value of the schema-version and user-version, respectively. Both
2018 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00002019 ** stored in the database header.
2020 **
2021 ** The schema-cookie is usually only manipulated internally by SQLite. It
2022 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00002023 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00002024 ** SQLite each time a query is executed to ensure that the internal cache
2025 ** of the schema used when compiling the SQL query matches the schema of
2026 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00002027 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2028 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00002029 ** crashes or database corruption. Use with caution!
2030 **
danielk1977b92b70b2004-11-12 16:11:59 +00002031 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00002032 ** applications for any purpose.
2033 */
drh9ccd8652013-09-13 16:36:46 +00002034 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00002035 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00002036 sqlite3VdbeUsesBtree(v, iDb);
drhc232aca2016-12-15 16:01:17 +00002037 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00002038 /* Write the specified cookie value */
2039 static const VdbeOpList setCookie[] = {
2040 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1861afc2016-02-01 21:48:34 +00002041 { OP_SetCookie, 0, 0, 0}, /* 1 */
danielk1977dae24952004-11-11 05:10:43 +00002042 };
drh2ce18652016-01-16 20:50:21 +00002043 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00002044 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
drh2ce18652016-01-16 20:50:21 +00002045 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
drhdad300d2016-01-18 00:20:26 +00002046 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00002047 aOp[0].p1 = iDb;
drh1861afc2016-02-01 21:48:34 +00002048 aOp[1].p1 = iDb;
2049 aOp[1].p2 = iCookie;
2050 aOp[1].p3 = sqlite3Atoi(zRight);
drhe3863b52020-07-01 16:19:14 +00002051 aOp[1].p5 = 1;
danielk1977dae24952004-11-11 05:10:43 +00002052 }else{
2053 /* Read the specified cookie value */
2054 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00002055 { OP_Transaction, 0, 0, 0}, /* 0 */
2056 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00002057 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00002058 };
drh2ce18652016-01-16 20:50:21 +00002059 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00002060 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
drh2ce18652016-01-16 20:50:21 +00002061 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
drhdad300d2016-01-18 00:20:26 +00002062 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00002063 aOp[0].p1 = iDb;
2064 aOp[1].p1 = iDb;
2065 aOp[1].p3 = iCookie;
drhf71a3662016-03-16 20:44:45 +00002066 sqlite3VdbeReusable(v);
danielk1977dae24952004-11-11 05:10:43 +00002067 }
drh9ccd8652013-09-13 16:36:46 +00002068 }
2069 break;
drh13d70422004-11-13 15:59:14 +00002070#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00002071
shanehdc97a8c2010-02-23 20:08:35 +00002072#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2073 /*
2074 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00002075 **
drh71caabf2010-02-26 15:39:24 +00002076 ** Return the names of all compile-time options used in this build,
2077 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00002078 */
drh9ccd8652013-09-13 16:36:46 +00002079 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00002080 int i = 0;
2081 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00002082 pParse->nMem = 1;
shanehdc97a8c2010-02-23 20:08:35 +00002083 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00002084 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00002085 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2086 }
drhf71a3662016-03-16 20:44:45 +00002087 sqlite3VdbeReusable(v);
drh9ccd8652013-09-13 16:36:46 +00002088 }
2089 break;
shanehdc97a8c2010-02-23 20:08:35 +00002090#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2091
dan5cf53532010-05-01 16:40:20 +00002092#ifndef SQLITE_OMIT_WAL
2093 /*
drh9b0cf342015-11-12 14:57:19 +00002094 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00002095 **
2096 ** Checkpoint the database.
2097 */
drh9ccd8652013-09-13 16:36:46 +00002098 case PragTyp_WAL_CHECKPOINT: {
drh099b3852021-03-10 16:35:37 +00002099 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
dancdc1f042010-11-18 12:11:05 +00002100 int eMode = SQLITE_CHECKPOINT_PASSIVE;
2101 if( zRight ){
2102 if( sqlite3StrICmp(zRight, "full")==0 ){
2103 eMode = SQLITE_CHECKPOINT_FULL;
2104 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2105 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00002106 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
2107 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00002108 }
2109 }
dancdc1f042010-11-18 12:11:05 +00002110 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00002111 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00002112 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00002113 }
2114 break;
dan5a299f92010-05-03 11:05:08 +00002115
2116 /*
2117 ** PRAGMA wal_autocheckpoint
2118 ** PRAGMA wal_autocheckpoint = N
2119 **
2120 ** Configure a database connection to automatically checkpoint a database
2121 ** after accumulating N frames in the log. Or query for the current value
2122 ** of N.
2123 */
drh9ccd8652013-09-13 16:36:46 +00002124 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00002125 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00002126 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00002127 }
drhc232aca2016-12-15 16:01:17 +00002128 returnSingleInt(v,
drhb033d8b2010-05-03 13:37:30 +00002129 db->xWalCallback==sqlite3WalDefaultHook ?
2130 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00002131 }
2132 break;
dan5cf53532010-05-01 16:40:20 +00002133#endif
dan7c246102010-04-12 19:00:29 +00002134
drh09419b42011-11-16 19:29:17 +00002135 /*
2136 ** PRAGMA shrink_memory
2137 **
drh51a74d42015-02-28 01:04:27 +00002138 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
2139 ** connection on which it is invoked to free up as much memory as it
2140 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00002141 */
drh9ccd8652013-09-13 16:36:46 +00002142 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00002143 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00002144 break;
2145 }
drh09419b42011-11-16 19:29:17 +00002146
drhf3603962012-09-07 16:46:59 +00002147 /*
drh2ead47c2017-02-22 20:24:10 +00002148 ** PRAGMA optimize
drh1cfaf8e2017-03-02 14:17:21 +00002149 ** PRAGMA optimize(MASK)
drh2ead47c2017-02-22 20:24:10 +00002150 ** PRAGMA schema.optimize
drh1cfaf8e2017-03-02 14:17:21 +00002151 ** PRAGMA schema.optimize(MASK)
drh99d5b4c2017-02-18 22:52:40 +00002152 **
drh2ead47c2017-02-22 20:24:10 +00002153 ** Attempt to optimize the database. All schemas are optimized in the first
drh1cfaf8e2017-03-02 14:17:21 +00002154 ** two forms, and only the specified schema is optimized in the latter two.
drh2ead47c2017-02-22 20:24:10 +00002155 **
drhe1f1c082017-03-06 20:44:13 +00002156 ** The details of optimizations performed by this pragma are expected
drh2ead47c2017-02-22 20:24:10 +00002157 ** to change and improve over time. Applications should anticipate that
2158 ** this pragma will perform new optimizations in future releases.
2159 **
drh1cfaf8e2017-03-02 14:17:21 +00002160 ** The optional argument is a bitmask of optimizations to perform:
drh2ead47c2017-02-22 20:24:10 +00002161 **
drh1cfaf8e2017-03-02 14:17:21 +00002162 ** 0x0001 Debugging mode. Do not actually perform any optimizations
2163 ** but instead return one line of text for each optimization
2164 ** that would have been done. Off by default.
drh99d5b4c2017-02-18 22:52:40 +00002165 **
drh1cfaf8e2017-03-02 14:17:21 +00002166 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
2167 ** See below for additional information.
2168 **
2169 ** 0x0004 (Not yet implemented) Record usage and performance
2170 ** information from the current session in the
2171 ** database file so that it will be available to "optimize"
2172 ** pragmas run by future database connections.
2173 **
2174 ** 0x0008 (Not yet implemented) Create indexes that might have
2175 ** been helpful to recent queries
2176 **
drhe7c6f972017-07-15 20:25:22 +00002177 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
2178 ** of the optimizations listed above except Debug Mode, including new
drh59dbe3a2017-03-06 23:51:16 +00002179 ** optimizations that have not yet been invented. If new optimizations are
2180 ** ever added that should be off by default, those off-by-default
2181 ** optimizations will have bitmasks of 0x10000 or larger.
drh1cfaf8e2017-03-02 14:17:21 +00002182 **
2183 ** DETERMINATION OF WHEN TO RUN ANALYZE
2184 **
2185 ** In the current implementation, a table is analyzed if only if all of
drh2ead47c2017-02-22 20:24:10 +00002186 ** the following are true:
drh99d5b4c2017-02-18 22:52:40 +00002187 **
drh1cfaf8e2017-03-02 14:17:21 +00002188 ** (1) MASK bit 0x02 is set.
2189 **
2190 ** (2) The query planner used sqlite_stat1-style statistics for one or
drh99d5b4c2017-02-18 22:52:40 +00002191 ** more indexes of the table at some point during the lifetime of
2192 ** the current connection.
2193 **
drh1cfaf8e2017-03-02 14:17:21 +00002194 ** (3) One or more indexes of the table are currently unanalyzed OR
drh99d5b4c2017-02-18 22:52:40 +00002195 ** the number of rows in the table has increased by 25 times or more
2196 ** since the last time ANALYZE was run.
drh2ead47c2017-02-22 20:24:10 +00002197 **
2198 ** The rules for when tables are analyzed are likely to change in
2199 ** future releases.
drh72052a72017-02-17 16:26:34 +00002200 */
drh2ead47c2017-02-22 20:24:10 +00002201 case PragTyp_OPTIMIZE: {
drh99d5b4c2017-02-18 22:52:40 +00002202 int iDbLast; /* Loop termination point for the schema loop */
2203 int iTabCur; /* Cursor for a table whose size needs checking */
2204 HashElem *k; /* Loop over tables of a schema */
2205 Schema *pSchema; /* The current schema */
2206 Table *pTab; /* A table in the schema */
2207 Index *pIdx; /* An index of the table */
2208 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
2209 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
drh1cfaf8e2017-03-02 14:17:21 +00002210 u32 opMask; /* Mask of operations to perform */
drh4a54bb52017-02-18 15:58:52 +00002211
drh1cfaf8e2017-03-02 14:17:21 +00002212 if( zRight ){
2213 opMask = (u32)sqlite3Atoi(zRight);
2214 if( (opMask & 0x02)==0 ) break;
2215 }else{
drh59dbe3a2017-03-06 23:51:16 +00002216 opMask = 0xfffe;
drh1cfaf8e2017-03-02 14:17:21 +00002217 }
drh4a54bb52017-02-18 15:58:52 +00002218 iTabCur = pParse->nTab++;
2219 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
2220 if( iDb==1 ) continue;
2221 sqlite3CodeVerifySchema(pParse, iDb);
2222 pSchema = db->aDb[iDb].pSchema;
2223 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
2224 pTab = (Table*)sqliteHashData(k);
drh99d5b4c2017-02-18 22:52:40 +00002225
2226 /* If table pTab has not been used in a way that would benefit from
2227 ** having analysis statistics during the current session, then skip it.
2228 ** This also has the effect of skipping virtual tables and views */
drh4a54bb52017-02-18 15:58:52 +00002229 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
drh99d5b4c2017-02-18 22:52:40 +00002230
2231 /* Reanalyze if the table is 25 times larger than the last analysis */
drh4a54bb52017-02-18 15:58:52 +00002232 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
2233 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2234 if( !pIdx->hasStat1 ){
drh99d5b4c2017-02-18 22:52:40 +00002235 szThreshold = 0; /* Always analyze if any index lacks statistics */
drh4a54bb52017-02-18 15:58:52 +00002236 break;
2237 }
2238 }
2239 if( szThreshold ){
2240 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
2241 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
drh1cfaf8e2017-03-02 14:17:21 +00002242 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
drh4a54bb52017-02-18 15:58:52 +00002243 VdbeCoverage(v);
2244 }
2245 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
2246 db->aDb[iDb].zDbSName, pTab->zName);
drh1cfaf8e2017-03-02 14:17:21 +00002247 if( opMask & 0x01 ){
2248 int r1 = sqlite3GetTempReg(pParse);
2249 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
2250 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
2251 }else{
2252 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
2253 }
drh4a54bb52017-02-18 15:58:52 +00002254 }
2255 }
drhbce04142017-02-23 00:58:36 +00002256 sqlite3VdbeAddOp0(v, OP_Expire);
drh72052a72017-02-17 16:26:34 +00002257 break;
2258 }
2259
2260 /*
drhf3603962012-09-07 16:46:59 +00002261 ** PRAGMA busy_timeout
2262 ** PRAGMA busy_timeout = N
2263 **
2264 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00002265 ** if one is set. If no busy handler or a different busy handler is set
2266 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2267 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00002268 */
drhd49c3582013-09-13 19:00:06 +00002269 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00002270 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00002271 if( zRight ){
2272 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2273 }
drhc232aca2016-12-15 16:01:17 +00002274 returnSingleInt(v, db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00002275 break;
2276 }
drhf3603962012-09-07 16:46:59 +00002277
drh55e85ca2013-09-13 21:01:56 +00002278 /*
2279 ** PRAGMA soft_heap_limit
2280 ** PRAGMA soft_heap_limit = N
2281 **
drh51a74d42015-02-28 01:04:27 +00002282 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
2283 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
2284 ** specified and is a non-negative integer.
2285 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
2286 ** returns the same integer that would be returned by the
2287 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00002288 */
2289 case PragTyp_SOFT_HEAP_LIMIT: {
2290 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00002291 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00002292 sqlite3_soft_heap_limit64(N);
2293 }
drhc232aca2016-12-15 16:01:17 +00002294 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00002295 break;
2296 }
2297
drh03459612014-08-25 15:13:22 +00002298 /*
drh10c0e712019-04-25 18:15:38 +00002299 ** PRAGMA hard_heap_limit
2300 ** PRAGMA hard_heap_limit = N
2301 **
2302 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
2303 ** limit. The hard heap limit can be activated or lowered by this
2304 ** pragma, but not raised or deactivated. Only the
2305 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
2306 ** the hard heap limit. This allows an application to set a heap limit
2307 ** constraint that cannot be relaxed by an untrusted SQL script.
2308 */
2309 case PragTyp_HARD_HEAP_LIMIT: {
2310 sqlite3_int64 N;
2311 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
2312 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
2313 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
2314 }
drh31999c52019-11-14 17:46:32 +00002315 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
drh10c0e712019-04-25 18:15:38 +00002316 break;
2317 }
2318
2319 /*
drh03459612014-08-25 15:13:22 +00002320 ** PRAGMA threads
2321 ** PRAGMA threads = N
2322 **
2323 ** Configure the maximum number of worker threads. Return the new
2324 ** maximum, which might be less than requested.
2325 */
2326 case PragTyp_THREADS: {
2327 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00002328 if( zRight
drh03459612014-08-25 15:13:22 +00002329 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
2330 && N>=0
2331 ){
drh111544c2014-08-29 16:20:47 +00002332 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00002333 }
drhc232aca2016-12-15 16:01:17 +00002334 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00002335 break;
2336 }
2337
drh49a76a82020-03-31 20:57:06 +00002338 /*
2339 ** PRAGMA analysis_limit
2340 ** PRAGMA analysis_limit = N
2341 **
2342 ** Configure the maximum number of rows that ANALYZE will examine
2343 ** in each index that it looks at. Return the new limit.
2344 */
2345 case PragTyp_ANALYSIS_LIMIT: {
2346 sqlite3_int64 N;
2347 if( zRight
drhbc98f902021-10-14 17:30:32 +00002348 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
drh49a76a82020-03-31 20:57:06 +00002349 && N>=0
2350 ){
2351 db->nAnalysisLimit = (int)(N&0x7fffffff);
2352 }
drhbc98f902021-10-14 17:30:32 +00002353 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
drh49a76a82020-03-31 20:57:06 +00002354 break;
2355 }
2356
dougcurrie81c95ef2004-06-18 23:21:47 +00002357#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00002358 /*
2359 ** Report the current state of file logs for all databases
2360 */
drh9ccd8652013-09-13 16:36:46 +00002361 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00002362 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00002363 "unlocked", "shared", "reserved", "pending", "exclusive"
2364 };
2365 int i;
drh2d401ab2008-01-10 23:50:11 +00002366 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00002367 for(i=0; i<db->nDb; i++){
2368 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00002369 const char *zState = "unknown";
2370 int j;
drh69c33822016-08-18 14:33:11 +00002371 if( db->aDb[i].zDbSName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00002372 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00002373 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00002374 zState = "closed";
drh69c33822016-08-18 14:33:11 +00002375 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
drh9e33c2c2007-08-31 18:34:59 +00002376 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2377 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00002378 }
drh69c33822016-08-18 14:33:11 +00002379 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
drh89ac8c12004-06-09 14:17:20 +00002380 }
drh9ccd8652013-09-13 16:36:46 +00002381 break;
2382 }
drh89ac8c12004-06-09 14:17:20 +00002383#endif
2384
drhb48c0d52020-02-07 01:12:53 +00002385#if defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00002386 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
drh21e2cab2006-09-25 18:01:57 +00002387 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002388 sqlite3_activate_cerod(&zRight[6]);
2389 }
drh9ccd8652013-09-13 16:36:46 +00002390 }
2391 break;
drh21e2cab2006-09-25 18:01:57 +00002392#endif
drh3c4f2a42005-12-08 18:12:56 +00002393
drh9ccd8652013-09-13 16:36:46 +00002394 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00002395
dan9e1ab1a2017-01-05 19:32:48 +00002396 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
2397 ** purpose is to execute assert() statements to verify that if the
2398 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
2399 ** to the PRAGMA, the implementation has not added any OP_ResultRow
2400 ** instructions to the VM. */
2401 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
2402 sqlite3VdbeVerifyNoResultRow(v);
2403 }
2404
danielk1977e0048402004-06-15 16:51:01 +00002405pragma_out:
drh633e6d52008-07-28 19:34:53 +00002406 sqlite3DbFree(db, zLeft);
2407 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00002408}
drh2fcc1592016-12-15 20:59:03 +00002409#ifndef SQLITE_OMIT_VIRTUALTABLE
2410/*****************************************************************************
2411** Implementation of an eponymous virtual table that runs a pragma.
2412**
2413*/
2414typedef struct PragmaVtab PragmaVtab;
2415typedef struct PragmaVtabCursor PragmaVtabCursor;
2416struct PragmaVtab {
2417 sqlite3_vtab base; /* Base class. Must be first */
2418 sqlite3 *db; /* The database connection to which it belongs */
2419 const PragmaName *pName; /* Name of the pragma */
2420 u8 nHidden; /* Number of hidden columns */
2421 u8 iHidden; /* Index of the first hidden column */
2422};
2423struct PragmaVtabCursor {
2424 sqlite3_vtab_cursor base; /* Base class. Must be first */
2425 sqlite3_stmt *pPragma; /* The pragma statement to run */
2426 sqlite_int64 iRowid; /* Current rowid */
2427 char *azArg[2]; /* Value of the argument and schema */
2428};
2429
2430/*
2431** Pragma virtual table module xConnect method.
2432*/
2433static int pragmaVtabConnect(
2434 sqlite3 *db,
2435 void *pAux,
2436 int argc, const char *const*argv,
2437 sqlite3_vtab **ppVtab,
2438 char **pzErr
2439){
2440 const PragmaName *pPragma = (const PragmaName*)pAux;
2441 PragmaVtab *pTab = 0;
2442 int rc;
2443 int i, j;
2444 char cSep = '(';
2445 StrAccum acc;
2446 char zBuf[200];
2447
drh344a1bf2016-12-22 14:53:25 +00002448 UNUSED_PARAMETER(argc);
2449 UNUSED_PARAMETER(argv);
drh2fcc1592016-12-15 20:59:03 +00002450 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drh0cdbe1a2018-05-09 13:46:26 +00002451 sqlite3_str_appendall(&acc, "CREATE TABLE x");
drh2fcc1592016-12-15 20:59:03 +00002452 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
drh0cdbe1a2018-05-09 13:46:26 +00002453 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
drh2fcc1592016-12-15 20:59:03 +00002454 cSep = ',';
2455 }
drh9a63f092016-12-16 02:14:15 +00002456 if( i==0 ){
drh0cdbe1a2018-05-09 13:46:26 +00002457 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
drh9a63f092016-12-16 02:14:15 +00002458 i++;
2459 }
drh2fcc1592016-12-15 20:59:03 +00002460 j = 0;
2461 if( pPragma->mPragFlg & PragFlg_Result1 ){
drh0cdbe1a2018-05-09 13:46:26 +00002462 sqlite3_str_appendall(&acc, ",arg HIDDEN");
drh2fcc1592016-12-15 20:59:03 +00002463 j++;
2464 }
2465 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
drh0cdbe1a2018-05-09 13:46:26 +00002466 sqlite3_str_appendall(&acc, ",schema HIDDEN");
drh2fcc1592016-12-15 20:59:03 +00002467 j++;
2468 }
drh0cdbe1a2018-05-09 13:46:26 +00002469 sqlite3_str_append(&acc, ")", 1);
drh2fcc1592016-12-15 20:59:03 +00002470 sqlite3StrAccumFinish(&acc);
2471 assert( strlen(zBuf) < sizeof(zBuf)-1 );
2472 rc = sqlite3_declare_vtab(db, zBuf);
2473 if( rc==SQLITE_OK ){
2474 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
2475 if( pTab==0 ){
2476 rc = SQLITE_NOMEM;
2477 }else{
2478 memset(pTab, 0, sizeof(PragmaVtab));
2479 pTab->pName = pPragma;
2480 pTab->db = db;
2481 pTab->iHidden = i;
2482 pTab->nHidden = j;
2483 }
2484 }else{
2485 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2486 }
2487
2488 *ppVtab = (sqlite3_vtab*)pTab;
2489 return rc;
2490}
2491
2492/*
2493** Pragma virtual table module xDisconnect method.
2494*/
2495static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
2496 PragmaVtab *pTab = (PragmaVtab*)pVtab;
2497 sqlite3_free(pTab);
2498 return SQLITE_OK;
2499}
2500
2501/* Figure out the best index to use to search a pragma virtual table.
2502**
2503** There are not really any index choices. But we want to encourage the
2504** query planner to give == constraints on as many hidden parameters as
2505** possible, and especially on the first hidden parameter. So return a
2506** high cost if hidden parameters are unconstrained.
2507*/
2508static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2509 PragmaVtab *pTab = (PragmaVtab*)tab;
2510 const struct sqlite3_index_constraint *pConstraint;
2511 int i, j;
2512 int seen[2];
2513
drhae7045c2016-12-15 21:33:55 +00002514 pIdxInfo->estimatedCost = (double)1;
drh2fcc1592016-12-15 20:59:03 +00002515 if( pTab->nHidden==0 ){ return SQLITE_OK; }
2516 pConstraint = pIdxInfo->aConstraint;
2517 seen[0] = 0;
2518 seen[1] = 0;
2519 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2520 if( pConstraint->usable==0 ) continue;
2521 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2522 if( pConstraint->iColumn < pTab->iHidden ) continue;
2523 j = pConstraint->iColumn - pTab->iHidden;
2524 assert( j < 2 );
drhd7175eb2016-12-15 21:11:15 +00002525 seen[j] = i+1;
drh2fcc1592016-12-15 20:59:03 +00002526 }
2527 if( seen[0]==0 ){
2528 pIdxInfo->estimatedCost = (double)2147483647;
2529 pIdxInfo->estimatedRows = 2147483647;
2530 return SQLITE_OK;
2531 }
drhd7175eb2016-12-15 21:11:15 +00002532 j = seen[0]-1;
drh2fcc1592016-12-15 20:59:03 +00002533 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
2534 pIdxInfo->aConstraintUsage[j].omit = 1;
2535 if( seen[1]==0 ) return SQLITE_OK;
2536 pIdxInfo->estimatedCost = (double)20;
2537 pIdxInfo->estimatedRows = 20;
drhd7175eb2016-12-15 21:11:15 +00002538 j = seen[1]-1;
drh2fcc1592016-12-15 20:59:03 +00002539 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
2540 pIdxInfo->aConstraintUsage[j].omit = 1;
2541 return SQLITE_OK;
2542}
2543
2544/* Create a new cursor for the pragma virtual table */
2545static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
2546 PragmaVtabCursor *pCsr;
2547 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
2548 if( pCsr==0 ) return SQLITE_NOMEM;
2549 memset(pCsr, 0, sizeof(PragmaVtabCursor));
2550 pCsr->base.pVtab = pVtab;
2551 *ppCursor = &pCsr->base;
2552 return SQLITE_OK;
2553}
2554
2555/* Clear all content from pragma virtual table cursor. */
2556static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
2557 int i;
2558 sqlite3_finalize(pCsr->pPragma);
2559 pCsr->pPragma = 0;
2560 for(i=0; i<ArraySize(pCsr->azArg); i++){
2561 sqlite3_free(pCsr->azArg[i]);
2562 pCsr->azArg[i] = 0;
2563 }
2564}
2565
2566/* Close a pragma virtual table cursor */
2567static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
2568 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
2569 pragmaVtabCursorClear(pCsr);
drh9a63f092016-12-16 02:14:15 +00002570 sqlite3_free(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002571 return SQLITE_OK;
2572}
2573
2574/* Advance the pragma virtual table cursor to the next row */
2575static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
2576 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2577 int rc = SQLITE_OK;
2578
2579 /* Increment the xRowid value */
2580 pCsr->iRowid++;
drh9a63f092016-12-16 02:14:15 +00002581 assert( pCsr->pPragma );
2582 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
2583 rc = sqlite3_finalize(pCsr->pPragma);
2584 pCsr->pPragma = 0;
2585 pragmaVtabCursorClear(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002586 }
2587 return rc;
2588}
2589
2590/*
2591** Pragma virtual table module xFilter method.
2592*/
2593static int pragmaVtabFilter(
2594 sqlite3_vtab_cursor *pVtabCursor,
2595 int idxNum, const char *idxStr,
2596 int argc, sqlite3_value **argv
2597){
2598 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2599 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2600 int rc;
drhd8b72002016-12-16 04:20:27 +00002601 int i, j;
drh2fcc1592016-12-15 20:59:03 +00002602 StrAccum acc;
2603 char *zSql;
2604
drh344a1bf2016-12-22 14:53:25 +00002605 UNUSED_PARAMETER(idxNum);
2606 UNUSED_PARAMETER(idxStr);
drh2fcc1592016-12-15 20:59:03 +00002607 pragmaVtabCursorClear(pCsr);
drhd8b72002016-12-16 04:20:27 +00002608 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
2609 for(i=0; i<argc; i++, j++){
dand8ecefa2017-07-15 20:48:30 +00002610 const char *zText = (const char*)sqlite3_value_text(argv[i]);
drhd8b72002016-12-16 04:20:27 +00002611 assert( j<ArraySize(pCsr->azArg) );
dand8ecefa2017-07-15 20:48:30 +00002612 assert( pCsr->azArg[j]==0 );
2613 if( zText ){
2614 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
2615 if( pCsr->azArg[j]==0 ){
2616 return SQLITE_NOMEM;
2617 }
drh2fcc1592016-12-15 20:59:03 +00002618 }
2619 }
drhd7175eb2016-12-15 21:11:15 +00002620 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
drh0cdbe1a2018-05-09 13:46:26 +00002621 sqlite3_str_appendall(&acc, "PRAGMA ");
drh2fcc1592016-12-15 20:59:03 +00002622 if( pCsr->azArg[1] ){
drh0cdbe1a2018-05-09 13:46:26 +00002623 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
drh2fcc1592016-12-15 20:59:03 +00002624 }
drh0cdbe1a2018-05-09 13:46:26 +00002625 sqlite3_str_appendall(&acc, pTab->pName->zName);
drh2fcc1592016-12-15 20:59:03 +00002626 if( pCsr->azArg[0] ){
drh0cdbe1a2018-05-09 13:46:26 +00002627 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
drh2fcc1592016-12-15 20:59:03 +00002628 }
2629 zSql = sqlite3StrAccumFinish(&acc);
2630 if( zSql==0 ) return SQLITE_NOMEM;
2631 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
2632 sqlite3_free(zSql);
2633 if( rc!=SQLITE_OK ){
2634 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
2635 return rc;
2636 }
2637 return pragmaVtabNext(pVtabCursor);
2638}
2639
2640/*
2641** Pragma virtual table module xEof method.
2642*/
2643static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
2644 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2645 return (pCsr->pPragma==0);
2646}
2647
2648/* The xColumn method simply returns the corresponding column from
2649** the PRAGMA.
2650*/
2651static int pragmaVtabColumn(
2652 sqlite3_vtab_cursor *pVtabCursor,
2653 sqlite3_context *ctx,
2654 int i
2655){
2656 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2657 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2658 if( i<pTab->iHidden ){
2659 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
2660 }else{
2661 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
2662 }
2663 return SQLITE_OK;
2664}
2665
2666/*
2667** Pragma virtual table module xRowid method.
2668*/
2669static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
2670 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2671 *p = pCsr->iRowid;
2672 return SQLITE_OK;
2673}
2674
2675/* The pragma virtual table object */
2676static const sqlite3_module pragmaVtabModule = {
2677 0, /* iVersion */
2678 0, /* xCreate - create a table */
2679 pragmaVtabConnect, /* xConnect - connect to an existing table */
2680 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
2681 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
2682 0, /* xDestroy - Drop a table */
2683 pragmaVtabOpen, /* xOpen - open a cursor */
2684 pragmaVtabClose, /* xClose - close a cursor */
2685 pragmaVtabFilter, /* xFilter - configure scan constraints */
2686 pragmaVtabNext, /* xNext - advance a cursor */
2687 pragmaVtabEof, /* xEof */
2688 pragmaVtabColumn, /* xColumn - read data */
2689 pragmaVtabRowid, /* xRowid - read data */
2690 0, /* xUpdate - write data */
2691 0, /* xBegin - begin transaction */
2692 0, /* xSync - sync transaction */
2693 0, /* xCommit - commit transaction */
2694 0, /* xRollback - rollback transaction */
2695 0, /* xFindFunction - function overloading */
2696 0, /* xRename - rename the table */
2697 0, /* xSavepoint */
2698 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002699 0, /* xRollbackTo */
2700 0 /* xShadowName */
drh2fcc1592016-12-15 20:59:03 +00002701};
2702
2703/*
2704** Check to see if zTabName is really the name of a pragma. If it is,
2705** then register an eponymous virtual table for that pragma and return
2706** a pointer to the Module object for the new virtual table.
2707*/
2708Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
2709 const PragmaName *pName;
2710 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
2711 pName = pragmaLocate(zName+7);
2712 if( pName==0 ) return 0;
2713 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
2714 assert( sqlite3HashFind(&db->aModule, zName)==0 );
drhd7175eb2016-12-15 21:11:15 +00002715 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
drh2fcc1592016-12-15 20:59:03 +00002716}
2717
2718#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh13d70422004-11-13 15:59:14 +00002719
drh8bfdf722009-06-19 14:06:03 +00002720#endif /* SQLITE_OMIT_PRAGMA */