Module: DbAgile::SequelAdapter::Schema::TableDriven

Included in:
DbAgile::SequelAdapter
Defined in:
lib/dbagile/adapter/sequel/schema/table_driven.rb

Instance Method Summary collapse

Instance Method Details

#column_names(table, sort_it_by_name = false) ⇒ Object

See Also:

  • Contract::Schema::TableDriven#columns_names


40
41
42
# File 'lib/dbagile/adapter/sequel/schema/table_driven.rb', line 40

def column_names(table, sort_it_by_name = false)
  sort_it_by_name ? db[table].columns.sort{|k1,k2| k1.to_s <=> k2.to_s} : db[table].columns
end

#dbtype_to_ruby_type(info) ⇒ Object

Returns the ruby type associated to a given column info



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dbagile/adapter/sequel/schema/table_driven.rb', line 7

def dbtype_to_ruby_type(info)
  type = info[:type]
  capitalized = type.to_s.capitalize
  begin
    Kernel.eval(capitalized)
  rescue NameError
    case type
      when :datetime
        Time
      when :boolean
        SByC::TypeSystem::Ruby::Boolean
      else
        Object
    end
  end
end

#has_table?(name) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



25
26
27
# File 'lib/dbagile/adapter/sequel/schema/table_driven.rb', line 25

def has_table?(name)
  db.table_exists?(name)
end

#heading(table_name) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/dbagile/adapter/sequel/schema/table_driven.rb', line 30

def heading(table_name)
  heading = {}
  db.schema(table_name).each do |pair|
    column_name, info = pair
    heading[column_name] = dbtype_to_ruby_type(info)
  end
  heading
end

#keys(table_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dbagile/adapter/sequel/schema/table_driven.rb', line 45

def keys(table_name)
  # take the indexes
  indexes = db.indexes(table_name).values
  indexes = indexes.select{|i| i[:unique] == true}.collect{|i| i[:columns]}.sort{|a1, a2| a1.size <=> a2.size}

  # take single keys as well
  key = db.schema(table_name).select{|pair|
    pair[1][:primary_key]
  }.collect{|pair| pair[0]}

  key.empty? ? indexes : (indexes + [ key ])
end