-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathpath_parse.sql
86 lines (81 loc) · 3.92 KB
/
path_parse.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
create or replace function public.path_parse(
path text,
root out text,
dir out text,
base out text,
name out text,
ext out text
)
/*
The function returns an object whose properties represent significant elements of the path.
The returned record will have the following properties:
dir text
root text
base text
name text
ext text
Compatible with https://nodejs.org/api/path.html#pathparsepath
*/
returns record
immutable
returns null on null input
parallel safe
language sql
set search_path = ''
cost 5
as
$$
select coalesce(case when left(m[1], 1) = '/' then '/' else '' end, '') as root,
coalesce(case when m[1] = '/' then '/' else rtrim(m[1], '\/') end, '') as dir,
coalesce(m[2], '') as base,
coalesce(m[3], '') as name,
coalesce(m[4], '') as ext
from regexp_match(
path,
$regexp$
^
( #1 dir
(?:[^":;*?<>|])*
[\\/]
)?
( #2 base
( #3 name
\.?
(?:
(?!\.[^.":;*?<>|\\/]+$)
[^":;*?<>|\\/]
)*
)
(\. #4 ext
[^.":;*?<>|\\/]+
)?
)
$
$regexp$,
'x'
) as m;
$$;
-- TEST
do $$
begin
--positive
-- проверка парсинга директорий
assert (select to_json(t)::text = '{"root":"","dir":"","base":"","name":"","ext":""}' from public.path_parse('') as t);
assert (select to_json(t)::text = '{"root":"/","dir":"/","base":"","name":"","ext":""}' from public.path_parse('/') as t);
assert (select to_json(t)::text = '{"root":"/","dir":"/","base":"file","name":"file","ext":""}' from public.path_parse('/file') as t);
assert (select to_json(t)::text = '{"root":"/","dir":"/dir","base":"file","name":"file","ext":""}' from public.path_parse('/dir/file') as t);
assert (select to_json(t)::text = '{"root":"/","dir":"/dir/to","base":"file","name":"file","ext":""}' from public.path_parse('/dir/to/file') as t);
assert (select to_json(t)::text = '{"root":"","dir":"dir","base":"file","name":"file","ext":""}' from public.path_parse('dir/file') as t);
assert (select to_json(t)::text = '{"root":"","dir":"dir/to","base":"file","name":"file","ext":""}' from public.path_parse('dir/to/file') as t);
-- проверка парсинга файлов
assert (select to_json(t)::text = '{"root":"","dir":"","base":"файл","name":"файл","ext":""}' from public.path_parse('файл') as t);
assert (select to_json(t)::text = '{"root":"","dir":"","base":"файл.ext","name":"файл","ext":".ext"}' from public.path_parse('файл.ext') as t);
assert (select to_json(t)::text = '{"root":"","dir":"","base":"файл.tar.gz","name":"файл.tar","ext":".gz"}' from public.path_parse('файл.tar.gz') as t);
assert (select to_json(t)::text = '{"root":"","dir":"","base":".file.tar.gz","name":".file.tar","ext":".gz"}' from public.path_parse('.file.tar.gz') as t);
assert (select to_json(t)::text = '{"root":"","dir":"","base":".file","name":".file","ext":""}' from public.path_parse('.file') as t);
assert (select to_json(t)::text = '{"root":"","dir":"","base":"..file","name":".","ext":".file"}' from public.path_parse('..file') as t);
assert (select to_json(t)::text = '{"root":"","dir":"","base":".file.ext","name":".file","ext":".ext"}' from public.path_parse('.file.ext') as t);
--negative
assert (select to_json(t)::text = '{"root":"","dir":"","base":"","name":"","ext":""}' from public.path_parse('www.ru/office-rent/index.php?id=30') as t);
end
$$;