Created
September 17, 2024 18:27
-
-
Save rickhull/464c6fdf5fdea217894f67618b4a2b0c to your computer and use it in GitHub Desktop.
SQLite pragma handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pragma | |
ENUM = { | |
'auto_vacuum' => %w[none full incremental], | |
'synchronous' => %w[off normal full], | |
'temp_store' => %w[default file memory], | |
} | |
RO = %w[data_version freelist_count page_count] | |
RW = | |
%w[application_id analysis_limit auto_vacuum automatic_index | |
busy_timeout cache_size cache_spill cell_size_check | |
checkpoint_fullfsync defer_foreign_keys encoding foreign_keys | |
fullfsync hard_heap_limit ignore_check_constraints journal_mode | |
journal_size_limit locking_mode max_page_count mmap_size page_size | |
query_only read_uncommitted recursive_triggers | |
reverse_unordered_selects secure_delete soft_heap_limit synchronous | |
temp_store threads trusted_schema user_version wal_autocheckpoint] | |
SCALAR = (RO + RW).sort | |
# debug: parser_trace schema_version stats vdbe_* writable_schema | |
# legacy: legacy_alter_table legacy_file_format | |
# deprecated: case_sensitive_like count_changes data_store_directory | |
# default_cache_size empty_result_callbacks full_column_names | |
# short_column_names temp_store_directory | |
REPORT = { | |
compile_options: nil, | |
# list | |
collation_list: nil, | |
database_list: nil, | |
function_list: nil, | |
module_list: nil, | |
pragma_list: nil, | |
table_list: nil, | |
index_list: :table_name, | |
foreign_key_list: :table_name, | |
# info | |
index_info: :index_name, | |
index_xinfo: :index_name, | |
table_info: :table_name, | |
table_xinfo: :table_name, | |
} | |
# either no args (nil) or a single arg (symbol) | |
COMMAND = { | |
# checks | |
foreign_key_check: :optional, # report | |
integrity_check: :optional, # ok | |
quick_check: :optional, # ok | |
# manipulation | |
incremental_vacuum: :optional, # empty | |
optimize: :optional, # empty | |
shrink_memory: nil, # empty | |
wal_checkpoint: :optional, | |
} | |
def initialize(db) | |
@db = db | |
end | |
def get(pragma) | |
@db.execute("PRAGMA #{pragma}")[0][0] | |
end | |
def set(pragma, val) | |
@db.execute("PRAGMA #{pragma} = #{val}") | |
get(pragma) | |
end | |
# just the rows | |
def list(pragma, arg = nil) | |
if arg.nil? | |
@db.execute("PRAGMA #{pragma}") | |
else | |
@db.execute("PRAGMA #{pragma}(#{arg})") | |
end | |
end | |
# include a header row | |
def report(pragma, arg = nil) | |
if arg.nil? | |
@db.execute2("PRAGMA #{pragma}") | |
else | |
@db.execute2("PRAGMA #{pragma}(#{arg})") | |
end | |
end | |
SCALAR.each { |pragma| define_method(pragma) { get(pragma) }} | |
RW.each { |pragma| | |
define_method(pragma + '=') { |val| set(pragma, val) } | |
} | |
(REPORT.merge(COMMAND)).each { |pragma, arg| | |
if arg.nil? | |
define_method(pragma) { report(pragma) } | |
elsif arg == :optional | |
define_method(pragma) { |val=nil| report(pragma, val) } | |
else | |
define_method(pragma) { |val| report(pragma, val) } | |
end | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment