Search in sources :

Example 6 with Condition

use of org.jooq.Condition in project jOOQ by jOOQ.

the class ParserImpl method parseFieldCaseIf.

private static final Field<?> parseFieldCaseIf(ParserContext ctx) {
    if (parseKeywordIf(ctx, "CASE")) {
        if (parseKeywordIf(ctx, "WHEN")) {
            CaseConditionStep step = null;
            Field result;
            do {
                Condition condition = parseCondition(ctx);
                parseKeyword(ctx, "THEN");
                Field value = parseField(ctx);
                step = step == null ? when(condition, value) : step.when(condition, value);
            } while (parseKeywordIf(ctx, "WHEN"));
            if (parseKeywordIf(ctx, "ELSE"))
                result = step.otherwise(parseField(ctx));
            else
                result = step;
            parseKeyword(ctx, "END");
            return result;
        } else {
            CaseValueStep init = choose(parseField(ctx));
            CaseWhenStep step = null;
            Field result;
            parseKeyword(ctx, "WHEN");
            do {
                Field when = parseField(ctx);
                parseKeyword(ctx, "THEN");
                Field then = parseField(ctx);
                step = step == null ? init.when(when, then) : step.when(when, then);
            } while (parseKeywordIf(ctx, "WHEN"));
            if (parseKeywordIf(ctx, "ELSE"))
                result = step.otherwise(parseField(ctx));
            else
                result = step;
            parseKeyword(ctx, "END");
            return result;
        }
    }
    return null;
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) CaseValueStep(org.jooq.CaseValueStep) CaseWhenStep(org.jooq.CaseWhenStep) CaseConditionStep(org.jooq.CaseConditionStep)

Example 7 with Condition

use of org.jooq.Condition in project jOOQ by jOOQ.

the class ParserImpl method parseBooleanTest.

private static final Condition parseBooleanTest(ParserContext ctx) {
    Condition condition = parseBooleanPrimary(ctx);
    if (parseKeywordIf(ctx, "IS")) {
        Field<Boolean> field = field(condition);
        boolean not = parseKeywordIf(ctx, "NOT");
        TruthValue truth = parseTruthValue(ctx);
        switch(truth) {
            case FALSE:
                return not ? field.ne(inline(false)) : field.eq(inline(false));
            case TRUE:
                return not ? field.ne(inline(true)) : field.eq(inline(true));
            case NULL:
                return not ? field.isNotNull() : field.isNull();
            default:
                throw ctx.internalError();
        }
    }
    return condition;
}
Also used : Condition(org.jooq.Condition)

Example 8 with Condition

use of org.jooq.Condition in project jOOQ by jOOQ.

the class ParserImpl method parseCreateTable.

private static final DDLQuery parseCreateTable(ParserContext ctx) {
    boolean ifNotExists = parseKeywordIf(ctx, "IF NOT EXISTS");
    Table<?> tableName = parseTableName(ctx);
    if (parseKeywordIf(ctx, "AS")) {
        Select<?> select = parseSelect(ctx);
        CreateTableAsStep<Record> s1 = ifNotExists ? ctx.dsl.createTableIfNotExists(tableName) : ctx.dsl.createTable(tableName);
        CreateTableFinalStep s2 = s1.as(select);
        return s2;
    } else {
        List<Field<?>> fields = new ArrayList<Field<?>>();
        List<Constraint> constraints = new ArrayList<Constraint>();
        boolean primary = false;
        boolean noConstraint = true;
        parse(ctx, '(');
        do {
            String fieldName = parseIdentifier(ctx);
            DataType<?> type = parseDataType(ctx);
            boolean nullable = false;
            boolean defaultValue = false;
            boolean unique = false;
            for (; ; ) {
                if (!nullable) {
                    if (parseKeywordIf(ctx, "NULL")) {
                        type = type.nullable(true);
                        nullable = true;
                        continue;
                    } else if (parseKeywordIf(ctx, "NOT NULL")) {
                        type = type.nullable(false);
                        nullable = true;
                        continue;
                    }
                }
                if (!defaultValue) {
                    if (parseKeywordIf(ctx, "DEFAULT")) {
                        type = type.defaultValue((Field) parseField(ctx));
                        defaultValue = true;
                        continue;
                    }
                }
                if (!unique) {
                    if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                        constraints.add(primaryKey(fieldName));
                        primary = true;
                        unique = true;
                        continue;
                    } else if (parseKeywordIf(ctx, "UNIQUE")) {
                        constraints.add(unique(fieldName));
                        unique = true;
                        continue;
                    }
                }
                if (parseKeywordIf(ctx, "CHECK")) {
                    parse(ctx, '(');
                    constraints.add(check(parseCondition(ctx)));
                    parse(ctx, ')');
                    continue;
                }
                break;
            }
            fields.add(field(name(fieldName), type));
        } while (parseIf(ctx, ',') && (noConstraint = !peekKeyword(ctx, "PRIMARY KEY") && !peekKeyword(ctx, "UNIQUE") && !peekKeyword(ctx, "FOREIGN KEY") && !peekKeyword(ctx, "CHECK") && !peekKeyword(ctx, "CONSTRAINT")));
        if (!noConstraint) {
            do {
                ConstraintTypeStep constraint = null;
                if (parseKeywordIf(ctx, "CONSTRAINT"))
                    constraint = constraint(parseIdentifier(ctx));
                if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                    if (primary) {
                        throw ctx.exception();
                    } else {
                        primary = true;
                        parse(ctx, '(');
                        Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                        parse(ctx, ')');
                        constraints.add(constraint == null ? primaryKey(fieldNames) : constraint.primaryKey(fieldNames));
                    }
                } else if (parseKeywordIf(ctx, "UNIQUE")) {
                    parse(ctx, '(');
                    Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    constraints.add(constraint == null ? unique(fieldNames) : constraint.unique(fieldNames));
                } else if (parseKeywordIf(ctx, "FOREIGN KEY")) {
                    parse(ctx, '(');
                    Field<?>[] referencing = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    parseKeyword(ctx, "REFERENCES");
                    Table<?> referencedTable = parseTableName(ctx);
                    parse(ctx, '(');
                    Field<?>[] referencedFields = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    if (referencing.length != referencedFields.length)
                        throw ctx.exception();
                    constraints.add(constraint == null ? foreignKey(referencing).references(referencedTable, referencedFields) : constraint.foreignKey(referencing).references(referencedTable, referencedFields));
                } else if (parseKeywordIf(ctx, "CHECK")) {
                    parse(ctx, '(');
                    Condition condition = parseCondition(ctx);
                    parse(ctx, ')');
                    constraints.add(constraint == null ? check(condition) : constraint.check(condition));
                } else {
                    throw ctx.unexpectedToken();
                }
            } while (parseIf(ctx, ','));
        }
        parse(ctx, ')');
        CreateTableAsStep<Record> s1 = ifNotExists ? ctx.dsl.createTableIfNotExists(tableName) : ctx.dsl.createTable(tableName);
        CreateTableColumnStep s2 = s1.columns(fields);
        CreateTableConstraintStep s3 = constraints.isEmpty() ? s2 : s2.constraints(constraints);
        return s3;
    }
}
Also used : CreateTableFinalStep(org.jooq.CreateTableFinalStep) ConstraintTypeStep(org.jooq.ConstraintTypeStep) Condition(org.jooq.Condition) CreateTableConstraintStep(org.jooq.CreateTableConstraintStep) Constraint(org.jooq.Constraint) ArrayList(java.util.ArrayList) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) CreateTableColumnStep(org.jooq.CreateTableColumnStep) Record(org.jooq.Record)

Example 9 with Condition

use of org.jooq.Condition in project jOOQ by jOOQ.

the class RowInCondition method delegate.

private final QueryPartInternal delegate(Configuration configuration) {
    if (asList(DERBY, FIREBIRD, SQLITE).contains(configuration.family())) {
        List<Condition> conditions = new ArrayList<Condition>();
        for (Row row : right) {
            conditions.add(new RowCondition(left, row, EQUALS));
        }
        Condition result = DSL.or(conditions);
        if (comparator == NOT_IN) {
            result = result.not();
        }
        return (QueryPartInternal) result;
    } else {
        return new Native();
    }
}
Also used : DSL.falseCondition(org.jooq.impl.DSL.falseCondition) Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) QueryPartInternal(org.jooq.QueryPartInternal) ArrayList(java.util.ArrayList) Row(org.jooq.Row)

Example 10 with Condition

use of org.jooq.Condition in project zipkin by openzipkin.

the class MySQLSpanStore method getTraces.

List<List<Span>> getTraces(@Nullable QueryRequest request, @Nullable Long traceIdHigh, @Nullable Long traceIdLow, boolean raw) {
    if (traceIdHigh != null && !strictTraceId)
        traceIdHigh = null;
    final Map<Pair<Long>, List<Span>> spansWithoutAnnotations;
    final Map<Row3<Long, Long, Long>, List<Record>> dbAnnotations;
    try (Connection conn = datasource.getConnection()) {
        Condition traceIdCondition = request != null ? schema.spanTraceIdCondition(toTraceIdQuery(context.get(conn), request)) : schema.spanTraceIdCondition(traceIdHigh, traceIdLow);
        spansWithoutAnnotations = context.get(conn).select(schema.spanFields).from(ZIPKIN_SPANS).where(traceIdCondition).stream().map(r -> Span.builder().traceIdHigh(maybeGet(r, ZIPKIN_SPANS.TRACE_ID_HIGH, 0L)).traceId(r.getValue(ZIPKIN_SPANS.TRACE_ID)).name(r.getValue(ZIPKIN_SPANS.NAME)).id(r.getValue(ZIPKIN_SPANS.ID)).parentId(r.getValue(ZIPKIN_SPANS.PARENT_ID)).timestamp(r.getValue(ZIPKIN_SPANS.START_TS)).duration(r.getValue(ZIPKIN_SPANS.DURATION)).debug(r.getValue(ZIPKIN_SPANS.DEBUG)).build()).collect(groupingBy((Span s) -> Pair.create(s.traceIdHigh, s.traceId), LinkedHashMap::new, Collectors.<Span>toList()));
        dbAnnotations = context.get(conn).select(schema.annotationFields).from(ZIPKIN_ANNOTATIONS).where(schema.annotationsTraceIdCondition(spansWithoutAnnotations.keySet())).orderBy(ZIPKIN_ANNOTATIONS.A_TIMESTAMP.asc(), ZIPKIN_ANNOTATIONS.A_KEY.asc()).stream().collect(groupingBy((Record a) -> row(maybeGet(a, ZIPKIN_ANNOTATIONS.TRACE_ID_HIGH, 0L), a.getValue(ZIPKIN_ANNOTATIONS.TRACE_ID), a.getValue(ZIPKIN_ANNOTATIONS.SPAN_ID)), LinkedHashMap::new, // LinkedHashMap preserves order while grouping
        Collectors.<Record>toList()));
    } catch (SQLException e) {
        throw new RuntimeException("Error querying for " + request + ": " + e.getMessage());
    }
    List<Span> allSpans = new ArrayList<>(spansWithoutAnnotations.size());
    for (List<Span> spans : spansWithoutAnnotations.values()) {
        for (Span s : spans) {
            Span.Builder span = s.toBuilder();
            Row3<Long, Long, Long> key = row(s.traceIdHigh, s.traceId, s.id);
            if (dbAnnotations.containsKey(key)) {
                for (Record a : dbAnnotations.get(key)) {
                    Endpoint endpoint = endpoint(a);
                    int type = a.getValue(ZIPKIN_ANNOTATIONS.A_TYPE);
                    if (type == -1) {
                        span.addAnnotation(Annotation.create(a.getValue(ZIPKIN_ANNOTATIONS.A_TIMESTAMP), a.getValue(ZIPKIN_ANNOTATIONS.A_KEY), endpoint));
                    } else {
                        span.addBinaryAnnotation(BinaryAnnotation.create(a.getValue(ZIPKIN_ANNOTATIONS.A_KEY), a.getValue(ZIPKIN_ANNOTATIONS.A_VALUE), Type.fromValue(type), endpoint));
                    }
                }
            }
            allSpans.add(span.build());
        }
    }
    return GroupByTraceId.apply(allSpans, strictTraceId, !raw);
}
Also used : Condition(org.jooq.Condition) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Span(zipkin.Span) DependencyLinkSpan(zipkin.internal.DependencyLinkSpan) Endpoint(zipkin.Endpoint) LinkedHashMap(java.util.LinkedHashMap) Endpoint(zipkin.Endpoint) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Record(org.jooq.Record) Pair(zipkin.internal.Pair) Row3(org.jooq.Row3)

Aggregations

Condition (org.jooq.Condition)29 Field (org.jooq.Field)14 GroupField (org.jooq.GroupField)11 SortField (org.jooq.SortField)11 TableField (org.jooq.TableField)11 ArrayList (java.util.ArrayList)9 List (java.util.List)5 Arrays.asList (java.util.Arrays.asList)3 Comparator (org.jooq.Comparator)3 QueryPartInternal (org.jooq.QueryPartInternal)3 Record (org.jooq.Record)3 SQLDialect (org.jooq.SQLDialect)3 Collections.emptyList (java.util.Collections.emptyList)2 LinkedHashMap (java.util.LinkedHashMap)2 ConstraintTypeStep (org.jooq.ConstraintTypeStep)2 Name (org.jooq.Name)2 Record1 (org.jooq.Record1)2 Row (org.jooq.Row)2 RowN (org.jooq.RowN)2 Table (org.jooq.Table)2