Class: ActiveRecord::ConnectionAdapters::IBM_DB2_LUW
- Inherits:
-
IBM_DB2
- Object
- IBM_DB2
- ActiveRecord::ConnectionAdapters::IBM_DB2_LUW
- Defined in:
- lib/connection_adapters/ibm_db_adapter.rb
Instance Method Summary collapse
Instance Method Details
#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 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 |
# File 'lib/connection_adapters/ibm_db_adapter.rb', line 60 def constraints(table_name, name = nil) #:nodoc constraints = [] upcase_table_name = table_name.upcase sql = %Q{ select CST.tabschema, CST.constname, LOWER(CST.tabname) as tabname, CST.type, LOWER(COL.colname) as colname, REF.constname as foreign_constraint_name, LOWER(REF.tabname) as foreign_table_name, REF.refkeyname, LOWER(REF.reftabname) as reftabname, REF.deleterule, REF.fk_colnames as foreign_columns, REF.pk_colnames as referenced_columns from ((select * from SYSCAT.TABCONST where tabname='#{upcase_table_name}') as CST inner join (select colname, constname from SYSCAT.KEYCOLUSE where tabname='#{upcase_table_name}') as COL on (CST.constname=COL.constname)) left outer join SYSCAT.REFERENCES REF on (CST.constname=REF.refkeyname or CST.constname=REF.constname) } 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['constname'] foreign_constraint_name = row['foreign_constraint_name'] column_name = row['colname'] table_name = row['tabname'] referenced_columns = row['referenced_columns'].downcase.split if row['referenced_columns'] foreign_columns = row['foreign_columns'].downcase.split if row['foreign_columns'] # Process constraints local to this table if !constraint_name_hash.has_key?(constraint_name) current_constraint = IBM_DBConstraint.new(row['tabschema'], constraint_name, row['type'], table_name, column_name, row['refkeyname'], row['reftabname'], referenced_columns, row['deleterule']) 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) referenced_columns.each do |column| current_constraint.referenced_column_names << column unless current_constraint.referenced_column_names.include?(column) end end # Process constraints that reference this table's local constraints if foreign_constraint_name if !constraint_name_hash.has_key?(foreign_constraint_name) current_foreign_constraint = IBM_DBConstraint.new(row['tabschema'], foreign_constraint_name, IBM_DBConstraint::FOREIGN_KEY_TYPE, row['foreign_table_name'], foreign_columns, constraint_name, table_name, column_name, row['deleterule']) constraints << current_foreign_constraint constraint_name_hash[foreign_constraint_name] = current_foreign_constraint # Composite FK else current_foreign_constraint = constraint_name_hash[foreign_constraint_name] foreign_columns.each do |fc| current_foreign_constraint.column_names << fc unless current_foreign_constraint.column_names.include?(fc) end referenced_columns.each do |rc| current_foreign_constraint.referenced_column_names << rc unless current_foreign_constraint.referenced_column_names.include?(rc) end end end end constraints end |