Search in sources :

Example 6 with QueryFlag

use of com.querydsl.core.QueryFlag in project querydsl by querydsl.

the class SerializerBase method serialize.

public final boolean serialize(final QueryFlag.Position position, final Set<QueryFlag> flags) {
    boolean handled = false;
    for (final QueryFlag flag : flags) {
        if (flag.getPosition() == position) {
            handle(flag.getFlag());
            handled = true;
        }
    }
    return handled;
}
Also used : QueryFlag(com.querydsl.core.QueryFlag)

Example 7 with QueryFlag

use of com.querydsl.core.QueryFlag in project querydsl by querydsl.

the class SQLServerTemplates method serialize.

@Override
public void serialize(QueryMetadata metadata, boolean forCountRow, SQLSerializer context) {
    if (!forCountRow && metadata.getModifiers().isRestricting() && !metadata.getJoins().isEmpty()) {
        QueryModifiers mod = metadata.getModifiers();
        if (mod.getOffset() == null) {
            // select top ...
            metadata = metadata.clone();
            metadata.addFlag(new QueryFlag(QueryFlag.Position.AFTER_SELECT, Expressions.template(Integer.class, topTemplate, mod.getLimit())));
            context.serializeForQuery(metadata, forCountRow);
        } else {
            throw new IllegalStateException("offset not supported");
        }
    } else {
        context.serializeForQuery(metadata, forCountRow);
    }
    if (!metadata.getFlags().isEmpty()) {
        context.serialize(Position.END, metadata.getFlags());
    }
}
Also used : QueryModifiers(com.querydsl.core.QueryModifiers) QueryFlag(com.querydsl.core.QueryFlag)

Example 8 with QueryFlag

use of com.querydsl.core.QueryFlag in project querydsl by querydsl.

the class OracleTemplates method serializeInsert.

@Override
public void serializeInsert(QueryMetadata metadata, RelationalPath<?> entity, List<SQLInsertBatch> batches, SQLSerializer context) {
    context.append(bulkInsertTemplate);
    metadata.addFlag(new QueryFlag(Position.START_OVERRIDE, bulkInsertSeparator));
    for (SQLInsertBatch batch : batches) {
        serializeInsert(metadata, entity, batch.getColumns(), batch.getValues(), batch.getSubQuery(), context);
    }
    context.append(" select * from dual");
}
Also used : QueryFlag(com.querydsl.core.QueryFlag) SQLInsertBatch(com.querydsl.sql.dml.SQLInsertBatch)

Example 9 with QueryFlag

use of com.querydsl.core.QueryFlag in project querydsl by querydsl.

the class SQLSerializer method serializeUnion.

public void serializeUnion(Expression<?> union, QueryMetadata metadata, boolean unionAll) {
    final List<? extends Expression<?>> groupBy = metadata.getGroupBy();
    final Predicate having = metadata.getHaving();
    final List<OrderSpecifier<?>> orderBy = metadata.getOrderBy();
    final Set<QueryFlag> flags = metadata.getFlags();
    final boolean hasFlags = !flags.isEmpty();
    // with
    if (hasFlags) {
        boolean handled = false;
        boolean recursive = false;
        for (QueryFlag flag : flags) {
            if (flag.getPosition() == Position.WITH) {
                if (flag.getFlag() == SQLTemplates.RECURSIVE) {
                    recursive = true;
                    continue;
                }
                if (handled) {
                    append(", ");
                }
                handle(flag.getFlag());
                handled = true;
            }
        }
        if (handled) {
            if (recursive) {
                prepend(templates.getWithRecursive());
            } else {
                prepend(templates.getWith());
            }
            append("\n");
        }
    }
    // union
    Stage oldStage = stage;
    handle(union);
    // group by
    if (hasFlags) {
        serialize(Position.BEFORE_GROUP_BY, flags);
    }
    if (!groupBy.isEmpty()) {
        stage = Stage.GROUP_BY;
        append(templates.getGroupBy()).handle(COMMA, groupBy);
    }
    if (hasFlags) {
        serialize(Position.AFTER_GROUP_BY, flags);
    }
    // having
    if (hasFlags) {
        serialize(Position.BEFORE_HAVING, flags);
    }
    if (having != null) {
        stage = Stage.HAVING;
        append(templates.getHaving()).handle(having);
    }
    if (hasFlags) {
        serialize(Position.AFTER_HAVING, flags);
    }
    // order by
    if (hasFlags) {
        serialize(Position.BEFORE_ORDER, flags);
    }
    if (!orderBy.isEmpty()) {
        stage = Stage.ORDER_BY;
        append(templates.getOrderBy());
        skipParent = true;
        handleOrderBy(orderBy);
        skipParent = false;
    }
    if (hasFlags) {
        serialize(Position.AFTER_ORDER, flags);
    }
    // end
    if (hasFlags) {
        serialize(Position.END, flags);
    }
    // reset stage
    stage = oldStage;
}
Also used : QueryFlag(com.querydsl.core.QueryFlag)

Example 10 with QueryFlag

use of com.querydsl.core.QueryFlag in project querydsl by querydsl.

the class SQLSerializer method serializeForQuery.

void serializeForQuery(QueryMetadata metadata, boolean forCountRow) {
    boolean oldInSubquery = inSubquery;
    inSubquery = inSubquery || getLength() > 0;
    boolean oldSkipParent = skipParent;
    skipParent = false;
    final Expression<?> select = metadata.getProjection();
    final List<JoinExpression> joins = metadata.getJoins();
    final Predicate where = metadata.getWhere();
    final List<? extends Expression<?>> groupBy = metadata.getGroupBy();
    final Predicate having = metadata.getHaving();
    final List<OrderSpecifier<?>> orderBy = metadata.getOrderBy();
    final Set<QueryFlag> flags = metadata.getFlags();
    final boolean hasFlags = !flags.isEmpty();
    String suffix = null;
    List<? extends Expression<?>> sqlSelect;
    if (select instanceof FactoryExpression) {
        sqlSelect = ((FactoryExpression<?>) select).getArgs();
    } else if (select != null) {
        sqlSelect = ImmutableList.of(select);
    } else {
        sqlSelect = ImmutableList.of();
    }
    // with
    if (hasFlags) {
        List<Expression<?>> withFlags = Lists.newArrayList();
        boolean recursive = false;
        for (QueryFlag flag : flags) {
            if (flag.getPosition() == Position.WITH) {
                if (flag.getFlag() == SQLTemplates.RECURSIVE) {
                    recursive = true;
                    continue;
                }
                withFlags.add(flag.getFlag());
            }
        }
        if (!withFlags.isEmpty()) {
            if (recursive) {
                append(templates.getWithRecursive());
            } else {
                append(templates.getWith());
            }
            handle(", ", withFlags);
            append("\n");
        }
    }
    // start
    if (hasFlags) {
        serialize(Position.START, flags);
    }
    // select
    Stage oldStage = stage;
    stage = Stage.SELECT;
    if (forCountRow) {
        append(templates.getSelect());
        if (hasFlags) {
            serialize(Position.AFTER_SELECT, flags);
        }
        if (!metadata.isDistinct()) {
            append(templates.getCountStar());
            if (!groupBy.isEmpty()) {
                append(templates.getFrom());
                append("(");
                append(templates.getSelect());
                append("1 as one ");
                suffix = ") internal";
            }
        } else {
            List<? extends Expression<?>> columns;
            if (sqlSelect.isEmpty()) {
                columns = getIdentifierColumns(joins, !templates.isCountDistinctMultipleColumns());
            } else {
                columns = sqlSelect;
            }
            if (!groupBy.isEmpty()) {
                // select count(*) from (select distinct ...)
                append(templates.getCountStar());
                append(templates.getFrom());
                append("(");
                append(templates.getSelectDistinct());
                handleSelect(COMMA, columns);
                suffix = ") internal";
            } else if (columns.size() == 1) {
                append(templates.getDistinctCountStart());
                handle(columns.get(0));
                append(templates.getDistinctCountEnd());
            } else if (templates.isCountDistinctMultipleColumns()) {
                append(templates.getDistinctCountStart());
                append("(").handleSelect(COMMA, columns).append(")");
                append(templates.getDistinctCountEnd());
            } else {
                // select count(*) from (select distinct ...)
                append(templates.getCountStar());
                append(templates.getFrom());
                append("(");
                append(templates.getSelectDistinct());
                handleSelect(COMMA, columns);
                suffix = ") internal";
            }
        }
    } else if (!sqlSelect.isEmpty()) {
        if (!metadata.isDistinct()) {
            append(templates.getSelect());
        } else {
            append(templates.getSelectDistinct());
        }
        if (hasFlags) {
            serialize(Position.AFTER_SELECT, flags);
        }
        handleSelect(COMMA, sqlSelect);
    }
    if (hasFlags) {
        serialize(Position.AFTER_PROJECTION, flags);
    }
    // from
    stage = Stage.FROM;
    serializeSources(joins);
    // where
    if (hasFlags) {
        serialize(Position.BEFORE_FILTERS, flags);
    }
    if (where != null) {
        stage = Stage.WHERE;
        append(templates.getWhere()).handle(where);
    }
    if (hasFlags) {
        serialize(Position.AFTER_FILTERS, flags);
    }
    // group by
    if (hasFlags) {
        serialize(Position.BEFORE_GROUP_BY, flags);
    }
    if (!groupBy.isEmpty()) {
        stage = Stage.GROUP_BY;
        append(templates.getGroupBy()).handle(COMMA, groupBy);
    }
    if (hasFlags) {
        serialize(Position.AFTER_GROUP_BY, flags);
    }
    // having
    if (hasFlags) {
        serialize(Position.BEFORE_HAVING, flags);
    }
    if (having != null) {
        stage = Stage.HAVING;
        append(templates.getHaving()).handle(having);
    }
    if (hasFlags) {
        serialize(Position.AFTER_HAVING, flags);
    }
    // order by
    if (hasFlags) {
        serialize(Position.BEFORE_ORDER, flags);
    }
    if (!orderBy.isEmpty() && !forCountRow) {
        stage = Stage.ORDER_BY;
        append(templates.getOrderBy());
        handleOrderBy(orderBy);
    }
    if (hasFlags) {
        serialize(Position.AFTER_ORDER, flags);
    }
    // modifiers
    if (!forCountRow && metadata.getModifiers().isRestricting() && !joins.isEmpty()) {
        stage = Stage.MODIFIERS;
        templates.serializeModifiers(metadata, this);
    }
    if (suffix != null) {
        append(suffix);
    }
    // reset stage
    stage = oldStage;
    skipParent = oldSkipParent;
    inSubquery = oldInSubquery;
}
Also used : QueryFlag(com.querydsl.core.QueryFlag) JoinExpression(com.querydsl.core.JoinExpression) JoinExpression(com.querydsl.core.JoinExpression)

Aggregations

QueryFlag (com.querydsl.core.QueryFlag)12 QueryModifiers (com.querydsl.core.QueryModifiers)7 JoinExpression (com.querydsl.core.JoinExpression)1 SQLInsertBatch (com.querydsl.sql.dml.SQLInsertBatch)1 Test (org.junit.Test)1