Class: DisposableDB::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/disposable_db/database.rb

Direct Known Subclasses

DisposableDB::Databases::SQLite

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection = nil, opts = {}) ⇒ Database

Returns a new instance of Database.



5
6
7
# File 'lib/disposable_db/database.rb', line 5

def initialize(connection = nil, opts = {})
  @connection = connection || Sequel.connect('sqlite:/')
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



3
4
5
# File 'lib/disposable_db/database.rb', line 3

def connection
  @connection
end

Instance Method Details

#create_table(table_template) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/disposable_db/database.rb', line 21

def create_table(table_template)
  @connection.create_table? table_template.name.to_sym do
    primary_key table_template.primary_key if table_template.primary_key 
    table_template.columns.each do |column|
      column column.name.to_sym, column.type
    end
    table_template.indexes.each do |idx|
      index idx.to_sym
    end
  end
end

#create_table!(table_template) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/disposable_db/database.rb', line 9

def create_table!(table_template)
  @connection.create_table! table_template.name.to_sym do
    primary_key table_template.primary_key if table_template.primary_key 
    table_template.columns.each do |column|
      column column.name.to_sym, column.type
    end
    table_template.indexes.each do |idx|
      index idx.to_sym
    end
  end
end

#table_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/disposable_db/database.rb', line 33

def table_exists?(name)
  @connection.table_exists? name
end