blob: 1dcd21400cb02348efd2527f73d641c95c120700 [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 ){
danielk1977983e2302008-07-08 07:35:51 +0000134 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000135 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
136 "from within a transaction");
137 return SQLITE_ERROR;
138 }
139 sqlite3BtreeClose(db->aDb[1].pBt);
140 db->aDb[1].pBt = 0;
drh81028a42012-05-15 18:28:27 +0000141 sqlite3ResetAllSchemasOfConnection(db);
drh90f5ecb2004-07-22 01:19:35 +0000142 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000143 return SQLITE_OK;
144}
drhbf216272005-02-26 18:10:44 +0000145#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000146
drhbf216272005-02-26 18:10:44 +0000147#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000148/*
149** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000150** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000151** or DEFAULT_TEMP_STORE pragmas.
152*/
153static int changeTempStorage(Parse *pParse, const char *zStorageType){
154 int ts = getTempStore(zStorageType);
155 sqlite3 *db = pParse->db;
156 if( db->temp_store==ts ) return SQLITE_OK;
157 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
158 return SQLITE_ERROR;
159 }
drh4f21c4a2008-12-10 22:15:00 +0000160 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000161 return SQLITE_OK;
162}
drhbf216272005-02-26 18:10:44 +0000163#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000164
165/*
drhc232aca2016-12-15 16:01:17 +0000166** Set result column names for a pragma.
drhb460e522015-09-03 03:29:51 +0000167*/
drhc232aca2016-12-15 16:01:17 +0000168static void setPragmaResultColumnNames(
drh2fcc1592016-12-15 20:59:03 +0000169 Vdbe *v, /* The query under construction */
170 const PragmaName *pPragma /* The pragma */
drhb460e522015-09-03 03:29:51 +0000171){
drhc232aca2016-12-15 16:01:17 +0000172 u8 n = pPragma->nPragCName;
173 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
174 if( n==0 ){
175 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
176 }else{
177 int i, j;
178 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
179 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
180 }
drhb460e522015-09-03 03:29:51 +0000181 }
182}
drhb460e522015-09-03 03:29:51 +0000183
184/*
drh90f5ecb2004-07-22 01:19:35 +0000185** Generate code to return a single integer value.
186*/
drhc232aca2016-12-15 16:01:17 +0000187static void returnSingleInt(Vdbe *v, i64 value){
drh7cc023c2015-09-03 04:28:25 +0000188 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
drh7cc023c2015-09-03 04:28:25 +0000189 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
190}
191
192/*
193** Generate code to return a single text value.
194*/
195static void returnSingleText(
196 Vdbe *v, /* Prepared statement under construction */
drh7cc023c2015-09-03 04:28:25 +0000197 const char *zValue /* Value to be returned */
198){
199 if( zValue ){
drh076e85f2015-09-03 13:46:12 +0000200 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
drh7cc023c2015-09-03 04:28:25 +0000201 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
202 }
drh90f5ecb2004-07-22 01:19:35 +0000203}
204
drhd3605a42013-08-17 15:42:29 +0000205
206/*
207** Set the safety_level and pager flags for pager iDb. Or if iDb<0
208** set these values for all pagers.
209*/
210#ifndef SQLITE_OMIT_PAGER_PRAGMAS
211static void setAllPagerFlags(sqlite3 *db){
212 if( db->autoCommit ){
213 Db *pDb = db->aDb;
214 int n = db->nDb;
215 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
216 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
217 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
218 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
219 == PAGER_FLAGS_MASK );
220 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
221 while( (n--) > 0 ){
222 if( pDb->pBt ){
223 sqlite3BtreeSetPagerFlags(pDb->pBt,
224 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
225 }
226 pDb++;
227 }
228 }
229}
drhfeb56e02013-08-23 17:33:46 +0000230#else
231# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29 +0000232#endif
233
234
drhd2cb50b2009-01-09 21:41:17 +0000235/*
236** Return a human-readable name for a constraint resolution action.
237*/
danba9108b2009-09-22 07:13:42 +0000238#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000239static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000240 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000241 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000242 case OE_SetNull: zName = "SET NULL"; break;
243 case OE_SetDflt: zName = "SET DEFAULT"; break;
244 case OE_Cascade: zName = "CASCADE"; break;
245 case OE_Restrict: zName = "RESTRICT"; break;
246 default: zName = "NO ACTION";
247 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000248 }
drhd2cb50b2009-01-09 21:41:17 +0000249 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000250}
danba9108b2009-09-22 07:13:42 +0000251#endif
danielk197750af3e12008-10-10 17:47:21 +0000252
dane04dc882010-04-20 18:53:15 +0000253
254/*
255** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
256** defined in pager.h. This function returns the associated lowercase
257** journal-mode name.
258*/
259const char *sqlite3JournalModename(int eMode){
260 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000261 "delete", "persist", "off", "truncate", "memory"
262#ifndef SQLITE_OMIT_WAL
263 , "wal"
264#endif
dane04dc882010-04-20 18:53:15 +0000265 };
266 assert( PAGER_JOURNALMODE_DELETE==0 );
267 assert( PAGER_JOURNALMODE_PERSIST==1 );
268 assert( PAGER_JOURNALMODE_OFF==2 );
269 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
270 assert( PAGER_JOURNALMODE_MEMORY==4 );
271 assert( PAGER_JOURNALMODE_WAL==5 );
272 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
273
274 if( eMode==ArraySize(azModeName) ) return 0;
275 return azModeName[eMode];
276}
277
drh87223182004-02-21 14:00:29 +0000278/*
drh2fcc1592016-12-15 20:59:03 +0000279** Locate a pragma in the aPragmaName[] array.
280*/
281static const PragmaName *pragmaLocate(const char *zName){
mistachkin2e525322017-02-01 22:43:08 +0000282 int upr, lwr, mid = 0, rc;
drh2fcc1592016-12-15 20:59:03 +0000283 lwr = 0;
284 upr = ArraySize(aPragmaName)-1;
285 while( lwr<=upr ){
286 mid = (lwr+upr)/2;
287 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
288 if( rc==0 ) break;
289 if( rc<0 ){
290 upr = mid - 1;
291 }else{
292 lwr = mid + 1;
293 }
294 }
295 return lwr>upr ? 0 : &aPragmaName[mid];
296}
297
298/*
drh66accfc2017-02-22 18:04:42 +0000299** Helper subroutine for PRAGMA integrity_check:
300**
drh9ecd7082017-09-10 01:06:05 +0000301** Generate code to output a single-column result row with a value of the
302** string held in register 3. Decrement the result count in register 1
303** and halt if the maximum number of result rows have been issued.
drh66accfc2017-02-22 18:04:42 +0000304*/
drh9ecd7082017-09-10 01:06:05 +0000305static int integrityCheckResultRow(Vdbe *v){
drh66accfc2017-02-22 18:04:42 +0000306 int addr;
drh9ecd7082017-09-10 01:06:05 +0000307 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh66accfc2017-02-22 18:04:42 +0000308 addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
309 VdbeCoverage(v);
drh9ecd7082017-09-10 01:06:05 +0000310 sqlite3VdbeAddOp0(v, OP_Halt);
drh66accfc2017-02-22 18:04:42 +0000311 return addr;
312}
313
314/*
drhc11d4f92003-04-06 21:08:24 +0000315** Process a pragma statement.
316**
317** Pragmas are of this form:
318**
drh9b0cf342015-11-12 14:57:19 +0000319** PRAGMA [schema.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000320**
321** The identifier might also be a string. The value is a string, and
322** identifier, or a number. If minusFlag is true, then the value is
323** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000324**
325** If the left side is "database.id" then pId1 is the database name
326** and pId2 is the id. If the left side is just "id" then pId1 is the
327** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000328*/
danielk197791cf71b2004-06-26 06:37:06 +0000329void sqlite3Pragma(
330 Parse *pParse,
drh9b0cf342015-11-12 14:57:19 +0000331 Token *pId1, /* First part of [schema.]id field */
332 Token *pId2, /* Second part of [schema.]id field, or NULL */
danielk197791cf71b2004-06-26 06:37:06 +0000333 Token *pValue, /* Token for <value>, or NULL */
334 int minusFlag /* True if a '-' sign preceded <value> */
335){
336 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
337 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
338 const char *zDb = 0; /* The database name */
339 Token *pId; /* Pointer to <id> token */
drh3fa97302012-02-22 16:58:36 +0000340 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000341 int iDb; /* Database index for <database> */
drh06fd5d62012-02-22 14:45:19 +0000342 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
343 sqlite3 *db = pParse->db; /* The database connection */
344 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000345 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drh2fcc1592016-12-15 20:59:03 +0000346 const PragmaName *pPragma; /* The pragma */
drh06fd5d62012-02-22 14:45:19 +0000347
drhc11d4f92003-04-06 21:08:24 +0000348 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000349 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000350 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000351
drh9b0cf342015-11-12 14:57:19 +0000352 /* Interpret the [schema.] part of the pragma statement. iDb is the
danielk197791cf71b2004-06-26 06:37:06 +0000353 ** index of the database this pragma is being applied to in db.aDb[]. */
354 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
355 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000356 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000357
danielk1977ddfb2f02006-02-17 12:25:14 +0000358 /* If the temp database has been explicitly named as part of the
359 ** pragma, make sure it is open.
360 */
361 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
362 return;
363 }
364
drh17435752007-08-16 04:30:38 +0000365 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000366 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000367 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000368 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000369 }else{
drh17435752007-08-16 04:30:38 +0000370 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000371 }
danielk197791cf71b2004-06-26 06:37:06 +0000372
drhd2cb50b2009-01-09 21:41:17 +0000373 assert( pId2 );
drh69c33822016-08-18 14:33:11 +0000374 zDb = pId2->n>0 ? pDb->zDbSName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000375 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000376 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000377 }
drh06fd5d62012-02-22 14:45:19 +0000378
379 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
380 ** connection. If it returns SQLITE_OK, then assume that the VFS
381 ** handled the pragma and generate a no-op prepared statement.
drh8dd7a6a2015-03-06 04:37:26 +0000382 **
383 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
384 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
385 ** object corresponding to the database file to which the pragma
386 ** statement refers.
387 **
388 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
389 ** file control is an array of pointers to strings (char**) in which the
390 ** second element of the array is the name of the pragma and the third
391 ** element is the argument to the pragma or NULL if the pragma has no
392 ** argument.
drh06fd5d62012-02-22 14:45:19 +0000393 */
drh3fa97302012-02-22 16:58:36 +0000394 aFcntl[0] = 0;
395 aFcntl[1] = zLeft;
396 aFcntl[2] = zRight;
397 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000398 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000399 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
400 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000401 sqlite3VdbeSetNumCols(v, 1);
402 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
403 returnSingleText(v, aFcntl[0]);
drh7cc023c2015-09-03 04:28:25 +0000404 sqlite3_free(aFcntl[0]);
drh9ccd8652013-09-13 16:36:46 +0000405 goto pragma_out;
406 }
407 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000408 if( aFcntl[0] ){
409 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
410 sqlite3_free(aFcntl[0]);
411 }
412 pParse->nErr++;
413 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000414 goto pragma_out;
415 }
416
417 /* Locate the pragma in the lookup table */
drh2fcc1592016-12-15 20:59:03 +0000418 pPragma = pragmaLocate(zLeft);
419 if( pPragma==0 ) goto pragma_out;
drh9ccd8652013-09-13 16:36:46 +0000420
drhf63936e2013-10-03 14:08:07 +0000421 /* Make sure the database schema is loaded if the pragma requires that */
drhc232aca2016-12-15 16:01:17 +0000422 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
drhf63936e2013-10-03 14:08:07 +0000423 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
424 }
425
drhc232aca2016-12-15 16:01:17 +0000426 /* Register the result column names for pragmas that return results */
dan9e1ab1a2017-01-05 19:32:48 +0000427 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
428 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
429 ){
drhc232aca2016-12-15 16:01:17 +0000430 setPragmaResultColumnNames(v, pPragma);
431 }
432
drh9ccd8652013-09-13 16:36:46 +0000433 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01 +0000434 switch( pPragma->ePragTyp ){
drh9ccd8652013-09-13 16:36:46 +0000435
drhe73c9142011-11-09 16:12:24 +0000436#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000437 /*
drh9b0cf342015-11-12 14:57:19 +0000438 ** PRAGMA [schema.]default_cache_size
439 ** PRAGMA [schema.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000440 **
441 ** The first form reports the current persistent setting for the
442 ** page cache size. The value returned is the maximum number of
443 ** pages in the page cache. The second form sets both the current
444 ** page cache size value and the persistent page cache size value
445 ** stored in the database file.
446 **
drh93791ea2010-04-26 17:36:35 +0000447 ** Older versions of SQLite would set the default cache size to a
448 ** negative number to indicate synchronous=OFF. These days, synchronous
449 ** is always on by default regardless of the sign of the default cache
450 ** size. But continue to take the absolute value of the default cache
451 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000452 */
drh9ccd8652013-09-13 16:36:46 +0000453 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09 +0000454 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000455 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000456 { OP_Transaction, 0, 0, 0}, /* 0 */
457 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000458 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000459 { OP_Integer, 0, 2, 0},
460 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000461 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000462 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000463 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000464 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000465 };
drh2ce18652016-01-16 20:50:21 +0000466 VdbeOp *aOp;
drhfb982642007-08-30 01:19:59 +0000467 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000468 if( !zRight ){
drh3c84ddf2008-01-09 02:15:38 +0000469 pParse->nMem += 2;
drhdad300d2016-01-18 00:20:26 +0000470 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
drh2ce18652016-01-16 20:50:21 +0000471 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
drhdad300d2016-01-18 00:20:26 +0000472 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000473 aOp[0].p1 = iDb;
474 aOp[1].p1 = iDb;
475 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
drhc11d4f92003-04-06 21:08:24 +0000476 }else{
drhd50ffc42011-03-08 02:38:28 +0000477 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000478 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh1861afc2016-02-01 21:48:34 +0000479 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
drh21206082011-04-04 18:22:02 +0000480 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000481 pDb->pSchema->cache_size = size;
482 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000483 }
drh9ccd8652013-09-13 16:36:46 +0000484 break;
485 }
drhe73c9142011-11-09 16:12:24 +0000486#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000487
drhe73c9142011-11-09 16:12:24 +0000488#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000489 /*
drh9b0cf342015-11-12 14:57:19 +0000490 ** PRAGMA [schema.]page_size
491 ** PRAGMA [schema.]page_size=N
drh90f5ecb2004-07-22 01:19:35 +0000492 **
493 ** The first form reports the current setting for the
494 ** database page size in bytes. The second form sets the
495 ** database page size value. The value can only be set if
496 ** the database has not yet been created.
497 */
drh9ccd8652013-09-13 16:36:46 +0000498 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000499 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000500 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000501 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000502 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drhc232aca2016-12-15 16:01:17 +0000503 returnSingleInt(v, size);
drh90f5ecb2004-07-22 01:19:35 +0000504 }else{
danielk1977992772c2007-08-30 10:07:38 +0000505 /* Malloc may fail when setting the page-size, as there is an internal
506 ** buffer that the pager module resizes using sqlite3_realloc().
507 */
drh60ac3f42010-11-23 18:59:27 +0000508 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000509 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
drh4a642b62016-02-05 01:55:27 +0000510 sqlite3OomFault(db);
danielk1977992772c2007-08-30 10:07:38 +0000511 }
drh90f5ecb2004-07-22 01:19:35 +0000512 }
drh9ccd8652013-09-13 16:36:46 +0000513 break;
514 }
danielk197741483462007-03-24 16:45:04 +0000515
516 /*
drh9b0cf342015-11-12 14:57:19 +0000517 ** PRAGMA [schema.]secure_delete
drha5907a82017-06-19 11:44:22 +0000518 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
drh5b47efa2010-02-12 18:18:39 +0000519 **
520 ** The first form reports the current setting for the
521 ** secure_delete flag. The second form changes the secure_delete
drha5907a82017-06-19 11:44:22 +0000522 ** flag setting and reports the new value.
drh5b47efa2010-02-12 18:18:39 +0000523 */
drh9ccd8652013-09-13 16:36:46 +0000524 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000525 Btree *pBt = pDb->pBt;
526 int b = -1;
527 assert( pBt!=0 );
528 if( zRight ){
drha5907a82017-06-19 11:44:22 +0000529 if( sqlite3_stricmp(zRight, "fast")==0 ){
530 b = 2;
531 }else{
532 b = sqlite3GetBoolean(zRight, 0);
533 }
drh5b47efa2010-02-12 18:18:39 +0000534 }
drhaf034ed2010-02-12 19:46:26 +0000535 if( pId2->n==0 && b>=0 ){
536 int ii;
537 for(ii=0; ii<db->nDb; ii++){
538 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
539 }
540 }
drh5b47efa2010-02-12 18:18:39 +0000541 b = sqlite3BtreeSecureDelete(pBt, b);
drhc232aca2016-12-15 16:01:17 +0000542 returnSingleInt(v, b);
drh9ccd8652013-09-13 16:36:46 +0000543 break;
544 }
drh5b47efa2010-02-12 18:18:39 +0000545
546 /*
drh9b0cf342015-11-12 14:57:19 +0000547 ** PRAGMA [schema.]max_page_count
548 ** PRAGMA [schema.]max_page_count=N
drh60ac3f42010-11-23 18:59:27 +0000549 **
550 ** The first form reports the current setting for the
551 ** maximum number of pages in the database file. The
552 ** second form attempts to change this setting. Both
553 ** forms return the current setting.
554 **
drhe73c9142011-11-09 16:12:24 +0000555 ** The absolute value of N is used. This is undocumented and might
556 ** change. The only purpose is to provide an easy way to test
557 ** the sqlite3AbsInt32() function.
558 **
drh9b0cf342015-11-12 14:57:19 +0000559 ** PRAGMA [schema.]page_count
danielk197759a93792008-05-15 17:48:20 +0000560 **
561 ** Return the number of pages in the specified database.
562 */
drh9ccd8652013-09-13 16:36:46 +0000563 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000564 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000565 sqlite3CodeVerifySchema(pParse, iDb);
566 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000567 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000568 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
569 }else{
drhe73c9142011-11-09 16:12:24 +0000570 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
571 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000572 }
danielk197759a93792008-05-15 17:48:20 +0000573 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
drh9ccd8652013-09-13 16:36:46 +0000574 break;
575 }
danielk197759a93792008-05-15 17:48:20 +0000576
577 /*
drh9b0cf342015-11-12 14:57:19 +0000578 ** PRAGMA [schema.]locking_mode
579 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
danielk197741483462007-03-24 16:45:04 +0000580 */
drh9ccd8652013-09-13 16:36:46 +0000581 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000582 const char *zRet = "normal";
583 int eMode = getLockingMode(zRight);
584
585 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
586 /* Simple "PRAGMA locking_mode;" statement. This is a query for
587 ** the current default locking mode (which may be different to
588 ** the locking-mode of the main database).
589 */
590 eMode = db->dfltLockMode;
591 }else{
592 Pager *pPager;
593 if( pId2->n==0 ){
594 /* This indicates that no database name was specified as part
595 ** of the PRAGMA command. In this case the locking-mode must be
596 ** set on all attached databases, as well as the main db file.
597 **
598 ** Also, the sqlite3.dfltLockMode variable is set so that
599 ** any subsequently attached databases also use the specified
600 ** locking mode.
601 */
602 int ii;
603 assert(pDb==&db->aDb[0]);
604 for(ii=2; ii<db->nDb; ii++){
605 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
606 sqlite3PagerLockingMode(pPager, eMode);
607 }
drh4f21c4a2008-12-10 22:15:00 +0000608 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000609 }
610 pPager = sqlite3BtreePager(pDb->pBt);
611 eMode = sqlite3PagerLockingMode(pPager, eMode);
612 }
613
drh9ccd8652013-09-13 16:36:46 +0000614 assert( eMode==PAGER_LOCKINGMODE_NORMAL
615 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000616 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
617 zRet = "exclusive";
618 }
drhc232aca2016-12-15 16:01:17 +0000619 returnSingleText(v, zRet);
drh9ccd8652013-09-13 16:36:46 +0000620 break;
621 }
drh3b020132008-04-17 17:02:01 +0000622
623 /*
drh9b0cf342015-11-12 14:57:19 +0000624 ** PRAGMA [schema.]journal_mode
625 ** PRAGMA [schema.]journal_mode =
drh3ebaee92010-05-06 21:37:22 +0000626 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000627 */
drh9ccd8652013-09-13 16:36:46 +0000628 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000629 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
630 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000631
drh3b020132008-04-17 17:02:01 +0000632 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000633 /* If there is no "=MODE" part of the pragma, do a query for the
634 ** current mode */
drh3b020132008-04-17 17:02:01 +0000635 eMode = PAGER_JOURNALMODE_QUERY;
636 }else{
dane04dc882010-04-20 18:53:15 +0000637 const char *zMode;
drhea678832008-12-10 19:26:22 +0000638 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000639 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000640 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
641 }
642 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000643 /* If the "=MODE" part does not match any known journal mode,
644 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000645 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000646 }
647 }
drhc6b2a0f2010-07-08 17:40:37 +0000648 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
649 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
650 iDb = 0;
651 pId2->n = 1;
652 }
653 for(ii=db->nDb-1; ii>=0; ii--){
654 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
655 sqlite3VdbeUsesBtree(v, ii);
656 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000657 }
drh3b020132008-04-17 17:02:01 +0000658 }
drh3b020132008-04-17 17:02:01 +0000659 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000660 break;
661 }
danielk1977b53e4962008-06-04 06:45:59 +0000662
663 /*
drh9b0cf342015-11-12 14:57:19 +0000664 ** PRAGMA [schema.]journal_size_limit
665 ** PRAGMA [schema.]journal_size_limit=N
danielk1977b53e4962008-06-04 06:45:59 +0000666 **
drha9e364f2009-01-13 20:14:15 +0000667 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000668 */
drh9ccd8652013-09-13 16:36:46 +0000669 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000670 Pager *pPager = sqlite3BtreePager(pDb->pBt);
671 i64 iLimit = -2;
672 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000673 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000674 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000675 }
676 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drhc232aca2016-12-15 16:01:17 +0000677 returnSingleInt(v, iLimit);
drh9ccd8652013-09-13 16:36:46 +0000678 break;
679 }
danielk1977b53e4962008-06-04 06:45:59 +0000680
drh13d70422004-11-13 15:59:14 +0000681#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000682
683 /*
drh9b0cf342015-11-12 14:57:19 +0000684 ** PRAGMA [schema.]auto_vacuum
685 ** PRAGMA [schema.]auto_vacuum=N
danielk1977951af802004-11-05 15:45:09 +0000686 **
drha9e364f2009-01-13 20:14:15 +0000687 ** Get or set the value of the database 'auto-vacuum' parameter.
688 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000689 */
690#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000691 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000692 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000693 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000694 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000695 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000696 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000697 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000698 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000699 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000700 /* Call SetAutoVacuum() to set initialize the internal auto and
701 ** incr-vacuum flags. This is required in case this connection
702 ** creates the database file. It is important that it is created
703 ** as an auto-vacuum capable db.
704 */
705 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
706 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
707 /* When setting the auto_vacuum mode to either "full" or
708 ** "incremental", write the value of meta[6] in the database
709 ** file. Before writing to meta[6], check that meta[3] indicates
710 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000711 */
drhb06a4ec2014-03-10 18:03:09 +0000712 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000713 static const VdbeOpList setMeta6[] = {
714 { OP_Transaction, 0, 1, 0}, /* 0 */
715 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
716 { OP_If, 1, 0, 0}, /* 2 */
717 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
drh1861afc2016-02-01 21:48:34 +0000718 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
drhf63936e2013-10-03 14:08:07 +0000719 };
drh2ce18652016-01-16 20:50:21 +0000720 VdbeOp *aOp;
721 int iAddr = sqlite3VdbeCurrentAddr(v);
drhdad300d2016-01-18 00:20:26 +0000722 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
drh2ce18652016-01-16 20:50:21 +0000723 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhdad300d2016-01-18 00:20:26 +0000724 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000725 aOp[0].p1 = iDb;
726 aOp[1].p1 = iDb;
727 aOp[2].p2 = iAddr+4;
drh1861afc2016-02-01 21:48:34 +0000728 aOp[4].p1 = iDb;
729 aOp[4].p3 = eAuto - 1;
drhf63936e2013-10-03 14:08:07 +0000730 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000731 }
danielk1977951af802004-11-05 15:45:09 +0000732 }
drh9ccd8652013-09-13 16:36:46 +0000733 break;
734 }
danielk1977951af802004-11-05 15:45:09 +0000735#endif
736
drhca5557f2007-05-04 18:30:40 +0000737 /*
drh9b0cf342015-11-12 14:57:19 +0000738 ** PRAGMA [schema.]incremental_vacuum(N)
drhca5557f2007-05-04 18:30:40 +0000739 **
740 ** Do N steps of incremental vacuuming on a database.
741 */
742#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000743 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000744 int iLimit, addr;
745 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
746 iLimit = 0x7fffffff;
747 }
748 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000749 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000750 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000751 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000752 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000753 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000754 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000755 break;
756 }
drhca5557f2007-05-04 18:30:40 +0000757#endif
758
drh13d70422004-11-13 15:59:14 +0000759#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000760 /*
drh9b0cf342015-11-12 14:57:19 +0000761 ** PRAGMA [schema.]cache_size
762 ** PRAGMA [schema.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000763 **
764 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000765 ** page cache size. The second form sets the local
766 ** page cache size value. If N is positive then that is the
767 ** number of pages in the cache. If N is negative, then the
768 ** number of pages is adjusted so that the cache uses -N kibibytes
769 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000770 */
drh9ccd8652013-09-13 16:36:46 +0000771 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000772 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000773 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000774 returnSingleInt(v, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000775 }else{
drh3b42abb2011-11-09 14:23:04 +0000776 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000777 pDb->pSchema->cache_size = size;
778 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000779 }
drh9ccd8652013-09-13 16:36:46 +0000780 break;
781 }
drhc11d4f92003-04-06 21:08:24 +0000782
783 /*
drh9b0cf342015-11-12 14:57:19 +0000784 ** PRAGMA [schema.]cache_spill
785 ** PRAGMA cache_spill=BOOLEAN
786 ** PRAGMA [schema.]cache_spill=N
787 **
788 ** The first form reports the current local setting for the
789 ** page cache spill size. The second form turns cache spill on
790 ** or off. When turnning cache spill on, the size is set to the
791 ** current cache_size. The third form sets a spill size that
792 ** may be different form the cache size.
793 ** If N is positive then that is the
794 ** number of pages in the cache. If N is negative, then the
795 ** number of pages is adjusted so that the cache uses -N kibibytes
796 ** of memory.
797 **
798 ** If the number of cache_spill pages is less then the number of
799 ** cache_size pages, no spilling occurs until the page count exceeds
800 ** the number of cache_size pages.
801 **
802 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
803 ** not just the schema specified.
804 */
805 case PragTyp_CACHE_SPILL: {
drh9b0cf342015-11-12 14:57:19 +0000806 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
807 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000808 returnSingleInt(v,
drh9b0cf342015-11-12 14:57:19 +0000809 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
810 sqlite3BtreeSetSpillSize(pDb->pBt,0));
811 }else{
drh4f9c8ec2015-11-12 15:47:48 +0000812 int size = 1;
drh9b0cf342015-11-12 14:57:19 +0000813 if( sqlite3GetInt32(zRight, &size) ){
814 sqlite3BtreeSetSpillSize(pDb->pBt, size);
815 }
drh4f9c8ec2015-11-12 15:47:48 +0000816 if( sqlite3GetBoolean(zRight, size!=0) ){
drh9b0cf342015-11-12 14:57:19 +0000817 db->flags |= SQLITE_CacheSpill;
818 }else{
drhd5b44d62018-12-06 17:06:02 +0000819 db->flags &= ~(u64)SQLITE_CacheSpill;
drh9b0cf342015-11-12 14:57:19 +0000820 }
drh4f9c8ec2015-11-12 15:47:48 +0000821 setAllPagerFlags(db);
drh9b0cf342015-11-12 14:57:19 +0000822 }
823 break;
824 }
825
826 /*
827 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000828 **
drh0d0614b2013-03-25 23:09:28 +0000829 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000830 ** used to limit the aggregate size of all memory mapped regions of the
831 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000832 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000833 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000834 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000835 **
drh0d0614b2013-03-25 23:09:28 +0000836 ** This value is advisory. The underlying VFS is free to memory map
837 ** as little or as much as it wants. Except, if N is set to 0 then the
838 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000839 */
drh9ccd8652013-09-13 16:36:46 +0000840 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000841 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000842#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000843 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000844 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000845 int ii;
drh9296c182014-07-23 13:40:49 +0000846 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000847 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
848 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000849 for(ii=db->nDb-1; ii>=0; ii--){
850 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000851 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000852 }
853 }
dan5d8a1372013-03-19 19:28:06 +0000854 }
drh9b4c59f2013-04-15 17:03:42 +0000855 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000856 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000857#else
dan3719f5f2013-05-23 10:13:18 +0000858 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000859 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000860#endif
dan3719f5f2013-05-23 10:13:18 +0000861 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000862 returnSingleInt(v, sz);
dan3719f5f2013-05-23 10:13:18 +0000863 }else if( rc!=SQLITE_NOTFOUND ){
864 pParse->nErr++;
865 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000866 }
drh9ccd8652013-09-13 16:36:46 +0000867 break;
868 }
dan5d8a1372013-03-19 19:28:06 +0000869
870 /*
drh90f5ecb2004-07-22 01:19:35 +0000871 ** PRAGMA temp_store
872 ** PRAGMA temp_store = "default"|"memory"|"file"
873 **
874 ** Return or set the local value of the temp_store flag. Changing
875 ** the local value does not make changes to the disk file and the default
876 ** value will be restored the next time the database is opened.
877 **
878 ** Note that it is possible for the library compile-time options to
879 ** override this setting
880 */
drh9ccd8652013-09-13 16:36:46 +0000881 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000882 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000883 returnSingleInt(v, db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000884 }else{
885 changeTempStorage(pParse, zRight);
886 }
drh9ccd8652013-09-13 16:36:46 +0000887 break;
888 }
drh90f5ecb2004-07-22 01:19:35 +0000889
890 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000891 ** PRAGMA temp_store_directory
892 ** PRAGMA temp_store_directory = ""|"directory_name"
893 **
894 ** Return or set the local value of the temp_store_directory flag. Changing
895 ** the value sets a specific directory to be used for temporary files.
896 ** Setting to a null string reverts to the default temporary directory search.
897 ** If temporary directory is changed, then invalidateTempStorage.
898 **
899 */
drh9ccd8652013-09-13 16:36:46 +0000900 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000901 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000902 returnSingleText(v, sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000903 }else{
drh78f82d12008-09-02 00:52:52 +0000904#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000905 if( zRight[0] ){
906 int res;
danielk1977fab11272008-09-16 14:38:02 +0000907 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
908 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000909 sqlite3ErrorMsg(pParse, "not a writable directory");
910 goto pragma_out;
911 }
drh268283b2005-01-08 15:44:25 +0000912 }
danielk1977b06a0b62008-06-26 10:54:12 +0000913 if( SQLITE_TEMP_STORE==0
914 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
915 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000916 ){
917 invalidateTempStorage(pParse);
918 }
drh17435752007-08-16 04:30:38 +0000919 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000920 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000921 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000922 }else{
drh268283b2005-01-08 15:44:25 +0000923 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000924 }
drh78f82d12008-09-02 00:52:52 +0000925#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000926 }
drh9ccd8652013-09-13 16:36:46 +0000927 break;
928 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000929
drhcc716452012-06-06 23:23:23 +0000930#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000931 /*
932 ** PRAGMA data_store_directory
933 ** PRAGMA data_store_directory = ""|"directory_name"
934 **
935 ** Return or set the local value of the data_store_directory flag. Changing
936 ** the value sets a specific directory to be used for database files that
937 ** were specified with a relative pathname. Setting to a null string reverts
938 ** to the default database directory, which for database files specified with
939 ** a relative path will probably be based on the current directory for the
940 ** process. Database file specified with an absolute path are not impacted
941 ** by this setting, regardless of its value.
942 **
943 */
drh9ccd8652013-09-13 16:36:46 +0000944 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +0000945 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000946 returnSingleText(v, sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +0000947 }else{
948#ifndef SQLITE_OMIT_WSD
949 if( zRight[0] ){
950 int res;
951 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
952 if( rc!=SQLITE_OK || res==0 ){
953 sqlite3ErrorMsg(pParse, "not a writable directory");
954 goto pragma_out;
955 }
956 }
957 sqlite3_free(sqlite3_data_directory);
958 if( zRight[0] ){
959 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
960 }else{
961 sqlite3_data_directory = 0;
962 }
963#endif /* SQLITE_OMIT_WSD */
964 }
drh9ccd8652013-09-13 16:36:46 +0000965 break;
966 }
drhcc716452012-06-06 23:23:23 +0000967#endif
mistachkina112d142012-03-14 00:44:01 +0000968
drhd2cb50b2009-01-09 21:41:17 +0000969#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000970 /*
drh9b0cf342015-11-12 14:57:19 +0000971 ** PRAGMA [schema.]lock_proxy_file
972 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +0000973 **
974 ** Return or set the value of the lock_proxy_file flag. Changing
975 ** the value sets a specific file to be used for database access locks.
976 **
977 */
978 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +0000979 if( !zRight ){
980 Pager *pPager = sqlite3BtreePager(pDb->pBt);
981 char *proxy_file_path = NULL;
982 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000983 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000984 &proxy_file_path);
drhc232aca2016-12-15 16:01:17 +0000985 returnSingleText(v, proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +0000986 }else{
987 Pager *pPager = sqlite3BtreePager(pDb->pBt);
988 sqlite3_file *pFile = sqlite3PagerFile(pPager);
989 int res;
990 if( zRight[0] ){
991 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
992 zRight);
993 } else {
994 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
995 NULL);
996 }
997 if( res!=SQLITE_OK ){
998 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
999 goto pragma_out;
1000 }
1001 }
drh9ccd8652013-09-13 16:36:46 +00001002 break;
1003 }
drhd2cb50b2009-01-09 21:41:17 +00001004#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +00001005
1006 /*
drh9b0cf342015-11-12 14:57:19 +00001007 ** PRAGMA [schema.]synchronous
drh6841b1c2016-02-03 19:20:15 +00001008 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
drhc11d4f92003-04-06 21:08:24 +00001009 **
1010 ** Return or set the local value of the synchronous flag. Changing
1011 ** the local value does not make changes to the disk file and the
1012 ** default value will be restored the next time the database is
1013 ** opened.
1014 */
drh9ccd8652013-09-13 16:36:46 +00001015 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +00001016 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +00001017 returnSingleInt(v, pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +00001018 }else{
danielk197791cf71b2004-06-26 06:37:06 +00001019 if( !db->autoCommit ){
1020 sqlite3ErrorMsg(pParse,
1021 "Safety level may not be changed inside a transaction");
drh7553c822017-03-15 14:04:03 +00001022 }else if( iDb!=1 ){
drhd99d2832015-04-17 15:58:33 +00001023 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
1024 if( iLevel==0 ) iLevel = 1;
1025 pDb->safety_level = iLevel;
drh50a1a5a2016-03-08 14:40:11 +00001026 pDb->bSyncSet = 1;
drhd3605a42013-08-17 15:42:29 +00001027 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001028 }
drhc11d4f92003-04-06 21:08:24 +00001029 }
drh9ccd8652013-09-13 16:36:46 +00001030 break;
1031 }
drh13d70422004-11-13 15:59:14 +00001032#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001033
drhbf216272005-02-26 18:10:44 +00001034#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001035 case PragTyp_FLAG: {
1036 if( zRight==0 ){
drhc232aca2016-12-15 16:01:17 +00001037 setPragmaResultColumnNames(v, pPragma);
1038 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001039 }else{
drhfd748c62018-10-30 16:25:35 +00001040 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001041 if( db->autoCommit==0 ){
1042 /* Foreign key support may not be enabled or disabled while not
1043 ** in auto-commit mode. */
1044 mask &= ~(SQLITE_ForeignKeys);
1045 }
drhd4530972014-09-09 14:47:53 +00001046#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001047 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001048 /* Do not allow non-admin users to modify the schema arbitrarily */
1049 mask &= ~(SQLITE_WriteSchema);
1050 }
1051#endif
drh9ccd8652013-09-13 16:36:46 +00001052
1053 if( sqlite3GetBoolean(zRight, 0) ){
1054 db->flags |= mask;
1055 }else{
1056 db->flags &= ~mask;
1057 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1058 }
1059
1060 /* Many of the flag-pragmas modify the code generated by the SQL
1061 ** compiler (eg. count_changes). So add an opcode to expire all
1062 ** compiled SQL statements after modifying a pragma value.
1063 */
drh01c736d2016-05-20 15:15:07 +00001064 sqlite3VdbeAddOp0(v, OP_Expire);
drh9ccd8652013-09-13 16:36:46 +00001065 setAllPagerFlags(db);
1066 }
1067 break;
1068 }
drhbf216272005-02-26 18:10:44 +00001069#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001070
drh13d70422004-11-13 15:59:14 +00001071#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001072 /*
1073 ** PRAGMA table_info(<table>)
1074 **
1075 ** Return a single row for each column of the named table. The columns of
1076 ** the returned data set are:
1077 **
1078 ** cid: Column id (numbered from left to right, starting at 0)
1079 ** name: Column name
1080 ** type: Column declaration type.
1081 ** notnull: True if 'NOT NULL' is part of column declaration
1082 ** dflt_value: The default value for the column, if any.
dan3e6ac162016-02-11 21:01:16 +00001083 ** pk: Non-zero for PK fields.
danielk197791cf71b2004-06-26 06:37:06 +00001084 */
drh9ccd8652013-09-13 16:36:46 +00001085 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001086 Table *pTab;
drh4d249e62016-06-10 22:49:01 +00001087 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001088 if( pTab ){
dan3c425482018-11-20 18:09:59 +00001089 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drh384b7fe2013-01-01 13:55:31 +00001090 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001091 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001092 Column *pCol;
drh44156282013-10-23 22:23:03 +00001093 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drhd7dc0a32018-10-01 18:28:42 +00001094 pParse->nMem = 7;
dan3c425482018-11-20 18:09:59 +00001095 sqlite3CodeVerifySchema(pParse, iTabDb);
danielk19774adee202004-05-08 08:23:19 +00001096 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001097 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
drhd7dc0a32018-10-01 18:28:42 +00001098 int isHidden = IsHiddenColumn(pCol);
1099 if( isHidden && pPragma->iArg==0 ){
danielk1977034ca142007-06-26 10:38:54 +00001100 nHidden++;
1101 continue;
1102 }
drh384b7fe2013-01-01 13:55:31 +00001103 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1104 k = 0;
1105 }else if( pPk==0 ){
1106 k = 1;
1107 }else{
drh1b678962015-04-15 07:19:27 +00001108 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001109 }
drh94fa9c42016-02-27 21:16:04 +00001110 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
drhd7dc0a32018-10-01 18:28:42 +00001111 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
drh076e85f2015-09-03 13:46:12 +00001112 i-nHidden,
drhd7564862016-03-22 20:05:09 +00001113 pCol->zName,
1114 sqlite3ColumnType(pCol,""),
drh076e85f2015-09-03 13:46:12 +00001115 pCol->notNull ? 1 : 0,
drh94fa9c42016-02-27 21:16:04 +00001116 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
drhd7dc0a32018-10-01 18:28:42 +00001117 k,
1118 isHidden);
drhc11d4f92003-04-06 21:08:24 +00001119 }
1120 }
drh9ccd8652013-09-13 16:36:46 +00001121 }
1122 break;
drhc11d4f92003-04-06 21:08:24 +00001123
drh33bec3f2017-02-17 13:38:15 +00001124#ifdef SQLITE_DEBUG
drh3ef26152013-10-12 20:22:00 +00001125 case PragTyp_STATS: {
1126 Index *pIdx;
1127 HashElem *i;
drh33bec3f2017-02-17 13:38:15 +00001128 pParse->nMem = 5;
drh3ef26152013-10-12 20:22:00 +00001129 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001130 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1131 Table *pTab = sqliteHashData(i);
drh33bec3f2017-02-17 13:38:15 +00001132 sqlite3VdbeMultiLoad(v, 1, "ssiii",
drh076e85f2015-09-03 13:46:12 +00001133 pTab->zName,
1134 0,
drhd566c952016-02-25 21:19:03 +00001135 pTab->szTabRow,
drh33bec3f2017-02-17 13:38:15 +00001136 pTab->nRowLogEst,
1137 pTab->tabFlags);
drh3ef26152013-10-12 20:22:00 +00001138 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh40cf27c2017-07-07 16:00:53 +00001139 sqlite3VdbeMultiLoad(v, 2, "siiiX",
drh076e85f2015-09-03 13:46:12 +00001140 pIdx->zName,
drhd566c952016-02-25 21:19:03 +00001141 pIdx->szIdxRow,
drh33bec3f2017-02-17 13:38:15 +00001142 pIdx->aiRowLogEst[0],
1143 pIdx->hasStat1);
1144 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drh3ef26152013-10-12 20:22:00 +00001145 }
1146 }
1147 }
1148 break;
drh33bec3f2017-02-17 13:38:15 +00001149#endif
drh3ef26152013-10-12 20:22:00 +00001150
drh9ccd8652013-09-13 16:36:46 +00001151 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001152 Index *pIdx;
1153 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001154 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001155 if( pIdx ){
dan3c425482018-11-20 18:09:59 +00001156 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
drhc11d4f92003-04-06 21:08:24 +00001157 int i;
drh5e7028c2015-03-05 14:29:02 +00001158 int mx;
1159 if( pPragma->iArg ){
1160 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1161 mx = pIdx->nColumn;
1162 pParse->nMem = 6;
1163 }else{
1164 /* PRAGMA index_info (legacy version) */
1165 mx = pIdx->nKeyCol;
1166 pParse->nMem = 3;
1167 }
drhc11d4f92003-04-06 21:08:24 +00001168 pTab = pIdx->pTable;
dan3c425482018-11-20 18:09:59 +00001169 sqlite3CodeVerifySchema(pParse, iIdxDb);
drhc232aca2016-12-15 16:01:17 +00001170 assert( pParse->nMem<=pPragma->nPragCName );
drhc228be52015-01-31 02:00:01 +00001171 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001172 i16 cnum = pIdx->aiColumn[i];
drh40cf27c2017-07-07 16:00:53 +00001173 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
drh076e85f2015-09-03 13:46:12 +00001174 cnum<0 ? 0 : pTab->aCol[cnum].zName);
drh5e7028c2015-03-05 14:29:02 +00001175 if( pPragma->iArg ){
drh40cf27c2017-07-07 16:00:53 +00001176 sqlite3VdbeMultiLoad(v, 4, "isiX",
drh076e85f2015-09-03 13:46:12 +00001177 pIdx->aSortOrder[i],
1178 pIdx->azColl[i],
1179 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001180 }
1181 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001182 }
1183 }
drh9ccd8652013-09-13 16:36:46 +00001184 }
1185 break;
drhc11d4f92003-04-06 21:08:24 +00001186
drh9ccd8652013-09-13 16:36:46 +00001187 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001188 Index *pIdx;
1189 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001190 int i;
drh2783e4b2004-10-05 15:42:53 +00001191 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001192 if( pTab ){
dan3c425482018-11-20 18:09:59 +00001193 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhc228be52015-01-31 02:00:01 +00001194 pParse->nMem = 5;
dan3c425482018-11-20 18:09:59 +00001195 sqlite3CodeVerifySchema(pParse, iTabDb);
drh3ef26152013-10-12 20:22:00 +00001196 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001197 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001198 sqlite3VdbeMultiLoad(v, 1, "isisi",
1199 i,
1200 pIdx->zName,
1201 IsUniqueIndex(pIdx),
1202 azOrigin[pIdx->idxType],
1203 pIdx->pPartIdxWhere!=0);
drhc11d4f92003-04-06 21:08:24 +00001204 }
1205 }
drh9ccd8652013-09-13 16:36:46 +00001206 }
1207 break;
drhc11d4f92003-04-06 21:08:24 +00001208
drh9ccd8652013-09-13 16:36:46 +00001209 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001210 int i;
drh2d401ab2008-01-10 23:50:11 +00001211 pParse->nMem = 3;
drh13d70422004-11-13 15:59:14 +00001212 for(i=0; i<db->nDb; i++){
1213 if( db->aDb[i].pBt==0 ) continue;
drh69c33822016-08-18 14:33:11 +00001214 assert( db->aDb[i].zDbSName!=0 );
drh076e85f2015-09-03 13:46:12 +00001215 sqlite3VdbeMultiLoad(v, 1, "iss",
1216 i,
drh69c33822016-08-18 14:33:11 +00001217 db->aDb[i].zDbSName,
drh076e85f2015-09-03 13:46:12 +00001218 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh13d70422004-11-13 15:59:14 +00001219 }
drh9ccd8652013-09-13 16:36:46 +00001220 }
1221 break;
danielk197748af65a2005-02-09 03:20:37 +00001222
drh9ccd8652013-09-13 16:36:46 +00001223 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001224 int i = 0;
1225 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001226 pParse->nMem = 2;
danielk197748af65a2005-02-09 03:20:37 +00001227 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1228 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001229 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
danielk197748af65a2005-02-09 03:20:37 +00001230 }
drh9ccd8652013-09-13 16:36:46 +00001231 }
1232 break;
drhab53bb62017-07-07 15:43:22 +00001233
drh8ae11aa2017-07-07 17:33:07 +00001234#ifdef SQLITE_INTROSPECTION_PRAGMAS
drhab53bb62017-07-07 15:43:22 +00001235 case PragTyp_FUNCTION_LIST: {
1236 int i;
1237 HashElem *j;
1238 FuncDef *p;
1239 pParse->nMem = 2;
1240 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
1241 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
drheea8eb62018-11-26 18:09:15 +00001242 if( p->funcFlags & SQLITE_FUNC_INTERNAL ) continue;
drhab53bb62017-07-07 15:43:22 +00001243 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
drhab53bb62017-07-07 15:43:22 +00001244 }
1245 }
1246 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
1247 p = (FuncDef*)sqliteHashData(j);
1248 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
drhab53bb62017-07-07 15:43:22 +00001249 }
1250 }
1251 break;
1252
1253#ifndef SQLITE_OMIT_VIRTUALTABLE
1254 case PragTyp_MODULE_LIST: {
1255 HashElem *j;
1256 pParse->nMem = 1;
1257 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
1258 Module *pMod = (Module*)sqliteHashData(j);
1259 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
drhab53bb62017-07-07 15:43:22 +00001260 }
1261 }
1262 break;
1263#endif /* SQLITE_OMIT_VIRTUALTABLE */
1264
drh8ae11aa2017-07-07 17:33:07 +00001265 case PragTyp_PRAGMA_LIST: {
1266 int i;
1267 for(i=0; i<ArraySize(aPragmaName); i++){
1268 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
drh8ae11aa2017-07-07 17:33:07 +00001269 }
1270 }
1271 break;
1272#endif /* SQLITE_INTROSPECTION_PRAGMAS */
drhab53bb62017-07-07 15:43:22 +00001273
drh13d70422004-11-13 15:59:14 +00001274#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1275
drhb7f91642004-10-31 02:22:47 +00001276#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001277 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001278 FKey *pFK;
1279 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001280 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001281 if( pTab ){
drh78100cc2003-08-23 22:40:53 +00001282 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001283 if( pFK ){
dan3c425482018-11-20 18:09:59 +00001284 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977742f9472004-06-16 12:02:43 +00001285 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001286 pParse->nMem = 8;
dan3c425482018-11-20 18:09:59 +00001287 sqlite3CodeVerifySchema(pParse, iTabDb);
danielk1977742f9472004-06-16 12:02:43 +00001288 while(pFK){
1289 int j;
1290 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001291 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1292 i,
1293 j,
1294 pFK->zTo,
1295 pTab->aCol[pFK->aCol[j].iFrom].zName,
1296 pFK->aCol[j].zCol,
1297 actionName(pFK->aAction[1]), /* ON UPDATE */
1298 actionName(pFK->aAction[0]), /* ON DELETE */
1299 "NONE");
danielk1977742f9472004-06-16 12:02:43 +00001300 }
1301 ++i;
1302 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001303 }
drh78100cc2003-08-23 22:40:53 +00001304 }
1305 }
drh9ccd8652013-09-13 16:36:46 +00001306 }
1307 break;
drhb7f91642004-10-31 02:22:47 +00001308#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001309
drh6c5b9152012-12-17 16:46:37 +00001310#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001311#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001312 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001313 FKey *pFK; /* A foreign key constraint */
1314 Table *pTab; /* Child table contain "REFERENCES" keyword */
1315 Table *pParent; /* Parent table that child points to */
1316 Index *pIdx; /* Index in the parent table */
1317 int i; /* Loop counter: Foreign key number for pTab */
1318 int j; /* Loop counter: Field of the foreign key */
1319 HashElem *k; /* Loop counter: Next table in schema */
1320 int x; /* result variable */
1321 int regResult; /* 3 registers to hold a result row */
1322 int regKey; /* Register to hold key for checking the FK */
1323 int regRow; /* Registers to hold a row from pTab */
1324 int addrTop; /* Top of a loop checking foreign keys */
1325 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001326 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001327
drh613028b2012-12-17 18:43:02 +00001328 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001329 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001330 regKey = ++pParse->nMem;
1331 regRow = ++pParse->nMem;
drh613028b2012-12-17 18:43:02 +00001332 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1333 while( k ){
dan3c425482018-11-20 18:09:59 +00001334 int iTabDb;
drh613028b2012-12-17 18:43:02 +00001335 if( zRight ){
1336 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1337 k = 0;
1338 }else{
1339 pTab = (Table*)sqliteHashData(k);
1340 k = sqliteHashNext(k);
1341 }
drh9148def2012-12-17 20:40:39 +00001342 if( pTab==0 || pTab->pFKey==0 ) continue;
dan3c425482018-11-20 18:09:59 +00001343 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1344 sqlite3CodeVerifySchema(pParse, iTabDb);
1345 sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001346 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
dan3c425482018-11-20 18:09:59 +00001347 sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001348 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh6c5b9152012-12-17 16:46:37 +00001349 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001350 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1351 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001352 pIdx = 0;
dan3c425482018-11-20 18:09:59 +00001353 sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001354 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001355 if( x==0 ){
1356 if( pIdx==0 ){
dan3c425482018-11-20 18:09:59 +00001357 sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead);
drh6c5b9152012-12-17 16:46:37 +00001358 }else{
dan3c425482018-11-20 18:09:59 +00001359 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iTabDb);
drh2ec2fb22013-11-06 19:59:23 +00001360 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001361 }
1362 }else{
drh613028b2012-12-17 18:43:02 +00001363 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001364 break;
1365 }
drh6c5b9152012-12-17 16:46:37 +00001366 }
dan5e878302013-10-12 19:06:48 +00001367 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001368 if( pFK ) break;
1369 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001370 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001371 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001372 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001373 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001374 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001375 if( pParent ){
1376 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1377 assert( x==0 );
1378 }
drhec4ccdb2018-12-29 02:26:59 +00001379 addrOk = sqlite3VdbeMakeLabel(pParse);
dan4bee5592017-04-17 18:42:33 +00001380
1381 /* Generate code to read the child key values into registers
1382 ** regRow..regRow+n. If any of the child key values are NULL, this
1383 ** row cannot cause an FK violation. Jump directly to addrOk in
1384 ** this case. */
1385 for(j=0; j<pFK->nCol; j++){
1386 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
1387 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
1388 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001389 }
dan4bee5592017-04-17 18:42:33 +00001390
1391 /* Generate code to query the parent index for a matching parent
1392 ** key. If a match is found, jump to addrOk. */
1393 if( pIdx ){
1394 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
1395 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
1396 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
1397 VdbeCoverage(v);
1398 }else if( pParent ){
1399 int jmp = sqlite3VdbeCurrentAddr(v)+2;
1400 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
1401 sqlite3VdbeGoto(v, addrOk);
1402 assert( pFK->nCol==1 );
1403 }
1404
1405 /* Generate code to report an FK violation to the caller. */
dan940464b2017-04-17 18:02:41 +00001406 if( HasRowid(pTab) ){
1407 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
1408 }else{
1409 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
1410 }
drh40cf27c2017-07-07 16:00:53 +00001411 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001412 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001413 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001414 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001415 }
drh688852a2014-02-17 22:40:43 +00001416 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001417 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001418 }
drh9ccd8652013-09-13 16:36:46 +00001419 }
1420 break;
dan09ff9e12013-03-11 11:49:03 +00001421#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001422#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1423
drh55ef4d92005-08-14 01:20:37 +00001424 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1425 ** used will be case sensitive or not depending on the RHS.
1426 */
drh9ccd8652013-09-13 16:36:46 +00001427 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001428 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001429 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001430 }
drh9ccd8652013-09-13 16:36:46 +00001431 }
1432 break;
drh55ef4d92005-08-14 01:20:37 +00001433
drh1dcdbc02007-01-27 02:24:54 +00001434#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1435# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1436#endif
1437
drhb7f91642004-10-31 02:22:47 +00001438#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh8b174f22017-02-22 15:11:36 +00001439 /* PRAGMA integrity_check
1440 ** PRAGMA integrity_check(N)
1441 ** PRAGMA quick_check
1442 ** PRAGMA quick_check(N)
1443 **
1444 ** Verify the integrity of the database.
1445 **
1446 ** The "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001447 ** integrity_check designed to detect most database corruption
drh8b174f22017-02-22 15:11:36 +00001448 ** without the overhead of cross-checking indexes. Quick_check
1449 ** is linear time wherease integrity_check is O(NlogN).
danielk197741c58b72007-12-29 13:39:19 +00001450 */
drh9ccd8652013-09-13 16:36:46 +00001451 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001452 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001453
drhc5227312011-10-13 17:09:01 +00001454 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001455
dan5885e762012-07-16 10:06:12 +00001456 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1457 ** then iDb is set to the index of the database identified by <db>.
1458 ** In this case, the integrity of database iDb only is verified by
1459 ** the VDBE created below.
1460 **
1461 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1462 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1463 ** to -1 here, to indicate that the VDBE should verify the integrity
1464 ** of all attached databases. */
1465 assert( iDb>=0 );
1466 assert( iDb==0 || pId2->z );
1467 if( pId2->z==0 ) iDb = -1;
1468
drhed717fe2003-06-15 23:42:24 +00001469 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001470 pParse->nMem = 6;
drh1dcdbc02007-01-27 02:24:54 +00001471
1472 /* Set the maximum error count */
1473 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1474 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001475 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001476 if( mxErr<=0 ){
1477 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1478 }
1479 }
drh66accfc2017-02-22 18:04:42 +00001480 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001481
1482 /* Do an integrity check on each database file */
1483 for(i=0; i<db->nDb; i++){
drh9ecd7082017-09-10 01:06:05 +00001484 HashElem *x; /* For looping over tables in the schema */
1485 Hash *pTbls; /* Set of all tables in the schema */
1486 int *aRoot; /* Array of root page numbers of all btrees */
1487 int cnt = 0; /* Number of entries in aRoot[] */
1488 int mxIdx = 0; /* Maximum number of indexes for any table */
drhed717fe2003-06-15 23:42:24 +00001489
danielk197753c0f742005-03-29 03:10:59 +00001490 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001491 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001492
drh80242052004-06-09 00:48:12 +00001493 sqlite3CodeVerifySchema(pParse, i);
1494
drhed717fe2003-06-15 23:42:24 +00001495 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001496 **
drh98968b22016-03-15 22:00:39 +00001497 ** Begin by finding the root pages numbers
drh2d401ab2008-01-10 23:50:11 +00001498 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001499 */
dan5885e762012-07-16 10:06:12 +00001500 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001501 pTbls = &db->aDb[i].pSchema->tblHash;
drh98968b22016-03-15 22:00:39 +00001502 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh9ecd7082017-09-10 01:06:05 +00001503 Table *pTab = sqliteHashData(x); /* Current table */
1504 Index *pIdx; /* An index on pTab */
1505 int nIdx; /* Number of indexes on pTab */
drh98968b22016-03-15 22:00:39 +00001506 if( HasRowid(pTab) ) cnt++;
drhbb9b5f22016-03-19 00:35:02 +00001507 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
1508 if( nIdx>mxIdx ) mxIdx = nIdx;
drh98968b22016-03-15 22:00:39 +00001509 }
1510 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
1511 if( aRoot==0 ) break;
1512 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1513 Table *pTab = sqliteHashData(x);
1514 Index *pIdx;
drhb5c10632017-09-21 00:49:15 +00001515 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
drh79069752004-05-22 21:30:40 +00001516 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhb5c10632017-09-21 00:49:15 +00001517 aRoot[++cnt] = pIdx->tnum;
drh79069752004-05-22 21:30:40 +00001518 }
1519 }
drhb5c10632017-09-21 00:49:15 +00001520 aRoot[0] = cnt;
drh2d401ab2008-01-10 23:50:11 +00001521
1522 /* Make sure sufficient number of registers have been allocated */
drhbb9b5f22016-03-19 00:35:02 +00001523 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
drh3963e582017-07-15 20:33:19 +00001524 sqlite3ClearTempRegCache(pParse);
drh2d401ab2008-01-10 23:50:11 +00001525
1526 /* Do the b-tree integrity checks */
drh98968b22016-03-15 22:00:39 +00001527 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
drh4f21c4a2008-12-10 22:15:00 +00001528 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001529 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001530 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh69c33822016-08-18 14:33:11 +00001531 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
drh66a51672008-01-03 00:01:23 +00001532 P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:05 +00001533 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
1534 integrityCheckResultRow(v);
drh1dcdbc02007-01-27 02:24:54 +00001535 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001536
1537 /* Make sure all the indices are constructed correctly.
1538 */
drh8b174f22017-02-22 15:11:36 +00001539 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001540 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001541 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001542 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001543 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001544 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001545 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001546
drha3b2da92017-03-17 03:21:14 +00001547 if( pTab->tnum<1 ) continue; /* Skip VIEWs or VIRTUAL TABLEs */
drh26198bb2013-10-31 11:15:09 +00001548 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
danfd261ec2015-10-22 20:54:33 +00001549 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001550 1, 0, &iDataCur, &iIdxCur);
drh9ecd7082017-09-10 01:06:05 +00001551 /* reg[7] counts the number of entries in the table.
1552 ** reg[8+i] counts the number of entries in the i-th index
1553 */
drh6fbe41a2013-10-30 20:22:55 +00001554 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001555 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001556 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001557 }
drhbb9b5f22016-03-19 00:35:02 +00001558 assert( pParse->nMem>=8+j );
1559 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
drh688852a2014-02-17 22:40:43 +00001560 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001561 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drh4011c442018-06-06 19:48:19 +00001562 if( !isQuick ){
1563 /* Sanity check on record header decoding */
1564 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nCol-1, 3);
1565 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1566 }
drhcefc87f2014-08-01 01:40:33 +00001567 /* Verify that all NOT NULL columns really are NOT NULL */
1568 for(j=0; j<pTab->nCol; j++){
1569 char *zErr;
drh66accfc2017-02-22 18:04:42 +00001570 int jmp2;
drhcefc87f2014-08-01 01:40:33 +00001571 if( j==pTab->iPKey ) continue;
1572 if( pTab->aCol[j].notNull==0 ) continue;
1573 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1574 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1575 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
drhcefc87f2014-08-01 01:40:33 +00001576 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1577 pTab->aCol[j].zName);
1578 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:05 +00001579 integrityCheckResultRow(v);
drhcefc87f2014-08-01 01:40:33 +00001580 sqlite3VdbeJumpHere(v, jmp2);
drhcefc87f2014-08-01 01:40:33 +00001581 }
drh8a284dc2017-02-22 14:15:37 +00001582 /* Verify CHECK constraints */
1583 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
dan75f95582017-04-04 19:58:54 +00001584 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
1585 if( db->mallocFailed==0 ){
drhec4ccdb2018-12-29 02:26:59 +00001586 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
1587 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
dan75f95582017-04-04 19:58:54 +00001588 char *zErr;
1589 int k;
drh6e97f8e2017-07-20 13:17:08 +00001590 pParse->iSelfTab = iDataCur + 1;
dan75f95582017-04-04 19:58:54 +00001591 for(k=pCheck->nExpr-1; k>0; k--){
1592 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
1593 }
1594 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
1595 SQLITE_JUMPIFNULL);
1596 sqlite3VdbeResolveLabel(v, addrCkFault);
drh3e34eab2017-07-19 19:48:40 +00001597 pParse->iSelfTab = 0;
dan75f95582017-04-04 19:58:54 +00001598 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
1599 pTab->zName);
1600 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh9ecd7082017-09-10 01:06:05 +00001601 integrityCheckResultRow(v);
dan75f95582017-04-04 19:58:54 +00001602 sqlite3VdbeResolveLabel(v, addrCkOk);
drh8a284dc2017-02-22 14:15:37 +00001603 }
dan75f95582017-04-04 19:58:54 +00001604 sqlite3ExprListDelete(db, pCheck);
drhcefc87f2014-08-01 01:40:33 +00001605 }
drh226cef42017-09-09 20:38:49 +00001606 if( !isQuick ){ /* Omit the remaining tests for quick_check */
drh226cef42017-09-09 20:38:49 +00001607 /* Validate index entries for the current row */
1608 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1609 int jmp2, jmp3, jmp4, jmp5;
drhec4ccdb2018-12-29 02:26:59 +00001610 int ckUniq = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:49 +00001611 if( pPk==pIdx ) continue;
1612 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1613 pPrior, r1);
1614 pPrior = pIdx;
1615 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
1616 /* Verify that an index entry exists for the current table row */
1617 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
1618 pIdx->nColumn); VdbeCoverage(v);
1619 sqlite3VdbeLoadString(v, 3, "row ");
1620 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1621 sqlite3VdbeLoadString(v, 4, " missing from index ");
1622 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1623 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
1624 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh9ecd7082017-09-10 01:06:05 +00001625 jmp4 = integrityCheckResultRow(v);
drh226cef42017-09-09 20:38:49 +00001626 sqlite3VdbeJumpHere(v, jmp2);
1627 /* For UNIQUE indexes, verify that only one entry exists with the
1628 ** current key. The entry is unique if (1) any column is NULL
1629 ** or (2) the next entry has a different key */
1630 if( IsUniqueIndex(pIdx) ){
drhec4ccdb2018-12-29 02:26:59 +00001631 int uniqOk = sqlite3VdbeMakeLabel(pParse);
drh226cef42017-09-09 20:38:49 +00001632 int jmp6;
1633 int kk;
1634 for(kk=0; kk<pIdx->nKeyCol; kk++){
1635 int iCol = pIdx->aiColumn[kk];
1636 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
1637 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
1638 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1639 VdbeCoverage(v);
1640 }
1641 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
1642 sqlite3VdbeGoto(v, uniqOk);
1643 sqlite3VdbeJumpHere(v, jmp6);
1644 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1645 pIdx->nKeyCol); VdbeCoverage(v);
1646 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1647 sqlite3VdbeGoto(v, jmp5);
1648 sqlite3VdbeResolveLabel(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00001649 }
drh226cef42017-09-09 20:38:49 +00001650 sqlite3VdbeJumpHere(v, jmp4);
1651 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhcefc87f2014-08-01 01:40:33 +00001652 }
drhed717fe2003-06-15 23:42:24 +00001653 }
drh688852a2014-02-17 22:40:43 +00001654 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001655 sqlite3VdbeJumpHere(v, loopTop-1);
1656#ifndef SQLITE_OMIT_BTREECOUNT
drh8b174f22017-02-22 15:11:36 +00001657 if( !isQuick ){
1658 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
1659 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1660 if( pPk==pIdx ) continue;
drh8b174f22017-02-22 15:11:36 +00001661 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh66accfc2017-02-22 18:04:42 +00001662 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
drh8b174f22017-02-22 15:11:36 +00001663 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh9ecd7082017-09-10 01:06:05 +00001664 sqlite3VdbeLoadString(v, 4, pIdx->zName);
1665 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
1666 integrityCheckResultRow(v);
drh66accfc2017-02-22 18:04:42 +00001667 sqlite3VdbeJumpHere(v, addr);
drh8b174f22017-02-22 15:11:36 +00001668 }
drhed717fe2003-06-15 23:42:24 +00001669 }
drh8a9789b2013-08-01 03:36:59 +00001670#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001671 }
1672 }
drh2ce18652016-01-16 20:50:21 +00001673 {
1674 static const int iLn = VDBE_OFFSET_LINENO(2);
1675 static const VdbeOpList endCode[] = {
1676 { OP_AddImm, 1, 0, 0}, /* 0 */
drh66accfc2017-02-22 18:04:42 +00001677 { OP_IfNotZero, 1, 4, 0}, /* 1 */
drh2ce18652016-01-16 20:50:21 +00001678 { OP_String8, 0, 3, 0}, /* 2 */
drh1b325542016-02-03 01:55:44 +00001679 { OP_ResultRow, 3, 1, 0}, /* 3 */
drh74588ce2017-09-13 00:13:05 +00001680 { OP_Halt, 0, 0, 0}, /* 4 */
1681 { OP_String8, 0, 3, 0}, /* 5 */
1682 { OP_Goto, 0, 3, 0}, /* 6 */
drh2ce18652016-01-16 20:50:21 +00001683 };
1684 VdbeOp *aOp;
1685
1686 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
1687 if( aOp ){
drh66accfc2017-02-22 18:04:42 +00001688 aOp[0].p2 = 1-mxErr;
drh2ce18652016-01-16 20:50:21 +00001689 aOp[2].p4type = P4_STATIC;
1690 aOp[2].p4.z = "ok";
drh74588ce2017-09-13 00:13:05 +00001691 aOp[5].p4type = P4_STATIC;
1692 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
drh2ce18652016-01-16 20:50:21 +00001693 }
drh74588ce2017-09-13 00:13:05 +00001694 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
drh2ce18652016-01-16 20:50:21 +00001695 }
drh9ccd8652013-09-13 16:36:46 +00001696 }
1697 break;
drhb7f91642004-10-31 02:22:47 +00001698#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1699
drh13d70422004-11-13 15:59:14 +00001700#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001701 /*
1702 ** PRAGMA encoding
1703 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1704 **
drh85b623f2007-12-13 21:54:09 +00001705 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001706 ** database. If the database is not initialized, it is initialized now.
1707 **
1708 ** The second form of this pragma is a no-op if the main database file
1709 ** has not already been initialized. In this case it sets the default
1710 ** encoding that will be used for the main database file if a new file
1711 ** is created. If an existing main database file is opened, then the
1712 ** default text encoding for the existing database is used.
1713 **
1714 ** In all cases new databases created using the ATTACH command are
1715 ** created to use the same default text encoding as the main database. If
1716 ** the main database has not been initialized and/or created when ATTACH
1717 ** is executed, this is done before the ATTACH operation.
1718 **
1719 ** In the second form this pragma sets the text encoding to be used in
1720 ** new database files created using this database handle. It is only
1721 ** useful if invoked immediately after the main database i
1722 */
drh9ccd8652013-09-13 16:36:46 +00001723 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001724 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001725 char *zName;
1726 u8 enc;
1727 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001728 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001729 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1730 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1731 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001732 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001733 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001734 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1735 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001736 { 0, 0 }
1737 };
drh0f7eb612006-08-08 13:51:43 +00001738 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001739 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001740 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00001741 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1742 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1743 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drhc232aca2016-12-15 16:01:17 +00001744 returnSingleText(v, encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00001745 }else{ /* "PRAGMA encoding = XXX" */
1746 /* Only change the value of sqlite.enc if the database handle is not
1747 ** initialized. If the main database exists, the new sqlite.enc value
1748 ** will be overwritten when the schema is next loaded. If it does not
1749 ** already exists, it will be created to use the new encoding value.
1750 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001751 if(
1752 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1753 DbHasProperty(db, 0, DB_Empty)
1754 ){
danielk19778e227872004-06-07 07:52:17 +00001755 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1756 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh9bd3cc42014-12-12 23:17:54 +00001757 SCHEMA_ENC(db) = ENC(db) =
1758 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001759 break;
1760 }
1761 }
1762 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001763 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001764 }
1765 }
1766 }
drh9ccd8652013-09-13 16:36:46 +00001767 }
1768 break;
drh13d70422004-11-13 15:59:14 +00001769#endif /* SQLITE_OMIT_UTF16 */
1770
1771#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001772 /*
drh9b0cf342015-11-12 14:57:19 +00001773 ** PRAGMA [schema.]schema_version
1774 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001775 **
drh9b0cf342015-11-12 14:57:19 +00001776 ** PRAGMA [schema.]user_version
1777 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001778 **
drhe459bd42016-03-16 20:05:57 +00001779 ** PRAGMA [schema.]freelist_count
1780 **
1781 ** PRAGMA [schema.]data_version
drh4ee09b42013-05-01 19:49:27 +00001782 **
drh9b0cf342015-11-12 14:57:19 +00001783 ** PRAGMA [schema.]application_id
1784 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00001785 **
danielk1977b92b70b2004-11-12 16:11:59 +00001786 ** The pragma's schema_version and user_version are used to set or get
1787 ** the value of the schema-version and user-version, respectively. Both
1788 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001789 ** stored in the database header.
1790 **
1791 ** The schema-cookie is usually only manipulated internally by SQLite. It
1792 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001793 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001794 ** SQLite each time a query is executed to ensure that the internal cache
1795 ** of the schema used when compiling the SQL query matches the schema of
1796 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001797 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1798 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001799 ** crashes or database corruption. Use with caution!
1800 **
danielk1977b92b70b2004-11-12 16:11:59 +00001801 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001802 ** applications for any purpose.
1803 */
drh9ccd8652013-09-13 16:36:46 +00001804 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00001805 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00001806 sqlite3VdbeUsesBtree(v, iDb);
drhc232aca2016-12-15 16:01:17 +00001807 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00001808 /* Write the specified cookie value */
1809 static const VdbeOpList setCookie[] = {
1810 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1861afc2016-02-01 21:48:34 +00001811 { OP_SetCookie, 0, 0, 0}, /* 1 */
danielk1977dae24952004-11-11 05:10:43 +00001812 };
drh2ce18652016-01-16 20:50:21 +00001813 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001814 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
drh2ce18652016-01-16 20:50:21 +00001815 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
drhdad300d2016-01-18 00:20:26 +00001816 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001817 aOp[0].p1 = iDb;
drh1861afc2016-02-01 21:48:34 +00001818 aOp[1].p1 = iDb;
1819 aOp[1].p2 = iCookie;
1820 aOp[1].p3 = sqlite3Atoi(zRight);
danielk1977dae24952004-11-11 05:10:43 +00001821 }else{
1822 /* Read the specified cookie value */
1823 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001824 { OP_Transaction, 0, 0, 0}, /* 0 */
1825 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001826 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001827 };
drh2ce18652016-01-16 20:50:21 +00001828 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001829 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
drh2ce18652016-01-16 20:50:21 +00001830 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
drhdad300d2016-01-18 00:20:26 +00001831 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001832 aOp[0].p1 = iDb;
1833 aOp[1].p1 = iDb;
1834 aOp[1].p3 = iCookie;
drhf71a3662016-03-16 20:44:45 +00001835 sqlite3VdbeReusable(v);
danielk1977dae24952004-11-11 05:10:43 +00001836 }
drh9ccd8652013-09-13 16:36:46 +00001837 }
1838 break;
drh13d70422004-11-13 15:59:14 +00001839#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001840
shanehdc97a8c2010-02-23 20:08:35 +00001841#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1842 /*
1843 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001844 **
drh71caabf2010-02-26 15:39:24 +00001845 ** Return the names of all compile-time options used in this build,
1846 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001847 */
drh9ccd8652013-09-13 16:36:46 +00001848 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00001849 int i = 0;
1850 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00001851 pParse->nMem = 1;
shanehdc97a8c2010-02-23 20:08:35 +00001852 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00001853 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00001854 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1855 }
drhf71a3662016-03-16 20:44:45 +00001856 sqlite3VdbeReusable(v);
drh9ccd8652013-09-13 16:36:46 +00001857 }
1858 break;
shanehdc97a8c2010-02-23 20:08:35 +00001859#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1860
dan5cf53532010-05-01 16:40:20 +00001861#ifndef SQLITE_OMIT_WAL
1862 /*
drh9b0cf342015-11-12 14:57:19 +00001863 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00001864 **
1865 ** Checkpoint the database.
1866 */
drh9ccd8652013-09-13 16:36:46 +00001867 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00001868 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001869 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1870 if( zRight ){
1871 if( sqlite3StrICmp(zRight, "full")==0 ){
1872 eMode = SQLITE_CHECKPOINT_FULL;
1873 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1874 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00001875 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1876 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00001877 }
1878 }
dancdc1f042010-11-18 12:11:05 +00001879 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00001880 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001881 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00001882 }
1883 break;
dan5a299f92010-05-03 11:05:08 +00001884
1885 /*
1886 ** PRAGMA wal_autocheckpoint
1887 ** PRAGMA wal_autocheckpoint = N
1888 **
1889 ** Configure a database connection to automatically checkpoint a database
1890 ** after accumulating N frames in the log. Or query for the current value
1891 ** of N.
1892 */
drh9ccd8652013-09-13 16:36:46 +00001893 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00001894 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001895 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001896 }
drhc232aca2016-12-15 16:01:17 +00001897 returnSingleInt(v,
drhb033d8b2010-05-03 13:37:30 +00001898 db->xWalCallback==sqlite3WalDefaultHook ?
1899 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00001900 }
1901 break;
dan5cf53532010-05-01 16:40:20 +00001902#endif
dan7c246102010-04-12 19:00:29 +00001903
drh09419b42011-11-16 19:29:17 +00001904 /*
1905 ** PRAGMA shrink_memory
1906 **
drh51a74d42015-02-28 01:04:27 +00001907 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
1908 ** connection on which it is invoked to free up as much memory as it
1909 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00001910 */
drh9ccd8652013-09-13 16:36:46 +00001911 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00001912 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00001913 break;
1914 }
drh09419b42011-11-16 19:29:17 +00001915
drhf3603962012-09-07 16:46:59 +00001916 /*
drh2ead47c2017-02-22 20:24:10 +00001917 ** PRAGMA optimize
drh1cfaf8e2017-03-02 14:17:21 +00001918 ** PRAGMA optimize(MASK)
drh2ead47c2017-02-22 20:24:10 +00001919 ** PRAGMA schema.optimize
drh1cfaf8e2017-03-02 14:17:21 +00001920 ** PRAGMA schema.optimize(MASK)
drh99d5b4c2017-02-18 22:52:40 +00001921 **
drh2ead47c2017-02-22 20:24:10 +00001922 ** Attempt to optimize the database. All schemas are optimized in the first
drh1cfaf8e2017-03-02 14:17:21 +00001923 ** two forms, and only the specified schema is optimized in the latter two.
drh2ead47c2017-02-22 20:24:10 +00001924 **
drhe1f1c082017-03-06 20:44:13 +00001925 ** The details of optimizations performed by this pragma are expected
drh2ead47c2017-02-22 20:24:10 +00001926 ** to change and improve over time. Applications should anticipate that
1927 ** this pragma will perform new optimizations in future releases.
1928 **
drh1cfaf8e2017-03-02 14:17:21 +00001929 ** The optional argument is a bitmask of optimizations to perform:
drh2ead47c2017-02-22 20:24:10 +00001930 **
drh1cfaf8e2017-03-02 14:17:21 +00001931 ** 0x0001 Debugging mode. Do not actually perform any optimizations
1932 ** but instead return one line of text for each optimization
1933 ** that would have been done. Off by default.
drh99d5b4c2017-02-18 22:52:40 +00001934 **
drh1cfaf8e2017-03-02 14:17:21 +00001935 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
1936 ** See below for additional information.
1937 **
1938 ** 0x0004 (Not yet implemented) Record usage and performance
1939 ** information from the current session in the
1940 ** database file so that it will be available to "optimize"
1941 ** pragmas run by future database connections.
1942 **
1943 ** 0x0008 (Not yet implemented) Create indexes that might have
1944 ** been helpful to recent queries
1945 **
drhe7c6f972017-07-15 20:25:22 +00001946 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
1947 ** of the optimizations listed above except Debug Mode, including new
drh59dbe3a2017-03-06 23:51:16 +00001948 ** optimizations that have not yet been invented. If new optimizations are
1949 ** ever added that should be off by default, those off-by-default
1950 ** optimizations will have bitmasks of 0x10000 or larger.
drh1cfaf8e2017-03-02 14:17:21 +00001951 **
1952 ** DETERMINATION OF WHEN TO RUN ANALYZE
1953 **
1954 ** In the current implementation, a table is analyzed if only if all of
drh2ead47c2017-02-22 20:24:10 +00001955 ** the following are true:
drh99d5b4c2017-02-18 22:52:40 +00001956 **
drh1cfaf8e2017-03-02 14:17:21 +00001957 ** (1) MASK bit 0x02 is set.
1958 **
1959 ** (2) The query planner used sqlite_stat1-style statistics for one or
drh99d5b4c2017-02-18 22:52:40 +00001960 ** more indexes of the table at some point during the lifetime of
1961 ** the current connection.
1962 **
drh1cfaf8e2017-03-02 14:17:21 +00001963 ** (3) One or more indexes of the table are currently unanalyzed OR
drh99d5b4c2017-02-18 22:52:40 +00001964 ** the number of rows in the table has increased by 25 times or more
1965 ** since the last time ANALYZE was run.
drh2ead47c2017-02-22 20:24:10 +00001966 **
1967 ** The rules for when tables are analyzed are likely to change in
1968 ** future releases.
drh72052a72017-02-17 16:26:34 +00001969 */
drh2ead47c2017-02-22 20:24:10 +00001970 case PragTyp_OPTIMIZE: {
drh99d5b4c2017-02-18 22:52:40 +00001971 int iDbLast; /* Loop termination point for the schema loop */
1972 int iTabCur; /* Cursor for a table whose size needs checking */
1973 HashElem *k; /* Loop over tables of a schema */
1974 Schema *pSchema; /* The current schema */
1975 Table *pTab; /* A table in the schema */
1976 Index *pIdx; /* An index of the table */
1977 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
1978 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
drh1cfaf8e2017-03-02 14:17:21 +00001979 u32 opMask; /* Mask of operations to perform */
drh4a54bb52017-02-18 15:58:52 +00001980
drh1cfaf8e2017-03-02 14:17:21 +00001981 if( zRight ){
1982 opMask = (u32)sqlite3Atoi(zRight);
1983 if( (opMask & 0x02)==0 ) break;
1984 }else{
drh59dbe3a2017-03-06 23:51:16 +00001985 opMask = 0xfffe;
drh1cfaf8e2017-03-02 14:17:21 +00001986 }
drh4a54bb52017-02-18 15:58:52 +00001987 iTabCur = pParse->nTab++;
1988 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
1989 if( iDb==1 ) continue;
1990 sqlite3CodeVerifySchema(pParse, iDb);
1991 pSchema = db->aDb[iDb].pSchema;
1992 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
1993 pTab = (Table*)sqliteHashData(k);
drh99d5b4c2017-02-18 22:52:40 +00001994
1995 /* If table pTab has not been used in a way that would benefit from
1996 ** having analysis statistics during the current session, then skip it.
1997 ** This also has the effect of skipping virtual tables and views */
drh4a54bb52017-02-18 15:58:52 +00001998 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
drh99d5b4c2017-02-18 22:52:40 +00001999
2000 /* Reanalyze if the table is 25 times larger than the last analysis */
drh4a54bb52017-02-18 15:58:52 +00002001 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
2002 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2003 if( !pIdx->hasStat1 ){
drh99d5b4c2017-02-18 22:52:40 +00002004 szThreshold = 0; /* Always analyze if any index lacks statistics */
drh4a54bb52017-02-18 15:58:52 +00002005 break;
2006 }
2007 }
2008 if( szThreshold ){
2009 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
2010 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
drh1cfaf8e2017-03-02 14:17:21 +00002011 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
drh4a54bb52017-02-18 15:58:52 +00002012 VdbeCoverage(v);
2013 }
2014 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
2015 db->aDb[iDb].zDbSName, pTab->zName);
drh1cfaf8e2017-03-02 14:17:21 +00002016 if( opMask & 0x01 ){
2017 int r1 = sqlite3GetTempReg(pParse);
2018 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
2019 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
2020 }else{
2021 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
2022 }
drh4a54bb52017-02-18 15:58:52 +00002023 }
2024 }
drhbce04142017-02-23 00:58:36 +00002025 sqlite3VdbeAddOp0(v, OP_Expire);
drh72052a72017-02-17 16:26:34 +00002026 break;
2027 }
2028
2029 /*
drhf3603962012-09-07 16:46:59 +00002030 ** PRAGMA busy_timeout
2031 ** PRAGMA busy_timeout = N
2032 **
2033 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00002034 ** if one is set. If no busy handler or a different busy handler is set
2035 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2036 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00002037 */
drhd49c3582013-09-13 19:00:06 +00002038 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00002039 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00002040 if( zRight ){
2041 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2042 }
drhc232aca2016-12-15 16:01:17 +00002043 returnSingleInt(v, db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00002044 break;
2045 }
drhf3603962012-09-07 16:46:59 +00002046
drh55e85ca2013-09-13 21:01:56 +00002047 /*
2048 ** PRAGMA soft_heap_limit
2049 ** PRAGMA soft_heap_limit = N
2050 **
drh51a74d42015-02-28 01:04:27 +00002051 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
2052 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
2053 ** specified and is a non-negative integer.
2054 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
2055 ** returns the same integer that would be returned by the
2056 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00002057 */
2058 case PragTyp_SOFT_HEAP_LIMIT: {
2059 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00002060 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00002061 sqlite3_soft_heap_limit64(N);
2062 }
drhc232aca2016-12-15 16:01:17 +00002063 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00002064 break;
2065 }
2066
drh03459612014-08-25 15:13:22 +00002067 /*
2068 ** PRAGMA threads
2069 ** PRAGMA threads = N
2070 **
2071 ** Configure the maximum number of worker threads. Return the new
2072 ** maximum, which might be less than requested.
2073 */
2074 case PragTyp_THREADS: {
2075 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00002076 if( zRight
drh03459612014-08-25 15:13:22 +00002077 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
2078 && N>=0
2079 ){
drh111544c2014-08-29 16:20:47 +00002080 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00002081 }
drhc232aca2016-12-15 16:01:17 +00002082 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00002083 break;
2084 }
2085
dougcurrie81c95ef2004-06-18 23:21:47 +00002086#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00002087 /*
2088 ** Report the current state of file logs for all databases
2089 */
drh9ccd8652013-09-13 16:36:46 +00002090 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00002091 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00002092 "unlocked", "shared", "reserved", "pending", "exclusive"
2093 };
2094 int i;
drh2d401ab2008-01-10 23:50:11 +00002095 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00002096 for(i=0; i<db->nDb; i++){
2097 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00002098 const char *zState = "unknown";
2099 int j;
drh69c33822016-08-18 14:33:11 +00002100 if( db->aDb[i].zDbSName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00002101 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00002102 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00002103 zState = "closed";
drh69c33822016-08-18 14:33:11 +00002104 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
drh9e33c2c2007-08-31 18:34:59 +00002105 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2106 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00002107 }
drh69c33822016-08-18 14:33:11 +00002108 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
drh89ac8c12004-06-09 14:17:20 +00002109 }
drh9ccd8652013-09-13 16:36:46 +00002110 break;
2111 }
drh89ac8c12004-06-09 14:17:20 +00002112#endif
2113
shanehad9f9f62010-02-17 17:48:46 +00002114#ifdef SQLITE_HAS_CODEC
drhfa5c62e2018-10-11 18:41:50 +00002115 /* Pragma iArg
2116 ** ---------- ------
2117 ** key 0
2118 ** rekey 1
2119 ** hexkey 2
2120 ** hexrekey 3
2121 ** textkey 4
2122 ** textrekey 5
2123 */
drh9ccd8652013-09-13 16:36:46 +00002124 case PragTyp_KEY: {
drhfa5c62e2018-10-11 18:41:50 +00002125 if( zRight ){
2126 int n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1;
2127 if( (pPragma->iArg & 1)==0 ){
2128 sqlite3_key_v2(db, zDb, zRight, n);
2129 }else{
2130 sqlite3_rekey_v2(db, zDb, zRight, n);
2131 }
2132 }
drh9ccd8652013-09-13 16:36:46 +00002133 break;
2134 }
2135 case PragTyp_HEXKEY: {
2136 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00002137 u8 iByte;
2138 int i;
drh9ccd8652013-09-13 16:36:46 +00002139 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00002140 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
2141 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
2142 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00002143 }
drhfa5c62e2018-10-11 18:41:50 +00002144 if( (pPragma->iArg & 1)==0 ){
drh9ccd8652013-09-13 16:36:46 +00002145 sqlite3_key_v2(db, zDb, zKey, i/2);
2146 }else{
2147 sqlite3_rekey_v2(db, zDb, zKey, i/2);
2148 }
drhd2cb50b2009-01-09 21:41:17 +00002149 }
drh9ccd8652013-09-13 16:36:46 +00002150 break;
2151 }
drh3c4f2a42005-12-08 18:12:56 +00002152#endif
shanehad9f9f62010-02-17 17:48:46 +00002153#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00002154 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00002155#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00002156 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002157 sqlite3_activate_see(&zRight[4]);
2158 }
2159#endif
2160#ifdef SQLITE_ENABLE_CEROD
2161 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002162 sqlite3_activate_cerod(&zRight[6]);
2163 }
2164#endif
drh9ccd8652013-09-13 16:36:46 +00002165 }
2166 break;
drh21e2cab2006-09-25 18:01:57 +00002167#endif
drh3c4f2a42005-12-08 18:12:56 +00002168
drh9ccd8652013-09-13 16:36:46 +00002169 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00002170
dan9e1ab1a2017-01-05 19:32:48 +00002171 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
2172 ** purpose is to execute assert() statements to verify that if the
2173 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
2174 ** to the PRAGMA, the implementation has not added any OP_ResultRow
2175 ** instructions to the VM. */
2176 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
2177 sqlite3VdbeVerifyNoResultRow(v);
2178 }
2179
danielk1977e0048402004-06-15 16:51:01 +00002180pragma_out:
drh633e6d52008-07-28 19:34:53 +00002181 sqlite3DbFree(db, zLeft);
2182 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00002183}
drh2fcc1592016-12-15 20:59:03 +00002184#ifndef SQLITE_OMIT_VIRTUALTABLE
2185/*****************************************************************************
2186** Implementation of an eponymous virtual table that runs a pragma.
2187**
2188*/
2189typedef struct PragmaVtab PragmaVtab;
2190typedef struct PragmaVtabCursor PragmaVtabCursor;
2191struct PragmaVtab {
2192 sqlite3_vtab base; /* Base class. Must be first */
2193 sqlite3 *db; /* The database connection to which it belongs */
2194 const PragmaName *pName; /* Name of the pragma */
2195 u8 nHidden; /* Number of hidden columns */
2196 u8 iHidden; /* Index of the first hidden column */
2197};
2198struct PragmaVtabCursor {
2199 sqlite3_vtab_cursor base; /* Base class. Must be first */
2200 sqlite3_stmt *pPragma; /* The pragma statement to run */
2201 sqlite_int64 iRowid; /* Current rowid */
2202 char *azArg[2]; /* Value of the argument and schema */
2203};
2204
2205/*
2206** Pragma virtual table module xConnect method.
2207*/
2208static int pragmaVtabConnect(
2209 sqlite3 *db,
2210 void *pAux,
2211 int argc, const char *const*argv,
2212 sqlite3_vtab **ppVtab,
2213 char **pzErr
2214){
2215 const PragmaName *pPragma = (const PragmaName*)pAux;
2216 PragmaVtab *pTab = 0;
2217 int rc;
2218 int i, j;
2219 char cSep = '(';
2220 StrAccum acc;
2221 char zBuf[200];
2222
drh344a1bf2016-12-22 14:53:25 +00002223 UNUSED_PARAMETER(argc);
2224 UNUSED_PARAMETER(argv);
drh2fcc1592016-12-15 20:59:03 +00002225 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drh0cdbe1a2018-05-09 13:46:26 +00002226 sqlite3_str_appendall(&acc, "CREATE TABLE x");
drh2fcc1592016-12-15 20:59:03 +00002227 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
drh0cdbe1a2018-05-09 13:46:26 +00002228 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
drh2fcc1592016-12-15 20:59:03 +00002229 cSep = ',';
2230 }
drh9a63f092016-12-16 02:14:15 +00002231 if( i==0 ){
drh0cdbe1a2018-05-09 13:46:26 +00002232 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
drh9a63f092016-12-16 02:14:15 +00002233 i++;
2234 }
drh2fcc1592016-12-15 20:59:03 +00002235 j = 0;
2236 if( pPragma->mPragFlg & PragFlg_Result1 ){
drh0cdbe1a2018-05-09 13:46:26 +00002237 sqlite3_str_appendall(&acc, ",arg HIDDEN");
drh2fcc1592016-12-15 20:59:03 +00002238 j++;
2239 }
2240 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
drh0cdbe1a2018-05-09 13:46:26 +00002241 sqlite3_str_appendall(&acc, ",schema HIDDEN");
drh2fcc1592016-12-15 20:59:03 +00002242 j++;
2243 }
drh0cdbe1a2018-05-09 13:46:26 +00002244 sqlite3_str_append(&acc, ")", 1);
drh2fcc1592016-12-15 20:59:03 +00002245 sqlite3StrAccumFinish(&acc);
2246 assert( strlen(zBuf) < sizeof(zBuf)-1 );
2247 rc = sqlite3_declare_vtab(db, zBuf);
2248 if( rc==SQLITE_OK ){
2249 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
2250 if( pTab==0 ){
2251 rc = SQLITE_NOMEM;
2252 }else{
2253 memset(pTab, 0, sizeof(PragmaVtab));
2254 pTab->pName = pPragma;
2255 pTab->db = db;
2256 pTab->iHidden = i;
2257 pTab->nHidden = j;
2258 }
2259 }else{
2260 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2261 }
2262
2263 *ppVtab = (sqlite3_vtab*)pTab;
2264 return rc;
2265}
2266
2267/*
2268** Pragma virtual table module xDisconnect method.
2269*/
2270static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
2271 PragmaVtab *pTab = (PragmaVtab*)pVtab;
2272 sqlite3_free(pTab);
2273 return SQLITE_OK;
2274}
2275
2276/* Figure out the best index to use to search a pragma virtual table.
2277**
2278** There are not really any index choices. But we want to encourage the
2279** query planner to give == constraints on as many hidden parameters as
2280** possible, and especially on the first hidden parameter. So return a
2281** high cost if hidden parameters are unconstrained.
2282*/
2283static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2284 PragmaVtab *pTab = (PragmaVtab*)tab;
2285 const struct sqlite3_index_constraint *pConstraint;
2286 int i, j;
2287 int seen[2];
2288
drhae7045c2016-12-15 21:33:55 +00002289 pIdxInfo->estimatedCost = (double)1;
drh2fcc1592016-12-15 20:59:03 +00002290 if( pTab->nHidden==0 ){ return SQLITE_OK; }
2291 pConstraint = pIdxInfo->aConstraint;
2292 seen[0] = 0;
2293 seen[1] = 0;
2294 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2295 if( pConstraint->usable==0 ) continue;
2296 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2297 if( pConstraint->iColumn < pTab->iHidden ) continue;
2298 j = pConstraint->iColumn - pTab->iHidden;
2299 assert( j < 2 );
drhd7175eb2016-12-15 21:11:15 +00002300 seen[j] = i+1;
drh2fcc1592016-12-15 20:59:03 +00002301 }
2302 if( seen[0]==0 ){
2303 pIdxInfo->estimatedCost = (double)2147483647;
2304 pIdxInfo->estimatedRows = 2147483647;
2305 return SQLITE_OK;
2306 }
drhd7175eb2016-12-15 21:11:15 +00002307 j = seen[0]-1;
drh2fcc1592016-12-15 20:59:03 +00002308 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
2309 pIdxInfo->aConstraintUsage[j].omit = 1;
2310 if( seen[1]==0 ) return SQLITE_OK;
2311 pIdxInfo->estimatedCost = (double)20;
2312 pIdxInfo->estimatedRows = 20;
drhd7175eb2016-12-15 21:11:15 +00002313 j = seen[1]-1;
drh2fcc1592016-12-15 20:59:03 +00002314 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
2315 pIdxInfo->aConstraintUsage[j].omit = 1;
2316 return SQLITE_OK;
2317}
2318
2319/* Create a new cursor for the pragma virtual table */
2320static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
2321 PragmaVtabCursor *pCsr;
2322 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
2323 if( pCsr==0 ) return SQLITE_NOMEM;
2324 memset(pCsr, 0, sizeof(PragmaVtabCursor));
2325 pCsr->base.pVtab = pVtab;
2326 *ppCursor = &pCsr->base;
2327 return SQLITE_OK;
2328}
2329
2330/* Clear all content from pragma virtual table cursor. */
2331static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
2332 int i;
2333 sqlite3_finalize(pCsr->pPragma);
2334 pCsr->pPragma = 0;
2335 for(i=0; i<ArraySize(pCsr->azArg); i++){
2336 sqlite3_free(pCsr->azArg[i]);
2337 pCsr->azArg[i] = 0;
2338 }
2339}
2340
2341/* Close a pragma virtual table cursor */
2342static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
2343 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
2344 pragmaVtabCursorClear(pCsr);
drh9a63f092016-12-16 02:14:15 +00002345 sqlite3_free(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002346 return SQLITE_OK;
2347}
2348
2349/* Advance the pragma virtual table cursor to the next row */
2350static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
2351 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2352 int rc = SQLITE_OK;
2353
2354 /* Increment the xRowid value */
2355 pCsr->iRowid++;
drh9a63f092016-12-16 02:14:15 +00002356 assert( pCsr->pPragma );
2357 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
2358 rc = sqlite3_finalize(pCsr->pPragma);
2359 pCsr->pPragma = 0;
2360 pragmaVtabCursorClear(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002361 }
2362 return rc;
2363}
2364
2365/*
2366** Pragma virtual table module xFilter method.
2367*/
2368static int pragmaVtabFilter(
2369 sqlite3_vtab_cursor *pVtabCursor,
2370 int idxNum, const char *idxStr,
2371 int argc, sqlite3_value **argv
2372){
2373 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2374 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2375 int rc;
drhd8b72002016-12-16 04:20:27 +00002376 int i, j;
drh2fcc1592016-12-15 20:59:03 +00002377 StrAccum acc;
2378 char *zSql;
2379
drh344a1bf2016-12-22 14:53:25 +00002380 UNUSED_PARAMETER(idxNum);
2381 UNUSED_PARAMETER(idxStr);
drh2fcc1592016-12-15 20:59:03 +00002382 pragmaVtabCursorClear(pCsr);
drhd8b72002016-12-16 04:20:27 +00002383 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
2384 for(i=0; i<argc; i++, j++){
dand8ecefa2017-07-15 20:48:30 +00002385 const char *zText = (const char*)sqlite3_value_text(argv[i]);
drhd8b72002016-12-16 04:20:27 +00002386 assert( j<ArraySize(pCsr->azArg) );
dand8ecefa2017-07-15 20:48:30 +00002387 assert( pCsr->azArg[j]==0 );
2388 if( zText ){
2389 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
2390 if( pCsr->azArg[j]==0 ){
2391 return SQLITE_NOMEM;
2392 }
drh2fcc1592016-12-15 20:59:03 +00002393 }
2394 }
drhd7175eb2016-12-15 21:11:15 +00002395 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
drh0cdbe1a2018-05-09 13:46:26 +00002396 sqlite3_str_appendall(&acc, "PRAGMA ");
drh2fcc1592016-12-15 20:59:03 +00002397 if( pCsr->azArg[1] ){
drh0cdbe1a2018-05-09 13:46:26 +00002398 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
drh2fcc1592016-12-15 20:59:03 +00002399 }
drh0cdbe1a2018-05-09 13:46:26 +00002400 sqlite3_str_appendall(&acc, pTab->pName->zName);
drh2fcc1592016-12-15 20:59:03 +00002401 if( pCsr->azArg[0] ){
drh0cdbe1a2018-05-09 13:46:26 +00002402 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
drh2fcc1592016-12-15 20:59:03 +00002403 }
2404 zSql = sqlite3StrAccumFinish(&acc);
2405 if( zSql==0 ) return SQLITE_NOMEM;
2406 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
2407 sqlite3_free(zSql);
2408 if( rc!=SQLITE_OK ){
2409 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
2410 return rc;
2411 }
2412 return pragmaVtabNext(pVtabCursor);
2413}
2414
2415/*
2416** Pragma virtual table module xEof method.
2417*/
2418static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
2419 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2420 return (pCsr->pPragma==0);
2421}
2422
2423/* The xColumn method simply returns the corresponding column from
2424** the PRAGMA.
2425*/
2426static int pragmaVtabColumn(
2427 sqlite3_vtab_cursor *pVtabCursor,
2428 sqlite3_context *ctx,
2429 int i
2430){
2431 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2432 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2433 if( i<pTab->iHidden ){
2434 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
2435 }else{
2436 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
2437 }
2438 return SQLITE_OK;
2439}
2440
2441/*
2442** Pragma virtual table module xRowid method.
2443*/
2444static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
2445 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2446 *p = pCsr->iRowid;
2447 return SQLITE_OK;
2448}
2449
2450/* The pragma virtual table object */
2451static const sqlite3_module pragmaVtabModule = {
2452 0, /* iVersion */
2453 0, /* xCreate - create a table */
2454 pragmaVtabConnect, /* xConnect - connect to an existing table */
2455 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
2456 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
2457 0, /* xDestroy - Drop a table */
2458 pragmaVtabOpen, /* xOpen - open a cursor */
2459 pragmaVtabClose, /* xClose - close a cursor */
2460 pragmaVtabFilter, /* xFilter - configure scan constraints */
2461 pragmaVtabNext, /* xNext - advance a cursor */
2462 pragmaVtabEof, /* xEof */
2463 pragmaVtabColumn, /* xColumn - read data */
2464 pragmaVtabRowid, /* xRowid - read data */
2465 0, /* xUpdate - write data */
2466 0, /* xBegin - begin transaction */
2467 0, /* xSync - sync transaction */
2468 0, /* xCommit - commit transaction */
2469 0, /* xRollback - rollback transaction */
2470 0, /* xFindFunction - function overloading */
2471 0, /* xRename - rename the table */
2472 0, /* xSavepoint */
2473 0, /* xRelease */
drh84c501b2018-11-05 23:01:45 +00002474 0, /* xRollbackTo */
2475 0 /* xShadowName */
drh2fcc1592016-12-15 20:59:03 +00002476};
2477
2478/*
2479** Check to see if zTabName is really the name of a pragma. If it is,
2480** then register an eponymous virtual table for that pragma and return
2481** a pointer to the Module object for the new virtual table.
2482*/
2483Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
2484 const PragmaName *pName;
2485 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
2486 pName = pragmaLocate(zName+7);
2487 if( pName==0 ) return 0;
2488 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
2489 assert( sqlite3HashFind(&db->aModule, zName)==0 );
drhd7175eb2016-12-15 21:11:15 +00002490 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
drh2fcc1592016-12-15 20:59:03 +00002491}
2492
2493#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh13d70422004-11-13 15:59:14 +00002494
drh8bfdf722009-06-19 14:06:03 +00002495#endif /* SQLITE_OMIT_PRAGMA */