use of liquibase.statement.core.RawSqlStatement in project liquibase by liquibase.
the class SqlPrecondition method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
DatabaseConnection connection = database.getConnection();
try {
Object oResult = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).queryForObject(new RawSqlStatement(getSql().replaceFirst(";$", "")), String.class);
if (oResult == null) {
throw new PreconditionFailedException("No rows returned from SQL Precondition", changeLog, this);
}
String result = oResult.toString();
String expectedResult = getExpectedResult();
if (!expectedResult.equals(result)) {
throw new PreconditionFailedException("SQL Precondition failed. Expected '" + expectedResult + "' got '" + result + "'", changeLog, this);
}
} catch (DatabaseException e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}
use of liquibase.statement.core.RawSqlStatement in project liquibase by liquibase.
the class InternalExecuteSqlCommandStep method run.
@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
CommandScope commandScope = resultsBuilder.getCommandScope();
Database database = commandScope.getArgumentValue(DATABASE_ARG);
String sql = commandScope.getArgumentValue(SQL_ARG);
String sqlFile = commandScope.getArgumentValue(SQLFILE_ARG);
Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
String sqlText;
if (sqlFile == null) {
sqlText = sql;
} else {
File file = new File(sqlFile);
if (!file.exists()) {
throw new LiquibaseException(String.format("The file '%s' does not exist", file.getCanonicalPath()));
}
sqlText = FileUtil.getContents(file);
}
String out = "";
String[] sqlStrings = StringUtil.processMultiLineSQL(sqlText, true, true, commandScope.getArgumentValue(DELIMITER_ARG));
for (String sqlString : sqlStrings) {
if (sqlString.toLowerCase().matches("\\s*select .*")) {
List<Map<String, ?>> rows = executor.queryForList(new RawSqlStatement(sqlString));
out += "Output of " + sqlString + ":\n";
if (rows.isEmpty()) {
out += "-- Empty Resultset --\n";
} else {
SortedSet<String> keys = new TreeSet<>();
for (Map<String, ?> row : rows) {
keys.addAll(row.keySet());
}
out += StringUtil.join(keys, " | ") + " |\n";
for (Map<String, ?> row : rows) {
for (String key : keys) {
out += row.get(key) + " | ";
}
out += "\n";
}
}
} else {
executor.execute(new RawSqlStatement(sqlString));
out += "Successfully Executed: " + sqlString + "\n";
}
out += "\n";
}
database.commit();
resultsBuilder.addResult("output", out.trim());
// Scope.getCurrentScope().getUI().sendMessage(out.trim());
}
use of liquibase.statement.core.RawSqlStatement in project liquibase by liquibase.
the class MSSQLDatabase method isCaseSensitive.
@Override
public boolean isCaseSensitive() {
if (caseSensitive == null) {
try {
if (getConnection() instanceof JdbcConnection) {
String catalog = getConnection().getCatalog();
String sql = "SELECT CONVERT([sysname], DATABASEPROPERTYEX(N'" + escapeStringForDatabase(catalog) + "', 'Collation'))";
String collation = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", this).queryForObject(new RawSqlStatement(sql), String.class);
caseSensitive = (collation != null) && !collation.contains("_CI_");
} else if (getConnection() instanceof OfflineConnection) {
caseSensitive = ((OfflineConnection) getConnection()).isCaseSensitive();
}
} catch (DatabaseException e) {
Scope.getCurrentScope().getLog(getClass()).warning("Cannot determine case sensitivity from MSSQL", e);
}
}
return (caseSensitive != null) && caseSensitive;
}
use of liquibase.statement.core.RawSqlStatement in project liquibase by liquibase.
the class MySQLDatabase method disableForeignKeyChecks.
@Override
public boolean disableForeignKeyChecks() throws DatabaseException {
boolean enabled = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", this).queryForInt(new RawSqlStatement("SELECT @@FOREIGN_KEY_CHECKS")) == 1;
Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", this).execute(new RawSqlStatement("SET FOREIGN_KEY_CHECKS=0"));
return enabled;
}
use of liquibase.statement.core.RawSqlStatement in project liquibase by liquibase.
the class AbstractSQLChangeTest method generateStatements_convertsEndingsOnSqlServer.
// @Test
// public void generateStatements_nullSqlMakesNoStatements() {
// assertEquals(0, new ExampleAbstractSQLChange(null).generateStatements(mock(Database.class)).length);
// }
//
// @Test
// public void generateStatements() {
// ExampleAbstractSQLChange change = new ExampleAbstractSQLChange("LINE 1;\n--a comment\nLINE 2;\nLINE 3;");
//
// change.setSplitStatements(true);
// change.setStripComments(true);
// SqlStatement[] statements = change.generateStatements(mock(Database.class));
// assertEquals(3, statements.length);
// assertEquals("LINE 1", ((RawSqlStatement) statements[0]).getSql());
// assertEquals("LINE 2", ((RawSqlStatement) statements[1]).getSql());
// assertEquals("LINE 3", ((RawSqlStatement) statements[2]).getSql());
// }
//
// @Test
// public void generateStatements_crlfEndingStandardizes() {
// ExampleAbstractSQLChange change = new ExampleAbstractSQLChange("LINE 1;\r\n--a comment\r\nLINE 2;\r\nLINE 3;");
//
// change.setSplitStatements(true);
// change.setStripComments(true);
// SqlStatement[] statements = change.generateStatements(mock(Database.class));
// assertEquals(3, statements.length);
// assertEquals("LINE 1", ((RawSqlStatement) statements[0]).getSql());
// assertEquals("LINE 2", ((RawSqlStatement) statements[1]).getSql());
// assertEquals("LINE 3", ((RawSqlStatement) statements[2]).getSql());
// }
@Test
public void generateStatements_convertsEndingsOnSqlServer() {
ExampleAbstractSQLChange change = new ExampleAbstractSQLChange("LINE 1;\n--a comment\nLINE 2;\nLINE 3;");
change.setSplitStatements(false);
change.setStripComments(true);
SqlStatement[] statements = change.generateStatements(new MSSQLDatabase());
assertEquals(1, statements.length);
assertEquals("LINE 1;\r\n\r\nLINE 2;\r\nLINE 3;", ((RawSqlStatement) statements[0]).getSql());
}
Aggregations