Search in sources :

Example 6 with Table

use of org.folio.dbschema.Table 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)

Example 7 with Table

use of org.folio.dbschema.Table in project raml-module-builder by folio-org.

the class CQL2PgJSON method initDbTable.

InitDbTableResult initDbTable() {
    if (jsonField == null) {
        logger.log(Level.SEVERE, "loadDbSchema(): No primary table name, can not load");
        return InitDbTableResult.NO_PRIMARY_TABLE_NAME;
    }
    // Remove the json blob field name, usually ".jsonb", but in tests also
    // ".user_data" etc.
    String tname = this.jsonField.replaceAll("\\.[^.]+$", "");
    for (Table table : dbSchema.getTables()) {
        if ("DELETE".equalsIgnoreCase(table.getMode())) {
            continue;
        }
        if (tname.equalsIgnoreCase(table.getTableName())) {
            dbTable = table;
            return InitDbTableResult.TABLE_FOUND;
        }
        if (table.isWithAuditing() && tname.equalsIgnoreCase(table.getAuditingTableName())) {
            return InitDbTableResult.AUDIT_TABLE_FOUND;
        }
    }
    logger.log(Level.SEVERE, "loadDbSchema loadDbSchema(): Table {0} NOT FOUND", tname);
    return InitDbTableResult.NOT_FOUND;
}
Also used : Table(org.folio.dbschema.Table)

Example 8 with Table

use of org.folio.dbschema.Table in project raml-module-builder by folio-org.

the class CQL2PgJSONTest method queryByFtComparatorException.

@Test(expected = QueryValidationException.class)
public void queryByFtComparatorException() throws QueryValidationException {
    Table setup = new Table();
    setup.setTableName("test");
    cql2pgJson.queryByFt("indexText", "name=abc", "unknownComparator", null, setup);
}
Also used : Table(org.folio.dbschema.Table) Test(org.junit.Test)

Example 9 with Table

use of org.folio.dbschema.Table in project raml-module-builder by folio-org.

the class SchemaMaker method generateOptimisticLocking.

public static String generateOptimisticLocking(String tenant, String module, String tablename) {
    try {
        Table table = new Table();
        table.setTableName(tablename);
        table.setWithOptimisticLocking(OptimisticLockingMode.FAIL);
        Map<String, Object> parameters = new HashMap<>(3);
        parameters.put("myuniversity", tenant);
        parameters.put("mymodule", module);
        parameters.put("table", table);
        Template tableTemplate = cfg.getTemplate("optimistic_locking.ftl");
        Writer writer = new StringWriter();
        tableTemplate.process(parameters, writer);
        return writer.toString();
    } catch (IOException | TemplateException e) {
        throw new RuntimeException(e);
    }
}
Also used : Table(org.folio.dbschema.Table) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Template(freemarker.template.Template)

Aggregations

Table (org.folio.dbschema.Table)9 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 DbFkInfo (org.folio.cql2pgjson.model.DbFkInfo)2 Schema (org.folio.dbschema.Schema)2 Test (org.junit.Test)2 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Path (java.nio.file.Path)1 Collections (java.util.Collections)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