/**
* Returns a string that looks like:
*
* (PK_COL1, PK_COL2, PK_COL3, ...)
*
* or null if there is no primary key for the specified table.
*
* @param sourceConn
* @param ti
* @return
* @throws SQLException
*/
/**
* Returns a string that looks like: (PK_COL1, PK_COL2, PK_COL3, ...) or null if there is no primary key
* for the specified table.
*
* @param sourceConn
* @param ti
* @return
* @throws SQLException
*/
public static String getPKColumnString(ISQLConnection sourceConn, ITableInfo ti) throws SQLException {
List<String> pkColumns = getPKColumnList(sourceConn, ti);
if (pkColumns == null || pkColumns.size() == 0) {
return null;
}
StringBuilder sb = new StringBuilder("(");
Iterator<String> i = pkColumns.iterator();
while (i.hasNext()) {
String columnName = i.next();
sb.append(columnName);
if (i.hasNext()) {
sb.append(", ");
}
}
sb.append(")");
return sb.toString();
}
/**
* Returns a list of primary keys or null if there are no primary keys for the specified table.
*
* @param sourceConn
* @param ti
* @return
* @throws SQLException
*/
private static List<String> getPKColumnList(ISQLConnection sourceConn, ITableInfo ti) throws SQLException {
ArrayList<String> pkColumns = new ArrayList<String>();
DatabaseMetaData md = sourceConn.getConnection().getMetaData();
ResultSet rs = null;
if (md.supportsCatalogsInTableDefinitions()) {
rs = md.getPrimaryKeys(ti.getCatalogName(), null, ti.getSimpleName());
}
else
if (md.supportsSchemasInTableDefinitions()) {
rs = md.getPrimaryKeys(null, ti.getSchemaName(), ti.getSimpleName());
}
else {
rs = md.getPrimaryKeys(null, null, ti.getSimpleName());
}
while (rs.next()) {
String keyColumn = rs.getString(4);
if (keyColumn != null) {
pkColumns.add(keyColumn);
}
}
if (pkColumns.size() == 0) {
return null;
}
return pkColumns;
}
|