|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SQLite3 |
| 4 | + # defines methods to de generate pragma statements |
| 5 | + module Pragmas |
| 6 | + class << self |
| 7 | + # The enumeration of valid synchronous modes. |
| 8 | + SYNCHRONOUS_MODES = [["full", 2], ["normal", 1], ["off", 0]].freeze |
| 9 | + |
| 10 | + # The enumeration of valid temp store modes. |
| 11 | + TEMP_STORE_MODES = [["default", 0], ["file", 1], ["memory", 2]].freeze |
| 12 | + |
| 13 | + # The enumeration of valid auto vacuum modes. |
| 14 | + AUTO_VACUUM_MODES = [["none", 0], ["full", 1], ["incremental", 2]].freeze |
| 15 | + |
| 16 | + # The list of valid journaling modes. |
| 17 | + JOURNAL_MODES = [["delete"], ["truncate"], ["persist"], ["memory"], ["wal"], ["off"]].freeze |
| 18 | + |
| 19 | + # The list of valid locking modes. |
| 20 | + LOCKING_MODES = [["normal"], ["exclusive"]].freeze |
| 21 | + |
| 22 | + # The list of valid encodings. |
| 23 | + ENCODINGS = [["utf-8"], ["utf-16"], ["utf-16le"], ["utf-16be"]].freeze |
| 24 | + |
| 25 | + # The list of valid WAL checkpoints. |
| 26 | + WAL_CHECKPOINTS = [["passive"], ["full"], ["restart"], ["truncate"]].freeze |
| 27 | + |
| 28 | + # Enforce foreign key constraints |
| 29 | + # https://www.sqlite.org/pragma.html#pragma_foreign_keys |
| 30 | + # https://www.sqlite.org/foreignkeys.html |
| 31 | + def foreign_keys(value) |
| 32 | + gen_boolean_pragma(:foreign_keys, value) |
| 33 | + end |
| 34 | + |
| 35 | + # Journal mode WAL allows for greater concurrency (many readers + one writer) |
| 36 | + # https://www.sqlite.org/pragma.html#pragma_journal_mode |
| 37 | + def journal_mode(value) |
| 38 | + gen_enum_pragma(:journal_mode, value, JOURNAL_MODES) |
| 39 | + end |
| 40 | + |
| 41 | + # Set more relaxed level of database durability |
| 42 | + # 2 = "FULL" (sync on every write), 1 = "NORMAL" (sync every 1000 written pages) and 0 = "NONE" |
| 43 | + # https://www.sqlite.org/pragma.html#pragma_synchronous |
| 44 | + def synchronous(value) |
| 45 | + gen_enum_pragma(:synchronous, value, SYNCHRONOUS_MODES) |
| 46 | + end |
| 47 | + |
| 48 | + def temp_store(value) |
| 49 | + gen_enum_pragma(:temp_store, value, TEMP_STORE_MODES) |
| 50 | + end |
| 51 | + |
| 52 | + # Set the global memory map so all processes can share some data |
| 53 | + # https://www.sqlite.org/pragma.html#pragma_mmap_size |
| 54 | + # https://www.sqlite.org/mmap.html |
| 55 | + def mmap_size(value) |
| 56 | + "PRAGMA mmap_size = #{value.to_i}" |
| 57 | + end |
| 58 | + |
| 59 | + # Impose a limit on the WAL file to prevent unlimited growth |
| 60 | + # https://www.sqlite.org/pragma.html#pragma_journal_size_limit |
| 61 | + def journal_size_limit(value) |
| 62 | + "PRAGMA journal_size_limit = #{value.to_i}" |
| 63 | + end |
| 64 | + |
| 65 | + # Set the local connection cache to 2000 pages |
| 66 | + # https://www.sqlite.org/pragma.html#pragma_cache_size |
| 67 | + def cache_size(value) |
| 68 | + "PRAGMA cache_size = #{value.to_i}" |
| 69 | + end |
| 70 | + |
| 71 | + private |
| 72 | + |
| 73 | + def gen_boolean_pragma(name, mode) |
| 74 | + case mode |
| 75 | + when String |
| 76 | + case mode.downcase |
| 77 | + when "on", "yes", "true", "y", "t" then mode = "'ON'" |
| 78 | + when "off", "no", "false", "n", "f" then mode = "'OFF'" |
| 79 | + else |
| 80 | + raise ActiveRecord::JDBCError, "unrecognized pragma parameter #{mode.inspect}" |
| 81 | + end |
| 82 | + when true, 1 |
| 83 | + mode = "ON" |
| 84 | + when false, 0, nil |
| 85 | + mode = "OFF" |
| 86 | + else |
| 87 | + raise ActiveRecord::JDBCError, "unrecognized pragma parameter #{mode.inspect}" |
| 88 | + end |
| 89 | + |
| 90 | + "PRAGMA #{name} = #{mode}" |
| 91 | + end |
| 92 | + |
| 93 | + def gen_enum_pragma(name, mode, enums) |
| 94 | + match = enums.find { |p| p.find { |i| i.to_s.downcase == mode.to_s.downcase } } |
| 95 | + |
| 96 | + unless match |
| 97 | + # Unknown pragma value |
| 98 | + raise ActiveRecord::JDBCError, "unrecognized #{name} #{mode.inspect}" |
| 99 | + end |
| 100 | + |
| 101 | + "PRAGMA #{name} = '#{match.first.upcase}'" |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | +end |
0 commit comments