-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#124: Started rework on metadata reading.
- Loading branch information
1 parent
9a1b6c0
commit a453c9c
Showing
6 changed files
with
241 additions
and
19 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...irtualschema-jdbc-adapter/src/main/java/com/exasol/adapter/jdbc/ColumnMetadataReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.exasol.adapter.jdbc; | ||
|
||
import static com.exasol.adapter.jdbc.RemoteMetadataReaderConstants.*; | ||
|
||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.exasol.adapter.metadata.ColumnMetadata; | ||
import com.exasol.adapter.metadata.DataType; | ||
|
||
public class ColumnMetadataReader { | ||
private static final String COLUMN_NAME_COLUMN = "COLUMN_NAME"; | ||
private final Connection connection; | ||
|
||
public ColumnMetadataReader(final Connection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
public List<ColumnMetadata> mapColumns(final String tableName) throws SQLException { | ||
final List<ColumnMetadata> columns = new ArrayList<>(); | ||
try (final ResultSet remoteColumns = this.connection.getMetaData().getColumns(ANY_CATALOG, ANY_SCHEMA, | ||
tableName, ANY_COLUMN)) { | ||
while (remoteColumns.next()) { | ||
final ColumnMetadata metadata = mapColumn(remoteColumns); | ||
columns.add(metadata); | ||
} | ||
} | ||
return columns; | ||
} | ||
|
||
private ColumnMetadata mapColumn(final ResultSet remoteColumn) throws SQLException { | ||
return ColumnMetadata.builder() // | ||
.name(remoteColumn.getString(COLUMN_NAME_COLUMN)) // | ||
.type(DataType.createBool()) // FIXME | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...irtualschema-jdbc-adapter/src/main/java/com/exasol/adapter/jdbc/RemoteMetadataReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.exasol.adapter.jdbc; | ||
|
||
import static com.exasol.adapter.jdbc.RemoteMetadataReaderConstants.*; | ||
|
||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import com.exasol.adapter.metadata.*; | ||
|
||
/** | ||
* This class implements basic reading of database metadata from JDBC | ||
* | ||
* <p> | ||
* See <a href="https://docs.oracle.com/javase/8/docs/api/java/sql/DatabaseMetaData.html">java.sql.DatabaseMetaData</a> | ||
*/ | ||
public class RemoteMetadataReader { | ||
private static final Logger LOGGER = Logger.getLogger(RemoteMetadataReader.class.getName()); | ||
private static final String TABLE_NAME_COLUMN = "TABLE_NAME"; | ||
|
||
private final Connection connection; | ||
|
||
public RemoteMetadataReader(final Connection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
public SchemaMetadata readRemoteSchemaMetadata() throws RemoteMetadataReaderException { | ||
try { | ||
final DatabaseMetaData remoteMetadata = this.connection.getMetaData(); | ||
final String adapterNotes = null; | ||
final List<TableMetadata> tables = extractTableMetadata(remoteMetadata); | ||
return new SchemaMetadata(adapterNotes, tables); | ||
} catch (final SQLException exception) { | ||
throw new RemoteMetadataReaderException("Unable to read remote schema metadata.", exception); | ||
} | ||
} | ||
|
||
private List<TableMetadata> extractTableMetadata(final DatabaseMetaData remoteMetadata) throws SQLException { | ||
final List<TableMetadata> translatedTables = new ArrayList<>(); | ||
try (final ResultSet remoteTables = remoteMetadata.getTables(ANY_CATALOG, ANY_SCHEMA, ANY_TABLE, ANY_TYPE)) { | ||
mapTables(translatedTables, remoteTables); | ||
return translatedTables; | ||
} | ||
} | ||
|
||
private void mapTables(final List<TableMetadata> translatedTables, final ResultSet remoteTables) | ||
throws SQLException { | ||
while (remoteTables.next()) { | ||
final TableMetadata tableMetadata = mapTable(remoteTables); | ||
translatedTables.add(tableMetadata); | ||
} | ||
} | ||
|
||
private TableMetadata mapTable(final ResultSet remoteTable) throws SQLException { | ||
final String tableName = remoteTable.getString(TABLE_NAME_COLUMN); | ||
LOGGER.info(() -> "Mapping metadata for table \"" + tableName + "\""); | ||
final String adapterNotes = null; | ||
final List<ColumnMetadata> columns = new ColumnMetadataReader(this.connection).mapColumns(tableName); | ||
final String comment = null; | ||
return new TableMetadata(tableName, adapterNotes, columns, comment); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ema-jdbc-adapter/src/main/java/com/exasol/adapter/jdbc/RemoteMetadataReaderConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.exasol.adapter.jdbc; | ||
|
||
public final class RemoteMetadataReaderConstants { | ||
private RemoteMetadataReaderConstants() { | ||
// prevent instantiation | ||
} | ||
|
||
public static final String ANY_CATALOG = null; | ||
public static final String ANY_SCHEMA = null; | ||
public static final String ANY_TABLE = "%"; | ||
public static final String[] ANY_TYPE = null; | ||
public static final String ANY_COLUMN = "%"; | ||
} |
12 changes: 12 additions & 0 deletions
12
...ema-jdbc-adapter/src/main/java/com/exasol/adapter/jdbc/RemoteMetadataReaderException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.exasol.adapter.jdbc; | ||
|
||
/** | ||
* This class represents exceptional conditions that occur during attempts to extract schema metadata from a remote data | ||
* source. | ||
*/ | ||
public class RemoteMetadataReaderException extends Exception { | ||
|
||
public RemoteMetadataReaderException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...alschema-jdbc-adapter/src/test/java/com/exasol/adapter/jdbc/RemoteMetadataReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.exasol.adapter.jdbc; | ||
|
||
import static org.hamcrest.Matchers.*; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.sql.*; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.*; | ||
|
||
import com.exasol.adapter.metadata.*; | ||
|
||
class RemoteMetadataReaderTest { | ||
@Mock | ||
private Connection connectionMock; | ||
@Mock | ||
private DatabaseMetaData remoteMetadataMock; | ||
|
||
@BeforeEach | ||
void beforeEach() throws SQLException { | ||
MockitoAnnotations.initMocks(this); | ||
when(this.connectionMock.getMetaData()).thenReturn(this.remoteMetadataMock); | ||
} | ||
|
||
@Test | ||
void testReadEmptyRemoteMetadata() throws RemoteMetadataReaderException, SQLException { | ||
mockGetAllTablesReturnsEmptyList(); | ||
final RemoteMetadataReader reader = new RemoteMetadataReader(this.connectionMock); | ||
final SchemaMetadata metadata = reader.readRemoteSchemaMetadata(); | ||
assertThat(metadata.getTables(), emptyIterableOf(TableMetadata.class)); | ||
} | ||
|
||
private void mockGetAllTablesReturnsEmptyList() throws SQLException { | ||
final ResultSet remoteTables = Mockito.mock(ResultSet.class); | ||
when(remoteTables.next()).thenReturn(false); | ||
mockGetAllTables(remoteTables); | ||
} | ||
|
||
@Test | ||
void testReadRemoteMetadata() throws RemoteMetadataReaderException, SQLException { | ||
mockGetColumnsCalls(); | ||
mockGetTableCalls(); | ||
final RemoteMetadataReader reader = new RemoteMetadataReader(this.connectionMock); | ||
final SchemaMetadata metadata = reader.readRemoteSchemaMetadata(); | ||
final List<TableMetadata> tables = metadata.getTables(); | ||
final TableMetadata tableAMetadata = tables.get(0); | ||
final TableMetadata tableBMetadata = tables.get(1); | ||
final List<ColumnMetadata> columnsAMetadata = tableAMetadata.getColumns(); | ||
final List<ColumnMetadata> columnsBMetadata = tableBMetadata.getColumns(); | ||
assertAll(() -> assertThat(tables, iterableWithSize(2)), | ||
() -> assertThat(tableAMetadata.getName(), equalTo("TABLE_A")), | ||
() -> assertThat(columnsAMetadata, iterableWithSize(2)), | ||
() -> assertThat(tableBMetadata.getName(), equalTo("TABLE_B")), | ||
() -> assertThat(columnsBMetadata, iterableWithSize(3))); | ||
} | ||
|
||
private void mockGetColumnsCalls() throws SQLException { | ||
mockTableA(); | ||
mockTableB(); | ||
} | ||
|
||
private void mockTableA() throws SQLException { | ||
final ResultSet tableAColumns = Mockito.mock(ResultSet.class); | ||
when(tableAColumns.next()).thenReturn(true, true, false); | ||
when(tableAColumns.getString("COLUMN_NAME")).thenReturn("COLUMN_A1", "COLUMN_A2"); | ||
when(tableAColumns.getBoolean("NULLABLE")).thenReturn(true, false); | ||
when(this.remoteMetadataMock.getColumns(null, null, "TABLE_A", "%")).thenReturn(tableAColumns); | ||
} | ||
|
||
private void mockTableB() throws SQLException { | ||
final ResultSet tableBColumns = Mockito.mock(ResultSet.class); | ||
when(tableBColumns.next()).thenReturn(true, true, true, false); | ||
when(tableBColumns.getString("COLUMN_NAME")).thenReturn("COLUMN_B1", "COLUMN_B2", "COLUMN_B3"); | ||
when(tableBColumns.getBoolean("NULLABLE")).thenReturn(false, true, false); | ||
when(this.remoteMetadataMock.getColumns(null, null, "TABLE_B", "%")).thenReturn(tableBColumns); | ||
} | ||
|
||
private void mockGetTableCalls() throws SQLException { | ||
final ResultSet tables = Mockito.mock(ResultSet.class); | ||
when(tables.next()).thenReturn(true, true, false); | ||
when(tables.getString("TABLE_NAME")).thenReturn("TABLE_A", "TABLE_B"); | ||
mockGetAllTables(tables); | ||
} | ||
|
||
private void mockGetAllTables(final ResultSet tables) throws SQLException { | ||
when(this.remoteMetadataMock.getTables(null, null, "%", null)).thenReturn(tables); | ||
} | ||
} |