Search in sources :

Example 1 with PredicateCombinerPair

use of com.hortonworks.registries.storage.search.PredicateCombinerPair in project registry by hortonworks.

the class AbstractSelectQuery method buildSqlWithSearchQuery.

protected String buildSqlWithSearchQuery(SearchQuery searchQuery, Schema schema) {
    String sql = "SELECT * FROM " + fieldEncloser() + tableName + fieldEncloser();
    WhereClause whereClause = searchQuery.getWhereClause();
    Map<Schema.Field, Object> fieldsToValues = new HashMap<>();
    if (whereClause != null) {
        sql += " WHERE ";
        StringBuilder clauseString = new StringBuilder();
        for (PredicateCombinerPair predicateCombinerPair : whereClause.getPredicateCombinerPairs()) {
            WhereClauseCombiner.Operation combinerOperation = predicateCombinerPair.getCombinerOperation();
            Predicate predicate = predicateCombinerPair.getPredicate();
            clauseString.append(generateClauseString(predicate, fieldsToValues, schema));
            if (combinerOperation != null) {
                String opStr;
                switch(combinerOperation) {
                    case ENCL_START:
                        opStr = " ( ";
                        break;
                    case ENCL_FINISH:
                        opStr = " ) ";
                        break;
                    default:
                        opStr = combinerOperation.toString();
                }
                clauseString.append(opStr);
            }
        }
        sql += clauseString;
    }
    List<OrderBy> orderByFields = searchQuery.getOrderByFields();
    if (orderByFields != null && !orderByFields.isEmpty()) {
        sql += " ORDER BY " + join(orderByFields.stream().map(x -> fieldEncloser() + x.getFieldName() + fieldEncloser() + (x.isAsc() ? " ASC " : " DESC ")).collect(Collectors.toList()), ", ");
    }
    primaryKey = new PrimaryKey(fieldsToValues);
    columns = Lists.newArrayList(fieldsToValues.keySet());
    return sql;
}
Also used : OrderBy(com.hortonworks.registries.storage.search.OrderBy) HashMap(java.util.HashMap) WhereClause(com.hortonworks.registries.storage.search.WhereClause) PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) PredicateCombinerPair(com.hortonworks.registries.storage.search.PredicateCombinerPair) Predicate(com.hortonworks.registries.storage.search.Predicate) OrderByField(com.hortonworks.registries.storage.OrderByField) WhereClauseCombiner(com.hortonworks.registries.storage.search.WhereClauseCombiner)

Aggregations

OrderByField (com.hortonworks.registries.storage.OrderByField)1 PrimaryKey (com.hortonworks.registries.storage.PrimaryKey)1 OrderBy (com.hortonworks.registries.storage.search.OrderBy)1 Predicate (com.hortonworks.registries.storage.search.Predicate)1 PredicateCombinerPair (com.hortonworks.registries.storage.search.PredicateCombinerPair)1 WhereClause (com.hortonworks.registries.storage.search.WhereClause)1 WhereClauseCombiner (com.hortonworks.registries.storage.search.WhereClauseCombiner)1 HashMap (java.util.HashMap)1