KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > scriptio > ScriptWriterText


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.scriptio;
33
34 import java.io.IOException JavaDoc;
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 /**
46  * Handles all scripting and logging operations. A script consists of two blocks:<p>
47  *
48  * DDL: SQL statements for table and user definitions
49  * DATA: INSERT statements for memory tables
50  *
51  * This happens as part of the CHECKPOINT and SHUTDOWN COMPACT
52  * process. In this case, the
53  * DATA block contains the CACHED table data as well.<p>
54  *
55  * A related use for this class is for saving a current snapshot of the
56  * database data to a user-defined file with the SCRIPT command
57  *
58  * A log consists of SQL statements of different types. Each statement is
59  * encoded as ASCII and saved.
60  *
61  *
62  * @author fredt@users
63  * @version 1.8.0
64  * @since 1.7.2
65  */

66 public class ScriptWriterText extends ScriptWriterBase {
67
68     RowOutputTextLog rowOut;
69
70     // todo - perhaps move this global into a lib utility class
71
public static final byte[] BYTES_LINE_SEP;
72
73     static {
74         String JavaDoc 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 JavaDoc 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 JavaDoc {}
104
105     protected void addSessionId(Session session) throws IOException JavaDoc {
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 JavaDoc s)
135                                   throws IOException JavaDoc, 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 JavaDoc[] data) throws HsqlException, IOException JavaDoc {
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 JavaDoc {
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 JavaDoc[] data)
199                                      throws HsqlException, IOException JavaDoc {
200
201         schemaToLog = table.getName().schema;
202
203         writeRow(session, table, data);
204     }
205
206     public void writeDeleteStatement(Session session, Table table,
207                                      Object JavaDoc[] data)
208                                      throws HsqlException, IOException JavaDoc {
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 JavaDoc {
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 JavaDoc {
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