SqlHookScript.java

  1. /*
  2.  *    Copyright 2010-2023 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.migration.hook;

  17. import java.io.ByteArrayOutputStream;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.IOException;
  21. import java.io.PrintStream;
  22. import java.io.StringReader;
  23. import java.util.Map;
  24. import java.util.Properties;

  25. import org.apache.ibatis.migration.MigrationException;
  26. import org.apache.ibatis.migration.VariableReplacer;
  27. import org.apache.ibatis.migration.utils.Util;

  28. public class SqlHookScript implements HookScript {

  29.   protected final File scriptFile;
  30.   protected final String charset;
  31.   protected final Properties variables;
  32.   protected final PrintStream printStream;
  33.   protected final VariableReplacer replacer;

  34.   public SqlHookScript(File scriptFile, String charset, String[] options, Properties variables,
  35.       PrintStream printStream) {
  36.     this.scriptFile = scriptFile;
  37.     this.charset = charset;
  38.     this.variables = variables;
  39.     this.printStream = printStream;
  40.     // options can be local variables in key=value format.
  41.     for (String option : options) {
  42.       int sep = option.indexOf('=');
  43.       if (sep > -1) {
  44.         this.variables.put(option.substring(0, sep), option.substring(sep + 1));
  45.       }
  46.     }
  47.     replacer = new VariableReplacer(this.variables);
  48.   }

  49.   @Override
  50.   public void execute(Map<String, Object> bindingMap) {
  51.     HookContext context = (HookContext) bindingMap.get(MigrationHook.HOOK_CONTEXT);
  52.     printStream.println(Util.horizontalLine("Applying SQL hook: " + scriptFile.getName(), 80));

  53.     try (FileInputStream inputStream = new FileInputStream(scriptFile);
  54.         ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
  55.       byte[] buffer = new byte[1024];
  56.       int length;
  57.       while ((length = inputStream.read(buffer)) != -1) {
  58.         outputStream.write(buffer, 0, length);
  59.       }
  60.       try (StringReader reader = new StringReader(replacer.replace(outputStream.toString(charset)))) {
  61.         context.executeSql(reader);
  62.       }
  63.     } catch (IOException e) {
  64.       throw new MigrationException("Error occurred while running SQL hook script.", e);
  65.     }
  66.   }
  67. }