Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract metadata from query generator
- Loading branch information
Showing
7 changed files
with
211 additions
and
159 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
baremaps-database/src/main/java/com/baremaps/database/metadata/Column.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,28 @@ | ||
package com.baremaps.database.metadata; | ||
|
||
public record Column( | ||
String tableCat, | ||
String tableSchem, | ||
String tableName, | ||
String columnName, | ||
int dataType, | ||
String typeName, | ||
int columnSize, | ||
int decimalDigits, | ||
int numPrecRadix, | ||
int nullable, | ||
String remarks, | ||
String columnDef, | ||
int sqlDataType, | ||
int sqlDateTimeSub, | ||
int charOctetLenght, | ||
int ordinalPosition, | ||
String isNullable, | ||
String scopeCatalog, | ||
String scopeSchema, | ||
String scopeTable, | ||
short sourceDataType, | ||
String isAutoIncrement, | ||
String isGeneratedColumn) { | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
baremaps-database/src/main/java/com/baremaps/database/metadata/Metadata.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,137 @@ | ||
package com.baremaps.database.metadata; | ||
|
||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import javax.sql.DataSource; | ||
|
||
public class Metadata { | ||
|
||
private final DataSource dataSource; | ||
|
||
public Metadata(DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public List<TableMetaData> getTableMetaData() { | ||
return getTableMetaData(null, null, null, null); | ||
} | ||
|
||
public List<TableMetaData> getTableMetaData( | ||
String catalog, | ||
String schema, | ||
String tableNamePattern, | ||
String[] types) { | ||
Map<String, Table> descriptions = | ||
getTables(catalog, schema, tableNamePattern, types).stream() | ||
.collect(Collectors.toMap(Table::tableName, Function.identity())); | ||
Map<String, List<Column>> columns = | ||
getColumns(catalog, schema, tableNamePattern, null).stream() | ||
.collect(Collectors.groupingBy(Column::tableName)); | ||
Map<String, List<PrimaryKey>> primaryKeys = | ||
getPrimaryKeys(catalog, schema, tableNamePattern).stream() | ||
.collect(Collectors.groupingBy(PrimaryKey::tableName)); | ||
return descriptions.entrySet().stream() | ||
.map( | ||
entry -> | ||
new TableMetaData( | ||
entry.getValue(), | ||
primaryKeys.getOrDefault(entry.getKey(), List.of()), | ||
columns.getOrDefault(entry.getKey(), List.of()))) | ||
.toList(); | ||
} | ||
|
||
private List<Table> getTables( | ||
String catalog, | ||
String schemaPattern, | ||
String tableNamePattern, | ||
String[] types) { | ||
var tableDescriptions = new ArrayList<Table>(); | ||
try (var connection = dataSource.getConnection(); | ||
var resultSet = connection.getMetaData().getTables(catalog, schemaPattern, tableNamePattern, types)) { | ||
while (resultSet.next()) { | ||
tableDescriptions.add(new Table( | ||
resultSet.getString("TABLE_CAT"), | ||
resultSet.getString("TABLE_SCHEM"), | ||
resultSet.getString("TABLE_NAME"), | ||
resultSet.getString("TABLE_TYPE"), | ||
resultSet.getString("REMARKS"), | ||
resultSet.getString("TYPE_CAT"), | ||
resultSet.getString("TYPE_SCHEM"), | ||
resultSet.getString("TYPE_NAME"), | ||
resultSet.getString("SELF_REFERENCING_COL_NAME"), | ||
resultSet.getString("REF_GENERATION"))); | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return tableDescriptions; | ||
} | ||
|
||
private List<Column> getColumns( | ||
String catalog, | ||
String schemaPattern, | ||
String tableNamePattern, | ||
String columnNamePattern) { | ||
var tableColumns = new ArrayList<Column>(); | ||
try (var connection = dataSource.getConnection(); | ||
var resultSet = connection.getMetaData() | ||
.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern)) { | ||
while (resultSet.next()) { | ||
tableColumns.add(new Column( | ||
resultSet.getString("TABLE_CAT"), | ||
resultSet.getString("TABLE_SCHEM"), | ||
resultSet.getString("TABLE_NAME"), | ||
resultSet.getString("COLUMN_NAME"), | ||
resultSet.getInt("DATA_TYPE"), | ||
resultSet.getString("TYPE_NAME"), | ||
resultSet.getInt("COLUMN_SIZE"), | ||
resultSet.getInt("DECIMAL_DIGITS"), | ||
resultSet.getInt("NUM_PREC_RADIX"), | ||
resultSet.getInt("NULLABLE"), | ||
resultSet.getString("REMARKS"), | ||
resultSet.getString("COLUMN_DEF"), | ||
resultSet.getInt("SQL_DATA_TYPE"), | ||
resultSet.getInt("SQL_DATETIME_SUB"), | ||
resultSet.getInt("CHAR_OCTET_LENGTH"), | ||
resultSet.getInt("ORDINAL_POSITION"), | ||
resultSet.getString("IS_NULLABLE"), | ||
resultSet.getString("SCOPE_CATALOG"), | ||
resultSet.getString("SCOPE_SCHEMA"), | ||
resultSet.getString("SCOPE_TABLE"), | ||
resultSet.getShort("SOURCE_DATA_TYPE"), | ||
resultSet.getString("IS_AUTOINCREMENT"), | ||
resultSet.getString("IS_GENERATEDCOLUMN"))); | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return tableColumns; | ||
} | ||
|
||
private List<PrimaryKey> getPrimaryKeys( | ||
String catalog, | ||
String schemaPattern, | ||
String table) { | ||
var tablePrimaryKeyColumns = new ArrayList<PrimaryKey>(); | ||
try (var connection = dataSource.getConnection(); | ||
var resultSet = connection.getMetaData().getPrimaryKeys(catalog, schemaPattern, table)) { | ||
while (resultSet.next()) { | ||
tablePrimaryKeyColumns.add(new PrimaryKey( | ||
resultSet.getString("TABLE_CAT"), | ||
resultSet.getString("TABLE_SCHEM"), | ||
resultSet.getString("TABLE_NAME"), | ||
resultSet.getString("COLUMN_NAME"), | ||
resultSet.getShort("KEY_SEQ"), | ||
resultSet.getString("PK_NAME"))); | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return tablePrimaryKeyColumns; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
baremaps-database/src/main/java/com/baremaps/database/metadata/PrimaryKey.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,11 @@ | ||
package com.baremaps.database.metadata; | ||
|
||
public record PrimaryKey( | ||
String tableCat, | ||
String tableSchem, | ||
String tableName, | ||
String columnName, | ||
short keySeq, | ||
String pkName) { | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
baremaps-database/src/main/java/com/baremaps/database/metadata/Table.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,15 @@ | ||
package com.baremaps.database.metadata; | ||
|
||
public record Table( | ||
String tableCat, | ||
String tableSchem, | ||
String tableName, | ||
String tableType, | ||
String remarks, | ||
String typeCat, | ||
String typeSchem, | ||
String typeName, | ||
String selfReferencingColName, | ||
String refGeneration) { | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
baremaps-database/src/main/java/com/baremaps/database/metadata/TableMetaData.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,10 @@ | ||
package com.baremaps.database.metadata; | ||
|
||
import java.util.List; | ||
|
||
public record TableMetaData(Table table, List<PrimaryKey> primaryKeys, List<Column> columns) { | ||
|
||
public List<Column> getGeometryColumns() { | ||
return columns.stream().filter(column -> "geometry".equals(column.typeName())).toList(); | ||
} | ||
} |
Oops, something went wrong.