Skip to content

Commit 777fb15

Browse files
authored
Add flag to ignore specific procedural languages (#185)
1 parent 9dde38b commit 777fb15

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__
44
.pytest_cache
55
.tox
66
dist
7+
.idea

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ options:
6161
Analyze PLpgSQL code (default: True)
6262
--explain EXPLAIN Describe an error/warning code
6363
--ignore IGNORE Ignore error or warning code
64+
--ignore-lang LANG Ignore unknown procedural language
6465
--sql-accepting SQL_FN
6566
Specify one or more sql-accepting functions
6667
```

src/pgspot/cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ def run():
5454
type=str,
5555
help="Ignore error or warning code",
5656
)
57+
parser.add_argument(
58+
"--ignore-lang",
59+
dest="ignore_lang",
60+
action="append",
61+
default=[],
62+
type=str,
63+
help="Ignore one or more unknown procedural languages",
64+
)
5765
parser.add_argument(
5866
"--sql-accepting",
5967
dest="sql_fn",
@@ -70,6 +78,7 @@ def run():
7078
)
7179

7280
args = parser.parse_args()
81+
args.ignore_lang = [lang.lower() for lang in args.ignore_lang]
7382

7483
counter = Counter(args)
7584
state = State(counter)

src/pgspot/visitors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ def visit_CreateFunctionStmt(self, ancestors, node):
267267
visit_plpgsql(state, node)
268268
case "c" | "internal":
269269
pass
270+
case language if language in self.state.args.ignore_lang:
271+
pass
270272
case _:
271273
self.state.unknown(f"Unknown function language: {language}")
272274

tests/ignore_lang_test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from util import run
2+
3+
4+
sql = """
5+
CREATE FUNCTION python_max(a integer, b integer) RETURNS integer AS $$
6+
return max(a, b)
7+
$$ LANGUAGE plpython3u SET search_path TO 'pg_catalog', 'pg_temp'
8+
;
9+
10+
CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
11+
if {$1 > $2} {return $1}
12+
return $2
13+
$$ LANGUAGE pltcl STRICT SET search_path TO 'pg_catalog', 'pg_temp'
14+
;
15+
"""
16+
17+
18+
def test_unknown_lang_plpython3u():
19+
output = run(sql)
20+
assert "Unknown function language: plpython3u" in output
21+
22+
23+
def test_unknown_lang_pltcl():
24+
output = run(sql)
25+
assert "Unknown function language: pltcl" in output
26+
27+
28+
def test_ignore_lang_plpython3u():
29+
output = run(sql, list("--ignore-lang=plpython3u"))
30+
assert "Unknown function language: plpython3u" not in output
31+
32+
33+
def test_ignore_lang_pltcl():
34+
output = run(sql, list("--ignore-lang=pltcl"))
35+
assert "Unknown function language: pltcl" not in output
36+
37+
38+
def test_ignore_lang_plpython3u_pltcl():
39+
output = run(sql, ["--ignore-lang=plpython3u", "--ignore-lang=pltcl"])
40+
assert (
41+
"Unknown function language: pltcl" not in output
42+
and "Unknown function language: plpython3u" not in output
43+
)
44+
45+
46+
def test_ignore_lang_upper():
47+
output = run(sql, ["--ignore-lang=PLPYTHON3U", "--ignore-lang=PLTCL"])
48+
assert (
49+
"Unknown function language: pltcl" not in output
50+
and "Unknown function language: plpython3u" not in output
51+
)

0 commit comments

Comments
 (0)