Class: Lutaml::Store::Adapter::Sqlite

Inherits:
Base
  • Object
show all
Defined in:
lib/lutaml/store/adapter/sqlite.rb

Constant Summary collapse

DEFAULT_TABLE_NAME =
"lutaml_store"

Instance Method Summary collapse

Methods inherited from Base

#bulk_get

Constructor Details

#initialize(config = {}) ⇒ Sqlite

Returns a new instance of Sqlite.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lutaml/store/adapter/sqlite.rb', line 11

def initialize(config = {})
  super
  begin
    require "sqlite3"
  rescue LoadError
    raise ConfigurationError,
          "sqlite3 gem is required for the SQLite adapter. Add it to your Gemfile."
  end
  @db_path = @config[:path] || raise(ConfigurationError, "SQLite adapter requires :path config")
  @table_name = @config[:table_name] || DEFAULT_TABLE_NAME
  @timeout = @config[:timeout] || 30_000

  setup_database
end

Instance Method Details

#allObject



64
65
66
67
68
69
70
# File 'lib/lutaml/store/adapter/sqlite.rb', line 64

def all
  result = {}
  execute_query("SELECT key, value FROM #{@table_name}") do |row|
    result[row[0]] = row[1]
  end
  result
end

#bulk_delete(keys) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lutaml/store/adapter/sqlite.rb', line 119

def bulk_delete(keys)
  result = {}
  @db.transaction do
    keys.each do |key|
      value = get(key)
      if value
        execute_statement("DELETE FROM #{@table_name} WHERE key = ?", [key])
        result[key] = value
      else
        result[key] = nil
      end
    end
  end
  result
end

#bulk_set(key_value_pairs) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/lutaml/store/adapter/sqlite.rb', line 107

def bulk_set(key_value_pairs)
  @db.transaction do
    key_value_pairs.each do |key, value|
      serialized_value = value.is_a?(String) ? value : JSON.generate(value)
      execute_statement(
        "INSERT OR REPLACE INTO #{@table_name} (key, value, updated_at) VALUES (?, ?, ?)",
        [key, serialized_value, Time.now.to_f]
      )
    end
  end
end

#clearObject



72
73
74
75
76
# File 'lib/lutaml/store/adapter/sqlite.rb', line 72

def clear
  count = size
  execute_statement("DELETE FROM #{@table_name}")
  count
end

#closeObject



93
94
95
96
# File 'lib/lutaml/store/adapter/sqlite.rb', line 93

def close
  @db&.close
  @db = nil
end

#delete(key) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/lutaml/store/adapter/sqlite.rb', line 49

def delete(key)
  value = get(key)
  return nil unless value

  execute_statement("DELETE FROM #{@table_name} WHERE key = ?", [key])
  value
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/lutaml/store/adapter/sqlite.rb', line 57

def exists?(key)
  execute_query("SELECT 1 FROM #{@table_name} WHERE key = ? LIMIT 1", [key]) do |_row|
    return true
  end
  false
end

#get(key) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lutaml/store/adapter/sqlite.rb', line 26

def get(key)
  result = nil
  execute_query("SELECT value FROM #{@table_name} WHERE key = ?", [key]) do |row|
    value = row[0]
    begin
      result = JSON.parse(value)
    rescue JSON::ParserError
      result = value
    end
  end
  result
end

#keysObject



85
86
87
88
89
90
91
# File 'lib/lutaml/store/adapter/sqlite.rb', line 85

def keys
  result = []
  execute_query("SELECT key FROM #{@table_name}") do |row|
    result << row[0]
  end
  result
end

#set(key, value) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/lutaml/store/adapter/sqlite.rb', line 39

def set(key, value)
  serialized_value = value.is_a?(String) ? value : JSON.generate(value)

  execute_statement(
    "INSERT OR REPLACE INTO #{@table_name} (key, value, updated_at) VALUES (?, ?, ?)",
    [key, serialized_value, Time.now.to_f]
  )
  value
end

#sizeObject



78
79
80
81
82
83
# File 'lib/lutaml/store/adapter/sqlite.rb', line 78

def size
  execute_query("SELECT COUNT(*) FROM #{@table_name}") do |row|
    return row[0]
  end
  0
end

#statsObject



98
99
100
101
102
103
104
105
# File 'lib/lutaml/store/adapter/sqlite.rb', line 98

def stats
  super.merge(
    db_path: @db_path,
    table_name: @table_name,
    database_size: calculate_database_size,
    schema_version: get_schema_version
  )
end