2020import java .sql .*;
2121import java .util .*;
2222import javax .sql .DataSource ;
23-
2423import org .apache .baremaps .calcite .data .DataColumn ;
2524import org .apache .baremaps .calcite .data .DataColumnFixed ;
2625import org .apache .baremaps .calcite .data .DataSchema ;
27- import org .apache .baremaps .postgres .metadata .DatabaseMetadata ;
2826import org .apache .baremaps .postgres .metadata .ColumnResult ;
27+ import org .apache .baremaps .postgres .metadata .DatabaseMetadata ;
2928import org .apache .calcite .DataContext ;
3029import org .apache .calcite .linq4j .AbstractEnumerable ;
3130import org .apache .calcite .linq4j .Enumerable ;
4039import org .locationtech .jts .io .WKBReader ;
4140
4241/**
43- * A Calcite table implementation for PostGIS. This table allows querying spatial data
44- * directly from a PostGIS-enabled PostgreSQL database.
42+ * A Calcite table implementation for PostGIS. This table allows querying spatial data directly from
43+ * a PostGIS-enabled PostgreSQL database.
4544 */
4645public class PostgisTable extends AbstractTable implements ScannableTable {
4746
@@ -69,7 +68,7 @@ public PostgisTable(DataSource dataSource, String tableName) throws SQLException
6968 * @param typeFactory the type factory
7069 * @throws SQLException if an SQL error occurs
7170 */
72- public PostgisTable (DataSource dataSource , String tableName , RelDataTypeFactory typeFactory )
71+ public PostgisTable (DataSource dataSource , String tableName , RelDataTypeFactory typeFactory )
7372 throws SQLException {
7473 this .dataSource = dataSource ;
7574 this .tableName = tableName ;
@@ -85,31 +84,32 @@ public PostgisTable(DataSource dataSource, String tableName, RelDataTypeFactory
8584 */
8685 private DataSchema discoverSchema () throws SQLException {
8786 List <DataColumn > columns = new ArrayList <>();
88-
87+
8988 // Use DatabaseMetadata to get column information
9089 DatabaseMetadata metadata = new DatabaseMetadata (dataSource );
91- var tableMetadata = metadata .getTableMetaData (null , null , tableName , new String []{"TABLE" , "VIEW" })
92- .stream ()
93- .filter (meta -> meta .table ().tableName ().equalsIgnoreCase (tableName ))
94- .findFirst ()
95- .orElseThrow (() -> new SQLException ("Table not found: " + tableName ));
96-
90+ var tableMetadata =
91+ metadata .getTableMetaData (null , null , tableName , new String [] {"TABLE" , "VIEW" })
92+ .stream ()
93+ .filter (meta -> meta .table ().tableName ().equalsIgnoreCase (tableName ))
94+ .findFirst ()
95+ .orElseThrow (() -> new SQLException ("Table not found: " + tableName ));
96+
9797 // Get geometry column information separately since it's PostGIS specific
9898 Map <String , String > geometryTypes = getGeometryTypes ();
99-
99+
100100 for (ColumnResult column : tableMetadata .columns ()) {
101101 String columnName = column .columnName ();
102102 String dataType = column .typeName ();
103103 boolean isNullable = "YES" .equalsIgnoreCase (column .isNullable ());
104-
104+
105105 // Determine column cardinality
106- DataColumn .Cardinality cardinality = isNullable ?
107- DataColumn .Cardinality .OPTIONAL : DataColumn .Cardinality .REQUIRED ;
108-
106+ DataColumn .Cardinality cardinality =
107+ isNullable ? DataColumn .Cardinality .OPTIONAL : DataColumn .Cardinality .REQUIRED ;
108+
109109 // Create a data column based on the type
110110 RelDataTypeFactory typeFactory = new org .apache .calcite .jdbc .JavaTypeFactoryImpl ();
111111 RelDataType relDataType ;
112-
112+
113113 if ("geometry" .equalsIgnoreCase (dataType ) && geometryTypes .containsKey (columnName )) {
114114 // This is a geometry column
115115 relDataType = typeFactory .createSqlType (SqlTypeName .GEOMETRY );
@@ -118,13 +118,13 @@ private DataSchema discoverSchema() throws SQLException {
118118 relDataType = PostgisTypeConversion .postgresTypeToRelDataType (
119119 typeFactory , dataType );
120120 }
121-
121+
122122 columns .add (new DataColumnFixed (columnName , cardinality , relDataType ));
123123 }
124-
124+
125125 return new DataSchema (tableName , columns );
126126 }
127-
127+
128128 /**
129129 * Gets geometry column types for the current table.
130130 *
@@ -133,14 +133,14 @@ private DataSchema discoverSchema() throws SQLException {
133133 */
134134 private Map <String , String > getGeometryTypes () throws SQLException {
135135 Map <String , String > geometryTypes = new HashMap <>();
136-
136+
137137 try (Connection connection = dataSource .getConnection ()) {
138138 // Query to get geometry column information
139139 String sql = "SELECT f_geometry_column, type FROM geometry_columns WHERE f_table_name = ?" ;
140-
140+
141141 try (PreparedStatement stmt = connection .prepareStatement (sql )) {
142142 stmt .setString (1 , tableName );
143-
143+
144144 try (ResultSet rs = stmt .executeQuery ()) {
145145 while (rs .next ()) {
146146 String column = rs .getString ("f_geometry_column" );
@@ -150,10 +150,10 @@ private Map<String, String> getGeometryTypes() throws SQLException {
150150 }
151151 }
152152 }
153-
153+
154154 return geometryTypes ;
155155 }
156-
156+
157157 @ Override
158158 public RelDataType getRowType (RelDataTypeFactory typeFactory ) {
159159 return rowType ;
@@ -216,18 +216,18 @@ public PostgisEnumerator(DataSource dataSource, DataSchema schema) {
216216
217217 private String buildSelectQuery () {
218218 List <String > columnProjections = new ArrayList <>();
219-
219+
220220 for (DataColumn column : schema .columns ()) {
221221 // Special handling for geometry columns to get WKB format
222222 if (column .sqlTypeName () == SqlTypeName .GEOMETRY ) {
223- columnProjections .add (String .format ("ST_AsBinary(\" %s\" ) AS \" %s\" " ,
223+ columnProjections .add (String .format ("ST_AsBinary(\" %s\" ) AS \" %s\" " ,
224224 column .name (), column .name ()));
225225 } else {
226226 columnProjections .add (String .format ("\" %s\" " , column .name ()));
227227 }
228228 }
229-
230- return "SELECT " + String .join (", " , columnProjections ) +
229+
230+ return "SELECT " + String .join (", " , columnProjections ) +
231231 " FROM \" " + schema .name () + "\" " ;
232232 }
233233
@@ -236,15 +236,15 @@ private Object[] convertCurrentRow() throws SQLException {
236236 for (int i = 0 ; i < schema .columns ().size (); i ++) {
237237 DataColumn column = schema .columns ().get (i );
238238 Object value ;
239-
239+
240240 // Special handling for geometry columns
241241 if (column .sqlTypeName () == SqlTypeName .GEOMETRY ) {
242242 byte [] wkb = resultSet .getBytes (i + 1 );
243243 value = deserializeWkb (wkb );
244244 } else {
245245 value = resultSet .getObject (i + 1 );
246246 }
247-
247+
248248 values [i ] = value ;
249249 }
250250 return values ;
@@ -274,7 +274,7 @@ public boolean moveNext() {
274274 current = null ;
275275 return false ;
276276 }
277-
277+
278278 current = convertCurrentRow ();
279279 hasNext = resultSet .next ();
280280 return true ;
@@ -321,4 +321,4 @@ public void close() {
321321 }
322322 }
323323 }
324- }
324+ }
0 commit comments