Skip to content

Commit 49216ba

Browse files
committed
working on some more esoteric record type configurations
1 parent efbb4df commit 49216ba

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

‎python_plsql/client.py‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import Optional
1313
from typing import Union
1414
from typing import Tuple
15+
from typing import NamedTuple
1516

1617
import cx_Oracle as oracle
1718

@@ -268,37 +269,43 @@ def filter_match(matches: Iterator[Overload]) -> Overload:
268269
return matches[0]
269270

270271

271-
def plsql_record(attributes):
272+
def plsql_record(attributes) -> Any:
272273
return namedtuple("Record", (attribute.name.lower() for attribute in attributes))
273274

274275

275-
def to_record(value, record, attributes):
276-
return record(*(getattr(value, attribute.name) for attribute in attributes))
276+
# noinspection PyTypeChecker
277+
def to_python(value):
278+
if isinstance(value, oracle.Object):
279+
return record_converter(value)
280+
return value
281+
282+
283+
def to_record(value, record):
284+
return record._make(
285+
to_python(getattr(value, attribute.upper())) for attribute in record._fields
286+
)
277287

278288

279289
def list_converter(value):
280290
if value.type.elementType:
281291
attributes = value.type.elementType.attributes
282292
record = plsql_record(attributes)
283-
return [to_record(val, record, attributes) for val in value.aslist()]
293+
return [to_record(val, record) for val in value.aslist()]
284294
return value.aslist()
285295

286296

287297
def dict_converter(value):
288298
if value.type.elementType:
289299
attributes = value.type.elementType.attributes
290300
record = plsql_record(attributes)
291-
return {
292-
key: to_record(val, record, attributes)
293-
for key, val in value.asdict().items()
294-
}
301+
return {key: to_record(val, record) for key, val in value.asdict().items()}
295302
return value.asdict()
296303

297304

298305
def record_converter(value, record=None):
299306
attributes = value.type.attributes
300307
record = record or plsql_record(attributes)
301-
return to_record(value, record, attributes)
308+
return to_record(value, record)
302309

303310

304311
converters = {

‎tests/func/test_return_values.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
("ret_char", "42"),
1717
("ret_bool", True),
1818
("ret_record", (42, 84, 126)),
19+
("ret_record_of_records", (42, (42, 84, 126), (42, 84, 126))),
1920
("ret_nested", [i * 2 for i in range(1, 11)]),
2021
("ret_nested_of_records", [(42, 84, 126) for i in range(1, 11)]),
2122
("ret_plsql_table", {i: i for i in range(1, 11)}),

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ CREATE OR REPLACE PACKAGE BODY test_return AS
139139
RETURN vr_record;
140140
END;
141141

142+
FUNCTION ret_record_of_records
143+
RETURN tr_record_of_records IS
144+
vr_record_of_records tr_record_of_records;
145+
BEGIN
146+
vr_record_of_records.t_int_1 := 42;
147+
vr_record_of_records.t_rec_2 := ret_record;
148+
vr_record_of_records.t_rec_3 := ret_record;
149+
RETURN vr_record_of_records;
150+
END ret_record_of_records;
151+
142152
-- 251 PL/SQL TABLE -- mapping
143153
FUNCTION ret_plsql_table
144154
RETURN tt_plsql_table IS

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ CREATE OR REPLACE PACKAGE test_return AS
66
t_int_3 PLS_INTEGER
77
);
88

9+
TYPE tr_record_of_records IS RECORD (
10+
t_int_1 PLS_INTEGER,
11+
t_rec_2 tr_record,
12+
t_rec_3 tr_record
13+
);
14+
915
TYPE tt_nested IS TABLE OF PLS_INTEGER;
1016

1117
TYPE tt_nested_of_records IS TABLE OF tr_record;
@@ -98,6 +104,9 @@ CREATE OR REPLACE PACKAGE test_return AS
98104
FUNCTION ret_record
99105
RETURN tr_record;
100106

107+
FUNCTION ret_record_of_records
108+
RETURN tr_record_of_records;
109+
101110
-- 251 PL/SQL TABLE -- mapping
102111
FUNCTION ret_plsql_table
103112
RETURN tt_plsql_table;

0 commit comments

Comments
 (0)