Debian Codegraph @

src/main/org/h2/command/dml/ScriptBase.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.command.dml;
7
8 import java.io.BufferedOutputStream;
9 import java.io.BufferedReader;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
13 import java.io.OutputStream;
14 import java.nio.charset.Charset;
15 import org.h2.api.ErrorCode;
16 import org.h2.command.Prepared;
17 import org.h2.engine.Constants;
18 import org.h2.engine.Database;
19 import org.h2.engine.SessionLocal;
20 import org.h2.engine.SysProperties;
21 import org.h2.expression.Expression;
22 import org.h2.message.DbException;
23 import org.h2.security.SHA256;
24 import org.h2.store.FileStore;
25 import org.h2.store.FileStoreInputStream;
26 import org.h2.store.FileStoreOutputStream;
27 import org.h2.store.fs.FileUtils;
28 import org.h2.tools.CompressTool;
29 import org.h2.util.IOUtils;
30 import org.h2.util.StringUtils;
31
32 /**
33 * This class is the base for RunScriptCommand and ScriptCommand.
34 */
35 abstract class ScriptBase extends Prepared {
36
37 /**
38 * The default name of the script file if .zip compression is used.
39 */
40 private static final String SCRIPT_SQL = "script.sql";
41
42 /**
43 * The output stream.
44 */
45 protected OutputStream out;
46
47 /**
48 * The input reader.
49 */
50 protected BufferedReader reader;
51
52 /**
53 * The file name (if set).
54 */
55 private Expression fileNameExpr;
56
57 private Expression password;
58
59 private String fileName;
60
61 private String cipher;
62 private FileStore store;
63 private String compressionAlgorithm;
64
65 ScriptBase(SessionLocal session) {
66 super(session);
67 }
68
69 public void setCipher(String c) {
70 cipher = c;
71 }
72
73 private boolean isEncrypted() {
74 return cipher != null;
75 }
76
77 public void setPassword(Expression password) {
78 this.password = password;
79 }
80
81 public void setFileNameExpr(Expression file) {
82 this.fileNameExpr = file;
83 }
84
85 protected String getFileName() {
86 if (fileNameExpr != null && fileName == null) {
87 fileName = fileNameExpr.optimize(session).getValue(session).getString();
88 if (fileName == null || StringUtils.isWhitespaceOrEmpty(fileName)) {
89 fileName = "script.sql";
90 }
91 fileName = SysProperties.getScriptDirectory() + fileName;
92 }
93 return fileName;
94 }
95
96 @Override
97 public boolean isTransactional() {
98 return false;
99 }
100
101 /**
102 * Delete the target file.
103 */
104 void deleteStore() {
105 String file = getFileName();
106 if (file != null) {
107 if (FileUtils.isRegularFile(file)) {
108 FileUtils.delete(file);
109 }
110 }
111 }
112
113 private void initStore() {
114 Database db = getDatabase();
115 byte[] key = null;
116 if (cipher != null && password != null) {
117 char[] pass = password.optimize(session).
118 getValue(session).getString().toCharArray();
119 key = SHA256.getKeyPasswordHash("script", pass);
120 }
121 String file = getFileName();
122 store = FileStore.open(db, file, "rw", cipher, key);
123 store.setCheckedWriting(false);
124 store.init();
125 }
126
127 /**
128 * Open the output stream.
129 */
130 void openOutput() {
131 String file = getFileName();
132 if (file == null) {
133 return;
134 }
135 if (isEncrypted()) {
136 initStore();
137 out = new FileStoreOutputStream(store, compressionAlgorithm);
138 // always use a big buffer, otherwise end-of-block is written a lot
139 out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE_COMPRESS);
140 } else {
141 OutputStream o;
142 try {
143 o = FileUtils.newOutputStream(file, false);
144 } catch (IOException e) {
145 throw DbException.convertIOException(e, null);
146 }
147 out = new BufferedOutputStream(o, Constants.IO_BUFFER_SIZE);
148 out = CompressTool.wrapOutputStream(out, compressionAlgorithm, SCRIPT_SQL);
149 }
150 }
151
152 /**
153 * Open the input stream.
154 *
155 * @param charset the charset to use
156 */
157 void openInput(Charset charset) {
158 String file = getFileName();
159 if (file == null) {
160 return;
161 }
162 InputStream in;
163 if (isEncrypted()) {
164 initStore();
165 in = new FileStoreInputStream(store, compressionAlgorithm != null, false);
166 } else {
167 try {
168 in = FileUtils.newInputStream(file);
169 } catch (IOException e) {
170 throw DbException.convertIOException(e, file);
171 }
172 in = CompressTool.wrapInputStream(in, compressionAlgorithm, SCRIPT_SQL);
173 if (in == null) {
174 throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, SCRIPT_SQL + " in " + file);
175 }
176 }
177 reader = new BufferedReader(new InputStreamReader(in, charset), Constants.IO_BUFFER_SIZE);
178 }
179
180 /**
181 * Close input and output streams.
182 */
183 void closeIO() {
184 IOUtils.closeSilently(out);
185 out = null;
186 IOUtils.closeSilently(reader);
187 reader = null;
188 if (store != null) {
189 store.closeSilently();
190 store = null;
191 }
192 }
193
194 @Override
195 public boolean needRecompile() {
196 return false;
197 }
198
199 public void setCompressionAlgorithm(String algorithm) {
200 this.compressionAlgorithm = algorithm;
201 }
202
203 }