Class: ActiveRecord::ConnectionAdapters::OracleAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/connection_adapters/oracle_adapter.rb

Instance Method Summary collapse

Instance Method Details

#base_columnsObject



79
# File 'lib/connection_adapters/oracle_adapter.rb', line 79

alias :base_columns :columns

#columns(table_name, name = nil) ⇒ Object

:nodoc:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/connection_adapters/oracle_adapter.rb', line 80

def columns(table_name, name = nil) #:nodoc:
  columns = base_columns(table_name)
  
  # This is a hack. Once Oracle offers a better means of auto-generating IDs,
  # this will be fixed
  # FIXME must be fixed once composite primary keys are supported
  sql = "select t1.column_name from all_cons_columns t1 inner join all_constraints t2 on \
  (t1.owner = t2.owner AND t1.constraint_name = t2.constraint_name) where t1.table_name='#{table_name.upcase}' and t2.constraint_type='P'"
  primary_key_column_name = select_one(sql, "Primary Key")['column_name']

  columns.each do |column|
    column.primary_key=(oracle_downcase(primary_key_column_name) == column.name)
  end
end

#constraints(table_name, name = nil) ⇒ Object

:nodoc:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/connection_adapters/oracle_adapter.rb', line 95

def constraints(table_name, name = nil)#:nodoc:
  constraints = []    
  upcase_table_name = table_name.upcase   
  sql = "select UC.constraint_name, UC.constraint_type, UC.table_name, COL.column_name, \
    REF.r_constraint_name as referenced_constraint_name, REF.constraint_name as foreign_constraint_name, \
    REF.delete_rule as foreign_delete_rule, COLREF.table_name as foreign_table_name, COLREF.column_name as foreign_column_name from \
    (select owner, constraint_name, constraint_type, table_name, r_owner, r_constraint_name \
    from all_constraints where table_name='#{upcase_table_name}') UC inner join \
    (select constraint_name, table_name, column_name from all_cons_columns where table_name='#{upcase_table_name}') COL on \
    (COL.constraint_name = UC.constraint_name) left join all_constraints REF on \
    (UC.constraint_name=REF.constraint_name OR UC.constraint_name=REF.r_constraint_name) left join all_cons_columns COLREF on \
    (not COLREF.table_name='#{upcase_table_name}' AND (REF.constraint_name=COLREF.constraint_name OR REF.r_constraint_name=COLREF.constraint_name))"
    
  results = select_all(sql, name)     
  constraint_name_hash = {}
  results.each do |row| 
    # The author(s) of the Oracle adapter chose to selectively downcase column names for
    # "cleanliness" within our Rails code. I had to follow suit with constraint column & table names
    # so that model objects could look up their data values using the generated column accessor methods
    column_name = oracle_downcase(row['column_name'])
    unless row['foreign_column_name'].nil? then row['foreign_column_name'] = oracle_downcase(row['foreign_column_name']) end
    unless row['table_name'].nil? then row['table_name'] = oracle_downcase(row['table_name']) end
    unless row['foreign_table_name'].nil? then row['foreign_table_name'] = oracle_downcase(row['foreign_table_name']) end
    constraint_name = row['constraint_name']
    foreign_constraint_name = row['foreign_constraint_name']
    
    # Process constraints local to this table
    if !constraint_name_hash.has_key?(constraint_name)
      current_constraint = OracleConstraint.new(constraint_name, row['constraint_type'], row['table_name'], 
      column_name, row['referenced_constraint_name'], row['foreign_table_name'], 
      row['foreign_column_name'], row['foreign_delete_rule'])
      constraints << current_constraint
      constraint_name_hash[constraint_name] = current_constraint
    # This key is a composite
    else
      current_constraint = constraint_name_hash[constraint_name]
      # Unique Keys are currently the only type of composite keys supported
      if current_constraint.component_of_unique_key? 
        current_constraint.column_name.add(column_name)
      end
    end
    
    # Process constraints that reference this table's local constraints
    if !foreign_constraint_name.nil? && !constraint_name_hash.has_key?(foreign_constraint_name)
      current_foreign_constraint = OracleConstraint.new(foreign_constraint_name, OracleConstraint::FOREIGN_KEY_TYPE,
        row['foreign_table_name'], row['foreign_column_name'], constraint_name,  
        row['table_name'], row['column_name'], row['foreign_delete_rule'])
        constraints << current_foreign_constraint
        constraint_name_hash[foreign_constraint_name] = current_foreign_constraint
    # Composite FKs are currently not supported 
    # else
    #  constraint_name_hash[foreign_constraint_name].column_name.add(row['foreign_column_name'])
    end   
  end
  constraints       
end