Search in sources :

Example 1 with JdbcRecordStore

use of com.revolsys.jdbc.io.JdbcRecordStore 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 JdbcRecordStore

use of com.revolsys.jdbc.io.JdbcRecordStore in project com.revolsys.open by revolsys.

the class PostgreSQLJdbcQueryResultPager method getNumResults.

@Override
public int getNumResults() {
    if (this.numResults == null) {
        final JdbcRecordStore recordStore = getRecordStore();
        final Query query = getQuery();
        this.numResults = recordStore.getRecordCount(query);
        updateNumPages();
    }
    return this.numResults;
}
Also used : Query(com.revolsys.record.query.Query) JdbcRecordStore(com.revolsys.jdbc.io.JdbcRecordStore)

Example 3 with JdbcRecordStore

use of com.revolsys.jdbc.io.JdbcRecordStore 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 JdbcRecordStore

use of com.revolsys.jdbc.io.JdbcRecordStore in project com.revolsys.open by revolsys.

the class JdbcUtils method lockTable.

public static void lockTable(final RecordStore recordStore, final String typePath) {
    if (recordStore instanceof JdbcRecordStore) {
        final JdbcRecordStore jdbcRecordStore = (JdbcRecordStore) recordStore;
        try (final JdbcConnection connection = jdbcRecordStore.getJdbcConnection()) {
            final String tableName = getQualifiedTableName(typePath);
            final String sql = "LOCK TABLE " + tableName + " IN SHARE MODE";
            executeUpdate(connection, sql);
        } catch (final SQLException e) {
            throw new RuntimeException("Unable to lock table " + typePath, e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) JdbcRecordStore(com.revolsys.jdbc.io.JdbcRecordStore)

Aggregations

JdbcRecordStore (com.revolsys.jdbc.io.JdbcRecordStore)4 SQLException (java.sql.SQLException)3 JdbcConnection (com.revolsys.jdbc.JdbcConnection)2 Record (com.revolsys.record.Record)2 Query (com.revolsys.record.query.Query)2 FieldDefinition (com.revolsys.record.schema.FieldDefinition)2 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 RecordFactory (com.revolsys.record.RecordFactory)1