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));
}
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);
}
}
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));
}
}
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);
}
}
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);
}
}
Aggregations