Search in sources :

Example 16 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression 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 17 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.

the class Parser method parseTableColumnDefinition.

private void parseTableColumnDefinition(CommandWithColumns command, Schema schema, String tableName) {
    DefineCommand c = parseAlterTableAddConstraintIf(tableName, schema, false);
    if (c != null) {
        command.addConstraintCommand(c);
    } else {
        String columnName = readColumnIdentifier();
        Column column = parseColumnForTable(columnName, true);
        if (column.isAutoIncrement() && column.isPrimaryKey()) {
            column.setPrimaryKey(false);
            IndexColumn[] cols = { new IndexColumn() };
            cols[0].columnName = column.getName();
            AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, false);
            pk.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY);
            pk.setTableName(tableName);
            pk.setIndexColumns(cols);
            command.addConstraintCommand(pk);
        }
        command.addColumn(column);
        String constraintName = null;
        if (readIf("CONSTRAINT")) {
            constraintName = readColumnIdentifier();
        }
        // For compatibility with Apache Ignite.
        boolean allowAffinityKey = database.getMode().allowAffinityKey;
        boolean affinity = allowAffinityKey && readIfAffinity();
        if (readIf("PRIMARY")) {
            read("KEY");
            boolean hash = readIf("HASH");
            IndexColumn[] cols = { new IndexColumn() };
            cols[0].columnName = column.getName();
            AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, false);
            pk.setConstraintName(constraintName);
            pk.setPrimaryKeyHash(hash);
            pk.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY);
            pk.setTableName(tableName);
            pk.setIndexColumns(cols);
            command.addConstraintCommand(pk);
            if (readIf("AUTO_INCREMENT")) {
                parseAutoIncrement(column);
            }
            if (affinity) {
                CreateIndex idx = createAffinityIndex(schema, tableName, cols);
                command.addConstraintCommand(idx);
            }
        } else if (affinity) {
            read("KEY");
            IndexColumn[] cols = { new IndexColumn() };
            cols[0].columnName = column.getName();
            CreateIndex idx = createAffinityIndex(schema, tableName, cols);
            command.addConstraintCommand(idx);
        } else if (readIf("UNIQUE")) {
            AlterTableAddConstraint unique = new AlterTableAddConstraint(session, schema, false);
            unique.setConstraintName(constraintName);
            unique.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE);
            IndexColumn[] cols = { new IndexColumn() };
            cols[0].columnName = columnName;
            unique.setIndexColumns(cols);
            unique.setTableName(tableName);
            command.addConstraintCommand(unique);
        }
        if (NullConstraintType.NULL_IS_NOT_ALLOWED == parseNotNullConstraint()) {
            column.setNullable(false);
        }
        if (readIf("CHECK")) {
            Expression expr = readExpression();
            column.addCheckConstraint(session, expr);
        }
        if (readIf("REFERENCES")) {
            AlterTableAddConstraint ref = new AlterTableAddConstraint(session, schema, false);
            ref.setConstraintName(constraintName);
            ref.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL);
            IndexColumn[] cols = { new IndexColumn() };
            cols[0].columnName = columnName;
            ref.setIndexColumns(cols);
            ref.setTableName(tableName);
            parseReferences(ref, schema, tableName);
            command.addConstraintCommand(ref);
        }
    }
}
Also used : AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) CreateIndex(org.h2.command.ddl.CreateIndex) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) ValueString(org.h2.value.ValueString) DefineCommand(org.h2.command.ddl.DefineCommand) IndexColumn(org.h2.table.IndexColumn)

Example 18 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression 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 19 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.

the class Parser method getDualTable.

private Table getDualTable(boolean noColumns) {
    Schema main = database.findSchema(Constants.SCHEMA_MAIN);
    Expression one = ValueExpression.get(ValueLong.get(1));
    return new RangeTable(main, one, one, noColumns);
}
Also used : Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) RangeTable(org.h2.table.RangeTable)

Example 20 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.

the class Parser method readJoin.

private TableFilter readJoin(TableFilter top) {
    TableFilter last = top;
    while (true) {
        TableFilter join;
        if (readIf("RIGHT")) {
            readIf("OUTER");
            read("JOIN");
            // the right hand side is the 'inner' table usually
            join = readTableFilter();
            join = readJoin(join);
            Expression on = null;
            if (readIf("ON")) {
                on = readExpression();
            }
            addJoin(join, top, true, on);
            top = join;
        } else if (readIf("LEFT")) {
            readIf("OUTER");
            read("JOIN");
            join = readTableFilter();
            join = readJoin(join);
            Expression on = null;
            if (readIf("ON")) {
                on = readExpression();
            }
            addJoin(top, join, true, on);
        } else if (readIf("FULL")) {
            throw getSyntaxError();
        } else if (readIf("INNER")) {
            read("JOIN");
            join = readTableFilter();
            top = readJoin(top);
            Expression on = null;
            if (readIf("ON")) {
                on = readExpression();
            }
            addJoin(top, join, false, on);
        } else if (readIf("JOIN")) {
            join = readTableFilter();
            top = readJoin(top);
            Expression on = null;
            if (readIf("ON")) {
                on = readExpression();
            }
            addJoin(top, join, false, on);
        } else if (readIf("CROSS")) {
            read("JOIN");
            join = readTableFilter();
            addJoin(top, join, false, null);
        } else if (readIf("NATURAL")) {
            read("JOIN");
            join = readTableFilter();
            Column[] tableCols = last.getTable().getColumns();
            Column[] joinCols = join.getTable().getColumns();
            String tableSchema = last.getTable().getSchema().getName();
            String joinSchema = join.getTable().getSchema().getName();
            Expression on = null;
            for (Column tc : tableCols) {
                String tableColumnName = tc.getName();
                for (Column c : joinCols) {
                    String joinColumnName = c.getName();
                    if (equalsToken(tableColumnName, joinColumnName)) {
                        join.addNaturalJoinColumn(c);
                        Expression tableExpr = new ExpressionColumn(database, tableSchema, last.getTableAlias(), tableColumnName);
                        Expression joinExpr = new ExpressionColumn(database, joinSchema, join.getTableAlias(), joinColumnName);
                        Expression equal = new Comparison(session, Comparison.EQUAL, tableExpr, joinExpr);
                        if (on == null) {
                            on = equal;
                        } else {
                            on = new ConditionAndOr(ConditionAndOr.AND, on, equal);
                        }
                    }
                }
            }
            addJoin(top, join, false, on);
        } else {
            break;
        }
        last = join;
    }
    return top;
}
Also used : TableFilter(org.h2.table.TableFilter) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) Comparison(org.h2.expression.Comparison) ValueString(org.h2.value.ValueString) ConditionAndOr(org.h2.expression.ConditionAndOr) ExpressionColumn(org.h2.expression.ExpressionColumn)

Aggregations

Expression (org.h2.expression.Expression)84 ValueExpression (org.h2.expression.ValueExpression)53 Column (org.h2.table.Column)45 ExpressionColumn (org.h2.expression.ExpressionColumn)38 IndexColumn (org.h2.table.IndexColumn)24 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)21 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)18 ValueString (org.h2.value.ValueString)18 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)17 TableFilter (org.h2.table.TableFilter)15 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)14 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)14 Expression (expression.Expression)13 Table (org.h2.table.Table)13 Value (org.h2.value.Value)12 ArrayList (java.util.ArrayList)11 Node (expression.Node)10 RangeTable (org.h2.table.RangeTable)10 Number (expression.Number)9 CreateTable (org.h2.command.ddl.CreateTable)9