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

标签 c# postgresql metadata npgsql

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

最佳答案

有两个感兴趣的函数:

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)

关于c# - 我可以用 c# 提取 postgresql 存储过程元数据吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20252960/

相关文章:

c# - 以非常特定的顺序对字符串列表进行排序

c# - 在列表框中查看项目

ruby-on-rails - 如何使用 ActiveRecord 访问 Postgres 列默认值?

python - 元数据中的启动脚本未运行(Python、谷歌计算引擎、云存储触发器)

c# - EmailAddress DataAnnotation 在 View 上标记为有效但 ModelState.IsValid = false?

sql - Postgres - 无需聚合即可分组?

java - CDI + JPA + EJB + JTA + JSF——使用 PostgreSQL 的 JBoss-Kitchen 示例?

Java:BufferedImage 上的 getHeight() 返回图像宽度,getWidth() 返回图像高度

php - 检索用户上传的视频规范

c# - Azure管道找不到 Selenium 测试