1 21 package net.sf.hajdbc; 22 23 import java.sql.Connection ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 38 public class UniqueConstraint implements Comparable <UniqueConstraint> 39 { 40 private String name; 41 private String schema; 42 private String table; 43 private List <String > columnList = new LinkedList <String >(); 44 45 51 public UniqueConstraint(String name, String schema, String table) 52 { 53 this.name = name; 54 this.schema = schema; 55 this.table = table; 56 } 57 58 61 public List <String > getColumnList() 62 { 63 return this.columnList; 64 } 65 66 69 public String getName() 70 { 71 return this.name; 72 } 73 74 77 public String getSchema() 78 { 79 return this.schema; 80 } 81 82 85 public String getTable() 86 { 87 return this.table; 88 } 89 90 93 @Override 94 public boolean equals(Object object) 95 { 96 UniqueConstraint key = (UniqueConstraint) object; 97 98 return (key != null) && (key.name != null) && key.name.equals(this.name); 99 } 100 101 104 @Override 105 public int hashCode() 106 { 107 return this.name.hashCode(); 108 } 109 110 113 public int compareTo(UniqueConstraint constraint) 114 { 115 return this.name.compareTo(constraint.name); 116 } 117 118 127 public static Collection <UniqueConstraint> collect(Connection connection, String schema, String table, String primaryKeyName) throws SQLException 128 { 129 Map <String , UniqueConstraint> keyMap = new HashMap <String , UniqueConstraint>(); 130 131 ResultSet resultSet = connection.getMetaData().getIndexInfo(null, schema, table, true, false); 132 133 while (resultSet.next()) 134 { 135 String name = resultSet.getString("INDEX_NAME"); 136 137 if ((name == null) || name.equals(primaryKeyName)) continue; 138 139 UniqueConstraint key = keyMap.get(name); 140 141 if (key == null) 142 { 143 key = new UniqueConstraint(name, schema, table); 144 145 keyMap.put(name, key); 146 } 147 148 String column = resultSet.getString("COLUMN_NAME"); 149 150 key.getColumnList().add(column); 151 } 152 153 resultSet.close(); 154 155 return keyMap.values(); 156 } 157 } 158 | Popular Tags |