| 
 | 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