我似乎无法从存储的函数中正确读取UDT postgres JDBC驱动程序。这是一些示例代码:
CREATE TYPE u_country AS ENUM ('Brazil', 'England', 'Germany')
CREATE TYPE u_street_type AS (
street VARCHAR(100),
no VARCHAR(30)
)
CREATE TYPE u_address_type AS (
street u_street_type,
zip VARCHAR(50),
city VARCHAR(50),
country u_country,
since DATE,
code INTEGER
)
CREATE TABLE t_author (
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50) NOT NULL,
date_of_birth DATE,
year_of_birth INTEGER,
address u_address_type
)
INSERT INTO t_author VALUES (1, 'George', 'Orwell',
TO_DATE('1903-06-25', 'YYYY-MM-DD'), 1903, ROW(ROW('Parliament Hill',
'77'), 'NW31A9', 'Hampstead', 'England', '1980-01-01', null))
INSERT INTO t_author VALUES (2, 'Paulo', 'Coelho',
TO_DATE('1947-08-24', 'YYYY-MM-DD'), 1947, ROW(ROW('Caixa Postal',
'43.003'), null, 'Rio de Janeiro', 'Brazil', '1940-01-01', 2))
CREATE FUNCTION p_enhance_address2 (address OUT u_address_type)
AS $$
BEGIN
SELECT t_author.address
INTO address
FROM t_author
WHERE first_name = 'George';
END;
$$ LANGUAGE plpgsql;
现在上面的内容完美地适用于postgres。我也可以选择UDT 列t_author.address直接带有SQL SELECT语句。但当 我通过JDBC从存储的函数p_enhance_address2中选择,得到一个 奇怪的行为。我试过这两个调用方案:
connection.prepareStatement("select * from p_enhance_address2()");
connection.prepareCall("{ call p_enhance_address2(?) }");
// the latter with an output parameter registered
两种呼叫方案都会产生相同的行为(实际上是 CallableStatement只不过是从函数中选择)。 似乎有两个非常明显的问题:
嵌套的UDT结构完全搞砸了抓取结果。这个 是我得到的JDBC:
PreparedStatement stmt = connection.prepareStatement(
"select * from p_enhance_address2()");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println("# of columns: " +
rs.getMetaData().getColumnCount());
System.out.println(rs.getObject(1));
}
输出:
列数:6 (“(”“国会山”,“77)”,NW31A9)
为什么有6列?为什么UDT被错误地取出(很多 字段丢失)
当嵌套UDT时,可以实现一点改进 u_street_type被“展平”到varchar,这导致了 假设JDBC驱动程序很难支持嵌套的UDT:
CREATE TYPE u_address_type AS (
street VARCHAR(80),
zip VARCHAR(50),
city VARCHAR(50),
country u_country,
since DATE,
code INTEGER
)
INSERT INTO t_author VALUES (1, 'George', 'Orwell',
TO_DATE('1903-06-25', 'YYYY-MM-DD'), 1903, ROW('Parliament Hill 77',
'NW31A9', 'Hampstead', 'England', '1980-01-01', null))
INSERT INTO t_author VALUES (2, 'Paulo', 'Coelho',
TO_DATE('1947-08-24', 'YYYY-MM-DD'), 1947, ROW('Caixa Postal 43.003',
null, 'Rio de Janeiro', 'Brazil', '1940-01-01', 2))
然后结果将是这样的:
列数:6 (“国会山77”,NW31A9,汉普斯特德,英国,1980-01-01,)
UDT记录现在看起来正确(从结果集中获取) 位置1)。但结果集中仍有6列。
一些事实:
有谁知道出了什么问题?
答案 0 :(得分:2)
如果没有错误,则postgres9.4驱动程序(postgresql-9.0-801.jdbc4.jar)没有正式支持UDT(用户定义类型)(JDBC一致性)。调用postgres函数形式java代码时,我也面临很多问题。 我用下面的司机解决了我的问题。
这是非官方的驱动程序链接, http://impossibl.github.io/pgjdbc-ng/ 复制驱动程序形成链接创建连接,如下所述
请参阅下面的工作示例,从我的POC Oracle10g到Postgres9.4迁移活动,请参见下面的示例POC,
-- Function: fn_test_t(text)
-- DROP FUNCTION fn_test_t(text);
CREATE OR REPLACE FUNCTION fn_test_t(
IN txt text,
OUT a typ_address[],
OUT b typ_address[])
RETURNS record AS
$BODY$
DECLARE
address typ_address[];
BEGIN
RAISE INFO '@@ inside fn_test:(%)',111;
address[1] := ROW('Amravati', 'Mahalaxmi', ROW('Test1'));
address[2] := ROW('Pune', 'NICMAR',ROW('Test2'));
address[3] := ROW('', '',ROW('Test3'));
RAISE INFO 'array of address:(%)',address;
--a := ROW(address);
--b := ROW(address);
a := address;
b := address;
RAISE INFO 'typ_address_t a:(%)',a;
RAISE INFO 'typ_address_t b:(%)',b;
-- RETURN address;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION fn_test_t(text)
OWNER TO postgres;
=========================
-- Type: typ_address
-- DROP TYPE typ_address;
CREATE TYPE typ_address AS
(add1 character varying,
add2 character varying,
t typ_test);
ALTER TYPE typ_address
OWNER TO postgres;
==========================
-- Type: typ_test
-- DROP TYPE typ_test;
CREATE TYPE typ_test AS
(t1 character varying);
ALTER TYPE typ_test
OWNER TO postgres;
==========================
主要功能调用,
public static void oracleToPosgresUDTCall() {
System.out.println("@@@ inside oracleToPosgresUDTCall...");
Connection c = null;
try {
Class.forName("com.impossibl.postgres.jdbc.PGDriver");
c = DriverManager
.getConnection("jdbc:pgsql://localhost:5433/orapg", "postgres", "root");
System.out.println(c.getMetaData().getDriverVersion());
//you can map your UDT to pojo here Great !!!
Map<String, Class<?>> m = c.getTypeMap();
m.put("typ_address", Address.class);
m.put("typ_test", AddressTypeTest.class);
c.setTypeMap(m);
// Procedure call
CallableStatement cstmt = c.prepareCall("{call fn_test_t(?,?,?)}");
cstmt.setString(1, "791000252423");
cstmt.registerOutParameter(2, Types.ARRAY);
cstmt.registerOutParameter(3, Types.ARRAY);
boolean b = cstmt.execute();
Array arr = cstmt.getArray(1); //output array starts from index 1
System.out.println("arr:" + arr.getBaseTypeName());
Object obj = arr.getArray();
System.out.println("Address obj:" + obj);
Address[] a = (Address[])obj;
System.out.println("Address obj:" + a[0].getAdd1());
System.out.println("Address obj:" + a[0].getTypeTest().getT1());
System.out.println("=======================================================");
//MORE
List<Address> list = Arrays.asList(a);
for(Address aa: list){
System.out.println(aa.getAdd1());
System.out.println(aa.getAdd2());
System.out.println("t1:" + aa.getTypeTest().getT1());
}
cstmt.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
答案 1 :(得分:1)
我可以重现这个,似乎这是一个错误。
我建议你把它发布到PostgreSQL JDBC邮件列表,这样开发人员就可以解决这个问题。