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



21
22
23
24
25
26
# 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_detailsObject



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

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

#instance_exists?Boolean

Returns:

  • (Boolean)


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

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

#remove_instanceObject



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

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

#store_instance(ip, id) ⇒ Object



36
37
38
# 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) ⇒ Boolean

Returns:

  • (Boolean)


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



40
41
42
# 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