Search in sources :

Example 21 with RawSqlStatement

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

Example 22 with RawSqlStatement

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());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Executor(liquibase.executor.Executor) TreeSet(java.util.TreeSet) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService) LiquibaseException(liquibase.exception.LiquibaseException) File(java.io.File) Map(java.util.Map)

Example 23 with RawSqlStatement

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;
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException)

Example 24 with RawSqlStatement

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;
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) ExecutorService(liquibase.executor.ExecutorService)

Example 25 with RawSqlStatement

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());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Test(org.junit.Test)

Aggregations

RawSqlStatement (liquibase.statement.core.RawSqlStatement)30 DatabaseException (liquibase.exception.DatabaseException)11 ExecutorService (liquibase.executor.ExecutorService)10 Executor (liquibase.executor.Executor)9 Map (java.util.Map)7 Schema (liquibase.structure.core.Schema)7 CatalogAndSchema (liquibase.CatalogAndSchema)6 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)6 SqlStatement (liquibase.statement.SqlStatement)6 Test (org.junit.Test)6 DatabaseConnection (liquibase.database.DatabaseConnection)5 Table (liquibase.structure.core.Table)5 ArrayList (java.util.ArrayList)4 Database (liquibase.database.Database)4 OfflineConnection (liquibase.database.OfflineConnection)4 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)4 Column (liquibase.structure.core.Column)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)3 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)3 SnapshotControl (liquibase.snapshot.SnapshotControl)3