-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathphone_serialize.sql
66 lines (59 loc) · 2.92 KB
/
phone_serialize.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
create or replace function public.phone_serialize(
country_code text, --код страны в любом формате или NULL
area_code text, --код зоны в любом формате или NULL
local_number text, --локальный номер телефона в любом формате или NULL
separator text default '(-.-)' -- набор символов в разделителе должен быть таким,
-- чтобы корректно работали функции phone_normalize() и phone_parse()
)
returns text --not null
immutable
--returns null on null input
parallel safe
language sql
set search_path = ''
as
$$
select concat_ws('', country_code, separator, area_code, separator, local_number);
$$;
comment on function public.phone_serialize(
country_code text,
area_code text,
local_number text,
separator text
) is 'Сериализует номер телефона из трёх полей в одно по специальному разделителю';
------------------------------------------------------------------------------------------------------------------------
create or replace function public.phone_serialize(
country_code int, --код страны в любом формате или NULL
area_code text, --код зоны в любом формате или NULL
local_number text, --локальный номер телефона в любом формате или NULL
separator text default '(-.-)' -- набор символов в разделителе должен быть таким,
-- чтобы корректно работали функции phone_normalize() и phone_parse()
)
returns text
immutable
--returns null on null input
parallel safe
language sql
as
$$
select public.phone_serialize(country_code::text, area_code, local_number);
$$;
comment on function public.phone_serialize(
country_code int,
area_code text,
local_number text,
separator text
) is 'Сериализует номер телефона из трёх полей в одно по специальному разделителю';
------------------------------------------------------------------------------------------------------------------------
--TEST
do $$
begin
assert public.phone_serialize(7, '965', '1234567') = '7(-.-)965(-.-)1234567';
assert public.phone_serialize('+7', '965', '1234567') = '+7(-.-)965(-.-)1234567';
assert public.phone_serialize('', '', '') = '(-.-)(-.-)';
assert public.phone_serialize(null::text, null, null) = '(-.-)(-.-)';
assert public.phone_serialize(null::int, null, null) = '(-.-)(-.-)';
assert public.phone_serialize('7', '965', '1234567', '') = '79651234567';
assert public.phone_serialize('7', '965', '1234567', null) = '79651234567';
end;
$$;