1 |
|
%% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
|
16 |
|
-module(pgsqlp). |
17 |
|
|
18 |
|
|
19 |
|
-feature(maybe_expr, enable). |
20 |
|
|
21 |
|
|
22 |
|
-export([name/0]). |
23 |
|
-export([parse/1]). |
24 |
|
-export([priv_consult/1]). |
25 |
|
-export([priv_dir/0]). |
26 |
|
-export([reserved/0]). |
27 |
|
-export([t/0]). |
28 |
|
-export([t/1]). |
29 |
|
-export([to_atom/1]). |
30 |
|
-import(scran_branch, [alt/1]). |
31 |
|
-import(scran_character_complete, [re_no_case/1]). |
32 |
|
-import(scran_combinator, [is_not/1]). |
33 |
|
-import(scran_combinator, [map_parser/2]). |
34 |
|
-import(scran_combinator, [map_result/2]). |
35 |
|
-import(scran_result, [into_atom/1]). |
36 |
|
-import(scran_result, [into_map/1]). |
37 |
|
-import(scran_result, [into_snake_case/1]). |
38 |
|
|
39 |
|
|
40 |
|
priv_dir() -> |
41 |
1 |
code:priv_dir(?MODULE). |
42 |
|
|
43 |
|
|
44 |
|
priv_consult(Filename) -> |
45 |
1 |
phrase_file:consult(filename:join(priv_dir(), Filename)). |
46 |
|
|
47 |
|
|
48 |
|
parse(SQL) -> |
49 |
15 |
maybe |
50 |
15 |
{_, Result} ?= (into_map( |
51 |
|
alt([pgsqlp_select:expression(), |
52 |
|
pgsqlp_copy:expression(), |
53 |
|
pgsqlp_tx:expression(), |
54 |
|
pgsqlp_replication:expression()])))(SQL), |
55 |
|
|
56 |
15 |
Result |
57 |
|
end. |
58 |
|
|
59 |
|
|
60 |
|
t(Parser) -> |
61 |
83 |
fun |
62 |
|
(Input) -> |
63 |
29 |
(map_result( |
64 |
|
to_atom(Parser), |
65 |
|
t()))(Input) |
66 |
|
end. |
67 |
|
|
68 |
|
|
69 |
|
t() -> |
70 |
29 |
fun |
71 |
|
(Value) -> |
72 |
12 |
{Value, true} |
73 |
|
end. |
74 |
|
|
75 |
|
|
76 |
|
to_atom(Parser) -> |
77 |
296 |
fun |
78 |
|
(Input) -> |
79 |
149 |
(into_atom(into_snake_case(Parser)))(Input) |
80 |
|
end. |
81 |
|
|
82 |
|
|
83 |
|
name() -> |
84 |
6 |
fun |
85 |
|
(Input) -> |
86 |
4 |
(map_parser( |
87 |
|
re_no_case("[a-z_]+"), |
88 |
|
is_not( |
89 |
|
pgsqlp:reserved())))(Input) |
90 |
|
end. |
91 |
|
|
92 |
|
|
93 |
|
reserved() -> |
94 |
2002 |
fun |
95 |
|
(Input) -> |
96 |
1365 |
(pgsqlp_keyword:by_category(reserved_keyword))(Input) |
97 |
|
end. |