Thread: [PATCH] NPE in meta data getPrimaryKeys()

[PATCH] NPE in meta data getPrimaryKeys()

From
Anders Hermansen
Date:
Hello,

I'm trying to use jakarta ojb for database access in a web application.
For this I have to generate a object-relational mapping. There is a
tool, reversedb, that will generate this for a live database.

When I run this against my postgresql database I get the following
exception:

java.lang.NullPointerException
    at
org.postgresql.jdbc1.AbstractJdbc1DatabaseMetaData.escapeQuotes(AbstractJdbc1DatabaseMetaData.java:1666)
    at
org.postgresql.jdbc1.AbstractJdbc1DatabaseMetaData.getPrimaryKeys(AbstractJdbc1DatabaseMetaData.java:2899)
    at
org.apache.ojb.tools.mapping.reversedb.DBMeta.read(Unknown Source)
    at
org.apache.ojb.tools.mapping.reversedb.gui.JFrmMainFrame.analyzeSchema(Unknown
Source)
(...)

It fails with a npe because table was set to null. Is setting table to
null for the getPrimaryKeys illegall according to jdbc specification?

As it seems ojb is requesting primary keys for all tables when table is
set to null, and is assuming this will be legal

From line 279 i in DBMeta.read() in ojb source:
rs = this.dbMeta.getPrimaryKeys(null, null, null);

Attached is a patch which makes the getPrimaryKeys method accept the
value of null for the table parameter.

Is this the correct solution?


Anders

--
Anders Hermansen
YoYo Mobile as

Attachment

Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Dave Cramer
Date:
Anders,

Where in the jdbc interface does it say that getTables takes 3 null
parameters?

Dave
On Tue, 2002-12-17 at 11:35, Anders Hermansen wrote:
> Hello,
>
> I'm trying to use jakarta ojb for database access in a web application.
> For this I have to generate a object-relational mapping. There is a
> tool, reversedb, that will generate this for a live database.
>
> When I run this against my postgresql database I get the following
> exception:
>
> java.lang.NullPointerException
>     at
> org.postgresql.jdbc1.AbstractJdbc1DatabaseMetaData.escapeQuotes(AbstractJdbc1DatabaseMetaData.java:1666)
>     at
> org.postgresql.jdbc1.AbstractJdbc1DatabaseMetaData.getPrimaryKeys(AbstractJdbc1DatabaseMetaData.java:2899)
>     at
> org.apache.ojb.tools.mapping.reversedb.DBMeta.read(Unknown Source)
>     at
> org.apache.ojb.tools.mapping.reversedb.gui.JFrmMainFrame.analyzeSchema(Unknown
> Source)
> (...)
>
> It fails with a npe because table was set to null. Is setting table to
> null for the getPrimaryKeys illegall according to jdbc specification?
>
> As it seems ojb is requesting primary keys for all tables when table is
> set to null, and is assuming this will be legal
>
> >From line 279 i in DBMeta.read() in ojb source:
> rs = this.dbMeta.getPrimaryKeys(null, null, null);
>
> Attached is a patch which makes the getPrimaryKeys method accept the
> value of null for the table parameter.
>
> Is this the correct solution?
>
>
> Anders
>
> --
> Anders Hermansen
> YoYo Mobile as
>
> ______________________________________________________________________
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
--
Dave Cramer <Dave@micro-automation.net>


Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Daniel Serodio
Date:
The Javadoc for DatabaseMetaData.getPrimaryKeys(...) clearly states that
catalog may be null, but doesn't mention anything about schema or table
being null. I understand this as meaning they (schema and table) can't
be null.

<quote>
Parameters:
    catalog - a catalog name; "" retrieves those without a catalog; null
means drop catalog name from the selection criteria
    schema - a schema name; "" retrieves those without a schema
    table - a table name
</quote>

On Tue, 2002-12-17 at 14:35, Anders Hermansen wrote:
> Hello,
>
> I'm trying to use jakarta ojb for database access in a web application.
> For this I have to generate a object-relational mapping. There is a
> tool, reversedb, that will generate this for a live database.
>
> When I run this against my postgresql database I get the following
> exception:
>
> java.lang.NullPointerException
>     at
> org.postgresql.jdbc1.AbstractJdbc1DatabaseMetaData.escapeQuotes(AbstractJdbc1DatabaseMetaData.java:1666)
>     at
> org.postgresql.jdbc1.AbstractJdbc1DatabaseMetaData.getPrimaryKeys(AbstractJdbc1DatabaseMetaData.java:2899)
>     at
> org.apache.ojb.tools.mapping.reversedb.DBMeta.read(Unknown Source)
>     at
> org.apache.ojb.tools.mapping.reversedb.gui.JFrmMainFrame.analyzeSchema(Unknown
> Source)
> (...)
>
> It fails with a npe because table was set to null. Is setting table to
> null for the getPrimaryKeys illegall according to jdbc specification?
>
> As it seems ojb is requesting primary keys for all tables when table is
> set to null, and is assuming this will be legal
>
> >From line 279 i in DBMeta.read() in ojb source:
> rs = this.dbMeta.getPrimaryKeys(null, null, null);
>
> Attached is a patch which makes the getPrimaryKeys method accept the
> value of null for the table parameter.
>
> Is this the correct solution?
>
>
> Anders
>
> --
> Anders Hermansen
> YoYo Mobile as
> ----
>

> Index: org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java
> ===================================================================
> RCS file:
/projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java,v
> retrieving revision 1.13
> diff -u -r1.13 AbstractJdbc1DatabaseMetaData.java
> --- org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java    2002/12/11 21:02:58    1.13
> +++ org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java    2002/12/17 16:34:20
> @@ -2903,9 +2903,11 @@
>              " ci.relname AS PK_NAME "+
>              from+
>              " WHERE ct.oid=i.indrelid AND ci.oid=i.indexrelid "+
> -            " AND a.attrelid=ci.oid AND i.indisprimary "+
> -            " AND ct.relname = '"+escapeQuotes(table)+"' "+
> -            where+
> +            " AND a.attrelid=ci.oid AND i.indisprimary ";
> +                        if (table != null && !"".equals(table)) {
> +                    sql += " AND ct.relname = '"+escapeQuotes(table)+"' ";
> +                        }
> +            sql += where+
>              " ORDER BY table_name, pk_name, key_seq";
>          return connection.createStatement().executeQuery(sql);
>      }
> ----
>

>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
--
[]'s
Daniel Serodio


Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Dave Cramer
Date:
That would be my understanding as well

Dave
On Tue, 2002-12-17 at 11:44, Daniel Serodio wrote:
> The Javadoc for DatabaseMetaData.getPrimaryKeys(...) clearly states that
> catalog may be null, but doesn't mention anything about schema or table
> being null. I understand this as meaning they (schema and table) can't
> be null.
>
> <quote>
> Parameters:
>     catalog - a catalog name; "" retrieves those without a catalog; null
> means drop catalog name from the selection criteria
>     schema - a schema name; "" retrieves those without a schema
>     table - a table name
> </quote>
>
> On Tue, 2002-12-17 at 14:35, Anders Hermansen wrote:
> > Hello,
> >
> > I'm trying to use jakarta ojb for database access in a web application.
> > For this I have to generate a object-relational mapping. There is a
> > tool, reversedb, that will generate this for a live database.
> >
> > When I run this against my postgresql database I get the following
> > exception:
> >
> > java.lang.NullPointerException
> >     at
> > org.postgresql.jdbc1.AbstractJdbc1DatabaseMetaData.escapeQuotes(AbstractJdbc1DatabaseMetaData.java:1666)
> >     at
> > org.postgresql.jdbc1.AbstractJdbc1DatabaseMetaData.getPrimaryKeys(AbstractJdbc1DatabaseMetaData.java:2899)
> >     at
> > org.apache.ojb.tools.mapping.reversedb.DBMeta.read(Unknown Source)
> >     at
> > org.apache.ojb.tools.mapping.reversedb.gui.JFrmMainFrame.analyzeSchema(Unknown
> > Source)
> > (...)
> >
> > It fails with a npe because table was set to null. Is setting table to
> > null for the getPrimaryKeys illegall according to jdbc specification?
> >
> > As it seems ojb is requesting primary keys for all tables when table is
> > set to null, and is assuming this will be legal
> >
> > >From line 279 i in DBMeta.read() in ojb source:
> > rs = this.dbMeta.getPrimaryKeys(null, null, null);
> >
> > Attached is a patch which makes the getPrimaryKeys method accept the
> > value of null for the table parameter.
> >
> > Is this the correct solution?
> >
> >
> > Anders
> >
> > --
> > Anders Hermansen
> > YoYo Mobile as
> > ----
> >
>
> > Index: org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java
> > ===================================================================
> > RCS file:
/projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java,v
> > retrieving revision 1.13
> > diff -u -r1.13 AbstractJdbc1DatabaseMetaData.java
> > --- org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java    2002/12/11 21:02:58    1.13
> > +++ org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java    2002/12/17 16:34:20
> > @@ -2903,9 +2903,11 @@
> >              " ci.relname AS PK_NAME "+
> >              from+
> >              " WHERE ct.oid=i.indrelid AND ci.oid=i.indexrelid "+
> > -            " AND a.attrelid=ci.oid AND i.indisprimary "+
> > -            " AND ct.relname = '"+escapeQuotes(table)+"' "+
> > -            where+
> > +            " AND a.attrelid=ci.oid AND i.indisprimary ";
> > +                        if (table != null && !"".equals(table)) {
> > +                    sql += " AND ct.relname = '"+escapeQuotes(table)+"' ";
> > +                        }
> > +            sql += where+
> >              " ORDER BY table_name, pk_name, key_seq";
> >          return connection.createStatement().executeQuery(sql);
> >      }
> > ----
> >
>
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 5: Have you checked our extensive FAQ?
> >
> > http://www.postgresql.org/users-lounge/docs/faq.html
--
Dave Cramer <Dave@micro-automation.net>


Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Anders Hermansen
Date:
* Dave Cramer (Dave@micro-automation.net) wrote:
> Where in the jdbc interface does it say that getTables takes 3 null
> parameters?

I can't see it does. I will therefore prepare a bug report for jakarta
ojb.

Dave: I'm sorry that this got sent to your email directly as well. My
mistake.


Anders

--
Anders Hermansen
YoYo Mobile as

Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Anders Hermansen
Date:
* Daniel Serodio (daniel@checkforte.com.br) wrote:
> The Javadoc for DatabaseMetaData.getPrimaryKeys(...) clearly states that
> catalog may be null, but doesn't mention anything about schema or table
> being null. I understand this as meaning they (schema and table) can't
> be null.

Maybe you have an old javadoc? In my (1.4.1) it specifies that schema
can be null as well. But it does not mention that table can be null. This is
probably a bug in ojb then.

<quote>
Parameters:
    catalog - a catalog name; must match the catalog name as it is stored in the database; "" retrieves those without a
catalog;null means that the catalog name should not be used to narrow the search 
    schema - a schema name; must match the schema name as it is stored in the database; "" retrieves those without a
schema;null means that the schema name should not be used to narrow the search 
    table - a table name; must match the table name as it is stored in the database
</quote>


Anders

--
Anders Hermansen
YoYo Mobile as

Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Daniel Serodio
Date:
On Tue, 2002-12-17 at 15:08, Anders Hermansen wrote:
> * Daniel Serodio (daniel@checkforte.com.br) wrote:
> > The Javadoc for DatabaseMetaData.getPrimaryKeys(...) clearly states that
> > catalog may be null, but doesn't mention anything about schema or table
> > being null. I understand this as meaning they (schema and table) can't
> > be null.
>
> Maybe you have an old javadoc? In my (1.4.1) it specifies that schema
> can be null as well. But it does not mention that table can be null. This is
> probably a bug in ojb then.

Old Javadoc indeed. I'm using 1.3.1. I can't recall right now, is the
JDBC in Java 1.3.1 is JDBC2? Maybe the app (reversedb) should try to
find out if it's talking to JDBC2 or JDBC3, then use table=null or not
accordingly. Is this viable?

> <quote>
> Parameters:
>     catalog - a catalog name; must match the catalog name as it is stored in the database; "" retrieves those without
acatalog; null means that the catalog name should not be used to narrow the search 
>     schema - a schema name; must match the schema name as it is stored in the database; "" retrieves those without a
schema;null means that the schema name should not be used to narrow the search 
>     table - a table name; must match the table name as it is stored in the database
> </quote>
>
>
> Anders
>
> --
> Anders Hermansen
> YoYo Mobile as
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
--
[]'s
Daniel Serodio


Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Anders Hermansen
Date:
* Daniel Serodio (daniel@checkforte.com.br) wrote:
> Old Javadoc indeed. I'm using 1.3.1. I can't recall right now, is the
> JDBC in Java 1.3.1 is JDBC2? Maybe the app (reversedb) should try to
> find out if it's talking to JDBC2 or JDBC3, then use table=null or not
> accordingly. Is this viable?

At what we have found out here, it should not use table=null in either
case. Right?

But this "extension" (table=null) must work with some database drivers
since it is in OJB. Maybe it is an "extension" that postgresql driver
should also have?

If this "extension" is not available then reversedb must iterate through
every table and do very many getPrimeryKeys, this will probably need a
lot more computer power?


Anders

--
Anders Hermansen
YoYo Mobile as

Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Daniel Serodio
Date:
On Tue, 2002-12-17 at 15:36, Anders Hermansen wrote:
> * Daniel Serodio (daniel@checkforte.com.br) wrote:
> > Old Javadoc indeed. I'm using 1.3.1. I can't recall right now, is the
> > JDBC in Java 1.3.1 is JDBC2? Maybe the app (reversedb) should try to
> > find out if it's talking to JDBC2 or JDBC3, then use table=null or not
> > accordingly. Is this viable?
>
> At what we have found out here, it should not use table=null in either
> case. Right?

Right. Sorry, I meant to say schema=null

> But this "extension" (table=null) must work with some database drivers
> since it is in OJB. Maybe it is an "extension" that postgresql driver
> should also have?
>
> If this "extension" is not available then reversedb must iterate through
> every table and do very many getPrimeryKeys, this will probably need a
> lot more computer power?

If I understand this correctly, if we were to implement this
functionality in the PostgreSQL driver, the driver would have to iterate
thru every table, so the only difference is where the iteration is done
(reversedb X postgresql driver).

> Anders
>
> --
> Anders Hermansen
> YoYo Mobile as

--
[]'s
Daniel Serodio


Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Dave Cramer
Date:
Have you tried getPrimaryKeys(null, "", "%");

This should give you all the primaryKeys

Dave
On Tue, 2002-12-17 at 12:44, Daniel Serodio wrote:
> On Tue, 2002-12-17 at 15:36, Anders Hermansen wrote:
> > * Daniel Serodio (daniel@checkforte.com.br) wrote:
> > > Old Javadoc indeed. I'm using 1.3.1. I can't recall right now, is the
> > > JDBC in Java 1.3.1 is JDBC2? Maybe the app (reversedb) should try to
> > > find out if it's talking to JDBC2 or JDBC3, then use table=null or not
> > > accordingly. Is this viable?
> >
> > At what we have found out here, it should not use table=null in either
> > case. Right?
>
> Right. Sorry, I meant to say schema=null
>
> > But this "extension" (table=null) must work with some database drivers
> > since it is in OJB. Maybe it is an "extension" that postgresql driver
> > should also have?
> >
> > If this "extension" is not available then reversedb must iterate through
> > every table and do very many getPrimeryKeys, this will probably need a
> > lot more computer power?
>
> If I understand this correctly, if we were to implement this
> functionality in the PostgreSQL driver, the driver would have to iterate
> thru every table, so the only difference is where the iteration is done
> (reversedb X postgresql driver).
>
> > Anders
> >
> > --
> > Anders Hermansen
> > YoYo Mobile as
--
Dave Cramer <Dave@micro-automation.net>


Re: [PATCH] NPE in meta data getPrimaryKeys()

From
Dave Cramer
Date:
Actually, I had a look at the spec more carefully and what i wrote
earlier will not work nor should it

in the jdk 1.4 docs catalog, and schema can be null, but table can't be
null, it has to match exactly, so it is true, either the driver iterates
through all the tables, or the user and since the spec indicates that
the user should, then  I think the driver is correctly implemented

Dave
On Tue, 2002-12-17 at 13:04, Dave Cramer wrote:
> Have you tried getPrimaryKeys(null, "", "%");
>
> This should give you all the primaryKeys
>
> Dave
> On Tue, 2002-12-17 at 12:44, Daniel Serodio wrote:
> > On Tue, 2002-12-17 at 15:36, Anders Hermansen wrote:
> > > * Daniel Serodio (daniel@checkforte.com.br) wrote:
> > > > Old Javadoc indeed. I'm using 1.3.1. I can't recall right now, is the
> > > > JDBC in Java 1.3.1 is JDBC2? Maybe the app (reversedb) should try to
> > > > find out if it's talking to JDBC2 or JDBC3, then use table=null or not
> > > > accordingly. Is this viable?
> > >
> > > At what we have found out here, it should not use table=null in either
> > > case. Right?
> >
> > Right. Sorry, I meant to say schema=null
> >
> > > But this "extension" (table=null) must work with some database drivers
> > > since it is in OJB. Maybe it is an "extension" that postgresql driver
> > > should also have?
> > >
> > > If this "extension" is not available then reversedb must iterate through
> > > every table and do very many getPrimeryKeys, this will probably need a
> > > lot more computer power?
> >
> > If I understand this correctly, if we were to implement this
> > functionality in the PostgreSQL driver, the driver would have to iterate
> > thru every table, so the only difference is where the iteration is done
> > (reversedb X postgresql driver).
> >
> > > Anders
> > >
> > > --
> > > Anders Hermansen
> > > YoYo Mobile as
--
Dave Cramer <Dave@micro-automation.net>