You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-dev@db.apache.org by Dy...@Sun.COM on 2006/03/07 13:09:02 UTC

A question about types

While working on DERBY-925 and DERBY-969, I need to get information
about the types of the procedure/function parameters. Specifically I
need to fill the COLUMN_DEF (Default value for the parameter, if any)
and CHAR_OCTET_LENGTH (Byte length of char/binary types, NULL for
other types) columns.

So given type T, how do I

1) Find out if T is character or binary?
2) What the maximum length of T is, if char/binary?
3) If T has a default value?
4) If T's default value is NULL?
5) If T's value cannot be represented without truncation?

>From what I can see, Derby has a number of classes/interfaces that
provides information about types:

org.apache.derby.catalog.TypeDescriptor (interface)
org.apache.derby.catalog.TypeDescriptorImpl (impl)
org.apache.derby.catalog.DataTypeDescriptor (impl)
- getMaximumWidth
- getMaximumWidthInBytes
- getPrecision
- getScale
- isNullable

org.apache.derby.iapi.types.DataTypeUtilities
- getColumnDisplaySize
- getDigitPrecision
- getPrecision
- isNullable

org.apache.derby.iapi.types.TypeId
- getApproximateLengthInBytes
- getMaximumMaximumWidth (sic)
- getMaximumPrecision
- getMaximumScale
- isBlobTypeId
- isClobTypeId
- isLobTypeId
- isNumericTypeId

So how can/should I use this to get the information I need? If I need
to add information, where do I add it?

Thanks,

-- 
dt

Re: A question about types

Posted by Daniel John Debrunner <dj...@apache.org>.
Dyre.Tjeldvoll@Sun.COM wrote:

> But this fails to compile because DataTypeDescriptor.isCharacterType
> and DataTypeDescriptor.isBinaryType are private.
> 
>     [javac] /private/tmp/DERBY/derby-969/java/engine/org/apache/derby/catalog/GetProcedureColumns.java:289: isCharacterType(int) has private access in org.apache.derby.iapi.types.DataTypeDescriptor
>     [javac]                             if (DataTypeDescriptor.isCharacterType(type) ||
>     [javac]                                                       ^
>     [javac] /private/tmp/DERBY/derby-969/java/engine/org/apache/derby/catalog/GetProcedureColumns.java:290: isBinaryType(int) has private access in org.apache.derby.iapi.types.DataTypeDescriptor
>     [javac]                                     DataTypeDescriptor.isBinaryType(type)) {
> 
> 
> Is that intentional? Would it be ok to make them more accessible?

probably, though logically these methods belong on TypeId, to match the
other similar methods.

Dan.



Re: A question about types

Posted by "David W. Van Couvering" <Da...@Sun.COM>.
You could create a new JIRA issue that's a blocker for DERBY-969 given 
that you can't create subtasks....

Dyre.Tjeldvoll@Sun.COM wrote:
>
[snip]

> 
> Well, DERBY-969 the closest thing to a JIRA issue for adding
> JDBC 4.0 functionality to getProcedureColumns(). The work covered by
> DERBY-969, as it stands, is too much for a single JIRA issue IMHO.
> Since it is already a sub-task, JIRA will not let me create new
> sub-tasks under it...   
> 
> 

Re: A question about types

Posted by Dy...@Sun.COM.
Daniel John Debrunner <dj...@apache.org> writes:

> Dyre.Tjeldvoll@Sun.COM wrote:
>
>> While working on DERBY-925 and DERBY-969, I need to get information
>> about the types of the procedure/function parameters. Specifically I
>> need to fill the COLUMN_DEF (Default value for the parameter, if any)
>> and CHAR_OCTET_LENGTH (Byte length of char/binary types, NULL for
>> other types) columns.
>> 
>> So given type T, how do I
>> 
>> 1) Find out if T is character or binary?
>> 2) What the maximum length of T is, if char/binary?
>> 3) If T has a default value?
>> 4) If T's default value is NULL?
>> 5) If T's value cannot be represented without truncation?
>> 
> [snip]
>> 
>> So how can/should I use this to get the information I need? If I need
>> to add information, where do I add it?
>
> Just commenting on DERBY-925, I couldn't see how 969 related to types.

Well, DERBY-969 the closest thing to a JIRA issue for adding
JDBC 4.0 functionality to getProcedureColumns(). The work covered by
DERBY-969, as it stands, is too much for a single JIRA issue IMHO.
Since it is already a sub-task, JIRA will not let me create new
sub-tasks under it...   

>
> Are you baseing your work on
> org.apache.derby.catalog.GetProcedureColumns, because that probably
> hanldes most of the cases you require.

Yes, that's my plan.

>
> The javadoc for the org.apache.derby.iapi.types package may provide some
> background.

I'm looking at it :)

> A routine is partially described by
>
> org.apache.derby.catalog.types.RoutineAliasInfo
>
> That has methods
>
> public TypeDescriptor[] getParameterTypes()
> public public TypeDescriptor getReturnType()
>
> TypeDescriptor represents a type in the catalogs, it's best to create an
> instance of the runtime type descriptor DataTypeDescriptor from it.
>
> TypeDescriptor td; // get from array/method
>
> // TypeId represents the fixed aspect of a type, without
> // nullability or any length attributes.
> TypeId typeId = TypeId.getBuiltInTypeId(td.getJDBCTypeId());
>
> DataTypeDescriptor dtd = new DataTypeDescriptor(typeId,
> td.getPrecision(), td.getScale(), td.isNullable(), td.getMaximumWidth());
>
> I think the above block of code should be made into a utility method in
> DataTypeDescriptor, but it's not there yet.
>
> Anyway DataTypeDescriptor has all the methods you need, some may hang
> off the TypeId returned by getTypeId.
>
Thanks, that helps. One question though. In GetProcedureColumns I now
have the following (sqlType is the TypeDescriptor from RoutineAliasInfo):

		case CHAR_OCTET_LENGTH:
			if (sqlType != null) {
				int type = sqlType.getJDBCTypeId();
				if (DataTypeDescriptor.isCharacterType(type) ||
					DataTypeDescriptor.isBinaryType(type)) {
                        return sqlType.getMaximumWidthInBytes();
				}
			}
			return 0; // FIXME must be converted to SQL NULL in query

But this fails to compile because DataTypeDescriptor.isCharacterType
and DataTypeDescriptor.isBinaryType are private.

    [javac] /private/tmp/DERBY/derby-969/java/engine/org/apache/derby/catalog/GetProcedureColumns.java:289: isCharacterType(int) has private access in org.apache.derby.iapi.types.DataTypeDescriptor
    [javac]                             if (DataTypeDescriptor.isCharacterType(type) ||
    [javac]                                                       ^
    [javac] /private/tmp/DERBY/derby-969/java/engine/org/apache/derby/catalog/GetProcedureColumns.java:290: isBinaryType(int) has private access in org.apache.derby.iapi.types.DataTypeDescriptor
    [javac]                                     DataTypeDescriptor.isBinaryType(type)) {


Is that intentional? Would it be ok to make them more accessible?

> Function parameters and return types do not have defaults.

OK. 

COLUMN_DEF is only part of getProcedureColumns in
JDBC 4.0. So there is no COLUMN_DEF in the result set from
getFunctionParameters().
>
> Not sure about the truncation question, what's doing the truncation,
> what do you mean value of T?

This comes from the JDBC 4.0 javadoc for getProcedureColumns():

# COLUMN_DEF String => The default column value.

    * The string NULL (not enclosed in quotes) - If NULL was specified as the default value
    * TRUNCATE (not enclosed in quotes) - If the specified default value cannot be represented without truncation
    * NULL - If a default value was not specified 


>
> Hope this helps,

Yes, it does, thank you.

-- 
dt


Re: A question about types

Posted by Daniel John Debrunner <dj...@apache.org>.
Dyre.Tjeldvoll@Sun.COM wrote:

> Daniel John Debrunner <dj...@apache.org> writes:
> 
> 
>>TypeDescriptor represents a type in the catalogs, it's best to create an
>>instance of the runtime type descriptor DataTypeDescriptor from it.
>>
>>TypeDescriptor td; // get from array/method
>>
>>// TypeId represents the fixed aspect of a type, without
>>// nullability or any length attributes.
>>TypeId typeId = TypeId.getBuiltInTypeId(td.getJDBCTypeId());
>>
>>DataTypeDescriptor dtd = new DataTypeDescriptor(typeId,
>>td.getPrecision(), td.getScale(), td.isNullable(), td.getMaximumWidth());
> 
> 
> Hmm, if I read the javadoc correctly it seems like both
> DataTypeDescriptor and TypeDescriptorImpl (which both implement the
> TypeDescriptor interface) already have a TypeId member variable, and
> an accessor method called getTypeId(). Unfortunately, this method in
> NOT part of the TypeDescriptor interface, so the only way to get at it is
> to downcast the TypeDescriptor :(
> 
> Would it be bad to add getTypeId() to the TypeDescriptor interface?

You could use the code I showed the other day that created a
DataTypeDesciptor out from a catalog TypeDescriptor.

Dan.



Re: A question about types

Posted by Dy...@Sun.COM.
Daniel John Debrunner <dj...@apache.org> writes:

> TypeDescriptor represents a type in the catalogs, it's best to create an
> instance of the runtime type descriptor DataTypeDescriptor from it.
>
> TypeDescriptor td; // get from array/method
>
> // TypeId represents the fixed aspect of a type, without
> // nullability or any length attributes.
> TypeId typeId = TypeId.getBuiltInTypeId(td.getJDBCTypeId());
>
> DataTypeDescriptor dtd = new DataTypeDescriptor(typeId,
> td.getPrecision(), td.getScale(), td.isNullable(), td.getMaximumWidth());

Hmm, if I read the javadoc correctly it seems like both
DataTypeDescriptor and TypeDescriptorImpl (which both implement the
TypeDescriptor interface) already have a TypeId member variable, and
an accessor method called getTypeId(). Unfortunately, this method in
NOT part of the TypeDescriptor interface, so the only way to get at it is
to downcast the TypeDescriptor :(

Would it be bad to add getTypeId() to the TypeDescriptor interface?

-- 
dt


Re: A question about types

Posted by Daniel John Debrunner <dj...@apache.org>.
Dyre.Tjeldvoll@Sun.COM wrote:

> While working on DERBY-925 and DERBY-969, I need to get information
> about the types of the procedure/function parameters. Specifically I
> need to fill the COLUMN_DEF (Default value for the parameter, if any)
> and CHAR_OCTET_LENGTH (Byte length of char/binary types, NULL for
> other types) columns.
> 
> So given type T, how do I
> 
> 1) Find out if T is character or binary?
> 2) What the maximum length of T is, if char/binary?
> 3) If T has a default value?
> 4) If T's default value is NULL?
> 5) If T's value cannot be represented without truncation?
> 
[snip]
> 
> So how can/should I use this to get the information I need? If I need
> to add information, where do I add it?

Just commenting on DERBY-925, I couldn't see how 969 related to types.

Are you baseing your work on
org.apache.derby.catalog.GetProcedureColumns, because that probably
hanldes most of the cases you require.

The javadoc for the org.apache.derby.iapi.types package may provide some
background.

A routine is partially described by

org.apache.derby.catalog.types.RoutineAliasInfo

That has methods

public TypeDescriptor[] getParameterTypes()
public public TypeDescriptor getReturnType()

TypeDescriptor represents a type in the catalogs, it's best to create an
instance of the runtime type descriptor DataTypeDescriptor from it.

TypeDescriptor td; // get from array/method

// TypeId represents the fixed aspect of a type, without
// nullability or any length attributes.
TypeId typeId = TypeId.getBuiltInTypeId(td.getJDBCTypeId());

DataTypeDescriptor dtd = new DataTypeDescriptor(typeId,
td.getPrecision(), td.getScale(), td.isNullable(), td.getMaximumWidth());

I think the above block of code should be made into a utility method in
DataTypeDescriptor, but it's not there yet.

Anyway DataTypeDescriptor has all the methods you need, some may hang
off the TypeId returned by getTypeId.

Function parameters and return types do not have defaults.

Not sure about the truncation question, what's doing the truncation,
what do you mean value of T?

Hope this helps,
Dan.