Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: third_party/sqlite/sqlite-src-3100200/src/pragma.c

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

Powered by Google App Engine
This is Rietveld 408576698