PLPython 与Postgresql 中的 CAST 复合数据类型

发布于 2025-01-05 09:55:23 字数 896 浏览 1 评论 0原文

我正在 PlPython 中使用用户定义的类型编写存储过程。我知道 Plpython 不支持用户定义类型,因此,我为用户定义类型创建了 CAST。当我调用 plpy.prepare 时,我仍然收到错误。我不确定我是否错误地使用了 CAST - 示例代码如下:

#User Defined Type - person
CREATE TYPE person As( name character varying(50), state  character(2));

#Table definition using 'person'
CREATE TABLE manager As(id integer, mgr person)

#CAST for person
CREATE OR REPLACE FUNCTION person_to_text(person) RETURNS text AS 'SELECT ROW($1.*)::text' LANGUAGE SQL;
CREATE CAST (cv_person as text) WITH FUNCTION person_to_text(person)

#PlPython procedure

CREATE OR REPLACE FUNCTION load_portfolio_assoc (name text, state text) RETURNS integer AS $$

  mgr_str ="('"+name+"','"+state+"')"

  insert_qry = 'Insert into manager (mgr) values($1)'
  value_type = ['text']

  qry = plpy.prepare(insert_qry,value_type)   
  rv  = plpy.execute(qry, [mgr_str])

  return 1


  $$ LANGUAGE plpython3u;

I am writing a stored procedure in PlPython with a user defined type. I know Plpython does not support user defined types, so, I have created a CAST for the user defined type. Still I keep getting an error when I call plpy.prepare. I am not sure if I am using the CAST incorrectly - the example code is below:

#User Defined Type - person
CREATE TYPE person As( name character varying(50), state  character(2));

#Table definition using 'person'
CREATE TABLE manager As(id integer, mgr person)

#CAST for person
CREATE OR REPLACE FUNCTION person_to_text(person) RETURNS text AS 'SELECT ROW($1.*)::text' LANGUAGE SQL;
CREATE CAST (cv_person as text) WITH FUNCTION person_to_text(person)

#PlPython procedure

CREATE OR REPLACE FUNCTION load_portfolio_assoc (name text, state text) RETURNS integer AS $

  mgr_str ="('"+name+"','"+state+"')"

  insert_qry = 'Insert into manager (mgr) values($1)'
  value_type = ['text']

  qry = plpy.prepare(insert_qry,value_type)   
  rv  = plpy.execute(qry, [mgr_str])

  return 1


  $ LANGUAGE plpython3u;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶视觉 2025-01-12 09:55:23

更新:

Plpython 在如下编写时接受查询,并使用此格式的变量指定的用户定义类型 $1:: 并停止抛出复合类型不支持的异常,

insert_qry = 'Insert into manager (mgr) values($1::person)'
value_type = ['text']

最后,我真的不需要做任何事情对数据库进行额外的转换操作。只需像上面那样调整变量就可以了。

An update :

Plpython accepts the query when it is written as follows with the user defined type specified with the variable in this format $1:: and stops throwing composite type not supported exceptions,

insert_qry = 'Insert into manager (mgr) values($1::person)'
value_type = ['text']

At the end, I really didn't have to do any extra casting operation on the database. It worked just by tweaking the variable as above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文