blob: befdf22e406ec5be1cf086797341bbcd1f1b9328 [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**
drhf7eece62006-02-06 21:34:27 +000014** $Id: pragma.c,v 1.115 2006/02/06 21:34:27 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);
131 sqlite3VdbeSetColName(v, 0, zLabel, P3_STATIC);
132 }
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 },
drh0cd2d4c2005-11-03 02:15:02 +0000155#ifndef SQLITE_OMIT_CHECK
156 { "ignore_check_constraints", SQLITE_IgnoreChecks },
157#endif
drh722e95a2004-10-25 20:33:44 +0000158 /* The following is VERY experimental */
drhf4040832004-10-22 20:29:21 +0000159 { "writable_schema", SQLITE_WriteSchema },
drh7bec5052005-02-06 02:45:41 +0000160 { "omit_readlock", SQLITE_NoReadlock },
danielk1977da184232006-01-05 11:34:32 +0000161
162 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
163 ** flag if there are any active statements. */
164 { "read_uncommitted", SQLITE_ReadUncommitted },
drh87223182004-02-21 14:00:29 +0000165 };
166 int i;
drh722e95a2004-10-25 20:33:44 +0000167 const struct sPragmaType *p;
168 for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
169 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000170 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000171 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000172 v = sqlite3GetVdbe(pParse);
173 if( v ){
174 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000175 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000176 }else{
danielk1977a21c6b62005-01-24 10:25:59 +0000177 if( getBoolean(zRight) ){
178 db->flags |= p->mask;
179 }else{
180 db->flags &= ~p->mask;
181 }
drhd89bd002005-01-22 03:03:54 +0000182 }
danielk1977a21c6b62005-01-24 10:25:59 +0000183 /* If one of these pragmas is executed, any prepared statements
184 ** need to be recompiled.
185 */
186 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
drh87223182004-02-21 14:00:29 +0000187 }
188 return 1;
189 }
190 }
191 return 0;
192}
drhbf216272005-02-26 18:10:44 +0000193#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000194
195/*
drhc11d4f92003-04-06 21:08:24 +0000196** Process a pragma statement.
197**
198** Pragmas are of this form:
199**
danielk197791cf71b2004-06-26 06:37:06 +0000200** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000201**
202** The identifier might also be a string. The value is a string, and
203** identifier, or a number. If minusFlag is true, then the value is
204** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000205**
206** If the left side is "database.id" then pId1 is the database name
207** and pId2 is the id. If the left side is just "id" then pId1 is the
208** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000209*/
danielk197791cf71b2004-06-26 06:37:06 +0000210void sqlite3Pragma(
211 Parse *pParse,
212 Token *pId1, /* First part of [database.]id field */
213 Token *pId2, /* Second part of [database.]id field, or NULL */
214 Token *pValue, /* Token for <value>, or NULL */
215 int minusFlag /* True if a '-' sign preceded <value> */
216){
217 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
218 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
219 const char *zDb = 0; /* The database name */
220 Token *pId; /* Pointer to <id> token */
221 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000222 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000223 Db *pDb;
danielk19774adee202004-05-08 08:23:19 +0000224 Vdbe *v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000225 if( v==0 ) return;
226
danielk197791cf71b2004-06-26 06:37:06 +0000227 /* Interpret the [database.] part of the pragma statement. iDb is the
228 ** index of the database this pragma is being applied to in db.aDb[]. */
229 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
230 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000231 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000232
233 zLeft = sqlite3NameFromToken(pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000234 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000235 if( minusFlag ){
drhae29ffb2004-09-25 14:39:18 +0000236 zRight = sqlite3MPrintf("-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000237 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000238 zRight = sqlite3NameFromToken(pValue);
drhc11d4f92003-04-06 21:08:24 +0000239 }
danielk197791cf71b2004-06-26 06:37:06 +0000240
drh90f5ecb2004-07-22 01:19:35 +0000241 zDb = ((iDb>0)?pDb->zName:0);
danielk197791cf71b2004-06-26 06:37:06 +0000242 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000243 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000244 }
245
drh13d70422004-11-13 15:59:14 +0000246#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000247 /*
drh90f5ecb2004-07-22 01:19:35 +0000248 ** PRAGMA [database.]default_cache_size
249 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000250 **
251 ** The first form reports the current persistent setting for the
252 ** page cache size. The value returned is the maximum number of
253 ** pages in the page cache. The second form sets both the current
254 ** page cache size value and the persistent page cache size value
255 ** stored in the database file.
256 **
257 ** The default cache size is stored in meta-value 2 of page 1 of the
258 ** database file. The cache size is actually the absolute value of
259 ** this memory location. The sign of meta-value 2 determines the
260 ** synchronous setting. A negative value means synchronous is off
261 ** and a positive value means synchronous is on.
262 */
danielk19774adee202004-05-08 08:23:19 +0000263 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000264 static const VdbeOpList getCacheSize[] = {
danielk197791cf71b2004-06-26 06:37:06 +0000265 { OP_ReadCookie, 0, 2, 0}, /* 0 */
drhc11d4f92003-04-06 21:08:24 +0000266 { OP_AbsValue, 0, 0, 0},
267 { OP_Dup, 0, 0, 0},
268 { OP_Integer, 0, 0, 0},
269 { OP_Ne, 0, 6, 0},
drh905793e2004-02-21 13:31:09 +0000270 { OP_Integer, 0, 0, 0}, /* 5 */
drhc11d4f92003-04-06 21:08:24 +0000271 { OP_Callback, 1, 0, 0},
272 };
drh905793e2004-02-21 13:31:09 +0000273 int addr;
danielk19778a414492004-06-29 08:59:35 +0000274 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000275 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000276 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000277 sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000278 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000279 sqlite3VdbeChangeP1(v, addr, iDb);
danielk19774adee202004-05-08 08:23:19 +0000280 sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES);
drhc11d4f92003-04-06 21:08:24 +0000281 }else{
drhc11d4f92003-04-06 21:08:24 +0000282 int size = atoi(zRight);
283 if( size<0 ) size = -size;
danielk197791cf71b2004-06-26 06:37:06 +0000284 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk19774adee202004-05-08 08:23:19 +0000285 sqlite3VdbeAddOp(v, OP_Integer, size, 0);
danielk197791cf71b2004-06-26 06:37:06 +0000286 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2);
danielk19774adee202004-05-08 08:23:19 +0000287 addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
288 sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3);
289 sqlite3VdbeAddOp(v, OP_Negative, 0, 0);
danielk197791cf71b2004-06-26 06:37:06 +0000290 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2);
danielk197714db2662006-01-09 16:12:04 +0000291 pDb->pSchema->cache_size = size;
292 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000293 }
294 }else
295
296 /*
drh90f5ecb2004-07-22 01:19:35 +0000297 ** PRAGMA [database.]page_size
298 ** PRAGMA [database.]page_size=N
299 **
300 ** The first form reports the current setting for the
301 ** database page size in bytes. The second form sets the
302 ** database page size value. The value can only be set if
303 ** the database has not yet been created.
304 */
305 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
306 Btree *pBt = pDb->pBt;
307 if( !zRight ){
308 int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000309 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000310 }else{
danielk197728129562005-01-11 10:25:06 +0000311 sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1);
drh90f5ecb2004-07-22 01:19:35 +0000312 }
313 }else
drh13d70422004-11-13 15:59:14 +0000314#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000315
316 /*
danielk1977951af802004-11-05 15:45:09 +0000317 ** PRAGMA [database.]auto_vacuum
318 ** PRAGMA [database.]auto_vacuum=N
319 **
320 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
321 */
322#ifndef SQLITE_OMIT_AUTOVACUUM
323 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
324 Btree *pBt = pDb->pBt;
325 if( !zRight ){
326 int auto_vacuum =
327 pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
328 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
329 }else{
330 sqlite3BtreeSetAutoVacuum(pBt, getBoolean(zRight));
331 }
332 }else
333#endif
334
drh13d70422004-11-13 15:59:14 +0000335#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000336 /*
drh90f5ecb2004-07-22 01:19:35 +0000337 ** PRAGMA [database.]cache_size
338 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000339 **
340 ** The first form reports the current local setting for the
341 ** page cache size. The local setting can be different from
342 ** the persistent cache size value that is stored in the database
343 ** file itself. The value returned is the maximum number of
344 ** pages in the page cache. The second form sets the local
345 ** page cache size value. It does not change the persistent
346 ** cache size stored on the disk so the cache size will revert
347 ** to its default value when the database is closed and reopened.
348 ** N should be a positive integer.
349 */
danielk19774adee202004-05-08 08:23:19 +0000350 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000351 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000352 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000353 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000354 }else{
355 int size = atoi(zRight);
356 if( size<0 ) size = -size;
danielk197714db2662006-01-09 16:12:04 +0000357 pDb->pSchema->cache_size = size;
358 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000359 }
360 }else
361
362 /*
drh90f5ecb2004-07-22 01:19:35 +0000363 ** PRAGMA temp_store
364 ** PRAGMA temp_store = "default"|"memory"|"file"
365 **
366 ** Return or set the local value of the temp_store flag. Changing
367 ** the local value does not make changes to the disk file and the default
368 ** value will be restored the next time the database is opened.
369 **
370 ** Note that it is possible for the library compile-time options to
371 ** override this setting
372 */
373 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
374 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000375 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000376 }else{
377 changeTempStorage(pParse, zRight);
378 }
379 }else
380
381 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000382 ** PRAGMA temp_store_directory
383 ** PRAGMA temp_store_directory = ""|"directory_name"
384 **
385 ** Return or set the local value of the temp_store_directory flag. Changing
386 ** the value sets a specific directory to be used for temporary files.
387 ** Setting to a null string reverts to the default temporary directory search.
388 ** If temporary directory is changed, then invalidateTempStorage.
389 **
390 */
391 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
392 if( !zRight ){
393 if( sqlite3_temp_directory ){
394 sqlite3VdbeSetNumCols(v, 1);
395 sqlite3VdbeSetColName(v, 0, "temp_store_directory", P3_STATIC);
396 sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0);
397 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
398 }
399 }else{
drh66560ad2006-01-06 14:32:19 +0000400 if( zRight[0] && !sqlite3OsIsDirWritable(zRight) ){
drh268283b2005-01-08 15:44:25 +0000401 sqlite3ErrorMsg(pParse, "not a writable directory");
402 goto pragma_out;
403 }
404 if( TEMP_STORE==0
405 || (TEMP_STORE==1 && db->temp_store<=1)
406 || (TEMP_STORE==2 && db->temp_store==1)
407 ){
408 invalidateTempStorage(pParse);
409 }
410 sqliteFree(sqlite3_temp_directory);
411 if( zRight[0] ){
412 sqlite3_temp_directory = zRight;
413 zRight = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000414 }else{
drh268283b2005-01-08 15:44:25 +0000415 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000416 }
417 }
418 }else
419
420 /*
drh90f5ecb2004-07-22 01:19:35 +0000421 ** PRAGMA [database.]synchronous
422 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000423 **
424 ** Return or set the local value of the synchronous flag. Changing
425 ** the local value does not make changes to the disk file and the
426 ** default value will be restored the next time the database is
427 ** opened.
428 */
danielk19774adee202004-05-08 08:23:19 +0000429 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000430 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000431 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000432 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000433 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000434 if( !db->autoCommit ){
435 sqlite3ErrorMsg(pParse,
436 "Safety level may not be changed inside a transaction");
437 }else{
drh90f5ecb2004-07-22 01:19:35 +0000438 pDb->safety_level = getSafetyLevel(zRight)+1;
439 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level);
danielk197791cf71b2004-06-26 06:37:06 +0000440 }
drhc11d4f92003-04-06 21:08:24 +0000441 }
442 }else
drh13d70422004-11-13 15:59:14 +0000443#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000444
drhbf216272005-02-26 18:10:44 +0000445#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000446 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000447 /* The flagPragma() subroutine also generates any necessary code
448 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000449 }else
drhbf216272005-02-26 18:10:44 +0000450#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000451
drh13d70422004-11-13 15:59:14 +0000452#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000453 /*
454 ** PRAGMA table_info(<table>)
455 **
456 ** Return a single row for each column of the named table. The columns of
457 ** the returned data set are:
458 **
459 ** cid: Column id (numbered from left to right, starting at 0)
460 ** name: Column name
461 ** type: Column declaration type.
462 ** notnull: True if 'NOT NULL' is part of column declaration
463 ** dflt_value: The default value for the column, if any.
464 */
drh5260f7e2004-06-26 19:35:29 +0000465 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000466 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000467 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000468 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000469 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000470 int i;
drhf7eece62006-02-06 21:34:27 +0000471 Column *pCol;
472 sqlite3VdbeSetNumCols(v, 8);
danielk19773cf86062004-05-26 10:11:05 +0000473 sqlite3VdbeSetColName(v, 0, "cid", P3_STATIC);
474 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
475 sqlite3VdbeSetColName(v, 2, "type", P3_STATIC);
476 sqlite3VdbeSetColName(v, 3, "notnull", P3_STATIC);
477 sqlite3VdbeSetColName(v, 4, "dflt_value", P3_STATIC);
478 sqlite3VdbeSetColName(v, 5, "pk", P3_STATIC);
drhf7eece62006-02-06 21:34:27 +0000479 sqlite3VdbeSetColName(v, 6, "autoinc", P3_STATIC);
480 sqlite3VdbeSetColName(v, 7, "collseq", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000481 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000482 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000483 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
drhf7eece62006-02-06 21:34:27 +0000484 sqlite3VdbeOp3(v, OP_String8, 0, 0, pCol->zName, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000485 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drhf7eece62006-02-06 21:34:27 +0000486 pCol->zType ? pCol->zType : "numeric", 0);
487 sqlite3VdbeAddOp(v, OP_Integer, pCol->notNull, 0);
488 sqlite3ExprCode(pParse, pCol->pDflt);
489 sqlite3VdbeAddOp(v, OP_Integer, pCol->isPrimKey, 0);
490 sqlite3VdbeAddOp(v, OP_Integer, pCol->isPrimKey && pTab->autoInc, 0);
491 sqlite3VdbeOp3(v, OP_String8, 0, 0,
492 pCol->zColl ? pCol->zColl : "binary", 0);
493 sqlite3VdbeAddOp(v, OP_Callback, 8, 0);
drhc11d4f92003-04-06 21:08:24 +0000494 }
495 }
496 }else
497
drh5260f7e2004-06-26 19:35:29 +0000498 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000499 Index *pIdx;
500 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000501 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000502 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000503 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000504 int i;
505 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000506 sqlite3VdbeSetNumCols(v, 3);
danielk19773cf86062004-05-26 10:11:05 +0000507 sqlite3VdbeSetColName(v, 0, "seqno", P3_STATIC);
508 sqlite3VdbeSetColName(v, 1, "cid", P3_STATIC);
509 sqlite3VdbeSetColName(v, 2, "name", P3_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000510 for(i=0; i<pIdx->nColumn; i++){
511 int cnum = pIdx->aiColumn[i];
danielk19774adee202004-05-08 08:23:19 +0000512 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
513 sqlite3VdbeAddOp(v, OP_Integer, cnum, 0);
drhc11d4f92003-04-06 21:08:24 +0000514 assert( pTab->nCol>cnum );
danielk19770f69c1e2004-05-29 11:24:50 +0000515 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0);
danielk19774adee202004-05-08 08:23:19 +0000516 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
drhc11d4f92003-04-06 21:08:24 +0000517 }
518 }
519 }else
520
drh5260f7e2004-06-26 19:35:29 +0000521 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000522 Index *pIdx;
523 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000524 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000525 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000526 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000527 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000528 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000529 if( pIdx ){
530 int i = 0;
531 sqlite3VdbeSetNumCols(v, 3);
532 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
533 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
534 sqlite3VdbeSetColName(v, 2, "unique", P3_STATIC);
535 while(pIdx){
536 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
537 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0);
538 sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
539 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
540 ++i;
541 pIdx = pIdx->pNext;
542 }
drhc11d4f92003-04-06 21:08:24 +0000543 }
544 }
545 }else
546
drh13d70422004-11-13 15:59:14 +0000547 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
548 int i;
549 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
550 sqlite3VdbeSetNumCols(v, 3);
551 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
552 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
553 sqlite3VdbeSetColName(v, 2, "file", P3_STATIC);
554 for(i=0; i<db->nDb; i++){
555 if( db->aDb[i].pBt==0 ) continue;
556 assert( db->aDb[i].zName!=0 );
557 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
558 sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0);
559 sqlite3VdbeOp3(v, OP_String8, 0, 0,
560 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
561 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
562 }
563 }else
danielk197748af65a2005-02-09 03:20:37 +0000564
565 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
566 int i = 0;
567 HashElem *p;
568 sqlite3VdbeSetNumCols(v, 2);
569 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
570 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
571 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
572 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
573 sqlite3VdbeAddOp(v, OP_Integer, i++, 0);
574 sqlite3VdbeOp3(v, OP_String8, 0, 0, pColl->zName, 0);
575 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
576 }
577 }else
drh13d70422004-11-13 15:59:14 +0000578#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
579
drhb7f91642004-10-31 02:22:47 +0000580#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000581 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000582 FKey *pFK;
583 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000584 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000585 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +0000586 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000587 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +0000588 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +0000589 if( pFK ){
590 int i = 0;
591 sqlite3VdbeSetNumCols(v, 5);
592 sqlite3VdbeSetColName(v, 0, "id", P3_STATIC);
593 sqlite3VdbeSetColName(v, 1, "seq", P3_STATIC);
594 sqlite3VdbeSetColName(v, 2, "table", P3_STATIC);
595 sqlite3VdbeSetColName(v, 3, "from", P3_STATIC);
596 sqlite3VdbeSetColName(v, 4, "to", P3_STATIC);
597 while(pFK){
598 int j;
599 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +0000600 char *zCol = pFK->aCol[j].zCol;
danielk1977742f9472004-06-16 12:02:43 +0000601 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
602 sqlite3VdbeAddOp(v, OP_Integer, j, 0);
603 sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0);
604 sqlite3VdbeOp3(v, OP_String8, 0, 0,
605 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2f471492005-06-23 03:15:07 +0000606 sqlite3VdbeOp3(v, zCol ? OP_String8 : OP_Null, 0, 0, zCol, 0);
danielk1977742f9472004-06-16 12:02:43 +0000607 sqlite3VdbeAddOp(v, OP_Callback, 5, 0);
608 }
609 ++i;
610 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +0000611 }
drh78100cc2003-08-23 22:40:53 +0000612 }
613 }
614 }else
drhb7f91642004-10-31 02:22:47 +0000615#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +0000616
drhc11d4f92003-04-06 21:08:24 +0000617#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +0000618 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
619 extern void sqlite3ParserTrace(FILE*, char *);
drh55ef4d92005-08-14 01:20:37 +0000620 if( zRight ){
621 if( getBoolean(zRight) ){
622 sqlite3ParserTrace(stderr, "parser: ");
623 }else{
624 sqlite3ParserTrace(0, 0);
625 }
drhc11d4f92003-04-06 21:08:24 +0000626 }
627 }else
628#endif
629
drh55ef4d92005-08-14 01:20:37 +0000630 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
631 ** used will be case sensitive or not depending on the RHS.
632 */
633 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
634 if( zRight ){
635 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
636 }
637 }else
638
drhb7f91642004-10-31 02:22:47 +0000639#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk19774adee202004-05-08 08:23:19 +0000640 if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){
drhed717fe2003-06-15 23:42:24 +0000641 int i, j, addr;
642
drhed717fe2003-06-15 23:42:24 +0000643 /* Code that appears at the end of the integrity check. If no error
644 ** messages have been generated, output OK. Otherwise output the
645 ** error message
646 */
drh57196282004-10-06 15:41:16 +0000647 static const VdbeOpList endCode[] = {
drhed717fe2003-06-15 23:42:24 +0000648 { OP_MemLoad, 0, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000649 { OP_Integer, 0, 0, 0},
650 { OP_Ne, 0, 0, 0}, /* 2 */
drh290c1942004-08-21 17:54:45 +0000651 { OP_String8, 0, 0, "ok"},
drhc11d4f92003-04-06 21:08:24 +0000652 { OP_Callback, 1, 0, 0},
653 };
drhed717fe2003-06-15 23:42:24 +0000654
655 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +0000656 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197722322fd2004-05-25 23:35:17 +0000657 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000658 sqlite3VdbeSetColName(v, 0, "integrity_check", P3_STATIC);
drhd654be82005-09-20 17:42:23 +0000659 sqlite3VdbeAddOp(v, OP_MemInt, 0, 0); /* Initialize error count to 0 */
drhed717fe2003-06-15 23:42:24 +0000660
661 /* Do an integrity check on each database file */
662 for(i=0; i<db->nDb; i++){
663 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +0000664 Hash *pTbls;
drh79069752004-05-22 21:30:40 +0000665 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +0000666
danielk197753c0f742005-03-29 03:10:59 +0000667 if( OMIT_TEMPDB && i==1 ) continue;
668
drh80242052004-06-09 00:48:12 +0000669 sqlite3CodeVerifySchema(pParse, i);
670
drhed717fe2003-06-15 23:42:24 +0000671 /* Do an integrity check of the B-Tree
672 */
danielk1977da184232006-01-05 11:34:32 +0000673 pTbls = &db->aDb[i].pSchema->tblHash;
674 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +0000675 Table *pTab = sqliteHashData(x);
676 Index *pIdx;
677 sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0);
678 cnt++;
679 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
680 sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0);
681 cnt++;
682 }
683 }
danielk19772b444852004-06-29 07:45:33 +0000684 assert( cnt>0 );
drh79069752004-05-22 21:30:40 +0000685 sqlite3VdbeAddOp(v, OP_IntegrityCk, cnt, i);
686 sqlite3VdbeAddOp(v, OP_Dup, 0, 1);
danielk19770f69c1e2004-05-29 11:24:50 +0000687 addr = sqlite3VdbeOp3(v, OP_String8, 0, 0, "ok", P3_STATIC);
drh23cc57f2005-10-06 13:59:26 +0000688 sqlite3VdbeAddOp(v, OP_Eq, 0, addr+7);
danielk19770f69c1e2004-05-29 11:24:50 +0000689 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drh79069752004-05-22 21:30:40 +0000690 sqlite3MPrintf("*** in database %s ***\n", db->aDb[i].zName),
691 P3_DYNAMIC);
692 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drh855eb1c2004-08-31 13:45:11 +0000693 sqlite3VdbeAddOp(v, OP_Concat, 0, 1);
drh79069752004-05-22 21:30:40 +0000694 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
drh15007a92006-01-08 18:10:17 +0000695 sqlite3VdbeAddOp(v, OP_MemIncr, 1, 0);
drhed717fe2003-06-15 23:42:24 +0000696
697 /* Make sure all the indices are constructed correctly.
698 */
danielk19774adee202004-05-08 08:23:19 +0000699 sqlite3CodeVerifySchema(pParse, i);
danielk1977da184232006-01-05 11:34:32 +0000700 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +0000701 Table *pTab = sqliteHashData(x);
702 Index *pIdx;
703 int loopTop;
704
705 if( pTab->pIndex==0 ) continue;
drh290c1942004-08-21 17:54:45 +0000706 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drhd654be82005-09-20 17:42:23 +0000707 sqlite3VdbeAddOp(v, OP_MemInt, 0, 1);
danielk19774adee202004-05-08 08:23:19 +0000708 loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0);
drh15007a92006-01-08 18:10:17 +0000709 sqlite3VdbeAddOp(v, OP_MemIncr, 1, 1);
drhed717fe2003-06-15 23:42:24 +0000710 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +0000711 int jmp2;
drh57196282004-10-06 15:41:16 +0000712 static const VdbeOpList idxErr[] = {
drh15007a92006-01-08 18:10:17 +0000713 { OP_MemIncr, 1, 0, 0},
drh290c1942004-08-21 17:54:45 +0000714 { OP_String8, 0, 0, "rowid "},
drhf0863fe2005-06-12 21:35:51 +0000715 { OP_Rowid, 1, 0, 0},
drh290c1942004-08-21 17:54:45 +0000716 { OP_String8, 0, 0, " missing from index "},
717 { OP_String8, 0, 0, 0}, /* 4 */
drh855eb1c2004-08-31 13:45:11 +0000718 { OP_Concat, 2, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000719 { OP_Callback, 1, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000720 };
drh51846b52004-05-28 16:00:21 +0000721 sqlite3GenerateIndexKey(v, pIdx, 1);
danielk19774adee202004-05-08 08:23:19 +0000722 jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0);
723 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
724 sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC);
drhd654be82005-09-20 17:42:23 +0000725 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +0000726 }
danielk19774adee202004-05-08 08:23:19 +0000727 sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +0000728 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +0000729 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +0000730 static const VdbeOpList cntIdx[] = {
drhd654be82005-09-20 17:42:23 +0000731 { OP_MemInt, 0, 2, 0},
732 { OP_Rewind, 0, 0, 0}, /* 1 */
drh15007a92006-01-08 18:10:17 +0000733 { OP_MemIncr, 1, 2, 0},
drhd654be82005-09-20 17:42:23 +0000734 { OP_Next, 0, 0, 0}, /* 3 */
drhed717fe2003-06-15 23:42:24 +0000735 { OP_MemLoad, 1, 0, 0},
736 { OP_MemLoad, 2, 0, 0},
drhd654be82005-09-20 17:42:23 +0000737 { OP_Eq, 0, 0, 0}, /* 6 */
drh15007a92006-01-08 18:10:17 +0000738 { OP_MemIncr, 1, 0, 0},
drh290c1942004-08-21 17:54:45 +0000739 { OP_String8, 0, 0, "wrong # of entries in index "},
drhd654be82005-09-20 17:42:23 +0000740 { OP_String8, 0, 0, 0}, /* 9 */
drh855eb1c2004-08-31 13:45:11 +0000741 { OP_Concat, 0, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000742 { OP_Callback, 1, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000743 };
744 if( pIdx->tnum==0 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000745 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +0000746 sqlite3VdbeChangeP1(v, addr+1, j+2);
747 sqlite3VdbeChangeP2(v, addr+1, addr+4);
748 sqlite3VdbeChangeP1(v, addr+3, j+2);
749 sqlite3VdbeChangeP2(v, addr+3, addr+2);
750 sqlite3VdbeJumpHere(v, addr+6);
751 sqlite3VdbeChangeP3(v, addr+9, pIdx->zName, P3_STATIC);
drhed717fe2003-06-15 23:42:24 +0000752 }
753 }
754 }
danielk19774adee202004-05-08 08:23:19 +0000755 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drhd654be82005-09-20 17:42:23 +0000756 sqlite3VdbeJumpHere(v, addr+2);
drhc11d4f92003-04-06 21:08:24 +0000757 }else
drhb7f91642004-10-31 02:22:47 +0000758#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
759
drh13d70422004-11-13 15:59:14 +0000760#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +0000761 /*
762 ** PRAGMA encoding
763 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
764 **
765 ** In it's first form, this pragma returns the encoding of the main
766 ** database. If the database is not initialized, it is initialized now.
767 **
768 ** The second form of this pragma is a no-op if the main database file
769 ** has not already been initialized. In this case it sets the default
770 ** encoding that will be used for the main database file if a new file
771 ** is created. If an existing main database file is opened, then the
772 ** default text encoding for the existing database is used.
773 **
774 ** In all cases new databases created using the ATTACH command are
775 ** created to use the same default text encoding as the main database. If
776 ** the main database has not been initialized and/or created when ATTACH
777 ** is executed, this is done before the ATTACH operation.
778 **
779 ** In the second form this pragma sets the text encoding to be used in
780 ** new database files created using this database handle. It is only
781 ** useful if invoked immediately after the main database i
782 */
783 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh57196282004-10-06 15:41:16 +0000784 static struct EncName {
danielk19778e227872004-06-07 07:52:17 +0000785 char *zName;
786 u8 enc;
787 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +0000788 { "UTF-8", SQLITE_UTF8 },
789 { "UTF8", SQLITE_UTF8 },
790 { "UTF-16le", SQLITE_UTF16LE },
791 { "UTF16le", SQLITE_UTF16LE },
792 { "UTF-16be", SQLITE_UTF16BE },
793 { "UTF16be", SQLITE_UTF16BE },
794 { "UTF-16", 0 /* Filled in at run-time */ },
795 { "UTF16", 0 /* Filled in at run-time */ },
danielk19778e227872004-06-07 07:52:17 +0000796 { 0, 0 }
797 };
798 struct EncName *pEnc;
drh998da3a2004-06-19 15:22:56 +0000799 encnames[6].enc = encnames[7].enc = SQLITE_UTF16NATIVE;
danielk197791cf71b2004-06-26 06:37:06 +0000800 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +0000801 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +0000802 sqlite3VdbeSetNumCols(v, 1);
803 sqlite3VdbeSetColName(v, 0, "encoding", P3_STATIC);
804 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
805 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
danielk197714db2662006-01-09 16:12:04 +0000806 if( pEnc->enc==ENC(pParse->db) ){
danielk19778e227872004-06-07 07:52:17 +0000807 sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC);
808 break;
809 }
810 }
811 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
812 }else{ /* "PRAGMA encoding = XXX" */
813 /* Only change the value of sqlite.enc if the database handle is not
814 ** initialized. If the main database exists, the new sqlite.enc value
815 ** will be overwritten when the schema is next loaded. If it does not
816 ** already exists, it will be created to use the new encoding value.
817 */
danielk1977b82e7ed2006-01-11 14:09:31 +0000818 if(
819 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
820 DbHasProperty(db, 0, DB_Empty)
821 ){
danielk19778e227872004-06-07 07:52:17 +0000822 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
823 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
danielk197714db2662006-01-09 16:12:04 +0000824 ENC(pParse->db) = pEnc->enc;
danielk19778e227872004-06-07 07:52:17 +0000825 break;
826 }
827 }
828 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +0000829 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +0000830 }
831 }
832 }
833 }else
drh13d70422004-11-13 15:59:14 +0000834#endif /* SQLITE_OMIT_UTF16 */
835
836#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +0000837 /*
danielk1977b92b70b2004-11-12 16:11:59 +0000838 ** PRAGMA [database.]schema_version
839 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +0000840 **
danielk1977b92b70b2004-11-12 16:11:59 +0000841 ** PRAGMA [database.]user_version
842 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +0000843 **
danielk1977b92b70b2004-11-12 16:11:59 +0000844 ** The pragma's schema_version and user_version are used to set or get
845 ** the value of the schema-version and user-version, respectively. Both
846 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +0000847 ** stored in the database header.
848 **
849 ** The schema-cookie is usually only manipulated internally by SQLite. It
850 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +0000851 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +0000852 ** SQLite each time a query is executed to ensure that the internal cache
853 ** of the schema used when compiling the SQL query matches the schema of
854 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +0000855 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
856 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +0000857 ** crashes or database corruption. Use with caution!
858 **
danielk1977b92b70b2004-11-12 16:11:59 +0000859 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +0000860 ** applications for any purpose.
861 */
danielk1977b92b70b2004-11-12 16:11:59 +0000862 if( sqlite3StrICmp(zLeft, "schema_version")==0 ||
863 sqlite3StrICmp(zLeft, "user_version")==0 ){
danielk1977dae24952004-11-11 05:10:43 +0000864
865 int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
866 if( zLeft[0]=='s' || zLeft[0]=='S' ){
867 iCookie = 0;
868 }else{
869 iCookie = 5;
870 }
871
872 if( zRight ){
873 /* Write the specified cookie value */
874 static const VdbeOpList setCookie[] = {
875 { OP_Transaction, 0, 1, 0}, /* 0 */
876 { OP_Integer, 0, 0, 0}, /* 1 */
877 { OP_SetCookie, 0, 0, 0}, /* 2 */
878 };
879 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
880 sqlite3VdbeChangeP1(v, addr, iDb);
881 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
882 sqlite3VdbeChangeP1(v, addr+2, iDb);
883 sqlite3VdbeChangeP2(v, addr+2, iCookie);
884 }else{
885 /* Read the specified cookie value */
886 static const VdbeOpList readCookie[] = {
887 { OP_ReadCookie, 0, 0, 0}, /* 0 */
888 { OP_Callback, 1, 0, 0}
889 };
890 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
891 sqlite3VdbeChangeP1(v, addr, iDb);
892 sqlite3VdbeChangeP2(v, addr, iCookie);
893 sqlite3VdbeSetNumCols(v, 1);
894 }
895 }
drh13d70422004-11-13 15:59:14 +0000896#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000897
dougcurrie81c95ef2004-06-18 23:21:47 +0000898#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +0000899 /*
900 ** Report the current state of file logs for all databases
901 */
902 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +0000903 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +0000904 "unlocked", "shared", "reserved", "pending", "exclusive"
905 };
906 int i;
907 Vdbe *v = sqlite3GetVdbe(pParse);
908 sqlite3VdbeSetNumCols(v, 2);
909 sqlite3VdbeSetColName(v, 0, "database", P3_STATIC);
910 sqlite3VdbeSetColName(v, 1, "status", P3_STATIC);
911 for(i=0; i<db->nDb; i++){
912 Btree *pBt;
913 Pager *pPager;
914 if( db->aDb[i].zName==0 ) continue;
drha67367e2005-09-17 16:36:55 +0000915 sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, P3_STATIC);
drh89ac8c12004-06-09 14:17:20 +0000916 pBt = db->aDb[i].pBt;
917 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drha67367e2005-09-17 16:36:55 +0000918 sqlite3VdbeOp3(v, OP_String8, 0, 0, "closed", P3_STATIC);
drh89ac8c12004-06-09 14:17:20 +0000919 }else{
920 int j = sqlite3pager_lockstate(pPager);
drha67367e2005-09-17 16:36:55 +0000921 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drh89ac8c12004-06-09 14:17:20 +0000922 (j>=0 && j<=4) ? azLockName[j] : "unknown", P3_STATIC);
923 }
924 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
925 }
926 }else
927#endif
928
drh89dec812005-04-28 19:03:37 +0000929#ifdef SQLITE_SSE
930 /*
931 ** Check to see if the sqlite_statements table exists. Create it
932 ** if it does not.
933 */
934 if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
danielk19773a3f38e2005-05-22 06:49:56 +0000935 extern int sqlite3CreateStatementsTable(Parse*);
936 sqlite3CreateStatementsTable(pParse);
drh89dec812005-04-28 19:03:37 +0000937 }else
938#endif
939
drh3c4f2a42005-12-08 18:12:56 +0000940#if SQLITE_HAS_CODEC
941 if( sqlite3StrICmp(zLeft, "key")==0 ){
942 sqlite3_key(db, zRight, strlen(zRight));
943 }else
944#endif
945
drhc11d4f92003-04-06 21:08:24 +0000946 {}
danielk1977a21c6b62005-01-24 10:25:59 +0000947
948 if( v ){
949 /* Code an OP_Expire at the end of each PRAGMA program to cause
950 ** the VDBE implementing the pragma to expire. Most (all?) pragmas
951 ** are only valid for a single execution.
952 */
953 sqlite3VdbeAddOp(v, OP_Expire, 1, 0);
954 }
danielk1977e0048402004-06-15 16:51:01 +0000955pragma_out:
drhc11d4f92003-04-06 21:08:24 +0000956 sqliteFree(zLeft);
957 sqliteFree(zRight);
958}
drh13d70422004-11-13 15:59:14 +0000959
drh0ccebe72005-06-07 22:22:50 +0000960#endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */