Search in sources :

Example 76 with Expression

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

the class Parser method parseUpdateSetClause.

private void parseUpdateSetClause(Update command, TableFilter filter, int start) {
    read("SET");
    if (readIf("(")) {
        ArrayList<Column> columns = New.arrayList();
        do {
            Column column = readTableColumn(filter);
            columns.add(column);
        } while (readIfMore(true));
        read("=");
        Expression expression = readExpression();
        if (columns.size() == 1) {
            // the expression is parsed as a simple value
            command.setAssignment(columns.get(0), expression);
        } else {
            for (int i = 0, size = columns.size(); i < size; i++) {
                Column column = columns.get(i);
                Function f = Function.getFunction(database, "ARRAY_GET");
                f.setParameter(0, expression);
                f.setParameter(1, ValueExpression.get(ValueInt.get(i + 1)));
                f.doneWithParameters();
                command.setAssignment(column, f);
            }
        }
    } else {
        do {
            Column column = readTableColumn(filter);
            read("=");
            Expression expression;
            if (readIf("DEFAULT")) {
                expression = ValueExpression.getDefault();
            } else {
                expression = readExpression();
            }
            command.setAssignment(column, expression);
        } while (readIf(","));
    }
    if (readIf("WHERE")) {
        Expression condition = readExpression();
        command.setCondition(condition);
    }
    if (readIf("ORDER")) {
        // for MySQL compatibility
        // (this syntax is supported, but ignored)
        read("BY");
        parseSimpleOrderList();
    }
    if (readIf("LIMIT")) {
        Expression limit = readTerm().optimize(session);
        command.setLimit(limit);
    }
    setSQL(command, "UPDATE", start);
}
Also used : Function(org.h2.expression.Function) TableFunction(org.h2.expression.TableFunction) JavaFunction(org.h2.expression.JavaFunction) 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) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 77 with Expression

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

the class Parser method readTermObjectDot.

private Expression readTermObjectDot(String objectName) {
    Expression expr = readWildcardOrSequenceValue(null, objectName);
    if (expr != null) {
        return expr;
    }
    String name = readColumnIdentifier();
    Schema s = database.findSchema(objectName);
    if ((!SysProperties.OLD_STYLE_OUTER_JOIN || s != null) && readIf("(")) {
        // if the old style outer joins are not supported
        return readFunction(s, name);
    } else if (readIf(".")) {
        String schema = objectName;
        objectName = name;
        expr = readWildcardOrSequenceValue(schema, objectName);
        if (expr != null) {
            return expr;
        }
        name = readColumnIdentifier();
        if (readIf("(")) {
            String databaseName = schema;
            if (!equalsToken(database.getShortName(), databaseName)) {
                throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, databaseName);
            }
            schema = objectName;
            return readFunction(database.getSchema(schema), name);
        } else if (readIf(".")) {
            String databaseName = schema;
            if (!equalsToken(database.getShortName(), databaseName)) {
                throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, databaseName);
            }
            schema = objectName;
            objectName = name;
            expr = readWildcardOrSequenceValue(schema, objectName);
            if (expr != null) {
                return expr;
            }
            name = readColumnIdentifier();
            return new ExpressionColumn(database, schema, objectName, name);
        }
        return new ExpressionColumn(database, schema, objectName, name);
    }
    return new ExpressionColumn(database, null, objectName, name);
}
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) ValueString(org.h2.value.ValueString) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 78 with Expression

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

the class Parser method parseJoinTableFilter.

private void parseJoinTableFilter(TableFilter top, final Select command) {
    top = readJoin(top);
    command.addTableFilter(top, true);
    boolean isOuter = false;
    while (true) {
        TableFilter n = top.getNestedJoin();
        if (n != null) {
            n.visit(new TableFilterVisitor() {

                @Override
                public void accept(TableFilter f) {
                    command.addTableFilter(f, false);
                }
            });
        }
        TableFilter join = top.getJoin();
        if (join == null) {
            break;
        }
        isOuter = isOuter | join.isJoinOuter();
        if (isOuter) {
            command.addTableFilter(join, false);
        } else {
            // make flat so the optimizer can work better
            Expression on = join.getJoinCondition();
            if (on != null) {
                command.addCondition(on);
            }
            join.removeJoinCondition();
            top.removeJoin();
            command.addTableFilter(join, true);
        }
        top = join;
    }
}
Also used : TableFilter(org.h2.table.TableFilter) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) TableFilterVisitor(org.h2.table.TableFilter.TableFilterVisitor)

Example 79 with Expression

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

the class Parser method parseSelectSimple.

private Select parseSelectSimple() {
    boolean fromFirst;
    if (readIf("SELECT")) {
        fromFirst = false;
    } else if (readIf("FROM")) {
        fromFirst = true;
    } else {
        throw getSyntaxError();
    }
    Select command = new Select(session);
    int start = lastParseIndex;
    Select oldSelect = currentSelect;
    currentSelect = command;
    currentPrepared = command;
    if (fromFirst) {
        parseSelectSimpleFromPart(command);
        read("SELECT");
        parseSelectSimpleSelectPart(command);
    } else {
        parseSelectSimpleSelectPart(command);
        if (!readIf("FROM")) {
            // select without FROM: convert to SELECT ... FROM
            // SYSTEM_RANGE(1,1)
            Table dual = getDualTable(false);
            TableFilter filter = new TableFilter(session, dual, null, rightsChecked, currentSelect, 0, null);
            command.addTableFilter(filter, true);
        } else {
            parseSelectSimpleFromPart(command);
        }
    }
    if (readIf("WHERE")) {
        Expression condition = readExpression();
        command.addCondition(condition);
    }
    // the group by is read for the outer select (or not a select)
    // so that columns that are not grouped can be used
    currentSelect = oldSelect;
    if (readIf("GROUP")) {
        read("BY");
        command.setGroupQuery();
        ArrayList<Expression> list = New.arrayList();
        do {
            Expression expr = readExpression();
            list.add(expr);
        } while (readIf(","));
        command.setGroupBy(list);
    }
    currentSelect = command;
    if (readIf("HAVING")) {
        command.setGroupQuery();
        Expression condition = readExpression();
        command.setHaving(condition);
    }
    command.setParameterList(parameters);
    currentSelect = oldSelect;
    setSQL(command, "SELECT", start);
    return command;
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) TableFilter(org.h2.table.TableFilter) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) ConditionInSelect(org.h2.expression.ConditionInSelect) Select(org.h2.command.dml.Select) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 80 with Expression

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

the class Parser method parseValuesTable.

private TableFilter parseValuesTable(int orderInFrom) {
    Schema mainSchema = database.getSchema(Constants.SCHEMA_MAIN);
    TableFunction tf = (TableFunction) Function.getFunction(database, "TABLE");
    ArrayList<Column> columns = New.arrayList();
    ArrayList<ArrayList<Expression>> rows = New.arrayList();
    do {
        int i = 0;
        ArrayList<Expression> row = New.arrayList();
        boolean multiColumn = readIf("(");
        do {
            Expression expr = readExpression();
            expr = expr.optimize(session);
            int type = expr.getType();
            long prec;
            int scale, displaySize;
            Column column;
            String columnName = "C" + (i + 1);
            if (rows.isEmpty()) {
                if (type == Value.UNKNOWN) {
                    type = Value.STRING;
                }
                DataType dt = DataType.getDataType(type);
                prec = dt.defaultPrecision;
                scale = dt.defaultScale;
                displaySize = dt.defaultDisplaySize;
                column = new Column(columnName, type, prec, scale, displaySize);
                columns.add(column);
            }
            prec = expr.getPrecision();
            scale = expr.getScale();
            displaySize = expr.getDisplaySize();
            if (i >= columns.size()) {
                throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
            }
            Column c = columns.get(i);
            type = Value.getHigherOrder(c.getType(), type);
            prec = Math.max(c.getPrecision(), prec);
            scale = Math.max(c.getScale(), scale);
            displaySize = Math.max(c.getDisplaySize(), displaySize);
            column = new Column(columnName, type, prec, scale, displaySize);
            columns.set(i, column);
            row.add(expr);
            i++;
        } while (multiColumn && readIfMore(true));
        rows.add(row);
    } while (readIf(","));
    int columnCount = columns.size();
    int rowCount = rows.size();
    for (ArrayList<Expression> row : rows) {
        if (row.size() != columnCount) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    for (int i = 0; i < columnCount; i++) {
        Column c = columns.get(i);
        if (c.getType() == Value.UNKNOWN) {
            c = new Column(c.getName(), Value.STRING, 0, 0, 0);
            columns.set(i, c);
        }
        Expression[] array = new Expression[rowCount];
        for (int j = 0; j < rowCount; j++) {
            array[j] = rows.get(j).get(i);
        }
        ExpressionList list = new ExpressionList(array);
        tf.setParameter(i, list);
    }
    tf.setColumns(columns);
    tf.doneWithParameters();
    Table table = new FunctionTable(mainSchema, session, tf, tf);
    return new TableFilter(session, table, null, rightsChecked, currentSelect, orderInFrom, null);
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ArrayList(java.util.ArrayList) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) 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) FunctionTable(org.h2.table.FunctionTable) TableFilter(org.h2.table.TableFilter) TableFunction(org.h2.expression.TableFunction) CreateUserDataType(org.h2.command.ddl.CreateUserDataType) DataType(org.h2.value.DataType) DropUserDataType(org.h2.command.ddl.DropUserDataType) UserDataType(org.h2.engine.UserDataType) ExpressionList(org.h2.expression.ExpressionList)

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