KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > types > SQLLongvarchar


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLLongvarchar
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.types;
23
24 import org.apache.derby.iapi.types.DataTypeDescriptor;
25 import org.apache.derby.iapi.types.DataValueDescriptor;
26 import org.apache.derby.iapi.types.TypeId;
27 import org.apache.derby.iapi.types.StringDataValue;
28 import org.apache.derby.iapi.reference.SQLState;
29 import org.apache.derby.iapi.error.StandardException;
30
31 import org.apache.derby.iapi.services.io.FormatIdUtil;
32 import org.apache.derby.iapi.services.io.StoredFormatIds;
33
34 import org.apache.derby.iapi.services.sanity.SanityManager;
35
36 import org.apache.derby.iapi.util.StringUtil;
37
38 /**
39  * SQLLongvarchar satisfies the DataValueDescriptor interfaces (i.e., OrderableDataType). It implements a String
40  * holder, e.g. for storing a column value; it can be specified
41  * when constructed to not allow nulls. Nullability cannot be changed
42  * after construction.
43  * <p>
44  * Because OrderableDataType is a subclass of DataType,
45  * SQLLongvarchar can play a role in either a DataType/ValueRow
46  * or a OrderableDataType/KeyRow, interchangeably.
47  *
48  * SQLLongvarchar is mostly the same as SQLVarchar, so it is implemented as a
49  * subclass of SQLVarchar. Only those methods with different behavior are
50  * implemented here.
51  */

52 public class SQLLongvarchar
53     extends SQLVarchar
54 {
55     /*
56      * DataValueDescriptor interface.
57      *
58      * These are actually all implemented in the super-class, but we need
59      * to duplicate some of them here so they can be called by byte-code
60      * generation, which needs to know the class the method appears in.
61      */

62
63     public String JavaDoc getTypeName()
64     {
65         return TypeId.LONGVARCHAR_NAME;
66     }
67
68     /*
69      * DataValueDescriptor interface
70      */

71
72     /** @see DataValueDescriptor#getClone */
73     public DataValueDescriptor getClone()
74     {
75         try
76         {
77             return new SQLLongvarchar(getString());
78         }
79         catch (StandardException se)
80         {
81             if (SanityManager.DEBUG)
82                 SanityManager.THROWASSERT("Unexpected exception " + se);
83             return null;
84         }
85     }
86
87     /**
88      * @see DataValueDescriptor#getNewNull
89      *
90      */

91     public DataValueDescriptor getNewNull()
92     {
93         return new SQLLongvarchar();
94     }
95
96     /*
97      * Storable interface, implies Externalizable, TypedFormat
98      */

99
100     /**
101         Return my format identifier.
102
103         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
104     */

105     public int getTypeFormatId() {
106         return StoredFormatIds.SQL_LONGVARCHAR_ID;
107     }
108
109     /*
110      * constructors
111      */

112
113     public SQLLongvarchar()
114     {
115     }
116
117     public SQLLongvarchar(String JavaDoc val)
118     {
119         super(val);
120     }
121
122     protected void normalize(DataTypeDescriptor desiredType, String JavaDoc sourceValue)
123         throws StandardException
124     {
125         //bug 5592 - for sql long varchar, any truncation is disallowed ie even the trailing blanks can't be truncated
126
if (sourceValue.length() > desiredType.getMaximumWidth())
127             throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), StringUtil.formatForPrint(sourceValue), String.valueOf(desiredType.getMaximumWidth()));
128
129         setValue(sourceValue);
130     }
131
132     /**
133      * @see StringDataValue#concatenate
134      *
135      * @exception StandardException Thrown on error
136      */

137     public StringDataValue concatenate(
138                 StringDataValue leftOperand,
139                 StringDataValue rightOperand,
140                 StringDataValue result)
141         throws StandardException
142     {
143         super.concatenate(leftOperand, rightOperand, result);
144
145         //bug 5600 - according to db2 concatenation documentation, for compatibility with previous versions, there is no automatic
146
//escalation of results involving LONG data types to LOB data types. For eg, concatenation of a CHAR(200) value and a
147
//completely full LONG VARCHAR value would result in an error rather than in a promotion to a CLOB data type
148

149         //need to check for concatenated string for null value
150
if ((result.getString() != null) && (result.getString().length() > TypeId.LONGVARCHAR_MAXWIDTH))
151             throw StandardException.newException(SQLState.LANG_CONCAT_STRING_OVERFLOW, "CONCAT", String.valueOf(TypeId.LONGVARCHAR_MAXWIDTH));
152
153         return result;
154     }
155
156     /*
157      * DataValueDescriptor interface
158      */

159
160     /* @see DataValueDescriptor#typePrecedence */
161     public int typePrecedence()
162     {
163         return TypeId.LONGVARCHAR_PRECEDENCE;
164     }
165 }
166
Popular Tags