From abdb9317fdbe0252d71afe2f65c0019643149e57 Mon Sep 17 00:00:00 2001 From: Markus Spann Date: Fri, 12 Jul 2024 15:30:07 +0200 Subject: [PATCH] Issue #7: Upgrade HSQLDB dependency to org.hsqldb:hsqldb:jar:2.7.3 --- pom.xml | 2 +- .../ucanaccess/converters/Persist2Jet.java | 25 ++++++------ .../jdbc/UcanaccessDatabaseMetadata.java | 40 +++++++++---------- .../net/ucanaccess/type/SqlConstants.java | 2 + .../net/ucanaccess/jdbc/AlterTableTest.java | 6 +-- 5 files changed, 39 insertions(+), 36 deletions(-) diff --git a/pom.xml b/pom.xml index c05477a..04ad43f 100644 --- a/pom.xml +++ b/pom.xml @@ -100,7 +100,7 @@ 5.0.0-SNAPSHOT 4.0.2 - 2.7.1 + 2.7.3 2.0.13 diff --git a/src/main/java/net/ucanaccess/converters/Persist2Jet.java b/src/main/java/net/ucanaccess/converters/Persist2Jet.java index 51d276b..78fe38e 100644 --- a/src/main/java/net/ucanaccess/converters/Persist2Jet.java +++ b/src/main/java/net/ucanaccess/converters/Persist2Jet.java @@ -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 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 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 { diff --git a/src/main/java/net/ucanaccess/jdbc/UcanaccessDatabaseMetadata.java b/src/main/java/net/ucanaccess/jdbc/UcanaccessDatabaseMetadata.java index dbf90e9..d465aca 100644 --- a/src/main/java/net/ucanaccess/jdbc/UcanaccessDatabaseMetadata.java +++ b/src/main/java/net/ucanaccess/jdbc/UcanaccessDatabaseMetadata.java @@ -108,26 +108,25 @@ private String select(String htableName, List exclude, List 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(); } @@ -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) { @@ -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) { @@ -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); @@ -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; @@ -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 + " " + KEY_SEQ; return executeQuery(select); } catch (SQLException _ex) { throw new UcanaccessSQLException(_ex); diff --git a/src/main/java/net/ucanaccess/type/SqlConstants.java b/src/main/java/net/ucanaccess/type/SqlConstants.java index 079f2b8..a04b60d 100644 --- a/src/main/java/net/ucanaccess/type/SqlConstants.java +++ b/src/main/java/net/ucanaccess/type/SqlConstants.java @@ -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"; @@ -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"; diff --git a/src/test/java/net/ucanaccess/jdbc/AlterTableTest.java b/src/test/java/net/ucanaccess/jdbc/AlterTableTest.java index c27598c..86f1a0b 100644 --- a/src/test/java/net/ucanaccess/jdbc/AlterTableTest.java +++ b/src/test/java/net/ucanaccess/jdbc/AlterTableTest.java @@ -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)");