Search in sources :

Example 1 with Wildcard

use of org.h2.expression.Wildcard in project h2database by h2database.

the class Parser method parseValues.

private Select parseValues() {
    Select command = new Select(session);
    currentSelect = command;
    TableFilter filter = parseValuesTable(0);
    ArrayList<Expression> list = New.arrayList();
    list.add(new Wildcard(null, null));
    command.setExpressions(list);
    command.addTableFilter(filter, true);
    command.init();
    return command;
}
Also used : TableFilter(org.h2.table.TableFilter) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) Wildcard(org.h2.expression.Wildcard) ConditionInSelect(org.h2.expression.ConditionInSelect) Select(org.h2.command.dml.Select)

Example 2 with Wildcard

use of org.h2.expression.Wildcard in project h2database by h2database.

the class Parser method readAggregate.

private Expression readAggregate(AggregateType aggregateType, String aggregateName) {
    if (currentSelect == null) {
        throw getSyntaxError();
    }
    currentSelect.setGroupQuery();
    Aggregate r;
    if (aggregateType == AggregateType.COUNT) {
        if (readIf("*")) {
            r = new Aggregate(AggregateType.COUNT_ALL, null, currentSelect, false);
        } else {
            boolean distinct = readIf("DISTINCT");
            Expression on = readExpression();
            if (on instanceof Wildcard && !distinct) {
                // PostgreSQL compatibility: count(t.*)
                r = new Aggregate(AggregateType.COUNT_ALL, null, currentSelect, false);
            } else {
                r = new Aggregate(AggregateType.COUNT, on, currentSelect, distinct);
            }
        }
    } else if (aggregateType == AggregateType.GROUP_CONCAT) {
        boolean distinct = readIf("DISTINCT");
        if (equalsToken("GROUP_CONCAT", aggregateName)) {
            r = new Aggregate(AggregateType.GROUP_CONCAT, readExpression(), currentSelect, distinct);
            if (readIf("ORDER")) {
                read("BY");
                r.setOrderByList(parseSimpleOrderList());
            }
            if (readIf("SEPARATOR")) {
                r.setGroupConcatSeparator(readExpression());
            }
        } else if (equalsToken("STRING_AGG", aggregateName)) {
            // PostgreSQL compatibility: string_agg(expression, delimiter)
            r = new Aggregate(AggregateType.GROUP_CONCAT, readExpression(), currentSelect, distinct);
            read(",");
            r.setGroupConcatSeparator(readExpression());
            if (readIf("ORDER")) {
                read("BY");
                r.setOrderByList(parseSimpleOrderList());
            }
        } else {
            r = null;
        }
    } else if (aggregateType == AggregateType.ARRAY_AGG) {
        boolean distinct = readIf("DISTINCT");
        r = new Aggregate(AggregateType.ARRAY_AGG, readExpression(), currentSelect, distinct);
        if (readIf("ORDER")) {
            read("BY");
            r.setOrderByList(parseSimpleOrderList());
        }
    } else {
        boolean distinct = readIf("DISTINCT");
        r = new Aggregate(aggregateType, readExpression(), currentSelect, distinct);
    }
    read(")");
    if (r != null && readIf("FILTER")) {
        read("(");
        read("WHERE");
        Expression condition = readExpression();
        read(")");
        r.setFilterCondition(condition);
    }
    return r;
}
Also used : Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) Wildcard(org.h2.expression.Wildcard) UserAggregate(org.h2.engine.UserAggregate) DropAggregate(org.h2.command.ddl.DropAggregate) Aggregate(org.h2.expression.Aggregate) JavaAggregate(org.h2.expression.JavaAggregate) CreateAggregate(org.h2.command.ddl.CreateAggregate)

Example 3 with Wildcard

use of org.h2.expression.Wildcard in project h2database by h2database.

the class Parser method parseSelectSimpleSelectPart.

private void parseSelectSimpleSelectPart(Select command) {
    Select temp = currentSelect;
    // make sure aggregate functions will not work in TOP and LIMIT
    currentSelect = null;
    if (readIf("TOP")) {
        // can't read more complex expressions here because
        // SELECT TOP 1 +? A FROM TEST could mean
        // SELECT TOP (1+?) A FROM TEST or
        // SELECT TOP 1 (+?) AS A FROM TEST
        Expression limit = readTerm().optimize(session);
        command.setLimit(limit);
    } else if (readIf("LIMIT")) {
        Expression offset = readTerm().optimize(session);
        command.setOffset(offset);
        Expression limit = readTerm().optimize(session);
        command.setLimit(limit);
    }
    currentSelect = temp;
    if (readIf("DISTINCT")) {
        command.setDistinct(true);
    } else {
        readIf("ALL");
    }
    ArrayList<Expression> expressions = New.arrayList();
    do {
        if (readIf("*")) {
            expressions.add(new Wildcard(null, null));
        } else {
            Expression expr = readExpression();
            if (readIf("AS") || currentTokenType == IDENTIFIER) {
                String alias = readAliasIdentifier();
                boolean aliasColumnName = database.getSettings().aliasColumnName;
                aliasColumnName |= database.getMode().aliasColumnName;
                expr = new Alias(expr, alias, aliasColumnName);
            }
            expressions.add(expr);
        }
    } while (readIf(","));
    command.setExpressions(expressions);
}
Also used : Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) Wildcard(org.h2.expression.Wildcard) CreateFunctionAlias(org.h2.command.ddl.CreateFunctionAlias) FunctionAlias(org.h2.engine.FunctionAlias) DropFunctionAlias(org.h2.command.ddl.DropFunctionAlias) Alias(org.h2.expression.Alias) ConditionInSelect(org.h2.expression.ConditionInSelect) Select(org.h2.command.dml.Select) ValueString(org.h2.value.ValueString)

Aggregations

Expression (org.h2.expression.Expression)3 ValueExpression (org.h2.expression.ValueExpression)3 Wildcard (org.h2.expression.Wildcard)3 Select (org.h2.command.dml.Select)2 ConditionInSelect (org.h2.expression.ConditionInSelect)2 CreateAggregate (org.h2.command.ddl.CreateAggregate)1 CreateFunctionAlias (org.h2.command.ddl.CreateFunctionAlias)1 DropAggregate (org.h2.command.ddl.DropAggregate)1 DropFunctionAlias (org.h2.command.ddl.DropFunctionAlias)1 FunctionAlias (org.h2.engine.FunctionAlias)1 UserAggregate (org.h2.engine.UserAggregate)1 Aggregate (org.h2.expression.Aggregate)1 Alias (org.h2.expression.Alias)1 JavaAggregate (org.h2.expression.JavaAggregate)1 TableFilter (org.h2.table.TableFilter)1 ValueString (org.h2.value.ValueString)1