-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sql
207 lines (161 loc) · 4.21 KB
/
functions.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
create schema if not exists test;
create sequence if not exists test.seq_users;
create sequence if not exists test.seq_comments;
create table if not exists test.users
(
id int not null default nextval('test.seq_users'::regclass),
name varchar not null,
email varchar not null,
constraint "PK_users" primary key (id),
constraint "UQ_users_email" unique (email),
constraint "CHK_users_email" check (email like '%@%')
);
create table if not exists test.comments
(
id int not null default nextval('test.seq_comments'::regclass),
id_user int not null,
txt varchar not null,
constraint "PK_comments" primary key (id)
);
create or replace function test.user_get(_id integer)
returns json as
$BODY$
declare
_ret json;
begin
if _id = 0 then
select array_to_json(array(
select row_to_json(r)
from (
select u.id, u.name, u.email
from test.users u
) r
)) into _ret;
else
select row_to_json(r) into _ret
from (
select u.id, u.name, u.email
from test.users u
where id = _id
) r;
end if;
return _ret;
exception when others then
return json_build_object('error', SQLERRM);
end
$BODY$
language plpgsql volatile cost 100;
create or replace function test.user_ins(_params json)
returns json as
$BODY$
declare
_newid integer;
begin
_newid = 0;
insert into test.users (name, email)
select name, email
from json_populate_record(null::test.users, _params)
returning id into _newid;
return json_build_object('id', _newid);
exception when others then
return json_build_object('error', SQLERRM);
end
$BODY$
language plpgsql volatile cost 100;
create or replace function test.user_upd(_id integer, _params json)
returns json as
$BODY$
begin
update test.users set
name = coalesce(_params->>'name', name),
email = coalesce(_params->>'email', email)
where id = _id;
return json_build_object('id', _id);
exception when others then
return json_build_object('error', SQLERRM);
end
$BODY$
language plpgsql volatile cost 100;
create or replace function test.user_del(_id integer)
returns json as
$BODY$
begin
delete from test.users where id = _id;
return json_build_object('id', _id);
exception when others then
raise notice 'Illegal operation: %', SQLERRM;
return json_build_object('error', SQLERRM);
end
$BODY$
language plpgsql volatile cost 100;
create or replace function test.comment_get(_id integer)
returns json as
$BODY$
declare
_ret json;
begin
if _id = 0 then
select array_to_json(array(
select row_to_json(r)
from (
select c.id, c.user_id, c.txt
from test.comments c
) r
)) into _ret;
else
select row_to_json(r) into _ret
from (
select c.id, c.user_id, c.txt
from test.comments c
where id = _id
) r;
end if;
return _ret;
exception when others then
return json_build_object('error', SQLERRM);
end
$BODY$
language plpgsql volatile cost 100;
-- редактирование текста комментария с указанным id. Авторство комментария менять нельзя.
create or replace function test.comment_upd(_id integer, _params json)
returns json as
$BODY$
begin
update test.comments set
txt = _params->>'txt'
where id = _id;
return json_build_object('id', _id);
exception when others then
return json_build_object('error', SQLERRM);
end
$BODY$
language plpgsql volatile cost 100;
create or replace function test.comment_del(_id integer)
returns json as
$BODY$
begin
delete from test.comments where id = _id;
return json_build_object('id', _id);
exception when others then
raise notice 'Illegal operation: %', SQLERRM;
return json_build_object('error', SQLERRM);
end
$BODY$
language plpgsql volatile cost 100;
create or replace function test.user_comment_ins(_id_user integer, _params json)
returns json as
$BODY$
declare
_newid integer;
begin
_newid = 0;
insert into test.comments (id_user, txt)
select _id_user, txt
from json_populate_record(null::test.comments, _params)
returning id into _newid;
return json_build_object('id', _newid);
exception when others then
return json_build_object('error', SQLERRM);
end
$BODY$
language plpgsql volatile cost 100;