KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > GenericResultDescription


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.GenericResultDescription
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;
23
24 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
25 import org.apache.derby.iapi.sql.ResultDescription;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import org.apache.derby.iapi.services.io.StoredFormatIds;
30 import org.apache.derby.iapi.services.io.FormatIdUtil;
31 import org.apache.derby.iapi.services.io.Formatable;
32
33 import java.io.ObjectOutput JavaDoc;
34 import java.io.ObjectInput JavaDoc;
35 import java.io.IOException JavaDoc;
36 /**
37  * GenericResultDescription: basic implementation of result
38  * description, used in conjunction with the other
39  * implementations in this package. This implementation
40  * of ResultDescription may be used by anyone.
41  *
42  * @author ames
43  */

44 public final class GenericResultDescription
45     implements ResultDescription, Formatable
46 {
47
48     /********************************************************
49     **
50     ** This class implements Formatable. That means that it
51     ** can write itself to and from a formatted stream. If
52     ** you add more fields to this class, make sure that you
53     ** also write/read them with the writeExternal()/readExternal()
54     ** methods.
55     **
56     ** If, inbetween releases, you add more fields to this class,
57     ** then you should bump the version number emitted by the getTypeFormatId()
58     ** method.
59     **
60     ********************************************************/

61
62     private ResultColumnDescriptor[] columns;
63     private String JavaDoc statementType;
64     
65     /**
66      * Niladic constructor for Formatable
67      */

68     public GenericResultDescription()
69     {
70     }
71
72     /**
73      * Build a GenericResultDescription from columns and type
74      *
75      * @param columns an array of col descriptors
76      * @param statementType the type
77      */

78     public GenericResultDescription(ResultColumnDescriptor[] columns,
79                     String JavaDoc statementType)
80     {
81         this.columns = columns;
82         this.statementType = statementType;
83     }
84
85     /**
86      * Build a GenericResultDescription
87      *
88      * @param rd the result description
89      * @param theCols the columns to take from the input rd
90      */

91     public GenericResultDescription
92     (
93         ResultDescription rd,
94         int[] theCols
95     )
96     {
97         if (SanityManager.DEBUG)
98         {
99             SanityManager.ASSERT(theCols != null, "theCols argument to GenericResultDescription is null");
100         }
101
102         this.columns = new ResultColumnDescriptor[theCols.length];
103         for (int i = 0; i < theCols.length; i++)
104         {
105             columns[i] = rd.getColumnDescriptor(theCols[i]);
106         }
107         this.statementType = rd.getStatementType();
108     }
109
110     //
111
// ResultDescription interface
112
//
113
/**
114      * @see ResultDescription#getStatementType
115      */

116     public String JavaDoc getStatementType() {
117         return statementType;
118     }
119
120     /**
121      * @see ResultDescription#getColumnCount
122      */

123     public int getColumnCount()
124     {
125         return (columns == null) ? 0 : columns.length;
126     }
127
128     public ResultColumnDescriptor[] getColumnInfo() {
129         return columns;
130     }
131
132     /**
133      * position is 1-based.
134      * @see ResultDescription#getColumnDescriptor
135      */

136     public ResultColumnDescriptor getColumnDescriptor(int position) {
137         return columns[position-1];
138     }
139
140     /**
141      * Get a new result description that has been truncated
142      * from input column number. If the input column is
143      * 5, then columns 5 to getColumnCount() are removed.
144      * The new ResultDescription points to the same
145      * ColumnDescriptors (this method performs a shallow
146      * copy.
147      *
148      * @param truncateFrom the starting column to remove
149      *
150      * @return a new ResultDescription
151      */

152     public ResultDescription truncateColumns(int truncateFrom)
153     {
154         if (SanityManager.DEBUG)
155         {
156             if (!(truncateFrom > 0 && columns != null))
157             {
158                 SanityManager.THROWASSERT("bad truncate value: "+truncateFrom+" is too low");
159             }
160             if (truncateFrom > columns.length)
161             {
162                 SanityManager.THROWASSERT("bad truncate value: "+truncateFrom+" is too high");
163             }
164         }
165         ResultColumnDescriptor[] newColumns = new ResultColumnDescriptor[truncateFrom-1];
166         System.arraycopy(columns, 0, newColumns, 0, newColumns.length);
167         return new GenericResultDescription(newColumns, statementType);
168     }
169
170
171     //////////////////////////////////////////////
172
//
173
// FORMATABLE
174
//
175
//////////////////////////////////////////////
176
/**
177      * Write this object out
178      *
179      * @param out write bytes here
180      *
181      * @exception IOException thrown on error
182      */

183     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
184     {
185         int len = (columns == null) ? 0 : columns.length;
186
187         out.writeObject(statementType);
188         out.writeInt(len);
189         while(len-- > 0)
190         {
191             /*
192             ** If we don't have a GenericColumnsDescriptor,
193             ** create one now and use that to write out.
194             ** Do this to avoid writing out query tree
195             ** implementations of ResultColumnDescriptor
196             */

197             if (!(columns[len] instanceof
198                         GenericColumnDescriptor))
199             {
200                 columns[len] = new GenericColumnDescriptor(columns[len]);
201             }
202             out.writeObject(columns[len]);
203         }
204     }
205
206     /**
207      * Read this object from a stream of stored objects.
208      *
209      * @param in read this.
210      *
211      * @exception IOException thrown on error
212      * @exception ClassNotFoundException thrown on error
213      */

214     public void readExternal(ObjectInput JavaDoc in)
215         throws IOException JavaDoc, ClassNotFoundException JavaDoc
216     {
217         int len;
218
219         columns = null;
220         statementType = (String JavaDoc)in.readObject();
221         len = in.readInt();
222         if (len > 0)
223         {
224             columns = new GenericColumnDescriptor[len];
225             while(len-- > 0)
226             {
227                 columns[len] = (ResultColumnDescriptor)in.readObject();
228             }
229         }
230     }
231     
232     /**
233      * Get the formatID which corresponds to this class.
234      *
235      * @return the formatID of this class
236      */

237     public int getTypeFormatId() { return StoredFormatIds.GENERIC_RESULT_DESCRIPTION_V01_ID; }
238
239
240     
241     public String JavaDoc toString()
242     {
243         if (SanityManager.DEBUG)
244         {
245             StringBuffer JavaDoc colStr = new StringBuffer JavaDoc();
246             for (int i = 0; i < columns.length; i++)
247             {
248                 colStr.append("column["+i+"]\n");
249                 colStr.append(columns[i].toString());
250             }
251             return "GenericResultDescription\n" +
252                     "\tStatementType = "+statementType+"\n" +
253                     "\tCOLUMNS\n" + colStr.toString();
254         }
255         else
256         {
257             return "";
258         }
259     }
260 }
261
262
Popular Tags