Skip to content

Commit 1db043c

Browse files
committed
in/out, out parameters now supported by functions
1 parent b979649 commit 1db043c

4 files changed

Lines changed: 49 additions & 16 deletions

File tree

‎plsql/client.py‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,33 @@ def __init__(self, name, plsql):
8484

8585
def __call__(self, **kwargs):
8686
with self.plsql.connection.cursor() as cursor:
87-
if self.definition.object_type == FUNCTION:
88-
return cursor.callfunc(self.name, self.return_type, keywordParameters=kwargs)
89-
elif self.definition.object_type == PROCEDURE:
9087

91-
in_out_parameters = [
92-
(argument.argument_name.lower(), cursor.var(self.argument_mapping[argument.data_type]))
93-
for argument
94-
in self.arguments
95-
if 'OUT' in argument.in_out
96-
]
88+
in_out_parameters = [
89+
(argument.argument_name.lower(), cursor.var(self.argument_mapping[argument.data_type]))
90+
for argument
91+
in self.arguments
92+
if 'OUT' in argument.in_out
93+
if argument.argument_name is not None
94+
]
9795

98-
for name, parameter in in_out_parameters:
99-
try:
100-
parameter.setvalue(0, kwargs[name])
101-
except KeyError:
102-
pass
103-
kwargs[name] = parameter
96+
for name, parameter in in_out_parameters:
97+
try:
98+
parameter.setvalue(0, kwargs[name])
99+
except KeyError:
100+
pass
101+
kwargs[name] = parameter
104102

103+
if self.definition.object_type == FUNCTION:
104+
result = cursor.callfunc(self.name, self.return_type, keywordParameters=kwargs)
105+
106+
if in_out_parameters:
107+
return result, {
108+
name: parameter.getvalue()
109+
for name, parameter
110+
in in_out_parameters
111+
}
112+
return result
113+
elif self.definition.object_type == PROCEDURE:
105114
cursor.callproc(self.name, keywordParameters=kwargs)
106115

107116
if in_out_parameters:
@@ -110,7 +119,6 @@ def __call__(self, **kwargs):
110119
for name, parameter
111120
in in_out_parameters
112121
}
113-
114122
else:
115123
raise NotImplementedError(f'Unrecognized object_type "{self.definition.object_type}"!')
116124

‎tests/conftest.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ def procedure_out_params(plsql):
5757
def procedure_in_out_params(plsql):
5858
with managed_object(plsql, 'procedure', 'procedure_in_out_params'):
5959
yield
60+
61+
62+
@pytest.fixture(scope='module')
63+
def function_in_out(plsql):
64+
with managed_object(plsql, 'function', 'function_in_out'):
65+
yield
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE OR REPLACE FUNCTION function_in_out(p_int IN INTEGER,
2+
p_float OUT NUMBER,
3+
p_str IN OUT VARCHAR2)
4+
RETURN INTEGER IS
5+
v_int INTEGER;
6+
BEGIN
7+
v_int := p_int * 2;
8+
p_float := p_int / 2;
9+
p_str := p_str || ': ' || TO_CHAR(p_int * 2);
10+
RETURN v_int;
11+
END;

‎tests/test_functions.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ def test_function_with_defaults(plsql, function_with_defaults):
1212

1313
def test_string_function(plsql, string_function):
1414
assert plsql.string_function(p_str='test') == 'testtest'
15+
16+
17+
def test_function_in_out(plsql, function_in_out):
18+
result, in_out = plsql.function_in_out(p_int=21, p_str='input')
19+
20+
assert result == 42
21+
assert in_out['p_str'] == 'input: 42'
22+
assert in_out['p_float'] == 10.5

0 commit comments

Comments
 (0)