use of com.revolsys.record.query.Query 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.record.query.Query in project com.revolsys.open by revolsys.
the class Records method copyRecords.
static void copyRecords(final RecordStore sourceRecordStore, final String sourceTableName, final RecordStore targetRecordStore, final String targetTableName) {
final Query query = new Query(sourceTableName);
try (RecordReader reader = sourceRecordStore.getRecords(query);
RecordWriter writer = targetRecordStore.newRecordWriter()) {
final RecordDefinition recordDefinition = targetRecordStore.getRecordDefinition(targetTableName);
for (final Record record : reader) {
final Record newRecord = recordDefinition.newRecord();
newRecord.setValuesAll(record);
writer.write(newRecord);
}
}
}
use of com.revolsys.record.query.Query 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.record.query.Query in project com.revolsys.open by revolsys.
the class RecordStoreQueryListModel method getRecords.
protected List<Record> getRecords(final String searchParam) {
if (Property.hasValue(searchParam) && searchParam.length() >= 2) {
final Map<String, Record> allObjects = new TreeMap<>();
for (Query query : this.queries) {
if (allObjects.size() < this.maxResults) {
query = query.clone();
query.setOrderBy(this.displayFieldName);
final Condition whereCondition = query.getWhereCondition();
if (whereCondition instanceof BinaryCondition) {
final BinaryCondition binaryCondition = (BinaryCondition) whereCondition;
if (binaryCondition.getOperator().equalsIgnoreCase("like")) {
final String likeString = "%" + searchParam.toUpperCase().replaceAll("[^A-Z0-9 ]", "%") + "%";
Q.setValue(0, binaryCondition, likeString);
} else {
Q.setValue(0, binaryCondition, searchParam);
}
}
query.setLimit(this.maxResults);
final Reader<Record> reader = this.recordStore.getRecords(query);
try {
final List<Record> objects = reader.toList();
for (final Record object : objects) {
if (allObjects.size() < this.maxResults) {
final String key = object.getString(this.displayFieldName);
if (!allObjects.containsKey(key)) {
if (searchParam.equals(key)) {
this.selectedItem = object;
}
allObjects.put(key, object);
}
}
}
} finally {
reader.close();
}
}
}
return new ArrayList<>(allObjects.values());
} else {
return Collections.emptyList();
}
}
use of com.revolsys.record.query.Query in project com.revolsys.open by revolsys.
the class AbstractRecordQueryField method queryDo.
protected void queryDo(final int searchIndex, final String queryText) {
if (searchIndex == this.searchIndex.get()) {
Record selectedRecord = null;
final Map<String, Record> allRecords = new TreeMap<>();
for (Query query : this.queries) {
if (allRecords.size() < this.maxResults) {
query = //
query.clone().addOrderBy(this.displayField);
final Condition whereCondition = query.getWhereCondition();
if (whereCondition instanceof BinaryCondition) {
final BinaryCondition binaryCondition = (BinaryCondition) whereCondition;
if (binaryCondition.getOperator().equalsIgnoreCase("like")) {
final String likeString = "%" + queryText.toUpperCase().replaceAll("[^A-Z0-9 ]", "%") + "%";
Q.setValue(0, binaryCondition, likeString);
} else {
Q.setValue(0, binaryCondition, queryText);
}
}
query.setLimit(this.maxResults);
final List<Record> records = getRecords(query);
for (final Record record : records) {
if (allRecords.size() < this.maxResults) {
final String key = record.getString(this.displayField);
if (!allRecords.containsKey(key)) {
if (queryText.equals(key)) {
selectedRecord = record;
}
allRecords.put(key, record);
}
}
}
}
}
setSearchResults(searchIndex, allRecords.values(), selectedRecord);
}
}
Aggregations