Search in sources :

Example 1 with CqlModifiers

use of org.folio.cql2pgjson.model.CqlModifiers in project raml-module-builder by folio-org.

the class CQL2PgJSON method indexNodeForForeignTable.

private String indexNodeForForeignTable(CQLTermNode node, Table targetTable, String[] foreignTarget) throws QueryValidationException {
    String foreignTableJsonb = targetTable.getTableName() + "." + JSONB_COLUMN_NAME;
    IndexTextAndJsonValues vals = new IndexTextAndJsonValues();
    vals.setIndexJson(SqlUtil.Cql2PgUtil.cqlNameAsSqlJson(foreignTableJsonb, foreignTarget[1]));
    vals.setIndexText(SqlUtil.Cql2PgUtil.cqlNameAsSqlText(foreignTableJsonb, foreignTarget[1]));
    CqlModifiers cqlModifiers = new CqlModifiers(node);
    String indexField = foreignTarget[1];
    return indexNode(indexField, targetTable, node, vals, cqlModifiers);
}
Also used : IndexTextAndJsonValues(org.folio.cql2pgjson.model.IndexTextAndJsonValues) CqlModifiers(org.folio.cql2pgjson.model.CqlModifiers)

Example 2 with CqlModifiers

use of org.folio.cql2pgjson.model.CqlModifiers in project raml-module-builder by folio-org.

the class CQL2PgJSON method toSql.

// suppress "reduce to one continue in for loop"
@SuppressWarnings("squid:S135")
private SqlSelect toSql(CQLSortNode node) throws QueryValidationException {
    StringBuilder order = new StringBuilder();
    String where = pg(node.getSubtree());
    boolean firstIndex = true;
    for (ModifierSet modifierSet : node.getSortIndexes()) {
        if (firstIndex) {
            firstIndex = false;
        } else {
            order.append(", ");
        }
        String desc = "";
        CqlModifiers modifiers = new CqlModifiers(modifierSet);
        if (modifiers.getCqlSort() == CqlSort.DESCENDING) {
            desc = " DESC";
        }
        // ASC not needed, it's Postgres' default
        String field = modifierSet.getBase();
        DbIndex dbIndex = dbIndexMap.computeIfAbsent(field, f -> DbSchemaUtils.getDbIndex(dbTable, f));
        if (dbIndex.isForeignKey() || "id".equals(field)) {
            order.append(field).append(desc);
            continue;
        }
        IndexTextAndJsonValues vals = getIndexTextAndJsonValues(field);
        // if sort field is marked explicitly as number type
        if (modifiers.getCqlTermFormat() == CqlTermFormat.NUMBER) {
            order.append(vals.getIndexJson()).append(desc);
            continue;
        }
        // We assume that a CREATE INDEX for this has been installed.
        order.append(wrapForLength(wrapInLowerUnaccent(vals.getIndexText(), modifiers))).append(desc).append(", ").append(wrapInLowerUnaccent(vals.getIndexText(), modifiers)).append(desc);
    }
    return new SqlSelect(where, order.toString());
}
Also used : CqlModifiers(org.folio.cql2pgjson.model.CqlModifiers) IndexTextAndJsonValues(org.folio.cql2pgjson.model.IndexTextAndJsonValues) SqlSelect(org.folio.cql2pgjson.model.SqlSelect) ModifierSet(org.z3950.zing.cql.ModifierSet) DbIndex(org.folio.cql2pgjson.model.DbIndex)

Example 3 with CqlModifiers

use of org.folio.cql2pgjson.model.CqlModifiers in project raml-module-builder by folio-org.

the class CQL2PgJSONTest method masked.

@Test
public void masked() throws CQLFeatureUnsupportedException {
    select("name=/masked Lea  # Lea Long");
    ModifierSet modifierSet = new ModifierSet("base");
    modifierSet.addModifier("masked");
    CqlModifiers cqlModifiers = new CqlModifiers(modifierSet);
    assertThat(cqlModifiers.getCqlMasking(), is(CqlMasking.MASKED));
}
Also used : CqlModifiers(org.folio.cql2pgjson.model.CqlModifiers) ModifierSet(org.z3950.zing.cql.ModifierSet) Test(org.junit.Test)

Example 4 with CqlModifiers

use of org.folio.cql2pgjson.model.CqlModifiers in project raml-module-builder by folio-org.

the class CQL2PgJSON method index2sql.

/**
 * Create an SQL expression where index is applied to all matches.
 *
 * @param index index to use
 * @param node CQLTermNode to use
 *
 * @return SQL expression
 * @throws QueryValidationException
 */
private String index2sql(String index, CQLTermNode node) throws QueryValidationException {
    IndexTextAndJsonValues vals = getIndexTextAndJsonValues(index);
    CqlModifiers cqlModifiers = new CqlModifiers(node);
    return indexNode(index, this.dbTable, node, vals, cqlModifiers);
}
Also used : IndexTextAndJsonValues(org.folio.cql2pgjson.model.IndexTextAndJsonValues) CqlModifiers(org.folio.cql2pgjson.model.CqlModifiers)

Example 5 with CqlModifiers

use of org.folio.cql2pgjson.model.CqlModifiers in project raml-module-builder by folio-org.

the class CQL2PgJSON method indexNode.

private String indexNode(String index, Table targetTable, CQLTermNode node, IndexTextAndJsonValues vals, CqlModifiers modifiers) throws QueryValidationException {
    // primary key
    if ("id".equals(index)) {
        return pgId(node, index);
    }
    DbIndex dbIndex;
    if (targetTable == null || dbTable.equals(targetTable)) {
        dbIndex = dbIndexMap.computeIfAbsent(index, i -> DbSchemaUtils.getDbIndex(dbTable, index));
    } else {
        // foreign table
        dbIndex = dbIndexMap.computeIfAbsent(vals.getIndexJson(), i -> DbSchemaUtils.getDbIndex(targetTable, index));
    }
    if (dbIndex.isForeignKey()) {
        return pgId(node, index);
    }
    String comparator = node.getRelation().getBase().toLowerCase();
    switch(comparator) {
        case "=":
            if (CqlTermFormat.NUMBER == modifiers.getCqlTermFormat()) {
                return queryBySql(dbIndex, vals, node, comparator, modifiers);
            } else {
                return queryByFt(index, dbIndex, vals, node, comparator, modifiers, targetTable);
            }
        case "adj":
        case "all":
        case "any":
            return queryByFt(index, dbIndex, vals, node, comparator, modifiers, targetTable);
        case "==":
        case "<>":
            if (CqlTermFormat.STRING == modifiers.getCqlTermFormat()) {
                return queryByLike(index, dbIndex, vals, node, comparator, modifiers, targetTable);
            } else {
                return queryBySql(dbIndex, vals, node, comparator, modifiers);
            }
        case "<":
        case ">":
        case "<=":
        case ">=":
            return queryBySql(dbIndex, vals, node, comparator, modifiers);
        default:
            throw new CQLFeatureUnsupportedException("Relation " + comparator + " not implemented yet: " + node.toString());
    }
}
Also used : ModifierSet(org.z3950.zing.cql.ModifierSet) SqlSelect(org.folio.cql2pgjson.model.SqlSelect) Cql2SqlUtil(org.folio.cql2pgjson.util.Cql2SqlUtil) CqlCase(org.folio.cql2pgjson.model.CqlCase) CQLTermNode(org.z3950.zing.cql.CQLTermNode) HashMap(java.util.HashMap) DbIndex(org.folio.cql2pgjson.model.DbIndex) CQLOrNode(org.z3950.zing.cql.CQLOrNode) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) CqlTermFormat(org.folio.cql2pgjson.model.CqlTermFormat) Index(org.folio.dbschema.Index) ObjectMapperTool(org.folio.dbschema.ObjectMapperTool) ObjectUtils(org.apache.commons.lang3.ObjectUtils) CqlSort(org.folio.cql2pgjson.model.CqlSort) DbFkInfo(org.folio.cql2pgjson.model.DbFkInfo) Map(java.util.Map) QueryValidationException(org.folio.cql2pgjson.exception.QueryValidationException) CQLParseException(org.z3950.zing.cql.CQLParseException) Schema(org.folio.dbschema.Schema) Table(org.folio.dbschema.Table) LinkedList(java.util.LinkedList) CQLFeatureUnsupportedException(org.folio.cql2pgjson.exception.CQLFeatureUnsupportedException) CQLSortNode(org.z3950.zing.cql.CQLSortNode) Modifier(org.z3950.zing.cql.Modifier) CQLBooleanNode(org.z3950.zing.cql.CQLBooleanNode) IndexTextAndJsonValues(org.folio.cql2pgjson.model.IndexTextAndJsonValues) SqlUtil(org.folio.dbschema.util.SqlUtil) IOException(java.io.IOException) CqlAccents(org.folio.cql2pgjson.model.CqlAccents) DbSchemaUtils(org.folio.cql2pgjson.util.DbSchemaUtils) Logger(java.util.logging.Logger) FieldException(org.folio.cql2pgjson.exception.FieldException) CqlModifiers(org.folio.cql2pgjson.model.CqlModifiers) List(java.util.List) CQLAndNode(org.z3950.zing.cql.CQLAndNode) CQLParser(org.z3950.zing.cql.CQLParser) ServerChoiceIndexesException(org.folio.cql2pgjson.exception.ServerChoiceIndexesException) ResourceUtil(org.folio.util.ResourceUtil) Pattern(java.util.regex.Pattern) CQLNode(org.z3950.zing.cql.CQLNode) Collections(java.util.Collections) CQLNotNode(org.z3950.zing.cql.CQLNotNode) DbIndex(org.folio.cql2pgjson.model.DbIndex) CQLFeatureUnsupportedException(org.folio.cql2pgjson.exception.CQLFeatureUnsupportedException)

Aggregations

CqlModifiers (org.folio.cql2pgjson.model.CqlModifiers)5 IndexTextAndJsonValues (org.folio.cql2pgjson.model.IndexTextAndJsonValues)4 DbIndex (org.folio.cql2pgjson.model.DbIndex)2 SqlSelect (org.folio.cql2pgjson.model.SqlSelect)2 ModifierSet (org.z3950.zing.cql.ModifierSet)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 List (java.util.List)1 Map (java.util.Map)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Pattern (java.util.regex.Pattern)1 ObjectUtils (org.apache.commons.lang3.ObjectUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1 CQLFeatureUnsupportedException (org.folio.cql2pgjson.exception.CQLFeatureUnsupportedException)1 FieldException (org.folio.cql2pgjson.exception.FieldException)1 QueryValidationException (org.folio.cql2pgjson.exception.QueryValidationException)1