Skip to content

Commit 157837d

Browse files
committed
fixed memory errors for in params
1 parent 3785276 commit 157837d

3 files changed

Lines changed: 93 additions & 79 deletions

File tree

‎python_plsql/client.py‎

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def to_python(value):
274274
if isinstance(value, oracle.Object):
275275
if value.type.attributes:
276276
return record_out_converter(value)
277-
if is_list_type(value.type):
277+
elif is_list(value.type):
278278
return list_out_converter(value)
279279
return dict_out_coverter(value)
280280
return value
@@ -330,34 +330,35 @@ def type_name(plsql: Database, resolved: ResolvedName, plsql_type: Argument):
330330
return ".".join(argument_type_name)
331331

332332

333-
def in_converter(object_type):
333+
def convert(object_type, value):
334+
if object_type:
335+
return make_in_converter(object_type)(value)
336+
return value
337+
338+
339+
def make_in_converter(object_type):
334340
def _in_converter(value):
335341
var = object_type.newobject()
336342

337-
for attribute, val in zip(object_type.attributes, value):
338-
# todo: self made change to ObjectAttr.c (cx_Oracle should implement this)
339-
if attribute.type:
340-
val = in_converter(attribute.type)(val)
341-
setattr(var, attribute.name, val)
342-
343343
if object_type.iscollection:
344-
if is_list_type(object_type):
344+
if is_list(object_type):
345345
for val in value:
346-
if object_type.elementType:
347-
val = in_converter(object_type.elementType)(val)
348-
var.append(val)
346+
var.append(convert(object_type.elementType, val))
349347
else:
350348
for key, val in value.items():
351-
if object_type.elementType:
352-
val = in_converter(object_type.elementType)(val)
353-
var.setelement(key, val)
349+
var.setelement(key, convert(object_type.elementType, val))
350+
else:
351+
for attribute, val in zip(object_type.attributes, value):
352+
# todo: self made change to ObjectAttr.c (cx_Oracle should implement this)
353+
# type is not accessible by default..
354+
setattr(var, attribute.name, convert(attribute.type, val))
354355

355356
return var
356357

357358
return _in_converter
358359

359360

360-
def is_list_type(object_type: oracle.ObjectType) -> bool:
361+
def is_list(object_type: oracle.ObjectType) -> bool:
361362
# todo: should ask cx_Oracle if there is a better way
362363
obj = object_type.newobject()
363364
try:
@@ -368,6 +369,32 @@ def is_list_type(object_type: oracle.ObjectType) -> bool:
368369
return False
369370

370371

372+
class SimpleVar:
373+
def __init__(self, cursor, object_type):
374+
self.cursor, self.object_type = cursor, object_type
375+
376+
def to_oracle(self, value):
377+
var = self.cursor.var(self.object_type)
378+
var.setvalue(0, value)
379+
return var
380+
381+
@staticmethod
382+
def from_oracle(var):
383+
return var
384+
385+
386+
class UserVar:
387+
def __init__(self, in_converter):
388+
self.in_converter = in_converter
389+
390+
def to_oracle(self, value):
391+
return self.in_converter(value)
392+
393+
@staticmethod
394+
def from_oracle(value):
395+
return to_python(value)
396+
397+
371398
def cursor_var(
372399
plsql: Database, cursor: oracle.Cursor, resolved: ResolvedName, plsql_type: Argument
373400
):
@@ -387,23 +414,22 @@ def cursor_var(
387414
}
388415

389416
try:
390-
return cursor.var(plsql_types[plsql_type.datatype])
417+
object_type = plsql_types[plsql_type.datatype]
391418
except KeyError:
392-
out_converters = {
393-
122: list_out_converter, # nested table
394-
123: list_out_converter, # varray
395-
250: record_out_converter, # plsql record
396-
251: dict_out_coverter, # plsql table
397-
}
419+
# nested table # varray # plsql record # plsql table
420+
if plsql_type.datatype not in {122, 123, 250, 251}:
421+
raise
398422

399423
object_type = plsql._connection.gettype(
400424
type_name(plsql=plsql, resolved=resolved, plsql_type=plsql_type)
401425
)
402-
return cursor.var(
426+
427+
return (
403428
object_type,
404-
outconverter=out_converters[plsql_type.datatype],
405-
inconverter=in_converter(object_type),
429+
UserVar(make_in_converter(object_type)),
406430
)
431+
else:
432+
return object_type, SimpleVar(cursor, object_type)
407433

408434

409435
def find_match(
@@ -434,17 +460,20 @@ def wrapper(self: Subprogram, *positional, **named):
434460

435461
wrapped_positional = []
436462
for value, argument in zip(positional, match.arguments):
437-
pos = cursor_var(self._plsql, cursor, self.resolved, argument)
438-
pos.setvalue(0, value)
439-
wrapped_positional.append(pos)
463+
_, converter = cursor_var(
464+
self._plsql, cursor, self.resolved, argument
465+
)
466+
467+
wrapped_positional.append(converter.to_oracle(value))
440468

441469
wrapped_named = {}
442470
for key, value in named.items():
443471
argument = match.argument(key)
444472
if argument:
445-
nam = cursor_var(self._plsql, cursor, self.resolved, argument)
446-
nam.setvalue(0, value)
447-
wrapped_named[key] = nam
473+
_, converter = cursor_var(
474+
self._plsql, cursor, self.resolved, argument
475+
)
476+
wrapped_named[key] = converter.to_oracle(value)
448477

449478
result = func(self, match, cursor, wrapped_positional, wrapped_named)
450479

@@ -468,18 +497,16 @@ def __init__(
468497

469498
@translate_arguments(match_filter=lambda x: x.is_function)
470499
def _call_function(self, match: Overload, cursor, positional, named):
471-
return cursor.callfunc(
472-
self.resolved.name,
473-
cursor_var(
474-
plsql=self._plsql,
475-
cursor=cursor,
476-
resolved=self.resolved,
477-
plsql_type=match.return_type,
478-
),
479-
positional,
480-
named,
500+
object_type, converter = cursor_var(
501+
plsql=self._plsql,
502+
cursor=cursor,
503+
resolved=self.resolved,
504+
plsql_type=match.return_type,
481505
)
482506

507+
result = cursor.callfunc(self.resolved.name, object_type, positional, named)
508+
return converter.from_oracle(result)
509+
483510
@translate_arguments(match_filter=lambda x: not x.is_function)
484511
def _call_procedure(self, match: Overload, cursor, positional, named):
485512
_ = match

‎tests/func/test_in_param.py‎

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,12 @@
5050
("in_varray", VARRAY),
5151
("in_plsql_table", PLSQL_TABLE),
5252
("in_plsql_table_of_records", {i: RECORD for i in range(1, 11)}),
53-
pytest.param(
54-
"in_varray_of_plsql_table",
55-
[PLSQL_TABLE] * 10,
56-
marks=pytest.mark.skip(reason="Memory access violation"),
57-
),
58-
pytest.param(
59-
"in_varray_of_nested",
60-
[NESTED] * 10,
61-
marks=pytest.mark.skip(reason="Memory access violation"),
62-
),
63-
pytest.param(
64-
"in_plsql_table_of_nested",
65-
{i: NESTED for i in range(1, 11)},
66-
marks=pytest.mark.skip(reason="Memory access violation"),
67-
),
68-
pytest.param(
69-
"in_nested_of_nested",
70-
[NESTED] * 10,
71-
marks=pytest.mark.skip(reason="Memory access violation"),
72-
),
73-
pytest.param(
74-
"in_nested_of_plsql_table",
75-
[PLSQL_TABLE] * 10,
76-
marks=pytest.mark.skip(reason="Memory access violation"),
77-
),
78-
pytest.param(
79-
"in_plsql_table_of_plsql_table",
80-
{i: PLSQL_TABLE for i in range(1, 11)},
81-
marks=pytest.mark.skip(reason="Memory access violation"),
82-
),
53+
("in_varray_of_plsql_table", [PLSQL_TABLE] * 10,),
54+
("in_varray_of_nested", [NESTED] * 10,),
55+
("in_plsql_table_of_nested", {i: NESTED for i in range(1, 11)},),
56+
("in_nested_of_nested", [NESTED] * 10,),
57+
("in_nested_of_plsql_table", [PLSQL_TABLE] * 10,),
58+
("in_plsql_table_of_plsql_table", {i: PLSQL_TABLE for i in range(1, 11)},),
8359
],
8460
)
8561
def test_return_values(plsql, func, in_value):

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,18 @@ CREATE OR REPLACE PACKAGE BODY test_in_params AS
158158

159159
PROCEDURE in_nested_of_nested(p_param IN test_return.tt_nested_of_nested) IS
160160
BEGIN
161-
err(TRUE);
162161
err(p_param.COUNT != test_return.ret_nested_of_nested().COUNT);
162+
FOR v_i IN p_param.FIRST .. p_param.LAST LOOP
163+
in_nested(p_param(v_i));
164+
END LOOP;
163165
END in_nested_of_nested;
164166

165167
PROCEDURE in_nested_of_plsql_table(p_param IN test_return.tt_nested_of_plsql_table) IS
166168
BEGIN
167-
err(TRUE);
168169
err(p_param.COUNT != test_return.ret_nested_of_plsql_table().COUNT);
170+
FOR v_i IN p_param.FIRST .. p_param.LAST LOOP
171+
in_plsql_table(p_param(v_i));
172+
END LOOP;
169173
END in_nested_of_plsql_table;
170174

171175
PROCEDURE in_nested_of_record_of_nested(p_param IN test_return.tt_nested_of_record_of_nested) IS
@@ -188,14 +192,18 @@ CREATE OR REPLACE PACKAGE BODY test_in_params AS
188192

189193
PROCEDURE in_varray_of_nested(p_param IN test_return.tt_varray_of_nested) IS
190194
BEGIN
191-
err(TRUE);
192195
err(p_param.COUNT != test_return.ret_varray_of_nested().COUNT);
196+
FOR v_i IN p_param.FIRST .. p_param.LAST LOOP
197+
in_nested(p_param(v_i));
198+
END LOOP;
193199
END in_varray_of_nested;
194200

195201
PROCEDURE in_varray_of_plsql_table(p_param IN test_return.tt_varray_of_plsql_table) IS
196202
BEGIN
197-
err(TRUE);
198203
err(p_param.COUNT != test_return.ret_varray_of_plsql_table().COUNT);
204+
FOR v_i IN p_param.FIRST .. p_param.LAST LOOP
205+
in_plsql_table(p_param(v_i));
206+
END LOOP;
199207
END in_varray_of_plsql_table;
200208

201209
-- 178 TIME -- datetime.datetime
@@ -228,7 +236,6 @@ CREATE OR REPLACE PACKAGE BODY test_in_params AS
228236
PROCEDURE in_record_of_records(p_param IN test_return.tr_record_of_records) IS
229237
vr_record_of_records test_return.tr_record_of_records := test_return.ret_record_of_records();
230238
BEGIN
231-
-- err(TRUE);
232239
err(p_param.t_int_1 != vr_record_of_records.t_int_1);
233240
in_record(p_param.t_rec_2);
234241
in_record(p_param.t_rec_3);
@@ -270,14 +277,18 @@ CREATE OR REPLACE PACKAGE BODY test_in_params AS
270277

271278
PROCEDURE in_plsql_table_of_nested(p_param IN test_return.tt_plsql_table_of_nested) IS
272279
BEGIN
273-
err(TRUE);
274280
err(p_param.COUNT != test_return.ret_plsql_table_of_nested().COUNT);
281+
FOR v_i IN p_param.FIRST .. p_param.LAST LOOP
282+
in_nested(p_param(v_i));
283+
END LOOP;
275284
END in_plsql_table_of_nested;
276285

277286
PROCEDURE in_plsql_table_of_plsql_table(p_param IN test_return.tt_plsql_table_of_plsql_table) IS
278287
BEGIN
279-
err(TRUE);
280288
err(p_param.COUNT != test_return.ret_plsql_table_of_plsql_table().COUNT);
289+
FOR v_i IN p_param.FIRST .. p_param.LAST LOOP
290+
in_plsql_table(p_param(v_i));
291+
END LOOP;
281292
END in_plsql_table_of_plsql_table;
282293

283294
-- 252 PL/SQL BOOLEAN -- bool

0 commit comments

Comments
 (0)