1 21 package net.sf.hajdbc; 22 23 import java.sql.Connection ; 24 import java.sql.DatabaseMetaData ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 34 40 public class ForeignKeyConstraint extends UniqueConstraint 41 { 42 private String foreignSchema; 43 private String foreignTable; 44 private List <String > foreignColumnList = new LinkedList <String >(); 45 private int updateRule; 46 private int deleteRule; 47 private int deferrability; 48 49 55 public ForeignKeyConstraint(String name, String schema, String table) 56 { 57 super(name, schema, table); 58 } 59 60 63 public String getForeignTable() 64 { 65 return this.foreignTable; 66 } 67 68 71 public String getForeignSchema() 72 { 73 return this.foreignSchema; 74 } 75 76 79 public List <String > getForeignColumnList() 80 { 81 return this.foreignColumnList; 82 } 83 84 87 public int getDeleteRule() 88 { 89 return this.deleteRule; 90 } 91 92 95 public int getUpdateRule() 96 { 97 return this.updateRule; 98 } 99 100 103 public int getDeferrability() 104 { 105 return this.deferrability; 106 } 107 108 111 public void setDeferrability(int deferrability) 112 { 113 this.deferrability = deferrability; 114 } 115 116 119 public void setDeleteRule(int deleteRule) 120 { 121 this.deleteRule = deleteRule; 122 } 123 124 127 public void setForeignSchema(String foreignSchema) 128 { 129 this.foreignSchema = foreignSchema; 130 } 131 132 135 public void setForeignTable(String foreignTable) 136 { 137 this.foreignTable = foreignTable; 138 } 139 140 143 public void setUpdateRule(int updateRule) 144 { 145 this.updateRule = updateRule; 146 } 147 148 155 public static Collection <ForeignKeyConstraint> collect(Connection connection, Map <String , List <String >> schemaMap) throws SQLException 156 { 157 Map <String , ForeignKeyConstraint> foreignKeyMap = new HashMap <String , ForeignKeyConstraint>(); 158 DatabaseMetaData metaData = connection.getMetaData(); 159 160 for (Map.Entry <String , List <String >> schemaMapEntry: schemaMap.entrySet()) 161 { 162 String schema = schemaMapEntry.getKey(); 163 List <String > tableList = schemaMapEntry.getValue(); 164 165 for (String table: tableList) 166 { 167 ResultSet resultSet = metaData.getImportedKeys(null, schema, table); 168 169 while (resultSet.next()) 170 { 171 String name = resultSet.getString("FK_NAME"); 172 173 ForeignKeyConstraint foreignKey = foreignKeyMap.get(name); 174 175 if (foreignKey == null) 176 { 177 foreignKey = new ForeignKeyConstraint(name, schema, table); 178 179 foreignKey.foreignSchema = resultSet.getString("PKTABLE_SCHEM"); 180 foreignKey.foreignTable = resultSet.getString("PKTABLE_NAME"); 181 foreignKey.deleteRule = resultSet.getInt("DELETE_RULE"); 182 foreignKey.updateRule = resultSet.getInt("UPDATE_RULE"); 183 foreignKey.deferrability = resultSet.getInt("DEFERRABILITY"); 184 185 foreignKeyMap.put(name, foreignKey); 186 } 187 188 String column = resultSet.getString("FKCOLUMN_NAME"); 189 String foreignColumn = resultSet.getString("PKCOLUMN_NAME"); 190 191 foreignKey.getColumnList().add(column); 192 foreignKey.getForeignColumnList().add(foreignColumn); 193 } 194 195 resultSet.close(); 196 } 197 } 198 199 return foreignKeyMap.values(); 200 } 201 } 202 | Popular Tags |