Search in sources :

Example 1 with RecordFactory

use of com.revolsys.record.RecordFactory 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)

Aggregations

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