Adjusted tutorial to use new AQ syntax. · dvska/python-cx_Oracle@3c4d83f · GitHub
Skip to content

Commit 3c4d83f

Browse files
Adjusted tutorial to use new AQ syntax.
1 parent e6a825d commit 3c4d83f

5 files changed

Lines changed: 131 additions & 84 deletions

File tree

samples/tutorial/Python-and-Oracle-Database-Scripting-for-the-Future.html

Lines changed: 65 additions & 33 deletions

samples/tutorial/aq.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#------------------------------------------------------------------------------
44

55
#------------------------------------------------------------------------------
6-
# Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
6+
# Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
77
#------------------------------------------------------------------------------
88

99
from __future__ import print_function
@@ -32,7 +32,7 @@
3232
end if;
3333
end;""")
3434

35-
# Create type
35+
# Create a type
3636
print("Creating books type UDT_BOOK...")
3737
cur.execute("""
3838
create type %s as object (
@@ -44,31 +44,39 @@
4444
# Create queue table and queue and start the queue
4545
print("Creating queue table...")
4646
cur.callproc("dbms_aqadm.create_queue_table",
47-
(QUEUE_TABLE_NAME, BOOK_TYPE_NAME))
47+
(QUEUE_TABLE_NAME, BOOK_TYPE_NAME))
4848
cur.callproc("dbms_aqadm.create_queue", (QUEUE_NAME, QUEUE_TABLE_NAME))
4949
cur.callproc("dbms_aqadm.start_queue", (QUEUE_NAME,))
5050

51-
# Enqueue a few messages
5251
booksType = con.gettype(BOOK_TYPE_NAME)
53-
book1 = booksType.newobject()
54-
book1.TITLE = "The Fellowship of the Ring"
55-
book1.AUTHORS = "Tolkien, J.R.R."
56-
book1.PRICE = decimal.Decimal("10.99")
57-
book2 = booksType.newobject()
58-
book2.TITLE = "Harry Potter and the Philosopher's Stone"
59-
book2.AUTHORS = "Rowling, J.K."
60-
book2.PRICE = decimal.Decimal("7.99")
61-
options = con.enqoptions()
62-
messageProperties = con.msgproperties()
63-
for book in (book1, book2):
64-
print("Enqueuing book", book.TITLE)
65-
con.enq(QUEUE_NAME, options, messageProperties, book)
66-
con.commit()
52+
queue = con.queue(QUEUE_NAME, booksType)
53+
54+
# Enqueue a few messages
55+
print("Enqueuing messages...")
56+
57+
BOOK_DATA = [
58+
("The Fellowship of the Ring", "Tolkien, J.R.R.", decimal.Decimal("10.99")),
59+
("Harry Potter and the Philosopher's Stone", "Rowling, J.K.",
60+
decimal.Decimal("7.99"))
61+
]
62+
63+
for title, authors, price in BOOK_DATA:
64+
book = booksType.newobject()
65+
book.TITLE = title
66+
book.AUTHORS = authors
67+
book.PRICE = price
68+
print(title)
69+
queue.enqOne(con.msgproperties(payload=book))
70+
con.commit()
6771

6872
# Dequeue the messages
69-
options = con.deqoptions()
70-
options.navigation = cx_Oracle.DEQ_FIRST_MSG
71-
options.wait = cx_Oracle.DEQ_NO_WAIT
72-
while con.deq(QUEUE_NAME, options, messageProperties, book):
73-
print("Dequeued book", book.TITLE)
74-
con.commit()
73+
print("\nDequeuing messages...")
74+
queue.deqOptions.wait = cx_Oracle.DEQ_NO_WAIT
75+
while True:
76+
props = queue.deqOne()
77+
if not props:
78+
break
79+
print(props.payload.TITLE)
80+
con.commit()
81+
82+
print("\nDone.")

samples/tutorial/solutions/aq-dequeue.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#------------------------------------------------------------------------------
44

55
#------------------------------------------------------------------------------
6-
# Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
6+
# Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
77
#------------------------------------------------------------------------------
88

99
from __future__ import print_function
@@ -20,12 +20,16 @@
2020
QUEUE_TABLE_NAME = "BOOK_QUEUE_TABLE"
2121

2222
# Dequeue the messages
23-
options = con.deqoptions()
24-
options.navigation = cx_Oracle.DEQ_FIRST_MSG
25-
options.wait = cx_Oracle.DEQ_NO_WAIT
26-
messageProperties = con.msgproperties()
2723
booksType = con.gettype(BOOK_TYPE_NAME)
28-
book = booksType.newobject()
29-
while con.deq(QUEUE_NAME, options, messageProperties, book):
30-
print("Dequeued book", book.TITLE)
31-
con.commit()
24+
queue = con.queue(QUEUE_NAME, booksType)
25+
queue.deqOptions.wait = cx_Oracle.DEQ_NO_WAIT
26+
queue.deqOptions.visibility = cx_Oracle.DEQ_IMMEDIATE
27+
28+
print("\nDequeuing messages...")
29+
while True:
30+
props = queue.deqOne()
31+
if not props:
32+
break
33+
print(props.payload.TITLE)
34+
35+
print("\nDone.")

samples/tutorial/solutions/aq-enqueue.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#------------------------------------------------------------------------------
44

55
#------------------------------------------------------------------------------
6-
# Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
6+
# Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
77
#------------------------------------------------------------------------------
88

99
from __future__ import print_function
@@ -20,18 +20,21 @@
2020
QUEUE_TABLE_NAME = "BOOK_QUEUE_TABLE"
2121

2222
# Enqueue a few messages
23+
print("Enqueuing messages...")
24+
25+
BOOK_DATA = [
26+
("The Fellowship of the Ring", "Tolkien, J.R.R.", decimal.Decimal("10.99")),
27+
("Harry Potter and the Philosopher's Stone", "Rowling, J.K.", decimal.Decimal("7.99"))
28+
]
29+
2330
booksType = con.gettype(BOOK_TYPE_NAME)
24-
book1 = booksType.newobject()
25-
book1.TITLE = "The Fellowship of the Ring"
26-
book1.AUTHORS = "Tolkien, J.R.R."
27-
book1.PRICE = decimal.Decimal("10.99")
28-
book2 = booksType.newobject()
29-
book2.TITLE = "Harry Potter and the Philosopher's Stone"
30-
book2.AUTHORS = "Rowling, J.K."
31-
book2.PRICE = decimal.Decimal("7.99")
32-
options = con.enqoptions()
33-
messageProperties = con.msgproperties()
34-
for book in (book1, book2):
35-
print("Enqueuing book", book.TITLE)
36-
con.enq(QUEUE_NAME, options, messageProperties, book)
37-
con.commit()
31+
queue = con.queue(QUEUE_NAME, booksType)
32+
33+
for title, authors, price in BOOK_DATA:
34+
book = booksType.newobject()
35+
book.TITLE = title
36+
book.AUTHORS = authors
37+
book.PRICE = price
38+
print(title)
39+
queue.enqOne(con.msgproperties(payload=book, expiration=4))
40+
con.commit()

samples/tutorial/solutions/aq-queuestart.py

Lines changed: 3 additions & 3 deletions

0 commit comments

Comments
 (0)