pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
TEXT columns should indentify as java.sql.Types.CLOB
14+ messages / 6 participants
[nested] [flat]

* TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-16 09:01  Toni Helenius <Toni.Helenius@syncrontech.com>
  0 siblings, 2 replies; 14+ messages in thread

From: Toni Helenius @ 2010-08-16 09:01 UTC (permalink / raw)
  To: pgsql-jdbc

Hello,

I'm using Postgres 8.3.11 database and the latest JDBC driver 8.4 Build 701 (JDBC 4) + Java 6. In our databases there are TEXT type columns. However if I make a query to identify these fields in Java, the field DATA TYPE is VARCHAR and the length is 2147483647. Type name is correct; "TEXT". But as we need database independent code, we are using DATA TYPE as I presume is correct. And I think these TEXT fields should return java.sql.Types.CLOB as DATA TYPE instead of VARCHAR.

Here is some code:

Connection fromConn;
DatabaseMetaData metaFrom;
String userFrom;

fromConn = from.getConnection();
metaFrom = fromConn.getMetaData();
userFrom = ((PooledConnection)from).getTableOwner();

ResultSet cols = metaFrom.getColumns(null, userFrom, code, null);
while (cols.next()) {
	cols.getShort("DATA_TYPE");
	cols.getString("TYPE_NAME");
	}

--
Toni Helenius / Syncron Tech Oy
Lappeenranta, Finland




^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-16 09:30  Thomas Kellerer <spam_eater@gmx.net>
  parent: Toni Helenius <Toni.Helenius@syncrontech.com>
  1 sibling, 1 reply; 14+ messages in thread

From: Thomas Kellerer @ 2010-08-16 09:30 UTC (permalink / raw)
  To: pgsql-jdbc

Toni Helenius wrote on 16.08.2010 11:01:
> I'm using Postgres 8.3.11 database and the latest JDBC driver 8.4
> Build 701 (JDBC 4) + Java 6. In our databases there are TEXT type
> columns. However if I make a query to identify these fields in Java,
> the field DATA TYPE is VARCHAR and the length is 2147483647. Type
> name is correct; "TEXT". But as we need database independent code, we
> are using DATA TYPE as I presume is correct. And I think these TEXT
> fields should return java.sql.Types.CLOB as DATA TYPE instead of
> VARCHAR.

I agree, this would make the driver more JDBC compatible and it would behave more like other drivers.

Regards
Thomas




^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-16 11:18  Oliver Jowett <oliver@opencloud.com>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 14+ messages in thread

From: Oliver Jowett @ 2010-08-16 11:18 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-jdbc

Thomas Kellerer wrote:
> Toni Helenius wrote on 16.08.2010 11:01:
>> I'm using Postgres 8.3.11 database and the latest JDBC driver 8.4
>> Build 701 (JDBC 4) + Java 6. In our databases there are TEXT type
>> columns. However if I make a query to identify these fields in Java,
>> the field DATA TYPE is VARCHAR and the length is 2147483647. Type
>> name is correct; "TEXT". But as we need database independent code, we
>> are using DATA TYPE as I presume is correct. And I think these TEXT
>> fields should return java.sql.Types.CLOB as DATA TYPE instead of
>> VARCHAR.
> 
> I agree, this would make the driver more JDBC compatible and it would
> behave more like other drivers.

Returning Types.CLOB would imply that the preferred way to deal with
those columns is via getClob() / setClob(), which surely isn't correct
for TEXT columns in most cases?

-O



^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-16 13:47  Thomas Kellerer <spam_eater@gmx.net>
  parent: Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Thomas Kellerer @ 2010-08-16 13:47 UTC (permalink / raw)
  To: pgsql-jdbc

Oliver Jowett wrote on 16.08.2010 13:18:
> Thomas Kellerer wrote:
>> Toni Helenius wrote on 16.08.2010 11:01:
>>> I'm using Postgres 8.3.11 database and the latest JDBC driver 8.4
>>> Build 701 (JDBC 4) + Java 6. In our databases there are TEXT type
>>> columns. However if I make a query to identify these fields in Java,
>>> the field DATA TYPE is VARCHAR and the length is 2147483647. Type
>>> name is correct; "TEXT". But as we need database independent code, we
>>> are using DATA TYPE as I presume is correct. And I think these TEXT
>>> fields should return java.sql.Types.CLOB as DATA TYPE instead of
>>> VARCHAR.
>>
>> I agree, this would make the driver more JDBC compatible and it would
>> behave more like other drivers.
>
> Returning Types.CLOB would imply that the preferred way to deal with
> those columns is via getClob() / setClob(), which surely isn't correct
> for TEXT columns in most cases?
>

Not necessarily.

Almost all drivers I know (Oracle, jTDS, MySQL and DB2 I think) can handle getString() on a CLOB column without problems. (Oracle only since it's 10.x drivers)

Regards
Thomas




^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-16 14:28  Kevin Grittner <Kevin.Grittner@wicourts.gov>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 14+ messages in thread

From: Kevin Grittner @ 2010-08-16 14:28 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; pgsql-jdbc

Thomas Kellerer <spam_eater@gmx.net> wrote:
> Oliver Jowett wrote on 16.08.2010 13:18:
 
>> Returning Types.CLOB would imply that the preferred way to deal
>> with those columns is via getClob() / setClob(), which surely
>> isn't correct for TEXT columns in most cases?
>>
> 
> Not necessarily.
> 
> Almost all drivers I know (Oracle, jTDS, MySQL and DB2 I think)
> can handle getString() on a CLOB column without problems. (Oracle
> only since it's 10.x drivers)
 
I'm with Oliver.  I've developed a lot of highly portable code, and
*my* assumption would be that a column categorized as Types.CLOB
would support getClob() / setClob().  That it might or might not
support getString() seems pretty irrelevant to the issue.
 
Your argument seems to be that if something behaves like character
varying without the length limitations of some other database
products on such types, it should be considered CLOB.  I think it
should be related to whether the CLOB access methods are supported.
 
-Kevin



^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as  java.sql.Types.CLOB
@ 2010-08-16 14:52  Thomas Kellerer <spam_eater@gmx.net>
  parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
  0 siblings, 1 reply; 14+ messages in thread

From: Thomas Kellerer @ 2010-08-16 14:52 UTC (permalink / raw)
  To: pgsql-jdbc

Kevin Grittner wrote on 16.08.2010 16:28:
> I'm with Oliver.  I've developed a lot of highly portable code, and
> *my* assumption would be that a column categorized as Types.CLOB
> would support getClob() / setClob().  That it might or might not
> support getString() seems pretty irrelevant to the issue.
>
> Your argument seems to be that if something behaves like character
> varying without the length limitations of some other database
> products on such types, it should be considered CLOB.  I think it
> should be related to whether the CLOB access methods are supported.

You have a point here.

So _if_ the driver were to return Types.CLOB it would need to implement getClob()/setClob() as well.
I still think it would be more consitent, to flag those columns as CLOB but I underst that it would require a lot more work than simply changing the datatype return value.


Regards
Thomas




^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as  java.sql.Types.CLOB
@ 2010-08-16 15:10  Donald Fraser <postgres@kiwi-fraser.net>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 0 replies; 14+ messages in thread

From: Donald Fraser @ 2010-08-16 15:10 UTC (permalink / raw)
  To: pgsql-jdbc


> So _if_ the driver were to return Types.CLOB it would need to implement 
> getClob()/setClob() as well.
> I still think it would be more consitent, to flag those columns as CLOB 
> but I underst that it would require a lot more work than simply changing 
> the datatype return value.

I cannot see an argument for "more consistant".

Please read the following documentation taken from:
http://download-llnw.oracle.com/javase/1.3/docs/guide/jdbc/getstart/mapping.html

Mapping SQL and Java Types
NOTE: The material in this chapter is based on JDBCtm API Tutorial and 
Reference, Second Edition: Universal Data Access for the Javatm 2 Platform, 
published by Addison Wesley as part of the Java series, ISBN 0-201-43328-1.

8.3.1 CHAR, VARCHAR, and LONGVARCHAR
The JDBC types CHAR, VARCHAR, and LONGVARCHAR are closely related. CHAR 
represents a small, fixed-length character string, VARCHAR represents a 
small, variable-length character string, and LONGVARCHAR represents a large, 
variable-length character string.

8.4.2 CLOB
The JDBC type CLOB represents the SQL3 type CLOB (Character Large Object).

A JDBC CLOB value is mapped to an instance of the Clob interface in the Java 
programming language. If a driver follows the standard implementation, a 
Clob object logically points to the CLOB value on the server rather than 
containing its character data, greatly improving efficiency. Two of the 
methods on the Clob interface materialize the data of a CLOB object on the 
client.




^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-16 15:36  dmp <danap@ttc-cmc.net>
  parent: Toni Helenius <Toni.Helenius@syncrontech.com>
  1 sibling, 2 replies; 14+ messages in thread

From: dmp @ 2010-08-16 15:36 UTC (permalink / raw)
  To: Toni Helenius <Toni.Helenius@syncrontech.com>; pgsql-jdbc

>
>
>Hello,
>
>I'm using Postgres 8.3.11 database and the latest JDBC driver 8.4 Build 701 (JDBC 4) + Java 6. In our databases there are TEXT type columns. However if I make a query to identify these fields in Java, the field DATA TYPE is VARCHAR and the length is 2147483647. Type name is correct; "TEXT". But as we need database independent code, we are using DATA TYPE as I presume is correct. And I think these TEXT fields should return java.sql.Types.CLOB as DATA TYPE instead of VARCHAR.
>
>Here is some code:
>
>Connection fromConn;
>DatabaseMetaData metaFrom;
>String userFrom;
>
>fromConn = from.getConnection();
>metaFrom = fromConn.getMetaData();
>userFrom = ((PooledConnection)from).getTableOwner();
>
>ResultSet cols = metaFrom.getColumns(null, userFrom, code, null);
>while (cols.next()) {
>	cols.getShort("DATA_TYPE");
>	cols.getString("TYPE_NAME");
>	}
>
I'm just not seeing it. Your code example seems to be collecting the 
information
from the database connection not a particular table. If your application 
needs
to identify column types regardless of different databases then the way 
I do it
is through evaluation of the table columns. Attached file containing the 
output
for the last three or so PostgreSQL JDBC drivers. As far as TEXT and CLOB
types I would prefer then to be identifed independently.

danap

String sqlStatementString = "SELECT * FROM " + schemaTableName + " LIMIT 1";
ResultSet db_resultSet = sqlStatement.executeQuery(sqlStatementString);
DatabaseMetaData dbMetaData = dbConnection.getMetaData();
ResultSetMetaData tableMetaData = db_resultSet.getMetaData();

for (int i = 1; i < tableMetaData.getColumnCount() + 1; i++)
{
// Collect Information on Column.

colNameString = tableMetaData.getColumnName(i);
comboBoxNameString = parseColumnNameField(colNameString);
columnClass = tableMetaData.getColumnClassName(i);
columnType = tableMetaData.getColumnTypeName(i);
columnSize = Integer.valueOf(tableMetaData.getColumnDisplaySize(i));

System.out.println(i + " " + colNameString + " " +
comboBoxNameString + " " +
columnClass + " " + columnType + " " +
columnSize);
}

Attachments:

datatypes_postgresqloutput.txt (text/plain, 9.4K) download ----------------------- PostgreSQL 8.4-701 1 data_type_id Data Type Id java.lang.Integer int4 11 2 smallint_type Smallint Type java.lang.Integer int2 6 3 int_type Int Type java.lang.Integer int4 11 4 bigint_type Bigint Type java.lang.Long int8 20 5 decimal_type Decimal Type java.math.BigDecimal numeric 18 6 numeric_type Numeric Type java.math.BigDecimal numeric 12 7 real_type Real Type java.lang.Float float4 14 8 doubleprecision_type Doubleprecision Type java.lang.Double float8 24 9 serial_type Serial Type java.lang.Integer int4 11 10 bigserial_type Bigserial Type java.lang.Long int8 20 11 varchar_type Varchar Type java.lang.String varchar 30 12 char_type Char Type java.lang.String bpchar 30 13 text_type Text Type java.lang.String text 2147483647 14 bytea_type Bytea Type [B bytea 2147483647 15 date_type Date Type java.sql.Date date 13 16 time_type Time Type java.sql.Time time 15 17 timetz_type Timetz Type java.sql.Time timetz 21 18 timestamp_type Timestamp Type java.sql.Timestamp timestamp 29 19 timestamptz_type Timestamptz Type java.sql.Timestamp timestamptz 35 20 interval_type Interval Type org.postgresql.util.PGInterval interval 49 21 boolean_type Boolean Type java.lang.Boolean bool 1 22 point_type Point Type org.postgresql.geometric.PGpoint point 2147483647 23 linesegment_type Linesegment Type org.postgresql.geometric.PGlseg lseg 2147483647 24 box_type Box Type org.postgresql.geometric.PGbox box 2147483647 25 path_type Path Type org.postgresql.geometric.PGpath path 2147483647 26 polygon_type Polygon Type org.postgresql.geometric.PGpolygon polygon 2147483647 27 circle_type Circle Type org.postgresql.geometric.PGcircle circle 2147483647 28 cidr_type Cidr Type java.lang.Object cidr 2147483647 29 inet_type Inet Type java.lang.Object inet 2147483647 30 macaddr_type Macaddr Type java.lang.Object macaddr 2147483647 31 bit2_type Bit2 Type java.lang.Boolean bit 2 32 bitvarying5_type Bitvarying5 Type java.lang.Object varbit 5 ----------------------- PostgreSQL 8.3-603 1 data_type_id Data Type Id java.lang.Integer int4 11 2 smallint_type Smallint Type java.lang.Integer int2 6 3 int_type Int Type java.lang.Integer int4 11 4 bigint_type Bigint Type java.lang.Long int8 20 5 decimal_type Decimal Type java.math.BigDecimal numeric 18 6 numeric_type Numeric Type java.math.BigDecimal numeric 12 7 real_type Real Type java.lang.Float float4 14 8 doubleprecision_type Doubleprecision Type java.lang.Double float8 24 9 serial_type Serial Type java.lang.Integer int4 11 10 bigserial_type Bigserial Type java.lang.Long int8 20 11 varchar_type Varchar Type java.lang.String varchar 30 12 char_type Char Type java.lang.String bpchar 30 13 text_type Text Type java.lang.String text 2147483647 14 bytea_type Bytea Type [B bytea 2147483647 15 date_type Date Type java.sql.Date date 13 16 time_type Time Type java.sql.Time time 15 17 timetz_type Timetz Type java.sql.Time timetz 21 18 timestamp_type Timestamp Type java.sql.Timestamp timestamp 29 19 timestamptz_type Timestamptz Type java.sql.Timestamp timestamptz 35 20 interval_type Interval Type org.postgresql.util.PGInterval interval 49 21 boolean_type Boolean Type java.lang.Boolean bool 1 22 point_type Point Type org.postgresql.geometric.PGpoint point 2147483647 23 linesegment_type Linesegment Type org.postgresql.geometric.PGlseg lseg 2147483647 24 box_type Box Type org.postgresql.geometric.PGbox box 2147483647 25 path_type Path Type org.postgresql.geometric.PGpath path 2147483647 26 polygon_type Polygon Type org.postgresql.geometric.PGpolygon polygon 2147483647 27 circle_type Circle Type org.postgresql.geometric.PGcircle circle 2147483647 28 cidr_type Cidr Type java.lang.Object cidr 2147483647 29 inet_type Inet Type java.lang.Object inet 2147483647 30 macaddr_type Macaddr Type java.lang.Object macaddr 2147483647 31 bit2_type Bit2 Type java.lang.Boolean bit 2 32 bitvarying5_type Bitvarying5 Type java.lang.Object varbit 5 ----------------------- PostgreSQL 8.2-506 1 data_type_id Data Type Id java.lang.Integer int4 11 2 smallint_type Smallint Type java.lang.Integer int2 6 3 int_type Int Type java.lang.Integer int4 11 4 bigint_type Bigint Type java.lang.Long int8 20 5 decimal_type Decimal Type java.math.BigDecimal numeric 18 6 numeric_type Numeric Type java.math.BigDecimal numeric 12 7 real_type Real Type java.lang.Float float4 14 8 doubleprecision_type Doubleprecision Type java.lang.Double float8 24 9 serial_type Serial Type java.lang.Integer int4 11 10 bigserial_type Bigserial Type java.lang.Long int8 20 11 varchar_type Varchar Type java.lang.String varchar 30 12 char_type Char Type java.lang.String bpchar 30 13 text_type Text Type java.lang.String text 2147483647 14 bytea_type Bytea Type [B bytea 2147483647 15 date_type Date Type java.sql.Date date 13 16 time_type Time Type java.sql.Time time 15 17 timetz_type Timetz Type java.sql.Time timetz 21 18 timestamp_type Timestamp Type java.sql.Timestamp timestamp 29 19 timestamptz_type Timestamptz Type java.sql.Timestamp timestamptz 35 20 interval_type Interval Type org.postgresql.util.PGInterval interval 49 21 boolean_type Boolean Type java.lang.Boolean bool 1 22 point_type Point Type org.postgresql.geometric.PGpoint point 2147483647 23 linesegment_type Linesegment Type org.postgresql.geometric.PGlseg lseg 2147483647 24 box_type Box Type org.postgresql.geometric.PGbox box 2147483647 25 path_type Path Type org.postgresql.geometric.PGpath path 2147483647 26 polygon_type Polygon Type org.postgresql.geometric.PGpolygon polygon 2147483647 27 circle_type Circle Type org.postgresql.geometric.PGcircle circle 2147483647 28 cidr_type Cidr Type java.lang.Object cidr 2147483647 29 inet_type Inet Type java.lang.Object inet 2147483647 30 macaddr_type Macaddr Type java.lang.Object macaddr 2147483647 31 bit2_type Bit2 Type java.lang.Boolean bit 2 32 bitvarying2_type Bitvarying2 Type java.lang.Object varbit 5 ----------------------- PostgreSQL 8.3-603 Arrays (Class Now All Array) 1 data_type_id Data Type Id java.lang.Integer int4 11 2 smallint_array Smallint Array java.sql.Array _int2 2147483647 3 int_array Int Array java.sql.Array _int4 2147483647 4 bigint_array Bigint Array java.sql.Array _int8 2147483647 5 decimal_array Decimal Array java.sql.Array _numeric 2147483647 6 numeric_array Numeric Array java.sql.Array _numeric 2147483647 7 real_array Real Array java.sql.Array _float4 2147483647 8 doubleprecision_array Doubleprecision Array java.sql.Array _float8 2147483647 9 varchar_array Varchar Array java.sql.Array _varchar 2147483647 10 char_array Char Array java.sql.Array _bpchar 2147483647 11 text_array Text Array java.sql.Array _text 2147483647 12 date_array Date Array java.sql.Array _date 2147483647 13 time_array Time Array java.sql.Array _time 2147483647 14 timetz_array Timetz Array java.sql.Array _timetz 2147483647 15 timestamp_array Timestamp Array java.sql.Array _timestamp 2147483647 16 timestamptz_array Timestamptz Array java.sql.Array _timestamptz 2147483647 17 interval_array Interval Array java.sql.Array _interval 2147483647 18 boolean_array Boolean Array java.sql.Array _bool 2147483647 19 point_array Point Array java.sql.Array _point 2147483647 20 linesegment_array Linesegment Array java.sql.Array _lseg 2147483647 21 box_array Box Array java.sql.Array _box 2147483647 22 path_array Path Array java.sql.Array _path 2147483647 23 polygon_array Polygon Array java.sql.Array _polygon 2147483647 24 circle_array Circle Array java.sql.Array _circle 2147483647 25 cidr_array Cidr Array java.sql.Array _cidr 2147483647 26 inet_array Inet Array java.sql.Array _inet 2147483647 27 macaddr_array Macaddr Array java.sql.Array _macaddr 2147483647 28 bit2_array Bit2 Array java.sql.Array _bit 2147483647 29 bitvarying5_array Bitvarying5 Array java.sql.Array _varbit 2147483647 ----------------------- PostgreSQL 8.2-506 Arrays (Class Object & Array) 1 data_type_id Data Type Id java.lang.Integer int4 11 2 smallint_array Smallint Array java.sql.Array _int2 2147483647 3 int_array Int Array java.sql.Array _int4 2147483647 4 bigint_array Bigint Array java.sql.Array _int8 2147483647 5 decimal_array Decimal Array java.sql.Array _numeric 2147483647 6 numeric_array Numeric Array java.sql.Array _numeric 2147483647 7 real_array Real Array java.sql.Array _float4 2147483647 8 doubleprecision_array Doubleprecision Array java.sql.Array _float8 2147483647 9 varchar_array Varchar Array java.sql.Array _varchar 2147483647 10 char_array Char Array java.sql.Array _bpchar 2147483647 11 text_array Text Array java.sql.Array _text 2147483647 12 date_array Date Array java.sql.Array _date 2147483647 13 time_array Time Array java.sql.Array _time 2147483647 14 timetz_array Timetz Array java.sql.Array _timetz 2147483647 15 timestamp_array Timestamp Array java.sql.Array _timestamp 2147483647 16 timestamptz_array Timestamptz Array java.sql.Array _timestamptz 2147483647 17 interval_array Interval Array java.lang.Object _interval 2147483647 18 point_array Point Array java.lang.Object _point 2147483647 19 linesegment_array Linesegment Array java.lang.Object _lseg 2147483647 20 box_array Box Array java.lang.Object _box 2147483647 21 path_array Path Array java.lang.Object _path 2147483647 22 polygon_array Polygon Array java.lang.Object _polygon 2147483647 23 circle_array Circle Array java.lang.Object _circle 2147483647 24 cidr_array Cidr Array java.lang.Object _cidr 2147483647 25 inet_array Inet Array java.lang.Object _inet 2147483647 26 macaddr_array Macaddr Array java.lang.Object _macaddr 2147483647 27 bit2_array Bit2 Array java.sql.Array _bit 2147483647 28 bitvarying5_array Bitvarying5 Array java.lang.Object _varbit 2147483647
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-17 07:08  Toni Helenius <Toni.Helenius@syncrontech.com>
  parent: dmp <danap@ttc-cmc.net>
  1 sibling, 0 replies; 14+ messages in thread

From: Toni Helenius @ 2010-08-17 07:08 UTC (permalink / raw)
  To: dmp <danap@ttc-cmc.net>; pgsql-jdbc

Hi, 

Thanks for the reply. Your code essentially does the same, except it relies that the table has some data in it to return a line. My code asks for columns in a certain table. It works as well.

You get:
text_type Text Type java.lang.String text 2147483647

You're not actually mapping the classes in a way we need to do in this case. This tells me that I should read te TEXT field as java.lang.String to Java, that is correct. But I need to create a CREATE SQL dynamically from these accross different databases, mapping the fields (also utilizing our fixed field mappings). Reading the field values isn't necessary here. This is why I need the details on what kind of field in common SQL sense we are talking about. This provides pretty good database independency!

cols.getShort("DATA_TYPE");
returns the java.sql.Types enumeration. TEXT field returns VARCHAR. Which in my oppinion should be CLOB.

ResultSet cols = metaFrom.getColumns(null, userFrom, code, null);
userFrom = schema string; code = table name;


-----Original Message-----
From: dmp [mailto:danap@ttc-cmc.net] 
Sent: 16. elokuuta 2010 18:36
To: Toni Helenius; pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] TEXT columns should indentify as java.sql.Types.CLOB

>
>
>Hello,
>
>I'm using Postgres 8.3.11 database and the latest JDBC driver 8.4 Build 701 (JDBC 4) + Java 6. In our databases there are TEXT type columns. However if I make a query to identify these fields in Java, the field DATA TYPE is VARCHAR and the length is 2147483647. Type name is correct; "TEXT". But as we need database independent code, we are using DATA TYPE as I presume is correct. And I think these TEXT fields should return java.sql.Types.CLOB as DATA TYPE instead of VARCHAR.
>
>Here is some code:
>
>Connection fromConn;
>DatabaseMetaData metaFrom;
>String userFrom;
>
>fromConn = from.getConnection();
>metaFrom = fromConn.getMetaData();
>userFrom = ((PooledConnection)from).getTableOwner();
>
>ResultSet cols = metaFrom.getColumns(null, userFrom, code, null); while 
>(cols.next()) {
>	cols.getShort("DATA_TYPE");
>	cols.getString("TYPE_NAME");
>	}
>
I'm just not seeing it. Your code example seems to be collecting the information from the database connection not a particular table. If your application needs to identify column types regardless of different databases then the way I do it is through evaluation of the table columns. Attached file containing the output for the last three or so PostgreSQL JDBC drivers. As far as TEXT and CLOB types I would prefer then to be identifed independently.

danap

String sqlStatementString = "SELECT * FROM " + schemaTableName + " LIMIT 1"; ResultSet db_resultSet = sqlStatement.executeQuery(sqlStatementString);
DatabaseMetaData dbMetaData = dbConnection.getMetaData(); ResultSetMetaData tableMetaData = db_resultSet.getMetaData();

for (int i = 1; i < tableMetaData.getColumnCount() + 1; i++) { // Collect Information on Column.

colNameString = tableMetaData.getColumnName(i); comboBoxNameString = parseColumnNameField(colNameString);
columnClass = tableMetaData.getColumnClassName(i);
columnType = tableMetaData.getColumnTypeName(i);
columnSize = Integer.valueOf(tableMetaData.getColumnDisplaySize(i));

System.out.println(i + " " + colNameString + " " + comboBoxNameString + " " + columnClass + " " + columnType + " " + columnSize); }



^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-18 07:04  Toni Helenius <Toni.Helenius@syncrontech.com>
  parent: dmp <danap@ttc-cmc.net>
  1 sibling, 1 reply; 14+ messages in thread

From: Toni Helenius @ 2010-08-18 07:04 UTC (permalink / raw)
  To: dmp <danap@ttc-cmc.net>; pgsql-jdbc

Hi,

An definitely a bug: BOOLEAN columns get identified as java.sql.Types.VARCHAR. Not java.sql.Types.BOOLEAN as they should. I end up getting SQL create statements where boolean fields are translated back as VARCHAR(1).

-----Original Message-----
From: dmp [mailto:danap@ttc-cmc.net] 
Sent: 16. elokuuta 2010 18:36
To: Toni Helenius; pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] TEXT columns should indentify as java.sql.Types.CLOB

>
>
>Hello,
>
>I'm using Postgres 8.3.11 database and the latest JDBC driver 8.4 Build 701 (JDBC 4) + Java 6. In our databases there are TEXT type columns. However if I make a query to identify these fields in Java, the field DATA TYPE is VARCHAR and the length is 2147483647. Type name is correct; "TEXT". But as we need database independent code, we are using DATA TYPE as I presume is correct. And I think these TEXT fields should return java.sql.Types.CLOB as DATA TYPE instead of VARCHAR.
>
>Here is some code:
>
>Connection fromConn;
>DatabaseMetaData metaFrom;
>String userFrom;
>
>fromConn = from.getConnection();
>metaFrom = fromConn.getMetaData();
>userFrom = ((PooledConnection)from).getTableOwner();
>
>ResultSet cols = metaFrom.getColumns(null, userFrom, code, null); while 
>(cols.next()) {
>	cols.getShort("DATA_TYPE");
>	cols.getString("TYPE_NAME");
>	}
>
I'm just not seeing it. Your code example seems to be collecting the information from the database connection not a particular table. If your application needs to identify column types regardless of different databases then the way I do it is through evaluation of the table columns. Attached file containing the output for the last three or so PostgreSQL JDBC drivers. As far as TEXT and CLOB types I would prefer then to be identifed independently.

danap

String sqlStatementString = "SELECT * FROM " + schemaTableName + " LIMIT 1"; ResultSet db_resultSet = sqlStatement.executeQuery(sqlStatementString);
DatabaseMetaData dbMetaData = dbConnection.getMetaData(); ResultSetMetaData tableMetaData = db_resultSet.getMetaData();

for (int i = 1; i < tableMetaData.getColumnCount() + 1; i++) { // Collect Information on Column.

colNameString = tableMetaData.getColumnName(i); comboBoxNameString = parseColumnNameField(colNameString);
columnClass = tableMetaData.getColumnClassName(i);
columnType = tableMetaData.getColumnTypeName(i);
columnSize = Integer.valueOf(tableMetaData.getColumnDisplaySize(i));

System.out.println(i + " " + colNameString + " " + comboBoxNameString + " " + columnClass + " " + columnType + " " + columnSize); }



^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-18 07:31  Oliver Jowett <oliver@opencloud.com>
  parent: Toni Helenius <Toni.Helenius@syncrontech.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Oliver Jowett @ 2010-08-18 07:31 UTC (permalink / raw)
  To: Toni Helenius <Toni.Helenius@syncrontech.com>; +Cc: pgsql-jdbc

Toni Helenius wrote:
> Hi,
> 
> An definitely a bug: BOOLEAN columns get identified as java.sql.Types.VARCHAR. Not java.sql.Types.BOOLEAN as they should. I end up getting SQL create statements where boolean fields are translated back as VARCHAR(1).

That does sound like a bug. Do you have a self-contained testcase
showing the problem?

-O



^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-18 08:15  Toni Helenius <Toni.Helenius@syncrontech.com>
  parent: Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Toni Helenius @ 2010-08-18 08:15 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

Hi,

Here is the best I can do, I'm sure you'll manage to fill in the holes:

private static final String DRIVER = "org.postgresql.Driver";
private static final String URL = "jdbc:postgresql://localhost/testdb";
private static final String USERNAME = "root";
private static final String PASSWORD = "";

Class.forName("DRIVER");
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
DatabaseMetaData  metaFrom = connection.getMetaData();
// Get the columns
ResultSet cols = metaFrom.getColumns(null, YOUR_SCHEMA_HERE, YOUR_TABLE_NAME_HERE, null);

// Get column info
while (cols.next()) {
	println("Column name: " + cols.getString("COLUMN_NAME"));
	short type = cols.getShort("DATA_TYPE"));

	if (java.sql.Types.BOOLEAN == type) {
		println("A boolean");
	}
	else {
		println("Not a boolean! (" + type + ")");
	}
	println("Postgre calls this: " + cols.getString("TYPE_NAME"));
	}


-----Original Message-----
From: Oliver Jowett [mailto:oliver@opencloud.com] 
Sent: 18. elokuuta 2010 10:32
To: Toni Helenius
Cc: pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] TEXT columns should indentify as java.sql.Types.CLOB

Toni Helenius wrote:
> Hi,
> 
> An definitely a bug: BOOLEAN columns get identified as java.sql.Types.VARCHAR. Not java.sql.Types.BOOLEAN as they should. I end up getting SQL create statements where boolean fields are translated back as VARCHAR(1).

That does sound like a bug. Do you have a self-contained testcase
showing the problem?

-O



^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-18 08:19  Oliver Jowett <oliver@opencloud.com>
  parent: Toni Helenius <Toni.Helenius@syncrontech.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Oliver Jowett @ 2010-08-18 08:19 UTC (permalink / raw)
  To: Toni Helenius <Toni.Helenius@syncrontech.com>; +Cc: pgsql-jdbc

Toni Helenius wrote:
> Hi,
> 
> Here is the best I can do, I'm sure you'll manage to fill in the holes:

Well, I was more interested in how you were ending up with VARCHAR.
It's normal for booleans to be mapped to Types.BIT (see previous list
discussion)

> 	if (java.sql.Types.BOOLEAN == type) {
> 		println("A boolean");
> 	}

-O



^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: TEXT columns should indentify as java.sql.Types.CLOB
@ 2010-08-18 09:01  Toni Helenius <Toni.Helenius@syncrontech.com>
  parent: Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 14+ messages in thread

From: Toni Helenius @ 2010-08-18 09:01 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

Hi,

Yes, good sir, you're right. It got mapped as a BIT. My hastiness and original if-else was a bit off on this. Sorry.

Still how do you feel about TEXT columns mapping as VARCHAR?

-----Original Message-----
From: Oliver Jowett [mailto:oliver@opencloud.com] 
Sent: 18. elokuuta 2010 11:19
To: Toni Helenius
Cc: pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] TEXT columns should indentify as java.sql.Types.CLOB

Toni Helenius wrote:
> Hi,
> 
> Here is the best I can do, I'm sure you'll manage to fill in the holes:

Well, I was more interested in how you were ending up with VARCHAR.
It's normal for booleans to be mapped to Types.BIT (see previous list
discussion)

> 	if (java.sql.Types.BOOLEAN == type) {
> 		println("A boolean");
> 	}

-O



^ permalink  raw  reply  [nested|flat] 14+ messages in thread


end of thread, other threads:[~2010-08-18 09:01 UTC | newest]

Thread overview: 14+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-08-16 09:01 TEXT columns should indentify as java.sql.Types.CLOB Toni Helenius <Toni.Helenius@syncrontech.com>
2010-08-16 09:30 ` Thomas Kellerer <spam_eater@gmx.net>
2010-08-16 11:18   ` Oliver Jowett <oliver@opencloud.com>
2010-08-16 13:47     ` Thomas Kellerer <spam_eater@gmx.net>
2010-08-16 14:28       ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2010-08-16 14:52         ` Re: TEXT columns should indentify as  java.sql.Types.CLOB Thomas Kellerer <spam_eater@gmx.net>
2010-08-16 15:10           ` Re: TEXT columns should indentify as  java.sql.Types.CLOB Donald Fraser <postgres@kiwi-fraser.net>
2010-08-16 15:36 ` dmp <danap@ttc-cmc.net>
2010-08-17 07:08   ` Toni Helenius <Toni.Helenius@syncrontech.com>
2010-08-18 07:04   ` Toni Helenius <Toni.Helenius@syncrontech.com>
2010-08-18 07:31     ` Oliver Jowett <oliver@opencloud.com>
2010-08-18 08:15       ` Toni Helenius <Toni.Helenius@syncrontech.com>
2010-08-18 08:19         ` Oliver Jowett <oliver@opencloud.com>
2010-08-18 09:01           ` Toni Helenius <Toni.Helenius@syncrontech.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox