Skip to content

Commit 3785276

Browse files
committed
in param conversions fleshed out
1 parent bc28eac commit 3785276

5 files changed

Lines changed: 216 additions & 139 deletions

File tree

‎python_plsql/client.py‎

Lines changed: 87 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ def __init__(self, arguments: Iterator[Argument]):
248248
def match(self, *args, **kwargs) -> bool:
249249
return (len(args) + len(kwargs)) == len(self.arguments)
250250

251+
def argument(self, name: str) -> Optional[Argument]:
252+
for argument in self.arguments:
253+
if argument.argument_name == name.upper():
254+
return argument
255+
251256
@property
252257
def is_function(self) -> bool:
253258
return bool(self.return_type)
@@ -269,7 +274,9 @@ def to_python(value):
269274
if isinstance(value, oracle.Object):
270275
if value.type.attributes:
271276
return record_out_converter(value)
272-
return list_out_converter(value) # todo: list/dict
277+
if is_list_type(value.type):
278+
return list_out_converter(value)
279+
return dict_out_coverter(value)
273280
return value
274281

275282

@@ -285,8 +292,7 @@ def list_out_converter(value):
285292
if attributes:
286293
record = plsql_record(attributes)
287294
return [to_record(val, record) for val in value.aslist()]
288-
# todo: list/dict
289-
return [list_out_converter(val) for val in value.aslist()]
295+
return [to_python(val) for val in value.aslist()]
290296
return value.aslist()
291297

292298

@@ -296,8 +302,7 @@ def dict_out_coverter(value):
296302
if attributes:
297303
record = plsql_record(attributes)
298304
return {key: to_record(val, record) for key, val in value.asdict().items()}
299-
# todo: list/dict
300-
return {key: dict_out_coverter(val) for key, val in value.asdict().items()}
305+
return {key: to_python(val) for key, val in value.asdict().items()}
301306
return value.asdict()
302307

303308

@@ -325,20 +330,58 @@ def type_name(plsql: Database, resolved: ResolvedName, plsql_type: Argument):
325330
return ".".join(argument_type_name)
326331

327332

328-
def python_type(
333+
def in_converter(object_type):
334+
def _in_converter(value):
335+
var = object_type.newobject()
336+
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+
343+
if object_type.iscollection:
344+
if is_list_type(object_type):
345+
for val in value:
346+
if object_type.elementType:
347+
val = in_converter(object_type.elementType)(val)
348+
var.append(val)
349+
else:
350+
for key, val in value.items():
351+
if object_type.elementType:
352+
val = in_converter(object_type.elementType)(val)
353+
var.setelement(key, val)
354+
355+
return var
356+
357+
return _in_converter
358+
359+
360+
def is_list_type(object_type: oracle.ObjectType) -> bool:
361+
# todo: should ask cx_Oracle if there is a better way
362+
obj = object_type.newobject()
363+
try:
364+
obj.trim(1)
365+
except oracle.DatabaseError:
366+
return True
367+
else:
368+
return False
369+
370+
371+
def cursor_var(
329372
plsql: Database, cursor: oracle.Cursor, resolved: ResolvedName, plsql_type: Argument
330373
):
331374
plsql_types = {
332375
1: oracle.STRING,
333376
2: oracle.NUMBER,
334377
3: oracle.NATIVE_INT,
335378
8: oracle.LONG_STRING, # LONG
336-
11: oracle.ROWID,
379+
11: oracle.STRING, # ROWID
337380
12: oracle.DATETIME,
338381
23: oracle.BINARY,
339382
24: oracle.LONG_BINARY,
340-
96: oracle.STRING, # oracle.FIXED_CHAR
341-
112: oracle.STRING, # oracle.CLOB
383+
96: oracle.STRING, # FIXED_CHAR
384+
112: oracle.STRING, # CLOB
342385
180: oracle.TIMESTAMP,
343386
252: oracle.BOOLEAN,
344387
}
@@ -353,37 +396,57 @@ def python_type(
353396
251: dict_out_coverter, # plsql table
354397
}
355398

356-
out_converter = out_converters[plsql_type.datatype]
357-
358399
object_type = plsql._connection.gettype(
359400
type_name(plsql=plsql, resolved=resolved, plsql_type=plsql_type)
360401
)
361-
return cursor.var(object_type, outconverter=out_converter)
402+
return cursor.var(
403+
object_type,
404+
outconverter=out_converters[plsql_type.datatype],
405+
inconverter=in_converter(object_type),
406+
)
362407

363408

364-
def find_matches(overloads, *positional, **named) -> Iterator[Overload]:
365-
return (overload for overload in overloads if overload.match(*positional, **named))
409+
def find_match(
410+
overloads: List[Overload], filter_function, *positional, **named
411+
) -> Overload:
366412

413+
matches = [
414+
overload
415+
for overload in overloads
416+
if overload.match(*positional, **named)
417+
if filter_function(overload)
418+
]
367419

368-
def filter_matches(filter_function, matches: Iterator[Overload]) -> Overload:
369-
matches = list(filter(filter_function, matches))
370420
if len(matches) > 1:
371421
raise ValueError("Arguments matched with too many subprograms")
372422
elif len(matches) == 0:
373423
raise ValueError("Arguments do not match subprogram")
374424
return matches[0]
375425

376426

377-
def translate_arguments(match_function):
427+
def translate_arguments(match_filter):
378428
def decorator(func):
379429
@wraps(func)
380430
def wrapper(self: Subprogram, *positional, **named):
381-
match = filter_matches(
382-
match_function, find_matches(self.overloads, *positional, **named)
383-
)
431+
match = find_match(self.overloads, match_filter, *positional, **named)
384432

385433
with self._plsql._connection.cursor() as cursor:
386-
result = func(self, match, cursor, positional, named)
434+
435+
wrapped_positional = []
436+
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)
440+
441+
wrapped_named = {}
442+
for key, value in named.items():
443+
argument = match.argument(key)
444+
if argument:
445+
nam = cursor_var(self._plsql, cursor, self.resolved, argument)
446+
nam.setvalue(0, value)
447+
wrapped_named[key] = nam
448+
449+
result = func(self, match, cursor, wrapped_positional, wrapped_named)
387450

388451
if match.is_function:
389452
return result
@@ -403,11 +466,11 @@ def __init__(
403466
group_by_overload(arguments=arguments),
404467
)
405468

406-
@translate_arguments(match_function=lambda x: x.is_function)
469+
@translate_arguments(match_filter=lambda x: x.is_function)
407470
def _call_function(self, match: Overload, cursor, positional, named):
408471
return cursor.callfunc(
409472
self.resolved.name,
410-
python_type(
473+
cursor_var(
411474
plsql=self._plsql,
412475
cursor=cursor,
413476
resolved=self.resolved,
@@ -417,7 +480,7 @@ def _call_function(self, match: Overload, cursor, positional, named):
417480
named,
418481
)
419482

420-
@translate_arguments(match_function=lambda x: not x.is_function)
483+
@translate_arguments(match_filter=lambda x: not x.is_function)
421484
def _call_procedure(self, match: Overload, cursor, positional, named):
422485
_ = match
423486
cursor.callproc(self.resolved.name, positional, named)

‎tests/func/test_in_param.py‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@
3535
("in_character", "42"),
3636
("in_bool", True),
3737
("in_record", RECORD),
38-
("in_record_of_records", (42, RECORD, RECORD)),
38+
pytest.param(
39+
"in_record_of_records",
40+
(42, RECORD, RECORD),
41+
marks=pytest.mark.skip(
42+
reason="OCI-21602: operation does not support the specified typecode"
43+
),
44+
),
3945
("in_record_of_nested", (42, NESTED, NESTED)),
4046
("in_record_of_plsql_table", (42, PLSQL_TABLE, PLSQL_TABLE),),
4147
("in_nested", NESTED),
@@ -44,12 +50,36 @@
4450
("in_varray", VARRAY),
4551
("in_plsql_table", PLSQL_TABLE),
4652
("in_plsql_table_of_records", {i: RECORD for i in range(1, 11)}),
47-
("in_varray_of_plsql_table", [PLSQL_TABLE] * 10,),
48-
("in_varray_of_nested", [NESTED] * 10,),
49-
("in_plsql_table_of_nested", {i: NESTED for i in range(1, 11)},),
50-
("in_nested_of_nested", [NESTED] * 10,),
51-
("in_nested_of_plsql_table", [PLSQL_TABLE] * 10,),
52-
("in_plsql_table_of_plsql_table", {i: PLSQL_TABLE 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+
),
5383
],
5484
)
5585
def test_return_values(plsql, func, in_value):

‎tests/func/test_return_values.py‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@
3737
("ret_record", RECORD),
3838
("ret_record_of_records", (42, RECORD, RECORD)),
3939
("ret_record_of_nested", (42, NESTED, NESTED)),
40-
pytest.param(
41-
"ret_record_of_plsql_table",
42-
(42, PLSQL_TABLE, PLSQL_TABLE),
43-
marks=pytest.mark.xfail(reason="can't tell between NESTED/PLSQL tables"),
44-
),
40+
("ret_record_of_plsql_table", (42, PLSQL_TABLE, PLSQL_TABLE),),
4541
("ret_nested", NESTED),
4642
("ret_nested_of_records", [RECORD] * 10),
4743
("ret_nested_of_record_of_nested", [(42, NESTED, NESTED)] * 10),
@@ -51,10 +47,7 @@
5147
pytest.param(
5248
"ret_varray_of_plsql_table",
5349
[PLSQL_TABLE] * 10,
54-
marks=[
55-
pytest.mark.skip(reason="Memory access violation"),
56-
pytest.mark.xfail(reason="can't tell between NESTED/PLSQL tables"),
57-
],
50+
marks=[pytest.mark.skip(reason="Memory access violation"),],
5851
),
5952
pytest.param(
6053
"ret_varray_of_nested",

0 commit comments

Comments
 (0)