我可以用c#提取postgresql存储过程元数据吗?

时间:2013-11-27 20:58:18

标签: c# postgresql metadata npgsql

我想传递一个包含存储过程名称的字符串,并使用c#(需要Npgsql点网提供程序)检索该过程(和/或主体,如果可能)的参数(元数据) 可以sb给出代码片段吗?

1 个答案:

答案 0 :(得分: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)