Source code for relstorage.adapters.oracle.oidallocator
################################################################################ Copyright (c) 2009 Zope Foundation and Contributors.# All Rights Reserved.## This software is subject to the provisions of the Zope Public License,# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS# FOR A PARTICULAR PURPOSE.###############################################################################"""IOIDAllocator implementations."""from__future__importabsolute_importfromzope.interfaceimportimplementerfrom..._utilimportmetricmethodfrom..interfacesimportIOIDAllocatorfrom..oidallocatorimportAbstractRangedOIDAllocator
[docs]@implementer(IOIDAllocator)classOracleOIDAllocator(AbstractRangedOIDAllocator):def__init__(self,connmanager):self.connmanager=connmanagerdef_set_min_oid_from_range(self,cursor,n):"""Ensure the next OID is at least the given OID."""stmt="SELECT zoid_seq.nextval FROM DUAL"cursor.execute(stmt)next_n=cursor.fetchone()[0]ifnext_n<n:# Oracle provides no way modify the sequence value# except through alter sequence or drop/create sequence,# but either statement kills the current transaction.# Therefore, open a temporary connection to make the# alteration.conn2,cursor2=self.connmanager.open()try:# Change the sequence by altering the increment.# (this is safer than dropping and re-creating the sequence)diff=n-next_ncursor2.execute("ALTER SEQUENCE zoid_seq INCREMENT BY %d"%diff)cursor2.execute("SELECT zoid_seq.nextval FROM DUAL")cursor2.execute("ALTER SEQUENCE zoid_seq INCREMENT BY 1")conn2.commit()finally:self.connmanager.close(conn2,cursor2)
[docs]@metricmethoddefnew_oids(self,cursor):"""Return a sequence of new, unused OIDs."""stmt="SELECT zoid_seq.nextval FROM DUAL"cursor.execute(stmt)n=cursor.fetchone()[0]returnself._oid_range_around(n)