Search in sources :

Example 1 with SelectOrderBy

use of org.h2.command.dml.SelectOrderBy in project h2database by h2database.

the class Parser method parseEndOfQuery.

private void parseEndOfQuery(Query command) {
    if (readIf("ORDER")) {
        read("BY");
        Select oldSelect = currentSelect;
        if (command instanceof Select) {
            currentSelect = (Select) command;
        }
        ArrayList<SelectOrderBy> orderList = New.arrayList();
        do {
            boolean canBeNumber = true;
            if (readIf("=")) {
                canBeNumber = false;
            }
            SelectOrderBy order = new SelectOrderBy();
            Expression expr = readExpression();
            if (canBeNumber && expr instanceof ValueExpression && expr.getType() == Value.INT) {
                order.columnIndexExpr = expr;
            } else if (expr instanceof Parameter) {
                recompileAlways = true;
                order.columnIndexExpr = expr;
            } else {
                order.expression = expr;
            }
            if (readIf("DESC")) {
                order.descending = true;
            } else {
                readIf("ASC");
            }
            if (readIf("NULLS")) {
                if (readIf("FIRST")) {
                    order.nullsFirst = true;
                } else {
                    read("LAST");
                    order.nullsLast = true;
                }
            }
            orderList.add(order);
        } while (readIf(","));
        command.setOrder(orderList);
        currentSelect = oldSelect;
    }
    // make sure aggregate functions will not work here
    Select temp = currentSelect;
    currentSelect = null;
    // http://sqlpro.developpez.com/SQL2008/
    if (readIf("OFFSET")) {
        command.setOffset(readExpression().optimize(session));
        if (!readIf("ROW")) {
            readIf("ROWS");
        }
    }
    if (readIf("FETCH")) {
        if (!readIf("FIRST")) {
            read("NEXT");
        }
        if (readIf("ROW")) {
            command.setLimit(ValueExpression.get(ValueInt.get(1)));
        } else {
            Expression limit = readExpression().optimize(session);
            command.setLimit(limit);
            if (!readIf("ROW")) {
                read("ROWS");
            }
        }
        read("ONLY");
    }
    currentSelect = temp;
    if (readIf("LIMIT")) {
        temp = currentSelect;
        // make sure aggregate functions will not work here
        currentSelect = null;
        Expression limit = readExpression().optimize(session);
        command.setLimit(limit);
        if (readIf("OFFSET")) {
            Expression offset = readExpression().optimize(session);
            command.setOffset(offset);
        } else if (readIf(",")) {
            // MySQL: [offset, ] rowcount
            Expression offset = limit;
            limit = readExpression().optimize(session);
            command.setOffset(offset);
            command.setLimit(limit);
        }
        if (readIf("SAMPLE_SIZE")) {
            Expression sampleSize = readExpression().optimize(session);
            command.setSampleSize(sampleSize);
        }
        currentSelect = temp;
    }
    if (readIf("FOR")) {
        if (readIf("UPDATE")) {
            if (readIf("OF")) {
                do {
                    readIdentifierWithSchema();
                } while (readIf(","));
            } else if (readIf("NOWAIT")) {
            // TODO parser: select for update nowait: should not wait
            }
            command.setForUpdate(true);
        } else if (readIf("READ") || readIf("FETCH")) {
            read("ONLY");
        }
    }
    if (database.getMode().isolationLevelInSelectOrInsertStatement) {
        parseIsolationClause();
    }
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) ValueExpression(org.h2.expression.ValueExpression) ConditionInSelect(org.h2.expression.ConditionInSelect) Select(org.h2.command.dml.Select) Parameter(org.h2.expression.Parameter) ConditionInParameter(org.h2.expression.ConditionInParameter)

Example 2 with SelectOrderBy

use of org.h2.command.dml.SelectOrderBy in project h2database by h2database.

the class Parser method parseSimpleOrderList.

private ArrayList<SelectOrderBy> parseSimpleOrderList() {
    ArrayList<SelectOrderBy> orderList = New.arrayList();
    do {
        SelectOrderBy order = new SelectOrderBy();
        order.expression = readExpression();
        if (readIf("DESC")) {
            order.descending = true;
        } else {
            readIf("ASC");
        }
        orderList.add(order);
    } while (readIf(","));
    return orderList;
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy)

Example 3 with SelectOrderBy

use of org.h2.command.dml.SelectOrderBy in project h2database by h2database.

the class Aggregate method getSQLArrayAggregate.

private String getSQLArrayAggregate() {
    StatementBuilder buff = new StatementBuilder("ARRAY_AGG(");
    if (distinct) {
        buff.append("DISTINCT ");
    }
    buff.append(on.getSQL());
    if (orderByList != null) {
        buff.append(" ORDER BY ");
        for (SelectOrderBy o : orderByList) {
            buff.appendExceptFirst(", ");
            buff.append(o.expression.getSQL());
            if (o.descending) {
                buff.append(" DESC");
            }
        }
    }
    buff.append(')');
    if (filterCondition != null) {
        buff.append(" FILTER (WHERE ").append(filterCondition.getSQL()).append(')');
    }
    return buff.toString();
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) StatementBuilder(org.h2.util.StatementBuilder)

Example 4 with SelectOrderBy

use of org.h2.command.dml.SelectOrderBy in project h2database by h2database.

the class Aggregate method updateAggregate.

@Override
public void updateAggregate(Session session) {
    // TODO aggregates: check nested MIN(MAX(ID)) and so on
    // if (on != null) {
    // on.updateAggregate();
    // }
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
        // this is a different level (the enclosing query)
        return;
    }
    int groupRowId = select.getCurrentGroupRowId();
    if (lastGroupRowId == groupRowId) {
        // already visited
        return;
    }
    lastGroupRowId = groupRowId;
    AggregateData data = (AggregateData) group.get(this);
    if (data == null) {
        data = AggregateData.create(type);
        group.put(this, data);
    }
    Value v = on == null ? null : on.getValue(session);
    if (type == AggregateType.GROUP_CONCAT) {
        if (v != ValueNull.INSTANCE) {
            v = v.convertTo(Value.STRING);
            if (orderByList != null) {
                int size = orderByList.size();
                Value[] array = new Value[1 + size];
                array[0] = v;
                for (int i = 0; i < size; i++) {
                    SelectOrderBy o = orderByList.get(i);
                    array[i + 1] = o.expression.getValue(session);
                }
                v = ValueArray.get(array);
            }
        }
    }
    if (type == AggregateType.ARRAY_AGG) {
        if (v != ValueNull.INSTANCE) {
            if (orderByList != null) {
                int size = orderByList.size();
                Value[] array = new Value[1 + size];
                array[0] = v;
                for (int i = 0; i < size; i++) {
                    SelectOrderBy o = orderByList.get(i);
                    array[i + 1] = o.expression.getValue(session);
                }
                v = ValueArray.get(array);
            }
        }
    }
    if (filterCondition != null) {
        if (!filterCondition.getBooleanValue(session)) {
            return;
        }
    }
    data.add(session.getDatabase(), dataType, distinct, v);
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) Value(org.h2.value.Value)

Example 5 with SelectOrderBy

use of org.h2.command.dml.SelectOrderBy in project h2database by h2database.

the class Aggregate method initOrder.

private SortOrder initOrder(Session session) {
    int size = orderByList.size();
    int[] index = new int[size];
    int[] sortType = new int[size];
    for (int i = 0; i < size; i++) {
        SelectOrderBy o = orderByList.get(i);
        index[i] = i + 1;
        int order = o.descending ? SortOrder.DESCENDING : SortOrder.ASCENDING;
        sortType[i] = order;
    }
    return new SortOrder(session.getDatabase(), index, sortType, null);
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) SortOrder(org.h2.result.SortOrder)

Aggregations

SelectOrderBy (org.h2.command.dml.SelectOrderBy)8 Expression (org.h2.expression.Expression)4 ValueExpression (org.h2.expression.ValueExpression)3 ExpressionColumn (org.h2.expression.ExpressionColumn)2 SortOrder (org.h2.result.SortOrder)2 StatementBuilder (org.h2.util.StatementBuilder)2 Value (org.h2.value.Value)2 Select (org.h2.command.dml.Select)1 Database (org.h2.engine.Database)1 Alias (org.h2.expression.Alias)1 ConditionInParameter (org.h2.expression.ConditionInParameter)1 ConditionInSelect (org.h2.expression.ConditionInSelect)1 Parameter (org.h2.expression.Parameter)1 TableFilter (org.h2.table.TableFilter)1