Search in sources :

Example 11 with DatabaseDataType

use of liquibase.datatype.DatabaseDataType in project liquibase by liquibase.

the class NVarcharType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    if (getRawDefinition() != null && getRawDefinition().toLowerCase().contains("national character varying")) {
        //just go to nvarchar
        setAdditionalInformation(null);
    }
    if (database instanceof HsqlDatabase || database instanceof PostgresDatabase || database instanceof DerbyDatabase) {
        return new DatabaseDataType("VARCHAR", getParameters());
    }
    if (database instanceof OracleDatabase) {
        return new DatabaseDataType("NVARCHAR2", getParameters());
    }
    if (database instanceof MSSQLDatabase) {
        Object[] parameters = getParameters();
        if (parameters.length > 0) {
            String param1 = parameters[0].toString();
            if (!param1.matches("\\d+") || new BigInteger(param1).compareTo(BigInteger.valueOf(4000L)) > 0) {
                try {
                    if (database.getDatabaseMajorVersion() <= 8) {
                        //2000 or earlier
                        DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("nvarchar"), "4000");
                        type.addAdditionalInformation(getAdditionalInformation());
                        return type;
                    }
                }//assuming it is a newer version
                 catch (DatabaseException ignore) {
                }
                DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("nvarchar"), "MAX");
                type.addAdditionalInformation(getAdditionalInformation());
                return type;
            }
        }
        if (parameters.length == 0) {
            parameters = new Object[] { 1 };
        } else if (parameters.length > 1) {
            parameters = Arrays.copyOfRange(parameters, 0, 1);
        }
        DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("nvarchar"), parameters);
        type.addAdditionalInformation(getAdditionalInformation());
        return type;
    }
    return super.toDatabaseDataType(database);
}
Also used : DatabaseDataType(liquibase.datatype.DatabaseDataType) BigInteger(java.math.BigInteger) DatabaseException(liquibase.exception.DatabaseException)

Example 12 with DatabaseDataType

use of liquibase.datatype.DatabaseDataType in project liquibase by liquibase.

the class SmallIntType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    if (database instanceof MSSQLDatabase) {
        //always smallint regardless of parameters passed
        return new DatabaseDataType(database.escapeDataTypeName("smallint"));
    }
    if (database instanceof MySQLDatabase) {
        DatabaseDataType type = new DatabaseDataType("SMALLINT");
        type.addAdditionalInformation(getAdditionalInformation());
        return type;
    }
    if (database instanceof DB2Database || database instanceof DerbyDatabase || database instanceof FirebirdDatabase || database instanceof InformixDatabase) {
        //always smallint regardless of parameters passed
        return new DatabaseDataType("SMALLINT");
    }
    if (database instanceof OracleDatabase) {
        return new DatabaseDataType("NUMBER", 5);
    }
    if (database instanceof PostgresDatabase) {
        if (isAutoIncrement()) {
            return new DatabaseDataType("SMALLSERIAL");
        } else {
            return new DatabaseDataType("SMALLINT");
        }
    }
    return super.toDatabaseDataType(database);
}
Also used : DatabaseDataType(liquibase.datatype.DatabaseDataType)

Example 13 with DatabaseDataType

use of liquibase.datatype.DatabaseDataType in project liquibase by liquibase.

the class MissingTableChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Table missingTable = (Table) missingObject;
    PrimaryKey primaryKey = missingTable.getPrimaryKey();
    //        if (control.diffResult.getReferenceSnapshot().getDatabase().isLiquibaseTable(missingTable.getSchema().toCatalogAndSchema(), missingTable.getName())) {
    //            continue;
    //        }
    CreateTableChange change = createCreateTableChange();
    change.setTableName(missingTable.getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(missingTable.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(missingTable.getSchema().getName());
    }
    if (missingTable.getRemarks() != null) {
        change.setRemarks(missingTable.getRemarks());
    }
    for (Column column : missingTable.getColumns()) {
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setName(column.getName());
        LiquibaseDataType ldt = DataTypeFactory.getInstance().from(column.getType(), referenceDatabase);
        DatabaseDataType ddt = ldt.toDatabaseDataType(comparisonDatabase);
        String typeString = ddt.toString();
        if (comparisonDatabase instanceof MSSQLDatabase) {
            typeString = comparisonDatabase.unescapeDataTypeString(typeString);
        }
        columnConfig.setType(typeString);
        if (column.isAutoIncrement()) {
            columnConfig.setAutoIncrement(true);
        }
        ConstraintsConfig constraintsConfig = null;
        // In MySQL, the primary key must be specified at creation for an autoincrement column
        if (column.isAutoIncrement() && primaryKey != null && primaryKey.getColumns().size() == 1 && primaryKey.getColumnNamesAsList().contains(column.getName())) {
            if (referenceDatabase instanceof MSSQLDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && !primaryKey.getBackingIndex().getClustered()) {
            // have to handle PK as a separate statement
            } else if (referenceDatabase instanceof PostgresDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && primaryKey.getBackingIndex().getClustered()) {
            // have to handle PK as a separate statement
            } else {
                constraintsConfig = new ConstraintsConfig();
                if (shouldAddPrimarykeyToConstraints(missingObject, control, referenceDatabase, comparisonDatabase)) {
                    constraintsConfig.setPrimaryKey(true);
                    constraintsConfig.setPrimaryKeyTablespace(primaryKey.getTablespace());
                    // MySQL sets some primary key names as PRIMARY which is invalid
                    if (comparisonDatabase instanceof MySQLDatabase && "PRIMARY".equals(primaryKey.getName())) {
                        constraintsConfig.setPrimaryKeyName(null);
                    } else {
                        constraintsConfig.setPrimaryKeyName(primaryKey.getName());
                    }
                    control.setAlreadyHandledMissing(primaryKey);
                    control.setAlreadyHandledMissing(primaryKey.getBackingIndex());
                } else {
                    constraintsConfig.setNullable(false);
                }
            }
        } else if (column.isNullable() != null && !column.isNullable()) {
            constraintsConfig = new ConstraintsConfig();
            constraintsConfig.setNullable(false);
        }
        if (constraintsConfig != null) {
            columnConfig.setConstraints(constraintsConfig);
        }
        setDefaultValue(columnConfig, column, referenceDatabase);
        if (column.getRemarks() != null) {
            columnConfig.setRemarks(column.getRemarks());
        }
        if (column.getAutoIncrementInformation() != null) {
            BigInteger startWith = column.getAutoIncrementInformation().getStartWith();
            BigInteger incrementBy = column.getAutoIncrementInformation().getIncrementBy();
            if (startWith != null && !startWith.equals(BigInteger.ONE)) {
                columnConfig.setStartWith(startWith);
            }
            if (incrementBy != null && !incrementBy.equals(BigInteger.ONE)) {
                columnConfig.setIncrementBy(incrementBy);
            }
        }
        change.addColumn(columnConfig);
        control.setAlreadyHandledMissing(column);
    }
    return new Change[] { change };
}
Also used : Table(liquibase.structure.core.Table) ColumnConfig(liquibase.change.ColumnConfig) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) PrimaryKey(liquibase.structure.core.PrimaryKey) MySQLDatabase(liquibase.database.core.MySQLDatabase) Change(liquibase.change.Change) CreateTableChange(liquibase.change.core.CreateTableChange) PostgresDatabase(liquibase.database.core.PostgresDatabase) DatabaseDataType(liquibase.datatype.DatabaseDataType) Column(liquibase.structure.core.Column) CreateTableChange(liquibase.change.core.CreateTableChange) ConstraintsConfig(liquibase.change.ConstraintsConfig) BigInteger(java.math.BigInteger) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 14 with DatabaseDataType

use of liquibase.datatype.DatabaseDataType in project liquibase by liquibase.

the class UnknownType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    int dataTypeMaxParameters;
    if (getName().equalsIgnoreCase("enum") || getName().equalsIgnoreCase("set")) {
        dataTypeMaxParameters = Integer.MAX_VALUE;
    } else {
        dataTypeMaxParameters = database.getDataTypeMaxParameters(getName());
    }
    Object[] parameters = getParameters();
    if (database instanceof OracleDatabase) {
        if (getName().equalsIgnoreCase("LONG") || getName().equalsIgnoreCase("BFILE") || getName().equalsIgnoreCase("ROWID") || getName().equalsIgnoreCase("ANYDATA") || getName().equalsIgnoreCase("SDO_GEOMETRY")) {
            parameters = new Object[0];
        } else if (getName().equalsIgnoreCase("RAW")) {
            return new DatabaseDataType(getName(), parameters);
        } else if (getName().toUpperCase().startsWith("INTERVAL ")) {
            return new DatabaseDataType(getName().replaceAll("\\(\\d+\\)", ""));
        } else {
            //probably a user defined type. Can't call getUserDefinedTypes() to know for sure, since that returns all types including system types.
            return new DatabaseDataType(getName().toUpperCase());
        }
    }
    if (dataTypeMaxParameters < parameters.length) {
        parameters = Arrays.copyOfRange(parameters, 0, dataTypeMaxParameters);
    }
    DatabaseDataType type;
    if (database instanceof MSSQLDatabase) {
        type = new DatabaseDataType(database.escapeDataTypeName(getName()), parameters);
    } else {
        type = new DatabaseDataType(getName().toUpperCase(), parameters);
    }
    type.addAdditionalInformation(getAdditionalInformation());
    return type;
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) DatabaseDataType(liquibase.datatype.DatabaseDataType) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 15 with DatabaseDataType

use of liquibase.datatype.DatabaseDataType in project liquibase by liquibase.

the class VarcharType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    if (database instanceof OracleDatabase || (database instanceof HsqlDatabase && ((HsqlDatabase) database).isUsingOracleSyntax())) {
        return new DatabaseDataType("VARCHAR2", getParameters());
    }
    if (database instanceof InformixDatabase && getSize() > 255) {
        return new DatabaseDataType("LVARCHAR", getParameters());
    }
    if (database instanceof MSSQLDatabase) {
        Object[] parameters = getParameters();
        if (parameters.length > 0) {
            String param1 = parameters[0].toString();
            if (!param1.matches("\\d+") || new BigInteger(param1).compareTo(BigInteger.valueOf(8000L)) > 0) {
                try {
                    if (database.getDatabaseMajorVersion() <= 8) {
                        //2000 or earlier
                        return new DatabaseDataType(database.escapeDataTypeName("varchar"), "8000");
                    }
                }//assuming it is a newer version
                 catch (DatabaseException ignore) {
                }
                DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("varchar"), "MAX");
                type.addAdditionalInformation(getAdditionalInformation());
                return type;
            }
        }
        if (parameters.length == 0) {
            parameters = new Object[] { 1 };
        } else if (parameters.length > 1) {
            parameters = Arrays.copyOfRange(parameters, 0, 1);
        }
        DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("varchar"), parameters);
        type.addAdditionalInformation(getAdditionalInformation());
        return type;
    } else if (database instanceof PostgresDatabase) {
        if (getParameters() != null && getParameters().length == 1 && getParameters()[0].toString().equals("2147483647")) {
            DatabaseDataType type = new DatabaseDataType("CHARACTER");
            type.addAdditionalInformation("VARYING");
            return type;
        }
    }
    return super.toDatabaseDataType(database);
}
Also used : DatabaseDataType(liquibase.datatype.DatabaseDataType) BigInteger(java.math.BigInteger) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

DatabaseDataType (liquibase.datatype.DatabaseDataType)17 BigInteger (java.math.BigInteger)6 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 DatabaseException (liquibase.exception.DatabaseException)4 GlobalConfiguration (liquibase.configuration.GlobalConfiguration)2 OracleDatabase (liquibase.database.core.OracleDatabase)2 PostgresDatabase (liquibase.database.core.PostgresDatabase)2 Sql (liquibase.sql.Sql)2 UnparsedSql (liquibase.sql.UnparsedSql)2 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Change (liquibase.change.Change)1 ColumnConfig (liquibase.change.ColumnConfig)1 ConstraintsConfig (liquibase.change.ConstraintsConfig)1 CreateTableChange (liquibase.change.core.CreateTableChange)1 HsqlDatabase (liquibase.database.core.HsqlDatabase)1 MySQLDatabase (liquibase.database.core.MySQLDatabase)1 LiquibaseDataType (liquibase.datatype.LiquibaseDataType)1 ForeignKeyConstraint (liquibase.statement.ForeignKeyConstraint)1