1 30 31 32 package org.hsqldb.scriptio; 33 34 import java.io.IOException ; 35 36 import org.hsqldb.Database; 37 import org.hsqldb.HsqlException; 38 import org.hsqldb.HsqlNameManager; 39 import org.hsqldb.HsqlNameManager.HsqlName; 40 import org.hsqldb.NumberSequence; 41 import org.hsqldb.Session; 42 import org.hsqldb.Table; 43 import org.hsqldb.rowio.RowOutputTextLog; 44 45 66 public class ScriptWriterText extends ScriptWriterBase { 67 68 RowOutputTextLog rowOut; 69 70 public static final byte[] BYTES_LINE_SEP; 72 73 static { 74 String sLineSep = System.getProperty("line.separator", "\n"); 75 76 BYTES_LINE_SEP = sLineSep.getBytes(); 77 } 78 79 static final byte[] BYTES_COMMIT = "COMMIT".getBytes(); 80 static final byte[] BYTES_INSERT_INTO = "INSERT INTO ".getBytes(); 81 static final byte[] BYTES_VALUES = " VALUES(".getBytes(); 82 static final byte[] BYTES_TERM = ")".getBytes(); 83 static final byte[] BYTES_DELETE_FROM = "DELETE FROM ".getBytes(); 84 static final byte[] BYTES_WHERE = " WHERE ".getBytes(); 85 static final byte[] BYTES_SEQUENCE = "ALTER SEQUENCE ".getBytes(); 86 static final byte[] BYTES_SEQUENCE_MID = " RESTART WITH ".getBytes(); 87 static final byte[] BYTES_C_ID_INIT = "/*C".getBytes(); 88 static final byte[] BYTES_C_ID_TERM = "*/".getBytes(); 89 static final byte[] BYTES_SCHEMA = "SET SCHEMA ".getBytes(); 90 91 ScriptWriterText() {} 92 93 public ScriptWriterText(Database db, String file, 94 boolean includeCachedData, boolean newFile, 95 boolean isDump) throws HsqlException { 96 super(db, file, includeCachedData, newFile, isDump); 97 } 98 99 protected void initBuffers() { 100 rowOut = new RowOutputTextLog(); 101 } 102 103 protected void writeDataTerm() throws IOException {} 104 105 protected void addSessionId(Session session) throws IOException { 106 107 if (session == null) { 108 return; 109 } 110 111 if (session != currentSession) { 112 rowOut.write(BYTES_C_ID_INIT); 113 rowOut.writeIntData(session.getId()); 114 rowOut.write(BYTES_C_ID_TERM); 115 116 currentSession = session; 117 } 118 119 if (schemaToLog != session.loggedSchema) { 120 writeSchemaStatement(schemaToLog); 121 122 session.loggedSchema = schemaToLog; 123 } 124 } 125 126 private void writeSchemaStatement(HsqlName schema) { 127 128 rowOut.write(BYTES_SCHEMA); 129 rowOut.writeString(schema.statementName); 130 rowOut.write(BYTES_LINE_SEP); 131 } 132 133 public void writeLogStatement(Session session, 134 String s) 135 throws IOException , HsqlException { 136 137 schemaToLog = session.currentSchema; 138 busyWriting = true; 139 140 rowOut.reset(); 141 addSessionId(session); 142 rowOut.writeString(s); 143 rowOut.write(BYTES_LINE_SEP); 144 fileStreamOut.write(rowOut.getBuffer(), 0, rowOut.size()); 145 146 byteCount += rowOut.size(); 147 needsSync = true; 148 busyWriting = false; 149 150 if (forceSync || writeDelay == 0) { 151 sync(); 152 } 153 } 154 155 protected void writeRow(Session session, Table table, 156 Object [] data) throws HsqlException, IOException { 157 158 busyWriting = true; 159 160 rowOut.reset(); 161 ((RowOutputTextLog) rowOut).setMode(RowOutputTextLog.MODE_INSERT); 162 addSessionId(session); 163 rowOut.write(BYTES_INSERT_INTO); 164 rowOut.writeString(table.getName().statementName); 165 rowOut.write(BYTES_VALUES); 166 rowOut.writeData(data, table); 167 rowOut.write(BYTES_TERM); 168 rowOut.write(BYTES_LINE_SEP); 169 fileStreamOut.write(rowOut.getBuffer(), 0, rowOut.size()); 170 171 byteCount += rowOut.size(); 172 needsSync |= session.isAutoCommit(); 173 busyWriting = false; 174 175 if (forceSync || writeDelay == 0) { 176 sync(); 177 } 178 } 179 180 protected void writeTableInit(Table t) throws HsqlException, IOException { 181 182 if (t.isEmpty(currentSession)) { 183 return; 184 } 185 186 if (schemaToLog == currentSession.loggedSchema) { 187 return; 188 } 189 190 rowOut.reset(); 191 writeSchemaStatement(t.getName().schema); 192 fileStreamOut.write(rowOut.getBuffer(), 0, rowOut.size()); 193 194 currentSession.loggedSchema = schemaToLog; 195 } 196 197 public void writeInsertStatement(Session session, Table table, 198 Object [] data) 199 throws HsqlException, IOException { 200 201 schemaToLog = table.getName().schema; 202 203 writeRow(session, table, data); 204 } 205 206 public void writeDeleteStatement(Session session, Table table, 207 Object [] data) 208 throws HsqlException, IOException { 209 210 schemaToLog = table.getName().schema; 211 busyWriting = true; 212 213 rowOut.reset(); 214 ((RowOutputTextLog) rowOut).setMode(RowOutputTextLog.MODE_DELETE); 215 addSessionId(session); 216 rowOut.write(BYTES_DELETE_FROM); 217 rowOut.writeString(table.getName().statementName); 218 rowOut.write(BYTES_WHERE); 219 rowOut.writeData(table.getColumnCount(), table.getColumnTypes(), 220 data, table.columnList, table.getPrimaryKey()); 221 rowOut.write(BYTES_LINE_SEP); 222 fileStreamOut.write(rowOut.getBuffer(), 0, rowOut.size()); 223 224 byteCount += rowOut.size(); 225 needsSync |= session.isAutoCommit(); 226 busyWriting = false; 227 228 if (forceSync || writeDelay == 0) { 229 sync(); 230 } 231 } 232 233 public void writeSequenceStatement(Session session, 234 NumberSequence seq) 235 throws HsqlException, IOException { 236 237 schemaToLog = seq.getName().schema; 238 busyWriting = true; 239 240 rowOut.reset(); 241 addSessionId(session); 242 rowOut.write(BYTES_SEQUENCE); 243 rowOut.writeString(seq.getName().statementName); 244 rowOut.write(BYTES_SEQUENCE_MID); 245 rowOut.writeLongData(seq.peek()); 246 rowOut.write(BYTES_LINE_SEP); 247 fileStreamOut.write(rowOut.getBuffer(), 0, rowOut.size()); 248 249 byteCount += rowOut.size(); 250 needsSync = true; 251 busyWriting = false; 252 253 if (forceSync || writeDelay == 0) { 254 sync(); 255 } 256 } 257 258 public void writeCommitStatement(Session session) 259 throws HsqlException, IOException { 260 261 busyWriting = true; 262 263 rowOut.reset(); 264 addSessionId(session); 265 rowOut.write(BYTES_COMMIT); 266 rowOut.write(BYTES_LINE_SEP); 267 fileStreamOut.write(rowOut.getBuffer(), 0, rowOut.size()); 268 269 byteCount += rowOut.size(); 270 needsSync = true; 271 busyWriting = false; 272 273 if (forceSync || writeDelay == 0) { 274 sync(); 275 } 276 } 277 278 protected void finalize() { 279 sync(); 280 } 281 } 282 | Popular Tags |