@@ -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 )
0 commit comments