View Javadoc
1   /*
2    *    Copyright 2010-2025 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  
18  import java.io.ByteArrayOutputStream;
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.PrintStream;
23  import java.io.StringReader;
24  import java.nio.charset.Charset;
25  import java.nio.file.Files;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import org.apache.ibatis.migration.MigrationException;
30  import org.apache.ibatis.migration.VariableReplacer;
31  import org.apache.ibatis.migration.utils.Util;
32  
33  public class SqlHookScript implements HookScript {
34  
35    protected final File scriptFile;
36    protected final String charset;
37    protected final Properties variables;
38    protected final PrintStream printStream;
39    protected final VariableReplacer replacer;
40  
41    public SqlHookScript(File scriptFile, String charset, String[] options, Properties variables,
42        PrintStream printStream) {
43      this.scriptFile = scriptFile;
44      this.charset = charset;
45      this.variables = variables;
46      this.printStream = printStream;
47      // options can be local variables in key=value format.
48      for (String option : options) {
49        int sep = option.indexOf('=');
50        if (sep > -1) {
51          this.variables.put(option.substring(0, sep), option.substring(sep + 1));
52        }
53      }
54      replacer = new VariableReplacer(this.variables);
55    }
56  
57    @Override
58    public void execute(Map<String, Object> bindingMap) {
59      HookContext context = (HookContext) bindingMap.get(MigrationHook.HOOK_CONTEXT);
60      printStream.println(Util.horizontalLine("Applying SQL hook: " + scriptFile.getName(), 80));
61  
62      try (InputStream inputStream = Files.newInputStream(scriptFile.toPath());
63          ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
64        byte[] buffer = new byte[1024];
65        int length;
66        while ((length = inputStream.read(buffer)) != -1) {
67          outputStream.write(buffer, 0, length);
68        }
69        try (StringReader reader = new StringReader(replacer.replace(outputStream.toString(Charset.forName(charset))))) {
70          context.executeSql(reader);
71        }
72      } catch (IOException e) {
73        throw new MigrationException("Error occurred while running SQL hook script.", e);
74      }
75    }
76  }