Module: TableNode

Defined in:
lib/sql_munger/nodes.rb

Instance Method Summary collapse

Instance Method Details

#constraintsObject



12
13
14
# File 'lib/sql_munger/nodes.rb', line 12

def constraints
  defns.select {|x| x.constraint?}
end

#defnsObject



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

#fieldsObject



4
5
6
# File 'lib/sql_munger/nodes.rb', line 4

def fields
  defns.select {|x| x.field? }
end

#indicesObject



8
9
10
# File 'lib/sql_munger/nodes.rb', line 8

def indices
  @indices ||= []
end

#nameObject



51
52
53
# File 'lib/sql_munger/nodes.rb', line 51

def name
  table_name.parts.last
end

#primary_keyObject

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_fieldsObject

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