KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > types > SQLRef


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLRef
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.iapi.types;
23
24 import org.apache.derby.iapi.services.io.ArrayInputStream;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.types.DataTypeDescriptor;
29 import org.apache.derby.iapi.types.DataValueDescriptor;
30 import org.apache.derby.iapi.types.TypeId;
31 import org.apache.derby.iapi.reference.SQLState;
32
33 import org.apache.derby.iapi.types.DataValueDescriptor;
34
35 import org.apache.derby.iapi.types.RowLocation;
36 import org.apache.derby.iapi.types.Orderable;
37
38 import org.apache.derby.iapi.services.io.StoredFormatIds;
39
40 import org.apache.derby.iapi.services.sanity.SanityManager;
41
42 import org.apache.derby.catalog.TypeDescriptor;
43
44 import org.apache.derby.iapi.types.DataType;
45 import org.apache.derby.iapi.types.RefDataValue;
46 import org.apache.derby.iapi.services.cache.ClassSize;
47
48 import java.io.ObjectOutput JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.IOException JavaDoc;
51
52 import java.sql.ResultSet JavaDoc;
53 import java.sql.PreparedStatement JavaDoc;
54
55 public class SQLRef extends DataType implements RefDataValue
56 {
57     protected RowLocation value;
58
59     private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLRef.class);
60
61     public int estimateMemoryUsage()
62     {
63         int sz = BASE_MEMORY_USAGE;
64         if( null != value)
65             sz += value.estimateMemoryUsage();
66         return sz;
67     } // end of estimateMemoryUsage
68

69     /*
70     ** DataValueDescriptor interface
71     ** (mostly implemented in DataType)
72     */

73
74     public String JavaDoc getString()
75     {
76         if (value != null)
77         {
78             return value.toString();
79         }
80         else
81         {
82             return null;
83         }
84     }
85
86     public Object JavaDoc getObject()
87     {
88         return value;
89     }
90
91     protected void setFrom(DataValueDescriptor theValue) throws StandardException {
92
93         if (theValue.isNull())
94             setToNull();
95         else
96             value = (RowLocation) theValue.getObject();
97     }
98
99     public int getLength()
100     {
101         return TypeDescriptor.MAXIMUM_WIDTH_UNKNOWN;
102     }
103
104     /* this is for DataType's error generator */
105     public String JavaDoc getTypeName()
106     {
107         return TypeId.REF_NAME;
108     }
109
110     /*
111      * Storable interface, implies Externalizable, TypedFormat
112      */

113
114
115     /**
116         Return my format identifier.
117
118         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
119     */

120     public int getTypeFormatId() {
121         return StoredFormatIds.SQL_REF_ID;
122     }
123
124     public boolean isNull()
125     {
126         return (value == null);
127     }
128
129     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
130
131         if (SanityManager.DEBUG)
132             SanityManager.ASSERT(value != null, "writeExternal() is not supposed to be called for null values.");
133
134         out.writeObject(value);
135     }
136
137     /**
138      * @see java.io.Externalizable#readExternal
139      *
140      * @exception IOException Thrown on error reading the object
141      * @exception ClassNotFoundException Thrown if the class of the object
142      * read from the stream can't be found
143      * (not likely, since it's supposed to
144      * be SQLRef).
145      */

146     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
147     {
148         value = (RowLocation) in.readObject();
149     }
150     public void readExternalFromArray(ArrayInputStream in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
151     {
152         value = (RowLocation) in.readObject();
153     }
154
155     /**
156      * @see org.apache.derby.iapi.services.io.Storable#restoreToNull
157      */

158
159     public void restoreToNull()
160     {
161         value = null;
162     }
163
164     /*
165     ** Orderable interface
166     */

167
168     /** @exception StandardException Thrown on error */
169     public boolean compare(int op,
170                            DataValueDescriptor other,
171                            boolean orderedNulls,
172                            boolean unknownRV)
173                     throws StandardException
174     {
175         return value.compare(op,
176                             ((SQLRef) other).value,
177                             orderedNulls,
178                             unknownRV);
179     }
180
181     /** @exception StandardException Thrown on error */
182     public int compare(DataValueDescriptor other) throws StandardException
183     {
184         return value.compare(((SQLRef) other).value);
185     }
186
187     /*
188      * DataValueDescriptor interface
189      */

190
191     /** @see DataValueDescriptor#getClone */
192     public DataValueDescriptor getClone()
193     {
194         /* In order to avoid a throws clause nightmare, we only call
195          * the constructors which do not have a throws clause.
196          *
197          * Clone the underlying RowLocation, if possible, so that we
198          * don't clobber the value in the clone.
199          */

200         if (value == null)
201             return new SQLRef();
202         else
203             return new SQLRef((RowLocation) value.cloneObject());
204     }
205
206     /**
207      * @see DataValueDescriptor#getNewNull
208      */

209     public DataValueDescriptor getNewNull()
210     {
211         return new SQLRef();
212     }
213
214     /**
215      * @see DataValueDescriptor#setValueFromResultSet
216      *
217      */

218     public void setValueFromResultSet(ResultSet JavaDoc resultSet, int colNumber,
219                                       boolean isNullable)
220     {
221         if (SanityManager.DEBUG)
222             SanityManager.THROWASSERT(
223                 "setValueFromResultSet() is not supposed to be called for SQLRef.");
224     }
225     public void setInto(PreparedStatement JavaDoc ps, int position) {
226         if (SanityManager.DEBUG)
227             SanityManager.THROWASSERT(
228                 "setValueInto(PreparedStatement) is not supposed to be called for SQLRef.");
229     }
230
231     /*
232     ** Class interface
233     */

234
235     /*
236     ** Constructors
237     */

238
239     public SQLRef()
240     {
241     }
242
243     public SQLRef(RowLocation rowLocation)
244     {
245         value = rowLocation;
246     }
247
248     public void setValue(RowLocation rowLocation)
249     {
250         value = rowLocation;
251     }
252
253     /*
254     ** String display of value
255     */

256
257     public String JavaDoc toString()
258     {
259         if (value == null)
260             return "NULL";
261         else
262             return value.toString();
263     }
264 }
265
Popular Tags