-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy path002-helpers.sql
124 lines (96 loc) · 3.99 KB
/
002-helpers.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
create or replace function endpoint.get_mimetype_id(_mimetype text) returns uuid as $$
select id from endpoint.mimetype where mimetype=_mimetype;
$$ language sql;
create or replace function endpoint.set_mimetype(
_schema name,
_table name,
_column name,
_mimetype text
) returns void as $$
insert into endpoint.column_mimetype (column_id, mimetype_id)
select c.id, m.id
from meta.relation_column c
cross join endpoint.mimetype m
where c.schema_name = _schema and
c.relation_name = _table and
c.name = _column and
m.mimetype = _mimetype
$$ language sql;
/******************************************************************************
* FUNCTION columns_json
*****************************************************************************/
drop type if exists column_type;
create type column_type as (
name text,
"type" text
);
-- returns the columns for a provided schema.relation as a json object
create or replace function endpoint.columns_json(
_schema_name text,
_relation_name text,
exclude text[],
include text[]
) returns json as $$
declare
json json;
begin
execute
'select (''['' || string_agg(row_to_json(row(c2.name, c2.type_name)::endpoint.column_type, true)::text, '','') || '']'')::json
from (select * from meta.relation_column c
where c.schema_name = ' || quote_literal(_schema_name) || ' and
c.relation_name = ' || quote_literal(_relation_name) ||
case when include is not null then
' and c.name = any(' || quote_literal(include) || ')'
else '' end ||
case when exclude is not null then
' and not c.name = any(' || quote_literal(exclude) || ')'
else '' end ||
' order by position) c2'
into json;
end;
$$
language plpgsql;
/****************************************************************************************************
* FUNCTION pk_name *
****************************************************************************************************/
create or replace function pk_name(
_schema_name name,
_relation_name name
) returns text as $$
select c.name
from meta.relation_column c --TODO: either use relation_column maybe? Or go look up the pk of a view somewhere else if we ever add that
where c.schema_name = _schema_name and
c.relation_name = _relation_name and
c.primary_key
$$
language sql security definer;
/****************************************************************************************************
* FUNCTION column_list
****************************************************************************************************/
create or replace function endpoint.column_list(
_schema_name text,
_relation_name text,
table_alias text,
exclude text[],
include text[],
out column_list text
) as $$
begin
if table_alias = '' then
table_alias := _schema_name || '.' || _relation_name;
end if;
execute
'select string_agg(' || quote_literal(table_alias) || ' || ''.'' || name, '', '')
from meta.relation_column
where schema_name = ' || quote_literal(_schema_name) || ' and
relation_name = ' || quote_literal(_relation_name) ||
case when include is not null then
' and name = any(' || quote_literal(include) || ')'
else '' end ||
case when exclude is not null then
' and not name = any(' || quote_literal(exclude) || ')'
else '' end
-- || ' group by position order by position' wrong.
into column_list;
end;
$$ language plpgsql;