--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.types.CollatorSQLVarchar\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 org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import java.text.RuleBasedCollator;\r
+\r
+/**\r
+ * CollatorSQLVarchar class differs from SQLVarchar based on how the 2 classes \r
+ * use different collations to collate their data. SQLVarchar uses Derby's \r
+ * default collation which is UCS_BASIC. Whereas, this class uses the \r
+ * RuleBasedCollator object that was passed to it in it's constructor and that \r
+ * RuleBasedCollator object decides the collation.\r
+ * \r
+ * In Derby 10.3, this class will be passed a RuleBasedCollator which is based \r
+ * on the database's territory. In future releases of Derby, this class can be \r
+ * used to do other kinds of collations like case-insensitive collation etc by \r
+ * just passing an appropriate RuleBasedCollator object for that kind of \r
+ * collation.\r
+ */\r
+class CollatorSQLVarchar extends SQLVarchar implements CollationElementsInterface\r
+{\r
+ private WorkHorseForCollatorDatatypes holderForCollationSensitiveInfo;\r
+\r
+ /*\r
+ * constructors\r
+ */\r
+ \r
+ /**\r
+ * Create SQL VARCHAR value initially set to NULL that\r
+ * performs collation according to collatorForCharacterDatatypes \r
+ */\r
+ CollatorSQLVarchar(RuleBasedCollator collatorForCharacterDatatypes)\r
+ {\r
+ setCollator(collatorForCharacterDatatypes);\r
+ }\r
+ \r
+ /**\r
+ * Create SQL VARCHAR value initially set to value that\r
+ * performs collation according to collatorForCharacterDatatypes \r
+ */\r
+ CollatorSQLVarchar(String val, RuleBasedCollator collatorForCharacterDatatypes)\r
+ {\r
+ super(val);\r
+ setCollator(collatorForCharacterDatatypes);\r
+ }\r
+\r
+ /**\r
+ * Set the RuleBasedCollator for this instance of CollatorSQLVarchar. It will\r
+ * be used to do the collation.\r
+ */\r
+ private void setCollator(RuleBasedCollator collatorForCharacterDatatypes)\r
+ {\r
+ holderForCollationSensitiveInfo = \r
+ new WorkHorseForCollatorDatatypes(collatorForCharacterDatatypes, this);\r
+ }\r
+\r
+ /**\r
+ * Get the RuleBasedCollator for this instance of CollatorSQLVarchar. It \r
+ * will be used to do the collation.\r
+ * \r
+ * @return The Collator object which should be used for collation \r
+ * operation on this object\r
+ */\r
+ protected RuleBasedCollator getCollatorForCollation() throws StandardException\r
+ {\r
+ return holderForCollationSensitiveInfo.getCollatorForCollation();\r
+ }\r
+ \r
+ /** @see CollationElementsInterface#getCollationElementsForString */\r
+ public int[] getCollationElementsForString() throws StandardException \r
+ {\r
+ return holderForCollationSensitiveInfo.getCollationElementsForString();\r
+ }\r
+\r
+ /** @see CollationElementsInterface#getCountOfCollationElements */\r
+ public int getCountOfCollationElements()\r
+ {\r
+ return holderForCollationSensitiveInfo.getCountOfCollationElements();\r
+ }\r
+\r
+ /*\r
+ * DataValueDescriptor interface\r
+ */\r
+\r
+ /**\r
+ * @see DataValueDescriptor#getClone\r
+ */\r
+ public DataValueDescriptor getClone()\r
+ {\r
+ try\r
+ {\r
+ return new CollatorSQLVarchar(getString(), \r
+ holderForCollationSensitiveInfo.getCollatorForCollation());\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
+ public DataValueDescriptor getNewNull()\r
+ {\r
+ CollatorSQLVarchar result = new CollatorSQLVarchar(\r
+ holderForCollationSensitiveInfo.getCollatorForCollation());\r
+ return result;\r
+ }\r
+\r
+ protected StringDataValue getNewVarchar() throws StandardException\r
+ {\r
+ CollatorSQLVarchar result = new CollatorSQLVarchar(\r
+ holderForCollationSensitiveInfo.getCollatorForCollation());\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * We do not anticipate this method on collation sensitive DVD to be\r
+ * ever called in Derby 10.3 In future, when Derby will start supporting\r
+ * SQL standard COLLATE clause, this method might get called on the\r
+ * collation sensitive DVDs.\r
+ * \r
+ * @see StringDataValue#getValue(RuleBasedCollator) \r
+ */\r
+ public StringDataValue getValue(RuleBasedCollator collatorForComparison)\r
+ {\r
+ if (collatorForComparison != null)\r
+ {\r
+ //non-null collatorForComparison means use this collator sensitive\r
+ //implementation of SQLVarchar\r
+ setCollator(collatorForComparison);\r
+ return this; \r
+ } else {\r
+ //null collatorForComparison means use UCS_BASIC for collation.\r
+ //For that, we need to use the base class SQLVarchar\r
+ SQLVarchar s = new SQLVarchar();\r
+ s.copyState(this);\r
+ return s;\r
+ }\r
+ }\r
+ \r
+ /** @see SQLChar#stringCompare(SQLChar, SQLChar) */\r
+ protected int stringCompare(SQLChar char1, SQLChar char2)\r
+ throws StandardException\r
+ {\r
+ return holderForCollationSensitiveInfo.stringCompare(char1, char2);\r
+ }\r
+\r
+ /**\r
+ * This method implements the like function for char (with no escape value).\r
+ * The difference in this method and the same method in superclass is that\r
+ * here we use special Collator object to do the comparison rather than\r
+ * using the Collator object associated with the default jvm locale.\r
+ *\r
+ * @param pattern The pattern to use\r
+ *\r
+ * @return A SQL boolean value telling whether the first operand is\r
+ * like the second operand\r
+ *\r
+ * @exception StandardException Thrown on error\r
+ */\r
+ public BooleanDataValue like(DataValueDescriptor pattern)\r
+ throws StandardException\r
+ {\r
+ return(holderForCollationSensitiveInfo.like(pattern));\r
+ }\r
+ \r
+ /**\r
+ * This method implements the like function for char with an escape value.\r
+ * \r
+ * @param pattern The pattern to use\r
+ * \r
+ * @return A SQL boolean value telling whether the first operand is\r
+ * like the second operand\r
+ *\r
+ * @exception StandardException Thrown on error\r
+ */\r
+ public BooleanDataValue like(DataValueDescriptor pattern,\r
+ DataValueDescriptor escape) throws StandardException\r
+ {\r
+ return(holderForCollationSensitiveInfo.like(pattern, escape));\r
+ }\r
+}\r