PRAGMA cipher_default_settings queries current default SQLCipher options
[sqlcipher.git] / tool / mkpragmatab.tcl
blob5b1f2eea93898c8e55965bdc4276493c4b667f20
1 #!/usr/bin/tclsh
3 # Run this script to generate the pragma name lookup table C code.
5 # To add new pragmas, first add the name and other relevant attributes
6 # of the pragma to the "pragma_def" object below. Then run this script
7 # to generate the ../src/pragma.h header file that contains macros and
8 # the lookup table needed for pragma name lookup in the pragma.c module.
9 # Then add the extra "case PragTyp_XXXXX:" and subsequent code for the
10 # new pragma in ../src/pragma.c.
13 # Flag meanings:
14 set flagMeaning(NeedSchema) {Force schema load before running}
15 set flagMeaning(ReadOnly) {Read-only HEADER_VALUE}
16 set flagMeaning(Result0) {Acts as query when no argument}
17 set flagMeaning(Result1) {Acts as query when has one argument}
18 set flagMeaning(SchemaReq) {Schema required - "main" is default}
19 set flagMeaning(SchemaOpt) {Schema restricts name search if present}
20 set flagMeaning(NoColumns) {OP_ResultRow called with zero columns}
21 set flagMeaning(NoColumns1) {zero columns if RHS argument is present}
23 set pragma_def {
24 NAME: full_column_names
25 TYPE: FLAG
26 ARG: SQLITE_FullColNames
27 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
29 NAME: short_column_names
30 TYPE: FLAG
31 ARG: SQLITE_ShortColNames
32 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
34 NAME: count_changes
35 TYPE: FLAG
36 ARG: SQLITE_CountRows
37 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
39 NAME: empty_result_callbacks
40 TYPE: FLAG
41 ARG: SQLITE_NullCallback
42 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
44 NAME: legacy_file_format
45 TYPE: FLAG
46 ARG: SQLITE_LegacyFileFmt
47 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
49 NAME: fullfsync
50 TYPE: FLAG
51 ARG: SQLITE_FullFSync
52 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
54 NAME: checkpoint_fullfsync
55 TYPE: FLAG
56 ARG: SQLITE_CkptFullFSync
57 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
59 NAME: cache_spill
60 FLAG: Result0 SchemaReq NoColumns1
61 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
63 NAME: reverse_unordered_selects
64 TYPE: FLAG
65 ARG: SQLITE_ReverseOrder
66 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
68 NAME: query_only
69 TYPE: FLAG
70 ARG: SQLITE_QueryOnly
71 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
73 NAME: automatic_index
74 TYPE: FLAG
75 ARG: SQLITE_AutoIndex
76 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
77 IF: !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
79 NAME: sql_trace
80 TYPE: FLAG
81 ARG: SQLITE_SqlTrace
82 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
83 IF: defined(SQLITE_DEBUG)
85 NAME: vdbe_listing
86 TYPE: FLAG
87 ARG: SQLITE_VdbeListing
88 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
89 IF: defined(SQLITE_DEBUG)
91 NAME: vdbe_trace
92 TYPE: FLAG
93 ARG: SQLITE_VdbeTrace
94 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
95 IF: defined(SQLITE_DEBUG)
97 NAME: vdbe_addoptrace
98 TYPE: FLAG
99 ARG: SQLITE_VdbeAddopTrace
100 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
101 IF: defined(SQLITE_DEBUG)
103 NAME: vdbe_debug
104 TYPE: FLAG
105 ARG: SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace
106 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
107 IF: defined(SQLITE_DEBUG)
109 NAME: vdbe_eqp
110 TYPE: FLAG
111 ARG: SQLITE_VdbeEQP
112 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
113 IF: defined(SQLITE_DEBUG)
115 NAME: ignore_check_constraints
116 TYPE: FLAG
117 ARG: SQLITE_IgnoreChecks
118 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119 IF: !defined(SQLITE_OMIT_CHECK)
121 NAME: writable_schema
122 TYPE: FLAG
123 ARG: SQLITE_WriteSchema|SQLITE_NoSchemaError
124 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
126 NAME: read_uncommitted
127 TYPE: FLAG
128 ARG: SQLITE_ReadUncommit
129 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131 NAME: recursive_triggers
132 TYPE: FLAG
133 ARG: SQLITE_RecTriggers
134 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
136 NAME: foreign_keys
137 TYPE: FLAG
138 ARG: SQLITE_ForeignKeys
139 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140 IF: !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
142 NAME: defer_foreign_keys
143 TYPE: FLAG
144 ARG: SQLITE_DeferFKs
145 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
146 IF: !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
148 NAME: cell_size_check
149 TYPE: FLAG
150 ARG: SQLITE_CellSizeCk
152 NAME: default_cache_size
153 FLAG: NeedSchema Result0 SchemaReq NoColumns1
154 COLS: cache_size
155 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
157 NAME: page_size
158 FLAG: Result0 SchemaReq NoColumns1
159 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
161 NAME: secure_delete
162 FLAG: Result0
163 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
165 NAME: page_count
166 FLAG: NeedSchema Result0 SchemaReq
167 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
169 NAME: max_page_count
170 TYPE: PAGE_COUNT
171 FLAG: NeedSchema Result0 SchemaReq
172 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
174 NAME: locking_mode
175 FLAG: Result0 SchemaReq
176 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
178 NAME: journal_mode
179 FLAG: NeedSchema Result0 SchemaReq
180 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
182 NAME: journal_size_limit
183 FLAG: Result0 SchemaReq
184 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
186 NAME: cache_size
187 FLAG: NeedSchema Result0 SchemaReq NoColumns1
188 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
190 NAME: mmap_size
191 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
193 NAME: auto_vacuum
194 FLAG: NeedSchema Result0 SchemaReq NoColumns1
195 IF: !defined(SQLITE_OMIT_AUTOVACUUM)
197 NAME: incremental_vacuum
198 FLAG: NeedSchema NoColumns
199 IF: !defined(SQLITE_OMIT_AUTOVACUUM)
201 NAME: temp_store
202 FLAG: Result0 NoColumns1
203 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
205 NAME: temp_store_directory
206 FLAG: NoColumns1
207 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
209 NAME: data_store_directory
210 FLAG: NoColumns1
211 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
213 NAME: lock_proxy_file
214 FLAG: NoColumns1
215 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
217 NAME: synchronous
218 FLAG: NeedSchema Result0 SchemaReq NoColumns1
219 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS)
221 NAME: table_info
222 FLAG: NeedSchema Result1 SchemaOpt
223 ARG: 0
224 COLS: cid name type notnull dflt_value pk
225 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
227 NAME: table_xinfo
228 TYPE: TABLE_INFO
229 FLAG: NeedSchema Result1 SchemaOpt
230 ARG: 1
231 COLS: cid name type notnull dflt_value pk hidden
232 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
234 NAME: stats
235 FLAG: NeedSchema Result0 SchemaReq
236 COLS: tbl idx wdth hght flgs
237 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
239 NAME: index_info
240 TYPE: INDEX_INFO
241 ARG: 0
242 FLAG: NeedSchema Result1 SchemaOpt
243 COLS: seqno cid name
244 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
246 NAME: index_xinfo
247 TYPE: INDEX_INFO
248 ARG: 1
249 FLAG: NeedSchema Result1 SchemaOpt
250 COLS: seqno cid name desc coll key
251 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
253 NAME: index_list
254 FLAG: NeedSchema Result1 SchemaOpt
255 COLS: seq name unique origin partial
256 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
258 NAME: database_list
259 FLAG: NeedSchema Result0
260 COLS: seq name file
261 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
263 NAME: function_list
264 FLAG: Result0
265 COLS: name builtin
266 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
267 IF: defined(SQLITE_INTROSPECTION_PRAGMAS)
269 NAME: module_list
270 FLAG: Result0
271 COLS: name
272 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
273 IF: !defined(SQLITE_OMIT_VIRTUALTABLE)
274 IF: defined(SQLITE_INTROSPECTION_PRAGMAS)
276 NAME: pragma_list
277 FLAG: Result0
278 COLS: name
279 IF: defined(SQLITE_INTROSPECTION_PRAGMAS)
281 NAME: collation_list
282 FLAG: Result0
283 COLS: seq name
284 IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
286 NAME: foreign_key_list
287 FLAG: NeedSchema Result1 SchemaOpt
288 COLS: id seq table from to on_update on_delete match
289 IF: !defined(SQLITE_OMIT_FOREIGN_KEY)
291 NAME: foreign_key_check
292 FLAG: NeedSchema Result0
293 COLS: table rowid parent fkid
294 IF: !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
296 NAME: parser_trace
297 IF: defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE)
299 NAME: case_sensitive_like
300 FLAG: NoColumns
302 NAME: integrity_check
303 FLAG: NeedSchema Result0 Result1
304 IF: !defined(SQLITE_OMIT_INTEGRITY_CHECK)
306 NAME: quick_check
307 TYPE: INTEGRITY_CHECK
308 FLAG: NeedSchema Result0 Result1
309 IF: !defined(SQLITE_OMIT_INTEGRITY_CHECK)
311 NAME: encoding
312 FLAG: Result0 NoColumns1
313 IF: !defined(SQLITE_OMIT_UTF16)
315 NAME: schema_version
316 TYPE: HEADER_VALUE
317 ARG: BTREE_SCHEMA_VERSION
318 FLAG: NoColumns1 Result0
319 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
321 NAME: user_version
322 TYPE: HEADER_VALUE
323 ARG: BTREE_USER_VERSION
324 FLAG: NoColumns1 Result0
325 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
327 NAME: data_version
328 TYPE: HEADER_VALUE
329 ARG: BTREE_DATA_VERSION
330 FLAG: ReadOnly Result0
331 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
333 NAME: freelist_count
334 TYPE: HEADER_VALUE
335 ARG: BTREE_FREE_PAGE_COUNT
336 FLAG: ReadOnly Result0
337 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
339 NAME: application_id
340 TYPE: HEADER_VALUE
341 ARG: BTREE_APPLICATION_ID
342 FLAG: NoColumns1 Result0
343 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
345 NAME: compile_options
346 FLAG: Result0
347 IF: !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
349 NAME: wal_checkpoint
350 FLAG: NeedSchema
351 COLS: busy log checkpointed
352 IF: !defined(SQLITE_OMIT_WAL)
354 NAME: wal_autocheckpoint
355 IF: !defined(SQLITE_OMIT_WAL)
357 NAME: shrink_memory
358 FLAG: NoColumns
360 NAME: busy_timeout
361 FLAG: Result0
362 COLS: timeout
364 NAME: lock_status
365 FLAG: Result0
366 COLS: database status
367 IF: defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
369 NAME: key
370 TYPE: KEY
371 ARG: 0
372 IF: defined(SQLITE_HAS_CODEC)
374 NAME: rekey
375 TYPE: KEY
376 ARG: 1
377 IF: defined(SQLITE_HAS_CODEC)
379 NAME: hexkey
380 TYPE: HEXKEY
381 ARG: 2
382 IF: defined(SQLITE_HAS_CODEC)
384 NAME: hexrekey
385 TYPE: HEXKEY
386 ARG: 3
387 IF: defined(SQLITE_HAS_CODEC)
389 NAME: textkey
390 TYPE: KEY
391 ARG: 4
392 IF: defined(SQLITE_HAS_CODEC)
394 NAME: textrekey
395 TYPE: KEY
396 ARG: 5
397 IF: defined(SQLITE_HAS_CODEC)
399 NAME: activate_extensions
400 IF: defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
402 NAME: soft_heap_limit
403 FLAG: Result0
405 NAME: threads
406 FLAG: Result0
408 NAME: optimize
409 FLAG: Result1 NeedSchema
411 NAME: legacy_alter_table
412 TYPE: FLAG
413 ARG: SQLITE_LegacyAlter
414 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS)
417 # Open the output file
419 set destfile "[file dir [file dir [file normal $argv0]]]/src/pragma.h"
420 puts "Overwriting $destfile with new pragma table..."
421 set fd [open $destfile wb]
422 puts $fd {/* DO NOT EDIT!
423 ** This file is automatically generated by the script at
424 ** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
425 ** that script and rerun it.
428 # Parse the PRAGMA table above.
430 set name {}
431 set type {}
432 set if {}
433 set flags {}
434 set cols {}
435 set cols_list {}
436 set arg 0
437 proc record_one {} {
438 global name type if arg allbyname typebyif flags cols all_cols
439 global cols_list colUsedBy
440 if {$name==""} return
441 if {$cols!=""} {
442 if {![info exists all_cols($cols)]} {
443 set all_cols($cols) 1
444 lappend cols_list $cols
446 set cx $cols
447 lappend colUsedBy($cols) $name
448 } else {
449 set cx 0
451 set allbyname($name) [list $type $arg $if $flags $cols]
452 set name {}
453 set type {}
454 set if {}
455 set flags {}
456 set cols {}
457 set arg 0
459 foreach line [split $pragma_def \n] {
460 set line [string trim $line]
461 if {$line==""} continue
462 foreach {id val} [split $line :] break
463 set val [string trim $val]
464 if {$id=="NAME"} {
465 record_one
466 set name $val
467 set type [string toupper $val]
468 } elseif {$id=="TYPE"} {
469 set type $val
470 if {$type=="FLAG"} {
471 lappend flags Result0 NoColumns1
473 } elseif {$id=="ARG"} {
474 set arg $val
475 } elseif {$id=="COLS"} {
476 set cols $val
477 } elseif {$id=="IF"} {
478 lappend if $val
479 } elseif {$id=="FLAG"} {
480 foreach term [split $val] {
481 lappend flags $term
482 set allflags($term) 1
484 } else {
485 error "bad pragma_def line: $line"
488 record_one
489 set allnames [lsort [array names allbyname]]
491 # Generate #defines for all pragma type names. Group the pragmas that are
492 # omit in default builds (defined(SQLITE_DEBUG) and defined(SQLITE_HAS_CODEC))
493 # at the end.
495 puts $fd "\n/* The various pragma types */"
496 set pnum 0
497 foreach name $allnames {
498 set type [lindex $allbyname($name) 0]
499 if {[info exists seentype($type)]} continue
500 set if [lindex $allbyname($name) 2]
501 if {[regexp SQLITE_DEBUG $if] || [regexp SQLITE_HAS_CODEC $if]} continue
502 set seentype($type) 1
503 puts $fd [format {#define %-35s %4d} PragTyp_$type $pnum]
504 incr pnum
506 foreach name $allnames {
507 set type [lindex $allbyname($name) 0]
508 if {[info exists seentype($type)]} continue
509 set if [lindex $allbyname($name) 2]
510 if {[regexp SQLITE_DEBUG $if]} continue
511 set seentype($type) 1
512 puts $fd [format {#define %-35s %4d} PragTyp_$type $pnum]
513 incr pnum
515 foreach name $allnames {
516 set type [lindex $allbyname($name) 0]
517 if {[info exists seentype($type)]} continue
518 set seentype($type) 1
519 puts $fd [format {#define %-35s %4d} PragTyp_$type $pnum]
520 incr pnum
523 # Generate #defines for flags
525 puts $fd "\n/* Property flags associated with various pragma. */"
526 set fv 1
527 foreach f [lsort [array names allflags]] {
528 puts $fd [format {#define PragFlg_%-10s 0x%02x /* %s */} \
529 $f $fv $flagMeaning($f)]
530 set fv [expr {$fv*2}]
533 # Sort the column lists so that longer column lists occur first
535 proc colscmp {a b} {
536 return [expr {[llength $b] - [llength $a]}]
538 set cols_list [lsort -command colscmp $cols_list]
540 # Generate the array of column names used by pragmas that act like
541 # queries.
543 puts $fd "\n/* Names of columns for pragmas that return multi-column result"
544 puts $fd "** or that return single-column results where the name of the"
545 puts $fd "** result column is different from the name of the pragma\n*/"
546 puts $fd "static const char *const pragCName\[\] = {"
547 set offset 0
548 set allcollist {}
549 foreach cols $cols_list {
550 set n [llength $cols]
551 set limit [expr {[llength $allcollist] - $n}]
552 for {set i 0} {$i<$limit} {incr i} {
553 set sublist [lrange $allcollist $i [expr {$i+$n-1}]]
554 if {$sublist==$cols} {
555 puts $fd [format "%27s/* $colUsedBy($cols) reuses $i */" ""]
556 set cols_offset($cols) $i
557 break
560 if {$i<$limit} continue
561 set cols_offset($cols) $offset
562 set ub " /* Used by: $colUsedBy($cols) */"
563 foreach c $cols {
564 lappend allcollist $c
565 puts $fd [format " /* %3d */ %-14s%s" $offset \"$c\", $ub]
566 set ub ""
567 incr offset
570 puts $fd "\175;"
572 # Generate the lookup table
574 puts $fd "\n/* Definitions of all built-in pragmas */"
575 puts $fd "typedef struct PragmaName \173"
576 puts $fd " const char *const zName; /* Name of pragma */"
577 puts $fd " u8 ePragTyp; /* PragTyp_XXX value */"
578 puts $fd " u8 mPragFlg; /* Zero or more PragFlg_XXX values */"
579 puts $fd { u8 iPragCName; /* Start of column names in pragCName[] */}
580 puts $fd " u8 nPragCName; \
581 /* Num of col names. 0 means use pragma name */"
582 puts $fd " u64 iArg; /* Extra argument */"
583 puts $fd "\175 PragmaName;"
584 puts $fd "static const PragmaName aPragmaName\[\] = \173"
586 set current_if {}
587 set spacer [format { %26s } {}]
588 foreach name $allnames {
589 foreach {type arg if flag cx} $allbyname($name) break
590 if {$cx==0 || $cx==""} {
591 set cy 0
592 set nx 0
593 } else {
594 set cy $cols_offset($cx)
595 set nx [llength $cx]
597 if {$if!=$current_if} {
598 if {$current_if!=""} {
599 foreach this_if $current_if {
600 puts $fd "#endif"
603 set current_if $if
604 if {$current_if!=""} {
605 foreach this_if $current_if {
606 puts $fd "#if $this_if"
610 set typex [format PragTyp_%-23s $type,]
611 if {$flag==""} {
612 set flagx "0"
613 } else {
614 set flagx PragFlg_[join $flag {|PragFlg_}]
616 puts $fd " \173/* zName: */ \"$name\","
617 puts $fd " /* ePragTyp: */ PragTyp_$type,"
618 puts $fd " /* ePragFlg: */ $flagx,"
619 puts $fd " /* ColNames: */ $cy, $nx,"
620 puts $fd " /* iArg: */ $arg \175,"
622 if {$current_if!=""} {
623 foreach this_if $current_if {
624 puts $fd "#endif"
627 puts $fd "\175;"
629 # count the number of pragmas, for information purposes
631 set allcnt 0
632 set dfltcnt 0
633 foreach name $allnames {
634 incr allcnt
635 set if [lindex $allbyname($name) 2]
636 if {[regexp {^defined} $if] || [regexp {[^!]defined} $if]} continue
637 incr dfltcnt
639 puts $fd "/* Number of pragmas: $dfltcnt on by default, $allcnt total. */"