pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedgetGeneratedKeys()
17+ messages / 9 participants
[nested] [flat]
* getGeneratedKeys()
@ 2003-03-05 21:19 Dennis R. Gesker <dennis@gesker.com>
0 siblings, 1 reply; 17+ messages in thread
From: Dennis R. Gesker @ 2003-03-05 21:19 UTC (permalink / raw)
To: pgsql-jdbc
Is getGeneratedKeys implemented in the JDBC3 driver (stable)?
I'm getting class load errors when I try to use the driver along with
the unknown-key and enity-command features of JBoss 3.2RC3.
Dennis
--
Dennis Roman Gesker
dennis@gesker.com
http://www.gesker.com
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-03-07 20:09 Barry Lind <blind@xythos.com>
parent: Dennis R. Gesker <dennis@gesker.com>
0 siblings, 0 replies; 17+ messages in thread
From: Barry Lind @ 2003-03-07 20:09 UTC (permalink / raw)
To: Dennis R. Gesker <dennis@gesker.com>; +Cc: pgsql-jdbc
Dennis,
It is stable in the sense that there are no known bugs. But I would
download the latest jar files from the jdbc.postgresql.org website to
get the latest fixes. If you are still having problems please report
what they are and we can get them resolved.
thanks,
--Barry
Dennis R. Gesker wrote:
> Is getGeneratedKeys implemented in the JDBC3 driver (stable)?
>
> I'm getting class load errors when I try to use the driver along with
> the unknown-key and enity-command features of JBoss 3.2RC3.
>
> Dennis
>
^ permalink raw reply [nested|flat] 17+ messages in thread
* getGeneratedKeys()
@ 2003-12-30 18:16 Henrik Andersson <henr-and@dsv.su.se>
0 siblings, 1 reply; 17+ messages in thread
From: Henrik Andersson @ 2003-12-30 18:16 UTC (permalink / raw)
To: pgsql-jdbc
Is getGeneratedKeys() implemented? I am using the package in Debian
Unstable but I am getting: "org.postgresql.util.PSQLException: This
method is not yet implemented."
--
Henrik
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-30 20:30 Kris Jurka <books@ejurka.com>
parent: Henrik Andersson <henr-and@dsv.su.se>
0 siblings, 1 reply; 17+ messages in thread
From: Kris Jurka @ 2003-12-30 20:30 UTC (permalink / raw)
To: Henrik Andersson <henr-and@dsv.su.se>; +Cc: pgsql-jdbc
On Tue, 30 Dec 2003, Henrik Andersson wrote:
> Is getGeneratedKeys() implemented? I am using the package in Debian
> Unstable but I am getting: "org.postgresql.util.PSQLException: This
> method is not yet implemented."
This method has not been implemented and most likely will not be
implemented anytime soon. First it is unclear to me what it means by
auto-generated keys. Does it mean just things like serial or anything
that a before insert trigger does to modify the row? Secondly what comes
back in the ResultSet? Just the generated keys? How is that useful
without anything to tie it to? Finally the real problem is that the
backend has no way of reporting this information to us. Possibly for a
single insert we could do a getLastOID() and assume it was unique in the
table to look it up, but this is obviously fragile in the presence of
duplicate oids, tables without oids, anything other than insert
statements, and statements that operate on more than one row.
Kris Jurka
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-30 21:55 henr-and@dsv.su.se
parent: Kris Jurka <books@ejurka.com>
0 siblings, 2 replies; 17+ messages in thread
From: henr-and@dsv.su.se @ 2003-12-30 21:55 UTC (permalink / raw)
To: books@ejurka.com; +Cc: pgsql-jdbc
> This method has not been implemented and most likely will not be
> implemented anytime soon. First it is unclear to me what it means by
> auto-generated keys. Does it mean just things like serial or anything
> that a before insert trigger does to modify the row?
In my case it's a serial. I want to get the id-number that my insert
generates.
MySQL's JDBC-driver has implemented the method.
--
Henrik
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-30 22:47 John Sidney-Woollett <johnsw@wardbrook.com>
parent: henr-and@dsv.su.se
1 sibling, 1 reply; 17+ messages in thread
From: John Sidney-Woollett @ 2003-12-30 22:47 UTC (permalink / raw)
To: henr-and@dsv.su.se; +Cc: books@ejurka.com; pgsql-jdbc
Can you do the insert using a function instead, and return the currval on
the underlying sequence?
John
henr-and@dsv.su.se said:
>
>> This method has not been implemented and most likely will not be
>> implemented anytime soon. First it is unclear to me what it means by
>> auto-generated keys. Does it mean just things like serial or anything
>> that a before insert trigger does to modify the row?
>
> In my case it's a serial. I want to get the id-number that my insert
> generates.
> MySQL's JDBC-driver has implemented the method.
>
> --
> Henrik
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-30 23:18 henr-and@dsv.su.se
parent: John Sidney-Woollett <johnsw@wardbrook.com>
0 siblings, 4 replies; 17+ messages in thread
From: henr-and@dsv.su.se @ 2003-12-30 23:18 UTC (permalink / raw)
To: johnsw@wardbrook.com; +Cc: pgsql-jdbc
> Can you do the insert using a function instead, and return the currval
> on the underlying sequence?
>
> John
>
I'm not sure what you mean. A stored procedure?
The good thing about getGeneratedKeys() is that you don't have to write
DB-specific code.
--
Henrik
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-30 23:28 Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>
parent: henr-and@dsv.su.se
3 siblings, 0 replies; 17+ messages in thread
From: Marcus Andree S. Magalhaes @ 2003-12-30 23:28 UTC (permalink / raw)
To: henr-and@dsv.su.se; +Cc: johnsw@wardbrook.com; pgsql-jdbc
Guys,
I am too dumb or a simple currval() would be useful?
We solved this by sending two queries in JDBC. The first one inserts
a single record in the db. The other issues a currval on the desired
sequence.
There was a posting a few weeks ago, describing a clever method,
issuing both queries on the same connection by using the ";" operator
to separate the queries.
hope this helps.
>> Can you do the insert using a function instead, and return the currval
>> on the underlying sequence?
>>
>> John
>>
>
> I'm not sure what you mean. A stored procedure?
>
> The good thing about getGeneratedKeys() is that you don't have to write
> DB-specific code.
> --
> Henrik
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-30 23:56 Paul Thomas <paul@tmsl.demon.co.uk>
parent: henr-and@dsv.su.se
3 siblings, 1 reply; 17+ messages in thread
From: Paul Thomas @ 2003-12-30 23:56 UTC (permalink / raw)
To: pgsql-jdbc
On 30/12/2003 23:18 henr-and@dsv.su.se wrote:
> > Can you do the insert using a function instead, and return the currval
> > on the underlying sequence?
> >
> > John
> >
>
> I'm not sure what you mean. A stored procedure?
>
> The good thing about getGeneratedKeys() is that you don't have to write
> DB-specific code.
I always thought that auto-generated keys _are_ db specific. Where are
they defined in SQL92/99?
Back to your problem: Does your app have to support different DBMSs or is
it PostgreSQL-only?
--
Paul Thomas
+------------------------------+---------------------------------------------+
| Thomas Micro Systems Limited | Software Solutions for the Smaller
Business |
| Computer Consultants |
http://www.thomas-micro-systems-ltd.co.uk |
+------------------------------+---------------------------------------------+
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-31 00:08 henr-and@dsv.su.se
parent: Paul Thomas <paul@tmsl.demon.co.uk>
0 siblings, 0 replies; 17+ messages in thread
From: henr-and@dsv.su.se @ 2003-12-31 00:08 UTC (permalink / raw)
To: pgsql-jdbc
> I always thought that auto-generated keys _are_ db specific. Where are
> they defined in SQL92/99?
>
> Back to your problem: Does your app have to support different DBMSs or
> is it PostgreSQL-only?
>
Different DBMSs.
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-31 02:38 Kris Jurka <books@ejurka.com>
parent: henr-and@dsv.su.se
1 sibling, 0 replies; 17+ messages in thread
From: Kris Jurka @ 2003-12-31 02:38 UTC (permalink / raw)
To: henr-and@dsv.su.se; +Cc: pgsql-jdbc
On Tue, 30 Dec 2003 henr-and@dsv.su.se wrote:
>
> > This method has not been implemented and most likely will not be
> > implemented anytime soon. First it is unclear to me what it means by
> > auto-generated keys. Does it mean just things like serial or anything
> > that a before insert trigger does to modify the row?
>
> In my case it's a serial. I want to get the id-number that my insert
> generates.
> MySQL's JDBC-driver has implemented the method.
>
They have implemented the method, but they have not answered any of the
questions I have raised. Their getGeneratedKeys call just wraps
getLastInsertID(). This would be the equivalent of us providing a currval
like function that didn't take a sequence argument. It is largely useless
in the face of after insert triggers calling another sequence's nextval
and tables with multiple serial columns. This doesn't even get into the
before trigger case.
Also with mysql's timestamp type auto-updating itself, shouldn't that be
returned as well?
Does anyone know of any documention other than the API javadoc? The
DatabaseMetaData functions that return ResultSets list the columns and
their contents, but this Statement function does no such thing.
Kris Jurka
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-31 07:53 John Sidney-Woollett <johnsw@wardbrook.com>
parent: henr-and@dsv.su.se
3 siblings, 0 replies; 17+ messages in thread
From: John Sidney-Woollett @ 2003-12-31 07:53 UTC (permalink / raw)
To: henr-and@dsv.su.se; +Cc: pgsql-jdbc
If you want a database neutral solution, then create a function to insert
the record. Have the function returns the ID (PK) of the record it just
inserted.
Here's an example:
CREATE OR REPLACE FUNCTION WEBDAV.CreateFolder (integer, varchar) RETURNS
integer AS '
-- creates a new folder (if it does not exist)
-- and returns the WDResource ID of the newly
-- created folder. If the folder already exists
-- or could not be created then -1 is returned
DECLARE
pFolderID ALIAS FOR $1;
pFolderName ALIAS FOR $2;
vUserID integer;
vCount integer;
vURL varchar(255);
BEGIN
-- get the parent folder information
select WDUserID, URL into vUserID, vURL
from WEBDAV.WDResource
where WDResourceID = pFolderID;
if vUserID is null then
return -1;
end if;
-- check that the folder does not exist
select count(*) into vCount from WEBDAV.WDResource where URL = vURL
||''/''|| pFolderName;
if (vCount > 0) then
return -1;
end if;
-- create the new folder resource
insert into WEBDAV.WDResource (WDPARENTID, WDUSERID, WDRESTYPEID, URL,
FILENAME, LASTMODIFIED)
values (pFolderID, vUserID, 1, vURL||''/''||pFolderName, pFolderName,
now());
return currval(''WEBDAV.SEQ_WDResource'');
END;
' LANGUAGE 'plpgsql';
The important things to note are that this function can be written for
Postgres, Oracle or any other RDBMS - you only change the internals of the
function. The function returns either -1 if no record is created, or the
PK value of the newly inserted record.
Does that help you?
John
henr-and@dsv.su.se said:
>> Can you do the insert using a function instead, and return the currval
>> on the underlying sequence?
>>
>> John
>>
>
> I'm not sure what you mean. A stored procedure?
>
> The good thing about getGeneratedKeys() is that you don't have to write
> DB-specific code.
> --
> Henrik
>
>
>
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys()
@ 2003-12-31 08:19 Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>
parent: henr-and@dsv.su.se
3 siblings, 0 replies; 17+ messages in thread
From: Marcus Andree S. Magalhaes @ 2003-12-31 08:19 UTC (permalink / raw)
To: henr-and@dsv.su.se; +Cc: johnsw@wardbrook.com; pgsql-jdbc
Ahhh.... yes.... good and old "multiplatform code compatibility"...
I remember that from '95, when version Beta 2.0 arrived and I started
to write Java code in applets...
There was a cool hack around to use Netscape to compile code and
generate a class file under Linux 1.2...
The promiss of "write once, run everywhere". Frames used AWT back
then and all have the same cup of cofee when iconized. After all,
the mac wasn;t able to iconize frames. Well, some flexibility was
trade off in the name of compatibility. Fine...
Then, we had to deal with threading issues in Solaris and Windows...
After that, JDBC arrived... True, multiplatform db access.
A query as complex as "select gettime()" could run in SQL Server
and Postgres (or it was called PostgreSQL at that time ?)
and return the same kind of data: a timestamp-like data in SQL Server
and an error in Postgres.
Since then, I never believed in platform independent coding. And it
was a long time ago....
>> Can you do the insert using a function instead, and return the currval
>> on the underlying sequence?
>>
>> John
>>
>
> I'm not sure what you mean. A stored procedure?
>
> The good thing about getGeneratedKeys() is that you don't have to write
> DB-specific code.
> --
> Henrik
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
^ permalink raw reply [nested|flat] 17+ messages in thread
* getGeneratedKeys
@ 2009-03-09 19:20 Saleem EDAH-TALLY <nmset@netcourrier.com>
0 siblings, 1 reply; 17+ messages in thread
From: Saleem EDAH-TALLY @ 2009-03-09 19:20 UTC (permalink / raw)
To: pgsql-jdbc
Hello,
This is just a request in the hope it may help not all but 95% of devs. The
other 5% would know by notice that they have to write their own solution; for
now, 100% of devs have to write their own.
I have followed
http://archives.postgresql.org//pgsql-jdbc/2004-09/msg00191.php
and
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&safe=off&threadm=Pin...
and I understand quite well that currval is useless with multiple inserted
rows, and the one inserted row situation with two colums having the same
sequence as default values cannot rely on currval. I consider these situations
to be in those 5% or less where the devs know what they are doing.
Sequences appended to columns as defaults can easily be retrieved
"SELECT column_default FROM information_schema.columns WHERE table_name = '" +
tblName + "' AND column_default LIKE 'nextval(''%_%_seq''::regclass)'"
The driver may parse the results to get the sequence(s) name(s) and hence
querying again to retrieve the currval(s), returning a resultset with most
often one column and more if there are more than one column with a sequence as
default value. If the same same sequence is found more than one time as
default value, then the driver may generate an error as 'not implemented'.
Of course multiple inserted rows cannot be managed at all.
Concerning having more than one same sequence as default values in a table is
quite an odd situation. Serious devs won't do that.
A table could yet have more than column with a sequence as default, and the
returned generated keys could all be forwarded by the driver if the sequences
are all different.
So I think the driver may provide getGeneratedKeys in a realistic approach,
and generating errors for these very rare odd situations.
Thank you for considering.
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys
@ 2009-03-09 21:38 Kris Jurka <books@ejurka.com>
parent: Saleem EDAH-TALLY <nmset@netcourrier.com>
0 siblings, 1 reply; 17+ messages in thread
From: Kris Jurka @ 2009-03-09 21:38 UTC (permalink / raw)
To: Saleem EDAH-TALLY <nmset@netcourrier.com>; +Cc: pgsql-jdbc
On Mon, 9 Mar 2009, Saleem EDAH-TALLY wrote:
> This is just a request in the hope it may help not all but 95% of devs. The
> other 5% would know by notice that they have to write their own solution; for
> now, 100% of devs have to write their own.
>
> I have followed
> http://archives.postgresql.org//pgsql-jdbc/2004-09/msg00191.php
> and
> http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&safe=off&threadm=Pin...
Those are pretty old, see:
http://archives.postgresql.org/pgsql-jdbc/2008-11/msg00041.php
http://archives.postgresql.org/pgsql-jdbc/2009-01/msg00015.php
The upcoming 8.4 driver will contain support for getGeneratedKeys. If you
want to test this out yourself, download the code from CVS or a prebuilt
jar from the link in the second message.
Kris Jurka
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys
@ 2009-03-09 22:26 Saleem EDAH-TALLY <nmset@netcourrier.com>
parent: Kris Jurka <books@ejurka.com>
0 siblings, 1 reply; 17+ messages in thread
From: Saleem EDAH-TALLY @ 2009-03-09 22:26 UTC (permalink / raw)
To: pgsql-jdbc
I've tried for single and multiple inserts and it does work.
I don't know what would be returned if a table has more than one sequence as
default value for columns, I don't have need for this so I won't try.
Just for curiosity, does it work by driver implementation solely or are there
changes in PostgreSQL backend that help the implementation ?
Thanks.
>
> Those are pretty old, see:
>
> http://archives.postgresql.org/pgsql-jdbc/2008-11/msg00041.php
>
> http://archives.postgresql.org/pgsql-jdbc/2009-01/msg00015.php
>
> The upcoming 8.4 driver will contain support for getGeneratedKeys. If you
> want to test this out yourself, download the code from CVS or a prebuilt
> jar from the link in the second message.
>
> Kris Jurka
^ permalink raw reply [nested|flat] 17+ messages in thread
* Re: getGeneratedKeys
@ 2009-03-09 22:39 Kris Jurka <books@ejurka.com>
parent: Saleem EDAH-TALLY <nmset@netcourrier.com>
0 siblings, 0 replies; 17+ messages in thread
From: Kris Jurka @ 2009-03-09 22:39 UTC (permalink / raw)
To: Saleem EDAH-TALLY <nmset@netcourrier.com>; +Cc: pgsql-jdbc
On Mon, 9 Mar 2009, Saleem EDAH-TALLY wrote:
> I've tried for single and multiple inserts and it does work.
> I don't know what would be returned if a table has more than one sequence as
> default value for columns, I don't have need for this so I won't try.
>
> Just for curiosity, does it work by driver implementation solely or are there
> changes in PostgreSQL backend that help the implementation ?
The 8.2 server release included support for the RETURNING clause for
inserts, updates, and deletes which allow the server to return the values
generated.
http://www.postgresql.org/docs/8.3/static/sql-insert.html
Kris Jurka
^ permalink raw reply [nested|flat] 17+ messages in thread
end of thread, other threads:[~2009-03-09 22:39 UTC | newest]
Thread overview: 17+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-03-05 21:19 getGeneratedKeys() Dennis R. Gesker <dennis@gesker.com>
2003-03-07 20:09 ` Barry Lind <blind@xythos.com>
2003-12-30 18:16 getGeneratedKeys() Henrik Andersson <henr-and@dsv.su.se>
2003-12-30 20:30 ` Kris Jurka <books@ejurka.com>
2003-12-30 21:55 ` henr-and@dsv.su.se
2003-12-30 22:47 ` John Sidney-Woollett <johnsw@wardbrook.com>
2003-12-30 23:18 ` henr-and@dsv.su.se
2003-12-30 23:28 ` Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>
2003-12-30 23:56 ` Paul Thomas <paul@tmsl.demon.co.uk>
2003-12-31 00:08 ` henr-and@dsv.su.se
2003-12-31 07:53 ` John Sidney-Woollett <johnsw@wardbrook.com>
2003-12-31 08:19 ` Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>
2003-12-31 02:38 ` Kris Jurka <books@ejurka.com>
2009-03-09 19:20 getGeneratedKeys Saleem EDAH-TALLY <nmset@netcourrier.com>
2009-03-09 21:38 ` Re: getGeneratedKeys Kris Jurka <books@ejurka.com>
2009-03-09 22:26 ` Re: getGeneratedKeys Saleem EDAH-TALLY <nmset@netcourrier.com>
2009-03-09 22:39 ` Re: getGeneratedKeys Kris Jurka <books@ejurka.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