blob: a467b62897cd1a6f129adab24a8c4fb697a937a2 [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.
13**
aswiftaebf4132008-11-21 00:10:35 +000014** $Id: pragma.c,v 1.195 2008/11/21 00:10:35 aswift Exp $
drhc11d4f92003-04-06 21:08:24 +000015*/
16#include "sqliteInt.h"
drh13bff812003-04-15 01:19:47 +000017#include <ctype.h>
drhc11d4f92003-04-06 21:08:24 +000018
drh13d70422004-11-13 15:59:14 +000019/* Ignore this whole file if pragmas are disabled
20*/
drh0ccebe72005-06-07 22:22:50 +000021#if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
drh13d70422004-11-13 15:59:14 +000022
drhc11d4f92003-04-06 21:08:24 +000023/*
drhc11d4f92003-04-06 21:08:24 +000024** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +000025** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
26** unrecognized string argument.
drhc11d4f92003-04-06 21:08:24 +000027**
28** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000029** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000030** to support legacy SQL code. The safety level used to be boolean
31** and older scripts may have used numbers 0 for OFF and 1 for ON.
32*/
drh2646da72005-12-09 20:02:05 +000033static int getSafetyLevel(const char *z){
drh722e95a2004-10-25 20:33:44 +000034 /* 123456789 123456789 */
35 static const char zText[] = "onoffalseyestruefull";
36 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
37 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
38 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
39 int i, n;
40 if( isdigit(*z) ){
drhc11d4f92003-04-06 21:08:24 +000041 return atoi(z);
42 }
drh722e95a2004-10-25 20:33:44 +000043 n = strlen(z);
danielk197700e13612008-11-17 19:18:54 +000044 for(i=0; i<ArraySize(iLength); i++){
drh722e95a2004-10-25 20:33:44 +000045 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
46 return iValue[i];
47 }
drhc11d4f92003-04-06 21:08:24 +000048 }
49 return 1;
50}
51
52/*
drh722e95a2004-10-25 20:33:44 +000053** Interpret the given string as a boolean value.
54*/
drh2646da72005-12-09 20:02:05 +000055static int getBoolean(const char *z){
drh722e95a2004-10-25 20:33:44 +000056 return getSafetyLevel(z)&1;
57}
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;
82 i = atoi(z);
83 return ((i>=0&&i<=2)?i:0);
84}
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;
121 sqlite3ResetInternalSchema(db, 0);
122 }
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 }
drh90f5ecb2004-07-22 01:19:35 +0000140 db->temp_store = ts;
141 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*/
drh5bb7ffe2004-09-02 15:14:00 +0000148static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
149 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000150 int mem = ++pParse->nMem;
drh4c583122008-01-04 22:01:03 +0000151 sqlite3VdbeAddOp2(v, OP_Integer, value, mem);
drh5bb7ffe2004-09-02 15:14:00 +0000152 if( pParse->explain==0 ){
153 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000154 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh5bb7ffe2004-09-02 15:14:00 +0000155 }
drh66a51672008-01-03 00:01:23 +0000156 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000157}
158
drhbf216272005-02-26 18:10:44 +0000159#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000160/*
drh87223182004-02-21 14:00:29 +0000161** Check to see if zRight and zLeft refer to a pragma that queries
162** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
163** Also, implement the pragma.
164*/
165static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000166 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000167 const char *zName; /* Name of the pragma */
168 int mask; /* Mask for the db->flags value */
169 } aPragma[] = {
drh87223182004-02-21 14:00:29 +0000170 { "full_column_names", SQLITE_FullColNames },
171 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000172 { "count_changes", SQLITE_CountRows },
173 { "empty_result_callbacks", SQLITE_NullCallback },
drhe321c292006-01-12 01:56:43 +0000174 { "legacy_file_format", SQLITE_LegacyFileFmt },
drhac530b12006-02-11 01:25:50 +0000175 { "fullfsync", SQLITE_FullFSync },
drhcf1023c2007-05-08 20:59:49 +0000176#ifdef SQLITE_DEBUG
177 { "sql_trace", SQLITE_SqlTrace },
178 { "vdbe_listing", SQLITE_VdbeListing },
179 { "vdbe_trace", SQLITE_VdbeTrace },
180#endif
drh0cd2d4c2005-11-03 02:15:02 +0000181#ifndef SQLITE_OMIT_CHECK
182 { "ignore_check_constraints", SQLITE_IgnoreChecks },
183#endif
drh722e95a2004-10-25 20:33:44 +0000184 /* The following is VERY experimental */
danielk197734c68fb2007-03-14 15:37:04 +0000185 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
drh7bec5052005-02-06 02:45:41 +0000186 { "omit_readlock", SQLITE_NoReadlock },
danielk1977da184232006-01-05 11:34:32 +0000187
188 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
189 ** flag if there are any active statements. */
190 { "read_uncommitted", SQLITE_ReadUncommitted },
drh87223182004-02-21 14:00:29 +0000191 };
192 int i;
drh722e95a2004-10-25 20:33:44 +0000193 const struct sPragmaType *p;
danielk197700e13612008-11-17 19:18:54 +0000194 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
drh722e95a2004-10-25 20:33:44 +0000195 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000196 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000197 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000198 v = sqlite3GetVdbe(pParse);
199 if( v ){
200 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000201 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000202 }else{
danielk1977a21c6b62005-01-24 10:25:59 +0000203 if( getBoolean(zRight) ){
204 db->flags |= p->mask;
205 }else{
206 db->flags &= ~p->mask;
207 }
danielk19778e556522007-11-13 10:30:24 +0000208
209 /* Many of the flag-pragmas modify the code generated by the SQL
210 ** compiler (eg. count_changes). So add an opcode to expire all
211 ** compiled SQL statements after modifying a pragma value.
212 */
drh66a51672008-01-03 00:01:23 +0000213 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000214 }
drh87223182004-02-21 14:00:29 +0000215 }
danielk19778e556522007-11-13 10:30:24 +0000216
drh87223182004-02-21 14:00:29 +0000217 return 1;
218 }
219 }
220 return 0;
221}
drhbf216272005-02-26 18:10:44 +0000222#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000223
danielk197750af3e12008-10-10 17:47:21 +0000224static const char *actionName(u8 action){
225 switch( action ){
226 case OE_SetNull: return "SET NULL";
227 case OE_SetDflt: return "SET DEFAULT";
228 case OE_Restrict: return "RESTRICT";
229 case OE_Cascade: return "CASCADE";
230 }
231 return "";
232}
233
drh87223182004-02-21 14:00:29 +0000234/*
drhc11d4f92003-04-06 21:08:24 +0000235** Process a pragma statement.
236**
237** Pragmas are of this form:
238**
danielk197791cf71b2004-06-26 06:37:06 +0000239** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000240**
241** The identifier might also be a string. The value is a string, and
242** identifier, or a number. If minusFlag is true, then the value is
243** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000244**
245** If the left side is "database.id" then pId1 is the database name
246** and pId2 is the id. If the left side is just "id" then pId1 is the
247** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000248*/
danielk197791cf71b2004-06-26 06:37:06 +0000249void sqlite3Pragma(
250 Parse *pParse,
251 Token *pId1, /* First part of [database.]id field */
252 Token *pId2, /* Second part of [database.]id field, or NULL */
253 Token *pValue, /* Token for <value>, or NULL */
254 int minusFlag /* True if a '-' sign preceded <value> */
255){
256 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
257 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
258 const char *zDb = 0; /* The database name */
259 Token *pId; /* Pointer to <id> token */
260 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000261 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000262 Db *pDb;
drh949f9cd2008-01-12 21:35:57 +0000263 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
drhc11d4f92003-04-06 21:08:24 +0000264 if( v==0 ) return;
drh9cbf3422008-01-17 16:22:13 +0000265 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000266
danielk197791cf71b2004-06-26 06:37:06 +0000267 /* Interpret the [database.] part of the pragma statement. iDb is the
268 ** index of the database this pragma is being applied to in db.aDb[]. */
269 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
270 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000271 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000272
danielk1977ddfb2f02006-02-17 12:25:14 +0000273 /* If the temp database has been explicitly named as part of the
274 ** pragma, make sure it is open.
275 */
276 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
277 return;
278 }
279
drh17435752007-08-16 04:30:38 +0000280 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000281 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000282 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000283 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000284 }else{
drh17435752007-08-16 04:30:38 +0000285 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000286 }
danielk197791cf71b2004-06-26 06:37:06 +0000287
danielk1977260d8a62008-08-20 16:34:24 +0000288 zDb = ((pId2 && pId2->n>0)?pDb->zName:0);
danielk197791cf71b2004-06-26 06:37:06 +0000289 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000290 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000291 }
292
drh13d70422004-11-13 15:59:14 +0000293#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000294 /*
drh90f5ecb2004-07-22 01:19:35 +0000295 ** PRAGMA [database.]default_cache_size
296 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000297 **
298 ** The first form reports the current persistent setting for the
299 ** page cache size. The value returned is the maximum number of
300 ** pages in the page cache. The second form sets both the current
301 ** page cache size value and the persistent page cache size value
302 ** stored in the database file.
303 **
304 ** The default cache size is stored in meta-value 2 of page 1 of the
305 ** database file. The cache size is actually the absolute value of
306 ** this memory location. The sign of meta-value 2 determines the
307 ** synchronous setting. A negative value means synchronous is off
308 ** and a positive value means synchronous is on.
309 */
danielk19774adee202004-05-08 08:23:19 +0000310 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000311 static const VdbeOpList getCacheSize[] = {
drh3c84ddf2008-01-09 02:15:38 +0000312 { OP_ReadCookie, 0, 1, 2}, /* 0 */
313 { OP_IfPos, 1, 6, 0},
314 { OP_Integer, 0, 2, 0},
315 { OP_Subtract, 1, 2, 1},
316 { OP_IfPos, 1, 6, 0},
317 { OP_Integer, 0, 1, 0}, /* 5 */
318 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000319 };
drh905793e2004-02-21 13:31:09 +0000320 int addr;
danielk19778a414492004-06-29 08:59:35 +0000321 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhfb982642007-08-30 01:19:59 +0000322 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000323 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000324 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000325 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000326 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000327 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000328 sqlite3VdbeChangeP1(v, addr, iDb);
drhc797d4d2007-05-08 01:08:49 +0000329 sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000330 }else{
drhc11d4f92003-04-06 21:08:24 +0000331 int size = atoi(zRight);
332 if( size<0 ) size = -size;
danielk197791cf71b2004-06-26 06:37:06 +0000333 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000334 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
335 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, 2);
336 addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
337 sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
drh3c84ddf2008-01-09 02:15:38 +0000338 sqlite3VdbeJumpHere(v, addr);
drh9cbf3422008-01-17 16:22:13 +0000339 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 2, 1);
danielk197714db2662006-01-09 16:12:04 +0000340 pDb->pSchema->cache_size = size;
341 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000342 }
343 }else
344
345 /*
drh90f5ecb2004-07-22 01:19:35 +0000346 ** PRAGMA [database.]page_size
347 ** PRAGMA [database.]page_size=N
348 **
349 ** The first form reports the current setting for the
350 ** database page size in bytes. The second form sets the
351 ** database page size value. The value can only be set if
352 ** the database has not yet been created.
353 */
354 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
355 Btree *pBt = pDb->pBt;
356 if( !zRight ){
357 int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000358 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000359 }else{
danielk1977992772c2007-08-30 10:07:38 +0000360 /* Malloc may fail when setting the page-size, as there is an internal
361 ** buffer that the pager module resizes using sqlite3_realloc().
362 */
danielk1977f653d782008-03-20 11:04:21 +0000363 db->nextPagesize = atoi(zRight);
364 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1) ){
danielk1977992772c2007-08-30 10:07:38 +0000365 db->mallocFailed = 1;
366 }
drh90f5ecb2004-07-22 01:19:35 +0000367 }
368 }else
danielk197741483462007-03-24 16:45:04 +0000369
370 /*
drhf8e632b2007-05-08 14:51:36 +0000371 ** PRAGMA [database.]max_page_count
372 ** PRAGMA [database.]max_page_count=N
373 **
374 ** The first form reports the current setting for the
375 ** maximum number of pages in the database file. The
376 ** second form attempts to change this setting. Both
377 ** forms return the current setting.
378 */
379 if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
380 Btree *pBt = pDb->pBt;
381 int newMax = 0;
382 if( zRight ){
383 newMax = atoi(zRight);
384 }
385 if( pBt ){
386 newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
387 }
388 returnSingleInt(pParse, "max_page_count", newMax);
389 }else
390
391 /*
danielk197759a93792008-05-15 17:48:20 +0000392 ** PRAGMA [database.]page_count
393 **
394 ** Return the number of pages in the specified database.
395 */
396 if( sqlite3StrICmp(zLeft,"page_count")==0 ){
397 Vdbe *v;
398 int iReg;
399 v = sqlite3GetVdbe(pParse);
400 if( !v || sqlite3ReadSchema(pParse) ) goto pragma_out;
401 sqlite3CodeVerifySchema(pParse, iDb);
402 iReg = ++pParse->nMem;
403 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
404 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
405 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000406 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", SQLITE_STATIC);
danielk197759a93792008-05-15 17:48:20 +0000407 }else
408
409 /*
danielk197741483462007-03-24 16:45:04 +0000410 ** PRAGMA [database.]locking_mode
411 ** PRAGMA [database.]locking_mode = (normal|exclusive)
412 */
413 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
414 const char *zRet = "normal";
415 int eMode = getLockingMode(zRight);
416
417 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
418 /* Simple "PRAGMA locking_mode;" statement. This is a query for
419 ** the current default locking mode (which may be different to
420 ** the locking-mode of the main database).
421 */
422 eMode = db->dfltLockMode;
423 }else{
424 Pager *pPager;
425 if( pId2->n==0 ){
426 /* This indicates that no database name was specified as part
427 ** of the PRAGMA command. In this case the locking-mode must be
428 ** set on all attached databases, as well as the main db file.
429 **
430 ** Also, the sqlite3.dfltLockMode variable is set so that
431 ** any subsequently attached databases also use the specified
432 ** locking mode.
433 */
434 int ii;
435 assert(pDb==&db->aDb[0]);
436 for(ii=2; ii<db->nDb; ii++){
437 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
438 sqlite3PagerLockingMode(pPager, eMode);
439 }
440 db->dfltLockMode = eMode;
441 }
442 pPager = sqlite3BtreePager(pDb->pBt);
443 eMode = sqlite3PagerLockingMode(pPager, eMode);
444 }
445
446 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
447 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
448 zRet = "exclusive";
449 }
450 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000451 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000452 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
453 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000454 }else
drh3b020132008-04-17 17:02:01 +0000455
456 /*
457 ** PRAGMA [database.]journal_mode
shanec782f692008-11-10 19:24:38 +0000458 ** PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory)
drh3b020132008-04-17 17:02:01 +0000459 */
460 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
461 int eMode;
danielk1977b3175382008-10-17 18:51:52 +0000462 static char * const azModeName[] = {
463 "delete", "persist", "off", "truncate", "memory"
464 };
drh3b020132008-04-17 17:02:01 +0000465
466 if( zRight==0 ){
467 eMode = PAGER_JOURNALMODE_QUERY;
468 }else{
469 int n = strlen(zRight);
drh04335882008-09-26 21:08:08 +0000470 eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1;
drh3b020132008-04-17 17:02:01 +0000471 while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
472 eMode--;
473 }
474 }
475 if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
danielk1977b53e4962008-06-04 06:45:59 +0000476 /* Simple "PRAGMA journal_mode;" statement. This is a query for
drh3b020132008-04-17 17:02:01 +0000477 ** the current default journal mode (which may be different to
478 ** the journal-mode of the main database).
479 */
480 eMode = db->dfltJournalMode;
481 }else{
482 Pager *pPager;
483 if( pId2->n==0 ){
484 /* This indicates that no database name was specified as part
485 ** of the PRAGMA command. In this case the journal-mode must be
486 ** set on all attached databases, as well as the main db file.
487 **
488 ** Also, the sqlite3.dfltJournalMode variable is set so that
489 ** any subsequently attached databases also use the specified
490 ** journal mode.
491 */
492 int ii;
493 assert(pDb==&db->aDb[0]);
drhfdc40e92008-04-17 20:59:37 +0000494 for(ii=1; ii<db->nDb; ii++){
495 if( db->aDb[ii].pBt ){
496 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
497 sqlite3PagerJournalMode(pPager, eMode);
498 }
drh3b020132008-04-17 17:02:01 +0000499 }
500 db->dfltJournalMode = eMode;
501 }
502 pPager = sqlite3BtreePager(pDb->pBt);
503 eMode = sqlite3PagerJournalMode(pPager, eMode);
504 }
505 assert( eMode==PAGER_JOURNALMODE_DELETE
drh04335882008-09-26 21:08:08 +0000506 || eMode==PAGER_JOURNALMODE_TRUNCATE
drh3b020132008-04-17 17:02:01 +0000507 || eMode==PAGER_JOURNALMODE_PERSIST
danielk1977b3175382008-10-17 18:51:52 +0000508 || eMode==PAGER_JOURNALMODE_OFF
509 || eMode==PAGER_JOURNALMODE_MEMORY );
drh3b020132008-04-17 17:02:01 +0000510 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000511 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000512 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0,
513 azModeName[eMode], P4_STATIC);
514 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
515 }else
danielk1977b53e4962008-06-04 06:45:59 +0000516
517 /*
518 ** PRAGMA [database.]journal_size_limit
519 ** PRAGMA [database.]journal_size_limit=N
520 **
521 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
522 */
523 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
524 Pager *pPager = sqlite3BtreePager(pDb->pBt);
525 i64 iLimit = -2;
526 if( zRight ){
527 int iLimit32 = atoi(zRight);
528 if( iLimit32<-1 ){
529 iLimit32 = -1;
530 }
531 iLimit = iLimit32;
532 }
533 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
534 returnSingleInt(pParse, "journal_size_limit", (int)iLimit);
535 }else
536
drh13d70422004-11-13 15:59:14 +0000537#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000538
539 /*
danielk1977951af802004-11-05 15:45:09 +0000540 ** PRAGMA [database.]auto_vacuum
541 ** PRAGMA [database.]auto_vacuum=N
542 **
543 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
544 */
545#ifndef SQLITE_OMIT_AUTOVACUUM
546 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
547 Btree *pBt = pDb->pBt;
danielk197727b1f952007-06-25 08:16:58 +0000548 if( sqlite3ReadSchema(pParse) ){
549 goto pragma_out;
550 }
danielk1977951af802004-11-05 15:45:09 +0000551 if( !zRight ){
552 int auto_vacuum =
553 pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
554 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
555 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000556 int eAuto = getAutoVacuum(zRight);
drhddac25c2007-12-05 01:38:23 +0000557 db->nextAutovac = eAuto;
danielk1977dddbcdc2007-04-26 14:42:34 +0000558 if( eAuto>=0 ){
danielk197727b1f952007-06-25 08:16:58 +0000559 /* Call SetAutoVacuum() to set initialize the internal auto and
560 ** incr-vacuum flags. This is required in case this connection
561 ** creates the database file. It is important that it is created
562 ** as an auto-vacuum capable db.
563 */
564 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
565 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
566 /* When setting the auto_vacuum mode to either "full" or
567 ** "incremental", write the value of meta[6] in the database
568 ** file. Before writing to meta[6], check that meta[3] indicates
569 ** that this really is an auto-vacuum capable database.
570 */
571 static const VdbeOpList setMeta6[] = {
572 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1db639c2008-01-17 02:36:28 +0000573 { OP_ReadCookie, 0, 1, 3}, /* 1 */
574 { OP_If, 1, 0, 0}, /* 2 */
danielk197727b1f952007-06-25 08:16:58 +0000575 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
drh1db639c2008-01-17 02:36:28 +0000576 { OP_Integer, 0, 1, 0}, /* 4 */
577 { OP_SetCookie, 0, 6, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000578 };
579 int iAddr;
580 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
581 sqlite3VdbeChangeP1(v, iAddr, iDb);
582 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
583 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
584 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
585 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000586 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000587 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000588 }
danielk1977951af802004-11-05 15:45:09 +0000589 }
590 }else
591#endif
592
drhca5557f2007-05-04 18:30:40 +0000593 /*
594 ** PRAGMA [database.]incremental_vacuum(N)
595 **
596 ** Do N steps of incremental vacuuming on a database.
597 */
598#ifndef SQLITE_OMIT_AUTOVACUUM
599 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
600 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000601 if( sqlite3ReadSchema(pParse) ){
602 goto pragma_out;
603 }
drhca5557f2007-05-04 18:30:40 +0000604 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
605 iLimit = 0x7fffffff;
606 }
607 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000608 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000609 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000610 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000611 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000612 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000613 sqlite3VdbeJumpHere(v, addr);
614 }else
615#endif
616
drh13d70422004-11-13 15:59:14 +0000617#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000618 /*
drh90f5ecb2004-07-22 01:19:35 +0000619 ** PRAGMA [database.]cache_size
620 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000621 **
622 ** The first form reports the current local setting for the
623 ** page cache size. The local setting can be different from
624 ** the persistent cache size value that is stored in the database
625 ** file itself. The value returned is the maximum number of
626 ** pages in the page cache. The second form sets the local
627 ** page cache size value. It does not change the persistent
628 ** cache size stored on the disk so the cache size will revert
629 ** to its default value when the database is closed and reopened.
630 ** N should be a positive integer.
631 */
danielk19774adee202004-05-08 08:23:19 +0000632 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000633 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000634 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000635 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000636 }else{
637 int size = atoi(zRight);
638 if( size<0 ) size = -size;
danielk197714db2662006-01-09 16:12:04 +0000639 pDb->pSchema->cache_size = size;
640 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000641 }
642 }else
643
644 /*
drh90f5ecb2004-07-22 01:19:35 +0000645 ** PRAGMA temp_store
646 ** PRAGMA temp_store = "default"|"memory"|"file"
647 **
648 ** Return or set the local value of the temp_store flag. Changing
649 ** the local value does not make changes to the disk file and the default
650 ** value will be restored the next time the database is opened.
651 **
652 ** Note that it is possible for the library compile-time options to
653 ** override this setting
654 */
655 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
656 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000657 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000658 }else{
659 changeTempStorage(pParse, zRight);
660 }
661 }else
662
663 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000664 ** PRAGMA temp_store_directory
665 ** PRAGMA temp_store_directory = ""|"directory_name"
666 **
667 ** Return or set the local value of the temp_store_directory flag. Changing
668 ** the value sets a specific directory to be used for temporary files.
669 ** Setting to a null string reverts to the default temporary directory search.
670 ** If temporary directory is changed, then invalidateTempStorage.
671 **
672 */
673 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
674 if( !zRight ){
675 if( sqlite3_temp_directory ){
676 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000677 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000678 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000679 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
680 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000681 }
682 }else{
drh78f82d12008-09-02 00:52:52 +0000683#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000684 if( zRight[0] ){
danielk1977fab11272008-09-16 14:38:02 +0000685 int rc;
danielk1977861f7452008-06-05 11:39:11 +0000686 int res;
danielk1977fab11272008-09-16 14:38:02 +0000687 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
688 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000689 sqlite3ErrorMsg(pParse, "not a writable directory");
690 goto pragma_out;
691 }
drh268283b2005-01-08 15:44:25 +0000692 }
danielk1977b06a0b62008-06-26 10:54:12 +0000693 if( SQLITE_TEMP_STORE==0
694 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
695 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000696 ){
697 invalidateTempStorage(pParse);
698 }
drh17435752007-08-16 04:30:38 +0000699 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000700 if( zRight[0] ){
drh633e6d52008-07-28 19:34:53 +0000701 sqlite3_temp_directory = sqlite3DbStrDup(0, zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000702 }else{
drh268283b2005-01-08 15:44:25 +0000703 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000704 }
drh78f82d12008-09-02 00:52:52 +0000705#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000706 }
707 }else
708
709 /*
aswiftaebf4132008-11-21 00:10:35 +0000710 ** PRAGMA [database.]lock_proxy_file
711 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
712 **
713 ** Return or set the value of the lock_proxy_file flag. Changing
714 ** the value sets a specific file to be used for database access locks.
715 **
716 */
717 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
718 if( !zRight ){
719 Pager *pPager = sqlite3BtreePager(pDb->pBt);
720 char *proxy_file_path = NULL;
721 sqlite3_file *pFile = sqlite3PagerFile(pPager);
722 sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
723 &proxy_file_path);
724
725 if( proxy_file_path ){
726 sqlite3VdbeSetNumCols(v, 1);
727 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
728 "lock_proxy_file", SQLITE_STATIC);
729 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
730 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
731 }
732 }else{
733 Pager *pPager = sqlite3BtreePager(pDb->pBt);
734 sqlite3_file *pFile = sqlite3PagerFile(pPager);
735 int res;
736 if( zRight[0] ){
737 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
738 zRight);
739 } else {
740 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
741 NULL);
742 }
743 if( res!=SQLITE_OK ){
744 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
745 goto pragma_out;
746 }
747 }
748 }else
749
750
751 /*
drh90f5ecb2004-07-22 01:19:35 +0000752 ** PRAGMA [database.]synchronous
753 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000754 **
755 ** Return or set the local value of the synchronous flag. Changing
756 ** the local value does not make changes to the disk file and the
757 ** default value will be restored the next time the database is
758 ** opened.
759 */
danielk19774adee202004-05-08 08:23:19 +0000760 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000761 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000762 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000763 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000764 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000765 if( !db->autoCommit ){
766 sqlite3ErrorMsg(pParse,
767 "Safety level may not be changed inside a transaction");
768 }else{
drh90f5ecb2004-07-22 01:19:35 +0000769 pDb->safety_level = getSafetyLevel(zRight)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000770 }
drhc11d4f92003-04-06 21:08:24 +0000771 }
772 }else
drh13d70422004-11-13 15:59:14 +0000773#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000774
drhbf216272005-02-26 18:10:44 +0000775#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000776 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000777 /* The flagPragma() subroutine also generates any necessary code
778 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000779 }else
drhbf216272005-02-26 18:10:44 +0000780#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000781
drh13d70422004-11-13 15:59:14 +0000782#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000783 /*
784 ** PRAGMA table_info(<table>)
785 **
786 ** Return a single row for each column of the named table. The columns of
787 ** the returned data set are:
788 **
789 ** cid: Column id (numbered from left to right, starting at 0)
790 ** name: Column name
791 ** type: Column declaration type.
792 ** notnull: True if 'NOT NULL' is part of column declaration
793 ** dflt_value: The default value for the column, if any.
794 */
drh5260f7e2004-06-26 19:35:29 +0000795 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000796 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000797 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000798 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000799 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000800 int i;
danielk1977034ca142007-06-26 10:38:54 +0000801 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +0000802 Column *pCol;
drh9f6696a2006-02-09 16:52:23 +0000803 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +0000804 pParse->nMem = 6;
danielk197710fb7492008-10-31 10:53:22 +0000805 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
806 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
807 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
808 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
809 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
810 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000811 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000812 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
drh417ec632006-08-14 14:23:41 +0000813 const Token *pDflt;
danielk1977034ca142007-06-26 10:38:54 +0000814 if( IsHiddenColumn(pCol) ){
815 nHidden++;
816 continue;
817 }
drh2d401ab2008-01-10 23:50:11 +0000818 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
819 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
820 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +0000821 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +0000822 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drh736c7d42006-11-30 13:06:37 +0000823 if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){
drh2d401ab2008-01-10 23:50:11 +0000824 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pDflt->z, pDflt->n);
drh6f835982006-09-25 13:48:30 +0000825 }else{
drh2d401ab2008-01-10 23:50:11 +0000826 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +0000827 }
drh2d401ab2008-01-10 23:50:11 +0000828 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
829 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +0000830 }
831 }
832 }else
833
drh5260f7e2004-06-26 19:35:29 +0000834 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000835 Index *pIdx;
836 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000837 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000838 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000839 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000840 int i;
841 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000842 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000843 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000844 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
845 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
846 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000847 for(i=0; i<pIdx->nColumn; i++){
848 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +0000849 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
850 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +0000851 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +0000852 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
853 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +0000854 }
855 }
856 }else
857
drh5260f7e2004-06-26 19:35:29 +0000858 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000859 Index *pIdx;
860 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000861 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000862 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000863 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000864 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000865 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000866 if( pIdx ){
867 int i = 0;
868 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000869 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000870 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
871 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
872 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000873 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +0000874 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
875 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
876 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
877 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +0000878 ++i;
879 pIdx = pIdx->pNext;
880 }
drhc11d4f92003-04-06 21:08:24 +0000881 }
882 }
883 }else
884
drh13d70422004-11-13 15:59:14 +0000885 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
886 int i;
887 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
888 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000889 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000890 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
891 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
892 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +0000893 for(i=0; i<db->nDb; i++){
894 if( db->aDb[i].pBt==0 ) continue;
895 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +0000896 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
897 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
898 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +0000899 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +0000900 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +0000901 }
902 }else
danielk197748af65a2005-02-09 03:20:37 +0000903
904 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
905 int i = 0;
906 HashElem *p;
907 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +0000908 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +0000909 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
910 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +0000911 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
912 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +0000913 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
914 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
915 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +0000916 }
917 }else
drh13d70422004-11-13 15:59:14 +0000918#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
919
drhb7f91642004-10-31 02:22:47 +0000920#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000921 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000922 FKey *pFK;
923 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000924 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000925 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +0000926 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000927 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +0000928 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +0000929 if( pFK ){
930 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +0000931 sqlite3VdbeSetNumCols(v, 8);
932 pParse->nMem = 8;
danielk197710fb7492008-10-31 10:53:22 +0000933 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
934 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
935 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
936 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
937 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
938 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
939 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
940 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000941 while(pFK){
942 int j;
943 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +0000944 char *zCol = pFK->aCol[j].zCol;
danielk197750af3e12008-10-10 17:47:21 +0000945 char *zOnUpdate = (char *)actionName(pFK->updateConf);
946 char *zOnDelete = (char *)actionName(pFK->deleteConf);
drh2d401ab2008-01-10 23:50:11 +0000947 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
948 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
949 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
950 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +0000951 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +0000952 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +0000953 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
954 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
955 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
956 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +0000957 }
958 ++i;
959 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +0000960 }
drh78100cc2003-08-23 22:40:53 +0000961 }
962 }
963 }else
drhb7f91642004-10-31 02:22:47 +0000964#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +0000965
drhc11d4f92003-04-06 21:08:24 +0000966#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +0000967 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +0000968 if( zRight ){
969 if( getBoolean(zRight) ){
970 sqlite3ParserTrace(stderr, "parser: ");
971 }else{
972 sqlite3ParserTrace(0, 0);
973 }
drhc11d4f92003-04-06 21:08:24 +0000974 }
975 }else
976#endif
977
drh55ef4d92005-08-14 01:20:37 +0000978 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
979 ** used will be case sensitive or not depending on the RHS.
980 */
981 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
982 if( zRight ){
983 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
984 }
985 }else
986
drh1dcdbc02007-01-27 02:24:54 +0000987#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
988# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
989#endif
990
drhb7f91642004-10-31 02:22:47 +0000991#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk197741c58b72007-12-29 13:39:19 +0000992 /* Pragma "quick_check" is an experimental reduced version of
993 ** integrity_check designed to detect most database corruption
994 ** without most of the overhead of a full integrity-check.
995 */
996 if( sqlite3StrICmp(zLeft, "integrity_check")==0
997 || sqlite3StrICmp(zLeft, "quick_check")==0
998 ){
drh1dcdbc02007-01-27 02:24:54 +0000999 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001000
drhed717fe2003-06-15 23:42:24 +00001001 /* Code that appears at the end of the integrity check. If no error
1002 ** messages have been generated, output OK. Otherwise output the
1003 ** error message
1004 */
drh57196282004-10-06 15:41:16 +00001005 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001006 { OP_AddImm, 1, 0, 0}, /* 0 */
1007 { OP_IfNeg, 1, 0, 0}, /* 1 */
1008 { OP_String8, 0, 3, 0}, /* 2 */
1009 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001010 };
drhed717fe2003-06-15 23:42:24 +00001011
danielk197741c58b72007-12-29 13:39:19 +00001012 int isQuick = (zLeft[0]=='q');
1013
drhed717fe2003-06-15 23:42:24 +00001014 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +00001015 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +00001016 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001017 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001018 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001019
1020 /* Set the maximum error count */
1021 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1022 if( zRight ){
1023 mxErr = atoi(zRight);
1024 if( mxErr<=0 ){
1025 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1026 }
1027 }
drh2d401ab2008-01-10 23:50:11 +00001028 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001029
1030 /* Do an integrity check on each database file */
1031 for(i=0; i<db->nDb; i++){
1032 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001033 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001034 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001035
danielk197753c0f742005-03-29 03:10:59 +00001036 if( OMIT_TEMPDB && i==1 ) continue;
1037
drh80242052004-06-09 00:48:12 +00001038 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001039 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001040 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001041 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001042
drhed717fe2003-06-15 23:42:24 +00001043 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001044 **
1045 ** Begin by filling registers 2, 3, ... with the root pages numbers
1046 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001047 */
danielk1977da184232006-01-05 11:34:32 +00001048 pTbls = &db->aDb[i].pSchema->tblHash;
1049 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001050 Table *pTab = sqliteHashData(x);
1051 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001052 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001053 cnt++;
1054 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001055 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001056 cnt++;
1057 }
1058 }
drh1dcdbc02007-01-27 02:24:54 +00001059 if( cnt==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001060
1061 /* Make sure sufficient number of registers have been allocated */
danielk1977a7a8e142008-02-13 18:25:27 +00001062 if( pParse->nMem < cnt+4 ){
1063 pParse->nMem = cnt+4;
drh98757152008-01-09 23:04:12 +00001064 }
drh2d401ab2008-01-10 23:50:11 +00001065
1066 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001067 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
1068 sqlite3VdbeChangeP5(v, i);
1069 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1070 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001071 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001072 P4_DYNAMIC);
drhb21e7c72008-06-22 12:37:57 +00001073 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001074 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001075 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001076 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001077
1078 /* Make sure all the indices are constructed correctly.
1079 */
danielk197741c58b72007-12-29 13:39:19 +00001080 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001081 Table *pTab = sqliteHashData(x);
1082 Index *pIdx;
1083 int loopTop;
1084
1085 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001086 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001087 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001088 sqlite3VdbeJumpHere(v, addr);
drh290c1942004-08-21 17:54:45 +00001089 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh2d401ab2008-01-10 23:50:11 +00001090 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
drh66a51672008-01-03 00:01:23 +00001091 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
drh2d401ab2008-01-10 23:50:11 +00001092 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
drhed717fe2003-06-15 23:42:24 +00001093 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +00001094 int jmp2;
drh57196282004-10-06 15:41:16 +00001095 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001096 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001097 { OP_String8, 0, 3, 0}, /* 1 */
1098 { OP_Rowid, 1, 4, 0},
1099 { OP_String8, 0, 5, 0}, /* 3 */
1100 { OP_String8, 0, 6, 0}, /* 4 */
1101 { OP_Concat, 4, 3, 3},
1102 { OP_Concat, 5, 3, 3},
1103 { OP_Concat, 6, 3, 3},
1104 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001105 { OP_IfPos, 1, 0, 0}, /* 9 */
1106 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001107 };
drhe14006d2008-03-25 17:23:32 +00001108 sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 1);
drh2d401ab2008-01-10 23:50:11 +00001109 jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3);
danielk19774adee202004-05-08 08:23:19 +00001110 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001111 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1112 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh66a51672008-01-03 00:01:23 +00001113 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
drhbb8a2792008-03-19 00:21:30 +00001114 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001115 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +00001116 }
drh66a51672008-01-03 00:01:23 +00001117 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +00001118 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +00001119 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +00001120 static const VdbeOpList cntIdx[] = {
drh4c583122008-01-04 22:01:03 +00001121 { OP_Integer, 0, 3, 0},
drhd654be82005-09-20 17:42:23 +00001122 { OP_Rewind, 0, 0, 0}, /* 1 */
drh8558cde2008-01-05 05:20:10 +00001123 { OP_AddImm, 3, 1, 0},
drhd654be82005-09-20 17:42:23 +00001124 { OP_Next, 0, 0, 0}, /* 3 */
drh2d401ab2008-01-10 23:50:11 +00001125 { OP_Eq, 2, 0, 3}, /* 4 */
drh8558cde2008-01-05 05:20:10 +00001126 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001127 { OP_String8, 0, 2, 0}, /* 6 */
1128 { OP_String8, 0, 3, 0}, /* 7 */
1129 { OP_Concat, 3, 2, 2},
1130 { OP_ResultRow, 2, 1, 0},
drhed717fe2003-06-15 23:42:24 +00001131 };
1132 if( pIdx->tnum==0 ) continue;
drh3c84ddf2008-01-09 02:15:38 +00001133 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
drh66a51672008-01-03 00:01:23 +00001134 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001135 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001136 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +00001137 sqlite3VdbeChangeP1(v, addr+1, j+2);
1138 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1139 sqlite3VdbeChangeP1(v, addr+3, j+2);
1140 sqlite3VdbeChangeP2(v, addr+3, addr+2);
drh2d401ab2008-01-10 23:50:11 +00001141 sqlite3VdbeJumpHere(v, addr+4);
1142 sqlite3VdbeChangeP4(v, addr+6,
drh24003452008-01-03 01:28:59 +00001143 "wrong # of entries in index ", P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001144 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
drhed717fe2003-06-15 23:42:24 +00001145 }
1146 }
1147 }
danielk19774adee202004-05-08 08:23:19 +00001148 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001149 sqlite3VdbeChangeP2(v, addr, -mxErr);
1150 sqlite3VdbeJumpHere(v, addr+1);
1151 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001152 }else
drhb7f91642004-10-31 02:22:47 +00001153#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1154
drh13d70422004-11-13 15:59:14 +00001155#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001156 /*
1157 ** PRAGMA encoding
1158 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1159 **
drh85b623f2007-12-13 21:54:09 +00001160 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001161 ** database. If the database is not initialized, it is initialized now.
1162 **
1163 ** The second form of this pragma is a no-op if the main database file
1164 ** has not already been initialized. In this case it sets the default
1165 ** encoding that will be used for the main database file if a new file
1166 ** is created. If an existing main database file is opened, then the
1167 ** default text encoding for the existing database is used.
1168 **
1169 ** In all cases new databases created using the ATTACH command are
1170 ** created to use the same default text encoding as the main database. If
1171 ** the main database has not been initialized and/or created when ATTACH
1172 ** is executed, this is done before the ATTACH operation.
1173 **
1174 ** In the second form this pragma sets the text encoding to be used in
1175 ** new database files created using this database handle. It is only
1176 ** useful if invoked immediately after the main database i
1177 */
1178 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001179 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001180 char *zName;
1181 u8 enc;
1182 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001183 { "UTF-8", SQLITE_UTF8 },
1184 { "UTF8", SQLITE_UTF8 },
1185 { "UTF-16le", SQLITE_UTF16LE },
1186 { "UTF16le", SQLITE_UTF16LE },
1187 { "UTF-16be", SQLITE_UTF16BE },
1188 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001189 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1190 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001191 { 0, 0 }
1192 };
drh0f7eb612006-08-08 13:51:43 +00001193 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001194 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001195 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001196 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001197 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001198 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
danielk19778e227872004-06-07 07:52:17 +00001199 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
danielk197714db2662006-01-09 16:12:04 +00001200 if( pEnc->enc==ENC(pParse->db) ){
drh66a51672008-01-03 00:01:23 +00001201 sqlite3VdbeChangeP4(v, -1, pEnc->zName, P4_STATIC);
danielk19778e227872004-06-07 07:52:17 +00001202 break;
1203 }
1204 }
drh2d401ab2008-01-10 23:50:11 +00001205 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001206 }else{ /* "PRAGMA encoding = XXX" */
1207 /* Only change the value of sqlite.enc if the database handle is not
1208 ** initialized. If the main database exists, the new sqlite.enc value
1209 ** will be overwritten when the schema is next loaded. If it does not
1210 ** already exists, it will be created to use the new encoding value.
1211 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001212 if(
1213 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1214 DbHasProperty(db, 0, DB_Empty)
1215 ){
danielk19778e227872004-06-07 07:52:17 +00001216 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1217 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001218 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001219 break;
1220 }
1221 }
1222 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001223 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001224 }
1225 }
1226 }
1227 }else
drh13d70422004-11-13 15:59:14 +00001228#endif /* SQLITE_OMIT_UTF16 */
1229
1230#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001231 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001232 ** PRAGMA [database.]schema_version
1233 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001234 **
danielk1977b92b70b2004-11-12 16:11:59 +00001235 ** PRAGMA [database.]user_version
1236 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001237 **
danielk1977b92b70b2004-11-12 16:11:59 +00001238 ** The pragma's schema_version and user_version are used to set or get
1239 ** the value of the schema-version and user-version, respectively. Both
1240 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001241 ** stored in the database header.
1242 **
1243 ** The schema-cookie is usually only manipulated internally by SQLite. It
1244 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001245 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001246 ** SQLite each time a query is executed to ensure that the internal cache
1247 ** of the schema used when compiling the SQL query matches the schema of
1248 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001249 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1250 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001251 ** crashes or database corruption. Use with caution!
1252 **
danielk1977b92b70b2004-11-12 16:11:59 +00001253 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001254 ** applications for any purpose.
1255 */
danielk1977180b56a2007-06-24 08:00:42 +00001256 if( sqlite3StrICmp(zLeft, "schema_version")==0
1257 || sqlite3StrICmp(zLeft, "user_version")==0
1258 || sqlite3StrICmp(zLeft, "freelist_count")==0
1259 ){
danielk1977dae24952004-11-11 05:10:43 +00001260 int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001261 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001262 switch( zLeft[0] ){
1263 case 's': case 'S':
1264 iCookie = 0;
1265 break;
1266 case 'f': case 'F':
1267 iCookie = 1;
1268 iDb = (-1*(iDb+1));
1269 assert(iDb<=0);
1270 break;
1271 default:
1272 iCookie = 5;
1273 break;
danielk1977dae24952004-11-11 05:10:43 +00001274 }
1275
danielk1977180b56a2007-06-24 08:00:42 +00001276 if( zRight && iDb>=0 ){
danielk1977dae24952004-11-11 05:10:43 +00001277 /* Write the specified cookie value */
1278 static const VdbeOpList setCookie[] = {
1279 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001280 { OP_Integer, 0, 1, 0}, /* 1 */
1281 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001282 };
1283 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1284 sqlite3VdbeChangeP1(v, addr, iDb);
1285 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
1286 sqlite3VdbeChangeP1(v, addr+2, iDb);
1287 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1288 }else{
1289 /* Read the specified cookie value */
1290 static const VdbeOpList readCookie[] = {
drh2d401ab2008-01-10 23:50:11 +00001291 { OP_ReadCookie, 0, 1, 0}, /* 0 */
1292 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001293 };
1294 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1295 sqlite3VdbeChangeP1(v, addr, iDb);
drh4c583122008-01-04 22:01:03 +00001296 sqlite3VdbeChangeP3(v, addr, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001297 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001298 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001299 }
drh6e345992007-03-30 11:12:08 +00001300 }else
drh13d70422004-11-13 15:59:14 +00001301#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001302
dougcurrie81c95ef2004-06-18 23:21:47 +00001303#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001304 /*
1305 ** Report the current state of file logs for all databases
1306 */
1307 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001308 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001309 "unlocked", "shared", "reserved", "pending", "exclusive"
1310 };
1311 int i;
1312 Vdbe *v = sqlite3GetVdbe(pParse);
1313 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001314 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001315 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1316 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001317 for(i=0; i<db->nDb; i++){
1318 Btree *pBt;
1319 Pager *pPager;
drh9e33c2c2007-08-31 18:34:59 +00001320 const char *zState = "unknown";
1321 int j;
drh89ac8c12004-06-09 14:17:20 +00001322 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001323 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001324 pBt = db->aDb[i].pBt;
1325 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001326 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001327 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001328 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1329 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001330 }
drh2d401ab2008-01-10 23:50:11 +00001331 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1332 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001333 }
danielk1977a4de4532008-09-02 15:44:08 +00001334
drh89ac8c12004-06-09 14:17:20 +00001335 }else
1336#endif
1337
drh89dec812005-04-28 19:03:37 +00001338#ifdef SQLITE_SSE
1339 /*
1340 ** Check to see if the sqlite_statements table exists. Create it
1341 ** if it does not.
1342 */
1343 if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
danielk19773a3f38e2005-05-22 06:49:56 +00001344 extern int sqlite3CreateStatementsTable(Parse*);
1345 sqlite3CreateStatementsTable(pParse);
drh89dec812005-04-28 19:03:37 +00001346 }else
1347#endif
1348
drh3c4f2a42005-12-08 18:12:56 +00001349#if SQLITE_HAS_CODEC
1350 if( sqlite3StrICmp(zLeft, "key")==0 ){
1351 sqlite3_key(db, zRight, strlen(zRight));
1352 }else
1353#endif
drh21e2cab2006-09-25 18:01:57 +00001354#if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
1355 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
1356#if SQLITE_HAS_CODEC
1357 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
1358 extern void sqlite3_activate_see(const char*);
1359 sqlite3_activate_see(&zRight[4]);
1360 }
1361#endif
1362#ifdef SQLITE_ENABLE_CEROD
1363 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
1364 extern void sqlite3_activate_cerod(const char*);
1365 sqlite3_activate_cerod(&zRight[6]);
1366 }
1367#endif
1368 }
1369#endif
drh3c4f2a42005-12-08 18:12:56 +00001370
drhc11d4f92003-04-06 21:08:24 +00001371 {}
danielk1977a21c6b62005-01-24 10:25:59 +00001372
1373 if( v ){
1374 /* Code an OP_Expire at the end of each PRAGMA program to cause
1375 ** the VDBE implementing the pragma to expire. Most (all?) pragmas
1376 ** are only valid for a single execution.
1377 */
drh66a51672008-01-03 00:01:23 +00001378 sqlite3VdbeAddOp2(v, OP_Expire, 1, 0);
drhac530b12006-02-11 01:25:50 +00001379
1380 /*
1381 ** Reset the safety level, in case the fullfsync flag or synchronous
1382 ** setting changed.
1383 */
danielk1977ddfb2f02006-02-17 12:25:14 +00001384#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhac530b12006-02-11 01:25:50 +00001385 if( db->autoCommit ){
1386 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
1387 (db->flags&SQLITE_FullFSync)!=0);
1388 }
danielk1977ddfb2f02006-02-17 12:25:14 +00001389#endif
danielk1977a21c6b62005-01-24 10:25:59 +00001390 }
danielk1977e0048402004-06-15 16:51:01 +00001391pragma_out:
drh633e6d52008-07-28 19:34:53 +00001392 sqlite3DbFree(db, zLeft);
1393 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001394}
drh13d70422004-11-13 15:59:14 +00001395
drh0ccebe72005-06-07 22:22:50 +00001396#endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */