blob: 6f2e0b545df596acc9f99f2d3be5e6bf429ee3b3 [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){
282 int upr, lwr, mid, rc;
283 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/*
drhc11d4f92003-04-06 21:08:24 +0000299** Process a pragma statement.
300**
301** Pragmas are of this form:
302**
drh9b0cf342015-11-12 14:57:19 +0000303** PRAGMA [schema.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000304**
305** The identifier might also be a string. The value is a string, and
306** identifier, or a number. If minusFlag is true, then the value is
307** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000308**
309** If the left side is "database.id" then pId1 is the database name
310** and pId2 is the id. If the left side is just "id" then pId1 is the
311** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000312*/
danielk197791cf71b2004-06-26 06:37:06 +0000313void sqlite3Pragma(
314 Parse *pParse,
drh9b0cf342015-11-12 14:57:19 +0000315 Token *pId1, /* First part of [schema.]id field */
316 Token *pId2, /* Second part of [schema.]id field, or NULL */
danielk197791cf71b2004-06-26 06:37:06 +0000317 Token *pValue, /* Token for <value>, or NULL */
318 int minusFlag /* True if a '-' sign preceded <value> */
319){
320 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
321 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
322 const char *zDb = 0; /* The database name */
323 Token *pId; /* Pointer to <id> token */
drh3fa97302012-02-22 16:58:36 +0000324 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000325 int iDb; /* Database index for <database> */
drh06fd5d62012-02-22 14:45:19 +0000326 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
327 sqlite3 *db = pParse->db; /* The database connection */
328 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000329 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drh2fcc1592016-12-15 20:59:03 +0000330 const PragmaName *pPragma; /* The pragma */
drh06fd5d62012-02-22 14:45:19 +0000331
drhc11d4f92003-04-06 21:08:24 +0000332 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000333 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000334 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000335
drh9b0cf342015-11-12 14:57:19 +0000336 /* Interpret the [schema.] part of the pragma statement. iDb is the
danielk197791cf71b2004-06-26 06:37:06 +0000337 ** index of the database this pragma is being applied to in db.aDb[]. */
338 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
339 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000340 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000341
danielk1977ddfb2f02006-02-17 12:25:14 +0000342 /* If the temp database has been explicitly named as part of the
343 ** pragma, make sure it is open.
344 */
345 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
346 return;
347 }
348
drh17435752007-08-16 04:30:38 +0000349 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000350 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000351 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000352 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000353 }else{
drh17435752007-08-16 04:30:38 +0000354 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000355 }
danielk197791cf71b2004-06-26 06:37:06 +0000356
drhd2cb50b2009-01-09 21:41:17 +0000357 assert( pId2 );
drh69c33822016-08-18 14:33:11 +0000358 zDb = pId2->n>0 ? pDb->zDbSName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000359 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000360 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000361 }
drh06fd5d62012-02-22 14:45:19 +0000362
363 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
364 ** connection. If it returns SQLITE_OK, then assume that the VFS
365 ** handled the pragma and generate a no-op prepared statement.
drh8dd7a6a2015-03-06 04:37:26 +0000366 **
367 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
368 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
369 ** object corresponding to the database file to which the pragma
370 ** statement refers.
371 **
372 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
373 ** file control is an array of pointers to strings (char**) in which the
374 ** second element of the array is the name of the pragma and the third
375 ** element is the argument to the pragma or NULL if the pragma has no
376 ** argument.
drh06fd5d62012-02-22 14:45:19 +0000377 */
drh3fa97302012-02-22 16:58:36 +0000378 aFcntl[0] = 0;
379 aFcntl[1] = zLeft;
380 aFcntl[2] = zRight;
381 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000382 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000383 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
384 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000385 sqlite3VdbeSetNumCols(v, 1);
386 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
387 returnSingleText(v, aFcntl[0]);
drh7cc023c2015-09-03 04:28:25 +0000388 sqlite3_free(aFcntl[0]);
drh9ccd8652013-09-13 16:36:46 +0000389 goto pragma_out;
390 }
391 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000392 if( aFcntl[0] ){
393 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
394 sqlite3_free(aFcntl[0]);
395 }
396 pParse->nErr++;
397 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000398 goto pragma_out;
399 }
400
401 /* Locate the pragma in the lookup table */
drh2fcc1592016-12-15 20:59:03 +0000402 pPragma = pragmaLocate(zLeft);
403 if( pPragma==0 ) goto pragma_out;
drh9ccd8652013-09-13 16:36:46 +0000404
drhf63936e2013-10-03 14:08:07 +0000405 /* Make sure the database schema is loaded if the pragma requires that */
drhc232aca2016-12-15 16:01:17 +0000406 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
drhf63936e2013-10-03 14:08:07 +0000407 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
408 }
409
drhc232aca2016-12-15 16:01:17 +0000410 /* Register the result column names for pragmas that return results */
411 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0 ){
412 setPragmaResultColumnNames(v, pPragma);
413 }
414
drh9ccd8652013-09-13 16:36:46 +0000415 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01 +0000416 switch( pPragma->ePragTyp ){
drh9ccd8652013-09-13 16:36:46 +0000417
drhe73c9142011-11-09 16:12:24 +0000418#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000419 /*
drh9b0cf342015-11-12 14:57:19 +0000420 ** PRAGMA [schema.]default_cache_size
421 ** PRAGMA [schema.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000422 **
423 ** The first form reports the current persistent setting for the
424 ** page cache size. The value returned is the maximum number of
425 ** pages in the page cache. The second form sets both the current
426 ** page cache size value and the persistent page cache size value
427 ** stored in the database file.
428 **
drh93791ea2010-04-26 17:36:35 +0000429 ** Older versions of SQLite would set the default cache size to a
430 ** negative number to indicate synchronous=OFF. These days, synchronous
431 ** is always on by default regardless of the sign of the default cache
432 ** size. But continue to take the absolute value of the default cache
433 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000434 */
drh9ccd8652013-09-13 16:36:46 +0000435 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09 +0000436 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000437 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000438 { OP_Transaction, 0, 0, 0}, /* 0 */
439 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000440 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000441 { OP_Integer, 0, 2, 0},
442 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000443 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000444 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000445 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000446 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000447 };
drh2ce18652016-01-16 20:50:21 +0000448 VdbeOp *aOp;
drhfb982642007-08-30 01:19:59 +0000449 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000450 if( !zRight ){
drh3c84ddf2008-01-09 02:15:38 +0000451 pParse->nMem += 2;
drhdad300d2016-01-18 00:20:26 +0000452 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
drh2ce18652016-01-16 20:50:21 +0000453 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
drhdad300d2016-01-18 00:20:26 +0000454 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000455 aOp[0].p1 = iDb;
456 aOp[1].p1 = iDb;
457 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
drhc11d4f92003-04-06 21:08:24 +0000458 }else{
drhd50ffc42011-03-08 02:38:28 +0000459 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000460 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh1861afc2016-02-01 21:48:34 +0000461 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
drh21206082011-04-04 18:22:02 +0000462 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000463 pDb->pSchema->cache_size = size;
464 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000465 }
drh9ccd8652013-09-13 16:36:46 +0000466 break;
467 }
drhe73c9142011-11-09 16:12:24 +0000468#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000469
drhe73c9142011-11-09 16:12:24 +0000470#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000471 /*
drh9b0cf342015-11-12 14:57:19 +0000472 ** PRAGMA [schema.]page_size
473 ** PRAGMA [schema.]page_size=N
drh90f5ecb2004-07-22 01:19:35 +0000474 **
475 ** The first form reports the current setting for the
476 ** database page size in bytes. The second form sets the
477 ** database page size value. The value can only be set if
478 ** the database has not yet been created.
479 */
drh9ccd8652013-09-13 16:36:46 +0000480 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000481 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000482 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000483 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000484 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drhc232aca2016-12-15 16:01:17 +0000485 returnSingleInt(v, size);
drh90f5ecb2004-07-22 01:19:35 +0000486 }else{
danielk1977992772c2007-08-30 10:07:38 +0000487 /* Malloc may fail when setting the page-size, as there is an internal
488 ** buffer that the pager module resizes using sqlite3_realloc().
489 */
drh60ac3f42010-11-23 18:59:27 +0000490 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000491 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
drh4a642b62016-02-05 01:55:27 +0000492 sqlite3OomFault(db);
danielk1977992772c2007-08-30 10:07:38 +0000493 }
drh90f5ecb2004-07-22 01:19:35 +0000494 }
drh9ccd8652013-09-13 16:36:46 +0000495 break;
496 }
danielk197741483462007-03-24 16:45:04 +0000497
498 /*
drh9b0cf342015-11-12 14:57:19 +0000499 ** PRAGMA [schema.]secure_delete
500 ** PRAGMA [schema.]secure_delete=ON/OFF
drh5b47efa2010-02-12 18:18:39 +0000501 **
502 ** The first form reports the current setting for the
503 ** secure_delete flag. The second form changes the secure_delete
504 ** flag setting and reports thenew value.
505 */
drh9ccd8652013-09-13 16:36:46 +0000506 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000507 Btree *pBt = pDb->pBt;
508 int b = -1;
509 assert( pBt!=0 );
510 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000511 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000512 }
drhaf034ed2010-02-12 19:46:26 +0000513 if( pId2->n==0 && b>=0 ){
514 int ii;
515 for(ii=0; ii<db->nDb; ii++){
516 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
517 }
518 }
drh5b47efa2010-02-12 18:18:39 +0000519 b = sqlite3BtreeSecureDelete(pBt, b);
drhc232aca2016-12-15 16:01:17 +0000520 returnSingleInt(v, b);
drh9ccd8652013-09-13 16:36:46 +0000521 break;
522 }
drh5b47efa2010-02-12 18:18:39 +0000523
524 /*
drh9b0cf342015-11-12 14:57:19 +0000525 ** PRAGMA [schema.]max_page_count
526 ** PRAGMA [schema.]max_page_count=N
drh60ac3f42010-11-23 18:59:27 +0000527 **
528 ** The first form reports the current setting for the
529 ** maximum number of pages in the database file. The
530 ** second form attempts to change this setting. Both
531 ** forms return the current setting.
532 **
drhe73c9142011-11-09 16:12:24 +0000533 ** The absolute value of N is used. This is undocumented and might
534 ** change. The only purpose is to provide an easy way to test
535 ** the sqlite3AbsInt32() function.
536 **
drh9b0cf342015-11-12 14:57:19 +0000537 ** PRAGMA [schema.]page_count
danielk197759a93792008-05-15 17:48:20 +0000538 **
539 ** Return the number of pages in the specified database.
540 */
drh9ccd8652013-09-13 16:36:46 +0000541 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000542 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000543 sqlite3CodeVerifySchema(pParse, iDb);
544 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000545 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000546 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
547 }else{
drhe73c9142011-11-09 16:12:24 +0000548 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
549 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000550 }
danielk197759a93792008-05-15 17:48:20 +0000551 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
drh9ccd8652013-09-13 16:36:46 +0000552 break;
553 }
danielk197759a93792008-05-15 17:48:20 +0000554
555 /*
drh9b0cf342015-11-12 14:57:19 +0000556 ** PRAGMA [schema.]locking_mode
557 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
danielk197741483462007-03-24 16:45:04 +0000558 */
drh9ccd8652013-09-13 16:36:46 +0000559 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000560 const char *zRet = "normal";
561 int eMode = getLockingMode(zRight);
562
563 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
564 /* Simple "PRAGMA locking_mode;" statement. This is a query for
565 ** the current default locking mode (which may be different to
566 ** the locking-mode of the main database).
567 */
568 eMode = db->dfltLockMode;
569 }else{
570 Pager *pPager;
571 if( pId2->n==0 ){
572 /* This indicates that no database name was specified as part
573 ** of the PRAGMA command. In this case the locking-mode must be
574 ** set on all attached databases, as well as the main db file.
575 **
576 ** Also, the sqlite3.dfltLockMode variable is set so that
577 ** any subsequently attached databases also use the specified
578 ** locking mode.
579 */
580 int ii;
581 assert(pDb==&db->aDb[0]);
582 for(ii=2; ii<db->nDb; ii++){
583 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
584 sqlite3PagerLockingMode(pPager, eMode);
585 }
drh4f21c4a2008-12-10 22:15:00 +0000586 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000587 }
588 pPager = sqlite3BtreePager(pDb->pBt);
589 eMode = sqlite3PagerLockingMode(pPager, eMode);
590 }
591
drh9ccd8652013-09-13 16:36:46 +0000592 assert( eMode==PAGER_LOCKINGMODE_NORMAL
593 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000594 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
595 zRet = "exclusive";
596 }
drhc232aca2016-12-15 16:01:17 +0000597 returnSingleText(v, zRet);
drh9ccd8652013-09-13 16:36:46 +0000598 break;
599 }
drh3b020132008-04-17 17:02:01 +0000600
601 /*
drh9b0cf342015-11-12 14:57:19 +0000602 ** PRAGMA [schema.]journal_mode
603 ** PRAGMA [schema.]journal_mode =
drh3ebaee92010-05-06 21:37:22 +0000604 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000605 */
drh9ccd8652013-09-13 16:36:46 +0000606 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000607 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
608 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000609
drh3b020132008-04-17 17:02:01 +0000610 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000611 /* If there is no "=MODE" part of the pragma, do a query for the
612 ** current mode */
drh3b020132008-04-17 17:02:01 +0000613 eMode = PAGER_JOURNALMODE_QUERY;
614 }else{
dane04dc882010-04-20 18:53:15 +0000615 const char *zMode;
drhea678832008-12-10 19:26:22 +0000616 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000617 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000618 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
619 }
620 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000621 /* If the "=MODE" part does not match any known journal mode,
622 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000623 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000624 }
625 }
drhc6b2a0f2010-07-08 17:40:37 +0000626 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
627 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
628 iDb = 0;
629 pId2->n = 1;
630 }
631 for(ii=db->nDb-1; ii>=0; ii--){
632 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
633 sqlite3VdbeUsesBtree(v, ii);
634 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000635 }
drh3b020132008-04-17 17:02:01 +0000636 }
drh3b020132008-04-17 17:02:01 +0000637 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000638 break;
639 }
danielk1977b53e4962008-06-04 06:45:59 +0000640
641 /*
drh9b0cf342015-11-12 14:57:19 +0000642 ** PRAGMA [schema.]journal_size_limit
643 ** PRAGMA [schema.]journal_size_limit=N
danielk1977b53e4962008-06-04 06:45:59 +0000644 **
drha9e364f2009-01-13 20:14:15 +0000645 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000646 */
drh9ccd8652013-09-13 16:36:46 +0000647 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000648 Pager *pPager = sqlite3BtreePager(pDb->pBt);
649 i64 iLimit = -2;
650 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000651 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000652 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000653 }
654 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drhc232aca2016-12-15 16:01:17 +0000655 returnSingleInt(v, iLimit);
drh9ccd8652013-09-13 16:36:46 +0000656 break;
657 }
danielk1977b53e4962008-06-04 06:45:59 +0000658
drh13d70422004-11-13 15:59:14 +0000659#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000660
661 /*
drh9b0cf342015-11-12 14:57:19 +0000662 ** PRAGMA [schema.]auto_vacuum
663 ** PRAGMA [schema.]auto_vacuum=N
danielk1977951af802004-11-05 15:45:09 +0000664 **
drha9e364f2009-01-13 20:14:15 +0000665 ** Get or set the value of the database 'auto-vacuum' parameter.
666 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000667 */
668#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000669 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000670 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000671 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000672 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000673 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000674 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000675 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000676 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000677 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000678 /* Call SetAutoVacuum() to set initialize the internal auto and
679 ** incr-vacuum flags. This is required in case this connection
680 ** creates the database file. It is important that it is created
681 ** as an auto-vacuum capable db.
682 */
683 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
684 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
685 /* When setting the auto_vacuum mode to either "full" or
686 ** "incremental", write the value of meta[6] in the database
687 ** file. Before writing to meta[6], check that meta[3] indicates
688 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000689 */
drhb06a4ec2014-03-10 18:03:09 +0000690 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000691 static const VdbeOpList setMeta6[] = {
692 { OP_Transaction, 0, 1, 0}, /* 0 */
693 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
694 { OP_If, 1, 0, 0}, /* 2 */
695 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
drh1861afc2016-02-01 21:48:34 +0000696 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
drhf63936e2013-10-03 14:08:07 +0000697 };
drh2ce18652016-01-16 20:50:21 +0000698 VdbeOp *aOp;
699 int iAddr = sqlite3VdbeCurrentAddr(v);
drhdad300d2016-01-18 00:20:26 +0000700 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
drh2ce18652016-01-16 20:50:21 +0000701 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhdad300d2016-01-18 00:20:26 +0000702 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000703 aOp[0].p1 = iDb;
704 aOp[1].p1 = iDb;
705 aOp[2].p2 = iAddr+4;
drh1861afc2016-02-01 21:48:34 +0000706 aOp[4].p1 = iDb;
707 aOp[4].p3 = eAuto - 1;
drhf63936e2013-10-03 14:08:07 +0000708 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000709 }
danielk1977951af802004-11-05 15:45:09 +0000710 }
drh9ccd8652013-09-13 16:36:46 +0000711 break;
712 }
danielk1977951af802004-11-05 15:45:09 +0000713#endif
714
drhca5557f2007-05-04 18:30:40 +0000715 /*
drh9b0cf342015-11-12 14:57:19 +0000716 ** PRAGMA [schema.]incremental_vacuum(N)
drhca5557f2007-05-04 18:30:40 +0000717 **
718 ** Do N steps of incremental vacuuming on a database.
719 */
720#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000721 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000722 int iLimit, addr;
723 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
724 iLimit = 0x7fffffff;
725 }
726 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000727 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000728 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000729 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000730 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000731 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000732 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000733 break;
734 }
drhca5557f2007-05-04 18:30:40 +0000735#endif
736
drh13d70422004-11-13 15:59:14 +0000737#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000738 /*
drh9b0cf342015-11-12 14:57:19 +0000739 ** PRAGMA [schema.]cache_size
740 ** PRAGMA [schema.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000741 **
742 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000743 ** page cache size. The second form sets the local
744 ** page cache size value. If N is positive then that is the
745 ** number of pages in the cache. If N is negative, then the
746 ** number of pages is adjusted so that the cache uses -N kibibytes
747 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000748 */
drh9ccd8652013-09-13 16:36:46 +0000749 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000750 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000751 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000752 returnSingleInt(v, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000753 }else{
drh3b42abb2011-11-09 14:23:04 +0000754 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000755 pDb->pSchema->cache_size = size;
756 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000757 }
drh9ccd8652013-09-13 16:36:46 +0000758 break;
759 }
drhc11d4f92003-04-06 21:08:24 +0000760
761 /*
drh9b0cf342015-11-12 14:57:19 +0000762 ** PRAGMA [schema.]cache_spill
763 ** PRAGMA cache_spill=BOOLEAN
764 ** PRAGMA [schema.]cache_spill=N
765 **
766 ** The first form reports the current local setting for the
767 ** page cache spill size. The second form turns cache spill on
768 ** or off. When turnning cache spill on, the size is set to the
769 ** current cache_size. The third form sets a spill size that
770 ** may be different form the cache size.
771 ** If N is positive then that is the
772 ** number of pages in the cache. If N is negative, then the
773 ** number of pages is adjusted so that the cache uses -N kibibytes
774 ** of memory.
775 **
776 ** If the number of cache_spill pages is less then the number of
777 ** cache_size pages, no spilling occurs until the page count exceeds
778 ** the number of cache_size pages.
779 **
780 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
781 ** not just the schema specified.
782 */
783 case PragTyp_CACHE_SPILL: {
drh9b0cf342015-11-12 14:57:19 +0000784 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
785 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000786 returnSingleInt(v,
drh9b0cf342015-11-12 14:57:19 +0000787 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
788 sqlite3BtreeSetSpillSize(pDb->pBt,0));
789 }else{
drh4f9c8ec2015-11-12 15:47:48 +0000790 int size = 1;
drh9b0cf342015-11-12 14:57:19 +0000791 if( sqlite3GetInt32(zRight, &size) ){
792 sqlite3BtreeSetSpillSize(pDb->pBt, size);
793 }
drh4f9c8ec2015-11-12 15:47:48 +0000794 if( sqlite3GetBoolean(zRight, size!=0) ){
drh9b0cf342015-11-12 14:57:19 +0000795 db->flags |= SQLITE_CacheSpill;
796 }else{
797 db->flags &= ~SQLITE_CacheSpill;
798 }
drh4f9c8ec2015-11-12 15:47:48 +0000799 setAllPagerFlags(db);
drh9b0cf342015-11-12 14:57:19 +0000800 }
801 break;
802 }
803
804 /*
805 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000806 **
drh0d0614b2013-03-25 23:09:28 +0000807 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000808 ** used to limit the aggregate size of all memory mapped regions of the
809 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000810 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000811 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000812 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000813 **
drh0d0614b2013-03-25 23:09:28 +0000814 ** This value is advisory. The underlying VFS is free to memory map
815 ** as little or as much as it wants. Except, if N is set to 0 then the
816 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000817 */
drh9ccd8652013-09-13 16:36:46 +0000818 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000819 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000820#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000821 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000822 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000823 int ii;
drh9296c182014-07-23 13:40:49 +0000824 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000825 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
826 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000827 for(ii=db->nDb-1; ii>=0; ii--){
828 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000829 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000830 }
831 }
dan5d8a1372013-03-19 19:28:06 +0000832 }
drh9b4c59f2013-04-15 17:03:42 +0000833 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000834 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000835#else
dan3719f5f2013-05-23 10:13:18 +0000836 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000837 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000838#endif
dan3719f5f2013-05-23 10:13:18 +0000839 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000840 returnSingleInt(v, sz);
dan3719f5f2013-05-23 10:13:18 +0000841 }else if( rc!=SQLITE_NOTFOUND ){
842 pParse->nErr++;
843 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000844 }
drh9ccd8652013-09-13 16:36:46 +0000845 break;
846 }
dan5d8a1372013-03-19 19:28:06 +0000847
848 /*
drh90f5ecb2004-07-22 01:19:35 +0000849 ** PRAGMA temp_store
850 ** PRAGMA temp_store = "default"|"memory"|"file"
851 **
852 ** Return or set the local value of the temp_store flag. Changing
853 ** the local value does not make changes to the disk file and the default
854 ** value will be restored the next time the database is opened.
855 **
856 ** Note that it is possible for the library compile-time options to
857 ** override this setting
858 */
drh9ccd8652013-09-13 16:36:46 +0000859 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000860 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000861 returnSingleInt(v, db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000862 }else{
863 changeTempStorage(pParse, zRight);
864 }
drh9ccd8652013-09-13 16:36:46 +0000865 break;
866 }
drh90f5ecb2004-07-22 01:19:35 +0000867
868 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000869 ** PRAGMA temp_store_directory
870 ** PRAGMA temp_store_directory = ""|"directory_name"
871 **
872 ** Return or set the local value of the temp_store_directory flag. Changing
873 ** the value sets a specific directory to be used for temporary files.
874 ** Setting to a null string reverts to the default temporary directory search.
875 ** If temporary directory is changed, then invalidateTempStorage.
876 **
877 */
drh9ccd8652013-09-13 16:36:46 +0000878 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000879 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000880 returnSingleText(v, sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000881 }else{
drh78f82d12008-09-02 00:52:52 +0000882#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000883 if( zRight[0] ){
884 int res;
danielk1977fab11272008-09-16 14:38:02 +0000885 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
886 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000887 sqlite3ErrorMsg(pParse, "not a writable directory");
888 goto pragma_out;
889 }
drh268283b2005-01-08 15:44:25 +0000890 }
danielk1977b06a0b62008-06-26 10:54:12 +0000891 if( SQLITE_TEMP_STORE==0
892 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
893 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000894 ){
895 invalidateTempStorage(pParse);
896 }
drh17435752007-08-16 04:30:38 +0000897 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000898 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000899 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000900 }else{
drh268283b2005-01-08 15:44:25 +0000901 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000902 }
drh78f82d12008-09-02 00:52:52 +0000903#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000904 }
drh9ccd8652013-09-13 16:36:46 +0000905 break;
906 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000907
drhcc716452012-06-06 23:23:23 +0000908#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000909 /*
910 ** PRAGMA data_store_directory
911 ** PRAGMA data_store_directory = ""|"directory_name"
912 **
913 ** Return or set the local value of the data_store_directory flag. Changing
914 ** the value sets a specific directory to be used for database files that
915 ** were specified with a relative pathname. Setting to a null string reverts
916 ** to the default database directory, which for database files specified with
917 ** a relative path will probably be based on the current directory for the
918 ** process. Database file specified with an absolute path are not impacted
919 ** by this setting, regardless of its value.
920 **
921 */
drh9ccd8652013-09-13 16:36:46 +0000922 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +0000923 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000924 returnSingleText(v, sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +0000925 }else{
926#ifndef SQLITE_OMIT_WSD
927 if( zRight[0] ){
928 int res;
929 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
930 if( rc!=SQLITE_OK || res==0 ){
931 sqlite3ErrorMsg(pParse, "not a writable directory");
932 goto pragma_out;
933 }
934 }
935 sqlite3_free(sqlite3_data_directory);
936 if( zRight[0] ){
937 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
938 }else{
939 sqlite3_data_directory = 0;
940 }
941#endif /* SQLITE_OMIT_WSD */
942 }
drh9ccd8652013-09-13 16:36:46 +0000943 break;
944 }
drhcc716452012-06-06 23:23:23 +0000945#endif
mistachkina112d142012-03-14 00:44:01 +0000946
drhd2cb50b2009-01-09 21:41:17 +0000947#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000948 /*
drh9b0cf342015-11-12 14:57:19 +0000949 ** PRAGMA [schema.]lock_proxy_file
950 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +0000951 **
952 ** Return or set the value of the lock_proxy_file flag. Changing
953 ** the value sets a specific file to be used for database access locks.
954 **
955 */
956 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +0000957 if( !zRight ){
958 Pager *pPager = sqlite3BtreePager(pDb->pBt);
959 char *proxy_file_path = NULL;
960 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000961 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000962 &proxy_file_path);
drhc232aca2016-12-15 16:01:17 +0000963 returnSingleText(v, proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +0000964 }else{
965 Pager *pPager = sqlite3BtreePager(pDb->pBt);
966 sqlite3_file *pFile = sqlite3PagerFile(pPager);
967 int res;
968 if( zRight[0] ){
969 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
970 zRight);
971 } else {
972 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
973 NULL);
974 }
975 if( res!=SQLITE_OK ){
976 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
977 goto pragma_out;
978 }
979 }
drh9ccd8652013-09-13 16:36:46 +0000980 break;
981 }
drhd2cb50b2009-01-09 21:41:17 +0000982#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000983
984 /*
drh9b0cf342015-11-12 14:57:19 +0000985 ** PRAGMA [schema.]synchronous
drh6841b1c2016-02-03 19:20:15 +0000986 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
drhc11d4f92003-04-06 21:08:24 +0000987 **
988 ** Return or set the local value of the synchronous flag. Changing
989 ** the local value does not make changes to the disk file and the
990 ** default value will be restored the next time the database is
991 ** opened.
992 */
drh9ccd8652013-09-13 16:36:46 +0000993 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +0000994 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000995 returnSingleInt(v, pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000996 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000997 if( !db->autoCommit ){
998 sqlite3ErrorMsg(pParse,
999 "Safety level may not be changed inside a transaction");
1000 }else{
drhd99d2832015-04-17 15:58:33 +00001001 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
1002 if( iLevel==0 ) iLevel = 1;
1003 pDb->safety_level = iLevel;
drh50a1a5a2016-03-08 14:40:11 +00001004 pDb->bSyncSet = 1;
drhd3605a42013-08-17 15:42:29 +00001005 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001006 }
drhc11d4f92003-04-06 21:08:24 +00001007 }
drh9ccd8652013-09-13 16:36:46 +00001008 break;
1009 }
drh13d70422004-11-13 15:59:14 +00001010#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001011
drhbf216272005-02-26 18:10:44 +00001012#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001013 case PragTyp_FLAG: {
1014 if( zRight==0 ){
drhc232aca2016-12-15 16:01:17 +00001015 setPragmaResultColumnNames(v, pPragma);
1016 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001017 }else{
drhc228be52015-01-31 02:00:01 +00001018 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001019 if( db->autoCommit==0 ){
1020 /* Foreign key support may not be enabled or disabled while not
1021 ** in auto-commit mode. */
1022 mask &= ~(SQLITE_ForeignKeys);
1023 }
drhd4530972014-09-09 14:47:53 +00001024#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001025 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001026 /* Do not allow non-admin users to modify the schema arbitrarily */
1027 mask &= ~(SQLITE_WriteSchema);
1028 }
1029#endif
drh9ccd8652013-09-13 16:36:46 +00001030
1031 if( sqlite3GetBoolean(zRight, 0) ){
1032 db->flags |= mask;
1033 }else{
1034 db->flags &= ~mask;
1035 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1036 }
1037
1038 /* Many of the flag-pragmas modify the code generated by the SQL
1039 ** compiler (eg. count_changes). So add an opcode to expire all
1040 ** compiled SQL statements after modifying a pragma value.
1041 */
drh01c736d2016-05-20 15:15:07 +00001042 sqlite3VdbeAddOp0(v, OP_Expire);
drh9ccd8652013-09-13 16:36:46 +00001043 setAllPagerFlags(db);
1044 }
1045 break;
1046 }
drhbf216272005-02-26 18:10:44 +00001047#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001048
drh13d70422004-11-13 15:59:14 +00001049#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001050 /*
1051 ** PRAGMA table_info(<table>)
1052 **
1053 ** Return a single row for each column of the named table. The columns of
1054 ** the returned data set are:
1055 **
1056 ** cid: Column id (numbered from left to right, starting at 0)
1057 ** name: Column name
1058 ** type: Column declaration type.
1059 ** notnull: True if 'NOT NULL' is part of column declaration
1060 ** dflt_value: The default value for the column, if any.
1061 */
drh9ccd8652013-09-13 16:36:46 +00001062 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001063 Table *pTab;
drh4d249e62016-06-10 22:49:01 +00001064 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001065 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001066 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001067 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001068 Column *pCol;
drh44156282013-10-23 22:23:03 +00001069 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001070 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001071 sqlite3CodeVerifySchema(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +00001072 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001073 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001074 if( IsHiddenColumn(pCol) ){
1075 nHidden++;
1076 continue;
1077 }
drh384b7fe2013-01-01 13:55:31 +00001078 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1079 k = 0;
1080 }else if( pPk==0 ){
1081 k = 1;
1082 }else{
drh1b678962015-04-15 07:19:27 +00001083 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001084 }
drh94fa9c42016-02-27 21:16:04 +00001085 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
drh076e85f2015-09-03 13:46:12 +00001086 sqlite3VdbeMultiLoad(v, 1, "issisi",
1087 i-nHidden,
drhd7564862016-03-22 20:05:09 +00001088 pCol->zName,
1089 sqlite3ColumnType(pCol,""),
drh076e85f2015-09-03 13:46:12 +00001090 pCol->notNull ? 1 : 0,
drh94fa9c42016-02-27 21:16:04 +00001091 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
drh076e85f2015-09-03 13:46:12 +00001092 k);
drh2d401ab2008-01-10 23:50:11 +00001093 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001094 }
1095 }
drh9ccd8652013-09-13 16:36:46 +00001096 }
1097 break;
drhc11d4f92003-04-06 21:08:24 +00001098
drh3ef26152013-10-12 20:22:00 +00001099 case PragTyp_STATS: {
1100 Index *pIdx;
1101 HashElem *i;
drh3ef26152013-10-12 20:22:00 +00001102 pParse->nMem = 4;
1103 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001104 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1105 Table *pTab = sqliteHashData(i);
drh076e85f2015-09-03 13:46:12 +00001106 sqlite3VdbeMultiLoad(v, 1, "ssii",
1107 pTab->zName,
1108 0,
drhd566c952016-02-25 21:19:03 +00001109 pTab->szTabRow,
1110 pTab->nRowLogEst);
drh3ef26152013-10-12 20:22:00 +00001111 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1112 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh076e85f2015-09-03 13:46:12 +00001113 sqlite3VdbeMultiLoad(v, 2, "sii",
1114 pIdx->zName,
drhd566c952016-02-25 21:19:03 +00001115 pIdx->szIdxRow,
1116 pIdx->aiRowLogEst[0]);
drh3ef26152013-10-12 20:22:00 +00001117 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1118 }
1119 }
1120 }
1121 break;
1122
drh9ccd8652013-09-13 16:36:46 +00001123 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001124 Index *pIdx;
1125 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001126 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001127 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001128 int i;
drh5e7028c2015-03-05 14:29:02 +00001129 int mx;
1130 if( pPragma->iArg ){
1131 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1132 mx = pIdx->nColumn;
1133 pParse->nMem = 6;
1134 }else{
1135 /* PRAGMA index_info (legacy version) */
1136 mx = pIdx->nKeyCol;
1137 pParse->nMem = 3;
1138 }
drhc11d4f92003-04-06 21:08:24 +00001139 pTab = pIdx->pTable;
drhc95e01d2013-02-14 16:16:05 +00001140 sqlite3CodeVerifySchema(pParse, iDb);
drhc232aca2016-12-15 16:01:17 +00001141 assert( pParse->nMem<=pPragma->nPragCName );
drhc228be52015-01-31 02:00:01 +00001142 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001143 i16 cnum = pIdx->aiColumn[i];
drh076e85f2015-09-03 13:46:12 +00001144 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum,
1145 cnum<0 ? 0 : pTab->aCol[cnum].zName);
drh5e7028c2015-03-05 14:29:02 +00001146 if( pPragma->iArg ){
drh076e85f2015-09-03 13:46:12 +00001147 sqlite3VdbeMultiLoad(v, 4, "isi",
1148 pIdx->aSortOrder[i],
1149 pIdx->azColl[i],
1150 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001151 }
1152 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001153 }
1154 }
drh9ccd8652013-09-13 16:36:46 +00001155 }
1156 break;
drhc11d4f92003-04-06 21:08:24 +00001157
drh9ccd8652013-09-13 16:36:46 +00001158 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001159 Index *pIdx;
1160 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001161 int i;
drh2783e4b2004-10-05 15:42:53 +00001162 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001163 if( pTab ){
drhc228be52015-01-31 02:00:01 +00001164 pParse->nMem = 5;
drhe13e9f52013-10-05 19:18:00 +00001165 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001166 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001167 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001168 sqlite3VdbeMultiLoad(v, 1, "isisi",
1169 i,
1170 pIdx->zName,
1171 IsUniqueIndex(pIdx),
1172 azOrigin[pIdx->idxType],
1173 pIdx->pPartIdxWhere!=0);
drhc228be52015-01-31 02:00:01 +00001174 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drhc11d4f92003-04-06 21:08:24 +00001175 }
1176 }
drh9ccd8652013-09-13 16:36:46 +00001177 }
1178 break;
drhc11d4f92003-04-06 21:08:24 +00001179
drh9ccd8652013-09-13 16:36:46 +00001180 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001181 int i;
drh2d401ab2008-01-10 23:50:11 +00001182 pParse->nMem = 3;
drh13d70422004-11-13 15:59:14 +00001183 for(i=0; i<db->nDb; i++){
1184 if( db->aDb[i].pBt==0 ) continue;
drh69c33822016-08-18 14:33:11 +00001185 assert( db->aDb[i].zDbSName!=0 );
drh076e85f2015-09-03 13:46:12 +00001186 sqlite3VdbeMultiLoad(v, 1, "iss",
1187 i,
drh69c33822016-08-18 14:33:11 +00001188 db->aDb[i].zDbSName,
drh076e85f2015-09-03 13:46:12 +00001189 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh2d401ab2008-01-10 23:50:11 +00001190 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001191 }
drh9ccd8652013-09-13 16:36:46 +00001192 }
1193 break;
danielk197748af65a2005-02-09 03:20:37 +00001194
drh9ccd8652013-09-13 16:36:46 +00001195 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001196 int i = 0;
1197 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001198 pParse->nMem = 2;
danielk197748af65a2005-02-09 03:20:37 +00001199 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1200 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001201 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
drh2d401ab2008-01-10 23:50:11 +00001202 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001203 }
drh9ccd8652013-09-13 16:36:46 +00001204 }
1205 break;
drh13d70422004-11-13 15:59:14 +00001206#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1207
drhb7f91642004-10-31 02:22:47 +00001208#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001209 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001210 FKey *pFK;
1211 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001212 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001213 if( pTab ){
drh78100cc2003-08-23 22:40:53 +00001214 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001215 if( pFK ){
1216 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001217 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001218 sqlite3CodeVerifySchema(pParse, iDb);
danielk1977742f9472004-06-16 12:02:43 +00001219 while(pFK){
1220 int j;
1221 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001222 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1223 i,
1224 j,
1225 pFK->zTo,
1226 pTab->aCol[pFK->aCol[j].iFrom].zName,
1227 pFK->aCol[j].zCol,
1228 actionName(pFK->aAction[1]), /* ON UPDATE */
1229 actionName(pFK->aAction[0]), /* ON DELETE */
1230 "NONE");
danielk197750af3e12008-10-10 17:47:21 +00001231 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001232 }
1233 ++i;
1234 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001235 }
drh78100cc2003-08-23 22:40:53 +00001236 }
1237 }
drh9ccd8652013-09-13 16:36:46 +00001238 }
1239 break;
drhb7f91642004-10-31 02:22:47 +00001240#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001241
drh6c5b9152012-12-17 16:46:37 +00001242#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001243#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001244 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001245 FKey *pFK; /* A foreign key constraint */
1246 Table *pTab; /* Child table contain "REFERENCES" keyword */
1247 Table *pParent; /* Parent table that child points to */
1248 Index *pIdx; /* Index in the parent table */
1249 int i; /* Loop counter: Foreign key number for pTab */
1250 int j; /* Loop counter: Field of the foreign key */
1251 HashElem *k; /* Loop counter: Next table in schema */
1252 int x; /* result variable */
1253 int regResult; /* 3 registers to hold a result row */
1254 int regKey; /* Register to hold key for checking the FK */
1255 int regRow; /* Registers to hold a row from pTab */
1256 int addrTop; /* Top of a loop checking foreign keys */
1257 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001258 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001259
drh613028b2012-12-17 18:43:02 +00001260 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001261 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001262 regKey = ++pParse->nMem;
1263 regRow = ++pParse->nMem;
drh613028b2012-12-17 18:43:02 +00001264 sqlite3CodeVerifySchema(pParse, iDb);
1265 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1266 while( k ){
1267 if( zRight ){
1268 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1269 k = 0;
1270 }else{
1271 pTab = (Table*)sqliteHashData(k);
1272 k = sqliteHashNext(k);
1273 }
drh9148def2012-12-17 20:40:39 +00001274 if( pTab==0 || pTab->pFKey==0 ) continue;
1275 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001276 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001277 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001278 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh6c5b9152012-12-17 16:46:37 +00001279 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001280 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1281 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001282 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001283 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001284 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001285 if( x==0 ){
1286 if( pIdx==0 ){
1287 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1288 }else{
drh6c5b9152012-12-17 16:46:37 +00001289 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001290 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001291 }
1292 }else{
drh613028b2012-12-17 18:43:02 +00001293 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001294 break;
1295 }
drh6c5b9152012-12-17 16:46:37 +00001296 }
dan5e878302013-10-12 19:06:48 +00001297 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001298 if( pFK ) break;
1299 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001300 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001301 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001302 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001303 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001304 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001305 if( pParent ){
1306 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1307 assert( x==0 );
1308 }
drh613028b2012-12-17 18:43:02 +00001309 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001310 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001311 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001312 assert( iKey>=0 && iKey<pTab->nCol );
1313 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001314 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1315 sqlite3ColumnDefault(v, pTab, iKey, regRow);
drh688852a2014-02-17 22:40:43 +00001316 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001317 }else{
drh613028b2012-12-17 18:43:02 +00001318 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001319 }
drheeb95652016-05-26 20:56:38 +00001320 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, 0, regRow); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001321 sqlite3VdbeGoto(v, addrOk);
drh613028b2012-12-17 18:43:02 +00001322 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1323 }else{
1324 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001325 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001326 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh688852a2014-02-17 22:40:43 +00001327 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001328 }
dan5e878302013-10-12 19:06:48 +00001329 if( pParent ){
drh57bf4a82014-02-17 14:59:22 +00001330 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
drhe9107692015-08-25 19:20:04 +00001331 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
dan5e878302013-10-12 19:06:48 +00001332 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh688852a2014-02-17 22:40:43 +00001333 VdbeCoverage(v);
dan5e878302013-10-12 19:06:48 +00001334 }
drh6c5b9152012-12-17 16:46:37 +00001335 }
drh613028b2012-12-17 18:43:02 +00001336 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh076e85f2015-09-03 13:46:12 +00001337 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001338 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001339 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001340 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001341 }
drh688852a2014-02-17 22:40:43 +00001342 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001343 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001344 }
drh9ccd8652013-09-13 16:36:46 +00001345 }
1346 break;
dan09ff9e12013-03-11 11:49:03 +00001347#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001348#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1349
drhc11d4f92003-04-06 21:08:24 +00001350#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001351 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001352 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001353 if( sqlite3GetBoolean(zRight, 0) ){
drh97e58a22015-11-10 14:27:17 +00001354 sqlite3ParserTrace(stdout, "parser: ");
drh55ef4d92005-08-14 01:20:37 +00001355 }else{
1356 sqlite3ParserTrace(0, 0);
1357 }
drhc11d4f92003-04-06 21:08:24 +00001358 }
drh9ccd8652013-09-13 16:36:46 +00001359 }
1360 break;
drhc11d4f92003-04-06 21:08:24 +00001361#endif
1362
drh55ef4d92005-08-14 01:20:37 +00001363 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1364 ** used will be case sensitive or not depending on the RHS.
1365 */
drh9ccd8652013-09-13 16:36:46 +00001366 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001367 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001368 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001369 }
drh9ccd8652013-09-13 16:36:46 +00001370 }
1371 break;
drh55ef4d92005-08-14 01:20:37 +00001372
drh1dcdbc02007-01-27 02:24:54 +00001373#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1374# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1375#endif
1376
drhb7f91642004-10-31 02:22:47 +00001377#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001378 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001379 ** integrity_check designed to detect most database corruption
1380 ** without most of the overhead of a full integrity-check.
1381 */
drh9ccd8652013-09-13 16:36:46 +00001382 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001383 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001384
drhc5227312011-10-13 17:09:01 +00001385 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001386
dan5885e762012-07-16 10:06:12 +00001387 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1388 ** then iDb is set to the index of the database identified by <db>.
1389 ** In this case, the integrity of database iDb only is verified by
1390 ** the VDBE created below.
1391 **
1392 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1393 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1394 ** to -1 here, to indicate that the VDBE should verify the integrity
1395 ** of all attached databases. */
1396 assert( iDb>=0 );
1397 assert( iDb==0 || pId2->z );
1398 if( pId2->z==0 ) iDb = -1;
1399
drhed717fe2003-06-15 23:42:24 +00001400 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001401 pParse->nMem = 6;
drh1dcdbc02007-01-27 02:24:54 +00001402
1403 /* Set the maximum error count */
1404 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1405 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001406 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001407 if( mxErr<=0 ){
1408 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1409 }
1410 }
drh2d401ab2008-01-10 23:50:11 +00001411 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001412
1413 /* Do an integrity check on each database file */
1414 for(i=0; i<db->nDb; i++){
1415 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001416 Hash *pTbls;
drh98968b22016-03-15 22:00:39 +00001417 int *aRoot;
drh79069752004-05-22 21:30:40 +00001418 int cnt = 0;
drhbb9b5f22016-03-19 00:35:02 +00001419 int mxIdx = 0;
1420 int nIdx;
drhed717fe2003-06-15 23:42:24 +00001421
danielk197753c0f742005-03-29 03:10:59 +00001422 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001423 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001424
drh80242052004-06-09 00:48:12 +00001425 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001426 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh688852a2014-02-17 22:40:43 +00001427 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001428 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001429 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001430
drhed717fe2003-06-15 23:42:24 +00001431 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001432 **
drh98968b22016-03-15 22:00:39 +00001433 ** Begin by finding the root pages numbers
drh2d401ab2008-01-10 23:50:11 +00001434 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001435 */
dan5885e762012-07-16 10:06:12 +00001436 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001437 pTbls = &db->aDb[i].pSchema->tblHash;
drh98968b22016-03-15 22:00:39 +00001438 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001439 Table *pTab = sqliteHashData(x);
1440 Index *pIdx;
drh98968b22016-03-15 22:00:39 +00001441 if( HasRowid(pTab) ) cnt++;
drhbb9b5f22016-03-19 00:35:02 +00001442 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
1443 if( nIdx>mxIdx ) mxIdx = nIdx;
drh98968b22016-03-15 22:00:39 +00001444 }
1445 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
1446 if( aRoot==0 ) break;
1447 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1448 Table *pTab = sqliteHashData(x);
1449 Index *pIdx;
1450 if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum;
drh79069752004-05-22 21:30:40 +00001451 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98968b22016-03-15 22:00:39 +00001452 aRoot[cnt++] = pIdx->tnum;
drh79069752004-05-22 21:30:40 +00001453 }
1454 }
drh98968b22016-03-15 22:00:39 +00001455 aRoot[cnt] = 0;
drh2d401ab2008-01-10 23:50:11 +00001456
1457 /* Make sure sufficient number of registers have been allocated */
drhbb9b5f22016-03-19 00:35:02 +00001458 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
drh2d401ab2008-01-10 23:50:11 +00001459
1460 /* Do the b-tree integrity checks */
drh98968b22016-03-15 22:00:39 +00001461 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
drh4f21c4a2008-12-10 22:15:00 +00001462 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001463 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001464 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh69c33822016-08-18 14:33:11 +00001465 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
drh66a51672008-01-03 00:01:23 +00001466 P4_DYNAMIC);
drh079a3072014-03-19 14:10:55 +00001467 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001468 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001469 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001470 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001471
1472 /* Make sure all the indices are constructed correctly.
1473 */
danielk197741c58b72007-12-29 13:39:19 +00001474 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001475 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001476 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001477 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001478 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001479 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001480 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001481
1482 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001483 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001484 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh688852a2014-02-17 22:40:43 +00001485 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001486 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001487 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001488 sqlite3ExprCacheClear(pParse);
danfd261ec2015-10-22 20:54:33 +00001489 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001490 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001491 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001492 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001493 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001494 }
drhbb9b5f22016-03-19 00:35:02 +00001495 assert( pParse->nMem>=8+j );
1496 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
drh688852a2014-02-17 22:40:43 +00001497 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001498 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhcefc87f2014-08-01 01:40:33 +00001499 /* Verify that all NOT NULL columns really are NOT NULL */
1500 for(j=0; j<pTab->nCol; j++){
1501 char *zErr;
1502 int jmp2, jmp3;
1503 if( j==pTab->iPKey ) continue;
1504 if( pTab->aCol[j].notNull==0 ) continue;
1505 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1506 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1507 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1508 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1509 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1510 pTab->aCol[j].zName);
1511 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1512 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1513 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1514 sqlite3VdbeAddOp0(v, OP_Halt);
1515 sqlite3VdbeJumpHere(v, jmp2);
1516 sqlite3VdbeJumpHere(v, jmp3);
1517 }
1518 /* Validate index entries for the current row */
drhed717fe2003-06-15 23:42:24 +00001519 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhcefc87f2014-08-01 01:40:33 +00001520 int jmp2, jmp3, jmp4, jmp5;
1521 int ckUniq = sqlite3VdbeMakeLabel(v);
drh6fbe41a2013-10-30 20:22:55 +00001522 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001523 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1524 pPrior, r1);
1525 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001526 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drhcefc87f2014-08-01 01:40:33 +00001527 /* Verify that an index entry exists for the current table row */
1528 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
drh688852a2014-02-17 22:40:43 +00001529 pIdx->nColumn); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001530 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001531 sqlite3VdbeLoadString(v, 3, "row ");
drh6fbe41a2013-10-30 20:22:55 +00001532 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001533 sqlite3VdbeLoadString(v, 4, " missing from index ");
drh6fbe41a2013-10-30 20:22:55 +00001534 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001535 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
drh6fbe41a2013-10-30 20:22:55 +00001536 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1537 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh688852a2014-02-17 22:40:43 +00001538 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001539 sqlite3VdbeAddOp0(v, OP_Halt);
drhd654be82005-09-20 17:42:23 +00001540 sqlite3VdbeJumpHere(v, jmp2);
drhcefc87f2014-08-01 01:40:33 +00001541 /* For UNIQUE indexes, verify that only one entry exists with the
1542 ** current key. The entry is unique if (1) any column is NULL
1543 ** or (2) the next entry has a different key */
1544 if( IsUniqueIndex(pIdx) ){
1545 int uniqOk = sqlite3VdbeMakeLabel(v);
1546 int jmp6;
1547 int kk;
1548 for(kk=0; kk<pIdx->nKeyCol; kk++){
1549 int iCol = pIdx->aiColumn[kk];
drh4b92f982015-09-29 17:20:14 +00001550 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
drh68391ac2015-09-25 20:49:16 +00001551 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
drhcefc87f2014-08-01 01:40:33 +00001552 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1553 VdbeCoverage(v);
1554 }
1555 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001556 sqlite3VdbeGoto(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00001557 sqlite3VdbeJumpHere(v, jmp6);
1558 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1559 pIdx->nKeyCol); VdbeCoverage(v);
1560 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001561 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1562 sqlite3VdbeGoto(v, jmp5);
drhcefc87f2014-08-01 01:40:33 +00001563 sqlite3VdbeResolveLabel(v, uniqOk);
1564 }
1565 sqlite3VdbeJumpHere(v, jmp4);
drh87744512014-04-13 19:15:49 +00001566 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhed717fe2003-06-15 23:42:24 +00001567 }
drh688852a2014-02-17 22:40:43 +00001568 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001569 sqlite3VdbeJumpHere(v, loopTop-1);
1570#ifndef SQLITE_OMIT_BTREECOUNT
drh076e85f2015-09-03 13:46:12 +00001571 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
drh8a9789b2013-08-01 03:36:59 +00001572 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001573 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001574 addr = sqlite3VdbeCurrentAddr(v);
drh688852a2014-02-17 22:40:43 +00001575 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001576 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001577 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh688852a2014-02-17 22:40:43 +00001578 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
drh5655c542014-02-19 19:14:34 +00001579 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh8a9789b2013-08-01 03:36:59 +00001580 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh076e85f2015-09-03 13:46:12 +00001581 sqlite3VdbeLoadString(v, 3, pIdx->zName);
drh8a9789b2013-08-01 03:36:59 +00001582 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1583 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001584 }
drh8a9789b2013-08-01 03:36:59 +00001585#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001586 }
1587 }
drh2ce18652016-01-16 20:50:21 +00001588 {
1589 static const int iLn = VDBE_OFFSET_LINENO(2);
1590 static const VdbeOpList endCode[] = {
1591 { OP_AddImm, 1, 0, 0}, /* 0 */
drh1b325542016-02-03 01:55:44 +00001592 { OP_If, 1, 4, 0}, /* 1 */
drh2ce18652016-01-16 20:50:21 +00001593 { OP_String8, 0, 3, 0}, /* 2 */
drh1b325542016-02-03 01:55:44 +00001594 { OP_ResultRow, 3, 1, 0}, /* 3 */
drh2ce18652016-01-16 20:50:21 +00001595 };
1596 VdbeOp *aOp;
1597
1598 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
1599 if( aOp ){
1600 aOp[0].p2 = -mxErr;
drh2ce18652016-01-16 20:50:21 +00001601 aOp[2].p4type = P4_STATIC;
1602 aOp[2].p4.z = "ok";
1603 }
1604 }
drh9ccd8652013-09-13 16:36:46 +00001605 }
1606 break;
drhb7f91642004-10-31 02:22:47 +00001607#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1608
drh13d70422004-11-13 15:59:14 +00001609#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001610 /*
1611 ** PRAGMA encoding
1612 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1613 **
drh85b623f2007-12-13 21:54:09 +00001614 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001615 ** database. If the database is not initialized, it is initialized now.
1616 **
1617 ** The second form of this pragma is a no-op if the main database file
1618 ** has not already been initialized. In this case it sets the default
1619 ** encoding that will be used for the main database file if a new file
1620 ** is created. If an existing main database file is opened, then the
1621 ** default text encoding for the existing database is used.
1622 **
1623 ** In all cases new databases created using the ATTACH command are
1624 ** created to use the same default text encoding as the main database. If
1625 ** the main database has not been initialized and/or created when ATTACH
1626 ** is executed, this is done before the ATTACH operation.
1627 **
1628 ** In the second form this pragma sets the text encoding to be used in
1629 ** new database files created using this database handle. It is only
1630 ** useful if invoked immediately after the main database i
1631 */
drh9ccd8652013-09-13 16:36:46 +00001632 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001633 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001634 char *zName;
1635 u8 enc;
1636 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001637 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001638 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1639 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1640 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001641 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001642 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001643 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1644 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001645 { 0, 0 }
1646 };
drh0f7eb612006-08-08 13:51:43 +00001647 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001648 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001649 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00001650 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1651 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1652 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drhc232aca2016-12-15 16:01:17 +00001653 returnSingleText(v, encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00001654 }else{ /* "PRAGMA encoding = XXX" */
1655 /* Only change the value of sqlite.enc if the database handle is not
1656 ** initialized. If the main database exists, the new sqlite.enc value
1657 ** will be overwritten when the schema is next loaded. If it does not
1658 ** already exists, it will be created to use the new encoding value.
1659 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001660 if(
1661 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1662 DbHasProperty(db, 0, DB_Empty)
1663 ){
danielk19778e227872004-06-07 07:52:17 +00001664 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1665 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh9bd3cc42014-12-12 23:17:54 +00001666 SCHEMA_ENC(db) = ENC(db) =
1667 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001668 break;
1669 }
1670 }
1671 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001672 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001673 }
1674 }
1675 }
drh9ccd8652013-09-13 16:36:46 +00001676 }
1677 break;
drh13d70422004-11-13 15:59:14 +00001678#endif /* SQLITE_OMIT_UTF16 */
1679
1680#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001681 /*
drh9b0cf342015-11-12 14:57:19 +00001682 ** PRAGMA [schema.]schema_version
1683 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001684 **
drh9b0cf342015-11-12 14:57:19 +00001685 ** PRAGMA [schema.]user_version
1686 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001687 **
drhe459bd42016-03-16 20:05:57 +00001688 ** PRAGMA [schema.]freelist_count
1689 **
1690 ** PRAGMA [schema.]data_version
drh4ee09b42013-05-01 19:49:27 +00001691 **
drh9b0cf342015-11-12 14:57:19 +00001692 ** PRAGMA [schema.]application_id
1693 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00001694 **
danielk1977b92b70b2004-11-12 16:11:59 +00001695 ** The pragma's schema_version and user_version are used to set or get
1696 ** the value of the schema-version and user-version, respectively. Both
1697 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001698 ** stored in the database header.
1699 **
1700 ** The schema-cookie is usually only manipulated internally by SQLite. It
1701 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001702 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001703 ** SQLite each time a query is executed to ensure that the internal cache
1704 ** of the schema used when compiling the SQL query matches the schema of
1705 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001706 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1707 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001708 ** crashes or database corruption. Use with caution!
1709 **
danielk1977b92b70b2004-11-12 16:11:59 +00001710 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001711 ** applications for any purpose.
1712 */
drh9ccd8652013-09-13 16:36:46 +00001713 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00001714 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00001715 sqlite3VdbeUsesBtree(v, iDb);
drhc232aca2016-12-15 16:01:17 +00001716 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00001717 /* Write the specified cookie value */
1718 static const VdbeOpList setCookie[] = {
1719 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1861afc2016-02-01 21:48:34 +00001720 { OP_SetCookie, 0, 0, 0}, /* 1 */
danielk1977dae24952004-11-11 05:10:43 +00001721 };
drh2ce18652016-01-16 20:50:21 +00001722 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001723 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
drh2ce18652016-01-16 20:50:21 +00001724 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
drhdad300d2016-01-18 00:20:26 +00001725 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001726 aOp[0].p1 = iDb;
drh1861afc2016-02-01 21:48:34 +00001727 aOp[1].p1 = iDb;
1728 aOp[1].p2 = iCookie;
1729 aOp[1].p3 = sqlite3Atoi(zRight);
danielk1977dae24952004-11-11 05:10:43 +00001730 }else{
1731 /* Read the specified cookie value */
1732 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001733 { OP_Transaction, 0, 0, 0}, /* 0 */
1734 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001735 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001736 };
drh2ce18652016-01-16 20:50:21 +00001737 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001738 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
drh2ce18652016-01-16 20:50:21 +00001739 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
drhdad300d2016-01-18 00:20:26 +00001740 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001741 aOp[0].p1 = iDb;
1742 aOp[1].p1 = iDb;
1743 aOp[1].p3 = iCookie;
drhf71a3662016-03-16 20:44:45 +00001744 sqlite3VdbeReusable(v);
danielk1977dae24952004-11-11 05:10:43 +00001745 }
drh9ccd8652013-09-13 16:36:46 +00001746 }
1747 break;
drh13d70422004-11-13 15:59:14 +00001748#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001749
shanehdc97a8c2010-02-23 20:08:35 +00001750#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1751 /*
1752 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001753 **
drh71caabf2010-02-26 15:39:24 +00001754 ** Return the names of all compile-time options used in this build,
1755 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001756 */
drh9ccd8652013-09-13 16:36:46 +00001757 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00001758 int i = 0;
1759 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00001760 pParse->nMem = 1;
shanehdc97a8c2010-02-23 20:08:35 +00001761 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00001762 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00001763 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1764 }
drhf71a3662016-03-16 20:44:45 +00001765 sqlite3VdbeReusable(v);
drh9ccd8652013-09-13 16:36:46 +00001766 }
1767 break;
shanehdc97a8c2010-02-23 20:08:35 +00001768#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1769
dan5cf53532010-05-01 16:40:20 +00001770#ifndef SQLITE_OMIT_WAL
1771 /*
drh9b0cf342015-11-12 14:57:19 +00001772 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00001773 **
1774 ** Checkpoint the database.
1775 */
drh9ccd8652013-09-13 16:36:46 +00001776 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00001777 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001778 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1779 if( zRight ){
1780 if( sqlite3StrICmp(zRight, "full")==0 ){
1781 eMode = SQLITE_CHECKPOINT_FULL;
1782 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1783 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00001784 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1785 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00001786 }
1787 }
dancdc1f042010-11-18 12:11:05 +00001788 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00001789 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001790 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00001791 }
1792 break;
dan5a299f92010-05-03 11:05:08 +00001793
1794 /*
1795 ** PRAGMA wal_autocheckpoint
1796 ** PRAGMA wal_autocheckpoint = N
1797 **
1798 ** Configure a database connection to automatically checkpoint a database
1799 ** after accumulating N frames in the log. Or query for the current value
1800 ** of N.
1801 */
drh9ccd8652013-09-13 16:36:46 +00001802 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00001803 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001804 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001805 }
drhc232aca2016-12-15 16:01:17 +00001806 returnSingleInt(v,
drhb033d8b2010-05-03 13:37:30 +00001807 db->xWalCallback==sqlite3WalDefaultHook ?
1808 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00001809 }
1810 break;
dan5cf53532010-05-01 16:40:20 +00001811#endif
dan7c246102010-04-12 19:00:29 +00001812
drh09419b42011-11-16 19:29:17 +00001813 /*
1814 ** PRAGMA shrink_memory
1815 **
drh51a74d42015-02-28 01:04:27 +00001816 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
1817 ** connection on which it is invoked to free up as much memory as it
1818 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00001819 */
drh9ccd8652013-09-13 16:36:46 +00001820 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00001821 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00001822 break;
1823 }
drh09419b42011-11-16 19:29:17 +00001824
drhf3603962012-09-07 16:46:59 +00001825 /*
1826 ** PRAGMA busy_timeout
1827 ** PRAGMA busy_timeout = N
1828 **
1829 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001830 ** if one is set. If no busy handler or a different busy handler is set
1831 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1832 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001833 */
drhd49c3582013-09-13 19:00:06 +00001834 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00001835 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00001836 if( zRight ){
1837 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1838 }
drhc232aca2016-12-15 16:01:17 +00001839 returnSingleInt(v, db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00001840 break;
1841 }
drhf3603962012-09-07 16:46:59 +00001842
drh55e85ca2013-09-13 21:01:56 +00001843 /*
1844 ** PRAGMA soft_heap_limit
1845 ** PRAGMA soft_heap_limit = N
1846 **
drh51a74d42015-02-28 01:04:27 +00001847 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
1848 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
1849 ** specified and is a non-negative integer.
1850 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
1851 ** returns the same integer that would be returned by the
1852 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00001853 */
1854 case PragTyp_SOFT_HEAP_LIMIT: {
1855 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00001856 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00001857 sqlite3_soft_heap_limit64(N);
1858 }
drhc232aca2016-12-15 16:01:17 +00001859 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00001860 break;
1861 }
1862
drh03459612014-08-25 15:13:22 +00001863 /*
1864 ** PRAGMA threads
1865 ** PRAGMA threads = N
1866 **
1867 ** Configure the maximum number of worker threads. Return the new
1868 ** maximum, which might be less than requested.
1869 */
1870 case PragTyp_THREADS: {
1871 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00001872 if( zRight
drh03459612014-08-25 15:13:22 +00001873 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
1874 && N>=0
1875 ){
drh111544c2014-08-29 16:20:47 +00001876 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00001877 }
drhc232aca2016-12-15 16:01:17 +00001878 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00001879 break;
1880 }
1881
dougcurrie81c95ef2004-06-18 23:21:47 +00001882#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001883 /*
1884 ** Report the current state of file logs for all databases
1885 */
drh9ccd8652013-09-13 16:36:46 +00001886 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00001887 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001888 "unlocked", "shared", "reserved", "pending", "exclusive"
1889 };
1890 int i;
drh2d401ab2008-01-10 23:50:11 +00001891 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00001892 for(i=0; i<db->nDb; i++){
1893 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001894 const char *zState = "unknown";
1895 int j;
drh69c33822016-08-18 14:33:11 +00001896 if( db->aDb[i].zDbSName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00001897 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001898 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001899 zState = "closed";
drh69c33822016-08-18 14:33:11 +00001900 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001901 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1902 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001903 }
drh69c33822016-08-18 14:33:11 +00001904 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
drh2d401ab2008-01-10 23:50:11 +00001905 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001906 }
drh9ccd8652013-09-13 16:36:46 +00001907 break;
1908 }
drh89ac8c12004-06-09 14:17:20 +00001909#endif
1910
shanehad9f9f62010-02-17 17:48:46 +00001911#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00001912 case PragTyp_KEY: {
1913 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1914 break;
1915 }
1916 case PragTyp_REKEY: {
1917 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1918 break;
1919 }
1920 case PragTyp_HEXKEY: {
1921 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00001922 u8 iByte;
1923 int i;
drh9ccd8652013-09-13 16:36:46 +00001924 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00001925 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
1926 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
1927 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00001928 }
1929 if( (zLeft[3] & 0xf)==0xb ){
1930 sqlite3_key_v2(db, zDb, zKey, i/2);
1931 }else{
1932 sqlite3_rekey_v2(db, zDb, zKey, i/2);
1933 }
drhd2cb50b2009-01-09 21:41:17 +00001934 }
drh9ccd8652013-09-13 16:36:46 +00001935 break;
1936 }
drh3c4f2a42005-12-08 18:12:56 +00001937#endif
shanehad9f9f62010-02-17 17:48:46 +00001938#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00001939 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001940#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001941 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001942 sqlite3_activate_see(&zRight[4]);
1943 }
1944#endif
1945#ifdef SQLITE_ENABLE_CEROD
1946 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001947 sqlite3_activate_cerod(&zRight[6]);
1948 }
1949#endif
drh9ccd8652013-09-13 16:36:46 +00001950 }
1951 break;
drh21e2cab2006-09-25 18:01:57 +00001952#endif
drh3c4f2a42005-12-08 18:12:56 +00001953
drh9ccd8652013-09-13 16:36:46 +00001954 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00001955
danielk1977e0048402004-06-15 16:51:01 +00001956pragma_out:
drh633e6d52008-07-28 19:34:53 +00001957 sqlite3DbFree(db, zLeft);
1958 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001959}
drh2fcc1592016-12-15 20:59:03 +00001960#ifndef SQLITE_OMIT_VIRTUALTABLE
1961/*****************************************************************************
1962** Implementation of an eponymous virtual table that runs a pragma.
1963**
1964*/
1965typedef struct PragmaVtab PragmaVtab;
1966typedef struct PragmaVtabCursor PragmaVtabCursor;
1967struct PragmaVtab {
1968 sqlite3_vtab base; /* Base class. Must be first */
1969 sqlite3 *db; /* The database connection to which it belongs */
1970 const PragmaName *pName; /* Name of the pragma */
1971 u8 nHidden; /* Number of hidden columns */
1972 u8 iHidden; /* Index of the first hidden column */
1973};
1974struct PragmaVtabCursor {
1975 sqlite3_vtab_cursor base; /* Base class. Must be first */
1976 sqlite3_stmt *pPragma; /* The pragma statement to run */
1977 sqlite_int64 iRowid; /* Current rowid */
1978 char *azArg[2]; /* Value of the argument and schema */
1979};
1980
1981/*
1982** Pragma virtual table module xConnect method.
1983*/
1984static int pragmaVtabConnect(
1985 sqlite3 *db,
1986 void *pAux,
1987 int argc, const char *const*argv,
1988 sqlite3_vtab **ppVtab,
1989 char **pzErr
1990){
1991 const PragmaName *pPragma = (const PragmaName*)pAux;
1992 PragmaVtab *pTab = 0;
1993 int rc;
1994 int i, j;
1995 char cSep = '(';
1996 StrAccum acc;
1997 char zBuf[200];
1998
drh344a1bf2016-12-22 14:53:25 +00001999 UNUSED_PARAMETER(argc);
2000 UNUSED_PARAMETER(argv);
drh2fcc1592016-12-15 20:59:03 +00002001 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
2002 sqlite3StrAccumAppendAll(&acc, "CREATE TABLE x");
2003 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
drhd7175eb2016-12-15 21:11:15 +00002004 sqlite3XPrintf(&acc, "%c\"%s\"", cSep, pragCName[j]);
drh2fcc1592016-12-15 20:59:03 +00002005 cSep = ',';
2006 }
drh9a63f092016-12-16 02:14:15 +00002007 if( i==0 ){
2008 sqlite3XPrintf(&acc, "(\"%s\"", pPragma->zName);
2009 cSep = ',';
2010 i++;
2011 }
drh2fcc1592016-12-15 20:59:03 +00002012 j = 0;
2013 if( pPragma->mPragFlg & PragFlg_Result1 ){
2014 sqlite3StrAccumAppendAll(&acc, ",arg HIDDEN");
2015 j++;
2016 }
2017 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
2018 sqlite3StrAccumAppendAll(&acc, ",schema HIDDEN");
2019 j++;
2020 }
2021 sqlite3StrAccumAppend(&acc, ")", 1);
2022 sqlite3StrAccumFinish(&acc);
2023 assert( strlen(zBuf) < sizeof(zBuf)-1 );
2024 rc = sqlite3_declare_vtab(db, zBuf);
2025 if( rc==SQLITE_OK ){
2026 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
2027 if( pTab==0 ){
2028 rc = SQLITE_NOMEM;
2029 }else{
2030 memset(pTab, 0, sizeof(PragmaVtab));
2031 pTab->pName = pPragma;
2032 pTab->db = db;
2033 pTab->iHidden = i;
2034 pTab->nHidden = j;
2035 }
2036 }else{
2037 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2038 }
2039
2040 *ppVtab = (sqlite3_vtab*)pTab;
2041 return rc;
2042}
2043
2044/*
2045** Pragma virtual table module xDisconnect method.
2046*/
2047static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
2048 PragmaVtab *pTab = (PragmaVtab*)pVtab;
2049 sqlite3_free(pTab);
2050 return SQLITE_OK;
2051}
2052
2053/* Figure out the best index to use to search a pragma virtual table.
2054**
2055** There are not really any index choices. But we want to encourage the
2056** query planner to give == constraints on as many hidden parameters as
2057** possible, and especially on the first hidden parameter. So return a
2058** high cost if hidden parameters are unconstrained.
2059*/
2060static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2061 PragmaVtab *pTab = (PragmaVtab*)tab;
2062 const struct sqlite3_index_constraint *pConstraint;
2063 int i, j;
2064 int seen[2];
2065
drhae7045c2016-12-15 21:33:55 +00002066 pIdxInfo->estimatedCost = (double)1;
drh2fcc1592016-12-15 20:59:03 +00002067 if( pTab->nHidden==0 ){ return SQLITE_OK; }
2068 pConstraint = pIdxInfo->aConstraint;
2069 seen[0] = 0;
2070 seen[1] = 0;
2071 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2072 if( pConstraint->usable==0 ) continue;
2073 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2074 if( pConstraint->iColumn < pTab->iHidden ) continue;
2075 j = pConstraint->iColumn - pTab->iHidden;
2076 assert( j < 2 );
drhd7175eb2016-12-15 21:11:15 +00002077 seen[j] = i+1;
drh2fcc1592016-12-15 20:59:03 +00002078 }
2079 if( seen[0]==0 ){
2080 pIdxInfo->estimatedCost = (double)2147483647;
2081 pIdxInfo->estimatedRows = 2147483647;
2082 return SQLITE_OK;
2083 }
drhd7175eb2016-12-15 21:11:15 +00002084 j = seen[0]-1;
drh2fcc1592016-12-15 20:59:03 +00002085 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
2086 pIdxInfo->aConstraintUsage[j].omit = 1;
2087 if( seen[1]==0 ) return SQLITE_OK;
2088 pIdxInfo->estimatedCost = (double)20;
2089 pIdxInfo->estimatedRows = 20;
drhd7175eb2016-12-15 21:11:15 +00002090 j = seen[1]-1;
drh2fcc1592016-12-15 20:59:03 +00002091 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
2092 pIdxInfo->aConstraintUsage[j].omit = 1;
2093 return SQLITE_OK;
2094}
2095
2096/* Create a new cursor for the pragma virtual table */
2097static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
2098 PragmaVtabCursor *pCsr;
2099 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
2100 if( pCsr==0 ) return SQLITE_NOMEM;
2101 memset(pCsr, 0, sizeof(PragmaVtabCursor));
2102 pCsr->base.pVtab = pVtab;
2103 *ppCursor = &pCsr->base;
2104 return SQLITE_OK;
2105}
2106
2107/* Clear all content from pragma virtual table cursor. */
2108static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
2109 int i;
2110 sqlite3_finalize(pCsr->pPragma);
2111 pCsr->pPragma = 0;
2112 for(i=0; i<ArraySize(pCsr->azArg); i++){
2113 sqlite3_free(pCsr->azArg[i]);
2114 pCsr->azArg[i] = 0;
2115 }
2116}
2117
2118/* Close a pragma virtual table cursor */
2119static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
2120 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
2121 pragmaVtabCursorClear(pCsr);
drh9a63f092016-12-16 02:14:15 +00002122 sqlite3_free(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002123 return SQLITE_OK;
2124}
2125
2126/* Advance the pragma virtual table cursor to the next row */
2127static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
2128 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2129 int rc = SQLITE_OK;
2130
2131 /* Increment the xRowid value */
2132 pCsr->iRowid++;
drh9a63f092016-12-16 02:14:15 +00002133 assert( pCsr->pPragma );
2134 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
2135 rc = sqlite3_finalize(pCsr->pPragma);
2136 pCsr->pPragma = 0;
2137 pragmaVtabCursorClear(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002138 }
2139 return rc;
2140}
2141
2142/*
2143** Pragma virtual table module xFilter method.
2144*/
2145static int pragmaVtabFilter(
2146 sqlite3_vtab_cursor *pVtabCursor,
2147 int idxNum, const char *idxStr,
2148 int argc, sqlite3_value **argv
2149){
2150 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2151 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2152 int rc;
drhd8b72002016-12-16 04:20:27 +00002153 int i, j;
drh2fcc1592016-12-15 20:59:03 +00002154 StrAccum acc;
2155 char *zSql;
2156
drh344a1bf2016-12-22 14:53:25 +00002157 UNUSED_PARAMETER(idxNum);
2158 UNUSED_PARAMETER(idxStr);
drh2fcc1592016-12-15 20:59:03 +00002159 pragmaVtabCursorClear(pCsr);
drhd8b72002016-12-16 04:20:27 +00002160 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
2161 for(i=0; i<argc; i++, j++){
2162 assert( j<ArraySize(pCsr->azArg) );
2163 pCsr->azArg[j] = sqlite3_mprintf("%s", sqlite3_value_text(argv[i]));
2164 if( pCsr->azArg[j]==0 ){
drh2fcc1592016-12-15 20:59:03 +00002165 return SQLITE_NOMEM;
2166 }
2167 }
drhd7175eb2016-12-15 21:11:15 +00002168 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
drh2fcc1592016-12-15 20:59:03 +00002169 sqlite3StrAccumAppendAll(&acc, "PRAGMA ");
2170 if( pCsr->azArg[1] ){
2171 sqlite3XPrintf(&acc, "%Q.", pCsr->azArg[1]);
2172 }
2173 sqlite3StrAccumAppendAll(&acc, pTab->pName->zName);
2174 if( pCsr->azArg[0] ){
2175 sqlite3XPrintf(&acc, "=%Q", pCsr->azArg[0]);
2176 }
2177 zSql = sqlite3StrAccumFinish(&acc);
2178 if( zSql==0 ) return SQLITE_NOMEM;
2179 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
2180 sqlite3_free(zSql);
2181 if( rc!=SQLITE_OK ){
2182 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
2183 return rc;
2184 }
2185 return pragmaVtabNext(pVtabCursor);
2186}
2187
2188/*
2189** Pragma virtual table module xEof method.
2190*/
2191static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
2192 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2193 return (pCsr->pPragma==0);
2194}
2195
2196/* The xColumn method simply returns the corresponding column from
2197** the PRAGMA.
2198*/
2199static int pragmaVtabColumn(
2200 sqlite3_vtab_cursor *pVtabCursor,
2201 sqlite3_context *ctx,
2202 int i
2203){
2204 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2205 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2206 if( i<pTab->iHidden ){
2207 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
2208 }else{
2209 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
2210 }
2211 return SQLITE_OK;
2212}
2213
2214/*
2215** Pragma virtual table module xRowid method.
2216*/
2217static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
2218 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2219 *p = pCsr->iRowid;
2220 return SQLITE_OK;
2221}
2222
2223/* The pragma virtual table object */
2224static const sqlite3_module pragmaVtabModule = {
2225 0, /* iVersion */
2226 0, /* xCreate - create a table */
2227 pragmaVtabConnect, /* xConnect - connect to an existing table */
2228 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
2229 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
2230 0, /* xDestroy - Drop a table */
2231 pragmaVtabOpen, /* xOpen - open a cursor */
2232 pragmaVtabClose, /* xClose - close a cursor */
2233 pragmaVtabFilter, /* xFilter - configure scan constraints */
2234 pragmaVtabNext, /* xNext - advance a cursor */
2235 pragmaVtabEof, /* xEof */
2236 pragmaVtabColumn, /* xColumn - read data */
2237 pragmaVtabRowid, /* xRowid - read data */
2238 0, /* xUpdate - write data */
2239 0, /* xBegin - begin transaction */
2240 0, /* xSync - sync transaction */
2241 0, /* xCommit - commit transaction */
2242 0, /* xRollback - rollback transaction */
2243 0, /* xFindFunction - function overloading */
2244 0, /* xRename - rename the table */
2245 0, /* xSavepoint */
2246 0, /* xRelease */
2247 0 /* xRollbackTo */
2248};
2249
2250/*
2251** Check to see if zTabName is really the name of a pragma. If it is,
2252** then register an eponymous virtual table for that pragma and return
2253** a pointer to the Module object for the new virtual table.
2254*/
2255Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
2256 const PragmaName *pName;
2257 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
2258 pName = pragmaLocate(zName+7);
2259 if( pName==0 ) return 0;
2260 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
2261 assert( sqlite3HashFind(&db->aModule, zName)==0 );
drhd7175eb2016-12-15 21:11:15 +00002262 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
drh2fcc1592016-12-15 20:59:03 +00002263}
2264
2265#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh13d70422004-11-13 15:59:14 +00002266
drh8bfdf722009-06-19 14:06:03 +00002267#endif /* SQLITE_OMIT_PRAGMA */