xref: /sqlite-3.40.0/src/pragma.c (revision 8a29dfde)
1 /*
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 **
14 ** $Id: pragma.c,v 1.174 2008/03/27 22:42:52 drh Exp $
15 */
16 #include "sqliteInt.h"
17 #include <ctype.h>
18 
19 /* Ignore this whole file if pragmas are disabled
20 */
21 #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
22 
23 /*
24 ** Interpret the given string as a safety level.  Return 0 for OFF,
25 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or
26 ** unrecognized string argument.
27 **
28 ** Note that the values returned are one less that the values that
29 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
30 ** to support legacy SQL code.  The safety level used to be boolean
31 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
32 */
33 static int getSafetyLevel(const char *z){
34                              /* 123456789 123456789 */
35   static const char zText[] = "onoffalseyestruefull";
36   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
37   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
38   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
39   int i, n;
40   if( isdigit(*z) ){
41     return atoi(z);
42   }
43   n = strlen(z);
44   for(i=0; i<sizeof(iLength); i++){
45     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
46       return iValue[i];
47     }
48   }
49   return 1;
50 }
51 
52 /*
53 ** Interpret the given string as a boolean value.
54 */
55 static int getBoolean(const char *z){
56   return getSafetyLevel(z)&1;
57 }
58 
59 /*
60 ** Interpret the given string as a locking mode value.
61 */
62 static int getLockingMode(const char *z){
63   if( z ){
64     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
65     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
66   }
67   return PAGER_LOCKINGMODE_QUERY;
68 }
69 
70 #ifndef SQLITE_OMIT_AUTOVACUUM
71 /*
72 ** Interpret the given string as an auto-vacuum mode value.
73 **
74 ** The following strings, "none", "full" and "incremental" are
75 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
76 */
77 static int getAutoVacuum(const char *z){
78   int i;
79   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
80   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
81   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
82   i = atoi(z);
83   return ((i>=0&&i<=2)?i:0);
84 }
85 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
86 
87 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
88 /*
89 ** Interpret the given string as a temp db location. Return 1 for file
90 ** backed temporary databases, 2 for the Red-Black tree in memory database
91 ** and 0 to use the compile-time default.
92 */
93 static int getTempStore(const char *z){
94   if( z[0]>='0' && z[0]<='2' ){
95     return z[0] - '0';
96   }else if( sqlite3StrICmp(z, "file")==0 ){
97     return 1;
98   }else if( sqlite3StrICmp(z, "memory")==0 ){
99     return 2;
100   }else{
101     return 0;
102   }
103 }
104 #endif /* SQLITE_PAGER_PRAGMAS */
105 
106 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
107 /*
108 ** Invalidate temp storage, either when the temp storage is changed
109 ** from default, or when 'file' and the temp_store_directory has changed
110 */
111 static int invalidateTempStorage(Parse *pParse){
112   sqlite3 *db = pParse->db;
113   if( db->aDb[1].pBt!=0 ){
114     if( !db->autoCommit ){
115       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
116         "from within a transaction");
117       return SQLITE_ERROR;
118     }
119     sqlite3BtreeClose(db->aDb[1].pBt);
120     db->aDb[1].pBt = 0;
121     sqlite3ResetInternalSchema(db, 0);
122   }
123   return SQLITE_OK;
124 }
125 #endif /* SQLITE_PAGER_PRAGMAS */
126 
127 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
128 /*
129 ** If the TEMP database is open, close it and mark the database schema
130 ** as needing reloading.  This must be done when using the TEMP_STORE
131 ** or DEFAULT_TEMP_STORE pragmas.
132 */
133 static int changeTempStorage(Parse *pParse, const char *zStorageType){
134   int ts = getTempStore(zStorageType);
135   sqlite3 *db = pParse->db;
136   if( db->temp_store==ts ) return SQLITE_OK;
137   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
138     return SQLITE_ERROR;
139   }
140   db->temp_store = ts;
141   return SQLITE_OK;
142 }
143 #endif /* SQLITE_PAGER_PRAGMAS */
144 
145 /*
146 ** Generate code to return a single integer value.
147 */
148 static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
149   Vdbe *v = sqlite3GetVdbe(pParse);
150   int mem = ++pParse->nMem;
151   sqlite3VdbeAddOp2(v, OP_Integer, value, mem);
152   if( pParse->explain==0 ){
153     sqlite3VdbeSetNumCols(v, 1);
154     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P4_STATIC);
155   }
156   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
157 }
158 
159 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
160 /*
161 ** Check to see if zRight and zLeft refer to a pragma that queries
162 ** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
163 ** Also, implement the pragma.
164 */
165 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
166   static const struct sPragmaType {
167     const char *zName;  /* Name of the pragma */
168     int mask;           /* Mask for the db->flags value */
169   } aPragma[] = {
170     { "full_column_names",        SQLITE_FullColNames  },
171     { "short_column_names",       SQLITE_ShortColNames },
172     { "count_changes",            SQLITE_CountRows     },
173     { "empty_result_callbacks",   SQLITE_NullCallback  },
174     { "legacy_file_format",       SQLITE_LegacyFileFmt },
175     { "fullfsync",                SQLITE_FullFSync     },
176 #ifdef SQLITE_DEBUG
177     { "sql_trace",                SQLITE_SqlTrace      },
178     { "vdbe_listing",             SQLITE_VdbeListing   },
179     { "vdbe_trace",               SQLITE_VdbeTrace     },
180 #endif
181 #ifndef SQLITE_OMIT_CHECK
182     { "ignore_check_constraints", SQLITE_IgnoreChecks  },
183 #endif
184     /* The following is VERY experimental */
185     { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
186     { "omit_readlock",            SQLITE_NoReadlock    },
187 
188     /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
189     ** flag if there are any active statements. */
190     { "read_uncommitted",         SQLITE_ReadUncommitted },
191   };
192   int i;
193   const struct sPragmaType *p;
194   for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
195     if( sqlite3StrICmp(zLeft, p->zName)==0 ){
196       sqlite3 *db = pParse->db;
197       Vdbe *v;
198       v = sqlite3GetVdbe(pParse);
199       if( v ){
200         if( zRight==0 ){
201           returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
202         }else{
203           if( getBoolean(zRight) ){
204             db->flags |= p->mask;
205           }else{
206             db->flags &= ~p->mask;
207           }
208 
209           /* Many of the flag-pragmas modify the code generated by the SQL
210           ** compiler (eg. count_changes). So add an opcode to expire all
211           ** compiled SQL statements after modifying a pragma value.
212           */
213           sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
214         }
215       }
216 
217       return 1;
218     }
219   }
220   return 0;
221 }
222 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
223 
224 /*
225 ** Process a pragma statement.
226 **
227 ** Pragmas are of this form:
228 **
229 **      PRAGMA [database.]id [= value]
230 **
231 ** The identifier might also be a string.  The value is a string, and
232 ** identifier, or a number.  If minusFlag is true, then the value is
233 ** a number that was preceded by a minus sign.
234 **
235 ** If the left side is "database.id" then pId1 is the database name
236 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
237 ** id and pId2 is any empty string.
238 */
239 void sqlite3Pragma(
240   Parse *pParse,
241   Token *pId1,        /* First part of [database.]id field */
242   Token *pId2,        /* Second part of [database.]id field, or NULL */
243   Token *pValue,      /* Token for <value>, or NULL */
244   int minusFlag       /* True if a '-' sign preceded <value> */
245 ){
246   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
247   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
248   const char *zDb = 0;   /* The database name */
249   Token *pId;            /* Pointer to <id> token */
250   int iDb;               /* Database index for <database> */
251   sqlite3 *db = pParse->db;
252   Db *pDb;
253   Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
254   if( v==0 ) return;
255   pParse->nMem = 2;
256 
257   /* Interpret the [database.] part of the pragma statement. iDb is the
258   ** index of the database this pragma is being applied to in db.aDb[]. */
259   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
260   if( iDb<0 ) return;
261   pDb = &db->aDb[iDb];
262 
263   /* If the temp database has been explicitly named as part of the
264   ** pragma, make sure it is open.
265   */
266   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
267     return;
268   }
269 
270   zLeft = sqlite3NameFromToken(db, pId);
271   if( !zLeft ) return;
272   if( minusFlag ){
273     zRight = sqlite3MPrintf(db, "-%T", pValue);
274   }else{
275     zRight = sqlite3NameFromToken(db, pValue);
276   }
277 
278   zDb = ((iDb>0)?pDb->zName:0);
279   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
280     goto pragma_out;
281   }
282 
283 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
284   /*
285   **  PRAGMA [database.]default_cache_size
286   **  PRAGMA [database.]default_cache_size=N
287   **
288   ** The first form reports the current persistent setting for the
289   ** page cache size.  The value returned is the maximum number of
290   ** pages in the page cache.  The second form sets both the current
291   ** page cache size value and the persistent page cache size value
292   ** stored in the database file.
293   **
294   ** The default cache size is stored in meta-value 2 of page 1 of the
295   ** database file.  The cache size is actually the absolute value of
296   ** this memory location.  The sign of meta-value 2 determines the
297   ** synchronous setting.  A negative value means synchronous is off
298   ** and a positive value means synchronous is on.
299   */
300   if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
301     static const VdbeOpList getCacheSize[] = {
302       { OP_ReadCookie,  0, 1,        2},  /* 0 */
303       { OP_IfPos,       1, 6,        0},
304       { OP_Integer,     0, 2,        0},
305       { OP_Subtract,    1, 2,        1},
306       { OP_IfPos,       1, 6,        0},
307       { OP_Integer,     0, 1,        0},  /* 5 */
308       { OP_ResultRow,   1, 1,        0},
309     };
310     int addr;
311     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
312     sqlite3VdbeUsesBtree(v, iDb);
313     if( !zRight ){
314       sqlite3VdbeSetNumCols(v, 1);
315       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P4_STATIC);
316       pParse->nMem += 2;
317       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
318       sqlite3VdbeChangeP1(v, addr, iDb);
319       sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE);
320     }else{
321       int size = atoi(zRight);
322       if( size<0 ) size = -size;
323       sqlite3BeginWriteOperation(pParse, 0, iDb);
324       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
325       sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, 2);
326       addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
327       sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
328       sqlite3VdbeJumpHere(v, addr);
329       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 2, 1);
330       pDb->pSchema->cache_size = size;
331       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
332     }
333   }else
334 
335   /*
336   **  PRAGMA [database.]page_size
337   **  PRAGMA [database.]page_size=N
338   **
339   ** The first form reports the current setting for the
340   ** database page size in bytes.  The second form sets the
341   ** database page size value.  The value can only be set if
342   ** the database has not yet been created.
343   */
344   if( sqlite3StrICmp(zLeft,"page_size")==0 ){
345     Btree *pBt = pDb->pBt;
346     if( !zRight ){
347       int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
348       returnSingleInt(pParse, "page_size", size);
349     }else{
350       /* Malloc may fail when setting the page-size, as there is an internal
351       ** buffer that the pager module resizes using sqlite3_realloc().
352       */
353       db->nextPagesize = atoi(zRight);
354       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1) ){
355         db->mallocFailed = 1;
356       }
357     }
358   }else
359 
360   /*
361   **  PRAGMA [database.]max_page_count
362   **  PRAGMA [database.]max_page_count=N
363   **
364   ** The first form reports the current setting for the
365   ** maximum number of pages in the database file.  The
366   ** second form attempts to change this setting.  Both
367   ** forms return the current setting.
368   */
369   if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
370     Btree *pBt = pDb->pBt;
371     int newMax = 0;
372     if( zRight ){
373       newMax = atoi(zRight);
374     }
375     if( pBt ){
376       newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
377     }
378     returnSingleInt(pParse, "max_page_count", newMax);
379   }else
380 
381   /*
382   **  PRAGMA [database.]locking_mode
383   **  PRAGMA [database.]locking_mode = (normal|exclusive)
384   */
385   if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
386     const char *zRet = "normal";
387     int eMode = getLockingMode(zRight);
388 
389     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
390       /* Simple "PRAGMA locking_mode;" statement. This is a query for
391       ** the current default locking mode (which may be different to
392       ** the locking-mode of the main database).
393       */
394       eMode = db->dfltLockMode;
395     }else{
396       Pager *pPager;
397       if( pId2->n==0 ){
398         /* This indicates that no database name was specified as part
399         ** of the PRAGMA command. In this case the locking-mode must be
400         ** set on all attached databases, as well as the main db file.
401         **
402         ** Also, the sqlite3.dfltLockMode variable is set so that
403         ** any subsequently attached databases also use the specified
404         ** locking mode.
405         */
406         int ii;
407         assert(pDb==&db->aDb[0]);
408         for(ii=2; ii<db->nDb; ii++){
409           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
410           sqlite3PagerLockingMode(pPager, eMode);
411         }
412         db->dfltLockMode = eMode;
413       }
414       pPager = sqlite3BtreePager(pDb->pBt);
415       eMode = sqlite3PagerLockingMode(pPager, eMode);
416     }
417 
418     assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
419     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
420       zRet = "exclusive";
421     }
422     sqlite3VdbeSetNumCols(v, 1);
423     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P4_STATIC);
424     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
425     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
426   }else
427 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
428 
429   /*
430   **  PRAGMA [database.]auto_vacuum
431   **  PRAGMA [database.]auto_vacuum=N
432   **
433   ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
434   */
435 #ifndef SQLITE_OMIT_AUTOVACUUM
436   if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
437     Btree *pBt = pDb->pBt;
438     if( sqlite3ReadSchema(pParse) ){
439       goto pragma_out;
440     }
441     if( !zRight ){
442       int auto_vacuum =
443           pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
444       returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
445     }else{
446       int eAuto = getAutoVacuum(zRight);
447       db->nextAutovac = eAuto;
448       if( eAuto>=0 ){
449         /* Call SetAutoVacuum() to set initialize the internal auto and
450         ** incr-vacuum flags. This is required in case this connection
451         ** creates the database file. It is important that it is created
452         ** as an auto-vacuum capable db.
453         */
454         int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
455         if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
456           /* When setting the auto_vacuum mode to either "full" or
457           ** "incremental", write the value of meta[6] in the database
458           ** file. Before writing to meta[6], check that meta[3] indicates
459           ** that this really is an auto-vacuum capable database.
460           */
461           static const VdbeOpList setMeta6[] = {
462             { OP_Transaction,    0,               1,        0},    /* 0 */
463             { OP_ReadCookie,     0,               1,        3},    /* 1 */
464             { OP_If,             1,               0,        0},    /* 2 */
465             { OP_Halt,           SQLITE_OK,       OE_Abort, 0},    /* 3 */
466             { OP_Integer,        0,               1,        0},    /* 4 */
467             { OP_SetCookie,      0,               6,        1},    /* 5 */
468           };
469           int iAddr;
470           iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
471           sqlite3VdbeChangeP1(v, iAddr, iDb);
472           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
473           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
474           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
475           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
476           sqlite3VdbeUsesBtree(v, iDb);
477         }
478       }
479     }
480   }else
481 #endif
482 
483   /*
484   **  PRAGMA [database.]incremental_vacuum(N)
485   **
486   ** Do N steps of incremental vacuuming on a database.
487   */
488 #ifndef SQLITE_OMIT_AUTOVACUUM
489   if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
490     int iLimit, addr;
491     if( sqlite3ReadSchema(pParse) ){
492       goto pragma_out;
493     }
494     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
495       iLimit = 0x7fffffff;
496     }
497     sqlite3BeginWriteOperation(pParse, 0, iDb);
498     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
499     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
500     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
501     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
502     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
503     sqlite3VdbeJumpHere(v, addr);
504   }else
505 #endif
506 
507 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
508   /*
509   **  PRAGMA [database.]cache_size
510   **  PRAGMA [database.]cache_size=N
511   **
512   ** The first form reports the current local setting for the
513   ** page cache size.  The local setting can be different from
514   ** the persistent cache size value that is stored in the database
515   ** file itself.  The value returned is the maximum number of
516   ** pages in the page cache.  The second form sets the local
517   ** page cache size value.  It does not change the persistent
518   ** cache size stored on the disk so the cache size will revert
519   ** to its default value when the database is closed and reopened.
520   ** N should be a positive integer.
521   */
522   if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
523     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
524     if( !zRight ){
525       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
526     }else{
527       int size = atoi(zRight);
528       if( size<0 ) size = -size;
529       pDb->pSchema->cache_size = size;
530       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
531     }
532   }else
533 
534   /*
535   **   PRAGMA temp_store
536   **   PRAGMA temp_store = "default"|"memory"|"file"
537   **
538   ** Return or set the local value of the temp_store flag.  Changing
539   ** the local value does not make changes to the disk file and the default
540   ** value will be restored the next time the database is opened.
541   **
542   ** Note that it is possible for the library compile-time options to
543   ** override this setting
544   */
545   if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
546     if( !zRight ){
547       returnSingleInt(pParse, "temp_store", db->temp_store);
548     }else{
549       changeTempStorage(pParse, zRight);
550     }
551   }else
552 
553   /*
554   **   PRAGMA temp_store_directory
555   **   PRAGMA temp_store_directory = ""|"directory_name"
556   **
557   ** Return or set the local value of the temp_store_directory flag.  Changing
558   ** the value sets a specific directory to be used for temporary files.
559   ** Setting to a null string reverts to the default temporary directory search.
560   ** If temporary directory is changed, then invalidateTempStorage.
561   **
562   */
563   if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
564     if( !zRight ){
565       if( sqlite3_temp_directory ){
566         sqlite3VdbeSetNumCols(v, 1);
567         sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
568             "temp_store_directory", P4_STATIC);
569         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
570         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
571       }
572     }else{
573       if( zRight[0]
574        && sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE)==0
575       ){
576         sqlite3ErrorMsg(pParse, "not a writable directory");
577         goto pragma_out;
578       }
579       if( TEMP_STORE==0
580        || (TEMP_STORE==1 && db->temp_store<=1)
581        || (TEMP_STORE==2 && db->temp_store==1)
582       ){
583         invalidateTempStorage(pParse);
584       }
585       sqlite3_free(sqlite3_temp_directory);
586       if( zRight[0] ){
587         sqlite3_temp_directory = zRight;
588         zRight = 0;
589       }else{
590         sqlite3_temp_directory = 0;
591       }
592     }
593   }else
594 
595   /*
596   **   PRAGMA [database.]synchronous
597   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
598   **
599   ** Return or set the local value of the synchronous flag.  Changing
600   ** the local value does not make changes to the disk file and the
601   ** default value will be restored the next time the database is
602   ** opened.
603   */
604   if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
605     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
606     if( !zRight ){
607       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
608     }else{
609       if( !db->autoCommit ){
610         sqlite3ErrorMsg(pParse,
611             "Safety level may not be changed inside a transaction");
612       }else{
613         pDb->safety_level = getSafetyLevel(zRight)+1;
614       }
615     }
616   }else
617 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
618 
619 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
620   if( flagPragma(pParse, zLeft, zRight) ){
621     /* The flagPragma() subroutine also generates any necessary code
622     ** there is nothing more to do here */
623   }else
624 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
625 
626 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
627   /*
628   **   PRAGMA table_info(<table>)
629   **
630   ** Return a single row for each column of the named table. The columns of
631   ** the returned data set are:
632   **
633   ** cid:        Column id (numbered from left to right, starting at 0)
634   ** name:       Column name
635   ** type:       Column declaration type.
636   ** notnull:    True if 'NOT NULL' is part of column declaration
637   ** dflt_value: The default value for the column, if any.
638   */
639   if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
640     Table *pTab;
641     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
642     pTab = sqlite3FindTable(db, zRight, zDb);
643     if( pTab ){
644       int i;
645       int nHidden = 0;
646       Column *pCol;
647       sqlite3VdbeSetNumCols(v, 6);
648       pParse->nMem = 6;
649       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P4_STATIC);
650       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
651       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P4_STATIC);
652       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P4_STATIC);
653       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P4_STATIC);
654       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P4_STATIC);
655       sqlite3ViewGetColumnNames(pParse, pTab);
656       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
657         const Token *pDflt;
658         if( IsHiddenColumn(pCol) ){
659           nHidden++;
660           continue;
661         }
662         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
663         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
664         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
665            pCol->zType ? pCol->zType : "", 0);
666         sqlite3VdbeAddOp2(v, OP_Integer, pCol->notNull, 4);
667         if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){
668           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pDflt->z, pDflt->n);
669         }else{
670           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
671         }
672         sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
673         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
674       }
675     }
676   }else
677 
678   if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
679     Index *pIdx;
680     Table *pTab;
681     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
682     pIdx = sqlite3FindIndex(db, zRight, zDb);
683     if( pIdx ){
684       int i;
685       pTab = pIdx->pTable;
686       sqlite3VdbeSetNumCols(v, 3);
687       pParse->nMem = 3;
688       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P4_STATIC);
689       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P4_STATIC);
690       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P4_STATIC);
691       for(i=0; i<pIdx->nColumn; i++){
692         int cnum = pIdx->aiColumn[i];
693         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
694         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
695         assert( pTab->nCol>cnum );
696         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
697         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
698       }
699     }
700   }else
701 
702   if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
703     Index *pIdx;
704     Table *pTab;
705     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
706     pTab = sqlite3FindTable(db, zRight, zDb);
707     if( pTab ){
708       v = sqlite3GetVdbe(pParse);
709       pIdx = pTab->pIndex;
710       if( pIdx ){
711         int i = 0;
712         sqlite3VdbeSetNumCols(v, 3);
713         pParse->nMem = 3;
714         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
715         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
716         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P4_STATIC);
717         while(pIdx){
718           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
719           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
720           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
721           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
722           ++i;
723           pIdx = pIdx->pNext;
724         }
725       }
726     }
727   }else
728 
729   if( sqlite3StrICmp(zLeft, "database_list")==0 ){
730     int i;
731     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
732     sqlite3VdbeSetNumCols(v, 3);
733     pParse->nMem = 3;
734     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
735     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
736     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P4_STATIC);
737     for(i=0; i<db->nDb; i++){
738       if( db->aDb[i].pBt==0 ) continue;
739       assert( db->aDb[i].zName!=0 );
740       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
741       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
742       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
743            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
744       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
745     }
746   }else
747 
748   if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
749     int i = 0;
750     HashElem *p;
751     sqlite3VdbeSetNumCols(v, 2);
752     pParse->nMem = 2;
753     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
754     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
755     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
756       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
757       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
758       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
759       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
760     }
761   }else
762 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
763 
764 #ifndef SQLITE_OMIT_FOREIGN_KEY
765   if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
766     FKey *pFK;
767     Table *pTab;
768     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
769     pTab = sqlite3FindTable(db, zRight, zDb);
770     if( pTab ){
771       v = sqlite3GetVdbe(pParse);
772       pFK = pTab->pFKey;
773       if( pFK ){
774         int i = 0;
775         sqlite3VdbeSetNumCols(v, 5);
776         pParse->nMem = 5;
777         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P4_STATIC);
778         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P4_STATIC);
779         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P4_STATIC);
780         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P4_STATIC);
781         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P4_STATIC);
782         while(pFK){
783           int j;
784           for(j=0; j<pFK->nCol; j++){
785             char *zCol = pFK->aCol[j].zCol;
786             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
787             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
788             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
789             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
790                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
791             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
792             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
793           }
794           ++i;
795           pFK = pFK->pNextFrom;
796         }
797       }
798     }
799   }else
800 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
801 
802 #ifndef NDEBUG
803   if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
804     if( zRight ){
805       if( getBoolean(zRight) ){
806         sqlite3ParserTrace(stderr, "parser: ");
807       }else{
808         sqlite3ParserTrace(0, 0);
809       }
810     }
811   }else
812 #endif
813 
814   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
815   ** used will be case sensitive or not depending on the RHS.
816   */
817   if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
818     if( zRight ){
819       sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
820     }
821   }else
822 
823 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
824 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
825 #endif
826 
827 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
828   /* Pragma "quick_check" is an experimental reduced version of
829   ** integrity_check designed to detect most database corruption
830   ** without most of the overhead of a full integrity-check.
831   */
832   if( sqlite3StrICmp(zLeft, "integrity_check")==0
833    || sqlite3StrICmp(zLeft, "quick_check")==0
834   ){
835     int i, j, addr, mxErr;
836 
837     /* Code that appears at the end of the integrity check.  If no error
838     ** messages have been generated, output OK.  Otherwise output the
839     ** error message
840     */
841     static const VdbeOpList endCode[] = {
842       { OP_AddImm,      1, 0,        0},    /* 0 */
843       { OP_IfNeg,       1, 0,        0},    /* 1 */
844       { OP_String8,     0, 3,        0},    /* 2 */
845       { OP_ResultRow,   3, 1,        0},
846     };
847 
848     int isQuick = (zLeft[0]=='q');
849 
850     /* Initialize the VDBE program */
851     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
852     pParse->nMem = 6;
853     sqlite3VdbeSetNumCols(v, 1);
854     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P4_STATIC);
855 
856     /* Set the maximum error count */
857     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
858     if( zRight ){
859       mxErr = atoi(zRight);
860       if( mxErr<=0 ){
861         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
862       }
863     }
864     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
865 
866     /* Do an integrity check on each database file */
867     for(i=0; i<db->nDb; i++){
868       HashElem *x;
869       Hash *pTbls;
870       int cnt = 0;
871 
872       if( OMIT_TEMPDB && i==1 ) continue;
873 
874       sqlite3CodeVerifySchema(pParse, i);
875       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
876       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
877       sqlite3VdbeJumpHere(v, addr);
878 
879       /* Do an integrity check of the B-Tree
880       **
881       ** Begin by filling registers 2, 3, ... with the root pages numbers
882       ** for all tables and indices in the database.
883       */
884       pTbls = &db->aDb[i].pSchema->tblHash;
885       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
886         Table *pTab = sqliteHashData(x);
887         Index *pIdx;
888         sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
889         cnt++;
890         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
891           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
892           cnt++;
893         }
894       }
895       if( cnt==0 ) continue;
896 
897       /* Make sure sufficient number of registers have been allocated */
898       if( pParse->nMem < cnt+4 ){
899         pParse->nMem = cnt+4;
900       }
901 
902       /* Do the b-tree integrity checks */
903       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
904       sqlite3VdbeChangeP5(v, i);
905       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
906       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
907          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
908          P4_DYNAMIC);
909       sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
910       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
911       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
912       sqlite3VdbeJumpHere(v, addr);
913 
914       /* Make sure all the indices are constructed correctly.
915       */
916       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
917         Table *pTab = sqliteHashData(x);
918         Index *pIdx;
919         int loopTop;
920 
921         if( pTab->pIndex==0 ) continue;
922         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
923         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
924         sqlite3VdbeJumpHere(v, addr);
925         sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
926         sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
927         loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
928         sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
929         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
930           int jmp2;
931           static const VdbeOpList idxErr[] = {
932             { OP_AddImm,      1, -1,  0},
933             { OP_String8,     0,  3,  0},    /* 1 */
934             { OP_Rowid,       1,  4,  0},
935             { OP_String8,     0,  5,  0},    /* 3 */
936             { OP_String8,     0,  6,  0},    /* 4 */
937             { OP_Concat,      4,  3,  3},
938             { OP_Concat,      5,  3,  3},
939             { OP_Concat,      6,  3,  3},
940             { OP_ResultRow,   3,  1,  0},
941             { OP_IfPos,       1,  0,  0},    /* 9 */
942             { OP_Halt,        0,  0,  0},
943           };
944           sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 1);
945           jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3);
946           addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
947           sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
948           sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
949           sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
950           sqlite3VdbeJumpHere(v, addr+9);
951           sqlite3VdbeJumpHere(v, jmp2);
952         }
953         sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
954         sqlite3VdbeJumpHere(v, loopTop);
955         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
956           static const VdbeOpList cntIdx[] = {
957              { OP_Integer,      0,  3,  0},
958              { OP_Rewind,       0,  0,  0},  /* 1 */
959              { OP_AddImm,       3,  1,  0},
960              { OP_Next,         0,  0,  0},  /* 3 */
961              { OP_Eq,           2,  0,  3},  /* 4 */
962              { OP_AddImm,       1, -1,  0},
963              { OP_String8,      0,  2,  0},  /* 6 */
964              { OP_String8,      0,  3,  0},  /* 7 */
965              { OP_Concat,       3,  2,  2},
966              { OP_ResultRow,    2,  1,  0},
967           };
968           if( pIdx->tnum==0 ) continue;
969           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
970           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
971           sqlite3VdbeJumpHere(v, addr);
972           addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
973           sqlite3VdbeChangeP1(v, addr+1, j+2);
974           sqlite3VdbeChangeP2(v, addr+1, addr+4);
975           sqlite3VdbeChangeP1(v, addr+3, j+2);
976           sqlite3VdbeChangeP2(v, addr+3, addr+2);
977           sqlite3VdbeJumpHere(v, addr+4);
978           sqlite3VdbeChangeP4(v, addr+6,
979                      "wrong # of entries in index ", P4_STATIC);
980           sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
981         }
982       }
983     }
984     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
985     sqlite3VdbeChangeP2(v, addr, -mxErr);
986     sqlite3VdbeJumpHere(v, addr+1);
987     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
988   }else
989 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
990 
991 #ifndef SQLITE_OMIT_UTF16
992   /*
993   **   PRAGMA encoding
994   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
995   **
996   ** In its first form, this pragma returns the encoding of the main
997   ** database. If the database is not initialized, it is initialized now.
998   **
999   ** The second form of this pragma is a no-op if the main database file
1000   ** has not already been initialized. In this case it sets the default
1001   ** encoding that will be used for the main database file if a new file
1002   ** is created. If an existing main database file is opened, then the
1003   ** default text encoding for the existing database is used.
1004   **
1005   ** In all cases new databases created using the ATTACH command are
1006   ** created to use the same default text encoding as the main database. If
1007   ** the main database has not been initialized and/or created when ATTACH
1008   ** is executed, this is done before the ATTACH operation.
1009   **
1010   ** In the second form this pragma sets the text encoding to be used in
1011   ** new database files created using this database handle. It is only
1012   ** useful if invoked immediately after the main database i
1013   */
1014   if( sqlite3StrICmp(zLeft, "encoding")==0 ){
1015     static const struct EncName {
1016       char *zName;
1017       u8 enc;
1018     } encnames[] = {
1019       { "UTF-8",    SQLITE_UTF8        },
1020       { "UTF8",     SQLITE_UTF8        },
1021       { "UTF-16le", SQLITE_UTF16LE     },
1022       { "UTF16le",  SQLITE_UTF16LE     },
1023       { "UTF-16be", SQLITE_UTF16BE     },
1024       { "UTF16be",  SQLITE_UTF16BE     },
1025       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
1026       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
1027       { 0, 0 }
1028     };
1029     const struct EncName *pEnc;
1030     if( !zRight ){    /* "PRAGMA encoding" */
1031       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
1032       sqlite3VdbeSetNumCols(v, 1);
1033       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P4_STATIC);
1034       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
1035       for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1036         if( pEnc->enc==ENC(pParse->db) ){
1037           sqlite3VdbeChangeP4(v, -1, pEnc->zName, P4_STATIC);
1038           break;
1039         }
1040       }
1041       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1042     }else{                        /* "PRAGMA encoding = XXX" */
1043       /* Only change the value of sqlite.enc if the database handle is not
1044       ** initialized. If the main database exists, the new sqlite.enc value
1045       ** will be overwritten when the schema is next loaded. If it does not
1046       ** already exists, it will be created to use the new encoding value.
1047       */
1048       if(
1049         !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1050         DbHasProperty(db, 0, DB_Empty)
1051       ){
1052         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1053           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
1054             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
1055             break;
1056           }
1057         }
1058         if( !pEnc->zName ){
1059           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
1060         }
1061       }
1062     }
1063   }else
1064 #endif /* SQLITE_OMIT_UTF16 */
1065 
1066 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
1067   /*
1068   **   PRAGMA [database.]schema_version
1069   **   PRAGMA [database.]schema_version = <integer>
1070   **
1071   **   PRAGMA [database.]user_version
1072   **   PRAGMA [database.]user_version = <integer>
1073   **
1074   ** The pragma's schema_version and user_version are used to set or get
1075   ** the value of the schema-version and user-version, respectively. Both
1076   ** the schema-version and the user-version are 32-bit signed integers
1077   ** stored in the database header.
1078   **
1079   ** The schema-cookie is usually only manipulated internally by SQLite. It
1080   ** is incremented by SQLite whenever the database schema is modified (by
1081   ** creating or dropping a table or index). The schema version is used by
1082   ** SQLite each time a query is executed to ensure that the internal cache
1083   ** of the schema used when compiling the SQL query matches the schema of
1084   ** the database against which the compiled query is actually executed.
1085   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1086   ** the schema-version is potentially dangerous and may lead to program
1087   ** crashes or database corruption. Use with caution!
1088   **
1089   ** The user-version is not used internally by SQLite. It may be used by
1090   ** applications for any purpose.
1091   */
1092   if( sqlite3StrICmp(zLeft, "schema_version")==0
1093    || sqlite3StrICmp(zLeft, "user_version")==0
1094    || sqlite3StrICmp(zLeft, "freelist_count")==0
1095   ){
1096 
1097     int iCookie;   /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
1098     sqlite3VdbeUsesBtree(v, iDb);
1099     switch( zLeft[0] ){
1100       case 's': case 'S':
1101         iCookie = 0;
1102         break;
1103       case 'f': case 'F':
1104         iCookie = 1;
1105         iDb = (-1*(iDb+1));
1106         assert(iDb<=0);
1107         break;
1108       default:
1109         iCookie = 5;
1110         break;
1111     }
1112 
1113     if( zRight && iDb>=0 ){
1114       /* Write the specified cookie value */
1115       static const VdbeOpList setCookie[] = {
1116         { OP_Transaction,    0,  1,  0},    /* 0 */
1117         { OP_Integer,        0,  1,  0},    /* 1 */
1118         { OP_SetCookie,      0,  0,  1},    /* 2 */
1119       };
1120       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1121       sqlite3VdbeChangeP1(v, addr, iDb);
1122       sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
1123       sqlite3VdbeChangeP1(v, addr+2, iDb);
1124       sqlite3VdbeChangeP2(v, addr+2, iCookie);
1125     }else{
1126       /* Read the specified cookie value */
1127       static const VdbeOpList readCookie[] = {
1128         { OP_ReadCookie,      0,  1,  0},    /* 0 */
1129         { OP_ResultRow,       1,  1,  0}
1130       };
1131       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1132       sqlite3VdbeChangeP1(v, addr, iDb);
1133       sqlite3VdbeChangeP3(v, addr, iCookie);
1134       sqlite3VdbeSetNumCols(v, 1);
1135       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P4_TRANSIENT);
1136     }
1137   }else
1138 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
1139 
1140 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
1141   /*
1142   ** Report the current state of file logs for all databases
1143   */
1144   if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
1145     static const char *const azLockName[] = {
1146       "unlocked", "shared", "reserved", "pending", "exclusive"
1147     };
1148     int i;
1149     Vdbe *v = sqlite3GetVdbe(pParse);
1150     sqlite3VdbeSetNumCols(v, 2);
1151     pParse->nMem = 2;
1152     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P4_STATIC);
1153     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P4_STATIC);
1154     for(i=0; i<db->nDb; i++){
1155       Btree *pBt;
1156       Pager *pPager;
1157       const char *zState = "unknown";
1158       int j;
1159       if( db->aDb[i].zName==0 ) continue;
1160       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
1161       pBt = db->aDb[i].pBt;
1162       if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
1163         zState = "closed";
1164       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
1165                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1166          zState = azLockName[j];
1167       }
1168       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1169       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
1170     }
1171   }else
1172 #endif
1173 
1174 #ifdef SQLITE_SSE
1175   /*
1176   ** Check to see if the sqlite_statements table exists.  Create it
1177   ** if it does not.
1178   */
1179   if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
1180     extern int sqlite3CreateStatementsTable(Parse*);
1181     sqlite3CreateStatementsTable(pParse);
1182   }else
1183 #endif
1184 
1185 #if SQLITE_HAS_CODEC
1186   if( sqlite3StrICmp(zLeft, "key")==0 ){
1187     sqlite3_key(db, zRight, strlen(zRight));
1188   }else
1189 #endif
1190 #if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
1191   if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
1192 #if SQLITE_HAS_CODEC
1193     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
1194       extern void sqlite3_activate_see(const char*);
1195       sqlite3_activate_see(&zRight[4]);
1196     }
1197 #endif
1198 #ifdef SQLITE_ENABLE_CEROD
1199     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
1200       extern void sqlite3_activate_cerod(const char*);
1201       sqlite3_activate_cerod(&zRight[6]);
1202     }
1203 #endif
1204   }
1205 #endif
1206 
1207   {}
1208 
1209   if( v ){
1210     /* Code an OP_Expire at the end of each PRAGMA program to cause
1211     ** the VDBE implementing the pragma to expire. Most (all?) pragmas
1212     ** are only valid for a single execution.
1213     */
1214     sqlite3VdbeAddOp2(v, OP_Expire, 1, 0);
1215 
1216     /*
1217     ** Reset the safety level, in case the fullfsync flag or synchronous
1218     ** setting changed.
1219     */
1220 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
1221     if( db->autoCommit ){
1222       sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
1223                  (db->flags&SQLITE_FullFSync)!=0);
1224     }
1225 #endif
1226   }
1227 pragma_out:
1228   sqlite3_free(zLeft);
1229   sqlite3_free(zRight);
1230 }
1231 
1232 #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */
1233