Skip to content

Commit 0abde24

Browse files
committed
function call now possible
1 parent f270aaa commit 0abde24

5 files changed

Lines changed: 40 additions & 7 deletions

File tree

‎python_plsql/client.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,25 @@ def __init__(
258258

259259
self.overloads = group_by_overload(arguments=arguments)
260260

261+
def _call_function(self, *args, **kwargs):
262+
with self.plsql._connection.cursor() as cursor:
263+
return cursor.callfunc(self.resolved.name, int, args, kwargs)
264+
265+
def _call_procedure(self, *args, **kwargs):
266+
with self.plsql._connection.cursor() as cursor:
267+
cursor.callproc(self.resolved.name, *args, **kwargs)
268+
261269
def __call__(self, *args, **kwargs):
262270
if not self._match(*args, **kwargs):
263271
raise ValueError("Arguments do not match subprogram")
264272

265-
with self.plsql._connection.cursor() as cursor:
266-
cursor.callproc(self.resolved.name, *args, **kwargs)
273+
if len(self._matches(*args, **kwargs)) > 1:
274+
raise ValueError("Matched with too many subprograms")
275+
276+
if self.is_function:
277+
return self._call_function(*args, **kwargs)
278+
else:
279+
self._call_procedure(*args, **kwargs)
267280

268281
def _matches(self, *args, **kwargs) -> List[Overload]:
269282
return [
@@ -279,6 +292,10 @@ def overloaded(self) -> bool:
279292
only applicable for package procedures/functions where overloading is possible"""
280293
return len(self.overloads) > 1
281294

295+
@property
296+
def is_function(self):
297+
return all(overload.return_type for overload in self.overloads)
298+
282299
@property
283300
def standalone(self) -> bool:
284301
"""returns true if procedure/function is not part of a package"""

‎tests/func/test_function_call.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
from functools import reduce
3+
4+
5+
@pytest.fixture(params=["simple_function", "test_package.simple_function"])
6+
def simple_function(plsql, request):
7+
for access in request.param.split("."):
8+
plsql = getattr(plsql, access)
9+
return plsql
10+
11+
12+
def test_function_returns_value(plsql, simple_function):
13+
assert simple_function(21) == 42
14+
assert simple_function(p_int=21) == 42
15+
assert simple_function(None) is None

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ 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-
FUNCTION simple_function
9+
FUNCTION simple_function(p_int IN INTEGER)
1010
RETURN INTEGER IS
1111
BEGIN
12-
RETURN 1;
12+
RETURN p_int * 2;
1313
END simple_function;
1414

1515
END test_package;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ CREATE OR REPLACE PACKAGE test_package AS
22

33
PROCEDURE simple_procedure;
44

5-
FUNCTION simple_function RETURN INTEGER;
5+
FUNCTION simple_function(p_int IN INTEGER)
6+
RETURN INTEGER;
67

78
END test_package;

‎tests/unit/test_describe_procedure.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def test_describe_procedure_no_arguments(plsql):
3333
assert len(list(result)) == 1
3434

3535

36-
def test_describe_function_no_arguments(plsql):
36+
def test_describe_package_function_with_arguments(plsql):
3737
result = plsql._describe_procedure("test_package.simple_function")
38-
assert len(list(result)) == 1
38+
assert len(list(result)) == 2
3939

4040

4141
def test_describe_function_with_arguments(plsql):

0 commit comments

Comments
 (0)