X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 2BE53D1B43D for ; Wed, 31 Dec 2003 07:58:15 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 49022-06 for ; Wed, 31 Dec 2003 03:57:26 -0400 (AST) Received: from mercury.wardbrook.com (217.79.104.122.adsl.griffin.net.uk [217.79.104.122]) by svr1.postgresql.org (Postfix) with ESMTP id 9336ED1B436 for ; Wed, 31 Dec 2003 03:57:24 -0400 (AST) Received: from localhost (mercury.wardbrook.com [127.0.0.1]) by mercury.wardbrook.com (Postfix) with ESMTP id CF607532EB; Wed, 31 Dec 2003 07:53:23 +0000 (GMT) Received: by mercury.wardbrook.com (Postfix, from userid 505) id 7025E532ED; Wed, 31 Dec 2003 07:53:22 +0000 (GMT) Received: from 192.168.1.219 (mercury.wardbrook.com [127.0.0.1]) by mercury.wardbrook.com (Postfix) with SMTP id A1CFC532EB; Wed, 31 Dec 2003 07:53:20 +0000 (GMT) Received: from 192.168.0.64 (SquirrelMail authenticated user johnsw) by mercury.wardbrook.com with HTTP; Wed, 31 Dec 2003 07:53:20 -0000 (GMT) Message-ID: <2491.192.168.0.64.1072857200.squirrel@mercury.wardbrook.com> In-Reply-To: <33309.213.89.23.209.1072826327.squirrel@www.dsv.su.se> References: <3FF1C0EE.4010107@dsv.su.se> <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> Date: Wed, 31 Dec 2003 07:53:20 -0000 (GMT) Subject: Re: getGeneratedKeys() From: "John Sidney-Woollett" To: henr-and@dsv.su.se Cc: pgsql-jdbc@postgresql.org Reply-To: johnsw@wardbrook.com User-Agent: SquirrelMail/1.4.2 [CVS] MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 Importance: Normal X-Virus-Scanned: by AMaViS 0.3.12 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/204 X-Sequence-Number: 8940 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 > > >