Skip to content

Commit 4f68e33

Browse files
authored
Update Python cx_Oracle examples for cx_Oracle 8.0 (oracle-samples#111)
1 parent 4b4c366 commit 4f68e33

Some content is hidden

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

54 files changed

+520
-538
lines changed

python/AdvancedQueuing.py

Lines changed: 0 additions & 91 deletions
This file was deleted.

python/AdvancedQueuingNotification.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@
66
# AdvancedQueuingNotification.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
9-
# "BOOKS" queue. This is most easily accomplished by running the
10-
# AdvancedQueuing sample.
9+
# "DEMO_BOOK_QUEUE" queue. This is most easily accomplished by running the
10+
# ObjectAQ.py sample.
1111
#
1212
# This script requires cx_Oracle 6.4 and higher.
1313
#------------------------------------------------------------------------------
1414

15-
from __future__ import print_function
16-
1715
import cx_Oracle
1816
import SampleEnv
1917
import threading
2018
import time
2119

2220
registered = True
2321

24-
def callback(message):
22+
def ProcessMessages(message):
2523
global registered
2624
print("Message type:", message.type)
2725
if message.type == cx_Oracle.EVENT_DEREG:
@@ -32,8 +30,8 @@ def callback(message):
3230
print("Consumer name:", message.consumerName)
3331

3432
connection = cx_Oracle.connect(SampleEnv.GetMainConnectString(), events = True)
35-
sub = connection.subscribe(namespace = cx_Oracle.SUBSCR_NAMESPACE_AQ,
36-
name = "BOOKS", callback = callback, timeout = 300)
33+
sub = connection.subscribe(namespace=cx_Oracle.SUBSCR_NAMESPACE_AQ,
34+
name="DEMO_BOOK_QUEUE", callback=ProcessMessages, timeout=300)
3735
print("Subscription:", sub)
3836
print("--> Connection:", sub.connection)
3937
print("--> Callback:", sub.callback)

python/AppContext.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
# This script requires cx_Oracle 5.3 and higher.
1717
#------------------------------------------------------------------------------
1818

19-
from __future__ import print_function
20-
2119
import cx_Oracle
2220
import SampleEnv
2321

python/ArrayDMLRowCounts.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
# This script requires cx_Oracle 5.2 and higher.
1414
#------------------------------------------------------------------------------
1515

16-
from __future__ import print_function
17-
1816
import cx_Oracle
1917
import SampleEnv
2018

python/BatchErrors.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
# This script requires cx_Oracle 5.2 and higher.
1616
#------------------------------------------------------------------------------
1717

18-
from __future__ import print_function
19-
2018
import cx_Oracle
2119
import SampleEnv
2220

python/BindInsert.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
# Demonstrate how to insert a row into a table using bind variables.
99
#------------------------------------------------------------------------------
1010

11-
from __future__ import print_function
12-
1311
import cx_Oracle
1412
import SampleEnv
1513

python/BindQuery.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# special characters or SQL injection attacks.
1313
#------------------------------------------------------------------------------
1414

15-
from __future__ import print_function
16-
1715
import cx_Oracle
1816
import SampleEnv
1917

python/BulkAQ.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
# This script requires cx_Oracle 7.2 and higher.
1717
#------------------------------------------------------------------------------
1818

19-
from __future__ import print_function
20-
2119
import cx_Oracle
2220
import SampleEnv
2321

python/CQN.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
# This script requires cx_Oracle 5.3 and higher.
1818
#------------------------------------------------------------------------------
1919

20-
from __future__ import print_function
21-
2220
import cx_Oracle
2321
import SampleEnv
24-
import threading
2522
import time
2623

2724
registered = True

python/CQN2.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#------------------------------------------------------------------------------
2+
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
#------------------------------------------------------------------------------
4+
5+
#------------------------------------------------------------------------------
6+
# CQN2.py
7+
# This script demonstrates using continuous query notification in Python, a
8+
# feature that is available in Oracle 11g and later. Once this script is
9+
# running, use another session to insert, update or delete rows from the table
10+
# cx_Oracle.TestTempTable and you will see the notification of that change.
11+
#
12+
# This script differs from CQN.py in that it shows how a connection can be
13+
# acquired from a session pool and used to query the changes that have been
14+
# made.
15+
#
16+
# This script requires cx_Oracle 7 or higher.
17+
#------------------------------------------------------------------------------
18+
19+
import cx_Oracle
20+
import SampleEnv
21+
import time
22+
23+
registered = True
24+
25+
def callback(message):
26+
global registered
27+
if not message.registered:
28+
print("Deregistration has taken place...")
29+
registered = False
30+
return
31+
connection = pool.acquire()
32+
for query in message.queries:
33+
for table in query.tables:
34+
if table.rows is None:
35+
print("Too many row changes detected in table", table.name)
36+
continue
37+
numRowsDeleted = 0
38+
print(len(table.rows), "row changes detected in table", table.name)
39+
for row in table.rows:
40+
if row.operation & cx_Oracle.OPCODE_DELETE:
41+
numRowsDeleted += 1
42+
continue
43+
ops = []
44+
if row.operation & cx_Oracle.OPCODE_INSERT:
45+
ops.append("inserted")
46+
if row.operation & cx_Oracle.OPCODE_UPDATE:
47+
ops.append("updated")
48+
cursor = connection.cursor()
49+
cursor.execute("""
50+
select IntCol
51+
from TestTempTable
52+
where rowid = :rid""",
53+
rid=row.rowid)
54+
intCol, = cursor.fetchone()
55+
print(" Row with IntCol", intCol, "was", " and ".join(ops))
56+
if numRowsDeleted > 0:
57+
print(" ", numRowsDeleted, "rows deleted")
58+
print("=" * 60)
59+
60+
pool = cx_Oracle.SessionPool(SampleEnv.GetMainUser(),
61+
SampleEnv.GetMainPassword(), SampleEnv.GetConnectString(), min=2,
62+
max=5, increment=1, events=True, threaded=True)
63+
with pool.acquire() as connection:
64+
sub = connection.subscribe(callback=callback, timeout=1800,
65+
qos=cx_Oracle.SUBSCR_QOS_QUERY | cx_Oracle.SUBSCR_QOS_ROWIDS)
66+
print("Subscription created with ID:", sub.id)
67+
queryId = sub.registerquery("select * from TestTempTable")
68+
print("Registered query with ID:", queryId)
69+
70+
while registered:
71+
print("Waiting for notifications....")
72+
time.sleep(5)

0 commit comments

Comments
 (0)