X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 727639DC98F for ; Fri, 6 Jan 2006 15:19:08 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 82272-03 for ; Fri, 6 Jan 2006 15:19:08 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id 5BB7B9DC959 for ; Fri, 6 Jan 2006 15:19:02 -0400 (AST) Received: (qmail 19689 invoked from network); 6 Jan 2006 19:19:03 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.2?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 6 Jan 2006 19:19:03 -0000 In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v746.2) Content-Type: multipart/alternative; boundary=Apple-Mail-1--645848177 Message-Id: <5011BEE9-5961-41D9-938F-15A49DC50658@fastcrypt.com> Cc: Oliver Jowett , pgsql-jdbc@postgresql.org From: Dave Cramer Subject: Re: Out Parameter Support Date: Fri, 6 Jan 2006 14:19:01 -0500 To: Kyle R Morse/Eden X-Mailer: Apple Mail (2.746.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.128 required=5 tests=[AWL=0.127, HTML_MESSAGE=0.001, UPPERCASE_25_50=0] X-Spam-Score: 0.128 X-Spam-Level: X-Archive-Number: 200601/27 X-Sequence-Number: 14478 --Apple-Mail-1--645848177 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On 6-Jan-06, at 9:55 AM, Kyle R Morse/Eden wrote: > > Thanks for your reply. > It is 'sort of' supported. I guess I need to be more specific. > Here is an example of one of the stored functions: > > CREATE OR REPLACE FUNCTION EDEN_ITPP.INSERT_COMPANY(INCOMPANY_NAME > IN VARCHAR, > INPARENT_COMPANY_ID IN > INTEGER, > INPHONE IN VARCHAR, > INFAX IN VARCHAR, > NEWID OUT INTEGER) AS $$ > DECLARE > TEMPNEWID INTEGER; > BEGIN > SELECT MAX(ID) INTO TEMPNEWID FROM COMPANY; > TEMPNEWID := TEMPNEWID + 1; > IF TEMPNEWID IS NULL THEN > TEMPNEWID := 1; > END IF; > INSERT INTO COMPANY > (ID,COMPANY_NAME,PARENT_COMPANY_ID,PHONE,FAX,ENABLED) > VALUES > (TEMPNEWID,INCOMPANY_NAME,INPARENT_COMPANY_ID,INPHONE,INFAX,1); > NEWID := TEMPNEWID; > END; > $$ LANGUAGE plpgsql; > > This function basically inserts a new company in the company table > and returns the new company's id in the out parameter. > This is the definition for the company table it is inserting on: > > CREATE TABLE EDEN_ITPP.COMPANY ( > ID INTEGER NOT NULL, > COMPANY_NAME VARCHAR(55) NOT NULL, > PARENT_COMPANY_ID INTEGER, > PHONE VARCHAR(30), > FAX VARCHAR(30), > ENABLED SMALLINT DEFAULT 1 NOT NULL ); > > -- DDL Statements for primary key on Table COMPANY > CREATE UNIQUE INDEX company_pkey ON EDEN_ITPP.COMPANY(ID); > > > This works fine from JDBC if I do this: > > PreparedStatement p = conn.prepareCall("? = call > EDEN_ITPP.INSERT_COMPANY(?, ?, ?, ?)"); > p.registerOutParameter(1, Types.INTEGER); > p.setString(2, "NewCompany"); > p.setInt(3, 0); > p.setString(4, "555-555-5555"); > p.setString(5, "555-555-5556"); > p.execute(); > System.out.println(p.getInt(1)); > > It returns the new company's ID as expected. > > However, the query is not constructed using the posgre syntax, it > uses the standard JDBC stored procedure syntax, so as to be > compatible with our other DBs > (Right now it runs on DB2, SQL Server, Oracle, and MySQL, all of > which work with this query): This is not specific to postgres, this is the jdbc spec. I'd have to look into why it isn't working as expected, however you have worse problems using max(id) to get a unique number in an mvcc database such as postgresql or oracle. Dave > > PreparedStatement p = conn.prepareCall("call > EDEN_ITPP.INSERT_COMPANY(?,?, ?, ?, ?)"); > p.setString(1, "NewCompany"); > p.setInt(2, 0); > p.setString(3, "555-555-5555"); > p.setString(4, "555-555-5556"); > p.registerOutParameter(5, Types.INTEGER); > p.execute(); > System.out.println(p.getInt(5)); > > But Postgre barfs at this. > The exception thrown is: > PSQLException: A CallableStatement function was executed and the > return was of type java.sql.Types=4 however type java.sql.Types=0 > was registered. > > I am using Server 8.1.1 and JDBC driver 8.1Build 404. > Does anyone know if there is any plan to support out parameters for > stored functions in PostgreSQL in the future? > > (See the Note regarding stored procedures and out parameter support) > http://jdbc.postgresql.org/documentation/81/callproc.html > > Thanks for your help, > Kyle > > --Apple-Mail-1--645848177 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1

On 6-Jan-06, = at 9:55 AM, Kyle R Morse/Eden wrote:


Thanks for your reply.
It is 'sort of' supported. =A0I guess I = need to be more specific. =A0Here is an example of one of the stored = functions:

CREATE = OR REPLACE FUNCTION EDEN_ITPP.INSERT_COMPANY(INCOMPANY_NAME IN = VARCHAR,
=A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = INPARENT_COMPANY_ID IN INTEGER,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 INPHONE IN VARCHAR,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 INFAX IN VARCHAR, =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 NEWID OUT INTEGER) = AS $$
=A0 =A0 =A0 =A0 = DECLARE
=A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 TEMPNEWID INTEGER;
=A0 =A0 =A0 =A0 BEGIN
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0SELECT = MAX(ID) INTO TEMPNEWID FROM COMPANY;
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0TEMPNEWID :=3D = TEMPNEWID + 1;
=A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0IF TEMPNEWID IS NULL THEN
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 TEMPNEWID :=3D 1;
=A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 END IF;
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0INSERT INTO = COMPANY(ID,COMPANY_NAME,PARENT_COMPANY_ID,PHONE,FAX,ENABLED) =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 = =A0VALUES(TEMPNEWID,INCOMPANY_NAME,INPARENT_COMPANY_ID,INPHONE,INFAX,1);
=A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0NEWID :=3D TEMPNEWID;
=A0 =A0 =A0 =A0 END;
$$ LANGUAGE plpgsql;

This function basically inserts a new company in the = company table and returns the new company's id in the out = parameter.
This is the = definition for the company table it is inserting on:
=
=A0CREATE TABLE = EDEN_ITPP.COMPANY =A0(
=A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ID INTEGER NOT NULL,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = COMPANY_NAME VARCHAR(55) NOT NULL,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = PARENT_COMPANY_ID INTEGER,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 PHONE = VARCHAR(30),
=A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 FAX VARCHAR(30),
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ENABLED SMALLINT = DEFAULT 1 NOT NULL );

-- DDL Statements for primary key on Table = COMPANY
CREATE UNIQUE = INDEX company_pkey ON EDEN_ITPP.COMPANY(ID);


This works fine from JDBC if I do = this:

PreparedStatement p =3D conn.prepareCall("? =3D call = EDEN_ITPP.INSERT_COMPANY(?, ?, ?, ?)");
p.registerOutParameter(1, Types.INTEGER); =
p.setString(2, = "NewCompany");
p.setInt(3,= 0);
p.setString(4, = "555-555-5555");
p.setString(5, "555-555-5556");
p.execute();
System.out.println(p.getInt(1));
=
It returns the new company's ID = as expected.=A0

However, the query is not constructed = using the posgre syntax, it uses the standard JDBC stored procedure = syntax, so as to be compatible with our other DBs
(Right now it runs on DB2, SQL Server, = Oracle, and MySQL, all of which work with this query): =

This = is not specific to postgres, this is the jdbc spec. I'd have to look = into why it isn't working as expected, however you have worse problems = using max(id) to get a unique number in an mvcc database such as = postgresql or oracle.

Dave

PreparedStatement = p =3D conn.prepareCall("call EDEN_ITPP.INSERT_COMPANY(?,?, ?, ?, = ?)");
p.setString(1, = "NewCompany");
p.setInt(2,= 0);
p.setString(3, = "555-555-5555");
p.setString(4, "555-555-5556");
p.registerOutParameter(5, = Types.INTEGER);
p.execute();
System.out.println(p.getInt(5));
=
But Postgre barfs at = this.
The exception = thrown is:
PSQLException: A = CallableStatement function was executed and the return was of type = java.sql.Types=3D4 however type java.sql.Types=3D0 was = registered.

I = am using Server 8.1.1 and JDBC driver 8.1Build 404.
Does anyone know if there is any plan to = support out parameters for stored functions in PostgreSQL in the = future?

(See the = Note regarding stored procedures and out parameter support) =
http://= jdbc.postgresql.org/documentation/81/callproc.html
=
Thanks for your help, =
Kyle

=

= --Apple-Mail-1--645848177--