blob: c34d5421c2ef62f93118803f1263096660cae772 [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/*
drhb460e522015-09-03 03:29:51 +0000166** Set the names of the first N columns to the values in azCol[]
167*/
168static void setAllColumnNames(
169 Vdbe *v, /* The query under construction */
170 int N, /* Number of columns */
171 const char **azCol /* Names of columns */
172){
173 int i;
174 sqlite3VdbeSetNumCols(v, N);
175 for(i=0; i<N; i++){
176 sqlite3VdbeSetColName(v, i, COLNAME_NAME, azCol[i], SQLITE_STATIC);
177 }
178}
179static void setOneColumnName(Vdbe *v, const char *z){
180 setAllColumnNames(v, 1, &z);
181}
182
183/*
drh90f5ecb2004-07-22 01:19:35 +0000184** Generate code to return a single integer value.
185*/
drh7cc023c2015-09-03 04:28:25 +0000186static void returnSingleInt(Vdbe *v, const char *zLabel, i64 value){
187 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
drhb460e522015-09-03 03:29:51 +0000188 setOneColumnName(v, zLabel);
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 */
197 const char *zLabel, /* Name of the result column */
198 const char *zValue /* Value to be returned */
199){
200 if( zValue ){
drh076e85f2015-09-03 13:46:12 +0000201 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
drh7cc023c2015-09-03 04:28:25 +0000202 setOneColumnName(v, zLabel);
203 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
204 }
drh90f5ecb2004-07-22 01:19:35 +0000205}
206
drhd3605a42013-08-17 15:42:29 +0000207
208/*
209** Set the safety_level and pager flags for pager iDb. Or if iDb<0
210** set these values for all pagers.
211*/
212#ifndef SQLITE_OMIT_PAGER_PRAGMAS
213static void setAllPagerFlags(sqlite3 *db){
214 if( db->autoCommit ){
215 Db *pDb = db->aDb;
216 int n = db->nDb;
217 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
218 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
219 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
220 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
221 == PAGER_FLAGS_MASK );
222 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
223 while( (n--) > 0 ){
224 if( pDb->pBt ){
225 sqlite3BtreeSetPagerFlags(pDb->pBt,
226 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
227 }
228 pDb++;
229 }
230 }
231}
drhfeb56e02013-08-23 17:33:46 +0000232#else
233# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29 +0000234#endif
235
236
drhd2cb50b2009-01-09 21:41:17 +0000237/*
238** Return a human-readable name for a constraint resolution action.
239*/
danba9108b2009-09-22 07:13:42 +0000240#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000241static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000242 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000243 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000244 case OE_SetNull: zName = "SET NULL"; break;
245 case OE_SetDflt: zName = "SET DEFAULT"; break;
246 case OE_Cascade: zName = "CASCADE"; break;
247 case OE_Restrict: zName = "RESTRICT"; break;
248 default: zName = "NO ACTION";
249 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000250 }
drhd2cb50b2009-01-09 21:41:17 +0000251 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000252}
danba9108b2009-09-22 07:13:42 +0000253#endif
danielk197750af3e12008-10-10 17:47:21 +0000254
dane04dc882010-04-20 18:53:15 +0000255
256/*
257** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
258** defined in pager.h. This function returns the associated lowercase
259** journal-mode name.
260*/
261const char *sqlite3JournalModename(int eMode){
262 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000263 "delete", "persist", "off", "truncate", "memory"
264#ifndef SQLITE_OMIT_WAL
265 , "wal"
266#endif
dane04dc882010-04-20 18:53:15 +0000267 };
268 assert( PAGER_JOURNALMODE_DELETE==0 );
269 assert( PAGER_JOURNALMODE_PERSIST==1 );
270 assert( PAGER_JOURNALMODE_OFF==2 );
271 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
272 assert( PAGER_JOURNALMODE_MEMORY==4 );
273 assert( PAGER_JOURNALMODE_WAL==5 );
274 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
275
276 if( eMode==ArraySize(azModeName) ) return 0;
277 return azModeName[eMode];
278}
279
drh87223182004-02-21 14:00:29 +0000280/*
drhc11d4f92003-04-06 21:08:24 +0000281** Process a pragma statement.
282**
283** Pragmas are of this form:
284**
drh9b0cf342015-11-12 14:57:19 +0000285** PRAGMA [schema.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000286**
287** The identifier might also be a string. The value is a string, and
288** identifier, or a number. If minusFlag is true, then the value is
289** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000290**
291** If the left side is "database.id" then pId1 is the database name
292** and pId2 is the id. If the left side is just "id" then pId1 is the
293** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000294*/
danielk197791cf71b2004-06-26 06:37:06 +0000295void sqlite3Pragma(
296 Parse *pParse,
drh9b0cf342015-11-12 14:57:19 +0000297 Token *pId1, /* First part of [schema.]id field */
298 Token *pId2, /* Second part of [schema.]id field, or NULL */
danielk197791cf71b2004-06-26 06:37:06 +0000299 Token *pValue, /* Token for <value>, or NULL */
300 int minusFlag /* True if a '-' sign preceded <value> */
301){
302 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
303 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
304 const char *zDb = 0; /* The database name */
305 Token *pId; /* Pointer to <id> token */
drh3fa97302012-02-22 16:58:36 +0000306 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000307 int iDb; /* Database index for <database> */
mistachkin1a51ce72015-01-12 18:38:02 +0000308 int lwr, upr, mid = 0; /* Binary search bounds */
drh06fd5d62012-02-22 14:45:19 +0000309 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
310 sqlite3 *db = pParse->db; /* The database connection */
311 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000312 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drhc228be52015-01-31 02:00:01 +0000313 const struct sPragmaNames *pPragma;
drh06fd5d62012-02-22 14:45:19 +0000314
drhc11d4f92003-04-06 21:08:24 +0000315 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000316 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000317 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000318
drh9b0cf342015-11-12 14:57:19 +0000319 /* Interpret the [schema.] part of the pragma statement. iDb is the
danielk197791cf71b2004-06-26 06:37:06 +0000320 ** index of the database this pragma is being applied to in db.aDb[]. */
321 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
322 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000323 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000324
danielk1977ddfb2f02006-02-17 12:25:14 +0000325 /* If the temp database has been explicitly named as part of the
326 ** pragma, make sure it is open.
327 */
328 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
329 return;
330 }
331
drh17435752007-08-16 04:30:38 +0000332 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000333 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000334 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000335 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000336 }else{
drh17435752007-08-16 04:30:38 +0000337 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000338 }
danielk197791cf71b2004-06-26 06:37:06 +0000339
drhd2cb50b2009-01-09 21:41:17 +0000340 assert( pId2 );
341 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000342 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000343 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000344 }
drh06fd5d62012-02-22 14:45:19 +0000345
346 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
347 ** connection. If it returns SQLITE_OK, then assume that the VFS
348 ** handled the pragma and generate a no-op prepared statement.
drh8dd7a6a2015-03-06 04:37:26 +0000349 **
350 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
351 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
352 ** object corresponding to the database file to which the pragma
353 ** statement refers.
354 **
355 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
356 ** file control is an array of pointers to strings (char**) in which the
357 ** second element of the array is the name of the pragma and the third
358 ** element is the argument to the pragma or NULL if the pragma has no
359 ** argument.
drh06fd5d62012-02-22 14:45:19 +0000360 */
drh3fa97302012-02-22 16:58:36 +0000361 aFcntl[0] = 0;
362 aFcntl[1] = zLeft;
363 aFcntl[2] = zRight;
364 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000365 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000366 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
367 if( rc==SQLITE_OK ){
drh7cc023c2015-09-03 04:28:25 +0000368 returnSingleText(v, "result", aFcntl[0]);
369 sqlite3_free(aFcntl[0]);
drh9ccd8652013-09-13 16:36:46 +0000370 goto pragma_out;
371 }
372 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000373 if( aFcntl[0] ){
374 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
375 sqlite3_free(aFcntl[0]);
376 }
377 pParse->nErr++;
378 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000379 goto pragma_out;
380 }
381
382 /* Locate the pragma in the lookup table */
383 lwr = 0;
384 upr = ArraySize(aPragmaNames)-1;
385 while( lwr<=upr ){
386 mid = (lwr+upr)/2;
387 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
388 if( rc==0 ) break;
389 if( rc<0 ){
390 upr = mid - 1;
391 }else{
392 lwr = mid + 1;
393 }
394 }
395 if( lwr>upr ) goto pragma_out;
drhc228be52015-01-31 02:00:01 +0000396 pPragma = &aPragmaNames[mid];
drh9ccd8652013-09-13 16:36:46 +0000397
drhf63936e2013-10-03 14:08:07 +0000398 /* Make sure the database schema is loaded if the pragma requires that */
drhc228be52015-01-31 02:00:01 +0000399 if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){
drhf63936e2013-10-03 14:08:07 +0000400 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
401 }
402
drh9ccd8652013-09-13 16:36:46 +0000403 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01 +0000404 switch( pPragma->ePragTyp ){
drh9ccd8652013-09-13 16:36:46 +0000405
drhe73c9142011-11-09 16:12:24 +0000406#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000407 /*
drh9b0cf342015-11-12 14:57:19 +0000408 ** PRAGMA [schema.]default_cache_size
409 ** PRAGMA [schema.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000410 **
411 ** The first form reports the current persistent setting for the
412 ** page cache size. The value returned is the maximum number of
413 ** pages in the page cache. The second form sets both the current
414 ** page cache size value and the persistent page cache size value
415 ** stored in the database file.
416 **
drh93791ea2010-04-26 17:36:35 +0000417 ** Older versions of SQLite would set the default cache size to a
418 ** negative number to indicate synchronous=OFF. These days, synchronous
419 ** is always on by default regardless of the sign of the default cache
420 ** size. But continue to take the absolute value of the default cache
421 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000422 */
drh9ccd8652013-09-13 16:36:46 +0000423 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09 +0000424 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000425 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000426 { OP_Transaction, 0, 0, 0}, /* 0 */
427 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000428 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000429 { OP_Integer, 0, 2, 0},
430 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000431 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000432 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000433 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000434 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000435 };
drh2ce18652016-01-16 20:50:21 +0000436 VdbeOp *aOp;
drhfb982642007-08-30 01:19:59 +0000437 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000438 if( !zRight ){
drhb460e522015-09-03 03:29:51 +0000439 setOneColumnName(v, "cache_size");
drh3c84ddf2008-01-09 02:15:38 +0000440 pParse->nMem += 2;
drhdad300d2016-01-18 00:20:26 +0000441 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
drh2ce18652016-01-16 20:50:21 +0000442 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
drhdad300d2016-01-18 00:20:26 +0000443 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000444 aOp[0].p1 = iDb;
445 aOp[1].p1 = iDb;
446 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
drhc11d4f92003-04-06 21:08:24 +0000447 }else{
drhd50ffc42011-03-08 02:38:28 +0000448 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000449 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh1861afc2016-02-01 21:48:34 +0000450 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
drh21206082011-04-04 18:22:02 +0000451 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000452 pDb->pSchema->cache_size = size;
453 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000454 }
drh9ccd8652013-09-13 16:36:46 +0000455 break;
456 }
drhe73c9142011-11-09 16:12:24 +0000457#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000458
drhe73c9142011-11-09 16:12:24 +0000459#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000460 /*
drh9b0cf342015-11-12 14:57:19 +0000461 ** PRAGMA [schema.]page_size
462 ** PRAGMA [schema.]page_size=N
drh90f5ecb2004-07-22 01:19:35 +0000463 **
464 ** The first form reports the current setting for the
465 ** database page size in bytes. The second form sets the
466 ** database page size value. The value can only be set if
467 ** the database has not yet been created.
468 */
drh9ccd8652013-09-13 16:36:46 +0000469 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000470 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000471 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000472 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000473 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh7cc023c2015-09-03 04:28:25 +0000474 returnSingleInt(v, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000475 }else{
danielk1977992772c2007-08-30 10:07:38 +0000476 /* Malloc may fail when setting the page-size, as there is an internal
477 ** buffer that the pager module resizes using sqlite3_realloc().
478 */
drh60ac3f42010-11-23 18:59:27 +0000479 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000480 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
drh4a642b62016-02-05 01:55:27 +0000481 sqlite3OomFault(db);
danielk1977992772c2007-08-30 10:07:38 +0000482 }
drh90f5ecb2004-07-22 01:19:35 +0000483 }
drh9ccd8652013-09-13 16:36:46 +0000484 break;
485 }
danielk197741483462007-03-24 16:45:04 +0000486
487 /*
drh9b0cf342015-11-12 14:57:19 +0000488 ** PRAGMA [schema.]secure_delete
489 ** PRAGMA [schema.]secure_delete=ON/OFF
drh5b47efa2010-02-12 18:18:39 +0000490 **
491 ** The first form reports the current setting for the
492 ** secure_delete flag. The second form changes the secure_delete
493 ** flag setting and reports thenew value.
494 */
drh9ccd8652013-09-13 16:36:46 +0000495 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000496 Btree *pBt = pDb->pBt;
497 int b = -1;
498 assert( pBt!=0 );
499 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000500 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000501 }
drhaf034ed2010-02-12 19:46:26 +0000502 if( pId2->n==0 && b>=0 ){
503 int ii;
504 for(ii=0; ii<db->nDb; ii++){
505 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
506 }
507 }
drh5b47efa2010-02-12 18:18:39 +0000508 b = sqlite3BtreeSecureDelete(pBt, b);
drh7cc023c2015-09-03 04:28:25 +0000509 returnSingleInt(v, "secure_delete", b);
drh9ccd8652013-09-13 16:36:46 +0000510 break;
511 }
drh5b47efa2010-02-12 18:18:39 +0000512
513 /*
drh9b0cf342015-11-12 14:57:19 +0000514 ** PRAGMA [schema.]max_page_count
515 ** PRAGMA [schema.]max_page_count=N
drh60ac3f42010-11-23 18:59:27 +0000516 **
517 ** The first form reports the current setting for the
518 ** maximum number of pages in the database file. The
519 ** second form attempts to change this setting. Both
520 ** forms return the current setting.
521 **
drhe73c9142011-11-09 16:12:24 +0000522 ** The absolute value of N is used. This is undocumented and might
523 ** change. The only purpose is to provide an easy way to test
524 ** the sqlite3AbsInt32() function.
525 **
drh9b0cf342015-11-12 14:57:19 +0000526 ** PRAGMA [schema.]page_count
danielk197759a93792008-05-15 17:48:20 +0000527 **
528 ** Return the number of pages in the specified database.
529 */
drh9ccd8652013-09-13 16:36:46 +0000530 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000531 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000532 sqlite3CodeVerifySchema(pParse, iDb);
533 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000534 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000535 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
536 }else{
drhe73c9142011-11-09 16:12:24 +0000537 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
538 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000539 }
danielk197759a93792008-05-15 17:48:20 +0000540 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
541 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000542 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
drh9ccd8652013-09-13 16:36:46 +0000543 break;
544 }
danielk197759a93792008-05-15 17:48:20 +0000545
546 /*
drh9b0cf342015-11-12 14:57:19 +0000547 ** PRAGMA [schema.]locking_mode
548 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
danielk197741483462007-03-24 16:45:04 +0000549 */
drh9ccd8652013-09-13 16:36:46 +0000550 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000551 const char *zRet = "normal";
552 int eMode = getLockingMode(zRight);
553
554 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
555 /* Simple "PRAGMA locking_mode;" statement. This is a query for
556 ** the current default locking mode (which may be different to
557 ** the locking-mode of the main database).
558 */
559 eMode = db->dfltLockMode;
560 }else{
561 Pager *pPager;
562 if( pId2->n==0 ){
563 /* This indicates that no database name was specified as part
564 ** of the PRAGMA command. In this case the locking-mode must be
565 ** set on all attached databases, as well as the main db file.
566 **
567 ** Also, the sqlite3.dfltLockMode variable is set so that
568 ** any subsequently attached databases also use the specified
569 ** locking mode.
570 */
571 int ii;
572 assert(pDb==&db->aDb[0]);
573 for(ii=2; ii<db->nDb; ii++){
574 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
575 sqlite3PagerLockingMode(pPager, eMode);
576 }
drh4f21c4a2008-12-10 22:15:00 +0000577 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000578 }
579 pPager = sqlite3BtreePager(pDb->pBt);
580 eMode = sqlite3PagerLockingMode(pPager, eMode);
581 }
582
drh9ccd8652013-09-13 16:36:46 +0000583 assert( eMode==PAGER_LOCKINGMODE_NORMAL
584 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000585 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
586 zRet = "exclusive";
587 }
drh7cc023c2015-09-03 04:28:25 +0000588 returnSingleText(v, "locking_mode", zRet);
drh9ccd8652013-09-13 16:36:46 +0000589 break;
590 }
drh3b020132008-04-17 17:02:01 +0000591
592 /*
drh9b0cf342015-11-12 14:57:19 +0000593 ** PRAGMA [schema.]journal_mode
594 ** PRAGMA [schema.]journal_mode =
drh3ebaee92010-05-06 21:37:22 +0000595 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000596 */
drh9ccd8652013-09-13 16:36:46 +0000597 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000598 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
599 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000600
drhb460e522015-09-03 03:29:51 +0000601 setOneColumnName(v, "journal_mode");
drh3b020132008-04-17 17:02:01 +0000602 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000603 /* If there is no "=MODE" part of the pragma, do a query for the
604 ** current mode */
drh3b020132008-04-17 17:02:01 +0000605 eMode = PAGER_JOURNALMODE_QUERY;
606 }else{
dane04dc882010-04-20 18:53:15 +0000607 const char *zMode;
drhea678832008-12-10 19:26:22 +0000608 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000609 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000610 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
611 }
612 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000613 /* If the "=MODE" part does not match any known journal mode,
614 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000615 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000616 }
617 }
drhc6b2a0f2010-07-08 17:40:37 +0000618 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
619 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
620 iDb = 0;
621 pId2->n = 1;
622 }
623 for(ii=db->nDb-1; ii>=0; ii--){
624 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
625 sqlite3VdbeUsesBtree(v, ii);
626 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000627 }
drh3b020132008-04-17 17:02:01 +0000628 }
drh3b020132008-04-17 17:02:01 +0000629 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000630 break;
631 }
danielk1977b53e4962008-06-04 06:45:59 +0000632
633 /*
drh9b0cf342015-11-12 14:57:19 +0000634 ** PRAGMA [schema.]journal_size_limit
635 ** PRAGMA [schema.]journal_size_limit=N
danielk1977b53e4962008-06-04 06:45:59 +0000636 **
drha9e364f2009-01-13 20:14:15 +0000637 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000638 */
drh9ccd8652013-09-13 16:36:46 +0000639 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000640 Pager *pPager = sqlite3BtreePager(pDb->pBt);
641 i64 iLimit = -2;
642 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000643 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000644 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000645 }
646 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh7cc023c2015-09-03 04:28:25 +0000647 returnSingleInt(v, "journal_size_limit", iLimit);
drh9ccd8652013-09-13 16:36:46 +0000648 break;
649 }
danielk1977b53e4962008-06-04 06:45:59 +0000650
drh13d70422004-11-13 15:59:14 +0000651#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000652
653 /*
drh9b0cf342015-11-12 14:57:19 +0000654 ** PRAGMA [schema.]auto_vacuum
655 ** PRAGMA [schema.]auto_vacuum=N
danielk1977951af802004-11-05 15:45:09 +0000656 **
drha9e364f2009-01-13 20:14:15 +0000657 ** Get or set the value of the database 'auto-vacuum' parameter.
658 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000659 */
660#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000661 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000662 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000663 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000664 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000665 returnSingleInt(v, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000666 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000667 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000668 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000669 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000670 /* Call SetAutoVacuum() to set initialize the internal auto and
671 ** incr-vacuum flags. This is required in case this connection
672 ** creates the database file. It is important that it is created
673 ** as an auto-vacuum capable db.
674 */
675 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
676 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
677 /* When setting the auto_vacuum mode to either "full" or
678 ** "incremental", write the value of meta[6] in the database
679 ** file. Before writing to meta[6], check that meta[3] indicates
680 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000681 */
drhb06a4ec2014-03-10 18:03:09 +0000682 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000683 static const VdbeOpList setMeta6[] = {
684 { OP_Transaction, 0, 1, 0}, /* 0 */
685 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
686 { OP_If, 1, 0, 0}, /* 2 */
687 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
drh1861afc2016-02-01 21:48:34 +0000688 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
drhf63936e2013-10-03 14:08:07 +0000689 };
drh2ce18652016-01-16 20:50:21 +0000690 VdbeOp *aOp;
691 int iAddr = sqlite3VdbeCurrentAddr(v);
drhdad300d2016-01-18 00:20:26 +0000692 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
drh2ce18652016-01-16 20:50:21 +0000693 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhdad300d2016-01-18 00:20:26 +0000694 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +0000695 aOp[0].p1 = iDb;
696 aOp[1].p1 = iDb;
697 aOp[2].p2 = iAddr+4;
drh1861afc2016-02-01 21:48:34 +0000698 aOp[4].p1 = iDb;
699 aOp[4].p3 = eAuto - 1;
drhf63936e2013-10-03 14:08:07 +0000700 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000701 }
danielk1977951af802004-11-05 15:45:09 +0000702 }
drh9ccd8652013-09-13 16:36:46 +0000703 break;
704 }
danielk1977951af802004-11-05 15:45:09 +0000705#endif
706
drhca5557f2007-05-04 18:30:40 +0000707 /*
drh9b0cf342015-11-12 14:57:19 +0000708 ** PRAGMA [schema.]incremental_vacuum(N)
drhca5557f2007-05-04 18:30:40 +0000709 **
710 ** Do N steps of incremental vacuuming on a database.
711 */
712#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000713 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000714 int iLimit, addr;
715 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
716 iLimit = 0x7fffffff;
717 }
718 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000719 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000720 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000721 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000722 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000723 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000724 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000725 break;
726 }
drhca5557f2007-05-04 18:30:40 +0000727#endif
728
drh13d70422004-11-13 15:59:14 +0000729#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000730 /*
drh9b0cf342015-11-12 14:57:19 +0000731 ** PRAGMA [schema.]cache_size
732 ** PRAGMA [schema.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000733 **
734 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000735 ** page cache size. The second form sets the local
736 ** page cache size value. If N is positive then that is the
737 ** number of pages in the cache. If N is negative, then the
738 ** number of pages is adjusted so that the cache uses -N kibibytes
739 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000740 */
drh9ccd8652013-09-13 16:36:46 +0000741 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000742 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000743 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000744 returnSingleInt(v, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000745 }else{
drh3b42abb2011-11-09 14:23:04 +0000746 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000747 pDb->pSchema->cache_size = size;
748 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000749 }
drh9ccd8652013-09-13 16:36:46 +0000750 break;
751 }
drhc11d4f92003-04-06 21:08:24 +0000752
753 /*
drh9b0cf342015-11-12 14:57:19 +0000754 ** PRAGMA [schema.]cache_spill
755 ** PRAGMA cache_spill=BOOLEAN
756 ** PRAGMA [schema.]cache_spill=N
757 **
758 ** The first form reports the current local setting for the
759 ** page cache spill size. The second form turns cache spill on
760 ** or off. When turnning cache spill on, the size is set to the
761 ** current cache_size. The third form sets a spill size that
762 ** may be different form the cache size.
763 ** If N is positive then that is the
764 ** number of pages in the cache. If N is negative, then the
765 ** number of pages is adjusted so that the cache uses -N kibibytes
766 ** of memory.
767 **
768 ** If the number of cache_spill pages is less then the number of
769 ** cache_size pages, no spilling occurs until the page count exceeds
770 ** the number of cache_size pages.
771 **
772 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
773 ** not just the schema specified.
774 */
775 case PragTyp_CACHE_SPILL: {
drh9b0cf342015-11-12 14:57:19 +0000776 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
777 if( !zRight ){
drh9b0cf342015-11-12 14:57:19 +0000778 returnSingleInt(v, "cache_spill",
779 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
780 sqlite3BtreeSetSpillSize(pDb->pBt,0));
781 }else{
drh4f9c8ec2015-11-12 15:47:48 +0000782 int size = 1;
drh9b0cf342015-11-12 14:57:19 +0000783 if( sqlite3GetInt32(zRight, &size) ){
784 sqlite3BtreeSetSpillSize(pDb->pBt, size);
785 }
drh4f9c8ec2015-11-12 15:47:48 +0000786 if( sqlite3GetBoolean(zRight, size!=0) ){
drh9b0cf342015-11-12 14:57:19 +0000787 db->flags |= SQLITE_CacheSpill;
788 }else{
789 db->flags &= ~SQLITE_CacheSpill;
790 }
drh4f9c8ec2015-11-12 15:47:48 +0000791 setAllPagerFlags(db);
drh9b0cf342015-11-12 14:57:19 +0000792 }
793 break;
794 }
795
796 /*
797 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000798 **
drh0d0614b2013-03-25 23:09:28 +0000799 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000800 ** used to limit the aggregate size of all memory mapped regions of the
801 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000802 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000803 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000804 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000805 **
drh0d0614b2013-03-25 23:09:28 +0000806 ** This value is advisory. The underlying VFS is free to memory map
807 ** as little or as much as it wants. Except, if N is set to 0 then the
808 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000809 */
drh9ccd8652013-09-13 16:36:46 +0000810 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000811 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000812#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000813 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000814 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000815 int ii;
drh9296c182014-07-23 13:40:49 +0000816 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000817 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
818 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000819 for(ii=db->nDb-1; ii>=0; ii--){
820 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000821 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000822 }
823 }
dan5d8a1372013-03-19 19:28:06 +0000824 }
drh9b4c59f2013-04-15 17:03:42 +0000825 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000826 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000827#else
dan3719f5f2013-05-23 10:13:18 +0000828 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000829 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000830#endif
dan3719f5f2013-05-23 10:13:18 +0000831 if( rc==SQLITE_OK ){
drh7cc023c2015-09-03 04:28:25 +0000832 returnSingleInt(v, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +0000833 }else if( rc!=SQLITE_NOTFOUND ){
834 pParse->nErr++;
835 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000836 }
drh9ccd8652013-09-13 16:36:46 +0000837 break;
838 }
dan5d8a1372013-03-19 19:28:06 +0000839
840 /*
drh90f5ecb2004-07-22 01:19:35 +0000841 ** PRAGMA temp_store
842 ** PRAGMA temp_store = "default"|"memory"|"file"
843 **
844 ** Return or set the local value of the temp_store flag. Changing
845 ** the local value does not make changes to the disk file and the default
846 ** value will be restored the next time the database is opened.
847 **
848 ** Note that it is possible for the library compile-time options to
849 ** override this setting
850 */
drh9ccd8652013-09-13 16:36:46 +0000851 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000852 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000853 returnSingleInt(v, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000854 }else{
855 changeTempStorage(pParse, zRight);
856 }
drh9ccd8652013-09-13 16:36:46 +0000857 break;
858 }
drh90f5ecb2004-07-22 01:19:35 +0000859
860 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000861 ** PRAGMA temp_store_directory
862 ** PRAGMA temp_store_directory = ""|"directory_name"
863 **
864 ** Return or set the local value of the temp_store_directory flag. Changing
865 ** the value sets a specific directory to be used for temporary files.
866 ** Setting to a null string reverts to the default temporary directory search.
867 ** If temporary directory is changed, then invalidateTempStorage.
868 **
869 */
drh9ccd8652013-09-13 16:36:46 +0000870 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000871 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000872 returnSingleText(v, "temp_store_directory", sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000873 }else{
drh78f82d12008-09-02 00:52:52 +0000874#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000875 if( zRight[0] ){
876 int res;
danielk1977fab11272008-09-16 14:38:02 +0000877 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
878 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000879 sqlite3ErrorMsg(pParse, "not a writable directory");
880 goto pragma_out;
881 }
drh268283b2005-01-08 15:44:25 +0000882 }
danielk1977b06a0b62008-06-26 10:54:12 +0000883 if( SQLITE_TEMP_STORE==0
884 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
885 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000886 ){
887 invalidateTempStorage(pParse);
888 }
drh17435752007-08-16 04:30:38 +0000889 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000890 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000891 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000892 }else{
drh268283b2005-01-08 15:44:25 +0000893 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000894 }
drh78f82d12008-09-02 00:52:52 +0000895#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000896 }
drh9ccd8652013-09-13 16:36:46 +0000897 break;
898 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000899
drhcc716452012-06-06 23:23:23 +0000900#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000901 /*
902 ** PRAGMA data_store_directory
903 ** PRAGMA data_store_directory = ""|"directory_name"
904 **
905 ** Return or set the local value of the data_store_directory flag. Changing
906 ** the value sets a specific directory to be used for database files that
907 ** were specified with a relative pathname. Setting to a null string reverts
908 ** to the default database directory, which for database files specified with
909 ** a relative path will probably be based on the current directory for the
910 ** process. Database file specified with an absolute path are not impacted
911 ** by this setting, regardless of its value.
912 **
913 */
drh9ccd8652013-09-13 16:36:46 +0000914 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +0000915 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000916 returnSingleText(v, "data_store_directory", sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +0000917 }else{
918#ifndef SQLITE_OMIT_WSD
919 if( zRight[0] ){
920 int res;
921 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
922 if( rc!=SQLITE_OK || res==0 ){
923 sqlite3ErrorMsg(pParse, "not a writable directory");
924 goto pragma_out;
925 }
926 }
927 sqlite3_free(sqlite3_data_directory);
928 if( zRight[0] ){
929 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
930 }else{
931 sqlite3_data_directory = 0;
932 }
933#endif /* SQLITE_OMIT_WSD */
934 }
drh9ccd8652013-09-13 16:36:46 +0000935 break;
936 }
drhcc716452012-06-06 23:23:23 +0000937#endif
mistachkina112d142012-03-14 00:44:01 +0000938
drhd2cb50b2009-01-09 21:41:17 +0000939#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000940 /*
drh9b0cf342015-11-12 14:57:19 +0000941 ** PRAGMA [schema.]lock_proxy_file
942 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +0000943 **
944 ** Return or set the value of the lock_proxy_file flag. Changing
945 ** the value sets a specific file to be used for database access locks.
946 **
947 */
948 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +0000949 if( !zRight ){
950 Pager *pPager = sqlite3BtreePager(pDb->pBt);
951 char *proxy_file_path = NULL;
952 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000953 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000954 &proxy_file_path);
drh7cc023c2015-09-03 04:28:25 +0000955 returnSingleText(v, "lock_proxy_file", proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +0000956 }else{
957 Pager *pPager = sqlite3BtreePager(pDb->pBt);
958 sqlite3_file *pFile = sqlite3PagerFile(pPager);
959 int res;
960 if( zRight[0] ){
961 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
962 zRight);
963 } else {
964 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
965 NULL);
966 }
967 if( res!=SQLITE_OK ){
968 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
969 goto pragma_out;
970 }
971 }
drh9ccd8652013-09-13 16:36:46 +0000972 break;
973 }
drhd2cb50b2009-01-09 21:41:17 +0000974#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000975
976 /*
drh9b0cf342015-11-12 14:57:19 +0000977 ** PRAGMA [schema.]synchronous
drh6841b1c2016-02-03 19:20:15 +0000978 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
drhc11d4f92003-04-06 21:08:24 +0000979 **
980 ** Return or set the local value of the synchronous flag. Changing
981 ** the local value does not make changes to the disk file and the
982 ** default value will be restored the next time the database is
983 ** opened.
984 */
drh9ccd8652013-09-13 16:36:46 +0000985 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +0000986 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000987 returnSingleInt(v, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000988 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000989 if( !db->autoCommit ){
990 sqlite3ErrorMsg(pParse,
991 "Safety level may not be changed inside a transaction");
992 }else{
drhd99d2832015-04-17 15:58:33 +0000993 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
994 if( iLevel==0 ) iLevel = 1;
995 pDb->safety_level = iLevel;
drhd3605a42013-08-17 15:42:29 +0000996 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +0000997 }
drhc11d4f92003-04-06 21:08:24 +0000998 }
drh9ccd8652013-09-13 16:36:46 +0000999 break;
1000 }
drh13d70422004-11-13 15:59:14 +00001001#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001002
drhbf216272005-02-26 18:10:44 +00001003#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001004 case PragTyp_FLAG: {
1005 if( zRight==0 ){
drh7cc023c2015-09-03 04:28:25 +00001006 returnSingleInt(v, pPragma->zName, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001007 }else{
drhc228be52015-01-31 02:00:01 +00001008 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001009 if( db->autoCommit==0 ){
1010 /* Foreign key support may not be enabled or disabled while not
1011 ** in auto-commit mode. */
1012 mask &= ~(SQLITE_ForeignKeys);
1013 }
drhd4530972014-09-09 14:47:53 +00001014#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001015 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001016 /* Do not allow non-admin users to modify the schema arbitrarily */
1017 mask &= ~(SQLITE_WriteSchema);
1018 }
1019#endif
drh9ccd8652013-09-13 16:36:46 +00001020
1021 if( sqlite3GetBoolean(zRight, 0) ){
1022 db->flags |= mask;
1023 }else{
1024 db->flags &= ~mask;
1025 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1026 }
1027
1028 /* Many of the flag-pragmas modify the code generated by the SQL
1029 ** compiler (eg. count_changes). So add an opcode to expire all
1030 ** compiled SQL statements after modifying a pragma value.
1031 */
1032 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
1033 setAllPagerFlags(db);
1034 }
1035 break;
1036 }
drhbf216272005-02-26 18:10:44 +00001037#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001038
drh13d70422004-11-13 15:59:14 +00001039#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001040 /*
1041 ** PRAGMA table_info(<table>)
1042 **
1043 ** Return a single row for each column of the named table. The columns of
1044 ** the returned data set are:
1045 **
1046 ** cid: Column id (numbered from left to right, starting at 0)
1047 ** name: Column name
1048 ** type: Column declaration type.
1049 ** notnull: True if 'NOT NULL' is part of column declaration
1050 ** dflt_value: The default value for the column, if any.
1051 */
drh9ccd8652013-09-13 16:36:46 +00001052 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001053 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001054 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001055 if( pTab ){
drhb460e522015-09-03 03:29:51 +00001056 static const char *azCol[] = {
1057 "cid", "name", "type", "notnull", "dflt_value", "pk"
1058 };
drh384b7fe2013-01-01 13:55:31 +00001059 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001060 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001061 Column *pCol;
drh44156282013-10-23 22:23:03 +00001062 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001063 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001064 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001065 setAllColumnNames(v, 6, azCol); assert( 6==ArraySize(azCol) );
danielk19774adee202004-05-08 08:23:19 +00001066 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001067 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001068 if( IsHiddenColumn(pCol) ){
1069 nHidden++;
1070 continue;
1071 }
drh384b7fe2013-01-01 13:55:31 +00001072 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1073 k = 0;
1074 }else if( pPk==0 ){
1075 k = 1;
1076 }else{
drh1b678962015-04-15 07:19:27 +00001077 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001078 }
drh076e85f2015-09-03 13:46:12 +00001079 sqlite3VdbeMultiLoad(v, 1, "issisi",
1080 i-nHidden,
1081 pCol->zName,
1082 pCol->zType ? pCol->zType : "",
1083 pCol->notNull ? 1 : 0,
1084 pCol->zDflt,
1085 k);
drh2d401ab2008-01-10 23:50:11 +00001086 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001087 }
1088 }
drh9ccd8652013-09-13 16:36:46 +00001089 }
1090 break;
drhc11d4f92003-04-06 21:08:24 +00001091
drh3ef26152013-10-12 20:22:00 +00001092 case PragTyp_STATS: {
drhb460e522015-09-03 03:29:51 +00001093 static const char *azCol[] = { "table", "index", "width", "height" };
drh3ef26152013-10-12 20:22:00 +00001094 Index *pIdx;
1095 HashElem *i;
1096 v = sqlite3GetVdbe(pParse);
drh3ef26152013-10-12 20:22:00 +00001097 pParse->nMem = 4;
1098 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001099 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
drh3ef26152013-10-12 20:22:00 +00001100 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1101 Table *pTab = sqliteHashData(i);
drh076e85f2015-09-03 13:46:12 +00001102 sqlite3VdbeMultiLoad(v, 1, "ssii",
1103 pTab->zName,
1104 0,
1105 (int)sqlite3LogEstToInt(pTab->szTabRow),
1106 (int)sqlite3LogEstToInt(pTab->nRowLogEst));
drh3ef26152013-10-12 20:22:00 +00001107 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1108 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh076e85f2015-09-03 13:46:12 +00001109 sqlite3VdbeMultiLoad(v, 2, "sii",
1110 pIdx->zName,
1111 (int)sqlite3LogEstToInt(pIdx->szIdxRow),
1112 (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]));
drh3ef26152013-10-12 20:22:00 +00001113 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1114 }
1115 }
1116 }
1117 break;
1118
drh9ccd8652013-09-13 16:36:46 +00001119 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001120 Index *pIdx;
1121 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001122 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001123 if( pIdx ){
drhb460e522015-09-03 03:29:51 +00001124 static const char *azCol[] = {
1125 "seqno", "cid", "name", "desc", "coll", "key"
1126 };
drhc11d4f92003-04-06 21:08:24 +00001127 int i;
drh5e7028c2015-03-05 14:29:02 +00001128 int mx;
1129 if( pPragma->iArg ){
1130 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1131 mx = pIdx->nColumn;
1132 pParse->nMem = 6;
1133 }else{
1134 /* PRAGMA index_info (legacy version) */
1135 mx = pIdx->nKeyCol;
1136 pParse->nMem = 3;
1137 }
drhc11d4f92003-04-06 21:08:24 +00001138 pTab = pIdx->pTable;
drhc95e01d2013-02-14 16:16:05 +00001139 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001140 assert( pParse->nMem<=ArraySize(azCol) );
drhb460e522015-09-03 03:29:51 +00001141 setAllColumnNames(v, pParse->nMem, azCol);
drhc228be52015-01-31 02:00:01 +00001142 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001143 i16 cnum = pIdx->aiColumn[i];
drh076e85f2015-09-03 13:46:12 +00001144 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum,
1145 cnum<0 ? 0 : pTab->aCol[cnum].zName);
drh5e7028c2015-03-05 14:29:02 +00001146 if( pPragma->iArg ){
drh076e85f2015-09-03 13:46:12 +00001147 sqlite3VdbeMultiLoad(v, 4, "isi",
1148 pIdx->aSortOrder[i],
1149 pIdx->azColl[i],
1150 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001151 }
1152 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001153 }
1154 }
drh9ccd8652013-09-13 16:36:46 +00001155 }
1156 break;
drhc11d4f92003-04-06 21:08:24 +00001157
drh9ccd8652013-09-13 16:36:46 +00001158 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001159 Index *pIdx;
1160 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001161 int i;
drh2783e4b2004-10-05 15:42:53 +00001162 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001163 if( pTab ){
drhb460e522015-09-03 03:29:51 +00001164 static const char *azCol[] = {
1165 "seq", "name", "unique", "origin", "partial"
1166 };
danielk19774adee202004-05-08 08:23:19 +00001167 v = sqlite3GetVdbe(pParse);
drhc228be52015-01-31 02:00:01 +00001168 pParse->nMem = 5;
drhe13e9f52013-10-05 19:18:00 +00001169 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001170 setAllColumnNames(v, 5, azCol); assert( 5==ArraySize(azCol) );
drh3ef26152013-10-12 20:22:00 +00001171 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001172 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001173 sqlite3VdbeMultiLoad(v, 1, "isisi",
1174 i,
1175 pIdx->zName,
1176 IsUniqueIndex(pIdx),
1177 azOrigin[pIdx->idxType],
1178 pIdx->pPartIdxWhere!=0);
drhc228be52015-01-31 02:00:01 +00001179 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drhc11d4f92003-04-06 21:08:24 +00001180 }
1181 }
drh9ccd8652013-09-13 16:36:46 +00001182 }
1183 break;
drhc11d4f92003-04-06 21:08:24 +00001184
drh9ccd8652013-09-13 16:36:46 +00001185 case PragTyp_DATABASE_LIST: {
drhb460e522015-09-03 03:29:51 +00001186 static const char *azCol[] = { "seq", "name", "file" };
drh13d70422004-11-13 15:59:14 +00001187 int i;
drh2d401ab2008-01-10 23:50:11 +00001188 pParse->nMem = 3;
drh076e85f2015-09-03 13:46:12 +00001189 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
drh13d70422004-11-13 15:59:14 +00001190 for(i=0; i<db->nDb; i++){
1191 if( db->aDb[i].pBt==0 ) continue;
1192 assert( db->aDb[i].zName!=0 );
drh076e85f2015-09-03 13:46:12 +00001193 sqlite3VdbeMultiLoad(v, 1, "iss",
1194 i,
1195 db->aDb[i].zName,
1196 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh2d401ab2008-01-10 23:50:11 +00001197 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001198 }
drh9ccd8652013-09-13 16:36:46 +00001199 }
1200 break;
danielk197748af65a2005-02-09 03:20:37 +00001201
drh9ccd8652013-09-13 16:36:46 +00001202 case PragTyp_COLLATION_LIST: {
drhb460e522015-09-03 03:29:51 +00001203 static const char *azCol[] = { "seq", "name" };
danielk197748af65a2005-02-09 03:20:37 +00001204 int i = 0;
1205 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001206 pParse->nMem = 2;
drh076e85f2015-09-03 13:46:12 +00001207 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
danielk197748af65a2005-02-09 03:20:37 +00001208 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1209 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001210 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
drh2d401ab2008-01-10 23:50:11 +00001211 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001212 }
drh9ccd8652013-09-13 16:36:46 +00001213 }
1214 break;
drh13d70422004-11-13 15:59:14 +00001215#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1216
drhb7f91642004-10-31 02:22:47 +00001217#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001218 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001219 FKey *pFK;
1220 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001221 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001222 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001223 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001224 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001225 if( pFK ){
drhb460e522015-09-03 03:29:51 +00001226 static const char *azCol[] = {
1227 "id", "seq", "table", "from", "to", "on_update", "on_delete",
1228 "match"
1229 };
danielk1977742f9472004-06-16 12:02:43 +00001230 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001231 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001232 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001233 setAllColumnNames(v, 8, azCol); assert( 8==ArraySize(azCol) );
danielk1977742f9472004-06-16 12:02:43 +00001234 while(pFK){
1235 int j;
1236 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001237 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1238 i,
1239 j,
1240 pFK->zTo,
1241 pTab->aCol[pFK->aCol[j].iFrom].zName,
1242 pFK->aCol[j].zCol,
1243 actionName(pFK->aAction[1]), /* ON UPDATE */
1244 actionName(pFK->aAction[0]), /* ON DELETE */
1245 "NONE");
danielk197750af3e12008-10-10 17:47:21 +00001246 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001247 }
1248 ++i;
1249 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001250 }
drh78100cc2003-08-23 22:40:53 +00001251 }
1252 }
drh9ccd8652013-09-13 16:36:46 +00001253 }
1254 break;
drhb7f91642004-10-31 02:22:47 +00001255#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001256
drh6c5b9152012-12-17 16:46:37 +00001257#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001258#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001259 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001260 FKey *pFK; /* A foreign key constraint */
1261 Table *pTab; /* Child table contain "REFERENCES" keyword */
1262 Table *pParent; /* Parent table that child points to */
1263 Index *pIdx; /* Index in the parent table */
1264 int i; /* Loop counter: Foreign key number for pTab */
1265 int j; /* Loop counter: Field of the foreign key */
1266 HashElem *k; /* Loop counter: Next table in schema */
1267 int x; /* result variable */
1268 int regResult; /* 3 registers to hold a result row */
1269 int regKey; /* Register to hold key for checking the FK */
1270 int regRow; /* Registers to hold a row from pTab */
1271 int addrTop; /* Top of a loop checking foreign keys */
1272 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001273 int *aiCols; /* child to parent column mapping */
drhb460e522015-09-03 03:29:51 +00001274 static const char *azCol[] = { "table", "rowid", "parent", "fkid" };
drh6c5b9152012-12-17 16:46:37 +00001275
drh613028b2012-12-17 18:43:02 +00001276 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001277 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001278 regKey = ++pParse->nMem;
1279 regRow = ++pParse->nMem;
1280 v = sqlite3GetVdbe(pParse);
drh076e85f2015-09-03 13:46:12 +00001281 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
drh613028b2012-12-17 18:43:02 +00001282 sqlite3CodeVerifySchema(pParse, iDb);
1283 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1284 while( k ){
1285 if( zRight ){
1286 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1287 k = 0;
1288 }else{
1289 pTab = (Table*)sqliteHashData(k);
1290 k = sqliteHashNext(k);
1291 }
drh9148def2012-12-17 20:40:39 +00001292 if( pTab==0 || pTab->pFKey==0 ) continue;
1293 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001294 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001295 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001296 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh6c5b9152012-12-17 16:46:37 +00001297 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001298 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1299 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001300 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001301 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001302 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001303 if( x==0 ){
1304 if( pIdx==0 ){
1305 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1306 }else{
drh6c5b9152012-12-17 16:46:37 +00001307 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001308 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001309 }
1310 }else{
drh613028b2012-12-17 18:43:02 +00001311 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001312 break;
1313 }
drh6c5b9152012-12-17 16:46:37 +00001314 }
dan5e878302013-10-12 19:06:48 +00001315 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001316 if( pFK ) break;
1317 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001318 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001319 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001320 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001321 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001322 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001323 if( pParent ){
1324 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1325 assert( x==0 );
1326 }
drh613028b2012-12-17 18:43:02 +00001327 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001328 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001329 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001330 assert( iKey>=0 && iKey<pTab->nCol );
1331 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001332 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1333 sqlite3ColumnDefault(v, pTab, iKey, regRow);
drh688852a2014-02-17 22:40:43 +00001334 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
1335 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1336 sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001337 }else{
drh613028b2012-12-17 18:43:02 +00001338 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001339 }
drh688852a2014-02-17 22:40:43 +00001340 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001341 sqlite3VdbeGoto(v, addrOk);
drh613028b2012-12-17 18:43:02 +00001342 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1343 }else{
1344 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001345 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001346 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh688852a2014-02-17 22:40:43 +00001347 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001348 }
dan5e878302013-10-12 19:06:48 +00001349 if( pParent ){
drh57bf4a82014-02-17 14:59:22 +00001350 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
drhe9107692015-08-25 19:20:04 +00001351 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
dan5e878302013-10-12 19:06:48 +00001352 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh688852a2014-02-17 22:40:43 +00001353 VdbeCoverage(v);
dan5e878302013-10-12 19:06:48 +00001354 }
drh6c5b9152012-12-17 16:46:37 +00001355 }
drh613028b2012-12-17 18:43:02 +00001356 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh076e85f2015-09-03 13:46:12 +00001357 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001358 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001359 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001360 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001361 }
drh688852a2014-02-17 22:40:43 +00001362 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001363 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001364 }
drh9ccd8652013-09-13 16:36:46 +00001365 }
1366 break;
dan09ff9e12013-03-11 11:49:03 +00001367#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001368#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1369
drhc11d4f92003-04-06 21:08:24 +00001370#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001371 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001372 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001373 if( sqlite3GetBoolean(zRight, 0) ){
drh97e58a22015-11-10 14:27:17 +00001374 sqlite3ParserTrace(stdout, "parser: ");
drh55ef4d92005-08-14 01:20:37 +00001375 }else{
1376 sqlite3ParserTrace(0, 0);
1377 }
drhc11d4f92003-04-06 21:08:24 +00001378 }
drh9ccd8652013-09-13 16:36:46 +00001379 }
1380 break;
drhc11d4f92003-04-06 21:08:24 +00001381#endif
1382
drh55ef4d92005-08-14 01:20:37 +00001383 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1384 ** used will be case sensitive or not depending on the RHS.
1385 */
drh9ccd8652013-09-13 16:36:46 +00001386 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001387 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001388 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001389 }
drh9ccd8652013-09-13 16:36:46 +00001390 }
1391 break;
drh55ef4d92005-08-14 01:20:37 +00001392
drh1dcdbc02007-01-27 02:24:54 +00001393#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1394# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1395#endif
1396
drhb7f91642004-10-31 02:22:47 +00001397#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001398 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001399 ** integrity_check designed to detect most database corruption
1400 ** without most of the overhead of a full integrity-check.
1401 */
drh9ccd8652013-09-13 16:36:46 +00001402 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001403 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001404
drhc5227312011-10-13 17:09:01 +00001405 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001406
dan5885e762012-07-16 10:06:12 +00001407 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1408 ** then iDb is set to the index of the database identified by <db>.
1409 ** In this case, the integrity of database iDb only is verified by
1410 ** the VDBE created below.
1411 **
1412 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1413 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1414 ** to -1 here, to indicate that the VDBE should verify the integrity
1415 ** of all attached databases. */
1416 assert( iDb>=0 );
1417 assert( iDb==0 || pId2->z );
1418 if( pId2->z==0 ) iDb = -1;
1419
drhed717fe2003-06-15 23:42:24 +00001420 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001421 pParse->nMem = 6;
drhb460e522015-09-03 03:29:51 +00001422 setOneColumnName(v, "integrity_check");
drh1dcdbc02007-01-27 02:24:54 +00001423
1424 /* Set the maximum error count */
1425 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1426 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001427 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001428 if( mxErr<=0 ){
1429 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1430 }
1431 }
drh2d401ab2008-01-10 23:50:11 +00001432 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001433
1434 /* Do an integrity check on each database file */
1435 for(i=0; i<db->nDb; i++){
1436 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001437 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001438 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001439
danielk197753c0f742005-03-29 03:10:59 +00001440 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001441 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001442
drh80242052004-06-09 00:48:12 +00001443 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001444 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh688852a2014-02-17 22:40:43 +00001445 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001446 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001447 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001448
drhed717fe2003-06-15 23:42:24 +00001449 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001450 **
1451 ** Begin by filling registers 2, 3, ... with the root pages numbers
1452 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001453 */
dan5885e762012-07-16 10:06:12 +00001454 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001455 pTbls = &db->aDb[i].pSchema->tblHash;
1456 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001457 Table *pTab = sqliteHashData(x);
1458 Index *pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001459 if( HasRowid(pTab) ){
1460 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001461 VdbeComment((v, "%s", pTab->zName));
drh6fbe41a2013-10-30 20:22:55 +00001462 cnt++;
1463 }
drh79069752004-05-22 21:30:40 +00001464 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001465 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001466 VdbeComment((v, "%s", pIdx->zName));
drh79069752004-05-22 21:30:40 +00001467 cnt++;
1468 }
1469 }
drh2d401ab2008-01-10 23:50:11 +00001470
1471 /* Make sure sufficient number of registers have been allocated */
drh6fbe41a2013-10-30 20:22:55 +00001472 pParse->nMem = MAX( pParse->nMem, cnt+8 );
drh2d401ab2008-01-10 23:50:11 +00001473
1474 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001475 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001476 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001477 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001478 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001479 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001480 P4_DYNAMIC);
drh079a3072014-03-19 14:10:55 +00001481 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001482 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001483 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001484 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001485
1486 /* Make sure all the indices are constructed correctly.
1487 */
danielk197741c58b72007-12-29 13:39:19 +00001488 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001489 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001490 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001491 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001492 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001493 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001494 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001495
1496 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001497 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001498 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh688852a2014-02-17 22:40:43 +00001499 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001500 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001501 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001502 sqlite3ExprCacheClear(pParse);
danfd261ec2015-10-22 20:54:33 +00001503 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001504 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001505 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001506 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001507 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001508 }
drh6fbe41a2013-10-30 20:22:55 +00001509 pParse->nMem = MAX(pParse->nMem, 8+j);
drh688852a2014-02-17 22:40:43 +00001510 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001511 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhcefc87f2014-08-01 01:40:33 +00001512 /* Verify that all NOT NULL columns really are NOT NULL */
1513 for(j=0; j<pTab->nCol; j++){
1514 char *zErr;
1515 int jmp2, jmp3;
1516 if( j==pTab->iPKey ) continue;
1517 if( pTab->aCol[j].notNull==0 ) continue;
1518 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1519 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1520 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1521 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1522 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1523 pTab->aCol[j].zName);
1524 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1525 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1526 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1527 sqlite3VdbeAddOp0(v, OP_Halt);
1528 sqlite3VdbeJumpHere(v, jmp2);
1529 sqlite3VdbeJumpHere(v, jmp3);
1530 }
1531 /* Validate index entries for the current row */
drhed717fe2003-06-15 23:42:24 +00001532 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhcefc87f2014-08-01 01:40:33 +00001533 int jmp2, jmp3, jmp4, jmp5;
1534 int ckUniq = sqlite3VdbeMakeLabel(v);
drh6fbe41a2013-10-30 20:22:55 +00001535 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001536 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1537 pPrior, r1);
1538 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001539 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drhcefc87f2014-08-01 01:40:33 +00001540 /* Verify that an index entry exists for the current table row */
1541 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
drh688852a2014-02-17 22:40:43 +00001542 pIdx->nColumn); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001543 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001544 sqlite3VdbeLoadString(v, 3, "row ");
drh6fbe41a2013-10-30 20:22:55 +00001545 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001546 sqlite3VdbeLoadString(v, 4, " missing from index ");
drh6fbe41a2013-10-30 20:22:55 +00001547 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001548 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
drh6fbe41a2013-10-30 20:22:55 +00001549 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1550 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh688852a2014-02-17 22:40:43 +00001551 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001552 sqlite3VdbeAddOp0(v, OP_Halt);
drhd654be82005-09-20 17:42:23 +00001553 sqlite3VdbeJumpHere(v, jmp2);
drhcefc87f2014-08-01 01:40:33 +00001554 /* For UNIQUE indexes, verify that only one entry exists with the
1555 ** current key. The entry is unique if (1) any column is NULL
1556 ** or (2) the next entry has a different key */
1557 if( IsUniqueIndex(pIdx) ){
1558 int uniqOk = sqlite3VdbeMakeLabel(v);
1559 int jmp6;
1560 int kk;
1561 for(kk=0; kk<pIdx->nKeyCol; kk++){
1562 int iCol = pIdx->aiColumn[kk];
drh4b92f982015-09-29 17:20:14 +00001563 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
drh68391ac2015-09-25 20:49:16 +00001564 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
drhcefc87f2014-08-01 01:40:33 +00001565 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1566 VdbeCoverage(v);
1567 }
1568 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001569 sqlite3VdbeGoto(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00001570 sqlite3VdbeJumpHere(v, jmp6);
1571 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1572 pIdx->nKeyCol); VdbeCoverage(v);
1573 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001574 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1575 sqlite3VdbeGoto(v, jmp5);
drhcefc87f2014-08-01 01:40:33 +00001576 sqlite3VdbeResolveLabel(v, uniqOk);
1577 }
1578 sqlite3VdbeJumpHere(v, jmp4);
drh87744512014-04-13 19:15:49 +00001579 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhed717fe2003-06-15 23:42:24 +00001580 }
drh688852a2014-02-17 22:40:43 +00001581 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001582 sqlite3VdbeJumpHere(v, loopTop-1);
1583#ifndef SQLITE_OMIT_BTREECOUNT
drh076e85f2015-09-03 13:46:12 +00001584 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
drh8a9789b2013-08-01 03:36:59 +00001585 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001586 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001587 addr = sqlite3VdbeCurrentAddr(v);
drh688852a2014-02-17 22:40:43 +00001588 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001589 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001590 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh688852a2014-02-17 22:40:43 +00001591 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
drh5655c542014-02-19 19:14:34 +00001592 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh8a9789b2013-08-01 03:36:59 +00001593 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh076e85f2015-09-03 13:46:12 +00001594 sqlite3VdbeLoadString(v, 3, pIdx->zName);
drh8a9789b2013-08-01 03:36:59 +00001595 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1596 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001597 }
drh8a9789b2013-08-01 03:36:59 +00001598#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001599 }
1600 }
drh2ce18652016-01-16 20:50:21 +00001601 {
1602 static const int iLn = VDBE_OFFSET_LINENO(2);
1603 static const VdbeOpList endCode[] = {
1604 { OP_AddImm, 1, 0, 0}, /* 0 */
drh1b325542016-02-03 01:55:44 +00001605 { OP_If, 1, 4, 0}, /* 1 */
drh2ce18652016-01-16 20:50:21 +00001606 { OP_String8, 0, 3, 0}, /* 2 */
drh1b325542016-02-03 01:55:44 +00001607 { OP_ResultRow, 3, 1, 0}, /* 3 */
drh2ce18652016-01-16 20:50:21 +00001608 };
1609 VdbeOp *aOp;
1610
1611 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
1612 if( aOp ){
1613 aOp[0].p2 = -mxErr;
drh2ce18652016-01-16 20:50:21 +00001614 aOp[2].p4type = P4_STATIC;
1615 aOp[2].p4.z = "ok";
1616 }
1617 }
drh9ccd8652013-09-13 16:36:46 +00001618 }
1619 break;
drhb7f91642004-10-31 02:22:47 +00001620#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1621
drh13d70422004-11-13 15:59:14 +00001622#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001623 /*
1624 ** PRAGMA encoding
1625 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1626 **
drh85b623f2007-12-13 21:54:09 +00001627 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001628 ** database. If the database is not initialized, it is initialized now.
1629 **
1630 ** The second form of this pragma is a no-op if the main database file
1631 ** has not already been initialized. In this case it sets the default
1632 ** encoding that will be used for the main database file if a new file
1633 ** is created. If an existing main database file is opened, then the
1634 ** default text encoding for the existing database is used.
1635 **
1636 ** In all cases new databases created using the ATTACH command are
1637 ** created to use the same default text encoding as the main database. If
1638 ** the main database has not been initialized and/or created when ATTACH
1639 ** is executed, this is done before the ATTACH operation.
1640 **
1641 ** In the second form this pragma sets the text encoding to be used in
1642 ** new database files created using this database handle. It is only
1643 ** useful if invoked immediately after the main database i
1644 */
drh9ccd8652013-09-13 16:36:46 +00001645 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001646 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001647 char *zName;
1648 u8 enc;
1649 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001650 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001651 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1652 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1653 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001654 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001655 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001656 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1657 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001658 { 0, 0 }
1659 };
drh0f7eb612006-08-08 13:51:43 +00001660 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001661 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001662 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00001663 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1664 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1665 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drh7cc023c2015-09-03 04:28:25 +00001666 returnSingleText(v, "encoding", encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00001667 }else{ /* "PRAGMA encoding = XXX" */
1668 /* Only change the value of sqlite.enc if the database handle is not
1669 ** initialized. If the main database exists, the new sqlite.enc value
1670 ** will be overwritten when the schema is next loaded. If it does not
1671 ** already exists, it will be created to use the new encoding value.
1672 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001673 if(
1674 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1675 DbHasProperty(db, 0, DB_Empty)
1676 ){
danielk19778e227872004-06-07 07:52:17 +00001677 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1678 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh9bd3cc42014-12-12 23:17:54 +00001679 SCHEMA_ENC(db) = ENC(db) =
1680 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001681 break;
1682 }
1683 }
1684 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001685 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001686 }
1687 }
1688 }
drh9ccd8652013-09-13 16:36:46 +00001689 }
1690 break;
drh13d70422004-11-13 15:59:14 +00001691#endif /* SQLITE_OMIT_UTF16 */
1692
1693#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001694 /*
drh9b0cf342015-11-12 14:57:19 +00001695 ** PRAGMA [schema.]schema_version
1696 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001697 **
drh9b0cf342015-11-12 14:57:19 +00001698 ** PRAGMA [schema.]user_version
1699 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001700 **
drh9b0cf342015-11-12 14:57:19 +00001701 ** PRAGMA [schema.]freelist_count = <integer>
drh4ee09b42013-05-01 19:49:27 +00001702 **
drh9b0cf342015-11-12 14:57:19 +00001703 ** PRAGMA [schema.]application_id
1704 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00001705 **
danielk1977b92b70b2004-11-12 16:11:59 +00001706 ** The pragma's schema_version and user_version are used to set or get
1707 ** the value of the schema-version and user-version, respectively. Both
1708 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001709 ** stored in the database header.
1710 **
1711 ** The schema-cookie is usually only manipulated internally by SQLite. It
1712 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001713 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001714 ** SQLite each time a query is executed to ensure that the internal cache
1715 ** of the schema used when compiling the SQL query matches the schema of
1716 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001717 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1718 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001719 ** crashes or database corruption. Use with caution!
1720 **
danielk1977b92b70b2004-11-12 16:11:59 +00001721 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001722 ** applications for any purpose.
1723 */
drh9ccd8652013-09-13 16:36:46 +00001724 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00001725 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00001726 sqlite3VdbeUsesBtree(v, iDb);
drhc228be52015-01-31 02:00:01 +00001727 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00001728 /* Write the specified cookie value */
1729 static const VdbeOpList setCookie[] = {
1730 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1861afc2016-02-01 21:48:34 +00001731 { OP_SetCookie, 0, 0, 0}, /* 1 */
danielk1977dae24952004-11-11 05:10:43 +00001732 };
drh2ce18652016-01-16 20:50:21 +00001733 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001734 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
drh2ce18652016-01-16 20:50:21 +00001735 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
drhdad300d2016-01-18 00:20:26 +00001736 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001737 aOp[0].p1 = iDb;
drh1861afc2016-02-01 21:48:34 +00001738 aOp[1].p1 = iDb;
1739 aOp[1].p2 = iCookie;
1740 aOp[1].p3 = sqlite3Atoi(zRight);
danielk1977dae24952004-11-11 05:10:43 +00001741 }else{
1742 /* Read the specified cookie value */
1743 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001744 { OP_Transaction, 0, 0, 0}, /* 0 */
1745 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001746 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001747 };
drh2ce18652016-01-16 20:50:21 +00001748 VdbeOp *aOp;
drhdad300d2016-01-18 00:20:26 +00001749 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
drh2ce18652016-01-16 20:50:21 +00001750 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
drhdad300d2016-01-18 00:20:26 +00001751 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
drh2ce18652016-01-16 20:50:21 +00001752 aOp[0].p1 = iDb;
1753 aOp[1].p1 = iDb;
1754 aOp[1].p3 = iCookie;
danielk1977dae24952004-11-11 05:10:43 +00001755 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001756 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001757 }
drh9ccd8652013-09-13 16:36:46 +00001758 }
1759 break;
drh13d70422004-11-13 15:59:14 +00001760#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001761
shanehdc97a8c2010-02-23 20:08:35 +00001762#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1763 /*
1764 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001765 **
drh71caabf2010-02-26 15:39:24 +00001766 ** Return the names of all compile-time options used in this build,
1767 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001768 */
drh9ccd8652013-09-13 16:36:46 +00001769 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00001770 int i = 0;
1771 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00001772 pParse->nMem = 1;
drhb460e522015-09-03 03:29:51 +00001773 setOneColumnName(v, "compile_option");
shanehdc97a8c2010-02-23 20:08:35 +00001774 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00001775 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00001776 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1777 }
drh9ccd8652013-09-13 16:36:46 +00001778 }
1779 break;
shanehdc97a8c2010-02-23 20:08:35 +00001780#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1781
dan5cf53532010-05-01 16:40:20 +00001782#ifndef SQLITE_OMIT_WAL
1783 /*
drh9b0cf342015-11-12 14:57:19 +00001784 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00001785 **
1786 ** Checkpoint the database.
1787 */
drh9ccd8652013-09-13 16:36:46 +00001788 case PragTyp_WAL_CHECKPOINT: {
drhb460e522015-09-03 03:29:51 +00001789 static const char *azCol[] = { "busy", "log", "checkpointed" };
dana58f26f2010-11-16 18:56:51 +00001790 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001791 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1792 if( zRight ){
1793 if( sqlite3StrICmp(zRight, "full")==0 ){
1794 eMode = SQLITE_CHECKPOINT_FULL;
1795 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1796 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00001797 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1798 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00001799 }
1800 }
drh076e85f2015-09-03 13:46:12 +00001801 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
dancdc1f042010-11-18 12:11:05 +00001802 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00001803 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001804 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00001805 }
1806 break;
dan5a299f92010-05-03 11:05:08 +00001807
1808 /*
1809 ** PRAGMA wal_autocheckpoint
1810 ** PRAGMA wal_autocheckpoint = N
1811 **
1812 ** Configure a database connection to automatically checkpoint a database
1813 ** after accumulating N frames in the log. Or query for the current value
1814 ** of N.
1815 */
drh9ccd8652013-09-13 16:36:46 +00001816 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00001817 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001818 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001819 }
drh7cc023c2015-09-03 04:28:25 +00001820 returnSingleInt(v, "wal_autocheckpoint",
drhb033d8b2010-05-03 13:37:30 +00001821 db->xWalCallback==sqlite3WalDefaultHook ?
1822 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00001823 }
1824 break;
dan5cf53532010-05-01 16:40:20 +00001825#endif
dan7c246102010-04-12 19:00:29 +00001826
drh09419b42011-11-16 19:29:17 +00001827 /*
1828 ** PRAGMA shrink_memory
1829 **
drh51a74d42015-02-28 01:04:27 +00001830 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
1831 ** connection on which it is invoked to free up as much memory as it
1832 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00001833 */
drh9ccd8652013-09-13 16:36:46 +00001834 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00001835 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00001836 break;
1837 }
drh09419b42011-11-16 19:29:17 +00001838
drhf3603962012-09-07 16:46:59 +00001839 /*
1840 ** PRAGMA busy_timeout
1841 ** PRAGMA busy_timeout = N
1842 **
1843 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001844 ** if one is set. If no busy handler or a different busy handler is set
1845 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1846 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001847 */
drhd49c3582013-09-13 19:00:06 +00001848 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00001849 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00001850 if( zRight ){
1851 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1852 }
drh7cc023c2015-09-03 04:28:25 +00001853 returnSingleInt(v, "timeout", db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00001854 break;
1855 }
drhf3603962012-09-07 16:46:59 +00001856
drh55e85ca2013-09-13 21:01:56 +00001857 /*
1858 ** PRAGMA soft_heap_limit
1859 ** PRAGMA soft_heap_limit = N
1860 **
drh51a74d42015-02-28 01:04:27 +00001861 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
1862 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
1863 ** specified and is a non-negative integer.
1864 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
1865 ** returns the same integer that would be returned by the
1866 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00001867 */
1868 case PragTyp_SOFT_HEAP_LIMIT: {
1869 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00001870 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00001871 sqlite3_soft_heap_limit64(N);
1872 }
drh7cc023c2015-09-03 04:28:25 +00001873 returnSingleInt(v, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00001874 break;
1875 }
1876
drh03459612014-08-25 15:13:22 +00001877 /*
1878 ** PRAGMA threads
1879 ** PRAGMA threads = N
1880 **
1881 ** Configure the maximum number of worker threads. Return the new
1882 ** maximum, which might be less than requested.
1883 */
1884 case PragTyp_THREADS: {
1885 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00001886 if( zRight
drh03459612014-08-25 15:13:22 +00001887 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
1888 && N>=0
1889 ){
drh111544c2014-08-29 16:20:47 +00001890 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00001891 }
drh7cc023c2015-09-03 04:28:25 +00001892 returnSingleInt(v, "threads",
drh111544c2014-08-29 16:20:47 +00001893 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00001894 break;
1895 }
1896
dougcurrie81c95ef2004-06-18 23:21:47 +00001897#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001898 /*
1899 ** Report the current state of file logs for all databases
1900 */
drh9ccd8652013-09-13 16:36:46 +00001901 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00001902 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001903 "unlocked", "shared", "reserved", "pending", "exclusive"
1904 };
drhb460e522015-09-03 03:29:51 +00001905 static const char *azCol[] = { "database", "status" };
drh89ac8c12004-06-09 14:17:20 +00001906 int i;
drh076e85f2015-09-03 13:46:12 +00001907 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
drh2d401ab2008-01-10 23:50:11 +00001908 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00001909 for(i=0; i<db->nDb; i++){
1910 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001911 const char *zState = "unknown";
1912 int j;
drh89ac8c12004-06-09 14:17:20 +00001913 if( db->aDb[i].zName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00001914 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001915 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001916 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001917 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001918 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1919 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001920 }
drh076e85f2015-09-03 13:46:12 +00001921 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zName, zState);
drh2d401ab2008-01-10 23:50:11 +00001922 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001923 }
drh9ccd8652013-09-13 16:36:46 +00001924 break;
1925 }
drh89ac8c12004-06-09 14:17:20 +00001926#endif
1927
shanehad9f9f62010-02-17 17:48:46 +00001928#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00001929 case PragTyp_KEY: {
1930 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1931 break;
1932 }
1933 case PragTyp_REKEY: {
1934 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1935 break;
1936 }
1937 case PragTyp_HEXKEY: {
1938 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00001939 u8 iByte;
1940 int i;
drh9ccd8652013-09-13 16:36:46 +00001941 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00001942 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
1943 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
1944 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00001945 }
1946 if( (zLeft[3] & 0xf)==0xb ){
1947 sqlite3_key_v2(db, zDb, zKey, i/2);
1948 }else{
1949 sqlite3_rekey_v2(db, zDb, zKey, i/2);
1950 }
drhd2cb50b2009-01-09 21:41:17 +00001951 }
drh9ccd8652013-09-13 16:36:46 +00001952 break;
1953 }
drh3c4f2a42005-12-08 18:12:56 +00001954#endif
shanehad9f9f62010-02-17 17:48:46 +00001955#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00001956 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001957#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001958 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001959 sqlite3_activate_see(&zRight[4]);
1960 }
1961#endif
1962#ifdef SQLITE_ENABLE_CEROD
1963 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001964 sqlite3_activate_cerod(&zRight[6]);
1965 }
1966#endif
drh9ccd8652013-09-13 16:36:46 +00001967 }
1968 break;
drh21e2cab2006-09-25 18:01:57 +00001969#endif
drh3c4f2a42005-12-08 18:12:56 +00001970
drh9ccd8652013-09-13 16:36:46 +00001971 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00001972
danielk1977e0048402004-06-15 16:51:01 +00001973pragma_out:
drh633e6d52008-07-28 19:34:53 +00001974 sqlite3DbFree(db, zLeft);
1975 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001976}
drh13d70422004-11-13 15:59:14 +00001977
drh8bfdf722009-06-19 14:06:03 +00001978#endif /* SQLITE_OMIT_PRAGMA */