KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > ValueRow


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.ValueRow
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.execute;
23
24 import org.apache.derby.iapi.services.io.Storable;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.reference.SQLState;
29
30 import org.apache.derby.iapi.sql.execute.ExecRow;
31 import org.apache.derby.iapi.sql.execute.ExecutionContext;
32 import org.apache.derby.iapi.types.DataValueDescriptor;
33
34 import org.apache.derby.iapi.reference.SQLState;
35
36 import org.apache.derby.iapi.types.RowLocation;
37
38 import org.apache.derby.iapi.services.sanity.SanityManager;
39
40 import org.apache.derby.iapi.services.io.Formatable;
41 import org.apache.derby.iapi.services.io.ArrayUtil;
42 import org.apache.derby.iapi.services.io.StoredFormatIds;
43 import org.apache.derby.iapi.services.io.FormatIdUtil;
44
45 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
46
47 import java.io.ObjectOutput JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.IOException JavaDoc;
50
51 import org.apache.derby.iapi.services.io.FormatableBitSet;
52
53 /**
54     Basic implementation of ExecRow.
55
56     @author ames
57  */

58 public class ValueRow implements ExecRow, Formatable
59 {
60     /********************************************************
61     **
62     ** This class implements Formatable. That means that it
63     ** can write itself to and from a formatted stream. If
64     ** you add more fields to this class, make sure that you
65     ** also write/read them with the writeExternal()/readExternal()
66     ** methods.
67     **
68     ** If, inbetween releases, you add more fields to this class,
69     ** then you should bump the version number emitted by the getTypeFormatId()
70     ** method.
71     **
72     ********************************************************/

73
74     ///////////////////////////////////////////////////////////////////////
75
//
76
// STATE
77
//
78
///////////////////////////////////////////////////////////////////////
79

80     private DataValueDescriptor[] column;
81     private int ncols;
82
83     ///////////////////////////////////////////////////////////////////////
84
//
85
// CONSTRUCTORS
86
//
87
///////////////////////////////////////////////////////////////////////
88

89     /**
90      * Public niladic constructor. Needed for Formatable interface to work.
91      *
92      */

93     public ValueRow() {}
94
95     /**
96       * Make a value row with a designated number of column slots.
97       *
98       * @param ncols number of columns to allocate
99       */

100     public ValueRow(int ncols)
101     {
102          column = new DataValueDescriptor[ncols];
103          this.ncols = ncols;
104     }
105
106
107     ///////////////////////////////////////////////////////////////////////
108
//
109
// EXECROW INTERFACE
110
//
111
///////////////////////////////////////////////////////////////////////
112

113     // this is the actual current # of columns
114
public int nColumns() {
115         return ncols;
116     }
117
118     // get a new Object[] for the row
119
public void getNewObjectArray()
120     {
121         column = new DataValueDescriptor[ncols];
122     }
123
124     /*
125      * Row interface
126      */

127     // position is 1-based
128
public DataValueDescriptor getColumn (int position) {
129         if (position <= column.length)
130             return (DataValueDescriptor) (column[position-1]);
131         else
132             return (DataValueDescriptor)null;
133     }
134
135     // position is 1-based.
136
public void setColumn(int position, DataValueDescriptor col) {
137  
138         if (position > column.length)
139             realloc(position); // enough for this column
140
column[position-1] = col;
141     }
142
143
144     /*
145     ** ExecRow interface
146     */

147
148     // position is 1-based
149
public ExecRow getClone()
150     {
151         return getClone((FormatableBitSet) null);
152     }
153
154     public ExecRow getClone(FormatableBitSet clonedCols)
155     {
156         int numColumns = column.length;
157
158         /* Get the right type of row */
159         ExecRow rowClone = cloneMe();
160
161         for (int colCtr = 0; colCtr < numColumns; colCtr++)
162         {
163             // Copy those columns whose bit isn't set (and there is a FormatableBitSet)
164
if (clonedCols != null && !(clonedCols.get(colCtr + 1)))
165             {
166                 /* Rows are 1-based, column[] is 0-based */
167                 rowClone.setColumn(colCtr + 1, (DataValueDescriptor) column[colCtr]);
168                 continue;
169             }
170
171             if (column[colCtr] != null)
172             {
173                 /* Rows are 1-based, column[] is 0-based */
174                 rowClone.setColumn(colCtr + 1, column[colCtr].getClone());
175             }
176         }
177         return rowClone;
178     }
179
180     // position is 1-based
181
public ExecRow getNewNullRow()
182     {
183         int numColumns = column.length;
184         ExecRow rowClone = cloneMe();
185
186
187         for (int colCtr = 0; colCtr < numColumns; colCtr++)
188         {
189             if (column[colCtr] != null)
190             {
191                 /* Rows are 1-based, column[] is 0-based */
192                 if (column[colCtr] instanceof RowLocation)
193                 {
194                     /*
195                     ** The getClone() method for a RowLocation has the same
196                     ** name as for DataValueDescriptor, but it's on a different
197                     ** interface, so the cast must be different.
198                     **
199                     */

200                     rowClone.setColumn(colCtr + 1, column[colCtr].getClone());
201                 }
202                 else
203                 {
204                     // otherwise, get a new null
205
rowClone.setColumn(colCtr + 1,
206                         ((DataValueDescriptor) (column[colCtr])).getNewNull());
207                 }
208             }
209         }
210         return rowClone;
211     }
212
213     ExecRow cloneMe() {
214         return new ValueRow(ncols);
215     }
216
217     // position is 1-based
218
public final DataValueDescriptor cloneColumn(int columnPosition)
219     {
220         return column[columnPosition -1].getClone();
221     }
222
223     /*
224      * class interface
225      */

226     public String JavaDoc toString() {
227         // NOTE: This method is required for external functionality (the
228
// consistency checker), so do not put it under SanityManager.DEBUG.
229
String JavaDoc s = "{ ";
230         for (int i = 0; i < column.length; i++)
231         {
232             if (column[i] == null)
233                 s += "null";
234             else
235                 s += column[i].toString();
236             if (i < (column.length - 1))
237                 s += ", ";
238         }
239         s += " }";
240         return s;
241     }
242
243
244     /**
245         Get the array form of the row that Access expects.
246
247         @see ExecRow#getRowArray
248     */

249     public DataValueDescriptor[] getRowArray() {
250         return column;
251     }
252
253     /**
254         Get a clone of the array form of the row that Access expects.
255
256         @see ExecRow#getRowArray
257     */

258     public DataValueDescriptor[] getRowArrayClone()
259     {
260         int numColumns = column.length;
261         DataValueDescriptor[] columnClones = new DataValueDescriptor[numColumns];
262
263         for (int colCtr = 0; colCtr < numColumns; colCtr++)
264         {
265             if (column[colCtr] != null)
266             {
267                 columnClones[colCtr] = column[colCtr].getClone();
268             }
269         }
270
271         return columnClones;
272     }
273
274     /**
275      * Set the row array
276      *
277      * @see ExecRow#setRowArray
278      */

279     public void setRowArray(DataValueDescriptor[] value)
280     {
281         column = value;
282     }
283
284     public void setRowArray(Storable[] value) {
285         if (value instanceof DataValueDescriptor[]) {
286             column = (DataValueDescriptor[]) value;
287             return;
288         }
289
290         if ((column == null) || (column.length != value.length))
291             column = new DataValueDescriptor[value.length];
292
293
294         System.arraycopy(value, 0, column, 0, column.length);
295     }
296         
297     // Set the number of columns in the row to ncols, preserving
298
// the existing contents.
299
protected void realloc(int ncols) {
300         DataValueDescriptor[] newcol = new DataValueDescriptor[ncols];
301
302         System.arraycopy(column, 0, newcol, 0, column.length);
303         column = newcol;
304     }
305
306     ///////////////////////////////////////////////////////////////////////
307
//
308
// FORMATABLE INTERFACE
309
//
310
///////////////////////////////////////////////////////////////////////
311

312     /**
313      * Read this object from a stream of stored objects.
314      *
315      * @param in read this.
316      *
317      * @exception IOException thrown on error
318      * @exception ClassNotFoundException thrown on error
319      */

320     public void readExternal( ObjectInput JavaDoc in )
321          throws IOException JavaDoc, ClassNotFoundException JavaDoc
322     {
323         column = new DataValueDescriptor[ArrayUtil.readArrayLength(in)];
324         ArrayUtil.readArrayItems(in, column);
325         ncols = column.length;
326     }
327
328     /**
329      * Write this object to a stream of stored objects.
330      *
331      * @param out write bytes here.
332      *
333      * @exception IOException thrown on error
334      */

335     public void writeExternal( ObjectOutput JavaDoc out )
336          throws IOException JavaDoc
337     {
338         ArrayUtil.writeArrayLength(out, column);
339         ArrayUtil.writeArrayItems(out, column);
340     }
341
342     /**
343      * Get the formatID which corresponds to this class.
344      *
345      * @return the formatID of this class
346      */

347     public int getTypeFormatId() { return StoredFormatIds.VALUE_ROW_V01_ID; }
348
349 }
350
Popular Tags