2013-11-27 34 views

回答

0

有感興趣的兩個功能:

SELECT pg_catalog.pg_get_function_arguments('foobar'::regproc); 

SELECT pg_catalog.pg_get_functiondef('foobar'::regproc); 

實施例輸出(具有相關聯的獎金功能,其將標籤以空格後者):

denis=# select pg_get_function_arguments('defproc'::regproc); 
pg_get_function_arguments 
--------------------------- 
_proc regprocedure 
(1 row) 

denis=# select pg_get_functiondef('defproc'::regproc); 
              pg_get_functiondef            
--------------------------------------------------------------------------------------------------------- 
CREATE OR REPLACE FUNCTION public.defproc(_proc regprocedure) 
    RETURNS text 
    LANGUAGE plpgsql 
    STABLE STRICT 
    SET search_path TO "$user",public 
AS $function$ 
DECLARE 
    _def  text; 
    _eol  text; 
    _unindent text; 
    _untabbed text; 
    line  text; 
    pos   int; 
BEGIN 
    _def := pg_get_functiondef($1); 
    _eol := (regexp_matches(_def, E'\\r?\\n'))[1]; 

    _unindent := (regexp_matches(_def, _eol || E'AS\\s*\\$(?:functionx*)\\$' || _eol || E'(\\t*)'))[1]; 

    IF _unindent <> '' 
    THEN 
     _def := regexp_replace(_def, '^' || _unindent, '', 'gn'); 
    END IF; 

    FOR line IN 
    SELECT regexp_split_to_table(_def, _eol) as line 
    LOOP 
     line = trim(trailing E' \t' from line); 
     LOOP 
      pos := position(E'\t' in line); 

      IF pos = 0 
      THEN 
       EXIT; 
      END IF; 

      line := substring(line from 1 for pos - 1) || 
        repeat(' ', 4 - (pos % 4)) || 
        substring(line from pos + 1 for length(line)); 
     END LOOP; 

     _untabbed := COALESCE(_untabbed || _eol, '') || line; 
    END LOOP; 

    RETURN _untabbed; 
END; 
$function$ 
(1 row) 
相關問題