Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 844c45a

Browse files
authored
Add files via upload
1 parent 709fa83 commit 844c45a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎functions/is_uuid.sql‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
create or replace function is_uuid(str text)
2+
returns boolean
3+
immutable
4+
returns null on null input
5+
parallel safe -- Postgres 10 or later
6+
language plpgsql
7+
cost 5
8+
as
9+
$$
10+
begin
11+
--https://postgrespro.ru/docs/postgresql/12/datatype-uuid
12+
if octet_length(str) not between 32 --a0eebc999c0b4ef8bb6d6bb9bd380a11
13+
and 36 --a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
14+
then
15+
return false;
16+
end if;
17+
18+
return (
19+
select s ~ '^[\da-fA-F]{32}$'
20+
and s ~ '\d'
21+
and s ~ '[a-fA-F]'
22+
from replace(str, '-', '') as t(s)
23+
);
24+
end
25+
$$;
26+
27+
comment on function is_uuid(text) is 'Проверяет, что переданная строка является UUID';
28+
29+
--TEST
30+
31+
DO $$
32+
BEGIN
33+
--positive
34+
assert is_uuid('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11');
35+
assert is_uuid('A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11');
36+
assert is_uuid('a0eebc999c0b4ef8bb6d6bb9bd380a11');
37+
38+
--negative
39+
assert not is_uuid('129Z4LOktlhkcG1hURE6Cc5chbSMYl5C');
40+
assert not is_uuid('A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A-11');
41+
42+
END $$;
43+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /