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