001: /*
002:
003: Derby - Class org.apache.derby.iapi.types.SQLNationalChar
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.types.DataTypeDescriptor;
025: import org.apache.derby.iapi.types.DataValueDescriptor;
026: import org.apache.derby.iapi.types.TypeId;
027: import org.apache.derby.iapi.types.StringDataValue;
028: import org.apache.derby.iapi.types.DataValueDescriptor;
029: import org.apache.derby.iapi.types.BooleanDataValue;
030: import org.apache.derby.iapi.reference.SQLState;
031:
032: import org.apache.derby.iapi.types.Orderable;
033:
034: import org.apache.derby.iapi.services.io.Storable;
035: import org.apache.derby.iapi.services.io.FormatIdUtil;
036: import org.apache.derby.iapi.services.io.StoredFormatIds;
037:
038: import org.apache.derby.iapi.error.StandardException;
039: import org.apache.derby.iapi.services.sanity.SanityManager;
040:
041: import org.apache.derby.iapi.util.StringUtil;
042:
043: import org.apache.derby.iapi.services.i18n.LocaleFinder;
044:
045: import java.io.ObjectOutput;
046: import java.io.ObjectInput;
047: import java.io.IOException;
048: import java.io.UTFDataFormatException;
049: import java.io.EOFException;
050: import java.sql.Date;
051: import java.sql.ResultSet;
052: import java.sql.SQLException;
053: import java.sql.Time;
054: import java.sql.Timestamp;
055: import java.util.Locale;
056: import java.util.Calendar;
057:
058: /**
059: * SQLNationalChar satisfies the DataValueDescriptor
060: * interfaces (i.e., OrderableDataType). It implements an String holder,
061: * e.g. for storing a column value; it can be specified
062: * when constructed to not allow nulls. Nullability cannot be changed
063: * after construction.
064: * <p>
065: * Because OrderableDataType is a subclass of DataType,
066: * SQLNationalChar can play a role in either a DataType/ValueRow
067: * or a OrderableDataType/KeyRow, interchangeably.
068: */
069: public class SQLNationalChar extends SQLChar {
070:
071: /*
072: * DataValueDescriptor interface.
073: *
074: * These are actually all implemented in the super-class, but we need
075: * to duplicate some of them here so they can be called by byte-code
076: * generation, which needs to know the class the method appears in.
077: */
078:
079: public String getTypeName() {
080: return TypeId.NATIONAL_CHAR_NAME;
081: }
082:
083: /*
084: * Storable interface, implies Externalizable, TypedFormat
085: */
086:
087: /**
088: Return my format identifier.
089:
090: @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
091: */
092: public int getTypeFormatId() {
093: return StoredFormatIds.SQL_NATIONAL_CHAR_ID;
094: }
095:
096: /*
097: * DataValueDescriptor interface
098: */
099:
100: /** @see DataValueDescriptor#getClone */
101: public DataValueDescriptor getClone() {
102: try {
103: /* NOTE: We pass instance variables for locale info
104: * because we only call methods when we know that we
105: * will need locale info.
106: */
107: return new SQLNationalChar(getString(), getLocaleFinder());
108: } catch (StandardException se) {
109: if (SanityManager.DEBUG)
110: SanityManager.THROWASSERT("Unexpected exception " + se);
111: return null;
112: }
113: }
114:
115: /**
116: * @see DataValueDescriptor#getNewNull
117: *
118: */
119: public DataValueDescriptor getNewNull() {
120: /* NOTE: We pass instance variables for locale info
121: * because we only call methods when we know that we
122: * will need locale info.
123: */
124: SQLNationalChar result = new SQLNationalChar();
125: result.setLocaleFinder(getLocaleFinder());
126: return result;
127: }
128:
129: /*
130: * class interface
131: */
132:
133: /*
134: * constructors
135: */
136:
137: /**
138: no-arg constructor, required by Formattable.
139: */
140: public SQLNationalChar() {
141: }
142:
143: public SQLNationalChar(String val, LocaleFinder localeFinder) {
144: super (val);
145: setLocaleFinder(localeFinder);
146: }
147:
148: /**
149: * @see DataValueDescriptor#getDate
150: * @exception StandardException thrown on failure to convert
151: */
152: public Date getDate(Calendar cal) throws StandardException {
153: return nationalGetDate(cal);
154: }
155:
156: /**
157: * @see DataValueDescriptor#getTime
158: * @exception StandardException thrown on failure to convert
159: */
160: public Time getTime(Calendar cal) throws StandardException {
161: return nationalGetTime(cal);
162: }
163:
164: /**
165: * @see DataValueDescriptor#getTimestamp
166: * @exception StandardException thrown on failure to convert
167: */
168: public Timestamp getTimestamp(Calendar cal)
169: throws StandardException {
170: return nationalGetTimestamp(cal);
171: }
172:
173: /**
174: * @see DataValueDescriptor#setValue
175: *
176: * @exception StandardException Thrown on error
177: */
178: public void setValue(Date theValue, Calendar cal)
179: throws StandardException {
180: setValue(getDateFormat(cal).format(theValue));
181: }
182:
183: /**
184: * @see DataValueDescriptor#setValue
185: *
186: * @exception StandardException Thrown on error
187: */
188: public void setValue(Time theValue, Calendar cal)
189: throws StandardException {
190: setValue(getTimeFormat(cal).format(theValue));
191: }
192:
193: /**
194: * @see DataValueDescriptor#setValue
195: *
196: * @exception StandardException Thrown on error
197: */
198: public void setValue(Timestamp theValue, Calendar cal)
199: throws StandardException {
200: setValue(getTimestampFormat(cal).format(theValue));
201: }
202:
203: /*
204: * DataValueDescriptor interface
205: */
206:
207: /** @see DataValueDescriptor#typePrecedence */
208: public int typePrecedence() {
209: return TypeId.NATIONAL_CHAR_PRECEDENCE;
210: }
211:
212: /**
213: * Compare two SQLChars. This method will be overriden in the
214: * National char wrappers so that the appropriate comparison
215: * is done.
216: *
217: * @exception StandardException Thrown on error
218: */
219: protected int stringCompare(SQLChar char1, SQLChar char2)
220: throws StandardException {
221: return char1.stringCollatorCompare(char2);
222: }
223:
224: /**
225: * Get a SQLVarchar for a built-in string function.
226: * (Could be either a SQLVarchar or SQLNationalVarchar.)
227: *
228: * @return a SQLVarchar or SQLNationalVarchar.
229: */
230: protected StringDataValue getNewVarchar() throws StandardException {
231: SQLNationalVarchar result = new SQLNationalVarchar();
232: result.setLocaleFinder(getLocaleFinder());
233: return result;
234: }
235:
236: /**
237: * Normalization method - this method may be called when putting
238: * a value into a SQLChar, for example, when inserting into a SQLChar
239: * column. See NormalizeResultSet in execution.
240: *
241: * @param desiredType The type to normalize the source column to
242: * @param source The value to normalize
243: *
244: * @exception StandardException Thrown for null into
245: * non-nullable column, and for
246: * truncation error
247: */
248:
249: public void normalize(DataTypeDescriptor desiredType,
250: DataValueDescriptor source) throws StandardException {
251:
252: normalize(desiredType, ((DataType) source)
253: .getNationalString(getLocaleFinder()));
254:
255: }
256:
257: protected void setFrom(DataValueDescriptor theValue)
258: throws StandardException {
259:
260: setValue(((DataType) theValue)
261: .getNationalString(getLocaleFinder()));
262: }
263:
264: /**
265: * Return whether or not this is a national character datatype.
266: */
267: protected boolean isNationalString() {
268: return true;
269: }
270:
271: /** @see java.lang.Object#hashCode */
272: public int hashCode() {
273: return nationalHashCode();
274: }
275: }
|