Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to call udf of pg and handle jsonb return value? #67

Closed
gzliudan opened this issue Feb 13, 2018 · 5 comments
Closed

How to call udf of pg and handle jsonb return value? #67

gzliudan opened this issue Feb 13, 2018 · 5 comments

Comments

@gzliudan
Copy link

I defined many functions which return value is jsonb data type in postgresql. How to call pg's udf and handle jsonb return value in pgmoon?

@leafo
Copy link
Owner

leafo commented Feb 14, 2018

json data should automatically be decoded to a lua table as long as the type of the value is correct coming from postgres. Are you sure it's not accidentally a string when being returned?

@gzliudan
Copy link
Author

gzliudan commented Feb 15, 2018

below is a udf example in my pg database, I want the return value is json or string data type after call udf in lua:
CREATE OR REPLACE FUNCTION get_my_data () RETURNS JSONB
AS
$BODY$
DECLARE
BEGIN
RETURN jsonb_build_object('return_code', 0, 'result', '{"id":"1","name":"test1"}'::JSONB);
END;
$BODY$
LANGUAGE plpgsql;

@gzliudan
Copy link
Author

gzliudan commented Apr 3, 2018

Automatically decoded json to lua table is acceptable. But it's better to provide 2 function and let user choose return type is string or lua table.

@leafo
Copy link
Owner

leafo commented Apr 3, 2018

You can cast the value to a string from postgres using ::text

If you want to disable json deserialization on the lua end, you can run:

client:set_type_oid(114, nil)
client:set_type_oid(3802, nil)

@gzliudan
Copy link
Author

gzliudan commented Apr 3, 2018

thank you very much.

@gzliudan gzliudan closed this as completed Apr 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants