Postgresql JDBC表值参数 [英] Postgresql JDBC Table Valued Parameters

查看:197
本文介绍了Postgresql JDBC表值参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSSQL 有一个很棒的功能,叫做表值参数。它允许您将自定义数据表传递给存储过程和函数。

MSSQL has a great feature called Table Valued Parameters. It allows you to pass a table of a custom data to stored procedures and functions.

我想知道 PostgreSQL 中的等价物是什么,如果一个存在,使用JDBC?
我知道将数组作为函数参数传递的选项,但这似乎仅限于 PostgreSQL 数据类型。

I was wondering what is the equivalent in PostgreSQL, if one exists, using JDBC? I know about the option of passing arrays as function parameters, but that seems limited to PostgreSQL data types.

考虑以下PL / pgSQL代码:

Consider the following PL/pgSQL code:

CREATE  TYPE number_with_time AS(
_num   float,
_date  timestamp
);

此函数标题:

CREATE OR REPLACE FUNCTION myfunc(arr number_with_time[])

任何人都可以使用JDBC驱动程序发布Java代码,使用用户定义数据类型的数组调用该函数?

Can anyone post a Java code using JDBC driver of calling that function with an array of the user defined data type?

推荐答案

假设你想要从客户端传递值 。如果数据库中已存在这些值,则还有其他更简单的方法...

Assuming you want to pass values from the client. If the values exist in the database already, there are other, simpler ways ...


我知道将数组作为函数参数传递的选项,但是
似乎仅限于PostgreSQL数据类型。

I know about the option of passing arrays as function parameters, but that seems limited to PostgreSQL data types.

你可以通过的似乎受到 Java类型和JDBC类型,似乎没有规定数组类型,更不用说复合数组了值...

What you can pass seems to be limited by Java Types and JDBC Types, and there does not seem be provisions for array types, not to speak of arrays of composite values ...

但是,您始终可以传递 text 表示。我基于两个基本事实:

However, you can always pass a text representation. I am building on two basic facts:

1)每个文档:


任何内置或用户定义的基类型的数组,枚举可以创建类型,
复合类型
。目前尚不支持域名数组。

Arrays of any built-in or user-defined base type, enum type, or composite type can be created. Arrays of domains are not yet supported.

大胆强调我的。因此,在您创建了问题中定义的类型 number_with_time 之后 - 或者定义了一个具有相同列的表(自动在系统中注册相应的复合类型) - 您可以还自动使用数组类型 number_with_time []

Bold emphasis mine. Therefore, after you have created the type number_with_time as defined in your question - or defined a table with the same columns, which registers the according composite type in the system automatically - you can also automatically use the array type number_with_time[].

2)有一个 text 每个值的表示。

2) There is a text representation for every value.

因此,还有一个文本表示形式 number_with_time []

Therefore, there is also a text representation for number_with_time[]:

'{"(1,2014-04-20 20:00:00)","(2,2014-04-21 21:00:00)"}'::number_with_time[]



函数调用



实际函数调用取决于函数中定义的返回值 - 隐藏在您的问题中。

Function call

The actual function call depends on the return values defined in your function - which is hidden in your question.

为避免JDBC中数组处理的复杂化,请传递 text 表示。使用 text 参数创建函数。

To avoid complications from array handling in JDBC, pass the text representation. Create the function taking a text parameter.

我不会将名称date用于时间戳。使用这个稍微调整过的类型定义:

I am not going to use the name "date" for a timestamp. Working with this slightly adjusted type definition:

CREATE TYPE number_with_time AS(
   _num float
 , _ts  timestamp
);

简单SQL函数:

CREATE OR REPLACE FUNCTION myfunc_sql(_arr_txt text)
  RETURNS integer AS       -- example
$func$
SELECT sum(_num)::int
FROM   unnest (_arr_txt::number_with_time[]) x
WHERE  _ts > '2014-04-19 20:00:00';
$func$
LANGUAGE sql;

致电:

SELECT myfunc_sql('{"(1,2014-04-20 20:00:00)","(2,2014-04-21 21:00:00)"}');

SQL Fiddle 演示:


  • 以上SQL函数

  • PL / pgSQL variant

  • 复合类型数组的几种语法变体

  • 函数调用。

  • above SQL function
  • PL/pgSQL variant
  • a couple of syntax variants for the array of composite type
  • the function calls.

调用该函数就像使用简单的 text 参数的任何其他函数一样:

Call the function like any other function taking a simple text parameter:

CallableStatement myProc = conn.prepareCall("{ ? = call myfunc_sql( ? ) }");
myProc.registerOutParameter(1, Types.VARCHAR);
// you have to escape double quotes in a Java string!
myProc.setString(2, "{\"(1,2014-04-20 20:00:00)\",\"(2,2014-04-21 21:00:00)\"}");
myProc.execute();
String mySum = myProc.getInt(1);
myProc.close(); 

Postgres JDBC手册中的详细信息。

通过JDBC返回整个表的示例:

从PL / pgSQL函数返回行

Example to return a whole table via JDBC:
Return rows from a PL/pgSQL function

这篇关于Postgresql JDBC表值参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆