|
| 1 | +package org.hypertrace.core.documentstore.postgres.registry; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.PreparedStatement; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.Set; |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | +import org.hypertrace.core.documentstore.postgres.PostgresTableIdentifier; |
| 14 | + |
| 15 | +/** |
| 16 | + * Implementation of PostgresColumnRegistry that queries the database schema to build column type |
| 17 | + * mappings dynamically. |
| 18 | + * |
| 19 | + * <p>This implementation queries the information_schema.columns table to discover the actual column |
| 20 | + * types in the PostgreSQL table and maps them to the appropriate PostgresColumnType enum values. |
| 21 | + */ |
| 22 | +@Slf4j |
| 23 | +public class PostgresColumnRegistryImpl implements PostgresColumnRegistry { |
| 24 | + |
| 25 | + private final String tableName; |
| 26 | + private final Map<String, PostgresColumnInfo> columnMappings; |
| 27 | + |
| 28 | + /** |
| 29 | + * Creates a new PostgresColumnRegistryImpl by querying the database schema. |
| 30 | + * |
| 31 | + * @param connection the database connection to use for schema queries |
| 32 | + * @param tableIdentifier the table identifier to query schema for |
| 33 | + * @throws SQLException if there's an error querying the database schema |
| 34 | + */ |
| 35 | + public PostgresColumnRegistryImpl(Connection connection, PostgresTableIdentifier tableIdentifier) |
| 36 | + throws SQLException { |
| 37 | + this.tableName = tableIdentifier.getTableName(); |
| 38 | + this.columnMappings = buildColumnMappings(connection, tableIdentifier); |
| 39 | + System.out.println("-- PostgresColumnRegistryImpl --"); |
| 40 | + System.out.printf("Table Name: %s\n", this.tableName); |
| 41 | + System.out.println(this.columnMappings); |
| 42 | + System.out.println("-- end --"); |
| 43 | + System.out.println(); |
| 44 | + |
| 45 | + log.debug( |
| 46 | + "Created PostgresColumnRegistry for table '{}' with {} first-class columns: {}", |
| 47 | + tableName, |
| 48 | + columnMappings.size(), |
| 49 | + columnMappings.keySet()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a new PostgresColumnRegistryImpl by querying the database schema. |
| 54 | + * |
| 55 | + * @param connection the database connection to use for schema queries |
| 56 | + * @param collectionName the collection name (table name) to query schema for |
| 57 | + * @throws SQLException if there's an error querying the database schema |
| 58 | + */ |
| 59 | + public PostgresColumnRegistryImpl(Connection connection, String collectionName) |
| 60 | + throws SQLException { |
| 61 | + this(connection, PostgresTableIdentifier.parse(collectionName)); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean isFirstClassColumn(String fieldName) { |
| 66 | + return columnMappings.containsKey(fieldName); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Optional<PostgresColumnType> getColumnType(String fieldName) { |
| 71 | + PostgresColumnInfo columnInfo = columnMappings.get(fieldName); |
| 72 | + return columnInfo != null ? Optional.of(columnInfo.getColumnType()) : Optional.empty(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Optional<String> getColumnName(String fieldName) { |
| 77 | + PostgresColumnInfo columnInfo = columnMappings.get(fieldName); |
| 78 | + return columnInfo != null ? Optional.of(columnInfo.getColumnName()) : Optional.empty(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Set<String> getAllFirstClassColumns() { |
| 83 | + return Collections.unmodifiableSet(columnMappings.keySet()); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String getTableName() { |
| 88 | + return tableName; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Builds the column mappings by querying the database schema. |
| 93 | + * |
| 94 | + * @param connection the database connection |
| 95 | + * @param tableIdentifier the table identifier |
| 96 | + * @return a map of field names to PostgresColumnInfo objects |
| 97 | + * @throws SQLException if there's an error querying the database |
| 98 | + */ |
| 99 | + private Map<String, PostgresColumnInfo> buildColumnMappings( |
| 100 | + Connection connection, PostgresTableIdentifier tableIdentifier) throws SQLException { |
| 101 | + |
| 102 | + Map<String, PostgresColumnInfo> mappings = new HashMap<>(); |
| 103 | + |
| 104 | + String query = |
| 105 | + "SELECT column_name, data_type, udt_name " |
| 106 | + + "FROM information_schema.columns " |
| 107 | + + "WHERE table_name = ? AND table_schema = ? " |
| 108 | + + "ORDER BY ordinal_position"; |
| 109 | + |
| 110 | + String schemaName = tableIdentifier.getSchema().orElse("public"); |
| 111 | + String tableName = tableIdentifier.getTableName(); |
| 112 | + |
| 113 | + try (PreparedStatement stmt = connection.prepareStatement(query)) { |
| 114 | + stmt.setString(1, tableName); |
| 115 | + stmt.setString(2, schemaName); |
| 116 | + |
| 117 | + log.debug("Querying schema for table: {}.{}", schemaName, tableName); |
| 118 | + |
| 119 | + try (ResultSet rs = stmt.executeQuery()) { |
| 120 | + if (rs != null) { |
| 121 | + while (rs.next()) { |
| 122 | + String columnName = rs.getString("column_name"); |
| 123 | + String dataType = rs.getString("data_type"); |
| 124 | + String udtName = rs.getString("udt_name"); |
| 125 | + |
| 126 | + PostgresColumnType columnType = PostgresColumnType.fromPostgresType(dataType, udtName); |
| 127 | + |
| 128 | + // Only include first-class columns (non-JSONB) in the registry |
| 129 | + if (columnType.isFirstClassField()) { |
| 130 | + PostgresColumnInfo columnInfo = new PostgresColumnInfo(columnName, columnType); |
| 131 | + mappings.put(columnName, columnInfo); |
| 132 | + |
| 133 | + log.debug( |
| 134 | + "Registered first-class column: {} -> {} ({})", columnName, columnType, dataType); |
| 135 | + } else { |
| 136 | + log.debug("Skipping JSONB column: {} ({})", columnName, dataType); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } catch (SQLException e) { |
| 142 | + log.error( |
| 143 | + "Failed to query schema for table {}.{}: {}", schemaName, tableName, e.getMessage()); |
| 144 | + throw e; |
| 145 | + } |
| 146 | + |
| 147 | + return mappings; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public String toString() { |
| 152 | + return String.format( |
| 153 | + "PostgresColumnRegistryImpl{tableName='%s', firstClassColumns=%d}", |
| 154 | + tableName, columnMappings.size()); |
| 155 | + } |
| 156 | +} |
0 commit comments