Search in sources :

Example 1 with CQLTermNode

use of org.z3950.zing.cql.CQLTermNode 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 CQLTermNode

use of org.z3950.zing.cql.CQLTermNode 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 CQLTermNode

use of org.z3950.zing.cql.CQLTermNode 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 CQLTermNode

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

the class CQL2PgJSON method queryByLike.

/**
 * Create an SQL expression using LIKE query syntax.
 *
 * @param hasIndex
 * @param vals
 * @param node
 * @param comparator
 * @param modifiers
 * @return
 */
private String queryByLike(String index, DbIndex dbIndex, IndexTextAndJsonValues vals, CQLTermNode node, String comparator, CqlModifiers modifiers, Table targetTable) throws QueryValidationException {
    final String indexText = vals.getIndexText();
    final Index schemaIndex = ObjectUtils.firstNonNull(dbIndex.getGinIndex(), dbIndex.getLikeIndex(), dbIndex.getUniqueIndex(), dbIndex.getIndex());
    String sql = null;
    List<Modifier> relationModifiers = modifiers.getRelationModifiers();
    if (!relationModifiers.isEmpty()) {
        sql = arrayNode(index, node, modifiers, relationModifiers, schemaIndex, vals, targetTable);
    } else {
        String likeOperator = comparator.equals("<>") ? "NOT LIKE" : "LIKE";
        String term = "'" + Cql2SqlUtil.cql2like(node.getTerm()) + "'";
        String indexMod;
        if (schemaIndex != null && schemaIndex.getMultiFieldNames() != null) {
            indexMod = schemaIndex.getFinalSqlExpression(targetTable.getTableName());
        } else if (schemaIndex != null && schemaIndex.getSqlExpression() != null) {
            indexMod = schemaIndex.getSqlExpression();
        } else {
            indexMod = wrapIndexExpression(indexText, schemaIndex);
        }
        if (schemaIndex != null && schemaIndex == dbIndex.getIndex()) {
            sql = createLikeLengthCase(comparator, indexMod, schemaIndex, likeOperator, term);
        } else {
            sql = indexMod + " " + likeOperator + " " + wrapQueryExpression(term, schemaIndex);
        }
    }
    if (Cql2SqlUtil.hasCqlWildCard(node.getTerm())) {
        // FIXME: right truncation "abc*" works with index/uniqueIndex
        if (!dbIndex.hasGinIndex() && !dbIndex.hasLikeIndex()) {
            String s = String.format("%s, CQL >>> SQL: %s >>> %s", indexText, node.toCQL(), sql);
            logger.log(Level.WARNING, "Doing wildcard LIKE search without index for {0}", s);
        }
    } else {
        if (schemaIndex == null) {
            String s = String.format("%s, CQL >>> SQL: %s >>> %s", indexText, node.toCQL(), sql);
            logger.log(Level.WARNING, "Doing LIKE search without index for {0}", s);
        }
    }
    logger.log(Level.FINE, "index {0} generated SQL {1}", new Object[] { indexText, sql });
    return sql;
}
Also used : DbIndex(org.folio.cql2pgjson.model.DbIndex) Index(org.folio.dbschema.Index) Modifier(org.z3950.zing.cql.Modifier)

Example 5 with CQLTermNode

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

the class CQL2PgJSON method arrayNode.

private String arrayNode(String index, CQLTermNode node, CqlModifiers modifiers, List<Modifier> relationModifiers, Index schemaIndex, IndexTextAndJsonValues incomingvals, Table targetTable) throws QueryValidationException {
    StringBuilder sqlAnd = new StringBuilder();
    StringBuilder sqlOr = new StringBuilder();
    // avoid recursion
    modifiers.setRelationModifiers(new LinkedList<>());
    for (Modifier relationModifier : relationModifiers) {
        final String modifierName = relationModifier.getType().substring(1);
        final String modifierValue = relationModifier.getValue();
        String foundModifier = lookupModifier(schemaIndex, modifierName);
        if (foundModifier == null) {
            throw new QueryValidationException("CQL: Unsupported relation modifier " + relationModifier.getType());
        }
        if (modifierValue == null) {
            if (sqlOr.length() == 0) {
                sqlOr.append("(");
            } else {
                sqlOr.append(" or ");
            }
            IndexTextAndJsonValues vals = new IndexTextAndJsonValues();
            vals.setIndexText(SqlUtil.Cql2PgUtil.cqlNameAsSqlText("t.c", foundModifier));
            sqlOr.append(indexNode(index, this.dbTable, node, vals, modifiers));
        } else {
            final String comparator = relationModifier.getComparison();
            if (!"=".equals(comparator)) {
                throw new QueryValidationException("CQL: Unsupported comparison for relation modifier " + relationModifier.getType());
            }
            sqlAnd.append(" and ");
            sqlAnd.append(queryByFt(SqlUtil.Cql2PgUtil.cqlNameAsSqlText("t.c", foundModifier), modifierValue, comparator, schemaIndex, targetTable));
        }
    }
    if (sqlOr.length() > 0) {
        sqlOr.append(")");
    } else {
        String modifiersSubfield = null;
        if (schemaIndex != null) {
            modifiersSubfield = schemaIndex.getArraySubfield();
        }
        if (modifiersSubfield == null) {
            throw new QueryValidationException("CQL: No arraySubfield defined for index " + index);
        }
        IndexTextAndJsonValues vals = new IndexTextAndJsonValues();
        vals.setIndexText(SqlUtil.Cql2PgUtil.cqlNameAsSqlText("t.c", modifiersSubfield));
        sqlOr.append(indexNode(index, this.dbTable, node, vals, modifiers));
    }
    return "id in (select t.id" + " from (select id as id, " + "             jsonb_array_elements(" + incomingvals.getIndexJson() + ") as c" + "      ) as t" + " where " + sqlOr.toString() + sqlAnd.toString() + ")";
}
Also used : IndexTextAndJsonValues(org.folio.cql2pgjson.model.IndexTextAndJsonValues) QueryValidationException(org.folio.cql2pgjson.exception.QueryValidationException) Modifier(org.z3950.zing.cql.Modifier)

Aggregations

CQLNode (org.z3950.zing.cql.CQLNode)5 Modifier (org.z3950.zing.cql.Modifier)5 CQLTermNode (org.z3950.zing.cql.CQLTermNode)4 DbIndex (org.folio.cql2pgjson.model.DbIndex)3 Index (org.folio.dbschema.Index)3 ModifierSet (org.z3950.zing.cql.ModifierSet)3 CQLFeatureUnsupportedException (org.folio.cql2pgjson.exception.CQLFeatureUnsupportedException)2 QueryValidationException (org.folio.cql2pgjson.exception.QueryValidationException)2 IndexTextAndJsonValues (org.folio.cql2pgjson.model.IndexTextAndJsonValues)2 CQLAndNode (org.z3950.zing.cql.CQLAndNode)2 CQLBooleanNode (org.z3950.zing.cql.CQLBooleanNode)2 CQLNotNode (org.z3950.zing.cql.CQLNotNode)2 CQLOrNode (org.z3950.zing.cql.CQLOrNode)2 CQLParser (org.z3950.zing.cql.CQLParser)2 CQLRelation (org.z3950.zing.cql.CQLRelation)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1