Skip to content

Commit

Permalink
Issue #7: Upgrade HSQLDB dependency to org.hsqldb:hsqldb:jar:2.7.3
Browse files Browse the repository at this point in the history
- Return primary keys JDBCDatabaseMetaData.getPrimaryKeys ordered by
column name
- Order hsqldb primary keys by KEY_SEQ
  • Loading branch information
spannm committed Jul 15, 2024
1 parent de169c6 commit e0e509f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<dep.jackcess.version>5.0.0-SNAPSHOT</dep.jackcess.version>
<dep.jackcess-encrypt.version>4.0.2</dep.jackcess-encrypt.version>

<dep.hsqldb.version>2.7.1</dep.hsqldb.version>
<dep.hsqldb.version>2.7.3</dep.hsqldb.version>
<dep.hsqldb.classifier/>

<dep.slf4j.version>2.0.13</dep.slf4j.version>
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/net/ucanaccess/converters/Persist2Jet.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,20 +640,21 @@ public void createIndex(String tableName, String indexName) throws IOException,

public void createPrimaryKey(String tableName) throws IOException, SQLException {
UcanaccessConnection conn = UcanaccessConnection.getCtxConnection();
Database db = conn.getDbIO();
String ntn = escape4Hsqldb(tableName);
String tn = escape4Access(tableName);
Table t = db.getTable(tn);
ResultSet pkrs = conn.getHSQLDBConnection().getMetaData().getPrimaryKeys(null, null, ntn.toUpperCase());
List<String> cols = new ArrayList<>();
IndexBuilder ib = new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME).withPrimaryKey();
while (pkrs.next()) {
String colName = pkrs.getString(COLUMN_NAME);
Metadata mt = new Metadata(conn);
colName = mt.getColumnName(ntn, colName);
cols.add(colName);
Table t = conn.getDbIO().getTable(escape4Access(tableName));
Metadata md = new Metadata(conn);

Map<Short, String> cols = new TreeMap<>();
try (ResultSet rs = conn.getHSQLDBConnection().getMetaData().getPrimaryKeys(null, null, ntn.toUpperCase())) {
while (rs.next()) {
cols.put(rs.getShort(KEY_SEQ), md.getColumnName(ntn, rs.getString(COLUMN_NAME)));
}
}
ib.withColumns(cols).addToTable(t);

new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME)
.withPrimaryKey()
.withColumns(cols.values())
.addToTable(t);
}

public void createForeignKey(String tableName, String referencedTable) throws IOException, SQLException {
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/net/ucanaccess/jdbc/UcanaccessDatabaseMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,25 @@ private String select(String htableName, List<String> exclude, List<String> repl
StringBuilder sb = new StringBuilder("SELECT ");

for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String cn = rsmd.getColumnName(i);
if (exclude.contains(cn)) {
String es = replace.get(exclude.indexOf(cn));
String colName = rsmd.getColumnName(i);
if (exclude.contains(colName)) {
String es = replace.get(exclude.indexOf(colName));
sb.append(comma);

if (es == null) {
sb.append(CAST_EXPR).append(cn);
sb.append(CAST_EXPR).append(colName);
} else if (PUBLIC.equals(es)) {
sb.append("'PUBLIC' AS ").append(cn);
sb.append("'PUBLIC' AS ").append(colName);
} else if (es.startsWith(CAST_EXPR)) {
sb.append(es);
} else {
String suffix = es.indexOf('.') > 0 ? "" : "r.";
sb.append(suffix).append(es).append(" AS ").append(cn);
sb.append(suffix).append(es).append(" AS ").append(colName);
}
} else {
sb.append(comma).append("l.").append(cn);

sb.append(comma).append("l.").append(colName);
}
comma = ",";
comma = ", ";
}
return sb.toString();
}
Expand Down Expand Up @@ -379,7 +378,7 @@ public ResultSet getCrossReference(String parentCatalog, String parentSchema, St
+ and("FKTABLE_CAT", "=", PUBLIC)
+ and("FKTABLE_SCHEM", "=", PUBLIC)
+ and("FKTABLE_NAME", "=", foreignTable)
+ " ORDER BY FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ";
+ " " + ORDER_BY + " FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, " + KEY_SEQ;

return executeQuery(select);
} catch (SQLException _ex) {
Expand Down Expand Up @@ -477,7 +476,7 @@ public ResultSet getExportedKeys(String catalog, String schema, String table) th
+ "ON( l.FKTABLE_NAME= v.ESCAPED_TABLE_NAME AND l.FKCOLUMN_NAME= v.ESCAPED_COLUMN_NAME)"
+ and("PKTABLE_CAT", "=", PUBLIC, " WHERE ")
+ and("PKTABLE_SCHEM", "=", PUBLIC) + and("PKTABLE_NAME", "=", table)
+ " ORDER BY FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ";
+ " " + ORDER_BY + " FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ";

return executeQuery(select);
} catch (SQLException _ex) {
Expand Down Expand Up @@ -542,7 +541,7 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) th
+ "ON( l.PKTABLE_NAME= v.ESCAPED_TABLE_NAME AND l.PKCOLUMN_NAME= v.ESCAPED_COLUMN_NAME)"
+ and("FKTABLE_CAT", "=", PUBLIC, " WHERE ")
+ and("FKTABLE_SCHEM", "=", PUBLIC) + and("FKTABLE_NAME", "=", table)
+ " ORDER BY PKTABLE_CAT, PKTABLE_SCHEM, PKTABLE_NAME, KEY_SEQ";
+ " " + ORDER_BY + " PKTABLE_CAT, PKTABLE_SCHEM, PKTABLE_NAME, " + KEY_SEQ;
return executeQuery(select);
} catch (SQLException _ex) {
throw new UcanaccessSQLException(_ex);
Expand Down Expand Up @@ -786,14 +785,14 @@ private boolean invokeWrapper(String catalog, String schema) {
}

@Override
public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException {
public ResultSet getPrimaryKeys(String _catalog, String _schema, String _table) throws SQLException {
if (_table == null) {
throw new InvalidParameterException("table", _table);
}
try {
if (table == null) {
throw new InvalidParameterException("table", table);
}
table = normalizeName(table);
if (invokeWrapper(catalog, schema)) {
return wrapped.getPrimaryKeys(catalog, schema, table);
String tn = normalizeName(_table);
if (invokeWrapper(_catalog, _schema)) {
return wrapped.getPrimaryKeys(_catalog, _schema, tn);
}
String cat = connection.isShowSchema() ? PUBLIC : null;
String schem = connection.isShowSchema() ? PUBLIC : null;
Expand All @@ -803,7 +802,8 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String table) thr
+ on(List.of(TABLE_NAME, COLUMN_NAME),
List.of(ESCAPED_TABLE_NAME, ESCAPED_COLUMN_NAME))
+ and(TABLE_CAT, "=", PUBLIC, " WHERE ")
+ and(TABLE_SCHEM, "=", PUBLIC) + and(TABLE_NAME, "=", table);
+ and(TABLE_SCHEM, "=", PUBLIC) + and(TABLE_NAME, "=", tn)
+ " " + ORDER_BY + " " + COLUMN_NAME;
return executeQuery(select);
} catch (SQLException _ex) {
throw new UcanaccessSQLException(_ex);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/ucanaccess/type/SqlConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class SqlConstants {
public static final String IS_CURRENCY = "IS_CURRENCY";
public static final String IS_NOT_NULL = "IS NOT NULL";
public static final String IS_NULL = "IS NULL";
public static final String KEY_SEQ = "KEY_SEQ";
public static final String LIKE = "LIKE";
public static final String LONGVARCHAR = "LONGVARCHAR";
public static final String NON_UNIQUE = "NON_UNIQUE";
Expand All @@ -50,6 +51,7 @@ public final class SqlConstants {
public static final String PIVOT = "PIVOT";
public static final String PKCOLUMN_NAME = "PKCOLUMN_NAME";
public static final String PKTABLE_NAME = "PKTABLE_NAME";
public static final String PK_NAME = "PK_NAME";
public static final String PUBLIC = "PUBLIC";
public static final String SELECT = "SELECT";
public static final String SELECT_COLUMN_ESCAPED = "SELECT_COLUMN_ESCAPED";
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/net/ucanaccess/jdbc/AlterTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ void testCreatePk(AccessVersion _accessVersion) throws SQLException, IOException
init(_accessVersion);

try (UcanaccessStatement st = ucanaccess.createStatement()) {
String tbl = "AAA n";
st.execute("ALTER TABLE [" + tbl + "] ADD PRIMARY KEY (baaaa, a)");
st.execute("ALTER TABLE [AAA n] ADD PRIMARY KEY (baaaa, a)");
Database db = ucanaccess.getDbIO();
assertThat(db.getTable(tbl).getPrimaryKeyIndex().getColumns().stream().map(Column::getName))
Table tbl = db.getTable("AAA n");
assertThat(tbl.getPrimaryKeyIndex().getColumns().stream().map(Column::getName))
.containsExactly("baaaa", "A");

st.execute("ALTER TABLE Sample ADD PRIMARY KEY (RegionId)");
Expand Down

0 comments on commit e0e509f

Please sign in to comment.