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);
}
}
}
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;
}
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;
}
}
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);
}
}
}
Aggregations