Search in sources :

Example 31 with Command

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

the class Parser method parseCreateConstant.

private CreateConstant parseCreateConstant() {
    boolean ifNotExists = readIfNotExists();
    String constantName = readIdentifierWithSchema();
    Schema schema = getSchema();
    if (isKeyword(constantName)) {
        throw DbException.get(ErrorCode.CONSTANT_ALREADY_EXISTS_1, constantName);
    }
    read("VALUE");
    Expression expr = readExpression();
    CreateConstant command = new CreateConstant(session, schema);
    command.setConstantName(constantName);
    command.setExpression(expr);
    command.setIfNotExists(ifNotExists);
    return command;
}
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) CreateConstant(org.h2.command.ddl.CreateConstant)

Example 32 with Command

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

the class Parser method parseInsertGivenTable.

private Insert parseInsertGivenTable(Insert command, Table table) {
    Column[] columns = null;
    if (readIf("(")) {
        if (isSelect()) {
            command.setQuery(parseSelect());
            read(")");
            return command;
        }
        columns = parseColumnList(table);
        command.setColumns(columns);
    }
    if (readIf("DIRECT")) {
        command.setInsertFromSelect(true);
    }
    if (readIf("SORTED")) {
        command.setSortedInsertMode(true);
    }
    if (readIf("DEFAULT")) {
        read("VALUES");
        Expression[] expr = {};
        command.addRow(expr);
    } else if (readIf("VALUES")) {
        read("(");
        do {
            ArrayList<Expression> values = New.arrayList();
            if (!readIf(")")) {
                do {
                    if (readIf("DEFAULT")) {
                        values.add(null);
                    } else {
                        values.add(readExpression());
                    }
                } while (readIfMore(false));
            }
            command.addRow(values.toArray(new Expression[0]));
        // the following condition will allow (..),; and (..);
        } while (readIf(",") && readIf("("));
    } else if (readIf("SET")) {
        if (columns != null) {
            throw getSyntaxError();
        }
        ArrayList<Column> columnList = New.arrayList();
        ArrayList<Expression> values = New.arrayList();
        do {
            columnList.add(parseColumn(table));
            read("=");
            Expression expression;
            if (readIf("DEFAULT")) {
                expression = ValueExpression.getDefault();
            } else {
                expression = readExpression();
            }
            values.add(expression);
        } while (readIf(","));
        command.setColumns(columnList.toArray(new Column[0]));
        command.addRow(values.toArray(new Expression[0]));
    } else {
        command.setQuery(parseSelect());
    }
    return null;
}
Also used : 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) ArrayList(java.util.ArrayList)

Example 33 with Command

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

the class Parser method parseSetJavaObjectSerializer.

private Set parseSetJavaObjectSerializer() {
    Set command = new Set(session, SetTypes.JAVA_OBJECT_SERIALIZER);
    String name = readString();
    command.setString(name);
    return command;
}
Also used : Set(org.h2.command.dml.Set) LinkedHashSet(java.util.LinkedHashSet) AlterTableSet(org.h2.command.dml.AlterTableSet) HashSet(java.util.HashSet) ValueString(org.h2.value.ValueString)

Example 34 with Command

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

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

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