Search in sources :

Example 1 with ReqlExpr

use of com.rethinkdb.gen.ast.ReqlExpr in project gora by apache.

the class RethinkDBStore method execute.

/**
 * {@inheritDoc}
 */
@Override
public Result<K, T> execute(Query<K, T> query) throws GoraException {
    String[] fields = getFieldsToQuery(query.getFields());
    RethinkDBQuery dataStoreQuery;
    if (query instanceof RethinkDBQuery) {
        dataStoreQuery = ((RethinkDBQuery) query);
    } else {
        dataStoreQuery = (RethinkDBQuery) ((PartitionQueryImpl<K, T>) query).getBaseQuery();
    }
    dataStoreQuery.populateRethinkDBQuery(rethinkDBMapping, rethinkDBStoreParameters, fields, getFields());
    try {
        ReqlExpr reqlExpr = dataStoreQuery.getRethinkDBDbQuery();
        com.rethinkdb.net.Result<MapObject> result = reqlExpr.run(connection, MapObject.class);
        return new RethinkDBResult<>(this, query, result);
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) PartitionQueryImpl(org.apache.gora.query.impl.PartitionQueryImpl) RethinkDBQuery(org.apache.gora.rethinkdb.query.RethinkDBQuery) RethinkDBResult(org.apache.gora.rethinkdb.query.RethinkDBResult) MapObject(com.rethinkdb.model.MapObject) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) ReqlExpr(com.rethinkdb.gen.ast.ReqlExpr)

Example 2 with ReqlExpr

use of com.rethinkdb.gen.ast.ReqlExpr in project gora by apache.

the class RethinkDBStore method deleteByQuery.

/**
 * {@inheritDoc}
 */
@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
    if (query.getFields() == null || (query.getFields().length == getFields().length)) {
        String[] fields = getFieldsToQuery(query.getFields());
        RethinkDBQuery dataStoreQuery = ((RethinkDBQuery) query);
        dataStoreQuery.populateRethinkDBQuery(rethinkDBMapping, rethinkDBStoreParameters, fields, getFields());
        ReqlExpr reqlExpr = dataStoreQuery.getRethinkDBDbQuery();
        MapObject<String, Object> document = reqlExpr.delete().run(connection, MapObject.class).first();
        int deleteCount = Integer.valueOf(document.get("deleted").toString());
        if (deleteCount > 0) {
            return deleteCount;
        } else {
            return 0;
        }
    } else {
        RethinkDBQuery<K, T> dataStoreQuery = new RethinkDBQuery<>(this);
        dataStoreQuery.setStartKey(query.getStartKey());
        dataStoreQuery.setEndKey(query.getEndKey());
        dataStoreQuery.populateRethinkDBQuery(rethinkDBMapping, rethinkDBStoreParameters, getFieldsToQuery(null), getFields());
        ReqlExpr reqlExpr = dataStoreQuery.getRethinkDBDbQuery();
        String[] projection = new String[query.getFields().length];
        int counter = 0;
        for (String k : query.getFields()) {
            String dbFieldName = rethinkDBMapping.getDocumentField(k);
            if (dbFieldName != null && dbFieldName.length() > 0) {
                projection[counter] = dbFieldName;
                counter++;
            }
        }
        MapObject<String, Object> document = reqlExpr.replace(row -> row.without(projection)).run(connection, MapObject.class).first();
        int replacedCount = Integer.valueOf(document.get("replaced").toString());
        if (replacedCount > 0) {
            return replacedCount;
        } else {
            return 0;
        }
    }
}
Also used : RethinkDBQuery(org.apache.gora.rethinkdb.query.RethinkDBQuery) MapObject(com.rethinkdb.model.MapObject) MapObject(com.rethinkdb.model.MapObject) ReqlExpr(com.rethinkdb.gen.ast.ReqlExpr)

Aggregations

ReqlExpr (com.rethinkdb.gen.ast.ReqlExpr)2 MapObject (com.rethinkdb.model.MapObject)2 RethinkDBQuery (org.apache.gora.rethinkdb.query.RethinkDBQuery)2 IOException (java.io.IOException)1 PartitionQueryImpl (org.apache.gora.query.impl.PartitionQueryImpl)1 RethinkDBResult (org.apache.gora.rethinkdb.query.RethinkDBResult)1 GoraException (org.apache.gora.util.GoraException)1