Class: AWSMine::DBHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_minecraft/db_helper.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initializeDBHelper

Returns a new instance of DBHelper.



7
8
9
10
# File 'lib/aws_minecraft/db_helper.rb', line 7

def initialize
  @db = SQLite3::Database.new 'minecraft.db'
  @tables = %w(instances)
end

Instance Method Details

#init_dbObject



20
21
22
23
24
25
# File 'lib/aws_minecraft/db_helper.rb', line 20

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_detailsObject



27
28
29
# File 'lib/aws_minecraft/db_helper.rb', line 27

def instance_details
  @db.execute('SELECT ip, id FROM instances;').first
end

#instance_exists?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/aws_minecraft/db_helper.rb', line 31

def instance_exists?
  !@db.execute('SELECT id FROM instances;').empty?
end

#remove_instanceObject



43
44
45
# File 'lib/aws_minecraft/db_helper.rb', line 43

def remove_instance
  @db.execute 'DELETE FROM instances;'
end

#store_instance(ip, id) ⇒ Object



35
36
37
# File 'lib/aws_minecraft/db_helper.rb', line 35

def store_instance(ip, id)
  @db.execute "INSERT INTO instances VALUES ('#{ip}', '#{id}');"
end

#table_exists?(table) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# 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) ⇒ Object



39
40
41
# File 'lib/aws_minecraft/db_helper.rb', line 39

def update_instance(ip, id)
  @db.execute "UPDATE instances SET ip='#{ip}' WHERE id='#{id}';"
end