Search in sources :

Example 1 with JdbcConnection

use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.

the class OracleJdbcQueryResultPager method updateResults.

/**
 * Update the cached results for the current page.
 */
@Override
protected void updateResults() {
    synchronized (this) {
        final JdbcRecordStore recordStore = getRecordStore();
        final Query query = getQuery();
        setNumResults(recordStore.getRecordCount(query));
        updateNumPages();
        final ArrayList<Record> results = new ArrayList<>();
        final int pageSize = getPageSize();
        final int pageNumber = getPageNumber();
        if (pageNumber != -1) {
            String sql = getSql();
            final int startRowNum = (pageNumber - 1) * pageSize + 1;
            final int endRowNum = startRowNum + pageSize - 1;
            sql = "SELECT * FROM ( SELECT  T2.*, ROWNUM TROWNUM FROM ( " + sql + ") T2 ) WHERE TROWNUM BETWEEN " + startRowNum + " AND " + endRowNum;
            try (final JdbcConnection connection = getRecordStore().getJdbcConnection()) {
                final RecordFactory<Record> recordFactory = getRecordFactory();
                final RecordDefinition recordDefinition = getRecordDefinition();
                final List<FieldDefinition> attributes = new ArrayList<>();
                final List<String> fieldNames = query.getFieldNames();
                if (fieldNames.isEmpty()) {
                    attributes.addAll(recordDefinition.getFields());
                } else {
                    for (final String fieldName : fieldNames) {
                        if (fieldName.equals("*")) {
                            attributes.addAll(recordDefinition.getFields());
                        } else {
                            final FieldDefinition attribute = recordDefinition.getField(fieldName);
                            if (attribute != null) {
                                attributes.add(attribute);
                            }
                        }
                    }
                }
                try (final PreparedStatement statement = connection.prepareStatement(sql);
                    final ResultSet resultSet = JdbcQueryIterator.getResultSet(statement, getQuery())) {
                    if (resultSet.next()) {
                        int i = 0;
                        do {
                            final Record record = JdbcQueryIterator.getNextRecord(recordStore, recordDefinition, attributes, recordFactory, resultSet, this.internStrings);
                            results.add(record);
                            i++;
                        } while (resultSet.next() && i < pageSize);
                    }
                } catch (final SQLException e) {
                    throw connection.getException("updateResults", sql, e);
                }
            }
            setResults(results);
        }
    }
}
Also used : Query(com.revolsys.record.query.Query) SQLException(java.sql.SQLException) JdbcRecordStore(com.revolsys.jdbc.io.JdbcRecordStore) FieldDefinition(com.revolsys.record.schema.FieldDefinition) ArrayList(java.util.ArrayList) JdbcConnection(com.revolsys.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RecordDefinition(com.revolsys.record.schema.RecordDefinition) ResultSet(java.sql.ResultSet) Record(com.revolsys.record.Record)

Example 2 with JdbcConnection

use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.

the class OracleSdoGeometryFieldAdder method initialize.

@Override
public void initialize(final JdbcRecordStoreSchema schema) {
    try (final JdbcConnection connection = this.recordStore.getJdbcConnection()) {
        final String schemaName = schema.getDbName();
        final String sridSql = "select M.TABLE_NAME, M.COLUMN_NAME, M.SRID, M.DIMINFO, C.GEOMETRY_TYPE " + "from ALL_SDO_GEOM_METADATA M " + "LEFT OUTER JOIN ALL_GEOMETRY_COLUMNS C ON (M.OWNER = C.F_TABLE_SCHEMA AND M.TABLE_NAME = C.F_TABLE_NAME AND M.COLUMN_NAME = C.F_GEOMETRY_COLUMN) " + "where OWNER = ?";
        try (final PreparedStatement statement = connection.prepareStatement(sridSql)) {
            statement.setString(1, schemaName);
            try (final ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    final String tableName = resultSet.getString(1);
                    final String columnName = resultSet.getString(2);
                    final PathName typePath = schema.getPathName().newChild(tableName);
                    int srid = resultSet.getInt(3);
                    if (resultSet.wasNull() || srid < 0) {
                        srid = 0;
                    }
                    final Object[] dimInfo = (Object[]) resultSet.getArray("DIMINFO").getArray();
                    int axisCount = dimInfo.length;
                    setColumnProperty(schema, typePath, columnName, AXIS_COUNT, axisCount);
                    if (axisCount < 2) {
                        axisCount = 2;
                    } else if (axisCount > 4) {
                        axisCount = 4;
                    }
                    final double[] scales = new double[axisCount];
                    for (int i = 0; i < scales.length; i++) {
                        scales[i] = getScale(dimInfo, i);
                    }
                    final GeometryFactory geometryFactory = this.recordStore.getGeometryFactory(srid, axisCount, scales);
                    setColumnProperty(schema, typePath, columnName, GEOMETRY_FACTORY, geometryFactory);
                    setColumnProperty(schema, typePath, columnName, ORACLE_SRID, srid);
                    final int geometryType = resultSet.getInt(5);
                    DataType geometryDataType;
                    if (resultSet.wasNull()) {
                        geometryDataType = DataTypes.GEOMETRY;
                    } else {
                        geometryDataType = ID_TO_DATA_TYPE.get(geometryType);
                        if (geometryDataType == null) {
                            geometryDataType = DataTypes.GEOMETRY;
                        }
                    }
                    setColumnProperty(schema, typePath, columnName, GEOMETRY_TYPE, geometryDataType);
                }
            }
        } catch (final SQLException e) {
            Logs.error(this, "Unable to initialize", e);
        }
    }
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataType(com.revolsys.datatype.DataType) JdbcConnection(com.revolsys.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) PathName(com.revolsys.io.PathName)

Example 3 with JdbcConnection

use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.

the class PostgreSQLJdbcQueryResultPager method getList.

@Override
public List<Record> getList() {
    synchronized (this) {
        if (this.results == null) {
            final ArrayList<Record> results = new ArrayList<>();
            final int pageSize = getPageSize();
            final int pageNumber = getPageNumber();
            if (pageNumber != -1) {
                String sql = getSql();
                final int startRowNum = (pageNumber - 1) * pageSize;
                sql = getSql() + " OFFSET " + startRowNum + " LIMIT " + pageSize;
                final RecordDefinition recordDefinition = getRecordDefinition();
                if (recordDefinition != null) {
                    final RecordFactory recordFactory = getRecordFactory();
                    final JdbcRecordStore recordStore = getRecordStore();
                    try (JdbcConnection connection = recordStore.getJdbcConnection()) {
                        final List<FieldDefinition> attributes = recordDefinition.getFields();
                        try (final PreparedStatement statement = connection.prepareStatement(sql);
                            final ResultSet resultSet = JdbcQueryIterator.getResultSet(statement, getQuery())) {
                            if (resultSet.next()) {
                                int i = 0;
                                do {
                                    final Record object = JdbcQueryIterator.getNextRecord(recordStore, recordDefinition, attributes, recordFactory, resultSet, this.internStrings);
                                    results.add(object);
                                    i++;
                                } while (resultSet.next() && i < pageSize);
                            }
                        } catch (final SQLException e) {
                            throw connection.getException("updateResults", sql, e);
                        }
                    }
                }
            }
            this.results = results;
        }
        return this.results;
    }
}
Also used : SQLException(java.sql.SQLException) JdbcRecordStore(com.revolsys.jdbc.io.JdbcRecordStore) FieldDefinition(com.revolsys.record.schema.FieldDefinition) ArrayList(java.util.ArrayList) JdbcConnection(com.revolsys.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RecordDefinition(com.revolsys.record.schema.RecordDefinition) RecordFactory(com.revolsys.record.RecordFactory) ResultSet(java.sql.ResultSet) Record(com.revolsys.record.Record)

Example 4 with JdbcConnection

use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.

the class AbstractJdbcRecordStore method deleteRecords.

@Override
public int deleteRecords(final Query query) {
    final String typeName = query.getTypeName();
    RecordDefinition recordDefinition = query.getRecordDefinition();
    if (recordDefinition == null) {
        if (typeName != null) {
            recordDefinition = getRecordDefinition(typeName);
            query.setRecordDefinition(recordDefinition);
        }
    }
    final String sql = JdbcUtils.getDeleteSql(query);
    try (Transaction transaction = newTransaction(com.revolsys.transaction.Propagation.REQUIRED)) {
        // rolled back.
        try (JdbcConnection connection = getJdbcConnection(isAutoCommit());
            final PreparedStatement statement = connection.prepareStatement(sql)) {
            JdbcUtils.setPreparedStatementParameters(statement, query);
            return statement.executeUpdate();
        } catch (final SQLException e) {
            transaction.setRollbackOnly();
            throw new RuntimeException("Unable to delete : " + sql, e);
        } catch (final RuntimeException e) {
            transaction.setRollbackOnly();
            throw e;
        } catch (final Error e) {
            transaction.setRollbackOnly();
            throw e;
        }
    }
}
Also used : Transaction(com.revolsys.transaction.Transaction) SQLException(java.sql.SQLException) JdbcConnection(com.revolsys.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 5 with JdbcConnection

use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.

the class AbstractJdbcRecordStore method getRecordCount.

@Override
public int getRecordCount(Query query) {
    if (query == null) {
        return 0;
    } else {
        query = query.clone();
        query.setSql(null);
        query.setFieldNames("count(*)");
        query.clearOrderBy();
        final String sql = JdbcUtils.getSelectSql(query);
        try (JdbcConnection connection = getJdbcConnection()) {
            try (final PreparedStatement statement = connection.prepareStatement(sql)) {
                JdbcUtils.setPreparedStatementParameters(statement, query);
                try (final ResultSet resultSet = statement.executeQuery()) {
                    if (resultSet.next()) {
                        final int rowCount = resultSet.getInt(1);
                        return rowCount;
                    } else {
                        return 0;
                    }
                }
            } catch (final SQLException e) {
                throw connection.getException("getRecordCount", sql, e);
            } catch (final IllegalArgumentException e) {
                Logs.error(this, "Cannot get row count: " + query, e);
                return 0;
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JdbcConnection(com.revolsys.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

JdbcConnection (com.revolsys.jdbc.JdbcConnection)7 SQLException (java.sql.SQLException)7 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)4 RecordDefinition (com.revolsys.record.schema.RecordDefinition)3 JdbcRecordStore (com.revolsys.jdbc.io.JdbcRecordStore)2 Record (com.revolsys.record.Record)2 FieldDefinition (com.revolsys.record.schema.FieldDefinition)2 ArrayList (java.util.ArrayList)2 DataType (com.revolsys.datatype.DataType)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 PathName (com.revolsys.io.PathName)1 RecordFactory (com.revolsys.record.RecordFactory)1 Query (com.revolsys.record.query.Query)1 Transaction (com.revolsys.transaction.Transaction)1 Connection (java.sql.Connection)1 PostConstruct (javax.annotation.PostConstruct)1 DataSource (javax.sql.DataSource)1 PgConnection (org.postgresql.jdbc.PgConnection)1 CoreConnection (org.sqlite.core.CoreConnection)1