Search in sources :

Example 21 with Expression

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

the class Parser method parseReplace.

/**
 * MySQL compatibility. REPLACE is similar to MERGE.
 */
private Replace parseReplace() {
    Replace command = new Replace(session);
    currentPrepared = command;
    read("INTO");
    Table table = readTableOrView();
    command.setTable(table);
    if (readIf("(")) {
        if (isSelect()) {
            command.setQuery(parseSelect());
            read(")");
            return command;
        }
        Column[] columns = parseColumnList(table);
        command.setColumns(columns);
    }
    if (readIf("VALUES")) {
        do {
            ArrayList<Expression> values = New.arrayList();
            read("(");
            if (!readIf(")")) {
                do {
                    if (readIf("DEFAULT")) {
                        values.add(null);
                    } else {
                        values.add(readExpression());
                    }
                } while (readIfMore(false));
            }
            command.addRow(values.toArray(new Expression[0]));
        } while (readIf(","));
    } else {
        command.setQuery(parseSelect());
    }
    return command;
}
Also used : Replace(org.h2.command.dml.Replace) 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) 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)

Example 22 with Expression

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

the class Parser method parseDelete.

private Delete parseDelete() {
    Delete command = new Delete(session);
    Expression limit = null;
    if (readIf("TOP")) {
        limit = readTerm().optimize(session);
    }
    currentPrepared = command;
    int start = lastParseIndex;
    if (!readIf("FROM") && database.getMode().getEnum() == ModeEnum.MySQL) {
        readIdentifierWithSchema();
        read("FROM");
    }
    TableFilter filter = readSimpleTableFilter(0, null);
    command.setTableFilter(filter);
    parseDeleteGivenTable(command, limit, start);
    return command;
}
Also used : Delete(org.h2.command.dml.Delete) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) TableFilter(org.h2.table.TableFilter) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 23 with Expression

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

the class Parser method parseInsert.

private Insert parseInsert() {
    Insert command = new Insert(session);
    currentPrepared = command;
    if (database.getMode().onDuplicateKeyUpdate && readIf("IGNORE")) {
        command.setIgnore(true);
    }
    read("INTO");
    Table table = readTableOrView();
    command.setTable(table);
    Insert returnedCommand = parseInsertGivenTable(command, table);
    if (returnedCommand != null) {
        return returnedCommand;
    }
    if (database.getMode().onDuplicateKeyUpdate) {
        if (readIf("ON")) {
            read("DUPLICATE");
            read("KEY");
            read("UPDATE");
            do {
                String columnName = readColumnIdentifier();
                if (readIf(".")) {
                    String schemaOrTableName = columnName;
                    String tableOrColumnName = readColumnIdentifier();
                    if (readIf(".")) {
                        if (!table.getSchema().getName().equals(schemaOrTableName)) {
                            throw DbException.get(ErrorCode.SCHEMA_NAME_MUST_MATCH);
                        }
                        columnName = readColumnIdentifier();
                    } else {
                        columnName = tableOrColumnName;
                        tableOrColumnName = schemaOrTableName;
                    }
                    if (!table.getName().equals(tableOrColumnName)) {
                        throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableOrColumnName);
                    }
                }
                Column column = table.getColumn(columnName);
                read("=");
                Expression expression;
                if (readIf("DEFAULT")) {
                    expression = ValueExpression.getDefault();
                } else {
                    expression = readExpression();
                }
                command.addAssignmentForDuplicate(column, expression);
            } while (readIf(","));
        }
    }
    if (database.getMode().isolationLevelInSelectOrInsertStatement) {
        parseIsolationClause();
    }
    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) 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) Insert(org.h2.command.dml.Insert)

Example 24 with Expression

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

the class Parser method readCase.

private Expression readCase() {
    if (readIf("END")) {
        readIf("CASE");
        return ValueExpression.getNull();
    }
    if (readIf("ELSE")) {
        Expression elsePart = readExpression().optimize(session);
        read("END");
        readIf("CASE");
        return elsePart;
    }
    int i;
    Function function;
    if (readIf("WHEN")) {
        function = Function.getFunction(database, "CASE");
        function.setParameter(0, null);
        i = 1;
        do {
            function.setParameter(i++, readExpression());
            read("THEN");
            function.setParameter(i++, readExpression());
        } while (readIf("WHEN"));
    } else {
        Expression expr = readExpression();
        if (readIf("END")) {
            readIf("CASE");
            return ValueExpression.getNull();
        }
        if (readIf("ELSE")) {
            Expression elsePart = readExpression().optimize(session);
            read("END");
            readIf("CASE");
            return elsePart;
        }
        function = Function.getFunction(database, "CASE");
        function.setParameter(0, expr);
        i = 1;
        read("WHEN");
        do {
            function.setParameter(i++, readExpression());
            read("THEN");
            function.setParameter(i++, readExpression());
        } while (readIf("WHEN"));
    }
    if (readIf("ELSE")) {
        function.setParameter(i, readExpression());
    }
    read("END");
    readIf("CASE");
    function.doneWithParameters();
    return function;
}
Also used : Function(org.h2.expression.Function) TableFunction(org.h2.expression.TableFunction) JavaFunction(org.h2.expression.JavaFunction) 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 25 with Expression

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

the class AlterTableRenameColumn method update.

@Override
public int update() {
    session.commit(true);
    Database db = session.getDatabase();
    Table table = getSchema().findTableOrView(session, tableName);
    if (table == null) {
        if (ifTableExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
    }
    Column column = table.getColumn(oldName);
    session.getUser().checkRight(table, Right.ALL);
    table.checkSupportAlter();
    // we need to update CHECK constraint
    // since it might reference the name of the column
    Expression newCheckExpr = column.getCheckConstraint(session, newName);
    table.renameColumn(column, newName);
    column.removeCheckConstraint();
    column.addCheckConstraint(session, newCheckExpr);
    table.setModified();
    db.updateMeta(session, table);
    for (DbObject child : table.getChildren()) {
        if (child.getCreateSQL() != null) {
            db.updateMeta(session, child);
        }
    }
    return 0;
}
Also used : Table(org.h2.table.Table) Column(org.h2.table.Column) Expression(org.h2.expression.Expression) DbObject(org.h2.engine.DbObject) Database(org.h2.engine.Database)

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