From e406a153c595a315fcf9cd0950a7eb1e206eaa7c Mon Sep 17 00:00:00 2001 From: Sindhu Date: Mon, 11 Sep 2023 23:59:42 -0700 Subject: [PATCH] Support load unique key for H2 #28035 (#28154) * Support Unique Key for H2 * formatting --- .../h2/metadata/data/loader/H2MetaDataLoader.java | 8 ++++++-- .../h2/metadata/data/loader/H2MetaDataLoaderTest.java | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoader.java b/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoader.java index fac7aa8f9e0c9..50b3251d42bc4 100644 --- a/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoader.java +++ b/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoader.java @@ -51,7 +51,7 @@ public final class H2MetaDataLoader implements DialectMetaDataLoader { private static final String TABLE_META_DATA_SQL_IN_TABLES = TABLE_META_DATA_NO_ORDER + " AND UPPER(TABLE_NAME) IN (%s)" + ORDER_BY_ORDINAL_POSITION; - private static final String INDEX_META_DATA_SQL = "SELECT TABLE_CATALOG, TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA.INDEXES" + private static final String INDEX_META_DATA_SQL = "SELECT TABLE_CATALOG, TABLE_NAME, INDEX_NAME, INDEX_TYPE_NAME FROM INFORMATION_SCHEMA.INDEXES" + " WHERE TABLE_CATALOG=? AND TABLE_SCHEMA=? AND UPPER(TABLE_NAME) IN (%s)"; private static final String PRIMARY_KEY_META_DATA_SQL = "SELECT TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_CATALOG=? AND TABLE_SCHEMA=?" @@ -126,10 +126,14 @@ private Map> loadIndexMetaData(final Connectio while (resultSet.next()) { String indexName = resultSet.getString("INDEX_NAME"); String tableName = resultSet.getString("TABLE_NAME"); + boolean uniqueIndex = "UNIQUE INDEX".equals(resultSet.getString("INDEX_TYPE_NAME")); if (!result.containsKey(tableName)) { result.put(tableName, new LinkedList<>()); } - result.get(tableName).add(new IndexMetaData(indexName)); + IndexMetaData indexMetaData = new IndexMetaData(indexName); + indexMetaData.setUnique(uniqueIndex); + result.get(tableName).add(indexMetaData); + } } } diff --git a/infra/database/type/h2/src/test/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoaderTest.java b/infra/database/type/h2/src/test/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoaderTest.java index 30ad5418a051b..38e548485aed2 100644 --- a/infra/database/type/h2/src/test/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoaderTest.java +++ b/infra/database/type/h2/src/test/java/org/apache/shardingsphere/infra/database/h2/metadata/data/loader/H2MetaDataLoaderTest.java @@ -55,7 +55,7 @@ void assertLoadWithoutTables() throws SQLException { .executeQuery()).thenReturn(resultSet); ResultSet indexResultSet = mockIndexMetaDataResultSet(); when(dataSource.getConnection().prepareStatement( - "SELECT TABLE_CATALOG, TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_CATALOG=? AND TABLE_SCHEMA=? AND UPPER(TABLE_NAME) IN ('TBL')") + "SELECT TABLE_CATALOG, TABLE_NAME, INDEX_NAME, INDEX_TYPE_NAME FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_CATALOG=? AND TABLE_SCHEMA=? AND UPPER(TABLE_NAME) IN ('TBL')") .executeQuery()).thenReturn(indexResultSet); ResultSet primaryKeys = mockPrimaryKeysMetaDataResultSet(); when(dataSource.getConnection().prepareStatement( @@ -78,7 +78,7 @@ void assertLoadWithTables() throws SQLException { .executeQuery()).thenReturn(resultSet); ResultSet indexResultSet = mockIndexMetaDataResultSet(); when(dataSource.getConnection().prepareStatement( - "SELECT TABLE_CATALOG, TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_CATALOG=? AND TABLE_SCHEMA=? AND UPPER(TABLE_NAME) IN ('TBL')") + "SELECT TABLE_CATALOG, TABLE_NAME, INDEX_NAME, INDEX_TYPE_NAME FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_CATALOG=? AND TABLE_SCHEMA=? AND UPPER(TABLE_NAME) IN ('TBL')") .executeQuery()).thenReturn(indexResultSet); ResultSet primaryKeys = mockPrimaryKeysMetaDataResultSet(); when(dataSource.getConnection().prepareStatement( @@ -142,6 +142,7 @@ private ResultSet mockIndexMetaDataResultSet() throws SQLException { when(result.next()).thenReturn(true, false); when(result.getString("INDEX_NAME")).thenReturn("id"); when(result.getString("TABLE_NAME")).thenReturn("tbl"); + when(result.getString("INDEX_TYPE_NAME")).thenReturn("UNIQUE INDEX"); return result; } @@ -160,6 +161,8 @@ private void assertTableMetaDataMap(final Collection schemaMetaD assertThat(columnsIterator.next(), is(new ColumnMetaData("name", Types.VARCHAR, false, false, false, false, false, true))); assertThat(actualTableMetaData.getIndexes().size(), is(1)); Iterator indexesIterator = actualTableMetaData.getIndexes().iterator(); - assertThat(indexesIterator.next(), is(new IndexMetaData("id"))); + IndexMetaData indexMetaData = new IndexMetaData("id"); + indexMetaData.setUnique(true); + assertThat(indexesIterator.next(), is(indexMetaData)); } }