Search in sources :

Example 1 with Selection

use of com.datastax.driver.core.querybuilder.Select.Selection in project incubator-hugegraph by apache.

the class CassandraTable method query2Select.

protected List<Select> query2Select(String table, Query query) {
    // Build query
    Selection selection = QueryBuilder.select();
    // Set aggregate
    Aggregate aggregate = query.aggregate();
    if (aggregate != null) {
        if (aggregate.countAll()) {
            selection.countAll();
        } else {
            selection.fcall(aggregate.func().string(), aggregate.column());
        }
    }
    // Set table
    Select select = selection.from(table);
    // NOTE: Cassandra does not support query.offset()
    if (query.offset() != 0) {
        LOG.debug("Query offset is not supported on Cassandra store " + "currently, it will be replaced by [0, offset + limit)");
    }
    // Set order-by
    for (Map.Entry<HugeKeys, Order> order : query.orders().entrySet()) {
        String name = formatKey(order.getKey());
        if (order.getValue() == Order.ASC) {
            select.orderBy(QueryBuilder.asc(name));
        } else {
            assert order.getValue() == Order.DESC;
            select.orderBy(QueryBuilder.desc(name));
        }
    }
    // Is query by id?
    List<Select> ids = this.queryId2Select(query, select);
    if (query.conditionsSize() == 0) {
        // Query only by id
        this.setPageState(query, ids);
        LOG.debug("Query only by id(s): {}", ids);
        return ids;
    } else {
        List<Select> conds = new ArrayList<>(ids.size());
        for (Select id : ids) {
            // Query by condition
            conds.addAll(this.queryCondition2Select(query, id));
        }
        this.setPageState(query, conds);
        LOG.debug("Query by conditions: {}", conds);
        return conds;
    }
}
Also used : Order(com.baidu.hugegraph.backend.query.Query.Order) Selection(com.datastax.driver.core.querybuilder.Select.Selection) Select(com.datastax.driver.core.querybuilder.Select) ArrayList(java.util.ArrayList) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Aggregate (com.baidu.hugegraph.backend.query.Aggregate)1 Order (com.baidu.hugegraph.backend.query.Query.Order)1 HugeKeys (com.baidu.hugegraph.type.define.HugeKeys)1 Select (com.datastax.driver.core.querybuilder.Select)1 Selection (com.datastax.driver.core.querybuilder.Select.Selection)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1