Search in sources :

Example 6 with Command

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

the class Parser method parseCreate.

private Prepared parseCreate() {
    boolean orReplace = false;
    if (readIf("OR")) {
        read("REPLACE");
        orReplace = true;
    }
    boolean force = readIf("FORCE");
    if (readIf("VIEW")) {
        return parseCreateView(force, orReplace);
    } else if (readIf("ALIAS")) {
        return parseCreateFunctionAlias(force);
    } else if (readIf("SEQUENCE")) {
        return parseCreateSequence();
    } else if (readIf("USER")) {
        return parseCreateUser();
    } else if (readIf("TRIGGER")) {
        return parseCreateTrigger(force);
    } else if (readIf("ROLE")) {
        return parseCreateRole();
    } else if (readIf("SCHEMA")) {
        return parseCreateSchema();
    } else if (readIf("CONSTANT")) {
        return parseCreateConstant();
    } else if (readIf("DOMAIN")) {
        return parseCreateUserDataType();
    } else if (readIf("TYPE")) {
        return parseCreateUserDataType();
    } else if (readIf("DATATYPE")) {
        return parseCreateUserDataType();
    } else if (readIf("AGGREGATE")) {
        return parseCreateAggregate(force);
    } else if (readIf("LINKED")) {
        return parseCreateLinkedTable(false, false, force);
    }
    // tables or linked tables
    boolean memory = false, cached = false;
    if (readIf("MEMORY")) {
        memory = true;
    } else if (readIf("CACHED")) {
        cached = true;
    }
    if (readIf("LOCAL")) {
        read("TEMPORARY");
        if (readIf("LINKED")) {
            return parseCreateLinkedTable(true, false, force);
        }
        read("TABLE");
        return parseCreateTable(true, false, cached);
    } else if (readIf("GLOBAL")) {
        read("TEMPORARY");
        if (readIf("LINKED")) {
            return parseCreateLinkedTable(true, true, force);
        }
        read("TABLE");
        return parseCreateTable(true, true, cached);
    } else if (readIf("TEMP") || readIf("TEMPORARY")) {
        if (readIf("LINKED")) {
            return parseCreateLinkedTable(true, true, force);
        }
        read("TABLE");
        return parseCreateTable(true, true, cached);
    } else if (readIf("TABLE")) {
        if (!cached && !memory) {
            cached = database.getDefaultTableType() == Table.TYPE_CACHED;
        }
        return parseCreateTable(false, false, cached);
    } else if (readIf("SYNONYM")) {
        return parseCreateSynonym(orReplace);
    } else {
        boolean hash = false, primaryKey = false;
        boolean unique = false, spatial = false;
        String indexName = null;
        Schema oldSchema = null;
        boolean ifNotExists = false;
        if (readIf("PRIMARY")) {
            read("KEY");
            if (readIf("HASH")) {
                hash = true;
            }
            primaryKey = true;
            if (!isToken("ON")) {
                ifNotExists = readIfNotExists();
                indexName = readIdentifierWithSchema(null);
                oldSchema = getSchema();
            }
        } else {
            if (readIf("UNIQUE")) {
                unique = true;
            }
            if (readIf("HASH")) {
                hash = true;
            }
            if (readIf("SPATIAL")) {
                spatial = true;
            }
            if (readIf("INDEX")) {
                if (!isToken("ON")) {
                    ifNotExists = readIfNotExists();
                    indexName = readIdentifierWithSchema(null);
                    oldSchema = getSchema();
                }
            } else {
                throw getSyntaxError();
            }
        }
        read("ON");
        String tableName = readIdentifierWithSchema();
        checkSchema(oldSchema);
        CreateIndex command = new CreateIndex(session, getSchema());
        command.setIfNotExists(ifNotExists);
        command.setPrimaryKey(primaryKey);
        command.setTableName(tableName);
        command.setUnique(unique);
        command.setIndexName(indexName);
        command.setComment(readCommentIf());
        read("(");
        command.setIndexColumns(parseIndexColumnList());
        if (readIf("USING")) {
            if (hash) {
                throw getSyntaxError();
            }
            if (spatial) {
                throw getSyntaxError();
            }
            if (readIf("BTREE")) {
            // default
            } else if (readIf("RTREE")) {
                spatial = true;
            } else if (readIf("HASH")) {
                hash = true;
            } else {
                throw getSyntaxError();
            }
        }
        command.setHash(hash);
        command.setSpatial(spatial);
        return command;
    }
}
Also used : CreateIndex(org.h2.command.ddl.CreateIndex) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString)

Example 7 with Command

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

the class Parser method parseAlterTable.

private Prepared parseAlterTable() {
    boolean ifTableExists = readIfExists(false);
    String tableName = readIdentifierWithSchema();
    Schema schema = getSchema();
    if (readIf("ADD")) {
        Prepared command = parseAlterTableAddConstraintIf(tableName, schema, ifTableExists);
        if (command != null) {
            return command;
        }
        return parseAlterTableAddColumn(tableName, schema, ifTableExists);
    } else if (readIf("SET")) {
        read("REFERENTIAL_INTEGRITY");
        int type = CommandInterface.ALTER_TABLE_SET_REFERENTIAL_INTEGRITY;
        boolean value = readBooleanSetting();
        AlterTableSet command = new AlterTableSet(session, schema, type, value);
        command.setTableName(tableName);
        command.setIfTableExists(ifTableExists);
        if (readIf("CHECK")) {
            command.setCheckExisting(true);
        } else if (readIf("NOCHECK")) {
            command.setCheckExisting(false);
        }
        return command;
    } else if (readIf("RENAME")) {
        if (readIf("COLUMN")) {
            // PostgreSQL syntax
            String columnName = readColumnIdentifier();
            read("TO");
            AlterTableRenameColumn command = new AlterTableRenameColumn(session, schema);
            command.setTableName(tableName);
            command.setIfTableExists(ifTableExists);
            command.setOldColumnName(columnName);
            String newName = readColumnIdentifier();
            command.setNewColumnName(newName);
            return command;
        } else if (readIf("CONSTRAINT")) {
            String constraintName = readIdentifierWithSchema(schema.getName());
            checkSchema(schema);
            read("TO");
            AlterTableRenameConstraint command = new AlterTableRenameConstraint(session, schema);
            command.setConstraintName(constraintName);
            String newName = readColumnIdentifier();
            command.setNewConstraintName(newName);
            return commandIfTableExists(schema, tableName, ifTableExists, command);
        } else {
            read("TO");
            String newName = readIdentifierWithSchema(schema.getName());
            checkSchema(schema);
            AlterTableRename command = new AlterTableRename(session, getSchema());
            command.setOldTableName(tableName);
            command.setNewTableName(newName);
            command.setIfTableExists(ifTableExists);
            command.setHidden(readIf("HIDDEN"));
            return command;
        }
    } else if (readIf("DROP")) {
        if (readIf("CONSTRAINT")) {
            boolean ifExists = readIfExists(false);
            String constraintName = readIdentifierWithSchema(schema.getName());
            ifExists = readIfExists(ifExists);
            checkSchema(schema);
            AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema(), ifExists);
            command.setConstraintName(constraintName);
            return commandIfTableExists(schema, tableName, ifTableExists, command);
        } else if (readIf("FOREIGN")) {
            // MySQL compatibility
            read("KEY");
            String constraintName = readIdentifierWithSchema(schema.getName());
            checkSchema(schema);
            AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema(), false);
            command.setConstraintName(constraintName);
            return commandIfTableExists(schema, tableName, ifTableExists, command);
        } else if (readIf("INDEX")) {
            // MySQL compatibility
            String indexOrConstraintName = readIdentifierWithSchema();
            final SchemaCommand command;
            if (schema.findIndex(session, indexOrConstraintName) != null) {
                DropIndex dropIndexCommand = new DropIndex(session, getSchema());
                dropIndexCommand.setIndexName(indexOrConstraintName);
                command = dropIndexCommand;
            } else {
                AlterTableDropConstraint dropCommand = new AlterTableDropConstraint(session, getSchema(), false);
                dropCommand.setConstraintName(indexOrConstraintName);
                command = dropCommand;
            }
            return commandIfTableExists(schema, tableName, ifTableExists, command);
        } else if (readIf("PRIMARY")) {
            read("KEY");
            Table table = tableIfTableExists(schema, tableName, ifTableExists);
            if (table == null) {
                return new NoOperation(session);
            }
            Index idx = table.getPrimaryKey();
            DropIndex command = new DropIndex(session, schema);
            command.setIndexName(idx.getName());
            return command;
        } else {
            readIf("COLUMN");
            boolean ifExists = readIfExists(false);
            ArrayList<Column> columnsToRemove = New.arrayList();
            Table table = tableIfTableExists(schema, tableName, ifTableExists);
            // For Oracle compatibility - open bracket required
            boolean openingBracketDetected = readIf("(");
            do {
                String columnName = readColumnIdentifier();
                if (table != null) {
                    if (!ifExists || table.doesColumnExist(columnName)) {
                        Column column = table.getColumn(columnName);
                        columnsToRemove.add(column);
                    }
                }
            } while (readIf(","));
            if (openingBracketDetected) {
                // For Oracle compatibility - close bracket
                read(")");
            }
            if (table == null || columnsToRemove.isEmpty()) {
                return new NoOperation(session);
            }
            AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
            command.setType(CommandInterface.ALTER_TABLE_DROP_COLUMN);
            command.setTableName(tableName);
            command.setIfTableExists(ifTableExists);
            command.setColumnsToRemove(columnsToRemove);
            return command;
        }
    } else if (readIf("CHANGE")) {
        // MySQL compatibility
        readIf("COLUMN");
        String columnName = readColumnIdentifier();
        String newColumnName = readColumnIdentifier();
        Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
        boolean nullable = column == null ? true : column.isNullable();
        // new column type ignored. RENAME and MODIFY are
        // a single command in MySQL but two different commands in H2.
        parseColumnForTable(newColumnName, nullable);
        AlterTableRenameColumn command = new AlterTableRenameColumn(session, schema);
        command.setTableName(tableName);
        command.setIfTableExists(ifTableExists);
        command.setOldColumnName(columnName);
        command.setNewColumnName(newColumnName);
        return command;
    } else if (readIf("MODIFY")) {
        // MySQL compatibility (optional)
        readIf("COLUMN");
        // Oracle specifies (but will not require) an opening parenthesis
        boolean hasOpeningBracket = readIf("(");
        String columnName = readColumnIdentifier();
        AlterTableAlterColumn command = null;
        NullConstraintType nullConstraint = parseNotNullConstraint();
        switch(nullConstraint) {
            case NULL_IS_ALLOWED:
            case NULL_IS_NOT_ALLOWED:
                command = new AlterTableAlterColumn(session, schema);
                command.setTableName(tableName);
                command.setIfTableExists(ifTableExists);
                Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
                command.setOldColumn(column);
                if (nullConstraint == NullConstraintType.NULL_IS_ALLOWED) {
                    command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
                } else {
                    command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL);
                }
                break;
            case NO_NULL_CONSTRAINT_FOUND:
                command = parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
                break;
            default:
                throw DbException.get(ErrorCode.UNKNOWN_MODE_1, "Internal Error - unhandled case: " + nullConstraint.name());
        }
        if (hasOpeningBracket) {
            read(")");
        }
        return command;
    } else if (readIf("ALTER")) {
        readIf("COLUMN");
        String columnName = readColumnIdentifier();
        Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
        if (readIf("RENAME")) {
            read("TO");
            AlterTableRenameColumn command = new AlterTableRenameColumn(session, schema);
            command.setTableName(tableName);
            command.setIfTableExists(ifTableExists);
            command.setOldColumnName(columnName);
            String newName = readColumnIdentifier();
            command.setNewColumnName(newName);
            return command;
        } else if (readIf("DROP")) {
            // PostgreSQL compatibility
            if (readIf("DEFAULT")) {
                AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
                command.setTableName(tableName);
                command.setIfTableExists(ifTableExists);
                command.setOldColumn(column);
                command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT);
                command.setDefaultExpression(null);
                return command;
            }
            if (readIf("ON")) {
                read("UPDATE");
                AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
                command.setTableName(tableName);
                command.setIfTableExists(ifTableExists);
                command.setOldColumn(column);
                command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_ON_UPDATE);
                command.setDefaultExpression(null);
                return command;
            }
            read("NOT");
            read("NULL");
            AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
            command.setTableName(tableName);
            command.setIfTableExists(ifTableExists);
            command.setOldColumn(column);
            command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
            return command;
        } else if (readIf("TYPE")) {
            // PostgreSQL compatibility
            return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
        } else if (readIf("SET")) {
            if (readIf("DATA")) {
                // Derby compatibility
                read("TYPE");
                return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
            }
            AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
            command.setTableName(tableName);
            command.setIfTableExists(ifTableExists);
            command.setOldColumn(column);
            NullConstraintType nullConstraint = parseNotNullConstraint();
            switch(nullConstraint) {
                case NULL_IS_ALLOWED:
                    command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
                    break;
                case NULL_IS_NOT_ALLOWED:
                    command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL);
                    break;
                case NO_NULL_CONSTRAINT_FOUND:
                    if (readIf("DEFAULT")) {
                        Expression defaultExpression = readExpression();
                        command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT);
                        command.setDefaultExpression(defaultExpression);
                    } else if (readIf("ON")) {
                        read("UPDATE");
                        Expression onUpdateExpression = readExpression();
                        command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_ON_UPDATE);
                        command.setDefaultExpression(onUpdateExpression);
                    } else if (readIf("INVISIBLE")) {
                        command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY);
                        command.setVisible(false);
                    } else if (readIf("VISIBLE")) {
                        command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY);
                        command.setVisible(true);
                    }
                    break;
                default:
                    throw DbException.get(ErrorCode.UNKNOWN_MODE_1, "Internal Error - unhandled case: " + nullConstraint.name());
            }
            return command;
        } else if (readIf("RESTART")) {
            readIf("WITH");
            Expression start = readExpression();
            AlterSequence command = new AlterSequence(session, schema);
            command.setColumn(column);
            command.setStartWith(start);
            return commandIfTableExists(schema, tableName, ifTableExists, command);
        } else if (readIf("SELECTIVITY")) {
            AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
            command.setTableName(tableName);
            command.setIfTableExists(ifTableExists);
            command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY);
            command.setOldColumn(column);
            command.setSelectivity(readExpression());
            return command;
        } else {
            return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
        }
    }
    throw getSyntaxError();
}
Also used : AlterSequence(org.h2.command.dml.AlterSequence) 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) AlterTableRename(org.h2.command.ddl.AlterTableRename) DropIndex(org.h2.command.ddl.DropIndex) Index(org.h2.index.Index) CreateIndex(org.h2.command.ddl.CreateIndex) ValueString(org.h2.value.ValueString) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) SchemaCommand(org.h2.command.ddl.SchemaCommand) AlterTableSet(org.h2.command.dml.AlterTableSet) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) NoOperation(org.h2.command.dml.NoOperation) 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) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) DropIndex(org.h2.command.ddl.DropIndex)

Example 8 with Command

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

the class Parser method parseDropAggregate.

private DropAggregate parseDropAggregate() {
    boolean ifExists = readIfExists(false);
    DropAggregate command = new DropAggregate(session);
    command.setName(readUniqueIdentifier());
    ifExists = readIfExists(ifExists);
    command.setIfExists(ifExists);
    return command;
}
Also used : DropAggregate(org.h2.command.ddl.DropAggregate)

Example 9 with Command

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

the class Parser method parseCall.

private Call parseCall() {
    Call command = new Call(session);
    currentPrepared = command;
    command.setExpression(readExpression());
    return command;
}
Also used : Call(org.h2.command.dml.Call) FunctionCall(org.h2.expression.FunctionCall)

Example 10 with Command

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

the class Parser method parseCreateView.

private CreateView parseCreateView(boolean force, boolean orReplace) {
    boolean ifNotExists = readIfNotExists();
    boolean isTableExpression = readIf("TABLE_EXPRESSION");
    String viewName = readIdentifierWithSchema();
    CreateView command = new CreateView(session, getSchema());
    this.createView = command;
    command.setViewName(viewName);
    command.setIfNotExists(ifNotExists);
    command.setComment(readCommentIf());
    command.setOrReplace(orReplace);
    command.setForce(force);
    command.setTableExpression(isTableExpression);
    if (readIf("(")) {
        String[] cols = parseColumnList();
        command.setColumnNames(cols);
    }
    String select = StringUtils.cache(sqlCommand.substring(parseIndex));
    read("AS");
    try {
        Query query;
        session.setParsingCreateView(true, viewName);
        try {
            query = parseSelect();
            query.prepare();
        } finally {
            session.setParsingCreateView(false, viewName);
        }
        command.setSelect(query);
    } catch (DbException e) {
        if (force) {
            command.setSelectSQL(select);
            while (currentTokenType != END) {
                read();
            }
        } else {
            throw e;
        }
    }
    return command;
}
Also used : Query(org.h2.command.dml.Query) ValueString(org.h2.value.ValueString) CreateView(org.h2.command.ddl.CreateView) DbException(org.h2.message.DbException)

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