Skip to content

Commit fba63bf

Browse files
committed
Update api_defn.sql
1 parent cee8bb0 commit fba63bf

File tree

1 file changed

+63
-47
lines changed

1 file changed

+63
-47
lines changed

server/api_defn.sql

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,59 +29,44 @@ select concat_ws(e'\n'
2929
, 'SELECT'
3030
, select_cols
3131
, format('FROM %I.%I', nspname, relname)
32-
, format('-- WHERE (%s) = ('''')', orderby_cols)
3332
, 'ORDER BY ' || orderby_cols
3433
, 'LIMIT 1000'
3534
, ';'
36-
, ''
37-
, format('-- https://www.postgresql.org/docs/%s/sql-altertable.html', pg_major_ver)
38-
, ''
39-
, '/*'
35+
,''
4036
, (
41-
case relkind
42-
-- view
43-
when 'v' then format(
44-
e'CREATE OR REPLACE VIEW %I.%I AS\n%s'
45-
, nspname
46-
, relname
37+
case
38+
when relkind = 'v' then concat_ws(e'\n'
39+
, format('/* https://www.postgresql.org/docs/%s/sql-alterview.html', pg_major_ver)
40+
, ''
41+
, format('CREATE OR REPLACE VIEW %I.%I AS', nspname, relname)
4742
, pg_get_viewdef(pg_class.oid, true)
43+
, ''
44+
,'*/'
4845
)
49-
when 'm' then format(
50-
e'CREATE OR REPLACE MATERIALIZED VIEW %I.%I AS\n%s'
51-
, nspname
52-
, relname
46+
when relkind = 'm' then concat_ws(e'\n'
47+
, format('/* https://www.postgresql.org/docs/%s/sql-altermaterializedview.html', pg_major_ver)
48+
, ''
49+
, format('CREATE OR REPLACE MATERIALIZED VIEW %I.%I AS', nspname, relname)
5350
, pg_get_viewdef(pg_class.oid, true)
51+
, ''
52+
,'*/'
5453
)
55-
-- TODO a lot of to do, maybe pg_tabledef will be invented sometime
54+
-- TODO a lot of to do, maybe pg_get_tabledef will be invented sometime
5655
-- https://github.com/postgres/postgres/blob/540c39cc56f51b27bff9a6fc78d6524564953c6c/src/bin/pg_dump/pg_dump.c#L17093
56+
-- https://www.postgresql.org/message-id/flat/CAFEN2wxsDSSuOvrU03CE33ZphVLqtyh9viPp6huODCDx2UQkYA%40mail.gmail.com
5757
-- https://www.postgresql.org/docs/current/sql-createtable.html
58-
when 'r' then format(
59-
e'CREATE TABLE %I.%I (\n%s\n)'
60-
, nspname
61-
, relname
62-
, (
63-
select string_agg(
64-
concat_ws(' '
65-
, ' '
66-
, quote_ident(attname)
67-
, format_type(atttypid, atttypmod)
68-
, 'COLLATE "' || nullif(collname, 'default') || '"'
69-
, case when attnotnull then 'NOT NULL' end
70-
, 'DEFAULT (' || pg_get_expr(adbin, adrelid) || ')'
71-
)
72-
, e',\n'
73-
order by attnum
74-
)
75-
from pg_attribute
76-
left outer join pg_attrdef on attrelid = adrelid and adnum = attnum
77-
left outer join pg_collation on pg_collation.oid = attcollation
78-
where attrelid = pg_class.oid and attnum > 0 and not attisdropped
79-
)
58+
when relkind in ('r', 'p') then concat_ws(e'\n'
59+
, format('/* https://www.postgresql.org/docs/%s/sql-altertable.html', pg_major_ver)
60+
, ''
61+
, format('CREATE TABLE %I.%I (', nspname, relname)
62+
, col_defs
63+
, ');'
64+
, ''
65+
, '*/'
8066
)
81-
else 'CREATE script not implemented for this relkind'
67+
else '/* CREATE script not implemented for this relkind */'
8268
end
8369
)
84-
, '*/'
8570
, ''
8671
)
8772
from substring(current_setting('server_version') from '\d+') pg_major_ver
@@ -90,11 +75,23 @@ join pg_namespace on pg_class.relnamespace = pg_namespace.oid
9075
left join pg_constraint pk on (contype, conrelid) = ('p', pg_class.oid)
9176
, lateral (
9277
select string_agg(format(' %I', attname), e',\n' order by attnum)
93-
-- TODO desc indexing
78+
-- TODO descending indexing
9479
, string_agg(format('%I', attname), ', ' order by pk_pos) filter (where pk_pos is not null)
95-
from pg_attribute, array_position(pk.conkey, attnum) pk_pos
80+
, string_agg(col_def, e',\n' order by attnum) filter (where attname != 'oid')
81+
from pg_attribute
82+
left join pg_attrdef on attrelid = adrelid and adnum = attnum
83+
left join pg_collation on pg_collation.oid = attcollation
84+
, array_position(pk.conkey, attnum) pk_pos
85+
, concat_ws(' '
86+
, ' '
87+
, quote_ident(attname)
88+
, format_type(atttypid, atttypmod)
89+
, 'COLLATE "' || nullif(collname, 'default') || '"'
90+
, case when attnotnull then 'NOT NULL' end
91+
, 'DEFAULT (' || pg_get_expr(adbin, adrelid) || ')'
92+
) col_def
9693
where attrelid = pg_class.oid and (attnum > 0 or attname = 'oid') and not attisdropped
97-
) _(select_cols, orderby_cols)
94+
) _(select_cols, orderby_cols, col_defs)
9895
where ('table', pg_class.oid) = ($1, $2)
9996

10097
-- function
@@ -117,7 +114,6 @@ from pg_proc p
117114
where ('function', p.oid) = ($1, $2)
118115
and not exists (select from pg_aggregate where aggfnoid = p.oid)
119116

120-
121117
-- aggregate
122118
union all
123119
select concat_ws(e'\n'
@@ -131,14 +127,34 @@ select concat_ws(e'\n'
131127
, ' ,SORTOP = ' || nullif(aggsortop, 0)::regoperator
132128
, ');'
133129
, ''
134-
, '/*'
135-
, format('DROP AGGREGATE %s(%s);', aggfnoid, fnargs)
136-
, '*/'
130+
-- , '/*'
131+
-- , format('DROP AGGREGATE %s(%s);', aggfnoid, fnargs)
132+
-- , '*/'
137133
, ''
138134
)
139135
from pg_aggregate, pg_get_function_identity_arguments(aggfnoid) fnargs
140136
where ('function', aggfnoid) = ($1, $2)
141137

138+
-- column
139+
union all
140+
select concat_ws(e'\n'
141+
, format('\connect %I', current_catalog)
142+
, ''
143+
, format('/* https://www.postgresql.org/docs/%s/sql-altertable.html'
144+
, substring(current_setting('server_version') from '\d+')
145+
)
146+
, ''
147+
, format('ALTER TABLE %s ALTER COLUMN %I ... ;', attrelid::regclass, attname)
148+
, ''
149+
, format('ALTER TABLE %s RENAME COLUMN %I TO %s;', attrelid::regclass, attname, attname)
150+
, ''
151+
, format('ALTER TABLE %s D_ROP COLUMN %I;', attrelid::regclass, attname)
152+
, ''
153+
, '*/'
154+
)
155+
from pg_attribute
156+
where ('column', attrelid, attnum::text) = ($1, $2, $3)
157+
142158
-- constraint
143159
union all
144160
select concat_ws(e'\n'

0 commit comments

Comments
 (0)