22
33from collections import namedtuple
44from enum import IntEnum
5+ from itertools import groupby
56from itertools import starmap
7+ from operator import attrgetter
68from operator import itemgetter
9+ from typing import Any
710from typing import Iterator
11+ from typing import List
812from typing import Tuple
913
1014import cx_Oracle as oracle
@@ -36,6 +40,8 @@ class NotFound(Exception):
3640def _dbms_describe_describe_procedure (
3741 connection : oracle .Connection , name : str
3842) -> Iterator [Tuple [Any , ...]]:
43+ """Wrapper for Oracle DBMS_DESCRIBE.DESCRIBE_PROCEDURE procedure"""
44+
3945 number_table , varchar2_table = (
4046 connection .gettype ("DBMS_DESCRIBE.NUMBER_TABLE" ),
4147 connection .gettype ("DBMS_DESCRIBE.VARCHAR2_TABLE" ),
@@ -73,7 +79,6 @@ def _dbms_describe_describe_procedure(
7379 scale ,
7480 radix ,
7581 spare ,
76- True ,
7782 ],
7883 )
7984
@@ -95,6 +100,7 @@ def _dbms_describe_describe_procedure(
95100def _dbms_utility_name_resolve (
96101 connection : oracle .Connection , name : str , context : int
97102) -> Tuple [str , str , str , str , int , int ]:
103+ """Wrapper for Oracle DBMS_UTILITY.NAME_RESOLVE procedure"""
98104 with connection .cursor () as cursor :
99105 schema = cursor .var (str )
100106 part1 = cursor .var (str )
@@ -130,6 +136,7 @@ def _dbms_utility_name_resolve(
130136def _exhaustive_name_resolution (
131137 connection : oracle .Connection , name : str
132138) -> Tuple [str , str , str , str , int , int ]:
139+ """Exhaustively searches for name in database using all available contexts possible in DBMS_UTILITY.NAME_RESOLVE"""
133140 for context in range (0 , 10 ):
134141 try :
135142 return _dbms_utility_name_resolve (
@@ -152,11 +159,6 @@ def __init__(self, plsql: Database, name: str):
152159 self ._plsql , self ._name = plsql , name
153160
154161
155- class Function :
156- def __init__ (self , plsql : Database , name : ResolvedName ):
157- self ._plsql , self ._name = plsql , name
158-
159-
160162class Query :
161163 def __init__ (
162164 self , connection : oracle .Connection , sql : str , * positional_binds , ** named_binds
@@ -191,6 +193,61 @@ def __iter__(self):
191193 return self ._execute ()
192194
193195
196+ class Function :
197+ def __init__ (self , plsql : Database , name : ResolvedName ):
198+ self ._plsql , self ._name = plsql , name
199+
200+
201+ def return_type (arguments : List [Argument ]):
202+ """
203+ Extracts return type from list of arguments.
204+ According to Oracle documentation: "Position 0 returns the values for the return type of a function."
205+ """
206+ for argument in arguments :
207+ if argument .position == 0 :
208+ return argument
209+
210+
211+ class Overload :
212+ def __init__ (self , arguments ):
213+ self .arguments = list (arguments )
214+
215+ self .return_type = return_type (self .arguments )
216+
217+ self .arguments = [
218+ argument
219+ for argument in self .arguments
220+ if argument .position and argument .datatype
221+ ]
222+
223+
224+ def group_by_overload (arguments : Iterator [Argument ]) -> List [Overload ]:
225+ return [
226+ Overload (arguments = grouped_arguments )
227+ for _ , grouped_arguments in groupby (arguments , attrgetter ("overload" ))
228+ ] or [Overload (arguments = [])]
229+
230+
231+ class Subprogram :
232+ def __init__ (
233+ self , plsql : Database , resolved : ResolvedName , arguments : Iterator [Argument ]
234+ ):
235+ self .plsql , self .resolved = plsql , resolved
236+
237+ self .overloads = group_by_overload (arguments = arguments )
238+
239+ @property
240+ def overloaded (self ) -> bool :
241+ return len (self .overloads ) > 1
242+
243+ @property
244+ def standalone (self ) -> bool :
245+ return self .resolved .object_type in {
246+ ObjectTypes .procedure ,
247+ ObjectTypes .function ,
248+ }
249+
250+
194251class Database :
195252 def __init__ (
196253 self ,
@@ -215,20 +272,25 @@ def __getattr__(self, item):
215272 username = item ,
216273 ).first :
217274 return Schema (plsql = self , name = item )
218-
219275 raise AttributeError
220276 else :
221- if name .object_type == ObjectTypes .function :
222- return Function (plsql = self , name = name )
277+ if name .object_type in {
278+ ObjectTypes .function ,
279+ ObjectTypes .procedure ,
280+ }:
281+ return Subprogram (
282+ plsql = self , resolved = name , arguments = self ._describe_procedure (item ),
283+ )
223284
224285 raise AttributeError
225286
226- def _describe_procedure (self , name : str ) -> List [Argument ]:
227- result = _dbms_describe_describe_procedure (self ._connection , name )
287+ def _describe_procedure (self , name : str ) -> Iterator [Argument ]:
288+ arguments = _dbms_describe_describe_procedure (self ._connection , name )
228289
229- return [ Argument (* argument ) for argument in result ]
290+ return ( Argument (* argument ) for argument in arguments )
230291
231292 def _name_resolve (self , name : str ) -> ResolvedName :
293+ """Resolves name using in-built database procedure DBMS_UTILITY.NAME_RESOLVE"""
232294 (
233295 schema ,
234296 part1 ,
0 commit comments