|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2024, 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 | +# json_duality_async.py |
| 27 | +# |
| 28 | +# An asynchronous version of json_duality.py |
| 29 | +# |
| 30 | +# Demonstrates Oracle Database 23ai JSON-Relational Duality Views. |
| 31 | +# |
| 32 | +# Reference: https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=ADJSN |
| 33 | +# ----------------------------------------------------------------------------- |
| 34 | + |
| 35 | +import asyncio |
| 36 | +import sys |
| 37 | + |
| 38 | +import oracledb |
| 39 | +import sample_env |
| 40 | + |
| 41 | + |
| 42 | +async def main(): |
| 43 | + connection = await oracledb.connect_async( |
| 44 | + user=sample_env.get_main_user(), |
| 45 | + password=sample_env.get_main_password(), |
| 46 | + dsn=sample_env.get_connect_string(), |
| 47 | + ) |
| 48 | + |
| 49 | + # this script only works with Oracle Database 23.5 or later |
| 50 | + if sample_env.get_server_version() < (23, 5): |
| 51 | + sys.exit("This example requires Oracle Database 23.5 or later") |
| 52 | + |
| 53 | + with connection.cursor() as cursor: |
| 54 | + |
| 55 | + # Create a JSON-Relational Duality View over the SampleJRDVAuthorTab |
| 56 | + # and SampleJRDVBookTab tables |
| 57 | + sql = """ |
| 58 | + create or replace json relational duality view BookDV as |
| 59 | + SampleJRDVBookTab @insert @update @delete |
| 60 | + { |
| 61 | + _id: BookId, |
| 62 | + book_title: BookTitle, |
| 63 | + author: SampleJRDVAuthorTab @insert @update |
| 64 | + { |
| 65 | + author_id: AuthorId, |
| 66 | + author_name: AuthorName |
| 67 | + } |
| 68 | + }""" |
| 69 | + await cursor.execute(sql) |
| 70 | + |
| 71 | + with connection.cursor() as cursor: |
| 72 | + |
| 73 | + # Insert a new book and author into the Duality View and show the |
| 74 | + # resulting new records in the relational tables |
| 75 | + data = dict( |
| 76 | + _id=101, |
| 77 | + book_title="Cooking at Home", |
| 78 | + author=dict(author_id=201, author_name="Dave Smith"), |
| 79 | + ) |
| 80 | + inssql = "insert into BookDV values (:1)" |
| 81 | + cursor.setinputsizes(oracledb.DB_TYPE_JSON) |
| 82 | + await cursor.execute(inssql, [data]) |
| 83 | + |
| 84 | + print("Authors in the relational table:") |
| 85 | + await cursor.execute( |
| 86 | + "select * from SampleJRDVAuthorTab order by AuthorId" |
| 87 | + ) |
| 88 | + async for row in cursor: |
| 89 | + print(row) |
| 90 | + |
| 91 | + print("\nBooks in the relational table:") |
| 92 | + await cursor.execute("select * from SampleJRDVBookTab order by BookId") |
| 93 | + async for row in cursor: |
| 94 | + print(row) |
| 95 | + |
| 96 | + # Select from the duality view |
| 97 | + |
| 98 | + with connection.cursor() as cursor: |
| 99 | + |
| 100 | + print("\nDuality view query for an author's books:") |
| 101 | + sql = """select b.data.book_title, b.data.author.author_name |
| 102 | + from BookDV b |
| 103 | + where b.data.author.author_id = :1""" |
| 104 | + await cursor.execute(sql, [1]) |
| 105 | + async for r in cursor: |
| 106 | + print(r) |
| 107 | + |
| 108 | + print("\nDuality view query of all records:") |
| 109 | + sql = """select data from BookDV""" |
| 110 | + await cursor.execute(sql) |
| 111 | + async for (j,) in cursor: |
| 112 | + print(j) |
| 113 | + |
| 114 | + |
| 115 | +asyncio.run(main()) |
0 commit comments