Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 79EDDB5DBCE for ; Mon, 4 Jul 2011 07:14:39 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 78499-01 for ; Mon, 4 Jul 2011 10:14:32 +0000 (UTC) X-Greylist: delayed 00:33:17.96276 by SQLgrey-1.7.6 Received: from interactive-1.com (interactive-1.com [193.85.232.82]) by mail.postgresql.org (Postfix) with ESMTP id B1971B5DBCB for ; Mon, 4 Jul 2011 07:14:31 -0300 (ADT) Received: from localhost (server106 [127.0.0.1]) by interactive-1.com (Postfix) with ESMTP id A5ECB1B281E for ; Mon, 4 Jul 2011 11:41:12 +0200 (CEST) Received: from interactive-1.com ([127.0.0.1]) by localhost (interactive-1.com [127.0.0.1]) (amavisd-new, port 10025) with ESMTP id 30h9TmciVuOL for ; Mon, 4 Jul 2011 11:41:12 +0200 (CEST) Message-ID: <4E118AA6.1080804@i3.cz> Date: Mon, 04 Jul 2011 11:40:54 +0200 From: Vaclav Tvrdik User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110606 Lightning/1.0b2 Icedove/3.1.10 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Question about passing array of a complex type from jdbc to plpgsql Content-Type: multipart/alternative; boundary="------------040105010306040303070109" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.889 tagged_above=-10 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, T_FRT_BELOW2=0.01 X-Spam-Level: X-Archive-Number: 201107/1 X-Sequence-Number: 22005 This is a multi-part message in MIME format. --------------040105010306040303070109 Content-Type: text/plain; charset=ISO-8859-2 Content-Transfer-Encoding: 7bit Hello, I have a question about passing about passing array of complex type into plpgsql function. We have function with following signature: create or replace function PARSE_BTS_WALK_TRACK( p_user BTS_WALK_TRACK.USER_ID%type, -- integer p_client_imei BTS_WALK_TRACK.IMEI%type, -- varchar p_btwt_recs BTWT_RECORD[], out po_create_complex_cnt integer, out po_rslt FCALL_RESULT) as $BODY$ ..... $BODY$ language 'plpgsql' volatile security definer; Types definitions are bellow and the point is in BTWT_RECORD which itself contains array of other type. It is possible (and correctly working) to execute this function from psql with a command: select PARSE_BTS_WALK_TRACK( 1, 'imei', ARRAY[('127.0.0.1', 80, 1111111, 230, 2, 1, 11, 230, 2, 1, 111, 14.0, 50.0, null, 1,ARRAY[(14.0, 54.0, null, null, null, null, null, null, 'TEST')::GPS_RECORD])::BTWT_RECORD]); First we have tried to call this method using JDBC Array and Struct types, but postgresql jdbc driver doesn't implement Struct yet so we weren't able to get thinks work (Connection.createStruct is not implemented and with custom Struct implementations are problems too). So we are using PGObject to push the data to the database and we have problem with correct syntax, mainly for complex usages.We could not find any info about it. Finally we have been able to call the function from jdbc by passing a text representation of the array of the nested type GPS_RECORD. Working approach seams to be like (the real working code we use is embraced in our framework and thus is quite long to be posted here): Connection conn = ... from some pool ... CallableStatement statement = connection.prepareCall("PARSE_BTS_WALK_TRACK(?, ?, ?, ?)"); ... register some input params String[] params ... params[0] = "(85.160.201.65, 58193, 1203099449, 230, 2, 3142, 203293913, 230, 2, 1137, 203343914, 0.0, 0.0,,,{\"(\"\\\\\",\"1\\\\\",\"\\\\\",\"1\\\\\",\"1\\\\\",\"1\\\\\",\"1\\\\\",\"1\\\\\",\"A\\\\\")\"})"; params[1] = next record definition here .... .... PGobject[] objs = new PGObject[params.length]; for (int i = 0; i < params.length;i++) { final PGobject obj = new PGobject(); obj.setType("BTWT_RECORD"); obj.setValue(params[a]); objs[i] = obj; } Object value = statement.getConnection().createArrayOf("BTWT_RECORD", objs); statement..setObject(1, value, Types.ARRAY); ... register some output params statement.execute(); So we have two basic questions: 1) is it supported passing such complex structures (array of types) from jdbc without using a text representation for the data (I mean purely using Java classes Array, PgObject or similar) ? 2) If not we are OK with using "mixed solution" (part of a passed data is text representaion), but rules for escaping are unclear to us (we found above syntax by try/error method, mainly assymetric backslashing of nested values looks weird) and we could not find some description how to do it. Can someone point us to description or clear how the rules for text representation of nested types are ? In near time we are going to pass even more complex data between jdbc and postgres, so try/error approach is not best one for us:-). Thanks in advance for any help Vaclav TVRDIK Types definition used in function are following: create type FCALL_RESULT as ( STATUS varchar(5), -- OK, WARN, ERR, NOTICE ERROR_CODE int, -- 0 = OK ERROR_DESC varchar(1024), PARAMS varchar(1024) -- if needed, csv list ); create type GPS_RECORD as ( GPS_LATITUDE float8, GPS_LONGTITUDE float8, GPS_ALTITUDE float8, GPS_VELOCITY float8, GPS_TIME integer, GPS_ALTITUDE_ACC float8, GPS_ACC float8, GPS_HEADING float8, GPS_SOURCE varchar ); create type BTWT_RECORD as ( IP_ADDRESS varchar, PORT integer, CLIENT_TIME integer, -- sec from epoch CELL_OUT_MCC integer, CELL_OUT_MNC integer, CELL_OUT_LAC integer, CELL_OUT_CELLID integer, CELL_IN_MCC integer, CELL_IN_MNC integer, CELL_IN_LAC integer, CELL_IN_CELLID integer, GPS_LATITUDE float8, GPS_LONGTITUDE float8, GPS_ALTITUDE float8, LOG_SEQNO integer, GEO_INFO GPS_RECORD[] ); --------------040105010306040303070109 Content-Type: text/html; charset=ISO-8859-2 Content-Transfer-Encoding: 8bit
Hello,

I have a question about passing about passing array of complex type into plpgsql function. We have function with following signature:

create or replace function PARSE_BTS_WALK_TRACK(        p_user                                BTS_WALK_TRACK.USER_ID%type, -- integer
                                                        p_client_imei                        BTS_WALK_TRACK.IMEI%type, -- varchar
                                                        p_btwt_recs                        BTWT_RECORD[],
                                                         out po_create_complex_cnt        integer,
                                                        out po_rslt                        FCALL_RESULT) as
$BODY$
.....
$BODY$
language 'plpgsql' volatile security definer;

Types definitions are bellow and the point is in BTWT_RECORD which itself contains array of other type.

It is possible (and correctly working) to execute this function from psql with a command:
select PARSE_BTS_WALK_TRACK( 1, 'imei', ARRAY[('127.0.0.1', 80, 1111111, 230, 2, 1, 11, 230, 2, 1, 111, 14.0, 50.0, null, 1,ARRAY[(14.0, 54.0, null, null, null, null, null, null, 'TEST')::GPS_RECORD])::BTWT_RECORD]);

First we have tried to call this method using JDBC Array and Struct types, but postgresql jdbc driver doesn't implement Struct yet so we weren't able to get thinks work (Connection.createStruct is not implemented and with custom Struct implementations are problems too). 

So we are using PGObject to push the data to the database and we have problem with correct syntax, mainly for complex usages. We could not find any info about it. Finally we have been able to call the function from jdbc by passing a text representation of the array of the nested type GPS_RECORD. Working approach seams to be like (the real working code we use is embraced in our framework and thus is quite long to be posted here):

 Connection conn = ... from some pool ...
 CallableStatement statement = connection.prepareCall("PARSE_BTS_WALK_TRACK(?, ?, ?, ?)");
 
... register some input params 
 
 String[] params ...
 params[0] = "(85.160.201.65, 58193, 1203099449, 230, 2, 3142, 203293913, 230, 2, 1137, 203343914, 0.0, 0.0,,,{\"(\"\\\\\",\"1\\\\\",\"\\\\\",\"1\\\\\",\"1\\\\\",\"1\\\\\",\"1\\\\\",\"1\\\\\",\"A\\\\\")\"})";
 params[1] = next record definition here
 ....
 ....

 PGobject[] objs = new PGObject[params.length];
 for (int i = 0; i < params.length;i++) {
     final PGobject obj = new PGobject();
     obj.setType("BTWT_RECORD");
     obj.setValue(params[a]);
     objs[i] = obj;
 }
 
 Object value = statement.getConnection().createArrayOf("BTWT_RECORD", objs);
 
 statement..setObject(1, value, Types.ARRAY);

... register some output params

statement.execute();


So we have two basic questions:
1) is it supported passing such complex structures (array of types) from jdbc without using a text representation for the data (I mean purely using Java classes Array, PgObject or similar) ?
2) If not we are OK with using "mixed solution" (part of a passed data is text representaion), but rules for escaping are unclear to us (we found above syntax by try/error method, mainly assymetric backslashing of nested values looks weird) and we could not find some description how to do it. Can someone point us to description or clear how the rules for text representation of nested types are ? In near time we are going to pass even more complex data between jdbc and postgres, so try/error approach is not best one for us:-).

Thanks in advance for any help

Vaclav TVRDIK

 

Types definition used in function are following:

create type FCALL_RESULT as (
        STATUS                varchar(5),        -- OK, WARN, ERR, NOTICE
        ERROR_CODE        int,                -- 0 = OK
        ERROR_DESC        varchar(1024),
        PARAMS                varchar(1024)        -- if needed, csv list
);

create type GPS_RECORD as (
        GPS_LATITUDE                float8,
        GPS_LONGTITUDE                float8,
        GPS_ALTITUDE                float8,
        GPS_VELOCITY                float8,
        GPS_TIME                integer,
        GPS_ALTITUDE_ACC        float8,
        GPS_ACC                        float8,
        GPS_HEADING                float8,
        GPS_SOURCE                varchar
);

create type BTWT_RECORD as (
        IP_ADDRESS                varchar,
        PORT                        integer,
        CLIENT_TIME                integer,                -- sec from epoch
        CELL_OUT_MCC                integer,
        CELL_OUT_MNC                integer,
        CELL_OUT_LAC                integer,
        CELL_OUT_CELLID                integer,
        CELL_IN_MCC                integer,
        CELL_IN_MNC                integer,
        CELL_IN_LAC                integer,
        CELL_IN_CELLID                integer,
        GPS_LATITUDE                float8,
        GPS_LONGTITUDE                float8,
        GPS_ALTITUDE                float8,
        LOG_SEQNO                integer,
        GEO_INFO                GPS_RECORD[]
);




--------------040105010306040303070109--