Skip to content

Commit 06793b4

Browse files
committed
still evolving code to basic functionality
1 parent 8753465 commit 06793b4

10 files changed

Lines changed: 157 additions & 18 deletions

‎plsql/client.py‎

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
from collections import namedtuple
44
from enum import IntEnum
5+
from itertools import groupby
56
from itertools import starmap
7+
from operator import attrgetter
68
from operator import itemgetter
9+
from typing import Any
710
from typing import Iterator
11+
from typing import List
812
from typing import Tuple
913

1014
import cx_Oracle as oracle
@@ -36,6 +40,8 @@ class NotFound(Exception):
3640
def _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(
95100
def _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(
130136
def _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-
160162
class 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+
194251
class 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,

‎requirements.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
cx_Oracle
2-
pytest
2+
pytest
3+
mypy

‎setup.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import setuptools
2+
3+
4+
setuptools.setup(
5+
name="python_plsql",
6+
author="LatvianPython",
7+
version="0.1.1",
8+
packages=setuptools.find_packages(),
9+
classifiers=[
10+
"Programming Language :: Python :: 3.8",
11+
"License :: OSI Approved :: MIT License",
12+
"Development Status :: 2 - Pre-Alpha",
13+
],
14+
python_requires=">=3.8",
15+
install_requires=["cx_Oracle"],
16+
)

‎tests/conftest.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@pytest.fixture(scope="session")
1212
def plsql():
1313
config = configparser.ConfigParser()
14-
config.read("./database.ini")
14+
config.read("./tests/database.ini")
1515

1616
parameters = dict(config["database"].items())
1717

@@ -32,7 +32,9 @@ def plsql():
3232
)
3333

3434
directories = [
35-
directory for directory in Path(".").glob("./sql/*/") if directory.is_dir()
35+
directory
36+
for directory in Path("./tests").glob("./sql/*/")
37+
if directory.is_dir()
3638
]
3739

3840
objects = [
@@ -41,6 +43,8 @@ def plsql():
4143
for file in directory.glob("*.sql")
4244
]
4345

46+
assert objects, "no objects found!"
47+
4448
drop_all_objects(database, objects)
4549
create_objects(database, objects)
4650

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE OR REPLACE PACKAGE BODY test_overload AS
2+
3+
FUNCTION to_string(p_in IN VARCHAR2)
4+
RETURN VARCHAR2 IS
5+
BEGIN
6+
RETURN p_in;
7+
END to_string;
8+
9+
FUNCTION to_string(p_in IN INTEGER)
10+
RETURN VARCHAR2 IS
11+
BEGIN
12+
RETURN TO_CHAR(p_in);
13+
END to_string;
14+
15+
END test_overload;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE OR REPLACE PACKAGE test_overload AS
2+
3+
FUNCTION to_string(p_in IN VARCHAR2)
4+
RETURN VARCHAR2;
5+
6+
FUNCTION to_string(p_in IN INTEGER)
7+
RETURN VARCHAR2;
8+
9+
END test_overload;

‎tests/sql/package/test_package-b.sql‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ CREATE OR REPLACE PACKAGE BODY test_package AS
55
NULL;
66
END simple_procedure;
77

8+
FUNCTION simple_function
9+
RETURN INTEGER IS
10+
BEGIN
11+
RETURN 1;
12+
END simple_function;
13+
814
END test_package;

‎tests/sql/package/test_package-h.sql‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ CREATE OR REPLACE PACKAGE test_package AS
22

33
PROCEDURE simple_procedure;
44

5+
FUNCTION simple_function RETURN INTEGER;
6+
57
END test_package;

‎tests/test_describe_procedure.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,31 @@
2323
# 252 PL/SQL BOOLEAN -- bool
2424

2525

26-
def test_describe_simple_function(plsql):
26+
def test_describe_simple_procedure(plsql):
2727
result = plsql._describe_procedure("test_synonym_procedure")
28+
assert len(list(result)) == 0
2829

29-
assert result is not None
30+
31+
def test_describe_procedure_no_arguments(plsql):
32+
result = plsql._describe_procedure("test_package.simple_procedure")
33+
assert len(list(result)) == 1
34+
35+
36+
def test_describe_function_no_arguments(plsql):
37+
result = plsql._describe_procedure("test_package.simple_function")
38+
assert len(list(result)) == 1
39+
40+
41+
def test_describe_function_with_arguments(plsql):
42+
result = plsql._describe_procedure("simple_function")
43+
assert len(list(result)) == 2
3044

3145

3246
def test_describe_all_parameters(plsql):
3347
result = plsql._describe_procedure("test_describe.oracle_datatypes")
48+
assert len(list(result)) == 27
49+
3450

35-
assert result is not None
51+
def test_overloaded_returns_multiple_definitions(plsql):
52+
result = plsql._describe_procedure("test_overload.to_string")
53+
assert len(list(result)) == 4
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest
2+
3+
4+
def test_procedure_found(plsql):
5+
procedure = plsql.simple_procedure
6+
assert procedure.resolved.name == "SIMPLE_PROCEDURE"

0 commit comments

Comments
 (0)