Add missing import; clarify example.

This commit is contained in:
Anthony Tuininga 2018-05-09 19:33:26 -06:00
parent 1dbfb60c61
commit 449d6b8b3c

View File

@ -1,5 +1,5 @@
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Copyright 2016, 2017, Oracle and/or its affiliates. All rights reserved. # Copyright 2016, 2018, Oracle and/or its affiliates. All rights reserved.
# #
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved. # Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
# #
@ -9,10 +9,10 @@
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Editioning.py # Editioning.py
# This script demonstrates the use of editioning, available in Oracle # This script demonstrates the use of Edition-Based Redefinition, available
# Database 11.2 and higher. See the Oracle documentation on the subject for # in Oracle# Database 11.2 and higher. See the Oracle documentation on the
# additional information. Adjust the contants at the top of the script for # subject for additional information. Adjust the contants at the top of the
# your own database as needed. # script for your own database as needed.
# #
# This script requires cx_Oracle 5.3 and higher. # This script requires cx_Oracle 5.3 and higher.
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
@ -21,51 +21,57 @@ from __future__ import print_function
import cx_Oracle import cx_Oracle
import SampleEnv import SampleEnv
import os
# connect to the editions user and create a procedure # connect to the editions user and create a procedure
connection = cx_Oracle.Connection(SampleEnv.EDITION_CONNECT_STRING) connection = cx_Oracle.Connection(SampleEnv.EDITION_CONNECT_STRING)
print("Edition should be None at this point, actual value is", print("Edition should be None, actual value is:",
connection.edition) repr(connection.edition))
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(""" cursor.execute("""
create or replace function TestEditions return varchar2 as create or replace function TestEditions return varchar2 as
begin begin
return 'Base Edition'; return 'Base Procedure';
end;""") end;""")
result = cursor.callfunc("TestEditions", str) result = cursor.callfunc("TestEditions", str)
print("Function call should return Base Edition, actually returns", result) print("Function should return 'Base Procedure', actually returns:",
repr(result))
# next, change the edition and recreate the procedure in the new edition # next, change the edition and recreate the procedure in the new edition
cursor.execute("alter session set edition = %s" % SampleEnv.EDITION_NAME) cursor.execute("alter session set edition = %s" % SampleEnv.EDITION_NAME)
print("Edition should be %s at this point, actual value is" % \ print("Edition should be", repr(SampleEnv.EDITION_NAME.upper()),
SampleEnv.EDITION_NAME.upper(), connection.edition) "actual value is:", repr(connection.edition))
cursor.execute(""" cursor.execute("""
create or replace function TestEditions return varchar2 as create or replace function TestEditions return varchar2 as
begin begin
return 'Edition 1'; return 'Edition 1 Procedure';
end;""") end;""")
result = cursor.callfunc("TestEditions", str) result = cursor.callfunc("TestEditions", str)
print("Function call should return Edition 1, actually returns", result) print("Function should return 'Edition 1 Procedure', actually returns:",
repr(result))
# next, change the edition back to the base edition and demonstrate that the # next, change the edition back to the base edition and demonstrate that the
# original function is being called # original function is being called
cursor.execute("alter session set edition = ORA$BASE") cursor.execute("alter session set edition = ORA$BASE")
result = cursor.callfunc("TestEditions", str) result = cursor.callfunc("TestEditions", str)
print("Function call should return Base Edition, actually returns", result) print("Function should return 'Base Procedure', actually returns:",
repr(result))
# the edition can be set upon connection # the edition can be set upon connection
connection = cx_Oracle.Connection(SampleEnv.EDITION_CONNECT_STRING, connection = cx_Oracle.Connection(SampleEnv.EDITION_CONNECT_STRING,
edition = SampleEnv.EDITION_NAME.upper()) edition = SampleEnv.EDITION_NAME.upper())
cursor = connection.cursor() cursor = connection.cursor()
result = cursor.callfunc("TestEditions", str) result = cursor.callfunc("TestEditions", str)
print("Function call should return Edition 1, actually returns", result) print("Function should return 'Edition 1 Procedure', actually returns:",
repr(result))
# it can also be set via the environment variable ORA_EDITION # it can also be set via the environment variable ORA_EDITION
os.environ["ORA_EDITION"] = SampleEnv.EDITION_NAME.upper() os.environ["ORA_EDITION"] = SampleEnv.EDITION_NAME.upper()
connection = cx_Oracle.Connection(SampleEnv.EDITION_CONNECT_STRING) connection = cx_Oracle.Connection(SampleEnv.EDITION_CONNECT_STRING)
print("Edition should be %s at this point, actual value is" % \ print("Edition should be", repr(SampleEnv.EDITION_NAME.upper()),
SampleEnv.EDITION_NAME.upper(), connection.edition) "actual value is:", repr(connection.edition))
cursor = connection.cursor() cursor = connection.cursor()
result = cursor.callfunc("TestEditions", str) result = cursor.callfunc("TestEditions", str)
print("Function call should return Edition 1, actually returns", result) print("Function should return 'Edition 1 Procedure', actually returns:",
repr(result))