001: /*
002:
003: Derby - Class org.apache.derby.iapi.types.SQLLongvarchar
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.util.StringUtil;
037:
038: /**
039: * SQLLongvarchar satisfies the DataValueDescriptor interfaces (i.e., OrderableDataType). It implements a String
040: * holder, e.g. for storing a column value; it can be specified
041: * when constructed to not allow nulls. Nullability cannot be changed
042: * after construction.
043: * <p>
044: * Because OrderableDataType is a subclass of DataType,
045: * SQLLongvarchar can play a role in either a DataType/ValueRow
046: * or a OrderableDataType/KeyRow, interchangeably.
047: *
048: * SQLLongvarchar is mostly the same as SQLVarchar, so it is implemented as a
049: * subclass of SQLVarchar. Only those methods with different behavior are
050: * implemented here.
051: */
052: public class SQLLongvarchar extends SQLVarchar {
053: /*
054: * DataValueDescriptor interface.
055: *
056: * These are actually all implemented in the super-class, but we need
057: * to duplicate some of them here so they can be called by byte-code
058: * generation, which needs to know the class the method appears in.
059: */
060:
061: public String getTypeName() {
062: return TypeId.LONGVARCHAR_NAME;
063: }
064:
065: /*
066: * DataValueDescriptor interface
067: */
068:
069: /** @see DataValueDescriptor#getClone */
070: public DataValueDescriptor getClone() {
071: try {
072: return new SQLLongvarchar(getString());
073: } catch (StandardException se) {
074: if (SanityManager.DEBUG)
075: SanityManager.THROWASSERT("Unexpected exception " + se);
076: return null;
077: }
078: }
079:
080: /**
081: * @see DataValueDescriptor#getNewNull
082: *
083: */
084: public DataValueDescriptor getNewNull() {
085: return new SQLLongvarchar();
086: }
087:
088: /*
089: * Storable interface, implies Externalizable, TypedFormat
090: */
091:
092: /**
093: Return my format identifier.
094:
095: @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
096: */
097: public int getTypeFormatId() {
098: return StoredFormatIds.SQL_LONGVARCHAR_ID;
099: }
100:
101: /*
102: * constructors
103: */
104:
105: public SQLLongvarchar() {
106: }
107:
108: public SQLLongvarchar(String val) {
109: super (val);
110: }
111:
112: protected void normalize(DataTypeDescriptor desiredType,
113: String sourceValue) throws StandardException {
114: //bug 5592 - for sql long varchar, any truncation is disallowed ie even the trailing blanks can't be truncated
115: if (sourceValue.length() > desiredType.getMaximumWidth())
116: throw StandardException.newException(
117: SQLState.LANG_STRING_TRUNCATION, getTypeName(),
118: StringUtil.formatForPrint(sourceValue), String
119: .valueOf(desiredType.getMaximumWidth()));
120:
121: setValue(sourceValue);
122: }
123:
124: /**
125: * @see StringDataValue#concatenate
126: *
127: * @exception StandardException Thrown on error
128: */
129: public StringDataValue concatenate(StringDataValue leftOperand,
130: StringDataValue rightOperand, StringDataValue result)
131: throws StandardException {
132: super .concatenate(leftOperand, rightOperand, result);
133:
134: //bug 5600 - according to db2 concatenation documentation, for compatibility with previous versions, there is no automatic
135: //escalation of results involving LONG data types to LOB data types. For eg, concatenation of a CHAR(200) value and a
136: //completely full LONG VARCHAR value would result in an error rather than in a promotion to a CLOB data type
137:
138: //need to check for concatenated string for null value
139: if ((result.getString() != null)
140: && (result.getString().length() > TypeId.LONGVARCHAR_MAXWIDTH))
141: throw StandardException.newException(
142: SQLState.LANG_CONCAT_STRING_OVERFLOW, "CONCAT",
143: String.valueOf(TypeId.LONGVARCHAR_MAXWIDTH));
144:
145: return result;
146: }
147:
148: /*
149: * DataValueDescriptor interface
150: */
151:
152: /* @see DataValueDescriptor#typePrecedence */
153: public int typePrecedence() {
154: return TypeId.LONGVARCHAR_PRECEDENCE;
155: }
156: }
|