File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\dml\ScriptBase.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.dml;
8
9 import java.io.BufferedInputStream;
10 import java.io.BufferedOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.sql.Connection;
15 import org.h2.command.Prepared;
16 import org.h2.constant.ErrorCode;
17 import org.h2.constant.SysProperties;
18 import org.h2.engine.Constants;
19 import org.h2.engine.Database;
20 import org.h2.engine.Session;
21 import org.h2.expression.Expression;
22 import org.h2.message.DbException;
23 import org.h2.security.SHA256;
24 import org.h2.store.DataHandler;
25 import org.h2.store.FileStore;
26 import org.h2.store.FileStoreInputStream;
27 import org.h2.store.FileStoreOutputStream;
28 import org.h2.store.LobStorage;
29 import org.h2.store.fs.FileUtils;
30 import org.h2.tools.CompressTool;
31 import org.h2.util.IOUtils;
32 import org.h2.util.SmallLRUCache;
33 import org.h2.util.TempFileDeleter;
34
35 /**
36 * This class is the base for RunScriptCommand and ScriptCommand.
37 */
38 abstract class ScriptBase extends Prepared implements DataHandler {
39
40 /**
41 * The default name of the script file if .zip compression is used.
42 */
43 private static final String SCRIPT_SQL = "script.sql";
44
45 /**
46 * The output stream.
47 */
48 protected OutputStream out;
49
50 /**
51 * The input stream.
52 */
53 protected InputStream in;
54
55 /**
56 * The file name (if set).
57 */
58 private Expression fileNameExpr;
59
60 private Expression password;
61
62 private String fileName;
63
64 private String cipher;
65 private FileStore store;
66 private String compressionAlgorithm;
67
68 ScriptBase(Session session) {
69 super(session);
70 }
71
72 public void setCipher(String c) {
73 cipher = c;
74 }
75
76 private boolean isEncrypted() {
77 return cipher != null;
78 }
79
80 public void setPassword(Expression password) {
81 this.password = password;
82 }
83
84 public void setFileNameExpr(Expression file) {
85 this.fileNameExpr = file;
86 }
87
88 protected String getFileName() {
89 if (fileNameExpr != null && fileName == null) {
90 fileName = fileNameExpr.optimize(session).getValue(session).getString();
91 if (fileName == null || fileName.trim().length() == 0) {
92 fileName = "script.sql";
93 }
94 fileName = SysProperties.getScriptDirectory() + fileName;
95 }
96 return fileName;
97 }
98
99 public boolean isTransactional() {
100 return false;
101 }
102
103 /**
104 * Delete the target file.
105 */
106 void deleteStore() {
107 String file = getFileName();
108 if (file != null) {
109 FileUtils.delete(file);
110 }
111 }
112
113 private void initStore() {
114 Database db = session.getDatabase();
115 byte[] key = null;
116 if (cipher != null && password != null) {
117 char[] pass = password.optimize(session).getValue(session).getString().toCharArray();
118 key = SHA256.getKeyPasswordHash("script", pass);
119 }
120 String file = getFileName();
121 store = FileStore.open(db, file, "rw", cipher, key);
122 store.setCheckedWriting(false);
123 store.init();
124 }
125
126 /**
127 * Open the output stream.
128 */
129 void openOutput() {
130 String file = getFileName();
131 if (file == null) {
132 return;
133 }
134 if (isEncrypted()) {
135 initStore();
136 out = new FileStoreOutputStream(store, this, compressionAlgorithm);
137 // always use a big buffer, otherwise end-of-block is written a lot
138 out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE_COMPRESS);
139 } else {
140 OutputStream o = FileUtils.newOutputStream(file, false);
141 out = new BufferedOutputStream(o, Constants.IO_BUFFER_SIZE);
142 out = CompressTool.wrapOutputStream(out, compressionAlgorithm, SCRIPT_SQL);
143 }
144 }
145
146 /**
147 * Open the input stream.
148 */
149 void openInput() {
150 String file = getFileName();
151 if (file == null) {
152 return;
153 }
154 if (isEncrypted()) {
155 initStore();
156 in = new FileStoreInputStream(store, this, compressionAlgorithm != null, false);
157 } else {
158 InputStream inStream;
159 try {
160 inStream = FileUtils.newInputStream(file);
161 } catch (IOException e) {
162 throw DbException.convertIOException(e, file);
163 }
164 in = new BufferedInputStream(inStream, Constants.IO_BUFFER_SIZE);
165 in = CompressTool.wrapInputStream(in, compressionAlgorithm, SCRIPT_SQL);
166 if (in == null) {
167 throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, SCRIPT_SQL + " in " + file);
168 }
169 }
170 }
171
172 /**
173 * Close input and output streams.
174 */
175 void closeIO() {
176 IOUtils.closeSilently(out);
177 out = null;
178 IOUtils.closeSilently(in);
179 in = null;
180 if (store != null) {
181 store.closeSilently();
182 store = null;
183 }
184 }
185
186 public boolean needRecompile() {
187 return false;
188 }
189
190 public String getDatabasePath() {
191 return null;
192 }
193
194 public FileStore openFile(String name, String mode, boolean mustExist) {
195 return null;
196 }
197
198 public void checkPowerOff() {
199 session.getDatabase().checkPowerOff();
200 }
201
202 public void checkWritingAllowed() {
203 session.getDatabase().checkWritingAllowed();
204 }
205
206 public int getMaxLengthInplaceLob() {
207 return session.getDatabase().getMaxLengthInplaceLob();
208 }
209
210 public TempFileDeleter getTempFileDeleter() {
211 return session.getDatabase().getTempFileDeleter();
212 }
213
214 public String getLobCompressionAlgorithm(int type) {
215 return session.getDatabase().getLobCompressionAlgorithm(type);
216 }
217
218 public void setCompressionAlgorithm(String algorithm) {
219 this.compressionAlgorithm = algorithm;
220 }
221
222 public Object getLobSyncObject() {
223 return this;
224 }
225
226 public SmallLRUCache<String, String[]> getLobFileListCache() {
227 return null;
228 }
229
230 public LobStorage getLobStorage() {
231 return null;
232 }
233
234 public Connection getLobConnection() {
235 return null;
236 }
237
238 public int readLob(long lobId, byte[] hmac, long offset, byte[] buff, int off, int length) {
239 throw DbException.throwInternalError();
240 }
241
242 }
243