Search in sources :

Example 1 with DateTimeType

use of liquibase.datatype.core.DateTimeType in project liquibase by liquibase.

the class MissingTableChangeGenerator method setDefaultValue.

public static void setDefaultValue(ColumnConfig columnConfig, Column column, Database database) {
    LiquibaseDataType dataType = DataTypeFactory.getInstance().from(column.getType(), database);
    Object defaultValue = column.getDefaultValue();
    if (defaultValue == null) {
    // do nothing
    } else if (column.isAutoIncrement()) {
    // do nothing
    } else if (defaultValue instanceof Date) {
        columnConfig.setDefaultValueDate((Date) defaultValue);
    } else if (defaultValue instanceof Boolean) {
        columnConfig.setDefaultValueBoolean(((Boolean) defaultValue));
    } else if (defaultValue instanceof Number) {
        columnConfig.setDefaultValueNumeric(((Number) defaultValue));
    } else if (defaultValue instanceof DatabaseFunction) {
        DatabaseFunction function = (DatabaseFunction) defaultValue;
        if ("current".equals(function.getValue())) {
            if (database instanceof InformixDatabase) {
                if (dataType instanceof DateTimeType) {
                    if (dataType.getAdditionalInformation() == null || dataType.getAdditionalInformation().length() == 0) {
                        if (dataType.getParameters() != null && dataType.getParameters().length > 0) {
                            String parameter = String.valueOf(dataType.getParameters()[0]);
                            if ("4365".equals(parameter)) {
                                function = new DatabaseFunction("current year to fraction(3)");
                            }
                            if ("3594".equals(parameter)) {
                                function = new DatabaseFunction("current year to second");
                            }
                            if ("3080".equals(parameter)) {
                                function = new DatabaseFunction("current year to minute");
                            }
                            if ("2052".equals(parameter)) {
                                function = new DatabaseFunction("current year to day");
                            }
                        }
                    }
                }
            }
        }
        columnConfig.setDefaultValueComputed(function);
    } else {
        String defaultValueString = null;
        try {
            defaultValueString = DataTypeFactory.getInstance().from(column.getType(), database).objectToSql(defaultValue, database);
        } catch (NullPointerException e) {
            throw e;
        }
        if (defaultValueString != null) {
            defaultValueString = defaultValueString.replaceFirst("'", "").replaceAll("'$", "");
        }
        columnConfig.setDefaultValue(defaultValueString);
    }
    columnConfig.setDefaultValueConstraintName(column.getDefaultValueConstraintName());
}
Also used : DateTimeType(liquibase.datatype.core.DateTimeType) InformixDatabase(liquibase.database.core.InformixDatabase) DatabaseFunction(liquibase.statement.DatabaseFunction) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) DatabaseObject(liquibase.structure.DatabaseObject) Date(java.util.Date)

Aggregations

Date (java.util.Date)1 InformixDatabase (liquibase.database.core.InformixDatabase)1 LiquibaseDataType (liquibase.datatype.LiquibaseDataType)1 DateTimeType (liquibase.datatype.core.DateTimeType)1 DatabaseFunction (liquibase.statement.DatabaseFunction)1 DatabaseObject (liquibase.structure.DatabaseObject)1