Search in sources :

Example 21 with Command

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

the class Parser method parseSelectSimpleFromPart.

private void parseSelectSimpleFromPart(Select command) {
    do {
        TableFilter filter = readTableFilter();
        parseJoinTableFilter(filter, command);
    } while (readIf(","));
    // to get the order as it was in the original query.
    if (session.isForceJoinOrder()) {
        sortTableFilters(command.getTopFilters());
    }
}
Also used : TableFilter(org.h2.table.TableFilter)

Example 22 with Command

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

the class Parser method parseRollback.

private TransactionCommand parseRollback() {
    TransactionCommand command;
    if (readIf("TRANSACTION")) {
        command = new TransactionCommand(session, CommandInterface.ROLLBACK_TRANSACTION);
        command.setTransactionName(readUniqueIdentifier());
        return command;
    }
    if (readIf("TO")) {
        read("SAVEPOINT");
        command = new TransactionCommand(session, CommandInterface.ROLLBACK_TO_SAVEPOINT);
        command.setSavepointName(readUniqueIdentifier());
    } else {
        readIf("WORK");
        command = new TransactionCommand(session, CommandInterface.ROLLBACK);
    }
    return command;
}
Also used : TransactionCommand(org.h2.command.dml.TransactionCommand)

Example 23 with Command

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

the class Parser method parseMergeUsing.

private MergeUsing parseMergeUsing(Merge oldCommand, int start) {
    MergeUsing command = new MergeUsing(oldCommand);
    currentPrepared = command;
    if (readIf("(")) {
        /* a select query is supplied */
        if (isSelect()) {
            command.setQuery(parseSelect());
            read(")");
        }
        command.setQueryAlias(readFromAlias(null, Collections.singletonList("ON")));
        String[] querySQLOutput = { null };
        List<Column> columnTemplateList = TableView.createQueryColumnTemplateList(null, command.getQuery(), querySQLOutput);
        TableView temporarySourceTableView = createCTEView(command.getQueryAlias(), querySQLOutput[0], columnTemplateList, false, /* no recursion */
        false, /* do not add to session */
        false, /* isPersistent */
        session);
        TableFilter sourceTableFilter = new TableFilter(session, temporarySourceTableView, command.getQueryAlias(), rightsChecked, (Select) command.getQuery(), 0, null);
        command.setSourceTableFilter(sourceTableFilter);
    } else {
        /* Its a table name, simulate a query by building a select query for the table */
        List<String> excludeIdentifiers = Collections.singletonList("ON");
        TableFilter sourceTableFilter = readSimpleTableFilter(0, excludeIdentifiers);
        command.setSourceTableFilter(sourceTableFilter);
        StringBuilder buff = new StringBuilder("SELECT * FROM ");
        appendTableWithSchemaAndAlias(buff, sourceTableFilter.getTable(), sourceTableFilter.getTableAlias());
        Prepared preparedQuery = prepare(session, buff.toString(), null);
        command.setQuery((Select) preparedQuery);
    }
    read("ON");
    read("(");
    Expression condition = readExpression();
    command.setOnCondition(condition);
    read(")");
    if (readIfAll("WHEN", "MATCHED", "THEN")) {
        int startMatched = lastParseIndex;
        if (readIf("UPDATE")) {
            Update updateCommand = new Update(session);
            // currentPrepared = updateCommand;
            TableFilter filter = command.getTargetTableFilter();
            updateCommand.setTableFilter(filter);
            parseUpdateSetClause(updateCommand, filter, startMatched);
            command.setUpdateCommand(updateCommand);
        }
        startMatched = lastParseIndex;
        if (readIf("DELETE")) {
            Delete deleteCommand = new Delete(session);
            TableFilter filter = command.getTargetTableFilter();
            deleteCommand.setTableFilter(filter);
            parseDeleteGivenTable(deleteCommand, null, startMatched);
            command.setDeleteCommand(deleteCommand);
        }
    }
    if (readIfAll("WHEN", "NOT", "MATCHED", "THEN")) {
        if (readIf("INSERT")) {
            Insert insertCommand = new Insert(session);
            insertCommand.setTable(command.getTargetTable());
            parseInsertGivenTable(insertCommand, command.getTargetTable());
            command.setInsertCommand(insertCommand);
        }
    }
    setSQL(command, "MERGE", start);
    // build and prepare the targetMatchQuery ready to test each rows
    // existence in the target table (using source row to match)
    StringBuilder targetMatchQuerySQL = new StringBuilder("SELECT _ROWID_ FROM ");
    appendTableWithSchemaAndAlias(targetMatchQuerySQL, command.getTargetTable(), command.getTargetTableFilter().getTableAlias());
    targetMatchQuerySQL.append(" WHERE ").append(command.getOnCondition().getSQL());
    command.setTargetMatchQuery((Select) parse(targetMatchQuerySQL.toString()));
    return command;
}
Also used : Delete(org.h2.command.dml.Delete) ValueString(org.h2.value.ValueString) Update(org.h2.command.dml.Update) Insert(org.h2.command.dml.Insert) 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) TableFilter(org.h2.table.TableFilter) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) MergeUsing(org.h2.command.dml.MergeUsing) TableView(org.h2.table.TableView)

Example 24 with Command

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

the class Parser method parseEndOfQuery.

private void parseEndOfQuery(Query command) {
    if (readIf("ORDER")) {
        read("BY");
        Select oldSelect = currentSelect;
        if (command instanceof Select) {
            currentSelect = (Select) command;
        }
        ArrayList<SelectOrderBy> orderList = New.arrayList();
        do {
            boolean canBeNumber = true;
            if (readIf("=")) {
                canBeNumber = false;
            }
            SelectOrderBy order = new SelectOrderBy();
            Expression expr = readExpression();
            if (canBeNumber && expr instanceof ValueExpression && expr.getType() == Value.INT) {
                order.columnIndexExpr = expr;
            } else if (expr instanceof Parameter) {
                recompileAlways = true;
                order.columnIndexExpr = expr;
            } else {
                order.expression = expr;
            }
            if (readIf("DESC")) {
                order.descending = true;
            } else {
                readIf("ASC");
            }
            if (readIf("NULLS")) {
                if (readIf("FIRST")) {
                    order.nullsFirst = true;
                } else {
                    read("LAST");
                    order.nullsLast = true;
                }
            }
            orderList.add(order);
        } while (readIf(","));
        command.setOrder(orderList);
        currentSelect = oldSelect;
    }
    // make sure aggregate functions will not work here
    Select temp = currentSelect;
    currentSelect = null;
    // http://sqlpro.developpez.com/SQL2008/
    if (readIf("OFFSET")) {
        command.setOffset(readExpression().optimize(session));
        if (!readIf("ROW")) {
            readIf("ROWS");
        }
    }
    if (readIf("FETCH")) {
        if (!readIf("FIRST")) {
            read("NEXT");
        }
        if (readIf("ROW")) {
            command.setLimit(ValueExpression.get(ValueInt.get(1)));
        } else {
            Expression limit = readExpression().optimize(session);
            command.setLimit(limit);
            if (!readIf("ROW")) {
                read("ROWS");
            }
        }
        read("ONLY");
    }
    currentSelect = temp;
    if (readIf("LIMIT")) {
        temp = currentSelect;
        // make sure aggregate functions will not work here
        currentSelect = null;
        Expression limit = readExpression().optimize(session);
        command.setLimit(limit);
        if (readIf("OFFSET")) {
            Expression offset = readExpression().optimize(session);
            command.setOffset(offset);
        } else if (readIf(",")) {
            // MySQL: [offset, ] rowcount
            Expression offset = limit;
            limit = readExpression().optimize(session);
            command.setOffset(offset);
            command.setLimit(limit);
        }
        if (readIf("SAMPLE_SIZE")) {
            Expression sampleSize = readExpression().optimize(session);
            command.setSampleSize(sampleSize);
        }
        currentSelect = temp;
    }
    if (readIf("FOR")) {
        if (readIf("UPDATE")) {
            if (readIf("OF")) {
                do {
                    readIdentifierWithSchema();
                } while (readIf(","));
            } else if (readIf("NOWAIT")) {
            // TODO parser: select for update nowait: should not wait
            }
            command.setForUpdate(true);
        } else if (readIf("READ") || readIf("FETCH")) {
            read("ONLY");
        }
    }
    if (database.getMode().isolationLevelInSelectOrInsertStatement) {
        parseIsolationClause();
    }
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) ValueExpression(org.h2.expression.ValueExpression) ConditionInSelect(org.h2.expression.ConditionInSelect) Select(org.h2.command.dml.Select) Parameter(org.h2.expression.Parameter) ConditionInParameter(org.h2.expression.ConditionInParameter)

Example 25 with Command

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

the class Parser method parseAlterIndex.

private AlterIndexRename parseAlterIndex() {
    boolean ifExists = readIfExists(false);
    String indexName = readIdentifierWithSchema();
    Schema old = getSchema();
    AlterIndexRename command = new AlterIndexRename(session);
    command.setOldSchema(old);
    command.setOldName(indexName);
    command.setIfExists(ifExists);
    read("RENAME");
    read("TO");
    String newName = readIdentifierWithSchema(old.getName());
    checkSchema(old);
    command.setNewName(newName);
    return command;
}
Also used : AlterIndexRename(org.h2.command.ddl.AlterIndexRename) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString)

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