Search in sources :

Example 1 with Query

use of com.revolsys.record.query.Query in project com.revolsys.open by revolsys.

the class CodeTableProperty method loadId.

@Override
protected synchronized Identifier loadId(final List<Object> values, final boolean createId) {
    if (this.loadAll && !this.loadMissingCodes && !isEmpty()) {
        return null;
    }
    Identifier id = null;
    if (createId && this.loadAll && !isLoaded()) {
        loadAll();
        id = getIdentifier(values, false);
    } else {
        final Query query = new Query(this.typePath);
        final And and = new And();
        if (!values.isEmpty()) {
            int i = 0;
            for (final String fieldName : this.valueFieldNames) {
                final Object value = values.get(i);
                if (value == null) {
                    and.and(Q.isNull(fieldName));
                } else {
                    final FieldDefinition fieldDefinition = this.recordDefinition.getField(fieldName);
                    and.and(Q.equal(fieldDefinition, value));
                }
                i++;
            }
        }
        query.setWhereCondition(and);
        final Reader<Record> reader = this.recordStore.getRecords(query);
        try {
            final List<Record> codes = reader.toList();
            if (codes.size() > 0) {
                final CategoryLabelCountMap statistics = this.recordStore.getStatistics();
                if (statistics != null) {
                    statistics.getLabelCountMap("query").addCount(this.typePath, -codes.size());
                }
                addValues(codes);
            }
            id = getIdByValue(values);
            Property.firePropertyChange(this, "valuesChanged", false, true);
        } finally {
            reader.close();
        }
    }
    if (createId && id == null) {
        return newIdentifier(values);
    } else {
        return id;
    }
}
Also used : Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) ListIdentifier(com.revolsys.identifier.ListIdentifier) Query(com.revolsys.record.query.Query) CategoryLabelCountMap(com.revolsys.util.count.CategoryLabelCountMap) And(com.revolsys.record.query.And) FieldDefinition(com.revolsys.record.schema.FieldDefinition) Record(com.revolsys.record.Record)

Example 2 with Query

use of com.revolsys.record.query.Query in project com.revolsys.open by revolsys.

the class RecordStoreQueryTask method run.

@Override
public void run() {
    this.objects = new ArrayList<>();
    final RecordDefinition recordDefinition = this.recordStore.getRecordDefinition(this.path);
    final Query query = Query.intersects(recordDefinition, this.boundingBox);
    try (final Reader<Record> reader = this.recordStore.getRecords(query)) {
        for (final Record object : reader) {
            try {
                this.objects.add(object);
            } catch (final NullPointerException e) {
                return;
            }
        }
    }
}
Also used : Query(com.revolsys.record.query.Query) Record(com.revolsys.record.Record) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 3 with Query

use of com.revolsys.record.query.Query in project com.revolsys.open by revolsys.

the class RecordStore method newGetRecordQuery.

default Query newGetRecordQuery(final PathName typePath, final Identifier id) {
    final RecordDefinition recordDefinition = getRecordDefinition(typePath);
    if (recordDefinition == null || id == null) {
        return null;
    } else {
        final List<Object> values = id.getValues();
        final List<String> idFieldNames = recordDefinition.getIdFieldNames();
        if (idFieldNames.isEmpty()) {
            throw new IllegalArgumentException(typePath + " does not have a primary key");
        } else if (values.size() != idFieldNames.size()) {
            throw new IllegalArgumentException(id + " not a valid id for " + typePath + " requires " + idFieldNames);
        } else {
            final Query query = new Query(recordDefinition);
            for (int i = 0; i < idFieldNames.size(); i++) {
                final String name = idFieldNames.get(i);
                final Object value = values.get(i);
                final FieldDefinition field = recordDefinition.getField(name);
                query.and(Q.equal(field, value));
            }
            return query;
        }
    }
}
Also used : Query(com.revolsys.record.query.Query)

Example 4 with Query

use of com.revolsys.record.query.Query in project com.revolsys.open by revolsys.

the class RecordStore method getRecordLocked.

default Record getRecordLocked(final PathName typePath, final LockMode lockMode, final Identifier id) {
    final Query query = newGetRecordQuery(typePath, id);
    if (query == null) {
        return null;
    } else {
        query.setLockMode(lockMode);
        final RecordReader records = getRecords(query);
        return records.getFirst();
    }
}
Also used : Query(com.revolsys.record.query.Query) ListRecordReader(com.revolsys.record.io.ListRecordReader) RecordReader(com.revolsys.record.io.RecordReader)

Example 5 with Query

use of com.revolsys.record.query.Query in project com.revolsys.open by revolsys.

the class RecordStore method getRecords.

default RecordReader getRecords(final PathName path) {
    final RecordStoreSchemaElement element = getRootSchema().getElement(path);
    if (element instanceof RecordDefinition) {
        final RecordDefinition recordDefinition = (RecordDefinition) element;
        final Query query = new Query(recordDefinition);
        return getRecords(query);
    } else if (element instanceof RecordStoreSchema) {
        final RecordStoreSchema schema = (RecordStoreSchema) element;
        final List<Query> queries = new ArrayList<>();
        for (final RecordDefinition recordDefinition : schema.getRecordDefinitions()) {
            final Query query = new Query(recordDefinition);
            queries.add(query);
        }
        return getRecords(queries);
    } else {
        return new ListRecordReader(null, Collections.emptyList());
    }
}
Also used : ListRecordReader(com.revolsys.record.io.ListRecordReader) Query(com.revolsys.record.query.Query) ArrayList(java.util.ArrayList) List(java.util.List)

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