Skip to content

Commit 7eea4b2

Browse files
committed
added support for blobs, clobs and dates, such as it is
1 parent eb1f7b2 commit 7eea4b2

5 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎plsql/client.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from collections import namedtuple
23
from operator import itemgetter
34

@@ -25,11 +26,14 @@ class Subprogram:
2526
AND subprogram_id = :subprogram_id
2627
AND object_type IN ('FUNCTION', 'PROCEDURE', 'PACKAGE')
2728
'''
28-
29+
# fixme: not gonna scale with user defined types in database
2930
argument_mapping = {
3031
'VARCHAR2': str,
3132
'INTEGER': int,
32-
'NUMBER': float
33+
'NUMBER': float,
34+
'DATE': datetime.datetime,
35+
'CLOB': oracle.CLOB,
36+
'BLOB': oracle.BLOB
3337
}
3438

3539
def __init__(self, plsql, name, owner, object_id, subprogram_id):
@@ -72,9 +76,15 @@ def __call__(self, **kwargs):
7276
pass
7377
kwargs[name] = parameter
7478

79+
def pythonize_value(value):
80+
try:
81+
return value.read()
82+
except AttributeError:
83+
return value
84+
7585
def parse_in_out(parameters):
7686
return {
77-
param_name: param_value.getvalue()
87+
param_name: pythonize_value(param_value.getvalue())
7888
for param_name, param_value
7989
in parameters
8090
}
@@ -90,6 +100,8 @@ def parse_in_out(parameters):
90100
if object_type == FUNCTION:
91101
result = cursor.callfunc(self.name, self.return_type, keywordParameters=kwargs)
92102

103+
result = pythonize_value(result)
104+
93105
if in_out_parameters:
94106
return result, parse_in_out(in_out_parameters)
95107
return result
@@ -177,7 +189,6 @@ def __getattr__(self, item):
177189
return self
178190

179191
def __call__(self, **parameters):
180-
# todo: potential
181192
found_subprogram = resolve_subprogram(attributes=self.attributes, parameters=parameters, plsql=self.plsql)
182193
subprogram = Subprogram(self.plsql, '.'.join(self.attributes), **found_subprogram._asdict())
183194
return subprogram(**parameters)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE OR REPLACE FUNCTION datetime_func(p_date IN OUT DATE)
2+
RETURN DATE IS
3+
BEGIN
4+
p_date := p_date + 10;
5+
RETURN p_date;
6+
END datetime_func;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
create or replace FUNCTION function_blob(p_blob IN OUT BLOB)
2+
RETURN BLOB IS
3+
v_blob BLOB;
4+
BEGIN
5+
dbms_lob.createtemporary(v_blob, TRUE);
6+
FOR i IN 1 .. 10000 LOOP
7+
DBMS_LOB.APPEND(v_blob, p_blob);
8+
END LOOP;
9+
10+
p_blob := v_blob;
11+
RETURN v_blob;
12+
END function_blob;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE OR REPLACE FUNCTION function_clob(p_clob IN OUT CLOB)
2+
RETURN CLOB IS
3+
v_clob CLOB;
4+
BEGIN
5+
FOR i IN 1 .. 10000 LOOP
6+
v_clob := v_clob || p_clob;
7+
END LOOP;
8+
9+
p_clob := v_clob;
10+
RETURN v_clob;
11+
END function_clob;

‎tests/test_functions.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import datetime
2+
3+
14
def test_simple_function(plsql):
25
assert plsql.simple_function(p_int=5) == 5 + 5
36
assert plsql.simple_function(p_int=None) is None
@@ -24,3 +27,27 @@ def test_function_in_out(plsql):
2427
assert result == 42
2528
assert in_out['p_str'] == 'input: 42'
2629
assert in_out['p_float'] == 10.5
30+
31+
32+
def test_datetime_func(plsql):
33+
date = datetime.datetime(year=2019, month=5, day=5)
34+
35+
ten_days_ahead = date + datetime.timedelta(days=10)
36+
37+
result, in_out = plsql.datetime_func(p_date=date)
38+
assert result == ten_days_ahead
39+
assert in_out['p_date'] == ten_days_ahead
40+
41+
42+
def test_function_clob(plsql):
43+
clob = '1'
44+
result, in_out = plsql.function_clob(p_clob=clob)
45+
assert len(result) == len(in_out['p_clob']) == 10000
46+
assert type(clob) == type(result) == type(in_out['p_clob'])
47+
48+
49+
def test_function_blob(plsql):
50+
blob = b'1'
51+
result, in_out = plsql.function_blob(p_blob=blob)
52+
assert len(result) == len(in_out['p_blob']) == 10000
53+
assert type(blob) == type(result) == type(in_out['p_blob'])

0 commit comments

Comments
 (0)