Skip to content

Commit

Permalink
Support load unique key for H2 apache#28035 (apache#28154)
Browse files Browse the repository at this point in the history
* Support Unique Key for H2

* formatting
  • Loading branch information
sindhunaydu authored Sep 12, 2023
1 parent 15e4677 commit e406a15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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=?"
Expand Down Expand Up @@ -126,10 +126,14 @@ private Map<String, Collection<IndexMetaData>> 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);

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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;
}

Expand All @@ -160,6 +161,8 @@ private void assertTableMetaDataMap(final Collection<SchemaMetaData> schemaMetaD
assertThat(columnsIterator.next(), is(new ColumnMetaData("name", Types.VARCHAR, false, false, false, false, false, true)));
assertThat(actualTableMetaData.getIndexes().size(), is(1));
Iterator<IndexMetaData> indexesIterator = actualTableMetaData.getIndexes().iterator();
assertThat(indexesIterator.next(), is(new IndexMetaData("id")));
IndexMetaData indexMetaData = new IndexMetaData("id");
indexMetaData.setUnique(true);
assertThat(indexesIterator.next(), is(indexMetaData));
}
}

0 comments on commit e406a15

Please sign in to comment.