Search in sources :

Example 1 with DBFDatabaseMetaData

use of org.apache.sis.internal.shapefile.jdbc.metadata.DBFDatabaseMetaData in project sis by apache.

the class InputFeatureStream method internalReadFeature.

/**
 * Read next feature responding to the SQL query.
 * @return Feature, null if no more feature is available.
 * @throws SQLNotNumericException if a field expected numeric isn't.
 * @throws SQLNotDateException if a field expected of date kind, isn't.
 * @throws SQLNoSuchFieldException if a field doesn't exist.
 * @throws SQLIllegalParameterException if a parameter is illegal in the query.
 * @throws SQLInvalidStatementException if the SQL statement is invalid.
 * @throws SQLConnectionClosedException if the connection is closed.
 * @throws SQLUnsupportedParsingFeatureException if a SQL ability is not currently available through this driver.
 * @throws SQLFeatureNotSupportedException if a SQL ability is not currently available through this driver.
 * @throws InvalidShapefileFormatException if the shapefile format is invalid.
 * @throws SQLNoDirectAccessAvailableException if the underlying SQL statement requires a direct access in the shapefile, but the shapefile cannot allow it.
 */
private AbstractFeature internalReadFeature() throws SQLConnectionClosedException, SQLInvalidStatementException, SQLIllegalParameterException, SQLNoSuchFieldException, SQLUnsupportedParsingFeatureException, SQLNotNumericException, SQLNotDateException, SQLFeatureNotSupportedException, InvalidShapefileFormatException, SQLNoDirectAccessAvailableException {
    try {
        if (this.endOfFile) {
            return null;
        }
        int previousRecordNumber = this.rs.getRowNum();
        if (this.rs.next() == false) {
            this.endOfFile = true;
            return null;
        }
        int currentRecordNumber = this.rs.getRowNum();
        // On the shapefile, only jump in another place if a direct access is needed.
        boolean directAccesRequired = currentRecordNumber != (previousRecordNumber + 1);
        if (directAccesRequired) {
            try {
                if (LOGGER.isLoggable(Level.FINER)) {
                    MessageFormat format = new MessageFormat(this.rsc.getString("log.shapefile_reading_with_direct_access"));
                    LOGGER.finer(format.format(new Object[] { previousRecordNumber, currentRecordNumber }));
                }
                this.shapefileReader.setRowNum(currentRecordNumber);
            } catch (SQLInvalidRecordNumberForDirectAccessException e) {
                // This would be an internal API problem, because as soon as we handle a shapefile index, we shall go through its relative shape feature file correctly.
                throw new RuntimeException(e.getMessage(), e);
            }
        } else {
            if (LOGGER.isLoggable(Level.FINER)) {
                MessageFormat format = new MessageFormat(this.rsc.getString("log.shapefile_reading_with_sequential_access"));
                LOGGER.finer(format.format(new Object[] { previousRecordNumber, currentRecordNumber }));
            }
        }
        AbstractFeature feature = this.featuresType.newInstance();
        this.shapefileReader.completeFeature(feature);
        DBFDatabaseMetaData metadata = (DBFDatabaseMetaData) this.connection.getMetaData();
        try (DBFBuiltInMemoryResultSetForColumnsListing rsDatabase = (DBFBuiltInMemoryResultSetForColumnsListing) metadata.getColumns(null, null, null, null)) {
            while (rsDatabase.next()) {
                String fieldName = rsDatabase.getString("COLUMN_NAME");
                Object fieldValue = this.rs.getObject(fieldName);
                // FIXME To allow features to be filled again, the values are converted to String again : feature should allow any kind of data.
                String stringValue;
                if (fieldValue == null) {
                    stringValue = null;
                } else {
                    if (fieldValue instanceof Integer || fieldValue instanceof Long) {
                        // Avoid thousand separator.
                        stringValue = MessageFormat.format("{0,number,#0}", fieldValue);
                    } else {
                        if (fieldValue instanceof Double || fieldValue instanceof Float) {
                            // Avoid thousand separator.
                            DecimalFormat df = new DecimalFormat();
                            df.setGroupingUsed(false);
                            stringValue = df.format(fieldValue);
                        } else
                            stringValue = fieldValue.toString();
                    }
                }
                feature.setPropertyValue(fieldName, stringValue);
            }
            return feature;
        } catch (SQLNoResultException e) {
            // This an internal trouble, if it occurs.
            throw new RuntimeException(e.getMessage(), e);
        }
    } catch (SQLNoResultException e) {
        // We are trying to prevent this. If it occurs, we have an internal problem.
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : MessageFormat(java.text.MessageFormat) DBFDatabaseMetaData(org.apache.sis.internal.shapefile.jdbc.metadata.DBFDatabaseMetaData) DecimalFormat(java.text.DecimalFormat) AbstractFeature(org.apache.sis.feature.AbstractFeature)

Aggregations

DecimalFormat (java.text.DecimalFormat)1 MessageFormat (java.text.MessageFormat)1 AbstractFeature (org.apache.sis.feature.AbstractFeature)1 DBFDatabaseMetaData (org.apache.sis.internal.shapefile.jdbc.metadata.DBFDatabaseMetaData)1