M
M
Michael2018-12-27 14:26:59
PostgreSQL
Michael, 2018-12-27 14:26:59

How can ProgreSQL limit itself to one function when generating JSON?

Here is the code (below the spoller) consisting of two functions returning JSON
One function returns an array converted to JSON and the second uses it as an object in another JSON

spoiler

create or replace function sheduller.get_duty (worship_id sheduller.worship.id%type) returns json as $$
declare
d_ret json;
begin
select array_to_json(array_agg(row_to_json(row_duty))) into d_ret from
(select * from sheduller.duty duty where duty.id = worship_id) row_duty;
return d_ret;
end;
$$
language plpgsql security invoker;
create or replace function sheduller.get_sheduler_day (dday date) returns json as $$
declare
d_ret json;
begin
select array_to_json(array_agg(row_to_json(row_worship))) into d_ret from
(select worship.* , sheduller.get_duty(worship.id) as duty from sheduller.worship worship where worship.day = dday) row_worship;
return d_ret;
end;
$$
language plpgsql security invoker;

This code works, but you can certainly do the same thing, but with one function. Can someone suggest how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2018-12-27
@melkij

json_agg is native, if I understand what you mean.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question