Search in sources :

Example 11 with DatabaseFunction

use of liquibase.statement.DatabaseFunction in project liquibase by liquibase.

the class JsonChangeLogSerializerTest method serialize_changeSet.

@Test
public void serialize_changeSet() {
    AddColumnChange addColumnChange = new AddColumnChange();
    addColumnChange.setCatalogName("cat");
    addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col1").setDefaultValueNumeric(3));
    addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col2").setDefaultValueComputed(new DatabaseFunction("NOW()")));
    addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col3").setDefaultValueBoolean(true));
    addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col2").setDefaultValueDate(new Date(0)));
    addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col2").setDefaultValueSequenceNext(new SequenceNextValueFunction("seq_me")));
    ChangeSet changeSet = new ChangeSet("1", "nvoxland", false, false, "path/to/file.json", null, null, null);
    changeSet.addChange(addColumnChange);
    assertEquals("{\n" + "  \"changeSet\": {\n" + "    \"id\": \"1\",\n" + "    \"author\": \"nvoxland\",\n" + "    \"objectQuotingStrategy\": \"LEGACY\",\n" + "    \"changes\": [\n" + "      {\n" + "        \"addColumn\": {\n" + "          \"catalogName\": \"cat\",\n" + "          \"columns\": [\n" + "            {\n" + "              \"column\": {\n" + "                \"defaultValueNumeric\": 3,\n" + "                \"name\": \"col1\"\n" + "              }\n" + "            },\n" + "            {\n" + "              \"column\": {\n" + "                \"defaultValueComputed\": \"NOW()\",\n" + "                \"name\": \"col2\"\n" + "              }\n" + "            },\n" + "            {\n" + "              \"column\": {\n" + "                \"defaultValueBoolean\": true,\n" + "                \"name\": \"col3\"\n" + "              }\n" + "            },\n" + "            {\n" + "              \"column\": {\n" + "                \"defaultValueDate\": 1970-01-01T00:00:00Z,\n" + "                \"name\": \"col2\"\n" + "              }\n" + "            },\n" + "            {\n" + "              \"column\": {\n" + "                \"defaultValueSequenceNext\": \"seq_me\",\n" + "                \"name\": \"col2\"\n" + "              }\n" + "            }]\n" + "          \n" + "        }\n" + "      }]\n" + "    \n" + "  }\n" + "}\n", new JsonChangeLogSerializer().serialize(changeSet, true));
}
Also used : DatabaseFunction(liquibase.statement.DatabaseFunction) AddColumnConfig(liquibase.change.AddColumnConfig) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) AddColumnChange(liquibase.change.core.AddColumnChange) ChangeSet(liquibase.changelog.ChangeSet) Date(java.util.Date) Test(org.junit.Test)

Example 12 with DatabaseFunction

use of liquibase.statement.DatabaseFunction in project opennms by OpenNMS.

the class AddNamedAutoIncrementChange method generateStatements.

@Override
public SqlStatement[] generateStatements(final Database database) {
    final List<SqlStatement> statements = new ArrayList<SqlStatement>();
    if (database instanceof PostgresDatabase) {
        String sequenceName = m_sequenceName;
        if (m_sequenceName == null) {
            sequenceName = (getTableName() + "_" + getColumnName() + "_seq").toLowerCase();
            statements.add(new CreateSequenceStatement(getSchemaName(), sequenceName));
        }
        statements.add(new SetNullableStatement(getSchemaName(), getTableName(), getColumnName(), null, false));
        statements.add(new AddDefaultValueStatement(getSchemaName(), getTableName(), getColumnName(), getColumnDataType(), new DatabaseFunction("NEXTVAL('" + sequenceName + "')")));
        return statements.toArray(new SqlStatement[0]);
    } else {
        return super.generateStatements(database);
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) PostgresDatabase(liquibase.database.core.PostgresDatabase) SetNullableStatement(liquibase.statement.core.SetNullableStatement) DatabaseFunction(liquibase.statement.DatabaseFunction) CreateSequenceStatement(liquibase.statement.core.CreateSequenceStatement) ArrayList(java.util.ArrayList) AddDefaultValueStatement(liquibase.statement.core.AddDefaultValueStatement)

Example 13 with DatabaseFunction

use of liquibase.statement.DatabaseFunction in project liquibase by liquibase.

the class AbstractJdbcDatabase method setCurrentDateTimeFunction.

// ------- DATABASE-SPECIFIC SQL METHODS ---- //
@Override
public void setCurrentDateTimeFunction(final String function) {
    if (function != null) {
        this.currentDateTimeFunction = function;
        this.dateFunctions.add(new DatabaseFunction(function));
    }
}
Also used : DatabaseFunction(liquibase.statement.DatabaseFunction)

Example 14 with DatabaseFunction

use of liquibase.statement.DatabaseFunction in project liquibase by liquibase.

the class DateTimeType method sqlToObject.

@Override
public Object sqlToObject(String value, Database database) {
    if (zeroTime(value)) {
        return value;
    }
    if (database instanceof DB2Database) {
        return value.replaceFirst("^\"SYSIBM\".\"TIMESTAMP\"\\('", "").replaceFirst("'\\)", "");
    }
    if (database instanceof DerbyDatabase) {
        return value.replaceFirst("^TIMESTAMP\\('", "").replaceFirst("'\\)", "");
    }
    try {
        DateFormat dateTimeFormat = getDateTimeFormat(database);
        if (database instanceof OracleDatabase && value.matches("to_date\\('\\d+\\-\\d+\\-\\d+ \\d+:\\d+:\\d+', 'YYYY\\-MM\\-DD HH24:MI:SS'\\)")) {
            dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:s");
            value = value.replaceFirst(".*?'", "").replaceFirst("',.*", "");
        }
        return new Timestamp(dateTimeFormat.parse(value).getTime());
    } catch (ParseException e) {
        String[] genericFormats = new String[] { "yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'HH:mm:ss" };
        for (String format : genericFormats) {
            try {
                return new Timestamp(new SimpleDateFormat(format).parse(value).getTime());
            } catch (ParseException ignore) {
            //doesn't match
            }
        }
        if (value.contains("/") || value.contains("-")) {
            //maybe a custom format the database expects. Just return it.
            return value;
        }
        return new DatabaseFunction(value);
    }
}
Also used : DatabaseFunction(liquibase.statement.DatabaseFunction) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp)

Example 15 with DatabaseFunction

use of liquibase.statement.DatabaseFunction in project liquibase by liquibase.

the class TimeType method sqlToObject.

@Override
public Object sqlToObject(String value, Database database) {
    if (zeroTime(value)) {
        return value;
    }
    if (database instanceof DB2Database) {
        return value.replaceFirst("^\"SYSIBM\".\"TIME\"\\('", "").replaceFirst("'\\)", "");
    }
    if (database instanceof DerbyDatabase) {
        return value.replaceFirst("^TIME\\('", "").replaceFirst("'\\)", "");
    }
    try {
        DateFormat timeFormat = getTimeFormat(database);
        if (database instanceof OracleDatabase && value.matches("to_date\\('\\d+:\\d+:\\d+', 'HH24:MI:SS'\\)")) {
            timeFormat = new SimpleDateFormat("HH:mm:s");
            value = value.replaceFirst(".*?'", "").replaceFirst("',.*", "");
        }
        return new java.sql.Time(timeFormat.parse(value).getTime());
    } catch (ParseException e) {
        return new DatabaseFunction(value);
    }
}
Also used : DatabaseFunction(liquibase.statement.DatabaseFunction) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

DatabaseFunction (liquibase.statement.DatabaseFunction)21 Date (java.util.Date)7 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)6 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)5 DatabaseObject (liquibase.structure.DatabaseObject)5 BigInteger (java.math.BigInteger)4 LiquibaseDataType (liquibase.datatype.LiquibaseDataType)4 DateFormat (java.text.DateFormat)3 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Method (java.lang.reflect.Method)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Change (liquibase.change.Change)2 ChangeSet (liquibase.changelog.ChangeSet)2 InformixDatabase (liquibase.database.core.InformixDatabase)2 SequenceCurrentValueFunction (liquibase.statement.SequenceCurrentValueFunction)2 SqlStatement (liquibase.statement.SqlStatement)2 RawSqlStatement (liquibase.statement.core.RawSqlStatement)2 Column (liquibase.structure.core.Column)2