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<>();
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 ObjectUtil method setProperty.
/**
* Tries to guess the "real" data type of propertyValue by the given propertyName, then sets the
* selected property of the given object to that value.
* @param object the object whose property should be set
* @param propertyName name of the property to set
* @param propertyValue new value of the property, as String
*/
public static void setProperty(Object object, String propertyName, String propertyValue) {
Method method = getWriteMethod(object, propertyName);
if (method == null) {
throw new UnexpectedLiquibaseException(String.format("Property [%s] was not found for object type [%s]", propertyName, object.getClass().getName()));
}
Class<?> parameterType = method.getParameterTypes()[0];
Object finalValue = propertyValue;
if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) {
finalValue = Boolean.valueOf(propertyValue);
} else if (parameterType.equals(Integer.class)) {
finalValue = Integer.valueOf(propertyValue);
} else if (parameterType.equals(Long.class)) {
finalValue = Long.valueOf(propertyValue);
} else if (parameterType.equals(BigInteger.class)) {
finalValue = new BigInteger(propertyValue);
} else if (parameterType.equals(BigDecimal.class)) {
finalValue = new BigDecimal(propertyValue);
} else if (parameterType.equals(DatabaseFunction.class)) {
finalValue = new DatabaseFunction(propertyValue);
} else if (parameterType.equals(SequenceNextValueFunction.class)) {
finalValue = new SequenceNextValueFunction(propertyValue);
} else if (parameterType.equals(SequenceCurrentValueFunction.class)) {
finalValue = new SequenceCurrentValueFunction(propertyValue);
} else if (Enum.class.isAssignableFrom(parameterType)) {
finalValue = Enum.valueOf((Class<Enum>) parameterType, propertyValue);
}
try {
method.invoke(object, finalValue);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new UnexpectedLiquibaseException(e);
} catch (IllegalArgumentException e) {
if (finalValue != null) {
throw new UnexpectedLiquibaseException("Cannot call " + method.toString() + " with value of type " + finalValue.getClass().getName());
} else {
throw new UnexpectedLiquibaseException("Cannot call " + method.toString() + " with a null argument");
}
}
}
use of liquibase.statement.DatabaseFunction in project liquibase by liquibase.
the class MarkChangeSetRanGenerator method generateSql.
@Override
public Sql[] generateSql(MarkChangeSetRanStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
String dateValue = database.getCurrentDateTimeFunction();
ChangeSet changeSet = statement.getChangeSet();
SqlStatement runStatement;
// use LEGACY quoting since we're dealing with system objects
ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
try {
try {
if (statement.getExecType().equals(ChangeSet.ExecType.FAILED) || statement.getExecType().equals(ChangeSet.ExecType.SKIPPED)) {
// don't mark
return new Sql[0];
}
String tag = null;
for (Change change : changeSet.getChanges()) {
if (change instanceof TagDatabaseChange) {
TagDatabaseChange tagChange = (TagDatabaseChange) change;
tag = tagChange.getTag();
}
}
if (statement.getExecType().ranBefore) {
runStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addNewColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addNewColumnValue("EXECTYPE", statement.getExecType().value).addNewColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId()).addNewColumnValue(COMMENTS, getCommentsColumn(changeSet)).addNewColumnValue(CONTEXTS, getContextsColumn(changeSet)).addNewColumnValue(LABELS, getLabelsColumn(changeSet)).setWhereClause(database.escapeObjectName("ID", LiquibaseColumn.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", LiquibaseColumn.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", LiquibaseColumn.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
if (tag != null) {
((UpdateStatement) runStatement).addNewColumnValue("TAG", tag);
}
} else {
runStatement = new InsertStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addColumnValue("ID", changeSet.getId()).addColumnValue("AUTHOR", changeSet.getAuthor()).addColumnValue("FILENAME", changeSet.getFilePath()).addColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addColumnValue("DESCRIPTION", limitSize(changeSet.getDescription())).addColumnValue(COMMENTS, getCommentsColumn(changeSet)).addColumnValue("EXECTYPE", statement.getExecType().value).addColumnValue(CONTEXTS, getContextsColumn(changeSet)).addColumnValue(LABELS, getLabelsColumn(changeSet)).addColumnValue("LIQUIBASE", StringUtil.limitSize(LiquibaseUtil.getBuildVersion().replaceAll("SNAPSHOT", "SNP").replaceAll("beta", "b").replaceAll("alpha", "b"), 20)).addColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId());
if (tag != null) {
((InsertStatement) runStatement).addColumnValue("TAG", tag);
}
}
} catch (LiquibaseException e) {
throw new UnexpectedLiquibaseException(e);
}
return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
} finally {
database.setObjectQuotingStrategy(currentStrategy);
}
}
use of liquibase.statement.DatabaseFunction in project liquibase by liquibase.
the class LockDatabaseChangeLogGenerator method generateSql.
@Override
public Sql[] generateSql(LockDatabaseChangeLogStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
String liquibaseSchema = database.getLiquibaseSchemaName();
String liquibaseCatalog = database.getLiquibaseCatalogName();
// use LEGACY quoting since we're dealing with system objects
ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
try {
String dateValue = database.getCurrentDateTimeFunction();
UpdateStatement updateStatement = new UpdateStatement(liquibaseCatalog, liquibaseSchema, database.getDatabaseChangeLogLockTableName());
updateStatement.addNewColumnValue("LOCKED", true);
updateStatement.addNewColumnValue("LOCKGRANTED", new DatabaseFunction(dateValue));
updateStatement.addNewColumnValue("LOCKEDBY", hostname + hostDescription + " (" + hostaddress + ")");
updateStatement.setWhereClause(database.escapeColumnName(liquibaseCatalog, liquibaseSchema, database.getDatabaseChangeLogTableName(), "ID") + " = 1 AND " + database.escapeColumnName(liquibaseCatalog, liquibaseSchema, database.getDatabaseChangeLogTableName(), "LOCKED") + " = " + DataTypeFactory.getInstance().fromDescription("boolean", database).objectToSql(false, database));
return SqlGeneratorFactory.getInstance().generateSql(updateStatement, database);
} finally {
database.setObjectQuotingStrategy(currentStrategy);
}
}
use of liquibase.statement.DatabaseFunction in project liquibase by liquibase.
the class UpdateGenerator method convertToString.
private String convertToString(Object newValue, Database database) {
String sqlString;
if ((newValue == null) || "NULL".equalsIgnoreCase(newValue.toString())) {
sqlString = "NULL";
} else if ((newValue instanceof String) && !looksLikeFunctionCall(((String) newValue), database)) {
sqlString = DataTypeFactory.getInstance().fromObject(newValue, database).objectToSql(newValue, database);
} else if (newValue instanceof Date) {
// converting java.util.Date to java.sql.Date
Date date = (Date) newValue;
if (date.getClass().equals(java.util.Date.class)) {
date = new java.sql.Date(date.getTime());
}
sqlString = database.getDateLiteral(date);
} else if (newValue instanceof Boolean) {
if (((Boolean) newValue)) {
sqlString = DataTypeFactory.getInstance().getTrueBooleanValue(database);
} else {
sqlString = DataTypeFactory.getInstance().getFalseBooleanValue(database);
}
} else if (newValue instanceof DatabaseFunction) {
sqlString = database.generateDatabaseFunctionValue((DatabaseFunction) newValue);
} else {
sqlString = newValue.toString();
}
return sqlString;
}
Aggregations