Class: Lich::Common::DatabaseAdapter

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

Overview

Database adapter to separate database concerns Database adapter to separate database concerns

This class handles the interaction with the database, including setting up the database table and saving/loading settings.

Examples:

Creating a new database adapter

adapter = Lich::Common::DatabaseAdapter.new("/path/to/data", "settings_table")

Instance Method Summary collapse

Constructor Details

#initialize(data_dir, table_name) ⇒ DatabaseAdapter

Initializes a new DatabaseAdapter instance.

Parameters:

  • data_dir (String)

    The directory where the database file is located.

  • table_name (String)

    The name of the table to use in the database.



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

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

Instance Method Details

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

Retrieves settings for a given script and scope.

Parameters:

  • script_name (String)

    The name of the script to retrieve settings for.

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

    The scope of the settings (default is “:”).

Returns:

  • (Hash)

    The settings as a hash, or an empty hash if not found.



47
48
49
50
# File 'documented/common/settings/database_adapter.rb', line 47

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 settings for a given script and scope.

Examples:

Saving settings

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

Parameters:

  • script_name (String)

    The name of the script to save settings for.

  • settings (Hash)

    The settings to save.

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

    The scope of the settings (default is “:”).

Returns:

  • (Boolean)

    True if settings were saved successfully, false otherwise.

Raises:

  • (ArgumentError)

    If settings is not a Hash.

  • (Sequel::DatabaseError)

    If there is a database error while saving.



62
63
64
65
66
67
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
# File 'documented/common/settings/database_adapter.rb', line 62

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 returns an undefined value.

Sets up the database table if it does not exist.



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

Returns the database table object.

Returns:

  • (Sequel::Dataset)

    The dataset representing the table.



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

def table
  @table
end