Class: Lich::Common::DatabaseAdapter

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

Overview

Handles database interactions for Lich.

This class is responsible for setting up the database and managing settings related to scripts and their scopes.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(data_dir, table_name) ⇒ void

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



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 = 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 for the script, or an empty hash if none found



42
43
44
45
# File 'documented/common/settings/database_adapter.rb', line 42

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:

Save settings for a script

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



54
55
56
57
58
59
60
61
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
# File 'documented/common/settings/database_adapter.rb', line 54

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.



23
24
25
26
27
28
29
30
# File 'documented/common/settings/database_adapter.rb', line 23

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



34
35
36
# File 'documented/common/settings/database_adapter.rb', line 34

def table
  @table
end