Skip to content

Commit 48fefb9

Browse files
committed
string functions work now
1 parent c4da74e commit 48fefb9

4 files changed

Lines changed: 72 additions & 25 deletions

File tree

‎plsql/client.py‎

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cx_Oracle as oracle
2+
from collections import namedtuple
23
from functools import lru_cache
34

45

@@ -18,20 +19,65 @@ def __init__(self, name, plsql):
1819
raise ValueError('Not a database schema!')
1920

2021

21-
@lru_cache(maxsize=None)
22-
def get_schema(name, plsql):
23-
try:
24-
return Schema(name=name, plsql=plsql)
25-
except ValueError:
26-
return None
22+
# @lru_cache(maxsize=None)
23+
# def get_schema(name, plsql):
24+
# try:
25+
# return Schema(name=name, plsql=plsql)
26+
# except ValueError:
27+
# return None
28+
#
29+
#
30+
# class Package:
31+
# pass
32+
#
33+
#
2734

28-
29-
class Package:
30-
pass
35+
TEST_SCHEMA = 'TEST_USER'
3136

3237

3338
class Subprogram:
34-
pass
39+
ArgumentRecord = namedtuple('ArgumentRecord', 'argument_name, data_type, defaulted, in_out')
40+
SubprogramRecord = namedtuple('SubprogramRecord', 'object_type, object_name')
41+
42+
subprogram_sql = f'''
43+
SELECT {','.join(ArgumentRecord._fields)}
44+
FROM all_procedures
45+
WHERE owner = UPPER(:owner)
46+
AND object_name = UPPER(:name)
47+
'''
48+
49+
argument_sql = f'''
50+
SELECT {','.join(ArgumentRecord._fields)}
51+
FROM all_arguments
52+
WHERE owner = UPPER(:owner)
53+
AND object_name = UPPER(:name)
54+
'''
55+
56+
argument_mapping = {
57+
'VARCHAR2': str,
58+
'INTEGER': int,
59+
'NUMBER': float
60+
}
61+
62+
def __init__(self, name, plsql):
63+
self.name, self.plsql = name, plsql
64+
65+
binds = {'owner': TEST_SCHEMA, 'name': name.upper()}
66+
self.arguments = (
67+
self.ArgumentRecord(*argument)
68+
for argument
69+
in plsql.query(sql_query=self.argument_sql, bind_variables=binds)
70+
)
71+
72+
self.return_type = next(
73+
self.argument_mapping[argument.data_type]
74+
for argument
75+
in self.arguments
76+
)
77+
78+
def __call__(self, *args, **kwargs):
79+
with self.plsql.connection.cursor() as cursor:
80+
return cursor.callfunc(self.name, self.return_type, keywordParameters=kwargs)
3581

3682

3783
class Database:
@@ -47,17 +93,7 @@ def execute_immediate(self, dynamic_string):
4793
def query(self, sql_query, bind_variables):
4894
with self.connection.cursor() as cursor:
4995
cursor.execute(sql_query, bind_variables)
50-
for row in cursor:
51-
yield row
96+
yield from cursor
5297

5398
def __getattr__(self, item):
54-
schema = get_schema(item, self)
55-
56-
if schema:
57-
return schema
58-
59-
def func(**kwargs):
60-
with self.connection.cursor() as cursor:
61-
return cursor.callfunc(item, int, keywordParameters=kwargs)
62-
63-
return func
99+
return Subprogram(item, self)

‎tests/conftest.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def managed_object(plsql, object_type, object_name):
2020

2121
plsql.execute_immediate(sql_text)
2222
yield
23-
plsql.execute_immediate(f'DROP {object_type} {object_name}')
23+
# plsql.execute_immediate(f'DROP {object_type} {object_name}')
2424

2525

2626
@pytest.fixture(scope='module')
@@ -33,3 +33,9 @@ def simple_function(plsql):
3333
def function_with_defaults(plsql):
3434
with managed_object(plsql, 'function', 'function_with_defaults'):
3535
yield
36+
37+
38+
@pytest.fixture(scope='module')
39+
def string_function(plsql):
40+
with managed_object(plsql, 'function', 'string_function'):
41+
yield
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE OR REPLACE FUNCTION string_function(p_str IN VARCHAR2)
2+
RETURN VARCHAR2 IS
3+
BEGIN
4+
RETURN p_str || p_str;
5+
END;

‎tests/test_functions.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ def test_function_with_defaults(plsql, function_with_defaults):
1313
assert plsql.function_with_defaults(p_int_a=0, p_int_b=0) == 0
1414

1515

16-
def test_schema(plsql):
17-
assert isinstance(plsql.test_user, Schema)
16+
def test_string_function(plsql, string_function):
17+
assert plsql.string_function(p_str='test') == 'testtest'

0 commit comments

Comments
 (0)