You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ma...@apache.org on 2007/03/11 08:34:26 UTC

svn commit: r516864 - in /db/derby/code/trunk/java/engine/org/apache/derby/iapi: services/io/RegisteredFormatIds.java services/io/StoredFormatIds.java types/CollatorSQLChar.java types/DTSClassInfo.java

Author: mamta
Date: Sat Mar 10 23:34:25 2007
New Revision: 516864

URL: http://svn.apache.org/viewvc?view=rev&rev=516864
Log:
Patch for DERBY-2416 : Provide a shell for a subclass of SQLChar which will use the passed Collator to do the collation rather than 
SQLChar's default collation of UCS_BASIC

This patch introduces a new class called CollatorSQLChar which extends SQLChar. The new class has a RuleBasedCollator associated with it
and that Collator object determines the collation. A new format id is added for this class and code regarding format id has gone into 
RegisteredFormatIds, StoredFormatIds and DTSClassInfo. 

The new class is bare minimum at this point and new collation related methods will be added to this class in later patches. This new class
does not get used in Derby at this point. The query compiler code will need to be changed so that this class can get some action. This will 
happen in upcoming patches.

Dan had some suggestion on creating collation elements as required rather than doing it all upfront. Dag also had suggestion on alternative
implementation for array handling involved with collation element creation. We agreed on tackling these issues once the basic functionality 
is in for language based ordering.


Added:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/CollatorSQLChar.java   (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/RegisteredFormatIds.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/StoredFormatIds.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DTSClassInfo.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/RegisteredFormatIds.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/RegisteredFormatIds.java?view=diff&rev=516864&r1=516863&r2=516864
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/RegisteredFormatIds.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/RegisteredFormatIds.java Sat Mar 10 23:34:25 2007
@@ -526,6 +526,9 @@
         /* 462 */   "org.apache.derby.impl.sql.catalog.CoreDDFinderClassInfo",
         /* 463 */   "org.apache.derby.impl.sql.catalog.CoreDDFinderClassInfo",
         /* 464 */   "org.apache.derby.iapi.types.SqlXmlUtil",        
-	/* 465 */   "org.apache.derby.impl.store.raw.data.CompressSpacePageOperation",
+		/* 465 */   "org.apache.derby.impl.store.raw.data.CompressSpacePageOperation",
+		
+	    /// --- For SQLCharWithNonDefaultCollation
+		/* 466 */   "org.apache.derby.iapi.types.DTSClassInfo", //InstanceGetter
 };
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/StoredFormatIds.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/StoredFormatIds.java?view=diff&rev=516864&r1=516863&r2=516864
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/StoredFormatIds.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/StoredFormatIds.java Sat Mar 10 23:34:25 2007
@@ -1474,6 +1474,9 @@
 
         public static final int SQL_INTEGER_ID = 
                 (MIN_ID_2 + 80);
+        
+        static public final int SQL_CHAR_WITH_NON_DEFAULT_COLLATION_ID =
+                (MIN_ID_2 + 466);
 
         public static final int SQL_REAL_ID = 
                 (MIN_ID_2 + 81);
@@ -1863,7 +1866,7 @@
          * Make sure this is updated when a new module is added
          */
         public static final int MAX_ID_2 =
-                (MIN_ID_2 + 465);
+                (MIN_ID_2 + 466);
 
         // DO NOT USE 4 BYTE IDS ANYMORE
         static public final int MAX_ID_4 =

Added: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/CollatorSQLChar.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/CollatorSQLChar.java?view=auto&rev=516864
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/CollatorSQLChar.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/CollatorSQLChar.java Sat Mar 10 23:34:25 2007
@@ -0,0 +1,234 @@
+/*
+
+   Derby - Class org.apache.derby.iapi.types.CollatorSQLChar
+ 
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to you under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+
+package org.apache.derby.iapi.types;
+
+import org.apache.derby.iapi.types.DataValueDescriptor;
+import org.apache.derby.iapi.types.BooleanDataValue;
+
+import org.apache.derby.iapi.services.io.StoredFormatIds;
+
+import org.apache.derby.iapi.error.StandardException;
+import org.apache.derby.iapi.services.sanity.SanityManager;
+
+import java.text.CollationElementIterator;
+import java.text.RuleBasedCollator;
+
+/**
+ * CollatorSQLChar satisfies the DataValueDescriptor
+ * interfaces (i.e., OrderableDataType). It implements an String holder,
+ * e.g. for storing a column value; it can be specified
+ * when constructed to not allow nulls. Nullability cannot be changed
+ * after construction.
+ * <p>
+ * Because OrderableDataType is a subclass of DataType,
+ * CollatorSQLChar can play a role in either a DataType/ValueRow
+ * or a OrderableDataType/KeyRow, interchangeably.
+ * 
+ * This class differs from SQLChar based on how the 2 classes use different
+ * collations to collate their data. SQLChar uses Derby's default collation
+ * which is UCS_BASIC. Whereas, this class uses the RuleBasedCollator object 
+ * that was passed to it in it's constructor and that RuleBasedCollator object  
+ * decides the collation.
+ * 
+ * In Derby 10.3, this class will be passed a RuleBasedCollator which is based 
+ * on the database's territory. In future releases of Derby, this class can be 
+ * used to do other kinds of collation like case-insensitive collation etc by  
+ * just passing an appropriate RuleBasedCollator object for that kind of 
+ * collation.
+ */
+public class CollatorSQLChar
+	extends SQLChar
+{
+	//Use this object for collation
+	RuleBasedCollator rbc;
+	//Following is the array holding a series of collation elements for the 
+	//string. It will be used in the like method 
+	private int[]	collationElementsForString;
+	//number of valid collation elements in the array above. 
+	private int		countOfCollationElements;
+
+	/*
+	 * constructors
+	 */
+
+	/**
+		no-arg constructor, required by Formattable.
+	*/
+	public CollatorSQLChar()
+	{
+	}
+
+	public CollatorSQLChar(String val, RuleBasedCollator rbc)
+	{
+		super(val);
+		this.rbc = rbc;
+	}
+
+	/**
+	 * Set the RuleBasedCollator for this instance of CollatorSQLChar. It will
+	 * be used to do the collation.
+	 * 
+	 * @return an array of collation elements for the string
+	 * @throws StandardException
+	 */
+	private void setCollator(RuleBasedCollator rbc)
+	{
+		this.rbc = rbc;
+	}
+
+	/**
+	 * This method translates the string into a series of collation elements.
+	 * These elements will get used in the like method.
+	 * 
+	 * @return an array of collation elements for the string
+	 * @throws StandardException
+	 */
+	private int[] getCollationElementsForString()
+		throws StandardException
+	{
+		if (isNull())
+		{
+			return (int[]) null;
+		}
+
+		if (collationElementsForString != null)
+		{
+			return collationElementsForString;
+		}
+
+		// countOfCollationElements should always be 0 when collationElementsForString is null
+		if (SanityManager.DEBUG)
+		{
+			if (countOfCollationElements != 0)
+			{
+				SanityManager.THROWASSERT(
+					"countOfCollationElements expected to be 0, not " + countOfCollationElements);
+			}
+		}
+
+		collationElementsForString = new int[getLength()];
+
+		CollationElementIterator cei = rbc.getCollationElementIterator(getString());
+		int nextInt;
+		while ((nextInt = cei.next()) != CollationElementIterator.NULLORDER)
+		{
+			/* Believe it or not, a String might have more
+			 * collation elements than characters.
+			 * So, we handle that case by increasing the int array
+			 * by 5 and copying array elements.
+			 */
+			if (countOfCollationElements == collationElementsForString.length)
+			{
+				int[] expandedArray = new int[countOfCollationElements + 5];
+				System.arraycopy(collationElementsForString, 0, expandedArray, 
+						0, collationElementsForString.length);
+				collationElementsForString = expandedArray;
+			}
+			collationElementsForString[countOfCollationElements++] = nextInt;
+		}
+
+		return collationElementsForString;
+	}
+
+
+	/**
+	 * This method returns the count of collation elements for this instance of
+	 * CollatorSQLChar. This method will return the correct value only if  
+	 * method getCollationElementsForString has been called previously on this 
+	 * instance of CollatorSQLChar. 
+	 *
+	 * @return count of collation elements for this instance of CollatorSQLChar
+	 */
+	private int getCountOfCollationElements()
+	{
+		return countOfCollationElements;
+	}
+
+	/**
+	 * This method implements the like function for char (with no escape value).
+	 *
+	 * @param pattern		The pattern to use
+	 *
+	 * @return	A SQL boolean value telling whether the first operand is
+	 *			like the second operand
+	 *
+	 * @exception StandardException		Thrown on error
+	 */
+	public BooleanDataValue like(DataValueDescriptor pattern)
+								throws StandardException
+	{
+		Boolean likeResult;
+
+		CollatorSQLChar patternSQLChar = (CollatorSQLChar) pattern;
+		likeResult = Like.like(getCollationElementsForString(),
+				getCountOfCollationElements(),
+				patternSQLChar.getCollationElementsForString(),
+				patternSQLChar.getCountOfCollationElements(),
+				rbc);
+
+		return SQLBoolean.truthValue(this,
+									 pattern,
+									 likeResult);
+	}
+
+	/*
+	 * DataValueDescriptor interface
+	 */
+
+	/**
+	 * @see DataValueDescriptor#getClone
+	 */
+	public DataValueDescriptor getClone()
+	{
+		try
+		{
+			return new CollatorSQLChar(getString(), rbc);
+		}
+		catch (StandardException se)
+		{
+			if (SanityManager.DEBUG)
+				SanityManager.THROWASSERT("Unexpected exception " + se);
+			return null;
+		}
+	}
+
+	/**
+	 * @see DataValueDescriptor#getNewNull
+	 */
+	public DataValueDescriptor getNewNull()
+	{
+		CollatorSQLChar result = new CollatorSQLChar();
+		result.setCollator(rbc);
+		return result;
+	}
+
+	/*
+	 * Storable interface
+	 */
+
+	/**
+	 * @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
+	 */ 
+	public int getTypeFormatId() {
+		return StoredFormatIds.SQL_CHAR_WITH_NON_DEFAULT_COLLATION_ID;
+	}
+}

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/CollatorSQLChar.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DTSClassInfo.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DTSClassInfo.java?view=diff&rev=516864&r1=516863&r2=516864
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DTSClassInfo.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DTSClassInfo.java Sat Mar 10 23:34:25 2007
@@ -59,6 +59,8 @@
                 case StoredFormatIds.SQL_CLOB_ID: return new SQLClob();
                 case StoredFormatIds.SQL_NCLOB_ID: return new SQLNClob();
                 case StoredFormatIds.XML_ID: return new XML();
+                case StoredFormatIds.SQL_CHAR_WITH_NON_DEFAULT_COLLATION_ID: 
+                	return new SQLCharWithNonDefaultCollation();
 
                 /* Type ids */
                 case StoredFormatIds.BIT_TYPE_ID: