Search in sources :

Example 21 with Domain

use of org.h2.schema.Domain in project h2database by h2database.

the class Parser method parseComment.

private Prepared parseComment() {
    int type = 0;
    read(ON);
    boolean column = false;
    if (readIf(TABLE) || readIf("VIEW")) {
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("COLUMN")) {
        column = true;
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("CONSTANT")) {
        type = DbObject.CONSTANT;
    } else if (readIf(CONSTRAINT)) {
        type = DbObject.CONSTRAINT;
    } else if (readIf("ALIAS")) {
        type = DbObject.FUNCTION_ALIAS;
    } else if (readIf("INDEX")) {
        type = DbObject.INDEX;
    } else if (readIf("ROLE")) {
        type = DbObject.ROLE;
    } else if (readIf("SCHEMA")) {
        type = DbObject.SCHEMA;
    } else if (readIf("SEQUENCE")) {
        type = DbObject.SEQUENCE;
    } else if (readIf("TRIGGER")) {
        type = DbObject.TRIGGER;
    } else if (readIf(USER)) {
        type = DbObject.USER;
    } else if (readIf("DOMAIN")) {
        type = DbObject.DOMAIN;
    } else {
        throw getSyntaxError();
    }
    SetComment command = new SetComment(session);
    String objectName;
    if (column) {
        // can't use readIdentifierWithSchema() because
        // it would not read [catalog.]schema.table.column correctly
        objectName = readIdentifier();
        String tmpSchemaName = null;
        read(DOT);
        boolean allowEmpty = database.getMode().allowEmptySchemaValuesAsDefaultSchema;
        String columnName = allowEmpty && currentTokenType == DOT ? null : readIdentifier();
        if (readIf(DOT)) {
            tmpSchemaName = objectName;
            objectName = columnName;
            columnName = allowEmpty && currentTokenType == DOT ? null : readIdentifier();
            if (readIf(DOT)) {
                checkDatabaseName(tmpSchemaName);
                tmpSchemaName = objectName;
                objectName = columnName;
                columnName = readIdentifier();
            }
        }
        if (columnName == null || objectName == null) {
            throw DbException.getSyntaxError(sqlCommand, token.start(), "table.column");
        }
        schemaName = tmpSchemaName != null ? tmpSchemaName : session.getCurrentSchemaName();
        command.setColumn(true);
        command.setColumnName(columnName);
    } else {
        objectName = readIdentifierWithSchema();
    }
    command.setSchemaName(schemaName);
    command.setObjectName(objectName);
    command.setObjectType(type);
    read(IS);
    command.setCommentExpression(readExpression());
    return command;
}
Also used : SetComment(org.h2.command.ddl.SetComment) ValueBigint(org.h2.value.ValueBigint) AlterDomainRenameConstraint(org.h2.command.ddl.AlterDomainRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterDomainDropConstraint(org.h2.command.ddl.AlterDomainDropConstraint) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterDomainAddConstraint(org.h2.command.ddl.AlterDomainAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 22 with Domain

use of org.h2.schema.Domain in project h2database by h2database.

the class Parser method parseDrop.

private Prepared parseDrop() {
    if (readIf(TABLE)) {
        boolean ifExists = readIfExists(false);
        DropTable command = new DropTable(session);
        do {
            String tableName = readIdentifierWithSchema();
            command.addTable(getSchema(), tableName);
        } while (readIf(COMMA));
        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")) {
            // TODO SET_DEFAULT works in the same way as CASCADE
            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(readIdentifier());
        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(readIdentifier());
        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(readIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        ConstraintActionType dropAction = parseCascadeOrRestrict();
        if (dropAction != null) {
            command.setDropAction(dropAction);
        }
        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") || readIf("TYPE") || readIf("DATATYPE")) {
        return parseDropDomain();
    } 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) 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)

Example 23 with Domain

use of org.h2.schema.Domain in project h2database by h2database.

the class Parser method readIfDataType1.

private TypeInfo readIfDataType1() {
    switch(currentTokenType) {
        case IDENTIFIER:
            if (token.isQuoted()) {
                return null;
            }
            break;
        case INTERVAL:
            {
                read();
                TypeInfo typeInfo = readIntervalQualifier();
                if (typeInfo == null) {
                    throw intervalQualifierError();
                }
                return typeInfo;
            }
        case NULL:
            read();
            return TypeInfo.TYPE_NULL;
        case ROW:
            read();
            return parseRowType();
        case ARRAY:
            // Partial compatibility with 1.4.200 and older versions
            if (session.isQuirksMode()) {
                read();
                return parseArrayType(TypeInfo.TYPE_VARCHAR);
            }
            addExpected("data type");
            throw getSyntaxError();
        default:
            if (isKeyword(currentToken)) {
                break;
            }
            addExpected("data type");
            throw getSyntaxError();
    }
    int index = tokenIndex;
    String originalCase = currentToken;
    read();
    if (currentTokenType == DOT) {
        setTokenIndex(index);
        return null;
    }
    String original = upperName(originalCase);
    switch(original) {
        case "BINARY":
            if (readIf("VARYING")) {
                original = "BINARY VARYING";
            } else if (readIf("LARGE")) {
                read("OBJECT");
                original = "BINARY LARGE OBJECT";
            } else if (variableBinary) {
                original = "VARBINARY";
            }
            break;
        case "CHAR":
            if (readIf("VARYING")) {
                original = "CHAR VARYING";
            } else if (readIf("LARGE")) {
                read("OBJECT");
                original = "CHAR LARGE OBJECT";
            }
            break;
        case "CHARACTER":
            if (readIf("VARYING")) {
                original = "CHARACTER VARYING";
            } else if (readIf("LARGE")) {
                read("OBJECT");
                original = "CHARACTER LARGE OBJECT";
            }
            break;
        case "DATETIME":
        case "DATETIME2":
            return parseDateTimeType(false);
        case "DEC":
        case "DECIMAL":
            return parseNumericType(true);
        case "DECFLOAT":
            return parseDecfloatType();
        case "DOUBLE":
            if (readIf("PRECISION")) {
                original = "DOUBLE PRECISION";
            }
            break;
        case "ENUM":
            return parseEnumType();
        case "FLOAT":
            return parseFloatType();
        case "GEOMETRY":
            return parseGeometryType();
        case "LONG":
            if (readIf("RAW")) {
                original = "LONG RAW";
            }
            break;
        case "NATIONAL":
            if (readIf("CHARACTER")) {
                if (readIf("VARYING")) {
                    original = "NATIONAL CHARACTER VARYING";
                } else if (readIf("LARGE")) {
                    read("OBJECT");
                    original = "NATIONAL CHARACTER LARGE OBJECT";
                } else {
                    original = "NATIONAL CHARACTER";
                }
            } else {
                read("CHAR");
                if (readIf("VARYING")) {
                    original = "NATIONAL CHAR VARYING";
                } else {
                    original = "NATIONAL CHAR";
                }
            }
            break;
        case "NCHAR":
            if (readIf("VARYING")) {
                original = "NCHAR VARYING";
            } else if (readIf("LARGE")) {
                read("OBJECT");
                original = "NCHAR LARGE OBJECT";
            }
            break;
        case "NUMBER":
            if (database.getMode().disallowedTypes.contains("NUMBER")) {
                throw DbException.get(ErrorCode.UNKNOWN_DATA_TYPE_1, "NUMBER");
            }
            if (!isToken(OPEN_PAREN)) {
                return TypeInfo.getTypeInfo(Value.DECFLOAT, 40, -1, null);
            }
        // $FALL-THROUGH$
        case "NUMERIC":
            return parseNumericType(false);
        case "SMALLDATETIME":
            return parseDateTimeType(true);
        case "TIME":
            return parseTimeType();
        case "TIMESTAMP":
            return parseTimestampType();
    }
    // Domain names can't have multiple words without quotes
    if (originalCase.length() == original.length()) {
        Domain domain = database.getSchema(session.getCurrentSchemaName()).findDomain(originalCase);
        if (domain != null) {
            setTokenIndex(index);
            return null;
        }
    }
    Mode mode = database.getMode();
    DataType dataType = DataType.getTypeByName(original, mode);
    if (dataType == null || mode.disallowedTypes.contains(original)) {
        throw DbException.get(ErrorCode.UNKNOWN_DATA_TYPE_1, original);
    }
    long precision;
    int scale;
    if (dataType.specialPrecisionScale) {
        precision = dataType.defaultPrecision;
        scale = dataType.defaultScale;
    } else {
        precision = -1L;
        scale = -1;
    }
    int t = dataType.type;
    if (database.getIgnoreCase() && t == Value.VARCHAR && !equalsToken("VARCHAR_CASESENSITIVE", original)) {
        dataType = DataType.getDataType(t = Value.VARCHAR_IGNORECASE);
    }
    if ((dataType.supportsPrecision || dataType.supportsScale) && readIf(OPEN_PAREN)) {
        if (!readIf("MAX")) {
            if (dataType.supportsPrecision) {
                precision = readPrecision(t);
                if (precision < dataType.minPrecision) {
                    throw getInvalidPrecisionException(dataType, precision);
                } else if (precision > dataType.maxPrecision)
                    badPrecision: {
                        if (session.isQuirksMode() || session.isTruncateLargeLength()) {
                            switch(dataType.type) {
                                case Value.CHAR:
                                case Value.VARCHAR:
                                case Value.VARCHAR_IGNORECASE:
                                case Value.BINARY:
                                case Value.VARBINARY:
                                case Value.JAVA_OBJECT:
                                case Value.JSON:
                                    precision = dataType.maxPrecision;
                                    break badPrecision;
                            }
                        }
                        throw getInvalidPrecisionException(dataType, precision);
                    }
                if (dataType.supportsScale) {
                    if (readIf(COMMA)) {
                        scale = readInt();
                        if (scale < dataType.minScale || scale > dataType.maxScale) {
                            throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), Integer.toString(dataType.minScale), Integer.toString(dataType.maxScale));
                        }
                    }
                }
            } else {
                scale = readInt();
                if (scale < dataType.minScale || scale > dataType.maxScale) {
                    throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), Integer.toString(dataType.minScale), Integer.toString(dataType.maxScale));
                }
            }
        }
        read(CLOSE_PAREN);
    }
    if (mode.allNumericTypesHavePrecision && (DataType.isNumericType(dataType.type) || dataType.type == Value.BOOLEAN)) {
        if (readIf(OPEN_PAREN)) {
            // Support for MySQL: INT(11), MEDIUMINT(8) and so on.
            // Just ignore the precision.
            readNonNegativeInt();
            read(CLOSE_PAREN);
        }
        readIf("UNSIGNED");
    }
    if (mode.forBitData && DataType.isStringType(t)) {
        if (readIf(FOR)) {
            read("BIT");
            read("DATA");
            dataType = DataType.getDataType(t = Value.VARBINARY);
        }
    }
    return TypeInfo.getTypeInfo(t, precision, scale, null);
}
Also used : Mode(org.h2.engine.Mode) CompareMode(org.h2.value.CompareMode) DataType(org.h2.value.DataType) Domain(org.h2.schema.Domain) DropDomain(org.h2.command.ddl.DropDomain) CreateDomain(org.h2.command.ddl.CreateDomain) TypeInfo(org.h2.value.TypeInfo) ValueBigint(org.h2.value.ValueBigint) AlterDomainRenameConstraint(org.h2.command.ddl.AlterDomainRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterDomainDropConstraint(org.h2.command.ddl.AlterDomainDropConstraint) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterDomainAddConstraint(org.h2.command.ddl.AlterDomainAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 24 with Domain

use of org.h2.schema.Domain in project h2database by h2database.

the class AlterDomainAddConstraint method tryUpdate.

/**
 * Try to execute the statement.
 *
 * @param schema the schema
 * @param domain the domain
 * @return the update count
 */
private int tryUpdate(Schema schema, Domain domain) {
    if (constraintName != null && schema.findConstraint(session, constraintName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, constraintName);
    }
    Database db = session.getDatabase();
    db.lockMeta(session);
    int id = getObjectId();
    String name = generateConstraintName(domain);
    ConstraintDomain constraint = new ConstraintDomain(schema, id, name, domain);
    constraint.setExpression(session, checkExpression);
    if (checkExisting) {
        constraint.checkExistingData(session);
    }
    constraint.setComment(comment);
    db.addSchemaObject(session, constraint);
    domain.addConstraint(constraint);
    return 0;
}
Also used : ConstraintDomain(org.h2.constraint.ConstraintDomain) Database(org.h2.engine.Database)

Example 25 with Domain

use of org.h2.schema.Domain in project h2database by h2database.

the class AlterDomainRename method update.

@Override
long update(Schema schema, Domain domain) {
    Domain d = schema.findDomain(newDomainName);
    if (d != null) {
        if (domain != d) {
            throw DbException.get(ErrorCode.DOMAIN_ALREADY_EXISTS_1, newDomainName);
        }
        if (newDomainName.equals(domain.getName())) {
            return 0;
        }
    }
    session.getDatabase().renameSchemaObject(session, domain, newDomainName);
    forAllDependencies(session, domain, null, null, false);
    return 0;
}
Also used : Domain(org.h2.schema.Domain)

Aggregations

Domain (org.h2.schema.Domain)22 ConstraintDomain (org.h2.constraint.ConstraintDomain)20 Schema (org.h2.schema.Schema)16 TypeInfo (org.h2.value.TypeInfo)14 ValueBigint (org.h2.value.ValueBigint)10 Constraint (org.h2.constraint.Constraint)8 AlterDomainAddConstraint (org.h2.command.ddl.AlterDomainAddConstraint)6 AlterDomainDropConstraint (org.h2.command.ddl.AlterDomainDropConstraint)6 AlterDomainRenameConstraint (org.h2.command.ddl.AlterDomainRenameConstraint)6 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)6 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)6 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)6 Expression (org.h2.expression.Expression)6 DbException (org.h2.message.DbException)6 Constant (org.h2.schema.Constant)6 FunctionAlias (org.h2.schema.FunctionAlias)6 JavaMethod (org.h2.schema.FunctionAlias.JavaMethod)6 Table (org.h2.table.Table)6 DataType (org.h2.value.DataType)6 DropSchema (org.h2.command.ddl.DropSchema)4