pgsql/schema_create_table_statement.sql
[Pman.Core] / pgsql / schema_create_table_statement.sql
1 CREATE OR REPLACE FUNCTION schema_create_table_statement(p_table_name varchar)
2   RETURNS text AS
3 $BODY$
4 DECLARE
5     v_table_ddl   text;
6     column_record record;
7     v_schema text;
8 BEGIN
9     FOR column_record IN 
10         SELECT 
11             b.nspname as schema_name,
12             b.relname as table_name,
13             a.attname as column_name,
14             pg_catalog.format_type(a.atttypid, a.atttypmod) as column_type,
15             CASE WHEN 
16                 (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
17                  FROM pg_catalog.pg_attrdef d
18                  WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) IS NOT NULL THEN
19                 'DEFAULT '|| (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
20                               FROM pg_catalog.pg_attrdef d
21                               WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
22             ELSE
23                 ''
24             END as column_default_value,
25             CASE WHEN a.attnotnull = true THEN 
26                 'NOT NULL'
27             ELSE
28                 'NULL'
29             END as column_not_null,
30             a.attnum as attnum,
31             e.max_attnum as max_attnum
32         FROM 
33             pg_catalog.pg_attribute a
34             INNER JOIN 
35              (SELECT c.oid,
36                 n.nspname,
37                 c.relname
38               FROM pg_catalog.pg_class c
39                    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
40               WHERE c.relname ~ ('^('||p_table_name||')$')
41                 AND pg_catalog.pg_table_is_visible(c.oid)
42               ORDER BY 2, 3) b
43             ON a.attrelid = b.oid
44             INNER JOIN 
45              (SELECT 
46                   a.attrelid,
47                   max(a.attnum) as max_attnum
48               FROM pg_catalog.pg_attribute a
49               WHERE a.attnum > 0 
50                 AND NOT a.attisdropped
51               GROUP BY a.attrelid) e
52             ON a.attrelid=e.attrelid
53         WHERE a.attnum > 0 
54           AND NOT a.attisdropped
55         ORDER BY a.attnum
56     LOOP
57         IF column_record.attnum = 1 THEN
58             v_table_ddl:='CREATE TABLE '||column_record.schema_name||'.'||column_record.table_name||' ();'||chr(10)||chr(10);
59         ELSE
60             
61         END IF;
62         -- what does this do?
63         v_schema := column_record.schema_name;
64          
65         IF column_record.attnum <= column_record.max_attnum THEN
66             v_table_ddl:= v_table_ddl||chr(10)||
67                      'ALTER TABLE  '||column_record.schema_name||'.'||column_record.table_name||
68                      ' ADD COLUMN ' ||column_record.column_name||' '||column_record.column_type||' '||column_record.column_default_value||' '||column_record.column_not_null || ';';
69         END IF;
70     END LOOP;
71     
72     FOR column_record IN 
73         
74         c.relname as iname,
75         CASE c.relkind
76             WHEN 'r' THEN 'table'
77             WHEN 'v' THEN 'view'
78             WHEN 'i' THEN 'index'
79             WHEN 'S' THEN 'sequence'
80             WHEN 's' THEN 'special' END as itype,
81           
82             c2.relname as itable
83         FROM pg_catalog.pg_class c
84              JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
85              JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid
86              LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner
87              LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
88         WHERE c.relkind IN ('i','')
89             AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
90             AND pg_catalog.pg_table_is_visible(c.oid)
91             AND c2.relname  = p_table_name
92         
93     LOOP
94          
95             v_table_ddl:= v_table_ddl||chr(10)||
96                      'CREATE INDEX ' ||  column_record.iname || 'ON '||v_schema||'.'||p_table_name|
97                      ' ADD COLUMN ' ||column_record.column_name||' '||column_record.column_type||' '||column_record.column_default_value||' '||column_record.column_not_null || ';';
98         END IF;
99     END LOOP;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115     --v_table_ddl:=v_table_ddl||');';
116     RETURN v_table_ddl;
117 END;
118 $BODY$
119   LANGUAGE 'plpgsql' COST 100.0 SECURITY INVOKER;
120   
121 SELECT schema_create_table_statement('item');
122