@@ -247,6 +247,10 @@ def __init__(self, arguments: Iterator[Argument]):
247247 def match (self , * args , ** kwargs ) -> bool :
248248 return (len (args ) + len (kwargs )) == len (self .arguments )
249249
250+ @property
251+ def is_function (self ) -> bool :
252+ return bool (self .return_type )
253+
250254
251255def group_by_overload (arguments : Iterator [Argument ]) -> List [Overload ]:
252256 return [
@@ -255,6 +259,15 @@ def group_by_overload(arguments: Iterator[Argument]) -> List[Overload]:
255259 ] or [Overload (arguments = [])]
256260
257261
262+ def filter_match (matches : Iterator [Overload ]) -> Overload :
263+ matches = list (matches )
264+ if len (matches ) > 1 :
265+ raise ValueError ("Arguments matched with too many subprograms" )
266+ elif len (matches ) == 0 :
267+ raise ValueError ("Arguments do not match subprogram" )
268+ return matches [0 ]
269+
270+
258271class Subprogram :
259272 def __init__ (
260273 self , plsql : Database , resolved : ResolvedName , arguments : Iterator [Argument ]
@@ -266,42 +279,39 @@ def __init__(
266279 )
267280
268281 def _call_function (self , * args , ** kwargs ):
269- # match = self._matches (*args, **kwargs)[0]
282+ _ = filter_match ( self ._function_matches (* args , ** kwargs ))
270283
271284 with self .plsql ._connection .cursor () as cursor :
272285 return cursor .callfunc (self .resolved .name , int , args , kwargs )
273286
274287 def _call_procedure (self , * args , ** kwargs ):
275- # match = self._matches (*args, **kwargs)[0]
288+ _ = filter_match ( self ._procedure_matches (* args , ** kwargs ))
276289
277290 with self .plsql ._connection .cursor () as cursor :
278- cursor .callproc (self .resolved .name , * args , ** kwargs )
291+ cursor .callproc (self .resolved .name , args , kwargs )
279292
280293 def __call__ (self , * args , ** kwargs ):
281- if (matches := len (self ._matches (* args , ** kwargs ))) > 1 :
282- raise ValueError ("Arguments matched with too many subprograms" )
283- elif matches == 0 :
284- raise ValueError ("Arguments do not match subprogram" )
285-
286- if self .is_function and self .is_procedure :
294+ if self .overloaded and self .is_function and self .is_procedure :
287295 raise NotImplementedError (
288296 "Subprogram is overloaded as a function and procedure, use _call_(procedure/function) method"
289297 )
290298
291299 if self .is_function :
292300 return self ._call_function (* args , ** kwargs )
293301 else :
294- self ._call_procedure (* args , ** kwargs )
302+ return self ._call_procedure (* args , ** kwargs )
303+
304+ def _function_matches (self , * args , ** kwargs ) -> Iterator [Overload ]:
305+ return filter (lambda x : x .is_function , self ._matches (* args , ** kwargs ))
295306
296- def _matches (self , * args , ** kwargs ) -> List [Overload ]:
307+ def _procedure_matches (self , * args , ** kwargs ) -> Iterator [Overload ]:
308+ return filter (lambda x : not x .is_function , self ._matches (* args , ** kwargs ))
309+
310+ def _matches (self , * args , ** kwargs ) -> Iterator [Overload ]:
297311 """returns overloads that match with provided parameters"""
298- return [
312+ return (
299313 overload for overload in self .overloads if overload .match (* args , ** kwargs )
300- ]
301-
302- def _match (self , * args , ** kwargs ) -> bool :
303- """returns true if function has"""
304- return bool (self ._matches (* args , ** kwargs ))
314+ )
305315
306316 @property
307317 def overloaded (self ) -> bool :
@@ -311,11 +321,11 @@ def overloaded(self) -> bool:
311321
312322 @property
313323 def is_function (self ):
314- return any (overload .return_type for overload in self .overloads )
324+ return any (overload .is_function for overload in self .overloads )
315325
316326 @property
317327 def is_procedure (self ):
318- return any (not overload .return_type for overload in self .overloads )
328+ return any (not overload .is_function for overload in self .overloads )
319329
320330 @property
321331 def standalone (self ) -> bool :
0 commit comments