monotone

monotone Mtn Source Tree

Root/sqlite/pragma.c

1/*
2** 2003 April 6
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code used to implement the PRAGMA command.
13**
14** $Id: pragma.c,v 1.1 2003/08/05 23:03:07 graydon Exp $
15*/
16#include "sqliteInt.h"
17#include <ctype.h>
18
19/*
20** Interpret the given string as a boolean value.
21*/
22static int getBoolean(char *z){
23 static char *azTrue[] = { "yes", "on", "true" };
24 int i;
25 if( z[0]==0 ) return 0;
26 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
27 return atoi(z);
28 }
29 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
30 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
31 }
32 return 0;
33}
34
35/*
36** Interpret the given string as a safety level. Return 0 for OFF,
37** 1 for ON or NORMAL and 2 for FULL.
38**
39** Note that the values returned are one less that the values that
40** should be passed into sqliteBtreeSetSafetyLevel(). 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*/
44static int getSafetyLevel(char *z){
45 static const struct {
46 const char *zWord;
47 int val;
48 } aKey[] = {
49 { "no", 0 },
50 { "off", 0 },
51 { "false", 0 },
52 { "yes", 1 },
53 { "on", 1 },
54 { "true", 1 },
55 { "full", 2 },
56 };
57 int i;
58 if( z[0]==0 ) return 1;
59 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
60 return atoi(z);
61 }
62 for(i=0; i<sizeof(aKey)/sizeof(aKey[0]); i++){
63 if( sqliteStrICmp(z,aKey[i].zWord)==0 ) return aKey[i].val;
64 }
65 return 1;
66}
67
68/*
69** Interpret the given string as a temp db location. Return 1 for file
70** backed temporary databases, 2 for the Red-Black tree in memory database
71** and 0 to use the compile-time default.
72*/
73static int getTempStore(char *z){
74 if( z[0]>='0' || z[0]<='2' ){
75 return z[0] - '0';
76 }else if( sqliteStrICmp(z, "file")==0 ){
77 return 1;
78 }else if( sqliteStrICmp(z, "memory")==0 ){
79 return 2;
80 }else{
81 return 0;
82 }
83}
84
85/*
86** Process a pragma statement.
87**
88** Pragmas are of this form:
89**
90** PRAGMA id = value
91**
92** The identifier might also be a string. The value is a string, and
93** identifier, or a number. If minusFlag is true, then the value is
94** a number that was preceded by a minus sign.
95*/
96void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
97 char *zLeft = 0;
98 char *zRight = 0;
99 sqlite *db = pParse->db;
100 Vdbe *v = sqliteGetVdbe(pParse);
101 if( v==0 ) return;
102
103 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
104 sqliteDequote(zLeft);
105 if( minusFlag ){
106 zRight = 0;
107 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
108 }else{
109 zRight = sqliteStrNDup(pRight->z, pRight->n);
110 sqliteDequote(zRight);
111 }
112 if( sqliteAuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, 0) ){
113 sqliteFree(zLeft);
114 sqliteFree(zRight);
115 return;
116 }
117
118 /*
119 ** PRAGMA default_cache_size
120 ** PRAGMA default_cache_size=N
121 **
122 ** The first form reports the current persistent setting for the
123 ** page cache size. The value returned is the maximum number of
124 ** pages in the page cache. The second form sets both the current
125 ** page cache size value and the persistent page cache size value
126 ** stored in the database file.
127 **
128 ** The default cache size is stored in meta-value 2 of page 1 of the
129 ** database file. The cache size is actually the absolute value of
130 ** this memory location. The sign of meta-value 2 determines the
131 ** synchronous setting. A negative value means synchronous is off
132 ** and a positive value means synchronous is on.
133 */
134 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
135 static VdbeOp getCacheSize[] = {
136 { OP_ReadCookie, 0, 2, 0},
137 { OP_AbsValue, 0, 0, 0},
138 { OP_Dup, 0, 0, 0},
139 { OP_Integer, 0, 0, 0},
140 { OP_Ne, 0, 6, 0},
141 { OP_Integer, MAX_PAGES,0, 0},
142 { OP_ColumnName, 0, 0, "cache_size"},
143 { OP_Callback, 1, 0, 0},
144 };
145 if( pRight->z==pLeft->z ){
146 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
147 }else{
148 int addr;
149 int size = atoi(zRight);
150 if( size<0 ) size = -size;
151 sqliteBeginWriteOperation(pParse, 0, 0);
152 sqliteVdbeAddOp(v, OP_Integer, size, 0);
153 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
154 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
155 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
156 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
157 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
158 sqliteEndWriteOperation(pParse);
159 db->cache_size = db->cache_size<0 ? -size : size;
160 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
161 }
162 }else
163
164 /*
165 ** PRAGMA cache_size
166 ** PRAGMA cache_size=N
167 **
168 ** The first form reports the current local setting for the
169 ** page cache size. The local setting can be different from
170 ** the persistent cache size value that is stored in the database
171 ** file itself. The value returned is the maximum number of
172 ** pages in the page cache. The second form sets the local
173 ** page cache size value. It does not change the persistent
174 ** cache size stored on the disk so the cache size will revert
175 ** to its default value when the database is closed and reopened.
176 ** N should be a positive integer.
177 */
178 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
179 static VdbeOp getCacheSize[] = {
180 { OP_ColumnName, 0, 0, "cache_size"},
181 { OP_Callback, 1, 0, 0},
182 };
183 if( pRight->z==pLeft->z ){
184 int size = db->cache_size;;
185 if( size<0 ) size = -size;
186 sqliteVdbeAddOp(v, OP_Integer, size, 0);
187 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
188 }else{
189 int size = atoi(zRight);
190 if( size<0 ) size = -size;
191 if( db->cache_size<0 ) size = -size;
192 db->cache_size = size;
193 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
194 }
195 }else
196
197 /*
198 ** PRAGMA default_synchronous
199 ** PRAGMA default_synchronous=ON|OFF|NORMAL|FULL
200 **
201 ** The first form returns the persistent value of the "synchronous" setting
202 ** that is stored in the database. This is the synchronous setting that
203 ** is used whenever the database is opened unless overridden by a separate
204 ** "synchronous" pragma. The second form changes the persistent and the
205 ** local synchronous setting to the value given.
206 **
207 ** If synchronous is OFF, SQLite does not attempt any fsync() systems calls
208 ** to make sure data is committed to disk. Write operations are very fast,
209 ** but a power failure can leave the database in an inconsistent state.
210 ** If synchronous is ON or NORMAL, SQLite will do an fsync() system call to
211 ** make sure data is being written to disk. The risk of corruption due to
212 ** a power loss in this mode is negligible but non-zero. If synchronous
213 ** is FULL, extra fsync()s occur to reduce the risk of corruption to near
214 ** zero, but with a write performance penalty. The default mode is NORMAL.
215 */
216 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
217 static VdbeOp getSync[] = {
218 { OP_ColumnName, 0, 0, "synchronous"},
219 { OP_ReadCookie, 0, 3, 0},
220 { OP_Dup, 0, 0, 0},
221 { OP_If, 0, 0, 0}, /* 3 */
222 { OP_ReadCookie, 0, 2, 0},
223 { OP_Integer, 0, 0, 0},
224 { OP_Lt, 0, 5, 0},
225 { OP_AddImm, 1, 0, 0},
226 { OP_Callback, 1, 0, 0},
227 { OP_Halt, 0, 0, 0},
228 { OP_AddImm, -1, 0, 0}, /* 10 */
229 { OP_Callback, 1, 0, 0}
230 };
231 if( pRight->z==pLeft->z ){
232 int addr = sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
233 sqliteVdbeChangeP2(v, addr+3, addr+10);
234 }else{
235 int addr;
236 int size = db->cache_size;
237 if( size<0 ) size = -size;
238 sqliteBeginWriteOperation(pParse, 0, 0);
239 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
240 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
241 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
242 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
243 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
244 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
245 db->safety_level = getSafetyLevel(zRight)+1;
246 if( db->safety_level==1 ){
247 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
248 size = -size;
249 }
250 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
251 sqliteVdbeAddOp(v, OP_Integer, db->safety_level, 0);
252 sqliteVdbeAddOp(v, OP_SetCookie, 0, 3);
253 sqliteEndWriteOperation(pParse);
254 db->cache_size = size;
255 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
256 sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
257 }
258 }else
259
260 /*
261 ** PRAGMA synchronous
262 ** PRAGMA synchronous=OFF|ON|NORMAL|FULL
263 **
264 ** Return or set the local value of the synchronous flag. Changing
265 ** the local value does not make changes to the disk file and the
266 ** default value will be restored the next time the database is
267 ** opened.
268 */
269 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
270 static VdbeOp getSync[] = {
271 { OP_ColumnName, 0, 0, "synchronous"},
272 { OP_Callback, 1, 0, 0},
273 };
274 if( pRight->z==pLeft->z ){
275 sqliteVdbeAddOp(v, OP_Integer, db->safety_level-1, 0);
276 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
277 }else{
278 int size = db->cache_size;
279 if( size<0 ) size = -size;
280 db->safety_level = getSafetyLevel(zRight)+1;
281 if( db->safety_level==1 ) size = -size;
282 db->cache_size = size;
283 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
284 sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
285 }
286 }else
287
288 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
289 if( getBoolean(zRight) ){
290 always_code_trigger_setup = 1;
291 }else{
292 always_code_trigger_setup = 0;
293 }
294 }else
295
296 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
297 if( getBoolean(zRight) ){
298 db->flags |= SQLITE_VdbeTrace;
299 }else{
300 db->flags &= ~SQLITE_VdbeTrace;
301 }
302 }else
303
304 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
305 if( getBoolean(zRight) ){
306 db->flags |= SQLITE_FullColNames;
307 }else{
308 db->flags &= ~SQLITE_FullColNames;
309 }
310 }else
311
312 if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
313 if( getBoolean(zRight) ){
314 db->flags |= SQLITE_ReportTypes;
315 }else{
316 db->flags &= ~SQLITE_ReportTypes;
317 }
318 }else
319
320 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
321 if( getBoolean(zRight) ){
322 db->flags |= SQLITE_CountRows;
323 }else{
324 db->flags &= ~SQLITE_CountRows;
325 }
326 }else
327
328 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
329 if( getBoolean(zRight) ){
330 db->flags |= SQLITE_NullCallback;
331 }else{
332 db->flags &= ~SQLITE_NullCallback;
333 }
334 }else
335
336 if( sqliteStrICmp(zLeft, "table_info")==0 ){
337 Table *pTab;
338 pTab = sqliteFindTable(db, zRight, 0);
339 if( pTab ){
340 static VdbeOp tableInfoPreface[] = {
341 { OP_ColumnName, 0, 0, "cid"},
342 { OP_ColumnName, 1, 0, "name"},
343 { OP_ColumnName, 2, 0, "type"},
344 { OP_ColumnName, 3, 0, "notnull"},
345 { OP_ColumnName, 4, 0, "dflt_value"},
346 };
347 int i;
348 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
349 sqliteViewGetColumnNames(pParse, pTab);
350 for(i=0; i<pTab->nCol; i++){
351 sqliteVdbeAddOp(v, OP_Integer, i, 0);
352 sqliteVdbeAddOp(v, OP_String, 0, 0);
353 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
354 sqliteVdbeAddOp(v, OP_String, 0, 0);
355 sqliteVdbeChangeP3(v, -1,
356 pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", P3_STATIC);
357 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
358 sqliteVdbeAddOp(v, OP_String, 0, 0);
359 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
360 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
361 }
362 }
363 }else
364
365 if( sqliteStrICmp(zLeft, "index_info")==0 ){
366 Index *pIdx;
367 Table *pTab;
368 pIdx = sqliteFindIndex(db, zRight, 0);
369 if( pIdx ){
370 static VdbeOp tableInfoPreface[] = {
371 { OP_ColumnName, 0, 0, "seqno"},
372 { OP_ColumnName, 1, 0, "cid"},
373 { OP_ColumnName, 2, 0, "name"},
374 };
375 int i;
376 pTab = pIdx->pTable;
377 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
378 for(i=0; i<pIdx->nColumn; i++){
379 int cnum = pIdx->aiColumn[i];
380 sqliteVdbeAddOp(v, OP_Integer, i, 0);
381 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
382 sqliteVdbeAddOp(v, OP_String, 0, 0);
383 assert( pTab->nCol>cnum );
384 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
385 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
386 }
387 }
388 }else
389
390 if( sqliteStrICmp(zLeft, "index_list")==0 ){
391 Index *pIdx;
392 Table *pTab;
393 pTab = sqliteFindTable(db, zRight, 0);
394 if( pTab ){
395 v = sqliteGetVdbe(pParse);
396 pIdx = pTab->pIndex;
397 }
398 if( pTab && pIdx ){
399 int i = 0;
400 static VdbeOp indexListPreface[] = {
401 { OP_ColumnName, 0, 0, "seq"},
402 { OP_ColumnName, 1, 0, "name"},
403 { OP_ColumnName, 2, 0, "unique"},
404 };
405
406 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
407 while(pIdx){
408 sqliteVdbeAddOp(v, OP_Integer, i, 0);
409 sqliteVdbeAddOp(v, OP_String, 0, 0);
410 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
411 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
412 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
413 ++i;
414 pIdx = pIdx->pNext;
415 }
416 }
417 }else
418
419 if( sqliteStrICmp(zLeft, "database_list")==0 ){
420 int i;
421 static VdbeOp indexListPreface[] = {
422 { OP_ColumnName, 0, 0, "seq"},
423 { OP_ColumnName, 1, 0, "name"},
424 };
425
426 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
427 for(i=0; i<db->nDb; i++){
428 if( db->aDb[i].pBt==0 ) continue;
429 assert( db->aDb[i].zName!=0 );
430 sqliteVdbeAddOp(v, OP_Integer, i, 0);
431 sqliteVdbeAddOp(v, OP_String, 0, 0);
432 sqliteVdbeChangeP3(v, -1, db->aDb[i].zName, P3_STATIC);
433 sqliteVdbeAddOp(v, OP_Callback, 2, 0);
434 }
435 }else
436 /*
437 ** PRAGMA temp_store
438 ** PRAGMA temp_store = "default"|"memory"|"file"
439 **
440 ** Return or set the local value of the temp_store flag. Changing
441 ** the local value does not make changes to the disk file and the default
442 ** value will be restored the next time the database is opened.
443 **
444 ** Note that it is possible for the library compile-time options to
445 ** override this setting
446 */
447 if( sqliteStrICmp(zLeft, "temp_store")==0 ){
448 static VdbeOp getTmpDbLoc[] = {
449 { OP_ColumnName, 0, 0, "temp_store"},
450 { OP_Callback, 1, 0, 0},
451 };
452 if( pRight->z==pLeft->z ){
453 sqliteVdbeAddOp(v, OP_Integer, db->temp_store, 0);
454 sqliteVdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
455 }else{
456 if (&db->aDb[1].pBt != 0) {
457sqliteErrorMsg(pParse, "The temporary database already exists - "
458 "its location cannot now be changed");
459 } else {
460db->temp_store = getTempStore(zRight);
461 }
462 }
463 }else
464
465 /*
466 ** PRAGMA default_temp_store
467 ** PRAGMA default_temp_store = "default"|"memory"|"file"
468 **
469 ** Return or set the value of the persistent temp_store flag (as
470 ** well as the value currently in force).
471 **
472 ** Note that it is possible for the library compile-time options to
473 ** override this setting
474 */
475 if( sqliteStrICmp(zLeft, "default_temp_store")==0 ){
476 static VdbeOp getTmpDbLoc[] = {
477 { OP_ColumnName, 0, 0, "temp_store"},
478 { OP_ReadCookie, 0, 5, 0},
479 { OP_Callback, 1, 0, 0}};
480 if( pRight->z==pLeft->z ){
481 sqliteVdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
482 }else{
483 if (&db->aDb[1].pBt != 0) {
484sqliteErrorMsg(pParse, "The temporary database already exists - "
485 "its location cannot now be changed");
486 } else {
487sqliteBeginWriteOperation(pParse, 0, 0);
488db->temp_store = getTempStore(zRight);
489sqliteVdbeAddOp(v, OP_Integer, db->temp_store, 0);
490sqliteVdbeAddOp(v, OP_SetCookie, 0, 5);
491sqliteEndWriteOperation(pParse);
492 }
493 }
494 }else
495
496#ifndef NDEBUG
497 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
498 extern void sqliteParserTrace(FILE*, char *);
499 if( getBoolean(zRight) ){
500 sqliteParserTrace(stdout, "parser: ");
501 }else{
502 sqliteParserTrace(0, 0);
503 }
504 }else
505#endif
506
507 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
508 static VdbeOp checkDb[] = {
509 { OP_SetInsert, 0, 0, "2"},
510 { OP_Integer, 0, 0, 0},
511 { OP_OpenRead, 0, 2, 0},
512 { OP_Rewind, 0, 7, 0},
513 { OP_Column, 0, 3, 0}, /* 4 */
514 { OP_SetInsert, 0, 0, 0},
515 { OP_Next, 0, 4, 0},
516 { OP_IntegrityCk, 0, 0, 0}, /* 7 */
517 { OP_ColumnName, 0, 0, "integrity_check"},
518 { OP_Callback, 1, 0, 0},
519 { OP_SetInsert, 1, 0, "2"},
520 { OP_Integer, 1, 0, 0},
521 { OP_OpenRead, 1, 2, 0},
522 { OP_Rewind, 1, 17, 0},
523 { OP_Column, 1, 3, 0}, /* 14 */
524 { OP_SetInsert, 1, 0, 0},
525 { OP_Next, 1, 14, 0},
526 { OP_IntegrityCk, 1, 1, 0}, /* 17 */
527 { OP_Callback, 1, 0, 0},
528 };
529 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
530 }else
531
532 {}
533 sqliteFree(zLeft);
534 sqliteFree(zRight);
535}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status