pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: John Sidney-Woollett <johnsw@wardbrook.com>
To: henr-and@dsv.su.se
Cc: pgsql-jdbc@postgresql.org
Subject: Re: getGeneratedKeys()
Date: Wed, 31 Dec 2003 07:53:20 -0000 (GMT)
Message-ID: <2491.192.168.0.64.1072857200.squirrel@mercury.wardbrook.com> (raw)
In-Reply-To: <33309.213.89.23.209.1072826327.squirrel@www.dsv.su.se>
References: <3FF1C0EE.4010107@dsv.su.se>
	<Pine.LNX.4.33.0312301523060.5331-100000@leary.csoft.net>
	<32899.213.89.23.209.1072821322.squirrel@www.dsv.su.se>
	<1216.192.168.0.64.1072824461.squirrel@mercury.wardbrook.com>
	<33309.213.89.23.209.1072826327.squirrel@www.dsv.su.se>

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
>
>
>




view thread (17+ messages)  latest in thread

Message-ID: <2491.192.168.0.64.1072857200.squirrel@mercury.wardbrook.com>
Permalink:  ../2491.192.168.0.64.1072857200.squirrel@mercury.wardbrook.com/
Also on:    postgresql.org/message-id/2491.192.168.0.64.1072857200.squirrel@mercury.wardbrook.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: johnsw@wardbrook.com, henr-and@dsv.su.se
  Subject: Re: getGeneratedKeys()
  In-Reply-To: <2491.192.168.0.64.1072857200.squirrel@mercury.wardbrook.com>
  References: <3FF1C0EE.4010107@dsv.su.se> <Pine.LNX.4.33.0312301523060.5331-100000@leary.csoft.net> <32899.213.89.23.209.1072821322.squirrel@www.dsv.su.se> <1216.192.168.0.64.1072824461.squirrel@mercury.wardbrook.com> <33309.213.89.23.209.1072826327.squirrel@www.dsv.su.se> <2491.192.168.0.64.1072857200.squirrel@mercury.wardbrook.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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