-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql-helpers.sql
More file actions
287 lines (237 loc) · 5.2 KB
/
Copy pathsql-helpers.sql
File metadata and controls
287 lines (237 loc) · 5.2 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
create or replace function public.json_is_array(j json) returns boolean
language plpgsql
as
$$
BEGIN
RETURN exists(SELECT regexp_matches(j :: VARCHAR, '\[.*\]'));
END;
$$;
create or replace function public.json_is_array(j character varying) returns boolean
language plpgsql
as
$$
BEGIN
RETURN exists(SELECT regexp_matches(j, '\[.*\]'));
END;
$$;
create or replace function public.json_is_empty(_json json) returns boolean
language plpgsql
as
$$
BEGIN
return NOT EXISTS (SELECT 1 FROM json_object_keys(_json));
END;
$$;
create or replace function public.json_array_values_as_text(_j json, _key text) returns text[]
immutable
language sql
as
$$
SELECT array_agg(elem->>_key)
FROM json_array_elements(_j) AS x(elem)
$$;
create or replace function public.jsonb_array_values_as_text(_j jsonb, _key text) returns text[]
immutable
language sql
as
$$
SELECT array_agg(elem->>_key)
FROM jsonb_array_elements(_j) AS x(elem)
$$;
create or replace function public.jsonb_is_array(j jsonb) returns boolean
language plpgsql
as
$$
BEGIN
RETURN exists(SELECT regexp_matches(j :: VARCHAR, '\[.*\]'));
END;
$$;
create or replace function public.require(params json, name text) returns void
language plpgsql
as
$$
BEGIN
if params->>name is null then raise exception '%: поле пустое', name USING ERRCODE='P0001'; end if;
END;
$$;
create or replace function public.requireBigint(params json, key text) returns bigint
language plpgsql
as
$$
BEGIN
perform public.require(params,key);
return params->>key;
END;
$$;
create or replace function public.requireBoolean(params json, key text) returns boolean
language plpgsql
as
$$
BEGIN
perform public.require(params,key);
return coalesce((params->>key)::BOOLEAN,false);
END;
$$;
create or replace function public.requireDate(params json, key text) returns timestamp without time zone
language plpgsql
as
$$
BEGIN
perform public.require(params,key);
return (params->>key)::TIMESTAMP;
END;
$$;
create or replace function public.requireDouble(params json, key text) returns double precision
language plpgsql
as
$$
BEGIN
perform public.require(params,key);
return (params->>key)::double precision;
END;
$$;
create or replace function public.requireInt(params json, key text) returns integer
language plpgsql
as
$$
BEGIN
perform public.require(params,key);
return params->>key;
END;
$$;
create or replace function public.requireJson(params json, key text) returns json
language plpgsql
as
$$
BEGIN
perform public.require(params,key);
return (params->>key)::json;
END;
$$;
create or replace function public.requireJsonb(params json, key text) returns jsonb
language plpgsql
as
$$
BEGIN
perform public.require(params,key);
return (params->>key)::jsonb;
END;
$$;
create or replace function public.requireJsonb(params jsonb, key text) returns jsonb
language plpgsql
as
$$
BEGIN
perform public.require(params::json, key);
return (params->>key)::jsonb;
END;
$$;
create or replace function public.requireNumeric(params json, key text) returns numeric
language plpgsql
as
$$
BEGIN
perform public.require(params,key);
return (params->>key)::NUMERIC(10,2);
END;
$$;
create or replace function public.requireText(params json, key text) returns text
language plpgsql
as
$$
BEGIN
perform public.require(params,key);
return trim(params->>key);
END;
$$;
create or replace function public.optionalBigint(params json, key text) returns bigint
language plpgsql
as
$$
BEGIN
return params->>key;
END;
$$;
create or replace function public.optionalBoolean(params json, key text) returns boolean
language plpgsql
as
$$
BEGIN
return coalesce((params->>key)::BOOLEAN,false);
END;
$$;
create or replace function public.optionalDate(params json, key text) returns timestamp without time zone
language plpgsql
as
$$
BEGIN
return (params->>key)::TIMESTAMP;
END;
$$;
create or replace function public.optionalDouble(params json, key text) returns double precision
language plpgsql
as
$$
BEGIN
return (params->>key)::double precision;
END;
$$;
create or replace function public.optionalInt(params json, key text) returns integer
language plpgsql
as
$$
BEGIN
return params->>key;
END;
$$;
create or replace function public.optionalJson(params json, key text) returns json
language plpgsql
as
$$
BEGIN
return (params->>key)::json;
END;
$$;
create or replace function public.optionalJsonb(params json, key text) returns jsonb
language plpgsql
as
$$
BEGIN
return (params->>key)::jsonb;
END;
$$;
create or replace function public.optionalJsonb(params jsonb, key text) returns jsonb
language plpgsql
as
$$
BEGIN
return (params->>key)::jsonb;
END;
$$;
create or replace function public.optionalNumeric(params json, key text) returns numeric
language plpgsql
as
$$
BEGIN
return (params->>key)::NUMERIC(10,2);
END;
$$;
create or replace function public.optionalText(params json, key text) returns text
language plpgsql
as
$$
BEGIN
return trim(params->>key);
END;
$$;
create or replace function public.optionalUuid(params json, key text, _default uuid) returns uuid
language plpgsql
as
$$
DECLARE
_val uuid;
BEGIN
_val = params ->> key;
return coalesce(_val, _default);
END;
$$;