KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > rowio > RowOutputTextLog


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.rowio;
33
34 import java.math.BigDecimal JavaDoc;
35 import java.sql.Date JavaDoc;
36 import java.sql.Time JavaDoc;
37 import java.sql.Timestamp JavaDoc;
38
39 import org.hsqldb.CachedRow;
40 import org.hsqldb.Column;
41 import org.hsqldb.HsqlDateTime;
42 import org.hsqldb.lib.StringConverter;
43 import org.hsqldb.types.Binary;
44 import org.hsqldb.types.JavaObject;
45
46 /**
47  * @author fredt@users
48  * @since 1.7.2
49  * @version 1.7.2
50  */

51 public class RowOutputTextLog extends RowOutputBase {
52
53     static final byte[] BYTES_NULL = "NULL".getBytes();
54     static final byte[] BYTES_TRUE = "TRUE".getBytes();
55     static final byte[] BYTES_FALSE = "FALSE".getBytes();
56     static final byte[] BYTES_AND = " AND ".getBytes();
57     static final byte[] BYTES_IS = " IS ".getBytes();
58     public static final int MODE_DELETE = 1;
59     public static final int MODE_INSERT = 0;
60     private boolean isWritten;
61     private int logMode;
62
63     public void setMode(int mode) {
64         logMode = mode;
65     }
66
67     protected void writeFieldPrefix() {
68
69         if (logMode == MODE_DELETE && isWritten) {
70             write(BYTES_AND);
71         }
72     }
73
74     protected void writeChar(String JavaDoc s, int t) {
75
76         write('\'');
77         StringConverter.unicodeToAscii(this, s, true);
78         write('\'');
79     }
80
81     protected void writeReal(Double JavaDoc o, int type) {
82         writeBytes(Column.createSQLString(((Number JavaDoc) o).doubleValue()));
83     }
84
85     protected void writeSmallint(Number JavaDoc o) {
86         this.writeBytes(o.toString());
87     }
88
89     public void writeEnd() {}
90
91     protected void writeTime(Time JavaDoc o) {
92
93         write('\'');
94         writeBytes(o.toString());
95         write('\'');
96     }
97
98     protected void writeBinary(Binary o, int t) {
99
100         ensureRoom(o.getBytesLength() * 2 + 2);
101         write('\'');
102         StringConverter.writeHex(getBuffer(), count, o.getBytes());
103
104         count += o.getBytesLength() * 2;
105
106         write('\'');
107     }
108
109     public void writeType(int type) {}
110
111     public void writeSize(int size) {}
112
113     protected void writeDate(Date JavaDoc o) {
114
115         write('\'');
116         this.writeBytes(o.toString());
117         write('\'');
118     }
119
120     public int getSize(CachedRow row) {
121         return 0;
122     }
123
124     protected void writeInteger(Number JavaDoc o) {
125         this.writeBytes(o.toString());
126     }
127
128     protected void writeBigint(Number JavaDoc o) {
129         this.writeBytes(o.toString());
130     }
131
132 //fredt@users - patch 1108647 by nkowalcz@users (NataliaK) fix for IS NULL
133
protected void writeNull(int type) {
134
135         if (logMode == MODE_DELETE) {
136             write(BYTES_IS);
137         } else if (isWritten) {
138             write(',');
139         }
140
141         isWritten = true;
142
143         write(BYTES_NULL);
144     }
145
146     protected void writeOther(JavaObject o) {
147
148         ensureRoom(o.getBytesLength() * 2 + 2);
149         write('\'');
150         StringConverter.writeHex(getBuffer(), count, o.getBytes());
151
152         count += o.getBytesLength() * 2;
153
154         write('\'');
155     }
156
157     public void writeString(String JavaDoc value) {
158         StringConverter.unicodeToAscii(this, value, false);
159     }
160
161     protected void writeBit(Boolean JavaDoc o) {
162         write(o.booleanValue() ? BYTES_TRUE
163                                : BYTES_FALSE);
164     }
165
166     protected void writeDecimal(BigDecimal JavaDoc o) {
167         this.writeBytes(o.toString());
168     }
169
170     protected void writeFieldType(int type) {
171
172         if (logMode == MODE_DELETE) {
173             write('=');
174         } else if (isWritten) {
175             write(',');
176         }
177
178         isWritten = true;
179     }
180
181     public void writeLongData(long value) {
182         this.writeBytes(Long.toString(value));
183     }
184
185     public void writeIntData(int i, int position) {}
186
187     protected void writeTimestamp(Timestamp JavaDoc o) {
188
189         write('\'');
190         this.writeBytes(HsqlDateTime.getTimestampString(o));
191         write('\'');
192     }
193
194     public void writeShortData(short i) {
195         writeBytes(Integer.toString(i));
196     }
197
198     public void writeIntData(int i) {
199         writeBytes(Integer.toString(i));
200     }
201
202     public void reset() {
203
204         super.reset();
205
206         isWritten = false;
207     }
208 }
209
Popular Tags