Function to return dynamic set of columns for given table

亡梦爱人 提交于 2019-11-28 11:41:50

Solution for the simple case

As explained in the referenced answers below, you can use registered (row) types, and thus implicitly declare the return type of a polymorphic function:

CREATE OR REPLACE FUNCTION public.get_table(_tbl_type anyelement)
  RETURNS SETOF anyelement AS
$func$
BEGIN
   RETURN QUERY EXECUTE format('TABLE %s', pg_typeof(_tbl_type));
END
$func$ LANGUAGE plpgsql;

Call:

SELECT * FROM public.get_table(NULL::public.users);  -- note the syntax!

Returns the complete table (with all user columns).

Wait! How?

Detailed explanation in this related answer, chapter "Various complete table types":

TABLE foo is just short for SELECT * FROM foo:

2 steps for completely dynamic return type

But what you are trying to do is strictly impossible in a single SQL command.

I want to pass schema_name and table_name as parameters to function and get record list, according to column_visible field in public.fields table.

There is no direct way to return an arbitrary selection of columns (return type not known at call time) from a function - or any SQL command. SQL demands to know number, names and types of resulting columns at call time. More in the 2nd chapter of this related answer:

There are various workarounds. You could wrap the result in one of the standard document types (json, jsonb, hstore, xml).

Or you generate the query with one function call and execute the result with the next:

CREATE OR REPLACE FUNCTION public.generate_get_table(_schema_name text, _table_name text)
  RETURNS text AS
$func$
   SELECT format('SELECT %s FROM %I.%I'
               , string_agg(quote_ident(column_name), ', ')
               , schema_name
               , table_name)
   FROM   fields
   WHERE  column_visible
   AND    schema_name = _schema_name 
   AND    table_name  = _table_name
   GROUP  BY schema_name, table_name
   ORDER  BY schema_name, table_name;
$func$  LANGUAGE sql;

Call:

SELECT public.generate_get_table('public', 'users');

This create a query of the form:

SELECT usr_id, usr FROM public.users;

Execute it in the 2nd step.
(You might want to add column numbers and order columns.)
Be sure to defend against SQL injection:

Asides

varchar(100) does not make much sense for identifiers, which are limited to 63 characters in standard Postgres:

If you understand how the object identifier type regclass works, you might replace schema and table name with a singe regclass column.

I think you just need another query to get the list of columns you want.

Maybe something like (this is untested):

create or replace function public.get_table(_schema_name text, _table_name text, active boolean default true) returns setof record as $$
declare
    entity_name text default schema_name || '.' || table_name;
    r record;
    columns varchar;
begin
    -- Get the list of columns
    SELECT string_agg(column_name, ', ')
         INTO columns
         FROM public.fields
         WHERE fields.schema_name = _schema_name
            AND fields.table_name = _table_name
            AND fields.column_visible = TRUE;

    -- Return rows from the specified table
    RETURN QUERY EXECUTE 'select ' || columns || ' from ' || entity_name;

    RETURN;
end
$$
language plpgsql;

Keep in mind that column/table references may need to be surrounded by double quotes if they have certain characters in them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!