use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class SelectFromDatabaseChangeLogGenerator method generateSql.
@Override
public Sql[] generateSql(SelectFromDatabaseChangeLogStatement statement, final Database database, SqlGeneratorChain sqlGeneratorChain) {
List<ColumnConfig> columnsToSelect = Arrays.asList(statement.getColumnsToSelect());
String sql = "SELECT " + (database instanceof MSSQLDatabase && statement.getLimit() != null ? "TOP " + statement.getLimit() + " " : "") + StringUtils.join(columnsToSelect, ",", new StringUtils.StringUtilsFormatter<ColumnConfig>() {
@Override
public String toString(ColumnConfig column) {
if (column.getComputed() != null && column.getComputed()) {
return column.getName();
} else {
return database.escapeColumnName(null, null, null, column.getName());
}
}
}).toUpperCase() + " FROM " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName());
SelectFromDatabaseChangeLogStatement.WhereClause whereClause = statement.getWhereClause();
if (whereClause != null) {
if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByTag) {
sql += " WHERE " + database.escapeColumnName(null, null, null, "TAG") + "='" + ((SelectFromDatabaseChangeLogStatement.ByTag) whereClause).getTagName() + "'";
} else if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByNotNullCheckSum) {
sql += " WHERE " + database.escapeColumnName(null, null, null, "MD5SUM") + " IS NOT NULL";
} else {
throw new UnexpectedLiquibaseException("Unknown where clause type: " + whereClause.getClass().getName());
}
}
if (statement.getOrderByColumns() != null && statement.getOrderByColumns().length > 0) {
sql += " ORDER BY ";
Iterator<String> orderBy = Arrays.asList(statement.getOrderByColumns()).iterator();
while (orderBy.hasNext()) {
String orderColumn = orderBy.next();
String[] orderColumnData = orderColumn.split(" ");
sql += database.escapeColumnName(null, null, null, orderColumnData[0]);
if (orderColumnData.length == 2) {
sql += " ";
sql += orderColumnData[1].toUpperCase();
}
if (orderBy.hasNext()) {
sql += ", ";
}
}
}
if (statement.getLimit() != null) {
if (database instanceof OracleDatabase) {
if (whereClause == null) {
sql += " WHERE ROWNUM=" + statement.getLimit();
} else {
sql += " AND ROWNUM=" + statement.getLimit();
}
} else if (database instanceof MySQLDatabase || database instanceof PostgresDatabase) {
sql += " LIMIT " + statement.getLimit();
} else if (database instanceof DB2Database) {
sql += " FETCH FIRST " + statement.getLimit() + " ROWS ONLY";
}
}
return new Sql[] { new UnparsedSql(sql) };
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class MD5Util method computeMD5.
public static String computeMD5(String input) {
if (input == null) {
return null;
}
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
digest.update(input.getBytes(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()));
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
byte[] digestBytes = digest.digest();
String returnString = new String(encodeHex(digestBytes));
String inputToLog = input;
if (inputToLog.length() > 500) {
inputToLog = inputToLog.substring(0, 500) + "... [truncated in log]";
}
LogFactory.getLogger().debug("Computed checksum for " + inputToLog + " as " + returnString);
return returnString;
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class ObjectUtil method setProperty.
public static void setProperty(Object object, String propertyName, String propertyValue) {
Method method = getWriteMethod(object, propertyName);
if (method == null) {
throw new UnexpectedLiquibaseException("Property '" + propertyName + "' not found on object type " + 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 e) {
throw new UnexpectedLiquibaseException(e);
} catch (IllegalArgumentException e) {
throw new UnexpectedLiquibaseException("Cannot call " + method.toString() + " with value of type " + finalValue.getClass().getName());
} catch (InvocationTargetException e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class AbstractExecuteTest method resetAvailableDatabases.
public void resetAvailableDatabases() throws Exception {
for (Database database : DatabaseTestContext.getInstance().getAvailableDatabases()) {
DatabaseConnection connection = database.getConnection();
Statement connectionStatement = ((JdbcConnection) connection).getUnderlyingConnection().createStatement();
try {
database.dropDatabaseObjects(CatalogAndSchema.DEFAULT);
} catch (Throwable e) {
throw new UnexpectedLiquibaseException("Error dropping objects for database " + database.getShortName(), e);
}
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()));
} catch (SQLException e) {
;
}
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()));
} catch (SQLException e) {
;
}
connection.commit();
if (database.supportsSchemas()) {
database.dropDatabaseObjects(new CatalogAndSchema(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA));
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, database.getDatabaseChangeLogLockTableName()));
} catch (SQLException e) {
//ok
}
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, database.getDatabaseChangeLogTableName()));
} catch (SQLException e) {
//ok
}
connection.commit();
}
List<? extends SqlStatement> setupStatements = setupStatements(database);
if (setupStatements != null) {
for (SqlStatement statement : setupStatements) {
ExecutorService.getInstance().getExecutor(database).execute(statement);
}
}
connectionStatement.close();
}
}
use of liquibase.exception.UnexpectedLiquibaseException in project dropwizard by dropwizard.
the class DbDumpCommand method generateChangeLog.
private void generateChangeLog(final Database database, final CatalogAndSchema catalogAndSchema, final DiffToChangeLog changeLogWriter, PrintStream outputStream, final Set<Class<? extends DatabaseObject>> compareTypes) throws DatabaseException, IOException, ParserConfigurationException {
@SuppressWarnings({ "unchecked", "rawtypes" }) final SnapshotControl snapshotControl = new SnapshotControl(database, compareTypes.toArray(new Class[compareTypes.size()]));
final CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema) }, compareTypes);
final CatalogAndSchema[] compareControlSchemas = compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE);
try {
final DatabaseSnapshot referenceSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControlSchemas, database, snapshotControl);
final DatabaseSnapshot comparisonSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControlSchemas, null, snapshotControl);
final DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(referenceSnapshot, comparisonSnapshot, compareControl);
changeLogWriter.setDiffResult(diffResult);
changeLogWriter.print(outputStream);
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
Aggregations