blob: ac217d45970c75ad2e8f42feb32287bffefc0e42 [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.
320 */
drh3fa97302012-02-22 16:58:36 +0000321 aFcntl[0] = 0;
322 aFcntl[1] = zLeft;
323 aFcntl[2] = zRight;
324 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000325 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000326 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
327 if( rc==SQLITE_OK ){
drh3fa97302012-02-22 16:58:36 +0000328 if( aFcntl[0] ){
329 int mem = ++pParse->nMem;
330 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
331 sqlite3VdbeSetNumCols(v, 1);
332 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
333 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
334 sqlite3_free(aFcntl[0]);
335 }
drh9ccd8652013-09-13 16:36:46 +0000336 goto pragma_out;
337 }
338 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000339 if( aFcntl[0] ){
340 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
341 sqlite3_free(aFcntl[0]);
342 }
343 pParse->nErr++;
344 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000345 goto pragma_out;
346 }
347
348 /* Locate the pragma in the lookup table */
349 lwr = 0;
350 upr = ArraySize(aPragmaNames)-1;
351 while( lwr<=upr ){
352 mid = (lwr+upr)/2;
353 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
354 if( rc==0 ) break;
355 if( rc<0 ){
356 upr = mid - 1;
357 }else{
358 lwr = mid + 1;
359 }
360 }
361 if( lwr>upr ) goto pragma_out;
drhc228be52015-01-31 02:00:01 +0000362 pPragma = &aPragmaNames[mid];
drh9ccd8652013-09-13 16:36:46 +0000363
drhf63936e2013-10-03 14:08:07 +0000364 /* Make sure the database schema is loaded if the pragma requires that */
drhc228be52015-01-31 02:00:01 +0000365 if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){
drhf63936e2013-10-03 14:08:07 +0000366 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
367 }
368
drh9ccd8652013-09-13 16:36:46 +0000369 /* Jump to the appropriate pragma handler */
drhc228be52015-01-31 02:00:01 +0000370 switch( pPragma->ePragTyp ){
drh9ccd8652013-09-13 16:36:46 +0000371
drhe73c9142011-11-09 16:12:24 +0000372#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000373 /*
drh90f5ecb2004-07-22 01:19:35 +0000374 ** PRAGMA [database.]default_cache_size
375 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000376 **
377 ** The first form reports the current persistent setting for the
378 ** page cache size. The value returned is the maximum number of
379 ** pages in the page cache. The second form sets both the current
380 ** page cache size value and the persistent page cache size value
381 ** stored in the database file.
382 **
drh93791ea2010-04-26 17:36:35 +0000383 ** Older versions of SQLite would set the default cache size to a
384 ** negative number to indicate synchronous=OFF. These days, synchronous
385 ** is always on by default regardless of the sign of the default cache
386 ** size. But continue to take the absolute value of the default cache
387 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000388 */
drh9ccd8652013-09-13 16:36:46 +0000389 case PragTyp_DEFAULT_CACHE_SIZE: {
drhb06a4ec2014-03-10 18:03:09 +0000390 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +0000391 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000392 { OP_Transaction, 0, 0, 0}, /* 0 */
393 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000394 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000395 { OP_Integer, 0, 2, 0},
396 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000397 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000398 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000399 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000400 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000401 };
drh905793e2004-02-21 13:31:09 +0000402 int addr;
drhfb982642007-08-30 01:19:59 +0000403 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000404 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000405 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000406 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000407 pParse->nMem += 2;
drh688852a2014-02-17 22:40:43 +0000408 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
danielk197791cf71b2004-06-26 06:37:06 +0000409 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000410 sqlite3VdbeChangeP1(v, addr+1, iDb);
411 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000412 }else{
drhd50ffc42011-03-08 02:38:28 +0000413 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000414 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000415 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000416 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000417 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000418 pDb->pSchema->cache_size = size;
419 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000420 }
drh9ccd8652013-09-13 16:36:46 +0000421 break;
422 }
drhe73c9142011-11-09 16:12:24 +0000423#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000424
drhe73c9142011-11-09 16:12:24 +0000425#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000426 /*
drh90f5ecb2004-07-22 01:19:35 +0000427 ** PRAGMA [database.]page_size
428 ** PRAGMA [database.]page_size=N
429 **
430 ** The first form reports the current setting for the
431 ** database page size in bytes. The second form sets the
432 ** database page size value. The value can only be set if
433 ** the database has not yet been created.
434 */
drh9ccd8652013-09-13 16:36:46 +0000435 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000436 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000437 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000438 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000439 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000440 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000441 }else{
danielk1977992772c2007-08-30 10:07:38 +0000442 /* Malloc may fail when setting the page-size, as there is an internal
443 ** buffer that the pager module resizes using sqlite3_realloc().
444 */
drh60ac3f42010-11-23 18:59:27 +0000445 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000446 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000447 db->mallocFailed = 1;
448 }
drh90f5ecb2004-07-22 01:19:35 +0000449 }
drh9ccd8652013-09-13 16:36:46 +0000450 break;
451 }
danielk197741483462007-03-24 16:45:04 +0000452
453 /*
drh5b47efa2010-02-12 18:18:39 +0000454 ** PRAGMA [database.]secure_delete
455 ** PRAGMA [database.]secure_delete=ON/OFF
456 **
457 ** The first form reports the current setting for the
458 ** secure_delete flag. The second form changes the secure_delete
459 ** flag setting and reports thenew value.
460 */
drh9ccd8652013-09-13 16:36:46 +0000461 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000462 Btree *pBt = pDb->pBt;
463 int b = -1;
464 assert( pBt!=0 );
465 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000466 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000467 }
drhaf034ed2010-02-12 19:46:26 +0000468 if( pId2->n==0 && b>=0 ){
469 int ii;
470 for(ii=0; ii<db->nDb; ii++){
471 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
472 }
473 }
drh5b47efa2010-02-12 18:18:39 +0000474 b = sqlite3BtreeSecureDelete(pBt, b);
475 returnSingleInt(pParse, "secure_delete", b);
drh9ccd8652013-09-13 16:36:46 +0000476 break;
477 }
drh5b47efa2010-02-12 18:18:39 +0000478
479 /*
drh60ac3f42010-11-23 18:59:27 +0000480 ** PRAGMA [database.]max_page_count
481 ** PRAGMA [database.]max_page_count=N
482 **
483 ** The first form reports the current setting for the
484 ** maximum number of pages in the database file. The
485 ** second form attempts to change this setting. Both
486 ** forms return the current setting.
487 **
drhe73c9142011-11-09 16:12:24 +0000488 ** The absolute value of N is used. This is undocumented and might
489 ** change. The only purpose is to provide an easy way to test
490 ** the sqlite3AbsInt32() function.
491 **
danielk197759a93792008-05-15 17:48:20 +0000492 ** PRAGMA [database.]page_count
493 **
494 ** Return the number of pages in the specified database.
495 */
drh9ccd8652013-09-13 16:36:46 +0000496 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000497 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000498 sqlite3CodeVerifySchema(pParse, iDb);
499 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000500 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000501 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
502 }else{
drhe73c9142011-11-09 16:12:24 +0000503 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
504 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000505 }
danielk197759a93792008-05-15 17:48:20 +0000506 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
507 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000508 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
drh9ccd8652013-09-13 16:36:46 +0000509 break;
510 }
danielk197759a93792008-05-15 17:48:20 +0000511
512 /*
danielk197741483462007-03-24 16:45:04 +0000513 ** PRAGMA [database.]locking_mode
514 ** PRAGMA [database.]locking_mode = (normal|exclusive)
515 */
drh9ccd8652013-09-13 16:36:46 +0000516 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000517 const char *zRet = "normal";
518 int eMode = getLockingMode(zRight);
519
520 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
521 /* Simple "PRAGMA locking_mode;" statement. This is a query for
522 ** the current default locking mode (which may be different to
523 ** the locking-mode of the main database).
524 */
525 eMode = db->dfltLockMode;
526 }else{
527 Pager *pPager;
528 if( pId2->n==0 ){
529 /* This indicates that no database name was specified as part
530 ** of the PRAGMA command. In this case the locking-mode must be
531 ** set on all attached databases, as well as the main db file.
532 **
533 ** Also, the sqlite3.dfltLockMode variable is set so that
534 ** any subsequently attached databases also use the specified
535 ** locking mode.
536 */
537 int ii;
538 assert(pDb==&db->aDb[0]);
539 for(ii=2; ii<db->nDb; ii++){
540 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
541 sqlite3PagerLockingMode(pPager, eMode);
542 }
drh4f21c4a2008-12-10 22:15:00 +0000543 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000544 }
545 pPager = sqlite3BtreePager(pDb->pBt);
546 eMode = sqlite3PagerLockingMode(pPager, eMode);
547 }
548
drh9ccd8652013-09-13 16:36:46 +0000549 assert( eMode==PAGER_LOCKINGMODE_NORMAL
550 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000551 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
552 zRet = "exclusive";
553 }
554 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000555 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000556 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
557 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000558 break;
559 }
drh3b020132008-04-17 17:02:01 +0000560
561 /*
562 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000563 ** PRAGMA [database.]journal_mode =
564 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000565 */
drh9ccd8652013-09-13 16:36:46 +0000566 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000567 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
568 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000569
570 sqlite3VdbeSetNumCols(v, 1);
571 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000572
573 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000574 /* If there is no "=MODE" part of the pragma, do a query for the
575 ** current mode */
drh3b020132008-04-17 17:02:01 +0000576 eMode = PAGER_JOURNALMODE_QUERY;
577 }else{
dane04dc882010-04-20 18:53:15 +0000578 const char *zMode;
drhea678832008-12-10 19:26:22 +0000579 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000580 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000581 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
582 }
583 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000584 /* If the "=MODE" part does not match any known journal mode,
585 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000586 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000587 }
588 }
drhc6b2a0f2010-07-08 17:40:37 +0000589 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
590 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
591 iDb = 0;
592 pId2->n = 1;
593 }
594 for(ii=db->nDb-1; ii>=0; ii--){
595 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
596 sqlite3VdbeUsesBtree(v, ii);
597 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000598 }
drh3b020132008-04-17 17:02:01 +0000599 }
drh3b020132008-04-17 17:02:01 +0000600 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000601 break;
602 }
danielk1977b53e4962008-06-04 06:45:59 +0000603
604 /*
605 ** PRAGMA [database.]journal_size_limit
606 ** PRAGMA [database.]journal_size_limit=N
607 **
drha9e364f2009-01-13 20:14:15 +0000608 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000609 */
drh9ccd8652013-09-13 16:36:46 +0000610 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000611 Pager *pPager = sqlite3BtreePager(pDb->pBt);
612 i64 iLimit = -2;
613 if( zRight ){
drh9296c182014-07-23 13:40:49 +0000614 sqlite3DecOrHexToI64(zRight, &iLimit);
drh3c713642009-04-04 16:02:32 +0000615 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000616 }
617 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000618 returnSingleInt(pParse, "journal_size_limit", iLimit);
drh9ccd8652013-09-13 16:36:46 +0000619 break;
620 }
danielk1977b53e4962008-06-04 06:45:59 +0000621
drh13d70422004-11-13 15:59:14 +0000622#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000623
624 /*
danielk1977951af802004-11-05 15:45:09 +0000625 ** PRAGMA [database.]auto_vacuum
626 ** PRAGMA [database.]auto_vacuum=N
627 **
drha9e364f2009-01-13 20:14:15 +0000628 ** Get or set the value of the database 'auto-vacuum' parameter.
629 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000630 */
631#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000632 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +0000633 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000634 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +0000635 if( !zRight ){
drhf63936e2013-10-03 14:08:07 +0000636 returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +0000637 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000638 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000639 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000640 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +0000641 /* Call SetAutoVacuum() to set initialize the internal auto and
642 ** incr-vacuum flags. This is required in case this connection
643 ** creates the database file. It is important that it is created
644 ** as an auto-vacuum capable db.
645 */
646 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
647 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
648 /* When setting the auto_vacuum mode to either "full" or
649 ** "incremental", write the value of meta[6] in the database
650 ** file. Before writing to meta[6], check that meta[3] indicates
651 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +0000652 */
drhb06a4ec2014-03-10 18:03:09 +0000653 static const int iLn = VDBE_OFFSET_LINENO(2);
drhf63936e2013-10-03 14:08:07 +0000654 static const VdbeOpList setMeta6[] = {
655 { OP_Transaction, 0, 1, 0}, /* 0 */
656 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
657 { OP_If, 1, 0, 0}, /* 2 */
658 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
659 { OP_Integer, 0, 1, 0}, /* 4 */
660 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
661 };
662 int iAddr;
drh688852a2014-02-17 22:40:43 +0000663 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhf63936e2013-10-03 14:08:07 +0000664 sqlite3VdbeChangeP1(v, iAddr, iDb);
665 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
666 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
667 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
668 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
669 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +0000670 }
danielk1977951af802004-11-05 15:45:09 +0000671 }
drh9ccd8652013-09-13 16:36:46 +0000672 break;
673 }
danielk1977951af802004-11-05 15:45:09 +0000674#endif
675
drhca5557f2007-05-04 18:30:40 +0000676 /*
677 ** PRAGMA [database.]incremental_vacuum(N)
678 **
679 ** Do N steps of incremental vacuuming on a database.
680 */
681#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +0000682 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +0000683 int iLimit, addr;
684 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
685 iLimit = 0x7fffffff;
686 }
687 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000688 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +0000689 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +0000690 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000691 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +0000692 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +0000693 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +0000694 break;
695 }
drhca5557f2007-05-04 18:30:40 +0000696#endif
697
drh13d70422004-11-13 15:59:14 +0000698#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000699 /*
drh90f5ecb2004-07-22 01:19:35 +0000700 ** PRAGMA [database.]cache_size
701 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000702 **
703 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000704 ** page cache size. The second form sets the local
705 ** page cache size value. If N is positive then that is the
706 ** number of pages in the cache. If N is negative, then the
707 ** number of pages is adjusted so that the cache uses -N kibibytes
708 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000709 */
drh9ccd8652013-09-13 16:36:46 +0000710 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +0000711 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000712 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000713 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000714 }else{
drh3b42abb2011-11-09 14:23:04 +0000715 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000716 pDb->pSchema->cache_size = size;
717 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000718 }
drh9ccd8652013-09-13 16:36:46 +0000719 break;
720 }
drhc11d4f92003-04-06 21:08:24 +0000721
722 /*
drh9b4c59f2013-04-15 17:03:42 +0000723 ** PRAGMA [database.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000724 **
drh0d0614b2013-03-25 23:09:28 +0000725 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000726 ** used to limit the aggregate size of all memory mapped regions of the
727 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000728 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000729 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000730 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000731 **
drh0d0614b2013-03-25 23:09:28 +0000732 ** This value is advisory. The underlying VFS is free to memory map
733 ** as little or as much as it wants. Except, if N is set to 0 then the
734 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000735 */
drh9ccd8652013-09-13 16:36:46 +0000736 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +0000737 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000738#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000739 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000740 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000741 int ii;
drh9296c182014-07-23 13:40:49 +0000742 sqlite3DecOrHexToI64(zRight, &sz);
drh9b4c59f2013-04-15 17:03:42 +0000743 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
744 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000745 for(ii=db->nDb-1; ii>=0; ii--){
746 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000747 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000748 }
749 }
dan5d8a1372013-03-19 19:28:06 +0000750 }
drh9b4c59f2013-04-15 17:03:42 +0000751 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000752 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000753#else
dan3719f5f2013-05-23 10:13:18 +0000754 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000755 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000756#endif
dan3719f5f2013-05-23 10:13:18 +0000757 if( rc==SQLITE_OK ){
drh9b4c59f2013-04-15 17:03:42 +0000758 returnSingleInt(pParse, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +0000759 }else if( rc!=SQLITE_NOTFOUND ){
760 pParse->nErr++;
761 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000762 }
drh9ccd8652013-09-13 16:36:46 +0000763 break;
764 }
dan5d8a1372013-03-19 19:28:06 +0000765
766 /*
drh90f5ecb2004-07-22 01:19:35 +0000767 ** PRAGMA temp_store
768 ** PRAGMA temp_store = "default"|"memory"|"file"
769 **
770 ** Return or set the local value of the temp_store flag. Changing
771 ** the local value does not make changes to the disk file and the default
772 ** value will be restored the next time the database is opened.
773 **
774 ** Note that it is possible for the library compile-time options to
775 ** override this setting
776 */
drh9ccd8652013-09-13 16:36:46 +0000777 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +0000778 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000779 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000780 }else{
781 changeTempStorage(pParse, zRight);
782 }
drh9ccd8652013-09-13 16:36:46 +0000783 break;
784 }
drh90f5ecb2004-07-22 01:19:35 +0000785
786 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000787 ** PRAGMA temp_store_directory
788 ** PRAGMA temp_store_directory = ""|"directory_name"
789 **
790 ** Return or set the local value of the temp_store_directory flag. Changing
791 ** the value sets a specific directory to be used for temporary files.
792 ** Setting to a null string reverts to the default temporary directory search.
793 ** If temporary directory is changed, then invalidateTempStorage.
794 **
795 */
drh9ccd8652013-09-13 16:36:46 +0000796 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +0000797 if( !zRight ){
798 if( sqlite3_temp_directory ){
799 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000800 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000801 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000802 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
803 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000804 }
805 }else{
drh78f82d12008-09-02 00:52:52 +0000806#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000807 if( zRight[0] ){
808 int res;
danielk1977fab11272008-09-16 14:38:02 +0000809 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
810 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000811 sqlite3ErrorMsg(pParse, "not a writable directory");
812 goto pragma_out;
813 }
drh268283b2005-01-08 15:44:25 +0000814 }
danielk1977b06a0b62008-06-26 10:54:12 +0000815 if( SQLITE_TEMP_STORE==0
816 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
817 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000818 ){
819 invalidateTempStorage(pParse);
820 }
drh17435752007-08-16 04:30:38 +0000821 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000822 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000823 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000824 }else{
drh268283b2005-01-08 15:44:25 +0000825 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000826 }
drh78f82d12008-09-02 00:52:52 +0000827#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000828 }
drh9ccd8652013-09-13 16:36:46 +0000829 break;
830 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000831
drhcc716452012-06-06 23:23:23 +0000832#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000833 /*
834 ** PRAGMA data_store_directory
835 ** PRAGMA data_store_directory = ""|"directory_name"
836 **
837 ** Return or set the local value of the data_store_directory flag. Changing
838 ** the value sets a specific directory to be used for database files that
839 ** were specified with a relative pathname. Setting to a null string reverts
840 ** to the default database directory, which for database files specified with
841 ** a relative path will probably be based on the current directory for the
842 ** process. Database file specified with an absolute path are not impacted
843 ** by this setting, regardless of its value.
844 **
845 */
drh9ccd8652013-09-13 16:36:46 +0000846 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +0000847 if( !zRight ){
848 if( sqlite3_data_directory ){
849 sqlite3VdbeSetNumCols(v, 1);
850 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
851 "data_store_directory", SQLITE_STATIC);
852 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
853 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
854 }
855 }else{
856#ifndef SQLITE_OMIT_WSD
857 if( zRight[0] ){
858 int res;
859 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
860 if( rc!=SQLITE_OK || res==0 ){
861 sqlite3ErrorMsg(pParse, "not a writable directory");
862 goto pragma_out;
863 }
864 }
865 sqlite3_free(sqlite3_data_directory);
866 if( zRight[0] ){
867 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
868 }else{
869 sqlite3_data_directory = 0;
870 }
871#endif /* SQLITE_OMIT_WSD */
872 }
drh9ccd8652013-09-13 16:36:46 +0000873 break;
874 }
drhcc716452012-06-06 23:23:23 +0000875#endif
mistachkina112d142012-03-14 00:44:01 +0000876
drhd2cb50b2009-01-09 21:41:17 +0000877#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000878 /*
drh9ccd8652013-09-13 16:36:46 +0000879 ** PRAGMA [database.]lock_proxy_file
880 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
881 **
882 ** Return or set the value of the lock_proxy_file flag. Changing
883 ** the value sets a specific file to be used for database access locks.
884 **
885 */
886 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +0000887 if( !zRight ){
888 Pager *pPager = sqlite3BtreePager(pDb->pBt);
889 char *proxy_file_path = NULL;
890 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000891 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000892 &proxy_file_path);
893
894 if( proxy_file_path ){
895 sqlite3VdbeSetNumCols(v, 1);
896 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
897 "lock_proxy_file", SQLITE_STATIC);
898 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
899 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
900 }
901 }else{
902 Pager *pPager = sqlite3BtreePager(pDb->pBt);
903 sqlite3_file *pFile = sqlite3PagerFile(pPager);
904 int res;
905 if( zRight[0] ){
906 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
907 zRight);
908 } else {
909 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
910 NULL);
911 }
912 if( res!=SQLITE_OK ){
913 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
914 goto pragma_out;
915 }
916 }
drh9ccd8652013-09-13 16:36:46 +0000917 break;
918 }
drhd2cb50b2009-01-09 21:41:17 +0000919#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000920
921 /*
drh90f5ecb2004-07-22 01:19:35 +0000922 ** PRAGMA [database.]synchronous
923 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000924 **
925 ** Return or set the local value of the synchronous flag. Changing
926 ** the local value does not make changes to the disk file and the
927 ** default value will be restored the next time the database is
928 ** opened.
929 */
drh9ccd8652013-09-13 16:36:46 +0000930 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +0000931 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000932 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000933 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000934 if( !db->autoCommit ){
935 sqlite3ErrorMsg(pParse,
936 "Safety level may not be changed inside a transaction");
937 }else{
drh908c0052012-01-30 18:40:55 +0000938 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
drhd3605a42013-08-17 15:42:29 +0000939 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +0000940 }
drhc11d4f92003-04-06 21:08:24 +0000941 }
drh9ccd8652013-09-13 16:36:46 +0000942 break;
943 }
drh13d70422004-11-13 15:59:14 +0000944#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000945
drhbf216272005-02-26 18:10:44 +0000946#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +0000947 case PragTyp_FLAG: {
948 if( zRight==0 ){
drhc228be52015-01-31 02:00:01 +0000949 returnSingleInt(pParse, pPragma->zName, (db->flags & pPragma->iArg)!=0 );
drh9ccd8652013-09-13 16:36:46 +0000950 }else{
drhc228be52015-01-31 02:00:01 +0000951 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
drh9ccd8652013-09-13 16:36:46 +0000952 if( db->autoCommit==0 ){
953 /* Foreign key support may not be enabled or disabled while not
954 ** in auto-commit mode. */
955 mask &= ~(SQLITE_ForeignKeys);
956 }
drhd4530972014-09-09 14:47:53 +0000957#if SQLITE_USER_AUTHENTICATION
drhb2445d52014-09-11 14:01:41 +0000958 if( db->auth.authLevel==UAUTH_User ){
drhd4530972014-09-09 14:47:53 +0000959 /* Do not allow non-admin users to modify the schema arbitrarily */
960 mask &= ~(SQLITE_WriteSchema);
961 }
962#endif
drh9ccd8652013-09-13 16:36:46 +0000963
964 if( sqlite3GetBoolean(zRight, 0) ){
965 db->flags |= mask;
966 }else{
967 db->flags &= ~mask;
968 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
969 }
970
971 /* Many of the flag-pragmas modify the code generated by the SQL
972 ** compiler (eg. count_changes). So add an opcode to expire all
973 ** compiled SQL statements after modifying a pragma value.
974 */
975 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
976 setAllPagerFlags(db);
977 }
978 break;
979 }
drhbf216272005-02-26 18:10:44 +0000980#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000981
drh13d70422004-11-13 15:59:14 +0000982#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000983 /*
984 ** PRAGMA table_info(<table>)
985 **
986 ** Return a single row for each column of the named table. The columns of
987 ** the returned data set are:
988 **
989 ** cid: Column id (numbered from left to right, starting at 0)
990 ** name: Column name
991 ** type: Column declaration type.
992 ** notnull: True if 'NOT NULL' is part of column declaration
993 ** dflt_value: The default value for the column, if any.
994 */
drh9ccd8652013-09-13 16:36:46 +0000995 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +0000996 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +0000997 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000998 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +0000999 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001000 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001001 Column *pCol;
drh44156282013-10-23 22:23:03 +00001002 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh9f6696a2006-02-09 16:52:23 +00001003 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +00001004 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001005 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001006 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
1007 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1008 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
1009 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
1010 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
1011 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001012 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001013 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001014 if( IsHiddenColumn(pCol) ){
1015 nHidden++;
1016 continue;
1017 }
drh2d401ab2008-01-10 23:50:11 +00001018 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
1019 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
1020 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +00001021 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +00001022 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +00001023 if( pCol->zDflt ){
1024 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +00001025 }else{
drh2d401ab2008-01-10 23:50:11 +00001026 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +00001027 }
drh384b7fe2013-01-01 13:55:31 +00001028 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1029 k = 0;
1030 }else if( pPk==0 ){
1031 k = 1;
1032 }else{
1033 for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
1034 }
1035 sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
drh2d401ab2008-01-10 23:50:11 +00001036 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001037 }
1038 }
drh9ccd8652013-09-13 16:36:46 +00001039 }
1040 break;
drhc11d4f92003-04-06 21:08:24 +00001041
drh3ef26152013-10-12 20:22:00 +00001042 case PragTyp_STATS: {
1043 Index *pIdx;
1044 HashElem *i;
1045 v = sqlite3GetVdbe(pParse);
1046 sqlite3VdbeSetNumCols(v, 4);
1047 pParse->nMem = 4;
1048 sqlite3CodeVerifySchema(pParse, iDb);
1049 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1050 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
1051 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
1052 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
1053 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1054 Table *pTab = sqliteHashData(i);
1055 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
1056 sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
1057 sqlite3VdbeAddOp2(v, OP_Integer,
1058 (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
dancfc9df72014-04-25 15:01:01 +00001059 sqlite3VdbeAddOp2(v, OP_Integer,
1060 (int)sqlite3LogEstToInt(pTab->nRowLogEst), 4);
drh3ef26152013-10-12 20:22:00 +00001061 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1062 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1063 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1064 sqlite3VdbeAddOp2(v, OP_Integer,
1065 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
dancfc9df72014-04-25 15:01:01 +00001066 sqlite3VdbeAddOp2(v, OP_Integer,
1067 (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]), 4);
drh3ef26152013-10-12 20:22:00 +00001068 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1069 }
1070 }
1071 }
1072 break;
1073
drh9ccd8652013-09-13 16:36:46 +00001074 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001075 Index *pIdx;
1076 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001077 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001078 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001079 int i;
drhc228be52015-01-31 02:00:01 +00001080 int mx = pPragma->iArg ? pIdx->nColumn : pIdx->nKeyCol;
drhc11d4f92003-04-06 21:08:24 +00001081 pTab = pIdx->pTable;
drhc228be52015-01-31 02:00:01 +00001082 sqlite3VdbeSetNumCols(v, 6);
1083 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001084 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001085 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
1086 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
1087 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc228be52015-01-31 02:00:01 +00001088 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "desc", SQLITE_STATIC);
1089 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "coll", SQLITE_STATIC);
1090 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "key", SQLITE_STATIC);
1091 for(i=0; i<mx; i++){
drhbbbdc832013-10-22 18:01:40 +00001092 i16 cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +00001093 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1094 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc228be52015-01-31 02:00:01 +00001095 if( cnum<0 ){
1096 sqlite3VdbeAddOp2(v, OP_Null, 0, 3);
1097 }else{
1098 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
1099 }
1100 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->aSortOrder[i], 4);
1101 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, pIdx->azColl[i], 0);
1102 sqlite3VdbeAddOp2(v, OP_Integer, i<pIdx->nKeyCol, 6);
1103 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001104 }
1105 }
drh9ccd8652013-09-13 16:36:46 +00001106 }
1107 break;
drhc11d4f92003-04-06 21:08:24 +00001108
drh9ccd8652013-09-13 16:36:46 +00001109 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001110 Index *pIdx;
1111 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001112 int i;
drh2783e4b2004-10-05 15:42:53 +00001113 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001114 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001115 v = sqlite3GetVdbe(pParse);
drhc228be52015-01-31 02:00:01 +00001116 sqlite3VdbeSetNumCols(v, 5);
1117 pParse->nMem = 5;
drhe13e9f52013-10-05 19:18:00 +00001118 sqlite3CodeVerifySchema(pParse, iDb);
1119 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1120 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1121 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
drhc228be52015-01-31 02:00:01 +00001122 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "origin", SQLITE_STATIC);
1123 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "partial", SQLITE_STATIC);
drh3ef26152013-10-12 20:22:00 +00001124 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhc228be52015-01-31 02:00:01 +00001125 const char *azOrigin[] = { "c", "u", "pk" };
drhe13e9f52013-10-05 19:18:00 +00001126 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1127 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
drh5f1d1d92014-07-31 22:59:04 +00001128 sqlite3VdbeAddOp2(v, OP_Integer, IsUniqueIndex(pIdx), 3);
drhc228be52015-01-31 02:00:01 +00001129 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, azOrigin[pIdx->idxType], 0);
1130 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->pPartIdxWhere!=0, 5);
1131 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
drhc11d4f92003-04-06 21:08:24 +00001132 }
1133 }
drh9ccd8652013-09-13 16:36:46 +00001134 }
1135 break;
drhc11d4f92003-04-06 21:08:24 +00001136
drh9ccd8652013-09-13 16:36:46 +00001137 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001138 int i;
drh13d70422004-11-13 15:59:14 +00001139 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001140 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +00001141 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1142 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1143 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +00001144 for(i=0; i<db->nDb; i++){
1145 if( db->aDb[i].pBt==0 ) continue;
1146 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +00001147 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1148 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
1149 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +00001150 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +00001151 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001152 }
drh9ccd8652013-09-13 16:36:46 +00001153 }
1154 break;
danielk197748af65a2005-02-09 03:20:37 +00001155
drh9ccd8652013-09-13 16:36:46 +00001156 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001157 int i = 0;
1158 HashElem *p;
1159 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001160 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001161 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1162 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +00001163 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1164 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001165 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1166 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1167 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001168 }
drh9ccd8652013-09-13 16:36:46 +00001169 }
1170 break;
drh13d70422004-11-13 15:59:14 +00001171#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1172
drhb7f91642004-10-31 02:22:47 +00001173#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001174 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001175 FKey *pFK;
1176 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001177 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001178 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001179 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001180 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001181 if( pFK ){
1182 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001183 sqlite3VdbeSetNumCols(v, 8);
1184 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001185 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001186 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1187 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1188 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1189 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1190 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1191 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1192 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1193 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001194 while(pFK){
1195 int j;
1196 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001197 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001198 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1199 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001200 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1201 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1202 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1203 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001204 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001205 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001206 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1207 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1208 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1209 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001210 }
1211 ++i;
1212 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001213 }
drh78100cc2003-08-23 22:40:53 +00001214 }
1215 }
drh9ccd8652013-09-13 16:36:46 +00001216 }
1217 break;
drhb7f91642004-10-31 02:22:47 +00001218#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001219
drh6c5b9152012-12-17 16:46:37 +00001220#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001221#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001222 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001223 FKey *pFK; /* A foreign key constraint */
1224 Table *pTab; /* Child table contain "REFERENCES" keyword */
1225 Table *pParent; /* Parent table that child points to */
1226 Index *pIdx; /* Index in the parent table */
1227 int i; /* Loop counter: Foreign key number for pTab */
1228 int j; /* Loop counter: Field of the foreign key */
1229 HashElem *k; /* Loop counter: Next table in schema */
1230 int x; /* result variable */
1231 int regResult; /* 3 registers to hold a result row */
1232 int regKey; /* Register to hold key for checking the FK */
1233 int regRow; /* Registers to hold a row from pTab */
1234 int addrTop; /* Top of a loop checking foreign keys */
1235 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001236 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001237
drh613028b2012-12-17 18:43:02 +00001238 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001239 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001240 regKey = ++pParse->nMem;
1241 regRow = ++pParse->nMem;
1242 v = sqlite3GetVdbe(pParse);
drh4b4b4732012-12-17 20:57:15 +00001243 sqlite3VdbeSetNumCols(v, 4);
drh613028b2012-12-17 18:43:02 +00001244 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1245 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
drh4b4b4732012-12-17 20:57:15 +00001246 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
1247 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
drh613028b2012-12-17 18:43:02 +00001248 sqlite3CodeVerifySchema(pParse, iDb);
1249 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1250 while( k ){
1251 if( zRight ){
1252 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1253 k = 0;
1254 }else{
1255 pTab = (Table*)sqliteHashData(k);
1256 k = sqliteHashNext(k);
1257 }
drh9148def2012-12-17 20:40:39 +00001258 if( pTab==0 || pTab->pFKey==0 ) continue;
1259 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001260 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001261 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh613028b2012-12-17 18:43:02 +00001262 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
1263 P4_TRANSIENT);
drh6c5b9152012-12-17 16:46:37 +00001264 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001265 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1266 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001267 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001268 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001269 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001270 if( x==0 ){
1271 if( pIdx==0 ){
1272 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1273 }else{
drh6c5b9152012-12-17 16:46:37 +00001274 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001275 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001276 }
1277 }else{
drh613028b2012-12-17 18:43:02 +00001278 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001279 break;
1280 }
drh6c5b9152012-12-17 16:46:37 +00001281 }
dan5e878302013-10-12 19:06:48 +00001282 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001283 if( pFK ) break;
1284 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001285 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001286 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001287 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001288 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001289 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001290 if( pParent ){
1291 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1292 assert( x==0 );
1293 }
drh613028b2012-12-17 18:43:02 +00001294 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001295 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001296 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001297 assert( iKey>=0 && iKey<pTab->nCol );
1298 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001299 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1300 sqlite3ColumnDefault(v, pTab, iKey, regRow);
drh688852a2014-02-17 22:40:43 +00001301 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
1302 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1303 sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001304 }else{
drh613028b2012-12-17 18:43:02 +00001305 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001306 }
drh688852a2014-02-17 22:40:43 +00001307 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001308 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
1309 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1310 }else{
1311 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001312 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001313 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh688852a2014-02-17 22:40:43 +00001314 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001315 }
dan5e878302013-10-12 19:06:48 +00001316 if( pParent ){
drh57bf4a82014-02-17 14:59:22 +00001317 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
1318 sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
dan5e878302013-10-12 19:06:48 +00001319 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh688852a2014-02-17 22:40:43 +00001320 VdbeCoverage(v);
dan5e878302013-10-12 19:06:48 +00001321 }
drh6c5b9152012-12-17 16:46:37 +00001322 }
drh613028b2012-12-17 18:43:02 +00001323 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh4b4b4732012-12-17 20:57:15 +00001324 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
1325 pFK->zTo, P4_TRANSIENT);
1326 sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
1327 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001328 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001329 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001330 }
drh688852a2014-02-17 22:40:43 +00001331 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001332 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001333 }
drh9ccd8652013-09-13 16:36:46 +00001334 }
1335 break;
dan09ff9e12013-03-11 11:49:03 +00001336#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001337#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1338
drhc11d4f92003-04-06 21:08:24 +00001339#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001340 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001341 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001342 if( sqlite3GetBoolean(zRight, 0) ){
drh55ef4d92005-08-14 01:20:37 +00001343 sqlite3ParserTrace(stderr, "parser: ");
1344 }else{
1345 sqlite3ParserTrace(0, 0);
1346 }
drhc11d4f92003-04-06 21:08:24 +00001347 }
drh9ccd8652013-09-13 16:36:46 +00001348 }
1349 break;
drhc11d4f92003-04-06 21:08:24 +00001350#endif
1351
drh55ef4d92005-08-14 01:20:37 +00001352 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1353 ** used will be case sensitive or not depending on the RHS.
1354 */
drh9ccd8652013-09-13 16:36:46 +00001355 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001356 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001357 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001358 }
drh9ccd8652013-09-13 16:36:46 +00001359 }
1360 break;
drh55ef4d92005-08-14 01:20:37 +00001361
drh1dcdbc02007-01-27 02:24:54 +00001362#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1363# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1364#endif
1365
drhb7f91642004-10-31 02:22:47 +00001366#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001367 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001368 ** integrity_check designed to detect most database corruption
1369 ** without most of the overhead of a full integrity-check.
1370 */
drh9ccd8652013-09-13 16:36:46 +00001371 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001372 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001373
drhed717fe2003-06-15 23:42:24 +00001374 /* Code that appears at the end of the integrity check. If no error
1375 ** messages have been generated, output OK. Otherwise output the
1376 ** error message
1377 */
drhb06a4ec2014-03-10 18:03:09 +00001378 static const int iLn = VDBE_OFFSET_LINENO(2);
drh57196282004-10-06 15:41:16 +00001379 static const VdbeOpList endCode[] = {
drh4336b0e2014-08-05 00:53:51 +00001380 { OP_IfNeg, 1, 0, 0}, /* 0 */
1381 { OP_String8, 0, 3, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001382 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001383 };
drhed717fe2003-06-15 23:42:24 +00001384
drhc5227312011-10-13 17:09:01 +00001385 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001386
dan5885e762012-07-16 10:06:12 +00001387 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1388 ** then iDb is set to the index of the database identified by <db>.
1389 ** In this case, the integrity of database iDb only is verified by
1390 ** the VDBE created below.
1391 **
1392 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1393 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1394 ** to -1 here, to indicate that the VDBE should verify the integrity
1395 ** of all attached databases. */
1396 assert( iDb>=0 );
1397 assert( iDb==0 || pId2->z );
1398 if( pId2->z==0 ) iDb = -1;
1399
drhed717fe2003-06-15 23:42:24 +00001400 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001401 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001402 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001403 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001404
1405 /* Set the maximum error count */
1406 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1407 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001408 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001409 if( mxErr<=0 ){
1410 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1411 }
1412 }
drh2d401ab2008-01-10 23:50:11 +00001413 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001414
1415 /* Do an integrity check on each database file */
1416 for(i=0; i<db->nDb; i++){
1417 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001418 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001419 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001420
danielk197753c0f742005-03-29 03:10:59 +00001421 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001422 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001423
drh80242052004-06-09 00:48:12 +00001424 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001425 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh688852a2014-02-17 22:40:43 +00001426 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001427 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001428 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001429
drhed717fe2003-06-15 23:42:24 +00001430 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001431 **
1432 ** Begin by filling registers 2, 3, ... with the root pages numbers
1433 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001434 */
dan5885e762012-07-16 10:06:12 +00001435 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001436 pTbls = &db->aDb[i].pSchema->tblHash;
1437 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001438 Table *pTab = sqliteHashData(x);
1439 Index *pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001440 if( HasRowid(pTab) ){
1441 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001442 VdbeComment((v, "%s", pTab->zName));
drh6fbe41a2013-10-30 20:22:55 +00001443 cnt++;
1444 }
drh79069752004-05-22 21:30:40 +00001445 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001446 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001447 VdbeComment((v, "%s", pIdx->zName));
drh79069752004-05-22 21:30:40 +00001448 cnt++;
1449 }
1450 }
drh2d401ab2008-01-10 23:50:11 +00001451
1452 /* Make sure sufficient number of registers have been allocated */
drh6fbe41a2013-10-30 20:22:55 +00001453 pParse->nMem = MAX( pParse->nMem, cnt+8 );
drh2d401ab2008-01-10 23:50:11 +00001454
1455 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001456 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001457 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001458 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001459 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001460 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001461 P4_DYNAMIC);
drh079a3072014-03-19 14:10:55 +00001462 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001463 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001464 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001465 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001466
1467 /* Make sure all the indices are constructed correctly.
1468 */
danielk197741c58b72007-12-29 13:39:19 +00001469 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001470 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001471 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001472 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001473 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001474 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001475 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001476
1477 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001478 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001479 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh688852a2014-02-17 22:40:43 +00001480 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001481 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001482 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001483 sqlite3ExprCacheClear(pParse);
drh26198bb2013-10-31 11:15:09 +00001484 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
drh6a534992013-11-16 20:13:39 +00001485 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001486 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001487 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001488 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001489 }
drh6fbe41a2013-10-30 20:22:55 +00001490 pParse->nMem = MAX(pParse->nMem, 8+j);
drh688852a2014-02-17 22:40:43 +00001491 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001492 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhcefc87f2014-08-01 01:40:33 +00001493 /* Verify that all NOT NULL columns really are NOT NULL */
1494 for(j=0; j<pTab->nCol; j++){
1495 char *zErr;
1496 int jmp2, jmp3;
1497 if( j==pTab->iPKey ) continue;
1498 if( pTab->aCol[j].notNull==0 ) continue;
1499 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1500 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1501 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1502 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1503 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1504 pTab->aCol[j].zName);
1505 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1506 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1507 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1508 sqlite3VdbeAddOp0(v, OP_Halt);
1509 sqlite3VdbeJumpHere(v, jmp2);
1510 sqlite3VdbeJumpHere(v, jmp3);
1511 }
1512 /* Validate index entries for the current row */
drhed717fe2003-06-15 23:42:24 +00001513 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhcefc87f2014-08-01 01:40:33 +00001514 int jmp2, jmp3, jmp4, jmp5;
1515 int ckUniq = sqlite3VdbeMakeLabel(v);
drh6fbe41a2013-10-30 20:22:55 +00001516 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001517 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1518 pPrior, r1);
1519 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001520 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drhcefc87f2014-08-01 01:40:33 +00001521 /* Verify that an index entry exists for the current table row */
1522 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
drh688852a2014-02-17 22:40:43 +00001523 pIdx->nColumn); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001524 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1525 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
1526 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
drhcefc87f2014-08-01 01:40:33 +00001527 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
1528 " missing from index ", P4_STATIC);
drh6fbe41a2013-10-30 20:22:55 +00001529 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
drhcefc87f2014-08-01 01:40:33 +00001530 jmp5 = sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
1531 pIdx->zName, P4_TRANSIENT);
drh6fbe41a2013-10-30 20:22:55 +00001532 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1533 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh688852a2014-02-17 22:40:43 +00001534 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001535 sqlite3VdbeAddOp0(v, OP_Halt);
drhd654be82005-09-20 17:42:23 +00001536 sqlite3VdbeJumpHere(v, jmp2);
drhcefc87f2014-08-01 01:40:33 +00001537 /* For UNIQUE indexes, verify that only one entry exists with the
1538 ** current key. The entry is unique if (1) any column is NULL
1539 ** or (2) the next entry has a different key */
1540 if( IsUniqueIndex(pIdx) ){
1541 int uniqOk = sqlite3VdbeMakeLabel(v);
1542 int jmp6;
1543 int kk;
1544 for(kk=0; kk<pIdx->nKeyCol; kk++){
1545 int iCol = pIdx->aiColumn[kk];
1546 assert( iCol>=0 && iCol<pTab->nCol );
1547 if( pTab->aCol[iCol].notNull ) continue;
1548 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1549 VdbeCoverage(v);
1550 }
1551 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
1552 sqlite3VdbeAddOp2(v, OP_Goto, 0, uniqOk);
1553 sqlite3VdbeJumpHere(v, jmp6);
1554 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1555 pIdx->nKeyCol); VdbeCoverage(v);
1556 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1557 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
1558 "non-unique entry in index ", P4_STATIC);
1559 sqlite3VdbeAddOp2(v, OP_Goto, 0, jmp5);
1560 sqlite3VdbeResolveLabel(v, uniqOk);
1561 }
1562 sqlite3VdbeJumpHere(v, jmp4);
drh87744512014-04-13 19:15:49 +00001563 sqlite3ResolvePartIdxLabel(pParse, jmp3);
drhed717fe2003-06-15 23:42:24 +00001564 }
drh688852a2014-02-17 22:40:43 +00001565 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001566 sqlite3VdbeJumpHere(v, loopTop-1);
1567#ifndef SQLITE_OMIT_BTREECOUNT
1568 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
drh24003452008-01-03 01:28:59 +00001569 "wrong # of entries in index ", P4_STATIC);
drh8a9789b2013-08-01 03:36:59 +00001570 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001571 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001572 addr = sqlite3VdbeCurrentAddr(v);
drh688852a2014-02-17 22:40:43 +00001573 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001574 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001575 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh688852a2014-02-17 22:40:43 +00001576 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
drh5655c542014-02-19 19:14:34 +00001577 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
drh8a9789b2013-08-01 03:36:59 +00001578 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
1579 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
1580 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1581 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001582 }
drh8a9789b2013-08-01 03:36:59 +00001583#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001584 }
1585 }
drh688852a2014-02-17 22:40:43 +00001586 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
drh4336b0e2014-08-05 00:53:51 +00001587 sqlite3VdbeChangeP3(v, addr, -mxErr);
1588 sqlite3VdbeJumpHere(v, addr);
1589 sqlite3VdbeChangeP4(v, addr+1, "ok", P4_STATIC);
drh9ccd8652013-09-13 16:36:46 +00001590 }
1591 break;
drhb7f91642004-10-31 02:22:47 +00001592#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1593
drh13d70422004-11-13 15:59:14 +00001594#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001595 /*
1596 ** PRAGMA encoding
1597 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1598 **
drh85b623f2007-12-13 21:54:09 +00001599 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001600 ** database. If the database is not initialized, it is initialized now.
1601 **
1602 ** The second form of this pragma is a no-op if the main database file
1603 ** has not already been initialized. In this case it sets the default
1604 ** encoding that will be used for the main database file if a new file
1605 ** is created. If an existing main database file is opened, then the
1606 ** default text encoding for the existing database is used.
1607 **
1608 ** In all cases new databases created using the ATTACH command are
1609 ** created to use the same default text encoding as the main database. If
1610 ** the main database has not been initialized and/or created when ATTACH
1611 ** is executed, this is done before the ATTACH operation.
1612 **
1613 ** In the second form this pragma sets the text encoding to be used in
1614 ** new database files created using this database handle. It is only
1615 ** useful if invoked immediately after the main database i
1616 */
drh9ccd8652013-09-13 16:36:46 +00001617 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001618 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001619 char *zName;
1620 u8 enc;
1621 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001622 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001623 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1624 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1625 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001626 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001627 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001628 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1629 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001630 { 0, 0 }
1631 };
drh0f7eb612006-08-08 13:51:43 +00001632 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001633 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001634 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001635 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001636 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001637 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001638 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1639 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1640 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1641 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001642 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001643 }else{ /* "PRAGMA encoding = XXX" */
1644 /* Only change the value of sqlite.enc if the database handle is not
1645 ** initialized. If the main database exists, the new sqlite.enc value
1646 ** will be overwritten when the schema is next loaded. If it does not
1647 ** already exists, it will be created to use the new encoding value.
1648 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001649 if(
1650 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1651 DbHasProperty(db, 0, DB_Empty)
1652 ){
danielk19778e227872004-06-07 07:52:17 +00001653 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1654 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh9bd3cc42014-12-12 23:17:54 +00001655 SCHEMA_ENC(db) = ENC(db) =
1656 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001657 break;
1658 }
1659 }
1660 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001661 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001662 }
1663 }
1664 }
drh9ccd8652013-09-13 16:36:46 +00001665 }
1666 break;
drh13d70422004-11-13 15:59:14 +00001667#endif /* SQLITE_OMIT_UTF16 */
1668
1669#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001670 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001671 ** PRAGMA [database.]schema_version
1672 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001673 **
danielk1977b92b70b2004-11-12 16:11:59 +00001674 ** PRAGMA [database.]user_version
1675 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001676 **
drh4ee09b42013-05-01 19:49:27 +00001677 ** PRAGMA [database.]freelist_count = <integer>
1678 **
1679 ** PRAGMA [database.]application_id
1680 ** PRAGMA [database.]application_id = <integer>
1681 **
danielk1977b92b70b2004-11-12 16:11:59 +00001682 ** The pragma's schema_version and user_version are used to set or get
1683 ** the value of the schema-version and user-version, respectively. Both
1684 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001685 ** stored in the database header.
1686 **
1687 ** The schema-cookie is usually only manipulated internally by SQLite. It
1688 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001689 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001690 ** SQLite each time a query is executed to ensure that the internal cache
1691 ** of the schema used when compiling the SQL query matches the schema of
1692 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001693 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1694 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001695 ** crashes or database corruption. Use with caution!
1696 **
danielk1977b92b70b2004-11-12 16:11:59 +00001697 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001698 ** applications for any purpose.
1699 */
drh9ccd8652013-09-13 16:36:46 +00001700 case PragTyp_HEADER_VALUE: {
drhc228be52015-01-31 02:00:01 +00001701 int iCookie = pPragma->iArg; /* Which cookie to read or write */
drhfb982642007-08-30 01:19:59 +00001702 sqlite3VdbeUsesBtree(v, iDb);
drhc228be52015-01-31 02:00:01 +00001703 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
danielk1977dae24952004-11-11 05:10:43 +00001704 /* Write the specified cookie value */
1705 static const VdbeOpList setCookie[] = {
1706 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001707 { OP_Integer, 0, 1, 0}, /* 1 */
1708 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001709 };
drh688852a2014-02-17 22:40:43 +00001710 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00001711 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00001712 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00001713 sqlite3VdbeChangeP1(v, addr+2, iDb);
1714 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1715 }else{
1716 /* Read the specified cookie value */
1717 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001718 { OP_Transaction, 0, 0, 0}, /* 0 */
1719 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001720 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001721 };
drh688852a2014-02-17 22:40:43 +00001722 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00001723 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001724 sqlite3VdbeChangeP1(v, addr+1, iDb);
1725 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001726 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001727 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001728 }
drh9ccd8652013-09-13 16:36:46 +00001729 }
1730 break;
drh13d70422004-11-13 15:59:14 +00001731#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001732
shanehdc97a8c2010-02-23 20:08:35 +00001733#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1734 /*
1735 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001736 **
drh71caabf2010-02-26 15:39:24 +00001737 ** Return the names of all compile-time options used in this build,
1738 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001739 */
drh9ccd8652013-09-13 16:36:46 +00001740 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00001741 int i = 0;
1742 const char *zOpt;
1743 sqlite3VdbeSetNumCols(v, 1);
1744 pParse->nMem = 1;
1745 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1746 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1747 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1748 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1749 }
drh9ccd8652013-09-13 16:36:46 +00001750 }
1751 break;
shanehdc97a8c2010-02-23 20:08:35 +00001752#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1753
dan5cf53532010-05-01 16:40:20 +00001754#ifndef SQLITE_OMIT_WAL
1755 /*
danf26a1542014-12-02 19:04:54 +00001756 ** PRAGMA [database.]wal_checkpoint = passive|full|restart|truncate
dan5cf53532010-05-01 16:40:20 +00001757 **
1758 ** Checkpoint the database.
1759 */
drh9ccd8652013-09-13 16:36:46 +00001760 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00001761 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001762 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1763 if( zRight ){
1764 if( sqlite3StrICmp(zRight, "full")==0 ){
1765 eMode = SQLITE_CHECKPOINT_FULL;
1766 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1767 eMode = SQLITE_CHECKPOINT_RESTART;
danf26a1542014-12-02 19:04:54 +00001768 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1769 eMode = SQLITE_CHECKPOINT_TRUNCATE;
dancdc1f042010-11-18 12:11:05 +00001770 }
1771 }
dancdc1f042010-11-18 12:11:05 +00001772 sqlite3VdbeSetNumCols(v, 3);
1773 pParse->nMem = 3;
1774 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
1775 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
1776 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
1777
drh30aa3b92011-02-07 23:56:01 +00001778 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001779 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00001780 }
1781 break;
dan5a299f92010-05-03 11:05:08 +00001782
1783 /*
1784 ** PRAGMA wal_autocheckpoint
1785 ** PRAGMA wal_autocheckpoint = N
1786 **
1787 ** Configure a database connection to automatically checkpoint a database
1788 ** after accumulating N frames in the log. Or query for the current value
1789 ** of N.
1790 */
drh9ccd8652013-09-13 16:36:46 +00001791 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00001792 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001793 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001794 }
drhb033d8b2010-05-03 13:37:30 +00001795 returnSingleInt(pParse, "wal_autocheckpoint",
1796 db->xWalCallback==sqlite3WalDefaultHook ?
1797 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00001798 }
1799 break;
dan5cf53532010-05-01 16:40:20 +00001800#endif
dan7c246102010-04-12 19:00:29 +00001801
drh09419b42011-11-16 19:29:17 +00001802 /*
1803 ** PRAGMA shrink_memory
1804 **
1805 ** This pragma attempts to free as much memory as possible from the
1806 ** current database connection.
1807 */
drh9ccd8652013-09-13 16:36:46 +00001808 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00001809 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00001810 break;
1811 }
drh09419b42011-11-16 19:29:17 +00001812
drhf3603962012-09-07 16:46:59 +00001813 /*
1814 ** PRAGMA busy_timeout
1815 ** PRAGMA busy_timeout = N
1816 **
1817 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001818 ** if one is set. If no busy handler or a different busy handler is set
1819 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1820 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001821 */
drhd49c3582013-09-13 19:00:06 +00001822 /*case PragTyp_BUSY_TIMEOUT*/ default: {
drhc228be52015-01-31 02:00:01 +00001823 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00001824 if( zRight ){
1825 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1826 }
1827 returnSingleInt(pParse, "timeout", db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00001828 break;
1829 }
drhf3603962012-09-07 16:46:59 +00001830
drh55e85ca2013-09-13 21:01:56 +00001831 /*
1832 ** PRAGMA soft_heap_limit
1833 ** PRAGMA soft_heap_limit = N
1834 **
1835 ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
1836 ** use -1.
1837 */
1838 case PragTyp_SOFT_HEAP_LIMIT: {
1839 sqlite3_int64 N;
drh9296c182014-07-23 13:40:49 +00001840 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
drh55e85ca2013-09-13 21:01:56 +00001841 sqlite3_soft_heap_limit64(N);
1842 }
1843 returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
1844 break;
1845 }
1846
drh03459612014-08-25 15:13:22 +00001847 /*
1848 ** PRAGMA threads
1849 ** PRAGMA threads = N
1850 **
1851 ** Configure the maximum number of worker threads. Return the new
1852 ** maximum, which might be less than requested.
1853 */
1854 case PragTyp_THREADS: {
1855 sqlite3_int64 N;
drh111544c2014-08-29 16:20:47 +00001856 if( zRight
drh03459612014-08-25 15:13:22 +00001857 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
1858 && N>=0
1859 ){
drh111544c2014-08-29 16:20:47 +00001860 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
drh03459612014-08-25 15:13:22 +00001861 }
drh111544c2014-08-29 16:20:47 +00001862 returnSingleInt(pParse, "threads",
1863 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
drh03459612014-08-25 15:13:22 +00001864 break;
1865 }
1866
dougcurrie81c95ef2004-06-18 23:21:47 +00001867#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001868 /*
1869 ** Report the current state of file logs for all databases
1870 */
drh9ccd8652013-09-13 16:36:46 +00001871 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00001872 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001873 "unlocked", "shared", "reserved", "pending", "exclusive"
1874 };
1875 int i;
drh89ac8c12004-06-09 14:17:20 +00001876 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001877 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001878 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1879 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001880 for(i=0; i<db->nDb; i++){
1881 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001882 const char *zState = "unknown";
1883 int j;
drh89ac8c12004-06-09 14:17:20 +00001884 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001885 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001886 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001887 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001888 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001889 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001890 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1891 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001892 }
drh2d401ab2008-01-10 23:50:11 +00001893 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1894 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001895 }
drh9ccd8652013-09-13 16:36:46 +00001896 break;
1897 }
drh89ac8c12004-06-09 14:17:20 +00001898#endif
1899
shanehad9f9f62010-02-17 17:48:46 +00001900#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00001901 case PragTyp_KEY: {
1902 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1903 break;
1904 }
1905 case PragTyp_REKEY: {
1906 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1907 break;
1908 }
1909 case PragTyp_HEXKEY: {
1910 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00001911 u8 iByte;
1912 int i;
drh9ccd8652013-09-13 16:36:46 +00001913 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00001914 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
1915 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
1916 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00001917 }
1918 if( (zLeft[3] & 0xf)==0xb ){
1919 sqlite3_key_v2(db, zDb, zKey, i/2);
1920 }else{
1921 sqlite3_rekey_v2(db, zDb, zKey, i/2);
1922 }
drhd2cb50b2009-01-09 21:41:17 +00001923 }
drh9ccd8652013-09-13 16:36:46 +00001924 break;
1925 }
drh3c4f2a42005-12-08 18:12:56 +00001926#endif
shanehad9f9f62010-02-17 17:48:46 +00001927#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00001928 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001929#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001930 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001931 sqlite3_activate_see(&zRight[4]);
1932 }
1933#endif
1934#ifdef SQLITE_ENABLE_CEROD
1935 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001936 sqlite3_activate_cerod(&zRight[6]);
1937 }
1938#endif
drh9ccd8652013-09-13 16:36:46 +00001939 }
1940 break;
drh21e2cab2006-09-25 18:01:57 +00001941#endif
drh3c4f2a42005-12-08 18:12:56 +00001942
drh9ccd8652013-09-13 16:36:46 +00001943 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00001944
danielk1977e0048402004-06-15 16:51:01 +00001945pragma_out:
drh633e6d52008-07-28 19:34:53 +00001946 sqlite3DbFree(db, zLeft);
1947 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001948}
drh13d70422004-11-13 15:59:14 +00001949
drh8bfdf722009-06-19 14:06:03 +00001950#endif /* SQLITE_OMIT_PRAGMA */