Skip to content

Commit 9023ac9

Browse files
committed
fixed bug with procedure calling
1 parent ca08069 commit 9023ac9

6 files changed

Lines changed: 53 additions & 43 deletions

File tree

‎python_plsql/client.py‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ def __init__(self, arguments: Iterator[Argument]):
247247
def match(self, *args, **kwargs) -> bool:
248248
return (len(args) + len(kwargs)) == len(self.arguments)
249249

250+
@property
251+
def is_function(self) -> bool:
252+
return bool(self.return_type)
253+
250254

251255
def group_by_overload(arguments: Iterator[Argument]) -> List[Overload]:
252256
return [
@@ -255,6 +259,15 @@ def group_by_overload(arguments: Iterator[Argument]) -> List[Overload]:
255259
] or [Overload(arguments=[])]
256260

257261

262+
def filter_match(matches: Iterator[Overload]) -> Overload:
263+
matches = list(matches)
264+
if len(matches) > 1:
265+
raise ValueError("Arguments matched with too many subprograms")
266+
elif len(matches) == 0:
267+
raise ValueError("Arguments do not match subprogram")
268+
return matches[0]
269+
270+
258271
class Subprogram:
259272
def __init__(
260273
self, plsql: Database, resolved: ResolvedName, arguments: Iterator[Argument]
@@ -266,42 +279,39 @@ def __init__(
266279
)
267280

268281
def _call_function(self, *args, **kwargs):
269-
# match = self._matches(*args, **kwargs)[0]
282+
_ = filter_match(self._function_matches(*args, **kwargs))
270283

271284
with self.plsql._connection.cursor() as cursor:
272285
return cursor.callfunc(self.resolved.name, int, args, kwargs)
273286

274287
def _call_procedure(self, *args, **kwargs):
275-
# match = self._matches(*args, **kwargs)[0]
288+
_ = filter_match(self._procedure_matches(*args, **kwargs))
276289

277290
with self.plsql._connection.cursor() as cursor:
278-
cursor.callproc(self.resolved.name, *args, **kwargs)
291+
cursor.callproc(self.resolved.name, args, kwargs)
279292

280293
def __call__(self, *args, **kwargs):
281-
if (matches := len(self._matches(*args, **kwargs))) > 1:
282-
raise ValueError("Arguments matched with too many subprograms")
283-
elif matches == 0:
284-
raise ValueError("Arguments do not match subprogram")
285-
286-
if self.is_function and self.is_procedure:
294+
if self.overloaded and self.is_function and self.is_procedure:
287295
raise NotImplementedError(
288296
"Subprogram is overloaded as a function and procedure, use _call_(procedure/function) method"
289297
)
290298

291299
if self.is_function:
292300
return self._call_function(*args, **kwargs)
293301
else:
294-
self._call_procedure(*args, **kwargs)
302+
return self._call_procedure(*args, **kwargs)
303+
304+
def _function_matches(self, *args, **kwargs) -> Iterator[Overload]:
305+
return filter(lambda x: x.is_function, self._matches(*args, **kwargs))
295306

296-
def _matches(self, *args, **kwargs) -> List[Overload]:
307+
def _procedure_matches(self, *args, **kwargs) -> Iterator[Overload]:
308+
return filter(lambda x: not x.is_function, self._matches(*args, **kwargs))
309+
310+
def _matches(self, *args, **kwargs) -> Iterator[Overload]:
297311
"""returns overloads that match with provided parameters"""
298-
return [
312+
return (
299313
overload for overload in self.overloads if overload.match(*args, **kwargs)
300-
]
301-
302-
def _match(self, *args, **kwargs) -> bool:
303-
"""returns true if function has"""
304-
return bool(self._matches(*args, **kwargs))
314+
)
305315

306316
@property
307317
def overloaded(self) -> bool:
@@ -311,11 +321,11 @@ def overloaded(self) -> bool:
311321

312322
@property
313323
def is_function(self):
314-
return any(overload.return_type for overload in self.overloads)
324+
return any(overload.is_function for overload in self.overloads)
315325

316326
@property
317327
def is_procedure(self):
318-
return any(not overload.return_type for overload in self.overloads)
328+
return any(not overload.is_function for overload in self.overloads)
319329

320330
@property
321331
def standalone(self) -> bool:

‎tests/func/test_subprograms.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ def test_function_returns_value(plsql, simple_function):
1212
def test_procedure_gets_called(simple_procedure):
1313
with pytest.raises(DatabaseError, match="ORA-20001.+You called me.+42"):
1414
simple_procedure()
15+
16+
17+
@pytest.mark.parametrize(
18+
"args, kwargs, expected",
19+
[(["aaa"], {}, "aaa"), ([], {"p_varchar": "aaa"}, "aaa"), ([None], {}, "NULL")],
20+
)
21+
def test_procedure_with_parameters(plsql, args, kwargs, expected):
22+
with pytest.raises(DatabaseError, match=f"ORA-20002.+p_varchar={expected}"):
23+
plsql.test_package.proc_with_params(*args, **kwargs)

‎tests/sql/package/test_package-b.sql‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ CREATE OR REPLACE PACKAGE BODY test_package AS
66
raise_application_error(-20001, 'You called me ' || TO_CHAR(v_int));
77
END simple_procedure;
88

9+
PROCEDURE proc_with_params(p_varchar IN VARCHAR2) IS
10+
BEGIN
11+
raise_application_error(-20002, 'p_varchar=' || NVL(p_varchar, 'NULL'));
12+
END;
13+
914
FUNCTION simple_function(p_int IN INTEGER)
1015
RETURN INTEGER IS
1116
BEGIN

‎tests/sql/package/test_package-h.sql‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ CREATE OR REPLACE PACKAGE test_package AS
22

33
PROCEDURE simple_procedure;
44

5+
PROCEDURE proc_with_params(p_varchar IN VARCHAR2);
6+
57
FUNCTION simple_function(p_int IN INTEGER)
68
RETURN INTEGER;
79

‎tests/unit/test_argument_matching.py‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎tests/unit/test_standalone_procedures.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ def procedure(plsql, request):
66
return getattr(plsql, request.param)
77

88

9-
def test_procedure_found_in_same_schema(plsql):
10-
procedure = plsql.simple_procedure
9+
def test_procedure_is_found(procedure):
1110
assert procedure.resolved.name == "SIMPLE_PROCEDURE"
1211

1312

14-
def test_procedure_found_with_synonym(plsql):
15-
procedure = plsql.synonym_procedure
16-
assert procedure.resolved.name == "SIMPLE_PROCEDURE"
17-
18-
19-
def test_standalone_procedures_are_not_overloaded(plsql, procedure):
13+
def test_standalone_procedures_are_not_overloaded(procedure):
2014
assert not procedure.overloaded
2115

2216

23-
def test_standalone_procedures_are_standalone(plsql, procedure):
17+
def test_standalone_procedures_are_standalone(procedure):
2418
assert procedure.standalone
19+
20+
21+
def test_procedure_identified_as_such(procedure):
22+
assert procedure.is_procedure
23+
assert not procedure.is_function

0 commit comments

Comments
 (0)