Module: Lich::Common::DB_Store
- Defined in:
- lib/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.
-
.save(scope = "#{XMLData.game}:#{XMLData.name}", script, val) ⇒ String
Saves data to the database based on the provided scope and script.
-
.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.
55 56 57 58 59 |
# File 'lib/common/db_store.rb', line 55 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.
67 68 69 70 71 |
# File 'lib/common/db_store.rb', line 67 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.
20 21 22 23 24 25 26 27 |
# File 'lib/common/db_store.rb', line 20 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 and script.
39 40 41 42 43 44 45 46 |
# File 'lib/common/db_store.rb', line 39 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
Stores data in the database for a specific script and scope.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/common/db_store.rb', line 83 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
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 'lib/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 |