Search in sources :

Example 1 with DatabaseDataType

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

the class TimestampType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    String originalDefinition = StringUtils.trimToEmpty(getRawDefinition());
    if (database instanceof MySQLDatabase) {
        if (getRawDefinition().contains(" ") || getRawDefinition().contains("(")) {
            return new DatabaseDataType(getRawDefinition());
        }
        return super.toDatabaseDataType(database);
    }
    if (database instanceof MSSQLDatabase) {
        if (!LiquibaseConfiguration.getInstance().getProperty(GlobalConfiguration.class, GlobalConfiguration.CONVERT_DATA_TYPES).getValue(Boolean.class) && originalDefinition.toLowerCase().startsWith("timestamp")) {
            return new DatabaseDataType(database.escapeDataTypeName("timestamp"));
        }
        return new DatabaseDataType(database.escapeDataTypeName("datetime"));
    }
    if (database instanceof SybaseDatabase) {
        return new DatabaseDataType(database.escapeDataTypeName("datetime"));
    }
    if (database instanceof DB2Database) {
        Object[] parameters = getParameters();
        if (parameters != null && parameters.length > 1) {
            parameters = new Object[] { parameters[1] };
        }
        return new DatabaseDataType(database.escapeDataTypeName("timestamp"), parameters);
    }
    if (getAdditionalInformation() != null && (database instanceof PostgresDatabase || database instanceof OracleDatabase) || database instanceof HsqlDatabase) {
        DatabaseDataType type = new DatabaseDataType("TIMESTAMP");
        String additionalInformation = this.getAdditionalInformation();
        if (additionalInformation != null && database instanceof PostgresDatabase) {
            if (additionalInformation.toUpperCase().contains("TIMEZONE")) {
                additionalInformation = additionalInformation.toUpperCase().replace("TIMEZONE", "TIME ZONE");
            }
        }
        type.addAdditionalInformation(additionalInformation);
        return type;
    }
    return super.toDatabaseDataType(database);
}
Also used : GlobalConfiguration(liquibase.configuration.GlobalConfiguration) DatabaseDataType(liquibase.datatype.DatabaseDataType)

Example 2 with DatabaseDataType

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

the class TinyIntType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    if (database instanceof MSSQLDatabase) {
        return new DatabaseDataType(database.escapeDataTypeName("tinyint"));
    }
    if (database instanceof DerbyDatabase || database instanceof PostgresDatabase || database instanceof FirebirdDatabase || database instanceof DB2Database) {
        return new DatabaseDataType("SMALLINT");
    }
    if (database instanceof MySQLDatabase) {
        DatabaseDataType type = new DatabaseDataType("TINYINT", getParameters());
        type.addAdditionalInformation(getAdditionalInformation());
        return type;
    }
    if (database instanceof OracleDatabase) {
        return new DatabaseDataType("NUMBER", 3);
    }
    return super.toDatabaseDataType(database);
}
Also used : DatabaseDataType(liquibase.datatype.DatabaseDataType)

Example 3 with DatabaseDataType

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

the class BigIntType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    if (database instanceof InformixDatabase) {
        if (isAutoIncrement()) {
            return new DatabaseDataType("SERIAL8");
        } else {
            return new DatabaseDataType("INT8");
        }
    }
    if (database instanceof OracleDatabase) {
        return new DatabaseDataType("NUMBER", 38, 0);
    }
    if (database instanceof MSSQLDatabase) {
        return new DatabaseDataType(database.escapeDataTypeName("bigint"));
    }
    if (database instanceof MySQLDatabase) {
        DatabaseDataType type = new DatabaseDataType("BIGINT");
        type.addAdditionalInformation(getAdditionalInformation());
        return type;
    }
    if (database instanceof DB2Database || database instanceof DerbyDatabase || database instanceof HsqlDatabase || database instanceof FirebirdDatabase) {
        return new DatabaseDataType("BIGINT");
    }
    if (database instanceof PostgresDatabase) {
        if (isAutoIncrement()) {
            return new DatabaseDataType("BIGSERIAL");
        }
    }
    return super.toDatabaseDataType(database);
}
Also used : DatabaseDataType(liquibase.datatype.DatabaseDataType)

Example 4 with DatabaseDataType

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

the class DateTimeType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    String originalDefinition = StringUtils.trimToEmpty(getRawDefinition());
    boolean allowFractional = supportsFractionalDigits(database);
    if (database instanceof DerbyDatabase || database instanceof FirebirdDatabase || database instanceof H2Database || database instanceof HsqlDatabase) {
        return new DatabaseDataType("TIMESTAMP");
    }
    if (database instanceof DB2Database || database instanceof OracleDatabase) {
        return new DatabaseDataType("TIMESTAMP", getParameters());
    }
    if (database instanceof MSSQLDatabase) {
        Object[] parameters = getParameters();
        if (originalDefinition.toLowerCase().startsWith("smalldatetime") || originalDefinition.toLowerCase().startsWith("[smalldatetime")) {
            return new DatabaseDataType(database.escapeDataTypeName("smalldatetime"));
        } else if (originalDefinition.equalsIgnoreCase("datetime2") || originalDefinition.equals("[datetime2]") || originalDefinition.matches("(?i)datetime2\\s*\\(.+") || originalDefinition.matches("\\[datetime2\\]\\s*\\(.+")) {
            try {
                if (database.getDatabaseMajorVersion() <= 9) {
                    //2005 or earlier
                    return new DatabaseDataType(database.escapeDataTypeName("datetime"));
                }
            }//assuming it is a newer version
             catch (DatabaseException ignore) {
            }
            if (parameters.length == 0) {
                parameters = new Object[] { 7 };
            } else if (parameters.length > 1) {
                parameters = new Object[] { parameters[1] };
            }
            return new DatabaseDataType(database.escapeDataTypeName("datetime2"), parameters);
        }
        return new DatabaseDataType(database.escapeDataTypeName("datetime"));
    }
    if (database instanceof InformixDatabase) {
        // From database to changelog
        if (getAdditionalInformation() == null || getAdditionalInformation().length() == 0) {
            if (getParameters() != null && getParameters().length > 0) {
                String parameter = String.valueOf(getParameters()[0]);
                if ("4365".equals(parameter)) {
                    return new DatabaseDataType("DATETIME YEAR TO FRACTION(3)");
                }
                if ("3594".equals(parameter)) {
                    return new DatabaseDataType("DATETIME YEAR TO SECOND");
                }
                if ("3080".equals(parameter)) {
                    return new DatabaseDataType("DATETIME YEAR TO MINUTE");
                }
                if ("2052".equals(parameter)) {
                    return new DatabaseDataType("DATETIME YEAR TO DAY");
                }
            }
        }
        // From changelog to the database
        if (getAdditionalInformation() != null && getAdditionalInformation().length() > 0) {
            return new DatabaseDataType(originalDefinition);
        }
        return new DatabaseDataType("DATETIME YEAR TO FRACTION", 5);
    }
    if (database instanceof PostgresDatabase) {
        String rawDefinition = originalDefinition.toLowerCase();
        Object[] params = getParameters();
        if (rawDefinition.contains("tz") || rawDefinition.contains("with time zone")) {
            if (params.length == 0 || !allowFractional) {
                return new DatabaseDataType("TIMESTAMP WITH TIME ZONE");
            } else {
                Object param = params[0];
                if (params.length == 2) {
                    param = params[1];
                }
                return new DatabaseDataType("TIMESTAMP(" + param + ") WITH TIME ZONE");
            }
        } else {
            if (params.length == 0 || !allowFractional) {
                return new DatabaseDataType("TIMESTAMP WITHOUT TIME ZONE");
            } else {
                Object param = params[0];
                if (params.length == 2) {
                    param = params[1];
                }
                return new DatabaseDataType("TIMESTAMP(" + param + ") WITHOUT TIME ZONE");
            }
        }
    }
    if (database instanceof SQLiteDatabase) {
        return new DatabaseDataType("TEXT");
    }
    if (database instanceof MySQLDatabase) {
        if (getParameters().length == 0 || !allowFractional) {
            // fast out...
            return new DatabaseDataType(getName());
        }
        Object[] params = getParameters();
        Integer precision = Integer.valueOf(params[0].toString());
        if (precision > 6) {
            LogFactory.getInstance().getLog().warning("MySQL does not support a timestamp precision" + " of '" + precision + "' - resetting to" + " the maximum of '6'");
            params = new Object[] { 6 };
        }
        return new DatabaseDataType(getName(), params);
    }
    return new DatabaseDataType(getName());
}
Also used : DatabaseDataType(liquibase.datatype.DatabaseDataType) DatabaseException(liquibase.exception.DatabaseException)

Example 5 with DatabaseDataType

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

the class MediumIntType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    if (database instanceof MSSQLDatabase) {
        return new DatabaseDataType(database.escapeDataTypeName("int"));
    }
    if (database instanceof MySQLDatabase) {
        DatabaseDataType type = new DatabaseDataType("MEDIUMINT");
        type.addAdditionalInformation(getAdditionalInformation());
        return type;
    }
    if (database instanceof DB2Database || database instanceof DerbyDatabase || database instanceof FirebirdDatabase) {
        //always smallint regardless of parameters passed
        return new DatabaseDataType("MEDIUMINT");
    }
    return super.toDatabaseDataType(database);
}
Also used : DatabaseDataType(liquibase.datatype.DatabaseDataType)

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