Use the SQLITE_TCLAPI macro in several extensions that were missed in the previous...
[sqlite.git] / src / pragma.c
blob1ee08fbec0bb70e7b7e60cc6036deac84a8e006a
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.
11 *************************************************************************
12 ** This file contains code used to implement the PRAGMA command.
14 #include "sqliteInt.h"
16 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
17 # if defined(__APPLE__)
18 # define SQLITE_ENABLE_LOCKING_STYLE 1
19 # else
20 # define SQLITE_ENABLE_LOCKING_STYLE 0
21 # endif
22 #endif
24 /***************************************************************************
25 ** The "pragma.h" include file is an automatically generated file that
26 ** that includes the PragType_XXXX macro definitions and the aPragmaName[]
27 ** object. This ensures that the aPragmaName[] table is arranged in
28 ** lexicographical order to facility a binary search of the pragma name.
29 ** Do not edit pragma.h directly. Edit and rerun the script in at
30 ** ../tool/mkpragmatab.tcl. */
31 #include "pragma.h"
34 ** Interpret the given string as a safety level. Return 0 for OFF,
35 ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
36 ** unrecognized string argument. The FULL and EXTRA option is disallowed
37 ** if the omitFull parameter it 1.
39 ** Note that the values returned are one less that the values that
40 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
41 ** to support legacy SQL code. The safety level used to be boolean
42 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
44 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
45 /* 123456789 123456789 123 */
46 static const char zText[] = "onoffalseyestruextrafull";
47 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
48 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
49 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
50 /* on no off false yes true extra full */
51 int i, n;
52 if( sqlite3Isdigit(*z) ){
53 return (u8)sqlite3Atoi(z);
55 n = sqlite3Strlen30(z);
56 for(i=0; i<ArraySize(iLength); i++){
57 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
58 && (!omitFull || iValue[i]<=1)
60 return iValue[i];
63 return dflt;
67 ** Interpret the given string as a boolean value.
69 u8 sqlite3GetBoolean(const char *z, u8 dflt){
70 return getSafetyLevel(z,1,dflt)!=0;
73 /* The sqlite3GetBoolean() function is used by other modules but the
74 ** remainder of this file is specific to PRAGMA processing. So omit
75 ** the rest of the file if PRAGMAs are omitted from the build.
77 #if !defined(SQLITE_OMIT_PRAGMA)
80 ** Interpret the given string as a locking mode value.
82 static int getLockingMode(const char *z){
83 if( z ){
84 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
85 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
87 return PAGER_LOCKINGMODE_QUERY;
90 #ifndef SQLITE_OMIT_AUTOVACUUM
92 ** Interpret the given string as an auto-vacuum mode value.
94 ** The following strings, "none", "full" and "incremental" are
95 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
97 static int getAutoVacuum(const char *z){
98 int i;
99 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
100 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
101 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
102 i = sqlite3Atoi(z);
103 return (u8)((i>=0&&i<=2)?i:0);
105 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
107 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
109 ** Interpret the given string as a temp db location. Return 1 for file
110 ** backed temporary databases, 2 for the Red-Black tree in memory database
111 ** and 0 to use the compile-time default.
113 static int getTempStore(const char *z){
114 if( z[0]>='0' && z[0]<='2' ){
115 return z[0] - '0';
116 }else if( sqlite3StrICmp(z, "file")==0 ){
117 return 1;
118 }else if( sqlite3StrICmp(z, "memory")==0 ){
119 return 2;
120 }else{
121 return 0;
124 #endif /* SQLITE_PAGER_PRAGMAS */
126 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
128 ** Invalidate temp storage, either when the temp storage is changed
129 ** from default, or when 'file' and the temp_store_directory has changed
131 static int invalidateTempStorage(Parse *pParse){
132 sqlite3 *db = pParse->db;
133 if( db->aDb[1].pBt!=0 ){
134 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
135 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
136 "from within a transaction");
137 return SQLITE_ERROR;
139 sqlite3BtreeClose(db->aDb[1].pBt);
140 db->aDb[1].pBt = 0;
141 sqlite3ResetAllSchemasOfConnection(db);
143 return SQLITE_OK;
145 #endif /* SQLITE_PAGER_PRAGMAS */
147 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
149 ** If the TEMP database is open, close it and mark the database schema
150 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
151 ** or DEFAULT_TEMP_STORE pragmas.
153 static int changeTempStorage(Parse *pParse, const char *zStorageType){
154 int ts = getTempStore(zStorageType);
155 sqlite3 *db = pParse->db;
156 if( db->temp_store==ts ) return SQLITE_OK;
157 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
158 return SQLITE_ERROR;
160 db->temp_store = (u8)ts;
161 return SQLITE_OK;
163 #endif /* SQLITE_PAGER_PRAGMAS */
166 ** Set the names of the first N columns to the values in azCol[]
168 static void setAllColumnNames(
169 Vdbe *v, /* The query under construction */
170 int N, /* Number of columns */
171 const char **azCol /* Names of columns */
173 int i;
174 sqlite3VdbeSetNumCols(v, N);
175 for(i=0; i<N; i++){
176 sqlite3VdbeSetColName(v, i, COLNAME_NAME, azCol[i], SQLITE_STATIC);
179 static void setOneColumnName(Vdbe *v, const char *z){
180 setAllColumnNames(v, 1, &z);
184 ** Generate code to return a single integer value.
186 static void returnSingleInt(Vdbe *v, const char *zLabel, i64 value){
187 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
188 setOneColumnName(v, zLabel);
189 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
193 ** Generate code to return a single text value.
195 static void returnSingleText(
196 Vdbe *v, /* Prepared statement under construction */
197 const char *zLabel, /* Name of the result column */
198 const char *zValue /* Value to be returned */
200 if( zValue ){
201 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
202 setOneColumnName(v, zLabel);
203 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
209 ** Set the safety_level and pager flags for pager iDb. Or if iDb<0
210 ** set these values for all pagers.
212 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
213 static void setAllPagerFlags(sqlite3 *db){
214 if( db->autoCommit ){
215 Db *pDb = db->aDb;
216 int n = db->nDb;
217 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
218 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
219 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
220 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
221 == PAGER_FLAGS_MASK );
222 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
223 while( (n--) > 0 ){
224 if( pDb->pBt ){
225 sqlite3BtreeSetPagerFlags(pDb->pBt,
226 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
228 pDb++;
232 #else
233 # define setAllPagerFlags(X) /* no-op */
234 #endif
238 ** Return a human-readable name for a constraint resolution action.
240 #ifndef SQLITE_OMIT_FOREIGN_KEY
241 static const char *actionName(u8 action){
242 const char *zName;
243 switch( action ){
244 case OE_SetNull: zName = "SET NULL"; break;
245 case OE_SetDflt: zName = "SET DEFAULT"; break;
246 case OE_Cascade: zName = "CASCADE"; break;
247 case OE_Restrict: zName = "RESTRICT"; break;
248 default: zName = "NO ACTION";
249 assert( action==OE_None ); break;
251 return zName;
253 #endif
257 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
258 ** defined in pager.h. This function returns the associated lowercase
259 ** journal-mode name.
261 const char *sqlite3JournalModename(int eMode){
262 static char * const azModeName[] = {
263 "delete", "persist", "off", "truncate", "memory"
264 #ifndef SQLITE_OMIT_WAL
265 , "wal"
266 #endif
268 assert( PAGER_JOURNALMODE_DELETE==0 );
269 assert( PAGER_JOURNALMODE_PERSIST==1 );
270 assert( PAGER_JOURNALMODE_OFF==2 );
271 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
272 assert( PAGER_JOURNALMODE_MEMORY==4 );
273 assert( PAGER_JOURNALMODE_WAL==5 );
274 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
276 if( eMode==ArraySize(azModeName) ) return 0;
277 return azModeName[eMode];
281 ** Process a pragma statement.
283 ** Pragmas are of this form:
285 ** PRAGMA [schema.]id [= value]
287 ** The identifier might also be a string. The value is a string, and
288 ** identifier, or a number. If minusFlag is true, then the value is
289 ** a number that was preceded by a minus sign.
291 ** If the left side is "database.id" then pId1 is the database name
292 ** and pId2 is the id. If the left side is just "id" then pId1 is the
293 ** id and pId2 is any empty string.
295 void sqlite3Pragma(
296 Parse *pParse,
297 Token *pId1, /* First part of [schema.]id field */
298 Token *pId2, /* Second part of [schema.]id field, or NULL */
299 Token *pValue, /* Token for <value>, or NULL */
300 int minusFlag /* True if a '-' sign preceded <value> */
302 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
303 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
304 const char *zDb = 0; /* The database name */
305 Token *pId; /* Pointer to <id> token */
306 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
307 int iDb; /* Database index for <database> */
308 int lwr, upr, mid = 0; /* Binary search bounds */
309 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
310 sqlite3 *db = pParse->db; /* The database connection */
311 Db *pDb; /* The specific database being pragmaed */
312 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
313 const struct sPragmaNames *pPragma;
315 if( v==0 ) return;
316 sqlite3VdbeRunOnlyOnce(v);
317 pParse->nMem = 2;
319 /* Interpret the [schema.] part of the pragma statement. iDb is the
320 ** index of the database this pragma is being applied to in db.aDb[]. */
321 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
322 if( iDb<0 ) return;
323 pDb = &db->aDb[iDb];
325 /* If the temp database has been explicitly named as part of the
326 ** pragma, make sure it is open.
328 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
329 return;
332 zLeft = sqlite3NameFromToken(db, pId);
333 if( !zLeft ) return;
334 if( minusFlag ){
335 zRight = sqlite3MPrintf(db, "-%T", pValue);
336 }else{
337 zRight = sqlite3NameFromToken(db, pValue);
340 assert( pId2 );
341 zDb = pId2->n>0 ? pDb->zName : 0;
342 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
343 goto pragma_out;
346 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
347 ** connection. If it returns SQLITE_OK, then assume that the VFS
348 ** handled the pragma and generate a no-op prepared statement.
350 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
351 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
352 ** object corresponding to the database file to which the pragma
353 ** statement refers.
355 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
356 ** file control is an array of pointers to strings (char**) in which the
357 ** second element of the array is the name of the pragma and the third
358 ** element is the argument to the pragma or NULL if the pragma has no
359 ** argument.
361 aFcntl[0] = 0;
362 aFcntl[1] = zLeft;
363 aFcntl[2] = zRight;
364 aFcntl[3] = 0;
365 db->busyHandler.nBusy = 0;
366 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
367 if( rc==SQLITE_OK ){
368 returnSingleText(v, "result", aFcntl[0]);
369 sqlite3_free(aFcntl[0]);
370 goto pragma_out;
372 if( rc!=SQLITE_NOTFOUND ){
373 if( aFcntl[0] ){
374 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
375 sqlite3_free(aFcntl[0]);
377 pParse->nErr++;
378 pParse->rc = rc;
379 goto pragma_out;
382 /* Locate the pragma in the lookup table */
383 lwr = 0;
384 upr = ArraySize(aPragmaNames)-1;
385 while( lwr<=upr ){
386 mid = (lwr+upr)/2;
387 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
388 if( rc==0 ) break;
389 if( rc<0 ){
390 upr = mid - 1;
391 }else{
392 lwr = mid + 1;
395 if( lwr>upr ) goto pragma_out;
396 pPragma = &aPragmaNames[mid];
398 /* Make sure the database schema is loaded if the pragma requires that */
399 if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){
400 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
403 /* Jump to the appropriate pragma handler */
404 switch( pPragma->ePragTyp ){
406 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
408 ** PRAGMA [schema.]default_cache_size
409 ** PRAGMA [schema.]default_cache_size=N
411 ** The first form reports the current persistent setting for the
412 ** page cache size. The value returned is the maximum number of
413 ** pages in the page cache. The second form sets both the current
414 ** page cache size value and the persistent page cache size value
415 ** stored in the database file.
417 ** Older versions of SQLite would set the default cache size to a
418 ** negative number to indicate synchronous=OFF. These days, synchronous
419 ** is always on by default regardless of the sign of the default cache
420 ** size. But continue to take the absolute value of the default cache
421 ** size of historical compatibility.
423 case PragTyp_DEFAULT_CACHE_SIZE: {
424 static const int iLn = VDBE_OFFSET_LINENO(2);
425 static const VdbeOpList getCacheSize[] = {
426 { OP_Transaction, 0, 0, 0}, /* 0 */
427 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
428 { OP_IfPos, 1, 8, 0},
429 { OP_Integer, 0, 2, 0},
430 { OP_Subtract, 1, 2, 1},
431 { OP_IfPos, 1, 8, 0},
432 { OP_Integer, 0, 1, 0}, /* 6 */
433 { OP_Noop, 0, 0, 0},
434 { OP_ResultRow, 1, 1, 0},
436 VdbeOp *aOp;
437 sqlite3VdbeUsesBtree(v, iDb);
438 if( !zRight ){
439 setOneColumnName(v, "cache_size");
440 pParse->nMem += 2;
441 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
442 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
443 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
444 aOp[0].p1 = iDb;
445 aOp[1].p1 = iDb;
446 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
447 }else{
448 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
449 sqlite3BeginWriteOperation(pParse, 0, iDb);
450 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
451 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
452 pDb->pSchema->cache_size = size;
453 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
455 break;
457 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
459 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
461 ** PRAGMA [schema.]page_size
462 ** PRAGMA [schema.]page_size=N
464 ** The first form reports the current setting for the
465 ** database page size in bytes. The second form sets the
466 ** database page size value. The value can only be set if
467 ** the database has not yet been created.
469 case PragTyp_PAGE_SIZE: {
470 Btree *pBt = pDb->pBt;
471 assert( pBt!=0 );
472 if( !zRight ){
473 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
474 returnSingleInt(v, "page_size", size);
475 }else{
476 /* Malloc may fail when setting the page-size, as there is an internal
477 ** buffer that the pager module resizes using sqlite3_realloc().
479 db->nextPagesize = sqlite3Atoi(zRight);
480 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
481 sqlite3OomFault(db);
484 break;
488 ** PRAGMA [schema.]secure_delete
489 ** PRAGMA [schema.]secure_delete=ON/OFF
491 ** The first form reports the current setting for the
492 ** secure_delete flag. The second form changes the secure_delete
493 ** flag setting and reports thenew value.
495 case PragTyp_SECURE_DELETE: {
496 Btree *pBt = pDb->pBt;
497 int b = -1;
498 assert( pBt!=0 );
499 if( zRight ){
500 b = sqlite3GetBoolean(zRight, 0);
502 if( pId2->n==0 && b>=0 ){
503 int ii;
504 for(ii=0; ii<db->nDb; ii++){
505 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
508 b = sqlite3BtreeSecureDelete(pBt, b);
509 returnSingleInt(v, "secure_delete", b);
510 break;
514 ** PRAGMA [schema.]max_page_count
515 ** PRAGMA [schema.]max_page_count=N
517 ** The first form reports the current setting for the
518 ** maximum number of pages in the database file. The
519 ** second form attempts to change this setting. Both
520 ** forms return the current setting.
522 ** The absolute value of N is used. This is undocumented and might
523 ** change. The only purpose is to provide an easy way to test
524 ** the sqlite3AbsInt32() function.
526 ** PRAGMA [schema.]page_count
528 ** Return the number of pages in the specified database.
530 case PragTyp_PAGE_COUNT: {
531 int iReg;
532 sqlite3CodeVerifySchema(pParse, iDb);
533 iReg = ++pParse->nMem;
534 if( sqlite3Tolower(zLeft[0])=='p' ){
535 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
536 }else{
537 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
538 sqlite3AbsInt32(sqlite3Atoi(zRight)));
540 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
541 sqlite3VdbeSetNumCols(v, 1);
542 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
543 break;
547 ** PRAGMA [schema.]locking_mode
548 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
550 case PragTyp_LOCKING_MODE: {
551 const char *zRet = "normal";
552 int eMode = getLockingMode(zRight);
554 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
555 /* Simple "PRAGMA locking_mode;" statement. This is a query for
556 ** the current default locking mode (which may be different to
557 ** the locking-mode of the main database).
559 eMode = db->dfltLockMode;
560 }else{
561 Pager *pPager;
562 if( pId2->n==0 ){
563 /* This indicates that no database name was specified as part
564 ** of the PRAGMA command. In this case the locking-mode must be
565 ** set on all attached databases, as well as the main db file.
567 ** Also, the sqlite3.dfltLockMode variable is set so that
568 ** any subsequently attached databases also use the specified
569 ** locking mode.
571 int ii;
572 assert(pDb==&db->aDb[0]);
573 for(ii=2; ii<db->nDb; ii++){
574 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
575 sqlite3PagerLockingMode(pPager, eMode);
577 db->dfltLockMode = (u8)eMode;
579 pPager = sqlite3BtreePager(pDb->pBt);
580 eMode = sqlite3PagerLockingMode(pPager, eMode);
583 assert( eMode==PAGER_LOCKINGMODE_NORMAL
584 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
585 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
586 zRet = "exclusive";
588 returnSingleText(v, "locking_mode", zRet);
589 break;
593 ** PRAGMA [schema.]journal_mode
594 ** PRAGMA [schema.]journal_mode =
595 ** (delete|persist|off|truncate|memory|wal|off)
597 case PragTyp_JOURNAL_MODE: {
598 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
599 int ii; /* Loop counter */
601 setOneColumnName(v, "journal_mode");
602 if( zRight==0 ){
603 /* If there is no "=MODE" part of the pragma, do a query for the
604 ** current mode */
605 eMode = PAGER_JOURNALMODE_QUERY;
606 }else{
607 const char *zMode;
608 int n = sqlite3Strlen30(zRight);
609 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
610 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
612 if( !zMode ){
613 /* If the "=MODE" part does not match any known journal mode,
614 ** then do a query */
615 eMode = PAGER_JOURNALMODE_QUERY;
618 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
619 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
620 iDb = 0;
621 pId2->n = 1;
623 for(ii=db->nDb-1; ii>=0; ii--){
624 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
625 sqlite3VdbeUsesBtree(v, ii);
626 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
629 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
630 break;
634 ** PRAGMA [schema.]journal_size_limit
635 ** PRAGMA [schema.]journal_size_limit=N
637 ** Get or set the size limit on rollback journal files.
639 case PragTyp_JOURNAL_SIZE_LIMIT: {
640 Pager *pPager = sqlite3BtreePager(pDb->pBt);
641 i64 iLimit = -2;
642 if( zRight ){
643 sqlite3DecOrHexToI64(zRight, &iLimit);
644 if( iLimit<-1 ) iLimit = -1;
646 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
647 returnSingleInt(v, "journal_size_limit", iLimit);
648 break;
651 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
654 ** PRAGMA [schema.]auto_vacuum
655 ** PRAGMA [schema.]auto_vacuum=N
657 ** Get or set the value of the database 'auto-vacuum' parameter.
658 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
660 #ifndef SQLITE_OMIT_AUTOVACUUM
661 case PragTyp_AUTO_VACUUM: {
662 Btree *pBt = pDb->pBt;
663 assert( pBt!=0 );
664 if( !zRight ){
665 returnSingleInt(v, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
666 }else{
667 int eAuto = getAutoVacuum(zRight);
668 assert( eAuto>=0 && eAuto<=2 );
669 db->nextAutovac = (u8)eAuto;
670 /* Call SetAutoVacuum() to set initialize the internal auto and
671 ** incr-vacuum flags. This is required in case this connection
672 ** creates the database file. It is important that it is created
673 ** as an auto-vacuum capable db.
675 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
676 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
677 /* When setting the auto_vacuum mode to either "full" or
678 ** "incremental", write the value of meta[6] in the database
679 ** file. Before writing to meta[6], check that meta[3] indicates
680 ** that this really is an auto-vacuum capable database.
682 static const int iLn = VDBE_OFFSET_LINENO(2);
683 static const VdbeOpList setMeta6[] = {
684 { OP_Transaction, 0, 1, 0}, /* 0 */
685 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
686 { OP_If, 1, 0, 0}, /* 2 */
687 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
688 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
690 VdbeOp *aOp;
691 int iAddr = sqlite3VdbeCurrentAddr(v);
692 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
693 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
694 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
695 aOp[0].p1 = iDb;
696 aOp[1].p1 = iDb;
697 aOp[2].p2 = iAddr+4;
698 aOp[4].p1 = iDb;
699 aOp[4].p3 = eAuto - 1;
700 sqlite3VdbeUsesBtree(v, iDb);
703 break;
705 #endif
708 ** PRAGMA [schema.]incremental_vacuum(N)
710 ** Do N steps of incremental vacuuming on a database.
712 #ifndef SQLITE_OMIT_AUTOVACUUM
713 case PragTyp_INCREMENTAL_VACUUM: {
714 int iLimit, addr;
715 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
716 iLimit = 0x7fffffff;
718 sqlite3BeginWriteOperation(pParse, 0, iDb);
719 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
720 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
721 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
722 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
723 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
724 sqlite3VdbeJumpHere(v, addr);
725 break;
727 #endif
729 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
731 ** PRAGMA [schema.]cache_size
732 ** PRAGMA [schema.]cache_size=N
734 ** The first form reports the current local setting for the
735 ** page cache size. The second form sets the local
736 ** page cache size value. If N is positive then that is the
737 ** number of pages in the cache. If N is negative, then the
738 ** number of pages is adjusted so that the cache uses -N kibibytes
739 ** of memory.
741 case PragTyp_CACHE_SIZE: {
742 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
743 if( !zRight ){
744 returnSingleInt(v, "cache_size", pDb->pSchema->cache_size);
745 }else{
746 int size = sqlite3Atoi(zRight);
747 pDb->pSchema->cache_size = size;
748 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
750 break;
754 ** PRAGMA [schema.]cache_spill
755 ** PRAGMA cache_spill=BOOLEAN
756 ** PRAGMA [schema.]cache_spill=N
758 ** The first form reports the current local setting for the
759 ** page cache spill size. The second form turns cache spill on
760 ** or off. When turnning cache spill on, the size is set to the
761 ** current cache_size. The third form sets a spill size that
762 ** may be different form the cache size.
763 ** If N is positive then that is the
764 ** number of pages in the cache. If N is negative, then the
765 ** number of pages is adjusted so that the cache uses -N kibibytes
766 ** of memory.
768 ** If the number of cache_spill pages is less then the number of
769 ** cache_size pages, no spilling occurs until the page count exceeds
770 ** the number of cache_size pages.
772 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
773 ** not just the schema specified.
775 case PragTyp_CACHE_SPILL: {
776 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
777 if( !zRight ){
778 returnSingleInt(v, "cache_spill",
779 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
780 sqlite3BtreeSetSpillSize(pDb->pBt,0));
781 }else{
782 int size = 1;
783 if( sqlite3GetInt32(zRight, &size) ){
784 sqlite3BtreeSetSpillSize(pDb->pBt, size);
786 if( sqlite3GetBoolean(zRight, size!=0) ){
787 db->flags |= SQLITE_CacheSpill;
788 }else{
789 db->flags &= ~SQLITE_CacheSpill;
791 setAllPagerFlags(db);
793 break;
797 ** PRAGMA [schema.]mmap_size(N)
799 ** Used to set mapping size limit. The mapping size limit is
800 ** used to limit the aggregate size of all memory mapped regions of the
801 ** database file. If this parameter is set to zero, then memory mapping
802 ** is not used at all. If N is negative, then the default memory map
803 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
804 ** The parameter N is measured in bytes.
806 ** This value is advisory. The underlying VFS is free to memory map
807 ** as little or as much as it wants. Except, if N is set to 0 then the
808 ** upper layers will never invoke the xFetch interfaces to the VFS.
810 case PragTyp_MMAP_SIZE: {
811 sqlite3_int64 sz;
812 #if SQLITE_MAX_MMAP_SIZE>0
813 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
814 if( zRight ){
815 int ii;
816 sqlite3DecOrHexToI64(zRight, &sz);
817 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
818 if( pId2->n==0 ) db->szMmap = sz;
819 for(ii=db->nDb-1; ii>=0; ii--){
820 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
821 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
825 sz = -1;
826 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
827 #else
828 sz = 0;
829 rc = SQLITE_OK;
830 #endif
831 if( rc==SQLITE_OK ){
832 returnSingleInt(v, "mmap_size", sz);
833 }else if( rc!=SQLITE_NOTFOUND ){
834 pParse->nErr++;
835 pParse->rc = rc;
837 break;
841 ** PRAGMA temp_store
842 ** PRAGMA temp_store = "default"|"memory"|"file"
844 ** Return or set the local value of the temp_store flag. Changing
845 ** the local value does not make changes to the disk file and the default
846 ** value will be restored the next time the database is opened.
848 ** Note that it is possible for the library compile-time options to
849 ** override this setting
851 case PragTyp_TEMP_STORE: {
852 if( !zRight ){
853 returnSingleInt(v, "temp_store", db->temp_store);
854 }else{
855 changeTempStorage(pParse, zRight);
857 break;
861 ** PRAGMA temp_store_directory
862 ** PRAGMA temp_store_directory = ""|"directory_name"
864 ** Return or set the local value of the temp_store_directory flag. Changing
865 ** the value sets a specific directory to be used for temporary files.
866 ** Setting to a null string reverts to the default temporary directory search.
867 ** If temporary directory is changed, then invalidateTempStorage.
870 case PragTyp_TEMP_STORE_DIRECTORY: {
871 if( !zRight ){
872 returnSingleText(v, "temp_store_directory", sqlite3_temp_directory);
873 }else{
874 #ifndef SQLITE_OMIT_WSD
875 if( zRight[0] ){
876 int res;
877 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
878 if( rc!=SQLITE_OK || res==0 ){
879 sqlite3ErrorMsg(pParse, "not a writable directory");
880 goto pragma_out;
883 if( SQLITE_TEMP_STORE==0
884 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
885 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
887 invalidateTempStorage(pParse);
889 sqlite3_free(sqlite3_temp_directory);
890 if( zRight[0] ){
891 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
892 }else{
893 sqlite3_temp_directory = 0;
895 #endif /* SQLITE_OMIT_WSD */
897 break;
900 #if SQLITE_OS_WIN
902 ** PRAGMA data_store_directory
903 ** PRAGMA data_store_directory = ""|"directory_name"
905 ** Return or set the local value of the data_store_directory flag. Changing
906 ** the value sets a specific directory to be used for database files that
907 ** were specified with a relative pathname. Setting to a null string reverts
908 ** to the default database directory, which for database files specified with
909 ** a relative path will probably be based on the current directory for the
910 ** process. Database file specified with an absolute path are not impacted
911 ** by this setting, regardless of its value.
914 case PragTyp_DATA_STORE_DIRECTORY: {
915 if( !zRight ){
916 returnSingleText(v, "data_store_directory", sqlite3_data_directory);
917 }else{
918 #ifndef SQLITE_OMIT_WSD
919 if( zRight[0] ){
920 int res;
921 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
922 if( rc!=SQLITE_OK || res==0 ){
923 sqlite3ErrorMsg(pParse, "not a writable directory");
924 goto pragma_out;
927 sqlite3_free(sqlite3_data_directory);
928 if( zRight[0] ){
929 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
930 }else{
931 sqlite3_data_directory = 0;
933 #endif /* SQLITE_OMIT_WSD */
935 break;
937 #endif
939 #if SQLITE_ENABLE_LOCKING_STYLE
941 ** PRAGMA [schema.]lock_proxy_file
942 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
944 ** Return or set the value of the lock_proxy_file flag. Changing
945 ** the value sets a specific file to be used for database access locks.
948 case PragTyp_LOCK_PROXY_FILE: {
949 if( !zRight ){
950 Pager *pPager = sqlite3BtreePager(pDb->pBt);
951 char *proxy_file_path = NULL;
952 sqlite3_file *pFile = sqlite3PagerFile(pPager);
953 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
954 &proxy_file_path);
955 returnSingleText(v, "lock_proxy_file", proxy_file_path);
956 }else{
957 Pager *pPager = sqlite3BtreePager(pDb->pBt);
958 sqlite3_file *pFile = sqlite3PagerFile(pPager);
959 int res;
960 if( zRight[0] ){
961 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
962 zRight);
963 } else {
964 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
965 NULL);
967 if( res!=SQLITE_OK ){
968 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
969 goto pragma_out;
972 break;
974 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
977 ** PRAGMA [schema.]synchronous
978 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
980 ** Return or set the local value of the synchronous flag. Changing
981 ** the local value does not make changes to the disk file and the
982 ** default value will be restored the next time the database is
983 ** opened.
985 case PragTyp_SYNCHRONOUS: {
986 if( !zRight ){
987 returnSingleInt(v, "synchronous", pDb->safety_level-1);
988 }else{
989 if( !db->autoCommit ){
990 sqlite3ErrorMsg(pParse,
991 "Safety level may not be changed inside a transaction");
992 }else{
993 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
994 if( iLevel==0 ) iLevel = 1;
995 pDb->safety_level = iLevel;
996 pDb->bSyncSet = 1;
997 setAllPagerFlags(db);
1000 break;
1002 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
1004 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
1005 case PragTyp_FLAG: {
1006 if( zRight==0 ){
1007 returnSingleInt(v, pPragma->zName, (db->flags & pPragma->iArg)!=0 );
1008 }else{
1009 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
1010 if( db->autoCommit==0 ){
1011 /* Foreign key support may not be enabled or disabled while not
1012 ** in auto-commit mode. */
1013 mask &= ~(SQLITE_ForeignKeys);
1015 #if SQLITE_USER_AUTHENTICATION
1016 if( db->auth.authLevel==UAUTH_User ){
1017 /* Do not allow non-admin users to modify the schema arbitrarily */
1018 mask &= ~(SQLITE_WriteSchema);
1020 #endif
1022 if( sqlite3GetBoolean(zRight, 0) ){
1023 db->flags |= mask;
1024 }else{
1025 db->flags &= ~mask;
1026 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1029 /* Many of the flag-pragmas modify the code generated by the SQL
1030 ** compiler (eg. count_changes). So add an opcode to expire all
1031 ** compiled SQL statements after modifying a pragma value.
1033 sqlite3VdbeAddOp0(v, OP_Expire);
1034 setAllPagerFlags(db);
1036 break;
1038 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
1040 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
1042 ** PRAGMA table_info(<table>)
1044 ** Return a single row for each column of the named table. The columns of
1045 ** the returned data set are:
1047 ** cid: Column id (numbered from left to right, starting at 0)
1048 ** name: Column name
1049 ** type: Column declaration type.
1050 ** notnull: True if 'NOT NULL' is part of column declaration
1051 ** dflt_value: The default value for the column, if any.
1053 case PragTyp_TABLE_INFO: if( zRight ){
1054 Table *pTab;
1055 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
1056 if( pTab ){
1057 static const char *azCol[] = {
1058 "cid", "name", "type", "notnull", "dflt_value", "pk"
1060 int i, k;
1061 int nHidden = 0;
1062 Column *pCol;
1063 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
1064 pParse->nMem = 6;
1065 sqlite3CodeVerifySchema(pParse, iDb);
1066 setAllColumnNames(v, 6, azCol); assert( 6==ArraySize(azCol) );
1067 sqlite3ViewGetColumnNames(pParse, pTab);
1068 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
1069 if( IsHiddenColumn(pCol) ){
1070 nHidden++;
1071 continue;
1073 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1074 k = 0;
1075 }else if( pPk==0 ){
1076 k = 1;
1077 }else{
1078 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
1080 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
1081 sqlite3VdbeMultiLoad(v, 1, "issisi",
1082 i-nHidden,
1083 pCol->zName,
1084 sqlite3ColumnType(pCol,""),
1085 pCol->notNull ? 1 : 0,
1086 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
1088 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
1092 break;
1094 case PragTyp_STATS: {
1095 static const char *azCol[] = { "table", "index", "width", "height" };
1096 Index *pIdx;
1097 HashElem *i;
1098 v = sqlite3GetVdbe(pParse);
1099 pParse->nMem = 4;
1100 sqlite3CodeVerifySchema(pParse, iDb);
1101 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
1102 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1103 Table *pTab = sqliteHashData(i);
1104 sqlite3VdbeMultiLoad(v, 1, "ssii",
1105 pTab->zName,
1107 pTab->szTabRow,
1108 pTab->nRowLogEst);
1109 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1110 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1111 sqlite3VdbeMultiLoad(v, 2, "sii",
1112 pIdx->zName,
1113 pIdx->szIdxRow,
1114 pIdx->aiRowLogEst[0]);
1115 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1119 break;
1121 case PragTyp_INDEX_INFO: if( zRight ){
1122 Index *pIdx;
1123 Table *pTab;
1124 pIdx = sqlite3FindIndex(db, zRight, zDb);
1125 if( pIdx ){
1126 static const char *azCol[] = {
1127 "seqno", "cid", "name", "desc", "coll", "key"
1129 int i;
1130 int mx;
1131 if( pPragma->iArg ){
1132 /* PRAGMA index_xinfo (newer version with more rows and columns) */
1133 mx = pIdx->nColumn;
1134 pParse->nMem = 6;
1135 }else{
1136 /* PRAGMA index_info (legacy version) */
1137 mx = pIdx->nKeyCol;
1138 pParse->nMem = 3;
1140 pTab = pIdx->pTable;
1141 sqlite3CodeVerifySchema(pParse, iDb);
1142 assert( pParse->nMem<=ArraySize(azCol) );
1143 setAllColumnNames(v, pParse->nMem, azCol);
1144 for(i=0; i<mx; i++){
1145 i16 cnum = pIdx->aiColumn[i];
1146 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum,
1147 cnum<0 ? 0 : pTab->aCol[cnum].zName);
1148 if( pPragma->iArg ){
1149 sqlite3VdbeMultiLoad(v, 4, "isi",
1150 pIdx->aSortOrder[i],
1151 pIdx->azColl[i],
1152 i<pIdx->nKeyCol);
1154 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
1158 break;
1160 case PragTyp_INDEX_LIST: if( zRight ){
1161 Index *pIdx;
1162 Table *pTab;
1163 int i;
1164 pTab = sqlite3FindTable(db, zRight, zDb);
1165 if( pTab ){
1166 static const char *azCol[] = {
1167 "seq", "name", "unique", "origin", "partial"
1169 v = sqlite3GetVdbe(pParse);
1170 pParse->nMem = 5;
1171 sqlite3CodeVerifySchema(pParse, iDb);
1172 setAllColumnNames(v, 5, azCol); assert( 5==ArraySize(azCol) );
1173 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
1174 const char *azOrigin[] = { "c", "u", "pk" };
1175 sqlite3VdbeMultiLoad(v, 1, "isisi",
1177 pIdx->zName,
1178 IsUniqueIndex(pIdx),
1179 azOrigin[pIdx->idxType],
1180 pIdx->pPartIdxWhere!=0);
1181 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
1185 break;
1187 case PragTyp_DATABASE_LIST: {
1188 static const char *azCol[] = { "seq", "name", "file" };
1189 int i;
1190 pParse->nMem = 3;
1191 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
1192 for(i=0; i<db->nDb; i++){
1193 if( db->aDb[i].pBt==0 ) continue;
1194 assert( db->aDb[i].zName!=0 );
1195 sqlite3VdbeMultiLoad(v, 1, "iss",
1197 db->aDb[i].zName,
1198 sqlite3BtreeGetFilename(db->aDb[i].pBt));
1199 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
1202 break;
1204 case PragTyp_COLLATION_LIST: {
1205 static const char *azCol[] = { "seq", "name" };
1206 int i = 0;
1207 HashElem *p;
1208 pParse->nMem = 2;
1209 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
1210 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1211 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
1212 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
1213 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
1216 break;
1217 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1219 #ifndef SQLITE_OMIT_FOREIGN_KEY
1220 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
1221 FKey *pFK;
1222 Table *pTab;
1223 pTab = sqlite3FindTable(db, zRight, zDb);
1224 if( pTab ){
1225 v = sqlite3GetVdbe(pParse);
1226 pFK = pTab->pFKey;
1227 if( pFK ){
1228 static const char *azCol[] = {
1229 "id", "seq", "table", "from", "to", "on_update", "on_delete",
1230 "match"
1232 int i = 0;
1233 pParse->nMem = 8;
1234 sqlite3CodeVerifySchema(pParse, iDb);
1235 setAllColumnNames(v, 8, azCol); assert( 8==ArraySize(azCol) );
1236 while(pFK){
1237 int j;
1238 for(j=0; j<pFK->nCol; j++){
1239 sqlite3VdbeMultiLoad(v, 1, "iissssss",
1242 pFK->zTo,
1243 pTab->aCol[pFK->aCol[j].iFrom].zName,
1244 pFK->aCol[j].zCol,
1245 actionName(pFK->aAction[1]), /* ON UPDATE */
1246 actionName(pFK->aAction[0]), /* ON DELETE */
1247 "NONE");
1248 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
1250 ++i;
1251 pFK = pFK->pNextFrom;
1256 break;
1257 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1259 #ifndef SQLITE_OMIT_FOREIGN_KEY
1260 #ifndef SQLITE_OMIT_TRIGGER
1261 case PragTyp_FOREIGN_KEY_CHECK: {
1262 FKey *pFK; /* A foreign key constraint */
1263 Table *pTab; /* Child table contain "REFERENCES" keyword */
1264 Table *pParent; /* Parent table that child points to */
1265 Index *pIdx; /* Index in the parent table */
1266 int i; /* Loop counter: Foreign key number for pTab */
1267 int j; /* Loop counter: Field of the foreign key */
1268 HashElem *k; /* Loop counter: Next table in schema */
1269 int x; /* result variable */
1270 int regResult; /* 3 registers to hold a result row */
1271 int regKey; /* Register to hold key for checking the FK */
1272 int regRow; /* Registers to hold a row from pTab */
1273 int addrTop; /* Top of a loop checking foreign keys */
1274 int addrOk; /* Jump here if the key is OK */
1275 int *aiCols; /* child to parent column mapping */
1276 static const char *azCol[] = { "table", "rowid", "parent", "fkid" };
1278 regResult = pParse->nMem+1;
1279 pParse->nMem += 4;
1280 regKey = ++pParse->nMem;
1281 regRow = ++pParse->nMem;
1282 v = sqlite3GetVdbe(pParse);
1283 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) );
1284 sqlite3CodeVerifySchema(pParse, iDb);
1285 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1286 while( k ){
1287 if( zRight ){
1288 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1289 k = 0;
1290 }else{
1291 pTab = (Table*)sqliteHashData(k);
1292 k = sqliteHashNext(k);
1294 if( pTab==0 || pTab->pFKey==0 ) continue;
1295 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
1296 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
1297 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
1298 sqlite3VdbeLoadString(v, regResult, pTab->zName);
1299 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1300 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1301 if( pParent==0 ) continue;
1302 pIdx = 0;
1303 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
1304 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
1305 if( x==0 ){
1306 if( pIdx==0 ){
1307 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1308 }else{
1309 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
1310 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
1312 }else{
1313 k = 0;
1314 break;
1317 assert( pParse->nErr>0 || pFK==0 );
1318 if( pFK ) break;
1319 if( pParse->nTab<i ) pParse->nTab = i;
1320 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
1321 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1322 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1323 pIdx = 0;
1324 aiCols = 0;
1325 if( pParent ){
1326 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1327 assert( x==0 );
1329 addrOk = sqlite3VdbeMakeLabel(v);
1330 if( pParent && pIdx==0 ){
1331 int iKey = pFK->aCol[0].iFrom;
1332 assert( iKey>=0 && iKey<pTab->nCol );
1333 if( iKey!=pTab->iPKey ){
1334 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1335 sqlite3ColumnDefault(v, pTab, iKey, regRow);
1336 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
1337 }else{
1338 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
1340 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, 0, regRow); VdbeCoverage(v);
1341 sqlite3VdbeGoto(v, addrOk);
1342 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1343 }else{
1344 for(j=0; j<pFK->nCol; j++){
1345 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
1346 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
1347 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
1349 if( pParent ){
1350 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
1351 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
1352 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
1353 VdbeCoverage(v);
1356 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
1357 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1);
1358 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
1359 sqlite3VdbeResolveLabel(v, addrOk);
1360 sqlite3DbFree(db, aiCols);
1362 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
1363 sqlite3VdbeJumpHere(v, addrTop);
1366 break;
1367 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
1368 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1370 #ifndef NDEBUG
1371 case PragTyp_PARSER_TRACE: {
1372 if( zRight ){
1373 if( sqlite3GetBoolean(zRight, 0) ){
1374 sqlite3ParserTrace(stdout, "parser: ");
1375 }else{
1376 sqlite3ParserTrace(0, 0);
1380 break;
1381 #endif
1383 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1384 ** used will be case sensitive or not depending on the RHS.
1386 case PragTyp_CASE_SENSITIVE_LIKE: {
1387 if( zRight ){
1388 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
1391 break;
1393 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1394 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1395 #endif
1397 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
1398 /* Pragma "quick_check" is reduced version of
1399 ** integrity_check designed to detect most database corruption
1400 ** without most of the overhead of a full integrity-check.
1402 case PragTyp_INTEGRITY_CHECK: {
1403 int i, j, addr, mxErr;
1405 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
1407 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1408 ** then iDb is set to the index of the database identified by <db>.
1409 ** In this case, the integrity of database iDb only is verified by
1410 ** the VDBE created below.
1412 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1413 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1414 ** to -1 here, to indicate that the VDBE should verify the integrity
1415 ** of all attached databases. */
1416 assert( iDb>=0 );
1417 assert( iDb==0 || pId2->z );
1418 if( pId2->z==0 ) iDb = -1;
1420 /* Initialize the VDBE program */
1421 pParse->nMem = 6;
1422 setOneColumnName(v, "integrity_check");
1424 /* Set the maximum error count */
1425 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1426 if( zRight ){
1427 sqlite3GetInt32(zRight, &mxErr);
1428 if( mxErr<=0 ){
1429 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1432 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
1434 /* Do an integrity check on each database file */
1435 for(i=0; i<db->nDb; i++){
1436 HashElem *x;
1437 Hash *pTbls;
1438 int *aRoot;
1439 int cnt = 0;
1440 int mxIdx = 0;
1441 int nIdx;
1443 if( OMIT_TEMPDB && i==1 ) continue;
1444 if( iDb>=0 && i!=iDb ) continue;
1446 sqlite3CodeVerifySchema(pParse, i);
1447 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
1448 VdbeCoverage(v);
1449 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1450 sqlite3VdbeJumpHere(v, addr);
1452 /* Do an integrity check of the B-Tree
1454 ** Begin by finding the root pages numbers
1455 ** for all tables and indices in the database.
1457 assert( sqlite3SchemaMutexHeld(db, i, 0) );
1458 pTbls = &db->aDb[i].pSchema->tblHash;
1459 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1460 Table *pTab = sqliteHashData(x);
1461 Index *pIdx;
1462 if( HasRowid(pTab) ) cnt++;
1463 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
1464 if( nIdx>mxIdx ) mxIdx = nIdx;
1466 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
1467 if( aRoot==0 ) break;
1468 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1469 Table *pTab = sqliteHashData(x);
1470 Index *pIdx;
1471 if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum;
1472 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1473 aRoot[cnt++] = pIdx->tnum;
1476 aRoot[cnt] = 0;
1478 /* Make sure sufficient number of registers have been allocated */
1479 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
1481 /* Do the b-tree integrity checks */
1482 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
1483 sqlite3VdbeChangeP5(v, (u8)i);
1484 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
1485 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
1486 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
1487 P4_DYNAMIC);
1488 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
1489 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
1490 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
1491 sqlite3VdbeJumpHere(v, addr);
1493 /* Make sure all the indices are constructed correctly.
1495 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
1496 Table *pTab = sqliteHashData(x);
1497 Index *pIdx, *pPk;
1498 Index *pPrior = 0;
1499 int loopTop;
1500 int iDataCur, iIdxCur;
1501 int r1 = -1;
1503 if( pTab->pIndex==0 ) continue;
1504 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
1505 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
1506 VdbeCoverage(v);
1507 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1508 sqlite3VdbeJumpHere(v, addr);
1509 sqlite3ExprCacheClear(pParse);
1510 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
1511 1, 0, &iDataCur, &iIdxCur);
1512 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
1513 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1514 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
1516 assert( pParse->nMem>=8+j );
1517 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
1518 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
1519 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
1520 /* Verify that all NOT NULL columns really are NOT NULL */
1521 for(j=0; j<pTab->nCol; j++){
1522 char *zErr;
1523 int jmp2, jmp3;
1524 if( j==pTab->iPKey ) continue;
1525 if( pTab->aCol[j].notNull==0 ) continue;
1526 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1527 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1528 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1529 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1530 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1531 pTab->aCol[j].zName);
1532 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1533 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1534 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1535 sqlite3VdbeAddOp0(v, OP_Halt);
1536 sqlite3VdbeJumpHere(v, jmp2);
1537 sqlite3VdbeJumpHere(v, jmp3);
1539 /* Validate index entries for the current row */
1540 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1541 int jmp2, jmp3, jmp4, jmp5;
1542 int ckUniq = sqlite3VdbeMakeLabel(v);
1543 if( pPk==pIdx ) continue;
1544 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1545 pPrior, r1);
1546 pPrior = pIdx;
1547 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
1548 /* Verify that an index entry exists for the current table row */
1549 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
1550 pIdx->nColumn); VdbeCoverage(v);
1551 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1552 sqlite3VdbeLoadString(v, 3, "row ");
1553 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1554 sqlite3VdbeLoadString(v, 4, " missing from index ");
1555 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1556 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
1557 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1558 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1559 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1560 sqlite3VdbeAddOp0(v, OP_Halt);
1561 sqlite3VdbeJumpHere(v, jmp2);
1562 /* For UNIQUE indexes, verify that only one entry exists with the
1563 ** current key. The entry is unique if (1) any column is NULL
1564 ** or (2) the next entry has a different key */
1565 if( IsUniqueIndex(pIdx) ){
1566 int uniqOk = sqlite3VdbeMakeLabel(v);
1567 int jmp6;
1568 int kk;
1569 for(kk=0; kk<pIdx->nKeyCol; kk++){
1570 int iCol = pIdx->aiColumn[kk];
1571 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
1572 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
1573 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1574 VdbeCoverage(v);
1576 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
1577 sqlite3VdbeGoto(v, uniqOk);
1578 sqlite3VdbeJumpHere(v, jmp6);
1579 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1580 pIdx->nKeyCol); VdbeCoverage(v);
1581 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1582 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1583 sqlite3VdbeGoto(v, jmp5);
1584 sqlite3VdbeResolveLabel(v, uniqOk);
1586 sqlite3VdbeJumpHere(v, jmp4);
1587 sqlite3ResolvePartIdxLabel(pParse, jmp3);
1589 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
1590 sqlite3VdbeJumpHere(v, loopTop-1);
1591 #ifndef SQLITE_OMIT_BTREECOUNT
1592 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
1593 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1594 if( pPk==pIdx ) continue;
1595 addr = sqlite3VdbeCurrentAddr(v);
1596 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
1597 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1598 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
1599 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
1600 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
1601 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
1602 sqlite3VdbeLoadString(v, 3, pIdx->zName);
1603 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1604 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
1606 #endif /* SQLITE_OMIT_BTREECOUNT */
1610 static const int iLn = VDBE_OFFSET_LINENO(2);
1611 static const VdbeOpList endCode[] = {
1612 { OP_AddImm, 1, 0, 0}, /* 0 */
1613 { OP_If, 1, 4, 0}, /* 1 */
1614 { OP_String8, 0, 3, 0}, /* 2 */
1615 { OP_ResultRow, 3, 1, 0}, /* 3 */
1617 VdbeOp *aOp;
1619 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
1620 if( aOp ){
1621 aOp[0].p2 = -mxErr;
1622 aOp[2].p4type = P4_STATIC;
1623 aOp[2].p4.z = "ok";
1627 break;
1628 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1630 #ifndef SQLITE_OMIT_UTF16
1632 ** PRAGMA encoding
1633 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1635 ** In its first form, this pragma returns the encoding of the main
1636 ** database. If the database is not initialized, it is initialized now.
1638 ** The second form of this pragma is a no-op if the main database file
1639 ** has not already been initialized. In this case it sets the default
1640 ** encoding that will be used for the main database file if a new file
1641 ** is created. If an existing main database file is opened, then the
1642 ** default text encoding for the existing database is used.
1644 ** In all cases new databases created using the ATTACH command are
1645 ** created to use the same default text encoding as the main database. If
1646 ** the main database has not been initialized and/or created when ATTACH
1647 ** is executed, this is done before the ATTACH operation.
1649 ** In the second form this pragma sets the text encoding to be used in
1650 ** new database files created using this database handle. It is only
1651 ** useful if invoked immediately after the main database i
1653 case PragTyp_ENCODING: {
1654 static const struct EncName {
1655 char *zName;
1656 u8 enc;
1657 } encnames[] = {
1658 { "UTF8", SQLITE_UTF8 },
1659 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1660 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1661 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
1662 { "UTF16le", SQLITE_UTF16LE },
1663 { "UTF16be", SQLITE_UTF16BE },
1664 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1665 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
1666 { 0, 0 }
1668 const struct EncName *pEnc;
1669 if( !zRight ){ /* "PRAGMA encoding" */
1670 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
1671 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1672 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1673 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1674 returnSingleText(v, "encoding", encnames[ENC(pParse->db)].zName);
1675 }else{ /* "PRAGMA encoding = XXX" */
1676 /* Only change the value of sqlite.enc if the database handle is not
1677 ** initialized. If the main database exists, the new sqlite.enc value
1678 ** will be overwritten when the schema is next loaded. If it does not
1679 ** already exists, it will be created to use the new encoding value.
1681 if(
1682 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1683 DbHasProperty(db, 0, DB_Empty)
1685 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1686 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
1687 SCHEMA_ENC(db) = ENC(db) =
1688 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
1689 break;
1692 if( !pEnc->zName ){
1693 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
1698 break;
1699 #endif /* SQLITE_OMIT_UTF16 */
1701 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
1703 ** PRAGMA [schema.]schema_version
1704 ** PRAGMA [schema.]schema_version = <integer>
1706 ** PRAGMA [schema.]user_version
1707 ** PRAGMA [schema.]user_version = <integer>
1709 ** PRAGMA [schema.]freelist_count
1711 ** PRAGMA [schema.]data_version
1713 ** PRAGMA [schema.]application_id
1714 ** PRAGMA [schema.]application_id = <integer>
1716 ** The pragma's schema_version and user_version are used to set or get
1717 ** the value of the schema-version and user-version, respectively. Both
1718 ** the schema-version and the user-version are 32-bit signed integers
1719 ** stored in the database header.
1721 ** The schema-cookie is usually only manipulated internally by SQLite. It
1722 ** is incremented by SQLite whenever the database schema is modified (by
1723 ** creating or dropping a table or index). The schema version is used by
1724 ** SQLite each time a query is executed to ensure that the internal cache
1725 ** of the schema used when compiling the SQL query matches the schema of
1726 ** the database against which the compiled query is actually executed.
1727 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1728 ** the schema-version is potentially dangerous and may lead to program
1729 ** crashes or database corruption. Use with caution!
1731 ** The user-version is not used internally by SQLite. It may be used by
1732 ** applications for any purpose.
1734 case PragTyp_HEADER_VALUE: {
1735 int iCookie = pPragma->iArg; /* Which cookie to read or write */
1736 sqlite3VdbeUsesBtree(v, iDb);
1737 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
1738 /* Write the specified cookie value */
1739 static const VdbeOpList setCookie[] = {
1740 { OP_Transaction, 0, 1, 0}, /* 0 */
1741 { OP_SetCookie, 0, 0, 0}, /* 1 */
1743 VdbeOp *aOp;
1744 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
1745 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
1746 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
1747 aOp[0].p1 = iDb;
1748 aOp[1].p1 = iDb;
1749 aOp[1].p2 = iCookie;
1750 aOp[1].p3 = sqlite3Atoi(zRight);
1751 }else{
1752 /* Read the specified cookie value */
1753 static const VdbeOpList readCookie[] = {
1754 { OP_Transaction, 0, 0, 0}, /* 0 */
1755 { OP_ReadCookie, 0, 1, 0}, /* 1 */
1756 { OP_ResultRow, 1, 1, 0}
1758 VdbeOp *aOp;
1759 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
1760 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
1761 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
1762 aOp[0].p1 = iDb;
1763 aOp[1].p1 = iDb;
1764 aOp[1].p3 = iCookie;
1765 sqlite3VdbeSetNumCols(v, 1);
1766 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
1767 sqlite3VdbeReusable(v);
1770 break;
1771 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
1773 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1775 ** PRAGMA compile_options
1777 ** Return the names of all compile-time options used in this build,
1778 ** one option per row.
1780 case PragTyp_COMPILE_OPTIONS: {
1781 int i = 0;
1782 const char *zOpt;
1783 pParse->nMem = 1;
1784 setOneColumnName(v, "compile_option");
1785 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1786 sqlite3VdbeLoadString(v, 1, zOpt);
1787 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1789 sqlite3VdbeReusable(v);
1791 break;
1792 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1794 #ifndef SQLITE_OMIT_WAL
1796 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
1798 ** Checkpoint the database.
1800 case PragTyp_WAL_CHECKPOINT: {
1801 static const char *azCol[] = { "busy", "log", "checkpointed" };
1802 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
1803 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1804 if( zRight ){
1805 if( sqlite3StrICmp(zRight, "full")==0 ){
1806 eMode = SQLITE_CHECKPOINT_FULL;
1807 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1808 eMode = SQLITE_CHECKPOINT_RESTART;
1809 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
1810 eMode = SQLITE_CHECKPOINT_TRUNCATE;
1813 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
1814 pParse->nMem = 3;
1815 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
1816 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
1818 break;
1821 ** PRAGMA wal_autocheckpoint
1822 ** PRAGMA wal_autocheckpoint = N
1824 ** Configure a database connection to automatically checkpoint a database
1825 ** after accumulating N frames in the log. Or query for the current value
1826 ** of N.
1828 case PragTyp_WAL_AUTOCHECKPOINT: {
1829 if( zRight ){
1830 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
1832 returnSingleInt(v, "wal_autocheckpoint",
1833 db->xWalCallback==sqlite3WalDefaultHook ?
1834 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
1836 break;
1837 #endif
1840 ** PRAGMA shrink_memory
1842 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
1843 ** connection on which it is invoked to free up as much memory as it
1844 ** can, by calling sqlite3_db_release_memory().
1846 case PragTyp_SHRINK_MEMORY: {
1847 sqlite3_db_release_memory(db);
1848 break;
1852 ** PRAGMA busy_timeout
1853 ** PRAGMA busy_timeout = N
1855 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
1856 ** if one is set. If no busy handler or a different busy handler is set
1857 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1858 ** disables the timeout.
1860 /*case PragTyp_BUSY_TIMEOUT*/ default: {
1861 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
1862 if( zRight ){
1863 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1865 returnSingleInt(v, "timeout", db->busyTimeout);
1866 break;
1870 ** PRAGMA soft_heap_limit
1871 ** PRAGMA soft_heap_limit = N
1873 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
1874 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
1875 ** specified and is a non-negative integer.
1876 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
1877 ** returns the same integer that would be returned by the
1878 ** sqlite3_soft_heap_limit64(-1) C-language function.
1880 case PragTyp_SOFT_HEAP_LIMIT: {
1881 sqlite3_int64 N;
1882 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
1883 sqlite3_soft_heap_limit64(N);
1885 returnSingleInt(v, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
1886 break;
1890 ** PRAGMA threads
1891 ** PRAGMA threads = N
1893 ** Configure the maximum number of worker threads. Return the new
1894 ** maximum, which might be less than requested.
1896 case PragTyp_THREADS: {
1897 sqlite3_int64 N;
1898 if( zRight
1899 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
1900 && N>=0
1902 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
1904 returnSingleInt(v, "threads",
1905 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
1906 break;
1909 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
1911 ** Report the current state of file logs for all databases
1913 case PragTyp_LOCK_STATUS: {
1914 static const char *const azLockName[] = {
1915 "unlocked", "shared", "reserved", "pending", "exclusive"
1917 static const char *azCol[] = { "database", "status" };
1918 int i;
1919 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
1920 pParse->nMem = 2;
1921 for(i=0; i<db->nDb; i++){
1922 Btree *pBt;
1923 const char *zState = "unknown";
1924 int j;
1925 if( db->aDb[i].zName==0 ) continue;
1926 pBt = db->aDb[i].pBt;
1927 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
1928 zState = "closed";
1929 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
1930 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1931 zState = azLockName[j];
1933 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zName, zState);
1934 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
1936 break;
1938 #endif
1940 #ifdef SQLITE_HAS_CODEC
1941 case PragTyp_KEY: {
1942 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1943 break;
1945 case PragTyp_REKEY: {
1946 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
1947 break;
1949 case PragTyp_HEXKEY: {
1950 if( zRight ){
1951 u8 iByte;
1952 int i;
1953 char zKey[40];
1954 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
1955 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
1956 if( (i&1)!=0 ) zKey[i/2] = iByte;
1958 if( (zLeft[3] & 0xf)==0xb ){
1959 sqlite3_key_v2(db, zDb, zKey, i/2);
1960 }else{
1961 sqlite3_rekey_v2(db, zDb, zKey, i/2);
1964 break;
1966 #endif
1967 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
1968 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
1969 #ifdef SQLITE_HAS_CODEC
1970 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
1971 sqlite3_activate_see(&zRight[4]);
1973 #endif
1974 #ifdef SQLITE_ENABLE_CEROD
1975 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
1976 sqlite3_activate_cerod(&zRight[6]);
1978 #endif
1980 break;
1981 #endif
1983 } /* End of the PRAGMA switch */
1985 pragma_out:
1986 sqlite3DbFree(db, zLeft);
1987 sqlite3DbFree(db, zRight);
1990 #endif /* SQLITE_OMIT_PRAGMA */