Search in sources :

Example 71 with Field

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

the class RowCondition method delegate.

private final QueryPartInternal delegate(Configuration configuration) {
    SQLDialect dialect = configuration.dialect();
    // Regular comparison predicate emulation
    if (asList(EQUALS, NOT_EQUALS).contains(comparator) && asList(DERBY, FIREBIRD, SQLITE).contains(dialect.family())) {
        List<Condition> conditions = new ArrayList<Condition>();
        Field<?>[] leftFields = left.fields();
        Field<?>[] rightFields = right.fields();
        for (int i = 0; i < leftFields.length; i++) {
            conditions.add(leftFields[i].equal((Field) rightFields[i]));
        }
        Condition result = DSL.and(conditions);
        if (comparator == NOT_EQUALS) {
            result = result.not();
        }
        return (QueryPartInternal) result;
    } else // Ordering comparison predicate emulation
    if (asList(GREATER, GREATER_OR_EQUAL, LESS, LESS_OR_EQUAL).contains(comparator) && asList(DERBY, CUBRID, FIREBIRD, SQLITE).contains(dialect.family())) {
        // The order component of the comparator (stripping the equal component)
        Comparator order = (comparator == GREATER) ? GREATER : (comparator == GREATER_OR_EQUAL) ? GREATER : (comparator == LESS) ? LESS : (comparator == LESS_OR_EQUAL) ? LESS : null;
        // [#2658] The factored order component of the comparator (enforcing the equal component)
        Comparator factoredOrder = (comparator == GREATER) ? GREATER_OR_EQUAL : (comparator == GREATER_OR_EQUAL) ? GREATER_OR_EQUAL : (comparator == LESS) ? LESS_OR_EQUAL : (comparator == LESS_OR_EQUAL) ? LESS_OR_EQUAL : null;
        // Whether the comparator has an equal component
        boolean equal = (comparator == GREATER_OR_EQUAL) || (comparator == LESS_OR_EQUAL);
        // The following algorithm emulates the equivalency of these expressions:
        // (A, B, C) > (X, Y, Z)
        // (A > X) OR (A = X AND B > Y) OR (A = X AND B = Y AND C > Z)
        List<Condition> outer = new ArrayList<Condition>();
        Field<?>[] leftFields = left.fields();
        Field<?>[] rightFields = right.fields();
        for (int i = 0; i < leftFields.length; i++) {
            List<Condition> inner = new ArrayList<Condition>();
            for (int j = 0; j < i; j++) {
                inner.add(leftFields[j].equal((Field) rightFields[j]));
            }
            inner.add(leftFields[i].compare(order, (Field) rightFields[i]));
            outer.add(DSL.and(inner));
        }
        if (equal) {
            outer.add(new RowCondition(left, right, Comparator.EQUALS));
        }
        Condition result = DSL.or(outer);
        // (A >= X) AND ((A > X) OR (A = X AND B > Y) OR (A = X AND B = Y AND C > Z))
        if (leftFields.length > 1) {
            result = leftFields[0].compare(factoredOrder, (Field) rightFields[0]).and(result);
        }
        return (QueryPartInternal) result;
    } else {
        return new Native();
    }
}
Also used : Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) Comparator(org.jooq.Comparator) QueryPartInternal(org.jooq.QueryPartInternal) Field(org.jooq.Field) SQLDialect(org.jooq.SQLDialect) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList)

Example 72 with Field

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

the class ParserImpl method parseGeneralSetFunctionIf.

private static final AggregateFunction<?> parseGeneralSetFunctionIf(ParserContext ctx) {
    boolean distinct;
    Field arg;
    ComputationalOperation operation = parseComputationalOperationIf(ctx);
    if (operation == null)
        return null;
    parse(ctx, '(');
    distinct = parseSetQuantifier(ctx);
    arg = parseField(ctx);
    parse(ctx, ')');
    switch(operation) {
        case AVG:
            return distinct ? avgDistinct(arg) : avg(arg);
        case MAX:
            return distinct ? maxDistinct(arg) : max(arg);
        case MIN:
            return distinct ? minDistinct(arg) : min(arg);
        case SUM:
            return distinct ? sumDistinct(arg) : sum(arg);
        case EVERY:
            return every(arg);
        case ANY:
            return boolOr(arg);
        case STDDEV_POP:
            return stddevPop(arg);
        case STDDEV_SAMP:
            return stddevSamp(arg);
        case VAR_POP:
            return varPop(arg);
        case VAR_SAMP:
            return varSamp(arg);
        default:
            throw ctx.unexpectedToken();
    }
}
Also used : TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField)

Example 73 with Field

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

the class ParserImpl method parseCreateIndex.

private static final DDLQuery parseCreateIndex(ParserContext ctx) {
    boolean ifNotExists = parseKeywordIf(ctx, "IF NOT EXISTS");
    Name indexName = parseIndexName(ctx);
    parseKeyword(ctx, "ON");
    Table<?> tableName = parseTableName(ctx);
    parse(ctx, '(');
    Field<?>[] fieldNames = Tools.fieldsByName(parseIdentifiers(ctx));
    parse(ctx, ')');
    Condition condition = parseKeywordIf(ctx, "WHERE") ? parseCondition(ctx) : null;
    CreateIndexStep s1 = ifNotExists ? ctx.dsl.createIndexIfNotExists(indexName) : ctx.dsl.createIndex(indexName);
    CreateIndexWhereStep s2 = s1.on(tableName, fieldNames);
    CreateIndexFinalStep s3 = condition != null ? s2.where(condition) : s2;
    return s3;
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) CreateIndexFinalStep(org.jooq.CreateIndexFinalStep) CreateIndexWhereStep(org.jooq.CreateIndexWhereStep) CreateIndexStep(org.jooq.CreateIndexStep) Name(org.jooq.Name)

Example 74 with Field

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

the class ParserImpl method parsePredicate.

private static final Condition parsePredicate(ParserContext ctx) {
    if (parseKeywordIf(ctx, "EXISTS")) {
        parse(ctx, '(');
        Select<?> select = parseSelect(ctx);
        parse(ctx, ')');
        return exists(select);
    } else {
        // TODO row value expressions
        Field left;
        Comparator comp;
        boolean not;
        left = parseFieldConcat(ctx, null);
        not = parseKeywordIf(ctx, "NOT");
        if (!not && (comp = parseComparatorIf(ctx)) != null) {
            boolean all = parseKeywordIf(ctx, "ALL");
            boolean any = !all && (parseKeywordIf(ctx, "ANY") || parseKeywordIf(ctx, "SOME"));
            if (all || any)
                parse(ctx, '(');
            Condition result = all ? left.compare(comp, DSL.all(parseSelect(ctx))) : any ? left.compare(comp, DSL.any(parseSelect(ctx))) : left.compare(comp, parseFieldConcat(ctx, null));
            if (all || any)
                parse(ctx, ')');
            return result;
        } else if (!not && parseKeywordIf(ctx, "IS")) {
            not = parseKeywordIf(ctx, "NOT");
            if (parseKeywordIf(ctx, "NULL"))
                return not ? left.isNotNull() : left.isNull();
            parseKeyword(ctx, "DISTINCT FROM");
            Field right = parseFieldConcat(ctx, null);
            return not ? left.isNotDistinctFrom(right) : left.isDistinctFrom(right);
        } else if (parseKeywordIf(ctx, "IN")) {
            Condition result;
            parse(ctx, '(');
            if (peekKeyword(ctx, "SELECT"))
                result = not ? left.notIn(parseSelect(ctx)) : left.in(parseSelect(ctx));
            else
                result = not ? left.notIn(parseFields(ctx)) : left.in(parseFields(ctx));
            parse(ctx, ')');
            return result;
        } else if (parseKeywordIf(ctx, "BETWEEN")) {
            boolean symmetric = parseKeywordIf(ctx, "SYMMETRIC");
            Field r1 = parseFieldConcat(ctx, null);
            parseKeyword(ctx, "AND");
            Field r2 = parseFieldConcat(ctx, null);
            return symmetric ? not ? left.notBetweenSymmetric(r1, r2) : left.betweenSymmetric(r1, r2) : not ? left.notBetween(r1, r2) : left.between(r1, r2);
        } else if (parseKeywordIf(ctx, "LIKE")) {
            Field right = parseFieldConcat(ctx, null);
            boolean escape = parseKeywordIf(ctx, "ESCAPE");
            char character = escape ? parseCharacterLiteral(ctx) : ' ';
            return escape ? not ? left.notLike(right, character) : left.like(right, character) : not ? left.notLike(right) : left.like(right);
        }
    }
    throw ctx.exception();
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) Comparator(org.jooq.Comparator)

Example 75 with Field

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

the class ParserImpl method parseQueryPrimary.

private static final SelectQueryImpl<Record> parseQueryPrimary(ParserContext ctx) {
    if (parseIf(ctx, '(')) {
        SelectQueryImpl<Record> result = parseSelect(ctx);
        parse(ctx, ')');
        return result;
    }
    parseKeyword(ctx, "SELECT");
    boolean distinct = parseKeywordIf(ctx, "DISTINCT") || parseKeywordIf(ctx, "UNIQUE");
    Long limit = null;
    Long offset = null;
    if (!distinct)
        parseKeywordIf(ctx, "ALL");
    if (parseKeywordIf(ctx, "TOP")) {
        limit = parseUnsignedInteger(ctx);
        if (parseKeywordIf(ctx, "START AT"))
            offset = parseUnsignedInteger(ctx);
    }
    List<Field<?>> select = parseSelectList(ctx);
    List<Table<?>> from = null;
    Condition startWith = null;
    Condition connectBy = null;
    boolean connectByNoCycle = false;
    Condition where = null;
    List<GroupField> groupBy = null;
    Condition having = null;
    if (parseKeywordIf(ctx, "FROM"))
        from = parseTables(ctx);
    // TODO is there a better way?
    if (from != null && from.size() == 1 && from.get(0).getName().equalsIgnoreCase("dual"))
        from = null;
    if (parseKeywordIf(ctx, "START WITH")) {
        startWith = parseCondition(ctx);
        parseKeyword(ctx, "CONNECT BY");
        connectByNoCycle = parseKeywordIf(ctx, "NOCYCLE");
        connectBy = parseCondition(ctx);
    } else if (parseKeywordIf(ctx, "CONNECT BY")) {
        connectByNoCycle = parseKeywordIf(ctx, "NOCYCLE");
        connectBy = parseCondition(ctx);
        if (parseKeywordIf(ctx, "START WITH"))
            startWith = parseCondition(ctx);
    }
    if (parseKeywordIf(ctx, "WHERE"))
        where = parseCondition(ctx);
    if (parseKeywordIf(ctx, "GROUP BY")) {
        if (parseIf(ctx, '(')) {
            parse(ctx, ')');
            groupBy = emptyList();
        } else if (parseKeywordIf(ctx, "ROLLUP")) {
            parse(ctx, '(');
            groupBy = singletonList(rollup(parseFields(ctx).toArray(EMPTY_FIELD)));
            parse(ctx, ')');
        } else if (parseKeywordIf(ctx, "CUBE")) {
            parse(ctx, '(');
            groupBy = singletonList(cube(parseFields(ctx).toArray(EMPTY_FIELD)));
            parse(ctx, ')');
        } else if (parseKeywordIf(ctx, "GROUPING SETS")) {
            List<List<Field<?>>> fieldSets = new ArrayList<List<Field<?>>>();
            parse(ctx, '(');
            do {
                parse(ctx, '(');
                if (parseIf(ctx, ')')) {
                    fieldSets.add(Collections.<Field<?>>emptyList());
                } else {
                    fieldSets.add(parseFields(ctx));
                    parse(ctx, ')');
                }
            } while (parseIf(ctx, ','));
            parse(ctx, ')');
            groupBy = singletonList(groupingSets(fieldSets.toArray((Collection[]) EMPTY_COLLECTION)));
        } else {
            groupBy = (List) parseFields(ctx);
            if (parseKeywordIf(ctx, "WITH ROLLUP"))
                groupBy = singletonList(rollup(groupBy.toArray(EMPTY_FIELD)));
        }
    }
    if (parseKeywordIf(ctx, "HAVING"))
        having = parseCondition(ctx);
    // TODO support WINDOW
    SelectQueryImpl<Record> result = (SelectQueryImpl<Record>) ctx.dsl.selectQuery();
    if (distinct)
        result.setDistinct(distinct);
    if (select.size() > 0)
        result.addSelect(select);
    if (from != null)
        result.addFrom(from);
    if (connectBy != null)
        if (connectByNoCycle)
            result.addConnectByNoCycle(connectBy);
        else
            result.addConnectBy(connectBy);
    if (startWith != null)
        result.setConnectByStartWith(startWith);
    if (where != null)
        result.addConditions(where);
    if (groupBy != null)
        result.addGroupBy(groupBy);
    if (having != null)
        result.addHaving(having);
    if (limit != null)
        if (offset != null)
            result.addLimit((int) (long) offset, (int) (long) limit);
        else
            result.addLimit((int) (long) limit);
    return result;
}
Also used : Condition(org.jooq.Condition) Table(org.jooq.Table) GroupField(org.jooq.GroupField) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) Collection(java.util.Collection) Record(org.jooq.Record) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Aggregations

Field (org.jooq.Field)146 TableField (org.jooq.TableField)93 SortField (org.jooq.SortField)74 GroupField (org.jooq.GroupField)66 SelectField (org.jooq.SelectField)56 Condition (org.jooq.Condition)54 Record (org.jooq.Record)43 ArrayList (java.util.ArrayList)38 Name (org.jooq.Name)31 List (java.util.List)28 Constraint (org.jooq.Constraint)28 Table (org.jooq.Table)28 SQLDialect (org.jooq.SQLDialect)27 Set (java.util.Set)26 Collection (java.util.Collection)25 Context (org.jooq.Context)25 Map (java.util.Map)24 Configuration (org.jooq.Configuration)24 DSLContext (org.jooq.DSLContext)24 Row (org.jooq.Row)24