class AWSMine::DBHelper
Initializes the database. The format to use if there are more tables is simple. Just create a SQL file corresponding to the table name and add that table to the @tables instance variable.
Public Class Methods
new()
click to toggle source
# File lib/aws_minecraft/db_helper.rb, line 7 def initialize @db = SQLite3::Database.new 'minecraft.db' @tables = %w(instances) end
Public Instance Methods
init_db()
click to toggle source
# File lib/aws_minecraft/db_helper.rb, line 21 def init_db @tables.each do |table| sql = File.read(File.join(__dir__, "../../cfg/#{table}.sql")) @db.execute sql unless table_exists? table end end
instance_details()
click to toggle source
# File lib/aws_minecraft/db_helper.rb, line 28 def instance_details @db.execute('SELECT ip, id FROM instances;').first end
instance_exists?()
click to toggle source
# File lib/aws_minecraft/db_helper.rb, line 32 def instance_exists? !@db.execute('SELECT id FROM instances;').empty? end
remove_instance()
click to toggle source
# File lib/aws_minecraft/db_helper.rb, line 44 def remove_instance @db.execute 'DELETE FROM instances;' end
store_instance(ip, id)
click to toggle source
# File lib/aws_minecraft/db_helper.rb, line 36 def store_instance(ip, id) @db.execute "INSERT INTO instances VALUES ('#{ip}', '#{id}');" end
table_exists?(table)
click to toggle source
# File lib/aws_minecraft/db_helper.rb, line 12 def table_exists?(table) retrieved = @db.execute <<-SQL SELECT name FROM sqlite_master WHERE type='table' AND name='#{table}'; SQL return false if retrieved.nil? || retrieved.empty? retrieved.first.first == table end
update_instance(ip, id)
click to toggle source
# File lib/aws_minecraft/db_helper.rb, line 40 def update_instance(ip, id) @db.execute "UPDATE instances SET ip='#{ip}' WHERE id='#{id}';" end