--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.types.SQLVarchar\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.types;\r
+\r
+import java.text.RuleBasedCollator;\r
+\r
+import org.apache.derby.iapi.types.DataTypeDescriptor;\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+import org.apache.derby.iapi.types.TypeId;\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+import org.apache.derby.iapi.types.StringDataValue;\r
+import org.apache.derby.iapi.reference.SQLState;\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.services.io.FormatIdUtil;\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.util.StringUtil;\r
+\r
+/**\r
+ * SQLVarchar represents a VARCHAR value with UCS_BASIC collation.\r
+ *\r
+ * SQLVarchar is mostly the same as SQLChar, so it is implemented as a\r
+ * subclass of SQLChar. Only those methods with different behavior are\r
+ * implemented here.\r
+ */\r
+public class SQLVarchar\r
+ extends SQLChar\r
+{\r
+\r
+ /*\r
+ * DataValueDescriptor interface.\r
+ *\r
+ */\r
+\r
+ public String getTypeName()\r
+ {\r
+ return TypeId.VARCHAR_NAME;\r
+ }\r
+\r
+ /*\r
+ * DataValueDescriptor interface\r
+ */\r
+\r
+ /** @see DataValueDescriptor#getClone */\r
+ public DataValueDescriptor getClone()\r
+ {\r
+ try\r
+ {\r
+ return new SQLVarchar(getString());\r
+ }\r
+ catch (StandardException se)\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.THROWASSERT("Unexpected exception", se);\r
+ return null;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @see DataValueDescriptor#getNewNull\r
+ *\r
+ */\r
+ public DataValueDescriptor getNewNull()\r
+ {\r
+ return new SQLVarchar();\r
+ }\r
+\r
+ /** @see StringDataValue#getValue(RuleBasedCollator) */\r
+ public StringDataValue getValue(RuleBasedCollator collatorForComparison)\r
+ {\r
+ if (collatorForComparison == null)\r
+ {//null collatorForComparison means use UCS_BASIC for collation\r
+ return this; \r
+ } else {\r
+ //non-null collatorForComparison means use collator sensitive\r
+ //implementation of SQLVarchar\r
+ CollatorSQLVarchar s = new CollatorSQLVarchar(collatorForComparison);\r
+ s.copyState(this);\r
+ return s;\r
+ }\r
+ }\r
+\r
+\r
+ /*\r
+ * Storable interface, implies Externalizable, TypedFormat\r
+ */\r
+\r
+ /**\r
+ Return my format identifier.\r
+\r
+ @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId\r
+ */\r
+ public int getTypeFormatId() {\r
+ return StoredFormatIds.SQL_VARCHAR_ID;\r
+ }\r
+\r
+ /*\r
+ * constructors\r
+ */\r
+\r
+ public SQLVarchar()\r
+ {\r
+ }\r
+\r
+ public SQLVarchar(String val)\r
+ {\r
+ super(val);\r
+ }\r
+\r
+ /**\r
+ * Normalization method - this method may be called when putting\r
+ * a value into a SQLVarchar, for example, when inserting into a SQLVarchar\r
+ * column. See NormalizeResultSet in execution.\r
+ *\r
+ * @param desiredType The type to normalize the source column to\r
+ * @param source The value to normalize\r
+ *\r
+ *\r
+ * @exception StandardException Thrown for null into\r
+ * non-nullable column, and for\r
+ * truncation error\r
+ */\r
+\r
+ public void normalize(\r
+ DataTypeDescriptor desiredType,\r
+ DataValueDescriptor source)\r
+ throws StandardException\r
+ {\r
+ normalize(desiredType, source.getString());\r
+ }\r
+\r
+ protected void normalize(DataTypeDescriptor desiredType, String sourceValue)\r
+ throws StandardException\r
+ {\r
+\r
+ int desiredWidth = desiredType.getMaximumWidth();\r
+\r
+ int sourceWidth = sourceValue.length();\r
+\r
+ /*\r
+ ** If the input is already the right length, no normalization is\r
+ ** necessary.\r
+ **\r
+ ** It's OK for a Varchar value to be shorter than the desired width.\r
+ ** This can happen, for example, if you insert a 3-character Varchar\r
+ ** value into a 10-character Varchar column. Just return the value\r
+ ** in this case.\r
+ */\r
+\r
+ if (sourceWidth > desiredWidth) {\r
+\r
+ hasNonBlankChars(sourceValue, desiredWidth, sourceWidth);\r
+\r
+ /*\r
+ ** No non-blank characters will be truncated. Truncate the blanks\r
+ ** to the desired width.\r
+ */\r
+ sourceValue = sourceValue.substring(0, desiredWidth);\r
+ }\r
+\r
+ setValue(sourceValue);\r
+ }\r
+\r
+\r
+ /*\r
+ * DataValueDescriptor interface\r
+ */\r
+\r
+ /* @see DataValueDescriptor#typePrecedence */\r
+ public int typePrecedence()\r
+ {\r
+ return TypeId.VARCHAR_PRECEDENCE;\r
+ }\r
+ \r
+ /**\r
+ * returns the reasonable minimum amount by \r
+ * which the array can grow . See readExternal. \r
+ * when we know that the array needs to grow by at least\r
+ * one byte, it is not performant to grow by just one byte\r
+ * instead this amount is used to provide a resonable growby size.\r
+ * @return minimum reasonable growby size\r
+ */\r
+ protected final int growBy()\r
+ {\r
+ return RETURN_SPACE_THRESHOLD; //seems reasonable for a varchar or clob \r
+ }\r
+}\r