Does anyone know how to set the body of a PLPGSQL function to the text of the result of another function? For example, this is what I have tried:

CREATE FUNCTION "Foo"() RETURNS TEXT AS $$
    BEGIN
        RETURN 'BEGIN END;';
    END;
$$ LANGUAGE PLPGSQL;

CREATE FUNCTION "Bar"() RETURNS TRIGGER AS "Foo"() LANGUAGE PLPGSQL;

What I would like is for "Bar" to be created such that it is equivalent to the following:

CREATE FUNCTION "Bar"() RETURNS TRIGGER AS $$
    BEGIN END;
$$ LANGUAGE PLPGSQL;

Does anyone know how to accomplish such a thing (e.g., define a function body based on the result of another function)? Thanks,

有帮助吗?

解决方案

Thanks to Craig Ringer for the suggestion. This is the code that I ended up writing to generate the function:

CREATE FUNCTION "Foo"() RETURNS VOID AS $$
    BEGIN
        EXECUTE
            'CREATE FUNCTION ' || QUOTE_IDENT('Bar') || '() RETURNS TRIGGER AS $a$' || chr(10) ||
            'BEGIN' || chr(10) ||
            'END;' || chr(10) ||
            '$a$ LANGUAGE PLPGSQL;';
    END;
$$ LANGUAGE PLPGSQL;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top