blob: a1aab2e592ef22598018e0c73ac01df0ba498866 [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**
drhf0863fe2005-06-12 21:35:51 +000014** $Id: pragma.c,v 1.95 2005/06/12 21:35:52 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*/
drh722e95a2004-10-25 20:33:44 +000039static int getSafetyLevel(const u8 *z){
40 /* 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*/
61static int getBoolean(const u8 *z){
62 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 },
drh722e95a2004-10-25 20:33:44 +0000154 /* The following is VERY experimental */
drhf4040832004-10-22 20:29:21 +0000155 { "writable_schema", SQLITE_WriteSchema },
drh7bec5052005-02-06 02:45:41 +0000156 { "omit_readlock", SQLITE_NoReadlock },
drh87223182004-02-21 14:00:29 +0000157 };
158 int i;
drh722e95a2004-10-25 20:33:44 +0000159 const struct sPragmaType *p;
160 for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
161 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000162 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000163 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000164 v = sqlite3GetVdbe(pParse);
165 if( v ){
166 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000167 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000168 }else{
danielk1977a21c6b62005-01-24 10:25:59 +0000169 if( getBoolean(zRight) ){
170 db->flags |= p->mask;
171 }else{
172 db->flags &= ~p->mask;
173 }
drhd89bd002005-01-22 03:03:54 +0000174 }
danielk1977a21c6b62005-01-24 10:25:59 +0000175 /* If one of these pragmas is executed, any prepared statements
176 ** need to be recompiled.
177 */
178 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
drh87223182004-02-21 14:00:29 +0000179 }
180 return 1;
181 }
182 }
183 return 0;
184}
drhbf216272005-02-26 18:10:44 +0000185#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000186
187/*
drhc11d4f92003-04-06 21:08:24 +0000188** Process a pragma statement.
189**
190** Pragmas are of this form:
191**
danielk197791cf71b2004-06-26 06:37:06 +0000192** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000193**
194** The identifier might also be a string. The value is a string, and
195** identifier, or a number. If minusFlag is true, then the value is
196** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000197**
198** If the left side is "database.id" then pId1 is the database name
199** and pId2 is the id. If the left side is just "id" then pId1 is the
200** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000201*/
danielk197791cf71b2004-06-26 06:37:06 +0000202void sqlite3Pragma(
203 Parse *pParse,
204 Token *pId1, /* First part of [database.]id field */
205 Token *pId2, /* Second part of [database.]id field, or NULL */
206 Token *pValue, /* Token for <value>, or NULL */
207 int minusFlag /* True if a '-' sign preceded <value> */
208){
209 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
210 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
211 const char *zDb = 0; /* The database name */
212 Token *pId; /* Pointer to <id> token */
213 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000214 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000215 Db *pDb;
danielk19774adee202004-05-08 08:23:19 +0000216 Vdbe *v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000217 if( v==0 ) return;
218
danielk197791cf71b2004-06-26 06:37:06 +0000219 /* Interpret the [database.] part of the pragma statement. iDb is the
220 ** index of the database this pragma is being applied to in db.aDb[]. */
221 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
222 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000223 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000224
225 zLeft = sqlite3NameFromToken(pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000226 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000227 if( minusFlag ){
drhae29ffb2004-09-25 14:39:18 +0000228 zRight = sqlite3MPrintf("-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000229 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000230 zRight = sqlite3NameFromToken(pValue);
drhc11d4f92003-04-06 21:08:24 +0000231 }
danielk197791cf71b2004-06-26 06:37:06 +0000232
drh90f5ecb2004-07-22 01:19:35 +0000233 zDb = ((iDb>0)?pDb->zName:0);
danielk197791cf71b2004-06-26 06:37:06 +0000234 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000235 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000236 }
237
drh13d70422004-11-13 15:59:14 +0000238#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000239 /*
drh90f5ecb2004-07-22 01:19:35 +0000240 ** PRAGMA [database.]default_cache_size
241 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000242 **
243 ** The first form reports the current persistent setting for the
244 ** page cache size. The value returned is the maximum number of
245 ** pages in the page cache. The second form sets both the current
246 ** page cache size value and the persistent page cache size value
247 ** stored in the database file.
248 **
249 ** The default cache size is stored in meta-value 2 of page 1 of the
250 ** database file. The cache size is actually the absolute value of
251 ** this memory location. The sign of meta-value 2 determines the
252 ** synchronous setting. A negative value means synchronous is off
253 ** and a positive value means synchronous is on.
254 */
danielk19774adee202004-05-08 08:23:19 +0000255 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000256 static const VdbeOpList getCacheSize[] = {
danielk197791cf71b2004-06-26 06:37:06 +0000257 { OP_ReadCookie, 0, 2, 0}, /* 0 */
drhc11d4f92003-04-06 21:08:24 +0000258 { OP_AbsValue, 0, 0, 0},
259 { OP_Dup, 0, 0, 0},
260 { OP_Integer, 0, 0, 0},
261 { OP_Ne, 0, 6, 0},
drh905793e2004-02-21 13:31:09 +0000262 { OP_Integer, 0, 0, 0}, /* 5 */
drhc11d4f92003-04-06 21:08:24 +0000263 { OP_Callback, 1, 0, 0},
264 };
drh905793e2004-02-21 13:31:09 +0000265 int addr;
danielk19778a414492004-06-29 08:59:35 +0000266 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000267 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000268 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000269 sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000270 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000271 sqlite3VdbeChangeP1(v, addr, iDb);
danielk19774adee202004-05-08 08:23:19 +0000272 sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES);
drhc11d4f92003-04-06 21:08:24 +0000273 }else{
drhc11d4f92003-04-06 21:08:24 +0000274 int size = atoi(zRight);
275 if( size<0 ) size = -size;
danielk197791cf71b2004-06-26 06:37:06 +0000276 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk19774adee202004-05-08 08:23:19 +0000277 sqlite3VdbeAddOp(v, OP_Integer, size, 0);
danielk197791cf71b2004-06-26 06:37:06 +0000278 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2);
danielk19774adee202004-05-08 08:23:19 +0000279 addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
280 sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3);
281 sqlite3VdbeAddOp(v, OP_Negative, 0, 0);
danielk197791cf71b2004-06-26 06:37:06 +0000282 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2);
drh90f5ecb2004-07-22 01:19:35 +0000283 pDb->cache_size = size;
284 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000285 }
286 }else
287
288 /*
drh90f5ecb2004-07-22 01:19:35 +0000289 ** PRAGMA [database.]page_size
290 ** PRAGMA [database.]page_size=N
291 **
292 ** The first form reports the current setting for the
293 ** database page size in bytes. The second form sets the
294 ** database page size value. The value can only be set if
295 ** the database has not yet been created.
296 */
297 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
298 Btree *pBt = pDb->pBt;
299 if( !zRight ){
300 int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000301 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000302 }else{
danielk197728129562005-01-11 10:25:06 +0000303 sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1);
drh90f5ecb2004-07-22 01:19:35 +0000304 }
305 }else
drh13d70422004-11-13 15:59:14 +0000306#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000307
308 /*
danielk1977951af802004-11-05 15:45:09 +0000309 ** PRAGMA [database.]auto_vacuum
310 ** PRAGMA [database.]auto_vacuum=N
311 **
312 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
313 */
314#ifndef SQLITE_OMIT_AUTOVACUUM
315 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
316 Btree *pBt = pDb->pBt;
317 if( !zRight ){
318 int auto_vacuum =
319 pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
320 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
321 }else{
322 sqlite3BtreeSetAutoVacuum(pBt, getBoolean(zRight));
323 }
324 }else
325#endif
326
drh13d70422004-11-13 15:59:14 +0000327#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000328 /*
drh90f5ecb2004-07-22 01:19:35 +0000329 ** PRAGMA [database.]cache_size
330 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000331 **
332 ** The first form reports the current local setting for the
333 ** page cache size. The local setting can be different from
334 ** the persistent cache size value that is stored in the database
335 ** file itself. The value returned is the maximum number of
336 ** pages in the page cache. The second form sets the local
337 ** page cache size value. It does not change the persistent
338 ** cache size stored on the disk so the cache size will revert
339 ** to its default value when the database is closed and reopened.
340 ** N should be a positive integer.
341 */
danielk19774adee202004-05-08 08:23:19 +0000342 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000343 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000344 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000345 returnSingleInt(pParse, "cache_size", pDb->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000346 }else{
347 int size = atoi(zRight);
348 if( size<0 ) size = -size;
drh90f5ecb2004-07-22 01:19:35 +0000349 pDb->cache_size = size;
350 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000351 }
352 }else
353
354 /*
drh90f5ecb2004-07-22 01:19:35 +0000355 ** PRAGMA temp_store
356 ** PRAGMA temp_store = "default"|"memory"|"file"
357 **
358 ** Return or set the local value of the temp_store flag. Changing
359 ** the local value does not make changes to the disk file and the default
360 ** value will be restored the next time the database is opened.
361 **
362 ** Note that it is possible for the library compile-time options to
363 ** override this setting
364 */
365 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
366 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000367 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000368 }else{
369 changeTempStorage(pParse, zRight);
370 }
371 }else
372
373 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000374 ** PRAGMA temp_store_directory
375 ** PRAGMA temp_store_directory = ""|"directory_name"
376 **
377 ** Return or set the local value of the temp_store_directory flag. Changing
378 ** the value sets a specific directory to be used for temporary files.
379 ** Setting to a null string reverts to the default temporary directory search.
380 ** If temporary directory is changed, then invalidateTempStorage.
381 **
382 */
383 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
384 if( !zRight ){
385 if( sqlite3_temp_directory ){
386 sqlite3VdbeSetNumCols(v, 1);
387 sqlite3VdbeSetColName(v, 0, "temp_store_directory", P3_STATIC);
388 sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0);
389 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
390 }
391 }else{
drh268283b2005-01-08 15:44:25 +0000392 if( zRight[0] && !sqlite3OsIsDirWritable(zRight) ){
393 sqlite3ErrorMsg(pParse, "not a writable directory");
394 goto pragma_out;
395 }
396 if( TEMP_STORE==0
397 || (TEMP_STORE==1 && db->temp_store<=1)
398 || (TEMP_STORE==2 && db->temp_store==1)
399 ){
400 invalidateTempStorage(pParse);
401 }
402 sqliteFree(sqlite3_temp_directory);
403 if( zRight[0] ){
404 sqlite3_temp_directory = zRight;
405 zRight = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000406 }else{
drh268283b2005-01-08 15:44:25 +0000407 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000408 }
409 }
410 }else
411
412 /*
drh90f5ecb2004-07-22 01:19:35 +0000413 ** PRAGMA [database.]synchronous
414 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000415 **
416 ** Return or set the local value of the synchronous flag. Changing
417 ** the local value does not make changes to the disk file and the
418 ** default value will be restored the next time the database is
419 ** opened.
420 */
danielk19774adee202004-05-08 08:23:19 +0000421 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000422 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000423 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000424 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000425 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000426 if( !db->autoCommit ){
427 sqlite3ErrorMsg(pParse,
428 "Safety level may not be changed inside a transaction");
429 }else{
drh90f5ecb2004-07-22 01:19:35 +0000430 pDb->safety_level = getSafetyLevel(zRight)+1;
431 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level);
danielk197791cf71b2004-06-26 06:37:06 +0000432 }
drhc11d4f92003-04-06 21:08:24 +0000433 }
434 }else
drh13d70422004-11-13 15:59:14 +0000435#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000436
drhbf216272005-02-26 18:10:44 +0000437#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000438 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000439 /* The flagPragma() subroutine also generates any necessary code
440 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000441 }else
drhbf216272005-02-26 18:10:44 +0000442#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000443
drh13d70422004-11-13 15:59:14 +0000444#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000445 /*
446 ** PRAGMA table_info(<table>)
447 **
448 ** Return a single row for each column of the named table. The columns of
449 ** the returned data set are:
450 **
451 ** cid: Column id (numbered from left to right, starting at 0)
452 ** name: Column name
453 ** type: Column declaration type.
454 ** notnull: True if 'NOT NULL' is part of column declaration
455 ** dflt_value: The default value for the column, if any.
456 */
drh5260f7e2004-06-26 19:35:29 +0000457 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000458 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000459 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000460 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000461 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000462 int i;
danielk197722322fd2004-05-25 23:35:17 +0000463 sqlite3VdbeSetNumCols(v, 6);
danielk19773cf86062004-05-26 10:11:05 +0000464 sqlite3VdbeSetColName(v, 0, "cid", P3_STATIC);
465 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
466 sqlite3VdbeSetColName(v, 2, "type", P3_STATIC);
467 sqlite3VdbeSetColName(v, 3, "notnull", P3_STATIC);
468 sqlite3VdbeSetColName(v, 4, "dflt_value", P3_STATIC);
469 sqlite3VdbeSetColName(v, 5, "pk", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000470 sqlite3ViewGetColumnNames(pParse, pTab);
drhc11d4f92003-04-06 21:08:24 +0000471 for(i=0; i<pTab->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000472 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000473 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[i].zName, 0);
474 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drh701a0ae2004-02-22 20:05:00 +0000475 pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", 0);
danielk19774adee202004-05-08 08:23:19 +0000476 sqlite3VdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
danielk19777977a172004-11-09 12:44:37 +0000477 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
danielk19774adee202004-05-08 08:23:19 +0000478 sqlite3VdbeAddOp(v, OP_Integer, pTab->aCol[i].isPrimKey, 0);
479 sqlite3VdbeAddOp(v, OP_Callback, 6, 0);
drhc11d4f92003-04-06 21:08:24 +0000480 }
481 }
482 }else
483
drh5260f7e2004-06-26 19:35:29 +0000484 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000485 Index *pIdx;
486 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000487 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000488 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000489 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000490 int i;
491 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000492 sqlite3VdbeSetNumCols(v, 3);
danielk19773cf86062004-05-26 10:11:05 +0000493 sqlite3VdbeSetColName(v, 0, "seqno", P3_STATIC);
494 sqlite3VdbeSetColName(v, 1, "cid", P3_STATIC);
495 sqlite3VdbeSetColName(v, 2, "name", P3_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000496 for(i=0; i<pIdx->nColumn; i++){
497 int cnum = pIdx->aiColumn[i];
danielk19774adee202004-05-08 08:23:19 +0000498 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
499 sqlite3VdbeAddOp(v, OP_Integer, cnum, 0);
drhc11d4f92003-04-06 21:08:24 +0000500 assert( pTab->nCol>cnum );
danielk19770f69c1e2004-05-29 11:24:50 +0000501 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0);
danielk19774adee202004-05-08 08:23:19 +0000502 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
drhc11d4f92003-04-06 21:08:24 +0000503 }
504 }
505 }else
506
drh5260f7e2004-06-26 19:35:29 +0000507 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000508 Index *pIdx;
509 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000510 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000511 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000512 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000513 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000514 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000515 if( pIdx ){
516 int i = 0;
517 sqlite3VdbeSetNumCols(v, 3);
518 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
519 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
520 sqlite3VdbeSetColName(v, 2, "unique", P3_STATIC);
521 while(pIdx){
522 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
523 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0);
524 sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
525 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
526 ++i;
527 pIdx = pIdx->pNext;
528 }
drhc11d4f92003-04-06 21:08:24 +0000529 }
530 }
531 }else
532
drh13d70422004-11-13 15:59:14 +0000533 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
534 int i;
535 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
536 sqlite3VdbeSetNumCols(v, 3);
537 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
538 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
539 sqlite3VdbeSetColName(v, 2, "file", P3_STATIC);
540 for(i=0; i<db->nDb; i++){
541 if( db->aDb[i].pBt==0 ) continue;
542 assert( db->aDb[i].zName!=0 );
543 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
544 sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0);
545 sqlite3VdbeOp3(v, OP_String8, 0, 0,
546 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
547 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
548 }
549 }else
danielk197748af65a2005-02-09 03:20:37 +0000550
551 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
552 int i = 0;
553 HashElem *p;
554 sqlite3VdbeSetNumCols(v, 2);
555 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
556 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
557 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
558 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
559 sqlite3VdbeAddOp(v, OP_Integer, i++, 0);
560 sqlite3VdbeOp3(v, OP_String8, 0, 0, pColl->zName, 0);
561 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
562 }
563 }else
drh13d70422004-11-13 15:59:14 +0000564#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
565
drhb7f91642004-10-31 02:22:47 +0000566#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000567 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000568 FKey *pFK;
569 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000570 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000571 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +0000572 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000573 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +0000574 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +0000575 if( pFK ){
576 int i = 0;
577 sqlite3VdbeSetNumCols(v, 5);
578 sqlite3VdbeSetColName(v, 0, "id", P3_STATIC);
579 sqlite3VdbeSetColName(v, 1, "seq", P3_STATIC);
580 sqlite3VdbeSetColName(v, 2, "table", P3_STATIC);
581 sqlite3VdbeSetColName(v, 3, "from", P3_STATIC);
582 sqlite3VdbeSetColName(v, 4, "to", P3_STATIC);
583 while(pFK){
584 int j;
585 for(j=0; j<pFK->nCol; j++){
586 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
587 sqlite3VdbeAddOp(v, OP_Integer, j, 0);
588 sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0);
589 sqlite3VdbeOp3(v, OP_String8, 0, 0,
590 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
591 sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->aCol[j].zCol, 0);
592 sqlite3VdbeAddOp(v, OP_Callback, 5, 0);
593 }
594 ++i;
595 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +0000596 }
drh78100cc2003-08-23 22:40:53 +0000597 }
598 }
599 }else
drhb7f91642004-10-31 02:22:47 +0000600#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +0000601
drhc11d4f92003-04-06 21:08:24 +0000602#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +0000603 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
604 extern void sqlite3ParserTrace(FILE*, char *);
drhc11d4f92003-04-06 21:08:24 +0000605 if( getBoolean(zRight) ){
danielk19774adee202004-05-08 08:23:19 +0000606 sqlite3ParserTrace(stdout, "parser: ");
drhc11d4f92003-04-06 21:08:24 +0000607 }else{
danielk19774adee202004-05-08 08:23:19 +0000608 sqlite3ParserTrace(0, 0);
drhc11d4f92003-04-06 21:08:24 +0000609 }
610 }else
611#endif
612
drhb7f91642004-10-31 02:22:47 +0000613#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk19774adee202004-05-08 08:23:19 +0000614 if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){
drhed717fe2003-06-15 23:42:24 +0000615 int i, j, addr;
616
617 /* Code that initializes the integrity check program. Set the
drh4be295b2003-12-16 03:44:47 +0000618 ** error count 0
drhed717fe2003-06-15 23:42:24 +0000619 */
drh57196282004-10-06 15:41:16 +0000620 static const VdbeOpList initCode[] = {
drh4be295b2003-12-16 03:44:47 +0000621 { OP_Integer, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000622 { OP_MemStore, 0, 1, 0},
drhed717fe2003-06-15 23:42:24 +0000623 };
624
drhed717fe2003-06-15 23:42:24 +0000625 /* Code that appears at the end of the integrity check. If no error
626 ** messages have been generated, output OK. Otherwise output the
627 ** error message
628 */
drh57196282004-10-06 15:41:16 +0000629 static const VdbeOpList endCode[] = {
drhed717fe2003-06-15 23:42:24 +0000630 { OP_MemLoad, 0, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000631 { OP_Integer, 0, 0, 0},
632 { OP_Ne, 0, 0, 0}, /* 2 */
drh290c1942004-08-21 17:54:45 +0000633 { OP_String8, 0, 0, "ok"},
drhc11d4f92003-04-06 21:08:24 +0000634 { OP_Callback, 1, 0, 0},
635 };
drhed717fe2003-06-15 23:42:24 +0000636
637 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +0000638 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197722322fd2004-05-25 23:35:17 +0000639 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000640 sqlite3VdbeSetColName(v, 0, "integrity_check", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000641 sqlite3VdbeAddOpList(v, ArraySize(initCode), initCode);
drhed717fe2003-06-15 23:42:24 +0000642
643 /* Do an integrity check on each database file */
644 for(i=0; i<db->nDb; i++){
645 HashElem *x;
drh79069752004-05-22 21:30:40 +0000646 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +0000647
danielk197753c0f742005-03-29 03:10:59 +0000648 if( OMIT_TEMPDB && i==1 ) continue;
649
drh80242052004-06-09 00:48:12 +0000650 sqlite3CodeVerifySchema(pParse, i);
651
drhed717fe2003-06-15 23:42:24 +0000652 /* Do an integrity check of the B-Tree
653 */
drh79069752004-05-22 21:30:40 +0000654 for(x=sqliteHashFirst(&db->aDb[i].tblHash); x; x=sqliteHashNext(x)){
655 Table *pTab = sqliteHashData(x);
656 Index *pIdx;
657 sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0);
658 cnt++;
659 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk1977e0048402004-06-15 16:51:01 +0000660 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ) goto pragma_out;
drh79069752004-05-22 21:30:40 +0000661 sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0);
662 cnt++;
663 }
664 }
danielk19772b444852004-06-29 07:45:33 +0000665 assert( cnt>0 );
drh79069752004-05-22 21:30:40 +0000666 sqlite3VdbeAddOp(v, OP_IntegrityCk, cnt, i);
667 sqlite3VdbeAddOp(v, OP_Dup, 0, 1);
danielk19770f69c1e2004-05-29 11:24:50 +0000668 addr = sqlite3VdbeOp3(v, OP_String8, 0, 0, "ok", P3_STATIC);
drh79069752004-05-22 21:30:40 +0000669 sqlite3VdbeAddOp(v, OP_Eq, 0, addr+6);
danielk19770f69c1e2004-05-29 11:24:50 +0000670 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drh79069752004-05-22 21:30:40 +0000671 sqlite3MPrintf("*** in database %s ***\n", db->aDb[i].zName),
672 P3_DYNAMIC);
673 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drh855eb1c2004-08-31 13:45:11 +0000674 sqlite3VdbeAddOp(v, OP_Concat, 0, 1);
drh79069752004-05-22 21:30:40 +0000675 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
drhed717fe2003-06-15 23:42:24 +0000676
677 /* Make sure all the indices are constructed correctly.
678 */
danielk19774adee202004-05-08 08:23:19 +0000679 sqlite3CodeVerifySchema(pParse, i);
drhed717fe2003-06-15 23:42:24 +0000680 for(x=sqliteHashFirst(&db->aDb[i].tblHash); x; x=sqliteHashNext(x)){
681 Table *pTab = sqliteHashData(x);
682 Index *pIdx;
683 int loopTop;
684
685 if( pTab->pIndex==0 ) continue;
drh290c1942004-08-21 17:54:45 +0000686 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
danielk19774adee202004-05-08 08:23:19 +0000687 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
688 sqlite3VdbeAddOp(v, OP_MemStore, 1, 1);
689 loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0);
690 sqlite3VdbeAddOp(v, OP_MemIncr, 1, 0);
drhed717fe2003-06-15 23:42:24 +0000691 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +0000692 int jmp2;
drh57196282004-10-06 15:41:16 +0000693 static const VdbeOpList idxErr[] = {
drh4be295b2003-12-16 03:44:47 +0000694 { OP_MemIncr, 0, 0, 0},
drh290c1942004-08-21 17:54:45 +0000695 { OP_String8, 0, 0, "rowid "},
drhf0863fe2005-06-12 21:35:51 +0000696 { OP_Rowid, 1, 0, 0},
drh290c1942004-08-21 17:54:45 +0000697 { OP_String8, 0, 0, " missing from index "},
698 { OP_String8, 0, 0, 0}, /* 4 */
drh855eb1c2004-08-31 13:45:11 +0000699 { OP_Concat, 2, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000700 { OP_Callback, 1, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000701 };
drh51846b52004-05-28 16:00:21 +0000702 sqlite3GenerateIndexKey(v, pIdx, 1);
danielk19774adee202004-05-08 08:23:19 +0000703 jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0);
704 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
705 sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC);
706 sqlite3VdbeChangeP2(v, jmp2, sqlite3VdbeCurrentAddr(v));
drhed717fe2003-06-15 23:42:24 +0000707 }
danielk19774adee202004-05-08 08:23:19 +0000708 sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1);
709 sqlite3VdbeChangeP2(v, loopTop, sqlite3VdbeCurrentAddr(v));
drhed717fe2003-06-15 23:42:24 +0000710 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +0000711 static const VdbeOpList cntIdx[] = {
drhed717fe2003-06-15 23:42:24 +0000712 { OP_Integer, 0, 0, 0},
713 { OP_MemStore, 2, 1, 0},
714 { OP_Rewind, 0, 0, 0}, /* 2 */
715 { OP_MemIncr, 2, 0, 0},
716 { OP_Next, 0, 0, 0}, /* 4 */
717 { OP_MemLoad, 1, 0, 0},
718 { OP_MemLoad, 2, 0, 0},
719 { OP_Eq, 0, 0, 0}, /* 7 */
drh4be295b2003-12-16 03:44:47 +0000720 { OP_MemIncr, 0, 0, 0},
drh290c1942004-08-21 17:54:45 +0000721 { OP_String8, 0, 0, "wrong # of entries in index "},
722 { OP_String8, 0, 0, 0}, /* 10 */
drh855eb1c2004-08-31 13:45:11 +0000723 { OP_Concat, 0, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000724 { OP_Callback, 1, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000725 };
726 if( pIdx->tnum==0 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000727 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
728 sqlite3VdbeChangeP1(v, addr+2, j+2);
729 sqlite3VdbeChangeP2(v, addr+2, addr+5);
730 sqlite3VdbeChangeP1(v, addr+4, j+2);
731 sqlite3VdbeChangeP2(v, addr+4, addr+3);
732 sqlite3VdbeChangeP2(v, addr+7, addr+ArraySize(cntIdx));
733 sqlite3VdbeChangeP3(v, addr+10, pIdx->zName, P3_STATIC);
drhed717fe2003-06-15 23:42:24 +0000734 }
735 }
736 }
danielk19774adee202004-05-08 08:23:19 +0000737 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
738 sqlite3VdbeChangeP2(v, addr+2, addr+ArraySize(endCode));
drhc11d4f92003-04-06 21:08:24 +0000739 }else
drhb7f91642004-10-31 02:22:47 +0000740#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
741
drh13d70422004-11-13 15:59:14 +0000742#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +0000743 /*
744 ** PRAGMA encoding
745 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
746 **
747 ** In it's first form, this pragma returns the encoding of the main
748 ** database. If the database is not initialized, it is initialized now.
749 **
750 ** The second form of this pragma is a no-op if the main database file
751 ** has not already been initialized. In this case it sets the default
752 ** encoding that will be used for the main database file if a new file
753 ** is created. If an existing main database file is opened, then the
754 ** default text encoding for the existing database is used.
755 **
756 ** In all cases new databases created using the ATTACH command are
757 ** created to use the same default text encoding as the main database. If
758 ** the main database has not been initialized and/or created when ATTACH
759 ** is executed, this is done before the ATTACH operation.
760 **
761 ** In the second form this pragma sets the text encoding to be used in
762 ** new database files created using this database handle. It is only
763 ** useful if invoked immediately after the main database i
764 */
765 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh57196282004-10-06 15:41:16 +0000766 static struct EncName {
danielk19778e227872004-06-07 07:52:17 +0000767 char *zName;
768 u8 enc;
769 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +0000770 { "UTF-8", SQLITE_UTF8 },
771 { "UTF8", SQLITE_UTF8 },
772 { "UTF-16le", SQLITE_UTF16LE },
773 { "UTF16le", SQLITE_UTF16LE },
774 { "UTF-16be", SQLITE_UTF16BE },
775 { "UTF16be", SQLITE_UTF16BE },
776 { "UTF-16", 0 /* Filled in at run-time */ },
777 { "UTF16", 0 /* Filled in at run-time */ },
danielk19778e227872004-06-07 07:52:17 +0000778 { 0, 0 }
779 };
780 struct EncName *pEnc;
drh998da3a2004-06-19 15:22:56 +0000781 encnames[6].enc = encnames[7].enc = SQLITE_UTF16NATIVE;
danielk197791cf71b2004-06-26 06:37:06 +0000782 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +0000783 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +0000784 sqlite3VdbeSetNumCols(v, 1);
785 sqlite3VdbeSetColName(v, 0, "encoding", P3_STATIC);
786 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
787 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
788 if( pEnc->enc==pParse->db->enc ){
789 sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC);
790 break;
791 }
792 }
793 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
794 }else{ /* "PRAGMA encoding = XXX" */
795 /* Only change the value of sqlite.enc if the database handle is not
796 ** initialized. If the main database exists, the new sqlite.enc value
797 ** will be overwritten when the schema is next loaded. If it does not
798 ** already exists, it will be created to use the new encoding value.
799 */
800 if( !(pParse->db->flags&SQLITE_Initialized) ){
801 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
802 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
803 pParse->db->enc = pEnc->enc;
804 break;
805 }
806 }
807 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +0000808 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +0000809 }
810 }
811 }
812 }else
drh13d70422004-11-13 15:59:14 +0000813#endif /* SQLITE_OMIT_UTF16 */
814
815#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +0000816 /*
danielk1977b92b70b2004-11-12 16:11:59 +0000817 ** PRAGMA [database.]schema_version
818 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +0000819 **
danielk1977b92b70b2004-11-12 16:11:59 +0000820 ** PRAGMA [database.]user_version
821 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +0000822 **
danielk1977b92b70b2004-11-12 16:11:59 +0000823 ** The pragma's schema_version and user_version are used to set or get
824 ** the value of the schema-version and user-version, respectively. Both
825 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +0000826 ** stored in the database header.
827 **
828 ** The schema-cookie is usually only manipulated internally by SQLite. It
829 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +0000830 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +0000831 ** SQLite each time a query is executed to ensure that the internal cache
832 ** of the schema used when compiling the SQL query matches the schema of
833 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +0000834 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
835 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +0000836 ** crashes or database corruption. Use with caution!
837 **
danielk1977b92b70b2004-11-12 16:11:59 +0000838 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +0000839 ** applications for any purpose.
840 */
danielk1977b92b70b2004-11-12 16:11:59 +0000841 if( sqlite3StrICmp(zLeft, "schema_version")==0 ||
842 sqlite3StrICmp(zLeft, "user_version")==0 ){
danielk1977dae24952004-11-11 05:10:43 +0000843
844 int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
845 if( zLeft[0]=='s' || zLeft[0]=='S' ){
846 iCookie = 0;
847 }else{
848 iCookie = 5;
849 }
850
851 if( zRight ){
852 /* Write the specified cookie value */
853 static const VdbeOpList setCookie[] = {
854 { OP_Transaction, 0, 1, 0}, /* 0 */
855 { OP_Integer, 0, 0, 0}, /* 1 */
856 { OP_SetCookie, 0, 0, 0}, /* 2 */
857 };
858 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
859 sqlite3VdbeChangeP1(v, addr, iDb);
860 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
861 sqlite3VdbeChangeP1(v, addr+2, iDb);
862 sqlite3VdbeChangeP2(v, addr+2, iCookie);
863 }else{
864 /* Read the specified cookie value */
865 static const VdbeOpList readCookie[] = {
866 { OP_ReadCookie, 0, 0, 0}, /* 0 */
867 { OP_Callback, 1, 0, 0}
868 };
869 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
870 sqlite3VdbeChangeP1(v, addr, iDb);
871 sqlite3VdbeChangeP2(v, addr, iCookie);
872 sqlite3VdbeSetNumCols(v, 1);
873 }
874 }
drh13d70422004-11-13 15:59:14 +0000875#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000876
dougcurrie81c95ef2004-06-18 23:21:47 +0000877#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +0000878 /*
879 ** Report the current state of file logs for all databases
880 */
881 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +0000882 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +0000883 "unlocked", "shared", "reserved", "pending", "exclusive"
884 };
885 int i;
886 Vdbe *v = sqlite3GetVdbe(pParse);
887 sqlite3VdbeSetNumCols(v, 2);
888 sqlite3VdbeSetColName(v, 0, "database", P3_STATIC);
889 sqlite3VdbeSetColName(v, 1, "status", P3_STATIC);
890 for(i=0; i<db->nDb; i++){
891 Btree *pBt;
892 Pager *pPager;
893 if( db->aDb[i].zName==0 ) continue;
894 sqlite3VdbeOp3(v, OP_String, 0, 0, db->aDb[i].zName, P3_STATIC);
895 pBt = db->aDb[i].pBt;
896 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
897 sqlite3VdbeOp3(v, OP_String, 0, 0, "closed", P3_STATIC);
898 }else{
899 int j = sqlite3pager_lockstate(pPager);
900 sqlite3VdbeOp3(v, OP_String, 0, 0,
901 (j>=0 && j<=4) ? azLockName[j] : "unknown", P3_STATIC);
902 }
903 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
904 }
905 }else
906#endif
907
drh89dec812005-04-28 19:03:37 +0000908#ifdef SQLITE_SSE
909 /*
910 ** Check to see if the sqlite_statements table exists. Create it
911 ** if it does not.
912 */
913 if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
danielk19773a3f38e2005-05-22 06:49:56 +0000914 extern int sqlite3CreateStatementsTable(Parse*);
915 sqlite3CreateStatementsTable(pParse);
drh89dec812005-04-28 19:03:37 +0000916 }else
917#endif
918
drhc11d4f92003-04-06 21:08:24 +0000919 {}
danielk1977a21c6b62005-01-24 10:25:59 +0000920
921 if( v ){
922 /* Code an OP_Expire at the end of each PRAGMA program to cause
923 ** the VDBE implementing the pragma to expire. Most (all?) pragmas
924 ** are only valid for a single execution.
925 */
926 sqlite3VdbeAddOp(v, OP_Expire, 1, 0);
927 }
danielk1977e0048402004-06-15 16:51:01 +0000928pragma_out:
drhc11d4f92003-04-06 21:08:24 +0000929 sqliteFree(zLeft);
930 sqliteFree(zRight);
931}
drh13d70422004-11-13 15:59:14 +0000932
drh0ccebe72005-06-07 22:22:50 +0000933#endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */