Search in sources :

Example 1 with ConstraintActionType

use of org.h2.constraint.ConstraintActionType in project h2database by h2database.

the class ConstraintReferential method prepare.

private Prepared prepare(Session session, String sql, ConstraintActionType action) {
    Prepared command = session.prepare(sql);
    if (action != ConstraintActionType.CASCADE) {
        ArrayList<Parameter> params = command.getParameters();
        for (int i = 0, len = columns.length; i < len; i++) {
            Column column = columns[i].column;
            Parameter param = params.get(i);
            Value value;
            if (action == ConstraintActionType.SET_NULL) {
                value = ValueNull.INSTANCE;
            } else {
                Expression expr = column.getDefaultExpression();
                if (expr == null) {
                    throw DbException.get(ErrorCode.NO_DEFAULT_SET_1, column.getName());
                }
                value = expr.getValue(session);
            }
            param.setValue(value);
        }
    }
    return command;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) Prepared(org.h2.command.Prepared) Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter)

Example 2 with ConstraintActionType

use of org.h2.constraint.ConstraintActionType in project h2database by h2database.

the class Parser method parseAction.

private ConstraintActionType parseAction() {
    ConstraintActionType result = parseCascadeOrRestrict();
    if (result != null) {
        return result;
    }
    if (readIf("NO")) {
        read("ACTION");
        return ConstraintActionType.RESTRICT;
    }
    read("SET");
    if (readIf("NULL")) {
        return ConstraintActionType.SET_NULL;
    }
    read("DEFAULT");
    return ConstraintActionType.SET_DEFAULT;
}
Also used : ConstraintActionType(org.h2.constraint.ConstraintActionType)

Example 3 with ConstraintActionType

use of org.h2.constraint.ConstraintActionType in project h2database by h2database.

the class Parser method parseDrop.

private Prepared parseDrop() {
    if (readIf("TABLE")) {
        boolean ifExists = readIfExists(false);
        String tableName = readIdentifierWithSchema();
        DropTable command = new DropTable(session, getSchema());
        command.setTableName(tableName);
        while (readIf(",")) {
            tableName = readIdentifierWithSchema();
            DropTable next = new DropTable(session, getSchema());
            next.setTableName(tableName);
            command.addNextDropTable(next);
        }
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
            readIf("CONSTRAINTS");
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        } else if (readIf("IGNORE")) {
            command.setDropAction(ConstraintActionType.SET_DEFAULT);
        }
        return command;
    } else if (readIf("INDEX")) {
        boolean ifExists = readIfExists(false);
        String indexName = readIdentifierWithSchema();
        DropIndex command = new DropIndex(session, getSchema());
        command.setIndexName(indexName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        // Support for MySQL: DROP INDEX index_name ON tbl_name
        if (readIf("ON")) {
            readIdentifierWithSchema();
        }
        return command;
    } else if (readIf("USER")) {
        boolean ifExists = readIfExists(false);
        DropUser command = new DropUser(session);
        command.setUserName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        readIf("CASCADE");
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SEQUENCE")) {
        boolean ifExists = readIfExists(false);
        String sequenceName = readIdentifierWithSchema();
        DropSequence command = new DropSequence(session, getSchema());
        command.setSequenceName(sequenceName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("CONSTANT")) {
        boolean ifExists = readIfExists(false);
        String constantName = readIdentifierWithSchema();
        DropConstant command = new DropConstant(session, getSchema());
        command.setConstantName(constantName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("TRIGGER")) {
        boolean ifExists = readIfExists(false);
        String triggerName = readIdentifierWithSchema();
        DropTrigger command = new DropTrigger(session, getSchema());
        command.setTriggerName(triggerName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("VIEW")) {
        boolean ifExists = readIfExists(false);
        String viewName = readIdentifierWithSchema();
        DropView command = new DropView(session, getSchema());
        command.setViewName(viewName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        ConstraintActionType dropAction = parseCascadeOrRestrict();
        if (dropAction != null) {
            command.setDropAction(dropAction);
        }
        return command;
    } else if (readIf("ROLE")) {
        boolean ifExists = readIfExists(false);
        DropRole command = new DropRole(session);
        command.setRoleName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("ALIAS")) {
        boolean ifExists = readIfExists(false);
        String aliasName = readIdentifierWithSchema();
        DropFunctionAlias command = new DropFunctionAlias(session, getSchema());
        command.setAliasName(aliasName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SCHEMA")) {
        boolean ifExists = readIfExists(false);
        DropSchema command = new DropSchema(session);
        command.setSchemaName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        }
        return command;
    } else if (readIf("ALL")) {
        read("OBJECTS");
        DropDatabase command = new DropDatabase(session);
        command.setDropAllObjects(true);
        if (readIf("DELETE")) {
            read("FILES");
            command.setDeleteFiles(true);
        }
        return command;
    } else if (readIf("DOMAIN")) {
        return parseDropUserDataType();
    } else if (readIf("TYPE")) {
        return parseDropUserDataType();
    } else if (readIf("DATATYPE")) {
        return parseDropUserDataType();
    } else if (readIf("AGGREGATE")) {
        return parseDropAggregate();
    } else if (readIf("SYNONYM")) {
        boolean ifExists = readIfExists(false);
        String synonymName = readIdentifierWithSchema();
        DropSynonym command = new DropSynonym(session, getSchema());
        command.setSynonymName(synonymName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    }
    throw getSyntaxError();
}
Also used : DropConstant(org.h2.command.ddl.DropConstant) DropTrigger(org.h2.command.ddl.DropTrigger) DropView(org.h2.command.ddl.DropView) DropUser(org.h2.command.ddl.DropUser) DropSynonym(org.h2.command.ddl.DropSynonym) DropSequence(org.h2.command.ddl.DropSequence) ValueString(org.h2.value.ValueString) DropTable(org.h2.command.ddl.DropTable) DropSchema(org.h2.command.ddl.DropSchema) DropRole(org.h2.command.ddl.DropRole) DropFunctionAlias(org.h2.command.ddl.DropFunctionAlias) DropDatabase(org.h2.command.ddl.DropDatabase) ConstraintActionType(org.h2.constraint.ConstraintActionType) DropIndex(org.h2.command.ddl.DropIndex)

Aggregations

ConstraintActionType (org.h2.constraint.ConstraintActionType)2 Prepared (org.h2.command.Prepared)1 DropConstant (org.h2.command.ddl.DropConstant)1 DropDatabase (org.h2.command.ddl.DropDatabase)1 DropFunctionAlias (org.h2.command.ddl.DropFunctionAlias)1 DropIndex (org.h2.command.ddl.DropIndex)1 DropRole (org.h2.command.ddl.DropRole)1 DropSchema (org.h2.command.ddl.DropSchema)1 DropSequence (org.h2.command.ddl.DropSequence)1 DropSynonym (org.h2.command.ddl.DropSynonym)1 DropTable (org.h2.command.ddl.DropTable)1 DropTrigger (org.h2.command.ddl.DropTrigger)1 DropUser (org.h2.command.ddl.DropUser)1 DropView (org.h2.command.ddl.DropView)1 Expression (org.h2.expression.Expression)1 Parameter (org.h2.expression.Parameter)1 Column (org.h2.table.Column)1 IndexColumn (org.h2.table.IndexColumn)1 Value (org.h2.value.Value)1 ValueString (org.h2.value.ValueString)1