1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }