blob: a0aa123fc43cf82966ded9085c4f3b0a20ac7931 [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 ;
drhceff7612022-03-02 01:02:16 +0000317 if( showInternFuncs ) mask = 0xffffffff;
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: {
drh00946d72022-04-01 16:22:41 +0000811 int iLimit = 0, addr;
drhca5557f2007-05-04 18:30:40 +0000812 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: {
drh18a3a482022-09-02 00:36:16 +0000968 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
tpoindex9a09a3c2004-12-20 19:01:32 +0000969 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000970 returnSingleText(v, sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000971 }else{
drh78f82d12008-09-02 00:52:52 +0000972#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000973 if( zRight[0] ){
974 int res;
danielk1977fab11272008-09-16 14:38:02 +0000975 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
976 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000977 sqlite3ErrorMsg(pParse, "not a writable directory");
drh18a3a482022-09-02 00:36:16 +0000978 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
danielk1977861f7452008-06-05 11:39:11 +0000979 goto pragma_out;
980 }
drh268283b2005-01-08 15:44:25 +0000981 }
danielk1977b06a0b62008-06-26 10:54:12 +0000982 if( SQLITE_TEMP_STORE==0
983 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
984 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000985 ){
986 invalidateTempStorage(pParse);
987 }
drh17435752007-08-16 04:30:38 +0000988 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000989 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000990 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000991 }else{
drh268283b2005-01-08 15:44:25 +0000992 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000993 }
drh78f82d12008-09-02 00:52:52 +0000994#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000995 }
drh18a3a482022-09-02 00:36:16 +0000996 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
drh9ccd8652013-09-13 16:36:46 +0000997 break;
998 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000999
drhcc716452012-06-06 23:23:23 +00001000#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +00001001 /*
1002 ** PRAGMA data_store_directory
1003 ** PRAGMA data_store_directory = ""|"directory_name"
1004 **
1005 ** Return or set the local value of the data_store_directory flag. Changing
1006 ** the value sets a specific directory to be used for database files that
1007 ** were specified with a relative pathname. Setting to a null string reverts
1008 ** to the default database directory, which for database files specified with
1009 ** a relative path will probably be based on the current directory for the
1010 ** process. Database file specified with an absolute path are not impacted
1011 ** by this setting, regardless of its value.
1012 **
1013 */
drh9ccd8652013-09-13 16:36:46 +00001014 case PragTyp_DATA_STORE_DIRECTORY: {
drh18a3a482022-09-02 00:36:16 +00001015 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
mistachkina112d142012-03-14 00:44:01 +00001016 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +00001017 returnSingleText(v, sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +00001018 }else{
1019#ifndef SQLITE_OMIT_WSD
1020 if( zRight[0] ){
1021 int res;
1022 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1023 if( rc!=SQLITE_OK || res==0 ){
1024 sqlite3ErrorMsg(pParse, "not a writable directory");
drh18a3a482022-09-02 00:36:16 +00001025 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
mistachkina112d142012-03-14 00:44:01 +00001026 goto pragma_out;
1027 }
1028 }
1029 sqlite3_free(sqlite3_data_directory);
1030 if( zRight[0] ){
1031 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1032 }else{
1033 sqlite3_data_directory = 0;
1034 }
1035#endif /* SQLITE_OMIT_WSD */
1036 }
drh18a3a482022-09-02 00:36:16 +00001037 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
drh9ccd8652013-09-13 16:36:46 +00001038 break;
1039 }
drhcc716452012-06-06 23:23:23 +00001040#endif
mistachkina112d142012-03-14 00:44:01 +00001041
drhd2cb50b2009-01-09 21:41:17 +00001042#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +00001043 /*
drh9b0cf342015-11-12 14:57:19 +00001044 ** PRAGMA [schema.]lock_proxy_file
1045 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +00001046 **
1047 ** Return or set the value of the lock_proxy_file flag. Changing
1048 ** the value sets a specific file to be used for database access locks.
1049 **
1050 */
1051 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +00001052 if( !zRight ){
1053 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1054 char *proxy_file_path = NULL;
1055 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +00001056 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +00001057 &proxy_file_path);
drhc232aca2016-12-15 16:01:17 +00001058 returnSingleText(v, proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +00001059 }else{
1060 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1061 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1062 int res;
1063 if( zRight[0] ){
1064 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1065 zRight);
1066 } else {
1067 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1068 NULL);
1069 }
1070 if( res!=SQLITE_OK ){
1071 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1072 goto pragma_out;
1073 }
1074 }
drh9ccd8652013-09-13 16:36:46 +00001075 break;
1076 }
drhd2cb50b2009-01-09 21:41:17 +00001077#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +00001078
1079 /*
drh9b0cf342015-11-12 14:57:19 +00001080 ** PRAGMA [schema.]synchronous
drh6841b1c2016-02-03 19:20:15 +00001081 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
drhc11d4f92003-04-06 21:08:24 +00001082 **
1083 ** Return or set the local value of the synchronous flag. Changing
1084 ** the local value does not make changes to the disk file and the
1085 ** default value will be restored the next time the database is
1086 ** opened.
1087 */
drh9ccd8652013-09-13 16:36:46 +00001088 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +00001089 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +00001090 returnSingleInt(v, pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +00001091 }else{
danielk197791cf71b2004-06-26 06:37:06 +00001092 if( !db->autoCommit ){
1093 sqlite3ErrorMsg(pParse,
1094 "Safety level may not be changed inside a transaction");
drh7553c822017-03-15 14:04:03 +00001095 }else if( iDb!=1 ){
drhd99d2832015-04-17 15:58:33 +00001096 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
1097 if( iLevel==0 ) iLevel = 1;
1098 pDb->safety_level = iLevel;
drh50a1a5a2016-03-08 14:40:11 +00001099 pDb->bSyncSet = 1;
drhd3605a42013-08-17 15:42:29 +00001100 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001101 }
drhc11d4f92003-04-06 21:08:24 +00001102 }
drh9ccd8652013-09-13 16:36:46 +00001103 break;
1104 }
drh13d70422004-11-13 15:59:14 +00001105#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001106
drhbf216272005-02-26 18:10:44 +00001107#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001108 case PragTyp_FLAG: {
1109 if( zRight==0 ){
drhc232aca2016-12-15 16:01:17 +00001110 setPragmaResultColumnNames(v, pPragma);
1111 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001112 }else{
drhfd748c62018-10-30 16:25:35 +00001113 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001114 if( db->autoCommit==0 ){
1115 /* Foreign key support may not be enabled or disabled while not
1116 ** in auto-commit mode. */
1117 mask &= ~(SQLITE_ForeignKeys);
1118 }
drhd4530972014-09-09 14:47:53 +00001119#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001120 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001121 /* Do not allow non-admin users to modify the schema arbitrarily */
1122 mask &= ~(SQLITE_WriteSchema);
1123 }
1124#endif
drh9ccd8652013-09-13 16:36:46 +00001125
1126 if( sqlite3GetBoolean(zRight, 0) ){
drh2eeca202020-01-08 20:37:45 +00001127 db->flags |= mask;
drh9ccd8652013-09-13 16:36:46 +00001128 }else{
1129 db->flags &= ~mask;
1130 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
drh635e6a92021-10-08 16:39:33 +00001131 if( (mask & SQLITE_WriteSchema)!=0
1132 && sqlite3_stricmp(zRight, "reset")==0
1133 ){
drhbc98f902021-10-14 17:30:32 +00001134 /* IMP: R-60817-01178 If the argument is "RESET" then schema
1135 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
1136 ** in addition, the schema is reloaded. */
drh635e6a92021-10-08 16:39:33 +00001137 sqlite3ResetAllSchemasOfConnection(db);
1138 }
drh9ccd8652013-09-13 16:36:46 +00001139 }
1140
1141 /* Many of the flag-pragmas modify the code generated by the SQL
1142 ** compiler (eg. count_changes). So add an opcode to expire all
1143 ** compiled SQL statements after modifying a pragma value.
1144 */
drh01c736d2016-05-20 15:15:07 +00001145 sqlite3VdbeAddOp0(v, OP_Expire);
drh9ccd8652013-09-13 16:36:46 +00001146 setAllPagerFlags(db);
1147 }
1148 break;
1149 }
drhbf216272005-02-26 18:10:44 +00001150#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001151
drh13d70422004-11-13 15:59:14 +00001152#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001153 /*
1154 ** PRAGMA table_info(<table>)
1155 **
1156 ** Return a single row for each column of the named table. The columns of
1157 ** the returned data set are:
1158 **
1159 ** cid: Column id (numbered from left to right, starting at 0)
1160 ** name: Column name
1161 ** type: Column declaration type.
1162 ** notnull: True if 'NOT NULL' is part of column declaration
1163 ** dflt_value: The default value for the column, if any.
dan3e6ac162016-02-11 21:01:16 +00001164 ** pk: Non-zero for PK fields.
danielk197791cf71b2004-06-26 06:37:06 +00001165 */
drh9ccd8652013-09-13 16:36:46 +00001166 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001167 Table *pTab;
drha78d2c02020-07-04 20:29:56 +00001168 sqlite3CodeVerifyNamedSchema(pParse, zDb);
drh4d249e62016-06-10 22:49:01 +00001169 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001170 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001171 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001172 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001173 Column *pCol;
drh44156282013-10-23 22:23:03 +00001174 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drhd7dc0a32018-10-01 18:28:42 +00001175 pParse->nMem = 7;
danielk19774adee202004-05-08 08:23:19 +00001176 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001177 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
drhab3c5f22019-10-17 13:15:40 +00001178 int isHidden = 0;
drhf9751072021-10-07 13:40:29 +00001179 const Expr *pColExpr;
drhab3c5f22019-10-17 13:15:40 +00001180 if( pCol->colFlags & COLFLAG_NOINSERT ){
drh676fa252019-10-17 14:21:07 +00001181 if( pPragma->iArg==0 ){
1182 nHidden++;
1183 continue;
1184 }
drhab3c5f22019-10-17 13:15:40 +00001185 if( pCol->colFlags & COLFLAG_VIRTUAL ){
1186 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
drhc1431142019-10-17 17:54:05 +00001187 }else if( pCol->colFlags & COLFLAG_STORED ){
drhab3c5f22019-10-17 13:15:40 +00001188 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
drhc1431142019-10-17 17:54:05 +00001189 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
drhab3c5f22019-10-17 13:15:40 +00001190 isHidden = 1; /* HIDDEN */
1191 }
danielk1977034ca142007-06-26 10:38:54 +00001192 }
drh384b7fe2013-01-01 13:55:31 +00001193 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1194 k = 0;
1195 }else if( pPk==0 ){
1196 k = 1;
1197 }else{
drh1b678962015-04-15 07:19:27 +00001198 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001199 }
drhf9751072021-10-07 13:40:29 +00001200 pColExpr = sqlite3ColumnExpr(pTab,pCol);
1201 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
drh9d43db52021-10-07 14:19:32 +00001202 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)
1203 || isHidden>=2 );
drhd7dc0a32018-10-01 18:28:42 +00001204 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
drh076e85f2015-09-03 13:46:12 +00001205 i-nHidden,
drhcf9d36d2021-08-02 18:03:43 +00001206 pCol->zCnName,
drhd7564862016-03-22 20:05:09 +00001207 sqlite3ColumnType(pCol,""),
drh076e85f2015-09-03 13:46:12 +00001208 pCol->notNull ? 1 : 0,
drhf9751072021-10-07 13:40:29 +00001209 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
drhd7dc0a32018-10-01 18:28:42 +00001210 k,
1211 isHidden);
drhc11d4f92003-04-06 21:08:24 +00001212 }
1213 }
drh9ccd8652013-09-13 16:36:46 +00001214 }
1215 break;
drhc11d4f92003-04-06 21:08:24 +00001216
drh2e50f672021-09-21 17:26:23 +00001217 /*
1218 ** PRAGMA table_list
1219 **
1220 ** Return a single row for each table, virtual table, or view in the
1221 ** entire schema.
1222 **
1223 ** schema: Name of attached database hold this table
1224 ** name: Name of the table itself
1225 ** type: "table", "view", "virtual", "shadow"
1226 ** ncol: Number of columns
1227 ** wr: True for a WITHOUT ROWID table
1228 ** strict: True for a STRICT table
1229 */
1230 case PragTyp_TABLE_LIST: {
1231 int ii;
1232 pParse->nMem = 6;
1233 sqlite3CodeVerifyNamedSchema(pParse, zDb);
1234 for(ii=0; ii<db->nDb; ii++){
1235 HashElem *k;
1236 Hash *pHash;
drhdc88b402021-10-21 19:48:14 +00001237 int initNCol;
drh2e50f672021-09-21 17:26:23 +00001238 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
drhdc88b402021-10-21 19:48:14 +00001239
1240 /* Ensure that the Table.nCol field is initialized for all views
1241 ** and virtual tables. Each time we initialize a Table.nCol value
1242 ** for a table, that can potentially disrupt the hash table, so restart
1243 ** the initialization scan.
1244 */
drh2e50f672021-09-21 17:26:23 +00001245 pHash = &db->aDb[ii].pSchema->tblHash;
drhdc88b402021-10-21 19:48:14 +00001246 initNCol = sqliteHashCount(pHash);
1247 while( initNCol-- ){
1248 for(k=sqliteHashFirst(pHash); 1; k=sqliteHashNext(k) ){
1249 Table *pTab;
1250 if( k==0 ){ initNCol = 0; break; }
1251 pTab = sqliteHashData(k);
1252 if( pTab->nCol==0 ){
1253 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
1254 if( zSql ){
1255 sqlite3_stmt *pDummy = 0;
1256 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
1257 (void)sqlite3_finalize(pDummy);
1258 sqlite3DbFree(db, zSql);
1259 }
drh0c7d3d32022-01-24 16:47:12 +00001260 if( db->mallocFailed ){
1261 sqlite3ErrorMsg(db->pParse, "out of memory");
1262 db->pParse->rc = SQLITE_NOMEM_BKPT;
1263 }
drhdc88b402021-10-21 19:48:14 +00001264 pHash = &db->aDb[ii].pSchema->tblHash;
1265 break;
1266 }
1267 }
1268 }
1269
drh2e50f672021-09-21 17:26:23 +00001270 for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k) ){
1271 Table *pTab = sqliteHashData(k);
1272 const char *zType;
1273 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
1274 if( IsView(pTab) ){
1275 zType = "view";
1276 }else if( IsVirtual(pTab) ){
1277 zType = "virtual";
1278 }else if( pTab->tabFlags & TF_Shadow ){
1279 zType = "shadow";
1280 }else{
1281 zType = "table";
1282 }
1283 sqlite3VdbeMultiLoad(v, 1, "sssiii",
1284 db->aDb[ii].zDbSName,
drha4a871c2021-11-04 14:04:20 +00001285 sqlite3PreferredTableName(pTab->zName),
drh2e50f672021-09-21 17:26:23 +00001286 zType,
1287 pTab->nCol,
1288 (pTab->tabFlags & TF_WithoutRowid)!=0,
1289 (pTab->tabFlags & TF_Strict)!=0
1290 );
1291 }
1292 }
1293 }
1294 break;
1295
drh33bec3f2017-02-17 13:38:15 +00001296#ifdef SQLITE_DEBUG
drh3ef26152013-10-12 20:22:00 +00001297 case PragTyp_STATS: {
1298 Index *pIdx;
1299 HashElem *i;
drh33bec3f2017-02-17 13:38:15 +00001300 pParse->nMem = 5;
drh3ef26152013-10-12 20:22:00 +00001301 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001302 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1303 Table *pTab = sqliteHashData(i);
drh33bec3f2017-02-17 13:38:15 +00001304 sqlite3VdbeMultiLoad(v, 1, "ssiii",
drha4a871c2021-11-04 14:04:20 +00001305 sqlite3PreferredTableName(pTab->zName),
drh076e85f2015-09-03 13:46:12 +00001306 0,
drhd566c952016-02-25 21:19:03 +00001307 pTab->szTabRow,
drh33bec3f2017-02-17 13:38:15 +00001308 pTab->nRowLogEst,
1309 pTab->tabFlags);
drh3ef26152013-10-12 20:22:00 +00001310 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh40cf27c2017-07-07 16:00:53 +00001311 sqlite3VdbeMultiLoad(v, 2, "siiiX",
drh076e85f2015-09-03 13:46:12 +00001312 pIdx->zName,
drhd566c952016-02-25 21:19:03 +00001313 pIdx->szIdxRow,
drh33bec3f2017-02-17 13:38:15 +00001314 pIdx->aiRowLogEst[0],
1315 pIdx->hasStat1);
1316 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drh3ef26152013-10-12 20:22:00 +00001317 }
1318 }
1319 }
1320 break;
drh33bec3f2017-02-17 13:38:15 +00001321#endif
drh3ef26152013-10-12 20:22:00 +00001322
drh9ccd8652013-09-13 16:36:46 +00001323 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001324 Index *pIdx;
1325 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001326 pIdx = sqlite3FindIndex(db, zRight, zDb);
drh7b1904e2019-07-17 11:01:11 +00001327 if( pIdx==0 ){
1328 /* If there is no index named zRight, check to see if there is a
1329 ** WITHOUT ROWID table named zRight, and if there is, show the
1330 ** structure of the PRIMARY KEY index for that table. */
1331 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
1332 if( pTab && !HasRowid(pTab) ){
1333 pIdx = sqlite3PrimaryKeyIndex(pTab);
1334 }
1335 }
drhc11d4f92003-04-06 21:08:24 +00001336 if( pIdx ){
dan3c425482018-11-20 18:09:59 +00001337 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
drhc11d4f92003-04-06 21:08:24 +00001338 int i;
drh5e7028c2015-03-05 14:29:02 +00001339 int mx;
1340 if( pPragma->iArg ){
1341 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1342 mx = pIdx->nColumn;
1343 pParse->nMem = 6;
1344 }else{
1345 /* PRAGMA index_info (legacy version) */
1346 mx = pIdx->nKeyCol;
1347 pParse->nMem = 3;
1348 }
drhc11d4f92003-04-06 21:08:24 +00001349 pTab = pIdx->pTable;
dan3c425482018-11-20 18:09:59 +00001350 sqlite3CodeVerifySchema(pParse, iIdxDb);
drhc232aca2016-12-15 16:01:17 +00001351 assert( pParse->nMem<=pPragma->nPragCName );
drhc228be52015-01-31 02:00:01 +00001352 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001353 i16 cnum = pIdx->aiColumn[i];
drh40cf27c2017-07-07 16:00:53 +00001354 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
drhcf9d36d2021-08-02 18:03:43 +00001355 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
drh5e7028c2015-03-05 14:29:02 +00001356 if( pPragma->iArg ){
drh40cf27c2017-07-07 16:00:53 +00001357 sqlite3VdbeMultiLoad(v, 4, "isiX",
drh076e85f2015-09-03 13:46:12 +00001358 pIdx->aSortOrder[i],
1359 pIdx->azColl[i],
1360 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001361 }
1362 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001363 }
1364 }
drh9ccd8652013-09-13 16:36:46 +00001365 }
1366 break;
drhc11d4f92003-04-06 21:08:24 +00001367
drh9ccd8652013-09-13 16:36:46 +00001368 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001369 Index *pIdx;
1370 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001371 int i;
drh2783e4b2004-10-05 15:42:53 +00001372 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001373 if( pTab ){
dan3c425482018-11-20 18:09:59 +00001374 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhc228be52015-01-31 02:00:01 +00001375 pParse->nMem = 5;
dan3c425482018-11-20 18:09:59 +00001376 sqlite3CodeVerifySchema(pParse, iTabDb);
drh3ef26152013-10-12 20:22:00 +00001377 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001378 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001379 sqlite3VdbeMultiLoad(v, 1, "isisi",
1380 i,
1381 pIdx->zName,
1382 IsUniqueIndex(pIdx),
1383 azOrigin[pIdx->idxType],
1384 pIdx->pPartIdxWhere!=0);
drhc11d4f92003-04-06 21:08:24 +00001385 }
1386 }
drh9ccd8652013-09-13 16:36:46 +00001387 }
1388 break;
drhc11d4f92003-04-06 21:08:24 +00001389
drh9ccd8652013-09-13 16:36:46 +00001390 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001391 int i;
drh2d401ab2008-01-10 23:50:11 +00001392 pParse->nMem = 3;
drh13d70422004-11-13 15:59:14 +00001393 for(i=0; i<db->nDb; i++){
1394 if( db->aDb[i].pBt==0 ) continue;
drh69c33822016-08-18 14:33:11 +00001395 assert( db->aDb[i].zDbSName!=0 );
drh076e85f2015-09-03 13:46:12 +00001396 sqlite3VdbeMultiLoad(v, 1, "iss",
1397 i,
drh69c33822016-08-18 14:33:11 +00001398 db->aDb[i].zDbSName,
drh076e85f2015-09-03 13:46:12 +00001399 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh13d70422004-11-13 15:59:14 +00001400 }
drh9ccd8652013-09-13 16:36:46 +00001401 }
1402 break;
danielk197748af65a2005-02-09 03:20:37 +00001403
drh9ccd8652013-09-13 16:36:46 +00001404 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001405 int i = 0;
1406 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001407 pParse->nMem = 2;
danielk197748af65a2005-02-09 03:20:37 +00001408 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1409 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001410 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
danielk197748af65a2005-02-09 03:20:37 +00001411 }
drh9ccd8652013-09-13 16:36:46 +00001412 }
1413 break;
drhab53bb62017-07-07 15:43:22 +00001414
drhcc3f3d12019-08-17 15:27:58 +00001415#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
drhab53bb62017-07-07 15:43:22 +00001416 case PragTyp_FUNCTION_LIST: {
1417 int i;
1418 HashElem *j;
1419 FuncDef *p;
drh337ca512020-01-04 19:58:28 +00001420 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
drh79d5bc82020-01-04 01:43:02 +00001421 pParse->nMem = 6;
drhab53bb62017-07-07 15:43:22 +00001422 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
1423 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
drhf9751072021-10-07 13:40:29 +00001424 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
drh337ca512020-01-04 19:58:28 +00001425 pragmaFunclistLine(v, p, 1, showInternFunc);
drhab53bb62017-07-07 15:43:22 +00001426 }
1427 }
1428 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
1429 p = (FuncDef*)sqliteHashData(j);
drhf9751072021-10-07 13:40:29 +00001430 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
drh337ca512020-01-04 19:58:28 +00001431 pragmaFunclistLine(v, p, 0, showInternFunc);
drhab53bb62017-07-07 15:43:22 +00001432 }
1433 }
1434 break;
1435
1436#ifndef SQLITE_OMIT_VIRTUALTABLE
1437 case PragTyp_MODULE_LIST: {
1438 HashElem *j;
1439 pParse->nMem = 1;
1440 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
1441 Module *pMod = (Module*)sqliteHashData(j);
1442 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
drhab53bb62017-07-07 15:43:22 +00001443 }
1444 }
1445 break;
1446#endif /* SQLITE_OMIT_VIRTUALTABLE */
1447
drh8ae11aa2017-07-07 17:33:07 +00001448 case PragTyp_PRAGMA_LIST: {
1449 int i;
1450 for(i=0; i<ArraySize(aPragmaName); i++){
1451 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
drh8ae11aa2017-07-07 17:33:07 +00001452 }
1453 }
1454 break;
1455#endif /* SQLITE_INTROSPECTION_PRAGMAS */
drhab53bb62017-07-07 15:43:22 +00001456
drh13d70422004-11-13 15:59:14 +00001457#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1458
drhb7f91642004-10-31 02:22:47 +00001459#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001460 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001461 FKey *pFK;
1462 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001463 pTab = sqlite3FindTable(db, zRight, zDb);
drh78b2fa82021-10-07 12:11:20 +00001464 if( pTab && IsOrdinaryTable(pTab) ){
drhf38524d2021-08-02 16:41:57 +00001465 pFK = pTab->u.tab.pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001466 if( pFK ){
dan3c425482018-11-20 18:09:59 +00001467 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977742f9472004-06-16 12:02:43 +00001468 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001469 pParse->nMem = 8;
dan3c425482018-11-20 18:09:59 +00001470 sqlite3CodeVerifySchema(pParse, iTabDb);
danielk1977742f9472004-06-16 12:02:43 +00001471 while(pFK){
1472 int j;
1473 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001474 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1475 i,
1476 j,
1477 pFK->zTo,
drhcf9d36d2021-08-02 18:03:43 +00001478 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
drh076e85f2015-09-03 13:46:12 +00001479 pFK->aCol[j].zCol,
1480 actionName(pFK->aAction[1]), /* ON UPDATE */
1481 actionName(pFK->aAction[0]), /* ON DELETE */
1482 "NONE");
danielk1977742f9472004-06-16 12:02:43 +00001483 }
1484 ++i;
1485 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001486 }
drh78100cc2003-08-23 22:40:53 +00001487 }
1488 }
drh9ccd8652013-09-13 16:36:46 +00001489 }
1490 break;
drhb7f91642004-10-31 02:22:47 +00001491#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001492
drh6c5b9152012-12-17 16:46:37 +00001493#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001494#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001495 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001496 FKey *pFK; /* A foreign key constraint */
1497 Table *pTab; /* Child table contain "REFERENCES" keyword */
1498 Table *pParent; /* Parent table that child points to */
1499 Index *pIdx; /* Index in the parent table */
1500 int i; /* Loop counter: Foreign key number for pTab */
1501 int j; /* Loop counter: Field of the foreign key */
1502 HashElem *k; /* Loop counter: Next table in schema */
1503 int x; /* result variable */
1504 int regResult; /* 3 registers to hold a result row */
drh613028b2012-12-17 18:43:02 +00001505 int regRow; /* Registers to hold a row from pTab */
1506 int addrTop; /* Top of a loop checking foreign keys */
1507 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001508 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001509
drh613028b2012-12-17 18:43:02 +00001510 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001511 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001512 regRow = ++pParse->nMem;
drh613028b2012-12-17 18:43:02 +00001513 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1514 while( k ){
1515 if( zRight ){
1516 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1517 k = 0;
1518 }else{
1519 pTab = (Table*)sqliteHashData(k);
1520 k = sqliteHashNext(k);
1521 }
drh78b2fa82021-10-07 12:11:20 +00001522 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
drhec1650a2020-07-03 12:15:59 +00001523 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1524 zDb = db->aDb[iDb].zDbSName;
1525 sqlite3CodeVerifySchema(pParse, iDb);
1526 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001527 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drhec1650a2020-07-03 12:15:59 +00001528 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001529 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh78b2fa82021-10-07 12:11:20 +00001530 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:57 +00001531 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001532 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1533 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001534 pIdx = 0;
drhec1650a2020-07-03 12:15:59 +00001535 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001536 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001537 if( x==0 ){
1538 if( pIdx==0 ){
drhec1650a2020-07-03 12:15:59 +00001539 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
drh6c5b9152012-12-17 16:46:37 +00001540 }else{
drhec1650a2020-07-03 12:15:59 +00001541 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001542 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001543 }
1544 }else{
drh613028b2012-12-17 18:43:02 +00001545 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001546 break;
1547 }
drh6c5b9152012-12-17 16:46:37 +00001548 }
dan5e878302013-10-12 19:06:48 +00001549 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001550 if( pFK ) break;
1551 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001552 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh78b2fa82021-10-07 12:11:20 +00001553 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:57 +00001554 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001555 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001556 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001557 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001558 if( pParent ){
1559 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
drhd96e3822020-09-16 19:48:23 +00001560 assert( x==0 || db->mallocFailed );
dan5e878302013-10-12 19:06:48 +00001561 }
drhec4ccdb2018-12-29 02:26:59 +00001562 addrOk = sqlite3VdbeMakeLabel(pParse);
dan4bee5592017-04-17 18:42:33 +00001563
1564 /* Generate code to read the child key values into registers
1565 ** regRow..regRow+n. If any of the child key values are NULL, this
1566 ** row cannot cause an FK violation. Jump directly to addrOk in
1567 ** this case. */
drh4f16ff92021-07-07 16:48:24 +00001568 if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
dan4bee5592017-04-17 18:42:33 +00001569 for(j=0; j<pFK->nCol; j++){
1570 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
1571 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
1572 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001573 }
dan4bee5592017-04-17 18:42:33 +00001574
1575 /* Generate code to query the parent index for a matching parent
1576 ** key. If a match is found, jump to addrOk. */
1577 if( pIdx ){
drh36d2d092022-04-04 18:17:59 +00001578 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
dan4bee5592017-04-17 18:42:33 +00001579 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
drh36d2d092022-04-04 18:17:59 +00001580 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
dan4bee5592017-04-17 18:42:33 +00001581 VdbeCoverage(v);
1582 }else if( pParent ){
1583 int jmp = sqlite3VdbeCurrentAddr(v)+2;
1584 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
1585 sqlite3VdbeGoto(v, addrOk);
drhd96e3822020-09-16 19:48:23 +00001586 assert( pFK->nCol==1 || db->mallocFailed );
dan4bee5592017-04-17 18:42:33 +00001587 }
1588
1589 /* Generate code to report an FK violation to the caller. */
dan940464b2017-04-17 18:02:41 +00001590 if( HasRowid(pTab) ){
1591 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
1592 }else{
1593 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
1594 }
drh40cf27c2017-07-07 16:00:53 +00001595 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001596 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001597 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001598 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001599 }
drh688852a2014-02-17 22:40:43 +00001600 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001601 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001602 }
drh9ccd8652013-09-13 16:36:46 +00001603 }
1604 break;
dan09ff9e12013-03-11 11:49:03 +00001605#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001606#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1607
drh08652b52019-05-08 17:27:18 +00001608#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
drh55ef4d92005-08-14 01:20:37 +00001609 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1610 ** used will be case sensitive or not depending on the RHS.
1611 */
drh9ccd8652013-09-13 16:36:46 +00001612 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001613 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001614 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001615 }
drh9ccd8652013-09-13 16:36:46 +00001616 }
1617 break;
drh08652b52019-05-08 17:27:18 +00001618#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
drh55ef4d92005-08-14 01:20:37 +00001619
drh1dcdbc02007-01-27 02:24:54 +00001620#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1621# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1622#endif
1623
drhb7f91642004-10-31 02:22:47 +00001624#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh8b174f22017-02-22 15:11:36 +00001625 /* PRAGMA integrity_check
1626 ** PRAGMA integrity_check(N)
1627 ** PRAGMA quick_check
1628 ** PRAGMA quick_check(N)
1629 **
1630 ** Verify the integrity of the database.
1631 **
1632 ** The "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001633 ** integrity_check designed to detect most database corruption
drh8b174f22017-02-22 15:11:36 +00001634 ** without the overhead of cross-checking indexes. Quick_check
1635 ** is linear time wherease integrity_check is O(NlogN).
drh17d2d592020-07-23 00:45:06 +00001636 **
1637 ** The maximum nubmer of errors is 100 by default. A different default
1638 ** can be specified using a numeric parameter N.
1639 **
1640 ** Or, the parameter N can be the name of a table. In that case, only
1641 ** the one table named is verified. The freelist is only verified if
1642 ** the named table is "sqlite_schema" (or one of its aliases).
1643 **
1644 ** All schemas are checked by default. To check just a single
1645 ** schema, use the form:
1646 **
1647 ** PRAGMA schema.integrity_check;
danielk197741c58b72007-12-29 13:39:19 +00001648 */
drh9ccd8652013-09-13 16:36:46 +00001649 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001650 int i, j, addr, mxErr;
drh17d2d592020-07-23 00:45:06 +00001651 Table *pObjTab = 0; /* Check only this one table, if not NULL */
drhed717fe2003-06-15 23:42:24 +00001652
drhc5227312011-10-13 17:09:01 +00001653 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001654
dan5885e762012-07-16 10:06:12 +00001655 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1656 ** then iDb is set to the index of the database identified by <db>.
1657 ** In this case, the integrity of database iDb only is verified by
1658 ** the VDBE created below.
1659 **
1660 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1661 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1662 ** to -1 here, to indicate that the VDBE should verify the integrity
1663 ** of all attached databases. */
1664 assert( iDb>=0 );
1665 assert( iDb==0 || pId2->z );
1666 if( pId2->z==0 ) iDb = -1;
1667
drhed717fe2003-06-15 23:42:24 +00001668 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001669 pParse->nMem = 6;
drh1dcdbc02007-01-27 02:24:54 +00001670
1671 /* Set the maximum error count */
1672 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1673 if( zRight ){
drh17d2d592020-07-23 00:45:06 +00001674 if( sqlite3GetInt32(zRight, &mxErr) ){
1675 if( mxErr<=0 ){
1676 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1677 }
1678 }else{
1679 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
1680 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
drh1dcdbc02007-01-27 02:24:54 +00001681 }
1682 }
drh66accfc2017-02-22 18:04:42 +00001683 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001684
1685 /* Do an integrity check on each database file */
1686 for(i=0; i<db->nDb; i++){
drh9ecd7082017-09-10 01:06:05 +00001687 HashElem *x; /* For looping over tables in the schema */
1688 Hash *pTbls; /* Set of all tables in the schema */
1689 int *aRoot; /* Array of root page numbers of all btrees */
1690 int cnt = 0; /* Number of entries in aRoot[] */
1691 int mxIdx = 0; /* Maximum number of indexes for any table */
drhed717fe2003-06-15 23:42:24 +00001692
danielk197753c0f742005-03-29 03:10:59 +00001693 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001694 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001695
drh80242052004-06-09 00:48:12 +00001696 sqlite3CodeVerifySchema(pParse, i);
1697
drhed717fe2003-06-15 23:42:24 +00001698 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001699 **
drh98968b22016-03-15 22:00:39 +00001700 ** Begin by finding the root pages numbers
drh2d401ab2008-01-10 23:50:11 +00001701 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001702 */
dan5885e762012-07-16 10:06:12 +00001703 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001704 pTbls = &db->aDb[i].pSchema->tblHash;
drh98968b22016-03-15 22:00:39 +00001705 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh9ecd7082017-09-10 01:06:05 +00001706 Table *pTab = sqliteHashData(x); /* Current table */
1707 Index *pIdx; /* An index on pTab */
1708 int nIdx; /* Number of indexes on pTab */
drh17d2d592020-07-23 00:45:06 +00001709 if( pObjTab && pObjTab!=pTab ) continue;
drh98968b22016-03-15 22:00:39 +00001710 if( HasRowid(pTab) ) cnt++;
drhbb9b5f22016-03-19 00:35:02 +00001711 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
1712 if( nIdx>mxIdx ) mxIdx = nIdx;
drh98968b22016-03-15 22:00:39 +00001713 }
drh17d2d592020-07-23 00:45:06 +00001714 if( cnt==0 ) continue;
1715 if( pObjTab ) cnt++;
drh98968b22016-03-15 22:00:39 +00001716 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
1717 if( aRoot==0 ) break;
drh17d2d592020-07-23 00:45:06 +00001718 cnt = 0;
1719 if( pObjTab ) aRoot[++cnt] = 0;
1720 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh98968b22016-03-15 22:00:39 +00001721 Table *pTab = sqliteHashData(x);
1722 Index *pIdx;
drh17d2d592020-07-23 00:45:06 +00001723 if( pObjTab && pObjTab!=pTab ) continue;
drhb5c10632017-09-21 00:49:15 +00001724 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
drh79069752004-05-22 21:30:40 +00001725 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhb5c10632017-09-21 00:49:15 +00001726 aRoot[++cnt] = pIdx->tnum;
drh79069752004-05-22 21:30:40 +00001727 }
1728 }
drhb5c10632017-09-21 00:49:15 +00001729 aRoot[0] = cnt;
drh2d401ab2008-01-10 23:50:11 +00001730
1731 /* Make sure sufficient number of registers have been allocated */
drhbb9b5f22016-03-19 00:35:02 +00001732 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
drh3963e582017-07-15 20:33:19 +00001733 sqlite3ClearTempRegCache(pParse);
drh2d401ab2008-01-10 23:50:11 +00001734
1735 /* Do the b-tree integrity checks */
drh98968b22016-03-15 22:00:39 +00001736 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
drh4f21c4a2008-12-10 22:15:00 +00001737 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001738 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001739 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh69c33822016-08-18 14:33:11 +00001740 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
drh66a51672008-01-03 00:01:23 +00001741 P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:05 +00001742 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
1743 integrityCheckResultRow(v);
drh1dcdbc02007-01-27 02:24:54 +00001744 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001745
1746 /* Make sure all the indices are constructed correctly.
1747 */
drh8b174f22017-02-22 15:11:36 +00001748 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001749 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001750 Index *pIdx, *pPk;
drh16b03c02022-08-17 18:07:52 +00001751 Index *pPrior = 0; /* Previous index */
drhed717fe2003-06-15 23:42:24 +00001752 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001753 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001754 int r1 = -1;
drhdb6940a2022-10-10 19:38:01 +00001755 int bStrict; /* True for a STRICT table */
drh16b03c02022-08-17 18:07:52 +00001756 int r2; /* Previous key for WITHOUT ROWID tables */
drhdb6940a2022-10-10 19:38:01 +00001757 int mxCol; /* Maximum non-virtual column number */
drhed717fe2003-06-15 23:42:24 +00001758
drh99666212021-10-11 15:21:42 +00001759 if( !IsOrdinaryTable(pTab) ) continue;
drh17d2d592020-07-23 00:45:06 +00001760 if( pObjTab && pObjTab!=pTab ) continue;
drh16b03c02022-08-17 18:07:52 +00001761 if( isQuick || HasRowid(pTab) ){
1762 pPk = 0;
1763 r2 = 0;
1764 }else{
1765 pPk = sqlite3PrimaryKeyIndex(pTab);
1766 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
1767 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
1768 }
danfd261ec2015-10-22 20:54:33 +00001769 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001770 1, 0, &iDataCur, &iIdxCur);
drh9ecd7082017-09-10 01:06:05 +00001771 /* reg[7] counts the number of entries in the table.
1772 ** reg[8+i] counts the number of entries in the i-th index
1773 */
drh6fbe41a2013-10-30 20:22:55 +00001774 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001775 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001776 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001777 }
drhbb9b5f22016-03-19 00:35:02 +00001778 assert( pParse->nMem>=8+j );
1779 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
drh688852a2014-02-17 22:40:43 +00001780 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001781 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhdb6940a2022-10-10 19:38:01 +00001782
1783 /* Fetch the right-most column from the table. This will cause
1784 ** the entire record header to be parsed and sanity checked. It
drhc9ef12f2022-10-10 21:21:04 +00001785 ** will also prepopulate the cursor column cache that is used
1786 ** by the OP_IsType code, so it is a required step.
1787 */
drhdb6940a2022-10-10 19:38:01 +00001788 mxCol = pTab->nCol-1;
1789 while( mxCol>=0
1790 && ((pTab->aCol[mxCol].colFlags & COLFLAG_VIRTUAL)!=0
1791 || pTab->iPKey==mxCol) ) mxCol--;
1792 if( mxCol>=0 ){
1793 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, mxCol, 3);
drhe995d2c2022-10-13 12:47:33 +00001794 sqlite3VdbeTypeofColumn(v, 3);
drhdb6940a2022-10-10 19:38:01 +00001795 }
drhc9ef12f2022-10-10 21:21:04 +00001796
drh4011c442018-06-06 19:48:19 +00001797 if( !isQuick ){
drh16b03c02022-08-17 18:07:52 +00001798 if( pPk ){
1799 /* Verify WITHOUT ROWID keys are in ascending order */
1800 int a1;
1801 char *zErr;
1802 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
1803 VdbeCoverage(v);
1804 sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v);
1805 zErr = sqlite3MPrintf(db,
1806 "row not in PRIMARY KEY order for %s",
1807 pTab->zName);
1808 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1809 integrityCheckResultRow(v);
1810 sqlite3VdbeJumpHere(v, a1);
1811 sqlite3VdbeJumpHere(v, a1+1);
1812 for(j=0; j<pPk->nKeyCol; j++){
1813 sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
1814 }
1815 }
drh4011c442018-06-06 19:48:19 +00001816 }
drh49d77ee2022-10-10 18:25:05 +00001817 /* Verify datatypes for all columns:
1818 **
1819 ** (1) NOT NULL columns may not contain a NULL
1820 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
1821 ** (3) Datatype for TEXT columns in non-STRICT tables must be
1822 ** NULL, TEXT, or BLOB.
1823 ** (4) Datatype for numeric columns in non-STRICT tables must not
1824 ** be a TEXT value that can be losslessly converted to numeric.
1825 */
drh9e1209d2021-08-19 02:58:15 +00001826 bStrict = (pTab->tabFlags & TF_Strict)!=0;
drhcefc87f2014-08-01 01:40:33 +00001827 for(j=0; j<pTab->nCol; j++){
1828 char *zErr;
drh49d77ee2022-10-10 18:25:05 +00001829 Column *pCol = pTab->aCol + j; /* The column to be checked */
drhc9ef12f2022-10-10 21:21:04 +00001830 int labelError; /* Jump here to report an error */
1831 int labelOk; /* Jump here if all looks ok */
1832 int p1, p3, p4; /* Operands to the OP_IsType opcode */
1833 int doTypeCheck; /* Check datatypes (besides NOT NULL) */
drh49d77ee2022-10-10 18:25:05 +00001834
drhcefc87f2014-08-01 01:40:33 +00001835 if( j==pTab->iPKey ) continue;
drh49d77ee2022-10-10 18:25:05 +00001836 if( bStrict ){
1837 doTypeCheck = pCol->eCType>COLTYPE_ANY;
1838 }else{
1839 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
drhebd70ee2019-12-09 15:52:07 +00001840 }
drh49d77ee2022-10-10 18:25:05 +00001841 if( pCol->notNull==0 && !doTypeCheck ) continue;
drhc9ef12f2022-10-10 21:21:04 +00001842
1843 /* Compute the operands that will be needed for OP_IsType */
drhdb6940a2022-10-10 19:38:01 +00001844 p4 = SQLITE_NULL;
drh49d77ee2022-10-10 18:25:05 +00001845 if( pCol->colFlags & COLFLAG_VIRTUAL ){
1846 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1847 p1 = -1;
1848 p3 = 3;
1849 }else{
1850 if( pCol->iDflt ){
1851 sqlite3_value *pDfltValue = 0;
1852 sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db),
1853 pCol->affinity, &pDfltValue);
1854 if( pDfltValue ){
1855 p4 = sqlite3_value_type(pDfltValue);
1856 sqlite3ValueFree(pDfltValue);
1857 }
1858 }
1859 p1 = iDataCur;
1860 if( !HasRowid(pTab) ){
1861 testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
1862 p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j);
1863 }else{
1864 p3 = sqlite3TableColumnToStorage(pTab,j);
1865 testcase( p3!=j);
1866 }
1867 }
drhc9ef12f2022-10-10 21:21:04 +00001868
1869 labelError = sqlite3VdbeMakeLabel(pParse);
1870 labelOk = sqlite3VdbeMakeLabel(pParse);
drh9e1209d2021-08-19 02:58:15 +00001871 if( pCol->notNull ){
drh49d77ee2022-10-10 18:25:05 +00001872 /* (1) NOT NULL columns may not contain a NULL */
drhc9ef12f2022-10-10 21:21:04 +00001873 int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
drh49d77ee2022-10-10 18:25:05 +00001874 sqlite3VdbeChangeP5(v, 0x0f);
1875 VdbeCoverage(v);
drh9e1209d2021-08-19 02:58:15 +00001876 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1877 pCol->zCnName);
1878 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drhc9ef12f2022-10-10 21:21:04 +00001879 if( doTypeCheck ){
1880 sqlite3VdbeGoto(v, labelError);
1881 sqlite3VdbeJumpHere(v, jmp2);
drh71c770f2021-08-19 16:29:33 +00001882 }else{
drhc9ef12f2022-10-10 21:21:04 +00001883 /* VDBE byte code will fall thru */
drh71c770f2021-08-19 16:29:33 +00001884 }
drh9e1209d2021-08-19 02:58:15 +00001885 }
drh49d77ee2022-10-10 18:25:05 +00001886 if( bStrict && doTypeCheck ){
1887 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
1888 static unsigned char aStdTypeMask[] = {
1889 0x1f, /* ANY */
1890 0x18, /* BLOB */
1891 0x11, /* INT */
1892 0x11, /* INTEGER */
1893 0x13, /* REAL */
1894 0x14 /* TEXT */
1895 };
drhc9ef12f2022-10-10 21:21:04 +00001896 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
drh49d77ee2022-10-10 18:25:05 +00001897 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
1898 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
drh9e1209d2021-08-19 02:58:15 +00001899 VdbeCoverage(v);
1900 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
1901 sqlite3StdType[pCol->eCType-1],
1902 pTab->zName, pTab->aCol[j].zCnName);
1903 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drhc9ef12f2022-10-10 21:21:04 +00001904 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
drh49d77ee2022-10-10 18:25:05 +00001905 /* (3) Datatype for TEXT columns in non-STRICT tables must be
1906 ** NULL, TEXT, or BLOB. */
drhc9ef12f2022-10-10 21:21:04 +00001907 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
drh49d77ee2022-10-10 18:25:05 +00001908 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
1909 VdbeCoverage(v);
1910 zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s",
1911 pTab->zName, pTab->aCol[j].zCnName);
1912 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drhc9ef12f2022-10-10 21:21:04 +00001913 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
drh49d77ee2022-10-10 18:25:05 +00001914 /* (4) Datatype for numeric columns in non-STRICT tables must not
1915 ** be a TEXT value that can be converted to numeric. */
drhc9ef12f2022-10-10 21:21:04 +00001916 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
drh49d77ee2022-10-10 18:25:05 +00001917 sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
1918 VdbeCoverage(v);
1919 if( p1>=0 ){
1920 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1921 }
1922 sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC);
drhc9ef12f2022-10-10 21:21:04 +00001923 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
drh49d77ee2022-10-10 18:25:05 +00001924 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
1925 VdbeCoverage(v);
1926 zErr = sqlite3MPrintf(db, "TEXT value in %s.%s",
1927 pTab->zName, pTab->aCol[j].zCnName);
1928 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh49d77ee2022-10-10 18:25:05 +00001929 }
drhc9ef12f2022-10-10 21:21:04 +00001930 sqlite3VdbeResolveLabel(v, labelError);
1931 integrityCheckResultRow(v);
1932 sqlite3VdbeResolveLabel(v, labelOk);
drhcefc87f2014-08-01 01:40:33 +00001933 }
drh8a284dc2017-02-22 14:15:37 +00001934 /* Verify CHECK constraints */
1935 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
dan75f95582017-04-04 19:58:54 +00001936 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
1937 if( db->mallocFailed==0 ){
drhec4ccdb2018-12-29 02:26:59 +00001938 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
1939 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
dan75f95582017-04-04 19:58:54 +00001940 char *zErr;
1941 int k;
drh6e97f8e2017-07-20 13:17:08 +00001942 pParse->iSelfTab = iDataCur + 1;
dan75f95582017-04-04 19:58:54 +00001943 for(k=pCheck->nExpr-1; k>0; k--){
1944 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
1945 }
1946 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
1947 SQLITE_JUMPIFNULL);
1948 sqlite3VdbeResolveLabel(v, addrCkFault);
drh3e34eab2017-07-19 19:48:40 +00001949 pParse->iSelfTab = 0;
dan75f95582017-04-04 19:58:54 +00001950 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
1951 pTab->zName);
1952 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:05 +00001953 integrityCheckResultRow(v);
dan75f95582017-04-04 19:58:54 +00001954 sqlite3VdbeResolveLabel(v, addrCkOk);
drh8a284dc2017-02-22 14:15:37 +00001955 }
dan75f95582017-04-04 19:58:54 +00001956 sqlite3ExprListDelete(db, pCheck);
drhcefc87f2014-08-01 01:40:33 +00001957 }
drh226cef42017-09-09 20:38:49 +00001958 if( !isQuick ){ /* Omit the remaining tests for quick_check */
drh226cef42017-09-09 20:38:49 +00001959 /* Validate index entries for the current row */
1960 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1961 int jmp2, jmp3, jmp4, jmp5;
drhec4ccdb2018-12-29 02:26:59 +00001962 int ckUniq = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:49 +00001963 if( pPk==pIdx ) continue;
1964 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1965 pPrior, r1);
1966 pPrior = pIdx;
1967 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
1968 /* Verify that an index entry exists for the current table row */
1969 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
1970 pIdx->nColumn); VdbeCoverage(v);
1971 sqlite3VdbeLoadString(v, 3, "row ");
1972 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1973 sqlite3VdbeLoadString(v, 4, " missing from index ");
1974 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1975 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
1976 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh9ecd7082017-09-10 01:06:05 +00001977 jmp4 = integrityCheckResultRow(v);
drh226cef42017-09-09 20:38:49 +00001978 sqlite3VdbeJumpHere(v, jmp2);
1979 /* For UNIQUE indexes, verify that only one entry exists with the
1980 ** current key. The entry is unique if (1) any column is NULL
1981 ** or (2) the next entry has a different key */
1982 if( IsUniqueIndex(pIdx) ){
drhec4ccdb2018-12-29 02:26:59 +00001983 int uniqOk = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:49 +00001984 int jmp6;
1985 int kk;
1986 for(kk=0; kk<pIdx->nKeyCol; kk++){
1987 int iCol = pIdx->aiColumn[kk];
1988 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
1989 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
1990 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1991 VdbeCoverage(v);
1992 }
1993 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
1994 sqlite3VdbeGoto(v, uniqOk);
1995 sqlite3VdbeJumpHere(v, jmp6);
1996 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1997 pIdx->nKeyCol); VdbeCoverage(v);
1998 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1999 sqlite3VdbeGoto(v, jmp5);
2000 sqlite3VdbeResolveLabel(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00002001 }
drh226cef42017-09-09 20:38:49 +00002002 sqlite3VdbeJumpHere(v, jmp4);
2003 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhcefc87f2014-08-01 01:40:33 +00002004 }
drhed717fe2003-06-15 23:42:24 +00002005 }
drh688852a2014-02-17 22:40:43 +00002006 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00002007 sqlite3VdbeJumpHere(v, loopTop-1);
drh8b174f22017-02-22 15:11:36 +00002008 if( !isQuick ){
2009 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
2010 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
2011 if( pPk==pIdx ) continue;
drh8b174f22017-02-22 15:11:36 +00002012 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh66accfc2017-02-22 18:04:42 +00002013 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
drh8b174f22017-02-22 15:11:36 +00002014 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh9ecd7082017-09-10 01:06:05 +00002015 sqlite3VdbeLoadString(v, 4, pIdx->zName);
2016 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
2017 integrityCheckResultRow(v);
drh66accfc2017-02-22 18:04:42 +00002018 sqlite3VdbeJumpHere(v, addr);
drh8b174f22017-02-22 15:11:36 +00002019 }
drh16b03c02022-08-17 18:07:52 +00002020 if( pPk ){
2021 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
2022 }
drhed717fe2003-06-15 23:42:24 +00002023 }
2024 }
2025 }
drh2ce18652016-01-16 20:50:21 +00002026 {
2027 static const int iLn = VDBE_OFFSET_LINENO(2);
2028 static const VdbeOpList endCode[] = {
2029 { OP_AddImm, 1, 0, 0}, /* 0 */
drh66accfc2017-02-22 18:04:42 +00002030 { OP_IfNotZero, 1, 4, 0}, /* 1 */
drh2ce18652016-01-16 20:50:21 +00002031 { OP_String8, 0, 3, 0}, /* 2 */
drh1b325542016-02-03 01:55:44 +00002032 { OP_ResultRow, 3, 1, 0}, /* 3 */
drh74588ce2017-09-13 00:13:05 +00002033 { OP_Halt, 0, 0, 0}, /* 4 */
2034 { OP_String8, 0, 3, 0}, /* 5 */
2035 { OP_Goto, 0, 3, 0}, /* 6 */
drh2ce18652016-01-16 20:50:21 +00002036 };
2037 VdbeOp *aOp;
2038
2039 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
2040 if( aOp ){
drh66accfc2017-02-22 18:04:42 +00002041 aOp[0].p2 = 1-mxErr;
drh2ce18652016-01-16 20:50:21 +00002042 aOp[2].p4type = P4_STATIC;
2043 aOp[2].p4.z = "ok";
drh74588ce2017-09-13 00:13:05 +00002044 aOp[5].p4type = P4_STATIC;
2045 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
drh2ce18652016-01-16 20:50:21 +00002046 }
drh74588ce2017-09-13 00:13:05 +00002047 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
drh2ce18652016-01-16 20:50:21 +00002048 }
drh9ccd8652013-09-13 16:36:46 +00002049 }
2050 break;
drhb7f91642004-10-31 02:22:47 +00002051#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
2052
drh13d70422004-11-13 15:59:14 +00002053#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00002054 /*
2055 ** PRAGMA encoding
2056 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
2057 **
drh85b623f2007-12-13 21:54:09 +00002058 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00002059 ** database. If the database is not initialized, it is initialized now.
2060 **
2061 ** The second form of this pragma is a no-op if the main database file
2062 ** has not already been initialized. In this case it sets the default
2063 ** encoding that will be used for the main database file if a new file
2064 ** is created. If an existing main database file is opened, then the
2065 ** default text encoding for the existing database is used.
2066 **
2067 ** In all cases new databases created using the ATTACH command are
2068 ** created to use the same default text encoding as the main database. If
2069 ** the main database has not been initialized and/or created when ATTACH
2070 ** is executed, this is done before the ATTACH operation.
2071 **
2072 ** In the second form this pragma sets the text encoding to be used in
2073 ** new database files created using this database handle. It is only
2074 ** useful if invoked immediately after the main database i
2075 */
drh9ccd8652013-09-13 16:36:46 +00002076 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00002077 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00002078 char *zName;
2079 u8 enc;
2080 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00002081 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00002082 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
2083 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
2084 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00002085 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00002086 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00002087 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
2088 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00002089 { 0, 0 }
2090 };
drh0f7eb612006-08-08 13:51:43 +00002091 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00002092 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00002093 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00002094 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
2095 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
2096 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drhc232aca2016-12-15 16:01:17 +00002097 returnSingleText(v, encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00002098 }else{ /* "PRAGMA encoding = XXX" */
2099 /* Only change the value of sqlite.enc if the database handle is not
2100 ** initialized. If the main database exists, the new sqlite.enc value
2101 ** will be overwritten when the schema is next loaded. If it does not
2102 ** already exists, it will be created to use the new encoding value.
2103 */
dan0ea2d422020-03-05 18:04:09 +00002104 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
danielk19778e227872004-06-07 07:52:17 +00002105 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
2106 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh42a630b2020-03-05 16:13:24 +00002107 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
2108 SCHEMA_ENC(db) = enc;
2109 sqlite3SetTextEncoding(db, enc);
danielk19778e227872004-06-07 07:52:17 +00002110 break;
2111 }
2112 }
2113 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00002114 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00002115 }
2116 }
2117 }
drh9ccd8652013-09-13 16:36:46 +00002118 }
2119 break;
drh13d70422004-11-13 15:59:14 +00002120#endif /* SQLITE_OMIT_UTF16 */
2121
2122#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00002123 /*
drh9b0cf342015-11-12 14:57:19 +00002124 ** PRAGMA [schema.]schema_version
2125 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002126 **
drh9b0cf342015-11-12 14:57:19 +00002127 ** PRAGMA [schema.]user_version
2128 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002129 **
drhe459bd42016-03-16 20:05:57 +00002130 ** PRAGMA [schema.]freelist_count
2131 **
2132 ** PRAGMA [schema.]data_version
drh4ee09b42013-05-01 19:49:27 +00002133 **
drh9b0cf342015-11-12 14:57:19 +00002134 ** PRAGMA [schema.]application_id
2135 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00002136 **
danielk1977b92b70b2004-11-12 16:11:59 +00002137 ** The pragma's schema_version and user_version are used to set or get
2138 ** the value of the schema-version and user-version, respectively. Both
2139 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00002140 ** stored in the database header.
2141 **
2142 ** The schema-cookie is usually only manipulated internally by SQLite. It
2143 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00002144 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00002145 ** SQLite each time a query is executed to ensure that the internal cache
2146 ** of the schema used when compiling the SQL query matches the schema of
2147 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00002148 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2149 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00002150 ** crashes or database corruption. Use with caution!
2151 **
danielk1977b92b70b2004-11-12 16:11:59 +00002152 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00002153 ** applications for any purpose.
2154 */
drh9ccd8652013-09-13 16:36:46 +00002155 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00002156 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00002157 sqlite3VdbeUsesBtree(v, iDb);
drhc232aca2016-12-15 16:01:17 +00002158 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00002159 /* Write the specified cookie value */
2160 static const VdbeOpList setCookie[] = {
2161 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1861afc2016-02-01 21:48:34 +00002162 { OP_SetCookie, 0, 0, 0}, /* 1 */
danielk1977dae24952004-11-11 05:10:43 +00002163 };
drh2ce18652016-01-16 20:50:21 +00002164 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00002165 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
drh2ce18652016-01-16 20:50:21 +00002166 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
drhdad300d2016-01-18 00:20:26 +00002167 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00002168 aOp[0].p1 = iDb;
drh1861afc2016-02-01 21:48:34 +00002169 aOp[1].p1 = iDb;
2170 aOp[1].p2 = iCookie;
2171 aOp[1].p3 = sqlite3Atoi(zRight);
drhe3863b52020-07-01 16:19:14 +00002172 aOp[1].p5 = 1;
drh7e475e52022-11-12 17:17:01 +00002173 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
2174 /* Do not allow the use of PRAGMA schema_version=VALUE in defensive
2175 ** mode. Change the OP_SetCookie opcode into a no-op. */
2176 aOp[1].opcode = OP_Noop;
2177 }
danielk1977dae24952004-11-11 05:10:43 +00002178 }else{
2179 /* Read the specified cookie value */
2180 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00002181 { OP_Transaction, 0, 0, 0}, /* 0 */
2182 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00002183 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00002184 };
drh2ce18652016-01-16 20:50:21 +00002185 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00002186 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
drh2ce18652016-01-16 20:50:21 +00002187 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
drhdad300d2016-01-18 00:20:26 +00002188 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00002189 aOp[0].p1 = iDb;
2190 aOp[1].p1 = iDb;
2191 aOp[1].p3 = iCookie;
drhf71a3662016-03-16 20:44:45 +00002192 sqlite3VdbeReusable(v);
danielk1977dae24952004-11-11 05:10:43 +00002193 }
drh9ccd8652013-09-13 16:36:46 +00002194 }
2195 break;
drh13d70422004-11-13 15:59:14 +00002196#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00002197
shanehdc97a8c2010-02-23 20:08:35 +00002198#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2199 /*
2200 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00002201 **
drh71caabf2010-02-26 15:39:24 +00002202 ** Return the names of all compile-time options used in this build,
2203 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00002204 */
drh9ccd8652013-09-13 16:36:46 +00002205 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00002206 int i = 0;
2207 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00002208 pParse->nMem = 1;
shanehdc97a8c2010-02-23 20:08:35 +00002209 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00002210 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00002211 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2212 }
drhf71a3662016-03-16 20:44:45 +00002213 sqlite3VdbeReusable(v);
drh9ccd8652013-09-13 16:36:46 +00002214 }
2215 break;
shanehdc97a8c2010-02-23 20:08:35 +00002216#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2217
dan5cf53532010-05-01 16:40:20 +00002218#ifndef SQLITE_OMIT_WAL
2219 /*
drh9b0cf342015-11-12 14:57:19 +00002220 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00002221 **
2222 ** Checkpoint the database.
2223 */
drh9ccd8652013-09-13 16:36:46 +00002224 case PragTyp_WAL_CHECKPOINT: {
drh099b3852021-03-10 16:35:37 +00002225 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
dancdc1f042010-11-18 12:11:05 +00002226 int eMode = SQLITE_CHECKPOINT_PASSIVE;
2227 if( zRight ){
2228 if( sqlite3StrICmp(zRight, "full")==0 ){
2229 eMode = SQLITE_CHECKPOINT_FULL;
2230 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2231 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00002232 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
2233 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00002234 }
2235 }
dancdc1f042010-11-18 12:11:05 +00002236 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00002237 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00002238 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00002239 }
2240 break;
dan5a299f92010-05-03 11:05:08 +00002241
2242 /*
2243 ** PRAGMA wal_autocheckpoint
2244 ** PRAGMA wal_autocheckpoint = N
2245 **
2246 ** Configure a database connection to automatically checkpoint a database
2247 ** after accumulating N frames in the log. Or query for the current value
2248 ** of N.
2249 */
drh9ccd8652013-09-13 16:36:46 +00002250 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00002251 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00002252 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00002253 }
drhc232aca2016-12-15 16:01:17 +00002254 returnSingleInt(v,
drhb033d8b2010-05-03 13:37:30 +00002255 db->xWalCallback==sqlite3WalDefaultHook ?
2256 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00002257 }
2258 break;
dan5cf53532010-05-01 16:40:20 +00002259#endif
dan7c246102010-04-12 19:00:29 +00002260
drh09419b42011-11-16 19:29:17 +00002261 /*
2262 ** PRAGMA shrink_memory
2263 **
drh51a74d42015-02-28 01:04:27 +00002264 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
2265 ** connection on which it is invoked to free up as much memory as it
2266 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00002267 */
drh9ccd8652013-09-13 16:36:46 +00002268 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00002269 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00002270 break;
2271 }
drh09419b42011-11-16 19:29:17 +00002272
drhf3603962012-09-07 16:46:59 +00002273 /*
drh2ead47c2017-02-22 20:24:10 +00002274 ** PRAGMA optimize
drh1cfaf8e2017-03-02 14:17:21 +00002275 ** PRAGMA optimize(MASK)
drh2ead47c2017-02-22 20:24:10 +00002276 ** PRAGMA schema.optimize
drh1cfaf8e2017-03-02 14:17:21 +00002277 ** PRAGMA schema.optimize(MASK)
drh99d5b4c2017-02-18 22:52:40 +00002278 **
drh2ead47c2017-02-22 20:24:10 +00002279 ** Attempt to optimize the database. All schemas are optimized in the first
drh1cfaf8e2017-03-02 14:17:21 +00002280 ** two forms, and only the specified schema is optimized in the latter two.
drh2ead47c2017-02-22 20:24:10 +00002281 **
drhe1f1c082017-03-06 20:44:13 +00002282 ** The details of optimizations performed by this pragma are expected
drh2ead47c2017-02-22 20:24:10 +00002283 ** to change and improve over time. Applications should anticipate that
2284 ** this pragma will perform new optimizations in future releases.
2285 **
drh1cfaf8e2017-03-02 14:17:21 +00002286 ** The optional argument is a bitmask of optimizations to perform:
drh2ead47c2017-02-22 20:24:10 +00002287 **
drh1cfaf8e2017-03-02 14:17:21 +00002288 ** 0x0001 Debugging mode. Do not actually perform any optimizations
2289 ** but instead return one line of text for each optimization
2290 ** that would have been done. Off by default.
drh99d5b4c2017-02-18 22:52:40 +00002291 **
drh1cfaf8e2017-03-02 14:17:21 +00002292 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
2293 ** See below for additional information.
2294 **
2295 ** 0x0004 (Not yet implemented) Record usage and performance
2296 ** information from the current session in the
2297 ** database file so that it will be available to "optimize"
2298 ** pragmas run by future database connections.
2299 **
2300 ** 0x0008 (Not yet implemented) Create indexes that might have
2301 ** been helpful to recent queries
2302 **
drhe7c6f972017-07-15 20:25:22 +00002303 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
2304 ** of the optimizations listed above except Debug Mode, including new
drh59dbe3a2017-03-06 23:51:16 +00002305 ** optimizations that have not yet been invented. If new optimizations are
2306 ** ever added that should be off by default, those off-by-default
2307 ** optimizations will have bitmasks of 0x10000 or larger.
drh1cfaf8e2017-03-02 14:17:21 +00002308 **
2309 ** DETERMINATION OF WHEN TO RUN ANALYZE
2310 **
2311 ** In the current implementation, a table is analyzed if only if all of
drh2ead47c2017-02-22 20:24:10 +00002312 ** the following are true:
drh99d5b4c2017-02-18 22:52:40 +00002313 **
drh1cfaf8e2017-03-02 14:17:21 +00002314 ** (1) MASK bit 0x02 is set.
2315 **
2316 ** (2) The query planner used sqlite_stat1-style statistics for one or
drh99d5b4c2017-02-18 22:52:40 +00002317 ** more indexes of the table at some point during the lifetime of
2318 ** the current connection.
2319 **
drh1cfaf8e2017-03-02 14:17:21 +00002320 ** (3) One or more indexes of the table are currently unanalyzed OR
drh99d5b4c2017-02-18 22:52:40 +00002321 ** the number of rows in the table has increased by 25 times or more
2322 ** since the last time ANALYZE was run.
drh2ead47c2017-02-22 20:24:10 +00002323 **
2324 ** The rules for when tables are analyzed are likely to change in
2325 ** future releases.
drh72052a72017-02-17 16:26:34 +00002326 */
drh2ead47c2017-02-22 20:24:10 +00002327 case PragTyp_OPTIMIZE: {
drh99d5b4c2017-02-18 22:52:40 +00002328 int iDbLast; /* Loop termination point for the schema loop */
2329 int iTabCur; /* Cursor for a table whose size needs checking */
2330 HashElem *k; /* Loop over tables of a schema */
2331 Schema *pSchema; /* The current schema */
2332 Table *pTab; /* A table in the schema */
2333 Index *pIdx; /* An index of the table */
2334 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
2335 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
drh1cfaf8e2017-03-02 14:17:21 +00002336 u32 opMask; /* Mask of operations to perform */
drh4a54bb52017-02-18 15:58:52 +00002337
drh1cfaf8e2017-03-02 14:17:21 +00002338 if( zRight ){
2339 opMask = (u32)sqlite3Atoi(zRight);
2340 if( (opMask & 0x02)==0 ) break;
2341 }else{
drh59dbe3a2017-03-06 23:51:16 +00002342 opMask = 0xfffe;
drh1cfaf8e2017-03-02 14:17:21 +00002343 }
drh4a54bb52017-02-18 15:58:52 +00002344 iTabCur = pParse->nTab++;
2345 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
2346 if( iDb==1 ) continue;
2347 sqlite3CodeVerifySchema(pParse, iDb);
2348 pSchema = db->aDb[iDb].pSchema;
2349 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
2350 pTab = (Table*)sqliteHashData(k);
drh99d5b4c2017-02-18 22:52:40 +00002351
2352 /* If table pTab has not been used in a way that would benefit from
2353 ** having analysis statistics during the current session, then skip it.
2354 ** This also has the effect of skipping virtual tables and views */
drh4a54bb52017-02-18 15:58:52 +00002355 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
drh99d5b4c2017-02-18 22:52:40 +00002356
2357 /* Reanalyze if the table is 25 times larger than the last analysis */
drh4a54bb52017-02-18 15:58:52 +00002358 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
2359 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2360 if( !pIdx->hasStat1 ){
drh99d5b4c2017-02-18 22:52:40 +00002361 szThreshold = 0; /* Always analyze if any index lacks statistics */
drh4a54bb52017-02-18 15:58:52 +00002362 break;
2363 }
2364 }
2365 if( szThreshold ){
2366 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
2367 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
drh1cfaf8e2017-03-02 14:17:21 +00002368 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
drh4a54bb52017-02-18 15:58:52 +00002369 VdbeCoverage(v);
2370 }
2371 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
2372 db->aDb[iDb].zDbSName, pTab->zName);
drh1cfaf8e2017-03-02 14:17:21 +00002373 if( opMask & 0x01 ){
2374 int r1 = sqlite3GetTempReg(pParse);
2375 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
2376 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
2377 }else{
2378 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
2379 }
drh4a54bb52017-02-18 15:58:52 +00002380 }
2381 }
drhbce04142017-02-23 00:58:36 +00002382 sqlite3VdbeAddOp0(v, OP_Expire);
drh72052a72017-02-17 16:26:34 +00002383 break;
2384 }
2385
2386 /*
drhf3603962012-09-07 16:46:59 +00002387 ** PRAGMA busy_timeout
2388 ** PRAGMA busy_timeout = N
2389 **
2390 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00002391 ** if one is set. If no busy handler or a different busy handler is set
2392 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2393 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00002394 */
drhd49c3582013-09-13 19:00:06 +00002395 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00002396 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00002397 if( zRight ){
2398 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2399 }
drhc232aca2016-12-15 16:01:17 +00002400 returnSingleInt(v, db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00002401 break;
2402 }
drhf3603962012-09-07 16:46:59 +00002403
drh55e85ca2013-09-13 21:01:56 +00002404 /*
2405 ** PRAGMA soft_heap_limit
2406 ** PRAGMA soft_heap_limit = N
2407 **
drh51a74d42015-02-28 01:04:27 +00002408 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
2409 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
2410 ** specified and is a non-negative integer.
2411 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
2412 ** returns the same integer that would be returned by the
2413 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00002414 */
2415 case PragTyp_SOFT_HEAP_LIMIT: {
2416 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00002417 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00002418 sqlite3_soft_heap_limit64(N);
2419 }
drhc232aca2016-12-15 16:01:17 +00002420 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00002421 break;
2422 }
2423
drh03459612014-08-25 15:13:22 +00002424 /*
drh10c0e712019-04-25 18:15:38 +00002425 ** PRAGMA hard_heap_limit
2426 ** PRAGMA hard_heap_limit = N
2427 **
2428 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
2429 ** limit. The hard heap limit can be activated or lowered by this
2430 ** pragma, but not raised or deactivated. Only the
2431 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
2432 ** the hard heap limit. This allows an application to set a heap limit
2433 ** constraint that cannot be relaxed by an untrusted SQL script.
2434 */
2435 case PragTyp_HARD_HEAP_LIMIT: {
2436 sqlite3_int64 N;
2437 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
2438 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
2439 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
2440 }
drh31999c52019-11-14 17:46:32 +00002441 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
drh10c0e712019-04-25 18:15:38 +00002442 break;
2443 }
2444
2445 /*
drh03459612014-08-25 15:13:22 +00002446 ** PRAGMA threads
2447 ** PRAGMA threads = N
2448 **
2449 ** Configure the maximum number of worker threads. Return the new
2450 ** maximum, which might be less than requested.
2451 */
2452 case PragTyp_THREADS: {
2453 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00002454 if( zRight
drh03459612014-08-25 15:13:22 +00002455 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
2456 && N>=0
2457 ){
drh111544c2014-08-29 16:20:47 +00002458 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00002459 }
drhc232aca2016-12-15 16:01:17 +00002460 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00002461 break;
2462 }
2463
drh49a76a82020-03-31 20:57:06 +00002464 /*
2465 ** PRAGMA analysis_limit
2466 ** PRAGMA analysis_limit = N
2467 **
2468 ** Configure the maximum number of rows that ANALYZE will examine
2469 ** in each index that it looks at. Return the new limit.
2470 */
2471 case PragTyp_ANALYSIS_LIMIT: {
2472 sqlite3_int64 N;
2473 if( zRight
drhbc98f902021-10-14 17:30:32 +00002474 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
drh49a76a82020-03-31 20:57:06 +00002475 && N>=0
2476 ){
2477 db->nAnalysisLimit = (int)(N&0x7fffffff);
2478 }
drhbc98f902021-10-14 17:30:32 +00002479 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
drh49a76a82020-03-31 20:57:06 +00002480 break;
2481 }
2482
dougcurrie81c95ef2004-06-18 23:21:47 +00002483#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00002484 /*
2485 ** Report the current state of file logs for all databases
2486 */
drh9ccd8652013-09-13 16:36:46 +00002487 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00002488 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00002489 "unlocked", "shared", "reserved", "pending", "exclusive"
2490 };
2491 int i;
drh2d401ab2008-01-10 23:50:11 +00002492 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00002493 for(i=0; i<db->nDb; i++){
2494 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00002495 const char *zState = "unknown";
2496 int j;
drh69c33822016-08-18 14:33:11 +00002497 if( db->aDb[i].zDbSName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00002498 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00002499 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00002500 zState = "closed";
drh69c33822016-08-18 14:33:11 +00002501 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
drh9e33c2c2007-08-31 18:34:59 +00002502 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2503 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00002504 }
drh69c33822016-08-18 14:33:11 +00002505 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
drh89ac8c12004-06-09 14:17:20 +00002506 }
drh9ccd8652013-09-13 16:36:46 +00002507 break;
2508 }
drh89ac8c12004-06-09 14:17:20 +00002509#endif
2510
drhb48c0d52020-02-07 01:12:53 +00002511#if defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00002512 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
drh21e2cab2006-09-25 18:01:57 +00002513 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002514 sqlite3_activate_cerod(&zRight[6]);
2515 }
drh9ccd8652013-09-13 16:36:46 +00002516 }
2517 break;
drh21e2cab2006-09-25 18:01:57 +00002518#endif
drh3c4f2a42005-12-08 18:12:56 +00002519
drh9ccd8652013-09-13 16:36:46 +00002520 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00002521
dan9e1ab1a2017-01-05 19:32:48 +00002522 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
2523 ** purpose is to execute assert() statements to verify that if the
2524 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
2525 ** to the PRAGMA, the implementation has not added any OP_ResultRow
2526 ** instructions to the VM. */
2527 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
2528 sqlite3VdbeVerifyNoResultRow(v);
2529 }
2530
danielk1977e0048402004-06-15 16:51:01 +00002531pragma_out:
drh633e6d52008-07-28 19:34:53 +00002532 sqlite3DbFree(db, zLeft);
2533 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00002534}
drh2fcc1592016-12-15 20:59:03 +00002535#ifndef SQLITE_OMIT_VIRTUALTABLE
2536/*****************************************************************************
2537** Implementation of an eponymous virtual table that runs a pragma.
2538**
2539*/
2540typedef struct PragmaVtab PragmaVtab;
2541typedef struct PragmaVtabCursor PragmaVtabCursor;
2542struct PragmaVtab {
2543 sqlite3_vtab base; /* Base class. Must be first */
2544 sqlite3 *db; /* The database connection to which it belongs */
2545 const PragmaName *pName; /* Name of the pragma */
2546 u8 nHidden; /* Number of hidden columns */
2547 u8 iHidden; /* Index of the first hidden column */
2548};
2549struct PragmaVtabCursor {
2550 sqlite3_vtab_cursor base; /* Base class. Must be first */
2551 sqlite3_stmt *pPragma; /* The pragma statement to run */
2552 sqlite_int64 iRowid; /* Current rowid */
2553 char *azArg[2]; /* Value of the argument and schema */
2554};
2555
2556/*
2557** Pragma virtual table module xConnect method.
2558*/
2559static int pragmaVtabConnect(
2560 sqlite3 *db,
2561 void *pAux,
2562 int argc, const char *const*argv,
2563 sqlite3_vtab **ppVtab,
2564 char **pzErr
2565){
2566 const PragmaName *pPragma = (const PragmaName*)pAux;
2567 PragmaVtab *pTab = 0;
2568 int rc;
2569 int i, j;
2570 char cSep = '(';
2571 StrAccum acc;
2572 char zBuf[200];
2573
drh344a1bf2016-12-22 14:53:25 +00002574 UNUSED_PARAMETER(argc);
2575 UNUSED_PARAMETER(argv);
drh2fcc1592016-12-15 20:59:03 +00002576 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drh0cdbe1a2018-05-09 13:46:26 +00002577 sqlite3_str_appendall(&acc, "CREATE TABLE x");
drh2fcc1592016-12-15 20:59:03 +00002578 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
drh0cdbe1a2018-05-09 13:46:26 +00002579 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
drh2fcc1592016-12-15 20:59:03 +00002580 cSep = ',';
2581 }
drh9a63f092016-12-16 02:14:15 +00002582 if( i==0 ){
drh0cdbe1a2018-05-09 13:46:26 +00002583 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
drh9a63f092016-12-16 02:14:15 +00002584 i++;
2585 }
drh2fcc1592016-12-15 20:59:03 +00002586 j = 0;
2587 if( pPragma->mPragFlg & PragFlg_Result1 ){
drh0cdbe1a2018-05-09 13:46:26 +00002588 sqlite3_str_appendall(&acc, ",arg HIDDEN");
drh2fcc1592016-12-15 20:59:03 +00002589 j++;
2590 }
2591 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
drh0cdbe1a2018-05-09 13:46:26 +00002592 sqlite3_str_appendall(&acc, ",schema HIDDEN");
drh2fcc1592016-12-15 20:59:03 +00002593 j++;
2594 }
drh0cdbe1a2018-05-09 13:46:26 +00002595 sqlite3_str_append(&acc, ")", 1);
drh2fcc1592016-12-15 20:59:03 +00002596 sqlite3StrAccumFinish(&acc);
2597 assert( strlen(zBuf) < sizeof(zBuf)-1 );
2598 rc = sqlite3_declare_vtab(db, zBuf);
2599 if( rc==SQLITE_OK ){
2600 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
2601 if( pTab==0 ){
2602 rc = SQLITE_NOMEM;
2603 }else{
2604 memset(pTab, 0, sizeof(PragmaVtab));
2605 pTab->pName = pPragma;
2606 pTab->db = db;
2607 pTab->iHidden = i;
2608 pTab->nHidden = j;
2609 }
2610 }else{
2611 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2612 }
2613
2614 *ppVtab = (sqlite3_vtab*)pTab;
2615 return rc;
2616}
2617
2618/*
2619** Pragma virtual table module xDisconnect method.
2620*/
2621static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
2622 PragmaVtab *pTab = (PragmaVtab*)pVtab;
2623 sqlite3_free(pTab);
2624 return SQLITE_OK;
2625}
2626
2627/* Figure out the best index to use to search a pragma virtual table.
2628**
2629** There are not really any index choices. But we want to encourage the
2630** query planner to give == constraints on as many hidden parameters as
2631** possible, and especially on the first hidden parameter. So return a
2632** high cost if hidden parameters are unconstrained.
2633*/
2634static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2635 PragmaVtab *pTab = (PragmaVtab*)tab;
2636 const struct sqlite3_index_constraint *pConstraint;
2637 int i, j;
2638 int seen[2];
2639
drhae7045c2016-12-15 21:33:55 +00002640 pIdxInfo->estimatedCost = (double)1;
drh2fcc1592016-12-15 20:59:03 +00002641 if( pTab->nHidden==0 ){ return SQLITE_OK; }
2642 pConstraint = pIdxInfo->aConstraint;
2643 seen[0] = 0;
2644 seen[1] = 0;
2645 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2646 if( pConstraint->usable==0 ) continue;
2647 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2648 if( pConstraint->iColumn < pTab->iHidden ) continue;
2649 j = pConstraint->iColumn - pTab->iHidden;
2650 assert( j < 2 );
drhd7175eb2016-12-15 21:11:15 +00002651 seen[j] = i+1;
drh2fcc1592016-12-15 20:59:03 +00002652 }
2653 if( seen[0]==0 ){
2654 pIdxInfo->estimatedCost = (double)2147483647;
2655 pIdxInfo->estimatedRows = 2147483647;
2656 return SQLITE_OK;
2657 }
drhd7175eb2016-12-15 21:11:15 +00002658 j = seen[0]-1;
drh2fcc1592016-12-15 20:59:03 +00002659 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
2660 pIdxInfo->aConstraintUsage[j].omit = 1;
2661 if( seen[1]==0 ) return SQLITE_OK;
2662 pIdxInfo->estimatedCost = (double)20;
2663 pIdxInfo->estimatedRows = 20;
drhd7175eb2016-12-15 21:11:15 +00002664 j = seen[1]-1;
drh2fcc1592016-12-15 20:59:03 +00002665 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
2666 pIdxInfo->aConstraintUsage[j].omit = 1;
2667 return SQLITE_OK;
2668}
2669
2670/* Create a new cursor for the pragma virtual table */
2671static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
2672 PragmaVtabCursor *pCsr;
2673 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
2674 if( pCsr==0 ) return SQLITE_NOMEM;
2675 memset(pCsr, 0, sizeof(PragmaVtabCursor));
2676 pCsr->base.pVtab = pVtab;
2677 *ppCursor = &pCsr->base;
2678 return SQLITE_OK;
2679}
2680
2681/* Clear all content from pragma virtual table cursor. */
2682static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
2683 int i;
2684 sqlite3_finalize(pCsr->pPragma);
2685 pCsr->pPragma = 0;
2686 for(i=0; i<ArraySize(pCsr->azArg); i++){
2687 sqlite3_free(pCsr->azArg[i]);
2688 pCsr->azArg[i] = 0;
2689 }
2690}
2691
2692/* Close a pragma virtual table cursor */
2693static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
2694 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
2695 pragmaVtabCursorClear(pCsr);
drh9a63f092016-12-16 02:14:15 +00002696 sqlite3_free(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002697 return SQLITE_OK;
2698}
2699
2700/* Advance the pragma virtual table cursor to the next row */
2701static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
2702 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2703 int rc = SQLITE_OK;
2704
2705 /* Increment the xRowid value */
2706 pCsr->iRowid++;
drh9a63f092016-12-16 02:14:15 +00002707 assert( pCsr->pPragma );
2708 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
2709 rc = sqlite3_finalize(pCsr->pPragma);
2710 pCsr->pPragma = 0;
2711 pragmaVtabCursorClear(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002712 }
2713 return rc;
2714}
2715
2716/*
2717** Pragma virtual table module xFilter method.
2718*/
2719static int pragmaVtabFilter(
2720 sqlite3_vtab_cursor *pVtabCursor,
2721 int idxNum, const char *idxStr,
2722 int argc, sqlite3_value **argv
2723){
2724 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2725 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2726 int rc;
drhd8b72002016-12-16 04:20:27 +00002727 int i, j;
drh2fcc1592016-12-15 20:59:03 +00002728 StrAccum acc;
2729 char *zSql;
2730
drh344a1bf2016-12-22 14:53:25 +00002731 UNUSED_PARAMETER(idxNum);
2732 UNUSED_PARAMETER(idxStr);
drh2fcc1592016-12-15 20:59:03 +00002733 pragmaVtabCursorClear(pCsr);
drhd8b72002016-12-16 04:20:27 +00002734 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
2735 for(i=0; i<argc; i++, j++){
dand8ecefa2017-07-15 20:48:30 +00002736 const char *zText = (const char*)sqlite3_value_text(argv[i]);
drhd8b72002016-12-16 04:20:27 +00002737 assert( j<ArraySize(pCsr->azArg) );
dand8ecefa2017-07-15 20:48:30 +00002738 assert( pCsr->azArg[j]==0 );
2739 if( zText ){
2740 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
2741 if( pCsr->azArg[j]==0 ){
2742 return SQLITE_NOMEM;
2743 }
drh2fcc1592016-12-15 20:59:03 +00002744 }
2745 }
drhd7175eb2016-12-15 21:11:15 +00002746 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
drh0cdbe1a2018-05-09 13:46:26 +00002747 sqlite3_str_appendall(&acc, "PRAGMA ");
drh2fcc1592016-12-15 20:59:03 +00002748 if( pCsr->azArg[1] ){
drh0cdbe1a2018-05-09 13:46:26 +00002749 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
drh2fcc1592016-12-15 20:59:03 +00002750 }
drh0cdbe1a2018-05-09 13:46:26 +00002751 sqlite3_str_appendall(&acc, pTab->pName->zName);
drh2fcc1592016-12-15 20:59:03 +00002752 if( pCsr->azArg[0] ){
drh0cdbe1a2018-05-09 13:46:26 +00002753 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
drh2fcc1592016-12-15 20:59:03 +00002754 }
2755 zSql = sqlite3StrAccumFinish(&acc);
2756 if( zSql==0 ) return SQLITE_NOMEM;
2757 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
2758 sqlite3_free(zSql);
2759 if( rc!=SQLITE_OK ){
2760 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
2761 return rc;
2762 }
2763 return pragmaVtabNext(pVtabCursor);
2764}
2765
2766/*
2767** Pragma virtual table module xEof method.
2768*/
2769static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
2770 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2771 return (pCsr->pPragma==0);
2772}
2773
2774/* The xColumn method simply returns the corresponding column from
2775** the PRAGMA.
2776*/
2777static int pragmaVtabColumn(
2778 sqlite3_vtab_cursor *pVtabCursor,
2779 sqlite3_context *ctx,
2780 int i
2781){
2782 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2783 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2784 if( i<pTab->iHidden ){
2785 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
2786 }else{
2787 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
2788 }
2789 return SQLITE_OK;
2790}
2791
2792/*
2793** Pragma virtual table module xRowid method.
2794*/
2795static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
2796 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2797 *p = pCsr->iRowid;
2798 return SQLITE_OK;
2799}
2800
2801/* The pragma virtual table object */
2802static const sqlite3_module pragmaVtabModule = {
2803 0, /* iVersion */
2804 0, /* xCreate - create a table */
2805 pragmaVtabConnect, /* xConnect - connect to an existing table */
2806 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
2807 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
2808 0, /* xDestroy - Drop a table */
2809 pragmaVtabOpen, /* xOpen - open a cursor */
2810 pragmaVtabClose, /* xClose - close a cursor */
2811 pragmaVtabFilter, /* xFilter - configure scan constraints */
2812 pragmaVtabNext, /* xNext - advance a cursor */
2813 pragmaVtabEof, /* xEof */
2814 pragmaVtabColumn, /* xColumn - read data */
2815 pragmaVtabRowid, /* xRowid - read data */
2816 0, /* xUpdate - write data */
2817 0, /* xBegin - begin transaction */
2818 0, /* xSync - sync transaction */
2819 0, /* xCommit - commit transaction */
2820 0, /* xRollback - rollback transaction */
2821 0, /* xFindFunction - function overloading */
2822 0, /* xRename - rename the table */
2823 0, /* xSavepoint */
2824 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002825 0, /* xRollbackTo */
2826 0 /* xShadowName */
drh2fcc1592016-12-15 20:59:03 +00002827};
2828
2829/*
2830** Check to see if zTabName is really the name of a pragma. If it is,
2831** then register an eponymous virtual table for that pragma and return
2832** a pointer to the Module object for the new virtual table.
2833*/
2834Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
2835 const PragmaName *pName;
2836 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
2837 pName = pragmaLocate(zName+7);
2838 if( pName==0 ) return 0;
2839 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
2840 assert( sqlite3HashFind(&db->aModule, zName)==0 );
drhd7175eb2016-12-15 21:11:15 +00002841 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
drh2fcc1592016-12-15 20:59:03 +00002842}
2843
2844#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh13d70422004-11-13 15:59:14 +00002845
drh8bfdf722009-06-19 14:06:03 +00002846#endif /* SQLITE_OMIT_PRAGMA */