Class: Lich::Common::DatabaseAdapter

Inherits:
Object
  • Object
show all
Defined in:
documented/common/settings/database_adapter.rb

Overview

Database adapter to separate database concerns

Instance Method Summary collapse

Constructor Details

#initialize(data_dir, table_name) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Opens or creates a SQLite database at data_dir/lich.db3 and initializes the specified table for storing serialized settings.

Parameters:

  • data_dir (String)

    the directory where the lich.db3 database file is stored

  • table_name (String)

    the name of the table to create or use for this adapter



14
15
16
17
18
19
# File 'documented/common/settings/database_adapter.rb', line 14

def initialize(data_dir, table_name)
  @file = File.join(data_dir, "lich.db3")
  @db = Lich.open_sequel_sqlite(@file)
  @table_name = table_name
  setup!
end

Instance Method Details

#get_settings(script_name, scope = ":") ⇒ Hash

Retrieves persisted settings for a script, returning an empty hash if no entry exists for the given script name and scope.

Examples:

adapter.get_settings("my_script", "global") #=> {"key" => "value"}

Parameters:

  • script_name (String)

    the name of the script

  • scope (String) (defaults to: ":")

    the scope identifier (default: ":")

Returns:

  • (Hash)

    the deserialized settings hash, or {} if not found



51
52
53
54
# File 'documented/common/settings/database_adapter.rb', line 51

def get_settings(script_name, scope = ":")
  entry = @table.first(script: script_name, scope: scope)
  entry.nil? ? {} : Marshal.load(entry[:hash])
end

#save_settings(script_name, settings, scope = ":") ⇒ Boolean

Saves or updates settings for a script by serializing the hash and upserting into the database. Returns true on success, false on validation or database errors.

Validates that settings is a Hash. On serialization or database errors, logs the exception and returns false.

Examples:

adapter.save_settings("my_script", {"key" => "value"}) #=> true

Parameters:

  • script_name (String)

    the name of the script

  • settings (Hash)

    the settings hash to persist

  • scope (String) (defaults to: ":")

    the scope identifier (default: ":")

Returns:

  • (Boolean)

    true if saved successfully, false if validation or save failed



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'documented/common/settings/database_adapter.rb', line 68

def save_settings(script_name, settings, scope = ":")
  unless settings.is_a?(Hash)
    Lich::Messaging.msg("error", "--- Error: Report this - settings must be a Hash, got #{settings.class} ---")
    Lich.log("--- Error: settings must be a Hash, got #{settings.class} from call initiated by #{script_name} ---")
    Lich.log(settings.inspect)
    return false
  end

  begin
    blob = Sequel::SQL::Blob.new(Marshal.dump(settings))
  rescue => e
    Lich::Messaging.msg("error", "--- Error: failed to serialize settings ---")
    Lich.log("--- Error: failed to serialize settings ---")
    Lich.log("#{e.message}\n#{e.backtrace.join("\n")}")
    return false
  end

  begin
    @table
      .insert_conflict(target: [:script, :scope], update: { hash: blob })
      .insert(script: script_name, scope: scope, hash: blob)
    return true
  rescue Sequel::DatabaseError => db_err
    Lich::Messaging.msg("error", "--- Database error while saving settings ---")
    Lich.log("--- Database error while saving settings ---")
    Lich.log("#{db_err.message}\n#{db_err.backtrace.join("\n")}")
  rescue => e
    Lich::Messaging.msg("error", "--- Unexpected error while saving settings ---")
    Lich.log("--- Unexpected error while saving settings ---")
    Lich.log("#{e.message}\n#{e.backtrace.join("\n")}")
  end

  false
end

#setup!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Creates the table if it does not exist, with columns for script name, scope, and a blob containing serialized settings data.



26
27
28
29
30
31
32
33
# File 'documented/common/settings/database_adapter.rb', line 26

def setup!
  @db.create_table?(@table_name) do
    text :script
    text :scope
    blob :hash
  end
  @table = @db[@table_name]
end

#tableSequel::Dataset

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the Sequel dataset for direct table access.

Returns:

  • (Sequel::Dataset)

    the underlying database table



39
40
41
# File 'documented/common/settings/database_adapter.rb', line 39

def table
  @table
end