Class: SQL::TableCreator

Inherits:
Object
  • Object
show all
Extended by:
DataMapper::Property::Lookup
Defined in:
lib/dm-migrations/sql/table_creator.rb

Defined Under Namespace

Classes: Column, SqlExpr

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, table_name, opts = {}, &block) ⇒ TableCreator

Returns a new instance of TableCreator.



9
10
11
12
13
14
15
16
17
# File 'lib/dm-migrations/sql/table_creator.rb', line 9

def initialize(adapter, table_name, opts = {}, &block)
  @adapter = adapter
  @table_name = table_name.to_s
  @opts = opts

  @columns = []

  instance_eval(&block)
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



7
8
9
# File 'lib/dm-migrations/sql/table_creator.rb', line 7

def opts
  @opts
end

#table_nameObject

Returns the value of attribute table_name.



7
8
9
# File 'lib/dm-migrations/sql/table_creator.rb', line 7

def table_name
  @table_name
end

Instance Method Details

#column(name, type, opts = {}) ⇒ Object



23
24
25
# File 'lib/dm-migrations/sql/table_creator.rb', line 23

def column(name, type, opts = {})
  @columns << Column.new(@adapter, name, type, opts)
end

#nowObject

A helper for using the native NOW() SQL function in a default



32
33
34
# File 'lib/dm-migrations/sql/table_creator.rb', line 32

def now
  SqlExpr.new('NOW()')
end

#quoted_table_nameObject



19
20
21
# File 'lib/dm-migrations/sql/table_creator.rb', line 19

def quoted_table_name
  @adapter.send(:quote_name, table_name)
end

#to_sqlObject



27
28
29
# File 'lib/dm-migrations/sql/table_creator.rb', line 27

def to_sql
  "CREATE TABLE #{quoted_table_name} (#{@columns.map(&:to_sql).join(', ')})#{@adapter.table_options(@opts)}"
end

#uuidObject

A helper for using the native UUID() SQL function in a default



37
38
39
# File 'lib/dm-migrations/sql/table_creator.rb', line 37

def uuid
  SqlExpr.new('UUID()')
end