liquibase.command.core.InternalExecuteSqlCommandStep Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
package liquibase.command.core;
import liquibase.Scope;
import liquibase.command.*;
import liquibase.database.Database;
import liquibase.exception.LiquibaseException;
import liquibase.executor.Executor;
import liquibase.executor.ExecutorService;
import liquibase.statement.core.RawSqlStatement;
import liquibase.util.FileUtil;
import liquibase.util.StringUtil;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
public class InternalExecuteSqlCommandStep extends AbstractCommandStep {
public static final String[] COMMAND_NAME = {"internalExecuteSql"};
public static final CommandArgumentDefinition DATABASE_ARG;
public static final CommandArgumentDefinition SQL_ARG;
public static final CommandArgumentDefinition SQLFILE_ARG;
public static final CommandArgumentDefinition DELIMITER_ARG;
static {
final CommandBuilder builder = new CommandBuilder(COMMAND_NAME);
DATABASE_ARG = builder.argument("database", Database.class).required().build();
SQL_ARG = builder.argument("sql", String.class).build();
SQLFILE_ARG = builder.argument("sqlFile", String.class).build();
DELIMITER_ARG = builder.argument("delimiter", String.class).defaultValue(";").build();
}
@Override
public String[][] defineCommandNames() {
return new String[][] { COMMAND_NAME };
}
@Override
public void adjustCommandDefinition(CommandDefinition commandDefinition) {
super.adjustCommandDefinition(commandDefinition);
commandDefinition.setInternal(true);
}
@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