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



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

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:



60
61
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
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/connection_adapters/postgresql_adapter.rb', line 60

def constraints(table_name, name = nil)#:nodoc:
  constraints = []     
  
  sql = %Q{
    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| 
    comparable_constraint_name = row[0].upcase
    referenced_column_name = row[8]
    column_name = row[5]
    existing_constraint = constraint_name_hash[comparable_constraint_name]
    if !existing_constraint
      new_constraint = PostgreSQLConstraint.new(row[0], row[1], row[2], row[3], row[4], column_name, row[6], row[7], 
        referenced_column_name, row[9], row[10])
      constraints << new_constraint
      constraint_name_hash[comparable_constraint_name] = new_constraint
    else
      existing_constraint.column_names << column_name unless existing_constraint.column_names.include?(column_name)
      if referenced_column_name
        existing_constraint.referenced_column_names << referenced_column_name unless existing_constraint.referenced_column_names.include?(referenced_column_name)
      end
    end
  end
  constraints
end