Skip to content

Commit 1c0f626

Browse files
Add Oracle object pretty-printing example.
1 parent 094a145 commit 1c0f626

File tree

4 files changed

+116
-22
lines changed

4 files changed

+116
-22
lines changed

doc/src/release_notes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Thin Mode Changes
2727
Common Changes
2828
++++++++++++++
2929

30-
#) Improved test suite.
30+
#) Improved test suite and samples.
3131

3232

3333
oracledb 1.3.0 (March 2023)

samples/insert_geometry.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#------------------------------------------------------------------------------
2-
# Copyright (c) 2016, 2022, Oracle and/or its affiliates.
2+
# Copyright (c) 2016, 2023, Oracle and/or its affiliates.
33
#
44
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
55
#
@@ -58,25 +58,7 @@
5858
print("Created object", obj)
5959

6060
with connection.cursor() as cursor:
61-
62-
# create sample table
63-
cursor.execute("""
64-
begin
65-
begin
66-
execute immediate 'drop table TestGeometry';
67-
exception
68-
when others then
69-
if sqlcode <> -942 then
70-
raise;
71-
end if;
72-
end;
73-
74-
execute immediate 'create table TestGeometry (
75-
IntCol number(9) not null,
76-
Geometry MDSYS.SDO_GEOMETRY)';
77-
end;""")
78-
79-
61+
cursor.execute("truncate table TestGeometry")
8062
print("Adding row to table...")
8163
cursor.execute("insert into TestGeometry values (1, :objbv)", objbv=obj)
8264
connection.commit()

samples/object_dump.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#------------------------------------------------------------------------------
2+
# Copyright (c) 2023, Oracle and/or its affiliates.
3+
#
4+
# This software is dual-licensed to you under the Universal Permissive License
5+
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
6+
# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
# either license.
8+
#
9+
# If you elect to accept the software under the Apache License, Version 2.0,
10+
# the following applies:
11+
#
12+
# Licensed under the Apache License, Version 2.0 (the "License");
13+
# you may not use this file except in compliance with the License.
14+
# You may obtain a copy of the License at
15+
#
16+
# https://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing, software
19+
# distributed under the License is distributed on an "AS IS" BASIS,
20+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
# See the License for the specific language governing permissions and
22+
# limitations under the License.
23+
#------------------------------------------------------------------------------
24+
25+
#------------------------------------------------------------------------------
26+
# object_dump.py
27+
#
28+
# Shows how to pretty-print an Oracle object or collection.
29+
# Also shows how to insert a Python object to an Oracle object column.
30+
#------------------------------------------------------------------------------
31+
32+
import oracledb
33+
import sample_env
34+
35+
# determine whether to use python-oracledb thin mode or thick mode
36+
if not sample_env.get_is_thin():
37+
oracledb.init_oracle_client(lib_dir=sample_env.get_oracle_client())
38+
39+
# Create Oracle connection and cursor objects
40+
connection = oracledb.connect(user=sample_env.get_main_user(),
41+
password=sample_env.get_main_password(),
42+
dsn=sample_env.get_connect_string())
43+
cursor = connection.cursor()
44+
45+
# Create a Python class equivalent to an Oracle SDO object
46+
class MySDO(object):
47+
48+
def __init__(self, gtype, elem_info, ordinates):
49+
self.gtype = gtype
50+
self.elem_info = elem_info
51+
self.ordinates = ordinates
52+
53+
54+
# Get Oracle type information
55+
obj_type = connection.gettype("MDSYS.SDO_GEOMETRY")
56+
element_info_type_obj = connection.gettype("MDSYS.SDO_ELEM_INFO_ARRAY")
57+
ordinate_type_obj = connection.gettype("MDSYS.SDO_ORDINATE_ARRAY")
58+
59+
# Convert a Python object to MDSYS.SDO_GEOMETRY
60+
def sdo_input_type_handler(cursor, value, num_elements):
61+
def sdo_in_converter(value):
62+
obj = obj_type.newobject()
63+
obj.SDO_GTYPE = value.gtype
64+
obj.SDO_ELEM_INFO = element_info_type_obj.newobject()
65+
obj.SDO_ELEM_INFO.extend(value.elem_info)
66+
obj.SDO_ORDINATES = ordinate_type_obj.newobject()
67+
obj.SDO_ORDINATES.extend(value.ordinates)
68+
return obj
69+
70+
if isinstance(value, MySDO):
71+
return cursor.var(obj_type, arraysize=num_elements,
72+
inconverter=sdo_in_converter)
73+
74+
75+
# Create and insert a Python object
76+
sdo = MySDO(2003, [1, 1003, 3], [1, 1, 5, 7])
77+
cursor.inputtypehandler = sdo_input_type_handler
78+
cursor.execute("truncate table TestGeometry")
79+
cursor.execute("insert into TestGeometry values (1, :1)", [sdo])
80+
81+
82+
# Define a function to pretty-print the contents of an Oracle object
83+
def dump_object(obj, prefix=""):
84+
if obj.type.iscollection:
85+
print(f"{prefix}[")
86+
for value in obj.aslist():
87+
if isinstance(value, oracledb.DbObject):
88+
dump_object(value, prefix + " ")
89+
else:
90+
print(f"{prefix} {repr(value)}")
91+
print(f"{prefix}]")
92+
else:
93+
print(f"{prefix}{{")
94+
for attr in obj.type.attributes:
95+
value = getattr(obj, attr.name)
96+
if isinstance(value, oracledb.DbObject):
97+
print(f"{prefix} {attr.name}:")
98+
dump_object(value, prefix + " ")
99+
else:
100+
print(f"{prefix} {attr.name}: {repr(value)}")
101+
print(f"{prefix}}}")
102+
103+
# Query the row back
104+
cursor.execute("select geometry from TestGeometry")
105+
for (obj,) in cursor:
106+
dump_object(obj)

samples/sql/create_schema.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*-----------------------------------------------------------------------------
2-
* Copyright 2017, 2022, Oracle and/or its affiliates.
2+
* Copyright 2017, 2023, Oracle and/or its affiliates.
33
*
44
* This software is dual-licensed to you under the Universal Permissive License
55
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
@@ -245,6 +245,12 @@ create table &main_user..LoadCsvTab (
245245
)
246246
/
247247

248+
create table &main_user..TestGeometry (
249+
id number(9) not null,
250+
geometry mdsys.sdo_geometry not null
251+
)
252+
/
253+
248254
declare
249255
t_Version number;
250256
begin

0 commit comments

Comments
 (0)