blob: cd1cfbe2131846b30a91bb6ac1c4e17e0743a24f [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;
drh81028a42012-05-15 18:28:27 +0000121 sqlite3ResetAllSchemasOfConnection(db);
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 },
drhe0962052013-01-29 19:14:31 +0000187 { "vdbe_addoptrace", SQLITE_VdbeAddopTrace},
188 { "vdbe_debug", SQLITE_SqlTrace | SQLITE_VdbeListing
189 | SQLITE_VdbeTrace },
drhcf1023c2007-05-08 20:59:49 +0000190#endif
drh0cd2d4c2005-11-03 02:15:02 +0000191#ifndef SQLITE_OMIT_CHECK
192 { "ignore_check_constraints", SQLITE_IgnoreChecks },
193#endif
drh722e95a2004-10-25 20:33:44 +0000194 /* The following is VERY experimental */
danielk197734c68fb2007-03-14 15:37:04 +0000195 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
danielk1977da184232006-01-05 11:34:32 +0000196
197 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
198 ** flag if there are any active statements. */
199 { "read_uncommitted", SQLITE_ReadUncommitted },
dan5bde73c2009-09-01 17:11:07 +0000200 { "recursive_triggers", SQLITE_RecTriggers },
dan1da40a32009-09-19 17:00:31 +0000201
dan75cbd982009-09-21 16:06:03 +0000202 /* This flag may only be set if both foreign-key and trigger support
203 ** are present in the build. */
204#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan1da40a32009-09-19 17:00:31 +0000205 { "foreign_keys", SQLITE_ForeignKeys },
dan75cbd982009-09-21 16:06:03 +0000206#endif
drh87223182004-02-21 14:00:29 +0000207 };
208 int i;
drh722e95a2004-10-25 20:33:44 +0000209 const struct sPragmaType *p;
danielk197700e13612008-11-17 19:18:54 +0000210 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
drh722e95a2004-10-25 20:33:44 +0000211 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000212 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000213 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000214 v = sqlite3GetVdbe(pParse);
drhd2cb50b2009-01-09 21:41:17 +0000215 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
216 if( ALWAYS(v) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000217 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000218 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000219 }else{
dan75cbd982009-09-21 16:06:03 +0000220 int mask = p->mask; /* Mask of bits to set or clear. */
221 if( db->autoCommit==0 ){
222 /* Foreign key support may not be enabled or disabled while not
223 ** in auto-commit mode. */
224 mask &= ~(SQLITE_ForeignKeys);
225 }
226
drh38d9c612012-01-31 14:24:47 +0000227 if( sqlite3GetBoolean(zRight, 0) ){
dan75cbd982009-09-21 16:06:03 +0000228 db->flags |= mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000229 }else{
dan75cbd982009-09-21 16:06:03 +0000230 db->flags &= ~mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000231 }
danielk19778e556522007-11-13 10:30:24 +0000232
233 /* Many of the flag-pragmas modify the code generated by the SQL
234 ** compiler (eg. count_changes). So add an opcode to expire all
235 ** compiled SQL statements after modifying a pragma value.
236 */
drh66a51672008-01-03 00:01:23 +0000237 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000238 }
drh87223182004-02-21 14:00:29 +0000239 }
danielk19778e556522007-11-13 10:30:24 +0000240
drh87223182004-02-21 14:00:29 +0000241 return 1;
242 }
243 }
244 return 0;
245}
drhbf216272005-02-26 18:10:44 +0000246#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000247
drhd2cb50b2009-01-09 21:41:17 +0000248/*
249** Return a human-readable name for a constraint resolution action.
250*/
danba9108b2009-09-22 07:13:42 +0000251#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000252static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000253 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000254 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000255 case OE_SetNull: zName = "SET NULL"; break;
256 case OE_SetDflt: zName = "SET DEFAULT"; break;
257 case OE_Cascade: zName = "CASCADE"; break;
258 case OE_Restrict: zName = "RESTRICT"; break;
259 default: zName = "NO ACTION";
260 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000261 }
drhd2cb50b2009-01-09 21:41:17 +0000262 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000263}
danba9108b2009-09-22 07:13:42 +0000264#endif
danielk197750af3e12008-10-10 17:47:21 +0000265
dane04dc882010-04-20 18:53:15 +0000266
267/*
268** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
269** defined in pager.h. This function returns the associated lowercase
270** journal-mode name.
271*/
272const char *sqlite3JournalModename(int eMode){
273 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000274 "delete", "persist", "off", "truncate", "memory"
275#ifndef SQLITE_OMIT_WAL
276 , "wal"
277#endif
dane04dc882010-04-20 18:53:15 +0000278 };
279 assert( PAGER_JOURNALMODE_DELETE==0 );
280 assert( PAGER_JOURNALMODE_PERSIST==1 );
281 assert( PAGER_JOURNALMODE_OFF==2 );
282 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
283 assert( PAGER_JOURNALMODE_MEMORY==4 );
284 assert( PAGER_JOURNALMODE_WAL==5 );
285 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
286
287 if( eMode==ArraySize(azModeName) ) return 0;
288 return azModeName[eMode];
289}
290
drh87223182004-02-21 14:00:29 +0000291/*
drhc11d4f92003-04-06 21:08:24 +0000292** Process a pragma statement.
293**
294** Pragmas are of this form:
295**
danielk197791cf71b2004-06-26 06:37:06 +0000296** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000297**
298** The identifier might also be a string. The value is a string, and
299** identifier, or a number. If minusFlag is true, then the value is
300** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000301**
302** If the left side is "database.id" then pId1 is the database name
303** and pId2 is the id. If the left side is just "id" then pId1 is the
304** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000305*/
danielk197791cf71b2004-06-26 06:37:06 +0000306void sqlite3Pragma(
307 Parse *pParse,
308 Token *pId1, /* First part of [database.]id field */
309 Token *pId2, /* Second part of [database.]id field, or NULL */
310 Token *pValue, /* Token for <value>, or NULL */
311 int minusFlag /* True if a '-' sign preceded <value> */
312){
313 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
314 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
315 const char *zDb = 0; /* The database name */
316 Token *pId; /* Pointer to <id> token */
317 int iDb; /* Database index for <database> */
drh3fa97302012-02-22 16:58:36 +0000318 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh06fd5d62012-02-22 14:45:19 +0000319 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
320 sqlite3 *db = pParse->db; /* The database connection */
321 Db *pDb; /* The specific database being pragmaed */
322 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db); /* Prepared statement */
323
drhc11d4f92003-04-06 21:08:24 +0000324 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000325 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000326 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000327
danielk197791cf71b2004-06-26 06:37:06 +0000328 /* Interpret the [database.] part of the pragma statement. iDb is the
329 ** index of the database this pragma is being applied to in db.aDb[]. */
330 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
331 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000332 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000333
danielk1977ddfb2f02006-02-17 12:25:14 +0000334 /* If the temp database has been explicitly named as part of the
335 ** pragma, make sure it is open.
336 */
337 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
338 return;
339 }
340
drh17435752007-08-16 04:30:38 +0000341 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000342 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000343 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000344 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000345 }else{
drh17435752007-08-16 04:30:38 +0000346 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000347 }
danielk197791cf71b2004-06-26 06:37:06 +0000348
drhd2cb50b2009-01-09 21:41:17 +0000349 assert( pId2 );
350 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000351 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000352 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000353 }
drh06fd5d62012-02-22 14:45:19 +0000354
355 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
356 ** connection. If it returns SQLITE_OK, then assume that the VFS
357 ** handled the pragma and generate a no-op prepared statement.
358 */
drh3fa97302012-02-22 16:58:36 +0000359 aFcntl[0] = 0;
360 aFcntl[1] = zLeft;
361 aFcntl[2] = zRight;
362 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000363 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000364 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
365 if( rc==SQLITE_OK ){
drh3fa97302012-02-22 16:58:36 +0000366 if( aFcntl[0] ){
367 int mem = ++pParse->nMem;
368 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
369 sqlite3VdbeSetNumCols(v, 1);
370 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
371 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
372 sqlite3_free(aFcntl[0]);
373 }
drh92c700d2012-02-22 19:56:17 +0000374 }else if( rc!=SQLITE_NOTFOUND ){
375 if( aFcntl[0] ){
376 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
377 sqlite3_free(aFcntl[0]);
378 }
379 pParse->nErr++;
380 pParse->rc = rc;
drh8d936842012-02-24 00:03:12 +0000381 }else
drh06fd5d62012-02-22 14:45:19 +0000382
drhc11d4f92003-04-06 21:08:24 +0000383
drhe73c9142011-11-09 16:12:24 +0000384#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000385 /*
drh90f5ecb2004-07-22 01:19:35 +0000386 ** PRAGMA [database.]default_cache_size
387 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000388 **
389 ** The first form reports the current persistent setting for the
390 ** page cache size. The value returned is the maximum number of
391 ** pages in the page cache. The second form sets both the current
392 ** page cache size value and the persistent page cache size value
393 ** stored in the database file.
394 **
drh93791ea2010-04-26 17:36:35 +0000395 ** Older versions of SQLite would set the default cache size to a
396 ** negative number to indicate synchronous=OFF. These days, synchronous
397 ** is always on by default regardless of the sign of the default cache
398 ** size. But continue to take the absolute value of the default cache
399 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000400 */
danielk19774adee202004-05-08 08:23:19 +0000401 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000402 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000403 { OP_Transaction, 0, 0, 0}, /* 0 */
404 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
405 { OP_IfPos, 1, 7, 0},
drh3c84ddf2008-01-09 02:15:38 +0000406 { OP_Integer, 0, 2, 0},
407 { OP_Subtract, 1, 2, 1},
danielk1977602b4662009-07-02 07:47:33 +0000408 { OP_IfPos, 1, 7, 0},
409 { OP_Integer, 0, 1, 0}, /* 6 */
drh3c84ddf2008-01-09 02:15:38 +0000410 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000411 };
drh905793e2004-02-21 13:31:09 +0000412 int addr;
danielk19778a414492004-06-29 08:59:35 +0000413 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhfb982642007-08-30 01:19:59 +0000414 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000415 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000416 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000417 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000418 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000419 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000420 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000421 sqlite3VdbeChangeP1(v, addr+1, iDb);
422 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000423 }else{
drhd50ffc42011-03-08 02:38:28 +0000424 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000425 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000426 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000427 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000428 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000429 pDb->pSchema->cache_size = size;
430 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000431 }
432 }else
drhe73c9142011-11-09 16:12:24 +0000433#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000434
drhe73c9142011-11-09 16:12:24 +0000435#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000436 /*
drh90f5ecb2004-07-22 01:19:35 +0000437 ** PRAGMA [database.]page_size
438 ** PRAGMA [database.]page_size=N
439 **
440 ** The first form reports the current setting for the
441 ** database page size in bytes. The second form sets the
442 ** database page size value. The value can only be set if
443 ** the database has not yet been created.
444 */
445 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
446 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000447 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000448 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000449 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000450 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000451 }else{
danielk1977992772c2007-08-30 10:07:38 +0000452 /* Malloc may fail when setting the page-size, as there is an internal
453 ** buffer that the pager module resizes using sqlite3_realloc().
454 */
drh60ac3f42010-11-23 18:59:27 +0000455 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000456 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000457 db->mallocFailed = 1;
458 }
drh90f5ecb2004-07-22 01:19:35 +0000459 }
460 }else
danielk197741483462007-03-24 16:45:04 +0000461
462 /*
drh5b47efa2010-02-12 18:18:39 +0000463 ** PRAGMA [database.]secure_delete
464 ** PRAGMA [database.]secure_delete=ON/OFF
465 **
466 ** The first form reports the current setting for the
467 ** secure_delete flag. The second form changes the secure_delete
468 ** flag setting and reports thenew value.
469 */
470 if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
471 Btree *pBt = pDb->pBt;
472 int b = -1;
473 assert( pBt!=0 );
474 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000475 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000476 }
drhaf034ed2010-02-12 19:46:26 +0000477 if( pId2->n==0 && b>=0 ){
478 int ii;
479 for(ii=0; ii<db->nDb; ii++){
480 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
481 }
482 }
drh5b47efa2010-02-12 18:18:39 +0000483 b = sqlite3BtreeSecureDelete(pBt, b);
484 returnSingleInt(pParse, "secure_delete", b);
485 }else
486
487 /*
drh60ac3f42010-11-23 18:59:27 +0000488 ** PRAGMA [database.]max_page_count
489 ** PRAGMA [database.]max_page_count=N
490 **
491 ** The first form reports the current setting for the
492 ** maximum number of pages in the database file. The
493 ** second form attempts to change this setting. Both
494 ** forms return the current setting.
495 **
drhe73c9142011-11-09 16:12:24 +0000496 ** The absolute value of N is used. This is undocumented and might
497 ** change. The only purpose is to provide an easy way to test
498 ** the sqlite3AbsInt32() function.
499 **
danielk197759a93792008-05-15 17:48:20 +0000500 ** PRAGMA [database.]page_count
501 **
502 ** Return the number of pages in the specified database.
503 */
drh60ac3f42010-11-23 18:59:27 +0000504 if( sqlite3StrICmp(zLeft,"page_count")==0
505 || sqlite3StrICmp(zLeft,"max_page_count")==0
506 ){
danielk197759a93792008-05-15 17:48:20 +0000507 int iReg;
drhd2cb50b2009-01-09 21:41:17 +0000508 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197759a93792008-05-15 17:48:20 +0000509 sqlite3CodeVerifySchema(pParse, iDb);
510 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000511 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000512 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
513 }else{
drhe73c9142011-11-09 16:12:24 +0000514 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
515 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000516 }
danielk197759a93792008-05-15 17:48:20 +0000517 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
518 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000519 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk197759a93792008-05-15 17:48:20 +0000520 }else
521
522 /*
danielk197741483462007-03-24 16:45:04 +0000523 ** PRAGMA [database.]locking_mode
524 ** PRAGMA [database.]locking_mode = (normal|exclusive)
525 */
526 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
527 const char *zRet = "normal";
528 int eMode = getLockingMode(zRight);
529
530 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
531 /* Simple "PRAGMA locking_mode;" statement. This is a query for
532 ** the current default locking mode (which may be different to
533 ** the locking-mode of the main database).
534 */
535 eMode = db->dfltLockMode;
536 }else{
537 Pager *pPager;
538 if( pId2->n==0 ){
539 /* This indicates that no database name was specified as part
540 ** of the PRAGMA command. In this case the locking-mode must be
541 ** set on all attached databases, as well as the main db file.
542 **
543 ** Also, the sqlite3.dfltLockMode variable is set so that
544 ** any subsequently attached databases also use the specified
545 ** locking mode.
546 */
547 int ii;
548 assert(pDb==&db->aDb[0]);
549 for(ii=2; ii<db->nDb; ii++){
550 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
551 sqlite3PagerLockingMode(pPager, eMode);
552 }
drh4f21c4a2008-12-10 22:15:00 +0000553 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000554 }
555 pPager = sqlite3BtreePager(pDb->pBt);
556 eMode = sqlite3PagerLockingMode(pPager, eMode);
557 }
558
559 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
560 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
561 zRet = "exclusive";
562 }
563 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000564 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000565 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
566 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000567 }else
drh3b020132008-04-17 17:02:01 +0000568
569 /*
570 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000571 ** PRAGMA [database.]journal_mode =
572 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000573 */
574 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000575 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
576 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000577
drh94714062011-10-10 12:04:14 +0000578 /* Force the schema to be loaded on all databases. This causes all
579 ** database files to be opened and the journal_modes set. This is
580 ** necessary because subsequent processing must know if the databases
581 ** are in WAL mode. */
danf6c61472010-07-07 13:54:28 +0000582 if( sqlite3ReadSchema(pParse) ){
583 goto pragma_out;
584 }
585
dane04dc882010-04-20 18:53:15 +0000586 sqlite3VdbeSetNumCols(v, 1);
587 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000588
589 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000590 /* If there is no "=MODE" part of the pragma, do a query for the
591 ** current mode */
drh3b020132008-04-17 17:02:01 +0000592 eMode = PAGER_JOURNALMODE_QUERY;
593 }else{
dane04dc882010-04-20 18:53:15 +0000594 const char *zMode;
drhea678832008-12-10 19:26:22 +0000595 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000596 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000597 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
598 }
599 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000600 /* If the "=MODE" part does not match any known journal mode,
601 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000602 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000603 }
604 }
drhc6b2a0f2010-07-08 17:40:37 +0000605 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
606 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
607 iDb = 0;
608 pId2->n = 1;
609 }
610 for(ii=db->nDb-1; ii>=0; ii--){
611 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
612 sqlite3VdbeUsesBtree(v, ii);
613 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000614 }
drh3b020132008-04-17 17:02:01 +0000615 }
drh3b020132008-04-17 17:02:01 +0000616 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
617 }else
danielk1977b53e4962008-06-04 06:45:59 +0000618
619 /*
620 ** PRAGMA [database.]journal_size_limit
621 ** PRAGMA [database.]journal_size_limit=N
622 **
drha9e364f2009-01-13 20:14:15 +0000623 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000624 */
625 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
626 Pager *pPager = sqlite3BtreePager(pDb->pBt);
627 i64 iLimit = -2;
628 if( zRight ){
drh9339da12010-09-30 00:50:49 +0000629 sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
drh3c713642009-04-04 16:02:32 +0000630 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000631 }
632 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000633 returnSingleInt(pParse, "journal_size_limit", iLimit);
danielk1977b53e4962008-06-04 06:45:59 +0000634 }else
635
drh13d70422004-11-13 15:59:14 +0000636#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000637
638 /*
danielk1977951af802004-11-05 15:45:09 +0000639 ** PRAGMA [database.]auto_vacuum
640 ** PRAGMA [database.]auto_vacuum=N
641 **
drha9e364f2009-01-13 20:14:15 +0000642 ** Get or set the value of the database 'auto-vacuum' parameter.
643 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000644 */
645#ifndef SQLITE_OMIT_AUTOVACUUM
646 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
647 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000648 assert( pBt!=0 );
danielk197727b1f952007-06-25 08:16:58 +0000649 if( sqlite3ReadSchema(pParse) ){
650 goto pragma_out;
651 }
danielk1977951af802004-11-05 15:45:09 +0000652 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000653 int auto_vacuum;
654 if( ALWAYS(pBt) ){
655 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
656 }else{
657 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
658 }
danielk1977951af802004-11-05 15:45:09 +0000659 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
660 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000661 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000662 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000663 db->nextAutovac = (u8)eAuto;
drhd2cb50b2009-01-09 21:41:17 +0000664 if( ALWAYS(eAuto>=0) ){
danielk197727b1f952007-06-25 08:16:58 +0000665 /* Call SetAutoVacuum() to set initialize the internal auto and
666 ** incr-vacuum flags. This is required in case this connection
667 ** creates the database file. It is important that it is created
668 ** as an auto-vacuum capable db.
669 */
drh70708602012-02-24 14:33:28 +0000670 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
danielk197727b1f952007-06-25 08:16:58 +0000671 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
672 /* When setting the auto_vacuum mode to either "full" or
673 ** "incremental", write the value of meta[6] in the database
674 ** file. Before writing to meta[6], check that meta[3] indicates
675 ** that this really is an auto-vacuum capable database.
676 */
677 static const VdbeOpList setMeta6[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000678 { OP_Transaction, 0, 1, 0}, /* 0 */
679 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
680 { OP_If, 1, 0, 0}, /* 2 */
681 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
682 { OP_Integer, 0, 1, 0}, /* 4 */
683 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000684 };
685 int iAddr;
686 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
687 sqlite3VdbeChangeP1(v, iAddr, iDb);
688 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
689 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
690 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
691 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000692 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000693 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000694 }
danielk1977951af802004-11-05 15:45:09 +0000695 }
696 }else
697#endif
698
drhca5557f2007-05-04 18:30:40 +0000699 /*
700 ** PRAGMA [database.]incremental_vacuum(N)
701 **
702 ** Do N steps of incremental vacuuming on a database.
703 */
704#ifndef SQLITE_OMIT_AUTOVACUUM
705 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
706 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000707 if( sqlite3ReadSchema(pParse) ){
708 goto pragma_out;
709 }
drhca5557f2007-05-04 18:30:40 +0000710 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
711 iLimit = 0x7fffffff;
712 }
713 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000714 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000715 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000716 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000717 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000718 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000719 sqlite3VdbeJumpHere(v, addr);
720 }else
721#endif
722
drh13d70422004-11-13 15:59:14 +0000723#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000724 /*
drh90f5ecb2004-07-22 01:19:35 +0000725 ** PRAGMA [database.]cache_size
726 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000727 **
728 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000729 ** page cache size. The second form sets the local
730 ** page cache size value. If N is positive then that is the
731 ** number of pages in the cache. If N is negative, then the
732 ** number of pages is adjusted so that the cache uses -N kibibytes
733 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000734 */
danielk19774adee202004-05-08 08:23:19 +0000735 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000736 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh21206082011-04-04 18:22:02 +0000737 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000738 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000739 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000740 }else{
drh3b42abb2011-11-09 14:23:04 +0000741 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000742 pDb->pSchema->cache_size = size;
743 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000744 }
745 }else
746
747 /*
drh0d0614b2013-03-25 23:09:28 +0000748 ** PRAGMA [database.]mmap_limit(N)
dan5d8a1372013-03-19 19:28:06 +0000749 **
drh0d0614b2013-03-25 23:09:28 +0000750 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000751 ** used to limit the aggregate size of all memory mapped regions of the
752 ** database file. If this parameter is set to zero, then memory mapping
drh0d0614b2013-03-25 23:09:28 +0000753 ** is not used at all. The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000754 **
drh0d0614b2013-03-25 23:09:28 +0000755 ** This value is advisory. The underlying VFS is free to memory map
756 ** as little or as much as it wants. Except, if N is set to 0 then the
757 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000758 */
drh0d0614b2013-03-25 23:09:28 +0000759 if( sqlite3StrICmp(zLeft,"mmap_limit")==0 ){
dan5d8a1372013-03-19 19:28:06 +0000760 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000761 if( zRight ){
762 sqlite3_int64 size;
763 sqlite3Atoi64(zRight, &size, 1000, SQLITE_UTF8);
764 sqlite3BtreeSetMmapLimit(pDb->pBt, size);
dan5d8a1372013-03-19 19:28:06 +0000765 }
766 }else
767
768 /*
drh90f5ecb2004-07-22 01:19:35 +0000769 ** PRAGMA temp_store
770 ** PRAGMA temp_store = "default"|"memory"|"file"
771 **
772 ** Return or set the local value of the temp_store flag. Changing
773 ** the local value does not make changes to the disk file and the default
774 ** value will be restored the next time the database is opened.
775 **
776 ** Note that it is possible for the library compile-time options to
777 ** override this setting
778 */
779 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
780 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000781 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000782 }else{
783 changeTempStorage(pParse, zRight);
784 }
785 }else
786
787 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000788 ** PRAGMA temp_store_directory
789 ** PRAGMA temp_store_directory = ""|"directory_name"
790 **
791 ** Return or set the local value of the temp_store_directory flag. Changing
792 ** the value sets a specific directory to be used for temporary files.
793 ** Setting to a null string reverts to the default temporary directory search.
794 ** If temporary directory is changed, then invalidateTempStorage.
795 **
796 */
797 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
798 if( !zRight ){
799 if( sqlite3_temp_directory ){
800 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000801 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000802 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000803 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
804 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000805 }
806 }else{
drh78f82d12008-09-02 00:52:52 +0000807#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000808 if( zRight[0] ){
809 int res;
danielk1977fab11272008-09-16 14:38:02 +0000810 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
811 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000812 sqlite3ErrorMsg(pParse, "not a writable directory");
813 goto pragma_out;
814 }
drh268283b2005-01-08 15:44:25 +0000815 }
danielk1977b06a0b62008-06-26 10:54:12 +0000816 if( SQLITE_TEMP_STORE==0
817 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
818 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000819 ){
820 invalidateTempStorage(pParse);
821 }
drh17435752007-08-16 04:30:38 +0000822 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000823 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000824 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000825 }else{
drh268283b2005-01-08 15:44:25 +0000826 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000827 }
drh78f82d12008-09-02 00:52:52 +0000828#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000829 }
830 }else
831
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 */
846 if( sqlite3StrICmp(zLeft, "data_store_directory")==0 ){
847 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 }
873 }else
drhcc716452012-06-06 23:23:23 +0000874#endif
mistachkina112d142012-03-14 00:44:01 +0000875
drhd2cb50b2009-01-09 21:41:17 +0000876#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
877# if defined(__APPLE__)
878# define SQLITE_ENABLE_LOCKING_STYLE 1
879# else
880# define SQLITE_ENABLE_LOCKING_STYLE 0
881# endif
882#endif
883#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000884 /*
aswiftaebf4132008-11-21 00:10:35 +0000885 ** PRAGMA [database.]lock_proxy_file
886 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
887 **
888 ** Return or set the value of the lock_proxy_file flag. Changing
889 ** the value sets a specific file to be used for database access locks.
890 **
891 */
892 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
893 if( !zRight ){
894 Pager *pPager = sqlite3BtreePager(pDb->pBt);
895 char *proxy_file_path = NULL;
896 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000897 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000898 &proxy_file_path);
899
900 if( proxy_file_path ){
901 sqlite3VdbeSetNumCols(v, 1);
902 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
903 "lock_proxy_file", SQLITE_STATIC);
904 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
905 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
906 }
907 }else{
908 Pager *pPager = sqlite3BtreePager(pDb->pBt);
909 sqlite3_file *pFile = sqlite3PagerFile(pPager);
910 int res;
911 if( zRight[0] ){
912 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
913 zRight);
914 } else {
915 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
916 NULL);
917 }
918 if( res!=SQLITE_OK ){
919 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
920 goto pragma_out;
921 }
922 }
923 }else
drhd2cb50b2009-01-09 21:41:17 +0000924#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000925
926 /*
drh90f5ecb2004-07-22 01:19:35 +0000927 ** PRAGMA [database.]synchronous
928 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000929 **
930 ** Return or set the local value of the synchronous flag. Changing
931 ** the local value does not make changes to the disk file and the
932 ** default value will be restored the next time the database is
933 ** opened.
934 */
danielk19774adee202004-05-08 08:23:19 +0000935 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000936 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000937 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000938 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000939 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000940 if( !db->autoCommit ){
941 sqlite3ErrorMsg(pParse,
942 "Safety level may not be changed inside a transaction");
943 }else{
drh908c0052012-01-30 18:40:55 +0000944 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000945 }
drhc11d4f92003-04-06 21:08:24 +0000946 }
947 }else
drh13d70422004-11-13 15:59:14 +0000948#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000949
drhbf216272005-02-26 18:10:44 +0000950#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000951 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000952 /* The flagPragma() subroutine also generates any necessary code
953 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000954 }else
drhbf216272005-02-26 18:10:44 +0000955#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000956
drh13d70422004-11-13 15:59:14 +0000957#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000958 /*
959 ** PRAGMA table_info(<table>)
960 **
961 ** Return a single row for each column of the named table. The columns of
962 ** the returned data set are:
963 **
964 ** cid: Column id (numbered from left to right, starting at 0)
965 ** name: Column name
966 ** type: Column declaration type.
967 ** notnull: True if 'NOT NULL' is part of column declaration
968 ** dflt_value: The default value for the column, if any.
969 */
drh5260f7e2004-06-26 19:35:29 +0000970 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000971 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000972 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000973 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000974 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +0000975 int i, k;
danielk1977034ca142007-06-26 10:38:54 +0000976 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +0000977 Column *pCol;
drh384b7fe2013-01-01 13:55:31 +0000978 Index *pPk;
979 for(pPk=pTab->pIndex; pPk && pPk->autoIndex!=2; pPk=pPk->pNext){}
drh9f6696a2006-02-09 16:52:23 +0000980 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +0000981 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +0000982 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +0000983 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
984 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
985 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
986 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
987 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
988 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000989 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000990 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +0000991 if( IsHiddenColumn(pCol) ){
992 nHidden++;
993 continue;
994 }
drh2d401ab2008-01-10 23:50:11 +0000995 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
996 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
997 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +0000998 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +0000999 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +00001000 if( pCol->zDflt ){
1001 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +00001002 }else{
drh2d401ab2008-01-10 23:50:11 +00001003 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +00001004 }
drh384b7fe2013-01-01 13:55:31 +00001005 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1006 k = 0;
1007 }else if( pPk==0 ){
1008 k = 1;
1009 }else{
1010 for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
1011 }
1012 sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
drh2d401ab2008-01-10 23:50:11 +00001013 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001014 }
1015 }
1016 }else
1017
drh5260f7e2004-06-26 19:35:29 +00001018 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +00001019 Index *pIdx;
1020 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +00001021 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001022 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001023 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001024 int i;
1025 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +00001026 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001027 pParse->nMem = 3;
drhc95e01d2013-02-14 16:16:05 +00001028 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001029 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
1030 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
1031 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001032 for(i=0; i<pIdx->nColumn; i++){
1033 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +00001034 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1035 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +00001036 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +00001037 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
1038 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +00001039 }
1040 }
1041 }else
1042
drh5260f7e2004-06-26 19:35:29 +00001043 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +00001044 Index *pIdx;
1045 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +00001046 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001047 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001048 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001049 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +00001050 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +00001051 if( pIdx ){
1052 int i = 0;
1053 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001054 pParse->nMem = 3;
drhc95e01d2013-02-14 16:16:05 +00001055 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001056 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1057 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1058 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001059 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +00001060 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1061 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1062 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
1063 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +00001064 ++i;
1065 pIdx = pIdx->pNext;
1066 }
drhc11d4f92003-04-06 21:08:24 +00001067 }
1068 }
1069 }else
1070
drh13d70422004-11-13 15:59:14 +00001071 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
1072 int i;
1073 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
1074 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001075 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +00001076 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1077 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1078 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +00001079 for(i=0; i<db->nDb; i++){
1080 if( db->aDb[i].pBt==0 ) continue;
1081 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +00001082 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1083 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
1084 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +00001085 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +00001086 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001087 }
1088 }else
danielk197748af65a2005-02-09 03:20:37 +00001089
1090 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
1091 int i = 0;
1092 HashElem *p;
1093 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001094 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001095 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1096 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +00001097 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1098 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001099 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1100 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1101 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001102 }
1103 }else
drh13d70422004-11-13 15:59:14 +00001104#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1105
drhb7f91642004-10-31 02:22:47 +00001106#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +00001107 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +00001108 FKey *pFK;
1109 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +00001110 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001111 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001112 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001113 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001114 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001115 if( pFK ){
1116 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001117 sqlite3VdbeSetNumCols(v, 8);
1118 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001119 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001120 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1121 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1122 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1123 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1124 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1125 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1126 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1127 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001128 while(pFK){
1129 int j;
1130 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001131 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001132 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1133 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001134 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1135 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1136 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1137 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001138 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001139 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001140 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1141 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1142 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1143 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001144 }
1145 ++i;
1146 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001147 }
drh78100cc2003-08-23 22:40:53 +00001148 }
1149 }
1150 }else
drhb7f91642004-10-31 02:22:47 +00001151#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001152
drh6c5b9152012-12-17 16:46:37 +00001153#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001154#ifndef SQLITE_OMIT_TRIGGER
drh613028b2012-12-17 18:43:02 +00001155 if( sqlite3StrICmp(zLeft, "foreign_key_check")==0 ){
1156 FKey *pFK; /* A foreign key constraint */
1157 Table *pTab; /* Child table contain "REFERENCES" keyword */
1158 Table *pParent; /* Parent table that child points to */
1159 Index *pIdx; /* Index in the parent table */
1160 int i; /* Loop counter: Foreign key number for pTab */
1161 int j; /* Loop counter: Field of the foreign key */
1162 HashElem *k; /* Loop counter: Next table in schema */
1163 int x; /* result variable */
1164 int regResult; /* 3 registers to hold a result row */
1165 int regKey; /* Register to hold key for checking the FK */
1166 int regRow; /* Registers to hold a row from pTab */
1167 int addrTop; /* Top of a loop checking foreign keys */
1168 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001169 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001170
1171 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh613028b2012-12-17 18:43:02 +00001172 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001173 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001174 regKey = ++pParse->nMem;
1175 regRow = ++pParse->nMem;
1176 v = sqlite3GetVdbe(pParse);
drh4b4b4732012-12-17 20:57:15 +00001177 sqlite3VdbeSetNumCols(v, 4);
drh613028b2012-12-17 18:43:02 +00001178 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1179 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
drh4b4b4732012-12-17 20:57:15 +00001180 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
1181 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
drh613028b2012-12-17 18:43:02 +00001182 sqlite3CodeVerifySchema(pParse, iDb);
1183 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1184 while( k ){
1185 if( zRight ){
1186 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1187 k = 0;
1188 }else{
1189 pTab = (Table*)sqliteHashData(k);
1190 k = sqliteHashNext(k);
1191 }
drh9148def2012-12-17 20:40:39 +00001192 if( pTab==0 || pTab->pFKey==0 ) continue;
1193 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001194 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001195 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh613028b2012-12-17 18:43:02 +00001196 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
1197 P4_TRANSIENT);
drh6c5b9152012-12-17 16:46:37 +00001198 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1199 pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
1200 if( pParent==0 ) break;
1201 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001202 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001203 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001204 if( x==0 ){
1205 if( pIdx==0 ){
1206 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1207 }else{
1208 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
1209 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
1210 sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
1211 }
1212 }else{
drh613028b2012-12-17 18:43:02 +00001213 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001214 break;
1215 }
drh6c5b9152012-12-17 16:46:37 +00001216 }
drh613028b2012-12-17 18:43:02 +00001217 if( pFK ) break;
1218 if( pParse->nTab<i ) pParse->nTab = i;
1219 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
1220 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1221 pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
1222 assert( pParent!=0 );
1223 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001224 aiCols = 0;
1225 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
drh613028b2012-12-17 18:43:02 +00001226 assert( x==0 );
1227 addrOk = sqlite3VdbeMakeLabel(v);
1228 if( pIdx==0 ){
1229 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001230 assert( iKey>=0 && iKey<pTab->nCol );
1231 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001232 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1233 sqlite3ColumnDefault(v, pTab, iKey, regRow);
1234 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
1235 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1236 sqlite3VdbeCurrentAddr(v)+3);
drh6c5b9152012-12-17 16:46:37 +00001237 }else{
drh613028b2012-12-17 18:43:02 +00001238 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001239 }
drh613028b2012-12-17 18:43:02 +00001240 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
1241 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
1242 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1243 }else{
1244 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001245 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
1246 aiCols ? aiCols[j] : pFK->aCol[0].iFrom, regRow+j);
drh613028b2012-12-17 18:43:02 +00001247 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
1248 }
1249 sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
1250 sqlite3VdbeChangeP4(v, -1,
1251 sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
1252 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh6c5b9152012-12-17 16:46:37 +00001253 }
drh613028b2012-12-17 18:43:02 +00001254 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh4b4b4732012-12-17 20:57:15 +00001255 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
1256 pFK->zTo, P4_TRANSIENT);
1257 sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
1258 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001259 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001260 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001261 }
drh613028b2012-12-17 18:43:02 +00001262 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
1263 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001264 }
1265 }else
dan09ff9e12013-03-11 11:49:03 +00001266#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001267#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1268
drhc11d4f92003-04-06 21:08:24 +00001269#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +00001270 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +00001271 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001272 if( sqlite3GetBoolean(zRight, 0) ){
drh55ef4d92005-08-14 01:20:37 +00001273 sqlite3ParserTrace(stderr, "parser: ");
1274 }else{
1275 sqlite3ParserTrace(0, 0);
1276 }
drhc11d4f92003-04-06 21:08:24 +00001277 }
1278 }else
1279#endif
1280
drh55ef4d92005-08-14 01:20:37 +00001281 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1282 ** used will be case sensitive or not depending on the RHS.
1283 */
1284 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
1285 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001286 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001287 }
1288 }else
1289
drh1dcdbc02007-01-27 02:24:54 +00001290#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1291# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1292#endif
1293
drhb7f91642004-10-31 02:22:47 +00001294#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk197741c58b72007-12-29 13:39:19 +00001295 /* Pragma "quick_check" is an experimental reduced version of
1296 ** integrity_check designed to detect most database corruption
1297 ** without most of the overhead of a full integrity-check.
1298 */
1299 if( sqlite3StrICmp(zLeft, "integrity_check")==0
1300 || sqlite3StrICmp(zLeft, "quick_check")==0
1301 ){
drh1dcdbc02007-01-27 02:24:54 +00001302 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001303
drhed717fe2003-06-15 23:42:24 +00001304 /* Code that appears at the end of the integrity check. If no error
1305 ** messages have been generated, output OK. Otherwise output the
1306 ** error message
1307 */
drh57196282004-10-06 15:41:16 +00001308 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001309 { OP_AddImm, 1, 0, 0}, /* 0 */
1310 { OP_IfNeg, 1, 0, 0}, /* 1 */
1311 { OP_String8, 0, 3, 0}, /* 2 */
1312 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001313 };
drhed717fe2003-06-15 23:42:24 +00001314
drhc5227312011-10-13 17:09:01 +00001315 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001316
dan5885e762012-07-16 10:06:12 +00001317 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1318 ** then iDb is set to the index of the database identified by <db>.
1319 ** In this case, the integrity of database iDb only is verified by
1320 ** the VDBE created below.
1321 **
1322 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1323 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1324 ** to -1 here, to indicate that the VDBE should verify the integrity
1325 ** of all attached databases. */
1326 assert( iDb>=0 );
1327 assert( iDb==0 || pId2->z );
1328 if( pId2->z==0 ) iDb = -1;
1329
drhed717fe2003-06-15 23:42:24 +00001330 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +00001331 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +00001332 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001333 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001334 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001335
1336 /* Set the maximum error count */
1337 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1338 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001339 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001340 if( mxErr<=0 ){
1341 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1342 }
1343 }
drh2d401ab2008-01-10 23:50:11 +00001344 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001345
1346 /* Do an integrity check on each database file */
1347 for(i=0; i<db->nDb; i++){
1348 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001349 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001350 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001351
danielk197753c0f742005-03-29 03:10:59 +00001352 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001353 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001354
drh80242052004-06-09 00:48:12 +00001355 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001356 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001357 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001358 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001359
drhed717fe2003-06-15 23:42:24 +00001360 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001361 **
1362 ** Begin by filling registers 2, 3, ... with the root pages numbers
1363 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001364 */
dan5885e762012-07-16 10:06:12 +00001365 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001366 pTbls = &db->aDb[i].pSchema->tblHash;
1367 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001368 Table *pTab = sqliteHashData(x);
1369 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001370 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001371 cnt++;
1372 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001373 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001374 cnt++;
1375 }
1376 }
drh2d401ab2008-01-10 23:50:11 +00001377
1378 /* Make sure sufficient number of registers have been allocated */
danielk1977a7a8e142008-02-13 18:25:27 +00001379 if( pParse->nMem < cnt+4 ){
1380 pParse->nMem = cnt+4;
drh98757152008-01-09 23:04:12 +00001381 }
drh2d401ab2008-01-10 23:50:11 +00001382
1383 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001384 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001385 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001386 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1387 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001388 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001389 P4_DYNAMIC);
drhe8e4af72012-09-21 00:04:28 +00001390 sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
danielk1977a7a8e142008-02-13 18:25:27 +00001391 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001392 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001393 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001394
1395 /* Make sure all the indices are constructed correctly.
1396 */
danielk197741c58b72007-12-29 13:39:19 +00001397 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001398 Table *pTab = sqliteHashData(x);
1399 Index *pIdx;
1400 int loopTop;
1401
1402 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001403 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001404 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001405 sqlite3VdbeJumpHere(v, addr);
drh290c1942004-08-21 17:54:45 +00001406 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh2d401ab2008-01-10 23:50:11 +00001407 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
drh66a51672008-01-03 00:01:23 +00001408 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
drh2d401ab2008-01-10 23:50:11 +00001409 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
drhed717fe2003-06-15 23:42:24 +00001410 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +00001411 int jmp2;
drh91fc4a02009-11-12 20:39:03 +00001412 int r1;
drh57196282004-10-06 15:41:16 +00001413 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001414 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001415 { OP_String8, 0, 3, 0}, /* 1 */
1416 { OP_Rowid, 1, 4, 0},
1417 { OP_String8, 0, 5, 0}, /* 3 */
1418 { OP_String8, 0, 6, 0}, /* 4 */
1419 { OP_Concat, 4, 3, 3},
1420 { OP_Concat, 5, 3, 3},
1421 { OP_Concat, 6, 3, 3},
1422 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001423 { OP_IfPos, 1, 0, 0}, /* 9 */
1424 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001425 };
drh91fc4a02009-11-12 20:39:03 +00001426 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
1427 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
danielk19774adee202004-05-08 08:23:19 +00001428 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001429 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1430 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh8d129422011-04-05 12:25:19 +00001431 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
drhbb8a2792008-03-19 00:21:30 +00001432 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001433 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +00001434 }
drh66a51672008-01-03 00:01:23 +00001435 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +00001436 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +00001437 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +00001438 static const VdbeOpList cntIdx[] = {
drh4c583122008-01-04 22:01:03 +00001439 { OP_Integer, 0, 3, 0},
drhd654be82005-09-20 17:42:23 +00001440 { OP_Rewind, 0, 0, 0}, /* 1 */
drh8558cde2008-01-05 05:20:10 +00001441 { OP_AddImm, 3, 1, 0},
drhd654be82005-09-20 17:42:23 +00001442 { OP_Next, 0, 0, 0}, /* 3 */
drh2d401ab2008-01-10 23:50:11 +00001443 { OP_Eq, 2, 0, 3}, /* 4 */
drh8558cde2008-01-05 05:20:10 +00001444 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001445 { OP_String8, 0, 2, 0}, /* 6 */
1446 { OP_String8, 0, 3, 0}, /* 7 */
1447 { OP_Concat, 3, 2, 2},
1448 { OP_ResultRow, 2, 1, 0},
drhed717fe2003-06-15 23:42:24 +00001449 };
drh3c84ddf2008-01-09 02:15:38 +00001450 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
drh66a51672008-01-03 00:01:23 +00001451 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001452 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001453 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +00001454 sqlite3VdbeChangeP1(v, addr+1, j+2);
1455 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1456 sqlite3VdbeChangeP1(v, addr+3, j+2);
1457 sqlite3VdbeChangeP2(v, addr+3, addr+2);
drh2d401ab2008-01-10 23:50:11 +00001458 sqlite3VdbeJumpHere(v, addr+4);
1459 sqlite3VdbeChangeP4(v, addr+6,
drh24003452008-01-03 01:28:59 +00001460 "wrong # of entries in index ", P4_STATIC);
drh8d129422011-04-05 12:25:19 +00001461 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
drhed717fe2003-06-15 23:42:24 +00001462 }
1463 }
1464 }
danielk19774adee202004-05-08 08:23:19 +00001465 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001466 sqlite3VdbeChangeP2(v, addr, -mxErr);
1467 sqlite3VdbeJumpHere(v, addr+1);
1468 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001469 }else
drhb7f91642004-10-31 02:22:47 +00001470#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1471
drh13d70422004-11-13 15:59:14 +00001472#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001473 /*
1474 ** PRAGMA encoding
1475 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1476 **
drh85b623f2007-12-13 21:54:09 +00001477 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001478 ** database. If the database is not initialized, it is initialized now.
1479 **
1480 ** The second form of this pragma is a no-op if the main database file
1481 ** has not already been initialized. In this case it sets the default
1482 ** encoding that will be used for the main database file if a new file
1483 ** is created. If an existing main database file is opened, then the
1484 ** default text encoding for the existing database is used.
1485 **
1486 ** In all cases new databases created using the ATTACH command are
1487 ** created to use the same default text encoding as the main database. If
1488 ** the main database has not been initialized and/or created when ATTACH
1489 ** is executed, this is done before the ATTACH operation.
1490 **
1491 ** In the second form this pragma sets the text encoding to be used in
1492 ** new database files created using this database handle. It is only
1493 ** useful if invoked immediately after the main database i
1494 */
1495 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001496 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001497 char *zName;
1498 u8 enc;
1499 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001500 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001501 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1502 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1503 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001504 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001505 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001506 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1507 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001508 { 0, 0 }
1509 };
drh0f7eb612006-08-08 13:51:43 +00001510 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001511 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001512 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001513 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001514 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001515 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001516 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1517 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1518 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1519 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001520 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001521 }else{ /* "PRAGMA encoding = XXX" */
1522 /* Only change the value of sqlite.enc if the database handle is not
1523 ** initialized. If the main database exists, the new sqlite.enc value
1524 ** will be overwritten when the schema is next loaded. If it does not
1525 ** already exists, it will be created to use the new encoding value.
1526 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001527 if(
1528 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1529 DbHasProperty(db, 0, DB_Empty)
1530 ){
danielk19778e227872004-06-07 07:52:17 +00001531 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1532 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001533 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001534 break;
1535 }
1536 }
1537 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001538 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001539 }
1540 }
1541 }
1542 }else
drh13d70422004-11-13 15:59:14 +00001543#endif /* SQLITE_OMIT_UTF16 */
1544
1545#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001546 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001547 ** PRAGMA [database.]schema_version
1548 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001549 **
danielk1977b92b70b2004-11-12 16:11:59 +00001550 ** PRAGMA [database.]user_version
1551 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001552 **
danielk1977b92b70b2004-11-12 16:11:59 +00001553 ** The pragma's schema_version and user_version are used to set or get
1554 ** the value of the schema-version and user-version, respectively. Both
1555 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001556 ** stored in the database header.
1557 **
1558 ** The schema-cookie is usually only manipulated internally by SQLite. It
1559 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001560 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001561 ** SQLite each time a query is executed to ensure that the internal cache
1562 ** of the schema used when compiling the SQL query matches the schema of
1563 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001564 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1565 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001566 ** crashes or database corruption. Use with caution!
1567 **
danielk1977b92b70b2004-11-12 16:11:59 +00001568 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001569 ** applications for any purpose.
1570 */
danielk1977180b56a2007-06-24 08:00:42 +00001571 if( sqlite3StrICmp(zLeft, "schema_version")==0
1572 || sqlite3StrICmp(zLeft, "user_version")==0
1573 || sqlite3StrICmp(zLeft, "freelist_count")==0
1574 ){
danielk19770d19f7a2009-06-03 11:25:07 +00001575 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001576 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001577 switch( zLeft[0] ){
danielk1977180b56a2007-06-24 08:00:42 +00001578 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00001579 iCookie = BTREE_FREE_PAGE_COUNT;
1580 break;
1581 case 's': case 'S':
1582 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001583 break;
1584 default:
danielk19770d19f7a2009-06-03 11:25:07 +00001585 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001586 break;
danielk1977dae24952004-11-11 05:10:43 +00001587 }
1588
danielk19770d19f7a2009-06-03 11:25:07 +00001589 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00001590 /* Write the specified cookie value */
1591 static const VdbeOpList setCookie[] = {
1592 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001593 { OP_Integer, 0, 1, 0}, /* 1 */
1594 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001595 };
1596 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1597 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00001598 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00001599 sqlite3VdbeChangeP1(v, addr+2, iDb);
1600 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1601 }else{
1602 /* Read the specified cookie value */
1603 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001604 { OP_Transaction, 0, 0, 0}, /* 0 */
1605 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001606 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001607 };
1608 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1609 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001610 sqlite3VdbeChangeP1(v, addr+1, iDb);
1611 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001612 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001613 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001614 }
drh6e345992007-03-30 11:12:08 +00001615 }else
drh13d70422004-11-13 15:59:14 +00001616#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001617
shanehdc97a8c2010-02-23 20:08:35 +00001618#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1619 /*
1620 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001621 **
drh71caabf2010-02-26 15:39:24 +00001622 ** Return the names of all compile-time options used in this build,
1623 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001624 */
drh264a2d42010-02-25 15:28:41 +00001625 if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
shanehdc97a8c2010-02-23 20:08:35 +00001626 int i = 0;
1627 const char *zOpt;
1628 sqlite3VdbeSetNumCols(v, 1);
1629 pParse->nMem = 1;
1630 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1631 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1632 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1633 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1634 }
1635 }else
1636#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1637
dan5cf53532010-05-01 16:40:20 +00001638#ifndef SQLITE_OMIT_WAL
1639 /*
dancdc1f042010-11-18 12:11:05 +00001640 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
dan5cf53532010-05-01 16:40:20 +00001641 **
1642 ** Checkpoint the database.
1643 */
dan5a299f92010-05-03 11:05:08 +00001644 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
dana58f26f2010-11-16 18:56:51 +00001645 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001646 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1647 if( zRight ){
1648 if( sqlite3StrICmp(zRight, "full")==0 ){
1649 eMode = SQLITE_CHECKPOINT_FULL;
1650 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1651 eMode = SQLITE_CHECKPOINT_RESTART;
1652 }
1653 }
dan5a299f92010-05-03 11:05:08 +00001654 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
dancdc1f042010-11-18 12:11:05 +00001655 sqlite3VdbeSetNumCols(v, 3);
1656 pParse->nMem = 3;
1657 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
1658 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
1659 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
1660
drh30aa3b92011-02-07 23:56:01 +00001661 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001662 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
dan7c246102010-04-12 19:00:29 +00001663 }else
dan5a299f92010-05-03 11:05:08 +00001664
1665 /*
1666 ** PRAGMA wal_autocheckpoint
1667 ** PRAGMA wal_autocheckpoint = N
1668 **
1669 ** Configure a database connection to automatically checkpoint a database
1670 ** after accumulating N frames in the log. Or query for the current value
1671 ** of N.
1672 */
1673 if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
1674 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001675 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001676 }
drhb033d8b2010-05-03 13:37:30 +00001677 returnSingleInt(pParse, "wal_autocheckpoint",
1678 db->xWalCallback==sqlite3WalDefaultHook ?
1679 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
dan5a299f92010-05-03 11:05:08 +00001680 }else
dan5cf53532010-05-01 16:40:20 +00001681#endif
dan7c246102010-04-12 19:00:29 +00001682
drh09419b42011-11-16 19:29:17 +00001683 /*
1684 ** PRAGMA shrink_memory
1685 **
1686 ** This pragma attempts to free as much memory as possible from the
1687 ** current database connection.
1688 */
1689 if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
1690 sqlite3_db_release_memory(db);
1691 }else
1692
drhf3603962012-09-07 16:46:59 +00001693 /*
1694 ** PRAGMA busy_timeout
1695 ** PRAGMA busy_timeout = N
1696 **
1697 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001698 ** if one is set. If no busy handler or a different busy handler is set
1699 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1700 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001701 */
1702 if( sqlite3StrICmp(zLeft, "busy_timeout")==0 ){
1703 if( zRight ){
1704 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1705 }
1706 returnSingleInt(pParse, "timeout", db->busyTimeout);
1707 }else
1708
dougcurrie81c95ef2004-06-18 23:21:47 +00001709#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001710 /*
1711 ** Report the current state of file logs for all databases
1712 */
1713 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001714 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001715 "unlocked", "shared", "reserved", "pending", "exclusive"
1716 };
1717 int i;
drh89ac8c12004-06-09 14:17:20 +00001718 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001719 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001720 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1721 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001722 for(i=0; i<db->nDb; i++){
1723 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001724 const char *zState = "unknown";
1725 int j;
drh89ac8c12004-06-09 14:17:20 +00001726 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001727 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001728 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001729 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001730 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001731 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001732 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1733 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001734 }
drh2d401ab2008-01-10 23:50:11 +00001735 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1736 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001737 }
danielk1977a4de4532008-09-02 15:44:08 +00001738
drh89ac8c12004-06-09 14:17:20 +00001739 }else
1740#endif
1741
shanehad9f9f62010-02-17 17:48:46 +00001742#ifdef SQLITE_HAS_CODEC
drhd2cb50b2009-01-09 21:41:17 +00001743 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
drhea678832008-12-10 19:26:22 +00001744 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
drh3c4f2a42005-12-08 18:12:56 +00001745 }else
drhd2cb50b2009-01-09 21:41:17 +00001746 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
1747 sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
1748 }else
1749 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
1750 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
1751 int i, h1, h2;
1752 char zKey[40];
1753 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
1754 h1 += 9*(1&(h1>>6));
1755 h2 += 9*(1&(h2>>6));
1756 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
1757 }
1758 if( (zLeft[3] & 0xf)==0xb ){
1759 sqlite3_key(db, zKey, i/2);
1760 }else{
1761 sqlite3_rekey(db, zKey, i/2);
1762 }
1763 }else
drh3c4f2a42005-12-08 18:12:56 +00001764#endif
shanehad9f9f62010-02-17 17:48:46 +00001765#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh7fe18b42013-01-16 20:33:02 +00001766 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 && zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001767#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001768 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001769 sqlite3_activate_see(&zRight[4]);
1770 }
1771#endif
1772#ifdef SQLITE_ENABLE_CEROD
1773 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001774 sqlite3_activate_cerod(&zRight[6]);
1775 }
1776#endif
drhd2cb50b2009-01-09 21:41:17 +00001777 }else
drh21e2cab2006-09-25 18:01:57 +00001778#endif
drh3c4f2a42005-12-08 18:12:56 +00001779
drhd2cb50b2009-01-09 21:41:17 +00001780
1781 {/* Empty ELSE clause */}
danielk1977a21c6b62005-01-24 10:25:59 +00001782
drhd2cb50b2009-01-09 21:41:17 +00001783 /*
1784 ** Reset the safety level, in case the fullfsync flag or synchronous
1785 ** setting changed.
1786 */
danielk1977ddfb2f02006-02-17 12:25:14 +00001787#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhd2cb50b2009-01-09 21:41:17 +00001788 if( db->autoCommit ){
1789 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
drhc97d8462010-11-19 18:23:35 +00001790 (db->flags&SQLITE_FullFSync)!=0,
1791 (db->flags&SQLITE_CkptFullFSync)!=0);
danielk1977a21c6b62005-01-24 10:25:59 +00001792 }
drhd2cb50b2009-01-09 21:41:17 +00001793#endif
danielk1977e0048402004-06-15 16:51:01 +00001794pragma_out:
drh633e6d52008-07-28 19:34:53 +00001795 sqlite3DbFree(db, zLeft);
1796 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001797}
drh13d70422004-11-13 15:59:14 +00001798
drh8bfdf722009-06-19 14:06:03 +00001799#endif /* SQLITE_OMIT_PRAGMA */