1 30 31 32 package org.hsqldb.scriptio; 33 34 import java.io.BufferedInputStream ; 35 import java.io.DataInputStream ; 36 import java.io.EOFException ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 40 import org.hsqldb.Database; 41 import org.hsqldb.HsqlException; 42 import org.hsqldb.Result; 43 import org.hsqldb.ResultConstants; 44 import org.hsqldb.Session; 45 import org.hsqldb.Table; 46 import org.hsqldb.Trace; 47 import org.hsqldb.lib.Iterator; 48 import org.hsqldb.lib.SimpleLog; 49 import org.hsqldb.rowio.RowInputBase; 50 import org.hsqldb.rowio.RowInputBinary; 51 52 59 class ScriptReaderBinary extends ScriptReaderBase { 60 61 private RowInputBinary rowIn; 62 protected DataInputStream dataStreamIn; 63 64 ScriptReaderBinary(Database db, 65 String file) throws HsqlException, IOException { 66 67 super(db, file); 68 69 rowIn = new RowInputBinary(); 70 } 71 72 protected void openFile() throws IOException { 73 74 InputStream d = db.isFilesInJar() 75 ? getClass().getResourceAsStream(fileName) 76 : db.getFileAccess().openInputStreamElement(fileName); 77 78 dataStreamIn = new DataInputStream (new BufferedInputStream (d, 79 1 << 13)); 80 } 81 82 protected void readDDL(Session session) 83 throws IOException , HsqlException { 84 85 Result r = Result.read(rowIn, dataStreamIn); 86 Iterator it = r.iterator(); 87 88 while (it.hasNext()) { 89 Object [] data = (Object []) it.next(); 90 String s = (String ) data[0]; 91 Result result = session.sqlExecuteDirectNoPreChecks(s); 92 93 if (result.isError()) { 94 db.logger.appLog.logContext(SimpleLog.LOG_ERROR, 95 result.getMainString()); 96 97 98 throw Trace.error(result); 99 } 100 } 101 } 102 103 protected void readExistingData(Session session) 104 throws IOException , HsqlException { 105 106 for (;;) { 107 String s = readTableInit(); 108 109 if (s == null) { 110 break; 111 } 112 113 String schema = session.getSchemaName(currentSchema); 114 Table t = db.schemaManager.getUserTable(session, s, schema); 115 int j = 0; 116 117 for (j = 0; ; j++) { 118 if (readRow(t) == false) { 119 break; 120 } 121 } 122 123 int checkCount = readTableTerm(); 124 125 if (j != checkCount) { 126 throw Trace.error(Trace.ERROR_IN_SCRIPT_FILE, 127 Trace.ERROR_IN_BINARY_SCRIPT_1, 128 new Object [] { 129 s, new Integer (j), new Integer (checkCount) 130 }); 131 } 132 } 133 } 134 135 protected boolean readRow(Table t) throws IOException , HsqlException { 138 139 boolean more = readRow(rowIn, 0); 140 141 if (!more) { 142 return false; 143 } 144 145 Object [] data = rowIn.readData(t.getColumnTypes()); 146 147 t.insertFromScript(data); 148 149 return true; 150 } 151 152 protected int readTableTerm() throws IOException , HsqlException { 154 return dataStreamIn.readInt(); 155 } 156 157 protected String readTableInit() throws IOException , HsqlException { 160 161 boolean more = readRow(rowIn, 0); 162 163 if (!more) { 164 return null; 165 } 166 167 String s = rowIn.readString(); 168 169 int checkOp = rowIn.readIntData(); 171 172 if (checkOp == ScriptWriterBase.INSERT_WITH_SCHEMA) { 173 currentSchema = rowIn.readString(); 174 } else { 175 currentSchema = null; 176 } 177 178 if (checkOp != ScriptWriterBase.INSERT 179 && checkOp != ScriptWriterBase.INSERT_WITH_SCHEMA) { 180 throw Trace.error(Trace.ERROR_IN_SCRIPT_FILE, 181 Trace.ERROR_IN_BINARY_SCRIPT_2); 182 } 183 184 return s; 185 } 186 187 boolean readRow(RowInputBase rowin, int pos) throws IOException { 188 189 try { 190 int length = dataStreamIn.readInt(); 191 int count = 4; 192 193 if (length == 0) { 194 return false; 195 } 196 197 rowin.resetRow(pos, length); 198 dataStreamIn.readFully(rowin.getBuffer(), count, length - count); 199 200 return true; 201 } catch (EOFException e) { 202 return false; 203 } 204 } 205 206 public boolean readLoggedStatement(Session session) throws IOException { 207 return false; 208 } 209 210 public void close() { 211 212 try { 213 dataStreamIn.close(); 214 } catch (IOException e) {} 215 } 216 } 217 | Popular Tags |