Skip to content

Commit 5763ef5

Browse files
committed
first attempts to create a way to match to a single function
1 parent deb69f1 commit 5763ef5

2 files changed

Lines changed: 61 additions & 29 deletions

File tree

‎plsql/client.py‎

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,26 @@
3030
#
3131
#
3232

33-
TEST_SCHEMA = 'TEST_USER'
3433
FUNCTION = 'FUNCTION'
3534
PROCEDURE = 'PROCEDURE'
3635

3736

3837
class Subprogram:
39-
subprogram_sql = f'''
40-
SELECT object_type, object_name
41-
FROM all_procedures
42-
WHERE owner = UPPER(:owner)
43-
AND object_name = UPPER(:name)
44-
'''
45-
4638
argument_sql = f'''
4739
SELECT argument_name, data_type, defaulted, in_out
4840
FROM all_arguments
4941
WHERE owner = UPPER(:owner)
50-
AND object_name = UPPER(:name)
42+
AND object_name = :object_name
43+
AND object_id = :object_id
44+
AND subprogram_id = :subprogram_id
45+
'''
46+
subprogram_sql = f'''
47+
SELECT owner, object_name, procedure_name, object_type
48+
FROM all_procedures
49+
WHERE owner = UPPER(:owner)
50+
AND object_id = :object_id
51+
AND subprogram_id = :subprogram_id
52+
AND object_type IN ('FUNCTION', 'PROCEDURE', 'PACKAGE')
5153
'''
5254

5355
argument_mapping = {
@@ -56,21 +58,17 @@ class Subprogram:
5658
'NUMBER': float
5759
}
5860

59-
def __init__(self, name, plsql):
60-
self.name, self.plsql = name, plsql
61-
62-
if '.' in name:
63-
schema, name = name.split('.')
61+
def __init__(self, plsql, name, owner, object_id, subprogram_id):
62+
self.owner, self.object_id, self.subprogram_id = owner, object_id, subprogram_id
63+
self.plsql = plsql
64+
self.name = name
6465

65-
binds = {'owner': TEST_SCHEMA, 'name': name}
66+
binds = {'owner': self.owner, 'object_id': self.object_id, 'subprogram_id': self.subprogram_id}
6667
self.definition = plsql.query(sql_query=self.subprogram_sql, bind_variables=binds).first
6768

68-
binds = {'owner': TEST_SCHEMA, 'name': name}
69-
self.arguments = [
70-
argument
71-
for argument
72-
in plsql.query(sql_query=self.argument_sql, bind_variables=binds).all
73-
]
69+
binds = {'owner': self.owner, 'object_name': self.definition.object_name,
70+
'object_id': self.object_id, 'subprogram_id': self.subprogram_id}
71+
self.arguments = list(plsql.query(sql_query=self.argument_sql, bind_variables=binds).all)
7472

7573
if self.definition.object_type == FUNCTION:
7674
self.return_type = next(
@@ -120,20 +118,54 @@ def parse_in_out(parameters):
120118
raise NotImplementedError(f'Unrecognized object_type "{self.definition.object_type}"!')
121119

122120

121+
def resolve_subprogram(attributes, parameters, plsql):
122+
assert attributes, 'Attributes should not be empty!'
123+
124+
attribute_len = len(attributes)
125+
if attribute_len > 3:
126+
raise ValueError('Attribute access is too deep, maximum of 3 allowed! (schema, package, subprogram)')
127+
128+
if attribute_len == 3:
129+
# schema, package, subprogram = attributes
130+
raise NotImplementedError('Can not access with full path!')
131+
elif attribute_len == 2:
132+
_ = parameters
133+
raise NotImplementedError('Can not access packages or specify schema of subprogram!')
134+
# search for subprogram assuming package name is provided
135+
# if fails then must be standalone and schema is provided
136+
else:
137+
subprogram_sql = f'''
138+
SELECT owner, object_id, subprogram_id
139+
FROM all_procedures
140+
WHERE object_name = UPPER(:name)
141+
AND object_type IN ('FUNCTION', 'PROCEDURE', 'PACKAGE')
142+
'''
143+
subprogram = attributes[0]
144+
145+
matching_subprograms = list(plsql.query(subprogram_sql, {'name': subprogram}).all)
146+
147+
if not matching_subprograms:
148+
raise AttributeError('No such subprogram exists!')
149+
150+
if len(matching_subprograms) > 1:
151+
raise NotImplementedError('Can not resolve to single subprogram!')
152+
153+
return matching_subprograms[0]
154+
155+
123156
class AttributeWalker:
124157
def __init__(self, plsql, attr):
125-
self.plsql, self.attr = plsql, [attr]
158+
self.plsql, self.attributes = plsql, [attr]
126159

127160
def __getattr__(self, item):
128-
self.attr.append(item)
161+
self.attributes.append(item)
129162
return self
130163

131-
def __call__(self, **kwargs):
132-
if len(self.attr) > 3:
133-
raise ValueError('Attribute access is too deep, maximum of 3 allowed! (schema, package, subprogram)')
164+
def __call__(self, **parameters):
165+
subprogram = resolve_subprogram(attributes=self.attributes, parameters=parameters, plsql=self.plsql)
134166

135-
subprogram = Subprogram('.'.join(self.attr), self.plsql)
136-
return subprogram(**kwargs)
167+
subprogram = Subprogram(self.plsql, '.'.join(self.attributes), **subprogram._asdict())
168+
return subprogram(**parameters)
137169

138170

139171
class Query:

‎tests/test_functions.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_function_with_defaults(plsql):
1111

1212

1313
def test_string_function(plsql):
14-
assert plsql.string_function(p_str='test') == 'testtest'
14+
assert plsql.string_function(p_str='test') == 'test' * 2
1515

1616

1717
def test_function_in_out(plsql):

0 commit comments

Comments
 (0)