Search in sources :

Example 86 with Command

use of org.h2.command.Command in project h2database by h2database.

the class Parser method prepareCommand.

/**
 * Parse a statement or a list of statements, and prepare it for execution.
 *
 * @param sql the SQL statement to parse
 * @return the command object
 */
public Command prepareCommand(String sql) {
    try {
        Prepared p = parse(sql);
        boolean hasMore = isToken(";");
        if (!hasMore && currentTokenType != END) {
            throw getSyntaxError();
        }
        p.prepare();
        Command c = new CommandContainer(this, sql, p);
        if (hasMore) {
            String remaining = originalSQL.substring(parseIndex);
            if (remaining.trim().length() != 0) {
                c = new CommandList(this, sql, c, remaining);
            }
        }
        return c;
    } catch (DbException e) {
        throw e.addSQL(originalSQL);
    }
}
Also used : RunScriptCommand(org.h2.command.dml.RunScriptCommand) TransactionCommand(org.h2.command.dml.TransactionCommand) ScriptCommand(org.h2.command.dml.ScriptCommand) SchemaCommand(org.h2.command.ddl.SchemaCommand) BackupCommand(org.h2.command.dml.BackupCommand) DefineCommand(org.h2.command.ddl.DefineCommand) ValueString(org.h2.value.ValueString) DbException(org.h2.message.DbException)

Example 87 with Command

use of org.h2.command.Command in project h2database by h2database.

the class Parser method parseSavepoint.

private TransactionCommand parseSavepoint() {
    TransactionCommand command = new TransactionCommand(session, CommandInterface.SAVEPOINT);
    command.setSavepointName(readUniqueIdentifier());
    return command;
}
Also used : TransactionCommand(org.h2.command.dml.TransactionCommand)

Example 88 with Command

use of org.h2.command.Command in project h2database by h2database.

the class Parser method parseRunScript.

private RunScriptCommand parseRunScript() {
    RunScriptCommand command = new RunScriptCommand(session);
    read("FROM");
    command.setFileNameExpr(readExpression());
    if (readIf("COMPRESSION")) {
        command.setCompressionAlgorithm(readUniqueIdentifier());
    }
    if (readIf("CIPHER")) {
        command.setCipher(readUniqueIdentifier());
        if (readIf("PASSWORD")) {
            command.setPassword(readExpression());
        }
    }
    if (readIf("CHARSET")) {
        command.setCharset(Charset.forName(readString()));
    }
    return command;
}
Also used : RunScriptCommand(org.h2.command.dml.RunScriptCommand)

Example 89 with Command

use of org.h2.command.Command 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 90 with Command

use of org.h2.command.Command 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)

Aggregations

ValueString (org.h2.value.ValueString)37 Expression (org.h2.expression.Expression)20 ValueExpression (org.h2.expression.ValueExpression)19 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)17 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)16 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)16 Column (org.h2.table.Column)16 IndexColumn (org.h2.table.IndexColumn)13 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)12 ExpressionColumn (org.h2.expression.ExpressionColumn)12 Table (org.h2.table.Table)12 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)11 Connection (java.sql.Connection)10 CreateLinkedTable (org.h2.command.ddl.CreateLinkedTable)10 CreateSchema (org.h2.command.ddl.CreateSchema)10 CreateTable (org.h2.command.ddl.CreateTable)10 DropSchema (org.h2.command.ddl.DropSchema)10 DropTable (org.h2.command.ddl.DropTable)10 Schema (org.h2.schema.Schema)10 TruncateTable (org.h2.command.ddl.TruncateTable)9