blob: 85693cddfe8f092cb74882a3bd043997af2c6095 [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,
jplyonb1639ff2004-01-19 04:52:29 +000035** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
drh908c0052012-01-30 18:40:55 +000036** unrecognized string argument. The FULL option is disallowed
37** 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){
drh722e95a2004-10-25 20:33:44 +000045 /* 123456789 123456789 */
46 static const char zText[] = "onoffalseyestruefull";
47 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
48 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
49 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
50 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +000051 if( sqlite3Isdigit(*z) ){
drh60ac3f42010-11-23 18:59:27 +000052 return (u8)sqlite3Atoi(z);
drhc11d4f92003-04-06 21:08:24 +000053 }
drhea678832008-12-10 19:26:22 +000054 n = sqlite3Strlen30(z);
drh908c0052012-01-30 18:40:55 +000055 for(i=0; i<ArraySize(iLength)-omitFull; i++){
drh722e95a2004-10-25 20:33:44 +000056 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
57 return iValue[i];
58 }
drhc11d4f92003-04-06 21:08:24 +000059 }
drh908c0052012-01-30 18:40:55 +000060 return dflt;
drhc11d4f92003-04-06 21:08:24 +000061}
62
63/*
drh722e95a2004-10-25 20:33:44 +000064** Interpret the given string as a boolean value.
65*/
drheac5bd72014-07-25 21:35:39 +000066u8 sqlite3GetBoolean(const char *z, u8 dflt){
drh38d9c612012-01-31 14:24:47 +000067 return getSafetyLevel(z,1,dflt)!=0;
drh722e95a2004-10-25 20:33:44 +000068}
69
drhc7dc9bf2011-06-03 13:02:57 +000070/* The sqlite3GetBoolean() function is used by other modules but the
71** remainder of this file is specific to PRAGMA processing. So omit
72** the rest of the file if PRAGMAs are omitted from the build.
73*/
74#if !defined(SQLITE_OMIT_PRAGMA)
75
danielk197741483462007-03-24 16:45:04 +000076/*
77** Interpret the given string as a locking mode value.
78*/
79static int getLockingMode(const char *z){
80 if( z ){
81 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
82 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
83 }
84 return PAGER_LOCKINGMODE_QUERY;
85}
86
danielk1977dddbcdc2007-04-26 14:42:34 +000087#ifndef SQLITE_OMIT_AUTOVACUUM
88/*
89** Interpret the given string as an auto-vacuum mode value.
90**
91** The following strings, "none", "full" and "incremental" are
92** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
93*/
94static int getAutoVacuum(const char *z){
95 int i;
96 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
97 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
98 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
drh60ac3f42010-11-23 18:59:27 +000099 i = sqlite3Atoi(z);
drh4f21c4a2008-12-10 22:15:00 +0000100 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +0000101}
102#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
103
danielk1977b84f96f2005-01-20 11:32:23 +0000104#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +0000105/*
drh90f5ecb2004-07-22 01:19:35 +0000106** Interpret the given string as a temp db location. Return 1 for file
107** backed temporary databases, 2 for the Red-Black tree in memory database
108** and 0 to use the compile-time default.
109*/
110static int getTempStore(const char *z){
111 if( z[0]>='0' && z[0]<='2' ){
112 return z[0] - '0';
113 }else if( sqlite3StrICmp(z, "file")==0 ){
114 return 1;
115 }else if( sqlite3StrICmp(z, "memory")==0 ){
116 return 2;
117 }else{
118 return 0;
119 }
120}
drhbf216272005-02-26 18:10:44 +0000121#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000122
drhbf216272005-02-26 18:10:44 +0000123#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000124/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000125** Invalidate temp storage, either when the temp storage is changed
126** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000127*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000128static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000129 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000130 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000131 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000132 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
133 "from within a transaction");
134 return SQLITE_ERROR;
135 }
136 sqlite3BtreeClose(db->aDb[1].pBt);
137 db->aDb[1].pBt = 0;
drh81028a42012-05-15 18:28:27 +0000138 sqlite3ResetAllSchemasOfConnection(db);
drh90f5ecb2004-07-22 01:19:35 +0000139 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000140 return SQLITE_OK;
141}
drhbf216272005-02-26 18:10:44 +0000142#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000143
drhbf216272005-02-26 18:10:44 +0000144#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000145/*
146** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000147** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000148** or DEFAULT_TEMP_STORE pragmas.
149*/
150static int changeTempStorage(Parse *pParse, const char *zStorageType){
151 int ts = getTempStore(zStorageType);
152 sqlite3 *db = pParse->db;
153 if( db->temp_store==ts ) return SQLITE_OK;
154 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
155 return SQLITE_ERROR;
156 }
drh4f21c4a2008-12-10 22:15:00 +0000157 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000158 return SQLITE_OK;
159}
drhbf216272005-02-26 18:10:44 +0000160#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000161
162/*
drhb460e522015-09-03 03:29:51 +0000163** Set the names of the first N columns to the values in azCol[]
164*/
165static void setAllColumnNames(
166 Vdbe *v, /* The query under construction */
167 int N, /* Number of columns */
168 const char **azCol /* Names of columns */
169){
170 int i;
171 sqlite3VdbeSetNumCols(v, N);
172 for(i=0; i<N; i++){
173 sqlite3VdbeSetColName(v, i, COLNAME_NAME, azCol[i], SQLITE_STATIC);
174 }
175}
176static void setOneColumnName(Vdbe *v, const char *z){
177 setAllColumnNames(v, 1, &z);
178}
179
180/*
drh90f5ecb2004-07-22 01:19:35 +0000181** Generate code to return a single integer value.
182*/
drh7cc023c2015-09-03 04:28:25 +0000183static void returnSingleInt(Vdbe *v, const char *zLabel, i64 value){
184 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
drhb460e522015-09-03 03:29:51 +0000185 setOneColumnName(v, zLabel);
drh7cc023c2015-09-03 04:28:25 +0000186 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
187}
188
189/*
190** Generate code to return a single text value.
191*/
192static void returnSingleText(
193 Vdbe *v, /* Prepared statement under construction */
194 const char *zLabel, /* Name of the result column */
195 const char *zValue /* Value to be returned */
196){
197 if( zValue ){
drh076e85f2015-09-03 13:46:12 +0000198 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
drh7cc023c2015-09-03 04:28:25 +0000199 setOneColumnName(v, zLabel);
200 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
201 }
drh90f5ecb2004-07-22 01:19:35 +0000202}
203
drhd3605a42013-08-17 15:42:29 +0000204
205/*
206** Set the safety_level and pager flags for pager iDb. Or if iDb<0
207** set these values for all pagers.
208*/
209#ifndef SQLITE_OMIT_PAGER_PRAGMAS
210static void setAllPagerFlags(sqlite3 *db){
211 if( db->autoCommit ){
212 Db *pDb = db->aDb;
213 int n = db->nDb;
214 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
215 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
216 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
217 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
218 == PAGER_FLAGS_MASK );
219 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
220 while( (n--) > 0 ){
221 if( pDb->pBt ){
222 sqlite3BtreeSetPagerFlags(pDb->pBt,
223 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
224 }
225 pDb++;
226 }
227 }
228}
drhfeb56e02013-08-23 17:33:46 +0000229#else
230# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29 +0000231#endif
232
233
drhd2cb50b2009-01-09 21:41:17 +0000234/*
235** Return a human-readable name for a constraint resolution action.
236*/
danba9108b2009-09-22 07:13:42 +0000237#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000238static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000239 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000240 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000241 case OE_SetNull: zName = "SET NULL"; break;
242 case OE_SetDflt: zName = "SET DEFAULT"; break;
243 case OE_Cascade: zName = "CASCADE"; break;
244 case OE_Restrict: zName = "RESTRICT"; break;
245 default: zName = "NO ACTION";
246 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000247 }
drhd2cb50b2009-01-09 21:41:17 +0000248 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000249}
danba9108b2009-09-22 07:13:42 +0000250#endif
danielk197750af3e12008-10-10 17:47:21 +0000251
dane04dc882010-04-20 18:53:15 +0000252
253/*
254** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
255** defined in pager.h. This function returns the associated lowercase
256** journal-mode name.
257*/
258const char *sqlite3JournalModename(int eMode){
259 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000260 "delete", "persist", "off", "truncate", "memory"
261#ifndef SQLITE_OMIT_WAL
262 , "wal"
263#endif
dane04dc882010-04-20 18:53:15 +0000264 };
265 assert( PAGER_JOURNALMODE_DELETE==0 );
266 assert( PAGER_JOURNALMODE_PERSIST==1 );
267 assert( PAGER_JOURNALMODE_OFF==2 );
268 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
269 assert( PAGER_JOURNALMODE_MEMORY==4 );
270 assert( PAGER_JOURNALMODE_WAL==5 );
271 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
272
273 if( eMode==ArraySize(azModeName) ) return 0;
274 return azModeName[eMode];
275}
276
drh87223182004-02-21 14:00:29 +0000277/*
drhc11d4f92003-04-06 21:08:24 +0000278** Process a pragma statement.
279**
280** Pragmas are of this form:
281**
drh9b0cf342015-11-12 14:57:19 +0000282** PRAGMA [schema.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000283**
284** The identifier might also be a string. The value is a string, and
285** identifier, or a number. If minusFlag is true, then the value is
286** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000287**
288** If the left side is "database.id" then pId1 is the database name
289** and pId2 is the id. If the left side is just "id" then pId1 is the
290** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000291*/
danielk197791cf71b2004-06-26 06:37:06 +0000292void sqlite3Pragma(
293 Parse *pParse,
drh9b0cf342015-11-12 14:57:19 +0000294 Token *pId1, /* First part of [schema.]id field */
295 Token *pId2, /* Second part of [schema.]id field, or NULL */
danielk197791cf71b2004-06-26 06:37:06 +0000296 Token *pValue, /* Token for <value>, or NULL */
297 int minusFlag /* True if a '-' sign preceded <value> */
298){
299 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
300 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
301 const char *zDb = 0; /* The database name */
302 Token *pId; /* Pointer to <id> token */
drh3fa97302012-02-22 16:58:36 +0000303 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000304 int iDb; /* Database index for <database> */
mistachkin1a51ce72015-01-12 18:38:02 +0000305 int lwr, upr, mid = 0; /* Binary search bounds */
drh06fd5d62012-02-22 14:45:19 +0000306 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
307 sqlite3 *db = pParse->db; /* The database connection */
308 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000309 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drhc228be52015-01-31 02:00:01 +0000310 const struct sPragmaNames *pPragma;
drh06fd5d62012-02-22 14:45:19 +0000311
drhc11d4f92003-04-06 21:08:24 +0000312 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000313 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000314 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000315
drh9b0cf342015-11-12 14:57:19 +0000316 /* Interpret the [schema.] part of the pragma statement. iDb is the
danielk197791cf71b2004-06-26 06:37:06 +0000317 ** index of the database this pragma is being applied to in db.aDb[]. */
318 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
319 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000320 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000321
danielk1977ddfb2f02006-02-17 12:25:14 +0000322 /* If the temp database has been explicitly named as part of the
323 ** pragma, make sure it is open.
324 */
325 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
326 return;
327 }
328
drh17435752007-08-16 04:30:38 +0000329 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000330 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000331 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000332 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000333 }else{
drh17435752007-08-16 04:30:38 +0000334 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000335 }
danielk197791cf71b2004-06-26 06:37:06 +0000336
drhd2cb50b2009-01-09 21:41:17 +0000337 assert( pId2 );
338 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000339 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000340 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000341 }
drh06fd5d62012-02-22 14:45:19 +0000342
343 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
344 ** connection. If it returns SQLITE_OK, then assume that the VFS
345 ** handled the pragma and generate a no-op prepared statement.
drh8dd7a6a2015-03-06 04:37:26 +0000346 **
347 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
348 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
349 ** object corresponding to the database file to which the pragma
350 ** statement refers.
351 **
352 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
353 ** file control is an array of pointers to strings (char**) in which the
354 ** second element of the array is the name of the pragma and the third
355 ** element is the argument to the pragma or NULL if the pragma has no
356 ** argument.
drh06fd5d62012-02-22 14:45:19 +0000357 */
drh3fa97302012-02-22 16:58:36 +0000358 aFcntl[0] = 0;
359 aFcntl[1] = zLeft;
360 aFcntl[2] = zRight;
361 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000362 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000363 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
364 if( rc==SQLITE_OK ){
drh7cc023c2015-09-03 04:28:25 +0000365 returnSingleText(v, "result", aFcntl[0]);
366 sqlite3_free(aFcntl[0]);
drh9ccd8652013-09-13 16:36:46 +0000367 goto pragma_out;
368 }
369 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000370 if( aFcntl[0] ){
371 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
372 sqlite3_free(aFcntl[0]);
373 }
374 pParse->nErr++;
375 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000376 goto pragma_out;
377 }
378
379 /* Locate the pragma in the lookup table */
380 lwr = 0;
381 upr = ArraySize(aPragmaNames)-1;
382 while( lwr<=upr ){
383 mid = (lwr+upr)/2;
384 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
385 if( rc==0 ) break;
386 if( rc<0 ){
387 upr = mid - 1;
388 }else{
389 lwr = mid + 1;
390 }
391 }
392 if( lwr>upr ) goto pragma_out;
drhc228be52015-01-31 02:00:01 +0000393 pPragma = &aPragmaNames[mid];
drh9ccd8652013-09-13 16:36:46 +0000394
drhf63936e2013-10-03 14:08:07 +0000395 /* Make sure the database schema is loaded if the pragma requires that */
drhc228be52015-01-31 02:00:01 +0000396 if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){
drhf63936e2013-10-03 14:08:07 +0000397 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
398 }
399
drh9ccd8652013-09-13 16:36:46 +0000400 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01 +0000401 switch( pPragma->ePragTyp ){
drh9ccd8652013-09-13 16:36:46 +0000402
drhe73c9142011-11-09 16:12:24 +0000403#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000404 /*
drh9b0cf342015-11-12 14:57:19 +0000405 ** PRAGMA [schema.]default_cache_size
406 ** PRAGMA [schema.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000407 **
408 ** The first form reports the current persistent setting for the
409 ** page cache size. The value returned is the maximum number of
410 ** pages in the page cache. The second form sets both the current
411 ** page cache size value and the persistent page cache size value
412 ** stored in the database file.
413 **
drh93791ea2010-04-26 17:36:35 +0000414 ** Older versions of SQLite would set the default cache size to a
415 ** negative number to indicate synchronous=OFF. These days, synchronous
416 ** is always on by default regardless of the sign of the default cache
417 ** size. But continue to take the absolute value of the default cache
418 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000419 */
drh9ccd8652013-09-13 16:36:46 +0000420 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09 +0000421 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000422 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000423 { OP_Transaction, 0, 0, 0}, /* 0 */
424 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000425 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000426 { OP_Integer, 0, 2, 0},
427 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000428 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000429 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000430 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000431 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000432 };
drh905793e2004-02-21 13:31:09 +0000433 int addr;
drhfb982642007-08-30 01:19:59 +0000434 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000435 if( !zRight ){
drhb460e522015-09-03 03:29:51 +0000436 setOneColumnName(v, "cache_size");
drh3c84ddf2008-01-09 02:15:38 +0000437 pParse->nMem += 2;
drh688852a2014-02-17 22:40:43 +0000438 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
danielk197791cf71b2004-06-26 06:37:06 +0000439 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000440 sqlite3VdbeChangeP1(v, addr+1, iDb);
441 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000442 }else{
drhd50ffc42011-03-08 02:38:28 +0000443 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000444 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000445 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000446 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000447 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000448 pDb->pSchema->cache_size = size;
449 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000450 }
drh9ccd8652013-09-13 16:36:46 +0000451 break;
452 }
drhe73c9142011-11-09 16:12:24 +0000453#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000454
drhe73c9142011-11-09 16:12:24 +0000455#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000456 /*
drh9b0cf342015-11-12 14:57:19 +0000457 ** PRAGMA [schema.]page_size
458 ** PRAGMA [schema.]page_size=N
drh90f5ecb2004-07-22 01:19:35 +0000459 **
460 ** The first form reports the current setting for the
461 ** database page size in bytes. The second form sets the
462 ** database page size value. The value can only be set if
463 ** the database has not yet been created.
464 */
drh9ccd8652013-09-13 16:36:46 +0000465 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000466 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000467 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000468 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000469 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh7cc023c2015-09-03 04:28:25 +0000470 returnSingleInt(v, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000471 }else{
danielk1977992772c2007-08-30 10:07:38 +0000472 /* Malloc may fail when setting the page-size, as there is an internal
473 ** buffer that the pager module resizes using sqlite3_realloc().
474 */
drh60ac3f42010-11-23 18:59:27 +0000475 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000476 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000477 db->mallocFailed = 1;
478 }
drh90f5ecb2004-07-22 01:19:35 +0000479 }
drh9ccd8652013-09-13 16:36:46 +0000480 break;
481 }
danielk197741483462007-03-24 16:45:04 +0000482
483 /*
drh9b0cf342015-11-12 14:57:19 +0000484 ** PRAGMA [schema.]secure_delete
485 ** PRAGMA [schema.]secure_delete=ON/OFF
drh5b47efa2010-02-12 18:18:39 +0000486 **
487 ** The first form reports the current setting for the
488 ** secure_delete flag. The second form changes the secure_delete
489 ** flag setting and reports thenew value.
490 */
drh9ccd8652013-09-13 16:36:46 +0000491 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000492 Btree *pBt = pDb->pBt;
493 int b = -1;
494 assert( pBt!=0 );
495 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000496 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000497 }
drhaf034ed2010-02-12 19:46:26 +0000498 if( pId2->n==0 && b>=0 ){
499 int ii;
500 for(ii=0; ii<db->nDb; ii++){
501 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
502 }
503 }
drh5b47efa2010-02-12 18:18:39 +0000504 b = sqlite3BtreeSecureDelete(pBt, b);
drh7cc023c2015-09-03 04:28:25 +0000505 returnSingleInt(v, "secure_delete", b);
drh9ccd8652013-09-13 16:36:46 +0000506 break;
507 }
drh5b47efa2010-02-12 18:18:39 +0000508
509 /*
drh9b0cf342015-11-12 14:57:19 +0000510 ** PRAGMA [schema.]max_page_count
511 ** PRAGMA [schema.]max_page_count=N
drh60ac3f42010-11-23 18:59:27 +0000512 **
513 ** The first form reports the current setting for the
514 ** maximum number of pages in the database file. The
515 ** second form attempts to change this setting. Both
516 ** forms return the current setting.
517 **
drhe73c9142011-11-09 16:12:24 +0000518 ** The absolute value of N is used. This is undocumented and might
519 ** change. The only purpose is to provide an easy way to test
520 ** the sqlite3AbsInt32() function.
521 **
drh9b0cf342015-11-12 14:57:19 +0000522 ** PRAGMA [schema.]page_count
danielk197759a93792008-05-15 17:48:20 +0000523 **
524 ** Return the number of pages in the specified database.
525 */
drh9ccd8652013-09-13 16:36:46 +0000526 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000527 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000528 sqlite3CodeVerifySchema(pParse, iDb);
529 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000530 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000531 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
532 }else{
drhe73c9142011-11-09 16:12:24 +0000533 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
534 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000535 }
danielk197759a93792008-05-15 17:48:20 +0000536 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
537 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000538 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
drh9ccd8652013-09-13 16:36:46 +0000539 break;
540 }
danielk197759a93792008-05-15 17:48:20 +0000541
542 /*
drh9b0cf342015-11-12 14:57:19 +0000543 ** PRAGMA [schema.]locking_mode
544 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
danielk197741483462007-03-24 16:45:04 +0000545 */
drh9ccd8652013-09-13 16:36:46 +0000546 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000547 const char *zRet = "normal";
548 int eMode = getLockingMode(zRight);
549
550 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
551 /* Simple "PRAGMA locking_mode;" statement. This is a query for
552 ** the current default locking mode (which may be different to
553 ** the locking-mode of the main database).
554 */
555 eMode = db->dfltLockMode;
556 }else{
557 Pager *pPager;
558 if( pId2->n==0 ){
559 /* This indicates that no database name was specified as part
560 ** of the PRAGMA command. In this case the locking-mode must be
561 ** set on all attached databases, as well as the main db file.
562 **
563 ** Also, the sqlite3.dfltLockMode variable is set so that
564 ** any subsequently attached databases also use the specified
565 ** locking mode.
566 */
567 int ii;
568 assert(pDb==&db->aDb[0]);
569 for(ii=2; ii<db->nDb; ii++){
570 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
571 sqlite3PagerLockingMode(pPager, eMode);
572 }
drh4f21c4a2008-12-10 22:15:00 +0000573 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000574 }
575 pPager = sqlite3BtreePager(pDb->pBt);
576 eMode = sqlite3PagerLockingMode(pPager, eMode);
577 }
578
drh9ccd8652013-09-13 16:36:46 +0000579 assert( eMode==PAGER_LOCKINGMODE_NORMAL
580 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000581 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
582 zRet = "exclusive";
583 }
drh7cc023c2015-09-03 04:28:25 +0000584 returnSingleText(v, "locking_mode", zRet);
drh9ccd8652013-09-13 16:36:46 +0000585 break;
586 }
drh3b020132008-04-17 17:02:01 +0000587
588 /*
drh9b0cf342015-11-12 14:57:19 +0000589 ** PRAGMA [schema.]journal_mode
590 ** PRAGMA [schema.]journal_mode =
drh3ebaee92010-05-06 21:37:22 +0000591 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000592 */
drh9ccd8652013-09-13 16:36:46 +0000593 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000594 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
595 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000596
drhb460e522015-09-03 03:29:51 +0000597 setOneColumnName(v, "journal_mode");
drh3b020132008-04-17 17:02:01 +0000598 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000599 /* If there is no "=MODE" part of the pragma, do a query for the
600 ** current mode */
drh3b020132008-04-17 17:02:01 +0000601 eMode = PAGER_JOURNALMODE_QUERY;
602 }else{
dane04dc882010-04-20 18:53:15 +0000603 const char *zMode;
drhea678832008-12-10 19:26:22 +0000604 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000605 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000606 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
607 }
608 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000609 /* If the "=MODE" part does not match any known journal mode,
610 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000611 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000612 }
613 }
drhc6b2a0f2010-07-08 17:40:37 +0000614 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
615 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
616 iDb = 0;
617 pId2->n = 1;
618 }
619 for(ii=db->nDb-1; ii>=0; ii--){
620 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
621 sqlite3VdbeUsesBtree(v, ii);
622 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000623 }
drh3b020132008-04-17 17:02:01 +0000624 }
drh3b020132008-04-17 17:02:01 +0000625 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000626 break;
627 }
danielk1977b53e4962008-06-04 06:45:59 +0000628
629 /*
drh9b0cf342015-11-12 14:57:19 +0000630 ** PRAGMA [schema.]journal_size_limit
631 ** PRAGMA [schema.]journal_size_limit=N
danielk1977b53e4962008-06-04 06:45:59 +0000632 **
drha9e364f2009-01-13 20:14:15 +0000633 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000634 */
drh9ccd8652013-09-13 16:36:46 +0000635 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000636 Pager *pPager = sqlite3BtreePager(pDb->pBt);
637 i64 iLimit = -2;
638 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000639 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000640 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000641 }
642 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh7cc023c2015-09-03 04:28:25 +0000643 returnSingleInt(v, "journal_size_limit", iLimit);
drh9ccd8652013-09-13 16:36:46 +0000644 break;
645 }
danielk1977b53e4962008-06-04 06:45:59 +0000646
drh13d70422004-11-13 15:59:14 +0000647#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000648
649 /*
drh9b0cf342015-11-12 14:57:19 +0000650 ** PRAGMA [schema.]auto_vacuum
651 ** PRAGMA [schema.]auto_vacuum=N
danielk1977951af802004-11-05 15:45:09 +0000652 **
drha9e364f2009-01-13 20:14:15 +0000653 ** Get or set the value of the database 'auto-vacuum' parameter.
654 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000655 */
656#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000657 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000658 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000659 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000660 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000661 returnSingleInt(v, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000662 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000663 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000664 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000665 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000666 /* Call SetAutoVacuum() to set initialize the internal auto and
667 ** incr-vacuum flags. This is required in case this connection
668 ** creates the database file. It is important that it is created
669 ** as an auto-vacuum capable db.
670 */
671 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
672 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
673 /* When setting the auto_vacuum mode to either "full" or
674 ** "incremental", write the value of meta[6] in the database
675 ** file. Before writing to meta[6], check that meta[3] indicates
676 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000677 */
drhb06a4ec2014-03-10 18:03:09 +0000678 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000679 static const VdbeOpList setMeta6[] = {
680 { OP_Transaction, 0, 1, 0}, /* 0 */
681 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
682 { OP_If, 1, 0, 0}, /* 2 */
683 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
684 { OP_Integer, 0, 1, 0}, /* 4 */
685 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
686 };
687 int iAddr;
drh688852a2014-02-17 22:40:43 +0000688 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhf63936e2013-10-03 14:08:07 +0000689 sqlite3VdbeChangeP1(v, iAddr, iDb);
690 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
691 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
692 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
693 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
694 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000695 }
danielk1977951af802004-11-05 15:45:09 +0000696 }
drh9ccd8652013-09-13 16:36:46 +0000697 break;
698 }
danielk1977951af802004-11-05 15:45:09 +0000699#endif
700
drhca5557f2007-05-04 18:30:40 +0000701 /*
drh9b0cf342015-11-12 14:57:19 +0000702 ** PRAGMA [schema.]incremental_vacuum(N)
drhca5557f2007-05-04 18:30:40 +0000703 **
704 ** Do N steps of incremental vacuuming on a database.
705 */
706#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000707 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000708 int iLimit, addr;
709 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
710 iLimit = 0x7fffffff;
711 }
712 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000713 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000714 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000715 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000716 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000717 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000718 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000719 break;
720 }
drhca5557f2007-05-04 18:30:40 +0000721#endif
722
drh13d70422004-11-13 15:59:14 +0000723#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000724 /*
drh9b0cf342015-11-12 14:57:19 +0000725 ** PRAGMA [schema.]cache_size
726 ** PRAGMA [schema.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000727 **
728 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000729 ** page cache size. The second form sets the local
730 ** page cache size value. If N is positive then that is the
731 ** number of pages in the cache. If N is negative, then the
732 ** number of pages is adjusted so that the cache uses -N kibibytes
733 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000734 */
drh9ccd8652013-09-13 16:36:46 +0000735 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000736 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000737 if( !zRight ){
drh957026a2015-07-16 18:18:19 +0000738 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh7cc023c2015-09-03 04:28:25 +0000739 returnSingleInt(v, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000740 }else{
drh3b42abb2011-11-09 14:23:04 +0000741 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000742 pDb->pSchema->cache_size = size;
743 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drh8ea60e32015-07-29 14:10:43 +0000744 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000745 }
drh9ccd8652013-09-13 16:36:46 +0000746 break;
747 }
drhc11d4f92003-04-06 21:08:24 +0000748
749 /*
drh9b0cf342015-11-12 14:57:19 +0000750 ** PRAGMA [schema.]cache_spill
751 ** PRAGMA cache_spill=BOOLEAN
752 ** PRAGMA [schema.]cache_spill=N
753 **
754 ** The first form reports the current local setting for the
755 ** page cache spill size. The second form turns cache spill on
756 ** or off. When turnning cache spill on, the size is set to the
757 ** current cache_size. The third form sets a spill size that
758 ** may be different form the cache size.
759 ** If N is positive then that is the
760 ** number of pages in the cache. If N is negative, then the
761 ** number of pages is adjusted so that the cache uses -N kibibytes
762 ** of memory.
763 **
764 ** If the number of cache_spill pages is less then the number of
765 ** cache_size pages, no spilling occurs until the page count exceeds
766 ** the number of cache_size pages.
767 **
768 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
769 ** not just the schema specified.
770 */
771 case PragTyp_CACHE_SPILL: {
772 int size;
773 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
774 if( !zRight ){
775 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
776 returnSingleInt(v, "cache_spill",
777 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
778 sqlite3BtreeSetSpillSize(pDb->pBt,0));
779 }else{
780 if( sqlite3GetInt32(zRight, &size) ){
781 sqlite3BtreeSetSpillSize(pDb->pBt, size);
782 }
783 if( sqlite3GetBoolean(zRight, 0) ){
784 db->flags |= SQLITE_CacheSpill;
785 }else{
786 db->flags &= ~SQLITE_CacheSpill;
787 }
788 }
789 break;
790 }
791
792 /*
793 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000794 **
drh0d0614b2013-03-25 23:09:28 +0000795 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000796 ** used to limit the aggregate size of all memory mapped regions of the
797 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000798 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000799 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000800 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000801 **
drh0d0614b2013-03-25 23:09:28 +0000802 ** This value is advisory. The underlying VFS is free to memory map
803 ** as little or as much as it wants. Except, if N is set to 0 then the
804 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000805 */
drh9ccd8652013-09-13 16:36:46 +0000806 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000807 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000808#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000809 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000810 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000811 int ii;
drh9296c182014-07-23 13:40:49 +0000812 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000813 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
814 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000815 for(ii=db->nDb-1; ii>=0; ii--){
816 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000817 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000818 }
819 }
dan5d8a1372013-03-19 19:28:06 +0000820 }
drh9b4c59f2013-04-15 17:03:42 +0000821 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000822 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000823#else
dan3719f5f2013-05-23 10:13:18 +0000824 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000825 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000826#endif
dan3719f5f2013-05-23 10:13:18 +0000827 if( rc==SQLITE_OK ){
drh7cc023c2015-09-03 04:28:25 +0000828 returnSingleInt(v, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +0000829 }else if( rc!=SQLITE_NOTFOUND ){
830 pParse->nErr++;
831 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000832 }
drh9ccd8652013-09-13 16:36:46 +0000833 break;
834 }
dan5d8a1372013-03-19 19:28:06 +0000835
836 /*
drh90f5ecb2004-07-22 01:19:35 +0000837 ** PRAGMA temp_store
838 ** PRAGMA temp_store = "default"|"memory"|"file"
839 **
840 ** Return or set the local value of the temp_store flag. Changing
841 ** the local value does not make changes to the disk file and the default
842 ** value will be restored the next time the database is opened.
843 **
844 ** Note that it is possible for the library compile-time options to
845 ** override this setting
846 */
drh9ccd8652013-09-13 16:36:46 +0000847 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000848 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000849 returnSingleInt(v, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000850 }else{
851 changeTempStorage(pParse, zRight);
852 }
drh9ccd8652013-09-13 16:36:46 +0000853 break;
854 }
drh90f5ecb2004-07-22 01:19:35 +0000855
856 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000857 ** PRAGMA temp_store_directory
858 ** PRAGMA temp_store_directory = ""|"directory_name"
859 **
860 ** Return or set the local value of the temp_store_directory flag. Changing
861 ** the value sets a specific directory to be used for temporary files.
862 ** Setting to a null string reverts to the default temporary directory search.
863 ** If temporary directory is changed, then invalidateTempStorage.
864 **
865 */
drh9ccd8652013-09-13 16:36:46 +0000866 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000867 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000868 returnSingleText(v, "temp_store_directory", sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000869 }else{
drh78f82d12008-09-02 00:52:52 +0000870#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000871 if( zRight[0] ){
872 int res;
danielk1977fab11272008-09-16 14:38:02 +0000873 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
874 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000875 sqlite3ErrorMsg(pParse, "not a writable directory");
876 goto pragma_out;
877 }
drh268283b2005-01-08 15:44:25 +0000878 }
danielk1977b06a0b62008-06-26 10:54:12 +0000879 if( SQLITE_TEMP_STORE==0
880 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
881 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000882 ){
883 invalidateTempStorage(pParse);
884 }
drh17435752007-08-16 04:30:38 +0000885 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000886 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000887 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000888 }else{
drh268283b2005-01-08 15:44:25 +0000889 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000890 }
drh78f82d12008-09-02 00:52:52 +0000891#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000892 }
drh9ccd8652013-09-13 16:36:46 +0000893 break;
894 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000895
drhcc716452012-06-06 23:23:23 +0000896#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000897 /*
898 ** PRAGMA data_store_directory
899 ** PRAGMA data_store_directory = ""|"directory_name"
900 **
901 ** Return or set the local value of the data_store_directory flag. Changing
902 ** the value sets a specific directory to be used for database files that
903 ** were specified with a relative pathname. Setting to a null string reverts
904 ** to the default database directory, which for database files specified with
905 ** a relative path will probably be based on the current directory for the
906 ** process. Database file specified with an absolute path are not impacted
907 ** by this setting, regardless of its value.
908 **
909 */
drh9ccd8652013-09-13 16:36:46 +0000910 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +0000911 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000912 returnSingleText(v, "data_store_directory", sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +0000913 }else{
914#ifndef SQLITE_OMIT_WSD
915 if( zRight[0] ){
916 int res;
917 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
918 if( rc!=SQLITE_OK || res==0 ){
919 sqlite3ErrorMsg(pParse, "not a writable directory");
920 goto pragma_out;
921 }
922 }
923 sqlite3_free(sqlite3_data_directory);
924 if( zRight[0] ){
925 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
926 }else{
927 sqlite3_data_directory = 0;
928 }
929#endif /* SQLITE_OMIT_WSD */
930 }
drh9ccd8652013-09-13 16:36:46 +0000931 break;
932 }
drhcc716452012-06-06 23:23:23 +0000933#endif
mistachkina112d142012-03-14 00:44:01 +0000934
drhd2cb50b2009-01-09 21:41:17 +0000935#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000936 /*
drh9b0cf342015-11-12 14:57:19 +0000937 ** PRAGMA [schema.]lock_proxy_file
938 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +0000939 **
940 ** Return or set the value of the lock_proxy_file flag. Changing
941 ** the value sets a specific file to be used for database access locks.
942 **
943 */
944 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +0000945 if( !zRight ){
946 Pager *pPager = sqlite3BtreePager(pDb->pBt);
947 char *proxy_file_path = NULL;
948 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000949 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000950 &proxy_file_path);
drh7cc023c2015-09-03 04:28:25 +0000951 returnSingleText(v, "lock_proxy_file", proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +0000952 }else{
953 Pager *pPager = sqlite3BtreePager(pDb->pBt);
954 sqlite3_file *pFile = sqlite3PagerFile(pPager);
955 int res;
956 if( zRight[0] ){
957 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
958 zRight);
959 } else {
960 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
961 NULL);
962 }
963 if( res!=SQLITE_OK ){
964 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
965 goto pragma_out;
966 }
967 }
drh9ccd8652013-09-13 16:36:46 +0000968 break;
969 }
drhd2cb50b2009-01-09 21:41:17 +0000970#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000971
972 /*
drh9b0cf342015-11-12 14:57:19 +0000973 ** PRAGMA [schema.]synchronous
974 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000975 **
976 ** Return or set the local value of the synchronous flag. Changing
977 ** the local value does not make changes to the disk file and the
978 ** default value will be restored the next time the database is
979 ** opened.
980 */
drh9ccd8652013-09-13 16:36:46 +0000981 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +0000982 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000983 returnSingleInt(v, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000984 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000985 if( !db->autoCommit ){
986 sqlite3ErrorMsg(pParse,
987 "Safety level may not be changed inside a transaction");
988 }else{
drhd99d2832015-04-17 15:58:33 +0000989 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
990 if( iLevel==0 ) iLevel = 1;
991 pDb->safety_level = iLevel;
drhd3605a42013-08-17 15:42:29 +0000992 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +0000993 }
drhc11d4f92003-04-06 21:08:24 +0000994 }
drh9ccd8652013-09-13 16:36:46 +0000995 break;
996 }
drh13d70422004-11-13 15:59:14 +0000997#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000998
drhbf216272005-02-26 18:10:44 +0000999#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001000 case PragTyp_FLAG: {
1001 if( zRight==0 ){
drh7cc023c2015-09-03 04:28:25 +00001002 returnSingleInt(v, pPragma->zName, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001003 }else{
drhc228be52015-01-31 02:00:01 +00001004 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001005 if( db->autoCommit==0 ){
1006 /* Foreign key support may not be enabled or disabled while not
1007 ** in auto-commit mode. */
1008 mask &= ~(SQLITE_ForeignKeys);
1009 }
drhd4530972014-09-09 14:47:53 +00001010#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001011 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001012 /* Do not allow non-admin users to modify the schema arbitrarily */
1013 mask &= ~(SQLITE_WriteSchema);
1014 }
1015#endif
drh9ccd8652013-09-13 16:36:46 +00001016
1017 if( sqlite3GetBoolean(zRight, 0) ){
1018 db->flags |= mask;
1019 }else{
1020 db->flags &= ~mask;
1021 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1022 }
1023
1024 /* Many of the flag-pragmas modify the code generated by the SQL
1025 ** compiler (eg. count_changes). So add an opcode to expire all
1026 ** compiled SQL statements after modifying a pragma value.
1027 */
1028 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
1029 setAllPagerFlags(db);
1030 }
1031 break;
1032 }
drhbf216272005-02-26 18:10:44 +00001033#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001034
drh13d70422004-11-13 15:59:14 +00001035#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001036 /*
1037 ** PRAGMA table_info(<table>)
1038 **
1039 ** Return a single row for each column of the named table. The columns of
1040 ** the returned data set are:
1041 **
1042 ** cid: Column id (numbered from left to right, starting at 0)
1043 ** name: Column name
1044 ** type: Column declaration type.
1045 ** notnull: True if 'NOT NULL' is part of column declaration
1046 ** dflt_value: The default value for the column, if any.
1047 */
drh9ccd8652013-09-13 16:36:46 +00001048 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001049 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001050 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001051 if( pTab ){
drhb460e522015-09-03 03:29:51 +00001052 static const char *azCol[] = {
1053 "cid", "name", "type", "notnull", "dflt_value", "pk"
1054 };
drh384b7fe2013-01-01 13:55:31 +00001055 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001056 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001057 Column *pCol;
drh44156282013-10-23 22:23:03 +00001058 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001059 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001060 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001061 setAllColumnNames(v, 6, azCol); assert( 6==ArraySize(azCol) );
danielk19774adee202004-05-08 08:23:19 +00001062 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001063 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001064 if( IsHiddenColumn(pCol) ){
1065 nHidden++;
1066 continue;
1067 }
drh384b7fe2013-01-01 13:55:31 +00001068 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1069 k = 0;
1070 }else if( pPk==0 ){
1071 k = 1;
1072 }else{
drh1b678962015-04-15 07:19:27 +00001073 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001074 }
drh076e85f2015-09-03 13:46:12 +00001075 sqlite3VdbeMultiLoad(v, 1, "issisi",
1076 i-nHidden,
1077 pCol->zName,
1078 pCol->zType ? pCol->zType : "",
1079 pCol->notNull ? 1 : 0,
1080 pCol->zDflt,
1081 k);
drh2d401ab2008-01-10 23:50:11 +00001082 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001083 }
1084 }
drh9ccd8652013-09-13 16:36:46 +00001085 }
1086 break;
drhc11d4f92003-04-06 21:08:24 +00001087
drh3ef26152013-10-12 20:22:00 +00001088 case PragTyp_STATS: {
drhb460e522015-09-03 03:29:51 +00001089 static const char *azCol[] = { "table", "index", "width", "height" };
drh3ef26152013-10-12 20:22:00 +00001090 Index *pIdx;
1091 HashElem *i;
1092 v = sqlite3GetVdbe(pParse);
drh3ef26152013-10-12 20:22:00 +00001093 pParse->nMem = 4;
1094 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001095 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
drh3ef26152013-10-12 20:22:00 +00001096 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1097 Table *pTab = sqliteHashData(i);
drh076e85f2015-09-03 13:46:12 +00001098 sqlite3VdbeMultiLoad(v, 1, "ssii",
1099 pTab->zName,
1100 0,
1101 (int)sqlite3LogEstToInt(pTab->szTabRow),
1102 (int)sqlite3LogEstToInt(pTab->nRowLogEst));
drh3ef26152013-10-12 20:22:00 +00001103 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1104 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh076e85f2015-09-03 13:46:12 +00001105 sqlite3VdbeMultiLoad(v, 2, "sii",
1106 pIdx->zName,
1107 (int)sqlite3LogEstToInt(pIdx->szIdxRow),
1108 (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]));
drh3ef26152013-10-12 20:22:00 +00001109 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1110 }
1111 }
1112 }
1113 break;
1114
drh9ccd8652013-09-13 16:36:46 +00001115 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001116 Index *pIdx;
1117 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001118 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001119 if( pIdx ){
drhb460e522015-09-03 03:29:51 +00001120 static const char *azCol[] = {
1121 "seqno", "cid", "name", "desc", "coll", "key"
1122 };
drhc11d4f92003-04-06 21:08:24 +00001123 int i;
drh5e7028c2015-03-05 14:29:02 +00001124 int mx;
1125 if( pPragma->iArg ){
1126 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1127 mx = pIdx->nColumn;
1128 pParse->nMem = 6;
1129 }else{
1130 /* PRAGMA index_info (legacy version) */
1131 mx = pIdx->nKeyCol;
1132 pParse->nMem = 3;
1133 }
drhc11d4f92003-04-06 21:08:24 +00001134 pTab = pIdx->pTable;
drhc95e01d2013-02-14 16:16:05 +00001135 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001136 assert( pParse->nMem<=ArraySize(azCol) );
drhb460e522015-09-03 03:29:51 +00001137 setAllColumnNames(v, pParse->nMem, azCol);
drhc228be52015-01-31 02:00:01 +00001138 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001139 i16 cnum = pIdx->aiColumn[i];
drh076e85f2015-09-03 13:46:12 +00001140 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum,
1141 cnum<0 ? 0 : pTab->aCol[cnum].zName);
drh5e7028c2015-03-05 14:29:02 +00001142 if( pPragma->iArg ){
drh076e85f2015-09-03 13:46:12 +00001143 sqlite3VdbeMultiLoad(v, 4, "isi",
1144 pIdx->aSortOrder[i],
1145 pIdx->azColl[i],
1146 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001147 }
1148 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001149 }
1150 }
drh9ccd8652013-09-13 16:36:46 +00001151 }
1152 break;
drhc11d4f92003-04-06 21:08:24 +00001153
drh9ccd8652013-09-13 16:36:46 +00001154 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001155 Index *pIdx;
1156 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001157 int i;
drh2783e4b2004-10-05 15:42:53 +00001158 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001159 if( pTab ){
drhb460e522015-09-03 03:29:51 +00001160 static const char *azCol[] = {
1161 "seq", "name", "unique", "origin", "partial"
1162 };
danielk19774adee202004-05-08 08:23:19 +00001163 v = sqlite3GetVdbe(pParse);
drhc228be52015-01-31 02:00:01 +00001164 pParse->nMem = 5;
drhe13e9f52013-10-05 19:18:00 +00001165 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001166 setAllColumnNames(v, 5, azCol); assert( 5==ArraySize(azCol) );
drh3ef26152013-10-12 20:22:00 +00001167 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001168 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001169 sqlite3VdbeMultiLoad(v, 1, "isisi",
1170 i,
1171 pIdx->zName,
1172 IsUniqueIndex(pIdx),
1173 azOrigin[pIdx->idxType],
1174 pIdx->pPartIdxWhere!=0);
drhc228be52015-01-31 02:00:01 +00001175 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drhc11d4f92003-04-06 21:08:24 +00001176 }
1177 }
drh9ccd8652013-09-13 16:36:46 +00001178 }
1179 break;
drhc11d4f92003-04-06 21:08:24 +00001180
drh9ccd8652013-09-13 16:36:46 +00001181 case PragTyp_DATABASE_LIST: {
drhb460e522015-09-03 03:29:51 +00001182 static const char *azCol[] = { "seq", "name", "file" };
drh13d70422004-11-13 15:59:14 +00001183 int i;
drh2d401ab2008-01-10 23:50:11 +00001184 pParse->nMem = 3;
drh076e85f2015-09-03 13:46:12 +00001185 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
drh13d70422004-11-13 15:59:14 +00001186 for(i=0; i<db->nDb; i++){
1187 if( db->aDb[i].pBt==0 ) continue;
1188 assert( db->aDb[i].zName!=0 );
drh076e85f2015-09-03 13:46:12 +00001189 sqlite3VdbeMultiLoad(v, 1, "iss",
1190 i,
1191 db->aDb[i].zName,
1192 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh2d401ab2008-01-10 23:50:11 +00001193 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001194 }
drh9ccd8652013-09-13 16:36:46 +00001195 }
1196 break;
danielk197748af65a2005-02-09 03:20:37 +00001197
drh9ccd8652013-09-13 16:36:46 +00001198 case PragTyp_COLLATION_LIST: {
drhb460e522015-09-03 03:29:51 +00001199 static const char *azCol[] = { "seq", "name" };
danielk197748af65a2005-02-09 03:20:37 +00001200 int i = 0;
1201 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001202 pParse->nMem = 2;
drh076e85f2015-09-03 13:46:12 +00001203 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
danielk197748af65a2005-02-09 03:20:37 +00001204 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1205 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001206 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
drh2d401ab2008-01-10 23:50:11 +00001207 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001208 }
drh9ccd8652013-09-13 16:36:46 +00001209 }
1210 break;
drh13d70422004-11-13 15:59:14 +00001211#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1212
drhb7f91642004-10-31 02:22:47 +00001213#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001214 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001215 FKey *pFK;
1216 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001217 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001218 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001219 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001220 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001221 if( pFK ){
drhb460e522015-09-03 03:29:51 +00001222 static const char *azCol[] = {
1223 "id", "seq", "table", "from", "to", "on_update", "on_delete",
1224 "match"
1225 };
danielk1977742f9472004-06-16 12:02:43 +00001226 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001227 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001228 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001229 setAllColumnNames(v, 8, azCol); assert( 8==ArraySize(azCol) );
danielk1977742f9472004-06-16 12:02:43 +00001230 while(pFK){
1231 int j;
1232 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001233 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1234 i,
1235 j,
1236 pFK->zTo,
1237 pTab->aCol[pFK->aCol[j].iFrom].zName,
1238 pFK->aCol[j].zCol,
1239 actionName(pFK->aAction[1]), /* ON UPDATE */
1240 actionName(pFK->aAction[0]), /* ON DELETE */
1241 "NONE");
danielk197750af3e12008-10-10 17:47:21 +00001242 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001243 }
1244 ++i;
1245 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001246 }
drh78100cc2003-08-23 22:40:53 +00001247 }
1248 }
drh9ccd8652013-09-13 16:36:46 +00001249 }
1250 break;
drhb7f91642004-10-31 02:22:47 +00001251#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001252
drh6c5b9152012-12-17 16:46:37 +00001253#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001254#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001255 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001256 FKey *pFK; /* A foreign key constraint */
1257 Table *pTab; /* Child table contain "REFERENCES" keyword */
1258 Table *pParent; /* Parent table that child points to */
1259 Index *pIdx; /* Index in the parent table */
1260 int i; /* Loop counter: Foreign key number for pTab */
1261 int j; /* Loop counter: Field of the foreign key */
1262 HashElem *k; /* Loop counter: Next table in schema */
1263 int x; /* result variable */
1264 int regResult; /* 3 registers to hold a result row */
1265 int regKey; /* Register to hold key for checking the FK */
1266 int regRow; /* Registers to hold a row from pTab */
1267 int addrTop; /* Top of a loop checking foreign keys */
1268 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001269 int *aiCols; /* child to parent column mapping */
drhb460e522015-09-03 03:29:51 +00001270 static const char *azCol[] = { "table", "rowid", "parent", "fkid" };
drh6c5b9152012-12-17 16:46:37 +00001271
drh613028b2012-12-17 18:43:02 +00001272 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001273 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001274 regKey = ++pParse->nMem;
1275 regRow = ++pParse->nMem;
1276 v = sqlite3GetVdbe(pParse);
drh076e85f2015-09-03 13:46:12 +00001277 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
drh613028b2012-12-17 18:43:02 +00001278 sqlite3CodeVerifySchema(pParse, iDb);
1279 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1280 while( k ){
1281 if( zRight ){
1282 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1283 k = 0;
1284 }else{
1285 pTab = (Table*)sqliteHashData(k);
1286 k = sqliteHashNext(k);
1287 }
drh9148def2012-12-17 20:40:39 +00001288 if( pTab==0 || pTab->pFKey==0 ) continue;
1289 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001290 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001291 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001292 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh6c5b9152012-12-17 16:46:37 +00001293 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001294 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1295 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001296 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001297 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001298 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001299 if( x==0 ){
1300 if( pIdx==0 ){
1301 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1302 }else{
drh6c5b9152012-12-17 16:46:37 +00001303 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001304 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001305 }
1306 }else{
drh613028b2012-12-17 18:43:02 +00001307 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001308 break;
1309 }
drh6c5b9152012-12-17 16:46:37 +00001310 }
dan5e878302013-10-12 19:06:48 +00001311 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001312 if( pFK ) break;
1313 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001314 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001315 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001316 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001317 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001318 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001319 if( pParent ){
1320 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1321 assert( x==0 );
1322 }
drh613028b2012-12-17 18:43:02 +00001323 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001324 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001325 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001326 assert( iKey>=0 && iKey<pTab->nCol );
1327 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001328 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1329 sqlite3ColumnDefault(v, pTab, iKey, regRow);
drh688852a2014-02-17 22:40:43 +00001330 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
1331 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1332 sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001333 }else{
drh613028b2012-12-17 18:43:02 +00001334 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001335 }
drh688852a2014-02-17 22:40:43 +00001336 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001337 sqlite3VdbeGoto(v, addrOk);
drh613028b2012-12-17 18:43:02 +00001338 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1339 }else{
1340 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001341 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001342 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh688852a2014-02-17 22:40:43 +00001343 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001344 }
dan5e878302013-10-12 19:06:48 +00001345 if( pParent ){
drh57bf4a82014-02-17 14:59:22 +00001346 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
drhe9107692015-08-25 19:20:04 +00001347 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
dan5e878302013-10-12 19:06:48 +00001348 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh688852a2014-02-17 22:40:43 +00001349 VdbeCoverage(v);
dan5e878302013-10-12 19:06:48 +00001350 }
drh6c5b9152012-12-17 16:46:37 +00001351 }
drh613028b2012-12-17 18:43:02 +00001352 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh076e85f2015-09-03 13:46:12 +00001353 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001354 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001355 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001356 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001357 }
drh688852a2014-02-17 22:40:43 +00001358 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001359 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001360 }
drh9ccd8652013-09-13 16:36:46 +00001361 }
1362 break;
dan09ff9e12013-03-11 11:49:03 +00001363#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001364#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1365
drhc11d4f92003-04-06 21:08:24 +00001366#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001367 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001368 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001369 if( sqlite3GetBoolean(zRight, 0) ){
drh97e58a22015-11-10 14:27:17 +00001370 sqlite3ParserTrace(stdout, "parser: ");
drh55ef4d92005-08-14 01:20:37 +00001371 }else{
1372 sqlite3ParserTrace(0, 0);
1373 }
drhc11d4f92003-04-06 21:08:24 +00001374 }
drh9ccd8652013-09-13 16:36:46 +00001375 }
1376 break;
drhc11d4f92003-04-06 21:08:24 +00001377#endif
1378
drh55ef4d92005-08-14 01:20:37 +00001379 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1380 ** used will be case sensitive or not depending on the RHS.
1381 */
drh9ccd8652013-09-13 16:36:46 +00001382 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001383 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001384 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001385 }
drh9ccd8652013-09-13 16:36:46 +00001386 }
1387 break;
drh55ef4d92005-08-14 01:20:37 +00001388
drh1dcdbc02007-01-27 02:24:54 +00001389#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1390# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1391#endif
1392
drhb7f91642004-10-31 02:22:47 +00001393#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001394 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001395 ** integrity_check designed to detect most database corruption
1396 ** without most of the overhead of a full integrity-check.
1397 */
drh9ccd8652013-09-13 16:36:46 +00001398 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001399 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001400
drhed717fe2003-06-15 23:42:24 +00001401 /* Code that appears at the end of the integrity check. If no error
1402 ** messages have been generated, output OK. Otherwise output the
1403 ** error message
1404 */
drhb06a4ec2014-03-10 18:03:09 +00001405 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +00001406 static const VdbeOpList endCode[] = {
drh8b0cf382015-10-06 21:07:06 +00001407 { OP_AddImm, 1, 0, 0}, /* 0 */
1408 { OP_If, 1, 0, 0}, /* 1 */
1409 { OP_String8, 0, 3, 0}, /* 2 */
drh2d401ab2008-01-10 23:50:11 +00001410 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001411 };
drhed717fe2003-06-15 23:42:24 +00001412
drhc5227312011-10-13 17:09:01 +00001413 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001414
dan5885e762012-07-16 10:06:12 +00001415 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1416 ** then iDb is set to the index of the database identified by <db>.
1417 ** In this case, the integrity of database iDb only is verified by
1418 ** the VDBE created below.
1419 **
1420 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1421 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1422 ** to -1 here, to indicate that the VDBE should verify the integrity
1423 ** of all attached databases. */
1424 assert( iDb>=0 );
1425 assert( iDb==0 || pId2->z );
1426 if( pId2->z==0 ) iDb = -1;
1427
drhed717fe2003-06-15 23:42:24 +00001428 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001429 pParse->nMem = 6;
drhb460e522015-09-03 03:29:51 +00001430 setOneColumnName(v, "integrity_check");
drh1dcdbc02007-01-27 02:24:54 +00001431
1432 /* Set the maximum error count */
1433 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1434 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001435 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001436 if( mxErr<=0 ){
1437 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1438 }
1439 }
drh2d401ab2008-01-10 23:50:11 +00001440 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001441
1442 /* Do an integrity check on each database file */
1443 for(i=0; i<db->nDb; i++){
1444 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001445 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001446 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001447
danielk197753c0f742005-03-29 03:10:59 +00001448 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001449 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001450
drh80242052004-06-09 00:48:12 +00001451 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001452 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh688852a2014-02-17 22:40:43 +00001453 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001454 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001455 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001456
drhed717fe2003-06-15 23:42:24 +00001457 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001458 **
1459 ** Begin by filling registers 2, 3, ... with the root pages numbers
1460 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001461 */
dan5885e762012-07-16 10:06:12 +00001462 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001463 pTbls = &db->aDb[i].pSchema->tblHash;
1464 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001465 Table *pTab = sqliteHashData(x);
1466 Index *pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001467 if( HasRowid(pTab) ){
1468 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001469 VdbeComment((v, "%s", pTab->zName));
drh6fbe41a2013-10-30 20:22:55 +00001470 cnt++;
1471 }
drh79069752004-05-22 21:30:40 +00001472 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001473 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001474 VdbeComment((v, "%s", pIdx->zName));
drh79069752004-05-22 21:30:40 +00001475 cnt++;
1476 }
1477 }
drh2d401ab2008-01-10 23:50:11 +00001478
1479 /* Make sure sufficient number of registers have been allocated */
drh6fbe41a2013-10-30 20:22:55 +00001480 pParse->nMem = MAX( pParse->nMem, cnt+8 );
drh2d401ab2008-01-10 23:50:11 +00001481
1482 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001483 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001484 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001485 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001486 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001487 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001488 P4_DYNAMIC);
drh079a3072014-03-19 14:10:55 +00001489 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001490 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001491 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001492 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001493
1494 /* Make sure all the indices are constructed correctly.
1495 */
danielk197741c58b72007-12-29 13:39:19 +00001496 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001497 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001498 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001499 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001500 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001501 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001502 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001503
1504 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001505 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001506 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh688852a2014-02-17 22:40:43 +00001507 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001508 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001509 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001510 sqlite3ExprCacheClear(pParse);
danfd261ec2015-10-22 20:54:33 +00001511 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001512 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001513 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001514 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001515 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001516 }
drh6fbe41a2013-10-30 20:22:55 +00001517 pParse->nMem = MAX(pParse->nMem, 8+j);
drh688852a2014-02-17 22:40:43 +00001518 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001519 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhcefc87f2014-08-01 01:40:33 +00001520 /* Verify that all NOT NULL columns really are NOT NULL */
1521 for(j=0; j<pTab->nCol; j++){
1522 char *zErr;
1523 int jmp2, jmp3;
1524 if( j==pTab->iPKey ) continue;
1525 if( pTab->aCol[j].notNull==0 ) continue;
1526 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1527 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1528 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1529 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1530 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1531 pTab->aCol[j].zName);
1532 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1533 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1534 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1535 sqlite3VdbeAddOp0(v, OP_Halt);
1536 sqlite3VdbeJumpHere(v, jmp2);
1537 sqlite3VdbeJumpHere(v, jmp3);
1538 }
1539 /* Validate index entries for the current row */
drhed717fe2003-06-15 23:42:24 +00001540 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhcefc87f2014-08-01 01:40:33 +00001541 int jmp2, jmp3, jmp4, jmp5;
1542 int ckUniq = sqlite3VdbeMakeLabel(v);
drh6fbe41a2013-10-30 20:22:55 +00001543 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001544 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1545 pPrior, r1);
1546 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001547 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drhcefc87f2014-08-01 01:40:33 +00001548 /* Verify that an index entry exists for the current table row */
1549 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
drh688852a2014-02-17 22:40:43 +00001550 pIdx->nColumn); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001551 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001552 sqlite3VdbeLoadString(v, 3, "row ");
drh6fbe41a2013-10-30 20:22:55 +00001553 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001554 sqlite3VdbeLoadString(v, 4, " missing from index ");
drh6fbe41a2013-10-30 20:22:55 +00001555 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001556 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
drh6fbe41a2013-10-30 20:22:55 +00001557 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1558 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh688852a2014-02-17 22:40:43 +00001559 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001560 sqlite3VdbeAddOp0(v, OP_Halt);
drhd654be82005-09-20 17:42:23 +00001561 sqlite3VdbeJumpHere(v, jmp2);
drhcefc87f2014-08-01 01:40:33 +00001562 /* For UNIQUE indexes, verify that only one entry exists with the
1563 ** current key. The entry is unique if (1) any column is NULL
1564 ** or (2) the next entry has a different key */
1565 if( IsUniqueIndex(pIdx) ){
1566 int uniqOk = sqlite3VdbeMakeLabel(v);
1567 int jmp6;
1568 int kk;
1569 for(kk=0; kk<pIdx->nKeyCol; kk++){
1570 int iCol = pIdx->aiColumn[kk];
drh4b92f982015-09-29 17:20:14 +00001571 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
drh68391ac2015-09-25 20:49:16 +00001572 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
drhcefc87f2014-08-01 01:40:33 +00001573 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1574 VdbeCoverage(v);
1575 }
1576 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001577 sqlite3VdbeGoto(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00001578 sqlite3VdbeJumpHere(v, jmp6);
1579 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1580 pIdx->nKeyCol); VdbeCoverage(v);
1581 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001582 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1583 sqlite3VdbeGoto(v, jmp5);
drhcefc87f2014-08-01 01:40:33 +00001584 sqlite3VdbeResolveLabel(v, uniqOk);
1585 }
1586 sqlite3VdbeJumpHere(v, jmp4);
drh87744512014-04-13 19:15:49 +00001587 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhed717fe2003-06-15 23:42:24 +00001588 }
drh688852a2014-02-17 22:40:43 +00001589 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001590 sqlite3VdbeJumpHere(v, loopTop-1);
1591#ifndef SQLITE_OMIT_BTREECOUNT
drh076e85f2015-09-03 13:46:12 +00001592 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
drh8a9789b2013-08-01 03:36:59 +00001593 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001594 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001595 addr = sqlite3VdbeCurrentAddr(v);
drh688852a2014-02-17 22:40:43 +00001596 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001597 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001598 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh688852a2014-02-17 22:40:43 +00001599 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
drh5655c542014-02-19 19:14:34 +00001600 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh8a9789b2013-08-01 03:36:59 +00001601 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh076e85f2015-09-03 13:46:12 +00001602 sqlite3VdbeLoadString(v, 3, pIdx->zName);
drh8a9789b2013-08-01 03:36:59 +00001603 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1604 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001605 }
drh8a9789b2013-08-01 03:36:59 +00001606#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001607 }
1608 }
drh688852a2014-02-17 22:40:43 +00001609 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
drh8b0cf382015-10-06 21:07:06 +00001610 sqlite3VdbeChangeP2(v, addr, -mxErr);
1611 sqlite3VdbeJumpHere(v, addr+1);
1612 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drh9ccd8652013-09-13 16:36:46 +00001613 }
1614 break;
drhb7f91642004-10-31 02:22:47 +00001615#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1616
drh13d70422004-11-13 15:59:14 +00001617#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001618 /*
1619 ** PRAGMA encoding
1620 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1621 **
drh85b623f2007-12-13 21:54:09 +00001622 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001623 ** database. If the database is not initialized, it is initialized now.
1624 **
1625 ** The second form of this pragma is a no-op if the main database file
1626 ** has not already been initialized. In this case it sets the default
1627 ** encoding that will be used for the main database file if a new file
1628 ** is created. If an existing main database file is opened, then the
1629 ** default text encoding for the existing database is used.
1630 **
1631 ** In all cases new databases created using the ATTACH command are
1632 ** created to use the same default text encoding as the main database. If
1633 ** the main database has not been initialized and/or created when ATTACH
1634 ** is executed, this is done before the ATTACH operation.
1635 **
1636 ** In the second form this pragma sets the text encoding to be used in
1637 ** new database files created using this database handle. It is only
1638 ** useful if invoked immediately after the main database i
1639 */
drh9ccd8652013-09-13 16:36:46 +00001640 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001641 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001642 char *zName;
1643 u8 enc;
1644 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001645 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001646 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1647 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1648 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001649 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001650 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001651 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1652 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001653 { 0, 0 }
1654 };
drh0f7eb612006-08-08 13:51:43 +00001655 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001656 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001657 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00001658 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1659 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1660 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drh7cc023c2015-09-03 04:28:25 +00001661 returnSingleText(v, "encoding", encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00001662 }else{ /* "PRAGMA encoding = XXX" */
1663 /* Only change the value of sqlite.enc if the database handle is not
1664 ** initialized. If the main database exists, the new sqlite.enc value
1665 ** will be overwritten when the schema is next loaded. If it does not
1666 ** already exists, it will be created to use the new encoding value.
1667 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001668 if(
1669 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1670 DbHasProperty(db, 0, DB_Empty)
1671 ){
danielk19778e227872004-06-07 07:52:17 +00001672 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1673 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh9bd3cc42014-12-12 23:17:54 +00001674 SCHEMA_ENC(db) = ENC(db) =
1675 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001676 break;
1677 }
1678 }
1679 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001680 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001681 }
1682 }
1683 }
drh9ccd8652013-09-13 16:36:46 +00001684 }
1685 break;
drh13d70422004-11-13 15:59:14 +00001686#endif /* SQLITE_OMIT_UTF16 */
1687
1688#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001689 /*
drh9b0cf342015-11-12 14:57:19 +00001690 ** PRAGMA [schema.]schema_version
1691 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001692 **
drh9b0cf342015-11-12 14:57:19 +00001693 ** PRAGMA [schema.]user_version
1694 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001695 **
drh9b0cf342015-11-12 14:57:19 +00001696 ** PRAGMA [schema.]freelist_count = <integer>
drh4ee09b42013-05-01 19:49:27 +00001697 **
drh9b0cf342015-11-12 14:57:19 +00001698 ** PRAGMA [schema.]application_id
1699 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00001700 **
danielk1977b92b70b2004-11-12 16:11:59 +00001701 ** The pragma's schema_version and user_version are used to set or get
1702 ** the value of the schema-version and user-version, respectively. Both
1703 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001704 ** stored in the database header.
1705 **
1706 ** The schema-cookie is usually only manipulated internally by SQLite. It
1707 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001708 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001709 ** SQLite each time a query is executed to ensure that the internal cache
1710 ** of the schema used when compiling the SQL query matches the schema of
1711 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001712 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1713 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001714 ** crashes or database corruption. Use with caution!
1715 **
danielk1977b92b70b2004-11-12 16:11:59 +00001716 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001717 ** applications for any purpose.
1718 */
drh9ccd8652013-09-13 16:36:46 +00001719 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00001720 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00001721 sqlite3VdbeUsesBtree(v, iDb);
drhc228be52015-01-31 02:00:01 +00001722 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00001723 /* Write the specified cookie value */
1724 static const VdbeOpList setCookie[] = {
1725 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001726 { OP_Integer, 0, 1, 0}, /* 1 */
1727 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001728 };
drh688852a2014-02-17 22:40:43 +00001729 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00001730 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00001731 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00001732 sqlite3VdbeChangeP1(v, addr+2, iDb);
1733 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1734 }else{
1735 /* Read the specified cookie value */
1736 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001737 { OP_Transaction, 0, 0, 0}, /* 0 */
1738 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001739 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001740 };
drh688852a2014-02-17 22:40:43 +00001741 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00001742 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001743 sqlite3VdbeChangeP1(v, addr+1, iDb);
1744 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001745 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001746 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001747 }
drh9ccd8652013-09-13 16:36:46 +00001748 }
1749 break;
drh13d70422004-11-13 15:59:14 +00001750#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001751
shanehdc97a8c2010-02-23 20:08:35 +00001752#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1753 /*
1754 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001755 **
drh71caabf2010-02-26 15:39:24 +00001756 ** Return the names of all compile-time options used in this build,
1757 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001758 */
drh9ccd8652013-09-13 16:36:46 +00001759 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00001760 int i = 0;
1761 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00001762 pParse->nMem = 1;
drhb460e522015-09-03 03:29:51 +00001763 setOneColumnName(v, "compile_option");
shanehdc97a8c2010-02-23 20:08:35 +00001764 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00001765 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00001766 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1767 }
drh9ccd8652013-09-13 16:36:46 +00001768 }
1769 break;
shanehdc97a8c2010-02-23 20:08:35 +00001770#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1771
dan5cf53532010-05-01 16:40:20 +00001772#ifndef SQLITE_OMIT_WAL
1773 /*
drh9b0cf342015-11-12 14:57:19 +00001774 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00001775 **
1776 ** Checkpoint the database.
1777 */
drh9ccd8652013-09-13 16:36:46 +00001778 case PragTyp_WAL_CHECKPOINT: {
drhb460e522015-09-03 03:29:51 +00001779 static const char *azCol[] = { "busy", "log", "checkpointed" };
dana58f26f2010-11-16 18:56:51 +00001780 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001781 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1782 if( zRight ){
1783 if( sqlite3StrICmp(zRight, "full")==0 ){
1784 eMode = SQLITE_CHECKPOINT_FULL;
1785 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1786 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00001787 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1788 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00001789 }
1790 }
drh076e85f2015-09-03 13:46:12 +00001791 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
dancdc1f042010-11-18 12:11:05 +00001792 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00001793 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001794 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00001795 }
1796 break;
dan5a299f92010-05-03 11:05:08 +00001797
1798 /*
1799 ** PRAGMA wal_autocheckpoint
1800 ** PRAGMA wal_autocheckpoint = N
1801 **
1802 ** Configure a database connection to automatically checkpoint a database
1803 ** after accumulating N frames in the log. Or query for the current value
1804 ** of N.
1805 */
drh9ccd8652013-09-13 16:36:46 +00001806 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00001807 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001808 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001809 }
drh7cc023c2015-09-03 04:28:25 +00001810 returnSingleInt(v, "wal_autocheckpoint",
drhb033d8b2010-05-03 13:37:30 +00001811 db->xWalCallback==sqlite3WalDefaultHook ?
1812 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00001813 }
1814 break;
dan5cf53532010-05-01 16:40:20 +00001815#endif
dan7c246102010-04-12 19:00:29 +00001816
drh09419b42011-11-16 19:29:17 +00001817 /*
1818 ** PRAGMA shrink_memory
1819 **
drh51a74d42015-02-28 01:04:27 +00001820 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
1821 ** connection on which it is invoked to free up as much memory as it
1822 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00001823 */
drh9ccd8652013-09-13 16:36:46 +00001824 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00001825 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00001826 break;
1827 }
drh09419b42011-11-16 19:29:17 +00001828
drhf3603962012-09-07 16:46:59 +00001829 /*
1830 ** PRAGMA busy_timeout
1831 ** PRAGMA busy_timeout = N
1832 **
1833 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001834 ** if one is set. If no busy handler or a different busy handler is set
1835 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1836 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001837 */
drhd49c3582013-09-13 19:00:06 +00001838 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00001839 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00001840 if( zRight ){
1841 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1842 }
drh7cc023c2015-09-03 04:28:25 +00001843 returnSingleInt(v, "timeout", db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00001844 break;
1845 }
drhf3603962012-09-07 16:46:59 +00001846
drh55e85ca2013-09-13 21:01:56 +00001847 /*
1848 ** PRAGMA soft_heap_limit
1849 ** PRAGMA soft_heap_limit = N
1850 **
drh51a74d42015-02-28 01:04:27 +00001851 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
1852 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
1853 ** specified and is a non-negative integer.
1854 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
1855 ** returns the same integer that would be returned by the
1856 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00001857 */
1858 case PragTyp_SOFT_HEAP_LIMIT: {
1859 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00001860 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00001861 sqlite3_soft_heap_limit64(N);
1862 }
drh7cc023c2015-09-03 04:28:25 +00001863 returnSingleInt(v, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00001864 break;
1865 }
1866
drh03459612014-08-25 15:13:22 +00001867 /*
1868 ** PRAGMA threads
1869 ** PRAGMA threads = N
1870 **
1871 ** Configure the maximum number of worker threads. Return the new
1872 ** maximum, which might be less than requested.
1873 */
1874 case PragTyp_THREADS: {
1875 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00001876 if( zRight
drh03459612014-08-25 15:13:22 +00001877 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
1878 && N>=0
1879 ){
drh111544c2014-08-29 16:20:47 +00001880 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00001881 }
drh7cc023c2015-09-03 04:28:25 +00001882 returnSingleInt(v, "threads",
drh111544c2014-08-29 16:20:47 +00001883 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00001884 break;
1885 }
1886
dougcurrie81c95ef2004-06-18 23:21:47 +00001887#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001888 /*
1889 ** Report the current state of file logs for all databases
1890 */
drh9ccd8652013-09-13 16:36:46 +00001891 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00001892 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001893 "unlocked", "shared", "reserved", "pending", "exclusive"
1894 };
drhb460e522015-09-03 03:29:51 +00001895 static const char *azCol[] = { "database", "status" };
drh89ac8c12004-06-09 14:17:20 +00001896 int i;
drh076e85f2015-09-03 13:46:12 +00001897 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
drh2d401ab2008-01-10 23:50:11 +00001898 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00001899 for(i=0; i<db->nDb; i++){
1900 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001901 const char *zState = "unknown";
1902 int j;
drh89ac8c12004-06-09 14:17:20 +00001903 if( db->aDb[i].zName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00001904 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001905 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001906 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001907 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001908 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1909 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001910 }
drh076e85f2015-09-03 13:46:12 +00001911 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zName, zState);
drh2d401ab2008-01-10 23:50:11 +00001912 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001913 }
drh9ccd8652013-09-13 16:36:46 +00001914 break;
1915 }
drh89ac8c12004-06-09 14:17:20 +00001916#endif
1917
shanehad9f9f62010-02-17 17:48:46 +00001918#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00001919 case PragTyp_KEY: {
1920 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1921 break;
1922 }
1923 case PragTyp_REKEY: {
1924 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1925 break;
1926 }
1927 case PragTyp_HEXKEY: {
1928 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00001929 u8 iByte;
1930 int i;
drh9ccd8652013-09-13 16:36:46 +00001931 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00001932 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
1933 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
1934 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00001935 }
1936 if( (zLeft[3] & 0xf)==0xb ){
1937 sqlite3_key_v2(db, zDb, zKey, i/2);
1938 }else{
1939 sqlite3_rekey_v2(db, zDb, zKey, i/2);
1940 }
drhd2cb50b2009-01-09 21:41:17 +00001941 }
drh9ccd8652013-09-13 16:36:46 +00001942 break;
1943 }
drh3c4f2a42005-12-08 18:12:56 +00001944#endif
shanehad9f9f62010-02-17 17:48:46 +00001945#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00001946 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001947#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001948 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001949 sqlite3_activate_see(&zRight[4]);
1950 }
1951#endif
1952#ifdef SQLITE_ENABLE_CEROD
1953 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001954 sqlite3_activate_cerod(&zRight[6]);
1955 }
1956#endif
drh9ccd8652013-09-13 16:36:46 +00001957 }
1958 break;
drh21e2cab2006-09-25 18:01:57 +00001959#endif
drh3c4f2a42005-12-08 18:12:56 +00001960
drh9ccd8652013-09-13 16:36:46 +00001961 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00001962
danielk1977e0048402004-06-15 16:51:01 +00001963pragma_out:
drh633e6d52008-07-28 19:34:53 +00001964 sqlite3DbFree(db, zLeft);
1965 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001966}
drh13d70422004-11-13 15:59:14 +00001967
drh8bfdf722009-06-19 14:06:03 +00001968#endif /* SQLITE_OMIT_PRAGMA */