private static void loadFieldsToFeatureType( Connection conn, PostgresqlStoreParameters params, DBFeatureType featureType) throws ReadException { String sql = ""; String columns = params.getFieldsString(); boolean fillTableData; if (params.getSqlSoure() != null) { sql = params.getSqlSoure(); fillTableData = false; } else { sql = "Select " + columns + " from " + params.tableID(); fillTableData = true; } try { Statement stAux = conn.createStatement(); stAux.setFetchSize(1); ResultSet rs = stAux.executeQuery(sql); ResultSetMetaData rsMetadata = rs.getMetaData(); int i; featureType.setTableID(params.tableID()); DBAttributeDescriptor attr; for (i = 1; i <= rsMetadata.getColumnCount(); i++) { attr = getAttributeFromJDBC(featureType, conn, rsMetadata, i); featureType.add(attr); // attr.setOrdinal(i-1); attr.loading(); attr.setCatalogName(params.getDb()); if (fillTableData) { attr.setSchemaName(params.getSchema()); attr.setTableName(params.getTableName()); } attr.stopLoading(); } rs.close(); stAux.close(); } catch (java.sql.SQLException e) { throw new SQLException(sql, "getFeatureType", e); } catch (IsNotAttributeSettingException e) { e.printStackTrace(); } }
private static DBAttributeDescriptor getAttributeFromJDBC( DefaultFeatureType fType, Connection conn, ResultSetMetaData rsMetadata, int colIndex) throws SQLException { DBAttributeDescriptor column = (DBAttributeDescriptor) fType.createAttributeDescriptor(); try { column.loading(); column.setName(rsMetadata.getColumnName(colIndex)); column.setCaseSensitive(rsMetadata.isCaseSensitive(colIndex)); column.setSqlType(rsMetadata.getColumnType(colIndex)); column.setAllowNull(rsMetadata.isNullable(colIndex) == ResultSetMetaData.columnNullable); column.setAutoIncrement(rsMetadata.isAutoIncrement(colIndex)); column.setReadOnly(rsMetadata.isReadOnly(colIndex)); column.setWritable(rsMetadata.isWritable(colIndex)); column.setClassName(rsMetadata.getColumnClassName(colIndex)); column.setCatalogName(rsMetadata.getCatalogName(colIndex)); column.setDefinitelyWritable(rsMetadata.isDefinitelyWritable(colIndex)); column.setLabel(rsMetadata.getColumnLabel(colIndex)); column.setSchemaName(rsMetadata.getSchemaName(colIndex)); column.setTableName(rsMetadata.getTableName(colIndex)); column.setCatalogName(rsMetadata.getCatalogName(colIndex)); column.setSqlTypeName(rsMetadata.getColumnTypeName(colIndex)); column.setSearchable(rsMetadata.isSearchable(colIndex)); column.setSigned(rsMetadata.isSigned(colIndex)); column.setCurrency(rsMetadata.isCurrency(colIndex)); column.setPrecision(rsMetadata.getPrecision(colIndex)); column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); switch (rsMetadata.getColumnType(colIndex)) { case java.sql.Types.INTEGER: column.setType(FeatureAttributeDescriptor.INT); break; case java.sql.Types.BIGINT: column.setType(FeatureAttributeDescriptor.LONG); break; case java.sql.Types.REAL: column.setType(FeatureAttributeDescriptor.DOUBLE); break; case java.sql.Types.DOUBLE: column.setType(FeatureAttributeDescriptor.DOUBLE); break; case java.sql.Types.CHAR: column.setType(FeatureAttributeDescriptor.STRING); break; case java.sql.Types.VARCHAR: column.setType(FeatureAttributeDescriptor.STRING); break; case java.sql.Types.FLOAT: column.setType(FeatureAttributeDescriptor.FLOAT); break; case java.sql.Types.DECIMAL: column.setType(FeatureAttributeDescriptor.FLOAT); break; case java.sql.Types.DATE: column.setType(FeatureAttributeDescriptor.DATE); break; case java.sql.Types.TIME: column.setType(FeatureAttributeDescriptor.TIME); break; case java.sql.Types.TIMESTAMP: column.setType(FeatureAttributeDescriptor.TIMESTAMP); break; case java.sql.Types.BOOLEAN: column.setType(FeatureAttributeDescriptor.BOOLEAN); break; case java.sql.Types.OTHER: if (column.getSqlTypeName().equalsIgnoreCase("geometry")) { column.setType(FeatureAttributeDescriptor.GEOMETRY); break; } // No hacemos break para que se quede en default default: column.setType(FeatureAttributeDescriptor.OBJECT); break; } column.stopLoading(); } catch (java.sql.SQLException e) { throw new SQLException("", "load attribute definition", e); } catch (IsNotAttributeSettingException e) { e.printStackTrace(); } return column; }