Module: TableNode
- Defined in:
- lib/sql_munger/nodes.rb
Instance Method Summary collapse
- #constraints ⇒ Object
- #defns ⇒ Object
- #fields ⇒ Object
- #indices ⇒ Object
- #name ⇒ Object
-
#primary_key ⇒ Object
Return the set of primary key field objects for this table.
-
#primary_key_fields ⇒ Object
return the set of primary key field names for this table.
Instance Method Details
#constraints ⇒ Object
12 13 14 |
# File 'lib/sql_munger/nodes.rb', line 12 def constraints defns.select {|x| x.constraint?} end |
#defns ⇒ Object
16 17 18 19 20 |
# File 'lib/sql_munger/nodes.rb', line 16 def defns table_element_list.elements.map do |node| node.table_element end end |
#fields ⇒ Object
4 5 6 |
# File 'lib/sql_munger/nodes.rb', line 4 def fields defns.select {|x| x.field? } end |
#indices ⇒ Object
8 9 10 |
# File 'lib/sql_munger/nodes.rb', line 8 def indices @indices ||= [] end |
#name ⇒ Object
51 52 53 |
# File 'lib/sql_munger/nodes.rb', line 51 def name table_name.parts.last end |
#primary_key ⇒ Object
Return the set of primary key field objects for this table. May be empty, or contain only one field.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sql_munger/nodes.rb', line 24 def primary_key single_key = fields.select{|f| f.clauses.any? {|c| c.clause_type == :primary_key } } multi_key = constraints.select{ |x| x.type == :primary_key } if single_key.size > 1 raise "Can't have more than one primary key field specifier" end if single_key.size > 0 && multi_key.size > 0 raise "Can't specify both single primary key and multi-key constraint" end if single_key.size == 1 # return single key field name [ single_key.first ] elsif multi_key.size == 1 fields.select {|f| multi_key.first.fields.include?( f.name ) } else [] end end |
#primary_key_fields ⇒ Object
return the set of primary key field names for this table.
47 48 49 |
# File 'lib/sql_munger/nodes.rb', line 47 def primary_key_fields primary_key.map{|f| f.name} end |