blob: 5251d0cd26140a2ca107540e402c804191f65987 [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 */
dan9e1ab1a2017-01-05 19:32:48 +0000411 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
412 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
413 ){
drhc232aca2016-12-15 16:01:17 +0000414 setPragmaResultColumnNames(v, pPragma);
415 }
416
drh9ccd8652013-09-13 16:36:46 +0000417 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01 +0000418 switch( pPragma->ePragTyp ){
drh9ccd8652013-09-13 16:36:46 +0000419
drhe73c9142011-11-09 16:12:24 +0000420#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000421 /*
drh9b0cf342015-11-12 14:57:19 +0000422 ** PRAGMA [schema.]default_cache_size
423 ** PRAGMA [schema.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000424 **
425 ** The first form reports the current persistent setting for the
426 ** page cache size. The value returned is the maximum number of
427 ** pages in the page cache. The second form sets both the current
428 ** page cache size value and the persistent page cache size value
429 ** stored in the database file.
430 **
drh93791ea2010-04-26 17:36:35 +0000431 ** Older versions of SQLite would set the default cache size to a
432 ** negative number to indicate synchronous=OFF. These days, synchronous
433 ** is always on by default regardless of the sign of the default cache
434 ** size. But continue to take the absolute value of the default cache
435 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000436 */
drh9ccd8652013-09-13 16:36:46 +0000437 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09 +0000438 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000439 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000440 { OP_Transaction, 0, 0, 0}, /* 0 */
441 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000442 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000443 { OP_Integer, 0, 2, 0},
444 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000445 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000446 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000447 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000448 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000449 };
drh2ce18652016-01-16 20:50:21 +0000450 VdbeOp *aOp;
drhfb982642007-08-30 01:19:59 +0000451 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000452 if( !zRight ){
drh3c84ddf2008-01-09 02:15:38 +0000453 pParse->nMem += 2;
drhdad300d2016-01-18 00:20:26 +0000454 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
drh2ce18652016-01-16 20:50:21 +0000455 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
drhdad300d2016-01-18 00:20:26 +0000456 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000457 aOp[0].p1 = iDb;
458 aOp[1].p1 = iDb;
459 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
drhc11d4f92003-04-06 21:08:24 +0000460 }else{
drhd50ffc42011-03-08 02:38:28 +0000461 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000462 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh1861afc2016-02-01 21:48:34 +0000463 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
drh21206082011-04-04 18:22:02 +0000464 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000465 pDb->pSchema->cache_size = size;
466 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000467 }
drh9ccd8652013-09-13 16:36:46 +0000468 break;
469 }
drhe73c9142011-11-09 16:12:24 +0000470#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000471
drhe73c9142011-11-09 16:12:24 +0000472#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000473 /*
drh9b0cf342015-11-12 14:57:19 +0000474 ** PRAGMA [schema.]page_size
475 ** PRAGMA [schema.]page_size=N
drh90f5ecb2004-07-22 01:19:35 +0000476 **
477 ** The first form reports the current setting for the
478 ** database page size in bytes. The second form sets the
479 ** database page size value. The value can only be set if
480 ** the database has not yet been created.
481 */
drh9ccd8652013-09-13 16:36:46 +0000482 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000483 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000484 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000485 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000486 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drhc232aca2016-12-15 16:01:17 +0000487 returnSingleInt(v, size);
drh90f5ecb2004-07-22 01:19:35 +0000488 }else{
danielk1977992772c2007-08-30 10:07:38 +0000489 /* Malloc may fail when setting the page-size, as there is an internal
490 ** buffer that the pager module resizes using sqlite3_realloc().
491 */
drh60ac3f42010-11-23 18:59:27 +0000492 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000493 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
drh4a642b62016-02-05 01:55:27 +0000494 sqlite3OomFault(db);
danielk1977992772c2007-08-30 10:07:38 +0000495 }
drh90f5ecb2004-07-22 01:19:35 +0000496 }
drh9ccd8652013-09-13 16:36:46 +0000497 break;
498 }
danielk197741483462007-03-24 16:45:04 +0000499
500 /*
drh9b0cf342015-11-12 14:57:19 +0000501 ** PRAGMA [schema.]secure_delete
502 ** PRAGMA [schema.]secure_delete=ON/OFF
drh5b47efa2010-02-12 18:18:39 +0000503 **
504 ** The first form reports the current setting for the
505 ** secure_delete flag. The second form changes the secure_delete
506 ** flag setting and reports thenew value.
507 */
drh9ccd8652013-09-13 16:36:46 +0000508 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000509 Btree *pBt = pDb->pBt;
510 int b = -1;
511 assert( pBt!=0 );
512 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000513 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000514 }
drhaf034ed2010-02-12 19:46:26 +0000515 if( pId2->n==0 && b>=0 ){
516 int ii;
517 for(ii=0; ii<db->nDb; ii++){
518 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
519 }
520 }
drh5b47efa2010-02-12 18:18:39 +0000521 b = sqlite3BtreeSecureDelete(pBt, b);
drhc232aca2016-12-15 16:01:17 +0000522 returnSingleInt(v, b);
drh9ccd8652013-09-13 16:36:46 +0000523 break;
524 }
drh5b47efa2010-02-12 18:18:39 +0000525
526 /*
drh9b0cf342015-11-12 14:57:19 +0000527 ** PRAGMA [schema.]max_page_count
528 ** PRAGMA [schema.]max_page_count=N
drh60ac3f42010-11-23 18:59:27 +0000529 **
530 ** The first form reports the current setting for the
531 ** maximum number of pages in the database file. The
532 ** second form attempts to change this setting. Both
533 ** forms return the current setting.
534 **
drhe73c9142011-11-09 16:12:24 +0000535 ** The absolute value of N is used. This is undocumented and might
536 ** change. The only purpose is to provide an easy way to test
537 ** the sqlite3AbsInt32() function.
538 **
drh9b0cf342015-11-12 14:57:19 +0000539 ** PRAGMA [schema.]page_count
danielk197759a93792008-05-15 17:48:20 +0000540 **
541 ** Return the number of pages in the specified database.
542 */
drh9ccd8652013-09-13 16:36:46 +0000543 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000544 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000545 sqlite3CodeVerifySchema(pParse, iDb);
546 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000547 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000548 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
549 }else{
drhe73c9142011-11-09 16:12:24 +0000550 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
551 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000552 }
danielk197759a93792008-05-15 17:48:20 +0000553 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
drh9ccd8652013-09-13 16:36:46 +0000554 break;
555 }
danielk197759a93792008-05-15 17:48:20 +0000556
557 /*
drh9b0cf342015-11-12 14:57:19 +0000558 ** PRAGMA [schema.]locking_mode
559 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
danielk197741483462007-03-24 16:45:04 +0000560 */
drh9ccd8652013-09-13 16:36:46 +0000561 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000562 const char *zRet = "normal";
563 int eMode = getLockingMode(zRight);
564
565 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
566 /* Simple "PRAGMA locking_mode;" statement. This is a query for
567 ** the current default locking mode (which may be different to
568 ** the locking-mode of the main database).
569 */
570 eMode = db->dfltLockMode;
571 }else{
572 Pager *pPager;
573 if( pId2->n==0 ){
574 /* This indicates that no database name was specified as part
575 ** of the PRAGMA command. In this case the locking-mode must be
576 ** set on all attached databases, as well as the main db file.
577 **
578 ** Also, the sqlite3.dfltLockMode variable is set so that
579 ** any subsequently attached databases also use the specified
580 ** locking mode.
581 */
582 int ii;
583 assert(pDb==&db->aDb[0]);
584 for(ii=2; ii<db->nDb; ii++){
585 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
586 sqlite3PagerLockingMode(pPager, eMode);
587 }
drh4f21c4a2008-12-10 22:15:00 +0000588 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000589 }
590 pPager = sqlite3BtreePager(pDb->pBt);
591 eMode = sqlite3PagerLockingMode(pPager, eMode);
592 }
593
drh9ccd8652013-09-13 16:36:46 +0000594 assert( eMode==PAGER_LOCKINGMODE_NORMAL
595 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000596 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
597 zRet = "exclusive";
598 }
drhc232aca2016-12-15 16:01:17 +0000599 returnSingleText(v, zRet);
drh9ccd8652013-09-13 16:36:46 +0000600 break;
601 }
drh3b020132008-04-17 17:02:01 +0000602
603 /*
drh9b0cf342015-11-12 14:57:19 +0000604 ** PRAGMA [schema.]journal_mode
605 ** PRAGMA [schema.]journal_mode =
drh3ebaee92010-05-06 21:37:22 +0000606 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000607 */
drh9ccd8652013-09-13 16:36:46 +0000608 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000609 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
610 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000611
drh3b020132008-04-17 17:02:01 +0000612 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000613 /* If there is no "=MODE" part of the pragma, do a query for the
614 ** current mode */
drh3b020132008-04-17 17:02:01 +0000615 eMode = PAGER_JOURNALMODE_QUERY;
616 }else{
dane04dc882010-04-20 18:53:15 +0000617 const char *zMode;
drhea678832008-12-10 19:26:22 +0000618 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000619 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000620 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
621 }
622 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000623 /* If the "=MODE" part does not match any known journal mode,
624 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000625 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000626 }
627 }
drhc6b2a0f2010-07-08 17:40:37 +0000628 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
629 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
630 iDb = 0;
631 pId2->n = 1;
632 }
633 for(ii=db->nDb-1; ii>=0; ii--){
634 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
635 sqlite3VdbeUsesBtree(v, ii);
636 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000637 }
drh3b020132008-04-17 17:02:01 +0000638 }
drh3b020132008-04-17 17:02:01 +0000639 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000640 break;
641 }
danielk1977b53e4962008-06-04 06:45:59 +0000642
643 /*
drh9b0cf342015-11-12 14:57:19 +0000644 ** PRAGMA [schema.]journal_size_limit
645 ** PRAGMA [schema.]journal_size_limit=N
danielk1977b53e4962008-06-04 06:45:59 +0000646 **
drha9e364f2009-01-13 20:14:15 +0000647 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000648 */
drh9ccd8652013-09-13 16:36:46 +0000649 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000650 Pager *pPager = sqlite3BtreePager(pDb->pBt);
651 i64 iLimit = -2;
652 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000653 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000654 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000655 }
656 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drhc232aca2016-12-15 16:01:17 +0000657 returnSingleInt(v, iLimit);
drh9ccd8652013-09-13 16:36:46 +0000658 break;
659 }
danielk1977b53e4962008-06-04 06:45:59 +0000660
drh13d70422004-11-13 15:59:14 +0000661#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000662
663 /*
drh9b0cf342015-11-12 14:57:19 +0000664 ** PRAGMA [schema.]auto_vacuum
665 ** PRAGMA [schema.]auto_vacuum=N
danielk1977951af802004-11-05 15:45:09 +0000666 **
drha9e364f2009-01-13 20:14:15 +0000667 ** Get or set the value of the database 'auto-vacuum' parameter.
668 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000669 */
670#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000671 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000672 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000673 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000674 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000675 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000676 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000677 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000678 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000679 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000680 /* Call SetAutoVacuum() to set initialize the internal auto and
681 ** incr-vacuum flags. This is required in case this connection
682 ** creates the database file. It is important that it is created
683 ** as an auto-vacuum capable db.
684 */
685 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
686 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
687 /* When setting the auto_vacuum mode to either "full" or
688 ** "incremental", write the value of meta[6] in the database
689 ** file. Before writing to meta[6], check that meta[3] indicates
690 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000691 */
drhb06a4ec2014-03-10 18:03:09 +0000692 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000693 static const VdbeOpList setMeta6[] = {
694 { OP_Transaction, 0, 1, 0}, /* 0 */
695 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
696 { OP_If, 1, 0, 0}, /* 2 */
697 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
drh1861afc2016-02-01 21:48:34 +0000698 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
drhf63936e2013-10-03 14:08:07 +0000699 };
drh2ce18652016-01-16 20:50:21 +0000700 VdbeOp *aOp;
701 int iAddr = sqlite3VdbeCurrentAddr(v);
drhdad300d2016-01-18 00:20:26 +0000702 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
drh2ce18652016-01-16 20:50:21 +0000703 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhdad300d2016-01-18 00:20:26 +0000704 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000705 aOp[0].p1 = iDb;
706 aOp[1].p1 = iDb;
707 aOp[2].p2 = iAddr+4;
drh1861afc2016-02-01 21:48:34 +0000708 aOp[4].p1 = iDb;
709 aOp[4].p3 = eAuto - 1;
drhf63936e2013-10-03 14:08:07 +0000710 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000711 }
danielk1977951af802004-11-05 15:45:09 +0000712 }
drh9ccd8652013-09-13 16:36:46 +0000713 break;
714 }
danielk1977951af802004-11-05 15:45:09 +0000715#endif
716
drhca5557f2007-05-04 18:30:40 +0000717 /*
drh9b0cf342015-11-12 14:57:19 +0000718 ** PRAGMA [schema.]incremental_vacuum(N)
drhca5557f2007-05-04 18:30:40 +0000719 **
720 ** Do N steps of incremental vacuuming on a database.
721 */
722#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000723 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000724 int iLimit, addr;
725 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
726 iLimit = 0x7fffffff;
727 }
728 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000729 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000730 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000731 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000732 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000733 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000734 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000735 break;
736 }
drhca5557f2007-05-04 18:30:40 +0000737#endif
738
drh13d70422004-11-13 15:59:14 +0000739#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000740 /*
drh9b0cf342015-11-12 14:57:19 +0000741 ** PRAGMA [schema.]cache_size
742 ** PRAGMA [schema.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000743 **
744 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000745 ** page cache size. The second form sets the local
746 ** page cache size value. If N is positive then that is the
747 ** number of pages in the cache. If N is negative, then the
748 ** number of pages is adjusted so that the cache uses -N kibibytes
749 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000750 */
drh9ccd8652013-09-13 16:36:46 +0000751 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000752 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000753 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000754 returnSingleInt(v, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000755 }else{
drh3b42abb2011-11-09 14:23:04 +0000756 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000757 pDb->pSchema->cache_size = size;
758 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000759 }
drh9ccd8652013-09-13 16:36:46 +0000760 break;
761 }
drhc11d4f92003-04-06 21:08:24 +0000762
763 /*
drh9b0cf342015-11-12 14:57:19 +0000764 ** PRAGMA [schema.]cache_spill
765 ** PRAGMA cache_spill=BOOLEAN
766 ** PRAGMA [schema.]cache_spill=N
767 **
768 ** The first form reports the current local setting for the
769 ** page cache spill size. The second form turns cache spill on
770 ** or off. When turnning cache spill on, the size is set to the
771 ** current cache_size. The third form sets a spill size that
772 ** may be different form the cache size.
773 ** If N is positive then that is the
774 ** number of pages in the cache. If N is negative, then the
775 ** number of pages is adjusted so that the cache uses -N kibibytes
776 ** of memory.
777 **
778 ** If the number of cache_spill pages is less then the number of
779 ** cache_size pages, no spilling occurs until the page count exceeds
780 ** the number of cache_size pages.
781 **
782 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
783 ** not just the schema specified.
784 */
785 case PragTyp_CACHE_SPILL: {
drh9b0cf342015-11-12 14:57:19 +0000786 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
787 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000788 returnSingleInt(v,
drh9b0cf342015-11-12 14:57:19 +0000789 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
790 sqlite3BtreeSetSpillSize(pDb->pBt,0));
791 }else{
drh4f9c8ec2015-11-12 15:47:48 +0000792 int size = 1;
drh9b0cf342015-11-12 14:57:19 +0000793 if( sqlite3GetInt32(zRight, &size) ){
794 sqlite3BtreeSetSpillSize(pDb->pBt, size);
795 }
drh4f9c8ec2015-11-12 15:47:48 +0000796 if( sqlite3GetBoolean(zRight, size!=0) ){
drh9b0cf342015-11-12 14:57:19 +0000797 db->flags |= SQLITE_CacheSpill;
798 }else{
799 db->flags &= ~SQLITE_CacheSpill;
800 }
drh4f9c8ec2015-11-12 15:47:48 +0000801 setAllPagerFlags(db);
drh9b0cf342015-11-12 14:57:19 +0000802 }
803 break;
804 }
805
806 /*
807 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000808 **
drh0d0614b2013-03-25 23:09:28 +0000809 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000810 ** used to limit the aggregate size of all memory mapped regions of the
811 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000812 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000813 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000814 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000815 **
drh0d0614b2013-03-25 23:09:28 +0000816 ** This value is advisory. The underlying VFS is free to memory map
817 ** as little or as much as it wants. Except, if N is set to 0 then the
818 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000819 */
drh9ccd8652013-09-13 16:36:46 +0000820 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000821 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000822#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000823 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000824 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000825 int ii;
drh9296c182014-07-23 13:40:49 +0000826 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000827 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
828 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000829 for(ii=db->nDb-1; ii>=0; ii--){
830 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000831 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000832 }
833 }
dan5d8a1372013-03-19 19:28:06 +0000834 }
drh9b4c59f2013-04-15 17:03:42 +0000835 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000836 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000837#else
dan3719f5f2013-05-23 10:13:18 +0000838 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000839 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000840#endif
dan3719f5f2013-05-23 10:13:18 +0000841 if( rc==SQLITE_OK ){
drhc232aca2016-12-15 16:01:17 +0000842 returnSingleInt(v, sz);
dan3719f5f2013-05-23 10:13:18 +0000843 }else if( rc!=SQLITE_NOTFOUND ){
844 pParse->nErr++;
845 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000846 }
drh9ccd8652013-09-13 16:36:46 +0000847 break;
848 }
dan5d8a1372013-03-19 19:28:06 +0000849
850 /*
drh90f5ecb2004-07-22 01:19:35 +0000851 ** PRAGMA temp_store
852 ** PRAGMA temp_store = "default"|"memory"|"file"
853 **
854 ** Return or set the local value of the temp_store flag. Changing
855 ** the local value does not make changes to the disk file and the default
856 ** value will be restored the next time the database is opened.
857 **
858 ** Note that it is possible for the library compile-time options to
859 ** override this setting
860 */
drh9ccd8652013-09-13 16:36:46 +0000861 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000862 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000863 returnSingleInt(v, db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000864 }else{
865 changeTempStorage(pParse, zRight);
866 }
drh9ccd8652013-09-13 16:36:46 +0000867 break;
868 }
drh90f5ecb2004-07-22 01:19:35 +0000869
870 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000871 ** PRAGMA temp_store_directory
872 ** PRAGMA temp_store_directory = ""|"directory_name"
873 **
874 ** Return or set the local value of the temp_store_directory flag. Changing
875 ** the value sets a specific directory to be used for temporary files.
876 ** Setting to a null string reverts to the default temporary directory search.
877 ** If temporary directory is changed, then invalidateTempStorage.
878 **
879 */
drh9ccd8652013-09-13 16:36:46 +0000880 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000881 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000882 returnSingleText(v, sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000883 }else{
drh78f82d12008-09-02 00:52:52 +0000884#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000885 if( zRight[0] ){
886 int res;
danielk1977fab11272008-09-16 14:38:02 +0000887 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
888 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000889 sqlite3ErrorMsg(pParse, "not a writable directory");
890 goto pragma_out;
891 }
drh268283b2005-01-08 15:44:25 +0000892 }
danielk1977b06a0b62008-06-26 10:54:12 +0000893 if( SQLITE_TEMP_STORE==0
894 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
895 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000896 ){
897 invalidateTempStorage(pParse);
898 }
drh17435752007-08-16 04:30:38 +0000899 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000900 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000901 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000902 }else{
drh268283b2005-01-08 15:44:25 +0000903 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000904 }
drh78f82d12008-09-02 00:52:52 +0000905#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000906 }
drh9ccd8652013-09-13 16:36:46 +0000907 break;
908 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000909
drhcc716452012-06-06 23:23:23 +0000910#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000911 /*
912 ** PRAGMA data_store_directory
913 ** PRAGMA data_store_directory = ""|"directory_name"
914 **
915 ** Return or set the local value of the data_store_directory flag. Changing
916 ** the value sets a specific directory to be used for database files that
917 ** were specified with a relative pathname. Setting to a null string reverts
918 ** to the default database directory, which for database files specified with
919 ** a relative path will probably be based on the current directory for the
920 ** process. Database file specified with an absolute path are not impacted
921 ** by this setting, regardless of its value.
922 **
923 */
drh9ccd8652013-09-13 16:36:46 +0000924 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +0000925 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000926 returnSingleText(v, sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +0000927 }else{
928#ifndef SQLITE_OMIT_WSD
929 if( zRight[0] ){
930 int res;
931 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
932 if( rc!=SQLITE_OK || res==0 ){
933 sqlite3ErrorMsg(pParse, "not a writable directory");
934 goto pragma_out;
935 }
936 }
937 sqlite3_free(sqlite3_data_directory);
938 if( zRight[0] ){
939 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
940 }else{
941 sqlite3_data_directory = 0;
942 }
943#endif /* SQLITE_OMIT_WSD */
944 }
drh9ccd8652013-09-13 16:36:46 +0000945 break;
946 }
drhcc716452012-06-06 23:23:23 +0000947#endif
mistachkina112d142012-03-14 00:44:01 +0000948
drhd2cb50b2009-01-09 21:41:17 +0000949#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000950 /*
drh9b0cf342015-11-12 14:57:19 +0000951 ** PRAGMA [schema.]lock_proxy_file
952 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +0000953 **
954 ** Return or set the value of the lock_proxy_file flag. Changing
955 ** the value sets a specific file to be used for database access locks.
956 **
957 */
958 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +0000959 if( !zRight ){
960 Pager *pPager = sqlite3BtreePager(pDb->pBt);
961 char *proxy_file_path = NULL;
962 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000963 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000964 &proxy_file_path);
drhc232aca2016-12-15 16:01:17 +0000965 returnSingleText(v, proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +0000966 }else{
967 Pager *pPager = sqlite3BtreePager(pDb->pBt);
968 sqlite3_file *pFile = sqlite3PagerFile(pPager);
969 int res;
970 if( zRight[0] ){
971 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
972 zRight);
973 } else {
974 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
975 NULL);
976 }
977 if( res!=SQLITE_OK ){
978 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
979 goto pragma_out;
980 }
981 }
drh9ccd8652013-09-13 16:36:46 +0000982 break;
983 }
drhd2cb50b2009-01-09 21:41:17 +0000984#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000985
986 /*
drh9b0cf342015-11-12 14:57:19 +0000987 ** PRAGMA [schema.]synchronous
drh6841b1c2016-02-03 19:20:15 +0000988 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
drhc11d4f92003-04-06 21:08:24 +0000989 **
990 ** Return or set the local value of the synchronous flag. Changing
991 ** the local value does not make changes to the disk file and the
992 ** default value will be restored the next time the database is
993 ** opened.
994 */
drh9ccd8652013-09-13 16:36:46 +0000995 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +0000996 if( !zRight ){
drhc232aca2016-12-15 16:01:17 +0000997 returnSingleInt(v, pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000998 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000999 if( !db->autoCommit ){
1000 sqlite3ErrorMsg(pParse,
1001 "Safety level may not be changed inside a transaction");
1002 }else{
drhd99d2832015-04-17 15:58:33 +00001003 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
1004 if( iLevel==0 ) iLevel = 1;
1005 pDb->safety_level = iLevel;
drh50a1a5a2016-03-08 14:40:11 +00001006 pDb->bSyncSet = 1;
drhd3605a42013-08-17 15:42:29 +00001007 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001008 }
drhc11d4f92003-04-06 21:08:24 +00001009 }
drh9ccd8652013-09-13 16:36:46 +00001010 break;
1011 }
drh13d70422004-11-13 15:59:14 +00001012#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001013
drhbf216272005-02-26 18:10:44 +00001014#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001015 case PragTyp_FLAG: {
1016 if( zRight==0 ){
drhc232aca2016-12-15 16:01:17 +00001017 setPragmaResultColumnNames(v, pPragma);
1018 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001019 }else{
drhc228be52015-01-31 02:00:01 +00001020 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001021 if( db->autoCommit==0 ){
1022 /* Foreign key support may not be enabled or disabled while not
1023 ** in auto-commit mode. */
1024 mask &= ~(SQLITE_ForeignKeys);
1025 }
drhd4530972014-09-09 14:47:53 +00001026#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001027 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001028 /* Do not allow non-admin users to modify the schema arbitrarily */
1029 mask &= ~(SQLITE_WriteSchema);
1030 }
1031#endif
drh9ccd8652013-09-13 16:36:46 +00001032
1033 if( sqlite3GetBoolean(zRight, 0) ){
1034 db->flags |= mask;
1035 }else{
1036 db->flags &= ~mask;
1037 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1038 }
1039
1040 /* Many of the flag-pragmas modify the code generated by the SQL
1041 ** compiler (eg. count_changes). So add an opcode to expire all
1042 ** compiled SQL statements after modifying a pragma value.
1043 */
drh01c736d2016-05-20 15:15:07 +00001044 sqlite3VdbeAddOp0(v, OP_Expire);
drh9ccd8652013-09-13 16:36:46 +00001045 setAllPagerFlags(db);
1046 }
1047 break;
1048 }
drhbf216272005-02-26 18:10:44 +00001049#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001050
drh13d70422004-11-13 15:59:14 +00001051#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001052 /*
1053 ** PRAGMA table_info(<table>)
1054 **
1055 ** Return a single row for each column of the named table. The columns of
1056 ** the returned data set are:
1057 **
1058 ** cid: Column id (numbered from left to right, starting at 0)
1059 ** name: Column name
1060 ** type: Column declaration type.
1061 ** notnull: True if 'NOT NULL' is part of column declaration
1062 ** dflt_value: The default value for the column, if any.
1063 */
drh9ccd8652013-09-13 16:36:46 +00001064 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001065 Table *pTab;
drh4d249e62016-06-10 22:49:01 +00001066 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001067 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001068 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001069 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001070 Column *pCol;
drh44156282013-10-23 22:23:03 +00001071 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001072 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001073 sqlite3CodeVerifySchema(pParse, iDb);
danielk19774adee202004-05-08 08:23:19 +00001074 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001075 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001076 if( IsHiddenColumn(pCol) ){
1077 nHidden++;
1078 continue;
1079 }
drh384b7fe2013-01-01 13:55:31 +00001080 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1081 k = 0;
1082 }else if( pPk==0 ){
1083 k = 1;
1084 }else{
drh1b678962015-04-15 07:19:27 +00001085 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001086 }
drh94fa9c42016-02-27 21:16:04 +00001087 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
drh076e85f2015-09-03 13:46:12 +00001088 sqlite3VdbeMultiLoad(v, 1, "issisi",
1089 i-nHidden,
drhd7564862016-03-22 20:05:09 +00001090 pCol->zName,
1091 sqlite3ColumnType(pCol,""),
drh076e85f2015-09-03 13:46:12 +00001092 pCol->notNull ? 1 : 0,
drh94fa9c42016-02-27 21:16:04 +00001093 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
drh076e85f2015-09-03 13:46:12 +00001094 k);
drh2d401ab2008-01-10 23:50:11 +00001095 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001096 }
1097 }
drh9ccd8652013-09-13 16:36:46 +00001098 }
1099 break;
drhc11d4f92003-04-06 21:08:24 +00001100
drh3ef26152013-10-12 20:22:00 +00001101 case PragTyp_STATS: {
1102 Index *pIdx;
1103 HashElem *i;
drh3ef26152013-10-12 20:22:00 +00001104 pParse->nMem = 4;
1105 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001106 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1107 Table *pTab = sqliteHashData(i);
drh076e85f2015-09-03 13:46:12 +00001108 sqlite3VdbeMultiLoad(v, 1, "ssii",
1109 pTab->zName,
1110 0,
drhd566c952016-02-25 21:19:03 +00001111 pTab->szTabRow,
1112 pTab->nRowLogEst);
drh3ef26152013-10-12 20:22:00 +00001113 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1114 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh076e85f2015-09-03 13:46:12 +00001115 sqlite3VdbeMultiLoad(v, 2, "sii",
1116 pIdx->zName,
drhd566c952016-02-25 21:19:03 +00001117 pIdx->szIdxRow,
1118 pIdx->aiRowLogEst[0]);
drh3ef26152013-10-12 20:22:00 +00001119 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1120 }
1121 }
1122 }
1123 break;
1124
drh9ccd8652013-09-13 16:36:46 +00001125 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001126 Index *pIdx;
1127 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001128 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001129 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001130 int i;
drh5e7028c2015-03-05 14:29:02 +00001131 int mx;
1132 if( pPragma->iArg ){
1133 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1134 mx = pIdx->nColumn;
1135 pParse->nMem = 6;
1136 }else{
1137 /* PRAGMA index_info (legacy version) */
1138 mx = pIdx->nKeyCol;
1139 pParse->nMem = 3;
1140 }
drhc11d4f92003-04-06 21:08:24 +00001141 pTab = pIdx->pTable;
drhc95e01d2013-02-14 16:16:05 +00001142 sqlite3CodeVerifySchema(pParse, iDb);
drhc232aca2016-12-15 16:01:17 +00001143 assert( pParse->nMem<=pPragma->nPragCName );
drhc228be52015-01-31 02:00:01 +00001144 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001145 i16 cnum = pIdx->aiColumn[i];
drh076e85f2015-09-03 13:46:12 +00001146 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum,
1147 cnum<0 ? 0 : pTab->aCol[cnum].zName);
drh5e7028c2015-03-05 14:29:02 +00001148 if( pPragma->iArg ){
drh076e85f2015-09-03 13:46:12 +00001149 sqlite3VdbeMultiLoad(v, 4, "isi",
1150 pIdx->aSortOrder[i],
1151 pIdx->azColl[i],
1152 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001153 }
1154 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001155 }
1156 }
drh9ccd8652013-09-13 16:36:46 +00001157 }
1158 break;
drhc11d4f92003-04-06 21:08:24 +00001159
drh9ccd8652013-09-13 16:36:46 +00001160 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001161 Index *pIdx;
1162 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001163 int i;
drh2783e4b2004-10-05 15:42:53 +00001164 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001165 if( pTab ){
drhc228be52015-01-31 02:00:01 +00001166 pParse->nMem = 5;
drhe13e9f52013-10-05 19:18:00 +00001167 sqlite3CodeVerifySchema(pParse, iDb);
drh3ef26152013-10-12 20:22:00 +00001168 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001169 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001170 sqlite3VdbeMultiLoad(v, 1, "isisi",
1171 i,
1172 pIdx->zName,
1173 IsUniqueIndex(pIdx),
1174 azOrigin[pIdx->idxType],
1175 pIdx->pPartIdxWhere!=0);
drhc228be52015-01-31 02:00:01 +00001176 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drhc11d4f92003-04-06 21:08:24 +00001177 }
1178 }
drh9ccd8652013-09-13 16:36:46 +00001179 }
1180 break;
drhc11d4f92003-04-06 21:08:24 +00001181
drh9ccd8652013-09-13 16:36:46 +00001182 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001183 int i;
drh2d401ab2008-01-10 23:50:11 +00001184 pParse->nMem = 3;
drh13d70422004-11-13 15:59:14 +00001185 for(i=0; i<db->nDb; i++){
1186 if( db->aDb[i].pBt==0 ) continue;
drh69c33822016-08-18 14:33:11 +00001187 assert( db->aDb[i].zDbSName!=0 );
drh076e85f2015-09-03 13:46:12 +00001188 sqlite3VdbeMultiLoad(v, 1, "iss",
1189 i,
drh69c33822016-08-18 14:33:11 +00001190 db->aDb[i].zDbSName,
drh076e85f2015-09-03 13:46:12 +00001191 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh2d401ab2008-01-10 23:50:11 +00001192 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001193 }
drh9ccd8652013-09-13 16:36:46 +00001194 }
1195 break;
danielk197748af65a2005-02-09 03:20:37 +00001196
drh9ccd8652013-09-13 16:36:46 +00001197 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001198 int i = 0;
1199 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001200 pParse->nMem = 2;
danielk197748af65a2005-02-09 03:20:37 +00001201 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1202 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001203 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
drh2d401ab2008-01-10 23:50:11 +00001204 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001205 }
drh9ccd8652013-09-13 16:36:46 +00001206 }
1207 break;
drh13d70422004-11-13 15:59:14 +00001208#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1209
drhb7f91642004-10-31 02:22:47 +00001210#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001211 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001212 FKey *pFK;
1213 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001214 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001215 if( pTab ){
drh78100cc2003-08-23 22:40:53 +00001216 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001217 if( pFK ){
1218 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001219 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001220 sqlite3CodeVerifySchema(pParse, iDb);
danielk1977742f9472004-06-16 12:02:43 +00001221 while(pFK){
1222 int j;
1223 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001224 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1225 i,
1226 j,
1227 pFK->zTo,
1228 pTab->aCol[pFK->aCol[j].iFrom].zName,
1229 pFK->aCol[j].zCol,
1230 actionName(pFK->aAction[1]), /* ON UPDATE */
1231 actionName(pFK->aAction[0]), /* ON DELETE */
1232 "NONE");
danielk197750af3e12008-10-10 17:47:21 +00001233 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001234 }
1235 ++i;
1236 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001237 }
drh78100cc2003-08-23 22:40:53 +00001238 }
1239 }
drh9ccd8652013-09-13 16:36:46 +00001240 }
1241 break;
drhb7f91642004-10-31 02:22:47 +00001242#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001243
drh6c5b9152012-12-17 16:46:37 +00001244#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001245#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001246 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001247 FKey *pFK; /* A foreign key constraint */
1248 Table *pTab; /* Child table contain "REFERENCES" keyword */
1249 Table *pParent; /* Parent table that child points to */
1250 Index *pIdx; /* Index in the parent table */
1251 int i; /* Loop counter: Foreign key number for pTab */
1252 int j; /* Loop counter: Field of the foreign key */
1253 HashElem *k; /* Loop counter: Next table in schema */
1254 int x; /* result variable */
1255 int regResult; /* 3 registers to hold a result row */
1256 int regKey; /* Register to hold key for checking the FK */
1257 int regRow; /* Registers to hold a row from pTab */
1258 int addrTop; /* Top of a loop checking foreign keys */
1259 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001260 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001261
drh613028b2012-12-17 18:43:02 +00001262 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001263 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001264 regKey = ++pParse->nMem;
1265 regRow = ++pParse->nMem;
drh613028b2012-12-17 18:43:02 +00001266 sqlite3CodeVerifySchema(pParse, iDb);
1267 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1268 while( k ){
1269 if( zRight ){
1270 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1271 k = 0;
1272 }else{
1273 pTab = (Table*)sqliteHashData(k);
1274 k = sqliteHashNext(k);
1275 }
drh9148def2012-12-17 20:40:39 +00001276 if( pTab==0 || pTab->pFKey==0 ) continue;
1277 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001278 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001279 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001280 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh6c5b9152012-12-17 16:46:37 +00001281 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001282 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1283 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001284 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001285 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001286 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001287 if( x==0 ){
1288 if( pIdx==0 ){
1289 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1290 }else{
drh6c5b9152012-12-17 16:46:37 +00001291 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001292 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001293 }
1294 }else{
drh613028b2012-12-17 18:43:02 +00001295 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001296 break;
1297 }
drh6c5b9152012-12-17 16:46:37 +00001298 }
dan5e878302013-10-12 19:06:48 +00001299 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001300 if( pFK ) break;
1301 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001302 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001303 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001304 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001305 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001306 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001307 if( pParent ){
1308 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1309 assert( x==0 );
1310 }
drh613028b2012-12-17 18:43:02 +00001311 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001312 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001313 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001314 assert( iKey>=0 && iKey<pTab->nCol );
1315 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001316 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1317 sqlite3ColumnDefault(v, pTab, iKey, regRow);
drh688852a2014-02-17 22:40:43 +00001318 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001319 }else{
drh613028b2012-12-17 18:43:02 +00001320 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001321 }
drheeb95652016-05-26 20:56:38 +00001322 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, 0, regRow); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001323 sqlite3VdbeGoto(v, addrOk);
drh613028b2012-12-17 18:43:02 +00001324 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1325 }else{
1326 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001327 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001328 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh688852a2014-02-17 22:40:43 +00001329 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001330 }
dan5e878302013-10-12 19:06:48 +00001331 if( pParent ){
drh57bf4a82014-02-17 14:59:22 +00001332 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
drhe9107692015-08-25 19:20:04 +00001333 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
dan5e878302013-10-12 19:06:48 +00001334 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh688852a2014-02-17 22:40:43 +00001335 VdbeCoverage(v);
dan5e878302013-10-12 19:06:48 +00001336 }
drh6c5b9152012-12-17 16:46:37 +00001337 }
drh613028b2012-12-17 18:43:02 +00001338 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh076e85f2015-09-03 13:46:12 +00001339 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001340 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001341 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001342 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001343 }
drh688852a2014-02-17 22:40:43 +00001344 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001345 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001346 }
drh9ccd8652013-09-13 16:36:46 +00001347 }
1348 break;
dan09ff9e12013-03-11 11:49:03 +00001349#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001350#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1351
drhc11d4f92003-04-06 21:08:24 +00001352#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001353 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001354 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001355 if( sqlite3GetBoolean(zRight, 0) ){
drh97e58a22015-11-10 14:27:17 +00001356 sqlite3ParserTrace(stdout, "parser: ");
drh55ef4d92005-08-14 01:20:37 +00001357 }else{
1358 sqlite3ParserTrace(0, 0);
1359 }
drhc11d4f92003-04-06 21:08:24 +00001360 }
drh9ccd8652013-09-13 16:36:46 +00001361 }
1362 break;
drhc11d4f92003-04-06 21:08:24 +00001363#endif
1364
drh55ef4d92005-08-14 01:20:37 +00001365 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1366 ** used will be case sensitive or not depending on the RHS.
1367 */
drh9ccd8652013-09-13 16:36:46 +00001368 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001369 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001370 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001371 }
drh9ccd8652013-09-13 16:36:46 +00001372 }
1373 break;
drh55ef4d92005-08-14 01:20:37 +00001374
drh1dcdbc02007-01-27 02:24:54 +00001375#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1376# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1377#endif
1378
drhb7f91642004-10-31 02:22:47 +00001379#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001380 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001381 ** integrity_check designed to detect most database corruption
1382 ** without most of the overhead of a full integrity-check.
1383 */
drh9ccd8652013-09-13 16:36:46 +00001384 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001385 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001386
drhc5227312011-10-13 17:09:01 +00001387 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001388
dan5885e762012-07-16 10:06:12 +00001389 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1390 ** then iDb is set to the index of the database identified by <db>.
1391 ** In this case, the integrity of database iDb only is verified by
1392 ** the VDBE created below.
1393 **
1394 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1395 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1396 ** to -1 here, to indicate that the VDBE should verify the integrity
1397 ** of all attached databases. */
1398 assert( iDb>=0 );
1399 assert( iDb==0 || pId2->z );
1400 if( pId2->z==0 ) iDb = -1;
1401
drhed717fe2003-06-15 23:42:24 +00001402 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001403 pParse->nMem = 6;
drh1dcdbc02007-01-27 02:24:54 +00001404
1405 /* Set the maximum error count */
1406 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1407 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001408 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001409 if( mxErr<=0 ){
1410 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1411 }
1412 }
drh2d401ab2008-01-10 23:50:11 +00001413 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001414
1415 /* Do an integrity check on each database file */
1416 for(i=0; i<db->nDb; i++){
1417 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001418 Hash *pTbls;
drh98968b22016-03-15 22:00:39 +00001419 int *aRoot;
drh79069752004-05-22 21:30:40 +00001420 int cnt = 0;
drhbb9b5f22016-03-19 00:35:02 +00001421 int mxIdx = 0;
1422 int nIdx;
drhed717fe2003-06-15 23:42:24 +00001423
danielk197753c0f742005-03-29 03:10:59 +00001424 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001425 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001426
drh80242052004-06-09 00:48:12 +00001427 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001428 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh688852a2014-02-17 22:40:43 +00001429 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001430 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001431 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001432
drhed717fe2003-06-15 23:42:24 +00001433 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001434 **
drh98968b22016-03-15 22:00:39 +00001435 ** Begin by finding the root pages numbers
drh2d401ab2008-01-10 23:50:11 +00001436 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001437 */
dan5885e762012-07-16 10:06:12 +00001438 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001439 pTbls = &db->aDb[i].pSchema->tblHash;
drh98968b22016-03-15 22:00:39 +00001440 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001441 Table *pTab = sqliteHashData(x);
1442 Index *pIdx;
drh98968b22016-03-15 22:00:39 +00001443 if( HasRowid(pTab) ) cnt++;
drhbb9b5f22016-03-19 00:35:02 +00001444 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
1445 if( nIdx>mxIdx ) mxIdx = nIdx;
drh98968b22016-03-15 22:00:39 +00001446 }
1447 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
1448 if( aRoot==0 ) break;
1449 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1450 Table *pTab = sqliteHashData(x);
1451 Index *pIdx;
1452 if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum;
drh79069752004-05-22 21:30:40 +00001453 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98968b22016-03-15 22:00:39 +00001454 aRoot[cnt++] = pIdx->tnum;
drh79069752004-05-22 21:30:40 +00001455 }
1456 }
drh98968b22016-03-15 22:00:39 +00001457 aRoot[cnt] = 0;
drh2d401ab2008-01-10 23:50:11 +00001458
1459 /* Make sure sufficient number of registers have been allocated */
drhbb9b5f22016-03-19 00:35:02 +00001460 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
drh2d401ab2008-01-10 23:50:11 +00001461
1462 /* Do the b-tree integrity checks */
drh98968b22016-03-15 22:00:39 +00001463 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
drh4f21c4a2008-12-10 22:15:00 +00001464 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001465 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001466 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh69c33822016-08-18 14:33:11 +00001467 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
drh66a51672008-01-03 00:01:23 +00001468 P4_DYNAMIC);
drh079a3072014-03-19 14:10:55 +00001469 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001470 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001471 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001472 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001473
1474 /* Make sure all the indices are constructed correctly.
1475 */
danielk197741c58b72007-12-29 13:39:19 +00001476 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001477 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001478 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001479 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001480 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001481 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001482 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001483
1484 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001485 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001486 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh688852a2014-02-17 22:40:43 +00001487 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001488 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001489 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001490 sqlite3ExprCacheClear(pParse);
danfd261ec2015-10-22 20:54:33 +00001491 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001492 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001493 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001494 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001495 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001496 }
drhbb9b5f22016-03-19 00:35:02 +00001497 assert( pParse->nMem>=8+j );
1498 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
drh688852a2014-02-17 22:40:43 +00001499 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001500 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhcefc87f2014-08-01 01:40:33 +00001501 /* Verify that all NOT NULL columns really are NOT NULL */
1502 for(j=0; j<pTab->nCol; j++){
1503 char *zErr;
1504 int jmp2, jmp3;
1505 if( j==pTab->iPKey ) continue;
1506 if( pTab->aCol[j].notNull==0 ) continue;
1507 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1508 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1509 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1510 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1511 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1512 pTab->aCol[j].zName);
1513 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1514 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1515 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1516 sqlite3VdbeAddOp0(v, OP_Halt);
1517 sqlite3VdbeJumpHere(v, jmp2);
1518 sqlite3VdbeJumpHere(v, jmp3);
1519 }
1520 /* Validate index entries for the current row */
drhed717fe2003-06-15 23:42:24 +00001521 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhcefc87f2014-08-01 01:40:33 +00001522 int jmp2, jmp3, jmp4, jmp5;
1523 int ckUniq = sqlite3VdbeMakeLabel(v);
drh6fbe41a2013-10-30 20:22:55 +00001524 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001525 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1526 pPrior, r1);
1527 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001528 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drhcefc87f2014-08-01 01:40:33 +00001529 /* Verify that an index entry exists for the current table row */
1530 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
drh688852a2014-02-17 22:40:43 +00001531 pIdx->nColumn); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001532 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001533 sqlite3VdbeLoadString(v, 3, "row ");
drh6fbe41a2013-10-30 20:22:55 +00001534 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001535 sqlite3VdbeLoadString(v, 4, " missing from index ");
drh6fbe41a2013-10-30 20:22:55 +00001536 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001537 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
drh6fbe41a2013-10-30 20:22:55 +00001538 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1539 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh688852a2014-02-17 22:40:43 +00001540 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001541 sqlite3VdbeAddOp0(v, OP_Halt);
drhd654be82005-09-20 17:42:23 +00001542 sqlite3VdbeJumpHere(v, jmp2);
drhcefc87f2014-08-01 01:40:33 +00001543 /* For UNIQUE indexes, verify that only one entry exists with the
1544 ** current key. The entry is unique if (1) any column is NULL
1545 ** or (2) the next entry has a different key */
1546 if( IsUniqueIndex(pIdx) ){
1547 int uniqOk = sqlite3VdbeMakeLabel(v);
1548 int jmp6;
1549 int kk;
1550 for(kk=0; kk<pIdx->nKeyCol; kk++){
1551 int iCol = pIdx->aiColumn[kk];
drh4b92f982015-09-29 17:20:14 +00001552 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
drh68391ac2015-09-25 20:49:16 +00001553 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
drhcefc87f2014-08-01 01:40:33 +00001554 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1555 VdbeCoverage(v);
1556 }
1557 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001558 sqlite3VdbeGoto(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00001559 sqlite3VdbeJumpHere(v, jmp6);
1560 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1561 pIdx->nKeyCol); VdbeCoverage(v);
1562 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001563 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1564 sqlite3VdbeGoto(v, jmp5);
drhcefc87f2014-08-01 01:40:33 +00001565 sqlite3VdbeResolveLabel(v, uniqOk);
1566 }
1567 sqlite3VdbeJumpHere(v, jmp4);
drh87744512014-04-13 19:15:49 +00001568 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhed717fe2003-06-15 23:42:24 +00001569 }
drh688852a2014-02-17 22:40:43 +00001570 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001571 sqlite3VdbeJumpHere(v, loopTop-1);
1572#ifndef SQLITE_OMIT_BTREECOUNT
drh076e85f2015-09-03 13:46:12 +00001573 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
drh8a9789b2013-08-01 03:36:59 +00001574 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001575 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001576 addr = sqlite3VdbeCurrentAddr(v);
drh688852a2014-02-17 22:40:43 +00001577 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001578 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001579 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh688852a2014-02-17 22:40:43 +00001580 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
drh5655c542014-02-19 19:14:34 +00001581 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh8a9789b2013-08-01 03:36:59 +00001582 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh076e85f2015-09-03 13:46:12 +00001583 sqlite3VdbeLoadString(v, 3, pIdx->zName);
drh8a9789b2013-08-01 03:36:59 +00001584 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1585 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001586 }
drh8a9789b2013-08-01 03:36:59 +00001587#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001588 }
1589 }
drh2ce18652016-01-16 20:50:21 +00001590 {
1591 static const int iLn = VDBE_OFFSET_LINENO(2);
1592 static const VdbeOpList endCode[] = {
1593 { OP_AddImm, 1, 0, 0}, /* 0 */
drh1b325542016-02-03 01:55:44 +00001594 { OP_If, 1, 4, 0}, /* 1 */
drh2ce18652016-01-16 20:50:21 +00001595 { OP_String8, 0, 3, 0}, /* 2 */
drh1b325542016-02-03 01:55:44 +00001596 { OP_ResultRow, 3, 1, 0}, /* 3 */
drh2ce18652016-01-16 20:50:21 +00001597 };
1598 VdbeOp *aOp;
1599
1600 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
1601 if( aOp ){
1602 aOp[0].p2 = -mxErr;
drh2ce18652016-01-16 20:50:21 +00001603 aOp[2].p4type = P4_STATIC;
1604 aOp[2].p4.z = "ok";
1605 }
1606 }
drh9ccd8652013-09-13 16:36:46 +00001607 }
1608 break;
drhb7f91642004-10-31 02:22:47 +00001609#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1610
drh13d70422004-11-13 15:59:14 +00001611#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001612 /*
1613 ** PRAGMA encoding
1614 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1615 **
drh85b623f2007-12-13 21:54:09 +00001616 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001617 ** database. If the database is not initialized, it is initialized now.
1618 **
1619 ** The second form of this pragma is a no-op if the main database file
1620 ** has not already been initialized. In this case it sets the default
1621 ** encoding that will be used for the main database file if a new file
1622 ** is created. If an existing main database file is opened, then the
1623 ** default text encoding for the existing database is used.
1624 **
1625 ** In all cases new databases created using the ATTACH command are
1626 ** created to use the same default text encoding as the main database. If
1627 ** the main database has not been initialized and/or created when ATTACH
1628 ** is executed, this is done before the ATTACH operation.
1629 **
1630 ** In the second form this pragma sets the text encoding to be used in
1631 ** new database files created using this database handle. It is only
1632 ** useful if invoked immediately after the main database i
1633 */
drh9ccd8652013-09-13 16:36:46 +00001634 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001635 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001636 char *zName;
1637 u8 enc;
1638 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001639 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001640 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1641 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1642 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001643 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001644 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001645 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1646 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001647 { 0, 0 }
1648 };
drh0f7eb612006-08-08 13:51:43 +00001649 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001650 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001651 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00001652 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1653 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1654 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drhc232aca2016-12-15 16:01:17 +00001655 returnSingleText(v, encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00001656 }else{ /* "PRAGMA encoding = XXX" */
1657 /* Only change the value of sqlite.enc if the database handle is not
1658 ** initialized. If the main database exists, the new sqlite.enc value
1659 ** will be overwritten when the schema is next loaded. If it does not
1660 ** already exists, it will be created to use the new encoding value.
1661 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001662 if(
1663 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1664 DbHasProperty(db, 0, DB_Empty)
1665 ){
danielk19778e227872004-06-07 07:52:17 +00001666 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1667 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh9bd3cc42014-12-12 23:17:54 +00001668 SCHEMA_ENC(db) = ENC(db) =
1669 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001670 break;
1671 }
1672 }
1673 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001674 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001675 }
1676 }
1677 }
drh9ccd8652013-09-13 16:36:46 +00001678 }
1679 break;
drh13d70422004-11-13 15:59:14 +00001680#endif /* SQLITE_OMIT_UTF16 */
1681
1682#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001683 /*
drh9b0cf342015-11-12 14:57:19 +00001684 ** PRAGMA [schema.]schema_version
1685 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001686 **
drh9b0cf342015-11-12 14:57:19 +00001687 ** PRAGMA [schema.]user_version
1688 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001689 **
drhe459bd42016-03-16 20:05:57 +00001690 ** PRAGMA [schema.]freelist_count
1691 **
1692 ** PRAGMA [schema.]data_version
drh4ee09b42013-05-01 19:49:27 +00001693 **
drh9b0cf342015-11-12 14:57:19 +00001694 ** PRAGMA [schema.]application_id
1695 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00001696 **
danielk1977b92b70b2004-11-12 16:11:59 +00001697 ** The pragma's schema_version and user_version are used to set or get
1698 ** the value of the schema-version and user-version, respectively. Both
1699 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001700 ** stored in the database header.
1701 **
1702 ** The schema-cookie is usually only manipulated internally by SQLite. It
1703 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001704 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001705 ** SQLite each time a query is executed to ensure that the internal cache
1706 ** of the schema used when compiling the SQL query matches the schema of
1707 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001708 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1709 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001710 ** crashes or database corruption. Use with caution!
1711 **
danielk1977b92b70b2004-11-12 16:11:59 +00001712 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001713 ** applications for any purpose.
1714 */
drh9ccd8652013-09-13 16:36:46 +00001715 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00001716 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00001717 sqlite3VdbeUsesBtree(v, iDb);
drhc232aca2016-12-15 16:01:17 +00001718 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00001719 /* Write the specified cookie value */
1720 static const VdbeOpList setCookie[] = {
1721 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1861afc2016-02-01 21:48:34 +00001722 { OP_SetCookie, 0, 0, 0}, /* 1 */
danielk1977dae24952004-11-11 05:10:43 +00001723 };
drh2ce18652016-01-16 20:50:21 +00001724 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001725 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
drh2ce18652016-01-16 20:50:21 +00001726 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
drhdad300d2016-01-18 00:20:26 +00001727 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001728 aOp[0].p1 = iDb;
drh1861afc2016-02-01 21:48:34 +00001729 aOp[1].p1 = iDb;
1730 aOp[1].p2 = iCookie;
1731 aOp[1].p3 = sqlite3Atoi(zRight);
danielk1977dae24952004-11-11 05:10:43 +00001732 }else{
1733 /* Read the specified cookie value */
1734 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001735 { OP_Transaction, 0, 0, 0}, /* 0 */
1736 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001737 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001738 };
drh2ce18652016-01-16 20:50:21 +00001739 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001740 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
drh2ce18652016-01-16 20:50:21 +00001741 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
drhdad300d2016-01-18 00:20:26 +00001742 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001743 aOp[0].p1 = iDb;
1744 aOp[1].p1 = iDb;
1745 aOp[1].p3 = iCookie;
drhf71a3662016-03-16 20:44:45 +00001746 sqlite3VdbeReusable(v);
danielk1977dae24952004-11-11 05:10:43 +00001747 }
drh9ccd8652013-09-13 16:36:46 +00001748 }
1749 break;
drh13d70422004-11-13 15:59:14 +00001750#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001751
shanehdc97a8c2010-02-23 20:08:35 +00001752#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1753 /*
1754 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001755 **
drh71caabf2010-02-26 15:39:24 +00001756 ** Return the names of all compile-time options used in this build,
1757 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001758 */
drh9ccd8652013-09-13 16:36:46 +00001759 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00001760 int i = 0;
1761 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00001762 pParse->nMem = 1;
shanehdc97a8c2010-02-23 20:08:35 +00001763 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00001764 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00001765 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1766 }
drhf71a3662016-03-16 20:44:45 +00001767 sqlite3VdbeReusable(v);
drh9ccd8652013-09-13 16:36:46 +00001768 }
1769 break;
shanehdc97a8c2010-02-23 20:08:35 +00001770#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1771
dan5cf53532010-05-01 16:40:20 +00001772#ifndef SQLITE_OMIT_WAL
1773 /*
drh9b0cf342015-11-12 14:57:19 +00001774 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00001775 **
1776 ** Checkpoint the database.
1777 */
drh9ccd8652013-09-13 16:36:46 +00001778 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00001779 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001780 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1781 if( zRight ){
1782 if( sqlite3StrICmp(zRight, "full")==0 ){
1783 eMode = SQLITE_CHECKPOINT_FULL;
1784 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1785 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00001786 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1787 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00001788 }
1789 }
dancdc1f042010-11-18 12:11:05 +00001790 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00001791 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001792 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00001793 }
1794 break;
dan5a299f92010-05-03 11:05:08 +00001795
1796 /*
1797 ** PRAGMA wal_autocheckpoint
1798 ** PRAGMA wal_autocheckpoint = N
1799 **
1800 ** Configure a database connection to automatically checkpoint a database
1801 ** after accumulating N frames in the log. Or query for the current value
1802 ** of N.
1803 */
drh9ccd8652013-09-13 16:36:46 +00001804 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00001805 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001806 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001807 }
drhc232aca2016-12-15 16:01:17 +00001808 returnSingleInt(v,
drhb033d8b2010-05-03 13:37:30 +00001809 db->xWalCallback==sqlite3WalDefaultHook ?
1810 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00001811 }
1812 break;
dan5cf53532010-05-01 16:40:20 +00001813#endif
dan7c246102010-04-12 19:00:29 +00001814
drh09419b42011-11-16 19:29:17 +00001815 /*
1816 ** PRAGMA shrink_memory
1817 **
drh51a74d42015-02-28 01:04:27 +00001818 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
1819 ** connection on which it is invoked to free up as much memory as it
1820 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00001821 */
drh9ccd8652013-09-13 16:36:46 +00001822 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00001823 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00001824 break;
1825 }
drh09419b42011-11-16 19:29:17 +00001826
drhf3603962012-09-07 16:46:59 +00001827 /*
1828 ** PRAGMA busy_timeout
1829 ** PRAGMA busy_timeout = N
1830 **
1831 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001832 ** if one is set. If no busy handler or a different busy handler is set
1833 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1834 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001835 */
drhd49c3582013-09-13 19:00:06 +00001836 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00001837 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00001838 if( zRight ){
1839 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1840 }
drhc232aca2016-12-15 16:01:17 +00001841 returnSingleInt(v, db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00001842 break;
1843 }
drhf3603962012-09-07 16:46:59 +00001844
drh55e85ca2013-09-13 21:01:56 +00001845 /*
1846 ** PRAGMA soft_heap_limit
1847 ** PRAGMA soft_heap_limit = N
1848 **
drh51a74d42015-02-28 01:04:27 +00001849 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
1850 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
1851 ** specified and is a non-negative integer.
1852 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
1853 ** returns the same integer that would be returned by the
1854 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00001855 */
1856 case PragTyp_SOFT_HEAP_LIMIT: {
1857 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00001858 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00001859 sqlite3_soft_heap_limit64(N);
1860 }
drhc232aca2016-12-15 16:01:17 +00001861 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00001862 break;
1863 }
1864
drh03459612014-08-25 15:13:22 +00001865 /*
1866 ** PRAGMA threads
1867 ** PRAGMA threads = N
1868 **
1869 ** Configure the maximum number of worker threads. Return the new
1870 ** maximum, which might be less than requested.
1871 */
1872 case PragTyp_THREADS: {
1873 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00001874 if( zRight
drh03459612014-08-25 15:13:22 +00001875 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
1876 && N>=0
1877 ){
drh111544c2014-08-29 16:20:47 +00001878 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00001879 }
drhc232aca2016-12-15 16:01:17 +00001880 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00001881 break;
1882 }
1883
dougcurrie81c95ef2004-06-18 23:21:47 +00001884#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001885 /*
1886 ** Report the current state of file logs for all databases
1887 */
drh9ccd8652013-09-13 16:36:46 +00001888 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00001889 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001890 "unlocked", "shared", "reserved", "pending", "exclusive"
1891 };
1892 int i;
drh2d401ab2008-01-10 23:50:11 +00001893 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00001894 for(i=0; i<db->nDb; i++){
1895 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001896 const char *zState = "unknown";
1897 int j;
drh69c33822016-08-18 14:33:11 +00001898 if( db->aDb[i].zDbSName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00001899 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001900 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001901 zState = "closed";
drh69c33822016-08-18 14:33:11 +00001902 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001903 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1904 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001905 }
drh69c33822016-08-18 14:33:11 +00001906 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
drh2d401ab2008-01-10 23:50:11 +00001907 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001908 }
drh9ccd8652013-09-13 16:36:46 +00001909 break;
1910 }
drh89ac8c12004-06-09 14:17:20 +00001911#endif
1912
shanehad9f9f62010-02-17 17:48:46 +00001913#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00001914 case PragTyp_KEY: {
1915 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1916 break;
1917 }
1918 case PragTyp_REKEY: {
1919 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1920 break;
1921 }
1922 case PragTyp_HEXKEY: {
1923 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00001924 u8 iByte;
1925 int i;
drh9ccd8652013-09-13 16:36:46 +00001926 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00001927 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
1928 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
1929 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00001930 }
1931 if( (zLeft[3] & 0xf)==0xb ){
1932 sqlite3_key_v2(db, zDb, zKey, i/2);
1933 }else{
1934 sqlite3_rekey_v2(db, zDb, zKey, i/2);
1935 }
drhd2cb50b2009-01-09 21:41:17 +00001936 }
drh9ccd8652013-09-13 16:36:46 +00001937 break;
1938 }
drh3c4f2a42005-12-08 18:12:56 +00001939#endif
shanehad9f9f62010-02-17 17:48:46 +00001940#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00001941 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001942#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001943 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001944 sqlite3_activate_see(&zRight[4]);
1945 }
1946#endif
1947#ifdef SQLITE_ENABLE_CEROD
1948 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001949 sqlite3_activate_cerod(&zRight[6]);
1950 }
1951#endif
drh9ccd8652013-09-13 16:36:46 +00001952 }
1953 break;
drh21e2cab2006-09-25 18:01:57 +00001954#endif
drh3c4f2a42005-12-08 18:12:56 +00001955
drh9ccd8652013-09-13 16:36:46 +00001956 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00001957
dan9e1ab1a2017-01-05 19:32:48 +00001958 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
1959 ** purpose is to execute assert() statements to verify that if the
1960 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
1961 ** to the PRAGMA, the implementation has not added any OP_ResultRow
1962 ** instructions to the VM. */
1963 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
1964 sqlite3VdbeVerifyNoResultRow(v);
1965 }
1966
danielk1977e0048402004-06-15 16:51:01 +00001967pragma_out:
drh633e6d52008-07-28 19:34:53 +00001968 sqlite3DbFree(db, zLeft);
1969 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001970}
drh2fcc1592016-12-15 20:59:03 +00001971#ifndef SQLITE_OMIT_VIRTUALTABLE
1972/*****************************************************************************
1973** Implementation of an eponymous virtual table that runs a pragma.
1974**
1975*/
1976typedef struct PragmaVtab PragmaVtab;
1977typedef struct PragmaVtabCursor PragmaVtabCursor;
1978struct PragmaVtab {
1979 sqlite3_vtab base; /* Base class. Must be first */
1980 sqlite3 *db; /* The database connection to which it belongs */
1981 const PragmaName *pName; /* Name of the pragma */
1982 u8 nHidden; /* Number of hidden columns */
1983 u8 iHidden; /* Index of the first hidden column */
1984};
1985struct PragmaVtabCursor {
1986 sqlite3_vtab_cursor base; /* Base class. Must be first */
1987 sqlite3_stmt *pPragma; /* The pragma statement to run */
1988 sqlite_int64 iRowid; /* Current rowid */
1989 char *azArg[2]; /* Value of the argument and schema */
1990};
1991
1992/*
1993** Pragma virtual table module xConnect method.
1994*/
1995static int pragmaVtabConnect(
1996 sqlite3 *db,
1997 void *pAux,
1998 int argc, const char *const*argv,
1999 sqlite3_vtab **ppVtab,
2000 char **pzErr
2001){
2002 const PragmaName *pPragma = (const PragmaName*)pAux;
2003 PragmaVtab *pTab = 0;
2004 int rc;
2005 int i, j;
2006 char cSep = '(';
2007 StrAccum acc;
2008 char zBuf[200];
2009
drh344a1bf2016-12-22 14:53:25 +00002010 UNUSED_PARAMETER(argc);
2011 UNUSED_PARAMETER(argv);
drh2fcc1592016-12-15 20:59:03 +00002012 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
2013 sqlite3StrAccumAppendAll(&acc, "CREATE TABLE x");
2014 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
drhd7175eb2016-12-15 21:11:15 +00002015 sqlite3XPrintf(&acc, "%c\"%s\"", cSep, pragCName[j]);
drh2fcc1592016-12-15 20:59:03 +00002016 cSep = ',';
2017 }
drh9a63f092016-12-16 02:14:15 +00002018 if( i==0 ){
2019 sqlite3XPrintf(&acc, "(\"%s\"", pPragma->zName);
2020 cSep = ',';
2021 i++;
2022 }
drh2fcc1592016-12-15 20:59:03 +00002023 j = 0;
2024 if( pPragma->mPragFlg & PragFlg_Result1 ){
2025 sqlite3StrAccumAppendAll(&acc, ",arg HIDDEN");
2026 j++;
2027 }
2028 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
2029 sqlite3StrAccumAppendAll(&acc, ",schema HIDDEN");
2030 j++;
2031 }
2032 sqlite3StrAccumAppend(&acc, ")", 1);
2033 sqlite3StrAccumFinish(&acc);
2034 assert( strlen(zBuf) < sizeof(zBuf)-1 );
2035 rc = sqlite3_declare_vtab(db, zBuf);
2036 if( rc==SQLITE_OK ){
2037 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
2038 if( pTab==0 ){
2039 rc = SQLITE_NOMEM;
2040 }else{
2041 memset(pTab, 0, sizeof(PragmaVtab));
2042 pTab->pName = pPragma;
2043 pTab->db = db;
2044 pTab->iHidden = i;
2045 pTab->nHidden = j;
2046 }
2047 }else{
2048 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2049 }
2050
2051 *ppVtab = (sqlite3_vtab*)pTab;
2052 return rc;
2053}
2054
2055/*
2056** Pragma virtual table module xDisconnect method.
2057*/
2058static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
2059 PragmaVtab *pTab = (PragmaVtab*)pVtab;
2060 sqlite3_free(pTab);
2061 return SQLITE_OK;
2062}
2063
2064/* Figure out the best index to use to search a pragma virtual table.
2065**
2066** There are not really any index choices. But we want to encourage the
2067** query planner to give == constraints on as many hidden parameters as
2068** possible, and especially on the first hidden parameter. So return a
2069** high cost if hidden parameters are unconstrained.
2070*/
2071static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2072 PragmaVtab *pTab = (PragmaVtab*)tab;
2073 const struct sqlite3_index_constraint *pConstraint;
2074 int i, j;
2075 int seen[2];
2076
drhae7045c2016-12-15 21:33:55 +00002077 pIdxInfo->estimatedCost = (double)1;
drh2fcc1592016-12-15 20:59:03 +00002078 if( pTab->nHidden==0 ){ return SQLITE_OK; }
2079 pConstraint = pIdxInfo->aConstraint;
2080 seen[0] = 0;
2081 seen[1] = 0;
2082 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2083 if( pConstraint->usable==0 ) continue;
2084 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2085 if( pConstraint->iColumn < pTab->iHidden ) continue;
2086 j = pConstraint->iColumn - pTab->iHidden;
2087 assert( j < 2 );
drhd7175eb2016-12-15 21:11:15 +00002088 seen[j] = i+1;
drh2fcc1592016-12-15 20:59:03 +00002089 }
2090 if( seen[0]==0 ){
2091 pIdxInfo->estimatedCost = (double)2147483647;
2092 pIdxInfo->estimatedRows = 2147483647;
2093 return SQLITE_OK;
2094 }
drhd7175eb2016-12-15 21:11:15 +00002095 j = seen[0]-1;
drh2fcc1592016-12-15 20:59:03 +00002096 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
2097 pIdxInfo->aConstraintUsage[j].omit = 1;
2098 if( seen[1]==0 ) return SQLITE_OK;
2099 pIdxInfo->estimatedCost = (double)20;
2100 pIdxInfo->estimatedRows = 20;
drhd7175eb2016-12-15 21:11:15 +00002101 j = seen[1]-1;
drh2fcc1592016-12-15 20:59:03 +00002102 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
2103 pIdxInfo->aConstraintUsage[j].omit = 1;
2104 return SQLITE_OK;
2105}
2106
2107/* Create a new cursor for the pragma virtual table */
2108static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
2109 PragmaVtabCursor *pCsr;
2110 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
2111 if( pCsr==0 ) return SQLITE_NOMEM;
2112 memset(pCsr, 0, sizeof(PragmaVtabCursor));
2113 pCsr->base.pVtab = pVtab;
2114 *ppCursor = &pCsr->base;
2115 return SQLITE_OK;
2116}
2117
2118/* Clear all content from pragma virtual table cursor. */
2119static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
2120 int i;
2121 sqlite3_finalize(pCsr->pPragma);
2122 pCsr->pPragma = 0;
2123 for(i=0; i<ArraySize(pCsr->azArg); i++){
2124 sqlite3_free(pCsr->azArg[i]);
2125 pCsr->azArg[i] = 0;
2126 }
2127}
2128
2129/* Close a pragma virtual table cursor */
2130static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
2131 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
2132 pragmaVtabCursorClear(pCsr);
drh9a63f092016-12-16 02:14:15 +00002133 sqlite3_free(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002134 return SQLITE_OK;
2135}
2136
2137/* Advance the pragma virtual table cursor to the next row */
2138static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
2139 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2140 int rc = SQLITE_OK;
2141
2142 /* Increment the xRowid value */
2143 pCsr->iRowid++;
drh9a63f092016-12-16 02:14:15 +00002144 assert( pCsr->pPragma );
2145 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
2146 rc = sqlite3_finalize(pCsr->pPragma);
2147 pCsr->pPragma = 0;
2148 pragmaVtabCursorClear(pCsr);
drh2fcc1592016-12-15 20:59:03 +00002149 }
2150 return rc;
2151}
2152
2153/*
2154** Pragma virtual table module xFilter method.
2155*/
2156static int pragmaVtabFilter(
2157 sqlite3_vtab_cursor *pVtabCursor,
2158 int idxNum, const char *idxStr,
2159 int argc, sqlite3_value **argv
2160){
2161 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2162 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2163 int rc;
drhd8b72002016-12-16 04:20:27 +00002164 int i, j;
drh2fcc1592016-12-15 20:59:03 +00002165 StrAccum acc;
2166 char *zSql;
2167
drh344a1bf2016-12-22 14:53:25 +00002168 UNUSED_PARAMETER(idxNum);
2169 UNUSED_PARAMETER(idxStr);
drh2fcc1592016-12-15 20:59:03 +00002170 pragmaVtabCursorClear(pCsr);
drhd8b72002016-12-16 04:20:27 +00002171 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
2172 for(i=0; i<argc; i++, j++){
2173 assert( j<ArraySize(pCsr->azArg) );
2174 pCsr->azArg[j] = sqlite3_mprintf("%s", sqlite3_value_text(argv[i]));
2175 if( pCsr->azArg[j]==0 ){
drh2fcc1592016-12-15 20:59:03 +00002176 return SQLITE_NOMEM;
2177 }
2178 }
drhd7175eb2016-12-15 21:11:15 +00002179 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
drh2fcc1592016-12-15 20:59:03 +00002180 sqlite3StrAccumAppendAll(&acc, "PRAGMA ");
2181 if( pCsr->azArg[1] ){
2182 sqlite3XPrintf(&acc, "%Q.", pCsr->azArg[1]);
2183 }
2184 sqlite3StrAccumAppendAll(&acc, pTab->pName->zName);
2185 if( pCsr->azArg[0] ){
2186 sqlite3XPrintf(&acc, "=%Q", pCsr->azArg[0]);
2187 }
2188 zSql = sqlite3StrAccumFinish(&acc);
2189 if( zSql==0 ) return SQLITE_NOMEM;
2190 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
2191 sqlite3_free(zSql);
2192 if( rc!=SQLITE_OK ){
2193 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
2194 return rc;
2195 }
2196 return pragmaVtabNext(pVtabCursor);
2197}
2198
2199/*
2200** Pragma virtual table module xEof method.
2201*/
2202static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
2203 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2204 return (pCsr->pPragma==0);
2205}
2206
2207/* The xColumn method simply returns the corresponding column from
2208** the PRAGMA.
2209*/
2210static int pragmaVtabColumn(
2211 sqlite3_vtab_cursor *pVtabCursor,
2212 sqlite3_context *ctx,
2213 int i
2214){
2215 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2216 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
2217 if( i<pTab->iHidden ){
2218 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
2219 }else{
2220 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
2221 }
2222 return SQLITE_OK;
2223}
2224
2225/*
2226** Pragma virtual table module xRowid method.
2227*/
2228static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
2229 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
2230 *p = pCsr->iRowid;
2231 return SQLITE_OK;
2232}
2233
2234/* The pragma virtual table object */
2235static const sqlite3_module pragmaVtabModule = {
2236 0, /* iVersion */
2237 0, /* xCreate - create a table */
2238 pragmaVtabConnect, /* xConnect - connect to an existing table */
2239 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
2240 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
2241 0, /* xDestroy - Drop a table */
2242 pragmaVtabOpen, /* xOpen - open a cursor */
2243 pragmaVtabClose, /* xClose - close a cursor */
2244 pragmaVtabFilter, /* xFilter - configure scan constraints */
2245 pragmaVtabNext, /* xNext - advance a cursor */
2246 pragmaVtabEof, /* xEof */
2247 pragmaVtabColumn, /* xColumn - read data */
2248 pragmaVtabRowid, /* xRowid - read data */
2249 0, /* xUpdate - write data */
2250 0, /* xBegin - begin transaction */
2251 0, /* xSync - sync transaction */
2252 0, /* xCommit - commit transaction */
2253 0, /* xRollback - rollback transaction */
2254 0, /* xFindFunction - function overloading */
2255 0, /* xRename - rename the table */
2256 0, /* xSavepoint */
2257 0, /* xRelease */
2258 0 /* xRollbackTo */
2259};
2260
2261/*
2262** Check to see if zTabName is really the name of a pragma. If it is,
2263** then register an eponymous virtual table for that pragma and return
2264** a pointer to the Module object for the new virtual table.
2265*/
2266Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
2267 const PragmaName *pName;
2268 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
2269 pName = pragmaLocate(zName+7);
2270 if( pName==0 ) return 0;
2271 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
2272 assert( sqlite3HashFind(&db->aModule, zName)==0 );
drhd7175eb2016-12-15 21:11:15 +00002273 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
drh2fcc1592016-12-15 20:59:03 +00002274}
2275
2276#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh13d70422004-11-13 15:59:14 +00002277
drh8bfdf722009-06-19 14:06:03 +00002278#endif /* SQLITE_OMIT_PRAGMA */