db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas Dudziak" <tom...@gmail.com>
Subject Re: should Clob.getSubstring() allow length of 0?
Date Mon, 17 Jul 2006 15:36:50 GMT
On 7/17/06, Lance J. Andersen <Lance.Andersen@sun.com> wrote:

>  Here is the description of String.subString()
>
>
> substring public String substring(int beginIndex,
>  int endIndex)
>  Returns a new string that is a substring of this string. The substring
> begins at the specified beginIndex and extends to the character at index
> endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
>
>  Examples:
>  "hamburger".substring(4, 8) returns "urge"
>  "smiles".substring(1, 5) returns "mile"
>
>  Parameters: beginIndex - the beginning index, inclusive. endIndex - the
> ending index, exclusive. Returns: the specified substring. Throws:
> IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is
> larger than the length of this String object, or beginIndex is larger than
> endIndex.
>
>  So this indicates to me that a SQLException is what is to be expected in
> this case in the case of Kathey's example.

Actually that example is quite misleading because the second parameter
does not specify a number of characters but rather a second index,
which obviously can only be 0 if the first index is 0 (which btw.
works without problems and returns an empty string). This is different
from Clob.getSubString where the second parameter is the number of
characters.
A probably more fitting example would be something like:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#copyValueOf(char[],%20int,%20int)

E.g. a program like this:

public class Main
{
	public static void main(String[] args)
	{
		char[] test     = {  'S', 'o', 'm', 'e', ' ', 'T', 'e', 's', 't', '
', 'S', 't', 'r', 'i', 'n', 'g' };
		String testStr1 = String.copyValueOf(test, 5, 4);
		String testStr2 = String.copyValueOf(test, 5, 0);

		System.out.println("Test strings: #1=" + testStr1 + ", #2=" + testStr2);
	}
}

works without problems, even if the number of characters to copy is 0.

Coming back to the Clob.getSubString() method, from my experience,
Derby is the only database that does not allow 0 as the number of
characters to return. The JDBC drivers of all other databases that I
have tried - Oracle, DB2, Sql Server, MySql, PostgreSql, and some
others - simply return empty strings. This is why I implemented the
part of DdlUtils that way, because the API did not say anything about
0 being a value that is not allowed to use (and as Craig pointed out,
it makes sense to allow it for consistency).

cheers,
Tom

Mime
View raw message