1 5 package org.h2.command.dml; 6 7 import java.io.BufferedInputStream ; 8 import java.io.BufferedOutputStream ; 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.OutputStream ; 15 import java.sql.SQLException ; 16 17 import org.h2.command.Prepared; 18 import org.h2.engine.Constants; 19 import org.h2.engine.Database; 20 import org.h2.engine.Session; 21 import org.h2.message.Message; 22 import org.h2.security.SHA256; 23 import org.h2.store.DataHandler; 24 import org.h2.store.FileStore; 25 import org.h2.store.FileStoreInputStream; 26 import org.h2.store.FileStoreOutputStream; 27 import org.h2.tools.CompressTool; 28 import org.h2.util.FileUtils; 29 import org.h2.util.IOUtils; 30 import org.h2.value.Value; 31 32 public class ScriptBase extends Prepared implements DataHandler { 33 34 private String cipher; 35 private byte[] key; 36 private FileStore store; 37 private FileOutputStream outStream; 38 private FileInputStream inStream; 39 protected OutputStream out; 40 protected InputStream in; 41 protected String fileName; 42 43 private String compressionAlgorithm; 44 45 public ScriptBase(Session session) { 46 super(session); 47 } 48 49 public void setCipher(String c) { 50 cipher = c; 51 } 52 53 protected boolean isEncrypted() { 54 return cipher != null; 55 } 56 57 public void setPassword(char[] password) { 58 SHA256 sha = new SHA256(); 59 key = sha.getKeyPasswordHash("script", password); 60 } 61 62 public void setFileName(String fileName) { 63 if(fileName == null || fileName.trim().length() == 0) { 64 fileName = "script.sql"; 65 } 66 this.fileName = Constants.SCRIPT_DIRECTORY + fileName; 67 } 68 69 public boolean isTransactional() { 70 return false; 71 } 72 73 protected void deleteStore() throws SQLException { 74 if(fileName != null) { 75 FileUtils.delete(fileName); 76 } 77 } 78 79 private void initStore() throws SQLException { 80 byte[] magic = Database.getMagic(true); 81 Database db = session.getDatabase(); 82 store = FileStore.open(db, fileName, magic, cipher, key); 84 store.init(); 85 } 86 87 protected void openOutput() throws SQLException { 88 if(fileName == null) { 89 return; 90 } 91 if(isEncrypted()) { 92 initStore(); 93 out = new FileStoreOutputStream(store, this, compressionAlgorithm); 94 out = new BufferedOutputStream (out, Constants.IO_BUFFER_SIZE_COMPRESS); 96 } else { 97 try { 98 outStream = FileUtils.openFileOutputStream(new File (fileName)); 99 } catch (IOException e) { 100 throw Message.convert(e); 101 } 102 out = new BufferedOutputStream (outStream, Constants.IO_BUFFER_SIZE); 103 out = CompressTool.wrapOutputStream(out, compressionAlgorithm, Constants.SCRIPT_SQL); 104 } 105 } 106 107 protected void openInput() throws SQLException { 108 if(fileName == null) { 109 return; 110 } 111 if(isEncrypted()) { 112 initStore(); 113 in = new FileStoreInputStream(store, this, compressionAlgorithm != null); 114 } else { 115 try { 116 inStream = new FileInputStream (fileName); 117 } catch (IOException e) { 118 throw Message.convert(e); 119 } 120 in = new BufferedInputStream (inStream, Constants.IO_BUFFER_SIZE); 121 in = CompressTool.wrapInputStream(in, compressionAlgorithm, Constants.SCRIPT_SQL); 122 if(in == null) { 123 throw Message.getSQLException(Message.FILE_NOT_FOUND_1, Constants.SCRIPT_SQL + " in " + fileName); 124 } 125 } 126 } 127 128 protected void closeIO() { 129 IOUtils.closeSilently(out); 130 out = null; 131 IOUtils.closeSilently(in); 132 in = null; 133 if(store != null) { 134 store.closeSilently(); 135 store = null; 136 } 137 IOUtils.closeSilently(inStream); 138 inStream = null; 139 IOUtils.closeSilently(outStream); 140 outStream = null; 141 } 142 143 public boolean needRecompile() { 144 return false; 145 } 146 147 public boolean getTextStorage() { 148 return true; 150 } 151 152 public String getDatabasePath() { 153 return null; 154 } 155 156 public FileStore openFile(String name, boolean mustExist) throws SQLException { 157 return null; 158 } 159 160 public int getChecksum(byte[] data, int start, int end) { 161 return session.getDatabase().getChecksum(data, start, end); 162 } 163 164 public void checkPowerOff() throws SQLException { 165 session.getDatabase().checkPowerOff(); 166 } 167 168 public void checkWritingAllowed() throws SQLException { 169 session.getDatabase().checkWritingAllowed(); 170 } 171 172 public void freeUpDiskSpace() throws SQLException { 173 session.getDatabase().checkWritingAllowed(); 174 } 175 176 public void handleInvalidChecksum() throws SQLException { 177 session.getDatabase().handleInvalidChecksum(); 178 } 179 180 public int compareTypeSave(Value a, Value b) throws SQLException { 181 throw Message.getInternalError(); 182 } 183 184 public int getMaxLengthInplaceLob() { 185 return session.getDatabase().getMaxLengthInplaceLob(); 186 } 187 188 public int allocateObjectId(boolean b, boolean c) { 189 return session.getDatabase().allocateObjectId(b, c); 190 } 191 192 public String createTempFile() throws SQLException { 193 return session.getDatabase().createTempFile(); 194 } 195 196 public String getLobCompressionAlgorithm(int type) { 197 return session.getDatabase().getLobCompressionAlgorithm(type); 198 } 199 200 public void setCompressionAlgorithm(String algorithm) { 201 this.compressionAlgorithm = algorithm; 202 } 203 204 } 205 | Popular Tags |