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