Class: ActiveRecord::ConnectionAdapters::IBM_DB2_I5

Inherits:
IBM_DB2
  • Object
show all
Defined in:
lib/connection_adapters/ibm_db_adapter.rb

Instance Method Summary collapse

Instance Method Details

#constraints(table_name, name = nil) ⇒ Object

:nodoc:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/connection_adapters/ibm_db_adapter.rb', line 136

def constraints(table_name, name = nil)#:nodoc:
 	constraints = []  
			upcase_table_name = table_name.upcase
 	sql = %Q{
 		select CST.constraint_schema, CST.constraint_name, LOWER(CST.table_name) as table_name, CST.constraint_type, 
					LOWER(COL.column_name) as column_name, REF.constraint_name as foreign_constraint_name, REF.unique_constraint_name as referenced_constraint_name, 
   			REF.delete_rule, LOWER(COLREF.table_name) as foreign_table_name, LOWER(COLREF.column_name) as foreign_column_name 
				from 
   			((select * from SYSCST where table_name='#{upcase_table_name}') as CST 
			inner join (select column_name, constraint_name from SYSCSTCOL where table_name='#{upcase_table_name}') as COL 
					on (CST.constraint_name=COL.constraint_name) 
					left outer join SYSREFCST REF 
					on (CST.constraint_name=REF.unique_constraint_name or CST.constraint_name=REF.constraint_name) 
			left join SYSCSTCOL AS COLREF 
					on (NOT COLREF.table_name='#{upcase_table_name}' 
						AND (REF.unique_constraint_name=COLREF.constraint_name or REF.constraint_name=COLREF.constraint_name)))
				}     
 	results = IBM_DB::exec(@adapter.connection, sql)
 	constraint_name_hash = {}
   
 	# Note that column names in constraint objects are downcased in order to
 	# be comparable with the column names produced by IBM_DBAdapter.columns  
 	while row = IBM_DB::fetch_assoc(results)
   		constraint_name = row['constraint_name']
   		foreign_constraint_name = row['foreign_constraint_name']
				column_name = row['column_name']
				table_name = row['table_name']
				foreign_column = row['foreign_column_name']				

   		# Process constraints local to this table
   		if !constraint_name_hash.has_key?(constraint_name)
     		current_constraint = IBM_DBConstraint.new(row['constraint_schema'], constraint_name, row['constraint_type'], 
       		table_name, column_name, row['referenced_constraint_name'], row['foreign_table_name'], 
       		foreign_column.to_a, row['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]
					current_constraint.column_names << column_name unless current_constraint.column_names.include?(column_name)
					current_constraint.referenced_column_names << foreign_column unless current_constraint.referenced_column_names.include?(foreign_column)
   		end
   
   		# Process constraints that reference this table's local constraints
   		if foreign_constraint_name && foreign_constraint_name != constraint_name
   			if !constraint_name_hash.has_key?(foreign_constraint_name)
     			current_foreign_constraint = IBM_DBConstraint.new(row['constraint_schema'], foreign_constraint_name, 
     				IBM_DBConstraint::FOREIGN_KEY_TYPE, row['foreign_table_name'], foreign_column, 
constraint_name, table_name, column_name.to_a, row['delete_rule'])
       			constraints << current_foreign_constraint
       			constraint_name_hash[foreign_constraint_name] = current_foreign_constraint	
   			# Composite FKs
   			else					
						current_foreign_constraint = constraint_name_hash[foreign_constraint_name]
						current_foreign_constraint.column_names << foreign_column unless current_foreign_constraint.column_names.include?(foreign_column)
						current_foreign_constraint.referenced_column_names << column_name unless current_foreign_constraint.referenced_column_names.include?(column_name)	
   			end  
				end      
 	end
 	constraints
end