blob: 78f28cadbc55fa2d9661fd447bfbb8b73bbd249f [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**
drh21e2cab2006-09-25 18:01:57 +000014** $Id: pragma.c,v 1.124 2006/09/25 18:01:57 drh Exp $
drhc11d4f92003-04-06 21:08:24 +000015*/
16#include "sqliteInt.h"
drh268283b2005-01-08 15:44:25 +000017#include "os.h"
drh13bff812003-04-15 01:19:47 +000018#include <ctype.h>
drhc11d4f92003-04-06 21:08:24 +000019
drh13d70422004-11-13 15:59:14 +000020/* Ignore this whole file if pragmas are disabled
21*/
drh0ccebe72005-06-07 22:22:50 +000022#if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
drh13d70422004-11-13 15:59:14 +000023
dougcurrie81c95ef2004-06-18 23:21:47 +000024#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +000025# include "pager.h"
26# include "btree.h"
27#endif
28
drhc11d4f92003-04-06 21:08:24 +000029/*
drhc11d4f92003-04-06 21:08:24 +000030** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +000031** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
32** unrecognized string argument.
drhc11d4f92003-04-06 21:08:24 +000033**
34** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000035** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000036** to support legacy SQL code. The safety level used to be boolean
37** and older scripts may have used numbers 0 for OFF and 1 for ON.
38*/
drh2646da72005-12-09 20:02:05 +000039static int getSafetyLevel(const char *z){
drh722e95a2004-10-25 20:33:44 +000040 /* 123456789 123456789 */
41 static const char zText[] = "onoffalseyestruefull";
42 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
43 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
44 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
45 int i, n;
46 if( isdigit(*z) ){
drhc11d4f92003-04-06 21:08:24 +000047 return atoi(z);
48 }
drh722e95a2004-10-25 20:33:44 +000049 n = strlen(z);
50 for(i=0; i<sizeof(iLength); i++){
51 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
52 return iValue[i];
53 }
drhc11d4f92003-04-06 21:08:24 +000054 }
55 return 1;
56}
57
58/*
drh722e95a2004-10-25 20:33:44 +000059** Interpret the given string as a boolean value.
60*/
drh2646da72005-12-09 20:02:05 +000061static int getBoolean(const char *z){
drh722e95a2004-10-25 20:33:44 +000062 return getSafetyLevel(z)&1;
63}
64
danielk1977b84f96f2005-01-20 11:32:23 +000065#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +000066/*
drh90f5ecb2004-07-22 01:19:35 +000067** Interpret the given string as a temp db location. Return 1 for file
68** backed temporary databases, 2 for the Red-Black tree in memory database
69** and 0 to use the compile-time default.
70*/
71static int getTempStore(const char *z){
72 if( z[0]>='0' && z[0]<='2' ){
73 return z[0] - '0';
74 }else if( sqlite3StrICmp(z, "file")==0 ){
75 return 1;
76 }else if( sqlite3StrICmp(z, "memory")==0 ){
77 return 2;
78 }else{
79 return 0;
80 }
81}
drhbf216272005-02-26 18:10:44 +000082#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +000083
drhbf216272005-02-26 18:10:44 +000084#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +000085/*
tpoindex9a09a3c2004-12-20 19:01:32 +000086** Invalidate temp storage, either when the temp storage is changed
87** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +000088*/
tpoindex9a09a3c2004-12-20 19:01:32 +000089static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +000090 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +000091 if( db->aDb[1].pBt!=0 ){
92 if( db->flags & SQLITE_InTrans ){
93 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
94 "from within a transaction");
95 return SQLITE_ERROR;
96 }
97 sqlite3BtreeClose(db->aDb[1].pBt);
98 db->aDb[1].pBt = 0;
99 sqlite3ResetInternalSchema(db, 0);
100 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000101 return SQLITE_OK;
102}
drhbf216272005-02-26 18:10:44 +0000103#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000104
drhbf216272005-02-26 18:10:44 +0000105#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000106/*
107** If the TEMP database is open, close it and mark the database schema
108** as needing reloading. This must be done when using the TEMP_STORE
109** or DEFAULT_TEMP_STORE pragmas.
110*/
111static int changeTempStorage(Parse *pParse, const char *zStorageType){
112 int ts = getTempStore(zStorageType);
113 sqlite3 *db = pParse->db;
114 if( db->temp_store==ts ) return SQLITE_OK;
115 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
116 return SQLITE_ERROR;
117 }
drh90f5ecb2004-07-22 01:19:35 +0000118 db->temp_store = ts;
119 return SQLITE_OK;
120}
drhbf216272005-02-26 18:10:44 +0000121#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000122
123/*
124** Generate code to return a single integer value.
125*/
drh5bb7ffe2004-09-02 15:14:00 +0000126static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
127 Vdbe *v = sqlite3GetVdbe(pParse);
drh90f5ecb2004-07-22 01:19:35 +0000128 sqlite3VdbeAddOp(v, OP_Integer, value, 0);
drh5bb7ffe2004-09-02 15:14:00 +0000129 if( pParse->explain==0 ){
130 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000131 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P3_STATIC);
drh5bb7ffe2004-09-02 15:14:00 +0000132 }
drh90f5ecb2004-07-22 01:19:35 +0000133 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
134}
135
drhbf216272005-02-26 18:10:44 +0000136#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000137/*
drh87223182004-02-21 14:00:29 +0000138** Check to see if zRight and zLeft refer to a pragma that queries
139** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
140** Also, implement the pragma.
141*/
142static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000143 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000144 const char *zName; /* Name of the pragma */
145 int mask; /* Mask for the db->flags value */
146 } aPragma[] = {
147 { "vdbe_trace", SQLITE_VdbeTrace },
drh35d4c2f2004-06-10 01:30:59 +0000148 { "sql_trace", SQLITE_SqlTrace },
149 { "vdbe_listing", SQLITE_VdbeListing },
drh87223182004-02-21 14:00:29 +0000150 { "full_column_names", SQLITE_FullColNames },
151 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000152 { "count_changes", SQLITE_CountRows },
153 { "empty_result_callbacks", SQLITE_NullCallback },
drhe321c292006-01-12 01:56:43 +0000154 { "legacy_file_format", SQLITE_LegacyFileFmt },
drhac530b12006-02-11 01:25:50 +0000155 { "fullfsync", SQLITE_FullFSync },
drh0cd2d4c2005-11-03 02:15:02 +0000156#ifndef SQLITE_OMIT_CHECK
157 { "ignore_check_constraints", SQLITE_IgnoreChecks },
158#endif
drh722e95a2004-10-25 20:33:44 +0000159 /* The following is VERY experimental */
drhf4040832004-10-22 20:29:21 +0000160 { "writable_schema", SQLITE_WriteSchema },
drh7bec5052005-02-06 02:45:41 +0000161 { "omit_readlock", SQLITE_NoReadlock },
danielk1977da184232006-01-05 11:34:32 +0000162
163 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
164 ** flag if there are any active statements. */
165 { "read_uncommitted", SQLITE_ReadUncommitted },
drh87223182004-02-21 14:00:29 +0000166 };
167 int i;
drh722e95a2004-10-25 20:33:44 +0000168 const struct sPragmaType *p;
169 for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
170 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000171 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000172 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000173 v = sqlite3GetVdbe(pParse);
174 if( v ){
175 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000176 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000177 }else{
danielk1977a21c6b62005-01-24 10:25:59 +0000178 if( getBoolean(zRight) ){
179 db->flags |= p->mask;
180 }else{
181 db->flags &= ~p->mask;
182 }
drhd89bd002005-01-22 03:03:54 +0000183 }
drh87223182004-02-21 14:00:29 +0000184 }
185 return 1;
186 }
187 }
188 return 0;
189}
drhbf216272005-02-26 18:10:44 +0000190#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000191
192/*
drhc11d4f92003-04-06 21:08:24 +0000193** Process a pragma statement.
194**
195** Pragmas are of this form:
196**
danielk197791cf71b2004-06-26 06:37:06 +0000197** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000198**
199** The identifier might also be a string. The value is a string, and
200** identifier, or a number. If minusFlag is true, then the value is
201** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000202**
203** If the left side is "database.id" then pId1 is the database name
204** and pId2 is the id. If the left side is just "id" then pId1 is the
205** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000206*/
danielk197791cf71b2004-06-26 06:37:06 +0000207void sqlite3Pragma(
208 Parse *pParse,
209 Token *pId1, /* First part of [database.]id field */
210 Token *pId2, /* Second part of [database.]id field, or NULL */
211 Token *pValue, /* Token for <value>, or NULL */
212 int minusFlag /* True if a '-' sign preceded <value> */
213){
214 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
215 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
216 const char *zDb = 0; /* The database name */
217 Token *pId; /* Pointer to <id> token */
218 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000219 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000220 Db *pDb;
danielk19774adee202004-05-08 08:23:19 +0000221 Vdbe *v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000222 if( v==0 ) return;
223
danielk197791cf71b2004-06-26 06:37:06 +0000224 /* Interpret the [database.] part of the pragma statement. iDb is the
225 ** index of the database this pragma is being applied to in db.aDb[]. */
226 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
227 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000228 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000229
danielk1977ddfb2f02006-02-17 12:25:14 +0000230 /* If the temp database has been explicitly named as part of the
231 ** pragma, make sure it is open.
232 */
233 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
234 return;
235 }
236
danielk197791cf71b2004-06-26 06:37:06 +0000237 zLeft = sqlite3NameFromToken(pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000238 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000239 if( minusFlag ){
drhae29ffb2004-09-25 14:39:18 +0000240 zRight = sqlite3MPrintf("-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000241 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000242 zRight = sqlite3NameFromToken(pValue);
drhc11d4f92003-04-06 21:08:24 +0000243 }
danielk197791cf71b2004-06-26 06:37:06 +0000244
drh90f5ecb2004-07-22 01:19:35 +0000245 zDb = ((iDb>0)?pDb->zName:0);
danielk197791cf71b2004-06-26 06:37:06 +0000246 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000247 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000248 }
249
drh13d70422004-11-13 15:59:14 +0000250#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000251 /*
drh90f5ecb2004-07-22 01:19:35 +0000252 ** PRAGMA [database.]default_cache_size
253 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000254 **
255 ** The first form reports the current persistent setting for the
256 ** page cache size. The value returned is the maximum number of
257 ** pages in the page cache. The second form sets both the current
258 ** page cache size value and the persistent page cache size value
259 ** stored in the database file.
260 **
261 ** The default cache size is stored in meta-value 2 of page 1 of the
262 ** database file. The cache size is actually the absolute value of
263 ** this memory location. The sign of meta-value 2 determines the
264 ** synchronous setting. A negative value means synchronous is off
265 ** and a positive value means synchronous is on.
266 */
danielk19774adee202004-05-08 08:23:19 +0000267 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000268 static const VdbeOpList getCacheSize[] = {
danielk197791cf71b2004-06-26 06:37:06 +0000269 { OP_ReadCookie, 0, 2, 0}, /* 0 */
drhc11d4f92003-04-06 21:08:24 +0000270 { OP_AbsValue, 0, 0, 0},
271 { OP_Dup, 0, 0, 0},
272 { OP_Integer, 0, 0, 0},
273 { OP_Ne, 0, 6, 0},
drh905793e2004-02-21 13:31:09 +0000274 { OP_Integer, 0, 0, 0}, /* 5 */
drhc11d4f92003-04-06 21:08:24 +0000275 { OP_Callback, 1, 0, 0},
276 };
drh905793e2004-02-21 13:31:09 +0000277 int addr;
danielk19778a414492004-06-29 08:59:35 +0000278 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000279 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000280 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000281 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000282 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000283 sqlite3VdbeChangeP1(v, addr, iDb);
danielk19774adee202004-05-08 08:23:19 +0000284 sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES);
drhc11d4f92003-04-06 21:08:24 +0000285 }else{
drhc11d4f92003-04-06 21:08:24 +0000286 int size = atoi(zRight);
287 if( size<0 ) size = -size;
danielk197791cf71b2004-06-26 06:37:06 +0000288 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk19774adee202004-05-08 08:23:19 +0000289 sqlite3VdbeAddOp(v, OP_Integer, size, 0);
danielk197791cf71b2004-06-26 06:37:06 +0000290 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2);
danielk19774adee202004-05-08 08:23:19 +0000291 addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
292 sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3);
293 sqlite3VdbeAddOp(v, OP_Negative, 0, 0);
danielk197791cf71b2004-06-26 06:37:06 +0000294 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2);
danielk197714db2662006-01-09 16:12:04 +0000295 pDb->pSchema->cache_size = size;
296 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000297 }
298 }else
299
300 /*
drh90f5ecb2004-07-22 01:19:35 +0000301 ** PRAGMA [database.]page_size
302 ** PRAGMA [database.]page_size=N
303 **
304 ** The first form reports the current setting for the
305 ** database page size in bytes. The second form sets the
306 ** database page size value. The value can only be set if
307 ** the database has not yet been created.
308 */
309 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
310 Btree *pBt = pDb->pBt;
311 if( !zRight ){
312 int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000313 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000314 }else{
danielk197728129562005-01-11 10:25:06 +0000315 sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1);
drh90f5ecb2004-07-22 01:19:35 +0000316 }
317 }else
drh13d70422004-11-13 15:59:14 +0000318#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000319
320 /*
danielk1977951af802004-11-05 15:45:09 +0000321 ** PRAGMA [database.]auto_vacuum
322 ** PRAGMA [database.]auto_vacuum=N
323 **
324 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
325 */
326#ifndef SQLITE_OMIT_AUTOVACUUM
327 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
328 Btree *pBt = pDb->pBt;
329 if( !zRight ){
330 int auto_vacuum =
331 pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
332 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
333 }else{
334 sqlite3BtreeSetAutoVacuum(pBt, getBoolean(zRight));
335 }
336 }else
337#endif
338
drh13d70422004-11-13 15:59:14 +0000339#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000340 /*
drh90f5ecb2004-07-22 01:19:35 +0000341 ** PRAGMA [database.]cache_size
342 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000343 **
344 ** The first form reports the current local setting for the
345 ** page cache size. The local setting can be different from
346 ** the persistent cache size value that is stored in the database
347 ** file itself. The value returned is the maximum number of
348 ** pages in the page cache. The second form sets the local
349 ** page cache size value. It does not change the persistent
350 ** cache size stored on the disk so the cache size will revert
351 ** to its default value when the database is closed and reopened.
352 ** N should be a positive integer.
353 */
danielk19774adee202004-05-08 08:23:19 +0000354 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000355 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000356 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000357 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000358 }else{
359 int size = atoi(zRight);
360 if( size<0 ) size = -size;
danielk197714db2662006-01-09 16:12:04 +0000361 pDb->pSchema->cache_size = size;
362 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000363 }
364 }else
365
366 /*
drh90f5ecb2004-07-22 01:19:35 +0000367 ** PRAGMA temp_store
368 ** PRAGMA temp_store = "default"|"memory"|"file"
369 **
370 ** Return or set the local value of the temp_store flag. Changing
371 ** the local value does not make changes to the disk file and the default
372 ** value will be restored the next time the database is opened.
373 **
374 ** Note that it is possible for the library compile-time options to
375 ** override this setting
376 */
377 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
378 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000379 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000380 }else{
381 changeTempStorage(pParse, zRight);
382 }
383 }else
384
385 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000386 ** PRAGMA temp_store_directory
387 ** PRAGMA temp_store_directory = ""|"directory_name"
388 **
389 ** Return or set the local value of the temp_store_directory flag. Changing
390 ** the value sets a specific directory to be used for temporary files.
391 ** Setting to a null string reverts to the default temporary directory search.
392 ** If temporary directory is changed, then invalidateTempStorage.
393 **
394 */
395 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
396 if( !zRight ){
397 if( sqlite3_temp_directory ){
398 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000399 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
400 "temp_store_directory", P3_STATIC);
tpoindex9a09a3c2004-12-20 19:01:32 +0000401 sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0);
402 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
403 }
404 }else{
drh66560ad2006-01-06 14:32:19 +0000405 if( zRight[0] && !sqlite3OsIsDirWritable(zRight) ){
drh268283b2005-01-08 15:44:25 +0000406 sqlite3ErrorMsg(pParse, "not a writable directory");
407 goto pragma_out;
408 }
409 if( TEMP_STORE==0
410 || (TEMP_STORE==1 && db->temp_store<=1)
411 || (TEMP_STORE==2 && db->temp_store==1)
412 ){
413 invalidateTempStorage(pParse);
414 }
415 sqliteFree(sqlite3_temp_directory);
416 if( zRight[0] ){
417 sqlite3_temp_directory = zRight;
418 zRight = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000419 }else{
drh268283b2005-01-08 15:44:25 +0000420 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000421 }
422 }
423 }else
424
425 /*
drh90f5ecb2004-07-22 01:19:35 +0000426 ** PRAGMA [database.]synchronous
427 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000428 **
429 ** Return or set the local value of the synchronous flag. Changing
430 ** the local value does not make changes to the disk file and the
431 ** default value will be restored the next time the database is
432 ** opened.
433 */
danielk19774adee202004-05-08 08:23:19 +0000434 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000435 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000436 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000437 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000438 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000439 if( !db->autoCommit ){
440 sqlite3ErrorMsg(pParse,
441 "Safety level may not be changed inside a transaction");
442 }else{
drh90f5ecb2004-07-22 01:19:35 +0000443 pDb->safety_level = getSafetyLevel(zRight)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000444 }
drhc11d4f92003-04-06 21:08:24 +0000445 }
446 }else
drh13d70422004-11-13 15:59:14 +0000447#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000448
drhbf216272005-02-26 18:10:44 +0000449#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000450 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000451 /* The flagPragma() subroutine also generates any necessary code
452 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000453 }else
drhbf216272005-02-26 18:10:44 +0000454#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000455
drh13d70422004-11-13 15:59:14 +0000456#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000457 /*
458 ** PRAGMA table_info(<table>)
459 **
460 ** Return a single row for each column of the named table. The columns of
461 ** the returned data set are:
462 **
463 ** cid: Column id (numbered from left to right, starting at 0)
464 ** name: Column name
465 ** type: Column declaration type.
466 ** notnull: True if 'NOT NULL' is part of column declaration
467 ** dflt_value: The default value for the column, if any.
468 */
drh5260f7e2004-06-26 19:35:29 +0000469 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000470 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000471 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000472 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000473 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000474 int i;
drhf7eece62006-02-06 21:34:27 +0000475 Column *pCol;
drh9f6696a2006-02-09 16:52:23 +0000476 sqlite3VdbeSetNumCols(v, 6);
danielk1977955de522006-02-10 02:27:42 +0000477 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P3_STATIC);
478 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
479 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P3_STATIC);
480 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P3_STATIC);
481 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P3_STATIC);
482 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000483 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000484 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
drh417ec632006-08-14 14:23:41 +0000485 const Token *pDflt;
486 static const Token noDflt = { (unsigned char*)"", 0, 0 };
danielk19774adee202004-05-08 08:23:19 +0000487 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
drhf7eece62006-02-06 21:34:27 +0000488 sqlite3VdbeOp3(v, OP_String8, 0, 0, pCol->zName, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000489 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drhbfa8b102006-03-03 21:20:16 +0000490 pCol->zType ? pCol->zType : "", 0);
drhf7eece62006-02-06 21:34:27 +0000491 sqlite3VdbeAddOp(v, OP_Integer, pCol->notNull, 0);
drh417ec632006-08-14 14:23:41 +0000492 pDflt = pCol->pDflt ? &pCol->pDflt->span : &noDflt;
drh6f835982006-09-25 13:48:30 +0000493 if( pDflt->z ){
494 sqlite3VdbeOp3(v, OP_String8, 0, 0, (char*)pDflt->z, pDflt->n);
495 }else{
496 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
497 }
drhf7eece62006-02-06 21:34:27 +0000498 sqlite3VdbeAddOp(v, OP_Integer, pCol->isPrimKey, 0);
drh9f6696a2006-02-09 16:52:23 +0000499 sqlite3VdbeAddOp(v, OP_Callback, 6, 0);
drhc11d4f92003-04-06 21:08:24 +0000500 }
501 }
502 }else
503
drh5260f7e2004-06-26 19:35:29 +0000504 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000505 Index *pIdx;
506 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000507 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000508 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000509 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000510 int i;
511 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000512 sqlite3VdbeSetNumCols(v, 3);
danielk1977955de522006-02-10 02:27:42 +0000513 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P3_STATIC);
514 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P3_STATIC);
515 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P3_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000516 for(i=0; i<pIdx->nColumn; i++){
517 int cnum = pIdx->aiColumn[i];
danielk19774adee202004-05-08 08:23:19 +0000518 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
519 sqlite3VdbeAddOp(v, OP_Integer, cnum, 0);
drhc11d4f92003-04-06 21:08:24 +0000520 assert( pTab->nCol>cnum );
danielk19770f69c1e2004-05-29 11:24:50 +0000521 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0);
danielk19774adee202004-05-08 08:23:19 +0000522 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
drhc11d4f92003-04-06 21:08:24 +0000523 }
524 }
525 }else
526
drh5260f7e2004-06-26 19:35:29 +0000527 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000528 Index *pIdx;
529 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000530 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000531 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000532 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000533 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000534 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000535 if( pIdx ){
536 int i = 0;
537 sqlite3VdbeSetNumCols(v, 3);
danielk1977955de522006-02-10 02:27:42 +0000538 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
539 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
540 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P3_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000541 while(pIdx){
542 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
543 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0);
544 sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
545 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
546 ++i;
547 pIdx = pIdx->pNext;
548 }
drhc11d4f92003-04-06 21:08:24 +0000549 }
550 }
551 }else
552
drh13d70422004-11-13 15:59:14 +0000553 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
554 int i;
555 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
556 sqlite3VdbeSetNumCols(v, 3);
danielk1977955de522006-02-10 02:27:42 +0000557 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
558 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
559 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P3_STATIC);
drh13d70422004-11-13 15:59:14 +0000560 for(i=0; i<db->nDb; i++){
561 if( db->aDb[i].pBt==0 ) continue;
562 assert( db->aDb[i].zName!=0 );
563 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
564 sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0);
565 sqlite3VdbeOp3(v, OP_String8, 0, 0,
566 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
567 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
568 }
569 }else
danielk197748af65a2005-02-09 03:20:37 +0000570
571 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
572 int i = 0;
573 HashElem *p;
574 sqlite3VdbeSetNumCols(v, 2);
danielk1977955de522006-02-10 02:27:42 +0000575 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
576 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
danielk197748af65a2005-02-09 03:20:37 +0000577 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
578 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
579 sqlite3VdbeAddOp(v, OP_Integer, i++, 0);
580 sqlite3VdbeOp3(v, OP_String8, 0, 0, pColl->zName, 0);
581 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
582 }
583 }else
drh13d70422004-11-13 15:59:14 +0000584#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
585
drhb7f91642004-10-31 02:22:47 +0000586#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000587 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000588 FKey *pFK;
589 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000590 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000591 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +0000592 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000593 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +0000594 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +0000595 if( pFK ){
596 int i = 0;
597 sqlite3VdbeSetNumCols(v, 5);
danielk1977955de522006-02-10 02:27:42 +0000598 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P3_STATIC);
599 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P3_STATIC);
600 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P3_STATIC);
601 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P3_STATIC);
602 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P3_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000603 while(pFK){
604 int j;
605 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +0000606 char *zCol = pFK->aCol[j].zCol;
danielk1977742f9472004-06-16 12:02:43 +0000607 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
608 sqlite3VdbeAddOp(v, OP_Integer, j, 0);
609 sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0);
610 sqlite3VdbeOp3(v, OP_String8, 0, 0,
611 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2f471492005-06-23 03:15:07 +0000612 sqlite3VdbeOp3(v, zCol ? OP_String8 : OP_Null, 0, 0, zCol, 0);
danielk1977742f9472004-06-16 12:02:43 +0000613 sqlite3VdbeAddOp(v, OP_Callback, 5, 0);
614 }
615 ++i;
616 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +0000617 }
drh78100cc2003-08-23 22:40:53 +0000618 }
619 }
620 }else
drhb7f91642004-10-31 02:22:47 +0000621#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +0000622
drhc11d4f92003-04-06 21:08:24 +0000623#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +0000624 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
625 extern void sqlite3ParserTrace(FILE*, char *);
drh55ef4d92005-08-14 01:20:37 +0000626 if( zRight ){
627 if( getBoolean(zRight) ){
628 sqlite3ParserTrace(stderr, "parser: ");
629 }else{
630 sqlite3ParserTrace(0, 0);
631 }
drhc11d4f92003-04-06 21:08:24 +0000632 }
633 }else
634#endif
635
drh55ef4d92005-08-14 01:20:37 +0000636 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
637 ** used will be case sensitive or not depending on the RHS.
638 */
639 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
640 if( zRight ){
641 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
642 }
643 }else
644
drhb7f91642004-10-31 02:22:47 +0000645#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk19774adee202004-05-08 08:23:19 +0000646 if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){
drhed717fe2003-06-15 23:42:24 +0000647 int i, j, addr;
648
drhed717fe2003-06-15 23:42:24 +0000649 /* Code that appears at the end of the integrity check. If no error
650 ** messages have been generated, output OK. Otherwise output the
651 ** error message
652 */
drh57196282004-10-06 15:41:16 +0000653 static const VdbeOpList endCode[] = {
drhed717fe2003-06-15 23:42:24 +0000654 { OP_MemLoad, 0, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000655 { OP_Integer, 0, 0, 0},
656 { OP_Ne, 0, 0, 0}, /* 2 */
drh290c1942004-08-21 17:54:45 +0000657 { OP_String8, 0, 0, "ok"},
drhc11d4f92003-04-06 21:08:24 +0000658 { OP_Callback, 1, 0, 0},
659 };
drhed717fe2003-06-15 23:42:24 +0000660
661 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +0000662 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197722322fd2004-05-25 23:35:17 +0000663 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000664 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P3_STATIC);
drhd654be82005-09-20 17:42:23 +0000665 sqlite3VdbeAddOp(v, OP_MemInt, 0, 0); /* Initialize error count to 0 */
drhed717fe2003-06-15 23:42:24 +0000666
667 /* Do an integrity check on each database file */
668 for(i=0; i<db->nDb; i++){
669 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +0000670 Hash *pTbls;
drh79069752004-05-22 21:30:40 +0000671 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +0000672
danielk197753c0f742005-03-29 03:10:59 +0000673 if( OMIT_TEMPDB && i==1 ) continue;
674
drh80242052004-06-09 00:48:12 +0000675 sqlite3CodeVerifySchema(pParse, i);
676
drhed717fe2003-06-15 23:42:24 +0000677 /* Do an integrity check of the B-Tree
678 */
danielk1977da184232006-01-05 11:34:32 +0000679 pTbls = &db->aDb[i].pSchema->tblHash;
680 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +0000681 Table *pTab = sqliteHashData(x);
682 Index *pIdx;
683 sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0);
684 cnt++;
685 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
686 sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0);
687 cnt++;
688 }
689 }
danielk19772b444852004-06-29 07:45:33 +0000690 assert( cnt>0 );
drh79069752004-05-22 21:30:40 +0000691 sqlite3VdbeAddOp(v, OP_IntegrityCk, cnt, i);
692 sqlite3VdbeAddOp(v, OP_Dup, 0, 1);
danielk19770f69c1e2004-05-29 11:24:50 +0000693 addr = sqlite3VdbeOp3(v, OP_String8, 0, 0, "ok", P3_STATIC);
drh23cc57f2005-10-06 13:59:26 +0000694 sqlite3VdbeAddOp(v, OP_Eq, 0, addr+7);
danielk19770f69c1e2004-05-29 11:24:50 +0000695 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drh79069752004-05-22 21:30:40 +0000696 sqlite3MPrintf("*** in database %s ***\n", db->aDb[i].zName),
697 P3_DYNAMIC);
698 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drh855eb1c2004-08-31 13:45:11 +0000699 sqlite3VdbeAddOp(v, OP_Concat, 0, 1);
drh79069752004-05-22 21:30:40 +0000700 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
drh15007a92006-01-08 18:10:17 +0000701 sqlite3VdbeAddOp(v, OP_MemIncr, 1, 0);
drhed717fe2003-06-15 23:42:24 +0000702
703 /* Make sure all the indices are constructed correctly.
704 */
danielk19774adee202004-05-08 08:23:19 +0000705 sqlite3CodeVerifySchema(pParse, i);
danielk1977da184232006-01-05 11:34:32 +0000706 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +0000707 Table *pTab = sqliteHashData(x);
708 Index *pIdx;
709 int loopTop;
710
711 if( pTab->pIndex==0 ) continue;
drh290c1942004-08-21 17:54:45 +0000712 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drhd654be82005-09-20 17:42:23 +0000713 sqlite3VdbeAddOp(v, OP_MemInt, 0, 1);
danielk19774adee202004-05-08 08:23:19 +0000714 loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0);
drh15007a92006-01-08 18:10:17 +0000715 sqlite3VdbeAddOp(v, OP_MemIncr, 1, 1);
drhed717fe2003-06-15 23:42:24 +0000716 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +0000717 int jmp2;
drh57196282004-10-06 15:41:16 +0000718 static const VdbeOpList idxErr[] = {
drh15007a92006-01-08 18:10:17 +0000719 { OP_MemIncr, 1, 0, 0},
drh290c1942004-08-21 17:54:45 +0000720 { OP_String8, 0, 0, "rowid "},
drhf0863fe2005-06-12 21:35:51 +0000721 { OP_Rowid, 1, 0, 0},
drh290c1942004-08-21 17:54:45 +0000722 { OP_String8, 0, 0, " missing from index "},
723 { OP_String8, 0, 0, 0}, /* 4 */
drh855eb1c2004-08-31 13:45:11 +0000724 { OP_Concat, 2, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000725 { OP_Callback, 1, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000726 };
drh51846b52004-05-28 16:00:21 +0000727 sqlite3GenerateIndexKey(v, pIdx, 1);
danielk19774adee202004-05-08 08:23:19 +0000728 jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0);
729 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
730 sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC);
drhd654be82005-09-20 17:42:23 +0000731 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +0000732 }
danielk19774adee202004-05-08 08:23:19 +0000733 sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +0000734 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +0000735 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +0000736 static const VdbeOpList cntIdx[] = {
drhd654be82005-09-20 17:42:23 +0000737 { OP_MemInt, 0, 2, 0},
738 { OP_Rewind, 0, 0, 0}, /* 1 */
drh15007a92006-01-08 18:10:17 +0000739 { OP_MemIncr, 1, 2, 0},
drhd654be82005-09-20 17:42:23 +0000740 { OP_Next, 0, 0, 0}, /* 3 */
drhed717fe2003-06-15 23:42:24 +0000741 { OP_MemLoad, 1, 0, 0},
742 { OP_MemLoad, 2, 0, 0},
drhd654be82005-09-20 17:42:23 +0000743 { OP_Eq, 0, 0, 0}, /* 6 */
drh15007a92006-01-08 18:10:17 +0000744 { OP_MemIncr, 1, 0, 0},
drh290c1942004-08-21 17:54:45 +0000745 { OP_String8, 0, 0, "wrong # of entries in index "},
drhd654be82005-09-20 17:42:23 +0000746 { OP_String8, 0, 0, 0}, /* 9 */
drh855eb1c2004-08-31 13:45:11 +0000747 { OP_Concat, 0, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000748 { OP_Callback, 1, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000749 };
750 if( pIdx->tnum==0 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000751 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +0000752 sqlite3VdbeChangeP1(v, addr+1, j+2);
753 sqlite3VdbeChangeP2(v, addr+1, addr+4);
754 sqlite3VdbeChangeP1(v, addr+3, j+2);
755 sqlite3VdbeChangeP2(v, addr+3, addr+2);
756 sqlite3VdbeJumpHere(v, addr+6);
757 sqlite3VdbeChangeP3(v, addr+9, pIdx->zName, P3_STATIC);
drhed717fe2003-06-15 23:42:24 +0000758 }
759 }
760 }
danielk19774adee202004-05-08 08:23:19 +0000761 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drhd654be82005-09-20 17:42:23 +0000762 sqlite3VdbeJumpHere(v, addr+2);
drhc11d4f92003-04-06 21:08:24 +0000763 }else
drhb7f91642004-10-31 02:22:47 +0000764#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
765
drh13d70422004-11-13 15:59:14 +0000766#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +0000767 /*
768 ** PRAGMA encoding
769 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
770 **
771 ** In it's first form, this pragma returns the encoding of the main
772 ** database. If the database is not initialized, it is initialized now.
773 **
774 ** The second form of this pragma is a no-op if the main database file
775 ** has not already been initialized. In this case it sets the default
776 ** encoding that will be used for the main database file if a new file
777 ** is created. If an existing main database file is opened, then the
778 ** default text encoding for the existing database is used.
779 **
780 ** In all cases new databases created using the ATTACH command are
781 ** created to use the same default text encoding as the main database. If
782 ** the main database has not been initialized and/or created when ATTACH
783 ** is executed, this is done before the ATTACH operation.
784 **
785 ** In the second form this pragma sets the text encoding to be used in
786 ** new database files created using this database handle. It is only
787 ** useful if invoked immediately after the main database i
788 */
789 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +0000790 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +0000791 char *zName;
792 u8 enc;
793 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +0000794 { "UTF-8", SQLITE_UTF8 },
795 { "UTF8", SQLITE_UTF8 },
796 { "UTF-16le", SQLITE_UTF16LE },
797 { "UTF16le", SQLITE_UTF16LE },
798 { "UTF-16be", SQLITE_UTF16BE },
799 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +0000800 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
801 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +0000802 { 0, 0 }
803 };
drh0f7eb612006-08-08 13:51:43 +0000804 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +0000805 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +0000806 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +0000807 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000808 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P3_STATIC);
danielk19778e227872004-06-07 07:52:17 +0000809 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
810 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
danielk197714db2662006-01-09 16:12:04 +0000811 if( pEnc->enc==ENC(pParse->db) ){
danielk19778e227872004-06-07 07:52:17 +0000812 sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC);
813 break;
814 }
815 }
816 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
817 }else{ /* "PRAGMA encoding = XXX" */
818 /* Only change the value of sqlite.enc if the database handle is not
819 ** initialized. If the main database exists, the new sqlite.enc value
820 ** will be overwritten when the schema is next loaded. If it does not
821 ** already exists, it will be created to use the new encoding value.
822 */
danielk1977b82e7ed2006-01-11 14:09:31 +0000823 if(
824 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
825 DbHasProperty(db, 0, DB_Empty)
826 ){
danielk19778e227872004-06-07 07:52:17 +0000827 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
828 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +0000829 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +0000830 break;
831 }
832 }
833 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +0000834 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +0000835 }
836 }
837 }
838 }else
drh13d70422004-11-13 15:59:14 +0000839#endif /* SQLITE_OMIT_UTF16 */
840
841#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +0000842 /*
danielk1977b92b70b2004-11-12 16:11:59 +0000843 ** PRAGMA [database.]schema_version
844 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +0000845 **
danielk1977b92b70b2004-11-12 16:11:59 +0000846 ** PRAGMA [database.]user_version
847 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +0000848 **
danielk1977b92b70b2004-11-12 16:11:59 +0000849 ** The pragma's schema_version and user_version are used to set or get
850 ** the value of the schema-version and user-version, respectively. Both
851 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +0000852 ** stored in the database header.
853 **
854 ** The schema-cookie is usually only manipulated internally by SQLite. It
855 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +0000856 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +0000857 ** SQLite each time a query is executed to ensure that the internal cache
858 ** of the schema used when compiling the SQL query matches the schema of
859 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +0000860 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
861 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +0000862 ** crashes or database corruption. Use with caution!
863 **
danielk1977b92b70b2004-11-12 16:11:59 +0000864 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +0000865 ** applications for any purpose.
866 */
danielk1977b92b70b2004-11-12 16:11:59 +0000867 if( sqlite3StrICmp(zLeft, "schema_version")==0 ||
868 sqlite3StrICmp(zLeft, "user_version")==0 ){
danielk1977dae24952004-11-11 05:10:43 +0000869
870 int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
871 if( zLeft[0]=='s' || zLeft[0]=='S' ){
872 iCookie = 0;
873 }else{
874 iCookie = 5;
875 }
876
877 if( zRight ){
878 /* Write the specified cookie value */
879 static const VdbeOpList setCookie[] = {
880 { OP_Transaction, 0, 1, 0}, /* 0 */
881 { OP_Integer, 0, 0, 0}, /* 1 */
882 { OP_SetCookie, 0, 0, 0}, /* 2 */
883 };
884 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
885 sqlite3VdbeChangeP1(v, addr, iDb);
886 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
887 sqlite3VdbeChangeP1(v, addr+2, iDb);
888 sqlite3VdbeChangeP2(v, addr+2, iCookie);
889 }else{
890 /* Read the specified cookie value */
891 static const VdbeOpList readCookie[] = {
892 { OP_ReadCookie, 0, 0, 0}, /* 0 */
893 { OP_Callback, 1, 0, 0}
894 };
895 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
896 sqlite3VdbeChangeP1(v, addr, iDb);
897 sqlite3VdbeChangeP2(v, addr, iCookie);
898 sqlite3VdbeSetNumCols(v, 1);
899 }
900 }
drh13d70422004-11-13 15:59:14 +0000901#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000902
dougcurrie81c95ef2004-06-18 23:21:47 +0000903#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +0000904 /*
905 ** Report the current state of file logs for all databases
906 */
907 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +0000908 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +0000909 "unlocked", "shared", "reserved", "pending", "exclusive"
910 };
911 int i;
912 Vdbe *v = sqlite3GetVdbe(pParse);
913 sqlite3VdbeSetNumCols(v, 2);
danielk1977955de522006-02-10 02:27:42 +0000914 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P3_STATIC);
915 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P3_STATIC);
drh89ac8c12004-06-09 14:17:20 +0000916 for(i=0; i<db->nDb; i++){
917 Btree *pBt;
918 Pager *pPager;
919 if( db->aDb[i].zName==0 ) continue;
drha67367e2005-09-17 16:36:55 +0000920 sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, P3_STATIC);
drh89ac8c12004-06-09 14:17:20 +0000921 pBt = db->aDb[i].pBt;
922 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drha67367e2005-09-17 16:36:55 +0000923 sqlite3VdbeOp3(v, OP_String8, 0, 0, "closed", P3_STATIC);
drh89ac8c12004-06-09 14:17:20 +0000924 }else{
925 int j = sqlite3pager_lockstate(pPager);
drha67367e2005-09-17 16:36:55 +0000926 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drh89ac8c12004-06-09 14:17:20 +0000927 (j>=0 && j<=4) ? azLockName[j] : "unknown", P3_STATIC);
928 }
929 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
930 }
931 }else
932#endif
933
drh89dec812005-04-28 19:03:37 +0000934#ifdef SQLITE_SSE
935 /*
936 ** Check to see if the sqlite_statements table exists. Create it
937 ** if it does not.
938 */
939 if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
danielk19773a3f38e2005-05-22 06:49:56 +0000940 extern int sqlite3CreateStatementsTable(Parse*);
941 sqlite3CreateStatementsTable(pParse);
drh89dec812005-04-28 19:03:37 +0000942 }else
943#endif
944
drh3c4f2a42005-12-08 18:12:56 +0000945#if SQLITE_HAS_CODEC
946 if( sqlite3StrICmp(zLeft, "key")==0 ){
947 sqlite3_key(db, zRight, strlen(zRight));
948 }else
949#endif
drh21e2cab2006-09-25 18:01:57 +0000950#if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
951 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
952#if SQLITE_HAS_CODEC
953 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
954 extern void sqlite3_activate_see(const char*);
955 sqlite3_activate_see(&zRight[4]);
956 }
957#endif
958#ifdef SQLITE_ENABLE_CEROD
959 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
960 extern void sqlite3_activate_cerod(const char*);
961 sqlite3_activate_cerod(&zRight[6]);
962 }
963#endif
964 }
965#endif
drh3c4f2a42005-12-08 18:12:56 +0000966
drhc11d4f92003-04-06 21:08:24 +0000967 {}
danielk1977a21c6b62005-01-24 10:25:59 +0000968
969 if( v ){
970 /* Code an OP_Expire at the end of each PRAGMA program to cause
971 ** the VDBE implementing the pragma to expire. Most (all?) pragmas
972 ** are only valid for a single execution.
973 */
974 sqlite3VdbeAddOp(v, OP_Expire, 1, 0);
drhac530b12006-02-11 01:25:50 +0000975
976 /*
977 ** Reset the safety level, in case the fullfsync flag or synchronous
978 ** setting changed.
979 */
danielk1977ddfb2f02006-02-17 12:25:14 +0000980#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhac530b12006-02-11 01:25:50 +0000981 if( db->autoCommit ){
982 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
983 (db->flags&SQLITE_FullFSync)!=0);
984 }
danielk1977ddfb2f02006-02-17 12:25:14 +0000985#endif
danielk1977a21c6b62005-01-24 10:25:59 +0000986 }
danielk1977e0048402004-06-15 16:51:01 +0000987pragma_out:
drhc11d4f92003-04-06 21:08:24 +0000988 sqliteFree(zLeft);
989 sqliteFree(zRight);
990}
drh13d70422004-11-13 15:59:14 +0000991
drh0ccebe72005-06-07 22:22:50 +0000992#endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */