Skip to content

Commit f92163b

Browse files
committed
Sync for Python cx_Oracle 8.2
1 parent 0def3d2 commit f92163b

Some content is hidden

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

57 files changed

+1042
-684
lines changed

python/README.md

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,47 @@
1-
This directory contains samples for cx_Oracle.
1+
# cx_Oracle Examples
2+
3+
This directory contains samples for [cx_Oracle][6]. Documentation is
4+
[here][7]. A separate tutorial is [here][8].
25

36
1. The schemas and SQL objects that are referenced in the samples can be
4-
created by running the Python script [SetupSamples.py][1]. The script
7+
created by running the Python script [setup_samples.py][1]. The script
58
requires SYSDBA privileges and will prompt for these credentials as well as
69
the names of the schemas and edition that will be created, unless a number
710
of environment variables are set as documented in the Python script
8-
[SampleEnv.py][2]. Run the script using the following command:
11+
[sample_env.py][2]. Run the script using the following command:
912

10-
python SetupSamples.py
13+
python setup_samples.py
1114

1215
Alternatively, the [SQL script][3] can be run directly via SQL\*Plus, which
1316
will always prompt for the names of the schemas and edition that will be
1417
created.
1518

16-
sqlplus sys/syspassword@hostname/servicename @sql/SetupSamples.sql
19+
sqlplus sys/syspassword@hostname/servicename @sql/setup_samples.sql
1720

1821
2. Run a Python script, for example:
1922

20-
python Query.py
23+
python query.py
2124

2225
3. After running cx_Oracle samples, the schemas and SQL objects can be
23-
dropped by running the Python script [DropSamples.py][4]. The script
26+
dropped by running the Python script [drop_samples.py][4]. The script
2427
requires SYSDBA privileges and will prompt for these credentials as well as
2528
the names of the schemas and edition that will be dropped, unless a number
2629
of environment variables are set as documented in the Python script
27-
[SampleEnv.py][2]. Run the script using the following command:
30+
[sample_env.py][2]. Run the script using the following command:
2831

29-
python DropSamples.py
32+
python drop_samples.py
3033

3134
Alternatively, the [SQL script][5] can be run directly via SQL\*Plus, which
3235
will always prompt for the names of the schemas and edition that will be
3336
dropped.
3437

35-
sqlplus sys/syspassword@hostname/servicename @sql/DropSamples.sql
38+
sqlplus sys/syspassword@hostname/servicename @sql/drop_samples.sql
3639

37-
[1]: https://github.com/oracle/python-cx_Oracle/blob/master/samples/SetupSamples.py
38-
[2]: https://github.com/oracle/python-cx_Oracle/blob/master/samples/SampleEnv.py
39-
[3]: https://github.com/oracle/python-cx_Oracle/blob/master/samples/sql/SetupSamples.sql
40-
[4]: https://github.com/oracle/python-cx_Oracle/blob/master/samples/DropSamples.py
41-
[5]: https://github.com/oracle/python-cx_Oracle/blob/master/samples/sql/DropSamples.sql
40+
[1]: https://github.com/oracle/python-cx_Oracle/blob/main/samples/setup_samples.py
41+
[2]: https://github.com/oracle/python-cx_Oracle/blob/main/samples/sample_env.py
42+
[3]: https://github.com/oracle/python-cx_Oracle/blob/main/samples/sql/setup_samples.sql
43+
[4]: https://github.com/oracle/python-cx_Oracle/blob/main/samples/drop_samples.py
44+
[5]: https://github.com/oracle/python-cx_Oracle/blob/main/samples/sql/drop_samples.sql
45+
[6]: https://oracle.github.io/python-cx_Oracle/
46+
[7]: http://cx-oracle.readthedocs.org/en/latest/index.html
47+
[8]: https://oracle.github.io/python-cx_Oracle/samples/tutorial/Python-and-Oracle-Database-Scripting-for-the-Future.html

python/app_context.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#------------------------------------------------------------------------------
2-
# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
33
#
44
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
55
#
@@ -8,16 +8,16 @@
88
#------------------------------------------------------------------------------
99

1010
#------------------------------------------------------------------------------
11-
# AppContext.py
11+
# app_context.py
1212
# This script demonstrates the use of application context. Application
1313
# context is available within logon triggers and can be retrieved by using the
1414
# function sys_context().
1515
#
1616
# This script requires cx_Oracle 5.3 and higher.
1717
#------------------------------------------------------------------------------
1818

19-
import cx_Oracle
20-
import SampleEnv
19+
import cx_Oracle as oracledb
20+
import sample_env
2121

2222
# define constants used throughout the script; adjust as desired
2323
APP_CTX_NAMESPACE = "CLIENTCONTEXT"
@@ -27,11 +27,10 @@
2727
( APP_CTX_NAMESPACE, "ATTR3", "VALUE3" )
2828
]
2929

30-
connection = cx_Oracle.connect(SampleEnv.GetMainConnectString(),
31-
appcontext = APP_CTX_ENTRIES)
30+
connection = oracledb.connect(sample_env.get_main_connect_string(),
31+
appcontext=APP_CTX_ENTRIES)
3232
cursor = connection.cursor()
3333
for namespace, name, value in APP_CTX_ENTRIES:
3434
cursor.execute("select sys_context(:1, :2) from dual", (namespace, name))
3535
value, = cursor.fetchone()
3636
print("Value of context key", name, "is", value)
37-

python/aq_notification.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
#------------------------------------------------------------------------------
2-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
#------------------------------------------------------------------------------
44

55
#------------------------------------------------------------------------------
6-
# AdvancedQueuingNotification.py
6+
# aq_notification.py
77
# This script demonstrates using advanced queuing notification. Once this
88
# script is running, use another session to enqueue a few messages to the
99
# "DEMO_BOOK_QUEUE" queue. This is most easily accomplished by running the
10-
# ObjectAQ.py sample.
10+
# object_aq.py sample.
1111
#
1212
# This script requires cx_Oracle 6.4 and higher.
1313
#------------------------------------------------------------------------------
1414

15-
import cx_Oracle
16-
import SampleEnv
17-
import threading
1815
import time
1916

17+
import cx_Oracle as oracledb
18+
import sample_env
19+
2020
registered = True
2121

22-
def ProcessMessages(message):
22+
def process_messages(message):
2323
global registered
2424
print("Message type:", message.type)
25-
if message.type == cx_Oracle.EVENT_DEREG:
25+
if message.type == oracledb.EVENT_DEREG:
2626
print("Deregistration has taken place...")
2727
registered = False
2828
return
2929
print("Queue name:", message.queueName)
3030
print("Consumer name:", message.consumerName)
3131

32-
connection = cx_Oracle.connect(SampleEnv.GetMainConnectString(), events = True)
33-
sub = connection.subscribe(namespace=cx_Oracle.SUBSCR_NAMESPACE_AQ,
34-
name="DEMO_BOOK_QUEUE", callback=ProcessMessages, timeout=300)
32+
connection = oracledb.connect(sample_env.get_main_connect_string(),
33+
events=True)
34+
sub = connection.subscribe(namespace=oracledb.SUBSCR_NAMESPACE_AQ,
35+
name="DEMO_BOOK_QUEUE", callback=process_messages,
36+
timeout=300)
3537
print("Subscription:", sub)
3638
print("--> Connection:", sub.connection)
3739
print("--> Callback:", sub.callback)
@@ -42,4 +44,3 @@ def ProcessMessages(message):
4244
while registered:
4345
print("Waiting for notifications....")
4446
time.sleep(5)
45-
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#------------------------------------------------------------------------------
2-
# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
33
#------------------------------------------------------------------------------
44

55
#------------------------------------------------------------------------------
6-
# ArrayDMLRowCounts.py
6+
# array_dml_rowcounts.py
77
#
88
# Demonstrate the use of the 12.1 feature that allows cursor.executemany()
99
# to return the number of rows affected by each individual execution as a list.
@@ -13,37 +13,36 @@
1313
# This script requires cx_Oracle 5.2 and higher.
1414
#------------------------------------------------------------------------------
1515

16-
import cx_Oracle
17-
import SampleEnv
16+
import cx_Oracle as oracledb
17+
import sample_env
1818

19-
connection = cx_Oracle.connect(SampleEnv.GetMainConnectString())
19+
connection = oracledb.connect(sample_env.get_main_connect_string())
2020
cursor = connection.cursor()
2121

2222
# show the number of rows for each parent ID as a means of verifying the
2323
# output from the delete statement
24-
for parentId, count in cursor.execute("""
24+
for parent_id, count in cursor.execute("""
2525
select ParentId, count(*)
2626
from ChildTable
2727
group by ParentId
2828
order by ParentId"""):
29-
print("Parent ID:", parentId, "has", int(count), "rows.")
29+
print("Parent ID:", parent_id, "has", int(count), "rows.")
3030
print()
3131

3232
# delete the following parent IDs only
33-
parentIdsToDelete = [20, 30, 50]
33+
parent_ids_to_delete = [20, 30, 50]
3434

35-
print("Deleting Parent IDs:", parentIdsToDelete)
35+
print("Deleting Parent IDs:", parent_ids_to_delete)
3636
print()
3737

3838
# enable array DML row counts for each iteration executed in executemany()
3939
cursor.executemany("""
4040
delete from ChildTable
4141
where ParentId = :1""",
42-
[(i,) for i in parentIdsToDelete],
42+
[(i,) for i in parent_ids_to_delete],
4343
arraydmlrowcounts = True)
4444

4545
# display the number of rows deleted for each parent ID
46-
rowCounts = cursor.getarraydmlrowcounts()
47-
for parentId, count in zip(parentIdsToDelete, rowCounts):
48-
print("Parent ID:", parentId, "deleted", count, "rows.")
49-
46+
row_counts = cursor.getarraydmlrowcounts()
47+
for parent_id, count in zip(parent_ids_to_delete, row_counts):
48+
print("Parent ID:", parent_id, "deleted", count, "rows.")

python/batch_errors.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#------------------------------------------------------------------------------
2-
# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
33
#------------------------------------------------------------------------------
44

55
#------------------------------------------------------------------------------
6-
# BatchErrors.py
6+
# batch_errors.py
77
#
88
# Demonstrate the use of the Oracle Database 12.1 feature that allows
99
# cursor.executemany() to complete successfully, even if errors take
@@ -15,14 +15,14 @@
1515
# This script requires cx_Oracle 5.2 and higher.
1616
#------------------------------------------------------------------------------
1717

18-
import cx_Oracle
19-
import SampleEnv
18+
import cx_Oracle as oracledb
19+
import sample_env
2020

21-
connection = cx_Oracle.connect(SampleEnv.GetMainConnectString())
21+
connection = oracledb.connect(sample_env.get_main_connect_string())
2222
cursor = connection.cursor()
2323

2424
# define data to insert
25-
dataToInsert = [
25+
data_to_insert = [
2626
(1016, 10, 'Child B of Parent 10'),
2727
(1017, 10, 'Child C of Parent 10'),
2828
(1018, 20, 'Child D of Parent 20'),
@@ -39,15 +39,15 @@
3939
from ChildTable""")
4040
count, = cursor.fetchone()
4141
print("number of rows in child table:", int(count))
42-
print("number of rows to insert:", len(dataToInsert))
42+
print("number of rows to insert:", len(data_to_insert))
4343

4444
# old method: executemany() with data errors results in stoppage after the
4545
# first error takes place; the row count is updated to show how many rows
4646
# actually succeeded
4747
try:
4848
cursor.executemany("insert into ChildTable values (:1, :2, :3)",
49-
dataToInsert)
50-
except cx_Oracle.DatabaseError as e:
49+
data_to_insert)
50+
except oracledb.DatabaseError as e:
5151
error, = e.args
5252
print("FAILED with error:", error.message)
5353
print("number of rows which succeeded:", cursor.rowcount)
@@ -64,12 +64,12 @@
6464

6565
# new method: executemany() with batch errors enabled (and array DML row counts
6666
# also enabled) results in no immediate error being raised
67-
cursor.executemany("insert into ChildTable values (:1, :2, :3)", dataToInsert,
68-
batcherrors = True, arraydmlrowcounts = True)
67+
cursor.executemany("insert into ChildTable values (:1, :2, :3)",
68+
data_to_insert, batcherrors=True, arraydmlrowcounts=True)
6969

7070
# where errors have taken place, the row count is 0; otherwise it is 1
71-
rowCounts = cursor.getarraydmlrowcounts()
72-
print("Array DML row counts:", rowCounts)
71+
row_counts = cursor.getarraydmlrowcounts()
72+
print("Array DML row counts:", row_counts)
7373

7474
# display the errors that have taken place
7575
errors = cursor.getbatcherrors()

python/bind_insert.py

+56-15
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,75 @@
11
#------------------------------------------------------------------------------
2-
# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
33
#------------------------------------------------------------------------------
44

55
#------------------------------------------------------------------------------
6-
# BindInsert.py
6+
# bind_insert.py
77
#
88
# Demonstrate how to insert a row into a table using bind variables.
99
#------------------------------------------------------------------------------
1010

11-
import cx_Oracle
12-
import SampleEnv
11+
import cx_Oracle as oracledb
12+
import sample_env
1313

14-
connection = cx_Oracle.connect(SampleEnv.GetMainConnectString())
14+
connection = oracledb.connect(sample_env.get_main_connect_string())
1515

16-
rows = [ (1, "First" ),
17-
(2, "Second" ),
18-
(3, "Third" ),
19-
(4, "Fourth" ),
20-
(5, "Fifth" ),
21-
(6, "Sixth" ),
22-
(7, "Seventh" ) ]
16+
#------------------------------------------------------------------------------
17+
# "Bind by position"
18+
#------------------------------------------------------------------------------
19+
20+
rows = [
21+
(1, "First"),
22+
(2, "Second"),
23+
(3, "Third"),
24+
(4, "Fourth"),
25+
(5, None), # Insert a NULL value
26+
(6, "Sixth"),
27+
(7, "Seventh")
28+
]
2329

2430
cursor = connection.cursor()
31+
32+
# predefine maximum string size to avoid data scans and memory reallocations;
33+
# the None value indicates that the default processing can take place
34+
cursor.setinputsizes(None, 20)
35+
2536
cursor.executemany("insert into mytab(id, data) values (:1, :2)", rows)
2637

27-
# Don't commit - this lets us run the demo multiple times
28-
#connection.commit()
38+
#------------------------------------------------------------------------------
39+
# "Bind by name"
40+
#------------------------------------------------------------------------------
41+
42+
rows = [
43+
{"d": "Eighth", "i": 8},
44+
{"d": "Ninth", "i": 9},
45+
{"d": "Tenth", "i": 10}
46+
]
47+
48+
cursor = connection.cursor()
49+
50+
# Predefine maximum string size to avoid data scans and memory reallocations
51+
cursor.setinputsizes(d=20)
2952

53+
cursor.executemany("insert into mytab(id, data) values (:i, :d)", rows)
54+
55+
#------------------------------------------------------------------------------
56+
# Inserting a single bind still needs tuples
57+
#------------------------------------------------------------------------------
58+
59+
rows = [
60+
("Eleventh",),
61+
("Twelth",)
62+
]
63+
64+
cursor = connection.cursor()
65+
cursor.executemany("insert into mytab(id, data) values (11, :1)", rows)
66+
67+
#------------------------------------------------------------------------------
3068
# Now query the results back
69+
#------------------------------------------------------------------------------
70+
71+
# Don't commit - this lets the demo be run multiple times
72+
#connection.commit()
3173

3274
for row in cursor.execute('select * from mytab'):
3375
print(row)
34-

0 commit comments

Comments
 (0)