You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Kathey Marsden <km...@sbcglobal.net> on 2006/07/15 00:13:27 UTC

should Clob.getSubstring() allow length of 0?

In this  recent thread the question came up.
http://www.nabble.com/The-tutorial-on-migrating-DB-to-derby-tf1787729.html#a4874605

Derby does not allow a length of 0 for Clob.getSubstring().

Is this a bug in Derby?  I am not sure from reading the javadoc:
http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Clob.html#getSubString(long,%20int)

Kathey

Re: should Clob.getSubstring() allow length of 0?

Posted by Paul J DeCoursey <pa...@decoursey.net>.
Big time NOT OK, it was highly insulting, maybe not directly to Kathey 
(but probably) but insulting to a general user like myself.

Kathey Marsden wrote:
> Lance, do you think your words describing the getSubString usage in 
> this thread :
> http://www.nabble.com/Re%3A-should-Clob.getSubstring%28%29-allow-length-of-0--p5336628.html 
>
>
> were
> [  ] OK
> [  ] Not OK
>
> ?
>
> Sorry for my lack of clarity  in describing my concerns.  I think it 
> stems from my unwillingness to quote or cut and paste the comment in 
> question.
>
> Kathey
>
>



My comment to Lance ( Was Re: should Clob.getSubstring() allow length of 0?)

Posted by Kathey Marsden <km...@sbcglobal.net>.
Now that the  Clob.getSubstring() thread is back on track,
I wanted to try to clarify my OK/Not OK post in Clob.getSubstring()  
thread  and apologize for any trouble it caused the community or 
Lance.   There I think were actually many points  of   misunderstanding. 
and poor judgment on my part.

1) I certainly was totally off base in thinking  Lance was talking about 
the user code.  It was something else entirely and I was clearly 
misinterpreted it .
2) Given that misperception,  I did not make clear the my reasoning for 
posting which was not out of personal insult but more out of general 
community concern.

   a) That developers are nervous about posting their code  or posting 
to the list.   Humorous jabs can make that worse and the problem is not 
that they feel insulted, they just feel hurt and embarrassed  and  feel  
this tremendous  need for time travel to go back and fix it.  Of course 
they can just post new code or a clarification to their  post  to fix it 
but still there is this tremendous apprehension.

   b)  The general tone on derby-user.   I actually have a filter set up 
for my derby-user mail, which is sad. I think sometimes the tone on 
derby-user is not the supportive environment I would like to see it be,  
so thought it might be good check in on what was OK/Not OK.   Even if 
this was what I thought it to be originally, I didn't think it was 
really bad, just a point for reflection.

3)   I did not give proper context or indicate the level of seriousness 
for the OK/ Not OK exercise.  I am the mother of a 9 year old and we 
have been engaging in  OK/Not OK interactions since my son was 3.   The 
flow goes something like this.
Parent -   Think about what you just said to Johnny.  Was that "OK"  or  
"Not OK"
Child -   "Not OK". "shouldn't have said "stupid"
Parent -  "Good.  Now go play and be nice to each other"

4) I have a tendency to bring my motherly tendencies onto the list which 
is sometimes "Not OK"   Will work on that.


Anyway,  I have made a thousand mistakes here on the list, including the 
4 above in just this one post.  My time travel machine has been broken 
for sometime so I send this repost to help  understand each other and 
try to improve and try to make the end result is as "OK" as it can be.

Thanks

Kathey


Re: should Clob.getSubstring() allow length of 0?

Posted by "Lance J. Andersen" <La...@Sun.COM>.

Kathey Marsden wrote:
> Craig L Russell wrote:
>
>>
>> Now that Kathey has kicked it back to you and me ...
>
> [snip conversation on addressing the issue]
>
> Thank you Craig for following up on this.  I always give myself a 
> point for every Derby bug I find and file even if it doesn't directly 
> affect me or my current support cases because I know it will help a 
> user down the road and help  make Derby better.  Even though this one 
> is no longer worth the point to me because of the language involved  I 
> am sure there is a user down the road that will benefit from your 
> efforts.
>
> I did want to clarify with Lance however whether he thinks the 
> language he used to describe the usage is appropriate for derby-user, 
> whether the usage itsself was correct or not. I am sure it was just 
> the result of a very hard day in the JDBC compliance world as I have 
> never seen anything like that from Lance before.  I have plenty of 
> posts in the permanant archives  resulting from such days that I wish 
> I could purge and often feel better  knowing at least my own personal 
> rejection of my comment is there for the record too.
>
As i said in the previous email,  my comments were not intended to 
offend anyone, but to add humor to something we had not specifically 
covered in the spec for a variety of reasons.

No worries i will not be responding on the alias going forward.

-lance
> Lance, do you think your words describing the getSubString usage in 
> this thread :
> http://www.nabble.com/Re%3A-should-Clob.getSubstring%28%29-allow-length-of-0--p5336628.html 
>
>
> were
> [  ] OK
> [  ] Not OK
>
> ?
>
> Sorry for my lack of clarity  in describing my concerns.  I think it 
> stems from my unwillingness to quote or cut and paste the comment in 
> question.
>
> Kathey
>

Re: should Clob.getSubstring() allow length of 0?

Posted by Kathey Marsden <km...@sbcglobal.net>.
Craig L Russell wrote:

>
> Now that Kathey has kicked it back to you and me ...

[snip conversation on addressing the issue]

Thank you Craig for following up on this.  I always give myself a point 
for every Derby bug I find and file even if it doesn't directly affect 
me or my current support cases because I know it will help a user down 
the road and help  make Derby better.  Even though this one is no longer 
worth the point to me because of the language involved  I am sure there 
is a user down the road that will benefit from your efforts.

I did want to clarify with Lance however whether he thinks the language 
he used to describe the usage is appropriate for derby-user, whether the 
usage itsself was correct or not. I am sure it was just the result of a 
very hard day in the JDBC compliance world as I have never seen anything 
like that from Lance before.  I have plenty of posts in the permanant 
archives  resulting from such days that I wish I could purge and often 
feel better  knowing at least my own personal rejection of my comment is 
there for the record too.

Lance, do you think your words describing the getSubString usage in this 
thread :
http://www.nabble.com/Re%3A-should-Clob.getSubstring%28%29-allow-length-of-0--p5336628.html

were
[  ] OK
[  ] Not OK

?

Sorry for my lack of clarity  in describing my concerns.  I think it 
stems from my unwillingness to quote or cut and paste the comment in 
question.

Kathey


Re: should Clob.getSubstring() allow length of 0?

Posted by "Lance J. Andersen" <La...@Sun.COM>.
Hi Craig,

Craig L Russell wrote:
> Hi Lance,
>
> On Jul 17, 2006, at 8:55 AM, Lance J. Andersen wrote:
>
>> The JDBC methods should, as much as the possibly can, be consistent 
>> with the behavior of methods in the JDK.
>>
> I agree with your point here. I think all of us are saying that there 
> is no inconsistency with zero length Strings in JDK. All of the 
> methods we've looked at are consistent with allowing the APIs to 
> handle zero length Strings as a simple boundary condition. The only 
> thing that's a bit hard to grok is the fact that JDBC generally uses 
> 1-origin indexing while JDK uses 0-origin. But everything in the API 
> allows zero-length Strings.
This issue has nothing to do do with zero-length Strings, but the valid 
parameter values for getSubString().  The javadocs currently make no 
claim on *any* values,  so it is open to interpretation.

As i said, it really boils down to what the EG will agree to document at 
this point in time as the correct behavior and until i conclude that 
discussion, there really is nothing more i can provide for input.

Regards
Lance
>
>> This specific issue could easily be argued either way and I see it 
>> from a different point of view
>
> I haven't seen anything in your examples (once corrected) that give 
> you a different point of view. Can you please re-read the thread and 
> tell us where there is a problem?
>
> Craig
>
> Craig Russell
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>

Re: should Clob.getSubstring() allow length of 0?

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Lance,

On Jul 17, 2006, at 8:55 AM, Lance J. Andersen wrote:

> The JDBC methods should, as much as the possibly can, be consistent  
> with the behavior of methods in the JDK.
>
I agree with your point here. I think all of us are saying that there  
is no inconsistency with zero length Strings in JDK. All of the  
methods we've looked at are consistent with allowing the APIs to  
handle zero length Strings as a simple boundary condition. The only  
thing that's a bit hard to grok is the fact that JDBC generally uses  
1-origin indexing while JDK uses 0-origin. But everything in the API  
allows zero-length Strings.

> This specific issue could easily be argued either way and I see it  
> from a different point of view

I haven't seen anything in your examples (once corrected) that give  
you a different point of view. Can you please re-read the thread and  
tell us where there is a problem?

Craig

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: should Clob.getSubstring() allow length of 0?

Posted by "Lance J. Andersen" <La...@Sun.COM>.
I understand what you and Andreas are saying, but even if i look at 
copyValueOf, the docs are not clear at the expected behavior.

Unfortunately, what ends up happening is for items that are not 
explicit, then they often take a life of their own and imitation is the 
biggest form of flattery.

The JDBC methods should, as much as the possibly can, be consistent with 
the behavior of methods in the JDK.  This specific issue could easily be 
argued either way and I see it from a different point of view and 
perhaps it is also all my years of FTN/C programming as well.

At the end of the day, it really does not matter what my own opinion is, 
it is coming to a consensus within the JDBC EG on the correct behavior.  
Unfortunately, there are cases where i have had to document the behavior 
as implementation defined given it can be too late to have the vendors 
all come to the same agreement and be willing to change their 
implementations.  This issue is more of an overall corner case, so i am 
not going to lose to much sleep over it regardless of how it turns out 
in the EG.


Regards
Lance
>
> 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

Re: should Clob.getSubstring() allow length of 0?

Posted by Craig L Russell <Cr...@Sun.COM>.
I've filed DERBY-1516 for this discussion.

I agree with Thomas, and would just point out that it appears that  
it's only the embedded Derby driver that has the issue. The code in  
the Derby access manager Blob and Clob are implemented correctly.

Craig

On Jul 17, 2006, at 8:36 AM, Thomas Dudziak wrote:

> On 7/17/06, Lance J. Andersen <La...@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

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: should Clob.getSubstring() allow length of 0?

Posted by Thomas Dudziak <to...@gmail.com>.
On 7/17/06, Lance J. Andersen <La...@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

Re: should Clob.getSubstring() allow length of 0?

Posted by "Lance J. Andersen" <La...@Sun.COM>.

Andreas Korneliussen wrote:
> Lance J. Andersen wrote:
>> Here is the description of String.subString()
>>
>>
>>       substring
>>
>> public String 
>> <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html> 
>> *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
>>         
>> <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/IndexOutOfBoundsException.html>| 
>>
>>         - 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.
>>
>
> I do not agree that a SQLException is expected.
> Clob.getSubString(..) is 1 based, String.substring(..) is 0 based.
> So Clob.getSubString(1, 0) maps down to String.substring(0, 0), which 
> returns the empty string. (And yes i do not like the fact that the 
> indexing is different then the JDK either for these methods...)
Yes but forget the getSubString(1,0) scenario  getSubString(4,3) would 
also apply to this case as well.

The issue as i said in my previous response to Thomas, is what does the 
EG want to document at this time given the behavior has been undefined 
since JDBC 2.0 and there are multiple JDBC drivers for some of the 
backends that Thomas has listed and without explicitly testing all of 
them, i certainly cannot speak to whether they all behave consistently.  
If they do, great it is easier to address.  If they do not, then it 
boils down to whether they can agree on a behavior or it gets documented 
as an implementation dependent issue.
>
> "".substring(0,0) returns the empty string ("").
>
> Andreas
>
>
>> Regards
>> Lance
>>
>>
>> Craig L Russell wrote:
>>
>>> Hi Lance,
>>>
>>> Now that Kathey has kicked it back to you and me, how about updating 
>>> the to-be-released JDBC 4 to point out that a length of zero for the 
>>> getSubString is valid?
>>>
>>> String getSubString(long pos,
>>>                     int length)
>>>                     throws SQLException
>>> Retrieves a copy of the specified substring in the CLOB value 
>>> designated by this Clob object. The substring begins at position pos 
>>> and has up to length consecutive characters.
>>>
>>> Parameters:
>>> pos - the first character of the substring to be extracted. The 
>>> first character is at position 1.
>>> length - the number of consecutive characters to be copied
>>>
>>> We might simply add to this description.
>>>
>>> length - the number of consecutive characters to be copied; 0 is a 
>>> valid value
>>>
>>> We have a similar case with Blob byte[ ] getBytes().
>>>
>>> Can you run this past the expert group for a quick consensus?
>>>
>>> Thanks,
>>>
>>> Craig
>>>
>>> On Jul 14, 2006, at 11:13 PM, Kathey Marsden wrote:
>>>
>>>> Craig L Russell wrote:
>>>>
>>>>> You can always work around odd code on the other side of an  
>>>>> incompletely defined interface. But you probably have code on the  
>>>>> Derby side that also checks for the clob.length() == 0 and take 
>>>>> some  extraordinary action.
>>>>>
>>>>> So much easier for neither side to check for zero length and let 
>>>>> the  natural boundary condition happen.
>>>>>
>>>> I am happy that you agree that the original  DDLUtils code was 
>>>> perfectly reasonable at not as Lance described it.   I will exit 
>>>> this issue now and allow you,  Lance, or someone else with interest 
>>>> determine the correct behavior, check it with Derby  and  file a 
>>>> Derby bug if needed.
>>>>
>>>> Kathey
>>>>
>>>
>>> Craig Russell
>>> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
>>> 408 276-5638 mailto:Craig.Russell@sun.com
>>> P.S. A good JDO? O, Gasp!
>>>
>

Re: should Clob.getSubstring() allow length of 0?

Posted by Andreas Korneliussen <An...@Sun.COM>.
Lance J. Andersen wrote:
> Here is the description of String.subString()
> 
> 
>       substring
> 
> public String <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html> *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
>         <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/IndexOutOfBoundsException.html>|
>         - 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.
> 

I do not agree that a SQLException is expected.
Clob.getSubString(..) is 1 based, String.substring(..) is 0 based.
So Clob.getSubString(1, 0) maps down to String.substring(0, 0), which 
returns the empty string.

"".substring(0,0) returns the empty string ("").

Andreas


> Regards
> Lance
> 
> 
> Craig L Russell wrote:
> 
>> Hi Lance,
>>
>> Now that Kathey has kicked it back to you and me, how about updating 
>> the to-be-released JDBC 4 to point out that a length of zero for the 
>> getSubString is valid?
>>
>> String getSubString(long pos,
>>                     int length)
>>                     throws SQLException
>> Retrieves a copy of the specified substring in the CLOB value 
>> designated by this Clob object. The substring begins at position pos 
>> and has up to length consecutive characters.
>>
>> Parameters:
>> pos - the first character of the substring to be extracted. The first 
>> character is at position 1.
>> length - the number of consecutive characters to be copied
>>
>> We might simply add to this description.
>>
>> length - the number of consecutive characters to be copied; 0 is a 
>> valid value
>>
>> We have a similar case with Blob byte[ ] getBytes().
>>
>> Can you run this past the expert group for a quick consensus?
>>
>> Thanks,
>>
>> Craig
>>
>> On Jul 14, 2006, at 11:13 PM, Kathey Marsden wrote:
>>
>>> Craig L Russell wrote:
>>>
>>>> You can always work around odd code on the other side of an  
>>>> incompletely defined interface. But you probably have code on the  
>>>> Derby side that also checks for the clob.length() == 0 and take 
>>>> some  extraordinary action.
>>>>
>>>> So much easier for neither side to check for zero length and let 
>>>> the  natural boundary condition happen.
>>>>
>>> I am happy that you agree that the original  DDLUtils code was 
>>> perfectly reasonable at not as Lance described it.   I will exit this 
>>> issue now and allow you,  Lance, or someone else with interest 
>>> determine the correct behavior, check it with Derby  and  file a 
>>> Derby bug if needed.
>>>
>>> Kathey
>>>
>>
>> Craig Russell
>> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
>> 408 276-5638 mailto:Craig.Russell@sun.com
>> P.S. A good JDO? O, Gasp!
>>


Re: should Clob.getSubstring() allow length of 0?

Posted by "Lance J. Andersen" <La...@Sun.COM>.
Here is the description of String.subString()


      substring

public String <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html> *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
        <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/IndexOutOfBoundsException.html>|
        - 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.

Regards
Lance


Craig L Russell wrote:
> Hi Lance,
>
> Now that Kathey has kicked it back to you and me, how about updating 
> the to-be-released JDBC 4 to point out that a length of zero for the 
> getSubString is valid?
>
> String getSubString(long pos,
>                     int length)
>                     throws SQLException
> Retrieves a copy of the specified substring in the CLOB value 
> designated by this Clob object. The substring begins at position pos 
> and has up to length consecutive characters.
>
> Parameters:
> pos - the first character of the substring to be extracted. The first 
> character is at position 1.
> length - the number of consecutive characters to be copied
>
> We might simply add to this description.
>
> length - the number of consecutive characters to be copied; 0 is a 
> valid value
>
> We have a similar case with Blob byte[ ] getBytes().
>
> Can you run this past the expert group for a quick consensus?
>
> Thanks,
>
> Craig
>
> On Jul 14, 2006, at 11:13 PM, Kathey Marsden wrote:
>
>> Craig L Russell wrote:
>>
>>> You can always work around odd code on the other side of an  
>>> incompletely defined interface. But you probably have code on the  
>>> Derby side that also checks for the clob.length() == 0 and take 
>>> some  extraordinary action.
>>>
>>> So much easier for neither side to check for zero length and let 
>>> the  natural boundary condition happen.
>>>
>> I am happy that you agree that the original  DDLUtils code was 
>> perfectly reasonable at not as Lance described it.   I will exit this 
>> issue now and allow you,  Lance, or someone else with interest 
>> determine the correct behavior, check it with Derby  and  file a 
>> Derby bug if needed.
>>
>> Kathey
>>
>
> Craig Russell
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>

Re: should Clob.getSubstring() allow length of 0?

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Lance,

Now that Kathey has kicked it back to you and me, how about updating  
the to-be-released JDBC 4 to point out that a length of zero for the  
getSubString is valid?

String getSubString(long pos,
                     int length)
                     throws SQLException
Retrieves a copy of the specified substring in the CLOB value  
designated by this Clob object. The substring begins at position pos  
and has up to length consecutive characters.

Parameters:
pos - the first character of the substring to be extracted. The first  
character is at position 1.
length - the number of consecutive characters to be copied

We might simply add to this description.

length - the number of consecutive characters to be copied; 0 is a  
valid value

We have a similar case with Blob byte[ ] getBytes().

Can you run this past the expert group for a quick consensus?

Thanks,

Craig

On Jul 14, 2006, at 11:13 PM, Kathey Marsden wrote:

> Craig L Russell wrote:
>
>> You can always work around odd code on the other side of an   
>> incompletely defined interface. But you probably have code on the   
>> Derby side that also checks for the clob.length() == 0 and take  
>> some  extraordinary action.
>>
>> So much easier for neither side to check for zero length and let  
>> the  natural boundary condition happen.
>>
> I am happy that you agree that the original  DDLUtils code was  
> perfectly reasonable at not as Lance described it.   I will exit  
> this issue now and allow you,  Lance, or someone else with interest  
> determine the correct behavior, check it with Derby  and  file a  
> Derby bug if needed.
>
> Kathey
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: should Clob.getSubstring() allow length of 0?

Posted by Kathey Marsden <km...@sbcglobal.net>.
Craig L Russell wrote:

> You can always work around odd code on the other side of an  
> incompletely defined interface. But you probably have code on the  
> Derby side that also checks for the clob.length() == 0 and take some  
> extraordinary action.
>
> So much easier for neither side to check for zero length and let the  
> natural boundary condition happen.
>
I am happy that you agree that the original  DDLUtils code was perfectly 
reasonable at not as Lance described it.   I will exit this issue now 
and allow you,  Lance, or someone else with interest determine the 
correct behavior, check it with Derby  and  file a Derby bug if needed.

Kathey


Re: should Clob.getSubstring() allow length of 0?

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Kathy,

On Jul 14, 2006, at 6:46 PM, Kathey Marsden wrote:

> Lance J. Andersen wrote:
>
>> Hi Kathy,
>>
>> I would probably expect a  SQLException to be thrown in this case  
>> or I guess you could return nothing.
>>
> I will assume then that the Derby behavior  is correct and won't  
> file a bug.
> Thank you for the clarification but I don't think your comment was  
> either necessary or appropriate.
>
> In the DDLUtils code, they just had the generic code:  value =  
> clob.getSubString(1,  (int)clob.length());  to handle all non-null  
> Clobs.
> The fix was to add in an extra branch to handle zero length Clobs  
> which they were more than happy to do although it made their code  
> look a little messier.

You can always work around odd code on the other side of an  
incompletely defined interface. But you probably have code on the  
Derby side that also checks for the clob.length() == 0 and take some  
extraordinary action.

So much easier for neither side to check for zero length and let the  
natural boundary condition happen.

Craig
>
> This was the change to work around the SQLException.
>
> +                            else if (clob.length() == 0)
> +                               {
> +                                       value = "";
> +                               }
>                        else
>                        {
>                            value = clob.getSubString(1,  (int) 
> clob.length());
>
> Kathey

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: should Clob.getSubstring() allow length of 0?

Posted by Kathey Marsden <km...@sbcglobal.net>.
Lance J. Andersen wrote:

> Hi Kathy,
>
> I would probably expect a  SQLException to be thrown in this case or I 
> guess you could return nothing.
>
I will assume then that the Derby behavior  is correct and won't file a bug.
Thank you for the clarification but I don't think your comment was 
either necessary or appropriate.

 In the DDLUtils code, they just had the generic code:  value = 
clob.getSubString(1,  (int)clob.length());  to handle all non-null Clobs.
The fix was to add in an extra branch to handle zero length Clobs which 
they were more than happy to do although it made their code look a 
little messier.

This was the change to work around the SQLException.

+                            else if (clob.length() == 0)
+                               {
+                                       value = "";
+                               }
                        else
                        {
                            value = clob.getSubString(1,  
(int)clob.length());

Kathey

Re: should Clob.getSubstring() allow length of 0?

Posted by Kathey Marsden <km...@sbcglobal.net>.
Lance J. Andersen wrote:

>
> First, it seems someone took my subtle attempt at humor the wrong 
> way.  My apologies as I was not trying to insult or offend anyone.

Thank you Lance for clarifying your intent.    I clearly misinterpreted 
your  post and  my request for clarification should have been less 
abrasive and more open-minded so was clearly Not OK.

> I think i will sign off derby-dev for a while...
>
Please do not sign off of derby-dev or derby-user.  You are a critically 
important member of the community.   I think we all want an environment 
where feedback on code or on posts is positive, where developers submit 
their code or ideas eager for thorough review and  are thrilled to get 
feedback for improvement.    Hopefully we can work through 
misunderstandings like this that occur along the way.  Clearly the 
answer is *not* to sign off.

Kathey



Re: should Clob.getSubstring() allow length of 0?

Posted by "Lance J. Andersen" <La...@Sun.COM>.
Hi Craig, Kathey,

First, it seems someone took my subtle attempt at humor the wrong way.  
My apologies as I was not trying to insult or offend anyone (but it was 
9:40pm EDT and I had been working since 6am to meet some deadlines).

My point was, that I cannot guarantee that a vendor is not throwing a 
SQLException in the case where the length is 0,  someone might have 
chosen to do this, i do not know.  The spec does not articulate a 
specific behavior one way or the other so from a spec perspective, they 
are within their rights to do so as the API does not define the 
behavior.  This was what i was referring to in my attempt at late night 
humor.  I have been surprised many times over as to how some items have 
been implemented by vendors given the lack of clarity.

I think i will sign off derby-dev for a while...

-lance

Craig L Russell wrote:
> Hi Lance,
>
> Unless I'm missing something, a zero length java.lang.String is legal 
> and necessary for completeness. Since there's nothing in the quoted 
> text, I'd assume that a zero-length String should be returned as long 
> as the start position is legal. Not an exception, not null, not 
> "nothing", but a valid zero-length String.
>
> Craig
>
> On Jul 14, 2006, at 6:32 PM, Lance J. Andersen wrote:
>
>> Hi Kathy,
>>
>> I would probably expect a  SQLException to be thrown in this case or 
>> I guess you could return nothing.
>>
>> We did not discuss this stupid human trick in the EG :-)  but i guess 
>> we should have.
>>
>> Regards
>> Lance
>>
>> Kathey Marsden wrote:
>>> Lance J. Andersen wrote:
>>>
>>>> Derby is correct.  The clarified JDBC 4 javadocs indicates the 
>>>> following:
>>>
>>>
>>>>         |pos| - the first character of the substring to be extracted.
>>>>         The first character is at position 1.
>>>>         |length| - the number of consecutive characters to be copied
>>> Thanks Lance for looking at this.  The question is about the 
>>> length.  The length in this case is 0.
>>> From the DDLUtils code it looked like this where clob.length() was 0.
>>>
>>> value = clob.getSubString(1, (int)clob.length());
>>>
>>> Is this  ok to send length 0 or to specify position 1 if there is no 
>>> character there?
>>>
>>> *
>>> *
>
> Craig Russell
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>

Re: should Clob.getSubstring() allow length of 0?

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Lance,

Unless I'm missing something, a zero length java.lang.String is legal  
and necessary for completeness. Since there's nothing in the quoted  
text, I'd assume that a zero-length String should be returned as long  
as the start position is legal. Not an exception, not null, not  
"nothing", but a valid zero-length String.

Craig

On Jul 14, 2006, at 6:32 PM, Lance J. Andersen wrote:

> Hi Kathy,
>
> I would probably expect a  SQLException to be thrown in this case  
> or I guess you could return nothing.
>
> We did not discuss this stupid human trick in the EG :-)  but i  
> guess we should have.
>
> Regards
> Lance
>
> Kathey Marsden wrote:
>> Lance J. Andersen wrote:
>>
>>> Derby is correct.  The clarified JDBC 4 javadocs indicates the  
>>> following:
>>
>>
>>>         |pos| - the first character of the substring to be  
>>> extracted.
>>>         The first character is at position 1.
>>>         |length| - the number of consecutive characters to be copied
>> Thanks Lance for looking at this.  The question is about the  
>> length.  The length in this case is 0.
>> From the DDLUtils code it looked like this where clob.length() was 0.
>>
>> value = clob.getSubString(1, (int)clob.length());
>>
>> Is this  ok to send length 0 or to specify position 1 if there is  
>> no character there?
>>
>> *
>> *

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: should Clob.getSubstring() allow length of 0?

Posted by "Lance J. Andersen" <La...@Sun.COM>.
Hi Kathy,

I would probably expect a  SQLException to be thrown in this case or I 
guess you could return nothing.

We did not discuss this stupid human trick in the EG :-)  but i guess we 
should have.

Regards
Lance

Kathey Marsden wrote:
> Lance J. Andersen wrote:
>
>> Derby is correct.  The clarified JDBC 4 javadocs indicates the 
>> following:
>
>
>>         |pos| - the first character of the substring to be extracted.
>>         The first character is at position 1.
>>         |length| - the number of consecutive characters to be copied
> Thanks Lance for looking at this.  The question is about the length.  
> The length in this case is 0.
> From the DDLUtils code it looked like this where clob.length() was 0.
>
> value = clob.getSubString(1, (int)clob.length());
>
> Is this  ok to send length 0 or to specify position 1 if there is no 
> character there?
>
> *
> *

Re: should Clob.getSubstring() allow length of 0?

Posted by Kathey Marsden <km...@sbcglobal.net>.
Lance J. Andersen wrote:

> Derby is correct.  The clarified JDBC 4 javadocs indicates the following:


>         |pos| - the first character of the substring to be extracted.
>         The first character is at position 1.
>         |length| - the number of consecutive characters to be copied 
>
Thanks Lance for looking at this.  The question is about the length.  
The length in this case is 0.
 From the DDLUtils code it looked like this where clob.length() was 0.

value = clob.getSubString(1, (int)clob.length());

Is this  ok to send length 0 or to specify position 1 if there is no 
character there?

*
*

Re: should Clob.getSubstring() allow length of 0?

Posted by "Lance J. Andersen" <La...@Sun.COM>.
Derby is correct.  The clarified JDBC 4 javadocs indicates the following:


      getSubString

java.lang.String *getSubString*(long pos,
                              int length)
                              throws SQLException <http://jse.east.sun.com/%7Elancea/javadoc-spec/java/sql/SQLException.html>

    Retrieves a copy of the specified substring in the |CLOB| value
    designated by this |Clob| object. The substring begins at position
    |pos| and has up to |length| consecutive characters.

    *Parameters:*
        |pos| - the first character of the substring to be extracted.
        The first character is at position 1.
        |length| - the number of consecutive characters to be copied 
    *Returns:*
        a |String| that is the specified substring in the |CLOB| value
        designated by this |Clob| object 
    *Throws:*
        |SQLException
        <http://jse.east.sun.com/%7Elancea/javadoc-spec/java/sql/SQLException.html>|
        - if there is an error accessing the |CLOB| value or if pos is
        less than 1 
        |SQLFeatureNotSupportedException
        <http://jse.east.sun.com/%7Elancea/javadoc-spec/java/sql/SQLFeatureNotSupportedException.html>|
        - if the JDBC driver does not support this method
    *Since:*
        1.2



Hopefully you will find many mysteries solved in JDBC 4 (and yes i am 
sure there are still some, but we knocked a ton of the off)

Kathey Marsden wrote:
> In this  recent thread the question came up.
> http://www.nabble.com/The-tutorial-on-migrating-DB-to-derby-tf1787729.html#a4874605 
>
>
> Derby does not allow a length of 0 for Clob.getSubstring().
>
> Is this a bug in Derby?  I am not sure from reading the javadoc:
> http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Clob.html#getSubString(long,%20int) 
>
>
> Kathey