Skip to content

Commit 3bc9bd2

Browse files
Added sample and test case to demonstrate use of universal rowids.
1 parent 0b01d96 commit 3bc9bd2

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

samples/UniversalRowids.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#------------------------------------------------------------------------------
2+
# Copyright 2017, Oracle and/or its affiliates. All rights reserved.
3+
#
4+
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
5+
#
6+
# Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
7+
# Canada. All rights reserved.
8+
#------------------------------------------------------------------------------
9+
10+
#------------------------------------------------------------------------------
11+
# UniversalRowids.py
12+
# This script demonstrates the use of universal rowids. Universal rowids are
13+
# used to identify rows in index organized tables.
14+
#
15+
# This script requires cx_Oracle 6.0 and higher.
16+
#------------------------------------------------------------------------------
17+
18+
from __future__ import print_function
19+
20+
import cx_Oracle
21+
import datetime
22+
23+
DATA = [
24+
(1, "String #1", datetime.datetime(2017, 4, 4)),
25+
(2, "String #2", datetime.datetime(2017, 4, 5)),
26+
(3, "A" * 250, datetime.datetime(2017, 4, 6))
27+
]
28+
29+
# truncate table so sample can be rerun
30+
connection = cx_Oracle.Connection("cx_Oracle/dev@localhost/orcl")
31+
cursor = connection.cursor()
32+
print("Truncating table...")
33+
cursor.execute("truncate table TestUniversalRowids")
34+
35+
# populate table with a few rows
36+
print("Populating table...")
37+
for row in DATA:
38+
print("Inserting", row)
39+
cursor.execute("insert into TestUniversalRowids values (:1, :2, :3)", row)
40+
connection.commit()
41+
42+
# fetch the rowids from the table
43+
rowids = [r for r, in cursor.execute("select rowid from TestUniversalRowids")]
44+
45+
# fetch each of the rows given the rowid
46+
for rowid in rowids:
47+
print("-" * 79)
48+
print("Rowid:", rowid)
49+
cursor.execute("""
50+
select IntCol, StringCol, DateCol
51+
from TestUniversalRowids
52+
where rowid = :rid""",
53+
rid = rowid)
54+
intCol, stringCol, dateCol = cursor.fetchone()
55+
print("IntCol:", intCol)
56+
print("StringCol:", stringCol)
57+
print("DateCol:", dateCol)
58+

test/SetupTest.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ create table cx_Oracle.TestIntervals (
168168
NullableCol interval day to second
169169
);
170170

171+
create table cx_Oracle.TestUniversalRowids (
172+
IntCol number(9) not null,
173+
StringCol varchar2(250) not null,
174+
DateCol date not null,
175+
constraint TestUniversalRowids_pk primary key (IntCol, StringCol, DateCol)
176+
) organization index;
177+
171178
create table cx_Oracle.TestBuildings (
172179
BuildingId number(9) not null,
173180
BuildingObj cx_Oracle.udt_Building not null

test/StringVar.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
"""Module for testing string variables."""
1212

13+
import datetime
14+
1315
class TestStringVar(BaseTestCase):
1416

1517
def setUp(self):
@@ -157,6 +159,33 @@ def testBindAndFetchRowid(self):
157159
value = rowid)
158160
self.assertEqual(self.cursor.fetchall(), [self.dataByKey[3]])
159161

162+
def testBindAndFetchUniversalRowids(self):
163+
"test binding (and fetching) universal rowids"
164+
self.cursor.execute("truncate table TestUniversalRowids")
165+
data = [
166+
(1, "ABC" * 75, datetime.datetime(2017, 4, 11)),
167+
(2, "DEF" * 80, datetime.datetime(2017, 4, 12))
168+
]
169+
for row in data:
170+
self.cursor.execute("""
171+
insert into TestUniversalRowids
172+
values (:1, :2, :3)""", row)
173+
self.connection.commit()
174+
self.cursor.execute("""
175+
select rowid
176+
from TestUniversalRowIds
177+
order by IntCol""")
178+
rowids = [r for r, in self.cursor]
179+
fetchedData = []
180+
for rowid in rowids:
181+
self.cursor.execute("""
182+
select *
183+
from TestUniversalRowids
184+
where rowid = :rid""",
185+
rid = rowid)
186+
fetchedData.extend(self.cursor.fetchall())
187+
self.assertEqual(fetchedData, data)
188+
160189
def testBindNull(self):
161190
"test binding in a null"
162191
self.cursor.execute("""

0 commit comments

Comments
 (0)