KickJava   Java API By Example, From Geeks To Geeks.

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


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.Column;
40 import org.hsqldb.Table;
41 import org.hsqldb.Trace;
42 import org.hsqldb.Types;
43 import org.hsqldb.lib.HashMappedList;
44 import org.hsqldb.lib.HsqlByteArrayOutputStream;
45 import org.hsqldb.types.Binary;
46 import org.hsqldb.types.JavaObject;
47
48 /**
49  * Base class for writing the data for a database row in different formats.
50  * Defines the methods that are independent of storage format and declares
51  * the format-dependent methods that subclasses should define.
52  *
53  * @author sqlbob@users (RMP)
54  * @author fredt@users
55  * @version 1.7.2
56  * @since 1.7.0
57  */

58 public abstract class RowOutputBase extends HsqlByteArrayOutputStream
59 implements RowOutputInterface {
60
61     public static final int CACHED_ROW_160 = 0;
62     public static final int CACHED_ROW_170 = 1;
63
64     // the last column in a table is an ID that should not be written to file
65
protected boolean skipSystemId = false;
66
67     /**
68      * Constructor used for persistent storage of a Table row
69      *
70      * @exception IOException when an IO error is encountered
71      */

72     public RowOutputBase() {
73         super();
74     }
75
76     /**
77      * Constructor used for result sets
78      *
79      * @exception IOException when an IO error is encountered
80      */

81     public RowOutputBase(int initialSize) {
82         super(initialSize);
83     }
84
85     /**
86      * Constructor used for network transmission of result sets
87      *
88      * @exception IOException when an IO error is encountered
89      */

90     public RowOutputBase(byte[] buffer) {
91         super(buffer);
92     }
93
94 // fredt@users - comment - methods for writing Result column type, name and data size
95
public abstract void writeEnd();
96
97     public abstract void writeSize(int size);
98
99     public abstract void writeType(int type);
100
101     public abstract void writeShortData(short i);
102
103     public abstract void writeIntData(int i);
104
105     public abstract void writeIntData(int i, int position);
106
107     public abstract void writeString(String JavaDoc s);
108
109 // fredt@users - comment - methods used for writing each SQL type
110
protected void writeFieldPrefix() {}
111
112     protected abstract void writeFieldType(int type);
113
114     protected abstract void writeNull(int type);
115
116     protected abstract void writeChar(String JavaDoc s, int t);
117
118     protected abstract void writeSmallint(Number JavaDoc o);
119
120     protected abstract void writeInteger(Number JavaDoc o);
121
122     protected abstract void writeBigint(Number JavaDoc o);
123
124     protected abstract void writeReal(Double JavaDoc o, int type);
125
126     protected abstract void writeDecimal(BigDecimal JavaDoc o);
127
128     protected abstract void writeBit(Boolean JavaDoc o);
129
130     protected abstract void writeDate(Date JavaDoc o);
131
132     protected abstract void writeTime(Time JavaDoc o);
133
134     protected abstract void writeTimestamp(Timestamp JavaDoc o);
135
136     protected abstract void writeOther(JavaObject o);
137
138     protected abstract void writeBinary(Binary o, int t);
139
140     public void writeRow(Object JavaDoc[] data, Table t) {
141
142         writeSize(0);
143         writeData(data, t);
144         writeIntData(size(), 0);
145     }
146
147     /**
148      * This method is called to write data for a table.
149      *
150      * @param data
151      * @param t
152      * @throws IOException
153      */

154     public void writeData(Object JavaDoc[] data, Table t) {
155
156         int[] types = t.getColumnTypes();
157         int l = t.getColumnCount();
158
159         writeData(l, types, data, null, null);
160     }
161
162     /**
163      * This method is called to write data for a Result.
164      *
165      * @param l
166      * @param types
167      * @param data
168      * @param cols
169      * @param primarykeys
170      * @throws IOException
171      */

172     public void writeData(int l, int[] types, Object JavaDoc[] data,
173                           HashMappedList cols, int[] primaryKeys) {
174
175         boolean hasPK = primaryKeys != null && primaryKeys.length != 0;
176         int limit = hasPK ? primaryKeys.length
177                               : l;
178
179         for (int i = 0; i < limit; i++) {
180             int j = hasPK ? primaryKeys[i]
181                              : i;
182             Object JavaDoc o = data[j];
183             int t = types[j];
184
185             if (cols != null) {
186                 Column col = (Column) cols.get(j);
187
188                 writeFieldPrefix();
189                 writeString(col.columnName.statementName);
190             }
191
192             if (o == null) {
193                 writeNull(t);
194
195                 continue;
196             }
197
198             writeFieldType(t);
199
200             switch (t) {
201
202                 case Types.NULL :
203                 case Types.CHAR :
204                 case Types.VARCHAR :
205                 case Types.VARCHAR_IGNORECASE :
206                 case Types.LONGVARCHAR :
207                     writeChar((String JavaDoc) o, t);
208                     break;
209
210                 case Types.TINYINT :
211                 case Types.SMALLINT :
212                     writeSmallint((Number JavaDoc) o);
213                     break;
214
215                 case Types.INTEGER :
216                     writeInteger((Number JavaDoc) o);
217                     break;
218
219                 case Types.BIGINT :
220                     writeBigint((Number JavaDoc) o);
221                     break;
222
223                 case Types.REAL :
224                 case Types.FLOAT :
225                 case Types.DOUBLE :
226                     writeReal((Double JavaDoc) o, t);
227                     break;
228
229                 case Types.NUMERIC :
230                 case Types.DECIMAL :
231                     writeDecimal((BigDecimal JavaDoc) o);
232                     break;
233
234                 case Types.BOOLEAN :
235                     writeBit((Boolean JavaDoc) o);
236                     break;
237
238                 case Types.DATE :
239                     writeDate((Date JavaDoc) o);
240                     break;
241
242                 case Types.TIME :
243                     writeTime((Time JavaDoc) o);
244                     break;
245
246                 case Types.TIMESTAMP :
247                     writeTimestamp((Timestamp JavaDoc) o);
248                     break;
249
250                 case Types.OTHER :
251                     writeOther((JavaObject) o);
252                     break;
253
254                 case Types.BINARY :
255                 case Types.VARBINARY :
256                 case Types.LONGVARBINARY :
257                     writeBinary((Binary) o, t);
258                     break;
259
260                 default :
261                     throw Trace.runtimeError(Trace.FUNCTION_NOT_SUPPORTED,
262                                              Types.getTypeString(t));
263             }
264         }
265     }
266
267     // returns the underlying HsqlByteArrayOutputStream
268
public HsqlByteArrayOutputStream getOutputStream() {
269         return this;
270     }
271 }
272
Popular Tags