liquibase.ext.databricks.sqlgenerator.UpdateGeneratorDatabricks Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-databricks Show documentation
Show all versions of liquibase-databricks Show documentation
Liquibase Extension for Databricks.
The newest version!
package liquibase.ext.databricks.sqlgenerator;
import liquibase.database.Database;
import liquibase.datatype.DataTypeFactory;
import liquibase.sql.Sql;
import liquibase.sql.UnparsedSql;
import liquibase.sqlgenerator.SqlGeneratorChain;
import liquibase.sqlgenerator.core.UpdateGenerator;
import liquibase.statement.DatabaseFunction;
import liquibase.statement.core.UpdateStatement;
import liquibase.util.SqlUtil;
import java.util.Date;
import liquibase.ext.databricks.database.DatabricksDatabase;
public class UpdateGeneratorDatabricks extends UpdateGenerator {
public UpdateGeneratorDatabricks() {
super();
}
@Override
public boolean supports(UpdateStatement statement, Database database) {
return super.supports(statement, database) && (database instanceof DatabricksDatabase);
}
@Override
public int getPriority() {
return PRIORITY_DATABASE;
}
@Override
public Sql[] generateSql(UpdateStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
StringBuilder sql = new StringBuilder("UPDATE ")
.append(database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()))
.append(" SET");
for (String column : statement.getNewColumnValues().keySet()) {
sql.append(" ")
.append(database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), column))
.append(" = ")
.append(this.convertObjectToString(statement.getNewColumnValues().get(column), database))
.append(",");
}
int lastComma = sql.lastIndexOf(",");
if (lastComma >= 0) {
sql.deleteCharAt(lastComma);
}
if (statement.getWhereClause() != null) {
sql.append(" WHERE ").append(SqlUtil.replacePredicatePlaceholders(database, statement.getWhereClause(), statement.getWhereColumnNames(), statement.getWhereParameters()));
}
return new Sql[]{
new UnparsedSql(sql.toString(), getAffectedTable(statement))
};
}
private String convertObjectToString(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;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy