pgsql.sql 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. CREATE OR REPLACE FUNCTION pgsql_type(a_type varchar) RETURNS varchar AS
  2. $BODY$
  3. DECLARE
  4. v_type varchar;
  5. BEGIN
  6. IF a_type='int8' THEN
  7. v_type:='bigint';
  8. ELSIF a_type='int4' THEN
  9. v_type:='integer';
  10. ELSIF a_type='int2' THEN
  11. v_type:='smallint';
  12. ELSIF a_type='bpchar' THEN
  13. v_type:='char';
  14. ELSE
  15. v_type:=a_type;
  16. END IF;
  17. RETURN v_type;
  18. END;
  19. $BODY$
  20. LANGUAGE PLPGSQL;
  21. CREATE TYPE "public"."tablestruct" AS (
  22. "fields_key_name" varchar(100),
  23. "fields_name" VARCHAR(200),
  24. "fields_type" VARCHAR(20),
  25. "fields_length" BIGINT,
  26. "fields_not_null" VARCHAR(10),
  27. "fields_default" VARCHAR(500),
  28. "fields_comment" VARCHAR(1000)
  29. );
  30. CREATE OR REPLACE FUNCTION "public"."table_msg" (a_schema_name varchar, a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
  31. $body$
  32. DECLARE
  33. v_ret tablestruct;
  34. v_oid oid;
  35. v_sql varchar;
  36. v_rec RECORD;
  37. v_key varchar;
  38. BEGIN
  39. SELECT
  40. pg_class.oid INTO v_oid
  41. FROM
  42. pg_class
  43. INNER JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid AND lower(pg_namespace.nspname) = a_schema_name)
  44. WHERE
  45. pg_class.relname=a_table_name;
  46. IF NOT FOUND THEN
  47. RETURN;
  48. END IF;
  49. v_sql='
  50. SELECT
  51. pg_attribute.attname AS fields_name,
  52. pg_attribute.attnum AS fields_index,
  53. pgsql_type(pg_type.typname::varchar) AS fields_type,
  54. pg_attribute.atttypmod-4 as fields_length,
  55. CASE WHEN pg_attribute.attnotnull THEN ''not null''
  56. ELSE ''''
  57. END AS fields_not_null,
  58. pg_attrdef.adsrc AS fields_default,
  59. pg_description.description AS fields_comment
  60. FROM
  61. pg_attribute
  62. INNER JOIN pg_class ON pg_attribute.attrelid = pg_class.oid
  63. INNER JOIN pg_type ON pg_attribute.atttypid = pg_type.oid
  64. LEFT OUTER JOIN pg_attrdef ON pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = pg_attribute.attnum
  65. LEFT OUTER JOIN pg_description ON pg_description.objoid = pg_class.oid AND pg_description.objsubid = pg_attribute.attnum
  66. WHERE
  67. pg_attribute.attnum > 0
  68. AND attisdropped <> ''t''
  69. AND pg_class.oid = ' || v_oid || '
  70. ORDER BY pg_attribute.attnum' ;
  71. FOR v_rec IN EXECUTE v_sql LOOP
  72. v_ret.fields_name=v_rec.fields_name;
  73. v_ret.fields_type=v_rec.fields_type;
  74. IF v_rec.fields_length > 0 THEN
  75. v_ret.fields_length:=v_rec.fields_length;
  76. ELSE
  77. v_ret.fields_length:=NULL;
  78. END IF;
  79. v_ret.fields_not_null=v_rec.fields_not_null;
  80. v_ret.fields_default=v_rec.fields_default;
  81. v_ret.fields_comment=v_rec.fields_comment;
  82. SELECT constraint_name INTO v_key FROM information_schema.key_column_usage WHERE table_schema=a_schema_name AND table_name=a_table_name AND column_name=v_rec.fields_name;
  83. IF FOUND THEN
  84. v_ret.fields_key_name=v_key;
  85. ELSE
  86. v_ret.fields_key_name='';
  87. END IF;
  88. RETURN NEXT v_ret;
  89. END LOOP;
  90. RETURN ;
  91. END;
  92. $body$
  93. LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
  94. COMMENT ON FUNCTION "public"."table_msg"(a_schema_name varchar, a_table_name varchar)
  95. IS '获得表信息';
  96. ---重载一个函数
  97. CREATE OR REPLACE FUNCTION "public"."table_msg" (a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
  98. $body$
  99. DECLARE
  100. v_ret tablestruct;
  101. BEGIN
  102. FOR v_ret IN SELECT * FROM table_msg('public',a_table_name) LOOP
  103. RETURN NEXT v_ret;
  104. END LOOP;
  105. RETURN;
  106. END;
  107. $body$
  108. LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
  109. COMMENT ON FUNCTION "public"."table_msg"(a_table_name varchar)
  110. IS '获得表信息';