blob: 151a7a2e82f3c1aa471c0261f1ee83673f10f973 [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**
301** Generate code to output a single-column result row with the result
302** held in register regResult. Decrement the result count and halt if
303** the maximum number of result rows have been issued.
304*/
305static int integrityCheckResultRow(Vdbe *v, int regResult){
306 int addr;
307 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 1);
308 addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
309 VdbeCoverage(v);
310 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
311 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
518 ** PRAGMA [schema.]secure_delete=ON/OFF
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
522 ** flag setting and reports thenew value.
523 */
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 ){
drh38d9c612012-01-31 14:24:47 +0000529 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000530 }
drhaf034ed2010-02-12 19:46:26 +0000531 if( pId2->n==0 && b>=0 ){
532 int ii;
533 for(ii=0; ii<db->nDb; ii++){
534 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
535 }
536 }
drh5b47efa2010-02-12 18:18:39 +0000537 b = sqlite3BtreeSecureDelete(pBt, b);
drhc232aca2016-12-15 16:01:17 +0000538 returnSingleInt(v, b);
drh9ccd8652013-09-13 16:36:46 +0000539 break;
540 }
drh5b47efa2010-02-12 18:18:39 +0000541
542 /*
drh9b0cf342015-11-12 14:57:19 +0000543 ** PRAGMA [schema.]max_page_count
544 ** PRAGMA [schema.]max_page_count=N
drh60ac3f42010-11-23 18:59:27 +0000545 **
546 ** The first form reports the current setting for the
547 ** maximum number of pages in the database file. The
548 ** second form attempts to change this setting. Both
549 ** forms return the current setting.
550 **
drhe73c9142011-11-09 16:12:24 +0000551 ** The absolute value of N is used. This is undocumented and might
552 ** change. The only purpose is to provide an easy way to test
553 ** the sqlite3AbsInt32() function.
554 **
drh9b0cf342015-11-12 14:57:19 +0000555 ** PRAGMA [schema.]page_count
danielk197759a93792008-05-15 17:48:20 +0000556 **
557 ** Return the number of pages in the specified database.
558 */
drh9ccd8652013-09-13 16:36:46 +0000559 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000560 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000561 sqlite3CodeVerifySchema(pParse, iDb);
562 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000563 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000564 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
565 }else{
drhe73c9142011-11-09 16:12:24 +0000566 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
567 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000568 }
danielk197759a93792008-05-15 17:48:20 +0000569 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
drh9ccd8652013-09-13 16:36:46 +0000570 break;
571 }
danielk197759a93792008-05-15 17:48:20 +0000572
573 /*
drh9b0cf342015-11-12 14:57:19 +0000574 ** PRAGMA [schema.]locking_mode
575 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
danielk197741483462007-03-24 16:45:04 +0000576 */
drh9ccd8652013-09-13 16:36:46 +0000577 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000578 const char *zRet = "normal";
579 int eMode = getLockingMode(zRight);
580
581 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
582 /* Simple "PRAGMA locking_mode;" statement. This is a query for
583 ** the current default locking mode (which may be different to
584 ** the locking-mode of the main database).
585 */
586 eMode = db->dfltLockMode;
587 }else{
588 Pager *pPager;
589 if( pId2->n==0 ){
590 /* This indicates that no database name was specified as part
591 ** of the PRAGMA command. In this case the locking-mode must be
592 ** set on all attached databases, as well as the main db file.
593 **
594 ** Also, the sqlite3.dfltLockMode variable is set so that
595 ** any subsequently attached databases also use the specified
596 ** locking mode.
597 */
598 int ii;
599 assert(pDb==&db->aDb[0]);
600 for(ii=2; ii<db->nDb; ii++){
601 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
602 sqlite3PagerLockingMode(pPager, eMode);
603 }
drh4f21c4a2008-12-10 22:15:00 +0000604 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000605 }
606 pPager = sqlite3BtreePager(pDb->pBt);
607 eMode = sqlite3PagerLockingMode(pPager, eMode);
608 }
609
drh9ccd8652013-09-13 16:36:46 +0000610 assert( eMode==PAGER_LOCKINGMODE_NORMAL
611 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000612 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
613 zRet = "exclusive";
614 }
drhc232aca2016-12-15 16:01:17 +0000615 returnSingleText(v, zRet);
drh9ccd8652013-09-13 16:36:46 +0000616 break;
617 }
drh3b020132008-04-17 17:02:01 +0000618
619 /*
drh9b0cf342015-11-12 14:57:19 +0000620 ** PRAGMA [schema.]journal_mode
621 ** PRAGMA [schema.]journal_mode =
drh3ebaee92010-05-06 21:37:22 +0000622 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000623 */
drh9ccd8652013-09-13 16:36:46 +0000624 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000625 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
626 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000627
drh3b020132008-04-17 17:02:01 +0000628 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000629 /* If there is no "=MODE" part of the pragma, do a query for the
630 ** current mode */
drh3b020132008-04-17 17:02:01 +0000631 eMode = PAGER_JOURNALMODE_QUERY;
632 }else{
dane04dc882010-04-20 18:53:15 +0000633 const char *zMode;
drhea678832008-12-10 19:26:22 +0000634 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000635 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000636 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
637 }
638 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000639 /* If the "=MODE" part does not match any known journal mode,
640 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000641 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000642 }
643 }
drhc6b2a0f2010-07-08 17:40:37 +0000644 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
645 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
646 iDb = 0;
647 pId2->n = 1;
648 }
649 for(ii=db->nDb-1; ii>=0; ii--){
650 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
651 sqlite3VdbeUsesBtree(v, ii);
652 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000653 }
drh3b020132008-04-17 17:02:01 +0000654 }
drh3b020132008-04-17 17:02:01 +0000655 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000656 break;
657 }
danielk1977b53e4962008-06-04 06:45:59 +0000658
659 /*
drh9b0cf342015-11-12 14:57:19 +0000660 ** PRAGMA [schema.]journal_size_limit
661 ** PRAGMA [schema.]journal_size_limit=N
danielk1977b53e4962008-06-04 06:45:59 +0000662 **
drha9e364f2009-01-13 20:14:15 +0000663 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000664 */
drh9ccd8652013-09-13 16:36:46 +0000665 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000666 Pager *pPager = sqlite3BtreePager(pDb->pBt);
667 i64 iLimit = -2;
668 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000669 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000670 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000671 }
672 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drhc232aca2016-12-15 16:01:17 +0000673 returnSingleInt(v, iLimit);
drh9ccd8652013-09-13 16:36:46 +0000674 break;
675 }
danielk1977b53e4962008-06-04 06:45:59 +0000676
drh13d70422004-11-13 15:59:14 +0000677#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000678
679 /*
drh9b0cf342015-11-12 14:57:19 +0000680 ** PRAGMA [schema.]auto_vacuum
681 ** PRAGMA [schema.]auto_vacuum=N
danielk1977951af802004-11-05 15:45:09 +0000682 **
drha9e364f2009-01-13 20:14:15 +0000683 ** Get or set the value of the database 'auto-vacuum' parameter.
684 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000685 */
686#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000687 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000688 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000689 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000690 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000691 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000692 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000693 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000694 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000695 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000696 /* Call SetAutoVacuum() to set initialize the internal auto and
697 ** incr-vacuum flags. This is required in case this connection
698 ** creates the database file. It is important that it is created
699 ** as an auto-vacuum capable db.
700 */
701 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
702 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
703 /* When setting the auto_vacuum mode to either "full" or
704 ** "incremental", write the value of meta[6] in the database
705 ** file. Before writing to meta[6], check that meta[3] indicates
706 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000707 */
drhb06a4ec2014-03-10 18:03:09 +0000708 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000709 static const VdbeOpList setMeta6[] = {
710 { OP_Transaction, 0, 1, 0}, /* 0 */
711 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
712 { OP_If, 1, 0, 0}, /* 2 */
713 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
drh1861afc2016-02-01 21:48:34 +0000714 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
drhf63936e2013-10-03 14:08:07 +0000715 };
drh2ce18652016-01-16 20:50:21 +0000716 VdbeOp *aOp;
717 int iAddr = sqlite3VdbeCurrentAddr(v);
drhdad300d2016-01-18 00:20:26 +0000718 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
drh2ce18652016-01-16 20:50:21 +0000719 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhdad300d2016-01-18 00:20:26 +0000720 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000721 aOp[0].p1 = iDb;
722 aOp[1].p1 = iDb;
723 aOp[2].p2 = iAddr+4;
drh1861afc2016-02-01 21:48:34 +0000724 aOp[4].p1 = iDb;
725 aOp[4].p3 = eAuto - 1;
drhf63936e2013-10-03 14:08:07 +0000726 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000727 }
danielk1977951af802004-11-05 15:45:09 +0000728 }
drh9ccd8652013-09-13 16:36:46 +0000729 break;
730 }
danielk1977951af802004-11-05 15:45:09 +0000731#endif
732
drhca5557f2007-05-04 18:30:40 +0000733 /*
drh9b0cf342015-11-12 14:57:19 +0000734 ** PRAGMA [schema.]incremental_vacuum(N)
drhca5557f2007-05-04 18:30:40 +0000735 **
736 ** Do N steps of incremental vacuuming on a database.
737 */
738#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000739 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000740 int iLimit, addr;
741 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
742 iLimit = 0x7fffffff;
743 }
744 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000745 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000746 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000747 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000748 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000749 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000750 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000751 break;
752 }
drhca5557f2007-05-04 18:30:40 +0000753#endif
754
drh13d70422004-11-13 15:59:14 +0000755#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000756 /*
drh9b0cf342015-11-12 14:57:19 +0000757 ** PRAGMA [schema.]cache_size
758 ** PRAGMA [schema.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000759 **
760 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000761 ** page cache size. The second form sets the local
762 ** page cache size value. If N is positive then that is the
763 ** number of pages in the cache. If N is negative, then the
764 ** number of pages is adjusted so that the cache uses -N kibibytes
765 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000766 */
drh9ccd8652013-09-13 16:36:46 +0000767 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000768 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000769 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000770 returnSingleInt(v, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000771 }else{
drh3b42abb2011-11-09 14:23:04 +0000772 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000773 pDb->pSchema->cache_size = size;
774 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000775 }
drh9ccd8652013-09-13 16:36:46 +0000776 break;
777 }
drhc11d4f92003-04-06 21:08:24 +0000778
779 /*
drh9b0cf342015-11-12 14:57:19 +0000780 ** PRAGMA [schema.]cache_spill
781 ** PRAGMA cache_spill=BOOLEAN
782 ** PRAGMA [schema.]cache_spill=N
783 **
784 ** The first form reports the current local setting for the
785 ** page cache spill size. The second form turns cache spill on
786 ** or off. When turnning cache spill on, the size is set to the
787 ** current cache_size. The third form sets a spill size that
788 ** may be different form the cache size.
789 ** If N is positive then that is the
790 ** number of pages in the cache. If N is negative, then the
791 ** number of pages is adjusted so that the cache uses -N kibibytes
792 ** of memory.
793 **
794 ** If the number of cache_spill pages is less then the number of
795 ** cache_size pages, no spilling occurs until the page count exceeds
796 ** the number of cache_size pages.
797 **
798 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
799 ** not just the schema specified.
800 */
801 case PragTyp_CACHE_SPILL: {
drh9b0cf342015-11-12 14:57:19 +0000802 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
803 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000804 returnSingleInt(v,
drh9b0cf342015-11-12 14:57:19 +0000805 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
806 sqlite3BtreeSetSpillSize(pDb->pBt,0));
807 }else{
drh4f9c8ec2015-11-12 15:47:48 +0000808 int size = 1;
drh9b0cf342015-11-12 14:57:19 +0000809 if( sqlite3GetInt32(zRight, &size) ){
810 sqlite3BtreeSetSpillSize(pDb->pBt, size);
811 }
drh4f9c8ec2015-11-12 15:47:48 +0000812 if( sqlite3GetBoolean(zRight, size!=0) ){
drh9b0cf342015-11-12 14:57:19 +0000813 db->flags |= SQLITE_CacheSpill;
814 }else{
815 db->flags &= ~SQLITE_CacheSpill;
816 }
drh4f9c8ec2015-11-12 15:47:48 +0000817 setAllPagerFlags(db);
drh9b0cf342015-11-12 14:57:19 +0000818 }
819 break;
820 }
821
822 /*
823 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000824 **
drh0d0614b2013-03-25 23:09:28 +0000825 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000826 ** used to limit the aggregate size of all memory mapped regions of the
827 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000828 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000829 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000830 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000831 **
drh0d0614b2013-03-25 23:09:28 +0000832 ** This value is advisory. The underlying VFS is free to memory map
833 ** as little or as much as it wants. Except, if N is set to 0 then the
834 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000835 */
drh9ccd8652013-09-13 16:36:46 +0000836 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000837 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000838#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000839 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000840 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000841 int ii;
drh9296c182014-07-23 13:40:49 +0000842 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000843 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
844 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000845 for(ii=db->nDb-1; ii>=0; ii--){
846 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000847 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000848 }
849 }
dan5d8a1372013-03-19 19:28:06 +0000850 }
drh9b4c59f2013-04-15 17:03:42 +0000851 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000852 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000853#else
dan3719f5f2013-05-23 10:13:18 +0000854 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000855 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000856#endif
dan3719f5f2013-05-23 10:13:18 +0000857 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000858 returnSingleInt(v, sz);
dan3719f5f2013-05-23 10:13:18 +0000859 }else if( rc!=SQLITE_NOTFOUND ){
860 pParse->nErr++;
861 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000862 }
drh9ccd8652013-09-13 16:36:46 +0000863 break;
864 }
dan5d8a1372013-03-19 19:28:06 +0000865
866 /*
drh90f5ecb2004-07-22 01:19:35 +0000867 ** PRAGMA temp_store
868 ** PRAGMA temp_store = "default"|"memory"|"file"
869 **
870 ** Return or set the local value of the temp_store flag. Changing
871 ** the local value does not make changes to the disk file and the default
872 ** value will be restored the next time the database is opened.
873 **
874 ** Note that it is possible for the library compile-time options to
875 ** override this setting
876 */
drh9ccd8652013-09-13 16:36:46 +0000877 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000878 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000879 returnSingleInt(v, db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000880 }else{
881 changeTempStorage(pParse, zRight);
882 }
drh9ccd8652013-09-13 16:36:46 +0000883 break;
884 }
drh90f5ecb2004-07-22 01:19:35 +0000885
886 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000887 ** PRAGMA temp_store_directory
888 ** PRAGMA temp_store_directory = ""|"directory_name"
889 **
890 ** Return or set the local value of the temp_store_directory flag. Changing
891 ** the value sets a specific directory to be used for temporary files.
892 ** Setting to a null string reverts to the default temporary directory search.
893 ** If temporary directory is changed, then invalidateTempStorage.
894 **
895 */
drh9ccd8652013-09-13 16:36:46 +0000896 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000897 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000898 returnSingleText(v, sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000899 }else{
drh78f82d12008-09-02 00:52:52 +0000900#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000901 if( zRight[0] ){
902 int res;
danielk1977fab11272008-09-16 14:38:02 +0000903 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
904 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000905 sqlite3ErrorMsg(pParse, "not a writable directory");
906 goto pragma_out;
907 }
drh268283b2005-01-08 15:44:25 +0000908 }
danielk1977b06a0b62008-06-26 10:54:12 +0000909 if( SQLITE_TEMP_STORE==0
910 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
911 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000912 ){
913 invalidateTempStorage(pParse);
914 }
drh17435752007-08-16 04:30:38 +0000915 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000916 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000917 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000918 }else{
drh268283b2005-01-08 15:44:25 +0000919 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000920 }
drh78f82d12008-09-02 00:52:52 +0000921#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000922 }
drh9ccd8652013-09-13 16:36:46 +0000923 break;
924 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000925
drhcc716452012-06-06 23:23:23 +0000926#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000927 /*
928 ** PRAGMA data_store_directory
929 ** PRAGMA data_store_directory = ""|"directory_name"
930 **
931 ** Return or set the local value of the data_store_directory flag. Changing
932 ** the value sets a specific directory to be used for database files that
933 ** were specified with a relative pathname. Setting to a null string reverts
934 ** to the default database directory, which for database files specified with
935 ** a relative path will probably be based on the current directory for the
936 ** process. Database file specified with an absolute path are not impacted
937 ** by this setting, regardless of its value.
938 **
939 */
drh9ccd8652013-09-13 16:36:46 +0000940 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +0000941 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000942 returnSingleText(v, sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +0000943 }else{
944#ifndef SQLITE_OMIT_WSD
945 if( zRight[0] ){
946 int res;
947 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
948 if( rc!=SQLITE_OK || res==0 ){
949 sqlite3ErrorMsg(pParse, "not a writable directory");
950 goto pragma_out;
951 }
952 }
953 sqlite3_free(sqlite3_data_directory);
954 if( zRight[0] ){
955 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
956 }else{
957 sqlite3_data_directory = 0;
958 }
959#endif /* SQLITE_OMIT_WSD */
960 }
drh9ccd8652013-09-13 16:36:46 +0000961 break;
962 }
drhcc716452012-06-06 23:23:23 +0000963#endif
mistachkina112d142012-03-14 00:44:01 +0000964
drhd2cb50b2009-01-09 21:41:17 +0000965#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000966 /*
drh9b0cf342015-11-12 14:57:19 +0000967 ** PRAGMA [schema.]lock_proxy_file
968 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +0000969 **
970 ** Return or set the value of the lock_proxy_file flag. Changing
971 ** the value sets a specific file to be used for database access locks.
972 **
973 */
974 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +0000975 if( !zRight ){
976 Pager *pPager = sqlite3BtreePager(pDb->pBt);
977 char *proxy_file_path = NULL;
978 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000979 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000980 &proxy_file_path);
drhc232aca2016-12-15 16:01:17 +0000981 returnSingleText(v, proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +0000982 }else{
983 Pager *pPager = sqlite3BtreePager(pDb->pBt);
984 sqlite3_file *pFile = sqlite3PagerFile(pPager);
985 int res;
986 if( zRight[0] ){
987 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
988 zRight);
989 } else {
990 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
991 NULL);
992 }
993 if( res!=SQLITE_OK ){
994 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
995 goto pragma_out;
996 }
997 }
drh9ccd8652013-09-13 16:36:46 +0000998 break;
999 }
drhd2cb50b2009-01-09 21:41:17 +00001000#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +00001001
1002 /*
drh9b0cf342015-11-12 14:57:19 +00001003 ** PRAGMA [schema.]synchronous
drh6841b1c2016-02-03 19:20:15 +00001004 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
drhc11d4f92003-04-06 21:08:24 +00001005 **
1006 ** Return or set the local value of the synchronous flag. Changing
1007 ** the local value does not make changes to the disk file and the
1008 ** default value will be restored the next time the database is
1009 ** opened.
1010 */
drh9ccd8652013-09-13 16:36:46 +00001011 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +00001012 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +00001013 returnSingleInt(v, pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +00001014 }else{
danielk197791cf71b2004-06-26 06:37:06 +00001015 if( !db->autoCommit ){
1016 sqlite3ErrorMsg(pParse,
1017 "Safety level may not be changed inside a transaction");
1018 }else{
drhd99d2832015-04-17 15:58:33 +00001019 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
1020 if( iLevel==0 ) iLevel = 1;
1021 pDb->safety_level = iLevel;
drh50a1a5a2016-03-08 14:40:11 +00001022 pDb->bSyncSet = 1;
drhd3605a42013-08-17 15:42:29 +00001023 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001024 }
drhc11d4f92003-04-06 21:08:24 +00001025 }
drh9ccd8652013-09-13 16:36:46 +00001026 break;
1027 }
drh13d70422004-11-13 15:59:14 +00001028#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001029
drhbf216272005-02-26 18:10:44 +00001030#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001031 case PragTyp_FLAG: {
1032 if( zRight==0 ){
drhc232aca2016-12-15 16:01:17 +00001033 setPragmaResultColumnNames(v, pPragma);
1034 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001035 }else{
drhc228be52015-01-31 02:00:01 +00001036 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001037 if( db->autoCommit==0 ){
1038 /* Foreign key support may not be enabled or disabled while not
1039 ** in auto-commit mode. */
1040 mask &= ~(SQLITE_ForeignKeys);
1041 }
drhd4530972014-09-09 14:47:53 +00001042#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001043 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001044 /* Do not allow non-admin users to modify the schema arbitrarily */
1045 mask &= ~(SQLITE_WriteSchema);
1046 }
1047#endif
drh9ccd8652013-09-13 16:36:46 +00001048
1049 if( sqlite3GetBoolean(zRight, 0) ){
1050 db->flags |= mask;
1051 }else{
1052 db->flags &= ~mask;
1053 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1054 }
1055
1056 /* Many of the flag-pragmas modify the code generated by the SQL
1057 ** compiler (eg. count_changes). So add an opcode to expire all
1058 ** compiled SQL statements after modifying a pragma value.
1059 */
drh01c736d2016-05-20 15:15:07 +00001060 sqlite3VdbeAddOp0(v, OP_Expire);
drh9ccd8652013-09-13 16:36:46 +00001061 setAllPagerFlags(db);
1062 }
1063 break;
1064 }
drhbf216272005-02-26 18:10:44 +00001065#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001066
drh13d70422004-11-13 15:59:14 +00001067#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001068 /*
1069 ** PRAGMA table_info(<table>)
1070 **
1071 ** Return a single row for each column of the named table. The columns of
1072 ** the returned data set are:
1073 **
1074 ** cid: Column id (numbered from left to right, starting at 0)
1075 ** name: Column name
1076 ** type: Column declaration type.
1077 ** notnull: True if 'NOT NULL' is part of column declaration
1078 ** dflt_value: The default value for the column, if any.
1079 */
drh9ccd8652013-09-13 16:36:46 +00001080 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001081 Table *pTab;
drh4d249e62016-06-10 22:49:01 +00001082 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001083 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001084 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001085 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001086 Column *pCol;
drh44156282013-10-23 22:23:03 +00001087 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001088 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001089 sqlite3CodeVerifySchema(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +00001090 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001091 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001092 if( IsHiddenColumn(pCol) ){
1093 nHidden++;
1094 continue;
1095 }
drh384b7fe2013-01-01 13:55:31 +00001096 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1097 k = 0;
1098 }else if( pPk==0 ){
1099 k = 1;
1100 }else{
drh1b678962015-04-15 07:19:27 +00001101 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001102 }
drh94fa9c42016-02-27 21:16:04 +00001103 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
drh076e85f2015-09-03 13:46:12 +00001104 sqlite3VdbeMultiLoad(v, 1, "issisi",
1105 i-nHidden,
drhd7564862016-03-22 20:05:09 +00001106 pCol->zName,
1107 sqlite3ColumnType(pCol,""),
drh076e85f2015-09-03 13:46:12 +00001108 pCol->notNull ? 1 : 0,
drh94fa9c42016-02-27 21:16:04 +00001109 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
drh076e85f2015-09-03 13:46:12 +00001110 k);
drh2d401ab2008-01-10 23:50:11 +00001111 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001112 }
1113 }
drh9ccd8652013-09-13 16:36:46 +00001114 }
1115 break;
drhc11d4f92003-04-06 21:08:24 +00001116
drh3ef26152013-10-12 20:22:00 +00001117 case PragTyp_STATS: {
1118 Index *pIdx;
1119 HashElem *i;
drh3ef26152013-10-12 20:22:00 +00001120 pParse->nMem = 4;
1121 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001122 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1123 Table *pTab = sqliteHashData(i);
drh076e85f2015-09-03 13:46:12 +00001124 sqlite3VdbeMultiLoad(v, 1, "ssii",
1125 pTab->zName,
1126 0,
drhd566c952016-02-25 21:19:03 +00001127 pTab->szTabRow,
1128 pTab->nRowLogEst);
drh3ef26152013-10-12 20:22:00 +00001129 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1130 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh076e85f2015-09-03 13:46:12 +00001131 sqlite3VdbeMultiLoad(v, 2, "sii",
1132 pIdx->zName,
drhd566c952016-02-25 21:19:03 +00001133 pIdx->szIdxRow,
1134 pIdx->aiRowLogEst[0]);
drh3ef26152013-10-12 20:22:00 +00001135 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1136 }
1137 }
1138 }
1139 break;
1140
drh9ccd8652013-09-13 16:36:46 +00001141 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001142 Index *pIdx;
1143 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001144 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001145 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001146 int i;
drh5e7028c2015-03-05 14:29:02 +00001147 int mx;
1148 if( pPragma->iArg ){
1149 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1150 mx = pIdx->nColumn;
1151 pParse->nMem = 6;
1152 }else{
1153 /* PRAGMA index_info (legacy version) */
1154 mx = pIdx->nKeyCol;
1155 pParse->nMem = 3;
1156 }
drhc11d4f92003-04-06 21:08:24 +00001157 pTab = pIdx->pTable;
drhc95e01d2013-02-14 16:16:05 +00001158 sqlite3CodeVerifySchema(pParse, iDb);
drhc232aca2016-12-15 16:01:17 +00001159 assert( pParse->nMem<=pPragma->nPragCName );
drhc228be52015-01-31 02:00:01 +00001160 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001161 i16 cnum = pIdx->aiColumn[i];
drh076e85f2015-09-03 13:46:12 +00001162 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum,
1163 cnum<0 ? 0 : pTab->aCol[cnum].zName);
drh5e7028c2015-03-05 14:29:02 +00001164 if( pPragma->iArg ){
drh076e85f2015-09-03 13:46:12 +00001165 sqlite3VdbeMultiLoad(v, 4, "isi",
1166 pIdx->aSortOrder[i],
1167 pIdx->azColl[i],
1168 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001169 }
1170 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001171 }
1172 }
drh9ccd8652013-09-13 16:36:46 +00001173 }
1174 break;
drhc11d4f92003-04-06 21:08:24 +00001175
drh9ccd8652013-09-13 16:36:46 +00001176 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001177 Index *pIdx;
1178 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001179 int i;
drh2783e4b2004-10-05 15:42:53 +00001180 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001181 if( pTab ){
drhc228be52015-01-31 02:00:01 +00001182 pParse->nMem = 5;
drhe13e9f52013-10-05 19:18:00 +00001183 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001184 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001185 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001186 sqlite3VdbeMultiLoad(v, 1, "isisi",
1187 i,
1188 pIdx->zName,
1189 IsUniqueIndex(pIdx),
1190 azOrigin[pIdx->idxType],
1191 pIdx->pPartIdxWhere!=0);
drhc228be52015-01-31 02:00:01 +00001192 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drhc11d4f92003-04-06 21:08:24 +00001193 }
1194 }
drh9ccd8652013-09-13 16:36:46 +00001195 }
1196 break;
drhc11d4f92003-04-06 21:08:24 +00001197
drh9ccd8652013-09-13 16:36:46 +00001198 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001199 int i;
drh2d401ab2008-01-10 23:50:11 +00001200 pParse->nMem = 3;
drh13d70422004-11-13 15:59:14 +00001201 for(i=0; i<db->nDb; i++){
1202 if( db->aDb[i].pBt==0 ) continue;
drh69c33822016-08-18 14:33:11 +00001203 assert( db->aDb[i].zDbSName!=0 );
drh076e85f2015-09-03 13:46:12 +00001204 sqlite3VdbeMultiLoad(v, 1, "iss",
1205 i,
drh69c33822016-08-18 14:33:11 +00001206 db->aDb[i].zDbSName,
drh076e85f2015-09-03 13:46:12 +00001207 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh2d401ab2008-01-10 23:50:11 +00001208 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001209 }
drh9ccd8652013-09-13 16:36:46 +00001210 }
1211 break;
danielk197748af65a2005-02-09 03:20:37 +00001212
drh9ccd8652013-09-13 16:36:46 +00001213 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001214 int i = 0;
1215 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001216 pParse->nMem = 2;
danielk197748af65a2005-02-09 03:20:37 +00001217 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1218 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001219 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
drh2d401ab2008-01-10 23:50:11 +00001220 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001221 }
drh9ccd8652013-09-13 16:36:46 +00001222 }
1223 break;
drh13d70422004-11-13 15:59:14 +00001224#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1225
drhb7f91642004-10-31 02:22:47 +00001226#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001227 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001228 FKey *pFK;
1229 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001230 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001231 if( pTab ){
drh78100cc2003-08-23 22:40:53 +00001232 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001233 if( pFK ){
1234 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001235 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001236 sqlite3CodeVerifySchema(pParse, iDb);
danielk1977742f9472004-06-16 12:02:43 +00001237 while(pFK){
1238 int j;
1239 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001240 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1241 i,
1242 j,
1243 pFK->zTo,
1244 pTab->aCol[pFK->aCol[j].iFrom].zName,
1245 pFK->aCol[j].zCol,
1246 actionName(pFK->aAction[1]), /* ON UPDATE */
1247 actionName(pFK->aAction[0]), /* ON DELETE */
1248 "NONE");
danielk197750af3e12008-10-10 17:47:21 +00001249 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001250 }
1251 ++i;
1252 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001253 }
drh78100cc2003-08-23 22:40:53 +00001254 }
1255 }
drh9ccd8652013-09-13 16:36:46 +00001256 }
1257 break;
drhb7f91642004-10-31 02:22:47 +00001258#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001259
drh6c5b9152012-12-17 16:46:37 +00001260#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001261#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001262 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001263 FKey *pFK; /* A foreign key constraint */
1264 Table *pTab; /* Child table contain "REFERENCES" keyword */
1265 Table *pParent; /* Parent table that child points to */
1266 Index *pIdx; /* Index in the parent table */
1267 int i; /* Loop counter: Foreign key number for pTab */
1268 int j; /* Loop counter: Field of the foreign key */
1269 HashElem *k; /* Loop counter: Next table in schema */
1270 int x; /* result variable */
1271 int regResult; /* 3 registers to hold a result row */
1272 int regKey; /* Register to hold key for checking the FK */
1273 int regRow; /* Registers to hold a row from pTab */
1274 int addrTop; /* Top of a loop checking foreign keys */
1275 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001276 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001277
drh613028b2012-12-17 18:43:02 +00001278 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001279 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001280 regKey = ++pParse->nMem;
1281 regRow = ++pParse->nMem;
drh613028b2012-12-17 18:43:02 +00001282 sqlite3CodeVerifySchema(pParse, iDb);
1283 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1284 while( k ){
1285 if( zRight ){
1286 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1287 k = 0;
1288 }else{
1289 pTab = (Table*)sqliteHashData(k);
1290 k = sqliteHashNext(k);
1291 }
drh9148def2012-12-17 20:40:39 +00001292 if( pTab==0 || pTab->pFKey==0 ) continue;
1293 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001294 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001295 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001296 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh6c5b9152012-12-17 16:46:37 +00001297 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001298 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1299 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001300 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001301 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001302 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001303 if( x==0 ){
1304 if( pIdx==0 ){
1305 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1306 }else{
drh6c5b9152012-12-17 16:46:37 +00001307 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001308 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001309 }
1310 }else{
drh613028b2012-12-17 18:43:02 +00001311 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001312 break;
1313 }
drh6c5b9152012-12-17 16:46:37 +00001314 }
dan5e878302013-10-12 19:06:48 +00001315 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001316 if( pFK ) break;
1317 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001318 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001319 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001320 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001321 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001322 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001323 if( pParent ){
1324 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1325 assert( x==0 );
1326 }
drh613028b2012-12-17 18:43:02 +00001327 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001328 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001329 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001330 assert( iKey>=0 && iKey<pTab->nCol );
1331 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001332 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1333 sqlite3ColumnDefault(v, pTab, iKey, regRow);
drh688852a2014-02-17 22:40:43 +00001334 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001335 }else{
drh613028b2012-12-17 18:43:02 +00001336 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001337 }
drheeb95652016-05-26 20:56:38 +00001338 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, 0, regRow); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001339 sqlite3VdbeGoto(v, addrOk);
drh613028b2012-12-17 18:43:02 +00001340 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1341 }else{
1342 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001343 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001344 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh688852a2014-02-17 22:40:43 +00001345 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001346 }
dan5e878302013-10-12 19:06:48 +00001347 if( pParent ){
drh57bf4a82014-02-17 14:59:22 +00001348 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
drhe9107692015-08-25 19:20:04 +00001349 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
dan5e878302013-10-12 19:06:48 +00001350 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh688852a2014-02-17 22:40:43 +00001351 VdbeCoverage(v);
dan5e878302013-10-12 19:06:48 +00001352 }
drh6c5b9152012-12-17 16:46:37 +00001353 }
drh613028b2012-12-17 18:43:02 +00001354 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh076e85f2015-09-03 13:46:12 +00001355 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001356 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001357 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001358 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001359 }
drh688852a2014-02-17 22:40:43 +00001360 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001361 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001362 }
drh9ccd8652013-09-13 16:36:46 +00001363 }
1364 break;
dan09ff9e12013-03-11 11:49:03 +00001365#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001366#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1367
drhc11d4f92003-04-06 21:08:24 +00001368#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001369 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001370 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001371 if( sqlite3GetBoolean(zRight, 0) ){
drh97e58a22015-11-10 14:27:17 +00001372 sqlite3ParserTrace(stdout, "parser: ");
drh55ef4d92005-08-14 01:20:37 +00001373 }else{
1374 sqlite3ParserTrace(0, 0);
1375 }
drhc11d4f92003-04-06 21:08:24 +00001376 }
drh9ccd8652013-09-13 16:36:46 +00001377 }
1378 break;
drhc11d4f92003-04-06 21:08:24 +00001379#endif
1380
drh55ef4d92005-08-14 01:20:37 +00001381 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1382 ** used will be case sensitive or not depending on the RHS.
1383 */
drh9ccd8652013-09-13 16:36:46 +00001384 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001385 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001386 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001387 }
drh9ccd8652013-09-13 16:36:46 +00001388 }
1389 break;
drh55ef4d92005-08-14 01:20:37 +00001390
drh1dcdbc02007-01-27 02:24:54 +00001391#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1392# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1393#endif
1394
drhb7f91642004-10-31 02:22:47 +00001395#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh8b174f22017-02-22 15:11:36 +00001396 /* PRAGMA integrity_check
1397 ** PRAGMA integrity_check(N)
1398 ** PRAGMA quick_check
1399 ** PRAGMA quick_check(N)
1400 **
1401 ** Verify the integrity of the database.
1402 **
1403 ** The "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001404 ** integrity_check designed to detect most database corruption
drh8b174f22017-02-22 15:11:36 +00001405 ** without the overhead of cross-checking indexes. Quick_check
1406 ** is linear time wherease integrity_check is O(NlogN).
danielk197741c58b72007-12-29 13:39:19 +00001407 */
drh9ccd8652013-09-13 16:36:46 +00001408 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001409 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001410
drhc5227312011-10-13 17:09:01 +00001411 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001412
dan5885e762012-07-16 10:06:12 +00001413 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1414 ** then iDb is set to the index of the database identified by <db>.
1415 ** In this case, the integrity of database iDb only is verified by
1416 ** the VDBE created below.
1417 **
1418 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1419 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1420 ** to -1 here, to indicate that the VDBE should verify the integrity
1421 ** of all attached databases. */
1422 assert( iDb>=0 );
1423 assert( iDb==0 || pId2->z );
1424 if( pId2->z==0 ) iDb = -1;
1425
drhed717fe2003-06-15 23:42:24 +00001426 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001427 pParse->nMem = 6;
drh1dcdbc02007-01-27 02:24:54 +00001428
1429 /* Set the maximum error count */
1430 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1431 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001432 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001433 if( mxErr<=0 ){
1434 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1435 }
1436 }
drh66accfc2017-02-22 18:04:42 +00001437 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001438
1439 /* Do an integrity check on each database file */
1440 for(i=0; i<db->nDb; i++){
1441 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001442 Hash *pTbls;
drh98968b22016-03-15 22:00:39 +00001443 int *aRoot;
drh79069752004-05-22 21:30:40 +00001444 int cnt = 0;
drhbb9b5f22016-03-19 00:35:02 +00001445 int mxIdx = 0;
1446 int nIdx;
drhed717fe2003-06-15 23:42:24 +00001447
danielk197753c0f742005-03-29 03:10:59 +00001448 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001449 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001450
drh80242052004-06-09 00:48:12 +00001451 sqlite3CodeVerifySchema(pParse, i);
1452
drhed717fe2003-06-15 23:42:24 +00001453 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001454 **
drh98968b22016-03-15 22:00:39 +00001455 ** Begin by finding the root pages numbers
drh2d401ab2008-01-10 23:50:11 +00001456 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001457 */
dan5885e762012-07-16 10:06:12 +00001458 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001459 pTbls = &db->aDb[i].pSchema->tblHash;
drh98968b22016-03-15 22:00:39 +00001460 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001461 Table *pTab = sqliteHashData(x);
1462 Index *pIdx;
drh98968b22016-03-15 22:00:39 +00001463 if( HasRowid(pTab) ) cnt++;
drhbb9b5f22016-03-19 00:35:02 +00001464 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
1465 if( nIdx>mxIdx ) mxIdx = nIdx;
drh98968b22016-03-15 22:00:39 +00001466 }
1467 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
1468 if( aRoot==0 ) break;
1469 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1470 Table *pTab = sqliteHashData(x);
1471 Index *pIdx;
1472 if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum;
drh79069752004-05-22 21:30:40 +00001473 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98968b22016-03-15 22:00:39 +00001474 aRoot[cnt++] = pIdx->tnum;
drh79069752004-05-22 21:30:40 +00001475 }
1476 }
drh98968b22016-03-15 22:00:39 +00001477 aRoot[cnt] = 0;
drh2d401ab2008-01-10 23:50:11 +00001478
1479 /* Make sure sufficient number of registers have been allocated */
drhbb9b5f22016-03-19 00:35:02 +00001480 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
drh2d401ab2008-01-10 23:50:11 +00001481
1482 /* Do the b-tree integrity checks */
drh98968b22016-03-15 22:00:39 +00001483 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
drh4f21c4a2008-12-10 22:15:00 +00001484 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001485 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001486 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh69c33822016-08-18 14:33:11 +00001487 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
drh66a51672008-01-03 00:01:23 +00001488 P4_DYNAMIC);
drh079a3072014-03-19 14:10:55 +00001489 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001490 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh66accfc2017-02-22 18:04:42 +00001491 integrityCheckResultRow(v, 2);
drh1dcdbc02007-01-27 02:24:54 +00001492 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001493
1494 /* Make sure all the indices are constructed correctly.
1495 */
drh8b174f22017-02-22 15:11:36 +00001496 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001497 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001498 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001499 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001500 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001501 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001502 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001503
drh8b174f22017-02-22 15:11:36 +00001504 if( pTab->pCheck==0
1505 && (pTab->tabFlags & TF_HasNotNull)==0
1506 && (pTab->pIndex==0 || isQuick)
1507 ){
1508 continue; /* No additional checks needed for this table */
1509 }
drh26198bb2013-10-31 11:15:09 +00001510 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh66518ca2013-08-01 15:09:57 +00001511 sqlite3ExprCacheClear(pParse);
danfd261ec2015-10-22 20:54:33 +00001512 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001513 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001514 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001515 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001516 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001517 }
drhbb9b5f22016-03-19 00:35:02 +00001518 assert( pParse->nMem>=8+j );
1519 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
drh688852a2014-02-17 22:40:43 +00001520 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001521 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhcefc87f2014-08-01 01:40:33 +00001522 /* Verify that all NOT NULL columns really are NOT NULL */
1523 for(j=0; j<pTab->nCol; j++){
1524 char *zErr;
drh66accfc2017-02-22 18:04:42 +00001525 int jmp2;
drhcefc87f2014-08-01 01:40:33 +00001526 if( j==pTab->iPKey ) continue;
1527 if( pTab->aCol[j].notNull==0 ) continue;
1528 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1529 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1530 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
drhcefc87f2014-08-01 01:40:33 +00001531 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1532 pTab->aCol[j].zName);
1533 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh66accfc2017-02-22 18:04:42 +00001534 integrityCheckResultRow(v, 3);
drhcefc87f2014-08-01 01:40:33 +00001535 sqlite3VdbeJumpHere(v, jmp2);
drhcefc87f2014-08-01 01:40:33 +00001536 }
drh8a284dc2017-02-22 14:15:37 +00001537 /* Verify CHECK constraints */
1538 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
1539 int addrCkFault = sqlite3VdbeMakeLabel(v);
1540 int addrCkOk = sqlite3VdbeMakeLabel(v);
1541 ExprList *pCheck = pTab->pCheck;
1542 char *zErr;
1543 int k;
1544 pParse->iSelfTab = iDataCur;
1545 sqlite3ExprCachePush(pParse);
1546 for(k=pCheck->nExpr-1; k>0; k--){
1547 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
1548 }
1549 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
1550 SQLITE_JUMPIFNULL);
1551 sqlite3VdbeResolveLabel(v, addrCkFault);
drh8a284dc2017-02-22 14:15:37 +00001552 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
1553 pTab->zName);
1554 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
drh66accfc2017-02-22 18:04:42 +00001555 integrityCheckResultRow(v, 3);
drh8a284dc2017-02-22 14:15:37 +00001556 sqlite3VdbeResolveLabel(v, addrCkOk);
1557 sqlite3ExprCachePop(pParse);
1558 }
drhcefc87f2014-08-01 01:40:33 +00001559 /* Validate index entries for the current row */
drh8b174f22017-02-22 15:11:36 +00001560 for(j=0, pIdx=pTab->pIndex; pIdx && !isQuick; pIdx=pIdx->pNext, j++){
drhcefc87f2014-08-01 01:40:33 +00001561 int jmp2, jmp3, jmp4, jmp5;
1562 int ckUniq = sqlite3VdbeMakeLabel(v);
drh6fbe41a2013-10-30 20:22:55 +00001563 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001564 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1565 pPrior, r1);
1566 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001567 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drhcefc87f2014-08-01 01:40:33 +00001568 /* Verify that an index entry exists for the current table row */
1569 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
drh688852a2014-02-17 22:40:43 +00001570 pIdx->nColumn); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001571 sqlite3VdbeLoadString(v, 3, "row ");
drh6fbe41a2013-10-30 20:22:55 +00001572 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001573 sqlite3VdbeLoadString(v, 4, " missing from index ");
drh6fbe41a2013-10-30 20:22:55 +00001574 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001575 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
drh6fbe41a2013-10-30 20:22:55 +00001576 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh66accfc2017-02-22 18:04:42 +00001577 jmp4 = integrityCheckResultRow(v, 3);
drhd654be82005-09-20 17:42:23 +00001578 sqlite3VdbeJumpHere(v, jmp2);
drhcefc87f2014-08-01 01:40:33 +00001579 /* For UNIQUE indexes, verify that only one entry exists with the
1580 ** current key. The entry is unique if (1) any column is NULL
1581 ** or (2) the next entry has a different key */
1582 if( IsUniqueIndex(pIdx) ){
1583 int uniqOk = sqlite3VdbeMakeLabel(v);
1584 int jmp6;
1585 int kk;
1586 for(kk=0; kk<pIdx->nKeyCol; kk++){
1587 int iCol = pIdx->aiColumn[kk];
drh4b92f982015-09-29 17:20:14 +00001588 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
drh68391ac2015-09-25 20:49:16 +00001589 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
drhcefc87f2014-08-01 01:40:33 +00001590 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1591 VdbeCoverage(v);
1592 }
1593 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001594 sqlite3VdbeGoto(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00001595 sqlite3VdbeJumpHere(v, jmp6);
1596 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1597 pIdx->nKeyCol); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001598 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1599 sqlite3VdbeGoto(v, jmp5);
drhcefc87f2014-08-01 01:40:33 +00001600 sqlite3VdbeResolveLabel(v, uniqOk);
1601 }
1602 sqlite3VdbeJumpHere(v, jmp4);
drh87744512014-04-13 19:15:49 +00001603 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhed717fe2003-06-15 23:42:24 +00001604 }
drh688852a2014-02-17 22:40:43 +00001605 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001606 sqlite3VdbeJumpHere(v, loopTop-1);
1607#ifndef SQLITE_OMIT_BTREECOUNT
drh8b174f22017-02-22 15:11:36 +00001608 if( !isQuick ){
1609 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
1610 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1611 if( pPk==pIdx ) continue;
drh8b174f22017-02-22 15:11:36 +00001612 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh66accfc2017-02-22 18:04:42 +00001613 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
drh8b174f22017-02-22 15:11:36 +00001614 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh8b174f22017-02-22 15:11:36 +00001615 sqlite3VdbeLoadString(v, 3, pIdx->zName);
1616 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
drh66accfc2017-02-22 18:04:42 +00001617 integrityCheckResultRow(v, 7);
1618 sqlite3VdbeJumpHere(v, addr);
drh8b174f22017-02-22 15:11:36 +00001619 }
drhed717fe2003-06-15 23:42:24 +00001620 }
drh8a9789b2013-08-01 03:36:59 +00001621#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001622 }
1623 }
drh2ce18652016-01-16 20:50:21 +00001624 {
1625 static const int iLn = VDBE_OFFSET_LINENO(2);
1626 static const VdbeOpList endCode[] = {
1627 { OP_AddImm, 1, 0, 0}, /* 0 */
drh66accfc2017-02-22 18:04:42 +00001628 { OP_IfNotZero, 1, 4, 0}, /* 1 */
drh2ce18652016-01-16 20:50:21 +00001629 { OP_String8, 0, 3, 0}, /* 2 */
drh1b325542016-02-03 01:55:44 +00001630 { OP_ResultRow, 3, 1, 0}, /* 3 */
drh2ce18652016-01-16 20:50:21 +00001631 };
1632 VdbeOp *aOp;
1633
1634 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
1635 if( aOp ){
drh66accfc2017-02-22 18:04:42 +00001636 aOp[0].p2 = 1-mxErr;
drh2ce18652016-01-16 20:50:21 +00001637 aOp[2].p4type = P4_STATIC;
1638 aOp[2].p4.z = "ok";
1639 }
1640 }
drh9ccd8652013-09-13 16:36:46 +00001641 }
1642 break;
drhb7f91642004-10-31 02:22:47 +00001643#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1644
drh13d70422004-11-13 15:59:14 +00001645#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001646 /*
1647 ** PRAGMA encoding
1648 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1649 **
drh85b623f2007-12-13 21:54:09 +00001650 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001651 ** database. If the database is not initialized, it is initialized now.
1652 **
1653 ** The second form of this pragma is a no-op if the main database file
1654 ** has not already been initialized. In this case it sets the default
1655 ** encoding that will be used for the main database file if a new file
1656 ** is created. If an existing main database file is opened, then the
1657 ** default text encoding for the existing database is used.
1658 **
1659 ** In all cases new databases created using the ATTACH command are
1660 ** created to use the same default text encoding as the main database. If
1661 ** the main database has not been initialized and/or created when ATTACH
1662 ** is executed, this is done before the ATTACH operation.
1663 **
1664 ** In the second form this pragma sets the text encoding to be used in
1665 ** new database files created using this database handle. It is only
1666 ** useful if invoked immediately after the main database i
1667 */
drh9ccd8652013-09-13 16:36:46 +00001668 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001669 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001670 char *zName;
1671 u8 enc;
1672 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001673 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001674 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1675 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1676 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001677 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001678 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001679 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1680 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001681 { 0, 0 }
1682 };
drh0f7eb612006-08-08 13:51:43 +00001683 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001684 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001685 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00001686 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1687 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1688 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drhc232aca2016-12-15 16:01:17 +00001689 returnSingleText(v, encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00001690 }else{ /* "PRAGMA encoding = XXX" */
1691 /* Only change the value of sqlite.enc if the database handle is not
1692 ** initialized. If the main database exists, the new sqlite.enc value
1693 ** will be overwritten when the schema is next loaded. If it does not
1694 ** already exists, it will be created to use the new encoding value.
1695 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001696 if(
1697 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1698 DbHasProperty(db, 0, DB_Empty)
1699 ){
danielk19778e227872004-06-07 07:52:17 +00001700 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1701 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh9bd3cc42014-12-12 23:17:54 +00001702 SCHEMA_ENC(db) = ENC(db) =
1703 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001704 break;
1705 }
1706 }
1707 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001708 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001709 }
1710 }
1711 }
drh9ccd8652013-09-13 16:36:46 +00001712 }
1713 break;
drh13d70422004-11-13 15:59:14 +00001714#endif /* SQLITE_OMIT_UTF16 */
1715
1716#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001717 /*
drh9b0cf342015-11-12 14:57:19 +00001718 ** PRAGMA [schema.]schema_version
1719 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001720 **
drh9b0cf342015-11-12 14:57:19 +00001721 ** PRAGMA [schema.]user_version
1722 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001723 **
drhe459bd42016-03-16 20:05:57 +00001724 ** PRAGMA [schema.]freelist_count
1725 **
1726 ** PRAGMA [schema.]data_version
drh4ee09b42013-05-01 19:49:27 +00001727 **
drh9b0cf342015-11-12 14:57:19 +00001728 ** PRAGMA [schema.]application_id
1729 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00001730 **
danielk1977b92b70b2004-11-12 16:11:59 +00001731 ** The pragma's schema_version and user_version are used to set or get
1732 ** the value of the schema-version and user-version, respectively. Both
1733 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001734 ** stored in the database header.
1735 **
1736 ** The schema-cookie is usually only manipulated internally by SQLite. It
1737 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001738 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001739 ** SQLite each time a query is executed to ensure that the internal cache
1740 ** of the schema used when compiling the SQL query matches the schema of
1741 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001742 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1743 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001744 ** crashes or database corruption. Use with caution!
1745 **
danielk1977b92b70b2004-11-12 16:11:59 +00001746 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001747 ** applications for any purpose.
1748 */
drh9ccd8652013-09-13 16:36:46 +00001749 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00001750 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00001751 sqlite3VdbeUsesBtree(v, iDb);
drhc232aca2016-12-15 16:01:17 +00001752 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00001753 /* Write the specified cookie value */
1754 static const VdbeOpList setCookie[] = {
1755 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1861afc2016-02-01 21:48:34 +00001756 { OP_SetCookie, 0, 0, 0}, /* 1 */
danielk1977dae24952004-11-11 05:10:43 +00001757 };
drh2ce18652016-01-16 20:50:21 +00001758 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001759 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
drh2ce18652016-01-16 20:50:21 +00001760 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
drhdad300d2016-01-18 00:20:26 +00001761 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001762 aOp[0].p1 = iDb;
drh1861afc2016-02-01 21:48:34 +00001763 aOp[1].p1 = iDb;
1764 aOp[1].p2 = iCookie;
1765 aOp[1].p3 = sqlite3Atoi(zRight);
danielk1977dae24952004-11-11 05:10:43 +00001766 }else{
1767 /* Read the specified cookie value */
1768 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001769 { OP_Transaction, 0, 0, 0}, /* 0 */
1770 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001771 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001772 };
drh2ce18652016-01-16 20:50:21 +00001773 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001774 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
drh2ce18652016-01-16 20:50:21 +00001775 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
drhdad300d2016-01-18 00:20:26 +00001776 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001777 aOp[0].p1 = iDb;
1778 aOp[1].p1 = iDb;
1779 aOp[1].p3 = iCookie;
drhf71a3662016-03-16 20:44:45 +00001780 sqlite3VdbeReusable(v);
danielk1977dae24952004-11-11 05:10:43 +00001781 }
drh9ccd8652013-09-13 16:36:46 +00001782 }
1783 break;
drh13d70422004-11-13 15:59:14 +00001784#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001785
shanehdc97a8c2010-02-23 20:08:35 +00001786#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1787 /*
1788 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001789 **
drh71caabf2010-02-26 15:39:24 +00001790 ** Return the names of all compile-time options used in this build,
1791 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001792 */
drh9ccd8652013-09-13 16:36:46 +00001793 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00001794 int i = 0;
1795 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00001796 pParse->nMem = 1;
shanehdc97a8c2010-02-23 20:08:35 +00001797 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00001798 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00001799 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1800 }
drhf71a3662016-03-16 20:44:45 +00001801 sqlite3VdbeReusable(v);
drh9ccd8652013-09-13 16:36:46 +00001802 }
1803 break;
shanehdc97a8c2010-02-23 20:08:35 +00001804#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1805
dan5cf53532010-05-01 16:40:20 +00001806#ifndef SQLITE_OMIT_WAL
1807 /*
drh9b0cf342015-11-12 14:57:19 +00001808 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00001809 **
1810 ** Checkpoint the database.
1811 */
drh9ccd8652013-09-13 16:36:46 +00001812 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00001813 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001814 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1815 if( zRight ){
1816 if( sqlite3StrICmp(zRight, "full")==0 ){
1817 eMode = SQLITE_CHECKPOINT_FULL;
1818 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1819 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00001820 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1821 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00001822 }
1823 }
dancdc1f042010-11-18 12:11:05 +00001824 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00001825 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001826 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00001827 }
1828 break;
dan5a299f92010-05-03 11:05:08 +00001829
1830 /*
1831 ** PRAGMA wal_autocheckpoint
1832 ** PRAGMA wal_autocheckpoint = N
1833 **
1834 ** Configure a database connection to automatically checkpoint a database
1835 ** after accumulating N frames in the log. Or query for the current value
1836 ** of N.
1837 */
drh9ccd8652013-09-13 16:36:46 +00001838 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00001839 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001840 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001841 }
drhc232aca2016-12-15 16:01:17 +00001842 returnSingleInt(v,
drhb033d8b2010-05-03 13:37:30 +00001843 db->xWalCallback==sqlite3WalDefaultHook ?
1844 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00001845 }
1846 break;
dan5cf53532010-05-01 16:40:20 +00001847#endif
dan7c246102010-04-12 19:00:29 +00001848
drh09419b42011-11-16 19:29:17 +00001849 /*
1850 ** PRAGMA shrink_memory
1851 **
drh51a74d42015-02-28 01:04:27 +00001852 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
1853 ** connection on which it is invoked to free up as much memory as it
1854 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00001855 */
drh9ccd8652013-09-13 16:36:46 +00001856 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00001857 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00001858 break;
1859 }
drh09419b42011-11-16 19:29:17 +00001860
drhf3603962012-09-07 16:46:59 +00001861 /*
1862 ** PRAGMA busy_timeout
1863 ** PRAGMA busy_timeout = N
1864 **
1865 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001866 ** if one is set. If no busy handler or a different busy handler is set
1867 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1868 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001869 */
drhd49c3582013-09-13 19:00:06 +00001870 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00001871 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00001872 if( zRight ){
1873 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1874 }
drhc232aca2016-12-15 16:01:17 +00001875 returnSingleInt(v, db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00001876 break;
1877 }
drhf3603962012-09-07 16:46:59 +00001878
drh55e85ca2013-09-13 21:01:56 +00001879 /*
1880 ** PRAGMA soft_heap_limit
1881 ** PRAGMA soft_heap_limit = N
1882 **
drh51a74d42015-02-28 01:04:27 +00001883 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
1884 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
1885 ** specified and is a non-negative integer.
1886 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
1887 ** returns the same integer that would be returned by the
1888 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00001889 */
1890 case PragTyp_SOFT_HEAP_LIMIT: {
1891 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00001892 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00001893 sqlite3_soft_heap_limit64(N);
1894 }
drhc232aca2016-12-15 16:01:17 +00001895 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00001896 break;
1897 }
1898
drh03459612014-08-25 15:13:22 +00001899 /*
1900 ** PRAGMA threads
1901 ** PRAGMA threads = N
1902 **
1903 ** Configure the maximum number of worker threads. Return the new
1904 ** maximum, which might be less than requested.
1905 */
1906 case PragTyp_THREADS: {
1907 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00001908 if( zRight
drh03459612014-08-25 15:13:22 +00001909 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
1910 && N>=0
1911 ){
drh111544c2014-08-29 16:20:47 +00001912 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00001913 }
drhc232aca2016-12-15 16:01:17 +00001914 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00001915 break;
1916 }
1917
dougcurrie81c95ef2004-06-18 23:21:47 +00001918#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001919 /*
1920 ** Report the current state of file logs for all databases
1921 */
drh9ccd8652013-09-13 16:36:46 +00001922 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00001923 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001924 "unlocked", "shared", "reserved", "pending", "exclusive"
1925 };
1926 int i;
drh2d401ab2008-01-10 23:50:11 +00001927 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00001928 for(i=0; i<db->nDb; i++){
1929 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001930 const char *zState = "unknown";
1931 int j;
drh69c33822016-08-18 14:33:11 +00001932 if( db->aDb[i].zDbSName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00001933 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001934 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001935 zState = "closed";
drh69c33822016-08-18 14:33:11 +00001936 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001937 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1938 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001939 }
drh69c33822016-08-18 14:33:11 +00001940 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
drh2d401ab2008-01-10 23:50:11 +00001941 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001942 }
drh9ccd8652013-09-13 16:36:46 +00001943 break;
1944 }
drh89ac8c12004-06-09 14:17:20 +00001945#endif
1946
shanehad9f9f62010-02-17 17:48:46 +00001947#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00001948 case PragTyp_KEY: {
1949 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1950 break;
1951 }
1952 case PragTyp_REKEY: {
1953 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1954 break;
1955 }
1956 case PragTyp_HEXKEY: {
1957 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00001958 u8 iByte;
1959 int i;
drh9ccd8652013-09-13 16:36:46 +00001960 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00001961 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
1962 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
1963 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00001964 }
1965 if( (zLeft[3] & 0xf)==0xb ){
1966 sqlite3_key_v2(db, zDb, zKey, i/2);
1967 }else{
1968 sqlite3_rekey_v2(db, zDb, zKey, i/2);
1969 }
drhd2cb50b2009-01-09 21:41:17 +00001970 }
drh9ccd8652013-09-13 16:36:46 +00001971 break;
1972 }
drh3c4f2a42005-12-08 18:12:56 +00001973#endif
shanehad9f9f62010-02-17 17:48:46 +00001974#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00001975 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001976#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001977 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001978 sqlite3_activate_see(&zRight[4]);
1979 }
1980#endif
1981#ifdef SQLITE_ENABLE_CEROD
1982 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001983 sqlite3_activate_cerod(&zRight[6]);
1984 }
1985#endif
drh9ccd8652013-09-13 16:36:46 +00001986 }
1987 break;
drh21e2cab2006-09-25 18:01:57 +00001988#endif
drh3c4f2a42005-12-08 18:12:56 +00001989
drh9ccd8652013-09-13 16:36:46 +00001990 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00001991
dan9e1ab1a2017-01-05 19:32:48 +00001992 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
1993 ** purpose is to execute assert() statements to verify that if the
1994 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
1995 ** to the PRAGMA, the implementation has not added any OP_ResultRow
1996 ** instructions to the VM. */
1997 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
1998 sqlite3VdbeVerifyNoResultRow(v);
1999 }
2000
danielk1977e0048402004-06-15 16:51:01 +00002001pragma_out:
drh633e6d52008-07-28 19:34:53 +00002002 sqlite3DbFree(db, zLeft);
2003 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00002004}
drh2fcc1592016-12-15 20:59:03 +00002005#ifndef SQLITE_OMIT_VIRTUALTABLE
2006/*****************************************************************************
2007** Implementation of an eponymous virtual table that runs a pragma.
2008**
2009*/
2010typedef struct PragmaVtab PragmaVtab;
2011typedef struct PragmaVtabCursor PragmaVtabCursor;
2012struct PragmaVtab {
2013 sqlite3_vtab base; /* Base class. Must be first */
2014 sqlite3 *db; /* The database connection to which it belongs */
2015 const PragmaName *pName; /* Name of the pragma */
2016 u8 nHidden; /* Number of hidden columns */
2017 u8 iHidden; /* Index of the first hidden column */
2018};
2019struct PragmaVtabCursor {
2020 sqlite3_vtab_cursor base; /* Base class. Must be first */
2021 sqlite3_stmt *pPragma; /* The pragma statement to run */
2022 sqlite_int64 iRowid; /* Current rowid */
2023 char *azArg[2]; /* Value of the argument and schema */
2024};
2025
2026/*
2027** Pragma virtual table module xConnect method.
2028*/
2029static int pragmaVtabConnect(
2030 sqlite3 *db,
2031 void *pAux,
2032 int argc, const char *const*argv,
2033 sqlite3_vtab **ppVtab,
2034 char **pzErr
2035){
2036 const PragmaName *pPragma = (const PragmaName*)pAux;
2037 PragmaVtab *pTab = 0;
2038 int rc;
2039 int i, j;
2040 char cSep = '(';
2041 StrAccum acc;
2042 char zBuf[200];
2043
drh344a1bf2016-12-22 14:53:25 +00002044 UNUSED_PARAMETER(argc);
2045 UNUSED_PARAMETER(argv);
drh2fcc1592016-12-15 20:59:03 +00002046 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
2047 sqlite3StrAccumAppendAll(&acc, "CREATE TABLE x");
2048 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
drhd7175eb2016-12-15 21:11:15 +00002049 sqlite3XPrintf(&acc, "%c\"%s\"", cSep, pragCName[j]);
drh2fcc1592016-12-15 20:59:03 +00002050 cSep = ',';
2051 }
drh9a63f092016-12-16 02:14:15 +00002052 if( i==0 ){
2053 sqlite3XPrintf(&acc, "(\"%s\"", pPragma->zName);
2054 cSep = ',';
2055 i++;
2056 }
drh2fcc1592016-12-15 20:59:03 +00002057 j = 0;
2058 if( pPragma->mPragFlg & PragFlg_Result1 ){
2059 sqlite3StrAccumAppendAll(&acc, ",arg HIDDEN");
2060 j++;
2061 }
2062 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
2063 sqlite3StrAccumAppendAll(&acc, ",schema HIDDEN");
2064 j++;
2065 }
2066 sqlite3StrAccumAppend(&acc, ")", 1);
2067 sqlite3StrAccumFinish(&acc);
2068 assert( strlen(zBuf) < sizeof(zBuf)-1 );
2069 rc = sqlite3_declare_vtab(db, zBuf);
2070 if( rc==SQLITE_OK ){
2071 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
2072 if( pTab==0 ){
2073 rc = SQLITE_NOMEM;
2074 }else{
2075 memset(pTab, 0, sizeof(PragmaVtab));
2076 pTab->pName = pPragma;
2077 pTab->db = db;
2078 pTab->iHidden = i;
2079 pTab->nHidden = j;
2080 }
2081 }else{
2082 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2083 }
2084
2085 *ppVtab = (sqlite3_vtab*)pTab;
2086 return rc;
2087}
2088
2089/*
2090** Pragma virtual table module xDisconnect method.
2091*/
2092static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
2093 PragmaVtab *pTab = (PragmaVtab*)pVtab;
2094 sqlite3_free(pTab);
2095 return SQLITE_OK;
2096}
2097
2098/* Figure out the best index to use to search a pragma virtual table.
2099**
2100** There are not really any index choices. But we want to encourage the
2101** query planner to give == constraints on as many hidden parameters as
2102** possible, and especially on the first hidden parameter. So return a
2103** high cost if hidden parameters are unconstrained.
2104*/
2105static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2106 PragmaVtab *pTab = (PragmaVtab*)tab;
2107 const struct sqlite3_index_constraint *pConstraint;
2108 int i, j;
2109 int seen[2];
2110
drhae7045c2016-12-15 21:33:55 +00002111 pIdxInfo->estimatedCost = (double)1;
drh2fcc1592016-12-15 20:59:03 +00002112 if( pTab->nHidden==0 ){ return SQLITE_OK; }
2113 pConstraint = pIdxInfo->aConstraint;
2114 seen[0] = 0;
2115 seen[1] = 0;
2116 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2117 if( pConstraint->usable==0 ) continue;
2118 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2119 if( pConstraint->iColumn < pTab->iHidden ) continue;
2120 j = pConstraint->iColumn - pTab->iHidden;
2121 assert( j < 2 );
drhd7175eb2016-12-15 21:11:15 +00002122 seen[j] = i+1;
drh2fcc1592016-12-15 20:59:03 +00002123 }
2124 if( seen[0]==0 ){
2125 pIdxInfo->estimatedCost = (double)2147483647;
2126 pIdxInfo->estimatedRows = 2147483647;
2127 return SQLITE_OK;
2128 }
drhd7175eb2016-12-15 21:11:15 +00002129 j = seen[0]-1;
drh2fcc1592016-12-15 20:59:03 +00002130 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
2131 pIdxInfo->aConstraintUsage[j].omit = 1;
2132 if( seen[1]==0 ) return SQLITE_OK;
2133 pIdxInfo->estimatedCost = (double)20;
2134 pIdxInfo->estimatedRows = 20;
drhd7175eb2016-12-15 21:11:15 +00002135 j = seen[1]-1;
drh2fcc1592016-12-15 20:59:03 +00002136 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
2137 pIdxInfo->aConstraintUsage[j].omit = 1;
2138 return SQLITE_OK;
2139}
2140
2141/* Create a new cursor for the pragma virtual table */
2142static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
2143 PragmaVtabCursor *pCsr;
2144 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
2145 if( pCsr==0 ) return SQLITE_NOMEM;
2146 memset(pCsr, 0, sizeof(PragmaVtabCursor));
2147 pCsr->base.pVtab = pVtab;
2148 *ppCursor = &pCsr->base;
2149 return SQLITE_OK;
2150}
2151
2152/* Clear all content from pragma virtual table cursor. */
2153static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
2154 int i;
2155 sqlite3_finalize(pCsr->pPragma);
2156 pCsr->pPragma = 0;
2157 for(i=0; i<ArraySize(pCsr->azArg); i++){
2158 sqlite3_free(pCsr->azArg[i]);
2159 pCsr->azArg[i] = 0;
2160 }
2161}
2162
2163/* Close a pragma virtual table cursor */
2164static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
2165 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
2166 pragmaVtabCursorClear(pCsr);
drh9a63f092016-12-16 02:14:15 +00002167 sqlite3_free(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002168 return SQLITE_OK;
2169}
2170
2171/* Advance the pragma virtual table cursor to the next row */
2172static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
2173 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2174 int rc = SQLITE_OK;
2175
2176 /* Increment the xRowid value */
2177 pCsr->iRowid++;
drh9a63f092016-12-16 02:14:15 +00002178 assert( pCsr->pPragma );
2179 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
2180 rc = sqlite3_finalize(pCsr->pPragma);
2181 pCsr->pPragma = 0;
2182 pragmaVtabCursorClear(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002183 }
2184 return rc;
2185}
2186
2187/*
2188** Pragma virtual table module xFilter method.
2189*/
2190static int pragmaVtabFilter(
2191 sqlite3_vtab_cursor *pVtabCursor,
2192 int idxNum, const char *idxStr,
2193 int argc, sqlite3_value **argv
2194){
2195 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2196 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2197 int rc;
drhd8b72002016-12-16 04:20:27 +00002198 int i, j;
drh2fcc1592016-12-15 20:59:03 +00002199 StrAccum acc;
2200 char *zSql;
2201
drh344a1bf2016-12-22 14:53:25 +00002202 UNUSED_PARAMETER(idxNum);
2203 UNUSED_PARAMETER(idxStr);
drh2fcc1592016-12-15 20:59:03 +00002204 pragmaVtabCursorClear(pCsr);
drhd8b72002016-12-16 04:20:27 +00002205 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
2206 for(i=0; i<argc; i++, j++){
2207 assert( j<ArraySize(pCsr->azArg) );
2208 pCsr->azArg[j] = sqlite3_mprintf("%s", sqlite3_value_text(argv[i]));
2209 if( pCsr->azArg[j]==0 ){
drh2fcc1592016-12-15 20:59:03 +00002210 return SQLITE_NOMEM;
2211 }
2212 }
drhd7175eb2016-12-15 21:11:15 +00002213 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
drh2fcc1592016-12-15 20:59:03 +00002214 sqlite3StrAccumAppendAll(&acc, "PRAGMA ");
2215 if( pCsr->azArg[1] ){
2216 sqlite3XPrintf(&acc, "%Q.", pCsr->azArg[1]);
2217 }
2218 sqlite3StrAccumAppendAll(&acc, pTab->pName->zName);
2219 if( pCsr->azArg[0] ){
2220 sqlite3XPrintf(&acc, "=%Q", pCsr->azArg[0]);
2221 }
2222 zSql = sqlite3StrAccumFinish(&acc);
2223 if( zSql==0 ) return SQLITE_NOMEM;
2224 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
2225 sqlite3_free(zSql);
2226 if( rc!=SQLITE_OK ){
2227 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
2228 return rc;
2229 }
2230 return pragmaVtabNext(pVtabCursor);
2231}
2232
2233/*
2234** Pragma virtual table module xEof method.
2235*/
2236static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
2237 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2238 return (pCsr->pPragma==0);
2239}
2240
2241/* The xColumn method simply returns the corresponding column from
2242** the PRAGMA.
2243*/
2244static int pragmaVtabColumn(
2245 sqlite3_vtab_cursor *pVtabCursor,
2246 sqlite3_context *ctx,
2247 int i
2248){
2249 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2250 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2251 if( i<pTab->iHidden ){
2252 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
2253 }else{
2254 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
2255 }
2256 return SQLITE_OK;
2257}
2258
2259/*
2260** Pragma virtual table module xRowid method.
2261*/
2262static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
2263 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2264 *p = pCsr->iRowid;
2265 return SQLITE_OK;
2266}
2267
2268/* The pragma virtual table object */
2269static const sqlite3_module pragmaVtabModule = {
2270 0, /* iVersion */
2271 0, /* xCreate - create a table */
2272 pragmaVtabConnect, /* xConnect - connect to an existing table */
2273 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
2274 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
2275 0, /* xDestroy - Drop a table */
2276 pragmaVtabOpen, /* xOpen - open a cursor */
2277 pragmaVtabClose, /* xClose - close a cursor */
2278 pragmaVtabFilter, /* xFilter - configure scan constraints */
2279 pragmaVtabNext, /* xNext - advance a cursor */
2280 pragmaVtabEof, /* xEof */
2281 pragmaVtabColumn, /* xColumn - read data */
2282 pragmaVtabRowid, /* xRowid - read data */
2283 0, /* xUpdate - write data */
2284 0, /* xBegin - begin transaction */
2285 0, /* xSync - sync transaction */
2286 0, /* xCommit - commit transaction */
2287 0, /* xRollback - rollback transaction */
2288 0, /* xFindFunction - function overloading */
2289 0, /* xRename - rename the table */
2290 0, /* xSavepoint */
2291 0, /* xRelease */
2292 0 /* xRollbackTo */
2293};
2294
2295/*
2296** Check to see if zTabName is really the name of a pragma. If it is,
2297** then register an eponymous virtual table for that pragma and return
2298** a pointer to the Module object for the new virtual table.
2299*/
2300Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
2301 const PragmaName *pName;
2302 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
2303 pName = pragmaLocate(zName+7);
2304 if( pName==0 ) return 0;
2305 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
2306 assert( sqlite3HashFind(&db->aModule, zName)==0 );
drhd7175eb2016-12-15 21:11:15 +00002307 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
drh2fcc1592016-12-15 20:59:03 +00002308}
2309
2310#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh13d70422004-11-13 15:59:14 +00002311
drh8bfdf722009-06-19 14:06:03 +00002312#endif /* SQLITE_OMIT_PRAGMA */