blob: eac505b936b0d9973c627b030f55258c8578bc86 [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**
danielk1977b84f96f2005-01-20 11:32:23 +000014** $Id: pragma.c,v 1.84 2005/01/20 11:32:24 danielk1977 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*/
22#ifndef SQLITE_OMIT_PRAGMA
23
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}
82
83/*
tpoindex9a09a3c2004-12-20 19:01:32 +000084** Invalidate temp storage, either when the temp storage is changed
85** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +000086*/
tpoindex9a09a3c2004-12-20 19:01:32 +000087static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +000088 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +000089 if( db->aDb[1].pBt!=0 ){
90 if( db->flags & SQLITE_InTrans ){
91 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
92 "from within a transaction");
93 return SQLITE_ERROR;
94 }
95 sqlite3BtreeClose(db->aDb[1].pBt);
96 db->aDb[1].pBt = 0;
97 sqlite3ResetInternalSchema(db, 0);
98 }
tpoindex9a09a3c2004-12-20 19:01:32 +000099 return SQLITE_OK;
100}
101
102/*
103** If the TEMP database is open, close it and mark the database schema
104** as needing reloading. This must be done when using the TEMP_STORE
105** or DEFAULT_TEMP_STORE pragmas.
106*/
107static int changeTempStorage(Parse *pParse, const char *zStorageType){
108 int ts = getTempStore(zStorageType);
109 sqlite3 *db = pParse->db;
110 if( db->temp_store==ts ) return SQLITE_OK;
111 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
112 return SQLITE_ERROR;
113 }
drh90f5ecb2004-07-22 01:19:35 +0000114 db->temp_store = ts;
115 return SQLITE_OK;
116}
danielk1977b84f96f2005-01-20 11:32:23 +0000117#endif
drh90f5ecb2004-07-22 01:19:35 +0000118
119/*
120** Generate code to return a single integer value.
121*/
drh5bb7ffe2004-09-02 15:14:00 +0000122static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
123 Vdbe *v = sqlite3GetVdbe(pParse);
drh90f5ecb2004-07-22 01:19:35 +0000124 sqlite3VdbeAddOp(v, OP_Integer, value, 0);
drh5bb7ffe2004-09-02 15:14:00 +0000125 if( pParse->explain==0 ){
126 sqlite3VdbeSetNumCols(v, 1);
127 sqlite3VdbeSetColName(v, 0, zLabel, P3_STATIC);
128 }
drh90f5ecb2004-07-22 01:19:35 +0000129 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
130}
131
132/*
drh87223182004-02-21 14:00:29 +0000133** Check to see if zRight and zLeft refer to a pragma that queries
134** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
135** Also, implement the pragma.
136*/
137static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000138 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000139 const char *zName; /* Name of the pragma */
140 int mask; /* Mask for the db->flags value */
141 } aPragma[] = {
142 { "vdbe_trace", SQLITE_VdbeTrace },
drh35d4c2f2004-06-10 01:30:59 +0000143 { "sql_trace", SQLITE_SqlTrace },
144 { "vdbe_listing", SQLITE_VdbeListing },
drh87223182004-02-21 14:00:29 +0000145 { "full_column_names", SQLITE_FullColNames },
146 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000147 { "count_changes", SQLITE_CountRows },
148 { "empty_result_callbacks", SQLITE_NullCallback },
drh722e95a2004-10-25 20:33:44 +0000149 /* The following is VERY experimental */
drhf4040832004-10-22 20:29:21 +0000150 { "writable_schema", SQLITE_WriteSchema },
drh87223182004-02-21 14:00:29 +0000151 };
152 int i;
drh722e95a2004-10-25 20:33:44 +0000153 const struct sPragmaType *p;
154 for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
155 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000156 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000157 Vdbe *v;
drh5260f7e2004-06-26 19:35:29 +0000158 if( zRight==0 ){
159 v = sqlite3GetVdbe(pParse);
160 if( v ){
drh722e95a2004-10-25 20:33:44 +0000161 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drh5260f7e2004-06-26 19:35:29 +0000162 }
drh87223182004-02-21 14:00:29 +0000163 }else if( getBoolean(zRight) ){
drh722e95a2004-10-25 20:33:44 +0000164 db->flags |= p->mask;
drh87223182004-02-21 14:00:29 +0000165 }else{
drh722e95a2004-10-25 20:33:44 +0000166 db->flags &= ~p->mask;
drh87223182004-02-21 14:00:29 +0000167 }
168 return 1;
169 }
170 }
171 return 0;
172}
173
174/*
drhc11d4f92003-04-06 21:08:24 +0000175** Process a pragma statement.
176**
177** Pragmas are of this form:
178**
danielk197791cf71b2004-06-26 06:37:06 +0000179** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000180**
181** The identifier might also be a string. The value is a string, and
182** identifier, or a number. If minusFlag is true, then the value is
183** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000184**
185** If the left side is "database.id" then pId1 is the database name
186** and pId2 is the id. If the left side is just "id" then pId1 is the
187** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000188*/
danielk197791cf71b2004-06-26 06:37:06 +0000189void sqlite3Pragma(
190 Parse *pParse,
191 Token *pId1, /* First part of [database.]id field */
192 Token *pId2, /* Second part of [database.]id field, or NULL */
193 Token *pValue, /* Token for <value>, or NULL */
194 int minusFlag /* True if a '-' sign preceded <value> */
195){
196 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
197 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
198 const char *zDb = 0; /* The database name */
199 Token *pId; /* Pointer to <id> token */
200 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000201 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000202 Db *pDb;
danielk19774adee202004-05-08 08:23:19 +0000203 Vdbe *v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000204 if( v==0 ) return;
205
danielk197791cf71b2004-06-26 06:37:06 +0000206 /* Interpret the [database.] part of the pragma statement. iDb is the
207 ** index of the database this pragma is being applied to in db.aDb[]. */
208 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
209 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000210 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000211
212 zLeft = sqlite3NameFromToken(pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000213 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000214 if( minusFlag ){
drhae29ffb2004-09-25 14:39:18 +0000215 zRight = sqlite3MPrintf("-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000216 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000217 zRight = sqlite3NameFromToken(pValue);
drhc11d4f92003-04-06 21:08:24 +0000218 }
danielk197791cf71b2004-06-26 06:37:06 +0000219
drh90f5ecb2004-07-22 01:19:35 +0000220 zDb = ((iDb>0)?pDb->zName:0);
danielk197791cf71b2004-06-26 06:37:06 +0000221 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000222 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000223 }
224
drh13d70422004-11-13 15:59:14 +0000225#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000226 /*
drh90f5ecb2004-07-22 01:19:35 +0000227 ** PRAGMA [database.]default_cache_size
228 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000229 **
230 ** The first form reports the current persistent setting for the
231 ** page cache size. The value returned is the maximum number of
232 ** pages in the page cache. The second form sets both the current
233 ** page cache size value and the persistent page cache size value
234 ** stored in the database file.
235 **
236 ** The default cache size is stored in meta-value 2 of page 1 of the
237 ** database file. The cache size is actually the absolute value of
238 ** this memory location. The sign of meta-value 2 determines the
239 ** synchronous setting. A negative value means synchronous is off
240 ** and a positive value means synchronous is on.
241 */
danielk19774adee202004-05-08 08:23:19 +0000242 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000243 static const VdbeOpList getCacheSize[] = {
danielk197791cf71b2004-06-26 06:37:06 +0000244 { OP_ReadCookie, 0, 2, 0}, /* 0 */
drhc11d4f92003-04-06 21:08:24 +0000245 { OP_AbsValue, 0, 0, 0},
246 { OP_Dup, 0, 0, 0},
247 { OP_Integer, 0, 0, 0},
248 { OP_Ne, 0, 6, 0},
drh905793e2004-02-21 13:31:09 +0000249 { OP_Integer, 0, 0, 0}, /* 5 */
drhc11d4f92003-04-06 21:08:24 +0000250 { OP_Callback, 1, 0, 0},
251 };
drh905793e2004-02-21 13:31:09 +0000252 int addr;
danielk19778a414492004-06-29 08:59:35 +0000253 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000254 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000255 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000256 sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000257 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000258 sqlite3VdbeChangeP1(v, addr, iDb);
danielk19774adee202004-05-08 08:23:19 +0000259 sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES);
drhc11d4f92003-04-06 21:08:24 +0000260 }else{
drhc11d4f92003-04-06 21:08:24 +0000261 int size = atoi(zRight);
262 if( size<0 ) size = -size;
danielk197791cf71b2004-06-26 06:37:06 +0000263 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk19774adee202004-05-08 08:23:19 +0000264 sqlite3VdbeAddOp(v, OP_Integer, size, 0);
danielk197791cf71b2004-06-26 06:37:06 +0000265 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2);
danielk19774adee202004-05-08 08:23:19 +0000266 addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
267 sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3);
268 sqlite3VdbeAddOp(v, OP_Negative, 0, 0);
danielk197791cf71b2004-06-26 06:37:06 +0000269 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2);
drh90f5ecb2004-07-22 01:19:35 +0000270 pDb->cache_size = size;
271 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000272 }
273 }else
274
275 /*
drh90f5ecb2004-07-22 01:19:35 +0000276 ** PRAGMA [database.]page_size
277 ** PRAGMA [database.]page_size=N
278 **
279 ** The first form reports the current setting for the
280 ** database page size in bytes. The second form sets the
281 ** database page size value. The value can only be set if
282 ** the database has not yet been created.
283 */
284 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
285 Btree *pBt = pDb->pBt;
286 if( !zRight ){
287 int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000288 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000289 }else{
danielk197728129562005-01-11 10:25:06 +0000290 sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1);
drh90f5ecb2004-07-22 01:19:35 +0000291 }
292 }else
drh13d70422004-11-13 15:59:14 +0000293#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000294
295 /*
danielk1977951af802004-11-05 15:45:09 +0000296 ** PRAGMA [database.]auto_vacuum
297 ** PRAGMA [database.]auto_vacuum=N
298 **
299 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
300 */
301#ifndef SQLITE_OMIT_AUTOVACUUM
302 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
303 Btree *pBt = pDb->pBt;
304 if( !zRight ){
305 int auto_vacuum =
306 pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
307 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
308 }else{
309 sqlite3BtreeSetAutoVacuum(pBt, getBoolean(zRight));
310 }
311 }else
312#endif
313
drh13d70422004-11-13 15:59:14 +0000314#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000315 /*
drh90f5ecb2004-07-22 01:19:35 +0000316 ** PRAGMA [database.]cache_size
317 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000318 **
319 ** The first form reports the current local setting for the
320 ** page cache size. The local setting can be different from
321 ** the persistent cache size value that is stored in the database
322 ** file itself. The value returned is the maximum number of
323 ** pages in the page cache. The second form sets the local
324 ** page cache size value. It does not change the persistent
325 ** cache size stored on the disk so the cache size will revert
326 ** to its default value when the database is closed and reopened.
327 ** N should be a positive integer.
328 */
danielk19774adee202004-05-08 08:23:19 +0000329 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000330 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000331 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000332 returnSingleInt(pParse, "cache_size", pDb->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000333 }else{
334 int size = atoi(zRight);
335 if( size<0 ) size = -size;
drh90f5ecb2004-07-22 01:19:35 +0000336 pDb->cache_size = size;
337 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000338 }
339 }else
340
341 /*
drh90f5ecb2004-07-22 01:19:35 +0000342 ** PRAGMA temp_store
343 ** PRAGMA temp_store = "default"|"memory"|"file"
344 **
345 ** Return or set the local value of the temp_store flag. Changing
346 ** the local value does not make changes to the disk file and the default
347 ** value will be restored the next time the database is opened.
348 **
349 ** Note that it is possible for the library compile-time options to
350 ** override this setting
351 */
352 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
353 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000354 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000355 }else{
356 changeTempStorage(pParse, zRight);
357 }
358 }else
359
360 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000361 ** PRAGMA temp_store_directory
362 ** PRAGMA temp_store_directory = ""|"directory_name"
363 **
364 ** Return or set the local value of the temp_store_directory flag. Changing
365 ** the value sets a specific directory to be used for temporary files.
366 ** Setting to a null string reverts to the default temporary directory search.
367 ** If temporary directory is changed, then invalidateTempStorage.
368 **
369 */
370 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
371 if( !zRight ){
372 if( sqlite3_temp_directory ){
373 sqlite3VdbeSetNumCols(v, 1);
374 sqlite3VdbeSetColName(v, 0, "temp_store_directory", P3_STATIC);
375 sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0);
376 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
377 }
378 }else{
drh268283b2005-01-08 15:44:25 +0000379 if( zRight[0] && !sqlite3OsIsDirWritable(zRight) ){
380 sqlite3ErrorMsg(pParse, "not a writable directory");
381 goto pragma_out;
382 }
383 if( TEMP_STORE==0
384 || (TEMP_STORE==1 && db->temp_store<=1)
385 || (TEMP_STORE==2 && db->temp_store==1)
386 ){
387 invalidateTempStorage(pParse);
388 }
389 sqliteFree(sqlite3_temp_directory);
390 if( zRight[0] ){
391 sqlite3_temp_directory = zRight;
392 zRight = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000393 }else{
drh268283b2005-01-08 15:44:25 +0000394 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000395 }
396 }
397 }else
398
399 /*
drh90f5ecb2004-07-22 01:19:35 +0000400 ** PRAGMA [database.]synchronous
401 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000402 **
403 ** Return or set the local value of the synchronous flag. Changing
404 ** the local value does not make changes to the disk file and the
405 ** default value will be restored the next time the database is
406 ** opened.
407 */
danielk19774adee202004-05-08 08:23:19 +0000408 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000409 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000410 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000411 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000412 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000413 if( !db->autoCommit ){
414 sqlite3ErrorMsg(pParse,
415 "Safety level may not be changed inside a transaction");
416 }else{
drh90f5ecb2004-07-22 01:19:35 +0000417 pDb->safety_level = getSafetyLevel(zRight)+1;
418 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level);
danielk197791cf71b2004-06-26 06:37:06 +0000419 }
drhc11d4f92003-04-06 21:08:24 +0000420 }
421 }else
drh13d70422004-11-13 15:59:14 +0000422#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000423
drh87223182004-02-21 14:00:29 +0000424 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000425 /* The flagPragma() subroutine also generates any necessary code
426 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000427 }else
428
drh13d70422004-11-13 15:59:14 +0000429#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000430 /*
431 ** PRAGMA table_info(<table>)
432 **
433 ** Return a single row for each column of the named table. The columns of
434 ** the returned data set are:
435 **
436 ** cid: Column id (numbered from left to right, starting at 0)
437 ** name: Column name
438 ** type: Column declaration type.
439 ** notnull: True if 'NOT NULL' is part of column declaration
440 ** dflt_value: The default value for the column, if any.
441 */
drh5260f7e2004-06-26 19:35:29 +0000442 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000443 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000444 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000445 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000446 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000447 int i;
danielk197722322fd2004-05-25 23:35:17 +0000448 sqlite3VdbeSetNumCols(v, 6);
danielk19773cf86062004-05-26 10:11:05 +0000449 sqlite3VdbeSetColName(v, 0, "cid", P3_STATIC);
450 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
451 sqlite3VdbeSetColName(v, 2, "type", P3_STATIC);
452 sqlite3VdbeSetColName(v, 3, "notnull", P3_STATIC);
453 sqlite3VdbeSetColName(v, 4, "dflt_value", P3_STATIC);
454 sqlite3VdbeSetColName(v, 5, "pk", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000455 sqlite3ViewGetColumnNames(pParse, pTab);
drhc11d4f92003-04-06 21:08:24 +0000456 for(i=0; i<pTab->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000457 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000458 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[i].zName, 0);
459 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drh701a0ae2004-02-22 20:05:00 +0000460 pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", 0);
danielk19774adee202004-05-08 08:23:19 +0000461 sqlite3VdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
danielk19777977a172004-11-09 12:44:37 +0000462 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
danielk19774adee202004-05-08 08:23:19 +0000463 sqlite3VdbeAddOp(v, OP_Integer, pTab->aCol[i].isPrimKey, 0);
464 sqlite3VdbeAddOp(v, OP_Callback, 6, 0);
drhc11d4f92003-04-06 21:08:24 +0000465 }
466 }
467 }else
468
drh5260f7e2004-06-26 19:35:29 +0000469 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000470 Index *pIdx;
471 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000472 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000473 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000474 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000475 int i;
476 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000477 sqlite3VdbeSetNumCols(v, 3);
danielk19773cf86062004-05-26 10:11:05 +0000478 sqlite3VdbeSetColName(v, 0, "seqno", P3_STATIC);
479 sqlite3VdbeSetColName(v, 1, "cid", P3_STATIC);
480 sqlite3VdbeSetColName(v, 2, "name", P3_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000481 for(i=0; i<pIdx->nColumn; i++){
482 int cnum = pIdx->aiColumn[i];
danielk19774adee202004-05-08 08:23:19 +0000483 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
484 sqlite3VdbeAddOp(v, OP_Integer, cnum, 0);
drhc11d4f92003-04-06 21:08:24 +0000485 assert( pTab->nCol>cnum );
danielk19770f69c1e2004-05-29 11:24:50 +0000486 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0);
danielk19774adee202004-05-08 08:23:19 +0000487 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
drhc11d4f92003-04-06 21:08:24 +0000488 }
489 }
490 }else
491
drh5260f7e2004-06-26 19:35:29 +0000492 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000493 Index *pIdx;
494 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000495 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000496 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000497 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000498 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000499 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000500 if( pIdx ){
501 int i = 0;
502 sqlite3VdbeSetNumCols(v, 3);
503 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
504 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
505 sqlite3VdbeSetColName(v, 2, "unique", P3_STATIC);
506 while(pIdx){
507 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
508 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0);
509 sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
510 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
511 ++i;
512 pIdx = pIdx->pNext;
513 }
drhc11d4f92003-04-06 21:08:24 +0000514 }
515 }
516 }else
517
drh13d70422004-11-13 15:59:14 +0000518 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
519 int i;
520 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
521 sqlite3VdbeSetNumCols(v, 3);
522 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
523 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
524 sqlite3VdbeSetColName(v, 2, "file", P3_STATIC);
525 for(i=0; i<db->nDb; i++){
526 if( db->aDb[i].pBt==0 ) continue;
527 assert( db->aDb[i].zName!=0 );
528 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
529 sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0);
530 sqlite3VdbeOp3(v, OP_String8, 0, 0,
531 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
532 sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
533 }
534 }else
drhb6c29892004-11-22 19:12:19 +0000535
536#ifndef SQLITE_OMIT_CURSOR
537 if( sqlite3StrICmp(zLeft, "cursor_list")==0 ){
538 int i;
539 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
540 sqlite3VdbeSetNumCols(v, 2);
541 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC);
542 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC);
543 for(i=0; i<db->nSqlCursor; i++){
544 SqlCursor *p = db->apSqlCursor[i];
545 if( p==0 ) continue;
546 assert( p->zName!=0 );
547 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
548 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
549 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
550 }
551 }else
552#endif /* SQLITE_OMIT_CURSOR */
drh13d70422004-11-13 15:59:14 +0000553#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
554
drhb7f91642004-10-31 02:22:47 +0000555#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000556 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000557 FKey *pFK;
558 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000559 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000560 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +0000561 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000562 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +0000563 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +0000564 if( pFK ){
565 int i = 0;
566 sqlite3VdbeSetNumCols(v, 5);
567 sqlite3VdbeSetColName(v, 0, "id", P3_STATIC);
568 sqlite3VdbeSetColName(v, 1, "seq", P3_STATIC);
569 sqlite3VdbeSetColName(v, 2, "table", P3_STATIC);
570 sqlite3VdbeSetColName(v, 3, "from", P3_STATIC);
571 sqlite3VdbeSetColName(v, 4, "to", P3_STATIC);
572 while(pFK){
573 int j;
574 for(j=0; j<pFK->nCol; j++){
575 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
576 sqlite3VdbeAddOp(v, OP_Integer, j, 0);
577 sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0);
578 sqlite3VdbeOp3(v, OP_String8, 0, 0,
579 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
580 sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->aCol[j].zCol, 0);
581 sqlite3VdbeAddOp(v, OP_Callback, 5, 0);
582 }
583 ++i;
584 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +0000585 }
drh78100cc2003-08-23 22:40:53 +0000586 }
587 }
588 }else
drhb7f91642004-10-31 02:22:47 +0000589#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +0000590
drhc11d4f92003-04-06 21:08:24 +0000591#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +0000592 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
593 extern void sqlite3ParserTrace(FILE*, char *);
drhc11d4f92003-04-06 21:08:24 +0000594 if( getBoolean(zRight) ){
danielk19774adee202004-05-08 08:23:19 +0000595 sqlite3ParserTrace(stdout, "parser: ");
drhc11d4f92003-04-06 21:08:24 +0000596 }else{
danielk19774adee202004-05-08 08:23:19 +0000597 sqlite3ParserTrace(0, 0);
drhc11d4f92003-04-06 21:08:24 +0000598 }
599 }else
600#endif
601
drhb7f91642004-10-31 02:22:47 +0000602#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk19774adee202004-05-08 08:23:19 +0000603 if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){
drhed717fe2003-06-15 23:42:24 +0000604 int i, j, addr;
605
606 /* Code that initializes the integrity check program. Set the
drh4be295b2003-12-16 03:44:47 +0000607 ** error count 0
drhed717fe2003-06-15 23:42:24 +0000608 */
drh57196282004-10-06 15:41:16 +0000609 static const VdbeOpList initCode[] = {
drh4be295b2003-12-16 03:44:47 +0000610 { OP_Integer, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000611 { OP_MemStore, 0, 1, 0},
drhed717fe2003-06-15 23:42:24 +0000612 };
613
drhed717fe2003-06-15 23:42:24 +0000614 /* Code that appears at the end of the integrity check. If no error
615 ** messages have been generated, output OK. Otherwise output the
616 ** error message
617 */
drh57196282004-10-06 15:41:16 +0000618 static const VdbeOpList endCode[] = {
drhed717fe2003-06-15 23:42:24 +0000619 { OP_MemLoad, 0, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000620 { OP_Integer, 0, 0, 0},
621 { OP_Ne, 0, 0, 0}, /* 2 */
drh290c1942004-08-21 17:54:45 +0000622 { OP_String8, 0, 0, "ok"},
drhc11d4f92003-04-06 21:08:24 +0000623 { OP_Callback, 1, 0, 0},
624 };
drhed717fe2003-06-15 23:42:24 +0000625
626 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +0000627 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197722322fd2004-05-25 23:35:17 +0000628 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000629 sqlite3VdbeSetColName(v, 0, "integrity_check", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000630 sqlite3VdbeAddOpList(v, ArraySize(initCode), initCode);
drhed717fe2003-06-15 23:42:24 +0000631
632 /* Do an integrity check on each database file */
633 for(i=0; i<db->nDb; i++){
634 HashElem *x;
drh79069752004-05-22 21:30:40 +0000635 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +0000636
drh80242052004-06-09 00:48:12 +0000637 sqlite3CodeVerifySchema(pParse, i);
638
drhed717fe2003-06-15 23:42:24 +0000639 /* Do an integrity check of the B-Tree
640 */
drh79069752004-05-22 21:30:40 +0000641 for(x=sqliteHashFirst(&db->aDb[i].tblHash); x; x=sqliteHashNext(x)){
642 Table *pTab = sqliteHashData(x);
643 Index *pIdx;
644 sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0);
645 cnt++;
646 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk1977e0048402004-06-15 16:51:01 +0000647 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ) goto pragma_out;
drh79069752004-05-22 21:30:40 +0000648 sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0);
649 cnt++;
650 }
651 }
danielk19772b444852004-06-29 07:45:33 +0000652 assert( cnt>0 );
drh79069752004-05-22 21:30:40 +0000653 sqlite3VdbeAddOp(v, OP_IntegrityCk, cnt, i);
654 sqlite3VdbeAddOp(v, OP_Dup, 0, 1);
danielk19770f69c1e2004-05-29 11:24:50 +0000655 addr = sqlite3VdbeOp3(v, OP_String8, 0, 0, "ok", P3_STATIC);
drh79069752004-05-22 21:30:40 +0000656 sqlite3VdbeAddOp(v, OP_Eq, 0, addr+6);
danielk19770f69c1e2004-05-29 11:24:50 +0000657 sqlite3VdbeOp3(v, OP_String8, 0, 0,
drh79069752004-05-22 21:30:40 +0000658 sqlite3MPrintf("*** in database %s ***\n", db->aDb[i].zName),
659 P3_DYNAMIC);
660 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drh855eb1c2004-08-31 13:45:11 +0000661 sqlite3VdbeAddOp(v, OP_Concat, 0, 1);
drh79069752004-05-22 21:30:40 +0000662 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
drhed717fe2003-06-15 23:42:24 +0000663
664 /* Make sure all the indices are constructed correctly.
665 */
danielk19774adee202004-05-08 08:23:19 +0000666 sqlite3CodeVerifySchema(pParse, i);
drhed717fe2003-06-15 23:42:24 +0000667 for(x=sqliteHashFirst(&db->aDb[i].tblHash); x; x=sqliteHashNext(x)){
668 Table *pTab = sqliteHashData(x);
669 Index *pIdx;
670 int loopTop;
671
672 if( pTab->pIndex==0 ) continue;
drh290c1942004-08-21 17:54:45 +0000673 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
danielk19774adee202004-05-08 08:23:19 +0000674 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
675 sqlite3VdbeAddOp(v, OP_MemStore, 1, 1);
676 loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0);
677 sqlite3VdbeAddOp(v, OP_MemIncr, 1, 0);
drhed717fe2003-06-15 23:42:24 +0000678 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +0000679 int jmp2;
drh57196282004-10-06 15:41:16 +0000680 static const VdbeOpList idxErr[] = {
drh4be295b2003-12-16 03:44:47 +0000681 { OP_MemIncr, 0, 0, 0},
drh290c1942004-08-21 17:54:45 +0000682 { OP_String8, 0, 0, "rowid "},
drhed717fe2003-06-15 23:42:24 +0000683 { OP_Recno, 1, 0, 0},
drh290c1942004-08-21 17:54:45 +0000684 { OP_String8, 0, 0, " missing from index "},
685 { OP_String8, 0, 0, 0}, /* 4 */
drh855eb1c2004-08-31 13:45:11 +0000686 { OP_Concat, 2, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000687 { OP_Callback, 1, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000688 };
drh51846b52004-05-28 16:00:21 +0000689 sqlite3GenerateIndexKey(v, pIdx, 1);
danielk19774adee202004-05-08 08:23:19 +0000690 jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0);
691 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
692 sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC);
693 sqlite3VdbeChangeP2(v, jmp2, sqlite3VdbeCurrentAddr(v));
drhed717fe2003-06-15 23:42:24 +0000694 }
danielk19774adee202004-05-08 08:23:19 +0000695 sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1);
696 sqlite3VdbeChangeP2(v, loopTop, sqlite3VdbeCurrentAddr(v));
drhed717fe2003-06-15 23:42:24 +0000697 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +0000698 static const VdbeOpList cntIdx[] = {
drhed717fe2003-06-15 23:42:24 +0000699 { OP_Integer, 0, 0, 0},
700 { OP_MemStore, 2, 1, 0},
701 { OP_Rewind, 0, 0, 0}, /* 2 */
702 { OP_MemIncr, 2, 0, 0},
703 { OP_Next, 0, 0, 0}, /* 4 */
704 { OP_MemLoad, 1, 0, 0},
705 { OP_MemLoad, 2, 0, 0},
706 { OP_Eq, 0, 0, 0}, /* 7 */
drh4be295b2003-12-16 03:44:47 +0000707 { OP_MemIncr, 0, 0, 0},
drh290c1942004-08-21 17:54:45 +0000708 { OP_String8, 0, 0, "wrong # of entries in index "},
709 { OP_String8, 0, 0, 0}, /* 10 */
drh855eb1c2004-08-31 13:45:11 +0000710 { OP_Concat, 0, 0, 0},
drh4be295b2003-12-16 03:44:47 +0000711 { OP_Callback, 1, 0, 0},
drhed717fe2003-06-15 23:42:24 +0000712 };
713 if( pIdx->tnum==0 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000714 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
715 sqlite3VdbeChangeP1(v, addr+2, j+2);
716 sqlite3VdbeChangeP2(v, addr+2, addr+5);
717 sqlite3VdbeChangeP1(v, addr+4, j+2);
718 sqlite3VdbeChangeP2(v, addr+4, addr+3);
719 sqlite3VdbeChangeP2(v, addr+7, addr+ArraySize(cntIdx));
720 sqlite3VdbeChangeP3(v, addr+10, pIdx->zName, P3_STATIC);
drhed717fe2003-06-15 23:42:24 +0000721 }
722 }
723 }
danielk19774adee202004-05-08 08:23:19 +0000724 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
725 sqlite3VdbeChangeP2(v, addr+2, addr+ArraySize(endCode));
drhc11d4f92003-04-06 21:08:24 +0000726 }else
drhb7f91642004-10-31 02:22:47 +0000727#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
728
drh13d70422004-11-13 15:59:14 +0000729#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +0000730 /*
731 ** PRAGMA encoding
732 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
733 **
734 ** In it's first form, this pragma returns the encoding of the main
735 ** database. If the database is not initialized, it is initialized now.
736 **
737 ** The second form of this pragma is a no-op if the main database file
738 ** has not already been initialized. In this case it sets the default
739 ** encoding that will be used for the main database file if a new file
740 ** is created. If an existing main database file is opened, then the
741 ** default text encoding for the existing database is used.
742 **
743 ** In all cases new databases created using the ATTACH command are
744 ** created to use the same default text encoding as the main database. If
745 ** the main database has not been initialized and/or created when ATTACH
746 ** is executed, this is done before the ATTACH operation.
747 **
748 ** In the second form this pragma sets the text encoding to be used in
749 ** new database files created using this database handle. It is only
750 ** useful if invoked immediately after the main database i
751 */
752 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh57196282004-10-06 15:41:16 +0000753 static struct EncName {
danielk19778e227872004-06-07 07:52:17 +0000754 char *zName;
755 u8 enc;
756 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +0000757 { "UTF-8", SQLITE_UTF8 },
758 { "UTF8", SQLITE_UTF8 },
759 { "UTF-16le", SQLITE_UTF16LE },
760 { "UTF16le", SQLITE_UTF16LE },
761 { "UTF-16be", SQLITE_UTF16BE },
762 { "UTF16be", SQLITE_UTF16BE },
763 { "UTF-16", 0 /* Filled in at run-time */ },
764 { "UTF16", 0 /* Filled in at run-time */ },
danielk19778e227872004-06-07 07:52:17 +0000765 { 0, 0 }
766 };
767 struct EncName *pEnc;
drh998da3a2004-06-19 15:22:56 +0000768 encnames[6].enc = encnames[7].enc = SQLITE_UTF16NATIVE;
danielk197791cf71b2004-06-26 06:37:06 +0000769 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +0000770 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +0000771 sqlite3VdbeSetNumCols(v, 1);
772 sqlite3VdbeSetColName(v, 0, "encoding", P3_STATIC);
773 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
774 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
775 if( pEnc->enc==pParse->db->enc ){
776 sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC);
777 break;
778 }
779 }
780 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
781 }else{ /* "PRAGMA encoding = XXX" */
782 /* Only change the value of sqlite.enc if the database handle is not
783 ** initialized. If the main database exists, the new sqlite.enc value
784 ** will be overwritten when the schema is next loaded. If it does not
785 ** already exists, it will be created to use the new encoding value.
786 */
787 if( !(pParse->db->flags&SQLITE_Initialized) ){
788 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
789 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
790 pParse->db->enc = pEnc->enc;
791 break;
792 }
793 }
794 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +0000795 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +0000796 }
797 }
798 }
799 }else
drh13d70422004-11-13 15:59:14 +0000800#endif /* SQLITE_OMIT_UTF16 */
801
802#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +0000803 /*
danielk1977b92b70b2004-11-12 16:11:59 +0000804 ** PRAGMA [database.]schema_version
805 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +0000806 **
danielk1977b92b70b2004-11-12 16:11:59 +0000807 ** PRAGMA [database.]user_version
808 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +0000809 **
danielk1977b92b70b2004-11-12 16:11:59 +0000810 ** The pragma's schema_version and user_version are used to set or get
811 ** the value of the schema-version and user-version, respectively. Both
812 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +0000813 ** stored in the database header.
814 **
815 ** The schema-cookie is usually only manipulated internally by SQLite. It
816 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +0000817 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +0000818 ** SQLite each time a query is executed to ensure that the internal cache
819 ** of the schema used when compiling the SQL query matches the schema of
820 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +0000821 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
822 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +0000823 ** crashes or database corruption. Use with caution!
824 **
danielk1977b92b70b2004-11-12 16:11:59 +0000825 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +0000826 ** applications for any purpose.
827 */
danielk1977b92b70b2004-11-12 16:11:59 +0000828 if( sqlite3StrICmp(zLeft, "schema_version")==0 ||
829 sqlite3StrICmp(zLeft, "user_version")==0 ){
danielk1977dae24952004-11-11 05:10:43 +0000830
831 int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
832 if( zLeft[0]=='s' || zLeft[0]=='S' ){
833 iCookie = 0;
834 }else{
835 iCookie = 5;
836 }
837
838 if( zRight ){
839 /* Write the specified cookie value */
840 static const VdbeOpList setCookie[] = {
841 { OP_Transaction, 0, 1, 0}, /* 0 */
842 { OP_Integer, 0, 0, 0}, /* 1 */
843 { OP_SetCookie, 0, 0, 0}, /* 2 */
844 };
845 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
846 sqlite3VdbeChangeP1(v, addr, iDb);
847 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
848 sqlite3VdbeChangeP1(v, addr+2, iDb);
849 sqlite3VdbeChangeP2(v, addr+2, iCookie);
850 }else{
851 /* Read the specified cookie value */
852 static const VdbeOpList readCookie[] = {
853 { OP_ReadCookie, 0, 0, 0}, /* 0 */
854 { OP_Callback, 1, 0, 0}
855 };
856 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
857 sqlite3VdbeChangeP1(v, addr, iDb);
858 sqlite3VdbeChangeP2(v, addr, iCookie);
859 sqlite3VdbeSetNumCols(v, 1);
860 }
861 }
drh13d70422004-11-13 15:59:14 +0000862#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000863
dougcurrie81c95ef2004-06-18 23:21:47 +0000864#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +0000865 /*
866 ** Report the current state of file logs for all databases
867 */
868 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +0000869 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +0000870 "unlocked", "shared", "reserved", "pending", "exclusive"
871 };
872 int i;
873 Vdbe *v = sqlite3GetVdbe(pParse);
874 sqlite3VdbeSetNumCols(v, 2);
875 sqlite3VdbeSetColName(v, 0, "database", P3_STATIC);
876 sqlite3VdbeSetColName(v, 1, "status", P3_STATIC);
877 for(i=0; i<db->nDb; i++){
878 Btree *pBt;
879 Pager *pPager;
880 if( db->aDb[i].zName==0 ) continue;
881 sqlite3VdbeOp3(v, OP_String, 0, 0, db->aDb[i].zName, P3_STATIC);
882 pBt = db->aDb[i].pBt;
883 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
884 sqlite3VdbeOp3(v, OP_String, 0, 0, "closed", P3_STATIC);
885 }else{
886 int j = sqlite3pager_lockstate(pPager);
887 sqlite3VdbeOp3(v, OP_String, 0, 0,
888 (j>=0 && j<=4) ? azLockName[j] : "unknown", P3_STATIC);
889 }
890 sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
891 }
892 }else
893#endif
894
drhc11d4f92003-04-06 21:08:24 +0000895 {}
danielk1977e0048402004-06-15 16:51:01 +0000896pragma_out:
drhc11d4f92003-04-06 21:08:24 +0000897 sqliteFree(zLeft);
898 sqliteFree(zRight);
899}
drh13d70422004-11-13 15:59:14 +0000900
901#endif /* SQLITE_OMIT_PRAGMA */