Search in sources :

Example 1 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<>();
    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 2 with DatabaseFunction

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");
        }
    }
}
Also used : SequenceCurrentValueFunction(liquibase.statement.SequenceCurrentValueFunction) DatabaseFunction(liquibase.statement.DatabaseFunction) Method(java.lang.reflect.Method) BigDecimal(java.math.BigDecimal) InvocationTargetException(java.lang.reflect.InvocationTargetException) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 3 with DatabaseFunction

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);
    }
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) DatabaseFunction(liquibase.statement.DatabaseFunction) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) Change(liquibase.change.Change) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) InsertStatement(liquibase.statement.core.InsertStatement) Sql(liquibase.sql.Sql) SqlStatement(liquibase.statement.SqlStatement) LiquibaseColumn(liquibase.changelog.column.LiquibaseColumn) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ChangeSet(liquibase.changelog.ChangeSet) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 4 with DatabaseFunction

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);
    }
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) DatabaseFunction(liquibase.statement.DatabaseFunction) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 5 with DatabaseFunction

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;
}
Also used : DatabaseFunction(liquibase.statement.DatabaseFunction) Date(java.util.Date)

Aggregations

DatabaseFunction (liquibase.statement.DatabaseFunction)24 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)6 BigInteger (java.math.BigInteger)5 Date (java.util.Date)5 LiquibaseDataType (liquibase.datatype.LiquibaseDataType)5 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)5 DatabaseObject (liquibase.structure.DatabaseObject)5 ParseException (java.text.ParseException)4 SqlStatement (liquibase.statement.SqlStatement)4 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ArrayList (java.util.ArrayList)3 InformixDatabase (liquibase.database.core.InformixDatabase)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 BooleanType (liquibase.datatype.core.BooleanType)3 UpdateStatement (liquibase.statement.core.UpdateStatement)3 Method (java.lang.reflect.Method)2 BigDecimal (java.math.BigDecimal)2 Calendar (java.util.Calendar)2 Change (liquibase.change.Change)2