Skip to content

Commit 56670b1

Browse files
author
Christopher Jones
authored
Updates for python-oracledb 2.0 (#305)
1 parent a101977 commit 56670b1

File tree

112 files changed

+7403
-1257
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+7403
-1257
lines changed

python/python-oracledb/README.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Python-oracledb Examples
1+
# Python-oracledb Examples
22

33
This directory contains samples for python-oracledb, the Python driver for
44
Oracle Database.
55

6+
### Basic Examples
7+
68
1. The schemas and SQL objects that are referenced in the samples can be
7-
created by running the Python script
8-
[create_schema.py](https://github.com/oracle-samples/oracle-db-examples/blob/main/python/create_schema.py). The
9-
script requires SYSDBA privileges and will prompt for these credentials as
10-
well as the names of the schemas and edition that will be created, unless a
11-
number of environment variables are set as documented in the Python script
12-
[sample_env.py](https://github.com/oracle-samples/oracle-db-examples/blob/main/python/sample_env.py). Run
13-
the script using the following command:
9+
created by running the Python script [create_schema.py][1]. The script
10+
requires SYSDBA privileges and will prompt for these credentials as well as
11+
the names of the schemas and edition that will be created, unless a number
12+
of environment variables are set as documented in the Python script
13+
[sample_env.py][2]. Run the script using the following command:
1414

1515
python create_schema.py
1616

@@ -19,16 +19,24 @@ Oracle Database.
1919
python query.py
2020

2121
3. After running python-oracledb samples, the schemas and SQL objects can be
22-
dropped by running the Python script
23-
[drop_schema.py](https://github.com/oracle-samples/oracle-db-examples/blob/main/python/drop_schema.py). The
24-
script requires SYSDBA privileges and will prompt for these credentials as
25-
well as the names of the schemas and edition that will be dropped, unless a
26-
number of environment variables are set as documented in the Python script
27-
[sample_env.py](https://github.com/oracle-samples/oracle-db-examples/blob/main/python/sample_env.py). Run
28-
the script using the following command:
22+
dropped by running the Python script [drop_schema.py][3]. The script
23+
requires SYSDBA privileges and will prompt for these credentials as well as
24+
the names of the schemas and edition that will be dropped, unless a number
25+
of environment variables are set as documented in the Python script
26+
[sample_env.py][2]. Run the script using the following command:
2927

3028
python drop_schema.py
3129

30+
### Examples in a Container
31+
32+
The [sample_container](./sample_container) directory has a Dockerfile that will
33+
build a container with the samples and a running Oracle Database.
34+
35+
### Notebooks
36+
37+
The [sample_notebooks](./sample_notebooks) directory has Jupyter notebooks with
38+
runnable examples.
39+
3240
## About python-oracledb
3341

3442
- Python-oracledb is the new name for Oracle's popular Python cx_Oracle driver
@@ -71,3 +79,8 @@ PyPI: [pypi.org/project/oracledb/](https://pypi.org/project/oracledb/)
7179
Source: [github.com/oracle/python-oracledb](https://github.com/oracle/python-oracledb)
7280

7381
Upgrading: [Upgrading from cx_Oracle 8.3 to python-oracledb](https://python-oracledb.readthedocs.io/en/latest/user_guide/appendix_c.html#upgrading-from-cx-oracle-8-3-to-python-oracledb)
82+
83+
84+
[1]: https://github.com/oracle/python-oracledb/blob/main/samples/create_schema.py
85+
[2]: https://github.com/oracle/python-oracledb/blob/main/samples/sample_env.py
86+
[3]: https://github.com/oracle/python-oracledb/blob/main/samples/drop_schema.py

python/python-oracledb/app_context.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#------------------------------------------------------------------------------
2-
# Copyright (c) 2016, 2022, Oracle and/or its affiliates.
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2016, 2023, Oracle and/or its affiliates.
33
#
44
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
55
#
@@ -25,15 +25,15 @@
2525
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2626
# See the License for the specific language governing permissions and
2727
# limitations under the License.
28-
#------------------------------------------------------------------------------
28+
# -----------------------------------------------------------------------------
2929

30-
#------------------------------------------------------------------------------
30+
# -----------------------------------------------------------------------------
3131
# app_context.py
3232
#
3333
# Demonstrates the use of application context. Application context is available
3434
# within logon triggers and can be retrieved by using the function
3535
# sys_context().
36-
#------------------------------------------------------------------------------
36+
# -----------------------------------------------------------------------------
3737

3838
import oracledb
3939
import sample_env
@@ -44,20 +44,22 @@
4444
# client context attributes to be set
4545
APP_CTX_NAMESPACE = "CLIENTCONTEXT"
4646
APP_CTX_ENTRIES = [
47-
( APP_CTX_NAMESPACE, "ATTR1", "VALUE1" ),
48-
( APP_CTX_NAMESPACE, "ATTR2", "VALUE2" ),
49-
( APP_CTX_NAMESPACE, "ATTR3", "VALUE3" )
47+
(APP_CTX_NAMESPACE, "ATTR1", "VALUE1"),
48+
(APP_CTX_NAMESPACE, "ATTR2", "VALUE2"),
49+
(APP_CTX_NAMESPACE, "ATTR3", "VALUE3"),
5050
]
5151

52-
connection = oracledb.connect(user=sample_env.get_main_user(),
53-
password=sample_env.get_main_password(),
54-
dsn=sample_env.get_connect_string(),
55-
appcontext=APP_CTX_ENTRIES)
52+
connection = oracledb.connect(
53+
user=sample_env.get_main_user(),
54+
password=sample_env.get_main_password(),
55+
dsn=sample_env.get_connect_string(),
56+
appcontext=APP_CTX_ENTRIES,
57+
)
5658

5759
with connection.cursor() as cursor:
58-
5960
for namespace, name, value in APP_CTX_ENTRIES:
60-
cursor.execute("select sys_context(:1, :2) from dual",
61-
(namespace, name))
62-
value, = cursor.fetchone()
61+
cursor.execute(
62+
"select sys_context(:1, :2) from dual", (namespace, name)
63+
)
64+
(value,) = cursor.fetchone()
6365
print("Value of context key", name, "is", value)

python/python-oracledb/aq_notification.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#------------------------------------------------------------------------------
2-
# Copyright (c) 2018, 2022, Oracle and/or its affiliates.
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2018, 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
@@ -20,15 +20,15 @@
2020
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2121
# See the License for the specific language governing permissions and
2222
# limitations under the License.
23-
#------------------------------------------------------------------------------
23+
# -----------------------------------------------------------------------------
2424

25-
#------------------------------------------------------------------------------
25+
# -----------------------------------------------------------------------------
2626
# aq_notification.py
2727
#
2828
# Demonstrates using advanced queuing notification. Once this script is
2929
# running, run object_aq.py in another terminal to enqueue a few messages to
3030
# the "DEMO_BOOK_QUEUE" queue.
31-
#------------------------------------------------------------------------------
31+
# -----------------------------------------------------------------------------
3232

3333
import time
3434

@@ -40,6 +40,7 @@
4040

4141
registered = True
4242

43+
4344
def process_messages(message):
4445
global registered
4546
print("Message type:", message.type)
@@ -51,14 +52,20 @@ def process_messages(message):
5152
print("Consumer name:", message.consumer_name)
5253
print("Message id:", message.msgid)
5354

54-
connection = oracledb.connect(user=sample_env.get_main_user(),
55-
password=sample_env.get_main_password(),
56-
dsn=sample_env.get_connect_string(),
57-
events=True)
5855

59-
sub = connection.subscribe(namespace=oracledb.SUBSCR_NAMESPACE_AQ,
60-
name="DEMO_BOOK_QUEUE", callback=process_messages,
61-
timeout=300)
56+
connection = oracledb.connect(
57+
user=sample_env.get_main_user(),
58+
password=sample_env.get_main_password(),
59+
dsn=sample_env.get_connect_string(),
60+
events=True,
61+
)
62+
63+
sub = connection.subscribe(
64+
namespace=oracledb.SUBSCR_NAMESPACE_AQ,
65+
name="DEMO_BOOK_QUEUE",
66+
callback=process_messages,
67+
timeout=300,
68+
)
6269
print("Subscription:", sub)
6370
print("--> Connection:", sub.connection)
6471
print("--> Callback:", sub.callback)

python/python-oracledb/array_dml_rowcounts.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#------------------------------------------------------------------------------
2-
# Copyright (c) 2016, 2022, Oracle and/or its affiliates.
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2016, 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
@@ -20,16 +20,16 @@
2020
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2121
# See the License for the specific language governing permissions and
2222
# limitations under the License.
23-
#------------------------------------------------------------------------------
23+
# -----------------------------------------------------------------------------
2424

25-
#------------------------------------------------------------------------------
25+
# -----------------------------------------------------------------------------
2626
# array_dml_rowcounts.py
2727
#
2828
# Demonstrates the use of the 12.1 feature that allows cursor.executemany()
2929
# to return the number of rows affected by each individual execution as a list.
3030
# The parameter "arraydmlrowcounts" must be set to True in the call to
3131
# cursor.executemany() after which cursor.getarraydmlrowcounts() can be called.
32-
#------------------------------------------------------------------------------
32+
# -----------------------------------------------------------------------------
3333

3434
import oracledb
3535
import sample_env
@@ -38,19 +38,23 @@
3838
if not sample_env.get_is_thin():
3939
oracledb.init_oracle_client(lib_dir=sample_env.get_oracle_client())
4040

41-
connection = oracledb.connect(user=sample_env.get_main_user(),
42-
password=sample_env.get_main_password(),
43-
dsn=sample_env.get_connect_string())
41+
connection = oracledb.connect(
42+
user=sample_env.get_main_user(),
43+
password=sample_env.get_main_password(),
44+
dsn=sample_env.get_connect_string(),
45+
)
4446

4547
with connection.cursor() as cursor:
46-
4748
# show the number of rows for each parent ID as a means of verifying the
4849
# output from the delete statement
49-
for parent_id, count in cursor.execute("""
50-
select ParentId, count(*)
51-
from ChildTable
52-
group by ParentId
53-
order by ParentId"""):
50+
for parent_id, count in cursor.execute(
51+
"""
52+
select ParentId, count(*)
53+
from ChildTable
54+
group by ParentId
55+
order by ParentId
56+
"""
57+
):
5458
print("Parent ID:", parent_id, "has", int(count), "rows.")
5559
print()
5660

@@ -61,11 +65,11 @@
6165
print()
6266

6367
# enable array DML row counts for each iteration executed in executemany()
64-
cursor.executemany("""
65-
delete from ChildTable
66-
where ParentId = :1""",
67-
[(i,) for i in parent_ids_to_delete],
68-
arraydmlrowcounts = True)
68+
cursor.executemany(
69+
"delete from ChildTable where ParentId = :1",
70+
[(i,) for i in parent_ids_to_delete],
71+
arraydmlrowcounts=True,
72+
)
6973

7074
# display the number of rows deleted for each parent ID
7175
row_counts = cursor.getarraydmlrowcounts()
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
# array_dml_rowcounts_async.py
27+
#
28+
# An asynchronous version of array_dml_rowcounts.py
29+
#
30+
# Demonstrates the use of the 12.1 feature that allows cursor.executemany()
31+
# to return the number of rows affected by each individual execution as a list.
32+
# The parameter "arraydmlrowcounts" must be set to True in the call to
33+
# cursor.executemany() after which cursor.getarraydmlrowcounts() can be called.
34+
# -----------------------------------------------------------------------------
35+
36+
import asyncio
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+
with connection.cursor() as cursor:
50+
# show the number of rows for each parent ID as a means of verifying
51+
# the output from the delete statement
52+
await cursor.execute(
53+
"""
54+
select ParentId, count(*)
55+
from ChildTable
56+
group by ParentId
57+
order by ParentId
58+
"""
59+
)
60+
async for parent_id, count in cursor:
61+
print("Parent ID:", parent_id, "has", int(count), "rows.")
62+
print()
63+
64+
# delete the following parent IDs only
65+
parent_ids_to_delete = [20, 30, 50]
66+
67+
print("Deleting Parent IDs:", parent_ids_to_delete)
68+
print()
69+
70+
# enable array DML row counts for each iteration executed in
71+
# executemany()
72+
await cursor.executemany(
73+
"delete from ChildTable where ParentId = :1",
74+
[(i,) for i in parent_ids_to_delete],
75+
arraydmlrowcounts=True,
76+
)
77+
78+
# display the number of rows deleted for each parent ID
79+
row_counts = cursor.getarraydmlrowcounts()
80+
for parent_id, count in zip(parent_ids_to_delete, row_counts):
81+
print("Parent ID:", parent_id, "deleted", count, "rows.")
82+
83+
84+
asyncio.run(main())

0 commit comments

Comments
 (0)