PLPython 与Postgresql 中的 CAST 复合数据类型
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
Plpython 在如下编写时接受查询,并使用此格式的变量指定的用户定义类型 $1:: 并停止抛出复合类型不支持的异常,
最后,我真的不需要做任何事情对数据库进行额外的转换操作。只需像上面那样调整变量就可以了。
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,
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.