|
| 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) |
0 commit comments