001: /*
002:
003: Derby - Class org.apache.derby.iapi.types.SQLRef
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.types;
023:
024: import org.apache.derby.iapi.services.io.ArrayInputStream;
025:
026: import org.apache.derby.iapi.error.StandardException;
027:
028: import org.apache.derby.iapi.types.DataTypeDescriptor;
029: import org.apache.derby.iapi.types.DataValueDescriptor;
030: import org.apache.derby.iapi.types.TypeId;
031: import org.apache.derby.iapi.reference.SQLState;
032:
033: import org.apache.derby.iapi.types.DataValueDescriptor;
034:
035: import org.apache.derby.iapi.types.RowLocation;
036: import org.apache.derby.iapi.types.Orderable;
037:
038: import org.apache.derby.iapi.services.io.StoredFormatIds;
039:
040: import org.apache.derby.iapi.services.sanity.SanityManager;
041:
042: import org.apache.derby.catalog.TypeDescriptor;
043:
044: import org.apache.derby.iapi.types.DataType;
045: import org.apache.derby.iapi.types.RefDataValue;
046: import org.apache.derby.iapi.services.cache.ClassSize;
047:
048: import java.io.ObjectOutput;
049: import java.io.ObjectInput;
050: import java.io.IOException;
051:
052: import java.sql.ResultSet;
053: import java.sql.PreparedStatement;
054:
055: public class SQLRef extends DataType implements RefDataValue {
056: protected RowLocation value;
057:
058: private static final int BASE_MEMORY_USAGE = ClassSize
059: .estimateBaseFromCatalog(SQLRef.class);
060:
061: public int estimateMemoryUsage() {
062: int sz = BASE_MEMORY_USAGE;
063: if (null != value)
064: sz += value.estimateMemoryUsage();
065: return sz;
066: } // end of estimateMemoryUsage
067:
068: /*
069: ** DataValueDescriptor interface
070: ** (mostly implemented in DataType)
071: */
072:
073: public String getString() {
074: if (value != null) {
075: return value.toString();
076: } else {
077: return null;
078: }
079: }
080:
081: public Object getObject() {
082: return value;
083: }
084:
085: protected void setFrom(DataValueDescriptor theValue)
086: throws StandardException {
087:
088: if (theValue.isNull())
089: setToNull();
090: else
091: value = (RowLocation) theValue.getObject();
092: }
093:
094: public int getLength() {
095: return TypeDescriptor.MAXIMUM_WIDTH_UNKNOWN;
096: }
097:
098: /* this is for DataType's error generator */
099: public String getTypeName() {
100: return TypeId.REF_NAME;
101: }
102:
103: /*
104: * Storable interface, implies Externalizable, TypedFormat
105: */
106:
107: /**
108: Return my format identifier.
109:
110: @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
111: */
112: public int getTypeFormatId() {
113: return StoredFormatIds.SQL_REF_ID;
114: }
115:
116: public boolean isNull() {
117: return (value == null);
118: }
119:
120: public void writeExternal(ObjectOutput out) throws IOException {
121:
122: if (SanityManager.DEBUG)
123: SanityManager
124: .ASSERT(value != null,
125: "writeExternal() is not supposed to be called for null values.");
126:
127: out.writeObject(value);
128: }
129:
130: /**
131: * @see java.io.Externalizable#readExternal
132: *
133: * @exception IOException Thrown on error reading the object
134: * @exception ClassNotFoundException Thrown if the class of the object
135: * read from the stream can't be found
136: * (not likely, since it's supposed to
137: * be SQLRef).
138: */
139: public void readExternal(ObjectInput in) throws IOException,
140: ClassNotFoundException {
141: value = (RowLocation) in.readObject();
142: }
143:
144: public void readExternalFromArray(ArrayInputStream in)
145: throws IOException, ClassNotFoundException {
146: value = (RowLocation) in.readObject();
147: }
148:
149: /**
150: * @see org.apache.derby.iapi.services.io.Storable#restoreToNull
151: */
152:
153: public void restoreToNull() {
154: value = null;
155: }
156:
157: /*
158: ** Orderable interface
159: */
160:
161: /** @exception StandardException Thrown on error */
162: public boolean compare(int op, DataValueDescriptor other,
163: boolean orderedNulls, boolean unknownRV)
164: throws StandardException {
165: return value.compare(op, ((SQLRef) other).value, orderedNulls,
166: unknownRV);
167: }
168:
169: /** @exception StandardException Thrown on error */
170: public int compare(DataValueDescriptor other)
171: throws StandardException {
172: return value.compare(((SQLRef) other).value);
173: }
174:
175: /*
176: * DataValueDescriptor interface
177: */
178:
179: /** @see DataValueDescriptor#getClone */
180: public DataValueDescriptor getClone() {
181: /* In order to avoid a throws clause nightmare, we only call
182: * the constructors which do not have a throws clause.
183: *
184: * Clone the underlying RowLocation, if possible, so that we
185: * don't clobber the value in the clone.
186: */
187: if (value == null)
188: return new SQLRef();
189: else
190: return new SQLRef((RowLocation) value.cloneObject());
191: }
192:
193: /**
194: * @see DataValueDescriptor#getNewNull
195: */
196: public DataValueDescriptor getNewNull() {
197: return new SQLRef();
198: }
199:
200: /**
201: * @see DataValueDescriptor#setValueFromResultSet
202: *
203: */
204: public void setValueFromResultSet(ResultSet resultSet,
205: int colNumber, boolean isNullable) {
206: if (SanityManager.DEBUG)
207: SanityManager
208: .THROWASSERT("setValueFromResultSet() is not supposed to be called for SQLRef.");
209: }
210:
211: public void setInto(PreparedStatement ps, int position) {
212: if (SanityManager.DEBUG)
213: SanityManager
214: .THROWASSERT("setValueInto(PreparedStatement) is not supposed to be called for SQLRef.");
215: }
216:
217: /*
218: ** Class interface
219: */
220:
221: /*
222: ** Constructors
223: */
224:
225: public SQLRef() {
226: }
227:
228: public SQLRef(RowLocation rowLocation) {
229: value = rowLocation;
230: }
231:
232: public void setValue(RowLocation rowLocation) {
233: value = rowLocation;
234: }
235:
236: /*
237: ** String display of value
238: */
239:
240: public String toString() {
241: if (value == null)
242: return "NULL";
243: else
244: return value.toString();
245: }
246: }
|