blob: 8f6ac647547fbc8f68e10c35e0b5da4060b7f48e [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/*
163** Generate code to return a single integer value.
164*/
drh3c713642009-04-04 16:02:32 +0000165static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000166 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000167 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000168 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
169 if( pI64 ){
170 memcpy(pI64, &value, sizeof(value));
171 }
172 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
drh10861722009-04-07 00:49:16 +0000173 sqlite3VdbeSetNumCols(v, 1);
174 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000175 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000176}
177
drhd3605a42013-08-17 15:42:29 +0000178
179/*
180** Set the safety_level and pager flags for pager iDb. Or if iDb<0
181** set these values for all pagers.
182*/
183#ifndef SQLITE_OMIT_PAGER_PRAGMAS
184static void setAllPagerFlags(sqlite3 *db){
185 if( db->autoCommit ){
186 Db *pDb = db->aDb;
187 int n = db->nDb;
188 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
189 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
190 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
191 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
192 == PAGER_FLAGS_MASK );
193 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
194 while( (n--) > 0 ){
195 if( pDb->pBt ){
196 sqlite3BtreeSetPagerFlags(pDb->pBt,
197 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
198 }
199 pDb++;
200 }
201 }
202}
drhfeb56e02013-08-23 17:33:46 +0000203#else
204# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29 +0000205#endif
206
207
drhd2cb50b2009-01-09 21:41:17 +0000208/*
209** Return a human-readable name for a constraint resolution action.
210*/
danba9108b2009-09-22 07:13:42 +0000211#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000212static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000213 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000214 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000215 case OE_SetNull: zName = "SET NULL"; break;
216 case OE_SetDflt: zName = "SET DEFAULT"; break;
217 case OE_Cascade: zName = "CASCADE"; break;
218 case OE_Restrict: zName = "RESTRICT"; break;
219 default: zName = "NO ACTION";
220 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000221 }
drhd2cb50b2009-01-09 21:41:17 +0000222 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000223}
danba9108b2009-09-22 07:13:42 +0000224#endif
danielk197750af3e12008-10-10 17:47:21 +0000225
dane04dc882010-04-20 18:53:15 +0000226
227/*
228** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
229** defined in pager.h. This function returns the associated lowercase
230** journal-mode name.
231*/
232const char *sqlite3JournalModename(int eMode){
233 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000234 "delete", "persist", "off", "truncate", "memory"
235#ifndef SQLITE_OMIT_WAL
236 , "wal"
237#endif
dane04dc882010-04-20 18:53:15 +0000238 };
239 assert( PAGER_JOURNALMODE_DELETE==0 );
240 assert( PAGER_JOURNALMODE_PERSIST==1 );
241 assert( PAGER_JOURNALMODE_OFF==2 );
242 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
243 assert( PAGER_JOURNALMODE_MEMORY==4 );
244 assert( PAGER_JOURNALMODE_WAL==5 );
245 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
246
247 if( eMode==ArraySize(azModeName) ) return 0;
248 return azModeName[eMode];
249}
250
drh87223182004-02-21 14:00:29 +0000251/*
drhc11d4f92003-04-06 21:08:24 +0000252** Process a pragma statement.
253**
254** Pragmas are of this form:
255**
danielk197791cf71b2004-06-26 06:37:06 +0000256** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000257**
258** The identifier might also be a string. The value is a string, and
259** identifier, or a number. If minusFlag is true, then the value is
260** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000261**
262** If the left side is "database.id" then pId1 is the database name
263** and pId2 is the id. If the left side is just "id" then pId1 is the
264** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000265*/
danielk197791cf71b2004-06-26 06:37:06 +0000266void sqlite3Pragma(
267 Parse *pParse,
268 Token *pId1, /* First part of [database.]id field */
269 Token *pId2, /* Second part of [database.]id field, or NULL */
270 Token *pValue, /* Token for <value>, or NULL */
271 int minusFlag /* True if a '-' sign preceded <value> */
272){
273 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
274 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
275 const char *zDb = 0; /* The database name */
276 Token *pId; /* Pointer to <id> token */
drh3fa97302012-02-22 16:58:36 +0000277 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000278 int iDb; /* Database index for <database> */
mistachkin1a51ce72015-01-12 18:38:02 +0000279 int lwr, upr, mid = 0; /* Binary search bounds */
drh06fd5d62012-02-22 14:45:19 +0000280 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
281 sqlite3 *db = pParse->db; /* The database connection */
282 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000283 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drhc228be52015-01-31 02:00:01 +0000284 const struct sPragmaNames *pPragma;
drh06fd5d62012-02-22 14:45:19 +0000285
drhc11d4f92003-04-06 21:08:24 +0000286 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000287 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000288 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000289
danielk197791cf71b2004-06-26 06:37:06 +0000290 /* Interpret the [database.] part of the pragma statement. iDb is the
291 ** index of the database this pragma is being applied to in db.aDb[]. */
292 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
293 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000294 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000295
danielk1977ddfb2f02006-02-17 12:25:14 +0000296 /* If the temp database has been explicitly named as part of the
297 ** pragma, make sure it is open.
298 */
299 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
300 return;
301 }
302
drh17435752007-08-16 04:30:38 +0000303 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000304 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000305 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000306 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000307 }else{
drh17435752007-08-16 04:30:38 +0000308 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000309 }
danielk197791cf71b2004-06-26 06:37:06 +0000310
drhd2cb50b2009-01-09 21:41:17 +0000311 assert( pId2 );
312 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000313 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000314 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000315 }
drh06fd5d62012-02-22 14:45:19 +0000316
317 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
318 ** connection. If it returns SQLITE_OK, then assume that the VFS
319 ** handled the pragma and generate a no-op prepared statement.
drh8dd7a6a2015-03-06 04:37:26 +0000320 **
321 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
322 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
323 ** object corresponding to the database file to which the pragma
324 ** statement refers.
325 **
326 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
327 ** file control is an array of pointers to strings (char**) in which the
328 ** second element of the array is the name of the pragma and the third
329 ** element is the argument to the pragma or NULL if the pragma has no
330 ** argument.
drh06fd5d62012-02-22 14:45:19 +0000331 */
drh3fa97302012-02-22 16:58:36 +0000332 aFcntl[0] = 0;
333 aFcntl[1] = zLeft;
334 aFcntl[2] = zRight;
335 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000336 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000337 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
338 if( rc==SQLITE_OK ){
drh3fa97302012-02-22 16:58:36 +0000339 if( aFcntl[0] ){
340 int mem = ++pParse->nMem;
341 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
342 sqlite3VdbeSetNumCols(v, 1);
343 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
344 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
345 sqlite3_free(aFcntl[0]);
346 }
drh9ccd8652013-09-13 16:36:46 +0000347 goto pragma_out;
348 }
349 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000350 if( aFcntl[0] ){
351 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
352 sqlite3_free(aFcntl[0]);
353 }
354 pParse->nErr++;
355 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000356 goto pragma_out;
357 }
358
359 /* Locate the pragma in the lookup table */
360 lwr = 0;
361 upr = ArraySize(aPragmaNames)-1;
362 while( lwr<=upr ){
363 mid = (lwr+upr)/2;
364 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
365 if( rc==0 ) break;
366 if( rc<0 ){
367 upr = mid - 1;
368 }else{
369 lwr = mid + 1;
370 }
371 }
372 if( lwr>upr ) goto pragma_out;
drhc228be52015-01-31 02:00:01 +0000373 pPragma = &aPragmaNames[mid];
drh9ccd8652013-09-13 16:36:46 +0000374
drhf63936e2013-10-03 14:08:07 +0000375 /* Make sure the database schema is loaded if the pragma requires that */
drhc228be52015-01-31 02:00:01 +0000376 if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){
drhf63936e2013-10-03 14:08:07 +0000377 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
378 }
379
drh9ccd8652013-09-13 16:36:46 +0000380 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01 +0000381 switch( pPragma->ePragTyp ){
drh9ccd8652013-09-13 16:36:46 +0000382
drhe73c9142011-11-09 16:12:24 +0000383#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000384 /*
drh90f5ecb2004-07-22 01:19:35 +0000385 ** PRAGMA [database.]default_cache_size
386 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000387 **
388 ** The first form reports the current persistent setting for the
389 ** page cache size. The value returned is the maximum number of
390 ** pages in the page cache. The second form sets both the current
391 ** page cache size value and the persistent page cache size value
392 ** stored in the database file.
393 **
drh93791ea2010-04-26 17:36:35 +0000394 ** Older versions of SQLite would set the default cache size to a
395 ** negative number to indicate synchronous=OFF. These days, synchronous
396 ** is always on by default regardless of the sign of the default cache
397 ** size. But continue to take the absolute value of the default cache
398 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000399 */
drh9ccd8652013-09-13 16:36:46 +0000400 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09 +0000401 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000402 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000403 { OP_Transaction, 0, 0, 0}, /* 0 */
404 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000405 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000406 { OP_Integer, 0, 2, 0},
407 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000408 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000409 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000410 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000411 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000412 };
drh905793e2004-02-21 13:31:09 +0000413 int addr;
drhfb982642007-08-30 01:19:59 +0000414 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000415 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000416 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000417 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000418 pParse->nMem += 2;
drh688852a2014-02-17 22:40:43 +0000419 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
danielk197791cf71b2004-06-26 06:37:06 +0000420 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000421 sqlite3VdbeChangeP1(v, addr+1, iDb);
422 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000423 }else{
drhd50ffc42011-03-08 02:38:28 +0000424 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000425 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000426 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000427 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000428 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000429 pDb->pSchema->cache_size = size;
430 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000431 }
drh9ccd8652013-09-13 16:36:46 +0000432 break;
433 }
drhe73c9142011-11-09 16:12:24 +0000434#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000435
drhe73c9142011-11-09 16:12:24 +0000436#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000437 /*
drh90f5ecb2004-07-22 01:19:35 +0000438 ** PRAGMA [database.]page_size
439 ** PRAGMA [database.]page_size=N
440 **
441 ** The first form reports the current setting for the
442 ** database page size in bytes. The second form sets the
443 ** database page size value. The value can only be set if
444 ** the database has not yet been created.
445 */
drh9ccd8652013-09-13 16:36:46 +0000446 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000447 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000448 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000449 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000450 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000451 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000452 }else{
danielk1977992772c2007-08-30 10:07:38 +0000453 /* Malloc may fail when setting the page-size, as there is an internal
454 ** buffer that the pager module resizes using sqlite3_realloc().
455 */
drh60ac3f42010-11-23 18:59:27 +0000456 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000457 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000458 db->mallocFailed = 1;
459 }
drh90f5ecb2004-07-22 01:19:35 +0000460 }
drh9ccd8652013-09-13 16:36:46 +0000461 break;
462 }
danielk197741483462007-03-24 16:45:04 +0000463
464 /*
drh5b47efa2010-02-12 18:18:39 +0000465 ** PRAGMA [database.]secure_delete
466 ** PRAGMA [database.]secure_delete=ON/OFF
467 **
468 ** The first form reports the current setting for the
469 ** secure_delete flag. The second form changes the secure_delete
470 ** flag setting and reports thenew value.
471 */
drh9ccd8652013-09-13 16:36:46 +0000472 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000473 Btree *pBt = pDb->pBt;
474 int b = -1;
475 assert( pBt!=0 );
476 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000477 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000478 }
drhaf034ed2010-02-12 19:46:26 +0000479 if( pId2->n==0 && b>=0 ){
480 int ii;
481 for(ii=0; ii<db->nDb; ii++){
482 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
483 }
484 }
drh5b47efa2010-02-12 18:18:39 +0000485 b = sqlite3BtreeSecureDelete(pBt, b);
486 returnSingleInt(pParse, "secure_delete", b);
drh9ccd8652013-09-13 16:36:46 +0000487 break;
488 }
drh5b47efa2010-02-12 18:18:39 +0000489
490 /*
drh60ac3f42010-11-23 18:59:27 +0000491 ** PRAGMA [database.]max_page_count
492 ** PRAGMA [database.]max_page_count=N
493 **
494 ** The first form reports the current setting for the
495 ** maximum number of pages in the database file. The
496 ** second form attempts to change this setting. Both
497 ** forms return the current setting.
498 **
drhe73c9142011-11-09 16:12:24 +0000499 ** The absolute value of N is used. This is undocumented and might
500 ** change. The only purpose is to provide an easy way to test
501 ** the sqlite3AbsInt32() function.
502 **
danielk197759a93792008-05-15 17:48:20 +0000503 ** PRAGMA [database.]page_count
504 **
505 ** Return the number of pages in the specified database.
506 */
drh9ccd8652013-09-13 16:36:46 +0000507 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000508 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000509 sqlite3CodeVerifySchema(pParse, iDb);
510 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000511 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000512 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
513 }else{
drhe73c9142011-11-09 16:12:24 +0000514 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
515 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000516 }
danielk197759a93792008-05-15 17:48:20 +0000517 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
518 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000519 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
drh9ccd8652013-09-13 16:36:46 +0000520 break;
521 }
danielk197759a93792008-05-15 17:48:20 +0000522
523 /*
danielk197741483462007-03-24 16:45:04 +0000524 ** PRAGMA [database.]locking_mode
525 ** PRAGMA [database.]locking_mode = (normal|exclusive)
526 */
drh9ccd8652013-09-13 16:36:46 +0000527 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000528 const char *zRet = "normal";
529 int eMode = getLockingMode(zRight);
530
531 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
532 /* Simple "PRAGMA locking_mode;" statement. This is a query for
533 ** the current default locking mode (which may be different to
534 ** the locking-mode of the main database).
535 */
536 eMode = db->dfltLockMode;
537 }else{
538 Pager *pPager;
539 if( pId2->n==0 ){
540 /* This indicates that no database name was specified as part
541 ** of the PRAGMA command. In this case the locking-mode must be
542 ** set on all attached databases, as well as the main db file.
543 **
544 ** Also, the sqlite3.dfltLockMode variable is set so that
545 ** any subsequently attached databases also use the specified
546 ** locking mode.
547 */
548 int ii;
549 assert(pDb==&db->aDb[0]);
550 for(ii=2; ii<db->nDb; ii++){
551 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
552 sqlite3PagerLockingMode(pPager, eMode);
553 }
drh4f21c4a2008-12-10 22:15:00 +0000554 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000555 }
556 pPager = sqlite3BtreePager(pDb->pBt);
557 eMode = sqlite3PagerLockingMode(pPager, eMode);
558 }
559
drh9ccd8652013-09-13 16:36:46 +0000560 assert( eMode==PAGER_LOCKINGMODE_NORMAL
561 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000562 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
563 zRet = "exclusive";
564 }
565 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000566 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000567 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
568 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000569 break;
570 }
drh3b020132008-04-17 17:02:01 +0000571
572 /*
573 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000574 ** PRAGMA [database.]journal_mode =
575 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000576 */
drh9ccd8652013-09-13 16:36:46 +0000577 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000578 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
579 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000580
581 sqlite3VdbeSetNumCols(v, 1);
582 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000583
584 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000585 /* If there is no "=MODE" part of the pragma, do a query for the
586 ** current mode */
drh3b020132008-04-17 17:02:01 +0000587 eMode = PAGER_JOURNALMODE_QUERY;
588 }else{
dane04dc882010-04-20 18:53:15 +0000589 const char *zMode;
drhea678832008-12-10 19:26:22 +0000590 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000591 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000592 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
593 }
594 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000595 /* If the "=MODE" part does not match any known journal mode,
596 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000597 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000598 }
599 }
drhc6b2a0f2010-07-08 17:40:37 +0000600 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
601 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
602 iDb = 0;
603 pId2->n = 1;
604 }
605 for(ii=db->nDb-1; ii>=0; ii--){
606 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
607 sqlite3VdbeUsesBtree(v, ii);
608 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000609 }
drh3b020132008-04-17 17:02:01 +0000610 }
drh3b020132008-04-17 17:02:01 +0000611 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000612 break;
613 }
danielk1977b53e4962008-06-04 06:45:59 +0000614
615 /*
616 ** PRAGMA [database.]journal_size_limit
617 ** PRAGMA [database.]journal_size_limit=N
618 **
drha9e364f2009-01-13 20:14:15 +0000619 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000620 */
drh9ccd8652013-09-13 16:36:46 +0000621 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000622 Pager *pPager = sqlite3BtreePager(pDb->pBt);
623 i64 iLimit = -2;
624 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000625 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000626 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000627 }
628 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000629 returnSingleInt(pParse, "journal_size_limit", iLimit);
drh9ccd8652013-09-13 16:36:46 +0000630 break;
631 }
danielk1977b53e4962008-06-04 06:45:59 +0000632
drh13d70422004-11-13 15:59:14 +0000633#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000634
635 /*
danielk1977951af802004-11-05 15:45:09 +0000636 ** PRAGMA [database.]auto_vacuum
637 ** PRAGMA [database.]auto_vacuum=N
638 **
drha9e364f2009-01-13 20:14:15 +0000639 ** Get or set the value of the database 'auto-vacuum' parameter.
640 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000641 */
642#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000643 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000644 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000645 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000646 if( !zRight ){
drhf63936e2013-10-03 14:08:07 +0000647 returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000648 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000649 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000650 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000651 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000652 /* Call SetAutoVacuum() to set initialize the internal auto and
653 ** incr-vacuum flags. This is required in case this connection
654 ** creates the database file. It is important that it is created
655 ** as an auto-vacuum capable db.
656 */
657 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
658 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
659 /* When setting the auto_vacuum mode to either "full" or
660 ** "incremental", write the value of meta[6] in the database
661 ** file. Before writing to meta[6], check that meta[3] indicates
662 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000663 */
drhb06a4ec2014-03-10 18:03:09 +0000664 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000665 static const VdbeOpList setMeta6[] = {
666 { OP_Transaction, 0, 1, 0}, /* 0 */
667 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
668 { OP_If, 1, 0, 0}, /* 2 */
669 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
670 { OP_Integer, 0, 1, 0}, /* 4 */
671 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
672 };
673 int iAddr;
drh688852a2014-02-17 22:40:43 +0000674 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhf63936e2013-10-03 14:08:07 +0000675 sqlite3VdbeChangeP1(v, iAddr, iDb);
676 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
677 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
678 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
679 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
680 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000681 }
danielk1977951af802004-11-05 15:45:09 +0000682 }
drh9ccd8652013-09-13 16:36:46 +0000683 break;
684 }
danielk1977951af802004-11-05 15:45:09 +0000685#endif
686
drhca5557f2007-05-04 18:30:40 +0000687 /*
688 ** PRAGMA [database.]incremental_vacuum(N)
689 **
690 ** Do N steps of incremental vacuuming on a database.
691 */
692#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000693 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000694 int iLimit, addr;
695 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
696 iLimit = 0x7fffffff;
697 }
698 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000699 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000700 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000701 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000702 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000703 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000704 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000705 break;
706 }
drhca5557f2007-05-04 18:30:40 +0000707#endif
708
drh13d70422004-11-13 15:59:14 +0000709#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000710 /*
drh90f5ecb2004-07-22 01:19:35 +0000711 ** PRAGMA [database.]cache_size
712 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000713 **
714 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000715 ** page cache size. The second form sets the local
716 ** page cache size value. If N is positive then that is the
717 ** number of pages in the cache. If N is negative, then the
718 ** number of pages is adjusted so that the cache uses -N kibibytes
719 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000720 */
drh9ccd8652013-09-13 16:36:46 +0000721 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000722 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000723 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000724 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000725 }else{
drh3b42abb2011-11-09 14:23:04 +0000726 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000727 pDb->pSchema->cache_size = size;
728 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000729 }
drh9ccd8652013-09-13 16:36:46 +0000730 break;
731 }
drhc11d4f92003-04-06 21:08:24 +0000732
733 /*
drh9b4c59f2013-04-15 17:03:42 +0000734 ** PRAGMA [database.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000735 **
drh0d0614b2013-03-25 23:09:28 +0000736 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000737 ** used to limit the aggregate size of all memory mapped regions of the
738 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000739 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000740 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000741 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000742 **
drh0d0614b2013-03-25 23:09:28 +0000743 ** This value is advisory. The underlying VFS is free to memory map
744 ** as little or as much as it wants. Except, if N is set to 0 then the
745 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000746 */
drh9ccd8652013-09-13 16:36:46 +0000747 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000748 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000749#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000750 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000751 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000752 int ii;
drh9296c182014-07-23 13:40:49 +0000753 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000754 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
755 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000756 for(ii=db->nDb-1; ii>=0; ii--){
757 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000758 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000759 }
760 }
dan5d8a1372013-03-19 19:28:06 +0000761 }
drh9b4c59f2013-04-15 17:03:42 +0000762 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000763 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000764#else
dan3719f5f2013-05-23 10:13:18 +0000765 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000766 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000767#endif
dan3719f5f2013-05-23 10:13:18 +0000768 if( rc==SQLITE_OK ){
drh9b4c59f2013-04-15 17:03:42 +0000769 returnSingleInt(pParse, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +0000770 }else if( rc!=SQLITE_NOTFOUND ){
771 pParse->nErr++;
772 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000773 }
drh9ccd8652013-09-13 16:36:46 +0000774 break;
775 }
dan5d8a1372013-03-19 19:28:06 +0000776
777 /*
drh90f5ecb2004-07-22 01:19:35 +0000778 ** PRAGMA temp_store
779 ** PRAGMA temp_store = "default"|"memory"|"file"
780 **
781 ** Return or set the local value of the temp_store flag. Changing
782 ** the local value does not make changes to the disk file and the default
783 ** value will be restored the next time the database is opened.
784 **
785 ** Note that it is possible for the library compile-time options to
786 ** override this setting
787 */
drh9ccd8652013-09-13 16:36:46 +0000788 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000789 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000790 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000791 }else{
792 changeTempStorage(pParse, zRight);
793 }
drh9ccd8652013-09-13 16:36:46 +0000794 break;
795 }
drh90f5ecb2004-07-22 01:19:35 +0000796
797 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000798 ** PRAGMA temp_store_directory
799 ** PRAGMA temp_store_directory = ""|"directory_name"
800 **
801 ** Return or set the local value of the temp_store_directory flag. Changing
802 ** the value sets a specific directory to be used for temporary files.
803 ** Setting to a null string reverts to the default temporary directory search.
804 ** If temporary directory is changed, then invalidateTempStorage.
805 **
806 */
drh9ccd8652013-09-13 16:36:46 +0000807 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000808 if( !zRight ){
809 if( sqlite3_temp_directory ){
810 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000811 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000812 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000813 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
814 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000815 }
816 }else{
drh78f82d12008-09-02 00:52:52 +0000817#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000818 if( zRight[0] ){
819 int res;
danielk1977fab11272008-09-16 14:38:02 +0000820 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
821 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000822 sqlite3ErrorMsg(pParse, "not a writable directory");
823 goto pragma_out;
824 }
drh268283b2005-01-08 15:44:25 +0000825 }
danielk1977b06a0b62008-06-26 10:54:12 +0000826 if( SQLITE_TEMP_STORE==0
827 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
828 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000829 ){
830 invalidateTempStorage(pParse);
831 }
drh17435752007-08-16 04:30:38 +0000832 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000833 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000834 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000835 }else{
drh268283b2005-01-08 15:44:25 +0000836 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000837 }
drh78f82d12008-09-02 00:52:52 +0000838#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000839 }
drh9ccd8652013-09-13 16:36:46 +0000840 break;
841 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000842
drhcc716452012-06-06 23:23:23 +0000843#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000844 /*
845 ** PRAGMA data_store_directory
846 ** PRAGMA data_store_directory = ""|"directory_name"
847 **
848 ** Return or set the local value of the data_store_directory flag. Changing
849 ** the value sets a specific directory to be used for database files that
850 ** were specified with a relative pathname. Setting to a null string reverts
851 ** to the default database directory, which for database files specified with
852 ** a relative path will probably be based on the current directory for the
853 ** process. Database file specified with an absolute path are not impacted
854 ** by this setting, regardless of its value.
855 **
856 */
drh9ccd8652013-09-13 16:36:46 +0000857 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +0000858 if( !zRight ){
859 if( sqlite3_data_directory ){
860 sqlite3VdbeSetNumCols(v, 1);
861 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
862 "data_store_directory", SQLITE_STATIC);
863 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
864 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
865 }
866 }else{
867#ifndef SQLITE_OMIT_WSD
868 if( zRight[0] ){
869 int res;
870 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
871 if( rc!=SQLITE_OK || res==0 ){
872 sqlite3ErrorMsg(pParse, "not a writable directory");
873 goto pragma_out;
874 }
875 }
876 sqlite3_free(sqlite3_data_directory);
877 if( zRight[0] ){
878 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
879 }else{
880 sqlite3_data_directory = 0;
881 }
882#endif /* SQLITE_OMIT_WSD */
883 }
drh9ccd8652013-09-13 16:36:46 +0000884 break;
885 }
drhcc716452012-06-06 23:23:23 +0000886#endif
mistachkina112d142012-03-14 00:44:01 +0000887
drhd2cb50b2009-01-09 21:41:17 +0000888#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000889 /*
drh9ccd8652013-09-13 16:36:46 +0000890 ** PRAGMA [database.]lock_proxy_file
891 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
892 **
893 ** Return or set the value of the lock_proxy_file flag. Changing
894 ** the value sets a specific file to be used for database access locks.
895 **
896 */
897 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +0000898 if( !zRight ){
899 Pager *pPager = sqlite3BtreePager(pDb->pBt);
900 char *proxy_file_path = NULL;
901 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000902 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000903 &proxy_file_path);
904
905 if( proxy_file_path ){
906 sqlite3VdbeSetNumCols(v, 1);
907 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
908 "lock_proxy_file", SQLITE_STATIC);
909 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
910 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
911 }
912 }else{
913 Pager *pPager = sqlite3BtreePager(pDb->pBt);
914 sqlite3_file *pFile = sqlite3PagerFile(pPager);
915 int res;
916 if( zRight[0] ){
917 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
918 zRight);
919 } else {
920 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
921 NULL);
922 }
923 if( res!=SQLITE_OK ){
924 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
925 goto pragma_out;
926 }
927 }
drh9ccd8652013-09-13 16:36:46 +0000928 break;
929 }
drhd2cb50b2009-01-09 21:41:17 +0000930#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000931
932 /*
drh90f5ecb2004-07-22 01:19:35 +0000933 ** PRAGMA [database.]synchronous
934 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000935 **
936 ** Return or set the local value of the synchronous flag. Changing
937 ** the local value does not make changes to the disk file and the
938 ** default value will be restored the next time the database is
939 ** opened.
940 */
drh9ccd8652013-09-13 16:36:46 +0000941 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +0000942 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000943 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000944 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000945 if( !db->autoCommit ){
946 sqlite3ErrorMsg(pParse,
947 "Safety level may not be changed inside a transaction");
948 }else{
drh908c0052012-01-30 18:40:55 +0000949 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
drhd3605a42013-08-17 15:42:29 +0000950 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +0000951 }
drhc11d4f92003-04-06 21:08:24 +0000952 }
drh9ccd8652013-09-13 16:36:46 +0000953 break;
954 }
drh13d70422004-11-13 15:59:14 +0000955#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000956
drhbf216272005-02-26 18:10:44 +0000957#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +0000958 case PragTyp_FLAG: {
959 if( zRight==0 ){
drhc228be52015-01-31 02:00:01 +0000960 returnSingleInt(pParse, pPragma->zName, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +0000961 }else{
drhc228be52015-01-31 02:00:01 +0000962 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +0000963 if( db->autoCommit==0 ){
964 /* Foreign key support may not be enabled or disabled while not
965 ** in auto-commit mode. */
966 mask &= ~(SQLITE_ForeignKeys);
967 }
drhd4530972014-09-09 14:47:53 +0000968#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +0000969 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +0000970 /* Do not allow non-admin users to modify the schema arbitrarily */
971 mask &= ~(SQLITE_WriteSchema);
972 }
973#endif
drh9ccd8652013-09-13 16:36:46 +0000974
975 if( sqlite3GetBoolean(zRight, 0) ){
976 db->flags |= mask;
977 }else{
978 db->flags &= ~mask;
979 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
980 }
981
982 /* Many of the flag-pragmas modify the code generated by the SQL
983 ** compiler (eg. count_changes). So add an opcode to expire all
984 ** compiled SQL statements after modifying a pragma value.
985 */
986 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
987 setAllPagerFlags(db);
988 }
989 break;
990 }
drhbf216272005-02-26 18:10:44 +0000991#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000992
drh13d70422004-11-13 15:59:14 +0000993#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000994 /*
995 ** PRAGMA table_info(<table>)
996 **
997 ** Return a single row for each column of the named table. The columns of
998 ** the returned data set are:
999 **
1000 ** cid: Column id (numbered from left to right, starting at 0)
1001 ** name: Column name
1002 ** type: Column declaration type.
1003 ** notnull: True if 'NOT NULL' is part of column declaration
1004 ** dflt_value: The default value for the column, if any.
1005 */
drh9ccd8652013-09-13 16:36:46 +00001006 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001007 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001008 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001009 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001010 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001011 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001012 Column *pCol;
drh44156282013-10-23 22:23:03 +00001013 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh9f6696a2006-02-09 16:52:23 +00001014 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +00001015 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001016 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001017 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
1018 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1019 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
1020 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
1021 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
1022 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001023 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001024 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001025 if( IsHiddenColumn(pCol) ){
1026 nHidden++;
1027 continue;
1028 }
drh2d401ab2008-01-10 23:50:11 +00001029 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
1030 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
1031 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +00001032 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +00001033 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +00001034 if( pCol->zDflt ){
1035 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +00001036 }else{
drh2d401ab2008-01-10 23:50:11 +00001037 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +00001038 }
drh384b7fe2013-01-01 13:55:31 +00001039 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1040 k = 0;
1041 }else if( pPk==0 ){
1042 k = 1;
1043 }else{
drh1b678962015-04-15 07:19:27 +00001044 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
drh384b7fe2013-01-01 13:55:31 +00001045 }
1046 sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
drh2d401ab2008-01-10 23:50:11 +00001047 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001048 }
1049 }
drh9ccd8652013-09-13 16:36:46 +00001050 }
1051 break;
drhc11d4f92003-04-06 21:08:24 +00001052
drh3ef26152013-10-12 20:22:00 +00001053 case PragTyp_STATS: {
1054 Index *pIdx;
1055 HashElem *i;
1056 v = sqlite3GetVdbe(pParse);
1057 sqlite3VdbeSetNumCols(v, 4);
1058 pParse->nMem = 4;
1059 sqlite3CodeVerifySchema(pParse, iDb);
1060 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1061 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
1062 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
1063 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
1064 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1065 Table *pTab = sqliteHashData(i);
1066 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
1067 sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
1068 sqlite3VdbeAddOp2(v, OP_Integer,
1069 (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
dancfc9df72014-04-25 15:01:01 +00001070 sqlite3VdbeAddOp2(v, OP_Integer,
1071 (int)sqlite3LogEstToInt(pTab->nRowLogEst), 4);
drh3ef26152013-10-12 20:22:00 +00001072 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1073 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1074 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1075 sqlite3VdbeAddOp2(v, OP_Integer,
1076 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
dancfc9df72014-04-25 15:01:01 +00001077 sqlite3VdbeAddOp2(v, OP_Integer,
1078 (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]), 4);
drh3ef26152013-10-12 20:22:00 +00001079 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1080 }
1081 }
1082 }
1083 break;
1084
drh9ccd8652013-09-13 16:36:46 +00001085 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001086 Index *pIdx;
1087 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001088 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001089 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001090 int i;
drh5e7028c2015-03-05 14:29:02 +00001091 int mx;
1092 if( pPragma->iArg ){
1093 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1094 mx = pIdx->nColumn;
1095 pParse->nMem = 6;
1096 }else{
1097 /* PRAGMA index_info (legacy version) */
1098 mx = pIdx->nKeyCol;
1099 pParse->nMem = 3;
1100 }
drhc11d4f92003-04-06 21:08:24 +00001101 pTab = pIdx->pTable;
drh5e7028c2015-03-05 14:29:02 +00001102 sqlite3VdbeSetNumCols(v, pParse->nMem);
drhc95e01d2013-02-14 16:16:05 +00001103 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001104 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
1105 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
1106 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drh5e7028c2015-03-05 14:29:02 +00001107 if( pPragma->iArg ){
1108 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "desc", SQLITE_STATIC);
1109 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "coll", SQLITE_STATIC);
1110 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "key", SQLITE_STATIC);
1111 }
drhc228be52015-01-31 02:00:01 +00001112 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001113 i16 cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +00001114 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1115 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc228be52015-01-31 02:00:01 +00001116 if( cnum<0 ){
1117 sqlite3VdbeAddOp2(v, OP_Null, 0, 3);
1118 }else{
1119 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
1120 }
drh5e7028c2015-03-05 14:29:02 +00001121 if( pPragma->iArg ){
1122 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->aSortOrder[i], 4);
1123 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, pIdx->azColl[i], 0);
1124 sqlite3VdbeAddOp2(v, OP_Integer, i<pIdx->nKeyCol, 6);
1125 }
1126 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
drhc11d4f92003-04-06 21:08:24 +00001127 }
1128 }
drh9ccd8652013-09-13 16:36:46 +00001129 }
1130 break;
drhc11d4f92003-04-06 21:08:24 +00001131
drh9ccd8652013-09-13 16:36:46 +00001132 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001133 Index *pIdx;
1134 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001135 int i;
drh2783e4b2004-10-05 15:42:53 +00001136 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001137 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001138 v = sqlite3GetVdbe(pParse);
drhc228be52015-01-31 02:00:01 +00001139 sqlite3VdbeSetNumCols(v, 5);
1140 pParse->nMem = 5;
drhe13e9f52013-10-05 19:18:00 +00001141 sqlite3CodeVerifySchema(pParse, iDb);
1142 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1143 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1144 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
drhc228be52015-01-31 02:00:01 +00001145 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "origin", SQLITE_STATIC);
1146 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "partial", SQLITE_STATIC);
drh3ef26152013-10-12 20:22:00 +00001147 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001148 const char *azOrigin[] = { "c", "u", "pk" };
drhe13e9f52013-10-05 19:18:00 +00001149 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1150 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
drh5f1d1d92014-07-31 22:59:04 +00001151 sqlite3VdbeAddOp2(v, OP_Integer, IsUniqueIndex(pIdx), 3);
drhc228be52015-01-31 02:00:01 +00001152 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, azOrigin[pIdx->idxType], 0);
1153 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->pPartIdxWhere!=0, 5);
1154 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drhc11d4f92003-04-06 21:08:24 +00001155 }
1156 }
drh9ccd8652013-09-13 16:36:46 +00001157 }
1158 break;
drhc11d4f92003-04-06 21:08:24 +00001159
drh9ccd8652013-09-13 16:36:46 +00001160 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001161 int i;
drh13d70422004-11-13 15:59:14 +00001162 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001163 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +00001164 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1165 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1166 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +00001167 for(i=0; i<db->nDb; i++){
1168 if( db->aDb[i].pBt==0 ) continue;
1169 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +00001170 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1171 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
1172 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +00001173 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +00001174 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001175 }
drh9ccd8652013-09-13 16:36:46 +00001176 }
1177 break;
danielk197748af65a2005-02-09 03:20:37 +00001178
drh9ccd8652013-09-13 16:36:46 +00001179 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001180 int i = 0;
1181 HashElem *p;
1182 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001183 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001184 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1185 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +00001186 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1187 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001188 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1189 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1190 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001191 }
drh9ccd8652013-09-13 16:36:46 +00001192 }
1193 break;
drh13d70422004-11-13 15:59:14 +00001194#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1195
drhb7f91642004-10-31 02:22:47 +00001196#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001197 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001198 FKey *pFK;
1199 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001200 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001201 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001202 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001203 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001204 if( pFK ){
1205 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001206 sqlite3VdbeSetNumCols(v, 8);
1207 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001208 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001209 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1210 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1211 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1212 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1213 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1214 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1215 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1216 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001217 while(pFK){
1218 int j;
1219 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001220 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001221 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1222 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001223 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1224 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1225 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1226 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001227 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001228 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001229 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1230 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1231 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1232 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001233 }
1234 ++i;
1235 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001236 }
drh78100cc2003-08-23 22:40:53 +00001237 }
1238 }
drh9ccd8652013-09-13 16:36:46 +00001239 }
1240 break;
drhb7f91642004-10-31 02:22:47 +00001241#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001242
drh6c5b9152012-12-17 16:46:37 +00001243#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001244#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001245 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001246 FKey *pFK; /* A foreign key constraint */
1247 Table *pTab; /* Child table contain "REFERENCES" keyword */
1248 Table *pParent; /* Parent table that child points to */
1249 Index *pIdx; /* Index in the parent table */
1250 int i; /* Loop counter: Foreign key number for pTab */
1251 int j; /* Loop counter: Field of the foreign key */
1252 HashElem *k; /* Loop counter: Next table in schema */
1253 int x; /* result variable */
1254 int regResult; /* 3 registers to hold a result row */
1255 int regKey; /* Register to hold key for checking the FK */
1256 int regRow; /* Registers to hold a row from pTab */
1257 int addrTop; /* Top of a loop checking foreign keys */
1258 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001259 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001260
drh613028b2012-12-17 18:43:02 +00001261 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001262 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001263 regKey = ++pParse->nMem;
1264 regRow = ++pParse->nMem;
1265 v = sqlite3GetVdbe(pParse);
drh4b4b4732012-12-17 20:57:15 +00001266 sqlite3VdbeSetNumCols(v, 4);
drh613028b2012-12-17 18:43:02 +00001267 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1268 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
drh4b4b4732012-12-17 20:57:15 +00001269 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
1270 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
drh613028b2012-12-17 18:43:02 +00001271 sqlite3CodeVerifySchema(pParse, iDb);
1272 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1273 while( k ){
1274 if( zRight ){
1275 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1276 k = 0;
1277 }else{
1278 pTab = (Table*)sqliteHashData(k);
1279 k = sqliteHashNext(k);
1280 }
drh9148def2012-12-17 20:40:39 +00001281 if( pTab==0 || pTab->pFKey==0 ) continue;
1282 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001283 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001284 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh613028b2012-12-17 18:43:02 +00001285 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
1286 P4_TRANSIENT);
drh6c5b9152012-12-17 16:46:37 +00001287 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001288 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1289 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001290 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001291 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001292 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001293 if( x==0 ){
1294 if( pIdx==0 ){
1295 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1296 }else{
drh6c5b9152012-12-17 16:46:37 +00001297 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001298 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001299 }
1300 }else{
drh613028b2012-12-17 18:43:02 +00001301 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001302 break;
1303 }
drh6c5b9152012-12-17 16:46:37 +00001304 }
dan5e878302013-10-12 19:06:48 +00001305 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001306 if( pFK ) break;
1307 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001308 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001309 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001310 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001311 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001312 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001313 if( pParent ){
1314 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1315 assert( x==0 );
1316 }
drh613028b2012-12-17 18:43:02 +00001317 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001318 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001319 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001320 assert( iKey>=0 && iKey<pTab->nCol );
1321 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001322 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1323 sqlite3ColumnDefault(v, pTab, iKey, regRow);
drh688852a2014-02-17 22:40:43 +00001324 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
1325 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1326 sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001327 }else{
drh613028b2012-12-17 18:43:02 +00001328 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001329 }
drh688852a2014-02-17 22:40:43 +00001330 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001331 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
1332 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1333 }else{
1334 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001335 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001336 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh688852a2014-02-17 22:40:43 +00001337 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001338 }
dan5e878302013-10-12 19:06:48 +00001339 if( pParent ){
drh57bf4a82014-02-17 14:59:22 +00001340 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
1341 sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
dan5e878302013-10-12 19:06:48 +00001342 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh688852a2014-02-17 22:40:43 +00001343 VdbeCoverage(v);
dan5e878302013-10-12 19:06:48 +00001344 }
drh6c5b9152012-12-17 16:46:37 +00001345 }
drh613028b2012-12-17 18:43:02 +00001346 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh4b4b4732012-12-17 20:57:15 +00001347 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
1348 pFK->zTo, P4_TRANSIENT);
1349 sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
1350 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001351 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001352 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001353 }
drh688852a2014-02-17 22:40:43 +00001354 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001355 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001356 }
drh9ccd8652013-09-13 16:36:46 +00001357 }
1358 break;
dan09ff9e12013-03-11 11:49:03 +00001359#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001360#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1361
drhc11d4f92003-04-06 21:08:24 +00001362#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001363 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001364 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001365 if( sqlite3GetBoolean(zRight, 0) ){
drh55ef4d92005-08-14 01:20:37 +00001366 sqlite3ParserTrace(stderr, "parser: ");
1367 }else{
1368 sqlite3ParserTrace(0, 0);
1369 }
drhc11d4f92003-04-06 21:08:24 +00001370 }
drh9ccd8652013-09-13 16:36:46 +00001371 }
1372 break;
drhc11d4f92003-04-06 21:08:24 +00001373#endif
1374
drh55ef4d92005-08-14 01:20:37 +00001375 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1376 ** used will be case sensitive or not depending on the RHS.
1377 */
drh9ccd8652013-09-13 16:36:46 +00001378 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001379 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001380 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001381 }
drh9ccd8652013-09-13 16:36:46 +00001382 }
1383 break;
drh55ef4d92005-08-14 01:20:37 +00001384
drh1dcdbc02007-01-27 02:24:54 +00001385#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1386# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1387#endif
1388
drhb7f91642004-10-31 02:22:47 +00001389#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001390 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001391 ** integrity_check designed to detect most database corruption
1392 ** without most of the overhead of a full integrity-check.
1393 */
drh9ccd8652013-09-13 16:36:46 +00001394 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001395 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001396
drhed717fe2003-06-15 23:42:24 +00001397 /* Code that appears at the end of the integrity check. If no error
1398 ** messages have been generated, output OK. Otherwise output the
1399 ** error message
1400 */
drhb06a4ec2014-03-10 18:03:09 +00001401 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +00001402 static const VdbeOpList endCode[] = {
drh4336b0e2014-08-05 00:53:51 +00001403 { OP_IfNeg, 1, 0, 0}, /* 0 */
1404 { OP_String8, 0, 3, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001405 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001406 };
drhed717fe2003-06-15 23:42:24 +00001407
drhc5227312011-10-13 17:09:01 +00001408 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001409
dan5885e762012-07-16 10:06:12 +00001410 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1411 ** then iDb is set to the index of the database identified by <db>.
1412 ** In this case, the integrity of database iDb only is verified by
1413 ** the VDBE created below.
1414 **
1415 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1416 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1417 ** to -1 here, to indicate that the VDBE should verify the integrity
1418 ** of all attached databases. */
1419 assert( iDb>=0 );
1420 assert( iDb==0 || pId2->z );
1421 if( pId2->z==0 ) iDb = -1;
1422
drhed717fe2003-06-15 23:42:24 +00001423 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001424 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001425 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001426 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001427
1428 /* Set the maximum error count */
1429 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1430 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001431 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001432 if( mxErr<=0 ){
1433 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1434 }
1435 }
drh2d401ab2008-01-10 23:50:11 +00001436 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001437
1438 /* Do an integrity check on each database file */
1439 for(i=0; i<db->nDb; i++){
1440 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001441 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001442 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001443
danielk197753c0f742005-03-29 03:10:59 +00001444 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001445 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001446
drh80242052004-06-09 00:48:12 +00001447 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001448 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh688852a2014-02-17 22:40:43 +00001449 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001450 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001451 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001452
drhed717fe2003-06-15 23:42:24 +00001453 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001454 **
1455 ** Begin by filling registers 2, 3, ... with the root pages numbers
1456 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001457 */
dan5885e762012-07-16 10:06:12 +00001458 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001459 pTbls = &db->aDb[i].pSchema->tblHash;
1460 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001461 Table *pTab = sqliteHashData(x);
1462 Index *pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001463 if( HasRowid(pTab) ){
1464 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001465 VdbeComment((v, "%s", pTab->zName));
drh6fbe41a2013-10-30 20:22:55 +00001466 cnt++;
1467 }
drh79069752004-05-22 21:30:40 +00001468 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001469 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001470 VdbeComment((v, "%s", pIdx->zName));
drh79069752004-05-22 21:30:40 +00001471 cnt++;
1472 }
1473 }
drh2d401ab2008-01-10 23:50:11 +00001474
1475 /* Make sure sufficient number of registers have been allocated */
drh6fbe41a2013-10-30 20:22:55 +00001476 pParse->nMem = MAX( pParse->nMem, cnt+8 );
drh2d401ab2008-01-10 23:50:11 +00001477
1478 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001479 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001480 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001481 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001482 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001483 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001484 P4_DYNAMIC);
drh079a3072014-03-19 14:10:55 +00001485 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001486 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001487 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001488 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001489
1490 /* Make sure all the indices are constructed correctly.
1491 */
danielk197741c58b72007-12-29 13:39:19 +00001492 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001493 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001494 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001495 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001496 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001497 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001498 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001499
1500 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001501 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001502 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh688852a2014-02-17 22:40:43 +00001503 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001504 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001505 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001506 sqlite3ExprCacheClear(pParse);
drh26198bb2013-10-31 11:15:09 +00001507 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
drh6a534992013-11-16 20:13:39 +00001508 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001509 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001510 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001511 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001512 }
drh6fbe41a2013-10-30 20:22:55 +00001513 pParse->nMem = MAX(pParse->nMem, 8+j);
drh688852a2014-02-17 22:40:43 +00001514 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001515 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhcefc87f2014-08-01 01:40:33 +00001516 /* Verify that all NOT NULL columns really are NOT NULL */
1517 for(j=0; j<pTab->nCol; j++){
1518 char *zErr;
1519 int jmp2, jmp3;
1520 if( j==pTab->iPKey ) continue;
1521 if( pTab->aCol[j].notNull==0 ) continue;
1522 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1523 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1524 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1525 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1526 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1527 pTab->aCol[j].zName);
1528 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1529 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1530 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1531 sqlite3VdbeAddOp0(v, OP_Halt);
1532 sqlite3VdbeJumpHere(v, jmp2);
1533 sqlite3VdbeJumpHere(v, jmp3);
1534 }
1535 /* Validate index entries for the current row */
drhed717fe2003-06-15 23:42:24 +00001536 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhcefc87f2014-08-01 01:40:33 +00001537 int jmp2, jmp3, jmp4, jmp5;
1538 int ckUniq = sqlite3VdbeMakeLabel(v);
drh6fbe41a2013-10-30 20:22:55 +00001539 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001540 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1541 pPrior, r1);
1542 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001543 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drhcefc87f2014-08-01 01:40:33 +00001544 /* Verify that an index entry exists for the current table row */
1545 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
drh688852a2014-02-17 22:40:43 +00001546 pIdx->nColumn); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001547 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1548 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
1549 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
drhcefc87f2014-08-01 01:40:33 +00001550 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
1551 " missing from index ", P4_STATIC);
drh6fbe41a2013-10-30 20:22:55 +00001552 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drhcefc87f2014-08-01 01:40:33 +00001553 jmp5 = sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
1554 pIdx->zName, P4_TRANSIENT);
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];
1569 assert( iCol>=0 && iCol<pTab->nCol );
1570 if( pTab->aCol[iCol].notNull ) continue;
1571 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1572 VdbeCoverage(v);
1573 }
1574 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
1575 sqlite3VdbeAddOp2(v, OP_Goto, 0, uniqOk);
1576 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 */
1580 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
1581 "non-unique entry in index ", P4_STATIC);
1582 sqlite3VdbeAddOp2(v, OP_Goto, 0, jmp5);
1583 sqlite3VdbeResolveLabel(v, uniqOk);
1584 }
1585 sqlite3VdbeJumpHere(v, jmp4);
drh87744512014-04-13 19:15:49 +00001586 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhed717fe2003-06-15 23:42:24 +00001587 }
drh688852a2014-02-17 22:40:43 +00001588 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001589 sqlite3VdbeJumpHere(v, loopTop-1);
1590#ifndef SQLITE_OMIT_BTREECOUNT
1591 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
drh24003452008-01-03 01:28:59 +00001592 "wrong # of entries in index ", P4_STATIC);
drh8a9789b2013-08-01 03:36:59 +00001593 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001594 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001595 addr = sqlite3VdbeCurrentAddr(v);
drh688852a2014-02-17 22:40:43 +00001596 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001597 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001598 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh688852a2014-02-17 22:40:43 +00001599 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
drh5655c542014-02-19 19:14:34 +00001600 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh8a9789b2013-08-01 03:36:59 +00001601 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
1602 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
1603 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1604 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001605 }
drh8a9789b2013-08-01 03:36:59 +00001606#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001607 }
1608 }
drh688852a2014-02-17 22:40:43 +00001609 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
drh4336b0e2014-08-05 00:53:51 +00001610 sqlite3VdbeChangeP3(v, addr, -mxErr);
1611 sqlite3VdbeJumpHere(v, addr);
1612 sqlite3VdbeChangeP4(v, addr+1, "ok", P4_STATIC);
drh9ccd8652013-09-13 16:36:46 +00001613 }
1614 break;
drhb7f91642004-10-31 02:22:47 +00001615#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1616
drh13d70422004-11-13 15:59:14 +00001617#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001618 /*
1619 ** PRAGMA encoding
1620 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1621 **
drh85b623f2007-12-13 21:54:09 +00001622 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001623 ** database. If the database is not initialized, it is initialized now.
1624 **
1625 ** The second form of this pragma is a no-op if the main database file
1626 ** has not already been initialized. In this case it sets the default
1627 ** encoding that will be used for the main database file if a new file
1628 ** is created. If an existing main database file is opened, then the
1629 ** default text encoding for the existing database is used.
1630 **
1631 ** In all cases new databases created using the ATTACH command are
1632 ** created to use the same default text encoding as the main database. If
1633 ** the main database has not been initialized and/or created when ATTACH
1634 ** is executed, this is done before the ATTACH operation.
1635 **
1636 ** In the second form this pragma sets the text encoding to be used in
1637 ** new database files created using this database handle. It is only
1638 ** useful if invoked immediately after the main database i
1639 */
drh9ccd8652013-09-13 16:36:46 +00001640 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001641 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001642 char *zName;
1643 u8 enc;
1644 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001645 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001646 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1647 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1648 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001649 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001650 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001651 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1652 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001653 { 0, 0 }
1654 };
drh0f7eb612006-08-08 13:51:43 +00001655 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001656 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001657 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001658 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001659 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001660 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001661 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1662 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1663 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1664 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001665 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001666 }else{ /* "PRAGMA encoding = XXX" */
1667 /* Only change the value of sqlite.enc if the database handle is not
1668 ** initialized. If the main database exists, the new sqlite.enc value
1669 ** will be overwritten when the schema is next loaded. If it does not
1670 ** already exists, it will be created to use the new encoding value.
1671 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001672 if(
1673 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1674 DbHasProperty(db, 0, DB_Empty)
1675 ){
danielk19778e227872004-06-07 07:52:17 +00001676 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1677 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh9bd3cc42014-12-12 23:17:54 +00001678 SCHEMA_ENC(db) = ENC(db) =
1679 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001680 break;
1681 }
1682 }
1683 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001684 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001685 }
1686 }
1687 }
drh9ccd8652013-09-13 16:36:46 +00001688 }
1689 break;
drh13d70422004-11-13 15:59:14 +00001690#endif /* SQLITE_OMIT_UTF16 */
1691
1692#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001693 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001694 ** PRAGMA [database.]schema_version
1695 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001696 **
danielk1977b92b70b2004-11-12 16:11:59 +00001697 ** PRAGMA [database.]user_version
1698 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001699 **
drh4ee09b42013-05-01 19:49:27 +00001700 ** PRAGMA [database.]freelist_count = <integer>
1701 **
1702 ** PRAGMA [database.]application_id
1703 ** PRAGMA [database.]application_id = <integer>
1704 **
danielk1977b92b70b2004-11-12 16:11:59 +00001705 ** The pragma's schema_version and user_version are used to set or get
1706 ** the value of the schema-version and user-version, respectively. Both
1707 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001708 ** stored in the database header.
1709 **
1710 ** The schema-cookie is usually only manipulated internally by SQLite. It
1711 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001712 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001713 ** SQLite each time a query is executed to ensure that the internal cache
1714 ** of the schema used when compiling the SQL query matches the schema of
1715 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001716 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1717 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001718 ** crashes or database corruption. Use with caution!
1719 **
danielk1977b92b70b2004-11-12 16:11:59 +00001720 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001721 ** applications for any purpose.
1722 */
drh9ccd8652013-09-13 16:36:46 +00001723 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00001724 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00001725 sqlite3VdbeUsesBtree(v, iDb);
drhc228be52015-01-31 02:00:01 +00001726 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00001727 /* Write the specified cookie value */
1728 static const VdbeOpList setCookie[] = {
1729 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001730 { OP_Integer, 0, 1, 0}, /* 1 */
1731 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001732 };
drh688852a2014-02-17 22:40:43 +00001733 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00001734 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00001735 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00001736 sqlite3VdbeChangeP1(v, addr+2, iDb);
1737 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1738 }else{
1739 /* Read the specified cookie value */
1740 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001741 { OP_Transaction, 0, 0, 0}, /* 0 */
1742 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001743 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001744 };
drh688852a2014-02-17 22:40:43 +00001745 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00001746 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001747 sqlite3VdbeChangeP1(v, addr+1, iDb);
1748 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001749 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001750 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001751 }
drh9ccd8652013-09-13 16:36:46 +00001752 }
1753 break;
drh13d70422004-11-13 15:59:14 +00001754#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001755
shanehdc97a8c2010-02-23 20:08:35 +00001756#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1757 /*
1758 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001759 **
drh71caabf2010-02-26 15:39:24 +00001760 ** Return the names of all compile-time options used in this build,
1761 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001762 */
drh9ccd8652013-09-13 16:36:46 +00001763 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00001764 int i = 0;
1765 const char *zOpt;
1766 sqlite3VdbeSetNumCols(v, 1);
1767 pParse->nMem = 1;
1768 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1769 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1770 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1771 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1772 }
drh9ccd8652013-09-13 16:36:46 +00001773 }
1774 break;
shanehdc97a8c2010-02-23 20:08:35 +00001775#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1776
dan5cf53532010-05-01 16:40:20 +00001777#ifndef SQLITE_OMIT_WAL
1778 /*
danf26a1542014-12-02 19:04:54 +00001779 ** PRAGMA [database.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00001780 **
1781 ** Checkpoint the database.
1782 */
drh9ccd8652013-09-13 16:36:46 +00001783 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00001784 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001785 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1786 if( zRight ){
1787 if( sqlite3StrICmp(zRight, "full")==0 ){
1788 eMode = SQLITE_CHECKPOINT_FULL;
1789 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1790 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00001791 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1792 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00001793 }
1794 }
dancdc1f042010-11-18 12:11:05 +00001795 sqlite3VdbeSetNumCols(v, 3);
1796 pParse->nMem = 3;
1797 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
1798 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
1799 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
1800
drh30aa3b92011-02-07 23:56:01 +00001801 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001802 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00001803 }
1804 break;
dan5a299f92010-05-03 11:05:08 +00001805
1806 /*
1807 ** PRAGMA wal_autocheckpoint
1808 ** PRAGMA wal_autocheckpoint = N
1809 **
1810 ** Configure a database connection to automatically checkpoint a database
1811 ** after accumulating N frames in the log. Or query for the current value
1812 ** of N.
1813 */
drh9ccd8652013-09-13 16:36:46 +00001814 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00001815 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001816 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001817 }
drhb033d8b2010-05-03 13:37:30 +00001818 returnSingleInt(pParse, "wal_autocheckpoint",
1819 db->xWalCallback==sqlite3WalDefaultHook ?
1820 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00001821 }
1822 break;
dan5cf53532010-05-01 16:40:20 +00001823#endif
dan7c246102010-04-12 19:00:29 +00001824
drh09419b42011-11-16 19:29:17 +00001825 /*
1826 ** PRAGMA shrink_memory
1827 **
drh51a74d42015-02-28 01:04:27 +00001828 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
1829 ** connection on which it is invoked to free up as much memory as it
1830 ** can, by calling sqlite3_db_release_memory().
drh09419b42011-11-16 19:29:17 +00001831 */
drh9ccd8652013-09-13 16:36:46 +00001832 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00001833 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00001834 break;
1835 }
drh09419b42011-11-16 19:29:17 +00001836
drhf3603962012-09-07 16:46:59 +00001837 /*
1838 ** PRAGMA busy_timeout
1839 ** PRAGMA busy_timeout = N
1840 **
1841 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001842 ** if one is set. If no busy handler or a different busy handler is set
1843 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1844 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001845 */
drhd49c3582013-09-13 19:00:06 +00001846 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00001847 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00001848 if( zRight ){
1849 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1850 }
1851 returnSingleInt(pParse, "timeout", db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00001852 break;
1853 }
drhf3603962012-09-07 16:46:59 +00001854
drh55e85ca2013-09-13 21:01:56 +00001855 /*
1856 ** PRAGMA soft_heap_limit
1857 ** PRAGMA soft_heap_limit = N
1858 **
drh51a74d42015-02-28 01:04:27 +00001859 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
1860 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
1861 ** specified and is a non-negative integer.
1862 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
1863 ** returns the same integer that would be returned by the
1864 ** sqlite3_soft_heap_limit64(-1) C-language function.
drh55e85ca2013-09-13 21:01:56 +00001865 */
1866 case PragTyp_SOFT_HEAP_LIMIT: {
1867 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00001868 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00001869 sqlite3_soft_heap_limit64(N);
1870 }
1871 returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
1872 break;
1873 }
1874
drh03459612014-08-25 15:13:22 +00001875 /*
1876 ** PRAGMA threads
1877 ** PRAGMA threads = N
1878 **
1879 ** Configure the maximum number of worker threads. Return the new
1880 ** maximum, which might be less than requested.
1881 */
1882 case PragTyp_THREADS: {
1883 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00001884 if( zRight
drh03459612014-08-25 15:13:22 +00001885 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
1886 && N>=0
1887 ){
drh111544c2014-08-29 16:20:47 +00001888 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00001889 }
drh111544c2014-08-29 16:20:47 +00001890 returnSingleInt(pParse, "threads",
1891 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00001892 break;
1893 }
1894
dougcurrie81c95ef2004-06-18 23:21:47 +00001895#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001896 /*
1897 ** Report the current state of file logs for all databases
1898 */
drh9ccd8652013-09-13 16:36:46 +00001899 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00001900 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001901 "unlocked", "shared", "reserved", "pending", "exclusive"
1902 };
1903 int i;
drh89ac8c12004-06-09 14:17:20 +00001904 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001905 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001906 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1907 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001908 for(i=0; i<db->nDb; i++){
1909 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001910 const char *zState = "unknown";
1911 int j;
drh89ac8c12004-06-09 14:17:20 +00001912 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001913 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001914 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001915 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001916 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001917 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001918 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1919 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001920 }
drh2d401ab2008-01-10 23:50:11 +00001921 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1922 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001923 }
drh9ccd8652013-09-13 16:36:46 +00001924 break;
1925 }
drh89ac8c12004-06-09 14:17:20 +00001926#endif
1927
shanehad9f9f62010-02-17 17:48:46 +00001928#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00001929 case PragTyp_KEY: {
1930 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1931 break;
1932 }
1933 case PragTyp_REKEY: {
1934 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1935 break;
1936 }
1937 case PragTyp_HEXKEY: {
1938 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00001939 u8 iByte;
1940 int i;
drh9ccd8652013-09-13 16:36:46 +00001941 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00001942 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
1943 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
1944 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00001945 }
1946 if( (zLeft[3] & 0xf)==0xb ){
1947 sqlite3_key_v2(db, zDb, zKey, i/2);
1948 }else{
1949 sqlite3_rekey_v2(db, zDb, zKey, i/2);
1950 }
drhd2cb50b2009-01-09 21:41:17 +00001951 }
drh9ccd8652013-09-13 16:36:46 +00001952 break;
1953 }
drh3c4f2a42005-12-08 18:12:56 +00001954#endif
shanehad9f9f62010-02-17 17:48:46 +00001955#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00001956 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001957#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001958 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001959 sqlite3_activate_see(&zRight[4]);
1960 }
1961#endif
1962#ifdef SQLITE_ENABLE_CEROD
1963 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001964 sqlite3_activate_cerod(&zRight[6]);
1965 }
1966#endif
drh9ccd8652013-09-13 16:36:46 +00001967 }
1968 break;
drh21e2cab2006-09-25 18:01:57 +00001969#endif
drh3c4f2a42005-12-08 18:12:56 +00001970
drh9ccd8652013-09-13 16:36:46 +00001971 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00001972
danielk1977e0048402004-06-15 16:51:01 +00001973pragma_out:
drh633e6d52008-07-28 19:34:53 +00001974 sqlite3DbFree(db, zLeft);
1975 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001976}
drh13d70422004-11-13 15:59:14 +00001977
drh8bfdf722009-06-19 14:06:03 +00001978#endif /* SQLITE_OMIT_PRAGMA */