pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bug in DatabaseMetaData.getColumns() with columns based on domains
8+ messages / 3 participants
[nested] [flat]

* Bug in DatabaseMetaData.getColumns() with columns based on domains
@ 2010-10-21 12:57  Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 2 replies; 8+ messages in thread

From: Thomas Kellerer @ 2010-10-21 12:57 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,

consider the following table and domain:

CREATE DOMAIN salary_domain AS numeric(12,2) NOT NULL CHECK (value > 0);
CREATE TABLE employee (id integer not null, salary salary_domain);

DatabaseMetaData.getColumns(null, "public", "employee", "%");

returns "YES" for the column IS_NULLABLE in the ResultSet whereas it should flag that column as not nullable.

Regards
Thomas




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

* Re: Bug in DatabaseMetaData.getColumns() with columns based on domains
@ 2010-10-21 17:57  dmp <danap@ttc-cmc.net>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  1 sibling, 1 reply; 8+ messages in thread

From: dmp @ 2010-10-21 17:57 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-jdbc

Thomas Kellerer wrote:
> Hi,
>
> consider the following table and domain:
>
> CREATE DOMAIN salary_domain AS numeric(12,2) NOT NULL CHECK (value > 0);
> CREATE TABLE employee (id integer not null, salary salary_domain);
>
> DatabaseMetaData.getColumns(null, "public", "employee", "%");
>
> returns "YES" for the column IS_NULLABLE in the ResultSet whereas it
> should flag that column as not nullable.
>
> Regards
> Thomas
>
>
Perhaps you could provide the Database and JDBC versions to help those that
will need to review the report. I have confirmed on PostgreSQL 9.0.1 and
JDBC postgresql-9.0-801.jdbc3.

Output:

Connection Created
SELECT * FROM "public"."employee" LIMIT 1
Column Name: id IS_NULLABLE: NO
Column Name: salary IS_NULLABLE: YES
Connection Closed

Attached files needed to setup test case and other reviewed data.

danap.

Attachments:

PostgreSQL_JDBC.java (text/plain, 5.7K) download //================================================================= // PostgreSQL_JDBC Class //================================================================= // // This class is used to control the running of the a generic // class to access the PostgreSQL database. // // << PostgreSQL_JDBC.java >> // //================================================================= // Copyright (C) 2010 Dana M. Proctor // Version 1.02 10/21/2010 // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version // 2 of the License, or (at your option) any later version. This // program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // the GNU General Public License for more details. You should // have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // (http://opensource.org) // //----------------------------------------------------------------- // danap@dandymadeproductions.com //================================================================= //================================================================= // PostgreSQL_JDBC Application //================================================================= import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.io.*; import java.sql.*; /** * The PostgreSQL class is used to control the running of the a generic class to * access the PostgreSQL database. Arguments -debug. * @author Dana M. Proctor * @version 1.02 10/21/2010 */ class PostgreSQL_JDBC { //============================================================== // PostgreSQL_JDBC Constructor //============================================================== public PostgreSQL_JDBC(Connection con) { testColumnNull(con); } //============================================================== // Test Methods // CREATE DOMAIN salary_domain AS numeric(12,2) NOT NULL CHECK (value > 0); // CREATE TABLE employee (id integer not null, salary salary_domain); // DatabaseMetaData.getColumns(null, "public", "employee", "%"); //============================================================== private void testColumnNull(Connection con) { // Method Instances DatabaseMetaData dbMetaData; ResultSetMetaData tableMetaData; String sqlStatementString; Statement sqlStatement; ResultSet rs, db_resultSet; String identifierQuoteString = "\""; try { // Setup a connection statement. sqlStatement = con.createStatement(); sqlStatementString = "SELECT * FROM " + identifierQuoteString + "public" + identifierQuoteString + "." + identifierQuoteString + "employee" + identifierQuoteString + " LIMIT 1"; System.out.println(sqlStatementString); db_resultSet = sqlStatement.executeQuery(sqlStatementString); dbMetaData = con.getMetaData(); rs = dbMetaData.getColumns(null, "public", "employee", "%"); while (rs.next()) { System.out.print("Column Name: " + rs.getString("COLUMN_NAME") + " " + "IS_NULLABLE: " + rs.getString("IS_NULLABLE")); System.out.println(); } rs.close(); db_resultSet.close(); sqlStatement.close(); } catch (SQLException sqle) { System.out.println("SQL Exeception" + sqle); } } //============================================================ // Main public access point method for instantiating the // PostgreSQL_JDBC application. Arguments: database, username, // & password. //============================================================== public static void main(String[] args) { String host, database, username, password; Connection dbConnection; // Collect connection properties. and setup connection. host = "localhost"; dbConnection = null; if (args.length != 0) { database = args[0]; username = (args.length > 1) ? args[1] : null; password = (args.length > 2) ? args[2] : null; } else { database = "your_database"; username = "your_username"; password = "your_password"; } try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException cne) {System.out.println("ClassNotFoundException" + cne.toString());} // Try to make a connection. try { dbConnection = DriverManager.getConnection("jdbc:postgresql://" + host + "/" + database, username, password); // Connection Good. if (dbConnection != null) { System.out.println("Connection Created"); // Go Do Something. new PostgreSQL_JDBC(dbConnection); // Close. dbConnection.close(); System.out.println("Connection Closed"); } } catch (SQLException sqle) {System.out.println("SQLExeception" + sqle.toString());} } }
public_employee_20101021.sql (text/plain, 585B) download -- -- MyJSQLView SQL Dump -- Version: 3.23 -- WebSite: http://myjsqlview.org -- -- Host: 127.0.0.1 -- Generated On: 2010.10.21 AD at 11:13:04 MDT -- SQL version: PostgreSQL 9.0.1 -- Database: key_tables -- -- ------------------------------------------ -- -- Table structure for table "public"."employee" -- DROP DOMAIN IF EXISTS "salary_domain"; CREATE DOMAIN "salary_domain" AS numeric(12,2) NOT NULL CHECK (value > 0); DROP TABLE IF EXISTS "public"."employee"; CREATE TABLE "public"."employee" ( "id" serial NOT NULL, "salary" "salary_domain", PRIMARY KEY ("id") );
public_employee_columns.sql (text/plain, 326B) download SELECT table_catalog, table_name, column_name, ordinal_position, column_default, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_scale, datetime_precision FROM information_schema.columns WHERE table_catalog='key_tables' AND table_schema='public' AND table_name='employee' ORDER BY ordinal_position
employee_columns_20101021.pdf (application/pdf, 2.1K) download ^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: Bug in DatabaseMetaData.getColumns() with columns based on domains
@ 2010-10-21 19:54  Thomas Kellerer <spam_eater@gmx.net>
  parent: dmp <danap@ttc-cmc.net>
  0 siblings, 0 replies; 8+ messages in thread

From: Thomas Kellerer @ 2010-10-21 19:54 UTC (permalink / raw)
  To: pgsql-jdbc

dmp wrote on 21.10.2010 19:57:
>> consider the following table and domain:
>>
>> CREATE DOMAIN salary_domain AS numeric(12,2) NOT NULL CHECK (value > 0);
>> CREATE TABLE employee (id integer not null, salary salary_domain);
>>
>> DatabaseMetaData.getColumns(null, "public", "employee", "%");
>>
>> returns "YES" for the column IS_NULLABLE in the ResultSet whereas it
>> should flag that column as not nullable.
>>
> Perhaps you could provide the Database and JDBC versions to help those that
> will need to review the report. I have confirmed on PostgreSQL 9.0.1 and
> JDBC postgresql-9.0-801.jdbc3.

Sorry, you are right I should have mentioned that.

I tried it with the exact same version as you (PG 9.0.1 and JDBC 9.0-801)

Regards
Thomas




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

* Re: Bug in DatabaseMetaData.getColumns() with columns based on domains
@ 2010-10-21 20:20  Thomas Kellerer <spam_eater@gmx.net>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  1 sibling, 1 reply; 8+ messages in thread

From: Thomas Kellerer @ 2010-10-21 20:20 UTC (permalink / raw)
  To: pgsql-jdbc

Thomas Kellerer wrote on 21.10.2010 14:57:
> Hi,
>
> consider the following table and domain:
>
> CREATE DOMAIN salary_domain AS numeric(12,2) NOT NULL CHECK (value > 0);
> CREATE TABLE employee (id integer not null, salary salary_domain);
>
> DatabaseMetaData.getColumns(null, "public", "employee", "%");
>
> returns "YES" for the column IS_NULLABLE in the ResultSet whereas it should flag that column as not nullable.
>

OK, I realized that not even psql or pgAdmin will display such a column as "not null"
So it is not a JDBC driver issue (but rather a PostgreSQL "core" issue)

Sorry for the noise
Thomas




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

* Re: Bug in DatabaseMetaData.getColumns() with columns based on domains
@ 2010-10-22 01:25  dmp <danap@ttc-cmc.net>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 8+ messages in thread

From: dmp @ 2010-10-22 01:25 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-jdbc

Thomas Kellerer wrote:
> Thomas Kellerer wrote on 21.10.2010 14:57:
>> Hi,
>>
>> consider the following table and domain:
>>
>> CREATE DOMAIN salary_domain AS numeric(12,2) NOT NULL CHECK (value > 0);
>> CREATE TABLE employee (id integer not null, salary salary_domain);
>>
>> DatabaseMetaData.getColumns(null, "public", "employee", "%");
>>
>> returns "YES" for the column IS_NULLABLE in the ResultSet whereas it
>> should flag that column as not nullable.
>>
>
> OK, I realized that not even psql or pgAdmin will display such a column
> as "not null"
> So it is not a JDBC driver issue (but rather a PostgreSQL "core" issue)
>
> Sorry for the noise
> Thomas
>
>

When the table field salary is defined directly the correct attribute for
IS_NULLABLE is returned by the DatabaseMetaData.getColumns(). When the
field is defined indirectly through the DOMAIN then the database appears
to be not setting the attnotnull entry correspondingly. As you indicated
I would also say it is the database. The attached files indicate the sql
statement generated by the JDBC and the result set for the getColumns()
for the table.

The early attached file query for the column's information was from the 
information_schema.columns table and does show the correct result for the
salary field IS_NULLABLE.

danap.

Attachments:

pgjdbc_column_select.sql (text/plain, 993B) download SELECT * FROM (SELECT n.nspname,c.relname,a.attname,a.atttypid,a.attnotnull,a.atttypmod,a.attlen,row_number() OVER (PARTITION BY a.attrelid ORDER BY a.attnum) AS attnum, pg_catalog.pg_get_expr(def.adbin, def.adrelid) AS adsrc,dsc.description,t.typbasetype,t.typtype FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_class c ON (c.relnamespace = n.oid) JOIN pg_catalog.pg_attribute a ON (a.attrelid=c.oid) JOIN pg_catalog.pg_type t ON (a.atttypid = t.oid) LEFT JOIN pg_catalog.pg_attrdef def ON (a.attrelid=def.adrelid AND a.attnum = def.adnum) LEFT JOIN pg_catalog.pg_description dsc ON (c.oid=dsc.objoid AND a.attnum = dsc.objsubid) LEFT JOIN pg_catalog.pg_class dc ON (dc.oid=dsc.classoid AND dc.relname='pg_class') LEFT JOIN pg_catalog.pg_namespace dn ON (dc.relnamespace=dn.oid AND dn.nspname='pg_catalog') WHERE a.attnum > 0 AND NOT a.attisdropped AND n.nspname LIKE 'public' AND c.relname LIKE 'employee' ) c WHERE true AND attname LIKE '%' ORDER BY nspname,c.relname,attnum
employee_columns2_20101021.pdf (application/pdf, 2.1K) download ^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: Bug in DatabaseMetaData.getColumns() with columns based on domains
@ 2010-10-23 07:37  Kris Jurka <books@ejurka.com>
  parent: dmp <danap@ttc-cmc.net>
  0 siblings, 2 replies; 8+ messages in thread

From: Kris Jurka @ 2010-10-23 07:37 UTC (permalink / raw)
  To: dmp <danap@ttc-cmc.net>; +Cc: Thomas Kellerer <spam_eater@gmx.net>; pgsql-jdbc



On Thu, 21 Oct 2010, dmp wrote:

> The early attached file query for the column's information was from the 
> information_schema.columns table and does show the correct result for the
> salary field IS_NULLABLE.
>

Thanks for the hint.  Doing the same thing that the information_schema 
does was pretty easy.  I've fixed this in CVS and I've also adjusted 
ResultSetMetaData.isNullable to have the same behavior.  I've backpatched 
this to 8.4 because that's as far as was easy.

Here's a jar you can test with:

http://ejurka.com/pgsql/jars/domnn/

Kris Jurka



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

* Re: Bug in DatabaseMetaData.getColumns() with columns based on domains
@ 2010-10-23 15:30  dmp <danap@ttc-cmc.net>
  parent: Kris Jurka <books@ejurka.com>
  1 sibling, 0 replies; 8+ messages in thread

From: dmp @ 2010-10-23 15:30 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-jdbc

> On Thu, 21 Oct 2010, dmp wrote:
>
>> The early attached file query for the column's information was from
>> the information_schema.columns table and does show the correct result
>> for the
>> salary field IS_NULLABLE.
>>
 >
> Kris Jurka wrote:
> Thanks for the hint. Doing the same thing that the information_schema
> does was pretty easy. I've fixed this in CVS and I've also adjusted
> ResultSetMetaData.isNullable to have the same behavior. I've backpatched
> this to 8.4 because that's as far as was easy.
>
> Here's a jar you can test with:
>
> http://ejurka.com/pgsql/jars/domnn/
>
> Kris Jurka
>

When I looked at the code and obtained the sql that was used to get the
columns IS_NULLABLE it was clear the difference in the methods that the
JDBC and I used in my project to create table definitions. At that time
I was advised either here or in the general forum to use the Information_schema. 
That way it was indicated that the table definition
generation would suffer less from internal database table changes. I
do not know if this is valid for the JDBC, but this is why I sent along
that last piece of information. MyJSQLView though limited was generating
the correct column default result for the salary field.

I will update my local copy and take a look at your changes.

danap.




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

* Re: Bug in DatabaseMetaData.getColumns() with columns based on domains
@ 2010-10-23 15:49  Thomas Kellerer <spam_eater@gmx.net>
  parent: Kris Jurka <books@ejurka.com>
  1 sibling, 0 replies; 8+ messages in thread

From: Thomas Kellerer @ 2010-10-23 15:49 UTC (permalink / raw)
  To: pgsql-jdbc

Kris Jurka wrote on 23.10.2010 09:37:
> Thanks for the hint. Doing the same thing that the information_schema
> does was pretty easy. I've fixed this in CVS and I've also adjusted
> ResultSetMetaData.isNullable to have the same behavior. I've
> backpatched this to 8.4 because that's as far as was easy.
>
> Here's a jar you can test with:
>
> http://ejurka.com/pgsql/jars/domnn/
>

Thanks for the quick reaction. I didn't expect this to be a client-side only problem (including psql ;)
With my little example, the nullable attribute is displayed correctly with your version of the driver.

Could you also look into the values reported for the TYPE_NAME in getColumns() for domains?

I posted a bug reported that the domain's name is not fully qualified so that it's impossible to distinguish between the same domains in different schemas...

Regards
Thomas




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


end of thread, other threads:[~2010-10-23 15:49 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-10-21 12:57 Bug in DatabaseMetaData.getColumns() with columns based on domains Thomas Kellerer <spam_eater@gmx.net>
2010-10-21 17:57 ` dmp <danap@ttc-cmc.net>
2010-10-21 19:54   ` Thomas Kellerer <spam_eater@gmx.net>
2010-10-21 20:20 ` Thomas Kellerer <spam_eater@gmx.net>
2010-10-22 01:25   ` dmp <danap@ttc-cmc.net>
2010-10-23 07:37     ` Kris Jurka <books@ejurka.com>
2010-10-23 15:30       ` dmp <danap@ttc-cmc.net>
2010-10-23 15:49       ` Thomas Kellerer <spam_eater@gmx.net>

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