Skip to content

Commit 60ce4d0

Browse files
committed
implemented simple procedure calls
1 parent d877d3c commit 60ce4d0

5 files changed

Lines changed: 60 additions & 4 deletions

File tree

‎python_plsql/client.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,20 @@ def return_type(arguments: List[Argument]):
209209

210210

211211
class Overload:
212-
def __init__(self, arguments):
212+
def __init__(self, arguments: Iterator[Argument]):
213213
self.arguments = list(arguments)
214214

215-
self.return_type = return_type(self.arguments)
215+
self.return_type: Argument = return_type(self.arguments)
216216

217217
self.arguments = [
218218
argument
219219
for argument in self.arguments
220220
if argument.position and argument.datatype
221221
]
222222

223+
def match(self, *args, **kwargs) -> bool:
224+
return (len(args) + len(kwargs)) == len(self.arguments)
225+
223226

224227
def group_by_overload(arguments: Iterator[Argument]) -> List[Overload]:
225228
return [
@@ -236,12 +239,30 @@ def __init__(
236239

237240
self.overloads = group_by_overload(arguments=arguments)
238241

242+
def __call__(self, *args, **kwargs):
243+
if not self._match(*args, **kwargs):
244+
raise ValueError("Arguments do not match subprogram")
245+
246+
with self.plsql._connection.cursor() as cursor:
247+
cursor.callproc(self.resolved.name, *args, **kwargs)
248+
249+
def _matches(self, *args, **kwargs) -> List[Overload]:
250+
return [
251+
overload for overload in self.overloads if overload.match(*args, **kwargs)
252+
]
253+
254+
def _match(self, *args, **kwargs) -> bool:
255+
return bool(self._matches(*args, **kwargs))
256+
239257
@property
240258
def overloaded(self) -> bool:
259+
"""returns true if exists multiple definitions of the same subprogram in a package
260+
only applicable for package procedures/functions where overloading is possible"""
241261
return len(self.overloads) > 1
242262

243263
@property
244264
def standalone(self) -> bool:
265+
"""returns true if procedure/function is not part of a package"""
245266
return self.resolved.object_type in {
246267
ObjectTypes.procedure,
247268
ObjectTypes.function,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CREATE OR REPLACE PROCEDURE simple_procedure IS
2-
v_int INTEGER;
2+
v_int INTEGER DEFAULT 42;
33
BEGIN
4-
v_int := 42;
4+
raise_application_error(-20001, 'You called me :) ' || TO_CHAR(v_int));
55
END;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE OR REPLACE PROCEDURE with_argument(p_int IN INTEGER) IS
2+
v_int INTEGER;
3+
BEGIN
4+
v_int := p_int;
5+
END;

‎tests/test_argument_matching.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
4+
def test_match_with_no_argument(plsql):
5+
assert plsql.simple_procedure._match()
6+
7+
8+
def test_match_with_argument(plsql):
9+
procedure = plsql.with_argument
10+
11+
assert procedure._match(1)
12+
assert procedure._match(p_int=1)
13+
assert not procedure._match()
14+
assert not procedure._match(1, 2)
15+
assert not procedure._match(p_int=1, p_str="2")

‎tests/test_procedure_call.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from python_plsql import DatabaseError
2+
import pytest
3+
4+
5+
def test_simple_procedure_gets_called(plsql):
6+
with pytest.raises(DatabaseError, match="You called me.+42"):
7+
plsql.simple_procedure()
8+
9+
10+
@pytest.mark.parametrize(
11+
"args, kwargs", [([1], {}), ([], {"p_int": 5}), ([1], {"p_int": 5})]
12+
)
13+
def test_incorrect_call_raises_exception(plsql, args, kwargs):
14+
with pytest.raises(ValueError, match="Arguments do not match subprogram"):
15+
plsql.simple_procedure(*args, **kwargs)

0 commit comments

Comments
 (0)