Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/connection_adapters/postgresql_adapter.rb

Instance Method Summary collapse

Instance Method Details

#columns(table_name, name = nil) ⇒ Object

FIXME patch postgresql_adapter so I don’t need to re-define columns here in order to get sufficient column metadata



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/connection_adapters/postgresql_adapter.rb', line 49

def columns(table_name, name = nil) #:nodoc:
  columns = []
  column_definitions(table_name).each do |name, type, default, notnull, typmod|
    # typmod now unused as limit, precision, scale all handled by superclass
    current = PostgreSQLColumn.new(name, default_value(default), translate_field_type(type), notnull == "f")
    current.generated= (!default.nil? && default.index('nextval') == 0)
    current.default_specified = !(default.nil? || default.blank?)
    columns << current
  end
  columns
end

#constraints(table_name, name = nil) ⇒ Object

:nodoc:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/connection_adapters/postgresql_adapter.rb', line 62

def constraints(table_name, name = nil)#:nodoc:
  constraints = []     
  
  sql = "select t1.*, KCU.column_name, REF2.unique_constraint_name, KCU2.table_name as referenced_table_name, \
    KCU2.column_name as referenced_column_name, REF2.delete_rule, REF2.update_rule from \
    (select constraint_name, table_catalog, table_schema, table_name, constraint_type from \
    information_schema.table_constraints where table_name='#{table_name.downcase}' or constraint_name in \
      (select REF.constraint_name from information_schema.referential_constraints as REF 
      where unique_constraint_name in (select constraint_name from information_schema.table_constraints 
      where table_name='#{table_name.downcase}'))) as t1 inner join information_schema.key_column_usage as KCU on \
      (t1.constraint_name=KCU.constraint_name) left join information_schema.referential_constraints as REF2 \
      on (REF2.constraint_name=t1.constraint_name) left join information_schema.key_column_usage as KCU2 \
      on (REF2.unique_constraint_name=KCU2.constraint_name)"
  
  results = query(sql, name)
  constraint_name_hash = {}
  results.each do |row| 
    constraints << PostgreSQLConstraint.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10])
    comparable_constraint_name = row[0].upcase
    constraint_name_count = constraint_name_hash[comparable_constraint_name]
    constraint_name_count ? 
      constraint_name_hash[comparable_constraint_name] = constraint_name_count + 1 :
      constraint_name_hash[comparable_constraint_name] = 1
  end

  constraints.each do |constraint|
    constraint.member_of_composite=(constraint_name_hash[constraint.constraint_name.upcase] > 1)
  end
  constraints
end