Module: Lich::Common::DB_Store
- Defined in:
- documented/common/db_store.rb
Class Method Summary collapse
-
.get_data(scope = "#{XMLData.game}:#{XMLData.name}", script) ⇒ Hash
Retrieves data from the database for a specific script and scope.
-
.get_vars(scope = "#{XMLData.game}:#{XMLData.name}") ⇒ Hash
Retrieves user variables from the database for a specific scope.
-
.read(scope = "#{XMLData.game}:#{XMLData.name}", script) ⇒ Hash
Reads data from the database based on the provided scope and script name.
-
.save(scope = "#{XMLData.game}:#{XMLData.name}", script, val) ⇒ String
Saves data to the database based on the provided scope, script name, and value.
-
.store_data(scope = "#{XMLData.game}:#{XMLData.name}", script, val) ⇒ String
Stores data in the database for a specific script and scope.
-
.store_vars(scope = "#{XMLData.game}:#{XMLData.name}", val) ⇒ String
Stores user variables in the database for a specific scope.
Class Method Details
.get_data(scope = "#{XMLData.game}:#{XMLData.name}", script) ⇒ Hash
Retrieves data from the database for a specific script and scope.
57 58 59 60 61 |
# File 'documented/common/db_store.rb', line 57 def self.get_data(scope = "#{XMLData.game}:#{XMLData.name}", script) hash = Lich.db.get_first_value('SELECT hash FROM script_auto_settings WHERE script=? AND scope=?;', [script.encode('UTF-8'), scope.encode('UTF-8')]) return {} unless hash Marshal.load(hash) end |
.get_vars(scope = "#{XMLData.game}:#{XMLData.name}") ⇒ Hash
Retrieves user variables from the database for a specific scope.
69 70 71 72 73 |
# File 'documented/common/db_store.rb', line 69 def self.get_vars(scope = "#{XMLData.game}:#{XMLData.name}") hash = Lich.db.get_first_value('SELECT hash FROM uservars WHERE scope=?;', scope.encode('UTF-8')) return {} unless hash Marshal.load(hash) end |
.read(scope = "#{XMLData.game}:#{XMLData.name}", script) ⇒ Hash
Reads data from the database based on the provided scope and script name.
24 25 26 27 28 29 30 31 |
# File 'documented/common/db_store.rb', line 24 def self.read(scope = "#{XMLData.game}:#{XMLData.name}", script) case script when 'vars', 'uservars' get_vars(scope) else get_data(scope, script) end end |
.save(scope = "#{XMLData.game}:#{XMLData.name}", script, val) ⇒ String
Saves data to the database based on the provided scope, script name, and value.
41 42 43 44 45 46 47 48 |
# File 'documented/common/db_store.rb', line 41 def self.save(scope = "#{XMLData.game}:#{XMLData.name}", script, val) case script when 'vars', 'uservars' store_vars(scope, val) else store_data(scope, script, val) end end |
.store_data(scope = "#{XMLData.game}:#{XMLData.name}", script, val) ⇒ String
This method is synchronized to prevent concurrent access issues.
Stores data in the database for a specific script and scope.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'documented/common/db_store.rb', line 84 def self.store_data(scope = "#{XMLData.game}:#{XMLData.name}", script, val) blob = SQLite3::Blob.new(Marshal.dump(val)) return 'Error: No data to store.' unless blob Lich.db_mutex.synchronize do begin Lich.db.execute('INSERT OR REPLACE INTO script_auto_settings(script,scope,hash) VALUES(?,?,?);', [script.encode('UTF-8'), scope.encode('UTF-8'), blob]) rescue SQLite3::BusyException sleep 0.05 retry rescue StandardError respond "--- Lich: error: #{$ERROR_INFO}" respond $ERROR_INFO.backtrace[0..1] end end end |
.store_vars(scope = "#{XMLData.game}:#{XMLData.name}", val) ⇒ String
This method is synchronized to prevent concurrent access issues.
Stores user variables in the database for a specific scope.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'documented/common/db_store.rb', line 109 def self.store_vars(scope = "#{XMLData.game}:#{XMLData.name}", val) blob = SQLite3::Blob.new(Marshal.dump(val)) return 'Error: No data to store.' unless blob Lich.db_mutex.synchronize do begin Lich.db.execute('INSERT OR REPLACE INTO uservars(scope,hash) VALUES(?,?);', [scope.encode('UTF-8'), blob]) rescue SQLite3::BusyException sleep 0.05 retry rescue StandardError respond "--- Lich: error: #{$ERROR_INFO}" respond $ERROR_INFO.backtrace[0..1] end end end |