blob: 079348e11b24c3baac7f0f03d3bc91566546d5b3 [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
16/*
drhc11d4f92003-04-06 21:08:24 +000017** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +000018** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
drh908c0052012-01-30 18:40:55 +000019** unrecognized string argument. The FULL option is disallowed
20** if the omitFull parameter it 1.
drhc11d4f92003-04-06 21:08:24 +000021**
22** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000023** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000024** to support legacy SQL code. The safety level used to be boolean
25** and older scripts may have used numbers 0 for OFF and 1 for ON.
26*/
drh908c0052012-01-30 18:40:55 +000027static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
drh722e95a2004-10-25 20:33:44 +000028 /* 123456789 123456789 */
29 static const char zText[] = "onoffalseyestruefull";
30 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
31 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
32 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
33 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +000034 if( sqlite3Isdigit(*z) ){
drh60ac3f42010-11-23 18:59:27 +000035 return (u8)sqlite3Atoi(z);
drhc11d4f92003-04-06 21:08:24 +000036 }
drhea678832008-12-10 19:26:22 +000037 n = sqlite3Strlen30(z);
drh908c0052012-01-30 18:40:55 +000038 for(i=0; i<ArraySize(iLength)-omitFull; i++){
drh722e95a2004-10-25 20:33:44 +000039 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
40 return iValue[i];
41 }
drhc11d4f92003-04-06 21:08:24 +000042 }
drh908c0052012-01-30 18:40:55 +000043 return dflt;
drhc11d4f92003-04-06 21:08:24 +000044}
45
46/*
drh722e95a2004-10-25 20:33:44 +000047** Interpret the given string as a boolean value.
48*/
drh38d9c612012-01-31 14:24:47 +000049u8 sqlite3GetBoolean(const char *z, int dflt){
50 return getSafetyLevel(z,1,dflt)!=0;
drh722e95a2004-10-25 20:33:44 +000051}
52
drhc7dc9bf2011-06-03 13:02:57 +000053/* The sqlite3GetBoolean() function is used by other modules but the
54** remainder of this file is specific to PRAGMA processing. So omit
55** the rest of the file if PRAGMAs are omitted from the build.
56*/
57#if !defined(SQLITE_OMIT_PRAGMA)
58
danielk197741483462007-03-24 16:45:04 +000059/*
60** Interpret the given string as a locking mode value.
61*/
62static int getLockingMode(const char *z){
63 if( z ){
64 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
65 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
66 }
67 return PAGER_LOCKINGMODE_QUERY;
68}
69
danielk1977dddbcdc2007-04-26 14:42:34 +000070#ifndef SQLITE_OMIT_AUTOVACUUM
71/*
72** Interpret the given string as an auto-vacuum mode value.
73**
74** The following strings, "none", "full" and "incremental" are
75** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
76*/
77static int getAutoVacuum(const char *z){
78 int i;
79 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
80 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
81 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
drh60ac3f42010-11-23 18:59:27 +000082 i = sqlite3Atoi(z);
drh4f21c4a2008-12-10 22:15:00 +000083 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +000084}
85#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
86
danielk1977b84f96f2005-01-20 11:32:23 +000087#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +000088/*
drh90f5ecb2004-07-22 01:19:35 +000089** Interpret the given string as a temp db location. Return 1 for file
90** backed temporary databases, 2 for the Red-Black tree in memory database
91** and 0 to use the compile-time default.
92*/
93static int getTempStore(const char *z){
94 if( z[0]>='0' && z[0]<='2' ){
95 return z[0] - '0';
96 }else if( sqlite3StrICmp(z, "file")==0 ){
97 return 1;
98 }else if( sqlite3StrICmp(z, "memory")==0 ){
99 return 2;
100 }else{
101 return 0;
102 }
103}
drhbf216272005-02-26 18:10:44 +0000104#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000105
drhbf216272005-02-26 18:10:44 +0000106#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000107/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000108** Invalidate temp storage, either when the temp storage is changed
109** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000110*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000111static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000112 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000113 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000114 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000115 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
116 "from within a transaction");
117 return SQLITE_ERROR;
118 }
119 sqlite3BtreeClose(db->aDb[1].pBt);
120 db->aDb[1].pBt = 0;
drhc7792fa2011-04-02 16:28:52 +0000121 sqlite3ResetInternalSchema(db, -1);
drh90f5ecb2004-07-22 01:19:35 +0000122 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000123 return SQLITE_OK;
124}
drhbf216272005-02-26 18:10:44 +0000125#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000126
drhbf216272005-02-26 18:10:44 +0000127#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000128/*
129** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000130** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000131** or DEFAULT_TEMP_STORE pragmas.
132*/
133static int changeTempStorage(Parse *pParse, const char *zStorageType){
134 int ts = getTempStore(zStorageType);
135 sqlite3 *db = pParse->db;
136 if( db->temp_store==ts ) return SQLITE_OK;
137 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
138 return SQLITE_ERROR;
139 }
drh4f21c4a2008-12-10 22:15:00 +0000140 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000141 return SQLITE_OK;
142}
drhbf216272005-02-26 18:10:44 +0000143#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000144
145/*
146** Generate code to return a single integer value.
147*/
drh3c713642009-04-04 16:02:32 +0000148static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000149 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000150 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000151 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
152 if( pI64 ){
153 memcpy(pI64, &value, sizeof(value));
154 }
155 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
drh10861722009-04-07 00:49:16 +0000156 sqlite3VdbeSetNumCols(v, 1);
157 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000158 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000159}
160
drhbf216272005-02-26 18:10:44 +0000161#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000162/*
drh87223182004-02-21 14:00:29 +0000163** Check to see if zRight and zLeft refer to a pragma that queries
164** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
165** Also, implement the pragma.
166*/
167static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000168 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000169 const char *zName; /* Name of the pragma */
170 int mask; /* Mask for the db->flags value */
171 } aPragma[] = {
drh87223182004-02-21 14:00:29 +0000172 { "full_column_names", SQLITE_FullColNames },
173 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000174 { "count_changes", SQLITE_CountRows },
175 { "empty_result_callbacks", SQLITE_NullCallback },
drhe321c292006-01-12 01:56:43 +0000176 { "legacy_file_format", SQLITE_LegacyFileFmt },
drhac530b12006-02-11 01:25:50 +0000177 { "fullfsync", SQLITE_FullFSync },
drhc97d8462010-11-19 18:23:35 +0000178 { "checkpoint_fullfsync", SQLITE_CkptFullFSync },
drh699b3d42009-02-23 16:52:07 +0000179 { "reverse_unordered_selects", SQLITE_ReverseOrder },
drhc6339082010-04-07 16:54:58 +0000180#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
181 { "automatic_index", SQLITE_AutoIndex },
182#endif
drhcf1023c2007-05-08 20:59:49 +0000183#ifdef SQLITE_DEBUG
184 { "sql_trace", SQLITE_SqlTrace },
185 { "vdbe_listing", SQLITE_VdbeListing },
186 { "vdbe_trace", SQLITE_VdbeTrace },
187#endif
drh0cd2d4c2005-11-03 02:15:02 +0000188#ifndef SQLITE_OMIT_CHECK
189 { "ignore_check_constraints", SQLITE_IgnoreChecks },
190#endif
drh722e95a2004-10-25 20:33:44 +0000191 /* The following is VERY experimental */
danielk197734c68fb2007-03-14 15:37:04 +0000192 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
danielk1977da184232006-01-05 11:34:32 +0000193
194 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
195 ** flag if there are any active statements. */
196 { "read_uncommitted", SQLITE_ReadUncommitted },
dan5bde73c2009-09-01 17:11:07 +0000197 { "recursive_triggers", SQLITE_RecTriggers },
dan1da40a32009-09-19 17:00:31 +0000198
dan75cbd982009-09-21 16:06:03 +0000199 /* This flag may only be set if both foreign-key and trigger support
200 ** are present in the build. */
201#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan1da40a32009-09-19 17:00:31 +0000202 { "foreign_keys", SQLITE_ForeignKeys },
dan75cbd982009-09-21 16:06:03 +0000203#endif
drh87223182004-02-21 14:00:29 +0000204 };
205 int i;
drh722e95a2004-10-25 20:33:44 +0000206 const struct sPragmaType *p;
danielk197700e13612008-11-17 19:18:54 +0000207 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
drh722e95a2004-10-25 20:33:44 +0000208 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000209 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000210 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000211 v = sqlite3GetVdbe(pParse);
drhd2cb50b2009-01-09 21:41:17 +0000212 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
213 if( ALWAYS(v) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000214 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000215 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000216 }else{
dan75cbd982009-09-21 16:06:03 +0000217 int mask = p->mask; /* Mask of bits to set or clear. */
218 if( db->autoCommit==0 ){
219 /* Foreign key support may not be enabled or disabled while not
220 ** in auto-commit mode. */
221 mask &= ~(SQLITE_ForeignKeys);
222 }
223
drh38d9c612012-01-31 14:24:47 +0000224 if( sqlite3GetBoolean(zRight, 0) ){
dan75cbd982009-09-21 16:06:03 +0000225 db->flags |= mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000226 }else{
dan75cbd982009-09-21 16:06:03 +0000227 db->flags &= ~mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000228 }
danielk19778e556522007-11-13 10:30:24 +0000229
230 /* Many of the flag-pragmas modify the code generated by the SQL
231 ** compiler (eg. count_changes). So add an opcode to expire all
232 ** compiled SQL statements after modifying a pragma value.
233 */
drh66a51672008-01-03 00:01:23 +0000234 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000235 }
drh87223182004-02-21 14:00:29 +0000236 }
danielk19778e556522007-11-13 10:30:24 +0000237
drh87223182004-02-21 14:00:29 +0000238 return 1;
239 }
240 }
241 return 0;
242}
drhbf216272005-02-26 18:10:44 +0000243#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000244
drhd2cb50b2009-01-09 21:41:17 +0000245/*
246** Return a human-readable name for a constraint resolution action.
247*/
danba9108b2009-09-22 07:13:42 +0000248#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000249static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000250 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000251 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000252 case OE_SetNull: zName = "SET NULL"; break;
253 case OE_SetDflt: zName = "SET DEFAULT"; break;
254 case OE_Cascade: zName = "CASCADE"; break;
255 case OE_Restrict: zName = "RESTRICT"; break;
256 default: zName = "NO ACTION";
257 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000258 }
drhd2cb50b2009-01-09 21:41:17 +0000259 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000260}
danba9108b2009-09-22 07:13:42 +0000261#endif
danielk197750af3e12008-10-10 17:47:21 +0000262
dane04dc882010-04-20 18:53:15 +0000263
264/*
265** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
266** defined in pager.h. This function returns the associated lowercase
267** journal-mode name.
268*/
269const char *sqlite3JournalModename(int eMode){
270 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000271 "delete", "persist", "off", "truncate", "memory"
272#ifndef SQLITE_OMIT_WAL
273 , "wal"
274#endif
dane04dc882010-04-20 18:53:15 +0000275 };
276 assert( PAGER_JOURNALMODE_DELETE==0 );
277 assert( PAGER_JOURNALMODE_PERSIST==1 );
278 assert( PAGER_JOURNALMODE_OFF==2 );
279 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
280 assert( PAGER_JOURNALMODE_MEMORY==4 );
281 assert( PAGER_JOURNALMODE_WAL==5 );
282 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
283
284 if( eMode==ArraySize(azModeName) ) return 0;
285 return azModeName[eMode];
286}
287
drh87223182004-02-21 14:00:29 +0000288/*
drhc11d4f92003-04-06 21:08:24 +0000289** Process a pragma statement.
290**
291** Pragmas are of this form:
292**
danielk197791cf71b2004-06-26 06:37:06 +0000293** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000294**
295** The identifier might also be a string. The value is a string, and
296** identifier, or a number. If minusFlag is true, then the value is
297** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000298**
299** If the left side is "database.id" then pId1 is the database name
300** and pId2 is the id. If the left side is just "id" then pId1 is the
301** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000302*/
danielk197791cf71b2004-06-26 06:37:06 +0000303void sqlite3Pragma(
304 Parse *pParse,
305 Token *pId1, /* First part of [database.]id field */
306 Token *pId2, /* Second part of [database.]id field, or NULL */
307 Token *pValue, /* Token for <value>, or NULL */
308 int minusFlag /* True if a '-' sign preceded <value> */
309){
310 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
311 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
312 const char *zDb = 0; /* The database name */
313 Token *pId; /* Pointer to <id> token */
314 int iDb; /* Database index for <database> */
drh3fa97302012-02-22 16:58:36 +0000315 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh06fd5d62012-02-22 14:45:19 +0000316 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
317 sqlite3 *db = pParse->db; /* The database connection */
318 Db *pDb; /* The specific database being pragmaed */
319 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db); /* Prepared statement */
320
drhc11d4f92003-04-06 21:08:24 +0000321 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000322 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000323 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000324
danielk197791cf71b2004-06-26 06:37:06 +0000325 /* Interpret the [database.] part of the pragma statement. iDb is the
326 ** index of the database this pragma is being applied to in db.aDb[]. */
327 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
328 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000329 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000330
danielk1977ddfb2f02006-02-17 12:25:14 +0000331 /* If the temp database has been explicitly named as part of the
332 ** pragma, make sure it is open.
333 */
334 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
335 return;
336 }
337
drh17435752007-08-16 04:30:38 +0000338 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000339 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000340 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000341 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000342 }else{
drh17435752007-08-16 04:30:38 +0000343 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000344 }
danielk197791cf71b2004-06-26 06:37:06 +0000345
drhd2cb50b2009-01-09 21:41:17 +0000346 assert( pId2 );
347 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000348 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000349 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000350 }
drh06fd5d62012-02-22 14:45:19 +0000351
352 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
353 ** connection. If it returns SQLITE_OK, then assume that the VFS
354 ** handled the pragma and generate a no-op prepared statement.
355 */
drh3fa97302012-02-22 16:58:36 +0000356 aFcntl[0] = 0;
357 aFcntl[1] = zLeft;
358 aFcntl[2] = zRight;
359 aFcntl[3] = 0;
drh06fd5d62012-02-22 14:45:19 +0000360 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
361 if( rc==SQLITE_OK ){
drh3fa97302012-02-22 16:58:36 +0000362 if( aFcntl[0] ){
363 int mem = ++pParse->nMem;
364 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
365 sqlite3VdbeSetNumCols(v, 1);
366 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
367 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
368 sqlite3_free(aFcntl[0]);
369 }
370 }else
drh06fd5d62012-02-22 14:45:19 +0000371
drhc11d4f92003-04-06 21:08:24 +0000372
drhe73c9142011-11-09 16:12:24 +0000373#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000374 /*
drh90f5ecb2004-07-22 01:19:35 +0000375 ** PRAGMA [database.]default_cache_size
376 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000377 **
378 ** The first form reports the current persistent setting for the
379 ** page cache size. The value returned is the maximum number of
380 ** pages in the page cache. The second form sets both the current
381 ** page cache size value and the persistent page cache size value
382 ** stored in the database file.
383 **
drh93791ea2010-04-26 17:36:35 +0000384 ** Older versions of SQLite would set the default cache size to a
385 ** negative number to indicate synchronous=OFF. These days, synchronous
386 ** is always on by default regardless of the sign of the default cache
387 ** size. But continue to take the absolute value of the default cache
388 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000389 */
danielk19774adee202004-05-08 08:23:19 +0000390 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
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 */
394 { OP_IfPos, 1, 7, 0},
drh3c84ddf2008-01-09 02:15:38 +0000395 { OP_Integer, 0, 2, 0},
396 { OP_Subtract, 1, 2, 1},
danielk1977602b4662009-07-02 07:47:33 +0000397 { OP_IfPos, 1, 7, 0},
398 { OP_Integer, 0, 1, 0}, /* 6 */
drh3c84ddf2008-01-09 02:15:38 +0000399 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000400 };
drh905793e2004-02-21 13:31:09 +0000401 int addr;
danielk19778a414492004-06-29 08:59:35 +0000402 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
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;
danielk19774adee202004-05-08 08:23:19 +0000408 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
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 }
421 }else
drhe73c9142011-11-09 16:12:24 +0000422#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000423
drhe73c9142011-11-09 16:12:24 +0000424#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000425 /*
drh90f5ecb2004-07-22 01:19:35 +0000426 ** PRAGMA [database.]page_size
427 ** PRAGMA [database.]page_size=N
428 **
429 ** The first form reports the current setting for the
430 ** database page size in bytes. The second form sets the
431 ** database page size value. The value can only be set if
432 ** the database has not yet been created.
433 */
434 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
435 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000436 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000437 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000438 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000439 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000440 }else{
danielk1977992772c2007-08-30 10:07:38 +0000441 /* Malloc may fail when setting the page-size, as there is an internal
442 ** buffer that the pager module resizes using sqlite3_realloc().
443 */
drh60ac3f42010-11-23 18:59:27 +0000444 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000445 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000446 db->mallocFailed = 1;
447 }
drh90f5ecb2004-07-22 01:19:35 +0000448 }
449 }else
danielk197741483462007-03-24 16:45:04 +0000450
451 /*
drh5b47efa2010-02-12 18:18:39 +0000452 ** PRAGMA [database.]secure_delete
453 ** PRAGMA [database.]secure_delete=ON/OFF
454 **
455 ** The first form reports the current setting for the
456 ** secure_delete flag. The second form changes the secure_delete
457 ** flag setting and reports thenew value.
458 */
459 if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
460 Btree *pBt = pDb->pBt;
461 int b = -1;
462 assert( pBt!=0 );
463 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000464 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000465 }
drhaf034ed2010-02-12 19:46:26 +0000466 if( pId2->n==0 && b>=0 ){
467 int ii;
468 for(ii=0; ii<db->nDb; ii++){
469 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
470 }
471 }
drh5b47efa2010-02-12 18:18:39 +0000472 b = sqlite3BtreeSecureDelete(pBt, b);
473 returnSingleInt(pParse, "secure_delete", b);
474 }else
475
476 /*
drh60ac3f42010-11-23 18:59:27 +0000477 ** PRAGMA [database.]max_page_count
478 ** PRAGMA [database.]max_page_count=N
479 **
480 ** The first form reports the current setting for the
481 ** maximum number of pages in the database file. The
482 ** second form attempts to change this setting. Both
483 ** forms return the current setting.
484 **
drhe73c9142011-11-09 16:12:24 +0000485 ** The absolute value of N is used. This is undocumented and might
486 ** change. The only purpose is to provide an easy way to test
487 ** the sqlite3AbsInt32() function.
488 **
danielk197759a93792008-05-15 17:48:20 +0000489 ** PRAGMA [database.]page_count
490 **
491 ** Return the number of pages in the specified database.
492 */
drh60ac3f42010-11-23 18:59:27 +0000493 if( sqlite3StrICmp(zLeft,"page_count")==0
494 || sqlite3StrICmp(zLeft,"max_page_count")==0
495 ){
danielk197759a93792008-05-15 17:48:20 +0000496 int iReg;
drhd2cb50b2009-01-09 21:41:17 +0000497 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
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);
danielk197759a93792008-05-15 17:48:20 +0000509 }else
510
511 /*
danielk197741483462007-03-24 16:45:04 +0000512 ** PRAGMA [database.]locking_mode
513 ** PRAGMA [database.]locking_mode = (normal|exclusive)
514 */
515 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
516 const char *zRet = "normal";
517 int eMode = getLockingMode(zRight);
518
519 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
520 /* Simple "PRAGMA locking_mode;" statement. This is a query for
521 ** the current default locking mode (which may be different to
522 ** the locking-mode of the main database).
523 */
524 eMode = db->dfltLockMode;
525 }else{
526 Pager *pPager;
527 if( pId2->n==0 ){
528 /* This indicates that no database name was specified as part
529 ** of the PRAGMA command. In this case the locking-mode must be
530 ** set on all attached databases, as well as the main db file.
531 **
532 ** Also, the sqlite3.dfltLockMode variable is set so that
533 ** any subsequently attached databases also use the specified
534 ** locking mode.
535 */
536 int ii;
537 assert(pDb==&db->aDb[0]);
538 for(ii=2; ii<db->nDb; ii++){
539 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
540 sqlite3PagerLockingMode(pPager, eMode);
541 }
drh4f21c4a2008-12-10 22:15:00 +0000542 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000543 }
544 pPager = sqlite3BtreePager(pDb->pBt);
545 eMode = sqlite3PagerLockingMode(pPager, eMode);
546 }
547
548 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
549 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
550 zRet = "exclusive";
551 }
552 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000553 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000554 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
555 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000556 }else
drh3b020132008-04-17 17:02:01 +0000557
558 /*
559 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000560 ** PRAGMA [database.]journal_mode =
561 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000562 */
563 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000564 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
565 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000566
drh94714062011-10-10 12:04:14 +0000567 /* Force the schema to be loaded on all databases. This causes all
568 ** database files to be opened and the journal_modes set. This is
569 ** necessary because subsequent processing must know if the databases
570 ** are in WAL mode. */
danf6c61472010-07-07 13:54:28 +0000571 if( sqlite3ReadSchema(pParse) ){
572 goto pragma_out;
573 }
574
dane04dc882010-04-20 18:53:15 +0000575 sqlite3VdbeSetNumCols(v, 1);
576 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000577
578 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000579 /* If there is no "=MODE" part of the pragma, do a query for the
580 ** current mode */
drh3b020132008-04-17 17:02:01 +0000581 eMode = PAGER_JOURNALMODE_QUERY;
582 }else{
dane04dc882010-04-20 18:53:15 +0000583 const char *zMode;
drhea678832008-12-10 19:26:22 +0000584 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000585 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000586 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
587 }
588 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000589 /* If the "=MODE" part does not match any known journal mode,
590 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000591 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000592 }
593 }
drhc6b2a0f2010-07-08 17:40:37 +0000594 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
595 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
596 iDb = 0;
597 pId2->n = 1;
598 }
599 for(ii=db->nDb-1; ii>=0; ii--){
600 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
601 sqlite3VdbeUsesBtree(v, ii);
602 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000603 }
drh3b020132008-04-17 17:02:01 +0000604 }
drh3b020132008-04-17 17:02:01 +0000605 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
606 }else
danielk1977b53e4962008-06-04 06:45:59 +0000607
608 /*
609 ** PRAGMA [database.]journal_size_limit
610 ** PRAGMA [database.]journal_size_limit=N
611 **
drha9e364f2009-01-13 20:14:15 +0000612 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000613 */
614 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
615 Pager *pPager = sqlite3BtreePager(pDb->pBt);
616 i64 iLimit = -2;
617 if( zRight ){
drh9339da12010-09-30 00:50:49 +0000618 sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
drh3c713642009-04-04 16:02:32 +0000619 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000620 }
621 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000622 returnSingleInt(pParse, "journal_size_limit", iLimit);
danielk1977b53e4962008-06-04 06:45:59 +0000623 }else
624
drh13d70422004-11-13 15:59:14 +0000625#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000626
627 /*
danielk1977951af802004-11-05 15:45:09 +0000628 ** PRAGMA [database.]auto_vacuum
629 ** PRAGMA [database.]auto_vacuum=N
630 **
drha9e364f2009-01-13 20:14:15 +0000631 ** Get or set the value of the database 'auto-vacuum' parameter.
632 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000633 */
634#ifndef SQLITE_OMIT_AUTOVACUUM
635 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
636 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000637 assert( pBt!=0 );
danielk197727b1f952007-06-25 08:16:58 +0000638 if( sqlite3ReadSchema(pParse) ){
639 goto pragma_out;
640 }
danielk1977951af802004-11-05 15:45:09 +0000641 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000642 int auto_vacuum;
643 if( ALWAYS(pBt) ){
644 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
645 }else{
646 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
647 }
danielk1977951af802004-11-05 15:45:09 +0000648 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
649 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000650 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000651 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000652 db->nextAutovac = (u8)eAuto;
drhd2cb50b2009-01-09 21:41:17 +0000653 if( ALWAYS(eAuto>=0) ){
danielk197727b1f952007-06-25 08:16:58 +0000654 /* Call SetAutoVacuum() to set initialize the internal auto and
655 ** incr-vacuum flags. This is required in case this connection
656 ** creates the database file. It is important that it is created
657 ** as an auto-vacuum capable db.
658 */
659 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
660 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
661 /* When setting the auto_vacuum mode to either "full" or
662 ** "incremental", write the value of meta[6] in the database
663 ** file. Before writing to meta[6], check that meta[3] indicates
664 ** that this really is an auto-vacuum capable database.
665 */
666 static const VdbeOpList setMeta6[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000667 { OP_Transaction, 0, 1, 0}, /* 0 */
668 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
669 { OP_If, 1, 0, 0}, /* 2 */
670 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
671 { OP_Integer, 0, 1, 0}, /* 4 */
672 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000673 };
674 int iAddr;
675 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
676 sqlite3VdbeChangeP1(v, iAddr, iDb);
677 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
678 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
679 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
680 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000681 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000682 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000683 }
danielk1977951af802004-11-05 15:45:09 +0000684 }
685 }else
686#endif
687
drhca5557f2007-05-04 18:30:40 +0000688 /*
689 ** PRAGMA [database.]incremental_vacuum(N)
690 **
691 ** Do N steps of incremental vacuuming on a database.
692 */
693#ifndef SQLITE_OMIT_AUTOVACUUM
694 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
695 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000696 if( sqlite3ReadSchema(pParse) ){
697 goto pragma_out;
698 }
drhca5557f2007-05-04 18:30:40 +0000699 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
700 iLimit = 0x7fffffff;
701 }
702 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000703 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000704 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000705 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000706 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000707 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000708 sqlite3VdbeJumpHere(v, addr);
709 }else
710#endif
711
drh13d70422004-11-13 15:59:14 +0000712#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000713 /*
drh90f5ecb2004-07-22 01:19:35 +0000714 ** PRAGMA [database.]cache_size
715 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000716 **
717 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000718 ** page cache size. The second form sets the local
719 ** page cache size value. If N is positive then that is the
720 ** number of pages in the cache. If N is negative, then the
721 ** number of pages is adjusted so that the cache uses -N kibibytes
722 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000723 */
danielk19774adee202004-05-08 08:23:19 +0000724 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000725 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh21206082011-04-04 18:22:02 +0000726 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000727 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000728 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000729 }else{
drh3b42abb2011-11-09 14:23:04 +0000730 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000731 pDb->pSchema->cache_size = size;
732 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000733 }
734 }else
735
736 /*
drh90f5ecb2004-07-22 01:19:35 +0000737 ** PRAGMA temp_store
738 ** PRAGMA temp_store = "default"|"memory"|"file"
739 **
740 ** Return or set the local value of the temp_store flag. Changing
741 ** the local value does not make changes to the disk file and the default
742 ** value will be restored the next time the database is opened.
743 **
744 ** Note that it is possible for the library compile-time options to
745 ** override this setting
746 */
747 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
748 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000749 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000750 }else{
751 changeTempStorage(pParse, zRight);
752 }
753 }else
754
755 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000756 ** PRAGMA temp_store_directory
757 ** PRAGMA temp_store_directory = ""|"directory_name"
758 **
759 ** Return or set the local value of the temp_store_directory flag. Changing
760 ** the value sets a specific directory to be used for temporary files.
761 ** Setting to a null string reverts to the default temporary directory search.
762 ** If temporary directory is changed, then invalidateTempStorage.
763 **
764 */
765 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
766 if( !zRight ){
767 if( sqlite3_temp_directory ){
768 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000769 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000770 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000771 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
772 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000773 }
774 }else{
drh78f82d12008-09-02 00:52:52 +0000775#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000776 if( zRight[0] ){
danielk1977fab11272008-09-16 14:38:02 +0000777 int rc;
danielk1977861f7452008-06-05 11:39:11 +0000778 int res;
danielk1977fab11272008-09-16 14:38:02 +0000779 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
780 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000781 sqlite3ErrorMsg(pParse, "not a writable directory");
782 goto pragma_out;
783 }
drh268283b2005-01-08 15:44:25 +0000784 }
danielk1977b06a0b62008-06-26 10:54:12 +0000785 if( SQLITE_TEMP_STORE==0
786 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
787 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000788 ){
789 invalidateTempStorage(pParse);
790 }
drh17435752007-08-16 04:30:38 +0000791 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000792 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000793 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000794 }else{
drh268283b2005-01-08 15:44:25 +0000795 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000796 }
drh78f82d12008-09-02 00:52:52 +0000797#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000798 }
799 }else
800
drhd2cb50b2009-01-09 21:41:17 +0000801#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
802# if defined(__APPLE__)
803# define SQLITE_ENABLE_LOCKING_STYLE 1
804# else
805# define SQLITE_ENABLE_LOCKING_STYLE 0
806# endif
807#endif
808#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000809 /*
aswiftaebf4132008-11-21 00:10:35 +0000810 ** PRAGMA [database.]lock_proxy_file
811 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
812 **
813 ** Return or set the value of the lock_proxy_file flag. Changing
814 ** the value sets a specific file to be used for database access locks.
815 **
816 */
817 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
818 if( !zRight ){
819 Pager *pPager = sqlite3BtreePager(pDb->pBt);
820 char *proxy_file_path = NULL;
821 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000822 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000823 &proxy_file_path);
824
825 if( proxy_file_path ){
826 sqlite3VdbeSetNumCols(v, 1);
827 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
828 "lock_proxy_file", SQLITE_STATIC);
829 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
830 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
831 }
832 }else{
833 Pager *pPager = sqlite3BtreePager(pDb->pBt);
834 sqlite3_file *pFile = sqlite3PagerFile(pPager);
835 int res;
836 if( zRight[0] ){
837 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
838 zRight);
839 } else {
840 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
841 NULL);
842 }
843 if( res!=SQLITE_OK ){
844 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
845 goto pragma_out;
846 }
847 }
848 }else
drhd2cb50b2009-01-09 21:41:17 +0000849#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000850
851 /*
drh90f5ecb2004-07-22 01:19:35 +0000852 ** PRAGMA [database.]synchronous
853 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000854 **
855 ** Return or set the local value of the synchronous flag. Changing
856 ** the local value does not make changes to the disk file and the
857 ** default value will be restored the next time the database is
858 ** opened.
859 */
danielk19774adee202004-05-08 08:23:19 +0000860 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000861 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000862 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000863 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000864 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000865 if( !db->autoCommit ){
866 sqlite3ErrorMsg(pParse,
867 "Safety level may not be changed inside a transaction");
868 }else{
drh908c0052012-01-30 18:40:55 +0000869 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000870 }
drhc11d4f92003-04-06 21:08:24 +0000871 }
872 }else
drh13d70422004-11-13 15:59:14 +0000873#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000874
drhbf216272005-02-26 18:10:44 +0000875#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000876 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000877 /* The flagPragma() subroutine also generates any necessary code
878 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000879 }else
drhbf216272005-02-26 18:10:44 +0000880#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000881
drh13d70422004-11-13 15:59:14 +0000882#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000883 /*
884 ** PRAGMA table_info(<table>)
885 **
886 ** Return a single row for each column of the named table. The columns of
887 ** the returned data set are:
888 **
889 ** cid: Column id (numbered from left to right, starting at 0)
890 ** name: Column name
891 ** type: Column declaration type.
892 ** notnull: True if 'NOT NULL' is part of column declaration
893 ** dflt_value: The default value for the column, if any.
894 */
drh5260f7e2004-06-26 19:35:29 +0000895 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000896 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000897 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000898 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000899 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000900 int i;
danielk1977034ca142007-06-26 10:38:54 +0000901 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +0000902 Column *pCol;
drh9f6696a2006-02-09 16:52:23 +0000903 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +0000904 pParse->nMem = 6;
danielk197710fb7492008-10-31 10:53:22 +0000905 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
906 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
907 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
908 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
909 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
910 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000911 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000912 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +0000913 if( IsHiddenColumn(pCol) ){
914 nHidden++;
915 continue;
916 }
drh2d401ab2008-01-10 23:50:11 +0000917 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
918 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
919 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +0000920 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +0000921 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +0000922 if( pCol->zDflt ){
923 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +0000924 }else{
drh2d401ab2008-01-10 23:50:11 +0000925 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +0000926 }
drh2d401ab2008-01-10 23:50:11 +0000927 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
928 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +0000929 }
930 }
931 }else
932
drh5260f7e2004-06-26 19:35:29 +0000933 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000934 Index *pIdx;
935 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000936 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000937 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000938 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000939 int i;
940 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000941 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000942 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000943 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
944 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
945 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000946 for(i=0; i<pIdx->nColumn; i++){
947 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +0000948 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
949 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +0000950 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +0000951 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
952 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +0000953 }
954 }
955 }else
956
drh5260f7e2004-06-26 19:35:29 +0000957 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000958 Index *pIdx;
959 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000960 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000961 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000962 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000963 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000964 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000965 if( pIdx ){
966 int i = 0;
967 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000968 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000969 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
970 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
971 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000972 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +0000973 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
974 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
975 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
976 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +0000977 ++i;
978 pIdx = pIdx->pNext;
979 }
drhc11d4f92003-04-06 21:08:24 +0000980 }
981 }
982 }else
983
drh13d70422004-11-13 15:59:14 +0000984 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
985 int i;
986 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
987 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000988 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000989 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
990 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
991 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +0000992 for(i=0; i<db->nDb; i++){
993 if( db->aDb[i].pBt==0 ) continue;
994 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +0000995 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
996 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
997 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +0000998 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +0000999 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001000 }
1001 }else
danielk197748af65a2005-02-09 03:20:37 +00001002
1003 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
1004 int i = 0;
1005 HashElem *p;
1006 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001007 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001008 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1009 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +00001010 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1011 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001012 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1013 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1014 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001015 }
1016 }else
drh13d70422004-11-13 15:59:14 +00001017#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1018
drhb7f91642004-10-31 02:22:47 +00001019#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +00001020 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +00001021 FKey *pFK;
1022 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +00001023 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001024 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001025 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001026 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001027 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001028 if( pFK ){
1029 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001030 sqlite3VdbeSetNumCols(v, 8);
1031 pParse->nMem = 8;
danielk197710fb7492008-10-31 10:53:22 +00001032 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1033 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1034 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1035 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1036 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1037 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1038 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1039 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001040 while(pFK){
1041 int j;
1042 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001043 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001044 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1045 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001046 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1047 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1048 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1049 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001050 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001051 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001052 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1053 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1054 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1055 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001056 }
1057 ++i;
1058 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001059 }
drh78100cc2003-08-23 22:40:53 +00001060 }
1061 }
1062 }else
drhb7f91642004-10-31 02:22:47 +00001063#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001064
drhc11d4f92003-04-06 21:08:24 +00001065#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +00001066 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +00001067 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001068 if( sqlite3GetBoolean(zRight, 0) ){
drh55ef4d92005-08-14 01:20:37 +00001069 sqlite3ParserTrace(stderr, "parser: ");
1070 }else{
1071 sqlite3ParserTrace(0, 0);
1072 }
drhc11d4f92003-04-06 21:08:24 +00001073 }
1074 }else
1075#endif
1076
drh55ef4d92005-08-14 01:20:37 +00001077 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1078 ** used will be case sensitive or not depending on the RHS.
1079 */
1080 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
1081 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001082 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001083 }
1084 }else
1085
drh1dcdbc02007-01-27 02:24:54 +00001086#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1087# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1088#endif
1089
drhb7f91642004-10-31 02:22:47 +00001090#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk197741c58b72007-12-29 13:39:19 +00001091 /* Pragma "quick_check" is an experimental reduced version of
1092 ** integrity_check designed to detect most database corruption
1093 ** without most of the overhead of a full integrity-check.
1094 */
1095 if( sqlite3StrICmp(zLeft, "integrity_check")==0
1096 || sqlite3StrICmp(zLeft, "quick_check")==0
1097 ){
drh1dcdbc02007-01-27 02:24:54 +00001098 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001099
drhed717fe2003-06-15 23:42:24 +00001100 /* Code that appears at the end of the integrity check. If no error
1101 ** messages have been generated, output OK. Otherwise output the
1102 ** error message
1103 */
drh57196282004-10-06 15:41:16 +00001104 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001105 { OP_AddImm, 1, 0, 0}, /* 0 */
1106 { OP_IfNeg, 1, 0, 0}, /* 1 */
1107 { OP_String8, 0, 3, 0}, /* 2 */
1108 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001109 };
drhed717fe2003-06-15 23:42:24 +00001110
drhc5227312011-10-13 17:09:01 +00001111 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001112
drhed717fe2003-06-15 23:42:24 +00001113 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +00001114 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +00001115 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001116 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001117 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001118
1119 /* Set the maximum error count */
1120 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1121 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001122 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001123 if( mxErr<=0 ){
1124 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1125 }
1126 }
drh2d401ab2008-01-10 23:50:11 +00001127 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001128
1129 /* Do an integrity check on each database file */
1130 for(i=0; i<db->nDb; i++){
1131 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001132 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001133 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001134
danielk197753c0f742005-03-29 03:10:59 +00001135 if( OMIT_TEMPDB && i==1 ) continue;
1136
drh80242052004-06-09 00:48:12 +00001137 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001138 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001139 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001140 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001141
drhed717fe2003-06-15 23:42:24 +00001142 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001143 **
1144 ** Begin by filling registers 2, 3, ... with the root pages numbers
1145 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001146 */
drh21206082011-04-04 18:22:02 +00001147 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00001148 pTbls = &db->aDb[i].pSchema->tblHash;
1149 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001150 Table *pTab = sqliteHashData(x);
1151 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001152 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001153 cnt++;
1154 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001155 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001156 cnt++;
1157 }
1158 }
drh2d401ab2008-01-10 23:50:11 +00001159
1160 /* Make sure sufficient number of registers have been allocated */
danielk1977a7a8e142008-02-13 18:25:27 +00001161 if( pParse->nMem < cnt+4 ){
1162 pParse->nMem = cnt+4;
drh98757152008-01-09 23:04:12 +00001163 }
drh2d401ab2008-01-10 23:50:11 +00001164
1165 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001166 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001167 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001168 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1169 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001170 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001171 P4_DYNAMIC);
drhb21e7c72008-06-22 12:37:57 +00001172 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001173 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001174 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001175 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001176
1177 /* Make sure all the indices are constructed correctly.
1178 */
danielk197741c58b72007-12-29 13:39:19 +00001179 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001180 Table *pTab = sqliteHashData(x);
1181 Index *pIdx;
1182 int loopTop;
1183
1184 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001185 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001186 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001187 sqlite3VdbeJumpHere(v, addr);
drh290c1942004-08-21 17:54:45 +00001188 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh2d401ab2008-01-10 23:50:11 +00001189 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
drh66a51672008-01-03 00:01:23 +00001190 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
drh2d401ab2008-01-10 23:50:11 +00001191 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
drhed717fe2003-06-15 23:42:24 +00001192 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +00001193 int jmp2;
drh91fc4a02009-11-12 20:39:03 +00001194 int r1;
drh57196282004-10-06 15:41:16 +00001195 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001196 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001197 { OP_String8, 0, 3, 0}, /* 1 */
1198 { OP_Rowid, 1, 4, 0},
1199 { OP_String8, 0, 5, 0}, /* 3 */
1200 { OP_String8, 0, 6, 0}, /* 4 */
1201 { OP_Concat, 4, 3, 3},
1202 { OP_Concat, 5, 3, 3},
1203 { OP_Concat, 6, 3, 3},
1204 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001205 { OP_IfPos, 1, 0, 0}, /* 9 */
1206 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001207 };
drh91fc4a02009-11-12 20:39:03 +00001208 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
1209 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
danielk19774adee202004-05-08 08:23:19 +00001210 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001211 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1212 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh8d129422011-04-05 12:25:19 +00001213 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
drhbb8a2792008-03-19 00:21:30 +00001214 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001215 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +00001216 }
drh66a51672008-01-03 00:01:23 +00001217 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +00001218 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +00001219 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +00001220 static const VdbeOpList cntIdx[] = {
drh4c583122008-01-04 22:01:03 +00001221 { OP_Integer, 0, 3, 0},
drhd654be82005-09-20 17:42:23 +00001222 { OP_Rewind, 0, 0, 0}, /* 1 */
drh8558cde2008-01-05 05:20:10 +00001223 { OP_AddImm, 3, 1, 0},
drhd654be82005-09-20 17:42:23 +00001224 { OP_Next, 0, 0, 0}, /* 3 */
drh2d401ab2008-01-10 23:50:11 +00001225 { OP_Eq, 2, 0, 3}, /* 4 */
drh8558cde2008-01-05 05:20:10 +00001226 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001227 { OP_String8, 0, 2, 0}, /* 6 */
1228 { OP_String8, 0, 3, 0}, /* 7 */
1229 { OP_Concat, 3, 2, 2},
1230 { OP_ResultRow, 2, 1, 0},
drhed717fe2003-06-15 23:42:24 +00001231 };
drh3c84ddf2008-01-09 02:15:38 +00001232 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
drh66a51672008-01-03 00:01:23 +00001233 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001234 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001235 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +00001236 sqlite3VdbeChangeP1(v, addr+1, j+2);
1237 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1238 sqlite3VdbeChangeP1(v, addr+3, j+2);
1239 sqlite3VdbeChangeP2(v, addr+3, addr+2);
drh2d401ab2008-01-10 23:50:11 +00001240 sqlite3VdbeJumpHere(v, addr+4);
1241 sqlite3VdbeChangeP4(v, addr+6,
drh24003452008-01-03 01:28:59 +00001242 "wrong # of entries in index ", P4_STATIC);
drh8d129422011-04-05 12:25:19 +00001243 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
drhed717fe2003-06-15 23:42:24 +00001244 }
1245 }
1246 }
danielk19774adee202004-05-08 08:23:19 +00001247 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001248 sqlite3VdbeChangeP2(v, addr, -mxErr);
1249 sqlite3VdbeJumpHere(v, addr+1);
1250 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001251 }else
drhb7f91642004-10-31 02:22:47 +00001252#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1253
drh13d70422004-11-13 15:59:14 +00001254#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001255 /*
1256 ** PRAGMA encoding
1257 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1258 **
drh85b623f2007-12-13 21:54:09 +00001259 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001260 ** database. If the database is not initialized, it is initialized now.
1261 **
1262 ** The second form of this pragma is a no-op if the main database file
1263 ** has not already been initialized. In this case it sets the default
1264 ** encoding that will be used for the main database file if a new file
1265 ** is created. If an existing main database file is opened, then the
1266 ** default text encoding for the existing database is used.
1267 **
1268 ** In all cases new databases created using the ATTACH command are
1269 ** created to use the same default text encoding as the main database. If
1270 ** the main database has not been initialized and/or created when ATTACH
1271 ** is executed, this is done before the ATTACH operation.
1272 **
1273 ** In the second form this pragma sets the text encoding to be used in
1274 ** new database files created using this database handle. It is only
1275 ** useful if invoked immediately after the main database i
1276 */
1277 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001278 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001279 char *zName;
1280 u8 enc;
1281 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001282 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001283 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1284 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1285 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001286 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001287 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001288 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1289 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001290 { 0, 0 }
1291 };
drh0f7eb612006-08-08 13:51:43 +00001292 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001293 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001294 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001295 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001296 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001297 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001298 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1299 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1300 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1301 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001302 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001303 }else{ /* "PRAGMA encoding = XXX" */
1304 /* Only change the value of sqlite.enc if the database handle is not
1305 ** initialized. If the main database exists, the new sqlite.enc value
1306 ** will be overwritten when the schema is next loaded. If it does not
1307 ** already exists, it will be created to use the new encoding value.
1308 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001309 if(
1310 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1311 DbHasProperty(db, 0, DB_Empty)
1312 ){
danielk19778e227872004-06-07 07:52:17 +00001313 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1314 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001315 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001316 break;
1317 }
1318 }
1319 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001320 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001321 }
1322 }
1323 }
1324 }else
drh13d70422004-11-13 15:59:14 +00001325#endif /* SQLITE_OMIT_UTF16 */
1326
1327#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001328 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001329 ** PRAGMA [database.]schema_version
1330 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001331 **
danielk1977b92b70b2004-11-12 16:11:59 +00001332 ** PRAGMA [database.]user_version
1333 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001334 **
danielk1977b92b70b2004-11-12 16:11:59 +00001335 ** The pragma's schema_version and user_version are used to set or get
1336 ** the value of the schema-version and user-version, respectively. Both
1337 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001338 ** stored in the database header.
1339 **
1340 ** The schema-cookie is usually only manipulated internally by SQLite. It
1341 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001342 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001343 ** SQLite each time a query is executed to ensure that the internal cache
1344 ** of the schema used when compiling the SQL query matches the schema of
1345 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001346 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1347 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001348 ** crashes or database corruption. Use with caution!
1349 **
danielk1977b92b70b2004-11-12 16:11:59 +00001350 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001351 ** applications for any purpose.
1352 */
danielk1977180b56a2007-06-24 08:00:42 +00001353 if( sqlite3StrICmp(zLeft, "schema_version")==0
1354 || sqlite3StrICmp(zLeft, "user_version")==0
1355 || sqlite3StrICmp(zLeft, "freelist_count")==0
1356 ){
danielk19770d19f7a2009-06-03 11:25:07 +00001357 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001358 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001359 switch( zLeft[0] ){
danielk1977180b56a2007-06-24 08:00:42 +00001360 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00001361 iCookie = BTREE_FREE_PAGE_COUNT;
1362 break;
1363 case 's': case 'S':
1364 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001365 break;
1366 default:
danielk19770d19f7a2009-06-03 11:25:07 +00001367 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001368 break;
danielk1977dae24952004-11-11 05:10:43 +00001369 }
1370
danielk19770d19f7a2009-06-03 11:25:07 +00001371 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00001372 /* Write the specified cookie value */
1373 static const VdbeOpList setCookie[] = {
1374 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001375 { OP_Integer, 0, 1, 0}, /* 1 */
1376 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001377 };
1378 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1379 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00001380 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00001381 sqlite3VdbeChangeP1(v, addr+2, iDb);
1382 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1383 }else{
1384 /* Read the specified cookie value */
1385 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001386 { OP_Transaction, 0, 0, 0}, /* 0 */
1387 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001388 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001389 };
1390 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1391 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001392 sqlite3VdbeChangeP1(v, addr+1, iDb);
1393 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001394 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001395 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001396 }
drh6e345992007-03-30 11:12:08 +00001397 }else
drh13d70422004-11-13 15:59:14 +00001398#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001399
shanehdc97a8c2010-02-23 20:08:35 +00001400#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1401 /*
1402 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001403 **
drh71caabf2010-02-26 15:39:24 +00001404 ** Return the names of all compile-time options used in this build,
1405 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001406 */
drh264a2d42010-02-25 15:28:41 +00001407 if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
shanehdc97a8c2010-02-23 20:08:35 +00001408 int i = 0;
1409 const char *zOpt;
1410 sqlite3VdbeSetNumCols(v, 1);
1411 pParse->nMem = 1;
1412 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1413 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1414 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1415 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1416 }
1417 }else
1418#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1419
dan5cf53532010-05-01 16:40:20 +00001420#ifndef SQLITE_OMIT_WAL
1421 /*
dancdc1f042010-11-18 12:11:05 +00001422 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
dan5cf53532010-05-01 16:40:20 +00001423 **
1424 ** Checkpoint the database.
1425 */
dan5a299f92010-05-03 11:05:08 +00001426 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
dana58f26f2010-11-16 18:56:51 +00001427 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001428 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1429 if( zRight ){
1430 if( sqlite3StrICmp(zRight, "full")==0 ){
1431 eMode = SQLITE_CHECKPOINT_FULL;
1432 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1433 eMode = SQLITE_CHECKPOINT_RESTART;
1434 }
1435 }
dan5a299f92010-05-03 11:05:08 +00001436 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
dancdc1f042010-11-18 12:11:05 +00001437 sqlite3VdbeSetNumCols(v, 3);
1438 pParse->nMem = 3;
1439 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
1440 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
1441 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
1442
drh30aa3b92011-02-07 23:56:01 +00001443 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001444 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
dan7c246102010-04-12 19:00:29 +00001445 }else
dan5a299f92010-05-03 11:05:08 +00001446
1447 /*
1448 ** PRAGMA wal_autocheckpoint
1449 ** PRAGMA wal_autocheckpoint = N
1450 **
1451 ** Configure a database connection to automatically checkpoint a database
1452 ** after accumulating N frames in the log. Or query for the current value
1453 ** of N.
1454 */
1455 if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
1456 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001457 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001458 }
drhb033d8b2010-05-03 13:37:30 +00001459 returnSingleInt(pParse, "wal_autocheckpoint",
1460 db->xWalCallback==sqlite3WalDefaultHook ?
1461 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
dan5a299f92010-05-03 11:05:08 +00001462 }else
dan5cf53532010-05-01 16:40:20 +00001463#endif
dan7c246102010-04-12 19:00:29 +00001464
drh09419b42011-11-16 19:29:17 +00001465 /*
1466 ** PRAGMA shrink_memory
1467 **
1468 ** This pragma attempts to free as much memory as possible from the
1469 ** current database connection.
1470 */
1471 if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
1472 sqlite3_db_release_memory(db);
1473 }else
1474
dougcurrie81c95ef2004-06-18 23:21:47 +00001475#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001476 /*
1477 ** Report the current state of file logs for all databases
1478 */
1479 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001480 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001481 "unlocked", "shared", "reserved", "pending", "exclusive"
1482 };
1483 int i;
drh89ac8c12004-06-09 14:17:20 +00001484 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001485 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001486 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1487 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001488 for(i=0; i<db->nDb; i++){
1489 Btree *pBt;
1490 Pager *pPager;
drh9e33c2c2007-08-31 18:34:59 +00001491 const char *zState = "unknown";
1492 int j;
drh89ac8c12004-06-09 14:17:20 +00001493 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001494 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001495 pBt = db->aDb[i].pBt;
1496 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001497 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001498 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001499 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1500 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001501 }
drh2d401ab2008-01-10 23:50:11 +00001502 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1503 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001504 }
danielk1977a4de4532008-09-02 15:44:08 +00001505
drh89ac8c12004-06-09 14:17:20 +00001506 }else
1507#endif
1508
shanehad9f9f62010-02-17 17:48:46 +00001509#ifdef SQLITE_HAS_CODEC
drhd2cb50b2009-01-09 21:41:17 +00001510 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
drhea678832008-12-10 19:26:22 +00001511 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
drh3c4f2a42005-12-08 18:12:56 +00001512 }else
drhd2cb50b2009-01-09 21:41:17 +00001513 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
1514 sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
1515 }else
1516 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
1517 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
1518 int i, h1, h2;
1519 char zKey[40];
1520 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
1521 h1 += 9*(1&(h1>>6));
1522 h2 += 9*(1&(h2>>6));
1523 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
1524 }
1525 if( (zLeft[3] & 0xf)==0xb ){
1526 sqlite3_key(db, zKey, i/2);
1527 }else{
1528 sqlite3_rekey(db, zKey, i/2);
1529 }
1530 }else
drh3c4f2a42005-12-08 18:12:56 +00001531#endif
shanehad9f9f62010-02-17 17:48:46 +00001532#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh21e2cab2006-09-25 18:01:57 +00001533 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
shanehad9f9f62010-02-17 17:48:46 +00001534#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001535 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001536 sqlite3_activate_see(&zRight[4]);
1537 }
1538#endif
1539#ifdef SQLITE_ENABLE_CEROD
1540 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001541 sqlite3_activate_cerod(&zRight[6]);
1542 }
1543#endif
drhd2cb50b2009-01-09 21:41:17 +00001544 }else
drh21e2cab2006-09-25 18:01:57 +00001545#endif
drh3c4f2a42005-12-08 18:12:56 +00001546
drhd2cb50b2009-01-09 21:41:17 +00001547
1548 {/* Empty ELSE clause */}
danielk1977a21c6b62005-01-24 10:25:59 +00001549
drhd2cb50b2009-01-09 21:41:17 +00001550 /*
1551 ** Reset the safety level, in case the fullfsync flag or synchronous
1552 ** setting changed.
1553 */
danielk1977ddfb2f02006-02-17 12:25:14 +00001554#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhd2cb50b2009-01-09 21:41:17 +00001555 if( db->autoCommit ){
1556 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
drhc97d8462010-11-19 18:23:35 +00001557 (db->flags&SQLITE_FullFSync)!=0,
1558 (db->flags&SQLITE_CkptFullFSync)!=0);
danielk1977a21c6b62005-01-24 10:25:59 +00001559 }
drhd2cb50b2009-01-09 21:41:17 +00001560#endif
danielk1977e0048402004-06-15 16:51:01 +00001561pragma_out:
drh633e6d52008-07-28 19:34:53 +00001562 sqlite3DbFree(db, zLeft);
1563 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001564}
drh13d70422004-11-13 15:59:14 +00001565
drh8bfdf722009-06-19 14:06:03 +00001566#endif /* SQLITE_OMIT_PRAGMA */