Search in sources :

Example 1 with CQLNode

use of org.z3950.zing.cql.CQLNode in project okapi by folio-org.

the class CQLUtil method reducer.

public static CQLNode reducer(CQLNode vn1, CQLTermNode tn, Comparator<CQLTermNode> cmp) {
    if (vn1 instanceof CQLBooleanNode) {
        return reduceBoolean((CQLBooleanNode) vn1, tn, cmp);
    } else if (vn1 instanceof CQLTermNode) {
        CQLTermNode n1 = (CQLTermNode) vn1;
        if (cmp != null && cmp.compare(n1, tn) == 0) {
            return null;
        }
        return new CQLTermNode(n1.getIndex(), n1.getRelation(), n1.getTerm());
    } else if (vn1 instanceof CQLSortNode) {
        CQLSortNode n1 = (CQLSortNode) vn1;
        CQLNode n2 = reducer(n1.getSubtree(), tn, cmp);
        if (n2 == null) {
            return null;
        } else {
            CQLSortNode sn = new CQLSortNode(n2);
            List<ModifierSet> mods = n1.getSortIndexes();
            for (ModifierSet mSet : mods) {
                sn.addSortIndex(mSet);
            }
            return sn;
        }
    } else if (vn1 instanceof CQLPrefixNode) {
        CQLPrefixNode n1 = (CQLPrefixNode) vn1;
        CQLNode n2 = reducer(n1.getSubtree(), tn, cmp);
        if (n2 == null) {
            return null;
        } else {
            CQLPrefix prefix = n1.getPrefix();
            return new CQLPrefixNode(prefix.getName(), prefix.getIdentifier(), n2);
        }
    } else {
        throw new IllegalArgumentException("unknown type for CQLNode: " + vn1.toString());
    }
}
Also used : CQLSortNode(org.z3950.zing.cql.CQLSortNode) CQLTermNode(org.z3950.zing.cql.CQLTermNode) CQLPrefixNode(org.z3950.zing.cql.CQLPrefixNode) CQLBooleanNode(org.z3950.zing.cql.CQLBooleanNode) CQLPrefix(org.z3950.zing.cql.CQLPrefix) CQLNode(org.z3950.zing.cql.CQLNode) ModifierSet(org.z3950.zing.cql.ModifierSet)

Example 2 with CQLNode

use of org.z3950.zing.cql.CQLNode in project okapi by folio-org.

the class CQLUtilTest method reduce.

private String reduce(String input) {
    CQLParser parser = new CQLParser(CQLParser.V1POINT2);
    try {
        CQLRelation rel = new CQLRelation("=");
        CQLTermNode source = new CQLTermNode("source", rel, "kb");
        CQLNode top = parser.parse(input);
        Comparator<CQLTermNode> f = (CQLTermNode n1, CQLTermNode n2) -> n1.getIndex().equals(n2.getIndex()) ? 0 : -1;
        CQLNode res = CQLUtil.reducer(top, source, f);
        if (res == null) {
            return "null";
        } else {
            return res.toCQL();
        }
    } catch (Exception ex) {
        return "Error: " + ex.getMessage();
    }
}
Also used : CQLTermNode(org.z3950.zing.cql.CQLTermNode) CQLParser(org.z3950.zing.cql.CQLParser) CQLRelation(org.z3950.zing.cql.CQLRelation) CQLNode(org.z3950.zing.cql.CQLNode)

Example 3 with CQLNode

use of org.z3950.zing.cql.CQLNode in project okapi by folio-org.

the class CQLUtilTest method eval.

private boolean eval(String input) {
    CQLParser parser = new CQLParser(CQLParser.V1POINT2);
    try {
        CQLRelation rel = new CQLRelation("=");
        CQLTermNode source = new CQLTermNode("source", rel, "kb");
        CQLNode top = parser.parse(input);
        Comparator<CQLTermNode> f = (CQLTermNode n1, CQLTermNode n2) -> {
            if (n1.getIndex().equals(n2.getIndex()) && !n1.getTerm().equals(n2.getTerm())) {
                return -1;
            }
            return 0;
        };
        return CQLUtil.eval(top, source, f);
    } catch (Exception ex) {
        return false;
    }
}
Also used : CQLTermNode(org.z3950.zing.cql.CQLTermNode) CQLParser(org.z3950.zing.cql.CQLParser) CQLRelation(org.z3950.zing.cql.CQLRelation) CQLNode(org.z3950.zing.cql.CQLNode)

Example 4 with CQLNode

use of org.z3950.zing.cql.CQLNode in project raml-module-builder by folio-org.

the class CQL2PgJSON method toSql.

/**
 * Convert the CQL query into a SQL query and return the WHERE and the ORDER BY clause.
 * @param cql  the query to convert
 * @return SQL query
 * @throws QueryValidationException
 */
public SqlSelect toSql(String cql) throws QueryValidationException {
    try {
        CQLParser parser = new CQLParser();
        CQLNode node = parser.parse(cql);
        return toSql(node);
    } catch (IOException | CQLParseException e) {
        throw new QueryValidationException(e);
    }
}
Also used : QueryValidationException(org.folio.cql2pgjson.exception.QueryValidationException) IOException(java.io.IOException) CQLParser(org.z3950.zing.cql.CQLParser) CQLParseException(org.z3950.zing.cql.CQLParseException) CQLNode(org.z3950.zing.cql.CQLNode)

Example 5 with CQLNode

use of org.z3950.zing.cql.CQLNode in project raml-module-builder by folio-org.

the class CQL2PgJSON method cql2pgJson.

/**
 * Return an SQL WHERE clause for the CQL expression.
 * @param cql  CQL expression to convert
 * @return SQL WHERE clause, without leading "WHERE ", may contain "ORDER BY" clause
 * @throws QueryValidationException  when parsing or validating cql fails
 *
 * @deprecated use toSql instead
 */
@Deprecated
public String cql2pgJson(String cql) throws QueryValidationException {
    try {
        CQLParser parser = new CQLParser();
        CQLNode node = parser.parse(cql);
        return pg(node);
    } catch (IOException | CQLParseException e) {
        throw new QueryValidationException(e);
    }
}
Also used : QueryValidationException(org.folio.cql2pgjson.exception.QueryValidationException) IOException(java.io.IOException) CQLParser(org.z3950.zing.cql.CQLParser) CQLParseException(org.z3950.zing.cql.CQLParseException) CQLNode(org.z3950.zing.cql.CQLNode)

Aggregations

CQLNode (org.z3950.zing.cql.CQLNode)7 CQLParser (org.z3950.zing.cql.CQLParser)5 IOException (java.io.IOException)3 CQLParseException (org.z3950.zing.cql.CQLParseException)3 CQLTermNode (org.z3950.zing.cql.CQLTermNode)3 QueryValidationException (org.folio.cql2pgjson.exception.QueryValidationException)2 CQLRelation (org.z3950.zing.cql.CQLRelation)2 ModifierSet (org.z3950.zing.cql.ModifierSet)2 CQLAndNode (org.z3950.zing.cql.CQLAndNode)1 CQLBooleanNode (org.z3950.zing.cql.CQLBooleanNode)1 CQLNotNode (org.z3950.zing.cql.CQLNotNode)1 CQLOrNode (org.z3950.zing.cql.CQLOrNode)1 CQLPrefix (org.z3950.zing.cql.CQLPrefix)1 CQLPrefixNode (org.z3950.zing.cql.CQLPrefixNode)1 CQLProxNode (org.z3950.zing.cql.CQLProxNode)1 CQLSortNode (org.z3950.zing.cql.CQLSortNode)1 Modifier (org.z3950.zing.cql.Modifier)1