Search in sources :

Example 6 with Query

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);
        }
    }
}
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 7 with Query

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);
        }
    }
}
Also used : RecordWriter(com.revolsys.record.io.RecordWriter) Query(com.revolsys.record.query.Query) RecordReader(com.revolsys.record.io.RecordReader) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 8 with Query

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;
}
Also used : Query(com.revolsys.record.query.Query) JdbcRecordStore(com.revolsys.jdbc.io.JdbcRecordStore)

Example 9 with Query

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();
    }
}
Also used : Condition(com.revolsys.record.query.Condition) BinaryCondition(com.revolsys.record.query.BinaryCondition) Query(com.revolsys.record.query.Query) BinaryCondition(com.revolsys.record.query.BinaryCondition) ArrayList(java.util.ArrayList) Record(com.revolsys.record.Record) TreeMap(java.util.TreeMap)

Example 10 with Query

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);
    }
}
Also used : Condition(com.revolsys.record.query.Condition) BinaryCondition(com.revolsys.record.query.BinaryCondition) Query(com.revolsys.record.query.Query) BinaryCondition(com.revolsys.record.query.BinaryCondition) Record(com.revolsys.record.Record) TreeMap(java.util.TreeMap)

Aggregations

Query (com.revolsys.record.query.Query)32 RecordDefinition (com.revolsys.record.schema.RecordDefinition)18 Record (com.revolsys.record.Record)10 FieldDefinition (com.revolsys.record.schema.FieldDefinition)8 Condition (com.revolsys.record.query.Condition)6 RecordReader (com.revolsys.record.io.RecordReader)5 Identifier (com.revolsys.identifier.Identifier)4 ArrayList (java.util.ArrayList)4 RecordWriter (com.revolsys.record.io.RecordWriter)3 RecordStore (com.revolsys.record.schema.RecordStore)3 JdbcRecordStore (com.revolsys.jdbc.io.JdbcRecordStore)2 ListRecordReader (com.revolsys.record.io.ListRecordReader)2 BinaryCondition (com.revolsys.record.query.BinaryCondition)2 SqlCondition (com.revolsys.record.query.SqlCondition)2 LayerRecord (com.revolsys.swing.map.layer.record.LayerRecord)2 CategoryLabelCountMap (com.revolsys.util.count.CategoryLabelCountMap)2 List (java.util.List)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 LruMap (com.revolsys.collection.map.LruMap)1