blob: 0d48057d210818c29d6558c617e10dc93c844b8d [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 ){
drh7cc023c2015-09-03 04:28:25 +0000738 returnSingleInt(v, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000739 }else{
drh3b42abb2011-11-09 14:23:04 +0000740 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000741 pDb->pSchema->cache_size = size;
742 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000743 }
drh9ccd8652013-09-13 16:36:46 +0000744 break;
745 }
drhc11d4f92003-04-06 21:08:24 +0000746
747 /*
drh9b0cf342015-11-12 14:57:19 +0000748 ** PRAGMA [schema.]cache_spill
749 ** PRAGMA cache_spill=BOOLEAN
750 ** PRAGMA [schema.]cache_spill=N
751 **
752 ** The first form reports the current local setting for the
753 ** page cache spill size. The second form turns cache spill on
754 ** or off. When turnning cache spill on, the size is set to the
755 ** current cache_size. The third form sets a spill size that
756 ** may be different form the cache size.
757 ** If N is positive then that is the
758 ** number of pages in the cache. If N is negative, then the
759 ** number of pages is adjusted so that the cache uses -N kibibytes
760 ** of memory.
761 **
762 ** If the number of cache_spill pages is less then the number of
763 ** cache_size pages, no spilling occurs until the page count exceeds
764 ** the number of cache_size pages.
765 **
766 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
767 ** not just the schema specified.
768 */
769 case PragTyp_CACHE_SPILL: {
drh9b0cf342015-11-12 14:57:19 +0000770 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
771 if( !zRight ){
drh9b0cf342015-11-12 14:57:19 +0000772 returnSingleInt(v, "cache_spill",
773 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
774 sqlite3BtreeSetSpillSize(pDb->pBt,0));
775 }else{
drh4f9c8ec2015-11-12 15:47:48 +0000776 int size = 1;
drh9b0cf342015-11-12 14:57:19 +0000777 if( sqlite3GetInt32(zRight, &size) ){
778 sqlite3BtreeSetSpillSize(pDb->pBt, size);
779 }
drh4f9c8ec2015-11-12 15:47:48 +0000780 if( sqlite3GetBoolean(zRight, size!=0) ){
drh9b0cf342015-11-12 14:57:19 +0000781 db->flags |= SQLITE_CacheSpill;
782 }else{
783 db->flags &= ~SQLITE_CacheSpill;
784 }
drh4f9c8ec2015-11-12 15:47:48 +0000785 setAllPagerFlags(db);
drh9b0cf342015-11-12 14:57:19 +0000786 }
787 break;
788 }
789
790 /*
791 ** PRAGMA [schema.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000792 **
drh0d0614b2013-03-25 23:09:28 +0000793 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000794 ** used to limit the aggregate size of all memory mapped regions of the
795 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000796 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000797 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000798 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000799 **
drh0d0614b2013-03-25 23:09:28 +0000800 ** This value is advisory. The underlying VFS is free to memory map
801 ** as little or as much as it wants. Except, if N is set to 0 then the
802 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000803 */
drh9ccd8652013-09-13 16:36:46 +0000804 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000805 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000806#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000807 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000808 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000809 int ii;
drh9296c182014-07-23 13:40:49 +0000810 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000811 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
812 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000813 for(ii=db->nDb-1; ii>=0; ii--){
814 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000815 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000816 }
817 }
dan5d8a1372013-03-19 19:28:06 +0000818 }
drh9b4c59f2013-04-15 17:03:42 +0000819 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000820 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000821#else
dan3719f5f2013-05-23 10:13:18 +0000822 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000823 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000824#endif
dan3719f5f2013-05-23 10:13:18 +0000825 if( rc==SQLITE_OK ){
drh7cc023c2015-09-03 04:28:25 +0000826 returnSingleInt(v, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +0000827 }else if( rc!=SQLITE_NOTFOUND ){
828 pParse->nErr++;
829 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000830 }
drh9ccd8652013-09-13 16:36:46 +0000831 break;
832 }
dan5d8a1372013-03-19 19:28:06 +0000833
834 /*
drh90f5ecb2004-07-22 01:19:35 +0000835 ** PRAGMA temp_store
836 ** PRAGMA temp_store = "default"|"memory"|"file"
837 **
838 ** Return or set the local value of the temp_store flag. Changing
839 ** the local value does not make changes to the disk file and the default
840 ** value will be restored the next time the database is opened.
841 **
842 ** Note that it is possible for the library compile-time options to
843 ** override this setting
844 */
drh9ccd8652013-09-13 16:36:46 +0000845 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000846 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000847 returnSingleInt(v, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000848 }else{
849 changeTempStorage(pParse, zRight);
850 }
drh9ccd8652013-09-13 16:36:46 +0000851 break;
852 }
drh90f5ecb2004-07-22 01:19:35 +0000853
854 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000855 ** PRAGMA temp_store_directory
856 ** PRAGMA temp_store_directory = ""|"directory_name"
857 **
858 ** Return or set the local value of the temp_store_directory flag. Changing
859 ** the value sets a specific directory to be used for temporary files.
860 ** Setting to a null string reverts to the default temporary directory search.
861 ** If temporary directory is changed, then invalidateTempStorage.
862 **
863 */
drh9ccd8652013-09-13 16:36:46 +0000864 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000865 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000866 returnSingleText(v, "temp_store_directory", sqlite3_temp_directory);
tpoindex9a09a3c2004-12-20 19:01:32 +0000867 }else{
drh78f82d12008-09-02 00:52:52 +0000868#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000869 if( zRight[0] ){
870 int res;
danielk1977fab11272008-09-16 14:38:02 +0000871 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
872 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000873 sqlite3ErrorMsg(pParse, "not a writable directory");
874 goto pragma_out;
875 }
drh268283b2005-01-08 15:44:25 +0000876 }
danielk1977b06a0b62008-06-26 10:54:12 +0000877 if( SQLITE_TEMP_STORE==0
878 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
879 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000880 ){
881 invalidateTempStorage(pParse);
882 }
drh17435752007-08-16 04:30:38 +0000883 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000884 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000885 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000886 }else{
drh268283b2005-01-08 15:44:25 +0000887 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000888 }
drh78f82d12008-09-02 00:52:52 +0000889#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000890 }
drh9ccd8652013-09-13 16:36:46 +0000891 break;
892 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000893
drhcc716452012-06-06 23:23:23 +0000894#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000895 /*
896 ** PRAGMA data_store_directory
897 ** PRAGMA data_store_directory = ""|"directory_name"
898 **
899 ** Return or set the local value of the data_store_directory flag. Changing
900 ** the value sets a specific directory to be used for database files that
901 ** were specified with a relative pathname. Setting to a null string reverts
902 ** to the default database directory, which for database files specified with
903 ** a relative path will probably be based on the current directory for the
904 ** process. Database file specified with an absolute path are not impacted
905 ** by this setting, regardless of its value.
906 **
907 */
drh9ccd8652013-09-13 16:36:46 +0000908 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +0000909 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000910 returnSingleText(v, "data_store_directory", sqlite3_data_directory);
mistachkina112d142012-03-14 00:44:01 +0000911 }else{
912#ifndef SQLITE_OMIT_WSD
913 if( zRight[0] ){
914 int res;
915 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
916 if( rc!=SQLITE_OK || res==0 ){
917 sqlite3ErrorMsg(pParse, "not a writable directory");
918 goto pragma_out;
919 }
920 }
921 sqlite3_free(sqlite3_data_directory);
922 if( zRight[0] ){
923 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
924 }else{
925 sqlite3_data_directory = 0;
926 }
927#endif /* SQLITE_OMIT_WSD */
928 }
drh9ccd8652013-09-13 16:36:46 +0000929 break;
930 }
drhcc716452012-06-06 23:23:23 +0000931#endif
mistachkina112d142012-03-14 00:44:01 +0000932
drhd2cb50b2009-01-09 21:41:17 +0000933#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000934 /*
drh9b0cf342015-11-12 14:57:19 +0000935 ** PRAGMA [schema.]lock_proxy_file
936 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
drh9ccd8652013-09-13 16:36:46 +0000937 **
938 ** Return or set the value of the lock_proxy_file flag. Changing
939 ** the value sets a specific file to be used for database access locks.
940 **
941 */
942 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +0000943 if( !zRight ){
944 Pager *pPager = sqlite3BtreePager(pDb->pBt);
945 char *proxy_file_path = NULL;
946 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000947 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000948 &proxy_file_path);
drh7cc023c2015-09-03 04:28:25 +0000949 returnSingleText(v, "lock_proxy_file", proxy_file_path);
aswiftaebf4132008-11-21 00:10:35 +0000950 }else{
951 Pager *pPager = sqlite3BtreePager(pDb->pBt);
952 sqlite3_file *pFile = sqlite3PagerFile(pPager);
953 int res;
954 if( zRight[0] ){
955 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
956 zRight);
957 } else {
958 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
959 NULL);
960 }
961 if( res!=SQLITE_OK ){
962 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
963 goto pragma_out;
964 }
965 }
drh9ccd8652013-09-13 16:36:46 +0000966 break;
967 }
drhd2cb50b2009-01-09 21:41:17 +0000968#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000969
970 /*
drh9b0cf342015-11-12 14:57:19 +0000971 ** PRAGMA [schema.]synchronous
972 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000973 **
974 ** Return or set the local value of the synchronous flag. Changing
975 ** the local value does not make changes to the disk file and the
976 ** default value will be restored the next time the database is
977 ** opened.
978 */
drh9ccd8652013-09-13 16:36:46 +0000979 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +0000980 if( !zRight ){
drh7cc023c2015-09-03 04:28:25 +0000981 returnSingleInt(v, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000982 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000983 if( !db->autoCommit ){
984 sqlite3ErrorMsg(pParse,
985 "Safety level may not be changed inside a transaction");
986 }else{
drhd99d2832015-04-17 15:58:33 +0000987 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
988 if( iLevel==0 ) iLevel = 1;
989 pDb->safety_level = iLevel;
drhd3605a42013-08-17 15:42:29 +0000990 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +0000991 }
drhc11d4f92003-04-06 21:08:24 +0000992 }
drh9ccd8652013-09-13 16:36:46 +0000993 break;
994 }
drh13d70422004-11-13 15:59:14 +0000995#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000996
drhbf216272005-02-26 18:10:44 +0000997#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +0000998 case PragTyp_FLAG: {
999 if( zRight==0 ){
drh7cc023c2015-09-03 04:28:25 +00001000 returnSingleInt(v, pPragma->zName, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +00001001 }else{
drhc228be52015-01-31 02:00:01 +00001002 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +00001003 if( db->autoCommit==0 ){
1004 /* Foreign key support may not be enabled or disabled while not
1005 ** in auto-commit mode. */
1006 mask &= ~(SQLITE_ForeignKeys);
1007 }
drhd4530972014-09-09 14:47:53 +00001008#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +00001009 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +00001010 /* Do not allow non-admin users to modify the schema arbitrarily */
1011 mask &= ~(SQLITE_WriteSchema);
1012 }
1013#endif
drh9ccd8652013-09-13 16:36:46 +00001014
1015 if( sqlite3GetBoolean(zRight, 0) ){
1016 db->flags |= mask;
1017 }else{
1018 db->flags &= ~mask;
1019 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1020 }
1021
1022 /* Many of the flag-pragmas modify the code generated by the SQL
1023 ** compiler (eg. count_changes). So add an opcode to expire all
1024 ** compiled SQL statements after modifying a pragma value.
1025 */
1026 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
1027 setAllPagerFlags(db);
1028 }
1029 break;
1030 }
drhbf216272005-02-26 18:10:44 +00001031#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001032
drh13d70422004-11-13 15:59:14 +00001033#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001034 /*
1035 ** PRAGMA table_info(<table>)
1036 **
1037 ** Return a single row for each column of the named table. The columns of
1038 ** the returned data set are:
1039 **
1040 ** cid: Column id (numbered from left to right, starting at 0)
1041 ** name: Column name
1042 ** type: Column declaration type.
1043 ** notnull: True if 'NOT NULL' is part of column declaration
1044 ** dflt_value: The default value for the column, if any.
1045 */
drh9ccd8652013-09-13 16:36:46 +00001046 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001047 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001048 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001049 if( pTab ){
drhb460e522015-09-03 03:29:51 +00001050 static const char *azCol[] = {
1051 "cid", "name", "type", "notnull", "dflt_value", "pk"
1052 };
drh384b7fe2013-01-01 13:55:31 +00001053 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001054 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001055 Column *pCol;
drh44156282013-10-23 22:23:03 +00001056 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001057 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001058 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001059 setAllColumnNames(v, 6, azCol); assert( 6==ArraySize(azCol) );
danielk19774adee202004-05-08 08:23:19 +00001060 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001061 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001062 if( IsHiddenColumn(pCol) ){
1063 nHidden++;
1064 continue;
1065 }
drh384b7fe2013-01-01 13:55:31 +00001066 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1067 k = 0;
1068 }else if( pPk==0 ){
1069 k = 1;
1070 }else{
drh1b678962015-04-15 07:19:27 +00001071 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001072 }
drh076e85f2015-09-03 13:46:12 +00001073 sqlite3VdbeMultiLoad(v, 1, "issisi",
1074 i-nHidden,
1075 pCol->zName,
1076 pCol->zType ? pCol->zType : "",
1077 pCol->notNull ? 1 : 0,
1078 pCol->zDflt,
1079 k);
drh2d401ab2008-01-10 23:50:11 +00001080 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001081 }
1082 }
drh9ccd8652013-09-13 16:36:46 +00001083 }
1084 break;
drhc11d4f92003-04-06 21:08:24 +00001085
drh3ef26152013-10-12 20:22:00 +00001086 case PragTyp_STATS: {
drhb460e522015-09-03 03:29:51 +00001087 static const char *azCol[] = { "table", "index", "width", "height" };
drh3ef26152013-10-12 20:22:00 +00001088 Index *pIdx;
1089 HashElem *i;
1090 v = sqlite3GetVdbe(pParse);
drh3ef26152013-10-12 20:22:00 +00001091 pParse->nMem = 4;
1092 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001093 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
drh3ef26152013-10-12 20:22:00 +00001094 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1095 Table *pTab = sqliteHashData(i);
drh076e85f2015-09-03 13:46:12 +00001096 sqlite3VdbeMultiLoad(v, 1, "ssii",
1097 pTab->zName,
1098 0,
1099 (int)sqlite3LogEstToInt(pTab->szTabRow),
1100 (int)sqlite3LogEstToInt(pTab->nRowLogEst));
drh3ef26152013-10-12 20:22:00 +00001101 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1102 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh076e85f2015-09-03 13:46:12 +00001103 sqlite3VdbeMultiLoad(v, 2, "sii",
1104 pIdx->zName,
1105 (int)sqlite3LogEstToInt(pIdx->szIdxRow),
1106 (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]));
drh3ef26152013-10-12 20:22:00 +00001107 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1108 }
1109 }
1110 }
1111 break;
1112
drh9ccd8652013-09-13 16:36:46 +00001113 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001114 Index *pIdx;
1115 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001116 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001117 if( pIdx ){
drhb460e522015-09-03 03:29:51 +00001118 static const char *azCol[] = {
1119 "seqno", "cid", "name", "desc", "coll", "key"
1120 };
drhc11d4f92003-04-06 21:08:24 +00001121 int i;
drh5e7028c2015-03-05 14:29:02 +00001122 int mx;
1123 if( pPragma->iArg ){
1124 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1125 mx = pIdx->nColumn;
1126 pParse->nMem = 6;
1127 }else{
1128 /* PRAGMA index_info (legacy version) */
1129 mx = pIdx->nKeyCol;
1130 pParse->nMem = 3;
1131 }
drhc11d4f92003-04-06 21:08:24 +00001132 pTab = pIdx->pTable;
drhc95e01d2013-02-14 16:16:05 +00001133 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001134 assert( pParse->nMem<=ArraySize(azCol) );
drhb460e522015-09-03 03:29:51 +00001135 setAllColumnNames(v, pParse->nMem, azCol);
drhc228be52015-01-31 02:00:01 +00001136 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001137 i16 cnum = pIdx->aiColumn[i];
drh076e85f2015-09-03 13:46:12 +00001138 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum,
1139 cnum<0 ? 0 : pTab->aCol[cnum].zName);
drh5e7028c2015-03-05 14:29:02 +00001140 if( pPragma->iArg ){
drh076e85f2015-09-03 13:46:12 +00001141 sqlite3VdbeMultiLoad(v, 4, "isi",
1142 pIdx->aSortOrder[i],
1143 pIdx->azColl[i],
1144 i<pIdx->nKeyCol);
drh5e7028c2015-03-05 14:29:02 +00001145 }
1146 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001147 }
1148 }
drh9ccd8652013-09-13 16:36:46 +00001149 }
1150 break;
drhc11d4f92003-04-06 21:08:24 +00001151
drh9ccd8652013-09-13 16:36:46 +00001152 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001153 Index *pIdx;
1154 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001155 int i;
drh2783e4b2004-10-05 15:42:53 +00001156 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001157 if( pTab ){
drhb460e522015-09-03 03:29:51 +00001158 static const char *azCol[] = {
1159 "seq", "name", "unique", "origin", "partial"
1160 };
danielk19774adee202004-05-08 08:23:19 +00001161 v = sqlite3GetVdbe(pParse);
drhc228be52015-01-31 02:00:01 +00001162 pParse->nMem = 5;
drhe13e9f52013-10-05 19:18:00 +00001163 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001164 setAllColumnNames(v, 5, azCol); assert( 5==ArraySize(azCol) );
drh3ef26152013-10-12 20:22:00 +00001165 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001166 const char *azOrigin[] = { "c", "u", "pk" };
drh076e85f2015-09-03 13:46:12 +00001167 sqlite3VdbeMultiLoad(v, 1, "isisi",
1168 i,
1169 pIdx->zName,
1170 IsUniqueIndex(pIdx),
1171 azOrigin[pIdx->idxType],
1172 pIdx->pPartIdxWhere!=0);
drhc228be52015-01-31 02:00:01 +00001173 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drhc11d4f92003-04-06 21:08:24 +00001174 }
1175 }
drh9ccd8652013-09-13 16:36:46 +00001176 }
1177 break;
drhc11d4f92003-04-06 21:08:24 +00001178
drh9ccd8652013-09-13 16:36:46 +00001179 case PragTyp_DATABASE_LIST: {
drhb460e522015-09-03 03:29:51 +00001180 static const char *azCol[] = { "seq", "name", "file" };
drh13d70422004-11-13 15:59:14 +00001181 int i;
drh2d401ab2008-01-10 23:50:11 +00001182 pParse->nMem = 3;
drh076e85f2015-09-03 13:46:12 +00001183 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
drh13d70422004-11-13 15:59:14 +00001184 for(i=0; i<db->nDb; i++){
1185 if( db->aDb[i].pBt==0 ) continue;
1186 assert( db->aDb[i].zName!=0 );
drh076e85f2015-09-03 13:46:12 +00001187 sqlite3VdbeMultiLoad(v, 1, "iss",
1188 i,
1189 db->aDb[i].zName,
1190 sqlite3BtreeGetFilename(db->aDb[i].pBt));
drh2d401ab2008-01-10 23:50:11 +00001191 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001192 }
drh9ccd8652013-09-13 16:36:46 +00001193 }
1194 break;
danielk197748af65a2005-02-09 03:20:37 +00001195
drh9ccd8652013-09-13 16:36:46 +00001196 case PragTyp_COLLATION_LIST: {
drhb460e522015-09-03 03:29:51 +00001197 static const char *azCol[] = { "seq", "name" };
danielk197748af65a2005-02-09 03:20:37 +00001198 int i = 0;
1199 HashElem *p;
drh2d401ab2008-01-10 23:50:11 +00001200 pParse->nMem = 2;
drh076e85f2015-09-03 13:46:12 +00001201 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
danielk197748af65a2005-02-09 03:20:37 +00001202 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1203 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh076e85f2015-09-03 13:46:12 +00001204 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
drh2d401ab2008-01-10 23:50:11 +00001205 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001206 }
drh9ccd8652013-09-13 16:36:46 +00001207 }
1208 break;
drh13d70422004-11-13 15:59:14 +00001209#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1210
drhb7f91642004-10-31 02:22:47 +00001211#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001212 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001213 FKey *pFK;
1214 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001215 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001216 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001217 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001218 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001219 if( pFK ){
drhb460e522015-09-03 03:29:51 +00001220 static const char *azCol[] = {
1221 "id", "seq", "table", "from", "to", "on_update", "on_delete",
1222 "match"
1223 };
danielk1977742f9472004-06-16 12:02:43 +00001224 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001225 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001226 sqlite3CodeVerifySchema(pParse, iDb);
drh076e85f2015-09-03 13:46:12 +00001227 setAllColumnNames(v, 8, azCol); assert( 8==ArraySize(azCol) );
danielk1977742f9472004-06-16 12:02:43 +00001228 while(pFK){
1229 int j;
1230 for(j=0; j<pFK->nCol; j++){
drh076e85f2015-09-03 13:46:12 +00001231 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1232 i,
1233 j,
1234 pFK->zTo,
1235 pTab->aCol[pFK->aCol[j].iFrom].zName,
1236 pFK->aCol[j].zCol,
1237 actionName(pFK->aAction[1]), /* ON UPDATE */
1238 actionName(pFK->aAction[0]), /* ON DELETE */
1239 "NONE");
danielk197750af3e12008-10-10 17:47:21 +00001240 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001241 }
1242 ++i;
1243 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001244 }
drh78100cc2003-08-23 22:40:53 +00001245 }
1246 }
drh9ccd8652013-09-13 16:36:46 +00001247 }
1248 break;
drhb7f91642004-10-31 02:22:47 +00001249#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001250
drh6c5b9152012-12-17 16:46:37 +00001251#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001252#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001253 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001254 FKey *pFK; /* A foreign key constraint */
1255 Table *pTab; /* Child table contain "REFERENCES" keyword */
1256 Table *pParent; /* Parent table that child points to */
1257 Index *pIdx; /* Index in the parent table */
1258 int i; /* Loop counter: Foreign key number for pTab */
1259 int j; /* Loop counter: Field of the foreign key */
1260 HashElem *k; /* Loop counter: Next table in schema */
1261 int x; /* result variable */
1262 int regResult; /* 3 registers to hold a result row */
1263 int regKey; /* Register to hold key for checking the FK */
1264 int regRow; /* Registers to hold a row from pTab */
1265 int addrTop; /* Top of a loop checking foreign keys */
1266 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001267 int *aiCols; /* child to parent column mapping */
drhb460e522015-09-03 03:29:51 +00001268 static const char *azCol[] = { "table", "rowid", "parent", "fkid" };
drh6c5b9152012-12-17 16:46:37 +00001269
drh613028b2012-12-17 18:43:02 +00001270 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001271 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001272 regKey = ++pParse->nMem;
1273 regRow = ++pParse->nMem;
1274 v = sqlite3GetVdbe(pParse);
drh076e85f2015-09-03 13:46:12 +00001275 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
drh613028b2012-12-17 18:43:02 +00001276 sqlite3CodeVerifySchema(pParse, iDb);
1277 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1278 while( k ){
1279 if( zRight ){
1280 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1281 k = 0;
1282 }else{
1283 pTab = (Table*)sqliteHashData(k);
1284 k = sqliteHashNext(k);
1285 }
drh9148def2012-12-17 20:40:39 +00001286 if( pTab==0 || pTab->pFKey==0 ) continue;
1287 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001288 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001289 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh076e85f2015-09-03 13:46:12 +00001290 sqlite3VdbeLoadString(v, regResult, pTab->zName);
drh6c5b9152012-12-17 16:46:37 +00001291 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001292 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1293 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001294 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001295 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001296 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001297 if( x==0 ){
1298 if( pIdx==0 ){
1299 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1300 }else{
drh6c5b9152012-12-17 16:46:37 +00001301 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001302 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001303 }
1304 }else{
drh613028b2012-12-17 18:43:02 +00001305 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001306 break;
1307 }
drh6c5b9152012-12-17 16:46:37 +00001308 }
dan5e878302013-10-12 19:06:48 +00001309 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001310 if( pFK ) break;
1311 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001312 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001313 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001314 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001315 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001316 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001317 if( pParent ){
1318 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1319 assert( x==0 );
1320 }
drh613028b2012-12-17 18:43:02 +00001321 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001322 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001323 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001324 assert( iKey>=0 && iKey<pTab->nCol );
1325 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001326 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1327 sqlite3ColumnDefault(v, pTab, iKey, regRow);
drh688852a2014-02-17 22:40:43 +00001328 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
1329 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1330 sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001331 }else{
drh613028b2012-12-17 18:43:02 +00001332 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001333 }
drh688852a2014-02-17 22:40:43 +00001334 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001335 sqlite3VdbeGoto(v, addrOk);
drh613028b2012-12-17 18:43:02 +00001336 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1337 }else{
1338 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001339 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001340 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh688852a2014-02-17 22:40:43 +00001341 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001342 }
dan5e878302013-10-12 19:06:48 +00001343 if( pParent ){
drh57bf4a82014-02-17 14:59:22 +00001344 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
drhe9107692015-08-25 19:20:04 +00001345 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
dan5e878302013-10-12 19:06:48 +00001346 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh688852a2014-02-17 22:40:43 +00001347 VdbeCoverage(v);
dan5e878302013-10-12 19:06:48 +00001348 }
drh6c5b9152012-12-17 16:46:37 +00001349 }
drh613028b2012-12-17 18:43:02 +00001350 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh076e85f2015-09-03 13:46:12 +00001351 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1);
drh4b4b4732012-12-17 20:57:15 +00001352 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001353 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001354 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001355 }
drh688852a2014-02-17 22:40:43 +00001356 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001357 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001358 }
drh9ccd8652013-09-13 16:36:46 +00001359 }
1360 break;
dan09ff9e12013-03-11 11:49:03 +00001361#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001362#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1363
drhc11d4f92003-04-06 21:08:24 +00001364#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001365 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001366 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001367 if( sqlite3GetBoolean(zRight, 0) ){
drh97e58a22015-11-10 14:27:17 +00001368 sqlite3ParserTrace(stdout, "parser: ");
drh55ef4d92005-08-14 01:20:37 +00001369 }else{
1370 sqlite3ParserTrace(0, 0);
1371 }
drhc11d4f92003-04-06 21:08:24 +00001372 }
drh9ccd8652013-09-13 16:36:46 +00001373 }
1374 break;
drhc11d4f92003-04-06 21:08:24 +00001375#endif
1376
drh55ef4d92005-08-14 01:20:37 +00001377 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1378 ** used will be case sensitive or not depending on the RHS.
1379 */
drh9ccd8652013-09-13 16:36:46 +00001380 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001381 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001382 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001383 }
drh9ccd8652013-09-13 16:36:46 +00001384 }
1385 break;
drh55ef4d92005-08-14 01:20:37 +00001386
drh1dcdbc02007-01-27 02:24:54 +00001387#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1388# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1389#endif
1390
drhb7f91642004-10-31 02:22:47 +00001391#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001392 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001393 ** integrity_check designed to detect most database corruption
1394 ** without most of the overhead of a full integrity-check.
1395 */
drh9ccd8652013-09-13 16:36:46 +00001396 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001397 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001398
drhed717fe2003-06-15 23:42:24 +00001399 /* Code that appears at the end of the integrity check. If no error
1400 ** messages have been generated, output OK. Otherwise output the
1401 ** error message
1402 */
drhb06a4ec2014-03-10 18:03:09 +00001403 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +00001404 static const VdbeOpList endCode[] = {
drh8b0cf382015-10-06 21:07:06 +00001405 { OP_AddImm, 1, 0, 0}, /* 0 */
1406 { OP_If, 1, 0, 0}, /* 1 */
1407 { OP_String8, 0, 3, 0}, /* 2 */
drh2d401ab2008-01-10 23:50:11 +00001408 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001409 };
drhed717fe2003-06-15 23:42:24 +00001410
drhc5227312011-10-13 17:09:01 +00001411 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001412
dan5885e762012-07-16 10:06:12 +00001413 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1414 ** then iDb is set to the index of the database identified by <db>.
1415 ** In this case, the integrity of database iDb only is verified by
1416 ** the VDBE created below.
1417 **
1418 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1419 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1420 ** to -1 here, to indicate that the VDBE should verify the integrity
1421 ** of all attached databases. */
1422 assert( iDb>=0 );
1423 assert( iDb==0 || pId2->z );
1424 if( pId2->z==0 ) iDb = -1;
1425
drhed717fe2003-06-15 23:42:24 +00001426 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001427 pParse->nMem = 6;
drhb460e522015-09-03 03:29:51 +00001428 setOneColumnName(v, "integrity_check");
drh1dcdbc02007-01-27 02:24:54 +00001429
1430 /* Set the maximum error count */
1431 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1432 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001433 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001434 if( mxErr<=0 ){
1435 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1436 }
1437 }
drh2d401ab2008-01-10 23:50:11 +00001438 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001439
1440 /* Do an integrity check on each database file */
1441 for(i=0; i<db->nDb; i++){
1442 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001443 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001444 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001445
danielk197753c0f742005-03-29 03:10:59 +00001446 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001447 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001448
drh80242052004-06-09 00:48:12 +00001449 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001450 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh688852a2014-02-17 22:40:43 +00001451 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001452 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001453 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001454
drhed717fe2003-06-15 23:42:24 +00001455 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001456 **
1457 ** Begin by filling registers 2, 3, ... with the root pages numbers
1458 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001459 */
dan5885e762012-07-16 10:06:12 +00001460 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001461 pTbls = &db->aDb[i].pSchema->tblHash;
1462 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001463 Table *pTab = sqliteHashData(x);
1464 Index *pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001465 if( HasRowid(pTab) ){
1466 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001467 VdbeComment((v, "%s", pTab->zName));
drh6fbe41a2013-10-30 20:22:55 +00001468 cnt++;
1469 }
drh79069752004-05-22 21:30:40 +00001470 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001471 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001472 VdbeComment((v, "%s", pIdx->zName));
drh79069752004-05-22 21:30:40 +00001473 cnt++;
1474 }
1475 }
drh2d401ab2008-01-10 23:50:11 +00001476
1477 /* Make sure sufficient number of registers have been allocated */
drh6fbe41a2013-10-30 20:22:55 +00001478 pParse->nMem = MAX( pParse->nMem, cnt+8 );
drh2d401ab2008-01-10 23:50:11 +00001479
1480 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001481 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001482 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001483 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001484 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001485 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001486 P4_DYNAMIC);
drh079a3072014-03-19 14:10:55 +00001487 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001488 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001489 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001490 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001491
1492 /* Make sure all the indices are constructed correctly.
1493 */
danielk197741c58b72007-12-29 13:39:19 +00001494 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001495 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001496 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001497 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001498 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001499 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001500 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001501
1502 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001503 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001504 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh688852a2014-02-17 22:40:43 +00001505 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001506 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001507 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001508 sqlite3ExprCacheClear(pParse);
danfd261ec2015-10-22 20:54:33 +00001509 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
drh6a534992013-11-16 20:13:39 +00001510 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001511 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001512 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001513 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001514 }
drh6fbe41a2013-10-30 20:22:55 +00001515 pParse->nMem = MAX(pParse->nMem, 8+j);
drh688852a2014-02-17 22:40:43 +00001516 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001517 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhcefc87f2014-08-01 01:40:33 +00001518 /* Verify that all NOT NULL columns really are NOT NULL */
1519 for(j=0; j<pTab->nCol; j++){
1520 char *zErr;
1521 int jmp2, jmp3;
1522 if( j==pTab->iPKey ) continue;
1523 if( pTab->aCol[j].notNull==0 ) continue;
1524 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1525 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1526 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1527 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1528 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1529 pTab->aCol[j].zName);
1530 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1531 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1532 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1533 sqlite3VdbeAddOp0(v, OP_Halt);
1534 sqlite3VdbeJumpHere(v, jmp2);
1535 sqlite3VdbeJumpHere(v, jmp3);
1536 }
1537 /* Validate index entries for the current row */
drhed717fe2003-06-15 23:42:24 +00001538 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhcefc87f2014-08-01 01:40:33 +00001539 int jmp2, jmp3, jmp4, jmp5;
1540 int ckUniq = sqlite3VdbeMakeLabel(v);
drh6fbe41a2013-10-30 20:22:55 +00001541 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001542 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1543 pPrior, r1);
1544 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001545 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drhcefc87f2014-08-01 01:40:33 +00001546 /* Verify that an index entry exists for the current table row */
1547 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
drh688852a2014-02-17 22:40:43 +00001548 pIdx->nColumn); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001549 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001550 sqlite3VdbeLoadString(v, 3, "row ");
drh6fbe41a2013-10-30 20:22:55 +00001551 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001552 sqlite3VdbeLoadString(v, 4, " missing from index ");
drh6fbe41a2013-10-30 20:22:55 +00001553 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drh076e85f2015-09-03 13:46:12 +00001554 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
drh6fbe41a2013-10-30 20:22:55 +00001555 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1556 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh688852a2014-02-17 22:40:43 +00001557 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001558 sqlite3VdbeAddOp0(v, OP_Halt);
drhd654be82005-09-20 17:42:23 +00001559 sqlite3VdbeJumpHere(v, jmp2);
drhcefc87f2014-08-01 01:40:33 +00001560 /* For UNIQUE indexes, verify that only one entry exists with the
1561 ** current key. The entry is unique if (1) any column is NULL
1562 ** or (2) the next entry has a different key */
1563 if( IsUniqueIndex(pIdx) ){
1564 int uniqOk = sqlite3VdbeMakeLabel(v);
1565 int jmp6;
1566 int kk;
1567 for(kk=0; kk<pIdx->nKeyCol; kk++){
1568 int iCol = pIdx->aiColumn[kk];
drh4b92f982015-09-29 17:20:14 +00001569 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
drh68391ac2015-09-25 20:49:16 +00001570 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
drhcefc87f2014-08-01 01:40:33 +00001571 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1572 VdbeCoverage(v);
1573 }
1574 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00001575 sqlite3VdbeGoto(v, uniqOk);
drhcefc87f2014-08-01 01:40:33 +00001576 sqlite3VdbeJumpHere(v, jmp6);
1577 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1578 pIdx->nKeyCol); VdbeCoverage(v);
1579 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
drh076e85f2015-09-03 13:46:12 +00001580 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1581 sqlite3VdbeGoto(v, jmp5);
drhcefc87f2014-08-01 01:40:33 +00001582 sqlite3VdbeResolveLabel(v, uniqOk);
1583 }
1584 sqlite3VdbeJumpHere(v, jmp4);
drh87744512014-04-13 19:15:49 +00001585 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhed717fe2003-06-15 23:42:24 +00001586 }
drh688852a2014-02-17 22:40:43 +00001587 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001588 sqlite3VdbeJumpHere(v, loopTop-1);
1589#ifndef SQLITE_OMIT_BTREECOUNT
drh076e85f2015-09-03 13:46:12 +00001590 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
drh8a9789b2013-08-01 03:36:59 +00001591 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001592 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001593 addr = sqlite3VdbeCurrentAddr(v);
drh688852a2014-02-17 22:40:43 +00001594 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001595 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001596 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh688852a2014-02-17 22:40:43 +00001597 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
drh5655c542014-02-19 19:14:34 +00001598 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh8a9789b2013-08-01 03:36:59 +00001599 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh076e85f2015-09-03 13:46:12 +00001600 sqlite3VdbeLoadString(v, 3, pIdx->zName);
drh8a9789b2013-08-01 03:36:59 +00001601 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1602 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001603 }
drh8a9789b2013-08-01 03:36:59 +00001604#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001605 }
1606 }
drh688852a2014-02-17 22:40:43 +00001607 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
drh8b0cf382015-10-06 21:07:06 +00001608 sqlite3VdbeChangeP2(v, addr, -mxErr);
1609 sqlite3VdbeJumpHere(v, addr+1);
1610 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drh9ccd8652013-09-13 16:36:46 +00001611 }
1612 break;
drhb7f91642004-10-31 02:22:47 +00001613#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1614
drh13d70422004-11-13 15:59:14 +00001615#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001616 /*
1617 ** PRAGMA encoding
1618 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1619 **
drh85b623f2007-12-13 21:54:09 +00001620 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001621 ** database. If the database is not initialized, it is initialized now.
1622 **
1623 ** The second form of this pragma is a no-op if the main database file
1624 ** has not already been initialized. In this case it sets the default
1625 ** encoding that will be used for the main database file if a new file
1626 ** is created. If an existing main database file is opened, then the
1627 ** default text encoding for the existing database is used.
1628 **
1629 ** In all cases new databases created using the ATTACH command are
1630 ** created to use the same default text encoding as the main database. If
1631 ** the main database has not been initialized and/or created when ATTACH
1632 ** is executed, this is done before the ATTACH operation.
1633 **
1634 ** In the second form this pragma sets the text encoding to be used in
1635 ** new database files created using this database handle. It is only
1636 ** useful if invoked immediately after the main database i
1637 */
drh9ccd8652013-09-13 16:36:46 +00001638 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001639 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001640 char *zName;
1641 u8 enc;
1642 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001643 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001644 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1645 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1646 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001647 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001648 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001649 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1650 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001651 { 0, 0 }
1652 };
drh0f7eb612006-08-08 13:51:43 +00001653 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001654 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001655 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhd2cb50b2009-01-09 21:41:17 +00001656 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1657 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1658 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
drh7cc023c2015-09-03 04:28:25 +00001659 returnSingleText(v, "encoding", encnames[ENC(pParse->db)].zName);
danielk19778e227872004-06-07 07:52:17 +00001660 }else{ /* "PRAGMA encoding = XXX" */
1661 /* Only change the value of sqlite.enc if the database handle is not
1662 ** initialized. If the main database exists, the new sqlite.enc value
1663 ** will be overwritten when the schema is next loaded. If it does not
1664 ** already exists, it will be created to use the new encoding value.
1665 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001666 if(
1667 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1668 DbHasProperty(db, 0, DB_Empty)
1669 ){
danielk19778e227872004-06-07 07:52:17 +00001670 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1671 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh9bd3cc42014-12-12 23:17:54 +00001672 SCHEMA_ENC(db) = ENC(db) =
1673 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001674 break;
1675 }
1676 }
1677 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001678 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001679 }
1680 }
1681 }
drh9ccd8652013-09-13 16:36:46 +00001682 }
1683 break;
drh13d70422004-11-13 15:59:14 +00001684#endif /* SQLITE_OMIT_UTF16 */
1685
1686#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001687 /*
drh9b0cf342015-11-12 14:57:19 +00001688 ** PRAGMA [schema.]schema_version
1689 ** PRAGMA [schema.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001690 **
drh9b0cf342015-11-12 14:57:19 +00001691 ** PRAGMA [schema.]user_version
1692 ** PRAGMA [schema.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001693 **
drh9b0cf342015-11-12 14:57:19 +00001694 ** PRAGMA [schema.]freelist_count = <integer>
drh4ee09b42013-05-01 19:49:27 +00001695 **
drh9b0cf342015-11-12 14:57:19 +00001696 ** PRAGMA [schema.]application_id
1697 ** PRAGMA [schema.]application_id = <integer>
drh4ee09b42013-05-01 19:49:27 +00001698 **
danielk1977b92b70b2004-11-12 16:11:59 +00001699 ** The pragma's schema_version and user_version are used to set or get
1700 ** the value of the schema-version and user-version, respectively. Both
1701 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001702 ** stored in the database header.
1703 **
1704 ** The schema-cookie is usually only manipulated internally by SQLite. It
1705 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001706 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001707 ** SQLite each time a query is executed to ensure that the internal cache
1708 ** of the schema used when compiling the SQL query matches the schema of
1709 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001710 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1711 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001712 ** crashes or database corruption. Use with caution!
1713 **
danielk1977b92b70b2004-11-12 16:11:59 +00001714 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001715 ** applications for any purpose.
1716 */
drh9ccd8652013-09-13 16:36:46 +00001717 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00001718 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00001719 sqlite3VdbeUsesBtree(v, iDb);
drhc228be52015-01-31 02:00:01 +00001720 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00001721 /* Write the specified cookie value */
1722 static const VdbeOpList setCookie[] = {
1723 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001724 { OP_Integer, 0, 1, 0}, /* 1 */
1725 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001726 };
drh688852a2014-02-17 22:40:43 +00001727 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00001728 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00001729 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00001730 sqlite3VdbeChangeP1(v, addr+2, iDb);
1731 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1732 }else{
1733 /* Read the specified cookie value */
1734 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001735 { OP_Transaction, 0, 0, 0}, /* 0 */
1736 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001737 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001738 };
drh688852a2014-02-17 22:40:43 +00001739 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00001740 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001741 sqlite3VdbeChangeP1(v, addr+1, iDb);
1742 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001743 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001744 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001745 }
drh9ccd8652013-09-13 16:36:46 +00001746 }
1747 break;
drh13d70422004-11-13 15:59:14 +00001748#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001749
shanehdc97a8c2010-02-23 20:08:35 +00001750#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1751 /*
1752 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001753 **
drh71caabf2010-02-26 15:39:24 +00001754 ** Return the names of all compile-time options used in this build,
1755 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001756 */
drh9ccd8652013-09-13 16:36:46 +00001757 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00001758 int i = 0;
1759 const char *zOpt;
shanehdc97a8c2010-02-23 20:08:35 +00001760 pParse->nMem = 1;
drhb460e522015-09-03 03:29:51 +00001761 setOneColumnName(v, "compile_option");
shanehdc97a8c2010-02-23 20:08:35 +00001762 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
drh076e85f2015-09-03 13:46:12 +00001763 sqlite3VdbeLoadString(v, 1, zOpt);
shanehdc97a8c2010-02-23 20:08:35 +00001764 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1765 }
drh9ccd8652013-09-13 16:36:46 +00001766 }
1767 break;
shanehdc97a8c2010-02-23 20:08:35 +00001768#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1769
dan5cf53532010-05-01 16:40:20 +00001770#ifndef SQLITE_OMIT_WAL
1771 /*
drh9b0cf342015-11-12 14:57:19 +00001772 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00001773 **
1774 ** Checkpoint the database.
1775 */
drh9ccd8652013-09-13 16:36:46 +00001776 case PragTyp_WAL_CHECKPOINT: {
drhb460e522015-09-03 03:29:51 +00001777 static const char *azCol[] = { "busy", "log", "checkpointed" };
dana58f26f2010-11-16 18:56:51 +00001778 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001779 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1780 if( zRight ){
1781 if( sqlite3StrICmp(zRight, "full")==0 ){
1782 eMode = SQLITE_CHECKPOINT_FULL;
1783 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1784 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00001785 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1786 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00001787 }
1788 }
drh076e85f2015-09-03 13:46:12 +00001789 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
dancdc1f042010-11-18 12:11:05 +00001790 pParse->nMem = 3;
drh30aa3b92011-02-07 23:56:01 +00001791 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001792 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00001793 }
1794 break;
dan5a299f92010-05-03 11:05:08 +00001795
1796 /*
1797 ** PRAGMA wal_autocheckpoint
1798 ** PRAGMA wal_autocheckpoint = N
1799 **
1800 ** Configure a database connection to automatically checkpoint a database
1801 ** after accumulating N frames in the log. Or query for the current value
1802 ** of N.
1803 */
drh9ccd8652013-09-13 16:36:46 +00001804 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00001805 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001806 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001807 }
drh7cc023c2015-09-03 04:28:25 +00001808 returnSingleInt(v, "wal_autocheckpoint",
drhb033d8b2010-05-03 13:37:30 +00001809 db->xWalCallback==sqlite3WalDefaultHook ?
1810 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00001811 }
1812 break;
dan5cf53532010-05-01 16:40:20 +00001813#endif
dan7c246102010-04-12 19:00:29 +00001814
drh09419b42011-11-16 19:29:17 +00001815 /*
1816 ** PRAGMA shrink_memory
1817 **
drh51a74d42015-02-28 01:04:27 +00001818 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
1819 ** connection on which it is invoked to free up as much memory as it
1820 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00001821 */
drh9ccd8652013-09-13 16:36:46 +00001822 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00001823 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00001824 break;
1825 }
drh09419b42011-11-16 19:29:17 +00001826
drhf3603962012-09-07 16:46:59 +00001827 /*
1828 ** PRAGMA busy_timeout
1829 ** PRAGMA busy_timeout = N
1830 **
1831 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001832 ** if one is set. If no busy handler or a different busy handler is set
1833 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1834 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001835 */
drhd49c3582013-09-13 19:00:06 +00001836 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00001837 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00001838 if( zRight ){
1839 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1840 }
drh7cc023c2015-09-03 04:28:25 +00001841 returnSingleInt(v, "timeout", db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00001842 break;
1843 }
drhf3603962012-09-07 16:46:59 +00001844
drh55e85ca2013-09-13 21:01:56 +00001845 /*
1846 ** PRAGMA soft_heap_limit
1847 ** PRAGMA soft_heap_limit = N
1848 **
drh51a74d42015-02-28 01:04:27 +00001849 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
1850 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
1851 ** specified and is a non-negative integer.
1852 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
1853 ** returns the same integer that would be returned by the
1854 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00001855 */
1856 case PragTyp_SOFT_HEAP_LIMIT: {
1857 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00001858 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00001859 sqlite3_soft_heap_limit64(N);
1860 }
drh7cc023c2015-09-03 04:28:25 +00001861 returnSingleInt(v, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
drh55e85ca2013-09-13 21:01:56 +00001862 break;
1863 }
1864
drh03459612014-08-25 15:13:22 +00001865 /*
1866 ** PRAGMA threads
1867 ** PRAGMA threads = N
1868 **
1869 ** Configure the maximum number of worker threads. Return the new
1870 ** maximum, which might be less than requested.
1871 */
1872 case PragTyp_THREADS: {
1873 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00001874 if( zRight
drh03459612014-08-25 15:13:22 +00001875 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
1876 && N>=0
1877 ){
drh111544c2014-08-29 16:20:47 +00001878 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00001879 }
drh7cc023c2015-09-03 04:28:25 +00001880 returnSingleInt(v, "threads",
drh111544c2014-08-29 16:20:47 +00001881 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00001882 break;
1883 }
1884
dougcurrie81c95ef2004-06-18 23:21:47 +00001885#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001886 /*
1887 ** Report the current state of file logs for all databases
1888 */
drh9ccd8652013-09-13 16:36:46 +00001889 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00001890 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001891 "unlocked", "shared", "reserved", "pending", "exclusive"
1892 };
drhb460e522015-09-03 03:29:51 +00001893 static const char *azCol[] = { "database", "status" };
drh89ac8c12004-06-09 14:17:20 +00001894 int i;
drh076e85f2015-09-03 13:46:12 +00001895 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
drh2d401ab2008-01-10 23:50:11 +00001896 pParse->nMem = 2;
drh89ac8c12004-06-09 14:17:20 +00001897 for(i=0; i<db->nDb; i++){
1898 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001899 const char *zState = "unknown";
1900 int j;
drh89ac8c12004-06-09 14:17:20 +00001901 if( db->aDb[i].zName==0 ) continue;
drh89ac8c12004-06-09 14:17:20 +00001902 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001903 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001904 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001905 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001906 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1907 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001908 }
drh076e85f2015-09-03 13:46:12 +00001909 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zName, zState);
drh2d401ab2008-01-10 23:50:11 +00001910 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001911 }
drh9ccd8652013-09-13 16:36:46 +00001912 break;
1913 }
drh89ac8c12004-06-09 14:17:20 +00001914#endif
1915
shanehad9f9f62010-02-17 17:48:46 +00001916#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00001917 case PragTyp_KEY: {
1918 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1919 break;
1920 }
1921 case PragTyp_REKEY: {
1922 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1923 break;
1924 }
1925 case PragTyp_HEXKEY: {
1926 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00001927 u8 iByte;
1928 int i;
drh9ccd8652013-09-13 16:36:46 +00001929 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00001930 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
1931 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
1932 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00001933 }
1934 if( (zLeft[3] & 0xf)==0xb ){
1935 sqlite3_key_v2(db, zDb, zKey, i/2);
1936 }else{
1937 sqlite3_rekey_v2(db, zDb, zKey, i/2);
1938 }
drhd2cb50b2009-01-09 21:41:17 +00001939 }
drh9ccd8652013-09-13 16:36:46 +00001940 break;
1941 }
drh3c4f2a42005-12-08 18:12:56 +00001942#endif
shanehad9f9f62010-02-17 17:48:46 +00001943#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00001944 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001945#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001946 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001947 sqlite3_activate_see(&zRight[4]);
1948 }
1949#endif
1950#ifdef SQLITE_ENABLE_CEROD
1951 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001952 sqlite3_activate_cerod(&zRight[6]);
1953 }
1954#endif
drh9ccd8652013-09-13 16:36:46 +00001955 }
1956 break;
drh21e2cab2006-09-25 18:01:57 +00001957#endif
drh3c4f2a42005-12-08 18:12:56 +00001958
drh9ccd8652013-09-13 16:36:46 +00001959 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00001960
danielk1977e0048402004-06-15 16:51:01 +00001961pragma_out:
drh633e6d52008-07-28 19:34:53 +00001962 sqlite3DbFree(db, zLeft);
1963 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001964}
drh13d70422004-11-13 15:59:14 +00001965
drh8bfdf722009-06-19 14:06:03 +00001966#endif /* SQLITE_OMIT_PRAGMA */